ipfrs-storage 0.2.0

Storage backends and block management for IPFRS content-addressed 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
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
//! Multi-stage block deduplication pipeline combining exact-match and content-based deduplication.
//!
//! The pipeline executes three configurable stages in order:
//! 1. **ExactHash** — FNV-1a over the full block; identical bytes → same hash → duplicate.
//! 2. **ChunkHash** — fixed-size CDC chunking followed by per-chunk FNV-1a hashing; detects
//!    near-identical blocks that share a large portion of chunks.
//! 3. **Similarity** — 64-bit SimHash fingerprint (FNV-1a on sliding byte windows) combined
//!    with Hamming distance to detect semantically similar blocks.
//!
//! Each stage is tried in order; the first match short-circuits the remaining stages and records
//! the originating block's CID together with the bytes saved.

use std::collections::HashMap;

// ─────────────────────────────────────────────────────────────────────────────
// Public Types
// ─────────────────────────────────────────────────────────────────────────────

/// Identifies which deduplication stage detected a duplicate.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DedupStage {
    /// Full-block FNV-1a hash equality — byte-for-byte identical data.
    ExactHash,
    /// Per-chunk FNV-1a hash equality after fixed-size CDC splitting.
    ChunkHash,
    /// SimHash / MinHash Hamming-distance similarity.
    Similarity,
}

/// Result produced by [`DeduplicationPipeline::process_block`].
#[derive(Debug, Clone)]
pub struct DedupResult {
    /// Content Identifier of the block that was processed.
    pub cid: String,
    /// `true` when the block was identified as a duplicate of an existing entry.
    pub is_duplicate: bool,
    /// CID of the canonical (first-seen) block if this block is a duplicate.
    pub duplicate_of: Option<String>,
    /// Which pipeline stage detected the duplication.
    pub stage_detected: Option<DedupStage>,
    /// Bytes that can be reclaimed because this block need not be stored again.
    pub space_saved: u64,
}

/// Runtime configuration for [`DeduplicationPipeline`].
#[derive(Debug, Clone)]
pub struct PipelineConfig {
    /// Ordered list of stages to execute.
    pub stages: Vec<DedupStage>,
    /// Target (maximum) chunk size in bytes used by the `ChunkHash` stage.
    pub chunk_size: usize,
    /// Minimum chunk size in bytes; chunks smaller than this are merged into
    /// the preceding chunk instead of being emitted as independent entries.
    pub min_chunk_size: usize,
    /// Hamming-distance threshold (expressed as similarity ∈ [0, 1]) for the
    /// `Similarity` stage.  Two fingerprints are considered similar when
    /// `1 − distance/bits ≥ similarity_threshold`.
    pub similarity_threshold: f64,
    /// Number of significant bits used in the SimHash computation.  Must be
    /// ≤ 64 (clamped internally).
    pub fingerprint_bits: u32,
}

impl Default for PipelineConfig {
    fn default() -> Self {
        Self {
            stages: vec![
                DedupStage::ExactHash,
                DedupStage::ChunkHash,
                DedupStage::Similarity,
            ],
            chunk_size: 4096,
            min_chunk_size: 512,
            similarity_threshold: 0.9,
            fingerprint_bits: 64,
        }
    }
}

/// An indexed block entry stored inside the pipeline.
#[derive(Debug, Clone)]
pub struct BlockEntry {
    /// Content Identifier assigned by the caller.
    pub cid: String,
    /// Raw block payload.
    pub data: Vec<u8>,
    /// FNV-1a hash over the complete payload.
    pub full_hash: u64,
    /// FNV-1a hashes of individual fixed-size chunks.
    pub chunks: Vec<u64>,
    /// 64-bit SimHash fingerprint of the payload.
    pub fingerprint: u64,
    /// Payload length in bytes.
    pub size: u64,
}

