reddb-io-server 1.1.2

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
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
//! Query Result Cache
//!
//! Caches query results for expensive cross-modal queries.
//! Supports TTL-based expiration and dependency-based invalidation.
//!
//! # Features
//!
//! - **Result Caching**: Cache expensive query results
//! - **TTL Expiration**: Automatic expiry based on time
//! - **Dependency Tracking**: Invalidate when underlying data changes
//! - **Size Management**: LRU eviction based on memory usage
//! - **Statistics**: Cache hit/miss tracking
//!
//! # Example
//!
//! ```ignore
//! use storage::cache::result::{ResultCache, CacheKey, CachePolicy};
//!
//! let mut cache = ResultCache::new(100_000_000); // 100MB max
//!
//! let key = CacheKey::new("attack_paths")
//!     .param("from", "external")
//!     .param("to", "database");
//!
//! if let Some(result) = cache.get(&key) {
//!     return result;
//! }
//!
//! let result = expensive_query();
//! cache.insert(key, result.clone(), CachePolicy::default()
//!     .ttl(Duration::from_secs(300))
//!     .depends_on(&["hosts", "vulnerabilities"]));
//! ```

use std::collections::{HashMap, HashSet};
use std::hash::{Hash, Hasher};
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, Instant};

// ============================================================================
// Cache Key
// ============================================================================

/// Cache key for query results
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CacheKey {
    /// Query type identifier
    pub query_type: String,
    /// Query parameters (sorted for consistent hashing)
    pub params: Vec<(String, String)>,
    /// Hash for fast comparison
    hash: u64,
}

impl CacheKey {
    /// Create a new cache key
    pub fn new(query_type: impl Into<String>) -> Self {
        let query_type = query_type.into();
        let mut key = Self {
            query_type,
            params: Vec::new(),
            hash: 0,
        };
        key.rehash();
        key
    }

    /// Add a parameter to the key
    pub fn param(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.params.push((name.into(), value.into()));
        self.params.sort_by(|a, b| a.0.cmp(&b.0));
        self.rehash();
        self
    }

    /// Add multiple parameters
    pub fn params(mut self, params: impl IntoIterator<Item = (String, String)>) -> Self {
        self.params.extend(params);
        self.params.sort_by(|a, b| a.0.cmp(&b.0));
        self.rehash();
        self
    }

    fn rehash(&mut self) {
        use std::collections::hash_map::DefaultHasher;
        let mut hasher = DefaultHasher::new();
        self.query_type.hash(&mut hasher);
        for (k, v) in &self.params {
            k.hash(&mut hasher);
            v.hash(&mut hasher);
        }
        self.hash = hasher.finish();
    }
}

impl Hash for CacheKey {
    fn hash<H: Hasher>(&self, state: &mut H) {
        state.write_u64(self.hash);
    }
}

// ============================================================================
// Cache Policy
// ============================================================================

/// Policy for cache entry behavior
#[derive(Debug, Clone)]
pub struct CachePolicy {
    /// Time to live
    pub ttl: Duration,
    /// Tables/collections this result depends on
    pub dependencies: HashSet<String>,
    /// Priority for eviction (higher = keep longer)
    pub priority: u8,
    /// Whether to refresh on access (sliding expiration)
    pub sliding: bool,
}

impl Default for CachePolicy {
    fn default() -> Self {
        Self {
            ttl: Duration::from_secs(300), // 5 minutes
            dependencies: HashSet::new(),
            priority: 50,
            sliding: false,
        }
    }
}

impl CachePolicy {
    /// Set TTL
    pub fn ttl(mut self, ttl: Duration) -> Self {
        self.ttl = ttl;
        self
    }

    /// Add dependencies
    pub fn depends_on(mut self, deps: &[&str]) -> Self {
        for dep in deps {
            self.dependencies.insert(dep.to_string());
        }
        self
    }

    /// Set priority
    pub fn priority(mut self, priority: u8) -> Self {
        self.priority = priority;
        self
    }

    /// Enable sliding expiration
    pub fn sliding(mut self) -> Self {
        self.sliding = true;
        self
    }
}

// ============================================================================
// Cache Entry
// ============================================================================

/// Cached query result
struct CacheEntry {
    /// Serialized result data
    data: Vec<u8>,
    /// Estimated size in bytes
    size: usize,
    /// When this entry was created
    created_at: Instant,
    /// Last access time
    last_accessed: Instant,
    /// Access count
    access_count: AtomicU64,
    /// Cache policy
    policy: CachePolicy,
}

impl CacheEntry {
    fn new(data: Vec<u8>, policy: CachePolicy) -> Self {
        let size = data.len();
        let now = Instant::now();
        Self {
            data,
            size,
            created_at: now,
            last_accessed: now,
            access_count: AtomicU64::new(0),
            policy,
        }
    }

