kitedb 0.2.2

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
607
608
609
610
611
612
613
//! LRU (Least Recently Used) Cache
//!
//! Generic LRU cache implementation with O(1) get/set/delete operations.
//! Uses a HashMap for O(1) lookups and a doubly-linked list for O(1) eviction.
//!
//! Ported from src/util/lru.ts

use std::borrow::Borrow;
use std::collections::HashMap;
use std::hash::Hash;
use std::ptr::NonNull;

// ============================================================================
// Node structure for doubly-linked list
// ============================================================================

struct LruNode<K, V> {
  key: K,
  value: V,
  prev: Option<NonNull<LruNode<K, V>>>,
  next: Option<NonNull<LruNode<K, V>>>,
}

impl<K, V> LruNode<K, V> {
  fn new(key: K, value: V) -> Self {
    Self {
      key,
      value,
      prev: None,
      next: None,
    }
  }
}

// ============================================================================
// LRU Cache
// ============================================================================

/// LRU Cache implementation
///
/// Maintains items in order of access, automatically evicting the least
/// recently used item when capacity is exceeded.
///
/// # Type Parameters
/// - `K`: Key type (must be Hash + Eq + Clone)
/// - `V`: Value type
///
/// # Example
/// ```
/// use kitedb::cache::lru::LruCache;
///
/// let mut cache = LruCache::new(3);
/// cache.set("a", 1);
/// cache.set("b", 2);
/// cache.set("c", 3);
///
/// assert_eq!(cache.get(&"a"), Some(&1));
///
/// // Adding a 4th item evicts "b" (least recently used)
/// cache.set("d", 4);
/// assert_eq!(cache.get(&"b"), None);
/// ```
pub struct LruCache<K: Hash + Eq + Clone, V> {
  max_size: usize,
  map: HashMap<K, NonNull<LruNode<K, V>>>,
  head: Option<NonNull<LruNode<K, V>>>,
  tail: Option<NonNull<LruNode<K, V>>>,
}

impl<K: Hash + Eq + Clone, V> LruCache<K, V> {
  /// Create a new LRU cache with specified maximum capacity
  ///
  /// # Panics
  /// Panics if max_size is 0
  pub fn new(max_size: usize) -> Self {
    assert!(max_size > 0, "LRU cache max_size must be greater than 0");
    Self {
      max_size,
      map: HashMap::with_capacity(max_size),
      head: None,
      tail: None,
    }
  }

  /// Create a new LRU cache with specified capacity hint
  pub fn with_capacity(max_size: usize, initial_capacity: usize) -> Self {
    assert!(max_size > 0, "LRU cache max_size must be greater than 0");
    Self {
      max_size,
      map: HashMap::with_capacity(initial_capacity.min(max_size)),
      head: None,
      tail: None,
    }
  }

  /// Get a reference to a value in the cache
  /// O(1) time complexity
  ///
  /// This marks the item as recently used.
  pub fn get<Q>(&mut self, key: &Q) -> Option<&V>
  where
    K: Borrow<Q>,
    Q: Hash + Eq + ?Sized,
  {
    let node_ptr = self.map.get(key).copied()?;

    // Move to front (most recently used)
    self.move_to_front(node_ptr);

    // SAFETY: node_ptr is valid because it's in our map
    unsafe { Some(&(*node_ptr.as_ptr()).value) }
  }

  /// Get a mutable reference to a value in the cache
  /// O(1) time complexity
  ///
  /// This marks the item as recently used.
  pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
  where
    K: Borrow<Q>,
    Q: Hash + Eq + ?Sized,
  {
    let node_ptr = self.map.get(key).copied()?;

    // Move to front (most recently used)
    self.move_to_front(node_ptr);

    // SAFETY: node_ptr is valid because it's in our map
    unsafe { Some(&mut (*node_ptr.as_ptr()).value) }
  }

  /// Peek at a value without marking it as recently used
  /// O(1) time complexity
  pub fn peek<Q>(&self, key: &Q) -> Option<&V>
  where
    K: Borrow<Q>,
    Q: Hash + Eq + ?Sized,
  {
    let node_ptr = self.map.get(key)?;
    // SAFETY: node_ptr is valid because it's in our map
    unsafe { Some(&(*node_ptr.as_ptr()).value) }
  }