/// Accumulated statistics for a running [`DeduplicationPipeline`].
#[derive(Debug, Clone, Default)]
pub struct DedupPipelineStats {
    /// Total number of blocks submitted to the pipeline.
    pub total_processed: u64,
    /// Blocks identified as exact (byte-level) duplicates.
    pub exact_duplicates: u64,
    /// Blocks identified as chunk-level duplicates.
    pub chunk_duplicates: u64,
    /// Blocks identified as similarity duplicates.
    pub similarity_duplicates: u64,
    /// Cumulative bytes saved (not re-stored) across all duplicate detections.
    pub bytes_saved: u64,
    /// Blocks that passed through all stages and were stored as originals.
    pub unique_blocks: u64,
}

// ─────────────────────────────────────────────────────────────────────────────
// Pipeline
// ─────────────────────────────────────────────────────────────────────────────

/// Multi-stage block deduplication pipeline.
///
/// # Example
/// ```rust
/// use ipfrs_storage::deduplication_pipeline::{DeduplicationPipeline, PipelineConfig, DedupStage};
///
/// let config = PipelineConfig::default();
/// let mut pipeline = DeduplicationPipeline::new(config);
///
/// let result = pipeline.process_block("cid-1", b"hello world");
/// assert!(!result.is_duplicate);
///
/// let result2 = pipeline.process_block("cid-2", b"hello world");
/// assert!(result2.is_duplicate);
/// ```
pub struct DeduplicationPipeline {
    config: PipelineConfig,
    /// full_hash → first-seen CID
    index: HashMap<u64, String>,
    /// chunk_hash → first-seen CID
    chunk_index: HashMap<u64, String>,
    /// (fingerprint, cid) pairs for similarity search
    fingerprints: Vec<(u64, String)>,
    stats: DedupPipelineStats,
}

impl DeduplicationPipeline {
    // ─────────────────────────────────────────────────────────────────────────
    // Construction
    // ─────────────────────────────────────────────────────────────────────────

    /// Create a new pipeline with the given [`PipelineConfig`].
    pub fn new(config: PipelineConfig) -> Self {
        Self {
            config,
            index: HashMap::new(),
            chunk_index: HashMap::new(),
            fingerprints: Vec::new(),
            stats: DedupPipelineStats::default(),
        }
    }

    // ─────────────────────────────────────────────────────────────────────────
    // Core processing
    // ─────────────────────────────────────────────────────────────────────────

    /// Submit a block to the pipeline.
    ///
    /// The block is run through the configured stages in order.  The first
    /// stage that reports a match short-circuits the remaining stages and
    /// returns a [`DedupResult`] with `is_duplicate = true`.  If no stage
    /// matches, the block is indexed and returned as unique.
    pub fn process_block(&mut self, cid: &str, data: &[u8]) -> DedupResult {
        self.stats.total_processed += 1;

        let full_hash = Self::compute_full_hash(data);
        let bits = self.config.fingerprint_bits.min(64);

        // Run each configured stage in order.
        for stage in self.config.stages.clone() {
            match stage {
                DedupStage::ExactHash => {
                    if let Some(original_cid) = self.index.get(&full_hash).cloned() {
                        let saved = data.len() as u64;
                        self.stats.exact_duplicates += 1;
                        self.stats.bytes_saved += saved;
                        return DedupResult {
                            cid: cid.to_string(),
                            is_duplicate: true,
                            duplicate_of: Some(original_cid),
                            stage_detected: Some(DedupStage::ExactHash),
                            space_saved: saved,
                        };
                    }
                }

                DedupStage::ChunkHash => {
                    let chunks = Self::compute_chunks(
                        data,
                        self.config.chunk_size,
                        self.config.min_chunk_size,
                    );
                    if let Some(original_cid) = self.find_chunk_duplicate(&chunks) {
                        let saved = data.len() as u64;
                        self.stats.chunk_duplicates += 1;
                        self.stats.bytes_saved += saved;
                        // Still index this block's chunks so future queries benefit.
                        self.index_chunks(&chunks, cid);
                        return DedupResult {
                            cid: cid.to_string(),
                            is_duplicate: true,
                            duplicate_of: Some(original_cid),
                            stage_detected: Some(DedupStage::ChunkHash),
                            space_saved: saved,
                        };
                    }
                    // Not a duplicate at this stage — index chunks for future lookups.
                    self.index_chunks(&chunks, cid);
                }

                DedupStage::Similarity => {
                    let fingerprint = Self::compute_simhash(data, bits);
                    let threshold = self.config.similarity_threshold;
                    if let Some(original_cid) = self
                        .check_similarity(fingerprint, threshold)
                        .map(str::to_string)
                    {
                        let saved = data.len() as u64;
                        self.stats.similarity_duplicates += 1;
                        self.stats.bytes_saved += saved;
                        // Index the fingerprint even for duplicates for richer future queries.
                        self.fingerprints.push((fingerprint, cid.to_string()));
                        return DedupResult {
                            cid: cid.to_string(),
                            is_duplicate: true,
                            duplicate_of: Some(original_cid),
                            stage_detected: Some(DedupStage::Similarity),
                            space_saved: saved,
                        };
                    }
                    self.fingerprints.push((fingerprint, cid.to_string()));
                }
            }
        }

        // Block is unique — record in primary index.
        self.index.insert(full_hash, cid.to_string());
        self.stats.unique_blocks += 1;

        DedupResult {
            cid: cid.to_string(),
            is_duplicate: false,
            duplicate_of: None,
            stage_detected: None,
            space_saved: 0,
        }
    }

