armdb 0.6.0

sharded bitcask key-value storage optimized for NVMe
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
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
use std::hash::Hash;
use std::mem::size_of;
use std::ops::Bound;

use crate::Key;

use crate::compaction::{CompactionIndex, compact_shard};
use crate::config::Config;
use crate::disk_loc::DiskLoc;
use crate::durability::{Bitcask, Durability, DurabilityInner};
use crate::engine::Engine;
use crate::error::{DbError, DbResult};
use crate::hook::{NoHook, WriteHook};
use crate::key::Location;
use crate::map_index::{
    Dir, KeyPage, MapIterView, Page, ShardIndex, ShardSource, dir_from, key_index_batch,
};
use crate::recovery::recover_const_map;
use crate::sync::{self, Mutex, MutexGuard};

pub(crate) struct MapEntry<const V: usize, L: Location> {
    pub(crate) loc: L,
    pub(crate) value: [u8; V],
}

/// A map with fixed-size keys and values. All values are stored inline in a per-shard HashMap.
/// Reads never touch disk — zero I/O reads. O(1) lookup instead of O(log n) SkipList.
/// Ordered iteration is available when opened with `Config::iterable(true)` via
/// [`iter_view`](Self::iter_view); otherwise use `ConstTree` for prefix/range scans.
///
/// Generic over `D: Durability` (default: [`Bitcask`]). Use [`Fixed`] backend for
/// frequent updates without compaction (`FixedMap` is a type alias for `ConstMap<K, V, Fixed>`).
///
/// Each `ConstMap` owns its storage engine — one map = one database directory.
///
/// # Write hooks
///
/// Uses [`WriteHook<K>`]. `on_write` fires on `put`/`insert`/`delete`/`cas`/`compare_delete`/`update`/`fetch_update`,
/// and on each mutation inside `atomic()` (replayed after the lock is released, in application order).
/// `on_init` fires once per live entry
/// during `migrate()` or `replay_init()`. Old value is always provided in
/// `on_write` — `NEEDS_OLD_VALUE` is ignored (value is inline, zero cost).
pub struct ConstMap<
    K: Key + Send + Sync + Hash + Eq,
    const V: usize,
    H: WriteHook<K> = NoHook,
    D: Durability = Bitcask,
> {
    indexes: Vec<Mutex<ShardIndex<K, MapEntry<V, D::Loc>>>>,
    durability: D,
    shard_prefix_bits: usize,
    hook: H,
    iterable: bool,
    reversed: bool,
}

// ==========================================================================
// Bitcask-specific: open, close, compact, sync_hints, config, flush_buffers
// ==========================================================================

impl<K: Key + Send + Sync + Hash + Eq, const V: usize> ConstMap<K, V, NoHook, Bitcask> {
    /// Open or create a `ConstMap` at the given path.
    /// Recovers the index from existing data files on disk.
    pub fn open(path: impl AsRef<std::path::Path>, config: Config) -> DbResult<Self> {
        Self::open_hooked(path, config, NoHook)
    }
}

impl<K: Key + Send + Sync + Hash + Eq, const V: usize, H: WriteHook<K>> ConstMap<K, V, H, Bitcask> {
    /// Open or create a `ConstMap` with a write hook for secondary index maintenance.
    pub fn open_hooked(
        path: impl AsRef<std::path::Path>,
        config: Config,
        hook: H,
    ) -> DbResult<Self> {
        let config = config.with_resolved_hints(false);
        Self::open_inner(path, config, hook)
    }

    fn open_inner(path: impl AsRef<std::path::Path>, config: Config, hook: H) -> DbResult<Self> {
        let compaction_threshold = config.compaction_threshold;
        let shard_prefix_bits = config.shard_prefix_bits;
        let iterable = config.iterable;
        let reversed = config.reversed;
        let engine = Engine::open(path, config)?;

        let shard_count = engine.shards().len();
        let mut indexes = Vec::with_capacity(shard_count);
        for _ in 0..shard_count {
            // Recover without the companion even when `iterable`: it is built once
            // via `set_iterable_and_rebuild` after recovery (below), avoiding a
            // redundant incremental build during the per-entry `upsert` pass.
            indexes.push(Mutex::new(ShardIndex::new(false)));
        }

        let durability = Bitcask {
            engine,
            compaction_threshold,
        };

        let map = Self {
            indexes,
            durability,
            shard_prefix_bits,
            hook,
            iterable,
            reversed,
        };

        // Recover index from disk
        let shard_dirs = map.durability.engine.shard_dirs();
        let shard_dir_refs = Engine::shard_dir_refs(&shard_dirs);
        let shard_ids = map.durability.engine.shard_ids();

        let hints = map.durability.engine.hints();
        let outcome = recover_const_map::<K, V>(
            &shard_dir_refs,
            &shard_ids,
            map.indexes(),
            hints,
            #[cfg(feature = "encryption")]
            map.durability.engine.cipher(),
        )?;
        for tail in &outcome.active_tails {
            map.durability.engine.shards()[tail.shard_idx].apply_recovery_tail(tail)?;
        }
        for (shard_idx, dead) in outcome.shard_dead_bytes {
            map.durability.engine.shards()[shard_idx].install_dead_bytes(dead);
        }
        let max_gsn = outcome.max_gsn;

        map.durability
            .engine
            .gsn()
            .fetch_max(max_gsn + 1, std::sync::atomic::Ordering::Relaxed);
        if hints {
            for shard in map.durability.engine.shards().iter() {
                shard.set_key_len(size_of::<K>());
            }
        }
        if iterable {
            for idx in &map.indexes {
                sync::lock(idx).set_iterable_and_rebuild();
            }
        }
        tracing::info!(
            key_size = size_of::<K>(),
            V,
            entries = map.len(),
            "const_map recovered"
        );

        Ok(map)
    }

    /// Graceful shutdown: write hint files (if enabled), flush write buffers + fsync.
    pub fn close(self) -> DbResult<()> {
        if self.durability.engine.hints() {
            self.sync_hints()?;
        }
        self.durability.engine.flush()
    }

    /// Flush all shard write buffers to disk (without fsync).
    pub fn flush_buffers(&self) -> DbResult<()> {
        self.durability.engine.flush_buffers()
    }

    /// Get the database configuration.
    pub fn config(&self) -> &Config {
        self.durability.engine.config()
    }

    /// Trigger a compaction pass across all shards.
    pub fn compact(&self) -> DbResult<usize> {
        let mut total_compacted = 0;
        for shard in self.durability.engine.shards().iter() {
            total_compacted += compact_shard(shard, self, self.durability.compaction_threshold)?;
        }
        Ok(total_compacted)
    }

    /// Write hint files for all active shard files. Call during graceful shutdown.
    pub fn sync_hints(&self) -> DbResult<()> {
        for shard in self.durability.engine.shards().iter() {
            shard.write_active_hint(size_of::<K>())?;
        }
        Ok(())
    }

    pub(crate) fn indexes(&self) -> &[Mutex<ShardIndex<K, MapEntry<V, DiskLoc>>>] {
        &self.indexes
    }
}

impl<K: Key + Send + Sync + Hash + Eq, const V: usize, H: WriteHook<K>> CompactionIndex<K>
    for ConstMap<K, V, H, Bitcask>
{
    fn update_if_match(&self, key: &K, old_loc: DiskLoc, new_loc: DiskLoc) -> bool {
        let mut index = sync::lock(&self.indexes[self.shard_for(key)]);
        if let Some(entry) = index.get_mut(key)
            && entry.loc == old_loc
        {
            entry.loc = new_loc;
            return true;
        }
        false
    }

    fn contains_key(&self, key: &K) -> bool {
        self.contains(key)
    }

    fn is_live(&self, shard_id: u8, key: &K, loc: DiskLoc) -> bool {
        let index = sync::lock(&self.indexes[shard_id as usize]);
        index.get(key).is_some_and(|e| e.loc == loc)
    }
}

// ==========================================================================
// Fixed-specific: open, close
// ==========================================================================

use crate::durability::Fixed;
use crate::fixed::config::FixedConfig;

impl<K: Key + Send + Sync + Hash + Eq, const V: usize> ConstMap<K, V, NoHook, Fixed> {
    /// Open or create a `ConstMap` with Fixed (fixed-slot) backend.
    /// Recovers the index from existing data files on disk.
    pub fn open(path: impl AsRef<std::path::Path>, config: FixedConfig) -> DbResult<Self> {
        Self::open_fixed_inner(path, config, NoHook)
    }
}

impl<K: Key + Send + Sync + Hash + Eq, const V: usize, H: WriteHook<K>> ConstMap<K, V, H, Fixed> {
    /// Open or create a `ConstMap` with a write hook, using Fixed (fixed-slot) backend.
    pub fn open_with_hook(
        path: impl AsRef<std::path::Path>,
        config: FixedConfig,
        hook: H,
    ) -> DbResult<Self> {
        Self::open_fixed_inner(path, config, hook)
    }

