armdb 0.14.4

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
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
use std::hash::Hash;
use std::mem::size_of;
use std::ops::{Bound, ControlFlow};
use std::sync::{Arc, OnceLock, Weak};

use crate::Key;

use crate::codec::Codec;
use crate::compaction::{CompactionIndex, compact_shard};
use crate::config::Config;
use crate::ctx::{Ctx, Family, Held, Owned, ShardGuards};
use crate::disk_loc::DiskLoc;
use crate::engine::Engine;
use crate::error::{DbError, DbResult};
use crate::hook::{NoHook, TypedWriteHook};
use crate::map_index::{
    Dir, KeyPage, MapIter, MapIterView, OrdKey, Page, ShardIndex, ShardSource, TryMapIter,
    dir_from, key_index_batch,
};
use crate::projection::IndexKeyed;
use crate::recovery::recover_typed_map;
use crate::shard::ShardInner;
use crate::skiplist::node::TypedData;
use crate::sync::{self, Mutex};
use crate::ttl::{SweepOutcome, TtlSlots};
use crate::typed_tree::TypedRef;
use crate::upsert::Upsert;
use crate::watch::{self, CodecWatchDecoder, WatchOptions, WatchReceiver, WatchSpec};
use crate::watch::{WatchEventInner, WatchWakeBatch};

enum MapSweepAction<K: Key> {
    /// `(key, candidate_deadline)` — deadline kept for requeue if delete fails.
    Delete(K, u64),
    Reschedule(K, u64),
    Drop,
    Retry(K),
}

// ---------------------------------------------------------------------------
// TypedMapEntry
// ---------------------------------------------------------------------------

pub(crate) struct TypedMapEntry<T> {
    pub(crate) ptr: *mut TypedData<T>,
}

// SAFETY: TypedData<T> is Send+Sync when T is, and we manage the pointer lifetime via seize.
unsafe impl<T: Send> Send for TypedMapEntry<T> {}
unsafe impl<T: Sync> Sync for TypedMapEntry<T> {}

/// Raw pointers produced by `update_locked_inner`. Caller drops locks, fires
/// hook, then retires `retire`.
struct UpdatePtrs<T> {
    /// Pointer to the old `TypedData<T>` (pre-update).
    old: *const TypedData<T>,
    /// Pointer to the new `TypedData<T>` (post-update).
    new: *const TypedData<T>,
    /// Same as `old` but typed `*mut` for `seize::retire`.
    retire: *mut TypedData<T>,
    /// Exact disk location of the new value.
    new_disk: DiskLoc,
}

// SAFETY: `TypedData<T>` is Send+Sync when T is.
unsafe impl<T: Send> Send for UpdatePtrs<T> {}
unsafe impl<T: Sync> Sync for UpdatePtrs<T> {}

/// Deferred hook/retire work returned by _locked methods.
/// Caller drops shard + index locks, fires hook, then retires pointers.
pub(crate) struct MutationResult<K, T> {
    /// Raw pointer for TypedRef return. None if key was new / not found.
    ref_ptr: Option<*const TypedData<T>>,
    /// Hook args: (old_value_ptr, new_value_ptr). None = don't fire hook.
    hook: Option<(Option<*const T>, Option<*const T>)>,
    /// Pointer to retire via seize after hook. None = nothing to retire.
    retire: Option<*mut TypedData<T>>,
    /// Exact locations produced by the committed mutation. They are read under
    /// the shard lock to build a typed watch event without a second codec pass.
    new_disk: Option<DiskLoc>,
    /// Materialized under the shard lock, then only enqueued by publication.
    watch: Option<Arc<WatchEventInner<K>>>,
}

pub(crate) type MutationEvents<K, T> = Vec<(K, MutationResult<K, T>)>;

// ---------------------------------------------------------------------------
// TypedMap
// ---------------------------------------------------------------------------

/// A map with fixed-size keys and typed values `T`. Values are encoded via
/// a [`Codec`] for disk persistence but stored as `T` in memory — reads never
/// touch disk and return [`TypedRef<T>`](TypedRef) (guard-protected reference).
///
/// Each `TypedMap` owns its storage engine — one map = one database directory.
///
/// # Clone-free reads
///
/// Returns [`TypedRef<T>`](TypedRef) — a guard-protected reference. The `seize` guard
/// prevents reclamation of the old data while the reference exists.
/// `put()`, `delete()`, and `update()` return the old value **without requiring `T: Clone`**.
/// Ordered iteration ([`iter_view`](Self::iter_view) → `iter`/`range`/`keys`) is likewise
/// clone-free; keys-only iteration never even dereferences `T`.
///
/// The methods that require `T: Clone` are [`compact()`](Self::compact) and the
/// value-collecting [`paginate`](TypedMapIterView::paginate) (keys-only
/// `keys_paginate` does not).
///
/// # Write hooks
///
/// Uses [`TypedWriteHook<K, T>`] — the hook receives `&T` directly (not encoded bytes).
/// `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).
/// Old value is always provided (it lives in
/// memory) — `NEEDS_OLD_VALUE` is ignored.
///
/// `on_init` fires once per live entry during `migrate()` or `replay_init()`
/// (enable via `NEEDS_INIT = true`).
///
/// # Ordered iteration
///
/// HashMap-based, O(1) lookup. Ordered scans are available when opened with
/// `iterable(true)` via [`iter_view`](Self::iter_view). Use [`TypedTree`](crate::TypedTree)
/// if you need prefix/range scans without the iterable flag.
///
/// # Usage
///
/// ```ignore
/// let map = TypedMap::<[u8; 16], MyValue, RapiraCodec>::open(
///     "data/sessions",
///     Config::balanced().build(),
///     RapiraCodec,
/// )?;
/// map.put(&key, value)?;
/// if let Some(r) = map.get(&key) {
///     println!("{:?}", &*r);  // TypedRef<MyValue> derefs to &MyValue
/// }
/// map.close()?;
/// ```
pub struct TypedMap<
    K: Key + Send + Sync + Hash + Eq,
    T: Send + Sync,
    C: Codec<T>,
    H: TypedWriteHook<K, T> = NoHook,
> {
    indexes: Vec<Mutex<ShardIndex<K, TypedMapEntry<T>>>>,
    collector: seize::Collector,
    engine: Engine,
    codec: C,
    compaction_threshold: f64,
    shard_prefix_bits: usize,
    pub(crate) hook: H,
    iterable: bool,
    reversed: bool,
    len: std::sync::atomic::AtomicUsize,
    encode_bufs: Vec<Mutex<Vec<u8>>>,
    /// Weak back-link for [`tx_deferred`](Self::tx_deferred): set once via
    /// [`bind_tx_self`](Self::bind_tx_self) when the map lives in an `Arc`.
    tx_self: OnceLock<Weak<Self>>,
    ttl: TtlSlots<K>,
    /// `counters_dirty` marker (T9 Task 7), bound once at managed
    /// open via `ManagedOpen::bind_counter_dirty`. Lives on the collection
    /// itself (not the hook) so a user-authored wrapper hook that forgets to
    /// forward some hook method can never silently disconnect it — see
    /// `counter_dirty.rs`.
    pub(crate) counter_dirty: OnceLock<crate::counter_dirty::CounterDirty>,
}

impl<K: Key + Send + Sync + Hash + Eq, T: Send + Sync, C: Codec<T> + Clone, H: TypedWriteHook<K, T>>
    watch::sealed::Spec for TypedMap<K, T, C, H>
{
}
impl<K: Key + Send + Sync + Hash + Eq, T: Send + Sync, C: Codec<T> + Clone, H: TypedWriteHook<K, T>>
    WatchSpec for TypedMap<K, T, C, H>
{
    type Key = K;
    type Value = T;
    type Decoder = CodecWatchDecoder<C, T>;
}
impl<
    K: Key + Send + Sync + Hash + Eq,
    T: Send + Sync,
    C: Codec<T> + Clone + Sync,
    H: TypedWriteHook<K, T> + watch::sealed::Enabled,
> TypedMap<K, T, C, H>
{
    /// Subscribe to this collection's mutations.
    ///
    /// Infallible and repeatable; every receiver is independent and owns a
    /// bounded ring. Delivery is **at-most-once** — a full ring drops the
    /// oldest event and reports [`WatchError::Lagged`] at the gap.
    ///
    /// The consumer recipe is `watch()` **first**, then scan, and rescan on
    /// `Lagged`. It is complete because of what this call guarantees on return:
    /// every write that decided not to publish to this receiver has already
    /// made its row visible. The scan half is why a watched Map must be opened
    /// with [`Config::iterable`] — without an iterator view the open itself is
    /// refused with [`DbError::WatchRequiresIterable`], so that this call can
    /// stay infallible.
    ///
    /// An event is a pointer, not a snapshot — a rebuild re-reads rows by key
    /// instead of applying payloads. There is no ordering across keys or
    /// collections, and transaction boundaries are invisible. A `new_only` hook
    /// reports every non-delete as [`WatchKind::Upsert`].
    ///
    /// Subscriptions end with the database: `Db::shutdown` (and `Drop`) closes
    /// every channel; `Db::close` leaves receivers live. See `docs/watch.md`.
    pub fn watch(&self, options: WatchOptions) -> WatchReceiver<Self> {
        WatchReceiver::subscribe(
            self.hook.watch_channel().expect("enabled watch hook"),
            CodecWatchDecoder::new(self.codec.clone()),
            options,
        )
    }
}

impl<K: Key + Send + Sync + Hash + Eq, T: Send + Sync, C: Codec<T> + Sync> TypedMap<K, T, C> {
    /// Open or create a `TypedMap` at the given path.
    /// Recovers the index from existing data files on disk.
    ///
    #[doc = include_str!("../docs/exclusive-access.md")]
    pub fn open(path: impl AsRef<std::path::Path>, config: Config, codec: C) -> DbResult<Self> {
        Self::open_hooked(path, config, codec, NoHook)
    }
}

impl<K: Key + Send + Sync + Hash + Eq, T: Send + Sync, C: Codec<T> + Sync, H: TypedWriteHook<K, T>>
    TypedMap<K, T, C, H>
{
    pub(crate) fn floor_recovered_gsns(&self, gsn_high: u64, shard_gsn_high: &[u64]) {
        self.engine.gsn().fetch_max(
            gsn_high.saturating_add(1),
            std::sync::atomic::Ordering::Relaxed,
        );
        for (shard_id, high) in shard_gsn_high.iter().copied().enumerate() {
            if let Some(shard) = self.engine.shards().get(shard_id) {
                shard.set_durable_recovered_gsn(shard.durable_recovered_gsn().max(high));
            }
        }
    }

    /// Open or create a `TypedMap` with a write hook for secondary index maintenance.
    ///
    #[doc = include_str!("../docs/exclusive-access.md")]
    pub fn open_hooked(
        path: impl AsRef<std::path::Path>,
        config: Config,
        codec: C,
        hook: H,
    ) -> DbResult<Self> {
        crate::counter_dirty::check_no_counters(&hook)?;
        crate::unique::check_no_claims(&hook)?;
        #[cfg(feature = "armour")]
        if H::EMITS {
            return Err(DbError::ChangeStreamRequiresManagedOpen);
        }
        let config = config.with_resolved_hints(false);
        Self::open_inner(path, config, codec, hook)
    }

    /// Skips [`check_no_counters`](crate::counter_dirty::check_no_counters) —
    /// only `Db::open_managed` may call this, immediately followed by
    /// `ManagedOpen::bind_counter_dirty` before the collection is published.
    pub(crate) fn open_inner(
        path: impl AsRef<std::path::Path>,
        config: Config,
        codec: C,
        hook: H,
    ) -> DbResult<Self> {
        if H::WATCHES && !config.iterable {
            return Err(DbError::WatchRequiresIterable);
        }
        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 {
            // Built once after recovery via `set_iterable_and_rebuild` — see the
            // ConstMap `open_inner` for the rationale.
            indexes.push(Mutex::new(ShardIndex::new(false)));
        }

        let encode_bufs: Vec<Mutex<Vec<u8>>> =
            (0..shard_count).map(|_| Mutex::new(Vec::new())).collect();

        let map = Self {
            indexes,
            collector: seize::Collector::new(),
            engine,
            codec,
            compaction_threshold,
            shard_prefix_bits,
            hook,
            iterable,
            reversed,
            len: std::sync::atomic::AtomicUsize::new(0),
            encode_bufs,
            tx_self: OnceLock::new(),
            ttl: TtlSlots::new(),
            counter_dirty: OnceLock::new(),
        };

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

        let hints = map.engine.hints();
        let outcome = recover_typed_map::<K, T, C>(
            &shard_dir_refs,
            &shard_ids,
            map.indexes(),
            &map.codec,
            hints,
            #[cfg(feature = "encryption")]
            map.engine.shard_ciphers(),
        )?;
        for tail in &outcome.active_tails {
            map.engine.shards()[tail.shard_idx].apply_recovery_tail(tail)?;
        }
        for (shard_idx, dead) in outcome.shard_dead_bytes {
            map.engine.shards()[shard_idx].install_dead_bytes(dead);
        }
        for (shard_idx, file_seqs) in outcome.shard_file_seqs {
            map.engine.shards()[shard_idx].install_recovery_file_seqs(file_seqs);
        }
        // The transaction replay rule compares a reserved GSN against this
        // watermark, so it must exist in every build — not only with
        // `replication` on.
        for (shard_idx, max_gsn) in &outcome.shard_max_gsns {
            map.engine.shards()[*shard_idx].set_durable_recovered_gsn(*max_gsn);
        }
        let max_gsn = outcome.max_gsn;

        map.engine
            .gsn()
            .fetch_max(max_gsn + 1, std::sync::atomic::Ordering::Relaxed);
        if hints {
            for shard in map.engine.shards().iter() {
                shard.set_key_len(size_of::<K>());
            }
        }
        if iterable {
            for idx in &map.indexes {
                sync::lock(idx).set_iterable_and_rebuild();
            }
        }
        let initial_len: usize = map.indexes.iter().map(|m| sync::lock(m).len()).sum();
        map.len
            .store(initial_len, std::sync::atomic::Ordering::Relaxed);

        tracing::info!(
            key_size = size_of::<K>(),
            entries = initial_len,
            "typed_map recovered"
        );

        Ok(map)
    }

    /// Full clean shutdown: write hint files (if enabled), flush write buffers + fsync.
    pub fn clean_shutdown(&self) -> DbResult<()> {
        if self.engine.hints() {
            self.sync_hints()?;
        }
        // `close`, not `flush`: it also drains the background hint worker, which
        // is what makes "cleanly closed ⇒ every retired file has a hint" hold.
        self.engine.close()
    }

    /// Graceful shutdown: write hint files (if enabled), flush write buffers + fsync.
    pub fn close(self) -> DbResult<()> {
        self.clean_shutdown()
    }

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

    /// Durable flush: write buffers + fsync active files. Migration path (D12):
    /// schema version is committed via fsync'd db.info; data must be durable first.
    pub(crate) fn durable_flush(&self) -> DbResult<()> {
        self.engine.flush()
    }

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

impl<
    K: Key + Send + Sync + Hash + Eq,
    T: Clone + Send + Sync,
    C: Codec<T> + Sync,
    H: TypedWriteHook<K, T>,
> CompactionIndex<K> for TypedMap<K, T, C, H>
{
    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) {
            let data = unsafe { &*entry.ptr };
            if data.disk == old_loc {
                let new_data = Box::into_raw(Box::new(TypedData {
                    disk: new_loc,
                    value: data.value.clone(),
                }));
                let old_ptr = entry.ptr;
                entry.ptr = new_data;
                unsafe {
                    self.collector
                        .retire(old_ptr, seize::reclaim::boxed::<TypedData<T>>);
                }
                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]);
        match index.get(key) {
            Some(entry) => unsafe { &*entry.ptr }.disk == loc,
            None => false,
        }
    }
}

