Skip to main content

interstice_core/persistence/
config.rs

1//! Persistence configuration for the runtime.
2//!
3//! Controls how transactions are logged and durability behavior.
4
5use std::path::PathBuf;
6
7/// Configuration for transaction log persistence.
8#[derive(Debug, Clone)]
9pub struct PersistenceConfig {
10    /// Enable transaction logging (default: true)
11    pub enabled: bool,
12    /// Path to log directory (default: "./interstice_logs")
13    pub log_dir: PathBuf,
14    /// Sync to disk on every append (default: true, safest but slower)
15    pub sync_on_append: bool,
16}
17
18impl PersistenceConfig {
19    /// Create with default settings (persistence enabled, safe mode)
20    pub fn default_safe() -> Self {
21        Self {
22            enabled: true,
23            log_dir: PathBuf::from("./interstice_logs"),
24            sync_on_append: true,
25        }
26    }
27
28    /// Create with default settings but faster (buffered writes)
29    pub fn default_fast() -> Self {
30        Self {
31            enabled: true,
32            log_dir: PathBuf::from("./interstice_logs"),
33            sync_on_append: false,
34        }
35    }
36
37    /// Create with persistence disabled (in-memory only)
38    pub fn disabled() -> Self {
39        Self {
40            enabled: false,
41            log_dir: PathBuf::from("./interstice_logs"),
42            sync_on_append: true,
43        }
44    }
45
46    /// Set custom log directory
47    pub fn with_log_dir(mut self, dir: PathBuf) -> Self {
48        self.log_dir = dir;
49        self
50    }
51
52    /// Get the path to the main transaction log file
53    pub fn log_file_path(&self) -> PathBuf {
54        self.log_dir.join("transactions.log")
55    }
56}
57
58impl Default for PersistenceConfig {
59    fn default() -> Self {
60        Self::default_safe()
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn test_default_config() {
70        let cfg = PersistenceConfig::default();
71        assert!(cfg.enabled);
72        assert!(cfg.sync_on_append);
73    }
74
75    #[test]
76    fn test_custom_config() {
77        let cfg = PersistenceConfig::disabled().with_log_dir(PathBuf::from("/tmp/logs"));
78        assert!(!cfg.enabled);
79        assert_eq!(cfg.log_dir, PathBuf::from("/tmp/logs"));
80    }
81
82    #[test]
83    fn test_log_file_path() {
84        let cfg = PersistenceConfig::default().with_log_dir(PathBuf::from("./data"));
85        assert_eq!(
86            cfg.log_file_path(),
87            PathBuf::from("./data/transactions.log")
88        );
89    }
90}