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
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
//! Content-aware block deduplication with variable-length chunking.
//!
//! Implements Content-Defined Chunking (CDC) using a Rabin-fingerprint-style
//! rolling hash based on FNV-1a. Chunk boundaries are detected when the rolling
//! hash of a sliding window satisfies `hash & mask == 0`, producing statistically
//! independent chunk boundaries that average `2^target_bits` bytes apart.
//!
//! # Architecture
//!
//! ```text
//! ┌──────────────┐     chunk_data()      ┌─────────────────────────────┐
//! │  Raw Object  │ ──────────────────>   │  Vec<(ChunkHash, Vec<u8>)>  │
//! └──────────────┘                       └─────────────────────────────┘
//!        │                                          │
//!        │  store_object()                          │ dedup lookup
//!        ▼                                          ▼
//! ┌──────────────────┐              ┌──────────────────────────────────┐
//! │  ObjectManifest  │              │  chunk_store: HashMap<ChunkHash, │
//! │  Vec<ChunkRef>   │              │    (Chunk, Vec<u8>)>             │
//! └──────────────────┘              └──────────────────────────────────┘
//! ```
//!
//! # Example
//!
//! ```rust
//! use ipfrs_storage::block_deduplicator::{BlockDeduplicator, ChunkingConfig};
//!
//! let config = ChunkingConfig::default();
//! let mut dedup = BlockDeduplicator::new(config);
//!
//! let data = b"Hello, World! This is some test data for deduplication.".to_vec();
//! let manifest = dedup.store_object("obj1".to_string(), data.clone()).unwrap();
//! let retrieved = dedup.retrieve_object("obj1").unwrap();
//! assert_eq!(data, retrieved);
//! ```

use std::collections::HashMap;
use std::fmt;
use std::time::{SystemTime, UNIX_EPOCH};

// ─── FNV-1a helpers ──────────────────────────────────────────────────────────

/// FNV-1a 64-bit hash — fast, non-cryptographic, excellent for CDC boundaries.
///
/// Uses the standard FNV prime and offset basis:
/// - Offset basis: 14695981039346656037
/// - Prime: 1099511628211
#[inline]
pub fn fnv1a_64(data: &[u8]) -> u64 {
    let mut h: u64 = 14695981039346656037;
    for &b in data {
        h ^= b as u64;
        h = h.wrapping_mul(1099511628211);
    }
    h
}

/// Rolling hash for CDC: compute hash over a sliding window of bytes.
/// Uses FNV-1a as the window function — cheap to compute, good distribution.
#[inline]
fn rolling_hash(window: &[u8]) -> u64 {
    fnv1a_64(window)
}

/// Current wall-clock time in seconds since UNIX epoch.
/// Falls back to 0 if the system clock is unavailable.
fn unix_timestamp() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0)
}

// ─── ChunkHash ───────────────────────────────────────────────────────────────

/// A compact 8-byte content hash derived from FNV-1a over chunk data.
///
/// Stored as little-endian bytes. Displayed as 16 lowercase hex characters.
#[derive(Hash, Eq, PartialEq, Clone, Copy, Debug)]
pub struct ChunkHash([u8; 8]);

impl ChunkHash {
    /// Create a `ChunkHash` directly from raw bytes.
    #[inline]
    pub fn from_bytes(bytes: [u8; 8]) -> Self {
        Self(bytes)
    }

    /// Access the raw bytes.
    #[inline]
    pub fn as_bytes(&self) -> &[u8; 8] {
        &self.0
    }

    /// Convert to `u64` (little-endian interpretation).
    #[inline]
    pub fn to_u64(self) -> u64 {
        u64::from_le_bytes(self.0)
    }
}

impl From<u64> for ChunkHash {
    #[inline]
    fn from(v: u64) -> Self {
        Self(v.to_le_bytes())
    }
}

impl fmt::Display for ChunkHash {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for b in &self.0 {
            write!(f, "{:02x}", b)?;
        }
        Ok(())
    }
}

// ─── Chunk ───────────────────────────────────────────────────────────────────

/// Metadata record for a single stored chunk.
///
/// Note: in `lib.rs` this is re-exported as `BddChunk` to avoid collision
/// with `chunk_manager::Chunk`.
#[derive(Debug, Clone)]
pub struct Chunk {
    /// Content-defined hash of this chunk's raw bytes.
    pub hash: ChunkHash,
    /// Byte offset of this chunk's *first occurrence* in the original object.
    pub offset: u64,
    /// Length in bytes of the raw (uncompressed) chunk data.
    pub length: usize,
    /// Number of live references to this chunk across all stored objects.
    pub ref_count: u32,
    /// Whether the stored bytes in the chunk store are compressed.
    pub compressed: bool,
}

// ─── DeduplicationStats ──────────────────────────────────────────────────────

/// Aggregate statistics for the [`BlockDeduplicator`].
///
/// Note: re-exported as `BddDeduplicationStats` in `lib.rs`.
#[derive(Debug, Clone)]
pub struct DeduplicationStats {
    /// Total logical chunk references across all live objects (including duplicates).
    pub total_chunks: u64,
    /// Number of physically distinct chunks stored on disk.
    pub unique_chunks: u64,
    /// Logical references that point to already-stored chunks (`total - unique`).
    pub duplicate_chunks: u64,
    /// Total raw bytes ingested via `store_object` calls.
    pub bytes_before: u64,
    /// Physical bytes currently occupying the chunk store.
    pub bytes_after: u64,
    /// Fraction of bytes saved by deduplication: `1 - (bytes_after / bytes_before)`.
    /// Range `[0.0, 1.0]`; higher is better.
    pub dedup_ratio: f64,
    /// Ratio `bytes_before / bytes_after`. Values > 1.0 mean space was saved.
    pub compression_ratio: f64,
}

// ─── ChunkingConfig ──────────────────────────────────────────────────────────

/// Configuration for the Content-Defined Chunking (CDC) algorithm.
///
/// Note: re-exported as `BddChunkingConfig` in `lib.rs` to avoid collision
/// with `dedup::ChunkingConfig`.
#[derive(Debug, Clone)]
pub struct ChunkingConfig {
    /// Minimum chunk size in bytes. Boundaries are not checked below this threshold.
    pub min_chunk_size: usize,
    /// Maximum chunk size in bytes. A boundary is forced if the current chunk
    /// reaches this length without a natural hash-based split.
    pub max_chunk_size: usize,
    /// Controls the average target chunk size: avg ≈ `2^target_bits` bytes.
    /// Default 13 → ~8 KiB average.
    pub target_bits: u8,
    /// Sliding window width used for rolling hash computation.
    pub window_size: usize,
    /// Whether to attempt compression on stored chunk data (currently marks metadata only).
    pub enable_compression: bool,
}

impl Default for ChunkingConfig {
    fn default() -> Self {
        Self {
            min_chunk_size: 2048,
            max_chunk_size: 65536,
            target_bits: 13,
            window_size: 48,
            enable_compression: false,
        }
    }
}