    fn open_fixed_inner(
        path: impl AsRef<std::path::Path>,
        config: FixedConfig,
        hook: H,
    ) -> DbResult<Self> {
        let shard_prefix_bits = config.shard_prefix_bits;
        let iterable = config.iterable;
        let reversed = config.reversed;
        let dur = Fixed::open(path, config, std::mem::size_of::<K>(), V)?;

        let shard_count = dur.shard_count();
        let mut indexes = Vec::with_capacity(shard_count);
        for _ in 0..shard_count {
            // Built once after recovery via `set_iterable_and_rebuild` — see the
            // Bitcask `open_inner` for the rationale.
            indexes.push(Mutex::new(ShardIndex::new(false)));
        }

        let total_recovered = dur.recover_entries(|shard_idx| {
            // Hold this shard's index lock for the whole shard recovery.
            let mut guard = sync::lock(&indexes[shard_idx]);
            move |key_bytes: &[u8], value_bytes: &[u8], slot_id: u32| {
                let key = K::from_bytes(key_bytes);
                let mut value = [0u8; V];
                value.copy_from_slice(value_bytes);
                guard.upsert(
                    key,
                    MapEntry {
                        loc: slot_id,
                        value,
                    },
                );
            }
        })?;

        tracing::info!(
            key_size = std::mem::size_of::<K>(),
            V,
            entries = total_recovered,
            "fixed_map recovered"
        );

        if iterable {
            for idx in &indexes {
                sync::lock(idx).set_iterable_and_rebuild();
            }
        }

        Ok(Self {
            indexes,
            durability: dur,
            shard_prefix_bits,
            hook,
            iterable,
            reversed,
        })
    }

    /// Perform a clean shutdown (Fixed backend).
    pub fn close(self) -> DbResult<()> {
        self.durability.close()
    }
}

// ==========================================================================
// Fixed-specific replication apply helpers
// ==========================================================================

#[cfg(feature = "replication")]
impl<K: Key + Send + Sync + Hash + Eq, const V: usize, H: WriteHook<K>> ConstMap<K, V, H, Fixed> {
    /// Accessor for the Fixed durability backend — used by the
    /// `FixedReplicationTarget` impl in `crate::fixed_replication::apply`.
    pub(crate) fn fixed_durability(&self) -> &Fixed {
        &self.durability
    }

    /// Return an `Arc<dyn FixedEngineAccess>` for this map's engine.
    /// Used by integration tests to construct a `FixedReplicationServer`.
    pub fn fixed_engine_access(
        &self,
    ) -> std::sync::Arc<dyn crate::fixed_replication::FixedEngineAccess> {
        self.durability.engine.clone()
    }

    /// Return the on-disk slot id for `key`, if present. O(1).
    ///
    /// Used by the Fixed replication apply path to detect stale entries that
    /// occupy a slot now reassigned to a different key.
    pub(crate) fn get_slot_id(&self, key: &K) -> Option<u32> {
        let index = sync::lock(&self.indexes[self.shard_for(key)]);
        index.get(key).map(|e| e.loc)
    }

    /// Remove `key` from the in-memory index only if its current slot matches
    /// `slot_id`. Returns `true` if a removal happened.
    ///
    /// Bypasses durability — the caller is responsible for the underlying slot
    /// state.
    pub(crate) fn remove_key_if_slot_matches(&self, key: &K, slot_id: u32) -> bool {
        let mut index = sync::lock(&self.indexes[self.shard_for(key)]);
        match index.get(key) {
            Some(entry) if entry.loc == slot_id => {
                index.remove(key);
                true
            }
            _ => false,
        }
    }

    /// Upsert `key → (value, slot_id)` in the in-memory index. Bypasses
    /// durability — replication has already rewritten the slot on disk.
    pub(crate) fn upsert_replicated(&self, key: &K, value: [u8; V], slot_id: u32) {
        let mut index = sync::lock(&self.indexes[self.shard_for(key)]);
        index.upsert(
            *key,
            MapEntry {
                loc: slot_id,
                value,
            },
        );
    }
}

// ==========================================================================
// Generic impl block — works with any D: Durability
// ==========================================================================

