kividb 1.2.2

A fast RedisĀ®-compatible in-memory store written in 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
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
pub mod filter;
pub mod postings;
pub mod score;
pub mod simd;
pub mod slots;
pub mod stem;
pub mod text;
use crate::store::ValueRef;
use ahash::AHashMap;
use parking_lot::RwLock;
use rand::Rng;
use std::sync::Arc;
use std::sync::atomic::Ordering::{Acquire, Relaxed, Release};
use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, AtomicUsize};

/// True while snapshot startup is rebuilding vector / FT live indexes from keys.
/// [`crate::commands::handler::execute_command`] returns `-LOADING` for commands not on the
/// same allowlist as replica bulk-sync (PING, INFO, SHUTDOWN, etc.).
pub static SNAPSHOT_INDEX_REBUILD: AtomicBool = AtomicBool::new(false);

/// Set for the duration of an AOF replay, so [`maybe_index`] and
/// [`maybe_unindex`] do nothing and the indexes are built by a single
/// [`backfill_from_db_progress`] pass once the dataset is whole.
///
/// Replaying an indexed dataset otherwise pays a full HNSW insertion per
/// record — 1.18M of them for glove-25, serially, before the listener is even
/// bound, which is the difference between a ~3 minute start and a ~3 second
/// one. It is also wasted work: a vector indexed by one record and overwritten
/// or deleted by a later one was inserted into the graph for nothing.
///
/// Safe because no write command reads the index to decide its result, so the
/// state the replay produces is identical either way. Reads cannot observe the
/// gap — the replay finishes before any client is accepted.
pub static DEFER_INDEXING: AtomicBool = AtomicBool::new(false);

/// One relaxed load on the write path, in the same shape as `MONITOR_COUNT`.
#[inline]
fn indexing_deferred() -> bool {
    DEFER_INDEXING.load(Relaxed)
}

#[derive(Debug, Clone, PartialEq)]
pub enum DistanceMetric {
    L2,
    Cosine,
    InnerProduct,
}

impl DistanceMetric {
    pub fn metric_from_str(s: &str) -> Option<Self> {
        match s.to_ascii_uppercase().as_str() {
            "L2" => Some(Self::L2),
            "COSINE" => Some(Self::Cosine),
            "IP" | "INNERPRODUCT" => Some(Self::InnerProduct),
            _ => None,
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum VectorAlgo {
    Flat,
    Hnsw {
        m: usize,
        ef_construction: usize,
        ef_runtime: usize,
    },
}

/// The element type of a vector field's blobs, `TYPE` in `FT.CREATE`.
///
/// Only the blobs are in this type: a hash field's value and a query's
/// vector parameter. The index holds every vector as `f32`, so a FLOAT64
/// field costs what a FLOAT32 one does and FLOAT16 saves nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum VectorType {
    #[default]
    Float32,
    Float64,
    Float16,
    BFloat16,
}

impl VectorType {
    pub fn parse(s: &str) -> Option<Self> {
        match s.to_ascii_uppercase().as_str() {
            "FLOAT32" => Some(Self::Float32),
            "FLOAT64" => Some(Self::Float64),
            "FLOAT16" => Some(Self::Float16),
            "BFLOAT16" => Some(Self::BFloat16),
            _ => None,
        }
    }

    /// As `FT.CREATE` and `FT.INFO` spell it.
    pub fn name(self) -> &'static str {
        match self {
            Self::Float32 => "FLOAT32",
            Self::Float64 => "FLOAT64",
            Self::Float16 => "FLOAT16",
            Self::BFloat16 => "BFLOAT16",
        }
    }

    /// Bytes per element.
    pub fn size(self) -> usize {
        match self {
            Self::Float32 => 4,
            Self::Float64 => 8,
            Self::Float16 | Self::BFloat16 => 2,
        }
    }

    /// A blob of `dim` little-endian elements, as `f32`. `None` if it is not
    /// `dim` elements long.
    pub fn decode(self, blob: &[u8], dim: usize) -> Option<Vec<f32>> {
        if blob.len() != dim * self.size() {
            return None;
        }
        Some(match self {
            Self::Float32 => blob
                .as_chunks::<4>()
                .0
                .iter()
                .map(|b| f32::from_le_bytes(*b))
                .collect(),
            Self::Float64 => blob
                .as_chunks::<8>()
                .0
                .iter()
                .map(|b| f64::from_le_bytes(*b) as f32)
                .collect(),
            Self::Float16 => blob
                .as_chunks::<2>()
                .0
                .iter()
                .map(|b| crate::commands::json_fpha::f16_bits_to_f32(u16::from_le_bytes(*b)))
                .collect(),
            Self::BFloat16 => blob
                .as_chunks::<2>()
                .0
                .iter()
                .map(|b| f32::from_bits(u32::from(u16::from_le_bytes(*b)) << 16))
                .collect(),
        })
    }

    /// One element as this type's little-endian bytes: a JSON document's
    /// number array is turned into the blob a hash would hold.
    pub fn encode(self, value: f64, out: &mut Vec<u8>) {
        match self {
            Self::Float32 => out.extend_from_slice(&(value as f32).to_le_bytes()),
            Self::Float64 => out.extend_from_slice(&value.to_le_bytes()),
            Self::Float16 => out.extend_from_slice(
                &crate::commands::json_fpha::f32_to_f16_bits(value as f32).to_le_bytes(),
            ),
            Self::BFloat16 => {
                let rounded = crate::commands::json_fpha::f32_to_bf16(value as f32);
                out.extend_from_slice(&((rounded.to_bits() >> 16) as u16).to_le_bytes());
            }
        }
    }
}

#[derive(Debug, Clone)]
pub enum FieldType {
    Text,
    Tag,
    Numeric,
    Vector {
        algo: VectorAlgo,
        dim: usize,
        metric: DistanceMetric,
        data_type: VectorType,
    },
}

#[derive(Debug, Clone)]
pub struct SchemaField {
    /// Where the value is read from: the hash field's name.
    pub name: String,
    pub field_type: FieldType,
    pub options: FieldOptions,
}

impl SchemaField {
    /// The name queries, `RETURN` and `SORTBY` know the field by: the one
    /// given with `AS`, or else its own.
    pub fn attribute(&self) -> &str {
        self.options.alias.as_deref().unwrap_or(&self.name)
    }

    /// The type as `FT.CREATE` and `FT.INFO` spell it.
    pub fn type_name(&self) -> &'static str {
        match self.field_type {
            FieldType::Text => "TEXT",
            FieldType::Tag => "TAG",
            FieldType::Numeric => "NUMERIC",
            FieldType::Vector { .. } => "VECTOR",
        }
    }
}

/// What may follow a field's type in a schema.
#[derive(Debug, Clone, PartialEq)]
pub struct FieldOptions {
    /// `AS name`.
    pub alias: Option<String>,
    pub sortable: bool,
    /// `SORTABLE UNF`: sort on the value as stored, not a normalised form.
    pub unf: bool,
    /// Not searchable; still returned, and sortable if `SORTABLE`.
    pub noindex: bool,
    /// TEXT: how much a match in this field counts towards the score.
    pub weight: f64,
    pub nostem: bool,
    pub phonetic: Option<String>,
    /// TAG: what separates one tag from the next in a value.
    pub separator: char,
    /// TAG: `Abc` and `abc` are different tags.
    pub case_sensitive: bool,
    pub with_suffix_trie: bool,
    pub index_empty: bool,
    pub index_missing: bool,
}

impl Default for FieldOptions {
    fn default() -> Self {
        Self {
            alias: None,
            sortable: false,
            unf: false,
            noindex: false,
            weight: 1.0,
            nostem: false,
            phonetic: None,
            separator: ',',
            case_sensitive: false,
            with_suffix_trie: false,
            index_empty: false,
            index_missing: false,
        }
    }
}

impl FieldOptions {
    /// Read the options that follow a field's type, from `args[*i]` on, and
    /// stop at the first word that is not one — the next field's name.
    ///
    /// An option is recognised only on a type it applies to. `WEIGHT` after a
    /// TAG field is therefore not consumed, and is reported by the caller as
    /// what it is: an option in the wrong place.
    pub fn consume(
        &mut self,
        field: &str,
        field_type: &FieldType,
        args: &[&[u8]],
        i: &mut usize,
    ) -> Result<(), String> {
        let text = matches!(field_type, FieldType::Text);
        let tag = matches!(field_type, FieldType::Tag);
        let vector = matches!(field_type, FieldType::Vector { .. });
        while let Some(word) = args.get(*i).and_then(|a| std::str::from_utf8(a).ok()) {
            let mut value = |what: &str| -> Result<String, String> {
                let value = args
                    .get(*i + 1)
                    .and_then(|a| std::str::from_utf8(a).ok())
                    .ok_or_else(|| format!("{what} of field `{field}` needs a value"))?;
                *i += 1;
                Ok(value.to_string())
            };
            match word.to_ascii_uppercase().as_str() {
                "SORTABLE" if !vector => {
                    self.sortable = true;
                    if args
                        .get(*i + 1)
                        .is_some_and(|a| a.eq_ignore_ascii_case(b"UNF"))
                    {
                        self.unf = true;
                        *i += 1;
                    }
                }
                "NOINDEX" if !vector => self.noindex = true,
                "INDEXMISSING" => self.index_missing = true,
                "INDEXEMPTY" if text || tag => self.index_empty = true,
                "WITHSUFFIXTRIE" if text || tag => self.with_suffix_trie = true,
                "WEIGHT" if text => {
                    let raw = value("WEIGHT")?;
                    self.weight = raw
                        .parse::<f64>()
                        .ok()
                        .filter(|w| w.is_finite() && *w >= 0.0)
                        .ok_or_else(|| format!("Invalid WEIGHT `{raw}` for field `{field}`"))?;
                }
                "NOSTEM" if text => self.nostem = true,
                "PHONETIC" if text => self.phonetic = Some(value("PHONETIC")?),
                "SEPARATOR" if tag => {
                    let raw = value("SEPARATOR")?;
                    let mut chars = raw.chars();
                    match (chars.next(), chars.next()) {
                        (Some(c), None) => self.separator = c,
                        _ => {
                            return Err(format!(
                                "Tag separator of field `{field}` must be a single character"
                            ));
                        }
                    }
                }
                "CASESENSITIVE" if tag => self.case_sensitive = true,
                _ => break,
            }
            *i += 1;
        }
        Ok(())
    }

    /// The options as `FT.CREATE` would be given them — and as `FT.INFO`
    /// lists them, after the type.
    pub fn words(&self, field_type: &FieldType) -> Vec<String> {
        let mut out = Vec::new();
        match field_type {
            FieldType::Text => {
                out.push("WEIGHT".to_string());
                out.push(format_weight(self.weight));
                if self.nostem {
                    out.push("NOSTEM".into());
                }
                if let Some(matcher) = &self.phonetic {
                    out.push("PHONETIC".into());
                    out.push(matcher.clone());
                }
            }
            FieldType::Tag => {
                out.push("SEPARATOR".to_string());
                out.push(self.separator.to_string());
                if self.case_sensitive {
                    out.push("CASESENSITIVE".into());
                }
            }
            FieldType::Numeric | FieldType::Vector { .. } => {}
        }
        if self.with_suffix_trie {
            out.push("WITHSUFFIXTRIE".into());
        }
        if self.index_empty {
            out.push("INDEXEMPTY".into());
        }
        if self.index_missing {
            out.push("INDEXMISSING".into());
        }
        if self.sortable {
            out.push("SORTABLE".into());
            if self.unf {
                out.push("UNF".into());
            }
        }
        if self.noindex {
            out.push("NOINDEX".into());
        }
        out
    }
}

impl IndexDef {
    /// The `FT.CREATE` that makes this definition, for a node that lacks
    /// it: one added to a cluster after the index was created, say (see
    /// `cluster::search` and `cluster::migration`). Synonym groups are not
    /// part of it.
    pub fn create_args(&self) -> Vec<Vec<u8>> {
        let mut a: Vec<String> = vec![
            "FT.CREATE".into(),
            self.name.clone(),
            "ON".into(),
            match self.on {
                IndexOn::Hash => "HASH",
                IndexOn::Json => "JSON",
            }
            .into(),
        ];
        if !self.prefixes.is_empty() {
            a.push("PREFIX".into());
            a.push(self.prefixes.len().to_string());
            a.extend(self.prefixes.iter().cloned());
        }
        a.extend(self.language.words());
        if let Some(words) = &self.stopwords {
            a.push("STOPWORDS".into());
            a.push(words.len().to_string());
            a.extend(words.iter().cloned());
        }
        if self.default_score != 1.0 {
            a.push("SCORE".into());
            a.push(self.default_score.to_string());
        }
        if let Some(field) = &self.score_field {
            a.push("SCORE_FIELD".into());
            a.push(field.clone());
        }
        a.push("SCHEMA".into());
        for field in &self.fields {
            a.push(field.name.clone());
            if let Some(alias) = &field.options.alias {
                a.push("AS".into());
                a.push(alias.clone());
            }
            a.push(field.type_name().into());
            if let FieldType::Vector {
                algo,
                dim,
                metric,
                data_type,
            } = &field.field_type
            {
                let mut attrs: Vec<String> = vec![
                    "TYPE".into(),
                    data_type.name().into(),
                    "DIM".into(),
                    dim.to_string(),
                    "DISTANCE_METRIC".into(),
                    match metric {
                        DistanceMetric::L2 => "L2",
                        DistanceMetric::Cosine => "COSINE",
                        DistanceMetric::InnerProduct => "IP",
                    }
                    .into(),
                ];
                match algo {
                    VectorAlgo::Flat => a.push("FLAT".into()),
                    VectorAlgo::Hnsw {
                        m,
                        ef_construction,
                        ef_runtime,
                    } => {
                        a.push("HNSW".into());
                        attrs.extend([
                            "M".into(),
                            m.to_string(),
                            "EF_CONSTRUCTION".into(),
                            ef_construction.to_string(),
                            "EF_RUNTIME".into(),
                            ef_runtime.to_string(),
                        ]);
                    }
                }
                a.push(attrs.len().to_string());
                a.extend(attrs);
            }
            a.extend(field.options.words(&field.field_type));
        }
        a.into_iter().map(String::into_bytes).collect()
    }
}

fn format_weight(weight: f64) -> String {
    if weight.fract() == 0.0 {
        format!("{}", weight as i64)
    } else {
        weight.to_string()
    }
}

/// Words that belong after a field's type, and the types they belong to.
/// Used to explain a schema that has one in the wrong place.
pub fn field_option_owner(word: &str) -> Option<&'static str> {
    match word.to_ascii_uppercase().as_str() {
        "WEIGHT" | "NOSTEM" | "PHONETIC" => Some("TEXT"),
        "SEPARATOR" | "CASESENSITIVE" => Some("TAG"),
        "SORTABLE" | "UNF" | "NOINDEX" => Some("TEXT, TAG and NUMERIC"),
        "WITHSUFFIXTRIE" | "INDEXEMPTY" => Some("TEXT and TAG"),
        "INDEXMISSING" => Some("every type of"),
        _ => None,
    }
}

#[derive(Debug, Clone)]
pub enum IndexOn {
    Hash,
    Json,
}

/// Holds the live index data — either FLAT (brute force) or HNSW (graph).
#[derive(Debug, Clone)]
pub enum LiveIndex {
    Flat(Arc<VectorIndex>),
    Hnsw(Arc<HnswIndex>),
}

/// A synonym group: a set of terms that are treated as equivalent during search.
/// Key = group ID (string), Value = set of synonym terms.
pub type SynonymGroups = AHashMap<String, Vec<String>>;

/// What separates one word from the next, in a document and in a query —
/// Redis's default separators. Everything else, `_` included, is part of a
/// word.
#[inline]
pub(crate) fn is_token_separator(c: char) -> bool {
    c.is_whitespace()
        || matches!(
            c,
            ',' | '.'
                | '/'
                | '('
                | ')'
                | '{'
                | '}'
                | '['
                | ']'
                | ':'
                | ';'
                | '\\'
                | '~'
                | '!'
                | '@'
                | '#'
                | '$'
                | '%'
                | '^'
                | '&'
                | '*'
                | '-'
                | '='
                | '+'
                | '|'
                | '\''
                | '`'
                | '"'
                | '<'
                | '>'
                | '?'
        )
}

/// The words of a text: split at separators, lower-cased.
///
/// One place that defines "what is a word" — used for indexing, for matching
/// and for spell-checking, so that they always agree.
///
/// It used to split at whitespace only and then drop the punctuation, which
/// made `wi-fi` the word `wifi` and `red,green` the word `redgreen`: a search
/// for `green` found nothing. Punctuation separates words, as in Redis.
///
/// A backslash makes the character after it part of the word, separator or
/// not, and is itself dropped: `users\.u_1` is the one word `users.u_1`, in a
/// document as in a query, as in Redis. LangGraph's store keeps namespaces
/// apart this way; splitting there made a namespace match the ones under it.
pub fn tokenize_for_vocab(text: &str) -> impl Iterator<Item = String> + '_ {
    // Most text has no backslash: it is split as it always was, at the cost
    // tokenizing had before escapes were read. Looking for one is a byte
    // search over the text.
    if text.contains('\\') {
        Words::Escaped(text)
    } else {
        Words::Plain(
            text.split(is_token_separator)
                .filter(|word| !word.is_empty())
                .map(str::to_lowercase),
        )
    }
}

/// The words of a text, for [`tokenize_for_vocab`]. `Plain` is the split a
/// text without a backslash has always had; it keeps the separator test a
/// function the compiler inlines, as a `fn` pointer would not.
enum Words<'a, P> {
    Plain(P),
    /// What is left of a text that has a backslash in it.
    Escaped(&'a str),
}

impl<P: Iterator<Item = String>> Iterator for Words<'_, P> {
    type Item = String;

    fn next(&mut self) -> Option<String> {
        match self {
            Words::Plain(words) => words.next(),
            Words::Escaped(rest) => loop {
                // A word starts at the first character that is not a
                // separator, or at a backslash, which escapes what follows.
                let start = rest.find(|c| c == '\\' || !is_token_separator(c))?;
                let text = &rest[start..];
                let mut word = String::new();
                let mut chars = text.char_indices();
                let mut end = text.len();
                while let Some((i, c)) = chars.next() {
                    if c == '\\' {
                        word.extend(chars.next().map(|(_, escaped)| escaped));
                    } else if is_token_separator(c) {
                        end = i;
                        break;
                    } else {
                        word.push(c);
                    }
                }
                *rest = &text[end..];
                if !word.is_empty() {
                    return Some(word.to_lowercase());
                }
            },
        }
    }
}

/// Returns true if the token is a common English stopword that should not
/// be indexed or matched. Stopwords are never added to the inverted index,
/// vocab, or BM25 stats — querying for them returns no results rather than
/// matching everything.
///
/// List matches Redis's default English stopword set.
pub fn is_stopword(tok: &str) -> bool {
    matches!(
        tok,
        "a" | "an"
            | "and"
            | "are"
            | "as"
            | "at"
            | "be"
            | "but"
            | "by"
            | "for"
            | "if"
            | "in"
            | "into"
            | "is"
            | "it"
            | "no"
            | "not"
            | "of"
            | "on"
            | "or"
            | "such"
            | "that"
            | "the"
            | "their"
            | "then"
            | "there"
            | "these"
            | "they"
            | "this"
            | "to"
            | "was"
            | "will"
            | "with"
    )
}

