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
//! In-Memory Record Store
//!
//! High-performance in-memory storage for MemoryRecords using HashMap.
//!
//! # Thread Safety
//!
//! The store itself is not thread-safe. For concurrent access,
//! wrap it in `SharedStore` using `shared_store()`.
//!
//! # Performance
//!
//! - Insert: O(1) amortized
//! - Get: O(1)
//! - Remove: O(1)
//! - Memory: ~(record_size * n) + HashMap overhead

use crate::error::{Error, Result};
use crate::store::traits::RecordStore;
use crate::types::{MemoryRecord, RecordId, RecordStatus};
use ahash::AHashMap;

/// In-memory record store.
///
/// Stores records in a HashMap for O(1) access.
///
/// # Example
///
/// ```ignore
/// use rag_plusplus_core::store::{InMemoryStore, RecordStore};
/// use rag_plusplus_core::types::MemoryRecord;
///
/// let mut store = InMemoryStore::new();
/// store.insert(record)?;
///
/// if let Some(record) = store.get(&"my-id".into()) {
///     println!("Found: {}", record.context);
/// }
/// ```
#[derive(Debug, Default)]
pub struct InMemoryStore {
    /// Records by ID
    records: AHashMap<RecordId, MemoryRecord>,
    /// Track memory usage (approximate)
    estimated_bytes: usize,
}

impl InMemoryStore {
    /// Create a new empty store.
    #[must_use]
    pub fn new() -> Self {
        Self {
            records: AHashMap::new(),
            estimated_bytes: 0,
        }
    }

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

    /// Estimate memory usage of a record.
    fn estimate_record_size(record: &MemoryRecord) -> usize {
        // Base struct size
        let base = std::mem::size_of::<MemoryRecord>();

        // String allocations
        let id_size = record.id.capacity();
        let context_size = record.context.capacity();

        // Embedding vector
        let embedding_size = record.embedding.capacity() * std::mem::size_of::<f32>();

        // Metadata (rough estimate)
        let metadata_size = record.metadata.len() * 64; // Assume 64 bytes per entry

        base + id_size + context_size + embedding_size + metadata_size
    }

    /// Get statistics about the store.
    #[must_use]
    pub fn stats(&self) -> StoreStats {
        let active_count = self
            .records
            .values()
            .filter(|r| r.status == RecordStatus::Active)
            .count();

        let total_outcomes: u64 = self.records.values().map(|r| r.stats.count()).sum();

        StoreStats {
            total_records: self.records.len(),
            active_records: active_count,
            total_outcome_updates: total_outcomes,
            memory_bytes: self.estimated_bytes,
        }
    }

    /// Iterate over all records.
    pub fn iter(&self) -> impl Iterator<Item = &MemoryRecord> {
        self.records.values()
    }

    /// Iterate over active records only.
    pub fn iter_active(&self) -> impl Iterator<Item = &MemoryRecord> {
        self.records
            .values()
            .filter(|r| r.status == RecordStatus::Active)
    }

    /// Get mutable reference to a record (internal use only).
    fn get_mut(&mut self, id: &RecordId) -> Option<&mut MemoryRecord> {
        self.records.get_mut(id)
    }

    /// Estimated memory usage in bytes.
    #[must_use]
    pub fn memory_bytes(&self) -> usize {
        self.estimated_bytes
    }
}

impl RecordStore for InMemoryStore {
    fn insert(&mut self, record: MemoryRecord) -> Result<RecordId> {
        let id = record.id.clone();

        if self.records.contains_key(&id) {
            return Err(Error::DuplicateRecord {
                record_id: id.to_string(),
            });
        }

        let size = Self::estimate_record_size(&record);
        self.records.insert(id.clone(), record);
        self.estimated_bytes += size;

        Ok(id)
    }

    fn get(&self, id: &RecordId) -> Option<MemoryRecord> {
        self.records.get(id).cloned()
    }

    fn contains(&self, id: &RecordId) -> bool {
        self.records.contains_key(id)
    }

    fn update_stats(&mut self, id: &RecordId, outcome: f64) -> Result<()> {
        let record = self.get_mut(id).ok_or_else(|| Error::RecordNotFound {
            record_id: id.to_string(),
        })?;

        // Only update stats, never modify core record data (INV-001)
        record.stats.update_scalar(outcome);

        Ok(())
    }

    fn remove(&mut self, id: &RecordId) -> Result<bool> {
        if let Some(record) = self.records.remove(id) {
            let size = Self::estimate_record_size(&record);
            self.estimated_bytes = self.estimated_bytes.saturating_sub(size);
            Ok(true)
        } else {
            Ok(false)
        }
    }

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

    fn clear(&mut self) {
        self.records.clear();
        self.estimated_bytes = 0;
    }

    fn ids(&self) -> Vec<RecordId> {
        self.records.keys().cloned().collect()
    }

    fn memory_usage(&self) -> usize {
        self.estimated_bytes
    }
}

/// Store statistics.
#[derive(Debug, Clone, Default)]
pub struct StoreStats {
    /// Total number of records (including archived/deleted)
    pub total_records: usize,
    /// Number of active records
    pub active_records: usize,
    /// Total outcome updates across all records
    pub total_outcome_updates: u64,
    /// Estimated memory usage in bytes
    pub memory_bytes: usize,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::store::traits::tests::*;
    use crate::OutcomeStats;