impl<K: Key + Send + Sync + Hash + Eq, T: Send + Sync, C: Codec<T> + Sync, H: TypedWriteHook<K, T>>
    Family for TypedMap<K, T, C, H>
{
    type Locked<'a>
        = (
        sync::MutexGuard<'a, ShardInner>,
        sync::MutexGuard<'a, ShardIndex<K, TypedMapEntry<T>>>,
    )
    where
        Self: 'a;
    type Event<'v> = (K, MutationResult<K, T>);
    type Owned = (K, MutationResult<K, T>);

    #[inline]
    fn lock_shard(&self, sid: usize) -> Self::Locked<'_> {
        let inner = self.engine.shards()[sid].lock();
        let index = sync::lock(&self.indexes[sid]);
        (inner, index)
    }

    fn should_sync<'a>(locked: &Self::Locked<'a>) -> bool
    where
        Self: 'a,
    {
        locked.0.should_sync()
    }

    fn sync(&self, sid: usize) -> DbResult<()> {
        #[cfg(feature = "ctx-gates")]
        crate::ctx_gates_sync::record_sync_call();
        self.engine.shards()[sid].flush()
    }

    fn fire(&self, ev: Self::Event<'_>) {
        let (k, mr) = ev;
        self.fire_hook_and_retire(&k, &mr);
    }

    fn own(ev: Self::Event<'_>) -> Self::Owned {
        ev
    }

    fn fire_owned(&self, ev: &Self::Owned) {
        self.fire_hook_and_retire(&ev.0, &ev.1);
    }

    fn materialize_watch<'a, 'v>(
        &self,
        locked: &mut Self::Locked<'a>,
        ev: &mut Self::Event<'v>,
        active: bool,
        wakes: &mut WatchWakeBatch,
    ) where
        Self: 'a,
    {
        if !H::WATCHES {
            return;
        }
        if !active {
            return;
        }
        let (key, mutation) = ev;
        let Some((old_ptr, _)) = mutation.hook else {
            return;
        };
        let old_existed = old_ptr.is_some();
        let (inner, _index) = &*locked;
        // The previous value is still a live `T` in the node this mutation
        // swapped out, so it is encoded here rather than read back from its
        // `DiskLoc`: a `pread` (plus decrypt) would run inside the shard lock
        // for a value that is already in RAM. A failed encode degrades the
        // event instead of discarding it — `old_existed` keeps the kind honest.
        let old = if self.hook.watch_old() {
            // SAFETY: the mutation's shard guard is still held and retirement
            // happens after the hooks fire, so the swapped-out node is live.
            match old_ptr.map(|ptr| unsafe { &*ptr }) {
                Some(value) => {
                    let mut buf = Vec::new();
                    match self.codec.encode_to(value, &mut buf) {
                        Ok(()) => Some(buf.into_boxed_slice()),
                        Err(e) => {
                            tracing::warn!(
                                key = ?key.as_bytes(),
                                error = %e,
                                "watch event lost its old value; reporting the write without it"
                            );
                            None
                        }
                    }
                }
                None => None,
            }
        } else {
            None
        };
        // A failed `new` read has no honest degradation — an event without new
        // bytes reads as a delete. Report the loss as a gap instead, so the
        // consumer resyncs rather than silently keeping a stale row.
        let new = match mutation.new_disk.as_ref() {
            Some(loc) => match inner.read_value_bytes_locked(loc) {
                Ok(bytes) => Some(bytes.into_boxed_slice()),
                Err(e) => {
                    tracing::error!(
                        key = ?key.as_bytes(),
                        error = %e,
                        "watch event could not be materialized; reporting a gap"
                    );
                    if let Some(channel) = self.hook.watch_channel() {
                        channel.publish_lost(wakes);
                    }
                    return;
                }
            },
            None => None,
        };
        mutation.watch = Some(Arc::new(WatchEventInner::from_write(
            *key,
            old,
            new,
            self.hook.watch_old(),
            old_existed,
        )));
    }

    fn publish_watch(&self, ev: &Self::Event<'_>, active: bool, wakes: &mut WatchWakeBatch) {
        if !H::WATCHES || !active {
            return;
        }
        let Some(channel) = self.hook.watch_channel() else {
            return;
        };
        if let Some(event) = ev.1.watch.as_ref() {
            channel.publish(Arc::clone(event), wakes);
        }
    }

    const WATCHES: bool = H::WATCHES;

    fn watch_enter(&self) -> crate::watch::WatchProducer<'_> {
        if H::WATCHES {
            self.hook
                .watch_channel()
                .and_then(|channel| channel.enter_producer())
                .unwrap_or_else(crate::watch::WatchProducer::inactive)
        } else {
            crate::watch::WatchProducer::inactive()
        }
    }

    fn apply_projection_for_event(&self, ev: &Self::Event<'_>) -> DbResult<()> {
        self.apply_projection_for_mutation(ev)
    }

    fn apply_projection_for_owned(&self, ev: &Self::Owned) -> DbResult<()> {
        self.apply_projection_for_mutation(ev)
    }
}