  /// Set a value in the cache
  /// O(1) time complexity
  ///
  /// If the key already exists, updates the value and marks as recently used.
  /// If at capacity, evicts the least recently used item.
  pub fn set(&mut self, key: K, value: V) {
    if let Some(&node_ptr) = self.map.get(&key) {
      // Update existing value and move to front
      unsafe {
        (*node_ptr.as_ptr()).value = value;
      }
      self.move_to_front(node_ptr);
      return;
    }

    // Create new node
    let node = Box::new(LruNode::new(key.clone(), value));
    let node_ptr = NonNull::new(Box::into_raw(node)).unwrap();

    // Add to front
    self.push_front(node_ptr);

    // Add to map
    self.map.insert(key, node_ptr);

    // Evict if over capacity
    if self.map.len() > self.max_size {
      self.evict();
    }
  }

  /// Insert a value, returning the old value if present
  /// O(1) time complexity
  pub fn insert(&mut self, key: K, value: V) -> Option<V> {
    if let Some(&node_ptr) = self.map.get(&key) {
      // Update existing value and move to front
      let old_value = unsafe {
        let node = &mut *node_ptr.as_ptr();
        std::mem::replace(&mut node.value, value)
      };
      self.move_to_front(node_ptr);
      return Some(old_value);
    }

    // Create new node
    let node = Box::new(LruNode::new(key.clone(), value));
    let node_ptr = NonNull::new(Box::into_raw(node)).unwrap();

    // Add to front
    self.push_front(node_ptr);

    // Add to map
    self.map.insert(key, node_ptr);

    // Evict if over capacity
    if self.map.len() > self.max_size {
      self.evict();
    }

    None
  }

  /// Delete a value from the cache
  /// O(1) time complexity
  ///
  /// Returns true if the key existed and was deleted
  pub fn delete<Q>(&mut self, key: &Q) -> bool
  where
    K: Borrow<Q>,
    Q: Hash + Eq + ?Sized,
  {
    if let Some(node_ptr) = self.map.remove(key) {
      self.remove_node(node_ptr);
      // SAFETY: We just removed this from the map, so we own it now
      unsafe {
        let _ = Box::from_raw(node_ptr.as_ptr());
      }
      true
    } else {
      false
    }
  }

  /// Remove a value from the cache, returning it if present
  /// O(1) time complexity
  pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
  where
    K: Borrow<Q>,
    Q: Hash + Eq + ?Sized,
  {
    if let Some(node_ptr) = self.map.remove(key) {
      self.remove_node(node_ptr);
      // SAFETY: We just removed this from the map, so we own it now
      unsafe {
        let node = Box::from_raw(node_ptr.as_ptr());
        Some(node.value)
      }
    } else {
      None
    }
  }

  /// Check if a key exists in the cache
  /// O(1) time complexity
  ///
  /// This does NOT mark the item as recently used.
  pub fn contains_key<Q>(&self, key: &Q) -> bool
  where
    K: Borrow<Q>,
    Q: Hash + Eq + ?Sized,
  {
    self.map.contains_key(key)
  }

  /// Clear all entries from the cache
  pub fn clear(&mut self) {
    // Free all nodes
    let mut current = self.head;
    while let Some(node_ptr) = current {
      unsafe {
        current = (*node_ptr.as_ptr()).next;
        let _ = Box::from_raw(node_ptr.as_ptr());
      }
    }

    self.map.clear();
    self.head = None;
    self.tail = None;
  }

  /// Get the current number of cached items
  #[inline]
  pub fn len(&self) -> usize {
    self.map.len()
  }

  /// Check if the cache is empty
  #[inline]
  pub fn is_empty(&self) -> bool {
    self.map.is_empty()
  }

  /// Get the maximum cache size
  #[inline]
  pub fn max_size(&self) -> usize {
    self.max_size
  }