#[derive(Debug, Clone)]
pub struct IndexDef {
    pub name: String,
    /// Which logical database (0-based) this index applies to.
    pub db_index: usize,
    pub on: IndexOn,
    pub prefixes: Vec<String>,
    pub fields: Vec<SchemaField>,
    /// Live index data for each VECTOR field, keyed by field name.
    /// A schema can declare multiple VECTOR fields; each one gets its own
    /// FLAT or HNSW structure here. Non-vector fields (TEXT/TAG/NUMERIC)
    /// are evaluated at scan time and don't appear in this map.
    pub indexes: AHashMap<String, LiveIndex>,
    /// `STOPWORDS n word…` of `FT.CREATE`: the words this index does not
    /// index. `None` is the default list; an empty list is "none at all".
    pub stopwords: Option<Arc<Vec<String>>>,
    pub synonyms: SynonymGroups,
    /// `LANGUAGE` and `LANGUAGE_FIELD`: what TEXT fields are stemmed in.
    pub language: stem::LanguageOptions,
    /// `SCORE`: the score a document has when `SCORE_FIELD` names no field
    /// it holds, and when the index names no field at all.
    pub default_score: f64,
    /// `SCORE_FIELD`: the document field whose number multiplies its
    /// relevance. A document without it scores [`IndexDef::default_score`].
    pub score_field: Option<String>,
    /// With `LANGUAGE_FIELD`: the documents whose words were stemmed in a
    /// language other than the index's, and that language. For a write of
    /// some fields of a hash that leaves its language field as it was.
    pub doc_languages: Arc<RwLock<AHashMap<String, stem::Language>>>,
    /// The TEXT fields' inverted and forward index, and their BM25
    /// statistics, in terms of the document ids of [`IndexDef::postings`]
    /// (see [`text`]). Every document of an index with a TEXT field has an
    /// id, whether or not the index has a TAG or NUMERIC field.
    pub text: Arc<RwLock<text::TextIndex>>,
    /// The TAG and NUMERIC postings, and the document ids they are kept in
    /// (see [`postings`]). For an index with no TAG or NUMERIC field, only
    /// the ids, and only if it has a TEXT field.
    pub postings: postings::SharedPostings,
}

impl IndexDef {
    /// The indexed TEXT fields, in the schema's order: a field's place among
    /// them is its number in [`IndexDef::text`].
    pub fn text_fields(&self) -> impl Iterator<Item = &SchemaField> {
        self.fields
            .iter()
            .filter(|f| matches!(f.field_type, FieldType::Text) && !f.options.noindex)
    }

    /// The number of TEXT field `name` in [`IndexDef::text`]: `None` if it
    /// is not an indexed TEXT field.
    pub fn text_field(&self, name: &str) -> Option<u16> {
        self.text_fields()
            .position(|f| f.name == name)
            .map(|i| i as u16)
    }

    /// By TEXT field number: what an occurrence of a word there counts, its
    /// `WEIGHT`, truncated, and at least 1 (RediSearch's frequencies are
    /// whole numbers).
    pub fn text_units(&self) -> Vec<u32> {
        self.text_fields()
            .map(|f| (f.options.weight as u32).max(1))
            .collect()
    }

    /// Whether the index gives its documents ids: it has a TEXT, TAG or
    /// NUMERIC field.
    #[inline]
    pub fn gives_ids(&self) -> bool {
        self.fields.iter().any(|f| match f.field_type {
            FieldType::Tag | FieldType::Numeric => true,
            FieldType::Text => !f.options.noindex,
            _ => false,
        })
    }

    /// Make what the TEXT fields numbered in `changes` of document `doc` hold
    /// the words given, stopwords already left out, each followed by its stem
    /// if it has one (`stem::index_words`): the inverted index, the
    /// vocabulary and the BM25 statistics become what they would be had the
    /// document always held them.
    pub fn write_text(&self, doc: u32, changes: &[(u16, Vec<String>)]) {
        if changes.is_empty() {
            return;
        }
        let units = self.text_units();
        self.text.write().set(doc, changes, |f| {
            units.get(f as usize).copied().unwrap_or(1)
        });
    }

    /// The TEXT index's part of a write, run by the postings with the
    /// document's id, under their lock (see [`postings::WithId`]): what
    /// [`IndexDef::write_text`] does with `changes`, after the fields
    /// numbered in `restemmed` (with whether they stem) are stemmed again
    /// in `language` from the words they hold. `None` if there is nothing
    /// to do.
    fn text_write(
        &self,
        mut changes: Vec<(u16, Vec<String>)>,
        restemmed: Vec<(u16, bool)>,
        language: stem::Language,
    ) -> Option<postings::WithId> {
        if changes.is_empty() && restemmed.is_empty() {
            return None;
        }
        let text = Arc::clone(&self.text);
        let units = self.text_units();
        Some(Box::new(move |doc| {
            let mut text = text.write();
            for (number, stems) in restemmed {
                if let Some(tokens) = restem(&text, doc, number, stems, language) {
                    changes.push((number, tokens));
                }
            }
            if !changes.is_empty() {
                text.set(doc, &changes, |f| {
                    units.get(f as usize).copied().unwrap_or(1)
                });
            }
        }))
    }

    /// The TEXT index's part of a removal: take out everything the
    /// document's TEXT fields put in, by the postings, under their lock,
    /// before its id is given back (see [`postings::WithId`]).
    fn text_removal(&self) -> Option<postings::WithId> {
        self.text_fields().next()?;
        let text = Arc::clone(&self.text);
        let units = self.text_units();
        Some(Box::new(move |doc| {
            text.write()
                .remove(doc, |f| units.get(f as usize).copied().unwrap_or(1));
        }))
    }

    /// The document at `key` is gone: forget the language it was in.
    fn forget_language(&self, key: &str) {
        if self.language.field.is_some() {
            self.doc_languages.write().remove(key);
        }
    }

    /// The language the words of the document at `key` are stemmed in, for a
    /// write of `fields` (all of the document's if `whole`): the one its
    /// language field names, or for a write that leaves that field as it
    /// was, the one it had. Kept for the next write.
    fn doc_language(
        &self,
        key: &str,
        fields: &[(String, Arc<[u8]>)],
        whole: bool,
    ) -> stem::Language {
        let Some(name) = &self.language.field else {
            return self.language.language;
        };
        let given = fields.iter().find(|(n, _)| n == name).map(|(_, v)| &**v);
        if given.is_none() && !whole {
            return self
                .doc_languages
                .read()
                .get(key)
                .copied()
                .unwrap_or(self.language.language);
        }
        let language = self.language.of(given);
        let mut languages = self.doc_languages.write();
        if language == self.language.language {
            languages.remove(key);
        } else {
            languages.insert(key.to_string(), language);
        }
        language
    }

    /// The language of the document at `key`, whose fields are all of `doc`.
    fn language_of(&self, key: &str, doc: &Fields) -> stem::Language {
        let Some(name) = &self.language.field else {
            return self.language.language;
        };
        let language = self.language.of(doc.get(name.as_str()).map(|v| &**v));
        let mut languages = self.doc_languages.write();
        if language == self.language.language {
            languages.remove(key);
        } else {
            languages.insert(key.to_string(), language);
        }
        language
    }

    /// Whether the index has a TAG or a NUMERIC field: whether it keeps
    /// postings (see [`postings`]).
    #[inline]
    pub fn keeps_postings(&self) -> bool {
        self.fields
            .iter()
            .any(|f| matches!(f.field_type, FieldType::Tag | FieldType::Numeric))
    }

    /// Whether `key` is under one of the index's prefixes.
    #[inline]
    pub fn covers(&self, key: &[u8]) -> bool {
        self.prefixes.is_empty() || self.prefixes.iter().any(|p| key.starts_with(p.as_bytes()))
    }

    /// Whether `token` is one of this index's stopwords.
    pub fn is_stopword(&self, token: &str) -> bool {
        match &self.stopwords {
            Some(words) => words.iter().any(|w| w == token),
            None => is_stopword(token),
        }
    }

    /// The field queries know as `attribute` — by the name given with `AS`,
    /// or its own if it has none.
    pub fn field_by_attribute(&self, attribute: &str) -> Option<&SchemaField> {
        self.fields.iter().find(|f| f.attribute() == attribute)
    }

    /// All VECTOR fields declared in the schema, in declaration order.
    /// Use this when you need to iterate over every vector field — for
    /// indexing, unindexing, info dumps, and so on.
    pub fn vector_fields(&self) -> impl Iterator<Item = &SchemaField> {
        self.fields
            .iter()
            .filter(|f| matches!(f.field_type, FieldType::Vector { .. }))
    }

    /// Look up a specific VECTOR field by name. Returns None if there's no
    /// such field, or if the field exists but isn't a VECTOR type.
    pub fn vector_field_by_name(&self, name: &str) -> Option<&SchemaField> {
        self.fields
            .iter()
            .find(|f| f.name == name && matches!(f.field_type, FieldType::Vector { .. }))
    }

    /// Deprecated. Returns the first VECTOR field. Kept temporarily for
    /// callsites that haven't been ported to multi-vector. New code should
    /// use `vector_fields()` or `vector_field_by_name()`.
    #[deprecated(note = "use vector_fields() or vector_field_by_name() instead")]
    pub fn vector_field(&self) -> Option<&SchemaField> {
        self.vector_fields().next()
    }
}
/// The search indexes of a store, and their aliases, by name.
pub type IndexStore = Arc<Indexes>;

/// [`IndexStore`]: the indexes behind their lock, and whether there may be
/// any, which a write can ask without the lock ([`Indexes::none`]).
///
/// Every `HSET` used to ask the lock whether it had an index to bring up to
/// date, twice (`handler::atomic_under_its_shard`, [`maybe_index`]). A
/// shared lock taken is a write to the lock's cache line, and every worker
/// took the one lock, so that line went from core to core with each write
/// of a hash: `HSET` at pipeline 16 lost 10% of its rate on 14 workers, as
/// much or as little as where the allocator put the lock decided (#544).
/// With no index, as nearly always, a write now loads a line that nothing
/// writes to.
///
/// [`Indexes::write`] makes [`Indexes::none`] false while it holds the
/// lock, and true again before it lets go if the map is empty then. A
/// write that finds it true is ordered before the holder of the lock as
/// one that took the shared lock before it would be: what the holder does
/// to the keyspace after taking the lock (a new index's backfill, under
/// the shards' locks) sees that write's key.
///
/// That orders the write's question before the holder, not the write. A
/// hash write that finds no index takes its key's order shared, beside the
/// other writes of the key (`handler::atomic_under_its_shard`), and brings
/// the indexes up to date after it has let go of the shard: one that asked
/// before an `FT.CREATE` added its index, and wrote after the walk had read
/// the key, indexed its own fields beside another such write, and the index
/// could keep the earlier one's (#570). So the first index is added with
/// [`Indexes::write_to_add`], which makes `none` false and waits out every
/// write that holds a key's order before it adds anything; and a hash write
/// that took its key's order shared asks again once it holds it
/// (`handler::ask_again_for_an_index`), and takes it whole if the answer has
/// changed.
#[derive(Default)]
pub struct Indexes {
    /// Whether the map may have anything in it. On a cache line of its own.
    any: OwnLine,
    map: RwLock<AHashMap<String, IndexDef>>,
    /// How many [`Indexes::write_to_add`] are waiting out the writes that
    /// found no index: `any` stays true while there are any. Changed, and
    /// read, with `map` held to change.
    raising: AtomicUsize,
}

#[derive(Default)]
#[repr(align(128))]
struct OwnLine(AtomicBool);

impl Indexes {
    /// The indexes, shared.
    #[inline]
    pub fn read(&self) -> parking_lot::RwLockReadGuard<'_, AHashMap<String, IndexDef>> {
        self.map.read()
    }

    /// The indexes, to change.
    pub fn write(&self) -> IndexesWriteGuard<'_> {
        let map = self.map.write();
        self.any.0.store(true, std::sync::atomic::Ordering::SeqCst);
        IndexesWriteGuard {
            map,
            any: &self.any.0,
            raising: &self.raising,
        }
    }

    /// The indexes, to add one to: [`Indexes::write`], and, when there is
    /// none, once every write that may have found none has finished (#570).
    ///
    /// `none` is made false first, and stays false; then each key's write
    /// order is taken whole and let go of (`write_order::barrier`), with the
    /// lock let go of meanwhile, since a write that holds an order may be
    /// waiting to read the indexes. A write that held an order shared then
    /// has finished, before anything was added: it falls before the new
    /// index, whose walk finds what it wrote. One that takes an order after
    /// the barrier has passed it finds `none` false, and takes it whole.
    ///
    /// Not with the command gate held alone (a transaction, or a script run
    /// alone): no other command runs then, and every one that runs after
    /// asks after it.
    ///
    /// Refused ([`FIRST_INDEX_BESIDE_OTHERS`]) in a script that runs beside
    /// others (`tag_locks`): other commands run meanwhile, and the barrier
    /// cannot, since the script holds its keys' order. With an index there
    /// already, nothing has to be waited out, and it is not refused.
    pub fn write_to_add(&self) -> Result<IndexesWriteGuard<'_>, &'static str> {
        let mut map = self.map.write();
        let first = map.is_empty() && !crate::command_gate::alone_on_this_thread();
        if first && (crate::tag_locks::beside_others() || crate::write_order::holds_stripes()) {
            return Err(FIRST_INDEX_BESIDE_OTHERS);
        }
        self.any.0.store(true, std::sync::atomic::Ordering::SeqCst);
        if first {
            self.raising.fetch_add(1, Relaxed);
            drop(map);
            crate::write_order::barrier();
            map = self.map.write();
            self.raising.fetch_sub(1, Relaxed);
        }
        Ok(IndexesWriteGuard {
            map,
            any: &self.any.0,
            raising: &self.raising,
        })
    }

    /// Whether there is no index, nor alias, without taking the lock: what
    /// a write asks before it looks for indexes to bring up to date.
    #[inline]
    pub fn none(&self) -> bool {
        !self.any.0.load(Acquire)
    }
}

/// [`Indexes::write_to_add`]'s refusal.
pub const FIRST_INDEX_BESIDE_OTHERS: &str = "ERR the first search index cannot be created \
     from a script that runs beside other scripts (lua-parallel-scripts): create it outside \
     the script";

/// [`Indexes::write`]'s hold of the lock.
pub struct IndexesWriteGuard<'a> {
    map: parking_lot::RwLockWriteGuard<'a, AHashMap<String, IndexDef>>,
    any: &'a AtomicBool,
    raising: &'a AtomicUsize,
}

impl std::ops::Deref for IndexesWriteGuard<'_> {
    type Target = AHashMap<String, IndexDef>;
    fn deref(&self) -> &Self::Target {
        &self.map
    }
}

impl std::ops::DerefMut for IndexesWriteGuard<'_> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.map
    }
}

impl Drop for IndexesWriteGuard<'_> {
    fn drop(&mut self) {
        // Before the lock is let go of (`map` is dropped after this).
        let any = !self.map.is_empty() || self.raising.load(Relaxed) > 0;
        self.any.store(any, Release);
    }
}

// ── Aliases ──────────────────────────────────────────────────────────────────
//
// An alias is an entry in the index store whose key is the alias and whose
// `name` is the index it stands for. The entry carries no schema and no index
// data — it is a name, resolved to the real index before a command runs (see
// `commands::vector::handle`).
//
// Aliases used to be full copies of the index definition. The copy shared the
// index's data through `Arc`s but was indexed into as if it were a second
// index, so every document counted twice toward the corpus statistics BM25
// scores with; it kept its own stale schema after `FT.ALTER`; it held the old
// graph alive after a compaction replaced it; it was listed by `FT._LIST` and
// survived `FT.DROPINDEX`; and it was written to snapshots as a second index
// of the same name, so the alias itself did not survive a restart.

/// Whether the entry stored under `key` is an alias rather than an index.
#[inline]
pub fn is_alias(key: &str, def: &IndexDef) -> bool {
    key != def.name
}

/// The index `name` refers to — itself, or the index it is an alias of.
pub fn resolve_index_name(store: &AHashMap<String, IndexDef>, name: &str) -> Option<String> {
    let def = store.get(name)?;
    // An alias whose index has since gone resolves to nothing.
    store.contains_key(&def.name).then(|| def.name.clone())
}

/// The index `name` is an alias of — `None` if it is an index itself, or
/// nothing at all. Allocation-free for the common case of a real index name.
pub fn alias_target(store: &AHashMap<String, IndexDef>, name: &str) -> Option<String> {
    let def = store.get(name)?;
    (is_alias(name, def) && store.contains_key(&def.name)).then(|| def.name.clone())
}

/// The aliases of `index`, sorted.
pub fn index_aliases(store: &AHashMap<String, IndexDef>, index: &str) -> Vec<String> {
    let mut aliases: Vec<String> = store
        .iter()
        .filter(|(key, def)| def.name == index && is_alias(key, def))
        .map(|(key, _)| key.clone())
        .collect();
    aliases.sort_unstable();
    aliases
}

/// Point `alias` at `index`, replacing whatever `alias` pointed at before.
/// The caller has checked that `index` exists and that `alias` is not itself
/// an index.
pub fn set_alias(store: &mut AHashMap<String, IndexDef>, alias: &str, index: &IndexDef) {
    let marker = IndexSchema {
        name: index.name.clone(),
        db_index: index.db_index,
        on: index.on.clone(),
        prefixes: Vec::new(),
        fields: Vec::new(),
        aliases: Vec::new(),
        stopwords: None,
        synonyms: Default::default(),
        language: Default::default(),
        default_score: 1.0,
        score_field: None,
    }
    .into_def();
    store.insert(alias.to_string(), marker);
}

/// Remove `index` and every alias of it. False if there is no such index.
pub fn remove_index(store: &mut AHashMap<String, IndexDef>, index: &str) -> bool {
    if store.get(index).is_none_or(|def| is_alias(index, def)) {
        return false;
    }
    store.retain(|_, def| def.name != index);
    true
}

pub fn new_index_store() -> IndexStore {
    Arc::new(Indexes::default())
}

/// Drop every search index, including HNSW/FLAT graphs, inverted indexes,
/// vocab, and aliases. Redis Stack does this on `FLUSHALL`; KiviDB stores
/// indexes outside the key HashMaps, so flushing keys alone leaves the
/// graphs resident (the 1.36 GB "empty after vector bench" case).
pub fn drop_all_indexes(index_store: &IndexStore) -> usize {
    let mut g = index_store.write();
    let n = g.len();
    *g = AHashMap::new();
    n
}

/// Drop indexes whose schema was created in `db_index`. Used by `FLUSHDB`.
pub fn drop_indexes_for_db(index_store: &IndexStore, db_index: usize) -> usize {
    let mut g = index_store.write();
    let before = g.len();
    g.retain(|_, def| def.db_index != db_index);
    before - g.len()
}

/// Lightweight, serialisation-friendly description of an index.
/// Contains only the schema (name, db, prefixes, field types) — no live graph
/// data. On snapshot load, convert to a full `IndexDef` via `into_def()` and
/// then call `backfill_from_db` to rebuild all live indexes from the key data.
#[derive(Debug, Clone)]
pub struct IndexSchema {
    pub name: String,
    pub db_index: usize,
    pub on: IndexOn,
    pub prefixes: Vec<String>,
    pub fields: Vec<SchemaField>,
    /// Aliases that resolve to this index (`FT.ALIASADD`).
    pub aliases: Vec<String>,
    /// See [`IndexDef::stopwords`].
    pub stopwords: Option<Vec<String>>,
    /// The index's synonym groups (`FT.SYNUPDATE`).
    pub synonyms: SynonymGroups,
    /// See [`IndexDef::language`].
    pub language: stem::LanguageOptions,
    /// See [`IndexDef::default_score`].
    pub default_score: f64,
    /// See [`IndexDef::score_field`].
    pub score_field: Option<String>,
}

impl IndexSchema {
    /// Build a full `IndexDef` with empty live indexes.
    /// Call `backfill_from_db` afterwards to populate them.
    pub fn into_def(self) -> IndexDef {
        let mut indexes: AHashMap<String, LiveIndex> = AHashMap::new();
        for f in &self.fields {
            if let FieldType::Vector { algo, .. } = &f.field_type {
                let live = match algo {
                    VectorAlgo::Flat => LiveIndex::Flat(Arc::new(VectorIndex::new())),
                    VectorAlgo::Hnsw {
                        m, ef_construction, ..
                    } => LiveIndex::Hnsw(Arc::new(HnswIndex::new(*m, *ef_construction))),
                };
                indexes.insert(f.name.clone(), live);
            }
        }
        let postings = postings::PostingsLock::new(postings::Postings::new(&self.fields));
        IndexDef {
            name: self.name,
            db_index: self.db_index,
            on: self.on,
            prefixes: self.prefixes,
            fields: self.fields,
            indexes,
            stopwords: self.stopwords.map(Arc::new),
            synonyms: self.synonyms,
            language: self.language,
            default_score: self.default_score,
            score_field: self.score_field,
            doc_languages: Arc::new(RwLock::new(AHashMap::new())),
            text: Arc::new(RwLock::new(text::TextIndex::new())),
            postings,
        }
    }
}