impl ChunkingConfig {
    /// Returns the rolling-hash boundary mask: `(1 << target_bits) - 1`.
    #[inline]
    pub fn boundary_mask(&self) -> u64 {
        (1u64 << self.target_bits).wrapping_sub(1)
    }
}

// ─── ChunkRef ────────────────────────────────────────────────────────────────

/// Reference to a chunk within an object manifest.
///
/// Describes one logical slice of the original object, pointing to the
/// physical chunk in the chunk store by hash.
#[derive(Debug, Clone)]
pub struct ChunkRef {
    /// Hash of the referenced chunk.
    pub hash: ChunkHash,
    /// Byte offset of this chunk's data within the reconstructed object.
    pub offset_in_object: u64,
    /// Length of this chunk's data in bytes.
    pub chunk_length: usize,
}

// ─── ObjectManifest ──────────────────────────────────────────────────────────

/// Ordered list of chunk references that fully describe a stored object.
///
/// Reassembling all chunks in order reconstructs the original byte sequence.
#[derive(Debug, Clone)]
pub struct ObjectManifest {
    /// Unique identifier for this object.
    pub object_id: String,
    /// Total uncompressed size of the object in bytes.
    pub total_size: u64,
    /// Ordered sequence of chunk references.
    pub chunks: Vec<ChunkRef>,
    /// UNIX timestamp (seconds) when this manifest was created.
    pub created_at: u64,
}

// ─── DeduplicatorError ───────────────────────────────────────────────────────

/// Errors that may arise from [`BlockDeduplicator`] operations.
#[derive(Debug, thiserror::Error)]
pub enum DeduplicatorError {
    /// The requested object ID does not exist in the manifest store.
    #[error("object not found: {0}")]
    ObjectNotFound(String),

    /// The chunk identified by the given hash is not present in the store.
    #[error("chunk not found: {0}")]
    ChunkNotFound(ChunkHash),

    /// A compression or decompression operation failed.
    #[error("compression failed: {0}")]
    CompressionFailed(String),

    /// The manifest data is structurally invalid or inconsistent.
    #[error("invalid manifest: {0}")]
    InvalidManifest(String),

    /// The chunk store has reached its capacity limit.
    #[error("storage full")]
    StorageFull,
}

// ─── BlockDeduplicator ───────────────────────────────────────────────────────

/// Content-aware block deduplicator using variable-length CDC chunking.
///
/// Stores objects by splitting them into content-defined chunks, using the
/// chunk hash as the storage key. Identical chunks are stored only once and
/// reference-counted. When all objects referencing a chunk are deleted, the
/// chunk data is freed.
///
/// # Algorithm
///
/// Content-Defined Chunking (CDC) uses a rolling hash over a sliding window:
/// - For each byte position after `min_chunk_size` into the current chunk,
///   compute `rolling_hash(data[pos-W..pos])` where `W = window_size`.
/// - If `hash & mask == 0` (where `mask = 2^target_bits - 1`), emit a boundary.
/// - Also emit a boundary when `chunk_length >= max_chunk_size`.
/// - The final remaining bytes are always emitted as the last chunk.
///
/// # Thread safety
///
/// `BlockDeduplicator` is not `Sync`. Use external locking (e.g. `Mutex`) when
/// sharing across threads.
pub struct BlockDeduplicator {
    config: ChunkingConfig,
    /// Primary storage: maps chunk hash → (metadata, raw bytes).
    chunk_store: HashMap<ChunkHash, (Chunk, Vec<u8>)>,
    /// Object manifests: maps object ID → ordered list of chunk refs.
    manifests: HashMap<String, ObjectManifest>,
    /// Cumulative raw bytes passed to `store_object` (denominator for dedup ratio).
    total_bytes_stored: u64,
    /// Bytes that were deduplicated (i.e., stored in already-present chunks).
    total_bytes_deduplicated: u64,
}

impl BlockDeduplicator {
    /// Create a new `BlockDeduplicator` with the given configuration.
    pub fn new(config: ChunkingConfig) -> Self {
        Self {
            config,
            chunk_store: HashMap::new(),
            manifests: HashMap::new(),
            total_bytes_stored: 0,
            total_bytes_deduplicated: 0,
        }
    }

    /// Create a `BlockDeduplicator` with default configuration.
    pub fn with_defaults() -> Self {
        Self::new(ChunkingConfig::default())
    }

    /// Split `data` into content-defined chunks using the rolling-hash CDC algorithm.
    ///
    /// Returns an ordered list of `(hash, chunk_bytes)` pairs. The concatenation
    /// of all `chunk_bytes` is guaranteed to equal `data` exactly.
    ///
    /// # Edge cases
    /// - Empty input → returns an empty `Vec`.
    /// - Input shorter than `min_chunk_size` → returns a single chunk.
    /// - Input shorter than `window_size` → rolling hash uses the full available prefix.
    pub fn chunk_data(&self, data: &[u8]) -> Vec<(ChunkHash, Vec<u8>)> {
        if data.is_empty() {
            return Vec::new();
        }

        let min_sz = self.config.min_chunk_size;
        let max_sz = self.config.max_chunk_size;
        let win = self.config.window_size;
        let mask = self.config.boundary_mask();

        let mut result = Vec::new();
        let mut chunk_start = 0usize;

        let mut pos = 0usize;
        while pos < data.len() {
            let chunk_len = pos - chunk_start;

            // We need at least min_chunk_size bytes before we check for a boundary.
            if chunk_len >= min_sz {
                // Determine the window: use min(win, pos) bytes ending at `pos`.
                let win_start = pos.saturating_sub(win);
                let window = &data[win_start..pos];
                let h = rolling_hash(window);

                let is_hash_boundary = (h & mask) == 0;
                let is_max_boundary = chunk_len >= max_sz;

                if is_hash_boundary || is_max_boundary {
                    // Emit chunk [chunk_start..pos]
                    let chunk_bytes = data[chunk_start..pos].to_vec();
                    let hash = ChunkHash::from(fnv1a_64(&chunk_bytes));
                    result.push((hash, chunk_bytes));
                    chunk_start = pos;
                }
            }

            pos += 1;
        }

        // Emit the final (possibly only) chunk — always non-empty since data is non-empty.
        if chunk_start < data.len() {
            let chunk_bytes = data[chunk_start..].to_vec();
            let hash = ChunkHash::from(fnv1a_64(&chunk_bytes));
            result.push((hash, chunk_bytes));
        }

        result
    }