impl<K: Key + Send + Sync + Hash + Eq, T: Send + Sync, C: Codec<T> + Sync, H: TypedWriteHook<K, T>>
    TypedMap<K, T, C, H>
{
    /// Build per-shard TTL indexes by walking live in-memory entries.
    #[doc(hidden)]
    pub fn build_ttl_index(&self) -> DbResult<()> {
        if !H::EXPIRES {
            return Ok(());
        }
        let n = self.engine.shards().len();
        let slots = self.ttl.install_and_clear(n);

        let _guard = self.collector.enter();
        for (sid, slot) in slots.iter().enumerate() {
            // Lock order: engine shard, then per-shard index — read values under
            // both so a concurrent put cannot leave a stale deadline in the rebuild.
            let _shard = self.engine.shards()[sid].lock();
            let index = sync::lock(&self.indexes[sid]);
            for (key, entry) in index.iter() {
                let data = unsafe { &*entry.ptr };
                let deadline = self.hook.deadline(key, &data.value);
                sync::lock(slot).record(key, deadline);
            }
        }
        Ok(())
    }

    fn record_ttl(&self, shard_id: usize, key: &K, expires_at: Option<u64>) {
        if !H::EXPIRES {
            return;
        }
        self.ttl.record(shard_id, key, expires_at);
    }

    #[doc(hidden)]
    pub fn ttl_debug_len(&self) -> usize {
        self.ttl.debug_len()
    }

    #[doc(hidden)]
    pub fn ttl_debug_deadline(&self, key: &K) -> Option<u64> {
        self.ttl.debug_deadline(self.shard_for(key), key)
    }

    pub fn sweep_expired(&self, now_ms: u64, budget: usize) -> DbResult<SweepOutcome> {
        if !H::EXPIRES {
            return Ok(SweepOutcome::default());
        }
        let Some(slots) = self.ttl.get() else {
            return Ok(SweepOutcome::default());
        };
        let Some(_pass) = self.ttl.begin_pass() else {
            return Ok(SweepOutcome::default());
        };
        let shard_count = slots.len();
        let mut remaining = budget;
        let mut outcome = SweepOutcome::default();

        let guard = self.collector.enter();

        while remaining > 0 && self.ttl.any_due(now_ms) {
            let sid = self.ttl.next_shard(shard_count);
            let mut shard_events = Vec::new();
            let mut wakes = WatchWakeBatch::default();
            let mut locked = self.lock_shard(sid);
            let producer = self.watch_enter();
            let active = producer.active();
            let mut idx = sync::lock(&slots[sid]);

            while remaining > 0 {
                let Some((candidate_dl, key)) = idx.pop_due(now_ms) else {
                    break;
                };
                remaining -= 1;

                let action = {
                    let index = &*locked.1;
                    match index.get(&key) {
                        Some(entry) => {
                            let value = &unsafe { &*entry.ptr }.value;
                            match crate::ttl::catch_deadline(|| self.hook.deadline(&key, value)) {
                                Some(Some(dl)) if dl <= now_ms => {
                                    MapSweepAction::Delete(key, candidate_dl)
                                }
                                Some(Some(dl)) => MapSweepAction::Reschedule(key, dl),
                                Some(None) => MapSweepAction::Drop,
                                None => MapSweepAction::Retry(key),
                            }
                        }
                        None => MapSweepAction::Drop,
                    }
                };

                match action {
                    MapSweepAction::Delete(key, candidate_dl) => {
                        drop(idx);
                        let (inner, index) = &mut locked;
                        match self.delete_locked_inner::<true>(sid, inner, index, &guard, &key) {
                            Ok(mr) => {
                                let mut event = (key, mr);
                                self.materialize_watch(&mut locked, &mut event, active, &mut wakes);
                                self.publish_watch(&event, active, &mut wakes);
                                idx = sync::lock(&slots[sid]);
                                if event.1.hook.is_some() {
                                    shard_events.push(event);
                                }
                                outcome.deleted += 1;
                            }
                            Err(e) => {
                                sync::lock(&slots[sid]).record(&key, Some(candidate_dl));
                                let needs_sync = Self::should_sync(&locked);
                                locked.release();
                                // Do not lose what this pass already did: `failed` counts rows that were
                                // rescheduled after a read/decode failure, and the warning is the only
                                // signal that a collection is degrading.
                                crate::ttl::log_sweep_failures(None, &outcome);
                                let _ = crate::projection::replay_owned_events(
                                    self,
                                    &shard_events,
                                    needs_sync,
                                    sid,
                                );
                                <Self as Family>::wake_watches(wakes);
                                return Err(e);
                            }
                        }
                    }
                    MapSweepAction::Reschedule(key, dl) => {
                        idx.record(&key, Some(dl));
                    }
                    MapSweepAction::Retry(key) => {
                        outcome.failed += 1;
                        idx.record(&key, Some(now_ms + crate::ttl::TTL_RETRY_BACKOFF_MS));
                    }
                    MapSweepAction::Drop => {}
                }
            }

            drop(idx);
            let needs_sync = Self::should_sync(&locked);
            locked.release();
            let replay =
                crate::projection::replay_owned_events(self, &shard_events, needs_sync, sid);
            <Self as Family>::wake_watches(wakes);
            replay?;
        }

        drop(guard);
        outcome.more = self.ttl.any_due(now_ms);
        crate::ttl::log_sweep_failures(None, &outcome);
        Ok(outcome)
    }

    pub fn set_ttl_sweep(&self, enabled: bool) -> DbResult<()> {
        if H::EXPIRES {
            self.ttl.set_enabled(enabled)
        } else {
            Ok(())
        }
    }

    pub fn mark_replication_follower(&self) {
        if H::EXPIRES {
            self.ttl.mark_replication_follower();
        }
    }

    pub fn rebuild_ttl_index(&self) -> DbResult<()> {
        if !H::EXPIRES {
            return Err(DbError::Client("collection has no TTL policy"));
        }
        self.build_ttl_index()?;
        self.ttl.clear_rebuild_requirement();
        Ok(())
    }

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

    // -- Reads ----------------------------------------------------------------

    /// Get a guard-protected reference to a value by key. O(1) lookup.
    /// Brief shard lock to read pointer, then lock-free access via seize guard.
    pub fn get(&self, key: &K) -> Option<TypedRef<'_, T>> {
        metrics::counter!("armdb.ops", "op" => "get", "tree" => "typed_map").increment(1);
        let guard = self.collector.enter();
        let data_ptr = {
            let index = sync::lock(&self.indexes[self.shard_for(key)]);
            let entry = index.get(key)?;
            entry.ptr as *const TypedData<T>
        };
        Some(TypedRef::new(guard, data_ptr))
    }

    /// Get a guard-protected reference to a value by key, returning `Err(KeyNotFound)` if absent.
    pub fn get_or_err(&self, key: &K) -> DbResult<TypedRef<'_, T>> {
        self.get(key).ok_or(DbError::KeyNotFound)
    }

    /// 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)
    }

    // -- Writes ---------------------------------------------------------------

    /// `Held`-only: buffer the event for replay, or — when hooks are off and no
    /// event will be buffered — retire the old value immediately.
    ///
    /// The facade (`Owned`) must **not** call this: it always returns the event so
    /// `Family::fire` → `fire_hook_and_retire` runs *after* the shard and index
    /// locks are released. `retire` can allocate, scan every thread's reservation
    /// and run `T::drop` in a batch, none of which belongs in the critical section.
    #[inline]
    fn defer_or_retire_event(
        &self,
        key: &K,
        mr: MutationResult<K, T>,
    ) -> Option<(K, MutationResult<K, T>)> {
        if H::NEEDS_WRITE || (H::WATCHES && crate::watch::context_active()) {
            Some((*key, mr))
        } else {
            if let Some(ptr) = mr.retire {
                unsafe {
                    self.collector
                        .retire(ptr, seize::reclaim::boxed::<TypedData<T>>);
                }
            }
            None
        }
    }

    /// Insert or update a key-value pair. Returns a `TypedRef` to the old value
    /// if the key existed (valid while the guard lives).
    pub fn put(&self, key: &K, value: T) -> DbResult<Option<TypedRef<'_, T>>> {
        metrics::counter!("armdb.ops", "op" => "put", "tree" => "typed_map").increment(1);
        let expires_at = if H::EXPIRES {
            self.hook.deadline(key, &value)
        } else {
            None
        };
        let shard_id = self.shard_for(key);
        let guard = self.collector.enter();
        let mut ctx = Owned;
        let ref_ptr = Ctx::with_shard(&mut ctx, self, shard_id, |locked, ev_out| {
            let (inner, index) = locked;
            let mr = self.put_locked_inner::<true>(
                shard_id,
                inner,
                index,
                &guard,
                key,
                value,
                crate::ttl::TtlAt::Precomputed(expires_at),
            )?;
            let ref_ptr = mr.ref_ptr;
            *ev_out = Some((*key, mr));
            Ok(ref_ptr)
        })?;
        Ok(ref_ptr.map(|p| TypedRef::new(guard, p)))
    }

    /// 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: T) -> DbResult<()> {
        metrics::counter!("armdb.ops", "op" => "insert", "tree" => "typed_map").increment(1);
        let expires_at = if H::EXPIRES {
            self.hook.deadline(key, &value)
        } else {
            None
        };
        let shard_id = self.shard_for(key);
        let guard = self.collector.enter();
        let mut ctx = Owned;
        Ctx::with_shard(&mut ctx, self, shard_id, |locked, ev_out| {
            let (inner, index) = locked;
            let mr =
                self.insert_locked::<true>(shard_id, inner, index, &guard, key, value, expires_at)?;
            *ev_out = Some((*key, mr));
            Ok(())
        })
    }

    /// Delete a key. Returns a `TypedRef` to the old value if the key existed.
    pub fn delete(&self, key: &K) -> DbResult<Option<TypedRef<'_, T>>> {
        metrics::counter!("armdb.ops", "op" => "delete", "tree" => "typed_map").increment(1);
        let shard_id = self.shard_for(key);
        let guard = self.collector.enter();
        let mut ctx = Owned;
        let ref_ptr = Ctx::with_shard(&mut ctx, self, shard_id, |locked, ev_out| {
            let (inner, index) = locked;
            let mr = self.delete_locked_inner::<true>(shard_id, inner, index, &guard, key)?;
            let ref_ptr = mr.ref_ptr;
            *ev_out = Some((*key, mr));
            Ok(ref_ptr)
        })?;
        Ok(ref_ptr.map(|p| TypedRef::new(guard, p)))
    }

    /// 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: &T, new_value: T) -> DbResult<()>
    where
        T: PartialEq,
    {
        metrics::counter!("armdb.ops", "op" => "cas", "tree" => "typed_map").increment(1);
        let expires_at = if H::EXPIRES {
            self.hook.deadline(key, &new_value)
        } else {
            None
        };
        let shard_id = self.shard_for(key);
        let _guard = self.collector.enter();
        let mut ctx = Owned;
        Ctx::with_shard(&mut ctx, self, shard_id, |locked, ev_out| {
            let (inner, index) = locked;

            let entry = index.get(key).ok_or(DbError::KeyNotFound)?;
            let current_data = unsafe { &*entry.ptr };
            if current_data.value != *expected {
                return Err(DbError::CasMismatch);
            }
            // T10 Task 5: `cas` has its own inline body (does not go through
            // `put_locked_inner`), so it needs its own guard call.
            self.claim_guard(key, Some(&current_data.value), Some(&new_value))?;

            let mut buf = sync::lock(&self.encode_bufs[shard_id]);
            self.codec.encode_to(&new_value, &mut buf)?;
            let (disk_loc, _gsn) =
                inner.append_entry(shard_id as u8, key.as_bytes(), &buf, false)?;

            let new_data_ptr = Box::into_raw(Box::new(TypedData {
                disk: disk_loc,
                value: new_value,
            }));
            let old_ptr = entry.ptr;
            index.get_mut(key).expect("key exists").ptr = new_data_ptr;

            let old_disk = unsafe { (*old_ptr).disk };
            inner.add_dead_bytes(
                old_disk.file_id,
                crate::entry::entry_size(size_of::<K>(), old_disk.len),
            );

            self.record_ttl(shard_id, key, expires_at);

            let mr = MutationResult {
                ref_ptr: None,
                hook: Some((
                    Some(&current_data.value as *const T),
                    Some(unsafe { &(*new_data_ptr).value as *const T }),
                )),
                retire: Some(old_ptr),
                new_disk: Some(disk_loc),
                watch: None,
            };
            *ev_out = Some((*key, mr));
            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.
    pub fn compare_delete(&self, key: &K, expected: &T) -> DbResult<()>
    where
        T: PartialEq,
    {
        metrics::counter!("armdb.ops", "op" => "compare_delete", "tree" => "typed_map")
            .increment(1);
        let shard_id = self.shard_for(key);
        let guard = self.collector.enter();
        let mut ctx = Owned;
        Ctx::with_shard(&mut ctx, self, shard_id, |locked, ev_out| {
            let (inner, index) = locked;

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

            let mr = self.delete_locked_inner::<true>(shard_id, inner, index, &guard, key)?;
            *ev_out = Some((*key, mr));
            Ok(())
        })
    }

    /// Compare-and-delete by pointer identity: delete only if the live entry still
    /// points at `expected`. Used by [`TypedMapIterView::retain`] without requiring `T: PartialEq`.
    pub(crate) fn compare_delete_if_ptr(
        &self,
        key: &K,
        expected: *const TypedData<T>,
    ) -> DbResult<()> {
        metrics::counter!("armdb.ops", "op" => "compare_delete", "tree" => "typed_map")
            .increment(1);
        let shard_id = self.shard_for(key);
        let guard = self.collector.enter();
        let mut ctx = Owned;
        Ctx::with_shard(&mut ctx, self, shard_id, |locked, ev_out| {
            let (inner, index) = locked;

            let entry = index.get(key).ok_or(DbError::KeyNotFound)?;
            if !std::ptr::eq(entry.ptr, expected as *mut TypedData<T>) {
                return Err(DbError::CasMismatch);
            }

            let mr = self.delete_locked_inner::<true>(shard_id, inner, index, &guard, key)?;
            *ev_out = Some((*key, mr));
            Ok(())
        })
    }

    /// Atomically read-modify-write. Returns `Some(TypedRef)` to the **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(&T) -> T) -> DbResult<Option<TypedRef<'_, T>>> {
        self.update_inner(key, f, false)
    }

    /// Like [`update()`](Self::update), but returns `Some(TypedRef)` to the **old** value.
    pub fn fetch_update(
        &self,
        key: &K,
        f: impl FnOnce(&T) -> T,
    ) -> DbResult<Option<TypedRef<'_, T>>> {
        self.update_inner(key, f, true)
    }

    /// Decide-and-write: insert, update, keep, or delete based on the closure's
    /// [`Upsert`](crate::Upsert) decision. The closure must be short — the shard
    /// lock is held across it, so it must not panic and must not call back into
    /// this collection (the shard mutex is not reentrant).
    pub fn upsert<U: crate::IntoUpsert<T>>(
        &self,
        key: &K,
        f: impl FnOnce(Option<&T>) -> U,
    ) -> crate::DbResult<U::Out> {
        metrics::counter!("armdb.ops", "op" => "upsert", "tree" => "typed_map").increment(1);
        self.atomic(key, |s| crate::upsert::upsert_via_held(s, key, f))
    }

    /// Swap the index pointer for `key` and return raw before/after pointers.
    /// Encodes and appends the new value to disk, then updates the index in place.
    /// **Caller must hold both `inner` and `index` locks.**
    /// No hook is fired and no retire is issued — that is the caller's responsibility.
    fn update_locked_inner(
        &self,
        shard_id: usize,
        inner: &mut ShardInner,
        index: &mut ShardIndex<K, TypedMapEntry<T>>,
        key: &K,
        f: impl FnOnce(&T) -> T,
    ) -> DbResult<Option<UpdatePtrs<T>>> {
        let entry = match index.get(key) {
            Some(e) => e,
            None => return Ok(None),
        };
        let old_ptr = entry.ptr;
        let old_data = unsafe { &*old_ptr };
        let new_value = f(&old_data.value);
        self.claim_guard(key, Some(&old_data.value), Some(&new_value))?;

        let mut buf = sync::lock(&self.encode_bufs[shard_id]);
        self.codec.encode_to(&new_value, &mut buf)?;
        let (disk_loc, _gsn) = inner.append_entry(shard_id as u8, key.as_bytes(), &buf, false)?;
        drop(buf);

        let new_data_ptr = Box::into_raw(Box::new(TypedData {
            disk: disk_loc,
            value: new_value,
        }));
        index.get_mut(key).expect("key exists").ptr = new_data_ptr;

        let old_disk = unsafe { (*old_ptr).disk };
        inner.add_dead_bytes(
            old_disk.file_id,
            crate::entry::entry_size(size_of::<K>(), old_disk.len),
        );

        let expires_at = if H::EXPIRES {
            self.hook.deadline(key, unsafe { &(*new_data_ptr).value })
        } else {
            None
        };
        self.record_ttl(shard_id, key, expires_at);

        Ok(Some(UpdatePtrs {
            old: old_ptr as *const TypedData<T>,
            new: new_data_ptr as *const TypedData<T>,
            retire: old_ptr,
            new_disk: disk_loc,
        }))
    }

    fn update_inner(
        &self,
        key: &K,
        f: impl FnOnce(&T) -> T,
        return_old: bool,
    ) -> DbResult<Option<TypedRef<'_, T>>> {
        metrics::counter!("armdb.ops", "op" => "update", "tree" => "typed_map").increment(1);
        let shard_id = self.shard_for(key);
        let guard = self.collector.enter();
        let mut ctx = Owned;
        let ref_ptr = Ctx::with_shard(&mut ctx, self, shard_id, |locked, ev_out| {
            let (inner, index) = locked;
            let ptrs = match self.update_locked_inner(shard_id, inner, index, key, f)? {
                Some(p) => p,
                None => return Ok(None),
            };
            let ref_ptr = if return_old { ptrs.old } else { ptrs.new };
            let mr = MutationResult {
                ref_ptr: None,
                hook: Some((
                    Some(unsafe { &(*ptrs.old).value as *const T }),
                    Some(unsafe { &(*ptrs.new).value as *const T }),
                )),
                retire: Some(ptrs.retire),
                new_disk: Some(ptrs.new_disk),
                watch: None,
            };
            *ev_out = Some((*key, mr));
            Ok(Some(ref_ptr))
        })?;
        Ok(ref_ptr.map(|p| TypedRef::new(guard, p)))
    }

    // -- Atomic ---------------------------------------------------------------

    /// 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 TypedMapShard<'_, K, T, C, H>) -> DbResult<R>,
    ) -> DbResult<R> {
        let _atomic_depth = crate::AtomicDepthGuard::try_enter()?;
        let shard_id = self.shard_for(shard_key);
        let mut shard = self.tx_shard(shard_id);
        let result = f(&mut shard);
        let needs_sync = Self::should_sync(&shard.held.locked);
        let (events, wakes, guard) = Self::tx_release_shard(shard);
        let result =
            crate::projection::finish_atomic_teardown(self, shard_id, result, needs_sync, &events);
        drop(guard);
        <Self as Family>::wake_watches(wakes);
        result
    }

    // -- Info -----------------------------------------------------------------

    pub fn len(&self) -> usize {
        self.len.load(std::sync::atomic::Ordering::Relaxed)
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Whether iterable scan yields descending key order.
    pub(crate) fn reversed(&self) -> bool {
        self.reversed
    }

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

    pub(crate) fn shard_count(&self) -> usize {
        self.engine.shards().len()
    }

    pub(crate) fn codec(&self) -> &C {
        &self.codec
    }

    /// Register the `Arc` handle used to build a `'static` deferred owner.
    #[cfg_attr(not(test), allow(dead_code))]
    pub(crate) fn bind_tx_self(arc: &Arc<Self>) {
        let _ = arc.tx_self.set(Arc::downgrade(arc));
    }

    pub(crate) fn tx_arc(&self) -> DbResult<Arc<Self>> {
        self.tx_self
            .get()
            .and_then(Weak::upgrade)
            .ok_or(DbError::Internal(
                "TypedMap::bind_tx_self must be called before transaction use",
            ))
    }

    /// Register projection targets and verify same-`Db` membership (T8 T5).
    pub(crate) fn tx_install_projection_metadata(&self, db: &crate::armour::Db) -> DbResult<()> {
        if H::PROJECTS
            && let Some(targets) = self.hook.projection_targets()
        {
            db.tx_register_projection_targets(targets)?;
        }
        if H::COUNTS
            && let Some(targets) = self.hook.counter_targets()
        {
            db.tx_register_counter_targets(targets)?;
        }
        if H::CLAIMS
            && let Some(targets) = self.hook.claim_targets()
        {
            db.tx_register_claim_targets(targets)?;
        }
        if H::EMITS
            && let Some(targets) = self.hook.stream_targets()
        {
            db.tx_register_stream_targets(targets)?;
        }
        Ok(())
    }

    /// Rank-1 participants a write here can reach: declared secondary-index
    /// targets, declared counter targets, **and** declared unique-namespace
    /// targets. All three are written under a lock the commit takes in its
    /// second round, so all three must be in the plan before the first lock
    /// is taken (T9 Task 9; unique namespaces added T10 Task 6). A namespace
    /// is preplanned whether or not this particular row claims — the plan
    /// cannot know what the projection will emit before it runs, and a
    /// namespace absent from the plan cannot be locked in the second round.
    pub(crate) fn tx_preplan_projection_targets(
        &self,
    ) -> DbResult<Vec<Arc<dyn crate::txn::participant::TxParticipant>>> {
        let mut out = Vec::new();
        if H::PROJECTS
            && let Some(targets) = self.hook.projection_targets()
        {
            out.extend(
                targets
                    .erased_targets()
                    .iter()
                    .filter_map(|h| targets.resolve_tx_participant(h.name)),
            );
        }
        if H::COUNTS
            && let Some(targets) = self.hook.counter_targets()
        {
            out.extend(targets.all().map(|m| m.as_tx_participant()));
        }
        if H::CLAIMS
            && let Some(targets) = self.hook.claim_targets()
        {
            out.extend(targets.all().map(|t| t.as_tx_participant()));
        }
        if H::EMITS
            && let Some(targets) = self.hook.stream_targets()
        {
            out.extend(targets.tx_participants());
        }
        Ok(out)
    }

    pub(crate) fn tx_preplan_stream_targets(
        &self,
    ) -> Vec<Arc<dyn crate::txn::participant::TxParticipant>> {
        self.hook
            .stream_targets()
            .map(|targets| targets.tx_participants())
            .unwrap_or_default()
    }
    pub(crate) fn tx_prepare_streams(
        &self,
    ) -> DbResult<Vec<crate::queue::retention::ProducerPermit>> {
        self.hook
            .stream_targets()
            .map_or(Ok(Vec::new()), crate::QueueTargets::prepare_streams)
    }

    pub(crate) fn tx_stream_writes(
        &self,
        key: &[u8],
        old: Option<&[u8]>,
        new: Option<&[u8]>,
    ) -> DbResult<crate::txn::participant::TxStreamWrites> {
        if !H::EMITS {
            return Ok(crate::txn::participant::TxStreamWrites::empty());
        }
        let Some(targets) = self.hook.stream_targets() else {
            return Ok(crate::txn::participant::TxStreamWrites::empty());
        };
        let key = K::from_bytes(key);
        let old = old.map(|bytes| self.codec.decode_from(bytes)).transpose()?;
        let new = new.map(|bytes| self.codec.decode_from(bytes)).transpose()?;
        let (max_rows, max_bytes) = self.effective_projection_limits();
        let mut sink = crate::EmitSink::new(targets, max_rows, max_bytes);
        self.hook
            .stream_project(&key, old.as_ref(), new.as_ref(), &mut sink)?;
        targets.lower(&sink)
    }

    pub(crate) fn tx_project_writes(
        &self,
        key: &[u8],
        old: Option<&[u8]>,
        new: Option<&[u8]>,
    ) -> DbResult<Vec<crate::txn::participant::TxDerivedWrite>> {
        if !H::PROJECTS {
            return Ok(Vec::new());
        }
        let Some(targets) = self.hook.projection_targets() else {
            return Ok(Vec::new());
        };
        let key_typed = K::from_bytes(key);
        let old_t = match old {
            Some(b) => Some(self.codec.decode_from(b)?),
            None => None,
        };
        let new_t = match new {
            Some(b) => Some(self.codec.decode_from(b)?),
            None => None,
        };
        let (max_rows, max_bytes) = self.effective_projection_limits();
        let plan = crate::projection::diff_projection(
            &self.hook,
            targets,
            max_rows,
            max_bytes,
            &key_typed,
            old_t.as_ref(),
            new_t.as_ref(),
        )?;
        plan.into_tx_derived(targets)
    }

    /// Counter deltas for one decided write inside a transaction — the counter
    /// twin of [`tx_project_writes`](Self::tx_project_writes).
    ///
    /// Deltas, not writes: the absolute value a delta resolves to is only
    /// knowable under the target's own lock, which the commit takes later.
    pub(crate) fn tx_counter_deltas_for(
        &self,
        key: &[u8],
        old: Option<&[u8]>,
        new: Option<&[u8]>,
    ) -> DbResult<Vec<crate::txn::participant::TxCounterDelta>> {
        if !H::COUNTS {
            return Ok(Vec::new());
        }
        let Some(targets) = self.hook.counter_targets() else {
            return Ok(Vec::new());
        };
        let key_typed = K::from_bytes(key);
        let old_t = match old {
            Some(b) => Some(self.codec().decode_from(b)?),
            None => None,
        };
        let new_t = match new {
            Some(b) => Some(self.codec().decode_from(b)?),
            None => None,
        };
        let (max_rows, max_bytes) = self.effective_projection_limits();
        let deltas = crate::counter::diff_counters(
            &self.hook,
            targets,
            max_rows,
            max_bytes,
            &key_typed,
            old_t.as_ref(),
            new_t.as_ref(),
        )?;
        crate::counter::into_tx_counter_deltas(targets, deltas)
    }

    /// Claim ops for one decided write inside a transaction — the claim twin
    /// of [`tx_project_writes`](Self::tx_project_writes)/
    /// [`tx_counter_deltas_for`](Self::tx_counter_deltas_for) (T10 Task 6).
    ///
    /// `name` is this collection's own [`CollectionMeta::NAME`] — supplied by
    /// the caller (the `TxParticipant` impl in `txn::families::typed`, where
    /// the `T: TxCollectionName` bound holds), because this loosely-bound
    /// impl block cannot name `T` itself.
    pub(crate) fn tx_claims_for(
        &self,
        name: &'static str,
        key: &[u8],
        old: Option<&[u8]>,
        new: Option<&[u8]>,
    ) -> DbResult<Vec<crate::txn::participant::ClaimOp>> {
        if !H::CLAIMS {
            return Ok(Vec::new());
        }
        let Some(targets) = self.hook.claim_targets() else {
            return Ok(Vec::new());
        };
        let key_typed = K::from_bytes(key);
        let old_t = match old {
            Some(b) => Some(self.codec().decode_from(b)?),
            None => None,
        };
        let new_t = match new {
            Some(b) => Some(self.codec().decode_from(b)?),
            None => None,
        };
        let (max_rows, max_bytes) = self.effective_projection_limits();
        let delta = crate::unique::claim_delta(
            &self.hook,
            targets,
            max_rows,
            max_bytes,
            &key_typed,
            old_t.as_ref(),
            new_t.as_ref(),
        )?;
        crate::unique::into_tx_claim_ops(targets, name, key, delta)
    }

    /// Lock `sid` and build the shard object, exactly as [`atomic`] does but
    /// without the closure scope, so a transaction can hold several shards.
    pub(crate) fn tx_shard(&self, sid: usize) -> TypedMapShard<'_, K, T, C, H> {
        let locked = self.lock_shard(sid);
        let guard = self.collector.enter();
        TypedMapShard {
            map: self,
            held: Held {
                locked,
                sid,
                events: Vec::new(),
                watch_wakes: WatchWakeBatch::default(),
                publish_watches: true,
            },
            guard,
        }
    }

    /// Release a shard built by [`tx_shard`], returning its buffered events.
    /// The `seize` guard is dropped by the caller's `Deferred` owner, not here.
    pub(crate) fn tx_release_shard(
        shard: TypedMapShard<'_, K, T, C, H>,
    ) -> (MutationEvents<K, T>, WatchWakeBatch, seize::LocalGuard<'_>) {
        let TypedMapShard { held, guard, .. } = shard;
        let Held {
            locked,
            events,
            watch_wakes,
            ..
        } = held;
        locked.release();
        (events, watch_wakes, guard)
    }

    pub(crate) fn tx_reserve_gsns(&self, count: usize) -> u64 {
        self.engine
            .gsn()
            .fetch_add(count as u64, std::sync::atomic::Ordering::Relaxed)
    }

    pub(crate) fn tx_encrypted(&self) -> bool {
        #[cfg(feature = "encryption")]
        {
            self.engine.config().encryption_key.is_some()
        }
        #[cfg(not(feature = "encryption"))]
        {
            false
        }
    }

    pub(crate) fn tx_durable_recovered_gsn(&self, sid: usize) -> u64 {
        self.engine.shards()[sid].durable_recovered_gsn()
    }

    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.engine.shards().len();
        }

        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.engine.shards().len()
    }

    // -- 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. Map and Tree
    /// scans are both weakly consistent; the Map implementation releases shard locks
    /// between batches, so concurrent writes may be observed partially.
    pub fn iter_view(&self) -> Option<TypedMapIterView<'_, K, T, C, H>> {
        self.iterable.then(|| TypedMapIterView {
            map: self,
            inner: MapIterView::new(self, dir_from(self.reversed)),
        })
    }

    /// Field-granular prefix / range / page builder. `None` unless opened `iterable(true)`.
    pub fn scope<const N: usize>(
        &self,
        key: &K,
    ) -> Option<crate::scope::Scope<'_, crate::scope::MapScan<&Self>, N>> {
        crate::scope::map_scope::<_, _, N>(self, key, dir_from(self.reversed()), self.iterable)
    }

    /// Arc-owned prefix/range builder for [`crate::Tx::delete_scope`].
    /// Returns `None` unless this map was opened iterable.
    #[cfg(feature = "armour")]
    pub fn tx_scope<const N: usize>(
        self: &Arc<Self>,
        key: &K,
    ) -> Option<crate::TxScope<Self, K, N>> {
        self.iterable.then(|| {
            crate::txn::scope::TxScope::from_parts(
                self.clone(),
                key,
                dir_from(self.reversed()),
                Self::tx_scope_keys,
            )
        })
    }

    fn tx_scope_keys<'a>(
        collection: &'a Self,
        start: Bound<&'a K>,
        end: Bound<&'a K>,
        dir: Dir,
    ) -> Box<dyn Iterator<Item = K> + 'a> {
        Box::new(MapIterView::new(collection, dir).keys_range_bounds(start, end))
    }

    /// First `(key, value)` in configured iteration order, or `None` if empty.
    ///
    /// Map scans are weakly consistent: shard locks are released between batches, so
    /// concurrent writes may be observed partially.
    pub fn first(&self) -> DbResult<Option<(K, TypedRef<'_, T>)>> {
        let view = self.iter_view().ok_or(DbError::NotImplemented)?;
        Ok(view.iter().next())
    }

    /// Last `(key, value)` in configured iteration order, or `None` if empty.
    ///
    /// Map scans are weakly consistent: shard locks are released between batches, so
    /// concurrent writes may be observed partially.
    pub fn last(&self) -> DbResult<Option<(K, TypedRef<'_, T>)>> {
        self.iter_view().ok_or(DbError::NotImplemented)?;
        let view = TypedMapIterView {
            map: self,
            inner: MapIterView::new(self, dir_from(self.reversed).flip()),
        };
        Ok(view.iter().next())
    }

    /// Shard count for limit-derived inspection batch sizing.
    pub(crate) fn inspection_shard_count(&self) -> usize {
        self.engine.shards().len()
    }

    /// Hold a seize guard while inspection code dereferences raw `TypedData` pointers.
    pub(crate) fn inspection_guard(&self) -> seize::LocalGuard<'_> {
        self.collector.enter()
    }

    /// Strict bounded ordered scan for inspection (not the public `iter_view`).
    pub(crate) fn inspection_range_bounds(
        &self,
        start: Bound<&K>,
        end: Bound<&K>,
        batch: usize,
    ) -> TryMapIter<'_, TypedMap<K, T, C, H>> {
        MapIterView::with_batch(self, dir_from(self.reversed), batch).try_range_bounds(start, end)
    }

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

    /// Read-only pass over all live entries. Order is unspecified; entries
    /// inserted/removed concurrently may be missed or visited once — intended
    /// for schema validation on a quiet database.
    pub fn for_each(&self, mut f: impl FnMut(&K, &T)) {
        let _guard = self.collector.enter();
        for i in 0..self.engine.shards().len() {
            let pairs: Vec<(K, *const TypedData<T>)> = {
                let index = sync::lock(&self.indexes[i]);
                index
                    .iter()
                    .map(|(k, entry)| (*k, entry.ptr as *const TypedData<T>))
                    .collect()
            };
            for (key, ptr) in pairs {
                let data = unsafe { &*ptr };
                f(&key, &data.value);
            }
        }
    }

    /// Unordered snapshot visitor for inspection; stops when `f` returns `Break`.
    pub(crate) fn for_each_while(
        &self,
        mut f: impl FnMut(&K, &T) -> ControlFlow<()>,
    ) -> ControlFlow<()> {
        let _guard = self.collector.enter();
        for i in 0..self.engine.shards().len() {
            let pairs: Vec<(K, *const TypedData<T>)> = {
                let index = sync::lock(&self.indexes[i]);
                index
                    .iter()
                    .map(|(k, entry)| (*k, entry.ptr as *const TypedData<T>))
                    .collect()
            };
            for (key, ptr) in pairs {
                let data = unsafe { &*ptr };
                if let ControlFlow::Break(()) = f(&key, &data.value) {
                    return ControlFlow::Break(());
                }
            }
        }
        ControlFlow::Continue(())
    }

    // -- Migrate --------------------------------------------------------------

    /// Iterate all entries and optionally mutate them. Call once at startup.
    ///
    /// **Must run before any other writer obtains a handle to this collection.**
    /// Concurrent writes during migration produce undefined results.
    ///
    /// The callback receives each (key, &T) and returns `MigrateAction`:
    /// - `Keep` — no change (fires `on_init` if `NEEDS_INIT`)
    /// - `Update(new_value)` — replace value (hook-free write, fires `on_init`)
    /// - `Delete` — remove entry (hook-free tombstone)
    ///
    /// Returns the number of mutated entries.
    pub fn migrate(&self, f: impl Fn(&K, &T) -> crate::MigrateAction<T>) -> DbResult<usize> {
        self.migrate_inner(f, true)
    }

    pub(crate) fn migrate_inner(
        &self,
        f: impl Fn(&K, &T) -> crate::MigrateAction<T>,
        fire_init: bool,
    ) -> DbResult<usize> {
        use crate::MigrateAction;

        let mut count = 0;
        for i in 0..self.engine.shards().len() {
            let keys: Vec<K> = {
                let index = sync::lock(&self.indexes[i]);
                index.keys().copied().collect()
            };
            for key in keys {
                let value_ref = match self.get(&key) {
                    Some(v) => v,
                    None => continue,
                };
                let action = f(&key, &*value_ref);
                drop(value_ref);

                match action {
                    MigrateAction::Keep => {
                        if fire_init
                            && H::NEEDS_INIT
                            && let Some(v) = self.get(&key)
                        {
                            self.hook.on_init(&key, &*v);
                        }
                    }
                    MigrateAction::Update(value) => {
                        let shard_id = self.shard_for(&key);
                        {
                            let mut inner = self.engine.shards()[shard_id].lock();
                            let mut index = sync::lock(&self.indexes[shard_id]);
                            let guard = self.collector.enter();
                            // `Derive`, not `Precomputed(None)`: a migration rewrites the
                            // value, so the row's deadline must be recomputed from it.
                            // Passing `None` here would clear the deadline of every
                            // migrated row — invisible until the row failed to expire.
                            self.put_locked_inner::<false>(
                                shard_id,
                                &mut inner,
                                &mut index,
                                &guard,
                                &key,
                                value,
                                crate::ttl::TtlAt::Derive,
                            )?;
                        }
                        if fire_init
                            && H::NEEDS_INIT
                            && let Some(v) = self.get(&key)
                        {
                            self.hook.on_init(&key, &*v);
                        }
                        count += 1;
                    }
                    MigrateAction::Delete => {
                        let shard_id = self.shard_for(&key);
                        let mut inner = self.engine.shards()[shard_id].lock();
                        let mut index = sync::lock(&self.indexes[shard_id]);
                        let guard = self.collector.enter();
                        self.delete_locked_inner::<false>(
                            shard_id, &mut inner, &mut index, &guard, &key,
                        )?;
                        count += 1;
                    }
                }
            }
        }

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

    pub(crate) fn replay_init(&self) {
        if !H::NEEDS_INIT {
            return;
        }
        let _guard = self.collector.enter();
        for shard in &self.indexes {
            let pairs: Vec<(K, *const TypedData<T>)> = {
                let index = sync::lock(shard);
                index
                    .iter()
                    .map(|(k, entry)| (*k, entry.ptr as *const TypedData<T>))
                    .collect()
            };
            for (key, ptr) in pairs {
                let data = unsafe { &*ptr };
                self.hook.on_init(&key, &data.value);
            }
        }
    }

    /// The budget one `project` call runs under: `min(hook budget, config
    /// budget)`. Every projection site — facade write, `atomic()`/TTL replay,
    /// transaction commit, migration reconcile — and the schema validator read
    /// this one accessor, so `validate`/`repair` can never evaluate a different
    /// contract than writes do.
    pub(crate) fn effective_projection_limits(&self) -> (usize, usize) {
        let cfg = self.config();
        let (hook_rows, hook_bytes) = self.hook.projection_limits();
        (
            hook_rows.min(cfg.max_projected_rows),
            hook_bytes.min(cfg.max_projected_bytes),
        )
    }

    /// T10 Task 5: refuse a non-transactional write that would change this
    /// source's claim set, before any mutation. See `TypedTree::claim_guard`
    /// (identical shape) for the full rationale.
    #[allow(clippy::collapsible_if)]
    #[inline]
    fn claim_guard(&self, key: &K, old: Option<&T>, new: Option<&T>) -> DbResult<()> {
        #[cfg(feature = "armour")]
        if H::EMITS && !crate::txn_depth::in_stream_apply() {
            if let Some(targets) = self.hook.stream_targets() {
                let collection = self
                    .counter_dirty
                    .get()
                    .map(crate::counter_dirty::CounterDirty::name)
                    .unwrap_or("");
                return Err(DbError::ChangeStreamRequiresTx {
                    collection,
                    queue: targets.first_name(),
                });
            }
        }
        if !H::CLAIMS {
            return Ok(());
        }
        // F1 fix (review): see `TypedTree::claim_guard` — `H::CLAIMS` is
        // `true` for every `Projected` carrier, not only claiming ones;
        // check the runtime declaration before doing anything else.
        if self.hook.claim_targets().is_none() {
            return Ok(());
        }
        let collection = self
            .counter_dirty
            .get()
            .map(crate::counter_dirty::CounterDirty::name)
            .unwrap_or("");
        crate::unique::guard_nontx_write(
            collection,
            &self.hook,
            self.effective_projection_limits(),
            key,
            old,
            new,
        )
    }

    pub(crate) fn reconcile_projections(&self) -> DbResult<()> {
        if !H::PROJECTS {
            return Ok(());
        }
        let Some(targets) = self.hook.projection_targets() else {
            return Ok(());
        };
        let (max_rows, max_bytes) = self.effective_projection_limits();
        crate::projection::reconcile_carrier_projections(
            targets,
            |emit| {
                let mut err: Option<DbError> = None;
                self.for_each(|k, v| {
                    if err.is_some() {
                        return;
                    }
                    let mut sink =
                        crate::projection::ProjectionSink::new(targets, max_rows, max_bytes);
                    match self.hook.project(k, v, &mut sink).and_then(|()| {
                        for row in sink.rows() {
                            emit(row.target, &row.key, &row.value)?;
                        }
                        Ok(())
                    }) {
                        Ok(()) => {}
                        Err(e) => err = Some(e),
                    }
                });
                err.map_or(Ok(()), Err)
            },
            true,
        )
    }

    /// Open-time / on-demand recount (T9 Task 8) — see `counter::recount` for
    /// the shared two-pass walk. No-op (`Ok(0)`) when this collection is not
    /// itself a counted source.
    pub(crate) fn reconcile_counters(&self) -> DbResult<u64> {
        if !H::COUNTS {
            return Ok(0);
        }
        let Some(targets) = self.hook.counter_targets() else {
            return Ok(0);
        };
        let limits = self.effective_projection_limits();
        crate::counter::recount(
            |k: &K, v: &T, sink| self.hook.counter_project(k, v, sink),
            targets,
            limits,
            &mut |visit| {
                let mut err: Option<DbError> = None;
                self.for_each(|k, v| {
                    if err.is_some() {
                        return;
                    }
                    if let Err(e) = visit(k, v) {
                        err = Some(e);
                    }
                });
                err.map_or(Ok(()), Err)
            },
        )
    }

    /// The collections whose `counters_dirty` flag this collection must also
    /// consult at open time (T9 Task 8) — one entry per declared counter
    /// target. Empty when this collection is not a counted source.
    pub(crate) fn counter_target_names(&self) -> Vec<&'static str> {
        self.hook
            .counter_targets()
            .map(|t| t.all().map(|m| m.collection_name()).collect())
            .unwrap_or_default()
    }

    // -- Private --------------------------------------------------------------

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

    /// Fire deferred on_write and retire old data. Called after lock release.
    ///
    /// SAFETY: caller must hold a live `seize::LocalGuard` that was entered
    /// before the mutation. This guarantees old/new value pointers are valid
    /// (not yet reclaimed) even if a reentrant hook retires them.
    pub(crate) fn fire_hook_and_retire(&self, key: &K, mr: &MutationResult<K, T>) {
        if let Some((old, new)) = mr.hook {
            unsafe {
                self.hook.on_write(key, old.map(|p| &*p), new.map(|p| &*p));
            }
        }
        if let Some(ptr) = mr.retire {
            unsafe {
                self.collector
                    .retire(ptr, seize::reclaim::boxed::<TypedData<T>>);
            }
        }
    }

    /// Persist `counters_dirty = true` for this collection (idempotent per
    /// session — see `CounterDirty::mark`). No-op if nothing is bound (a
    /// direct open, or no counter targets declared).
    ///
    /// Called once, from `Db::open_managed`, *before* the collection is
    /// published — never from the write path. See `counter_dirty.rs`.
    pub(crate) fn mark_counter_dirty(&self) -> DbResult<()> {
        match self.counter_dirty.get() {
            Some(d) => d.mark(),
            None => Ok(()),
        }
    }

    fn apply_projection_for_mutation(&self, ev: &(K, MutationResult<K, T>)) -> DbResult<()> {
        if !H::PROJECTS && !H::COUNTS {
            return Ok(());
        }
        let (key, mr) = ev;
        let Some((old_ptr, new_ptr)) = mr.hook else {
            return Ok(());
        };
        let old = old_ptr.map(|p| unsafe { &*p });
        let new = new_ptr.map(|p| unsafe { &*p });
        let (max_rows, max_bytes) = self.effective_projection_limits();

        let mut plan = match (H::PROJECTS, self.hook.projection_targets()) {
            (true, Some(targets)) => crate::projection::diff_projection(
                &self.hook, targets, max_rows, max_bytes, key, old, new,
            )?,
            _ => crate::projection::PendingProjection::default(),
        };
        if H::COUNTS
            && let Some(counter_targets) = self.hook.counter_targets()
        {
            let deltas = crate::counter::diff_counters(
                &self.hook,
                counter_targets,
                max_rows,
                max_bytes,
                key,
                old,
                new,
            )?;
            if !deltas.is_empty() {
                plan.set_deltas(deltas, counter_targets.clone());
            }
        }
        plan.apply_or_incomplete()
    }

    #[allow(clippy::too_many_arguments)]
    fn put_locked_inner<const HOOKS: bool>(
        &self,
        shard_id: usize,
        inner: &mut ShardInner,
        index: &mut ShardIndex<K, TypedMapEntry<T>>,
        _guard: &seize::LocalGuard<'_>,
        key: &K,
        value: T,
        expires_at: crate::ttl::TtlAt,
    ) -> DbResult<MutationResult<K, T>> {
        // Resolve before `value` is moved into the index below.
        let expires_at = match expires_at {
            crate::ttl::TtlAt::Precomputed(at) => at,
            crate::ttl::TtlAt::Derive if H::EXPIRES => self.hook.deadline(key, &value),
            crate::ttl::TtlAt::Derive => None,
        };
        // T10 Task 5: before any mutation — a non-mutating, additive lookup
        // (only when `H::CLAIMS`), since `index.upsert` below both reads the
        // old entry and writes the new one in a single call and cannot be
        // used for a pre-mutation read. Mirrors `TypedTree::put_locked_inner`.
        //
        // Gated on `claim_targets().is_some()` too (F1 fix, review): see
        // `TypedTree::put_locked_inner` for why.
        if H::EMITS || (H::CLAIMS && self.hook.claim_targets().is_some()) {
            #[cfg(feature = "ctx-gates")]
            crate::ctx_gates_sync::record_claim_pre_read();
            let old = index.get(key).map(|e| unsafe { &(*e.ptr).value });
            self.claim_guard(key, old, Some(&value))?;
        }
        let mut buf = sync::lock(&self.encode_bufs[shard_id]);
        self.codec.encode_to(&value, &mut buf)?;
        let (disk_loc, _gsn) = inner.append_entry(shard_id as u8, key.as_bytes(), &buf, false)?;

        let new_data_ptr = Box::into_raw(Box::new(TypedData {
            disk: disk_loc,
            value,
        }));

        if let Some(old_entry) = index.upsert(*key, TypedMapEntry { ptr: new_data_ptr }) {
            let old_data = old_entry.ptr as *const TypedData<T>;

            let old_disk = unsafe { (*old_entry.ptr).disk };
            inner.add_dead_bytes(
                old_disk.file_id,
                crate::entry::entry_size(size_of::<K>(), old_disk.len),
            );

            if HOOKS {
                self.record_ttl(shard_id, key, expires_at);
                return Ok(MutationResult {
                    ref_ptr: Some(old_data),
                    hook: Some((
                        Some(unsafe { &(*old_data).value as *const T }),
                        Some(unsafe { &(*new_data_ptr).value as *const T }),
                    )),
                    retire: Some(old_entry.ptr),
                    new_disk: Some(disk_loc),
                    watch: None,
                });
            }
            self.record_ttl(shard_id, key, expires_at);
            unsafe {
                self.collector
                    .retire(old_entry.ptr, seize::reclaim::boxed::<TypedData<T>>);
            }
            Ok(MutationResult {
                ref_ptr: Some(old_data),
                hook: None,
                retire: None,
                new_disk: None,
                watch: None,
            })
        } else {
            self.len.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
            self.record_ttl(shard_id, key, expires_at);
            let hook = if HOOKS {
                Some((None, Some(unsafe { &(*new_data_ptr).value as *const T })))
            } else {
                None
            };
            Ok(MutationResult {
                ref_ptr: None,
                hook,
                retire: None,
                new_disk: Some(disk_loc),
                watch: None,
            })
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn insert_locked<const HOOKS: bool>(
        &self,
        shard_id: usize,
        inner: &mut ShardInner,
        index: &mut ShardIndex<K, TypedMapEntry<T>>,
        _guard: &seize::LocalGuard<'_>,
        key: &K,
        value: T,
        expires_at: Option<u64>,
    ) -> DbResult<MutationResult<K, T>> {
        if index.contains_key(key) {
            return Err(DbError::KeyExists);
        }
        // An insert only ever reaches here on an absent key, so `old` is
        // always `None` — still routes through the guard: a source that
        // claims must reserve its very first claim inside `db.tx()` too.
        self.claim_guard(key, None, Some(&value))?;

        let mut buf = sync::lock(&self.encode_bufs[shard_id]);
        self.codec.encode_to(&value, &mut buf)?;
        let (disk_loc, _gsn) = inner.append_entry(shard_id as u8, key.as_bytes(), &buf, false)?;

        let new_data_ptr = Box::into_raw(Box::new(TypedData {
            disk: disk_loc,
            value,
        }));

        index.upsert(*key, TypedMapEntry { ptr: new_data_ptr });
        self.len.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        self.record_ttl(shard_id, key, expires_at);
        let hook = if HOOKS {
            Some((None, Some(unsafe { &(*new_data_ptr).value as *const T })))
        } else {
            None
        };
        Ok(MutationResult {
            ref_ptr: None,
            hook,
            retire: None,
            new_disk: Some(disk_loc),
            watch: None,
        })
    }

    fn delete_locked_inner<const HOOKS: bool>(
        &self,
        shard_id: usize,
        inner: &mut ShardInner,
        index: &mut ShardIndex<K, TypedMapEntry<T>>,
        _guard: &seize::LocalGuard<'_>,
        key: &K,
    ) -> DbResult<MutationResult<K, T>> {
        #[cfg(test)]
        if crate::durability::ttl_delete_should_fail() {
            return Err(DbError::Internal("test: TTL delete injected failure"));
        }
        let entry = match index.get(key) {
            Some(e) => e,
            None => {
                return Ok(MutationResult {
                    ref_ptr: None,
                    hook: None,
                    retire: None,
                    new_disk: None,
                    watch: None,
                });
            }
        };

        let old_data = entry.ptr as *const TypedData<T>;

        // T10 Task 5: a delete is a pure release ({k} -> ∅) — refused outside
        // `db.tx()` exactly like an insert's pure claim (see `insert_locked`).
        self.claim_guard(key, Some(unsafe { &(*old_data).value }), None)?;

        inner.append_entry(shard_id as u8, key.as_bytes(), &[], true)?;

        let removed = index.remove(key).expect("key exists after probe");
        self.len.fetch_sub(1, std::sync::atomic::Ordering::Relaxed);

        let old_disk = unsafe { (*removed.ptr).disk };
        inner.add_dead_bytes(
            old_disk.file_id,
            crate::entry::entry_size(size_of::<K>(), old_disk.len),
        );

        if HOOKS {
            self.record_ttl(shard_id, key, None);
            Ok(MutationResult {
                ref_ptr: Some(old_data),
                hook: Some((Some(unsafe { &(*old_data).value as *const T }), None)),
                retire: Some(removed.ptr),
                new_disk: None,
                watch: None,
            })
        } else {
            self.record_ttl(shard_id, key, None);
            unsafe {
                self.collector
                    .retire(removed.ptr, seize::reclaim::boxed::<TypedData<T>>);
            }
            Ok(MutationResult {
                ref_ptr: Some(old_data),
                hook: None,
                retire: None,
                new_disk: None,
                watch: None,
            })
        }
    }
}

impl<K: Key + Send + Sync + Hash + Eq, T: Send + Sync, C: Codec<T>, H: TypedWriteHook<K, T>> Drop
    for TypedMap<K, T, C, H>
{
    fn drop(&mut self) {
        for index_mutex in &self.indexes {
            let map = sync::lock(index_mutex);
            for entry in map.values() {
                unsafe {
                    drop(Box::from_raw(entry.ptr));
                }
            }
        }
    }
}

impl<K: Key + Send + Sync + Hash + Eq, T: Send + Sync, C: Codec<T> + Sync, H: TypedWriteHook<K, T>>
    ShardSource for TypedMap<K, T, C, H>
{
    type Key = K;
    type Val = *const TypedData<T>;

    fn shard_count(&self) -> usize {
        self.engine.shards().len()
    }

    fn next_batch(
        &self,
        shard: usize,
        after: Option<&K>,
        dir: Dir,
        n: usize,
        range_start: Bound<OrdKey<K>>,
        range_end: Bound<OrdKey<K>>,
    ) -> Vec<(K, *const TypedData<T>)> {
        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.ptr as *const TypedData<T>));
            }
        }
        out
    }

    fn next_keys_batch(
        &self,
        shard: usize,
        after: Option<&K>,
        dir: Dir,
        n: usize,
        range_start: Bound<OrdKey<K>>,
        range_end: Bound<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 [`TypedMap`]; exposes ordered iteration yielding
/// guard-protected [`TypedRef`] values.
pub struct TypedMapIterView<
    'a,
    K: Key + Send + Sync + Hash + Eq,
    T: Send + Sync,
    C: Codec<T> + Sync,
    H: TypedWriteHook<K, T>,
> {
    map: &'a TypedMap<K, T, C, H>,
    inner: MapIterView<'a, TypedMap<K, T, C, H>>,
}

impl<'a, K, T, C, H> TypedMapIterView<'a, K, T, C, H>
where
    K: Key + Send + Sync + Hash + Eq,
    T: Send + Sync,
    C: Codec<T> + Sync,
    H: TypedWriteHook<K, T>,
{
    /// Full ordered scan in the collection's configured direction.
    pub fn iter(&self) -> TypedMapIter<'a, K, T, C, H> {
        let guard = self.map.collector.enter();
        TypedMapIter {
            map: self.map,
            inner: self.inner.iter(),
            _guard: guard,
        }
    }

    /// Iterate entries in `[start, end)` — start inclusive, end exclusive.
    pub fn range(&self, start: &K, end: &K) -> TypedMapIter<'a, K, T, C, H> {
        let guard = self.map.collector.enter();
        TypedMapIter {
            map: self.map,
            inner: self.inner.range(start, end),
            _guard: guard,
        }
    }

    /// Iterate entries in range defined by `start` and `end` bounds.
    pub fn range_bounds(&self, start: Bound<&K>, end: Bound<&K>) -> TypedMapIter<'a, K, T, C, H> {
        let guard = self.map.collector.enter();
        TypedMapIter {
            map: self.map,
            inner: self.inner.range_bounds(start, end),
            _guard: guard,
        }
    }

    /// Iterate entries whose keys start with `prefix`.
    pub fn prefix_iter(&self, prefix: &[u8]) -> TypedMapIter<'a, K, T, C, H> {
        let guard = self.map.collector.enter();
        TypedMapIter {
            map: self.map,
            inner: self.inner.prefix_iter(prefix),
            _guard: guard,
        }
    }

    /// Keyset pagination in the collection's configured direction.
    ///
    /// Requires `T: Clone` because each item is collected as an owned value.
    pub fn paginate(&self, after: Option<&K>, limit: usize) -> Page<K, T>
    where
        T: Clone,
    {
        let iter = match (after, dir_from(self.map.reversed)) {
            (None, _) => self.iter(),
            (Some(k), Dir::Asc) => self.range_bounds(Bound::Excluded(k), Bound::Unbounded),
            (Some(k), Dir::Desc) => self.range_bounds(Bound::Unbounded, Bound::Excluded(k)),
        };
        let items: Vec<_> = iter
            .take(limit)
            .map(|(k, r)| (k, r.clone_value()))
            .collect();
        let next = if items.len() == limit {
            items.last().map(|(k, _)| *k)
        } else {
            None
        };
        Page { items, next }
    }

    // -- Keys-only iteration -------------------------------------------------
    //
    // Keys are owned `K` values, so — unlike value iteration — these need no
    // `seize` guard (no pointer is dereferenced) and never touch the stored `T`.

    /// Full ordered **keys-only** scan — no value dereferenced, no guard needed.
    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 pointer-identity
    /// compare-delete (no `T: PartialEq` required).
    ///
    /// If the entry was replaced or removed concurrently (`CasMismatch` /
    /// `KeyNotFound`), the delete is skipped. Not atomic across the collection; a
    /// genuine storage error aborts the scan and is returned.
    pub fn retain(&self, mut f: impl FnMut(&K, &T) -> bool) -> DbResult<()> {
        let _guard = self.map.collector.enter();
        for (key, ptr) in self.inner.iter() {
            let val = unsafe { &(*ptr).value };
            if f(&key, val) {
                continue;
            }
            match self.map.compare_delete_if_ptr(&key, ptr) {
                Ok(()) => {}
                Err(DbError::CasMismatch) | Err(DbError::KeyNotFound) => {
                    tracing::debug!("retain: compare_delete skipped (concurrent change)");
                }
                Err(e) => return Err(e),
            }
        }
        Ok(())
    }
}

