reddb-io-server 1.2.0

RedDB server-side engine: storage, runtime, replication, MCP, AI, and the gRPC/HTTP/RedWire/PG-wire dispatchers. Re-exported by the umbrella `reddb` crate.
Documentation
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
//! Similarity Search Integration for Query Engine
//!
//! Provides vector similarity search capabilities integrated with
//! the query engine for semantic search and nearest neighbor queries.

use super::filter::Filter;
use super::sort::QueryLimits;
use crate::storage::engine::distance::DistanceMetric;
use crate::storage::engine::vector_store::{SearchResult, VectorCollection, VectorId};
use crate::storage::schema::Value;
use std::collections::HashMap;

/// Dense vector wrapper for similarity queries
#[derive(Debug, Clone)]
pub struct DenseVector {
    values: Vec<f32>,
}

impl DenseVector {
    pub fn new(values: Vec<f32>) -> Self {
        Self { values }
    }

    pub fn as_slice(&self) -> &[f32] {
        &self.values
    }
}

impl From<Vec<f32>> for DenseVector {
    fn from(values: Vec<f32>) -> Self {
        Self { values }
    }
}

/// Similarity query parameters
#[derive(Debug, Clone)]
pub struct SimilarityQuery {
    /// Query vector
    pub vector: DenseVector,
    /// Number of neighbors to find
    pub k: usize,
    /// Distance metric
    pub distance: DistanceMetric,
    /// Optional filter to apply before similarity search
    pub filter: Option<Filter>,
    /// Number of probes for IVF index (if applicable)
    pub n_probes: Option<usize>,
    /// Distance threshold (for range queries)
    pub distance_threshold: Option<f32>,
}

impl SimilarityQuery {
    /// Create a new similarity query
    pub fn new(vector: DenseVector, k: usize) -> Self {
        Self {
            vector,
            k,
            distance: DistanceMetric::Cosine,
            filter: None,
            n_probes: None,
            distance_threshold: None,
        }
    }

    /// Set distance metric
    pub fn with_distance(mut self, distance: DistanceMetric) -> Self {
        self.distance = distance;
        self
    }

    /// Set pre-filter
    pub fn with_filter(mut self, filter: Filter) -> Self {
        self.filter = Some(filter);
        self
    }

    /// Set number of IVF probes
    pub fn with_probes(mut self, n_probes: usize) -> Self {
        self.n_probes = Some(n_probes);
        self
    }

    /// Set distance threshold for range query
    pub fn with_threshold(mut self, threshold: f32) -> Self {
        self.distance_threshold = Some(threshold);
        self
    }
}

/// Similarity search result with metadata
#[derive(Debug, Clone)]
pub struct SimilarityResult {
    /// Vector ID
    pub id: VectorId,
    /// Distance to query
    pub distance: f32,
    /// Similarity score (1 - normalized_distance for bounded metrics)
    pub score: f32,
    /// Associated metadata (optional)
    pub metadata: Option<HashMap<String, Value>>,
}

impl SimilarityResult {
    /// Create a new result
    pub fn new(id: VectorId, distance: f32) -> Self {
        Self {
            id,
            distance,
            score: 1.0 / (1.0 + distance), // Simple similarity transform
            metadata: None,
        }
    }

    /// Create with score conversion based on distance metric
    pub fn with_metric(id: VectorId, distance: f32, metric: DistanceMetric) -> Self {
        let score = match metric {
            DistanceMetric::Cosine => 1.0 - distance, // Cosine: 0 = identical, 2 = opposite
            DistanceMetric::InnerProduct => -distance, // Negated dot product
            DistanceMetric::L2 => 1.0 / (1.0 + distance),
        };

        Self {
            id,
            distance,
            score: score.max(0.0),
            metadata: None,
        }
    }

    /// Add metadata
    pub fn with_metadata(mut self, metadata: HashMap<String, Value>) -> Self {
        self.metadata = Some(metadata);
        self
    }
}