impl<K: Key + Send + Sync + Hash + Eq, const V: usize, H: WriteHook<K>, D: Durability>
    ConstMap<K, V, H, D>
{
    /// Configured iteration direction (`true` = DESC / newest-first).
    /// Backend-agnostic — `config()` exposes `reversed` only on Bitcask, so
    /// wrappers (`ZeroMap`) read direction through this on either backend.
    pub(crate) fn reversed(&self) -> bool {
        self.reversed
    }

    /// Get a value by key. O(1) HashMap lookup, zero disk I/O.
    /// Takes a brief per-shard mutex to read the index (value is inline).
    pub fn get(&self, key: &K) -> Option<[u8; V]> {
        metrics::counter!("armdb.ops", "op" => "get", "tree" => "const_map").increment(1);
        #[cfg(feature = "hot-path-tracing")]
        tracing::trace!("const_map.get");
        let index = sync::lock(&self.indexes[self.shard_for(key)]);
        index.get(key).map(|e| e.value)
    }

    /// Get a value by key, returning `Err(KeyNotFound)` if absent.
    pub fn get_or_err(&self, key: &K) -> DbResult<[u8; V]> {
        self.get(key).ok_or(DbError::KeyNotFound)
    }

    /// Insert or update a key-value pair. Returns the old value if the key existed.
    pub fn put(&self, key: &K, value: &[u8; V]) -> DbResult<Option<[u8; V]>> {
        metrics::counter!("armdb.ops", "op" => "put", "tree" => "const_map").increment(1);
        #[cfg(feature = "hot-path-tracing")]
        tracing::trace!("const_map.put");
        let shard_id = self.shard_for(key);
        let mut inner = self.durability.lock_shard(shard_id);
        let mut index = sync::lock(&self.indexes[shard_id]);
        let old = self.put_locked(shard_id, &mut *inner, &mut index, key, value)?;
        let needs_sync = inner.should_sync();
        drop(index);
        drop(inner);
        if needs_sync {
            self.durability.lock_shard(shard_id).sync()?;
        }
        self.hook
            .on_write(key, old.as_ref().map(|v| &v[..]), Some(&value[..]));
        Ok(old)
    }

    /// Insert a key-value pair only if the key does not exist.
    /// Returns `Err(KeyExists)` if the key is already present.
    pub fn insert(&self, key: &K, value: &[u8; V]) -> DbResult<()> {
        metrics::counter!("armdb.ops", "op" => "insert", "tree" => "const_map").increment(1);
        #[cfg(feature = "hot-path-tracing")]
        tracing::trace!("const_map.insert");
        let shard_id = self.shard_for(key);
        let mut inner = self.durability.lock_shard(shard_id);
        let mut index = sync::lock(&self.indexes[shard_id]);
        self.insert_locked(shard_id, &mut *inner, &mut index, key, value)?;
        let needs_sync = inner.should_sync();
        drop(index);
        drop(inner);
        if needs_sync {
            self.durability.lock_shard(shard_id).sync()?;
        }
        self.hook.on_write(key, None, Some(&value[..]));
        Ok(())
    }

    /// Delete a key. Returns the old value if the key existed.
    pub fn delete(&self, key: &K) -> DbResult<Option<[u8; V]>> {
        metrics::counter!("armdb.ops", "op" => "delete", "tree" => "const_map").increment(1);
        #[cfg(feature = "hot-path-tracing")]
        tracing::trace!("const_map.delete");
        let shard_id = self.shard_for(key);
        let mut inner = self.durability.lock_shard(shard_id);
        let mut index = sync::lock(&self.indexes[shard_id]);
        let old = self.delete_locked(shard_id, &mut *inner, &mut index, key)?;
        let needs_sync = inner.should_sync();
        drop(index);
        drop(inner);
        if needs_sync {
            self.durability.lock_shard(shard_id).sync()?;
        }
        if let Some(ref old_val) = old {
            self.hook.on_write(key, Some(&old_val[..]), None);
        }
        Ok(old)
    }

    /// Compare-and-swap: if current value == expected, replace with new_value.
    /// Returns `Ok(())` on success, `Err(CasMismatch)` if current != expected,
    /// `Err(KeyNotFound)` if key doesn't exist.
    pub fn cas(&self, key: &K, expected: &[u8; V], new_value: &[u8; V]) -> DbResult<()> {
        metrics::counter!("armdb.ops", "op" => "cas", "tree" => "const_map").increment(1);
        #[cfg(feature = "hot-path-tracing")]
        tracing::trace!("const_map.cas");
        let shard_id = self.shard_for(key);
        let mut inner = self.durability.lock_shard(shard_id);
        let mut index = sync::lock(&self.indexes[shard_id]);

        let entry = index.get(key).ok_or(DbError::KeyNotFound)?;
        if entry.value != *expected {
            return Err(DbError::CasMismatch);
        }

        let old_loc = entry.loc;
        let new_loc = inner.write_update(shard_id as u8, old_loc, key.as_bytes(), new_value)?;

        index.upsert(
            *key,
            MapEntry {
                loc: new_loc,
                value: *new_value,
            },
        );

        let needs_sync = inner.should_sync();
        drop(index);
        drop(inner);
        if needs_sync {
            self.durability.lock_shard(shard_id).sync()?;
        }

        self.hook
            .on_write(key, Some(&expected[..]), Some(&new_value[..]));
        Ok(())
    }

    /// Compare-and-delete: if the current value == `expected`, delete the key.
    /// Returns `Ok(())` on success, `Err(CasMismatch)` if current != expected,
    /// `Err(KeyNotFound)` if the key doesn't exist.
    /// Works on both Bitcask and FixedStore backends.
    pub fn compare_delete(&self, key: &K, expected: &[u8; V]) -> DbResult<()> {
        metrics::counter!("armdb.ops", "op" => "compare_delete", "tree" => "const_map")
            .increment(1);
        #[cfg(feature = "hot-path-tracing")]
        tracing::trace!("const_map.compare_delete");
        let shard_id = self.shard_for(key);
        let mut inner = self.durability.lock_shard(shard_id);
        let mut index = sync::lock(&self.indexes[shard_id]);

        let entry = index.get(key).ok_or(DbError::KeyNotFound)?;
        if entry.value != *expected {
            return Err(DbError::CasMismatch);
        }
        let old_value = entry.value;
        let old_loc = entry.loc;

        inner.write_tombstone(shard_id as u8, old_loc, key.as_bytes())?;
        index.remove(key);

        let needs_sync = inner.should_sync();
        drop(index);
        drop(inner);
        if needs_sync {
            self.durability.lock_shard(shard_id).sync()?;
        }

        self.hook.on_write(key, Some(&old_value[..]), None);
        Ok(())
    }

    /// Atomically read-modify-write. Returns `Some(new_value)` if key existed, `None` otherwise.
    /// The closure must not be heavy (shard lock is held).
    pub fn update(
        &self,
        key: &K,
        f: impl FnOnce(&[u8; V]) -> [u8; V],
    ) -> DbResult<Option<[u8; V]>> {
        self.update_inner(key, f, false)
    }

    /// Like [`update()`](Self::update), but returns `Some(old_value)` instead of the new one.
    pub fn fetch_update(
        &self,
        key: &K,
        f: impl FnOnce(&[u8; V]) -> [u8; V],
    ) -> DbResult<Option<[u8; V]>> {
        self.update_inner(key, f, true)
    }

    fn update_inner(
        &self,
        key: &K,
        f: impl FnOnce(&[u8; V]) -> [u8; V],
        return_old: bool,
    ) -> DbResult<Option<[u8; V]>> {
        metrics::counter!("armdb.ops", "op" => "update", "tree" => "const_map").increment(1);
        #[cfg(feature = "hot-path-tracing")]
        tracing::trace!("const_map.update");
        let shard_id = self.shard_for(key);
        let mut inner = self.durability.lock_shard(shard_id);
        let mut index = sync::lock(&self.indexes[shard_id]);

        let (old_value, new_value) =
            match self.update_locked(shard_id, &mut *inner, &mut index, key, f)? {
                Some(pair) => pair,
                None => return Ok(None),
            };

        let needs_sync = inner.should_sync();
        drop(index);
        drop(inner);
        if needs_sync {
            self.durability.lock_shard(shard_id).sync()?;
        }
        self.hook
            .on_write(key, Some(&old_value[..]), Some(&new_value[..]));
        Ok(Some(if return_old { old_value } else { new_value }))
    }

    /// Check if a key exists.
    pub fn contains(&self, key: &K) -> bool {
        let index = sync::lock(&self.indexes[self.shard_for(key)]);
        index.contains_key(key)
    }

    /// Total number of entries across all shards.
    pub fn len(&self) -> usize {
        self.indexes.iter().map(|m| sync::lock(m).len()).sum()
    }

    pub fn is_empty(&self) -> bool {
        self.indexes.iter().all(|m| sync::lock(m).is_empty())
    }

    fn put_no_hook(&self, key: &K, value: &[u8; V]) -> DbResult<Option<[u8; V]>> {
        let shard_id = self.shard_for(key);
        let mut inner = self.durability.lock_shard(shard_id);
        let mut index = sync::lock(&self.indexes[shard_id]);
        let result = self.put_locked(shard_id, &mut *inner, &mut index, key, value);
        let needs_sync = result.is_ok() && inner.should_sync();
        drop(index);
        drop(inner);
        if needs_sync {
            self.durability.lock_shard(shard_id).sync()?;
        }
        result
    }

    fn delete_no_hook(&self, key: &K) -> DbResult<Option<[u8; V]>> {
        let shard_id = self.shard_for(key);
        let mut inner = self.durability.lock_shard(shard_id);
        let mut index = sync::lock(&self.indexes[shard_id]);
        let result = self.delete_locked(shard_id, &mut *inner, &mut index, key);
        let needs_sync = result.is_ok() && inner.should_sync();
        drop(index);
        drop(inner);
        if needs_sync {
            self.durability.lock_shard(shard_id).sync()?;
        }
        result
    }

    /// Atomically execute multiple operations on a single shard.
    /// All keys must route to the same shard as `shard_key`.
    /// The closure must be short — shard lock is held for its duration.
    pub fn atomic<R>(
        &self,
        shard_key: &K,
        f: impl FnOnce(&mut ConstMapShard<'_, K, V, H, D>) -> DbResult<R>,
    ) -> DbResult<R> {
        let shard_id = self.shard_for(shard_key);
        let inner = self.durability.lock_shard(shard_id);
        let index = sync::lock(&self.indexes[shard_id]);
        let mut shard = ConstMapShard {
            tree: self,
            inner,
            index,
            shard_id,
            events: Vec::new(),
        };
        let result = f(&mut shard);
        let ConstMapShard {
            inner,
            index,
            events,
            ..
        } = shard;
        let needs_sync = inner.should_sync();
        drop(index);
        drop(inner);
        if needs_sync {
            self.durability.lock_shard(shard_id).sync()?;
        }
        if H::NEEDS_WRITE {
            for (k, old, new) in &events {
                self.hook.on_write(
                    k,
                    old.as_ref().map(|v| &v[..]),
                    new.as_ref().map(|v| &v[..]),
                );
            }
        }
        result
    }

    fn put_locked(
        &self,
        shard_id: usize,
        inner: &mut D::Inner,
        index: &mut ShardIndex<K, MapEntry<V, D::Loc>>,
        key: &K,
        value: &[u8; V],
    ) -> DbResult<Option<[u8; V]>> {
        // Fast path: key exists — write_update (dead-bytes tracking / in-place overwrite)
        if let Some(existing) = index.get(key) {
            let old_value = existing.value;
            let old_loc = existing.loc;
            let new_loc = inner.write_update(shard_id as u8, old_loc, key.as_bytes(), value)?;
            index.upsert(
                *key,
                MapEntry {
                    loc: new_loc,
                    value: *value,
                },
            );
            return Ok(Some(old_value));
        }

        // Slow path: new key — write_new
        let loc = inner.write_new(shard_id as u8, key.as_bytes(), value)?;
        index.upsert(*key, MapEntry { loc, value: *value });
        Ok(None)
    }

    fn update_locked(
        &self,
        shard_id: usize,
        inner: &mut D::Inner,
        index: &mut ShardIndex<K, MapEntry<V, D::Loc>>,
        key: &K,
        f: impl FnOnce(&[u8; V]) -> [u8; V],
    ) -> DbResult<Option<([u8; V], [u8; V])>> {
        let entry = match index.get(key) {
            Some(e) => e,
            None => return Ok(None),
        };
        let old_value = entry.value;
        let old_loc = entry.loc;
        let new_value = f(&old_value);
        let new_loc = inner.write_update(shard_id as u8, old_loc, key.as_bytes(), &new_value)?;
        index.upsert(
            *key,
            MapEntry {
                loc: new_loc,
                value: new_value,
            },
        );
        Ok(Some((old_value, new_value)))
    }

    fn insert_locked(
        &self,
        shard_id: usize,
        inner: &mut D::Inner,
        index: &mut ShardIndex<K, MapEntry<V, D::Loc>>,
        key: &K,
        value: &[u8; V],
    ) -> DbResult<()> {
        if index.contains_key(key) {
            return Err(DbError::KeyExists);
        }

        let loc = inner.write_new(shard_id as u8, key.as_bytes(), value)?;
        index.upsert(*key, MapEntry { loc, value: *value });
        Ok(())
    }

    fn delete_locked(
        &self,
        shard_id: usize,
        inner: &mut D::Inner,
        index: &mut ShardIndex<K, MapEntry<V, D::Loc>>,
        key: &K,
    ) -> DbResult<Option<[u8; V]>> {
        let old = match index.get(key) {
            Some(old) => old,
            None => return Ok(None),
        };

        inner.write_tombstone(shard_id as u8, old.loc, key.as_bytes())?;
        let value = old.value;
        index.remove(key);
        Ok(Some(value))
    }

    pub fn shard_for(&self, key: &K) -> usize {
        if self.shard_prefix_bits == 0 || self.shard_prefix_bits >= size_of::<K>() * 8 {
            let hash = xxhash_rust::xxh3::xxh3_64(key.as_bytes());
            return (hash as usize) % self.durability.shard_count();
        }

        let full_bytes = self.shard_prefix_bits / 8;
        let extra_bits = self.shard_prefix_bits % 8;

        let hash = if extra_bits == 0 {
            xxhash_rust::xxh3::xxh3_64(&key.as_bytes()[..full_bytes])
        } else {
            let mut buf = K::zeroed();
            buf.as_bytes_mut()[..full_bytes].copy_from_slice(&key.as_bytes()[..full_bytes]);
            let mask = !((1u8 << (8 - extra_bits)) - 1);
            buf.as_bytes_mut()[full_bytes] = key.as_bytes()[full_bytes] & mask;
            xxhash_rust::xxh3::xxh3_64(&buf.as_bytes()[..full_bytes + 1])
        };

        (hash as usize) % self.durability.shard_count()
    }

    /// Flush the durability backend.
    pub fn flush(&self) -> DbResult<()> {
        self.durability.flush()
    }

    // -- Ordered iteration ----------------------------------------------------

    /// Ordered scan view. `None` unless the collection was opened with
    /// `iterable(true)`. Direction follows `Config::reversed` (default DESC).
    ///
    /// **Occasional admin/maintenance API, not a hot path.** Use it to browse a
    /// collection or bulk-clean stale rows (`retain`); for scan-heavy access reach
    /// for the `*Tree` sibling — a Map's strength is O(1) point lookup, and the
    /// companion key index costs extra memory plus O(log N) per write. Unlike a
    /// `Tree` scan it is **not a point-in-time snapshot**: the k-way merge drops
    /// each shard lock between batches, so concurrent writes may be only partially
    /// reflected (run on a quiet DB if you need a coherent view).
    pub fn iter_view(&self) -> Option<ConstMapIterView<'_, K, V, H, D>> {
        self.iterable.then(|| ConstMapIterView {
            inner: MapIterView::new(self, dir_from(self.reversed())),
        })
    }

    // -- Read-only scan -------------------------------------------------------

    /// Read-only pass over all live entries. Order is unspecified; consistency
    /// is per-shard (snapshot under the shard index lock). Intended for schema
    /// validation / maintenance on a quiet database.
    ///
    /// Memory: each shard is snapshotted as a `Vec<(K, [u8; V])>` before the
    /// callback runs, so peak usage is one full shard of keys+values. For
    /// large `V` on big collections prefer trees (streaming `iter()`).
    pub fn for_each(&self, mut f: impl FnMut(&K, &[u8; V])) {
        for i in 0..self.durability.shard_count() {
            let entries: Vec<(K, [u8; V])> = {
                let index = sync::lock(&self.indexes[i]);
                index.iter().map(|(k, e)| (*k, e.value)).collect()
            };
            for (key, value) in entries {
                f(&key, &value);
            }
        }
    }

    // -- Migration ------------------------------------------------------------

    /// Iterate all entries and optionally mutate them.
    ///
    /// Must be called at startup before concurrent writers are active.
    /// Concurrent writes during migration are unsupported and may cause
    /// stale reads.
    ///
    /// Fires `on_init` for Keep/Update (if `NEEDS_INIT`). `on_write` is never
    /// fired during migration. Returns the number of mutated entries.
    pub fn migrate(
        &self,
        f: impl Fn(&K, &[u8; V]) -> crate::MigrateAction<[u8; V]>,
    ) -> DbResult<usize> {
        use crate::MigrateAction;

        let mut count = 0;
        for i in 0..self.durability.shard_count() {
            let entries: Vec<(K, [u8; V])> = {
                let index = sync::lock(&self.indexes[i]);
                index.iter().map(|(k, e)| (*k, e.value)).collect()
            };
            for (key, value) in entries {
                match f(&key, &value) {
                    MigrateAction::Keep => {
                        if H::NEEDS_INIT {
                            self.hook.on_init(&key, &value[..]);
                        }
                    }
                    MigrateAction::Update(new_value) => {
                        self.put_no_hook(&key, &new_value)?;
                        if H::NEEDS_INIT {
                            self.hook.on_init(&key, &new_value[..]);
                        }
                        count += 1;
                    }
                    MigrateAction::Delete => {
                        self.delete_no_hook(&key)?;
                        count += 1;
                    }
                }
            }
        }

        tracing::info!(mutations = count, "const_map migration complete");
        Ok(count)
    }

    /// Replay `on_init` for every live entry. Used when no migration runs.
    ///
    /// Lock order: snapshot entries under per-shard index lock, release the
    /// lock, then call `on_init` outside the lock. This allows hooks to
    /// re-enter the same collection and shard without deadlocking.
    pub(crate) fn replay_init(&self) {
        if !H::NEEDS_INIT {
            return;
        }
        for shard in &self.indexes {
            let entries: Vec<(K, [u8; V])> = {
                let index = sync::lock(shard);
                index.iter().map(|(k, e)| (*k, e.value)).collect()
            };
            for (key, value) in entries {
                self.hook.on_init(&key, &value[..]);
            }
        }
    }
}

