pub mod expiry_sweep;
pub mod history_promotion;
pub mod hnsw_rebuild;
pub mod metrics;
pub mod runner;
pub mod segment_merge;
pub mod throttle;
pub mod tombstone_audit;
pub use expiry_sweep::{ExpirySweep, SweepResult};
pub use history_promotion::{HistoryPromotion, PromotionResult};
pub use hnsw_rebuild::{HnswRebuilder, RebuildResult};
pub use metrics::CompactionMetrics;
pub use runner::{CompactionResult, CompactionRunner};
pub use segment_merge::{MergeResult, SegmentMerger};
pub use throttle::{Throttle, ThrottleConfig};
pub use tombstone_audit::{AuditResult, TombstoneAudit};
#[derive(Debug, Clone)]
pub struct CompactionThresholds {
pub tombstone_ratio_pct: f32,
pub max_segment_count: usize,
pub hnsw_staleness_pct: f32,
}
impl Default for CompactionThresholds {
fn default() -> Self {
Self {
tombstone_ratio_pct: 0.20,
max_segment_count: 10,
hnsw_staleness_pct: 0.15,
}
}
}
#[derive(Debug, Clone)]
pub struct CompactionConfig {
pub thresholds: CompactionThresholds,
pub batch_delay_ms: u64,
pub batch_size: usize,
pub enabled: bool,
pub expiry_sweep_enabled: bool,
pub history_promotion_enabled: bool,
pub history_promotion_age_ms: i64,
}
impl Default for CompactionConfig {
fn default() -> Self {
Self {
thresholds: CompactionThresholds::default(),
batch_delay_ms: 10,
batch_size: 100,
enabled: true,
expiry_sweep_enabled: true,
history_promotion_enabled: true,
history_promotion_age_ms: 90 * 86_400_000,
}
}
}
pub struct CompactionManager {
config: CompactionConfig,
}
impl CompactionManager {
pub fn new(config: CompactionConfig) -> Self {
Self { config }
}
pub fn check_thresholds(&self, metrics: &CompactionMetrics) -> bool {
metrics.exceeds_tombstone_threshold(self.config.thresholds.tombstone_ratio_pct)
|| metrics.exceeds_segment_threshold(self.config.thresholds.max_segment_count)
|| metrics.exceeds_hnsw_staleness_threshold(self.config.thresholds.hnsw_staleness_pct)
}
pub fn config(&self) -> &CompactionConfig {
&self.config
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_thresholds() {
let thresholds = CompactionThresholds::default();
assert!((thresholds.tombstone_ratio_pct - 0.20).abs() < 0.001);
assert_eq!(thresholds.max_segment_count, 10);
assert!((thresholds.hnsw_staleness_pct - 0.15).abs() < 0.001);
}
#[test]
fn default_config() {
let config = CompactionConfig::default();
assert_eq!(config.batch_delay_ms, 10);
assert_eq!(config.batch_size, 100);
assert!(config.enabled);
}
#[test]
fn manager_creation() {
let config = CompactionConfig::default();
let manager = CompactionManager::new(config);
assert!(manager.config().enabled);
}
#[test]
fn check_thresholds_below() {
let manager = CompactionManager::new(CompactionConfig::default());
let metrics = CompactionMetrics {
tombstone_ratio: 0.10, segment_count: 5, hnsw_staleness: 0.05, ..Default::default()
};
assert!(!manager.check_thresholds(&metrics));
}
#[test]
fn check_thresholds_tombstone_exceeded() {
let manager = CompactionManager::new(CompactionConfig::default());
let metrics = CompactionMetrics {
tombstone_ratio: 0.25, segment_count: 5,
hnsw_staleness: 0.05,
..Default::default()
};
assert!(manager.check_thresholds(&metrics));
}
#[test]
fn check_thresholds_segment_exceeded() {
let manager = CompactionManager::new(CompactionConfig::default());
let metrics = CompactionMetrics {
tombstone_ratio: 0.10,
segment_count: 15, hnsw_staleness: 0.05,
..Default::default()
};
assert!(manager.check_thresholds(&metrics));
}
#[test]
fn check_thresholds_hnsw_exceeded() {
let manager = CompactionManager::new(CompactionConfig::default());
let metrics = CompactionMetrics {
tombstone_ratio: 0.10,
segment_count: 5,
hnsw_staleness: 0.20, ..Default::default()
};
assert!(manager.check_thresholds(&metrics));
}
}