oximedia-dedup 0.1.2

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
//! Extended deduplication reporting and statistics.
//!
//! Augments the base [`report`](crate::report) module with:
//! - **`ReportBuilder`**: fluent builder for assembling detailed reports
//! - **`SizeDistribution`**: histogram of duplicate file sizes
//! - **`FormatBreakdown`**: per-format duplicate statistics
//! - **`ReportSummary`**: human-readable summary generation

#![allow(dead_code)]
#![allow(clippy::cast_precision_loss)]

use std::collections::HashMap;
use std::path::{Path, PathBuf};

// ---------------------------------------------------------------------------
// DuplicateEntry
// ---------------------------------------------------------------------------

/// A single duplicate file entry for reporting.
#[derive(Debug, Clone)]
pub struct DuplicateEntry {
    /// Path to the file.
    pub path: PathBuf,
    /// File size in bytes.
    pub size: u64,
    /// Hash digest.
    pub digest: String,
    /// File extension (lowercase, no dot).
    pub extension: String,
}

impl DuplicateEntry {
    /// Create a new entry.
    pub fn new(path: PathBuf, size: u64, digest: &str) -> Self {
        let extension = path
            .extension()
            .and_then(|e| e.to_str())
            .unwrap_or("")
            .to_lowercase();
        Self {
            path,
            size,
            digest: digest.to_string(),
            extension,
        }
    }
}

// ---------------------------------------------------------------------------
// ReportBuilder
// ---------------------------------------------------------------------------

/// Fluent builder for constructing an extended dedup report.
pub struct ReportBuilder {
    /// All duplicate entries.
    entries: Vec<DuplicateEntry>,
    /// Title of the report.
    title: String,
    /// Minimum group size to include.
    min_group_size: usize,
    /// Only include files larger than this threshold.
    min_file_size: u64,
}

impl ReportBuilder {
    /// Start building a new report.
    #[must_use]
    pub fn new() -> Self {
        Self {
            entries: Vec::new(),
            title: "Deduplication Report".to_string(),
            min_group_size: 2,
            min_file_size: 0,
        }
    }

    /// Set the report title.
    #[must_use]
    pub fn title(mut self, title: &str) -> Self {
        self.title = title.to_string();
        self
    }

    /// Set minimum duplicate group size to include.
    #[must_use]
    pub fn min_group_size(mut self, n: usize) -> Self {
        self.min_group_size = n;
        self
    }

    /// Set minimum file size filter.
    #[must_use]
    pub fn min_file_size(mut self, bytes: u64) -> Self {
        self.min_file_size = bytes;
        self
    }

    /// Add a duplicate entry.
    pub fn add_entry(&mut self, entry: DuplicateEntry) {
        self.entries.push(entry);
    }

    /// Add multiple entries.
    pub fn add_entries(&mut self, entries: impl IntoIterator<Item = DuplicateEntry>) {
        self.entries.extend(entries);
    }

    /// Build the final report.
    #[must_use]
    pub fn build(self) -> ExtendedReport {
        // Group entries by digest
        let mut groups: HashMap<String, Vec<DuplicateEntry>> = HashMap::new();
        for entry in self.entries {
            if entry.size >= self.min_file_size {
                groups.entry(entry.digest.clone()).or_default().push(entry);
            }
        }

        // Filter by min group size
        let dup_groups: Vec<DuplicateGroup> = groups
            .into_iter()
            .filter(|(_, v)| v.len() >= self.min_group_size)
            .map(|(digest, files)| {
                let total_size: u64 = files.iter().map(|f| f.size).sum();
                let recoverable = files.iter().skip(1).map(|f| f.size).sum();
                DuplicateGroup {
                    digest,
                    files,
                    total_size,
                    recoverable_bytes: recoverable,
                }
            })
            .collect();

        let total_files: usize = dup_groups.iter().map(|g| g.files.len()).sum();
        let total_recoverable: u64 = dup_groups.iter().map(|g| g.recoverable_bytes).sum();

        ExtendedReport {
            title: self.title,
            groups: dup_groups,
            total_duplicate_files: total_files,
            total_recoverable_bytes: total_recoverable,
        }
    }
}

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

// ---------------------------------------------------------------------------
// DuplicateGroup / ExtendedReport
// ---------------------------------------------------------------------------