    fn is_expired(&self) -> bool {
        let elapsed = if self.policy.sliding {
            self.last_accessed.elapsed()
        } else {
            self.created_at.elapsed()
        };
        elapsed > self.policy.ttl
    }

    fn touch(&mut self) {
        self.access_count.fetch_add(1, Ordering::Relaxed);
        self.last_accessed = Instant::now();
    }

    /// Calculate eviction score (lower = more likely to evict)
    fn eviction_score(&self) -> u64 {
        let frequency = self.access_count.load(Ordering::Relaxed);
        let recency = self.last_accessed.elapsed().as_secs();
        let priority = self.policy.priority as u64;

        // Score: frequency * priority / recency
        // Higher = keep longer
        frequency
            .saturating_mul(priority)
            .checked_div(recency)
            .unwrap_or_else(|| frequency.saturating_mul(priority).saturating_mul(1000))
    }
}

// ============================================================================
// Cache Statistics
// ============================================================================

/// Cache statistics
#[derive(Debug, Clone, Default)]
pub struct ResultCacheStats {
    /// Cache hits
    pub hits: u64,
    /// Cache misses
    pub misses: u64,
    /// Entries evicted
    pub evictions: u64,
    /// Current entry count
    pub entry_count: usize,
    /// Current memory usage in bytes
    pub memory_bytes: usize,
    /// Maximum memory limit
    pub max_memory_bytes: usize,
    /// Entries expired
    pub expirations: u64,
    /// Invalidations by dependency
    pub invalidations: u64,
}

impl ResultCacheStats {
    /// Calculate hit rate
    pub fn hit_rate(&self) -> f64 {
        let total = self.hits + self.misses;
        if total == 0 {
            0.0
        } else {
            self.hits as f64 / total as f64
        }
    }

    /// Calculate memory utilization
    pub fn memory_utilization(&self) -> f64 {
        if self.max_memory_bytes == 0 {
            0.0
        } else {
            self.memory_bytes as f64 / self.max_memory_bytes as f64
        }
    }
}

// ============================================================================
// Result Cache
// ============================================================================

/// LRU cache for query results with memory management
pub struct ResultCache {
    /// Cached entries
    entries: HashMap<CacheKey, CacheEntry>,
    /// Dependency index: table -> keys depending on it
    dependency_index: HashMap<String, HashSet<CacheKey>>,
    /// Maximum memory in bytes
    max_memory: usize,
    /// Current memory usage
    current_memory: usize,
    /// Statistics
    stats: ResultCacheStats,
}

impl ResultCache {
    /// Create a new result cache with max memory limit
    pub fn new(max_memory_bytes: usize) -> Self {
        Self {
            entries: HashMap::new(),
            dependency_index: HashMap::new(),
            max_memory: max_memory_bytes,
            current_memory: 0,
            stats: ResultCacheStats {
                max_memory_bytes,
                ..Default::default()
            },
        }
    }

    /// Get a cached result
    pub fn get(&mut self, key: &CacheKey) -> Option<Vec<u8>> {
        // Remove expired entry if present
        if let Some(entry) = self.entries.get(key) {
            if entry.is_expired() {
                self.remove(key);
                self.stats.expirations += 1;
                self.stats.misses += 1;
                return None;
            }
        }

        if let Some(entry) = self.entries.get_mut(key) {
            entry.touch();
            self.stats.hits += 1;
            Some(entry.data.clone())
        } else {
            self.stats.misses += 1;
            None
        }
    }

    /// Check if a key exists (without touching)
    pub fn contains(&self, key: &CacheKey) -> bool {
        if let Some(entry) = self.entries.get(key) {
            !entry.is_expired()
        } else {
            false
        }
    }

    /// Insert a result into the cache
    pub fn insert(&mut self, key: CacheKey, data: Vec<u8>, policy: CachePolicy) {
        let entry_size = data.len() + std::mem::size_of::<CacheEntry>();

        // Remove existing entry if present
        if self.entries.contains_key(&key) {
            self.remove(&key);
        }

        // Evict until we have space
        while self.current_memory + entry_size > self.max_memory && !self.entries.is_empty() {
            self.evict_one();
        }

        // Index dependencies
        for dep in &policy.dependencies {
            self.dependency_index
                .entry(dep.clone())
                .or_default()
                .insert(key.clone());
        }

        let entry = CacheEntry::new(data, policy);
        self.current_memory += entry.size;
        self.entries.insert(key, entry);
        self.stats.entry_count = self.entries.len();
        self.stats.memory_bytes = self.current_memory;
    }

    /// Remove an entry
    pub fn remove(&mut self, key: &CacheKey) -> Option<Vec<u8>> {
        if let Some(entry) = self.entries.remove(key) {
            self.current_memory = self.current_memory.saturating_sub(entry.size);

            // Remove from dependency index
            for dep in &entry.policy.dependencies {
                if let Some(keys) = self.dependency_index.get_mut(dep) {
                    keys.remove(key);
                }
            }

            self.stats.entry_count = self.entries.len();
            self.stats.memory_bytes = self.current_memory;
            Some(entry.data)
        } else {
            None
        }
    }

