rrag 0.1.0-alpha.2

High-performance Rust framework for Retrieval-Augmented Generation with pluggable components, async-first design, and comprehensive observability
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
//! # Core Cache Implementations
//!
//! Foundation cache data structures with different eviction policies.

use super::{Cache, CacheEntryMetadata, CacheStats};
use crate::RragResult;
use std::collections::{HashMap, VecDeque};
use std::hash::Hash;
use std::time::{Duration, SystemTime};

/// LRU Cache implementation
pub struct LRUCache<K, V>
where
    K: Hash + Eq + Clone + Send + Sync + 'static,
    V: Clone + Send + Sync + 'static,
{
    /// Internal storage
    storage: HashMap<K, CacheNode<V>>,

    /// Access order tracking
    access_order: VecDeque<K>,

    /// Maximum capacity
    max_size: usize,

    /// Cache statistics
    stats: CacheStats,

    /// Thread safety
    _phantom: std::marker::PhantomData<(K, V)>,
}

/// LFU Cache implementation
pub struct LFUCache<K, V>
where
    K: Hash + Eq + Clone + Send + Sync + 'static,
    V: Clone + Send + Sync + 'static,
{
    /// Internal storage
    storage: HashMap<K, CacheNode<V>>,

    /// Frequency tracking
    frequencies: HashMap<K, u64>,

    /// Frequency buckets for efficient eviction
    frequency_buckets: HashMap<u64, Vec<K>>,

    /// Minimum frequency
    min_frequency: u64,

    /// Maximum capacity
    max_size: usize,

    /// Cache statistics
    stats: CacheStats,
}

/// TTL Cache implementation
pub struct TTLCache<K, V>
where
    K: Hash + Eq + Clone + Send + Sync + 'static,
    V: Clone + Send + Sync + 'static,
{
    /// Internal storage with expiry
    storage: HashMap<K, (V, SystemTime)>,

    /// Default TTL
    default_ttl: Duration,

    /// Cleanup interval
    cleanup_interval: Duration,

    /// Last cleanup time
    last_cleanup: SystemTime,

    /// Cache statistics
    stats: CacheStats,
}

/// ARC (Adaptive Replacement Cache) implementation
pub struct ARCCache<K, V>
where
    K: Hash + Eq + Clone + Send + Sync + 'static,
    V: Clone + Send + Sync + 'static,
{
    /// Recently used cache (T1)
    t1: HashMap<K, V>,

    /// Frequently used cache (T2)
    t2: HashMap<K, V>,

    /// Ghost entries recently evicted from T1 (B1)
    b1: HashMap<K, ()>,

    /// Ghost entries recently evicted from T2 (B2)
    b2: HashMap<K, ()>,

    /// LRU lists for T1 and T2
    t1_lru: VecDeque<K>,
    t2_lru: VecDeque<K>,
    b1_lru: VecDeque<K>,
    b2_lru: VecDeque<K>,

    /// Adaptive parameter
    p: f32,

    /// Maximum capacity
    max_size: usize,

    /// Cache statistics
    stats: CacheStats,
}

/// Semantic-aware cache implementation
pub struct SemanticAwareCache<K, V>
where
    K: Hash + Eq + Clone + Send + Sync + 'static,
    V: Clone + Send + Sync + 'static,
{
    /// Primary storage
    storage: HashMap<K, CacheNode<V>>,

    /// Semantic similarity tracking
    similarity_groups: HashMap<u64, Vec<K>>,

    /// Embedding vectors for similarity computation
    embeddings: HashMap<K, Vec<f32>>,

    /// Access patterns
    access_patterns: HashMap<K, AccessPattern>,

    /// Maximum capacity
    max_size: usize,

    /// Similarity threshold for grouping
    similarity_threshold: f32,

    /// Cache statistics
    stats: CacheStats,
}

/// Cache node with metadata
#[derive(Debug, Clone)]
pub struct CacheNode<V> {
    /// The cached value
    pub value: V,

    /// Entry metadata
    pub metadata: CacheEntryMetadata,

    /// Computed size in bytes (approximate)
    pub size_bytes: usize,
}

/// Access pattern tracking
#[derive(Debug, Clone)]
pub struct AccessPattern {
    /// Total accesses
    pub count: u64,