    fn create_test_record(id: &str) -> MemoryRecord {
        MemoryRecord {
            id: id.into(),
            embedding: vec![1.0, 2.0, 3.0],
            context: format!("Context for {id}"),
            outcome: 0.5,
            metadata: Default::default(),
            created_at: 1234567890,
            status: RecordStatus::Active,
            stats: OutcomeStats::new(1),
        }
    }

    #[test]
    fn test_new_store() {
        let store = InMemoryStore::new();
        assert!(store.is_empty());
        assert_eq!(store.len(), 0);
    }

    #[test]
    fn test_with_capacity() {
        let store = InMemoryStore::with_capacity(100);
        assert!(store.is_empty());
    }

    #[test]
    fn test_insert_and_get() {
        let mut store = InMemoryStore::new();
        let record = create_test_record("test-1");

        let id = store.insert(record).unwrap();
        assert_eq!(id.as_str(), "test-1");

        let retrieved = store.get(&id).unwrap();
        assert_eq!(retrieved.id.as_str(), "test-1");
        assert_eq!(retrieved.context, "Context for test-1");
    }

    #[test]
    fn test_duplicate_insert_error() {
        let mut store = InMemoryStore::new();
        let record = create_test_record("dup");

        store.insert(record.clone()).unwrap();
        let result = store.insert(record);

        assert!(result.is_err());
    }

    #[test]
    fn test_update_stats() {
        let mut store = InMemoryStore::new();
        store.insert(create_test_record("stats-test")).unwrap();

        let id: RecordId = "stats-test".into();

        store.update_stats(&id, 0.8).unwrap();
        store.update_stats(&id, 0.9).unwrap();
        store.update_stats(&id, 0.7).unwrap();

        let record = store.get(&id).unwrap();
        assert_eq!(record.stats.count(), 3);
        assert!((record.stats.mean_scalar().unwrap() - 0.8).abs() < 0.01);
    }

    #[test]
    fn test_update_stats_not_found() {
        let mut store = InMemoryStore::new();
        let result = store.update_stats(&"nonexistent".into(), 0.5);
        assert!(result.is_err());
    }

    #[test]
    fn test_remove() {
        let mut store = InMemoryStore::new();
        store.insert(create_test_record("to-remove")).unwrap();

        assert_eq!(store.len(), 1);

        let removed = store.remove(&"to-remove".into()).unwrap();
        assert!(removed);
        assert_eq!(store.len(), 0);

        let removed_again = store.remove(&"to-remove".into()).unwrap();
        assert!(!removed_again);
    }

    #[test]
    fn test_iter() {
        let mut store = InMemoryStore::new();

        for i in 0..5 {
            store.insert(create_test_record(&format!("iter-{i}"))).unwrap();
        }

        let count = store.iter().count();
        assert_eq!(count, 5);
    }

    #[test]
    fn test_iter_active() {
        let mut store = InMemoryStore::new();

        for i in 0..5 {
            let mut record = create_test_record(&format!("active-{i}"));
            if i % 2 == 0 {
                record.status = RecordStatus::Archived;
            }
            store.insert(record).unwrap();
        }

        let active_count = store.iter_active().count();
        assert_eq!(active_count, 2); // Only odd indices are active
    }

    #[test]
    fn test_stats() {
        let mut store = InMemoryStore::new();

        for i in 0..10 {
            store.insert(create_test_record(&format!("stat-{i}"))).unwrap();
        }

        store.update_stats(&"stat-0".into(), 0.5).unwrap();
        store.update_stats(&"stat-0".into(), 0.6).unwrap();
        store.update_stats(&"stat-1".into(), 0.7).unwrap();

        let stats = store.stats();
        assert_eq!(stats.total_records, 10);
        assert_eq!(stats.active_records, 10);
        assert_eq!(stats.total_outcome_updates, 3);
        assert!(stats.memory_bytes > 0);
    }

    #[test]
    fn test_memory_tracking() {
        let mut store = InMemoryStore::new();

        let initial = store.memory_usage();
        assert_eq!(initial, 0);

        store.insert(create_test_record("mem-1")).unwrap();
        let after_one = store.memory_usage();
        assert!(after_one > 0);

        store.insert(create_test_record("mem-2")).unwrap();
        let after_two = store.memory_usage();
        assert!(after_two > after_one);

        store.remove(&"mem-1".into()).unwrap();
        let after_remove = store.memory_usage();
        assert!(after_remove < after_two);
    }

    #[test]
    fn test_clear() {
        let mut store = InMemoryStore::new();

        for i in 0..10 {
            store.insert(create_test_record(&format!("clear-{i}"))).unwrap();
        }

        assert_eq!(store.len(), 10);
        assert!(store.memory_usage() > 0);

        store.clear();

        assert!(store.is_empty());
        assert_eq!(store.memory_usage(), 0);
    }

    #[test]
    fn test_ids() {
        let mut store = InMemoryStore::new();

        for i in 0..5 {
            store.insert(create_test_record(&format!("id-{i}"))).unwrap();
        }

        let ids = store.ids();
        assert_eq!(ids.len(), 5);
    }

    // Run trait compliance tests
    #[test]
    fn test_trait_basic_crud() {
        let mut store = InMemoryStore::new();
        test_basic_crud(&mut store);
    }

    #[test]
    fn test_trait_batch_operations() {
        let mut store = InMemoryStore::new();
        test_batch_operations(&mut store);
    }

    #[test]
    fn test_trait_duplicate_insert() {
        let mut store = InMemoryStore::new();
        test_duplicate_insert(&mut store);
    }
}