kitedb 0.2.15

High-performance embedded graph database
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
//! Query Cache
//!
//! Caches complex query results with optional TTL (time-to-live).
//! Uses content-addressed keys based on query parameters.
//!
//! Ported from src/cache/query-cache.ts

use super::lru::LruCache;
use crate::types::QueryCacheConfig;
use std::any::Any;
use std::time::{Duration, Instant};

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

/// Cached query result with timestamp for TTL support
struct CachedQueryResult {
  /// The cached value (type-erased)
  value: Box<dyn Any + Send>,
  /// When the entry was cached (for TTL)
  timestamp: Instant,
}

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

/// Query cache statistics
#[derive(Debug, Clone, Default)]
pub struct QueryCacheStats {
  pub hits: u64,
  pub misses: u64,
  pub expirations: u64,
  pub cache_size: usize,
  pub max_cache_size: usize,
}

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

// ============================================================================
// Query Cache
// ============================================================================

/// Query cache for complex query results
///
/// Supports optional TTL (time-to-live) for automatic expiration.
/// Uses type-erased storage to support arbitrary result types.
///
/// # Example
/// ```
/// use kitedb::cache::query::QueryCache;
/// use kitedb::types::QueryCacheConfig;
///
/// let config = QueryCacheConfig {
///     max_entries: 100,
///     ttl_ms: Some(60000), // 1 minute TTL
/// };
/// let mut cache = QueryCache::new(config);
///
/// // Cache a query result
/// cache.set("query1".to_string(), vec![1u64, 2, 3]);
///
/// // Retrieve the result
/// let result: Option<&Vec<u64>> = cache.get("query1");
/// assert!(matches!(result, Some(values) if values.as_slice() == [1u64, 2, 3]));
/// ```
pub struct QueryCache {
  /// The LRU cache for query results
  cache: LruCache<String, CachedQueryResult>,

  /// Optional TTL in duration
  ttl: Option<Duration>,

  /// Cache statistics
  hits: u64,
  misses: u64,
  expirations: u64,
}

impl QueryCache {
  /// Create a new query cache with the given configuration
  pub fn new(config: QueryCacheConfig) -> Self {
    Self {
      cache: LruCache::new(config.max_entries),
      ttl: config.ttl_ms.map(Duration::from_millis),
      hits: 0,
      misses: 0,
      expirations: 0,
    }
  }

  /// Create a new query cache with no TTL
  pub fn new_without_ttl(max_entries: usize) -> Self {
    Self {
      cache: LruCache::new(max_entries),
      ttl: None,
      hits: 0,
      misses: 0,
      expirations: 0,
    }
  }

  /// Create a new query cache with TTL
  pub fn new_with_ttl(max_entries: usize, ttl: Duration) -> Self {
    Self {
      cache: LruCache::new(max_entries),
      ttl: Some(ttl),
      hits: 0,
      misses: 0,
      expirations: 0,
    }
  }

  /// Get a cached query result
  ///
  /// Returns None if:
  /// - Key is not in cache
  /// - Entry has expired (if TTL is configured)
  /// - Type mismatch
  pub fn get<T: 'static>(&mut self, key: &str) -> Option<&T> {
    // Use peek first to check if entry exists without affecting LRU
    let has_entry = self.cache.peek(key).is_some();

    if !has_entry {
      self.misses += 1;
      return None;
    }

    // Check TTL if configured
    if let Some(ttl) = self.ttl {
      let entry = self.cache.peek(key)?;
      let age = entry.timestamp.elapsed();

      if age > ttl {
        // Expired, remove from cache
        self.cache.delete(key);
        self.misses += 1;
        self.expirations += 1;
        return None;
      }
    }

    // Get the entry (this updates LRU)
    let entry = self.cache.get(key)?;

