oximedia-archive 0.1.7

Media archive verification and long-term preservation system
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
//! Deduplication reporting for media archives.
//!
//! Provides analysis of an archive's deduplication state: space-savings
//! summaries, per-group duplicate listings, reduction ratios, and formatted
//! text/CSV/JSON report output.
//!
//! This module is intentionally dependency-light and operates on in-memory
//! data structures populated by the caller (e.g., from `dedup_archive`).

#![allow(dead_code)]

use crate::{ArchiveError, ArchiveResult};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt::Write as FmtWrite;
use std::path::PathBuf;

// ---------------------------------------------------------------------------
// Core data types
// ---------------------------------------------------------------------------

/// A group of files that share the same content fingerprint.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DuplicateGroup {
    /// Content fingerprint / digest that all files share.
    pub fingerprint: String,
    /// Size of a single copy in bytes.
    pub file_size: u64,
    /// Paths of all copies (at least 2 for a duplicate group).
    pub paths: Vec<PathBuf>,
}

impl DuplicateGroup {
    /// Create a new duplicate group.
    pub fn new(fingerprint: impl Into<String>, file_size: u64, paths: Vec<PathBuf>) -> Self {
        Self {
            fingerprint: fingerprint.into(),
            file_size,
            paths,
        }
    }

    /// Number of redundant copies (total copies − 1).
    #[must_use]
    pub fn redundant_copies(&self) -> usize {
        self.paths.len().saturating_sub(1)
    }

    /// Bytes that could be freed by keeping only one copy.
    #[must_use]
    pub fn reclaimable_bytes(&self) -> u64 {
        self.file_size
            .saturating_mul(self.redundant_copies() as u64)
    }
}

/// Overall deduplication statistics for an archive.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DedupStats {
    /// Total number of files scanned.
    pub total_files: u64,
    /// Number of unique content fingerprints found.
    pub unique_files: u64,
    /// Number of files that are duplicates (total − unique).
    pub duplicate_files: u64,
    /// Total logical size of all files in bytes (including duplicates).
    pub total_bytes: u64,
    /// Size of the unique set in bytes (sum of one copy per fingerprint).
    pub unique_bytes: u64,
    /// Bytes that could be freed by deduplication.
    pub reclaimable_bytes: u64,
    /// Number of duplicate groups (fingerprints with ≥ 2 copies).
    pub duplicate_groups: u64,
}

impl DedupStats {
    /// Deduplication ratio: logical bytes / unique bytes.
    ///
    /// Returns `1.0` when there is nothing to deduplicate.
    #[must_use]
    pub fn dedup_ratio(&self) -> f64 {
        if self.unique_bytes == 0 {
            return 1.0;
        }
        self.total_bytes as f64 / self.unique_bytes as f64
    }

    /// Space savings as a percentage: `(1 - 1/ratio) * 100`.
    #[must_use]
    pub fn savings_pct(&self) -> f64 {
        let ratio = self.dedup_ratio();
        if ratio <= 1.0 {
            return 0.0;
        }
        (1.0 - 1.0 / ratio) * 100.0
    }
}

// ---------------------------------------------------------------------------
// DedupAnalyzer
// ---------------------------------------------------------------------------

/// Analyses a set of (path, size, fingerprint) records and produces
/// a `DedupReport`.
#[derive(Debug, Default)]
pub struct DedupAnalyzer {
    /// All records ingested: (fingerprint, size, path).
    records: Vec<(String, u64, PathBuf)>,
}

impl DedupAnalyzer {
    /// Create a new empty analyzer.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a file record.
    pub fn add_file(
        &mut self,
        fingerprint: impl Into<String>,
        size_bytes: u64,
        path: impl Into<PathBuf>,
    ) {
        self.records
            .push((fingerprint.into(), size_bytes, path.into()));
    }

