oximedia-dedup 0.1.1

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
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
//! Cryptographic and content-based hashing for deduplication.
//!
//! This module provides:
//! - BLAKE3 cryptographic hashing for exact duplicate detection
//! - Content-based chunking with rolling hash (Rabin fingerprinting)
//! - Chunk-level deduplication for efficient storage
//! - Hash indexing and comparison

use crate::{DedupError, DedupResult};
use blake3::Hasher;
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::Path;

/// Size of read buffer for hashing
const BUFFER_SIZE: usize = 65536; // 64 KB

/// Default chunk size for content-based chunking
const DEFAULT_CHUNK_SIZE: usize = 4096; // 4 KB

/// Minimum chunk size
const MIN_CHUNK_SIZE: usize = 1024; // 1 KB

/// Maximum chunk size
const MAX_CHUNK_SIZE: usize = 1048576; // 1 MB

/// Rabin polynomial for rolling hash
const RABIN_POLYNOMIAL: u64 = 0x3DA3_358B_4DC1_73E9;

/// File hash result containing BLAKE3 hash.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileHash {
    hash: [u8; 32],
}

impl FileHash {
    /// Create from byte array.
    #[must_use]
    pub fn from_bytes(bytes: [u8; 32]) -> Self {
        Self { hash: bytes }
    }

    /// Get hash as bytes.
    #[must_use]
    pub fn as_bytes(&self) -> &[u8; 32] {
        &self.hash
    }

    /// Convert to hex string.
    #[must_use]
    pub fn to_hex(&self) -> String {
        hex::encode(self.hash)
    }

    /// Create from hex string.
    ///
    /// # Errors
    ///
    /// Returns an error if the hex string is invalid.
    pub fn from_hex(s: &str) -> DedupResult<Self> {
        let bytes =
            hex::decode(s).map_err(|e| DedupError::Hash(format!("Invalid hex string: {e}")))?;
        if bytes.len() != 32 {
            return Err(DedupError::Hash(format!(
                "Invalid hash length: expected 32, got {}",
                bytes.len()
            )));
        }
        let mut hash = [0u8; 32];
        hash.copy_from_slice(&bytes);
        Ok(Self::from_bytes(hash))
    }

    /// Compute Hamming distance between two hashes.
    #[must_use]
    pub fn hamming_distance(&self, other: &Self) -> u32 {
        self.hash
            .iter()
            .zip(other.hash.iter())
            .map(|(a, b)| (a ^ b).count_ones())
            .sum()
    }

    /// Compute similarity (0.0-1.0) based on Hamming distance.
    #[must_use]
    pub fn similarity(&self, other: &Self) -> f64 {
        let distance = self.hamming_distance(other);
        let max_distance = 256; // 32 bytes * 8 bits
        1.0 - (f64::from(distance) / f64::from(max_distance))
    }
}

/// Compute BLAKE3 hash of a file.
///
/// # Errors
///
/// Returns an error if the file cannot be read.
pub fn compute_file_hash(path: impl AsRef<Path>) -> DedupResult<FileHash> {
    let file = File::open(path)?;
    let mut reader = BufReader::with_capacity(BUFFER_SIZE, file);
    let mut hasher = Hasher::new();
    let mut buffer = vec![0u8; BUFFER_SIZE];

    loop {
        let n = reader.read(&mut buffer)?;
        if n == 0 {
            break;
        }
        hasher.update(&buffer[..n]);
    }

    let hash = hasher.finalize();
    Ok(FileHash::from_bytes(*hash.as_bytes()))
}

/// Compute BLAKE3 hash of data.
#[must_use]
pub fn compute_data_hash(data: &[u8]) -> FileHash {
    let hash = blake3::hash(data);
    FileHash::from_bytes(*hash.as_bytes())
}

/// Content chunk with hash.
#[derive(Debug, Clone)]
pub struct Chunk {
    /// Offset in file
    pub offset: u64,

    /// Size of chunk
    pub size: usize,

    /// BLAKE3 hash of chunk
    pub hash: FileHash,
}

/// Rolling hash for content-based chunking.
pub struct RollingHash {
    window_size: usize,
    polynomial: u64,
    mod_mask: u64,
    hash: u64,
    window: Vec<u8>,
    window_pos: usize,
    pow_table: Vec<u64>,
}

