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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//! Log configuration.
/// The default maximum record size: 64 MiB.
///
/// Generous enough for any realistic single record, small enough that a
/// corrupt length prefix cannot request a wild allocation.
const DEFAULT_MAX_RECORD_SIZE: u32 = 64 * 1024 * 1024;
/// How [`Wal::iter`](crate::Wal::iter) reacts to a damaged record.
///
/// This governs *iteration*, not what [`Wal::open`](crate::Wal::open) does with
/// damage it finds while opening; that is [`OpenPolicy`]. It matters when a
/// record in the middle of an already-recovered log is damaged — bit rot, say —
/// and you are reading the log back.
/// What opening a log does when its scan stops before the end of the store.
///
/// Opening a log scans it from the head and stops at the first record that is
/// incomplete or fails its checksum. Nothing from that point on is part of the
/// recovered log. The policy decides whether those bytes may be cut.
///
/// The common cause is a torn tail: a crash partway through an append, leaving
/// a final record that was never acknowledged durable. Cutting it loses nothing
/// the caller was promised. The other cause is damage in the middle of the log
/// (bit rot, a bad sector, a missing segment file), where the records after the
/// damage are intact and were acknowledged. Cutting those loses committed data.
/// [`TruncateTail`](OpenPolicy::TruncateTail) cuts in both cases;
/// [`Strict`](OpenPolicy::Strict) cuts only in the first.
///
/// Set it with [`WalConfig::with_open_policy`]. Under either policy,
/// [`Wal::truncated_on_open`](crate::Wal::truncated_on_open) reports how many
/// bytes were cut.
///
/// # Examples
///
/// ```
/// use wal_db::{OpenPolicy, Wal, WalConfig, WalError};
///
/// # fn main() -> Result<(), WalError> {
/// # let dir = tempfile::tempdir().map_err(WalError::from)?;
/// # let path = dir.path().join("log.wal");
/// // Two records, then damage in the first one's payload.
/// let wal = Wal::open(&path)?;
/// wal.append(b"first")?;
/// wal.append(b"second")?;
/// wal.sync()?;
/// drop(wal);
/// let mut bytes = std::fs::read(&path).map_err(WalError::from)?;
/// bytes[8] ^= 0xFF;
/// std::fs::write(&path, &bytes).map_err(WalError::from)?;
///
/// // Strict open refuses, because an intact record follows the damage, and
/// // leaves the file as it was.
/// let strict = WalConfig::new().with_open_policy(OpenPolicy::Strict);
/// let refused = Wal::open_with(&path, strict);
/// assert!(matches!(refused, Err(WalError::Corruption { offset: 0, .. })));
/// assert_eq!(std::fs::read(&path).map_err(WalError::from)?, bytes);
///
/// // The default cuts the log at the damage, intact second record included.
/// let wal = Wal::open(&path)?;
/// assert_eq!(wal.truncated_on_open(), 27);
/// assert!(wal.is_empty());
/// # Ok(())
/// # }
/// ```
/// Tunable parameters for a [`Wal`](crate::Wal).
///
/// `WalConfig` is a builder. Construct it with [`WalConfig::new`] (or
/// [`Default`]), set the parameters you care about with the `with_*` methods,
/// and pass it to [`Wal::open_with`](crate::Wal::open_with) or
/// [`Wal::with_store_and_config`](crate::Wal::with_store_and_config). The
/// builder methods take and return `self`, so they chain.
///
/// New parameters are added here as later milestones land (segment size, sync
/// policy, group-commit window). The builder shape means those additions do not
/// break existing call sites.
///
/// # Examples
///
/// ```
/// use wal_db::WalConfig;
///
/// // Cap records at 1 MiB instead of the 64 MiB default.
/// let config = WalConfig::new().with_max_record_size(1024 * 1024);
/// assert_eq!(config.max_record_size(), 1024 * 1024);
/// ```