Skip to main content

ares_vector/
config.rs

1//! Configuration for ares-vector.
2
3use std::path::PathBuf;
4
5/// Configuration for the vector database.
6#[derive(Debug, Clone)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8pub struct Config {
9    /// Path to store data on disk. If None, data is kept in memory only.
10    pub data_path: Option<PathBuf>,
11
12    /// HNSW index configuration.
13    pub hnsw_config: HnswConfig,
14
15    /// Maximum number of vectors per collection (0 = unlimited).
16    pub max_vectors: usize,
17
18    /// Enable automatic persistence (periodic snapshots).
19    pub auto_persist: bool,
20
21    /// Interval for automatic persistence in seconds.
22    pub persist_interval_secs: u64,
23}
24
25impl Default for Config {
26    fn default() -> Self {
27        Self {
28            data_path: None,
29            hnsw_config: HnswConfig::default(),
30            max_vectors: 0,
31            auto_persist: false,
32            persist_interval_secs: 300,
33        }
34    }
35}
36
37impl Config {
38    /// Create an in-memory configuration.
39    ///
40    /// Data will not be persisted and will be lost when the process exits.
41    pub fn memory() -> Self {
42        Self::default()
43    }
44
45    /// Create a persistent configuration.
46    ///
47    /// Data will be stored at the specified path and loaded on startup.
48    pub fn persistent<P: Into<PathBuf>>(path: P) -> Self {
49        Self {
50            data_path: Some(path.into()),
51            auto_persist: true,
52            ..Self::default()
53        }
54    }
55
56    /// Set the HNSW configuration.
57    pub fn with_hnsw_config(mut self, config: HnswConfig) -> Self {
58        self.hnsw_config = config;
59        self
60    }
61
62    /// Set the maximum number of vectors per collection.
63    pub fn with_max_vectors(mut self, max: usize) -> Self {
64        self.max_vectors = max;
65        self
66    }
67
68    /// Enable or disable automatic persistence.
69    pub fn with_auto_persist(mut self, enabled: bool) -> Self {
70        self.auto_persist = enabled;
71        self
72    }
73
74    /// Set the persistence interval in seconds.
75    pub fn with_persist_interval(mut self, secs: u64) -> Self {
76        self.persist_interval_secs = secs;
77        self
78    }
79}
80
81/// HNSW index configuration.
82///
83/// These parameters control the trade-off between search accuracy,
84/// speed, and memory usage.
85#[derive(Debug, Clone)]
86#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
87pub struct HnswConfig {
88    /// Maximum number of connections per element per layer.
89    ///
90    /// Higher values improve search quality but use more memory.
91    /// Typical values: 12-48. Default: 16.
92    pub m: usize,
93
94    /// Maximum number of connections for the first layer.
95    ///
96    /// Usually set to 2 * M. Default: 32.
97    pub m_max: usize,
98
99    /// Size of the dynamic candidate list during construction.
100    ///
101    /// Higher values improve index quality but slow down construction.
102    /// Typical values: 100-500. Default: 200.
103    pub ef_construction: usize,
104
105    /// Size of the dynamic candidate list during search.
106    ///
107    /// Higher values improve search quality but slow down search.
108    /// Must be >= k (number of results). Default: 100.
109    pub ef_search: usize,
110
111    /// Enable parallel index construction.
112    ///
113    /// Uses multiple threads to speed up batch insertions.
114    pub parallel_construction: bool,
115
116    /// Number of threads for parallel operations (0 = auto).
117    pub num_threads: usize,
118}
119
120impl Default for HnswConfig {
121    fn default() -> Self {
122        Self {
123            m: 16,
124            m_max: 32,
125            ef_construction: 200,
126            ef_search: 100,
127            parallel_construction: true,
128            num_threads: 0, // Auto-detect
129        }
130    }
131}
132
133impl HnswConfig {
134    /// Create a configuration optimized for speed.
135    ///
136    /// Lower accuracy but faster search and construction.
137    pub fn fast() -> Self {
138        Self {
139            m: 8,
140            m_max: 16,
141            ef_construction: 100,
142            ef_search: 50,
143            parallel_construction: true,
144            num_threads: 0,
145        }
146    }
147
148    /// Create a configuration optimized for accuracy.
149    ///
150    /// Higher accuracy but slower search and uses more memory.
151    pub fn accurate() -> Self {
152        Self {
153            m: 32,
154            m_max: 64,
155            ef_construction: 400,
156            ef_search: 200,
157            parallel_construction: true,
158            num_threads: 0,
159        }
160    }
161
162    /// Create a configuration optimized for memory efficiency.
163    ///
164    /// Lower memory usage but may have lower accuracy.
165    pub fn memory_efficient() -> Self {
166        Self {
167            m: 8,
168            m_max: 16,
169            ef_construction: 100,
170            ef_search: 64,
171            parallel_construction: false,
172            num_threads: 1,
173        }
174    }
175
176    /// Set the M parameter (connections per layer).
177    pub fn with_m(mut self, m: usize) -> Self {
178        self.m = m;
179        self.m_max = m * 2;
180        self
181    }
182
183    /// Set the ef_construction parameter.
184    pub fn with_ef_construction(mut self, ef: usize) -> Self {
185        self.ef_construction = ef;
186        self
187    }
188
189    /// Set the ef_search parameter.
190    pub fn with_ef_search(mut self, ef: usize) -> Self {
191        self.ef_search = ef;
192        self
193    }
194
195    /// Set the number of threads for parallel operations.
196    pub fn with_num_threads(mut self, n: usize) -> Self {
197        self.num_threads = n;
198        self
199    }
200}
201
202#[cfg(test)]
203mod tests {
204    use super::*;
205
206    #[test]
207    fn test_memory_config() {
208        let config = Config::memory();
209        assert!(config.data_path.is_none());
210        assert!(!config.auto_persist);
211    }
212
213    #[test]
214    fn test_persistent_config() {
215        let config = Config::persistent("/tmp/vectors");
216        assert!(config.data_path.is_some());
217        assert!(config.auto_persist);
218    }
219
220    #[test]
221    fn test_hnsw_presets() {
222        let fast = HnswConfig::fast();
223        let accurate = HnswConfig::accurate();
224
225        assert!(fast.m < accurate.m);
226        assert!(fast.ef_construction < accurate.ef_construction);
227    }
228}