/// Result set from similarity search
#[derive(Debug, Clone)]
pub struct SimilarityResultSet {
    /// Results sorted by distance
    pub results: Vec<SimilarityResult>,
    /// Query vector dimension
    pub dimension: usize,
    /// Distance metric used
    pub distance: DistanceMetric,
    /// Total vectors searched (for approximate search)
    pub vectors_searched: Option<usize>,
    /// Search time in microseconds
    pub search_time_us: u64,
}

impl SimilarityResultSet {
    /// Create empty result set
    pub fn empty(dimension: usize, distance: DistanceMetric) -> Self {
        Self {
            results: Vec::new(),
            dimension,
            distance,
            vectors_searched: None,
            search_time_us: 0,
        }
    }

    /// Create from search results
    pub fn from_results(
        results: Vec<SearchResult>,
        dimension: usize,
        distance: DistanceMetric,
    ) -> Self {
        let similarity_results = results
            .into_iter()
            .map(|r| SimilarityResult::with_metric(r.id, r.distance, distance))
            .collect();

        Self {
            results: similarity_results,
            dimension,
            distance,
            vectors_searched: None,
            search_time_us: 0,
        }
    }

    /// Get number of results
    pub fn len(&self) -> usize {
        self.results.len()
    }

    /// Check if empty
    pub fn is_empty(&self) -> bool {
        self.results.is_empty()
    }

    /// Get top-k IDs
    pub fn top_ids(&self, k: usize) -> Vec<VectorId> {
        self.results.iter().take(k).map(|r| r.id).collect()
    }

    /// Get results above score threshold
    pub fn above_score(&self, threshold: f32) -> Vec<&SimilarityResult> {
        self.results
            .iter()
            .filter(|r| r.score >= threshold)
            .collect()
    }

    /// Apply limits
    pub fn apply_limits(mut self, limits: QueryLimits) -> Self {
        self.results = limits.apply(self.results);
        self
    }
}

/// Trait for vector index that supports similarity search
pub trait VectorIndex: Send + Sync {
    /// Search for k nearest neighbors
    fn search(&self, query: &DenseVector, k: usize) -> Vec<SearchResult>;

    /// Search with optional parameters
    fn search_with_params(
        &self,
        query: &DenseVector,
        k: usize,
        n_probes: Option<usize>,
    ) -> Vec<SearchResult>;

    /// Get vector by ID
    fn get(&self, id: VectorId) -> Option<DenseVector>;

    /// Get dimension
    fn dimension(&self) -> usize;

    /// Get distance metric
    fn distance_metric(&self) -> DistanceMetric;

    /// Get number of indexed vectors
    fn len(&self) -> usize;