    // Try to downcast to expected type
    match entry.value.downcast_ref::<T>() {
      Some(value) => {
        self.hits += 1;
        Some(value)
      }
      None => {
        // Type mismatch - treat as miss
        self.misses += 1;
        None
      }
    }
  }

  /// Peek at a cached query result without affecting LRU or TTL
  pub fn peek<T: 'static>(&self, key: &str) -> Option<&T> {
    let entry = self.cache.peek(key)?;

    // Check TTL if configured (but don't remove)
    if let Some(ttl) = self.ttl {
      if entry.timestamp.elapsed() > ttl {
        return None;
      }
    }

    entry.value.downcast_ref::<T>()
  }

  /// Set a query result in cache
  pub fn set<T: 'static + Send>(&mut self, key: String, value: T) {
    let entry = CachedQueryResult {
      value: Box::new(value),
      timestamp: Instant::now(),
    };
    self.cache.set(key, entry);
  }

  /// Delete a cached query result
  pub fn delete(&mut self, key: &str) -> bool {
    self.cache.delete(key)
  }

  /// Check if a key exists in cache (without checking TTL)
  pub fn contains_key(&self, key: &str) -> bool {
    self.cache.contains_key(key)
  }

  /// Check if a key exists and is not expired
  pub fn contains_key_valid(&self, key: &str) -> bool {
    let Some(entry) = self.cache.peek(key) else {
      return false;
    };

    if let Some(ttl) = self.ttl {
      if entry.timestamp.elapsed() > ttl {
        return false;
      }
    }

    true
  }

  /// Clear all cached queries
  pub fn clear(&mut self) {
    self.cache.clear();
    self.hits = 0;
    self.misses = 0;
    self.expirations = 0;
  }

  /// Get cache statistics
  pub fn stats(&self) -> QueryCacheStats {
    QueryCacheStats {
      hits: self.hits,
      misses: self.misses,
      expirations: self.expirations,
      cache_size: self.cache.len(),
      max_cache_size: self.cache.max_size(),
    }
  }

  /// Get current cache size
  pub fn len(&self) -> usize {
    self.cache.len()
  }

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

  /// Get the configured TTL
  pub fn ttl(&self) -> Option<Duration> {
    self.ttl
  }

  /// Set a new TTL (affects new entries and future gets)
  pub fn set_ttl(&mut self, ttl: Option<Duration>) {
    self.ttl = ttl;
  }

  /// Reset statistics counters
  pub fn reset_stats(&mut self) {
    self.hits = 0;
    self.misses = 0;
    self.expirations = 0;
  }

  /// Prune expired entries (useful for periodic cleanup)
  pub fn prune_expired(&mut self) -> usize {
    let Some(ttl) = self.ttl else {
      return 0;
    };

    // Collect expired keys
    let expired_keys: Vec<String> = self
      .cache
      .iter()
      .filter(|(_, entry)| entry.timestamp.elapsed() > ttl)
      .map(|(k, _)| k.clone())
      .collect();

    let count = expired_keys.len();

    // Remove expired entries
    for key in expired_keys {
      self.cache.delete(&key);
      self.expirations += 1;
    }

    count
  }
}

// ============================================================================
// Key Generation Utilities
// ============================================================================

/// Generate a cache key from a string
pub fn generate_key_string(s: &str) -> String {
  s.to_string()
}

/// Generate a cache key from query parameters
///
/// Sorts keys for consistent hashing and serializes values.
pub fn generate_key_params<K, V>(params: impl IntoIterator<Item = (K, V)>) -> String
where
  K: AsRef<str> + Ord,
  V: std::fmt::Debug,
{
  let mut items: Vec<_> = params.into_iter().collect();
  items.sort_by(|a, b| a.0.as_ref().cmp(b.0.as_ref()));

  items
    .into_iter()
    .map(|(k, v)| format!("{}:{:?}", k.as_ref(), v))
    .collect::<Vec<_>>()
    .join("|")
}

/// Generate a cache key by hashing with xxHash64
pub fn generate_key_hash(data: &[u8]) -> String {
  use xxhash_rust::xxh64::xxh64;
  xxh64(data, 0).to_string()
}

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

#[cfg(test)]
mod tests {
  use super::*;
  use std::thread;
  use std::time::Duration;

  fn make_cache() -> QueryCache {
    QueryCache::new(QueryCacheConfig {
      max_entries: 100,
      ttl_ms: None,
    })
  }