impl<K: Key + Send + Sync + Hash + Eq, const V: usize, H: WriteHook<K>, D: Durability> ShardSource
    for ConstMap<K, V, H, D>
{
    type Key = K;
    type Val = [u8; V];

    fn shard_count(&self) -> usize {
        self.durability.shard_count()
    }

    fn next_batch(
        &self,
        shard: usize,
        after: Option<&K>,
        dir: Dir,
        n: usize,
        range_start: Bound<crate::map_index::OrdKey<K>>,
        range_end: Bound<crate::map_index::OrdKey<K>>,
    ) -> Vec<(K, [u8; V])> {
        let index = sync::lock(&self.indexes[shard]);
        let Some(ki) = index.key_index() else {
            return Vec::new();
        };
        let keys = key_index_batch(ki, after, dir, n, range_start, range_end);
        let mut out = Vec::with_capacity(keys.len());
        for k in keys {
            if let Some(e) = index.get(&k) {
                out.push((k, e.value));
            }
        }
        out
    }

    fn next_keys_batch(
        &self,
        shard: usize,
        after: Option<&K>,
        dir: Dir,
        n: usize,
        range_start: Bound<crate::map_index::OrdKey<K>>,
        range_end: Bound<crate::map_index::OrdKey<K>>,
    ) -> Vec<K> {
        let index = sync::lock(&self.indexes[shard]);
        let Some(ki) = index.key_index() else {
            return Vec::new();
        };
        key_index_batch(ki, after, dir, n, range_start, range_end)
    }
}

/// View over an iterable [`ConstMap`]; exposes ordered iteration over
/// `(K, [u8; V])` entries and cheap keys-only iteration. Obtained via
/// [`ConstMap::iter_view`].
pub struct ConstMapIterView<'a, K, const V: usize, H = NoHook, D = Bitcask>
where
    K: Key + Send + Sync + Hash + Eq,
    H: WriteHook<K>,
    D: Durability,
{
    inner: MapIterView<'a, ConstMap<K, V, H, D>>,
}

impl<K, const V: usize, H, D> ConstMapIterView<'_, K, V, H, D>
where
    K: Key + Send + Sync + Hash + Eq,
    H: WriteHook<K>,
    D: Durability,
{
    /// Full ordered scan in the collection's configured direction.
    pub fn iter(&self) -> impl Iterator<Item = (K, [u8; V])> + '_ {
        self.inner.iter()
    }

    /// Iterate entries in `[start, end)` — start inclusive, end exclusive.
    pub fn range(&self, start: &K, end: &K) -> impl Iterator<Item = (K, [u8; V])> + '_ {
        self.inner.range(start, end)
    }

    /// Iterate entries in range defined by `start` and `end` bounds.
    pub fn range_bounds(
        &self,
        start: Bound<&K>,
        end: Bound<&K>,
    ) -> impl Iterator<Item = (K, [u8; V])> + '_ {
        self.inner.range_bounds(start, end)
    }

    /// Iterate entries whose keys start with `prefix`.
    pub fn prefix_iter(&self, prefix: &[u8]) -> impl Iterator<Item = (K, [u8; V])> + '_ {
        self.inner.prefix_iter(prefix)
    }

    /// Keyset pagination in the collection's configured direction.
    pub fn paginate(&self, after: Option<&K>, limit: usize) -> Page<K, [u8; V]> {
        self.inner.paginate(after, limit)
    }

    /// Full ordered **keys-only** scan — no value materialized.
    pub fn keys(&self) -> impl Iterator<Item = K> + '_ {
        self.inner.keys()
    }

    /// Keys-only iteration over `[start, end)`.
    pub fn keys_range(&self, start: &K, end: &K) -> impl Iterator<Item = K> + '_ {
        self.inner.keys_range(start, end)
    }

    /// Keys-only iteration over a bounded range.
    pub fn keys_range_bounds(
        &self,
        start: Bound<&K>,
        end: Bound<&K>,
    ) -> impl Iterator<Item = K> + '_ {
        self.inner.keys_range_bounds(start, end)
    }

    /// Keys-only iteration over keys starting with `prefix`.
    pub fn keys_prefix(&self, prefix: &[u8]) -> impl Iterator<Item = K> + '_ {
        self.inner.keys_prefix(prefix)
    }

    /// Keys-only pagination — cheap, materializes no values.
    pub fn keys_paginate(&self, after: Option<&K>, limit: usize) -> KeyPage<K> {
        self.inner.keys_paginate(after, limit)
    }

    /// Keep entries where `f` returns `true`; delete the rest via
    /// [`ConstMap::compare_delete`] using the value observed during iteration.
    ///
    /// If the value changed between iteration and delete (`CasMismatch`) or the
    /// key was removed concurrently (`KeyNotFound`), the delete is skipped (R5).
    /// Not atomic across the collection; a genuine storage error aborts the scan
    /// and is returned.
    pub fn retain(&self, mut f: impl FnMut(&K, &[u8; V]) -> bool) -> DbResult<()> {
        for (key, value) in self.inner.iter() {
            if f(&key, &value) {
                continue;
            }
            match self.inner.src().compare_delete(&key, &value) {
                Ok(()) => {}
                Err(DbError::CasMismatch) | Err(DbError::KeyNotFound) => {
                    tracing::debug!("retain: compare_delete skipped (concurrent change)");
                }
                Err(e) => return Err(e),
            }
        }
        Ok(())
    }
}

