kevy_embedded/config.rs
1//! Embedded-store configuration. Builder-style — every knob has a sane
2//! default so `Config::default()` works for the simplest use case
3//! (in-memory, no persistence, background TTL reaper).
4
5use std::path::PathBuf;
6use std::time::Duration;
7
8pub use kevy_persist::Fsync as AppendFsync;
9pub use kevy_store::EvictionPolicy;
10
11/// How the active TTL reaper runs.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum TtlReaperMode {
14 /// Spawn a background thread that ticks at the configured interval
15 /// (default 100 ms / 10 Hz, matching Redis's `hz=10`). Default.
16 Background,
17 /// Caller-driven via [`crate::Store::tick`]. Required for WASM
18 /// targets (no threads) and single-threaded apps that don't want a
19 /// background worker.
20 Manual,
21}
22
23/// Embedded-store config. Build by chaining `with_*` methods on
24/// [`Config::default`].
25#[derive(Debug, Clone)]
26pub struct Config {
27 /// Soft memory ceiling in bytes. `0` (default) = unlimited.
28 pub maxmemory: u64,
29 /// Eviction policy when over `maxmemory`. Default `NoEviction`.
30 pub eviction_policy: EvictionPolicy,
31 /// Persistence directory. `None` = pure in-memory (no AOF, no snapshot).
32 pub data_dir: Option<PathBuf>,
33 /// AOF on/off when `data_dir` is set. Defaults to `true` (on) when
34 /// `with_persist` was called; ignored if `data_dir` is `None`.
35 pub aof: bool,
36 /// AOF fsync policy. Default `EverySec` (matches Redis: ≤ 1 s loss).
37 pub appendfsync: AppendFsync,
38 /// Snapshot file name inside `data_dir` (single-shard only; `n > 1`
39 /// always uses `dump-{i}.rdb`). Default `"dump-0.rdb"`. A custom name
40 /// opts the dir out of server interop: no `shards.meta` is recorded,
41 /// and a `kevy` server opening the same dir won't find the files.
42 pub snapshot_filename: String,
43 /// AOF file name inside `data_dir` (single-shard only; `n > 1` always
44 /// uses `aof-{i}.aof`). Default `"aof-0.aof"`. Same interop opt-out as
45 /// [`Self::snapshot_filename`].
46 pub aof_filename: String,
47 /// TTL reaper mode. Default `Background`.
48 pub ttl_reaper: TtlReaperMode,
49 /// Reaper tick interval. Default 100 ms (10 Hz).
50 pub reaper_interval: Duration,
51 /// `tick_expire` samples per round. Default 20 (matches Redis).
52 pub reaper_samples: usize,
53 /// Max sample rounds per tick. Default 16.
54 pub reaper_max_rounds: u32,
55 /// Auto-`BGREWRITEAOF` trigger: rewrite when the live AOF has grown by at
56 /// least this percent over its size at the previous rewrite. `0` disables
57 /// (call [`crate::Store::rewrite_aof`] manually). Default `100` (Redis).
58 pub auto_aof_rewrite_pct: u32,
59 /// Floor below which auto-rewrite is skipped. Default `64 MiB` (Redis).
60 pub auto_aof_rewrite_min_size: u64,
61 /// Optional push-style metric callback (replay / rewrite events). Default
62 /// `None`. Set via [`Self::with_metric_sink`]; not part of `Debug` output.
63 pub(crate) metric_sink: Option<crate::metric::MetricSink>,
64 /// Keyspace shard count (`hash(key) % shards`), each a fully independent
65 /// lock + keyspace + AOF (shared-nothing) — concurrent access scales across
66 /// cores. **Default `1`** (single shard = the original single-lock /
67 /// single-`aof-0.aof` layout, zero migration). Set `> 1` via
68 /// [`Self::with_shards`]; the first open with `> 1` re-shards an existing
69 /// single AOF into per-shard files.
70 pub shards: usize,
71}
72
73impl Default for Config {
74 fn default() -> Self {
75 Self {
76 maxmemory: 0,
77 eviction_policy: EvictionPolicy::NoEviction,
78 data_dir: None,
79 aof: true,
80 appendfsync: AppendFsync::EverySec,
81 snapshot_filename: String::from("dump-0.rdb"),
82 aof_filename: String::from("aof-0.aof"),
83 ttl_reaper: TtlReaperMode::Background,
84 reaper_interval: Duration::from_millis(100),
85 reaper_samples: 20,
86 reaper_max_rounds: 16,
87 auto_aof_rewrite_pct: 100,
88 auto_aof_rewrite_min_size: 64 * 1024 * 1024,
89 metric_sink: None,
90 shards: 1,
91 }
92 }
93}
94
95impl Config {
96 /// Enable persistence under `dir` — snapshot file + AOF land inside.
97 /// AOF defaults on; turn it off with [`Self::without_aof`] for pure
98 /// snapshot-only durability.
99 pub fn with_persist(mut self, dir: impl Into<PathBuf>) -> Self {
100 self.data_dir = Some(dir.into());
101 self
102 }
103
104 /// Disable the AOF (snapshot-only persistence — explicit `save_snapshot`
105 /// calls are the only way data survives restart).
106 pub fn without_aof(mut self) -> Self {
107 self.aof = false;
108 self
109 }
110
111 /// Soft memory ceiling in bytes. `0` keeps the default (unlimited).
112 pub fn with_max_memory(mut self, bytes: u64) -> Self {
113 self.maxmemory = bytes;
114 self
115 }
116
117 /// Eviction policy when over [`Self::with_max_memory`].
118 pub fn with_eviction(mut self, policy: EvictionPolicy) -> Self {
119 self.eviction_policy = policy;
120 self
121 }
122
123 /// AOF fsync policy. Default [`AppendFsync::EverySec`].
124 pub fn with_appendfsync(mut self, fsync: AppendFsync) -> Self {
125 self.appendfsync = fsync;
126 self
127 }
128
129 /// Auto-`BGREWRITEAOF` thresholds: rewrite once the AOF has grown `pct`
130 /// percent past its size at the last rewrite AND is at least `min_size`
131 /// bytes. In `Background` reaper mode the check runs on the reaper tick;
132 /// in `Manual` mode it runs when you call [`crate::Store::tick`]. Pass
133 /// `pct = 0` to disable auto-rewrite (you can still call
134 /// [`crate::Store::rewrite_aof`] yourself). Defaults: 100 % / 64 MiB.
135 pub fn with_auto_aof_rewrite(mut self, pct: u32, min_size: u64) -> Self {
136 self.auto_aof_rewrite_pct = pct;
137 self.auto_aof_rewrite_min_size = min_size;
138 self
139 }
140
141 /// Shard the keyspace into `n` shared-nothing partitions (`hash(key) % n`),
142 /// each with its own lock + keyspace + AOF, so concurrent access scales
143 /// across cores. `n` clamps to ≥ 1; `1` (default) is the original
144 /// single-shard layout. Going from a single-AOF store to `n > 1`
145 /// re-shards the existing `aof-0.aof` into `aof-0..aof-{n-1}` on the next
146 /// open (the old file is backed up to `aof-0.aof.premigration.<ts>` first).
147 /// Pub/sub is process-wide (handled on shard 0), not sharded.
148 pub fn with_shards(mut self, n: usize) -> Self {
149 self.shards = n.max(1);
150 self
151 }
152
153 /// Register a push-style metric callback. It receives a [`crate::KevyMetric`] for
154 /// each AOF replay (startup) and AOF rewrite (compaction) — wire it to
155 /// Prometheus / a log line / a counter. The callback runs synchronously on
156 /// the emitting thread (reaper thread for background rewrites), so keep it
157 /// fast and non-blocking. Replaces any previously-set sink.
158 pub fn with_metric_sink(
159 mut self,
160 sink: impl Fn(crate::KevyMetric) + Send + Sync + 'static,
161 ) -> Self {
162 self.metric_sink = Some(crate::metric::MetricSink::new(sink));
163 self
164 }
165
166 /// Caller-driven TTL reaping — disables the background thread.
167 /// Required for WASM (no threads available). Call
168 /// [`crate::Store::tick`] yourself from your event loop.
169 pub fn with_ttl_reaper_manual(mut self) -> Self {
170 self.ttl_reaper = TtlReaperMode::Manual;
171 self
172 }
173
174 /// Override the background reaper interval. Default 100 ms.
175 pub fn with_reaper_interval(mut self, iv: Duration) -> Self {
176 self.reaper_interval = iv;
177 self
178 }
179
180 /// Override the snapshot file name inside `data_dir`.
181 pub fn with_snapshot_filename(mut self, name: impl Into<String>) -> Self {
182 self.snapshot_filename = name.into();
183 self
184 }
185
186 /// Override the AOF file name inside `data_dir`.
187 pub fn with_aof_filename(mut self, name: impl Into<String>) -> Self {
188 self.aof_filename = name.into();
189 self
190 }
191}
192
193#[cfg(test)]
194mod tests {
195 use super::*;
196
197 #[test]
198 fn default_is_pure_in_memory() {
199 let c = Config::default();
200 assert_eq!(c.maxmemory, 0);
201 assert!(c.data_dir.is_none());
202 assert_eq!(c.ttl_reaper, TtlReaperMode::Background);
203 assert!(c.aof);
204 }
205
206 #[test]
207 fn builder_chains() {
208 let c = Config::default()
209 .with_persist("/tmp/foo")
210 .with_max_memory(1024)
211 .with_eviction(EvictionPolicy::AllKeysLru)
212 .with_ttl_reaper_manual()
213 .with_appendfsync(AppendFsync::Always);
214 assert_eq!(c.data_dir.as_deref(), Some(std::path::Path::new("/tmp/foo")));
215 assert_eq!(c.maxmemory, 1024);
216 assert_eq!(c.eviction_policy, EvictionPolicy::AllKeysLru);
217 assert_eq!(c.ttl_reaper, TtlReaperMode::Manual);
218 }
219
220 #[test]
221 fn without_aof_disables_logging_path() {
222 let c = Config::default().with_persist("/tmp/foo").without_aof();
223 assert!(c.data_dir.is_some());
224 assert!(!c.aof);
225 }
226}