surrealkv 0.21.0

A low-level, versioned, embedded, ACID-compliant, key-value database for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
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
use std::collections::{HashMap, HashSet};
use std::fs::File;
use std::sync::atomic::AtomicU64;
use std::sync::{Arc, RwLock};

use tempfile::TempDir;
use test_log::test;

use crate::clock::MockLogicalClock;
use crate::compaction::compactor::{CompactionOptions, Compactor};
use crate::compaction::leveled::{CompactionPriority, Strategy};
use crate::compaction::{CompactionChoice, CompactionStrategy};
use crate::comparator::{BytewiseComparator, InternalKeyComparator};
use crate::error::{BackgroundErrorHandler, Result};
use crate::iter::CompactionIterator;
use crate::levels::{write_manifest_to_disk, Level, LevelManifest, Levels};
use crate::memtable::ImmutableMemtables;
use crate::snapshot::SnapshotTracker;
use crate::sstable::table::{Table, TableFormat, TableWriter};
use crate::vlog::ValueLocation;
use crate::{CompressionType, InternalKey, InternalKeyKind, Key, LSMIterator, Options, Value};

/// Test environment setup helpers
struct TestEnv {
	#[allow(unused)]
	temp_dir: TempDir,
	options: Arc<Options>,
}

impl TestEnv {
	fn new() -> Self {
		Self::new_with_levels(4) // Default to 4 levels
	}

	fn new_with_levels(level_count: u8) -> Self {
		let temp_dir = TempDir::new().unwrap();
		let table_dir = temp_dir.path().join("sstables");
		std::fs::create_dir_all(&table_dir).unwrap();

		let options = Arc::new(Options {
			path: temp_dir.path().to_path_buf(),
			level_count,
			max_memtable_size: 1024 * 1024, // 1MB
			..Default::default()
		});

		Self {
			temp_dir,
			options,
		}
	}

	fn create_test_table(
		&self,
		id: u64,
		entries: Vec<(InternalKey, Vec<u8>)>,
	) -> Result<Arc<Table>> {
		let table_path = self.options.sstable_file_path(id);
		let file = File::create(&table_path)?;

		// Create a TableWriter
		let mut writer = TableWriter::new(file, id, Arc::clone(&self.options), 0); // Test table, use L0

		// Add entries to the table
		for (key, value) in entries {
			writer.add(key, &value)?;
		}

		// Finish writing the table
		writer.finish()?;

		// Open the table
		let file = std::fs::File::open(&table_path)?;
		let file_size = file.metadata()?.len();
		let file = Arc::new(file);

		// Create and return the table
		let table = Table::new(id, Arc::clone(&self.options), file, file_size)?;

		Ok(Arc::new(table))
	}
}

/// Helper function to create a comparator for testing
fn create_comparator() -> Arc<InternalKeyComparator> {
	Arc::new(InternalKeyComparator::new(Arc::new(BytewiseComparator::default())))
}

/// Helper function to create encoded inline values for testing
fn create_inline_value(value: &[u8]) -> Vec<u8> {
	let location = ValueLocation::with_inline_value(value.to_vec());
	location.encode()
}

/// Helper function to create test entries with automatic value encoding
fn create_test_entries(
	min_key: u64,
	max_key: u64,
	min_seq: u64,
	value_prefix: &str,
) -> Vec<(InternalKey, Vec<u8>)> {
	let mut entries = Vec::new();
	for key_val in min_key..=max_key {
		let user_key = format!("key-{key_val:010}").into_bytes();
		let key =
			InternalKey::new(user_key, min_seq + (key_val - min_key), InternalKeyKind::Set, 0);
		let value = format!("{value_prefix}-{key_val}").into_bytes();
		let encoded_value = create_inline_value(&value);
		entries.push((key, encoded_value));
	}
	entries
}

/// Helper function to create ordered entries with automatic value encoding
fn create_ordered_entries(
	key_prefix: &str,
	start: u32,
	count: u32,
	seq_num: u64,
	value_prefix: Option<&str>,
) -> Vec<(InternalKey, Vec<u8>)> {
	let mut entries = Vec::new();
	let value_prefix = value_prefix.unwrap_or("value");

	for i in 0..count {
		let key = format!("{}-{:05}", key_prefix, start + i).into_bytes();
		let value = format!("{}-{:05}", value_prefix, start + i).into_bytes();
		let internal_key = InternalKey::new(key, seq_num + i as u64, InternalKeyKind::Set, 0);
		let encoded_value = create_inline_value(&value);
		entries.push((internal_key, encoded_value));
	}
	entries
}

/// Creates key-value entries for testing
fn create_entries(min_key: u64, max_key: u64, min_seq: u64) -> Vec<(InternalKey, Vec<u8>)> {
	create_test_entries(min_key, max_key, min_seq, "value")
}

/// Helper function to create Options compatible with old Strategy::new(level0_trigger, multiplier)
/// This approximates the old count-based behavior with bytes-based limits
fn create_options_with_compaction_settings(
	base_opts: &Options,
	level0_trigger: usize,
	multiplier: f64,
) -> Arc<Options> {
	let mut opts = (*base_opts).clone();
	opts.level0_max_files = level0_trigger;
	opts.max_bytes_for_level = 1024;
	opts.level_multiplier = multiplier;
	Arc::new(opts)
}

/// Helper function to create a Strategy with a specific compaction priority for tests
fn create_strategy_with_priority(opts: &Options, priority: CompactionPriority) -> Strategy {
	Strategy::from_options_with_priority(Arc::new(opts.clone()), priority)
}

/// Creates test manifest with tables at specified levels
fn create_test_manifest(
	env: &TestEnv,
	level_tables: Vec<Vec<(u64, u64, u64, u64)>>, // id, min_seq, min_key, max_key
) -> Result<Arc<RwLock<LevelManifest>>> {
	let manifest_path = env.options.path.join("test_manifest");

	// Initialize empty levels
	let level_count = level_tables.len();
	let mut levels = Levels::new(level_count, 10);

	// Create and add tables to levels
	let mut max_table_id = 0;

	for (level_idx, level_specs) in level_tables.iter().enumerate() {
		for &(id, min_seq, min_key, max_key) in level_specs {
			max_table_id = std::cmp::max(max_table_id, id);

			let entries = create_entries(min_key, max_key, min_seq);
			let table = env.create_test_table(id, entries)?;

			// Add table to the appropriate level
			Arc::make_mut(&mut levels.get_levels_mut()[level_idx]).insert(table);
		}
	}

	// Choose a safe value for next_table_id
	let next_table_id = max_table_id + 1000;

	// Create the manifest with next_table_id
	let manifest = LevelManifest {
		path: manifest_path,
		levels,
		hidden_set: HashSet::new(),
		next_table_id: Arc::new(AtomicU64::new(next_table_id)),
		manifest_format_version: crate::levels::MANIFEST_FORMAT_VERSION_V1,
		snapshots: Vec::new(),
		log_number: 0,
		last_sequence: 0,
	};

	// Write the manifest to disk
	write_manifest_to_disk(&manifest)?;

	Ok(Arc::new(RwLock::new(manifest)))
}

/// Creates compaction options for testing
fn create_compaction_options(
	opts: Arc<Options>,
	manifest: Arc<RwLock<LevelManifest>>,
) -> CompactionOptions {
	std::fs::create_dir_all(opts.vlog_dir()).unwrap();

	let vlog = Arc::new(crate::vlog::VLog::new(Arc::clone(&opts)).unwrap());

	CompactionOptions {
		lopts: opts,
		level_manifest: manifest,
		immutable_memtables: Arc::new(RwLock::new(ImmutableMemtables::default())),
		vlog: Some(vlog),
		error_handler: Arc::new(BackgroundErrorHandler::new()),
		snapshot_tracker: SnapshotTracker::new(),
		versioned_index: None,
	}
}

/// Verifies all expected key-value pairs are present after compaction
fn verify_keys_after_compaction(
	manifest: &RwLock<LevelManifest>,
	expected_keys: &HashSet<(Key, Value)>,
) -> (usize, HashMap<Key, Value>) {
	let manifest_guard = manifest.read().unwrap();
	let levels = manifest_guard.levels.get_levels();

	// Build a map of all key-value pairs from all tables across all levels
	let mut all_key_values = HashMap::new();
	let mut count = 0;

	for level in levels {
		for table in &level.tables {
			let mut iter = table.iter(None).unwrap();
			iter.seek_first().unwrap();
			while iter.valid() {
				let key = iter.key().to_owned().user_key.clone();
				let value = iter.value_encoded().unwrap().to_vec();
				count += 1;
				all_key_values.insert(key, value);
				iter.next().unwrap();
			}
		}
	}

	// Check if we found all expected keys
	let mut missing_keys = Vec::new();
	for (expected_key, expected_value) in expected_keys {
		if let Some(actual_value) = all_key_values.get(expected_key) {
			// Verify value matches
			assert_eq!(actual_value, expected_value, "Value mismatch for key {expected_key:?}");
		} else {
			missing_keys.push(expected_key.clone());
		}
	}

	assert_eq!(missing_keys.len(), 0, "Missing keys after compaction: {missing_keys:?}");

	(count, all_key_values)
}

/// Verifies that all expected keys are present with correct values
fn verify_all_keys_present(
	manifest: &RwLock<LevelManifest>,
	expected_keys: &HashMap<Key, Value>,
) -> bool {
	let manifest_guard = manifest.read().unwrap();

	// Build map of all keys found after compaction
	let mut all_key_values = HashMap::new();
	let levels = manifest_guard.levels.get_levels();

	// Collect all keys from all tables across all levels
	for level in levels {
		for table in &level.tables {
			let mut iter = table.iter(None).unwrap();
			iter.seek_first().unwrap();

			while iter.valid() {
				let key = iter.key().to_owned().user_key.clone();
				let value = iter.value_encoded().unwrap().to_vec();
				all_key_values.insert(key, value);
				iter.next().unwrap();
			}
		}
	}

	// Verify all expected keys are present with correct values
	let mut all_keys_found = true;
	for (expected_key, expected_value) in expected_keys {
		if let Some(actual_value) = all_key_values.get(expected_key) {
			if actual_value != expected_value {
				println!("Value mismatch for key {expected_key:?}");
				all_keys_found = false;
			}
		} else {
			println!("Missing key {expected_key:?}");
			all_keys_found = false;
		}
	}

	all_keys_found
}

/// Performs N rounds of compaction
fn perform_compaction_rounds(compactor: &Compactor, rounds: usize) {
	for i in 1..=rounds {
		let result = compactor.compact();
		assert!(result.is_ok(), "Compaction round {} failed: {:?}", i, result.err());
		println!("Compaction round {i} completed");
	}
}

#[test]
fn test_level_selection() {
	let env = TestEnv::new();

	// Define tables for each level
	let level_tables = vec![
		// L0: 5 tables (exceeds limit of 4)
		vec![
			(1, 100, 10, 20), // id, min_seq, min_key, max_key
			(2, 110, 15, 25),
			(3, 120, 20, 30),
			(4, 130, 25, 35),
			(5, 140, 30, 40),
		],
		// L1: 3 tables (within limit)
		vec![(11, 50, 5, 15), (12, 60, 20, 30), (13, 70, 35, 45)],
		// L2: 2 tables
		vec![(21, 30, 0, 25), (22, 40, 30, 50)],
	];

	// Create the manifest with these tables
	let manifest = create_test_manifest(&env, level_tables).unwrap();

	// Create the leveled compaction strategy
	let opts = create_options_with_compaction_settings(&env.options, 4, 2.0);
	let strategy = Strategy::from_options(opts);

	// Test the strategy's level selection
	let choice = strategy.pick_levels(&manifest.read().unwrap()).unwrap();

	// Verify L0 was selected for compaction (as it exceeds its limit)
	match choice {
		CompactionChoice::Merge(input) => {
			assert_eq!(input.source_level, 0, "L0 should be selected as source level");
			assert_eq!(input.target_level, 1, "L1 should be selected as target level");

			// Verify all L0 tables are included
			for id in 1..=5 {
				assert!(
					input.tables_to_merge.contains(&id),
					"Table {id} from L0 should be included"
				);
			}

			// Verify overlapping L1 table is included
			assert!(
				input.tables_to_merge.contains(&12),
				"Overlapping table 12 from L1 should be included"
			);
		}
		CompactionChoice::Skip => {
			panic!("Compaction should not be skipped when L0 exceeds limit");
		}
	}
}

#[test]
fn test_compaction_edge_cases() {
	let env = TestEnv::new();

	// 1. Empty level test
	let empty_level_tables = vec![
		vec![],                // Empty L0
		vec![(11, 50, 5, 15)], // One table in L1
	];

	let manifest = create_test_manifest(&env, empty_level_tables).unwrap();
	let opts = create_options_with_compaction_settings(&env.options, 4, 2.0);
	let strategy = Strategy::from_options(opts);

	// Test strategy with empty level
	let choice = strategy.pick_levels(&manifest.read().unwrap()).unwrap();

	// Strategy should skip compaction when L0 is empty
	match choice {
		CompactionChoice::Skip => { /* Expected */ }
		CompactionChoice::Merge(_) => {
			panic!("Compaction should be skipped when L0 is empty");
		}
	}

	// 2. Last level test
	let last_level_tables = vec![
		vec![], // L0
		vec![], // L1
		vec![], // L2
		vec![
			// Many tables in L3 (last level)
			(31, 30, 10, 20),
			(32, 40, 30, 40),
			(33, 50, 50, 60),
			(34, 60, 70, 80),
			(35, 70, 90, 100),
		],
	];

	let manifest = create_test_manifest(&env, last_level_tables).unwrap();

	// Test strategy with many tables in last level
	let choice = strategy.pick_levels(&manifest.read().unwrap()).unwrap();

	// If the last level exceeds its byte limit, it can compact to itself for tombstone cleanup
	match choice {
		CompactionChoice::Skip => {
			// Skip is OK if last level doesn't exceed byte limit
		}
		CompactionChoice::Merge(input) => {
			// If compaction is selected, it should be same-level compaction
			if input.source_level == 3 {
				assert_eq!(input.target_level, 3, "Bottom level should compact to itself");
			}
		}
	}
}

