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
//! Data access methods for SSTableReader
//!
//! This module contains all methods related to reading data from SSTables,
//! including point lookups, range scans, and sequential access.
use super::source::ScanCursor;
use super::SSTableReader;
use crate::parser::DataFormat;
use crate::types::{CellWriteMetadata, TableId, Value};
use crate::util::cassandra_murmur3::cassandra_murmur3_token;
use crate::{Error, Result, RowKey};
use log::{debug, warn};
use std::io::SeekFrom;
use tokio::io::AsyncSeekExt;
use tokio::sync::mpsc;
/// Counter of `scan_for_key` invocations, used by tests to prove the BTI
/// point-lookup path never falls through to a sequential scan (issue #831).
///
/// Incremented at the top of [`SSTableReader::scan_for_key`] and read via
/// [`SSTableReader::scan_for_key_call_count`]. The increment is a single
/// `Relaxed` atomic add on a cold path, so the runtime cost is negligible; it is
/// not gated behind `cfg(test)` because integration tests in the `tests/`
/// directory compile against the library crate without its `test` cfg.
pub(crate) static SCAN_FOR_KEY_CALLS: std::sync::atomic::AtomicU64 =
std::sync::atomic::AtomicU64::new(0);
/// Compare two table IDs, handling both qualified (keyspace.table) and unqualified (table) formats.
///
/// This function allows flexible matching:
/// - "keyspace.table" matches "keyspace.table" (exact match)
/// - "table" matches "keyspace.table" (unqualified matches qualified)
/// - "keyspace.table" matches "table" (qualified matches unqualified)
/// - "table" matches "table" (exact match)
///
/// This is necessary because:
/// - Dataset mode SSTables store qualified table_ids (e.g., "test_basic.simple_table")
/// - Queries can use either qualified ("test_basic.simple_table") or unqualified ("simple_table") names
/// - Production SSTables may use unqualified table_ids
fn table_ids_match(entry_table_id: &TableId, query_table_id: &TableId) -> bool {
let entry_name = entry_table_id.name();
let query_name = query_table_id.name();
// Fast path: exact match
if entry_name == query_name {
return true;
}
// Extract unqualified table names for comparison
let entry_unqualified = if let Some(dot_pos) = entry_name.rfind('.') {
&entry_name[dot_pos + 1..]
} else {
entry_name
};
let query_unqualified = if let Some(dot_pos) = query_name.rfind('.') {
&query_name[dot_pos + 1..]
} else {
query_name
};
// Match if unqualified names are the same
entry_unqualified == query_unqualified
}
/// Stricter table-id match used by the BTI point-lookup guard (issue #831 review).
///
/// [`table_ids_match`] matches on the unqualified table name, so it treats
/// `ks_a.users` and `ks_b.users` as equal — fine for index lookups that are
/// already scoped to one table, but too permissive as a defensive guard against
/// a fully-qualified wrong-keyspace query. When BOTH ids are qualified
/// (`keyspace.table`), this requires exact `keyspace.table` equality; it only
/// falls back to the permissive unqualified match when one side lacks a
/// keyspace (preserving qualified-vs-unqualified flexibility).
fn table_ids_match_strict(entry_table_id: &TableId, query_table_id: &TableId) -> bool {
let entry_qualified = entry_table_id.name().contains('.');
let query_qualified = query_table_id.name().contains('.');
if entry_qualified && query_qualified {
entry_table_id.name() == query_table_id.name()
} else {
table_ids_match(entry_table_id, query_table_id)
}
}
/// Per-iteration decision for the BTI chunk-targeted point-lookup loop.
#[derive(Debug, PartialEq, Eq)]
enum BtiLookupStep {
/// The full partition-key prefix is buffered and matches the queried key —
/// parse the partition.
Parse,
/// The header/key prefix straddles a chunk boundary and is not yet fully
/// buffered — read the next chunk before parsing (chunk-targeted path only).
PullNextChunk,
/// Treat the partition as absent: either the buffered key prefix does not
/// match, or a whole-section window is structurally too short to grow.
Absent,
}
/// Decide what the BTI point-lookup loop should do for the current window state.
///
/// Pure so the chunk-straddle control flow is unit-testable without a real
/// multi-chunk BTI fixture (DataOffset partitions are narrow and fit in one
/// chunk, so a boundary-straddling header cannot be produced by the available
/// fixtures). Crucially, when the key prefix is not yet buffered on the
/// chunk-targeted path this returns [`BtiLookupStep::PullNextChunk`] — it must
/// NOT lead to parsing a truncated header (issue #831 review).
fn bti_lookup_step(
key_prefix_available: bool,
key_matches: bool,
chunk_targeted: bool,
) -> BtiLookupStep {
if key_prefix_available {
if key_matches {
BtiLookupStep::Parse
} else {
BtiLookupStep::Absent
}
} else if chunk_targeted {
BtiLookupStep::PullNextChunk
} else {
BtiLookupStep::Absent
}
}
/// Sort a result slice in ascending Cassandra token order.
///
/// The authoritative ordering for SSTable partitions is ascending Murmur3 token, with
/// equal-token ties broken by raw key bytes (lexicographic). This matches the on-disk
/// physical order (spec §5, Appendix B §313) and the write engine's `PartitionPosition::cmp`.
///
/// Computes each key's token once to avoid O(n log n) recomputation inside the comparator.
fn sort_by_token_order(results: &mut Vec<(RowKey, Value)>) {
// Map to (token, RowKey, Value), sort, then reassemble.
let mut tagged: Vec<(i64, RowKey, Value)> = results
.drain(..)
.map(|(k, v)| {
let t = cassandra_murmur3_token(k.as_bytes());
(t, k, v)
})
.collect();
tagged.sort_by(|a, b| a.0.cmp(&b.0).then_with(|| a.1.cmp(&b.1)));
results.extend(tagged.into_iter().map(|(_, k, v)| (k, v)));
}
/// Sort `(RowKey, Value, CellMeta)` triples by Cassandra Murmur3 token order.
fn sort_by_token_order_with_meta(
results: &mut Vec<(
RowKey,
Value,
std::collections::HashMap<String, CellWriteMetadata>,
)>,
) {
let mut tagged: Vec<(
i64,
RowKey,
Value,
std::collections::HashMap<String, CellWriteMetadata>,
)> = results
.drain(..)
.map(|(k, v, m)| {
let t = cassandra_murmur3_token(k.as_bytes());
(t, k, v, m)
})
.collect();
tagged.sort_by(|a, b| a.0.cmp(&b.0).then_with(|| a.1.cmp(&b.1)));
results.extend(tagged.into_iter().map(|(_, k, v, m)| (k, v, m)));
}
impl SSTableReader {
/// Return `true` when Data.db uses the V5CompressedLegacy NB chunked format and
/// therefore requires all chunks to be stitched before parsing.
///
/// The correct predicate is:
/// data_format == V5CompressedLegacy AND is_nb_format()
///
/// Rationale:
/// - `V5CompressedLegacy` identifies the row serialization format (u16 length
/// prefixes, legacy encoding) used by all Cassandra 5 'nb' SSTables.
/// - `is_nb_format()` identifies the chunked-compression read path. It intentionally
/// EXCLUDES `V5_0Uncompressed`, which uses the same row format but stores data as
/// a single contiguous block (no chunk boundaries, no stitching needed).
/// - Using `is_compressed` (compression_reader.is_some()) would be wrong for NB
/// format because the per-chunk decompression is handled inside `stitch_and_parse_all_chunks`,
/// and `is_compressed` may differ from `is_nb_format` for edge-case versions.
fn requires_chunk_stitching(&self) -> bool {
let data_format = self.header.cassandra_version.data_format();
matches!(data_format, DataFormat::V5CompressedLegacy)
&& self.header.cassandra_version.is_nb_format()
}
/// Get a value by key from the SSTable
pub async fn get(&self, table_id: &TableId, key: &RowKey) -> Result<Option<Value>> {
// Issue #831 / #909: BTI ("da") readers resolve partitions via the
// Partitions.db trie (O(log n)), never via Index.db (absent for BTI) or
// the sequential scan. The trie is the AUTHORITATIVE presence oracle for a
// BTI SSTable — it answers present/absent definitively — so we branch here
// BEFORE the bloom-filter pre-check. Skipping the bloom filter for BTI is
// both correct (the trie is authoritative; bloom is only an optimization)
// and necessary: a writer-produced Filter.db whose hashing does not match
// the reader's would otherwise cause false negatives and drop live
// partitions (the writer→reader roundtrip #909 must read back). It also
// guarantees a BTI get() can never fall through to scan_for_key.
if self.bti_partitions_db.is_some() {
return self.bti_point_lookup(table_id, key).await;
}
// First check bloom filter if available
if let Some(bloom_filter) = &self.bloom_filter {
if !bloom_filter.might_contain(key.as_bytes()) {
return Ok(None);
}
}
// Use index for efficient lookup if available
if let Some(index) = &self.index {
if let Some(entry) = index.find_entry(table_id, key).await? {
// When Index.db reports size=0 (Cassandra 5.0), fall back to sequential scan
if entry.size == 0 {
log::debug!(
"Index reports size=0 for key {:?}, using sequential scan fallback",
key
);
return self.scan_for_key(table_id, key).await;
}
// Index offsets are relative to data section start - adjust for header
let file_offset = entry.offset + self.actual_header_size as u64;
return self.read_value_at_offset(file_offset, entry.size).await;
}
// Issue #517: The SSTableIndex is built from Index.db key *digests* (16-byte
// Murmur3 hashes), not raw partition key bytes. A raw-key lookup via
// find_entry() always misses. Fall back to scan_for_key() so that get()
// and scan() agree on which partitions exist.
log::debug!(
"Index lookup returned no entry for key {:?} (possible digest/raw-key mismatch), \
falling back to sequential scan",
key
);
return self.scan_for_key(table_id, key).await;
} else {
// No index at all — fall back to sequential scan
return self.scan_for_key(table_id, key).await;
}
}
/// Current value of the test-only `scan_for_key` invocation counter.
///
/// Issue #831: tests use this to assert that a BTI `get()` resolves entirely
/// through the Partitions.db trie and never falls through to the sequential
/// scan. See [`SCAN_FOR_KEY_CALLS`].
pub fn scan_for_key_call_count() -> u64 {
SCAN_FOR_KEY_CALLS.load(std::sync::atomic::Ordering::Relaxed)
}
/// BTI ("da") point lookup: resolve a partition key via the Partitions.db
/// trie, decode the partition at the resolved offset, and return its row
/// `Value` (issue #831).
///
/// Correctness invariants (see issue #831 / #755):
///
/// - **Offset domain**: the trie returns an *uncompressed* Data.db offset, so
/// we decode the partition out of the DECOMPRESSED data section, never via
/// `read_value_at_offset`/`get_cached_data` (which seek raw file bytes).
/// - **Own decompression**: `requires_chunk_stitching()` is `false` for BTI,
/// so this path decompresses the chunk-compressed Data.db itself via the
/// reader's CompressionInfo + compression_reader. Because the trie already
/// resolved the EXACT uncompressed offset of the target partition, this only
/// decompresses the chunk that contains that offset and continues forward
/// chunk-by-chunk ONLY until the target partition is fully parsed — it never
/// decompresses earlier chunks or the rest of the file (issue #831 perf
/// finding). The whole-section [`stitch_all_chunks`] fallback is used only
/// when chunk targeting is impossible (no/zero `chunk_length`).
/// - **Prefix-collision guard**: the trie may return a candidate for a
/// prefix-colliding key, so the decoded partition key is verified to equal
/// the queried key before any row is returned.
async fn bti_point_lookup(&self, table_id: &TableId, key: &RowKey) -> Result<Option<Value>> {
// 1. Resolve the uncompressed Data.db offset via the trie.
let offset = match self.lookup_partition_via_bti_trie(key.as_bytes())? {
Some(off) => off as usize,
None => return Ok(None), // not in this SSTable
};
// 2. Obtain a DECOMPRESSED window that contains the target partition.
//
// `window_base` is the uncompressed offset of the window's first byte
// and `window` holds the decompressed bytes from there onward. The
// target partition starts at `offset - window_base` inside `window`
// (INVARIANT 1: the trie offset indexes the uncompressed data section).
//
// For the chunk-targeted path the window starts at the chunk that
// contains `offset` (so `window_base = target_chunk * chunk_length`);
// for the whole-section fallback the window starts at offset 0
// (`window_base = 0`). Either way the parse below uses the same
// `within = offset - window_base` index.
let schema_opt = self.get_table_schema(None);
let parser = self.build_v5_parser();
let found = self
.bti_decompress_and_parse_target(offset, key, table_id, schema_opt.as_ref(), &parser)
.await?;
match found {
Some(value) => {
if !self.filter_tombstone(&value) {
return Ok(None);
}
Ok(Some(value))
}
None => Ok(None),
}
}
/// Compute the chunk that contains uncompressed `offset`, the uncompressed
/// offset of that chunk's start, and the within-chunk index — given the
/// CompressionInfo `chunk_length` (issue #831).
///
/// Returns `(target_chunk, window_base, within)` where
/// `window_base = target_chunk * chunk_length` and `within = offset - window_base`.
/// Pure arithmetic so it can be unit-tested independently of any I/O.
#[inline]
fn bti_chunk_target(offset: usize, chunk_length: usize) -> (usize, usize, usize) {
let target_chunk = offset / chunk_length;
let window_base = target_chunk * chunk_length;
let within = offset - window_base;
(target_chunk, window_base, within)
}
/// Decompress only the chunk(s) needed to fully parse the target partition at
/// uncompressed `offset`, then parse and return its row value (issue #831).
///
/// Chunk targeting (the fast path): when `CompressionInfo` with a non-zero
/// `chunk_length` is present, the chunk containing `offset` is
/// `target_chunk = offset / chunk_length`; we seek that chunk via its
/// `chunk_offsets` entry, set the cursor's chunk index to `target_chunk`, then
/// decompress forward chunk-by-chunk, appending each into `window`. After each
/// appended chunk we attempt to parse the FIRST partition at `window[within..]`
/// (`within = offset % chunk_length`). The stop condition (correctness-critical
/// — never return a truncated parse):
/// - parse returns `Ok` AND the emit closure fired (a COMPLETE partition was
/// decoded) -> stop and return what the closure captured;
/// - parse returns `Err` (buffer truncated mid-partition) OR the closure
/// never fired -> append the next chunk and retry;
/// - `read_next_block()` returns `None` (EOF) and still not parsed -> stop
/// (the caller treats `None` as "absent", matching prior behaviour).
///
/// Fallbacks (preserve prior behaviour exactly): when `compression_info` is
/// `None` (uncompressed BTI Data.db) or `chunk_length` is 0/absent, this
/// decompresses the WHOLE section via [`stitch_all_chunks`] (`window_base = 0`)
/// and runs the same single-partition parse.
///
/// Uses its own per-scan [`ScanCursor`] (private file position + chunk
/// index), so concurrent lookups run in parallel without serialization
/// (issue #815).
async fn bti_decompress_and_parse_target(
&self,
offset: usize,
key: &RowKey,
table_id: &TableId,
schema_opt: Option<&crate::schema::TableSchema>,
parser: &crate::storage::sstable::reader::parsing::V5CompressedLegacyParser,
) -> Result<Option<Value>> {
use crate::storage::sstable::compression::Compression;
// Issue #815: each lookup uses its own cursor so concurrent lookups on
// this reader never share a mutable file position / chunk index.
let cursor = self.new_scan_cursor().await?;
// Determine the chunk-targeting parameters. `chunk_length == 0` (or no
// CompressionInfo) means we cannot chunk-target -> whole-section fallback.
let chunk_length = self
.compression_info
.as_ref()
.map(|ci| ci.chunk_length as usize)
.filter(|&len| len > 0);
let (target_chunk, window_base, mut window) = match chunk_length {
Some(len) => {
let (target_chunk, window_base, _within) = Self::bti_chunk_target(offset, len);
// Seek to the START of target_chunk so read_next_block() reads it
// first, and set the shared chunk index accordingly. Chunk offsets
// are relative to file start for NB/BTI (header_offset = 0).
let chunk_start = self
.compression_info
.as_ref()
.and_then(|ci| ci.compressed_chunk_offset(target_chunk))
.ok_or_else(|| {
Error::corruption(format!(
"BTI point lookup: no compressed offset for target chunk {} \
(offset {}, chunk_length {})",
target_chunk, offset, len
))
})?;
{
let mut file_guard = cursor.file.lock().await;
file_guard.seek(SeekFrom::Start(chunk_start)).await?;
}
cursor
.chunk_index
.store(target_chunk, std::sync::atomic::Ordering::Relaxed);
(target_chunk, window_base, Vec::<u8>::new())
}
None => {
// Whole-section fallback (uncompressed BTI, or chunk_length absent/0).
let header_size = self.calculate_header_size();
{
let mut file_guard = cursor.file.lock().await;
file_guard.seek(SeekFrom::Start(header_size as u64)).await?;
}
let whole = self.stitch_all_chunks(&cursor).await?;
(0usize, 0usize, whole)
}
};
// `within` is the start of the target partition inside `window`.
if offset < window_base {
return Err(Error::corruption(format!(
"BTI point lookup: resolved offset {} precedes window base {} (chunk {})",
offset, window_base, target_chunk
)));
}
let within = offset - window_base;
// For the chunk-targeted path we still need to populate `window`. For the
// whole-section fallback `window` is already complete.
let chunk_targeted = chunk_length.is_some();
loop {
// If chunk-targeted, append the next chunk before each parse attempt
// (the whole-section fallback already has all bytes in `window`).
if chunk_targeted {
match self.read_next_block(&cursor).await? {
Some(compressed_chunk) => {
let decompressed_chunk = if let Some(compression_reader) =
&self.compression_reader
{
let compression = Compression::new(*compression_reader.algorithm())?;
compression.decompress(&compressed_chunk).map_err(|e| {
Error::corruption(format!(
"BTI point lookup: failed to decompress chunk: {}",
e
))
})?
} else {
// No compression reader despite CompressionInfo:
// treat raw chunk bytes as the decompressed data.
compressed_chunk
};
window.extend_from_slice(&decompressed_chunk);
}
None => {
// EOF: no more chunks. If we never parsed a complete
// partition, the partition is treated as absent (matching
// the prior whole-section behaviour for an unparseable tail).
return Ok(None);
}
}
}
// Need at least the partition header to attempt a match.
if within >= window.len() {
if chunk_targeted {
// Not enough bytes yet; pull the next chunk.
continue;
}
// Whole-section window can't grow: offset is past the data.
return Err(Error::corruption(format!(
"BTI trie resolved Data.db offset {} beyond decompressed data section ({} bytes)",
offset,
window.len()
)));
}
// INVARIANT 3 + chunk-straddle gate. The parse/pull/absent decision is
// factored into the pure `bti_lookup_step` so the chunk-straddle control
// flow is unit-testable without a multi-chunk fixture (issue #831 review):
// when the header/key prefix is not yet fully buffered we must NOT invoke
// the parser on a truncated header (it can skip bytes and emit a later
// false-positive entry), and must read the next chunk first.
let key_available =
Self::bti_partition_key_bytes_available(&window, within, key.as_bytes());
let key_matches =
key_available && self.bti_partition_key_matches(&window, within, key.as_bytes());
match bti_lookup_step(key_available, key_matches, chunk_targeted) {
BtiLookupStep::Parse => { /* full key prefix buffered and matches */ }
BtiLookupStep::PullNextChunk => continue,
BtiLookupStep::Absent => {
if key_available {
debug!(
"BTI trie candidate at offset {} did not match queried key \
(prefix collision); treating as absent",
offset
);
}
return Ok(None);
}
}
// Attempt to parse the FIRST partition at window[within..]. The parser
// detects the next partition boundary / 0x01 end-of-partition marker and
// stops; we break after the first emitted entry. A complete partition
// means: parse returned Ok AND the closure fired.
let mut found: Option<Value> = None;
let mut emitted = false;
let parse_result = parser.parse_block_emit(
&window[within..],
schema_opt,
self,
|(tid, entry_key, entry_value)| {
emitted = true;
// Verify BOTH the emitted table id matches the queried table
// (a wrong-table query never returns a row, issue #831 review)
// AND the parser-decoded partition key equals the queried key.
if table_ids_match_strict(&tid, table_id)
&& entry_key.as_bytes() == key.as_bytes()
{
found = Some(entry_value);
}
Ok(std::ops::ControlFlow::Break(()))
},
);
match parse_result {
Ok(()) if emitted => {
// A COMPLETE partition was decoded — accept it and stop.
return Ok(found);
}
_ => {
// Either Err (truncated mid-partition) or the closure never
// fired (no complete partition yet). For the chunk-targeted
// path, pull the next chunk and retry; never accept a partial.
if chunk_targeted {
continue;
}
// Whole-section fallback already has every byte: a failure here
// means the partition genuinely could not be parsed -> absent.
return Ok(None);
}
}
}
}
/// Returns true when the `[flags][key_len: u8][key bytes]` prefix at `within`
/// is fully present in `window` AND `key_len` equals `expected_key.len()`.
///
/// Used by the chunk-targeted BTI lookup to decide whether the INVARIANT-3
/// key match can be evaluated yet, or whether more chunk bytes must be pulled
/// first (issue #831).
fn bti_partition_key_bytes_available(
window: &[u8],
within: usize,
_expected_key: &[u8],
) -> bool {
// Need flags + key_len byte first.
if within + 2 > window.len() {
return false;
}
let key_len = window[within + 1] as usize;
// The declared key bytes must all be buffered. (Whether `key_len` equals
// the expected length is decided by the subsequent match check, which
// fails fast on a mismatch — here we only require the bytes be present.)
within + 2 + key_len <= window.len()
}
/// Verify the on-disk partition-key bytes at `offset` in the decompressed
/// data section equal `expected_key` (issue #831, INVARIANT 3).
///
/// Reads the `[flags][key_len: u8][key bytes]` prefix. Returns `false` (rather
/// than erroring) on any structural mismatch so the caller can treat the trie
/// candidate as absent.
fn bti_partition_key_matches(
&self,
decompressed: &[u8],
offset: usize,
expected_key: &[u8],
) -> bool {
// Need at least flags + key_len.
if offset + 2 > decompressed.len() {
return false;
}
let key_len = decompressed[offset + 1] as usize;
let key_start = offset + 2;
let key_end = key_start + key_len;
if key_end > decompressed.len() {
return false;
}
&decompressed[key_start..key_end] == expected_key
}
/// BTI ("da") full scan: decompress the whole Data.db section and parse
/// every partition in token order (issue #660).
///
/// BTI SSTables carry no Index.db/Summary.db, so a range/full scan cannot
/// use the index path. Instead we stitch the entire (chunk-compressed) data
/// section into one buffer and run [`parse_block_with_cell_metadata`], which
/// walks ALL partitions — the same per-partition decode the point-lookup
/// path uses, but without stopping at the first match.
///
/// Returns entries with per-cell write metadata so the WRITETIME/TTL scan
/// (`scan_with_cell_metadata`) and the plain `scan` (which drops the metadata)
/// can share a single implementation. Results are filtered by the optional
/// `[start_key, end_key]` range and tombstone-suppressed, then sorted into
/// Murmur3 token order and truncated to `limit` — identical post-processing
/// to the V5CompressedLegacy stitched path.
///
/// Uses its own per-scan [`ScanCursor`], so it runs in parallel with other
/// scans on this reader without serialization (issue #815).
///
/// [`parse_block_with_cell_metadata`]: crate::storage::sstable::reader::parsing::V5CompressedLegacyParser::parse_block_with_cell_metadata
async fn bti_scan_with_metadata(
&self,
start_key: Option<&RowKey>,
end_key: Option<&RowKey>,
limit: Option<usize>,
schema: Option<&crate::schema::TableSchema>,
) -> Result<
Vec<(
RowKey,
Value,
std::collections::HashMap<String, CellWriteMetadata>,
)>,
> {
let cursor = self.new_scan_cursor().await?;
// Decompress the entire data section. Precondition for stitch_all_chunks:
// cursor's file seeked to data-section start (fresh cursor is at chunk 0).
let header_size = self.calculate_header_size();
{
let mut file_guard = cursor.file.lock().await;
file_guard.seek(SeekFrom::Start(header_size as u64)).await?;
}
let whole = self.stitch_all_chunks(&cursor).await?;
// Resolve schema via the four-tier strategy (provided > header > registry).
// V5CompressedLegacy partition decode requires a schema (cells lack names).
let effective_schema = self.get_table_schema(schema);
let parser = self.build_v5_parser();
let parsed =
parser.parse_block_with_cell_metadata(&whole, effective_schema.as_ref(), self)?;
let mut results = Vec::new();
for (_entry_table_id, entry_key, entry_value, cell_meta) in parsed {
if let Some(start) = start_key {
if &entry_key < start {
continue;
}
}
if let Some(end) = end_key {
if &entry_key > end {
continue;
}
}
if !self.filter_tombstone(&entry_value) {
continue;
}
results.push((entry_key, entry_value, cell_meta));
}
sort_by_token_order_with_meta(&mut results);
if let Some(lim) = limit {
results.truncate(lim);
}
log::debug!(
"SSTableReader::bti_scan_with_metadata - Returning {} results",
results.len()
);
Ok(results)
}
/// Scan a range of keys
///
/// # Arguments
/// * `table_id` - The table to scan
/// * `start_key` - Optional start key for range scan
/// * `end_key` - Optional end key for range scan
/// * `limit` - Optional limit on number of results
/// * `schema` - Optional table schema for schema-aware parsing. When provided,
/// enables accurate type detection and avoids heuristic-based parsing.
/// Strongly recommended for Cassandra 5.0+ formats.
pub async fn scan(
&self,
table_id: &TableId,
start_key: Option<&RowKey>,
end_key: Option<&RowKey>,
limit: Option<usize>,
schema: Option<&crate::schema::TableSchema>,
) -> Result<Vec<(RowKey, Value)>> {
log::debug!("SSTableReader::scan - Starting scan");
log::debug!("SSTableReader::scan - File path: {:?}", self.file_path);
log::debug!("SSTableReader::scan - Table ID: {}", table_id);
log::debug!("SSTableReader::scan - Start key: {:?}", start_key);
log::debug!("SSTableReader::scan - End key: {:?}", end_key);
log::debug!("SSTableReader::scan - Limit: {:?}", limit);
log::debug!("SSTableReader::scan - Has schema: {}", schema.is_some());
log::debug!("SSTableReader::scan - Has index: {}", self.index.is_some());
log::debug!(
"SSTableReader::scan - Has bloom filter: {}",
self.bloom_filter.is_some()
);
// Issue #660: BTI ("da") readers have no Index.db/Summary.db. A full scan
// walks the whole (chunk-compressed) Data.db once and parses every
// partition — the same partition decode the point-lookup path proves
// correct, but emitting ALL partitions instead of stopping at the first.
if self.bti_partitions_db.is_some() {
let entries = self
.bti_scan_with_metadata(start_key, end_key, limit, schema)
.await?;
return Ok(entries.into_iter().map(|(k, v, _meta)| (k, v)).collect());
}
let mut results = Vec::new();
// Use index for efficient range scan if available
if let Some(index) = &self.index {
log::debug!("SSTableReader::scan - Using index-based scan");
let entries = index.get_range(table_id, start_key, end_key)?;
log::debug!(
"SSTableReader::scan - Index returned {} entries",
entries.len()
);
// Issue #256 FIX: Fall back to sequential scan when index returns no entries
//
// This handles BTI (Big Trie Index) format where parsing may be incomplete or
// where the index format is not yet fully supported. Without this check, tables
// using BTI format return 0 rows because:
// 1. The index exists (so we take the index-based path)
// 2. But get_range() returns 0 entries (BTI parsing incomplete)
// 3. The has_zero_size check never triggers (no entries to check)
// 4. The for loop iterates 0 times, returning empty results
//
// Sequential scan correctly parses Data.db directly, bypassing index issues.
if entries.is_empty() {
log::debug!(
"SSTableReader::scan - Index returned 0 entries (BTI format or incomplete parsing), falling back to sequential scan"
);
return self
.sequential_scan(table_id, start_key, end_key, limit, schema)
.await;
}
// Check if any entry has size=0 (Cassandra 5.0 format)
let has_zero_size = entries.iter().any(|e| e.size == 0);
if has_zero_size {
log::debug!("SSTableReader::scan - Index reports size=0 for some entries, using sequential scan fallback");
return self
.sequential_scan(table_id, start_key, end_key, limit, schema)
.await;
}
// Collect ALL index entries (limit applied after sort — BLOCKING-1).
for (i, entry) in entries.iter().enumerate() {
// Index offsets are relative to data section start - adjust for header
let file_offset = entry.offset + self.actual_header_size as u64;
log::debug!(
"SSTableReader::scan - Processing index entry {}: index_offset={}, file_offset={}, size={}",
i, entry.offset, file_offset, entry.size
);
if let Some(value) = self.read_value_at_offset(file_offset, entry.size).await? {
log::debug!(
"SSTableReader::scan - Successfully read value at offset {}",
entry.offset
);
results.push((entry.key.clone(), value));
} else {
log::debug!("SSTableReader::scan - Value at offset {} was filtered out (tombstone or expired)", entry.offset);
}
}
} else {
// Fallback to sequential scan. sequential_scan() already returns results in
// token order (NON-BLOCKING-1: avoid double-sort — return directly).
log::debug!("SSTableReader::scan - No index, falling back to sequential scan");
let seq_results = self
.sequential_scan(table_id, start_key, end_key, limit, schema)
.await?;
log::debug!(
"SSTableReader::scan - Sequential scan returned {} results",
seq_results.len()
);
log::debug!(
"SSTableReader::scan - Returning {} final results",
seq_results.len()
);
return Ok(seq_results);
}
// Index-based path: sort by Murmur3 token order (ascending token, then key bytes).
// This matches the on-disk physical order (spec §5, Appendix B §313) and the write
// engine's PartitionPosition::cmp. Compute each key's token once before sorting to
// avoid O(n log n) recomputation inside the comparator.
sort_by_token_order(&mut results);
// Limit applied AFTER sort so LIMIT N returns the N token-smallest partitions.
if let Some(lim) = limit {
results.truncate(lim);
}
log::debug!(
"SSTableReader::scan - Returning {} final results",
results.len()
);
Ok(results)
}
/// Get all entries in the SSTable.
///
/// # Tombstone contract (Issue #505)
///
/// This is a **user-facing** accessor: row tombstones are filtered out via
/// [`Self::filter_tombstone`] and never appear in the returned entries. The
/// underlying `parse_block` path emits `Value::Tombstone(RowTombstone)` for
/// deleted rows, but those are suppressed here so callers see exactly the live
/// rows (matching the previous `Value::Null` suppression behaviour).
///
/// The compaction k-way merger must instead use
/// [`Self::iterate_all_partitions_for_compaction`], which preserves
/// `Value::Tombstone` entries (with their authoritative deletion timestamps)
/// so that tombstone-shadowing semantics can be applied during the merge.
pub async fn get_all_entries(&self) -> Result<Vec<(TableId, RowKey, Value)>> {
// Issue #660: BTI ("da") tables have no Index.db; route through the
// whole-Data.db BTI scan, which resolves schema via get_table_schema
// (header/registry) and decodes every partition. It mints its own
// per-scan cursor, as does the non-BTI path below (issue #815).
if self.bti_partitions_db.is_some() {
let table_id = TableId::new(format!(
"{}.{}",
self.header.keyspace, self.header.table_name
));
let entries = self.bti_scan_with_metadata(None, None, None, None).await?;
return Ok(entries
.into_iter()
.map(|(k, v, _meta)| (table_id.clone(), k, v))
.collect());
}
// Issue #815: independent per-scan cursor — no cross-scan serialization.
let cursor = self.new_scan_cursor().await?;
let mut results = Vec::new();
// Reset to beginning of data section
let header_size = self.calculate_header_size();
{
let mut file_guard = cursor.file.lock().await;
file_guard.seek(SeekFrom::Start(header_size as u64)).await?;
}
if self.requires_chunk_stitching() {
// V5CompressedLegacy: Row payloads can span multiple compressed chunks
// We must decompress and stitch all chunks together before parsing
log::debug!(
"V5CompressedLegacy format detected, decompressing and stitching all chunks before parsing"
);
// Use shared stitching helper method
let entries = self.stitch_and_parse_all_chunks(&cursor, None).await?;
results.extend(entries);
} else {
// Other formats: Read and parse blocks individually
while let Some(block) = self.read_next_block(&cursor).await? {
let entries = self.parse_block_entries(&block, None)?;
results.extend(entries);
}
}
// Issue #505: suppress row tombstones from user-facing output. The compaction
// path (iterate_all_partitions_for_compaction) bypasses this filter.
results.retain(|(_tid, _key, value)| self.filter_tombstone(value));
Ok(results)
}
/// Stitch all compressed chunks and parse as a single buffer (V5CompressedLegacy)
///
/// This helper method extracts the stitching logic from get_all_entries so it can be
/// reused by sequential_scan and other methods that need to handle V5CompressedLegacy
/// format where partitions can span chunk boundaries.
async fn stitch_and_parse_all_chunks(
&self,
cursor: &ScanCursor,
schema: Option<&crate::schema::TableSchema>,
) -> Result<Vec<(TableId, RowKey, Value)>> {
let stitched_buffer = self.stitch_all_chunks(cursor).await?;
let parser = self.build_v5_parser();
// Get schema (use provided schema or reader's schema)
let reader_schema;
let table_schema = if let Some(s) = schema {
Some(s)
} else {
reader_schema = self.get_table_schema(None);
reader_schema.as_ref()
};
// Parse the stitched decompressed buffer
let entries = parser.parse_block(&stitched_buffer, table_schema, self)?;
log::debug!(
"stitch_and_parse_all_chunks: Parsed {} entries from stitched buffer",
entries.len()
);
Ok(entries)
}
/// Like [`stitch_and_parse_all_chunks`] but also returns per-cell write metadata.
///
/// Used when `ProjectionFlags::include_cell_metadata` is set (issue #693).
async fn stitch_and_parse_all_chunks_with_metadata(
&self,
cursor: &ScanCursor,
schema: Option<&crate::schema::TableSchema>,
) -> Result<
Vec<(
TableId,
RowKey,
Value,
std::collections::HashMap<String, CellWriteMetadata>,
)>,
> {
let stitched_buffer = self.stitch_all_chunks(cursor).await?;
let parser = self.build_v5_parser();
let reader_schema;
let table_schema = if let Some(s) = schema {
Some(s)
} else {
reader_schema = self.get_table_schema(None);
reader_schema.as_ref()
};
let entries =
parser.parse_block_with_cell_metadata(&stitched_buffer, table_schema, self)?;
log::debug!(
"stitch_and_parse_all_chunks_with_metadata: Parsed {} entries with metadata",
entries.len()
);
Ok(entries)
}
/// Read, decompress, and concatenate every compressed chunk of the data
/// section into a single buffer.
///
/// V5CompressedLegacy partitions can span chunk boundaries, so the whole
/// data section must be stitched before parsing. The returned buffer is
/// bounded by the *uncompressed data-section size* — it scales with on-disk
/// bytes, not row count (issue #790).
///
/// Precondition: the caller has seeked `cursor`'s file to the start of the
/// data section (the cursor's chunk index starts at 0 when freshly minted).
async fn stitch_all_chunks(&self, cursor: &ScanCursor) -> Result<Vec<u8>> {
use crate::storage::sstable::compression::Compression;
// Pre-allocate buffer for ~2.5MB (estimated max size for test data)
let mut stitched_buffer = Vec::with_capacity(2_500_000);
let mut chunk_count = 0;
while let Some(compressed_chunk) = self.read_next_block(cursor).await? {
let decompressed_chunk = if let Some(compression_reader) = &self.compression_reader {
let compression = Compression::new(*compression_reader.algorithm())?;
match compression.decompress(&compressed_chunk) {
Ok(decompressed) => decompressed,
Err(e) => {
return Err(Error::corruption(format!(
"stitch_all_chunks: Failed to decompress chunk {}: {}",
chunk_count, e
)));
}
}
} else {
// No compression (should not happen for V5CompressedLegacy)
log::warn!("stitch_all_chunks: No compression reader, using raw chunk data");
compressed_chunk
};
stitched_buffer.extend_from_slice(&decompressed_chunk);
chunk_count += 1;
}
log::debug!(
"stitch_all_chunks: Stitched {} chunks, total buffer: {} bytes",
chunk_count,
stitched_buffer.len()
);
Ok(stitched_buffer)
}
/// Build a [`V5CompressedLegacyParser`] configured from this reader's header,
/// statistics (EncodingStats), version gates, and UDT registry.
///
/// [`V5CompressedLegacyParser`]: crate::storage::sstable::reader::parsing::V5CompressedLegacyParser
fn build_v5_parser(
&self,
) -> crate::storage::sstable::reader::parsing::V5CompressedLegacyParser {
let keyspace = self.header.keyspace.clone();
let table_name = self.header.table_name.clone();
// Extract EncodingStats from statistics_reader (if available)
let (min_timestamp, min_local_deletion_time, min_ttl) =
if let Some(stats_reader) = &self.statistics_reader {
let ts_stats = &stats_reader.statistics().timestamp_stats;
(
ts_stats.min_timestamp,
ts_stats.min_deletion_time,
ts_stats.min_ttl,
)
} else {
(0, 0, None)
};
let parser = crate::storage::sstable::reader::parsing::V5CompressedLegacyParser::new(
keyspace,
table_name,
min_timestamp,
min_local_deletion_time,
min_ttl,
)
// VG1: thread VersionGates from SSTableReader down to row parser so
// that VG3 can flip gate-sensitive code paths without re-deriving gates.
.with_version_gates(self.version_gates.clone());
// Add UDT registry if available for UDT-aware collection parsing (Issue #238)
if let Some(ref registry) = self.udt_registry {
parser.with_udt_registry(registry.clone())
} else {
parser
}
}
/// Streaming scan (issue #790): yield `(RowKey, Value)` entries lazily
/// through a bounded channel instead of materializing the whole result in a
/// `Vec`. Live heap is bounded by `buffer_size` rows (plus the stitched
/// data-section buffer) rather than growing O(rows).
///
/// Entries are yielded in on-disk order — token order for a single SSTable —
/// matching the order of the materializing [`scan`](Self::scan) path. The
/// bounded channel applies backpressure: parsing pauses when the consumer
/// falls behind and stops entirely if the consumer is dropped.
pub fn scan_stream(
self: std::sync::Arc<Self>,
table_id: TableId,
start_key: Option<RowKey>,
end_key: Option<RowKey>,
schema: Option<crate::schema::TableSchema>,
buffer_size: usize,
) -> mpsc::Receiver<Result<(RowKey, Value)>> {
let (tx, rx) = mpsc::channel(buffer_size.max(1));
tokio::spawn(async move {
if let Err(e) = self
.run_scan_stream(table_id, start_key, end_key, schema, tx.clone())
.await
{
// Surface the error to the consumer as a terminal stream item.
let _ = tx.send(Err(e)).await;
}
});
rx
}
async fn run_scan_stream(
self: std::sync::Arc<Self>,
table_id: TableId,
start_key: Option<RowKey>,
end_key: Option<RowKey>,
schema: Option<crate::schema::TableSchema>,
tx: mpsc::Sender<Result<(RowKey, Value)>>,
) -> Result<()> {
// Issue #815: independent per-scan cursor — no cross-scan serialization.
let cursor = self.new_scan_cursor().await?;
// Position at the start of the data section (mirrors sequential_scan).
let header_size = self.calculate_header_size();
{
let mut file_guard = cursor.file.lock().await;
file_guard.seek(SeekFrom::Start(header_size as u64)).await?;
}
if self.requires_chunk_stitching() {
// Stitch the (bounded) data section, then parse on a blocking thread,
// emitting one entry at a time. `blocking_send` provides backpressure
// so parsed Values are never all live at once.
let stitched = self.stitch_all_chunks(&cursor).await?;
let reader = std::sync::Arc::clone(&self);
let parse = tokio::task::spawn_blocking(move || {
reader.parse_stitched_stream(&stitched, schema.as_ref(), start_key, end_key, &tx)
})
.await;
match parse {
Ok(result) => result,
Err(join_err) => Err(Error::corruption(format!(
"scan_stream: parse task failed: {join_err}"
))),
}
} else {
// Non-stitching formats already read block-by-block; emit per block so
// only one block's entries are live at a time.
while let Some(block) = self.read_next_block(&cursor).await? {
let entries = self.parse_block_entries_with_schema(&block, schema.as_ref())?;
for (entry_table_id, entry_key, entry_value) in entries {
if !table_ids_match(&entry_table_id, &table_id) {
continue;
}
if let Some(ref start) = start_key {
if &entry_key < start {
continue;
}
}
if let Some(ref end) = end_key {
if &entry_key > end {
continue;
}
}
if !self.filter_tombstone(&entry_value) {
continue;
}
if tx.send(Ok((entry_key, entry_value))).await.is_err() {
return Ok(()); // consumer dropped
}
}
}
Ok(())
}
}
/// Parse a stitched V5CompressedLegacy buffer, sending each filtered
/// `(RowKey, Value)` through `tx` with `blocking_send` for backpressure.
///
/// CPU-bound and synchronous: must be invoked via `spawn_blocking`, never on
/// an async worker thread (`blocking_send` would otherwise stall the runtime).
fn parse_stitched_stream(
&self,
stitched: &[u8],
schema: Option<&crate::schema::TableSchema>,
start_key: Option<RowKey>,
end_key: Option<RowKey>,
tx: &mpsc::Sender<Result<(RowKey, Value)>>,
) -> Result<()> {
let parser = self.build_v5_parser();
let reader_schema;
let table_schema = if let Some(s) = schema {
Some(s)
} else {
reader_schema = self.get_table_schema(None);
reader_schema.as_ref()
};
parser.parse_block_emit(stitched, table_schema, self, |(_table_id, key, value)| {
// Key-range filter (start/end inclusive), mirroring sequential_scan.
if let Some(ref start) = start_key {
if &key < start {
return Ok(std::ops::ControlFlow::Continue(()));
}
}
if let Some(ref end) = end_key {
if &key > end {
return Ok(std::ops::ControlFlow::Continue(()));
}
}
// Suppress row tombstones from user-facing scan output (Issue #505).
if !self.filter_tombstone(&value) {
return Ok(std::ops::ControlFlow::Continue(()));
}
match tx.blocking_send(Ok((key, value))) {
Ok(()) => Ok(std::ops::ControlFlow::Continue(())),
Err(_) => Ok(std::ops::ControlFlow::Break(())), // consumer dropped
}
})
}
/// Stitch all compressed chunks and parse with per-row timestamps (for compaction).
///
/// Identical to [`stitch_and_parse_all_chunks`] but delegates to
/// [`V5CompressedLegacyParser::parse_block_with_timestamps`] so that each
/// entry carries its actual row-level write timestamp rather than
/// `SystemTime::now()`. Row and cell tombstones are emitted as
/// `Value::Tombstone` with their authoritative deletion timestamps.
///
/// Used exclusively by the compaction k-way merger path (Issue #505).
async fn stitch_and_parse_all_chunks_for_compaction(
&self,
cursor: &ScanCursor,
schema: Option<&crate::schema::TableSchema>,
) -> Result<Vec<super::compaction_row::CompactionRow>> {
log::debug!("stitch_and_parse_all_chunks_for_compaction: stitching chunks");
let mut stitched_buffer = Vec::with_capacity(2_500_000);
let mut chunk_count = 0;
while let Some(compressed_chunk) = self.read_next_block(cursor).await? {
use crate::storage::sstable::compression::Compression;
let decompressed_chunk = if let Some(compression_reader) = &self.compression_reader {
let compression = Compression::new(*compression_reader.algorithm())?;
compression.decompress(&compressed_chunk).map_err(|e| {
Error::corruption(format!(
"stitch_and_parse_all_chunks_for_compaction: Failed to decompress chunk {}: {}",
chunk_count, e
))
})?
} else {
compressed_chunk
};
stitched_buffer.extend_from_slice(&decompressed_chunk);
chunk_count += 1;
}
log::debug!(
"stitch_and_parse_all_chunks_for_compaction: {} chunks, {} bytes total",
chunk_count,
stitched_buffer.len()
);
let keyspace = self.header.keyspace.clone();
let table_name = self.header.table_name.clone();
let (min_timestamp, min_local_deletion_time, min_ttl) =
if let Some(stats_reader) = &self.statistics_reader {
let ts_stats = &stats_reader.statistics().timestamp_stats;
(
ts_stats.min_timestamp,
ts_stats.min_deletion_time,
ts_stats.min_ttl,
)
} else {
(0, 0, None)
};
let parser = crate::storage::sstable::reader::parsing::V5CompressedLegacyParser::new(
keyspace,
table_name,
min_timestamp,
min_local_deletion_time,
min_ttl,
)
// VG1: thread VersionGates from SSTableReader down to row parser.
.with_version_gates(self.version_gates.clone());
let parser = if let Some(ref registry) = self.udt_registry {
parser.with_udt_registry(registry.clone())
} else {
parser
};
let reader_schema;
let table_schema = if let Some(s) = schema {
Some(s)
} else {
reader_schema = self.get_table_schema(None);
reader_schema.as_ref()
};
let entries = parser.parse_block_for_compaction(&stitched_buffer, table_schema, self)?;
log::debug!(
"stitch_and_parse_all_chunks_for_compaction: parsed {} entries",
entries.len()
);
Ok(entries)
}
/// Iterate all partitions with per-row timestamps, for use by the compaction merger.
///
/// Returns `(RowKey, Value, row_timestamp_micros)` for every row in the SSTable.
/// Unlike [`iterate_all_partitions`]:
///
/// - Row tombstones are returned as `Value::Tombstone(RowTombstone)` carrying
/// the actual deletion timestamp extracted from the on-disk row header.
/// - Cell tombstones within live rows are stored as `Value::Tombstone(CellTombstone)`
/// inside the `Value::Map`, also carrying the actual cell-level deletion timestamp.
/// - The third tuple element is the decoded row-level write timestamp, so the
/// merger can perform timestamp-accurate last-write-wins comparisons.
///
/// Normal user-facing reads use [`scan`] / [`get`] / [`iterate_all_partitions`],
/// which apply tombstone filtering. Do NOT use this method for user-visible queries.
///
/// (Issue #505)
pub async fn iterate_all_partitions_for_compaction(
&self,
schema: Option<&crate::schema::TableSchema>,
) -> Result<Vec<super::compaction_row::CompactionRow>> {
// Only the V5CompressedLegacy NB chunk-stitching path is supported here
// (that is the format the WriteEngine produces). For other formats, fall
// back to iterate_all_partitions and attach timestamp 0 as a conservative
// default (LWW ordering then relies solely on run_index).
if self.requires_chunk_stitching() {
// We need schema; retrieve it once.
// `schema` is Option<&TableSchema>; clone it into an owned value so we
// can pass it to the async helper without borrow-checker issues.
let owned_schema = schema.cloned().or_else(|| self.get_table_schema(None));
// Reset chunk reader to start of data section (own per-scan cursor).
let cursor = self.new_scan_cursor().await?;
let header_size = self.calculate_header_size();
{
let mut file_guard = cursor.file.lock().await;
use tokio::io::AsyncSeekExt;
file_guard
.seek(std::io::SeekFrom::Start(header_size as u64))
.await?;
}
let entries = self
.stitch_and_parse_all_chunks_for_compaction(&cursor, owned_schema.as_ref())
.await?;
return Ok(entries);
}
// Non-stitching fallback: use iterate_all_partitions and attach ts=0.
let entries = self.iterate_all_partitions().await?;
Ok(entries
.into_iter()
.map(|(key, value)| {
super::compaction_row::CompactionRow::from_legacy_value(key, value, 0)
})
.collect())
}
/// Streaming compaction read (issue #827): yield `(RowKey, Value, ts)`
/// entries via `emit` one partition at a time, so peak memory is bounded by
/// `max_partition_size + one_chunk` rather than by the total input size.
///
/// This is the incremental counterpart of
/// [`iterate_all_partitions_for_compaction`], which fully materialises the
/// decompressed data section and parses every entry into a `Vec` before
/// returning. The k-way merge producer (`merge::producer_thread`) uses this
/// to forward entries into its bounded channel directly, so a source's
/// decompressed content is never fully resident.
///
/// ## Sliding-window driver
///
/// The V5CompressedLegacy chunk-stitching path keeps a `window: Vec<u8>` of
/// decompressed bytes. After appending each decompressed chunk it drains
/// confirmed partitions via `parse_one_partition_with_timestamps`,
/// `drain(0..consumed)`-ing the front of the window after every `Emitted`,
/// and stopping at `NeedMore` to await the next chunk (a partition can
/// straddle a chunk boundary). At EOF a final drain pass runs with
/// `at_final_chunk = true` so the trailing (possibly truncated) partition is
/// terminal rather than requesting a refill that will never come.
///
/// Returning `ControlFlow::Break` from `emit` stops the scan early
/// (consumer dropped). Tombstone / timestamp semantics are byte-identical to
/// the Vec variant (Issue #505/#533).
pub async fn stream_all_partitions_for_compaction<F>(
&self,
schema: Option<&crate::schema::TableSchema>,
mut emit: F,
) -> Result<()>
where
F: FnMut(super::compaction_row::CompactionRow) -> Result<std::ops::ControlFlow<()>>,
{
// Reset chunk reader to the start of the data section (mirrors
// iterate_all_partitions_for_compaction) using an own per-scan cursor.
let cursor = self.new_scan_cursor().await?;
let header_size = self.calculate_header_size();
{
let mut file_guard = cursor.file.lock().await;
file_guard.seek(SeekFrom::Start(header_size as u64)).await?;
}
// Non-stitching formats are single-block / small: emit via the
// materialising iterator with ts=0 (matches the Vec-variant fallback).
if !self.requires_chunk_stitching() {
let entries = self.iterate_all_partitions().await?;
for (key, value) in entries {
let row = super::compaction_row::CompactionRow::from_legacy_value(key, value, 0);
match emit(row)? {
std::ops::ControlFlow::Continue(()) => {}
std::ops::ControlFlow::Break(()) => return Ok(()),
}
}
return Ok(());
}
// Resolve the schema the parser needs (cells lack column names on disk).
let owned_schema = schema.cloned().or_else(|| self.get_table_schema(None));
let parser = self.build_v5_parser();
let mut window: Vec<u8> = Vec::new();
let mut broke = false;
use crate::storage::sstable::compression::Compression;
let mut chunk_count = 0;
while let Some(compressed_chunk) = self.read_next_block(&cursor).await? {
let decompressed_chunk = if let Some(compression_reader) = &self.compression_reader {
let compression = Compression::new(*compression_reader.algorithm())?;
compression.decompress(&compressed_chunk).map_err(|e| {
Error::corruption(format!(
"stream_all_partitions_for_compaction: Failed to decompress chunk {}: {}",
chunk_count, e
))
})?
} else {
compressed_chunk
};
window.extend_from_slice(&decompressed_chunk);
chunk_count += 1;
// Not the final chunk yet: NeedMore means "await more bytes". Drain
// every confirmed partition from the front of the window.
self.drain_compaction_window(
&parser,
owned_schema.as_ref(),
&mut window,
false,
&mut emit,
&mut broke,
)?;
if broke {
return Ok(());
}
}
// EOF: final drain — a truncated/unterminated trailing partition is now
// terminal (Done), not a refill request.
if !broke {
self.drain_compaction_window(
&parser,
owned_schema.as_ref(),
&mut window,
true,
&mut emit,
&mut broke,
)?;
}
log::debug!(
"stream_all_partitions_for_compaction: drained {} chunks (final window {} bytes)",
chunk_count,
window.len()
);
Ok(())
}
/// Drain every confirmed partition from the front of the sliding `window`,
/// emitting each row via `emit` (issue #827). After each `Emitted` the
/// consumed prefix is removed so the window's peak size stays bounded by
/// `max_partition_size + one_chunk`. Stops at `NeedMore` / `Done` (await the
/// next chunk / genuine end) or when `emit` returns `Break` (sets `*broke`).
fn drain_compaction_window<F>(
&self,
parser: &crate::storage::sstable::reader::parsing::V5CompressedLegacyParser,
schema: Option<&crate::schema::TableSchema>,
window: &mut Vec<u8>,
at_final_chunk: bool,
emit: &mut F,
broke: &mut bool,
) -> Result<()>
where
F: FnMut(super::compaction_row::CompactionRow) -> Result<std::ops::ControlFlow<()>>,
{
use crate::storage::sstable::reader::parsing::ParseStep;
loop {
if *broke || window.is_empty() {
return Ok(());
}
let mut local_break = false;
let step = parser.parse_one_partition_for_compaction(
window.as_slice(),
schema,
self,
at_final_chunk,
&mut |row: super::compaction_row::CompactionRow| match emit(row)? {
std::ops::ControlFlow::Continue(()) => Ok(std::ops::ControlFlow::Continue(())),
std::ops::ControlFlow::Break(()) => {
local_break = true;
Ok(std::ops::ControlFlow::Break(()))
}
},
)?;
match step {
ParseStep::Emitted(consumed) => {
let take = if consumed == 0 { 1 } else { consumed };
window.drain(0..take.min(window.len()));
if local_break {
*broke = true;
return Ok(());
}
}
ParseStep::NeedMore | ParseStep::Done => return Ok(()),
}
}
}
/// Read value at a specific offset with caching
pub async fn read_value_at_offset(&self, offset: u64, size: u32) -> Result<Option<Value>> {
use crate::parser::header::CassandraVersion;
use crate::storage::sstable::compression::Compression;
// Size must be non-zero for offset-based reading
if size == 0 {
return Err(Error::corruption(format!(
"Cannot read value at offset {} with size=0. This should have been caught earlier and handled via sequential scan.",
offset
)));
}
// Use cached reading with metrics tracking
let buffer = self.get_cached_data(offset, size).await?;
// Decompress if needed
let data = if let Some(compression_reader) = &self.compression_reader {
let compression = Compression::new(*compression_reader.algorithm())?;
match compression.decompress(&buffer) {
Ok(decompressed) => {
debug!(
"Successfully decompressed {} bytes to {} bytes",
buffer.len(),
decompressed.len()
);
decompressed
}
Err(e) => {
// For modern formats (4.x/5.x), decompression failure is an error
if self.header.cassandra_version != CassandraVersion::Legacy {
return Err(Error::corruption(format!(
"Decompression failed for modern format at offset={}, size={}, algorithm={:?}: {}",
offset,
size,
compression_reader.algorithm(),
e
)));
} else {
// Only allow fallback for legacy formats
warn!(
"Decompression failed for legacy format ({}), using raw data",
e
);
debug!(
"First 32 bytes of raw data: {:02x?}",
&buffer[..std::cmp::min(32, buffer.len())]
);
buffer
}
}
}
} else {
buffer
};
// TODO: Parse value using schema-driven type information
// For now, preserve raw data until schema is available
let value = Value::Blob(data.to_vec());
// Extract write time from value (placeholder - would need to be parsed from SSTable)
let _write_time = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_micros() as i64)
.unwrap_or_else(|e| {
warn!("Failed to get system time: {}; using fallback value 0", e);
0
});
// Filter out tombstones and expired data
if !self.filter_tombstone(&value) {
return Ok(None);
}
Ok(Some(value))
}
/// Read block with caching support and hit/miss tracking
async fn get_cached_data(&self, block_offset: u64, size: u32) -> Result<Vec<u8>> {
use crate::parser::header::CassandraVersion;
use crate::storage::sstable::compression::Compression;
use tokio::io::AsyncReadExt;
// Calculate block identifier based on offset and size
let _block_id = block_offset;
// For now, always read from disk and track as cache miss
self.record_cache_miss();
// Read from disk
let mut file = self.file.lock().await;
file.seek(SeekFrom::Start(block_offset)).await?;
let mut buffer = vec![0u8; size as usize];
file.read_exact(&mut buffer).await?;
drop(file); // Release file lock early
// Decompress if needed
let data = if let Some(compression_reader) = &self.compression_reader {
let compression = Compression::new(*compression_reader.algorithm())?;
match compression.decompress(&buffer) {
Ok(decompressed) => decompressed,
Err(e) => {
// Handle decompression errors based on format
if self.header.cassandra_version != CassandraVersion::Legacy {
return Err(Error::corruption(format!(
"Decompression failed at offset={}, size={}: {}",
block_offset, size, e
)));
} else {
buffer // Fall back to raw data for legacy formats
}
}
}
} else {
buffer
};
Ok(data)
}
async fn scan_for_key(&self, table_id: &TableId, key: &RowKey) -> Result<Option<Value>> {
// Issue #831: record the call so tests can assert the BTI point-lookup
// path never reaches the sequential scan.
SCAN_FOR_KEY_CALLS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
// Issue #815: independent per-scan cursor — no cross-scan serialization.
let cursor = self.new_scan_cursor().await?;
let header_size = self.calculate_header_size();
// For V5CompressedLegacy NB format, partitions can span chunk boundaries.
// The block-by-block parser will miss any partition whose bytes cross a
// chunk boundary. Use the same stitched-buffer path that sequential_scan()
// uses so that get() and scan() share a consistent view of the data.
// (Issue #517)
if self.requires_chunk_stitching() {
log::debug!(
"scan_for_key: V5CompressedLegacy NB detected, using stitched buffer for key lookup"
);
// `stitch_all_chunks` reads from the CURRENT cursor position forward,
// so its precondition is "seeked to the data-section start" (the fresh
// cursor's chunk index already starts at 0). Each call uses its own
// cursor (issue #815), so there is no cross-call position to reset.
{
let mut file_guard = cursor.file.lock().await;
file_guard.seek(SeekFrom::Start(header_size as u64)).await?;
}
// Pass the reader's own schema so that V5CompressedLegacy rows can be fully
// parsed and their partition RowKeys emitted. Without a schema, parse_row_v5
// fails for all rows in a partition, causing no entries to be pushed and making
// the key comparison always miss even when the key exists.
let schema_opt = self.get_table_schema(None);
let all_entries = match self
.stitch_and_parse_all_chunks(&cursor, schema_opt.as_ref())
.await
{
Ok(entries) => entries,
Err(e) => {
// Schema may not be available for this reader (e.g., wrong table type).
// Return None so the caller can try the next reader.
log::debug!(
"scan_for_key: stitch_and_parse_all_chunks failed (schema missing?): {}",
e
);
return Ok(None);
}
};
// NOTE: The SSTableIndex is built from 16-byte Murmur3 *digests*, not raw keys,
// so find_entry() always misses and falls through to this path. For a found key
// we stop early (O(found position)); for a key not present we must scan the whole
// stitched buffer — O(file size). This O(file) miss cost is an existing
// limitation of the digest-index design and is tracked separately as a follow-up.
//
// NON-BLOCKING-2: Table-id matching is intentionally skipped in the stitching path
// (consistent with sequential_scan's stitching path). The V5CompressedLegacy parser
// returns entries tagged with the table_id from the SSTable header, which may hold
// default or incorrect values when headers use bare keyspace/table names rather than
// the query's fully-qualified form. Since all entries in this stitch buffer come from
// the single SSTable being queried, skipping the check is correct and safe.
for (_, entry_key, entry_value) in all_entries {
if entry_key == *key {
// Early-return on first match (BLOCKING-2: don't parse the rest of the file).
if !self.filter_tombstone(&entry_value) {
return Ok(None);
}
return Ok(Some(entry_value));
}
}
return Ok(None);
}
{
let mut file_guard = cursor.file.lock().await;
file_guard.seek(SeekFrom::Start(header_size as u64)).await?;
}
// Sequential scan through blocks
while let Some(block) = self.read_next_block(&cursor).await? {
let entries = self.parse_block_entries(&block, None)?;
for (entry_table_id, entry_key, entry_value) in entries {
if table_ids_match(&entry_table_id, table_id) && entry_key == *key {
// Extract write time from entry metadata
let _write_time = self.extract_write_time_from_entry(&entry_key, &entry_value);
// Filter out tombstones and expired data
if !self.filter_tombstone(&entry_value) {
return Ok(None);
}
return Ok(Some(entry_value));
}
}
}
Ok(None)
}
pub(super) async fn sequential_scan(
&self,
table_id: &TableId,
start_key: Option<&RowKey>,
end_key: Option<&RowKey>,
limit: Option<usize>,
schema: Option<&crate::schema::TableSchema>,
) -> Result<Vec<(RowKey, Value)>> {
log::debug!("SSTableReader::sequential_scan - Starting sequential scan");
log::debug!("SSTableReader::sequential_scan - Table ID: {}", table_id);
log::debug!(
"SSTableReader::sequential_scan - Has schema: {}",
schema.is_some()
);
// Issue #815: each scan uses its own cursor (private file position and
// chunk index), so concurrent scans on this reader run in parallel
// without the per-scan serialization #805 introduced for correctness.
let cursor = self.new_scan_cursor().await?;
let mut results = Vec::new();
let header_size = self.calculate_header_size();
log::debug!(
"SSTableReader::sequential_scan - Header size: {} bytes",
header_size
);
{
let mut file_guard = cursor.file.lock().await;
file_guard.seek(SeekFrom::Start(header_size as u64)).await?;
log::debug!(
"SSTableReader::sequential_scan - Seeked to start of data section at offset {}",
header_size
);
}
// CRITICAL FIX: V5CompressedLegacy partitions can span chunk boundaries.
// We must stitch all chunks together before parsing to avoid dropping partitions.
// Use `requires_chunk_stitching()` as the single source of truth for whether
// stitching is needed (BLOCKING-3: unified predicate).
//
// Note: We intentionally skip table_id matching in the stitching path because the
// parser may return incorrect table_ids from header defaults. Since sequential_scan
// is called with a specific table_id, all entries from this SSTable match it.
if self.requires_chunk_stitching() {
log::debug!(
"SSTableReader::sequential_scan - V5CompressedLegacy NB detected, using stitched buffer"
);
// Stitch all chunks together (reuse logic from get_all_entries)
let all_entries = self.stitch_and_parse_all_chunks(&cursor, schema).await?;
log::debug!(
"SSTableReader::sequential_scan - Stitched parsing returned {} total entries",
all_entries.len()
);
// Apply key-range filter and tombstone filter; collect ALL matching entries
// before sorting. Limit is applied AFTER sort so that LIMIT N returns the N
// token-smallest partitions, not the first N encountered in parse order.
// (BLOCKING-1: limit-after-order)
for (_entry_table_id, entry_key, entry_value) in all_entries {
if let Some(start) = start_key {
if &entry_key < start {
continue;
}
}
if let Some(end) = end_key {
if &entry_key > end {
continue;
}
}
if !self.filter_tombstone(&entry_value) {
continue;
}
results.push((entry_key, entry_value));
}
log::debug!(
"SSTableReader::sequential_scan - Filtered to {} results before limit (limit: {:?})",
results.len(),
limit
);
// Sort by Murmur3 token order (spec §5, Appendix B §313), then truncate to limit.
sort_by_token_order(&mut results);
if let Some(lim) = limit {
results.truncate(lim);
}
log::debug!(
"SSTableReader::sequential_scan - Returning {} results after sort+limit",
results.len()
);
return Ok(results);
}
// Non-stitching path for other formats
let mut block_count = 0;
while let Some(block) = self.read_next_block(&cursor).await? {
block_count += 1;
log::debug!(
"SSTableReader::sequential_scan - Read block {}, size {} bytes",
block_count,
block.len()
);
let entries = self.parse_block_entries_with_schema(&block, schema)?;
log::debug!(
"SSTableReader::sequential_scan - Block {} contains {} entries",
block_count,
entries.len()
);
for (i, (entry_table_id, entry_key, entry_value)) in entries.iter().enumerate() {
log::debug!(
"SSTableReader::sequential_scan - Block {} entry {}: table_id='{}', key={:?}",
block_count,
i,
entry_table_id,
entry_key
);
// Match table IDs - supports both qualified (keyspace.table) and unqualified (table) formats
// This allows queries with either format to match SSTables stored with either format
if !table_ids_match(entry_table_id, table_id) {
log::debug!("SSTableReader::sequential_scan - Skipping entry: table_id mismatch ('{}' != '{}')",
entry_table_id, table_id);
continue;
}
// Check key range
if let Some(start) = start_key {
if entry_key < start {
log::debug!(
"SSTableReader::sequential_scan - Skipping entry: key < start_key"
);
continue;
}
}
if let Some(end) = end_key {
if entry_key > end {
log::debug!(
"SSTableReader::sequential_scan - Skipping entry: key > end_key"
);
continue;
}
}
// Extract write time from entry metadata
let _write_time = self.extract_write_time_from_entry(entry_key, entry_value);
// Filter out tombstones and expired data
if !self.filter_tombstone(entry_value) {
log::debug!("SSTableReader::sequential_scan - Skipping entry: filtered out (tombstone or expired)");
continue;
}
log::debug!("SSTableReader::sequential_scan - Including entry in results");
results.push((entry_key.clone(), entry_value.clone()));
}
}
log::debug!(
"SSTableReader::sequential_scan - Finished scanning {} blocks",
block_count
);
log::debug!(
"SSTableReader::sequential_scan - {} results before sort+limit",
results.len()
);
// Sort by Murmur3 token order (spec §5, Appendix B §313), then apply limit.
// Limit is applied AFTER sort so that LIMIT N returns the N token-smallest
// partitions (BLOCKING-1: limit-after-order).
sort_by_token_order(&mut results);
if let Some(lim) = limit {
results.truncate(lim);
}
log::debug!(
"SSTableReader::sequential_scan - Returning {} results after sort+limit",
results.len()
);
Ok(results)
}
/// Scan a range of keys AND return per-cell write metadata.
///
/// Used when `ProjectionFlags::include_cell_metadata` is set (issue #693).
/// Falls through to `stitch_and_parse_all_chunks_with_metadata` for
/// V5CompressedLegacy format (the common path for real SSTables).
/// Returns `None` as the metadata for non-V5 formats (they do not carry
/// per-cell timestamps in a way the parser currently surfaces).
pub async fn scan_with_cell_metadata(
&self,
table_id: &TableId,
start_key: Option<&RowKey>,
end_key: Option<&RowKey>,
limit: Option<usize>,
schema: Option<&crate::schema::TableSchema>,
) -> Result<
Vec<(
RowKey,
Value,
std::collections::HashMap<String, CellWriteMetadata>,
)>,
> {
log::debug!("SSTableReader::scan_with_cell_metadata - Starting");
// Issue #660: BTI ("da") metadata scan — same whole-Data.db walk as the
// plain BTI scan, but surfaces per-cell write metadata for WRITETIME/TTL.
if self.bti_partitions_db.is_some() {
return self
.bti_scan_with_metadata(start_key, end_key, limit, schema)
.await;
}
// Issue #815: independent per-scan cursor — no cross-scan serialization.
let cursor = self.new_scan_cursor().await?;
let header_size = self.calculate_header_size();
{
let mut file_guard = cursor.file.lock().await;
file_guard.seek(SeekFrom::Start(header_size as u64)).await?;
}
// V5CompressedLegacy (stitching) path — the common path for Cassandra 5.0 SSTables.
if self.requires_chunk_stitching() {
let all_entries = self
.stitch_and_parse_all_chunks_with_metadata(&cursor, schema)
.await?;
let mut results = Vec::new();
for (_entry_table_id, entry_key, entry_value, cell_meta) in all_entries {
if let Some(start) = start_key {
if &entry_key < start {
continue;
}
}
if let Some(end) = end_key {
if &entry_key > end {
continue;
}
}
if !self.filter_tombstone(&entry_value) {
continue;
}
results.push((entry_key, entry_value, cell_meta));
}
sort_by_token_order_with_meta(&mut results);
if let Some(lim) = limit {
results.truncate(lim);
}
log::debug!(
"SSTableReader::scan_with_cell_metadata - Returning {} results (stitched path)",
results.len()
);
return Ok(results);
}
// Non-stitching path: fall back to regular scan + empty metadata.
// Per-cell metadata is not yet surfaced for block-entry formats.
let plain = self
.sequential_scan(table_id, start_key, end_key, limit, schema)
.await?;
Ok(plain
.into_iter()
.map(|(k, v)| (k, v, std::collections::HashMap::new()))
.collect())
}
/// Mint a fresh, independent cursor for one scan (issue #815).
///
/// Each cursor owns a private file handle (or mmap cursor) and chunk index,
/// so concurrent scans on this reader never share a mutable file position —
/// they run in parallel without the per-scan serialization #805 required.
pub(super) async fn new_scan_cursor(&self) -> Result<ScanCursor> {
Ok(ScanCursor::new(
self.scan_source.open(&self.file_path).await?,
))
}
/// Read the next block from a scan-local `cursor` (its own file position and
/// chunk index). See [`Self::new_scan_cursor`].
pub(super) async fn read_next_block(&self, cursor: &ScanCursor) -> Result<Option<Vec<u8>>> {
use super::block_io;
block_io::read_next_block(
&cursor.file,
&self.header.cassandra_version,
&self.config,
&self.compression_info,
&cursor.chunk_index,
self.actual_header_size as u64,
)
.await
}
/// Prepare for a delta-scan pass: stitch all compressed chunks of the data
/// section and return the decompressed buffer together with a pre-configured
/// parser.
///
/// Uses its own per-scan cursor (issue #815), so it no longer needs the
/// caller to serialize against concurrent reads. This method is gated on the
/// `delta-scan` feature and is the only bridge between the SSTableReader
/// internals and the `delta_scan` module, which cannot access private
/// helpers directly.
///
/// The `schema` parameter is not used here — it is threaded through the
/// caller's `parse_block_emit_delta` invocation instead. The parser is
/// built via `build_v5_parser()` which handles version-gates and UDT
/// registry without needing the schema at construction time.
#[cfg(feature = "delta-scan")]
pub async fn prepare_delta_scan(
&self,
) -> Result<(Vec<u8>, super::parsing::V5CompressedLegacyParser)> {
use tokio::io::AsyncSeekExt;
// Seek the per-scan cursor to the start of the data section.
let cursor = self.new_scan_cursor().await?;
let header_size = self.calculate_header_size();
{
let mut file_guard = cursor.file.lock().await;
file_guard
.seek(std::io::SeekFrom::Start(header_size as u64))
.await?;
}
// Stitch all compressed chunks (bounded by uncompressed data-section size).
let stitched = self.stitch_all_chunks(&cursor).await?;
// Build a parser (re-using the existing builder so version-gates and
// UDT registry are threaded through correctly).
let parser = self.build_v5_parser();
Ok((stitched, parser))
}
}
#[cfg(test)]
mod tests {
use super::*;
// =========================================================================
// table_ids_match tests
// =========================================================================
#[test]
fn test_table_ids_match_strict_keyspace_aware() {
let a = TableId::new("ks_a.users".to_string());
let b = TableId::new("ks_b.users".to_string());
// Both qualified, different keyspace, same table name → must NOT match
// (the permissive helper would match these).
assert!(table_ids_match(&a, &b), "permissive helper matches on name");
assert!(
!table_ids_match_strict(&a, &b),
"strict guard must reject a wrong-keyspace same-name query"
);
// Both qualified, identical → match.
let a2 = TableId::new("ks_a.users".to_string());
assert!(table_ids_match_strict(&a, &a2));
// One side unqualified → fall back to permissive name match.
let unq = TableId::new("users".to_string());
assert!(table_ids_match_strict(&a, &unq));
assert!(table_ids_match_strict(&unq, &a));
}
#[test]
fn test_bti_lookup_step_decision() {
// Key prefix buffered and matches → parse.
assert_eq!(bti_lookup_step(true, true, true), BtiLookupStep::Parse);
assert_eq!(bti_lookup_step(true, true, false), BtiLookupStep::Parse);
// Key prefix buffered but does NOT match → absent (prefix collision).
assert_eq!(bti_lookup_step(true, false, true), BtiLookupStep::Absent);
assert_eq!(bti_lookup_step(true, false, false), BtiLookupStep::Absent);
// Key prefix NOT yet buffered (header straddles a chunk boundary):
// - chunk-targeted path MUST pull the next chunk, never parse a
// truncated header (issue #831 review regression);
assert_eq!(
bti_lookup_step(false, false, true),
BtiLookupStep::PullNextChunk
);
// - whole-section fallback cannot grow → absent.
assert_eq!(bti_lookup_step(false, false, false), BtiLookupStep::Absent);
}
#[test]
fn test_table_ids_match_exact() {
// Exact match cases
let id1 = TableId::new("simple_table".to_string());
let id2 = TableId::new("simple_table".to_string());
assert!(table_ids_match(&id1, &id2));
let id3 = TableId::new("test_basic.simple_table".to_string());
let id4 = TableId::new("test_basic.simple_table".to_string());
assert!(table_ids_match(&id3, &id4));
}
#[test]
fn test_table_ids_match_qualified_vs_unqualified() {
// Qualified matches unqualified
let qualified = TableId::new("test_basic.simple_table".to_string());
let unqualified = TableId::new("simple_table".to_string());
assert!(table_ids_match(&qualified, &unqualified));
assert!(table_ids_match(&unqualified, &qualified));
}
#[test]
fn test_table_ids_match_different_keyspaces() {
// Different keyspaces but same table name - should match on table name
let id1 = TableId::new("keyspace1.users".to_string());
let id2 = TableId::new("keyspace2.users".to_string());
assert!(
table_ids_match(&id1, &id2),
"Same table name should match across keyspaces"
);
}
#[test]
fn test_table_ids_match_completely_different() {
// Completely different tables - should not match
let id1 = TableId::new("users".to_string());
let id2 = TableId::new("orders".to_string());
assert!(!table_ids_match(&id1, &id2));
let id3 = TableId::new("test.users".to_string());
let id4 = TableId::new("test.orders".to_string());
assert!(!table_ids_match(&id3, &id4));
}
// =========================================================================
// Issue #831: BTI chunk-targeting math + window stop-condition logic
// =========================================================================
/// The chunk-index arithmetic must match `CompressionInfo`'s definitions:
/// `target_chunk = off / chunk_length`, `window_base = target_chunk *
/// chunk_length`, `within = off - window_base` (== `off % chunk_length`).
#[test]
fn bti_chunk_target_arithmetic() {
// Single-chunk case (simple_table fixture shape): chunk_length 16384,
// offset 0/63/125 all land in chunk 0 with within == offset.
let chunk_length = 16384;
for off in [0usize, 63, 125] {
let (chunk, base, within) = SSTableReader::bti_chunk_target(off, chunk_length);
assert_eq!(chunk, 0, "off {off} must be in chunk 0");
assert_eq!(base, 0, "chunk 0 window base must be 0");
assert_eq!(within, off, "within must equal offset in chunk 0");
}
// Multi-chunk arithmetic with a small chunk_length to exercise the math.
let cl = 100usize;
// Exactly on a chunk boundary.
assert_eq!(SSTableReader::bti_chunk_target(100, cl), (1, 100, 0));
assert_eq!(SSTableReader::bti_chunk_target(200, cl), (2, 200, 0));
// Inside chunk 1.
assert_eq!(SSTableReader::bti_chunk_target(150, cl), (1, 100, 50));
// Just before a boundary.
assert_eq!(SSTableReader::bti_chunk_target(99, cl), (0, 0, 99));
// Within always equals off % chunk_length, base = chunk * chunk_length.
for off in [0usize, 1, 99, 100, 101, 250, 999] {
let (chunk, base, within) = SSTableReader::bti_chunk_target(off, cl);
assert_eq!(within, off % cl);
assert_eq!(base, chunk * cl);
assert_eq!(base + within, off);
}
}
/// `bti_partition_key_bytes_available` drives the growing-window stop
/// condition: while the `[flags][key_len][key bytes]` prefix is NOT yet fully
/// buffered it returns false (the chunk-targeted loop pulls another chunk);
/// once the declared key bytes have all arrived it returns true (the
/// INVARIANT-3 key match can be evaluated). This is the SYNTHETIC spanning
/// test: the key prefix straddles a simulated chunk boundary and the window
/// grows one byte at a time across it.
///
/// NOTE: a full multi-chunk-spanning parse against a real
/// `V5CompressedLegacyParser` has NO real BTI DataOffset fixture — these are
/// narrow partitions that fit within a single chunk — so the spanning *parse*
/// path is only exercised structurally here via the byte-availability gate
/// that decides when a parse may even be attempted. This calls the real
/// associated function (no I/O), so a regression in its boundary math is
/// caught.
#[test]
fn bti_partition_key_bytes_available_growing_window() {
// Header at within=0: [flags=0x00][key_len=4][k0 k1 k2 k3]. Simulate a
// window that grows from 0 bytes up to the full prefix; availability must
// flip to true exactly when all 4 declared key bytes are buffered.
let expected_key = [0xAA, 0xBB, 0xCC, 0xDD];
let within = 0usize;
let full = {
let mut v = vec![0x00u8, expected_key.len() as u8];
v.extend_from_slice(&expected_key);
v
};
let avail = |len: usize| {
SSTableReader::bti_partition_key_bytes_available(&full[..len], within, &expected_key)
};
// Not enough for flags+key_len yet.
assert!(!avail(0));
assert!(!avail(1));
// flags+key_len present but key bytes not fully buffered.
assert!(!avail(2));
assert!(!avail(3)); // 1 key byte
assert!(!avail(4)); // 2 key bytes
assert!(!avail(5)); // 3 key bytes
// All 4 key bytes buffered -> available (boundary fully crossed).
assert!(avail(6));
assert!(avail(full.len()));
// A non-zero `within` (target partition not at window start) must use the
// same relative math.
let mut padded = vec![0x77u8, 0x88];
padded.extend_from_slice(&full);
assert!(!SSTableReader::bti_partition_key_bytes_available(
&padded[..2 + 5],
2,
&expected_key
));
assert!(SSTableReader::bti_partition_key_bytes_available(
&padded,
2,
&expected_key
));
}
#[test]
fn test_table_ids_match_edge_cases() {
// Table names with dots (unusual but possible)
let id1 = TableId::new("schema.table.subtable".to_string());
let id2 = TableId::new("subtable".to_string());
assert!(
table_ids_match(&id1, &id2),
"Should match on last component"
);
}
#[test]
fn test_table_ids_match_empty() {
// Empty table IDs
let id1 = TableId::new("".to_string());
let id2 = TableId::new("".to_string());
assert!(table_ids_match(&id1, &id2), "Empty IDs should match");
}
// =========================================================================
// Key comparison tests
// =========================================================================
#[test]
fn test_row_key_comparison() {
let key1 = RowKey::new(vec![1, 2, 3]);
let key2 = RowKey::new(vec![1, 2, 3]);
let key3 = RowKey::new(vec![1, 2, 4]);
assert_eq!(key1, key2);
assert_ne!(key1, key3);
assert!(key1 < key3);
}
#[test]
fn test_row_key_ordering() {
let key_a = RowKey::new(vec![0x01]);
let key_b = RowKey::new(vec![0x02]);
let key_c = RowKey::new(vec![0x01, 0x00]); // Longer but starts with 0x01
assert!(key_a < key_b);
assert!(key_a < key_c); // Shorter prefix comes first in lexicographic order
}
// =========================================================================
// Value tests
// =========================================================================
#[test]
fn test_value_blob_creation() {
let data = vec![1, 2, 3, 4, 5];
let value = Value::Blob(data.clone());
if let Value::Blob(v) = value {
assert_eq!(v, data);
} else {
panic!("Expected Value::Blob");
}
}
// =========================================================================
// Integration tests with real SSTable data
// =========================================================================
#[tokio::test]
async fn test_get_nonexistent_key() {
use std::path::PathBuf;
use std::sync::Arc;
// Test with real SSTable data if available
let datasets_root = match std::env::var("CQLITE_DATASETS_ROOT") {
Ok(root) => PathBuf::from(root),
Err(_) => {
eprintln!("CQLITE_DATASETS_ROOT not set, skipping test");
return;
}
};
let simple_table_dir = datasets_root.join("sstables/test_basic");
if !simple_table_dir.exists() {
eprintln!("test_basic not found, skipping test");
return;
}
// Find simple_table
let table_dir = std::fs::read_dir(&simple_table_dir)
.ok()
.and_then(|entries| {
entries
.filter_map(|e| e.ok())
.find(|e| {
e.file_name()
.to_str()
.map(|n| n.starts_with("simple_table"))
.unwrap_or(false)
})
.map(|e| e.path())
});
let Some(table_path) = table_dir else {
eprintln!("simple_table not found, skipping");
return;
};
// Find Data.db file
let data_file = std::fs::read_dir(&table_path).ok().and_then(|entries| {
entries
.filter_map(|e| e.ok())
.find(|e| {
e.file_name()
.to_str()
.map(|n| n.ends_with("-Data.db"))
.unwrap_or(false)
})
.map(|e| e.path())
});
let Some(data_path) = data_file else {
eprintln!("Data.db not found, skipping");
return;
};
let config = crate::Config::default();
let platform = Arc::new(
crate::Platform::new(&config)
.await
.expect("Failed to create platform"),
);
let reader = SSTableReader::open(&data_path, &config, platform)
.await
.expect("Failed to open SSTable");
// Try to get a key that doesn't exist
let table_id = TableId::new("test_basic.simple_table".to_string());
let nonexistent_key = RowKey::new(vec![0xFF, 0xFF, 0xFF, 0xFF]); // Very unlikely to exist
let result = reader.get(&table_id, &nonexistent_key).await;
assert!(
result.is_ok(),
"get() should succeed even for nonexistent key"
);
assert!(
result.unwrap().is_none(),
"Nonexistent key should return None"
);
}
#[tokio::test]
async fn test_scan_with_limit() {
use std::path::PathBuf;
use std::sync::Arc;
let datasets_root = match std::env::var("CQLITE_DATASETS_ROOT") {
Ok(root) => PathBuf::from(root),
Err(_) => {
eprintln!("CQLITE_DATASETS_ROOT not set, skipping test");
return;
}
};
let simple_table_dir = datasets_root.join("sstables/test_basic");
if !simple_table_dir.exists() {
eprintln!("test_basic not found, skipping test");
return;
}
// Find simple_table
let table_dir = std::fs::read_dir(&simple_table_dir)
.ok()
.and_then(|entries| {
entries
.filter_map(|e| e.ok())
.find(|e| {
e.file_name()
.to_str()
.map(|n| n.starts_with("simple_table"))
.unwrap_or(false)
})
.map(|e| e.path())
});
let Some(table_path) = table_dir else {
eprintln!("simple_table not found, skipping");
return;
};
let data_file = std::fs::read_dir(&table_path).ok().and_then(|entries| {
entries
.filter_map(|e| e.ok())
.find(|e| {
e.file_name()
.to_str()
.map(|n| n.ends_with("-Data.db"))
.unwrap_or(false)
})
.map(|e| e.path())
});
let Some(data_path) = data_file else {
eprintln!("Data.db not found, skipping");
return;
};
let config = crate::Config::default();
let platform = Arc::new(
crate::Platform::new(&config)
.await
.expect("Failed to create platform"),
);
let reader = SSTableReader::open(&data_path, &config, platform)
.await
.expect("Failed to open SSTable");
let table_id = TableId::new("test_basic.simple_table".to_string());
// Test scan with limit
let result = reader.scan(&table_id, None, None, Some(5), None).await;
assert!(result.is_ok(), "scan() should succeed");
let entries = result.unwrap();
assert!(
entries.len() <= 5,
"Scan with limit 5 should return at most 5 entries, got {}",
entries.len()
);
eprintln!("Scan with limit 5 returned {} entries", entries.len());
}
#[tokio::test]
async fn test_scan_full_table() {
use std::path::PathBuf;
use std::sync::Arc;
let datasets_root = match std::env::var("CQLITE_DATASETS_ROOT") {
Ok(root) => PathBuf::from(root),
Err(_) => {
eprintln!("CQLITE_DATASETS_ROOT not set, skipping test");
return;
}
};
let simple_table_dir = datasets_root.join("sstables/test_basic");
if !simple_table_dir.exists() {
eprintln!("test_basic not found, skipping test");
return;
}
// Find simple_table
let table_dir = std::fs::read_dir(&simple_table_dir)
.ok()
.and_then(|entries| {
entries
.filter_map(|e| e.ok())
.find(|e| {
e.file_name()
.to_str()
.map(|n| n.starts_with("simple_table"))
.unwrap_or(false)
})
.map(|e| e.path())
});
let Some(table_path) = table_dir else {
eprintln!("simple_table not found, skipping");
return;
};
let data_file = std::fs::read_dir(&table_path).ok().and_then(|entries| {
entries
.filter_map(|e| e.ok())
.find(|e| {
e.file_name()
.to_str()
.map(|n| n.ends_with("-Data.db"))
.unwrap_or(false)
})
.map(|e| e.path())
});
let Some(data_path) = data_file else {
eprintln!("Data.db not found, skipping");
return;
};
let config = crate::Config::default();
let platform = Arc::new(
crate::Platform::new(&config)
.await
.expect("Failed to create platform"),
);
let reader = SSTableReader::open(&data_path, &config, platform)
.await
.expect("Failed to open SSTable");
let table_id = TableId::new("test_basic.simple_table".to_string());
// Full table scan (no limit)
let result = reader.scan(&table_id, None, None, None, None).await;
assert!(result.is_ok(), "Full scan should succeed");
let entries = result.unwrap();
eprintln!("Full scan returned {} entries", entries.len());
}
#[tokio::test]
async fn test_get_all_entries() {
use std::path::PathBuf;
use std::sync::Arc;
let datasets_root = match std::env::var("CQLITE_DATASETS_ROOT") {
Ok(root) => PathBuf::from(root),
Err(_) => {
eprintln!("CQLITE_DATASETS_ROOT not set, skipping test");
return;
}
};
let simple_table_dir = datasets_root.join("sstables/test_basic");
if !simple_table_dir.exists() {
eprintln!("test_basic not found, skipping test");
return;
}
// Find simple_table
let table_dir = std::fs::read_dir(&simple_table_dir)
.ok()
.and_then(|entries| {
entries
.filter_map(|e| e.ok())
.find(|e| {
e.file_name()
.to_str()
.map(|n| n.starts_with("simple_table"))
.unwrap_or(false)
})
.map(|e| e.path())
});
let Some(table_path) = table_dir else {
eprintln!("simple_table not found, skipping");
return;
};
let data_file = std::fs::read_dir(&table_path).ok().and_then(|entries| {
entries
.filter_map(|e| e.ok())
.find(|e| {
e.file_name()
.to_str()
.map(|n| n.ends_with("-Data.db"))
.unwrap_or(false)
})
.map(|e| e.path())
});
let Some(data_path) = data_file else {
eprintln!("Data.db not found, skipping");
return;
};
let config = crate::Config::default();
let platform = Arc::new(
crate::Platform::new(&config)
.await
.expect("Failed to create platform"),
);
let reader = SSTableReader::open(&data_path, &config, platform)
.await
.expect("Failed to open SSTable");
// Get all entries (for compaction use case)
let result = reader.get_all_entries().await;
assert!(result.is_ok(), "get_all_entries() should succeed");
let entries = result.unwrap();
eprintln!("get_all_entries() returned {} entries", entries.len());
}
/// Regression test for Issue #480: static cell duplication on read.
///
/// static_columns_table has 100 partitions, each containing one static_block
/// and one clustering row. CQLite should return exactly 100 result rows — one
/// per partition — not 200 (which would occur if static rows were emitted as
/// separate result entries).
///
/// Two bugs were fixed:
/// 1. Snappy varint collision: bytes `0xC0 0x51` at the start of the Snappy
/// stream were misidentified as the V5_0StaticColumns magic number, causing
/// the file pointer to advance past part of the compressed data before
/// decompression, resulting in "corrupt input" errors.
/// 2. Static row duplication: static rows were pushed into `results` just like
/// clustering rows. They should be accumulated per-partition and merged into
/// each subsequent clustering row instead.
#[tokio::test]
async fn test_static_columns_table_row_count_issue480() {
use std::path::PathBuf;
use std::sync::Arc;
let datasets_root = match std::env::var("CQLITE_DATASETS_ROOT") {
Ok(root) => PathBuf::from(root),
Err(_) => {
eprintln!("CQLITE_DATASETS_ROOT not set, skipping Issue #480 regression test");
return;
}
};
let table_base = datasets_root.join("sstables/test_basic");
if !table_base.exists() {
eprintln!("test_basic dir not found, skipping Issue #480 regression test");
return;
}
// Locate the static_columns_table directory
let table_dir = std::fs::read_dir(&table_base).ok().and_then(|entries| {
entries
.filter_map(|e| e.ok())
.find(|e| {
e.file_name()
.to_str()
.map(|n| n.starts_with("static_columns_table"))
.unwrap_or(false)
})
.map(|e| e.path())
});
let Some(table_path) = table_dir else {
eprintln!("static_columns_table not found, skipping Issue #480 regression test");
return;
};
// Find the Data.db file (must be real binary, not macOS ._resource_fork)
let data_file = std::fs::read_dir(&table_path).ok().and_then(|entries| {
entries
.filter_map(|e| e.ok())
.find(|e| {
let name = e.file_name();
let s = name.to_str().unwrap_or("");
s.ends_with("-Data.db") && !s.starts_with("._")
})
.map(|e| e.path())
});
let Some(data_path) = data_file else {
eprintln!("Data.db not found in static_columns_table dir, skipping");
return;
};
let config = crate::Config::default();
let platform = Arc::new(
crate::Platform::new(&config)
.await
.expect("Failed to create platform"),
);
let reader = SSTableReader::open(&data_path, &config, platform)
.await
.expect("Failed to open static_columns_table SSTable");
let table_id = crate::types::TableId::new("test_basic.static_columns_table".to_string());
let result = reader.scan(&table_id, None, None, None, None).await;
assert!(
result.is_ok(),
"Scan of static_columns_table should succeed: {:?}",
result.err()
);
let entries = result.unwrap();
eprintln!(
"Issue #480 regression: static_columns_table scan returned {} rows",
entries.len()
);
// Expected: 100 rows (one per partition, static data merged into clustering row)
// Before fix: 0 rows (Snappy decompression failure)
// After fixing only decompression: 200 rows (static rows emitted separately)
// After full fix: 100 rows
assert_eq!(
entries.len(),
100,
"static_columns_table should return 100 rows (one per partition), \
got {}. Regression for Issue #480: static cell duplication on read.",
entries.len()
);
}
}