1use std::path::PathBuf;
4
5#[derive(Debug, Clone)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8pub struct Config {
9 pub data_path: Option<PathBuf>,
11
12 pub hnsw_config: HnswConfig,
14
15 pub max_vectors: usize,
17
18 pub auto_persist: bool,
20
21 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 pub fn memory() -> Self {
42 Self::default()
43 }
44
45 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 pub fn with_hnsw_config(mut self, config: HnswConfig) -> Self {
58 self.hnsw_config = config;
59 self
60 }
61
62 pub fn with_max_vectors(mut self, max: usize) -> Self {
64 self.max_vectors = max;
65 self
66 }
67
68 pub fn with_auto_persist(mut self, enabled: bool) -> Self {
70 self.auto_persist = enabled;
71 self
72 }
73
74 pub fn with_persist_interval(mut self, secs: u64) -> Self {
76 self.persist_interval_secs = secs;
77 self
78 }
79}
80
81#[derive(Debug, Clone)]
86#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
87pub struct HnswConfig {
88 pub m: usize,
93
94 pub m_max: usize,
98
99 pub ef_construction: usize,
104
105 pub ef_search: usize,
110
111 pub parallel_construction: bool,
115
116 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, }
130 }
131}
132
133impl HnswConfig {
134 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 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 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 pub fn with_m(mut self, m: usize) -> Self {
178 self.m = m;
179 self.m_max = m * 2;
180 self
181 }
182
183 pub fn with_ef_construction(mut self, ef: usize) -> Self {
185 self.ef_construction = ef;
186 self
187 }
188
189 pub fn with_ef_search(mut self, ef: usize) -> Self {
191 self.ef_search = ef;
192 self
193 }
194
195 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 let memory_efficient = HnswConfig::memory_efficient();
225
226 assert!(fast.m < accurate.m);
227 assert!(fast.ef_construction < accurate.ef_construction);
228 assert_eq!(memory_efficient.m, 8);
229 assert!(!memory_efficient.parallel_construction);
230 assert_eq!(memory_efficient.num_threads, 1);
231 }
232
233 #[test]
234 fn test_config_default() {
235 let config = Config::default();
236 assert!(config.data_path.is_none());
237 assert_eq!(config.max_vectors, 0);
238 assert!(!config.auto_persist);
239 assert_eq!(config.persist_interval_secs, 300);
240 }
241
242 #[test]
243 fn test_config_builder_methods() {
244 let hnsw = HnswConfig::fast()
245 .with_m(12)
246 .with_ef_construction(150)
247 .with_ef_search(75);
248 let config = Config::persistent("/data/vectors")
249 .with_hnsw_config(hnsw.clone())
250 .with_max_vectors(10_000)
251 .with_auto_persist(false)
252 .with_persist_interval(60);
253
254 assert_eq!(config.data_path, Some(PathBuf::from("/data/vectors")));
255 assert!(!config.auto_persist);
256 assert_eq!(config.max_vectors, 10_000);
257 assert_eq!(config.persist_interval_secs, 60);
258 assert_eq!(config.hnsw_config.m, 12);
259 assert_eq!(config.hnsw_config.m_max, 24);
260 assert_eq!(config.hnsw_config.ef_construction, 150);
261 assert_eq!(config.hnsw_config.ef_search, 75);
262 }
263
264 #[test]
265 fn test_hnsw_builder_methods() {
266 let config = HnswConfig::default()
267 .with_m(20)
268 .with_ef_construction(300)
269 .with_ef_search(150)
270 .with_num_threads(4);
271
272 assert_eq!(config.m, 20);
273 assert_eq!(config.m_max, 40);
274 assert_eq!(config.ef_construction, 300);
275 assert_eq!(config.ef_search, 150);
276 assert_eq!(config.num_threads, 4);
277 }
278
279 #[test]
280 fn test_hnsw_default() {
281 let config = HnswConfig::default();
282 assert_eq!(config.m, 16);
283 assert_eq!(config.m_max, 32);
284 assert_eq!(config.ef_construction, 200);
285 assert_eq!(config.ef_search, 100);
286 assert!(config.parallel_construction);
287 assert_eq!(config.num_threads, 0);
288 }
289}