/// A group of duplicate files sharing the same digest.
#[derive(Debug, Clone)]
pub struct DuplicateGroup {
    /// The shared digest.
    pub digest: String,
    /// Files in this group.
    pub files: Vec<DuplicateEntry>,
    /// Total size of all files in the group.
    pub total_size: u64,
    /// Bytes recoverable by keeping only one copy.
    pub recoverable_bytes: u64,
}

/// Full extended deduplication report.
#[derive(Debug, Clone)]
pub struct ExtendedReport {
    /// Report title.
    pub title: String,
    /// Duplicate groups.
    pub groups: Vec<DuplicateGroup>,
    /// Total number of duplicate files.
    pub total_duplicate_files: usize,
    /// Total bytes recoverable.
    pub total_recoverable_bytes: u64,
}

impl ExtendedReport {
    /// Return the number of duplicate groups.
    #[must_use]
    pub fn group_count(&self) -> usize {
        self.groups.len()
    }

    /// Return a per-extension breakdown of duplicates.
    #[must_use]
    pub fn format_breakdown(&self) -> FormatBreakdown {
        let mut by_ext: HashMap<String, ExtStats> = HashMap::new();

        for group in &self.groups {
            for file in &group.files {
                let ext = if file.extension.is_empty() {
                    "(none)".to_string()
                } else {
                    file.extension.clone()
                };
                let stats = by_ext.entry(ext).or_insert_with(ExtStats::default);
                stats.file_count += 1;
                stats.total_bytes += file.size;
            }
        }

        FormatBreakdown {
            by_extension: by_ext,
        }
    }

    /// Return a human-readable summary string.
    #[must_use]
    pub fn summary_text(&self) -> String {
        format!(
            "{}: {} duplicate groups, {} files, {:.2} MB recoverable",
            self.title,
            self.groups.len(),
            self.total_duplicate_files,
            self.total_recoverable_bytes as f64 / (1024.0 * 1024.0),
        )
    }

    /// Build a size distribution histogram with the given bucket boundaries.
    #[must_use]
    pub fn size_distribution(&self, bucket_boundaries: &[u64]) -> SizeDistribution {
        let mut buckets = vec![0u64; bucket_boundaries.len() + 1];

        for group in &self.groups {
            for file in &group.files {
                let idx = bucket_boundaries
                    .iter()
                    .position(|&b| file.size < b)
                    .unwrap_or(bucket_boundaries.len());
                buckets[idx] += 1;
            }
        }

        SizeDistribution {
            boundaries: bucket_boundaries.to_vec(),
            counts: buckets,
        }
    }

    /// Filter groups, keeping only those containing a file under `prefix`.
    #[must_use]
    pub fn filter_by_path(&self, prefix: &Path) -> Vec<&DuplicateGroup> {
        self.groups
            .iter()
            .filter(|g| g.files.iter().any(|f| f.path.starts_with(prefix)))
            .collect()
    }
}

// ---------------------------------------------------------------------------
// FormatBreakdown
// ---------------------------------------------------------------------------

/// Per-extension statistics.
#[derive(Debug, Clone, Default)]
pub struct ExtStats {
    /// Number of files.
    pub file_count: usize,
    /// Total bytes.
    pub total_bytes: u64,
}

/// Duplicate statistics broken down by file extension.
#[derive(Debug, Clone)]
pub struct FormatBreakdown {
    /// Map from extension to stats.
    pub by_extension: HashMap<String, ExtStats>,
}

impl FormatBreakdown {
    /// Return the extension with the most duplicate files.
    #[must_use]
    pub fn most_common_ext(&self) -> Option<(&str, usize)> {
        self.by_extension
            .iter()
            .max_by_key(|(_, s)| s.file_count)
            .map(|(ext, s)| (ext.as_str(), s.file_count))
    }
}

// ---------------------------------------------------------------------------
// SizeDistribution
// ---------------------------------------------------------------------------

/// Histogram of file sizes.
#[derive(Debug, Clone)]
pub struct SizeDistribution {
    /// Bucket boundaries (upper-exclusive).
    pub boundaries: Vec<u64>,
    /// Count of files in each bucket.
    pub counts: Vec<u64>,
}