/// Index the words of some TEXT fields of every document the index covers —
/// for fields added to an index that already has documents (`FT.ALTER`).
/// Without it a query for a word in the new field finds only documents
/// written after the field was added.
pub fn backfill_text_fields(db: &crate::store::Db, def: &IndexDef, fields: &[String]) {
    walk_documents(db, def, |key, inner| {
        let language = def.language_of(key, &inner);
        let mut changes = Vec::new();
        for field in fields {
            let Some(text) = inner.get(field).and_then(|b| std::str::from_utf8(b).ok()) else {
                continue;
            };
            let Some(schema) = def.fields.iter().find(|f| &f.name == field) else {
                continue;
            };
            let Some(number) = def.text_field(field) else {
                continue;
            };
            changes.push((number, stem::index_words(def, schema, language, text)));
        }
        if !changes.is_empty() {
            let doc = def.postings.write().ensure(key);
            def.write_text(doc, &changes);
        }
    });
}

/// Start keeping postings for TAG and NUMERIC fields added to an index
/// (`FT.ALTER`), and fill them in from the documents it covers — all of its
/// postings, which is as cheap as only the new ones. With `scan` false
/// (`SKIPINITIALSCAN`), documents are only given ids, so that `NOT` and `*`
/// still know every document.
pub fn backfill_postings(db: &crate::store::Db, def: &IndexDef, added: &[SchemaField], scan: bool) {
    def.postings.write().add_fields(added);
    walk_documents(db, def, |key, inner| {
        let mut postings = def.postings.write();
        if scan {
            postings.write_doc(key, &inner);
        } else {
            postings.ensure(key);
        }
        def.postings.done(postings);
    });
}

/// Each document of `def` in `db` (its database), with its fields as the
/// index reads them: for a walk that indexes them (an index's backfill).
///
/// A key at a time, its write order held (`write_order::lock_key_for_walk`)
/// while its value is read, under its shard's lock, and `visit` indexes it:
/// a write to the key falls before the read, and the walk indexes what it
/// wrote, or after `visit`, and indexes the key itself (#275). The walk
/// used to read the whole shard under one read lock, and a write that
/// changes a hash or a JSON document in place, under the value's own lock,
/// does not wait for that: the walk could index what the key held before a
/// write that had already indexed what it holds now, and the index kept the
/// older value until the key's next write. (The order is the writes' own:
/// held, before #271, by the writes something reads the stream of.)
///
/// The keys of a shard are listed first, under its read lock, and each is
/// looked up again: a write to the key takes its order before its shard's
/// lock, so the walk cannot hold the shard while it waits for an order.
pub fn walk_documents(
    db: &crate::store::Db,
    def: &IndexDef,
    mut visit: impl FnMut(&str, DocFields<'_>),
) {
    for shard in db.iter() {
        // The keys themselves, for their order and for reading them; the
        // visit is given the document's name (`key_bytes::text_of`).
        let keys: Vec<crate::store::Key> = shard
            .read()
            .keys()
            .filter(|key| def.covers(key))
            .map(crate::store::Key::new)
            .collect();
        for key in keys {
            let _order = crate::write_order::lock_key_for_walk(def.db_index, &key);
            let map = shard.read();
            let Some(inner) = map
                .peek(&key)
                .and_then(|value| doc_fields(&def.on, &def.fields, value))
            else {
                continue;
            };
            crate::aof::failpoint("index-walk-read");
            visit(&crate::key_bytes::text_of(&key), inner);
        }
    }
}

/// Build every index again from the documents of its database: after
/// `SWAPDB`, which swaps what two databases hold under the indexes of each.
///
/// It replaces indexes that are there, and never adds the first one: a
/// write that asks finds an index all along, and takes its key's order
/// whole, so the walks below meet it key by key (`walk_documents`), and
/// nothing has to be waited out first ([`Indexes::write_to_add`]).
pub fn rebuild_indexes(index_store: &IndexStore, store: &crate::store::Store) {
    let rebuilt: Vec<IndexDef> = {
        let mut indexes = index_store.write();
        let names: Vec<String> = indexes
            .iter()
            .filter(|(name, def)| !is_alias(name, def))
            .map(|(name, _)| name.clone())
            .collect();
        let mut rebuilt = Vec::with_capacity(names.len());
        for name in names {
            let def = &indexes[&name];
            let fresh = IndexSchema {
                name: def.name.clone(),
                db_index: def.db_index,
                on: def.on.clone(),
                prefixes: def.prefixes.clone(),
                fields: def.fields.clone(),
                aliases: Vec::new(),
                stopwords: def.stopwords.as_deref().cloned(),
                synonyms: def.synonyms.clone(),
                language: def.language.clone(),
                default_score: def.default_score,
                score_field: def.score_field.clone(),
            }
            .into_def();
            indexes.insert(name, fresh.clone());
            rebuilt.push(fresh);
        }
        rebuilt
    };
    for def in rebuilt {
        if let Some(db) = store.get(def.db_index) {
            backfill_from_db(db, &def);
        }
    }
}

/// Backfill an `IndexDef` by scanning all matching keys in `db`.
/// This is the DB-access core shared by `backfill_index` (called on FT.CREATE)
/// and snapshot restoration (called on startup after keys are loaded).
pub fn backfill_from_db(db: &crate::store::Db, def: &IndexDef) {
    backfill_from_db_progress(db, def, false, false);
}

/// Like [`backfill_from_db`], but logs progress every [`BACKFILL_PROGRESS_KEYS`] matching keys
/// when `log_progress` is true (used after KDB snapshot restore).
///
/// When `keep_loaded_hnsw` is true (startup only), an HNSW graph that already holds vectors
/// (loaded from the snapshot's `KIVIHN1` section) is brought up to date with the keys rather
/// than built again (see `loaded` below). FLAT fields and TEXT / inverted-index work run as
/// for any backfill.
const BACKFILL_PROGRESS_KEYS: usize = 50_000;

pub fn backfill_from_db_progress(
    db: &crate::store::Db,
    def: &IndexDef,
    log_progress: bool,
    keep_loaded_hnsw: bool,
) {
    struct VecFieldWork {
        name: String,
        dim: usize,
        data_type: VectorType,
        metric: DistanceMetric,
        live: LiveIndex,
        /// The graph came with the snapshot. It is the graph as it was at
        /// the checkpoint, and the keys are as they are now: the writes
        /// replayed from the AOF since (whose indexing a replay defers) are
        /// in the keys only. So a vector the graph holds as the key holds it
        /// is left where it is, one it lacks or holds otherwise is inserted,
        /// and after the walk a node whose key no longer holds a vector is
        /// deleted. The graph used to be taken as it was, and the vectors
        /// written after the checkpoint were in no graph after a restart.
        ///
        /// Decided once, before the walk — asked per key, the first vector
        /// this walk inserts would make the graph look loaded, which left a
        /// graph rebuilt after an AOF replay holding one vector.
        loaded: bool,
    }
    let mut vec_work: Vec<VecFieldWork> = Vec::new();
    for vf in def.vector_fields() {
        let (dim, data_type, metric) = match &vf.field_type {
            FieldType::Vector {
                dim,
                data_type,
                metric,
                ..
            } => (*dim, *data_type, metric.clone()),
            _ => continue,
        };
        let live = match def.indexes.get(&vf.name) {
            Some(LiveIndex::Flat(a)) => LiveIndex::Flat(Arc::clone(a)),
            Some(LiveIndex::Hnsw(a)) => LiveIndex::Hnsw(Arc::clone(a)),
            None => continue,
        };
        let loaded = keep_loaded_hnsw
            && matches!(&live, LiveIndex::Hnsw(idx) if idx.live_count.load(Relaxed) > 0);
        vec_work.push(VecFieldWork {
            name: vf.name.clone(),
            dim,
            data_type,
            metric,
            live,
            loaded,
        });
    }

    let text_fields: Vec<&SchemaField> = def.text_fields().collect();
    let gives_ids = def.gives_ids();

    let mut keys_matched: usize = 0;

    // Ordered against writes to each key: see `walk_documents`. `FT.CREATE`
    // registers the index before this walk, so a write the walk has passed
    // indexes itself.
    walk_documents(db, def, |key, inner| {
        keys_matched += 1;
        if log_progress && keys_matched.is_multiple_of(BACKFILL_PROGRESS_KEYS) {
            log_verbose!(
                "Index backfill `{}`: processed ~{} matching keys…",
                def.name,
                keys_matched
            );
        }
        let doc = if gives_ids {
            let mut postings = def.postings.write();
            let doc = postings.write_doc(key, &inner);
            def.postings.done(postings);
            doc
        } else {
            postings::NO_DOC
        };

        for w in &vec_work {
            let raw_bytes = match inner.get(&w.name) {
                Some(b) => b.clone(),
                None => continue,
            };
            let Some(vec) = w.data_type.decode(&raw_bytes, w.dim) else {
                continue;
            };
            match &w.live {
                LiveIndex::Flat(idx) => idx.insert(key.to_string(), vec),
                LiveIndex::Hnsw(idx) => {
                    if w.loaded && holds_vector(idx, key, &vec, &w.metric) {
                        continue;
                    }
                    hnsw_insert_doc(idx, key.to_string(), vec, &w.metric, doc);
                }
            }
        }

        if text_fields.is_empty() {
            return;
        }
        let language = def.language_of(key, &inner);
        let mut changes = Vec::new();
        for (number, field) in text_fields.iter().enumerate() {
            if let Some(raw) = inner.get(&field.name)
                && let Ok(s) = std::str::from_utf8(raw)
            {
                changes.push((number as u16, stem::index_words(def, field, language, s)));
            }
        }
        def.write_text(doc, &changes);
    });

    // A loaded graph's nodes whose keys were deleted since the checkpoint, or
    // no longer hold a vector in the field. Each is looked at under its
    // key's shard lock, so that a write to the key made meanwhile (by a
    // master's stream) is either seen here or indexed after this deletes.
    for w in vec_work.iter().filter(|w| w.loaded) {
        let LiveIndex::Hnsw(idx) = &w.live else {
            continue;
        };
        let keys: Vec<String> = idx.key_to_id.read().keys().cloned().collect();
        for key in keys {
            // A document's key is kept as text (`key_bytes::text_of`).
            let raw = crate::key_bytes::bytes_of(&key);
            let map = crate::store::get_shard(db, &*raw).read();
            let holds = def.covers(&raw)
                && map
                    .peek(&*raw)
                    .and_then(|value| doc_fields(&def.on, &def.fields, value))
                    .and_then(|inner| {
                        inner
                            .get(&w.name)
                            .map(|raw| w.data_type.decode(raw, w.dim).is_some())
                    })
                    .unwrap_or(false);
            if !holds {
                unindex_vector(&w.live, &key);
            }
        }
    }
}

/// Whether `index` holds `vector` for `key`, as an insert of it would have
/// stored it (a cosine vector at unit length, by the same arithmetic).
fn holds_vector(index: &HnswIndex, key: &str, vector: &[f32], metric: &DistanceMetric) -> bool {
    let Some(id) = index.key_to_id.read().get(key).copied() else {
        return false;
    };
    let nodes = index.nodes.read();
    let Some(held) = index.vector_of(&nodes, id, key) else {
        return false;
    };
    if matches!(metric, DistanceMetric::Cosine) {
        held == normalize_to_unit(vector.to_vec()).as_slice()
    } else {
        held == vector
    }
}

// ── Compaction request channel ────────────────────────────────────────────────
//
// HNSW deletes leave tombstones. When the tombstone ratio crosses a threshold
// we want to rebuild the graph in the background. Rather than spawning a
// task per deletion (or doing the rebuild inline on the request path), we
// send a `CompactionRequest` on a sync_channel. A dedicated background OS
// thread drains the channel and rebuilds one index at a time.
//
// This is the same pattern used for AOF and replica client — a long-lived
// thread with an mpsc channel, no shared lock contention with the fast path.

#[derive(Debug, Clone)]
pub struct CompactionRequest {
    pub index_name: String,
    /// The specific VECTOR field whose graph to rebuild. Multi-vector
    /// indexes track tombstones per field, so compaction is per field too.
    pub field_name: String,
}
/// Sender side of the compaction channel. Cloned into every server worker
/// via Ctx; calls to `try_send` from the fast path never block.
pub type CompactionTx = std::sync::mpsc::SyncSender<CompactionRequest>;

/// Receiver side, owned by the dedicated compaction thread.
pub type CompactionRx = std::sync::mpsc::Receiver<CompactionRequest>;

/// Channel capacity. Small on purpose: if more than this number of compactions
/// are pending, the system is so overwhelmed that dropping additional requests
/// (the `try_send` Err path) is the right behaviour — they'll be re-queued
/// next time the threshold is crossed.
pub const COMPACTION_CHANNEL_CAPACITY: usize = 32;

pub fn new_compaction_channel() -> (CompactionTx, CompactionRx) {
    std::sync::mpsc::sync_channel(COMPACTION_CHANNEL_CAPACITY)
}

/// Tombstone ratio above which we trigger background compaction.
/// 0.20 = 20% of slots are ghosts — chosen as a balance between rebuild
/// frequency (CPU) and recall degradation. hnswlib uses 0.5 by default;
/// we're more aggressive because rebuild is a tokio task, not a stop-the-world.
pub const COMPACTION_THRESHOLD: f64 = 0.20;

/// Minimum tombstone count before we even consider compacting.
/// Without this, a 10-vector index with 3 deletes (30% ratio) would compact
/// constantly. We want to amortize over real workloads.
pub const COMPACTION_MIN_TOMBSTONES: usize = 64;

// ── Dictionary store ──────────────────────────────────────────────────────────
//
// Named global dictionaries for FT.DICTADD/DEL/DUMP and FT.SPELLCHECK.
// Key = dictionary name (String), Value = set of words in that dict.
// Global — not tied to any index, shared across all connections.
//
// We use AHashSet<String> per dict for O(1) membership checks during
// spell-check. The outer Arc<RwLock<...>> is the same pattern as IndexStore.

pub type DictStore = Arc<RwLock<AHashMap<String, ahash::AHashSet<String>>>>;

/// The dictionaries (`FT.DICTADD`) that go with each index store, so that
/// what saves and loads an index store's indexes can save and load its
/// dictionaries too. By the store's address; a server has one of each.
static DICT_STORES: RwLock<Vec<(usize, DictStore)>> = RwLock::new(Vec::new());

pub fn register_dict_store(index_store: &IndexStore, dicts: &DictStore) {
    let id = Arc::as_ptr(index_store) as usize;
    let mut stores = DICT_STORES.write();
    stores.retain(|(other, _)| *other != id);
    stores.push((id, Arc::clone(dicts)));
}

pub fn dict_store_for(index_store: &IndexStore) -> Option<DictStore> {
    let id = Arc::as_ptr(index_store) as usize;
    DICT_STORES
        .read()
        .iter()
        .find(|(other, _)| *other == id)
        .map(|(_, dicts)| Arc::clone(dicts))
}

/// Put back the dictionaries a snapshot held.
pub fn restore_dictionaries(index_store: &IndexStore, dictionaries: Vec<(String, Vec<String>)>) {
    if dictionaries.is_empty() {
        return;
    }
    let Some(dicts) = dict_store_for(index_store) else {
        return;
    };
    let mut dicts = dicts.write();
    for (name, terms) in dictionaries {
        dicts.entry(name).or_default().extend(terms);
    }
}

pub fn new_dict_store() -> DictStore {
    Arc::new(RwLock::new(AHashMap::new()))
}

/// Drop FT.DICTADD dictionaries. They are not Redis keys either.
pub fn drop_all_dicts(dict_store: &DictStore) {
    let mut g = dict_store.write();
    *g = AHashMap::new();
}

#[derive(Debug, Default)]
pub struct VectorIndex {
    pub vectors: RwLock<AHashMap<String, Vec<f32>>>,
}

impl VectorIndex {
    pub fn new() -> Self {
        Self {
            vectors: RwLock::new(AHashMap::new()),
        }
    }

    pub fn insert(&self, key: String, vec: Vec<f32>) {
        self.vectors.write().insert(key, vec);
    }
}

/// One node in the HNSW graph: its key, its document id, and its links
/// above layer 0. Its vector, its layer-0 links and whether it is live are
/// kept apart, in the graph's slots at its id ([`slots::Slots`]): those are
/// what a search reads for every node it meets, and there they are found
/// from the id alone.
///
/// A node's key and vector do not change while it is in the graph.
/// Deleting it, or filling its slot again for a new node, happens under the
/// graph's write lock. Its links have a lock of their own (`upper`), so that
/// inserts link themselves into different parts of the graph at the same
/// time, as hnswlib's do; layer 0's are read without it.
#[derive(Debug)]
pub struct HnswNode {
    pub key: String,
    /// The node's lock, held to change its links at any layer. It guards
    /// the lists above layer 0: `upper[l - 1]` is layer `l`'s.
    upper: parking_lot::Mutex<Vec<Vec<u32>>>,
    /// The top layer the node is in.
    level: usize,
    /// The id its document has in the index's postings, or
    /// [`postings::NO_DOC`] until it is looked up: a node made by a
    /// compaction, or read from a snapshot, learns it the first time a
    /// filtered search asks (see [`knn_hnsw_admitting`]). A document keeps
    /// its id for as long as it is in the index, and leaving the index
    /// deletes its node first, so a node never names another's.
    pub doc: AtomicU32,
}

impl HnswNode {
    fn new(key: String, level: usize, upper: Vec<Vec<u32>>, doc: u32) -> Self {
        Self {
            key,
            upper: parking_lot::Mutex::new(upper),
            level,
            doc: AtomicU32::new(doc),
        }
    }

    /// The node of a slot that holds none (deleted, or never filled).
    fn empty() -> Self {
        Self::new(String::new(), 0, Vec::new(), postings::NO_DOC)
    }

    /// How many layers the node is in.
    pub fn layer_count(&self) -> usize {
        self.level + 1
    }
}

/// The nodes of a graph, by id. Appending takes only a shared reference, and
/// ids never move, so an insert adds its node under the graph's read lock.
pub type HnswNodes = boxcar::Vec<HnswNode>;

/// The graph held for reading.
type NodesGuard<'a> = parking_lot::RwLockReadGuard<'a, HnswNodes>;

/// The HNSW graph index.
#[derive(Debug, Default)]
pub struct HnswIndex {
    /// Searches and inserts hold this for reading. Deleting a node, and
    /// filling a deleted node's slot again, take it for writing: a reader
    /// may still hold the id. Vectors are read only while it is held.
    pub nodes: RwLock<HnswNodes>,
    /// The vectors, layer-0 links and live flags, by id: made with the
    /// first vector, which sets the dimension.
    slots: std::sync::OnceLock<slots::Slots>,
    pub key_to_id: RwLock<AHashMap<String, usize>>,
    pub entry_point: RwLock<Option<usize>>, // node id of current entry point
    pub m: usize,                           // max neighbours per layer (except L0)
    pub m0: usize,                          // max neighbours at layer 0 (= 2*M); see `slack`
    pub ef_construction: usize,
    pub ml: f64, // level multiplier = 1/ln(M)

    /// Number of nodes currently in `nodes` that are live (not tombstoned).
    /// We maintain this explicitly so `FT.INFO` and the compaction threshold
    /// check don't have to walk the whole Vec.
    pub live_count: AtomicUsize,

    /// Number of deleted slots in `nodes` not filled again yet.
    /// Invariant: nodes.count() == live_count + tombstone_count (at rest).
    pub tombstone_count: AtomicUsize,

    /// True while a background compaction is already scheduled/running for
    /// this index. Prevents piling up duplicate compaction tasks on a
    /// write-heavy delete stream.
    pub compaction_in_progress: AtomicBool,

    /// Deleted slots, which the next inserts fill before the graph grows.
    free: parking_lot::Mutex<Vec<u32>>,

    /// Bumped each time a slot is filled again. A search holds ids across
    /// the moments it lets its read lock go; if this moved meanwhile, an id
    /// it kept may name another node now, and it measures its results again
    /// before it answers.
    reused: AtomicU64,
}

impl HnswIndex {
    pub fn new(m: usize, ef_construction: usize) -> Self {
        Self {
            nodes: RwLock::new(HnswNodes::new()),
            slots: std::sync::OnceLock::new(),
            key_to_id: RwLock::new(AHashMap::new()),
            entry_point: RwLock::new(None),
            m,
            m0: m * 2,
            ef_construction,
            ml: 1.0 / (m as f64).ln(),
            live_count: AtomicUsize::new(0),
            tombstone_count: AtomicUsize::new(0),
            compaction_in_progress: AtomicBool::new(false),
            free: parking_lot::Mutex::new(Vec::new()),
            reused: AtomicU64::new(0),
        }
    }

    /// Reconstruct an `HnswIndex` from a serialised snapshot: for each slot
    /// in order, `None` for a deleted one, or the node's key, vector (as the
    /// graph held it) and links by layer. The graph topology is preserved
    /// exactly — no backfill or re-insertion is needed. Deleted slots are
    /// filled again by the next inserts.
    pub fn restore(
        m: usize,
        ef_construction: usize,
        nodes: Vec<Option<RestoredNode>>,
        entry_point: Option<usize>,
    ) -> Self {
        let mut index = Self::new(m, ef_construction);
        let dim = nodes.iter().flatten().map(|n| n.vector.len()).next();
        let slots = dim.map(|dim| {
            index
                .slots
                .get_or_init(|| slots::Slots::new(dim, index.m0 + slack(index.m0)))
        });
        let count = nodes.len();
        {
            let graph = index.nodes.get_mut();
            let mut key_to_id = index.key_to_id.write();
            let mut free = index.free.lock();
            for (id, entry) in nodes.into_iter().enumerate() {
                if let Some(slots) = slots {
                    slots.reserve(id);
                }
                let node = match (entry, slots) {
                    (Some(n), Some(slots)) if n.vector.len() == slots.dim() => {
                        let mut layers = n.neighbors.into_iter().map(|list| {
                            list.into_iter()
                                .filter(|&nb| nb < count && nb != id)
                                .map(|nb| nb as u32)
                                .collect::<Vec<u32>>()
                        });
                        let layer0 = layers.next().unwrap_or_default();
                        let upper: Vec<Vec<u32>> = layers.collect();
                        // SAFETY: the graph is not shared yet.
                        unsafe { slots.write_vector(id, &n.vector) };
                        slots.write_links(id, layer0);
                        slots.flag(id).store(1, Release);
                        key_to_id.insert(n.key.clone(), id);
                        index.live_count.fetch_add(1, Relaxed);
                        HnswNode::new(n.key, upper.len(), upper, postings::NO_DOC)
                    }
                    _ => {
                        free.push(id as u32);
                        index.tombstone_count.fetch_add(1, Relaxed);
                        HnswNode::empty()
                    }
                };
                graph.push(node);
            }
            // The lowest ids are filled first.
            free.reverse();
        }
        let entry_point = match entry_point {
            Some(id) if index.is_live(id) => Some(id),
            _ => index.key_to_id.read().values().next().copied(),
        };
        *index.entry_point.write() = entry_point;
        index
    }

    /// The slots, once the first vector has made them.
    #[inline]
    pub fn slots(&self) -> Option<&slots::Slots> {
        self.slots.get()
    }

    /// Whether slot `id` holds a live node.
    #[inline]
    pub fn is_live(&self, id: usize) -> bool {
        self.slots.get().is_some_and(|s| s.has(id) && s.is_live(id))
    }

    /// The vector of node `id` if it is live. The caller holds the graph
    /// for reading (see [`HnswIndex::nodes`]).
    #[inline]
    pub fn vector(&self, id: usize) -> Option<&[f32]> {
        let s = self.slots.get()?;
        (s.has(id) && s.is_live(id)).then(|| s.vector(id))
    }

    /// The vector of node `id` if it is live and is `key`'s: a slot found
    /// through `key_to_id` may have been filled again for another key
    /// between the lookup and the read lock.
    #[inline]
    pub fn vector_of<'a>(
        &'a self,
        nodes: &'a HnswNodes,
        id: usize,
        key: &str,
    ) -> Option<&'a [f32]> {
        let v = self.vector(id)?;
        nodes.get(id).filter(|n| n.key == key).map(|_| v)
    }

    /// The links of node `id` at `layer`, appended to `out`. The caller
    /// holds the graph for reading.
    pub fn links(&self, nodes: &HnswNodes, id: usize, layer: usize, out: &mut Vec<u32>) {
        if layer == 0 {
            if let Some(s) = self.slots.get()
                && s.has(id)
            {
                s.read_links(id, out);
            }
        } else if let Some(node) = nodes.get(id)
            && let Some(list) = node.upper.lock().get(layer - 1)
        {
            out.extend_from_slice(list);
        }
    }

    /// Bytes the slots take: the vectors, layer-0 links and flags.
    pub fn slot_bytes(&self) -> usize {
        self.slots.get().map_or(0, |s| s.bytes())
    }

    /// Delete node `id`: its slot is emptied and kept for the next insert.
    /// Under the graph's write lock.
    fn delete(&self, nodes: &mut HnswNodes, id: usize) {
        if !self.is_live(id) {
            return;
        }
        let Some(s) = self.slots.get() else { return };
        s.flag(id).store(0, Release);
        s.write_links(id, std::iter::empty());
        if let Some(node) = nodes.get_mut(id) {
            *node = HnswNode::empty();
        }
        self.free.lock().push(id as u32);
        self.tombstone_count.fetch_add(1, Relaxed);
        self.live_count.fetch_sub(1, Relaxed);
    }

    /// Draw a random top layer for a new node.
    /// Uses the same formula as the original HNSW paper.
    pub fn random_level(&self) -> usize {
        let mut rng = rand::thread_rng();
        let f: f64 = rng.r#gen::<f64>();
        (-f.ln() * self.ml).floor() as usize
    }

    /// Ratio of tombstones to total slots. Returns 0.0 for empty indexes.
    /// Cheap: two atomic loads.
    pub fn tombstone_ratio(&self) -> f64 {
        use std::sync::atomic::Ordering::Relaxed;
        let tomb = self.tombstone_count.load(Relaxed);
        let live = self.live_count.load(Relaxed);
        let total = tomb + live;
        if total == 0 {
            0.0
        } else {
            tomb as f64 / total as f64
        }
    }
}

