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
#![allow(dead_code)]
//! In-memory signature storage with lookup and expiration.
//!
//! Provides a time-aware store for media content signatures (hashes,
//! fingerprints) with automatic expiration and efficient lookup,
//! suitable for caching deduplication results.

use std::collections::HashMap;
use std::time::{Duration, Instant};

/// Default time-to-live for signature entries (1 hour).
const DEFAULT_TTL_SECS: u64 = 3600;

/// Maximum number of entries before forced eviction.
const DEFAULT_MAX_ENTRIES: usize = 100_000;

/// A stored signature entry with metadata.
#[derive(Debug, Clone)]
pub struct SignatureEntry {
    /// The signature bytes.
    pub signature: Vec<u8>,
    /// File size of the source.
    pub file_size: u64,
    /// Timestamp when the entry was created.
    created_at: Instant,
    /// Timestamp of the last access.
    last_accessed: Instant,
    /// Number of times this entry was accessed.
    access_count: u64,
    /// Time-to-live for this entry.
    ttl: Duration,
}

impl SignatureEntry {
    /// Create a new signature entry.
    pub fn new(signature: Vec<u8>, file_size: u64, ttl: Duration) -> Self {
        let now = Instant::now();
        Self {
            signature,
            file_size,
            created_at: now,
            last_accessed: now,
            access_count: 0,
            ttl,
        }
    }

    /// Check if this entry has expired.
    pub fn is_expired(&self) -> bool {
        self.created_at.elapsed() > self.ttl
    }

    /// Get the age of this entry.
    pub fn age(&self) -> Duration {
        self.created_at.elapsed()
    }

    /// Get the time since last access.
    pub fn idle_time(&self) -> Duration {
        self.last_accessed.elapsed()
    }

    /// Get the access count.
    pub fn access_count(&self) -> u64 {
        self.access_count
    }

    /// Record an access.
    pub fn record_access(&mut self) {
        self.last_accessed = Instant::now();
        self.access_count += 1;
    }

    /// Get the signature bytes.
    pub fn signature(&self) -> &[u8] {
        &self.signature
    }
}

/// Configuration for the signature store.
#[derive(Debug, Clone)]
pub struct StoreConfig {
    /// Default time-to-live for entries.
    pub default_ttl: Duration,
    /// Maximum number of entries.
    pub max_entries: usize,
    /// Whether to use LRU eviction when full.
    pub lru_eviction: bool,
}

impl Default for StoreConfig {
    fn default() -> Self {
        Self {
            default_ttl: Duration::from_secs(DEFAULT_TTL_SECS),
            max_entries: DEFAULT_MAX_ENTRIES,
            lru_eviction: true,
        }
    }
}

impl StoreConfig {
    /// Set the default TTL.
    pub fn with_ttl(mut self, ttl: Duration) -> Self {
        self.default_ttl = ttl;
        self
    }

    /// Set the max entries.
    pub fn with_max_entries(mut self, max: usize) -> Self {
        self.max_entries = max.max(1);
        self
    }

    /// Enable or disable LRU eviction.
    pub fn with_lru_eviction(mut self, enabled: bool) -> Self {
        self.lru_eviction = enabled;
        self
    }
}

/// In-memory signature store.
#[derive(Debug)]
pub struct SignatureStore {
    /// Stored entries keyed by an identifier.
    entries: HashMap<String, SignatureEntry>,
    /// Configuration.
    config: StoreConfig,
    /// Total number of inserts.
    total_inserts: u64,
    /// Total number of lookups.
    total_lookups: u64,
    /// Total number of hits.
    total_hits: u64,
    /// Total number of evictions.
    total_evictions: u64,
}

impl SignatureStore {
    /// Create a new signature store with default configuration.
    pub fn new() -> Self {
        Self::with_config(StoreConfig::default())
    }

    /// Create a new signature store with custom configuration.
    pub fn with_config(config: StoreConfig) -> Self {
        Self {
            entries: HashMap::new(),
            config,
            total_inserts: 0,
            total_lookups: 0,
            total_hits: 0,
            total_evictions: 0,
        }
    }

    /// Insert a signature into the store.
    pub fn insert(&mut self, key: String, signature: Vec<u8>, file_size: u64) {
        // Evict if needed
        if self.entries.len() >= self.config.max_entries {
            self.evict_one();
        }
        let entry = SignatureEntry::new(signature, file_size, self.config.default_ttl);
        self.entries.insert(key, entry);
        self.total_inserts += 1;
    }

    /// Insert with a custom TTL.
    pub fn insert_with_ttl(
        &mut self,
        key: String,
        signature: Vec<u8>,
        file_size: u64,
        ttl: Duration,
    ) {
        if self.entries.len() >= self.config.max_entries {
            self.evict_one();
        }
        let entry = SignatureEntry::new(signature, file_size, ttl);
        self.entries.insert(key, entry);
        self.total_inserts += 1;
    }