/// Iterator over entries in an iterable [`TypedMap`].
///
/// Weakly-consistent: concurrent inserts/updates may be visible during iteration.
/// The iterator holds a `seize` guard for its lifetime so batched raw pointers
/// remain valid; each returned [`TypedRef`] carries an additional guard so values
/// outlive the iterator.
pub struct TypedMapIter<
    'a,
    K: Key + Send + Sync + Hash + Eq,
    T: Send + Sync,
    C: Codec<T> + Sync,
    H: TypedWriteHook<K, T>,
> {
    map: &'a TypedMap<K, T, C, H>,
    inner: MapIter<'a, TypedMap<K, T, C, H>>,
    _guard: seize::LocalGuard<'a>,
}

impl<'a, K, T, C, H> Iterator for TypedMapIter<'a, K, T, C, H>
where
    K: Key + Send + Sync + Hash + Eq,
    T: Send + Sync,
    C: Codec<T> + Sync,
    H: TypedWriteHook<K, T>,
{
    type Item = (K, TypedRef<'a, T>);

    fn next(&mut self) -> Option<Self::Item> {
        let (k, ptr) = self.inner.next()?;
        let guard = self.map.collector.enter();
        Some((k, TypedRef::new(guard, ptr)))
    }
}

// ---------------------------------------------------------------------------
// TypedMapShard
// ---------------------------------------------------------------------------

/// Handle for atomic multi-key operations within a single shard.
/// Obtained via [`TypedMap::atomic`]. The shard + index locks are held for the
/// lifetime of this struct — keep the closure short.
pub struct TypedMapShard<
    'a,
    K: Key + Send + Sync + Hash + Eq,
    T: Send + Sync,
    C: Codec<T>,
    H: TypedWriteHook<K, T> = NoHook,
> {
    map: &'a TypedMap<K, T, C, H>,
    pub(crate) held: Held<'a, TypedMap<K, T, C, H>>,
    guard: seize::LocalGuard<'a>,
}

impl<K: Key + Send + Sync + Hash + Eq, T: Send + Sync, C: Codec<T> + Sync, H: TypedWriteHook<K, T>>
    TypedMapShard<'_, K, T, C, H>
{
    pub fn put(&mut self, key: &K, value: T) -> DbResult<Option<TypedRef<'_, T>>> {
        self.check_shard(key)?;
        let map = self.map;
        let sid = self.held.sid;
        let ref_ptr = {
            let guard = &self.guard;
            Ctx::with_shard(&mut self.held, map, sid, |locked, ev_out| {
                let (inner, index) = locked;
                let mr = map.put_locked_inner::<true>(
                    sid,
                    inner,
                    index,
                    guard,
                    key,
                    value,
                    crate::ttl::TtlAt::Derive,
                )?;
                let ref_ptr = mr.ref_ptr;
                *ev_out = map.defer_or_retire_event(key, mr);
                Ok(ref_ptr)
            })?
        };
        let guard = self.map.collector.enter();
        Ok(ref_ptr.map(|p| TypedRef::new(guard, p)))
    }

    pub fn insert(&mut self, key: &K, value: T) -> DbResult<()> {
        self.check_shard(key)?;
        let map = self.map;
        let sid = self.held.sid;
        let expires_at = if H::EXPIRES {
            map.hook.deadline(key, &value)
        } else {
            None
        };
        let guard = &self.guard;
        Ctx::with_shard(&mut self.held, map, sid, |locked, ev_out| {
            let (inner, index) = locked;
            let mr = map.insert_locked::<true>(sid, inner, index, guard, key, value, expires_at)?;
            *ev_out = map.defer_or_retire_event(key, mr);
            Ok(())
        })
    }

    pub fn delete(&mut self, key: &K) -> DbResult<Option<TypedRef<'_, T>>> {
        self.check_shard(key)?;
        let map = self.map;
        let sid = self.held.sid;
        let ref_ptr = {
            let guard = &self.guard;
            Ctx::with_shard(&mut self.held, map, sid, |locked, ev_out| {
                let (inner, index) = locked;
                let mr = map.delete_locked_inner::<true>(sid, inner, index, guard, key)?;
                let ref_ptr = mr.ref_ptr;
                *ev_out = map.defer_or_retire_event(key, mr);
                Ok(ref_ptr)
            })?
        };
        let guard = self.map.collector.enter();
        Ok(ref_ptr.map(|p| TypedRef::new(guard, p)))
    }

    pub fn try_get(&self, key: &K) -> DbResult<Option<&T>> {
        self.check_shard(key)?;
        Ok(match self.held.locked.1.get(key) {
            Some(entry) => Some(unsafe { &(*entry.ptr).value }),
            None => None,
        })
    }

    pub fn get_or_err(&self, key: &K) -> DbResult<&T> {
        self.try_get(key)?.ok_or(DbError::KeyNotFound)
    }

    pub fn try_contains(&self, key: &K) -> DbResult<bool> {
        self.check_shard(key)?;
        Ok(self.held.locked.1.contains_key(key))
    }

    /// Atomically read-modify-write within a shard transaction.
    /// Returns `Some(TypedRef)` to the **new** value if key existed, `None` otherwise.
    pub fn update(
        &mut self,
        key: &K,
        f: impl FnOnce(&T) -> T,
    ) -> DbResult<Option<TypedRef<'_, T>>> {
        self.update_inner(key, f, false)
    }

    /// Like [`update`](Self::update), but returns `Some(TypedRef)` to the **old** value.
    pub fn fetch_update(
        &mut self,
        key: &K,
        f: impl FnOnce(&T) -> T,
    ) -> DbResult<Option<TypedRef<'_, T>>> {
        self.update_inner(key, f, true)
    }

    /// Decide-and-write within the shard. The closure must be short — shard lock is held.
    pub fn upsert<U: crate::IntoUpsert<T>>(
        &mut self,
        key: &K,
        f: impl FnOnce(Option<&T>) -> U,
    ) -> crate::DbResult<U::Out> {
        crate::upsert::upsert_via_held(self, key, f)
    }

    fn update_inner(
        &mut self,
        key: &K,
        f: impl FnOnce(&T) -> T,
        return_old: bool,
    ) -> DbResult<Option<TypedRef<'_, T>>> {
        self.check_shard(key)?;
        let map = self.map;
        let sid = self.held.sid;
        let ref_ptr = {
            Ctx::with_shard(&mut self.held, map, sid, |locked, ev_out| {
                let (inner, index) = locked;
                let ptrs = match map.update_locked_inner(sid, inner, index, key, f)? {
                    Some(p) => p,
                    None => return Ok(None),
                };
                let ref_ptr = if return_old { ptrs.old } else { ptrs.new };
                let mr = MutationResult {
                    ref_ptr: None,
                    hook: Some((
                        Some(unsafe { &(*ptrs.old).value as *const T }),
                        Some(unsafe { &(*ptrs.new).value as *const T }),
                    )),
                    retire: Some(ptrs.retire),
                    new_disk: Some(ptrs.new_disk),
                    watch: None,
                };
                *ev_out = map.defer_or_retire_event(key, mr);
                Ok(Some(ref_ptr))
            })?
        };
        let guard = self.map.collector.enter();
        Ok(ref_ptr.map(|p| TypedRef::new(guard, p)))
    }

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