/// A node as a snapshot holds it: see [`HnswIndex::restore`].
pub struct RestoredNode {
    pub key: String,
    pub vector: Vec<f32>,
    /// Links by layer, layer 0 first.
    pub neighbors: Vec<Vec<usize>>,
}

/// Install index definitions carried by a KDB image (short write lock).
///
/// HNSW graphs saved in a snapshot are spliced straight into the `IndexDef`, so the
/// backfill that follows skips those fields entirely and the graph's precision
/// is the one that was saved rather than one rebuilt from scratch.
///
/// Lives here rather than beside the startup code because both readers of a KDB
/// image need it: `dump.kdb` at boot, and the AOF preamble during replay.
pub fn register_index_schemas(
    index_store: &IndexStore,
    schemas: &[IndexSchema],
    hnsw_graphs: &std::collections::HashMap<
        String,
        std::collections::HashMap<String, Arc<HnswIndex>>,
    >,
) {
    if schemas.is_empty() {
        return;
    }
    // Possibly the first index: see `Indexes::write_to_add` (#570). Never
    // from a script, so never refused.
    let mut idx = index_store
        .write_to_add()
        .unwrap_or_else(|_| index_store.write());
    for schema in schemas {
        let mut def = schema.clone().into_def();
        if let Some(field_graphs) = hnsw_graphs.get(&def.name) {
            for (field_name, hnsw_arc) in field_graphs {
                if let Some(live) = def.indexes.get_mut(field_name) {
                    *live = LiveIndex::Hnsw(Arc::clone(hnsw_arc));
                }
            }
        }
        for alias in &schema.aliases {
            if !idx.contains_key(alias) {
                set_alias(&mut idx, alias, &def);
            }
        }
        idx.insert(schema.name.clone(), def);
    }
}

// ── Documents ─────────────────────────────────────────────────────────────────
//
// An index is `ON HASH` or `ON JSON`. Everything that reads a document for
// an index — indexing it, matching a query against it, sorting by one of its
// fields — reads the same thing: the document's fields by the name the
// schema reads them under, as bytes. For a hash that is the hash. For a JSON
// document it is what the schema's paths select, written as a hash would
// hold it: a string as it is, a number as text, an array of strings as one
// value with the field's separator between them, an array of numbers under
// a VECTOR field as little-endian floats.

/// A document's fields, as its index reads them.
pub type Fields = crate::types::HashFields;

/// [`Fields`] of one document: a hash's own, locked for reading, or the
/// projection of a JSON document.
pub enum DocFields<'a> {
    Hash(parking_lot::RwLockReadGuard<'a, Fields>),
    /// A hash some of whose fields have a TTL.
    HashEx(parking_lot::RwLockReadGuard<'a, crate::types::HashInner>),
    Json {
        fields: Fields,
        doc: parking_lot::RwLockReadGuard<'a, serde_json::Value>,
    },
}

impl DocFields<'_> {
    /// The document itself, for a JSON document.
    pub fn json(&self) -> Option<&serde_json::Value> {
        match self {
            DocFields::Json { doc, .. } => Some(doc),
            _ => None,
        }
    }
}

impl std::ops::Deref for DocFields<'_> {
    type Target = Fields;
    fn deref(&self) -> &Fields {
        match self {
            DocFields::Hash(fields) => fields,
            DocFields::HashEx(hash) => &hash.fields,
            DocFields::Json { fields, .. } => fields,
        }
    }
}

/// Whether `value` is the kind of document an index `ON on` covers.
pub fn is_document(on: &IndexOn, value: crate::store::ValueRef<'_>) -> bool {
    matches!(
        (on, value),
        (IndexOn::Hash, crate::store::ValueRef::Hash(_))
            | (IndexOn::Hash, crate::store::ValueRef::HashEx(_))
            | (IndexOn::Json, crate::store::ValueRef::Json(_))
    )
}

/// The fields of `value` as an index `ON on` with this schema reads them.
/// `None` if it is not a document of that kind.
pub fn doc_fields<'a>(
    on: &IndexOn,
    schema: &[SchemaField],
    value: crate::store::ValueRef<'a>,
) -> Option<DocFields<'a>> {
    match (on, value) {
        (IndexOn::Hash, crate::store::ValueRef::Hash(hash)) => Some(DocFields::Hash(hash.read())),
        // A hash is no less a document for one of its fields having a TTL:
        // it used to drop out of every query the moment one was given one.
        (IndexOn::Hash, crate::store::ValueRef::HashEx(hash)) => {
            Some(DocFields::HashEx(hash.read()))
        }
        (IndexOn::Json, crate::store::ValueRef::Json(doc)) => {
            let doc = doc.read();
            Some(DocFields::Json {
                fields: project_json(schema, &doc),
                doc,
            })
        }
        _ => None,
    }
}

/// A schema field's JSONPath, parsed. Paths are few and documents many, so
/// each thread remembers the ones it has parsed.
fn json_path(path: &str) -> Option<Arc<crate::commands::json_path::Query>> {
    use crate::commands::json_path::Query;
    thread_local! {
        static PARSED: std::cell::RefCell<AHashMap<String, Option<Arc<Query>>>> =
            std::cell::RefCell::new(AHashMap::new());
    }
    PARSED.with(|parsed| {
        let mut parsed = parsed.borrow_mut();
        if let Some(query) = parsed.get(path) {
            return query.clone();
        }
        if parsed.len() >= 1024 {
            parsed.clear();
        }
        let query = Query::parse(path).ok().map(Arc::new);
        parsed.insert(path.to_string(), query.clone());
        query
    })
}

/// What a JSON document holds for each field of `schema`, keyed by the
/// field's path.
pub fn project_json(schema: &[SchemaField], doc: &serde_json::Value) -> Fields {
    let mut fields = Fields::with_capacity_and_hasher(schema.len(), Default::default());
    for field in schema {
        let Some(query) = json_path(&field.name) else {
            continue;
        };
        let values = query.values(doc);
        if let Some(bytes) = json_field_bytes(field, &values) {
            fields.insert(field.name.clone(), bytes);
        }
    }
    fields
}

/// The strings among `values`, with those inside arrays.
fn json_strings<'a>(
    values: &'a [std::borrow::Cow<'a, serde_json::Value>],
    booleans: bool,
) -> Vec<std::borrow::Cow<'a, str>> {
    use serde_json::Value;
    use std::borrow::Cow;
    let mut out = Vec::new();
    let mut take = |value: &'a Value| match value {
        Value::String(s) => out.push(Cow::Borrowed(s.as_str())),
        Value::Bool(b) if booleans => out.push(Cow::Borrowed(if *b { "true" } else { "false" })),
        _ => {}
    };
    for value in values {
        match value.as_ref() {
            Value::Array(items) => items.iter().for_each(&mut take),
            other => take(other),
        }
    }
    out
}

fn json_field_bytes(
    field: &SchemaField,
    values: &[std::borrow::Cow<'_, serde_json::Value>],
) -> Option<Arc<[u8]>> {
    use serde_json::Value;
    let first = values.first()?;
    match &field.field_type {
        FieldType::Text => {
            let words = json_strings(values, false);
            (!words.is_empty()).then(|| Arc::from(words.join(" ").into_bytes()))
        }
        FieldType::Tag => {
            let tags = json_strings(values, true);
            let separator = field.options.separator.to_string();
            (!tags.is_empty()).then(|| Arc::from(tags.join(&separator).into_bytes()))
        }
        FieldType::Numeric => {
            // The first number the path selects.
            let number = values.iter().find_map(|value| match value.as_ref() {
                Value::Number(n) => Some(n),
                Value::Array(items) => items.iter().find_map(|item| match item {
                    Value::Number(n) => Some(n),
                    _ => None,
                }),
                _ => None,
            })?;
            Some(Arc::from(number.to_string().into_bytes()))
        }
        // The blob a hash would hold, in the field's type, so that one
        // decoder reads both.
        FieldType::Vector { dim, data_type, .. } => {
            let Value::Array(items) = first.as_ref() else {
                return None;
            };
            if items.len() != *dim {
                return None;
            }
            let mut bytes = Vec::with_capacity(dim * data_type.size());
            for item in items {
                data_type.encode(item.as_f64()?, &mut bytes);
            }
            Some(Arc::from(bytes))
        }
    }
}

/// The number at `path` in a JSON document, as `SCORE_FIELD` reads it.
pub(crate) fn json_score_field_number(path: &str, doc: &serde_json::Value) -> Option<f64> {
    let query = json_path(path)?;
    let values = query.values(doc);
    let serde_json::Value::Number(number) = values.first()?.as_ref() else {
        return None;
    };
    number.as_f64()
}

/// What `FT.SEARCH … RETURN` shows for `path` of a JSON document: a string
/// as it is, anything else as JSON. `None` if the path selects nothing.
pub fn json_return_value(doc: &serde_json::Value, path: &str) -> Option<Vec<u8>> {
    let query = json_path(path)?;
    let values = query.values(doc);
    Some(match values.first()?.as_ref() {
        serde_json::Value::String(s) => s.clone().into_bytes(),
        other => serde_json::to_vec(other).ok()?,
    })
}

/// A JSON document was written: bring the `ON JSON` indexes of database
/// `db` that cover its key up to date with it.
pub fn maybe_index_json(
    index_store: &IndexStore,
    db: usize,
    key: &(impl AsRef<[u8]> + ?Sized),
    doc: &serde_json::Value,
) {
    let key = key.as_ref();
    if indexing_deferred() || index_store.none() {
        return;
    }
    let store = index_store.read();
    for (name, def) in store.iter() {
        if is_alias(name, def)
            || !matches!(def.on, IndexOn::Json)
            || def.db_index != db
            || !def.covers(key)
        {
            continue;
        }
        let fields: Vec<(String, Arc<[u8]>)> = project_json(&def.fields, doc).into_iter().collect();
        index_fields(def, &crate::key_bytes::text_of(key), &fields, Write::Whole);
    }
}

/// [`maybe_index_json`] for whatever is at `key` now, if it is a JSON
/// document and any index is `ON JSON`.
/// A hash was put under `key` whole (`RESTORE`, and so `MIGRATE` and slot
/// migration): the `ON HASH` indexes that cover it are brought up to date
/// with all of its fields. A restored hash used to be in no index, so that a
/// key moved to another node of a cluster could not be found there.
pub fn reindex_json_key(
    index_store: &IndexStore,
    store: &crate::store::Store,
    db: usize,
    key: &(impl AsRef<[u8]> + ?Sized),
) {
    let key = key.as_ref();
    if indexing_deferred()
        || index_store.none()
        || !index_store
            .read()
            .values()
            .any(|def| matches!(def.on, IndexOn::Json))
    {
        return;
    }
    let Some(db_ref) = store.get(db) else {
        return;
    };
    let value = crate::store::get_shard(db_ref, key)
        .read()
        .get(key)
        .map(ValueRef::to_value);
    if let Some(crate::types::KiviDBValue::Json(doc)) = value {
        maybe_index_json(index_store, db, key, &doc.read());
    }
}

/// Bring every index of database `db` that covers `key` up to date with what
/// is there now, whatever it is: for a command that changed a document
/// other than by writing fields of it (`HDEL`, `HINCRBY`, a field that
/// expired), or put one under a key (`RENAME`, `COPY`, `MOVE`, `RESTORE`).
/// A key that holds no document of an index any more leaves it.
///
/// With [`Write::Refresh`], a vector already in the graph is left there: the
/// write did not touch it, and inserting it again would cost an insertion.
pub fn reindex_key(
    index_store: &IndexStore,
    store: &crate::store::Store,
    db: usize,
    key: &(impl AsRef<[u8]> + ?Sized),
    how: Write,
) {
    let key = key.as_ref();
    if indexing_deferred() || index_store.none() {
        return;
    }
    let indexes = index_store.read();
    if !indexes
        .iter()
        .any(|(name, def)| !is_alias(name, def) && def.db_index == db && def.covers(key))
    {
        return;
    }
    let Some(db_ref) = store.get(db) else {
        return;
    };
    let value = crate::store::get_shard(db_ref, key)
        .read()
        .get(key)
        .map(ValueRef::to_value);
    for (name, def) in indexes.iter() {
        if is_alias(name, def) || def.db_index != db || !def.covers(key) {
            continue;
        }
        match (&def.on, &value) {
            (IndexOn::Hash, Some(crate::types::KiviDBValue::Hash(hash))) => {
                let pairs: Vec<(String, Arc<[u8]>)> = hash
                    .read()
                    .iter()
                    .map(|(field, value)| (field.to_string(), Arc::clone(value)))
                    .collect();
                index_fields(def, &crate::key_bytes::text_of(key), &pairs, how);
            }
            (IndexOn::Hash, Some(crate::types::KiviDBValue::HashEx(hash))) => {
                let pairs: Vec<(String, Arc<[u8]>)> = hash
                    .read()
                    .fields
                    .iter()
                    .map(|(field, value)| (field.to_string(), Arc::clone(value)))
                    .collect();
                index_fields(def, &crate::key_bytes::text_of(key), &pairs, how);
            }
            (IndexOn::Json, Some(crate::types::KiviDBValue::Json(doc))) => {
                let fields: Vec<(String, Arc<[u8]>)> =
                    project_json(&def.fields, &doc.read()).into_iter().collect();
                index_fields(def, &crate::key_bytes::text_of(key), &fields, how);
            }
            _ => unindex_from(def, &crate::key_bytes::text_of(key)),
        }
    }
}

/// How a write is applied to an index (see [`index_fields`]).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Write {
    /// The fields given were written; the document's others are as they
    /// were (`HSET`).
    Fields,
    /// The fields given are all the document has: a field of the schema not
    /// among them is one it does not hold (a new hash, a JSON document, a
    /// value put under the key whole).
    Whole,
    /// [`Write::Whole`], but a vector the graph already holds for the key is
    /// left there (see [`reindex_key`]).
    Refresh,
}

