use std::collections::HashMap;
use nitrite::common::{NitriteModule, NitritePlugin, PluginRegistrar};
use nitrite::errors::NitriteResult;
use nitrite::index::NitriteIndexer;
use crate::distance::Metric;
use crate::indexer::VectorIndexer;
use crate::precision::Precision;
use crate::vector_index::{IndexBackend, VectorIndexConfig};
pub struct VectorModule {
indexer: VectorIndexer,
}
impl VectorModule {
pub fn new(config: VectorIndexConfig) -> Self {
VectorModule {
indexer: VectorIndexer::new(config),
}
}
pub fn builder(dim: usize, metric: Metric) -> VectorModuleBuilder {
VectorModuleBuilder {
config: VectorIndexConfig::new(dim, metric),
per_index: HashMap::new(),
}
}
}
impl NitriteModule for VectorModule {
fn plugins(&self) -> NitriteResult<Vec<NitritePlugin>> {
Ok(vec![NitritePlugin::new(self.indexer.clone())])
}
fn load(&self, plugin_registrar: &PluginRegistrar) -> NitriteResult<()> {
plugin_registrar.register_indexer_plugin(NitriteIndexer::new(self.indexer.clone()))
}
}
pub struct VectorModuleBuilder {
config: VectorIndexConfig,
per_index: HashMap<(String, String), VectorIndexConfig>,
}
impl VectorModuleBuilder {
pub fn index_config(
mut self,
collection: impl Into<String>,
field: impl Into<String>,
config: VectorIndexConfig,
) -> Self {
self.per_index.insert((collection.into(), field.into()), config);
self
}
pub fn backend(mut self, backend: IndexBackend) -> Self {
self.config = self.config.backend(backend);
self
}
pub fn precision(mut self, precision: Precision) -> Self {
self.config = self.config.precision(precision);
self
}
pub fn m(mut self, m: usize) -> Self {
self.config = self.config.with_m(m);
self
}
pub fn ef_construction(mut self, ef: usize) -> Self {
self.config = self.config.with_ef_construction(ef);
self
}
pub fn ef_search(mut self, ef: usize) -> Self {
self.config = self.config.with_ef_search(ef);
self
}
pub fn cache_bytes(mut self, bytes: usize) -> Self {
self.config = self.config.cache_bytes(bytes);
self
}
pub fn degree(mut self, degree: usize) -> Self {
self.config = self.config.degree(degree);
self
}
pub fn build_beam(mut self, beam: usize) -> Self {
self.config = self.config.build_beam(beam);
self
}
pub fn search_beam(mut self, beam: usize) -> Self {
self.config = self.config.search_beam(beam);
self
}
pub fn alpha(mut self, alpha: f32) -> Self {
self.config = self.config.alpha(alpha);
self
}
pub fn pq_subvectors(mut self, m: usize) -> Self {
self.config = self.config.pq_subvectors(m);
self
}
pub fn pq_train_threshold(mut self, n: usize) -> Self {
self.config = self.config.pq_train_threshold(n);
self
}
pub fn consolidate_threshold(mut self, n: usize) -> Self {
self.config = self.config.consolidate_threshold(n);
self
}
pub fn config(&self) -> VectorIndexConfig {
self.config
}
pub fn build(self) -> VectorModule {
VectorModule {
indexer: VectorIndexer::with_configs(self.config, self.per_index),
}
}
}