velesdb-cli 1.13.5

Interactive CLI and REPL for VelesDB with VelesQL support
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! Session configuration for VelesDB REPL.
//!
//! Manages session-level settings that can be modified with `\set` and viewed with `\show`.

use std::collections::HashMap;
use velesdb_core::SearchQuality;

/// Session settings for the REPL.
#[derive(Debug, Clone)]
pub struct SessionSettings {
    /// Current search mode.
    mode: SearchQuality,
    /// Override ef_search (None = use mode default).
    ef_search: Option<usize>,
    /// Query timeout in milliseconds.
    timeout_ms: u64,
    /// Enable reranking after quantized search.
    rerank: bool,
    /// Maximum results per query.
    max_results: usize,
    /// Active collection (for \use command).
    active_collection: Option<String>,
    /// Custom settings.
    custom: HashMap<String, String>,
}

impl Default for SessionSettings {
    fn default() -> Self {
        Self {
            mode: SearchQuality::Balanced,
            ef_search: None,
            timeout_ms: 30000,
            rerank: true,
            max_results: 100,
            active_collection: None,
            custom: HashMap::new(),
        }
    }
}

impl SessionSettings {
    /// Creates new session settings with defaults.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Gets the current search mode.
    #[must_use]
    pub fn mode(&self) -> SearchQuality {
        self.mode
    }

    /// Gets the effective ef_search value.
    ///
    /// Uses a default `k=10` for the quality profile's ef calculation.
    #[must_use]
    #[allow(dead_code)] // Reason: public API for session-aware query execution (used in tests)
    pub fn effective_ef_search(&self) -> usize {
        self.ef_search.unwrap_or_else(|| self.mode.ef_search(10))
    }

    /// Gets the query timeout in milliseconds.
    #[must_use]
    #[allow(dead_code)] // Reason: public API for session-aware query execution (used in tests)
    pub fn timeout_ms(&self) -> u64 {
        self.timeout_ms
    }

    /// Gets the rerank setting.
    #[must_use]
    #[allow(dead_code)] // Reason: public API for session-aware query execution (used in tests)
    pub fn rerank(&self) -> bool {
        self.rerank
    }

    /// Gets max results.
    #[must_use]
    #[allow(dead_code)] // Reason: public API for session-aware query execution (used in tests)
    pub fn max_results(&self) -> usize {
        self.max_results
    }

    /// Gets the active collection.
    #[must_use]
    pub fn active_collection(&self) -> Option<&str> {
        self.active_collection.as_deref()
    }

    /// Sets a session parameter.
    ///
    /// # Returns
    ///
    /// Ok(()) if the parameter was set, Err(message) if invalid.
    pub fn set(&mut self, key: &str, value: &str) -> Result<(), String> {
        match key.to_lowercase().as_str() {
            "mode" => {
                self.mode = parse_mode(value)?;
                self.ef_search = None; // Reset ef_search when mode changes
                Ok(())
            }
            "ef_search" => {
                let ef = value
                    .parse::<usize>()
                    .map_err(|_| format!("Invalid integer: {value}"))?;
                if !(16..=4096).contains(&ef) {
                    return Err(format!("ef_search must be between 16 and 4096, got {ef}"));
                }
                self.ef_search = Some(ef);
                Ok(())
            }
            "timeout_ms" | "timeout" => {
                let ms = value
                    .parse::<u64>()
                    .map_err(|_| format!("Invalid integer: {value}"))?;
                if ms < 100 {
                    return Err("timeout_ms must be at least 100".to_string());
                }
                self.timeout_ms = ms;
                Ok(())
            }
            "rerank" => {
                self.rerank = parse_bool(value)?;
                Ok(())
            }
            "max_results" => {
                let max = value
                    .parse::<usize>()
                    .map_err(|_| format!("Invalid integer: {value}"))?;
                if max == 0 || max > 10000 {
                    return Err("max_results must be between 1 and 10000".to_string());
                }
                self.max_results = max;
                Ok(())
            }
            _ => {
                self.custom.insert(key.to_string(), value.to_string());
                Ok(())
            }
        }
    }

    /// Sets the active collection.
    pub fn use_collection(&mut self, name: Option<String>) {
        self.active_collection = name;
    }