/// Some fields of a hash in database `db` were written: bring the `ON HASH`
/// indexes of that database that cover its key up to date with them. `how`
/// is [`Write::Whole`] for a hash the write created.
pub fn maybe_index(
    index_store: &IndexStore,
    db: usize,
    key: &(impl AsRef<[u8]> + ?Sized),
    fields: &[(String, Arc<[u8]>)],
    how: Write,
) {
    let key = key.as_ref();
    if indexing_deferred() || index_store.none() {
        return;
    }
    let store = index_store.read();

    for (name, def) in store.iter() {
        if is_alias(name, def)
            || !matches!(def.on, IndexOn::Hash)
            || def.db_index != db
            || !def.covers(key)
        {
            continue;
        }
        index_fields(def, &crate::key_bytes::text_of(key), fields, how);
    }
}

/// Index `fields` of the document at `key` in `def`: the words of its TEXT
/// fields, its TAG and NUMERIC values, and its vectors.
fn index_fields(def: &IndexDef, key: &str, fields: &[(String, Arc<[u8]>)], how: Write) {
    #[cfg(test)]
    crate::test_hooks::at("index_fields: fields read");
    let whole = how != Write::Fields;

    // ── The words of the TEXT fields written ────────────────────────────────
    //
    // Each TEXT field written replaces what it held before: its words,
    // stopwords left out, with their stems (see `IndexDef::write_text`).
    // Read before any lock is taken.
    let mut changes: Vec<(u16, Vec<String>)> = Vec::new();
    let mut restemmed: Vec<(u16, bool)> = Vec::new();
    let mut language = def.language.language;
    if def.text_fields().next().is_some() {
        let before = (def.language.field.is_some() && !whole).then(|| {
            def.doc_languages
                .read()
                .get(key)
                .copied()
                .unwrap_or(def.language.language)
        });
        language = def.doc_language(key, fields, whole);
        for (number, f) in def.text_fields().enumerate() {
            let number = number as u16;
            let Some((_, raw)) = fields.iter().find(|(n, _)| n == &f.name) else {
                if whole {
                    changes.push((number, Vec::new()));
                } else if before.is_some_and(|before| before != language) {
                    // The document's language changed and this field did
                    // not: its words are stemmed again in the new one.
                    restemmed.push((number, !f.options.nostem));
                }
                continue;
            };
            let tokens: Vec<String> = match std::str::from_utf8(raw) {
                Ok(s) => stem::index_words(def, f, language, s),
                Err(_) => Vec::new(),
            };
            changes.push((number, tokens));
        }
    }

    // ── Document id, TAG and NUMERIC postings ───────────────────────────────
    //
    // Every document of the index is given an id, whatever it holds: `*`
    // and `NOT` are in terms of all of them, and the TEXT index is too. A
    // write that finds a query reading them is queued, not kept waiting (see
    // `postings`): its vector then learns its id when a filtered search
    // first asks. Its words (inverted index, vocabulary, BM25 statistics) go
    // with it: the postings write them with the id they give it, under their
    // lock, at once or when the queue is applied (see `postings::WithId`).
    let doc = if def.gives_ids() {
        let words = def.text_write(changes, restemmed, language);
        def.postings.write_fields(key, fields, whole, words)
    } else {
        postings::NO_DOC
    };

    {
        // ── Update live vector indexes ────────────────────────────────────────
        //
        // Walk every VECTOR field in this index's schema. Each one may have
        // a value in the incoming HSET fields; insert into that field's
        // LiveIndex. Fields without a value in this HSET are simply skipped.
        for f in &def.fields {
            let (dim, data_type, metric) = match &f.field_type {
                FieldType::Vector {
                    dim,
                    data_type,
                    metric,
                    ..
                } => (*dim, *data_type, metric.clone()),
                _ => continue,
            };
            let Some(live) = def.indexes.get(&f.name) else {
                continue;
            };
            let vec = fields
                .iter()
                .find(|(n, _)| n == &f.name)
                .and_then(|(_, b)| data_type.decode(b, dim));
            let Some(vec) = vec else {
                // The document holds no vector there (any more).
                if whole {
                    unindex_vector(live, key);
                }
                continue;
            };
            match live {
                LiveIndex::Flat(idx) => idx.insert(key.to_string(), vec),
                LiveIndex::Hnsw(idx) => {
                    if how == Write::Refresh && idx.key_to_id.read().contains_key(key) {
                        continue;
                    }
                    hnsw_insert_doc(idx, key.to_string(), vec, &metric, doc);
                }
            }
        }
    }
}

/// The words TEXT field `number` of document `doc` holds in `text`, stemmed
/// again in `language` if the field `stems`: `None` if it holds none.
fn restem(
    text: &text::TextIndex,
    doc: u32,
    number: u16,
    stems: bool,
    language: stem::Language,
) -> Option<Vec<String>> {
    let words = text.field_words(doc, number)?;
    let mut tokens = Vec::with_capacity(words.len() * 2);
    for word in words {
        let stemmed = if stems {
            stem::index_stem(language, &word)
        } else {
            None
        };
        tokens.push(word);
        if let Some(s) = stemmed {
            tokens.push(format!("{}{s}", stem::STEM_PREFIX));
        }
    }
    Some(tokens)
}

/// Take the vector of the document at `key` out of a vector index: from a
/// FLAT index, at once; from a graph, by deleting its node in place.
fn unindex_vector(live: &LiveIndex, key: &str) {
    match live {
        LiveIndex::Flat(idx) => {
            idx.vectors.write().remove(key);
        }
        LiveIndex::Hnsw(idx) => {
            let node_id = idx.key_to_id.write().remove(key);
            if let Some(node_id) = node_id {
                let mut nodes = idx.nodes.write();
                if nodes.get(node_id).is_some_and(|n| n.key == key) {
                    idx.delete(&mut nodes, node_id);
                }
            }
        }
    }
}

/// The document at `key` has left `def`: its vectors, then its words, then
/// its postings and its id (the order a filtered search relies on: see
/// [`HnswNode::doc`]; the words are in terms of the id).
fn unindex_from(def: &IndexDef, key: &str) {
    for live in def.indexes.values() {
        unindex_vector(live, key);
    }
    // The words are taken out by the postings, before they give the id back.
    if def.gives_ids() {
        def.postings.remove(key, def.text_removal());
    }
    def.forget_language(key);
}

// ── Keys that expire on being read ───────────────────────────────────────────
//
// A key found expired by a read is deleted there and then (`helpers::
// delete_key_if_expired`), where there is no index store to hand. Each
// server's store is paired with its index store here, so that the key leaves
// its indexes too: before, it stayed in the graph and was still found by a
// KNN query.

static INDEX_STORES: RwLock<Vec<(usize, IndexStore)>> = RwLock::new(Vec::new());

pub(crate) fn store_id(store: &crate::store::Store) -> usize {
    Arc::as_ptr(store) as *const () as usize
}

/// Pair a server's store with its index store.
pub fn register_index_store(store: &crate::store::Store, index_store: &IndexStore) {
    let id = store_id(store);
    let mut stores = INDEX_STORES.write();
    stores.retain(|(other, _)| *other != id);
    stores.push((id, Arc::clone(index_store)));
}

/// `key` of database `db` of the store at address `id` ([`store_id`])
/// expired and was deleted: out of its indexes too.
pub fn unindex_expired_in(id: usize, db: usize, key: &(impl AsRef<[u8]> + ?Sized)) {
    let index_store = INDEX_STORES
        .read()
        .iter()
        .find(|(other, _)| *other == id)
        .map(|(_, index_store)| Arc::clone(index_store));
    if let Some(index_store) = index_store {
        maybe_unindex(&index_store, db, key);
    }
}

// ── Keys on their way out of the indexes ─────────────────────────────────────
//
// An expiry takes a key out of the store under its shard's lock, and out of
// its indexes once that lock is let go of: taken out under it, a vector would
// wait there for a KNN query's hold of the graph, and every write to the
// shard behind it. In between, the key is gone from the store and its
// document still in the postings. A count read off the postings (FT.SEARCH
// ... LIMIT 0 0) leaves out the keys past their time that nothing has deleted
// yet, found in the shards' lists of keys with a time to live; a key the
// expiry had taken was in no list, and counted (#249). So the expiry marks
// the key leaving before it lets go of the shard, and unmarks it once the
// key has left its indexes (its postings' removal applied, or queued for
// whoever takes their lock next); the count leaves out the keys marked too.
//
// Only where the key's database has an index: elsewhere nothing is marked.
// The marks' lock is taken last, with nothing waited for under it.

/// Keys marked leaving: the store's address ([`store_id`]), the database,
/// the key.
static LEAVING: parking_lot::Mutex<Vec<(usize, usize, Box<str>)>> =
    parking_lot::Mutex::new(Vec::new());

/// Whether `index_store` has an index of database `db`: whether a key of it
/// that expires is marked leaving ([`mark_leaving`]).
///
/// Asked with the keys' write order held. Held shared (a hash write that
/// found no index, and expires its fields), the write is over before a
/// first index is added ([`Indexes::write_to_add`]); held whole, it falls
/// before or after a walk's visit of each key, as any write does (#570).
pub fn has_index_of(index_store: &IndexStore, db: usize) -> bool {
    !index_store.none() && index_store.read().values().any(|def| def.db_index == db)
}

/// The index store of the store at address `id` ([`store_id`]), if it has an
/// index of database `db`.
pub fn indexes_of(id: usize, db: usize) -> Option<IndexStore> {
    let index_store = INDEX_STORES
        .read()
        .iter()
        .find(|(other, _)| *other == id)
        .map(|(_, index_store)| Arc::clone(index_store))?;
    has_index_of(&index_store, db).then_some(index_store)
}

/// `key` of database `db` of the store at address `id` is gone from the
/// store and not yet from its indexes: under the shard's lock that took it.
pub fn mark_leaving(id: usize, db: usize, key: &(impl AsRef<[u8]> + ?Sized)) {
    let key: Box<str> = crate::key_bytes::text_of(key.as_ref()).into();
    LEAVING.lock().push((id, db, key));
}

/// `key` has left its indexes: what [`mark_leaving`] marked, unmarked.
pub fn unmark_leaving(id: usize, db: usize, key: &(impl AsRef<[u8]> + ?Sized)) {
    let key = crate::key_bytes::text_of(key.as_ref());
    let key: &str = &key;
    let mut leaving = LEAVING.lock();
    if let Some(at) = leaving
        .iter()
        .position(|(i, d, k)| *i == id && *d == db && **k == *key)
    {
        leaving.swap_remove(at);
    }
}

/// The keys of database `db` of the store at address `id` marked leaving.
pub fn leaving(id: usize, db: usize) -> Vec<Box<str>> {
    LEAVING
        .lock()
        .iter()
        .filter(|(i, d, _)| *i == id && *d == db)
        .map(|(_, _, key)| key.clone())
        .collect()
}

#[cfg(test)]
mod leaving_tests {
    use super::*;

    #[test]
    fn a_key_is_leaving_from_its_mark_to_its_unmark() {
        // An address no store has.
        let id = usize::MAX - 249;
        assert!(leaving(id, 0).is_empty());
        mark_leaving(id, 0, "a");
        mark_leaving(id, 0, "b");
        mark_leaving(id, 1, "a");
        let mut keys = leaving(id, 0);
        keys.sort();
        assert_eq!(keys, vec![Box::from("a"), Box::from("b")]);
        unmark_leaving(id, 0, "a");
        assert_eq!(leaving(id, 0), vec![Box::from("b")]);
        assert_eq!(leaving(id, 1), vec![Box::from("a")]);
        unmark_leaving(id, 0, "b");
        unmark_leaving(id, 1, "a");
        assert!(leaving(id, 0).is_empty());
        assert!(leaving(id, 1).is_empty());
    }
}

#[cfg(test)]
mod indexes_tests {
    use super::*;

    fn schema(name: &str) -> IndexDef {
        IndexSchema {
            name: name.into(),
            db_index: 0,
            on: IndexOn::Hash,
            prefixes: Vec::new(),
            fields: Vec::new(),
            aliases: Vec::new(),
            stopwords: None,
            synonyms: Default::default(),
            language: Default::default(),
            default_score: 1.0,
            score_field: None,
        }
        .into_def()
    }

    /// `none` is what the map holds whenever the lock is free, and false
    /// for as long as anything holds it to change it (#544).
    #[test]
    fn none_follows_the_map_and_is_false_while_it_is_changed() {
        let store = new_index_store();
        assert!(store.none());
        {
            let mut indexes = store.write();
            assert!(!store.none(), "held to change: not none");
            indexes.insert("idx".into(), schema("idx"));
        }
        assert!(!store.none());
        set_alias(&mut store.write(), "al", &schema("idx"));
        assert!(!store.none());
        {
            let indexes = store.write();
            assert!(!store.none());
            drop(indexes);
        }
        assert!(!store.none(), "still has its index");
        assert!(remove_index(&mut store.write(), "idx"));
        assert!(store.none(), "the index and its alias are gone");
        assert!(store.read().is_empty());
        {
            let _held = store.write();
            assert!(!store.none());
        }
        assert!(store.none(), "nothing was added");
        store.write().insert("other".into(), schema("other"));
        assert!(has_index_of(&store, 0));
        assert_eq!(drop_all_indexes(&store), 1);
        assert!(store.none());
        assert!(!has_index_of(&store, 0));
    }

    /// The first index is added only once every write that holds a key's
    /// order (a hash write that found no index holds it shared) is done;
    /// meanwhile `none` is false, so a write that asks then takes its order
    /// whole (#570). With an index there, nothing is waited for.
    #[test]
    fn the_first_index_waits_for_the_writes_that_found_none() {
        use std::sync::atomic::Ordering::SeqCst;
        use std::time::{Duration, Instant};
        let store = new_index_store();
        // An `HSET` that found no index, holding its key's order shared.
        let writing = crate::write_order::lock_key_shared(0, b"doc:570");
        let added = Arc::new(AtomicBool::new(false));
        let adder = {
            let (store, added) = (Arc::clone(&store), Arc::clone(&added));
            std::thread::spawn(move || {
                store
                    .write_to_add()
                    .unwrap()
                    .insert("idx".into(), schema("idx"));
                added.store(true, SeqCst);
            })
        };
        let deadline = Instant::now() + Duration::from_secs(10);
        while store.none() {
            assert!(Instant::now() < deadline, "none never became false");
            std::thread::yield_now();
        }
        std::thread::sleep(Duration::from_millis(100));
        assert!(
            !added.load(SeqCst),
            "added beside a write that found no index"
        );
        assert!(store.read().is_empty());
        assert!(!store.none(), "none while the adder waits");
        drop(writing);
        adder.join().unwrap();
        assert!(store.read().contains_key("idx"));
        assert!(!store.none());

        let writing = crate::write_order::lock_key_shared(0, b"doc:570");
        let more = Arc::clone(&store);
        std::thread::spawn(move || {
            more.write_to_add()
                .unwrap()
                .insert("other".into(), schema("other"));
        })
        .join()
        .unwrap();
        drop(writing);
        assert_eq!(drop_all_indexes(&store), 2);
        assert!(store.none());
    }
}

// ── Distance functions ───────────────────────────────────────────────────────
//
// Each is one pass of a kernel in `simd`: AVX-512, AVX2 with FMA, or NEON,
// picked at run time, or eight independent sums where there is none. The
// sequential `iter().map().sum()` these replace could not be vectorised: the
// compiler may not reorder float additions.

pub fn dist_l2_sq(a: &[f32], b: &[f32]) -> f32 {
    simd::l2_sq(a, b)
}

pub fn dist_cosine(a: &[f32], b: &[f32]) -> f32 {
    let (dot, na, nb) = simd::dot_norms(a, b);
    let denom = na.sqrt() * nb.sqrt();
    if denom == 0.0 {
        return 1.0;
    }
    1.0 - (dot / denom)
}

pub fn dist_ip(a: &[f32], b: &[f32]) -> f32 {
    1.0 - simd::dot(a, b)
}

pub fn distance(metric: &DistanceMetric, a: &[f32], b: &[f32]) -> f32 {
    match metric {
        DistanceMetric::L2 => dist_l2_sq(a, b),
        DistanceMetric::Cosine => dist_cosine(a, b),
        DistanceMetric::InnerProduct => dist_ip(a, b),
    }
}

/// Fast cosine distance for pre-normalized (unit-length) vectors.
/// Cosine vectors are normalized to unit length at ingestion, so the
/// denominator is always 1.0 and we skip the two sqrt() calls entirely.
/// For L2 and InnerProduct the behaviour is identical to `distance`.
#[inline(always)]
pub(crate) fn distance_prenorm(metric: &DistanceMetric, a: &[f32], b: &[f32]) -> f32 {
    match metric {
        DistanceMetric::Cosine => dist_ip(a, b), // |a|=|b|=1 → 1 – dot(a,b)
        DistanceMetric::L2 => dist_l2_sq(a, b),
        DistanceMetric::InnerProduct => dist_ip(a, b),
    }
}

/// Normalize a vector to unit length in-place (for cosine ingestion).
fn normalize_to_unit(mut v: Vec<f32>) -> Vec<f32> {
    let mag = simd::norm_sq(&v).sqrt();
    if mag > 0.0 {
        for x in &mut v {
            *x /= mag;
        }
    }
    v
}

/// A query made ready to be measured against many stored vectors: for a
/// cosine, scaled to unit length once, so that each distance after is one
/// pass that leaves out the query's own norm.
pub struct PreparedQuery<'a> {
    metric: &'a DistanceMetric,
    vector: std::borrow::Cow<'a, [f32]>,
}

impl<'a> PreparedQuery<'a> {
    pub fn new(metric: &'a DistanceMetric, query: &'a [f32]) -> Self {
        let vector = match metric {
            DistanceMetric::Cosine => std::borrow::Cow::Owned(normalize_to_unit(query.to_vec())),
            _ => std::borrow::Cow::Borrowed(query),
        };
        Self { metric, vector }
    }

    /// The query as it is measured: of unit length for a cosine.
    pub fn vector(&self) -> &[f32] {
        &self.vector
    }

    /// The distance to a vector stored as it was given (a FLAT index):
    /// what [`distance`] gives on the query as it was.
    #[inline]
    pub fn to_raw(&self, stored: &[f32]) -> f32 {
        match self.metric {
            DistanceMetric::Cosine => {
                let (dot, nb) = simd::dot_norm_b(&self.vector, stored);
                if nb == 0.0 {
                    // A zero vector on either side, as `dist_cosine` has it.
                    return 1.0;
                }
                1.0 - dot / nb.sqrt()
            }
            DistanceMetric::L2 => dist_l2_sq(&self.vector, stored),
            DistanceMetric::InnerProduct => dist_ip(&self.vector, stored),
        }
    }

    /// The distance to a vector an HNSW graph holds: of unit length for a
    /// cosine.
    #[inline]
    pub fn to_unit(&self, stored: &[f32]) -> f32 {
        distance_prenorm(self.metric, &self.vector, stored)
    }
}

// A result from KNN search
#[derive(Debug)]
pub struct KnnResult {
    pub key: String,
    pub score: f32,
}

// ── The f32 ordering problem ─────────────────────────────────────────────────
//
// Rust's BinaryHeap requires Ord. f32 does NOT implement Ord because NaN
// breaks total ordering (NaN != NaN). We wrap f32 in a newtype and implement
// Ord ourselves, treating NaN as greater than everything (i.e. worst score).
// This is a common Rust pattern whenever you need to sort/heap floats.

// #[derive(PartialEq)]
// struct OrdF32(f32);

// impl Eq for OrdF32 {}

// impl PartialOrd for OrdF32 {
//     fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
//         Some(self.cmp(other))
//     }
// }

// impl Ord for OrdF32 {
//     fn cmp(&self, other: &Self) -> std::cmp::Ordering {
//         // total_cmp is stable since Rust 1.62 — handles NaN/inf correctly
//         self.0.total_cmp(&other.0)
//     }
// }

pub fn knn_flat(
    index: &VectorIndex,
    query: &[f32],
    k: usize,
    metric: &DistanceMetric,
) -> Vec<KnnResult> {
    // Collect all distances first, then partial-sort.
    // Avoids the heap tie-breaking problem entirely.
    let vectors = index.vectors.read();
    let prepared = PreparedQuery::new(metric, query);

    let mut scored: Vec<(f32, &str)> = vectors
        .iter()
        .filter(|(_, vec)| vec.len() == query.len())
        .map(|(key, vec)| (prepared.to_raw(vec), key.as_str()))
        .collect();

    // partial_sort_unstable would be ideal but isn't in std.
    // select_nth_unstable partitions so that element k is in its sorted position
    // and everything before it is <= it — O(n) average, no full sort needed.
    // This is the standard competitive KNN trick for FLAT search.
    if scored.len() > k {
        scored.select_nth_unstable_by(k, |a, b| a.0.total_cmp(&b.0));
        scored.truncate(k);
    }

    // Now sort the kept k results ascending by distance
    scored.sort_unstable_by(|a, b| a.0.total_cmp(&b.0));

    scored
        .into_iter()
        .map(|(score, key)| KnnResult {
            key: key.to_string(),
            score,
        })
        .collect()
}