impl<K: Key + Send + Sync + Hash + Eq, T: Send + Sync, C: Codec<T> + Sync, H: TypedWriteHook<K, T>>
    TypedMapShard<'_, K, T, C, H>
{
    pub(crate) fn tx_arm_gsn(&mut self, gsn: u64) {
        self.held.locked.0.arm_gsn(gsn);
    }

    pub(crate) fn tx_clear_armed_gsn(&mut self) {
        self.held.locked.0.clear_armed_gsn();
    }

    /// Refuse further appends on this shard: a committed transaction operation
    /// reserved `gsn` and never wrote it, so only replay can complete it.
    /// Returns the effective fence (an earlier, lower one wins).
    pub(crate) fn tx_fence(&mut self, gsn: u64) -> u64 {
        self.held.locked.0.fence(gsn)
    }

    /// Hook events buffered so far. The transaction reads it around every
    /// mutation to learn how many events that mutation produced — a write that
    /// changed nothing produces none.
    pub(crate) fn tx_buffered_events(&self) -> usize {
        self.held.events.len()
    }
}

#[cfg(feature = "armour")]
impl<T, C, H> crate::armour::collection::Collection for TypedMap<T::SelfId, T, C, H>
where
    T: crate::CollectionMeta + Clone + Send + Sync,
    C: crate::Codec<T> + Sync,
    H: crate::hook::TypedWriteHook<T::SelfId, T>,
    T::SelfId: crate::Key + Send + Sync + std::hash::Hash + Eq,
{
    fn name(&self) -> &str {
        T::NAME
    }
    fn len(&self) -> usize {
        self.len()
    }
    fn compact(&self) -> crate::DbResult<usize> {
        self.compact()
    }
    fn flush(&self) -> crate::DbResult<()> {
        crate::projection::collection_flush_fault()?;
        self.flush_buffers()?;
        if self.engine.hints() {
            self.sync_hints()?;
        }
        Ok(())
    }
    fn dirty_shards(&self) -> Option<usize> {
        Some(self.engine.dirty_shards())
    }
    fn periodic_flush(&self) -> crate::DbResult<()> {
        TypedMap::durable_flush(self)
    }
    fn durable_flush(&self) -> crate::DbResult<()> {
        TypedMap::durable_flush(self)
    }
    fn clean_shutdown(&self) -> crate::DbResult<()> {
        TypedMap::clean_shutdown(self)
    }
    fn sweep_expired(
        &self,
        now_ms: u64,
        budget: usize,
    ) -> crate::DbResult<crate::ttl::SweepOutcome> {
        self.sweep_expired(now_ms, budget)
    }
    fn set_ttl_sweep(&self, enabled: bool) -> crate::DbResult<()> {
        self.set_ttl_sweep(enabled)
    }
    fn rebuild_ttl_index(&self) -> crate::DbResult<()> {
        self.rebuild_ttl_index()
    }
    fn reset_counter_dirty_marker(&self) -> bool {
        self.counter_dirty.get().is_some_and(|d| d.take_marked())
    }
    fn reconcile_counters(&self) -> crate::DbResult<u64> {
        TypedMap::reconcile_counters(self)
    }
    fn counter_target_names(&self) -> Vec<&'static str> {
        TypedMap::counter_target_names(self)
    }
    fn close_watches(&self) {
        if let Some(channel) = self.hook.watch_channel() {
            channel.close_terminal();
        }
    }
}