    /// Check if empty
    fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl VectorIndex for VectorCollection {
    fn search(&self, query: &DenseVector, k: usize) -> Vec<SearchResult> {
        VectorCollection::search(self, query.as_slice(), k)
    }

    fn search_with_params(
        &self,
        query: &DenseVector,
        k: usize,
        _n_probes: Option<usize>,
    ) -> Vec<SearchResult> {
        VectorCollection::search(self, query.as_slice(), k)
    }

    fn get(&self, id: VectorId) -> Option<DenseVector> {
        VectorCollection::get(self, id).map(|vec| DenseVector::new(vec.clone()))
    }

    fn dimension(&self) -> usize {
        self.dimension
    }

    fn distance_metric(&self) -> DistanceMetric {
        self.metric
    }

    fn len(&self) -> usize {
        self.len()
    }
}

/// Execute similarity search
pub fn execute_similarity_search(
    index: &dyn VectorIndex,
    query: &SimilarityQuery,
) -> SimilarityResultSet {
    let start = std::time::Instant::now();

    // Perform search
    let results = if let Some(threshold) = query.distance_threshold {
        // Range query: get more results then filter
        let candidates = index.search_with_params(&query.vector, query.k * 10, query.n_probes);
        candidates
            .into_iter()
            .filter(|r| r.distance <= threshold)
            .take(query.k)
            .collect()
    } else {
        index.search_with_params(&query.vector, query.k, query.n_probes)
    };

    let search_time = start.elapsed().as_micros() as u64;

    let mut result_set =
        SimilarityResultSet::from_results(results, index.dimension(), index.distance_metric());
    result_set.search_time_us = search_time;
    result_set.vectors_searched = Some(index.len());

    result_set
}

/// Hybrid search combining filter and similarity
pub fn execute_hybrid_search<F>(
    index: &dyn VectorIndex,
    query: &SimilarityQuery,
    get_metadata: F,
    filter_matches: impl Fn(VectorId, &Filter) -> bool,
) -> SimilarityResultSet
where
    F: Fn(VectorId) -> Option<HashMap<String, Value>>,
{
    let start = std::time::Instant::now();

    // Get more candidates than needed to account for filtering
    let over_fetch = if query.filter.is_some() { 10 } else { 1 };
    let candidates = index.search_with_params(&query.vector, query.k * over_fetch, query.n_probes);

    // Apply filter and collect results
    let results: Vec<SimilarityResult> = candidates
        .into_iter()
        .filter(|r| {
            if let Some(filter) = &query.filter {
                filter_matches(r.id, filter)
            } else {
                true
            }
        })
        .take(query.k)
        .map(|r| {
            let mut result =
                SimilarityResult::with_metric(r.id, r.distance, index.distance_metric());
            if let Some(meta) = get_metadata(r.id) {
                result = result.with_metadata(meta);
            }
            result
        })
        .collect();

    let search_time = start.elapsed().as_micros() as u64;

    SimilarityResultSet {
        results,
        dimension: index.dimension(),
        distance: index.distance_metric(),
        vectors_searched: Some(index.len()),
        search_time_us: search_time,
    }
}

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

    fn create_test_index() -> VectorCollection {
        let mut collection = VectorCollection::new("test", 3).with_metric(DistanceMetric::Cosine);

        // Add test vectors
        let _ = collection.insert(vec![1.0, 0.0, 0.0], None);
        let _ = collection.insert(vec![0.0, 1.0, 0.0], None);
        let _ = collection.insert(vec![0.0, 0.0, 1.0], None);
        let _ = collection.insert(vec![0.7, 0.7, 0.0], None);
        let _ = collection.insert(vec![0.5, 0.5, 0.7], None);

        collection
    }

    #[test]
    fn test_similarity_query_basic() {
        let index = create_test_index();

        let query = SimilarityQuery::new(DenseVector::new(vec![1.0, 0.0, 0.0]), 3);
        let results = execute_similarity_search(&index, &query);

        assert_eq!(results.len(), 3);
        assert_eq!(results.results[0].id, 0); // Exact match
        assert!(results.results[0].distance < 0.01);
    }

    #[test]
    fn test_similarity_result_score() {
        // Cosine distance 0 = identical
        let result = SimilarityResult::with_metric(1, 0.0, DistanceMetric::Cosine);
        assert!((result.score - 1.0).abs() < 0.01);

        // Cosine distance 1 = orthogonal
        let result = SimilarityResult::with_metric(1, 1.0, DistanceMetric::Cosine);
        assert!(result.score < 0.01);
    }

    #[test]
    fn test_similarity_result_set_top_ids() {
        let index = create_test_index();

        let query = SimilarityQuery::new(DenseVector::new(vec![1.0, 0.0, 0.0]), 5);
        let results = execute_similarity_search(&index, &query);

        let top3 = results.top_ids(3);
        assert_eq!(top3.len(), 3);
        assert_eq!(top3[0], 0);
    }

    #[test]
    fn test_similarity_threshold() {
        let index = create_test_index();

        // Query with distance threshold
        let query =
            SimilarityQuery::new(DenseVector::new(vec![1.0, 0.0, 0.0]), 10).with_threshold(0.5);

        let results = execute_similarity_search(&index, &query);

        // Only vectors within threshold should be returned
        for result in &results.results {
            assert!(result.distance <= 0.5);
        }
    }