impl SizeDistribution {
    /// Return total file count across all buckets.
    #[must_use]
    pub fn total(&self) -> u64 {
        self.counts.iter().sum()
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn sample_entries() -> Vec<DuplicateEntry> {
        vec![
            DuplicateEntry::new(PathBuf::from("/a.mp4"), 1_000_000, "hash1"),
            DuplicateEntry::new(PathBuf::from("/b.mp4"), 1_000_000, "hash1"),
            DuplicateEntry::new(PathBuf::from("/c.mov"), 500_000, "hash2"),
            DuplicateEntry::new(PathBuf::from("/d.mov"), 500_000, "hash2"),
            DuplicateEntry::new(PathBuf::from("/e.wav"), 200_000, "hash3"),
        ]
    }

    #[test]
    fn test_report_builder_basic() {
        let mut builder = ReportBuilder::new();
        builder.add_entries(sample_entries());
        let report = builder.build();
        assert_eq!(report.group_count(), 2); // hash1 and hash2 have groups >= 2
    }

    #[test]
    fn test_report_builder_title() {
        let report = ReportBuilder::new().title("My Report").build();
        assert_eq!(report.title, "My Report");
    }

    #[test]
    fn test_report_builder_min_group_size() {
        let mut builder = ReportBuilder::new().min_group_size(3);
        builder.add_entries(sample_entries());
        let report = builder.build();
        assert_eq!(report.group_count(), 0); // no group has 3+ files
    }

    #[test]
    fn test_report_builder_min_file_size() {
        let mut builder = ReportBuilder::new().min_file_size(600_000);
        builder.add_entries(sample_entries());
        let report = builder.build();
        // Only hash1 group (1MB each) passes
        assert_eq!(report.group_count(), 1);
    }

    #[test]
    fn test_recoverable_bytes() {
        let mut builder = ReportBuilder::new();
        builder.add_entries(sample_entries());
        let report = builder.build();
        // hash1: 1M recoverable, hash2: 500k recoverable
        assert_eq!(report.total_recoverable_bytes, 1_500_000);
    }

    #[test]
    fn test_summary_text() {
        let mut builder = ReportBuilder::new().title("Test");
        builder.add_entries(sample_entries());
        let report = builder.build();
        let text = report.summary_text();
        assert!(text.contains("Test"));
        assert!(text.contains("duplicate groups"));
    }

    #[test]
    fn test_format_breakdown() {
        let mut builder = ReportBuilder::new();
        builder.add_entries(sample_entries());
        let report = builder.build();
        let breakdown = report.format_breakdown();
        assert!(breakdown.by_extension.contains_key("mp4"));
        assert!(breakdown.by_extension.contains_key("mov"));
    }

    #[test]
    fn test_most_common_ext() {
        let mut builder = ReportBuilder::new();
        builder.add_entries(sample_entries());
        let report = builder.build();
        let breakdown = report.format_breakdown();
        let (ext, count) = breakdown
            .most_common_ext()
            .expect("operation should succeed");
        // mp4 and mov both have 2 files; either is acceptable
        assert!(count >= 2);
        assert!(ext == "mp4" || ext == "mov");
    }

    #[test]
    fn test_size_distribution() {
        let mut builder = ReportBuilder::new();
        builder.add_entries(sample_entries());
        let report = builder.build();
        let dist = report.size_distribution(&[100_000, 750_000, 2_000_000]);
        assert_eq!(dist.total(), 4); // 4 files in 2 groups
    }

    #[test]
    fn test_filter_by_path() {
        let entries = vec![
            DuplicateEntry::new(PathBuf::from("/archive/a.mp4"), 100, "h1"),
            DuplicateEntry::new(PathBuf::from("/tmp/b.mp4"), 100, "h1"),
        ];
        let mut builder = ReportBuilder::new();
        builder.add_entries(entries);
        let report = builder.build();
        let filtered = report.filter_by_path(Path::new("/archive"));
        assert_eq!(filtered.len(), 1);
    }

    #[test]
    fn test_empty_report() {
        let report = ReportBuilder::new().build();
        assert_eq!(report.group_count(), 0);
        assert_eq!(report.total_duplicate_files, 0);
        assert_eq!(report.total_recoverable_bytes, 0);
    }

    #[test]
    fn test_duplicate_entry_extension() {
        let e = DuplicateEntry::new(PathBuf::from("/foo.MP4"), 0, "x");
        assert_eq!(e.extension, "mp4");
    }
}