Skip to main content

open_wal/
config.rs

1//! WAL configuration.
2
3/// Configuration for a WAL instance.
4///
5/// Both fields are caller-supplied (§11 of `docs/wal_design_v6.md`). Tests use
6/// tiny sizes to force segment rolls and commit-time splits.
7#[derive(Clone, Copy, Debug)]
8pub struct WalConfig {
9    /// Bytes pre-allocated per segment file. Default in production: 128 MiB.
10    pub segment_size: u64,
11
12    /// Hard upper bound on a single record's payload length.
13    ///
14    /// A record must not span segments (§5.3), so this is constrained by
15    /// `max_record_size + 91 <= segment_size` (64-byte segment header +
16    /// 20-byte record header + up to 7 padding bytes). The bound is written in
17    /// additive form on purpose: the equivalent `segment_size - 91` underflows
18    /// for `segment_size < 91`, which would *bypass* the check. That precondition
19    /// is **not** enforced by this struct; [`Wal::open`](crate::Wal::open)
20    /// validates it and returns
21    /// [`InvalidConfig`](crate::WalError::InvalidConfig) when violated.
22    pub max_record_size: u32,
23}
24
25impl Default for WalConfig {
26    /// Production-oriented defaults: a 128 MiB segment with a 1 MiB max payload.
27    ///
28    /// These match the sizing guidance in §5.3/§11 (payloads ≤ ~1 MiB with a
29    /// 64–128 MiB segment) and satisfy the `max_record_size ≤ segment_size − 91`
30    /// precondition with large headroom, so a default-configured WAL always
31    /// passes `open()`'s validation.
32    fn default() -> Self {
33        WalConfig {
34            segment_size: 128 * 1024 * 1024,
35            max_record_size: 1024 * 1024,
36        }
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    #[test]
45    fn default_satisfies_section_5_3_bound() {
46        // max_record_size + 91 ≤ segment_size (§5.3) must hold for the default,
47        // so a default-configured WAL never trips InvalidConfig at open().
48        // Additive form avoids the segment_size − 91 underflow trap.
49        let c = WalConfig::default();
50        assert!(u64::from(c.max_record_size) + 91 <= c.segment_size);
51    }
52}