#[test]
fn test_level_selection_score_based() {
	let env = TestEnv::new();

	// Create levels with tables of known sizes to verify score calculation
	let mut levels = Levels::new(3, 10);

	// Create L1 with small amount of data
	let l1_entries = create_entries(0, 5, 100);
	let l1_table = env.create_test_table(1, l1_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[1]).insert(Arc::clone(&l1_table));

	// Create L2 with more data (larger file)
	let l2_entries = create_entries(0, 50, 200);
	let l2_table = env.create_test_table(2, l2_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[2]).insert(Arc::clone(&l2_table));

	// Read actual file sizes
	let l1_bytes = l1_table.file_size;
	let l2_bytes = l2_table.file_size;

	// Set up options where L2 will have a higher score than L1
	// max_bytes_for_level_base: Set so L1 score < 1.0 but L2 score > 1.0
	// level_multiplier: 1.0 so L2 target = L1 target (both use base)
	let max_bytes_for_level_base = l1_bytes * 2; // L1 score will be ~0.5
	let level_multiplier = 1.0; // L2 target = base * 1.0^1 = base

	// Create manifest
	let manifest_path = env.options.path.join("test_manifest_score");
	let manifest = LevelManifest {
		path: manifest_path,
		levels,
		hidden_set: HashSet::new(),
		next_table_id: Arc::new(AtomicU64::new(1000)),
		manifest_format_version: crate::levels::MANIFEST_FORMAT_VERSION_V1,
		snapshots: Vec::new(),
		log_number: 0,
		last_sequence: 0,
	};
	write_manifest_to_disk(&manifest).unwrap();
	let manifest = Arc::new(RwLock::new(manifest));

	// Calculate expected scores using the actual formula
	// L1 score = l1_bytes / max_bytes_for_level(1)
	//          = l1_bytes / (max_bytes_for_level_base * multiplier^0)
	//          = l1_bytes / max_bytes_for_level_base
	let l1_score = l1_bytes as f64 / max_bytes_for_level_base as f64;

	// L2 score = l2_bytes / max_bytes_for_level(2)
	//          = l2_bytes / (max_bytes_for_level_base * multiplier^1)
	//          = l2_bytes / (max_bytes_for_level_base * 1.0)
	//          = l2_bytes / max_bytes_for_level_base
	let l2_score = l2_bytes as f64 / max_bytes_for_level_base as f64;

	// Create strategy with calculated limits
	let mut opts = (*env.options).clone();
	opts.max_bytes_for_level = max_bytes_for_level_base;
	opts.level_multiplier = level_multiplier;
	let opts = Arc::new(opts);
	let strategy = Strategy::from_options(opts);

	// Verify selection matches expected highest score
	let manifest_guard = manifest.read().unwrap();
	let choice = strategy.pick_levels(&manifest_guard).unwrap();

	match choice {
		CompactionChoice::Merge(input) => {
			// Since l2_bytes > l1_bytes, l2_score > l1_score
			// And l2_score should be >= 1.0 (since we set base to make L1 ~0.5)
			if l2_score > l1_score && l2_score >= 1.0 {
				assert_eq!(
					input.source_level, 2,
					"L2 should be selected (score {:.2} > L1 score {:.2})",
					l2_score, l1_score
				);
			} else if l1_score >= 1.0 {
				assert_eq!(
					input.source_level, 1,
					"L1 should be selected (score {:.2} >= 1.0)",
					l1_score
				);
			} else {
				panic!(
					"Unexpected selection: L{} selected when L1 score {:.2}, L2 score {:.2}",
					input.source_level, l1_score, l2_score
				);
			}
		}
		CompactionChoice::Skip => {
			assert!(
				l1_score < 1.0 && l2_score < 1.0,
				"Skip only when all scores < 1.0 (L1: {:.2}, L2: {:.2})",
				l1_score,
				l2_score
			);
		}
	}
}

/// Generates key-value entries for a table
fn generate_entries(
	table_idx: usize,
	keys_per_table: usize,
	seq_num: u64,
) -> Vec<(InternalKey, Vec<u8>)> {
	let mut entries = Vec::new();

	for i in 0..keys_per_table {
		// Create a key with table and index - format: "table{:02d}-key-{:03d}"
		let key = format!("table{table_idx:02}-key-{i:03}").into_bytes();
		let internal_key = InternalKey::new(key, seq_num, InternalKeyKind::Set, 0);

		// Create a value that's predictable - format: "value-{:02d}-{:03d}"
		let value = format!("value-{table_idx:02}-{i:03}").into_bytes();
		let encoded_value = create_inline_value(&value);

		entries.push((internal_key, encoded_value));
	}

	entries
}

#[test(tokio::test)]
async fn test_simple_merge_compaction() {
	let env = TestEnv::new();

	// Create 10 tables, each with 10 keys
	const TABLE_COUNT: usize = 10;
	const KEYS_PER_TABLE: usize = 10;
	const TOTAL_KEYS: usize = TABLE_COUNT * KEYS_PER_TABLE;

	// Track expected keys for verification
	let mut expected_keys = HashSet::new();

	// Create tables for L0
	let mut l0_tables = Vec::new();
	for i in 0..TABLE_COUNT {
		let id = (i + 1) as u64;
		let seq = 100 + i as u64;

		let entries = generate_entries(i, KEYS_PER_TABLE, seq);

		// Track expected keys
		for (key, value) in &entries {
			expected_keys.insert((key.user_key.clone(), value.clone()));
		}

		let table = env.create_test_table(id, entries).unwrap();
		l0_tables.push(table);
	}

	// Initialize levels with these tables
	let mut levels = Levels::new(3, 10);

	// Add tables to L0
	for table in l0_tables {
		Arc::make_mut(&mut levels.get_levels_mut()[0]).insert(table);
	}

	// Verify we have the expected number of tables and keys
	{
		let mut total_keys = 0;
		for table in &levels.get_levels()[0].tables {
			let mut table_keys = 0;
			let mut iter = table.iter(None).unwrap();
			iter.seek_first().unwrap();

			while iter.valid() {
				table_keys += 1;
				iter.next().unwrap();
			}
			total_keys += table_keys;
		}

		assert_eq!(
			levels.get_levels()[0].tables.len(),
			TABLE_COUNT,
			"Should have exactly {TABLE_COUNT} tables in L0"
		);
		assert_eq!(
			total_keys, TOTAL_KEYS,
			"Should have exactly {TOTAL_KEYS} keys across all tables"
		);
	}

	// Create and write the manifest
	let manifest_path = env.options.path.join("test_manifest");

	// Find the maximum table ID used
	let mut max_table_id = 0;
	for level in levels.get_levels() {
		for table in &level.tables {
			max_table_id = std::cmp::max(max_table_id, table.id);
		}
	}

	// Use a safe starting value for next_table_id
	let next_table_id = max_table_id + 1000;

	let manifest = LevelManifest {
		path: manifest_path,
		levels,
		hidden_set: HashSet::new(),
		next_table_id: Arc::new(AtomicU64::new(next_table_id)),
		manifest_format_version: crate::levels::MANIFEST_FORMAT_VERSION_V1,
		snapshots: Vec::new(),
		log_number: 0,
		last_sequence: 0,
	};

	write_manifest_to_disk(&manifest).unwrap();
	let manifest = Arc::new(RwLock::new(manifest));

	// Create the leveled compaction strategy
	let opts = create_options_with_compaction_settings(&env.options, 4, 2.0);
	let strategy = Arc::new(Strategy::from_options(opts));

	// Create compaction options
	let compaction_options = create_compaction_options(env.options, Arc::clone(&manifest));

	// Create the compactor
	let compactor = Compactor::new(compaction_options, strategy);

	// Run compaction
	let result = compactor.compact();
	assert!(result.is_ok(), "Compaction should succeed");

	// Verify all keys are still present after compaction
	let (_, all_key_values) = verify_keys_after_compaction(&manifest, &expected_keys);

	// Verify correct total key count
	assert_eq!(
		all_key_values.len(),
		TOTAL_KEYS,
		"Expected to find all {} keys, but only found {}",
		TOTAL_KEYS,
		all_key_values.len()
	);

	{
		let updated_manifest = manifest.read().unwrap();

		// L1 should have at least one table after compaction
		let l1_tables = &updated_manifest.levels.get_levels()[1].tables;
		assert!(!l1_tables.is_empty(), "L1 should have at least one table after compaction");

		// L0 should be under its limit after compaction
		let l0_tables = &updated_manifest.levels.get_levels()[0].tables;
		assert!(l0_tables.len() < 4, "L0 should be under its limit after compaction");

		// All original L0 tables should be removed by compaction since ALL L0 tables
		// are selected for L0→L1 compaction
		let original_table_ids: Vec<u64> = (1..=TABLE_COUNT as u64).collect();
		let all_tables = updated_manifest.get_all_tables();
		let remaining_original_count =
			original_table_ids.iter().filter(|id| all_tables.contains_key(id)).count();

		assert_eq!(
                remaining_original_count, 0,
                "All original L0 tables should be removed by compaction. Remaining: {remaining_original_count}"
            );
	}
}

#[test(tokio::test)]
async fn test_multi_level_merge_compaction() {
	// Generate key-value entries for a table
	fn generate_entries(
		level: usize,
		table_idx: usize,
		start_idx: usize,
		keys_per_table: usize,
		seq_num: u64,
	) -> Vec<(InternalKey, Vec<u8>)> {
		let mut entries = Vec::new();

		for i in 0..keys_per_table {
			let idx = start_idx + i;
			let key = format!("L{level}-T{table_idx:02}-K-{idx:05}").into_bytes();
			let internal_key = InternalKey::new(key, seq_num, InternalKeyKind::Set, 0);
			let value = format!("V-{level}-{table_idx:02}-{idx:05}").into_bytes();
			let encoded_value = create_inline_value(&value);
			entries.push((internal_key, encoded_value));
		}

		entries
	}

	let env = TestEnv::new();

	// Define level configuration
	struct LevelConfig {
		level: usize,
		table_count: usize,
		keys_per_table: usize,
		base_id: u64,
		base_seq: u64,
	}

	// Configure each level with increasing number of keys
	let level_configs = vec![
		// Level 0: 10 tables × 10 keys = 100 keys total
		LevelConfig {
			level: 0,
			table_count: 10,
			keys_per_table: 10,
			base_id: 1,
			base_seq: 1000,
		},
		// Level 1: 8 tables × 15 keys = 120 keys total
		LevelConfig {
			level: 1,
			table_count: 8,
			keys_per_table: 15,
			base_id: 100,
			base_seq: 900,
		},
		// Level 2: 16 tables × 20 keys = 320 keys total
		LevelConfig {
			level: 2,
			table_count: 16,
			keys_per_table: 20,
			base_id: 200,
			base_seq: 800,
		},
		// Level 3: 30 tables × 30 keys = 900 keys total
		LevelConfig {
			level: 3,
			table_count: 30,
			keys_per_table: 30,
			base_id: 300,
			base_seq: 700,
		},
		// Level 4: 45 tables × 40 keys = 1800 keys total
		LevelConfig {
			level: 4,
			table_count: 45,
			keys_per_table: 40,
			base_id: 400,
			base_seq: 600,
		},
	];

	// Calculate total expected keys
	let total_expected_keys: usize =
		level_configs.iter().map(|c| c.table_count * c.keys_per_table).sum();

	// Track expected keys for verification
	let mut expected_keys = HashSet::new();
	let mut key_to_level_map = HashMap::new();
	let mut all_tables: Vec<Vec<Arc<Table>>> = vec![Vec::new(); level_configs.len()];

	for config in &level_configs {
		let mut level_tables = Vec::new();

		for i in 0..config.table_count {
			let id = config.base_id + i as u64;
			let seq = config.base_seq + i as u64;
			let start_idx = i * config.keys_per_table;

			let entries = generate_entries(config.level, i, start_idx, config.keys_per_table, seq);

			// Track expected keys
			for (key, value) in &entries {
				expected_keys.insert((key.user_key.clone(), value.clone()));
				key_to_level_map.insert(key.user_key.clone(), config.level);
			}

			let table = env.create_test_table(id, entries).unwrap();
			level_tables.push(table);
		}

		all_tables[config.level] = level_tables;
	}

	// Initialize levels
	let mut levels = Levels::new(level_configs.len(), 10);

	// Add tables to their respective levels
	for (level_idx, level_tables) in all_tables.into_iter().enumerate() {
		for table in level_tables {
			Arc::make_mut(&mut levels.get_levels_mut()[level_idx]).insert(table);
		}
	}

	// Create and write the manifest
	let manifest_path = env.options.path.join("test_manifest");
	let next_table_id = 1000; // Start well above existing IDs

	// Create a shared table ID counter that will be used by both the test and
	// compactor
	let shared_table_id_counter = Arc::new(AtomicU64::new(next_table_id));

	let manifest = LevelManifest {
		path: manifest_path,
		levels,
		hidden_set: HashSet::new(),
		next_table_id: shared_table_id_counter,
		manifest_format_version: crate::levels::MANIFEST_FORMAT_VERSION_V1,
		snapshots: Vec::new(),
		log_number: 0,
		last_sequence: 0,
	};

	write_manifest_to_disk(&manifest).unwrap();
	let manifest = Arc::new(RwLock::new(manifest));

	// Create the strategy and compactor
	let opts = create_options_with_compaction_settings(&env.options, 4, 2.0);
	let strategy = Arc::new(Strategy::from_options(opts));
	let compaction_options = create_compaction_options(env.options, Arc::clone(&manifest));
	let compactor = Compactor::new(compaction_options, strategy);

	// Run multiple rounds of compaction
	const COMPACTION_ROUNDS: usize = 15;

	for round in 1..=COMPACTION_ROUNDS {
		let result = compactor.compact();
		if result.is_err() {
			// Not all rounds will have work to do, which is fine
			continue;
		}

		// Check if all levels are within limits
		let manifest_guard = manifest.read().unwrap();
		let levels = manifest_guard.levels.get_levels();

		let all_levels_ok = levels.iter().enumerate().all(|(idx, level)| {
			let limit = if idx == 0 {
				4
			} else {
				8 * 2u32.pow(idx as u32 - 1) as usize
			};
			level.tables.len() <= limit
		});

		if all_levels_ok && round >= 5 {
			break;
		}
	}

	// Verify all keys are still present after compaction
	let (_, all_key_values) = verify_keys_after_compaction(&manifest, &expected_keys);

	// Verify we found all keys
	assert_eq!(
		all_key_values.len(),
		total_expected_keys,
		"Expected to find all {} keys, but only found {}",
		total_expected_keys,
		all_key_values.len()
	);

	// Check for key range overlaps within levels
	let manifest_guard = manifest.read().unwrap();
	let levels = manifest_guard.levels.get_levels();

	for (level_idx, level) in levels.iter().enumerate().skip(1) {
		if level.tables.len() >= 2 {
			// Get key ranges for all tables in this level
			let mut table_ranges = Vec::new();

			for table in &level.tables {
				// Find min and max key in this table
				let mut min_key = None;
				let mut max_key = None;

				let mut iter = table.iter(None).unwrap();
				iter.seek_first().unwrap();
				while iter.valid() {
					let key = iter.key().to_owned().user_key.clone();
					if min_key.is_none() {
						min_key = Some(key.clone());
					}
					max_key = Some(key);
					iter.next().unwrap();
				}

				if let (Some(min), Some(max)) = (min_key, max_key) {
					table_ranges.push((table.id, min, max));
				}
			}

			// Sort by min_key
			table_ranges.sort_by(|a, b| a.1.cmp(&b.1));

			// Check for overlaps between adjacent tables
			for i in 0..(table_ranges.len().saturating_sub(1)) {
				let (id1, _, max_key1) = &table_ranges[i];
				let (id2, min_key2, _) = &table_ranges[i + 1];

				// Check if max_key1 >= min_key2, which would indicate overlap
				assert!(
					max_key1 < min_key2,
					"Overlap detected in L{} between table {} and {}: {:?} >= {:?}",
					level_idx,
					id1,
					id2,
					String::from_utf8_lossy(max_key1),
					String::from_utf8_lossy(min_key2)
				);
			}
		}
	}
}