  fn make_cache_with_ttl(ttl_ms: u64) -> QueryCache {
    QueryCache::new(QueryCacheConfig {
      max_entries: 100,
      ttl_ms: Some(ttl_ms),
    })
  }

  #[test]
  fn test_new_cache() {
    let cache = make_cache();
    assert!(cache.is_empty());
    assert_eq!(cache.stats().hits, 0);
    assert_eq!(cache.stats().misses, 0);
  }

  #[test]
  fn test_cache_miss() {
    let mut cache = make_cache();
    let result: Option<&i32> = cache.get("nonexistent");
    assert!(result.is_none());
    assert_eq!(cache.stats().misses, 1);
  }

  #[test]
  fn test_cache_hit() {
    let mut cache = make_cache();
    cache.set("query1".to_string(), vec![1i32, 2, 3]);

    let result: Option<&Vec<i32>> = cache.get("query1");
    assert!(result.is_some());
    assert_eq!(result.expect("expected value"), &vec![1, 2, 3]);
    assert_eq!(cache.stats().hits, 1);
  }

  #[test]
  fn test_type_mismatch() {
    let mut cache = make_cache();
    cache.set("query1".to_string(), 42i32);

    // Try to get as wrong type
    let result: Option<&String> = cache.get("query1");
    assert!(result.is_none());
    assert_eq!(cache.stats().misses, 1);

    // Get as correct type should work
    let result: Option<&i32> = cache.get("query1");
    assert_eq!(result, Some(&42));
    assert_eq!(cache.stats().hits, 1);
  }

  #[test]
  fn test_update_value() {
    let mut cache = make_cache();
    cache.set("query1".to_string(), 42i32);
    cache.set("query1".to_string(), 100i32);

    let result: Option<&i32> = cache.get("query1");
    assert_eq!(result, Some(&100));
    assert_eq!(cache.len(), 1);
  }

  #[test]
  fn test_delete() {
    let mut cache = make_cache();
    cache.set("query1".to_string(), 42i32);

    assert!(cache.delete("query1"));
    assert!(!cache.delete("query1")); // Already deleted

    let result: Option<&i32> = cache.get("query1");
    assert!(result.is_none());
  }

  #[test]
  fn test_contains_key() {
    let mut cache = make_cache();
    cache.set("query1".to_string(), 42i32);

    assert!(cache.contains_key("query1"));
    assert!(!cache.contains_key("query2"));
  }

  #[test]
  fn test_clear() {
    let mut cache = make_cache();
    cache.set("query1".to_string(), 42i32);
    cache.set("query2".to_string(), 100i32);

    // Generate stats
    let _: Option<&i32> = cache.get("query1");
    let _: Option<&i32> = cache.get("nonexistent");

    cache.clear();

    assert!(cache.is_empty());
    assert_eq!(cache.stats().hits, 0);
    assert_eq!(cache.stats().misses, 0);
  }

  #[test]
  fn test_ttl_expiration() {
    let mut cache = make_cache_with_ttl(50); // 50ms TTL

    cache.set("query1".to_string(), 42i32);

    // Should be accessible immediately
    let result: Option<&i32> = cache.get("query1");
    assert_eq!(result, Some(&42));
    assert_eq!(cache.stats().hits, 1);

    // Wait for TTL to expire
    thread::sleep(Duration::from_millis(60));

    // Should be expired now
    let result: Option<&i32> = cache.get("query1");
    assert!(result.is_none());
    assert_eq!(cache.stats().misses, 1);
    assert_eq!(cache.stats().expirations, 1);
  }

  #[test]
  fn test_contains_key_valid_with_ttl() {
    let mut cache = make_cache_with_ttl(50);

    cache.set("query1".to_string(), 42i32);

    assert!(cache.contains_key_valid("query1"));

    thread::sleep(Duration::from_millis(60));

    // Key still exists but is expired
    assert!(cache.contains_key("query1"));
    assert!(!cache.contains_key_valid("query1"));
  }