#[cfg(feature = "typed-tree")]
impl<
    K: Key + Send + Sync + std::hash::Hash + Eq + 'static,
    T: armour_core::GetType
        + crate::tree_meta::CollectionMeta
        + Clone
        + PartialEq
        + Send
        + Sync
        + 'static,
    C: Codec<T> + Sync + 'static,
    H: TypedWriteHook<K, T> + 'static,
> crate::projection::sealed::Sealed for TypedMap<K, T, C, H>
{
}

impl<
    K: Key + Send + Sync + std::hash::Hash + Eq + 'static,
    T: armour_core::GetType
        + crate::tree_meta::CollectionMeta
        + Clone
        + PartialEq
        + Send
        + Sync
        + 'static,
    C: Codec<T> + Sync + 'static,
    H: TypedWriteHook<K, T> + 'static,
> crate::projection::ProjectableIndex for TypedMap<K, T, C, H>
{
    fn collection_name(&self) -> &'static str {
        T::NAME
    }

    fn put_bytes(&self, key: &[u8], value: &[u8]) -> DbResult<()> {
        if key.len() != size_of::<K>() {
            return Err(DbError::CorruptedEntry { offset: 0 });
        }
        let k = K::from_bytes(key);
        let v = self.codec().decode_from(value)?;
        self.put(&k, v)?;
        Ok(())
    }

    fn delete_bytes(&self, key: &[u8]) -> DbResult<()> {
        if key.len() != size_of::<K>() {
            return Err(DbError::CorruptedEntry { offset: 0 });
        }
        let k = K::from_bytes(key);
        self.delete(&k)?;
        Ok(())
    }

    fn values_equal(&self, a: &[u8], b: &[u8]) -> DbResult<bool> {
        if a == b {
            return Ok(true);
        }
        let va = self.codec().decode_from(a)?;
        let vb = self.codec().decode_from(b)?;
        Ok(va == vb)
    }

    fn for_each_bytes(&self, f: &mut dyn FnMut(&[u8], &[u8]) -> DbResult<()>) -> DbResult<()> {
        let mut err: Option<DbError> = None;
        self.for_each(|k, v| {
            if err.is_some() {
                return;
            }
            let mut buf = Vec::new();
            match self
                .codec()
                .encode_to(v, &mut buf)
                .and_then(|()| f(k.as_bytes(), &buf))
            {
                Ok(()) => {}
                Err(e) => err = Some(e),
            }
        });
        match err {
            Some(e) => Err(e),
            None => Ok(()),
        }
    }

    fn durable_flush(&self) -> DbResult<()> {
        TypedMap::durable_flush(self)
    }
}