/// Algorithm 4 from the HNSW paper — heuristic neighbor selection.
///
/// The greedy approach (take the M closest) clusters neighbors in the same
/// direction in high-dimensional spaces. This hurts recall badly at 100D+.
///
/// The heuristic: accept a candidate only if it is closer to the query `q`
/// than it is to every already-accepted result neighbor. This ensures the
/// M selected neighbors "fan out" across the local neighborhood rather than
/// piling up in one direction.
///
/// `candidates` — (dist_to_q, node_id), sorted ascending by dist_to_q.
/// `keep_pruned` — if true, fill remaining slots with the discarded set so
///                 we always return exactly m_max neighbors when possible.
/// Choose up to `m_max` of `candidates` as a node's neighbours. Like the
/// search, it offers its read lock to a waiting writer every few candidates.
fn select_neighbors_heuristic(
    slots: &slots::Slots,
    nodes: &mut NodesGuard<'_>,
    candidates: &[(f32, u32)],
    m_max: usize,
    metric: &DistanceMetric,
    keep_pruned: bool,
) -> Vec<u32> {
    let mut result: Vec<(f32, u32)> = Vec::with_capacity(m_max);
    let mut discarded: Vec<(f32, u32)> = Vec::new();
    let next_lines = PREFETCH_NEXT;

    'cand: for (i, &(d_to_q, cand_id)) in candidates.iter().enumerate() {
        if result.len() >= m_max {
            break;
        }
        if i > 0 && i.is_multiple_of(EXPANSIONS_PER_YIELD) {
            parking_lot::RwLockReadGuard::bump(nodes);
        }
        if let Some(&(_, next)) = candidates.get(i + 1) {
            slots.prefetch(next as usize, next_lines);
        }
        let cand = cand_id as usize;
        if !slots.is_live(cand) {
            continue;
        }
        let cand_vec = slots.vector(cand);
        // Key heuristic check: is the candidate closer to q than to any
        // already-selected result neighbor?  If a result neighbor is closer
        // to the candidate than q is, the candidate is "dominated" — the
        // result neighbor already covers that direction better.
        for &(_, sel_id) in &result {
            let d_cand_to_sel = distance_prenorm(metric, cand_vec, slots.vector(sel_id as usize));
            if d_cand_to_sel < d_to_q {
                discarded.push((d_to_q, cand_id));
                continue 'cand;
            }
        }
        result.push((d_to_q, cand_id));
    }

    // Fill remaining capacity from the pruned set so we don't leave a node
    // with fewer connections than intended (sparse graphs hurt recall too).
    if keep_pruned {
        for &(d, id) in &discarded {
            if result.len() >= m_max {
                break;
            }
            result.push((d, id));
        }
    }

    result.into_iter().map(|(_, id)| id).collect()
}

/// The `m_max` of `ids` nearest the vector `of`, nearest first: the
/// neighbours a node keeps when one more is linked to it than it has room
/// for and the list pruned during the search is out of date.
fn nearest_of(
    slots: &slots::Slots,
    metric: &DistanceMetric,
    of: &[f32],
    ids: &[u32],
    m_max: usize,
) -> Vec<u32> {
    BACKLINK_SCRATCH.with(|cell| {
        let mut scored = cell.borrow_mut();
        scored.clear();
        scored.reserve(ids.len());
        for &id in ids {
            if !slots.is_live(id as usize) {
                continue;
            }
            scored.push((distance_prenorm(metric, of, slots.vector(id as usize)), id));
        }
        scored.sort_unstable_by(|a, b| a.0.total_cmp(&b.0));
        scored.truncate(m_max);
        scored.iter().map(|&(_, id)| id).collect()
    })
}

/// Insert a new vector into the HNSW index.
pub fn hnsw_insert(index: &Arc<HnswIndex>, key: String, vector: Vec<f32>, metric: &DistanceMetric) {
    hnsw_insert_doc(index, key, vector, metric, postings::NO_DOC);
}

/// A forward neighbour that will overflow when it receives its back-link,
/// pruned during the search: (neighbour id, layer, its neighbours as the
/// search saw them, the list to replace them with). The new node is not in
/// the graph yet, so the list names it [`NEW_NODE`].
type PrecomputedBacklink = (u32, usize, Vec<u32>, Vec<u32>);
const NEW_NODE: u32 = u32::MAX;

/// [`hnsw_insert`] of the vector of the document whose id is `doc`.
pub fn hnsw_insert_doc(
    index: &Arc<HnswIndex>,
    key: String,
    vector: Vec<f32>,
    metric: &DistanceMetric,
    doc: u32,
) {
    // ── Phase 0: delete the key's old node, if it has one ─────────────────
    // Its slot goes to the free list; this insert may well fill it again.
    let old_id = index.key_to_id.write().remove(&key);
    if let Some(old_id) = old_id {
        let mut nodes = index.nodes.write();
        if nodes.get(old_id).is_some_and(|n| n.key == key) {
            index.delete(&mut nodes, old_id);
        }
    }

    // Cosine vectors are stored as unit-length so that all distance calls can
    // skip the expensive sqrt/divide and use pure dot product instead.
    let vector = if matches!(metric, DistanceMetric::Cosine) {
        normalize_to_unit(vector)
    } else {
        vector
    };
    // The first vector sets the graph's dimension; one of another (which
    // the schema rules out) has nowhere to go.
    let slots = index
        .slots
        .get_or_init(|| slots::Slots::new(vector.len(), index.m0 + slack(index.m0)));
    if vector.len() != slots.dim() {
        return;
    }

    let level = {
        use rand::Rng;
        let f: f64 = rand::thread_rng().r#gen::<f64>();
        (-f.ln() * index.ml).floor() as usize
    };

    // ── Phase 1: snapshot entry point ────────────────────────────────────
    let ep_snapshot = *index.entry_point.read();

    // ── Phase 2: pure read traversal — NO global write lock held ─────────
    // This is the hot path. Concurrent inserts and searches run freely here.
    //
    // For each forward neighbour predicted to overflow when it receives its
    // back-link, its neighbours are scored against it here, under the read
    // lock, so that linking (under the neighbour's lock) only swaps the
    // pruned list in.
    let mut precomputed_backlinks: Vec<PrecomputedBacklink> = Vec::new();

    // The whole insert holds the graph for reading: it appends its node and
    // links it in under the neighbours' own locks, so inserts do not wait for
    // one another, nor searches for them. Before, each insert took the write
    // lock to link itself in, and a writer that waits holds back every reader
    // that comes after it: 6,000 inserts a second on 64 cores, 51 of 62
    // workers waiting.
    let mut nodes = index.nodes.read();
    let mut neighbors_per_layer: Vec<Vec<u32>> = if let Some(mut ep) = ep_snapshot {
        // Check if entry point is tombstoned — find a live replacement
        if !index.is_live(ep) {
            let k2id = index.key_to_id.read();
            match k2id.values().next().copied() {
                Some(id) => ep = id,
                None => {
                    // No live nodes at all — we become the entry point
                    drop(k2id);
                    let mut links: Vec<Vec<u32>> = (0..=level).map(|_| Vec::new()).collect();
                    let new_id = publish(
                        index,
                        slots,
                        &mut nodes,
                        key.clone(),
                        &vector,
                        level,
                        doc,
                        &mut links,
                    );
                    index.key_to_id.write().insert(key, new_id);
                    index.live_count.fetch_add(1, Relaxed);
                    *index.entry_point.write() = Some(new_id);
                    return;
                }
            }
        }

        let ep_level = nodes.get(ep).map_or(0, |n| n.level);

        // Greedy descent from top layer to level+1 (just finding better ep)
        let mut curr_ep = ep as u32;
        let mut per_layer: Vec<Vec<u32>> = Vec::with_capacity(level + 1);
        for lc in (level + 1..=ep_level).rev() {
            let results = search_layer(index, slots, &mut nodes, &vector, curr_ep, 1, lc, metric);
            if let Some(&(_, best)) = results.first() {
                curr_ep = best;
            }
        }

        // Search and collect neighbors for layers 0..=min(level, ep_level)
        for lc in (0..=level.min(ep_level)).rev() {
            let m_max = if lc == 0 { index.m0 } else { index.m };
            let candidates = search_layer(
                index,
                slots,
                &mut nodes,
                &vector,
                curr_ep,
                index.ef_construction,
                lc,
                metric,
            );
            if let Some(&(_, best)) = candidates.first() {
                curr_ep = best;
            }
            let selected =
                select_neighbors_heuristic(slots, &mut nodes, &candidates, m_max, metric, true);
            per_layer.push(selected);
        }

        // Reverse first so per_layer[0] = layer-0 neighbors, then pad with
        // empty vecs for layers above ep_level when level > ep_level.
        // (Padding before reversing would place the empty entries at [0].)
        per_layer.reverse(); // per_layer[0] = layer 0 neighbors
        while per_layer.len() <= level {
            per_layer.push(vec![]);
        }

        // Prune, before taking any neighbour's lock, every forward neighbour
        // that the back-link will overflow; linking then only swaps the
        // pruned list in. Pruning reads up to M x 4 vectors from all over the
        // graph, for some 32 neighbours an insert.
        let mut seen: Vec<u32> = Vec::with_capacity(index.m0);
        for (lc, forward_neighbors) in per_layer.iter().enumerate() {
            let m_max = if lc == 0 { index.m0 } else { index.m };
            for &nb_id in forward_neighbors {
                let nb = nb_id as usize;
                if !slots.is_live(nb) {
                    continue;
                }
                seen.clear();
                index.links(&nodes, nb, lc, &mut seen);
                if seen.len() < m_max + slack(m_max) {
                    continue; // won't overflow
                }
                let nb_vec = slots.vector(nb);
                let vector_of = |id: u32| {
                    if id == NEW_NODE {
                        &vector[..]
                    } else {
                        slots.vector(id as usize)
                    }
                };
                let mut scored: Vec<(f32, u32)> = seen
                    .iter()
                    .filter(|&&id| slots.is_live(id as usize))
                    .map(|&id| {
                        (
                            distance_prenorm(metric, nb_vec, slots.vector(id as usize)),
                            id,
                        )
                    })
                    .collect();
                scored.push((distance_prenorm(metric, nb_vec, &vector), NEW_NODE));
                scored.sort_unstable_by(|a, b| a.0.total_cmp(&b.0));

                // Bounded Algorithm-4 heuristic: check each candidate
                // against at most K=4 already-accepted neighbours.
                for i in 0..scored.len() {
                    let (d_i, id_i) = scored[i];
                    let vec_i = vector_of(id_i);
                    let mut checks = 0usize;
                    for j in 0..i {
                        let (d_j, id_j) = scored[j];
                        if d_j.is_infinite() {
                            continue;
                        }
                        if distance_prenorm(metric, vec_i, vector_of(id_j)) < d_i {
                            scored[i].0 = f32::INFINITY;
                            break;
                        }
                        checks += 1;
                        if checks >= 4 {
                            break;
                        }
                    }
                }
                let mut pruned = Vec::with_capacity(m_max);
                let kept = scored.iter().filter(|(d, _)| !d.is_infinite());
                let dropped = scored.iter().filter(|(d, _)| d.is_infinite());
                pruned.extend(kept.chain(dropped).take(m_max).map(|&(_, id)| id));
                precomputed_backlinks.push((nb_id, lc, seen.clone(), pruned));
                parking_lot::RwLockReadGuard::bump(&mut nodes);
            }
        }

        per_layer
    } else {
        // First node ever — empty neighbor lists
        (0..=level).map(|_| vec![]).collect()
    };

    // Append the node. Nothing finds it until a neighbour links back to it
    // below, or a reader looks its key up after the next line.
    let new_id = publish(
        index,
        slots,
        &mut nodes,
        key.clone(),
        &vector,
        level,
        doc,
        &mut neighbors_per_layer,
    );
    index.key_to_id.write().insert(key, new_id);
    index.live_count.fetch_add(1, Relaxed);

    // Entry point bump if the new node has higher level.
    {
        let mut ep = index.entry_point.write();
        match *ep {
            None => *ep = Some(new_id),
            Some(current_ep) => {
                let current_level = nodes.get(current_ep).map_or(0, |n| n.level);
                if level > current_level || !index.is_live(current_ep) {
                    *ep = Some(new_id);
                }
            }
        }
    }

    // Back-links, each under that neighbour's lock alone.
    let mut list: Vec<u32> = Vec::with_capacity(index.m0 + 1);
    for (lc, forward) in neighbors_per_layer.iter().enumerate() {
        let m_max = if lc == 0 { index.m0 } else { index.m };
        for &nb_id in forward {
            let nb = nb_id as usize;
            if nb == new_id || !slots.is_live(nb) {
                continue;
            }
            let Some(nb_node) = nodes.get(nb) else {
                continue;
            };
            let mut upper = nb_node.upper.lock();
            if lc == 0 {
                if slots.push_link(nb, new_id as u32) {
                    continue;
                }
                list.clear();
                slots.read_links(nb, &mut list);
            } else {
                while upper.len() < lc {
                    upper.push(Vec::new());
                }
                let links = &mut upper[lc - 1];
                if links.len() < m_max + slack(m_max) {
                    links.push(new_id as u32);
                    continue;
                }
                list.clear();
                list.extend_from_slice(links);
            }

            // Fast path: the list pruned during the search, if the
            // neighbour's list is still the one it pruned.
            let fresh = precomputed_backlinks
                .iter()
                .find(|(pid, plc, seen, _)| *pid == nb_id && *plc == lc && *seen == list);
            let replacement: Vec<u32> = if let Some((_, _, _, pruned)) = fresh {
                pruned
                    .iter()
                    .map(|&id| if id == NEW_NODE { new_id as u32 } else { id })
                    .collect()
            } else {
                // Stale (a concurrent insert changed the list): keep the
                // m_max nearest.
                list.push(new_id as u32);
                nearest_of(slots, metric, slots.vector(nb), &list, m_max)
            };
            if lc == 0 {
                slots.write_links(nb, replacement);
            } else {
                upper[lc - 1] = replacement;
            }
        }
    }
}

/// How many links past its most a node's list may take before it is pruned
/// back to its most, so that a pruning is paid once for several back-links
/// rather than for each.
///
/// A list is full nearly always, so with no slack every back-link pruned it
/// again: an insert pruned some 32 neighbours, each measuring the
/// neighbour's links against one another, and that was half the distances
/// an insert measured. Pruning to below the most instead (leaving room)
/// saves as much, but leaves nodes with fewer links than before, and recall
/// at ef 64 fell by 0.004 in graphs built by the server. With slack a node
/// has at least the links it had, and up to 4 more: 16 bytes a node.
/// Measured on dbpedia-openai-100K-1536 (M 16, EF_CONSTRUCTION 128, an
/// 8-CPU D16s_v5): see the commit that brought it.
const PRUNE_SLACK: usize = 4;

/// [`PRUNE_SLACK`], for a list of at most `m_max` links.
fn slack(m_max: usize) -> usize {
    PRUNE_SLACK.min(m_max / 4)
}

/// Put a new node in the graph: in a deleted node's slot if there is one,
/// under the write lock (a search may still hold that id), or else in a
/// new slot, which no reader can reach before its node is there. `links`
/// are its links by layer; a link to the slot it takes is dropped (a node
/// met live in the search may have been deleted since, and its slot be the
/// one given to this node).
#[allow(clippy::too_many_arguments)]
fn publish(
    index: &HnswIndex,
    slots: &slots::Slots,
    nodes: &mut NodesGuard<'_>,
    key: String,
    vector: &[f32],
    level: usize,
    doc: u32,
    links: &mut [Vec<u32>],
) -> usize {
    let reuse = index.free.lock().pop();
    if let Some(id) = reuse {
        for list in links.iter_mut() {
            list.retain(|&x| x != id);
        }
        let id = id as usize;
        let upper = links[1..].to_vec();
        let layer0 = &links[0];
        parking_lot::RwLockReadGuard::unlocked(nodes, || {
            let mut graph = index.nodes.write();
            // SAFETY: the graph is held for writing, so no one reads the
            // slot; it was deleted, so no one writes it.
            unsafe { slots.write_vector(id, vector) };
            slots.write_links(id, layer0.iter().copied());
            if let Some(node) = graph.get_mut(id) {
                *node = HnswNode::new(key, level, upper, doc);
            }
            slots.flag(id).store(1, Release);
            index.reused.fetch_add(1, Release);
            index.tombstone_count.fetch_sub(1, Relaxed);
        });
        id
    } else {
        let upper = links[1..].to_vec();
        let layer0 = &links[0];
        nodes.push_with(|id| {
            slots.reserve(id);
            // SAFETY: a new slot: nothing links to it and no key names it.
            unsafe { slots.write_vector(id, vector) };
            slots.write_links(id, layer0.iter().copied());
            slots.flag(id).store(1, Release);
            HnswNode::new(key, level, upper, doc)
        })
    }
}

thread_local! {
    static BACKLINK_SCRATCH: std::cell::RefCell<Vec<(f32, u32)>> =
        std::cell::RefCell::new(Vec::with_capacity(64));
}

type MinHeap =
    std::collections::BinaryHeap<std::cmp::Reverse<(ordered_float::OrderedFloat<f32>, u32)>>;
type MaxHeap = std::collections::BinaryHeap<(ordered_float::OrderedFloat<f32>, u32)>;

/// Per-thread scratch buffers for HNSW search.
/// Allocated once per thread, reused across every search call.
struct SearchBuffers {
    visited: slots::Visited,
    candidates: MinHeap,
    dynamic: MaxHeap,
    neighbor_scratch: Vec<u32>,
}
impl SearchBuffers {
    fn new() -> Self {
        Self {
            visited: slots::Visited::default(),
            candidates: std::collections::BinaryHeap::with_capacity(256),
            dynamic: std::collections::BinaryHeap::with_capacity(256),
            neighbor_scratch: Vec::with_capacity(64),
        }
    }

    /// Empty, for a graph of `nodes` slots.
    fn clear(&mut self, nodes: usize) {
        self.visited.reset(nodes);
        self.candidates.clear();
        self.dynamic.clear();
        self.neighbor_scratch.clear();
    }
}

thread_local! {
    static SEARCH_BUFFERS: std::cell::RefCell<SearchBuffers> =
        std::cell::RefCell::new(SearchBuffers::new());
}

/// How many candidates a search expands between offers of its read lock to a
/// waiting writer (see [`search_layer`]).
const EXPANSIONS_PER_YIELD: usize = 4;

/// Cache lines of each neighbour's vector asked for as soon as the
/// neighbours of a node are known, before any is measured, and of the next
/// neighbour's while one is measured.
///
/// The hardware brings the rest of a vector in once its first lines are
/// read. Measured on dbpedia-openai-100K-1536 (D16s_v5, ef 128): nothing
/// asked for, 778 queries a second on one thread and 3,872 on eight; the
/// whole of the next vector, 766 and 3,526 (the lines asked for push out
/// those being read); 4 and 8 lines, 958 and 4,353. From 1 to 4 lines up
/// front and 2 to 8 of the next measured within a few percent of that.
const PREFETCH_FIRST: usize = 4;
const PREFETCH_NEXT: usize = 8;