    /// Recent access times
    pub recent_accesses: VecDeque<SystemTime>,

    /// Average access interval
    pub avg_interval: Duration,

    /// Access trend (increasing, decreasing, stable)
    pub trend: AccessTrend,
}

/// Access trend types
#[derive(Debug, Clone, Copy)]
pub enum AccessTrend {
    Increasing,
    Decreasing,
    Stable,
    Unknown,
}

/// Priority entry for frequency-based eviction
#[derive(Debug, Clone, PartialEq, Eq)]
struct FrequencyEntry<K>
where
    K: Ord,
{
    key: K,
    frequency: u64,
    last_access: SystemTime,
}

impl<K, V> LRUCache<K, V>
where
    K: Hash + Eq + Clone + Send + Sync + 'static,
    V: Clone + Send + Sync + 'static,
{
    /// Create new LRU cache
    pub fn new(max_size: usize) -> Self {
        Self {
            storage: HashMap::with_capacity(max_size),
            access_order: VecDeque::with_capacity(max_size),
            max_size,
            stats: CacheStats::default(),
            _phantom: std::marker::PhantomData,
        }
    }

    /// Update LRU order
    fn update_lru(&mut self, key: &K) {
        // Remove from current position
        if let Some(pos) = self.access_order.iter().position(|k| k == key) {
            self.access_order.remove(pos);
        }

        // Add to front (most recent)
        self.access_order.push_front(key.clone());
    }

    /// Evict least recently used entry
    fn evict_lru(&mut self) -> Option<K> {
        if let Some(key) = self.access_order.pop_back() {
            self.storage.remove(&key);
            self.stats.evictions += 1;
            Some(key)
        } else {
            None
        }
    }
}

impl<K, V> Cache<K, V> for LRUCache<K, V>
where
    K: Hash + Eq + Clone + Send + Sync + 'static,
    V: Clone + Send + Sync + 'static,
{
    fn get(&self, key: &K) -> Option<V> {
        let _start_time = SystemTime::now();

        if let Some(node) = self.storage.get(key) {
            // Update stats - hits handled by mutable reference in real implementation
            Some(node.value.clone())
        } else {
            // Miss handled by mutable reference in real implementation
            None
        }
    }

    fn put(&mut self, key: K, value: V) -> RragResult<()> {
        let size_bytes = std::mem::size_of::<V>();
        let node = CacheNode {
            value,
            metadata: CacheEntryMetadata::new(),
            size_bytes,
        };

        // If key exists, update and move to front
        if self.storage.contains_key(&key) {
            self.storage.insert(key.clone(), node);
            self.update_lru(&key);
            return Ok(());
        }

        // If at capacity, evict LRU
        if self.storage.len() >= self.max_size {
            self.evict_lru();
        }

        // Insert new entry
        self.storage.insert(key.clone(), node);
        self.update_lru(&key);

        Ok(())
    }

    fn remove(&mut self, key: &K) -> Option<V> {
        if let Some(node) = self.storage.remove(key) {
            // Remove from LRU order
            if let Some(pos) = self.access_order.iter().position(|k| k == key) {
                self.access_order.remove(pos);
            }
            Some(node.value)
        } else {
            None
        }
    }

    fn contains(&self, key: &K) -> bool {
        self.storage.contains_key(key)
    }

    fn clear(&mut self) {
        self.storage.clear();
        self.access_order.clear();
        self.stats = CacheStats::default();
    }

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

    fn stats(&self) -> CacheStats {
        self.stats.clone()
    }
}