    /// Look up a signature by key.
    pub fn get(&mut self, key: &str) -> Option<&[u8]> {
        self.total_lookups += 1;
        // Remove if expired
        if self.entries.get(key).map_or(false, |e| e.is_expired()) {
            self.entries.remove(key);
            return None;
        }
        if let Some(entry) = self.entries.get_mut(key) {
            entry.record_access();
            self.total_hits += 1;
            Some(entry.signature())
        } else {
            None
        }
    }

    /// Check if a key exists and is not expired.
    pub fn contains(&self, key: &str) -> bool {
        self.entries.get(key).map_or(false, |e| !e.is_expired())
    }

    /// Remove a specific entry.
    pub fn remove(&mut self, key: &str) -> bool {
        self.entries.remove(key).is_some()
    }

    /// Get the number of entries (including potentially expired ones).
    pub fn len(&self) -> usize {
        self.entries.len()
    }

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

    /// Remove all expired entries.
    pub fn purge_expired(&mut self) -> usize {
        let before = self.entries.len();
        self.entries.retain(|_, entry| !entry.is_expired());
        let removed = before - self.entries.len();
        self.total_evictions += removed as u64;
        removed
    }

    /// Clear all entries.
    pub fn clear(&mut self) {
        self.entries.clear();
    }

    /// Get store statistics.
    pub fn stats(&self) -> StoreStats {
        let total_signature_bytes: u64 = self
            .entries
            .values()
            .map(|e| e.signature.len() as u64)
            .sum();
        StoreStats {
            entries: self.entries.len(),
            total_inserts: self.total_inserts,
            total_lookups: self.total_lookups,
            total_hits: self.total_hits,
            total_evictions: self.total_evictions,
            total_signature_bytes,
        }
    }

    /// Evict one entry (LRU or oldest).
    fn evict_one(&mut self) {
        if self.entries.is_empty() {
            return;
        }

        // First, try to evict an expired entry
        let expired_key = self
            .entries
            .iter()
            .find(|(_, e)| e.is_expired())
            .map(|(k, _)| k.clone());

        if let Some(key) = expired_key {
            self.entries.remove(&key);
            self.total_evictions += 1;
            return;
        }

        // Otherwise, evict the least recently used entry
        if self.config.lru_eviction {
            let lru_key = self
                .entries
                .iter()
                .min_by_key(|(_, e)| e.last_accessed)
                .map(|(k, _)| k.clone());
            if let Some(key) = lru_key {
                self.entries.remove(&key);
                self.total_evictions += 1;
            }
        } else {
            // Evict the oldest entry
            let oldest_key = self
                .entries
                .iter()
                .min_by_key(|(_, e)| e.created_at)
                .map(|(k, _)| k.clone());
            if let Some(key) = oldest_key {
                self.entries.remove(&key);
                self.total_evictions += 1;
            }
        }
    }

    /// Find entries whose signatures match a given signature.
    pub fn find_matching(&self, signature: &[u8]) -> Vec<&str> {
        self.entries
            .iter()
            .filter(|(_, e)| !e.is_expired() && e.signature == signature)
            .map(|(k, _)| k.as_str())
            .collect()
    }
}

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

/// Store statistics.
#[derive(Debug, Clone)]
pub struct StoreStats {
    /// Number of current entries.
    pub entries: usize,
    /// Total inserts performed.
    pub total_inserts: u64,
    /// Total lookups performed.
    pub total_lookups: u64,
    /// Total cache hits.
    pub total_hits: u64,
    /// Total evictions.
    pub total_evictions: u64,
    /// Total bytes of stored signatures.
    pub total_signature_bytes: u64,
}