  #[test]
  fn test_peek_does_not_affect_stats() {
    let cache = make_cache();

    // Peek should not affect stats
    let result: Option<&i32> = cache.peek("nonexistent");
    assert!(result.is_none());
    assert_eq!(cache.stats().hits, 0);
    assert_eq!(cache.stats().misses, 0);
  }

  #[test]
  fn test_prune_expired() {
    let mut cache = make_cache_with_ttl(50);

    cache.set("query1".to_string(), 1i32);
    cache.set("query2".to_string(), 2i32);

    // Wait for TTL to expire
    thread::sleep(Duration::from_millis(60));

    // Add a fresh entry
    cache.set("query3".to_string(), 3i32);

    // Prune expired
    let pruned = cache.prune_expired();
    assert_eq!(pruned, 2);

    // Only query3 should remain
    assert_eq!(cache.len(), 1);
    let result: Option<&i32> = cache.get("query3");
    assert_eq!(result, Some(&3));
  }

  #[test]
  fn test_set_ttl() {
    let mut cache = make_cache();
    assert!(cache.ttl().is_none());

    cache.set_ttl(Some(Duration::from_millis(100)));
    assert_eq!(cache.ttl(), Some(Duration::from_millis(100)));

    cache.set_ttl(None);
    assert!(cache.ttl().is_none());
  }

  #[test]
  fn test_various_types() {
    let mut cache = make_cache();

    cache.set("int".to_string(), 42i64);
    cache.set("float".to_string(), std::f64::consts::PI);
    cache.set("string".to_string(), "hello".to_string());
    cache.set("vec".to_string(), vec![1u32, 2, 3]);
    cache.set("tuple".to_string(), (1i32, "a".to_string()));

    assert_eq!(cache.get::<i64>("int"), Some(&42));
    assert_eq!(cache.get::<f64>("float"), Some(&std::f64::consts::PI));
    assert_eq!(cache.get::<String>("string"), Some(&"hello".to_string()));
    assert_eq!(cache.get::<Vec<u32>>("vec"), Some(&vec![1, 2, 3]));
    assert_eq!(
      cache.get::<(i32, String)>("tuple"),
      Some(&(1, "a".to_string()))
    );
  }

  #[test]
  fn test_stats() {
    let mut cache = make_cache();

    cache.set("query1".to_string(), 42i32);

    // 2 hits
    let _: Option<&i32> = cache.get("query1");
    let _: Option<&i32> = cache.get("query1");

    // 2 misses
    let _: Option<&i32> = cache.get("missing");
    let _: Option<&String> = cache.get("query1"); // Type mismatch

    let stats = cache.stats();
    assert_eq!(stats.hits, 2);
    assert_eq!(stats.misses, 2);
    assert_eq!(stats.hit_rate(), 0.5);
  }

  #[test]
  fn test_reset_stats() {
    let mut cache = make_cache();

    cache.set("query1".to_string(), 42i32);
    let _: Option<&i32> = cache.get("query1");
    let _: Option<&i32> = cache.get("missing");

    cache.reset_stats();

    assert_eq!(cache.stats().hits, 0);
    assert_eq!(cache.stats().misses, 0);

    // Cache should still have data
    assert_eq!(cache.len(), 1);
  }

  #[test]
  fn test_generate_key_string() {
    assert_eq!(generate_key_string("hello"), "hello");
  }

  #[test]
  fn test_generate_key_params() {
    let params = vec![("b", 2), ("a", 1), ("c", 3)];

    // Should be sorted by key
    let key = generate_key_params(params);
    assert_eq!(key, "a:1|b:2|c:3");
  }

  #[test]
  fn test_new_without_ttl() {
    let cache = QueryCache::new_without_ttl(50);
    assert!(cache.ttl().is_none());
    assert_eq!(cache.stats().max_cache_size, 50);
  }

  #[test]
  fn test_new_with_ttl() {
    let cache = QueryCache::new_with_ttl(50, Duration::from_secs(60));
    assert_eq!(cache.ttl(), Some(Duration::from_secs(60)));
    assert_eq!(cache.stats().max_cache_size, 50);
  }
}