    // ─────────────────────────────────────────────────────────────────────────
    // Hashing primitives
    // ─────────────────────────────────────────────────────────────────────────

    /// Compute a 64-bit FNV-1a hash over the entire `data` slice.
    pub fn compute_full_hash(data: &[u8]) -> u64 {
        fnv1a_64(data)
    }

    /// Split `data` into fixed-size chunks and return the FNV-1a hash of each.
    ///
    /// Chunks smaller than `min_size` are merged into the preceding chunk.
    /// If `data` is empty, an empty `Vec` is returned.
    pub fn compute_chunks(data: &[u8], chunk_size: usize, min_size: usize) -> Vec<u64> {
        if data.is_empty() {
            return Vec::new();
        }

        // Guard against degenerate configurations.
        let effective_chunk = chunk_size.max(1);
        let effective_min = min_size.min(effective_chunk);

        let mut hashes: Vec<u64> = Vec::new();
        let mut offset = 0usize;

        while offset < data.len() {
            let remaining = data.len() - offset;

            // If the remaining bytes are less than min_size, merge into the
            // previous chunk (or emit as the first/only chunk).
            if remaining < effective_min && !hashes.is_empty() {
                // Re-hash last chunk + tail together.
                let tail_start = offset.saturating_sub(effective_chunk);
                let merged_hash = fnv1a_64(&data[tail_start..]);
                if let Some(last) = hashes.last_mut() {
                    *last = merged_hash;
                }
                break;
            }

            let end = (offset + effective_chunk).min(data.len());
            hashes.push(fnv1a_64(&data[offset..end]));
            offset = end;
        }

        hashes
    }

    /// Compute a `bits`-wide SimHash fingerprint of `data`.
    ///
    /// Uses FNV-1a on sliding 8-byte (or smaller near the end) windows to
    /// build feature hashes, then accumulates a weighted bit-vector which is
    /// thresholded into the final fingerprint.
    pub fn compute_simhash(data: &[u8], bits: u32) -> u64 {
        let bits = bits.min(64) as usize;
        if data.is_empty() || bits == 0 {
            return 0;
        }

        // bit_weights[i] accumulates the signed weight for bit position i.
        let mut bit_weights = vec![0i64; bits];
        let window = 8usize;

        // Slide a window across `data`, computing FNV-1a for each window.
        let num_windows = data.len().saturating_sub(window).saturating_add(1);
        let actual_windows = num_windows.max(1);

        for start in 0..actual_windows {
            let end = (start + window).min(data.len());
            let h = fnv1a_64(&data[start..end]);

            // For each bit position, update the weight based on whether the
            // corresponding bit in `h` is set.
            for (bit, weight) in bit_weights.iter_mut().enumerate().take(bits) {
                if (h >> bit) & 1 == 1 {
                    *weight += 1;
                } else {
                    *weight -= 1;
                }
            }
        }

        // Build the fingerprint: bit i is 1 when weight[i] > 0.
        let mut fingerprint = 0u64;
        for (bit, &weight) in bit_weights.iter().enumerate().take(bits) {
            if weight > 0 {
                fingerprint |= 1u64 << bit;
            }
        }

        fingerprint
    }