/// Search one layer of the graph from `ep`, the graph held for reading.
/// Returns up to `ef` candidates as (distance, node_id), sorted ascending.
///
/// Every few candidates the read lock is handed to a writer waiting for it,
/// if there is one, and taken back: a writer that waits holds back every
/// reader that comes after it, so a long search would hold up all of them.
/// Ids stay valid across the yield: slots never move. One may name another
/// node after it, if a deleted slot was filled again meanwhile; the search
/// then only measures a node it did not mean to (see [`HnswIndex::reused`]).
///
/// The neighbours of each node expanded are read without a lock, and their
/// vectors asked for before any is measured: the vectors are the search's
/// cost, and most come from memory.
#[allow(clippy::too_many_arguments)]
fn search_layer(
    index: &HnswIndex,
    slots: &slots::Slots,
    nodes: &mut NodesGuard<'_>,
    query: &[f32],
    ep: u32,
    ef: usize,
    layer: usize,
    metric: &DistanceMetric,
) -> Vec<(f32, u32)> {
    use ordered_float::OrderedFloat;
    use std::cmp::Reverse;
    let ep_id = ep as usize;
    if !slots.has(ep_id) || !slots.is_live(ep_id) {
        return vec![];
    }
    let ep_dist = distance_prenorm(metric, query, slots.vector(ep_id));
    let (first, next) = (PREFETCH_FIRST, PREFETCH_NEXT);

    SEARCH_BUFFERS.with(|cell| {
        let mut guard = cell.borrow_mut();
        let bufs = &mut *guard;
        bufs.clear(nodes.count());

        let of_ep = OrderedFloat(ep_dist);
        bufs.visited.insert(ep);
        bufs.candidates.push(Reverse((of_ep, ep)));
        bufs.dynamic.push((of_ep, ep));

        let mut expanded = 0usize;
        while let Some(Reverse((c_dist, c_id))) = bufs.candidates.pop() {
            let worst = bufs.dynamic.peek().map(|(d, _)| d.0).unwrap_or(f32::MAX);
            if bufs.dynamic.len() >= ef && c_dist.0 > worst {
                break;
            }
            expanded += 1;
            if expanded.is_multiple_of(EXPANSIONS_PER_YIELD) {
                parking_lot::RwLockReadGuard::bump(nodes);
            }

            // The neighbours not met yet, each asked for ahead of its turn.
            bufs.neighbor_scratch.clear();
            index.links(nodes, c_id as usize, layer, &mut bufs.neighbor_scratch);
            let mut fresh = 0;
            for i in 0..bufs.neighbor_scratch.len() {
                let id = bufs.neighbor_scratch[i];
                if bufs.visited.insert(id) {
                    bufs.neighbor_scratch[fresh] = id;
                    fresh += 1;
                    slots.prefetch(id as usize, first);
                }
            }

            for i in 0..fresh {
                if i + 1 < fresh {
                    slots.prefetch(bufs.neighbor_scratch[i + 1] as usize, next);
                }
                let nb_id = bufs.neighbor_scratch[i];
                if !slots.is_live(nb_id as usize) {
                    continue;
                }
                let nb_dist = distance_prenorm(metric, query, slots.vector(nb_id as usize));

                let worst = bufs.dynamic.peek().map(|(d, _)| d.0).unwrap_or(f32::MAX);
                if nb_dist < worst || bufs.dynamic.len() < ef {
                    let of = OrderedFloat(nb_dist);
                    bufs.candidates.push(Reverse((of, nb_id)));
                    bufs.dynamic.push((of, nb_id));
                    if bufs.dynamic.len() > ef {
                        bufs.dynamic.pop();
                    }
                }
            }
        }

        let mut result: Vec<(f32, u32)> = bufs.dynamic.drain().map(|(d, id)| (d.0, id)).collect();
        result.sort_unstable_by(|a, b| a.0.total_cmp(&b.0));
        result
    })
}

/// The `k` nearest of `found` (sorted ascending), as results: the live
/// ones, with their keys. If a deleted slot was filled again since the
/// search took note of `reused`, an id it holds may name another node now,
/// and each is measured again first.
#[allow(clippy::too_many_arguments)]
fn into_results(
    index: &HnswIndex,
    slots: &slots::Slots,
    nodes: &HnswNodes,
    query: &[f32],
    metric: &DistanceMetric,
    mut found: Vec<(f32, u32)>,
    k: usize,
    reused: u64,
) -> Vec<KnnResult> {
    if index.reused.load(Acquire) != reused {
        for r in found.iter_mut() {
            let id = r.1 as usize;
            r.0 = if slots.is_live(id) {
                distance_prenorm(metric, query, slots.vector(id))
            } else {
                f32::INFINITY
            };
        }
        found.sort_unstable_by(|a, b| a.0.total_cmp(&b.0));
    }
    found.truncate(k);
    found
        .into_iter()
        .filter(|(_, id)| slots.is_live(*id as usize))
        .filter_map(|(score, id)| {
            nodes.get(id as usize).map(|n| KnnResult {
                key: n.key.clone(),
                score,
            })
        })
        .collect()
}

/// KNN search using the HNSW graph.
pub fn knn_hnsw(
    index: &HnswIndex,
    query: &[f32],
    k: usize,
    ef: usize,
    metric: &DistanceMetric,
) -> Vec<KnnResult> {
    let ef = ef.max(k).max(50);

    // Normalize the query for cosine so distances match the pre-normalized stored vectors.
    let query_buf: Vec<f32>;
    let query: &[f32] = if matches!(metric, DistanceMetric::Cosine) {
        query_buf = normalize_to_unit(query.to_vec());
        &query_buf
    } else {
        query
    };

    let Some(slots) = index.slots() else {
        return vec![];
    };
    if query.len() != slots.dim() {
        return vec![];
    }

    let mut ep_id = match *index.entry_point.read() {
        Some(id) => id,
        None => return vec![],
    };

    let mut nodes = index.nodes.read();
    let reused = index.reused.load(Acquire);

    // Handle tombstoned entry point
    if !index.is_live(ep_id) {
        let k2id = index.key_to_id.read();
        match k2id.values().next().copied() {
            Some(id) => ep_id = id,
            None => return vec![],
        }
    }

    let live_count = index.live_count.load(std::sync::atomic::Ordering::Relaxed);

    // Small graph: brute force
    if live_count < 500 {
        let mut scored: Vec<(f32, u32)> = nodes
            .iter()
            .filter(|(i, _)| slots.is_live(*i))
            .map(|(i, _)| (distance_prenorm(metric, query, slots.vector(i)), i as u32))
            .collect();
        scored.sort_unstable_by(|a, b| a.0.total_cmp(&b.0));
        return into_results(index, slots, &nodes, query, metric, scored, k, reused);
    }

    // Large graph: HNSW traversal
    let ep_level = nodes.get(ep_id).map_or(0, |n| n.level);
    let mut curr_ep = ep_id as u32;

    for lc in (1..=ep_level).rev() {
        let candidates = search_layer(index, slots, &mut nodes, query, curr_ep, 1, lc, metric);
        if let Some(&(_, best)) = candidates.first() {
            curr_ep = best;
        }
    }

    let mut results = search_layer(index, slots, &mut nodes, query, curr_ep, ef, 0, metric);

    // Fallback search from original entry point if we got too few results
    if results.len() < k && curr_ep as usize != ep_id {
        let mut more = search_layer(index, slots, &mut nodes, query, ep_id as u32, ef, 0, metric);
        results.append(&mut more);
        let mut seen: AHashMap<u32, f32> = AHashMap::new();
        for (dist, id) in results {
            seen.entry(id)
                .and_modify(|d| {
                    if dist < *d {
                        *d = dist;
                    }
                })
                .or_insert(dist);
        }
        results = seen.into_iter().map(|(id, dist)| (dist, id)).collect();
        results.sort_unstable_by(|a, b| a.0.total_cmp(&b.0));
    }

    into_results(index, slots, &nodes, query, metric, results, k, reused)
}

/// KNN over the graph, among only the nodes `admit` lets in: the search
/// walks every node, as an unfiltered one does, but only those let in are
/// kept as results, and it goes on until it holds `ef` of them or has
/// nowhere nearer to go. hnswlib filters the same way.
///
/// `query` is as the graph holds vectors: of unit length for a cosine (see
/// [`PreparedQuery::vector`]). After `budget` distances the search gives up
/// and returns `None`: a filter that lets few nodes in makes the walk long,
/// and measuring the few it lets in is then the cheaper way.
///
/// When `deadline` has passed, the search stops with the nearest of the
/// nodes let in so far, and the second value is true: a query's `TIMEOUT`
/// holds here as in a scan. `BATCHES` gives the walk no budget, and a filter
/// that lets few nodes in makes it walk the whole graph.
#[allow(clippy::too_many_arguments)]
pub fn knn_hnsw_admitting(
    index: &HnswIndex,
    query: &[f32],
    k: usize,
    ef: usize,
    metric: &DistanceMetric,
    admit: &mut dyn FnMut(&HnswNode) -> bool,
    budget: usize,
    deadline: Option<std::time::Instant>,
) -> Option<(Vec<KnnResult>, bool)> {
    use ordered_float::OrderedFloat;
    use std::cmp::Reverse;
    use std::collections::BinaryHeap;

    let ef = ef.max(k);
    let slots = index.slots()?;
    if query.len() != slots.dim() {
        return Some((Vec::new(), false));
    }
    let mut ep_id = (*index.entry_point.read())?;
    let mut nodes = index.nodes.read();
    let reused = index.reused.load(Acquire);
    if !index.is_live(ep_id) {
        ep_id = index.key_to_id.read().values().next().copied()?;
    }

    // Down to layer 1, nearest first, as an unfiltered search goes.
    let ep_level = nodes.get(ep_id).map_or(0, |n| n.level);
    let mut curr = ep_id as u32;
    for lc in (1..=ep_level).rev() {
        let found = search_layer(index, slots, &mut nodes, query, curr, 1, lc, metric);
        if let Some(&(_, best)) = found.first() {
            curr = best;
        }
    }

    // Layer 0, keeping only what is let in.
    let (first, next) = (PREFETCH_FIRST, PREFETCH_NEXT);
    let mut spent = 0usize;
    let mut visited = slots::Visited::default();
    visited.reset(nodes.count());
    let mut candidates: BinaryHeap<Reverse<(OrderedFloat<f32>, u32)>> = BinaryHeap::new();
    let mut results: BinaryHeap<(OrderedFloat<f32>, u32)> = BinaryHeap::new();
    let mut neighbors: Vec<u32> = Vec::with_capacity(64);

    if !index.is_live(curr as usize) {
        return Some((Vec::new(), false));
    }
    let d = OrderedFloat(distance_prenorm(metric, query, slots.vector(curr as usize)));
    spent += 1;
    visited.insert(curr);
    candidates.push(Reverse((d, curr)));
    if let Some(n) = nodes.get(curr as usize)
        && admit(n)
    {
        results.push((d, curr));
    }
    let mut bound = if results.len() >= ef {
        results.peek().map_or(f32::INFINITY, |(d, _)| d.0)
    } else {
        f32::INFINITY
    };

    let mut expanded = 0usize;
    let mut cut = false;
    while let Some(Reverse((d, id))) = candidates.pop() {
        if d.0 > bound && results.len() >= ef {
            break;
        }
        expanded += 1;
        // The clock every DEADLINE_EXPANSIONS nodes expanded: a few
        // thousand distances apart.
        if expanded.is_multiple_of(DEADLINE_EXPANSIONS)
            && deadline.is_some_and(|d| std::time::Instant::now() >= d)
        {
            cut = true;
            break;
        }
        if expanded.is_multiple_of(EXPANSIONS_PER_YIELD) {
            parking_lot::RwLockReadGuard::bump(&mut nodes);
        }
        neighbors.clear();
        slots.read_links(id as usize, &mut neighbors);
        let mut fresh = 0;
        for i in 0..neighbors.len() {
            let nb = neighbors[i];
            if visited.insert(nb) {
                neighbors[fresh] = nb;
                fresh += 1;
                slots.prefetch(nb as usize, first);
            }
        }
        for i in 0..fresh {
            if i + 1 < fresh {
                slots.prefetch(neighbors[i + 1] as usize, next);
            }
            let nb = neighbors[i];
            if !slots.is_live(nb as usize) {
                continue;
            }
            spent += 1;
            if spent > budget {
                return None;
            }
            let dist = distance_prenorm(metric, query, slots.vector(nb as usize));
            if results.len() < ef || dist < bound {
                candidates.push(Reverse((OrderedFloat(dist), nb)));
                if let Some(n) = nodes.get(nb as usize)
                    && admit(n)
                {
                    results.push((OrderedFloat(dist), nb));
                    if results.len() > ef {
                        results.pop();
                    }
                    if results.len() >= ef {
                        bound = results.peek().map_or(f32::INFINITY, |(d, _)| d.0);
                    }
                }
            }
        }
    }

    let mut found: Vec<(f32, u32)> = results.into_iter().map(|(d, id)| (d.0, id)).collect();
    found.sort_unstable_by(|a, b| a.0.total_cmp(&b.0));
    if index.reused.load(Acquire) != reused {
        // A slot filled again meanwhile holds another document now: it is
        // let in again or not.
        found.retain(|(_, id)| match nodes.get(*id as usize) {
            Some(n) => admit(n),
            None => false,
        });
    }
    let nearest = into_results(index, slots, &nodes, query, metric, found, k, reused);
    Some((nearest, cut))
}

/// Nodes a filtered graph search expands between reads of the clock
/// (`knn_hnsw_admitting`).
const DEADLINE_EXPANSIONS: usize = 64;

/// Called when a key is deleted. Removes it from any matching indexes.
/// For FLAT this is O(1). For HNSW we tombstone — mark deleted but
/// leave the graph structure intact (full graph surgery is expensive).
///
/// Only the indexes of database `db`: the same key in another database is
/// another document. A `DEL` in one database used to take the document of
/// the same name out of an index of another.
pub fn maybe_unindex(index_store: &IndexStore, db: usize, key: &(impl AsRef<[u8]> + ?Sized)) {
    let key = key.as_ref();
    if indexing_deferred() || index_store.none() {
        return;
    }
    let store = index_store.read();

    for (name, def) in store.iter() {
        if is_alias(name, def) || def.db_index != db || !def.covers(key) {
            continue;
        }
        // Every vector field's, its postings, and what its TEXT fields put
        // in: the words it held, from the inverted index and the BM25
        // statistics.
        unindex_from(def, &crate::key_bytes::text_of(key));
    }
}
// ── HNSW compaction ───────────────────────────────────────────────────────────
//
// Rebuilds an HNSW index from scratch, dropping all tombstoned slots.
// Runs on the dedicated compaction thread, NEVER on a server worker.
//
// Concurrency model: shadow-index with atomic Arc swap.
//   1. Read snapshot: clone the current Arc<HnswIndex>. Cheap (refcount bump).
//      Searches and inserts continue using this Arc — they are unaffected.
//   2. Rebuild: walk the snapshot's nodes, re-insert each live one into a
//      fresh HnswIndex. No locks held on the snapshot during this loop;
//      each node read takes the snapshot's RwLock briefly then releases.
//   3. Swap: take a write lock on IndexStore, replace LiveIndex::Hnsw(old_arc)
//      with LiveIndex::Hnsw(new_arc). Drop the lock immediately.
//   4. Old Arc has its refcount drop to zero once all in-flight searches
//      finish their reads. Memory is reclaimed automatically.
//
// Writes (HSET) that arrived during step 2 went into the OLD graph. After
// the swap they are visible to no-one — this is the only correctness gotcha
// of shadow-index. We mitigate by re-checking after swap: any keys present
// in the IndexStore's data hashes but missing from the new graph get
// re-inserted in a "catch-up" pass.

pub fn compact_hnsw(
    index_store: &IndexStore,
    db_store: &crate::store::Store,
    index_name: &str,
    field_name: &str,
) -> Result<usize, &'static str> {
    use std::sync::atomic::Ordering;

    // ── Step 1: snapshot the Arc<HnswIndex> for the named field ──────────
    let (old_arc, metric, dim, data_type, prefixes, m, ef_construction) = {
        let store = index_store.read();
        let def = match store.get(index_name) {
            Some(d) => d,
            None => return Err("index not found"),
        };

        let old_arc = match def.indexes.get(field_name) {
            Some(LiveIndex::Hnsw(arc)) => Arc::clone(arc),
            Some(_) => return Err("field is not HNSW"),
            None => return Err("no such vector field on index"),
        };

        if old_arc
            .compaction_in_progress
            .compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
            .is_err()
        {
            return Err("compaction already in progress");
        }

        let vf = match def.vector_field_by_name(field_name) {
            Some(f) => f,
            None => {
                old_arc
                    .compaction_in_progress
                    .store(false, Ordering::Release);
                return Err("vector field metadata missing");
            }
        };

        let (dim, data_type, metric) = match &vf.field_type {
            FieldType::Vector {
                dim,
                data_type,
                metric,
                ..
            } => (*dim, *data_type, metric.clone()),
            _ => {
                old_arc
                    .compaction_in_progress
                    .store(false, Ordering::Release);
                return Err("vector field type mismatch");
            }
        };

        let m = old_arc.m;
        let ef_construction = old_arc.ef_construction;
        let prefixes = def.prefixes.clone();
        (
            old_arc,
            metric,
            dim,
            data_type,
            prefixes,
            m,
            ef_construction,
        )
    };

    struct CompactionGuard<'a>(&'a HnswIndex);
    impl<'a> Drop for CompactionGuard<'a> {
        fn drop(&mut self) {
            self.0
                .compaction_in_progress
                .store(false, std::sync::atomic::Ordering::Release);
        }
    }
    let _guard = CompactionGuard(&old_arc);

    // ── Step 2: build the new index from live nodes only ──────────────────
    let live_pairs: Vec<(String, Vec<f32>)> = {
        let nodes = old_arc.nodes.read();
        nodes
            .iter()
            .filter_map(|(id, n)| Some((n.key.clone(), old_arc.vector(id)?.to_vec())))
            .collect()
    };

    let new_index = Arc::new(HnswIndex::new(m, ef_construction));
    for (key, vec) in &live_pairs {
        hnsw_insert(&new_index, key.clone(), vec.clone(), &metric);
    }

    // ── Step 3: swap into the named slot in `indexes` ─────────────────────
    let (doc_on, doc_schema) = {
        let mut store = index_store.write();
        let def = match store.get_mut(index_name) {
            Some(d) => d,
            None => return Err("index dropped during compaction"),
        };
        match def.indexes.get(field_name) {
            Some(LiveIndex::Hnsw(current)) => {
                if !Arc::ptr_eq(current, &old_arc) {
                    return Err("index was replaced during compaction");
                }
            }
            _ => return Err("field changed type during compaction"),
        }
        def.indexes.insert(
            field_name.to_string(),
            LiveIndex::Hnsw(Arc::clone(&new_index)),
        );
        (def.on.clone(), def.fields.clone())
    };

    // ── Step 4: catch-up pass for writes that landed in old_arc ───────────
    let mut catchup = 0usize;
    {
        let already: ahash::AHashSet<String> = {
            let kti = new_index.key_to_id.read();
            kti.keys().cloned().collect()
        };

        let db_ref = &db_store[0];
        for shard in db_ref.iter() {
            #[cfg(target_os = "linux")]
            let map = shard.read();
            #[cfg(not(target_os = "linux"))]
            let map = shard.read();

            for (key, value) in map.iter() {
                let matches =
                    prefixes.is_empty() || prefixes.iter().any(|p| key.starts_with(p.as_bytes()));
                let key = crate::key_bytes::text_of(key);
                let key: &str = &key;
                if !matches || already.contains(key) {
                    continue;
                }
                let Some(inner) = doc_fields(&doc_on, &doc_schema, value) else {
                    continue;
                };
                let raw = match inner.get(field_name) {
                    Some(b) => b,
                    None => continue,
                };
                let Some(v) = data_type.decode(raw, dim) else {
                    continue;
                };
                hnsw_insert(&new_index, key.to_string(), v, &metric);
                catchup += 1;
            }
        }
    }

    Ok(live_pairs.len() + catchup)
}

/// Spawn the dedicated compaction worker thread.
/// Receives requests on `rx` and processes them serially.
pub fn spawn_compaction_worker(
    index_store: IndexStore,
    db_store: crate::store::Store,
    rx: CompactionRx,
) {
    std::thread::Builder::new()
        .name("hnsw-compaction".into())
        .spawn(move || {
            // Serial processing: one compaction at a time across all indexes.
            // If parallel compaction is ever needed (multiple busy indexes),
            // the fix is a thread pool here — not a tokio runtime.
            while let Ok(req) = rx.recv() {
                match compact_hnsw(&index_store, &db_store, &req.index_name, &req.field_name) {
                    Ok(n) => {
                        log_info!(
                            "HNSW compaction complete for '{}' field '{}': {n} live vectors",
                            req.index_name,
                            req.field_name
                        );
                    }
                    Err(e) => {
                        log_warn!(
                            "HNSW compaction skipped for '{}' field '{}': {e}",
                            req.index_name,
                            req.field_name
                        );
                    }
                }
            }
            // Channel closed — shutdown.
        })
        .expect("failed to spawn HNSW compaction worker");
}

