rag-plusplus-core 0.1.0

High-performance retrieval engine with SIMD-accelerated vector search and trajectory memory
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
//! Index Registry
//!
//! Manages multiple named indexes for multi-index retrieval scenarios.
//!
//! # Overview
//!
//! The registry provides a centralized way to manage multiple vector indexes,
//! enabling multi-modal retrieval (e.g., separate indexes for text embeddings,
//! code embeddings, and image embeddings).
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────┐
//! │                    IndexRegistry                             │
//! ├─────────────────────────────────────────────────────────────┤
//! │  indexes: HashMap<String, Box<dyn VectorIndex>>              │
//! ├─────────────────────────────────────────────────────────────┤
//! │  + register(name, index)                                     │
//! │  + get(name) -> &dyn VectorIndex                             │
//! │  + get_mut(name) -> &mut dyn VectorIndex                     │
//! │  + remove(name) -> Option<Box<dyn VectorIndex>>              │
//! │  + list() -> Vec<&str>                                       │
//! │  + search_all(query, k) -> MultiIndexResults                 │
//! └─────────────────────────────────────────────────────────────┘
//! ```

use crate::error::{Error, Result};
use crate::index::traits::{DistanceType, SearchResult, VectorIndex};
use ahash::AHashMap;
use parking_lot::RwLock;
use std::sync::Arc;

/// Information about a registered index.
#[derive(Debug, Clone)]
pub struct IndexInfo {
    /// Index name
    pub name: String,
    /// Vector dimension
    pub dimension: usize,
    /// Distance metric
    pub distance_type: DistanceType,
    /// Number of vectors
    pub size: usize,
    /// Memory usage in bytes
    pub memory_bytes: usize,
}

/// Results from a multi-index search.
#[derive(Debug, Clone)]
pub struct MultiIndexResult {
    /// Index name this result came from
    pub index_name: String,
    /// Search results from this index
    pub results: Vec<SearchResult>,
}

/// Results aggregated from multiple indexes.
#[derive(Debug, Clone, Default)]
pub struct MultiIndexResults {
    /// Results per index
    pub by_index: Vec<MultiIndexResult>,
    /// Total results across all indexes
    pub total_count: usize,
}

impl MultiIndexResults {
    /// Create empty results.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Add results from an index.
    pub fn add(&mut self, index_name: String, results: Vec<SearchResult>) {
        self.total_count += results.len();
        self.by_index.push(MultiIndexResult {
            index_name,
            results,
        });
    }

    /// Flatten all results into a single vector.
    ///
    /// Note: Results are not re-ranked; use fusion for proper merging.
    #[must_use]
    pub fn flatten(&self) -> Vec<(String, SearchResult)> {
        self.by_index
            .iter()
            .flat_map(|mir| {
                mir.results
                    .iter()
                    .cloned()
                    .map(|r| (mir.index_name.clone(), r))
            })
            .collect()
    }
}

/// Thread-safe registry for managing multiple named indexes.
///
/// # Example
///
/// ```ignore
/// use rag_plusplus_core::index::{IndexRegistry, FlatIndex, IndexConfig};
///
/// let mut registry = IndexRegistry::new();
///
/// // Register indexes for different modalities
/// let text_index = FlatIndex::new(IndexConfig::new(768));
/// let code_index = FlatIndex::new(IndexConfig::new(512));
///
/// registry.register("text_embeddings", text_index)?;
/// registry.register("code_embeddings", code_index)?;
///
/// // Search specific index
/// let results = registry.search("text_embeddings", &query, 10)?;
///
/// // Search all indexes
/// let all_results = registry.search_all(&query, 10)?;
/// ```
#[derive(Debug, Default)]
pub struct IndexRegistry {
    /// Named indexes
    indexes: AHashMap<String, Box<dyn VectorIndex>>,
}

impl IndexRegistry {
    /// Create a new empty registry.
    #[must_use]
    pub fn new() -> Self {
        Self {
            indexes: AHashMap::new(),
        }
    }