impl RollingHash {
    /// Create a new rolling hash with specified window size.
    #[must_use]
    pub fn new(window_size: usize) -> Self {
        let polynomial = RABIN_POLYNOMIAL;
        let mod_mask = (1u64 << 63) - 1; // Use lower 63 bits

        // Precompute powers of polynomial
        let mut pow_table = vec![1u64; window_size];
        for i in 1..window_size {
            pow_table[i] = pow_table[i - 1].wrapping_mul(polynomial) & mod_mask;
        }

        Self {
            window_size,
            polynomial,
            mod_mask,
            hash: 0,
            window: vec![0u8; window_size],
            window_pos: 0,
            pow_table,
        }
    }

    /// Roll the hash with a new byte.
    pub fn roll(&mut self, byte: u8) -> u64 {
        // Remove oldest byte's contribution
        let old_byte = self.window[self.window_pos];
        let old_contribution =
            u64::from(old_byte).wrapping_mul(self.pow_table[self.window_size - 1]);
        self.hash = self.hash.wrapping_sub(old_contribution) & self.mod_mask;

        // Shift hash and add new byte
        self.hash = self.hash.wrapping_mul(self.polynomial) & self.mod_mask;
        self.hash = self.hash.wrapping_add(u64::from(byte)) & self.mod_mask;

        // Update window
        self.window[self.window_pos] = byte;
        self.window_pos = (self.window_pos + 1) % self.window_size;

        self.hash
    }

    /// Get current hash value.
    #[must_use]
    pub fn hash(&self) -> u64 {
        self.hash
    }

    /// Reset the rolling hash.
    pub fn reset(&mut self) {
        self.hash = 0;
        self.window.fill(0);
        self.window_pos = 0;
    }
}

/// Chunker for content-based chunking.
pub struct Chunker {
    chunk_size: usize,
    min_size: usize,
    max_size: usize,
    rolling_hash: RollingHash,
}

impl Chunker {
    /// Create a new chunker with specified parameters.
    #[must_use]
    pub fn new(chunk_size: usize) -> Self {
        let chunk_size = chunk_size.clamp(MIN_CHUNK_SIZE, MAX_CHUNK_SIZE);
        let min_size = chunk_size / 2;
        let max_size = chunk_size * 2;
        let rolling_hash = RollingHash::new(64); // 64-byte window

        Self {
            chunk_size,
            min_size,
            max_size,
            rolling_hash,
        }
    }

    /// Check if this is a chunk boundary.
    fn is_boundary(&self, hash: u64) -> bool {
        // Use lower bits as boundary marker
        let mask = (1u64 << 12) - 1; // 12-bit mask for 4KB average chunks
        (hash & mask) == 0
    }

    /// Chunk data into content-defined chunks.
    #[must_use]
    pub fn chunk(&mut self, data: &[u8]) -> Vec<Chunk> {
        let mut chunks = Vec::new();
        let mut chunk_start = 0;
        let mut offset = 0u64;

        self.rolling_hash.reset();

        for (pos, &byte) in data.iter().enumerate() {
            let hash = self.rolling_hash.roll(byte);
            let chunk_len = pos - chunk_start;

            // Check for chunk boundary
            if chunk_len >= self.min_size && (self.is_boundary(hash) || chunk_len >= self.max_size)
            {
                let chunk_data = &data[chunk_start..pos + 1];
                let chunk_hash = compute_data_hash(chunk_data);

                chunks.push(Chunk {
                    offset,
                    size: chunk_data.len(),
                    hash: chunk_hash,
                });

                chunk_start = pos + 1;
                offset += chunk_data.len() as u64;
                self.rolling_hash.reset();
            }
        }

        // Add remaining data as final chunk
        if chunk_start < data.len() {
            let chunk_data = &data[chunk_start..];
            let chunk_hash = compute_data_hash(chunk_data);

            chunks.push(Chunk {
                offset,
                size: chunk_data.len(),
                hash: chunk_hash,
            });
        }

        chunks
    }