    /// Build a `DedupReport` from all added records.
    pub fn build_report(&self) -> DedupReport {
        // Group paths by fingerprint, tracking size per fingerprint.
        let mut groups: HashMap<String, (u64, Vec<PathBuf>)> = HashMap::new();
        for (fp, size, path) in &self.records {
            let entry = groups
                .entry(fp.clone())
                .or_insert_with(|| (*size, Vec::new()));
            entry.1.push(path.clone());
        }

        let total_files = self.records.len() as u64;
        let mut duplicate_groups_vec: Vec<DuplicateGroup> = Vec::new();
        let mut unique_bytes: u64 = 0;
        let mut total_bytes: u64 = 0;
        let mut duplicate_files: u64 = 0;
        let mut duplicate_group_count: u64 = 0;

        for (fp, (size, paths)) in &groups {
            unique_bytes = unique_bytes.saturating_add(*size);
            total_bytes = total_bytes.saturating_add(size.saturating_mul(paths.len() as u64));

            if paths.len() > 1 {
                duplicate_group_count += 1;
                duplicate_files += (paths.len() - 1) as u64;
                duplicate_groups_vec.push(DuplicateGroup::new(fp.clone(), *size, paths.clone()));
            }
        }

        // Sort duplicate groups by reclaimable bytes descending for readability.
        duplicate_groups_vec.sort_by(|a, b| b.reclaimable_bytes().cmp(&a.reclaimable_bytes()));

        let reclaimable_bytes = total_bytes.saturating_sub(unique_bytes);

        let stats = DedupStats {
            total_files,
            unique_files: groups.len() as u64,
            duplicate_files,
            total_bytes,
            unique_bytes,
            reclaimable_bytes,
            duplicate_groups: duplicate_group_count,
        };

        DedupReport {
            stats,
            duplicate_groups: duplicate_groups_vec,
        }
    }
}

// ---------------------------------------------------------------------------
// DedupReport
// ---------------------------------------------------------------------------

/// Complete deduplication report.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DedupReport {
    /// Aggregate statistics.
    pub stats: DedupStats,
    /// Per-group duplicate listings.
    pub duplicate_groups: Vec<DuplicateGroup>,
}

/// Output format for a rendered report.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReportFormat {
    /// Human-readable plain text.
    Text,
    /// JSON (pretty-printed).
    Json,
    /// CSV with one row per duplicate pair.
    Csv,
}

impl DedupReport {
    /// Render this report as a string in the requested format.
    pub fn render(&self, format: ReportFormat) -> ArchiveResult<String> {
        match format {
            ReportFormat::Text => self.render_text(),
            ReportFormat::Json => self.render_json(),
            ReportFormat::Csv => self.render_csv(),
        }
    }

    /// Write the report to a file.
    pub fn write_to_file(&self, path: &std::path::Path, format: ReportFormat) -> ArchiveResult<()> {
        let content = self.render(format)?;
        std::fs::write(path, content.as_bytes()).map_err(ArchiveError::Io)
    }

    fn render_text(&self) -> ArchiveResult<String> {
        let s = &self.stats;
        let mut out = String::new();
        writeln!(out, "=== Archive Deduplication Report ===").ok();
        writeln!(out).ok();
        writeln!(out, "Total files scanned : {}", s.total_files).ok();
        writeln!(out, "Unique fingerprints  : {}", s.unique_files).ok();
        writeln!(out, "Duplicate files      : {}", s.duplicate_files).ok();
        writeln!(out, "Duplicate groups     : {}", s.duplicate_groups).ok();
        writeln!(out).ok();
        writeln!(out, "Total logical size   : {} bytes", s.total_bytes).ok();
        writeln!(out, "Unique set size      : {} bytes", s.unique_bytes).ok();
        writeln!(out, "Reclaimable space    : {} bytes", s.reclaimable_bytes).ok();
        writeln!(out, "Deduplication ratio  : {:.3}x", s.dedup_ratio()).ok();
        writeln!(out, "Space savings        : {:.1}%", s.savings_pct()).ok();

        if !self.duplicate_groups.is_empty() {
            writeln!(out).ok();
            writeln!(out, "--- Duplicate Groups (by reclaimable bytes desc) ---").ok();
            for (i, group) in self.duplicate_groups.iter().enumerate() {
                writeln!(
                    out,
                    "\n[Group {}] fingerprint={} size={} copies={} reclaimable={}",
                    i + 1,
                    group.fingerprint,
                    group.file_size,
                    group.paths.len(),
                    group.reclaimable_bytes()
                )
                .ok();
                for (j, path) in group.paths.iter().enumerate() {
                    let marker = if j == 0 { "keep" } else { "dup " };
                    writeln!(out, "  [{marker}] {}", path.display()).ok();
                }
            }
        }

        Ok(out)
    }

    fn render_json(&self) -> ArchiveResult<String> {
        serde_json::to_string_pretty(self)
            .map_err(|e| ArchiveError::Validation(format!("JSON error: {e}")))
    }