    /// Compute the Hamming distance between two 64-bit values (number of
    /// differing bits via `popcount` of XOR).
    #[inline]
    pub fn hamming_distance(a: u64, b: u64) -> u32 {
        (a ^ b).count_ones()
    }

    /// Derive a similarity score in [0, 1] from a Hamming distance.
    ///
    /// `similarity = 1 − distance / bits`
    ///
    /// Returns 0.0 when `bits == 0` to avoid division by zero.
    #[inline]
    pub fn similarity_from_hamming(distance: u32, bits: u32) -> f64 {
        if bits == 0 {
            return 0.0;
        }
        1.0 - (distance as f64 / bits as f64)
    }

    /// Search the fingerprint index for any stored fingerprint that has a
    /// similarity ≥ `threshold` with `fingerprint`.
    ///
    /// Returns the CID of the first match found, or `None`.
    pub fn check_similarity(&self, fingerprint: u64, threshold: f64) -> Option<&str> {
        let bits = self.config.fingerprint_bits.min(64);
        for (stored_fp, stored_cid) in &self.fingerprints {
            let distance = Self::hamming_distance(fingerprint, *stored_fp);
            let similarity = Self::similarity_from_hamming(distance, bits);
            if similarity >= threshold {
                return Some(stored_cid.as_str());
            }
        }
        None
    }

    // ─────────────────────────────────────────────────────────────────────────
    // Index management
    // ─────────────────────────────────────────────────────────────────────────

    /// Remove all index entries associated with `cid`.
    ///
    /// Returns `true` if at least one entry was removed.
    pub fn remove_block(&mut self, cid: &str) -> bool {
        let mut removed = false;

        // Remove from primary (full-hash) index.
        self.index.retain(|_, v| {
            if v.as_str() == cid {
                removed = true;
                false
            } else {
                true
            }
        });

        // Remove from chunk index.
        self.chunk_index.retain(|_, v| {
            if v.as_str() == cid {
                removed = true;
                false
            } else {
                true
            }
        });

        // Remove from fingerprint list.
        let before = self.fingerprints.len();
        self.fingerprints.retain(|(_, c)| c.as_str() != cid);
        if self.fingerprints.len() < before {
            removed = true;
        }

        removed
    }

    /// Return the number of unique full-hash entries in the primary index.
    pub fn index_size(&self) -> usize {
        self.index.len()
    }

    /// Return a reference to the accumulated pipeline statistics.
    pub fn stats(&self) -> &DedupPipelineStats {
        &self.stats
    }

    // ─────────────────────────────────────────────────────────────────────────
    // Internal helpers
    // ─────────────────────────────────────────────────────────────────────────

    /// Check whether any chunk hash in `chunks` already appears in the chunk
    /// index, indicating a previously stored block shares that chunk.
    fn find_chunk_duplicate(&self, chunks: &[u64]) -> Option<String> {
        for &ch in chunks {
            if let Some(original_cid) = self.chunk_index.get(&ch) {
                return Some(original_cid.clone());
            }
        }
        None
    }

