use crate::core::error::{Error, Result};
use crate::db::AletheiaDB;
use crate::index::vector::hnsw::HnswConfig;
use crate::index::vector::temporal::TemporalVectorConfig;
#[must_use = "VectorIndexBuilder does nothing unless .enable() is called"]
pub struct VectorIndexBuilder<'a> {
db: &'a AletheiaDB,
property_name: String,
hnsw_config: Option<HnswConfig>,
temporal_config: Option<TemporalVectorConfig>,
}
impl<'a> VectorIndexBuilder<'a> {
pub fn new(db: &'a AletheiaDB, property_name: String) -> Self {
VectorIndexBuilder {
db,
property_name,
hnsw_config: None,
temporal_config: None,
}
}
pub fn hnsw(mut self, config: HnswConfig) -> Self {
self.hnsw_config = Some(config);
self
}
pub fn temporal(mut self, config: TemporalVectorConfig) -> Self {
self.temporal_config = Some(config);
self
}
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn enable(self) -> Result<()> {
let hnsw_config = self
.hnsw_config
.ok_or_else(|| {
Error::Vector(crate::core::error::VectorError::IndexError(
"HNSW configuration is required. Call .hnsw() before .enable()".to_string(),
))
})
.inspect_err(|err| {
err.record_metric();
})?;
if let Some(temporal_config) = self.temporal_config {
let temporal_config_with_hnsw = TemporalVectorConfig {
hnsw_config: Some(hnsw_config),
..temporal_config
};
self.db
.enable_temporal_vector_index(&self.property_name, temporal_config_with_hnsw)
} else {
self.db
.enable_vector_index(&self.property_name, hnsw_config)
}
}
}