#[test]
fn test_select_tables_for_compaction_bug() {
	let env = TestEnv::new();
	let opts = create_options_with_compaction_settings(&env.options, 4, 10.0);
	let strategy = Strategy::from_options(opts);

	// Create source level (L0) with tables with different but overlapping key
	// ranges
	let mut source_level = Level::with_capacity(10);

	// Create 3 tables for source level
	let table1 = env
		.create_test_table(
			1,
			create_ordered_entries("a", 10, 10, 100, None), // a-00010 to a-00019
		)
		.unwrap();

	let table2 = env
		.create_test_table(
			2,
			create_ordered_entries("b", 15, 10, 150, None), // b-00015 to b-00024
		)
		.unwrap();

	let table3 = env
		.create_test_table(
			3,
			create_ordered_entries("c", 20, 10, 200, None), // c-00020 to c-00029
		)
		.unwrap();

	source_level.insert(table1);
	source_level.insert(table2);
	source_level.insert(table3);

	// Create next level (L1) with tables that have overlapping key ranges with
	// source
	let mut next_level = Level::with_capacity(10);

	let table10 = env
		.create_test_table(
			10,
			create_ordered_entries("a", 5, 10, 50, None), /* a-00005 to a-00014 (overlaps
			                                               * with table1) */
		)
		.unwrap();

	let table11 = env
		.create_test_table(
			11,
			create_ordered_entries("b", 10, 10, 120, None), /* b-00010 to b-00019 (overlaps
			                                                 * with table2) */
		)
		.unwrap();

	let table12 = env
		.create_test_table(
			12,
			create_ordered_entries("c", 15, 10, 180, None), /* c-00015 to c-00024 (overlaps
			                                                 * with table3) */
		)
		.unwrap();

	// Create a table with the same ID as one in source level to trigger the bug
	let table_dup = env
		.create_test_table(
			1, // Same ID as table1 in source_level
			create_ordered_entries("a", 5, 8, 90, None), /* a-00005 to a-00012 (overlaps
			    * with table1) */
		)
		.unwrap();

	next_level.insert(table10);
	next_level.insert(table11);
	next_level.insert(table12);
	next_level.insert(table_dup); // This will potentially cause problems

	// Call select_tables_for_compaction
	let selected_tables =
		strategy.select_tables_for_compaction(&source_level, &next_level, 0).unwrap();

	// Check for duplicates
	let mut unique_ids = HashSet::new();
	let mut has_duplicates = false;

	for &id in &selected_tables {
		if !unique_ids.insert(id) {
			has_duplicates = true;
		}
	}

	// Check if all source tables were selected
	let source_table_ids: HashSet<_> = source_level.tables.iter().map(|t| t.id).collect();
	let selected_source_ids: HashSet<_> =
		selected_tables.iter().filter(|&&id| source_table_ids.contains(&id)).copied().collect();

	// Verify all source tables are selected
	assert_eq!(
		source_table_ids.len(),
		selected_source_ids.len(),
		"Not all source tables were selected!"
	);

	// Verify no duplicates
	assert!(!has_duplicates, "Found duplicate table IDs in the selected tables list");

	// Count occurrences of each table ID
	let mut id_count = HashMap::new();
	for &id in &selected_tables {
		*id_count.entry(id).or_insert(0) += 1;
	}

	// Check for any ID that appears more than once
	for (&id, &count) in &id_count {
		assert_eq!(count, 1, "Table ID {id} appears {count} times in the selected tables list");
	}
}

#[test]
fn test_l1_to_l2_table_selection() {
	let env = TestEnv::new();
	let opts = create_options_with_compaction_settings(&env.options, 4, 10.0);
	let strategy = Strategy::from_options(opts);

	// Create L1 with 3 tables (non-overlapping as per L1+ invariant)
	let mut source_level = Level::with_capacity(10);

	let table1 = env
		.create_test_table(
			1,
			create_ordered_entries("a", 10, 10, 100, None), // a-00010 to a-00019
		)
		.unwrap();

	let table2 = env
		.create_test_table(
			2,
			create_ordered_entries("b", 20, 10, 150, None), // b-00020 to b-00029
		)
		.unwrap();

	let table3 = env
		.create_test_table(
			3,
			create_ordered_entries("c", 30, 10, 200, None), // c-00030 to c-00039
		)
		.unwrap();

	source_level.insert(table1);
	source_level.insert(table2);
	source_level.insert(table3);

	// Create L2 with tables that have overlapping key ranges with some L1 tables
	let mut next_level = Level::with_capacity(10);

	let table10 = env
		.create_test_table(
			10,
			create_ordered_entries("a", 5, 15, 50, None), /* a-00005 to a-00019 (overlaps
			                                               * with table1) */
		)
		.unwrap();

	let table11 = env
		.create_test_table(
			11,
			create_ordered_entries("d", 40, 10, 120, None), // d-00040 to d-00049 (no overlap)
		)
		.unwrap();

	next_level.insert(table10);
	next_level.insert(table11);

	// Call select_tables_for_compaction for L1 → L2 (source_level_num = 1)
	let selected_tables =
		strategy.select_tables_for_compaction(&source_level, &next_level, 1).unwrap();

	// For L1+, we should select only ONE table from source level
	let source_table_ids: HashSet<_> = source_level.tables.iter().map(|t| t.id).collect();
	let selected_source_ids: HashSet<_> =
		selected_tables.iter().filter(|&&id| source_table_ids.contains(&id)).copied().collect();

	// Should select exactly ONE source table (not all like L0→L1)
	assert_eq!(
		selected_source_ids.len(),
		1,
		"L1+ compaction should select exactly ONE source table, got {}",
		selected_source_ids.len()
	);

	// The algorithm picks the first table from the source level, which is table 3
	// (since tables are stored in insertion order and we inserted 1, 2, 3)
	let _selected_l1_table = *selected_source_ids.iter().next().unwrap();

	// Should not select the other L1 tables since we only pick one for L1+
	assert!(
		!selected_tables.contains(&1)
			|| !selected_tables.contains(&2)
			|| !selected_tables.contains(&3),
		"Should NOT select all L1 tables (only one L1+ table)"
	);

	// Note: The overlap detection might not be working if no L2 tables are selected
	// This could be due to missing key range metadata in the test tables
	assert_eq!(selected_tables.len(), 1, "Should select exactly 1 table (only L1 table since overlap detection may not work in test)");
}

#[test]
fn test_l1_compaction_bounds_correctness() {
	// This test verifies that compaction uses the CORRECT table's bounds
	// when selecting overlapping tables from the next level.
	//
	// Bug scenario: If we select table3 (by size) but compute bounds from table1 (first by key),
	// we'll select wrong L2 tables.

	let env = TestEnv::new();
	let opts = create_options_with_compaction_settings(&env.options, 4, 10.0);
	let strategy = Strategy::from_options(opts);

	// Create L1 with 3 tables using proper L1+ insertion (sorted by key)
	let mut source_level = Level::with_capacity(10);

	// Table1: Smallest key ("a"), SMALL size (10 entries)
	let table1 = env
		.create_test_table(
			1,
			create_ordered_entries("a", 10, 10, 100, None), // a-00010 to a-00019, 10 entries
		)
		.unwrap();

	// Table2: Middle key ("b"), MEDIUM size (20 entries)
	let table2 = env
		.create_test_table(
			2,
			create_ordered_entries("b", 20, 20, 150, None), // b-00020 to b-00039, 20 entries
		)
		.unwrap();

	// Table3: Largest key ("c"), LARGEST size (30 entries) - should be selected by compensated size
	let table3 = env
		.create_test_table(
			3,
			create_ordered_entries("c", 30, 30, 200, None), // c-00030 to c-00059, 30 entries
		)
		.unwrap();

	// Use proper L1+ insertion (sorted by smallest key)
	source_level.insert_sorted_by_key(Arc::clone(&table1)); // First by key: a < b < c
	source_level.insert_sorted_by_key(Arc::clone(&table2));
	source_level.insert_sorted_by_key(Arc::clone(&table3));

	// Verify ordering: table1 should be first (smallest key)
	assert_eq!(source_level.tables[0].id, 1, "First table should be table1 (smallest key)");

	// Verify table3 is largest (most entries = largest file size)
	assert!(table3.file_size > table2.file_size);
	assert!(table2.file_size > table1.file_size);

	// Create L2 with tables that overlap with SPECIFIC L1 tables
	let mut next_level = Level::with_capacity(10);

	// L2 table that overlaps with table1 (a-keys)
	let l2_table_a = env
		.create_test_table(
			10,
			create_ordered_entries("a", 5, 15, 50, None), // a-00005 to a-00019 (overlaps table1)
		)
		.unwrap();

	// L2 table that overlaps with table3 (c-keys) - this is the one we should select!
	let l2_table_c = env
		.create_test_table(
			11,
			create_ordered_entries("c", 25, 35, 120, None), // c-00025 to c-00059 (overlaps table3)
		)
		.unwrap();

	// L2 table that doesn't overlap with any L1 table
	let l2_table_d = env
		.create_test_table(
			12,
			create_ordered_entries("d", 100, 10, 300, None), // d-00100 to d-00109 (no overlap)
		)
		.unwrap();

	next_level.insert_sorted_by_key(l2_table_a);
	next_level.insert_sorted_by_key(l2_table_c);
	next_level.insert_sorted_by_key(l2_table_d);

	// Call select_tables_for_compaction for L1 → L2
	let selected_tables =
		strategy.select_tables_for_compaction(&source_level, &next_level, 1).unwrap();

	// Verify we selected exactly ONE L1 table
	let source_table_ids: HashSet<_> = source_level.tables.iter().map(|t| t.id).collect();
	let selected_source_ids: HashSet<_> =
		selected_tables.iter().filter(|&&id| source_table_ids.contains(&id)).copied().collect();

	assert_eq!(selected_source_ids.len(), 1, "Should select exactly ONE L1 table");

	// The selected L1 table should be table3 (largest by compensated size)
	let selected_l1_id = *selected_source_ids.iter().next().unwrap();
	assert_eq!(
		selected_l1_id, 3,
		"Should select table3 (largest size), but selected table{}",
		selected_l1_id
	);

	// Verify we selected the CORRECT L2 table (l2_table_c, which overlaps with table3)
	// NOT l2_table_a (which overlaps with table1, the first table by key)
	let l2_table_ids: HashSet<_> = next_level.tables.iter().map(|t| t.id).collect();
	let selected_l2_ids: HashSet<_> =
		selected_tables.iter().filter(|&&id| l2_table_ids.contains(&id)).copied().collect();

	assert!(
		selected_l2_ids.contains(&11),
		"Should select L2 table 11 (overlaps with selected table3), but selected: {:?}",
		selected_l2_ids
	);
	assert!(
		!selected_l2_ids.contains(&10),
		"Should NOT select L2 table 10 (overlaps with table1, not selected table3)"
	);
	assert!(!selected_l2_ids.contains(&12), "Should NOT select L2 table 12 (no overlap)");
}

#[test(tokio::test)]
async fn test_compaction_with_large_keys_and_values() {
	let env = TestEnv::new();

	// Create tables with some large keys and values
	let mut levels = Levels::new(3, 10);

	// Create entries with large keys and values
	let mut large_entries = Vec::new();
	for i in 0..5 {
		// Reduced from 10 to 5 to keep test time manageable
		// Create large key (1KB)
		let key_base = format!("large-key-{i}");
		let key_padding = "X".repeat(1000);
		let key = format!("{key_base}{key_padding}").into_bytes();

		// Create large value (4KB)
		let value_base = format!("large-value-{i}");
		let value_padding = "Y".repeat(4000);
		let value = format!("{value_base}{value_padding}").into_bytes();

		let internal_key = InternalKey::new(key, 1000, InternalKeyKind::Set, 0);

		large_entries.push((internal_key, value));
	}

	// Add table with large entries to L0
	let large_table = env.create_test_table(1, large_entries.clone()).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[0]).insert(large_table);

	// Create manifest
	let manifest_path = env.options.path.join("test_manifest");
	let manifest = LevelManifest {
		path: manifest_path,
		levels,
		hidden_set: HashSet::new(),
		next_table_id: Arc::new(AtomicU64::new(1000)),
		manifest_format_version: crate::levels::MANIFEST_FORMAT_VERSION_V1,
		snapshots: Vec::new(),
		log_number: 0,
		last_sequence: 0,
	};

	write_manifest_to_disk(&manifest).unwrap();
	let manifest = Arc::new(RwLock::new(manifest));

	// Track expected data
	let mut expected_data = HashMap::new();
	for (key, value) in &large_entries {
		expected_data.insert(key.user_key.clone(), value.clone());
	}

	// Set up compaction
	let opts = create_options_with_compaction_settings(&env.options, 4, 2.0);
	let strategy = Arc::new(Strategy::from_options(opts));
	let compaction_options = create_compaction_options(env.options, Arc::clone(&manifest));
	let compactor = Compactor::new(compaction_options, strategy);

	// Run compaction
	perform_compaction_rounds(&compactor, 2);

	// Verify all large keys and values are preserved
	assert!(
		verify_all_keys_present(&manifest, &expected_data),
		"Compaction did not preserve large keys and values"
	);
}

