Skip to main content

grafeo_engine/
config.rs

1//! Database configuration.
2
3use std::path::PathBuf;
4
5/// Database configuration.
6#[derive(Debug, Clone)]
7pub struct Config {
8    /// Path to the database directory (None for in-memory only).
9    pub path: Option<PathBuf>,
10
11    /// Memory limit in bytes (None for unlimited).
12    pub memory_limit: Option<usize>,
13
14    /// Path for spilling data to disk under memory pressure.
15    pub spill_path: Option<PathBuf>,
16
17    /// Number of worker threads for query execution.
18    pub threads: usize,
19
20    /// Whether to enable WAL for durability.
21    pub wal_enabled: bool,
22
23    /// WAL flush interval in milliseconds.
24    pub wal_flush_interval_ms: u64,
25
26    /// Whether to maintain backward edges.
27    pub backward_edges: bool,
28
29    /// Whether to enable query logging.
30    pub query_logging: bool,
31}
32
33impl Default for Config {
34    fn default() -> Self {
35        Self {
36            path: None,
37            memory_limit: None,
38            spill_path: None,
39            threads: num_cpus::get(),
40            wal_enabled: true,
41            wal_flush_interval_ms: 100,
42            backward_edges: true,
43            query_logging: false,
44        }
45    }
46}
47
48impl Config {
49    /// Creates a new configuration for an in-memory database.
50    #[must_use]
51    pub fn in_memory() -> Self {
52        Self {
53            path: None,
54            wal_enabled: false,
55            ..Default::default()
56        }
57    }
58
59    /// Creates a new configuration for a persistent database.
60    #[must_use]
61    pub fn persistent(path: impl Into<PathBuf>) -> Self {
62        Self {
63            path: Some(path.into()),
64            wal_enabled: true,
65            ..Default::default()
66        }
67    }
68
69    /// Sets the memory limit.
70    #[must_use]
71    pub fn with_memory_limit(mut self, limit: usize) -> Self {
72        self.memory_limit = Some(limit);
73        self
74    }
75
76    /// Sets the number of worker threads.
77    #[must_use]
78    pub fn with_threads(mut self, threads: usize) -> Self {
79        self.threads = threads;
80        self
81    }
82
83    /// Disables backward edges.
84    #[must_use]
85    pub fn without_backward_edges(mut self) -> Self {
86        self.backward_edges = false;
87        self
88    }
89
90    /// Enables query logging.
91    #[must_use]
92    pub fn with_query_logging(mut self) -> Self {
93        self.query_logging = true;
94        self
95    }
96
97    /// Sets the memory budget as a fraction of system RAM.
98    #[must_use]
99    pub fn with_memory_fraction(mut self, fraction: f64) -> Self {
100        use grafeo_common::memory::buffer::BufferManagerConfig;
101        let system_memory = BufferManagerConfig::detect_system_memory();
102        self.memory_limit = Some((system_memory as f64 * fraction) as usize);
103        self
104    }
105
106    /// Sets the spill directory for out-of-core processing.
107    #[must_use]
108    pub fn with_spill_path(mut self, path: impl Into<PathBuf>) -> Self {
109        self.spill_path = Some(path.into());
110        self
111    }
112}
113
114/// Helper function to get CPU count (fallback implementation).
115mod num_cpus {
116    pub fn get() -> usize {
117        std::thread::available_parallelism()
118            .map(|n| n.get())
119            .unwrap_or(4)
120    }
121}