#[cfg(feature = "typed-tree")]
impl<
    K: Key + Send + Sync + std::hash::Hash + Eq + 'static,
    T: armour_core::GetType
        + crate::tree_meta::CollectionMeta
        + Clone
        + PartialEq
        + Send
        + Sync
        + 'static,
    C: Codec<T> + Sync + 'static,
    H: TypedWriteHook<K, T> + 'static,
> crate::projection::ProjectableIndexTx for TypedMap<K, T, C, H>
{
    fn as_tx_participant(self: Arc<Self>) -> Arc<dyn crate::txn::participant::TxParticipant> {
        self
    }
}

#[cfg(feature = "typed-tree")]
impl<
    K: Key + Send + Sync + std::hash::Hash + Eq + 'static,
    T: armour_core::GetType
        + crate::tree_meta::CollectionMeta
        + Clone
        + PartialEq
        + Send
        + Sync
        + 'static,
    C: Codec<T> + Sync + 'static,
    H: TypedWriteHook<K, T> + 'static,
> crate::projection::IndexKeyed for TypedMap<K, T, C, H>
{
    type K = K;
    type V = T;

    fn encode_value(&self, v: &T) -> DbResult<Vec<u8>> {
        let mut buf = Vec::new();
        self.codec().encode_to(v, &mut buf)?;
        Ok(buf)
    }
}