    /// Chunk a file into content-defined chunks.
    ///
    /// # Errors
    ///
    /// Returns an error if the file cannot be read.
    pub fn chunk_file(&mut self, path: impl AsRef<Path>) -> DedupResult<Vec<Chunk>> {
        let file = File::open(path)?;
        let mut reader = BufReader::with_capacity(BUFFER_SIZE, file);
        let mut chunks = Vec::new();
        let mut chunk_buffer = Vec::new();
        let mut offset = 0u64;
        let mut buffer = vec![0u8; BUFFER_SIZE];

        self.rolling_hash.reset();

        loop {
            let n = reader.read(&mut buffer)?;
            if n == 0 {
                break;
            }

            for &byte in &buffer[..n] {
                chunk_buffer.push(byte);
                let hash = self.rolling_hash.roll(byte);
                let chunk_len = chunk_buffer.len();

                // Check for chunk boundary
                if chunk_len >= self.min_size
                    && (self.is_boundary(hash) || chunk_len >= self.max_size)
                {
                    let chunk_hash = compute_data_hash(&chunk_buffer);

                    chunks.push(Chunk {
                        offset,
                        size: chunk_buffer.len(),
                        hash: chunk_hash,
                    });

                    offset += chunk_buffer.len() as u64;
                    chunk_buffer.clear();
                    self.rolling_hash.reset();
                }
            }
        }

        // Add remaining data as final chunk
        if !chunk_buffer.is_empty() {
            let chunk_hash = compute_data_hash(&chunk_buffer);

            chunks.push(Chunk {
                offset,
                size: chunk_buffer.len(),
                hash: chunk_hash,
            });
        }

        Ok(chunks)
    }
}

/// Chunk index for deduplication.
pub struct ChunkIndex {
    chunks: Vec<(FileHash, Vec<String>)>,
}

impl ChunkIndex {
    /// Create a new chunk index.
    #[must_use]
    pub fn new() -> Self {
        Self { chunks: Vec::new() }
    }

    /// Add chunks from a file.
    pub fn add_file(&mut self, file_path: &str, chunks: &[Chunk]) {
        for chunk in chunks {
            if let Some((_, files)) = self.chunks.iter_mut().find(|(h, _)| h == &chunk.hash) {
                if !files.contains(&file_path.to_string()) {
                    files.push(file_path.to_string());
                }
            } else {
                self.chunks
                    .push((chunk.hash.clone(), vec![file_path.to_string()]));
            }
        }
    }

    /// Find duplicate chunks.
    #[must_use]
    pub fn find_duplicates(&self) -> Vec<(FileHash, Vec<String>)> {
        self.chunks
            .iter()
            .filter(|(_, files)| files.len() > 1)
            .cloned()
            .collect()
    }

    /// Get total number of chunks.
    #[must_use]
    pub fn chunk_count(&self) -> usize {
        self.chunks.len()
    }

    /// Get number of duplicate chunks.
    #[must_use]
    pub fn duplicate_count(&self) -> usize {
        self.chunks
            .iter()
            .filter(|(_, files)| files.len() > 1)
            .count()
    }

    /// Calculate deduplication ratio.
    #[must_use]
    pub fn dedup_ratio(&self) -> f64 {
        if self.chunks.is_empty() {
            return 0.0;
        }
        let total: usize = self.chunks.iter().map(|(_, files)| files.len()).sum();
        let unique = self.chunks.len();
        if total == 0 {
            return 0.0;
        }
        1.0 - (unique as f64 / total as f64)
    }
}

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

/// Compute similarity between two files based on shared chunks.
///
/// # Errors
///
/// Returns an error if files cannot be read or chunked.
pub fn compute_chunk_similarity(
    path1: impl AsRef<Path>,
    path2: impl AsRef<Path>,
    chunk_size: usize,
) -> DedupResult<f64> {
    let mut chunker = Chunker::new(chunk_size);

    let chunks1 = chunker.chunk_file(path1)?;
    let chunks2 = chunker.chunk_file(path2)?;

    if chunks1.is_empty() || chunks2.is_empty() {
        return Ok(0.0);
    }

    // Count shared chunks
    let hashes1: Vec<_> = chunks1.iter().map(|c| &c.hash).collect();
    let hashes2: Vec<_> = chunks2.iter().map(|c| &c.hash).collect();

    let shared = hashes1.iter().filter(|h| hashes2.contains(h)).count();

    let total = hashes1.len().max(hashes2.len());
    Ok(shared as f64 / total as f64)
}

/// Helper function to decode hex strings
mod hex {
    use super::DedupError;

    pub fn encode(bytes: [u8; 32]) -> String {
        bytes.iter().map(|b| format!("{b:02x}")).collect::<String>()
    }

