Skip to main content

azoth_core/config/
projection.rs

1use super::canonical::ReadPoolConfig;
2use serde::{Deserialize, Serialize};
3use std::path::PathBuf;
4
5/// Configuration for projection store
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct ProjectionConfig {
8    /// Path to the SQLite database file
9    pub path: PathBuf,
10
11    /// Enable WAL mode
12    /// Default: true
13    #[serde(default = "default_wal_mode")]
14    pub wal_mode: bool,
15
16    /// SQLite synchronous mode
17    #[serde(default)]
18    pub synchronous: SynchronousMode,
19
20    /// Target schema version for migrations
21    /// Default: 1
22    #[serde(default = "default_schema_version")]
23    pub schema_version: u32,
24
25    /// SQLite cache size (in pages, negative = KB)
26    /// Default: -64000 (64MB)
27    #[serde(default = "default_cache_size")]
28    pub cache_size: i32,
29
30    /// Read pool configuration (optional, disabled by default)
31    ///
32    /// When enabled, maintains a pool of read-only connections for
33    /// concurrent read access without blocking writes.
34    #[serde(default)]
35    pub read_pool: ReadPoolConfig,
36}
37
38#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
39pub enum SynchronousMode {
40    /// Full fsync (safest, slowest)
41    Full,
42    /// fsync at critical moments (good balance)
43    #[default]
44    Normal,
45    /// No fsync (fastest, least safe)
46    Off,
47}
48
49fn default_wal_mode() -> bool {
50    true
51}
52
53fn default_schema_version() -> u32 {
54    1
55}
56
57fn default_cache_size() -> i32 {
58    -64000 // 64MB
59}
60
61impl ProjectionConfig {
62    pub fn new(path: PathBuf) -> Self {
63        Self {
64            path,
65            wal_mode: default_wal_mode(),
66            synchronous: SynchronousMode::default(),
67            schema_version: default_schema_version(),
68            cache_size: default_cache_size(),
69            read_pool: ReadPoolConfig::default(),
70        }
71    }
72
73    pub fn with_synchronous(mut self, synchronous: SynchronousMode) -> Self {
74        self.synchronous = synchronous;
75        self
76    }
77
78    pub fn with_wal_mode(mut self, wal_mode: bool) -> Self {
79        self.wal_mode = wal_mode;
80        self
81    }
82
83    /// Configure read connection pooling
84    pub fn with_read_pool(mut self, config: ReadPoolConfig) -> Self {
85        self.read_pool = config;
86        self
87    }
88
89    /// Enable read pooling with the specified pool size
90    pub fn with_read_pool_size(mut self, pool_size: usize) -> Self {
91        self.read_pool = ReadPoolConfig::enabled(pool_size);
92        self
93    }
94}