    /// Insert all chunk hashes from `chunks` into the chunk index, mapping to
    /// `cid`.  Existing mappings are not overwritten to preserve the canonical
    /// (first-seen) CID.
    fn index_chunks(&mut self, chunks: &[u64], cid: &str) {
        for &ch in chunks {
            self.chunk_index
                .entry(ch)
                .or_insert_with(|| cid.to_string());
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// FNV-1a 64-bit implementation
// ─────────────────────────────────────────────────────────────────────────────

/// 64-bit FNV-1a hash of an arbitrary byte slice.
#[inline]
fn fnv1a_64(data: &[u8]) -> u64 {
    const OFFSET_BASIS: u64 = 14695981039346656037;
    const PRIME: u64 = 1099511628211;
    let mut hash = OFFSET_BASIS;
    for &byte in data {
        hash ^= byte as u64;
        hash = hash.wrapping_mul(PRIME);
    }
    hash
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

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

    // ── helpers ───────────────────────────────────────────────────────────────

    fn default_pipeline() -> DeduplicationPipeline {
        DeduplicationPipeline::new(PipelineConfig::default())
    }

    fn exact_only_pipeline() -> DeduplicationPipeline {
        DeduplicationPipeline::new(PipelineConfig {
            stages: vec![DedupStage::ExactHash],
            ..PipelineConfig::default()
        })
    }

    fn chunk_only_pipeline() -> DeduplicationPipeline {
        DeduplicationPipeline::new(PipelineConfig {
            stages: vec![DedupStage::ChunkHash],
            ..PipelineConfig::default()
        })
    }

    fn similarity_only_pipeline() -> DeduplicationPipeline {
        DeduplicationPipeline::new(PipelineConfig {
            stages: vec![DedupStage::Similarity],
            similarity_threshold: 0.8,
            ..PipelineConfig::default()
        })
    }

    // ── 1. Unique block accepted ───────────────────────────────────────────────

    #[test]
    fn test_unique_block_accepted() {
        let mut p = default_pipeline();
        let r = p.process_block("cid-1", b"unique data block");
        assert!(!r.is_duplicate);
        assert!(r.duplicate_of.is_none());
        assert!(r.stage_detected.is_none());
        assert_eq!(r.space_saved, 0);
        assert_eq!(r.cid, "cid-1");
    }

    // ── 2. Exact duplicate detected ───────────────────────────────────────────

    #[test]
    fn test_exact_duplicate_detected() {
        let mut p = exact_only_pipeline();
        p.process_block("cid-1", b"identical block data");
        let r = p.process_block("cid-2", b"identical block data");
        assert!(r.is_duplicate);
        assert_eq!(r.duplicate_of.as_deref(), Some("cid-1"));
        assert_eq!(r.stage_detected, Some(DedupStage::ExactHash));
        assert_eq!(r.space_saved, b"identical block data".len() as u64);
    }

    // ── 3. Different blocks are not exact duplicates ──────────────────────────

    #[test]
    fn test_different_blocks_not_exact_duplicate() {
        let mut p = exact_only_pipeline();
        p.process_block("cid-1", b"block A");
        let r = p.process_block("cid-2", b"block B");
        assert!(!r.is_duplicate);
    }

    // ── 4. Three-way exact duplicate chain ────────────────────────────────────

    #[test]
    fn test_three_way_exact_duplicate() {
        let mut p = exact_only_pipeline();
        p.process_block("cid-1", b"shared data");
        let r2 = p.process_block("cid-2", b"shared data");
        let r3 = p.process_block("cid-3", b"shared data");
        assert!(r2.is_duplicate);
        assert!(r3.is_duplicate);
        // Both duplicates point to the original.
        assert_eq!(r2.duplicate_of.as_deref(), Some("cid-1"));
        assert_eq!(r3.duplicate_of.as_deref(), Some("cid-1"));
    }

    // ── 5. Chunk duplicate detected ───────────────────────────────────────────

    #[test]
    fn test_chunk_duplicate_detected() {
        let mut p = chunk_only_pipeline();
        // Build a block that is larger than chunk_size (4096) so chunking happens.
        let block: Vec<u8> = (0u8..=255).cycle().take(8192).collect();
        p.process_block("cid-1", &block);
        // Submit same block under a new CID.
        let r = p.process_block("cid-2", &block);
        assert!(r.is_duplicate);
        assert_eq!(r.stage_detected, Some(DedupStage::ChunkHash));
    }

    // ── 6. Chunk stage: partial overlap triggers duplicate ────────────────────

    #[test]
    fn test_chunk_partial_overlap() {
        let mut p = chunk_only_pipeline();
        let block_a: Vec<u8> = vec![0xABu8; 8192];
        p.process_block("cid-1", &block_a);

        // Second block shares the first 4096 bytes exactly.
        let mut block_b = block_a[..4096].to_vec();
        block_b.extend_from_slice(&[0xCDu8; 4096]);
        let r = p.process_block("cid-2", &block_b);
        assert!(r.is_duplicate);
        assert_eq!(r.stage_detected, Some(DedupStage::ChunkHash));
        assert_eq!(r.duplicate_of.as_deref(), Some("cid-1"));
    }

    // ── 7. compute_full_hash is deterministic ─────────────────────────────────

    #[test]
    fn test_compute_full_hash_deterministic() {
        let data = b"deterministic hash input";
        let h1 = DeduplicationPipeline::compute_full_hash(data);
        let h2 = DeduplicationPipeline::compute_full_hash(data);
        assert_eq!(h1, h2);
    }

    // ── 8. compute_full_hash differs for different inputs ────────────────────

    #[test]
    fn test_compute_full_hash_differs() {
        let h1 = DeduplicationPipeline::compute_full_hash(b"aaa");
        let h2 = DeduplicationPipeline::compute_full_hash(b"bbb");
        assert_ne!(h1, h2);
    }

    // ── 9. compute_full_hash empty slice ─────────────────────────────────────

    #[test]
    fn test_compute_full_hash_empty() {
        // Should not panic.
        let h = DeduplicationPipeline::compute_full_hash(b"");
        // FNV-1a offset basis for empty input.
        assert_eq!(h, 14695981039346656037u64);
    }

    // ── 10. compute_chunks basic ─────────────────────────────────────────────

    #[test]
    fn test_compute_chunks_basic() {
        let data: Vec<u8> = (0u8..255).collect();
        let chunks = DeduplicationPipeline::compute_chunks(&data, 64, 16);
        // 255 bytes / 64 ≈ 3.98 → at most 4 chunks.
        assert!(!chunks.is_empty());
        assert!(chunks.len() <= 4);
    }

    // ── 11. compute_chunks empty input ───────────────────────────────────────

    #[test]
    fn test_compute_chunks_empty() {
        let chunks = DeduplicationPipeline::compute_chunks(&[], 64, 16);
        assert!(chunks.is_empty());
    }

    // ── 12. compute_chunks single chunk (data smaller than chunk_size) ────────

    #[test]
    fn test_compute_chunks_single_chunk() {
        let data = vec![0x42u8; 100];
        let chunks = DeduplicationPipeline::compute_chunks(&data, 4096, 512);
        assert_eq!(chunks.len(), 1);
    }

    // ── 13. hamming_distance identical values ────────────────────────────────

    #[test]
    fn test_hamming_distance_identical() {
        assert_eq!(
            DeduplicationPipeline::hamming_distance(
                0xDEAD_BEEF_CAFE_BABEu64,
                0xDEAD_BEEF_CAFE_BABEu64
            ),
            0
        );
    }

    // ── 14. hamming_distance all bits differ ─────────────────────────────────

    #[test]
    fn test_hamming_distance_all_bits() {
        assert_eq!(DeduplicationPipeline::hamming_distance(0u64, u64::MAX), 64);
    }

    // ── 15. hamming_distance single bit ──────────────────────────────────────

    #[test]
    fn test_hamming_distance_single_bit() {
        assert_eq!(DeduplicationPipeline::hamming_distance(0u64, 1u64), 1);
        assert_eq!(DeduplicationPipeline::hamming_distance(0u64, 1u64 << 63), 1);
    }

    // ── 16. similarity_from_hamming exact values ──────────────────────────────

    #[test]
    fn test_similarity_from_hamming_exact() {
        // 0 differing bits out of 64 → similarity 1.0.
        let s = DeduplicationPipeline::similarity_from_hamming(0, 64);
        assert!((s - 1.0).abs() < 1e-10);

        // 64 differing bits out of 64 → similarity 0.0.
        let s = DeduplicationPipeline::similarity_from_hamming(64, 64);
        assert!((s - 0.0).abs() < 1e-10);

        // 32 differing bits out of 64 → similarity 0.5.
        let s = DeduplicationPipeline::similarity_from_hamming(32, 64);
        assert!((s - 0.5).abs() < 1e-10);
    }

    // ── 17. similarity_from_hamming zero bits ─────────────────────────────────

    #[test]
    fn test_similarity_from_hamming_zero_bits() {
        let s = DeduplicationPipeline::similarity_from_hamming(0, 0);
        assert_eq!(s, 0.0);
    }

    // ── 18. compute_simhash deterministic ────────────────────────────────────

    #[test]
    fn test_compute_simhash_deterministic() {
        let data = b"simhash test data";
        let f1 = DeduplicationPipeline::compute_simhash(data, 64);
        let f2 = DeduplicationPipeline::compute_simhash(data, 64);
        assert_eq!(f1, f2);
    }

    // ── 19. compute_simhash empty input ──────────────────────────────────────

    #[test]
    fn test_compute_simhash_empty() {
        // Should not panic and should return 0.
        let f = DeduplicationPipeline::compute_simhash(b"", 64);
        assert_eq!(f, 0);
    }

    // ── 20. Similarity duplicate detected ────────────────────────────────────

    #[test]
    fn test_similarity_duplicate_detected() {
        let mut p = similarity_only_pipeline();
        let base: Vec<u8> = (0u8..=200).cycle().take(500).collect();
        p.process_block("cid-1", &base);

        // Submit an identical block; Hamming distance = 0 → similarity = 1.0.
        let r = p.process_block("cid-2", &base);
        assert!(r.is_duplicate);
        assert_eq!(r.stage_detected, Some(DedupStage::Similarity));
        assert_eq!(r.duplicate_of.as_deref(), Some("cid-1"));
    }

    // ── 21. Similarity threshold boundary — below threshold not duplicate ─────

    #[test]
    fn test_similarity_threshold_below_not_duplicate() {
        let mut p = DeduplicationPipeline::new(PipelineConfig {
            stages: vec![DedupStage::Similarity],
            // Very high threshold — only identical fingerprints qualify.
            similarity_threshold: 1.0,
            ..PipelineConfig::default()
        });
        let data_a = b"alpha alpha alpha alpha alpha alpha alpha alpha alpha alpha";
        let data_b = b"beta beta beta beta beta beta beta beta beta beta beta beta";
        p.process_block("cid-1", data_a);
        let r = p.process_block("cid-2", data_b);
        // Very different data almost certainly produces different fingerprints.
        // Even if it somehow matches (unlikely), the test validates pipeline logic.
        // We just verify the pipeline does not panic.
        let _ = r.is_duplicate;
    }

    // ── 22. Multi-stage pipeline: exact takes precedence ─────────────────────

    #[test]
    fn test_multi_stage_exact_precedence() {
        let mut p = default_pipeline();
        let data = b"multi-stage block";
        p.process_block("cid-1", data);
        let r = p.process_block("cid-2", data);
        // Exact match should be detected before chunk or similarity stages.
        assert_eq!(r.stage_detected, Some(DedupStage::ExactHash));
    }

    // ── 23. Stats: total_processed tracked correctly ──────────────────────────

    #[test]
    fn test_stats_total_processed() {
        let mut p = default_pipeline();
        p.process_block("c1", b"a");
        p.process_block("c2", b"b");
        p.process_block("c3", b"c");
        assert_eq!(p.stats().total_processed, 3);
    }

    // ── 24. Stats: exact_duplicates counted ───────────────────────────────────

    #[test]
    fn test_stats_exact_duplicates() {
        let mut p = exact_only_pipeline();
        p.process_block("c1", b"same");
        p.process_block("c2", b"same");
        p.process_block("c3", b"same");
        assert_eq!(p.stats().exact_duplicates, 2);
        assert_eq!(p.stats().unique_blocks, 1);
    }

    // ── 25. Stats: bytes_saved accumulates ────────────────────────────────────

    #[test]
    fn test_stats_bytes_saved() {
        let data = b"12345678901234567890"; // 20 bytes
        let mut p = exact_only_pipeline();
        p.process_block("c1", data);
        p.process_block("c2", data);
        p.process_block("c3", data);
        assert_eq!(p.stats().bytes_saved, 40); // 2 duplicates × 20 bytes each
    }

    // ── 26. Stats: unique_blocks counted ──────────────────────────────────────

    #[test]
    fn test_stats_unique_blocks() {
        let mut p = exact_only_pipeline();
        p.process_block("c1", b"alpha");
        p.process_block("c2", b"beta");
        p.process_block("c3", b"gamma");
        assert_eq!(p.stats().unique_blocks, 3);
        assert_eq!(p.stats().exact_duplicates, 0);
    }

    // ── 27. index_size reflects unique full-hash entries ─────────────────────

    #[test]
    fn test_index_size() {
        let mut p = exact_only_pipeline();
        assert_eq!(p.index_size(), 0);
        p.process_block("c1", b"a");
        p.process_block("c2", b"b");
        assert_eq!(p.index_size(), 2);
        // Duplicate should not grow the index.
        p.process_block("c3", b"a");
        assert_eq!(p.index_size(), 2);
    }

    // ── 28. remove_block removes from primary index ───────────────────────────

    #[test]
    fn test_remove_block_primary_index() {
        let mut p = exact_only_pipeline();
        p.process_block("c1", b"removable data");
        assert_eq!(p.index_size(), 1);
        let removed = p.remove_block("c1");
        assert!(removed);
        assert_eq!(p.index_size(), 0);
    }

    // ── 29. remove_block: non-existent CID returns false ─────────────────────

    #[test]
    fn test_remove_block_not_found() {
        let mut p = default_pipeline();
        p.process_block("c1", b"existing block");
        let removed = p.remove_block("c-nonexistent");
        assert!(!removed);
    }

    // ── 30. remove_block allows re-insertion after removal ───────────────────

    #[test]
    fn test_remove_block_allows_reinsertion() {
        let mut p = exact_only_pipeline();
        let data = b"reinsert me";
        p.process_block("c1", data);
        p.remove_block("c1");

        // After removal the block should be treated as unique again.
        let r = p.process_block("c2", data);
        assert!(!r.is_duplicate);
    }

    // ── 31. All three stages enabled end-to-end ───────────────────────────────

    #[test]
    fn test_all_stages_enabled() {
        let mut p = default_pipeline();

        // First block — unique.
        let r1 = p.process_block("cid-a", b"hello from stage one");
        assert!(!r1.is_duplicate);

        // Exact duplicate of the first block.
        let r2 = p.process_block("cid-b", b"hello from stage one");
        assert!(r2.is_duplicate);
        assert_eq!(r2.stage_detected, Some(DedupStage::ExactHash));

        // Distinct block — unique.
        let r3 = p.process_block("cid-c", b"completely different data");
        assert!(!r3.is_duplicate);

        assert_eq!(p.stats().total_processed, 3);
        assert_eq!(p.stats().unique_blocks, 2);
        assert_eq!(p.stats().exact_duplicates, 1);
    }

    // ── 32. Chunk index populated for unique blocks ───────────────────────────

    #[test]
    fn test_chunk_index_populated() {
        let mut p = chunk_only_pipeline();
        let block: Vec<u8> = vec![0x77u8; 8192];
        let r1 = p.process_block("c1", &block);
        assert!(!r1.is_duplicate);
        // Now submit the same block content under a different CID.
        let r2 = p.process_block("c2", &block);
        assert!(r2.is_duplicate);
    }

    // ── 33. Empty block handled without panic ─────────────────────────────────

    #[test]
    fn test_empty_block_no_panic() {
        let mut p = default_pipeline();
        let r = p.process_block("empty", b"");
        // An empty block should be processable without panic.
        assert_eq!(r.cid, "empty");
    }

    // ── 34. Stats: similarity_duplicates counted ──────────────────────────────

    #[test]
    fn test_stats_similarity_duplicates() {
        let mut p = similarity_only_pipeline();
        let data: Vec<u8> = (0u8..=127).cycle().take(256).collect();
        p.process_block("c1", &data);
        p.process_block("c2", &data); // identical → similarity = 1.0
        assert_eq!(p.stats().similarity_duplicates, 1);
    }

    // ── 35. remove_block removes fingerprint entries ──────────────────────────

    #[test]
    fn test_remove_block_removes_fingerprint() {
        let mut p = similarity_only_pipeline();
        let data: Vec<u8> = (0u8..=200).cycle().take(400).collect();
        p.process_block("c1", &data);
        let removed = p.remove_block("c1");
        assert!(removed);

        // After removal, the same data should be treated as unique.
        let r = p.process_block("c2", &data);
        assert!(!r.is_duplicate);
    }
}