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