use std::path::PathBuf;
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Config {
pub data_path: Option<PathBuf>,
pub hnsw_config: HnswConfig,
pub max_vectors: usize,
pub auto_persist: bool,
pub persist_interval_secs: u64,
}
impl Default for Config {
fn default() -> Self {
Self {
data_path: None,
hnsw_config: HnswConfig::default(),
max_vectors: 0,
auto_persist: false,
persist_interval_secs: 300,
}
}
}
impl Config {
pub fn memory() -> Self {
Self::default()
}
pub fn persistent<P: Into<PathBuf>>(path: P) -> Self {
Self {
data_path: Some(path.into()),
auto_persist: true,
..Self::default()
}
}
pub fn with_hnsw_config(mut self, config: HnswConfig) -> Self {
self.hnsw_config = config;
self
}
pub fn with_max_vectors(mut self, max: usize) -> Self {
self.max_vectors = max;
self
}
pub fn with_auto_persist(mut self, enabled: bool) -> Self {
self.auto_persist = enabled;
self
}
pub fn with_persist_interval(mut self, secs: u64) -> Self {
self.persist_interval_secs = secs;
self
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct HnswConfig {
pub m: usize,
pub m_max: usize,
pub ef_construction: usize,
pub ef_search: usize,
pub parallel_construction: bool,
pub num_threads: usize,
}
impl Default for HnswConfig {
fn default() -> Self {
Self {
m: 16,
m_max: 32,
ef_construction: 200,
ef_search: 100,
parallel_construction: true,
num_threads: 0, }
}
}
impl HnswConfig {
pub fn fast() -> Self {
Self {
m: 8,
m_max: 16,
ef_construction: 100,
ef_search: 50,
parallel_construction: true,
num_threads: 0,
}
}
pub fn accurate() -> Self {
Self {
m: 32,
m_max: 64,
ef_construction: 400,
ef_search: 200,
parallel_construction: true,
num_threads: 0,
}
}
pub fn memory_efficient() -> Self {
Self {
m: 8,
m_max: 16,
ef_construction: 100,
ef_search: 64,
parallel_construction: false,
num_threads: 1,
}
}
pub fn with_m(mut self, m: usize) -> Self {
self.m = m;
self.m_max = m * 2;
self
}
pub fn with_ef_construction(mut self, ef: usize) -> Self {
self.ef_construction = ef;
self
}
pub fn with_ef_search(mut self, ef: usize) -> Self {
self.ef_search = ef;
self
}
pub fn with_num_threads(mut self, n: usize) -> Self {
self.num_threads = n;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_memory_config() {
let config = Config::memory();
assert!(config.data_path.is_none());
assert!(!config.auto_persist);
}
#[test]
fn test_persistent_config() {
let config = Config::persistent("/tmp/vectors");
assert!(config.data_path.is_some());
assert!(config.auto_persist);
}
#[test]
fn test_hnsw_presets() {
let fast = HnswConfig::fast();
let accurate = HnswConfig::accurate();
let memory_efficient = HnswConfig::memory_efficient();
assert!(fast.m < accurate.m);
assert!(fast.ef_construction < accurate.ef_construction);
assert_eq!(memory_efficient.m, 8);
assert!(!memory_efficient.parallel_construction);
assert_eq!(memory_efficient.num_threads, 1);
}
#[test]
fn test_config_default() {
let config = Config::default();
assert!(config.data_path.is_none());
assert_eq!(config.max_vectors, 0);
assert!(!config.auto_persist);
assert_eq!(config.persist_interval_secs, 300);
}
#[test]
fn test_config_builder_methods() {
let hnsw = HnswConfig::fast()
.with_m(12)
.with_ef_construction(150)
.with_ef_search(75);
let config = Config::persistent("/data/vectors")
.with_hnsw_config(hnsw.clone())
.with_max_vectors(10_000)
.with_auto_persist(false)
.with_persist_interval(60);
assert_eq!(config.data_path, Some(PathBuf::from("/data/vectors")));
assert!(!config.auto_persist);
assert_eq!(config.max_vectors, 10_000);
assert_eq!(config.persist_interval_secs, 60);
assert_eq!(config.hnsw_config.m, 12);
assert_eq!(config.hnsw_config.m_max, 24);
assert_eq!(config.hnsw_config.ef_construction, 150);
assert_eq!(config.hnsw_config.ef_search, 75);
}
#[test]
fn test_hnsw_builder_methods() {
let config = HnswConfig::default()
.with_m(20)
.with_ef_construction(300)
.with_ef_search(150)
.with_num_threads(4);
assert_eq!(config.m, 20);
assert_eq!(config.m_max, 40);
assert_eq!(config.ef_construction, 300);
assert_eq!(config.ef_search, 150);
assert_eq!(config.num_threads, 4);
}
#[test]
fn test_hnsw_default() {
let config = HnswConfig::default();
assert_eq!(config.m, 16);
assert_eq!(config.m_max, 32);
assert_eq!(config.ef_construction, 200);
assert_eq!(config.ef_search, 100);
assert!(config.parallel_construction);
assert_eq!(config.num_threads, 0);
}
}