    pub fn decode(s: &str) -> Result<Vec<u8>, DedupError> {
        if s.len() % 2 != 0 {
            return Err(DedupError::Hash("Odd hex string length".to_string()));
        }

        (0..s.len())
            .step_by(2)
            .map(|i| {
                u8::from_str_radix(&s[i..i + 2], 16)
                    .map_err(|e| DedupError::Hash(format!("Invalid hex digit: {e}")))
            })
            .collect()
    }
}

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

    #[test]
    fn test_compute_data_hash() {
        let data = b"Hello, World!";
        let hash = compute_data_hash(data);
        assert_eq!(hash.as_bytes().len(), 32);
    }

    #[test]
    fn test_hash_hex() {
        let data = b"Test";
        let hash = compute_data_hash(data);
        let hex = hash.to_hex();
        assert_eq!(hex.len(), 64); // 32 bytes * 2 hex chars

        let decoded = FileHash::from_hex(&hex).expect("operation should succeed");
        assert_eq!(hash, decoded);
    }

    #[test]
    fn test_hash_similarity() {
        let hash1 = compute_data_hash(b"Hello");
        let hash2 = compute_data_hash(b"Hello");
        let hash3 = compute_data_hash(b"World");

        assert_eq!(hash1.similarity(&hash2), 1.0);
        assert!(hash1.similarity(&hash3) < 1.0);
    }

    #[test]
    fn test_rolling_hash() {
        let mut rh = RollingHash::new(8);
        let data = b"Hello, World!";

        for &byte in data {
            rh.roll(byte);
        }

        assert!(rh.hash() != 0);

        rh.reset();
        assert_eq!(rh.hash(), 0);
    }

    #[test]
    fn test_chunker() {
        let mut chunker = Chunker::new(DEFAULT_CHUNK_SIZE);
        let data = vec![0u8; 100_000]; // 100 KB of zeros

        let chunks = chunker.chunk(&data);
        assert!(!chunks.is_empty());

        // Verify chunk properties
        let total_size: usize = chunks.iter().map(|c| c.size).sum();
        assert_eq!(total_size, data.len());

        // Verify offsets
        let mut expected_offset = 0u64;
        for chunk in &chunks {
            assert_eq!(chunk.offset, expected_offset);
            expected_offset += chunk.size as u64;
        }
    }

    #[test]
    fn test_chunk_index() {
        let mut index = ChunkIndex::new();

        let chunks1 = vec![
            Chunk {
                offset: 0,
                size: 100,
                hash: compute_data_hash(b"chunk1"),
            },
            Chunk {
                offset: 100,
                size: 100,
                hash: compute_data_hash(b"chunk2"),
            },
        ];

        let chunks2 = vec![
            Chunk {
                offset: 0,
                size: 100,
                hash: compute_data_hash(b"chunk1"), // Duplicate
            },
            Chunk {
                offset: 100,
                size: 100,
                hash: compute_data_hash(b"chunk3"),
            },
        ];

        index.add_file("file1.txt", &chunks1);
        index.add_file("file2.txt", &chunks2);

        assert_eq!(index.chunk_count(), 3); // chunk1, chunk2, chunk3
        assert_eq!(index.duplicate_count(), 1); // chunk1 is duplicated

        let duplicates = index.find_duplicates();
        assert_eq!(duplicates.len(), 1);
        assert_eq!(duplicates[0].1.len(), 2); // chunk1 in both files
    }

    #[test]
    fn test_dedup_ratio() {
        let mut index = ChunkIndex::new();
        assert_eq!(index.dedup_ratio(), 0.0);

        // Add unique chunks
        let chunks = vec![
            Chunk {
                offset: 0,
                size: 100,
                hash: compute_data_hash(b"a"),
            },
            Chunk {
                offset: 100,
                size: 100,
                hash: compute_data_hash(b"b"),
            },
        ];
        index.add_file("file1", &chunks);

        // No deduplication yet
        assert_eq!(index.dedup_ratio(), 0.0);

        // Add duplicate chunks
        index.add_file("file2", &chunks);

        // 50% deduplication (2 unique, 4 total references)
        assert!((index.dedup_ratio() - 0.5).abs() < 0.01);
    }

    #[test]
    fn test_hamming_distance() {
        let hash1 = FileHash::from_bytes([0xFF; 32]);
        let hash2 = FileHash::from_bytes([0x00; 32]);
        assert_eq!(hash1.hamming_distance(&hash2), 256);

        let hash3 = FileHash::from_bytes([0xFF; 32]);
        assert_eq!(hash1.hamming_distance(&hash3), 0);
    }
}