impl<K, V> LFUCache<K, V>
where
    K: Hash + Eq + Clone + Send + Sync + 'static,
    V: Clone + Send + Sync + 'static,
{
    /// Create new LFU cache
    pub fn new(max_size: usize) -> Self {
        Self {
            storage: HashMap::with_capacity(max_size),
            frequencies: HashMap::with_capacity(max_size),
            frequency_buckets: HashMap::new(),
            min_frequency: 1,
            max_size,
            stats: CacheStats::default(),
        }
    }

    /// Update frequency
    fn update_frequency(&mut self, key: &K) {
        let old_freq = self.frequencies.get(key).copied().unwrap_or(0);
        let new_freq = old_freq + 1;

        self.frequencies.insert(key.clone(), new_freq);

        // Update frequency buckets
        if old_freq > 0 {
            if let Some(bucket) = self.frequency_buckets.get_mut(&old_freq) {
                bucket.retain(|k| k != key);
                if bucket.is_empty() && old_freq == self.min_frequency {
                    self.min_frequency += 1;
                }
            }
        }

        self.frequency_buckets
            .entry(new_freq)
            .or_insert_with(Vec::new)
            .push(key.clone());
    }

    /// Evict least frequently used entry
    fn evict_lfu(&mut self) -> Option<K> {
        if let Some(bucket) = self.frequency_buckets.get_mut(&self.min_frequency) {
            if let Some(key) = bucket.pop() {
                self.storage.remove(&key);
                self.frequencies.remove(&key);
                self.stats.evictions += 1;
                return Some(key);
            }
        }
        None
    }
}

impl<K, V> Cache<K, V> for LFUCache<K, V>
where
    K: Hash + Eq + Clone + Send + Sync + 'static,
    V: Clone + Send + Sync + 'static,
{
    fn get(&self, key: &K) -> Option<V> {
        if let Some(node) = self.storage.get(key) {
            Some(node.value.clone())
        } else {
            None
        }
    }

    fn put(&mut self, key: K, value: V) -> RragResult<()> {
        let size_bytes = std::mem::size_of::<V>();
        let node = CacheNode {
            value,
            metadata: CacheEntryMetadata::new(),
            size_bytes,
        };

        // If key exists, update
        if self.storage.contains_key(&key) {
            self.storage.insert(key.clone(), node);
            self.update_frequency(&key);
            return Ok(());
        }

        // If at capacity, evict LFU
        if self.storage.len() >= self.max_size {
            self.evict_lfu();
        }

        // Insert new entry
        self.storage.insert(key.clone(), node);
        self.update_frequency(&key);

        Ok(())
    }

    fn remove(&mut self, key: &K) -> Option<V> {
        if let Some(node) = self.storage.remove(key) {
            self.frequencies.remove(key);
            Some(node.value)
        } else {
            None
        }
    }

    fn contains(&self, key: &K) -> bool {
        self.storage.contains_key(key)
    }

    fn clear(&mut self) {
        self.storage.clear();
        self.frequencies.clear();
        self.frequency_buckets.clear();
        self.min_frequency = 1;
        self.stats = CacheStats::default();
    }

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

    fn stats(&self) -> CacheStats {
        self.stats.clone()
    }
}

impl<K, V> TTLCache<K, V>
where
    K: Hash + Eq + Clone + Send + Sync + 'static,
    V: Clone + Send + Sync + 'static,
{
    /// Create new TTL cache
    pub fn new(default_ttl: Duration) -> Self {
        Self {
            storage: HashMap::new(),
            default_ttl,
            cleanup_interval: Duration::from_secs(60), // 1 minute
            last_cleanup: SystemTime::now(),
            stats: CacheStats::default(),
        }
    }

    /// Cleanup expired entries
    fn cleanup_expired(&mut self) {
        let now = SystemTime::now();

        // Only cleanup if interval has passed
        if now.duration_since(self.last_cleanup).unwrap_or_default() < self.cleanup_interval {
            return;
        }

        let before_count = self.storage.len();
        self.storage.retain(|_key, (_, expiry)| now < *expiry);
        let after_count = self.storage.len();

        self.stats.evictions += (before_count - after_count) as u64;
        self.last_cleanup = now;
    }
}

impl<K, V> Cache<K, V> for TTLCache<K, V>
where
    K: Hash + Eq + Clone + Send + Sync + 'static,
    V: Clone + Send + Sync + 'static,
{
    fn get(&self, key: &K) -> Option<V> {
        if let Some((value, expiry)) = self.storage.get(key) {
            if SystemTime::now() < *expiry {
                Some(value.clone())
            } else {
                None
            }
        } else {
            None
        }
    }

    fn put(&mut self, key: K, value: V) -> RragResult<()> {
        let expiry = SystemTime::now() + self.default_ttl;
        self.storage.insert(key, (value, expiry));

        // Periodic cleanup
        self.cleanup_expired();

        Ok(())
    }

    fn remove(&mut self, key: &K) -> Option<V> {
        self.storage.remove(key).map(|(value, _)| value)
    }

    fn contains(&self, key: &K) -> bool {
        if let Some((_, expiry)) = self.storage.get(key) {
            SystemTime::now() < *expiry
        } else {
            false
        }
    }

    fn clear(&mut self) {
        self.storage.clear();
        self.stats = CacheStats::default();
    }

    fn size(&self) -> usize {
        // Count only non-expired entries
        let now = SystemTime::now();
        self.storage
            .values()
            .filter(|(_, expiry)| now < *expiry)
            .count()
    }

    fn stats(&self) -> CacheStats {
        self.stats.clone()
    }
}