    fn render_csv(&self) -> ArchiveResult<String> {
        let mut out = String::new();
        writeln!(
            out,
            "fingerprint,file_size_bytes,canonical_path,duplicate_path,reclaimable_bytes"
        )
        .ok();
        for group in &self.duplicate_groups {
            let canonical = group
                .paths
                .first()
                .map(|p| p.display().to_string())
                .unwrap_or_default();
            for dup in group.paths.iter().skip(1) {
                writeln!(
                    out,
                    "{},{},{},{},{}",
                    group.fingerprint,
                    group.file_size,
                    csv_escape(&canonical),
                    csv_escape(&dup.display().to_string()),
                    group.file_size,
                )
                .ok();
            }
        }
        Ok(out)
    }
}

/// Escape a string for CSV output (wrap in quotes if it contains commas/quotes).
fn csv_escape(s: &str) -> String {
    if s.contains(',') || s.contains('"') || s.contains('\n') {
        format!("\"{}\"", s.replace('"', "\"\""))
    } else {
        s.to_string()
    }
}

// ---------------------------------------------------------------------------
// Top-level convenience function
// ---------------------------------------------------------------------------

/// Build a dedup report from a slice of `(fingerprint, size, path)` tuples.
pub fn build_report_from_records(records: &[(String, u64, PathBuf)]) -> DedupReport {
    let mut analyzer = DedupAnalyzer::new();
    for (fp, size, path) in records {
        analyzer.add_file(fp.clone(), *size, path.clone());
    }
    analyzer.build_report()
}

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

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

    fn p(s: &str) -> PathBuf {
        PathBuf::from(s)
    }

    // ── DuplicateGroup ─────────────────────────────────────────────────────────

    #[test]
    fn test_duplicate_group_reclaimable_bytes() {
        let group = DuplicateGroup::new(
            "abc123",
            1_000_000,
            vec![p("/a/file.mkv"), p("/b/file.mkv"), p("/c/file.mkv")],
        );
        assert_eq!(group.redundant_copies(), 2);
        assert_eq!(group.reclaimable_bytes(), 2_000_000);
    }

    #[test]
    fn test_duplicate_group_single_copy_no_redundancy() {
        let group = DuplicateGroup::new("abc", 500, vec![p("/only.mp4")]);
        assert_eq!(group.redundant_copies(), 0);
        assert_eq!(group.reclaimable_bytes(), 0);
    }

    // ── DedupStats ─────────────────────────────────────────────────────────────

    #[test]
    fn test_dedup_stats_ratio_and_savings() {
        let stats = DedupStats {
            total_files: 4,
            unique_files: 2,
            duplicate_files: 2,
            total_bytes: 4_000,
            unique_bytes: 2_000,
            reclaimable_bytes: 2_000,
            duplicate_groups: 2,
        };
        assert!((stats.dedup_ratio() - 2.0).abs() < 1e-9);
        assert!((stats.savings_pct() - 50.0).abs() < 1e-6);
    }

    #[test]
    fn test_dedup_stats_no_duplicates() {
        let stats = DedupStats {
            total_files: 3,
            unique_files: 3,
            duplicate_files: 0,
            total_bytes: 3_000,
            unique_bytes: 3_000,
            reclaimable_bytes: 0,
            duplicate_groups: 0,
        };
        assert!((stats.dedup_ratio() - 1.0).abs() < 1e-9);
        assert!(stats.savings_pct() < 1e-9);
    }

    // ── DedupAnalyzer ─────────────────────────────────────────────────────────

    #[test]
    fn test_analyzer_no_duplicates() {
        let mut analyzer = DedupAnalyzer::new();
        analyzer.add_file("fp1", 100, p("/a.mkv"));
        analyzer.add_file("fp2", 200, p("/b.mkv"));
        let report = analyzer.build_report();
        assert_eq!(report.stats.total_files, 2);
        assert_eq!(report.stats.duplicate_files, 0);
        assert!(report.duplicate_groups.is_empty());
    }

    #[test]
    fn test_analyzer_one_duplicate_group() {
        let mut analyzer = DedupAnalyzer::new();
        analyzer.add_file("same_fp", 1_000, p("/copy1.mkv"));
        analyzer.add_file("same_fp", 1_000, p("/copy2.mkv"));
        analyzer.add_file("unique", 500, p("/unique.mkv"));
        let report = analyzer.build_report();
        assert_eq!(report.stats.total_files, 3);
        assert_eq!(report.stats.unique_files, 2);
        assert_eq!(report.stats.duplicate_files, 1);
        assert_eq!(report.stats.duplicate_groups, 1);
        assert_eq!(report.stats.reclaimable_bytes, 1_000);
    }

    #[test]
    fn test_analyzer_multiple_duplicate_groups_sorted() {
        let mut analyzer = DedupAnalyzer::new();
        // group A: small file, 3 copies → 2 reclaimable × 100 = 200
        for i in 0..3 {
            analyzer.add_file("group_a", 100, p(&format!("/a_{i}.mkv")));
        }
        // group B: large file, 2 copies → 1 reclaimable × 10_000 = 10_000
        for i in 0..2 {
            analyzer.add_file("group_b", 10_000, p(&format!("/b_{i}.mkv")));
        }
        let report = analyzer.build_report();
        // Largest reclaimable group should be first
        assert_eq!(report.duplicate_groups[0].fingerprint, "group_b");
        assert_eq!(report.stats.reclaimable_bytes, 10_200);
    }

    // ── DedupReport::render ────────────────────────────────────────────────────

    #[test]
    fn test_render_text_contains_key_fields() {
        let mut analyzer = DedupAnalyzer::new();
        analyzer.add_file("fp", 500, p("/x.mkv"));
        analyzer.add_file("fp", 500, p("/y.mkv"));
        let report = analyzer.build_report();
        let text = report.render(ReportFormat::Text).expect("render");
        assert!(text.contains("Deduplication ratio"));
        assert!(text.contains("Space savings"));
        assert!(text.contains("Duplicate Groups"));
    }

    #[test]
    fn test_render_json_valid() {
        let mut analyzer = DedupAnalyzer::new();
        analyzer.add_file("fp", 100, p("/f.mkv"));
        let report = analyzer.build_report();
        let json = report.render(ReportFormat::Json).expect("render");
        let val: serde_json::Value = serde_json::from_str(&json).expect("valid json");
        assert!(val.get("stats").is_some());
    }

    #[test]
    fn test_render_csv_header_and_rows() {
        let mut analyzer = DedupAnalyzer::new();
        analyzer.add_file("fp1", 1_024, p("/a.mkv"));
        analyzer.add_file("fp1", 1_024, p("/b.mkv"));
        let report = analyzer.build_report();
        let csv = report.render(ReportFormat::Csv).expect("render");
        let lines: Vec<&str> = csv.lines().collect();
        assert!(lines[0].starts_with("fingerprint"));
        assert!(lines.len() >= 2); // header + at least one data row
        assert!(lines[1].contains("fp1"));
    }

    // ── write_to_file ─────────────────────────────────────────────────────────

    #[test]
    fn test_write_report_to_file() {
        let dir = std::env::temp_dir();
        let path = dir.join("oximedia_dedup_report_test.json");
        let _ = std::fs::remove_file(&path);

        let mut analyzer = DedupAnalyzer::new();
        analyzer.add_file("x", 42, p("/file.mkv"));
        let report = analyzer.build_report();
        report
            .write_to_file(&path, ReportFormat::Json)
            .expect("write");

        let content = std::fs::read_to_string(&path).expect("read");
        assert!(!content.is_empty());
        let _ = std::fs::remove_file(&path);
    }

    // ── build_report_from_records ─────────────────────────────────────────────

    #[test]
    fn test_build_report_from_records_convenience() {
        let records = vec![
            ("digest_a".to_string(), 256_u64, p("/a1.wav")),
            ("digest_a".to_string(), 256_u64, p("/a2.wav")),
            ("digest_b".to_string(), 512_u64, p("/b.wav")),
        ];
        let report = build_report_from_records(&records);
        assert_eq!(report.stats.total_files, 3);
        assert_eq!(report.stats.duplicate_groups, 1);
        assert_eq!(report.stats.reclaimable_bytes, 256);
    }

    // ── csv_escape ────────────────────────────────────────────────────────────

    #[test]
    fn test_csv_escape_plain_string() {
        assert_eq!(csv_escape("simple"), "simple");
    }

    #[test]
    fn test_csv_escape_comma() {
        let escaped = csv_escape("a,b");
        assert!(escaped.starts_with('"'));
        assert!(escaped.ends_with('"'));
    }
}