1use std::collections::HashSet;
14use std::path::PathBuf;
15use std::sync::Arc;
16
17use serde::Deserialize;
18
19use crate::util::serde::env_var;
20
21#[derive(Deserialize)]
22pub struct Config {
23 pub normalization: ConfigNormalization,
24
25 pub tokenization: ConfigTokenization,
26
27 pub stopwords: ConfigStopwords,
28
29 pub search: ConfigSearch,
30
31 pub store: ConfigStore,
32}
33
34impl Config {
35 pub fn validate(&self) {
36 if self.store.kv.database.write_buffer_size == Some(0) {
38 panic!("write_buffer for kv must not be zero");
39 }
40
41 if self.store.kv.database.flush_after >= self.store.kv.pool.inactive_after {
43 panic!("flush_after for kv must be strictly lower than inactive_after");
44 }
45
46 if self.store.kv.database.max_flushes.is_some()
48 && self.store.kv.database.max_background_jobs.is_some()
49 {
50 panic!("max_background_jobs makes max_flushes unneeded, don’t configure both");
51 }
52
53 if self.store.fst.graph.consolidate_after >= self.store.fst.pool.inactive_after {
55 panic!("consolidate_after for fst must be strictly lower than inactive_after");
56 }
57 }
58}
59
60#[derive(Deserialize, Clone, Copy)]
63pub struct ConfigNormalization {
64 #[serde(with = "crate::util::serde::none_string_as_none")]
65 pub unicode_normalization: Option<UnicodeNormalization>,
66
67 pub diacritic_folding_enabled: bool,
68
69 #[cfg(feature = "stemming")]
70 pub stemming_enabled: bool,
71}
72
73#[derive(Deserialize, Debug, Clone, Copy)]
74pub enum UnicodeNormalization {
75 Nfc,
77 Nfkc,
79}
80
81#[derive(Deserialize, Clone, Copy)]
83pub struct ConfigTokenization {
84 pub detect_special_patterns: bool,
85
86 #[serde(alias = "split_special_patterns")]
87 pub compat_split_special_patterns: bool,
88}
89
90#[derive(Deserialize, Clone, Default)]
91pub struct ConfigStopwords {
92 #[serde(deserialize_with = "to_stopwords")]
93 pub allow: HashSet<String>,
94
95 #[serde(deserialize_with = "to_stopwords")]
96 pub deny: HashSet<String>,
97}
98
99fn to_stopwords<'de, D>(deserializer: D) -> Result<HashSet<String>, D::Error>
100where
101 D: serde::de::Deserializer<'de>,
102{
103 use unicode_normalization::UnicodeNormalization as _;
104
105 let vec: Vec<Box<str>> = Deserialize::deserialize(deserializer)?;
106 let stopwords_iter = vec.into_iter().map(|s| s.nfkd().to_string());
107 Ok(HashSet::from_iter(stopwords_iter))
108}
109
110#[derive(Deserialize)]
111pub struct ConfigSearch {
112 pub query_limit_default: u16,
113
114 pub query_limit_maximum: u16,
115
116 pub query_alternates_try: usize,
117
118 pub query_minimum_term_idf_default: f32,
119
120 pub query_minimum_term_idf_minimum_object_count: u64,
121
122 pub suggest_limit_default: u16,
123
124 pub suggest_limit_maximum: u16,
125
126 pub list_limit_default: u16,
127
128 pub list_limit_maximum: u16,
129}
130
131#[derive(Deserialize)]
132pub struct ConfigStore {
133 pub kv: Arc<ConfigStoreKV>,
134
135 pub fst: Arc<ConfigStoreFST>,
136}
137
138#[derive(Deserialize)]
139pub struct ConfigStoreKV {
140 #[serde(deserialize_with = "env_var::path_buf")]
141 pub path: PathBuf,
142
143 pub retain_word_objects: usize,
144
145 pub pool: ConfigStoreKVPool,
146
147 pub database: ConfigStoreKVDatabase,
148}
149
150#[derive(Deserialize)]
151pub struct ConfigStoreKVPool {
152 pub inactive_after: u64,
153}
154
155#[derive(Deserialize)]
156#[serde(deny_unknown_fields)]
157pub struct ConfigStoreKVDatabase {
158 pub flush_after: u64,
159
160 pub write_ahead_log: bool,
161
162 #[serde(default)]
167 pub compress: Option<bool>,
168
169 #[serde(default)]
170 pub parallelism: Option<i32>,
171
172 #[serde(default)]
173 #[serde(alias = "max_files")]
174 pub max_open_files: Option<i32>,
175
176 #[serde(default = "default_write_buffer_size")]
179 #[serde(alias = "write_buffer")]
180 pub write_buffer_size: Option<usize>,
181
182 #[serde(default)]
183 pub max_write_buffer_number: Option<i32>,
184
185 #[serde(default)]
186 pub min_write_buffer_number: Option<i32>,
187
188 #[serde(default)]
189 pub min_write_buffer_number_to_merge: Option<i32>,
190
191 #[serde(default)]
192 pub block_cache_size: Option<u32>,
193
194 #[serde(default)]
195 pub cache_index_and_filter_blocks: Option<bool>,
196
197 #[serde(default)]
198 #[serde(alias = "compression")]
199 #[serde(deserialize_with = "to_rocksdb_compression_type_opt")]
200 pub compression_type: Option<rocksdb::DBCompressionType>,
201
202 #[serde(default)]
203 #[serde(alias = "wal_compression")]
204 #[serde(deserialize_with = "to_rocksdb_compression_type_opt")]
205 pub wal_compression_type: Option<rocksdb::DBCompressionType>,
206
207 #[serde(default)]
208 pub wal_ttl_seconds: Option<u64>,
209
210 #[serde(default)]
211 pub wal_size_limit_mb: Option<u64>,
212
213 #[serde(default)]
214 pub wal_bytes_per_sync: Option<u64>,
215
216 #[serde(default)]
217 #[serde(deserialize_with = "to_rocksdb_recovery_mode_opt")]
218 pub wal_recovery_mode: Option<rocksdb::DBRecoveryMode>,
219
220 #[serde(default)]
221 pub compression_level: Option<i32>,
222
223 #[serde(default)]
224 #[serde(alias = "compression_start_level")]
225 pub min_level_to_compress: Option<std::ffi::c_int>,
226
227 #[serde(default)]
228 #[serde(alias = "level0_file_num_compaction_trigger")]
229 pub level_zero_file_num_compaction_trigger: Option<i32>,
230
231 #[serde(default)]
232 #[serde(alias = "level0_slowdown_writes_trigger")]
233 pub level_zero_slowdown_writes_trigger: Option<i32>,
234
235 #[serde(default)]
236 #[serde(alias = "level0_stop_writes_trigger")]
237 pub level_zero_stop_writes_trigger: Option<i32>,
238
239 #[serde(default)]
240 pub max_bytes_for_level_base: Option<u64>,
241
242 #[serde(default)]
243 pub max_bytes_for_level_multiplier: Option<f64>,
244
245 #[serde(default)]
246 pub target_file_size_base: Option<u64>,
247
248 #[serde(default)]
249 pub max_background_jobs: Option<i32>,
250
251 #[serde(default)]
252 #[serde(alias = "max_compactions")]
253 pub max_subcompactions: Option<u32>,
254
255 #[serde(default)]
256 pub max_flushes: Option<u32>,
257
258 #[serde(default)]
259 pub stats_dump_period_sec: Option<u32>,
260}
261
262fn default_write_buffer_size() -> Option<usize> {
263 Some(16384)
264}
265
266fn parse_rocksdb_compression_type<E: serde::de::Error>(
267 str: &str,
268) -> Result<rocksdb::DBCompressionType, E> {
269 match str.to_ascii_lowercase().as_str() {
272 "none" => Ok(rocksdb::DBCompressionType::None),
273 "zstd" => Ok(rocksdb::DBCompressionType::Zstd),
279 _ => Err(serde::de::Error::unknown_variant(
280 str,
281 &[
282 "none",
283 "zstd",
289 ],
290 )),
291 }
292}
293
294fn to_rocksdb_compression_type_opt<'de, D>(
295 deserializer: D,
296) -> Result<Option<rocksdb::DBCompressionType>, D::Error>
297where
298 D: serde::de::Deserializer<'de>,
299{
300 let str: Option<String> = Deserialize::deserialize(deserializer)?;
301 str.map(|s| parse_rocksdb_compression_type(&s)).transpose()
302}
303
304fn parse_rocksdb_recovery_mode<E: serde::de::Error>(
305 str: &str,
306) -> Result<rocksdb::DBRecoveryMode, E> {
307 match str.to_ascii_lowercase().as_str() {
308 "tolerate_corrupted_tail_records" | "TolerateCorruptedTailRecords" => {
309 Ok(rocksdb::DBRecoveryMode::TolerateCorruptedTailRecords)
310 }
311 "absolute_consistency" | "AbsoluteConsistency" => {
312 Ok(rocksdb::DBRecoveryMode::AbsoluteConsistency)
313 }
314 "point_in_time" | "PointInTime" => Ok(rocksdb::DBRecoveryMode::PointInTime),
315 "skip_any_corrupted_record" | "SkipAnyCorruptedRecord" => {
316 Ok(rocksdb::DBRecoveryMode::SkipAnyCorruptedRecord)
317 }
318 _ => Err(serde::de::Error::unknown_variant(
319 str,
320 &[
321 "tolerate_corrupted_tail_records",
322 "absolute_consistency",
323 "point_in_time",
324 "skip_any_corrupted_record",
325 ],
326 )),
327 }
328}
329
330fn to_rocksdb_recovery_mode_opt<'de, D>(
331 deserializer: D,
332) -> Result<Option<rocksdb::DBRecoveryMode>, D::Error>
333where
334 D: serde::de::Deserializer<'de>,
335{
336 let str: Option<String> = Deserialize::deserialize(deserializer)?;
337 str.map(|s| parse_rocksdb_recovery_mode(&s)).transpose()
338}
339
340#[derive(Deserialize)]
341pub struct ConfigStoreFST {
342 #[serde(deserialize_with = "env_var::path_buf")]
343 pub path: PathBuf,
344
345 pub pool: ConfigStoreFSTPool,
346
347 pub graph: ConfigStoreFSTGraph,
348}
349
350#[derive(Deserialize)]
351pub struct ConfigStoreFSTPool {
352 pub inactive_after: u64,
353}
354
355#[derive(Deserialize)]
356pub struct ConfigStoreFSTGraph {
357 pub consolidate_after: u64,
358
359 pub max_size: usize,
360
361 pub max_words: usize,
362}
363
364#[cfg(test)]
365pub(crate) mod tests {
366 pub fn defaults_toml() -> &'static str {
367 r#"
368 [channel]
369 inet = "[::1]:1491"
370 tcp_timeout = 300
371
372 [normalization]
373 unicode_normalization = "none"
374 diacritic_folding_enabled = true
375 stemming_enabled = false
376
377 [tokenization]
378 detect_special_patterns = true
379 compat_split_special_patterns = false
380
381 [stopwords]
382 allow = []
383 deny = []
384
385 [search]
386 query_limit_default = 10
387 query_limit_maximum = 100
388 query_alternates_try = 4
389 query_minimum_term_idf_default = 0.1
390 query_minimum_term_idf_minimum_object_count = 100
391 suggest_limit_default = 5
392 suggest_limit_maximum = 20
393 list_limit_default = 100
394 list_limit_maximum = 500
395
396 [store.kv]
397 path = "./data/store/kv/"
398 retain_word_objects = 1000
399 pool.inactive_after = 1800
400 database.flush_after = 900
401 database.compression_type = "zstd"
402 database.parallelism = 2
403 database.write_ahead_log = true
404
405 [store.fst]
406 path = "./data/store/fst/"
407 pool.inactive_after = 300
408 graph.consolidate_after = 180
409 graph.max_size = 2048
410 graph.max_words = 250000
411 "#
412 }
413}