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();
assert!(fast.m < accurate.m);
assert!(fast.ef_construction < accurate.ef_construction);
}
}