do_memory_storage_redb/persistence/
config.rs1use std::path::PathBuf;
4use std::time::Duration;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
8pub enum PersistenceMode {
9 Full,
11 Incremental,
13 #[default]
15 Hybrid,
16 Disabled,
18}
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
22pub enum PersistenceStrategy {
23 Immediate,
25 #[default]
27 Interval,
28 OnShutdown,
30 Threshold,
32}
33
34#[derive(Debug, Clone)]
36pub struct PersistenceConfig {
37 pub enabled: bool,
39 pub persistence_path: PathBuf,
41 pub mode: PersistenceMode,
43 pub strategy: PersistenceStrategy,
45 pub save_interval: Duration,
47 pub min_entries_threshold: usize,
49 pub max_entries: usize,
51 pub compression_enabled: bool,
53 pub compression_level: u32,
55 pub encryption_enabled: bool,
57 pub max_snapshot_age_secs: u64,
59 pub backup_count: usize,
61}
62
63impl Default for PersistenceConfig {
64 fn default() -> Self {
65 Self {
66 enabled: true,
67 persistence_path: PathBuf::from("./cache.redb.snapshot"),
68 mode: PersistenceMode::default(),
69 strategy: PersistenceStrategy::default(),
70 save_interval: Duration::from_secs(300), min_entries_threshold: 100,
72 max_entries: 100_000,
73 compression_enabled: true,
74 compression_level: 6,
75 encryption_enabled: false,
76 max_snapshot_age_secs: 3600, backup_count: 3,
78 }
79 }
80}
81
82impl PersistenceConfig {
83 pub fn with_path(path: impl Into<PathBuf>) -> Self {
85 Self {
86 persistence_path: path.into(),
87 ..Default::default()
88 }
89 }
90
91 pub fn disabled() -> Self {
93 Self {
94 enabled: false,
95 ..Default::default()
96 }
97 }
98
99 pub fn with_immediate_strategy(mut self) -> Self {
101 self.strategy = PersistenceStrategy::Immediate;
102 self
103 }
104
105 pub fn with_interval(mut self, interval: Duration) -> Self {
107 self.strategy = PersistenceStrategy::Interval;
108 self.save_interval = interval;
109 self
110 }
111
112 pub fn with_shutdown_strategy(mut self) -> Self {
114 self.strategy = PersistenceStrategy::OnShutdown;
115 self
116 }
117
118 pub fn with_min_entries(mut self, threshold: usize) -> Self {
120 self.min_entries_threshold = threshold;
121 self
122 }
123
124 pub fn with_max_entries(mut self, max: usize) -> Self {
126 self.max_entries = max;
127 self
128 }
129
130 pub fn with_compression(mut self, enabled: bool) -> Self {
132 self.compression_enabled = enabled;
133 self
134 }
135
136 pub fn with_compression_level(mut self, level: u32) -> Self {
138 self.compression_level = level.clamp(0, 9);
139 self
140 }
141
142 pub fn with_backup_count(mut self, count: usize) -> Self {
144 self.backup_count = count;
145 self
146 }
147}
148
149#[cfg(test)]
150mod tests {
151 use super::*;
152
153 #[test]
154 fn test_default_config() {
155 let config = PersistenceConfig::default();
156 assert!(config.enabled);
157 assert_eq!(config.mode, PersistenceMode::Hybrid);
158 assert_eq!(config.strategy, PersistenceStrategy::Interval);
159 assert_eq!(config.save_interval, Duration::from_secs(300));
160 assert_eq!(config.min_entries_threshold, 100);
161 assert!(config.compression_enabled);
162 assert_eq!(config.compression_level, 6);
163 assert!(!config.encryption_enabled);
164 }
165
166 #[test]
167 fn test_disabled_config() {
168 let config = PersistenceConfig::disabled();
169 assert!(!config.enabled);
170 }
171
172 #[test]
173 fn test_builder_methods() {
174 let config = PersistenceConfig::default()
175 .with_immediate_strategy()
176 .with_min_entries(50)
177 .with_max_entries(1000)
178 .with_compression(false)
179 .with_compression_level(9)
180 .with_backup_count(5);
181
182 assert_eq!(config.strategy, PersistenceStrategy::Immediate);
183 assert_eq!(config.min_entries_threshold, 50);
184 assert_eq!(config.max_entries, 1000);
185 assert!(!config.compression_enabled);
186 assert_eq!(config.compression_level, 9);
187 assert_eq!(config.backup_count, 5);
188 }
189
190 #[test]
191 fn test_interval_config() {
192 let config = PersistenceConfig::default().with_interval(Duration::from_secs(60));
193
194 assert_eq!(config.strategy, PersistenceStrategy::Interval);
195 assert_eq!(config.save_interval, Duration::from_secs(60));
196 }
197
198 #[test]
199 fn test_shutdown_strategy() {
200 let config = PersistenceConfig::default().with_shutdown_strategy();
201
202 assert_eq!(config.strategy, PersistenceStrategy::OnShutdown);
203 }
204
205 #[test]
206 fn test_compression_level_clamping() {
207 let config = PersistenceConfig::default().with_compression_level(15);
208
209 assert_eq!(config.compression_level, 9);
210
211 let config = PersistenceConfig::default().with_compression_level(5);
212
213 assert_eq!(config.compression_level, 5);
214 }
215}