pub use super::graph::HnswGraph;
#[derive(Debug, Clone)]
pub struct HnswMaintenancePlan {
pub rebuild_layers: u32,
pub reason: String,
}
pub struct HnswMaintenanceScheduler {
pub update_threshold_ratio: f32,
}
impl HnswMaintenanceScheduler {
pub fn new(update_threshold_ratio: f32) -> Self {
Self {
update_threshold_ratio,
}
}
pub fn should_rebuild(
&self,
total_vectors: u64,
updated_vectors: u64,
) -> Option<HnswMaintenancePlan> {
if total_vectors == 0 {
return None;
}
let ratio = updated_vectors as f32 / total_vectors as f32;
if ratio >= self.update_threshold_ratio {
Some(HnswMaintenancePlan {
rebuild_layers: 3,
reason: format!("update ratio {:.2} exceeded", ratio),
})
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hnsw_rebuild_triggers_on_threshold() {
let scheduler = HnswMaintenanceScheduler::new(0.05);
let plan = scheduler.should_rebuild(1000, 60).unwrap();
assert_eq!(plan.rebuild_layers, 3);
}
#[test]
fn hnsw_rebuild_skips_below_threshold() {
let scheduler = HnswMaintenanceScheduler::new(0.10);
assert!(scheduler.should_rebuild(1000, 50).is_none());
}
}