#[cfg(feature = "typed-tree")]
impl<
    K: Key + Send + Sync + std::hash::Hash + Eq + 'static,
    T: armour_core::GetType
        + crate::tree_meta::CollectionMeta
        + Clone
        + PartialEq
        + Send
        + Sync
        + 'static,
    C: Codec<T> + Sync + 'static,
    H: TypedWriteHook<K, T> + 'static,
> crate::counter::CounterCollection for TypedMap<K, T, C, H>
{
    fn decode_value(&self, bytes: &[u8]) -> DbResult<T> {
        self.codec().decode_from(bytes)
    }

    /// Read path — no shard write lock: `get` is an O(1) index lookup.
    fn read_bytes(&self, key: &[u8]) -> DbResult<Option<Vec<u8>>> {
        if key.len() != size_of::<K>() {
            return Err(DbError::CorruptedEntry { offset: 0 });
        }
        let k = K::from_bytes(key);
        self.get(&k).map(|v| self.encode_value(&v)).transpose()
    }

    fn contains_bytes(&self, key: &[u8]) -> DbResult<bool> {
        if key.len() != size_of::<K>() {
            return Err(DbError::CorruptedEntry { offset: 0 });
        }
        let k = K::from_bytes(key);
        Ok(self.contains(&k))
    }

    fn upsert_counter(
        &self,
        key: &[u8],
        f: &mut dyn FnMut(Option<&T>) -> Upsert<T>,
    ) -> DbResult<()> {
        if key.len() != size_of::<K>() {
            return Err(DbError::CorruptedEntry { offset: 0 });
        }
        let k = K::from_bytes(key);
        self.upsert(&k, f)?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::codec::Codec;
    use crate::config::Config;
    use crate::hook::TypedWriteHook;
    use std::collections::HashMap as StdHashMap;
    use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrdering};
    use tempfile::tempdir;

    struct U64Codec;

    impl Codec<u64> for U64Codec {
        fn encode_to(&self, value: &u64, buf: &mut Vec<u8>) -> DbResult<()> {
            buf.clear();
            buf.extend_from_slice(&value.to_be_bytes());
            Ok(())
        }

        fn decode_from(&self, bytes: &[u8]) -> DbResult<u64> {
            let arr: [u8; 8] = bytes
                .try_into()
                .map_err(|_| DbError::CorruptedEntry { offset: 0 })?;
            Ok(u64::from_be_bytes(arr))
        }
    }

    type Map = TypedMap<[u8; 8], u64, U64Codec>;

    fn open(dir: &std::path::Path) -> Map {
        TypedMap::<[u8; 8], u64, U64Codec>::open(dir, Config::test(), U64Codec).unwrap()
    }

    fn key_on_other_shard(map: &Map, anchor: &[u8; 8]) -> [u8; 8] {
        let anchor_shard = map.shard_for(anchor);
        (2u64..1000)
            .map(|i| i.to_be_bytes())
            .find(|k| map.shard_for(k) != anchor_shard)
            .expect("key on different shard")
    }

    #[derive(Default)]
    struct TRecHook {
        writes: AtomicUsize,
        #[allow(clippy::type_complexity)]
        seq: crate::sync::Mutex<Vec<(u64, Option<u64>, Option<u64>)>>,
    }

    impl TypedWriteHook<[u8; 8], u64> for TRecHook {
        fn on_write(&self, key: &[u8; 8], old: Option<&u64>, new: Option<&u64>) {
            self.writes.fetch_add(1, AtomicOrdering::Relaxed);
            crate::sync::lock(&self.seq).push((
                u64::from_be_bytes(*key),
                old.copied(),
                new.copied(),
            ));
        }
    }

    fn open_typed_map_hooked<H: TypedWriteHook<[u8; 8], u64>>(
        dir: &std::path::Path,
        cfg: Config,
        hook: H,
    ) -> TypedMap<[u8; 8], u64, U64Codec, H> {
        TypedMap::open_hooked(dir, cfg, U64Codec, hook).expect("open hooked")
    }

    #[test]
    fn typed_map_shard_update_and_fetch_update() {
        let dir = tempdir().unwrap();
        let mut cfg = Config::test();
        cfg.shard_count = 1;
        let map: TypedMap<[u8; 8], u64, U64Codec> =
            TypedMap::open(dir.path(), cfg, U64Codec).unwrap();
        let key = 1u64.to_be_bytes();
        map.put(&key, 10).unwrap();
        let (new_v, old_v, missing) = map
            .atomic(&key, |s| {
                // update returns new value (11)
                let new_v = s.update(&key, |old| old + 1)?.as_deref().copied();
                // fetch_update returns old value (11), writes 12
                let old_v = s.fetch_update(&key, |old| old + 1)?.as_deref().copied();
                // missing key returns None
                let missing = s.update(&2u64.to_be_bytes(), |old| *old)?.is_none();
                Ok((new_v, old_v, missing))
            })
            .expect("atomic");
        assert_eq!(new_v, Some(11));
        assert_eq!(old_v, Some(11));
        assert!(missing);
        assert_eq!(map.get(&key).as_deref().copied(), Some(12));
    }

    #[test]
    fn typed_map_atomic_update_fires_hook() {
        let dir = tempdir().unwrap();
        let mut cfg = Config::test();
        cfg.shard_count = 1;
        let map = open_typed_map_hooked(dir.path(), cfg, TRecHook::default());
        let k = 7u64.to_be_bytes();
        map.put(&k, 100).unwrap();
        // reset hook counter after the put
        map.hook.writes.store(0, AtomicOrdering::Relaxed);
        crate::sync::lock(&map.hook.seq).clear();

        map.atomic(&k, |s| {
            s.update(&k, |old| old + 1)?;
            Ok(())
        })
        .expect("atomic");

        assert_eq!(map.hook.writes.load(AtomicOrdering::Relaxed), 1);
        let seq = crate::sync::lock(&map.hook.seq).clone();
        assert_eq!(seq[0], (7, Some(100), Some(101)));
        assert_eq!(map.get(&k).as_deref().copied(), Some(101));
    }

    #[test]
    fn typed_map_atomic_fires_hooks_in_order() {
        let dir = tempdir().unwrap();
        let mut cfg = Config::test();
        cfg.shard_count = 1;
        let map = open_typed_map_hooked(dir.path(), cfg, TRecHook::default());
        let k = 5u64.to_be_bytes();
        map.atomic(&k, |s| {
            s.put(&k, 10)?; // new key
            s.put(&k, 20)?; // update, old=10
            s.delete(&k)?; // delete, old=20
            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], (5, None, Some(10)));
        assert_eq!(seq[1], (5, Some(10), Some(20)));
        assert_eq!(seq[2], (5, Some(20), None));
    }

    #[test]
    fn typed_map_atomic_fires_for_applied_on_err() {
        let dir = tempdir().unwrap();
        let mut cfg = Config::test();
        cfg.shard_count = 1;
        let map = open_typed_map_hooked(dir.path(), cfg, TRecHook::default());
        let k = 1u64.to_be_bytes();
        let r: DbResult<()> = map.atomic(&k, |s| {
            s.put(&k, 99)?;
            Err(DbError::KeyNotFound)
        });
        assert!(r.is_err());
        assert_eq!(map.hook.writes.load(AtomicOrdering::Relaxed), 1);
    }

    #[test]
    fn typed_map_first_last_contract() {
        let dir = tempdir().unwrap();
        let cfg = Config::balanced()
            .shard_count(2)
            .hints(true)
            .iterable(true)
            .reversed(false)
            .build();
        let map = TypedMap::<[u8; 8], u64, U64Codec>::open(dir.path(), cfg, U64Codec).unwrap();
        assert!(map.first().unwrap().is_none());
        assert!(map.last().unwrap().is_none());
        for id in [3_u64, 1, 2] {
            map.put(&id.to_be_bytes(), id * 10).unwrap();
        }
        assert_eq!(
            map.first()
                .unwrap()
                .map(|(k, v)| (u64::from_be_bytes(k), *v)),
            Some((1, 10))
        );
        assert_eq!(
            map.last()
                .unwrap()
                .map(|(k, v)| (u64::from_be_bytes(k), *v)),
            Some((3, 30))
        );

        let no_index_dir = tempdir().unwrap();
        let no_index =
            TypedMap::<[u8; 8], u64, U64Codec>::open(no_index_dir.path(), Config::test(), U64Codec)
                .unwrap();
        assert!(matches!(no_index.first(), Err(DbError::NotImplemented)));
        assert!(matches!(no_index.last(), Err(DbError::NotImplemented)));
    }

    #[cfg(feature = "rapira-codec")]
    #[test]
    fn typed_map_iter_no_clone_bound() {
        #[derive(rapira::Rapira, PartialEq, Debug)]
        struct NoClone {
            n: u32,
            s: String,
        }

        let dir = tempdir().unwrap();
        let cfg = Config::balanced()
            .shard_count(2)
            .hints(true)
            .iterable(true)
            .build();
        let m = TypedMap::<[u8; 2], NoClone, crate::RapiraCodec>::open(
            dir.path(),
            cfg,
            crate::RapiraCodec,
        )
        .unwrap();
        m.put(
            &[1, 0],
            NoClone {
                n: 1,
                s: "a".into(),
            },
        )
        .unwrap();
        m.put(
            &[2, 0],
            NoClone {
                n: 2,
                s: "b".into(),
            },
        )
        .unwrap();
        let v = m.iter_view().unwrap();
        let ns: Vec<u32> = v.iter().map(|(_, r)| r.n).collect();
        assert_eq!(ns, [2, 1]);
        // keys-only iteration is clone-free and guard-free (never derefs `T`).
        let keys: Vec<[u8; 2]> = v.keys().collect();
        assert_eq!(keys, [[2, 0], [1, 0]]);
        v.retain(|_k, val| val.n % 2 == 0).unwrap();
        assert_eq!(m.iter_view().unwrap().iter().count(), 1);
    }

    #[test]
    fn for_each_visits_all_entries() {
        let dir = tempdir().unwrap();
        let map = open(dir.path());

        let key_a = 1u64.to_be_bytes();
        let key_b = key_on_other_shard(&map, &key_a);
        let key_c = key_on_other_shard(&map, &key_b);

        let expected = StdHashMap::from([(key_a, 10u64), (key_b, 20u64), (key_c, 30u64)]);
        for (key, value) in &expected {
            map.put(key, *value).unwrap();
        }

        let mut seen = StdHashMap::new();
        map.for_each(|key, value| {
            seen.insert(*key, *value);
        });

        assert_eq!(seen, expected);
        assert_ne!(map.shard_for(&key_a), map.shard_for(&key_b));
    }

    #[test]
    fn for_each_while_stops_after_break_without_visiting_later_shards() {
        use std::ops::ControlFlow;
        use std::sync::atomic::{AtomicUsize, Ordering};

        let dir = tempdir().unwrap();
        let map = open(dir.path());

        let key_a = 1u64.to_be_bytes();
        let key_b = key_on_other_shard(&map, &key_a);
        assert_ne!(map.shard_for(&key_a), map.shard_for(&key_b));

        map.put(&key_a, 10).unwrap();
        map.put(&key_b, 20).unwrap();

        let seen = AtomicUsize::new(0);
        let flow = map.for_each_while(|_k, _v| {
            seen.fetch_add(1, Ordering::Relaxed);
            ControlFlow::Break(())
        });
        assert_eq!(seen.load(Ordering::Relaxed), 1);
        assert!(matches!(flow, ControlFlow::Break(())));
    }
}