// ==========================================================================
// Replication (Bitcask only — uses DiskLoc)
// ==========================================================================

#[cfg(feature = "replication")]
impl<K: Key + Send + Sync + Hash + Eq, const V: usize, H: WriteHook<K>>
    crate::replication::ReplicationTarget for ConstMap<K, V, H, Bitcask>
{
    fn apply_entry(
        &self,
        _shard_inner: &mut crate::shard::ShardInner,
        _shard_id: u8,
        file_id: u32,
        entry_offset: u64,
        header: &crate::entry::EntryHeader,
        key: &[u8],
        value: &[u8],
    ) -> DbResult<crate::replication::ApplyOutcome> {
        use crate::replication::ApplyOutcome;

        let key = K::from_bytes(key);

        let value_offset =
            entry_offset + size_of::<crate::entry::EntryHeader>() as u64 + size_of::<K>() as u64;
        let disk = DiskLoc::new(file_id, value_offset as u32, header.value_len);

        if header.is_tombstone() {
            let old = sync::lock(&self.indexes[self.shard_for(&key)]).remove(&key);
            match old {
                Some(old_entry) => Ok(ApplyOutcome::TombstoneRemoved(old_entry.loc)),
                None => Ok(ApplyOutcome::Inserted), // no-op tombstone — no dead bytes
            }
        } else {
            let value: [u8; V] = value.try_into().map_err(|_| DbError::CorruptedEntry {
                offset: entry_offset,
            })?;
            let old = sync::lock(&self.indexes[self.shard_for(&key)])
                .upsert(key, MapEntry { loc: disk, value });
            match old {
                Some(old_entry) => Ok(ApplyOutcome::Replaced(old_entry.loc)),
                None => Ok(ApplyOutcome::Inserted),
            }
        }
    }

    fn try_apply_entry(
        &self,
        shard_inner: &mut crate::shard::ShardInner,
        shard_id: u8,
        file_id: u32,
        entry_offset: u64,
        header: &crate::entry::EntryHeader,
        raw_after_header: &[u8],
    ) -> DbResult<crate::replication::ApplyOutcome> {
        use crate::replication::ApplyOutcome;

        let value_len = header.value_len as usize;
        if raw_after_header.len() < size_of::<K>() + value_len {
            return Ok(ApplyOutcome::NotMatched);
        }
        let key = &raw_after_header[..size_of::<K>()];
        let value = &raw_after_header[size_of::<K>()..size_of::<K>() + value_len];
        let crc = crate::entry::compute_crc32(header.gsn, header.value_len, key, value);
        if crc != header.crc32 {
            return Ok(ApplyOutcome::NotMatched);
        }
        self.apply_entry(
            shard_inner,
            shard_id,
            file_id,
            entry_offset,
            header,
            key,
            value,
        )
    }

    fn key_len(&self) -> usize {
        size_of::<K>()
    }
}

// ==========================================================================
// ConstMapShard — generic over D: Durability
// ==========================================================================

type ConstShardEvent<K, const V: usize> = (K, Option<[u8; V]>, Option<[u8; V]>);

/// Handle for atomic multi-key operations within a single shard.
/// Obtained via [`ConstMap::atomic`]. The shard + index locks are held for the
/// lifetime of this struct — keep the closure short.
pub struct ConstMapShard<
    'a,
    K: Key + Send + Sync + Hash + Eq,
    const V: usize,
    H: WriteHook<K> = NoHook,
    D: Durability = Bitcask,
> {
    tree: &'a ConstMap<K, V, H, D>,
    inner: MutexGuard<'a, D::Inner>,
    index: MutexGuard<'a, ShardIndex<K, MapEntry<V, D::Loc>>>,
    shard_id: usize,
    events: Vec<ConstShardEvent<K, V>>,
}

impl<K: Key + Send + Sync + Hash + Eq, const V: usize, H: WriteHook<K>, D: Durability>
    ConstMapShard<'_, K, V, H, D>
{
    pub fn put(&mut self, key: &K, value: &[u8; V]) -> DbResult<Option<[u8; V]>> {
        self.check_shard(key)?;
        let old =
            self.tree
                .put_locked(self.shard_id, &mut *self.inner, &mut self.index, key, value)?;
        if H::NEEDS_WRITE {
            self.events.push((*key, old, Some(*value)));
        }
        Ok(old)
    }

    pub fn insert(&mut self, key: &K, value: &[u8; V]) -> DbResult<()> {
        self.check_shard(key)?;
        self.tree
            .insert_locked(self.shard_id, &mut *self.inner, &mut self.index, key, value)?;
        if H::NEEDS_WRITE {
            self.events.push((*key, None, Some(*value)));
        }
        Ok(())
    }

    pub fn delete(&mut self, key: &K) -> DbResult<Option<[u8; V]>> {
        self.check_shard(key)?;
        let old = self
            .tree
            .delete_locked(self.shard_id, &mut *self.inner, &mut self.index, key)?;
        if H::NEEDS_WRITE
            && let Some(ref old_val) = old
        {
            self.events.push((*key, Some(*old_val), None));
        }
        Ok(old)
    }

    /// Read-modify-write within this shard: applies `f` to the current value and
    /// stores the result. Returns `Some(new_value)` if the key existed, `None`
    /// otherwise (closure not invoked). The closure must not be heavy — the shard
    /// + index locks are held.
    pub fn update(
        &mut self,
        key: &K,
        f: impl FnOnce(&[u8; V]) -> [u8; V],
    ) -> DbResult<Option<[u8; V]>> {
        self.update_inner(key, f, false)
    }

    /// Like [`update()`](Self::update), but returns `Some(old_value)` instead of
    /// the new one.
    pub fn fetch_update(
        &mut self,
        key: &K,
        f: impl FnOnce(&[u8; V]) -> [u8; V],
    ) -> DbResult<Option<[u8; V]>> {
        self.update_inner(key, f, true)
    }

    fn update_inner(
        &mut self,
        key: &K,
        f: impl FnOnce(&[u8; V]) -> [u8; V],
        return_old: bool,
    ) -> DbResult<Option<[u8; V]>> {
        self.check_shard(key)?;
        let (old_value, new_value) = match self.tree.update_locked(
            self.shard_id,
            &mut *self.inner,
            &mut self.index,
            key,
            f,
        )? {
            Some(pair) => pair,
            None => return Ok(None),
        };
        if H::NEEDS_WRITE {
            self.events.push((*key, Some(old_value), Some(new_value)));
        }
        Ok(Some(if return_old { old_value } else { new_value }))
    }

    pub fn get(&self, key: &K) -> Option<[u8; V]> {
        self.index.get(key).map(|e| e.value)
    }

    pub fn get_or_err(&self, key: &K) -> DbResult<[u8; V]> {
        self.get(key).ok_or(DbError::KeyNotFound)
    }

    pub fn contains(&self, key: &K) -> bool {
        self.index.contains_key(key)
    }

    fn check_shard(&self, key: &K) -> DbResult<()> {
        if self.tree.shard_for(key) != self.shard_id {
            return Err(DbError::ShardMismatch);
        }
        Ok(())
    }
}

// ==========================================================================
// MultiTx — cross-collection transaction support (feature `armour`)
// ==========================================================================

#[cfg(feature = "armour")]
type ConstMapGuard<'a, K, const V: usize, D> = (
    usize,
    MutexGuard<'a, <D as Durability>::Inner>,
    MutexGuard<'a, ShardIndex<K, MapEntry<V, <D as Durability>::Loc>>>,
);

/// Multi-shard transaction handle for [`ConstMap`] inside `Db::atomicN`. Unlike
/// the tree handle it stores a `(durability inner, index HashMap)` guard pair per
/// shard and has no epoch guard.
#[cfg(feature = "armour")]
pub struct ConstMapTx<'a, K, const V: usize, H = NoHook, D = Bitcask>
where
    K: Key + Send + Sync + Hash + Eq,
    H: WriteHook<K>,
    D: Durability,
{
    tree: &'a ConstMap<K, V, H, D>,
    shards: Vec<ConstMapGuard<'a, K, V, D>>,
    log: Vec<ConstShardEvent<K, V>>,
}