impl StoreStats {
    /// Get the hit rate as a fraction.
    #[allow(clippy::cast_precision_loss)]
    pub fn hit_rate(&self) -> f64 {
        if self.total_lookups == 0 {
            return 0.0;
        }
        self.total_hits as f64 / self.total_lookups as f64
    }
}

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

    #[test]
    fn test_signature_entry_new() {
        let entry = SignatureEntry::new(vec![1, 2, 3], 1024, Duration::from_secs(60));
        assert_eq!(entry.signature(), &[1, 2, 3]);
        assert_eq!(entry.file_size, 1024);
        assert_eq!(entry.access_count(), 0);
        assert!(!entry.is_expired());
    }

    #[test]
    fn test_signature_entry_access() {
        let mut entry = SignatureEntry::new(vec![1], 100, Duration::from_secs(600));
        entry.record_access();
        entry.record_access();
        assert_eq!(entry.access_count(), 2);
    }

    #[test]
    fn test_store_config_default() {
        let cfg = StoreConfig::default();
        assert_eq!(cfg.default_ttl, Duration::from_secs(DEFAULT_TTL_SECS));
        assert_eq!(cfg.max_entries, DEFAULT_MAX_ENTRIES);
        assert!(cfg.lru_eviction);
    }

    #[test]
    fn test_store_config_builders() {
        let cfg = StoreConfig::default()
            .with_ttl(Duration::from_secs(300))
            .with_max_entries(500)
            .with_lru_eviction(false);
        assert_eq!(cfg.default_ttl, Duration::from_secs(300));
        assert_eq!(cfg.max_entries, 500);
        assert!(!cfg.lru_eviction);
    }

    #[test]
    fn test_store_insert_and_get() {
        let mut store = SignatureStore::new();
        store.insert("file1".to_string(), vec![0xAB, 0xCD], 2048);
        let sig = store.get("file1").unwrap();
        assert_eq!(sig, &[0xAB, 0xCD]);
    }

    #[test]
    fn test_store_get_nonexistent() {
        let mut store = SignatureStore::new();
        assert!(store.get("nope").is_none());
    }

    #[test]
    fn test_store_contains() {
        let mut store = SignatureStore::new();
        store.insert("key1".to_string(), vec![1], 100);
        assert!(store.contains("key1"));
        assert!(!store.contains("key2"));
    }

    #[test]
    fn test_store_remove() {
        let mut store = SignatureStore::new();
        store.insert("key1".to_string(), vec![1], 100);
        assert!(store.remove("key1"));
        assert!(!store.remove("key1"));
        assert!(!store.contains("key1"));
    }

    #[test]
    fn test_store_clear() {
        let mut store = SignatureStore::new();
        store.insert("a".to_string(), vec![1], 100);
        store.insert("b".to_string(), vec![2], 200);
        store.clear();
        assert!(store.is_empty());
        assert_eq!(store.len(), 0);
    }

    #[test]
    fn test_store_eviction_on_max_entries() {
        let config = StoreConfig::default().with_max_entries(3);
        let mut store = SignatureStore::with_config(config);
        store.insert("a".to_string(), vec![1], 100);
        store.insert("b".to_string(), vec![2], 100);
        store.insert("c".to_string(), vec![3], 100);
        // This should evict one entry
        store.insert("d".to_string(), vec![4], 100);
        assert_eq!(store.len(), 3);
        assert!(store.contains("d"));
    }

    #[test]
    fn test_store_stats() {
        let mut store = SignatureStore::new();
        store.insert("a".to_string(), vec![1, 2, 3], 100);
        let _ = store.get("a");
        let _ = store.get("b"); // miss

        let stats = store.stats();
        assert_eq!(stats.entries, 1);
        assert_eq!(stats.total_inserts, 1);
        assert_eq!(stats.total_lookups, 2);
        assert_eq!(stats.total_hits, 1);
        assert_eq!(stats.total_signature_bytes, 3);
    }

    #[test]
    fn test_store_hit_rate() {
        let stats = StoreStats {
            entries: 10,
            total_inserts: 10,
            total_lookups: 100,
            total_hits: 75,
            total_evictions: 0,
            total_signature_bytes: 100,
        };
        assert!((stats.hit_rate() - 0.75).abs() < f64::EPSILON);
    }

    #[test]
    fn test_store_hit_rate_empty() {
        let stats = StoreStats {
            entries: 0,
            total_inserts: 0,
            total_lookups: 0,
            total_hits: 0,
            total_evictions: 0,
            total_signature_bytes: 0,
        };
        assert!((stats.hit_rate() - 0.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_find_matching() {
        let mut store = SignatureStore::new();
        store.insert("f1".to_string(), vec![1, 2, 3], 100);
        store.insert("f2".to_string(), vec![1, 2, 3], 200);
        store.insert("f3".to_string(), vec![4, 5, 6], 300);

        let matches = store.find_matching(&[1, 2, 3]);
        assert_eq!(matches.len(), 2);
        assert!(matches.contains(&"f1"));
        assert!(matches.contains(&"f2"));
    }

    #[test]
    fn test_insert_with_custom_ttl() {
        let mut store = SignatureStore::new();
        store.insert_with_ttl("k".to_string(), vec![9], 50, Duration::from_secs(1));
        assert!(store.contains("k"));
    }

    #[test]
    fn test_expired_entry_not_returned() {
        let mut store = SignatureStore::with_config(
            StoreConfig::default().with_ttl(Duration::from_millis(0)),
        );
        store.insert("expired".to_string(), vec![1], 100);
        // With 0ms TTL, the entry should be expired immediately (or within the test)
        // We use a sleep-free approach: the TTL is 0, so elapsed > 0 => expired
        std::thread::sleep(Duration::from_millis(1));
        assert!(store.get("expired").is_none());
    }
}