    /// Store an object, deduplicating its content-defined chunks.
    ///
    /// If an identical chunk (same hash) already exists, its `ref_count` is
    /// incremented and the duplicate bytes are not stored again. A manifest is
    /// built and stored internally.
    ///
    /// # Errors
    ///
    /// Currently infallible, but returns `Result` for future extensibility
    /// (e.g., when storage-full limits are enforced).
    pub fn store_object(
        &mut self,
        object_id: String,
        data: Vec<u8>,
    ) -> Result<ObjectManifest, DeduplicatorError> {
        let total_size = data.len() as u64;
        self.total_bytes_stored += total_size;

        let chunks = self.chunk_data(&data);
        let mut chunk_refs = Vec::with_capacity(chunks.len());
        let mut offset_in_object: u64 = 0;
        let mut chunk_offset_in_store: u64 = 0;

        for (hash, chunk_bytes) in chunks {
            let chunk_len = chunk_bytes.len();

            if let Some((existing, _)) = self.chunk_store.get_mut(&hash) {
                // Duplicate: increment reference count.
                existing.ref_count = existing.ref_count.saturating_add(1);
                self.total_bytes_deduplicated += chunk_len as u64;
            } else {
                // New unique chunk: store it.
                let chunk = Chunk {
                    hash,
                    offset: chunk_offset_in_store,
                    length: chunk_len,
                    ref_count: 1,
                    compressed: false,
                };
                self.chunk_store.insert(hash, (chunk, chunk_bytes));
                chunk_offset_in_store += chunk_len as u64;
            }

            chunk_refs.push(ChunkRef {
                hash,
                offset_in_object,
                chunk_length: chunk_len,
            });

            offset_in_object += chunk_len as u64;
        }

        let manifest = ObjectManifest {
            object_id: object_id.clone(),
            total_size,
            chunks: chunk_refs,
            created_at: unix_timestamp(),
        };

        self.manifests.insert(object_id, manifest.clone());
        Ok(manifest)
    }

    /// Retrieve the full byte content of a stored object by reconstructing
    /// it from its chunk manifest.
    ///
    /// # Errors
    ///
    /// - [`DeduplicatorError::ObjectNotFound`] if `object_id` has no manifest.
    /// - [`DeduplicatorError::ChunkNotFound`] if a chunk referenced by the manifest
    ///   is missing from the store (indicates data corruption).
    /// - [`DeduplicatorError::InvalidManifest`] if the reconstructed size does not
    ///   match the manifest's declared `total_size`.
    pub fn retrieve_object(&self, object_id: &str) -> Result<Vec<u8>, DeduplicatorError> {
        let manifest = self
            .manifests
            .get(object_id)
            .ok_or_else(|| DeduplicatorError::ObjectNotFound(object_id.to_string()))?;

        let mut result = Vec::with_capacity(manifest.total_size as usize);

        for chunk_ref in &manifest.chunks {
            let (_, data) = self
                .chunk_store
                .get(&chunk_ref.hash)
                .ok_or(DeduplicatorError::ChunkNotFound(chunk_ref.hash))?;
            result.extend_from_slice(data);
        }

        if result.len() as u64 != manifest.total_size {
            return Err(DeduplicatorError::InvalidManifest(format!(
                "object '{}': expected {} bytes, reconstructed {} bytes",
                object_id,
                manifest.total_size,
                result.len()
            )));
        }

        Ok(result)
    }

    /// Delete a stored object, decrementing ref counts of its chunks.
    ///
    /// Any chunk whose `ref_count` reaches zero is removed from the store.
    /// Returns the hashes of all physically removed chunks.
    ///
    /// # Errors
    ///
    /// - [`DeduplicatorError::ObjectNotFound`] if `object_id` has no manifest.
    pub fn delete_object(&mut self, object_id: &str) -> Result<Vec<ChunkHash>, DeduplicatorError> {
        let manifest = self
            .manifests
            .remove(object_id)
            .ok_or_else(|| DeduplicatorError::ObjectNotFound(object_id.to_string()))?;

        let mut removed = Vec::new();

        for chunk_ref in &manifest.chunks {
            if let Some((meta, _)) = self.chunk_store.get_mut(&chunk_ref.hash) {
                meta.ref_count = meta.ref_count.saturating_sub(1);
                if meta.ref_count == 0 {
                    self.chunk_store.remove(&chunk_ref.hash);
                    removed.push(chunk_ref.hash);
                }
            }
        }

        Ok(removed)
    }

    /// Return a reference to the `Chunk` metadata for the given hash.
    ///
    /// # Errors
    ///
    /// - [`DeduplicatorError::ChunkNotFound`] if no chunk with that hash exists.
    pub fn get_chunk(&self, hash: &ChunkHash) -> Result<&Chunk, DeduplicatorError> {
        self.chunk_store
            .get(hash)
            .map(|(meta, _)| meta)
            .ok_or(DeduplicatorError::ChunkNotFound(*hash))
    }

    /// Return `true` if a chunk with the given hash is present in the store.
    pub fn chunk_exists(&self, hash: &ChunkHash) -> bool {
        self.chunk_store.contains_key(hash)
    }

    /// Remove all chunks whose `ref_count == 0` from the store.
    ///
    /// This is a garbage-collection pass for chunks that were decremented to
    /// zero during earlier `delete_object` calls but not yet physically removed
    /// (e.g., if `delete_object` was called but removal was deferred). In the
    /// current implementation, `delete_object` removes zero-ref chunks eagerly,
    /// so this acts as a safety pass.
    ///
    /// Returns the number of chunks removed.
    pub fn compact(&mut self) -> usize {
        let before = self.chunk_store.len();
        self.chunk_store.retain(|_, (meta, _)| meta.ref_count > 0);
        before - self.chunk_store.len()
    }

    /// Compute and return a snapshot of current deduplication statistics.
    pub fn stats(&self) -> DeduplicationStats {
        let unique_chunks = self.chunk_store.len() as u64;
        let total_chunks: u64 = self
            .chunk_store
            .values()
            .map(|(meta, _)| meta.ref_count as u64)
            .sum();
        let duplicate_chunks = total_chunks.saturating_sub(unique_chunks);

        let bytes_after: u64 = self
            .chunk_store
            .values()
            .map(|(_, data)| data.len() as u64)
            .sum();

        let bytes_before = self.total_bytes_stored;

        let dedup_ratio = if bytes_before > 0 {
            1.0 - (bytes_after as f64 / bytes_before as f64)
        } else {
            0.0
        };

        let compression_ratio = bytes_before as f64 / bytes_after.max(1) as f64;

        DeduplicationStats {
            total_chunks,
            unique_chunks,
            duplicate_chunks,
            bytes_before,
            bytes_after,
            dedup_ratio,
            compression_ratio,
        }
    }

    /// Return a sorted list of all stored object IDs.
    pub fn list_objects(&self) -> Vec<String> {
        let mut ids: Vec<String> = self.manifests.keys().cloned().collect();
        ids.sort();
        ids
    }

    /// Return the number of unique chunks currently in the store.
    pub fn chunk_count(&self) -> usize {
        self.chunk_store.len()
    }

    /// Return the number of objects currently stored.
    pub fn object_count(&self) -> usize {
        self.manifests.len()
    }

    /// Retrieve the manifest for a stored object without reconstructing data.
    pub fn get_manifest(&self, object_id: &str) -> Option<&ObjectManifest> {
        self.manifests.get(object_id)
    }

