Skip to main content

sentinel_wal/
config.rs

1//! WAL configuration structures and operational modes.
2
3use std::collections::HashMap;
4
5use serde::{Deserialize, Serialize};
6
7/// Failure handling modes for WAL operations.
8///
9/// These modes control how WAL-related failures are handled:
10/// - `Disabled`: WAL operations are skipped entirely
11/// - `Warn`: WAL failures are logged as warnings but don't fail operations
12/// - `Strict`: WAL failures cause operations to fail immediately
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
14#[serde(rename_all = "lowercase")]
15pub enum WalFailureMode {
16    /// WAL operations are completely disabled
17    Disabled,
18    /// WAL failures are logged as warnings but operations continue
19    Warn,
20    /// WAL failures cause operations to fail (default for data integrity)
21    #[default]
22    Strict,
23}
24
25impl std::str::FromStr for WalFailureMode {
26    type Err = String;
27
28    fn from_str(s: &str) -> Result<Self, Self::Err> {
29        match s.to_lowercase().as_str() {
30            "disabled" => Ok(Self::Disabled),
31            "warn" => Ok(Self::Warn),
32            "strict" => Ok(Self::Strict),
33            _ => Err(format!("Invalid WAL failure mode: {}", s)),
34        }
35    }
36}
37
38impl std::fmt::Display for WalFailureMode {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        match *self {
41            Self::Disabled => write!(f, "disabled"),
42            Self::Warn => write!(f, "warn"),
43            Self::Strict => write!(f, "strict"),
44        }
45    }
46}
47
48/// Configuration for WAL operations at the collection level.
49///
50/// This struct defines how WAL should behave for a specific collection,
51/// including operational modes, verification settings, recovery options,
52/// and low-level file management parameters.
53#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
54pub struct CollectionWalConfig {
55    /// Operational mode for WAL write operations (insert/update/delete)
56    pub write_mode:            WalFailureMode,
57    /// Operational mode for WAL verification operations
58    pub verification_mode:     WalFailureMode,
59    /// Whether to automatically verify documents against WAL on read
60    pub auto_verify:           bool,
61    /// Whether to enable WAL-based recovery features
62    pub enable_recovery:       bool,
63    /// Optional maximum WAL file size in bytes
64    pub max_wal_size_bytes:    Option<u64>,
65    /// Optional compression algorithm for rotated WAL files
66    pub compression_algorithm: Option<crate::CompressionAlgorithm>,
67    /// Optional maximum number of records per WAL file
68    pub max_records_per_file:  Option<usize>,
69    /// WAL file format
70    #[serde(default)]
71    pub format:                crate::manager::WalFormat,
72}
73
74impl Default for CollectionWalConfig {
75    fn default() -> Self {
76        Self {
77            write_mode:            WalFailureMode::Strict,
78            verification_mode:     WalFailureMode::Warn,
79            auto_verify:           false,
80            enable_recovery:       true,
81            max_wal_size_bytes:    Some(10 * 1024 * 1024), // 10MB
82            compression_algorithm: Some(crate::CompressionAlgorithm::Zstd),
83            max_records_per_file:  Some(1000),
84            format:                crate::manager::WalFormat::default(),
85        }
86    }
87}
88
89/// Overrides for CollectionWalConfig, where None means "use existing value".
90#[derive(Debug, Clone, Default)]
91pub struct CollectionWalConfigOverrides {
92    pub write_mode:            Option<WalFailureMode>,
93    pub verification_mode:     Option<WalFailureMode>,
94    pub auto_verify:           Option<bool>,
95    pub enable_recovery:       Option<bool>,
96    pub max_wal_size_bytes:    Option<Option<u64>>, // None means don't override, Some(None) means set to None
97    pub compression_algorithm: Option<Option<crate::CompressionAlgorithm>>,
98    pub max_records_per_file:  Option<Option<usize>>,
99    pub format:                Option<crate::manager::WalFormat>,
100    /// Whether to persist the merged configuration to disk (for existing collections)
101    pub persist_overrides:     bool,
102}
103
104impl CollectionWalConfig {
105    /// Apply overrides to this config, returning a new config with overrides applied.
106    pub fn apply_overrides(&self, overrides: &CollectionWalConfigOverrides) -> Self {
107        Self {
108            write_mode:            overrides.write_mode.unwrap_or(self.write_mode),
109            verification_mode:     overrides
110                .verification_mode
111                .unwrap_or(self.verification_mode),
112            auto_verify:           overrides.auto_verify.unwrap_or(self.auto_verify),
113            enable_recovery:       overrides.enable_recovery.unwrap_or(self.enable_recovery),
114            max_wal_size_bytes:    overrides
115                .max_wal_size_bytes
116                .unwrap_or(self.max_wal_size_bytes),
117            compression_algorithm: overrides
118                .compression_algorithm
119                .unwrap_or(self.compression_algorithm),
120            max_records_per_file:  overrides
121                .max_records_per_file
122                .unwrap_or(self.max_records_per_file),
123            format:                overrides.format.unwrap_or(self.format),
124        }
125    }
126}
127
128/// Configuration for WAL operations at the store level.
129///
130/// This struct defines global WAL settings that apply to all collections
131/// in the store, with collection-specific overrides possible.
132#[derive(Debug, Clone, Serialize, Deserialize)]
133pub struct StoreWalConfig {
134    /// Default WAL configuration for collections
135    pub default_collection_config: CollectionWalConfig,
136    /// Collection-specific WAL configurations (overrides defaults)
137    pub collection_configs:        HashMap<String, CollectionWalConfig>,
138    /// Failure handling mode for store-level WAL operations (checkpoints, etc.)
139    pub store_failure_mode:        WalFailureMode,
140    /// Whether to enable automatic store-wide checkpoints
141    pub auto_checkpoint:           bool,
142    /// Interval for automatic checkpoints (in seconds, 0 = disabled)
143    pub checkpoint_interval_secs:  u64,
144    /// Maximum WAL file size before forcing checkpoint (in bytes)
145    pub max_wal_size_bytes:        u64,
146}
147
148impl Default for StoreWalConfig {
149    fn default() -> Self {
150        Self {
151            default_collection_config: CollectionWalConfig::default(),
152            collection_configs:        HashMap::new(),
153            store_failure_mode:        WalFailureMode::Strict,
154            auto_checkpoint:           true,
155            checkpoint_interval_secs:  300,               // 5 minutes
156            max_wal_size_bytes:        100 * 1024 * 1024, // 100MB
157        }
158    }
159}
160
161impl From<CollectionWalConfig> for crate::manager::WalConfig {
162    fn from(config: CollectionWalConfig) -> Self {
163        Self {
164            max_file_size:         config.max_wal_size_bytes,
165            compression_algorithm: config.compression_algorithm,
166            max_records_per_file:  config.max_records_per_file,
167            format:                config.format,
168        }
169    }
170}