use crate::core::error::{Result, VectorError};
use crate::index::vector::hnsw::HnswConfig;
use std::time::Duration;
pub const MAX_SNAPSHOT_RETRIES: usize = 3;
pub const MAX_DELTA_CHAIN_DEPTH: usize = 50;
pub const MIN_CAPACITY_ESTIMATE: usize = 100;
pub const MAX_ACCUMULATED_CHANGES: usize = 100_000;
#[derive(Debug, Clone, PartialEq)]
pub enum RetentionPolicy {
KeepAll,
KeepN(usize),
KeepDuration(Duration),
}
impl Default for RetentionPolicy {
fn default() -> Self {
RetentionPolicy::KeepN(100)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct TemporalVectorConfig {
pub snapshot_strategy: SnapshotStrategy,
pub retention_policy: RetentionPolicy,
pub max_snapshots: usize,
pub full_snapshot_interval: usize,
pub hnsw_config: Option<HnswConfig>,
}
impl TemporalVectorConfig {
pub fn validate(&self) -> Result<()> {
if self.max_snapshots == 0 {
return Err(
VectorError::IndexError("max_snapshots must be at least 1".to_string()).into(),
);
}
Ok(())
}
pub fn default_with_hnsw(hnsw_config: HnswConfig) -> Self {
TemporalVectorConfig {
snapshot_strategy: SnapshotStrategy::TransactionInterval(10),
retention_policy: RetentionPolicy::KeepN(100),
max_snapshots: 100,
full_snapshot_interval: 10,
hnsw_config: Some(hnsw_config),
}
}
pub fn default_temporal_only() -> Self {
TemporalVectorConfig {
snapshot_strategy: SnapshotStrategy::TransactionInterval(10),
retention_policy: RetentionPolicy::KeepN(100),
max_snapshots: 100,
full_snapshot_interval: 10,
hnsw_config: None,
}
}
pub fn with_time_interval(hnsw_config: HnswConfig, interval_secs: u64) -> Self {
TemporalVectorConfig {
snapshot_strategy: SnapshotStrategy::TimeInterval(interval_secs),
retention_policy: RetentionPolicy::KeepN(100),
max_snapshots: 100,
full_snapshot_interval: 10,
hnsw_config: Some(hnsw_config),
}
}
pub fn with_change_threshold(hnsw_config: HnswConfig, threshold: f64) -> Self {
TemporalVectorConfig {
snapshot_strategy: SnapshotStrategy::ChangeThreshold(threshold),
retention_policy: RetentionPolicy::KeepN(100),
max_snapshots: 100,
full_snapshot_interval: 10,
hnsw_config: Some(hnsw_config),
}
}
}
impl Default for TemporalVectorConfig {
fn default() -> Self {
Self::default_temporal_only()
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum SnapshotStrategy {
TransactionInterval(usize),
TimeInterval(u64),
ChangeThreshold(f64),
Hybrid {
transaction_interval: usize,
time_interval_secs: u64,
change_threshold: f64,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum DriftMetric {
#[default]
Cosine,
Euclidean,
Angular,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_temporal_vector_config_default() {
let config = TemporalVectorConfig::default();
let expected = TemporalVectorConfig::default_temporal_only();
assert_eq!(config, expected);
assert_eq!(
config.snapshot_strategy,
SnapshotStrategy::TransactionInterval(10)
);
assert_eq!(config.retention_policy, RetentionPolicy::KeepN(100));
assert_eq!(config.max_snapshots, 100);
assert_eq!(config.full_snapshot_interval, 10);
assert!(config.hnsw_config.is_none());
}
}