  /// Iterate over entries in order from most to least recently used
  pub fn iter(&self) -> LruIter<'_, K, V> {
    LruIter {
      current: self.head,
      _marker: std::marker::PhantomData,
    }
  }

  // ========================================================================
  // Internal linked list operations
  // ========================================================================

  /// Push a node to the front of the list
  fn push_front(&mut self, node_ptr: NonNull<LruNode<K, V>>) {
    unsafe {
      let node = node_ptr.as_ptr();
      (*node).prev = None;
      (*node).next = self.head;

      if let Some(head) = self.head {
        (*head.as_ptr()).prev = Some(node_ptr);
      }

      self.head = Some(node_ptr);

      if self.tail.is_none() {
        self.tail = Some(node_ptr);
      }
    }
  }

  /// Move a node to the front of the list
  fn move_to_front(&mut self, node_ptr: NonNull<LruNode<K, V>>) {
    if self.head == Some(node_ptr) {
      return; // Already at front
    }

    // Remove from current position
    self.remove_node(node_ptr);

    // Add to front
    self.push_front(node_ptr);
  }

  /// Remove a node from the linked list (but don't free it)
  fn remove_node(&mut self, node_ptr: NonNull<LruNode<K, V>>) {
    unsafe {
      let node = node_ptr.as_ptr();
      let prev = (*node).prev;
      let next = (*node).next;

      if let Some(prev_ptr) = prev {
        (*prev_ptr.as_ptr()).next = next;
      } else {
        // Node is head
        self.head = next;
      }

      if let Some(next_ptr) = next {
        (*next_ptr.as_ptr()).prev = prev;
      } else {
        // Node is tail
        self.tail = prev;
      }

      (*node).prev = None;
      (*node).next = None;
    }
  }

  /// Evict the least recently used item (tail of the list)
  fn evict(&mut self) {
    if let Some(tail_ptr) = self.tail {
      unsafe {
        let key = (*tail_ptr.as_ptr()).key.clone();
        self.remove_node(tail_ptr);
        self.map.remove(&key);
        let _ = Box::from_raw(tail_ptr.as_ptr());
      }
    }
  }
}

impl<K: Hash + Eq + Clone, V> Drop for LruCache<K, V> {
  fn drop(&mut self) {
    self.clear();
  }
}

// SAFETY: LruCache can be sent between threads if K and V are Send
unsafe impl<K: Hash + Eq + Clone + Send, V: Send> Send for LruCache<K, V> {}

// ============================================================================
// Iterator
// ============================================================================

/// Iterator over LRU cache entries (most to least recently used)
pub struct LruIter<'a, K: Hash + Eq + Clone, V> {
  current: Option<NonNull<LruNode<K, V>>>,
  _marker: std::marker::PhantomData<&'a LruCache<K, V>>,
}

impl<'a, K: Hash + Eq + Clone, V> Iterator for LruIter<'a, K, V> {
  type Item = (&'a K, &'a V);

