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 (enabled by default with 4 connections).
31    ///
32    /// Maintains a pool of read-only connections for concurrent read access
33    /// without blocking writes. Disable with `ReadPoolConfig::default()` if
34    /// you need single-connection behavior.
35    #[serde(default = "default_read_pool")]
36    pub read_pool: ReadPoolConfig,
37}
38
39#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
40pub enum SynchronousMode {
41    /// Full fsync (safest, slowest)
42    Full,
43    /// fsync at critical moments (good balance)
44    #[default]
45    Normal,
46    /// No fsync (fastest, least safe)
47    Off,
48}
49
50fn default_wal_mode() -> bool {
51    true
52}
53
54fn default_schema_version() -> u32 {
55    1
56}
57
58fn default_cache_size() -> i32 {
59    -64000 // 64MB
60}
61
62fn default_read_pool() -> ReadPoolConfig {
63    ReadPoolConfig::enabled(4)
64}
65
66impl ProjectionConfig {
67    pub fn new(path: PathBuf) -> Self {
68        Self {
69            path,
70            wal_mode: default_wal_mode(),
71            synchronous: SynchronousMode::default(),
72            schema_version: default_schema_version(),
73            cache_size: default_cache_size(),
74            read_pool: default_read_pool(),
75        }
76    }
77
78    pub fn with_synchronous(mut self, synchronous: SynchronousMode) -> Self {
79        self.synchronous = synchronous;
80        self
81    }
82
83    pub fn with_wal_mode(mut self, wal_mode: bool) -> Self {
84        self.wal_mode = wal_mode;
85        self
86    }
87
88    /// Configure read connection pooling
89    pub fn with_read_pool(mut self, config: ReadPoolConfig) -> Self {
90        self.read_pool = config;
91        self
92    }
93
94    /// Enable read pooling with the specified pool size
95    pub fn with_read_pool_size(mut self, pool_size: usize) -> Self {
96        self.read_pool = ReadPoolConfig::enabled(pool_size);
97        self
98    }
99}