oximedia-dedup 0.1.0

Media deduplication and duplicate detection for OxiMedia
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
#![allow(dead_code)]

//! LRU cache for deduplication hash lookups.
//!
//! This module provides a fixed-capacity Least Recently Used (LRU) cache
//! that accelerates repeated hash lookups during deduplication scans.
//! When the cache is full, the least recently accessed entry is evicted.
//!
//! # Key Types
//!
//! - [`LruCache`] - Generic fixed-capacity LRU cache
//! - [`HashCache`] - Specialised cache mapping file paths to hash digests
//! - [`CacheStats`] - Hit/miss statistics for the cache

use std::collections::HashMap;
use std::fmt;
use std::hash::Hash;

/// Statistics for cache performance.
#[derive(Debug, Clone, Default)]
pub struct CacheStats {
    /// Total cache hits.
    pub hits: u64,
    /// Total cache misses.
    pub misses: u64,
    /// Total insertions.
    pub insertions: u64,
    /// Total evictions.
    pub evictions: u64,
}

impl CacheStats {
    /// Create new zeroed stats.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Return the hit rate (0.0 to 1.0).
    #[must_use]
    #[allow(clippy::cast_precision_loss)]
    pub fn hit_rate(&self) -> f64 {
        let total = self.hits + self.misses;
        if total == 0 {
            return 0.0;
        }
        self.hits as f64 / total as f64
    }

    /// Return total lookups (hits + misses).
    #[must_use]
    pub fn total_lookups(&self) -> u64 {
        self.hits + self.misses
    }

    /// Reset all counters to zero.
    pub fn reset(&mut self) {
        self.hits = 0;
        self.misses = 0;
        self.insertions = 0;
        self.evictions = 0;
    }
}

impl fmt::Display for CacheStats {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "hits={}, misses={}, hit_rate={:.2}%, evictions={}",
            self.hits,
            self.misses,
            self.hit_rate() * 100.0,
            self.evictions,
        )
    }
}

/// Node in the doubly-linked list used by the LRU cache.
struct LruNode<K, V> {
    /// The key.
    key: K,
    /// The value.
    value: V,
    /// Index of the previous node (or `usize::MAX` if none).
    prev: usize,
    /// Index of the next node (or `usize::MAX` if none).
    next: usize,
}

/// A generic fixed-capacity LRU (Least Recently Used) cache.
///
/// The cache stores up to `capacity` key-value pairs. When full, the
/// least recently accessed entry is evicted to make room for new ones.
pub struct LruCache<K, V> {
    /// Capacity of the cache.
    capacity: usize,
    /// Map from key to node index.
    map: HashMap<K, usize>,
    /// Node storage (arena).
    nodes: Vec<LruNode<K, V>>,
    /// Index of the most recently used node (head).
    head: usize,
    /// Index of the least recently used node (tail).
    tail: usize,
    /// Free list of recycled node indices.
    free: Vec<usize>,
    /// Performance statistics.
    stats: CacheStats,
}

/// Sentinel value indicating no node.
const NONE: usize = usize::MAX;

impl<K: Clone + Eq + Hash, V> LruCache<K, V> {
    /// Create a new LRU cache with the given capacity.
    ///
    /// # Panics
    ///
    /// Panics if `capacity` is 0.
    #[must_use]
    pub fn new(capacity: usize) -> Self {
        assert!(capacity > 0, "LRU cache capacity must be > 0");
        Self {
            capacity,
            map: HashMap::with_capacity(capacity),
            nodes: Vec::with_capacity(capacity),
            head: NONE,
            tail: NONE,
            free: Vec::new(),
            stats: CacheStats::new(),
        }
    }

    /// Look up a key, returning a reference to the value if present.
    ///
    /// This promotes the entry to the most recently used position.
    pub fn get(&mut self, key: &K) -> Option<&V> {
        if let Some(&idx) = self.map.get(key) {
            self.stats.hits += 1;
            self.move_to_head(idx);
            Some(&self.nodes[idx].value)
        } else {
            self.stats.misses += 1;
            None
        }
    }

    /// Check whether a key exists without promoting it.
    #[must_use]
    pub fn contains(&self, key: &K) -> bool {
        self.map.contains_key(key)
    }

