use std::sync::Arc;
use crate::error::Result;
use crate::storage::Storage;
use crate::vector::index::VectorIndex;
use crate::vector::index::config::VectorIndexTypeConfig;
use crate::vector::index::flat::FlatIndex;
use crate::vector::index::hnsw::HnswIndex;
use crate::vector::index::ivf::IvfIndex;
pub struct VectorIndexFactory;
impl VectorIndexFactory {
pub fn create(
storage: Arc<dyn Storage>,
name: &str,
config: VectorIndexTypeConfig,
) -> Result<Box<dyn VectorIndex>> {
match config {
VectorIndexTypeConfig::Flat(flat_config) => {
let index = FlatIndex::create(storage, name, flat_config)?;
Ok(Box::new(index))
}
VectorIndexTypeConfig::HNSW(hnsw_config) => {
let index = HnswIndex::create(storage, name, hnsw_config)?;
Ok(Box::new(index))
}
VectorIndexTypeConfig::IVF(ivf_config) => {
let index = IvfIndex::create(storage, name, ivf_config)?;
Ok(Box::new(index))
}
}
}
pub fn open_or_create(
storage: Arc<dyn Storage>,
name: &str,
config: VectorIndexTypeConfig,
) -> Result<Box<dyn VectorIndex>> {
if storage.file_exists("metadata.json") {
Self::open(storage, name, config)
} else {
Self::create(storage, name, config)
}
}
pub fn open(
storage: Arc<dyn Storage>,
name: &str,
config: VectorIndexTypeConfig,
) -> Result<Box<dyn VectorIndex>> {
match config {
VectorIndexTypeConfig::Flat(flat_config) => {
let index = FlatIndex::open(storage, name, flat_config)?;
Ok(Box::new(index))
}
VectorIndexTypeConfig::HNSW(hnsw_config) => {
let index = HnswIndex::open(storage, name, hnsw_config)?;
Ok(Box::new(index))
}
VectorIndexTypeConfig::IVF(ivf_config) => {
let index = IvfIndex::open(storage, name, ivf_config)?;
Ok(Box::new(index))
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::storage::memory::MemoryStorage;
use crate::storage::memory::MemoryStorageConfig;
use crate::vector::index::config::{
FlatIndexConfig, HnswIndexConfig, IvfIndexConfig, VectorIndexTypeConfig,
};
#[test]
fn test_vector_index_creation() {
let config = VectorIndexTypeConfig::default();
let storage = Arc::new(MemoryStorage::new(MemoryStorageConfig::default()));
let index = VectorIndexFactory::create(storage, "test_index", config).unwrap();
assert!(!index.is_closed());
}
#[test]
fn test_vector_index_open() {
let config = VectorIndexTypeConfig::default();
let storage = Arc::new(MemoryStorage::new(MemoryStorageConfig::default()));
let index =
VectorIndexFactory::create(storage.clone(), "test_index", config.clone()).unwrap();
index.close().unwrap();
let index = VectorIndexFactory::open(storage, "test_index", config).unwrap();
assert!(!index.is_closed());
}
#[test]
fn test_vector_index_stats() {
let config = VectorIndexTypeConfig::default();
let storage = Arc::new(MemoryStorage::new(MemoryStorageConfig::default()));
let index = VectorIndexFactory::create(storage, "test_index", config).unwrap();
let stats = index.stats().unwrap();
assert_eq!(stats.vector_count, 0);
assert_eq!(stats.dimension, 128); assert_eq!(stats.deleted_count, 0);
assert!(stats.last_modified > 0);
}
#[test]
fn test_vector_index_close() {
let config = VectorIndexTypeConfig::default();
let storage = Arc::new(MemoryStorage::new(MemoryStorageConfig::default()));
let index = VectorIndexFactory::create(storage, "test_index", config).unwrap();
assert!(!index.is_closed());
index.close().unwrap();
assert!(index.is_closed());
let result = index.stats();
assert!(result.is_err());
}
#[test]
fn test_vector_index_config() {
let config = VectorIndexTypeConfig::default();
match config {
VectorIndexTypeConfig::Flat(flat) => {
assert_eq!(flat.dimension, 128);
}
_ => panic!("Expected Flat config as default"),
}
}
#[test]
fn test_factory_create_flat() {
let storage = Arc::new(MemoryStorage::new(MemoryStorageConfig::default()));
let config = VectorIndexTypeConfig::Flat(FlatIndexConfig::default());
let index = VectorIndexFactory::create(storage, "flat_index", config).unwrap();
assert!(!index.is_closed());
}
#[test]
fn test_factory_create_hnsw() {
let storage = Arc::new(MemoryStorage::new(MemoryStorageConfig::default()));
let config = VectorIndexTypeConfig::HNSW(HnswIndexConfig::default());
let index = VectorIndexFactory::create(storage, "hnsw_index", config).unwrap();
assert!(!index.is_closed());
}
#[test]
fn test_factory_create_ivf() {
let storage = Arc::new(MemoryStorage::new(MemoryStorageConfig::default()));
let config = VectorIndexTypeConfig::IVF(IvfIndexConfig::default());
let index = VectorIndexFactory::create(storage, "ivf_index", config).unwrap();
assert!(!index.is_closed());
}
}