    /// Create registry with pre-allocated capacity.
    #[must_use]
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            indexes: AHashMap::with_capacity(capacity),
        }
    }

    /// Register a new index with the given name.
    ///
    /// # Errors
    ///
    /// Returns error if an index with the same name already exists.
    pub fn register<I: VectorIndex + 'static>(
        &mut self,
        name: impl Into<String>,
        index: I,
    ) -> Result<()> {
        let name = name.into();
        if self.indexes.contains_key(&name) {
            return Err(Error::DuplicateIndex { name });
        }
        self.indexes.insert(name, Box::new(index));
        Ok(())
    }

    /// Register or replace an index.
    ///
    /// Returns the previous index if one existed.
    pub fn register_or_replace<I: VectorIndex + 'static>(
        &mut self,
        name: impl Into<String>,
        index: I,
    ) -> Option<Box<dyn VectorIndex>> {
        self.indexes.insert(name.into(), Box::new(index))
    }

    /// Get a reference to an index by name.
    #[must_use]
    pub fn get(&self, name: &str) -> Option<&dyn VectorIndex> {
        self.indexes.get(name).map(AsRef::as_ref)
    }

    /// Remove an index by name.
    ///
    /// Returns the removed index if it existed.
    pub fn remove(&mut self, name: &str) -> Option<Box<dyn VectorIndex>> {
        self.indexes.remove(name)
    }

    /// Check if an index exists.
    #[must_use]
    pub fn contains(&self, name: &str) -> bool {
        self.indexes.contains_key(name)
    }

    /// List all registered index names.
    #[must_use]
    pub fn list(&self) -> Vec<&str> {
        self.indexes.keys().map(String::as_str).collect()
    }

    /// Get information about all registered indexes.
    #[must_use]
    pub fn info(&self) -> Vec<IndexInfo> {
        self.indexes
            .iter()
            .map(|(name, index)| IndexInfo {
                name: name.clone(),
                dimension: index.dimension(),
                distance_type: index.distance_type(),
                size: index.len(),
                memory_bytes: index.memory_usage(),
            })
            .collect()
    }

    /// Number of registered indexes.
    #[must_use]
    pub fn len(&self) -> usize {
        self.indexes.len()
    }

    /// Check if registry is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.indexes.is_empty()
    }

    /// Total number of vectors across all indexes.
    #[must_use]
    pub fn total_vectors(&self) -> usize {
        self.indexes.values().map(|i| i.len()).sum()
    }

    /// Total memory usage across all indexes.
    #[must_use]
    pub fn total_memory(&self) -> usize {
        self.indexes.values().map(|i| i.memory_usage()).sum()
    }

    /// Search a specific index by name.
    ///
    /// # Errors
    ///
    /// Returns error if index doesn't exist or search fails.
    pub fn search(&self, name: &str, query: &[f32], k: usize) -> Result<Vec<SearchResult>> {
        let index = self.indexes.get(name).ok_or_else(|| Error::IndexNotFound {
            name: name.to_string(),
        })?;
        index.search(query, k)
    }

    /// Search all indexes with the same query.
    ///
    /// Note: This is a sequential search. For parallel search, use `parallel_search_all`.
    ///
    /// # Errors
    ///
    /// Returns error if any search fails.
    pub fn search_all(&self, query: &[f32], k: usize) -> Result<MultiIndexResults> {
        let mut results = MultiIndexResults::new();

        for (name, index) in &self.indexes {
            // Skip indexes with incompatible dimensions
            if index.dimension() != query.len() {
                continue;
            }

            let index_results = index.search(query, k)?;
            results.add(name.clone(), index_results);
        }

        Ok(results)
    }

    /// Search multiple specific indexes.
    ///
    /// # Arguments
    ///
    /// * `names` - Index names to search
    /// * `query` - Query vector
    /// * `k` - Number of results per index
    ///
    /// # Errors
    ///
    /// Returns error if any index doesn't exist or search fails.
    pub fn search_indexes(
        &self,
        names: &[&str],
        query: &[f32],
        k: usize,
    ) -> Result<MultiIndexResults> {
        let mut results = MultiIndexResults::new();

        for name in names {
            let index = self.indexes.get(*name).ok_or_else(|| Error::IndexNotFound {
                name: (*name).to_string(),
            })?;

            // Check dimension compatibility
            if index.dimension() != query.len() {
                return Err(Error::DimensionMismatch {
                    expected: index.dimension(),
                    got: query.len(),
                });
            }

            let index_results = index.search(query, k)?;
            results.add((*name).to_string(), index_results);
        }

        Ok(results)
    }

    /// Add a vector to a specific index.
    ///
    /// # Errors
    ///
    /// Returns error if index doesn't exist or add fails.
    pub fn add(&mut self, index_name: &str, id: String, vector: &[f32]) -> Result<()> {
        let index = self
            .indexes
            .get_mut(index_name)
            .ok_or_else(|| Error::IndexNotFound {
                name: index_name.to_string(),
            })?;
        index.add(id, vector)
    }

    /// Remove a vector from a specific index.
    ///
    /// # Errors
    ///
    /// Returns error if index doesn't exist.
    pub fn remove_vector(&mut self, index_name: &str, id: &str) -> Result<bool> {
        let index = self
            .indexes
            .get_mut(index_name)
            .ok_or_else(|| Error::IndexNotFound {
                name: index_name.to_string(),
            })?;
        index.remove(id)
    }

    /// Clear all vectors from all indexes.
    pub fn clear_all(&mut self) {
        for index in self.indexes.values_mut() {
            index.clear();
        }
    }
}

