hightower_kv/
config.rs

1use std::thread;
2use std::time::Duration;
3
4/// Configuration for the key-value store
5#[derive(Clone, Debug)]
6pub struct StoreConfig {
7    /// Directory where data files are stored
8    pub data_dir: String,
9    /// Maximum size of a log segment in bytes before rotation
10    pub max_segment_size: u64,
11    /// Interval between compaction runs
12    pub compaction_interval: Duration,
13    /// Interval between fsync operations
14    pub fsync_interval: Duration,
15    /// Whether to emit a snapshot after compaction
16    pub emit_snapshot_after_compaction: bool,
17    /// Number of worker threads for background tasks
18    pub worker_threads: usize,
19}
20
21impl Default for StoreConfig {
22    fn default() -> Self {
23        let default_workers = thread::available_parallelism()
24            .map(|n| n.get())
25            .unwrap_or(2)
26            .max(1);
27        Self {
28            data_dir: ".hightower".to_string(),
29            max_segment_size: 64 * 1024 * 1024,
30            compaction_interval: Duration::from_secs(60),
31            fsync_interval: Duration::from_millis(25),
32            emit_snapshot_after_compaction: true,
33            worker_threads: default_workers,
34        }
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn default_values_are_reasonable() {
44        let cfg = StoreConfig::default();
45        assert_eq!(cfg.data_dir, ".hightower");
46        assert!(cfg.max_segment_size >= 4 * 1024 * 1024);
47        assert!(cfg.compaction_interval >= Duration::from_secs(1));
48        assert!(cfg.fsync_interval <= Duration::from_millis(100));
49        assert!(cfg.emit_snapshot_after_compaction);
50        assert!(cfg.worker_threads >= 1);
51    }
52}