  fn next(&mut self) -> Option<Self::Item> {
    self.current.map(|node_ptr| unsafe {
      let node = node_ptr.as_ptr();
      self.current = (*node).next;
      (&(*node).key, &(*node).value)
    })
  }
}

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

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

  #[test]
  fn test_new_cache() {
    let cache: LruCache<String, i32> = LruCache::new(10);
    assert_eq!(cache.len(), 0);
    assert!(cache.is_empty());
    assert_eq!(cache.max_size(), 10);
  }

  #[test]
  #[should_panic(expected = "max_size must be greater than 0")]
  fn test_zero_capacity_panics() {
    let _cache: LruCache<String, i32> = LruCache::new(0);
  }

  #[test]
  fn test_set_and_get() {
    let mut cache = LruCache::new(10);
    cache.set("a", 1);
    cache.set("b", 2);
    cache.set("c", 3);

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

  #[test]
  fn test_update_value() {
    let mut cache = LruCache::new(10);
    cache.set("a", 1);
    assert_eq!(cache.get(&"a"), Some(&1));

    cache.set("a", 100);
    assert_eq!(cache.get(&"a"), Some(&100));
    assert_eq!(cache.len(), 1);
  }

  #[test]
  fn test_eviction() {
    let mut cache = LruCache::new(3);
    cache.set("a", 1);
    cache.set("b", 2);
    cache.set("c", 3);

    // All should be present
    assert_eq!(cache.len(), 3);
    assert!(cache.contains_key(&"a"));
    assert!(cache.contains_key(&"b"));
    assert!(cache.contains_key(&"c"));

    // Adding a 4th item should evict "a" (oldest)
    cache.set("d", 4);
    assert_eq!(cache.len(), 3);
    assert!(!cache.contains_key(&"a"));
    assert!(cache.contains_key(&"b"));
    assert!(cache.contains_key(&"c"));
    assert!(cache.contains_key(&"d"));
  }

  #[test]
  fn test_lru_order() {
    let mut cache = LruCache::new(3);
    cache.set("a", 1);
    cache.set("b", 2);
    cache.set("c", 3);

    // Access "a" to make it most recently used
    cache.get(&"a");

    // "b" is now least recently used
    cache.set("d", 4);
    assert!(!cache.contains_key(&"b"));
    assert!(cache.contains_key(&"a"));
    assert!(cache.contains_key(&"c"));
    assert!(cache.contains_key(&"d"));
  }

  #[test]
  fn test_delete() {
    let mut cache = LruCache::new(10);
    cache.set("a", 1);
    cache.set("b", 2);

    assert!(cache.delete(&"a"));
    assert!(!cache.delete(&"a")); // Already deleted
    assert_eq!(cache.len(), 1);
    assert_eq!(cache.get(&"a"), None);
    assert_eq!(cache.get(&"b"), Some(&2));
  }

  #[test]
  fn test_remove() {
    let mut cache = LruCache::new(10);
    cache.set("a", 1);
    cache.set("b", 2);

    assert_eq!(cache.remove(&"a"), Some(1));
    assert_eq!(cache.remove(&"a"), None);
    assert_eq!(cache.len(), 1);
  }

  #[test]
  fn test_clear() {
    let mut cache = LruCache::new(10);
    cache.set("a", 1);
    cache.set("b", 2);
    cache.set("c", 3);

    cache.clear();
    assert!(cache.is_empty());
    assert_eq!(cache.len(), 0);
    assert_eq!(cache.get(&"a"), None);
  }

  #[test]
  fn test_peek() {
    let mut cache = LruCache::new(3);
    cache.set("a", 1);
    cache.set("b", 2);
    cache.set("c", 3);

    // Peek at "a" - should not affect LRU order
    assert_eq!(cache.peek(&"a"), Some(&1));

    // "a" should still be least recently used (not "b")
    cache.set("d", 4);
    assert!(!cache.contains_key(&"a"));
    assert!(cache.contains_key(&"b"));
  }

  #[test]
  fn test_insert_returns_old_value() {
    let mut cache = LruCache::new(10);
    assert_eq!(cache.insert("a", 1), None);
    assert_eq!(cache.insert("a", 2), Some(1));
    assert_eq!(cache.get(&"a"), Some(&2));
  }

  #[test]
  fn test_get_mut() {
    let mut cache = LruCache::new(10);
    cache.set("a", 1);

    if let Some(val) = cache.get_mut(&"a") {
      *val = 100;
    }

    assert_eq!(cache.get(&"a"), Some(&100));
  }

  #[test]
  fn test_iter() {
    let mut cache = LruCache::new(10);
    cache.set("a", 1);
    cache.set("b", 2);
    cache.set("c", 3);

    // Access "a" to make it most recently used
    cache.get(&"a");

    let items: Vec<_> = cache.iter().collect();
    // Most recently used first
    assert_eq!(items.len(), 3);
    assert_eq!(*items[0].0, "a"); // Most recently accessed
    assert_eq!(*items[1].0, "c"); // Second most recent (last added before access)
    assert_eq!(*items[2].0, "b"); // Least recently used
  }

  #[test]
  fn test_single_item() {
    let mut cache = LruCache::new(1);
    cache.set("a", 1);
    assert_eq!(cache.get(&"a"), Some(&1));

    cache.set("b", 2);
    assert_eq!(cache.get(&"a"), None);
    assert_eq!(cache.get(&"b"), Some(&2));
    assert_eq!(cache.len(), 1);
  }

  #[test]
  fn test_complex_keys() {
    let mut cache: LruCache<(u32, u32), String> = LruCache::new(10);
    cache.set((1, 2), "one-two".to_string());
    cache.set((3, 4), "three-four".to_string());

    assert_eq!(cache.get(&(1, 2)), Some(&"one-two".to_string()));
    assert_eq!(cache.get(&(3, 4)), Some(&"three-four".to_string()));
    assert_eq!(cache.get(&(5, 6)), None);
  }

  #[test]
  fn test_with_capacity() {
    let cache: LruCache<String, i32> = LruCache::with_capacity(100, 50);
    assert_eq!(cache.max_size(), 100);
    assert_eq!(cache.len(), 0);
  }
}