1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
//! CRUD Operations Module
//!
//! Provides complete Create, Read, Update, Delete operations for rows
//!
//! # Features
//! - Row-level operations (insert_row, get_row, update_row, delete_row)
//! - Table-aware operations (insert_row_to_table, get_table_row, etc.)
//! - Batch operations (batch_insert_rows, batch_get_rows)
//! - Scan operations (scan_all_rows, scan_table_rows)
//! - Prefetching and caching for sequential access
use super::core::MoteDB;
use crate::storage::row_format;
use crate::txn::wal::WALRecord;
use crate::types::{ColumnType, PartitionId, Row, RowId, Value};
use crate::{Result, StorageError};
use std::collections::HashSet;
use std::sync::Arc;
/// Extract column types from a table schema for RawRow encoding.
/// Deserialize a row, trying RawRow first (with schema) and falling back to bincode.
fn deserialize_row(data: &[u8], col_types: &[ColumnType]) -> crate::Result<Row> {
row_format::decode(data, col_types)
}
impl MoteDB {
// ==================== Table-Aware CRUD Operations ====================
/// Insert a row to a specific table (table-aware API)
///
/// # Arguments
/// * `table_name` - Name of the table
/// * `row` - Row data to insert
///
/// # Example
/// ```ignore
/// let row_id = db.insert_row_to_table("users", vec![
/// Value::Integer(1),
/// Value::Text("Alice".into()),
/// ])?;
/// ```ignore
pub fn insert_row_to_table(&self, table_name: &str, mut row: Row) -> Result<RowId> {
ensure_open!(self);
// 1. Get table schema
let schema = self.table_registry.get_table(table_name)?;
// 1.5 Check primary key uniqueness for non-AUTO_INCREMENT tables
if !schema.is_primary_key_auto_increment() {
if let Some(pk_name) = schema.primary_key() {
if let Some(pk_col) = schema.get_column(pk_name) {
if let Some(pk_value) = row.get(pk_col.position) {
// NULL primary key is invalid per SQL standard
if matches!(pk_value, Value::Null) {
return Err(StorageError::InvalidData(format!(
"NULL primary key is not allowed for table '{}'",
table_name
)));
}
let pk_key = crate::database::pk_cache::PkKey::from_value(pk_value);
// Atomic check-and-insert: eliminates TOCTOU race between
// the uniqueness check and the actual row insert.
// On cache hit (duplicate), insert_if_absent returns Err immediately.
// On cache miss, falls through to slow path below.
if let Some(lookup) = self.pk_lookup.get(table_name) {
match lookup
.insert_if_absent(pk_key.clone(), 0 /* placeholder row_id */)
{
Ok(()) => {
// Successfully reserved — will update with real row_id below
}
Err(_) => {
return Err(StorageError::InvalidData(format!(
"Duplicate primary key {:?} for table '{}'",
pk_value, table_name
)));
}
}
} else {
// No PK cache yet — fall back to slow path
match self.query_by_column(table_name, pk_name, pk_value) {
Ok(found) if !found.is_empty() => {
let mut has_live = false;
for &rid in &found {
if self.get_table_row(table_name, rid)?.is_some() {
has_live = true;
break;
}
}
if has_live {
return Err(StorageError::InvalidData(format!(
"Duplicate primary key {:?} for table '{}'",
pk_value, table_name
)));
}
}
_ => {}
}
}
}
}
}
}
// 2. 🚀 P3+4: For AUTO_INCREMENT primary key, use per-table counter
// Ensure row has enough slots for AUTO_INCREMENT PK column before validation
if schema.is_primary_key_auto_increment() {
if let Some(pk_col_name) = schema.primary_key() {
if let Some(pk_col) = schema.get_column(pk_col_name) {
while row.len() <= pk_col.position {
row.push(Value::Null);
}
}
}
}
// 3. Validate row (before allocating AUTO_INCREMENT to avoid ID waste)
schema.validate_row(&row).map_err(|e| {
StorageError::InvalidData(format!(
"Row validation failed for table '{}': {}",
table_name, e
))
})?;
let row_id = if schema.is_primary_key_auto_increment() {
// Check if the user provided an explicit PK value.
let explicit_id = schema
.primary_key()
.and_then(|pk| schema.get_column(pk))
.and_then(|col| row.get(col.position).cloned())
.and_then(|v| {
if let Value::Integer(i) = v {
Some(i)
} else {
None
}
});
if let Some(explicit) = explicit_id {
// 🔑 User provided an explicit ID for an AUTO_INCREMENT column.
// Check PK uniqueness first — explicit IDs can collide with
// previously auto-assigned IDs (the bug: explicit ID was accepted
// without checking, allowing duplicate PKs).
let pk_val = Value::Integer(explicit);
let pk_key = crate::database::pk_cache::PkKey::from_value(&pk_val);
if let Some(lookup) = self.pk_lookup.get(table_name) {
if lookup.get_pk(&pk_key).is_some() {
return Err(StorageError::InvalidData(format!(
"Duplicate primary key {} for table '{}'",
explicit, table_name
)));
}
}
// Use it, but advance the counter past it so the next auto
// insert doesn't collide.
let counter = {
self.table_auto_increment
.entry(table_name.to_string())
.or_insert_with(|| {
Arc::new(std::sync::atomic::AtomicI64::new(
schema.get_auto_increment_start(),
))
})
.value()
.clone()
};
// Advance counter to max(current, explicit + 1).
let _ = counter.fetch_max(explicit + 1, std::sync::atomic::Ordering::Relaxed);
if let Err(e) = self
.table_registry
.update_auto_increment_counter(table_name, explicit)
{
warn_log!(
"[MoteDB] Auto-increment counter update failed for {}: {}",
table_name,
e
);
}
explicit as RowId
} else {
// 🚀 Phase 4: Use per-table AUTO_INCREMENT counter (lock-free AtomicI64)
let counter = {
self.table_auto_increment
.entry(table_name.to_string())
.or_insert_with(|| {
Arc::new(std::sync::atomic::AtomicI64::new(
schema.get_auto_increment_start(),
))
})
.value()
.clone()
};
// 🚀 Phase 5: Overflow protection (B1)
let id = counter.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
if !(0..=i64::MAX - 1000).contains(&id) {
return Err(StorageError::AutoIncrementOverflow(table_name.to_string()));
}
// P2: Update persisted counter (lazy — persisted during checkpoint)
if let Err(e) = self
.table_registry
.update_auto_increment_counter(table_name, id)
{
warn_log!(
"[MoteDB] Auto-increment counter update failed for {}: {}",
table_name,
e
);
}
// Fill AUTO_INCREMENT primary key with id
if let Some(pk_col_name) = schema.primary_key() {
if let Some(pk_col) = schema.get_column(pk_col_name) {
// Ensure row has enough slots
while row.len() <= pk_col.position {
row.push(Value::Null);
}
// Fill in id as primary key value
row[pk_col.position] = Value::Integer(id);
}
}
id as RowId
} // end else (auto counter path)
} else {
// Non-AUTO_INCREMENT: use global row_id (lock-free atomic)
self.next_row_id
.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
};
// 4. Determine partition
let composite_key = self.make_composite_key(table_name, row_id);
let partition = (composite_key % self.num_partitions as u64) as PartitionId;
// 5. Encode row to raw bytes (shared between WAL and LSM — zero-copy recovery)
let col_types = schema.col_types();
let row_data = row_format::encode(&row, col_types).or_else(|_| {
bincode::serialize(&row)
.map_err(|e| StorageError::Serialization(format!("Row encode failed: {}", e)))
})?;
// 6. Increment pending counter BEFORE WAL write (checkpoint uses this as barrier)
self.increment_pending_updates();
// 7. Write to WAL for durability
self.wal
.log_insert_raw_ref(table_name, partition, row_id, &row_data, 0)?;
// 7. Write to columnar buffer (primary storage). Skip LSM — columnar is the source of truth.
let ts = self
.write_lsn
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
// 🔑 PERF: use put_ref (&str) to avoid table_name.to_string() allocation.
self.row_cache.put_ref(table_name, row_id, row.clone());
// 🚀 Columnar write buffer (zero-encode path).
// 🔑 PERF: Skip the legacy columnar_write_bufs for ColSegmentStore tables
// — the data is written to ColSegmentStore below (the source of truth since
// v0.3.0), and sync_col_segment_to_sstables copies it into columnar_sstables
// for legacy read paths. Writing it here too doubled the per-INSERT work
// (add_values + lock + Vec<Value> clone) for zero benefit — removing it cut
// single-row INSERT from 9.5µs to ~6µs on the NoSync path.
// The legacy buffer is still used by pre-v0.3 tables that lack a
// ColSegmentStore entry (created lazily below).
if !self.col_segment_stores.contains_key(table_name) {
use dashmap::mapref::entry::Entry;
let builder_arc = match self.columnar_write_bufs.entry(table_name.to_string()) {
Entry::Occupied(o) => o.get().clone(),
Entry::Vacant(v) => {
let indexes_dir = self.path.join("indexes");
std::fs::create_dir_all(&indexes_dir).ok();
let path = indexes_dir.join(format!("{}_col.sst", table_name));
let b = Arc::new(parking_lot::Mutex::new(
crate::storage::lsm::columnar::ColumnarSSTableBuilder::new(
path,
schema.col_types().to_vec(),
),
));
v.insert(b.clone());
b
}
};
let mut builder = builder_arc.lock();
// 🔑 PERF: composite_key already packs table_id<<32 | row_id — reuse it.
let _ = builder.add_values(composite_key, ts, false, &row);
}
// S9: also write to ColSegmentStore (so single-row INSERTs survive restart).
// 🔑 PERF: use append_row_ref (by reference) instead of append_rows
// (which clones Vec<Value> into a tuple). Saves one heap alloc per INSERT.
{
// 🔑 PERF: reuse composite_key (table_id<<32 | row_id), and pass
// col_types by reference — get_or_create skips the to_vec() when the
// store already exists (the common case after the first insert).
let store = self.get_or_create_col_segment_store(table_name, schema.col_types())?;
store.append_row_ref(composite_key, ts, &row)?;
if store.buffered_row_count() >= 100000 {
store.flush_buffer()?;
}
}
// 7. Update indexes
{
let mut index_errors: Vec<String> = Vec::new();
// Reusable index key buffer (allocated once, reused per column)
let mut index_key_buf = String::with_capacity(table_name.len() + 1 + 16);
for col_def in &schema.columns {
let col_name = &col_def.name;
let col_value = row.get(col_def.position);
let Some(col_value) = col_value else {
continue;
};
// In-memory PK lookup (O(1) resolution, bypasses disk-based B-Tree)
if let Some(pk_name) = schema.primary_key() {
if col_name == pk_name && !schema.is_primary_key_auto_increment() {
if let Some(lookup) = self.pk_lookup.get(table_name) {
lookup.insert(
crate::database::pk_cache::PkKey::from_value(col_value),
row_id,
);
}
}
}
// 7.1 Column Index — reuse key buffer to avoid per-column allocation
{
index_key_buf.clear();
index_key_buf.push_str(table_name);
index_key_buf.push('.');
index_key_buf.push_str(col_name);
if let Some(index_ref) = self.column_indexes.get(&index_key_buf) {
// NULL values are valid SQL but not indexable — skip silently
if !matches!(col_value, Value::Null) {
if let Err(_e) = index_ref.value().insert(col_value, row_id) {
debug_log!(
"[insert_row] Failed to update column index '{}': {}",
col_name,
_e
);
index_errors.push(index_key_buf.clone());
}
}
}
}
// 7.2 Vector Index
if let crate::types::ColumnType::Tensor(_dim) = col_def.col_type {
if let Some(index_name) = self.index_registry.find_by_column(
table_name,
col_name,
crate::database::index_metadata::IndexType::Vector,
) {
let f32_vec = match col_value {
crate::types::Value::Vector(vec) => Some(vec.as_slice().to_vec()),
crate::types::Value::Tensor(tensor) => Some(tensor.to_f32()),
_ => None,
};
if let Some(vec) = f32_vec {
if let Err(_e) = self.update_vector(row_id, &index_name, &vec) {
debug_log!(
"[insert_row] Failed to update vector index '{}': {}",
index_name,
_e
);
index_errors.push(index_name.clone());
}
}
}
}
// 7.3 Text Index
if matches!(col_def.col_type, crate::types::ColumnType::Text) {
if let Some(index_name) = self.index_registry.find_by_column(
table_name,
col_name,
crate::database::index_metadata::IndexType::Text,
) {
if let crate::types::Value::Text(text) = col_value {
if let Err(_e) = self.insert_text(row_id, &index_name, text) {
debug_log!(
"[insert_row] Failed to update text index '{}': {}",
index_name,
_e
);
index_errors.push(index_name.clone());
}
}
}
}
// 7.4 i-Octree Index (3D point cloud)
if matches!(col_def.col_type, crate::types::ColumnType::Spatial) {
if let Some(index_name) = self.index_registry.find_by_column(
table_name,
col_name,
crate::database::index_metadata::IndexType::Octree,
) {
if let crate::types::Value::Spatial(geom) = col_value {
if let Err(_e) = self.insert_ioctree_point(row_id, &index_name, geom) {
debug_log!(
"[insert_row] Failed to update ioctree index '{}': {}",
index_name,
_e
);
index_errors.push(index_name.clone());
}
}
}
}
}
// Mark only the individual failed indexes as stale
if !index_errors.is_empty() {
debug_log!(
"[insert_row] {} index updates failed for table '{}', marking stale",
index_errors.len(),
table_name
);
for idx_name in &index_errors {
self.index_registry.mark_stale(idx_name);
}
}
} // end index_update_strategy check
// 9. Increment row count for COUNT(*) fast path
if let Some(counter) = self.table_row_count.get(table_name) {
counter.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}
Ok(row_id)
}
/// Get a row from a specific table (table-aware API)
///
/// # Arguments
/// * `table_name` - Name of the table
/// * `row_id` - Internal row ID
///
/// # Example
/// ```ignore
/// let row = db.get_table_row("users", row_id)?;
/// ```ignore
pub fn get_table_row(&self, table_name: &str, row_id: RowId) -> Result<Option<Row>> {
ensure_open!(self);
let schema = self.table_registry.get_table(table_name)?;
self.get_table_row_with_schema(table_name, row_id, &schema)
}
/// Get a row using a pre-fetched schema (avoids redundant RwLock acquisition).
pub fn get_table_row_with_schema(
&self,
table_name: &str,
row_id: RowId,
schema: &crate::types::TableSchema,
) -> Result<Option<Row>> {
// Try cache first
if let Some(row_arc) = self.row_cache.get(table_name, row_id) {
if let Some((next_row_id, count, stride)) =
self.row_cache.check_prefetch(table_name, row_id)
{
self.trigger_prefetch(table_name, next_row_id, count, stride);
}
return Ok(Some((*row_arc).clone()));
}
// 🆕 S9: ColSegmentStore cached point lookup — FIRST after row_cache.
// Uses per-segment column decode cache (get_row_cached), so repeated
// lookups (e.g. UPDATE of 500 rows) are O(1) per row after the first
// column decompress. This must precede the columnar_sstables check
// below, which calls the uncached ColumnarSSTable::get_row (decompresses
// the whole column on every call — was 8.7ms/row, the UPDATE bottleneck).
let composite_key = self.make_composite_key(table_name, row_id);
if let Some(store) = self.col_segment_stores.get(table_name) {
if let Some(row) = store.get(composite_key) {
let row_arc = Arc::new(row);
self.row_cache
.put_arc(table_name.to_string(), row_id, Arc::clone(&row_arc));
return Ok(Some(
Arc::try_unwrap(row_arc).unwrap_or_else(|a| (*a).clone()),
));
}
// Not in any segment — key doesn't exist (store is authoritative).
return Ok(None);
}
// Check write buffer for tombstones / updates before consulting SSTable
if let Some(builder_arc) = self.columnar_write_bufs.get(table_name) {
let guard = builder_arc.value().lock();
if let Some(deleted) = guard.check_key(composite_key) {
if deleted {
return Ok(None); // Tombstoned in write buffer
}
// Key exists in buffer but cache missed — row_cache should have caught it.
// Fall through: try SSTable, then LSM.
}
}
// 🚀 Columnar point query: binary search in RowMap, O(log N)
if let Some(col_sst) = self.columnar_sstables.get(table_name) {
let key = self.make_composite_key(table_name, row_id);
if let Some(row) = col_sst.get_row(key, schema.col_types()) {
let row_arc = Arc::new(row);
self.row_cache
.put_arc(table_name.to_string(), row_id, Arc::clone(&row_arc));
return Ok(Some(
Arc::try_unwrap(row_arc).unwrap_or_else(|a| (*a).clone()),
));
}
return Ok(None);
}
// Cache miss - load from LSM
let composite_key = self.make_composite_key(table_name, row_id);
if let Some(value) = self.lsm_engine.get(composite_key)? {
if value.deleted {
return Ok(None);
}
let data: Vec<u8> = match &value.data {
crate::storage::lsm::ValueData::Inline(bytes) => bytes.to_vec(),
crate::storage::lsm::ValueData::Blob(blob_ref) => {
match self.lsm_engine.resolve_blob(blob_ref) {
Ok(data) => data,
Err(e) => {
return Err(StorageError::Serialization(format!(
"Failed to resolve blob for row {}: {}",
row_id, e
)))
}
}
}
};
let col_types = schema.col_types();
let fc = row_format::compute_fixed_count(col_types);
let row: Row = row_format::decode_fast(&data, col_types, fc).map_err(|e| {
StorageError::Serialization(format!("Failed to deserialize row {}: {}", row_id, e))
})?;
let row_arc = Arc::new(row);
self.row_cache
.put_arc(table_name.to_string(), row_id, Arc::clone(&row_arc));
if let Some((next_row_id, count, stride)) =
self.row_cache.check_prefetch(table_name, row_id)
{
self.trigger_prefetch(table_name, next_row_id, count, stride);
}
Ok(Some(
Arc::try_unwrap(row_arc).unwrap_or_else(|a| (*a).clone()),
))
} else {
Ok(None)
}
}
/// Get a row as Arc<Row> — avoids cloning the row data for cache hits.
/// Use when the caller doesn't need to modify the row (PK SELECT fast path).
pub fn get_table_row_arc(
&self,
table_name: &str,
row_id: RowId,
schema: &crate::types::TableSchema,
) -> Result<Option<Arc<Row>>> {
// Fast path: skip prefetch tracking for single-row lookups
if let Some(row_arc) = self.row_cache.get_fast(table_name, row_id) {
if let Some((next_row_id, count, stride)) =
self.row_cache.check_prefetch(table_name, row_id)
{
self.trigger_prefetch(table_name, next_row_id, count, stride);
}
return Ok(Some(row_arc));
}
// 🆕 S9: Check ColSegmentStore FIRST (before LSM) — ColSegmentStore
// tables store data in segments, not in LSM. Without this check,
// FTS/Vector/Column index lookups fail to load matching rows.
let composite_key = self.make_composite_key(table_name, row_id);
if let Some(store) = self.col_segment_stores.get(table_name) {
if let Some(row) = store.get(composite_key) {
let row_arc = Arc::new(row);
self.row_cache
.put_arc(table_name.to_string(), row_id, Arc::clone(&row_arc));
return Ok(Some(row_arc));
}
// Not in any segment — fall through to LSM (some tables have
// data split between ColSegmentStore and LSM).
}
// Cache miss — load from LSM (no prefetch for single-row PK lookup)
if let Some(value) = self.lsm_engine.get(composite_key)? {
if value.deleted {
return Ok(None);
}
let data = match &value.data {
crate::storage::lsm::ValueData::Inline(bytes) => bytes.as_slice(),
crate::storage::lsm::ValueData::Blob(_) => {
return Err(StorageError::InvalidData(
"Blob values not yet supported".into(),
));
}
};
let col_types = schema.col_types();
let fc = row_format::compute_fixed_count(col_types);
let row: Row = row_format::decode_fast(data, col_types, fc).map_err(|e| {
StorageError::Serialization(format!("Failed to deserialize row {}: {}", row_id, e))
})?;
let row_arc = Arc::new(row);
self.row_cache
.put_arc(table_name.to_string(), row_id, Arc::clone(&row_arc));
Ok(Some(row_arc))
} else {
Ok(None)
}
}
/// Read a row with MVCC snapshot isolation. For transactional reads, the version
/// store's `get_visible_version` is consulted to filter out rows that are not yet
/// committed under the given snapshot. Rows that were inserted via the auto-commit
/// path (not through a transaction) have no version-store entry and are always visible.
pub fn get_table_row_arc_with_mvcc(
&self,
table_name: &str,
row_id: RowId,
schema: &crate::types::TableSchema,
snapshot: &crate::txn::Snapshot,
isolation: crate::txn::IsolationLevel,
) -> Result<Option<Arc<Row>>> {
// Check version store first — if this row_id has transactional data,
// version store visibility rules take precedence over LSM.
if let Some(visible) = self
.version_store
.get_visible_version(row_id, snapshot, isolation)?
{
return Ok(Some(Arc::new(visible)));
}
// Check if version store has any entry (even if not visible). If so, the row
// exists transactionally but is hidden by MVCC — don't fall through to LSM.
if self.version_store.versions.get(&row_id).is_some() {
return Ok(None); // Row exists but is not visible under this snapshot
}
// No version store entry — fall through to LSM (auto-commit path)
self.get_table_row_arc(table_name, row_id, schema)
}
/// Update a row in a specific table (table-aware API)
///
/// # Arguments
/// * `table_name` - Name of the table
/// * `row_id` - Internal row ID
/// * `old_row` - Old row data (to avoid re-loading)
/// * `new_row` - New row data
///
/// # Example
/// ```ignore
/// db.update_row_in_table("users", row_id, old_row, vec![Value::Integer(1), Value::Text("Bob".into())])?;
/// ```ignore
pub fn update_row_in_table(
&self,
table_name: &str,
row_id: RowId,
old_row: Row,
new_row: Row,
) -> Result<()> {
ensure_open!(self);
let schema = self.table_registry.get_table(table_name)?;
self.update_row_with_schema_ref(table_name, row_id, &old_row, new_row, &schema)
}
/// Update a row with pre-resolved schema (avoids redundant lookup).
/// Takes ownership of old_row for backwards compatibility.
pub fn update_row_in_table_with_schema(
&self,
table_name: &str,
row_id: RowId,
old_row: Row,
new_row: Row,
schema: &crate::types::TableSchema,
) -> Result<()> {
self.update_row_with_schema_ref(table_name, row_id, &old_row, new_row, schema)
}
/// Core UPDATE implementation — borrows old_row (avoids caller clone).
pub fn update_row_with_schema_ref(
&self,
table_name: &str,
row_id: RowId,
old_row: &Row,
new_row: Row,
schema: &crate::types::TableSchema,
) -> Result<()> {
ensure_open!(self);
// 1. Check PK uniqueness if primary key is being changed
if !schema.is_primary_key_auto_increment() {
if let Some(pk_name) = schema.primary_key() {
if let Some(pk_col) = schema.get_column(pk_name) {
let old_pk = old_row.get(pk_col.position);
let new_pk = new_row.get(pk_col.position);
if old_pk != new_pk {
if let Some(new_val) = new_pk {
if !matches!(new_val, Value::Null) {
let pk_key = crate::database::pk_cache::PkKey::from_value(new_val);
// Check PK cache for existing entry with different row_id
if let Some(lookup) = self.pk_lookup.get(table_name) {
if let Some(existing_rid) = lookup.get_pk(&pk_key) {
if existing_rid != row_id {
return Err(StorageError::InvalidData(format!(
"Duplicate primary key {:?} for table '{}'",
new_val, table_name
)));
}
}
}
}
}
}
}
}
}
// 2. Construct composite key
let composite_key = self.make_composite_key(table_name, row_id);
// 3. Determine partition
let partition = (composite_key % self.num_partitions as u64) as PartitionId;
// 4. Encode rows to raw bytes
let col_types = schema.col_types();
let raw_old = row_format::encode(old_row, col_types).or_else(|_| {
bincode::serialize(&old_row)
.map_err(|e| StorageError::Serialization(format!("Row encode failed: {}", e)))
})?;
let raw_new = row_format::encode(&new_row, col_types).or_else(|_| {
bincode::serialize(&new_row)
.map_err(|e| StorageError::Serialization(format!("Row encode failed: {}", e)))
})?;
// 5. Increment pending counter BEFORE WAL write (checkpoint barrier)
self.increment_pending_updates();
// 6. Write to WAL first (durability) — raw bytes
self.wal
.log_update_raw_ref(table_name, partition, row_id, &raw_old, &raw_new, 0)?;
// 6. Write to columnar buffer (primary storage) + WAL (durability)
let timestamp = self
.write_lsn
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
self.row_cache
.put(table_name.to_string(), row_id, new_row.clone());
// Add new row to columnar buffer (create if first write to this table)
{
// S9: append the updated row to ColSegmentStore so queries see it.
// 🔑 PERF: skip the legacy columnar_write_bufs for ColSegmentStore
// tables (same optimization as INSERT) — halves UPDATE columnar work.
// 🔑 PERF: use append_row_ref (by reference) to avoid Vec<Value> clone.
let store = self.get_or_create_col_segment_store(table_name, schema.col_types())?;
let table_id = self.table_registry.get_table_id(table_name).unwrap_or(0) as u64;
let key = (table_id << 32) | (row_id & 0xFFFFFFFF);
store.append_row_ref(key, timestamp, &new_row)?;
if store.buffered_row_count() >= 100000 {
let _ = store.flush_buffer();
}
// Legacy builder: only for pre-S9 tables without a ColSegmentStore.
if !self.col_segment_stores.contains_key(table_name) {
use dashmap::mapref::entry::Entry;
let builder_arc = match self.columnar_write_bufs.entry(table_name.to_string()) {
Entry::Occupied(o) => o.get().clone(),
Entry::Vacant(v) => {
let indexes_dir = self.path.join("indexes");
std::fs::create_dir_all(&indexes_dir).ok();
let col_sst_path = indexes_dir.join(format!("{}_col.sst", table_name));
let b = Arc::new(parking_lot::Mutex::new(
crate::storage::lsm::columnar::ColumnarSSTableBuilder::new(
col_sst_path,
schema.col_types().to_vec(),
),
));
v.insert(b.clone());
b
}
};
let mut builder = builder_arc.lock();
let _ = builder.add_values(key, timestamp, false, &new_row);
}
}
// 6. Update indexes. Collect failures, then mark ALL stale consistently.
let mut index_errors = Vec::new();
// Reusable index key buffer
let mut index_key_buf = String::with_capacity(table_name.len() + 1 + 16);
for col_def in &schema.columns {
let col_name = &col_def.name;
let old_value = old_row.get(col_def.position);
let new_value = new_row.get(col_def.position);
// Skip unchanged columns
if old_value == new_value {
continue;
}
// 6.1 Column Index — reuse key buffer
{
index_key_buf.clear();
index_key_buf.push_str(table_name);
index_key_buf.push('.');
index_key_buf.push_str(col_name);
}
if let Some(index_ref) = self.column_indexes.get(&index_key_buf) {
let index = index_ref.value();
let old_is_null = old_value.is_none() || matches!(old_value, Some(Value::Null));
let new_is_null = new_value.is_none() || matches!(new_value, Some(Value::Null));
if !old_is_null && !new_is_null {
if let (Some(old_val), Some(new_val)) = (old_value, new_value) {
if let Err(_e) = index.update(old_val, new_val, row_id) {
debug_log!(
"[update_row] Failed to update column index '{}': {}",
col_name,
_e
);
index_errors.push(index_key_buf.clone());
}
}
} else if !old_is_null && new_is_null {
if let Some(old_val) = old_value {
if let Err(_e) = index.delete(old_val, row_id) {
debug_log!(
"[update_row] Failed to delete column index '{}': {}",
col_name,
_e
);
index_errors.push(index_key_buf.clone());
}
}
} else if old_is_null && !new_is_null {
if let Some(new_val) = new_value {
if let Err(_e) = index.insert(new_val, row_id) {
debug_log!(
"[update_row] Failed to insert column index '{}': {}",
col_name,
_e
);
index_errors.push(index_key_buf.clone());
}
}
}
// NULL -> NULL: no index change needed
}
// 6.2 Vector Index
if let crate::types::ColumnType::Tensor(_dim) = col_def.col_type {
if let Some(index_name) = self.index_registry.find_by_column(
table_name,
col_name,
crate::database::index_metadata::IndexType::Vector,
) {
let mut failed = false;
if let Err(_e) = self.delete_vector(row_id, &index_name) {
debug_log!(
"[update_row] Failed to delete old vector '{}': {}",
index_name,
_e
);
failed = true;
}
if let Some(new_vec) = new_value.and_then(|v| match v {
crate::types::Value::Vector(vec) => Some(vec.as_slice().to_vec()),
crate::types::Value::Tensor(tensor) => Some(tensor.to_f32()),
_ => None,
}) {
if let Err(_e) = self.update_vector(row_id, &index_name, &new_vec) {
debug_log!(
"[update_row] Failed to update vector index '{}': {}",
index_name,
_e
);
failed = true;
}
}
if failed {
index_errors.push(index_name.clone());
}
}
}
// 6.3 Text Index
if matches!(col_def.col_type, crate::types::ColumnType::Text) {
if let Some(index_name) = self.index_registry.find_by_column(
table_name,
col_name,
crate::database::index_metadata::IndexType::Text,
) {
if let (
Some(crate::types::Value::Text(old_text)),
Some(crate::types::Value::Text(new_text)),
) = (old_value, new_value)
{
if let Err(_e) = self.update_text(row_id, &index_name, old_text, new_text) {
debug_log!(
"[update_row] Failed to update text index '{}': {}",
index_name,
_e
);
index_errors.push(index_name.clone());
}
}
}
}
// 6.4 i-Octree Index (3D point cloud)
if matches!(col_def.col_type, crate::types::ColumnType::Spatial) {
if let Some(octree_name) = self.index_registry.find_by_column(
table_name,
col_name,
crate::database::index_metadata::IndexType::Octree,
) {
let mut failed = false;
if let Err(_e) = self.delete_ioctree_point(row_id, &octree_name) {
debug_log!(
"[update_row] Failed to delete old ioctree point '{}': {}",
octree_name,
_e
);
failed = true;
}
if let Some(crate::types::Value::Spatial(new_geom)) = new_value {
if let Err(_e) = self.insert_ioctree_point(row_id, &octree_name, new_geom) {
debug_log!(
"[update_row] Failed to update ioctree index '{}': {}",
octree_name,
_e
);
failed = true;
}
}
if failed {
index_errors.push(octree_name.clone());
}
}
}
}
// 7. Update PK lookup cache if primary key value changed
if let Some(pk_name) = schema.primary_key() {
if !schema.is_primary_key_auto_increment() {
if let Some(pk_col) = schema.get_column(pk_name) {
let old_pk = old_row.get(pk_col.position);
let new_pk = new_row.get(pk_col.position);
if old_pk != new_pk {
if let Some(pk_lookup) = self.pk_lookup.get(table_name) {
if let Some(old_val) = old_pk {
let old_key = crate::database::pk_cache::PkKey::from_value(old_val);
pk_lookup.remove_pk(&old_key);
}
if let Some(new_val) = new_pk {
let new_key = crate::database::pk_cache::PkKey::from_value(new_val);
pk_lookup.insert(new_key, row_id);
}
}
}
}
}
}
// If any index update failed, mark ALL indexes for this table stale
if !index_errors.is_empty() {
debug_log!(
"[update_row] {} index updates failed for table '{}', marking all stale",
index_errors.len(),
table_name
);
for meta in self.index_registry.list_table_indexes(table_name) {
self.index_registry.mark_stale(&meta.name);
}
}
Ok(())
}
/// Delete a row from a specific table (table-aware API)
///
/// # Arguments
/// * `table_name` - Name of the table
/// * `row_id` - Internal row ID
/// * `old_row` - Old row data (to avoid re-loading)
///
/// # Example
/// ```ignore
/// db.delete_row_from_table("users", row_id, old_row)?;
/// ```ignore
pub fn delete_row_from_table(
&self,
table_name: &str,
row_id: RowId,
old_row: Row,
) -> Result<()> {
ensure_open!(self);
// 1. Get schema (old_row is now passed in to avoid re-loading)
let schema = self.table_registry.get_table(table_name)?;
// 2. Construct composite key
let composite_key = self.make_composite_key(table_name, row_id);
// 3. Determine partition
let partition = (composite_key % self.num_partitions as u64) as PartitionId;
// 4. Compute timestamp (used by both WAL and LSM)
let timestamp = self
.write_lsn
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
// 5. Write to WAL first (durability guarantee)
// WAL must be written BEFORE any mutation so that a crash at any
// point below can be recovered correctly.
// 5. Write to WAL first (durability guarantee) — raw bytes
let col_types = schema.col_types();
let raw_old = row_format::encode(&old_row, col_types).or_else(|_| {
bincode::serialize(&old_row)
.map_err(|e| StorageError::Serialization(format!("Row encode failed: {}", e)))
})?;
self.increment_pending_updates();
self.wal
.log_delete_raw(table_name, partition, composite_key, raw_old, timestamp, 0)?;
// 🚀 Columnar tombstone is the source of truth. LSM delete removed.
// Columnar tombstone below marks the row deleted in all reads.
{
use dashmap::mapref::entry::Entry;
let builder_arc = match self.columnar_write_bufs.entry(table_name.to_string()) {
Entry::Occupied(o) => o.get().clone(),
Entry::Vacant(v) => {
let indexes_dir = self.path.join("indexes");
std::fs::create_dir_all(&indexes_dir).ok();
let col_sst_path = indexes_dir.join(format!("{}_col.sst", table_name));
let b = Arc::new(parking_lot::Mutex::new(
crate::storage::lsm::columnar::ColumnarSSTableBuilder::new(
col_sst_path,
schema.col_types().to_vec(),
),
));
v.insert(b.clone());
b
}
};
let mut builder = builder_arc.lock();
let table_id = self.table_registry.get_table_id(table_name).unwrap_or(0) as u64;
let key = (table_id << 32) | (row_id & 0xFFFFFFFF);
let _ = builder.add_values(key, timestamp, true, &old_row);
}
// 🆕 S9: write tombstone to ColSegmentStore so multi-segment scans
// see the deletion (legacy columnar_write_bufs tombstone is not read
// by ColSegmentStore scan paths).
//
// Flush the tombstone to its own segment immediately. This guarantees
// ALL read paths observe the deletion — including materialize_as_streaming
// (the LSM/SELECT * path), aggregate scans, and ColSegmentStore scans.
// 🔑 PERF: single flush (was two). The tombstone is appended to the
// write buffer after the existing rows, so it has a higher index (newer
// version) within the same segment. Newest-version-wins scans and the
// binary-search get() both see the tombstone correctly. The old code
// flushed twice (before + after tombstone) — two segment writes + two
// manifest fsyncs per DELETE. Now we flush existing data first (so the
// tombstone segment is strictly newer), then append the tombstone; the
// tombstone flushes lazily on the next query's flush_buffer call.
if self.col_segment_stores.contains_key(table_name) {
if let Some(store) = self.col_segment_stores.get(table_name) {
let _ = store.flush_buffer();
let table_id = self.table_registry.get_table_id(table_name).unwrap_or(0) as u64;
let key = (table_id << 32) | (row_id & 0xFFFFFFFF);
store.append_tombstone(key, timestamp)?;
// No second flush — tombstone stays in buffer, flushed lazily
// by the next query path (flush_buffer at scan start).
}
}
// Invalidate cache AFTER LSM write — single invalidation
self.row_cache.invalidate(table_name, row_id);
// 7.1 Decrement row count for COUNT(*) fast path
// Use saturating subtract via fetch_update to avoid both underflow
// AND the stuck-at-zero bug from CAS-based guard loops.
if let Some(counter) = self.table_row_count.get(table_name) {
let _ = counter.fetch_update(
std::sync::atomic::Ordering::AcqRel,
std::sync::atomic::Ordering::Acquire,
|c| Some(c.saturating_sub(1)),
);
}
// 7.2 Remove from PK lookup cache (prevents stale lookups)
if let Some(pk_name) = schema.primary_key() {
if !schema.is_primary_key_auto_increment() {
if let Some(pk_col) = schema.get_column(pk_name) {
if let Some(pk_value) = old_row.get(pk_col.position) {
if let Some(lookup) = self.pk_lookup.get(table_name) {
lookup
.remove_pk(&crate::database::pk_cache::PkKey::from_value(pk_value));
}
}
}
}
}
// 8. Update indexes (after data is durable).
// If an index deletion fails, the index is marked stale and can be
// rebuilt later. Since indexes are derived data, this is safe.
// DashMap direct lookup for indexed columns
let prefix_len = table_name.len() + 1;
for col_def in &schema.columns {
let col_name = &col_def.name;
let col_value = old_row.get(col_def.position);
let Some(col_value) = col_value else {
continue;
};
// Column Index — single DashMap lookup
let mut col_index_key = String::with_capacity(prefix_len + col_name.len());
col_index_key.push_str(table_name);
col_index_key.push('.');
col_index_key.push_str(col_name);
if let Some(index_ref) = self.column_indexes.get(&col_index_key) {
if let Err(_e) = index_ref.value().delete(col_value, row_id) {
debug_log!(
"[delete_row] Failed to delete from column index '{}': {}",
col_name,
_e
);
self.index_registry.mark_stale(&col_index_key);
}
}
// Vector Index
if let crate::types::ColumnType::Tensor(_dim) = col_def.col_type {
if let Some(index_name) = self.index_registry.find_by_column(
table_name,
col_name,
crate::database::index_metadata::IndexType::Vector,
) {
if let Err(_e) = self.delete_vector(row_id, &index_name) {
debug_log!(
"[delete_row] Failed to delete from vector index '{}': {}",
index_name,
_e
);
self.index_registry.mark_stale(&index_name);
}
}
}
// Text Index
if matches!(col_def.col_type, crate::types::ColumnType::Text) {
if let Some(index_name) = self.index_registry.find_by_column(
table_name,
col_name,
crate::database::index_metadata::IndexType::Text,
) {
if let crate::types::Value::Text(text) = col_value {
if let Err(_e) = self.delete_text(row_id, &index_name, text) {
debug_log!(
"[delete_row] Failed to delete from text index '{}': {}",
index_name,
_e
);
self.index_registry.mark_stale(&index_name);
}
}
}
}
// i-Octree Index (3D point cloud)
if matches!(col_def.col_type, crate::types::ColumnType::Spatial) {
if let Some(octree_name) = self.index_registry.find_by_column(
table_name,
col_name,
crate::database::index_metadata::IndexType::Octree,
) {
if let Err(_e) = self.delete_ioctree_point(row_id, &octree_name) {
debug_log!(
"[delete_row] Failed to delete from ioctree index '{}': {}",
octree_name,
_e
);
self.index_registry.mark_stale(&octree_name);
}
}
}
}
Ok(())
}
/// Scan all rows in a specific table
///
/// # Arguments
/// * `table_name` - Name of the table
///
/// # Example
/// ```ignore
/// let rows = db.scan_table_rows("users")?;
/// ```ignore
pub fn scan_table_rows(&self, table_name: &str) -> Result<Vec<(RowId, Row)>> {
ensure_open!(self);
let schema = self.table_registry.get_table(table_name)?;
let col_types = schema.col_types();
let table_prefix = self.compute_table_prefix(table_name);
let start_key = table_prefix << 32;
let end_key = (table_prefix + 1) << 32;
// Use streaming scan to avoid materializing full BTreeMap (saves ~420 MB for 300K rows)
let lsm_iter = self.lsm_engine.scan_range_streaming(start_key, end_key)?;
let mut result = Vec::new();
for item in lsm_iter {
let (composite_key, value) = item?;
if value.deleted {
continue;
}
let row_id = (composite_key & 0xFFFFFFFF) as RowId;
let data = match &value.data {
crate::storage::lsm::ValueData::Inline(bytes) => bytes.as_slice(),
crate::storage::lsm::ValueData::Blob(_) => {
return Err(StorageError::InvalidData(
"Blob references should be resolved by LSM engine".into(),
));
}
};
// Deserialize row
let row: Row = deserialize_row(data, col_types)?;
result.push((row_id, row));
}
Ok(result)
}
/// 🚀 流式扫描表行(批量迭代器,内存友好)
///
/// 返回一个迭代器,每次产出一批行数据(默认 1000 行),而不是一次性加载全部。
///
/// # 性能对比
/// - `scan_table_rows()`: 30 万行 × 1.4 KB = 420 MB 内存峰值 🔴
/// - `scan_table_rows_batched()`: 1000 行 × 1.4 KB = 1.4 MB 内存峰值 ✅
///
/// # 使用场景
/// - COUNT(*) - 只需遍历不需要保存全部数据
/// - WHERE 过滤 - 逐批过滤,只保留匹配的行
/// - UPDATE/DELETE - 逐批处理,减少内存占用
///
/// # 示例
/// ```ignore
/// let iter = db.scan_table_rows_batched("users", 1000)?;
/// let mut count = 0;
/// for batch_result in iter {
/// let batch = batch_result?;
/// count += batch.len();
/// }
/// println!("Total rows: {}", count);
/// ```
pub fn scan_table_rows_batched(
&self,
table_name: &str,
batch_size: usize,
) -> Result<TableRowBatchedIterator> {
ensure_open!(self);
// Get table schema first (validates table exists)
let schema = self.table_registry.get_table(table_name)?;
// Use LSM batched scan
let table_prefix = self.compute_table_prefix(table_name);
let start_key = table_prefix << 32;
let end_key = (table_prefix + 1) << 32;
let lsm_iter = self
.lsm_engine
.scan_range_batched(start_key, end_key, batch_size)?;
Ok(TableRowBatchedIterator {
lsm_iter,
_table_name: table_name.to_string(),
col_types: Some(schema.col_types().to_vec()),
fixed_count: crate::storage::row_format::compute_fixed_count(schema.col_types()),
})
}
/// 🚀 真正的流式扫描表行(O(1) 内存占用)
///
/// 使用多路归并迭代器,逐个返回行数据,**真正的流式处理**,不预先加载任何数据到内存。
///
/// # 内存对比
/// - `scan_table_rows()`: 30 万行 × 1.4 KB = 420 MB 🔴
/// - `scan_table_rows_batched()`: 仍需合并所有数据 = 420 MB 🔴
/// - `scan_table_rows_streaming()`: 13 个迭代器 × 1.5 KB = 20 KB ✅
/// - **节省 99.995% 内存**
///
/// # 使用场景
/// - COUNT(*) - 只需遍历不需要保存数据
/// - WHERE 过滤 - 逐行过滤,只保留匹配的行
/// - 大表查询 - 避免内存溢出
///
/// # 示例
/// ```ignore
/// let iter = db.scan_table_rows_streaming("users")?;
/// let mut count = 0;
/// for result in iter {
/// let (row_id, row) = result?;
/// count += 1;
/// }
/// println!("Total rows: {}", count);
/// ```
pub fn scan_table_rows_streaming(&self, table_name: &str) -> Result<TableRowStreamingIterator> {
ensure_open!(self);
let schema = self.table_registry.get_table(table_name)?;
let col_types = schema.col_types();
// Columnar-backed scan: if the table's data lives in a columnar SSTable
// (rather than the LSM), decode column arrays and synthesize rows.
// Falling back to the LSM scan here would yield empty/stale results.
// 🆕 S9: ColSegmentStore tables — flush buffer (cheap) then sync to
// columnar_sstables (no compaction). 🔑 PERF: the old code ran up to 5
// force_compact_all() passes PER TABLE PER JOIN CALL — rewriting the
// entire table 10x for a 2-table join. sync_col_segment_to_sstables
// updates columnar_sstables from the latest segment without compaction.
if self.col_segment_stores.contains_key(table_name) {
// Flush buffered writes so they're visible, then sync to the
// legacy columnar_sstables map (reads the latest segment, no rewrite).
self.sync_col_segment_to_sstables(table_name);
}
if let Some(col_sst) = self.columnar_sstables.get(table_name).as_deref() {
let num_cols = col_types.len();
let mut segments: Vec<ColumnarSegment> = Vec::with_capacity(num_cols);
for col_idx in 0..num_cols {
segments.push(build_column_segment(col_sst, col_idx, col_sst.num_rows)?);
}
let col_names: Vec<String> = schema.columns.iter().map(|c| c.name.clone()).collect();
return Ok(TableRowStreamingIterator {
inner: TableRowStreamingInner::Columnar {
row_map: col_sst.row_map.clone(),
segments,
col_names,
col_types: col_types.to_vec(),
current_idx: 0,
num_rows: col_sst.num_rows,
},
});
}
// Use LSM streaming scan
let table_prefix = self.compute_table_prefix(table_name);
let start_key = table_prefix << 32;
let end_key = (table_prefix + 1) << 32;
let lsm_iter = self.lsm_engine.scan_range_streaming(start_key, end_key)?;
let use_raw = lsm_iter.has_raw_sst();
// Detect whether any column is nullable
let has_nullable = schema.columns.iter().any(|c| c.nullable);
Ok(TableRowStreamingIterator {
inner: TableRowStreamingInner::Lsm {
lsm_iter,
decode_ctx: {
let mut ctx = crate::storage::row_format::SchemaDecodeContext::new(col_types);
ctx.trust_utf8 = true; // Data encoded by MoteDB, safe to skip UTF-8 validation
ctx.skip_magic_check = true; // All data from our own encode()
if !has_nullable {
ctx.has_nullable_columns = false;
}
Some(ctx)
},
use_raw,
},
})
}
/// Raw byte streaming scan — returns (row_id, raw_data_bytes) without decoding.
/// Caller can use row_format::get_column() for partial decode.
pub fn scan_table_raw_streaming(&self, table_name: &str) -> Result<TableRawStreamingIterator> {
ensure_open!(self);
let table_prefix = self.compute_table_prefix(table_name);
let start_key = table_prefix << 32;
let end_key = (table_prefix + 1) << 32;
let lsm_iter = self.lsm_engine.scan_range_streaming(start_key, end_key)?;
Ok(TableRawStreamingIterator { lsm_iter })
}
/// Zero-copy decode streaming scan — yields (row_id) and decodes each row
/// directly into a caller-provided Vec via `decode_row_into`.
///
/// Uses `ValueBytes` (Arc-shared block data) instead of `to_vec()`,
/// eliminating the per-row memcpy that `scan_table_raw_streaming` performs.
pub fn scan_table_decode_streaming(
&self,
table_name: &str,
col_types: &[crate::types::ColumnType],
) -> Result<TableDecodeStreamingIterator> {
ensure_open!(self);
let table_prefix = self.compute_table_prefix(table_name);
let start_key = table_prefix << 32;
let end_key = (table_prefix + 1) << 32;
let lsm_iter = self.lsm_engine.scan_range_streaming(start_key, end_key)?;
let use_raw = lsm_iter.has_raw_sst();
let mut ctx = crate::storage::row_format::SchemaDecodeContext::new(col_types);
ctx.trust_utf8 = true;
ctx.skip_magic_check = true; // All data from our own encode()
Ok(TableDecodeStreamingIterator {
lsm_iter,
decode_ctx: ctx,
use_raw,
})
}
/// Columnar streaming scan — decodes rows into `ColumnArray` slices instead
/// of `Vec<Value>`. Uses O(columns) allocations instead of O(rows), saving
/// ~50% memory and improving cache locality for aggregate queries.
///
/// Returns a `ColumnarRowSet` containing all rows decoded into column arrays.
/// The caller can then compute COUNT/SUM/MIN/MAX directly from typed arrays.
pub fn scan_table_columnar(
&self,
table_name: &str,
col_types: &[crate::types::ColumnType],
column_names: Vec<String>,
) -> Result<crate::storage::row_format::ColumnarRowSet> {
ensure_open!(self);
let table_prefix = self.compute_table_prefix(table_name);
let start_key = table_prefix << 32;
let end_key = (table_prefix + 1) << 32;
let mut lsm_iter = self.lsm_engine.scan_range_streaming(start_key, end_key)?;
let mut ctx = crate::storage::row_format::SchemaDecodeContext::new(col_types);
ctx.trust_utf8 = true;
ctx.skip_magic_check = true;
// Detect nullable columns
let schema = self.table_registry.get_table(table_name)?;
let has_nullable = schema.columns.iter().any(|c| c.nullable);
if !has_nullable {
ctx.has_nullable_columns = false;
}
let mut result = crate::storage::row_format::ColumnarRowSet::new(column_names, col_types);
let mut col_data: Vec<crate::storage::row_format::ColumnArray> = col_types
.iter()
.map(|ct| match ct {
crate::types::ColumnType::Integer => {
crate::storage::row_format::ColumnArray::Integers(Vec::new())
}
crate::types::ColumnType::Float => {
crate::storage::row_format::ColumnArray::Floats(Vec::new())
}
crate::types::ColumnType::Text => {
crate::storage::row_format::ColumnArray::Texts(Vec::new())
}
crate::types::ColumnType::Timestamp => {
crate::storage::row_format::ColumnArray::Timestamps(Vec::new())
}
crate::types::ColumnType::Boolean => {
crate::storage::row_format::ColumnArray::Bools(Vec::new())
}
_ => crate::storage::row_format::ColumnArray::Values(Vec::new()),
})
.collect();
// Pre-allocate 300K capacity per column
let size_hint = self
.fast_row_count(table_name)
.map(|c| c as usize)
.unwrap_or(1024);
for arr in &mut col_data {
match arr {
crate::storage::row_format::ColumnArray::Integers(v) => v.reserve(size_hint),
crate::storage::row_format::ColumnArray::Floats(v) => v.reserve(size_hint),
crate::storage::row_format::ColumnArray::Texts(v) => v.reserve(size_hint),
crate::storage::row_format::ColumnArray::Timestamps(v) => v.reserve(size_hint),
crate::storage::row_format::ColumnArray::Bools(v) => v.reserve(size_hint),
crate::storage::row_format::ColumnArray::Values(v) => v.reserve(size_hint),
}
}
let mut count = 0usize;
loop {
match lsm_iter.next() {
Some(Ok((_composite_key, value))) => {
if value.deleted {
continue;
}
let data = match &value.data {
crate::storage::lsm::ValueData::Inline(bytes) => bytes.as_slice(),
_ => continue,
};
if let Err(_e) = crate::storage::row_format::decode_row_into_columns(
&ctx,
data,
&mut col_data,
) {
continue; // skip malformed rows
}
count += 1;
}
Some(Err(_)) => break,
None => break,
}
}
result.data = col_data;
result.num_rows = count;
Ok(result)
}
/// Returns true if this table stores its data in a columnar SSTable
/// (rather than the LSM row store). Used by the query layer to pick the
/// correct scan path — scanning the LSM for a columnar table yields empty
/// results because the data lives in `columnar_sstables`, not the LSM.
pub fn is_columnar_table(&self, table_name: &str) -> bool {
self.columnar_sstables.contains_key(table_name)
|| self.columnar_write_bufs.contains_key(table_name)
|| self.col_segment_stores.contains_key(table_name)
}
/// Get or create the multi-segment ColSegmentStore for a table.
/// This is the new append-only path; coexists with the legacy
/// single-SSTable fields during migration (S6-S9).
pub fn get_or_create_col_segment_store(
&self,
table_name: &str,
col_types: &[crate::types::ColumnType],
) -> Result<Arc<crate::storage::col_segment::ColSegmentStore>> {
// 🔑 Atomic entry-based creation (fixes concurrent INSERT data loss).
// 🔑 PERF: takes &[ColumnType] so callers don't pay a to_vec() when the
// store already exists (the common case — every INSERT after the first).
use dashmap::mapref::entry::Entry;
match self.col_segment_stores.entry(table_name.to_string()) {
Entry::Occupied(o) => Ok(o.get().clone()),
Entry::Vacant(v) => {
let store = crate::storage::col_segment::ColSegmentStore::create(
&self.path,
table_name,
col_types.to_vec(),
)?;
v.insert(store.clone());
Ok(store)
}
}
}
/// Whether this table has an active ColSegmentStore (new multi-segment path).
pub fn has_col_segment_store(&self, table_name: &str) -> bool {
self.col_segment_stores.contains_key(table_name)
}
/// 🆕 S9: sync a ColSegmentStore table's latest single segment (after flush
/// + compaction) into the legacy `columnar_sstables` map. Legacy aggregate /
/// GROUP BY / scan paths read `columnar_sstables` directly; this shares the
/// same Arc<ColumnarSSTable> so they observe the data without cloning.
/// Idempotent: safe to call before any query that uses legacy columnar reads.
pub fn sync_col_segment_to_sstables(&self, table_name: &str) {
if let Some(store) = self.col_segment_stores.get(table_name) {
let _ = store.flush_buffer();
// 🔑 PERF: only compact + release pages when there are 2+ segments.
// The old code always ran clear_cache()+release_pages() even for a
// single segment — evicting mmap pages that the next query must
// re-fault. For the common single-segment case, just update the
// columnar_sstables pointer (no compaction, no page eviction).
let seg_count = store.segment_count();
if seg_count >= 2 {
let mut compactions = 0;
while store.segment_count() >= 2 {
let _ = store.force_compact_all();
compactions += 1;
if compactions > 5 {
break;
}
}
// Release mmap pages only after actual compaction.
for seg in store.segments_snapshot() {
seg.clear_cache();
seg.release_pages();
}
}
if let Some(sst) = store.latest_segment_sst() {
self.columnar_sstables.insert(table_name.to_string(), sst);
}
}
}
/// Finalize unflushed columnar write buffer for a table.
/// Converts accumulated INSERT data to a columnar SSTable file.
/// Safe: only removes from write buffer AFTER successful finalization.
pub fn finalize_columnar_buffer(&self, table_name: &str) {
// S9: for tables on the ColSegmentStore path, just flush the store's
// in-memory buffer (cheap delta write). The legacy single-SSTable merge
// below is skipped — it was the full-table-rewrite regression source.
if self.col_segment_stores.contains_key(table_name) {
if let Some(store) = self.col_segment_stores.get(table_name) {
let _ = store.flush_buffer();
}
return;
}
if let Some(builder_arc) = self.columnar_write_bufs.get(table_name) {
let (path, num_rows) = {
let guard = builder_arc.value().lock();
(guard.path.clone(), guard.num_rows)
};
if num_rows == 0 {
return;
}
// MERGE: read existing SSTable rows into the builder so finalize
// produces a complete file, not just the buffer's delta.
// Buffer entries override SSTable entries (higher timestamp = newer).
if let Some(old_sst) = self.columnar_sstables.get(table_name) {
let col_types: Vec<crate::types::ColumnType> = old_sst
.column_tags
.iter()
.map(|t| t.to_column_type())
.collect();
let mut guard = builder_arc.value().lock();
for i in 0..old_sst.num_rows {
if old_sst.row_map.is_deleted(i) {
continue;
}
let k = old_sst.row_map.key(i);
// Skip if buffer already has a newer entry for this key
if guard.check_key(k).is_some() {
continue;
}
let ts = old_sst.row_map.timestamp(i);
if let Some(row) = old_sst.get_row(k, &col_types) {
let _ = guard.add_values(k, ts, false, &row);
}
}
}
let result = builder_arc.value().lock().finish_and_reset();
match result {
Ok(()) => {
if let Ok(col_sst) = crate::storage::lsm::columnar::ColumnarSSTable::open(&path)
{
self.columnar_sstables
.insert(table_name.to_string(), Arc::new(col_sst));
}
}
Err(e) => {
debug_log!(
"[columnar] Finalize failed for '{}': {:?} — data preserved",
table_name,
e
);
}
}
}
}
/// Scan from a columnar SSTable, yielding rows one at a time via iterator.
/// Much faster than row-based scan + uses O(columns) memory instead of O(rows).
pub fn scan_columnar_sstable_streaming(
&self,
table_name: &str,
col_types: &[crate::types::ColumnType],
) -> Result<ColumnarScanIterator> {
// Finalize with merge: combines write buffer + existing SSTable.
// Safe now: merge reads old SSTable rows before overwriting.
self.finalize_columnar_buffer(table_name);
let col_sst = match self.columnar_sstables.get(table_name) {
Some(sst) => sst.clone(),
None => {
return Err(StorageError::InvalidData(format!(
"No columnar SSTable for table '{}'",
table_name
)))
}
};
let num_cols = col_types.len();
let mut segments: Vec<ColumnarSegment> = Vec::with_capacity(num_cols);
for col_idx in 0..num_cols {
segments.push(build_column_segment(&col_sst, col_idx, col_sst.num_rows)?);
}
Ok(ColumnarScanIterator {
row_map: col_sst.row_map.clone(),
segments,
col_types: col_types.to_vec(),
current_idx: 0,
num_rows: col_sst.num_rows,
match_filter: None,
})
}
/// Streaming columnar scan with column projection — only reads the specified
/// column positions. For SELECT id, amount, only 2/4 segments are loaded.
pub fn scan_columnar_sstable_projection(
&self,
table_name: &str,
col_types: &[crate::types::ColumnType],
col_positions: &[usize],
) -> Result<ColumnarScanIterator> {
let col_sst = match self.columnar_sstables.get(table_name) {
Some(sst) => sst.clone(),
None => {
return Err(StorageError::InvalidData(format!(
"No columnar SSTable for table '{}'",
table_name
)))
}
};
// Build mapping: output position → segment index in SSTable
let mut segments: Vec<(usize, ColumnarSegment)> = Vec::with_capacity(col_positions.len());
for &col_idx in col_positions {
let seg = if col_sst.column_tags[col_idx].is_fixed() {
ColumnarSegment::Fixed(col_sst.read_fixed_i64(col_idx)?)
} else {
ColumnarSegment::Text(col_sst.read_text(col_idx)?)
};
segments.push((col_idx, seg));
}
Ok(ColumnarScanIterator {
row_map: col_sst.row_map.clone(),
segments: segments.into_iter().map(|(_, s)| s).collect(),
col_types: col_positions
.iter()
.map(|&i| col_types[i].clone())
.collect(),
current_idx: 0,
num_rows: col_sst.num_rows,
match_filter: None,
})
}
/// Streaming columnar scan with equality filter on one column.
/// Only rows matching filter_col = filter_value are yielded.
/// For WHERE region = 'US' (100K/300K), this saves decoding 200K rows.
pub fn scan_columnar_sstable_filtered(
&self,
table_name: &str,
col_types: &[crate::types::ColumnType],
filter_col: usize,
filter_value: &crate::types::Value,
) -> Result<ColumnarScanIterator> {
let col_sst = match self.columnar_sstables.get(table_name) {
Some(sst) => sst.clone(),
None => return Err(StorageError::InvalidData("No columnar SSTable".into())),
};
let num_cols = col_types.len();
let mut segments: Vec<ColumnarSegment> = Vec::with_capacity(num_cols);
for col_idx in 0..num_cols {
segments.push(build_column_segment(&col_sst, col_idx, col_sst.num_rows)?);
}
// Find matching row indices by scanning the filter column
let mut match_indices: Vec<usize> = Vec::new();
for row_idx in 0..col_sst.num_rows {
if col_sst.row_map.is_deleted(row_idx) {
continue;
}
let matches = match &segments[filter_col] {
ColumnarSegment::Fixed(f) => match filter_value {
crate::types::Value::Integer(iv) => f.get_i64(row_idx) == Some(*iv),
crate::types::Value::Float(fv) => {
(f.get_f64(row_idx).unwrap_or(f64::NAN) - fv).abs() < f64::EPSILON
}
_ => false,
},
ColumnarSegment::Text(t) => match filter_value {
crate::types::Value::Text(tv) => t.get_str(row_idx) == Some(tv.as_str()),
_ => false,
},
// Vector/Spatial columns aren't supported as filter columns
// here; the caller routes such queries through a different path.
ColumnarSegment::Vector(_) | ColumnarSegment::Spatial(_) => false,
};
if matches {
match_indices.push(row_idx);
}
}
Ok(ColumnarScanIterator {
row_map: col_sst.row_map.clone(),
segments,
col_types: col_types.to_vec(),
current_idx: 0,
num_rows: col_sst.num_rows,
match_filter: Some(match_indices),
})
}
/// Streaming columnar scan with text prefix filter (LIKE 'prefix%').
/// Only rows where the text column starts with `prefix` are yielded.
pub fn scan_columnar_sstable_prefix(
&self,
table_name: &str,
col_types: &[crate::types::ColumnType],
filter_col: usize,
prefix: &str,
) -> Result<ColumnarScanIterator> {
let col_sst = match self.columnar_sstables.get(table_name) {
Some(sst) => sst.clone(),
None => return Err(StorageError::InvalidData("No columnar SSTable".into())),
};
let num_cols = col_types.len();
let mut segments: Vec<ColumnarSegment> = Vec::with_capacity(num_cols);
for col_idx in 0..num_cols {
segments.push(build_column_segment(&col_sst, col_idx, col_sst.num_rows)?);
}
// Find rows where text column starts with prefix
let mut match_indices: Vec<usize> = Vec::new();
if let ColumnarSegment::Text(ref text_seg) = segments[filter_col] {
for row_idx in 0..col_sst.num_rows {
if col_sst.row_map.is_deleted(row_idx) {
continue;
}
if let Some(s) = text_seg.get_str(row_idx) {
if s.starts_with(prefix) {
match_indices.push(row_idx);
}
}
}
}
Ok(ColumnarScanIterator {
row_map: col_sst.row_map.clone(),
segments,
col_types: col_types.to_vec(),
current_idx: 0,
num_rows: col_sst.num_rows,
match_filter: Some(match_indices),
})
}
/// Columnar Top-K: find indices of top K rows by a column value.
/// Returns (row_indices, values) for the top K, without materializing rows.
pub fn scan_columnar_sstable_topk(
&self,
table_name: &str,
sort_col: usize,
k: usize,
ascending: bool,
) -> Result<(Vec<usize>, Vec<crate::types::Value>)> {
let col_sst = match self.columnar_sstables.get(table_name) {
Some(sst) => sst.clone(),
None => return Err(StorageError::InvalidData("No columnar SSTable".into())),
};
use std::cmp::Reverse;
use std::collections::BinaryHeap;
if col_sst.column_tags[sort_col].is_fixed() {
let seg = col_sst.read_fixed_i64(sort_col)?;
if ascending {
let mut heap: BinaryHeap<(OrderedF64, usize)> = BinaryHeap::with_capacity(k + 1);
for i in 0..col_sst.num_rows {
if col_sst.row_map.is_deleted(i) {
continue;
}
if let Some(v) = seg.get_f64(i) {
heap.push((OrderedF64(v), i));
if heap.len() > k {
heap.pop();
}
}
}
let mut res: Vec<(OrderedF64, usize)> = heap.into_vec();
res.sort_by_key(|(v, _)| *v);
res.truncate(k);
let indices: Vec<usize> = res.iter().map(|(_, i)| *i).collect();
let vals: Vec<crate::types::Value> = res
.iter()
.map(|(v, _)| crate::types::Value::Float(v.0))
.collect();
Ok((indices, vals))
} else {
let mut heap: BinaryHeap<Reverse<(OrderedF64, usize)>> =
BinaryHeap::with_capacity(k + 1);
for i in 0..col_sst.num_rows {
if col_sst.row_map.is_deleted(i) {
continue;
}
if let Some(v) = seg.get_f64(i) {
heap.push(Reverse((OrderedF64(v), i)));
if heap.len() > k {
heap.pop();
}
}
}
let mut res: Vec<Reverse<(OrderedF64, usize)>> = heap.into_vec();
res.sort_by_key(|r| std::cmp::Reverse(r.0 .0));
res.truncate(k);
let indices: Vec<usize> = res.iter().map(|r| r.0 .1).collect();
let vals: Vec<crate::types::Value> = res
.iter()
.map(|r| crate::types::Value::Float(r.0 .0 .0))
.collect();
Ok((indices, vals))
}
} else {
// Text sort column
let seg = col_sst.read_text(sort_col)?;
if ascending {
let mut heap: BinaryHeap<(String, usize)> = BinaryHeap::with_capacity(k + 1);
for i in 0..col_sst.num_rows {
if col_sst.row_map.is_deleted(i) {
continue;
}
if let Some(s) = seg.get_str(i) {
heap.push((s.to_string(), i));
if heap.len() > k {
heap.pop();
}
}
}
let mut res: Vec<(String, usize)> = heap.into_vec();
res.sort_by(|a, b| a.0.cmp(&b.0));
res.truncate(k);
let indices: Vec<usize> = res.iter().map(|(_, i)| *i).collect();
let vals: Vec<crate::types::Value> = res
.iter()
.map(|(s, _)| {
crate::types::Value::Text(crate::types::ArcString(std::sync::Arc::from(
s.as_str(),
)))
})
.collect();
Ok((indices, vals))
} else {
let mut heap: BinaryHeap<Reverse<(String, usize)>> =
BinaryHeap::with_capacity(k + 1);
for i in 0..col_sst.num_rows {
if col_sst.row_map.is_deleted(i) {
continue;
}
if let Some(s) = seg.get_str(i) {
heap.push(Reverse((s.to_string(), i)));
if heap.len() > k {
heap.pop();
}
}
}
let mut res: Vec<Reverse<(String, usize)>> = heap.into_vec();
res.sort_by(|a, b| b.0 .0.cmp(&a.0 .0));
res.truncate(k);
let indices: Vec<usize> = res.iter().map(|r| r.0 .1).collect();
let vals: Vec<crate::types::Value> = res
.iter()
.map(|r| {
crate::types::Value::Text(crate::types::ArcString(std::sync::Arc::from(
r.0 .0.as_str(),
)))
})
.collect();
Ok((indices, vals))
}
}
}
} // impl MoteDB
/// OrderedF64: f64 wrapper with total ordering for BinaryHeap.
/// NaN sorts last (largest) so it's popped first from a max-heap.
#[derive(Clone, Copy, PartialEq)]
struct OrderedF64(f64);
impl Eq for OrderedF64 {}
impl PartialOrd for OrderedF64 {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for OrderedF64 {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match (self.0.is_nan(), other.0.is_nan()) {
(true, true) => std::cmp::Ordering::Equal,
(true, false) => std::cmp::Ordering::Greater,
(false, true) => std::cmp::Ordering::Less,
(false, false) => self
.0
.partial_cmp(&other.0)
.unwrap_or(std::cmp::Ordering::Equal),
}
}
}
impl MoteDB {
/// Fetch specific rows by index from a columnar SSTable.
/// Used after top-K or filter to materialize only the winning rows.
pub fn scan_columnar_sstable_rows(
&self,
table_name: &str,
col_types: &[crate::types::ColumnType],
indices: &[usize],
) -> Result<Vec<Vec<Value>>> {
let iter = self.scan_columnar_sstable_streaming(table_name, col_types)?;
let mut rows = Vec::with_capacity(indices.len());
for &idx in indices {
rows.push(iter.build_row(idx));
}
Ok(rows)
}
/// Batch version: collect all rows (used when materialization is needed).
pub fn scan_columnar_sstable(
&self,
table_name: &str,
col_types: &[crate::types::ColumnType],
) -> Result<Vec<Vec<Value>>> {
let iter = self.scan_columnar_sstable_streaming(table_name, col_types)?;
let mut rows = Vec::with_capacity(iter.num_rows);
for row in iter {
rows.push(row);
}
Ok(rows)
}
/// Get approximate row count for a table (fast estimation)
///
/// Uses LSM storage statistics to estimate row count without full scan.
/// Useful for query optimization (e.g., index selectivity calculation).
///
/// # Performance
/// - Full scan: O(n) - 300ms for 300K rows
/// - Estimation: O(1) - <1ms (reads metadata only)
///
/// # Accuracy
/// - ±5% error rate (due to tombstones and MemTable)
/// - Accurate enough for query planning
///
/// # Example
/// ```ignore
/// let count = db.estimate_table_row_count("users")?;
/// // count ≈ 100,000 (actual: 95,000-105,000)
/// ```
pub fn estimate_table_row_count(&self, table_name: &str) -> Result<usize> {
// Validate table exists
let _schema = self.table_registry.get_table(table_name)?;
// Use LSM metadata to estimate count
let table_prefix = self.compute_table_prefix(table_name);
let start_key = table_prefix << 32;
let end_key = (table_prefix + 1) << 32;
// Count SSTable entries (fast - reads metadata only)
let sst_count = self
.lsm_engine
.estimate_key_count_in_range(start_key, end_key)?;
// MemTable typically contains 1-5% of data, add 5% buffer for safety
let estimated_total = (sst_count as f64 * 1.05) as usize;
Ok(estimated_total)
}
/// Fast row count from atomic counter (O(1), may be approximate)
pub fn fast_row_count(&self, table_name: &str) -> Option<u64> {
self.table_row_count
.get(table_name)
.map(|c| c.load(std::sync::atomic::Ordering::Relaxed))
}
/// 🚀 PHASE B.2: Scan table rows with partial deserialization
///
/// Only deserializes the columns specified in `col_positions`, skipping others.
/// Uses a reusable output buffer to avoid per-row allocations.
///
/// ## Performance
/// - SELECT 2/10 columns: 5x faster (400µs → 80µs)
/// - SELECT 5/10 columns: 2x faster (400µs → 200µs)
/// - SELECT * : fallback to full deserialization
pub fn scan_table_rows_partial(
&self,
table_name: &str,
col_positions: &[usize],
) -> Result<TableRowPartialIterator> {
ensure_open!(self);
let schema = self.table_registry.get_table(table_name)?;
let col_types = schema.col_types();
let fixed_count = crate::storage::row_format::compute_fixed_count(col_types);
let table_prefix = self.compute_table_prefix(table_name);
let start_key = table_prefix << 32;
let end_key = (table_prefix + 1) << 32;
let lsm_iter = self.lsm_engine.scan_range_streaming(start_key, end_key)?;
Ok(TableRowPartialIterator {
lsm_iter,
col_types: col_types.to_vec(),
fixed_count,
col_positions: col_positions.to_vec(),
out_buf: Vec::new(),
})
}
// ==================== Batch Operations ====================
/// Batch insert rows to a specific table with incremental index updates
///
/// **NOTE**: This method updates indexes incrementally for each row, ensuring consistency
/// even for small datasets (< 500 rows) that don't trigger batch index building.
///
/// # Example
/// ```ignore
/// let rows = vec![
/// vec![Value::Integer(1), Value::Text("Alice".into())],
/// vec![Value::Integer(2), Value::Text("Bob".into())],
/// ];
/// let row_ids = db.batch_insert_rows_to_table("users", rows)?;
/// ```ignore
pub fn batch_insert_rows_to_table(
&self,
table_name: &str,
mut rows: Vec<Row>,
) -> Result<Vec<RowId>> {
ensure_open!(self);
if rows.is_empty() {
return Ok(Vec::new());
}
// 1. Get table schema
let schema = self.table_registry.get_table(table_name)?;
// 🚀 Fast path: AUTO_INCREMENT tables with columnar storage skip per-row
// validation, WAL clone overhead, and mmap page release. This is the
// hot path for bulk INSERT benchmarks — ~3x faster than the full path.
let auto_inc = schema.is_primary_key_auto_increment();
// Only use fast_batch_insert for large batches with ColSegmentStore.
// Single-row inserts go through the normal path (WAL + index updates).
if auto_inc && rows.len() >= 100 {
return self.fast_batch_insert(table_name, rows, &schema);
}
// 2. Validate all rows
for (idx, row) in rows.iter().enumerate() {
schema.validate_row(row).map_err(|e| {
StorageError::InvalidData(format!(
"Row {} validation failed for table '{}': {}",
idx, table_name, e
))
})?;
}
// 2.5 Check primary key uniqueness for non-AUTO_INCREMENT tables
if !schema.is_primary_key_auto_increment() {
if let Some(pk_name) = schema.primary_key() {
if let Some(pk_col) = schema.get_column(pk_name) {
let mut batch_pks: HashSet<crate::database::pk_cache::PkKey> =
HashSet::with_capacity(rows.len());
for (idx, row) in rows.iter().enumerate() {
if let Some(pk_value) = row.get(pk_col.position) {
// NULL primary key is invalid per SQL standard
if matches!(pk_value, Value::Null) {
return Err(StorageError::InvalidData(format!(
"Batch row {}: NULL primary key is not allowed for table '{}'",
idx, table_name
)));
}
let pk_key = crate::database::pk_cache::PkKey::from_value(pk_value);
// Intra-batch duplicate check
if !batch_pks.insert(pk_key.clone()) {
return Err(StorageError::InvalidData(format!(
"Batch row {}: duplicate primary key {:?} within batch for table '{}'", idx, pk_value, table_name
)));
}
// Atomic check-and-insert into PK cache.
// This reserves the PK key, preventing concurrent inserts
// from using the same key (eliminates TOCTOU race).
if let Some(lookup) = self.pk_lookup.get(table_name) {
match lookup.insert_if_absent(pk_key, 0 /* placeholder */) {
Ok(()) => {} // Reserved successfully
Err(_) => {
return Err(StorageError::InvalidData(format!(
"Batch row {}: duplicate primary key {:?} for table '{}'", idx, pk_value, table_name
)));
}
}
} else {
// No PK cache — fall back to slow path (column index check)
match self.query_by_column(table_name, pk_name, pk_value) {
Ok(found) if !found.is_empty() => {
let mut has_live = false;
for &rid in &found {
if self.get_table_row(table_name, rid)?.is_some() {
has_live = true;
break;
}
}
if has_live {
return Err(StorageError::InvalidData(format!(
"Batch row {}: duplicate primary key {:?} for table '{}'", idx, pk_value, table_name
)));
}
}
_ => {}
}
}
}
}
}
}
}
// 3. Batch allocate row IDs
let mut row_ids = Vec::with_capacity(rows.len());
let auto_inc = schema.is_primary_key_auto_increment();
// Ensure all rows have enough slots for AUTO_INCREMENT PK column
if auto_inc {
if let Some(pk_name) = schema.primary_key() {
if let Some(pk_col) = schema.get_column(pk_name) {
for row in rows.iter_mut() {
while row.len() <= pk_col.position {
row.push(Value::Null);
}
}
}
}
}
// Pre-validate all rows before allocating IDs (avoid wasting AUTO_INCREMENT IDs on invalid rows)
{
for (idx, row) in rows.iter().enumerate() {
schema.validate_row(row).map_err(|e| {
StorageError::InvalidData(format!(
"Row {} validation failed for table '{}': {}",
idx, table_name, e
))
})?;
}
}
if auto_inc {
// Use per-table AUTO_INCREMENT counter (consistent with insert_row_to_table)
let counter = {
self.table_auto_increment
.entry(table_name.to_string())
.or_insert_with(|| {
Arc::new(std::sync::atomic::AtomicI64::new(
schema.get_auto_increment_start(),
))
})
.value()
.clone()
};
for _ in 0..rows.len() {
let id = counter.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
if !(0..=i64::MAX - 1000).contains(&id) {
return Err(StorageError::AutoIncrementOverflow(table_name.to_string()));
}
row_ids.push(id as u64);
}
} else {
// Non-AUTO_INCREMENT: use global row_id
let start_id = self
.next_row_id
.fetch_add(rows.len() as u64, std::sync::atomic::Ordering::Relaxed);
for i in 0..rows.len() {
row_ids.push(start_id + i as u64);
}
}
// 3.5 Fill AUTO_INCREMENT PK column values in rows
let mut rows = rows;
if auto_inc {
if let Some(pk_name) = schema.primary_key() {
if let Some(pk_col) = schema.get_column(pk_name) {
for (i, row) in rows.iter_mut().enumerate() {
while row.len() <= pk_col.position {
row.push(Value::Null);
}
row[pk_col.position] = Value::Integer(row_ids[i] as i64);
}
}
}
}
// 4. Write WAL first (durability guarantee) + columnar buffer (primary storage).
// WAL ensures zero data loss on crash; columnar buffer enables fast reads.
let col_types = schema.col_types();
let table_id = self.table_registry.get_table_id(table_name).unwrap_or(0) as u64;
let base_ts = self
.write_lsn
.fetch_add(rows.len() as u64, std::sync::atomic::Ordering::Relaxed);
// Build WAL records (lightweight: no RawRow encoding, just Row values)
let wal_records: Vec<WALRecord> = rows
.iter()
.enumerate()
.map(|(i, row)| WALRecord::Insert {
table_name: table_name.to_string(),
row_id: row_ids[i],
partition: (self.make_composite_key(table_name, row_ids[i])
% self.num_partitions as u64) as PartitionId,
data: row.clone(),
txn_id: 0,
})
.collect();
self.increment_pending_updates();
self.wal.batch_append(0, wal_records)?;
// 🆕 S9: write ONLY to the multi-segment ColSegmentStore (single-track).
// The legacy columnar_write_bufs path is bypassed for batch INSERT —
// eliminating the dual-write overhead (256ms → ~65ms for 60K rows).
// Queries route to ColSegmentStore via has_col_segment_store().
{
let store = self.get_or_create_col_segment_store(table_name, col_types)?;
let store_rows: Vec<(u64, u64, Row)> = rows
.iter()
.enumerate()
.map(|(i, row)| {
let key = (table_id << 32) | (row_ids[i] & 0xFFFFFFFF);
(key, base_ts + i as u64, row.clone())
})
.collect();
store.append_rows(&store_rows)?;
// Flush periodically to bound the in-memory buffer. 20K rows keeps
// buffer ~3MB while limiting segment count (500K → ~25 segs vs 125).
if store.buffered_row_count() >= 100000 {
store.flush_buffer()?;
// Defer compaction to background (keeps INSERT memory <30MB).
// Compaction at query time (SelectColumnar) handles first-query latency.
}
}
// Release segment mmap pages after bulk INSERT to keep RSS low.
// Pages re-fault on next read access.
if let Some(store) = self.col_segment_stores.get(table_name) {
for seg in store.segments_snapshot() {
seg.clear_cache();
seg.release_pages();
}
}
// Keeps write buffer under ~1.6 MB (10K × 40B × 4 cols) on embedded devices.
let _old_count = self
.pending_updates
.fetch_add(rows.len(), std::sync::atomic::Ordering::Release);
// 6.5 Update PK cache for non-auto_increment tables
if !auto_inc {
if let Some(pk_name) = schema.primary_key() {
if let Some(pk_col) = schema.get_column(pk_name) {
if let Some(lookup) = self.pk_lookup.get(table_name) {
for (i, row) in rows.iter().enumerate() {
if let Some(pk_value) = row.get(pk_col.position) {
lookup.insert(
crate::database::pk_cache::PkKey::from_value(pk_value),
row_ids[i],
);
}
}
}
}
}
}
// 7. Batch update all indexes
debug_log!(
"[batch_insert_rows_to_table] Batch updating indexes for {} rows in table '{}'",
rows.len(),
table_name
);
// 7.1 Collect data for all column indexes, then insert in parallel.
// Each column index is independent — no shared state between them.
//
// 🚀 Skip index updates when using columnar storage — WHERE/LIKE/aggregate
// queries use columnar filter paths instead of BTree indexes.
// Saves ~22MB per index on embedded devices.
if self.columnar_sstables.contains_key(table_name)
|| self.columnar_write_bufs.contains_key(table_name)
{
debug_log!(
"[batch_insert] Skipping column index updates — columnar storage active for '{}'",
table_name
);
// Still need to handle vector/text/spatial indexes below
} else {
let prefix_len = table_name.len() + 1;
let mut column_tasks: Vec<(
Arc<crate::index::column_value::ColumnValueIndex>,
Vec<(Value, RowId)>,
String,
)> = Vec::new();
for col_def in &schema.columns {
let col_name = &col_def.name;
// Collect column index data
let mut col_index_key = String::with_capacity(prefix_len + col_name.len());
col_index_key.push_str(table_name);
col_index_key.push('.');
col_index_key.push_str(col_name);
if let Some(index_ref) = self.column_indexes.get(&col_index_key) {
let mut column_data: Vec<(Value, RowId)> = Vec::with_capacity(rows.len());
for (row_id, row) in row_ids.iter().zip(rows.iter()) {
if let Some(col_value) = row.get(col_def.position) {
column_data.push((col_value.clone(), *row_id));
}
}
if !column_data.is_empty() {
column_tasks.push((
index_ref.value().clone(),
column_data,
col_index_key.clone(),
));
}
}
}
// Parallel insert into column indexes (one thread per index).
// Each index has its own mem_buffer and BTree — no shared state.
if column_tasks.len() > 1 {
std::thread::scope(|s| {
for (index, data, key) in column_tasks {
s.spawn(move || {
if let Err(_e) = index.batch_insert(data) {
debug_log!(
"[batch_insert] Failed to batch update column index '{}': {}",
key,
_e
);
}
});
}
});
} else {
for (index, data, key) in column_tasks {
if let Err(_e) = index.batch_insert(data) {
debug_log!(
"[batch_insert] Failed to batch update column index '{}': {}",
key,
_e
);
self.index_registry.mark_stale(&key);
}
}
}
} // end else (columnar SSTable exists → skip column indexes)
// 7.2 Collect and batch update non-column indexes (vector, text, spatial)
for col_def in &schema.columns {
let col_name = &col_def.name;
// 7.2a 批量更新 Vector Index
if let crate::types::ColumnType::Tensor(_dim) = col_def.col_type {
if let Some(index_name) = self.index_registry.find_by_column(
table_name,
col_name,
crate::database::index_metadata::IndexType::Vector,
) {
let mut vectors: Vec<(RowId, Vec<f32>)> = Vec::with_capacity(rows.len());
for (row_id, row) in row_ids.iter().zip(rows.iter()) {
if let Some(crate::types::Value::Vector(arc_vec)) =
row.get(col_def.position)
{
// ArcVec 是 Arc<Vec<f32>> 的包装,需要解引用
vectors.push((*row_id, (*arc_vec.0).clone()));
}
}
if !vectors.is_empty() {
if let Err(_e) = self.batch_insert_vectors(&index_name, &vectors) {
debug_log!(
"[batch_insert] Failed to batch update vector index '{}': {}",
index_name,
_e
);
self.index_registry.mark_stale(&index_name);
}
}
}
}
// 7.3 批量更新 Text Index
if matches!(col_def.col_type, crate::types::ColumnType::Text) {
if let Some(index_name) = self.index_registry.find_by_column(
table_name,
col_name,
crate::database::index_metadata::IndexType::Text,
) {
let mut texts: Vec<(RowId, String)> = Vec::with_capacity(rows.len());
for (row_id, row) in row_ids.iter().zip(rows.iter()) {
if let Some(crate::types::Value::Text(text)) = row.get(col_def.position) {
texts.push((*row_id, text.to_string()));
}
}
if !texts.is_empty() {
let texts_ref: Vec<(RowId, &str)> =
texts.iter().map(|(id, s)| (*id, s.as_str())).collect();
if let Err(_e) = self.batch_insert_texts(&index_name, &texts_ref) {
debug_log!(
"[batch_insert] Failed to batch update text index '{}': {}",
index_name,
_e
);
self.index_registry.mark_stale(&index_name);
}
}
}
}
// 7.4 i-Octree Index (3D point cloud)
if matches!(col_def.col_type, crate::types::ColumnType::Spatial) {
if let Some(octree_name) = self.index_registry.find_by_column(
table_name,
col_name,
crate::database::index_metadata::IndexType::Octree,
) {
for (row_id, row) in row_ids.iter().zip(rows.iter()) {
if let Some(crate::types::Value::Spatial(geom)) = row.get(col_def.position)
{
if let Err(_e) = self.insert_ioctree_point(*row_id, &octree_name, geom)
{
debug_log!(
"[batch_insert] Failed to update ioctree index '{}': {}",
octree_name,
_e
);
self.index_registry.mark_stale(&octree_name);
}
}
}
}
}
// 7.5 Timestamp Index (legacy single-index architecture, handled by batch build)
// Note: Timestamp index uses a different architecture (single BTree index)
// and is updated during flush via batch building
}
// 8. Update row count for COUNT(*) fast path
if let Some(counter) = self.table_row_count.get(table_name) {
use std::sync::atomic::Ordering;
counter.fetch_add(rows.len() as u64, Ordering::Relaxed);
}
// Auto-flush trigger
let old_count = self
.pending_updates
.fetch_add(rows.len(), std::sync::atomic::Ordering::Release);
if old_count / 2_000 != (old_count + rows.len()) / 2_000 {
self.request_auto_flush();
}
Ok(row_ids)
}
/// 🚀 Fast batch INSERT for AUTO_INCREMENT tables.
///
/// Skips: per-row validation, WAL record cloning, mmap page release, PK cache.
/// Writes directly to ColSegmentStore buffer. ~3x faster than full path.
/// Durability: ColSegmentStore MANIFEST is fsync'd on flush_buffer().
fn fast_batch_insert(
&self,
table_name: &str,
mut rows: Vec<Row>,
schema: &crate::types::TableSchema,
) -> Result<Vec<RowId>> {
let n = rows.len();
let col_types = schema.col_types();
// Allocate AUTO_INCREMENT IDs atomically (batch).
let pk_pos = schema
.primary_key()
.and_then(|pk| schema.get_column(pk))
.map(|c| c.position);
let counter = {
self.table_auto_increment
.entry(table_name.to_string())
.or_insert_with(|| {
Arc::new(std::sync::atomic::AtomicI64::new(
schema.get_auto_increment_start(),
))
})
.value()
.clone()
};
let start_id = counter.fetch_add(n as i64, std::sync::atomic::Ordering::Relaxed);
let row_ids: Vec<u64> = (0..n).map(|i| (start_id + i as i64) as u64).collect();
// Fill PK column values in-place (no clone).
if let Some(pk_pos) = pk_pos {
for (i, row) in rows.iter_mut().enumerate() {
while row.len() <= pk_pos {
row.push(Value::Null);
}
row[pk_pos] = Value::Integer(row_ids[i] as i64);
}
}
// Write directly to ColSegmentStore (skip WAL for edge config).
let store = self.get_or_create_col_segment_store(table_name, col_types)?;
let table_id = self.table_registry.get_table_id(table_name).unwrap_or(0) as u64;
let base_ts = self
.write_lsn
.fetch_add(n as u64, std::sync::atomic::Ordering::Relaxed);
// Build store rows: (key, timestamp, values).
// Use Vec::with_capacity + drain to avoid cloning the rows Vec.
let store_rows: Vec<(u64, u64, Row)> = rows
.into_iter()
.enumerate()
.map(|(i, row)| {
let key = (table_id << 32) | (row_ids[i] & 0xFFFFFFFF);
(key, base_ts + i as u64, row)
})
.collect();
store.append_rows(&store_rows)?;
// Flush periodically to bound memory (same threshold as full path).
if store.buffered_row_count() >= 100_000 {
store.flush_buffer()?;
}
// Update row count for COUNT(*) fast path.
if let Some(counter) = self.table_row_count.get(table_name) {
counter.fetch_add(n as u64, std::sync::atomic::Ordering::Relaxed);
}
Ok(row_ids)
}
/// Batch get rows from a table (smart optimization for continuous IDs)
///
/// **Smart Strategy**:
/// - If row_ids are continuous (e.g. [100,101,102,...]): Use LSM range scan (22-45x faster)
/// - Otherwise: Batch point query (4-9x faster than individual calls)
///
/// # Performance
/// - Continuous IDs: ~1-2ms for 1000 rows
/// - Random IDs: ~5-10ms for 1000 rows
/// - Single calls: ~45ms for 1000 rows (baseline)
///
/// # Example
/// ```ignore
/// let row_ids = vec![100, 101, 102, 103]; // Continuous
/// let rows = db.get_table_rows_batch("robots", &row_ids)?;
/// ```ignore
pub fn get_table_rows_batch(
&self,
table_name: &str,
row_ids: &[RowId],
) -> Result<Vec<(RowId, Option<Row>)>> {
let arc_results = self.get_table_rows_batch_arc(table_name, row_ids)?;
Ok(arc_results
.into_iter()
.map(|(rid, opt)| (rid, opt.map(|a| (*a).clone())))
.collect())
}
/// Batch fetch rows, returning Arc<Row> to avoid clone on cache hit
pub fn get_table_rows_batch_arc(
&self,
table_name: &str,
row_ids: &[RowId],
) -> Result<Vec<(RowId, Option<Arc<Row>>)>> {
if row_ids.is_empty() {
return Ok(Vec::new());
}
let _schema = self.table_registry.get_table(table_name)?;
// Batch cache check — single lock acquisition for all rows
let cached = self.row_cache.batch_get(table_name, row_ids);
let mut missed_ids: Vec<RowId> = Vec::new();
let mut missed_indices: Vec<usize> = Vec::new();
let mut results: Vec<(RowId, Option<Arc<Row>>)> = Vec::with_capacity(row_ids.len());
for (&row_id, opt) in row_ids.iter().zip(cached.into_iter()) {
match opt {
Some(arc) => results.push((row_id, Some(arc))),
None => {
results.push((row_id, None));
missed_ids.push(row_id);
missed_indices.push(results.len() - 1);
}
}
}
if missed_ids.is_empty() {
return Ok(results);
}
let is_continuous = self.is_continuous_row_ids(&missed_ids);
let fetched: Vec<(RowId, Option<Row>)> = if missed_ids.len() == 1 {
// Single row_id: direct point get is faster than scan+filter
let schema = self.table_registry.get_table(table_name)?;
let row_id = missed_ids[0];
let opt = self
.get_table_row_arc(table_name, row_id, &schema)?
.map(|arc| match Arc::try_unwrap(arc) {
Ok(row) => row,
Err(arc) => (*arc).clone(),
});
vec![(row_id, opt)]
} else if is_continuous {
self.get_table_rows_batch_range(table_name, &missed_ids)?
} else {
let mut sorted_ids = missed_ids.clone();
sorted_ids.sort_unstable();
sorted_ids.dedup();
self.get_table_rows_scan_with_filter(table_name, &sorted_ids)?
};
// Sort fetched results by row_id for binary search (avoids HashMap allocation)
let mut fetched_sorted: Vec<(RowId, Row)> = fetched
.into_iter()
.filter_map(|(rid, opt)| opt.map(|r| (rid, r)))
.collect();
fetched_sorted.sort_unstable_by_key(|(rid, _)| *rid);
for (i, &row_id) in missed_ids.iter().enumerate() {
if let Some(&result_idx) = missed_indices.get(i) {
if let Ok(pos) = fetched_sorted.binary_search_by_key(&row_id, |(rid, _)| *rid) {
let row_arc = Arc::new(fetched_sorted[pos].1.clone());
results[result_idx] = (row_id, Some(row_arc));
}
}
}
Ok(results)
}
// ==================== Internal Helpers ====================
/// Increment pending updates counter and trigger auto-flush if needed
/// 🚀 P0 CRITICAL FIX: 使用原子操作避免锁竞争,解决 CPU 飙升问题
fn increment_pending_updates(&self) {
use std::sync::atomic::Ordering;
let count = self.pending_updates.fetch_add(1, Ordering::Release);
// 每2000条触发一次flush(与LSM一致)
if count.is_multiple_of(2_000) && count > 0 {
debug_log!("[AUTO-FLUSH] Triggered after {} writes", count);
self.request_auto_flush();
}
}
/// Trigger background prefetch
///
/// ⚠️ IMPORTANT: This method MUST NOT call get_table_rows_batch() to avoid infinite recursion!
fn trigger_prefetch(&self, table_name: &str, start_row_id: RowId, count: usize, stride: i64) {
// stride == 0 means same row accessed repeatedly — skip prefetch
if stride == 0 {
return;
}
let mut row_ids_to_fetch = Vec::with_capacity(count);
let mut current_id = start_row_id as i64;
// Generate row_ids based on stride
for _ in 0..count {
if current_id > 0 {
row_ids_to_fetch.push(current_id as RowId);
}
current_id += stride;
// Safety check
if !(0..=i64::MAX / 2).contains(¤t_id) {
break;
}
}
// Record prefetch attempt
self.row_cache.record_prefetch(row_ids_to_fetch.len());
// Get schema for correct type-aware decoding (decode_any treats all fixed cols as Integer!)
let col_types = match self.table_registry.get_table(table_name) {
Ok(schema) => schema.col_types().to_vec(),
Err(_) => return,
};
// Directly fetch from LSM without triggering get_table_rows_batch (avoid recursion)
for row_id in row_ids_to_fetch {
let composite_key = self.make_composite_key(table_name, row_id);
if let Ok(Some(value)) = self.lsm_engine.get(composite_key) {
if !value.deleted {
if let crate::storage::lsm::ValueData::Inline(bytes) = &value.data {
if let Ok(row) = crate::storage::row_format::decode(bytes, &col_types) {
self.row_cache.put(table_name.to_string(), row_id, row);
self.row_cache.record_prefetch_hit();
}
}
}
}
}
}
/// Check if row_ids are continuous
fn is_continuous_row_ids(&self, row_ids: &[RowId]) -> bool {
if row_ids.len() < 2 {
return false;
}
for i in 1..row_ids.len() {
if row_ids[i] != row_ids[i - 1] + 1 {
return false;
}
}
true
}
/// Batch get using LSM range scan (for continuous row_ids)
/// Fetch rows for sorted, non-continuous row_ids using a single LSM range scan + HashSet filter.
/// Converts N random reads into 1 sequential scan — ~100x faster for scattered IDs.
fn get_table_rows_scan_with_filter(
&self,
table_name: &str,
sorted_ids: &[RowId],
) -> Result<Vec<(RowId, Option<Row>)>> {
if sorted_ids.is_empty() {
return Ok(Vec::new());
}
// 🆕 S9: For ColSegmentStore tables, use store.get() per row_id.
// This is the authoritative data source — LSM may not have the data.
if let Some(store) = self.col_segment_stores.get(table_name) {
let _ = store.flush_buffer();
let mut result: Vec<(RowId, Option<Row>)> = Vec::with_capacity(sorted_ids.len());
for &row_id in sorted_ids {
let composite_key = self.make_composite_key(table_name, row_id);
let row = store.get(composite_key);
result.push((row_id, row));
}
return Ok(result);
}
let min_id = sorted_ids[0];
let max_id = sorted_ids[sorted_ids.len() - 1];
let start_key = self.make_composite_key(table_name, min_id);
let end_key = self.make_composite_key(table_name, max_id + 1);
// Use streaming scan to avoid materializing all rows into a Vec.
let lsm_iter = self.lsm_engine.scan_range_streaming(start_key, end_key)?;
// Pre-compute decode info outside the loop
let decode_info = self.table_registry.get_table(table_name).ok().map(|s| {
let col_types = s.col_types();
let fc = crate::storage::row_format::compute_fixed_count(col_types);
(col_types.to_vec(), fc)
});
let mut result = Vec::with_capacity(sorted_ids.len());
for item in lsm_iter {
let (composite_key, value) = item?;
let row_id = (composite_key & 0xFFFFFFFF) as RowId;
// Binary search instead of HashSet — avoids heap allocation for id_set
if sorted_ids.binary_search(&row_id).is_err() {
continue;
}
if value.deleted {
result.push((row_id, None));
continue;
}
let data = match &value.data {
crate::storage::lsm::ValueData::Inline(bytes) => bytes.as_slice(),
crate::storage::lsm::ValueData::Blob(_) => {
return Err(StorageError::InvalidData("Blob not supported".into()));
}
};
let row: Row = if let Some((ref col_types, fc)) = decode_info {
crate::storage::row_format::decode_fast(data, col_types, fc)
.map_err(|e| StorageError::Serialization(e.to_string()))?
} else {
crate::storage::row_format::decode_any(data)
.map_err(|e| StorageError::Serialization(e.to_string()))?
};
self.row_cache
.put(table_name.to_string(), row_id, row.clone());
result.push((row_id, Some(row)));
}
Ok(result)
}
fn get_table_rows_batch_range(
&self,
table_name: &str,
row_ids: &[RowId],
) -> Result<Vec<(RowId, Option<Row>)>> {
let min_id = *row_ids.iter().min().unwrap();
let max_id = *row_ids.iter().max().unwrap();
let start_key = self.make_composite_key(table_name, min_id);
let end_key = self.make_composite_key(table_name, max_id + 1);
let lsm_rows = self.lsm_engine.scan_range(start_key, end_key)?;
// Pre-compute decode info outside the loop
let decode_info = self.table_registry.get_table(table_name).ok().map(|s| {
let col_types = s.col_types();
let fc = crate::storage::row_format::compute_fixed_count(col_types);
(col_types.to_vec(), fc)
});
let mut result = Vec::new();
for (composite_key, value) in lsm_rows {
let row_id = (composite_key & 0xFFFFFFFF) as RowId;
if value.deleted {
result.push((row_id, None));
continue;
}
let data = match &value.data {
crate::storage::lsm::ValueData::Inline(bytes) => bytes.as_slice(),
crate::storage::lsm::ValueData::Blob(_) => {
return Err(StorageError::InvalidData("Blob not supported".into()));
}
};
let row: Row = if let Some((ref col_types, fc)) = decode_info {
crate::storage::row_format::decode_fast(data, col_types, fc)
.map_err(|e| StorageError::Serialization(e.to_string()))?
} else {
crate::storage::row_format::decode_any(data)
.map_err(|e| StorageError::Serialization(e.to_string()))?
};
// Cache row
self.row_cache
.put(table_name.to_string(), row_id, row.clone());
result.push((row_id, Some(row)));
}
Ok(result)
}
}
/// 🚀 表行批量迭代器
///
/// 每次返回一批行数据,避免一次性加载全部数据到内存。
pub struct TableRowBatchedIterator {
lsm_iter: crate::storage::lsm::LSMBatchedIterator,
_table_name: String,
col_types: Option<Vec<crate::types::ColumnType>>,
fixed_count: usize,
}
impl Iterator for TableRowBatchedIterator {
type Item = Result<Vec<(RowId, Row)>>;
fn next(&mut self) -> Option<Self::Item> {
match self.lsm_iter.next() {
Some(Ok(batch)) => {
let mut result = Vec::with_capacity(batch.len());
for (composite_key, value) in batch {
// Skip tombstones (deleted rows)
if value.deleted {
continue;
}
// Extract row_id from composite_key
let row_id = (composite_key & 0xFFFFFFFF) as RowId;
// Extract data
let data = match &value.data {
crate::storage::lsm::ValueData::Inline(bytes) => bytes.as_slice(),
crate::storage::lsm::ValueData::Blob(_) => {
return Some(Err(StorageError::InvalidData(
"Blob references should be resolved by LSM engine".into(),
)));
}
};
// Deserialize row: prefer schema-aware decode
let row: Row = if let Some(ref col_types) = self.col_types {
match crate::storage::row_format::decode_fast(
data,
col_types,
self.fixed_count,
) {
Ok(row) => row,
Err(_) => match crate::storage::row_format::decode_any(data) {
Ok(row) => row,
Err(e) => {
return Some(Err(StorageError::Serialization(e.to_string())))
}
},
}
} else {
match crate::storage::row_format::decode_any(data) {
Ok(row) => row,
Err(e) => return Some(Err(StorageError::Serialization(e.to_string()))),
}
};
result.push((row_id, row));
}
Some(Ok(result))
}
Some(Err(e)) => Some(Err(e)),
None => None,
}
}
}
/// Raw byte streaming iterator — yields (row_id, raw_bytes) without row decode.
pub struct TableRawStreamingIterator {
lsm_iter: crate::storage::lsm::MergingIterator,
}
impl Iterator for TableRawStreamingIterator {
type Item = Result<(RowId, Vec<u8>)>;
fn next(&mut self) -> Option<Self::Item> {
loop {
match self.lsm_iter.next() {
Some(Ok((composite_key, value))) => {
if value.deleted {
continue;
}
let row_id = (composite_key & 0xFFFFFFFF) as RowId;
match &value.data {
crate::storage::lsm::ValueData::Inline(bytes) => {
return Some(Ok((row_id, bytes.to_vec())));
}
crate::storage::lsm::ValueData::Blob(_) => {
return Some(Err(StorageError::InvalidData(
"Blob references should be resolved by LSM engine".into(),
)));
}
}
}
Some(Err(e)) => return Some(Err(e)),
None => return None,
}
}
}
}
/// 🚀 表行流式迭代器(真正的 O(1) 内存占用)
///
/// 逐个返回行数据,不预先加载任何数据到内存。
/// 使用 SchemaDecodeContext 实现预计算 schema 上下文,消除每行冗余计算。
pub struct TableRowStreamingIterator {
inner: TableRowStreamingInner,
}
enum TableRowStreamingInner {
/// LSM row-store backed scan.
Lsm {
lsm_iter: crate::storage::lsm::MergingIterator,
decode_ctx: Option<crate::storage::row_format::SchemaDecodeContext>,
use_raw: bool,
},
/// Columnar SSTable backed scan. For tables whose data lives in the
/// columnar SSTable (not the LSM), we decode column arrays into rows.
/// `col_types` drives the decode: Integer→get_i64, Float→get_f64,
/// Boolean→get_bool. Without it every fixed-width column was decoded as
/// Integer, reinterpreting Float/Boolean bits as i64 (garbage values).
Columnar {
row_map: crate::storage::lsm::columnar::RowMap,
segments: Vec<ColumnarSegment>,
col_names: Vec<String>,
col_types: Vec<crate::types::ColumnType>,
current_idx: usize,
num_rows: usize,
},
}
impl Iterator for TableRowStreamingIterator {
type Item = Result<(RowId, Row)>;
fn next(&mut self) -> Option<Self::Item> {
match &mut self.inner {
TableRowStreamingInner::Lsm {
lsm_iter,
decode_ctx,
use_raw,
} => lsm_next(lsm_iter, decode_ctx, *use_raw),
TableRowStreamingInner::Columnar {
row_map,
segments,
col_names,
col_types,
current_idx,
num_rows,
} => {
while *current_idx < *num_rows {
let idx = *current_idx;
*current_idx += 1;
if row_map.is_deleted(idx) {
continue;
}
let key = row_map.key(idx);
let row_id = (key & 0xFFFFFFFF) as RowId;
let mut row: Row = Vec::with_capacity(col_names.len());
for (ci, seg) in segments.iter().enumerate() {
// Decode by the column's declared type so Float/Boolean
// are not reinterpreted as Integer bit patterns.
let v = match seg {
ColumnarSegment::Fixed(f) => match col_types.get(ci) {
Some(crate::types::ColumnType::Float) => {
f.get_f64(idx).map(crate::types::Value::Float)
}
Some(crate::types::ColumnType::Boolean) => {
f.get_bool(idx).map(crate::types::Value::Bool)
}
_ => f.get_i64(idx).map(crate::types::Value::Integer),
}
.unwrap_or(crate::types::Value::Null),
ColumnarSegment::Text(t) => t
.get_str(idx)
.map(|s| crate::types::Value::Text(s.into()))
.unwrap_or(crate::types::Value::Null),
ColumnarSegment::Vector(cols) => cols
.get(idx)
.cloned()
.flatten()
.map(|f32s| {
crate::types::Value::Vector(crate::types::ArcVec(
std::sync::Arc::new(f32s),
))
})
.unwrap_or(crate::types::Value::Null),
ColumnarSegment::Spatial(cols) => cols
.get(idx)
.cloned()
.flatten()
.map(|g| crate::types::Value::Spatial(std::boxed::Box::new(g)))
.unwrap_or(crate::types::Value::Null),
};
row.push(v);
}
return Some(Ok((row_id, row)));
}
None
}
}
}
}
/// Shared LSM scan logic, factored out so the enum dispatch stays readable.
fn lsm_next(
lsm_iter: &mut crate::storage::lsm::MergingIterator,
decode_ctx: &mut Option<crate::storage::row_format::SchemaDecodeContext>,
use_raw: bool,
) -> Option<Result<(RowId, Row)>> {
if use_raw {
loop {
match lsm_iter.next_raw() {
Some(Ok((composite_key, _ts, deleted, vb))) => {
if deleted {
continue;
}
if vb.len == 0 {
continue;
}
let row_id = (composite_key & 0xFFFFFFFF) as RowId;
let row: Row = if let Some(ref mut ctx) = decode_ctx {
match ctx.decode_row(vb.as_slice()) {
Ok(row) => row,
Err(e) => return Some(Err(e)),
}
} else {
match crate::storage::row_format::decode_any_with_pool(vb.as_slice(), None)
{
Ok(row) => row,
Err(e) => return Some(Err(e)),
}
};
return Some(Ok((row_id, row)));
}
Some(Err(e)) => return Some(Err(e)),
None => return None,
}
}
}
loop {
match lsm_iter.next() {
Some(Ok((composite_key, value))) => {
if value.deleted {
continue;
}
let row_id = (composite_key & 0xFFFFFFFF) as RowId;
let data = match &value.data {
crate::storage::lsm::ValueData::Inline(bytes) => bytes.as_slice(),
crate::storage::lsm::ValueData::Blob(_) => {
return Some(Err(StorageError::InvalidData(
"Blob references should be resolved by LSM engine".into(),
)));
}
};
let row: Row = if let Some(ref mut ctx) = decode_ctx {
match ctx.decode_row(data) {
Ok(row) => row,
Err(e) => return Some(Err(e)),
}
} else {
match crate::storage::row_format::decode_any_with_pool(data, None) {
Ok(row) => row,
Err(e) => return Some(Err(e)),
}
};
return Some(Ok((row_id, row)));
}
Some(Err(e)) => return Some(Err(e)),
None => return None,
}
}
}
/// Zero-copy decode streaming iterator — decodes rows directly into a
/// caller-provided `Vec<Value>` using `ValueBytes::as_slice()`.
///
/// Column segment wrapper for streaming columnar scans.
enum ColumnarSegment {
Fixed(crate::storage::lsm::columnar::FixedSegment),
Text(crate::storage::lsm::columnar::TextSegment),
/// Pre-decoded Vector column: one Vec<f32> per row (None = NULL).
Vector(Vec<Option<Vec<f32>>>),
/// Pre-decoded Spatial column: one Geometry per row (None = NULL).
Spatial(Vec<Option<crate::types::Geometry>>),
}
/// Build a ColumnarSegment from an SSTable column, dispatching on the stored
/// column tag. Vector/Spatial columns are pre-decoded into per-row option vecs
/// (since they have no zero-copy FixedSegment/TextSegment readers used by the
/// row iterators); the rest use zero-copy Fixed/Text segments.
fn build_column_segment(
col_sst: &crate::storage::lsm::columnar::ColumnarSSTable,
col_idx: usize,
num_rows: usize,
) -> Result<ColumnarSegment> {
use crate::storage::lsm::columnar::ColumnTypeTag;
let tag = col_sst.column_tags.get(col_idx);
match tag {
Some(ColumnTypeTag::Vector) => {
// read_vectors returns (row_id, vec) pairs in row order (i=0..n);
// map each to its original row index by re-iterating in lockstep.
let decoded = col_sst.read_vectors(col_idx)?;
let mut per_row: Vec<Option<Vec<f32>>> = vec![None; num_rows];
let mut di = 0usize;
for i in 0..num_rows {
if col_sst.row_map.is_deleted(i) {
continue;
}
// read_vectors skips nulls too; its k-th output corresponds to
// the k-th non-null, non-deleted row. Match by row_id.
let expected_key = col_sst.row_map.key(i) & 0xFFFFFFFF;
while di < decoded.len() && decoded[di].0 != expected_key {
di += 1;
}
if di < decoded.len() {
per_row[i] = Some(decoded[di].1.clone());
di += 1;
}
}
Ok(ColumnarSegment::Vector(per_row))
}
Some(ColumnTypeTag::Spatial) => {
let decoded = col_sst.read_spatial(col_idx)?;
let mut per_row: Vec<Option<crate::types::Geometry>> = vec![None; num_rows];
let mut di = 0usize;
for i in 0..num_rows {
if col_sst.row_map.is_deleted(i) {
continue;
}
let expected_key = col_sst.row_map.key(i) & 0xFFFFFFFF;
while di < decoded.len() && decoded[di].0 != expected_key {
di += 1;
}
if di < decoded.len() {
per_row[i] = Some(decoded[di].1.clone());
di += 1;
}
}
Ok(ColumnarSegment::Spatial(per_row))
}
Some(t) if t.is_fixed() => Ok(ColumnarSegment::Fixed(col_sst.read_fixed_i64(col_idx)?)),
_ => Ok(ColumnarSegment::Text(col_sst.read_text(col_idx)?)),
}
}
/// Streaming iterator over a columnar SSTable. Yields one row at a time
/// as Vec<Value>, avoiding full materialization.
pub struct ColumnarScanIterator {
row_map: crate::storage::lsm::columnar::RowMap,
segments: Vec<ColumnarSegment>,
col_types: Vec<crate::types::ColumnType>,
current_idx: usize,
num_rows: usize,
/// Pre-computed matching row indices (for filtered scans). If None, scan all.
pub match_filter: Option<Vec<usize>>,
}
impl Iterator for ColumnarScanIterator {
type Item = Vec<crate::types::Value>;
fn next(&mut self) -> Option<Self::Item> {
if let Some(ref matches) = self.match_filter {
// Filtered scan: yield the next matching row. `next()` returns one
// item per call, so we advance current_idx until we find a row to
// emit (skipping nothing here — matches already holds live rows).
if let Some(&row_idx) = matches.get(self.current_idx) {
self.current_idx += 1;
return Some(self.build_row(row_idx));
}
None
} else {
// Full scan: yield all non-deleted rows
while self.current_idx < self.num_rows {
let idx = self.current_idx;
self.current_idx += 1;
if self.row_map.is_deleted(idx) {
continue;
}
return Some(self.build_row(idx));
}
None
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
if let Some(ref m) = self.match_filter {
let rem = m.len().saturating_sub(self.current_idx);
(rem, Some(m.len()))
} else {
let rem = self.num_rows.saturating_sub(self.current_idx);
(rem, Some(self.num_rows))
}
}
}
impl ColumnarScanIterator {
pub(crate) fn build_row(&self, idx: usize) -> Vec<crate::types::Value> {
let mut row = Vec::with_capacity(self.col_types.len());
for (col_idx, ct) in self.col_types.iter().enumerate() {
let val = match &self.segments[col_idx] {
ColumnarSegment::Fixed(f) => match ct {
crate::types::ColumnType::Integer => {
f.get_i64(idx).map(crate::types::Value::Integer)
}
crate::types::ColumnType::Float => {
f.get_f64(idx).map(crate::types::Value::Float)
}
crate::types::ColumnType::Boolean => {
f.get_bool(idx).map(crate::types::Value::Bool)
}
_ => None,
}
.unwrap_or(crate::types::Value::Null),
ColumnarSegment::Text(t) => t
.get_str(idx)
.map(|s| {
crate::types::Value::Text(crate::types::ArcString(std::sync::Arc::from(s)))
})
.unwrap_or(crate::types::Value::Null),
ColumnarSegment::Vector(cols) => cols
.get(idx)
.cloned()
.flatten()
.map(|v| {
crate::types::Value::Vector(crate::types::ArcVec(std::sync::Arc::new(v)))
})
.unwrap_or(crate::types::Value::Null),
ColumnarSegment::Spatial(cols) => cols
.get(idx)
.cloned()
.flatten()
.map(|g| crate::types::Value::Spatial(std::boxed::Box::new(g)))
.unwrap_or(crate::types::Value::Null),
};
row.push(val);
}
row
}
}
/// Unlike `TableRawStreamingIterator` (which copies `bytes.to_vec()` per row),
/// this iterator borrows the shared block Arc data, eliminating the per-row memcpy.
pub struct TableDecodeStreamingIterator {
lsm_iter: crate::storage::lsm::MergingIterator,
decode_ctx: crate::storage::row_format::SchemaDecodeContext,
use_raw: bool,
}
impl TableDecodeStreamingIterator {
/// Decode the next row directly into `out`. Returns `Some(Ok(row_id))` on success.
/// The caller is responsible for clearing `out` before each call (or it appends).
pub fn decode_next_into(
&mut self,
out: &mut Vec<crate::types::Value>,
) -> Option<Result<RowId>> {
if self.use_raw {
loop {
match self.lsm_iter.next_raw() {
Some(Ok((composite_key, _ts, deleted, vb))) => {
if deleted {
continue;
}
if vb.len == 0 {
continue;
}
let row_id = (composite_key & 0xFFFFFFFF) as RowId;
match self.decode_ctx.decode_row_into(out, vb.as_slice()) {
Ok(()) => return Some(Ok(row_id)),
Err(e) => return Some(Err(e)),
}
}
Some(Err(e)) => return Some(Err(e)),
None => return None,
}
}
}
// Standard path (no raw_sst)
loop {
match self.lsm_iter.next() {
Some(Ok((composite_key, value))) => {
if value.deleted {
continue;
}
let row_id = (composite_key & 0xFFFFFFFF) as RowId;
let data = match &value.data {
crate::storage::lsm::ValueData::Inline(bytes) => bytes.as_slice(),
crate::storage::lsm::ValueData::Blob(_) => {
return Some(Err(StorageError::InvalidData(
"Blob references should be resolved by LSM engine".into(),
)));
}
};
match self.decode_ctx.decode_row_into(out, data) {
Ok(()) => return Some(Ok(row_id)),
Err(e) => return Some(Err(e)),
}
}
Some(Err(e)) => return Some(Err(e)),
None => return None,
}
}
}
}
/// Streaming iterator that only decodes specified columns.
/// Owns a reusable decode buffer — no lifetime constraints on the iterator.
pub struct TableRowPartialIterator {
lsm_iter: crate::storage::lsm::MergingIterator,
col_types: Vec<crate::types::ColumnType>,
fixed_count: usize,
col_positions: Vec<usize>,
out_buf: Vec<Value>,
}
impl Iterator for TableRowPartialIterator {
type Item = Result<(RowId, Vec<Value>)>;
fn next(&mut self) -> Option<Self::Item> {
match self.lsm_iter.next() {
Some(Ok((composite_key, value))) => {
let row_id = (composite_key & 0xFFFFFFFF) as RowId;
let data = match &value.data {
crate::storage::lsm::ValueData::Inline(bytes) => bytes.as_slice(),
crate::storage::lsm::ValueData::Blob(_) => {
return Some(Err(StorageError::InvalidData(
"Blob references should be resolved by LSM engine".into(),
)));
}
};
match crate::storage::row_format::decode_fast_partial_into(
data,
&self.col_types,
self.fixed_count,
&self.col_positions,
&mut self.out_buf,
) {
Ok(_) => {
// Take the buffer contents — avoids cloning values.
// out_buf becomes empty Vec (preserving allocation for next row).
let projected = std::mem::take(&mut self.out_buf);
Some(Ok((row_id, projected)))
}
Err(e) => Some(Err(e)),
}
}
Some(Err(e)) => Some(Err(e)),
None => None,
}
}
}
#[cfg(test)]
mod tests {
use crate::types::Value;
use crate::Database;
use tempfile::TempDir;
fn setup() -> (Database, TempDir) {
let dir = TempDir::new().unwrap();
let db = Database::create(dir.path()).unwrap();
db.execute("CREATE TABLE t (id INT PRIMARY KEY, name TEXT, val INT)")
.unwrap();
(db, dir)
}
fn select_rows(db: &Database, sql: &str) -> Vec<Vec<Value>> {
use crate::sql::QueryResult;
match db.execute(sql).unwrap().materialize().unwrap() {
QueryResult::Select { rows, .. } => rows,
other => panic!("Expected Select, got {:?}", other),
}
}
#[test]
fn test_insert_and_select_pk() {
let (db, _dir) = setup();
db.execute("INSERT INTO t VALUES (1, 'alice', 100)")
.unwrap();
let rows = select_rows(&db, "SELECT * FROM t WHERE id = 1");
assert_eq!(rows[0][0], Value::Integer(1));
assert_eq!(
rows[0][1],
Value::Text(crate::types::ArcString::from("alice"))
);
assert_eq!(rows[0][2], Value::Integer(100));
}
#[test]
fn test_insert_many_count() {
let (db, _dir) = setup();
for i in 0..100i64 {
db.execute(&format!(
"INSERT INTO t VALUES ({}, 'n{}', {})",
i,
i,
i * 10
))
.unwrap();
}
assert_eq!(
select_rows(&db, "SELECT COUNT(*) FROM t")[0][0],
Value::Integer(100)
);
}
#[test]
fn test_update_value() {
let (db, _dir) = setup();
db.execute("INSERT INTO t VALUES (1, 'alice', 100)")
.unwrap();
db.execute("UPDATE t SET name = 'bob', val = 200 WHERE id = 1")
.unwrap();
let rows = select_rows(&db, "SELECT name, val FROM t WHERE id = 1");
assert_eq!(
rows[0][0],
Value::Text(crate::types::ArcString::from("bob"))
);
assert_eq!(rows[0][1], Value::Integer(200));
}
#[test]
fn test_delete_removes_row() {
let (db, _dir) = setup();
db.execute("INSERT INTO t VALUES (1, 'a', 1)").unwrap();
db.execute("INSERT INTO t VALUES (2, 'b', 2)").unwrap();
db.execute("DELETE FROM t WHERE id = 1").unwrap();
assert_eq!(
select_rows(&db, "SELECT COUNT(*) FROM t")[0][0],
Value::Integer(1)
);
assert_eq!(select_rows(&db, "SELECT * FROM t WHERE id = 2").len(), 1);
}
#[test]
fn test_insert_null_columns() {
let (db, _dir) = setup();
db.execute("INSERT INTO t VALUES (1, NULL, NULL)").unwrap();
let rows = select_rows(&db, "SELECT * FROM t WHERE id = 1");
assert_eq!(rows[0][1], Value::Null);
assert_eq!(rows[0][2], Value::Null);
}
#[test]
fn test_order_by_asc() {
let (db, _dir) = setup();
for i in 0..10i64 {
db.execute(&format!("INSERT INTO t VALUES ({}, '', {})", i, i * 10))
.unwrap();
}
let rows = select_rows(&db, "SELECT id FROM t ORDER BY id ASC");
for (i, row) in rows.iter().enumerate() {
assert_eq!(row[0], Value::Integer(i as i64));
}
}
#[test]
fn test_partial_column_scan_select() {
let (db, _dir) = setup();
db.execute("INSERT INTO t VALUES (1, 'x', 10)").unwrap();
db.execute("INSERT INTO t VALUES (2, 'y', 20)").unwrap();
let rows = select_rows(&db, "SELECT id, val FROM t ORDER BY id ASC");
assert_eq!(rows.len(), 2);
assert_eq!(rows[0], vec![Value::Integer(1), Value::Integer(10)]);
assert_eq!(rows[1], vec![Value::Integer(2), Value::Integer(20)]);
}
}