    /// Return a reference to the current chunking configuration.
    pub fn config(&self) -> &ChunkingConfig {
        &self.config
    }
}

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

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

    // ── xorshift64 PRNG for deterministic test data (no rand crate) ─────────

    fn xorshift64(state: &mut u64) -> u64 {
        let mut x = *state;
        x ^= x << 13;
        x ^= x >> 7;
        x ^= x << 17;
        *state = x;
        x
    }

    fn gen_bytes(seed: u64, len: usize) -> Vec<u8> {
        let mut state = seed;
        let mut out = Vec::with_capacity(len);
        while out.len() < len {
            let v = xorshift64(&mut state);
            let bytes = v.to_le_bytes();
            let remaining = len - out.len();
            let take = remaining.min(8);
            out.extend_from_slice(&bytes[..take]);
        }
        out
    }

    fn default_dedup() -> BlockDeduplicator {
        BlockDeduplicator::with_defaults()
    }

    // ── 1. CDC Chunking tests ────────────────────────────────────────────────

    #[test]
    fn test_single_chunk_small_data() {
        let dedup = default_dedup();
        // 100 bytes < min_chunk_size (2048) → must produce exactly 1 chunk
        let data = gen_bytes(1, 100);
        let chunks = dedup.chunk_data(&data);
        assert_eq!(chunks.len(), 1);
        assert_eq!(chunks[0].1, data);
    }

    #[test]
    fn test_chunk_data_empty() {
        let dedup = default_dedup();
        let chunks = dedup.chunk_data(&[]);
        assert!(chunks.is_empty(), "empty input should produce no chunks");
    }

    #[test]
    fn test_chunk_data_exact_min() {
        let config = ChunkingConfig {
            min_chunk_size: 128,
            max_chunk_size: 65536,
            target_bits: 8, // low target bits → boundaries rare but possible
            window_size: 48,
            enable_compression: false,
        };
        let dedup = BlockDeduplicator::new(config);
        // Exactly min_chunk_size bytes — may or may not split depending on hash,
        // but the full data must be covered.
        let data = gen_bytes(42, 128);
        let chunks = dedup.chunk_data(&data);
        assert!(!chunks.is_empty());
        let total: usize = chunks.iter().map(|(_, b)| b.len()).sum();
        assert_eq!(total, 128);
    }

    #[test]
    fn test_chunk_data_max_size_boundary() {
        let config = ChunkingConfig {
            min_chunk_size: 512,
            max_chunk_size: 1024,
            target_bits: 20, // very high bits → hash boundary almost never fires
            window_size: 32,
            enable_compression: false,
        };
        let dedup = BlockDeduplicator::new(config);
        // 3 * max_chunk_size bytes → at least 3 chunks forced by max boundary
        let data = gen_bytes(7, 3 * 1024);
        let chunks = dedup.chunk_data(&data);
        assert!(
            chunks.len() >= 3,
            "expected at least 3 chunks, got {}",
            chunks.len()
        );
    }

    #[test]
    fn test_chunk_data_deterministic() {
        let dedup = default_dedup();
        let data = gen_bytes(99, 200_000);
        let c1 = dedup.chunk_data(&data);
        let c2 = dedup.chunk_data(&data);
        assert_eq!(c1.len(), c2.len());
        for (a, b) in c1.iter().zip(c2.iter()) {
            assert_eq!(a.0, b.0);
            assert_eq!(a.1, b.1);
        }
    }

    #[test]
    fn test_chunk_data_window_smaller_than_data() {
        let config = ChunkingConfig {
            min_chunk_size: 4,
            max_chunk_size: 65536,
            target_bits: 4, // lots of boundaries
            window_size: 48,
            enable_compression: false,
        };
        let dedup = BlockDeduplicator::new(config);
        // data shorter than window_size (48) but larger than min_chunk_size (4)
        let data = gen_bytes(3, 20);
        let chunks = dedup.chunk_data(&data);
        // must cover all bytes
        let total: usize = chunks.iter().map(|(_, b)| b.len()).sum();
        assert_eq!(total, 20);
    }

    #[test]
    fn test_chunk_data_covers_all_bytes() {
        let dedup = default_dedup();
        let data = gen_bytes(55, 500_000);
        let chunks = dedup.chunk_data(&data);
        let total: usize = chunks.iter().map(|(_, b)| b.len()).sum();
        assert_eq!(total, data.len(), "chunks must cover all bytes exactly");
    }

    #[test]
    fn test_chunk_hash_hex_display() {
        let hash = ChunkHash::from(0xDEADBEEF_CAFEBABE_u64);
        let s = format!("{}", hash);
        assert_eq!(s.len(), 16, "ChunkHash hex must be exactly 16 chars");
        assert!(s.chars().all(|c| c.is_ascii_hexdigit()), "must be hex");
    }

    #[test]
    fn test_chunk_hash_from_u64_roundtrip() {
        let v: u64 = 0x0102030405060708;
        let hash = ChunkHash::from(v);
        assert_eq!(hash.to_u64(), v);
    }

    #[test]
    fn test_rolling_hash_produces_boundaries() {
        // Craft data where we know a boundary fires by brute-forcing a window that
        // produces a hash value satisfying `h & mask == 0` for a small mask.
        let config = ChunkingConfig {
            min_chunk_size: 4,
            max_chunk_size: 65536,
            target_bits: 4, // mask = 0xF → boundary every ~16 bytes on average
            window_size: 4,
            enable_compression: false,
        };
        let dedup = BlockDeduplicator::new(config);
        // With 10,000 random bytes and target_bits=4, we expect many boundaries.
        let data = gen_bytes(1234, 10_000);
        let chunks = dedup.chunk_data(&data);
        // There should be multiple chunks
        assert!(
            chunks.len() > 2,
            "expected multiple hash-triggered boundaries, got {}",
            chunks.len()
        );
        // All bytes must still be accounted for
        let total: usize = chunks.iter().map(|(_, b)| b.len()).sum();
        assert_eq!(total, 10_000);
    }

    #[test]
    fn test_chunk_data_large_uniform_zeros() {
        let config = ChunkingConfig {
            min_chunk_size: 512,
            max_chunk_size: 1024,
            target_bits: 20, // hash boundary essentially never fires for uniform data
            window_size: 32,
            enable_compression: false,
        };
        let dedup = BlockDeduplicator::new(config);
        // Uniform zeros — boundaries only triggered by max_chunk_size
        let data = vec![0u8; 5 * 1024];
        let chunks = dedup.chunk_data(&data);
        // At least 5 chunks (max_chunk_size = 1024, data = 5120 bytes)
        assert!(chunks.len() >= 5);
        let total: usize = chunks.iter().map(|(_, b)| b.len()).sum();
        assert_eq!(total, 5 * 1024);
    }

    #[test]
    fn test_chunk_data_multiple_chunks() {
        let config = ChunkingConfig {
            min_chunk_size: 256,
            max_chunk_size: 1024,
            target_bits: 20, // force max-size splits
            window_size: 32,
            enable_compression: false,
        };
        let dedup = BlockDeduplicator::new(config);
        let data = gen_bytes(11, 4096);
        let chunks = dedup.chunk_data(&data);
        assert!(
            chunks.len() >= 4,
            "expected ≥4 chunks from 4096 bytes with max_size=1024, got {}",
            chunks.len()
        );
    }

    #[test]
    fn test_chunk_hash_from_bytes() {
        let bytes = [0u8, 1, 2, 3, 4, 5, 6, 7];
        let hash = ChunkHash::from_bytes(bytes);
        assert_eq!(hash.as_bytes(), &bytes);
    }

    #[test]
    fn test_chunk_data_each_chunk_nonempty() {
        let dedup = default_dedup();
        let data = gen_bytes(77, 300_000);
        let chunks = dedup.chunk_data(&data);
        for (i, (_, b)) in chunks.iter().enumerate() {
            assert!(!b.is_empty(), "chunk {} must not be empty", i);
        }
    }

    // ── 2. Store / Retrieve / Delete tests ──────────────────────────────────

    #[test]
    fn test_store_and_retrieve_small() {
        let mut dedup = default_dedup();
        let data = b"hello world".to_vec();
        dedup
            .store_object("obj1".to_string(), data.clone())
            .unwrap();
        let retrieved = dedup.retrieve_object("obj1").unwrap();
        assert_eq!(data, retrieved);
    }

    #[test]
    fn test_store_and_retrieve_large() {
        let mut dedup = default_dedup();
        let data = gen_bytes(1001, 500_000);
        dedup
            .store_object("large".to_string(), data.clone())
            .unwrap();
        let retrieved = dedup.retrieve_object("large").unwrap();
        assert_eq!(data, retrieved, "large object round-trip failed");
    }

    #[test]
    fn test_store_duplicate_objects() {
        let mut dedup = default_dedup();
        let data = gen_bytes(22, 100_000);
        dedup.store_object("a".to_string(), data.clone()).unwrap();
        dedup.store_object("b".to_string(), data.clone()).unwrap();
        let ra = dedup.retrieve_object("a").unwrap();
        let rb = dedup.retrieve_object("b").unwrap();
        assert_eq!(data, ra);
        assert_eq!(data, rb);
    }

    #[test]
    fn test_store_two_different_objects() {
        let mut dedup = default_dedup();
        let d1 = gen_bytes(101, 50_000);
        let d2 = gen_bytes(202, 60_000);
        dedup.store_object("x".to_string(), d1.clone()).unwrap();
        dedup.store_object("y".to_string(), d2.clone()).unwrap();
        assert_eq!(dedup.retrieve_object("x").unwrap(), d1);
        assert_eq!(dedup.retrieve_object("y").unwrap(), d2);
    }

    #[test]
    fn test_delete_object_removes_manifest() {
        let mut dedup = default_dedup();
        let data = gen_bytes(5, 10_000);
        dedup.store_object("del_me".to_string(), data).unwrap();
        dedup.delete_object("del_me").unwrap();
        let err = dedup.retrieve_object("del_me").unwrap_err();
        assert!(matches!(err, DeduplicatorError::ObjectNotFound(_)));
    }

    #[test]
    fn test_delete_decrements_ref_count() {
        let mut dedup = BlockDeduplicator::new(ChunkingConfig {
            min_chunk_size: 256,
            max_chunk_size: 65536,
            target_bits: 20, // very few natural splits → objects share same chunks
            window_size: 32,
            enable_compression: false,
        });
        let data = gen_bytes(9, 1024); // fits in single chunk
        dedup
            .store_object("obj_a".to_string(), data.clone())
            .unwrap();
        dedup
            .store_object("obj_b".to_string(), data.clone())
            .unwrap();

        // After storing twice, chunk should have ref_count == 2.
        // Get the hash of the single chunk.
        let manifest = dedup.get_manifest("obj_a").unwrap().clone();
        let hash = manifest.chunks[0].hash;
        assert_eq!(dedup.get_chunk(&hash).unwrap().ref_count, 2);

        // Delete one object — ref_count should drop to 1, chunk still present.
        let removed = dedup.delete_object("obj_a").unwrap();
        assert!(
            removed.is_empty(),
            "chunk still referenced by obj_b, should not be removed"
        );
        assert_eq!(dedup.get_chunk(&hash).unwrap().ref_count, 1);
    }

    #[test]
    fn test_delete_removes_unshared_chunks() {
        let mut dedup = default_dedup();
        let data = gen_bytes(13, 50_000);
        let manifest = dedup.store_object("only_obj".to_string(), data).unwrap();
        let n_chunks = manifest.chunks.len();
        let removed = dedup.delete_object("only_obj").unwrap();
        assert_eq!(
            removed.len(),
            n_chunks,
            "all unshared chunks should be removed"
        );
    }

    #[test]
    fn test_delete_nonexistent_object() {
        let mut dedup = default_dedup();
        let err = dedup.delete_object("ghost").unwrap_err();
        assert!(matches!(err, DeduplicatorError::ObjectNotFound(_)));
    }

    #[test]
    fn test_retrieve_nonexistent_object() {
        let dedup = default_dedup();
        let err = dedup.retrieve_object("ghost").unwrap_err();
        assert!(matches!(err, DeduplicatorError::ObjectNotFound(_)));
    }

    #[test]
    fn test_store_empty_data() {
        let mut dedup = default_dedup();
        let manifest = dedup.store_object("empty".to_string(), vec![]).unwrap();
        assert_eq!(manifest.total_size, 0);
        assert!(manifest.chunks.is_empty());
        let retrieved = dedup.retrieve_object("empty").unwrap();
        assert!(retrieved.is_empty());
    }

    #[test]
    fn test_store_overwrite_same_id() {
        let mut dedup = default_dedup();
        let d1 = gen_bytes(1, 1000);
        let d2 = gen_bytes(2, 2000);
        dedup.store_object("obj".to_string(), d1).unwrap();
        dedup.store_object("obj".to_string(), d2.clone()).unwrap();
        // The manifest is overwritten; retrieve should return d2.
        let retrieved = dedup.retrieve_object("obj").unwrap();
        assert_eq!(retrieved, d2);
    }

    #[test]
    fn test_manifest_chunk_order() {
        let mut dedup = default_dedup();
        let data = gen_bytes(33, 200_000);
        let manifest = dedup.store_object("ordered".to_string(), data).unwrap();
        let mut prev_offset = 0u64;
        for (i, cr) in manifest.chunks.iter().enumerate() {
            assert!(
                cr.offset_in_object >= prev_offset || i == 0,
                "chunk {} has non-monotonic offset",
                i
            );
            prev_offset = cr.offset_in_object;
        }
    }

    #[test]
    fn test_manifest_total_size() {
        let mut dedup = default_dedup();
        let data = gen_bytes(44, 150_000);
        let expected_size = data.len() as u64;
        let manifest = dedup.store_object("sized".to_string(), data).unwrap();
        assert_eq!(manifest.total_size, expected_size);
    }

    #[test]
    fn test_retrieve_exact_bytes() {
        let mut dedup = default_dedup();
        let data: Vec<u8> = (0..=255u8).cycle().take(10_000).collect();
        dedup
            .store_object("pattern".to_string(), data.clone())
            .unwrap();
        let retrieved = dedup.retrieve_object("pattern").unwrap();
        assert_eq!(data, retrieved, "byte-for-byte equality required");
    }

    #[test]
    fn test_delete_returns_removed_hashes() {
        let mut dedup = default_dedup();
        let data = gen_bytes(66, 30_000);
        let manifest = dedup.store_object("ret_hashes".to_string(), data).unwrap();
        let expected_hashes: Vec<ChunkHash> = manifest.chunks.iter().map(|cr| cr.hash).collect();
        let removed = dedup.delete_object("ret_hashes").unwrap();
        for h in &expected_hashes {
            assert!(removed.contains(h), "hash {} should be in removed list", h);
        }
    }

    #[test]
    fn test_manifest_chunk_offsets_are_contiguous() {
        let mut dedup = default_dedup();
        let data = gen_bytes(88, 300_000);
        let manifest = dedup.store_object("contiguous".to_string(), data).unwrap();
        let mut expected_offset = 0u64;
        for cr in &manifest.chunks {
            assert_eq!(
                cr.offset_in_object, expected_offset,
                "chunk offsets must be contiguous"
            );
            expected_offset += cr.chunk_length as u64;
        }
    }

    // ── 3. Dedup ratio / ref counting tests ─────────────────────────────────

    #[test]
    fn test_ref_count_increments_on_duplicate() {
        let mut dedup = BlockDeduplicator::new(ChunkingConfig {
            min_chunk_size: 256,
            max_chunk_size: 65536,
            target_bits: 20,
            window_size: 32,
            enable_compression: false,
        });
        let data = gen_bytes(111, 1024); // single chunk
        dedup.store_object("d1".to_string(), data.clone()).unwrap();
        dedup.store_object("d2".to_string(), data.clone()).unwrap();

        let manifest = dedup.get_manifest("d1").unwrap().clone();
        let hash = manifest.chunks[0].hash;
        assert_eq!(
            dedup.get_chunk(&hash).unwrap().ref_count,
            2,
            "ref_count should be 2 after two stores"
        );
    }

    #[test]
    fn test_ref_count_one_unique() {
        let mut dedup = default_dedup();
        let data = gen_bytes(222, 50_000);
        let manifest = dedup.store_object("unique_obj".to_string(), data).unwrap();
        for cr in &manifest.chunks {
            let chunk = dedup.get_chunk(&cr.hash).unwrap();
            assert_eq!(
                chunk.ref_count, 1,
                "unique chunk should have ref_count == 1"
            );
        }
    }

    #[test]
    fn test_dedup_ratio_identical_objects() {
        let mut dedup = default_dedup();
        let data = gen_bytes(333, 200_000);
        dedup
            .store_object("copy1".to_string(), data.clone())
            .unwrap();
        dedup
            .store_object("copy2".to_string(), data.clone())
            .unwrap();

        let stats = dedup.stats();
        // bytes_before = 2 * 200_000; bytes_after = ~200_000
        // dedup_ratio should be close to 0.5
        assert!(
            stats.dedup_ratio > 0.3,
            "expected dedup ratio > 0.3, got {}",
            stats.dedup_ratio
        );
    }

    #[test]
    fn test_dedup_ratio_unique_objects() {
        let mut dedup = default_dedup();
        let d1 = gen_bytes(401, 100_000);
        let d2 = gen_bytes(402, 100_000);
        dedup.store_object("u1".to_string(), d1).unwrap();
        dedup.store_object("u2".to_string(), d2).unwrap();

        let stats = dedup.stats();
        // No sharing expected → bytes_before ≈ bytes_after → ratio ≈ 0
        assert!(
            stats.dedup_ratio < 0.2,
            "expected low dedup ratio for unique data, got {}",
            stats.dedup_ratio
        );
    }

    #[test]
    fn test_stats_total_chunks() {
        let mut dedup = BlockDeduplicator::new(ChunkingConfig {
            min_chunk_size: 256,
            max_chunk_size: 65536,
            target_bits: 20,
            window_size: 32,
            enable_compression: false,
        });
        let data = gen_bytes(500, 1024); // single chunk, ref counted twice
        dedup.store_object("s1".to_string(), data.clone()).unwrap();
        dedup.store_object("s2".to_string(), data.clone()).unwrap();

        let stats = dedup.stats();
        // 1 unique chunk with ref_count == 2 → total_chunks == 2
        assert_eq!(stats.total_chunks, 2);
        assert_eq!(stats.unique_chunks, 1);
        assert_eq!(stats.duplicate_chunks, 1);
    }

    #[test]
    fn test_stats_unique_chunks_equals_store_size() {
        let mut dedup = default_dedup();
        let d1 = gen_bytes(601, 50_000);
        let d2 = gen_bytes(602, 50_000);
        dedup.store_object("a".to_string(), d1).unwrap();
        dedup.store_object("b".to_string(), d2).unwrap();
        let stats = dedup.stats();
        assert_eq!(stats.unique_chunks, dedup.chunk_count() as u64);
    }

    #[test]
    fn test_stats_bytes_before_after() {
        let mut dedup = default_dedup();
        let data = gen_bytes(700, 100_000);
        dedup
            .store_object("ba_test".to_string(), data.clone())
            .unwrap();
        let stats = dedup.stats();
        assert_eq!(stats.bytes_before, 100_000);
        // bytes_after should equal the sum of stored chunk sizes
        let expected_after: u64 = dedup
            .chunk_store
            .values()
            .map(|(_, d)| d.len() as u64)
            .sum();
        assert_eq!(stats.bytes_after, expected_after);
    }

    #[test]
    fn test_chunk_exists_true() {
        let mut dedup = default_dedup();
        let data = b"some data for hashing".to_vec();
        let manifest = dedup.store_object("ce_true".to_string(), data).unwrap();
        let hash = manifest.chunks[0].hash;
        assert!(dedup.chunk_exists(&hash));
    }

    #[test]
    fn test_chunk_exists_false() {
        let dedup = default_dedup();
        let fake_hash = ChunkHash::from(0xFFFF_FFFF_FFFF_FFFFu64);
        assert!(!dedup.chunk_exists(&fake_hash));
    }

    #[test]
    fn test_get_chunk_success() {
        let mut dedup = default_dedup();
        let data = b"chunk metadata test".to_vec();
        let expected_len = data.len();
        let manifest = dedup.store_object("gc_ok".to_string(), data).unwrap();
        let hash = manifest.chunks[0].hash;
        let chunk = dedup.get_chunk(&hash).unwrap();
        assert_eq!(chunk.hash, hash);
        assert_eq!(chunk.length, expected_len);
        assert_eq!(chunk.ref_count, 1);
    }

    #[test]
    fn test_get_chunk_not_found() {
        let dedup = default_dedup();
        let missing = ChunkHash::from(0xDEAD_C0DE_0000_0001u64);
        let err = dedup.get_chunk(&missing).unwrap_err();
        assert!(matches!(err, DeduplicatorError::ChunkNotFound(_)));
    }

    #[test]
    fn test_dedup_shared_chunks_across_objects() {
        // Two objects that are identical share all their chunks.
        // We store the same data under two different object IDs and confirm
        // every chunk has ref_count == 2.
        let config = ChunkingConfig {
            min_chunk_size: 512,
            max_chunk_size: 4096,
            target_bits: 20,
            window_size: 32,
            enable_compression: false,
        };
        let mut dedup = BlockDeduplicator::new(config);
        // Use 4096 bytes → exactly one max-size chunk, guaranteed identical for both objects.
        let data = gen_bytes(801, 4096);

        dedup
            .store_object("obj1".to_string(), data.clone())
            .unwrap();
        dedup
            .store_object("obj2".to_string(), data.clone())
            .unwrap();

        // All chunks should have ref_count == 2 (shared between both objects)
        let manifest1 = dedup.get_manifest("obj1").unwrap().clone();
        for cr in &manifest1.chunks {
            let chunk = dedup.get_chunk(&cr.hash).unwrap();
            assert_eq!(
                chunk.ref_count, 2,
                "shared chunk {} should have ref_count == 2",
                cr.hash
            );
        }
    }

    // ── 4. Compact tests ─────────────────────────────────────────────────────

    #[test]
    fn test_compact_removes_orphans() {
        let mut dedup = default_dedup();
        let data = gen_bytes(901, 50_000);
        dedup.store_object("to_compact".to_string(), data).unwrap();
        // Force ref_count to 0 by directly manipulating (simulate partial delete bug).
        // Instead, do a normal delete and then compact.
        dedup.delete_object("to_compact").unwrap();
        // After delete, chunks with ref_count=0 are already removed eagerly.
        // compact() should find nothing extra to remove.
        let removed = dedup.compact();
        // Whether 0 or more, the store must be empty afterwards.
        assert_eq!(dedup.chunk_count(), 0);
        let _ = removed; // count is valid either way
    }

    #[test]
    fn test_compact_keeps_referenced() {
        let mut dedup = BlockDeduplicator::new(ChunkingConfig {
            min_chunk_size: 256,
            max_chunk_size: 65536,
            target_bits: 20,
            window_size: 32,
            enable_compression: false,
        });
        let data = gen_bytes(1001, 1024);
        dedup
            .store_object("keep_a".to_string(), data.clone())
            .unwrap();
        dedup
            .store_object("keep_b".to_string(), data.clone())
            .unwrap();
        dedup.delete_object("keep_a").unwrap();

        // chunk still referenced by keep_b → compact must keep it
        let removed = dedup.compact();
        assert_eq!(removed, 0, "no orphans should exist");
        assert_eq!(dedup.chunk_count(), 1, "chunk still referenced by keep_b");
    }

    #[test]
    fn test_compact_returns_count() {
        let mut dedup = default_dedup();
        let data = gen_bytes(1100, 50_000);
        let manifest = dedup.store_object("count_test".to_string(), data).unwrap();
        let n_chunks = manifest.chunks.len();

        // Manually set all ref_counts to 0 to simulate orphaned chunks
        // (bypassing normal delete to test compact independently).
        for (meta, _) in dedup.chunk_store.values_mut() {
            meta.ref_count = 0;
        }
        // Also remove the manifest so list_objects is clean.
        dedup.manifests.remove("count_test");

        let removed = dedup.compact();
        assert_eq!(
            removed, n_chunks,
            "compact should remove all orphaned chunks"
        );
        assert_eq!(dedup.chunk_count(), 0);
    }

    #[test]
    fn test_compact_empty_store() {
        let mut dedup = default_dedup();
        let removed = dedup.compact();
        assert_eq!(removed, 0);
    }

    #[test]
    fn test_compact_idempotent() {
        let mut dedup = default_dedup();
        let data = gen_bytes(1200, 50_000);
        dedup.store_object("idem".to_string(), data).unwrap();
        dedup.delete_object("idem").unwrap();
        let _r1 = dedup.compact();
        let r2 = dedup.compact();
        assert_eq!(r2, 0, "second compact should remove nothing");
    }

    #[test]
    fn test_compact_after_delete_all() {
        let mut dedup = default_dedup();
        let d1 = gen_bytes(1301, 60_000);
        let d2 = gen_bytes(1302, 60_000);
        dedup.store_object("c1".to_string(), d1).unwrap();
        dedup.store_object("c2".to_string(), d2).unwrap();
        dedup.delete_object("c1").unwrap();
        dedup.delete_object("c2").unwrap();
        dedup.compact();
        assert_eq!(dedup.chunk_count(), 0, "all chunks should be gone");
        assert_eq!(dedup.object_count(), 0);
    }

    // ── 5. Error cases ───────────────────────────────────────────────────────

    #[test]
    fn test_error_object_not_found_display() {
        let err = DeduplicatorError::ObjectNotFound("my-special-object".to_string());
        let msg = format!("{}", err);
        assert!(
            msg.contains("my-special-object"),
            "error message must contain object id: {}",
            msg
        );
    }

    #[test]
    fn test_error_chunk_not_found_display() {
        let hash = ChunkHash::from(0xABCD_EF01_2345_6789u64);
        let err = DeduplicatorError::ChunkNotFound(hash);
        let msg = format!("{}", err);
        // The display includes the hash hex
        assert!(!msg.is_empty(), "error message must not be empty");
    }

    #[test]
    fn test_chunk_hash_equality() {
        let b = [1u8, 2, 3, 4, 5, 6, 7, 8];
        let h1 = ChunkHash::from_bytes(b);
        let h2 = ChunkHash::from_bytes(b);
        assert_eq!(h1, h2);
    }

    #[test]
    fn test_deduplicator_list_objects_empty() {
        let dedup = default_dedup();
        let objs = dedup.list_objects();
        assert!(objs.is_empty());
    }

    #[test]
    fn test_deduplicator_list_objects_sorted() {
        let mut dedup = default_dedup();
        let ids = ["zebra", "alpha", "mango", "beta"];
        for id in &ids {
            dedup
                .store_object((*id).to_string(), gen_bytes(42, 100))
                .unwrap();
        }
        let listed = dedup.list_objects();
        let mut expected: Vec<String> = ids.iter().map(|s| s.to_string()).collect();
        expected.sort();
        assert_eq!(listed, expected);
    }

    // ── 6. Integration / misc tests ──────────────────────────────────────────

    #[test]
    fn test_large_object_chunked_correctly() {
        let mut dedup = default_dedup();
        let data = gen_bytes(9999, 1_000_000); // 1 MB
        dedup
            .store_object("mega".to_string(), data.clone())
            .unwrap();
        let retrieved = dedup.retrieve_object("mega").unwrap();
        assert_eq!(data, retrieved, "1 MB round-trip failed");
    }

    #[test]
    fn test_many_small_objects() {
        let mut dedup = default_dedup();
        let mut originals: Vec<Vec<u8>> = Vec::new();
        let mut seed = 12345u64;
        for i in 0..100 {
            let data = gen_bytes(seed, 500 + i * 7);
            seed = seed.wrapping_add(1);
            dedup
                .store_object(format!("small_{}", i), data.clone())
                .unwrap();
            originals.push(data);
        }
        for (i, original) in originals.iter().enumerate() {
            let retrieved = dedup.retrieve_object(&format!("small_{}", i)).unwrap();
            assert_eq!(*original, retrieved, "small object {} failed round-trip", i);
        }
    }

    #[test]
    fn test_config_default_values() {
        let cfg = ChunkingConfig::default();
        assert_eq!(cfg.min_chunk_size, 2048);
        assert_eq!(cfg.max_chunk_size, 65536);
        assert_eq!(cfg.target_bits, 13);
        assert_eq!(cfg.window_size, 48);
        assert!(!cfg.enable_compression);
    }

    #[test]
    fn test_deduplicator_new_starts_empty() {
        let dedup = default_dedup();
        assert_eq!(dedup.chunk_count(), 0);
        assert_eq!(dedup.object_count(), 0);
        assert!(dedup.list_objects().is_empty());
        let stats = dedup.stats();
        assert_eq!(stats.total_chunks, 0);
        assert_eq!(stats.bytes_before, 0);
    }

    #[test]
    fn test_full_dedup_workflow() {
        let mut dedup = default_dedup();

        // 1. Store two objects with overlapping content.
        let shared = gen_bytes(5555, 200_000);
        let extra1 = gen_bytes(6001, 50_000);
        let extra2 = gen_bytes(6002, 50_000);

        let mut obj_a = shared.clone();
        obj_a.extend_from_slice(&extra1);
        let mut obj_b = shared.clone();
        obj_b.extend_from_slice(&extra2);

        let m_a = dedup
            .store_object("workflow_a".to_string(), obj_a.clone())
            .unwrap();
        let m_b = dedup
            .store_object("workflow_b".to_string(), obj_b.clone())
            .unwrap();

        // 2. Both objects retrievable and correct.
        assert_eq!(dedup.retrieve_object("workflow_a").unwrap(), obj_a);
        assert_eq!(dedup.retrieve_object("workflow_b").unwrap(), obj_b);

        // 3. Objects are listed.
        let listed = dedup.list_objects();
        assert!(listed.contains(&"workflow_a".to_string()));
        assert!(listed.contains(&"workflow_b".to_string()));

        // 4. Stats reflect two stores.
        let stats = dedup.stats();
        assert_eq!(stats.bytes_before, (obj_a.len() + obj_b.len()) as u64);

        // 5. Delete one object.
        dedup.delete_object("workflow_a").unwrap();
        assert!(dedup
            .retrieve_object("workflow_a")
            .unwrap_err()
            .to_string()
            .contains("workflow_a"));

        // 6. Other object still intact.
        assert_eq!(dedup.retrieve_object("workflow_b").unwrap(), obj_b);

        // 7. Compact.
        dedup.compact();

        // 8. Remaining manifest is consistent.
        let n_chunks_b = m_b.chunks.len();
        assert!(dedup.chunk_count() > 0, "workflow_b chunks still present");
        // But fewer than the combined chunk set.
        assert!(dedup.chunk_count() <= m_a.chunks.len() + n_chunks_b);

        // 9. Delete last object and compact → empty store.
        dedup.delete_object("workflow_b").unwrap();
        dedup.compact();
        assert_eq!(dedup.chunk_count(), 0);
        assert_eq!(dedup.object_count(), 0);
    }

    #[test]
    fn test_chunk_data_chunk_hashes_are_content_based() {
        let dedup = default_dedup();
        let d1 = vec![0u8; 100];
        let d2 = vec![1u8; 100];
        let c1 = dedup.chunk_data(&d1);
        let c2 = dedup.chunk_data(&d2);
        assert_ne!(
            c1[0].0, c2[0].0,
            "different content must produce different hashes"
        );
    }

    #[test]
    fn test_boundary_mask_calculation() {
        let config = ChunkingConfig {
            target_bits: 13,
            ..ChunkingConfig::default()
        };
        let mask = config.boundary_mask();
        assert_eq!(mask, (1u64 << 13) - 1);
        assert_eq!(mask, 8191);
    }

    #[test]
    fn test_store_single_byte() {
        let mut dedup = default_dedup();
        let data = vec![42u8];
        let manifest = dedup
            .store_object("single_byte".to_string(), data.clone())
            .unwrap();
        assert_eq!(manifest.total_size, 1);
        assert_eq!(manifest.chunks.len(), 1);
        let retrieved = dedup.retrieve_object("single_byte").unwrap();
        assert_eq!(retrieved, data);
    }

    #[test]
    fn test_stats_compression_ratio_at_least_one() {
        let mut dedup = default_dedup();
        let data = gen_bytes(7777, 100_000);
        dedup.store_object("cr_test".to_string(), data).unwrap();
        let stats = dedup.stats();
        // With no actual compression, bytes_before == bytes_after → ratio == 1.0
        assert!(
            (stats.compression_ratio - 1.0).abs() < 1e-6,
            "expected ratio ~1.0, got {}",
            stats.compression_ratio
        );
    }

    #[test]
    fn test_chunk_length_matches_metadata() {
        let mut dedup = default_dedup();
        let data = gen_bytes(8888, 200_000);
        let manifest = dedup.store_object("len_check".to_string(), data).unwrap();
        for cr in &manifest.chunks {
            let chunk_meta = dedup.get_chunk(&cr.hash).unwrap();
            assert_eq!(
                chunk_meta.length, cr.chunk_length,
                "chunk metadata length must match ChunkRef length"
            );
        }
    }

    #[test]
    fn test_object_count_updates_correctly() {
        let mut dedup = default_dedup();
        assert_eq!(dedup.object_count(), 0);
        dedup
            .store_object("oc1".to_string(), gen_bytes(1, 1000))
            .unwrap();
        assert_eq!(dedup.object_count(), 1);
        dedup
            .store_object("oc2".to_string(), gen_bytes(2, 1000))
            .unwrap();
        assert_eq!(dedup.object_count(), 2);
        dedup.delete_object("oc1").unwrap();
        assert_eq!(dedup.object_count(), 1);
        dedup.delete_object("oc2").unwrap();
        assert_eq!(dedup.object_count(), 0);
    }
}