// TODO: add more tests for:
// - Compaction with keys that are split across multiple tables
#[test(tokio::test)]
async fn test_compaction_respects_sequence_numbers() {
	let env = TestEnv::new();

	// Create tables with same keys but different sequence numbers
	let mut levels = Levels::new(3, 10);

	// Create map to track expected final values
	let mut expected_final_values = HashMap::new();

	// Create L0 tables with overlapping keys but different sequence numbers
	for i in 0..5 {
		let base_seq = 100 - (i * 10); // Decreasing sequence numbers
		let mut entries = Vec::new();

		// Create 10 keys, same keys in each table but with different values and seqs
		for j in 0..10 {
			let key = format!("key-{j:03}").into_bytes();
			let key_bytes = key;
			let value_encoded = format!("value-from-table-{}-seq-{}", i, base_seq + j).into_bytes();
			let encoded_value = create_inline_value(&value_encoded);

			let internal_key =
				InternalKey::new(key_bytes.clone(), (base_seq + j) as u64, InternalKeyKind::Set, 0);

			entries.push((internal_key, encoded_value));

			// Update expected value if this is a higher sequence number
			if i == 0 {
				// First table has highest sequence numbers, so these should win
				expected_final_values.insert(key_bytes, value_encoded);
			}
		}

		// Create the table
		let table = env.create_test_table(i as u64 + 1, entries).unwrap();
		Arc::make_mut(&mut levels.get_levels_mut()[0]).insert(table);
	}

	// Create manifest
	let manifest_path = env.options.path.join("test_manifest");
	let manifest = LevelManifest {
		path: manifest_path,
		levels,
		hidden_set: HashSet::new(),
		next_table_id: Arc::new(AtomicU64::new(1000)),
		manifest_format_version: crate::levels::MANIFEST_FORMAT_VERSION_V1,
		snapshots: Vec::new(),
		log_number: 0,
		last_sequence: 0,
	};

	write_manifest_to_disk(&manifest).unwrap();
	let manifest = Arc::new(RwLock::new(manifest));

	// Set up compaction
	let opts = create_options_with_compaction_settings(&env.options, 4, 2.0);
	let strategy = Arc::new(Strategy::from_options(opts));
	let compaction_options = create_compaction_options(env.options, Arc::clone(&manifest));
	let compactor = Compactor::new(compaction_options, strategy);

	// Run compaction
	perform_compaction_rounds(&compactor, 2);

	// Verify the highest sequence number values are preserved
	let manifest_guard = manifest.read().unwrap();
	let levels = manifest_guard.levels.get_levels();

	// There should be no tables in L0 after compaction
	assert_eq!(levels[0].tables.len(), 0, "L0 should be empty after compaction");

	// Verify the key-values match expected
	let mut all_keys = HashMap::new();
	for level in levels {
		for table in &level.tables {
			let mut iter = table.iter(None).unwrap();
			iter.seek_first().unwrap();
			while iter.valid() {
				let key = iter.key().to_owned().user_key.clone();
				let location = ValueLocation::decode(iter.value_encoded().unwrap()).unwrap();
				if location.is_value_pointer() {
					panic!("Unexpected VLog pointer in test");
				}
				all_keys.insert(key, (*location.value).to_vec());
				iter.next().unwrap();
			}
		}
	}

	for (key, expected_value) in &expected_final_values {
		if let Some(actual_value) = all_keys.get(key) {
			assert_eq!(
				actual_value.as_slice(),
				expected_value.as_slice(),
				"Value for key {key:?} doesn't match highest sequence number"
			);
		} else {
			panic!("Key {key:?} is missing after compaction");
		}
	}
}

#[test(tokio::test)]
async fn test_tombstone_propagation() {
	// Test 95% deletion via tombstones with bottom-level filtering
	let env = TestEnv::new();
	let mut levels = Levels::new(3, 10);

	// Create entries with tombstones before values (higher seq numbers first)
	let mut all_entries = Vec::new();
	for i in 0..100 {
		let key = format!("key-{i:03}").into_bytes();
		let key_bytes = key;

		// Add tombstone first (higher sequence number) for 95% of keys
		if i < 95 {
			let delete_key =
				InternalKey::new(key_bytes.clone(), 300 + i, InternalKeyKind::Delete, 0);
			all_entries.push((delete_key, vec![]));
		}

		// Add value second (lower sequence number)
		let value_encoded = format!("original-value-{i}").into_bytes();
		let encoded_value = create_inline_value(&value_encoded);

		let set_key = InternalKey::new(key_bytes, 100 + i, InternalKeyKind::Set, 0);
		all_entries.push((set_key, encoded_value));
	}

	// Create L0 table and manifest
	let table = env.create_test_table(100, all_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[0]).insert(table);

	let manifest_path = env.options.path.join("test_manifest");
	let manifest = LevelManifest {
		path: manifest_path,
		levels,
		hidden_set: HashSet::new(),
		next_table_id: Arc::new(AtomicU64::new(1000)),
		manifest_format_version: crate::levels::MANIFEST_FORMAT_VERSION_V1,
		snapshots: Vec::new(),
		log_number: 0,
		last_sequence: 0,
	};
	write_manifest_to_disk(&manifest).unwrap();
	let manifest = Arc::new(RwLock::new(manifest));

	// Run compaction
	let opts = create_options_with_compaction_settings(&env.options, 1, 2.0);
	let strategy = Arc::new(Strategy::from_options(opts));
	let compaction_options = create_compaction_options(env.options, Arc::clone(&manifest));
	let compactor = Compactor::new(compaction_options, strategy);
	let result = compactor.compact();
	assert!(result.is_ok(), "Compaction failed");

	// Verify exactly 5 keys remain (95-99)
	let manifest_guard = manifest.read().unwrap();
	let levels = manifest_guard.levels.get_levels();

	let mut remaining_keys = Vec::new();
	for level in levels {
		for table in &level.tables {
			let mut iter = table.iter(None).unwrap();
			iter.seek_first().unwrap();
			while iter.valid() {
				let key = iter.key().to_owned();
				if key.kind() == InternalKeyKind::Set {
					let key_str = String::from_utf8_lossy(&key.user_key);
					remaining_keys.push(key_str.to_string());
				}
				iter.next().unwrap();
			}
		}
	}
	remaining_keys.sort();

	let expected_keys = vec!["key-095", "key-096", "key-097", "key-098", "key-099"];
	assert_eq!(remaining_keys, expected_keys);
}

#[test(tokio::test)]
async fn test_l0_overlapping_keys_compaction() {
	let env = TestEnv::new();
	let mut levels = Levels::new(3, 10);

	// Create L0 tables with overlapping key ranges
	// Table 1: key-005 to key-015, seq 105-115
	let mut entries1 = Vec::new();
	for i in 5..=15 {
		let key = format!("key-{i:03}").into_bytes();
		let value_encoded = format!("value-from-table1-{i}").into_bytes();
		let encoded_value = create_inline_value(&value_encoded);

		let internal_key = InternalKey::new(key, 100 + i, InternalKeyKind::Set, 0);
		entries1.push((internal_key, encoded_value));
	}

	// Table 2: key-010 to key-020, seq 150-160 (overlaps with table1)
	let mut entries2 = Vec::new();
	for i in 10..=20 {
		let key = format!("key-{i:03}").into_bytes();
		let value_encoded = format!("value-from-table2-{i}").into_bytes();
		let encoded_value = create_inline_value(&value_encoded);

		let internal_key = InternalKey::new(key, 150 + i - 10, InternalKeyKind::Set, 0);
		entries2.push((internal_key, encoded_value));
	}

	// Table 3: key-008 to key-012 with highest seq numbers + tombstone for key-014
	let mut entries3 = Vec::new();
	for i in 8..=12 {
		let key = format!("key-{i:03}").into_bytes();
		let value_encoded = format!("value-from-table3-{i}").into_bytes();
		let encoded_value = create_inline_value(&value_encoded);

		let internal_key = InternalKey::new(key, 200 + i - 8, InternalKeyKind::Set, 0);
		entries3.push((internal_key, encoded_value));
	}
	// Add tombstone that should win over other tables
	let tombstone_key = "key-014".as_bytes().to_vec();
	let tombstone = InternalKey::new(tombstone_key, 210, InternalKeyKind::Delete, 0);
	entries3.push((tombstone, vec![]));

	// Create tables and add to L0
	let table1 = env.create_test_table(1, entries1).unwrap();
	let table2 = env.create_test_table(2, entries2).unwrap();
	let table3 = env.create_test_table(3, entries3).unwrap();

	Arc::make_mut(&mut levels.get_levels_mut()[0]).insert(table1);
	Arc::make_mut(&mut levels.get_levels_mut()[0]).insert(table2);
	Arc::make_mut(&mut levels.get_levels_mut()[0]).insert(table3);

	// Create manifest and run compaction
	let manifest_path = env.options.path.join("test_manifest");
	let manifest = LevelManifest {
		path: manifest_path,
		levels,
		hidden_set: HashSet::new(),
		next_table_id: Arc::new(AtomicU64::new(1000)),
		manifest_format_version: crate::levels::MANIFEST_FORMAT_VERSION_V1,
		snapshots: Vec::new(),
		log_number: 0,
		last_sequence: 0,
	};
	write_manifest_to_disk(&manifest).unwrap();
	let manifest = Arc::new(RwLock::new(manifest));

	let opts = create_options_with_compaction_settings(&env.options, 1, 2.0);
	let strategy = Arc::new(Strategy::from_options(opts));
	let compaction_options = create_compaction_options(env.options, Arc::clone(&manifest));
	let compactor = Compactor::new(compaction_options, strategy);
	perform_compaction_rounds(&compactor, 2);

	// Verify sequence number precedence: highest seq wins for overlapping keys
	let manifest_guard = manifest.read().unwrap();
	let levels = manifest_guard.levels.get_levels();

	let mut all_keys = HashMap::new();
	let mut tombstones = HashMap::new();
	for level in levels {
		for table in &level.tables {
			let mut iter = table.iter(None).unwrap();
			iter.seek_first().unwrap();
			while iter.valid() {
				let key = iter.key().to_owned();
				let encoded_value = iter.value_encoded().unwrap().to_vec();
				match key.kind() {
					InternalKeyKind::Set => {
						let location = ValueLocation::decode(&encoded_value).unwrap();
						if location.is_value_pointer() {
							panic!("Unexpected VLog pointer in test");
						}
						all_keys.insert(key.user_key.clone(), (*location.value).to_vec());
					}
					InternalKeyKind::Delete => {
						tombstones.insert(key.user_key.clone(), key.seq_num());
					}
					_ => {}
				}
				iter.next().unwrap();
			}
		}
	}

	// Test specific key outcomes
	assert!(all_keys.contains_key(b"key-005".as_slice()), "key-005 should exist (only in table1)");
	assert!(all_keys.contains_key(b"key-010".as_slice()), "key-010 should exist (table3 wins)");
	assert!(
		!all_keys.contains_key(b"key-014".as_slice()),
		"key-014 should be deleted by tombstone"
	);
	assert!(all_keys.contains_key(b"key-015".as_slice()), "key-015 should exist (table2 wins)");
	assert!(all_keys.contains_key(b"key-020".as_slice()), "key-020 should exist (only in table2)");

	// Verify tombstone preservation at intermediate level
	if levels.len() >= 3 {
		assert!(
			tombstones.contains_key(b"key-014".as_slice()),
			"Tombstone should be preserved in intermediate level"
		);
		assert_eq!(
			tombstones[b"key-014".as_slice()],
			210,
			"Tombstone should have correct sequence number"
		);
	}
}

#[test(tokio::test)]
async fn test_l0_tombstone_propagation_overlapping() {
	let env = TestEnv::new();
	let mut levels = Levels::new(3, 10);

	// Create 3 overlapping L0 tables with different sequence numbers
	// Table 1: Base data (seq 100-119) for keys 0-19
	let mut entries1 = Vec::new();
	for i in 0..20 {
		let key = format!("key-{i:03}").into_bytes();
		let value_encoded = format!("original-value-{i}").into_bytes();
		let encoded_value = create_inline_value(&value_encoded);

		entries1.push((InternalKey::new(key, 100 + i, InternalKeyKind::Set, 0), encoded_value));
	}

	// Table 2: Mixed updates/deletes (seq 150-159) for keys 5-14
	let mut entries2 = Vec::new();
	for i in 5..15 {
		let key = format!("key-{i:03}").into_bytes();
		let (kind, value) = if i % 3 == 0 {
			(InternalKeyKind::Delete, vec![]) // Every 3rd key becomes tombstone
		} else {
			let value_encoded = format!("updated-value-{i}").into_bytes();
			let encoded_value = create_inline_value(&value_encoded);

			(InternalKeyKind::Set, encoded_value)
		};
		entries2.push((InternalKey::new(key, 150 + i - 5, kind, 0), value));
	}

	// Table 3: Final tombstones (seq 200+) for specific keys
	let mut entries3 = Vec::new();
	for i in [2, 8, 14, 17] {
		let key = format!("key-{i:03}").into_bytes();
		entries3.push((InternalKey::new(key, 200 + i / 2, InternalKeyKind::Delete, 0), vec![]));
	}

	// Add tables to L0
	for (id, entries) in [(1, entries1), (2, entries2), (3, entries3)] {
		let table = env.create_test_table(id, entries).unwrap();
		Arc::make_mut(&mut levels.get_levels_mut()[0]).insert(table);
	}

	// Create manifest and run compaction
	let manifest_path = env.options.path.join("test_manifest_tombstone");
	let manifest = LevelManifest {
		path: manifest_path,
		levels,
		hidden_set: HashSet::new(),
		next_table_id: Arc::new(AtomicU64::new(1000)),
		manifest_format_version: crate::levels::MANIFEST_FORMAT_VERSION_V1,
		snapshots: Vec::new(),
		log_number: 0,
		last_sequence: 0,
	};
	write_manifest_to_disk(&manifest).unwrap();
	let manifest = Arc::new(RwLock::new(manifest));

	let opts = create_options_with_compaction_settings(&env.options, 1, 2.0);
	let strategy = Arc::new(Strategy::from_options(opts));
	let compaction_options = create_compaction_options(env.options, Arc::clone(&manifest));
	let compactor = Compactor::new(compaction_options, strategy);
	perform_compaction_rounds(&compactor, 2);

	// Verify tombstone wins: keys 2, 6, 8, 9, 12, 14, 17 should be deleted
	let manifest_guard = manifest.read().unwrap();
	let levels = manifest_guard.levels.get_levels();

	let mut survivors = HashMap::new();
	for level in levels {
		for table in &level.tables {
			let mut iter = table.iter(None).unwrap();
			iter.seek_first().unwrap();
			while iter.valid() {
				let key = iter.key().to_owned();
				let encoded_value = iter.value_encoded().unwrap().to_vec();
				if key.kind() == InternalKeyKind::Set {
					let location = ValueLocation::decode(&encoded_value).unwrap();
					if location.is_value_pointer() {
						panic!("Unexpected VLog pointer in test");
					}
					survivors.insert(key.user_key.clone(), (*location.value).to_vec());
				}
				iter.next().unwrap();
			}
		}
	}

	// Test specific key outcomes (highest sequence number wins)
	assert!(
		!survivors.contains_key(b"key-002".as_slice()),
		"key-002 should be deleted by Table 3 tombstone"
	);
	assert!(
		!survivors.contains_key(b"key-006".as_slice()),
		"key-006 should be deleted by Table 2 tombstone"
	);
	assert!(
		!survivors.contains_key(b"key-009".as_slice()),
		"key-009 should be deleted by Table 2 tombstone"
	);
	assert!(
		!survivors.contains_key(b"key-012".as_slice()),
		"key-012 should be deleted by Table 2 tombstone"
	);
	assert!(
		!survivors.contains_key(b"key-014".as_slice()),
		"key-014 should be deleted by Table 3 tombstone"
	);

	// Test survivors have correct values
	assert!(
		survivors[b"key-000".as_slice()].starts_with(b"original-value"),
		"key-000 should have original value"
	);
	assert!(
		survivors[b"key-007".as_slice()].starts_with(b"updated-value"),
		"key-007 should have updated value from Table 2"
	);
	assert!(
		survivors[b"key-015".as_slice()].starts_with(b"original-value"),
		"key-015 should have original value"
	);

	// Verify expected total count (20 original - 5 deleted = 15 survivors)
	assert_eq!(survivors.len(), 13, "Should have 13 surviving keys after tombstone propagation");
}