    /// Resets a specific setting or all settings.
    pub fn reset(&mut self, key: Option<&str>) {
        match key {
            None => {
                *self = Self::default();
            }
            Some(k) => match k.to_lowercase().as_str() {
                "mode" => self.mode = SearchQuality::Balanced,
                "ef_search" => self.ef_search = None,
                "timeout_ms" | "timeout" => self.timeout_ms = 30000,
                "rerank" => self.rerank = true,
                "max_results" => self.max_results = 100,
                "collection" => self.active_collection = None,
                _ => {
                    self.custom.remove(k);
                }
            },
        }
    }

    /// Returns all settings as displayable key-value pairs.
    #[must_use]
    pub fn all_settings(&self) -> Vec<(String, String)> {
        let mut settings = vec![
            ("mode".to_string(), format_quality(self.mode)),
            (
                "ef_search".to_string(),
                self.ef_search.map_or_else(
                    || format!("auto ({})", self.mode.ef_search(10)),
                    |v| v.to_string(),
                ),
            ),
            ("timeout_ms".to_string(), self.timeout_ms.to_string()),
            ("rerank".to_string(), self.rerank.to_string()),
            ("max_results".to_string(), self.max_results.to_string()),
            (
                "collection".to_string(),
                self.active_collection
                    .clone()
                    .unwrap_or_else(|| "(none)".to_string()),
            ),
        ];

        for (k, v) in &self.custom {
            settings.push((k.clone(), v.clone()));
        }

        settings
    }

    /// Gets a single setting value.
    #[must_use]
    pub fn get(&self, key: &str) -> Option<String> {
        match key.to_lowercase().as_str() {
            "mode" => Some(format_quality(self.mode)),
            "ef_search" => Some(self.ef_search.map_or_else(
                || format!("auto ({})", self.mode.ef_search(10)),
                |v| v.to_string(),
            )),
            "timeout_ms" | "timeout" => Some(self.timeout_ms.to_string()),
            "rerank" => Some(self.rerank.to_string()),
            "max_results" => Some(self.max_results.to_string()),
            "collection" => Some(
                self.active_collection
                    .clone()
                    .unwrap_or_else(|| "(none)".to_string()),
            ),
            _ => self.custom.get(key).cloned(),
        }
    }
}

/// Formats a `SearchQuality` for display in session settings.
fn format_quality(q: SearchQuality) -> String {
    match q {
        SearchQuality::Fast => "fast".to_string(),
        SearchQuality::Balanced => "balanced".to_string(),
        SearchQuality::Accurate => "accurate".to_string(),
        SearchQuality::Perfect => "perfect".to_string(),
        SearchQuality::AutoTune => "autotune".to_string(),
        SearchQuality::Custom(ef) => format!("custom:{ef}"),
        SearchQuality::Adaptive { min_ef, max_ef } => {
            format!("adaptive:{min_ef}:{max_ef}")
        }
        _ => format!("{q:?}").to_lowercase(),
    }
}

fn parse_mode(value: &str) -> Result<SearchQuality, String> {
    let lower = value.to_lowercase();
    match lower.as_str() {
        "fast" => Ok(SearchQuality::Fast),
        "balanced" => Ok(SearchQuality::Balanced),
        "accurate" => Ok(SearchQuality::Accurate),
        "perfect" => Ok(SearchQuality::Perfect),
        "autotune" => Ok(SearchQuality::AutoTune),
        _ => parse_parameterized_mode(&lower),
    }
}

/// Parses `custom:<ef>` and `adaptive:<min>:<max>` mode strings.
fn parse_parameterized_mode(value: &str) -> Result<SearchQuality, String> {
    if let Some(ef_str) = value.strip_prefix("custom:") {
        let ef = ef_str
            .parse::<usize>()
            .map_err(|_| format!("Invalid ef value in 'custom:{ef_str}'"))?;
        return Ok(SearchQuality::Custom(ef));
    }
    if let Some(rest) = value.strip_prefix("adaptive:") {
        let parts: Vec<&str> = rest.splitn(2, ':').collect();
        if parts.len() != 2 {
            return Err("adaptive format: adaptive:<min_ef>:<max_ef>".to_string());
        }
        let min_ef = parts[0]
            .parse::<usize>()
            .map_err(|_| format!("Invalid min_ef in '{value}'"))?;
        let max_ef = parts[1]
            .parse::<usize>()
            .map_err(|_| format!("Invalid max_ef in '{value}'"))?;
        return Ok(SearchQuality::Adaptive { min_ef, max_ef });
    }
    Err(format!(
        "Invalid mode '{value}'. Valid: fast, balanced, accurate, \
         perfect, autotune, custom:<ef>, adaptive:<min>:<max>"
    ))
}

