mcpmem_core/storage.rs
1/// How aggressively to push WAL writes to durable storage before acknowledging
2/// the client.
3///
4/// The default [`Async`](Durability::Async) flushes to the kernel page cache
5/// and returns immediately; the background sync thread calls `fsync` within
6/// ~1 second. Journal-mode filesystems (ext4, APFS, NTFS) typically absorb a
7/// power loss within that window.
8///
9/// [`Sync`](Durability::Sync) calls `fsync` before returning, confirming the
10/// data is on stable media. Use this when every write must survive an immediate
11/// power failure, at the cost of higher write latency.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum Durability {
14 Async,
15 Sync,
16}
17
18impl Durability {
19 pub const fn is_sync(self) -> bool {
20 matches!(self, Durability::Sync)
21 }
22}
23
24impl std::str::FromStr for Durability {
25 type Err = String;
26 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
27 match s {
28 "async" | "Async" => Ok(Durability::Async),
29 "sync" | "Sync" => Ok(Durability::Sync),
30 _ => Err(format!(
31 "unknown durability '{s}'; expected 'async' or 'sync'"
32 )),
33 }
34 }
35}
36
37/// Tunable SQLite pragmas applied when opening the database. `page_size` and
38/// `auto_vacuum` only take effect on a freshly-created database (they are fixed
39/// once the file has content); the rest apply on every open. Defaults target a
40/// Linux host (4 KiB pages match the OS page / filesystem block size).
41#[derive(Debug, Clone, Copy)]
42pub struct SqliteTuning {
43 /// `PRAGMA mmap_size` in bytes.
44 pub mmap_size: i64,
45 /// `PRAGMA page_size` in bytes (fresh DB only). Must be a power of two.
46 pub page_size: i64,
47 /// `PRAGMA cache_size` magnitude in KiB (applied as the negative form).
48 pub cache_size_kb: i64,
49 /// `PRAGMA busy_timeout` in milliseconds.
50 pub busy_timeout_ms: u64,
51 /// `PRAGMA journal_size_limit` in bytes.
52 pub journal_size_limit: i64,
53}
54
55impl Default for SqliteTuning {
56 fn default() -> Self {
57 Self {
58 mmap_size: 268_435_456, // 256 MiB
59 page_size: 4096, // 4 KiB — matches Linux page/fs block
60 cache_size_kb: 50_000, // ~50 MiB
61 busy_timeout_ms: 5000,
62 journal_size_limit: 134_217_728, // 128 MiB
63 }
64 }
65}