    #[test]
    fn test_vector_index_trait() {
        let index = create_test_index();

        let index_ref: &dyn VectorIndex = &index;

        assert_eq!(index_ref.dimension(), 3);
        assert_eq!(index_ref.len(), 5);
        assert!(!index_ref.is_empty());

        let vec = index_ref.get(0).unwrap();
        assert_eq!(vec.as_slice(), &[1.0, 0.0, 0.0]);
    }

    #[test]
    fn test_above_score_filter() {
        let results = SimilarityResultSet {
            results: vec![
                SimilarityResult::new(1, 0.1), // score ~0.91
                SimilarityResult::new(2, 0.5), // score ~0.67
                SimilarityResult::new(3, 2.0), // score ~0.33
            ],
            dimension: 3,
            distance: DistanceMetric::L2,
            vectors_searched: Some(100),
            search_time_us: 100,
        };

        let above_05 = results.above_score(0.5);
        assert_eq!(above_05.len(), 2); // 0.91 and 0.67 are >= 0.5
    }

    #[test]
    fn test_similarity_query_builder() {
        let query = SimilarityQuery::new(DenseVector::new(vec![1.0, 0.0, 0.0]), 10)
            .with_distance(DistanceMetric::L2)
            .with_probes(5)
            .with_threshold(1.0);

        assert_eq!(query.k, 10);
        assert_eq!(query.distance, DistanceMetric::L2);
        assert_eq!(query.n_probes, Some(5));
        assert_eq!(query.distance_threshold, Some(1.0));
    }

    #[test]
    fn test_hybrid_search_with_filter() {
        let index = create_test_index();

        // Mock metadata
        let metadata: HashMap<VectorId, HashMap<String, Value>> = [
            (
                1,
                [("category".to_string(), Value::text("A".to_string()))]
                    .into_iter()
                    .collect(),
            ),
            (
                2,
                [("category".to_string(), Value::text("B".to_string()))]
                    .into_iter()
                    .collect(),
            ),
            (
                3,
                [("category".to_string(), Value::text("A".to_string()))]
                    .into_iter()
                    .collect(),
            ),
            (
                4,
                [("category".to_string(), Value::text("B".to_string()))]
                    .into_iter()
                    .collect(),
            ),
            (
                5,
                [("category".to_string(), Value::text("A".to_string()))]
                    .into_iter()
                    .collect(),
            ),
        ]
        .into_iter()
        .collect();

        let filter = Filter::eq("category", Value::text("A".to_string()));
        let query = SimilarityQuery::new(DenseVector::new(vec![1.0, 0.0, 0.0]), 5)
            .with_filter(filter.clone());

        let results = execute_hybrid_search(
            &index,
            &query,
            |id| metadata.get(&id).cloned(),
            |id, filter| {
                if let Some(meta) = metadata.get(&id) {
                    filter.evaluate(&|col| meta.get(col).cloned())
                } else {
                    false
                }
            },
        );

        // Should only return vectors with category "A"
        assert!(results.len() <= 3); // Only 3 vectors have category A
        for result in &results.results {
            if let Some(meta) = &result.metadata {
                assert_eq!(meta.get("category"), Some(&Value::text("A".to_string())));
            }
        }
    }

    #[test]
    fn test_apply_limits() {
        let results = SimilarityResultSet {
            results: (0..10)
                .map(|i| SimilarityResult::new(i, i as f32 * 0.1))
                .collect(),
            dimension: 3,
            distance: DistanceMetric::L2,
            vectors_searched: Some(100),
            search_time_us: 100,
        };

        let limited = results.apply_limits(QueryLimits::none().offset(2).limit(3));
        assert_eq!(limited.len(), 3);
        assert_eq!(limited.results[0].id, 2);
    }
}