#[cfg(feature = "armour")]
impl<'a, K, const V: usize, H, D> ConstMapTx<'a, K, V, H, D>
where
    K: Key + Send + Sync + Hash + Eq,
    H: WriteHook<K>,
    D: Durability,
{
    fn position(&self, key: &K) -> DbResult<usize> {
        let sid = self.tree.shard_for(key);
        self.shards
            .iter()
            .position(|(s, _, _)| *s == sid)
            .ok_or(DbError::ShardMismatch)
    }

    pub fn try_get(&self, key: &K) -> DbResult<Option<[u8; V]>> {
        let i = self.position(key)?;
        Ok(self.shards[i].2.get(key).map(|e| e.value))
    }

    pub fn try_contains(&self, key: &K) -> DbResult<bool> {
        let i = self.position(key)?;
        Ok(self.shards[i].2.contains_key(key))
    }

    pub fn get_or_err(&self, key: &K) -> DbResult<[u8; V]> {
        self.try_get(key)?.ok_or(DbError::KeyNotFound)
    }

    pub fn put(&mut self, key: &K, value: &[u8; V]) -> DbResult<Option<[u8; V]>> {
        let i = self.position(key)?;
        let (sid, inner, index) = &mut self.shards[i];
        let old = self
            .tree
            .put_locked(*sid, &mut **inner, &mut **index, key, value)?;
        if H::NEEDS_WRITE {
            self.log.push((*key, old, Some(*value)));
        }
        Ok(old)
    }

    pub fn insert(&mut self, key: &K, value: &[u8; V]) -> DbResult<()> {
        let i = self.position(key)?;
        let (sid, inner, index) = &mut self.shards[i];
        self.tree
            .insert_locked(*sid, &mut **inner, &mut **index, key, value)?;
        if H::NEEDS_WRITE {
            self.log.push((*key, None, Some(*value)));
        }
        Ok(())
    }

    pub fn delete(&mut self, key: &K) -> DbResult<Option<[u8; V]>> {
        let i = self.position(key)?;
        let (sid, inner, index) = &mut self.shards[i];
        let old = self
            .tree
            .delete_locked(*sid, &mut **inner, &mut **index, key)?;
        if H::NEEDS_WRITE
            && let Some(ref old_val) = old
        {
            self.log.push((*key, Some(*old_val), None));
        }
        Ok(old)
    }
}

