1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
use crate::try_lock;
use crate::defer;
use crate::types::{RemDbError, Result, DEFAULT_JSON_SIZE, DEFAULT_TEXT_SIZE};
use crate::DdlExecutor;
use core::default::Default;
use core::ptr::NonNull;
// 引入alloc模块
extern crate alloc;
use alloc::vec::Vec;
#[cfg(feature = "log")]
use crate::log::{error, info, warn};
/// 事务隔离级别
#[derive(PartialEq)]
#[repr(u8)]
pub enum IsolationLevel {
/// 未提交读
ReadUncommitted = 0,
/// 提交读
ReadCommitted = 1,
/// 可重复读
RepeatableRead = 2,
/// 串行化
Serializable = 3,
}
/// 事务类型
#[derive(PartialEq)]
#[repr(u8)]
pub enum TransactionType {
/// 只读事务
ReadOnly = 0,
/// 读写事务
ReadWrite = 1,
}
/// 事务状态
#[derive(PartialEq)]
#[repr(u8)]
pub enum TransactionStatus {
/// 活跃
Active = 0,
/// 已提交
Committed = 1,
/// 已回滚
RolledBack = 2,
/// 已准备
Prepared = 3,
}
/// 事务日志操作类型
#[repr(u8)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum LogOperation {
/// 插入记录
Insert = 0,
/// 删除记录
Delete = 1,
/// 更新记录
Update = 2,
/// 时序数据插入
TimeSeriesInsert = 3,
/// 创建表
CreateTable = 4,
/// 事务提交
Commit = 5,
/// 事务回滚
Abort = 6,
/// 检查点
Checkpoint = 7,
/// 创建索引
CreateIndex = 8,
/// 进入低功耗模式
EnterLowPowerMode = 9,
/// 退出低功耗模式
ExitLowPowerMode = 10,
/// 修改表结构
AlterTable = 11,
/// 删除表
DropTable = 12,
/// 创建数据库
CreateDatabase = 13,
}
/// 日志文件头
#[repr(C)]
#[derive(Copy, Clone)]
pub struct LogHeader {
/// 魔数
pub magic: u32, // 'LOGM'
/// 版本号
pub version: u32,
/// 创建时间戳(微秒)
pub created_at: u64,
/// 日志记录数
pub record_count: u32,
/// 校验和
pub checksum: u32,
/// 压缩类型 (0=None, 1=LZ4, 2=ZSTD)
pub compression_type: u8,
}
/// 事务日志数据块大小(已废弃,保留用于向后兼容)
pub const LOG_DATA_BULK_SIZE: usize = 512;
/// 事务日志项头部(仅包含元数据,不包含数据)
#[repr(C)]
#[derive(Copy, Clone)]
pub struct LogItem {
/// 操作类型
pub op_type: LogOperation,
/// 表ID
pub table_id: u8,
/// 记录ID
pub record_id: u16,
/// 旧数据大小
pub old_data_size: u16,
/// 新数据大小
pub new_data_size: u16,
/// 事务ID
pub tx_id: u32,
/// 时间戳(微秒)
pub timestamp: u64,
/// 校验和
pub checksum: u32,
}
/// 可变大小日志项(唯一支持的WAL格式)
#[derive(Clone)]
pub struct VariableSizeLogItem {
/// 固定头部
pub header: LogItem,
/// 旧数据(用于回滚)
pub old_data: Vec<u8>,
/// 新数据
pub new_data: Vec<u8>,
}
/// 为LogItem实现Default trait
impl Default for LogItem {
fn default() -> Self {
Self {
op_type: LogOperation::Insert,
table_id: 0,
record_id: 0,
old_data_size: 0,
new_data_size: 0,
tx_id: 0,
timestamp: 0,
checksum: 0,
}
}
}
impl Default for VariableSizeLogItem {
fn default() -> Self {
Self {
header: LogItem::default(),
old_data: Vec::new(),
new_data: Vec::new(),
}
}
}
/// 日志检查点
#[repr(C)]
#[derive(Copy, Clone)]
pub struct LogCheckpoint {
/// 检查点时间戳
pub timestamp: u64,
/// 已处理的日志记录数
pub processed_records: u32,
/// 校验和
pub checksum: u32,
}
/// 事务上下文
pub struct Transaction {
/// 事务ID
pub id: u32,
/// 事务类型
pub tx_type: TransactionType,
/// 事务状态
pub status: TransactionStatus,
/// 事务隔离级别
pub isolation_level: IsolationLevel,
/// 开始时间戳(微秒)
pub start_time: u64,
/// 日志项数组(使用 VariableSizeLogItem 以支持数据存储)
log_items: NonNull<VariableSizeLogItem>,
/// 最大日志项数量
max_log_items: usize,
/// 当前日志项数量
log_item_count: usize,
/// 嵌套深度(不支持嵌套事务,固定为1)
pub depth: u8,
/// 自旋锁
lock: u32,
}
/// 为Transaction实现Default trait
impl Default for Transaction {
fn default() -> Self {
unsafe {
Transaction {
id: 0,
tx_type: TransactionType::ReadWrite,
status: TransactionStatus::Active,
isolation_level: IsolationLevel::RepeatableRead,
start_time: 0,
log_items: NonNull::dangling(),
max_log_items: 0,
log_item_count: 0,
depth: 1,
lock: 0,
}
}
}
}
/// 日志缓冲区配置
#[repr(C)]
pub struct LogBufferConfig {
/// 缓冲区大小(日志项数量)
pub size: usize,
/// 刷新阈值(当缓冲区使用率超过此阈值时自动刷新)
pub flush_threshold: usize,
}
/// 日志管理器
pub struct LogManager {
/// 日志文件路径
log_path: &'static str,
/// 日志文件句柄
log_handle: crate::platform::FileHandle,
/// 日志头
header: LogHeader,
/// 检查点
checkpoint: LogCheckpoint,
/// 自旋锁
lock: u32,
/// 日志模式
log_mode: crate::config::LogMode,
/// 日志缓冲区(使用 VariableSizeLogItem 以支持数据存储)
log_buffer: alloc::vec::Vec<VariableSizeLogItem>,
/// 缓冲区配置
buffer_config: LogBufferConfig,
/// 上次刷新时间
last_flush_time: u64,
/// 上次检查点时间
last_checkpoint_time: u64,
/// 检查点间隔
checkpoint_interval_ms: u64,
/// 日志文件大小限制
log_file_size_limit: usize,
/// 日志分段大小
log_segment_size: usize,
/// 当前日志项的偏移量(用于可变大小日志项)
current_log_offset: usize,
/// 恢复时最大连续无效记录数
max_consecutive_invalid: u32,
/// 恢复时跳过预分配空间的阈值
skip_threshold: u32,
/// 恢复时跳过的块大小
skip_block_size: usize,
/// 恢复时最大跳过尝试次数
max_skip_attempts: u32,
/// WAL压缩类型
compression_type: crate::config::WALCompressionType,
/// 压缩级别
compression_level: u8,
}
impl Transaction {
/// 计算可变大小日志项的完整校验和(包括头部和数据部分)
pub unsafe fn calculate_variable_size_log_item_checksum(
var_log_item: &VariableSizeLogItem,
) -> u32 {
let mut checksum = 0u32;
checksum ^= (var_log_item.header.op_type as u32).to_le();
checksum ^= (var_log_item.header.table_id as u32).to_le();
checksum ^= (var_log_item.header.record_id as u32).to_le();
checksum ^= (var_log_item.header.old_data_size as u32).to_le();
checksum ^= (var_log_item.header.new_data_size as u32).to_le();
checksum ^= (var_log_item.header.tx_id as u32).to_le();
checksum ^= (var_log_item.header.timestamp as u32).to_le();
for i in 0..var_log_item.old_data.len() {
checksum ^= (var_log_item.old_data[i] as u32).to_le();
}
for i in 0..var_log_item.new_data.len() {
checksum ^= (var_log_item.new_data[i] as u32).to_le();
}
checksum
}
/// 计算LogItem头部的校验和(不包含数据部分)
pub unsafe fn calculate_log_item_checksum(log_item: &LogItem) -> u32 {
let mut checksum = 0u32;
checksum ^= (log_item.op_type as u32).to_le();
checksum ^= (log_item.table_id as u32).to_le();
checksum ^= (log_item.record_id as u32).to_le();
checksum ^= (log_item.old_data_size as u32).to_le();
checksum ^= (log_item.new_data_size as u32).to_le();
checksum ^= (log_item.tx_id as u32).to_le();
checksum ^= (log_item.timestamp as u32).to_le();
checksum
}
}
impl LogManager {
/// 创建新的日志管理器
pub unsafe fn new(config: &crate::config::DbConfig) -> Result<Self> {
// 构造完整的日志文件路径:log_path目录 + remdb.wal文件名
let log_dir = &config.wal_config.log_path;
// 在no_std环境下使用alloc::format宏
use alloc::format;
let wal_file_path = format!("{}/remdb.wal", log_dir);
// 确保日志目录存在(仅在std环境下)
#[cfg(feature = "std")]
{
use std::fs;
use std::path::Path;
let log_path = Path::new(&log_dir);
if !log_path.exists() {
fs::create_dir_all(log_path).unwrap_or(());
}
}
// 尝试打开日志文件,如果不存在则创建
let log_handle = crate::platform::file_open(
wal_file_path.as_str(),
crate::platform::FileMode::ReadWrite,
)
.map_err(|_| RemDbError::FileIoError)?;
// 获取当前时间
let now = crate::platform::get_timestamp_us();
let now_ms = now / 1000;
let mut manager = LogManager {
log_path: config.wal_config.log_path,
log_handle,
header: LogHeader {
magic: 0x4C4F474D, // 'LOGM'
version: 1,
created_at: now,
record_count: 0,
checksum: 0,
compression_type: match config.wal_config.compression_type {
crate::config::WALCompressionType::None => 0,
crate::config::WALCompressionType::LZ4 => 1,
crate::config::WALCompressionType::ZSTD => 2,
},
},
checkpoint: LogCheckpoint {
timestamp: 0,
processed_records: 0,
checksum: 0,
},
lock: 0,
log_mode: config.wal_config.log_mode,
log_buffer: alloc::vec::Vec::new(), // 默认缓冲区大小1024
buffer_config: LogBufferConfig {
size: 1024,
flush_threshold: 800, // 80%使用率时刷新
},
last_flush_time: now,
last_checkpoint_time: now_ms,
checkpoint_interval_ms: config.wal_config.checkpoint_interval_ms,
log_file_size_limit: config.wal_config.log_file_size_limit,
log_segment_size: config.wal_config.log_segment_size,
current_log_offset: core::mem::size_of::<LogHeader>()
+ core::mem::size_of::<LogCheckpoint>(),
max_consecutive_invalid: config.wal_config.max_consecutive_invalid,
skip_threshold: config.wal_config.skip_threshold,
skip_block_size: config.wal_config.skip_block_size,
max_skip_attempts: config.wal_config.max_skip_attempts,
compression_type: config.wal_config.compression_type,
compression_level: config.wal_config.compression_level,
};
// 预分配缓冲区空间
manager.log_buffer.reserve(1024);
// 读取日志头,如果文件为空或格式不正确则写入新的日志头
let mut header_buffer = [0u8; core::mem::size_of::<LogHeader>()];
let read =
crate::platform::file_read(log_handle, header_buffer.as_mut_ptr(), header_buffer.len())
.map_err(|_| RemDbError::FileIoError)?;
let mut header_valid = false;
if read >= core::mem::size_of::<LogHeader>() {
// 尝试读取日志头
let header = core::ptr::read_unaligned(header_buffer.as_ptr() as *const LogHeader);
// 验证魔数和版本号
if header.magic == 0x4C4F474D && header.version == 1 {
manager.header = header;
// 尝试读取检查点
if manager.read_checkpoint().is_ok() {
header_valid = true;
}
}
}
if !header_valid {
// 文件为空或格式不正确,写入新的日志头
// 直接回到文件开头,准备写入新的日志头
crate::platform::file_seek(log_handle, 0, crate::platform::SeekWhence::SeekSet)
.map_err(|_| RemDbError::FileIoError)?;
// 如果配置了预分配大小,则预分配文件空间
if config.wal_config.log_prealloc_size > 0 {
// 定位到预分配大小位置
crate::platform::file_seek(
log_handle,
config.wal_config.log_prealloc_size as i64 - 1,
crate::platform::SeekWhence::SeekSet,
)
.map_err(|_| RemDbError::FileIoError)?;
// 写入一个字节来扩展文件
let zero_byte = [0u8; 1];
crate::platform::file_write(log_handle, zero_byte.as_ptr(), 1)
.map_err(|_| RemDbError::FileIoError)?;
// 回到文件开头
crate::platform::file_seek(log_handle, 0, crate::platform::SeekWhence::SeekSet)
.map_err(|_| RemDbError::FileIoError)?;
}
// 写入新的日志头
manager.write_header()?;
}
Ok(manager)
}
/// 写入日志头
pub unsafe fn write_header(&mut self) -> Result<()> {
// 计算日志头校验和
let mut header_bytes = [0u8; core::mem::size_of::<LogHeader>()];
core::ptr::write_unaligned(header_bytes.as_mut_ptr() as *mut LogHeader, self.header);
// 清除旧的校验和 (checksum offset = 4 + 4 + 8 + 4 = 20)
let checksum_ptr = header_bytes.as_mut_ptr().add(20) as *mut u32;
*checksum_ptr = 0;
// 计算新的校验和
self.header.checksum = Transaction::calculate_checksum(&header_bytes);
// 重新写入完整的日志头
core::ptr::write_unaligned(header_bytes.as_mut_ptr() as *mut LogHeader, self.header);
// 定位到文件开头
crate::platform::file_seek(self.log_handle, 0, crate::platform::SeekWhence::SeekSet)
.map_err(|_| RemDbError::FileIoError)?;
// 写入日志头
let written =
crate::platform::file_write(self.log_handle, header_bytes.as_ptr(), header_bytes.len())
.map_err(|_| RemDbError::FileIoError)?;
if written != header_bytes.len() {
return Err(RemDbError::FileIoError);
}
Ok(())
}
/// 读取检查点
pub unsafe fn read_checkpoint(&mut self) -> Result<()> {
// 定位到检查点位置(日志头之后)
let checkpoint_offset = core::mem::size_of::<LogHeader>();
crate::platform::file_seek(
self.log_handle,
checkpoint_offset as i64,
crate::platform::SeekWhence::SeekSet,
)
.map_err(|_| RemDbError::FileIoError)?;
// 读取检查点
let mut checkpoint_buffer = [0u8; core::mem::size_of::<LogCheckpoint>()];
let read = crate::platform::file_read(
self.log_handle,
checkpoint_buffer.as_mut_ptr(),
checkpoint_buffer.len(),
)
.map_err(|_| RemDbError::FileIoError)?;
if read == 0 {
// 检查点不存在,使用默认值
self.checkpoint = LogCheckpoint {
timestamp: 0,
processed_records: 0,
checksum: 0,
};
} else {
// 读取检查点
self.checkpoint =
core::ptr::read_unaligned(checkpoint_buffer.as_ptr() as *const LogCheckpoint);
}
Ok(())
}
/// 写入检查点
pub unsafe fn write_checkpoint(&mut self) -> Result<()> {
// 计算检查点校验和
let mut checkpoint_bytes = [0u8; core::mem::size_of::<LogCheckpoint>()];
core::ptr::write_unaligned(
checkpoint_bytes.as_mut_ptr() as *mut LogCheckpoint,
self.checkpoint,
);
// 清除旧的校验和
let checksum_ptr = checkpoint_bytes.as_mut_ptr().add(12) as *mut u32;
*checksum_ptr = 0;
// 计算新的校验和
self.checkpoint.checksum = Transaction::calculate_checksum(&checkpoint_bytes);
// 重新写入完整的检查点
core::ptr::write_unaligned(
checkpoint_bytes.as_mut_ptr() as *mut LogCheckpoint,
self.checkpoint,
);
// 定位到检查点位置
let checkpoint_offset = core::mem::size_of::<LogHeader>();
crate::platform::file_seek(
self.log_handle,
checkpoint_offset as i64,
crate::platform::SeekWhence::SeekSet,
)
.map_err(|_| RemDbError::FileIoError)?;
// 写入检查点
let written = crate::platform::file_write(
self.log_handle,
checkpoint_bytes.as_ptr(),
checkpoint_bytes.len(),
)
.map_err(|_| RemDbError::FileIoError)?;
if written != checkpoint_bytes.len() {
return Err(RemDbError::FileIoError);
}
Ok(())
}
/// 刷新日志缓冲区到磁盘
pub unsafe fn flush_buffer(&mut self) -> Result<()> {
if self.log_buffer.is_empty() {
return Ok(());
}
// 自旋锁保护
crate::platform::spin_lock(&mut self.lock);
// 定位到日志记录区域的末尾
let log_offset = core::mem::size_of::<LogHeader>()
+ core::mem::size_of::<LogCheckpoint>()
+ (self.header.record_count as usize) * core::mem::size_of::<LogItem>();
crate::platform::file_seek(
self.log_handle,
log_offset as i64,
crate::platform::SeekWhence::SeekSet,
)
.map_err(|_| RemDbError::FileIoError)?;
// 批量写入日志项
let buffer_size = self.log_buffer.len();
for i in 0..buffer_size {
let var_log_item = &self.log_buffer[i];
// 计算可变大小日志项的总大小
let header_size = core::mem::size_of::<LogItem>();
let total_size =
header_size + var_log_item.old_data.len() + var_log_item.new_data.len();
// 分配缓冲区
let mut log_bytes = vec![0u8; total_size];
// 写入头部
let header_ptr = log_bytes.as_mut_ptr() as *mut LogItem;
core::ptr::write_unaligned(header_ptr, var_log_item.header);
// 写入旧数据
let old_data_start = header_size;
if !var_log_item.old_data.is_empty() {
log_bytes[old_data_start..old_data_start + var_log_item.old_data.len()]
.copy_from_slice(&var_log_item.old_data);
}
// 写入新数据
let new_data_start = old_data_start + var_log_item.old_data.len();
if !var_log_item.new_data.is_empty() {
log_bytes[new_data_start..new_data_start + var_log_item.new_data.len()]
.copy_from_slice(&var_log_item.new_data);
}
let written =
crate::platform::file_write(self.log_handle, log_bytes.as_ptr(), log_bytes.len())
.map_err(|_| RemDbError::FileIoError)?;
if written != log_bytes.len() {
crate::platform::spin_unlock(&mut self.lock);
return Err(RemDbError::FileIoError);
}
// 更新日志头记录计数
self.header.record_count += 1;
}
// 清空缓冲区
self.log_buffer.clear();
// 更新日志头校验和
self.header.checksum = 0; // 会在write_header中重新计算
// 释放锁,避免在write_header中再次借用冲突
crate::platform::spin_unlock(&mut self.lock);
self.write_header()?;
// 更新上次刷新时间
self.last_flush_time = crate::platform::get_timestamp_us();
Ok(())
}
/// 写入日志项(可变大小版本)
pub unsafe fn write_variable_size_log_item(
&mut self,
var_log_item: &VariableSizeLogItem,
) -> Result<()> {
// 检查是否需要刷新缓冲区或创建检查点
self.check_flush_and_checkpoint()?;
match self.log_mode {
crate::config::LogMode::Sync => {
// 自旋锁保护
crate::platform::spin_lock(&mut self.lock);
// 定位到日志记录区域的末尾(使用当前偏移量)
crate::platform::file_seek(
self.log_handle,
self.current_log_offset as i64,
crate::platform::SeekWhence::SeekSet,
)
.map_err(|_| RemDbError::FileIoError)?;
// 写入日志项头部
let mut log_bytes = [0u8; core::mem::size_of::<LogItem>()];
core::ptr::write_unaligned(
log_bytes.as_mut_ptr() as *mut LogItem,
var_log_item.header,
);
let written = crate::platform::file_write(
self.log_handle,
log_bytes.as_ptr(),
log_bytes.len(),
)
.map_err(|_| RemDbError::FileIoError)?;
if written != log_bytes.len() {
crate::platform::spin_unlock(&mut self.lock);
return Err(RemDbError::FileIoError);
}
// 写入旧数据(压缩)
if var_log_item.header.old_data_size > 0 {
let compressed_old_data = crate::wal_compression::compress_wal_data(
&var_log_item.old_data,
self.compression_type,
self.compression_level,
)?;
// 写入压缩后的大小
let compressed_size = compressed_old_data.len() as u32;
let size_bytes = compressed_size.to_le_bytes();
let size_written = crate::platform::file_write(
self.log_handle,
size_bytes.as_ptr(),
size_bytes.len(),
)
.map_err(|_| RemDbError::FileIoError)?;
if size_written != size_bytes.len() {
crate::platform::spin_unlock(&mut self.lock);
return Err(RemDbError::FileIoError);
}
// 写入压缩后的数据
let old_data_written = crate::platform::file_write(
self.log_handle,
compressed_old_data.as_ptr(),
compressed_old_data.len(),
)
.map_err(|_| RemDbError::FileIoError)?;
if old_data_written != compressed_old_data.len() {
crate::platform::spin_unlock(&mut self.lock);
return Err(RemDbError::FileIoError);
}
}
// 写入新数据(压缩)
if var_log_item.header.new_data_size > 0 {
let compressed_new_data = crate::wal_compression::compress_wal_data(
&var_log_item.new_data,
self.compression_type,
self.compression_level,
)?;
// 写入压缩后的大小
let compressed_size = compressed_new_data.len() as u32;
let size_bytes = compressed_size.to_le_bytes();
let size_written = crate::platform::file_write(
self.log_handle,
size_bytes.as_ptr(),
size_bytes.len(),
)
.map_err(|_| RemDbError::FileIoError)?;
if size_written != size_bytes.len() {
crate::platform::spin_unlock(&mut self.lock);
return Err(RemDbError::FileIoError);
}
// 写入压缩后的数据
let new_data_written = crate::platform::file_write(
self.log_handle,
compressed_new_data.as_ptr(),
compressed_new_data.len(),
)
.map_err(|_| RemDbError::FileIoError)?;
if new_data_written != compressed_new_data.len() {
crate::platform::spin_unlock(&mut self.lock);
return Err(RemDbError::FileIoError);
}
}
// 更新日志头
self.header.record_count += 1;
self.header.checksum = 0;
// 更新当前日志偏移量
let mut data_size = core::mem::size_of::<LogItem>();
if var_log_item.header.old_data_size > 0 {
let compressed_old_data = crate::wal_compression::compress_wal_data(
&var_log_item.old_data,
self.compression_type,
self.compression_level,
)?;
data_size += 4 + compressed_old_data.len();
}
if var_log_item.header.new_data_size > 0 {
let compressed_new_data = crate::wal_compression::compress_wal_data(
&var_log_item.new_data,
self.compression_type,
self.compression_level,
)?;
data_size += 4 + compressed_new_data.len();
}
self.current_log_offset += data_size;
// 释放锁,避免在write_header中再次借用冲突
crate::platform::spin_unlock(&mut self.lock);
self.write_header()?;
// 触发WAL复制
self.replicate_variable_size_wal(var_log_item)?;
// Publish to pubsub
#[cfg(feature = "pubsub")]
self.publish_variable_size_to_pubsub(var_log_item)?;
Ok(())
}
crate::config::LogMode::Async => {
// 异步模式:暂时不支持可变大小日志项的缓冲
// 直接写入磁盘(使用同步写入)
// 自旋锁保护
crate::platform::spin_lock(&mut self.lock);
// 定位到日志记录区域的末尾(使用当前偏移量)
crate::platform::file_seek(
self.log_handle,
self.current_log_offset as i64,
crate::platform::SeekWhence::SeekSet,
)
.map_err(|_| RemDbError::FileIoError)?;
// 写入日志项头部
let mut log_bytes = [0u8; core::mem::size_of::<LogItem>()];
core::ptr::write_unaligned(
log_bytes.as_mut_ptr() as *mut LogItem,
var_log_item.header,
);
let written = crate::platform::file_write(
self.log_handle,
log_bytes.as_ptr(),
log_bytes.len(),
)
.map_err(|_| RemDbError::FileIoError)?;
if written != log_bytes.len() {
crate::platform::spin_unlock(&mut self.lock);
return Err(RemDbError::FileIoError);
}
// 写入旧数据(压缩)
if var_log_item.header.old_data_size > 0 {
let compressed_old_data = crate::wal_compression::compress_wal_data(
&var_log_item.old_data,
self.compression_type,
self.compression_level,
)?;
// 写入压缩后的大小
let compressed_size = compressed_old_data.len() as u32;
let size_bytes = compressed_size.to_le_bytes();
let size_written = crate::platform::file_write(
self.log_handle,
size_bytes.as_ptr(),
size_bytes.len(),
)
.map_err(|_| RemDbError::FileIoError)?;
if size_written != size_bytes.len() {
crate::platform::spin_unlock(&mut self.lock);
return Err(RemDbError::FileIoError);
}
// 写入压缩后的数据
let old_data_written = crate::platform::file_write(
self.log_handle,
compressed_old_data.as_ptr(),
compressed_old_data.len(),
)
.map_err(|_| RemDbError::FileIoError)?;
if old_data_written != compressed_old_data.len() {
crate::platform::spin_unlock(&mut self.lock);
return Err(RemDbError::FileIoError);
}
}
// 写入新数据(压缩)
if var_log_item.header.new_data_size > 0 {
let compressed_new_data = crate::wal_compression::compress_wal_data(
&var_log_item.new_data,
self.compression_type,
self.compression_level,
)?;
// 写入压缩后的大小
let compressed_size = compressed_new_data.len() as u32;
let size_bytes = compressed_size.to_le_bytes();
let size_written = crate::platform::file_write(
self.log_handle,
size_bytes.as_ptr(),
size_bytes.len(),
)
.map_err(|_| RemDbError::FileIoError)?;
if size_written != size_bytes.len() {
crate::platform::spin_unlock(&mut self.lock);
return Err(RemDbError::FileIoError);
}
// 写入压缩后的数据
let new_data_written = crate::platform::file_write(
self.log_handle,
compressed_new_data.as_ptr(),
compressed_new_data.len(),
)
.map_err(|_| RemDbError::FileIoError)?;
if new_data_written != compressed_new_data.len() {
crate::platform::spin_unlock(&mut self.lock);
return Err(RemDbError::FileIoError);
}
}
// 更新日志头
self.header.record_count += 1;
self.header.checksum = 0;
// 更新当前日志偏移量
let mut data_size = core::mem::size_of::<LogItem>();
if var_log_item.header.old_data_size > 0 {
let compressed_old_data = crate::wal_compression::compress_wal_data(
&var_log_item.old_data,
self.compression_type,
self.compression_level,
)?;
data_size += 4 + compressed_old_data.len();
}
if var_log_item.header.new_data_size > 0 {
let compressed_new_data = crate::wal_compression::compress_wal_data(
&var_log_item.new_data,
self.compression_type,
self.compression_level,
)?;
data_size += 4 + compressed_new_data.len();
}
self.current_log_offset += data_size;
// 释放锁,避免在write_header中再次借用冲突
crate::platform::spin_unlock(&mut self.lock);
self.write_header()?;
// 触发WAL复制
self.replicate_variable_size_wal(var_log_item)?;
// Publish to pubsub
#[cfg(feature = "pubsub")]
self.publish_variable_size_to_pubsub(var_log_item)?;
Ok(())
}
}
}
/// 写入日志项(仅支持可变大小格式)
pub unsafe fn write_log_item(&mut self, log_item: &LogItem) -> Result<()> {
let var_log_item = VariableSizeLogItem {
header: *log_item,
old_data: Vec::new(),
new_data: Vec::new(),
};
self.write_variable_size_log_item(&var_log_item)
}
/// 发布WAL日志到pubsub
#[cfg(feature = "pubsub")]
unsafe fn publish_to_pubsub(&self, log_item: &LogItem) -> Result<()> {
use crate::pubsub::topics::*;
// Serialize log_item to bytes
let mut log_bytes = [0u8; core::mem::size_of::<LogItem>()];
core::ptr::write_unaligned(log_bytes.as_mut_ptr() as *mut LogItem, *log_item);
// Only publish to WAL_TOPIC
if let Some(topic_id) = crate::pubsub::get_topic_id(WAL_TOPIC) {
let _ = crate::pubsub::publish(topic_id, &log_bytes);
}
Ok(())
}
/// 发布可变大小WAL日志到pubsub
#[cfg(feature = "pubsub")]
unsafe fn publish_variable_size_to_pubsub(
&self,
var_log_item: &VariableSizeLogItem,
) -> Result<()> {
use crate::pubsub::topics::*;
// 计算总大小
let header_size = core::mem::size_of::<LogItem>();
let total_size = header_size + var_log_item.old_data.len() + var_log_item.new_data.len();
// 分配缓冲区
let mut log_bytes = alloc::vec::Vec::with_capacity(total_size);
// 写入头部
let header_bytes = core::slice::from_raw_parts(
&var_log_item.header as *const LogItem as *const u8,
header_size,
);
log_bytes.extend_from_slice(header_bytes);
// 写入旧数据
log_bytes.extend_from_slice(&var_log_item.old_data);
// 写入新数据
log_bytes.extend_from_slice(&var_log_item.new_data);
// Only publish to WAL_TOPIC
if let Some(topic_id) = crate::pubsub::get_topic_id(WAL_TOPIC) {
let _ = crate::pubsub::publish(topic_id, &log_bytes);
}
Ok(())
}
/// 复制WAL日志到从节点
unsafe fn replicate_wal(&self, log_item: &LogItem) -> Result<()> {
// 尝试获取HA管理器
#[cfg(feature = "ha")]
{
if let Some(ha_manager) = crate::ha::get_ha_manager() {
// 调用HA管理器复制WAL日志
match ha_manager.replicate_wal(log_item) {
Ok(_) => Ok(()),
Err(_) => Ok(()), // 复制失败不影响主节点操作
}
} else {
Ok(()) // HA管理器未初始化,跳过复制
}
}
#[cfg(not(feature = "ha"))]
Ok(()) // HA功能未启用,跳过复制
}
/// 复制可变大小WAL日志到从节点
unsafe fn replicate_variable_size_wal(
&self,
_var_log_item: &VariableSizeLogItem,
) -> Result<()> {
// 尝试获取HA管理器
#[cfg(feature = "ha")]
{
if let Some(_ha_manager) = crate::ha::get_ha_manager() {
// 将可变大小日志项转换为固定大小日志项进行复制
// 这里需要HA管理器支持可变大小日志项
// 暂时跳过复制,避免兼容性问题
Ok(())
} else {
Ok(()) // HA管理器未初始化,跳过复制
}
}
#[cfg(not(feature = "ha"))]
Ok(()) // HA功能未启用,跳过复制
}
/// 检查是否需要刷新缓冲区或创建检查点
pub unsafe fn check_flush_and_checkpoint(&mut self) -> Result<()> {
let now = crate::platform::get_timestamp_us();
let now_ms = now / 1000;
// 检查是否需要刷新缓冲区(超过1秒未刷新)
if (now - self.last_flush_time) > 1_000_000 && !self.log_buffer.is_empty() {
self.flush_buffer()?;
}
// 检查是否需要创建检查点
if (now_ms - self.last_checkpoint_time) >= self.checkpoint_interval_ms {
self.create_checkpoint()?;
}
Ok(())
}
/// 读取可变大小日志项
pub unsafe fn read_variable_size_log_item(&self, index: u32) -> Result<VariableSizeLogItem> {
// 检查索引有效性
if index >= self.header.record_count {
return Err(RemDbError::LogRecordNotFound);
}
// 构造完整的日志文件路径
use alloc::format;
let wal_file_path = format!("{}/remdb.wal", self.log_path);
let handle =
crate::platform::file_open(wal_file_path.as_str(), crate::platform::FileMode::Read)
.map_err(|_| RemDbError::FileIoError)?;
defer! {
let _ = crate::platform::file_close(handle);
};
// 计算可变大小日志项的偏移量
// 需要遍历前面的所有日志项来计算正确的偏移量
let mut log_offset =
core::mem::size_of::<LogHeader>() + core::mem::size_of::<LogCheckpoint>();
// 遍历前面的所有日志项来计算偏移量
for _i in 0..index {
// 读取当前日志项的头部
let current_offset = log_offset;
crate::platform::file_seek(
handle,
current_offset as i64,
crate::platform::SeekWhence::SeekSet,
)
.map_err(|_| RemDbError::FileIoError)?;
// 读取头部
let mut log_bytes = [0u8; core::mem::size_of::<LogItem>()];
let read = crate::platform::file_read(handle, log_bytes.as_mut_ptr(), log_bytes.len())
.map_err(|_| RemDbError::FileIoError)?;
if read != log_bytes.len() {
return Err(RemDbError::FileIoError);
}
let header = core::ptr::read_unaligned(log_bytes.as_ptr() as *const LogItem);
// 更新偏移量:头部大小
log_offset += core::mem::size_of::<LogItem>();
// 计算旧数据大小(包括大小前缀)
if header.old_data_size > 0 {
// 读取压缩大小
let mut compressed_size_bytes = [0u8; 4];
let size_read =
crate::platform::file_read(handle, compressed_size_bytes.as_mut_ptr(), 4)
.map_err(|_| RemDbError::FileIoError)?;
if size_read != 4 {
return Err(RemDbError::FileIoError);
}
let compressed_size = u32::from_le_bytes(compressed_size_bytes) as usize;
log_offset += 4 + compressed_size;
}
// 计算新数据大小(包括大小前缀)
if header.new_data_size > 0 {
// 读取压缩大小
let mut compressed_size_bytes = [0u8; 4];
let size_read =
crate::platform::file_read(handle, compressed_size_bytes.as_mut_ptr(), 4)
.map_err(|_| RemDbError::FileIoError)?;
if size_read != 4 {
return Err(RemDbError::FileIoError);
}
let compressed_size = u32::from_le_bytes(compressed_size_bytes) as usize;
log_offset += 4 + compressed_size;
}
}
// 定位到目标日志项头部位置
crate::platform::file_seek(
handle,
log_offset as i64,
crate::platform::SeekWhence::SeekSet,
)
.map_err(|_| RemDbError::FileIoError)?;
// 读取日志项头部
let mut log_bytes = [0u8; core::mem::size_of::<LogItem>()];
let read = crate::platform::file_read(handle, log_bytes.as_mut_ptr(), log_bytes.len())
.map_err(|_| RemDbError::FileIoError)?;
if read != log_bytes.len() {
return Err(RemDbError::FileIoError);
}
let header = core::ptr::read_unaligned(log_bytes.as_ptr() as *const LogItem);
// 更新偏移量:指向旧数据的起始位置
log_offset += core::mem::size_of::<LogItem>();
// 创建可变大小日志项
let mut var_log_item = VariableSizeLogItem {
header,
old_data: Vec::new(),
new_data: Vec::new(),
};
// 读取旧数据
if header.old_data_size > 0 {
crate::platform::file_seek(
handle,
log_offset as i64,
crate::platform::SeekWhence::SeekSet,
)
.map_err(|_| RemDbError::FileIoError)?;
// 读取压缩大小
let mut compressed_size_bytes = [0u8; 4];
let size_read =
crate::platform::file_read(handle, compressed_size_bytes.as_mut_ptr(), 4)
.map_err(|_| RemDbError::FileIoError)?;
if size_read != 4 {
return Err(RemDbError::FileIoError);
}
let compressed_size = u32::from_le_bytes(compressed_size_bytes) as usize;
var_log_item.old_data.resize(compressed_size, 0);
let old_data_read = crate::platform::file_read(
handle,
var_log_item.old_data.as_mut_ptr(),
compressed_size,
)
.map_err(|_| RemDbError::FileIoError)?;
if old_data_read != compressed_size {
return Err(RemDbError::FileIoError);
}
// 解压缩数据
var_log_item.old_data = crate::wal_compression::decompress_wal_data(
&var_log_item.old_data,
self.compression_type,
)?;
// 更新偏移量:指向新数据的起始位置
log_offset += 4 + compressed_size;
}
// 读取新数据
if header.new_data_size > 0 {
crate::platform::file_seek(
handle,
log_offset as i64,
crate::platform::SeekWhence::SeekSet,
)
.map_err(|_| RemDbError::FileIoError)?;
// 读取压缩大小
let mut compressed_size_bytes = [0u8; 4];
let size_read =
crate::platform::file_read(handle, compressed_size_bytes.as_mut_ptr(), 4)
.map_err(|_| RemDbError::FileIoError)?;
if size_read != 4 {
return Err(RemDbError::FileIoError);
}
let compressed_size = u32::from_le_bytes(compressed_size_bytes) as usize;
var_log_item.new_data.resize(compressed_size, 0);
let new_data_read = crate::platform::file_read(
handle,
var_log_item.new_data.as_mut_ptr(),
compressed_size,
)
.map_err(|_| RemDbError::FileIoError)?;
if new_data_read != compressed_size {
return Err(RemDbError::FileIoError);
}
// 解压缩数据
var_log_item.new_data = crate::wal_compression::decompress_wal_data(
&var_log_item.new_data,
self.compression_type,
)?;
}
// 验证校验和
let calculated_checksum =
Transaction::calculate_variable_size_log_item_checksum(&var_log_item);
if header.checksum != calculated_checksum {
return Err(RemDbError::LogChecksumError);
}
Ok(var_log_item)
}
/// 创建检查点
pub unsafe fn create_checkpoint(&mut self) -> Result<()> {
// 自旋锁保护
crate::platform::spin_lock(&mut self.lock);
// 更新检查点
let now = crate::platform::get_timestamp_us();
self.checkpoint = LogCheckpoint {
timestamp: now,
processed_records: self.header.record_count,
checksum: 0, // 会在write_checkpoint中重新计算
};
// 创建检查点日志项头部
let checkpoint_header = LogItem {
op_type: LogOperation::Checkpoint,
table_id: 0, // 检查点操作不关联特定表
record_id: 0, // 检查点操作不关联特定记录
old_data_size: 0, // 检查点操作没有数据
new_data_size: 0, // 检查点操作没有数据
tx_id: 0, // 检查点操作不关联特定事务
timestamp: now,
checksum: 0, // 后面会计算
};
// 计算校验和:直接基于字段计算,避免结构体填充问题
let calculated_checksum = Transaction::calculate_log_item_checksum(&checkpoint_header);
let mut final_checkpoint_header = checkpoint_header;
final_checkpoint_header.checksum = calculated_checksum;
// 写入检查点日志头部
let log_offset = core::mem::size_of::<LogHeader>()
+ core::mem::size_of::<LogCheckpoint>()
+ (self.header.record_count as usize) * core::mem::size_of::<LogItem>();
crate::platform::file_seek(
self.log_handle,
log_offset as i64,
crate::platform::SeekWhence::SeekSet,
)
.map_err(|_| RemDbError::FileIoError)?;
// 写入日志项头部
let mut log_bytes = [0u8; core::mem::size_of::<LogItem>()];
core::ptr::write_unaligned(
log_bytes.as_mut_ptr() as *mut LogItem,
final_checkpoint_header,
);
let written =
crate::platform::file_write(self.log_handle, log_bytes.as_ptr(), log_bytes.len())
.map_err(|_| RemDbError::FileIoError)?;
if written != log_bytes.len() {
crate::platform::spin_unlock(&mut self.lock);
return Err(RemDbError::FileIoError);
}
// 更新日志头记录计数
self.header.record_count += 1;
self.header.checksum = 0; // 会在write_header中重新计算
// 释放锁,避免在write_checkpoint中再次借用冲突
crate::platform::spin_unlock(&mut self.lock);
// 写入检查点
self.write_checkpoint()?;
// 更新日志头
self.write_header()?;
// 更新上次检查点时间
self.last_checkpoint_time = now / 1000;
Ok(())
}
/// 关闭日志管理器
pub unsafe fn close(&mut self) -> Result<()> {
// 刷新所有缓冲的日志
self.flush_buffer()?;
// 写入最终的检查点
self.create_checkpoint()?;
// 关闭文件句柄
crate::platform::file_close(self.log_handle).map_err(|_| RemDbError::FileIoError)?;
Ok(())
}
/// 恢复日志
pub unsafe fn recover(&self, db: &mut crate::RemDb) -> Result<()> {
use alloc::format;
let wal_file_path = format!("{}/remdb.wal", self.log_path);
let file_size = crate::platform::file_size(wal_file_path.as_str())
.map_err(|_| RemDbError::FileIoError)?;
let header_size = core::mem::size_of::<LogHeader>() + core::mem::size_of::<LogCheckpoint>();
let log_region_size = if file_size > header_size {
file_size - header_size
} else {
0
};
#[cfg(feature = "log")]
info!(
"WAL recovery started: file size = {}, log region size = {}",
file_size, log_region_size
);
let mut valid_log_items = alloc::vec::Vec::new();
let handle = match crate::platform::file_open(
wal_file_path.as_str(),
crate::platform::FileMode::Read,
) {
Ok(handle) => handle,
Err(_) => {
#[cfg(feature = "log")]
warn!("Failed to open log file for recovery, skipping recovery process");
return Ok(());
}
};
let mut current_offset = header_size;
let mut record_index = 0u32;
let mut consecutive_invalid_records = 0u32;
let max_consecutive_invalid = self.max_consecutive_invalid;
let skip_block_size = self.skip_block_size;
let skip_threshold = self.skip_threshold;
let max_skip_attempts = self.max_skip_attempts;
while current_offset < file_size {
if let Err(_) = crate::platform::file_seek(
handle,
current_offset as i64,
crate::platform::SeekWhence::SeekSet,
) {
#[cfg(feature = "log")]
warn!(
"Failed to seek to offset {}, stopping recovery",
current_offset
);
break;
}
const HEADER_SIZE_BYTES: usize = core::mem::size_of::<LogItem>();
let mut log_bytes = [0u8; HEADER_SIZE_BYTES];
let read =
match crate::platform::file_read(handle, log_bytes.as_mut_ptr(), log_bytes.len()) {
Ok(read) => read,
Err(_) => {
#[cfg(feature = "log")]
warn!(
"Failed to read log item header at offset {}, stopping recovery",
current_offset
);
break;
}
};
if read != log_bytes.len() {
#[cfg(feature = "log")]
warn!(
"Incomplete log item header at offset {}, stopping recovery",
current_offset
);
break;
}
let header = core::ptr::read_unaligned(log_bytes.as_ptr() as *const LogItem);
let mut var_log_item = VariableSizeLogItem {
header,
old_data: Vec::new(),
new_data: Vec::new(),
};
let mut data_offset = current_offset + HEADER_SIZE_BYTES;
let mut read_success = true;
// Determine compression type from log header
let compression_type = match self.header.compression_type {
0 => crate::config::WALCompressionType::None,
1 => crate::config::WALCompressionType::LZ4,
2 => crate::config::WALCompressionType::ZSTD,
_ => crate::config::WALCompressionType::None,
};
if header.old_data_size > 0 {
if let Err(_) = crate::platform::file_seek(
handle,
data_offset as i64,
crate::platform::SeekWhence::SeekSet,
) {
read_success = false;
} else {
// Read compressed size (4 bytes) if compression is enabled
if matches!(compression_type, crate::config::WALCompressionType::None) {
var_log_item
.old_data
.resize(header.old_data_size as usize, 0);
let old_data_read = crate::platform::file_read(
handle,
var_log_item.old_data.as_mut_ptr(),
header.old_data_size as usize,
);
if old_data_read
.map(|v| v != header.old_data_size as usize)
.unwrap_or(true)
{
read_success = false;
} else {
data_offset += header.old_data_size as usize;
}
} else {
let mut compressed_size_bytes = [0u8; 4];
let size_read = crate::platform::file_read(
handle,
compressed_size_bytes.as_mut_ptr(),
4,
);
if size_read.map(|v| v != 4).unwrap_or(true) {
read_success = false;
} else {
let compressed_size =
u32::from_le_bytes(compressed_size_bytes) as usize;
var_log_item.old_data.resize(compressed_size, 0);
let old_data_read = crate::platform::file_read(
handle,
var_log_item.old_data.as_mut_ptr(),
compressed_size,
);
if old_data_read.map(|v| v != compressed_size).unwrap_or(true) {
read_success = false;
} else {
// Decompress the data
match crate::wal_compression::decompress_wal_data(
&var_log_item.old_data,
compression_type,
) {
Ok(decompressed) => {
var_log_item.old_data = decompressed;
data_offset += 4 + compressed_size;
}
Err(_) => {
read_success = false;
}
}
}
}
}
}
}
if read_success && header.new_data_size > 0 {
if let Err(_) = crate::platform::file_seek(
handle,
data_offset as i64,
crate::platform::SeekWhence::SeekSet,
) {
read_success = false;
} else {
// Read compressed size (4 bytes) if compression is enabled
if matches!(compression_type, crate::config::WALCompressionType::None) {
var_log_item
.new_data
.resize(header.new_data_size as usize, 0);
let new_data_read = crate::platform::file_read(
handle,
var_log_item.new_data.as_mut_ptr(),
header.new_data_size as usize,
);
if new_data_read
.map(|v| v != header.new_data_size as usize)
.unwrap_or(true)
{
read_success = false;
} else {
data_offset += header.new_data_size as usize;
}
} else {
let mut compressed_size_bytes = [0u8; 4];
let size_read = crate::platform::file_read(
handle,
compressed_size_bytes.as_mut_ptr(),
4,
);
if size_read.map(|v| v != 4).unwrap_or(true) {
read_success = false;
} else {
let compressed_size =
u32::from_le_bytes(compressed_size_bytes) as usize;
var_log_item.new_data.resize(compressed_size, 0);
let new_data_read = crate::platform::file_read(
handle,
var_log_item.new_data.as_mut_ptr(),
compressed_size,
);
if new_data_read.map(|v| v != compressed_size).unwrap_or(true) {
read_success = false;
} else {
// Decompress the data
match crate::wal_compression::decompress_wal_data(
&var_log_item.new_data,
compression_type,
) {
Ok(decompressed) => {
var_log_item.new_data = decompressed;
data_offset += 4 + compressed_size;
}
Err(_) => {
read_success = false;
}
}
}
}
}
}
}
if read_success {
let calculated_checksum =
Transaction::calculate_variable_size_log_item_checksum(&var_log_item);
if header.checksum == calculated_checksum {
let is_zero_initialized = header.tx_id == 0 && header.timestamp == 0;
let is_valid_insert = header.op_type == LogOperation::Insert
&& (header.old_data_size > 0 || header.new_data_size > 0);
let has_valid_data = header.old_data_size > 0 || header.new_data_size > 0;
let is_system_op = matches!(
header.op_type,
LogOperation::CreateTable
| LogOperation::CreateIndex
| LogOperation::Checkpoint
| LogOperation::Commit
| LogOperation::Abort
| LogOperation::EnterLowPowerMode
| LogOperation::ExitLowPowerMode
| LogOperation::AlterTable
);
if !is_zero_initialized || is_valid_insert || has_valid_data || is_system_op {
if matches!(
header.op_type,
LogOperation::Insert
| LogOperation::Delete
| LogOperation::Update
| LogOperation::CreateTable
| LogOperation::CreateDatabase
| LogOperation::TimeSeriesInsert
| LogOperation::Commit
| LogOperation::Abort
| LogOperation::Checkpoint
| LogOperation::CreateIndex
| LogOperation::EnterLowPowerMode
| LogOperation::ExitLowPowerMode
| LogOperation::AlterTable
| LogOperation::DropTable
) {
valid_log_items.push(var_log_item);
current_offset += HEADER_SIZE_BYTES
+ header.old_data_size as usize
+ header.new_data_size as usize;
record_index += 1;
consecutive_invalid_records = 0;
continue;
}
}
}
}
consecutive_invalid_records += 1;
// 如果连续无效记录达到跳过阈值,尝试跳过预分配的未使用空间
if consecutive_invalid_records == skip_threshold {
#[cfg(feature = "log")]
warn!("Detected {} consecutive invalid records, attempting to skip preallocated space at offset {}", consecutive_invalid_records, current_offset);
let mut skip_attempts = 0;
let mut found_valid = false;
// 尝试多次跳过,直到找到有效记录或达到最大尝试次数
while skip_attempts < max_skip_attempts {
let new_offset = current_offset + skip_block_size;
if new_offset >= file_size {
break;
}
#[cfg(feature = "log")]
warn!(
"Skip attempt {}: trying offset {} (+{} bytes)",
skip_attempts + 1,
new_offset,
skip_block_size
);
// 检查新偏移量处是否有有效记录
// 这里我们直接跳过,让主循环的下一次迭代来检查
// 如果跳过后仍然是无效记录,计数器会增加,我们会继续尝试
current_offset = new_offset;
skip_attempts += 1;
// 重置计数器,因为我们已经跳过了这些无效记录
consecutive_invalid_records = 0;
// 继续主循环,让下一次迭代检查新位置
found_valid = true;
break;
}
if found_valid {
continue;
} else {
#[cfg(feature = "log")]
warn!("Failed to find valid records after {} skip attempts, continuing normal recovery", skip_attempts);
}
}
if consecutive_invalid_records > max_consecutive_invalid {
#[cfg(feature = "log")]
error!(
"Too many consecutive invalid records ({}), stopping recovery at offset {}",
consecutive_invalid_records, current_offset
);
break;
}
#[cfg(feature = "log")]
warn!(
"Invalid log item at offset {}, skipping to next record (consecutive invalid: {})",
current_offset, consecutive_invalid_records
);
current_offset += HEADER_SIZE_BYTES;
}
let _ = crate::platform::file_close(handle);
#[cfg(feature = "log")]
info!(
"Phase 1 completed: {} valid log items read",
valid_log_items.len()
);
if valid_log_items.is_empty() {
#[cfg(feature = "log")]
info!("WAL file is empty, no data to recover");
return Err(RemDbError::FileIoError);
}
let mut data_operations_count = 0;
for log_item in &valid_log_items {
match log_item.header.op_type {
LogOperation::Insert | LogOperation::Update | LogOperation::Delete => {
data_operations_count += 1;
}
_ => {}
}
}
// 如果没有有效的数据操作,返回错误
if data_operations_count == 0 {
#[cfg(feature = "log")]
info!("WAL file contains only schema operations, no data operations");
return Err(RemDbError::FileIoError);
}
// 阶段2:先处理所有的数据库创建、表创建和索引创建操作,确保数据库和表结构已建立
#[cfg(feature = "log")]
info!(
"Phase 2: Processing schema operations (CreateDatabase, CreateTable, CreateIndex)..."
);
for log_item in &valid_log_items {
match log_item.header.op_type {
LogOperation::CreateDatabase => {
// 执行创建数据库操作
// 从日志中解析数据库名
let db_name_len = if log_item.new_data.len() > 0 {
log_item.new_data[0] as usize
} else {
continue;
};
let db_name = core::str::from_utf8(&log_item.new_data[1..1 + db_name_len])
.unwrap_or_else(|_| "unknown");
#[cfg(feature = "log")]
info!("Creating database from WAL: {}", db_name);
// 调用数据库管理器的创建数据库方法
let _ = db.database_manager.create_database(db_name, "", None);
}
LogOperation::CreateTable => {
// 正确解析CreateTable日志项,参考lib.rs中的实现
// 从日志中解析表名
let name_len = if log_item.new_data.len() > 0 {
log_item.new_data[0] as usize
} else {
#[cfg(feature = "log")]
warn!("Empty new_data for CreateTable log item, skipping...");
continue;
};
// 检查name_len是否有效且不会导致越界
if name_len == 0 || name_len + 1 > log_item.new_data.len() {
#[cfg(feature = "log")]
warn!(
"Invalid name_len {} for CreateTable log item, skipping...",
name_len
);
continue;
}
let table_name_str = core::str::from_utf8(&log_item.new_data[1..1 + name_len])
.unwrap_or_else(|_| "unknown");
let table_name = Box::leak(table_name_str.to_string().into_boxed_str());
// 参考lib.rs中的实现,使用固定偏移量解析
let new_data_len = log_item.new_data.len();
// 从日志中解析字段数量(固定偏移65)
let field_count = if new_data_len > 65 {
log_item.new_data[65] as usize
} else {
#[cfg(feature = "log")]
warn!("Offset out of bounds when parsing field count, skipping...");
continue;
};
// 从日志中解析主键字段数量(固定偏移66)
let primary_key_count = if new_data_len > 66 {
log_item.new_data[66] as usize
} else {
#[cfg(feature = "log")]
warn!("Offset out of bounds when parsing primary key count, skipping...");
continue;
};
// 从日志中解析主键索引列表(从固定偏移67开始)
let mut primary_key_list = Vec::new();
for i in 0..primary_key_count {
// 检查offset是否超出边界,如果超出则停止解析
let pk_offset = 67 + i;
if pk_offset >= new_data_len {
#[cfg(feature = "log")]
warn!("Offset out of bounds while parsing primary key index, skipping remaining indices...");
break;
}
let pk_idx = log_item.new_data[pk_offset] as usize;
primary_key_list.push(pk_idx);
}
let mut fields = alloc::vec::Vec::with_capacity(field_count);
// 计算字段解析的起始偏移量(固定偏移67 + 主键数量)
let mut offset = 67 + primary_key_count;
// 确保offset初始值不超出边界
if offset >= new_data_len {
#[cfg(feature = "log")]
warn!("Invalid initial offset for CreateTable log item, skipping...");
continue;
}
for _ in 0..field_count {
// 检查offset是否超出边界,如果超出则停止解析
if offset >= new_data_len {
#[cfg(feature = "log")]
warn!("Offset out of bounds while parsing field, skipping remaining fields...");
break;
}
// 解析字段名
let field_name_len = log_item.new_data[offset] as usize;
offset += 1;
if offset + 32 > new_data_len {
#[cfg(feature = "log")]
warn!("Offset out of bounds for field name, skipping field...");
continue;
}
let field_name_str = core::str::from_utf8(
&log_item.new_data[offset..offset + field_name_len],
)
.unwrap_or_else(|_| "unknown");
let field_name = Box::leak(field_name_str.to_string().into_boxed_str());
offset += 32; // 固定32字节字段名空间
// 检查offset是否超出边界
if offset + 3 >= new_data_len {
#[cfg(feature = "log")]
warn!("Offset out of bounds while parsing field type/constraints, skipping field...");
continue;
}
// 解析数据类型
let data_type = crate::types::DataType::from(log_item.new_data[offset]);
offset += 1;
// 解析字段约束
let constraints = log_item.new_data[offset];
offset += 1;
let primary_key_flag = (constraints & 0b0001) != 0;
let not_null_flag = (constraints & 0b0010) != 0;
let unique_flag = (constraints & 0b0100) != 0;
let auto_increment_flag = (constraints & 0b1000) != 0;
// 解析字段大小(4字节)
let field_size = if offset + 4 <= new_data_len {
u32::from_le_bytes([
log_item.new_data[offset],
log_item.new_data[offset + 1],
log_item.new_data[offset + 2],
log_item.new_data[offset + 3],
]) as usize
} else {
// 如果没有字段大小信息,使用默认值
0
};
offset += 4;
// 解析向量维度(2字节)
let vector_dimension = if offset + 1 < new_data_len {
u16::from_le_bytes([
log_item.new_data[offset],
log_item.new_data[offset + 1],
])
} else {
0
};
offset += 2;
// 解析默认值存在标志
let has_default = log_item.new_data[offset] != 0;
offset += 1;
// 解析默认值
let default_value = if has_default {
// 保存当前offset,用于解析默认值
let _default_offset = offset;
// 根据数据类型解析默认值
let value = unsafe {
match data_type {
crate::types::DataType::Bool => {
if offset + 1 <= new_data_len {
let val = log_item.new_data[offset] != 0;
offset += 1;
crate::types::Value { bool: val }
} else {
offset += 1;
crate::types::Value { bool: false }
}
}
crate::types::DataType::Int8 => {
if offset + 1 <= new_data_len {
let val = log_item.new_data[offset] as i8;
offset += 1;
crate::types::Value { i8: val }
} else {
offset += 1;
crate::types::Value { i8: 0 }
}
}
crate::types::DataType::UInt8 => {
if offset + 1 <= new_data_len {
let val = log_item.new_data[offset];
offset += 1;
crate::types::Value { u8: val }
} else {
offset += 1;
crate::types::Value { u8: 0 }
}
}
crate::types::DataType::Int16 => {
if offset + 2 <= new_data_len {
let val = i16::from_le_bytes([
log_item.new_data[offset],
log_item.new_data[offset + 1],
]);
offset += 2;
crate::types::Value { i16: val }
} else {
offset += 2;
crate::types::Value { i16: 0 }
}
}
crate::types::DataType::UInt16 => {
if offset + 2 <= new_data_len {
let val = u16::from_le_bytes([
log_item.new_data[offset],
log_item.new_data[offset + 1],
]);
offset += 2;
crate::types::Value { u16: val }
} else {
offset += 2;
crate::types::Value { u16: 0 }
}
}
crate::types::DataType::Int32 => {
if offset + 4 <= new_data_len {
let val = i32::from_le_bytes([
log_item.new_data[offset],
log_item.new_data[offset + 1],
log_item.new_data[offset + 2],
log_item.new_data[offset + 3],
]);
offset += 4;
crate::types::Value { i32: val }
} else {
offset += 4;
crate::types::Value { i32: 0 }
}
}
crate::types::DataType::UInt32 => {
if offset + 4 <= new_data_len {
let val = u32::from_le_bytes([
log_item.new_data[offset],
log_item.new_data[offset + 1],
log_item.new_data[offset + 2],
log_item.new_data[offset + 3],
]);
offset += 4;
crate::types::Value { u32: val }
} else {
offset += 4;
crate::types::Value { u32: 0 }
}
}
crate::types::DataType::Float32 => {
if offset + 4 <= new_data_len {
let val = f32::from_le_bytes([
log_item.new_data[offset],
log_item.new_data[offset + 1],
log_item.new_data[offset + 2],
log_item.new_data[offset + 3],
]);
offset += 4;
crate::types::Value { float32: val }
} else {
offset += 4;
crate::types::Value { float32: 0.0 }
}
}
crate::types::DataType::Int64 => {
if offset + 8 <= new_data_len {
let val = i64::from_le_bytes([
log_item.new_data[offset],
log_item.new_data[offset + 1],
log_item.new_data[offset + 2],
log_item.new_data[offset + 3],
log_item.new_data[offset + 4],
log_item.new_data[offset + 5],
log_item.new_data[offset + 6],
log_item.new_data[offset + 7],
]);
offset += 8;
crate::types::Value { i64: val }
} else {
offset += 8;
crate::types::Value { i64: 0 }
}
}
crate::types::DataType::UInt64 => {
if offset + 8 <= new_data_len {
let val = u64::from_le_bytes([
log_item.new_data[offset],
log_item.new_data[offset + 1],
log_item.new_data[offset + 2],
log_item.new_data[offset + 3],
log_item.new_data[offset + 4],
log_item.new_data[offset + 5],
log_item.new_data[offset + 6],
log_item.new_data[offset + 7],
]);
offset += 8;
crate::types::Value { u64: val }
} else {
offset += 8;
crate::types::Value { u64: 0 }
}
}
crate::types::DataType::Float64 => {
if offset + 8 <= new_data_len {
let val = f64::from_le_bytes([
log_item.new_data[offset],
log_item.new_data[offset + 1],
log_item.new_data[offset + 2],
log_item.new_data[offset + 3],
log_item.new_data[offset + 4],
log_item.new_data[offset + 5],
log_item.new_data[offset + 6],
log_item.new_data[offset + 7],
]);
offset += 8;
crate::types::Value { float64: val }
} else {
offset += 8;
crate::types::Value { float64: 0.0 }
}
}
crate::types::DataType::VarChar
| crate::types::DataType::Char
| crate::types::DataType::Text => {
// 确保有足够空间读取字符串长度
if offset + 1 <= new_data_len {
let _str_len = log_item.new_data[offset] as usize; // 1字节长度
offset += 1;
// 创建字符串默认值
let mut str_val = [0u8; crate::types::MAX_STRING_LEN];
// 字符串内容固定64字节
let str_data_size = 64;
// 只读取实际需要的字符串数据,不超过剩余空间
let actual_data_size =
if offset + str_data_size <= new_data_len {
str_data_size
} else {
// 空间不足,只读取可用的数据
let remaining = new_data_len - offset;
remaining
};
// 复制字符串数据
for i in 0..actual_data_size {
if i < str_val.len() {
str_val[i] = log_item.new_data[offset + i];
}
}
offset += str_data_size; // 固定64字节字符串空间
crate::types::Value { string: str_val }
} else {
// 空间不足,跳过字符串长度
offset += 1;
offset += 64; // 跳过固定64字节字符串空间
crate::types::Value {
string: [0u8; crate::types::MAX_STRING_LEN],
}
}
}
_ => {
// 默认跳过8字节,但确保不超出边界
if offset + 7 < new_data_len {
offset += 8;
} else {
// 空间不足,只跳过可用的数据
offset = new_data_len;
}
crate::types::Value { u64: 0 }
}
}
};
Some(value)
} else {
// 没有默认值
None
};
// 计算字段大小:优先使用从日志中读取的field_size,如果为0则根据数据类型计算
let final_field_size = if field_size > 0 {
field_size
} else {
match data_type {
crate::types::DataType::Bool
| crate::types::DataType::Int8
| crate::types::DataType::UInt8 => 1,
crate::types::DataType::Int16 | crate::types::DataType::UInt16 => 2,
crate::types::DataType::Int32
| crate::types::DataType::UInt32
| crate::types::DataType::Float32 => 4,
crate::types::DataType::Int64
| crate::types::DataType::UInt64
| crate::types::DataType::Float64
| crate::types::DataType::Timestamp
| crate::types::DataType::TimestampTZ => 8,
crate::types::DataType::VarChar | crate::types::DataType::Char => {
64
} // 默认64字节字符串
crate::types::DataType::Text => DEFAULT_TEXT_SIZE, // TEXT类型默认512字节
crate::types::DataType::Json => DEFAULT_JSON_SIZE, // JSON类型默认512字节
crate::types::DataType::Vector => {
// 向量大小 = 维度 * 4字节(float32)
if vector_dimension > 0 {
vector_dimension as usize * 4
} else {
8 // 默认8字节
}
}
crate::types::DataType::Interval => 10, // 8字节值 + 1字节精度 + 1字节标志
_ => 8, // 默认8字节
}
};
// 创建向量元数据(如果是向量类型且维度大于0)
let vector_metadata = if data_type == crate::types::DataType::Vector
&& vector_dimension > 0
{
Some(crate::types::VectorMetadata {
dimension: vector_dimension,
distance_type: crate::types::DistanceType::L2, // 默认L2距离
index_type: crate::types::VectorIndexType::HNSW, // 默认HNSW索引
compression_enabled: false, // 默认不启用压缩
compression_scheme: 0, // 默认无压缩
compression_level: 3, // 默认压缩级别
hnsw_m: 16,
hnsw_ef_construction: 200,
hnsw_ef_search: 128,
ivf_nlist: 1024,
ivf_nprobe: 16,
})
} else {
None
};
// 创建字段定义
let field_def = crate::types::FieldDef {
name: field_name.to_string(),
data_type,
size: final_field_size,
string_length: None,
offset: 0, // 偏移量会在表创建时计算
primary_key: primary_key_flag,
not_null: not_null_flag,
unique: unique_flag,
auto_increment: auto_increment_flag,
default_value: default_value, // 使用解析出的默认值
vector_metadata: vector_metadata, // 设置向量元数据
json_metadata: None,
};
fields.push(field_def);
}
// 从日志中解析record_size和max_records,但确保不超出边界
let mut record_size = if offset + 1 < log_item.new_data.len() {
u16::from_le_bytes([
log_item.new_data[offset],
log_item.new_data[offset + 1],
]) as usize
} else {
// 超出边界,使用默认值
0
};
offset += 2;
// 如果record_size为0,根据字段大小重新计算
if record_size == 0 {
record_size = fields.iter().fold(0, |acc, field| acc + field.size);
}
let mut max_records = if offset + 3 < log_item.new_data.len() {
u32::from_le_bytes([
log_item.new_data[offset],
log_item.new_data[offset + 1],
log_item.new_data[offset + 2],
log_item.new_data[offset + 3],
]) as usize
} else {
// 超出边界,使用默认值
100000
};
offset += 4;
// 确保max_records至少为1,避免创建无法使用的表
if max_records == 0 {
max_records = 100000; // 使用默认值
}
// 检查并限制max_records,避免内存不足
// 优先使用低功耗模式限制,否则使用默认最大记录数
let max_allowed = if db.config.low_power_mode_supported {
db.config
.low_power_max_records
.unwrap_or(db.config.default_max_records)
} else {
db.config.default_max_records
};
if max_records > max_allowed {
#[cfg(feature = "log")]
info!("Limiting table '{}' max_records from {} to {} to prevent memory exhaustion",
table_name, max_records, max_allowed);
max_records = max_allowed;
}
// 创建表定义
#[cfg(feature = "log")]
info!(
"Creating table from WAL for table_id {} (table name: {})",
log_item.header.table_id, table_name
);
// 计算字段偏移量
let mut offset = 0;
for field in &mut fields {
field.offset = offset;
offset += field.size;
}
// 将字段定义转换为静态切片
// 创建表定义
let table_def = crate::types::TableDef {
id: log_item.header.table_id,
name: table_name.to_string(),
fields: fields,
primary_key: primary_key_list,
secondary_index: None,
secondary_index_type: crate::types::IndexType::SortedArray,
record_size: record_size,
max_records: max_records,
version: 1u32,
created_at: crate::platform::get_timestamp_us(),
updated_at: crate::platform::get_timestamp_us(),
};
// 直接实现从TableDef创建表的逻辑
unsafe {
// 检查表格是否已存在(同时检查ID和表名)
let table_exists_by_id = db.tables.len() > table_def.id as usize
&& db.tables[table_def.id as usize].is_some();
// 检查是否有同名的表(即使ID不同)
let table_exists_by_name = db.tables.iter().any(|t| {
if let Some(table) = t {
table.def.name.as_str() == table_name
} else {
false
}
});
if table_exists_by_id || table_exists_by_name {
#[cfg(feature = "log")]
warn!("Skipping CreateTable operation for table_id {} (table '{}' already exists)", log_item.header.table_id, table_name);
continue;
}
// 确保tables向量有足够的容量
if table_def.id as usize >= db.tables.len() {
let new_capacity =
core::cmp::max(db.tables.len() * 2, table_def.id as usize + 1);
db.tables.resize_with(new_capacity, || None);
db.primary_indices.resize_with(new_capacity, || None);
db.secondary_indices.resize_with(new_capacity, || None);
}
// 创建内存表,跳过状态初始化,由WAL恢复过程处理
let table_def_arc = alloc::sync::Arc::new(table_def.clone());
match crate::table::MemoryTable::new_with_options(
table_def_arc.clone(),
true,
) {
Ok(table) => {
// 添加到表向量
db.tables[table_def.id as usize] = Some(table);
// 创建主键索引
let hash_table_size =
(table_def.max_records * 2).next_power_of_two();
let index_memory_size =
crate::index::PrimaryIndex::calculate_memory_size(
&table_def,
hash_table_size,
table_def.max_records,
);
match crate::memory::allocator::alloc(index_memory_size) {
Ok(index_memory) => {
let hash_table_start = index_memory.as_ptr()
as *mut Option<
core::ptr::NonNull<crate::index::PrimaryIndexItem>,
>;
let items_start = (index_memory.as_ptr() as usize
+ hash_table_size
* core::mem::size_of::<
Option<
core::ptr::NonNull<
crate::index::PrimaryIndexItem,
>,
>,
>(
))
as *mut crate::index::PrimaryIndexItem;
let primary_index = crate::index::PrimaryIndex::new(
table_def_arc.clone(),
hash_table_start,
items_start,
hash_table_size,
table_def.max_records,
);
db.primary_indices[table_def.id as usize] =
Some(primary_index);
// 初始化辅助索引位置
db.secondary_indices[table_def.id as usize] = None;
}
Err(err) => {
#[cfg(feature = "log")]
warn!("Failed to allocate memory for primary index: {:?}, skipping CreateTable operation for table_id {}", err, log_item.header.table_id);
db.tables[table_def.id as usize] = None;
continue;
}
}
}
Err(err) => {
#[cfg(feature = "log")]
warn!("Failed to create MemoryTable: {:?}, skipping CreateTable operation for table_id {}", err, log_item.header.table_id);
continue;
}
}
}
}
LogOperation::CreateIndex => {
// 执行创建索引操作
// 从日志中解析表名和字段名
let table_name_len = if log_item.new_data.len() > 0 {
log_item.new_data[0] as usize
} else {
continue;
};
let table_name =
core::str::from_utf8(&log_item.new_data[1..1 + table_name_len])
.unwrap_or_else(|_| "unknown");
let field_name_len = if log_item.new_data.len() > 65 {
log_item.new_data[65] as usize
} else {
continue;
};
let field_name =
core::str::from_utf8(&log_item.new_data[66..66 + field_name_len])
.unwrap_or_else(|_| "unknown");
let index_type: crate::types::IndexType = if log_item.new_data.len() > 130 {
log_item.new_data[130].into()
} else {
continue;
};
// 调用数据库的create_index方法
let _ = db.create_index(table_name, field_name, index_type);
}
LogOperation::AlterTable => {
// 从日志中解析表名
let table_name_len = if log_item.new_data.len() > 0 {
log_item.new_data[0] as usize
} else {
continue;
};
let table_name =
core::str::from_utf8(&log_item.new_data[1..1 + table_name_len])
.unwrap_or_else(|_| "unknown");
// 从日志中解析操作类型
let op_type = if log_item.new_data.len() > 65 {
log_item.new_data[65] as usize
} else {
continue;
};
// 根据操作类型执行不同的恢复逻辑
let alter_operation = match op_type {
0 => {
// AddColumn
let col_name_len = if log_item.new_data.len() > 67 {
log_item.new_data[67] as usize
} else {
continue;
};
let col_name =
core::str::from_utf8(&log_item.new_data[68..68 + col_name_len])
.unwrap_or_else(|_| "unknown")
.to_string();
let data_type: crate::types::DataType = if log_item.new_data.len() > 132
{
log_item.new_data[132].into()
} else {
continue;
};
let size = if log_item.new_data.len() > 134 {
u16::from_le_bytes([log_item.new_data[133], log_item.new_data[134]])
} else {
continue;
};
let constraints = if log_item.new_data.len() > 135 {
log_item.new_data[135]
} else {
continue;
};
let primary_key = (constraints & 0b0001) != 0;
let not_null = (constraints & 0b0010) != 0;
let unique = (constraints & 0b0100) != 0;
let auto_increment = (constraints & 0b1000) != 0;
crate::AlterTableOperation::AddColumn {
name: col_name,
data_type,
size,
distance_type: None,
default_value: None,
constraints: crate::FieldConstraint {
primary_key,
not_null,
unique,
auto_increment,
},
}
}
1 => {
// DropColumn
let col_name_len = if log_item.new_data.len() > 67 {
log_item.new_data[67] as usize
} else {
continue;
};
let col_name =
core::str::from_utf8(&log_item.new_data[68..68 + col_name_len])
.unwrap_or_else(|_| "unknown")
.to_string();
crate::AlterTableOperation::DropColumn { name: col_name }
}
2 => {
// ModifyColumn
let col_name_len = if log_item.new_data.len() > 67 {
log_item.new_data[67] as usize
} else {
continue;
};
let col_name =
core::str::from_utf8(&log_item.new_data[68..68 + col_name_len])
.unwrap_or_else(|_| "unknown")
.to_string();
let data_type: crate::types::DataType = if log_item.new_data.len() > 132
{
log_item.new_data[132].into()
} else {
continue;
};
let size = if log_item.new_data.len() > 134 {
u16::from_le_bytes([log_item.new_data[133], log_item.new_data[134]])
} else {
continue;
};
let constraints = if log_item.new_data.len() > 135 {
log_item.new_data[135]
} else {
continue;
};
let primary_key = (constraints & 0b0001) != 0;
let not_null = (constraints & 0b0010) != 0;
let unique = (constraints & 0b0100) != 0;
let auto_increment = (constraints & 0b1000) != 0;
crate::AlterTableOperation::ModifyColumn {
name: col_name,
data_type,
size,
distance_type: None,
default_value: None,
constraints: crate::FieldConstraint {
primary_key,
not_null,
unique,
auto_increment,
},
}
}
3 => {
// RenameColumn
let old_name_len = if log_item.new_data.len() > 67 {
log_item.new_data[67] as usize
} else {
continue;
};
let old_name =
core::str::from_utf8(&log_item.new_data[68..68 + old_name_len])
.unwrap_or_else(|_| "unknown")
.to_string();
let new_name_len = if log_item.new_data.len() > 132 {
log_item.new_data[132] as usize
} else {
continue;
};
let new_name =
core::str::from_utf8(&log_item.new_data[133..133 + new_name_len])
.unwrap_or_else(|_| "unknown")
.to_string();
crate::AlterTableOperation::RenameColumn { old_name, new_name }
}
_ => {
// 未知操作类型,跳过
#[cfg(feature = "log")]
warn!(
"Unknown AlterTable operation type {} for table {} from WAL - skipping",
op_type, table_name
);
continue;
}
};
// 执行ALTER TABLE操作
if let Err(err) = DdlExecutor::alter_table(db, table_name, alter_operation) {
#[cfg(feature = "log")]
error!(
"Failed to recover AlterTable operation for table {} from WAL: {:?}",
table_name, err
);
} else {
#[cfg(feature = "log")]
info!(
"Successfully recovered AlterTable operation for table {} from WAL",
table_name
);
}
}
_ => continue, // 跳过非schema操作
}
}
// 阶段3:处理所有的数据操作和系统操作
#[cfg(feature = "log")]
info!("Phase 3: Processing operations (Insert, Update, Delete, TimeSeriesInsert, LowPowerMode)...");
let mut data_operations_count = 0;
for log_item in &valid_log_items {
match log_item.header.op_type {
LogOperation::EnterLowPowerMode => {
// 处理进入低功耗模式日志
#[cfg(feature = "log")]
info!("Processing EnterLowPowerMode log item");
// 检查配置是否支持低功耗模式
if db.config.low_power_mode_supported {
// 设置事务管理器为低功耗模式
crate::transaction::set_low_power_mode(true);
// 如果数据库尚未进入低功耗模式,执行相关操作
if !db.is_low_power_mode() {
// 执行进入低功耗模式的准备工作
unsafe {
// 检查当前内存使用情况
let current_memory = db.config.total_memory;
if current_memory > db.low_power_memory_limit {
// 内存使用超出限制,需要进行优化
db.optimize_memory_usage();
}
}
// 遍历所有表,设置低功耗模式
for table in &mut db.tables.iter_mut() {
if let Some(table) = table {
table.set_low_power_mode(true, db.config.low_power_max_records);
}
}
// 更新状态
db.low_power_mode = true;
}
} else {
// 如果配置不支持低功耗模式,确保事务管理器也处于正常模式
crate::transaction::set_low_power_mode(false);
}
}
LogOperation::ExitLowPowerMode => {
// 处理退出低功耗模式日志
#[cfg(feature = "log")]
info!("Processing ExitLowPowerMode log item");
// 无论配置是否支持低功耗模式,都确保事务管理器处于正常模式
crate::transaction::set_low_power_mode(false);
// 检查配置是否支持低功耗模式
if db.config.low_power_mode_supported {
// 如果数据库当前处于低功耗模式,执行相关操作
if db.is_low_power_mode() {
// 执行退出低功耗模式的准备工作
unsafe {
// 恢复正常的索引更新频率
// 恢复正常的事务日志写入频率
// 检查并扩展内存使用(如果需要)
}
// 遍历所有表,退出低功耗模式
for table in &mut db.tables.iter_mut() {
if let Some(table) = table {
table.set_low_power_mode(false, None);
}
}
// 更新状态
db.low_power_mode = false;
}
} else {
// 如果配置不支持低功耗模式,确保所有表也处于正常模式
for table in &mut db.tables.iter_mut() {
if let Some(table) = table {
table.set_low_power_mode(false, None);
}
}
// 确保数据库状态也更新
db.low_power_mode = false;
}
}
LogOperation::Insert => {
// 执行插入操作
// 检查table_id是否在有效范围内
let table_id = log_item.header.table_id as usize;
if table_id >= db.tables.len() {
// 表可能还未创建,跳过当前日志项
#[cfg(feature = "log")]
warn!("Table ID {} out of bounds (tables.len() = {}), skipping Insert log item", table_id, db.tables.len());
continue;
}
let table = match &mut db.tables[table_id] {
Some(table) => table,
None => {
#[cfg(feature = "log")]
warn!(
"Table ID {} exists but is None, skipping Insert log item",
table_id
);
continue;
}
};
#[cfg(feature = "log")]
info!(
"Processing Insert operation for table_id {} record_id {}",
table_id, log_item.header.record_id
);
data_operations_count += 1;
// 无论记录是否存在,都执行插入操作
let status_ptr = table.get_status_ptr(log_item.header.record_id as usize);
let record_ptr = table.get_record_ptr_mut(log_item.header.record_id as usize);
// 复制数据到记录位置
// 使用实际的记录大小,而不是LOG_DATA_BULK_SIZE
let actual_data_size =
core::cmp::min(log_item.new_data.len(), table.record_size);
crate::platform::memcpy(
record_ptr,
log_item.new_data.as_ptr(),
actual_data_size,
);
// 检查记录是否已存在
if (*status_ptr).status != crate::types::RecordStatus::Used {
// 记录不存在,更新状态和计数
(*status_ptr).status = crate::types::RecordStatus::Used;
(*status_ptr).version += 1;
(*status_ptr).create_tx_id = log_item.header.tx_id;
(*status_ptr).delete_tx_id = 0;
(*status_ptr).next_version_ptr = 0;
table.record_count += 1;
// 从空闲槽栈中移除该槽位,确保不重复使用
if table.free_slot_count > 0 {
// 查找并移除该槽位
let mut found = false;
let mut i = 0;
while i < table.free_slot_count {
if *table.free_slots.as_ptr().add(i)
== log_item.header.record_id as usize
{
// 找到,将最后一个元素移动到当前位置
*table.free_slots.as_ptr().add(i) =
*table.free_slots.as_ptr().add(table.free_slot_count - 1);
table.free_slot_count -= 1;
found = true;
break;
}
i += 1;
}
if !found {
// 如果没有找到,说明可能已经被移除,或者初始状态不对,直接减少free_slot_count
// 但确保不小于0
if table.free_slot_count > 0 {
table.free_slot_count -= 1;
}
}
}
} else {
// 记录已存在,更新版本号和创建事务ID
(*status_ptr).version += 1;
(*status_ptr).create_tx_id = log_item.header.tx_id;
}
// 更新主键索引
if let Some(primary_index) = &mut db.primary_indices[table_id] {
// 先删除旧的索引项(如果存在)
let _: Result<()> = primary_index.delete_composite(record_ptr);
// 使用复合键插入方法
let _: Result<()> = primary_index
.insert_composite(record_ptr, log_item.header.record_id as u16);
}
// 更新表的max_pk值,确保新插入的记录不会覆盖旧记录
// 对于复合主键,只考虑第一个主键字段
if !table.def.primary_key.is_empty() {
let primary_key_field = &table.def.fields[table.def.primary_key[0]];
let key_ptr = record_ptr.add(primary_key_field.offset);
let new_pk = match primary_key_field.data_type {
crate::types::DataType::UInt8 => {
(unsafe { std::ptr::read_unaligned(key_ptr as *const u8) }) as u64
}
crate::types::DataType::UInt16 => {
(unsafe { std::ptr::read_unaligned(key_ptr as *const u16) }) as u64
}
crate::types::DataType::UInt32 => {
(unsafe { std::ptr::read_unaligned(key_ptr as *const u32) }) as u64
}
crate::types::DataType::UInt64 => unsafe {
std::ptr::read_unaligned(key_ptr as *const u64)
},
crate::types::DataType::Int8 => {
(unsafe { std::ptr::read_unaligned(key_ptr as *const i8) }) as u64
}
crate::types::DataType::Int16 => {
(unsafe { std::ptr::read_unaligned(key_ptr as *const i16) }) as u64
}
crate::types::DataType::Int32 => {
(unsafe { std::ptr::read_unaligned(key_ptr as *const i32) }) as u64
}
crate::types::DataType::Int64 => {
(unsafe { std::ptr::read_unaligned(key_ptr as *const i64) }) as u64
}
_ => 0,
};
if new_pk > table.max_pk {
table.max_pk = new_pk;
}
}
}
LogOperation::Delete => {
// 执行删除操作
// 检查table_id是否在有效范围内
let table_id = log_item.header.table_id as usize;
if table_id >= db.tables.len() {
// 表可能还未创建,跳过当前日志项
#[cfg(feature = "log")]
warn!("Table ID {} out of bounds (tables.len() = {}), skipping Delete log item", table_id, db.tables.len());
continue;
}
let table = match &mut db.tables[table_id] {
Some(table) => table,
None => {
#[cfg(feature = "log")]
warn!(
"Table ID {} exists but is None, skipping Delete log item",
table_id
);
continue;
}
};
// 检查记录是否存在
let status_ptr = table.get_status_ptr(log_item.header.record_id as usize);
if (*status_ptr).status == crate::types::RecordStatus::Used {
// 从主键索引中删除
if let Some(primary_index) = &mut db.primary_indices[table_id] {
let record_ptr =
table.get_record_ptr_mut(log_item.header.record_id as usize);
let _: Result<()> = primary_index.delete_composite(record_ptr);
}
// 与实际delete方法保持一致:直接标记为Free
(*status_ptr).status = crate::types::RecordStatus::Free;
(*status_ptr).version += 1;
// 清空记录数据
let record_ptr =
table.get_record_ptr_mut(log_item.header.record_id as usize);
crate::platform::memset(record_ptr, 0, table.record_size);
// 将空闲槽压回栈中,确保不超过数组大小
if table.free_slot_count < table.def.max_records {
*table.free_slots.as_ptr().add(table.free_slot_count) =
log_item.header.record_id as usize;
table.free_slot_count += 1;
}
// 更新记录计数
table.record_count -= 1;
}
}
LogOperation::Update => {
// 执行更新操作
// 检查table_id是否在有效范围内
let table_id = log_item.header.table_id as usize;
if table_id >= db.tables.len() {
// 表可能还未创建,跳过当前日志项
#[cfg(feature = "log")]
warn!("Table ID {} out of bounds (tables.len() = {}), skipping Update log item", table_id, db.tables.len());
continue;
}
let table = match &mut db.tables[table_id] {
Some(table) => table,
None => {
#[cfg(feature = "log")]
warn!(
"Table ID {} exists but is None, skipping Update log item",
table_id
);
continue;
}
};
// 检查记录是否存在
let status_ptr = table.get_status_ptr(log_item.header.record_id as usize);
if (*status_ptr).status == crate::types::RecordStatus::Used {
// 从主键索引中删除旧记录
if let Some(primary_index) = &mut db.primary_indices[table_id] {
let record_ptr =
table.get_record_ptr_mut(log_item.header.record_id as usize);
let _: Result<()> = primary_index.delete_composite(record_ptr);
}
// 记录存在,执行更新
let record_ptr =
table.get_record_ptr_mut(log_item.header.record_id as usize);
let data_size = core::cmp::min(
log_item.header.new_data_size as usize,
log_item.new_data.len(),
);
crate::platform::memcpy(record_ptr, log_item.new_data.as_ptr(), data_size);
(*status_ptr).version += 1;
(*status_ptr).create_tx_id = log_item.header.tx_id;
// 将新记录插入到主键索引中
if let Some(primary_index) = &mut db.primary_indices[table_id] {
let _: Result<()> = primary_index
.insert_composite(record_ptr, log_item.header.record_id as u16);
}
}
}
LogOperation::TimeSeriesInsert => {
// 执行时间序列插入操作
if (log_item.header.table_id as usize) < db.time_series_tables.len() {
let ts_table = match &mut db.time_series_tables
[log_item.header.table_id as usize]
{
Some(table) => table,
None => {
#[cfg(feature = "log")]
warn!("TimeSeries table ID {} exists but is None, skipping TimeSeriesInsert log item", log_item.header.table_id);
continue;
}
};
// 从日志中解析出时间序列记录
let mut record = crate::time_series::TimeSeriesRecord {
timestamp: 0,
value: 0.0,
tag_count: 0,
tags: [0; 8],
};
let data_size = core::cmp::min(
core::mem::size_of::<crate::time_series::TimeSeriesRecord>(),
log_item.new_data.len(),
);
crate::platform::memcpy(
&mut record as *mut _ as *mut u8,
log_item.new_data.as_ptr(),
data_size,
);
// 获取或创建分区
let mut partitions_guard = try_lock!(ts_table.partitions);
let partition = partitions_guard.get_or_create_partition(record.timestamp);
// 写入记录到分区
let mut partition_guard = try_lock!(partition);
partition_guard.records.push(record);
partition_guard.stats.record_count = partition_guard.records.len();
// 更新索引
ts_table
.index
.insert(record.timestamp, partition_guard.records.len() - 1);
}
}
_ => continue, // 跳过非数据操作
}
}
#[cfg(feature = "log")]
info!("WAL recovery completed successfully");
// 检查是否真的恢复了数据(至少有一个数据操作)
if data_operations_count == 0 {
#[cfg(feature = "log")]
info!("WAL file contains only schema operations, no data operations");
return Err(RemDbError::FileIoError);
}
Ok(())
}
}
/// 事务管理器
pub struct TransactionManager {
/// 当前活动事务
current_tx: Option<NonNull<Transaction>>,
/// 事务ID计数器
tx_id_counter: u32,
/// 日志管理器
log_manager: Option<LogManager>,
/// 快照版本号
snapshot_version: u32,
/// 活动快照列表
active_snapshots: Vec<u32>,
/// 低功耗模式标志
low_power_mode: bool,
/// 自旋锁
lock: u32,
}
impl TransactionManager {
/// 创建新的事务管理器
pub const fn new() -> Self {
Self {
current_tx: None,
tx_id_counter: 1,
log_manager: None,
snapshot_version: 0,
active_snapshots: Vec::new(),
low_power_mode: false,
lock: 0,
}
}
/// 设置日志管理器
pub unsafe fn set_log_manager(&mut self, log_manager: LogManager) {
self.log_manager = Some(log_manager);
}
/// 清除日志管理器
pub fn clear_log_manager(&mut self) {
self.log_manager = None;
}
/// 获取日志管理器(可变)
pub unsafe fn get_log_manager_mut(&mut self) -> Option<&mut LogManager> {
self.log_manager.as_mut()
}
/// 获取日志管理器(只读)
pub unsafe fn get_log_manager(&self) -> Option<&LogManager> {
self.log_manager.as_ref()
}
/// 刷新所有日志
pub unsafe fn flush_logs(&mut self) -> Result<()> {
if let Some(log_manager) = &mut self.log_manager {
log_manager.flush_buffer()
} else {
Ok(())
}
}
/// 检查是否有活动事务
pub fn has_active_tx(&self) -> bool {
self.current_tx.is_some()
}
/// 获取当前事务ID计数器
pub fn tx_id_counter(&self) -> u32 {
self.tx_id_counter
}
/// 开始事务
pub unsafe fn begin(
&mut self,
tx_type: TransactionType,
isolation_level: IsolationLevel,
tx_buffer: *mut Transaction,
log_buffer: *mut VariableSizeLogItem,
max_log_items: usize,
) -> Result<NonNull<Transaction>> {
// 自旋锁保护
crate::platform::spin_lock(&mut self.lock);
// 检查是否已有活动事务
if self.current_tx.is_some() {
crate::platform::spin_unlock(&mut self.lock);
return Err(RemDbError::TransactionError);
}
// 生成新的事务ID
let tx_id = self.tx_id_counter;
self.tx_id_counter += 1;
// 检查tx_buffer是否为空
if tx_buffer.is_null() {
crate::platform::spin_unlock(&mut self.lock);
return Err(RemDbError::TransactionError);
}
// 初始化事务
let tx = tx_buffer.as_mut().ok_or(RemDbError::UnexpectedNone(
"Transaction buffer not initialized",
))?;
*tx = Transaction {
id: tx_id,
tx_type,
status: TransactionStatus::Active,
isolation_level,
start_time: crate::platform::get_timestamp_us(),
log_items: NonNull::new(log_buffer).unwrap_or_else(|| NonNull::dangling()),
max_log_items: if log_buffer.is_null() {
0
} else {
max_log_items
},
log_item_count: 0,
depth: 1,
lock: 0,
};
// 设置当前事务
self.current_tx = Some(NonNull::new_unchecked(tx_buffer));
// 解锁
crate::platform::spin_unlock(&mut self.lock);
Ok(NonNull::new_unchecked(tx_buffer))
}
/// 提交事务
pub unsafe fn commit(&mut self) -> Result<()> {
// 自旋锁保护
crate::platform::spin_lock(&mut self.lock);
// 检查是否有活动事务
let tx = match self.current_tx {
Some(tx) => tx,
None => {
crate::platform::spin_unlock(&mut self.lock);
return Err(RemDbError::TransactionError);
}
};
// 保存当前事务指针并清除current_tx,确保无论后续操作是否成功,current_tx都被重置
let mut tx_ptr = tx;
self.current_tx = None;
// 解锁以允许日志写入
crate::platform::spin_unlock(&mut self.lock);
// 提交事务日志项
tx_ptr.as_mut().commit_log_item()?;
// 重新获取锁来更新事务状态
crate::platform::spin_lock(&mut self.lock);
// 更新事务状态
tx_ptr.as_mut().status = TransactionStatus::Committed;
// 解锁
crate::platform::spin_unlock(&mut self.lock);
Ok(())
}
/// 回滚事务
pub unsafe fn rollback(&mut self) -> Result<()> {
// 自旋锁保护
crate::platform::spin_lock(&mut self.lock);
// 检查是否有活动事务
let tx = match self.current_tx {
Some(tx) => tx,
None => {
crate::platform::spin_unlock(&mut self.lock);
return Err(RemDbError::TransactionError);
}
};
// 保存当前事务指针并清除current_tx,确保无论后续操作是否成功,current_tx都被重置
let mut tx_ptr = tx;
self.current_tx = None;
// 解锁以允许回滚操作
crate::platform::spin_unlock(&mut self.lock);
// 执行实际的回滚操作:遍历日志项,按相反顺序撤销操作
for i in (0..tx_ptr.as_mut().log_item_count).rev() {
// SAFETY: log_items pointer is valid and within bounds
let log_item = unsafe { &*tx_ptr.as_mut().log_items.as_ptr().add(i) };
// 获取数据库实例
let db = crate::get_global_db().ok_or(RemDbError::InternalError)?;
match log_item.header.op_type {
LogOperation::Insert => {
// 撤销插入:执行删除操作
let table_id = log_item.header.table_id as usize;
if table_id < db.tables.len() {
if let Some(table) = &mut db.tables[table_id] {
// 检查记录是否存在
let status_ptr =
table.get_status_ptr(log_item.header.record_id as usize);
if (*status_ptr).status == crate::types::RecordStatus::Used {
// 从主键索引中删除
if let Some(primary_index) = &mut db.primary_indices[table_id] {
let record_ptr = table
.get_record_ptr_mut(log_item.header.record_id as usize);
// 使用复合键删除方法
primary_index.delete_composite(record_ptr);
}
// 标记记录为空闲
(*status_ptr).status = crate::types::RecordStatus::Free;
(*status_ptr).version += 1;
// 清空记录数据
let record_ptr =
table.get_record_ptr_mut(log_item.header.record_id as usize);
crate::platform::memset(record_ptr, 0, table.record_size);
// 将空闲槽压回栈中,确保不超过数组大小
if table.free_slot_count < table.def.max_records {
*table.free_slots.as_ptr().add(table.free_slot_count) =
log_item.header.record_id as usize;
table.free_slot_count += 1;
}
// 更新记录计数
table.record_count -= 1;
}
}
}
}
LogOperation::Delete => {
// 撤销删除:执行插入操作,恢复旧数据
let table_id = log_item.header.table_id as usize;
if table_id < db.tables.len() {
if let Some(table) = &mut db.tables[table_id] {
// 检查记录是否空闲
let status_ptr =
table.get_status_ptr(log_item.header.record_id as usize);
if (*status_ptr).status != crate::types::RecordStatus::Used {
// 恢复记录数据
let record_ptr =
table.get_record_ptr_mut(log_item.header.record_id as usize);
crate::platform::memcpy(
record_ptr,
log_item.old_data.as_ptr(),
core::cmp::min(log_item.old_data.len(), table.record_size),
);
// 标记记录为已使用
(*status_ptr).status = crate::types::RecordStatus::Used;
(*status_ptr).version += 1;
(*status_ptr).create_tx_id = log_item.header.tx_id;
(*status_ptr).delete_tx_id = 0;
(*status_ptr).next_version_ptr = 0;
table.record_count += 1;
// 从空闲槽栈中移除该槽位,确保不重复使用
if table.free_slot_count > 0 {
let mut found = false;
let mut i = 0;
while i < table.free_slot_count {
if *table.free_slots.as_ptr().add(i)
== log_item.header.record_id as usize
{
// 找到,将最后一个元素移动到当前位置
*table.free_slots.as_ptr().add(i) = *table
.free_slots
.as_ptr()
.add(table.free_slot_count - 1);
table.free_slot_count -= 1;
found = true;
break;
}
i += 1;
}
}
// 更新主键索引
if let Some(primary_index) = &mut db.primary_indices[table_id] {
let _: Result<()> = primary_index.insert_composite(
record_ptr,
log_item.header.record_id as u16,
);
}
}
}
}
}
LogOperation::Update => {
// 撤销更新:恢复旧数据
let table_id = log_item.header.table_id as usize;
if table_id < db.tables.len() {
if let Some(table) = &mut db.tables[table_id] {
// 检查记录是否存在
let status_ptr =
table.get_status_ptr(log_item.header.record_id as usize);
if (*status_ptr).status == crate::types::RecordStatus::Used {
// 从主键索引中删除旧记录(当前更新后的数据)
if let Some(primary_index) = &mut db.primary_indices[table_id] {
let record_ptr = table
.get_record_ptr_mut(log_item.header.record_id as usize);
let _: Result<()> = primary_index.delete_composite(record_ptr);
}
// 恢复旧数据
let record_ptr =
table.get_record_ptr_mut(log_item.header.record_id as usize);
crate::platform::memcpy(
record_ptr,
log_item.old_data.as_ptr(),
core::cmp::min(log_item.old_data.len(), table.record_size),
);
// 更新版本号和创建事务ID
(*status_ptr).version += 1;
(*status_ptr).create_tx_id = log_item.header.tx_id;
// 将恢复后的数据插入到主键索引中
if let Some(primary_index) = &mut db.primary_indices[table_id] {
let _: Result<()> = primary_index.insert_composite(
record_ptr,
log_item.header.record_id as u16,
);
}
}
}
}
}
_ => {
// 其他操作类型不需要回滚
continue;
}
}
}
// 更新事务状态为回滚中
tx_ptr.as_mut().status = TransactionStatus::RolledBack;
Ok(())
}
/// 检查记录是否对当前事务可见(MVCC实现)
pub fn is_visible(&self, create_tx_id: u32, delete_tx_id: u32, current_tx_id: u32) -> bool {
// MVCC可见性规则:
// 1. 记录是由当前事务创建的,对当前事务可见
// 2. 记录是由已提交事务创建的,并且未被删除或被当前事务删除
// 3. 记录是由已提交事务创建的,并且删除事务尚未提交
// 如果当前事务是创建者,记录可见
if create_tx_id == current_tx_id {
return true;
}
// 如果记录已被删除,并且删除事务已提交,记录不可见
if delete_tx_id > 0 && delete_tx_id < current_tx_id {
return false;
}
// 如果记录是由已提交事务创建的,并且未被删除,记录可见
if create_tx_id < current_tx_id {
return true;
}
// 其他情况,记录不可见
false
}
/// 获取当前活动事务
pub fn get_current_tx(&self) -> Option<NonNull<Transaction>> {
self.current_tx
}
/// 设置低功耗模式
pub fn set_low_power_mode(&mut self, enabled: bool) {
self.low_power_mode = enabled;
}
/// 获取低功耗模式状态
pub fn is_low_power_mode(&self) -> bool {
self.low_power_mode
}
/// 重置事务管理器状态
pub fn reset(&mut self) {
// 先重置锁状态,防止上一次测试中锁未正确释放导致死锁
unsafe {
core::sync::atomic::AtomicU32::from_ptr(&mut self.lock as *mut u32)
.store(0, core::sync::atomic::Ordering::Release);
}
// 自旋锁保护
crate::platform::spin_lock(&mut self.lock);
// 清除所有活动事务
self.current_tx = None;
self.active_snapshots.clear();
// 重置事务计数
self.tx_id_counter = 1; // 从1开始,避免与初始值0冲突
self.snapshot_version = 0;
// 解锁
crate::platform::spin_unlock(&mut self.lock);
}
}
impl Transaction {
/// 提交事务日志项
pub unsafe fn commit_log_item(&mut self) -> Result<()> {
// 检查是否有活动事务
if self.status != TransactionStatus::Active {
return Err(RemDbError::TransactionError);
}
// 如果没有日志项,直接返回成功
if self.log_item_count == 0 {
return Ok(());
}
// 遍历所有日志项
for i in 0..self.log_item_count {
// SAFETY: log_items pointer is valid and within bounds
let log_item = unsafe { &*self.log_items.as_ptr().add(i) };
// 写入日志项到日志管理器
if let Some(log_manager) = crate::transaction::TX_MANAGER.get_log_manager_mut() {
// 创建可变大小的日志项并写入
// 注意:系统已完全支持可变大小日志,begin_log_item不会截断数据
let var_log_item = VariableSizeLogItem {
header: log_item.header,
old_data: log_item.old_data.clone(),
new_data: log_item.new_data.clone(),
};
log_manager.write_variable_size_log_item(&var_log_item)?;
}
}
Ok(())
}
/// 开始事务日志项(可变大小版本)
pub unsafe fn begin_variable_size_log_item(
&mut self,
tx_id: u32,
op_type: LogOperation,
table_id: u8,
record_id: u16,
old_data: Option<&[u8]>,
new_data: Option<&[u8]>,
) -> VariableSizeLogItem {
let old_data_size = old_data.map_or(0, |d| d.len() as u16);
let new_data_size = new_data.map_or(0, |d| d.len() as u16);
let header = LogItem {
op_type,
table_id,
record_id,
old_data_size,
new_data_size,
tx_id,
timestamp: crate::platform::get_timestamp_us(),
checksum: 0,
};
let old_data_vec = old_data.map_or(Vec::new(), |d| d.to_vec());
let new_data_vec = new_data.map_or(Vec::new(), |d| d.to_vec());
let mut var_log_item = VariableSizeLogItem {
header,
old_data: old_data_vec,
new_data: new_data_vec,
};
// 计算校验和:直接基于字段计算,避免结构体填充问题
let calculated_checksum =
Transaction::calculate_variable_size_log_item_checksum(&var_log_item);
var_log_item.header.checksum = calculated_checksum;
var_log_item
}
/// 开始事务日志项(使用 VariableSizeLogItem 以支持数据存储)
pub unsafe fn begin_log_item(
&mut self,
tx_id: u32,
op_type: LogOperation,
table_id: u8,
record_id: u16,
_data_size: u16,
old_data: Option<&[u8]>,
new_data: Option<&[u8]>,
) -> Option<NonNull<VariableSizeLogItem>> {
if self.log_item_count >= self.max_log_items {
return None;
}
let log_item_ptr = self.log_items.as_ptr().add(self.log_item_count);
// SAFETY: log_item_ptr is valid and non-null
let log_item = unsafe { log_item_ptr.as_mut() }.unwrap();
// 初始化日志项头部
log_item.header.op_type = op_type;
log_item.header.table_id = table_id;
log_item.header.record_id = record_id;
log_item.header.old_data_size = old_data.map_or(0, |d| d.len() as u16);
log_item.header.new_data_size = new_data.map_or(0, |d| d.len() as u16);
log_item.header.tx_id = tx_id;
log_item.header.timestamp = crate::platform::get_timestamp_us();
log_item.header.checksum = 0;
// 复制数据到日志项
if let Some(data) = old_data {
log_item.old_data = data.to_vec();
} else {
log_item.old_data = Vec::new();
}
if let Some(data) = new_data {
log_item.new_data = data.to_vec();
} else {
log_item.new_data = Vec::new();
}
// 计算校验和
let calculated_checksum = Transaction::calculate_variable_size_log_item_checksum(log_item);
log_item.header.checksum = calculated_checksum;
self.log_item_count += 1;
Some(NonNull::new_unchecked(log_item_ptr))
}
/// 检查事务是否活跃
pub fn is_active(&self) -> bool {
self.status == TransactionStatus::Active
}
/// 检查事务是否为只读
pub fn is_read_only(&self) -> bool {
self.tx_type == TransactionType::ReadOnly
}
/// 计算数据校验和
pub fn calculate_checksum(data: &[u8]) -> u32 {
let mut checksum = 0u32;
let mut i = 0;
// 按4字节为单位计算校验和
while i + 4 <= data.len() {
let value = u32::from_le_bytes([data[i], data[i + 1], data[i + 2], data[i + 3]]);
checksum ^= value;
i += 4;
}
// 处理剩余的字节
while i < data.len() {
checksum ^= data[i] as u32;
i += 1;
}
checksum
}
}
/// 事务管理器全局实例
static mut TX_MANAGER: TransactionManager = TransactionManager::new();
/// 获取全局事务管理器
pub fn get_tx_manager() -> &'static mut TransactionManager {
unsafe { &mut TX_MANAGER }
}
/// 初始化事务管理器
pub fn init_tx_manager() {
unsafe {
TX_MANAGER.reset();
}
}
/// 刷新所有日志
pub unsafe fn flush_all_logs() -> Result<()> {
TX_MANAGER.flush_logs()
}
/// 设置全局日志管理器
pub unsafe fn set_log_manager(log_manager: LogManager) {
TX_MANAGER.set_log_manager(log_manager);
}
/// 获取全局日志管理器
pub fn get_log_manager() -> Option<&'static mut LogManager> {
unsafe { TX_MANAGER.get_log_manager_mut() }
}
/// 重置全局日志管理器
pub fn reset_log_manager() {
unsafe {
TX_MANAGER.clear_log_manager();
}
}
/// 设置低功耗模式
pub fn set_low_power_mode(enabled: bool) {
unsafe {
TX_MANAGER.set_low_power_mode(enabled);
}
}
/// 获取低功耗模式状态
pub fn is_low_power_mode() -> bool {
unsafe { TX_MANAGER.is_low_power_mode() }
}
/// 检查是否有活动事务
pub fn has_active_tx() -> bool {
unsafe { TX_MANAGER.has_active_tx() }
}
/// 获取当前事务
pub unsafe fn get_current_tx() -> Option<NonNull<Transaction>> {
TX_MANAGER.get_current_tx()
}
/// 开始事务
pub unsafe fn begin(
tx_type: TransactionType,
isolation_level: IsolationLevel,
tx_buffer: *mut Transaction,
log_buffer: *mut VariableSizeLogItem,
max_log_items: usize,
) -> Result<NonNull<Transaction>> {
TX_MANAGER.begin(
tx_type,
isolation_level,
tx_buffer,
log_buffer,
max_log_items,
)
}
/// 简化的事务开始函数,用于SQL查询
pub unsafe fn begin_transaction() {
// 使用Box::leak创建静态缓冲区,确保生命周期足够长
static mut TX_BUFFER: Option<Box<Transaction>> = None;
static mut LOG_BUFFER: Option<Box<[VariableSizeLogItem]>> = None;
// 初始化缓冲区(仅第一次调用时)
if TX_BUFFER.is_none() {
TX_BUFFER = Some(Box::new(Transaction::default()));
}
if LOG_BUFFER.is_none() {
LOG_BUFFER = Some(vec![VariableSizeLogItem::default(); 1024].into_boxed_slice());
}
// 默认使用读写事务和可重复读隔离级别
let _ = TX_MANAGER.begin(
TransactionType::ReadWrite,
IsolationLevel::RepeatableRead,
&mut **TX_BUFFER.as_mut().unwrap(),
LOG_BUFFER.as_mut().unwrap().as_mut_ptr(),
1024,
);
}
/// 提交事务
pub unsafe fn commit() -> Result<()> {
TX_MANAGER.commit()
}
/// 简化的事务提交函数,用于SQL查询
pub unsafe fn commit_transaction() {
let _ = TX_MANAGER.commit();
}
/// 回滚事务
pub unsafe fn rollback() -> Result<()> {
TX_MANAGER.rollback()
}
/// 简化的事务回滚函数,用于SQL查询
pub unsafe fn rollback_transaction() {
let _ = TX_MANAGER.rollback();
}
/// 检查记录是否对当前事务可见(MVCC实现)
pub fn is_visible(create_tx_id: u32, delete_tx_id: u32, current_tx_id: u32) -> bool {
unsafe { TX_MANAGER.is_visible(create_tx_id, delete_tx_id, current_tx_id) }
}
/// 获取当前事务ID计数器
pub fn tx_id_counter() -> u32 {
unsafe { TX_MANAGER.tx_id_counter() }
}