/// Thread-safe shared registry using Arc<RwLock>.
pub type SharedRegistry = Arc<RwLock<IndexRegistry>>;

/// Create a new shared registry.
#[must_use]
pub fn shared_registry() -> SharedRegistry {
    Arc::new(RwLock::new(IndexRegistry::new()))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::index::{FlatIndex, IndexConfig};

    fn create_test_index(dim: usize) -> FlatIndex {
        FlatIndex::new(IndexConfig::new(dim))
    }

    #[test]
    fn test_register_and_get() {
        let mut registry = IndexRegistry::new();
        let index = create_test_index(128);

        registry.register("test", index).unwrap();

        assert!(registry.contains("test"));
        assert!(!registry.contains("other"));
        assert_eq!(registry.len(), 1);

        let retrieved = registry.get("test").unwrap();
        assert_eq!(retrieved.dimension(), 128);
    }

    #[test]
    fn test_duplicate_register_error() {
        let mut registry = IndexRegistry::new();

        registry.register("test", create_test_index(128)).unwrap();
        let result = registry.register("test", create_test_index(256));

        assert!(result.is_err());
    }

    #[test]
    fn test_register_or_replace() {
        let mut registry = IndexRegistry::new();

        // First registration
        let old = registry.register_or_replace("test", create_test_index(128));
        assert!(old.is_none());

        // Replace
        let old = registry.register_or_replace("test", create_test_index(256));
        assert!(old.is_some());
        assert_eq!(old.unwrap().dimension(), 128);

        // New index has new dimension
        assert_eq!(registry.get("test").unwrap().dimension(), 256);
    }

    #[test]
    fn test_remove() {
        let mut registry = IndexRegistry::new();
        registry.register("test", create_test_index(128)).unwrap();

        let removed = registry.remove("test");
        assert!(removed.is_some());
        assert_eq!(removed.unwrap().dimension(), 128);
        assert!(registry.is_empty());
    }

    #[test]
    fn test_list_and_info() {
        let mut registry = IndexRegistry::new();
        registry.register("a", create_test_index(128)).unwrap();
        registry.register("b", create_test_index(256)).unwrap();

        let names = registry.list();
        assert_eq!(names.len(), 2);
        assert!(names.contains(&"a"));
        assert!(names.contains(&"b"));

        let info = registry.info();
        assert_eq!(info.len(), 2);
    }

    #[test]
    fn test_search_specific_index() {
        let mut registry = IndexRegistry::new();
        let mut index = create_test_index(4);

        // Add vectors
        index.add("v1".to_string(), &[1.0, 0.0, 0.0, 0.0]).unwrap();
        index.add("v2".to_string(), &[0.0, 1.0, 0.0, 0.0]).unwrap();

        registry.register("test", index).unwrap();

        let query = [1.0, 0.0, 0.0, 0.0];
        let results = registry.search("test", &query, 2).unwrap();

        assert_eq!(results.len(), 2);
        assert_eq!(results[0].id, "v1"); // Closest to query
    }

    #[test]
    fn test_search_nonexistent_index() {
        let registry = IndexRegistry::new();
        let result = registry.search("nonexistent", &[1.0], 1);

        assert!(result.is_err());
    }

    #[test]
    fn test_search_all() {
        let mut registry = IndexRegistry::new();

        // Create two indexes with same dimension
        let mut index1 = create_test_index(4);
        index1.add("a1".to_string(), &[1.0, 0.0, 0.0, 0.0]).unwrap();

        let mut index2 = create_test_index(4);
        index2.add("b1".to_string(), &[0.0, 1.0, 0.0, 0.0]).unwrap();

        registry.register("index1", index1).unwrap();
        registry.register("index2", index2).unwrap();

        let query = [0.5, 0.5, 0.0, 0.0];
        let results = registry.search_all(&query, 10).unwrap();

        assert_eq!(results.by_index.len(), 2);
        assert_eq!(results.total_count, 2);
    }

    #[test]
    fn test_search_all_skips_incompatible_dimensions() {
        let mut registry = IndexRegistry::new();

        let mut index1 = create_test_index(4);
        index1.add("a1".to_string(), &[1.0, 0.0, 0.0, 0.0]).unwrap();

        let mut index2 = create_test_index(8); // Different dimension
        index2
            .add("b1".to_string(), &[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
            .unwrap();

        registry.register("index1", index1).unwrap();
        registry.register("index2", index2).unwrap();

        // Query with dimension 4 - should only search index1
        let query = [0.5, 0.5, 0.0, 0.0];
        let results = registry.search_all(&query, 10).unwrap();

        assert_eq!(results.by_index.len(), 1);
        assert_eq!(results.by_index[0].index_name, "index1");
    }

    #[test]
    fn test_search_indexes() {
        let mut registry = IndexRegistry::new();

        let mut index1 = create_test_index(4);
        index1.add("a1".to_string(), &[1.0, 0.0, 0.0, 0.0]).unwrap();

        let mut index2 = create_test_index(4);
        index2.add("b1".to_string(), &[0.0, 1.0, 0.0, 0.0]).unwrap();

        let mut index3 = create_test_index(4);
        index3.add("c1".to_string(), &[0.0, 0.0, 1.0, 0.0]).unwrap();

        registry.register("idx1", index1).unwrap();
        registry.register("idx2", index2).unwrap();
        registry.register("idx3", index3).unwrap();

        // Only search idx1 and idx2
        let query = [0.5, 0.5, 0.0, 0.0];
        let results = registry
            .search_indexes(&["idx1", "idx2"], &query, 10)
            .unwrap();

        assert_eq!(results.by_index.len(), 2);
        assert_eq!(results.total_count, 2);
    }

    #[test]
    fn test_add_to_index() {
        let mut registry = IndexRegistry::new();
        registry.register("test", create_test_index(4)).unwrap();

        registry
            .add("test", "v1".to_string(), &[1.0, 0.0, 0.0, 0.0])
            .unwrap();

        assert_eq!(registry.get("test").unwrap().len(), 1);
    }

    #[test]
    fn test_multi_index_results_flatten() {
        let mut results = MultiIndexResults::new();

        results.add(
            "idx1".to_string(),
            vec![SearchResult::new("a".to_string(), 0.5, DistanceType::L2)],
        );
        results.add(
            "idx2".to_string(),
            vec![SearchResult::new("b".to_string(), 0.3, DistanceType::L2)],
        );

        let flat = results.flatten();
        assert_eq!(flat.len(), 2);
        assert_eq!(flat[0].0, "idx1");
        assert_eq!(flat[0].1.id, "a");
        assert_eq!(flat[1].0, "idx2");
        assert_eq!(flat[1].1.id, "b");
    }

    #[test]
    fn test_total_vectors_and_memory() {
        let mut registry = IndexRegistry::new();

        let mut index1 = create_test_index(4);
        index1.add("a".to_string(), &[1.0, 0.0, 0.0, 0.0]).unwrap();
        index1.add("b".to_string(), &[0.0, 1.0, 0.0, 0.0]).unwrap();

        let mut index2 = create_test_index(4);
        index2.add("c".to_string(), &[0.0, 0.0, 1.0, 0.0]).unwrap();

        registry.register("idx1", index1).unwrap();
        registry.register("idx2", index2).unwrap();

        assert_eq!(registry.total_vectors(), 3);
        assert!(registry.total_memory() > 0);
    }

    #[test]
    fn test_clear_all() {
        let mut registry = IndexRegistry::new();

        let mut index1 = create_test_index(4);
        index1.add("a".to_string(), &[1.0, 0.0, 0.0, 0.0]).unwrap();

        let mut index2 = create_test_index(4);
        index2.add("b".to_string(), &[0.0, 1.0, 0.0, 0.0]).unwrap();

        registry.register("idx1", index1).unwrap();
        registry.register("idx2", index2).unwrap();

        assert_eq!(registry.total_vectors(), 2);

        registry.clear_all();

        assert_eq!(registry.total_vectors(), 0);
        assert_eq!(registry.len(), 2); // Indexes still exist, just empty
    }

    #[test]
    fn test_shared_registry() {
        let registry = shared_registry();

        // Write access
        {
            let mut reg = registry.write();
            reg.register("test", create_test_index(128)).unwrap();
        }

        // Read access
        {
            let reg = registry.read();
            assert!(reg.contains("test"));
        }
    }
}