    /// Invalidate all entries depending on a table/collection
    pub fn invalidate_by_dependency(&mut self, dependency: &str) {
        if let Some(keys) = self.dependency_index.remove(dependency) {
            for key in keys {
                if self.entries.remove(&key).is_some() {
                    self.stats.invalidations += 1;
                }
            }
            self.stats.entry_count = self.entries.len();
            // Recalculate memory
            self.current_memory = self.entries.values().map(|e| e.size).sum();
            self.stats.memory_bytes = self.current_memory;
        }
    }

    /// Invalidate entries matching a predicate
    pub fn invalidate_where<F>(&mut self, predicate: F)
    where
        F: Fn(&CacheKey) -> bool,
    {
        let keys_to_remove: Vec<CacheKey> = self
            .entries
            .keys()
            .filter(|k| predicate(k))
            .cloned()
            .collect();

        for key in keys_to_remove {
            self.remove(&key);
            self.stats.invalidations += 1;
        }
    }

    /// Prune all expired entries
    pub fn prune_expired(&mut self) {
        let expired: Vec<CacheKey> = self
            .entries
            .iter()
            .filter(|(_, v)| v.is_expired())
            .map(|(k, _)| k.clone())
            .collect();

        for key in expired {
            self.remove(&key);
            self.stats.expirations += 1;
        }
    }

    /// Clear all entries
    pub fn clear(&mut self) {
        self.entries.clear();
        self.dependency_index.clear();
        self.current_memory = 0;
        self.stats.entry_count = 0;
        self.stats.memory_bytes = 0;
    }

    /// Get cache statistics
    pub fn stats(&self) -> &ResultCacheStats {
        &self.stats
    }

    /// Evict one entry (lowest eviction score)
    fn evict_one(&mut self) {
        let victim = self
            .entries
            .iter()
            .min_by_key(|(_, v)| v.eviction_score())
            .map(|(k, _)| k.clone());

        if let Some(key) = victim {
            self.remove(&key);
            self.stats.evictions += 1;
        }
    }
}

// ============================================================================
// Materialized View Cache
// ============================================================================

/// Definition of a materialized view
#[derive(Debug, Clone)]
pub struct MaterializedViewDef {
    /// View name
    pub name: String,
    /// Query that populates the view
    pub query: String,
    /// Tables this view depends on
    pub dependencies: Vec<String>,
    /// Refresh policy
    pub refresh: RefreshPolicy,
}

/// How to refresh a materialized view
#[derive(Debug, Clone)]
pub enum RefreshPolicy {
    /// Refresh on demand only
    Manual,
    /// Refresh when dependencies change
    OnChange,
    /// Refresh periodically
    Periodic(Duration),
    /// Refresh after N invalidations
    AfterWrites(usize),
}

/// Materialized view cache entry
struct MaterializedView {
    /// The cached result
    data: Vec<u8>,
    /// View definition
    def: MaterializedViewDef,
    /// When last refreshed
    last_refresh: Instant,
    /// Write count since last refresh
    writes_since_refresh: usize,
    /// Whether the view is stale
    stale: bool,
}

/// Cache for materialized views
pub struct MaterializedViewCache {
    /// Views by name
    views: HashMap<String, MaterializedView>,
    /// Dependency index: table -> view names
    dependency_index: HashMap<String, HashSet<String>>,
}

impl MaterializedViewCache {
    /// Create a new materialized view cache
    pub fn new() -> Self {
        Self {
            views: HashMap::new(),
            dependency_index: HashMap::new(),
        }
    }

    /// Register a view definition
    pub fn register(&mut self, def: MaterializedViewDef) {
        // Index dependencies
        for dep in &def.dependencies {
            self.dependency_index
                .entry(dep.clone())
                .or_default()
                .insert(def.name.clone());
        }

        let view = MaterializedView {
            data: Vec::new(),
            def,
            last_refresh: Instant::now(),
            writes_since_refresh: 0,
            stale: true,
        };

        self.views.insert(view.def.name.clone(), view);
    }

    /// Get view data (if not stale)
    pub fn get(&self, name: &str) -> Option<&[u8]> {
        self.views
            .get(name)
            .filter(|v| !v.stale && !v.data.is_empty())
            .map(|v| v.data.as_slice())
    }

    /// Check if view needs refresh
    pub fn needs_refresh(&self, name: &str) -> bool {
        self.views.get(name).map(|v| v.stale).unwrap_or(false)
    }