#[cfg(feature = "armour")]
impl<K, const V: usize, H, D> crate::armour::multi_tx::MultiTx for ConstMap<K, V, H, D>
where
    K: Key + Send + Sync + Hash + Eq,
    H: WriteHook<K>,
    D: Durability,
{
    type Key = K;
    type Tx<'a>
        = ConstMapTx<'a, K, V, H, D>
    where
        Self: 'a;

    fn shard_for_key(&self, key: &K) -> usize {
        self.shard_for(key)
    }

    fn begin_tx(&self) -> ConstMapTx<'_, K, V, H, D> {
        ConstMapTx {
            tree: self,
            shards: Vec::new(),
            log: Vec::new(),
        }
    }

    fn lock_shard_into<'a>(&'a self, shard_id: usize, tx: &mut ConstMapTx<'a, K, V, H, D>) {
        // Lock durability inner first, then the per-shard index — SAME order the
        // single-shard atomic() uses, so a concurrent atomic() on this shard cannot
        // invert against us.
        let inner = self.durability.lock_shard(shard_id);
        let index = sync::lock(&self.indexes[shard_id]);
        tx.shards.push((shard_id, inner, index));
    }

    fn release_locks(
        &self,
        tx: &mut ConstMapTx<'_, K, V, H, D>,
    ) -> crate::armour::multi_tx::SyncNeeds {
        let mut needs = crate::armour::multi_tx::SyncNeeds::none();
        for (sid, inner, _) in &tx.shards {
            if inner.should_sync() {
                needs.push(*sid);
            }
        }
        tx.shards.clear(); // drops index + inner guards
        needs
    }

    fn run_sync(&self, needs: crate::armour::multi_tx::SyncNeeds) -> DbResult<()> {
        for &sid in needs.shards() {
            self.durability.lock_shard(sid).sync()?;
        }
        Ok(())
    }

    fn replay_hooks(&self, tx: ConstMapTx<'_, K, V, H, D>) {
        if H::NEEDS_WRITE {
            for (k, old, new) in &tx.log {
                self.hook.on_write(
                    k,
                    old.as_ref().map(|v| &v[..]),
                    new.as_ref().map(|v| &v[..]),
                );
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Config;
    use crate::hook::WriteHook;
    use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering, Ordering as AtomicOrdering};
    use std::sync::mpsc;
    use std::time::Duration;
    use tempfile::tempdir;

    #[derive(Default)]
    struct RecHook {
        writes: AtomicUsize,
        #[allow(clippy::type_complexity)]
        seq: crate::sync::Mutex<Vec<(u64, Option<Vec<u8>>, Option<Vec<u8>>)>>,
    }
    impl WriteHook<[u8; 8]> for RecHook {
        const NEEDS_OLD_VALUE: bool = true;
        fn on_write(&self, key: &[u8; 8], old: Option<&[u8]>, new: Option<&[u8]>) {
            self.writes.fetch_add(1, AtomicOrdering::Relaxed);
            crate::sync::lock(&self.seq).push((
                u64::from_be_bytes(*key),
                old.map(<[u8]>::to_vec),
                new.map(<[u8]>::to_vec),
            ));
        }
    }

    fn open_map_hooked(dir: &std::path::Path, hook: RecHook) -> ConstMap<[u8; 8], 4, RecHook> {
        let mut cfg = Config::test();
        cfg.shard_count = 1;
        ConstMap::open_hooked(dir, cfg, hook).expect("open hooked")
    }

    #[test]
    fn iter_view_none_unless_iterable() {
        let dir = tempfile::tempdir().unwrap();
        let m = ConstMap::<[u8; 2], 1>::open(dir.path(), Config::test()).unwrap();
        assert!(m.iter_view().is_none());
    }

    #[test]
    fn iter_desc_by_default_across_shards() {
        let dir = tempfile::tempdir().unwrap();
        let cfg = Config::balanced()
            .shard_count(4)
            .hints(true)
            .iterable(true)
            .build();
        let m = ConstMap::<[u8; 2], 1>::open(dir.path(), cfg).unwrap();
        for k in [[1u8, 0], [3, 0], [2, 0], [5, 0], [4, 0]] {
            m.insert(&k, &[7]).unwrap();
        }
        let got: Vec<[u8; 2]> = m.iter_view().unwrap().iter().map(|(k, _)| k).collect();
        assert_eq!(got, [[5, 0], [4, 0], [3, 0], [2, 0], [1, 0]]);
    }

    #[test]
    fn paginate_covers_keyset_once_desc() {
        let dir = tempfile::tempdir().unwrap();
        let cfg = Config::balanced()
            .shard_count(3)
            .hints(true)
            .iterable(true)
            .build();
        let m = ConstMap::<[u8; 2], 1>::open(dir.path(), cfg).unwrap();
        for i in 1u8..=10 {
            m.insert(&[i, 0], &[i]).unwrap();
        }
        let view = m.iter_view().unwrap();
        let mut seen = Vec::new();
        let mut after: Option<[u8; 2]> = None;
        loop {
            let page = view.paginate(after.as_ref(), 3);
            if page.items.is_empty() {
                break;
            }
            seen.extend(page.items.iter().map(|(k, _)| *k));
            after = page.next;
            if after.is_none() {
                break;
            }
        }
        let mut sorted = seen.clone();
        sorted.sort();
        sorted.dedup();
        assert_eq!(sorted.len(), 10, "every key exactly once");
        assert_eq!(seen.first().unwrap(), &[10, 0]);
    }

    #[test]
    fn range_and_prefix_bounds() {
        let dir = tempfile::tempdir().unwrap();
        let cfg = Config::balanced()
            .shard_count(2)
            .hints(true)
            .iterable(true)
            .reversed(false)
            .build();
        let m = ConstMap::<[u8; 2], 1>::open(dir.path(), cfg).unwrap();
        for i in 1u8..=5 {
            m.insert(&[i, 0], &[i]).unwrap();
        }
        let v = m.iter_view().unwrap();
        let r: Vec<_> = v.range(&[2, 0], &[4, 0]).map(|(k, _)| k).collect();
        assert_eq!(r, [[2, 0], [3, 0]]);
        // value prefix_iter (exercises prefix_to_end_bound on the value path).
        let p: Vec<[u8; 2]> = v.prefix_iter(&[3]).map(|(k, _)| k).collect();
        assert_eq!(p, [[3, 0]]);
    }

    #[test]
    fn degenerate_range_is_empty_not_panic() {
        let dir = tempfile::tempdir().unwrap();
        let cfg = Config::balanced()
            .shard_count(2)
            .hints(true)
            .iterable(true)
            .reversed(false)
            .build();
        let m = ConstMap::<[u8; 2], 1>::open(dir.path(), cfg).unwrap();
        for i in 1u8..=5 {
            m.insert(&[i, 0], &[i]).unwrap();
        }
        let v = m.iter_view().unwrap();
        // Excluded == Excluded (empty) and inverted bounds must not panic.
        assert_eq!(
            v.range_bounds(Bound::Excluded(&[3, 0]), Bound::Excluded(&[3, 0]))
                .count(),
            0
        );
        assert_eq!(
            v.keys_range_bounds(Bound::Included(&[4, 0]), Bound::Included(&[2, 0]))
                .count(),
            0
        );
    }

    #[test]
    fn keys_only_matches_iter_keys_and_is_ordered() {
        let dir = tempfile::tempdir().unwrap();
        let cfg = Config::balanced()
            .shard_count(3)
            .hints(true)
            .iterable(true)
            .build();
        let m = ConstMap::<[u8; 2], 1>::open(dir.path(), cfg).unwrap();
        for i in 1u8..=6 {
            m.insert(&[i, 0], &[i]).unwrap();
        }
        let v = m.iter_view().unwrap();
        let from_keys: Vec<[u8; 2]> = v.keys().collect();
        let from_iter: Vec<[u8; 2]> = v.iter().map(|(k, _)| k).collect();
        assert_eq!(from_keys, from_iter, "keys() order must match iter() keys");
        assert_eq!(
            from_keys,
            [[6, 0], [5, 0], [4, 0], [3, 0], [2, 0], [1, 0]],
            "DESC by default"
        );
    }

    #[test]
    fn keys_range_and_prefix() {
        let dir = tempfile::tempdir().unwrap();
        let cfg = Config::balanced()
            .shard_count(2)
            .hints(true)
            .iterable(true)
            .reversed(false)
            .build();
        let m = ConstMap::<[u8; 2], 1>::open(dir.path(), cfg).unwrap();
        for i in 1u8..=5 {
            m.insert(&[i, 0], &[i]).unwrap();
        }
        let v = m.iter_view().unwrap();
        let r: Vec<[u8; 2]> = v.keys_range(&[2, 0], &[4, 0]).collect();
        assert_eq!(r, [[2, 0], [3, 0]]);
        let p: Vec<[u8; 2]> = v.keys_prefix(&[3]).collect();
        assert_eq!(p, [[3, 0]]);
    }

    #[test]
    fn keys_paginate_covers_keyset_once_desc() {
        let dir = tempfile::tempdir().unwrap();
        let cfg = Config::balanced()
            .shard_count(3)
            .hints(true)
            .iterable(true)
            .build();
        let m = ConstMap::<[u8; 2], 1>::open(dir.path(), cfg).unwrap();
        for i in 1u8..=10 {
            m.insert(&[i, 0], &[i]).unwrap();
        }
        let v = m.iter_view().unwrap();
        let mut seen = Vec::new();
        let mut after: Option<[u8; 2]> = None;
        loop {
            let page = v.keys_paginate(after.as_ref(), 3);
            if page.keys.is_empty() {
                break;
            }
            seen.extend(page.keys.iter().copied());
            after = page.next;
            if after.is_none() {
                break;
            }
        }
        let mut sorted = seen.clone();
        sorted.sort();
        sorted.dedup();
        assert_eq!(sorted.len(), 10, "every key exactly once");
        assert_eq!(
            seen.first().unwrap(),
            &[10, 0],
            "DESC starts at the max key"
        );
    }

    /// R2: the Fixed replication apply helpers (`upsert_replicated` /
    /// `remove_key_if_slot_matches`) must keep the iterable companion in sync, so a
    /// follower opened `iterable(true)` reflects the replicated keyset.
    #[cfg(feature = "replication")]
    #[test]
    fn replication_apply_maintains_iterable_companion_fixed() {
        let dir = tempfile::tempdir().unwrap();
        let mut cfg = crate::FixedConfig::test();
        cfg.iterable = true;
        let m = crate::FixedMap::<[u8; 2], 1>::open(dir.path(), cfg).unwrap();

        // Simulate a follower applying replicated upserts (slots bypass durability).
        m.upsert_replicated(&[1, 0], [10], 0);
        m.upsert_replicated(&[2, 0], [20], 1);
        m.upsert_replicated(&[3, 0], [30], 2);

        let keys: Vec<[u8; 2]> = m.iter_view().unwrap().keys().collect();
        assert_eq!(
            keys,
            [[3, 0], [2, 0], [1, 0]],
            "all replicated keys present (Fixed is DESC)"
        );

        // Apply a replicated removal — companion must drop the key.
        let slot = m.get_slot_id(&[2, 0]).unwrap();
        assert!(m.remove_key_if_slot_matches(&[2, 0], slot));
        let keys: Vec<[u8; 2]> = m.iter_view().unwrap().keys().collect();
        assert_eq!(
            keys,
            [[3, 0], [1, 0]],
            "replicated tombstone removed from companion"
        );
    }

    #[test]
    fn iter_reflects_delete() {
        let dir = tempfile::tempdir().unwrap();
        let cfg = Config::balanced()
            .shard_count(2)
            .hints(true)
            .iterable(true)
            .build();
        let m = ConstMap::<[u8; 2], 1>::open(dir.path(), cfg).unwrap();
        m.insert(&[1, 0], &[1]).unwrap();
        m.insert(&[2, 0], &[2]).unwrap();
        m.delete(&[1, 0]).unwrap();
        let got: Vec<[u8; 2]> = m.iter_view().unwrap().iter().map(|(k, _)| k).collect();
        assert_eq!(got, [[2, 0]]);
    }

    #[test]
    fn const_map_atomic_fires_hooks_in_order() {
        let dir = tempfile::tempdir().unwrap();
        let map = open_map_hooked(dir.path(), RecHook::default());
        let k = 7u64.to_be_bytes();
        map.atomic(&k, |s| {
            s.put(&k, &[1, 1, 1, 1])?;
            s.put(&k, &[2, 2, 2, 2])?;
            s.delete(&k)?;
            Ok(())
        })
        .expect("atomic");
        assert_eq!(map.hook.writes.load(AtomicOrdering::Relaxed), 3);
        let seq = crate::sync::lock(&map.hook.seq).clone();
        assert_eq!(seq[0], (7, None, Some(vec![1, 1, 1, 1])));
        assert_eq!(seq[1], (7, Some(vec![1, 1, 1, 1]), Some(vec![2, 2, 2, 2])));
        assert_eq!(seq[2], (7, Some(vec![2, 2, 2, 2]), None));
    }

    #[test]
    fn const_map_atomic_fires_for_applied_on_err() {
        let dir = tempfile::tempdir().unwrap();
        let map = open_map_hooked(dir.path(), RecHook::default());
        let k = 1u64.to_be_bytes();
        let r: DbResult<()> = map.atomic(&k, |s| {
            s.put(&k, &[9, 9, 9, 9])?;
            Err(DbError::KeyNotFound)
        });
        assert!(r.is_err());
        assert_eq!(map.hook.writes.load(AtomicOrdering::Relaxed), 1);
    }

    #[test]
    fn const_map_atomic_fires_hooks_fixedstore_sync_seam() {
        // FixedStore is the one family where `should_sync()` can be true, so
        // `atomic()` runs `sync()?` BEFORE replaying hooks. Force the seam with
        // `sync_batch_size = 1` (every write trips it) and assert hooks still
        // fire, in application order, after the sync.
        let dir = tempdir().unwrap();
        let fixed_cfg = crate::FixedConfig {
            shard_count: 1,
            grow_step: 64,
            sync_batch_size: 1,
            ..crate::FixedConfig::test()
        };
        let map = crate::FixedMap::<[u8; 8], 4, RecHook>::open_with_hook(
            dir.path(),
            fixed_cfg,
            RecHook::default(),
        )
        .expect("open fixed hooked");

        let k = 7u64.to_be_bytes();
        map.atomic(&k, |s| {
            s.put(&k, &[1, 1, 1, 1])?;
            s.put(&k, &[2, 2, 2, 2])?;
            s.delete(&k)?;
            Ok(())
        })
        .expect("atomic");

        assert_eq!(map.hook.writes.load(AtomicOrdering::Relaxed), 3);
        let seq = crate::sync::lock(&map.hook.seq).clone();
        assert_eq!(seq[0], (7, None, Some(vec![1, 1, 1, 1])));
        assert_eq!(seq[1], (7, Some(vec![1, 1, 1, 1]), Some(vec![2, 2, 2, 2])));
        assert_eq!(seq[2], (7, Some(vec![2, 2, 2, 2]), None));
    }

    #[allow(clippy::type_complexity)]
    struct ReplayInitReenterHook<const V: usize> {
        target: sync::Mutex<Option<std::sync::Arc<ConstMap<[u8; 8], V, Self>>>>,
        entered: AtomicBool,
    }

    impl<const V: usize> WriteHook<[u8; 8]> for ReplayInitReenterHook<V> {
        const NEEDS_OLD_VALUE: bool = false;
        const NEEDS_INIT: bool = true;
        const NEEDS_WRITE: bool = false;

        fn on_write(&self, _key: &[u8; 8], _old: Option<&[u8]>, _new: Option<&[u8]>) {}

        fn on_init(&self, key: &[u8; 8], _value: &[u8]) {
            self.entered.store(true, Ordering::Release);
            let map = sync::lock(&self.target)
                .as_ref()
                .expect("hook target must be installed")
                .clone();
            let _ = map.get(key);
        }
    }

    #[test]
    fn hook_lifecycle_replay_init_reentry_const_map() {
        let dir = tempdir().unwrap();
        let hook = ReplayInitReenterHook::<8> {
            target: sync::Mutex::new(None),
            entered: AtomicBool::new(false),
        };
        let map = std::sync::Arc::new(
            ConstMap::<[u8; 8], 8, ReplayInitReenterHook<8>>::open_hooked(
                dir.path(),
                Config::test(),
                hook,
            )
            .unwrap(),
        );
        *sync::lock(&map.hook.target) = Some(std::sync::Arc::clone(&map));

        let key = 1u64.to_be_bytes();
        map.put(&key, &[0u8; 8]).unwrap();

        let (tx, rx) = mpsc::sync_channel(1);
        let map2 = std::sync::Arc::clone(&map);
        std::thread::spawn(move || {
            map2.replay_init();
            let _ = tx.send(());
        });
        rx.recv_timeout(Duration::from_secs(2))
            .expect("replay_init deadlocked — on_init called with index lock held");
        assert!(map.hook.entered.load(Ordering::Acquire));
    }

    #[test]
    fn retain_deletes_and_persists() {
        let dir = tempfile::tempdir().unwrap();
        let cfg = Config::balanced()
            .shard_count(2)
            .hints(true)
            .iterable(true)
            .build();
        {
            let m = ConstMap::<[u8; 2], 1>::open(dir.path(), cfg.clone()).unwrap();
            for i in 1u8..=6 {
                m.insert(&[i, 0], &[i]).unwrap();
            }
            m.iter_view()
                .unwrap()
                .retain(|_k, v| v[0] % 2 == 0)
                .unwrap();
            let got: Vec<_> = m.iter_view().unwrap().iter().map(|(k, _)| k[0]).collect();
            let mut g = got.clone();
            g.sort();
            assert_eq!(g, [2, 4, 6]);
            m.close().unwrap();
        }
        let m = ConstMap::<[u8; 2], 1>::open(dir.path(), cfg).unwrap();
        let got: Vec<_> = m.iter_view().unwrap().iter().map(|(k, _)| k[0]).collect();
        let mut g = got.clone();
        g.sort();
        assert_eq!(g, [2, 4, 6]);
    }

    #[test]
    fn retain_compare_delete_skips_changed_value() {
        let dir = tempfile::tempdir().unwrap();
        let cfg = Config::balanced()
            .shard_count(2)
            .hints(true)
            .iterable(true)
            .build();
        let m = std::sync::Arc::new(ConstMap::<[u8; 2], 1>::open(dir.path(), cfg).unwrap());
        m.insert(&[1, 0], &[1]).unwrap();

        let m2 = std::sync::Arc::clone(&m);
        m.iter_view()
            .unwrap()
            .retain(|k, _v| {
                if k == &[1, 0] {
                    m2.put(k, &[9]).unwrap();
                    return false;
                }
                true
            })
            .unwrap();

        assert_eq!(m.get(&[1, 0]), Some([9]));
    }

    #[test]
    fn const_map_shard_update_and_fetch_update() {
        let dir = tempdir().unwrap();
        let mut cfg = Config::test();
        cfg.shard_count = 1;
        let map = ConstMap::<[u8; 8], 4>::open(dir.path(), cfg).unwrap();
        let key = 1u64.to_be_bytes();
        map.put(&key, &[10u8; 4]).unwrap();

        let (upd, fetched, missing) = map
            .atomic(&key, |shard| {
                let upd = shard.update(&key, |old| {
                    let mut v = *old;
                    v[0] = v[0].wrapping_add(1);
                    v
                })?;
                let fetched = shard.fetch_update(&key, |old| {
                    let mut v = *old;
                    v[0] = v[0].wrapping_add(1);
                    v
                })?;
                let missing = shard.update(&2u64.to_be_bytes(), |_| [0u8; 4])?;
                Ok((upd, fetched, missing))
            })
            .expect("atomic");

        assert_eq!(upd, Some([11, 10, 10, 10]));
        assert_eq!(fetched, Some([11, 10, 10, 10]));
        assert_eq!(missing, None);
        assert_eq!(map.get(&key), Some([12, 10, 10, 10]));
    }

    #[test]
    fn const_map_atomic_update_fires_hook_with_old_new() {
        let dir = tempdir().unwrap();
        let map = open_map_hooked(dir.path(), RecHook::default());
        let key = 1u64.to_be_bytes();
        map.put(&key, &[10u8; 4]).unwrap();
        // put fired hook once
        assert_eq!(map.hook.writes.load(AtomicOrdering::Relaxed), 1);

        map.atomic(&key, |shard| {
            shard.update(&key, |old| {
                let mut v = *old;
                v[0] = v[0].wrapping_add(5);
                v
            })
        })
        .unwrap();

        // one more write event from update
        assert_eq!(map.hook.writes.load(AtomicOrdering::Relaxed), 2);
        assert_eq!(map.get(&key), Some([15, 10, 10, 10]));
    }

    #[test]
    fn compare_delete_updates_companion() {
        let dir = tempfile::tempdir().unwrap();
        let cfg = Config::balanced()
            .shard_count(2)
            .hints(true)
            .iterable(true)
            .build();
        let m = ConstMap::<[u8; 2], 1>::open(dir.path(), cfg).unwrap();
        m.insert(&[1, 0], &[9]).unwrap();
        m.compare_delete(&[1, 0], &[9]).unwrap();
        assert_eq!(m.iter_view().unwrap().iter().count(), 0);
    }

    #[test]
    fn fixed_map_iteration_direction() {
        // default reversed=true -> DESC
        let dir = tempdir().unwrap();
        let cfg = crate::FixedConfig {
            iterable: true,
            ..crate::FixedConfig::test()
        };
        let m = crate::FixedMap::<[u8; 8], 8>::open(dir.path(), cfg).unwrap();
        for i in 1u64..=3 {
            m.put(&i.to_be_bytes(), &i.to_be_bytes()).unwrap();
        }
        let desc: Vec<u64> = m
            .iter_view()
            .unwrap()
            .keys()
            .map(u64::from_be_bytes)
            .collect();
        assert_eq!(desc, [3, 2, 1], "FixedMap default DESC");

        // reversed=false -> ASC
        let dir2 = tempdir().unwrap();
        let cfg2 = crate::FixedConfig {
            iterable: true,
            reversed: false,
            ..crate::FixedConfig::test()
        };
        let m2 = crate::FixedMap::<[u8; 8], 8>::open(dir2.path(), cfg2).unwrap();
        for i in 1u64..=3 {
            m2.put(&i.to_be_bytes(), &i.to_be_bytes()).unwrap();
        }
        let asc: Vec<u64> = m2
            .iter_view()
            .unwrap()
            .keys()
            .map(u64::from_be_bytes)
            .collect();
        assert_eq!(asc, [1, 2, 3], "FixedMap reversed=false ASC");
    }

    #[test]
    fn compare_delete_fixed_store_match_mismatch_absent() {
        let dir = tempdir().unwrap();
        let map =
            crate::FixedMap::<[u8; 8], 8>::open(dir.path(), crate::FixedConfig::test()).unwrap();

        let key = 1u64.to_be_bytes();
        let val = 42u64.to_be_bytes();
        let other = 99u64.to_be_bytes();
        map.put(&key, &val).unwrap();

        assert!(matches!(
            map.compare_delete(&key, &other),
            Err(DbError::CasMismatch)
        ));
        assert_eq!(map.get(&key), Some(val));
        assert!(map.compare_delete(&key, &val).is_ok());
        assert_eq!(map.get(&key), None);
        assert!(matches!(
            map.compare_delete(&key, &val),
            Err(DbError::KeyNotFound)
        ));
    }

    #[test]
    fn compare_delete_bitcask_match_mismatch_absent() {
        let dir = tempdir().unwrap();
        let map = ConstMap::<[u8; 8], 8>::open(dir.path(), Config::test()).unwrap();

        let key = 7u64.to_be_bytes();
        let val = 7u64.to_be_bytes();
        let other = 8u64.to_be_bytes();
        map.put(&key, &val).unwrap();

        assert!(matches!(
            map.compare_delete(&key, &other),
            Err(DbError::CasMismatch)
        ));
        assert_eq!(map.get(&key), Some(val));
        assert!(map.compare_delete(&key, &val).is_ok());
        assert_eq!(map.get(&key), None);
        assert!(matches!(
            map.compare_delete(&key, &val),
            Err(DbError::KeyNotFound)
        ));
    }

    #[test]
    fn fixed_map_reopen_iterable_recovers_all() {
        let dir = tempfile::tempdir().unwrap();
        let mut cfg = crate::FixedConfig::test(); // shard_count 3
        cfg.iterable = true;

        {
            let m = crate::FixedMap::<[u8; 2], 1>::open(dir.path(), cfg.clone()).unwrap();
            m.insert(&[1, 0], &[10]).unwrap();
            m.insert(&[2, 0], &[20]).unwrap();
            m.insert(&[3, 0], &[30]).unwrap();
            m.close().unwrap(); // clean shutdown -> sidecar path
        }

        let m = crate::FixedMap::<[u8; 2], 1>::open(dir.path(), cfg).unwrap();
        // iterable companion rebuilt after recovery (lock hoisting preserves order).
        let keys: Vec<[u8; 2]> = m.iter_view().unwrap().keys().collect();
        assert_eq!(keys, [[3, 0], [2, 0], [1, 0]], "DESC, all keys recovered");
        assert_eq!(m.get(&[2, 0]).unwrap(), [20]);
    }

    #[test]
    fn fixed_map_reopen_dirty_recovers_all() {
        let dir = tempfile::tempdir().unwrap();
        let cfg = crate::FixedConfig::test(); // shard_count 3

        {
            let m = crate::FixedMap::<[u8; 8], 1>::open(dir.path(), cfg.clone()).unwrap();
            for i in 0..50u64 {
                m.insert(&i.to_be_bytes(), &[(i & 0xff) as u8]).unwrap();
            }
            // drop without close -> dirty (full-scan) recovery on reopen.
        }

        let m = crate::FixedMap::<[u8; 8], 1>::open(dir.path(), cfg).unwrap();
        for i in 0..50u64 {
            assert_eq!(
                m.get(&i.to_be_bytes()).unwrap(),
                [(i & 0xff) as u8],
                "key {i}"
            );
        }
    }
}