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
305
306
307
308
309
310
311
312
313
314
//! `Db` open-time configuration.
//!
//! Mirrors the [`design.md`](https://github.com/uname-n/obj/blob/master/design.md)
//! `Config` builder pattern. Currently a thin
//! wrapper around `obj_core::pager::Config` plus the M6-specific
//! `busy_timeout` knob.
use Duration;
use CompressionMode;
use Config as PagerConfig;
use SyncMode;
/// Upper bound on the LRU cache size, expressed in 4 KiB frames.
/// [`Config::cache_size`] clamps any request above this ceiling down
/// to it rather than erroring. `4_194_304` frames × 4 KiB = 16 GiB —
/// far above any realistic working set, but bounded so a bogus
/// `usize::MAX` byte count cannot ask the pager to pre-size an
/// absurd cache (Power-of-ten Rule 3 — keep allocation bounds
/// explicit).
pub const MAX_CACHE_FRAMES: usize = 4_194_304;
/// `Db` open-time configuration. Construct via [`Config::default`]
/// and modify with the builder methods.
///
/// `Debug` is implemented manually so the embedded
/// `pager.encryption_key` field never leaks key material — the
/// derived `Debug` on the pager's `Config` already redacts it, but
/// implementing it manually here keeps the redaction story local
/// for the obj-db crate as well.
///
/// # Examples
///
/// Chain the setters from [`Config::default`] and hand the result
/// to [`Db::open_with`](crate::Db::open_with):
///
/// ```
/// # fn main() -> obj::Result<()> {
/// use obj::{Config, Db, SyncMode};
/// use std::time::Duration;
///
/// let dir = tempfile::tempdir()?;
///
/// let cfg = Config::default()
/// // Cache size in bytes. Rounded down to whole 4 KiB pages and
/// // clamped into range. Default: 256 KiB (64 frames).
/// .cache_size(64 * 1024 * 1024)
/// // Durability mode used by the WAL on every commit.
/// // Default: SyncMode::Full (survives system-wide power loss).
/// .sync_mode(SyncMode::Full)
/// // Maximum wait when acquiring the writer / reader lock.
/// // Default: 5 seconds. Beyond the budget, the txn returns
/// // `Err(Error::Busy)` rather than blocking indefinitely.
/// .busy_timeout(Duration::from_secs(2))
/// // Skip the open-time catalog walk. Default: false. Production
/// // callers should leave this alone.
/// .skip_open_check(false)
/// // Cross-process file locking. Default: true.
/// .cross_process_lock(true);
///
/// let _db = Db::open_with(dir.path().join("configured.obj"), cfg)?;
/// # Ok(())
/// # }
/// ```
///
/// Quick reference for when to change each knob:
///
/// - [`Config::cache_size`] — bigger cache for read-heavy
/// workloads on large databases; tiny cache on
/// memory-constrained targets.
/// - [`Config::sync_mode`] — [`SyncMode::Normal`] if you accept
/// losing the last few milliseconds of writes on a power loss;
/// [`SyncMode::Off`] only for tests and benchmarks.
/// - [`Config::busy_timeout`] — shorter when the caller prefers a
/// fast `Error::Busy` to a long wait; longer when contention is
/// rare and you would rather block than retry.
/// - [`Config::skip_open_check`] — leave on in production. The
/// narrow use-cases are fault-injection harnesses, hot-reload
/// tooling that opens the same file many times per second, and
/// developer workflows that have just run a full
/// `integrity_check`.
/// - [`Config::cross_process_lock`] — leave on for any real
/// deployment. The off path is for in-process stress tests where
/// one shared `Db` serves many threads on a single fd.
// `Copy` is intentionally NOT derived: the embedded
// `pager.encryption_key` is key material, and `Copy` would let it be
// duplicated freely and would permanently preclude a future
// `Zeroize`-on-`Drop` impl (issue #31). `Clone` is kept for the
// builder chain.