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
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2025-present, fjall-rs
// Copyright (c) 2026-present, Dmitry Prudnikov
pub mod block;
pub(crate) mod block_index;
pub(crate) mod block_layout;
#[cfg(feature = "columnar")]
pub(crate) mod column_page;
pub mod column_type;
#[cfg(feature = "columnar")]
pub mod columnar;
#[cfg(feature = "columnar")]
pub mod columnar_cursor;
#[cfg(feature = "columnar")]
pub mod columnar_predicate;
pub mod data_block;
pub mod delete_bitmap;
pub(crate) mod file_pool;
pub mod filter;
#[cfg(feature = "columnar")]
pub(crate) mod group_carry;
mod id;
mod index_block;
mod inner;
#[cfg(feature = "std")]
pub(crate) use inner::PUNCH_ALIGN;
#[cfg(test)]
pub(crate) use inner::PUNCH_CHUNK;
pub(crate) mod iter;
#[cfg(feature = "zstd")]
pub(crate) mod lazy_block;
pub(crate) mod locator;
pub(crate) mod meta;
pub(crate) mod multi_writer;
pub(crate) mod probe_stats;
pub(crate) mod regions;
#[cfg(feature = "std")]
pub(crate) mod relocate;
#[cfg(feature = "columnar")]
pub(crate) mod row_group;
mod scanner;
pub(crate) mod seqno_bounds;
pub(crate) mod staged;
// Reached through the doc-hidden `table` module by the crate's own tools; it
// carries no stability promise, and its items change with the read path.
pub mod util;
pub mod writer;
pub(crate) mod zone_map;
// std-gated like the sibling test modules: the tests drive real files through
// `StdFs` and the std-only `restrict_bound` sidecar API, so a no-std test
// build must skip the whole module.
#[cfg(all(test, feature = "std"))]
#[allow(
clippy::unwrap_used,
clippy::indexing_slicing,
clippy::useless_vec,
clippy::needless_borrows_for_generic_args,
reason = "test code"
)]
mod tests;
#[cfg(all(test, feature = "std", feature = "metrics"))]
#[expect(clippy::expect_used, reason = "test code")]
mod lookup_fd_tests;
pub use block::{Block, BlockOffset};
pub use data_block::DataBlock;
pub use id::{GlobalTableId, TableId};
pub use index_block::{BlockHandle, IndexBlock, KeyedBlockHandle};
pub use scanner::Scanner;
pub use writer::Writer;
use crate::{
Checksum, CompressionType, InternalValue, SeqNo, TreeId, UserKey,
cache::Cache,
comparator::SharedComparator,
descriptor_table::DescriptorTable,
file_accessor::FileAccessor,
fs::{Fs, FsFile, FsOpenOptions},
range_tombstone::RangeTombstone,
table::{
block::{BlockType, ParsedItem},
block_index::{BlockIndex, FullBlockIndex, TwoLevelBlockIndex, VolatileBlockIndex},
filter::block::FilterBlock,
regions::ParsedRegions,
writer::LinkedFile,
},
};
use alloc::sync::Arc;
#[cfg(not(feature = "std"))]
use alloc::{boxed::Box, vec::Vec};
use block_index::BlockIndexImpl;
use core::ops::{Bound, RangeBounds};
use inner::Inner;
use iter::Iter;
use crate::path::PathBuf;
use portable_atomic::AtomicU64;
use util::{LookupFd, ReadCharge};
#[cfg(feature = "metrics")]
use crate::metrics::Metrics;
pub type TableInner = Inner;
/// Plan produced by [`Table::plan_block_tasks`]: the SST's file handle, the
/// table-local read seqno, whether the blocks need the special load path
/// (Page-ECC / columnar), and per data block its handle plus the positions
/// (into the input key batch) of the keys that fall in it.
pub(crate) type BlockTaskPlan = (
Arc<dyn crate::fs::FsFile>,
SeqNo,
bool,
Vec<(BlockHandle, Vec<usize>)>,
);
/// How [`Table::recover_inner`] treats degraded sidecars and the metadata id
/// cross-check.
#[derive(Clone, Copy)]
pub(crate) enum RecoveryMode {
/// Live-tree open: a corrupt delete-bitmap / unreadable zone map fails
/// closed, and the caller's durable id (manifest entry / file name) is
/// cross-checked against the meta payload (with MID-mirror fallback on a
/// bad TAIL copy).
Live,
/// Salvage open: a corrupt delete-bitmap / missing zone map degrades
/// instead of failing, so a damaged sidecar still opens. For an
/// UNENCRYPTED source the id cross-check uses `expected_id`: `Some` when
/// the caller knows the durable id out-of-band (repair — from the SST
/// file name), so a forged TAIL id falls back to the intact MID mirror
/// instead of poisoning the recovered copy's identity; `None` for a
/// standalone recovery reader, where the source's own stored id IS the
/// identity to recover under. An ENCRYPTED open always cross-checks the
/// caller's id — the meta block's AAD binds it regardless.
Salvage {
/// The durable table id known out-of-band, or `None` when the
/// source's stored id is authoritative.
expected_id: Option<TableId>,
/// Load the MID meta mirror FIRST (tail as fallback), inverting the
/// default tail-first order. Set by the salvage arbitration when the
/// two mirrors decode to DIVERGENT contents: neither copy can be
/// proven genuine, so salvage attempts both orders and keeps the
/// attempt that recovers more.
prefer_mid_meta: bool,
},
}
/// Everything [`Table::recover`] needs, with the context a caller does not care
/// about defaulted by [`RecoverParams::new`].
///
/// A params struct rather than positional arguments on purpose: the old
/// signature carried three integers and two booleans in a row, so a transposed
/// `tree_id`/`table_id` or `pin_filter`/`pin_index` compiled cleanly and
/// surfaced only as misbehavior at runtime. Named fields make every call state
/// what it passes.
pub struct RecoverParams {
/// Where the SST lives.
pub file_path: PathBuf,
/// The table's whole-file checksum, from the manifest (or freshly computed
/// by a repair scan).
pub checksum: Checksum,
/// Bulk-ingest sequence offset from the manifest; `0` for a table whose
/// intrinsic seqnos are authoritative.
pub global_seqno: SeqNo,
/// L0 recency key from the manifest; `None` takes the one the table's
/// file was written with. An ingested table's comes from its install,
/// which happens after its file is written.
pub recency: Option<TableId>,
/// Owning tree, keying shared caches; `0` for a transient open that must
/// not pollute them.
pub tree_id: TreeId,
/// The table's durable id (its file name / manifest entry).
pub table_id: TableId,
/// Block cache.
pub cache: Arc<Cache>,
/// Descriptor table for pooled file handles; `None` opens per read.
pub descriptor_table: Option<Arc<DescriptorTable>>,
/// Filesystem the file is reachable through.
pub fs: Arc<dyn Fs>,
/// Pin the filter blocks in memory on open.
pub pin_filter: bool,
/// Pin the index blocks in memory on open.
pub pin_index: bool,
/// At-rest encryption provider, matching how the table was written.
pub encryption: Option<Arc<dyn crate::encryption::EncryptionProvider>>,
/// Every dictionary the tree can decompress against. The table resolves
/// the id it recorded against this set; supplying a set that does not hold
/// that id fails the recovery, naming the id.
#[cfg(zstd_any)]
pub zstd_dictionaries: crate::compression::ZstdDictionaries,
/// The tree's key ordering.
pub comparator: SharedComparator,
/// The tree's prefix extractor. A prefix scan consults the table's filter
/// only when the table's filter indexes this extractor's prefixes; `None`
/// never consults it.
pub prefix_extractor: Option<Arc<dyn crate::prefix::PrefixExtractor>>,
/// Metrics sink.
#[cfg(feature = "metrics")]
pub metrics: Arc<Metrics>,
}
impl RecoverParams {
/// Params for recovering the table at `file_path`, with every contextual
/// field at its neutral default: no ingest offset, tree id `0` (a transient
/// open that must not pollute shared caches), no descriptor table, nothing
/// pinned, no encryption, no dictionary. Callers set what differs.
#[must_use]
pub fn new(
file_path: PathBuf,
checksum: Checksum,
table_id: TableId,
fs: Arc<dyn Fs>,
comparator: SharedComparator,
cache: Arc<Cache>,
) -> Self {
Self {
file_path,
checksum,
global_seqno: 0,
recency: None,
tree_id: 0,
table_id,
cache,
descriptor_table: None,
fs,
pin_filter: false,
pin_index: false,
encryption: None,
#[cfg(zstd_any)]
zstd_dictionaries: crate::compression::ZstdDictionaries::new(),
comparator,
prefix_extractor: None,
#[cfg(feature = "metrics")]
metrics: Arc::new(Metrics::default()),
}
}
}
/// Cached outcome of the heal's lazy hard-link probe/detach (see
/// [`Table::ensure_unshared_for_write`]): the probe and copy run at most
/// once per scan, and only when a write-back is actually needed.
#[cfg(feature = "page_ecc")]
enum UnshareState {
/// No write attempted yet: the link count has not been probed.
Unprobed,
/// The handle is safe to write through (exclusive, or already detached).
Ready,
/// The unshare failed; every write is refused with this reason.
Failed(alloc::string::String),
}
/// A disk segment (a.k.a. `Table`, `SSTable`, `SST`, `sorted string table`) that is located on disk
///
/// A table is an immutable list of key-value pairs, split into compressed blocks.
/// A reference to the block (`block handle`) is saved in the "block index".
///
/// Deleted entries are represented by tombstones.
///
/// The tree-wide machinery a freshly created table must be bound to before it
/// becomes reachable, passed to [`Table::bind_to_tree`].
///
/// Collected in one type on purpose: publication happens from several places
/// (flush, recovery, ingest, compaction, the tight-space slice loop), and
/// having each install sinks by hand is what let two of them silently drift
/// into publishing tables that could never schedule their own healing.
pub(crate) struct TableSinks<'a> {
/// Defers cleanup while a checkpoint is capturing, so an in-place heal
/// cannot race a hard-link.
pub deletion_pause: &'a Arc<crate::deletion_pause::DeletionPause>,
/// Where a confirmed-persistent ECC correction queues the table for a
/// healing rewrite.
pub heal_hints: &'a Arc<crate::heal_hints::HealHints>,
/// How the tree's columnar reads fetch their pages.
pub read_budget: crate::config::ReadBudget,
/// Moves an obsolete table's `unlink` off the foreground path.
#[cfg(feature = "std")]
pub background_deleter: &'a Arc<crate::BackgroundDeleter>,
/// Whether the tree counts the table's filter probes: it does when it
/// allocates filter memory by probe load.
pub track_filter_probes: bool,
}
/// Tables can be merged together to improve read performance and free unneeded disk space by removing outdated item versions.
#[doc(alias("sstable", "sst", "sorted string table"))]
#[derive(Clone)]
pub struct Table(
Arc<Inner>,
/// Tight-space restriction: when `Some(bound)`, this version's view of the
/// table is clamped to keys `>= bound`. The on-disk data blocks below
/// `bound` have been punched out ([`crate::fs::Fs::punch_hole`]) and their
/// content lives in a freshly merged output table that supersedes them, so
/// reads must not touch the punched prefix. Carried on the `Table` wrapper
/// (not the shared `Arc<Inner>`) so an older snapshot keeps its own
/// unrestricted view of the same physical SST. `None` on the common path.
Option<UserKey>,
/// Refreshed full-file checksum: when `Some`, an in-place heal changed the
/// file's bytes AFTER it was recovered, and this digest supersedes the one
/// captured at recovery. Carried on the wrapper (like the restriction) so
/// the version diff sees the change and persists it to the manifest, while
/// older snapshots keep the digest they were recovered under. `None` on
/// the common path.
Option<Checksum>,
);
impl core::ops::Deref for Table {
type Target = Inner;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[cfg_attr(coverage_nightly, coverage(off))]
impl core::fmt::Debug for Table {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "Table:{}({:?})", self.id(), self.metadata.key_range)
}
}
/// How an SST's rows are visible at a query snapshot, returned by
/// [`Table::seqno_visibility`]. Drives whether the tree-level columnar scan can
/// stream a segment verbatim ([`All`](Self::All)) or must apply a per-row seqno
/// mask ([`Partial`](Self::Partial)); [`None`](Self::None) segments are dropped.
/// The range estimators use the same classification to skip a table a read at
/// that snapshot would return nothing from.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) enum SeqnoVisibility {
/// No row is visible at the snapshot (the SST postdates it).
None,
/// Every row is visible (the SST entirely predates the snapshot).
All,
/// The snapshot straddles the SST's seqno range; visibility is per-row.
Partial,
}
/// How a batched read gets a Page-ECC or columnar table's data block, as
/// [`Table::batched_special_read`] decides.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum SpecialRead {
/// `len` bytes of the table's file from `offset`, read with the batch's
/// other blocks and handed to [`Table::decode_special_block`].
Bytes { offset: u64, len: usize },
/// Through the table's own load path, in a job.
Load,
}
/// Which data blocks a key range boundary takes in [`Table::data_span`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum SpanEdge {
/// Every block holding a key of the range, boundary blocks whole: a range
/// inside one block covers that block rather than nothing.
Whole,
/// The blocks whose last key lies in the range. Ranges that do not
/// overlap take disjoint blocks, so splitting a table's blocks between
/// several ranges hands each block out once.
ByLastKey,
}
/// The part of a table's data section a key range covers, in byte offsets
/// (see [`Table::data_span`]).
#[derive(Clone, Copy, Debug)]
pub(crate) struct DataSpan {
/// Bytes of data blocks the range covers, within the live part.
pub covered: u64,
/// Where the live part starts: past the prefix a restricted view punched
/// out, 0 otherwise.
pub live_start: u64,
/// The end of the data section.
pub data_end: u64,
}
/// Result of a bloom filter check.
enum BloomResult {
/// Bloom says key is definitely absent — skip point read.
Skip,
/// The key sorts past the last filter partition: absent by the partition
/// index alone, with no filter answering, so no probe of one.
PastPartitions,
/// Point read should proceed.
Proceed {
/// Whether a filter was present, for metrics and probe accounting.
has_filter: bool,
},
}
/// Where the filter a key is checked against lives (see
/// [`Table::filter_source`]).
pub(crate) enum FilterSource<'a> {
/// The table has no filter, or none it can consult.
None,
/// The table's whole filter, pinned in memory.
Pinned(&'a FilterBlock),
/// The filter block, or the filter partition holding the key, to read.
Block(BlockHandle),
/// The key sorts past the last filter partition.
PastPartitions,
}
/// What a key check of a table's filters found (see
/// [`Table::key_filter_answer`]).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum KeyFilterAnswer {
/// A filter answered absent.
Absent,
/// The key sorts past the last filter partition: absent by the partition
/// index alone, with no filter answering.
PastPartitions,
/// The key may be present, or nothing could rule it out.
MayContain,
}
impl BloomResult {
fn should_skip(&self) -> bool {
matches!(self, Self::Skip | Self::PastPartitions)
}
fn has_filter(&self) -> bool {
matches!(self, Self::Proceed { has_filter: true })
}
/// What this answer adds to its table's probe counts: a probe when a
/// filter answered, and a negative one when it answered absent.
const fn probe_counts(&self) -> crate::table::probe_stats::ProbeCounts {
let (probes, negatives) = match self {
Self::Skip => (1, 1),
Self::Proceed { has_filter: true } => (1, 0),
Self::PastPartitions | Self::Proceed { has_filter: false } => (0, 0),
};
crate::table::probe_stats::ProbeCounts { probes, negatives }
}
}
/// Re-applies a bulk-ingested table's base sequence number to a table-local
/// seqno, translating it back to the global coordinate that callers compare
/// across tables.
///
/// The sum never overflows on any reachable input: a row reaches this
/// translation only when it is visible at the query snapshot `Q`, which (by the
/// exclusive MVCC check `local < Q - global`) requires `local + global < Q`, and
/// `Q <= SeqNo::MAX`. The `checked_add` therefore always succeeds; an overflow
/// would mean the invariant was violated, so it aborts loudly in both debug and
/// release builds rather than wrapping to a subtly wrong seqno (the silent-
/// corruption class `saturating_add` shares). For a non-ingested table `global`
/// is `0` and this is the identity.
#[inline]
fn apply_global_seqno(local: SeqNo, global: SeqNo) -> SeqNo {
local.checked_add(global).unwrap_or_else(|| {
unreachable!(
"apply_global_seqno: table-local seqno + global base overflowed SeqNo::MAX, \
but a row is only translated here when visible (local + global < query snapshot)"
)
})
}
/// Result of [`Table::salvage_load_block`]: the decoded block plus, when it read
/// back cleanly, its raw on-disk bytes for a verbatim copy.
pub(crate) struct SalvageBlock {
/// The decoded (decompressed / decrypted / ECC-healed) block: the source of
/// the per-row entries the salvage walk accounts and, on the re-encode path,
/// re-serializes.
pub block: Block,
/// `Some((raw_on_disk_bytes, header, inner_layout))` when the block read back
/// cleanly (no ECC recovery): the walk byte-copies these verbatim. `None` when
/// ECC recovery healed the block, so the faulty on-disk bytes must not be
/// propagated and the caller re-encodes the healed payload instead.
pub verbatim: Option<VerbatimCopy>,
/// Whether ECC recovery had to heal the block to read it. Kept separate
/// from `verbatim` (which is also `None` for verbatim-ineligible clean
/// reads) so the walk's live progress counts genuine heals only.
pub ecc_recovered: bool,
}
/// What one handle in a section names, for the walks that frame handles
/// against the bytes they point at.
#[cfg(feature = "std")]
#[derive(Clone, Copy)]
enum FrameUnit {
/// One block: every handle in the index section.
Block,
/// One data unit: a block for a row-major table, a row group for a
/// columnar one.
DataUnit,
}
/// Result of [`Table::salvage_load_row_group`]: the decoded group plus, when
/// every block in it read back cleanly, the group's raw bytes.
#[cfg(all(feature = "columnar", feature = "std"))]
pub(crate) struct SalvageRowGroup {
/// The group's verified blocks. Left undecoded so the walk can tell a
/// group whose bytes did not read from one whose content does not decode:
/// the two are dropped for different reasons and reported differently.
pub group: crate::table::row_group::RowGroupBlocks,
/// `Some((raw_group_bytes, uncompressed_length))` when every block of the
/// group qualified for a verbatim copy; `None` otherwise, and the caller
/// re-encodes the decoded group.
pub verbatim: Option<(alloc::vec::Vec<u8>, u64)>,
/// Whether ECC recovery healed any block of the group.
pub ecc_recovered: bool,
}
/// Raw on-disk frame captured for a verbatim block copy:
/// `(raw_on_disk_bytes, header, inner_layout)`. See [`SalvageBlock::verbatim`].
pub(crate) type VerbatimCopy = (
alloc::vec::Vec<u8>,
crate::table::block::Header,
alloc::vec::Vec<u32>,
);
/// Outcome of [`Table::conservative_restriction`] — the CONSERVATIVE
/// punch-geometry classification for a punched SST whose exact
/// `.restrict-bound` sidecar is not trustworthy.
#[cfg(feature = "std")]
pub(crate) enum DerivedRestriction {
/// The zeroed blocks form a CLEAN prefix (a fully successful punch):
/// restrict to this bound — the first readable block's end key — and no
/// superseded key resurrects, at the cost of at most that one straddling
/// block's live suffix.
Bound(UserKey),
/// Every data block reads as zeros: no live data survives the punch; the
/// caller excludes the table.
NoLiveData,
/// A readable block sits BELOW a zeroed one — positive evidence of failed
/// `punch_hole` calls, after which no geometry bound can separate
/// intact-but-consumed blocks from live ones. The caller sets the table
/// aside (resurrection mode derives greedily instead, accepting the
/// re-exposure by contract).
IrregularPunch,
}
/// Verdict of [`Table::punch_geometry`] — whether any zeroed
/// data-block run carries the physical hole that proves a tight-space
/// reclaim.
#[cfg(feature = "std")]
pub(crate) enum PunchProbe {
/// A zeroed run carries a proven hole: the SST was punched.
Punched,
/// No zeroed data blocks, or every zeroed run is proven ALLOCATED —
/// zeros are damage, not a reclaim. Also the verdict on a mount that
/// cannot punch at all: a reclaim never ran there.
Unpunched,
/// Zeroed runs exist on a PUNCH-CAPABLE mount whose hole probe cannot
/// answer (`None`): a lost-sidecar reclaim and damage are
/// indistinguishable, so the caller must fail closed unless resurrection
/// explicitly accepts the ambiguity.
Unproven,
}
/// Which semantic cross-check a combined reconcile pass tripped on, so the
/// caller keeps its own per-gate wording instead of one generic message.
#[cfg(feature = "std")]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ReconcileGate {
/// Index separators against the addressed blocks' decoded last keys.
Separators,
/// Per-KV footer digests.
KvChecksums,
/// Recorded seqno bounds against the decoded entries.
SeqnoBounds,
/// Declared entry count against the decoded one.
BlockEntryCounts,
/// Recorded zone map against the decoded statistics.
ZoneMap,
/// Recorded locator against the decoded newest-version blocks.
Locator,
/// Filter against the decoded keys (and their prefixes).
Filter,
/// In-block indexes and the global sort order.
PointReadReachability,
/// Recorded metadata bounds against the decoded contents.
MetadataBounds,
/// Recorded inner-block boundaries against the frame's actual zstd blocks.
///
/// Only a zstd build can raise it: the writer emits the `block_layout`
/// section exclusively for data blocks that split into two or more inner
/// zstd blocks, so without zstd there are no such frames and no boundaries
/// to disagree about. The section-level forgery (a present-but-EMPTY map,
/// i.e. a relabelled `delete_bitmap`) is caught on every build, but by
/// `verify_block_layout` and outside this enum.
#[cfg_attr(
not(feature = "zstd"),
expect(
dead_code,
reason = "the per-frame gate that raises it needs a zstd decoder"
)
)]
BlockLayout,
}
/// The recorded metadata a reconcile judges, plus the accumulators the block
/// walk fills for it: read and seeded once, then compared in `finish`.
#[cfg(feature = "std")]
pub(crate) struct MetaBoundsProbe {
/// The disk-fresh meta whose bounds are on trial.
meta: crate::table::meta::ParsedMeta,
/// The recorded range tombstones, whose spans the key range must cover.
tombstones: Option<Vec<crate::range_tombstone::RangeTombstone>>,
/// `(start, seqno)` of the synthetic RT-only sentinel entry, excluded from
/// the KV seqno maximum exactly once.
sentinel: Option<(UserKey, SeqNo)>,
/// Whether that exclusion has already happened.
sentinel_excluded: bool,
/// Whether this is a tight-space restricted view, which relaxes the
/// item-count equality to a subset check.
restricted: bool,
/// Blocks the index lists, punched prefix included.
block_count: u64,
/// Whether the caller already authenticated this file against a matching
/// manifest digest.
bitmap_digest_authenticated: bool,
/// Entries the live blocks decoded to.
count: u64,
/// First and last decoded user key over the live suffix.
first_key: Option<UserKey>,
last_key: Option<UserKey>,
/// Lowest and highest decoded seqno over the live suffix.
seqno_lo: Option<SeqNo>,
seqno_hi: Option<SeqNo>,
}
/// The on-disk filter state one reconcile needs, so the probe reads the
/// section (and each partition) once no matter how many blocks it checks.
#[cfg(feature = "std")]
pub(crate) struct FilterProbe<'a> {
/// The table being judged, for its comparator and id.
table: &'a Table,
/// The file the sections are read from, lent by the pass that opened it
/// and borrowed across the walk for the partition reads.
file: &'a dyn crate::fs::FsFile,
/// Codec / encryption / ECC context every filter block decodes under.
transform: crate::table::block::BlockTransform<'a>,
/// The partition index, when the filter is partitioned.
index: Option<IndexBlock>,
/// The single filter block, when it is not.
full: Option<crate::table::filter::block::FilterBlock>,
/// Partitions already read, keyed by file offset: one read each.
partitions: alloc::collections::BTreeMap<u64, crate::table::filter::block::FilterBlock>,
/// The last key probed, so a key's older versions are not re-probed
/// across a block boundary.
prev_key: Option<UserKey>,
/// The extractor whose prefixes a full filter must also report present.
prefix_extractor: Option<alloc::sync::Arc<dyn crate::prefix::PrefixExtractor>>,
}
#[cfg(feature = "std")]
impl FilterProbe<'_> {
/// Probes one decoded block's keys, whose hashes `key_hashes` holds in
/// entry order, against the filter.
///
/// # Errors
///
/// [`crate::Error::InvalidHeader`] when the filter reports an existing key
/// (or its prefix) as definitely absent, or a partition is missing / empty
/// for a key that exists.
fn check_block(&mut self, block: &DecodedBlock, key_hashes: &[u64]) -> crate::Result<()> {
Table::check_filter_block(self, block, key_hashes)
}
}
/// One live data block, decoded ONCE for every cross-check gate that needs its
/// contents, from [`Table::for_each_live_block`].
///
/// Each gate used to walk the whole table itself, so a single reconcile decoded
/// every block seven to nine times and the decode dominates a repair's cost.
/// Materializing the block's row view, its columnar batch and its entries here
/// hands the SAME decode to all of them.
// Compiled under no_std alongside `verify_kv_checksums`, which walks with it
// and is itself dead there (the verify / scrub callers are std-gated).
#[cfg_attr(
not(feature = "std"),
expect(
dead_code,
reason = "gate-only decode; the verify/scrub consumers are std-gated"
)
)]
pub(crate) struct DecodedBlock {
/// Where the block lives, as the block index records it.
pub(crate) handle: BlockHandle,
/// The block exactly as it came off disk, per-KV footer INTACT. The row
/// view below strips the footer, so the per-KV digest gate needs these
/// bytes rather than the stripped ones.
pub(crate) raw: Block,
/// The same block one layer earlier: the payload as the writer compressed
/// it, post-checksum and post-decrypt but pre-decompression. Carried from
/// the SAME read that produced `raw`, so the gate that cross-checks the
/// frame's inner-block boundaries costs no extra I/O. Equal to `raw.data`
/// on an uncompressed table, where the two layers coincide.
#[cfg_attr(
not(feature = "zstd"),
expect(
dead_code,
reason = "the only consumer is the block-layout boundary gate, which needs a zstd decoder"
)
)]
pub(crate) frame: crate::Slice,
/// The row-major view. `None` on a columnar table, whose rows are
/// reconstructed from `batch` instead.
pub(crate) row: Option<DataBlock>,
/// The columnar batch this block decoded to, for the per-column statistics
/// only a columnar table records. `None` on a row table.
#[cfg(feature = "columnar")]
pub(crate) batch: Option<crate::table::columnar::ColumnBatch>,
/// The block's entries in block order, materialized once.
pub(crate) entries: Vec<InternalValue>,
}
/// Everything one pass over the data blocks can say about a tight-space
/// reclaim, from [`Table::punch_geometry`].
///
/// Read ONCE and classified once: proving a block zeroed reads it in FULL,
/// and a reclaimed prefix can be hundreds of blocks — deriving the verdict and
/// the bound on separate passes read the same dead prefix twice and left their
/// agreement resting on the caller's ordering.
#[cfg(feature = "std")]
pub(crate) struct PunchGeometry {
/// Whether the zeroed runs are backed by a physical hole.
pub(crate) verdict: PunchProbe,
/// Index and END key of the first readable block. The conservative bound
/// is that key: at or above the true mid-block bound, so no superseded key
/// resurrects. `None` when every block reads as zeros.
pub(crate) first_readable: Option<(usize, UserKey)>,
/// Index of the first readable block AFTER the last zeroed one — the
/// greedy (resurrection) anchor, which keeps the whole readable region.
pub(crate) after_last_zeroed: Option<usize>,
/// A readable block sits BELOW a zeroed one: positive evidence that
/// individual `punch_hole` calls failed mid-reclaim, after which no
/// geometry bound separates intact-but-consumed blocks from live ones.
pub(crate) irregular: bool,
}
impl Table {
#[must_use]
pub fn global_seqno(&self) -> SeqNo {
self.0.global_seqno
}
/// The lowest and highest sequence numbers this table holds, in the tree's
/// own seqno space, or `None` when they cannot be expressed there.
///
/// The stored bounds are LOCAL to the table: a bulk ingest writes rows at
/// local seqnos and carries the base in [`Self::global_seqno`], so the two
/// have to be added before a bound means anything outside this table. Use
/// it to compare the ages of two tables; use [`Self::seqno_visibility`] to
/// ask what a given snapshot sees.
///
/// The two halves of that sum come from different places, the bounds from
/// the table's own metadata and the base from the manifest, so nothing in
/// the type system says they still fit together. A table written normally
/// always fits, since both halves were seqnos the tree issued; damaged
/// metadata need not, and `None` says exactly that rather than wrapping
/// into a plausible-looking bound. A caller comparing ages has to treat it
/// as "cannot tell", never as an extreme.
#[must_use]
pub fn seqno_range(&self) -> Option<(SeqNo, SeqNo)> {
let base = self.global_seqno();
Some((
self.metadata.seqnos.0.checked_add(base)?,
self.metadata.seqnos.1.checked_add(base)?,
))
}
/// Classifies how this SST's rows are visible at query snapshot `seqno`,
/// using the same exclusive MVCC rule as [`Self::point_read`]: a row is
/// visible iff its effective seqno (`local + global_seqno`) is `< seqno`.
///
/// Bulk-ingested columnar SSTs carry a uniform per-row seqno (every local
/// seqno is `0`, sharing one `global_seqno`), so they classify as wholly
/// [`All`](SeqnoVisibility::All) or wholly [`None`](SeqnoVisibility::None);
/// only a flush-produced multi-seqno SST whose seqno range straddles the
/// snapshot is [`Partial`](SeqnoVisibility::Partial), which the tree-level
/// columnar scan resolves with a per-row seqno mask.
pub(crate) fn seqno_visibility(&self, seqno: SeqNo) -> SeqnoVisibility {
// Translate the query snapshot into this table's local seqno space; a
// snapshot below the base predates every row.
let Some(local_threshold) = seqno.checked_sub(self.global_seqno()) else {
return SeqnoVisibility::None;
};
// seqnos are (min, max) local; visible rows satisfy `local < threshold`.
if self.metadata.seqnos.0 >= local_threshold {
return SeqnoVisibility::None;
}
if self.metadata.seqnos.1 < local_threshold {
SeqnoVisibility::All
} else {
SeqnoVisibility::Partial
}
}
/// On-disk bytes of the blob values this table references.
///
/// # Errors
///
/// When the table's `linked_blob_files` section cannot be read or parsed.
pub fn referenced_blob_bytes(&self) -> crate::Result<u64> {
Ok(self.blob_links()?.iter().map(|f| f.on_disk_bytes).sum())
}
/// The blob files this table references, with the key span each is
/// referenced over: read from the `linked_blob_files` section on first use
/// and kept. Empty for a table that references no blob file.
///
/// # Errors
///
/// When the section cannot be read or parsed; nothing is kept then, so a
/// later call reads it again.
pub fn blob_links(&self) -> crate::Result<&[LinkedFile]> {
self.0
.blob_links
.get_or_try_init(|| {
// The links are kept once read, so the descriptor is needed
// this once only: it is taken without promoting or caching it,
// which keeps a statistics call over every table from churning
// the descriptors the workload is using.
let links = self.read_blob_file_references(|| {
Ok(self
.file_accessor
.peek_or_open_table(&self.global_id(), &self.path)?)
})?;
Ok(links.unwrap_or_default().into_boxed_slice().into())
})
.map(|links| &**links)
}
pub fn list_blob_file_references(&self) -> crate::Result<Option<Vec<LinkedFile>>> {
self.read_blob_file_references(|| {
Ok(self
.file_accessor
.get_or_open_table(&self.global_id(), &self.path)?
.0)
})
}
/// [`Self::list_blob_file_references`] for verification: the descriptor is
/// taken without promoting or caching it, so the check leaves no trace.
#[cfg(feature = "std")]
fn untraced_blob_file_references(&self) -> crate::Result<Option<Vec<LinkedFile>>> {
self.read_blob_file_references(|| {
Ok(self
.file_accessor
.peek_or_open_table(&self.global_id(), &self.path)?)
})
}
/// The blob objects this table's cell rows own, as `(blob file, offset)`
/// sorted ascending: read from the `owned_blob_objects` section, not kept.
/// Empty for a table that owns none. What dropping the whole table
/// releases, learned without reading its data.
///
/// # Errors
///
/// When the section cannot be read, fails its checks, or does not parse.
pub(crate) fn owned_blob_objects(&self) -> crate::Result<Vec<(crate::vlog::BlobFileId, u64)>> {
let Some(handle) = self.regions.owned_blob_objects else {
return Ok(Vec::new());
};
// Taken without promoting or caching the descriptor: a drop reads this
// once, for a table that is going.
let fd = self
.file_accessor
.peek_or_open_table(&self.global_id(), &self.path)?;
let transform = match self.encryption.as_deref() {
Some(enc) => crate::table::block::BlockTransform::Encrypted(enc),
None => crate::table::block::BlockTransform::PLAIN,
};
let transform = match self.metadata.ecc_params {
Some(ecc) => transform.with_ecc(ecc),
None => transform,
};
let block = Block::from_file(
fd.as_ref(),
handle,
crate::table::block::BlockIdentity {
table_id: self.metadata.id,
block_type: BlockType::OwnedBlobObjects,
dict_id: 0,
window_log: 0,
},
&transform,
)?;
if block.header.block_type != BlockType::OwnedBlobObjects {
return Err(crate::Error::InvalidTag((
"BlockType",
block.header.block_type.into(),
)));
}
crate::table::writer::parse_owned_blob_objects(&block.data)
}
/// Reads and parses the `linked_blob_files` section through the descriptor
/// `open` yields, opened only when the section exists.
fn read_blob_file_references(
&self,
open: impl FnOnce() -> crate::Result<Arc<dyn FsFile>>,
) -> crate::Result<Option<Vec<LinkedFile>>> {
Ok(if let Some(handle) = &self.regions.linked_blob_files {
let fd = open()?;
// Read the exact region using pread-style helper
let buf =
crate::file::read_exact(fd.as_ref(), *handle.offset(), handle.size() as usize)?;
Some(crate::table::writer::parse_linked_blob_files(&buf)?)
} else {
None
})
}
/// Gets the global table ID.
#[must_use]
pub(crate) fn global_id(&self) -> GlobalTableId {
(self.tree_id, self.id()).into()
}
#[must_use]
pub fn filter_size(&self) -> u32 {
self.regions.filter.map(|x| x.size()).unwrap_or_default()
}
/// The bytes of the sections that grow with the table's data, past its
/// data blocks: every section but its metadata and the framing that holds
/// the file together.
#[cfg(feature = "std")]
pub(crate) fn data_scaled_section_bytes(&self) -> u64 {
let regions = &self.regions;
[
Some(regions.tli),
regions.tli_tail,
regions.index,
regions.filter,
regions.filter_tli,
regions.range_tombstones,
regions.block_layout,
regions.locator,
regions.seqno_bounds,
regions.zone_map,
regions.delete_bitmap,
regions.owned_blob_objects,
regions.linked_blob_files,
]
.into_iter()
.flatten()
.map(|handle| u64::from(handle.size()))
.sum()
}
#[must_use]
pub fn pinned_filter_size(&self) -> usize {
self.pinned_filter_block
.as_ref()
.map(FilterBlock::size)
.unwrap_or_default()
}
/// Filter bytes held in memory now: the pinned filter, or the filter
/// blocks resident in the block cache, counted at their on-disk size.
/// Asks the cache without counting a hit, so a report does not keep the
/// blocks it looks at resident.
#[must_use]
pub(crate) fn resident_filter_bytes(&self) -> u64 {
// A pinned full filter is resident whole; its on-disk handle, not the
// decoded payload, carries the framing the other figures count.
if self.pinned_filter_block.is_some() {
return u64::from(self.filter_size());
}
let resident = |handle: &BlockHandle| {
if self.cache.has_block(self.global_id(), handle.offset()) {
u64::from(handle.size())
} else {
0
}
};
if let Some(filter_idx) = &self.pinned_filter_index {
// A partitioned filter's partitions, each cached on its own.
filter_idx
.iter(self.comparator.clone())
.map(|handle| resident(handle.materialize(filter_idx.as_slice()).as_ref()))
.sum()
} else {
self.regions.filter.as_ref().map_or(0, resident)
}
}
#[must_use]
pub fn pinned_block_index_size(&self) -> usize {
match &*self.block_index {
BlockIndexImpl::Full(full_block_index) => full_block_index.inner().inner.size(),
BlockIndexImpl::VolatileFull(_) | BlockIndexImpl::Closed => 0,
BlockIndexImpl::TwoLevel(two_level_block_index) => {
two_level_block_index.top_level_index.inner.size()
}
}
}
/// Gets the table ID.
///
/// The table ID is unique for this tree, but not
/// across multiple trees, use [`Table::global_id`] for that.
#[must_use]
pub fn id(&self) -> TableId {
self.metadata.id
}
/// The table's L0 recency key, as the manifest records it: an id a flush
/// takes when it starts, an ingested table's id taken when it is
/// installed, a compaction output's highest INPUT recency. Higher = newer
/// content; L0 is ordered by it because a table's own id is allocated at
/// write start, or earlier for a pooled file, and says nothing about where
/// its content belongs.
#[must_use]
pub(crate) fn l0_recency(&self) -> TableId {
self.0.l0_recency
}
/// Records that the directory entry naming this table's file was durable
/// before the table was written into it.
pub(crate) fn mark_entry_durable(&self) {
self.0
.entry_durable
.store(true, core::sync::atomic::Ordering::Relaxed);
}
/// Whether the directory entry naming this table's file was durable before
/// the table was written into it, so the install that names it need not
/// sync its folder.
#[must_use]
pub(crate) fn entry_durable(&self) -> bool {
self.0
.entry_durable
.load(core::sync::atomic::Ordering::Relaxed)
}
/// This segment's positional delete-bitmap (rows deleted by position),
/// loaded on open. Empty when the segment has no materialized deletes, in
/// which case a scan applies no mask.
#[must_use]
pub fn delete_bitmap(&self) -> &crate::table::delete_bitmap::DeleteBitmap {
self.delete_bitmap.as_ref()
}
/// Whether this segment was written WITH a positional delete bitmap (it
/// carries a `delete_bitmap` section), independent of whether that bitmap is
/// currently loaded. This stays `true` even when a salvage-mode open degraded
/// a corrupt bitmap to empty, so the salvage walk can tell "no deletes ever"
/// apart from "deletes whose bitmap was lost" — and must NOT byte-copy a block
/// of the latter verbatim (which would resurrect positionally-deleted rows
/// the recovered copy no longer masks).
///
/// `pub(crate)` for the salvage walk ([`crate::salvage`]); only the columnar
/// copy-through path consults it, so it is gated to that feature.
#[cfg(feature = "columnar")]
pub(crate) fn has_delete_bitmap_section(&self) -> bool {
self.regions.delete_bitmap.is_some()
}
/// Whether the loaded delete-bitmap's CONTENTS match the meta-recorded
/// `descriptor#delete_bitmap_hash` (and length). The section's own block
/// checksum only proves the bytes are self-consistent; an equal-cardinality
/// substitution (a different, checksum-valid bitmap) passes both that and the
/// positional cross-check, yet masks the WRONG rows. Salvage has no original
/// whole-file digest to compare against, so it MUST authenticate the contents
/// before masking. A present section with a missing hash (a table written
/// before the field) cannot be authenticated → `false` (fail closed). A table
/// with no delete-bitmap section has nothing to authenticate → `true`.
#[cfg(feature = "columnar")]
pub(crate) fn delete_bitmap_authenticated(&self) -> bool {
if !self.has_delete_bitmap_section() {
return true;
}
match self.metadata.delete_bitmap_hash {
Some(recorded) => {
let bitmap = self.delete_bitmap();
crate::hash::hash128(&bitmap.encode()) == recorded
&& self.metadata.delete_bitmap_len == Some(bitmap.len())
}
None => false,
}
}
/// Whether this segment carries a parallel `zone_map` section, i.e. it was
/// written with the zone-map policy on and held at least one data block.
/// The section powers predicate-based block-skip; absence means scans read
/// every block. Read-transparent either way, so this is the only way to
/// observe that a flush actually persisted zone maps.
#[must_use]
pub fn has_zone_map(&self) -> bool {
self.regions.zone_map.is_some()
}
/// Whether this segment carries a parallel `seqno_bounds` section. Unlike
/// the loaded map (best-effort at recover time — an unreadable section
/// degrades it to empty), this reflects actual SECTION presence, so a
/// source with rotted bounds still salvages into a copy WITH the section
/// (the writer re-derives the ranges from the re-emitted entries).
#[must_use]
pub fn has_seqno_bounds(&self) -> bool {
self.regions.seqno_bounds.is_some()
}
/// The fraction of this segment's rows masked by its positional
/// delete-bitmap, as a percentage in `0..=100`, or `None` when the segment
/// carries no delete-bitmap (nothing masked).
///
/// Drives the density-based rewrite policy: a segment whose masked fraction
/// has grown past the adaptive purge threshold is worth physically rewriting
/// (dropping the masked rows and clearing the bitmap) rather than paying the
/// merge-on-read mask cost on every scan.
#[must_use]
pub fn delete_density(&self) -> Option<u8> {
let deleted = self.delete_bitmap().len();
if deleted == 0 {
return None;
}
let total = self.metadata.item_count.max(1);
// Widen to u128 so `* 100` cannot overflow regardless of the (already
// bounded) inputs; the quotient is clamped to the 0..=100 percentage range.
let percent = (u128::from(deleted) * 100 / u128::from(total)).min(100);
Some(u8::try_from(percent).unwrap_or(100))
}
/// Iterates every data-block handle in block-index (key) order; each item
/// carries the block's last key. The salvage walk ([`crate::salvage`]) uses
/// this to enumerate the blocks to recover one at a time; a corrupt index
/// entry surfaces as an `Err` item rather than aborting the iteration.
// std-only: the sole consumer is the std-gated salvage walk.
#[cfg(feature = "std")]
pub(crate) fn data_block_handles(&self) -> block_index::BlockIndexIterImpl {
self.maintenance_index_walk()
}
/// Loads the columnar row group `handle` names and decodes the pages
/// `want` selects, one batch per row page, each column in write order.
///
/// The single place a columnar group becomes
/// [`ColumnBatch`](crate::table::columnar::ColumnBatch)es; every read path
/// that consumes one goes through here, so the page format is known to
/// exactly one reader.
///
/// Only the selected pages are read and decoded, after the directory
/// ([`GroupRead::load`](crate::table::row_group::GroupRead::load)): a
/// narrow projection over wide rows does not pay for the columns it
/// skips, nor a read of a few rows for the row pages that do not hold
/// them.
///
/// `charge` applies to the read and to what decoding the pages copies out
/// of them, which a counted read charges to the gather counter before the
/// batch is judged: a group refused after a copy still made it.
///
/// # Errors
///
/// Any block's verification error, a malformed directory or page, or
/// [`crate::Error::InvalidHeader`] for a zero-row group: no writer emits
/// one.
#[cfg(feature = "columnar")]
pub(crate) fn load_row_group(
&self,
handle: &BlockHandle,
want: &crate::table::row_group::PageWant<'_>,
charge: ReadCharge,
) -> crate::Result<crate::table::row_group::RowPages> {
let blocks = self
.group_read(handle, charge, &LookupFd::default())
.load(want)?;
self.row_pages(&blocks, charge)
}
/// Every page of every row group, with what its values were encoded as:
/// the per-page choice the writer made, laid out for a diagnosis of it.
/// Reads each group whole and decodes no values. A row-major table has
/// no pages and returns none.
///
/// # Errors
///
/// A block's verification error, or a malformed directory or page.
#[cfg(all(feature = "columnar", feature = "std"))]
#[doc(hidden)]
pub fn column_page_encodings(&self) -> crate::Result<Vec<crate::inspect::PageEncoding>> {
let mut out = Vec::new();
if !self.metadata.columnar {
return Ok(out);
}
for (group, handle) in (0u64..).zip(self.data_block_handles()) {
let handle = handle?;
let blocks = self
.group_read(handle.as_ref(), ReadCharge::Untraced, &LookupFd::default())
.load(&crate::table::row_group::PageWant::ALL)?;
for page in blocks.page_encodings()? {
out.push(crate::inspect::PageEncoding {
group,
column_id: page.entry.id.column_id,
row_page: page.entry.row_page,
rows: page.rows,
stored_len: page.entry.length,
expression: page.expression,
offsets_len: page.offsets_len,
});
}
}
Ok(out)
}
/// The context a read of the row group `handle` names needs, charged as
/// `charge` says, the file taken through `fd`.
#[cfg(feature = "columnar")]
fn group_read<'a>(
&'a self,
handle: &'a BlockHandle,
charge: ReadCharge,
fd: &'a LookupFd,
) -> crate::table::row_group::GroupRead<'a> {
crate::table::row_group::GroupRead {
table_id: self.global_id(),
path: &self.path,
file_accessor: &self.file_accessor,
cache: &self.cache,
group: handle,
compression: self.metadata.data_block_compression,
encryption: self.encryption.as_deref(),
ecc: self.metadata.ecc_params,
#[cfg(zstd_any)]
zstd_dict: self.zstd_dictionary.as_deref(),
heal_hints: self.heal_hints.get().map(AsRef::as_ref),
#[cfg(feature = "metrics")]
metrics: &self.metrics,
charge,
budget: self.read_budget(),
pace: None,
fd,
}
}
/// `blocks` decoded into row pages, what decoding copied charged as
/// `charge` says.
#[cfg(feature = "columnar")]
#[cfg_attr(
not(feature = "metrics"),
expect(clippy::unused_self, reason = "the gather counter is behind `metrics`")
)]
fn row_pages(
&self,
blocks: &crate::table::row_group::RowGroupBlocks,
charge: ReadCharge,
) -> crate::Result<crate::table::row_group::RowPages> {
let mut copied = 0usize;
let pages = blocks.to_row_pages(&mut copied);
#[cfg(feature = "metrics")]
if charge.is_counted() {
self.metrics.record_gather(copied);
}
#[cfg(not(feature = "metrics"))]
let _ = (copied, charge);
pages
}
fn load_block(
&self,
handle: &BlockHandle,
block_type: BlockType,
compression: CompressionType,
#[cfg(zstd_any)] zstd_dict: Option<&crate::compression::ZstdDictionary>,
fd: &LookupFd,
) -> crate::Result<Block> {
self.load_block_charged(
handle,
block_type,
compression,
#[cfg(zstd_any)]
zstd_dict,
ReadCharge::Foreground,
None,
fd,
)
}
/// [`Self::load_block`] charged as `charge` says, for a read that is not a
/// caller's, and told to `pace` when given.
#[cfg_attr(
zstd_any,
expect(
clippy::too_many_arguments,
reason = "the block's read context: handle, role, codec, dictionary, charge, pacer and file"
)
)]
fn load_block_charged(
&self,
handle: &BlockHandle,
block_type: BlockType,
compression: CompressionType,
#[cfg(zstd_any)] zstd_dict: Option<&crate::compression::ZstdDictionary>,
charge: ReadCharge,
pace: Option<&dyn crate::table::util::ReadPacer>,
fd: &LookupFd,
) -> crate::Result<Block> {
crate::table::util::load_block_paced(
self.global_id(),
&self.path,
&self.file_accessor,
&self.cache,
handle,
block_type,
compression,
self.encryption.as_deref(),
self.metadata.ecc_params,
#[cfg(zstd_any)]
zstd_dict,
self.heal_hints.get().map(AsRef::as_ref),
#[cfg(feature = "metrics")]
&self.metrics,
charge,
pace,
fd,
)
}
/// Loads a data-carrying block STRAIGHT FROM THE FILE, bypassing the
/// block cache in both directions, for the semantic reconcile gates: a
/// block read before an on-disk alteration leaves its pristine copy
/// cached, and a gate served that stale original would judge bytes
/// other than the ones the digest refresh is about to trust. Reuses a
/// cached file descriptor without promoting it, opens one privately on a
/// miss, and never consults or populates the block cache (the cold
/// verification reads must not evict the live working set either).
/// Decodes under the table's data-block codec context, so
/// it fits every gate that walks `block_index` (Data or Columnar role).
// Compiled under no_std alongside its `verify_kv_checksums` consumer,
// which is itself dead there (the verify/scrub caller is std-gated).
#[cfg_attr(
not(feature = "std"),
expect(
dead_code,
reason = "gate-only loader; the verify/scrub consumers are std-gated"
)
)]
fn load_block_from_disk(
&self,
handle: &BlockHandle,
block_type: BlockType,
) -> crate::Result<(Block, crate::Slice)> {
let fd = self
.file_accessor
.peek_or_open_table(&self.global_id(), &self.path)?;
let compression = self.compression_for_role(block_type);
let transform = crate::table::util::build_block_transform(
compression,
self.encryption.as_deref(),
self.metadata.ecc_params,
#[cfg(zstd_any)]
self.dictionary_for_role(block_type),
)?;
// The two halves of ONE read: the frame as the writer compressed it,
// and the block that frame decodes to. Gates that cross-check the
// frame's inner structure (the block-layout boundaries) used to reach
// for it with a second pread of the same bytes.
let (header, frame, _status, _recovery) = Block::read_verified_payload(
fd.as_ref(),
*handle,
crate::table::block::BlockIdentity {
table_id: self.metadata.id,
block_type,
dict_id: compression.dict_id(),
window_log: 0,
},
&transform,
|| {},
)?;
// Swap-defence role check, mirroring `load_block`. Before the
// decompress: a block that is not what the index claims has no
// business being handed to a codec.
if header.block_type != block_type {
return Err(crate::Error::InvalidTag((
"BlockType",
header.block_type.into(),
)));
}
let data = Block::decompress_payload(&header, frame.clone(), &transform, &mut 0)?;
Ok((Block { header, data }, frame))
}
/// Frames the block starting at `offset` by reading its HEADER through an
/// already-open `file`: the on-disk span is header + payload + parity
/// trailer (SST blocks carry no `block_flags` byte, so the trailer is sized
/// from the per-SST descriptor scheme). The writer emits blocks
/// back-to-back, making the physical tiling ground truth: the salvage gap
/// walk frames index-omitted bytes with this, and the TLI mirror gate
/// compares each decoded handle against the frame its header derives. A
/// header that fails to decode, or a span leaving `section_end`, means the
/// bytes are not frameable as a block.
///
/// Takes an open handle because every caller scans many offsets (the
/// salvage resync loop steps one byte at a time, since block starts are
/// not aligned), and one `open` per probe would dominate the walk.
///
/// # Errors
///
/// [`crate::Error::InvalidHeader`] on an undecodable header or an
/// out-of-section span; any I/O error from the read.
#[cfg(feature = "std")]
pub(crate) fn probe_block_handle_in(
&self,
file: &dyn crate::fs::FsFile,
offset: u64,
section_end: u64,
) -> crate::Result<BlockHandle> {
use crate::coding::Decode;
use crate::table::block::Header;
// Positional read of the largest possible header (block_flags-bearing
// types are one byte longer than the SST minimum); a short read only
// matters if it cuts into the bytes `decode_from` actually consumes.
let mut buf = [0u8; Header::MAX_LEN];
let got = file.read_at(&mut buf, offset)?;
let mut cursor = buf.get(..got).ok_or(crate::Error::InvalidHeader(
"block header read out of bounds",
))?;
let header = Header::decode_from(&mut cursor)?;
let header_len = Header::header_len(header.block_type) as u64;
let parity_len = self.metadata.ecc_params.map_or(0, |scheme| {
u64::from(crate::table::block::expected_parity_len(
header.data_length,
scheme,
))
});
let total = header_len
.checked_add(u64::from(header.data_length))
.and_then(|t| t.checked_add(parity_len))
.ok_or(crate::Error::InvalidHeader("block span overflows the file"))?;
let end = offset
.checked_add(total)
.ok_or(crate::Error::InvalidHeader("block span overflows the file"))?;
if end > section_end {
return Err(crate::Error::InvalidHeader(
"block extends past its section",
));
}
let size = u32::try_from(total)
.map_err(|_| crate::Error::InvalidHeader("block span exceeds the block size limit"))?;
Ok(BlockHandle::new(BlockOffset(offset), size))
}
/// Frames the DATA UNIT starting at `offset` — what one data index entry
/// covers — from the bytes alone, as [`Self::probe_block_handle_in`] frames
/// one block.
///
/// A row-major table's unit is one block. A columnar table's is a row
/// group, and its extent is not in the first block's header: that header
/// is the directory's, which frames only itself. So the directory is read
/// and the group's extent is its own length, every page it lists and the
/// zone block it counts.
/// Framing the directory alone would make a group look like a run of
/// unrelated blocks to every walk that tiles the data section by frames.
///
/// # Errors
///
/// As [`Self::probe_block_handle_in`]; for a columnar table also when the
/// block at `offset` is not a page directory, or the directory is
/// unreadable or reaches past `section_end`.
#[cfg(feature = "std")]
pub(crate) fn probe_data_unit_in(
&self,
file: &dyn crate::fs::FsFile,
offset: u64,
section_end: u64,
) -> crate::Result<BlockHandle> {
let first = self.probe_block_handle_in(file, offset, section_end)?;
if !self.metadata.columnar {
return Ok(first);
}
#[cfg(feature = "columnar")]
{
let directory_block = Block::from_file(
file,
first,
crate::table::block::BlockIdentity {
table_id: self.id(),
block_type: BlockType::ColumnPageDirectory,
dict_id: 0,
window_log: 0,
},
&crate::table::util::build_block_transform(
CompressionType::None,
self.encryption.as_deref(),
self.metadata.ecc_params,
#[cfg(zstd_any)]
None,
)?,
)?;
if directory_block.header.block_type != BlockType::ColumnPageDirectory {
return Err(crate::Error::InvalidTag((
"BlockType",
directory_block.header.block_type.into(),
)));
}
let directory =
crate::table::column_page::PageDirectory::decode(&directory_block.data)?;
let size = directory
.group_len(first.size())
.ok_or(crate::Error::InvalidHeader("row group span overflows"))?;
if offset
.checked_add(u64::from(size))
.is_none_or(|end| end > section_end)
{
return Err(crate::Error::InvalidHeader(
"row group extends past its section",
));
}
// A group found by its frame has no index entry to name it; the
// tag its own directory carries is the only one there is, and the
// pages' stamps are still checked against it when they decode.
// A zero tag names no group, and every read then refuses it.
let row_group = core::num::NonZeroU64::new(directory.group_tag()).and_then(|tag| {
Some(crate::table::index_block::RowGroupRef {
tag,
directory_len: core::num::NonZeroU32::new(first.size())?,
head_zones_len: directory.pages_start(),
})
});
Ok(BlockHandle::new(BlockOffset(offset), size).with_row_group(row_group))
}
#[cfg(not(feature = "columnar"))]
{
Err(crate::Error::FeatureUnsupported("columnar"))
}
}
/// Loads (and, for columnar SSTs, reconstructs + delete-masks) a data block.
/// Returns `Ok(None)` when a columnar block is wholly deleted by the
/// positional mask, so the caller treats it as carrying no keys.
///
/// `pub(crate)` so the repair tests can load one block of a table they
/// damaged; the salvage walk reads through its own loader,
/// [`Table::salvage_load_block`].
///
/// The file is taken through `fd`, which the lookup's other block reads
/// share.
pub(crate) fn load_data_block(
&self,
handle: &BlockHandle,
fd: &LookupFd,
) -> crate::Result<Option<DataBlock>> {
self.load_data_block_paced(handle, ReadCharge::Foreground, None, fd)
}
/// [`Self::load_data_block`] for maintenance that sizes a rewrite of the
/// table: not counted as a read, and told to `pace`, the rewrite's own,
/// as the rewrite's reads are.
#[cfg(feature = "std")]
pub(crate) fn load_data_block_for_maintenance(
&self,
handle: &BlockHandle,
pace: &dyn crate::table::util::ReadPacer,
) -> crate::Result<Option<DataBlock>> {
self.load_data_block_paced(
handle,
ReadCharge::Maintenance,
Some(pace),
&LookupFd::default(),
)
}
/// [`Self::load_data_block`] charged as `charge` says, for a read that is
/// not a caller's.
fn load_data_block_charged(
&self,
handle: &BlockHandle,
charge: ReadCharge,
) -> crate::Result<Option<DataBlock>> {
self.load_data_block_paced(handle, charge, None, &LookupFd::default())
}
/// [`Self::load_data_block_charged`], told to `pace` when given, the file
/// taken through `fd`.
fn load_data_block_paced(
&self,
handle: &BlockHandle,
charge: ReadCharge,
pace: Option<&dyn crate::table::util::ReadPacer>,
fd: &LookupFd,
) -> crate::Result<Option<DataBlock>> {
// Columnar SSTs store each data block as a PAX `ColumnBatch`; reconstruct
// the row entries on load so every row read path works unchanged.
#[cfg(feature = "columnar")]
if self.metadata.columnar {
return self.load_columnar_data_block(handle, charge, pace, fd);
}
// `from_loaded` transparently strips the per-KV checksum footer when
// this SST carries one. Footer presence is a per-SST property
// (`kv_checksum_algo`), not a per-block header flag — data blocks omit
// the block_flags byte — so the descriptor supplies it here.
let has_kv_footer = self.metadata.kv_checksum_algo.is_some();
self.load_block_charged(
handle,
BlockType::Data,
self.metadata.data_block_compression,
#[cfg(zstd_any)]
self.zstd_dictionary.as_deref(),
charge,
pace,
fd,
)
.and_then(|block| DataBlock::from_loaded(block, has_kv_footer))
.map(Some)
.map_err(|e| self.classify_excised(handle, e, charge))
}
/// Re-reports a failed block load as [`crate::Error::Excised`] when the
/// block's bytes are physically absent — an all-zero extent, the signature
/// of a hole punch — rather than damaged.
///
/// Tight-space reclaim punches consumed extents in place, so a table can
/// legitimately survive with intact blocks around a hole. A read that
/// lands in the hole is a permanent loss of exactly those rows, and the
/// distinction matters to the caller: rotted bytes may be healable and
/// are worth retrying or scrubbing, an excised extent never is. The probe
/// runs ONLY on the failure path, so a healthy read pays nothing for it,
/// and it needs no recorded state — the zeros identify themselves, which
/// is what lets an in-place excision survive any crash unrecorded.
#[cfg(feature = "std")]
fn classify_excised(
&self,
handle: &BlockHandle,
err: crate::Error,
charge: ReadCharge,
) -> crate::Error {
// A transient / positioned-read failure is not a verdict about the
// bytes: leave it exactly as it is so the caller can still retry.
if matches!(err, crate::Error::Io(_)) {
return err;
}
let Ok(file) = self.fs.open(&self.path, &FsOpenOptions::new().read(true)) else {
return err;
};
// The probe's reads are this read path's too, so they are charged
// like the load that failed.
#[cfg(feature = "metrics")]
let mut on_read = |len: u64| {
if charge.is_counted() {
crate::table::util::record_block_read(&self.metrics, BlockType::Data, len);
}
};
#[cfg(not(feature = "metrics"))]
let mut on_read = |_: u64| {
let _ = charge;
};
match Self::block_is_zeroed_in(&*file, handle, &mut on_read) {
Ok(true) => crate::Error::Excised {
offset: handle.offset().0,
},
// Not zeroed, or the probe itself failed: the original verdict
// stands — never let a probe failure mask the real error.
Ok(false) | Err(_) => err,
}
}
/// No-std builds have no `Fs` open on this path; the original error stands.
#[cfg(not(feature = "std"))]
const fn classify_excised(
&self,
_handle: &BlockHandle,
err: crate::Error,
_charge: ReadCharge,
) -> crate::Error {
err
}
/// Loads a columnar data block and reconstructs it as a row-major
/// [`DataBlock`]: decode the `ColumnBatch`, rebuild the entries, and
/// re-encode them row-major in memory so the existing point-read / iterator
/// machinery is reused verbatim. The native column-projection read path
/// (decode only the referenced columns) is a later optimization.
///
/// Returns `Ok(None)` when the positional delete-bitmap deletes every row of
/// the block, so the caller treats it as carrying no keys.
#[cfg(feature = "columnar")]
fn load_columnar_data_block(
&self,
handle: &BlockHandle,
charge: ReadCharge,
pace: Option<&dyn crate::table::util::ReadPacer>,
fd: &LookupFd,
) -> crate::Result<Option<DataBlock>> {
let blocks = crate::table::row_group::GroupRead {
pace,
..self.group_read(handle, charge, fd)
}
.load(&crate::table::row_group::PageWant::ALL)?;
self.columnar_data_block_from(handle, &blocks, charge)
}
/// The row-major data block of the row group `handle` names, rebuilt from
/// its loaded `blocks` and delete-masked: `None` when the mask removes
/// every row.
#[cfg(feature = "columnar")]
fn columnar_data_block_from(
&self,
handle: &BlockHandle,
blocks: &crate::table::row_group::RowGroupBlocks,
charge: ReadCharge,
) -> crate::Result<Option<DataBlock>> {
let batch = self.row_pages(blocks, charge)?.batches;
let restart = self.metadata.data_block_restart_interval;
// The segment has materialized deletes and this block has a recorded
// start position: drop the deleted rows during reconstruction. The
// start-row map is built at open from the zone map (every block), so
// an unmapped block is unreachable; it falls through to the whole-block
// reconstruction below rather than masking against the wrong positions.
let mut values = 0usize;
let rebuilt = if let Some(&start) = self
.delete_block_starts
.as_ref()
.and_then(|starts| starts.get(&handle.offset().0))
{
DataBlock::from_column_batch_masked(
batch,
restart,
&self.delete_bitmap,
start,
&mut values,
)
} else {
// No materialized deletes (or, unreachably, an unmapped block):
// reconstruct the whole block.
DataBlock::from_column_batch(batch, restart, &mut values).map(Some)
};
// Two gathers, each charged at the size of what it built: the values
// rebuilt from sub-columns (none when they are views), and the
// row-major block encoded from the rows. The first is charged before
// the result is judged: a block refused after it still did it.
#[cfg(feature = "metrics")]
if charge.is_counted() {
self.metrics.record_gather(values);
}
#[cfg(not(feature = "metrics"))]
let _ = (values, charge);
let rebuilt = rebuilt?;
#[cfg(feature = "metrics")]
if let Some(rebuilt) = &rebuilt
&& charge.is_counted()
{
self.metrics.record_gather(rebuilt.inner.data.len());
}
Ok(rebuilt)
}
/// Loads a columnar data block as a delete-masked
/// [`ColumnBatch`](crate::table::columnar::ColumnBatch), preserving its
/// per-field value sub-columns (and per-row seqnos) instead of reconstructing
/// rows. Salvage re-emits the result verbatim so a recovered columnar SST
/// keeps its sub-columns and MVCC versions; `Ok(None)` when the positional
/// delete-bitmap removes every row of the block.
///
/// `pub(crate)` for the salvage walk ([`crate::salvage`]). Salvage is
/// maintenance, so nothing here is charged to the gather counter.
#[cfg(feature = "columnar")]
pub(crate) fn load_columnar_block_masked(
&self,
handle: &BlockHandle,
) -> crate::Result<Option<crate::table::columnar::ColumnBatch>> {
// `load_row_group` already refuses a zero-row group: a real writer
// never emits one, and a caller counting it as recovered would
// misreport an unrecovered group as salvaged.
// Salvage re-emits the group through a writer that takes it whole and
// cuts its own row pages, so the pages are joined here.
let batch = self
.load_row_group(
handle,
&crate::table::row_group::PageWant::ALL,
ReadCharge::Maintenance,
)?
.into_batch()?;
let Some(start) = self
.delete_block_starts
.as_ref()
.and_then(|starts| starts.get(&handle.offset().0))
.copied()
else {
// No materialized deletes: the whole block is live.
return Ok(Some(batch));
};
// Drop positionally-deleted rows: this block's rows occupy global
// positions `[start, start + row_count)` in write order. A corrupt zone
// map can make `start` large enough that `start + i` overflows the u32
// row-position space and wraps back to the start of the bitmap, masking
// unrelated rows. Fail closed on overflow: the salvage walk drops and
// re-emits this block as corrupt rather than applying deletes at wrong
// positions.
let keep: alloc::vec::Vec<bool> = (0..batch.row_count)
.map(|i| {
let pos = start.checked_add(i).ok_or(crate::Error::InvalidHeader(
"columnar delete position overflow",
))?;
Ok(!self.delete_bitmap.contains(pos))
})
.collect::<crate::Result<_>>()?;
let masked = crate::table::columnar_predicate::filter_batch(&batch, &keep)?;
if masked.row_count == 0 {
Ok(None)
} else {
Ok(Some(masked))
}
}
/// Cross-checks the positional delete mask against the ACTUAL per-block
/// row counts. `delete_block_starts` is derived from the zone map, and a
/// zone map that decodes but carries wrong counts (a checksum-repatched
/// tamper) shifts every later block's claimed start — the mask would then
/// delete the WRONG rows, silently. Walks the data blocks in index order
/// and requires each block's claimed start to equal the running sum of
/// actual decoded row counts. An UNREADABLE block fails the verification
/// outright: its actual count is unknowable, and trusting the zone map's
/// claim for it would let a repatched count on exactly that block shift
/// every later mask undetected. Trivially `true` when the segment has no
/// materialized deletes.
///
/// `pub(crate)` for the salvage walk ([`crate::salvage`]), which must not
/// mask against unverified positions.
///
/// # Errors
///
/// Propagates an ENVIRONMENTAL failure from an index / block read (see
/// [`crate::Error::is_environmental`]): a flaky device, a refused mount, a
/// missing key or dictionary. `Ok(false)` is reserved for a STRUCTURAL
/// failure (a reordered index, a count mismatch, an undecodable or zero-row
/// block): folding a fixable-context fault into `false` would classify it
/// as a persistent unpositionable mask, and — with the default
/// `allow_delete_resurrection == false` — abort salvage, letting
/// `repair_with_salvage` rebuild the manifest WITHOUT a table a retry (or
/// the right key) could have recovered faithfully.
#[cfg(feature = "columnar")]
pub(crate) fn delete_positions_verified(&self) -> crate::Result<bool> {
let Some(starts) = self.delete_block_starts.as_deref() else {
// No materialized deletes: there is nothing to position.
return Ok(true);
};
let mut cumulative: u32 = 0;
// Anchor the walk to PHYSICAL block order. `delete_block_starts` is built
// by walking this same index, so a forged TLI that REORDERS the handles
// rebuilds the starts in that reordered sequence and self-validates
// against them, yet the bitmap positions were assigned in the writer's
// physical block order, so the salvage walk (which sorts blocks by
// offset) would mask against the wrong starts. Requiring strictly
// increasing offsets rejects the reorder: a genuine index is always in
// offset order (the writer emits blocks back-to-back).
let mut prev_offset: Option<u64> = None;
for keyed in self.maintenance_index_walk() {
let keyed = match keyed {
Ok(keyed) => keyed,
// Only an ENVIRONMENTAL read propagates: a retry, the right key,
// or the right dictionary could verify the mask, and answering
// `Ok(false)` would let the caller fail the salvage closed over
// an intact table. A read that fails on the DATA makes every
// later position unverifiable, so degrade to an unpositionable
// mask (`Ok(false)`) and let the resurrection opt-in decide.
Err(e) if e.is_environmental() => return Err(e),
Err(_) => return Ok(false),
};
let offset = keyed.offset().0;
if prev_offset.is_some_and(|prev| offset <= prev) {
// Out-of-order (or duplicate) offset: the index was reordered, so
// the physical positions cannot be trusted.
return Ok(false);
}
prev_offset = Some(offset);
if starts.get(&offset) != Some(&cumulative) {
return Ok(false);
}
let handle = *keyed.as_ref();
// FULLY decode the group rather than trusting the directory's row
// count: a checksum-repatched tamper can keep that field intact
// while breaking a page's column framing. The salvage walk would
// drop such a group as undecodable — but its ACTUAL row count is
// then just as unknowable as an unreadable group's, so accepting
// the claimed count here would let the mask land on unproven
// positions for every later group. Decoding checks every page
// against that count, so a group that decodes has proved it.
let pages = match self.load_row_group(
&handle,
&crate::table::row_group::PageWant::ALL,
ReadCharge::Maintenance,
) {
Ok(pages) => pages,
// Only an ENVIRONMENTAL read propagates (see the index arm
// above); a load or decode that fails on the DATA leaves the
// group's actual count unknowable, so every later position is
// unverifiable — degrade to an unpositionable mask
// (`Ok(false)`) rather than trust the (potentially tampered)
// zone-map claim for it, and let the resurrection opt-in decide.
Err(e) if e.is_environmental() => return Err(e),
Err(_) => return Ok(false),
};
// A ZERO-ROW batch is malformed input (a real writer never emits
// an empty block) and the salvage walk DROPS it — so accepting it
// here (a tampered zone map can claim 0 for exactly that block,
// keeping the chain self-consistent) would verify positions the
// bitmap was never built against for every later block. Reject
// it like the rest of the salvage pipeline does.
let advance = pages.row_count();
if advance == 0 {
return Ok(false);
}
// `wrapping_add` matches how the open path builds the starts map,
// so the comparison chain stays consistent (the salvage read mask
// separately rejects positions that would overflow).
cumulative = cumulative.wrapping_add(advance);
}
Ok(true)
}
/// Salvage helper: load one data block recovery-aware and, when it reads back
/// cleanly, also capture its raw on-disk bytes for a verbatim copy.
///
/// Bypasses the block cache so the returned recovery status reflects THIS read
/// of the medium (a cached block hides whether the on-disk bytes needed ECC
/// repair). On a clean read ([`verbatim`](SalvageBlock::verbatim) is `Some`),
/// the salvage walk byte-copies the raw bytes into the recovered SST instead of
/// decoding + re-encoding the block; on an ECC-recovered read (`None`) the
/// faulty on-disk bytes must not be propagated, so the caller re-encodes the
/// healed payload in [`block`](SalvageBlock::block) instead.
///
/// `pub(crate)` for the salvage walk ([`crate::salvage`]).
pub(crate) fn salvage_load_block(
&self,
handle: &BlockHandle,
block_type: BlockType,
) -> crate::Result<SalvageBlock> {
let table_id = self.global_id();
let (fd, _) = self
.file_accessor
.get_or_open_table(&table_id, &self.path)?;
let compression = self.compression_for_role(block_type);
let transform = crate::table::util::build_block_transform(
compression,
self.encryption.as_deref(),
self.metadata.ecc_params,
#[cfg(zstd_any)]
self.dictionary_for_role(block_type),
)?;
let (block, status, recovery) = crate::table::block::Block::from_file_with_recovery(
fd.as_ref(),
*handle,
crate::table::block::BlockIdentity {
table_id: table_id.table_id(),
block_type,
dict_id: compression.dict_id(),
window_log: 0,
},
&transform,
)?;
// A wrong block type means a swapped / corrupt index entry pointed us at
// the wrong bytes; surface it rather than salvage the wrong block.
if block.header.block_type != block_type {
return Err(crate::Error::InvalidTag((
"BlockType",
block.header.block_type.into(),
)));
}
// A table whose ECC descriptor this build cannot interpret still reads
// cleanly (`EccStatus::Unrecognized`, `recovery.is_none()`), but its raw
// bytes carry an opaque parity trailer the salvage writer's mirrored ECC
// (`ecc_params = None`) does not account for. A verbatim copy of those
// bytes would fail the writer's on-disk-size check and abort the whole
// salvage, so force the re-encode path (which emits a trailer-free block
// from the decoded payload) for such tables. The same applies to a
// RECOGNIZED scheme on a build without `page_ecc`: the salvage writer
// mirrors ECC as `None` there (it cannot emit parity), so raw bytes
// with a trailer must be re-encoded trailer-free, not byte-copied.
let ecc_verbatim_ok = cfg!(feature = "page_ecc") || self.metadata.ecc_params.is_none();
// Clean read: capture the raw on-disk bytes (and inner layout) for a
// verbatim copy. A second pread of a cold block costs far less than the
// re-compression the copy avoids.
//
// `Err` from this function is reserved for the initial VERIFIED read:
// by this point that read has already produced a recoverable decoded
// block, so ANY failure of the re-read — a transient I/O error or a
// corrupt header, same as a checksum / parity mismatch below — must
// not drop the block; it just disqualifies the byte-copy and falls
// back to re-encoding the verified payload (`verbatim = None`).
let capture_verbatim = || -> Option<VerbatimCopy> {
use crate::coding::Decode;
let raw =
crate::file::read_exact(fd.as_ref(), *handle.offset(), handle.size() as usize)
.ok()?;
let header = crate::table::block::Header::decode_from(&mut &raw[..]).ok()?;
let layout = self.inner_block_layout(handle.offset().0);
// A recorded MULTI-INNER layout is the untrusted `block_layout`
// section itself: a checksum-consistent forge of it routes an
// otherwise-readable zstd SST through salvage, and copying the block
// verbatim would re-emit the same unauthenticated inner boundaries,
// so partial range reads keep omitting keys even though salvage
// reports success. Disqualify verbatim for such blocks; the re-encode
// path below rebuilds the frame from the verified decoded payload and
// records a fresh, self-consistent layout. Single-inner blocks (the
// common case, empty layout) are unaffected.
if !layout.is_empty() {
return None;
}
// The bytes COPIED are this second read, not the verified first
// one, so validate this exact frame before marking it
// verbatim-safe (a transient fault or concurrent mutation between
// the reads must not persist unchecked bytes):
// - the re-read header must equal the verified read's header;
// - the re-read payload must hash to the header's stored checksum
// (the checksum covers the on-disk payload bytes uniformly —
// pre-decrypt, pre-decompress);
// - for an ECC table, the parity trailer must match freshly
// computed parity (a clean payload checksum never validates the
// trailer — parity is only consulted on a mismatch — so bit rot
// confined to the trailer otherwise reads as a clean block).
// Any mismatch falls back to the re-encode path, which emits the
// VERIFIED first read's decoded payload with fresh framing.
let header_len = crate::table::block::Header::header_len(header.block_type);
// checked_add: a forged/rotted data_length can overflow the
// payload-end sum on 32-bit targets; overflow = not verbatim-safe.
let payload_checksum_ok = header_len
.checked_add(header.data_length as usize)
.and_then(|payload_end| raw.get(header_len..payload_end))
.is_some_and(|payload| {
crate::hash::hash128(payload)
== header
.payload_checksum(crate::table::block::ChecksumAt::block(
table_id.table_id(),
block_type,
*handle.offset(),
))
.into_u128()
});
(header == block.header
&& payload_checksum_ok
&& self.raw_block_parity_verifies(&raw, &header))
.then(|| (raw.to_vec(), header, layout))
};
// The PER-BLOCK status must be Ok too: `EccStatus::Unrecognized` means
// the frame carried trailing bytes the transform could not attribute
// (e.g. an over-sized forged index handle leaking the next section's
// bytes into the read) — the payload verified, but the raw frame is
// longer than the header's on-disk size and a verbatim copy would be
// rejected by the writer, dropping a recoverable block. Re-encode the
// verified payload instead.
let verbatim = if recovery.is_none()
&& status == crate::table::block::EccStatus::Ok
&& !self.metadata.ecc_unrecognized
&& ecc_verbatim_ok
{
capture_verbatim()
} else {
None
};
Ok(SalvageBlock {
block,
verbatim,
ecc_recovered: recovery.is_some(),
})
}
/// Salvage helper for a columnar table's entry: loads every block of the
/// row group through [`Self::salvage_load_block`].
///
/// The group may be byte-copied only when EVERY block in it qualified for
/// a verbatim copy on its own. One ECC-healed page makes the whole group's
/// raw bytes unsafe to propagate, because they are one unit on disk and
/// one index entry in the copy; the caller then re-encodes the decoded
/// batch instead. When they all qualify, their raw frames — adjacent by
/// construction, as the extent check proved — ARE the group's bytes.
///
/// `pub(crate)` for the salvage walk ([`crate::salvage`]).
///
/// # Errors
///
/// A block's verified read failing, or a directory that cannot be read or
/// does not fill the group: without it the pages cannot be located, so
/// the group is lost as a unit, exactly as a columnar block was.
#[cfg(all(feature = "columnar", feature = "std"))]
pub(crate) fn salvage_load_row_group(
&self,
group: &BlockHandle,
) -> crate::Result<SalvageRowGroup> {
let mut blocks = self.data_unit_blocks(group)?.into_iter();
let Some((directory_handle, directory_role)) = blocks.next() else {
return Err(crate::Error::InvalidHeader(
"columnar: row group has no directory",
));
};
let directory = self.salvage_load_block(&directory_handle, directory_role)?;
let decoded = crate::table::column_page::PageDirectory::decode(&directory.block.data)?;
let mut ecc_recovered = directory.ecc_recovered;
let mut verbatim = directory
.verbatim
.map(|(raw, header, _)| (raw, u64::from(header.uncompressed_length)));
let mut pages = alloc::vec::Vec::with_capacity(decoded.entries().len());
// The zone blocks in the order they lie: the head one, then the rest.
let listed: alloc::vec::Vec<crate::table::column_page::ZoneBlock> = decoded
.head_zone_block()
.into_iter()
.chain(decoded.zone_blocks().iter().copied())
.collect();
let mut zone_blocks = listed.iter();
for (handle, role) in blocks {
let block = self.salvage_load_block(&handle, role)?;
ecc_recovered |= block.ecc_recovered;
// Every block of the group, the zone blocks included, goes into
// the verbatim copy: the copy is the group whole or nothing.
verbatim = match (verbatim, block.verbatim) {
(Some((mut raw, uncompressed)), Some((block_raw, header, _))) => {
raw.extend_from_slice(&block_raw);
Some((raw, uncompressed + u64::from(header.uncompressed_length)))
}
_ => None,
};
if role == BlockType::ColumnZones {
// Decoded against the column the directory files it under, so
// a zone block that verifies as a block but not as that
// column's zones fails the group here, not in a later read.
let column_id =
zone_blocks
.next()
.map(|z| z.column_id)
.ok_or(crate::Error::InvalidHeader(
"columnar: a zone block the directory does not list",
))?;
decoded.decode_zone_block(column_id, &block.block.data)?;
} else {
pages.push(block.block);
}
}
Ok(SalvageRowGroup {
group: crate::table::row_group::RowGroupBlocks::whole(
alloc::sync::Arc::new(decoded),
pages,
),
verbatim,
ecc_recovered,
})
}
/// Whether `raw`'s parity trailer matches freshly computed parity over its
/// payload — the verbatim-copy eligibility check for a block of an
/// ECC-carrying table (see [`Self::salvage_load_block`]). Trivially `true`
/// for a table without a recognized ECC scheme; a build without `page_ecc`
/// never reaches this for an ECC table (the caller's gate already routes
/// those to the re-encode path).
#[cfg_attr(
not(feature = "page_ecc"),
expect(
clippy::unused_self,
reason = "without page_ecc the caller's gate excludes ECC tables, so there is no parity to check"
)
)]
fn raw_block_parity_verifies(&self, raw: &[u8], header: &crate::table::block::Header) -> bool {
#[cfg(feature = "page_ecc")]
{
matches!(self.raw_block_parity_delta(raw, header), Ok(None))
}
#[cfg(not(feature = "page_ecc"))]
{
let _ = (raw, header);
true
}
}
/// Compares `raw`'s parity trailer against freshly computed parity over
/// its payload. `Ok(None)` — the trailer matches (or the table carries no
/// recognized ECC scheme); `Ok(Some(fresh))` — MISMATCH, `fresh` is the
/// parity the trailer should hold (the in-place heal persists it);
/// `Err(())` — the frame is inconsistent or the encoder rejected the
/// shape, so the trailer is unverifiable.
#[cfg(feature = "page_ecc")]
fn raw_block_parity_delta(
&self,
raw: &[u8],
header: &crate::table::block::Header,
) -> Result<Option<alloc::vec::Vec<u8>>, ()> {
let Some(params) = self.metadata.ecc_params else {
return Ok(None);
};
let header_len = crate::table::block::Header::header_len(header.block_type);
// Checked: `data_length` comes from a re-read header, so a forged
// value must fail as "unverifiable", never wrap (32-bit `usize`).
let Some(payload_end) = header_len.checked_add(header.data_length as usize) else {
return Err(());
};
// Treat any frame inconsistency as "unverifiable" rather than panicking.
let (Some(payload), Some(trailer)) =
(raw.get(header_len..payload_end), raw.get(payload_end..))
else {
return Err(());
};
let fresh = match params {
crate::table::block::EccParams::Secded => crate::secded::encode_block_parity(payload),
crate::table::block::EccParams::Shard { .. } => {
let (ds, ps) = params.as_shards();
match crate::ecc::encode_parity(payload, ds, ps) {
Ok(p) => p,
Err(_) => return Err(()),
}
}
};
// The heal writes `fresh` back AT the trailer's offset, so the frame
// must hold EXACTLY that many trailer bytes: a shorter (or longer)
// on-disk trailer means the frame is malformed, and "healing" it
// would write past the frame's end into the next block's bytes,
// breaking the size-preserving heal contract. Unverifiable, not
// healable.
if trailer.len() != fresh.len() {
return Err(());
}
if trailer == fresh {
Ok(None)
} else {
Ok(Some(fresh))
}
}
/// The inner-zstd block layout (cumulative decompressed end offsets) for the
/// data block at `offset`, or empty when the block has a single inner block
/// (the common case) — and always, on a build without zstd, where data blocks
/// never split. Salvage's verbatim copy passes this through so a multi-inner
/// block keeps its layout at its new file offset (the offsets are
/// decompressed-space and block-relative, so they stay valid after the move).
#[cfg_attr(
not(feature = "zstd"),
expect(
clippy::unused_self,
reason = "the layout lookup is zstd-only; without zstd no data block splits, so the layout is always empty and `self` is unused"
)
)]
fn inner_block_layout(&self, offset: u64) -> alloc::vec::Vec<u32> {
#[cfg(feature = "zstd")]
{
self.block_layout
.ends_for(offset)
.map(<[u32]>::to_vec)
.unwrap_or_default()
}
#[cfg(not(feature = "zstd"))]
{
let _ = offset;
alloc::vec::Vec::new()
}
}
/// Point-read counterpart to [`Self::load_columnar_data_block`]: rebuilds
/// only `needle`'s rows (its MVCC versions, minus any masked by the
/// positional delete-bitmap) into a tiny row block, or `Ok(None)` when the
/// key is absent / wholly deleted. The caller runs the normal seqno-aware
/// point read on the result.
///
/// Only the key pages of the row pages whose key zone holds `needle` are
/// read first, and a key they do not hold ends the read there: a miss
/// never reads the group's other pages. On a hit only the row pages that
/// hold the key are read, the directory and key pages coming from the
/// cache.
#[cfg(feature = "columnar")]
fn load_columnar_point_block(
&self,
handle: &BlockHandle,
needle: &[u8],
fd: &LookupFd,
) -> crate::Result<Option<DataBlock>> {
use crate::table::columnar::{COL_USER_KEY, TypeTag};
use crate::table::row_group::{PageWant, RowPageSelect};
// A key equal to the needle is the needle's bytes (the comparator
// contract makes equality byte equality), so it lies within the
// byte-wise zone of the row page holding it, whatever the key order.
let group = self.group_read(handle, ReadCharge::Foreground, fd);
let key_blocks = group.load(&PageWant::projected(
&[COL_USER_KEY],
RowPageSelect::Zone {
column_id: COL_USER_KEY,
lower: Some(needle),
upper: Some(needle),
},
))?;
// The key pages as stored: the search reads the keys it compares and
// decodes no page whole. What both steps hold decoded is the group's.
let mut budget = crate::table::columnar::DecodeBudget::default();
let key_pages = key_blocks.page_columns(|_| true, &mut budget)?;
// The key's versions are one run of rows, sorted with the rest of the
// group, so they sit on consecutive row pages: the first page whose
// keys reach it through the page where the run ends. Every page of
// the run is selected, since each holds the needle.
let mut hit: Option<core::ops::Range<u16>> = None;
// Each hit page's run of the needle's rows, so the second step reads
// those rows without searching the keys again.
let mut runs: Vec<core::ops::Range<u32>> = Vec::new();
for page in key_pages {
let (ordinal, row_count) = (page.ordinal, page.rows);
let Some(key_col) = page
.columns
.into_iter()
.next()
.filter(|c| c.column_id == COL_USER_KEY && c.type_tag == TypeTag::Bytes)
else {
return Err(crate::Error::InvalidHeader(
"columnar: row group has no user-key column",
));
};
let keys = budget.rows(key_col.values, TypeTag::Bytes, row_count)?;
let rows =
crate::table::columnar::key_rows(row_count, needle, &self.comparator, |row| {
keys.get(TypeTag::Bytes, row_count, row)
})?;
if rows.is_empty() {
// Every key of a page before the run is smaller than the
// needle, so the search stops at `row_count`; a run that has
// started, or a page past the needle, ends the search.
if hit.is_some() || rows.start < row_count {
break;
}
continue;
}
let end = ordinal + 1;
hit = Some(hit.map_or(ordinal..end, |h| h.start..end));
let last = rows.end < row_count;
runs.push(rows);
if last {
break;
}
}
let Some(row_pages) = hit else {
return Ok(None);
};
// The rest of the row's columns on those row pages, through the
// directory and key pages the first step already has.
let blocks = group.load_after(
&key_blocks,
&PageWant {
columns: None,
row_pages: RowPageSelect::Range(row_pages),
whole: false,
},
)?;
// The key search already placed the needle's rows, whose key is the
// needle: the key pages are not read again.
let pages = blocks.page_columns(|column_id| column_id != COL_USER_KEY, &mut budget)?;
let first_row = pages.first().map_or(0, |page| page.start);
let deletes = match self
.delete_block_starts
.as_ref()
.and_then(|starts| starts.get(&handle.offset().0))
{
Some(&start) => Some((
self.delete_bitmap.as_ref(),
start
.checked_add(first_row)
.ok_or(crate::Error::InvalidHeader(
"columnar: row position exceeds u32::MAX",
))?,
)),
None => None,
};
let mut rows = 0usize;
let rebuilt = DataBlock::columnar_point_block(
pages,
&runs,
needle,
self.metadata.data_block_restart_interval,
deletes,
&mut rows,
&mut budget,
);
// `rows` is the needle's keys and values copied out of the columns
// when it is present (what decoding the pages copied was charged by
// `load_row_group`); a miss or a refused group still did those copies,
// so they are charged either way. The small block encoded from the
// rows exists only on a hit.
#[cfg(feature = "metrics")]
self.metrics.record_gather(rows);
#[cfg(not(feature = "metrics"))]
let _ = rows;
let rebuilt = rebuilt?;
#[cfg(feature = "metrics")]
if let Some(rebuilt) = &rebuilt {
self.metrics.record_gather(rebuilt.inner.data.len());
}
Ok(rebuilt)
}
/// Loads the data block to point-read for `needle`: for a columnar SST the
/// key-aware fast path that rebuilds only the matching key's rows; for a row
/// SST the whole block. `Ok(None)` means the block carries no row for `needle`
/// (columnar: absent / wholly deleted), so the caller moves on. The file is
/// taken through `fd`, the lookup's.
fn load_point_block(
&self,
handle: &BlockHandle,
needle: &[u8],
fd: &LookupFd,
) -> crate::Result<Option<DataBlock>> {
#[cfg(feature = "columnar")]
if self.metadata.columnar {
return self.load_columnar_point_block(handle, needle, fd);
}
let _ = needle;
self.load_data_block(handle, fd)
}
/// Loads a columnar data block and decodes only the projected columns of
/// the row pages `row_pages` selects, stepping over the rest without
/// reading them. The returned batches, one per row page read, carry the
/// requested columns for those rows. This is the projection read the
/// vectorized scan uses, distinct from the whole-block reconstruction that
/// the row read paths use. `whole` is [`PageWant::whole`].
///
/// [`PageWant::whole`]: crate::table::row_group::PageWant::whole
#[cfg(feature = "columnar")]
fn load_columnar_block_projected(
&self,
handle: &BlockHandle,
projection: &[u16],
row_pages: crate::table::row_group::RowPageSelect<'_>,
whole: bool,
) -> crate::Result<crate::table::row_group::RowPages> {
self.load_row_group(
handle,
&crate::table::row_group::PageWant {
columns: Some(projection),
row_pages,
whole,
},
ReadCharge::Foreground,
)
}
/// Returns the (possibly compressed) file size.
pub(crate) fn file_size(&self) -> u64 {
self.metadata.file_size
}
/// The bytes this VIEW can contribute to a merge output: the whole file for
/// a normal table, or `[punch_offset, end)` for a tight-space RESTRICTED
/// view. Its punched prefix is superseded — the merge never reads it and
/// never rewrites it — so charging a space gate the original `file_size`
/// stalls a compaction whose real output fits the headroom.
///
/// # Errors
///
/// Propagates a restricted view's punch-offset lookup.
#[cfg_attr(
not(feature = "std"),
allow(
dead_code,
reason = "merge-output sizing; its compaction consumer is std-gated, so unused under no_std"
)
)]
pub(crate) fn live_file_size(&self) -> crate::Result<u64> {
// The file's length: the recorded `file_size` leaves out the sections
// written after the metadata, which a rewrite writes too.
let size = self.file_len();
// The punch offset is a data-block offset inside this same file, so the
// subtraction cannot underflow; falling back to the whole size keeps a
// space gate conservative if the two ever disagreed.
Ok(size.checked_sub(self.punch_offset()?).unwrap_or(size))
}
/// The reads that consulted this table's data since it was written:
/// point reads that passed its filter, and range scans that reached it.
pub(crate) fn reads(&self) -> u64 {
self.point_reads() + self.scans()
}
/// How hot the table is now: its [`Self::reads`] per thousand seconds
/// since it began counting them, so a table read long ago does not
/// outrank one read often since it was written.
pub(crate) fn heat(&self) -> u64 {
let now = crate::time::unix_timestamp().as_secs();
// A clock stepped back, or no clock at all under `no_std`, reads as
// the table's first second: its reads rank as they stand. Floored at
// zero by design, since an age is never negative.
let age = now.saturating_sub(self.counting_since_secs).max(1);
let per_kilosecond = u128::from(self.reads()) * 1_000 / u128::from(age);
// Below `reads() * 1000`, far within `u64` for any count of reads.
u64::try_from(per_kilosecond).unwrap_or(u64::MAX)
}
/// The point reads that passed this table's filter since it was written.
pub(crate) fn point_reads(&self) -> u64 {
self.read_count.load(core::sync::atomic::Ordering::Relaxed)
}
/// The range scans that reached this table since it was written.
pub(crate) fn scans(&self) -> u64 {
self.scan_count.load(core::sync::atomic::Ordering::Relaxed)
}
/// Records a range scan reaching this table: one `Relaxed` bump per scan
/// and table, beside the scan's own reads of the table.
pub(crate) fn note_scan(&self) {
self.scan_count
.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
}
/// The bytes the file holds: what a whole read of it takes, and what
/// writing it took.
pub(crate) fn file_len(&self) -> u64 {
self.file_len
}
/// The bytes of the data section: every data block or row group as it lies
/// on disk. It opens the file, and the index follows it, or the top-level
/// index where the index is not partitioned.
pub(crate) fn data_section_len(&self) -> u64 {
self.regions.index.unwrap_or(self.regions.tli).offset().0
}
/// The blocks one data index entry covers, each with the ROLE its writer
/// sealed it under.
///
/// A row-major table's entry is one [`BlockType::Data`] block and costs no
/// read. A columnar table's entry is a row group: its
/// [`BlockType::ColumnPageDirectory`] followed by its
/// [`BlockType::ColumnPage`]s, found by reading the directory straight
/// from disk, bypassing the cache — the walks that call this (scrub, heal,
/// the reconcile gates) are judging the bytes on disk, and a cached
/// directory is a statement about bytes that may since have changed.
///
/// The roles are the swap-defence those walks check each block against,
/// so they come from each block's POSITION in the group, never from the
/// block's own header: a header is exactly what a misdirected block would
/// carry.
///
/// # Errors
///
/// When the directory cannot be read or does not fill the group exactly.
/// Without it the pages cannot be located, so the whole entry is reported
/// rather than guessed at.
#[cfg(feature = "std")]
pub(crate) fn data_unit_blocks(
&self,
entry: &BlockHandle,
) -> crate::Result<alloc::vec::Vec<(BlockHandle, BlockType)>> {
if !self.metadata.columnar {
return Ok(alloc::vec![(*entry, BlockType::Data)]);
}
#[cfg(feature = "columnar")]
{
self.row_group_block_handles(entry)
}
#[cfg(not(feature = "columnar"))]
{
Err(crate::Error::FeatureUnsupported("columnar"))
}
}
/// The handles of a row group's directory and pages, read from disk.
/// See [`Self::data_unit_blocks`].
#[cfg(all(feature = "std", feature = "columnar"))]
fn row_group_block_handles(
&self,
group: &BlockHandle,
) -> crate::Result<alloc::vec::Vec<(BlockHandle, BlockType)>> {
let (fd, _) = self
.file_accessor
.get_or_open_table(&self.global_id(), &self.path)?;
let directory_len = crate::table::row_group::indexed_directory_len(group)?;
let directory_handle = BlockHandle::new(group.offset(), directory_len);
let directory_block = Block::from_file(
fd.as_ref(),
directory_handle,
crate::table::block::BlockIdentity {
table_id: self.id(),
block_type: BlockType::ColumnPageDirectory,
dict_id: 0,
window_log: 0,
},
&crate::table::util::build_block_transform(
CompressionType::None,
self.encryption.as_deref(),
self.metadata.ecc_params,
#[cfg(zstd_any)]
None,
)?,
)?;
if directory_block.header.block_type != BlockType::ColumnPageDirectory {
return Err(crate::Error::InvalidTag((
"BlockType",
directory_block.header.block_type.into(),
)));
}
crate::table::row_group::check_directory_len(
directory_len,
directory_block
.header
.on_disk_size_with(self.metadata.ecc_params),
)?;
let directory = crate::table::column_page::PageDirectory::decode(&directory_block.data)?;
crate::table::row_group::check_group_extent(group, directory_len, &directory)?;
crate::table::row_group::check_head_zones_len(group, &directory)?;
// Verification, salvage and scrub take the group's blocks from here, so
// another group's, in this one's place, is refused before it can be
// certified or copied as this group's.
crate::table::row_group::check_group_tag(group.group_tag(), &directory)?;
// The blocks in the order they lie: the directory, the head zone
// block, the pages, the other zone blocks.
let mut blocks = alloc::vec::Vec::with_capacity(directory.entries().len() + 2);
blocks.push((directory_handle, BlockType::ColumnPageDirectory));
let after_directory = *group.offset() + u64::from(directory_len);
if let Some(head) = directory.head_zone_block() {
blocks.push((
BlockHandle::new(
crate::table::block::BlockOffset(after_directory),
head.length,
),
BlockType::ColumnZones,
));
}
let pages_at = after_directory + u64::from(directory.pages_start());
for page in directory.entries() {
let offset = pages_at + u64::from(page.offset);
blocks.push((
BlockHandle::new(crate::table::block::BlockOffset(offset), page.length),
BlockType::ColumnPage,
));
}
let mut offset = pages_at + u64::from(directory.pages_len());
for zone_block in directory.zone_blocks() {
blocks.push((
BlockHandle::new(crate::table::block::BlockOffset(offset), zone_block.length),
BlockType::ColumnZones,
));
offset += u64::from(zone_block.length);
}
Ok(blocks)
}
/// Patrol-scrubs every data block of this table: a cache-bypassing read that
/// runs the Page-ECC verify+correct path, recording a heal hint (when
/// `auto_heal` is on) on a confirmed-persistent correction.
///
/// Returns a partial [`PatrolScrubReport`](crate::scrub::PatrolScrubReport)
/// for this SST (`sst_files_scanned == 1`) so the caller can merge it across
/// the tree. Always runs to completion: an uncorrectable / unreadable block
/// is recorded (and logged), not silently skipped, and the next block is
/// still scrubbed. A block-index walk failure stops this table early (later
/// offsets are untrustworthy) but other tables still scrub.
#[cfg(feature = "std")]
pub(crate) fn scrub_data_blocks(&self) -> crate::scrub::PatrolScrubReport {
use crate::scrub::{PatrolScrubReport, ScrubError};
use crate::table::util::{BlockScrubOutcome, scrub_block};
let mut report = PatrolScrubReport {
sst_files_scanned: 1,
..PatrolScrubReport::default()
};
for entry in self.untraced_index_walk() {
let keyed = match entry {
Ok(h) => h,
Err(e) => {
// A structural index error means later offsets can't be
// trusted — stop this table, record it, let others run.
log::error!(
"patrol scrub: block index of table {} at {} unreadable: {e:?}",
self.id(),
self.path.display(),
);
report.errors.push(ScrubError::BlockIndexUnreadable {
table_id: self.id(),
path: self.path.to_path_buf(),
reason: alloc::format!("{e:?}"),
});
break;
}
};
// Tight-space restriction: skip a block whose last key is below the
// bound: it sits in the punched-out prefix a superseding output
// table now owns, so scrubbing its reclaimed bytes would report
// spurious corruption for a restricted view (see `Table::range`).
if let Some(bound) = &self.1
&& self.comparator.compare(keyed.end_key(), bound) == core::cmp::Ordering::Less
{
continue;
}
let entry_handle = *keyed.as_ref();
let blocks = match self.data_unit_blocks(&entry_handle) {
Ok(blocks) => blocks,
// A row group whose directory cannot be read has no known
// pages: the entry is reported as one uncorrectable block at
// the group's offset rather than skipped, so a scan never ends
// clean over bytes it could not look at.
Err(e) => {
report.blocks_scanned += 1;
self.record_uncorrectable(&mut report, keyed.offset().0, &e);
continue;
}
};
for (handle, role) in blocks {
report.blocks_scanned += 1;
match scrub_block(
self.global_id(),
&self.path,
&self.file_accessor,
&handle,
role,
self.compression_for_role(role),
self.encryption.as_deref(),
self.metadata.ecc_params,
#[cfg(zstd_any)]
self.dictionary_for_role(role),
self.heal_hints.get().map(AsRef::as_ref),
#[cfg(feature = "metrics")]
&self.metrics,
) {
Ok(BlockScrubOutcome::Clean) => {}
Ok(BlockScrubOutcome::Corrected { scheduled }) => {
report.corrections_applied += 1;
if scheduled {
// heal_hints dedups per SST, so `scheduled` is true at
// most once per table — this counts distinct SSTs.
report.ssts_scheduled_for_rewrite += 1;
}
}
Err(e) => self.record_uncorrectable(&mut report, handle.offset().0, &e),
}
}
}
report
}
/// Records one block the patrol scrub could not verify or correct.
#[cfg(feature = "std")]
fn record_uncorrectable(
&self,
report: &mut crate::scrub::PatrolScrubReport,
block_offset: u64,
e: &crate::Error,
) {
report.uncorrectable_blocks += 1;
log::error!(
"patrol scrub: uncorrectable block at offset {block_offset} in table {} at {}: {e:?}",
self.id(),
self.path.display(),
);
report
.errors
.push(crate::scrub::ScrubError::UncorrectableBlock {
table_id: self.id(),
path: self.path.to_path_buf(),
block_offset,
reason: alloc::format!("{e:?}"),
});
}
/// The codec a data-carrying block of `role` was written under: a row
/// group's directory and zone block are always plain, everything else
/// uses the table's data codec.
fn compression_for_role(&self, role: BlockType) -> CompressionType {
if matches!(
role,
BlockType::ColumnPageDirectory | BlockType::ColumnZones
) {
CompressionType::None
} else {
self.metadata.data_block_compression
}
}
/// The zstd dictionary a data-carrying block of `role` needs, if any: none
/// for a directory or a zone block, which are never compressed.
#[cfg(zstd_any)]
fn dictionary_for_role(&self, role: BlockType) -> Option<&crate::compression::ZstdDictionary> {
if matches!(
role,
BlockType::ColumnPageDirectory | BlockType::ColumnZones
) {
None
} else {
self.zstd_dictionary.as_deref()
}
}
/// Ensures the heal's handle may be WRITTEN through: probes the link
/// count on first use and detaches a multiply-linked inode onto a
/// private copy via [`Self::unshare_for_heal`]. The outcome is cached in
/// `state`, so the probe and copy run at most once per scan and only
/// when a write is actually needed (a clean scan never detaches a
/// checkpoint link). A failed link-count query is treated as shared
/// (fail closed) — the copy path is safe either way.
///
/// # Errors
///
/// The unshare failure reason when the write must NOT happen (the inode
/// may still be shared); repeated calls keep returning it.
#[cfg(feature = "page_ecc")]
fn ensure_unshared_for_write(
&self,
file: &mut Box<dyn crate::fs::FsFile>,
state: &mut UnshareState,
sync_mode: crate::fs::SyncMode,
) -> Result<(), alloc::string::String> {
match state {
UnshareState::Ready => Ok(()),
UnshareState::Failed(reason) => Err(reason.clone()),
UnshareState::Unprobed => {
let shared = match file.hard_link_count() {
Ok(n) => n > 1,
Err(e) => {
log::warn!(
"in-place heal: link-count query failed for table {} at {}: {e}; \
assuming the inode is shared",
self.id(),
self.path.display(),
);
true
}
};
if shared {
// A pinned FD (no descriptor cache) is bound to its
// inode for the table's lifetime: after the unshare's
// copy + rename, every later read, scrub probe, and
// digest check would keep resolving the DEAD inode
// while the manifest points at the healed live path.
// Refuse the detach — the blocked write-backs surface
// as findings and every link stays byte-identical.
if !self.file_accessor.can_retarget() {
let reason = alloc::string::String::from(
"the inode is multiply linked and the pinned file \
descriptor cannot be retargeted at a detached copy; \
refusing the in-place write",
);
*state = UnshareState::Failed(reason.clone());
return Err(reason);
}
match self.unshare_for_heal(file.as_ref(), sync_mode) {
Ok(fresh) => *file = fresh,
Err(reason) => {
*state = UnshareState::Failed(reason.clone());
return Err(reason);
}
}
}
*state = UnshareState::Ready;
Ok(())
}
}
}
/// Detaches this table's live path from a multiply-linked inode so an
/// in-place heal can write without touching the other links (checkpoint
/// snapshots): streams the file into a sibling `*.healtmp-{n}` copy, syncs
/// it, and atomically renames it over the live path. The returned handle
/// is the copy's (still valid after the rename), open read+write.
///
/// The temp name carries a process-wide sequence number so two concurrent
/// heal scans of the same table can never remove or rename each other's
/// in-progress copy (cross-process exclusion comes from the directory
/// lock). Every pre-rename failure removes the copy before returning —
/// recovery refuses to open a tree whose `tables/` holds a file it cannot
/// parse as a table id, so an abandoned artifact must never outlive the
/// heal (recovery still sweeps `*.healtmp-*` left by a hard crash).
#[cfg(feature = "page_ecc")]
pub(crate) fn unshare_for_heal(
&self,
source: &dyn crate::fs::FsFile,
sync_mode: crate::fs::SyncMode,
) -> Result<Box<dyn crate::fs::FsFile>, alloc::string::String> {
use std::io::{Seek, SeekFrom, Write};
static HEAL_TMP_SEQ: core::sync::atomic::AtomicU64 = core::sync::atomic::AtomicU64::new(0);
let len = source
.metadata()
.map_err(|e| alloc::format!("metadata: {e}"))?
.len;
// A tight-space-restricted view has reclaimed the DATA blocks below its
// frontier. The frontier lies inside the `data` section (index / filter /
// meta sections all sit past the data region), and `Inner::drop` punches
// each data block INDIVIDUALLY, top-down, stopping at the first failure —
// so the prefix may be zeroed only partially. Reproduce the punched
// extents as HOLES in the heal copy — probing each one, not assuming it —
// instead of materializing the zeros (which would re-allocate the
// reclaimed space, an ENOSPC risk on the near-full disk tight-space runs
// on) or blindly zeroing the whole prefix (which would discard the
// intact bytes an incomplete punch left behind). `punch` is `0` for a
// normal (unrestricted) table, so its copy is byte-for-byte as before.
let punch = self
.punch_offset()
.map_err(|e| alloc::format!("punch offset: {e}"))?;
let sparse = punch > 0 && self.fs.capabilities(&self.path).punch_hole;
let seq = HEAL_TMP_SEQ.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
let tmp_path = self.path.with_extension(alloc::format!("healtmp-{seq}"));
let mut tmp = self
.fs
.open(
&tmp_path,
&FsOpenOptions::new()
.read(true)
.write(true)
.create_new(true)
.write_origin(crate::fs::WriteOrigin::Repair),
)
.map_err(|e| alloc::format!("create heal copy: {e}"))?;
// Any failure past this point must take the copy with it (see above).
let mut copy = || -> Result<(), alloc::string::String> {
// The reclaimed DATA-block extents below the frontier — exactly what
// tight-space punched (via `Inner::drop`). The block index yields the
// data-block handles, so the copied complement keeps everything the
// punch never touches (the straddling block and every later section).
//
// Each candidate extent is PROBED for actual zeros rather than assumed
// punched from the logical bound alone: a slice that committed but
// failed its restriction-sidecar write deliberately leaves the input
// UNPUNCHED (punching without the sidecar would force a lossy
// conservative bound on a later manifest-loss repair), and the heal
// copy must not introduce holes that state never had. An intact
// sub-bound block reads non-zero (its header magic alone guarantees
// it) and is copied verbatim; a genuinely punched extent reads as
// zeros and stays a hole.
let mut holes: alloc::vec::Vec<(u64, u64)> = alloc::vec::Vec::new();
if sparse {
let mut probe = alloc::vec::Vec::new();
for handle in self.maintenance_index_walk() {
let handle = handle.map_err(|e| alloc::format!("block index iter: {e}"))?;
let block_off = handle.offset().0;
let block_len = u64::from(handle.size());
if block_off >= punch {
continue;
}
#[expect(
clippy::cast_possible_truncation,
reason = "a block's on-disk size is a u32, which fits usize"
)]
{
probe.resize(block_len as usize, 0u8);
}
let n = source
.read_at(&mut probe, block_off)
.map_err(|e| alloc::format!("probe block at {block_off}: {e}"))?;
if n < probe.len() {
return Err(alloc::format!(
"probe block at {block_off}: short read ({n} of {} bytes)",
probe.len(),
));
}
if probe.iter().all(|&b| b == 0) {
holes.push((block_off, block_len));
}
}
holes.sort_unstable();
// Adjacent holes are one extent: one punch call each below.
holes.dedup_by(|next, run| {
let adjacent = run.0 + run.1 == next.0;
if adjacent {
run.1 += next.1;
}
adjacent
});
// Establish the full size so the un-written data-block extents stay
// sparse holes.
tmp.set_len(len)
.map_err(|e| alloc::format!("set heal copy length: {e}"))?;
}
// Live ranges to stream = the complement of the data-block holes across
// `[0, len)`. With no holes (a normal table) this is the whole file.
let mut live: alloc::vec::Vec<(u64, u64)> = alloc::vec::Vec::new();
let mut cursor = 0u64;
for &(h_off, h_len) in &holes {
if h_off > cursor {
live.push((cursor, h_off));
}
cursor = cursor.max(h_off + h_len);
}
if cursor < len {
live.push((cursor, len));
}
let mut buf = alloc::vec![0u8; 1 << 20];
for &(start, end) in &live {
tmp.seek(SeekFrom::Start(start))
.map_err(|e| alloc::format!("seek heal copy to {start}: {e}"))?;
let mut off = start;
while off < end {
#[expect(
clippy::cast_possible_truncation,
reason = "the u64 min() is taken first, so the value is bounded by buf.len()"
)]
let want = (end - off).min(buf.len() as u64) as usize;
let Some(chunk) = buf.get_mut(..want) else {
return Err(alloc::string::String::from("chunk within buffer"));
};
let n = source
.read_at(chunk, off)
.map_err(|e| alloc::format!("read source at {off}: {e}"))?;
if n < want {
// Fill-or-EOF contract: a short read here means the file
// shrank underneath us — abort rather than install a
// truncated copy.
return Err(alloc::format!(
"short read at {off}: got {n} of {want} bytes"
));
}
tmp.write_all(chunk)
.map_err(|e| alloc::format!("write heal copy at {off}: {e}"))?;
off += want as u64;
}
}
// Deallocate the data-block holes even where `set_len` zero-allocated, so
// the reclaimed space is not silently re-consumed by the heal copy.
for &(h_off, h_len) in &holes {
self.fs
.punch_hole(&tmp_path, h_off, h_len)
.map_err(|e| alloc::format!("punch heal copy data block at {h_off}: {e}"))?;
}
// sync_all, not sync_data: the copy is a NEW file, its size must
// be durable before the rename publishes it. Mode-aware: the
// caller selected the tree's durability (Normal skips the macOS
// F_FULLFSYNC hardware barrier, same as the flush path).
tmp.sync_all_with(sync_mode)
.map_err(|e| alloc::format!("sync heal copy: {e}"))?;
// Replaces the live path on every platform, open handles included
// — see the `Fs::rename` contract.
self.fs
.rename(&tmp_path, &self.path)
.map_err(|e| alloc::format!("rename heal copy into place: {e}"))
};
if let Err(reason) = copy() {
if let Err(e) = self.fs.remove_file(&tmp_path) {
log::warn!(
"failed to remove abandoned heal copy {}: {e}",
tmp_path.display(),
);
}
return Err(reason);
}
// The rename has replaced the live path, so the descriptor cache now
// holds the OLD inode's fd: a later heal (or read) resolving through
// it would scrub the detached inode while writes target the live one,
// misreporting a live-only fault as an unexplained (uncorrectable)
// mismatch. Drop the stale entry IMMEDIATELY — before the durability
// sync below, whose failure must not leave the cache pinned to the
// dead inode — so the next access reopens the live path.
self.file_accessor.remove_for_table(&self.global_id());
if let Some(parent) = self.path.parent() {
self.fs
.sync_directory_with(parent, sync_mode)
.map_err(|e| alloc::format!("sync directory after rename: {e}"))?;
}
// The handle survives the rename (same inode, now the live path).
Ok(tmp)
}
/// In-place ECC autoheal: like [`Self::scrub_data_blocks`], but PERSISTS each
/// correction by writing the corrected block back at its existing offset
/// (size-preserving) instead of scheduling a full-file healing rewrite.
/// Healthy blocks are never rewritten, so the cost is O(damage), not O(file).
///
/// Opens the SST read+write through the tree's `Fs` (bypassing the block
/// cache, like scrub), and for each data block that fails its checksum but
/// Page-ECC recovers it, writes back `header ++ recovered_data ++ recomputed
/// parity` and `sync_data`s it before moving on, so a crash mid-heal leaves
/// the block in its prior, still-RS-correctable state. Uncorrectable /
/// unreadable blocks are recorded as findings and left for block salvage.
///
/// HARD-LINK SAFE by unsharing: checkpoints hard-link SSTs, and a
/// checkpoint's manifest records the digest of the bytes AT SNAPSHOT TIME,
/// which the checkpoint (immutable by design) can never reconcile the way
/// the live tree does. Writing through a shared inode would therefore
/// desynchronize the snapshot from its own manifest whenever the recorded
/// digest is not the original file's (a manifest rebuilt over rotted
/// bytes). Right before the FIRST write-back (lazily — a clean scan of a
/// linked healthy table performs zero writes and keeps the checkpoint's
/// disk sharing), the link count is probed and a multiply-linked live
/// path is detached onto a private copy (copy + atomic rename), which is
/// then healed, leaving every other link byte-identical to what its
/// manifest describes. Cached read-only descriptors may keep serving the
/// old inode until they are reopened; its bytes are unchanged, so reads
/// stay correct (ECC-corrected on the fly as before).
///
/// Returns the scrub report plus an ATTRIBUTION flag: `true` when the
/// file's digest was computed right before this pass's FIRST write and it
/// matched the manifest digest — every byte the file now differs from
/// the manifest state by is provably one of this pass's verified
/// corrections. The digest reconciliation uses it to decide whether a
/// post-heal mismatch may cover sections that cannot be semantically
/// authenticated (deletion metadata). `false` whenever nothing was
/// written, the pre-write digest could not be computed, or it already
/// disagreed with the manifest.
///
/// `pub(crate)` for [`crate::scrub::patrol_scrub`] (heal-in-place enabled).
/// `sync_mode` is the tree's configured durability
/// ([`Config::sync_mode`](crate::config::Config::sync_mode)): every
/// write-back, the unshare copy, and the post-rename directory sync
/// honor it, so a patrol with many corrections does not pay a macOS
/// `F_FULLFSYNC` hardware barrier per block when the caller selected
/// [`SyncMode::Normal`](crate::fs::SyncMode::Normal).
///
/// `manifest_checksum` is the table's CURRENT manifest digest, read by
/// the caller under the per-table heal lock: a concurrent patrol may
/// have refreshed the manifest after this view was captured, and the
/// attribution probe must compare against what the manifest says NOW.
#[cfg(feature = "page_ecc")]
pub(crate) fn heal_data_blocks_in_place(
&self,
sync_mode: crate::fs::SyncMode,
manifest_checksum: Checksum,
) -> (crate::scrub::PatrolScrubReport, bool) {
use crate::scrub::{PatrolScrubReport, ScrubError};
use std::io::{Seek, SeekFrom, Write};
let mut report = PatrolScrubReport {
sst_files_scanned: 1,
..PatrolScrubReport::default()
};
// Whether any write-back was ATTEMPTED. A write_all that errors after a
// partial write, or a full write whose sync then fails, may already have
// changed the on-disk bytes even though `blocks_healed_in_place` stays 0,
// so the marker (the only attribution for a later digest refresh) must be
// KEPT in that case; it is removed only when no mutation ever began.
let mut write_attempted = false;
// NEITHER exclusion is taken here — both are held by the patrol
// scrub across this scan AND the digest reconciliation that follows
// it, because re-acquiring either inside would deadlock:
// - the per-table `heal_lock` (a Mutex) serializes same-table heals
// for the whole scan-to-reconcile span, so one patrol cannot
// install a digest computed before another patrol's heal;
// - the `DeletionPause` mutation window excludes a checkpoint's link
// pass for the same span, so a checkpoint cannot link healed bytes
// under a stale digest.
// A single read+write handle for both the recovery read and the
// write-back, opened directly (not via the read-only descriptor cache) so
// the heal sees the medium and can mutate it.
let mut file = match self.fs.open(
&self.path,
&FsOpenOptions::new()
.read(true)
.write(true)
.write_origin(crate::fs::WriteOrigin::Repair),
) {
Ok(f) => f,
Err(e) => {
// Cannot persist corrections (a read-only replica, restrictive
// permissions) — but the table must still get its integrity
// CHECKED. Record the failed open, then fall back to the
// read-only scrub so corruption still surfaces instead of
// returning a healthy-looking report with zero blocks scanned.
report.errors.push(ScrubError::BlockIndexUnreadable {
table_id: self.id(),
path: self.path.to_path_buf(),
reason: alloc::format!("open read+write for heal: {e}"),
});
report.merge(self.scrub_data_blocks());
// Both passes stamped this SST as scanned; it is one file.
report.sst_files_scanned = 1;
return (report, false);
}
};
// A multiply-linked inode (a checkpoint shares it) must not be healed
// through — but detaching costs a full-file copy and permanently
// ends the checkpoint's disk sharing, so it happens LAZILY: the link
// count is probed (and a shared inode detached onto a private copy)
// only right before the FIRST write-back. A clean scan of a linked
// healthy table therefore stays O(damage) = zero writes. The probe
// stays honest under the caller's mutation window: a checkpoint
// cannot link this SST anywhere inside the scan-to-write span.
let mut unshare_state = UnshareState::Unprobed;
let transform = match crate::table::util::build_block_transform(
self.metadata.data_block_compression,
self.encryption.as_deref(),
self.metadata.ecc_params,
#[cfg(zstd_any)]
self.zstd_dictionary.as_deref(),
) {
Ok(t) => t,
Err(e) => {
report.errors.push(ScrubError::BlockIndexUnreadable {
table_id: self.id(),
path: self.path.to_path_buf(),
reason: alloc::format!("build transform for heal: {e:?}"),
});
return (report, false);
}
};
// Attribution + crash-recovery marker, computed UP FRONT while the file
// still holds its pre-heal bytes. The heal is deterministic, so the
// digest of the file with every correction applied is known before any
// write lands: bind THAT into the attestation (not just `pre ==
// manifest`), so a marker a crash leaves behind can only ever
// re-authorize the exact healed bytes, never an unrelated later forge.
// A clean scan (no corrections) writes nothing. `pre_heal_matched`
// still drives the returned attribution flag and the zero-heal marker
// cleanup below; the per-block correction is recomputed identically in
// the write loop (both call `heal_correction_for_block`).
// A restricted view predicts (and later digests) only its live suffix;
// its corrections all lie there, and the punched prefix is not hashed.
// A transient failure resolving that bound must ABORT, not fall back to
// offset 0: predicting (and attesting) over the WHOLE physical file while
// the manifest and reconciliation hash only the suffix would, after a
// crash mid-heal, leave a completed marker whose digest can never match
// the suffix — permanently stranding the healed table.
let heal_start = match self.restrict_lower_bound() {
Some(bound) => match self.punch_offset_for(bound) {
Ok(off) => off,
Err(e) => {
report.errors.push(ScrubError::ChecksumRefreshFailed {
table_id: self.id(),
path: self.path.to_path_buf(),
reason: alloc::format!(
"could not resolve the restricted heal start; heal skipped to \
keep the table reconcilable: {e:?}"
),
});
return (report, false);
}
},
None => 0,
};
// Predict the post-heal digest AND the exact set of offsets the heal
// will touch, streaming so a broadly damaged table does not materialize
// every corrected frame at once. The write loop below applies ONLY these
// offsets, so a fault appearing after this pass is never healed under a
// digest that did not attest it.
let (predicted_digest, predicted_offsets) =
match self.predict_heal_digest_and_offsets(file.as_ref(), &transform, heal_start) {
Ok(pair) => pair,
Err(e) => {
report.errors.push(ScrubError::ChecksumRefreshFailed {
table_id: self.id(),
path: self.path.to_path_buf(),
reason: alloc::format!(
"could not predict the post-heal digest; heal skipped to keep the \
table reconcilable: {e:?}"
),
});
return (report, false);
}
};
// The pre-heal digest probe result: `None` when there is nothing to
// heal; otherwise `Some(matched)` where `matched` says whether the
// current bytes still match the manifest (the ATTRIBUTABLE path). A
// probe FAILURE aborts before the first write: writing corrections with
// no completed marker can permanently strand a table whose manifest
// legitimately described the pre-heal bytes.
let pre_heal_matched: Option<bool> = if predicted_offsets.is_empty() {
None
} else {
let matched = match self.pre_heal_digest_matches(manifest_checksum) {
Ok(matched) => matched,
Err(e) => {
report.errors.push(ScrubError::ChecksumRefreshFailed {
table_id: self.id(),
path: self.path.to_path_buf(),
reason: alloc::format!(
"pre-heal digest probe failed; heal skipped to keep the \
table reconcilable: {e:?}"
),
});
return (report, false);
}
};
if matched {
// ATTRIBUTABLE path: the heal is about to change bytes the
// manifest digest still matches, so the crash-recovery marker
// MUST be durable BEFORE the first mutation. If the post-heal
// digest cannot be predicted, or the attestation cannot be
// persisted, do NOT heal: a crash after a corrected block syncs
// but before the in-process manifest refresh would leave healed
// bytes under the stale digest with no marker, and fail-closed
// reconciliation rejects that mismatch forever, permanently
// stranding a table that was reconcilable a moment earlier.
// Leaving the block corrupt keeps the table reconcilable; the
// next patrol retries once the marker can be written. (A
// non-crash run also reconciles directly, but the marker is the
// ONLY thing that survives a crash, so its durability gates the
// mutation.)
if let Err(e) = crate::scrub::heal_attest::write(
&*self.fs,
&self.path,
self.encryption.as_deref(),
self.id(),
manifest_checksum,
Checksum::from_raw(predicted_digest),
) {
report.errors.push(ScrubError::ChecksumRefreshFailed {
table_id: self.id(),
path: self.path.to_path_buf(),
reason: alloc::format!(
"could not persist the heal attestation; heal skipped to keep the \
table reconcilable: {e}"
),
});
return (report, false);
}
Some(true)
} else {
// NOT-matched pre-heal: the current bytes already differ from the
// manifest digest. If the predicted heal RESTORES the manifest
// digest (the common restorative case: a rotted file healing back to
// exactly what the manifest describes), the write window STILL needs
// a durable marker BEFORE the first mutation. A crash after syncing
// SOME of several corrections leaves the file matching neither the
// manifest nor the predicted digest; with no marker a checkpoint
// hard-links those intermediate bytes under the stale manifest
// digest, producing a permanently inconsistent checkpoint. The
// marker records `(manifest, predicted)` (here equal), so a reconcile
// trusts the file only once it hashes to the restored digest. (The
// DIVERGING sub-case, predicted != manifest, is gated separately
// below against an EXISTING completed marker via `attests_post`.)
if Checksum::from_raw(predicted_digest) == manifest_checksum
&& let Err(e) = crate::scrub::heal_attest::write(
&*self.fs,
&self.path,
self.encryption.as_deref(),
self.id(),
manifest_checksum,
Checksum::from_raw(predicted_digest),
)
{
report.errors.push(ScrubError::ChecksumRefreshFailed {
table_id: self.id(),
path: self.path.to_path_buf(),
reason: alloc::format!(
"could not persist the restorative heal attestation; heal \
skipped to keep the table reconcilable: {e}"
),
});
return (report, false);
}
Some(false)
}
};
// A DIVERGING heal on a stale-manifest file must NOT proceed: when the
// current bytes do not match the manifest AND the predicted post-heal
// digest would not match it either, the file is drifting away from the
// manifest (e.g. a prior heal crashed after writing its completed
// attestation but before refreshing the manifest, then a fresh fault
// appeared). Writing these corrections would move the bytes off any
// existing attestation's `post` digest with no chaining marker, so the
// reconcile that runs right after this returns could not attribute the
// result and would strip the marker — leaving future scrubs and
// checkpoints unable to reconcile the table. Leave it untouched (fail
// closed): the reconcile still reconciles an existing marker against the
// manifest, and a later patrol heals the fault once the bytes line up.
//
// A not-matched file whose predicted heal RESTORES the manifest digest
// (a plain rotted file healing back to what the manifest describes), or
// restores a digest an existing COMPLETED marker already attests (a fresh
// fault on the just-healed bytes, which that marker still reconciles), is
// NOT diverging, so it proceeds. `None` (nothing to heal) is unaffected.
if pre_heal_matched == Some(false)
&& Checksum::from_raw(predicted_digest) != manifest_checksum
{
use crate::scrub::heal_attest::AttestResult;
match crate::scrub::heal_attest::attests_post(
&*self.fs,
&self.path,
self.encryption.as_deref(),
self.id(),
Checksum::from_raw(predicted_digest),
) {
// An existing COMPLETED marker already attests the predicted post:
// the heal restores an attested digest, not a divergence — proceed.
AttestResult::Attests => {}
// No attesting marker: healing would drift the file off any
// marker's post with no chaining attribution, so the reconcile
// could not attribute it and would strip the marker. Fail closed.
AttestResult::Absent => return (report, false),
// The attestation probe hit a TRANSIENT read. Collapsing that to
// "does not attest" (the old behavior) would skip the heal, leaving
// the file diverged from a marker that MAY attest the predicted
// post; the reconcile then rereads the now-readable marker, finds
// it no longer matches the current bytes, and deletes it — stranding
// the table. Record a finding so the report is non-clean (a
// checkpoint's `reconcile_pending_heals` then aborts instead of
// removing) and skip; the next patrol retries once the probe reads
// cleanly and the marker's verdict is conclusive.
AttestResult::Inconclusive => {
report.errors.push(ScrubError::ChecksumRefreshFailed {
table_id: self.id(),
path: self.path.to_path_buf(),
reason: "heal attestation probe was inconclusive (transient read); heal \
skipped to preserve the existing marker for the next patrol"
.to_string(),
});
return (report, false);
}
}
}
for entry in self.maintenance_index_walk() {
let keyed = match entry {
Ok(h) => h,
Err(e) => {
// A structural index error means later offsets can't be
// trusted — stop this table, record it.
report.errors.push(ScrubError::BlockIndexUnreadable {
table_id: self.id(),
path: self.path.to_path_buf(),
reason: alloc::format!("{e:?}"),
});
break;
}
};
// Tight-space restriction: a block whose last key is below the bound
// sits in the punched-out (reclaimed) prefix that a superseding
// output table now owns. Its bytes are gone, so reading it reports a
// spurious uncorrectable error that would suppress the digest refresh
// for a real correction in the LIVE suffix. Skip it: the read path
// clamps scans the same way (see `Table::range`).
if let Some(bound) = &self.1
&& self.comparator.compare(keyed.end_key(), bound) == core::cmp::Ordering::Less
{
continue;
}
let entry_handle = *keyed.as_ref();
let blocks = match self.data_unit_blocks(&entry_handle) {
Ok(blocks) => blocks,
// A row group whose directory cannot be read has no locatable
// pages: report the entry as one uncorrectable block rather
// than end the pass clean over bytes it could not look at.
Err(e) => {
report.blocks_scanned += 1;
report.uncorrectable_blocks += 1;
report.errors.push(ScrubError::UncorrectableBlock {
table_id: self.id(),
path: self.path.to_path_buf(),
block_offset: keyed.offset().0,
reason: alloc::format!("{e:?}"),
});
continue;
}
};
for (handle, role) in blocks {
let block_offset = handle.offset().0;
report.blocks_scanned += 1;
// Verify the block through the SAME full read the scrub path uses
// (checksum + decode + ECC recovery), not just a bare frame check.
// This detects a checksum-clean-but-undecodable block (e.g. a corrupt
// `uncompressed_length`) and a corrupt block in a non-ECC segment,
// reporting it as uncorrectable instead of silently clean. `heal_hints
// = None`: this path persists the correction IN PLACE, so it must not
// also queue a full-file healing rewrite. The metric is recorded inside
// `scrub_block`.
let outcome = crate::table::util::scrub_block(
self.global_id(),
&self.path,
&self.file_accessor,
&handle,
role,
self.compression_for_role(role),
self.encryption.as_deref(),
self.metadata.ecc_params,
#[cfg(zstd_any)]
self.dictionary_for_role(role),
None,
#[cfg(feature = "metrics")]
&self.metrics,
);
match outcome {
// Verified clean: nothing to persist.
// Verified clean — but a clean PAYLOAD checksum never
// validates the parity trailer (parity is only consulted on a
// mismatch), so rot confined to the trailer would silently
// leave dead ECC on disk: a later payload fault in this block
// could no longer be recovered. This pass holds the read+write
// handle and the payload is untouched, so a size-preserving
// trailer rebuild is exactly the heal it exists to perform.
Ok(crate::table::util::BlockScrubOutcome::Clean) => {
let raw = match crate::file::read_exact(
file.as_ref(),
block_offset,
handle.size() as usize,
) {
Ok(raw) => raw,
Err(e) => {
report.uncorrectable_blocks += 1;
report.errors.push(ScrubError::UncorrectableBlock {
table_id: self.id(),
path: self.path.to_path_buf(),
block_offset,
reason: alloc::format!(
"in-place heal: parity re-read failed: {e:?}"
),
});
continue;
}
};
use crate::coding::Decode;
let Ok(raw_header) =
crate::table::block::Header::decode_from(&mut &raw[..])
else {
// The scrub just read this frame cleanly; a header that
// no longer decodes is an inconsistency worth surfacing.
report.uncorrectable_blocks += 1;
report.errors.push(ScrubError::UncorrectableBlock {
table_id: self.id(),
path: self.path.to_path_buf(),
block_offset,
reason: alloc::string::String::from(
"in-place heal: block scrubbed clean but its header no \
longer decodes",
),
});
continue;
};
// The bytes examined below are this SECOND read, not the
// frame the scrub just verified: a transient fault or
// fresh rot between the reads would otherwise feed the
// parity comparison an unverified payload, and the
// rebuild arm would PERSIST parity computed over those
// corrupt bytes — turning a recoverable block into one
// whose ECC agrees with the corruption. Verify the
// re-read header's ROLE and its payload against the
// header checksum first; a mismatch is surfaced, never
// acted on.
if raw_header.block_type != role {
report.uncorrectable_blocks += 1;
report.errors.push(ScrubError::UncorrectableBlock {
table_id: self.id(),
path: self.path.to_path_buf(),
block_offset,
reason: alloc::string::String::from(
"in-place heal: block scrubbed clean but its re-read \
header carries a different block role",
),
});
continue;
}
let header_len =
crate::table::block::Header::header_len(raw_header.block_type);
// checked_add: a forged/rotted data_length can overflow
// the payload-end sum on 32-bit targets; overflow is an
// uncorrectable finding, not a panic.
let payload_ok = header_len
.checked_add(raw_header.data_length as usize)
.and_then(|payload_end| raw.get(header_len..payload_end))
.is_some_and(|payload| {
crate::hash::hash128(payload)
== raw_header
.payload_checksum(crate::table::block::ChecksumAt::block(
self.id(),
role,
block_offset,
))
.into_u128()
});
if !payload_ok {
report.uncorrectable_blocks += 1;
report.errors.push(ScrubError::UncorrectableBlock {
table_id: self.id(),
path: self.path.to_path_buf(),
block_offset,
reason: alloc::string::String::from(
"in-place heal: block scrubbed clean but its re-read \
payload does not match its checksum",
),
});
continue;
}
match self.raw_block_parity_delta(&raw, &raw_header) {
// Trailer matches (or no ECC): nothing to persist.
Ok(None) => {}
// Trailer rot: persist the freshly computed parity at
// its on-disk position (header + payload unchanged).
Ok(Some(fresh)) => {
let trailer_offset = block_offset
+ crate::table::block::Header::header_len(raw_header.block_type)
as u64
+ u64::from(raw_header.data_length);
// Apply ONLY corrections the prediction pass attested.
// A trailer rebuild that appears now but was not
// predicted (fresh rot between the two reads) is left
// as-is: healing it would put bytes on disk the
// marker's digest never covered, and a later
// checkpoint would snapshot them under the stale
// digest. It stays RS-correctable for the next patrol.
if !predicted_offsets.contains(&trailer_offset) {
continue;
}
// Attribution + the crash-recovery marker were
// captured UP FRONT (before this loop), so nothing is
// done here. First write: make sure no checkpoint link
// shares the inode (lazy detach).
if let Err(reason) = self.ensure_unshared_for_write(
&mut file,
&mut unshare_state,
sync_mode,
) {
report.uncorrectable_blocks += 1;
report.errors.push(ScrubError::UncorrectableBlock {
table_id: self.id(),
path: self.path.to_path_buf(),
block_offset,
reason: alloc::format!(
"unshare hard-linked SST for heal: {reason}"
),
});
continue;
}
let write_back = file
.seek(SeekFrom::Start(trailer_offset))
.and_then(|_| file.write_all(&fresh));
// The write_all may have written some bytes even if it
// then errored; mark the file as possibly mutated so a
// later failure does not drop the attestation.
write_attempted = true;
let durable = match write_back {
Ok(()) => file
.sync_data_with(sync_mode)
.map_err(|e| alloc::format!("sync: {e}")),
Err(e) => Err(alloc::format!("write: {e}")),
};
if let Err(reason) = durable {
report.uncorrectable_blocks += 1;
report.errors.push(ScrubError::UncorrectableBlock {
table_id: self.id(),
path: self.path.to_path_buf(),
block_offset,
reason: alloc::format!("in-place parity rebuild {reason}"),
});
continue;
}
report.blocks_healed_in_place += 1;
}
Err(()) => {
report.uncorrectable_blocks += 1;
report.errors.push(ScrubError::UncorrectableBlock {
table_id: self.id(),
path: self.path.to_path_buf(),
block_offset,
reason: alloc::string::String::from(
"in-place heal: parity trailer unverifiable on a \
checksum-clean block",
),
});
}
}
}
// Recovered from parity: persist the corrected frame in place.
Ok(crate::table::util::BlockScrubOutcome::Corrected { .. }) => {
// Apply ONLY corrections the prediction pass attested. A block
// that recovers now but was not in the predicted set (a fault
// that appeared after the prediction) is left corrupt: writing
// it would heal bytes the marker's digest never covered, so a
// later checkpoint could snapshot them under the stale digest.
// It stays RS-correctable, so the next patrol retries.
if !predicted_offsets.contains(&block_offset) {
continue;
}
let frame = match crate::table::block::Block::heal_frame(
file.as_ref(),
handle,
self.id(),
&transform,
) {
Ok(Some((frame, _kind))) => frame,
// The scrub read corrected a fault but the confirming
// re-read is already clean: a TRANSIENT fault the first
// read hit and the re-read did not. This mirrors the
// schedule path (`maybe_record_persistent_heal`), which
// treats a clean confirmation re-read as transient and
// does not act — there is nothing on disk to persist.
Ok(None) => {
log::debug!(
"in-place heal: transient correction on block at offset \
{block_offset} in table {} at {}; re-read clean, nothing to \
persist",
self.id(),
self.path.display(),
);
continue;
}
// A real read/decode error on the re-read: surface it
// rather than silently skip the write-back.
Err(e) => {
report.uncorrectable_blocks += 1;
report.errors.push(ScrubError::UncorrectableBlock {
table_id: self.id(),
path: self.path.to_path_buf(),
block_offset,
reason: alloc::format!(
"in-place heal: block scrubbed as corrected but the heal \
re-read failed: {e:?}"
),
});
continue;
}
};
// Attribution + the crash-recovery marker were captured UP
// FRONT (before this loop). First write: make sure no
// checkpoint link shares the inode (lazy detach).
if let Err(reason) =
self.ensure_unshared_for_write(&mut file, &mut unshare_state, sync_mode)
{
report.uncorrectable_blocks += 1;
report.errors.push(ScrubError::UncorrectableBlock {
table_id: self.id(),
path: self.path.to_path_buf(),
block_offset,
reason: alloc::format!(
"unshare hard-linked SST for heal: {reason}"
),
});
continue;
}
// Seek + write (std::io) and sync (crate::io) carry different
// error types, so each is handled separately; both render to
// text for the finding. `sync_data` (not `sync_all`): the file
// size is unchanged, so only the data needs flushing, and it
// must land before the next block so a crash leaves the block
// in its prior, still-RS-correctable state.
let write_back = file
.seek(SeekFrom::Start(block_offset))
.and_then(|_| file.write_all(&frame));
// The write_all may have written some bytes even if it then
// errored (a partial write); mark the file as possibly mutated
// so a later failure does not drop the attestation.
//
// Retaining the attestation across a failed sync is what lets
// a later patrol attribute these bytes — but those bytes are
// NOT durable yet, and that patrol may read them straight from
// the page cache and find the table clean. Recording their
// digest then would let a power loss discard the healed block
// while the manifest keeps the post-heal digest. The
// reconciliation therefore syncs the SST itself before
// refreshing (and refuses the refresh when that sync fails);
// see `crate::scrub`'s marker-based reconcile.
write_attempted = true;
let durable = match write_back {
Ok(()) => file
.sync_data_with(sync_mode)
.map_err(|e| alloc::format!("sync: {e}")),
Err(e) => Err(alloc::format!("write: {e}")),
};
if let Err(reason) = durable {
report.uncorrectable_blocks += 1;
log::error!(
"in-place heal: write-back failed for block at offset \
{block_offset} in table {} at {}: {reason}",
self.id(),
self.path.display(),
);
report.errors.push(ScrubError::UncorrectableBlock {
table_id: self.id(),
path: self.path.to_path_buf(),
block_offset,
reason: alloc::format!("in-place heal {reason}"),
});
continue;
}
report.corrections_applied += 1;
report.blocks_healed_in_place += 1;
}
Err(e) => {
report.uncorrectable_blocks += 1;
log::error!(
"in-place heal: uncorrectable block at offset {block_offset} in table \
{} at {}: {e:?}",
self.id(),
self.path.display(),
);
report.errors.push(ScrubError::UncorrectableBlock {
table_id: self.id(),
path: self.path.to_path_buf(),
block_offset,
reason: alloc::format!("{e:?}"),
});
}
}
}
}
// The in-progress marker is written speculatively before the first
// block write; if NO block was actually healed (every candidate turned
// out uncorrectable) AND no write was ever attempted, the file is
// unchanged and the marker attests to a heal that never happened. Remove
// it so it cannot later authorize an unrelated digest mismatch (its
// `pre == manifest` binding does not expire on its own). But when a
// write WAS attempted and then failed (a partial write, or a full write
// whose sync failed), the on-disk bytes may already differ from the
// manifest digest: KEEP the marker so a later patrol can still attribute
// and refresh the altered table rather than stranding it.
let healed = pre_heal_matched == Some(true);
if healed && report.blocks_healed_in_place == 0 && !write_attempted {
crate::scrub::heal_attest::remove(&*self.fs, &self.path);
}
(report, healed)
}
/// The correction the in-place heal would apply to one data block, computed
/// WITHOUT writing. [`Self::predict_heal_digest_and_offsets`] calls this to
/// PREDICT the post-heal digest and the offsets to touch; the write loop in
/// [`Self::heal_data_blocks_in_place`] performs the same decision inline and
/// applies it. The heal is deterministic (RS recovery / parity rebuild over
/// the same pre-heal bytes), so the prediction and the later application
/// are byte-identical. This MIRRORS the write loop's arms; a drift only
/// weakens crash recovery (a predicted digest that no longer matches the
/// applied bytes fails the marker CLOSED, never authorizing wrong bytes),
/// it is never a correctness hazard.
///
/// # Errors
///
/// Propagates a TRANSIENT read failure (the confirming block / frame re-read)
/// rather than mapping it to "no correction": a swallowed read would omit the
/// block from the prediction's offset set, and the write loop's gate would then
/// skip a correction it re-discovers, reporting a clean pass over known damage.
#[cfg(feature = "page_ecc")]
fn heal_correction_for_block(
&self,
file: &dyn crate::fs::FsFile,
handle: BlockHandle,
role: BlockType,
transform: &crate::table::block::BlockTransform<'_>,
) -> crate::Result<Option<(u64, alloc::vec::Vec<u8>)>> {
use crate::coding::Decode;
let block_offset = handle.offset().0;
let outcome = crate::table::util::scrub_block(
self.global_id(),
&self.path,
&self.file_accessor,
&handle,
role,
self.compression_for_role(role),
self.encryption.as_deref(),
self.metadata.ecc_params,
#[cfg(zstd_any)]
self.dictionary_for_role(role),
None,
#[cfg(feature = "metrics")]
&self.metrics,
);
match outcome {
Ok(crate::table::util::BlockScrubOutcome::Clean) => {
// A clean-payload block still needs a parity-trailer rebuild if its
// trailer rotted. The confirming re-read PROPAGATES on a transient
// failure (see the doc), but a decode / structural inconsistency
// leaves the bytes unchanged (`Ok(None)`), matching the write loop's
// report-and-skip.
let raw = crate::file::read_exact(file, block_offset, handle.size() as usize)?;
let Ok(raw_header) = crate::table::block::Header::decode_from(&mut &raw[..]) else {
return Ok(None);
};
if raw_header.block_type != role {
return Ok(None);
}
let header_len = crate::table::block::Header::header_len(raw_header.block_type);
let payload_ok = header_len
.checked_add(raw_header.data_length as usize)
.and_then(|payload_end| raw.get(header_len..payload_end))
.is_some_and(|payload| {
crate::hash::hash128(payload)
== raw_header
.payload_checksum(crate::table::block::ChecksumAt::block(
self.id(),
role,
block_offset,
))
.into_u128()
});
if !payload_ok {
return Ok(None);
}
match self.raw_block_parity_delta(&raw, &raw_header) {
Ok(Some(fresh)) => {
let trailer_offset = block_offset
+ crate::table::block::Header::header_len(raw_header.block_type) as u64
+ u64::from(raw_header.data_length);
Ok(Some((trailer_offset, fresh)))
}
// Trailer matches (no ECC / already fresh) or is
// unverifiable: nothing to persist.
Ok(None) | Err(()) => Ok(None),
}
}
Ok(crate::table::util::BlockScrubOutcome::Corrected { .. }) => {
match crate::table::block::Block::heal_frame(file, handle, self.id(), transform) {
Ok(Some((frame, _kind))) => Ok(Some((block_offset, frame))),
// A confirming re-read that is now clean: a transient fault the
// first read hit and this one did not, so nothing to persist.
Ok(None) => Ok(None),
// A real read / decode error on the confirming read PROPAGATES:
// mapping it to "no correction" would let the write loop's gate
// skip a correction it re-discovers and report a clean pass.
Err(e) => Err(e),
}
}
// A TRANSIENT read (Io) during this prediction PROPAGATES: swallowing
// it drops the block from the predicted offset set, so the write pass
// would skip a correction it re-discovers on an ECC-correctable block
// and report a clean pass over the fault still on disk. An
// UNCORRECTABLE / structural failure leaves the bytes as they are (the
// scrub already recorded the finding and the write pass re-surfaces
// it), so there is no correction to predict.
Err(e @ crate::Error::Io(_)) => Err(e),
Err(_) => Ok(None),
}
}
/// Read-only pass predicting the post-heal whole-file digest AND the set of
/// write offsets the heal will touch, WITHOUT materializing every corrected
/// frame. The predicted digest is streamed: each correction (see
/// [`Self::heal_correction_for_block`]) is spliced into a running hash and
/// then DROPPED, so a broadly damaged multi-gigabyte table costs only one
/// correction frame of heap at a time instead of the whole repaired file.
///
/// The returned offset set is what the write loop gates on: it applies ONLY
/// corrections whose offset was predicted here, so a fault that appears
/// AFTER this pass (transient rot, a byte flipped between the two reads) is
/// left unwritten rather than healed under a digest that never attested it.
///
/// The streamed digest is byte-identical to
/// [`crate::repair::compute_table_checksum_with_overrides`] fed the same
/// corrections: the file is hashed from `heal_start` to EOF with each
/// size-preserving correction substituted at its offset. Corrections are
/// non-overlapping and strictly increasing (block index order; one
/// correction per block, within that block), so a single forward pass with a
/// discard-the-replaced-bytes cursor reproduces exactly that byte stream.
/// Skips a restricted view's punched prefix exactly as the write loop does.
///
/// # Errors
///
/// Any I/O error opening or streaming the file.
#[cfg(feature = "page_ecc")]
fn predict_heal_digest_and_offsets(
&self,
file: &dyn crate::fs::FsFile,
transform: &crate::table::block::BlockTransform<'_>,
heal_start: u64,
) -> crate::Result<(u128, crate::HashSet<u64>)> {
use std::io::{Read, Seek, SeekFrom};
let mut hasher = xxhash_rust::xxh3::Xxh3Default::new();
let mut offsets = crate::HashSet::default();
// A dedicated sequential reader for the digest stream: it walks forward
// from `heal_start` while `file` still serves the per-block scrub reads.
let mut rdr = self.fs.open(&self.path, &FsOpenOptions::new().read(true))?;
rdr.seek(SeekFrom::Start(heal_start))?;
let mut buf = alloc::vec![0u8; 256 * 1024];
// Absolute file position the sequential reader has consumed up to.
let mut pos = heal_start;
// Reads exactly `count` bytes from `rdr`, hashing them when `hash` is set
// and discarding them (the region a correction replaces) otherwise.
let consume = |rdr: &mut alloc::boxed::Box<dyn crate::fs::FsFile>,
hasher: &mut xxhash_rust::xxh3::Xxh3Default,
buf: &mut [u8],
mut count: u64,
hash: bool|
-> crate::Result<()> {
while count > 0 {
let want = usize::try_from(count.min(buf.len() as u64)).unwrap_or(buf.len());
let Some(window) = buf.get_mut(..want) else {
break;
};
rdr.read_exact(window)?;
if hash {
hasher.update(window);
}
count -= want as u64;
}
Ok(())
};
for entry in self.maintenance_index_walk() {
// Propagate a transient index-read failure rather than `break`: a
// truncated prediction would return a digest and an offset set that
// omit every later block, and the write loop's `predicted_offsets`
// guard would then silently skip any correctable fault it finds there,
// reporting a clean heal while leaving known damage on disk. Aborting
// makes the patrol report the failed pass and retry.
let keyed = entry?;
if let Some(bound) = &self.1
&& self.comparator.compare(keyed.end_key(), bound) == core::cmp::Ordering::Less
{
continue;
}
// An unreadable row-group directory aborts the prediction for the
// same reason an index-read failure does: its pages would silently
// drop out of the predicted offset set.
let blocks = self.data_unit_blocks(keyed.as_ref())?;
for (handle, role) in blocks {
let Some((write_offset, bytes)) =
self.heal_correction_for_block(file, handle, role, transform)?
else {
continue;
};
// Corrections are strictly increasing and non-overlapping, so a
// regression means the block index is damaged (or restamped) — and
// it must ABORT for the same reason the index-read failure above
// does: stopping here would return a digest and an offset set that
// omit every later block, and the write loop's `predicted_offsets`
// guard would then silently skip any correctable fault it finds
// there, reporting a clean heal over known damage.
let Some(gap) = write_offset.checked_sub(pos) else {
return Err(crate::Error::InvalidHeader(
"block index yields a heal correction below the position already consumed",
));
};
consume(&mut rdr, &mut hasher, &mut buf, gap, true)?;
hasher.update(&bytes);
let replaced = bytes.len() as u64;
consume(&mut rdr, &mut hasher, &mut buf, replaced, false)?;
pos = write_offset + replaced;
offsets.insert(write_offset);
// `bytes` is dropped here: only its offset is retained.
}
}
// Hash the untouched tail from the last correction to EOF.
loop {
let n = rdr.read(&mut buf)?;
if n == 0 {
break;
}
let Some(window) = buf.get(..n) else { break };
hasher.update(window);
}
Ok((hasher.digest128(), offsets))
}
/// Whether the file's CURRENT digest equals `manifest_checksum` — the
/// attribution probe [`Self::heal_data_blocks_in_place`] takes right
/// before its first write-back. The caller supplies the CURRENT
/// manifest digest (read under the per-table heal lock), not this
/// view's snapshot: a concurrent patrol may have refreshed the manifest
/// after this view was captured, and comparing against the stale
/// snapshot would mark a legitimate heal unattributable.
///
/// # Errors
///
/// A failed digest read PROPAGATES rather than grading `false`: the caller
/// must abort the heal on a probe failure, because proceeding would write
/// corrections with no completed attestation, and if the manifest
/// legitimately described the pre-heal bytes the healed digest would then be
/// permanently unreconcilable.
#[cfg(feature = "page_ecc")]
fn pre_heal_digest_matches(&self, manifest_checksum: Checksum) -> crate::Result<bool> {
// Restriction-aware: a restricted view's manifest digest covers only its
// live suffix, so probe the same region.
Ok(self.live_region_checksum()? == manifest_checksum)
}
/// Whether the ON-DISK file carries deletion metadata the digest
/// reconciliation cannot semantically authenticate: a `range_tombstones`
/// or `delete_bitmap` section. These are AUTHORITATIVE — nothing in-file
/// can re-derive which rows or ranges were genuinely deleted — so unlike
/// the derived sections (zone map, seqno bounds, locator, filter) a
/// re-stamped payload has no cross-check. Read from the file's TOC, not
/// the recover-time regions, for the same distrust-of-memory reason as
/// the other gates.
///
/// # Errors
///
/// Any I/O / decode error from reading the SFA trailer.
#[cfg(feature = "std")]
pub(crate) fn has_deletion_metadata(&self) -> crate::Result<bool> {
let mut file = self.fs.open(&self.path, &FsOpenOptions::new().read(true))?;
let trailer = crate::sfa::Reader::from_reader(&mut file)?;
let regions = regions::ParsedRegions::parse_from_toc(trailer.toc())?;
Ok(regions.range_tombstones.is_some() || regions.delete_bitmap.is_some())
}
/// Scrub: verifies the per-KV checksum footer of every data block in this
/// table, decoding each block and recomputing each entry's logical-content
/// digest.
///
/// Footer presence is a per-SST property read from the descriptor
/// (`metadata.kv_checksum_algo`), not a per-block header flag — SST data
/// blocks omit the `block_flags` byte. When the descriptor reports no
/// footers the whole scrub is a no-op; otherwise every data block is
/// verified under the descriptor's algorithm. This is the paranoid /
/// offline integrity path — the live read path does NOT verify per-entry
/// digests (the block-level checksum already covers the on-disk bytes).
/// Stops and returns on the first detected mismatch.
///
/// # Errors
///
/// - [`crate::Error::ChecksumMismatch`] if any entry's recomputed digest
/// disagrees with the stored value (corruption of the entry bytes or
/// the stored digest).
/// - Any I/O / decode error encountered while loading a block.
#[cfg_attr(
not(feature = "std"),
allow(
dead_code,
reason = "core+alloc per-KV scrub over the table; the verify/scrub consumer is std-gated, so unused under no_std"
)
)]
pub(crate) fn verify_kv_checksums(&self) -> crate::Result<()> {
// Footer presence is a per-SST property recorded in the descriptor
// (`kv_checksum_algo`); data blocks omit the block_flags byte, so the
// descriptor is the authoritative source. When it reports no footers,
// there is nothing to scrub.
let Some(expected_algo) = self.metadata.kv_checksum_algo else {
return Ok(());
};
// Descriptor declares this SST footer-bearing, and an SST is
// homogeneous — every data block carries a footer under `expected_algo`.
// A restricted view's punched prefix blocks are dead and read as zeros,
// so the walk skips them (only the live suffix is footer-verified).
self.for_each_live_block(|block| Self::check_block_kv_checksums(expected_algo, block))
}
/// The per-block half of [`Self::verify_kv_checksums`], so the combined
/// reconcile pass runs it on the shared decode.
///
/// Reads the footer off the RAW block (footer intact: the row view strips
/// it) and judges the entries the walk ALREADY materialized from that same
/// block. The bytes are disk-fresh, so a pristine copy cached before an
/// on-disk re-stamp cannot stand in for a file whose stale footer no longer
/// matches its altered value bytes.
///
/// Fed from `block.entries` rather than through `verify_kv_checked`, which
/// decodes and materializes the block for itself: on a footer-bearing table
/// that would decode every block twice, and the read count would not show
/// it — the second decode reads no additional bytes off the disk. Taking no
/// comparator is what keeps it that way: decoding a data block needs one,
/// so this cannot re-decode even by mistake.
#[cfg_attr(
not(feature = "std"),
expect(
dead_code,
reason = "per-KV gate half; the verify/scrub consumers are std-gated"
)
)]
fn check_block_kv_checksums(
expected_algo: crate::runtime_config::types::ChecksumAlgorithm,
block: &DecodedBlock,
) -> crate::Result<()> {
let mut probe = crate::table::data_block::KvDigestProbe::open(
&block.raw.data,
block.raw.header,
Some(expected_algo),
)?;
for item in &block.entries {
probe.observe(item)?;
}
probe.finish()
}
/// Cross-checks the recorded `linked_blob_files` section against the
/// COMPLETE accounting derived from this table's indirection entries
/// (per blob id: reference count, logical bytes, on-disk bytes). The
/// section carries NO per-section checksum, so structurally valid rot —
/// a flipped id byte OR a flipped counter byte — is invisible to the
/// out-of-band walk; the entries themselves are the only integrity
/// source, and the counters feed fragmentation math (a forged total can
/// make a blob file look dead while another table still references it).
/// Duplicate records for one id are rejected too. A no-op (`Ok`) for
/// tables without the section.
///
/// # Errors
///
/// [`crate::Error::InvalidHeader`] when the recorded records disagree
/// with the derived ones; any I/O / decode error from the full scan.
#[cfg(feature = "std")]
pub(crate) fn verify_blob_links(&self) -> crate::Result<()> {
use crate::coding::Decode;
use alloc::collections::BTreeMap;
// A restricted view's punched prefix is unscannable, so its derived
// accounting can only cover the LIVE SUFFIX; the recorded section
// aggregates the WHOLE table. An EXACT match is therefore impossible, but
// skipping the check entirely lets a same-size bit flip in the
// (checksum-less) section drop or forge a blob id the readable suffix
// still addresses, after which blob GC can retire a file the suffix points
// into. So a restricted view runs a CONTAINMENT check instead (see the
// final comparison below): every id/count the suffix derives must be
// COVERED BY the recorded aggregate. (This is orthogonal to the
// heal-attribution the unrestricted exact check also provides: an in-place
// heal rewrites DATA blocks only, never `linked_blob_files`.)
let restricted = self.restrict_lower_bound().is_some();
// Derive the indirection accounting FIRST, even when the section is
// absent: a table that still carries `ValueHandle` indirections but
// advertises no `linked_blob_files` section must NOT pass. Blob GC
// consults `list_blob_file_references()` to decide whether other tables
// reference a blob, so an accepted table with hidden indirections lets
// GC rewrite / drop a blob file this table still points into.
// (len, bytes, on_disk_bytes, first key, last key) per blob id,
// accumulated exactly the way the writer folds them from indirections:
// the scan yields keys in order, so the first key seen is the first
// and the latest the last.
let mut derived: BTreeMap<crate::vlog::BlobFileId, (usize, u64, u64, UserKey, UserKey)> =
BTreeMap::new();
// The objects the cell rows own, as the `owned_blob_objects` section
// lists them.
let mut derived_owned: Vec<(crate::vlog::BlobFileId, u64)> = Vec::new();
{
// An owned reference adds to its file's counts, a borrowed one
// (a cell-row reference whose owner is another version) only
// links the file, as the writer registers them.
let mut add =
|ind: &crate::blob_tree::handle::BlobIndirection, owned: bool, key: &UserKey| {
let slot = derived
.entry(ind.vhandle.blob_file_id)
.or_insert_with(|| (0, 0, 0, key.clone(), key.clone()));
if owned {
slot.0 += 1;
slot.1 += u64::from(ind.size);
slot.2 += u64::from(ind.vhandle.on_disk_size);
}
slot.4.clone_from(key);
};
let mut accumulate = |kv: InternalValue| -> crate::Result<()> {
if kv.key.value_type == crate::ValueType::Indirection {
let mut cursor = &kv.value[..];
let ind = crate::blob_tree::handle::BlobIndirection::decode_from(&mut cursor)?;
add(&ind, true, &kv.key.user_key);
} else if kv.key.value_type == crate::ValueType::CellRow {
for (ind, owned) in crate::blob_tree::field_row::row_refs(&kv.value)? {
add(&ind, owned, &kv.key.user_key);
if owned {
derived_owned.push((ind.vhandle.blob_file_id, ind.vhandle.offset));
}
}
}
Ok(())
};
// A restricted view's `[0, punch)` prefix is hole-punched and reads as
// zeros: `range` raises its lower bound to the restriction so it never
// loads a punched block, whereas `scan` walks every physical block from
// offset 0 (and would fail decoding a zeroed one). Derive from the live
// region accordingly; the restricted derive covers only the live suffix.
// Like `scan`, the walk is verification: uncounted, and it leaves
// the workload's block cache as it found it.
if restricted {
for kv in self.range_iter(..).untraced() {
accumulate(kv?)?;
}
} else {
for kv in self.scan()? {
accumulate(kv?)?;
}
}
}
// The owned objects a drop of this table releases: a list that leaves
// one out would let a reference to it be written back after the drop,
// a row holding what the accounting already charged.
self.verify_owned_blob_objects(derived_owned, restricted)?;
let Some(recorded) = self.untraced_blob_file_references()? else {
// No section is valid ONLY for a table with no indirections; a
// non-empty derived map with no recorded section is a dropped /
// renamed section hiding live blob references.
if !derived.is_empty() {
return Err(crate::Error::InvalidHeader(
"table carries indirection entries but no linked_blob_files section",
));
}
return Ok(());
};
// The writer OMITS the linked_blob_files section when there are no blob
// references, so a PRESENT but empty (zero-count) section is a forgery,
// not a legitimate no-op: e.g. a delete_bitmap replaced by a four-byte
// zero-count linked_blob_files. Both `derived` and `recorded_map` would
// then be empty and the equality check below would pass, keeping the
// table after its deletion metadata vanished. Reject it here.
if recorded.is_empty() {
return Err(crate::Error::InvalidHeader(
"linked_blob_files section is present but records no blob references",
));
}
let mut recorded_map: BTreeMap<
crate::vlog::BlobFileId,
(usize, u64, u64, UserKey, UserKey),
> = BTreeMap::new();
for link in recorded {
if recorded_map
.insert(
link.blob_file_id,
(
link.len,
link.bytes,
link.on_disk_bytes,
link.first_key,
link.last_key,
),
)
.is_some()
{
return Err(crate::Error::InvalidHeader(
"linked_blob_files carries duplicate records for one blob id",
));
}
}
if restricted {
// Containment: the recorded aggregate covers the WHOLE table, so each
// id the live suffix references must appear with a count/byte total at
// least as large as the suffix's own. A suffix-referenced id missing
// from the section, or recorded below the suffix's derived total,
// means the section dropped or under-counted a live reference — reject
// so blob GC cannot retire a file the suffix still addresses.
// The recorded key span covers the whole table's references, so it
// must enclose the span the suffix derives.
let cmp = self.comparator.as_ref();
for (id, derived_counts) in &derived {
match recorded_map.get(id) {
Some(rec)
if rec.0 >= derived_counts.0
&& rec.1 >= derived_counts.1
&& rec.2 >= derived_counts.2
&& cmp.compare(&rec.3, &derived_counts.3)
!= core::cmp::Ordering::Greater
&& cmp.compare(&rec.4, &derived_counts.4)
!= core::cmp::Ordering::Less => {}
_ => {
return Err(crate::Error::InvalidHeader(
"linked_blob_files omits or under-counts a blob id the \
restricted suffix still references",
));
}
}
}
return Ok(());
}
if derived != recorded_map {
return Err(crate::Error::InvalidHeader(
"linked_blob_files disagrees with the table's indirection entries",
));
}
Ok(())
}
/// Cross-checks the `owned_blob_objects` section against `derived`, the
/// objects the scanned cell rows own: equal for a whole table, containing
/// them for a `restricted` view, whose punched prefix cannot be scanned.
/// The writer omits the section when the table owns nothing, so a present
/// but empty one is a forgery.
///
/// # Errors
///
/// [`crate::Error::InvalidHeader`] when the section disagrees, and any
/// error reading it.
#[cfg(feature = "std")]
fn verify_owned_blob_objects(
&self,
mut derived: Vec<(crate::vlog::BlobFileId, u64)>,
restricted: bool,
) -> crate::Result<()> {
let recorded = self.owned_blob_objects()?;
if self.regions.owned_blob_objects.is_some() && recorded.is_empty() {
return Err(crate::Error::InvalidHeader(
"owned_blob_objects section is present but lists no object",
));
}
derived.sort_unstable();
derived.dedup();
let agrees = if restricted {
derived
.iter()
.all(|object| recorded.binary_search(object).is_ok())
} else {
derived == recorded
};
if !agrees {
return Err(crate::Error::InvalidHeader(
"owned_blob_objects disagrees with the objects the table's cell rows own",
));
}
Ok(())
}
/// Compares the two DECODED TLI mirrors (head `tli` vs `tli_tail`) and
/// validates the decoded handle list against the PHYSICAL section
/// layout. Each copy is independently checksum- (and parity-)consistent,
/// so a forged tail that encodes a DIFFERENT handle list passes every
/// byte-level check — and BOTH mirrors forged to the SAME list pass the
/// equality comparison too, since two forged copies prove nothing about
/// the data blocks. The writer emits blocks strictly back-to-back, so
/// the decoded handles must exactly TILE their section: in full-index
/// mode the handles tile the `data` section; in partitioned mode the TLI
/// handles tile the `index` section and the partitions' concatenated
/// data handles tile the `data` section. An omitted, redirected, or
/// duplicated handle leaves a gap or an overlap.
///
/// # Errors
///
/// [`crate::Error::InvalidHeader`] when the decoded mirrors differ or
/// the handle list does not tile the physical sections; any I/O / decode
/// error while loading either copy (an unreadable mirror is equally
/// untrustworthy for the callers' restamp / keep decisions).
#[cfg(feature = "std")]
pub(crate) fn verify_tli_mirrors(&self) -> crate::Result<()> {
self.verify_tli_mirrors_inner(true)
}
/// Whether the index STRUCTURE alone authenticates its offsets as original
/// block boundaries: the mirrors agree, the binary-index pointers verify,
/// the partition separators are consistent, and the handles TILE their
/// section. This is the block-boundary provenance the salvage walk needs to
/// trust an indexed offset without re-reading each (possibly corrupt) data
/// block — [`verify_tli_mirrors`](Self::verify_tli_mirrors) additionally
/// frames and decodes every data block, which a bit-rotted-but-index-intact
/// table (the salvage case) would fail spuriously.
///
/// # Errors
///
/// Propagates only a TRANSIENT [`crate::Error::Io`] from opening / reading
/// the index mirrors: folding a flaky read into `false` would make the
/// salvage walk fall back to physical-chain provenance and surrender every
/// block past the first header break, dropping otherwise healthy keys the
/// intact TLI could have anchored on retry. `Ok(false)` covers a STRUCTURAL
/// authentication failure (mirrors disagree, pointers do not verify, handles
/// do not tile) AND a PERSISTENT read failure of one mirror: both mean the
/// index cannot be trusted here, so the walk should fall back to the readable
/// data section rather than abort and recover nothing.
#[cfg(feature = "std")]
pub(crate) fn tli_structure_authenticated(&self) -> crate::Result<bool> {
match self.verify_tli_mirrors_inner(false) {
Ok(()) => Ok(true),
// Only an ENVIRONMENTAL read propagates (so the salvage walk aborts
// for a retry, or for the right key, rather than surrendering an
// intact index to a fixable fault — `false` costs every indexed
// block past the first header break). A failure on the DATA (a
// bad-sector `UnexpectedEof` on one mirror while the other and the
// data section stay readable) is untrusted input, not a reason to
// abort: fold it into `false` so the walk falls back to
// physical-chain provenance and still recovers the readable blocks.
Err(e) if e.is_environmental() => Err(e),
Err(_) => Ok(false),
}
}
/// Shared body. `frame_blocks` additionally frames and decodes each
/// addressed DATA block (the full integrity check the scrub reconcile
/// wants); with it `false` only the index structure is authenticated, for
/// the salvage walk that runs over corrupt data blocks by design.
#[cfg(feature = "std")]
fn verify_tli_mirrors_inner(&self, frame_blocks: bool) -> crate::Result<()> {
use crate::table::block::ParsedItem as _;
let mut file = self.fs.open(&self.path, &FsOpenOptions::new().read(true))?;
let trailer = crate::sfa::Reader::from_reader(&mut file)?;
let regions = regions::ParsedRegions::parse_from_toc(trailer.toc())?;
let head = Self::read_tli_at(
&*file,
regions.tli,
self.metadata.id,
self.metadata.index_block_compression,
self.encryption.as_deref(),
self.metadata.ecc_params,
)?;
if let Some(tail_handle) = regions.tli_tail {
let tail = Self::read_tli_at(
&*file,
tail_handle,
self.metadata.id,
self.metadata.index_block_compression,
self.encryption.as_deref(),
self.metadata.ecc_params,
)?;
if head.as_slice() != tail.as_slice() {
return Err(crate::Error::InvalidHeader(
"tli_tail decodes to a different handle list than the head tli",
));
}
}
// Structural coverage: the decoded handles must tile their section.
let data_section = trailer
.toc()
.section(b"data")
.ok_or(crate::Error::InvalidHeader("data section missing"))?;
head.try_iter(self.comparator.clone())?;
// The sequential walk below never reads the BINARY INDEX pointers,
// so a pointer redirected to another restart head passes mirror
// equality, tiling, and every separator check — yet the seek path
// trusts it after reopen. Authenticate the pointers against the
// sequentially derived restart heads.
head.verify_binary_index()?;
let keyed: alloc::vec::Vec<KeyedBlockHandle> = head
.iter(self.comparator.clone())
.map(|i| i.materialize(head.as_slice()))
.collect();
let handles: alloc::vec::Vec<BlockHandle> = keyed.iter().map(|k| *k.as_ref()).collect();
if let Some(index_handle) = regions.index {
// Partitioned: the TLI addresses index partitions inside the
// `index` section; the partitions address the data blocks.
let index_section = trailer
.toc()
.section(b"index")
.ok_or(crate::Error::InvalidHeader("index section missing"))?;
let _ = index_handle;
if !Self::frames_tile_section(&handles, index_section.pos(), index_section.len()) {
return Err(crate::Error::InvalidHeader(
"tli handles do not tile the index section",
));
}
self.verify_handles_frame_blocks(
&handles,
index_section.pos(),
index_section.len(),
FrameUnit::Block,
)?;
let mut data_handles = alloc::vec::Vec::new();
for top in &keyed {
let part = Self::read_tli_at(
&*file,
*top.as_ref(),
self.metadata.id,
self.metadata.index_block_compression,
self.encryption.as_deref(),
self.metadata.ecc_params,
)?;
part.try_iter(self.comparator.clone())?;
// Same pointer authentication as the TLI above: a partition
// seek trusts its binary index too.
part.verify_binary_index()?;
let part_keyed: alloc::vec::Vec<KeyedBlockHandle> = part
.iter(self.comparator.clone())
.map(|i| i.materialize(part.as_slice()))
.collect();
// The TLI's top-level SEPARATOR for this partition must equal the
// partition's LAST data-block separator. `frames_tile_section`
// and the per-block separator checks ignore the top-level keys,
// so a re-stamped partition boundary (in both mirrors) passes
// them all — yet `TwoLevelBlockIndex::forward_reader` seeks by the
// top-level separator and would route reads to the wrong
// partition, skipping the keys the real partition holds.
let Some(part_last) = part_keyed.last() else {
return Err(crate::Error::InvalidHeader(
"tli addresses an index partition that decodes to zero entries",
));
};
if self
.comparator
.compare(top.end_key().as_ref(), part_last.end_key().as_ref())
!= core::cmp::Ordering::Equal
{
return Err(crate::Error::InvalidHeader(
"tli separator disagrees with its partition's last separator",
));
}
data_handles.extend(part_keyed.iter().map(|k| *k.as_ref()));
}
if !Self::frames_tile_section(&data_handles, data_section.pos(), data_section.len()) {
return Err(crate::Error::InvalidHeader(
"index partitions' data handles do not tile the data section",
));
}
if frame_blocks {
self.verify_handles_frame_blocks(
&data_handles,
data_section.pos(),
data_section.len(),
FrameUnit::DataUnit,
)?;
}
} else {
if !Self::frames_tile_section(&handles, data_section.pos(), data_section.len()) {
return Err(crate::Error::InvalidHeader(
"tli data handles do not tile the data section",
));
}
if frame_blocks {
self.verify_handles_frame_blocks(
&handles,
data_section.pos(),
data_section.len(),
FrameUnit::DataUnit,
)?;
}
}
Ok(())
}
/// Requires every handle's SIZE to equal the physical frame its block's
/// on-disk header derives (header + payload + parity). The cumulative
/// tiling check cannot tell ONE handle spanning several back-to-back
/// blocks from the real per-block layout: the spanned frame still
/// decodes its FIRST payload (the tail reads as an unrecognized trailer
/// on a non-ECC block), so the separator cross-check passes too — yet
/// every later physical block is unreachable through the index and
/// reads silently miss its keys.
///
/// `unit` says what one handle names in this section: a block for the
/// index section, a data unit for the data section. A columnar table's
/// data unit is a row group — several blocks under one handle by design —
/// so its frame is the group's, and the protection still holds: a forged
/// handle spanning two groups disagrees with the frame of one.
#[cfg(feature = "std")]
fn verify_handles_frame_blocks(
&self,
handles: &[BlockHandle],
pos: u64,
len: u64,
unit: FrameUnit,
) -> crate::Result<()> {
// checked, not saturating: a re-stamped TOC could overflow `pos + len`,
// and a saturated `u64::MAX` bound would then accept a forged oversized
// handle instead of rejecting the corrupt section.
let section_end = pos
.checked_add(len)
.ok_or(crate::Error::InvalidHeader("data section length overflows"))?;
// A restricted view's punched prefix blocks read as zeros, so their
// physical frame can no longer be probed. They tile the section by
// length (punch preserves file size) but are dead, so skip framing
// them. Index-section handles sit above the (data-section) punch
// offset and are never skipped.
let punch = self.punch_offset()?;
let file = self.fs.open(&self.path, &FsOpenOptions::new().read(true))?;
for handle in handles {
if handle.offset().0 < punch {
continue;
}
let probed = match unit {
FrameUnit::Block => {
self.probe_block_handle_in(&*file, handle.offset().0, section_end)?
}
FrameUnit::DataUnit => {
self.probe_data_unit_in(&*file, handle.offset().0, section_end)?
}
};
if probed.size() != handle.size() {
return Err(crate::Error::InvalidHeader(
"an index handle's size disagrees with its block's physical frame",
));
}
}
Ok(())
}
/// Whether `handles`, in order, exactly tile `[pos, pos + len)`: the
/// first starts at `pos`, each next starts where the previous ended, and
/// the last ends at `pos + len`. The writer emits blocks back-to-back,
/// so any omitted, redirected, or duplicated handle breaks the tiling.
#[cfg(feature = "std")]
fn frames_tile_section(handles: &[BlockHandle], pos: u64, len: u64) -> bool {
let mut at = pos;
for handle in handles {
if handle.offset().0 != at {
return false;
}
at = match at.checked_add(u64::from(handle.size())) {
Some(v) => v,
None => return false,
};
}
pos.checked_add(len) == Some(at)
}
/// Reads the disk-fresh leaf SEPARATORS: each live data block's offset
/// mapped to the `end_key` the on-disk index records for it. `None` for a
/// table whose index carries no entries at all.
///
/// Tail-first with head fallback, mirroring recovery ([`Self::read_tli`]):
/// the next open trusts that copy, so it is the one the separators must be
/// judged from. Mirror EQUALITY is not this reader's job —
/// [`Self::verify_tli_mirrors`] establishes it, along with the section
/// tiling, the binary-index pointers and (when partitioned) the top-level
/// partition boundaries.
///
/// Blocks below the punch offset are left out: a restricted view's punched
/// prefix decodes to zeros, so those separators are dead and the walk skips
/// their blocks too.
///
/// # Errors
///
/// Propagates the index reads, and rejects an index partition that decodes
/// to zero entries.
#[cfg(feature = "std")]
fn read_tli_separators(
&self,
file: &dyn crate::fs::FsFile,
regions: ®ions::ParsedRegions,
) -> crate::Result<crate::HashMap<u64, UserKey>> {
let punch = self.punch_offset()?;
let head = Self::read_tli(
regions,
file,
self.metadata.id,
self.metadata.index_block_compression,
self.encryption.as_deref(),
self.metadata.ecc_params,
)?;
let mut separators = crate::HashMap::default();
let mut collect = |idx: &IndexBlock| -> crate::Result<()> {
for keyed in idx.iter(self.comparator.clone()) {
let keyed = keyed.materialize(idx.as_slice());
if keyed.offset().0 < punch {
continue;
}
separators.insert(keyed.offset().0, keyed.end_key().clone());
}
Ok(())
};
if regions.index.is_some() {
// Partitioned: the top level addresses index partitions, and the
// partitions address the data blocks whose separators are wanted.
let tops: Vec<BlockHandle> = head
.iter(self.comparator.clone())
.map(|i| *i.materialize(head.as_slice()).as_ref())
.collect();
for top in tops {
let part = Self::read_tli_at(
file,
top,
self.metadata.id,
self.metadata.index_block_compression,
self.encryption.as_deref(),
self.metadata.ecc_params,
)?;
collect(&part)?;
}
} else {
collect(&head)?;
}
Ok(separators)
}
/// Confirms a data block's decoded LAST key equals the separator the
/// on-disk index records for it. A forged separator re-stamped to another
/// still-sorted value passes the mirror comparison and the section tiling
/// (both ignore the keys), yet the index binary search then routes keys in
/// `(forged_separator, real_last_key]` to the wrong block and `point_read`
/// misses them after reopen.
///
/// Judged on the walk's decode, and paired BOTH ways: a live block the
/// index does not address is as much corruption as an addressed block whose
/// contents disagree, and only the pairing catches the first.
///
/// # Errors
///
/// [`crate::Error::InvalidHeader`] when the block has no separator, decodes
/// to zero entries, or disagrees with its separator.
#[cfg(feature = "std")]
fn check_block_separator(
&self,
separators: &crate::HashMap<u64, UserKey>,
block: &DecodedBlock,
) -> crate::Result<()> {
let Some(separator) = separators.get(&block.handle.offset().0) else {
return Err(crate::Error::InvalidHeader(
"a live data block carries no tli separator",
));
};
let Some(last) = block.entries.last() else {
return Err(crate::Error::InvalidHeader(
"tli separator addresses a data block that decodes to zero entries",
));
};
if self
.comparator
.compare(separator.as_ref(), last.key.user_key.as_ref())
!= core::cmp::Ordering::Equal
{
return Err(crate::Error::InvalidHeader(
"tli separator does not match the addressed block's decoded last key",
));
}
Ok(())
}
/// Runs the whole semantic cross-check family on ONE disk-fresh decode per
/// live data block.
///
/// Each gate used to walk the table itself, so a reconcile decoded every
/// block seven to nine times and every gate added another full pass. Here
/// the sections are read once, the blocks are decoded once, and each
/// block feeds every per-block check before the next block is read.
///
/// The gates run in the order the callers' own chains use, so a table with
/// a single defect reports the same gate — and the same message — as
/// before. `Err` names the gate that tripped, so a caller can keep its own
/// per-gate wording.
///
/// # Errors
///
/// The failing gate and its error. Section reads and block decodes surface
/// as the error of the gate that needed them.
#[cfg(feature = "std")]
pub(crate) fn verify_reconcile_gates(
&self,
prefix_extractor: Option<&alloc::sync::Arc<dyn crate::prefix::PrefixExtractor>>,
bitmap_digest_authenticated: bool,
) -> Result<(), (ReconcileGate, crate::Error)> {
use ReconcileGate as G;
let tag = |gate: G| move |e: crate::Error| (gate, e);
// Sections first: every gate that has one reads it before the walk, so
// a forged section fails without decoding a single block.
let kv_algo = self.metadata.kv_checksum_algo;
// One open and one TOC parse for the whole pass: every section below is
// read from the SAME on-disk image, so the gates cannot disagree about
// which bytes they are judging.
//
// The sections stay live for the whole walk, where the gate-by-gate
// shape held one at a time. That is a peak of their SUM rather than
// their max, bounded by what an open table already pins permanently
// (`Inner` holds the seqno bounds, zone map, locator and filter
// simultaneously for its whole lifetime), so the increment is one extra
// copy of resident data for the duration of one table's pass. Dropping
// any of them mid-walk is not available: every one is consulted on
// EVERY block, so releasing early means walking again per section,
// which is the cost this pass exists to remove.
let (file, regions) = self.open_sections().map_err(tag(G::SeqnoBounds))?;
let separators = self
.read_tli_separators(&*file, ®ions)
.map_err(tag(G::Separators))?;
let seqno_bounds = self
.read_seqno_bounds_section(&*file, ®ions)
.map_err(tag(G::SeqnoBounds))?;
let zone_map = self
.read_zone_map_section(&*file, ®ions)
.map_err(tag(G::ZoneMap))?;
let zone_map = match zone_map {
// A PRESENT-but-empty map on a table with data blocks is a forgery;
// the standalone gate rejects it before its walk, so do it here.
Some(map) if map.is_empty() => {
if self.untraced_index_walk().next().is_some() {
return Err((
G::ZoneMap,
crate::Error::InvalidHeader(
"zone_map section is present but empty on a table with data blocks",
),
));
}
None
}
other => other,
};
let locator = self
.read_locator_section(&*file, ®ions)
.map_err(tag(G::Locator))?;
let mut filter = self
.filter_probe(&*file, ®ions, prefix_extractor)
.map_err(tag(G::Filter))?;
let mut meta = self
.meta_bounds_probe(&*file, ®ions, bitmap_digest_authenticated)
.map_err(tag(G::MetadataBounds))?;
// The per-frame half of the block-layout check. The section-level half
// (present-but-empty, which is a relabelled delete_bitmap dropping the
// deletion metadata) stays in `verify_block_layout`, where it runs on
// EVERY build; this half needs a zstd decoder and the frame the walk
// already has.
#[cfg(feature = "zstd")]
let block_layout = self
.read_block_layout_section(&*file, ®ions)
.map_err(tag(G::BlockLayout))?
.filter(|map| !map.is_empty());
// Per-block state the gates carry across the walk.
let mut separators_checked = 0usize;
let mut seqno_checked = 0usize;
let mut zone_checked = 0usize;
#[cfg(feature = "zstd")]
let mut layout_checked = 0usize;
let mut locator_seen: crate::HashSet<Vec<u8>> = crate::HashSet::default();
let mut prev_internal: Option<(UserKey, SeqNo)> = None;
// The hash of each entry's key of the block being checked, taken once a
// key for the locator and filter gates, which both probe by it.
let mut entry_hashes: Vec<u64> = Vec::new();
// The walk's closure returns the plain engine error, so the gate that
// produced it is recorded here and paired with it after the walk.
let mut failed: Option<ReconcileGate> = None;
let walk = self.for_each_live_block(|block| {
// First, as the standalone separator pass ran before every other
// gate in both callers' chains.
if let Err(e) = self.check_block_separator(&separators, block) {
failed = Some(G::Separators);
return Err(e);
}
let Some(next) = separators_checked.checked_add(1) else {
failed = Some(G::Separators);
return Err(crate::Error::InvalidHeader("tli separators"));
};
separators_checked = next;
if let Some(algo) = kv_algo
&& let Err(e) = Self::check_block_kv_checksums(algo, block)
{
failed = Some(G::KvChecksums);
return Err(e);
}
if let Some(map) = &seqno_bounds {
if let Err(e) = Self::check_block_seqno_bounds(map, block) {
failed = Some(G::SeqnoBounds);
return Err(e);
}
let Some(next) = seqno_checked.checked_add(1) else {
failed = Some(G::SeqnoBounds);
return Err(crate::Error::InvalidHeader("seqno_bounds"));
};
seqno_checked = next;
}
if let Err(e) = Self::check_block_entry_count(block) {
failed = Some(G::BlockEntryCounts);
return Err(e);
}
if let Some(map) = &zone_map {
if let Err(e) = Self::check_block_zone_entry(map, block) {
failed = Some(G::ZoneMap);
return Err(e);
}
let Some(next) = zone_checked.checked_add(1) else {
failed = Some(G::ZoneMap);
return Err(crate::Error::InvalidHeader("zone_map"));
};
zone_checked = next;
}
if locator.is_some() || filter.is_some() {
entry_hashes.clear();
let mut previous: Option<(&[u8], u64)> = None;
for entry in &block.entries {
let key: &[u8] = &entry.key.user_key;
// A key's versions are adjacent and share its hash.
let hash = match previous {
Some((previous, hash))
if crate::comparator::same_user_key(previous, key) =>
{
hash
}
_ => crate::hash::hash64(key),
};
entry_hashes.push(hash);
previous = Some((key, hash));
}
}
if let Some(locator) = &locator
&& let Err(e) =
self.check_block_locator(locator, &mut locator_seen, block, &entry_hashes)
{
failed = Some(G::Locator);
return Err(e);
}
if let Some(probe) = &mut filter
&& let Err(e) = probe.check_block(block, &entry_hashes)
{
failed = Some(G::Filter);
return Err(e);
}
if let Err(e) = self.check_block_reachability(&mut prev_internal, block) {
failed = Some(G::PointReadReachability);
return Err(e);
}
if let Err(e) = Self::observe_meta_bounds(&mut meta, block) {
failed = Some(G::MetadataBounds);
return Err(e);
}
#[cfg(feature = "zstd")]
if let Some(map) = &block_layout {
match Self::check_block_layout_entry(map, block) {
Err(e) => {
failed = Some(G::BlockLayout);
return Err(e);
}
// Blocks absent from the map are single-inner-block frames
// the writer had nothing to record for.
Ok(false) => {}
Ok(true) => {
let Some(next) = layout_checked.checked_add(1) else {
failed = Some(G::BlockLayout);
return Err(crate::Error::InvalidHeader("block_layout"));
};
layout_checked = next;
}
}
}
Ok(())
});
if let Err(e) = walk {
// An untagged failure came from the walk itself (an index read, a
// block read, a decode), which is the entry-count gate's own job.
return Err((failed.unwrap_or(G::BlockEntryCounts), e));
}
// Finalizers: the counts the per-block checks accumulated.
//
// The separator pairing closes the other direction: the per-block half
// rejects a live block the index does not address, this rejects a live
// separator no walked block claimed. The standalone pass could express
// neither — it walked the index side alone.
if separators_checked != separators.len() {
return Err((
G::Separators,
crate::Error::InvalidHeader(
"the index carries separators for blocks it does not hold",
),
));
}
let punch = self.punch_offset().map_err(tag(G::SeqnoBounds))?;
if let Some(map) = &seqno_bounds
&& seqno_checked != map.live_len(punch)
{
return Err((
G::SeqnoBounds,
crate::Error::InvalidHeader(
"seqno_bounds carries entries for blocks the index does not hold",
),
));
}
if let Some(map) = &zone_map
&& zone_checked != map.live_len(punch)
{
return Err((
G::ZoneMap,
crate::Error::InvalidHeader(
"zone_map carries entries for blocks the index does not hold",
),
));
}
// Every recorded LIVE entry matched a walked block (offsets are unique
// on both sides), so equal counts mean the map records ONLY this
// table's blocks. A restricted view compares against the live entries:
// its punched prefix keeps its own, legitimately, because the map is
// not rewritten on reopen.
#[cfg(feature = "zstd")]
if let Some(map) = &block_layout
&& layout_checked != map.live_len(punch)
{
return Err((
G::BlockLayout,
crate::Error::InvalidHeader(
"block_layout carries entries for blocks the index does not hold",
),
));
}
self.finish_meta_bounds(meta)
.map_err(tag(G::MetadataBounds))
}
/// Opens the file and parses its TOC: the pair every reconcile section read
/// works from. Done ONCE per pass and lent to each reader, so a reconcile
/// costs one open and one TOC parse instead of one per section, and every
/// gate judges the SAME on-disk image rather than up to five successive
/// ones.
///
/// # Errors
///
/// Propagates the file open, the SFA trailer read and the TOC parse.
#[cfg(feature = "std")]
fn open_sections(
&self,
) -> crate::Result<(
alloc::boxed::Box<dyn crate::fs::FsFile>,
regions::ParsedRegions,
)> {
let mut file = self.fs.open(&self.path, &FsOpenOptions::new().read(true))?;
let trailer = crate::sfa::Reader::from_reader(&mut file)?;
let regions = regions::ParsedRegions::parse_from_toc(trailer.toc())?;
Ok((file, regions))
}
/// Reads the `seqno_bounds` section FROM DISK, or `None` when the table
/// has none.
///
/// Disk-fresh on purpose: the in-memory map was loaded at recover time, so
/// an on-disk re-stamp after the open — the very forge this gate exists
/// for — would be invisible to it. Unlike the best-effort recover load, an
/// unreadable section is an error here: the callers are deciding whether to
/// trust the file's bytes.
///
/// # Errors
///
/// Propagates the section decode, and rejects a block that does not carry
/// the seqno-bounds role.
#[cfg(feature = "std")]
fn read_seqno_bounds_section(
&self,
file: &dyn crate::fs::FsFile,
regions: ®ions::ParsedRegions,
) -> crate::Result<Option<crate::table::seqno_bounds::SeqnoBoundsMap>> {
let Some(sb_handle) = regions.seqno_bounds else {
return Ok(None);
};
let block = Block::from_file(
file,
sb_handle,
crate::table::block::BlockIdentity {
table_id: self.metadata.id,
block_type: BlockType::SeqnoBounds,
dict_id: 0,
window_log: 0,
},
&self.section_transform(),
)?;
if block.header.block_type != BlockType::SeqnoBounds {
return Err(crate::Error::InvalidTag((
"BlockType",
block.header.block_type.into(),
)));
}
Ok(Some(crate::table::seqno_bounds::SeqnoBoundsMap::decode(
&block.data,
)?))
}
/// The encryption / ECC context the uncompressed side sections decode
/// under. Every reconcile section read builds the same one.
#[cfg(feature = "std")]
fn section_transform(&self) -> crate::table::block::BlockTransform<'_> {
let t = match self.encryption.as_deref() {
Some(enc) => crate::table::block::BlockTransform::Encrypted(enc),
None => crate::table::block::BlockTransform::PLAIN,
};
if let Some(ecc) = self.metadata.ecc_params {
t.with_ecc(ecc)
} else {
t
}
}
/// The recorded per-block seqno range must equal the one the block's
/// decoded entries span.
#[cfg(feature = "std")]
fn check_block_seqno_bounds(
recorded_map: &crate::table::seqno_bounds::SeqnoBoundsMap,
block: &DecodedBlock,
) -> crate::Result<()> {
let Some(recorded) = recorded_map.bounds_for(block.handle.offset().0) else {
return Err(crate::Error::InvalidHeader(
"seqno_bounds is missing a data block's entry",
));
};
let mut seqnos = block.entries.iter().map(|e| e.key.seqno);
let Some(first) = seqnos.next() else {
return Err(crate::Error::InvalidHeader(
"data block decodes to zero entries",
));
};
let derived = seqnos.fold((first, first), |(lo, hi), s| (lo.min(s), hi.max(s)));
if derived != recorded {
return Err(crate::Error::InvalidHeader(
"seqno_bounds disagrees with the block's decoded entries",
));
}
Ok(())
}
/// Checks a block's declared entry count against the DECODED one. The
/// out-of-band walk verifies only the outer frame, and `verify_kv_checksums`
/// is a no-op for a footer-less SST, so a checksum- and parity-consistent
/// block with a valid prefix followed by a malformed entry is otherwise
/// graded clean: the entry decoder turns a mid-stream parse failure into an
/// ordinary end of iteration, so a later scan silently omits the malformed
/// tail. Decoding to fewer (or more) entries than the trailer declares is
/// corruption. Covers both row-major and columnar blocks.
///
/// # Errors
///
/// [`crate::Error::InvalidHeader`] when the block decodes to a different
/// entry count than its trailer declares.
#[cfg(feature = "std")]
fn check_block_entry_count(block: &DecodedBlock) -> crate::Result<()> {
#[cfg(feature = "columnar")]
if let Some(batch) = &block.batch {
// The entries were fully materialized by the walk, so a malformed
// batch already failed there rather than truncating silently.
if block.entries.len() != batch.row_count as usize {
return Err(crate::Error::InvalidHeader(
"columnar block decodes to fewer rows than its batch declares",
));
}
return Ok(());
}
let Some(row) = &block.row else {
return Ok(());
};
if block.entries.len() != row.len() {
return Err(crate::Error::InvalidHeader(
"data block decodes to fewer entries than its trailer declares",
));
}
Ok(())
}
/// Reads the `zone_map` section FROM DISK, or `None` when the table has
/// none. Disk-fresh for the same reason as the seqno bounds: the in-memory
/// map is best-effort at recover time, so an on-disk re-stamp after the
/// open is invisible to it.
///
/// # Errors
///
/// Propagates the section decode, and rejects a block that does not carry
/// the zone-map role.
#[cfg(feature = "std")]
fn read_zone_map_section(
&self,
file: &dyn crate::fs::FsFile,
regions: ®ions::ParsedRegions,
) -> crate::Result<Option<crate::table::zone_map::ZoneMap>> {
let Some(zm_handle) = regions.zone_map else {
return Ok(None);
};
let block = Block::from_file(
file,
zm_handle,
crate::table::block::BlockIdentity {
table_id: self.metadata.id,
block_type: BlockType::ZoneMap,
dict_id: 0,
window_log: 0,
},
&self.section_transform(),
)?;
if block.header.block_type != BlockType::ZoneMap {
return Err(crate::Error::InvalidTag((
"BlockType",
block.header.block_type.into(),
)));
}
Ok(Some(crate::table::zone_map::ZoneMap::decode(&block.data)?))
}
/// Reads the `block_layout` section FROM DISK, or `None` when the table
/// has none.
///
/// Disk-fresh for the same reason every other reconcile section is: the
/// in-memory copy was loaded at recover time and would not show an on-disk
/// re-stamp made after the open.
///
/// The encryption-aware transform is not optional here. On an encrypted
/// table the section is AEAD-sealed and its AAD binds the block type, so a
/// `delete_bitmap` relabelled into a `block_layout` fails to open — which
/// is precisely the forgery the emptiness check downstream exists to
/// catch.
///
/// # Errors
///
/// Propagates the section decode, and rejects a block that does not carry
/// the block-layout role.
#[cfg(all(feature = "std", feature = "zstd"))]
fn read_block_layout_section(
&self,
file: &dyn crate::fs::FsFile,
regions: ®ions::ParsedRegions,
) -> crate::Result<Option<crate::table::block_layout::BlockLayoutMap>> {
let Some(bl_handle) = regions.block_layout else {
return Ok(None);
};
let block = Block::from_file(
file,
bl_handle,
crate::table::block::BlockIdentity {
table_id: self.metadata.id,
block_type: BlockType::BlockLayout,
dict_id: 0,
window_log: 0,
},
&self.section_transform(),
)?;
if block.header.block_type != BlockType::BlockLayout {
return Err(crate::Error::InvalidTag((
"BlockType",
block.header.block_type.into(),
)));
}
Ok(Some(crate::table::block_layout::BlockLayoutMap::decode(
&block.data,
)?))
}
/// The recorded zone-map entry must match the one the block's decoded
/// contents produce. A columnar block records
/// one entry per stored column, re-derived by the SAME function the writer
/// used, so any divergence (a re-stamped range, an added / dropped column,
/// a flipped id) is a forgery; a row block records a single synthetic
/// whole-block key range.
/// Every boundary the map records for this block must be exactly where the
/// frame's k-th inner zstd block ends, and the last one must EXHAUST the
/// frame: an unrecorded inner block past the final boundary would hide
/// data the partial-read path never decompresses.
///
/// Decodes one inner block per recorded end, carrying the resume state and
/// the accumulated window forward, so `n` boundaries cost `n` inner-block
/// decodes. The stepwise-from-zero shape this replaces re-decoded the
/// prefix `[0, k)` for every `k`, i.e. `n(n+1)/2`.
///
/// A zstd reset / decode failure on a checksum-clean frame is DETERMINISTIC
/// codec corruption (a forge that kept the block checksum but broke the
/// inner framing), not transient I/O: it maps to the structural error so
/// the repair gate routes the table through salvage, which re-encodes the
/// block, instead of aborting for a retry that can never succeed.
///
/// # Errors
///
/// [`crate::Error::InvalidHeader`] when the boundaries are not strictly
/// increasing, disagree with the frame's inner blocks, or leave the frame
/// unfinished.
#[cfg(all(feature = "std", feature = "zstd"))]
fn check_block_layout_entry(
recorded_map: &crate::table::block_layout::BlockLayoutMap,
block: &DecodedBlock,
) -> crate::Result<bool> {
const ERR: crate::Error =
crate::Error::InvalidHeader("block_layout disagrees with the frames' inner blocks");
let Some(ends) = recorded_map.ends_for(block.handle.offset().0) else {
return Ok(false);
};
if !ends.iter().zip(ends.iter().skip(1)).all(|(a, b)| a < b) {
return Err(ERR);
}
if ends.last() != Some(&block.raw.header.uncompressed_length) {
return Err(ERR);
}
let mut src = std::io::Cursor::new(block.frame.as_ref());
let mut decoder = structured_zstd::decoding::FrameDecoder::new();
// The resume window: every inner block decoded so far. zstd
// back-references reach into it, so it is the decoder's input as much
// as the compressed bytes are.
let mut window: Vec<u8> = Vec::new();
let mut resume: Option<structured_zstd::decoding::ResumeState> = None;
let mut compressed_cursor = 0u64;
for (idx, &end) in ends.iter().enumerate() {
#[expect(
clippy::cast_possible_truncation,
reason = "inner-block index is bounded by ends.len(), well within u32"
)]
let end_block = (idx + 1) as u32;
// `reset` re-parses the frame header from the start; a resume then
// repositions the source to the inner block it left off at.
src.set_position(0);
decoder.reset(&mut src).map_err(|_| ERR)?;
let pd = if let Some(state) = resume.as_ref() {
src.set_position(compressed_cursor);
decoder
.decode_blocks_partial(
&mut src,
state.block_index(),
end_block,
Some(structured_zstd::decoding::ResumeInput {
window_prime: &window,
state,
}),
true,
)
.map_err(|_| ERR)?
} else {
decoder
.decode_blocks_partial(&mut src, 0, end_block, None, true)
.map_err(|_| ERR)?
};
if pd.stopped_at.is_some() || pd.start_block + pd.blocks_decoded != end_block {
return Err(ERR);
}
// A fresh decode emits `[0, end_block)`; a resume emits only the
// new tail, contiguous with what the window already holds — so the
// window is reset for the former and appended to for the latter,
// and the cursor advances rather than restarting.
if resume.is_some() {
compressed_cursor += decoder.bytes_read_from_source();
} else {
compressed_cursor = decoder.bytes_read_from_source();
window.clear();
}
window.extend_from_slice(&pd.data);
if window.len() != end as usize {
return Err(ERR);
}
// The final recorded end must exhaust the frame.
if idx + 1 == ends.len() && !pd.frame_finished {
return Err(ERR);
}
resume = pd.resume_state;
}
Ok(true)
}
#[cfg(feature = "std")]
fn check_block_zone_entry(
recorded_map: &crate::table::zone_map::ZoneMap,
block: &DecodedBlock,
) -> crate::Result<()> {
let Some(recorded) = recorded_map.columns_for(block.handle.offset().0) else {
return Err(crate::Error::InvalidHeader(
"zone_map is missing a data block's entry",
));
};
#[cfg(feature = "columnar")]
if let Some(batch) = &block.batch {
if batch.row_count == 0 {
return Err(crate::Error::InvalidHeader(
"columnar data block decodes to zero rows",
));
}
if recorded != batch.zone_stats().as_slice() {
return Err(crate::Error::InvalidHeader(
"zone_map disagrees with the columnar block's per-column statistics",
));
}
return Ok(());
}
Self::verify_row_block_zone_entry(recorded, block)
}
/// Cross-checks a ROW data block's recorded zone-map entry: it must be
/// exactly one synthetic whole-block column (`column_id == 0`, zero type /
/// codec / null fields) whose `min` / `max` / `row_count` equal the block's
/// decoded key range and row count.
///
/// Authenticates the column's IDENTITY, not just its key bounds. A
/// re-stamped map that keeps the checked min / max / `row_count` but changes
/// the id to a consumer value-column id would let
/// [`ColumnRangePredicate::can_skip_block`](crate::table::columnar_predicate::ColumnRangePredicate::can_skip_block)
/// read those key bounds as value-column statistics and skip blocks holding
/// matching rows. Split out of [`Self::check_block_zone_entry`] so the
/// columnar path can authenticate its per-column entry instead.
#[cfg(feature = "std")]
fn verify_row_block_zone_entry(
recorded: &[crate::table::zone_map::ColumnStats],
block: &DecodedBlock,
) -> crate::Result<()> {
let [col] = recorded else {
return Err(crate::Error::InvalidHeader(
"zone_map block does not carry exactly one synthetic column",
));
};
if col.column_id != 0 || col.type_tag != 0 || col.codec_id != 0 || col.null_count != 0 {
return Err(crate::Error::InvalidHeader(
"zone_map synthetic column identity disagrees with the \
writer's whole-block column (id / type / codec / null)",
));
}
let (Some(first), Some(last)) = (block.entries.first(), block.entries.last()) else {
return Err(crate::Error::InvalidHeader(
"row data block decodes to zero entries",
));
};
if col.min != first.key.user_key.as_ref()
|| col.max != last.key.user_key.as_ref()
|| col.row_count as usize != block.entries.len()
{
return Err(crate::Error::InvalidHeader(
"zone_map disagrees with the block's decoded key range or row count",
));
}
Ok(())
}
/// The sidecar write [`Self::write_restrict_sidecar`] makes, for
/// [`crate::restrict_bound::write_many`] to publish with the other tables a
/// slice restricts, under the same ordering.
#[cfg(feature = "std")]
pub(crate) fn restrict_sidecar_write<'a>(
&'a self,
bound: &'a [u8],
) -> crate::restrict_bound::SidecarWrite<'a> {
crate::restrict_bound::SidecarWrite {
fs: &*self.fs,
table_path: &self.path,
encryption: self.encryption.as_deref(),
table_id: self.metadata.id,
bound,
}
}
/// Publishes this table's tight-space restriction lower bound to its
/// `.restrict-bound` sidecar, so manifest repair recovers the exact bound
/// WITHOUT the SST itself being mutated (which would invalidate the whole-file
/// checksum the manifest still holds for it). MUST be called STRICTLY AFTER the
/// slice's version install commits and BEFORE the prefix is hole-punched: the
/// post-commit ordering makes a sidecar on disk always denote a committed
/// restriction (so repair honors it without a commit protocol), and writing it
/// before the punch gives every punched input a recoverable exact bound. The
/// atomic `temp + rename` write leaves the sidecar fully present or absent
/// across a crash, beside an untouched SST.
///
/// # Errors
///
/// Propagates encryption / filesystem failures from the atomic sidecar write.
#[cfg(feature = "std")]
pub(crate) fn write_restrict_sidecar(
&self,
bound: &[u8],
sync_mode: crate::fs::SyncMode,
) -> crate::Result<()> {
crate::restrict_bound::write(
&*self.fs,
&self.path,
self.encryption.as_deref(),
self.metadata.id,
bound,
sync_mode,
)
}
/// Reads one data block's RAW on-disk bytes through `file` and reports
/// whether they are all zero — the signature of a hole-punched (reclaimed)
/// block. A short all-zero prefix alone is NOT proof (a corrupt block could
/// begin with zeros), so a zero opening window falls through to a full-extent
/// read; a nonzero window proves the block intact without reading the rest
/// (a real block's header and first entry bytes are never all zero).
///
/// `on_read` is told the length of each read before it is issued, so a
/// read path can charge the probe to its byte counters.
///
/// # Errors
///
/// Propagates the positioned read failure.
#[cfg(feature = "std")]
fn block_is_zeroed_in(
file: &dyn crate::fs::FsFile,
block_handle: &BlockHandle,
on_read: &mut dyn FnMut(u64),
) -> crate::Result<bool> {
const WINDOW: usize = 64;
let size = block_handle.size() as usize;
let window = size.min(WINDOW);
on_read(window as u64);
let head = crate::file::read_exact(file, block_handle.offset().0, window)?;
if head.iter().any(|&b| b != 0) {
return Ok(false);
}
if size <= window {
return Ok(true);
}
on_read(size as u64);
let bytes = crate::file::read_exact(file, block_handle.offset().0, size)?;
Ok(bytes.iter().all(|&b| b == 0))
}
/// Whether ANY of this table's data blocks is hole-punched (reads as zeros)
/// AND the file carries the physical hole to prove it.
///
/// This is the punch test for the case where the sidecar bound itself could
/// not be read: a punched data block means the SST genuinely lost data to a
/// reclaim (bound lost → derive one from the geometry), while a fully intact
/// data section means an unpunched file. EVERY block is inspected, not just
/// the first: the punch-on-drop reclaim continues past an individual
/// `punch_hole` failure, so a partially-punched SST can keep its first block
/// intact while later prefix blocks are zeroed — a first-block-only probe
/// would misread that file as unpunched and recover it unrestricted, routing
/// later reads into the zeroed blocks.
///
/// Zeros alone are NOT the evidence, because corruption that destroys the
/// leading data block leaves the same read-as-zeros shape a completed punch
/// does, and reading it as a reclaim would drop that block plus the
/// sub-bound rows of the first readable one while reporting the table
/// recovered. A punch physically deallocates, so the zeroed region must
/// carry an actual hole. The probe is run-local on purpose: consecutive
/// zeroed blocks coalesce into one run (bounded by readable blocks, so the
/// hole is attributed to the zeros and not to the file at large — a
/// file-wide allocation total attributes nothing, and on a filesystem with
/// transparent compression an ordinary, fully-written file already reports
/// fewer physical bytes than its length), and the run is asked whether it
/// CONTAINS a hole rather than whether it wholly IS one: the reclaim
/// punches at unaligned extents (the prefix ends mid-page, and a block
/// edge between two calls need not fall on a page of this filesystem), and the
/// filesystem deallocates only the wholly-contained pages while
/// zero-filling the edges, so a whole-extent probe would reject genuine
/// punches.
///
/// A backend that cannot answer the hole probe (`None`) leaves the punch
/// UNPROVEN, which is a distinct verdict from "unpunched": on a mount
/// that CAN punch, zeroed runs the probe cannot attribute are
/// indistinguishable from a lost-sidecar reclaim, and reading them as
/// "no punch" would publish the table unrestricted — resurrecting the
/// boundary block's rows below the committed restriction. On a mount
/// that cannot punch at all, zeros were never a reclaim and stay
/// classified as damage.
///
/// # Errors
///
/// Propagates the open, block-index, positioned-read or hole-probe failure.
#[cfg(feature = "std")]
pub(crate) fn punch_geometry(&self) -> crate::Result<PunchGeometry> {
let file = self.fs.open(&self.path, &FsOpenOptions::new().read(true))?;
// [start, end) of the current run of consecutive zeroed blocks.
let mut run: Option<(u64, u64)> = None;
let mut unattributed = false;
let mut punched = false;
let mut probe = |start: u64, end: u64| -> crate::Result<bool> {
let answer = self
.fs
.extent_contains_hole(&self.path, start, end - start)?;
if answer.is_none() {
unattributed = true;
}
Ok(answer == Some(true))
};
// The geometry every classifier used to re-derive on its own pass.
let mut first_readable: Option<(usize, UserKey)> = None;
let mut after_last_zeroed: Option<usize> = None;
let mut irregular = false;
for (i, handle) in self.maintenance_index_walk().enumerate() {
let handle = handle?;
let block = *handle.as_ref();
// A recovery walk over the file's geometry, not a read a caller
// asked for, so it charges nothing to the read counters.
if Self::block_is_zeroed_in(&*file, &block, &mut |_| {})? {
// A readable block BELOW a zeroed one is positive evidence of
// a punch that failed mid-reclaim.
if first_readable.is_some() {
irregular = true;
}
// The greedy (resurrection) anchor only counts blocks that
// follow the LAST zeroed one, so a zeroed block resets it.
after_last_zeroed = None;
let start = block.offset().0;
let end = start + u64::from(block.size());
run = Some(match run {
Some((run_start, _)) => (run_start, end),
None => (start, end),
});
continue;
}
if first_readable.is_none() {
first_readable = Some((i, handle.end_key().clone()));
}
if after_last_zeroed.is_none() {
after_last_zeroed = Some(i);
}
if let Some((start, end)) = run.take()
&& probe(start, end)?
{
punched = true;
}
}
if let Some((start, end)) = run
&& probe(start, end)?
{
punched = true;
}
let verdict = if punched {
PunchProbe::Punched
} else if unattributed && self.fs.capabilities(&self.path).punch_hole {
PunchProbe::Unproven
} else {
PunchProbe::Unpunched
};
Ok(PunchGeometry {
verdict,
first_readable,
after_last_zeroed,
irregular,
})
}
/// The conservative restriction bound derived from the PHYSICAL punch alone,
/// for a punched SST whose exact `.restrict-bound` sidecar is not trustworthy
/// (lost / corrupt / unbacked) and whose manifest restriction is also gone.
///
/// Walks the data blocks in key order and classifies the punch geometry.
///
/// A CLEAN zeroed prefix (every zeroed block precedes every readable one,
/// starting at the first data block — the pattern a fully successful
/// punch-on-drop reclaim leaves) yields
/// [`DerivedRestriction::Bound`]: the END key of the first readable block.
/// Restricting to that key drops the punched prefix AND the first readable
/// block, which may STRADDLE the true (mid-block) bound: since the end key
/// is that block's maximum, it is at or above the true bound, so every
/// served key is live and NO superseded key is resurrected. The cost is at
/// most that one block's live suffix, which is exactly the trade the
/// resurrection flag governs; the resurrection path keeps the whole
/// readable region instead.
///
/// An IRREGULAR pattern — any readable block BELOW a zeroed one — is
/// positive evidence that individual `punch_hole` calls failed mid-reclaim
/// (the reclaim logs and continues per block). Then ANY readable block may
/// equally be an intact-but-consumed block whose punch also failed, and no
/// geometry bound can separate consumed from live: anchoring anywhere
/// either resurrects superseded rows or discards live ones. That is
/// [`DerivedRestriction::IrregularPunch`] — the caller sets the table
/// aside (the resurrection path, which accepts re-exposure by contract,
/// still derives greedily instead). The punch-on-drop reclaim punches
/// top-down and stops at its first failure precisely so any failure lands
/// in the DETECTABLE irregular class; the only invisible case is a reclaim
/// whose very first punch failed (no hole at all — zero evidence), which
/// reads as an unpunched table here.
///
/// [`DerivedRestriction::NoLiveData`] when EVERY block reads as zeros: no
/// live data survives the punch, so the caller excludes the table (no
/// recoverable live data to lose).
///
/// # Errors
///
/// Propagates a block-index or positioned-read failure.
#[cfg(feature = "std")]
pub(crate) fn conservative_restriction(geometry: &PunchGeometry) -> DerivedRestriction {
if geometry.irregular {
return DerivedRestriction::IrregularPunch;
}
match &geometry.first_readable {
Some((_, end_key)) => DerivedRestriction::Bound(end_key.clone()),
None => DerivedRestriction::NoLiveData,
}
}
/// The GREEDY counterpart of [`conservative_restriction`](Self::conservative_restriction)
/// for RESURRECTION mode: returns the FIRST (lowest) key of the first
/// readable block after the LAST zeroed one, so restricting to it keeps the
/// WHOLE straddling block — resurrecting its sub-bound keys — while still
/// excluding every punched (zeroed) block below it. Returning the punched
/// table UNRESTRICTED instead would route a read to a zeroed,
/// physically-missing block and fail after a supposedly successful repair;
/// anchoring before the last zeroed block (a partial punch can leave intact
/// blocks interleaved with zeroed ones) would do the same. Wholly-empty
/// readable blocks (e.g. a columnar block fully masked by its delete bitmap)
/// are skipped. `None` when no readable non-empty block follows the last
/// zeroed one.
///
/// # Errors
///
/// Propagates a block-index, positioned-read, or block-decode failure.
#[cfg(feature = "std")]
pub(crate) fn greedy_restriction_bound(
&self,
geometry: &PunchGeometry,
) -> crate::Result<Option<UserKey>> {
let Some(start) = geometry.after_last_zeroed else {
return Ok(None);
};
// Every block from here on is readable (a zeroed one would have reset
// the anchor), so this only walks forward past WHOLLY EMPTY blocks —
// e.g. a columnar block fully masked by its delete bitmap, which
// carries no key to anchor on.
for (i, handle) in self.maintenance_index_walk().enumerate() {
let handle = handle?;
if i < start {
continue;
}
let bh = *handle.as_ref();
if let Some(db) = self.load_data_block_charged(&bh, ReadCharge::Maintenance)?
&& let Some(first) = db.first_user_key(self.comparator.clone())?
{
return Ok(Some(first));
}
}
Ok(None)
}
/// Reads the `locator` section FROM DISK and pairs it with the ordinal →
/// handle map (the writer's `block_id` order is the index order), mirroring
/// the open path. `None` when the table has no locator.
///
/// Read from the file rather than taken from the open table because a
/// checksum- and parity-consistent forged locator is accepted by the
/// out-of-band walk on its block role alone, while `point_read_inner`
/// trusts its answer and reads the addressed block directly.
///
/// # Errors
///
/// Propagates the section decode and the index walk, and rejects a block
/// that does not carry the locator role.
#[cfg(feature = "std")]
fn read_locator_section(
&self,
file: &dyn crate::fs::FsFile,
regions: ®ions::ParsedRegions,
) -> crate::Result<Option<crate::table::locator::LoadedLocator>> {
let Some(loc_handle) = regions.locator else {
return Ok(None);
};
let block = Block::from_file(
file,
loc_handle,
crate::table::block::BlockIdentity {
table_id: self.metadata.id,
block_type: BlockType::Locator,
dict_id: 0,
window_log: 0,
},
&self.section_transform(),
)?;
if block.header.block_type != BlockType::Locator {
return Err(crate::Error::InvalidTag((
"BlockType",
block.header.block_type.into(),
)));
}
// Verification, not a caller's read: like every other gate's walk it
// stays out of the read counters and leaves the block cache alone.
let blocks: Vec<BlockHandle> = self
.untraced_index_walk()
.map(|r| r.map(|kbh| *kbh.as_ref()))
.collect::<crate::Result<Vec<_>>>()?;
Ok(Some(crate::table::locator::LoadedLocator::new(
block.data, blocks,
)))
}
/// Cross-checks the recorded `locator` section against the ACTUAL
/// key → newest-version-block mapping the decode yields. A locator
/// redirected from a key's newest-version block to a LATER block holding an
/// OLDER version returns that stale value without falling back to the
/// sorted index, so any key whose locator answer points at a different
/// block than the decode does is corruption.
///
/// `seen` carries ACROSS blocks: the first block holding a user key holds
/// its newest version (blocks are sorted by key then descending seqno), so
/// that block is the locator's expected answer and a later block's older
/// versions must not overwrite the expectation.
///
/// `key_hashes[i]` is the hash of `block.entries[i]`'s key.
#[cfg(feature = "std")]
fn check_block_locator(
&self,
locator: &crate::table::locator::LoadedLocator,
seen: &mut crate::HashSet<Vec<u8>>,
block: &DecodedBlock,
key_hashes: &[u64],
) -> crate::Result<()> {
{
let block_handle = block.handle;
// Columnar blocks carry no in-block slot semantics (rows are
// reconstructed on load), so there the locator is per-block and
// only the block-id is checked.
let row_block = block.row.as_ref();
debug_assert_eq!(key_hashes.len(), block.entries.len());
for (entry, &key_hash) in block.entries.iter().zip(key_hashes) {
let user_key: &[u8] = &entry.key.user_key;
// Copied only when first seen: the set outlives the block.
if seen.contains(user_key) {
continue;
}
seen.insert(user_key.to_vec());
// The writer OMITS the locator section when it cannot build one
// and otherwise encodes EVERY unique key, so a present locator
// that gives NO answer for a decoded key is a forgery — e.g. a
// delete_bitmap relabeled/re-roled to a locator that resolves
// nothing. The read path falls back to the sorted index on a
// miss, but a verifier deciding whether to trust the bytes must
// treat the unanswered key as corrupt (otherwise the relabel
// records no degradation and the deleted rows come back live).
let Some((located, hint)) = locator.locate_block(key_hash)? else {
return Err(crate::Error::InvalidHeader(
"locator gives no answer for a decoded key it should resolve",
));
};
if located.offset() != block_handle.offset() {
return Err(crate::Error::InvalidHeader(
"locator resolves a key to a block other than its newest-version block",
));
}
// A `Restart` / `Entry` slot hint sends `point_read_at_slot`
// straight to that in-block position: a checksum-clean
// locator can keep the right block id yet redirect the slot
// to a later restart interval holding an OLDER version of a
// multi-version key, and the read returns the stale value
// without falling back to the sorted index. Probe the hint
// and require the NEWEST version (the first decoded entry for
// the key, which is `entry` here since `located` == this
// block). A `None` hint (per-block precision) has no slot to
// validate.
if let (Some((slot, is_entry)), Some(block)) = (hint, row_block) {
let found = block.point_read_at_slot(
slot,
is_entry,
user_key,
crate::seqno::MAX_SEQNO,
&self.comparator,
)?;
let disagrees = match &found {
Some(v) => {
v.key.seqno != entry.key.seqno
|| v.key.value_type != entry.key.value_type
|| v.value != entry.value
}
None => true,
};
if disagrees {
return Err(crate::Error::InvalidHeader(
"locator slot hint does not resolve a key's newest version",
));
}
}
}
}
Ok(())
}
/// Confirms every decoded key is RETRIEVABLE through its own block's
/// in-block indexes, and that the internal-key sort order holds.
///
/// Judged on the DISK-FRESH bytes the walk decoded. A data block's embedded
/// HASH INDEX is checksum-clean to the out-of-band walk even when a bucket
/// was re-stamped to `MARKER_FREE`: the sequential decode gates still see
/// every entry, but `point_read` trusts the index and returns `None` for
/// the affected keys. Each block is probed through ITS OWN `point_read` —
/// the table-level probe this replaces went through the recovery-time
/// in-memory index and the block cache, so a pristine cached copy masked
/// the on-disk forge. Filter, locator and TLI misdirection have their own
/// disk-fresh gates.
///
/// A columnar block has no in-block key index to probe (rows are
/// reconstructed on load), but the GLOBAL order (user key ASC, then seqno
/// DESC per key) is still an invariant the read path relies on:
/// `column_batch_match_entries` binary-searches the key column assuming it
/// is sorted, so a checksum-restamped block with reordered keys — which
/// every other columnar check tolerates — would make point reads miss a key
/// or return a stale version.
///
/// `prev_internal` carries the last decoded internal key ACROSS blocks,
/// which is what makes the order check global rather than per-block: a
/// restamped later block must not raise the seqno of a key that also ends
/// the preceding block, since both would decode and probe cleanly on their
/// own while a later compaction persisted the stale version.
#[cfg(feature = "std")]
fn check_block_reachability(
&self,
prev_internal: &mut Option<(UserKey, SeqNo)>,
block: &DecodedBlock,
) -> crate::Result<()> {
{
// A block whose keys all resolve through its HASH index never
// exercises the binary index in the probes below, yet range
// seeks still trust it — authenticate the pointers directly.
// A columnar block has no in-block index to probe: rows are
// reconstructed on load, so only the sort order is enforced there.
if let Some(data_block) = &block.row {
data_block.verify_binary_index()?;
}
let data_block = block.row.as_ref();
// A key's versions are adjacent within the block (sorted by key
// then descending seqno), so the FIRST occurrence is the newest
// and one probe per distinct key suffices.
let mut prev_key: Option<UserKey> = None;
for entry in block.entries.iter().cloned() {
// Enforce the global sort order on EVERY entry (before the
// per-key dedup below), tracking the previous entry across block
// boundaries: the user key must strictly increase, or (for the
// same user key) the seqno must strictly decrease.
if let Some((pk, ps)) = &prev_internal {
let out_of_order = match self.comparator.compare(&entry.key.user_key, pk) {
core::cmp::Ordering::Greater => false,
core::cmp::Ordering::Less => true,
core::cmp::Ordering::Equal => entry.key.seqno >= *ps,
};
if out_of_order {
// Same invariant, but the two block layouts report it
// in their own words: a columnar table has no
// point_read to fall back on, so its message names the
// layout that produced the disorder.
return Err(crate::Error::InvalidHeader(if data_block.is_some() {
"data-block entries are out of order (a user key decreased, or an \
equal key's seqno did not strictly decrease) across the walk"
} else {
"columnar entries are out of order (a user key decreased, or an \
equal key's seqno did not strictly decrease) across the walk"
}));
}
}
*prev_internal = Some((entry.key.user_key.clone(), entry.key.seqno));
// Columnar blocks have no in-block index, so the sort-order
// enforcement above is the whole check for them.
let Some(data_block) = data_block else {
continue;
};
if prev_key
.as_ref()
.is_some_and(|p| crate::comparator::same_user_key(p, &entry.key.user_key))
{
continue;
}
// Require the probe to return the NEWEST version, not merely
// SOME version. A key spanning restart intervals has a
// conflict-marked hash bucket; re-stamping that bucket to a
// later interval holding an OLDER version still yields
// `Some`, so an `is_none` check would pass — yet point reads
// after reopen would return the stale value. Match the
// decoded newest entry's seqno, value type, and bytes.
let found = data_block.point_read(
entry.key.user_key.as_ref(),
crate::seqno::MAX_SEQNO,
&self.comparator,
)?;
let disagrees = match &found {
Some(v) => {
v.key.seqno != entry.key.seqno
|| v.key.value_type != entry.key.value_type
|| v.value != entry.value
}
None => true,
};
if disagrees {
return Err(crate::Error::InvalidHeader(
"a decoded key's point_read does not return its newest version \
(an in-block index disagrees with the entries)",
));
}
prev_key = Some(entry.key.user_key);
}
}
Ok(())
}
/// Whether the salvage-mode open degraded a REBUILDABLE side section
/// (filter / `filter_tli`, seqno bounds, zone map, locator) because its
/// block did not decode as the claimed type — see
/// [`Inner::rebuildable_section_degraded`](crate::table::inner::Inner). Used
/// by salvage to fail closed on a table whose degraded section may be a
/// relabeled deletion it would otherwise discard and resurrect.
///
/// `std`-gated because its only consumer is the salvage path (`std`-only).
#[cfg(feature = "std")]
pub(crate) fn salvage_degraded_a_rebuildable_section(&self) -> bool {
self.rebuildable_section_degraded
}
/// Reads the `filter` section and everything a probe of it needs, so the
/// combined reconcile pass builds the state once and then probes it with
/// the shared decode. `None` for a table without a filter.
///
/// Read from the file rather than taken from the open table because a
/// checksum- and parity-consistent forged filter is accepted by the
/// out-of-band walk on its framing and role alone — the walk never probes
/// it — while `check_bloom` trusts it to SKIP point reads.
///
/// # Errors
///
/// Propagates the section reads, and rejects a present-but-empty filter on
/// a table that has data blocks.
#[cfg(feature = "std")]
fn filter_probe<'a>(
&'a self,
file: &'a dyn crate::fs::FsFile,
regions: ®ions::ParsedRegions,
prefix_extractor: Option<&alloc::sync::Arc<dyn crate::prefix::PrefixExtractor>>,
) -> crate::Result<Option<FilterProbe<'a>>> {
// Re-read the filter FROM DISK: the open path PINS the filter (or
// its partition index) in memory at recover time, so an on-disk
// re-stamp after the open (the very forge this check exists for)
// would be invisible to `check_bloom`. An unreadable filter is an
// error here (the caller is deciding whether to trust the bytes),
// unlike the read path's permissive empty-payload sentinel.
if regions.filter.is_none() && regions.filter_tli.is_none() {
return Ok(None);
}
let filter_transform = self.section_transform();
// Partitioned mode: the partition index maps a key to its filter
// block. Loaded from disk for the same reason as the filter itself.
let filter_index = if let Some(idx_handle) = regions.filter_tli {
let block = Block::from_file(
file,
idx_handle,
crate::table::block::BlockIdentity {
table_id: self.metadata.id,
block_type: BlockType::Index,
dict_id: 0,
window_log: 0,
},
&{
let t = crate::table::block::BlockTransform::from_parts(
self.metadata.index_block_compression,
self.encryption.as_deref(),
#[cfg(zstd_any)]
None,
)?;
if let Some(ecc) = self.metadata.ecc_params {
t.with_ecc(ecc)
} else {
t
}
},
)?;
if block.header.block_type != BlockType::Index {
return Err(crate::Error::InvalidTag((
"BlockType",
block.header.block_type.into(),
)));
}
let idx = IndexBlock::new(block);
idx.try_iter(self.comparator.clone())?;
Some(idx)
} else {
None
};
let full_filter = if filter_index.is_none() {
regions
.filter
.map(|h| Self::load_filter_block(file, &filter_transform, self.metadata.id, h))
.transpose()?
} else {
None
};
// A present full filter whose payload decodes to the empty "no filter"
// sentinel is a forgery on a table with data blocks: the writer omits
// the section entirely when filtering is disabled, so it never emits a
// present-but-empty full filter. Left unrejected, the read-path probe
// below reports Ok(true) for EVERY key (the permissive empty sentinel),
// so a delete_bitmap renamed and re-roled to an empty filter passes the
// whole check, the SST is kept, and reopening resurrects the rows the
// hidden bitmap deleted.
if let Some(filter) = &full_filter
&& filter.is_empty()
&& self.untraced_index_walk().next().is_some()
{
return Err(crate::Error::InvalidHeader(
"filter section is present but empty on a table with data blocks",
));
}
// A restricted view's punched prefix blocks decode to zeros; the walk
// skips them. Their keys are superseded, so the live filter is only
// obligated to report the suffix keys present.
Ok(Some(FilterProbe {
table: self,
file,
transform: filter_transform,
index: filter_index,
full: full_filter,
// Partition blocks are shared by many keys; memoized by file offset
// so the probe reads each partition once.
partitions: alloc::collections::BTreeMap::new(),
prev_key: None,
// Only the extractor the filter was built under emitted the
// prefixes it holds: probed under another, a healthy filter
// reports them absent.
prefix_extractor: prefix_extractor
.filter(|extractor| {
self.metadata.prefix_extractor.as_deref() == Some(extractor.name().as_bytes())
})
.cloned(),
}))
}
/// Every key the block decodes must be reported POSSIBLY PRESENT by the
/// on-disk filter. A false positive is unprovable (it is the filter's
/// normal error mode), but a false NEGATIVE on an existing key is
/// corruption by construction: it silently disappears the key from every
/// read that consults the filter first.
#[cfg(feature = "std")]
fn check_filter_block(
probe: &mut FilterProbe<'_>,
block: &DecodedBlock,
key_hashes: &[u64],
) -> crate::Result<()> {
{
let table = probe.table;
debug_assert_eq!(key_hashes.len(), block.entries.len());
for (entry, &key_hash) in block.entries.iter().zip(key_hashes) {
// Blocks are sorted by key then descending seqno, so a key's
// older versions are always adjacent — one probe per key.
if probe
.prev_key
.as_deref()
.is_some_and(|p| crate::comparator::same_user_key(p, &entry.key.user_key))
{
continue;
}
let user_key: &[u8] = &entry.key.user_key;
let maybe_present = if let Some(idx) = &probe.index {
let mut iter = idx.iter(table.comparator.clone());
iter.seek(user_key, crate::seqno::MAX_SEQNO);
let Some(part_handle) = iter.next() else {
// A key past the last partition is a definite miss on
// the read path — a false negative by construction.
return Err(crate::Error::InvalidHeader(
"filter partition index does not cover an existing key",
));
};
let part_handle = part_handle.materialize(idx.as_slice()).into_inner();
let filter = match probe.partitions.entry(part_handle.offset().0) {
alloc::collections::btree_map::Entry::Occupied(e) => e.into_mut(),
alloc::collections::btree_map::Entry::Vacant(e) => {
e.insert(Self::load_filter_block(
probe.file,
&probe.transform,
table.metadata.id,
part_handle,
)?)
}
};
// The partition index only addresses partitions the writer
// filled with keys, so a partition that decodes to the empty
// "no filter" sentinel yet is sought for an existing key is a
// forgery (a delete_bitmap relabeled to an empty filter
// partition under a re-stamped filter_tli). Left unrejected,
// the probe below reports Ok(true) permissively for every
// key and the relabel passes as a filter-less table.
if filter.is_empty() {
return Err(crate::Error::InvalidHeader(
"filter partition is present but empty for an existing key",
));
}
filter.maybe_contains_hash(key_hash)?
} else if let Some(filter) = &probe.full {
filter.maybe_contains_hash(key_hash)?
} else {
true
};
if !maybe_present {
return Err(crate::Error::InvalidHeader(
"filter reports an existing key as definitely absent",
));
}
// A full filter also indexes every PREFIX the extractor
// emits for a key, and `maybe_contains_prefix` trusts it to
// SKIP whole tables on a prefix scan. A filter rebuilt from
// complete-key hashes alone (a salvage without the extractor)
// passes the key probe above yet turns every prefix into a
// false negative — the table silently vanishes from prefix
// scans. Probe each emitted prefix hash too. Partitioned
// filters stay conservative (their prefix probe is
// deliberately best-effort), so this runs only for a full
// filter with a configured extractor.
if let (Some(filter), Some(extractor)) = (&probe.full, &probe.prefix_extractor) {
for prefix in extractor.prefixes(user_key) {
let prefix_hash = crate::hash::hash64(prefix);
if !filter.maybe_contains_hash(prefix_hash)? {
return Err(crate::Error::InvalidHeader(
"filter reports an existing key's prefix as definitely absent",
));
}
}
}
probe.prev_key = Some(entry.key.user_key.clone());
}
Ok(())
}
}
/// Reads one filter block (full or partition) from the already-open file,
/// rejecting a block that does not carry the filter role.
#[cfg(feature = "std")]
fn load_filter_block(
file: &dyn crate::fs::FsFile,
transform: &crate::table::block::BlockTransform<'_>,
table_id: crate::TableId,
handle: BlockHandle,
) -> crate::Result<crate::table::filter::block::FilterBlock> {
let block = Block::from_file(
file,
handle,
crate::table::block::BlockIdentity {
table_id,
block_type: BlockType::Filter,
dict_id: 0,
window_log: 0,
},
transform,
)?;
if block.header.block_type != BlockType::Filter {
return Err(crate::Error::InvalidTag((
"BlockType",
block.header.block_type.into(),
)));
}
Ok(crate::table::filter::block::FilterBlock::new(block))
}
/// Reads the meta (and range-tombstone) sections the bounds check judges
/// against, and seeds the accumulators the walk fills, so the combined
/// reconcile pass builds the state once and feeds it the shared decode.
///
/// Read from the file rather than taken from the open table because both
/// meta mirrors re-stamped CONSISTENTLY (fresh checksums and parity) pass
/// every byte-level check and the mirror comparison, while run selection
/// trusts `key#min`/`key#max` to route reads AROUND this table — a narrowed
/// range silently hides real keys (and the range tombstones that mask older
/// tables).
///
/// `bitmap_digest_authenticated`: whether the caller has ALREADY
/// authenticated this file's bytes — bitmap section included — against a
/// matching manifest digest (the directly attributable heal path: the
/// pre-heal digest probed equal, so the file differs solely by that
/// pass's corrections). Only then may a LEGACY table whose bitmap
/// carries no `descriptor#delete_bitmap_hash` pass; repair has no such
/// digest and must keep failing closed on it. Carried on the probe for the
/// delete-bitmap gate in [`Self::finish_meta_bounds`], which exists only on
/// columnar builds (a positional delete bitmap is a columnar-layout
/// section).
///
/// # Errors
///
/// Propagates the section reads and rejects a disk-fresh meta that
/// disagrees with the recovery-time descriptor.
#[cfg(feature = "std")]
fn meta_bounds_probe(
&self,
file: &dyn crate::fs::FsFile,
regions: ®ions::ParsedRegions,
bitmap_digest_authenticated: bool,
) -> crate::Result<MetaBoundsProbe> {
// Tail-first with MID fallback, mirroring recovery: the next open
// trusts the same copy, and a table living off its intact MID mirror
// (unreadable forged tail) must be judged by THAT copy's bounds.
let meta = match crate::table::meta::ParsedMeta::load_with_handle(
file,
®ions.metadata,
Some(self.metadata.id),
self.encryption.as_deref(),
) {
Ok(meta) => meta,
Err(tail_err) => {
let Some(mid_handle) = regions.metadata_mid else {
return Err(tail_err);
};
crate::table::meta::ParsedMeta::load_with_handle(
file,
&mid_handle,
Some(self.metadata.id),
self.encryption.as_deref(),
)?
}
};
// Nothing on the heal path legitimately rewrites the meta sections,
// so the disk-fresh copy must equal the recovery-time one FIELD FOR
// FIELD. A single-field comparison is not enough: any descriptor
// re-stamped consistently in both mirrors passes the mirror walk,
// and fields no gate can re-derive from the entries stay
// authoritative-only — `descriptor#kv_checksum` flipped to `None`
// makes every reader misread footer bytes as the block trailer
// after reopen, and a back-dated `created_at` lets FIFO compaction
// drop the live SST as TTL-expired. The recovery-time copy is
// trustworthy (the blocks decoded under it at open), so any
// disk-fresh divergence is a post-open re-stamp.
if meta != self.metadata {
return Err(crate::Error::InvalidHeader(
"disk-fresh meta disagrees with the recovery-time copy",
));
}
// Load the range tombstones UP FRONT: the synthetic sentinel an RT-ONLY
// table records is derived from them and must be EXCLUDED from the KV
// seqno bounds below (its seqno is RT-derived and lies outside the
// recorded KV range), and the same list feeds the key-range coverage
// check further down.
let tombstones = if let Some(rt_handle) = regions.range_tombstones {
let block = Block::from_file(
file,
rt_handle,
crate::table::block::BlockIdentity {
table_id: self.metadata.id,
block_type: BlockType::RangeTombstone,
dict_id: 0,
window_log: 0,
},
&self.section_transform(),
)?;
if block.header.block_type != BlockType::RangeTombstone {
return Err(crate::Error::InvalidTag((
"BlockType",
block.header.block_type.into(),
)));
}
Some(Self::decode_range_tombstones(
&block,
self.comparator.as_ref(),
)?)
} else {
None
};
// The `(start, seqno)` of the synthetic sentinel entry (a weak tombstone
// at the (seqno, start)-minimal tombstone), or `None` when the table is
// not RT-only. Excluded ONCE from the decoded KV seqno bounds below.
//
// Only an RT-ONLY table (no real KV items) carries a synthetic sentinel,
// written SOLELY to give an otherwise KV-empty range-tombstone table one
// index entry (the writer's `finish`), so its `item_count` is exactly 1.
// Gate on that — NOT on a seqno comparison against `highest_kv_seqno`: the
// sentinel exclusion feeds the seqno-bounds check below, and
// `highest_kv_seqno` is precisely the field that check validates, so using
// it to identify the sentinel is circular — an attacker who lowers a
// re-stamped `highest_kv_seqno` below a REAL weak tombstone would make it
// look synthetic, exclude it, and slip the forged bound past. `item_count`
// is instead cross-checked against the DECODED entry count below, so a
// forged `item_count == 1` on a multi-entry table is rejected there; a
// genuine single-real-KV table whose sole weak tombstone matches the
// RT-minimal `(key, seqno)` is unreachable (the covering RT would have to
// start at that sole key and end past it, exceeding the one-key range the
// coverage check enforces, and an empty point tombstone is rejected at
// decode), so this never wrongly excludes a real entry.
let sentinel = tombstones
.as_deref()
.filter(|_| meta.item_count == 1)
.and_then(crate::range_tombstone::RangeTombstone::sentinel)
.map(|(k, s)| (k.clone(), s));
// A restricted (tight-space) view's `[0, punch)` prefix blocks are
// punched to zeros: the index still LISTS them (so counting index
// entries yields the whole-table block count, which `meta` records),
// but they no longer DECODE. Count every block toward `data_block_count`
// without decoding, and aggregate entries / keys / seqnos over the live
// suffix only. `meta.item_count` describes the whole table, so its
// exact-equality check relaxes to a subset check for a restricted view.
let restricted = self.restrict_lower_bound().is_some();
// `data_block_count` describes the WHOLE table, punched prefix
// included, so it is counted off the index itself — no decode.
let mut block_count: u64 = 0;
for handle in self.untraced_index_walk() {
handle?;
block_count = block_count
.checked_add(1)
.ok_or(crate::Error::InvalidHeader("meta bounds"))?;
}
Ok(MetaBoundsProbe {
meta,
tombstones,
sentinel,
sentinel_excluded: false,
restricted,
block_count,
bitmap_digest_authenticated,
count: 0,
first_key: None,
last_key: None,
seqno_lo: None,
seqno_hi: None,
})
}
/// Accumulates the decoded entry count, key range and seqno range that
/// [`Self::finish_meta_bounds`] judges the recorded bounds against.
#[cfg(feature = "std")]
fn observe_meta_bounds(probe: &mut MetaBoundsProbe, block: &DecodedBlock) -> crate::Result<()> {
{
let sentinel = probe.sentinel.clone();
let count = &mut probe.count;
let first_key = &mut probe.first_key;
let last_key = &mut probe.last_key;
let seqno_lo = &mut probe.seqno_lo;
let seqno_hi = &mut probe.seqno_hi;
let sentinel_excluded = &mut probe.sentinel_excluded;
let entries = &block.entries;
*count = count
.checked_add(entries.len() as u64)
.ok_or(crate::Error::InvalidHeader("meta bounds"))?;
if first_key.is_none() {
*first_key = entries.first().map(|e| e.key.user_key.clone());
}
if let Some(last) = entries.last() {
*last_key = Some(last.key.user_key.clone());
}
for entry in entries {
let s = entry.key.seqno;
// The MINIMUM counts every decoded entry, sentinel included.
// `seqno#min` is written over range tombstones as well as KV
// entries, so a genuine RT-only table's sentinel carries a
// seqno the recorded minimum already covers and cannot trip the
// check. Excluding it here instead would hand an adversary the
// whole cross-check: with `key#max` re-stamped to admit an RT
// that starts at a one-entry table's sole key, the table's only
// real seqno looks synthetic, drops out, and leaves nothing to
// contradict a raised `seqno#min`.
*seqno_lo = Some(seqno_lo.map_or(s, |lo| lo.min(s)));
// The MAXIMUM must still skip the synthetic sentinel (once):
// `seqno#kv_max` excludes range tombstones, so an RT-only
// table's sentinel seqno legitimately sits above it.
if !*sentinel_excluded
&& let Some((sentinel_key, sentinel_seqno)) = &sentinel
&& s == *sentinel_seqno
&& entry.key.value_type == crate::ValueType::WeakTombstone
&& entry.key.user_key.as_ref() == sentinel_key.as_ref()
{
*sentinel_excluded = true;
continue;
}
*seqno_hi = Some(seqno_hi.map_or(s, |hi| hi.max(s)));
}
Ok(())
}
}
/// Judges the recorded metadata BOUNDS against everything the walk
/// accumulated. Checks, against the ON-DISK meta:
/// - the recorded key range COVERS the decoded first/last data keys and
/// every recorded range tombstone's bounds (covers, not equals: the
/// writer legitimately widens the range over tombstone-only spans);
/// - the recorded `item_count` equals the decoded entry count (the
/// tombstone sentinel is an on-disk entry counted on both sides);
/// - the recorded `data_block_count` equals the indexed block count
/// ([`Table::scan`] hands it to the compaction scanner, which stops
/// after that many blocks — a smaller forge drops the tail);
/// - the disk-fresh per-KV footer descriptor matches the recovery-time
/// one (a re-stamped `None` would misread footer bytes as the trailer
/// after reopen);
/// - for tables WITHOUT a range-tombstone sentinel, the recorded
/// `seqno#min` is at or below the decoded minimum and `seqno#kv_max`
/// is at or above the decoded maximum (a raised min / lowered max
/// hides visible versions from snapshot reads). RT-bearing tables skip
/// this: the writer records the KV range EXCLUDING the sentinel's
/// synthetic RT-derived seqno, so the decoded range legitimately
/// differs — and those tables are already gated by the fail-closed
/// deletion-metadata attribution rule.
///
/// # Errors
///
/// [`crate::Error::InvalidHeader`] when the recorded bounds disagree with
/// the decoded contents.
#[cfg(feature = "std")]
#[cfg_attr(
not(feature = "columnar"),
expect(
unused_variables,
reason = "the delete-bitmap gate the authentication flag drives is columnar-only"
)
)]
fn finish_meta_bounds(&self, probe: MetaBoundsProbe) -> crate::Result<()> {
let MetaBoundsProbe {
meta,
tombstones,
restricted,
block_count,
bitmap_digest_authenticated,
count,
first_key,
last_key,
seqno_lo,
seqno_hi,
..
} = probe;
if restricted {
// The live suffix is a subset of the whole table `meta` describes;
// it must not decode to MORE entries than the recorded whole-table
// count (that would be a grafted-in forge, not a punched prefix).
if count > meta.item_count {
return Err(crate::Error::InvalidHeader(
"restricted view decodes to more entries than meta item_count",
));
}
} else if count != meta.item_count {
return Err(crate::Error::InvalidHeader(
"meta item_count disagrees with the decoded entry count",
));
}
// Seqno bounds route snapshot visibility: `get_for_key_cmp` skips this
// table when the query snapshot is at or below the recorded minimum,
// and treats it as fully visible above the recorded maximum. Both
// meta mirrors re-stamped with `seqno#min` raised (or `seqno#kv_max`
// lowered) pass every other gate yet silently hide older / newer
// visible versions after reopen. Cross-check against the decoded seqnos.
// The synthetic RT sentinel was already excluded above (its RT-derived
// seqno lies outside the recorded KV range), so this runs for RT-bearing
// tables too: an RT-ONLY table's sole entry drops out, leaving no bounds
// to check, while an RT+KV table's real entries are checked. A recorded
// minimum ABOVE the real minimum, or a recorded KV maximum BELOW the
// real maximum, is corruption.
// Checked INDEPENDENTLY: an RT-only table decodes a minimum (its
// sentinel counts there) but no KV maximum, and pairing the two would
// skip the minimum check on exactly the table shape an adversary builds
// to escape it.
if let Some(lo) = seqno_lo
&& meta.seqnos.0 > lo
{
return Err(crate::Error::InvalidHeader(
"meta seqno#min is above the decoded minimum seqno",
));
}
if let Some(hi) = seqno_hi
&& meta.highest_kv_seqno < hi
{
return Err(crate::Error::InvalidHeader(
"meta seqno#kv_max is below the decoded maximum seqno",
));
}
// `Table::scan` hands the recorded count to the compaction scanner,
// which stops after that many blocks: a count re-stamped SMALLER
// silently drops every key in the omitted tail from rewrites.
if block_count != meta.data_block_count {
return Err(crate::Error::InvalidHeader(
"meta data_block_count disagrees with the indexed block count",
));
}
let cmp = &self.comparator;
let covers = |key: &[u8]| {
cmp.compare(meta.key_range.min().as_ref(), key) != core::cmp::Ordering::Greater
&& cmp.compare(meta.key_range.max().as_ref(), key) != core::cmp::Ordering::Less
};
if let (Some(first), Some(last)) = (&first_key, &last_key)
&& !(covers(first.as_ref()) && covers(last.as_ref()))
{
return Err(crate::Error::InvalidHeader(
"meta key range does not cover the decoded data keys",
));
}
// The decoded tombstone count must match the recorded
// `range_tombstone_count`. A re-stamped RT block that decodes to a
// SUBSET, or a dropped section, passes the coverage check below for its
// surviving entries while the missing ranges no longer mask lower-level
// data — reads then resurrect the keys those tombstones deleted. The
// count lives in the meta block (already cross-checked field-for-field
// against the recovery-time copy above), so it is the trustworthy side.
let decoded_rt_count = tombstones.as_ref().map_or(0, alloc::vec::Vec::len) as u64;
if decoded_rt_count != meta.range_tombstone_count {
return Err(crate::Error::InvalidHeader(
"range_tombstones count disagrees with the recorded range_tombstone_count",
));
}
// The recorded positional-delete count must match the readable
// delete_bitmap section EXACTLY, in both directions. The section is
// OPTIONAL (the writer omits it, and records the count as 0, precisely
// when the bitmap is empty), so its effective length is its decoded
// length when present and 0 when absent. A re-stamped TOC can break the
// agreement either way:
// - a `> 0` count with the section RENAMED away (or REPLACED by
// another valid optional section, e.g. a full filter that passes
// every probe) leaves the table reporting no deletion, resurrecting
// every positionally-deleted row;
// - a `0` count with a live non-empty section GRAFTED on (the
// no-delete count is genuine, but a bitmap from another table is
// spliced in) makes reads apply that mask and drop live rows.
// Comparing effective length to the recorded count catches both, like
// the range-tombstone count check above. The count lives in the meta
// block (already cross-checked field-for-field against the recovery-time
// copy above and against the mirror), so it is the trustworthy side.
// `None` is an older table without the field: nothing to cross-check.
// Columnar-only: the bitmap is a columnar-layout section.
#[cfg(feature = "columnar")]
if let Some(recorded) = meta.delete_bitmap_len {
let effective = if self.has_delete_bitmap_section() {
self.delete_bitmap().len()
} else {
0
};
if effective != recorded {
return Err(crate::Error::InvalidHeader(
"delete_bitmap count disagrees with the recorded \
descriptor#delete_bitmap_len (the section was hidden, \
replaced, or grafted on)",
));
}
}
// Authenticate the delete-bitmap CONTENTS, not just its cardinality: the
// count check above accepts an equal-cardinality checksum-valid bitmap
// substituted for the real one, which — during manifest repair, with no
// original whole-file digest to compare against — would resurrect the
// originally-deleted rows and drop different live ones. The meta-bound
// hash of the section's encoded bytes catches any content substitution;
// the meta block is itself checksum- and mirror-verified, so a forger
// cannot restamp the hash without failing meta integrity. Re-encoding the
// decoded bitmap is byte-identical to the on-disk section (the container
// kind and its contents round-trip verbatim), so it matches the writer's
// hash.
//
// This gate runs ONLY during repair / heal reconciliation, never on an
// ordinary read (which the manifest's whole-file digest already
// protects). Those paths have no original digest, so a PRESENT bitmap
// section MUST carry a content hash to be authenticated here: a table
// written before this field (`None`) cannot be authenticated, so it fails
// closed rather than accepting a possibly-substituted equal-cardinality
// mask. Ordinary reads of such an older table are unaffected.
#[cfg(feature = "columnar")]
if self.has_delete_bitmap_section() {
match meta.delete_bitmap_hash {
Some(recorded_hash) => {
let actual_hash = crate::hash::hash128(&self.delete_bitmap().encode());
if actual_hash != recorded_hash {
return Err(crate::Error::InvalidHeader(
"delete_bitmap contents disagree with the recorded \
descriptor#delete_bitmap_hash (an equal-cardinality bitmap \
was substituted)",
));
}
}
None if !bitmap_digest_authenticated => {
return Err(crate::Error::InvalidHeader(
"delete_bitmap section present without a \
descriptor#delete_bitmap_hash; its contents cannot be \
authenticated during repair / heal",
));
}
// A LEGACY table (written before the hash existed) on the
// directly attributable heal path: the pre-heal digest
// matched the manifest, so the bitmap bytes — untouched by
// the data-block heal — were just authenticated by that
// digest. Rejecting here would strip the heal attestation
// and strand the healed table under a stale digest forever.
None => {}
}
}
// Range tombstones mask entries in OLDER tables during reads and
// merges, so a key range narrowed below a tombstone's extent routes
// reads around this table and resurrects the data it deletes. Reuse the
// list loaded up front (for the sentinel derivation) rather than
// re-reading the block.
if let Some(tombstones) = &tombstones {
for rt in tombstones {
if !(covers(rt.start.as_ref()) && covers(rt.end.as_ref())) {
return Err(crate::Error::InvalidHeader(
"meta key range does not cover a recorded range tombstone",
));
}
}
}
Ok(())
}
/// The SECTION-level half of the block-layout check: the section must not
/// be present-but-empty on a table that has data blocks.
///
/// The writer emits `block_layout` only when it has a multi-inner-block
/// frame to record, so an empty map is a forgery — a `delete_bitmap`
/// renamed and re-roled, which drops the deletion metadata and resurrects
/// positionally-deleted rows. The per-block cross-check cannot catch that
/// one: a block absent from the map is skipped, so an empty map trivially
/// "agrees" with every block.
///
/// This runs on EVERY build, including non-zstd, because the forgery does
/// not need zstd to do its damage. The per-frame half — where each recorded
/// boundary is matched against the frame's actual inner blocks — lives in
/// [`Self::verify_reconcile_gates`], which already holds the decoded frame
/// and needs no second read to do it.
///
/// # Errors
///
/// [`crate::Error::InvalidHeader`] on the empty-map forgery,
/// [`crate::Error::InvalidTag`] when the section does not carry the
/// block-layout role, plus any I/O / decode error from the section read.
#[cfg(feature = "std")]
pub(crate) fn verify_block_layout(&self) -> crate::Result<()> {
// The section-presence + EMPTINESS check runs on EVERY build (including
// non-zstd, where the per-frame cross-check below is compiled out): a
// relabeled delete_bitmap→empty block_layout drops the deletion metadata
// regardless of zstd, so the forgery must be caught either way.
//
// Re-read the section FROM DISK: the in-memory map was loaded at recover
// time, so an on-disk re-stamp after the open (the very forge this check
// exists for) would be invisible to it.
let mut file = self.fs.open(&self.path, &FsOpenOptions::new().read(true))?;
let trailer = crate::sfa::Reader::from_reader(&mut file)?;
let regions = regions::ParsedRegions::parse_from_toc(trailer.toc())?;
let Some(bl_handle) = regions.block_layout else {
return Ok(());
};
// Decode the map with an encryption-aware transform: on an encrypted
// table the section is AEAD-sealed, so both the emptiness check below
// AND catching a relabeled delete_bitmap (its AAD binds the block type,
// so decoding it as a BlockLayout fails the AEAD open) need the provider.
let map = {
let block = Block::from_file(
&*file,
bl_handle,
crate::table::block::BlockIdentity {
table_id: self.metadata.id,
block_type: BlockType::BlockLayout,
dict_id: 0,
window_log: 0,
},
&self.section_transform(),
)?;
if block.header.block_type != BlockType::BlockLayout {
return Err(crate::Error::InvalidTag((
"BlockType",
block.header.block_type.into(),
)));
}
crate::table::block_layout::BlockLayoutMap::decode(&block.data)?
};
// The writer emits the block_layout section ONLY when it has at least
// one multi-inner-block frame to record, so a PRESENT-but-empty map on a
// table with data blocks is a forgery — e.g. a delete_bitmap renamed and
// re-roled to an empty block_layout, which drops the deletion metadata
// and resurrects positionally-deleted rows. (The per-block loop below
// cannot catch it: a block absent from the map is skipped, so an empty
// map trivially "agrees" with every block.)
if map.is_empty() {
if self.untraced_index_walk().next().is_some() {
return Err(crate::Error::InvalidHeader(
"block_layout section is present but empty on a table with data blocks",
));
}
return Ok(());
}
Ok(())
}
/// Walks the LIVE data blocks once, decoding each DISK-FRESH, and hands
/// every gate the same [`DecodedBlock`].
///
/// A restricted view's punched prefix blocks are dead (they read as zeros),
/// so the walk starts at the punch offset — every gate skipped them
/// individually before, with the same reasoning.
///
/// # Errors
///
/// Propagates the index walk, the punch-offset lookup, the block read and
/// the decode, plus whatever `check` returns.
#[cfg_attr(
not(feature = "std"),
expect(
dead_code,
reason = "gate-only walk; the verify/scrub consumers are std-gated"
)
)]
fn for_each_live_block<F>(&self, mut check: F) -> crate::Result<()>
where
F: FnMut(&DecodedBlock) -> crate::Result<()>,
{
use crate::table::block::ParsedItem as _;
let punch = self.punch_offset()?;
for handle in self.untraced_index_walk() {
let handle = *handle?.as_ref();
if handle.offset().0 < punch {
continue;
}
// Keep the block as it came off disk (`raw`) and derive the views
// from a CLONE: `from_loaded` strips the per-KV footer, and the
// digest gate needs the footered bytes. A `Block` clone is a
// header copy plus a refcount bump on the payload, not a copy.
#[cfg(feature = "columnar")]
let (raw, frame, row, batch) = if self.metadata.columnar {
let (raw, frame, batch) = self.load_row_group_from_disk(&handle)?;
(raw, frame, None, Some(batch))
} else {
let (raw, frame) = self.load_block_from_disk(&handle, BlockType::Data)?;
let row =
DataBlock::from_loaded(raw.clone(), self.metadata.kv_checksum_algo.is_some())?;
(raw, frame, Some(row), None)
};
#[cfg(not(feature = "columnar"))]
let (raw, frame, row) = {
let (raw, frame) = self.load_block_from_disk(&handle, BlockType::Data)?;
let row =
DataBlock::from_loaded(raw.clone(), self.metadata.kv_checksum_algo.is_some())?;
(raw, frame, Some(row))
};
// Materialize the entries from whichever view this table has. The
// columnar reconstruction is the same one `decode_block_entries`
// performs, so a malformed batch still fails here rather than
// truncating silently.
let entries: Vec<InternalValue> = match &row {
Some(block) => block
.try_iter(self.comparator.clone())?
.map(|p| p.materialize(block.as_slice()))
.collect(),
#[cfg(feature = "columnar")]
None => match &batch {
Some(batch) => crate::table::columnar::column_batch_to_entries(batch)?,
None => Vec::new(),
},
#[cfg(not(feature = "columnar"))]
None => Vec::new(),
};
check(&DecodedBlock {
handle,
raw,
frame,
row,
#[cfg(feature = "columnar")]
batch,
entries,
})?;
}
Ok(())
}
/// Loads a columnar row group STRAIGHT FROM THE FILE, bypassing the block
/// cache, for the reconcile gates — the reason [`Self::load_block_from_disk`]
/// exists, applied to every block of the group.
///
/// Returns the directory block and its frame (the entry's first block,
/// which is what the gates that look at a raw block get for a columnar
/// table: pages carry no per-KV footer and no inner-frame layout, so those
/// gates have nothing further to check in a group) together with the
/// decoded batch.
#[cfg(all(feature = "columnar", feature = "std"))]
fn load_row_group_from_disk(
&self,
group: &BlockHandle,
) -> crate::Result<(Block, crate::Slice, crate::table::columnar::ColumnBatch)> {
let mut blocks = self.data_unit_blocks(group)?.into_iter();
let Some((directory_handle, directory_role)) = blocks.next() else {
return Err(crate::Error::InvalidHeader(
"columnar: row group has no directory",
));
};
let (directory, frame) = self.load_block_from_disk(&directory_handle, directory_role)?;
let directory_decoded = crate::table::column_page::PageDirectory::decode(&directory.data)?;
let mut pages = alloc::vec::Vec::with_capacity(directory_decoded.entries().len());
// The zone blocks in the order they lie: the head one, then the rest.
let zone_blocks: alloc::vec::Vec<crate::table::column_page::ZoneBlock> = directory_decoded
.head_zone_block()
.into_iter()
.chain(directory_decoded.zone_blocks().iter().copied())
.collect();
let mut block_zones = alloc::vec::Vec::with_capacity(zone_blocks.len());
let mut listed = zone_blocks.iter();
for (handle, role) in blocks {
let block = self.load_block_from_disk(&handle, role)?.0;
if role == BlockType::ColumnZones {
let column_id =
listed
.next()
.map(|z| z.column_id)
.ok_or(crate::Error::InvalidHeader(
"columnar: a zone block the directory does not list",
))?;
block_zones.push(directory_decoded.decode_zone_block(column_id, &block.data)?);
} else {
pages.push(block);
}
}
let group = crate::table::row_group::RowGroupBlocks::whole(
alloc::sync::Arc::new(directory_decoded),
pages,
);
// The gates are verification, which the counters leave out; they
// re-derive per-group statistics, so the row pages are joined.
let batch = group.to_row_pages(&mut 0)?.into_batch()?;
// The row pages' zones decide which pages a read skips, so they are
// authenticated like the zone map: re-derived from the decoded rows,
// every zone block's, in the order the writer lays them out, the key
// column's as the head block before the pages; a zone block that
// should not exist, or lies elsewhere, is as wrong as one that is
// missing. A zone that disagrees would let a read prune a row page
// holding a match, and nothing on the read path looks at the rows it
// pruned.
let (key_zones, other_zones) = batch.group_zones(group.directory.row_pages())?;
let key_column = key_zones.columns().first().copied();
let expected: alloc::vec::Vec<_> = key_column
.map(|_| key_zones)
.into_iter()
.chain(
other_zones
.columns()
.iter()
.map(|&column_id| other_zones.only(|c| c == column_id)),
)
.collect();
let head_column = group.directory.head_zone_block().map(|z| z.column_id);
if expected != block_zones || head_column != key_column {
return Err(crate::Error::InvalidHeader(
"columnar: row page statistics disagree with the group's rows",
));
}
Ok((directory, frame, batch))
}
/// Decodes ONE data block's entries (row or columnar) into
/// [`InternalValue`]s, disk-fresh ([`Self::load_block_from_disk`]).
///
/// Test-only: the cross-check gates consume the walk's shared decode
/// ([`Self::for_each_live_block`]) rather than decoding a block apiece.
/// What is left is fixtures that need one named block's contents to build
/// a forgery from.
#[cfg(all(feature = "std", test))]
fn decode_block_entries(
&self,
block_handle: &BlockHandle,
) -> crate::Result<Vec<InternalValue>> {
use crate::table::block::ParsedItem as _;
#[cfg(feature = "columnar")]
if self.metadata.columnar {
let (_, _, batch) = self.load_row_group_from_disk(block_handle)?;
return crate::table::columnar::column_batch_to_entries(&batch);
}
let (block, _frame) = self.load_block_from_disk(block_handle, BlockType::Data)?;
let data_block = DataBlock::from_loaded(block, self.metadata.kv_checksum_algo.is_some())?;
Ok(data_block
.try_iter(self.comparator.clone())?
.map(|p| p.materialize(data_block.as_slice()))
.collect())
}
/// Loads the filter block (if any) and checks the bloom filter, counting
/// nothing.
///
/// Returns `Ok(BloomResult::Skip)` if the bloom filter says the key is definitely absent,
/// `Ok(BloomResult::Proceed { has_filter })` otherwise. A filter block read
/// from the file is read through `fd`, the lookup's.
fn judge_bloom(&self, key: &[u8], key_hash: u64, fd: &LookupFd) -> crate::Result<BloomResult> {
debug_assert_eq!(
key_hash,
crate::hash::hash64(key),
"key_hash must match the hash of the provided key"
);
match self.filter_source(key) {
FilterSource::None => Self::answer_bloom(None, key_hash),
FilterSource::Pinned(block) => Self::answer_bloom(Some(block), key_hash),
FilterSource::Block(handle) => {
let block = self.load_block(
&handle,
BlockType::Filter,
CompressionType::None,
#[cfg(zstd_any)]
None,
fd,
)?;
Self::answer_bloom(Some(&FilterBlock::new(block)), key_hash)
}
FilterSource::PastPartitions => Ok(BloomResult::PastPartitions),
}
}
/// [`Self::judge_bloom`], counted at once in the global metrics, as a
/// point read counts its filter answers.
fn check_bloom(&self, key: &[u8], key_hash: u64, fd: &LookupFd) -> crate::Result<BloomResult> {
let answer = self.judge_bloom(key, key_hash, fd)?;
#[cfg(feature = "metrics")]
if answer.should_skip() {
use core::sync::atomic::Ordering::Relaxed;
self.metrics.filter_queries.fetch_add(1, Relaxed);
self.metrics.io_skipped_by_filter.fetch_add(1, Relaxed);
}
Ok(answer)
}
/// Adds `answer` to `tally`, counted when the plan holding it is.
fn tally_bloom(tally: &mut crate::table::probe_stats::PlanCounts, answer: &BloomResult) {
tally.probes += answer.probe_counts();
if answer.should_skip() {
tally.filter_queries += 1;
tally.filter_skips += 1;
}
}
/// Where the filter `key` is checked against lives, found without a read:
/// the pinned filter, the block of the filter or of the filter partition
/// holding the key, or no filter at all.
pub(crate) fn filter_source(&self, key: &[u8]) -> FilterSource<'_> {
if let Some(block) = &self.pinned_filter_block {
FilterSource::Pinned(block)
} else if let Some(filter_idx) = &self.pinned_filter_index {
let mut iter = filter_idx.iter(self.comparator.clone());
// Filter partitions are written with seqno=0, making the seqno
// parameter irrelevant to partition selection. Use MAX_SEQNO
// consistently to match the index-block seek in Table::range().
iter.seek(key, crate::seqno::MAX_SEQNO);
match iter.next() {
Some(handle) => {
FilterSource::Block(handle.materialize(filter_idx.as_slice()).into_inner())
}
// Key sorts past the last filter partition: a definite miss.
None => FilterSource::PastPartitions,
}
} else if self.regions.filter_tli.is_some() {
// Partitioned, yet its index is not loaded: a live open always
// loads it, so this is a salvage open that found it unreadable.
// Without the index no partition can be found, so no key is ruled
// out.
FilterSource::None
} else if let Some(handle) = &self.regions.filter {
FilterSource::Block(*handle)
} else {
FilterSource::None
}
}
/// The answer of `filter` (none: nothing rules the key out) for a key
/// hashing to `key_hash`, counting nothing.
fn answer_bloom(filter: Option<&FilterBlock>, key_hash: u64) -> crate::Result<BloomResult> {
let Some(filter) = filter else {
return Ok(BloomResult::Proceed { has_filter: false });
};
if !filter.maybe_contains_hash(key_hash)? {
return Ok(BloomResult::Skip);
}
Ok(BloomResult::Proceed { has_filter: true })
}
/// Records a data-consulting point read for per-segment tiering / placement
/// stats: a single `Relaxed` counter bump plus, on `std`, the access time.
/// Called only after the seqno-range + bloom gates pass, so bloom misses do
/// not inflate the count. Raw counter; the consumer derives a rate / EMA from
/// successive polls.
fn record_access(&self) {
self.record_accesses(1);
}
/// Records `reads` data-consulting point reads at once, as
/// [`Self::record_access`] records one: what a batched read let through
/// its gates.
pub(crate) fn record_accesses(&self, reads: u64) {
self.read_count
.fetch_add(reads, core::sync::atomic::Ordering::Relaxed);
#[cfg(feature = "std")]
self.last_access_secs.store(
crate::time::unix_timestamp().as_secs(),
core::sync::atomic::Ordering::Relaxed,
);
}
pub fn get(
&self,
key: &[u8],
seqno: SeqNo,
key_hash: u64,
) -> crate::Result<Option<InternalValue>> {
// Tight-space restriction: this version sees the table only at keys
// `>= bound` (the prefix below it is punched out and superseded by a
// merged output table). Keys below `bound` must miss here so the read
// falls through to that output; the punched blocks are never touched.
if self.is_below_restriction(key) {
return Ok(None);
}
let global_seqno = self.global_seqno();
// A query snapshot below this table's base seqno predates the table, so
// none of its rows are visible. `checked_sub` yields `None` there (where a
// saturating sub would silently clamp to 0); the seqno-range gate below
// then handles the in-range case.
let Some(seqno) = seqno.checked_sub(global_seqno) else {
return Ok(None);
};
if self.metadata.seqnos.0 >= seqno {
return Ok(None);
}
// The filter, index and data blocks this lookup reads share the file.
let fd = LookupFd::default();
let bloom = self.check_bloom(key, key_hash, &fd)?;
self.count_probe(&bloom);
if bloom.should_skip() {
return Ok(None);
}
// Access accounting after the seqno-range + bloom gates, so a segment
// that excludes the key (seqno range) or rejects it (bloom miss) is not
// counted as serving it.
self.record_access();
// Row-cache fast path: a prior latest-version read cached this key's
// resolved value for this (immutable) SST, so we can skip the index walk
// + data-block decode. The cached value is in table-local seqno space
// (same as `point_read`). Use it only when the cached newest version is
// visible at the query snapshot; otherwise fall through, because an older
// version may apply at this snapshot.
if let Some(mut iv) = self.cache.get_row(self.global_id(), key_hash, key) {
// Snapshot reads are exclusive: a version is visible iff its seqno is
// strictly less than the query seqno. Only serve the cached newest
// version when it is visible; otherwise fall through (an older
// version may apply at this snapshot).
if iv.key.seqno < seqno {
iv.key.seqno = apply_global_seqno(iv.key.seqno, global_seqno);
return Ok(Some(iv));
}
}
let item = self.point_read(key, seqno, key_hash, &fd)?;
// Populate the row cache only when this read could see the SST's newest
// version (`seqno > max`, exclusive), so the resolved value is the SST's
// newest version for this key — which keeps the seqno-visibility check
// above correct for later snapshot reads. SSTs are immutable, so the
// entry stays valid until the SST is compacted away.
if seqno > self.metadata.seqnos.1
&& let Some(iv) = &item
{
let detached = self.cache.insert_row(
self.global_id(),
key_hash,
&iv.key.user_key,
iv.key.seqno,
iv.key.value_type,
&iv.value,
);
#[cfg(feature = "metrics")]
self.metrics.record_gather(detached);
#[cfg(not(feature = "metrics"))]
let _ = detached;
}
// Translate table-local seqno back to global coordinate so callers
// can compare across tables/memtables (L0 best-selection, RT suppression).
let item = item.map(|mut iv| {
iv.key.seqno = apply_global_seqno(iv.key.seqno, global_seqno);
iv
});
#[cfg(feature = "metrics")]
{
use core::sync::atomic::Ordering::Relaxed;
// NOTE: `check_bloom()` accounts for lookups rejected by the filter
// (skip I/O entirely). This path accounts for negative point lookups
// that still reached storage even though a filter was present, so
// `filter_queries` remains interpretable alongside `filter_efficiency()`.
// https://github.com/fjall-rs/lsm-tree/issues/246
if item.is_none() && bloom.has_filter() {
self.metrics.filter_queries.fetch_add(1, Relaxed);
}
}
if item.is_none() && bloom.has_filter() {
self.count_false_positive(seqno);
}
Ok(item)
}
/// Value-only point read: `(value_type, seqno, value)` without
/// reconstructing the entry key. Used by the value-returning `get` path,
/// which never reads the matched key (the caller has the needle), so the
/// delta-key fusion in [`DataBlock::point_read`] is skipped. The value is a
/// zero-copy slice of the cached block.
pub(crate) fn get_value(
&self,
key: &[u8],
seqno: SeqNo,
key_hash: u64,
) -> crate::Result<Option<(crate::ValueType, SeqNo, crate::Slice)>> {
// Tight-space restriction (mirrors `Table::get`): a key below the bound
// misses so the read falls through to the superseding output.
if self.is_below_restriction(key) {
return Ok(None);
}
let global_seqno = self.global_seqno();
// A query snapshot below this table's base seqno predates the table, so
// none of its rows are visible. `checked_sub` yields `None` there (where a
// saturating sub would silently clamp to 0); the seqno-range gate below
// then handles the in-range case.
let Some(seqno) = seqno.checked_sub(global_seqno) else {
return Ok(None);
};
if self.metadata.seqnos.0 >= seqno {
return Ok(None);
}
// The filter, index and data blocks this lookup reads share the file.
let fd = LookupFd::default();
let bloom = self.check_bloom(key, key_hash, &fd)?;
self.count_probe(&bloom);
if bloom.should_skip() {
return Ok(None);
}
// Access accounting after the seqno-range + bloom gates (mirrors `Table::get`).
self.record_access();
// Row-cache fast path (mirrors `Table::get`): serve the value tuple from
// a prior cached point-read result, skipping the index walk + block
// decode, when the cached newest version is visible at this snapshot.
if let Some(iv) = self.cache.get_row(self.global_id(), key_hash, key) {
// Exclusive snapshot visibility (see `Table::get`): serve only when
// the cached newest version is strictly older than the query seqno.
if iv.key.seqno < seqno {
let s = apply_global_seqno(iv.key.seqno, global_seqno);
return Ok(Some((iv.key.value_type, s, iv.value)));
}
}
let item = self.point_read_value(key, seqno, key_hash, &fd)?;
// Populate only when this read could see the SST's newest version
// (`seqno > max`, exclusive), mirroring `Table::get`. The value path does
// not reconstruct the matched key, so the row is keyed by the query key
// (the needle) + the resolved `(value_type, seqno, value)`.
if seqno > self.metadata.seqnos.1
&& let Some((vt, s, v)) = &item
{
let detached = self
.cache
.insert_row(self.global_id(), key_hash, key, *s, *vt, v);
#[cfg(feature = "metrics")]
self.metrics.record_gather(detached);
#[cfg(not(feature = "metrics"))]
let _ = detached;
}
// Translate table-local seqno back to the global coordinate, mirroring
// `Table::get`.
let item = item.map(|(vt, s, v)| (vt, apply_global_seqno(s, global_seqno), v));
#[cfg(feature = "metrics")]
{
use core::sync::atomic::Ordering::Relaxed;
if item.is_none() && bloom.has_filter() {
self.metrics.filter_queries.fetch_add(1, Relaxed);
}
}
if item.is_none() && bloom.has_filter() {
self.count_false_positive(seqno);
}
Ok(item)
}
/// Value-only block-index walk: companion to [`Table::point_read_inner`]
/// that reads each candidate data block with
/// [`DataBlock::point_read_value`] (no key fusion, no retained block).
fn point_read_value(
&self,
key: &[u8],
seqno: SeqNo,
key_hash: u64,
fd: &LookupFd,
) -> crate::Result<Option<(crate::ValueType, SeqNo, crate::Slice)>> {
// Fast path: retrieval-ribbon locator (see `point_read_inner` for the
// MVCC-correctness argument). A located-block miss falls through to the
// index walk below.
if let Some((handle, hint)) = self.locator_block(key_hash)?
&& let Some(data_block) = self.load_point_block(&handle, key, fd)?
{
// A columnar block is narrowed to this key's rows by load_point_block,
// so the full-block slot hint does not apply.
let is_columnar = cfg!(feature = "columnar") && self.metadata.columnar;
let found = match hint {
Some((slot, is_entry)) if !is_columnar => data_block.point_read_value_at_slot(
slot,
is_entry,
key,
seqno,
&self.comparator,
)?,
_ => data_block.point_read_value_hashed(
key,
Some(key_hash),
seqno,
&self.comparator,
)?,
};
if let Some(found) = found {
return Ok(Some(found));
}
}
let Some(mut iter) = self.block_index.point_read_reader(key, seqno) else {
return Ok(None);
};
while let Some(block_handle) = iter.next_with(fd) {
let block_handle = block_handle?;
// A columnar block carrying no row for this key (absent / wholly
// deleted) returns None here; still honor the end-key cutoff before
// skipping, so an absent key below this block's end key stops the
// scan instead of probing every later candidate block.
let Some(data_block) = self.load_point_block(block_handle.as_ref(), key, fd)? else {
if self.comparator.compare(block_handle.end_key(), key)
== core::cmp::Ordering::Greater
{
return Ok(None);
}
continue;
};
if let Some(found) =
data_block.point_read_value_hashed(key, Some(key_hash), seqno, &self.comparator)?
{
return Ok(Some(found));
}
if self.comparator.compare(block_handle.end_key(), key) == core::cmp::Ordering::Greater
{
return Ok(None);
}
}
Ok(None)
}
/// Like [`Table::get`], but also returns the [`Block`] containing the value.
///
/// Used by `get_pinned()` to construct `PinnableSlice::Pinned`.
///
pub(crate) fn get_with_block(
&self,
key: &[u8],
seqno: SeqNo,
key_hash: u64,
) -> crate::Result<Option<(InternalValue, Block)>> {
// Tight-space restriction (mirrors `Table::get`): a key below the bound
// misses so the read falls through to the superseding output and never
// touches the punched-out prefix.
if self.is_below_restriction(key) {
return Ok(None);
}
let global_seqno = self.global_seqno();
// A query snapshot below this table's base seqno predates the table, so
// none of its rows are visible. `checked_sub` yields `None` there (where a
// saturating sub would silently clamp to 0); the seqno-range gate below
// then handles the in-range case.
let Some(seqno) = seqno.checked_sub(global_seqno) else {
return Ok(None);
};
if self.metadata.seqnos.0 >= seqno {
return Ok(None);
}
// The filter, index and data blocks this lookup reads share the file.
let fd = LookupFd::default();
let bloom = self.check_bloom(key, key_hash, &fd)?;
self.count_probe(&bloom);
if bloom.should_skip() {
return Ok(None);
}
// Access accounting after the seqno-range + bloom gates (mirrors `Table::get`).
self.record_access();
let result = self.point_read_with_block(key, seqno, key_hash, &fd)?;
// Translate table-local seqno back to global coordinate (see Table::get).
let result = result.map(|(mut iv, block)| {
iv.key.seqno = apply_global_seqno(iv.key.seqno, global_seqno);
(iv, block)
});
#[cfg(feature = "metrics")]
{
use core::sync::atomic::Ordering::Relaxed;
if result.is_none() && bloom.has_filter() {
self.metrics.filter_queries.fetch_add(1, Relaxed);
}
}
if result.is_none() && bloom.has_filter() {
self.count_false_positive(seqno);
}
Ok(result)
}
/// Shared block-index walk for point reads. Returns the matching entry
/// together with the [`DataBlock`] it was found in, so callers that need
/// the block (e.g. for [`PinnableSlice`]) can keep it alive.
/// Resolve the data block holding `key_hash`'s newest version (plus an
/// optional in-block slot hint) via the retrieval-ribbon locator, if one is
/// loaded. `Ok(None)` means no locator or the ribbon could not answer → the
/// caller uses the sorted-index walk.
fn locator_block(
&self,
key_hash: u64,
) -> crate::Result<Option<crate::table::locator::Located>> {
match &self.locator_index {
Some(loc) => loc.locate_block(key_hash),
None => Ok(None),
}
}
fn point_read_inner(
&self,
key: &[u8],
seqno: SeqNo,
key_hash: u64,
fd: &LookupFd,
) -> crate::Result<Option<(InternalValue, DataBlock)>> {
// Fast path: a retrieval-ribbon locator resolves the key to its data
// block in O(1), skipping the index-block binary search. The located
// block holds the newest version (the run's highest-seqno prefix), so a
// hit returns the correct MVCC answer and a miss (absent key, or the
// visible version lives in a later block) safely falls through to the
// index walk below.
if let Some((handle, hint)) = self.locator_block(key_hash)?
&& let Some(data_block) = self.load_point_block(&handle, key, fd)?
{
// A columnar block is narrowed to this key's rows by load_point_block,
// so the full-block slot hint does not apply.
let is_columnar = cfg!(feature = "columnar") && self.metadata.columnar;
let found = match hint {
Some((slot, is_entry)) if !is_columnar => {
data_block.point_read_at_slot(slot, is_entry, key, seqno, &self.comparator)?
}
_ => data_block.point_read_hashed(key, Some(key_hash), seqno, &self.comparator)?,
};
if let Some(item) = found {
return Ok(Some((item, data_block)));
}
}
// Borrowing point-read seek: avoids cloning the index block + reuses
// the trailer metadata parsed at table open (see
// `BlockIndexImpl::point_read_reader`).
let Some(mut iter) = self.block_index.point_read_reader(key, seqno) else {
return Ok(None);
};
while let Some(block_handle) = iter.next_with(fd) {
let block_handle = block_handle?;
// A columnar block carrying no row for this key (absent / wholly
// deleted) returns None here; still honor the end-key cutoff before
// skipping, so an absent key below this block's end key stops the
// scan instead of probing every later candidate block.
let Some(data_block) = self.load_point_block(block_handle.as_ref(), key, fd)? else {
if self.comparator.compare(block_handle.end_key(), key)
== core::cmp::Ordering::Greater
{
return Ok(None);
}
continue;
};
if let Some(item) =
data_block.point_read_hashed(key, Some(key_hash), seqno, &self.comparator)?
{
return Ok(Some((item, data_block)));
}
// NOTE: If the last block key is higher than ours,
// our key cannot be in the next block
if self.comparator.compare(block_handle.end_key(), key) == core::cmp::Ordering::Greater
{
return Ok(None);
}
}
Ok(None)
}
fn point_read(
&self,
key: &[u8],
seqno: SeqNo,
key_hash: u64,
fd: &LookupFd,
) -> crate::Result<Option<InternalValue>> {
self.point_read_inner(key, seqno, key_hash, fd)
.map(|opt| opt.map(|(iv, _)| iv))
}
/// Like [`Table::point_read`], but also returns the underlying [`Block`].
///
/// Holding on to the returned [`Block`] (e.g. for [`PinnableSlice`]) keeps the
/// block data alive while the value is in use, but does not guarantee that the
/// cache will retain its own entry for that block.
fn point_read_with_block(
&self,
key: &[u8],
seqno: SeqNo,
key_hash: u64,
fd: &LookupFd,
) -> crate::Result<Option<(InternalValue, Block)>> {
self.point_read_inner(key, seqno, key_hash, fd)
.map(|opt| opt.map(|(iv, db)| (iv, db.inner)))
}
/// Batch point-read variant of [`Table::get`].
///
/// Each input pair is `(key, key_hash)`. The slice **must be
/// strictly sorted ascending by `key` under this table's
/// comparator** — duplicate adjacent keys are a caller bug
/// (callers should dedup before batching; a duplicate
/// suggests a logic error in the query construction) and
/// are rejected by a `debug_assert!` in debug builds.
/// Returns one `Option<InternalValue>` per input pair in
/// input order: `Some(_)` for found values (including
/// tombstones — callers distinguish via [`InternalValue`]'s
/// value type), `None` for absent keys.
///
/// # Hash contract
///
/// `key_hash` **must** equal `crate::hash::hash64(key)` — the
/// same function the writer used when populating the bloom
/// filter. The bloom probe consumes the hash; the
/// key↔hash agreement check is a `debug_assert!` only, so
/// release builds trust the caller. Passing a wrong hash in
/// release produces false-negative skips: the corresponding
/// `results[i]` slot stays `None` as if the key weren't in
/// the table (the result vector itself is always
/// `sorted_keys.len()` long — nothing is dropped from it).
/// Callers should derive both values from the same
/// `(&[u8], u64) = (key, hash64(key))` expression at the
/// same scope to make the agreement trivially auditable.
///
/// In partitioned-filter mode (`pinned_filter_index` /
/// `filter_tli`), `check_bloom` ALSO uses the raw `key`
/// bytes — not the hash — to select which filter partition
/// to probe. The hash drives the bit probes inside the
/// selected partition. Bottom line: BOTH inputs are
/// load-bearing in the partitioned case; only the
/// monolithic-filter case is "hash-only".
///
/// # Why this exists vs. calling [`Table::get`] in a loop
///
/// Sequential per-key calls each pay:
///
/// 1. Bloom-filter dereference + N hash probes — duplicated
/// across calls.
/// 2. Block-index seek from scratch — every call walks
/// `forward_reader(key, seqno)` and re-pays the index
/// binary search even when the previous call already
/// landed inside the same data block.
/// 3. Block load — every call re-fetches the data block
/// from cache, so cache hits still pay a hashmap lookup
/// + Arc clone per call.
///
/// `batch_get` collapses all three:
///
/// 1. Filter probed once per key in a tight loop. For
/// monolithic filters (the default) the filter block is
/// fetched once and the loop just checks N hashes
/// against it. For partitioned filters
/// (`pinned_filter_index` / `filter_tli`), each probe
/// still seeks the partition index and may load the
/// relevant partition block lazily — so "one filter
/// fetch total" only holds in the monolithic case;
/// partitioned filters amortise loads across keys that
/// land in the same partition rather than across the
/// whole batch.
/// 2. Block-index seek runs at the smallest passing key and
/// again only at a passing key past the block before it;
/// keys in the same or the next block share the walk, and
/// a sparse batch over a large table never steps through
/// the entries and partitions between its keys.
/// 3. Each data block is loaded at most once for the entire
/// batch. Multiple input keys that fall in the same block
/// share a single load.
///
/// The wire-format is identical to N independent `get()`
/// calls; the savings are purely call-overhead.
///
/// # Sort requirement
///
/// Sorting is the caller's responsibility because the
/// `batch_get_from_tables` driver already maintains the
/// remaining-keys list in comparator order between L1+ runs
/// (re-sorted after each `covered_miss` split). Re-sorting
/// inside `batch_get` would be redundant work; passing
/// pre-sorted input lets the implementation rely on a
/// monotone two-pointer walk between input keys and block
/// boundaries.
///
/// # Errors
///
/// Propagates any I/O / corruption error from the filter
/// fetch, block-index read, or data-block load. On error
/// the partial `results` vector is discarded — callers
/// observe an all-or-nothing outcome per call.
#[expect(
clippy::indexing_slicing,
reason = "every index access in this routine is bounded by construction: \
`passing` indices are produced from enumerate(sorted_keys) so they're \
< sorted_keys.len() == results.len(); `passing[p]` is guarded by \
`p < passing.len()` on every loop iteration; `passing[0]` is read \
only after an explicit emptiness check above."
)]
pub fn batch_get(
&self,
sorted_keys: &[(&[u8], u64)],
seqno: SeqNo,
) -> crate::Result<Vec<Option<InternalValue>>> {
let mut results: Vec<Option<InternalValue>> = vec![None; sorted_keys.len()];
if sorted_keys.is_empty() {
return Ok(results);
}
// Debug-time guard for the sorted-input contract.
// Unsorted input would silently return wrong Nones
// (the two-pointer walk between block_iter and the
// input slice assumes monotone keys); catch the
// accidental misuse before it ships to a release
// benchmark. Strict-monotone is the contract — equal
// adjacent keys would be a duplicate query, also a
// caller bug.
debug_assert!(
sorted_keys
.windows(2)
.all(|w| self.comparator.compare(w[0].0, w[1].0) == core::cmp::Ordering::Less),
"batch_get input must be strictly sorted ascending by key under \
the table's comparator; unsorted/duplicate input produces silent \
None misses because the two-pointer walk assumes monotone keys"
);
let global_seqno = self.global_seqno();
// A query snapshot below this table's base seqno predates the table, so no
// key is visible. `checked_sub` yields `None` there (a saturating sub would
// clamp to 0); the seqno-range gate below handles the in-range case.
let Some(table_seqno) = seqno.checked_sub(global_seqno) else {
return Ok(results);
};
// Table is entirely above the snapshot — no key is visible.
if self.metadata.seqnos.0 >= table_seqno {
return Ok(results);
}
// Filter the input through the bloom filter once. The
// filter resource (mmap / Arc) is fetched lazily by
// check_bloom on the first call; subsequent calls reuse
// it through the table-internal cache.
let mut passing: Vec<usize> = Vec::with_capacity(sorted_keys.len());
let mut had_filter = false;
// Every filter, index and data block the batch reads shares the file.
let fd = LookupFd::default();
for (i, (key, hash)) in sorted_keys.iter().enumerate() {
// Below a tight-space restriction, as for a point read.
if self.is_below_restriction(key) {
continue;
}
let bloom = self.check_bloom(key, *hash, &fd)?;
self.count_probe(&bloom);
if !bloom.should_skip() {
passing.push(i);
if bloom.has_filter() {
had_filter = true;
}
}
}
if passing.is_empty() {
return Ok(results);
}
// Counted as single reads are, after the seqno-range and filter gates.
self.record_accesses(u64::try_from(passing.len()).unwrap_or(u64::MAX));
// The filter let these keys through; the ones no read below finds are
// its false positives.
let count_false_positives = |results: &[Option<InternalValue>]| {
if had_filter && self.probe_stats().is_some() {
for &i in &passing {
if results.get(i).is_some_and(Option::is_none) {
self.count_false_positive(table_seqno);
}
}
}
};
// Seek the block index once at the smallest passing key.
// forward_reader returns the first block whose end_key
// can cover that key; everything past it walks forward.
let first_key = sorted_keys[passing[0]].0;
let Some(mut block_iter) = self.block_index.forward_reader(first_key, table_seqno) else {
// No block can contain the smallest passing key — every
// passing key is "negative with filter present" for
// metrics accounting purposes, mirroring Table::get
// where a bloom-passing key that point_read can't find
// increments filter_queries. Falling through to the
// shared metrics block below ensures the batch path
// doesn't under-report compared to N independent get()s.
#[cfg(feature = "metrics")]
{
// Use core::* rather than std::* re-exports: the
// `metrics` feature isn't std-gated in Cargo.toml,
// and `Ordering` lives in `core::sync::atomic`
// unchanged — keeps this hot-path import no-std
// friendly without any runtime impact (the std
// path is just a re-export of the core symbol).
use core::sync::atomic::Ordering::Relaxed;
if had_filter && !passing.is_empty() {
self.metrics
.filter_queries
.fetch_add(passing.len(), Relaxed);
}
}
count_false_positives(&results);
return Ok(results);
};
// Two-pointer walk: outer loop advances block_iter, inner
// loop drains passing keys that fall inside the current
// block's range. Both sides are monotone (sorted by the
// same comparator), so each side advances at most once
// per pair.
let mut p = 0_usize;
while p < passing.len() {
let Some(handle_result) = block_iter.next_with(&fd) else {
break;
};
let block_handle = handle_result?;
let end_key = block_handle.end_key();
// Lazy load: only fetch the data block if at least
// one passing key falls into this block's range.
// Most blocks will contain at least one key (we
// seeked here precisely because the first key did),
// but bloom may have skipped enough later keys that
// the next passing one is in a later block — in
// which case we skip the load.
let first_in_block = sorted_keys[passing[p]].0;
if self.comparator.compare(first_in_block, end_key) == core::cmp::Ordering::Greater {
// The next passing key is BEYOND this block's range: skip
// the load and seek the index at that key, so a sparse
// batch over a large table reads only the entries and
// partitions its keys fall in instead of stepping through
// every one between them.
let Some(reader) = self.block_index.forward_reader(first_in_block, table_seqno)
else {
break;
};
block_iter = reader;
continue;
}
// A wholly-deleted columnar block carries no keys; skip it.
let Some(data_block) = self.load_data_block(block_handle.as_ref(), &fd)? else {
continue;
};
// Drain passing keys that fall inside [..end_key].
//
// Three-way handling mirrors Table::point_read_inner's
// end-key boundary check:
// - Greater (key > end_key): key belongs to a later
// block. Break inner loop, advance outer.
// - Less (key < end_key): key is strictly inside
// this block. point_read decides; either way the
// key cannot continue into the next block (block
// keys are sorted, and a later block's first key
// is > this block's end_key), so we always advance
// p — set Some on hit, leave None on miss.
// - Equal (key == end_key): block end_key matches
// the query exactly. point_read may return None
// even when a visible version of THIS user key
// exists in the NEXT block (same-key spans block
// boundary — common with MVCC versions of a hot
// key). On None, do NOT advance p — break out so
// the next outer iteration loads the next block
// and retries the same key.
while p < passing.len() {
let key_idx = passing[p];
let (key, key_hash) = sorted_keys[key_idx];
match self.comparator.compare(key, end_key) {
core::cmp::Ordering::Greater => break,
core::cmp::Ordering::Less => {
if let Some(mut item) = data_block.point_read_hashed(
key,
Some(key_hash),
table_seqno,
&self.comparator,
)? {
// Translate table-local seqno back to
// the global coordinate so callers can
// compare results across tables /
// memtables (matches Table::get's
// contract).
item.key.seqno = apply_global_seqno(item.key.seqno, global_seqno);
results[key_idx] = Some(item);
}
p += 1;
}
core::cmp::Ordering::Equal => {
if let Some(mut item) = data_block.point_read_hashed(
key,
Some(key_hash),
table_seqno,
&self.comparator,
)? {
item.key.seqno = apply_global_seqno(item.key.seqno, global_seqno);
results[key_idx] = Some(item);
p += 1;
} else {
// Same user key may continue in the
// next block — leave p in place so the
// outer loop's next iteration retries
// this key against the next block.
break;
}
}
}
}
}
#[cfg(feature = "metrics")]
{
// core::* (vs the std re-export) for no-std friendliness;
// see the comment on the matching import above.
use core::sync::atomic::Ordering::Relaxed;
// Mirror Table::get's accounting: count negative
// point lookups that reached storage despite a
// filter being present. Only keys that passed bloom
// AND came back empty count.
if had_filter {
let negative_with_filter =
passing.iter().filter(|&&i| results[i].is_none()).count();
if negative_with_filter > 0 {
// filter_queries is AtomicUsize; the count is
// already a usize, no conversion needed.
self.metrics
.filter_queries
.fetch_add(negative_with_filter, Relaxed);
}
}
}
count_false_positives(&results);
Ok(results)
}
/// Shared setup for the prewarm and chunked block planners: rejects a table
/// that cannot contribute (empty input, or entirely above the read snapshot),
/// bloom-filters `sorted_keys` to the passing positions, and opens a forward
/// block-index reader at the first passing key.
///
/// `Ok(Some(..))` carries the passing positions, the reader, and the
/// table-local read seqno. `Ok(None)` means the table genuinely contributes no
/// block (nothing passes the snapshot/bloom/index). `Err` is a real
/// [`Table::check_bloom`] failure (a partitioned filter's block read) and is
/// propagated so the authoritative chunked planner surfaces it instead of
/// mistaking it for a miss; the best-effort prewarm planner maps it back to
/// `None`.
///
/// The filter answers are added to `tally` when one is given, for the
/// caller to count once it answers from this plan. Filter blocks are read
/// through `fd`, the caller's lookup.
#[expect(
clippy::indexing_slicing,
reason = "passing[0] is valid after the emptiness check"
)]
fn plan_block_walk_setup(
&self,
sorted_keys: &[(&[u8], u64)],
seqno: SeqNo,
tally: &mut crate::table::probe_stats::PlanCounts,
fd: &LookupFd,
) -> crate::Result<Option<(Vec<usize>, block_index::BlockIndexIterImpl, SeqNo)>> {
if sorted_keys.is_empty() {
return Ok(None);
}
let global_seqno = self.global_seqno();
let Some(table_seqno) = seqno.checked_sub(global_seqno) else {
return Ok(None);
};
if self.metadata.seqnos.0 >= table_seqno {
return Ok(None);
}
let mut passing: Vec<usize> = Vec::with_capacity(sorted_keys.len());
for (i, (key, hash)) in sorted_keys.iter().enumerate() {
// Below a tight-space restriction, as for a point read.
if self.is_below_restriction(key) {
continue;
}
// Counted with the plan, not here: a level handed to the serial
// resolve is probed and counted there.
let bloom = self.judge_bloom(key, *hash, fd)?;
Self::tally_bloom(tally, &bloom);
if !bloom.should_skip() {
passing.push(i);
}
}
if passing.is_empty() {
return Ok(None);
}
let Some(block_iter) = self
.block_index
.forward_reader(sorted_keys[passing[0]].0, table_seqno)
else {
// Every passed key lies past the last block: none is held.
self.tally_blockless(tally, table_seqno, passing.len());
return Ok(None);
};
Ok(Some((passing, block_iter, table_seqno)))
}
/// Counts in `tally` the `keys` the filter let through that no block of
/// the table can hold, read at `table_seqno`: each a false positive, as
/// [`Self::count_false_positive`] counts one, and a filter query that
/// reached the table, as a point read counts one.
pub(crate) fn tally_blockless(
&self,
tally: &mut crate::table::probe_stats::PlanCounts,
table_seqno: SeqNo,
keys: usize,
) {
if !self.has_filter() {
return;
}
tally.filter_queries += keys;
if table_seqno > self.metadata.seqnos.1 {
tally.probes.negatives += keys as u64;
}
}
/// Charges blocks of `block_type` a batched multi-get read is about to ask
/// of the filesystem, at the moment it is issued.
#[cfg_attr(
not(feature = "metrics"),
expect(
clippy::unused_self,
reason = "the table's counters are the feature's payload"
)
)]
pub(crate) fn record_batched_read(&self, block_type: BlockType, handles: &[BlockHandle]) {
#[cfg(feature = "metrics")]
for handle in handles {
crate::table::util::record_block_read(&self.metrics, block_type, handle.size().into());
}
#[cfg(not(feature = "metrics"))]
let _ = (block_type, handles);
}
/// Capacity in bytes of this table's (shared) block cache, for the level
/// prewarm's eviction-avoiding size bound.
pub(crate) fn cache_capacity(&self) -> u64 {
self.cache.capacity()
}
/// How many files the descriptor cache this table opens through keeps
/// open, or `None` when its descriptor is pinned and always open.
pub(crate) fn descriptor_capacity(&self) -> Option<u64> {
self.file_accessor
.as_descriptor_table()
.map(crate::descriptor_table::DescriptorTable::capacity)
}
/// This table's file, for a caller that reads its blocks itself.
///
/// # Errors
///
/// Propagates a failure to open the file.
pub(crate) fn open_file(&self) -> crate::Result<Arc<dyn crate::fs::FsFile>> {
self.lookup_file(LookupFd::default())
}
/// The file `fd` resolved for a lookup, or this table's file taken now,
/// owned for a caller that reads the lookup's blocks itself. Counted as the
/// load path counts the file it opens.
fn lookup_file(&self, fd: LookupFd) -> crate::Result<Arc<dyn crate::fs::FsFile>> {
Ok(fd.into_file(
&self.file_accessor,
&self.global_id(),
&self.path,
ReadCharge::Foreground,
#[cfg(feature = "metrics")]
&self.metrics,
)?)
}
/// Whether this table's data blocks need the special load path rather than a
/// plain bytes decode: Page-ECC (re-read recovery) or columnar (PAX
/// reconstruction). The chunked `multi_get` resolver reads these via
/// [`Table::load_data_block`] instead of [`Table::decode_data_block_from_bytes`].
pub(crate) fn is_chunk_special(&self) -> bool {
if self.metadata.ecc_params.is_some() {
return true;
}
#[cfg(feature = "columnar")]
if self.metadata.columnar {
return true;
}
false
}
/// Plans the data blocks [`Table::batch_get`] would read for `sorted_keys`
/// (bloom + block-index walk, no decode), returning this table's file handle,
/// the table-local read seqno, whether it needs the special load path, and per
/// block the positions (into `sorted_keys`) of the keys that fall in it.
///
/// Used by the chunked `multi_get` resolver to read MANY SSTs' blocks in one
/// batch and point-read directly, without the cache. Conservative at block
/// boundaries: a key equal to a block's end key is listed in that block AND
/// the next (an MVCC version of the same key can span the boundary), mirroring
/// `batch_get`'s span-retry; the higher-seqno hit wins at resolution.
///
/// `Ok(None)` means this table covers none of `sorted_keys`. Unlike the
/// best-effort prewarm planner, a bloom-probe or table-open failure is
/// PROPAGATED (not swallowed to `None`): the chunked resolver is authoritative
/// for its level, so a swallowed error would let a stale lower level answer.
///
/// # Errors
///
/// Propagates a bloom-probe or table-open failure.
///
/// The filter answers, and the filter queries and skips they make, are
/// added to `tally`: the chunked resolve counts them once it answers from
/// its plan, and not when it hands the level to the serial resolve, which
/// probes the same filters again.
#[expect(
clippy::indexing_slicing,
reason = "`passing` positions index into `sorted_keys` (< its len); `passing[p]` \
is guarded by `p < passing.len()` each iteration."
)]
pub(crate) fn plan_block_tasks(
&self,
sorted_keys: &[(&[u8], u64)],
seqno: SeqNo,
tally: &mut crate::table::probe_stats::PlanCounts,
) -> crate::Result<Option<BlockTaskPlan>> {
// The filter and index blocks the plan reads, and the file the planned
// blocks are read through after it, share one descriptor access.
let fd = LookupFd::default();
let Some((passing, mut block_iter, table_seqno)) =
self.plan_block_walk_setup(sorted_keys, seqno, tally, &fd)?
else {
return Ok(None);
};
let mut blocks: Vec<(BlockHandle, Vec<usize>)> = Vec::new();
let mut p = 0_usize;
while p < passing.len() {
// None ends the index; an Err (index-read / decode failure) is
// PROPAGATED, not treated as end-of-index. Swallowing it would skip
// the rest of this table and let a lower level answer a key the
// failed table actually covers (same `?` contract as `batch_get`).
let Some(handle_result) = block_iter.next_with(&fd) else {
break;
};
let block_handle = handle_result?;
let end_key = block_handle.end_key();
let first_in_block = sorted_keys[passing[p]].0;
if self.comparator.compare(first_in_block, end_key) == core::cmp::Ordering::Greater {
// Sought at the next key, as `batch_get` does, instead of
// stepped through every entry between.
let Some(reader) = self.block_index.forward_reader(first_in_block, table_seqno)
else {
break;
};
block_iter = reader;
continue;
}
let handle = *block_handle.as_ref();
let mut block_keys: Vec<usize> = Vec::new();
while p < passing.len() {
let pos = passing[p];
match self.comparator.compare(sorted_keys[pos].0, end_key) {
core::cmp::Ordering::Greater => break,
core::cmp::Ordering::Less => {
block_keys.push(pos);
p += 1;
}
// Equal: list in THIS block and (by not advancing p) the next,
// since a version of this key may continue across the boundary.
core::cmp::Ordering::Equal => {
block_keys.push(pos);
break;
}
}
}
blocks.push((handle, block_keys));
}
// The passed keys past the last block. A key equal to that block's
// end key is listed in it and left for a next block there is not.
let listed = blocks.last().map(|(_, keys)| keys.as_slice());
let blockless = passing
.iter()
.skip(p)
.filter(|pos| !listed.is_some_and(|keys| keys.contains(pos)))
.count();
self.tally_blockless(tally, table_seqno, blockless);
if blocks.is_empty() {
return Ok(None);
}
// The blocks are read through this file, with no second lookup.
let file = self.lookup_file(fd)?;
Ok(Some((file, table_seqno, self.is_chunk_special(), blocks)))
}
/// Decodes the data block at `handle` from its on-disk `bytes` (read by the
/// batched multi-get), using the same path as [`Table::load_data_block`]
/// for a non-special table ([`Block::from_reader`] shares the header /
/// decrypt helpers), so the block is byte-identical, and puts it in the
/// cache when its weight fits in `room`, taking it out of `room`. Not for
/// Page-ECC / columnar tables
/// ([`is_chunk_special`]).
///
/// # Errors
///
/// Propagates a corruption / decode error (the resolver surfaces it).
pub(crate) fn decode_data_block_keeping(
&self,
bytes: &[u8],
handle: &BlockHandle,
room: &mut u64,
lead: Option<crate::cache::BlockLead>,
) -> crate::Result<DataBlock> {
let block = self.decode_block_from_bytes(bytes, *handle.offset(), BlockType::Data)?;
// Weighed as the cache weighs it: decoded, not as it was read.
let weight = crate::cache::block_weight(&block);
if weight <= *room {
*room -= weight;
self.cache
.insert_block(self.global_id(), handle.offset(), block.clone());
}
// Published whether cached or not: a point read waiting on it caches
// the block itself when this read had no room left for it.
if let Some(lead) = lead {
lead.publish(&block);
}
self.data_block_of(block)
}
/// How a batched read gets the data block at `handle` of this Page-ECC or
/// columnar table: as bytes it reads with its other blocks, then
/// [`Self::decode_special_block`], or through the table's own load path in
/// a job.
///
/// A columnar group goes through the load path when its directory is
/// cached (its pages may be too, which that path takes from the cache),
/// when it is wider than one request, or when it carries Page-ECC, whose
/// recovery re-reads a page to confirm a heal.
pub(crate) fn batched_special_read(&self, handle: &BlockHandle) -> SpecialRead {
if self.check_block_size(handle).is_err() {
return SpecialRead::Load;
}
#[cfg(feature = "columnar")]
if self.metadata.columnar
&& (self.metadata.ecc_params.is_some()
|| handle.size() > self.read_budget().io_buffer()
|| self
.cache
.get_directory(self.global_id(), handle.offset(), false)
.is_some())
{
return SpecialRead::Load;
}
SpecialRead::Bytes {
offset: *handle.offset(),
len: handle.size() as usize,
}
}
/// The data block at `handle` of this Page-ECC table, when the cache
/// holds it, counted as a cache hit as the load path counts one. Never a
/// columnar group's: the cache holds its directory and pages, not the
/// group, and the load path assembles it from them.
pub(crate) fn cached_special_block(&self, handle: &BlockHandle) -> Option<DataBlock> {
#[cfg(feature = "columnar")]
if self.metadata.columnar {
return None;
}
self.cached_data_block(handle)
}
/// The role a batched read of a Page-ECC or columnar data block is
/// charged to: a columnar group is read as its pages are.
#[cfg_attr(
not(feature = "columnar"),
expect(clippy::unused_self, reason = "only a columnar table reads as pages")
)]
pub(crate) fn special_read_role(&self) -> BlockType {
#[cfg(feature = "columnar")]
if self.metadata.columnar {
return BlockType::ColumnPage;
}
BlockType::Data
}
/// The data block at `handle` of this Page-ECC or columnar table, from the
/// `bytes` [`Self::batched_special_read`] asked for: checked, recovered
/// and reconstructed exactly as the load path does it, cached (a row block
/// while it fits in `room`), and `None` when a columnar block's delete
/// mask removes every row.
///
/// # Errors
///
/// A check fails; and a block whose payload Page-ECC had to correct,
/// which the load path reads again to schedule its heal, so the caller
/// takes that path for it.
pub(crate) fn decode_special_block(
&self,
handle: &BlockHandle,
bytes: alloc::vec::Vec<u8>,
room: &mut u64,
) -> crate::Result<Option<DataBlock>> {
#[cfg(feature = "columnar")]
if self.metadata.columnar {
let fd = LookupFd::default();
let blocks = self
.group_read(handle, ReadCharge::Foreground, &fd)
.load_whole(&crate::Slice::from(bytes))?;
return self.columnar_data_block_from(handle, &blocks, ReadCharge::Foreground);
}
let compression = self.metadata.data_block_compression;
let transform = crate::table::util::build_block_transform(
compression,
self.encryption.as_deref(),
self.metadata.ecc_params,
#[cfg(zstd_any)]
self.zstd_dictionary.as_deref(),
)?;
let identity = crate::table::block::BlockIdentity {
table_id: self.global_id().table_id(),
block_type: BlockType::Data,
dict_id: compression.dict_id(),
window_log: 0,
};
let (header, payload, status, recovery) =
Block::verify_frame(bytes, *handle, identity, &transform)?;
if matches!(status, crate::table::block::EccStatus::Corrected) {
return Err(crate::Error::Io(crate::io::Error::other(
"a block Page-ECC corrected is loaded through the path that heals it",
)));
}
let mut produced = 0;
let data = Block::decompress_payload(&header, payload, &transform, &mut produced);
#[cfg(feature = "metrics")]
crate::table::util::record_block_decoded(&self.metrics, ReadCharge::Foreground, produced);
let block = crate::table::util::admit_read_block(
self.global_id(),
&self.path,
&self.file_accessor,
handle,
BlockType::Data,
compression,
self.encryption.as_deref(),
self.metadata.ecc_params,
#[cfg(zstd_any)]
self.zstd_dictionary.as_deref(),
self.heal_hints.get().map(AsRef::as_ref),
#[cfg(feature = "metrics")]
&self.metrics,
ReadCharge::Foreground,
Block {
header,
data: data?,
},
status,
recovery,
)?;
let weight = crate::cache::block_weight(&block);
if weight <= *room {
*room -= weight;
self.cache
.insert_block(self.global_id(), handle.offset(), block.clone());
}
self.data_block_of(block).map(Some)
}
/// `block`, a data block of this table, as the point reads take it.
pub(crate) fn data_block_of(&self, block: Block) -> crate::Result<DataBlock> {
DataBlock::from_loaded(block, self.metadata.kv_checksum_algo.is_some())
}
/// Refuses the block at `handle` when its size is one no block of this
/// table can have, without allocating for it.
///
/// # Errors
///
/// The size is past the largest block this table can hold.
pub(crate) fn check_block_size(&self, handle: &BlockHandle) -> crate::Result<()> {
crate::table::block::check_on_disk_size(
u64::from(handle.size()),
self.encryption.as_deref(),
self.metadata.ecc_params,
)
}
/// The data block at `handle`, when the cache holds it, counted as a cache
/// hit as the load path counts one.
pub(crate) fn cached_data_block(&self, handle: &BlockHandle) -> Option<DataBlock> {
let block = self.cached_block(handle, BlockType::Data)?;
DataBlock::from_loaded(block, self.metadata.kv_checksum_algo.is_some()).ok()
}
/// The block of `block_type` at `handle`, when the cache holds it, counted
/// as a cache hit as the load path counts one.
pub(crate) fn cached_block(
&self,
handle: &BlockHandle,
block_type: BlockType,
) -> Option<Block> {
let block = self
.cache
.get_block(self.global_id(), handle.offset())
.filter(|block| block.header.block_type == block_type)?;
#[cfg(feature = "metrics")]
crate::table::util::record_block_load_cached(&self.metrics, block_type);
Some(block)
}
/// The codec a block a batched read decodes is written with. Filter blocks
/// are written uncompressed and without the dictionary, index blocks with
/// the index codec, data blocks with the data codec and its dictionary:
/// the same choice each block's load makes.
fn batched_codec(&self, block_type: BlockType) -> CompressionType {
match block_type {
BlockType::Filter => CompressionType::None,
BlockType::Index => self.metadata.index_block_compression,
_ => self.metadata.data_block_compression,
}
}
/// Claims the lead of the load of the block at `handle`, of `block_type`,
/// for a batched read about to read it with others, when `lead` lets it:
/// a point read missing the same block meanwhile then waits for it rather
/// than reading it again. `None` when the read reads it alone. Never
/// waits, since a batched read holds the leads of its other blocks.
pub(crate) fn claim_batched_block(
&self,
handle: &BlockHandle,
block_type: BlockType,
lead: bool,
) -> Option<crate::cache::BlockLead<'_>> {
self.cache.claim_batched_block(
self.global_id(),
handle.offset(),
crate::cache::LoadIdentity::Block {
block_type,
size: handle.size(),
compression: self.batched_codec(block_type),
encrypted: self.encryption.is_some(),
},
lead,
)
}
/// Decodes the on-disk `bytes` of this table's block of `block_type` read
/// at `offset`, with the codec that block type is written with, exactly as
/// the load path decodes it; a block of another type is refused. Not for
/// Page-ECC tables, whose recovery needs the re-reading load path.
///
/// # Errors
///
/// Propagates a corruption or decode error.
pub(crate) fn decode_block_from_bytes(
&self,
bytes: &[u8],
offset: u64,
block_type: BlockType,
) -> crate::Result<Block> {
// Never an ECC table's block: `from_reader` repairs an ECC-corrected
// payload silently, and a corrected block kept in the cache as clean
// would let later cache hits skip the heal scheduling the load path
// does, leaving the latent fault on disk.
debug_assert!(
self.metadata.ecc_params.is_none(),
"a Page-ECC table's blocks are read through the load path, which heals"
);
let compression = self.batched_codec(block_type);
let transform = crate::table::util::build_block_transform(
compression,
self.encryption.as_deref(),
self.metadata.ecc_params,
#[cfg(zstd_any)]
if block_type == BlockType::Data {
self.zstd_dictionary.as_deref()
} else {
None
},
)?;
let identity = crate::table::block::BlockIdentity {
table_id: self.global_id().table_id(),
block_type,
dict_id: compression.dict_id(),
window_log: 0,
};
// The transform runs here, outside the block cache, so its output is
// charged here, before the result is judged: a block the length or
// role check refuses was still decoded. The read was charged when the
// batch was issued.
let mut produced = 0;
let decoded = Block::from_reader_counting(
&mut crate::io::Cursor::new(bytes),
identity,
&transform,
crate::table::block::ChecksumAt::block(identity.table_id, block_type, offset),
&mut produced,
);
#[cfg(feature = "metrics")]
self.metrics
.block_bytes_decoded
.fetch_add(produced as u64, core::sync::atomic::Ordering::Relaxed);
let block = decoded?;
if block.header.block_type != block_type {
return Err(crate::Error::InvalidTag((
"BlockType",
block.header.block_type.into(),
)));
}
// Loaded from disk, as the load path counts a block once it passed
// every check.
#[cfg(feature = "metrics")]
crate::table::util::record_block_loaded(&self.metrics, block_type);
Ok(block)
}
/// Point-reads `key` in an already-decoded `block`, translating the
/// table-local seqno of any hit back to the global coordinate (matching
/// [`Table::batch_get`]'s contract).
///
/// # Errors
///
/// Propagates a decode / corruption error from the point read.
pub(crate) fn point_read_translated(
&self,
block: &DataBlock,
key: &[u8],
key_hash: u64,
table_seqno: SeqNo,
) -> crate::Result<Option<InternalValue>> {
let global_seqno = self.global_seqno();
Ok(block
.point_read_hashed(key, Some(key_hash), table_seqno, &self.comparator)?
.map(|mut item| {
item.key.seqno = apply_global_seqno(item.key.seqno, global_seqno);
item
}))
}
/// Whether the table has a filter to probe.
pub(crate) fn has_filter(&self) -> bool {
self.pinned_filter_block.is_some()
|| self.pinned_filter_index.is_some()
|| self.regions.filter.is_some()
|| self.regions.filter_tli.is_some()
}
/// Creates a scanner over the `Table`.
///
/// The scanner is ĺogically the same as a normal iter(),
/// however it uses its own file descriptor, does not look into the block cache
/// and uses buffered I/O.
///
/// Used for compactions and thus not available to a user.
///
/// # Errors
///
/// Will return `Err` if an IO error occurs.
#[doc(hidden)]
pub fn scan(&self) -> crate::Result<Scanner> {
self.scan_paced(None)
}
/// [`Self::scan`], telling `pace` how long each read from the file takes,
/// from the first.
pub(crate) fn scan_paced(
&self,
pace: Option<&crate::table::util::Pacer>,
) -> crate::Result<Scanner> {
#[expect(
clippy::expect_used,
reason = "there shouldn't be 4 billion data blocks in a single table"
)]
let mut block_count: usize = self
.metadata
.data_block_count
.try_into()
.expect("data block count should fit");
// Tight-space restriction: start at the first LIVE block. Blocks
// wholly below the bound are hole-punched (they read as zeros and
// cannot decode), and even before the punch runs their rows'
// authoritative copies live in the superseding slice output — a
// compaction reading them here would merge every prefix row twice.
// The bound never exceeds the table's last key, so at least one live
// block always remains; the straddling block's sub-bound entries are
// dropped by the scanner's key filter.
let mut start_offset = 0u64;
// A columnar scan streams the data without the index, so the index
// hands it the entry of every group it will read: a group whose tag or
// lengths disagree with its entry is refused against it, as an indexed
// read refuses it.
let mut groups = Vec::new();
if self.1.is_some() || self.metadata.columnar {
let mut started = self.1.is_none();
// The index walk reads from the file too, a cold index at length:
// its reads report as the scan's do.
let walk = self.maintenance_index_walk();
let walk = match pace {
Some(pace) => walk.with_pace(alloc::sync::Arc::clone(pace)),
None => walk,
};
for keyed in walk {
let keyed = keyed?;
if !started {
if let Some(bound) = &self.1
&& self.comparator.compare(keyed.end_key(), bound.as_ref())
== core::cmp::Ordering::Less
{
// Cannot underflow: this loop walks the block index the
// count was derived from, so at most `block_count`
// decrements can ever run.
block_count -= 1;
continue;
}
start_offset = keyed.offset().0;
started = true;
if !self.metadata.columnar {
break;
}
}
groups.push(*keyed.as_ref());
}
}
self.scan_groups(block_count, start_offset, groups, pace)
}
/// A scanner of `block_count` blocks from `start_offset`, checking each
/// columnar group it streams against its index entry in `groups`, and
/// telling `pace` how long its reads take.
fn scan_groups(
&self,
block_count: usize,
start_offset: u64,
groups: Vec<BlockHandle>,
pace: Option<&crate::table::util::Pacer>,
) -> crate::Result<Scanner> {
Scanner::new(
&self.fs,
&self.path,
block_count,
self.metadata.data_block_compression,
self.global_seqno(),
self.encryption.clone(),
self.metadata.ecc_params,
self.metadata.kv_checksum_algo.is_some(),
#[cfg(zstd_any)]
self.zstd_dictionary.clone(),
self.comparator.clone(),
self.metadata.id,
self.metadata.columnar,
self.metadata.data_block_restart_interval,
start_offset,
self.1.clone(),
groups,
pace.cloned(),
)
}
/// Scans this columnar SST lazily, yielding one [`ColumnBatch`] per row page
/// of every data block that survives the optional predicate, each carrying
/// only the projected columns. A block is read when the scan reaches it, so
/// a caller that stops early reads nothing more.
///
/// `projection` lists the column ids to decode; every other column is
/// stepped over without decoding. When `predicate` is set, a block or row
/// page whose statistics prove it out of range is skipped without being
/// loaded, and, unless the predicate only prunes, each surviving one is
/// filtered to the rows that match. How far the predicate ran is what
/// [`ColumnarCursor::predicate_support`] reports for the blocks read so far;
/// [`Tree::columnar_scan`](crate::Tree::columnar_scan) reports it for a
/// whole scan.
///
/// [`ColumnBatch`]: crate::table::columnar::ColumnBatch
/// [`ColumnarCursor::predicate_support`]: crate::table::columnar_cursor::ColumnarCursor::predicate_support
///
/// # Errors
///
/// Returns an error if this SST is not columnar, and, while iterating, on a
/// block read / decode failure.
#[cfg(feature = "columnar")]
pub fn columnar_scan(
&self,
projection: &[u16],
predicate: Option<&crate::table::columnar_predicate::ColumnRangePredicate>,
) -> crate::Result<crate::table::columnar_cursor::ColumnarCursor> {
self.columnar_cursor(
projection,
predicate,
core::ops::Bound::Unbounded,
core::ops::Bound::Unbounded,
None,
)
}
/// [`Self::columnar_scan`] over the rows within `lo..hi`: blocks wholly
/// outside the range are not read. Rows of a boundary block that fall
/// outside it are still yielded, for the caller to mask. With `share`,
/// a row group larger than it is read in runs of row pages that fit it.
#[cfg(feature = "columnar")]
pub(crate) fn columnar_cursor(
&self,
projection: &[u16],
predicate: Option<&crate::table::columnar_predicate::ColumnRangePredicate>,
lo: core::ops::Bound<UserKey>,
hi: core::ops::Bound<UserKey>,
share: Option<u64>,
) -> crate::Result<crate::table::columnar_cursor::ColumnarCursor> {
crate::table::columnar_cursor::ColumnarCursor::new(
self, projection, predicate, lo, hi, share,
)
}
/// Creates an iterator over the `Table`.
///
/// # Errors
///
/// Will return `Err` if an IO error occurs.
#[must_use]
#[doc(hidden)]
pub fn iter(&self) -> impl DoubleEndedIterator<Item = crate::Result<InternalValue>> + use<> {
self.range(..)
}
/// Collects the entries of this SST whose global seqno is in
/// `[target_seqno, end_seqno]`, applying the per-block seqno-bounds skip
/// when the SST carries it: a data block whose `seqno_bounds` entry cannot
/// overlap the window is skipped without being read, and an SST without
/// the section reads and filters every block. Entries come back in the
/// SST's stored order (key-ascending, seqno-descending within a key);
/// ordering across sources is the caller's job.
///
/// The upper bound is INCLUSIVE so that a watermark of [`SeqNo::MAX`] — the
/// value a scan derives when an entry sits at the maximum seqno — still
/// delivers the entry that defined it.
/// The upper bound lets the tree-level scan pin a stable snapshot watermark
/// so a concurrent write cannot leak in mid-scan.
///
/// `block_skip` enables the per-block seqno-bounds optimization (skip data
/// blocks whose recorded `[seqno_min, seqno_max]` cannot overlap the
/// window). Pass `false` for a paranoid full scan that reads every block and
/// filters per entry, so even an undetected-corrupt seqno bound (one that
/// somehow slipped past the block XXH3 checksum) cannot cause a qualifying
/// record to be skipped.
///
/// `keys` bounds the blocks read, in the table comparator's order: the
/// index is sought to the first block that can hold the lower bound and
/// the walk stops after the first block reaching past the upper one. The
/// blocks at either end hold keys outside the bounds too; filtering those
/// is the caller's job.
///
/// # Errors
///
/// Returns `Err` if reading the index or a data block fails.
#[doc(hidden)]
pub fn scan_seqno_range(
&self,
target_seqno: SeqNo,
end_seqno: SeqNo,
block_skip: bool,
keys: (Bound<&[u8]>, Bound<&[u8]>),
) -> crate::Result<Vec<InternalValue>> {
// Bulk-ingested tables store entries at LOCAL seqno coordinates with a
// `global_seqno` offset; the on-disk seqno bounds and per-entry seqnos
// are all local. Translate the incoming global target down to local
// for the comparisons, then translate matched record seqnos back up to
// global before returning — exactly as `Table::get` does. For a
// non-ingested table `global_seqno` is 0 and both translations are
// no-ops.
let global_seqno = self.global_seqno();
// An upper bound BELOW the ingest base excludes the whole table: every
// effective seqno here is >= `global_seqno`. Checked BEFORE the
// saturating translation, because both bounds would clamp to local 0
// and `[0, 0]` is a valid one-seqno window that matches every stored
// row of a bulk-ingested table (all at local 0) — returning entries
// whose translated-back seqno exceeds the caller's inclusive bound.
if end_seqno < global_seqno {
return Ok(Vec::new());
}
// Here the saturating clamp to 0 is the INTENDED result, not the silent
// overflow-masking the point-read path avoids: a lower bound below the
// offset means "start at the table's first entry", so clamping the
// translated lower bound to 0 is exactly right.
let local_target = target_seqno.saturating_sub(global_seqno);
// Upper bound in local coords. `SeqNo::MAX` (the unbounded case) maps to
// `MAX - global_seqno`, still far above any reachable local seqno, so
// every entry passes (effectively unbounded); a bound below the offset
// returned above already.
let local_end = end_seqno.saturating_sub(global_seqno);
// Empty window (a target ABOVE the watermark — e.g. a bulk-ingest offset
// that puts the whole table below the caller's target): nothing can
// qualify, so skip walking the index entirely. Without this a legacy SST
// (no per-block seqno bounds) would load + filter every block to return
// nothing on every poll. The bound is inclusive, so equality is a
// one-seqno window, not an empty one.
if local_target > local_end {
return Ok(Vec::new());
}
use block_index::BlockIndexIter;
use core::cmp::Ordering;
let mut out = Vec::new();
let mut index = self.block_index.iter();
// The first block that can hold the lower bound: every block before it
// ends below the bound.
if let Bound::Included(lo) | Bound::Excluded(lo) = keys.0
&& !index.seek_lower(lo, SeqNo::MAX)
{
return Ok(out);
}
for handle in index {
let handle = handle?;
// The last block that can hold a key within the upper bound. A
// block ending ON an included bound may be followed by older
// versions of that key, and the walk goes on to them.
let reaches_hi = match keys.1 {
Bound::Included(hi) => {
self.comparator.compare(handle.end_key(), hi) == Ordering::Greater
}
Bound::Excluded(hi) => {
self.comparator.compare(handle.end_key(), hi) != Ordering::Less
}
Bound::Unbounded => false,
};
self.collect_seqno_range(&handle, (local_target, local_end), block_skip, &mut out)?;
if reaches_hi {
break;
}
}
Ok(out)
}
/// The data blocks that can hold a version of `key`, whatever its seqno,
/// found through the index alone, with the table's file to read them
/// through. A key's versions run from the block its first one is in
/// through every block ending on the key.
///
/// # Errors
///
/// The index or the file fails to read.
pub(crate) fn key_blocks(
&self,
key: &[u8],
) -> crate::Result<(Arc<dyn crate::fs::FsFile>, Vec<BlockHandle>)> {
let file = self.open_file()?;
let mut blocks = Vec::new();
if let Some(mut index) = self.block_index.forward_reader(key, SeqNo::MAX) {
let fd = LookupFd::default();
while let Some(handle) = index.next_with(&fd) {
let handle = handle?;
// A block ending before the key holds none of it; one ending
// on it may be followed by older versions in the next.
match self.comparator.compare(key, handle.end_key()) {
core::cmp::Ordering::Greater => {}
core::cmp::Ordering::Equal => blocks.push(*handle.as_ref()),
core::cmp::Ordering::Less => {
blocks.push(*handle.as_ref());
break;
}
}
}
}
Ok((file, blocks))
}
/// The versions of `key` in `block`, one of the blocks
/// [`Self::key_blocks`] names, newest first, with their global seqnos, of
/// those visible at `seqno`; and whether the block holds a version of the
/// key at all, whatever its seqno, as the table's reader reports it.
pub(crate) fn key_versions(
&self,
block: &DataBlock,
key: &[u8],
seqno: SeqNo,
) -> (Vec<InternalValue>, bool) {
let global_seqno = self.global_seqno();
let mut versions = Vec::new();
let mut found = false;
let data = &block.inner.data;
let mut entries = block.iter(self.comparator.clone());
if !entries.seek(key, SeqNo::MAX) {
return (versions, found);
}
for item in entries {
let mut version = item.materialize(data);
match self.comparator.compare(&version.key.user_key, key) {
core::cmp::Ordering::Less => continue,
core::cmp::Ordering::Greater => break,
core::cmp::Ordering::Equal => {}
}
found = true;
// Filtered before it is translated, as the table's reader takes a
// version: a sum past `SeqNo::MAX` is above every snapshot.
if let Some(global) = version.key.seqno.checked_add(global_seqno)
&& crate::range::seqno_filter(global, seqno)
{
version.key.seqno = global;
versions.push(version);
}
}
(versions, found)
}
/// The entries of the block `handle` names whose local seqno lies in
/// `window`, with their global seqnos, appended to `out`.
fn collect_seqno_range(
&self,
handle: &KeyedBlockHandle,
(local_target, local_end): (SeqNo, SeqNo),
block_skip: bool,
out: &mut Vec<InternalValue>,
) -> crate::Result<()> {
let global_seqno = self.global_seqno();
// Tight-space restriction: a block whose last key is below the bound
// sits entirely in the punched-out (zeroed) prefix — never read it.
// The block straddling the bound is intact (punch starts at its
// offset) and is filtered per entry in the loop below.
if let Some(bound) = &self.1
&& self.comparator.compare(handle.end_key(), bound) == core::cmp::Ordering::Less
{
return Ok(());
}
// Block-skip: look this block's seqno bounds up in the parallel
// `seqno_bounds` section (keyed by file offset). If its (local) min
// exceeds the upper bound, or its (local) max is below the target, it
// cannot reference a qualifying record — skip the data-block read.
// Bounds live in the section, NOT inline in the index entry, so a
// point read never pays for them. Disabled in paranoid full-scan
// mode (`block_skip == false`); absent for a table written without
// the section → no skip, full filter (correct regardless).
if block_skip
&& let Some((seqno_min, seqno_max)) =
self.seqno_bounds.bounds_for(handle.as_ref().offset().0)
&& (seqno_max < local_target || seqno_min > local_end)
{
return Ok(());
}
// A wholly-deleted columnar block carries no keys; skip it.
let Some(block) = self.load_data_block(handle.as_ref(), &LookupFd::default())? else {
return Ok(());
};
let data = &block.inner.data;
for item in block.iter(self.comparator.clone()) {
let mut value = item.materialize(data);
// Drop entries below the restriction bound in the straddling
// block (their authoritative copy lives in the superseding
// output table).
if let Some(bound) = &self.1
&& self.comparator.compare(&value.key.user_key, bound) == core::cmp::Ordering::Less
{
continue;
}
if value.key.seqno >= local_target && value.key.seqno <= local_end {
value.key.seqno = apply_global_seqno(value.key.seqno, global_seqno);
out.push(value);
}
}
Ok(())
}
/// Creates a ranged iterator over the `Table`.
///
/// # Errors
///
/// Will return `Err` if an IO error occurs.
#[must_use]
#[doc(hidden)]
pub fn range<R: RangeBounds<UserKey> + Send>(
&self,
range: R,
) -> impl DoubleEndedIterator<Item = crate::Result<InternalValue>> + Send + use<R> {
self.range_iter(range)
}
/// Builds the positional delete mask for a columnar iterator from the
/// on-open cache: the delete-bitmap plus each block's first global row
/// position. `None` when the segment has no deletes, so a delete-free table
/// pays nothing. Cheap (two `Arc` clones); the cumulative row counts were
/// computed once on open.
fn build_delete_mask(&self) -> Option<iter::DeleteMask> {
let block_start_rows = self.delete_block_starts.clone()?;
Some(iter::DeleteMask {
bitmap: self.delete_bitmap.clone(),
block_start_rows,
})
}
/// Like [`Self::range`] but returns the concrete [`iter::Iter`] reader.
///
/// The seekable range pipeline holds the concrete type so it can re-position
/// the reader in place via [`Self::reseek_range`] instead of rebuilding it.
pub(crate) fn range_iter<R: RangeBounds<UserKey> + Send>(&self, range: R) -> iter::Iter {
let index_iter = self.block_index.iter();
let mut iter = Iter::new(
self.global_id(),
self.global_seqno(),
self.path.clone(),
index_iter,
self.file_accessor.clone(),
self.cache.clone(),
self.metadata.data_block_compression,
self.encryption.clone(),
self.metadata.ecc_params,
self.heal_hints.get().cloned(),
self.metadata.kv_checksum_algo.is_some(),
self.metadata.columnar,
self.build_delete_mask(),
#[cfg(zstd_any)]
self.zstd_dictionary.clone(),
self.comparator.clone(),
#[cfg(feature = "zstd")]
self.block_layout.clone(),
#[cfg(feature = "zstd")]
self.metadata.data_block_restart_interval,
#[cfg(feature = "metrics")]
self.metrics.clone(),
)
.with_read_budget(self.read_budget());
match range.start_bound() {
Bound::Included(key) => iter.set_lower_bound(iter::Bound::Included(key.clone())),
Bound::Excluded(key) => iter.set_lower_bound(iter::Bound::Excluded(key.clone())),
Bound::Unbounded => {}
}
// Tight-space restriction: raise the scan's lower bound up to `bound`
// when this version restricts the table, so the iterator never walks
// index entries pointing into the punched-out (zeroed) prefix below
// `bound`. Only raises (never lowers) the requested start: a request
// already at or above `bound` is left untouched.
if let Some(bound) = &self.1 {
let raise = match range.start_bound() {
Bound::Included(key) | Bound::Excluded(key) => {
self.comparator.compare(bound, key) == core::cmp::Ordering::Greater
}
Bound::Unbounded => true,
};
if raise {
iter.set_lower_bound(iter::Bound::Included(bound.clone()));
}
}
match range.end_bound() {
Bound::Included(key) => iter.set_upper_bound(iter::Bound::Included(key.clone())),
Bound::Excluded(key) => iter.set_upper_bound(iter::Bound::Excluded(key.clone())),
Bound::Unbounded => {}
}
iter
}
/// Re-position an existing [`iter::Iter`] (produced by [`Self::range`] on
/// this same table) to a fresh `range`, reusing its owned index iterator and
/// `Arc` handles instead of constructing a new reader.
///
/// Applies the exact same bound translation as [`Self::range`] (including the
/// tight-space lower-bound raise), so the re-seeked iterator yields the same
/// entries a freshly-built `self.range(range)` would. Used by the seekable
/// range pipeline to move leaf cursors without per-seek allocation.
#[doc(hidden)]
pub fn reseek_range<R: RangeBounds<UserKey> + Send>(&self, iter: &mut iter::Iter, range: R) {
iter.reset_for_reseek();
match range.start_bound() {
Bound::Included(key) => iter.set_lower_bound(iter::Bound::Included(key.clone())),
Bound::Excluded(key) => iter.set_lower_bound(iter::Bound::Excluded(key.clone())),
Bound::Unbounded => {}
}
// Mirror `range()`'s tight-space restriction: raise the scan's lower
// bound up to `bound` when this version restricts the table.
if let Some(bound) = &self.1 {
let raise = match range.start_bound() {
Bound::Included(key) | Bound::Excluded(key) => {
self.comparator.compare(bound, key) == core::cmp::Ordering::Greater
}
Bound::Unbounded => true,
};
if raise {
iter.set_lower_bound(iter::Bound::Included(bound.clone()));
}
}
match range.end_bound() {
Bound::Included(key) => iter.set_upper_bound(iter::Bound::Included(key.clone())),
Bound::Excluded(key) => iter.set_upper_bound(iter::Bound::Excluded(key.clone())),
Bound::Unbounded => {}
}
}
fn read_tli(
regions: &ParsedRegions,
file: &dyn FsFile,
table_id: TableId,
compression: CompressionType,
encryption: Option<&dyn crate::encryption::EncryptionProvider>,
ecc: Option<crate::table::block::EccParams>,
) -> crate::Result<IndexBlock> {
// Tail copy first (preferred): if a fresh `tli_tail` exists it
// landed after the head `tli`, so it's the most-recently
// fsynced copy. On any decode / decrypt / checksum failure
// fall back to the head `tli` if present.
//
// Both copies encode the same handles list (the writer hands
// a single `tli_bytes` buffer to both sites) and both are
// written under the same `CompressionType`
// (`metadata.index_block_compression`); the block header does
// not record a compression tag, so this single value decodes
// either copy. Encryption nonce differs per copy (fresh per
// `Block::write_into`) and the ciphertext therefore differs
// byte-for-byte, but both decrypt to the same plaintext
// IndexBlock.
//
// Tables written before the TLI-mirror change have no
// `tli_tail`; reader falls straight through to the head copy.
if let Some(tail_handle) = regions.tli_tail {
log::trace!("Reading TLI tail mirror, with tli_tail_ptr={tail_handle:?}");
match Self::read_tli_at(file, tail_handle, table_id, compression, encryption, ecc) {
Ok(idx) => return Ok(idx),
Err(tail_err) => {
log::warn!(
"TLI tail mirror unreadable ({tail_err}); falling back to TLI head copy at {:?}",
regions.tli,
);
// Match the meta-mirror pattern: when BOTH
// copies fail, surface the original `tail_err`
// (callers care about the authoritative /
// preferred copy's failure mode). The head
// failure goes to the log so it's not silently
// dropped from diagnostics.
log::trace!("Reading TLI head copy, with tli_ptr={:?}", regions.tli);
return match Self::read_tli_at(
file,
regions.tli,
table_id,
compression,
encryption,
ecc,
) {
Ok(idx) => Ok(idx),
Err(head_err) => {
log::warn!(
"TLI head copy also unreadable ({head_err}); returning original tail error",
);
Err(tail_err)
}
};
}
}
}
log::trace!("Reading TLI head copy, with tli_ptr={:?}", regions.tli);
Self::read_tli_at(file, regions.tli, table_id, compression, encryption, ecc)
}
fn read_tli_at(
file: &dyn FsFile,
handle: BlockHandle,
table_id: TableId,
compression: CompressionType,
encryption: Option<&dyn crate::encryption::EncryptionProvider>,
ecc: Option<crate::table::block::EccParams>,
) -> crate::Result<IndexBlock> {
let block = Block::from_file(
file,
handle,
crate::table::block::BlockIdentity {
table_id,
block_type: BlockType::Index,
dict_id: 0,
window_log: 0,
},
&{
// Index blocks are SST blocks that omit the block_flags byte,
// so ECC presence comes from the per-SST descriptor: upgrade
// to the `*Ecc` transform when this table was written with
// Page ECC. Identity without the feature.
let t = crate::table::block::BlockTransform::from_parts(
compression,
encryption,
#[cfg(zstd_any)]
None,
)?;
if let Some(ecc) = ecc {
t.with_ecc(ecc)
} else {
t
}
},
)?;
if block.header.block_type != BlockType::Index {
return Err(crate::Error::InvalidTag((
"BlockType",
block.header.block_type.into(),
)));
}
Ok(IndexBlock::new(block))
}
/// Tries to recover a table from a file.
///
/// A corrupt delete-bitmap fails recovery rather than silently resurrecting
/// deleted rows; see [`Self::recover_inner`] for the salvage variant that
/// degrades to "all rows live" instead.
pub fn recover(params: RecoverParams) -> crate::Result<Self> {
Self::recover_inner(params, RecoveryMode::Live)
}
/// Recovers a table, optionally in **salvage mode** (see [`RecoveryMode`]).
///
/// In salvage mode a corrupt or truncated delete-bitmap degrades to empty
/// ("all rows live, pending recompaction") and a delete-bitmap with an
/// unreadable zone map is ignored rather than erroring, so a columnar
/// segment with a damaged sidecar still opens and its data blocks can be
/// recovered. Normal recovery ([`RecoveryMode::Live`]) fails closed on
/// both, to avoid resurrecting deleted rows. Used by [`crate::salvage`].
#[expect(clippy::too_many_lines, reason = "recovery is inherently complex")]
pub(crate) fn recover_inner(params: RecoverParams, mode: RecoveryMode) -> crate::Result<Self> {
use core::sync::atomic::AtomicBool;
use meta::ParsedMeta;
use regions::ParsedRegions;
let RecoverParams {
file_path,
checksum,
global_seqno,
recency,
tree_id,
table_id,
cache,
descriptor_table,
fs,
pin_filter,
pin_index,
encryption,
#[cfg(zstd_any)]
zstd_dictionaries,
comparator,
prefix_extractor,
#[cfg(feature = "metrics")]
metrics,
} = params;
let salvage = matches!(mode, RecoveryMode::Salvage { .. });
log::debug!("Recovering table from file {}", file_path.display());
let mut file = fs.open(&file_path, &FsOpenOptions::new().read(true))?;
let file_path = Arc::new(file_path);
let file_len = file.metadata()?.len;
#[cfg(feature = "metrics")]
metrics
.table_file_opened_uncached
.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
let trailer = crate::sfa::Reader::from_reader(&mut file)?;
let regions = ParsedRegions::parse_from_toc(trailer.toc())?;
log::trace!("Reading meta block, with meta_ptr={:?}", regions.metadata);
// The expected-id cross-check rejects a swapped / wrong-id file in a
// LIVE tree, where the manifest / file name is the durable identity.
// A salvage open with a caller-known id (repair — from the file name)
// keeps the check so a forged TAIL id falls back to the MID mirror; a
// STANDALONE salvage reader has no out-of-band id, so the SOURCE's
// own stored id is the identity and the check is skipped (None). An
// ENCRYPTED open always passes the caller's id — the meta block's AAD
// binds it, so decryption itself requires the right id.
let expected_id = match mode {
RecoveryMode::Live => Some(table_id),
RecoveryMode::Salvage { expected_id, .. } => {
if encryption.is_some() {
Some(table_id)
} else {
expected_id
}
}
};
// Salvage arbitration may invert the mirror order (see
// `RecoveryMode::Salvage::prefer_mid_meta`); live opens always load
// tail-first.
let prefer_mid_meta = matches!(
mode,
RecoveryMode::Salvage {
prefer_mid_meta: true,
..
}
);
// TAIL first (authoritative copy by convention; physically
// identical content to MID — same `file_size`, same
// `created_at`, same KV map — the only difference is which
// SFA section is loaded). On any decode/decrypt/checksum
// failure fall back to the MID copy if present. Under salvage
// arbitration (`prefer_mid_meta`) the order is inverted: MID
// first, tail as the fallback.
let (first_handle, first_name, second, second_name) =
if prefer_mid_meta && let Some(mid_handle) = regions.metadata_mid {
(mid_handle, "MID", Some(regions.metadata), "TAIL")
} else {
(regions.metadata, "TAIL", regions.metadata_mid, "MID")
};
let metadata = match ParsedMeta::load_with_handle(
&*file,
&first_handle,
expected_id,
encryption.as_deref(),
) {
Ok(m) => m,
Err(first_err) => {
if let Some(second_handle) = second {
log::warn!(
"{first_name} meta block unreadable for {} ({first_err}); \
falling back to {second_name} copy",
file_path.display(),
);
// Match the PR contract: when BOTH copies fail,
// surface the FIRST error (callers care about the
// preferred copy's failure mode). The fallback
// failure goes to the log so it's not silently
// dropped from diagnostics.
// MID and TAIL are byte-identical: same `file_size`
// (= `*self.meta.file_pos`, only bumped inside
// `spill_block`, unchanged between the two writes),
// same `created_at` (snapshotted once in
// `finish()`), same KV map. Either payload is usable
// directly — no sentinel patching, no
// `std::fs::metadata` (which would also bypass the
// pluggable `Fs` backend).
match ParsedMeta::load_with_handle(
&*file,
&second_handle,
expected_id,
encryption.as_deref(),
) {
Ok(m) => m,
Err(second_err) => {
log::warn!(
"{second_name} meta block also unreadable for {}: {second_err}; \
returning original {first_name} error",
file_path.display(),
);
return Err(first_err);
}
}
} else {
return Err(first_err);
}
}
};
// Refuse a table whose BuRR sections are in the superseded wire format,
// here rather than on the first probe. The filter and the retrieval
// locator share `FORMAT_VERSION`, so both are covered by the one stamp
// and both are checked by the one condition — a table converted on the
// filter side alone would otherwise open and then fail inside a point
// read on its stale locator.
//
// Gated on a section actually being present: a table with no filter and
// no locator has nothing that could be misread, and refusing it would
// turn a harmless absence into an outage.
//
// NOT exempted in salvage mode, though salvage does re-derive every one
// of these sections from the recovered entries and so could in
// principle re-emit a legacy table in the current format. Exempting it
// here alone would not produce that migration: `verify_keep_decision`
// grades a table that RECOVERED by its block-verify verdict, and a
// healthy legacy table verdicts Clean and is KEPT verbatim — so the
// rebuilt manifest would reference unconverted tables, repair would
// report success, and the next ordinary open would refuse again. A
// repair that reports success and leaves an unopenable store is worse
// than one that refuses. Turning salvage into a real migration means a
// keep-decision reason that forces the rewrite, which is the offline
// converter's job and is tracked with it.
{
let has_burr_section = regions.filter.is_some()
|| regions.filter_tli.is_some()
|| regions.locator.is_some();
let expected = crate::table::filter::ribbon::burr::FORMAT_VERSION;
if has_burr_section && metadata.filter_format != Some(expected) {
return Err(crate::Error::UnsupportedFormat {
part: crate::FormatPart::Filter,
found: metadata.filter_format,
expected,
});
}
}
// The same refusal for a columnar table's row groups, for the same
// reasons and with the same salvage-mode reasoning as above. It
// matters more here, not less: a row group read under the wrong
// layout fails as a block-role mismatch, which the recovery paths
// grade as DAMAGE, so without this a repair would drop every columnar
// table in a store awaiting conversion. Gated on the table being
// columnar: a row-major table has no row groups to misread. A build
// without the `columnar` feature reads no columnar layout at all and
// reports that on the first read, which is a different remedy from
// conversion, so the refusal is not raised there.
#[cfg(feature = "columnar")]
if metadata.columnar
&& metadata.columnar_format != Some(crate::table::meta::COLUMNAR_FORMAT_VERSION)
{
return Err(crate::Error::UnsupportedFormat {
part: crate::FormatPart::Columnar,
found: metadata.columnar_format,
expected: crate::table::meta::COLUMNAR_FORMAT_VERSION,
});
}
// Resolve the dictionary this table was written against, by the id the
// table itself records. Fail-fast: a tree that does not hold that id
// cannot decompress a single data block, so say so here rather than on
// the first read. `got` reports what the tree DOES hold when it holds
// exactly one, which is the common misconfiguration (the wrong
// dictionary supplied) and the only case where naming one is useful.
#[cfg(zstd_any)]
let zstd_dictionary =
if let CompressionType::ZstdDict { dict_id, .. } = metadata.data_block_compression {
let Some(dict) = zstd_dictionaries.get(dict_id) else {
return Err(crate::Error::ZstdDictMismatch {
expected: dict_id,
got: (zstd_dictionaries.len() == 1)
.then(|| zstd_dictionaries.ids().next())
.flatten(),
});
};
Some(dict.clone())
} else {
// Not dictionary-compressed: carrying a dictionary would be a
// handle nothing reads.
None
};
let file_handle: Arc<dyn FsFile> = Arc::from(file);
let file_accessor = if let Some(dt) = descriptor_table {
FileAccessor::DescriptorTable {
table: dt,
fs: fs.clone(),
}
} else {
FileAccessor::File(file_handle.clone())
};
let block_index = if regions.index.is_some() {
log::trace!(
"Creating partitioned block index, with tli_ptr={:?}",
regions.tli,
);
let block = Self::read_tli(
®ions,
file_handle.as_ref(),
metadata.id,
metadata.index_block_compression,
encryption.as_deref(),
metadata.ecc_params,
)?;
BlockIndexImpl::TwoLevel(TwoLevelBlockIndex {
top_level_index: block,
cache: cache.clone(),
compression: metadata.index_block_compression,
path: Arc::clone(&file_path),
file_accessor: file_accessor.clone(),
table_id: (tree_id, metadata.id).into(),
encryption: encryption.clone(),
ecc: metadata.ecc_params,
comparator: comparator.clone(),
#[cfg(feature = "metrics")]
metrics: metrics.clone(),
})
} else if pin_index {
log::trace!(
"Creating pinned, full block index, with tli_ptr={:?}",
regions.tli,
);
let block = Self::read_tli(
®ions,
file_handle.as_ref(),
metadata.id,
metadata.index_block_compression,
encryption.as_deref(),
metadata.ecc_params,
)?;
BlockIndexImpl::Full(FullBlockIndex::new(block, comparator.clone())?)
} else {
log::trace!("Creating volatile, full block index");
BlockIndexImpl::VolatileFull(VolatileBlockIndex {
cache: cache.clone(),
compression: metadata.index_block_compression,
file_accessor: file_accessor.clone(),
handle: regions.tli,
path: Arc::clone(&file_path),
table_id: (tree_id, metadata.id).into(),
encryption: encryption.clone(),
ecc: metadata.ecc_params,
comparator: comparator.clone(),
#[cfg(feature = "metrics")]
metrics: metrics.clone(),
})
};
// Set when the salvage-mode open DEGRADES a rebuildable side section
// (filter / filter_tli, seqno bounds, zone map, locator) because its
// block did not decode as the claimed type. Salvage re-derives every
// such section from the recovered entries, so a section that is present
// but does not decode may be a `range_tombstones` / `delete_bitmap`
// relabeled to a rebuildable name and re-roled — which salvage would
// discard, resurrecting the suppressed rows. This is a purely
// STRUCTURAL signal (each decode reads its own section's bytes,
// independent of the data blocks), so a corrupt DATA block does not
// trip it. `block_layout` is excluded: it fails the open outright, so a
// relabel to it is rejected by the failed recovery rather than
// salvaged.
let mut rebuildable_section_degraded = false;
let pinned_filter_index = if let Some(filter_tli_handle) = regions.filter_tli {
let load = || -> crate::Result<IndexBlock> {
let block = Block::from_file(
file_handle.as_ref(),
filter_tli_handle,
crate::table::block::BlockIdentity {
table_id: metadata.id,
block_type: BlockType::Index,
dict_id: 0,
window_log: 0,
},
&{
// Filter TLI is an Index (SST) block: no block_flags byte,
// so ECC presence comes from the per-SST descriptor.
let t = crate::table::block::BlockTransform::from_parts(
metadata.index_block_compression,
encryption.as_deref(),
#[cfg(zstd_any)]
None,
)?;
if let Some(ecc) = metadata.ecc_params {
t.with_ecc(ecc)
} else {
t
}
},
)?;
if block.header.block_type != BlockType::Index {
return Err(crate::Error::InvalidTag((
"BlockType",
block.header.block_type.into(),
)));
}
let idx = IndexBlock::new(block);
// Validate filter index trailer eagerly (same as FullBlockIndex::new)
// so later iter() calls cannot panic on malformed blocks.
idx.try_iter(comparator.clone())?;
Ok(idx)
};
match load() {
Ok(idx) => Some(idx),
// Only an ENVIRONMENTAL read propagates so the caller can retry
// (or supply the right key): degradation is not free — it sets
// `rebuildable_section_degraded`, which fails salvage closed and
// costs the whole recoverable table. A failure on the DATA (bad
// sector, truncation) degrades under salvage like the
// seqno-bounds / zone-map / delete-bitmap / locator loaders.
Err(e) if e.is_environmental() => return Err(e),
// Salvage never consults the source's filter — the
// destination writer rebuilds it from the recovered keys —
// so a STRUCTURALLY or PERSISTENTLY unreadable filter index must not
// cost the recoverable data (a live open still fails closed).
Err(e) if salvage => {
log::warn!(
"filter index for table {:?} is unreadable ({e}); salvaging \
without it (the recovered copy re-derives its filter)",
metadata.id
);
rebuildable_section_degraded = true;
None
}
Err(e) => return Err(e),
}
} else {
None
};
// TODO: FilterBlock newtype
//
// In SALVAGE mode the source filter is never PINNED (the destination
// rebuilds it from the recovered keys), but a present full filter must
// still be PROBED even when `pin_filter` is false: a delete_bitmap
// renamed and re-roled to a `filter` (an empty sentinel, or a
// checksum/parity-valid but structurally broken BuRR payload) launders
// the deletion metadata. Loading and parsing it here trips the
// rebuildable-section degradation, which the salvage guard turns into a
// fail-closed refusal. A live open (pin_filter, not salvage) keeps
// its exact prior behaviour.
let pinned_filter_block = if pinned_filter_index.is_none() && (pin_filter || salvage) {
let loaded = regions
.filter
.map(|filter_handle| {
log::debug!(
"Loading and pinning filter block, with filter_ptr={filter_handle:?}"
);
let block = Block::from_file(
file_handle.as_ref(),
filter_handle,
crate::table::block::BlockIdentity {
table_id: metadata.id,
block_type: BlockType::Filter,
dict_id: 0,
window_log: 0,
},
// Filter blocks are never written compressed, so the
// transform is Plain or Encrypted depending on whether
// the table is keyed. Filter is an SST block (no
// block_flags byte), so ECC presence comes from the
// per-SST descriptor: upgrade to `*Ecc` when page_ecc.
&{
let t = match encryption.as_deref() {
Some(enc) => crate::table::block::BlockTransform::Encrypted(enc),
None => crate::table::block::BlockTransform::PLAIN,
};
if let Some(ecc) = metadata.ecc_params {
t.with_ecc(ecc)
} else {
t
}
},
)
.and_then(|block| {
if block.header.block_type == BlockType::Filter {
Ok(block)
} else {
Err(crate::Error::InvalidTag((
"BlockType",
block.header.block_type.into(),
)))
}
})?;
Ok::<_, crate::Error>(FilterBlock::new(block))
})
.transpose();
match loaded {
Ok(Some(filter)) => {
// A present full filter that decodes to the empty sentinel,
// or whose BuRR payload does not parse, is not something the
// writer ever emits (it omits the section when filtering is
// disabled). In salvage mode treat it as a degraded
// rebuildable section so a relabeled delete_bitmap cannot
// pass as a filter-less table and resurrect deleted rows.
if salvage && (filter.is_empty() || filter.maybe_contains_hash(0).is_err()) {
log::warn!(
"filter block for table {:?} is empty or unparsable; salvaging \
as a degraded rebuildable section",
metadata.id
);
rebuildable_section_degraded = true;
}
// Never PIN in salvage (the destination rebuilds the
// filter); a live open with pin_filter keeps it.
if pin_filter { Some(filter) } else { None }
}
Ok(None) => None,
// An InvalidTag here is STRUCTURAL, not corruption: the block
// loaded and verified its own PAYLOAD checksum, it is just the
// WRONG role. `Header::checksum` covers the payload, not the
// header, so a `block_type` byte that flips to another valid SST
// discriminant does NOT fail that checksum — it reaches this
// role check exactly like a TOC rename (a delete_bitmap renamed
// to `filter` without re-roling its header). Both are a valid
// block of the wrong name (the relabel signature), so degrade
// like the empty / unparsable payload above and fail closed.
//
// Any OTHER load failure (payload checksum / AEAD) is GENUINE
// bit-rot: a re-stamped relabel produces a checksum-VALID block,
// so a broken payload checksum is real corruption, rebuilt from
// the recovered keys. A delete-free table with a bit-rotted
// filter must auto-repair, not fail closed, so salvaging
// continues without degrading.
Err(e) if salvage => {
if matches!(e, crate::Error::InvalidTag(_)) {
log::warn!(
"filter block for table {:?} has the wrong role ({e}); salvaging \
as a degraded rebuildable section",
metadata.id
);
rebuildable_section_degraded = true;
} else {
log::warn!(
"filter block for table {:?} is unreadable ({e}); salvaging \
without it (the recovered copy re-derives its filter)",
metadata.id
);
}
None
}
Err(e) => return Err(e),
}
} else {
None
};
// Load range tombstones (if present)
let range_tombstones = if let Some(rt_handle) = regions.range_tombstones {
log::trace!("Loading range tombstone block, with rt_ptr={rt_handle:?}");
let block = Block::from_file(
file_handle.as_ref(),
rt_handle,
crate::table::block::BlockIdentity {
table_id: metadata.id,
block_type: BlockType::RangeTombstone,
dict_id: 0,
window_log: 0,
},
// Range-tombstone blocks are always uncompressed; the
// transform is Plain or Encrypted depending on whether the
// table is keyed. RangeTombstone is an SST block (no
// block_flags byte), so ECC presence comes from the per-SST
// descriptor: upgrade to `*Ecc` when page_ecc.
&{
let t = match encryption.as_deref() {
Some(enc) => crate::table::block::BlockTransform::Encrypted(enc),
None => crate::table::block::BlockTransform::PLAIN,
};
if let Some(ecc) = metadata.ecc_params {
t.with_ecc(ecc)
} else {
t
}
},
)?;
if block.header.block_type != BlockType::RangeTombstone {
return Err(crate::Error::InvalidTag((
"BlockType",
block.header.block_type.into(),
)));
}
let mut rts = Self::decode_range_tombstones(&block, comparator.as_ref())?;
// Sort range tombstones by (start asc, seqno desc) using the
// user comparator so the order matches the tree's key ordering.
// The seqno-desc tiebreaker ensures higher-seqno RTs are checked
// first when multiple share the same start key.
let cmp = &comparator;
rts.sort_unstable_by(|a, b| {
cmp.compare(&a.start, &b.start)
.then_with(|| b.seqno.cmp(&a.seqno))
});
rts
} else {
Vec::new()
};
let range_tombstones_end = range_tombstones
.iter()
.map(|rt| &rt.end)
.max_by(|a, b| comparator.compare(a, b))
.cloned();
// Load the optional inner-block layout section (present only when the
// table has data blocks that split into >= 2 inner zstd blocks). Mirrors
// the range-tombstone loader: same Plain/Encrypted (+ optional ECC)
// transform the writer used for this uncompressed meta section.
let block_layout = if let Some(bl_handle) = regions.block_layout {
let block = Block::from_file(
file_handle.as_ref(),
bl_handle,
crate::table::block::BlockIdentity {
table_id: metadata.id,
block_type: BlockType::BlockLayout,
dict_id: 0,
window_log: 0,
},
&{
let t = match encryption.as_deref() {
Some(enc) => crate::table::block::BlockTransform::Encrypted(enc),
None => crate::table::block::BlockTransform::PLAIN,
};
if let Some(ecc) = metadata.ecc_params {
t.with_ecc(ecc)
} else {
t
}
},
)?;
if block.header.block_type != BlockType::BlockLayout {
return Err(crate::Error::InvalidTag((
"BlockType",
block.header.block_type.into(),
)));
}
let map = crate::table::block_layout::BlockLayoutMap::decode(&block.data)?;
log::trace!(
"Loaded block-layout index with {} multi-inner-block entries",
map.len(),
);
map
} else {
crate::table::block_layout::BlockLayoutMap::default()
};
// Load the optional seqno-bounds section (parallel to the index; powers
// the scan_since_seqno block-skip). Absent unless seqno_in_index was on.
//
// Best-effort, like the zone map below: the seqno-bounds section is
// derived, non-authoritative metadata, so a corrupt / unreadable section
// disables the block-skip (falling back to a full per-entry filter)
// rather than failing the whole table open.
let seqno_bounds = if let Some(sb_handle) = regions.seqno_bounds {
let load = || -> crate::Result<crate::table::seqno_bounds::SeqnoBoundsMap> {
let block = Block::from_file(
file_handle.as_ref(),
sb_handle,
crate::table::block::BlockIdentity {
table_id: metadata.id,
block_type: BlockType::SeqnoBounds,
dict_id: 0,
window_log: 0,
},
&{
let t = match encryption.as_deref() {
Some(enc) => crate::table::block::BlockTransform::Encrypted(enc),
None => crate::table::block::BlockTransform::PLAIN,
};
if let Some(ecc) = metadata.ecc_params {
t.with_ecc(ecc)
} else {
t
}
},
)?;
if block.header.block_type != BlockType::SeqnoBounds {
return Err(crate::Error::InvalidTag((
"BlockType",
block.header.block_type.into(),
)));
}
crate::table::seqno_bounds::SeqnoBoundsMap::decode(&block.data)
};
match load() {
Ok(m) => m,
// Only an ENVIRONMENTAL read propagates so the caller can retry
// (or supply the right key); a failure on the DATA (bad sector,
// truncation) degrades this rebuildable, derived section to an
// empty map (seqno block-skip disabled) rather than failing the
// whole open — turning an optimization's bit-rot into a hard
// availability loss would be wrong.
Err(e) if e.is_environmental() => return Err(e),
Err(e) => {
log::warn!(
"seqno-bounds section for table {:?} is unreadable ({e}); disabling seqno block-skip",
metadata.id
);
rebuildable_section_degraded = true;
crate::table::seqno_bounds::SeqnoBoundsMap::default()
}
}
} else {
crate::table::seqno_bounds::SeqnoBoundsMap::default()
};
if !seqno_bounds.is_empty() {
log::trace!("Loaded {} seqno-bounds entries", seqno_bounds.len());
}
// Load the optional zone-map section (parallel to the index; powers the
// predicate-based block-skip). Absent unless the zone-map policy was on.
//
// Best-effort: the zone map is DERIVED, non-authoritative metadata. A
// corrupt or unreadable section disables block-skip for this table (an
// empty map) rather than failing the whole `Table::recover` — turning an
// optimization's bit-rot into a hard availability loss would be wrong.
let zone_map = if let Some(zm_handle) = regions.zone_map {
let load = || -> crate::Result<crate::table::zone_map::ZoneMap> {
let block = Block::from_file(
file_handle.as_ref(),
zm_handle,
crate::table::block::BlockIdentity {
table_id: metadata.id,
block_type: BlockType::ZoneMap,
dict_id: 0,
window_log: 0,
},
&{
let t = match encryption.as_deref() {
Some(enc) => crate::table::block::BlockTransform::Encrypted(enc),
None => crate::table::block::BlockTransform::PLAIN,
};
if let Some(ecc) = metadata.ecc_params {
t.with_ecc(ecc)
} else {
t
}
},
)?;
if block.header.block_type != BlockType::ZoneMap {
return Err(crate::Error::InvalidTag((
"BlockType",
block.header.block_type.into(),
)));
}
crate::table::zone_map::ZoneMap::decode(&block.data)
};
match load() {
Ok(m) => m,
// Only an ENVIRONMENTAL read propagates so the caller can retry
// (or supply the right key); a failure on the DATA degrades this
// rebuildable, derived section to an empty map (block-skip
// disabled) rather than failing the whole open.
Err(e) if e.is_environmental() => return Err(e),
Err(e) => {
log::warn!(
"zone-map section for table {:?} is unreadable ({e}); disabling block-skip",
metadata.id
);
rebuildable_section_degraded = true;
crate::table::zone_map::ZoneMap::default()
}
}
} else {
crate::table::zone_map::ZoneMap::default()
};
// Load the optional positional delete-bitmap section. Unlike the zone
// map (a skip optimization that degrades safely to empty), the delete
// bitmap is correctness data: silently dropping an unreadable one would
// resurrect deleted rows, so normal recovery propagates the error and
// fails. In salvage mode it degrades to empty ("all rows live, pending
// recompaction") so the segment's data is still recoverable.
let mut delete_bitmap_degraded = false;
let mut delete_bitmap = if let Some(db_handle) = regions.delete_bitmap {
let load = || -> crate::Result<crate::table::delete_bitmap::DeleteBitmap> {
let block = Block::from_file(
file_handle.as_ref(),
db_handle,
crate::table::block::BlockIdentity {
table_id: metadata.id,
block_type: BlockType::DeleteBitmap,
dict_id: 0,
window_log: 0,
},
&{
let t = match encryption.as_deref() {
Some(enc) => crate::table::block::BlockTransform::Encrypted(enc),
None => crate::table::block::BlockTransform::PLAIN,
};
if let Some(ecc) = metadata.ecc_params {
t.with_ecc(ecc)
} else {
t
}
},
)?;
if block.header.block_type != BlockType::DeleteBitmap {
return Err(crate::Error::InvalidTag((
"BlockType",
block.header.block_type.into(),
)));
}
crate::table::delete_bitmap::DeleteBitmap::decode(&block.data)
};
match load() {
Ok(db) => db,
// An ENVIRONMENTAL read propagates so the caller can retry (or
// supply the right key): degrading the delete mask to "all rows
// live" on a fixable fault would resurrect deleted rows in the
// rebuilt table, or — under the default policy — cost the whole
// table. A read failure on the DATA in salvage mode instead
// falls through to the degradation branch below, where the
// caller's `allow_delete_resurrection` opt-in is honored
// downstream (a non-salvage open still fails closed via the
// final arm).
Err(e) if e.is_environmental() => return Err(e),
Err(e) if salvage => {
log::warn!(
"delete-bitmap for table {:?} is unreadable ({e}); salvaging all rows as live",
metadata.id
);
delete_bitmap_degraded = true;
crate::table::delete_bitmap::DeleteBitmap::default()
}
Err(e) => return Err(e),
}
} else {
crate::table::delete_bitmap::DeleteBitmap::default()
};
// A delete-bitmap is positional; masking it on the read path needs each
// block's row count, which comes from the zone map. The writer co-writes
// both, so a bitmap without a zone map is a malformed SST (masking would
// resolve every block to position 0 and corrupt visibility).
if !delete_bitmap.is_empty() && zone_map.is_empty() {
if salvage {
// Without a readable zone map the bitmap cannot be positioned
// (every block would resolve to row 0), so ignore it: show all
// rows live rather than masking against the wrong positions.
log::warn!(
"salvage: delete-bitmap for table {:?} has no readable zone map; ignoring it (all rows live)",
metadata.id
);
delete_bitmap_degraded = true;
delete_bitmap = crate::table::delete_bitmap::DeleteBitmap::default();
} else {
return Err(crate::Error::InvalidHeader(
"delete-bitmap SST is missing its zone map",
));
}
}
// Cache each data block's first global row position (file offset -> start
// row) once on open, so positional delete masking is O(1) per block on
// every read instead of recomputing cumulative row counts. Empty when the
// segment has no deletes.
let delete_block_starts = if delete_bitmap.is_empty() {
None
} else {
let mut map = crate::HashMap::default();
let mut start: u32 = 0;
// Part of opening the table, not a read a caller made, so it stays
// out of the read counters like the locator walk below.
let walk = block_index.iter().with_charge(ReadCharge::Maintenance);
for keyed in walk {
let keyed = keyed?;
map.insert(keyed.offset().0, start);
let row_count = zone_map
.columns_for(keyed.offset().0)
.and_then(|stats| stats.first())
.map_or(0, |col| col.row_count);
start = start.wrapping_add(row_count);
}
Some(alloc::sync::Arc::new(map))
};
let delete_bitmap = alloc::sync::Arc::new(delete_bitmap);
// Load the optional retrieval-ribbon locator section and pair it with an
// ordinal → data-block-handle map (the index yields handles in key/write
// order, which is the writer's block_id ordering). Only when the section
// exists, so non-locator tables pay nothing.
// Load the optional retrieval-ribbon locator as a BEST-EFFORT point-read
// accelerator: any failure (corrupt locator section, unexpected block
// type, or a corrupt sub-index block hit while walking the index to pair
// locators with their data-block handles) degrades to `None` rather than
// failing the table open. Point reads then use the sorted-index path,
// which isolates a corrupt sub-index partition to its own keys — so
// enabling the locator by default does NOT widen the blast radius of a
// partitioned-index corruption from "one partition" back to "whole SST".
let locator_block = match regions.locator {
None => None,
Some(loc_handle) => match Block::from_file(
file_handle.as_ref(),
loc_handle,
crate::table::block::BlockIdentity {
table_id: metadata.id,
block_type: BlockType::Locator,
dict_id: 0,
window_log: 0,
},
&{
let t = match encryption.as_deref() {
Some(enc) => crate::table::block::BlockTransform::Encrypted(enc),
None => crate::table::block::BlockTransform::PLAIN,
};
if let Some(ecc) = metadata.ecc_params {
t.with_ecc(ecc)
} else {
t
}
},
) {
Ok(block) => Some(block),
// An ENVIRONMENTAL locator read during salvage must PROPAGATE,
// not degrade: `rebuildable_section_degraded` makes
// `salvage_attempt` read a delete-free table as possibly hiding
// deletion metadata and fail the whole SST
// (`FeatureUnsupported`), dropping an otherwise-salvageable
// table instead of surfacing a fault the caller can fix by
// retrying or supplying the right key. Mirrors the zone-map /
// seqno-bounds / delete-bitmap loaders. A non-salvage open keeps
// the best-effort accelerator behavior (degrade to the
// sorted-index path), so a fault there never fails the open.
Err(e) if salvage && e.is_environmental() => return Err(e),
Err(e) => {
log::warn!("retrieval-ribbon locator disabled: section load failed: {e:?}");
rebuildable_section_degraded = true;
None
}
},
};
let locator_index = locator_block.and_then(|block| {
if block.header.block_type != BlockType::Locator {
log::warn!(
"retrieval-ribbon locator disabled: unexpected block type {:?}",
block.header.block_type
);
rebuildable_section_degraded = true;
return None;
}
let walk = block_index.iter().with_charge(ReadCharge::Maintenance);
let blocks: Vec<BlockHandle> = walk
.map(|r| r.map(|kbh| *kbh.as_ref()))
.collect::<crate::Result<Vec<_>>>()
.inspect_err(|e| {
log::warn!("retrieval-ribbon locator disabled: index walk failed: {e:?}");
})
.ok()?;
log::trace!(
"Loaded retrieval-ribbon locator over {} blocks",
blocks.len()
);
Some(crate::table::locator::LoadedLocator::new(
block.data, blocks,
))
});
log::debug!(
"Recovered table #{} from {}",
metadata.id,
file_path.display(),
);
let l0_recency = recency.unwrap_or(metadata.recency);
// Decided once, here: the tree's extractor is fixed for the life of
// an open tree, and a filter indexes only the prefixes of the one it
// was built under.
let prefix_filter = prefix_extractor.filter(|current| {
metadata.prefix_extractor.as_deref() == Some(current.name().as_bytes())
});
Ok(Self(
Arc::new(Inner {
path: file_path,
tree_id,
metadata,
file_len,
l0_recency,
regions,
cache,
file_accessor,
fs,
block_index: Arc::new(block_index),
pinned_filter_index,
pinned_filter_block,
prefix_filter,
is_deleted: AtomicBool::default(),
entry_durable: AtomicBool::default(),
punch_on_drop: AtomicU64::new(u64::MAX),
punched_through: once_cell::race::OnceBox::new(),
checksum,
global_seqno,
comparator,
#[cfg(feature = "metrics")]
metrics,
blob_links: once_cell::race::OnceBox::new(),
read_count: AtomicU64::new(0),
scan_count: AtomicU64::new(0),
counting_since_secs: crate::time::unix_timestamp().as_secs(),
last_access_secs: AtomicU64::new(0),
probe_stats: once_cell::race::OnceBox::new(),
range_tombstones,
range_tombstones_end,
block_layout,
seqno_bounds,
zone_map,
delete_bitmap,
delete_block_starts,
delete_bitmap_degraded,
rebuildable_section_degraded,
locator_index,
encryption,
#[cfg(zstd_any)]
zstd_dictionary,
deletion_pause: once_cell::race::OnceBox::new(),
#[cfg(feature = "std")]
background_deleter: once_cell::race::OnceBox::new(),
heal_hints: once_cell::race::OnceBox::new(),
read_budget: once_cell::race::OnceBox::new(),
#[cfg(all(feature = "std", feature = "page_ecc"))]
heal_lock: once_cell::race::OnceBox::new(),
}),
None,
None,
))
}
/// The tight-space restriction lower bound for this version's view of the
/// table, or `None` on the common path. `Some(bound)` means the data below
/// `bound` has been punched out and superseded by a merged output table, so
/// reads route keys `< bound` elsewhere and clamp this table's scans to
/// start at `bound` (its index still references the punched prefix).
#[must_use]
pub(crate) fn restrict_lower_bound(&self) -> Option<&UserKey> {
self.1.as_ref()
}
/// True when `key` is below this version's tight-space restriction bound, so
/// a read must miss here and fall through to the output table that
/// superseded the punched-out prefix. Every point-read entry point
/// ([`get`](Self::get), [`get_value`](Self::get_value),
/// [`get_with_block`](Self::get_with_block)) and every batch read
/// ([`batch_get`](Self::batch_get), the block planners and the staged
/// read) consults this first.
#[inline]
pub(crate) fn is_below_restriction(&self, key: &[u8]) -> bool {
self.1
.as_ref()
.is_some_and(|bound| self.comparator.compare(key, bound) == core::cmp::Ordering::Less)
}
/// Whether this version sees the table only from a tight-space
/// restriction's bound up.
pub(crate) fn is_restricted(&self) -> bool {
self.1.is_some()
}
/// Returns a view of this table restricted to keys `>= lower`, for
/// tight-space compaction. Shares the same `Arc<Inner>` (no file re-open,
/// no extra handle, no [`Drop`] interaction), so the original and the
/// restricted view are one physical SST seen by different versions. The
/// caller punches the data blocks below `lower` only after this view is
/// durably installed.
#[must_use]
pub(crate) fn with_restriction(&self, lower: UserKey) -> Self {
Self(self.0.clone(), Some(lower), self.2)
}
/// Re-opens this table as a DISTINCT [`Inner`](inner::Inner) (its own file
/// handle and fresh drop / punch-on-drop atomics) restricted to keys
/// `>= lower`. Used by tight-space compaction so the PRIOR unrestricted view
/// can drop — and punch its consumed prefix on that drop — independently of
/// this restricted view, which keeps serving the suffix. Heavier than
/// [`with_restriction`](Self::with_restriction) (re-reads the footer + block
/// index), which is acceptable on the opt-in, emergency tight-space path.
///
/// Opens with its own file handle (no shared descriptor table) so the old
/// view's handle lifecycle stays fully separate.
///
/// The suffix digest captured here must stay consistent with what the caller
/// installs in the manifest, so the caller MUST hold this table's
/// [`heal_lock_arc`](Self::heal_lock_arc) across BOTH this call and the
/// version edit that installs the returned view. Without it a concurrent
/// patrol heal could refresh a suffix block between the capture and the
/// install, binding the restricted manifest to a pre-heal digest that the
/// post-restriction patrol can neither match nor re-attribute (its
/// attestation binds whole-file, not suffix, digests).
///
/// # Errors
///
/// Propagates any error from re-opening the SST file.
// std-only: computes the suffix digest via `crate::repair` (file I/O) and is
// reached only from tight-space compaction, which is itself std-gated.
#[cfg(feature = "std")]
pub(crate) fn reopen_restricted(&self, lower: UserKey) -> crate::Result<Self> {
self.reopen_restricted_with(lower, None)
}
/// [`Self::reopen_restricted`], with the view's file kept in the given
/// descriptor cache under the given tree id instead of a handle of its
/// own: for a view no other view of this table shares that id with, such
/// as the tables a repair holds.
///
/// # Errors
///
/// Propagates any error from re-opening the SST file.
#[cfg(feature = "std")]
pub(crate) fn reopen_restricted_with(
&self,
lower: UserKey,
descriptors: Option<(Arc<crate::DescriptorTable>, crate::tree::inner::TreeId)>,
) -> crate::Result<Self> {
// The restricted view's digest is the LIVE SUFFIX only: its
// `[0, punch_offset)` prefix is hole-punched right after this view is
// installed, so a whole-file digest (what `self.checksum()` holds) would
// never match the punched file. Compute the suffix digest over the
// CURRENT bytes NOW, while the file is still whole — the suffix is
// untouched by the punch, and reading it fresh also folds in any in-place
// heal that refreshed this table (so a tight-space swap installs the
// healed suffix digest, never a stale pre-heal one).
let punch_offset = self.punch_offset_for(&lower)?;
let restricted_checksum = crate::Checksum::from_raw(
crate::repair::compute_table_checksum_from(&*self.fs, &self.path, punch_offset)?,
);
let reopened = {
let mut params = RecoverParams::new(
(*self.path).clone(),
restricted_checksum,
self.metadata.id,
self.fs.clone(),
self.comparator.clone(),
self.cache.clone(),
);
params.global_seqno = self.global_seqno;
params.recency = Some(self.l0_recency);
params.tree_id = self.tree_id;
if let Some((cache, tree_id)) = descriptors {
params.descriptor_table = Some(cache);
params.tree_id = tree_id;
}
params.pin_filter = self.pinned_filter_size() > 0;
params.pin_index = self.pinned_block_index_size() > 0;
params.encryption.clone_from(&self.encryption);
// The same file under the same meta: its filter indexes the
// prefixes it did, of the extractor the table was opened under.
params.prefix_extractor.clone_from(&self.prefix_filter);
#[cfg(zstd_any)]
{
// Reopening THIS table, so the only dictionary the recovery can
// need is the one it already resolved: the reopened view has
// the same id in its metadata.
params.zstd_dictionaries = self
.zstd_dictionary
.clone()
.map_or_else(crate::compression::ZstdDictionaries::new, |dict| {
crate::compression::ZstdDictionaries::new().with(dict)
});
}
#[cfg(feature = "metrics")]
{
params.metrics = self.metrics.clone();
}
Self::recover(params)?
};
// The reopened `Inner` is DISTINCT from this one, so it starts without
// the tree-installed shared gates. Carry them forward, or the restricted
// view would lose them: without the checkpoint deletion pause a
// checkpoint could link healed bytes under a stale digest, and without
// the shared heal lock two patrols could heal + reconcile the same SST
// concurrently and leave a clean file mismatched with the manifest.
if let Some(pause) = self.0.deletion_pause.get() {
reopened.install_deletion_pause(Arc::clone(pause));
}
// How far the file is punched is the file's, not a view's.
let punched = self
.0
.punched_through
.get_or_init(|| Box::new(Arc::new(AtomicU64::new(0))));
reopened
.0
.punched_through
.get_or_init(|| Box::new(Arc::clone(punched)));
#[cfg(all(feature = "std", feature = "page_ecc"))]
reopened.install_heal_lock(self.heal_lock_arc());
// The heal-hint sink too: a correctable read from the restricted view
// must still queue this table for a healing recompaction, or
// persistent bitrot keeps being corrected in memory on every read but
// is never scheduled for a durable rewrite.
if let Some(hints) = self.0.heal_hints.get() {
reopened.install_heal_hints(Arc::clone(hints));
}
// And the columnar read budget, or the suffix's scans fall back to the
// default budget whatever the tree was configured with.
if let Some(budget) = self.0.read_budget.get() {
reopened.0.read_budget.get_or_init(|| Box::new(*budget));
}
// The suffix keeps the share of the probe counts its data holds; the
// slice outputs take the prefix's share from this view.
if self.probe_stats().is_some() {
let suffix = crate::table::probe_stats::share_of(
self,
(
core::ops::Bound::Included(lower.as_ref()),
core::ops::Bound::Unbounded,
),
)?;
reopened.0.probe_stats.get_or_init(Box::default).add(suffix);
}
Ok(reopened.with_restriction(lower))
}
/// Marks this view to punch `[0, offset)` when its last `Arc` drops (see
/// [`Inner::punch_on_drop`](inner::Inner::punch_on_drop)). Set on the PRIOR
/// unrestricted view once a tight-space slice has been installed, so the
/// consumed prefix is reclaimed exactly when no reader can still see it.
#[cfg_attr(
not(feature = "std"),
allow(
dead_code,
reason = "tight-space punch hook; its compaction consumer is std-gated, so unused under no_std"
)
)]
pub(crate) fn mark_punch_on_drop(&self, offset: u64) {
self.0
.punch_on_drop
.store(offset, core::sync::atomic::Ordering::Release);
}
/// A handle that tells whether this view's file is still held: its
/// punch or deletion runs only once no handle is left.
#[cfg(feature = "std")]
pub(crate) fn downgrade(&self) -> alloc::sync::Weak<inner::Inner> {
Arc::downgrade(&self.0)
}
/// Byte offset of the first data block whose last key reaches `key`. Punching
/// `[0, offset)` reclaims every data block strictly below `key` while leaving
/// the straddling block and the index / footer (which follow all data blocks)
/// intact. When `key` is past the last block's keys, returns the end of the
/// data region (every data block is punchable).
///
/// # Errors
///
/// Propagates a block-index read error.
#[cfg_attr(
not(feature = "std"),
allow(
dead_code,
reason = "tight-space punch offset; its compaction consumer is std-gated, so unused under no_std"
)
)]
pub(crate) fn punch_offset_for(&self, key: &[u8]) -> crate::Result<u64> {
self.punch_offset_charged(key, ReadCharge::Maintenance)
}
/// [`Self::punch_offset_for`], every index block the walk reads from the
/// file paced by `pace` just before the read is made.
#[cfg_attr(
not(feature = "std"),
allow(dead_code, reason = "its verify consumer is std-gated")
)]
pub(crate) fn punch_offset_paced(
&self,
key: &[u8],
charge: ReadCharge,
pace: crate::table::util::Pacer,
) -> crate::Result<u64> {
self.punch_offset_walk(key, self.index_walk_charged(charge).with_pace(pace))
}
/// [`Self::punch_offset_for`], charging its index walk as `charge` says.
fn punch_offset_charged(&self, key: &[u8], charge: ReadCharge) -> crate::Result<u64> {
self.punch_offset_walk(key, self.index_walk_charged(charge))
}
/// The punch offset for `key` over the index handles `walk` yields.
fn punch_offset_walk(
&self,
key: &[u8],
walk: block_index::BlockIndexIterImpl,
) -> crate::Result<u64> {
let mut data_end = 0u64;
for handle in walk {
let handle = handle?;
if self.comparator.compare(handle.end_key(), key) != core::cmp::Ordering::Less {
return Ok(handle.offset().0);
}
data_end = handle.offset().0 + u64::from(handle.size());
}
Ok(data_end)
}
/// Byte offset of this view's first LIVE data block: `0` for a normal table,
/// or the punch offset for a tight-space RESTRICTED view (its `[0, offset)`
/// data blocks are hole-punched and read as zeros). A data block or section
/// entry at a lower offset is DEAD (superseded, never read), so the
/// disk-fresh verification gates skip it.
///
/// # Errors
///
/// Propagates a restricted view's punch-offset lookup failure instead of
/// grading it `0`: falling back to `0` would make the verification gates walk
/// the hole-punched prefix (which reads as zeros), report its blocks as
/// structural corruption, and — on the heal reconcile path — strip a valid
/// heal attestation for what is really a transient partitioned-index read.
/// Propagating keeps a transient failure inconclusive (the marker survives for
/// the next patrol). A normal (unrestricted) table never calls the fallible
/// lookup, so it always returns `Ok(0)`.
pub(crate) fn punch_offset(&self) -> crate::Result<u64> {
match self.restrict_lower_bound() {
Some(bound) => self.punch_offset_for(bound),
None => Ok(0),
}
}
/// The whole-file digest for a normal table, or the LIVE-SUFFIX digest for a
/// tight-space RESTRICTED view: its `[0, punch_offset)` prefix is hole-
/// punched once a superseding output table owns those keys, so hashing the
/// whole physical file would fold the punched (zeroed) prefix into the
/// digest and never match the manifest. Digesting only `[punch_offset, end)`
/// keeps the checksum stable across the punch, and it is what
/// [`reopen_restricted`](Self::reopen_restricted) records and what
/// verification / heal reconciliation must recompute for a restricted view.
pub(crate) fn live_region_checksum(&self) -> crate::Result<Checksum> {
self.suffix_checksum_for(self.restrict_lower_bound())
}
/// The entries this view actually serves.
///
/// `metadata.item_count` describes the WHOLE original SST, prefix included,
/// so a restricted view reports entries a superseding output now owns.
/// While a tight-space slice is in flight the version holds both, and
/// summing the raw metadata counts those entries twice.
///
/// Exact when the table carries a zone map (per-block row counts); without
/// one the blocks above the straddling one are apportioned by data bytes,
/// which is the same granularity every other estimate over this table uses.
///
/// `charge` says whose read this is. A query planner's estimate is part of
/// the query and is counted; a monitoring report is not, so polling it
/// leaves a workload's read counters unchanged.
///
/// # Errors
///
/// Propagates the index walk and the read of the straddling block.
pub(crate) fn live_item_count(&self, charge: ReadCharge) -> crate::Result<u64> {
let Some(bound) = self.restrict_lower_bound() else {
return Ok(self.metadata.item_count);
};
// One walk serves all of it: the blocks below the straddling one are
// skipped, the straddling one is counted, the rest are summed.
let mut walk = self.index_walk_charged(charge);
let mut any_block = false;
let straddle = loop {
let Some(handle) = walk.next() else {
break None;
};
let handle = handle?;
any_block = true;
if self.comparator.compare(handle.end_key(), bound) != core::cmp::Ordering::Less {
break Some(handle);
}
};
let Some(straddle) = straddle else {
// Past every block the whole data region is superseded; a table
// with no data blocks keeps its recorded count.
return Ok(if any_block {
0
} else {
self.metadata.item_count
});
};
let punch = straddle.offset().0;
if punch == 0 {
return Ok(self.metadata.item_count);
}
let straddle_live = self.straddling_block_live(&straddle, bound, charge)?;
if !self.zone_map.is_empty() {
// The straddling block is counted exactly above: its recorded row
// count covers the dead rows below the bound too.
let mut rows = straddle_live;
for handle in walk {
let handle = handle?;
if let Some(col) = self
.zone_map
.columns_for(*handle.offset())
.and_then(<[_]>::first)
{
rows += u64::from(col.row_count);
}
}
return Ok(rows);
}
let straddle_end = punch + u64::from(straddle.size());
let last = match walk.next_back() {
Some(last) => last?,
None => straddle,
};
let data_end = *last.offset() + u64::from(last.size());
// A straddling block reaching past the data section cannot be reasoned
// about; keep the recorded count rather than inventing a smaller one.
let Some(above) = data_end.checked_sub(straddle_end).filter(|_| data_end > 0) else {
return Ok(self.metadata.item_count);
};
let apportioned = u64::try_from(
u128::from(self.metadata.item_count) * u128::from(above) / u128::from(data_end),
)
.unwrap_or(self.metadata.item_count);
Ok(straddle_live + apportioned)
}
/// This view's block-index walk, charged to the read counters only when it
/// serves a caller's read.
fn index_walk_charged(&self, charge: ReadCharge) -> block_index::BlockIndexIterImpl {
self.block_index.iter().with_charge(charge)
}
/// The live rows of the block STRADDLING the restriction: the first whose
/// last key reaches `bound`, of which the view serves only the entries
/// `>= bound`.
///
/// Every block below it is dead in full and every block above is live in
/// full, so it is the only one whose rows have to be counted rather than
/// read off the index — and a bound landing on a block's last key (what a
/// tight-space slice commonly produces) makes almost all of its rows dead.
///
/// # Errors
///
/// Propagates the read of the block.
fn straddling_block_live(
&self,
straddle: &KeyedBlockHandle,
bound: &[u8],
charge: ReadCharge,
) -> crate::Result<u64> {
// A wholly delete-masked columnar block serves no keys at all.
let Some(block) = self.load_data_block_charged(straddle.as_ref(), charge)? else {
return Ok(0);
};
let data = &block.inner.data;
let cmp = &*self.comparator;
let mut live = 0u64;
for item in block.iter(self.comparator.clone()) {
if item.compare_key(bound, data, cmp) != core::cmp::Ordering::Less {
live += 1;
}
}
Ok(live)
}
/// The same digest for a restriction this VIEW does not carry yet: the
/// recovery scan digests a candidate before the manifest's restrictions are
/// attached (that happens when the version is built), so a restricted table
/// would otherwise be hashed whole and never match its committed
/// live-suffix digest.
///
/// `None` digests the whole file, which is what an unrestricted table's
/// manifest entry records.
///
/// # Errors
///
/// Propagates the index lookup of `bound` and the read of the file.
pub(crate) fn suffix_checksum_for(&self, bound: Option<&UserKey>) -> crate::Result<Checksum> {
let start = match bound {
Some(bound) => self.punch_offset_for(bound)?,
None => 0,
};
crate::file::checksum_from_with_overrides(&*self.fs, &self.path, start, &[])
.map(Checksum::from_raw)
}
/// Installs the tree-wide deletion pause used by checkpoints.
///
/// Idempotent: a second call is a no-op. Called by the owning tree
/// after recovery and after compaction registers freshly-built tables.
pub(crate) fn install_deletion_pause(&self, pause: Arc<crate::deletion_pause::DeletionPause>) {
let _ = self.0.deletion_pause.set(Box::new(pause));
}
/// The shared heal-serialization lock for this table, lazily created on
/// first use. Held by the patrol scrub across the whole scan-to-reconcile
/// span so two overlapping heals cannot race the link-count probe or the
/// digest reconciliation. Shared by STABLE table identity:
/// [`reopen_restricted`](Self::reopen_restricted) propagates it into the
/// distinct `Inner` it creates.
#[cfg(all(feature = "std", feature = "page_ecc"))]
pub(crate) fn heal_lock_arc(&self) -> Arc<parking_lot::Mutex<()>> {
Arc::clone(
self.0
.heal_lock
.get_or_init(|| Box::new(Arc::new(parking_lot::Mutex::new(())))),
)
}
/// Installs a shared heal lock, so a re-opened view serializes heals against
/// the original. Idempotent: a second call is a no-op.
#[cfg(all(feature = "std", feature = "page_ecc"))]
pub(crate) fn install_heal_lock(&self, lock: Arc<parking_lot::Mutex<()>>) {
let _ = self.0.heal_lock.set(Box::new(lock));
}
/// Installs the tree-wide background file deleter.
///
/// Idempotent: a second call is a no-op. Called by the owning tree after
/// recovery and after compaction registers freshly-built tables, so an
/// obsolete SST's `unlink` runs off the foreground path while its blocks
/// are reclaimed synchronously at Drop.
#[cfg(feature = "std")]
pub(crate) fn install_background_deleter(&self, deleter: Arc<crate::BackgroundDeleter>) {
let _ = self.0.background_deleter.set(Box::new(deleter));
}
/// Installs the tree-wide ECC heal-hint sink.
///
/// Idempotent: a second call is a no-op. Called by the owning tree after
/// recovery and after compaction registers freshly-built tables, so a
/// confirmed-persistent ECC correction on a read can queue this SST for a
/// healing recompaction.
pub(crate) fn install_heal_hints(&self, hints: Arc<crate::heal_hints::HealHints>) {
let _ = self.0.heal_hints.set(Box::new(hints));
}
/// Binds this freshly created table to the tree's shared machinery.
///
/// **Every path that makes a new table reachable must call this**, and
/// nothing else should install the individual sinks by hand: a table is
/// only correct once it is bound, and hand-rolled installation at each
/// publication site is how sites come to differ. A table that skips it
/// looks perfectly healthy and fails silently and much later — a
/// confirmed-persistent ECC correction can never queue it for a healing
/// rewrite, so the bitrot stays on disk and every read pays the
/// correction again, and its in-place heal can race a checkpoint's
/// hard-link.
///
/// Idempotent per sink, so re-binding a table is harmless.
pub(crate) fn bind_to_tree(&self, sinks: &TableSinks<'_>) {
self.install_deletion_pause(Arc::clone(sinks.deletion_pause));
self.install_heal_hints(Arc::clone(sinks.heal_hints));
// A second bind keeps the first budget, like the other sinks.
let _ = self.0.read_budget.set(Box::new(sinks.read_budget));
#[cfg(feature = "std")]
self.install_background_deleter(Arc::clone(sinks.background_deleter));
// A second bind keeps the counts the first one started.
if sinks.track_filter_probes {
self.0.probe_stats.get_or_init(Box::default);
}
}
/// This table's filter probes, when its tree counts them.
pub(crate) fn probe_stats(&self) -> Option<&crate::table::probe_stats::ProbeStats> {
self.0.probe_stats.get()
}
/// Counts a probe the filter answered, and whether it answered absent.
/// Only the reads that produce the answer count: a prewarm that probes
/// ahead of the read it warms does not.
fn count_probe(&self, bloom: &BloomResult) {
if let Some(stats) = self.probe_stats() {
let counts = bloom.probe_counts();
if counts.probes != 0 {
stats.probe();
}
if counts.negatives != 0 {
stats.negative();
}
}
}
/// Counts the probes a read tallied while planning, once it answers from
/// that plan.
pub(crate) fn count_probes(&self, counts: crate::table::probe_stats::ProbeCounts) {
if let Some(stats) = self.probe_stats() {
stats.add(counts);
}
}
/// Counts what a batched read tallied while planning, once it answers
/// from that plan: the probes, and the filter queries and skips.
pub(crate) fn count_plan(&self, counts: crate::table::probe_stats::PlanCounts) {
self.count_probes(counts.probes);
if counts.point_reads > 0 {
self.record_accesses(u64::try_from(counts.point_reads).unwrap_or(u64::MAX));
}
#[cfg(feature = "metrics")]
{
use core::sync::atomic::Ordering::Relaxed;
self.metrics
.filter_queries
.fetch_add(counts.filter_queries, Relaxed);
self.metrics
.io_skipped_by_filter
.fetch_add(counts.filter_skips, Relaxed);
}
}
/// Counts a probe the filter let through whose read found no version of
/// the key.
///
/// That is a false positive only when the read could see every version the
/// table holds (`table_seqno` above its newest): at an older snapshot the
/// key may be held only in versions the snapshot cannot see, and the index
/// walk skips the blocks holding them, so telling the two apart would cost
/// a read the probe did not make. Those probes are left uncounted.
pub(crate) fn count_false_positive(&self, table_seqno: SeqNo) {
if let Some(stats) = self.probe_stats()
&& table_seqno > self.metadata.seqnos.1
{
stats.negative();
}
}
/// Whether a key this table's filter let through and no read found is
/// counted: the table has a filter, and the count has somewhere to go.
pub(crate) fn counts_filter_misses(&self) -> bool {
self.has_filter() && (cfg!(feature = "metrics") || self.probe_stats().is_some())
}
/// Counts a key the filter let through that the read of this table, at
/// `table_seqno`, found no version of: a filter query that reached the
/// table, and a false positive where [`Self::count_false_positive`] says.
pub(crate) fn count_filter_miss(&self, table_seqno: SeqNo) {
#[cfg(feature = "metrics")]
self.metrics
.filter_queries
.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
self.count_false_positive(table_seqno);
}
/// The bytes of this table's data section a key range covers, at data
/// block granularity and read from the block index alone (no data block
/// is read), or `None` when the table has no data block.
///
/// `edge` says which blocks a range boundary takes (see [`SpanEdge`]).
/// `table_seqno` is table-local.
pub(crate) fn data_span(
&self,
bounds: (core::ops::Bound<&[u8]>, core::ops::Bound<&[u8]>),
table_seqno: SeqNo,
edge: SpanEdge,
) -> crate::Result<Option<DataSpan>> {
use crate::table::block_index::BlockIndex;
use core::cmp::Ordering;
use core::ops::Bound;
// data_end = the data section's byte extent = last data block's end.
let Some(last) = self.block_index.iter().next_back() else {
return Ok(None);
};
let last = last?;
let data_end = *last.offset() + u64::from(last.size());
if data_end == 0 {
return Ok(None);
}
// The data block that would contain `key`, as (start, end) byte
// offsets, or `None` when `key` is past the last block.
let block_span = |key: &[u8]| -> crate::Result<Option<(u64, u64)>> {
let Some(mut iter) = self.block_index.forward_reader(key, table_seqno) else {
return Ok(None);
};
let Some(handle) = iter.next() else {
return Ok(None);
};
let h = handle?;
let start = *h.offset();
Ok(Some((start, (start + u64::from(h.size())).min(data_end))))
};
// Tight-space restriction: a restricted table view serves only keys
// at or above its lower bound, with the punched-out prefix served by
// the replacement table, so the data below it is not this view's.
let live_start = match self.restrict_lower_bound() {
Some(rb) => block_span(rb.as_ref())?.map_or(data_end, |(start, _)| start),
None => 0,
};
// Where the first block whose last key is at or past `key` starts, or
// strictly past it when `past`; the data end when there is none. The
// versions of one key can fill several blocks, all ending at it.
let first_block_from = |key: &[u8], past: bool| -> crate::Result<u64> {
let Some(iter) = self.block_index.forward_reader(key, table_seqno) else {
return Ok(data_end);
};
for handle in iter {
let h = handle?;
if !past || self.comparator.compare(h.end_key(), key) != Ordering::Equal {
return Ok(*h.offset());
}
}
Ok(data_end)
};
let off_lo = match (bounds.0, edge) {
(Bound::Unbounded, _) => 0,
(Bound::Included(k) | Bound::Excluded(k), SpanEdge::Whole) => {
block_span(k)?.map_or(data_end, |(start, _)| start)
}
(Bound::Included(k), SpanEdge::ByLastKey) => first_block_from(k, false)?,
(Bound::Excluded(k), SpanEdge::ByLastKey) => first_block_from(k, true)?,
}
.max(live_start);
let off_hi = match (bounds.1, edge) {
(Bound::Unbounded, _) => data_end,
(Bound::Included(k) | Bound::Excluded(k), SpanEdge::Whole) => {
block_span(k)?.map_or(data_end, |(_, end)| end)
}
(Bound::Included(k), SpanEdge::ByLastKey) => first_block_from(k, true)?,
(Bound::Excluded(k), SpanEdge::ByLastKey) => first_block_from(k, false)?,
};
Ok(Some(DataSpan {
// A range ending before it starts covers nothing.
covered: off_hi.saturating_sub(off_lo),
live_start,
data_end,
}))
}
/// How this table's columnar reads fetch their pages: the budget its tree
/// bound it with, or the default for a table no tree owns.
pub(crate) fn read_budget(&self) -> crate::config::ReadBudget {
self.0.read_budget.get().copied().unwrap_or_default()
}
/// The installed heal-hint sink, exposed so tests outside this module can
/// assert every publication path binds it. Gated with the tests that use
/// it: without Page ECC nothing produces a heal hint to begin with.
#[cfg(all(test, feature = "page_ecc"))]
pub(crate) fn heal_hints_for_test(&self) -> Option<Arc<crate::heal_hints::HealHints>> {
self.0.heal_hints.get().cloned()
}
#[must_use]
pub fn checksum(&self) -> Checksum {
// The refreshed digest (an in-place heal changed the bytes after
// recovery) supersedes the one captured at recovery.
self.2.unwrap_or(self.0.checksum)
}
/// A view of this table whose full-file checksum is `checksum`: an
/// in-place heal changed the file's bytes, and installing this view into
/// a new version makes the diff persist the refreshed digest to the
/// manifest (see [`crate::Version::with_refreshed_table_checksum`]).
#[must_use]
pub(crate) fn with_refreshed_checksum(&self, checksum: Checksum) -> Self {
Self(self.0.clone(), self.1.clone(), Some(checksum))
}
/// Read `len` bytes from the cursor position with checked arithmetic.
/// Uses `.get()` instead of direct indexing to satisfy `clippy::indexing_slicing`.
#[expect(
clippy::cast_possible_truncation,
reason = "block sizes are bounded well within usize on all supported platforms"
)]
fn read_checked_slice(
cursor: &mut crate::io::Cursor<&[u8]>,
field: &'static str,
len: usize,
) -> crate::Result<Vec<u8>> {
let offset = cursor.position();
let data = cursor.get_ref();
let pos = offset as usize;
let end_pos = pos
.checked_add(len)
.ok_or(crate::Error::RangeTombstoneDecode { field, offset })?;
let buf = data
.get(pos..end_pos)
.ok_or(crate::Error::RangeTombstoneDecode { field, offset })?
.to_vec();
cursor.set_position(end_pos as u64);
Ok(buf)
}
/// Decodes range tombstones from a raw block.
///
/// Wire format (repeated): `[start_len:u16_le][start][end_len:u16_le][end][seqno:u64_le]`
///
/// # Errors
///
/// Will return `Err` if the block data is malformed.
#[expect(
clippy::cast_possible_truncation,
reason = "block sizes are bounded well within usize on all supported platforms"
)]
fn decode_range_tombstones(
block: &Block,
comparator: &dyn crate::comparator::UserComparator,
) -> crate::Result<Vec<RangeTombstone>> {
use crate::io::{Cursor, LE, ReadBytesExt};
let mut tombstones = Vec::new();
let data = block.data.as_ref();
// A dedicated RT block with empty payload is corruption — the writer
// only creates an RT block handle when at least one tombstone exists.
if data.is_empty() {
log::error!("Range tombstone block: missing start_len");
return Err(crate::Error::RangeTombstoneDecode {
field: "start_len",
offset: 0,
});
}
let mut cursor = Cursor::new(data);
while (cursor.position() as usize) < data.len() {
let entry_offset = cursor.position();
let start_len_offset = entry_offset;
let start_len =
cursor
.read_u16::<LE>()
.map_err(|_| crate::Error::RangeTombstoneDecode {
field: "start_len",
offset: start_len_offset,
})? as usize;
// Validate length against remaining data before allocating
let remaining = data.len() - cursor.position() as usize;
if start_len > remaining {
log::error!(
"Range tombstone block: start_len {start_len} exceeds remaining {remaining}"
);
return Err(crate::Error::RangeTombstoneDecode {
field: "start_len",
offset: start_len_offset,
});
}
// Extract validated slice from cursor position.
// Using .get() instead of direct indexing to satisfy clippy::indexing_slicing.
let start_buf = Self::read_checked_slice(&mut cursor, "start", start_len)?;
let end_len_offset = cursor.position();
let end_len =
cursor
.read_u16::<LE>()
.map_err(|_| crate::Error::RangeTombstoneDecode {
field: "end_len",
offset: end_len_offset,
})? as usize;
let remaining = data.len() - cursor.position() as usize;
if end_len > remaining {
log::error!(
"Range tombstone block: end_len {end_len} exceeds remaining {remaining}"
);
return Err(crate::Error::RangeTombstoneDecode {
field: "end_len",
offset: end_len_offset,
});
}
let end_buf = Self::read_checked_slice(&mut cursor, "end", end_len)?;
let seqno_offset = cursor.position();
let seqno =
cursor
.read_u64::<LE>()
.map_err(|_| crate::Error::RangeTombstoneDecode {
field: "seqno",
offset: seqno_offset,
})?;
let start = UserKey::from(start_buf);
let end = UserKey::from(end_buf);
// Validate invariant: start < end using the tree's comparator
// (reject corrupted or misordered intervals)
if comparator.compare(&start, &end) != core::cmp::Ordering::Less {
log::error!("Range tombstone block: invalid interval (start >= end)");
return Err(crate::Error::RangeTombstoneDecode {
field: "interval",
offset: entry_offset,
});
}
tombstones.push(RangeTombstone::new(start, end, seqno));
}
Ok(tombstones)
}
/// Returns the range tombstones stored in this table.
#[must_use]
pub(crate) fn range_tombstones(&self) -> &[RangeTombstone] {
&self.0.range_tombstones
}
/// The greatest end of this table's range tombstones, in the tree's key
/// order: with the first one's start, the stretch they can cover, which
/// may reach past the table's key range. `None` without tombstones.
pub(crate) fn range_tombstones_end(&self) -> Option<&UserKey> {
self.0.range_tombstones_end.as_ref()
}
/// The range tombstones as visible through THIS view: a tight-space
/// restriction clamps them to the live suffix (`>= bound`). The punched
/// prefix's deletions belong to the slice output that superseded it, so
/// exposing them unclamped would emit a change-data-capture event twice
/// (once from the clipped copy in the slice output, once from here) and
/// cover keys this view no longer owns. Read-path masking keeps using
/// the raw list — an over-wide tombstone there only masks keys the
/// restricted view refuses to serve anyway.
/// Borrowed as stored; only a tombstone the restriction clamps is a new
/// one.
pub(crate) fn visible_range_tombstones(
&self,
) -> impl Iterator<Item = alloc::borrow::Cow<'_, RangeTombstone>> + '_ {
use alloc::borrow::Cow;
self.0.range_tombstones.iter().filter_map(move |rt| {
let Some(bound) = self.1.as_ref() else {
return Some(Cow::Borrowed(rt));
};
// Live keys are `>= bound`; a tombstone covers `[start, end)`.
if self.comparator.compare(&rt.end, bound) != core::cmp::Ordering::Greater {
// Wholly below the restriction: every covered key is punched.
return None;
}
if self.comparator.compare(&rt.start, bound) == core::cmp::Ordering::Less {
return Some(Cow::Owned(RangeTombstone::new(
bound.clone(),
rt.end.clone(),
rt.seqno,
)));
}
Some(Cow::Borrowed(rt))
})
}
pub(crate) fn mark_as_deleted(&self) {
self.0
.is_deleted
.store(true, core::sync::atomic::Ordering::Release);
}
/// Checks if a key range overlaps (partially or fully) with this table's key range.
pub(crate) fn check_key_range_overlap_cmp(
&self,
bounds: &(Bound<&[u8]>, Bound<&[u8]>),
cmp: &dyn crate::comparator::UserComparator,
) -> bool {
if !self
.metadata
.key_range
.overlaps_with_bounds_cmp(bounds, cmp)
{
return false;
}
// Tight-space restriction: the live range is `[bound, hi]`. If the
// query's upper bound is strictly below `bound`, the query targets only
// the punched-out prefix (now served by a superseding output table), so
// this table does not overlap.
if let Some(bound) = &self.1 {
match bounds.1 {
Bound::Included(end) => {
if cmp.compare(end, bound) == core::cmp::Ordering::Less {
return false;
}
}
Bound::Excluded(end) => {
// end <= bound: every key the query can reach is below the
// live range.
if cmp.compare(end, bound) != core::cmp::Ordering::Greater {
return false;
}
}
Bound::Unbounded => {}
}
}
true
}
/// Checks the full-table bloom filter for a hash value.
///
/// Returns `Ok(true)` if the hash may exist in the filter (or if no full
/// filter is available), `Ok(false)` if the hash is definitely absent.
///
/// Handles full (non-partitioned) filters directly. Partitioned / TLI
/// filters are keyed by user key, not raw hash, so this method returns
/// `Ok(true)` conservatively for those types.
fn bloom_may_contain_hash(&self, hash: u64) -> crate::Result<bool> {
// Full (non-partitioned) filter — single bloom covers the entire table
if let Some(block) = &self.pinned_filter_block {
return block.maybe_contains_hash(hash);
}
// Partitioned / TLI filters: partition index is keyed by user key, not
// raw hash — we would need to scan ALL partitions to check,
// which is O(partitions) I/O and defeats the purpose of bloom skip.
// Returning Ok(true) is correct (conservative: segment is NOT skipped).
if self.pinned_filter_index.is_some() || self.regions.filter_tli.is_some() {
return Ok(true);
}
// Unpinned full filter — load from disk.
// Safe: if we reach here, filter_tli is None (no partitioned filter),
// so regions.filter is a single full-table bloom, not a concatenation.
if let Some(filter_block_handle) = &self.regions.filter {
let block = self.load_block(
filter_block_handle,
BlockType::Filter,
CompressionType::None, // NOTE: Filter blocks are never compressed (crate invariant)
#[cfg(zstd_any)]
None,
&LookupFd::default(),
)?;
let block = FilterBlock::new(block);
return block.maybe_contains_hash(hash);
}
// No filter available — cannot rule out the hash
Ok(true)
}
/// Checks the bloom filter for a prefix hash.
///
/// Returns `Ok(true)` if the prefix may exist in this table (or if no
/// filter is available), `Ok(false)` if the prefix is definitely absent.
///
/// This is used by prefix scans to skip segments that contain no keys
/// with a matching prefix. The prefix must have been indexed at write
/// time via a [`PrefixExtractor`](crate::PrefixExtractor): a table whose
/// filter indexes another extractor's prefixes, or none, cannot rule the
/// prefix out and answers `Ok(true)`.
pub(crate) fn maybe_contains_prefix(&self, prefix_hash: u64) -> crate::Result<bool> {
if self.prefix_filter.is_none() {
return Ok(true);
}
self.bloom_may_contain_hash(prefix_hash)
}
/// Whether [`Self::maybe_contains_prefix`] asks the filter at all: the
/// filter indexes the prefixes of the extractor the table was opened
/// under.
pub(crate) fn has_prefix_filter(&self) -> bool {
self.prefix_filter.is_some()
}
/// Checks the bloom filter for a precomputed key hash.
///
/// Returns `Ok(true)` if the key may exist in this table (or if no
/// filter is available), `Ok(false)` if the key is definitely absent.
///
/// Used by the point-read merge pipeline to pre-filter disk tables
/// before building range iterators. For partitioned or TLI filter
/// configurations, the underlying check returns `Ok(true)` conservatively,
/// so pre-filtering is best-effort and configuration-dependent.
pub(crate) fn bloom_may_contain_key_hash(&self, key_hash: u64) -> crate::Result<bool> {
self.bloom_may_contain_hash(key_hash)
}
/// Checks the bloom filter for a key, with partition-aware seeking.
///
/// Unlike [`bloom_may_contain_key_hash`](Self::bloom_may_contain_key_hash)
/// which falls back to `Ok(true)` for partitioned filters, this method
/// uses the user key to seek the partition index and check only the
/// matching partition's bloom filter.
///
/// `key_hash` must be the xxh3 hash of `key` (pre-computed by the caller
/// to avoid redundant hashing — same pattern as [`Table::get`]).
pub(crate) fn bloom_may_contain_key(&self, key: &[u8], key_hash: u64) -> crate::Result<bool> {
self.key_filter_answer(key, key_hash)
.map(|answer| answer == KeyFilterAnswer::MayContain)
}
/// [`Self::bloom_may_contain_key`], telling a filter's absent answer
/// apart from a key the partition index alone rules out.
pub(crate) fn key_filter_answer(
&self,
key: &[u8],
key_hash: u64,
) -> crate::Result<KeyFilterAnswer> {
debug_assert_eq!(
crate::hash::hash64(key),
key_hash,
"bloom_may_contain_key: key_hash must be crate::hash::hash64(key)"
);
let by_hash = |may: bool| {
if may {
KeyFilterAnswer::MayContain
} else {
KeyFilterAnswer::Absent
}
};
// Full (non-partitioned) filter — delegate to hash-only path.
// A table has either pinned_filter_block (full) or pinned_filter_index
// (partitioned), never both — checked at construction time.
if self.pinned_filter_block.is_some() {
return self.bloom_may_contain_hash(key_hash).map(by_hash);
}
// Partitioned filter with pinned TLI — seek to the matching partition
if let Some(filter_idx) = &self.pinned_filter_index {
let mut iter = filter_idx.iter(self.comparator.clone());
iter.seek(key, crate::seqno::MAX_SEQNO);
if let Some(filter_block_handle) = iter.next() {
let filter_block_handle = filter_block_handle.materialize(filter_idx.as_slice());
let block = self.load_block(
&filter_block_handle.into_inner(),
BlockType::Filter,
CompressionType::None,
#[cfg(zstd_any)]
None,
&LookupFd::default(),
)?;
let block = FilterBlock::new(block);
return block.maybe_contains_hash(key_hash).map(by_hash);
}
// iter.next() == None means the key is beyond all partition
// boundaries (seek found no ceiling entry in the TLI, which is
// ordered by each partition's last user key). The key cannot
// exist in this table, as in `check_bloom`.
return Ok(KeyFilterAnswer::PastPartitions);
}
// Unpinned filter — fall through to hash-only path (handles both
// unpinned full filters and the no-filter case)
self.bloom_may_contain_hash(key_hash).map(by_hash)
}
/// Whether a key check answers from a filter rather than passing the key
/// for want of one: [`Self::bloom_may_contain_key`] when `by_key`, which
/// reaches a partitioned filter through its pinned index, otherwise
/// [`Self::bloom_may_contain_key_hash`], which reaches only a full one.
pub(crate) fn key_check_consults_filter(&self, by_key: bool) -> bool {
self.pinned_filter_block.is_some()
|| (by_key && self.pinned_filter_index.is_some())
|| (self.regions.filter.is_some()
&& self.regions.filter_tli.is_none()
&& self.pinned_filter_index.is_none())
}
/// Returns the highest effective sequence number in the table.
///
/// For tables produced by flush/compaction (`global_seqno == 0`), this
/// returns the highest item seqno directly.
///
/// For tables produced by bulk ingestion (`global_seqno > 0`), items
/// are written with local seqno 0 and the table carries a global offset.
/// The effective seqno of each item is `global_seqno + local_seqno`,
/// which mirrors the translation in [`Table::get`].
#[must_use]
pub fn get_highest_seqno(&self) -> SeqNo {
self.metadata.seqnos.1 + self.global_seqno()
}
/// The highest LOCAL (on-disk, pre-`global_seqno`) sequence number of any
/// entry, `0` for an empty table. Unlike [`get_highest_seqno`], it does NOT
/// add the offset. Manifest repair uses it as the LEGACY bulk-ingest
/// signature: a table of unknown provenance whose entries all sit at local
/// seqno 0 may itself be a legacy bulk-ingested table.
///
/// [`get_highest_seqno`]: Self::get_highest_seqno
#[must_use]
pub(crate) fn max_local_seqno(&self) -> SeqNo {
self.metadata.seqnos.1
}
/// The local seqno bounds the meta records: the lowest and highest over
/// every entry and range tombstone, and the highest over the entries
/// alone.
#[cfg(feature = "std")]
pub(crate) fn local_seqno_bounds(&self) -> ((SeqNo, SeqNo), SeqNo) {
(self.metadata.seqnos, self.metadata.highest_kv_seqno)
}
/// Returns the highest sequence number from KV entries only,
/// excluding range tombstone seqnos.
///
/// This enables more aggressive table-skip: a covering RT stored
/// in the same table can trigger skip because its seqno may exceed
/// the KV-only max even though it doesn't exceed the overall max.
///
/// For tables written before this field was introduced, falls back
/// to `get_highest_seqno()` (conservative but correct).
#[must_use]
pub fn get_highest_kv_seqno(&self) -> SeqNo {
self.metadata.highest_kv_seqno + self.global_seqno()
}
/// Returns the number of tombstone markers in the `Table`.
#[must_use]
#[doc(hidden)]
pub fn tombstone_count(&self) -> u64 {
self.metadata.tombstone_count
}
/// Returns the number of weak (single delete) tombstones in the `Table`.
#[must_use]
#[doc(hidden)]
pub fn weak_tombstone_count(&self) -> u64 {
self.metadata.weak_tombstone_count
}
/// Returns the number of value entries reclaimable once weak tombstones can be GC'd.
#[must_use]
#[doc(hidden)]
pub fn weak_tombstone_reclaimable(&self) -> u64 {
self.metadata.weak_tombstone_reclaimable
}
/// Returns the ratio of tombstone markers in the `Table`.
#[must_use]
#[doc(hidden)]
pub fn tombstone_ratio(&self) -> f32 {
todo!()
// self.metadata.tombstone_count as f32 / self.metadata.key_count as f32
}
}