Skip to main content

kevy_embedded/
config_stage.rs

1//! The persistence knobs that are derived rather than set one by one.
2//! Split from `config.rs` to keep it under the 500-line cap.
3
4use crate::config::Config;
5#[cfg(feature = "persist")]
6use crate::config::ReplayMode;
7
8impl Config {
9    /// Best-effort replay: recover the good records BEHIND a corrupt v2
10    /// record instead of dropping them (a production incident lost a
11    /// 231 MB well-formed tail over one bad frame). The skip is
12    /// deterministic — length + CRC32C + an exactly-one-command parse must
13    /// all agree before a record is trusted — and the open still reports
14    /// `corrupt` so hosts still alert. [`ReplayMode::Strict`] (the
15    /// default) remains the conservative choice: nothing after the first
16    /// bad byte is trusted.
17    ///
18    /// ```
19    /// use kevy_embedded::{Config, ReplayMode};
20    ///
21    /// let cfg = Config::default().with_replay_mode(ReplayMode::Resync);
22    /// assert_eq!(cfg.replay_mode, ReplayMode::Resync);
23    /// ```
24    #[cfg(feature = "persist")]
25    #[must_use]
26    pub fn with_replay_mode(mut self, mode: ReplayMode) -> Self {
27        self.replay_mode = mode;
28        self
29    }
30
31    /// Stage appends in a ring of `bytes` per shard (a power of two, at
32    /// least 64 KiB), or turn staging off with 0. See
33    /// [`Config::stage_bytes`].
34    ///
35    /// ```
36    /// let config = kevy_embedded::Config::default().with_stage_ring(1 << 20);
37    /// assert_eq!(config.stage_bytes, 1 << 20);
38    /// ```
39    pub fn with_stage_ring(mut self, bytes: u64) -> Self {
40        assert!(
41            bytes == 0 || (bytes.is_power_of_two() && bytes >= 64 * 1024),
42            "a staging ring is 0 or a power of two of at least 64 KiB, not {bytes}"
43        );
44        self.stage_bytes = bytes;
45        self
46    }
47
48    /// Append through a mapping of the AOF (`true`) or through `write()`.
49    /// See [`Config::mapped_aof`].
50    ///
51    /// ```
52    /// let config = kevy_embedded::Config::default().with_mapped_aof(false);
53    /// assert!(!config.mapped_aof);
54    /// ```
55    pub fn with_mapped_aof(mut self, on: bool) -> Self {
56        self.mapped_aof = on;
57        self
58    }
59
60    /// The auto-rewrite rules this config asks for.
61    #[cfg(feature = "persist")]
62    pub(crate) fn rewrite_policy(&self) -> kevy_persist::RewritePolicy {
63        kevy_persist::RewritePolicy::default()
64            .with_pct(self.auto_aof_rewrite_pct)
65            .with_min_size(self.auto_aof_rewrite_min_size)
66            .with_bytes(self.auto_aof_rewrite_bytes)
67            .with_interval_secs(self.auto_aof_rewrite_interval_secs)
68    }
69}