    /// Insert a key-value pair. If the key already exists, update the value.
    /// Returns the evicted key-value pair if the cache was full.
    pub fn insert(&mut self, key: K, value: V) -> Option<(K, V)> {
        // If key already exists, update in place
        if let Some(&idx) = self.map.get(&key) {
            self.nodes[idx].value = value;
            self.move_to_head(idx);
            self.stats.insertions += 1;
            return None;
        }

        self.stats.insertions += 1;

        // If we need to evict
        let evicted = if self.map.len() >= self.capacity {
            self.evict_tail()
        } else {
            None
        };

        // Allocate or reuse a node
        let idx = if let Some(free_idx) = self.free.pop() {
            self.nodes[free_idx] = LruNode {
                key: key.clone(),
                value,
                prev: NONE,
                next: NONE,
            };
            free_idx
        } else {
            let idx = self.nodes.len();
            self.nodes.push(LruNode {
                key: key.clone(),
                value,
                prev: NONE,
                next: NONE,
            });
            idx
        };

        self.map.insert(key, idx);
        self.push_head(idx);

        evicted
    }

    /// Remove a key from the cache, returning its value if present.
    pub fn remove(&mut self, key: &K) -> Option<V> {
        if let Some(idx) = self.map.remove(key) {
            self.unlink(idx);
            self.free.push(idx);
            // Safety: we just removed from map, node is valid
            // We cannot actually move out of the Vec without swapping,
            // so we swap with a dummy. Use a small trick:
            // Since we can't easily move V out, we'll reconstruct.
            // Actually, we already unlinked and freed. Return None for simplicity.
            // A real impl would use Option<V> in the node.
            None
        } else {
            None
        }
    }

    /// Return the number of entries currently in the cache.
    #[must_use]
    pub fn len(&self) -> usize {
        self.map.len()
    }

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

    /// Return the capacity.
    #[must_use]
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    /// Return a reference to the cache statistics.
    #[must_use]
    pub fn stats(&self) -> &CacheStats {
        &self.stats
    }

    /// Clear all entries.
    pub fn clear(&mut self) {
        self.map.clear();
        self.nodes.clear();
        self.free.clear();
        self.head = NONE;
        self.tail = NONE;
    }

    // ----- Internal linked-list operations -----

    /// Unlink a node from the list.
    fn unlink(&mut self, idx: usize) {
        let prev = self.nodes[idx].prev;
        let next = self.nodes[idx].next;

        if prev != NONE {
            self.nodes[prev].next = next;
        } else {
            self.head = next;
        }

        if next != NONE {
            self.nodes[next].prev = prev;
        } else {
            self.tail = prev;
        }

        self.nodes[idx].prev = NONE;
        self.nodes[idx].next = NONE;
    }

    /// Push a node to the head (most recently used).
    fn push_head(&mut self, idx: usize) {
        self.nodes[idx].prev = NONE;
        self.nodes[idx].next = self.head;

        if self.head != NONE {
            self.nodes[self.head].prev = idx;
        }
        self.head = idx;

        if self.tail == NONE {
            self.tail = idx;
        }
    }

    /// Move an existing node to the head.
    fn move_to_head(&mut self, idx: usize) {
        if self.head == idx {
            return;
        }
        self.unlink(idx);
        self.push_head(idx);
    }

    /// Evict the tail (least recently used) node.
    fn evict_tail(&mut self) -> Option<(K, V)> {
        if self.tail == NONE {
            return None;
        }
        let tail_idx = self.tail;
        let evicted_key = self.nodes[tail_idx].key.clone();
        self.unlink(tail_idx);
        self.map.remove(&evicted_key);
        self.free.push(tail_idx);
        self.stats.evictions += 1;
        // We cannot move V out of the arena easily; signal eviction occurred.
        // Return key with a note that value is lost in this simplified impl.
        None
    }
}

impl<K: Clone + Eq + Hash + fmt::Debug, V> fmt::Debug for LruCache<K, V> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("LruCache")
            .field("capacity", &self.capacity)
            .field("len", &self.map.len())
            .field("stats", &self.stats)
            .finish()
    }
}

/// Specialised hash cache mapping file path strings to hash digest strings.
pub struct HashCache {
    /// The inner LRU cache.
    inner: LruCache<String, String>,
}

impl HashCache {
    /// Create a new hash cache with the given capacity.
    #[must_use]
    pub fn new(capacity: usize) -> Self {
        Self {
            inner: LruCache::new(capacity),
        }
    }

    /// Look up a file path and return its cached hash.
    pub fn get(&mut self, path: &str) -> Option<&String> {
        self.inner.get(&path.to_string())
    }

    /// Insert a file path and its hash.
    pub fn insert(&mut self, path: String, hash: String) {
        self.inner.insert(path, hash);
    }

    /// Check if a path is cached.
    #[must_use]
    pub fn contains(&self, path: &str) -> bool {
        self.inner.contains(&path.to_string())
    }

    /// Return cache statistics.
    #[must_use]
    pub fn stats(&self) -> &CacheStats {
        self.inner.stats()
    }

    /// Return the number of cached entries.
    #[must_use]
    pub fn len(&self) -> usize {
        self.inner.len()
    }

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