#[test(tokio::test)]
async fn test_tombstone_propagation_through_levels() {
	let env = TestEnv::new();
	let mut levels = Levels::new(4, 10);

	// Create L2 tables: 4 tables to exceed limit and trigger L2→L3 compaction
	for table_idx in 0..4 {
		let mut l2_entries = Vec::new();
		for i in (table_idx * 3)..((table_idx + 1) * 3) {
			let key = format!("key-{i:03}").into_bytes();
			let (seq, kind, value) = if i % 2 == 0 {
				(200 + i, InternalKeyKind::Delete, vec![]) // Even keys = tombstones
			} else {
				let value_encoded = format!("l2-value-{i}").into_bytes();
				let encoded_value = create_inline_value(&value_encoded);

				(200 + i, InternalKeyKind::Set, encoded_value) // Odd keys = values
			};
			l2_entries.push((InternalKey::new(key, seq, kind, 0), value));
		}
		let table = env.create_test_table(100 + table_idx, l2_entries).unwrap();
		Arc::make_mut(&mut levels.get_levels_mut()[2]).insert(table);
	}

	// Create L3 with older values for all keys (overlapping with L2)
	let mut l3_entries = Vec::new();
	for i in 0..12 {
		let key = format!("key-{i:03}").into_bytes();
		let value_encoded = format!("l3-old-value-{i}").into_bytes();
		let encoded_value = create_inline_value(&value_encoded);

		l3_entries.push((InternalKey::new(key, 100 + i, InternalKeyKind::Set, 0), encoded_value));
	}
	let l3_table = env.create_test_table(200, l3_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[3]).insert(l3_table);

	// Create manifest and run L2→L3 compaction (bottom level)
	let manifest_path = env.options.path.join("test_manifest_propagation");
	let manifest = LevelManifest {
		path: manifest_path,
		levels,
		hidden_set: HashSet::new(),
		next_table_id: Arc::new(AtomicU64::new(1000)),
		manifest_format_version: crate::levels::MANIFEST_FORMAT_VERSION_V1,
		snapshots: Vec::new(),
		log_number: 0,
		last_sequence: 0,
	};
	write_manifest_to_disk(&manifest).unwrap();
	let manifest = Arc::new(RwLock::new(manifest));

	// Use very small byte limits to ensure compaction is triggered
	let mut opts = (*env.options).clone();
	opts.level0_max_files = 1;
	opts.max_bytes_for_level = 1024; // 1KB - very small so tables exceed it
	opts.level_multiplier = 2.0;
	let opts = Arc::new(opts);
	let strategy = Arc::new(Strategy::from_options(opts));
	let compaction_options = create_compaction_options(env.options, Arc::clone(&manifest));
	let compactor = Compactor::new(compaction_options, strategy);
	compactor.compact().unwrap();

	// Verify bottom-level tombstone filtering: L3 should have no tombstones
	let manifest_guard = manifest.read().unwrap();
	let levels = manifest_guard.levels.get_levels();

	let mut tombstones = 0;
	let mut values = 0;
	for table in &levels[3].tables {
		let mut iter = table.iter(None).unwrap();
		iter.seek_first().unwrap();
		while iter.valid() {
			let key = iter.key().to_owned();
			match key.kind() {
				InternalKeyKind::Delete => tombstones += 1,
				InternalKeyKind::Set => values += 1,
				_ => {}
			}
			iter.next().unwrap();
		}
	}

	// bottom level should filter out all tombstones
	assert_eq!(tombstones, 0, "Bottom level L3 should have no tombstones");
	assert!(values > 0, "L3 should contain some values after compaction");
	// With bytes-based limits, compaction behavior may vary - just verify L2 has fewer tables than
	// before
	assert!(
		levels[2].tables.len() < 4,
		"L2 should have fewer tables after compaction (had 4, now {})",
		levels[2].tables.len()
	);
}

#[test]
fn test_tombstone_propagation_journey() {
	let env = TestEnv::new();

	// Create tombstone (seq=100) and older value (seq=50) for same key
	let key = "test-key".as_bytes().to_vec();
	let key_bytes = key;

	// Table 1: tombstone entry
	let mut tombstone_entries = Vec::new();
	let tombstone = InternalKey::new(key_bytes.clone(), 100, InternalKeyKind::Delete, 0);
	tombstone_entries.push((tombstone, vec![]));
	let tombstone_table = env.create_test_table(100, tombstone_entries).unwrap();

	// Table 2: older value entry
	let mut value_entries = Vec::new();
	let value_key = InternalKey::new(key_bytes, 50, InternalKeyKind::Set, 0);

	let value_encoded = b"old-value".to_vec();
	let encoded_value = create_inline_value(&value_encoded);

	value_entries.push((value_key, encoded_value));
	let value_table = env.create_test_table(101, value_entries).unwrap();

	// Test non-bottom level compaction
	let iterators: Vec<_> = vec![
		Box::new(tombstone_table.iter(None).unwrap()) as Box<dyn LSMIterator>,
		Box::new(value_table.iter(None).unwrap()) as Box<dyn LSMIterator>,
	];
	let mut comp_iter_non_bottom = CompactionIterator::new(
		iterators,
		create_comparator(),
		false,
		false,
		0,
		Arc::new(MockLogicalClock::new()),
		vec![],
	);
	let non_bottom_result: Vec<_> = comp_iter_non_bottom.by_ref().map(|r| r.unwrap()).collect();

	// Non-bottom level should preserve tombstone
	assert_eq!(non_bottom_result.len(), 1, "Non-bottom level should have 1 entry");
	let (key, _) = &non_bottom_result[0];
	assert_eq!(key.kind(), InternalKeyKind::Delete, "Non-bottom level should preserve tombstone");
	assert_eq!(key.seq_num(), 100, "Should be the newer tombstone");

	// Test bottom level compaction
	let iterators: Vec<_> = vec![
		Box::new(tombstone_table.iter(None).unwrap()) as Box<dyn LSMIterator>,
		Box::new(value_table.iter(None).unwrap()) as Box<dyn LSMIterator>,
	];
	let mut comp_iter_bottom = CompactionIterator::new(
		iterators,
		create_comparator(),
		true,
		false,
		0,
		Arc::new(MockLogicalClock::new()),
		vec![],
	);
	let bottom_result: Vec<_> = comp_iter_bottom.by_ref().map(|r| r.unwrap()).collect();

	// Bottom level should filter out tombstones
	let has_tombstones = bottom_result.iter().any(|(key, _)| key.is_hard_delete_marker());
	assert!(!has_tombstones, "Bottom level should filter out tombstones");

	// Key should be completely gone after tombstone consumes older value
	let has_test_key = bottom_result.iter().any(|(key, _)| &key.user_key == b"test-key");
	assert!(!has_test_key, "test-key should be completely deleted");
	assert_eq!(
		bottom_result.len(),
		0,
		"Bottom level should have no entries when tombstone consumes value"
	);
}

#[test]
fn test_table_properties_population() {
	let env = TestEnv::new();

	let mut entries = Vec::new();
	let expected_deletions = 25u64;
	let expected_tombstones = 25u64;

	for i in 0..100 {
		let key = format!("key-{i:03}").into_bytes();
		let value = format!("value-{i:03}").into_bytes();
		let seq = 1000 + i;

		let kind = match i % 20 {
			0..=11 => InternalKeyKind::Set,
			12..=15 => InternalKeyKind::Delete,
			16..=18 => InternalKeyKind::Merge,
			19 => InternalKeyKind::RangeDelete,
			_ => unreachable!(),
		};

		let internal_key = InternalKey::new(key, seq, kind, 0);

		let entry_value = match kind {
			InternalKeyKind::Delete | InternalKeyKind::RangeDelete => vec![],
			_ => value,
		};

		entries.push((internal_key, entry_value));
	}

	let table_id = 11;
	let table = env.create_test_table(table_id, entries).unwrap();

	let meta = &table.meta;
	let props = &meta.properties;

	// Verify Properties fields
	assert_eq!(props.id, table_id);
	assert_eq!(props.table_format, TableFormat::LSMV1);
	assert_eq!(props.num_entries, 100);
	assert_eq!(props.item_count, 100);
	assert_eq!(props.key_count, 100);
	assert_eq!(props.num_deletions, expected_deletions);
	assert_eq!(props.tombstone_count, expected_tombstones);
	assert_eq!(props.data_size, 2975);
	assert_eq!(props.oldest_vlog_file_id, 0);
	assert_eq!(props.num_data_blocks, 1);

	assert_eq!(props.index_size, 74, "Index size should be tracked");
	assert_eq!(props.index_partitions, 1, "Should have 1 index partition for small table");
	assert_eq!(props.top_level_index_size, 32, "Top-level index size should be tracked");
	// Verify filter metrics (should have bloom filter by default)
	assert_eq!(props.filter_size, 135, "Filter size should be tracked with default bloom filter");
	assert_eq!(props.raw_key_size, 2300, "Raw key size should be tracked");
	assert_eq!(props.raw_value_size, 675, "Raw value size should be tracked");
	assert!(
		props.raw_key_size + props.raw_value_size == props.data_size,
		"Raw sizes should be == data_size"
	);

	// Verify time metrics (timestamps are 0 in this test)
	assert_eq!(props.oldest_key_time, Some(0), "Oldest key time is 0 in test");
	assert_eq!(props.newest_key_time, Some(0), "Newest key time is 0 in test");

	// Verify range deletion metrics
	assert_eq!(props.num_range_deletions, 5, "Should have 5 range deletions (every 20th key)");

	assert!(props.created_at > 0);
	assert_eq!(props.block_size, 2757);
	assert_eq!(props.block_count, 1);
	assert_eq!(props.compression, CompressionType::None);
	assert_eq!(props.seqnos.0, 1000);
	assert_eq!(props.seqnos.1, 1099);
	assert!(meta.smallest_point.is_some());
	assert!(meta.largest_point.is_some());
	if let Some(smallest) = &meta.smallest_point {
		assert_eq!(&smallest.user_key, b"key-000");
	}
	if let Some(largest) = &meta.largest_point {
		assert_eq!(&largest.user_key, b"key-099");
	}

	// Verify TableMetadata fields
	assert_eq!(meta.has_point_keys, Some(true));
	assert_eq!(meta.smallest_seq_num, Some(1000));
	assert_eq!(meta.largest_seq_num, Some(1099));
	assert!(meta.smallest_point.is_some());
	assert!(meta.largest_point.is_some());
	if let Some(ref smallest) = meta.smallest_point {
		assert_eq!(&smallest.user_key, b"key-000");
		assert_eq!(smallest.seq_num(), 1000);
	}
	if let Some(ref largest) = meta.largest_point {
		assert_eq!(&largest.user_key, b"key-099");
		assert_eq!(largest.seq_num(), 1099);
	}

	// Test compaction strategy can use properties
	let opts = create_options_with_compaction_settings(&env.options, 4, 10.0);
	let strategy = Strategy::from_options(opts);
	let mut test_level = Level::with_capacity(10);
	test_level.insert(table);

	let selected = strategy.select_by_compensated_size(&test_level);
	assert_eq!(selected, Some(table_id));
}