impl<K> PartialOrd for FrequencyEntry<K>
where
    K: Ord,
{
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl<K> Ord for FrequencyEntry<K>
where
    K: Ord,
{
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        // Lower frequency first (for min-heap)
        self.frequency
            .cmp(&other.frequency)
            .then_with(|| self.last_access.cmp(&other.last_access))
    }
}

impl AccessPattern {
    /// Create new access pattern
    pub fn new() -> Self {
        Self {
            count: 0,
            recent_accesses: VecDeque::new(),
            avg_interval: Duration::from_secs(0),
            trend: AccessTrend::Unknown,
        }
    }

    /// Record an access
    pub fn record_access(&mut self) {
        let now = SystemTime::now();
        self.count += 1;
        self.recent_accesses.push_back(now);

        // Keep only recent accesses (last 10)
        if self.recent_accesses.len() > 10 {
            self.recent_accesses.pop_front();
        }

        self.update_metrics();
    }

    /// Update computed metrics
    fn update_metrics(&mut self) {
        if self.recent_accesses.len() < 2 {
            return;
        }

        // Calculate average interval
        let mut total_interval = Duration::from_secs(0);
        let mut interval_count = 0;

        for window in self.recent_accesses.as_slices().0.windows(2) {
            if let Ok(interval) = window[1].duration_since(window[0]) {
                total_interval += interval;
                interval_count += 1;
            }
        }

        if interval_count > 0 {
            self.avg_interval = total_interval / interval_count as u32;
        }

        // Determine trend (simplified)
        if self.recent_accesses.len() >= 4 {
            let _first_half_avg = self.recent_accesses.len() / 2;
            // Trend analysis would go here
            self.trend = AccessTrend::Stable; // Simplified for now
        }
    }
}

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

    #[test]
    fn test_lru_cache() {
        let mut cache = LRUCache::new(3);

        cache.put("a".to_string(), 1).unwrap();
        cache.put("b".to_string(), 2).unwrap();
        cache.put("c".to_string(), 3).unwrap();

        assert_eq!(cache.size(), 3);
        assert_eq!(cache.get(&"a".to_string()), Some(1));

        // This should evict the LRU entry
        cache.put("d".to_string(), 4).unwrap();
        assert_eq!(cache.size(), 3);
    }

    #[test]
    fn test_lfu_cache() {
        let mut cache = LFUCache::new(2);

        cache.put("a".to_string(), 1).unwrap();
        cache.put("b".to_string(), 2).unwrap();

        // Access 'a' more frequently
        cache.get(&"a".to_string());
        cache.get(&"a".to_string());

        // This should evict 'b' (less frequent)
        cache.put("c".to_string(), 3).unwrap();

        assert_eq!(cache.get(&"a".to_string()), Some(1));
        assert_eq!(cache.get(&"b".to_string()), None);
        assert_eq!(cache.get(&"c".to_string()), Some(3));
    }

    #[test]
    fn test_ttl_cache() {
        let mut cache = TTLCache::new(Duration::from_millis(100));

        cache.put("key".to_string(), "value".to_string()).unwrap();
        assert_eq!(cache.get(&"key".to_string()), Some("value".to_string()));

        // Sleep longer than TTL
        std::thread::sleep(Duration::from_millis(150));
        assert_eq!(cache.get(&"key".to_string()), None);
    }

    #[test]
    fn test_access_pattern() {
        let mut pattern = AccessPattern::new();
        assert_eq!(pattern.count, 0);

        pattern.record_access();
        assert_eq!(pattern.count, 1);

        pattern.record_access();
        assert_eq!(pattern.count, 2);
    }
}