1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use std::fs::File;
use std::io::{BufWriter, Write};
use crate::Location;
/// Writer for appending data to an anonymous temp file.
#[must_use = "call `finish()` to flush the backing file"]
pub struct FileArenaWriter {
file_index: u16,
writer: BufWriter<File>,
cursor: usize,
}
impl FileArenaWriter {
/// Creates a new writer backed by an anonymous temp file.
///
/// `file_index` is the index this writer's file will occupy in the
/// `FileArena`. It is baked into every `Location` returned by `push()`.
///
/// Uses a default buffer size of 8KB. For custom buffer sizes, use
/// [`with_capacity`](Self::with_capacity).
///
/// # Errors
///
/// Returns an error if the temporary file cannot be created.
pub fn new(file_index: u16) -> std::io::Result<Self> {
Self::with_capacity(file_index, 8 * 1024)
}
/// Creates a new writer with a custom buffer size.
///
/// `file_index` is the index this writer's file will occupy in the
/// `FileArena`. `capacity` is the buffer size in bytes.
///
/// Larger buffers reduce syscalls for sequential writes but use more memory.
/// Smaller buffers flush more frequently, which may be better for crash
/// recovery or when memory is constrained.
///
/// # Errors
///
/// Returns an error if the temporary file cannot be created.
pub fn with_capacity(file_index: u16, capacity: usize) -> std::io::Result<Self> {
let file = tempfile::tempfile()?;
let writer = BufWriter::with_capacity(capacity, file);
Ok(Self {
file_index,
writer,
cursor: 0,
})
}
/// Appends data, returning a [`Location`] pointing to the written bytes.
///
/// # Errors
///
/// Returns an error if writing to the underlying file fails.
pub fn push(&mut self, data: impl AsRef<[u8]>) -> std::io::Result<Location> {
let data = data.as_ref();
if data.is_empty() {
return Ok(Location::new(self.file_index, self.cursor, 0));
}
let offset = self.cursor;
self.writer.write_all(data)?;
self.cursor += data.len();
Ok(Location::new(self.file_index, offset, data.len()))
}
/// Returns the current cursor position (total bytes written).
#[must_use]
pub fn len(&self) -> usize {
self.cursor
}
/// Returns true if no data has been written.
#[must_use]
pub fn is_empty(&self) -> bool {
self.cursor == 0
}
/// Returns the file index for this writer.
#[must_use]
pub fn file_index(&self) -> u16 {
self.file_index
}
/// Flushes the writer and returns the underlying file.
///
/// Always returns a file — empty writers return a 0-byte file.
///
/// # Errors
///
/// Returns an error if flushing the buffer fails.
pub fn finish(mut self) -> std::io::Result<File> {
self.writer.flush()?;
self.writer.into_inner().map_err(|e| e.into_error())
}
/// Flushes the writer and wraps its file in a single-file [`FileArena`].
///
/// Convenience for the common case where one writer produces one arena.
/// For multi-writer workflows, use [`finish`](Self::finish) instead.
///
/// # Errors
///
/// Returns an error if flushing the buffer fails.
pub fn into_arena(self) -> std::io::Result<crate::FileArena> {
let file = self.finish()?;
crate::FileArena::new(vec![file])
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn file_index_getter() {
assert_eq!(FileArenaWriter::new(7).unwrap().file_index(), 7);
}
#[test]
fn push_returns_location_with_correct_file_index() {
let mut writer = FileArenaWriter::new(3).unwrap();
let loc = writer.push("hello").unwrap();
assert_eq!(loc.file_index(), 3);
assert_eq!(loc.offset(), 0);
assert_eq!(loc.len(), 5);
}
#[test]
fn push_advances_offset() {
let mut writer = FileArenaWriter::new(0).unwrap();
let loc1 = writer.push("abc").unwrap();
let loc2 = writer.push("de").unwrap();
assert_eq!(loc1.offset(), 0);
assert_eq!(loc1.len(), 3);
assert_eq!(loc2.offset(), 3);
assert_eq!(loc2.len(), 2);
}
#[test]
fn push_empty_returns_zero_location() {
let mut writer = FileArenaWriter::new(1).unwrap();
let loc = writer.push("").unwrap();
assert_eq!(loc.len(), 0);
assert_eq!(loc.offset(), 0);
assert!(writer.is_empty());
}
#[test]
fn push_empty_after_data_uses_current_cursor() {
let mut writer = FileArenaWriter::new(0).unwrap();
let loc1 = writer.push("hello").unwrap();
assert_eq!(loc1.offset(), 0);
assert_eq!(loc1.len(), 5);
assert_eq!(writer.len(), 5);
// Empty push after data should use current cursor (5)
let loc2 = writer.push("").unwrap();
assert_eq!(loc2.len(), 0);
assert_eq!(loc2.offset(), 5);
let loc3 = writer.push("world").unwrap();
assert_eq!(loc3.offset(), 5);
assert_eq!(loc3.len(), 5);
}
#[test]
fn finish_empty_writer_returns_file() {
// No Option — always returns a File even when nothing was written
let writer = FileArenaWriter::new(0).unwrap();
let file = writer.finish().unwrap();
// Can confirm it's a 0-byte file
assert_eq!(file.metadata().unwrap().len(), 0);
}
#[test]
fn finish_returns_file_after_push() {
let mut writer = FileArenaWriter::new(0).unwrap();
writer.push("data").unwrap();
let file = writer.finish().unwrap();
assert_eq!(file.metadata().unwrap().len(), 4);
}
}