#[test(tokio::test)]
async fn test_soft_delete_compaction_behavior() {
	let env = TestEnv::new_with_levels(2); // Only 2 levels: L0 and L1
	let mut levels = Levels::new(3, 10);

	// Create L0 tables: 2 tables to exceed L0 limit (1) and trigger L0→L1
	// compaction
	for table_idx in 0..2 {
		let mut l0_entries = Vec::new();
		for i in (table_idx * 6)..((table_idx + 1) * 6) {
			let key = format!("key-{i:03}").into_bytes();
			let (seq, kind, value) = if i % 3 == 0 {
				// Every 3rd key = soft delete
				(200 + i, InternalKeyKind::SoftDelete, vec![])
			} else if i % 3 == 1 {
				// Every 3rd+1 key = regular delete
				(200 + i, InternalKeyKind::Delete, vec![])
			} else {
				// Every 3rd+2 key = set value
				let value_encoded = format!("l0-value-{i}").into_bytes();
				let encoded_value = create_inline_value(&value_encoded);
				(200 + i, InternalKeyKind::Set, encoded_value)
			};
			l0_entries.push((InternalKey::new(key, seq, kind, 0), value));
		}
		let table = env.create_test_table(100 + table_idx, l0_entries.clone()).unwrap();
		Arc::make_mut(&mut levels.get_levels_mut()[0]).insert(table);
	}

	// Create L1 (last level) with older values for all keys
	let mut l1_entries = Vec::new();
	for i in 0..12 {
		let key = format!("key-{i:03}").into_bytes();
		let value_encoded = format!("l1-old-value-{i}").into_bytes();
		let encoded_value = create_inline_value(&value_encoded);

		l1_entries.push((InternalKey::new(key, 100 + i, InternalKeyKind::Set, 0), encoded_value));
	}
	let l1_table = env.create_test_table(200, l1_entries.clone()).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[1]).insert(l1_table);

	// Create manifest and run L0→L1 compaction
	let manifest_path = env.options.path.join("test_manifest_soft_delete");
	let manifest = LevelManifest {
		path: manifest_path,
		levels,
		hidden_set: HashSet::new(),
		next_table_id: Arc::new(AtomicU64::new(1000)),
		manifest_format_version: crate::levels::MANIFEST_FORMAT_VERSION_V1,
		snapshots: Vec::new(),
		log_number: 0,
		last_sequence: 0,
	};
	write_manifest_to_disk(&manifest).unwrap();
	let manifest = Arc::new(RwLock::new(manifest));

	let opts = create_options_with_compaction_settings(&env.options, 1, 1.0);
	let strategy = Arc::new(Strategy::from_options(opts));
	let mut compaction_options = create_compaction_options(env.options, Arc::clone(&manifest));
	compaction_options.vlog = None;
	let compactor = Compactor::new(compaction_options, strategy);

	compactor.compact().unwrap();

	// Verify that soft deletes flow through compaction normally (like any other
	// key)
	let manifest_guard = manifest.read().unwrap();
	let levels = manifest_guard.levels.get_levels();

	let mut soft_deletes = 0;
	let mut regular_deletes = 0;
	let mut values = 0;

	// Count entries in L1 (bottom level) after compaction
	for table in &levels[1].tables {
		let mut iter = table.iter(None).unwrap();
		iter.seek_first().unwrap();
		while iter.valid() {
			let key = iter.key().to_owned();
			match key.kind() {
				InternalKeyKind::SoftDelete => soft_deletes += 1,
				InternalKeyKind::Delete => regular_deletes += 1,
				InternalKeyKind::Set => values += 1,
				_ => {}
			}
			iter.next().unwrap();
		}
	}

	// Verify exact counts
	assert_eq!(soft_deletes, 4, "Should have exactly 4 soft deletes (keys 0, 3, 6, 9)");
	assert_eq!(regular_deletes, 0, "Regular deletes should be filtered out at the bottom level");
	assert_eq!(values, 4, "Should have exactly 4 values (keys 2, 5, 8, 11)");

	// Verify that values are the latest and correct
	let mut found_keys = HashSet::new();
	for table in &levels[1].tables {
		let mut iter = table.iter(None).unwrap();
		iter.seek_first().unwrap();
		while iter.valid() {
			let key = iter.key().to_owned();
			let value = iter.value_encoded().unwrap().to_vec();
			match key.kind() {
				InternalKeyKind::Set => {
					let key_str = String::from_utf8(key.user_key.clone()).unwrap();

					// Decode the ValueLocation to get the actual value
					let location = crate::vlog::ValueLocation::decode(&value).unwrap();
					let actual_value = if location.is_value_pointer() {
						panic!("Unexpected VLog pointer in test");
					} else {
						(*location.value).to_vec()
					};
					let value_str = String::from_utf8(actual_value).unwrap();

					// Verify we get the latest L0 values, not the old L1 values
					if key_str.starts_with("key-") {
						let key_num: usize = key_str.split('-').nth(1).unwrap().parse().unwrap();
						if key_num % 3 == 2 {
							// These should be Set values
							assert!(
								value_str.starts_with("l0-value-"),
								"Key {} should have L0 value, got: {}",
								key_str,
								value_str
							);
							found_keys.insert(key_str);
						}
					}
				}
				InternalKeyKind::SoftDelete => {
					let key_str = String::from_utf8(key.user_key.clone()).unwrap();
					if key_str.starts_with("key-") {
						let key_num: usize = key_str.split('-').nth(1).unwrap().parse().unwrap();
						assert_eq!(key_num % 3, 0, "Soft delete should be on keys 0, 3, 6, 9");
					}
				}
				_ => {}
			}
			iter.next().unwrap();
		}
	}

	// Verify we found all expected Set keys
	assert_eq!(found_keys.len(), 4, "Should have found all 4 Set keys");
	for i in [2, 5, 8, 11] {
		let expected_key = format!("key-{:03}", i);
		assert!(found_keys.contains(&expected_key), "Missing expected key: {}", expected_key);
	}
}

#[test(tokio::test)]
async fn test_older_soft_delete_marked_stale_during_compaction() {
	// This test verifies that when a key has multiple versions including older soft
	// deletes, compaction correctly handles the older soft deletes when marking
	// them as stale.
	//
	// Scenario: key "test-key" has 3 versions:
	//   - SoftDelete (seq 300) - LATEST
	//   - Set (seq 200)        - older
	//   - SoftDelete (seq 100) - oldest
	//
	// With versioning disabled, the older versions should be marked stale.
	// The bug was that older soft deletes (which have empty values) would crash
	// when the code tried to decode them as ValueLocation.

	let env = TestEnv::new_with_levels(2);
	let mut levels = Levels::new(3, 10);

	let key = b"test-key".to_vec();

	// Create L0 table with latest SoftDelete (seq 300)
	let l0_entries = vec![(
		InternalKey::new(key.clone(), 300, InternalKeyKind::SoftDelete, 0),
		vec![], // SoftDelete has empty value
	)];
	let l0_table = env.create_test_table(100, l0_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[0]).insert(l0_table);

	// Create L1 table with older Set (seq 200) and oldest SoftDelete (seq 100)
	let value_encoded = b"some-value".to_vec();
	let encoded_value = create_inline_value(&value_encoded);
	let l1_entries = vec![
		(InternalKey::new(key.clone(), 200, InternalKeyKind::Set, 0), encoded_value),
		(
			InternalKey::new(key, 100, InternalKeyKind::SoftDelete, 0),
			vec![], // Older SoftDelete also has empty value
		),
	];
	let l1_table = env.create_test_table(200, l1_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[1]).insert(l1_table);

	// Create manifest and run compaction
	let manifest_path = env.options.path.join("test_manifest_older_soft_delete");
	let manifest = LevelManifest {
		path: manifest_path,
		levels,
		hidden_set: HashSet::new(),
		next_table_id: Arc::new(AtomicU64::new(1000)),
		manifest_format_version: crate::levels::MANIFEST_FORMAT_VERSION_V1,
		snapshots: Vec::new(),
		log_number: 0,
		last_sequence: 0,
	};
	write_manifest_to_disk(&manifest).unwrap();
	let manifest = Arc::new(RwLock::new(manifest));

	let opts = create_options_with_compaction_settings(&env.options, 1, 1.0);
	let strategy = Arc::new(Strategy::from_options(opts));
	// NOTE: Do NOT set vlog = None - we need VLog enabled to trigger the bug
	let compaction_options = create_compaction_options(env.options, Arc::clone(&manifest));

	let compactor = Compactor::new(compaction_options, strategy);

	// This should NOT panic - older soft deletes should be handled correctly
	compactor.compact().unwrap();

	// Verify the result: only the latest SoftDelete should remain
	let manifest_guard = manifest.read().unwrap();
	let levels = manifest_guard.levels.get_levels();

	let mut soft_deletes = 0;
	let mut sets = 0;

	for table in &levels[1].tables {
		let mut iter = table.iter(None).unwrap();
		iter.seek_first().unwrap();
		while iter.valid() {
			let key = iter.key().to_owned();
			match key.kind() {
				InternalKeyKind::SoftDelete => soft_deletes += 1,
				InternalKeyKind::Set => sets += 1,
				_ => {}
			}
			iter.next().unwrap();
		}
	}

	// Only the latest SoftDelete (seq 300) should remain
	assert_eq!(soft_deletes, 1, "Should have exactly 1 soft delete (the latest)");
	assert_eq!(sets, 0, "Should have no Set entries (superseded by SoftDelete)");
}

#[test]
fn test_score_based_level_selection() {
	let env = TestEnv::new();
	// Ensure manifest directory exists
	std::fs::create_dir_all(env.options.manifest_dir()).unwrap();
	let mut manifest = LevelManifest::new(Arc::clone(&env.options)).unwrap();

	// Create options with specific compaction settings
	let mut opts = (*env.options).clone();
	opts.level0_max_files = 4;
	opts.max_bytes_for_level = 100 * 1024 * 1024; // 100MB
	opts.level_multiplier = 10.0;
	let opts = Arc::new(opts);
	let strategy = Strategy::from_options(opts);

	// Add 5 L0 files (score = 5/4 = 1.25)
	for i in 0..5 {
		let entries = create_ordered_entries("l0_key", i, 1, i as u64, None);
		let table = env.create_test_table(i as u64, entries).unwrap();
		Arc::make_mut(&mut manifest.levels.get_levels_mut()[0]).insert(table);
	}

	// Add L1 files totaling 150MB (score = 150/100 = 1.5)
	// Note: Actual file sizes will be determined by content, but we test the score logic
	for i in 0..3 {
		let entries = create_ordered_entries("l1_key", i, 10, (10 + i) as u64, None);
		let table = env.create_test_table((10 + i) as u64, entries).unwrap();
		Arc::make_mut(&mut manifest.levels.get_levels_mut()[1]).insert(table);
	}

	// Score-based selection should pick the level with highest score
	let choice = strategy.pick_levels(&manifest).unwrap();
	match choice {
		CompactionChoice::Merge(input) => {
			// Should pick a level that needs compaction (score >= 1.0)
			assert!(input.source_level <= 1, "Should pick L0 or L1");
		}
		CompactionChoice::Skip => panic!("Should not skip when levels need compaction"),
	}
}

#[test]
fn test_bytes_based_level_limits() {
	let env = TestEnv::new();
	// Ensure manifest directory exists
	std::fs::create_dir_all(env.options.manifest_dir()).unwrap();
	let mut manifest = LevelManifest::new(Arc::clone(&env.options)).unwrap();

	// Create options with bytes-based limits
	let mut opts = (*env.options).clone();
	opts.max_bytes_for_level = 100 * 1024 * 1024; // 100MB base
	opts.level_multiplier = 10.0;
	let opts = Arc::new(opts);
	let strategy = Strategy::from_options(opts);

	// Add L1 files - actual sizes will be based on content
	// The score calculation uses actual file sizes from the tables
	for i in 0..3 {
		let entries = create_ordered_entries("l1_key", i, 100, i as u64, None);
		let table = env.create_test_table(i as u64, entries).unwrap();
		Arc::make_mut(&mut manifest.levels.get_levels_mut()[1]).insert(table);
	}

	// Check that L1 can be selected if it exceeds limit
	let choice = strategy.pick_levels(&manifest).unwrap();
	match choice {
		CompactionChoice::Merge(input) => {
			// Should pick a level that needs compaction
			assert!(input.source_level <= 2, "Should pick a valid level");
		}
		CompactionChoice::Skip => {
			// Skip is OK if levels don't exceed limits yet
		}
	}
}

#[test]
fn test_bottom_level_compaction() {
	let env = TestEnv::new_with_levels(3); // 3 levels: L0, L1, L2 (L2 is bottom)
										// Ensure manifest directory exists
	std::fs::create_dir_all(env.options.manifest_dir()).unwrap();
	let mut manifest = LevelManifest::new(Arc::clone(&env.options)).unwrap();

	// Create options
	let mut opts = (*env.options).clone();
	opts.max_bytes_for_level = 100 * 1024 * 1024; // 100MB
	opts.level_multiplier = 10.0;
	let opts = Arc::new(opts);
	let strategy = Strategy::from_options(opts);

	// Add L2 files (bottom level)
	for i in 0..3 {
		let entries = create_ordered_entries("l2_key", i, 100, (20 + i) as u64, None);
		let table = env.create_test_table((20 + i) as u64, entries).unwrap();
		Arc::make_mut(&mut manifest.levels.get_levels_mut()[2]).insert(table);
	}

	// Bottom level compaction should be allowed (same-level compaction)
	// Note: This will only trigger if L2 exceeds its byte limit
	let choice = strategy.pick_levels(&manifest).unwrap();
	match choice {
		CompactionChoice::Merge(input) => {
			// If L2 is selected, it should compact to same level
			if input.source_level == 2 {
				assert_eq!(
					input.target_level, 2,
					"Should compact to same level (tombstone cleanup)"
				);
			}
		}
		CompactionChoice::Skip => {
			// Skip is OK if L2 doesn't exceed limit
		}
	}
}

fn create_test_table_with_bounds(
	temp_dir: &TempDir,
	opts: &Arc<Options>,
	id: u64,
	smallest_key: &[u8],
	largest_key: &[u8],
) -> Arc<crate::sstable::table::Table> {
	// Ensure the sstable directory exists in the temp directory
	let sstable_dir = temp_dir.path().join("sstables");
	std::fs::create_dir_all(&sstable_dir).unwrap();

	let table_path = opts.sstable_file_path(id);
	let file = File::create(&table_path).unwrap();

	let mut writer = crate::sstable::table::TableWriter::new(file, id, Arc::clone(opts), 0);

	// Add smallest key
	let small_key = InternalKey::new(smallest_key.to_vec(), 1, InternalKeyKind::Set, 0);
	let value = ValueLocation::with_inline_value(b"v".to_vec()).encode();
	writer.add(small_key, &value).unwrap();

	// Add largest key (if different)
	if smallest_key != largest_key {
		let large_key = InternalKey::new(largest_key.to_vec(), 2, InternalKeyKind::Set, 0);
		writer.add(large_key, &value).unwrap();
	}

	writer.finish().unwrap();

	let file = std::fs::File::open(&table_path).unwrap();
	let file_size = file.metadata().unwrap().len();
	let table =
		crate::sstable::table::Table::new(id, Arc::clone(opts), Arc::new(file), file_size).unwrap();
	Arc::new(table)
}

#[test]
fn test_combined_key_range_single_table() {
	let temp_dir = TempDir::new().unwrap();
	let opts = Arc::new(Options {
		path: temp_dir.path().to_path_buf(),
		..Default::default()
	});

	let mut level = Level::with_capacity(10);
	let table = create_test_table_with_bounds(&temp_dir, &opts, 1, b"a", b"z");
	level.insert(table);

	let mut ids = HashSet::new();
	ids.insert(1);

	let result = Strategy::combined_key_range(level.tables.iter().filter(|t| ids.contains(&t.id)));
	assert!(result.is_some());
	if let Some((std::ops::Bound::Included(smallest), std::ops::Bound::Included(largest))) = result
	{
		assert_eq!(&smallest.user_key, b"a");
		assert_eq!(&largest.user_key, b"z");
	} else {
		panic!("Expected Included bounds");
	}
}

#[test]
fn test_combined_key_range_multiple_tables() {
	let temp_dir = TempDir::new().unwrap();
	let opts = Arc::new(Options {
		path: temp_dir.path().to_path_buf(),
		..Default::default()
	});

	let mut level = Level::with_capacity(10);
	let table1 = create_test_table_with_bounds(&temp_dir, &opts, 1, b"a", b"m");
	let table2 = create_test_table_with_bounds(&temp_dir, &opts, 2, b"g", b"z");
	level.insert(table1);
	level.insert(table2);

	let mut ids = HashSet::new();
	ids.insert(1);
	ids.insert(2);

	let result = Strategy::combined_key_range(level.tables.iter().filter(|t| ids.contains(&t.id)));
	assert!(result.is_some());
	if let Some((std::ops::Bound::Included(smallest), std::ops::Bound::Included(largest))) = result
	{
		assert_eq!(&smallest.user_key, b"a");
		assert_eq!(&largest.user_key, b"z");
	} else {
		panic!("Expected Included bounds");
	}
}