    /// Refresh a view with new data
    pub fn refresh(&mut self, name: &str, data: Vec<u8>) {
        if let Some(view) = self.views.get_mut(name) {
            view.data = data;
            view.last_refresh = Instant::now();
            view.writes_since_refresh = 0;
            view.stale = false;
        }
    }

    /// Mark views depending on a table as stale
    pub fn mark_stale(&mut self, table: &str) {
        if let Some(view_names) = self.dependency_index.get(table) {
            for name in view_names.clone() {
                if let Some(view) = self.views.get_mut(&name) {
                    view.writes_since_refresh += 1;

                    match &view.def.refresh {
                        RefreshPolicy::OnChange => {
                            view.stale = true;
                        }
                        RefreshPolicy::AfterWrites(threshold)
                            if view.writes_since_refresh >= *threshold =>
                        {
                            view.stale = true;
                        }
                        _ => {}
                    }
                }
            }
        }
    }

    /// Get views needing periodic refresh
    pub fn due_for_refresh(&self) -> Vec<String> {
        self.views
            .values()
            .filter(|v| {
                if let RefreshPolicy::Periodic(interval) = &v.def.refresh {
                    v.last_refresh.elapsed() >= *interval
                } else {
                    false
                }
            })
            .map(|v| v.def.name.clone())
            .collect()
    }

    /// Remove a view
    pub fn remove(&mut self, name: &str) {
        if let Some(view) = self.views.remove(name) {
            for dep in &view.def.dependencies {
                if let Some(names) = self.dependency_index.get_mut(dep) {
                    names.remove(name);
                }
            }
        }
    }

    /// List all view names
    pub fn list(&self) -> Vec<&str> {
        self.views.keys().map(|s| s.as_str()).collect()
    }
}

impl Default for MaterializedViewCache {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_cache_key_hashing() {
        let key1 = CacheKey::new("attack_paths")
            .param("from", "host1")
            .param("to", "host2");

        let key2 = CacheKey::new("attack_paths")
            .param("to", "host2")
            .param("from", "host1"); // Different order

        assert_eq!(key1, key2);
        assert_eq!(key1.hash, key2.hash);
    }

    #[test]
    fn test_result_cache_basic() {
        let mut cache = ResultCache::new(1024 * 1024); // 1MB

        let key = CacheKey::new("test_query").param("id", "123");
        let data = vec![1, 2, 3, 4, 5];

        cache.insert(key.clone(), data.clone(), CachePolicy::default());

        let result = cache.get(&key);
        assert_eq!(result, Some(data));
        assert_eq!(cache.stats().hits, 1);
    }

    #[test]
    fn test_cache_expiration() {
        let mut cache = ResultCache::new(1024 * 1024);

        let key = CacheKey::new("test");
        let data = vec![1, 2, 3];

        // Very short TTL
        cache.insert(
            key.clone(),
            data,
            CachePolicy::default().ttl(Duration::from_millis(1)),
        );

        // Wait for expiration
        std::thread::sleep(Duration::from_millis(10));

        assert!(cache.get(&key).is_none());
        assert_eq!(cache.stats().expirations, 1);
    }

    #[test]
    fn test_dependency_invalidation() {
        let mut cache = ResultCache::new(1024 * 1024);

        let key = CacheKey::new("host_query");
        cache.insert(
            key.clone(),
            vec![1, 2, 3],
            CachePolicy::default().depends_on(&["hosts"]),
        );

        assert!(cache.contains(&key));

        // Invalidate hosts table
        cache.invalidate_by_dependency("hosts");

        assert!(!cache.contains(&key));
        assert_eq!(cache.stats().invalidations, 1);
    }

    #[test]
    fn test_memory_eviction() {
        let mut cache = ResultCache::new(100); // Very small

        // Insert enough to trigger eviction
        for i in 0..10 {
            let key = CacheKey::new("query").param("i", i.to_string());
            cache.insert(key, vec![0u8; 20], CachePolicy::default());
        }

        // Should have evicted some entries
        assert!(cache.stats().evictions > 0);
        assert!(cache.stats().memory_bytes <= 100);
    }

    #[test]
    fn test_materialized_view() {
        let mut cache = MaterializedViewCache::new();

        cache.register(MaterializedViewDef {
            name: "active_hosts".to_string(),
            query: "SELECT * FROM hosts WHERE status = 'active'".to_string(),
            dependencies: vec!["hosts".to_string()],
            refresh: RefreshPolicy::OnChange,
        });

        // Initially stale
        assert!(cache.needs_refresh("active_hosts"));

        // Refresh it
        cache.refresh("active_hosts", vec![1, 2, 3]);
        assert!(!cache.needs_refresh("active_hosts"));
        assert_eq!(cache.get("active_hosts"), Some(&[1, 2, 3][..]));

        // Mark stale due to dependency
        cache.mark_stale("hosts");
        assert!(cache.needs_refresh("active_hosts"));
    }
}