lora_wal/config.rs
1use std::path::PathBuf;
2
3/// Durability mode for committed transactions.
4///
5/// The current release has at most one committer at a time. The enum still
6/// names the durability strategies we want long term, but no mode requires a
7/// background thread today. [`SyncMode::PerCommit`] is the default.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
9pub enum SyncMode {
10 /// `fsync` the active segment before the committing thread releases the
11 /// store write lock. The strongest durability guarantee the WAL offers;
12 /// every observed query result is fully durable on native filesystems.
13 /// On `wasm32-unknown-unknown`, fsync is intentionally a no-op.
14 #[default]
15 PerCommit,
16
17 /// Write commit bytes to the OS immediately, but defer `fsync` until an
18 /// explicit `force_fsync`, checkpoint, `Database::sync`, or clean WAL
19 /// drop. The interval is retained as part of the public configuration so a
20 /// later release can add a background/group flusher without changing
21 /// callers, but it is not scheduled in the single-threaded release.
22 ///
23 /// A future background fsync failure will poison the WAL through the
24 /// existing `Wal::bg_failure` surface. In this release, Group mode is
25 /// cooperative, so that field remains `None` unless another caller poisons
26 /// the WAL.
27 Group { interval_ms: u32 },
28
29 /// Append but never `fsync` from the WAL; rely on whatever the OS
30 /// happens to flush. Intended for CDC-only deployments where the WAL
31 /// is consumed by an external reader and durability is provided
32 /// elsewhere.
33 None,
34}
35
36/// Configuration for opening a [`Wal`](crate::wal::Wal).
37///
38/// `Disabled` is the zero-overhead variant `lora-database` falls back to
39/// when the operator has not configured a WAL directory; it does not
40/// install a recorder or open any files.
41#[derive(Debug, Clone, Default)]
42pub enum WalConfig {
43 #[default]
44 Disabled,
45 Enabled {
46 dir: PathBuf,
47 sync_mode: SyncMode,
48 /// Target size of an active segment before rotation. Rotation
49 /// only happens at a `TxBegin` boundary so a transaction never
50 /// spans segments.
51 segment_target_bytes: u64,
52 },
53}
54
55impl WalConfig {
56 /// Quick constructor for the default `PerCommit` mode and an 8 MiB
57 /// segment target.
58 pub fn enabled(dir: impl Into<PathBuf>) -> Self {
59 Self::Enabled {
60 dir: dir.into(),
61 sync_mode: SyncMode::PerCommit,
62 segment_target_bytes: 8 * 1024 * 1024,
63 }
64 }
65}