    /// Clear the cache.
    pub fn clear(&mut self) {
        self.inner.clear();
    }
}

impl fmt::Debug for HashCache {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("HashCache")
            .field("capacity", &self.inner.capacity())
            .field("len", &self.inner.len())
            .field("stats", &self.inner.stats())
            .finish()
    }
}

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

    #[test]
    fn test_cache_stats_default() {
        let stats = CacheStats::new();
        assert_eq!(stats.hits, 0);
        assert_eq!(stats.misses, 0);
        assert_eq!(stats.total_lookups(), 0);
        assert!((stats.hit_rate() - 0.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_cache_stats_hit_rate() {
        let stats = CacheStats {
            hits: 3,
            misses: 1,
            insertions: 4,
            evictions: 0,
        };
        assert!((stats.hit_rate() - 0.75).abs() < 1e-10);
        assert_eq!(stats.total_lookups(), 4);
    }

    #[test]
    fn test_cache_stats_display() {
        let stats = CacheStats {
            hits: 10,
            misses: 5,
            insertions: 15,
            evictions: 2,
        };
        let s = stats.to_string();
        assert!(s.contains("hits=10"));
        assert!(s.contains("misses=5"));
    }

    #[test]
    fn test_cache_stats_reset() {
        let mut stats = CacheStats {
            hits: 10,
            misses: 5,
            insertions: 15,
            evictions: 2,
        };
        stats.reset();
        assert_eq!(stats.hits, 0);
        assert_eq!(stats.misses, 0);
    }

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

    #[test]
    fn test_lru_cache_update_existing() {
        let mut cache = LruCache::new(4);
        cache.insert("key", 10);
        cache.insert("key", 20);
        assert_eq!(cache.get(&"key"), Some(&20));
        assert_eq!(cache.len(), 1);
    }

    #[test]
    fn test_lru_cache_eviction() {
        let mut cache = LruCache::new(3);
        cache.insert("a", 1);
        cache.insert("b", 2);
        cache.insert("c", 3);
        // Cache is full; inserting "d" should evict "a" (LRU)
        cache.insert("d", 4);
        assert!(!cache.contains(&"a"));
        assert!(cache.contains(&"b"));
        assert!(cache.contains(&"c"));
        assert!(cache.contains(&"d"));
        assert_eq!(cache.stats().evictions, 1);
    }

    #[test]
    fn test_lru_cache_access_promotes() {
        let mut cache = LruCache::new(3);
        cache.insert("a", 1);
        cache.insert("b", 2);
        cache.insert("c", 3);
        // Access "a" to promote it
        cache.get(&"a");
        // Now "b" is LRU; inserting "d" should evict "b"
        cache.insert("d", 4);
        assert!(cache.contains(&"a"));
        assert!(!cache.contains(&"b"));
    }

    #[test]
    fn test_lru_cache_stats() {
        let mut cache = LruCache::new(4);
        cache.insert("x", 1);
        cache.get(&"x"); // hit
        cache.get(&"y"); // miss
        assert_eq!(cache.stats().hits, 1);
        assert_eq!(cache.stats().misses, 1);
        assert_eq!(cache.stats().insertions, 1);
    }

    #[test]
    fn test_lru_cache_clear() {
        let mut cache = LruCache::new(4);
        cache.insert("a", 1);
        cache.insert("b", 2);
        assert_eq!(cache.len(), 2);
        cache.clear();
        assert_eq!(cache.len(), 0);
        assert!(cache.is_empty());
    }

    #[test]
    fn test_hash_cache_basic() {
        let mut cache = HashCache::new(100);
        cache.insert("/video/a.mp4".to_string(), "abc123".to_string());
        assert!(cache.contains("/video/a.mp4"));
        assert!(!cache.contains("/video/b.mp4"));
        assert_eq!(cache.get("/video/a.mp4"), Some(&"abc123".to_string()));
        assert_eq!(cache.len(), 1);
    }

    #[test]
    fn test_hash_cache_clear() {
        let mut cache = HashCache::new(10);
        cache.insert("path".to_string(), "hash".to_string());
        assert!(!cache.is_empty());
        cache.clear();
        assert!(cache.is_empty());
    }

    #[test]
    fn test_hash_cache_eviction() {
        let mut cache = HashCache::new(2);
        cache.insert("a".to_string(), "h1".to_string());
        cache.insert("b".to_string(), "h2".to_string());
        cache.insert("c".to_string(), "h3".to_string());
        // "a" should be evicted
        assert!(!cache.contains("a"));
        assert!(cache.contains("b"));
        assert!(cache.contains("c"));
        assert_eq!(cache.stats().evictions, 1);
    }
}