/// Check tombstone ratio for any indexes touched by this key delete and,
/// if they've crossed the threshold, send a compaction request.
///
/// Called immediately after `maybe_unindex` from the command handlers.
/// `try_send` never blocks — if the channel is full (32 pending requests)
/// or no receiver exists yet (during startup), we silently drop. The next
/// delete will re-check and re-queue.
pub fn maybe_request_compaction(
    index_store: &IndexStore,
    compaction_tx: &CompactionTx,
    deleted_key: &(impl AsRef<[u8]> + ?Sized),
) {
    let deleted_key = deleted_key.as_ref();
    use std::sync::atomic::Ordering::Relaxed;

    if index_store.none() {
        return;
    }
    let store = index_store.read();
    for (name, def) in store.iter() {
        if is_alias(name, def) {
            continue;
        }
        let matches = def.prefixes.is_empty()
            || def
                .prefixes
                .iter()
                .any(|p| deleted_key.starts_with(p.as_bytes()));
        if !matches {
            continue;
        }
        // Each HNSW vector field is its own graph with its own tombstones.
        // Check each one independently — one field may need compaction
        // while another is fresh.
        for (field_name, live) in def.indexes.iter() {
            let idx = match live {
                LiveIndex::Hnsw(i) => i,
                _ => continue,
            };
            let tomb = idx.tombstone_count.load(Relaxed);
            if tomb < COMPACTION_MIN_TOMBSTONES {
                continue;
            }
            if idx.tombstone_ratio() < COMPACTION_THRESHOLD {
                continue;
            }
            if idx.compaction_in_progress.load(Relaxed) {
                continue;
            }
            let _ = compaction_tx.try_send(CompactionRequest {
                index_name: name.clone(),
                field_name: field_name.clone(),
            });
        }
    }
}

#[cfg(test)]
mod prepared_query_tests {
    use super::*;

    fn vector(dim: usize, seed: u64) -> Vec<f32> {
        let mut state = seed.wrapping_mul(0x9E37_79B9_7F4A_7C15) | 1;
        (0..dim)
            .map(|_| {
                state ^= state << 13;
                state ^= state >> 7;
                state ^= state << 17;
                ((state >> 40) as f32 / (1u64 << 24) as f32) * 4.0 - 2.0
            })
            .collect()
    }

    /// The scalar code the kernels replaced.
    fn scalar_cosine(a: &[f32], b: &[f32]) -> f32 {
        let dot: f32 = a.iter().zip(b).map(|(x, y)| x * y).sum();
        let ma = a.iter().map(|x| x * x).sum::<f32>().sqrt();
        let mb = b.iter().map(|x| x * x).sum::<f32>().sqrt();
        if ma * mb == 0.0 {
            return 1.0;
        }
        1.0 - dot / (ma * mb)
    }

    #[test]
    fn a_prepared_query_measures_what_distance_does() {
        let metrics = [
            DistanceMetric::Cosine,
            DistanceMetric::L2,
            DistanceMetric::InnerProduct,
        ];
        for dim in [1usize, 3, 7, 8, 15, 16, 25, 128, 512, 1536, 2048] {
            let q = vector(dim, dim as u64);
            let s = vector(dim, 7 + dim as u64);
            for metric in &metrics {
                let prepared = PreparedQuery::new(metric, &q);
                let want = distance(metric, &q, &s);
                let tol = 1e-4 * want.abs().max(1.0) * (dim as f32).sqrt();
                let got = prepared.to_raw(&s);
                assert!(
                    (got - want).abs() <= tol,
                    "{metric:?} {dim}: {got} vs {want}"
                );
            }
            // Against a vector the graph holds: of unit length for a cosine.
            let cosine = DistanceMetric::Cosine;
            let unit = normalize_to_unit(s.clone());
            let got = PreparedQuery::new(&cosine, &q).to_unit(&unit);
            let want = scalar_cosine(&q, &s);
            assert!(
                (got - want).abs() <= 1e-5,
                "unit cosine {dim}: {got} vs {want}"
            );
            assert!(
                (dist_cosine(&q, &s) - want).abs() <= 1e-5,
                "dist_cosine {dim}"
            );
        }
    }

    #[test]
    fn a_zero_vector_is_at_cosine_distance_one() {
        let cosine = DistanceMetric::Cosine;
        let zero = vec![0.0f32; 16];
        let v = vector(16, 3);
        assert_eq!(dist_cosine(&zero, &v), 1.0);
        assert_eq!(PreparedQuery::new(&cosine, &zero).to_raw(&v), 1.0);
        assert_eq!(PreparedQuery::new(&cosine, &v).to_raw(&zero), 1.0);
        assert_eq!(
            PreparedQuery::new(&cosine, &zero).to_unit(&normalize_to_unit(v)),
            1.0
        );
    }
}

#[cfg(test)]
mod flush_index_tests {
    use super::*;

    #[test]
    fn drop_all_indexes_drops_hnsw_arc() {
        let store = new_index_store();
        let def = IndexSchema {
            name: "idx".into(),
            db_index: 0,
            on: IndexOn::Hash,
            prefixes: vec!["v:".into()],
            fields: vec![SchemaField {
                name: "vec".into(),
                field_type: FieldType::Vector {
                    algo: VectorAlgo::Hnsw {
                        m: 16,
                        ef_construction: 64,
                        ef_runtime: 10,
                    },
                    dim: 2,
                    metric: DistanceMetric::L2,
                    data_type: VectorType::Float32,
                },
                options: FieldOptions::default(),
            }],
            aliases: Vec::new(),
            stopwords: None,
            synonyms: Default::default(),
            language: Default::default(),
            default_score: 1.0,
            score_field: None,
        }
        .into_def();
        let graph = match def.indexes.get("vec") {
            Some(LiveIndex::Hnsw(arc)) => Arc::clone(arc),
            _ => panic!("expected HNSW field"),
        };
        hnsw_insert(&graph, "v:1".into(), vec![1.0, 0.0], &DistanceMetric::L2);
        store.write().insert("idx".into(), def);

        let weak = Arc::downgrade(&graph);
        drop(graph);
        assert_eq!(drop_all_indexes(&store), 1);
        assert!(store.read().is_empty());
        assert!(
            weak.upgrade().is_none(),
            "HNSW graph must be freed when FLUSHALL drops the index"
        );
    }

    #[test]
    fn create_args_carries_score_and_score_field() {
        let def = IndexSchema {
            name: "idx".into(),
            db_index: 0,
            on: IndexOn::Hash,
            prefixes: vec!["s:".into()],
            fields: vec![SchemaField {
                name: "title".into(),
                field_type: FieldType::Text,
                options: FieldOptions::default(),
            }],
            aliases: Vec::new(),
            stopwords: None,
            synonyms: Default::default(),
            language: Default::default(),
            default_score: 0.5,
            score_field: Some("rank".into()),
        }
        .into_def();
        let args: Vec<String> = def
            .create_args()
            .into_iter()
            .map(|a| String::from_utf8(a).unwrap())
            .collect();
        let at = |word: &str| args.iter().position(|a| a == word).unwrap();
        assert_eq!(args[at("SCORE") + 1], "0.5");
        assert_eq!(args[at("SCORE_FIELD") + 1], "rank");
        assert!(at("SCORE_FIELD") < at("SCHEMA"));
    }

    #[test]
    fn drop_indexes_for_db_keeps_other_dbs() {
        let store = new_index_store();
        let mk = |name: &str, db: usize| {
            IndexSchema {
                name: name.into(),
                db_index: db,
                on: IndexOn::Hash,
                prefixes: vec![],
                fields: vec![SchemaField {
                    name: "t".into(),
                    field_type: FieldType::Text,
                    options: FieldOptions::default(),
                }],
                aliases: Vec::new(),
                stopwords: None,
                synonyms: Default::default(),
                language: Default::default(),
                default_score: 1.0,
                score_field: None,
            }
            .into_def()
        };
        store.write().insert("a".into(), mk("a", 0));
        store.write().insert("b".into(), mk("b", 1));
        assert_eq!(drop_indexes_for_db(&store, 1), 1);
        let g = store.read();
        assert!(g.contains_key("a"));
        assert!(!g.contains_key("b"));
    }
}

#[cfg(test)]
mod alias_tests {
    use super::*;

    fn index(name: &str) -> IndexDef {
        IndexSchema {
            name: name.into(),
            db_index: 0,
            on: IndexOn::Hash,
            prefixes: vec!["doc:".into()],
            fields: vec![SchemaField {
                name: "t".into(),
                field_type: FieldType::Text,
                options: FieldOptions::default(),
            }],
            aliases: Vec::new(),
            stopwords: None,
            synonyms: Default::default(),
            language: Default::default(),
            default_score: 1.0,
            score_field: None,
        }
        .into_def()
    }

    #[test]
    fn an_alias_resolves_to_its_index_and_goes_with_it() {
        let mut store = AHashMap::new();
        store.insert("idx".to_string(), index("idx"));
        let def = store["idx"].clone();
        set_alias(&mut store, "b", &def);
        set_alias(&mut store, "a", &def);

        assert_eq!(resolve_index_name(&store, "a").as_deref(), Some("idx"));
        assert_eq!(resolve_index_name(&store, "idx").as_deref(), Some("idx"));
        assert_eq!(resolve_index_name(&store, "nope"), None);
        assert_eq!(index_aliases(&store, "idx"), ["a", "b"]);
        assert!(is_alias("a", &store["a"]));
        assert!(!is_alias("idx", &store["idx"]));

        // An alias is not an index: it cannot be dropped as one.
        assert!(!remove_index(&mut store, "a"));
        assert!(remove_index(&mut store, "idx"));
        assert!(store.is_empty(), "aliases go with their index");
    }

    /// A document is indexed once, however many names the index has.
    #[test]
    fn documents_are_not_indexed_through_aliases() {
        let store = new_index_store();
        store.write().insert("idx".to_string(), index("idx"));
        {
            let mut g = store.write();
            let def = g["idx"].clone();
            set_alias(&mut g, "alias", &def);
        }
        maybe_index(
            &store,
            0,
            "doc:1",
            &[("t".to_string(), Arc::from(&b"hello world"[..]))],
            Write::Fields,
        );
        let g = store.read();
        assert_eq!(g["idx"].text.read().doc_count(), 1);
        assert_eq!(g["alias"].text.read().doc_count(), 0);
    }

    #[test]
    fn aliases_are_restored_with_their_schema() {
        let store = new_index_store();
        let schema = IndexSchema {
            name: "idx".into(),
            db_index: 0,
            on: IndexOn::Hash,
            prefixes: vec![],
            fields: vec![],
            aliases: vec!["a".into()],
            stopwords: None,
            synonyms: Default::default(),
            language: Default::default(),
            default_score: 1.0,
            score_field: None,
        };
        register_index_schemas(&store, &[schema], &Default::default());
        let g = store.read();
        assert_eq!(resolve_index_name(&g, "a").as_deref(), Some("idx"));
        assert_eq!(index_aliases(&g, "idx"), ["a"]);
    }
}

#[cfg(test)]
mod concurrent_insert_tests {
    use super::*;
    use rand::{Rng, SeedableRng};

    fn random_vectors(n: usize, dim: usize, seed: u64) -> Vec<Vec<f32>> {
        let mut rng = rand::rngs::StdRng::seed_from_u64(seed);
        (0..n)
            .map(|_| (0..dim).map(|_| rng.r#gen::<f32>() - 0.5).collect())
            .collect()
    }

    /// Inserts link themselves in under their neighbours' locks alone, while
    /// searches run and keys are overwritten (which takes the write lock).
    /// The graph that comes out must be whole, and every vector findable.
    #[test]
    fn concurrent_inserts_searches_and_overwrites_leave_a_whole_graph() {
        const THREADS: usize = 8;
        const PER_THREAD: usize = 1500;
        const DIM: usize = 16;
        let metric = DistanceMetric::Cosine;
        let index = Arc::new(HnswIndex::new(16, 100));
        let vectors: Vec<Vec<Vec<f32>>> = (0..THREADS)
            .map(|t| random_vectors(PER_THREAD, DIM, t as u64))
            .collect();

        std::thread::scope(|s| {
            for (t, vs) in vectors.iter().enumerate() {
                let index = &index;
                let metric = &metric;
                s.spawn(move || {
                    for (i, v) in vs.iter().enumerate() {
                        hnsw_insert(index, format!("k:{t}:{i}"), v.clone(), metric);
                        if i % 10 == 0 {
                            knn_hnsw(index, v, 5, 50, metric);
                        }
                        // Overwrite an earlier key now and then: a tombstone
                        // under the write lock, then a fresh insert.
                        if i % 100 == 99 {
                            let j = i - 50;
                            hnsw_insert(index, format!("k:{t}:{j}"), vs[j].clone(), metric);
                        }
                    }
                });
            }
        });

        let nodes = index.nodes.read();
        let count = nodes.count();
        let overwrites = THREADS * (PER_THREAD / 100);
        // An overwrite deletes the key's node and the slot is filled again
        // by a later insert: the graph grows by fewer slots than inserts.
        let tombstones = index.tombstone_count.load(Relaxed);
        assert_eq!(index.live_count.load(Relaxed), THREADS * PER_THREAD);
        assert!(tombstones <= overwrites, "{tombstones} empty slots");
        assert_eq!(count, THREADS * PER_THREAD + tombstones);
        let mut list = Vec::new();
        for (id, node) in nodes.iter() {
            for lc in 0..node.layer_count() {
                list.clear();
                index.links(&nodes, id, lc, &mut list);
                let m_max = if lc == 0 { index.m0 } else { index.m };
                assert!(
                    list.len() <= m_max + slack(m_max),
                    "node {id} layer {lc}: {} links",
                    list.len()
                );
                for &nb in &list {
                    let nb = nb as usize;
                    assert!(nb < count && nb != id, "node {id} links to {nb}");
                }
            }
        }
        drop(nodes);

        // Every vector is found as its own nearest neighbour (nearly always:
        // HNSW is approximate).
        let mut found = 0;
        for (t, vs) in vectors.iter().enumerate() {
            for (i, v) in vs.iter().enumerate() {
                let hits = knn_hnsw(&index, v, 1, 100, &metric);
                if hits.first().is_some_and(|h| h.key == format!("k:{t}:{i}")) {
                    found += 1;
                }
            }
        }
        let total = THREADS * PER_THREAD;
        assert!(found * 100 >= total * 98, "found {found} of {total}");
    }
}

#[cfg(test)]
mod slot_reuse_tests {
    use super::*;

    fn point(i: usize) -> Vec<f32> {
        vec![i as f32, (i % 7) as f32, 1.0, 2.0]
    }

    /// A graph past the size searched by brute force.
    fn graph(n: usize) -> Arc<HnswIndex> {
        let index = Arc::new(HnswIndex::new(8, 64));
        for i in 0..n {
            hnsw_insert(&index, format!("k{i}"), point(i), &DistanceMetric::L2);
        }
        index
    }

    #[test]
    fn a_deleted_nodes_slot_is_filled_by_the_next_insert() {
        let metric = DistanceMetric::L2;
        let index = graph(600);
        let live = LiveIndex::Hnsw(Arc::clone(&index));
        unindex_vector(&live, "k10");
        assert_eq!(index.tombstone_count.load(Relaxed), 1);
        assert_eq!(index.live_count.load(Relaxed), 599);
        let slots_before = index.nodes.read().count();

        hnsw_insert(&index, "new".into(), vec![10.25, 3.0, 1.0, 2.0], &metric);
        assert_eq!(index.nodes.read().count(), slots_before, "no new slot");
        assert_eq!(index.tombstone_count.load(Relaxed), 0);
        assert_eq!(index.live_count.load(Relaxed), 600);
        assert_eq!(index.key_to_id.read().get("new"), Some(&10));

        let hits = knn_hnsw(&index, &[10.25, 3.0, 1.0, 2.0], 1, 64, &metric);
        assert_eq!(hits[0].key, "new");
        assert!(hits[0].score < 1e-6);
        let all = knn_hnsw(&index, &point(10), 600, 600, &metric);
        assert!(
            all.iter().all(|h| h.key != "k10"),
            "the deleted key is gone"
        );
        // Every result is measured against its own vector.
        for h in &all {
            let i: usize = match h.key.strip_prefix('k') {
                Some(i) => i.parse().unwrap(),
                None => continue,
            };
            let want = dist_l2_sq(&point(10), &point(i));
            assert!(
                (h.score - want).abs() < 1e-3,
                "{}: {} vs {want}",
                h.key,
                h.score
            );
        }
    }

    #[test]
    fn an_overwritten_key_keeps_its_slot() {
        let metric = DistanceMetric::L2;
        let index = graph(600);
        let slots_before = index.nodes.read().count();
        for round in 0..3 {
            hnsw_insert(
                &index,
                "k5".into(),
                vec![1000.0 + round as f32, 0.0, 0.0, 0.0],
                &metric,
            );
        }
        assert_eq!(index.nodes.read().count(), slots_before);
        assert_eq!(index.live_count.load(Relaxed), 600);
        assert_eq!(index.tombstone_count.load(Relaxed), 0);
        let hits = knn_hnsw(&index, &[1002.0, 0.0, 0.0, 0.0], 1, 64, &metric);
        assert_eq!(hits[0].key, "k5");
        assert!(hits[0].score < 1e-3);
    }

    #[test]
    fn a_restored_graph_fills_its_deleted_slots_first() {
        let metric = DistanceMetric::L2;
        let node = |key: &str, v: [f32; 2], links: Vec<usize>| {
            Some(RestoredNode {
                key: key.into(),
                vector: v.to_vec(),
                neighbors: vec![links],
            })
        };
        // The entry point saved is a deleted slot; a link points past the
        // graph.
        let nodes = vec![
            node("a", [1.0, 0.0], vec![2, 9]),
            None,
            node("c", [0.0, 1.0], vec![0]),
        ];
        let index = Arc::new(HnswIndex::restore(8, 64, nodes, Some(1)));
        assert_eq!(index.live_count.load(Relaxed), 2);
        assert_eq!(index.tombstone_count.load(Relaxed), 1);
        let ep = index.entry_point.read().unwrap();
        assert!(index.is_live(ep));
        let mut links = Vec::new();
        index.links(&index.nodes.read(), 0, 0, &mut links);
        assert_eq!(links, [2]);

        hnsw_insert(&index, "b".into(), vec![1.0, 1.0], &metric);
        assert_eq!(index.nodes.read().count(), 3);
        assert_eq!(index.key_to_id.read().get("b"), Some(&1));
        let hits = knn_hnsw(&index, &[1.0, 1.0], 3, 50, &metric);
        assert_eq!(hits[0].key, "b");
        assert_eq!(hits.len(), 3);
    }

    #[test]
    fn deletes_and_inserts_racing_searches_leave_every_answer_right() {
        let metric = DistanceMetric::L2;
        let index = graph(1000);
        std::thread::scope(|s| {
            for t in 0..4 {
                let index = &index;
                let metric = &metric;
                s.spawn(move || {
                    for r in 0..200 {
                        let i = (t * 250 + r) % 1000;
                        let live = LiveIndex::Hnsw(Arc::clone(index));
                        unindex_vector(&live, &format!("k{i}"));
                        hnsw_insert(index, format!("k{i}"), point(i), metric);
                    }
                });
            }
            for _ in 0..2 {
                let index = &index;
                let metric = &metric;
                s.spawn(move || {
                    for r in 0..300 {
                        let q = point(r * 3 % 1000);
                        for h in knn_hnsw(index, &q, 10, 64, metric) {
                            let i: usize = h.key[1..].parse().unwrap();
                            let want = dist_l2_sq(&q, &point(i));
                            assert!((h.score - want).abs() < 1e-3, "{}: {}", h.key, h.score);
                        }
                    }
                });
            }
        });
        assert_eq!(index.live_count.load(Relaxed), 1000);
        assert_eq!(
            index.nodes.read().count(),
            1000 + index.tombstone_count.load(Relaxed)
        );
    }
}