#[test]
fn test_combined_key_range_overlapping_tables() {
	let temp_dir = TempDir::new().unwrap();
	let opts = Arc::new(Options {
		path: temp_dir.path().to_path_buf(),
		..Default::default()
	});

	let mut level = Level::with_capacity(10);
	let table1 = create_test_table_with_bounds(&temp_dir, &opts, 1, b"a", b"m");
	let table2 = create_test_table_with_bounds(&temp_dir, &opts, 2, b"m", b"t");
	let table3 = create_test_table_with_bounds(&temp_dir, &opts, 3, b"t", b"v");
	level.insert(table1);
	level.insert(table2);
	level.insert(table3);

	let mut ids = HashSet::new();
	ids.insert(1);
	ids.insert(2);
	ids.insert(3);

	let result = Strategy::combined_key_range(level.tables.iter().filter(|t| ids.contains(&t.id)));
	assert!(result.is_some());
	if let Some((std::ops::Bound::Included(smallest), std::ops::Bound::Included(largest))) = result
	{
		assert_eq!(&smallest.user_key, b"a");
		assert_eq!(&largest.user_key, b"v");
	} else {
		panic!("Expected Included bounds");
	}
}

#[test]
fn test_combined_key_range_empty_ids() {
	let temp_dir = TempDir::new().unwrap();
	let opts = Arc::new(Options {
		path: temp_dir.path().to_path_buf(),
		..Default::default()
	});

	let mut level = Level::with_capacity(10);
	let table = create_test_table_with_bounds(&temp_dir, &opts, 1, b"a", b"z");
	level.insert(table);

	let ids: HashSet<u64> = HashSet::new();

	let result = Strategy::combined_key_range(level.tables.iter().filter(|t| ids.contains(&t.id)));
	assert_eq!(result, None);
}

#[test]
fn test_select_overlapping_ranges_no_overlap() {
	let temp_dir = TempDir::new().unwrap();
	let opts = Arc::new(Options {
		path: temp_dir.path().to_path_buf(),
		..Default::default()
	});

	let mut level = Level::with_capacity(10);
	let table1 = create_test_table_with_bounds(&temp_dir, &opts, 1, b"a", b"b");
	let table2 = create_test_table_with_bounds(&temp_dir, &opts, 2, b"d", b"e");
	let table3 = create_test_table_with_bounds(&temp_dir, &opts, 3, b"g", b"h");
	level.insert(table1);
	level.insert(table2);
	level.insert(table3);

	let result = Strategy::select_overlapping_ranges(&level, 2).unwrap();
	assert_eq!(result.len(), 1);
	assert!(result.contains(&2));
}

#[test]
fn test_select_overlapping_ranges_direct_overlap() {
	let temp_dir = TempDir::new().unwrap();
	let opts = Arc::new(Options {
		path: temp_dir.path().to_path_buf(),
		..Default::default()
	});

	let mut level = Level::with_capacity(10);
	let table1 = create_test_table_with_bounds(&temp_dir, &opts, 1, b"a", b"c");
	let table2 = create_test_table_with_bounds(&temp_dir, &opts, 2, b"c", b"e");
	level.insert(table1);
	level.insert(table2);

	let result = Strategy::select_overlapping_ranges(&level, 1).unwrap();
	assert_eq!(result.len(), 2);
	assert!(result.contains(&1));
	assert!(result.contains(&2));
}

#[test]
fn test_select_overlapping_ranges_chain() {
	let temp_dir = TempDir::new().unwrap();
	let opts = Arc::new(Options {
		path: temp_dir.path().to_path_buf(),
		..Default::default()
	});

	let mut level = Level::with_capacity(10);
	let table1 = create_test_table_with_bounds(&temp_dir, &opts, 1, b"a", b"b");
	let table2 = create_test_table_with_bounds(&temp_dir, &opts, 2, b"b", b"c");
	let table3 = create_test_table_with_bounds(&temp_dir, &opts, 3, b"c", b"d");
	let table4 = create_test_table_with_bounds(&temp_dir, &opts, 4, b"e", b"f");
	level.insert(table1);
	level.insert(table2);
	level.insert(table3);
	level.insert(table4);

	let result = Strategy::select_overlapping_ranges(&level, 2).unwrap();
	assert_eq!(result.len(), 3);
	assert!(result.contains(&1));
	assert!(result.contains(&2));
	assert!(result.contains(&3));
	assert!(!result.contains(&4));
}

#[test]
fn test_select_overlapping_ranges_full_overlap() {
	let temp_dir = TempDir::new().unwrap();
	let opts = Arc::new(Options {
		path: temp_dir.path().to_path_buf(),
		..Default::default()
	});

	let mut level = Level::with_capacity(10);
	let table1 = create_test_table_with_bounds(&temp_dir, &opts, 1, b"a", b"z");
	let table2 = create_test_table_with_bounds(&temp_dir, &opts, 2, b"b", b"c");
	level.insert(table1);
	level.insert(table2);

	let result = Strategy::select_overlapping_ranges(&level, 1).unwrap();
	assert_eq!(result.len(), 2);
	assert!(result.contains(&1));
	assert!(result.contains(&2));
}

#[test]
fn test_select_overlapping_ranges_adjacent_no_overlap() {
	let temp_dir = TempDir::new().unwrap();
	let opts = Arc::new(Options {
		path: temp_dir.path().to_path_buf(),
		..Default::default()
	});

	let mut level = Level::with_capacity(10);
	let table1 = create_test_table_with_bounds(&temp_dir, &opts, 1, b"a", b"b");
	let table2 = create_test_table_with_bounds(&temp_dir, &opts, 2, b"c", b"d");
	level.insert(table1);
	level.insert(table2);

	let result = Strategy::select_overlapping_ranges(&level, 1).unwrap();
	assert_eq!(result.len(), 1);
	assert!(result.contains(&1));
}

#[test]
fn test_expand_same_user_key_different_seq() {
	let temp_dir = TempDir::new().unwrap();
	let opts = Arc::new(Options {
		path: temp_dir.path().to_path_buf(),
		..Default::default()
	});

	let mut level = Level::with_capacity(10);

	// Ensure the sstable directory exists in the temp directory
	let sstable_dir = temp_dir.path().join("sstables");
	std::fs::create_dir_all(&sstable_dir).unwrap();

	// File 1: user_key="foo" at seq=100 (newest)
	let table_path1 = opts.sstable_file_path(1);
	let file1 = File::create(&table_path1).unwrap();
	let mut writer1 = crate::sstable::table::TableWriter::new(file1, 1, Arc::clone(&opts), 0);
	let key1 = InternalKey::new(b"foo".to_vec(), 100, InternalKeyKind::Set, 0);
	let value = ValueLocation::with_inline_value(b"v1".to_vec()).encode();
	writer1.add(key1, &value).unwrap();
	writer1.finish().unwrap();
	let file1 = std::fs::File::open(&table_path1).unwrap();
	let file_size1 = file1.metadata().unwrap().len();
	let table1 =
		crate::sstable::table::Table::new(1, Arc::clone(&opts), Arc::new(file1), file_size1)
			.unwrap();
	level.insert(Arc::new(table1));

	// File 2: user_key="foo" at seq=50 (older version)
	let table_path2 = opts.sstable_file_path(2);
	let file2 = File::create(&table_path2).unwrap();
	let mut writer2 = crate::sstable::table::TableWriter::new(file2, 2, Arc::clone(&opts), 0);
	let key2 = InternalKey::new(b"foo".to_vec(), 50, InternalKeyKind::Set, 0);
	writer2.add(key2, &value).unwrap();
	writer2.finish().unwrap();
	let file2 = std::fs::File::open(&table_path2).unwrap();
	let file_size2 = file2.metadata().unwrap().len();
	let table2 =
		crate::sstable::table::Table::new(2, Arc::clone(&opts), Arc::new(file2), file_size2)
			.unwrap();
	level.insert(Arc::new(table2));

	// When selecting File 1, File 2 MUST be included (same user key, different seq)
	let result = Strategy::select_overlapping_ranges(&level, 1).unwrap();
	assert_eq!(result.len(), 2, "Both files with same user key should be included");
	assert!(result.contains(&1));
	assert!(result.contains(&2));
}

#[test]
fn test_select_overlapping_ranges_nonexistent_id() {
	let temp_dir = TempDir::new().unwrap();
	let opts = Arc::new(Options {
		path: temp_dir.path().to_path_buf(),
		..Default::default()
	});

	let mut level = Level::with_capacity(10);
	let table = create_test_table_with_bounds(&temp_dir, &opts, 1, b"a", b"z");
	level.insert(table);

	let result = Strategy::select_overlapping_ranges(&level, 999);
	assert!(result.is_err());
	match result {
		Err(crate::error::Error::TableNotFound(id)) => assert_eq!(id, 999),
		_ => panic!("Expected TableNotFound error"),
	}
}

/// Helper function to create entries with specific string keys
fn create_entries_with_keys(keys: &[&str], seq_num: u64) -> Vec<(InternalKey, Vec<u8>)> {
	let mut entries = Vec::new();
	for (i, key) in keys.iter().enumerate() {
		let user_key = key.as_bytes().to_vec();
		let internal_key = InternalKey::new(user_key, seq_num + i as u64, InternalKeyKind::Set, 0);
		let value = format!("value-{}", key).into_bytes();
		let encoded_value = create_inline_value(&value);
		entries.push((internal_key, encoded_value));
	}
	entries
}

#[test]
fn test_clean_cut_shared_boundary_key() {
	let env = TestEnv::new();

	// Create L1 with files that share a boundary key "foo"
	// File 1: keys "a" to "foo" (boundary)
	// File 2: keys "foo" to "m" (shares boundary with file 1)
	// File 3: keys "n" to "z" (no shared boundary)

	let mut levels = Levels::new(3, 10);

	// File 1: a, b, c, ..., foo
	let mut file1_keys: Vec<String> = (b'a'..=b'f').map(|c| char::from(c).to_string()).collect();
	file1_keys.push("foo".to_string());
	let file1_keys_refs: Vec<&str> = file1_keys.iter().map(|s| s.as_str()).collect();
	let file1_entries = create_entries_with_keys(&file1_keys_refs, 100);
	let table1 = env.create_test_table(1, file1_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[1]).insert(table1);

	// File 2: foo, g, h, ..., m
	let mut file2_keys: Vec<String> = vec!["foo".to_string()];
	file2_keys.extend((b'g'..=b'm').map(|c| char::from(c).to_string()));
	let file2_keys_refs: Vec<&str> = file2_keys.iter().map(|s| s.as_str()).collect();
	let file2_entries = create_entries_with_keys(&file2_keys_refs, 200);
	let table2 = env.create_test_table(2, file2_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[1]).insert(table2);

	// File 3: n, o, p, ..., z (no shared boundary)
	let file3_keys: Vec<String> = (b'n'..=b'z').map(|c| char::from(c).to_string()).collect();
	let file3_keys_refs: Vec<&str> = file3_keys.iter().map(|s| s.as_str()).collect();
	let file3_entries = create_entries_with_keys(&file3_keys_refs, 300);
	let table3 = env.create_test_table(3, file3_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[1]).insert(table3);

	// Create manifest
	let manifest_path = env.options.path.join("test_manifest");
	let manifest = LevelManifest {
		path: manifest_path,
		levels,
		hidden_set: HashSet::new(),
		next_table_id: Arc::new(AtomicU64::new(1000)),
		manifest_format_version: crate::levels::MANIFEST_FORMAT_VERSION_V1,
		snapshots: Vec::new(),
		log_number: 0,
		last_sequence: 0,
	};
	write_manifest_to_disk(&manifest).unwrap();
	let manifest = Arc::new(RwLock::new(manifest));

	let levels_guard = manifest.read().unwrap();
	let source_level = &levels_guard.levels.get_levels()[1];

	// Test clean cut expansion directly: selecting File 1 should expand to include File 2
	let selected = Strategy::select_overlapping_ranges(source_level, 1).unwrap();

	// Verify file 1 and file 2 are both included (shared boundary "foo")
	assert!(selected.contains(&1), "File 1 should be included (initially selected)");
	assert!(
		selected.contains(&2),
		"File 2 should be included (shares boundary key 'foo' with file 1)"
	);
	// Verify file 3 is NOT included (no shared boundary)
	assert!(!selected.contains(&3), "File 3 should NOT be included (no shared boundary)");
}

#[test]
fn test_clean_cut_chain_expansion() {
	let env = TestEnv::new();

	// Create L1 with files forming a chain of shared boundaries
	// File 1: keys "a" to "b" (boundary "b")
	// File 2: keys "b" to "c" (shares "b" with file 1, boundary "c")
	// File 3: keys "c" to "d" (shares "c" with file 2)
	// File 4: keys "e" to "f" (no shared boundary)

	let mut levels = Levels::new(3, 10);

	// File 1: a, b
	let file1_entries = create_entries_with_keys(&["a", "b"], 100);
	let table1 = env.create_test_table(1, file1_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[1]).insert(table1);

	// File 2: b, c
	let file2_entries = create_entries_with_keys(&["b", "c"], 200);
	let table2 = env.create_test_table(2, file2_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[1]).insert(table2);

	// File 3: c, d
	let file3_entries = create_entries_with_keys(&["c", "d"], 300);
	let table3 = env.create_test_table(3, file3_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[1]).insert(table3);

	// File 4: e, f (no shared boundary)
	let file4_entries = create_entries_with_keys(&["e", "f"], 400);
	let table4 = env.create_test_table(4, file4_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[1]).insert(table4);

	// Create manifest
	let manifest_path = env.options.path.join("test_manifest");
	let manifest = LevelManifest {
		path: manifest_path,
		levels,
		hidden_set: HashSet::new(),
		next_table_id: Arc::new(AtomicU64::new(1000)),
		manifest_format_version: crate::levels::MANIFEST_FORMAT_VERSION_V1,
		snapshots: Vec::new(),
		log_number: 0,
		last_sequence: 0,
	};
	write_manifest_to_disk(&manifest).unwrap();
	let manifest = Arc::new(RwLock::new(manifest));

	let levels_guard = manifest.read().unwrap();
	let source_level = &levels_guard.levels.get_levels()[1];

	// Test clean cut expansion directly: selecting File 2 should expand to include Files 1 and 3
	let selected = Strategy::select_overlapping_ranges(source_level, 2).unwrap();

	// Verify all three files in the chain are included
	assert!(selected.contains(&1), "File 1 should be included (chain expansion)");
	assert!(selected.contains(&2), "File 2 should be included (initially selected)");
	assert!(selected.contains(&3), "File 3 should be included (chain expansion)");
	// Verify file 4 is NOT included (no shared boundary)
	assert!(!selected.contains(&4), "File 4 should NOT be included (no shared boundary)");
}

