oxirs_vec/compaction/
config.rs1use super::strategies::CompactionStrategy;
4use serde::{Deserialize, Serialize};
5use std::time::Duration;
6
7#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct CompactionConfig {
10 pub strategy: CompactionStrategy,
12 pub enable_background: bool,
14 pub compaction_interval: Duration,
16 pub fragmentation_threshold: f64,
18 pub batch_size: usize,
20 pub max_concurrent_batches: usize,
22 pub pause_between_batches: Duration,
24 pub max_compaction_duration: Duration,
26 pub min_free_space_bytes: u64,
28 pub enable_verification: bool,
30 pub cpu_priority: u8,
32 pub memory_limit_bytes: u64,
34 pub enable_metrics: bool,
36 pub metrics_retention: Duration,
38}
39
40impl Default for CompactionConfig {
41 fn default() -> Self {
42 Self {
43 strategy: CompactionStrategy::Adaptive,
44 enable_background: true,
45 compaction_interval: Duration::from_secs(3600), fragmentation_threshold: 0.3, batch_size: 1000,
48 max_concurrent_batches: 4,
49 pause_between_batches: Duration::from_millis(100),
50 max_compaction_duration: Duration::from_secs(300), min_free_space_bytes: 100 * 1024 * 1024, enable_verification: true,
53 cpu_priority: 10, memory_limit_bytes: 512 * 1024 * 1024, enable_metrics: true,
56 metrics_retention: Duration::from_secs(7 * 24 * 3600), }
58 }
59}
60
61impl CompactionConfig {
62 pub fn validate(&self) -> anyhow::Result<()> {
64 if self.fragmentation_threshold <= 0.0 || self.fragmentation_threshold > 1.0 {
65 anyhow::bail!("Fragmentation threshold must be in (0.0, 1.0]");
66 }
67 if self.batch_size == 0 {
68 anyhow::bail!("Batch size must be positive");
69 }
70 if self.max_concurrent_batches == 0 {
71 anyhow::bail!("Max concurrent batches must be positive");
72 }
73 if self.cpu_priority > 100 {
74 anyhow::bail!("CPU priority must be in [0, 100]");
75 }
76 Ok(())
77 }
78
79 pub fn development() -> Self {
81 Self {
82 compaction_interval: Duration::from_secs(60), fragmentation_threshold: 0.1, batch_size: 100,
85 pause_between_batches: Duration::from_millis(10),
86 ..Default::default()
87 }
88 }
89
90 pub fn production() -> Self {
92 Self {
93 compaction_interval: Duration::from_secs(7200), fragmentation_threshold: 0.5, batch_size: 5000,
96 max_concurrent_batches: 8,
97 pause_between_batches: Duration::from_millis(500),
98 max_compaction_duration: Duration::from_secs(600), memory_limit_bytes: 2 * 1024 * 1024 * 1024, ..Default::default()
101 }
102 }
103}
104
105#[cfg(test)]
106mod tests {
107 use super::*;
108
109 #[test]
110 fn test_default_config_is_valid() {
111 let config = CompactionConfig::default();
112 assert!(config.validate().is_ok());
113 }
114
115 #[test]
116 fn test_development_config_is_valid() {
117 let config = CompactionConfig::development();
118 assert!(config.validate().is_ok());
119 }
120
121 #[test]
122 fn test_production_config_is_valid() {
123 let config = CompactionConfig::production();
124 assert!(config.validate().is_ok());
125 }
126
127 #[test]
128 fn test_invalid_fragmentation_threshold() {
129 let config = CompactionConfig {
130 fragmentation_threshold: 1.5,
131 ..Default::default()
132 };
133 assert!(config.validate().is_err());
134 }
135
136 #[test]
137 fn test_invalid_batch_size() {
138 let config = CompactionConfig {
139 batch_size: 0,
140 ..Default::default()
141 };
142 assert!(config.validate().is_err());
143 }
144}