fn parse_bool(value: &str) -> Result<bool, String> {
    match value.to_lowercase().as_str() {
        "true" | "on" | "1" | "yes" => Ok(true),
        "false" | "off" | "0" | "no" => Ok(false),
        _ => Err(format!(
            "Invalid boolean '{}'. Use true/false, on/off, 1/0",
            value
        )),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_session_defaults() {
        let session = SessionSettings::new();
        assert_eq!(session.mode(), SearchQuality::Balanced);
        assert_eq!(session.effective_ef_search(), 160);
        assert_eq!(session.timeout_ms(), 30000);
        assert!(session.rerank());
        assert_eq!(session.max_results(), 100);
        assert!(session.active_collection().is_none());
    }

    #[test]
    fn test_set_mode() {
        let mut session = SessionSettings::new();
        session.set("mode", "fast").unwrap();
        assert_eq!(session.mode(), SearchQuality::Fast);
        assert_eq!(session.effective_ef_search(), 96);
    }

    #[test]
    fn test_set_ef_search() {
        let mut session = SessionSettings::new();
        session.set("ef_search", "512").unwrap();
        assert_eq!(session.effective_ef_search(), 512);
    }

    #[test]
    fn test_set_ef_search_invalid_range() {
        let mut session = SessionSettings::new();
        assert!(session.set("ef_search", "10").is_err());
        assert!(session.set("ef_search", "5000").is_err());
    }

    #[test]
    fn test_set_timeout() {
        let mut session = SessionSettings::new();
        session.set("timeout_ms", "5000").unwrap();
        assert_eq!(session.timeout_ms(), 5000);
    }

    #[test]
    fn test_set_rerank() {
        let mut session = SessionSettings::new();
        session.set("rerank", "off").unwrap();
        assert!(!session.rerank());
        session.set("rerank", "true").unwrap();
        assert!(session.rerank());
    }

    #[test]
    fn test_use_collection() {
        let mut session = SessionSettings::new();
        session.use_collection(Some("documents".to_string()));
        assert_eq!(session.active_collection(), Some("documents"));
    }

    #[test]
    fn test_reset_single() {
        let mut session = SessionSettings::new();
        session.set("mode", "fast").unwrap();
        session.reset(Some("mode"));
        assert_eq!(session.mode(), SearchQuality::Balanced);
    }

    #[test]
    fn test_reset_all() {
        let mut session = SessionSettings::new();
        session.set("mode", "fast").unwrap();
        session.set("ef_search", "512").unwrap();
        session.reset(None);
        assert_eq!(session.mode(), SearchQuality::Balanced);
        assert!(session.ef_search.is_none());
    }

    #[test]
    fn test_all_settings() {
        let session = SessionSettings::new();
        let settings = session.all_settings();
        assert!(settings.iter().any(|(k, _)| k == "mode"));
        assert!(settings.iter().any(|(k, _)| k == "ef_search"));
    }

    #[test]
    fn test_get_setting() {
        let session = SessionSettings::new();
        assert_eq!(session.get("mode"), Some("balanced".to_string()));
        assert!(session.get("unknown").is_none());
    }

    #[test]
    fn test_custom_setting() {
        let mut session = SessionSettings::new();
        session.set("custom_key", "custom_value").unwrap();
        assert_eq!(session.get("custom_key"), Some("custom_value".to_string()));
    }

    #[test]
    fn test_set_mode_autotune() {
        let mut session = SessionSettings::new();
        session.set("mode", "autotune").unwrap();
        assert_eq!(session.mode(), SearchQuality::AutoTune);
    }

    #[test]
    fn test_set_mode_custom() {
        let mut session = SessionSettings::new();
        session.set("mode", "custom:256").unwrap();
        assert_eq!(session.mode(), SearchQuality::Custom(256));
        assert_eq!(session.effective_ef_search(), 256);
    }

    #[test]
    fn test_set_mode_adaptive() {
        let mut session = SessionSettings::new();
        session.set("mode", "adaptive:32:512").unwrap();
        assert_eq!(
            session.mode(),
            SearchQuality::Adaptive {
                min_ef: 32,
                max_ef: 512
            }
        );
    }

    #[test]
    fn test_set_mode_invalid() {
        let mut session = SessionSettings::new();
        assert!(session.set("mode", "nonexistent").is_err());
        assert!(session.set("mode", "custom:abc").is_err());
        assert!(session.set("mode", "adaptive:32").is_err());
    }
}