#[test]
fn test_clean_cut_no_expansion_needed() {
	let env = TestEnv::new();

	// Create L1 with files that have no shared boundaries
	// File 1: keys "a" to "b"
	// File 2: keys "d" to "e"
	// File 3: keys "g" to "h"

	let mut levels = Levels::new(3, 10);

	// File 1: a, b
	let file1_entries = create_entries_with_keys(&["a", "b"], 100);
	let table1 = env.create_test_table(1, file1_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[1]).insert(table1);

	// File 2: d, e
	let file2_entries = create_entries_with_keys(&["d", "e"], 200);
	let table2 = env.create_test_table(2, file2_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[1]).insert(table2);

	// File 3: g, h
	let file3_entries = create_entries_with_keys(&["g", "h"], 300);
	let table3 = env.create_test_table(3, file3_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[1]).insert(table3);

	// Create manifest
	let manifest_path = env.options.path.join("test_manifest");
	let manifest = LevelManifest {
		path: manifest_path,
		levels,
		hidden_set: HashSet::new(),
		next_table_id: Arc::new(AtomicU64::new(1000)),
		manifest_format_version: crate::levels::MANIFEST_FORMAT_VERSION_V1,
		snapshots: Vec::new(),
		log_number: 0,
		last_sequence: 0,
	};
	write_manifest_to_disk(&manifest).unwrap();
	let manifest = Arc::new(RwLock::new(manifest));

	let levels_guard = manifest.read().unwrap();
	let source_level = &levels_guard.levels.get_levels()[1];

	// Test clean cut expansion directly: selecting File 1 should not expand (no shared boundaries)
	let selected = Strategy::select_overlapping_ranges(source_level, 1).unwrap();

	// Verify only File 1 is selected (no expansion needed)
	assert_eq!(
		selected.len(),
		1,
		"Only one file should be selected when there are no shared boundaries"
	);
	assert!(selected.contains(&1), "File 1 should be the only selected file");
}

#[test]
fn test_clean_cut_integration_shared_boundary() {
	let env = TestEnv::new();

	// Create L1 with files where File 1 is largest and shares boundary with File 2
	// File 1: keys "a" to "foo" (largest file - will be selected by ByCompensatedSize)
	// File 2: keys "foo" to "m" (shares boundary with file 1)
	// File 3: keys "n" to "z" (no shared boundary)

	let mut levels = Levels::new(3, 10);

	// File 1: a, b, c, ..., foo (make it largest by adding keys BEFORE foo)
	let mut file1_keys: Vec<String> = Vec::new();
	// Add many keys in the range before "foo"
	for i in 0..20 {
		file1_keys.push(format!("a{:02}", i));
	}
	file1_keys.extend((b'b'..=b'f').map(|c| char::from(c).to_string()));
	file1_keys.push("foo".to_string());
	let file1_keys_refs: Vec<&str> = file1_keys.iter().map(|s| s.as_str()).collect();
	let file1_entries = create_entries_with_keys(&file1_keys_refs, 100);
	let table1 = env.create_test_table(1, file1_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[1]).insert(table1);

	// File 2: foo, g, h, ..., m (smaller)
	let mut file2_keys: Vec<String> = vec!["foo".to_string()];
	file2_keys.extend((b'g'..=b'm').map(|c| char::from(c).to_string()));
	let file2_keys_refs: Vec<&str> = file2_keys.iter().map(|s| s.as_str()).collect();
	let file2_entries = create_entries_with_keys(&file2_keys_refs, 200);
	let table2 = env.create_test_table(2, file2_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[1]).insert(table2);

	// File 3: n, o, p, ..., z (smaller - no shared boundary)
	let file3_keys: Vec<String> = (b'n'..=b'z').map(|c| char::from(c).to_string()).collect();
	let file3_keys_refs: Vec<&str> = file3_keys.iter().map(|s| s.as_str()).collect();
	let file3_entries = create_entries_with_keys(&file3_keys_refs, 300);
	let table3 = env.create_test_table(3, file3_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[1]).insert(table3);

	// Create manifest
	let manifest_path = env.options.path.join("test_manifest");
	let manifest = LevelManifest {
		path: manifest_path,
		levels,
		hidden_set: HashSet::new(),
		next_table_id: Arc::new(AtomicU64::new(1000)),
		manifest_format_version: crate::levels::MANIFEST_FORMAT_VERSION_V1,
		snapshots: Vec::new(),
		log_number: 0,
		last_sequence: 0,
	};
	write_manifest_to_disk(&manifest).unwrap();
	let manifest = Arc::new(RwLock::new(manifest));

	// Create strategy with default priority (ByCompensatedSize)
	let opts = create_options_with_compaction_settings(&env.options, 4, 2.0);
	let strategy = Strategy::from_options(opts);

	let levels_guard = manifest.read().unwrap();
	let source_level = &levels_guard.levels.get_levels()[1];
	let next_level = &levels_guard.levels.get_levels()[2]; // Empty L2

	// File 1 should be selected (largest), and should expand to include File 2
	let selected = strategy.select_tables_for_compaction(source_level, next_level, 1).unwrap();

	// Verify File 1 and File 2 are both included (shared "foo" boundary)
	assert!(selected.contains(&1), "File 1 should be included (initially selected as largest)");
	assert!(
		selected.contains(&2),
		"File 2 should be included (shares boundary key 'foo' with file 1)"
	);
	// Verify File 3 is NOT included (no shared boundary)
	assert!(!selected.contains(&3), "File 3 should NOT be included (no shared boundary)");
}

#[test]
fn test_clean_cut_integration_chain_expansion() {
	let env = TestEnv::new();

	// Create L1 with files forming a chain, File 2 is largest
	// File 1: keys "a" to "b"
	// File 2: keys "b" to "c" (largest - will be selected)
	// File 3: keys "c" to "d" (shares boundary with file 2)
	// File 4: keys "e" to "f" (no shared boundary)

	let mut levels = Levels::new(3, 10);

	// File 1: a, b (smallest)
	let file1_entries = create_entries_with_keys(&["a", "b"], 100);
	let table1 = env.create_test_table(1, file1_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[1]).insert(table1);

	// File 2: b, c (largest - add more entries to make it largest)
	// File 2: b, c (largest - add more entries BEFORE "c" to make it largest)
	let mut file2_keys = vec!["b".to_string()];
	// Add more keys between "b" and "c" to make File 2 the largest
	for i in 0..10 {
		file2_keys.push(format!("b{}", i)); // "b0", "b1", ..., "b9" are all < "c"
	}
	file2_keys.push("c".to_string()); // "c" is still the largest key
	let file2_keys_refs: Vec<&str> = file2_keys.iter().map(|s| s.as_str()).collect();
	let file2_entries = create_entries_with_keys(&file2_keys_refs, 200);
	let table2 = env.create_test_table(2, file2_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[1]).insert(table2);

	// File 3: c, d
	let file3_entries = create_entries_with_keys(&["c", "d"], 300);
	let table3 = env.create_test_table(3, file3_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[1]).insert(table3);

	// File 4: e, f (no shared boundary)
	let file4_entries = create_entries_with_keys(&["e", "f"], 400);
	let table4 = env.create_test_table(4, file4_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[1]).insert(table4);

	// Create manifest
	let manifest_path = env.options.path.join("test_manifest");
	let manifest = LevelManifest {
		path: manifest_path,
		levels,
		hidden_set: HashSet::new(),
		next_table_id: Arc::new(AtomicU64::new(1000)),
		manifest_format_version: crate::levels::MANIFEST_FORMAT_VERSION_V1,
		snapshots: Vec::new(),
		log_number: 0,
		last_sequence: 0,
	};
	write_manifest_to_disk(&manifest).unwrap();
	let manifest = Arc::new(RwLock::new(manifest));

	// Create strategy with default priority (ByCompensatedSize)
	let opts = create_options_with_compaction_settings(&env.options, 4, 2.0);
	let strategy = Strategy::from_options(opts);

	let levels_guard = manifest.read().unwrap();
	let source_level = &levels_guard.levels.get_levels()[1];
	let next_level = &levels_guard.levels.get_levels()[2]; // Empty L2

	// File 2 should be selected (largest), and should expand to include Files 1 and 3
	let selected = strategy.select_tables_for_compaction(source_level, next_level, 1).unwrap();

	// Verify all three files in the chain are included
	assert!(selected.contains(&1), "File 1 should be included (chain expansion)");
	assert!(selected.contains(&2), "File 2 should be included (initially selected as largest)");
	assert!(selected.contains(&3), "File 3 should be included (chain expansion)");
	// Verify File 4 is NOT included (no shared boundary)
	assert!(!selected.contains(&4), "File 4 should NOT be included (no shared boundary)");
}

#[test]
fn test_clean_cut_integration_with_oldest_seq_priority() {
	let env = TestEnv::new();

	// Create L1 with files where File 1 has oldest sequence
	// File 1: seq 100, keys "a" to "foo"
	// File 2: seq 200, keys "foo" to "m"
	// File 3: seq 300, keys "n" to "z"

	let mut levels = Levels::new(3, 10);

	// File 1: a, b, c, ..., foo (oldest sequence)
	let mut file1_keys: Vec<String> = (b'a'..=b'f').map(|c| char::from(c).to_string()).collect();
	file1_keys.push("foo".to_string());
	let file1_keys_refs: Vec<&str> = file1_keys.iter().map(|s| s.as_str()).collect();
	let file1_entries = create_entries_with_keys(&file1_keys_refs, 100);
	let table1 = env.create_test_table(1, file1_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[1]).insert(table1);

	// File 2: foo, g, h, ..., m
	let mut file2_keys: Vec<String> = vec!["foo".to_string()];
	file2_keys.extend((b'g'..=b'm').map(|c| char::from(c).to_string()));
	let file2_keys_refs: Vec<&str> = file2_keys.iter().map(|s| s.as_str()).collect();
	let file2_entries = create_entries_with_keys(&file2_keys_refs, 200);
	let table2 = env.create_test_table(2, file2_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[1]).insert(table2);

	// File 3: n, o, p, ..., z
	let file3_keys: Vec<String> = (b'n'..=b'z').map(|c| char::from(c).to_string()).collect();
	let file3_keys_refs: Vec<&str> = file3_keys.iter().map(|s| s.as_str()).collect();
	let file3_entries = create_entries_with_keys(&file3_keys_refs, 300);
	let table3 = env.create_test_table(3, file3_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[1]).insert(table3);

	// Create manifest
	let manifest_path = env.options.path.join("test_manifest");
	let manifest = LevelManifest {
		path: manifest_path,
		levels,
		hidden_set: HashSet::new(),
		next_table_id: Arc::new(AtomicU64::new(1000)),
		manifest_format_version: crate::levels::MANIFEST_FORMAT_VERSION_V1,
		snapshots: Vec::new(),
		log_number: 0,
		last_sequence: 0,
	};
	write_manifest_to_disk(&manifest).unwrap();
	let manifest = Arc::new(RwLock::new(manifest));

	// Create strategy with OldestSmallestSeqFirst priority
	let opts = create_options_with_compaction_settings(&env.options, 4, 2.0);
	let strategy = create_strategy_with_priority(&opts, CompactionPriority::OldestSmallestSeqFirst);

	let levels_guard = manifest.read().unwrap();
	let source_level = &levels_guard.levels.get_levels()[1];
	let next_level = &levels_guard.levels.get_levels()[2]; // Empty L2

	// File 1 should be selected (oldest sequence), and should expand to include File 2
	let selected = strategy.select_tables_for_compaction(source_level, next_level, 1).unwrap();

	// Verify File 1 and File 2 are both included (shared "foo" boundary)
	assert!(selected.contains(&1), "File 1 should be included (initially selected as oldest)");
	assert!(
		selected.contains(&2),
		"File 2 should be included (shares boundary key 'foo' with file 1)"
	);
	// Verify File 3 is NOT included (no shared boundary)
	assert!(!selected.contains(&3), "File 3 should NOT be included (no shared boundary)");
}

#[test]
fn test_clean_cut_integration_no_expansion() {
	let env = TestEnv::new();

	// Create L1 with files that have no shared boundaries, File 1 is largest
	// File 1: keys "a" to "b" (largest)
	// File 2: keys "d" to "e"
	// File 3: keys "g" to "h"

	let mut levels = Levels::new(3, 10);

	// File 1: a, b (make it largest by adding more keys BEFORE "b")
	let mut file1_keys = vec!["a".to_string()];
	for i in 0..10 {
		file1_keys.push(format!("a{}", i)); // "a0", "a1", ..., "a9" are all < "b"
	}
	file1_keys.push("b".to_string()); // "b" is still the largest key
	let file1_keys_refs: Vec<&str> = file1_keys.iter().map(|s| s.as_str()).collect();
	let file1_entries = create_entries_with_keys(&file1_keys_refs, 100);
	let table1 = env.create_test_table(1, file1_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[1]).insert(table1);

	// File 2: d, e
	let file2_entries = create_entries_with_keys(&["d", "e"], 200);
	let table2 = env.create_test_table(2, file2_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[1]).insert(table2);

	// File 3: g, h
	let file3_entries = create_entries_with_keys(&["g", "h"], 300);
	let table3 = env.create_test_table(3, file3_entries).unwrap();
	Arc::make_mut(&mut levels.get_levels_mut()[1]).insert(table3);

	// Create manifest
	let manifest_path = env.options.path.join("test_manifest");
	let manifest = LevelManifest {
		path: manifest_path,
		levels,
		hidden_set: HashSet::new(),
		next_table_id: Arc::new(AtomicU64::new(1000)),
		manifest_format_version: crate::levels::MANIFEST_FORMAT_VERSION_V1,
		snapshots: Vec::new(),
		log_number: 0,
		last_sequence: 0,
	};
	write_manifest_to_disk(&manifest).unwrap();
	let manifest = Arc::new(RwLock::new(manifest));

	// Create strategy with default priority (ByCompensatedSize)
	let opts = create_options_with_compaction_settings(&env.options, 4, 2.0);
	let strategy = Strategy::from_options(opts);

	let levels_guard = manifest.read().unwrap();
	let source_level = &levels_guard.levels.get_levels()[1];
	let next_level = &levels_guard.levels.get_levels()[2]; // Empty L2

	// File 1 should be selected (largest), but should NOT expand (no shared boundaries)
	let selected = strategy.select_tables_for_compaction(source_level, next_level, 1).unwrap();

	// Verify only File 1 is selected (no expansion needed)
	assert_eq!(
		selected.len(),
		1,
		"Only one file should be selected when there are no shared boundaries"
	);
	assert!(selected.contains(&1), "File 1 should be the only selected file");
}