vantadb 0.1.4

VantaDB: An embedded persistent memory and vector retrieval engine for local-first AI applications.
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
use dashmap::DashMap;
use memmap2::MmapMut;
use rand::{Rng, SeedableRng};
use serde::{Deserialize, Serialize};
use std::collections::BinaryHeap;
use std::fs::{File, OpenOptions};
use std::hash::BuildHasherDefault;
use std::io::{BufWriter, Write};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use tracing::{info, warn};
use twox_hash::XxHash64;

const ENTRY_POINT_NONE: u64 = u64::MAX;

pub use crate::node::{DistanceMetric, SendPtr, VectorRepresentations};
use crate::vector::quantization::{rabitq_similarity, turbo_quant_similarity};

/// SCALE-01: Prefetching Predictivo del Kernel para búsqueda HNSW MMap.
///
/// Emite una sugerencia asíncrona al OS para pre-cargar páginas físicas del vector
/// de un nodo candidato *antes* de que la CPU las calcule. Esto oculta la latencia
/// de page fault detrás del cálculo de distancia del nodo actual.
///
/// La función es no-bloqueante y best-effort: si falla (permisos, no soportado),
/// la búsqueda continúa correctamente sin degradación.
///
/// # Safety
/// El puntero `mmap_ptr` debe ser válido durante la duración de la llamada.
/// El rango `[offset, offset+len)` debe estar dentro del mmap mapeado.
#[inline(always)]
#[allow(unused_variables)]
fn prefetch_mmap_vector(mmap_ptr: *const u8, offset: usize, len: usize) {
    #[cfg(unix)]
    {
        // POSIX: madvise(MADV_WILLNEED) — solicita al kernel cargar páginas de forma asíncrona.
        // Disponible en Linux y macOS. No bloquea el hilo llamante.
        unsafe {
            // SAFETY: mmap_ptr es un puntero activo al mmap; offset+len está validado por
            // el caller. madvise falla silenciosamente si el rango es inválido.
            libc::madvise(
                mmap_ptr.add(offset) as *mut libc::c_void,
                len,
                libc::MADV_WILLNEED,
            );
        }
    }

    #[cfg(windows)]
    {
        // Windows: PrefetchVirtualMemory — equivalente a MADV_WILLNEED.
        // Disponible desde Windows 8 / Server 2012. Falla silenciosamente en versiones anteriores.
        use windows_sys::Win32::System::Memory::{PrefetchVirtualMemory, WIN32_MEMORY_RANGE_ENTRY};
        use windows_sys::Win32::System::Threading::GetCurrentProcess;
        unsafe {
            // SAFETY: mismas garantías que el caso Unix.
            let addr = mmap_ptr.add(offset) as *mut core::ffi::c_void;
            let entry = WIN32_MEMORY_RANGE_ENTRY {
                VirtualAddress: addr,
                NumberOfBytes: len,
            };
            let process_handle = GetCurrentProcess();
            // La firma acepta *const WIN32_MEMORY_RANGE_ENTRY y Flags=0 (requerido por Win32)
            PrefetchVirtualMemory(process_handle, 1, std::ptr::addr_of!(entry), 0);
        }
    }

    // Fallback no-op para plataformas sin soporte (e.g., WASM, Tier-3).
    // El compilador elimina este bloque vacío en release.
    #[cfg(not(any(unix, windows)))]
    let _ = (mmap_ptr, offset, len);
}

/// Libera páginas de memoria del mmap para nodos fríos (Cold tier).
/// Usa madvise(MADV_DONTNEED) para indicar al kernel que estas páginas
/// pueden ser liberadas de RAM, reduciendo el RSS sin invalidar el mmap.
///
/// El rango `[offset, offset+len)` debe estar dentro del mmap mapeado.
///
/// # Safety
///
/// - `mmap_ptr` must be a valid pointer to an active memory-mapped region.
/// - `offset + len` must not exceed the length of the mapped region.
/// - The caller must hold a read guard (or equivalent) ensuring the mmap
///   is not unmapped for the duration of this call.
#[inline(always)]
#[allow(unused_variables)]
pub unsafe fn release_mmap_vector(mmap_ptr: *const u8, offset: usize, len: usize) {
    #[cfg(unix)]
    {
        // POSIX: madvise(MADV_DONTNEED) — indica al kernel que estas páginas
        // no son necesarias y pueden ser liberadas de RAM. El mmap sigue válido,
        // pero las páginas se cargarán bajo demanda desde disco cuando se accedan.
        // Disponible en Linux y macOS. No bloquea el hilo llamante.
        unsafe {
            // SAFETY: mmap_ptr es un puntero activo al mmap; offset+len está validado por
            // el caller. madvise falla silenciosamente si el rango es inválido.
            libc::madvise(
                mmap_ptr.add(offset) as *mut libc::c_void,
                len,
                libc::MADV_DONTNEED,
            );
        }
    }

    #[cfg(windows)]
    {
        // Windows: No hay equivalente directo a MADV_DONTNEED.
        // VirtualUnlock puede liberar páginas del working set, pero requiere
        // que las páginas estén bloqueadas primero. Para simplicidad, no-op.
        let _ = (mmap_ptr, offset, len);
    }

    // Fallback no-op para plataformas sin soporte (e.g., WASM, Tier-3).
    // El compilador elimina este bloque vacío en release.
    #[cfg(not(any(unix, windows)))]
    let _ = (mmap_ptr, offset, len);
}

use crate::config::PrefetchMode;
use std::sync::OnceLock;

/// Global prefetch mode — initialized once from `VantaConfig::prefetch_mode`
/// or falls back to the env var `VANTA_PREFETCH` / `VANTA_DISABLE_PREFETCH`.
static PREFETCH_MODE: OnceLock<PrefetchMode> = OnceLock::new();

/// Override the global prefetch mode at runtime (called during initialization).
pub fn set_prefetch_mode(mode: PrefetchMode) {
    let _ = PREFETCH_MODE.set(mode);
}

#[inline(always)]
fn should_prefetch() -> bool {
    if let Some(mode) = PREFETCH_MODE.get() {
        return mode.is_prefetch_enabled();
    }
    // Fallback: if config never initialized, read env var
    let mode = std::env::var("VANTA_PREFETCH")
        .ok()
        .map(|v| PrefetchMode::from_env_value(&v));
    let disabled = std::env::var("VANTA_DISABLE_PREFETCH")
        .ok()
        .map(|v| v == "1" || v == "true")
        .unwrap_or(false);
    match (mode, disabled) {
        (Some(m), _) => m.is_prefetch_enabled(),
        (_, true) => false,
        _ => true,
    }
}

const VECTOR_INDEX_VERSION: u16 = 4; // Upgraded for zero-copy aligned vector paging

#[inline(always)]
fn f32_dot_and_norm_b_sq(a: &[f32], b: &[f32]) -> (f32, f32) {
    if a.len() != b.len() || a.is_empty() {
        return (0.0, 0.0);
    }
    use wide::f32x8;
    let mut dot_v = f32x8::ZERO;
    let mut norm_b_v = f32x8::ZERO;
    let chunks_a = a.chunks_exact(8);
    let chunks_b = b.chunks_exact(8);
    let rem_a = chunks_a.remainder();
    let rem_b = chunks_b.remainder();
    for (a_chunk, b_chunk) in chunks_a.zip(chunks_b) {
        let va = f32x8::from(
            *<&[f32; 8]>::try_from(a_chunk).expect("chunks_exact(8) yields 8-element chunks"),
        );
        let vb = f32x8::from(
            *<&[f32; 8]>::try_from(b_chunk).expect("chunks_exact(8) yields 8-element chunks"),
        );
        dot_v += va * vb;
        norm_b_v += vb * vb;
    }
    let mut dot = dot_v.reduce_add();
    let mut norm_b = norm_b_v.reduce_add();
    for i in 0..rem_a.len() {
        dot += rem_a[i] * rem_b[i];
        norm_b += rem_b[i] * rem_b[i];
    }
    (dot, norm_b)
}

/// Pure dot product — no norm computation. ~2x faster than f32_dot_and_norm_b_sq
/// when norms are already cached.
#[inline(always)]
fn f32_dot_product(a: &[f32], b: &[f32]) -> f32 {
    if a.len() != b.len() || a.is_empty() {
        return 0.0;
    }
    use wide::f32x8;
    let mut dot_v = f32x8::ZERO;
    let chunks_a = a.chunks_exact(8);
    let chunks_b = b.chunks_exact(8);
    let rem_a = chunks_a.remainder();
    let rem_b = chunks_b.remainder();
    for (a_chunk, b_chunk) in chunks_a.zip(chunks_b) {
        let va = f32x8::from(
            *<&[f32; 8]>::try_from(a_chunk).expect("chunks_exact(8) yields 8-element chunks"),
        );
        let vb = f32x8::from(
            *<&[f32; 8]>::try_from(b_chunk).expect("chunks_exact(8) yields 8-element chunks"),
        );
        dot_v += va * vb;
    }
    let mut dot = dot_v.reduce_add();
    for i in 0..rem_a.len() {
        dot += rem_a[i] * rem_b[i];
    }
    dot
}

#[inline(always)]
pub fn f32_l2_norm(v: &[f32]) -> f32 {
    if v.is_empty() {
        return 0.0;
    }
    let (_, norm_sq) = f32_dot_and_norm_b_sq(v, v);
    norm_sq.sqrt()
}

/// Cosine similarity when BOTH inverse norms are pre-cached. Uses pure dot product and multiplications only.
/// This is the fastest path — eliminates 100% of division and ~50% of SIMD work.
#[inline(always)]
pub fn cosine_sim_cached_norms(a: &[f32], inv_norm_a: f32, b: &[f32], inv_norm_b: f32) -> f32 {
    if inv_norm_a < f32::EPSILON || inv_norm_b < f32::EPSILON || a.len() != b.len() || a.is_empty()
    {
        return 0.0;
    }
    let dot = f32_dot_product(a, b);
    dot * inv_norm_a * inv_norm_b
}

/// Cosine similarity when `||query||` was already computed for the search hot path.
#[inline(always)]
pub fn cosine_sim_with_query_norm(query: &[f32], query_norm: f32, b: &[f32]) -> f32 {
    if query_norm < f32::EPSILON || query.len() != b.len() || query.is_empty() {
        return 0.0;
    }
    let (dot, norm_b_sq) = f32_dot_and_norm_b_sq(query, b);
    let norm_b = norm_b_sq.sqrt();
    if norm_b < f32::EPSILON {
        0.0
    } else {
        dot / (query_norm * norm_b)
    }
}

#[inline(always)]
pub fn cosine_sim_f32(a: &[f32], b: &[f32]) -> f32 {
    if a.len() != b.len() || a.is_empty() {
        return 0.0;
    }
    let norm_a = f32_l2_norm(a);
    cosine_sim_with_query_norm(a, norm_a, b)
}

#[inline(always)]
pub fn euclidean_distance_squared_f32(a: &[f32], b: &[f32]) -> f32 {
    if a.len() != b.len() || a.is_empty() {
        return 0.0;
    }
    use wide::f32x8;
    let mut sum_v = f32x8::ZERO;
    let chunks_a = a.chunks_exact(8);
    let chunks_b = b.chunks_exact(8);
    let rem_a = chunks_a.remainder();
    let rem_b = chunks_b.remainder();
    for (a_chunk, b_chunk) in chunks_a.zip(chunks_b) {
        let va = f32x8::from(
            *<&[f32; 8]>::try_from(a_chunk).expect("chunks_exact(8) yields 8-element chunks"),
        );
        let vb = f32x8::from(
            *<&[f32; 8]>::try_from(b_chunk).expect("chunks_exact(8) yields 8-element chunks"),
        );
        let diff = va - vb;
        sum_v += diff * diff;
    }
    let mut sum = sum_v.reduce_add();
    for i in 0..rem_a.len() {
        let diff = rem_a[i] - rem_b[i];
        sum += diff * diff;
    }
    sum
}

/// Compute similarity against a raw query when SQ8 is the only available
/// representation for the stored node. Decodes on the fly.
fn sq8_similarity_fallback(
    raw_query: &[f32],
    sq8_data: &[i8],
    sq8_scale: f32,
    metric: DistanceMetric,
    _query_norm: Option<f32>,
) -> f32 {
    let inv_scale = sq8_scale / 127.0;
    match metric {
        DistanceMetric::Cosine => {
            let mut dot = 0.0_f32;
            let mut norm_q = 0.0_f32;
            for (&q, &s) in raw_query.iter().zip(sq8_data.iter()) {
                let decoded = (s as f32) * inv_scale;
                dot += q * decoded;
                norm_q += q * q;
            }
            let norm_sq = sq8_data.iter().fold(0.0_f32, |acc, &s| {
                let d = (s as f32) * inv_scale;
                acc + d * d
            });
            if norm_q <= f32::EPSILON || norm_sq <= f32::EPSILON {
                return 0.0;
            }
            dot / (norm_q.sqrt() * norm_sq.sqrt())
        }
        DistanceMetric::Euclidean => {
            let mut sum_sq = 0.0_f32;
            for (&q, &s) in raw_query.iter().zip(sq8_data.iter()) {
                let diff = q - (s as f32) * inv_scale;
                sum_sq += diff * diff;
            }
            -sum_sq
        }
    }
}

pub fn calculate_similarity(
    raw_query: &[f32],
    query_norm: Option<f32>,
    quantized_query_1bit: Option<&[u64]>,
    quantized_query_3bit: Option<(&[u8], f32)>,
    node_vec: &VectorRepresentations,
    metric: DistanceMetric,
) -> f32 {
    match node_vec {
        VectorRepresentations::Binary(b) => {
            if let Some(q1) = quantized_query_1bit {
                rabitq_similarity(q1, b)
            } else {
                0.0
            }
        }
        VectorRepresentations::Turbo(t) => {
            if let Some((q3, max_abs)) = quantized_query_3bit {
                turbo_quant_similarity(q3, max_abs, t, 1.0)
            } else {
                0.0
            }
        }
        VectorRepresentations::SQ8(data, scale) => {
            // Decode SQ8 on the fly and compute similarity
            sq8_similarity_fallback(raw_query, data, *scale, metric, query_norm)
        }
        VectorRepresentations::Full(f) => match metric {
            DistanceMetric::Cosine => match query_norm {
                Some(norm) => cosine_sim_with_query_norm(raw_query, norm, f),
                None => cosine_sim_f32(raw_query, f),
            },
            DistanceMetric::Euclidean => -euclidean_distance_squared_f32(raw_query, f),
        },
        VectorRepresentations::MmapFull(ptr, len) => {
            let slice = unsafe { std::slice::from_raw_parts(ptr.0, *len) };
            match metric {
                DistanceMetric::Cosine => match query_norm {
                    Some(norm) => cosine_sim_with_query_norm(raw_query, norm, slice),
                    None => cosine_sim_f32(raw_query, slice),
                },
                DistanceMetric::Euclidean => -euclidean_distance_squared_f32(raw_query, slice),
            }
        }
        VectorRepresentations::None => 0.0,
    }
}

#[inline(always)]
fn f32_slice_similarity(
    query_vec: &[f32],
    query_norm: Option<f32>,
    candidate: &[f32],
    metric: DistanceMetric,
) -> f32 {
    match metric {
        DistanceMetric::Cosine => match query_norm {
            Some(norm) => cosine_sim_with_query_norm(query_vec, norm, candidate),
            None => cosine_sim_f32(query_vec, candidate),
        },
        DistanceMetric::Euclidean => -euclidean_distance_squared_f32(query_vec, candidate),
    }
}

pub struct HnswNode {
    pub id: u64,
    pub bitset: u128,
    pub vec_data: VectorRepresentations,
    pub neighbors: Vec<Vec<u64>>,
    /// Offset into the VantaFile (Phase 3)
    pub storage_offset: u64,
    /// Pre-computed inverse L2 norm (1.0 / L2_norm) for Cosine fast-path. 0.0 = not cached.
    pub inv_cached_norm: f32,
}

#[derive(Debug)]
pub enum IndexBackend {
    InMemory,
    MMapFile {
        path: PathBuf,
        mmap: Option<MmapMut>,
    },
}

impl IndexBackend {
    pub fn new_mmap(path: PathBuf) -> Self {
        IndexBackend::MMapFile { path, mmap: None }
    }

    pub fn is_mmap(&self) -> bool {
        matches!(self, IndexBackend::MMapFile { .. })
    }

    pub fn mmap_path(&self) -> Option<&Path> {
        match self {
            IndexBackend::MMapFile { path, .. } => Some(path.as_path()),
            IndexBackend::InMemory => None,
        }
    }

    /// Returns the number of resident (physically-in-RAM) bytes for the HNSW index mmap.
    /// Uses the live mmap pointer directly when available (zero syscall overhead for address resolution).
    /// Falls back to opening the file and creating a temporary read-only Mmap if the mutable mmap is not loaded.
    pub fn mmap_resident_bytes(&self) -> Option<u64> {
        match self {
            IndexBackend::MMapFile { mmap: Some(m), .. } => {
                crate::storage::get_resident_bytes(m.as_ptr(), m.len())
            }
            IndexBackend::MMapFile { path, mmap: None } => {
                // Fallback: open the file and create a temporary read-only mmap
                let file = File::open(path).ok()?;
                let mmap = unsafe { memmap2::Mmap::map(&file).ok()? };
                crate::storage::get_resident_bytes(mmap.as_ptr(), mmap.len())
            }
            IndexBackend::InMemory => None,
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HnswConfig {
    pub m: usize,
    pub m_max0: usize,
    pub ef_construction: usize,
    pub ef_search: usize,
    pub ml: f64,
    #[serde(default)]
    pub distance_metric: DistanceMetric,
}

impl Default for HnswConfig {
    fn default() -> Self {
        Self {
            m: 32,
            m_max0: 64,
            ef_construction: 200,
            ef_search: 100,
            ml: 1.0 / (32_f64).ln(),
            distance_metric: DistanceMetric::Cosine,
        }
    }
}

// Custom wrapper to store (similarity, node_id) in BinaryHeap (Max-Heap)
#[derive(Clone, PartialEq, Debug)]
struct NodeSim(f32, u64);

impl Eq for NodeSim {}

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

impl Ord for NodeSim {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        match self
            .0
            .partial_cmp(&other.0)
            .unwrap_or(std::cmp::Ordering::Equal)
        {
            std::cmp::Ordering::Equal => other.1.cmp(&self.1),
            cmp => cmp,
        }
    }
}

// Wrapper for Min-Heap (used to track closest in result set)
#[derive(Clone, PartialEq, Debug)]
struct NodeSimMin(f32, u64);

impl Eq for NodeSimMin {}

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

impl Ord for NodeSimMin {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        match other
            .0
            .partial_cmp(&self.0)
            .unwrap_or(std::cmp::Ordering::Equal)
        {
            std::cmp::Ordering::Equal => self.1.cmp(&other.1),
            cmp => cmp,
        }
    }
}

pub struct CPIndex {
    pub nodes: DashMap<u64, HnswNode, BuildHasherDefault<XxHash64>>,
    pub max_layer: AtomicUsize,
    pub entry_point: AtomicU64,
    pub backend: IndexBackend,
    pub config: HnswConfig,
    rng: parking_lot::Mutex<rand::rngs::StdRng>,
}

impl CPIndex {
    pub fn new() -> Self {
        Self {
            nodes: Default::default(),
            max_layer: AtomicUsize::new(0),
            entry_point: AtomicU64::new(ENTRY_POINT_NONE),
            backend: IndexBackend::InMemory,
            config: HnswConfig::default(),
            rng: parking_lot::Mutex::new(rand::rngs::StdRng::seed_from_u64(42)),
        }
    }

    pub fn new_with_config(config: HnswConfig) -> Self {
        Self {
            nodes: Default::default(),
            max_layer: AtomicUsize::new(0),
            entry_point: AtomicU64::new(ENTRY_POINT_NONE),
            backend: IndexBackend::InMemory,
            config,
            rng: parking_lot::Mutex::new(rand::rngs::StdRng::seed_from_u64(42)),
        }
    }

    pub fn with_backend(backend: IndexBackend) -> Self {
        Self {
            nodes: Default::default(),
            max_layer: AtomicUsize::new(0),
            entry_point: AtomicU64::new(ENTRY_POINT_NONE),
            backend,
            config: HnswConfig::default(),
            rng: parking_lot::Mutex::new(rand::rngs::StdRng::seed_from_u64(42)),
        }
    }

    pub fn estimate_memory_bytes(&self) -> usize {
        let mut total = 0usize;
        for r in self.nodes.iter() {
            let node = r.value();
            match &node.vec_data {
                VectorRepresentations::Full(v) => total += v.len() * std::mem::size_of::<f32>(),
                VectorRepresentations::MmapFull(_, _) => {} // Zero heap allocations for mapped memory
                VectorRepresentations::Binary(b) => total += b.len() * std::mem::size_of::<u64>(),
                VectorRepresentations::Turbo(t) => total += t.len(),
                VectorRepresentations::SQ8(d, _) => total += d.len() + 4,
                VectorRepresentations::None => {}
            }
            for layer in &node.neighbors {
                total += layer.len() * std::mem::size_of::<u64>() + std::mem::size_of::<Vec<u64>>();
            }
            total += std::mem::size_of::<HnswNode>();
        }
        total += self.nodes.len() * 60;
        total
    }

    fn random_layer(&self) -> usize {
        let mut rng = self.rng.lock();
        let r: f64 = rng.random_range(0.0001..1.0);
        (-r.ln() * self.config.ml).floor() as usize
    }

    /// Thread-safe accessor for the current entry point.
    #[inline]
    pub fn get_entry_point(&self) -> Option<u64> {
        let ep = self.entry_point.load(Ordering::Acquire);
        if ep == ENTRY_POINT_NONE {
            None
        } else {
            Some(ep)
        }
    }

    /// Thread-safe setter. Uses Release ordering to ensure the node
    /// is fully visible in DashMap before other threads follow this pointer.
    #[inline]
    pub fn set_entry_point(&self, id: u64) {
        self.entry_point.store(id, Ordering::Release);
    }

    #[inline(always)]
    fn fast_similarity(
        &self,
        query_vec: &[f32],
        query_norm: Option<f32>,
        query_inv_norm: Option<f32>,
        node: &HnswNode,
        metric: DistanceMetric,
    ) -> f32 {
        match metric {
            DistanceMetric::Cosine => {
                if let Some(q_inv_norm) = query_inv_norm {
                    let node_inv_norm = node.inv_cached_norm;
                    if node_inv_norm > f32::EPSILON {
                        if let Some(node_slice) = node.vec_data.as_f32_slice() {
                            return cosine_sim_cached_norms(
                                query_vec,
                                q_inv_norm,
                                node_slice,
                                node_inv_norm,
                            );
                        }
                    }
                }
                calculate_similarity(query_vec, query_norm, None, None, &node.vec_data, metric)
            }
            DistanceMetric::Euclidean => {
                if let Some(node_slice) = node.vec_data.as_f32_slice() {
                    -euclidean_distance_squared_f32(query_vec, node_slice)
                } else {
                    calculate_similarity(query_vec, query_norm, None, None, &node.vec_data, metric)
                }
            }
        }
    }

    /// Primary search subroutine for HNSW.
    /// Performs a greedy beam search to return the `ef` nearest neighbors
    /// found at `layer`. Candidates are validated against `query_mask`.
    #[allow(clippy::too_many_arguments)]
    fn search_layer(
        &self,
        query_vec: &[f32],
        query_norm: Option<f32>,
        query_inv_norm: Option<f32>,
        entry_points: &[u64],
        ef: usize,
        layer: usize,
        query_mask: u128,
        vector_store: Option<&crate::storage::VantaFile>,
        metric: DistanceMetric,
    ) -> BinaryHeap<NodeSimMin> {
        let mut visited = std::collections::HashSet::with_capacity_and_hasher(
            ef * 2,
            BuildHasherDefault::<XxHash64>::default(),
        );
        let mut candidates = BinaryHeap::new(); // Max-heap: candidates to visit
        let mut results = BinaryHeap::new(); // Min-heap: best `ef` bounds

        for &ep in entry_points {
            if let Some(node) = self.nodes.get(&ep) {
                let d = if let Some(vs) = vector_store {
                    // Zero-copy search from VantaFile
                    if let Some(header) = vs.read_header(node.storage_offset) {
                        let vec_start = header.vector_offset as usize;
                        let vec_end = vec_start + (header.vector_len as usize * 4);
                        if vec_end > vs.mmap_bytes().len() {
                            0.0
                        } else {
                            let vec_data = &vs.mmap_bytes()[vec_start..vec_end];
                            // Safety: we trust the header.vector_len and bounds checking above
                            let f32_vec: &[f32] = unsafe {
                                std::slice::from_raw_parts(
                                    vec_data.as_ptr() as *const f32,
                                    header.vector_len as usize,
                                )
                            };
                            match metric {
                                DistanceMetric::Cosine => {
                                    if let Some(q_inv_norm) = query_inv_norm {
                                        let node_inv_norm = node.inv_cached_norm;
                                        if node_inv_norm > f32::EPSILON {
                                            cosine_sim_cached_norms(
                                                query_vec,
                                                q_inv_norm,
                                                f32_vec,
                                                node_inv_norm,
                                            )
                                        } else {
                                            f32_slice_similarity(
                                                query_vec, query_norm, f32_vec, metric,
                                            )
                                        }
                                    } else {
                                        f32_slice_similarity(query_vec, query_norm, f32_vec, metric)
                                    }
                                }
                                DistanceMetric::Euclidean => {
                                    -euclidean_distance_squared_f32(query_vec, f32_vec)
                                }
                            }
                        }
                    } else {
                        0.0
                    }
                } else {
                    self.fast_similarity(query_vec, query_norm, query_inv_norm, &node, metric)
                };

                candidates.push(NodeSim(d, ep));

                let eligible = if let Some(vs) = vector_store {
                    vs.read_header(node.storage_offset)
                        .map(|h| (h.flags & 0x8) == 0)
                        .unwrap_or(false)
                } else {
                    true
                };
                if eligible && (query_mask == u128::MAX || (node.bitset & query_mask) == query_mask)
                {
                    results.push(NodeSimMin(d, ep));
                }
                visited.insert(ep);
            }
        }

        while let Some(NodeSim(d_cand, cand_id)) = candidates.pop() {
            // Early stopping condition: if candidate is worse than the worst result
            if results.len() >= ef {
                if let Some(worst) = results.peek() {
                    // Because it's a min-heap, peek gives the smallest similarity (worst match)
                    if d_cand < worst.0 {
                        break;
                    }
                }
            }

            let neighbors = if let Some(node) = self.nodes.get(&cand_id) {
                if layer < node.neighbors.len() {
                    Some(node.neighbors[layer].clone())
                } else {
                    None
                }
            } else {
                None
            };

            if let Some(neighbors_list) = neighbors {
                // SCALE-01: Prefetch predictivo — emitimos sugerencias de pre-carga para
                // TODOS los vecinos del candidato actual antes de calcular cualquier distancia.
                // Esto permite al kernel (y al controlador de SSD) iniciar DMA de las páginas
                // físicas en paralelo mientras la CPU calcula la distancia del nodo actual.
                if should_prefetch() {
                    if let Some(vs) = vector_store {
                        let mmap_base = vs.mmap_bytes().as_ptr();
                        let mmap_len = vs.mmap_bytes().len();
                        for &pf_neighbor_id in &neighbors_list {
                            if !visited.contains(&pf_neighbor_id) {
                                if let Some(pf_node) = self.nodes.get(&pf_neighbor_id) {
                                    if let Some(h) = vs.read_header(pf_node.storage_offset) {
                                        let vec_start = h.vector_offset as usize;
                                        let vec_len_bytes = h.vector_len as usize * 4;
                                        // Validar bounds antes de emitir prefetch
                                        if vec_start + vec_len_bytes <= mmap_len
                                            && vec_len_bytes > 0
                                        {
                                            prefetch_mmap_vector(
                                                mmap_base,
                                                vec_start,
                                                vec_len_bytes,
                                            );
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                for &neighbor_id in &neighbors_list {
                    if !visited.contains(&neighbor_id) {
                        visited.insert(neighbor_id);

                        if let Some(neighbor) = self.nodes.get(&neighbor_id) {
                            let d = if let Some(vs) = vector_store {
                                if let Some(h) = vs.read_header(neighbor.storage_offset) {
                                    let vec_start = h.vector_offset as usize;
                                    let vec_end = vec_start + (h.vector_len as usize * 4);
                                    if vec_end > vs.mmap_bytes().len() {
                                        0.0
                                    } else {
                                        let v_data = &vs.mmap_bytes()[vec_start..vec_end];
                                        // Safety: trusted bounds and aligned data
                                        let f32_v: &[f32] = unsafe {
                                            std::slice::from_raw_parts(
                                                v_data.as_ptr() as *const f32,
                                                h.vector_len as usize,
                                            )
                                        };
                                        match metric {
                                            DistanceMetric::Cosine => {
                                                if let Some(q_inv_norm) = query_inv_norm {
                                                    let neighbor_inv_norm =
                                                        neighbor.inv_cached_norm;
                                                    if neighbor_inv_norm > f32::EPSILON {
                                                        cosine_sim_cached_norms(
                                                            query_vec,
                                                            q_inv_norm,
                                                            f32_v,
                                                            neighbor_inv_norm,
                                                        )
                                                    } else {
                                                        f32_slice_similarity(
                                                            query_vec, query_norm, f32_v, metric,
                                                        )
                                                    }
                                                } else {
                                                    f32_slice_similarity(
                                                        query_vec, query_norm, f32_v, metric,
                                                    )
                                                }
                                            }
                                            DistanceMetric::Euclidean => {
                                                -euclidean_distance_squared_f32(query_vec, f32_v)
                                            }
                                        }
                                    }
                                } else {
                                    0.0
                                }
                            } else {
                                self.fast_similarity(
                                    query_vec,
                                    query_norm,
                                    query_inv_norm,
                                    &neighbor,
                                    metric,
                                )
                            };

                            if results.len() < ef || results.peek().is_some_and(|worst| d > worst.0)
                            {
                                candidates.push(NodeSim(d, neighbor_id));

                                let eligible = if let Some(vs) = vector_store {
                                    vs.read_header(neighbor.storage_offset)
                                        .map(|h| (h.flags & 0x8) == 0)
                                        .unwrap_or(false)
                                } else {
                                    true
                                };
                                if eligible
                                    && (query_mask == u128::MAX
                                        || (neighbor.bitset & query_mask) == query_mask)
                                {
                                    results.push(NodeSimMin(d, neighbor_id));
                                    if results.len() > ef {
                                        results.pop(); // Remove the worst to keep size at `ef`
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        results
    }

    /// Select neighbors using the HNSW paper heuristic (Algorithm 4, Malkov & Yashunin 2018).
    /// Applies spatial diversity from slot 0 — no unconditional acceptance.
    /// keepPrunedConnections=true: fills limited remaining slots with discarded candidates.
    ///
    /// Metric: cosine similarity (higher = closer). The diversity condition is:
    ///   reject if similarity(candidate, selected) > similarity(candidate, query)
    /// This is the correct inversion of the paper's distance-based condition
    /// because cosine similarity is monotonically inverse to angular distance.
    fn select_neighbors(&self, candidates: BinaryHeap<NodeSimMin>, m: usize) -> Vec<u64> {
        // Consumes the heap directly. Callers must extract entry_points
        // BEFORE calling this function if they need the original heap data.
        let sorted = candidates.into_sorted_vec();
        // into_sorted_vec returns ascending order based on NodeSimMin's Ord
        // NodeSimMin Ord equates higher similarity to "Less", meaning best candidates come first!

        struct SelectedInfo {
            id: u64,
            vec: Option<Vec<f32>>,
            inv_norm: f32,
        }

        let mut selected: Vec<SelectedInfo> = Vec::with_capacity(m);
        let mut discarded: Vec<u64> = Vec::new();

        for ns in sorted.into_iter() {
            if selected.len() >= m {
                break;
            }

            let cand_id = ns.1;
            let sim_q_cand = ns.0;

            let (cand_slice, cand_inv_norm) = match self.nodes.get(&cand_id) {
                Some(n) => (
                    n.vec_data.as_f32_slice().map(|s| s.to_vec()),
                    n.inv_cached_norm,
                ),
                None => continue,
            };

            let mut is_diverse = true;
            for sel in &selected {
                let sim_cand_sel = match self.config.distance_metric {
                    DistanceMetric::Cosine => {
                        if let (Some(c_slice), Some(s_slice)) = (&cand_slice, &sel.vec) {
                            cosine_sim_cached_norms(c_slice, cand_inv_norm, s_slice, sel.inv_norm)
                        } else {
                            if let Some(sel_node) = self.nodes.get(&sel.id) {
                                let cand_norm = if cand_inv_norm > f32::EPSILON {
                                    Some(1.0 / cand_inv_norm)
                                } else {
                                    None
                                };
                                calculate_similarity(
                                    cand_slice.as_deref().unwrap_or(&[]),
                                    cand_norm,
                                    None,
                                    None,
                                    &sel_node.vec_data,
                                    self.config.distance_metric,
                                )
                            } else {
                                0.0
                            }
                        }
                    }
                    DistanceMetric::Euclidean => {
                        if let (Some(c_slice), Some(s_slice)) = (&cand_slice, &sel.vec) {
                            -euclidean_distance_squared_f32(c_slice, s_slice)
                        } else {
                            if let Some(sel_node) = self.nodes.get(&sel.id) {
                                calculate_similarity(
                                    cand_slice.as_deref().unwrap_or(&[]),
                                    None,
                                    None,
                                    None,
                                    &sel_node.vec_data,
                                    self.config.distance_metric,
                                )
                            } else {
                                0.0
                            }
                        }
                    }
                };

                if sim_cand_sel > sim_q_cand {
                    is_diverse = false;
                    break;
                }
            }

            if is_diverse {
                selected.push(SelectedInfo {
                    id: cand_id,
                    vec: cand_slice,
                    inv_norm: cand_inv_norm,
                });
            } else {
                discarded.push(cand_id);
            }
        }

        // keepPrunedConnections: fill remaining slots with discarded candidates.
        // HNSW relies on keeping degree close to M.
        let mut final_selected: Vec<u64> = selected.into_iter().map(|s| s.id).collect();
        for &disc_id in discarded.iter() {
            if final_selected.len() >= m {
                break;
            }
            final_selected.push(disc_id);
        }

        final_selected
    }

    fn validate_node(
        &self,
        id: u64,
        bitset: u128,
        vec_data: &VectorRepresentations,
        storage_offset: u64,
    ) -> bool {
        if let Some(mut node) = self.nodes.get_mut(&id) {
            node.bitset = bitset;
            node.vec_data = vec_data.clone();
            node.storage_offset = storage_offset;
            return true;
        }

        if vec_data.is_none() {
            self.nodes.insert(
                id,
                HnswNode {
                    id,
                    bitset,
                    vec_data: vec_data.clone(),
                    neighbors: vec![Vec::new()],
                    storage_offset,
                    inv_cached_norm: 0.0,
                },
            );
            return true;
        }

        false
    }

    #[tracing::instrument(skip(self, vec_data), level = "debug")]
    pub fn add(&self, id: u64, bitset: u128, vec_data: VectorRepresentations, storage_offset: u64) {
        if self.validate_node(id, bitset, &vec_data, storage_offset) {
            return;
        }

        self.insert_hnsw(id, bitset, vec_data, storage_offset);
    }

    fn insert_hnsw(
        &self,
        id: u64,
        bitset: u128,
        vec_data: VectorRepresentations,
        storage_offset: u64,
    ) {
        let level = self.random_layer();
        let ef_cons = self.config.ef_construction;

        let inv_cached_norm = match self.config.distance_metric {
            DistanceMetric::Cosine => vec_data
                .as_f32_slice()
                .map(|s| {
                    let norm = f32_l2_norm(s);
                    if norm > f32::EPSILON {
                        1.0 / norm
                    } else {
                        0.0
                    }
                })
                .unwrap_or(0.0),
            DistanceMetric::Euclidean => 0.0,
        };

        let query_f32 = vec_data.to_f32();

        let node = HnswNode {
            id,
            bitset,
            vec_data,
            neighbors: vec![Vec::new(); level + 1],
            storage_offset,
            inv_cached_norm,
        };

        let ep = match self.get_entry_point() {
            None => {
                self.set_entry_point(id);
                self.max_layer.store(level, Ordering::Release);
                self.nodes.insert(id, node);
                return;
            }
            Some(entry) => entry,
        };

        self.nodes.insert(id, node);

        let query_f32 = match query_f32 {
            Some(v) => v,
            None => return,
        };

        let (query_norm, query_inv_norm) = match self.config.distance_metric {
            DistanceMetric::Cosine => {
                let norm = f32_l2_norm(&query_f32);
                if norm < f32::EPSILON {
                    self.nodes.remove(&id);
                    return;
                }
                (Some(norm), Some(1.0 / norm))
            }
            DistanceMetric::Euclidean => (None, None),
        };

        let mut curr_entry_points = vec![ep];
        let top_layer = self.max_layer.load(Ordering::Acquire);

        for layer in (level + 1..=top_layer).rev() {
            let mut w = self.search_layer(
                &query_f32,
                query_norm,
                query_inv_norm,
                &curr_entry_points,
                1,
                layer,
                u128::MAX,
                None,
                self.config.distance_metric,
            );
            if let Some(NodeSimMin(_, best_id)) = w.pop() {
                curr_entry_points = vec![best_id];
            }
        }

        let start_layer = std::cmp::min(level, top_layer);
        for layer in (0..=start_layer).rev() {
            let w = self.search_layer(
                &query_f32,
                query_norm,
                query_inv_norm,
                &curr_entry_points,
                ef_cons,
                layer,
                u128::MAX,
                None,
                self.config.distance_metric,
            );

            let m_max = if layer == 0 {
                self.config.m_max0
            } else {
                self.config.m
            };

            curr_entry_points = w.iter().map(|ns| ns.1).collect();
            let selected_neighbors = self.select_neighbors(w, m_max);

            if let Some(mut n) = self.nodes.get_mut(&id) {
                n.neighbors[layer] = selected_neighbors.clone();
            }

            for &neighbor_id in &selected_neighbors {
                let (needs_shrink, current_neighbors) = {
                    if let Some(mut neighbor_node) = self.nodes.get_mut(&neighbor_id) {
                        if layer < neighbor_node.neighbors.len() {
                            if !neighbor_node.neighbors[layer].contains(&id) {
                                neighbor_node.neighbors[layer].push(id);
                            }

                            if neighbor_node.neighbors[layer].len() > m_max {
                                (true, neighbor_node.neighbors[layer].clone())
                            } else {
                                (false, Vec::new())
                            }
                        } else {
                            (false, Vec::new())
                        }
                    } else {
                        (false, Vec::new())
                    }
                };

                if needs_shrink {
                    let (nb_vec, nb_inv_norm) = match self.nodes.get(&neighbor_id) {
                        Some(n) => (
                            n.vec_data.as_f32_slice().map(|s| s.to_vec()),
                            n.inv_cached_norm,
                        ),
                        None => (None, 0.0),
                    };

                    if let Some(nb_v) = nb_vec {
                        let mut cand_heap = BinaryHeap::new();
                        let q_norm = if nb_inv_norm > f32::EPSILON {
                            Some(1.0 / nb_inv_norm)
                        } else {
                            None
                        };
                        let q_inv_norm = if nb_inv_norm > f32::EPSILON {
                            Some(nb_inv_norm)
                        } else {
                            None
                        };
                        for &n_target in &current_neighbors {
                            if let Some(nt) = self.nodes.get(&n_target) {
                                let d = self.fast_similarity(
                                    &nb_v,
                                    q_norm,
                                    q_inv_norm,
                                    &nt,
                                    self.config.distance_metric,
                                );
                                cand_heap.push(NodeSimMin(d, n_target));
                            }
                        }
                        let pruned = self.select_neighbors(cand_heap, m_max);
                        if let Some(mut neighbor_node) = self.nodes.get_mut(&neighbor_id) {
                            neighbor_node.neighbors[layer] = pruned;
                        }
                    }
                }
            }
        }

        self.update_metadata(level, id);
    }

    fn update_metadata(&self, level: usize, id: u64) {
        let current_max = self.max_layer.load(Ordering::Acquire);
        if level > current_max {
            self.max_layer.fetch_max(level, Ordering::Release);
            self.set_entry_point(id);
        }
    }

    #[tracing::instrument(skip(self, query_vec, vector_store), level = "debug")]
    pub fn search_nearest(
        &self,
        query_vec: &[f32],
        _q_1bit: Option<&[u64]>, // We let these pass but currently default to calculate_similarity internal handler
        _q_3bit: Option<(&[u8], f32)>,
        query_mask: u128,
        top_k: usize,
        vector_store: Option<&crate::storage::VantaFile>,
    ) -> Vec<(u64, f32)> {
        let ep = match self.get_entry_point() {
            Some(id) => id,
            None => return Vec::new(),
        };

        let ef_search = self.config.ef_search.max(top_k);
        // When Cosine metric is configured but the query vector is zero-norm
        // (no defined direction), fall back to Euclidean for this query.
        // Returning empty results for a zero-vector query breaks practical
        // use-cases where callers use [0.0]*N as a "find all nearest" probe.
        let (effective_metric, query_norm, query_inv_norm) = match self.config.distance_metric {
            DistanceMetric::Cosine => {
                let norm = f32_l2_norm(query_vec);
                if norm < f32::EPSILON {
                    // Zero-norm fallback: use Euclidean without norm info
                    (DistanceMetric::Euclidean, None, None)
                } else {
                    (DistanceMetric::Cosine, Some(norm), Some(1.0 / norm))
                }
            }
            DistanceMetric::Euclidean => (DistanceMetric::Euclidean, None, None),
        };
        let mut curr_entry_points = vec![ep];

        let max_l = self.max_layer.load(Ordering::Acquire);
        for layer in (1..=max_l).rev() {
            let mut w = self.search_layer(
                query_vec,
                query_norm,
                query_inv_norm,
                &curr_entry_points,
                1,
                layer,
                u128::MAX,
                vector_store,
                effective_metric,
            );
            if let Some(NodeSimMin(_, best_id)) = w.pop() {
                curr_entry_points = vec![best_id];
            }
        }

        let w = self.search_layer(
            query_vec,
            query_norm,
            query_inv_norm,
            &curr_entry_points,
            ef_search,
            0,
            query_mask,
            vector_store,
            effective_metric,
        );

        let mut result = w.into_sorted_vec();

        // into_sorted_vec returns highest similarity (best) first!
        result.truncate(top_k);

        let mut final_results = Vec::with_capacity(result.len());
        for NodeSimMin(score, id) in result {
            let adjusted_score = match effective_metric {
                DistanceMetric::Euclidean => -(-score).max(0.0).sqrt(),
                DistanceMetric::Cosine => score,
            };
            final_results.push((id, adjusted_score));
        }
        final_results
    }

    /// BFS traversal order for on-disk layout: entry point first, then graph neighbors
    /// (upper layers before lower) to improve mmap locality on large indexes.
    pub(crate) fn serialization_order(&self) -> Vec<u64> {
        use std::collections::{HashSet, VecDeque};

        let mut order = Vec::with_capacity(self.nodes.len());
        let mut seen = HashSet::new();

        if let Some(ep) = self.get_entry_point() {
            let mut queue = VecDeque::new();
            queue.push_back(ep);
            seen.insert(ep);

            while let Some(node_id) = queue.pop_front() {
                order.push(node_id);
                if let Some(node) = self.nodes.get(&node_id) {
                    for layer in (0..node.neighbors.len()).rev() {
                        for &neighbor_id in &node.neighbors[layer] {
                            if seen.insert(neighbor_id) {
                                queue.push_back(neighbor_id);
                            }
                        }
                    }
                }
            }
        }

        let mut orphans: Vec<u64> = self
            .nodes
            .iter()
            .map(|r| *r.key())
            .filter(|id| !seen.contains(id))
            .collect();
        orphans.sort_unstable();
        order.extend(orphans);
        order
    }

    pub fn serialize_to_bytes(&self) -> Vec<u8> {
        let mut buf = Vec::with_capacity(self.nodes.len() * 256 + 128);

        let header = crate::binary_header::VantaHeader::new(*b"VNDX", VECTOR_INDEX_VERSION, 0);
        buf.extend_from_slice(&header.serialize());
        buf.extend_from_slice(&(self.max_layer.load(Ordering::Acquire) as u64).to_le_bytes());

        // Config block (only in V2+)
        buf.extend_from_slice(&(self.config.m as u64).to_le_bytes());
        buf.extend_from_slice(&(self.config.m_max0 as u64).to_le_bytes());
        buf.extend_from_slice(&(self.config.ef_construction as u64).to_le_bytes());
        buf.extend_from_slice(&(self.config.ef_search as u64).to_le_bytes());
        buf.extend_from_slice(&self.config.ml.to_le_bytes());
        // V3+: distance metric byte (0 = Cosine, 1 = Euclidean)
        let metric_byte: u8 = match self.config.distance_metric {
            DistanceMetric::Cosine => 0,
            DistanceMetric::Euclidean => 1,
        };
        buf.push(metric_byte);

        match self.get_entry_point() {
            Some(ep) => {
                buf.push(1);
                buf.extend_from_slice(&ep.to_le_bytes());
            }
            None => {
                buf.push(0);
                buf.extend_from_slice(&0u64.to_le_bytes());
            }
        }

        let node_count = self.nodes.len() as u64;
        buf.extend_from_slice(&node_count.to_le_bytes());

        for node_id in self.serialization_order() {
            let Some(node) = self.nodes.get(&node_id) else {
                continue;
            };
            buf.extend_from_slice(&node.id.to_le_bytes());
            buf.extend_from_slice(&node.bitset.to_le_bytes());
            buf.extend_from_slice(&node.storage_offset.to_le_bytes());

            match &node.vec_data {
                VectorRepresentations::Full(f) => {
                    buf.push(1);
                    buf.extend_from_slice(&(f.len() as u64).to_le_bytes());
                    let padding = (4 - (buf.len() % 4)) % 4;
                    if padding > 0 {
                        buf.extend(std::iter::repeat_n(0, padding));
                    }
                    for &val in f {
                        buf.extend_from_slice(&val.to_le_bytes());
                    }
                }
                VectorRepresentations::MmapFull(ptr, len) => {
                    buf.push(1);
                    buf.extend_from_slice(&(*len as u64).to_le_bytes());
                    let padding = (4 - (buf.len() % 4)) % 4;
                    if padding > 0 {
                        buf.extend(std::iter::repeat_n(0, padding));
                    }
                    let slice = unsafe { std::slice::from_raw_parts(ptr.0, *len) };
                    for &val in slice {
                        buf.extend_from_slice(&val.to_le_bytes());
                    }
                }
                VectorRepresentations::Binary(b) => {
                    buf.push(2);
                    buf.extend_from_slice(&(b.len() as u64).to_le_bytes());
                    for &val in b {
                        buf.extend_from_slice(&val.to_le_bytes());
                    }
                }
                VectorRepresentations::Turbo(t) => {
                    buf.push(3);
                    buf.extend_from_slice(&(t.len() as u64).to_le_bytes());
                    buf.extend_from_slice(t);
                }
                VectorRepresentations::SQ8(d, scale) => {
                    buf.push(4);
                    buf.extend_from_slice(&(d.len() as u64).to_le_bytes());
                    for &v in d {
                        buf.push(v as u8);
                    }
                    buf.extend_from_slice(&scale.to_le_bytes());
                }
                VectorRepresentations::None => {
                    buf.push(0);
                    buf.extend_from_slice(&0u64.to_le_bytes());
                }
            }

            let layer_count = node.neighbors.len() as u64;
            buf.extend_from_slice(&layer_count.to_le_bytes());
            for layer in &node.neighbors {
                let neighbor_count = layer.len() as u64;
                buf.extend_from_slice(&neighbor_count.to_le_bytes());
                for &nid in layer {
                    buf.extend_from_slice(&nid.to_le_bytes());
                }
            }
        }

        buf
    }

    pub fn deserialize_from_bytes(data: &[u8], force_copy: bool) -> std::io::Result<Self> {
        use std::io::{Error, ErrorKind};

        #[inline]
        fn take_bytes<'a>(
            data: &'a [u8],
            pos: &mut usize,
            n: usize,
            field: &str,
        ) -> std::io::Result<&'a [u8]> {
            if *pos + n > data.len() {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::UnexpectedEof,
                    format!("Truncated {field}"),
                ));
            }
            let slice = &data[*pos..*pos + n];
            *pos += n;
            Ok(slice)
        }

        #[inline]
        fn read_le_u64(data: &[u8], pos: &mut usize, field: &str) -> std::io::Result<u64> {
            let bytes = take_bytes(data, pos, 8, field)?;
            Ok(u64::from_le_bytes(bytes.try_into().map_err(|e| {
                std::io::Error::new(
                    std::io::ErrorKind::InvalidData,
                    format!("failed to parse {field} as u64: {e}"),
                )
            })?))
        }

        #[inline]
        fn read_le_f64(data: &[u8], pos: &mut usize, field: &str) -> std::io::Result<f64> {
            let bytes = take_bytes(data, pos, 8, field)?;
            Ok(f64::from_le_bytes(bytes.try_into().map_err(|e| {
                std::io::Error::new(
                    std::io::ErrorKind::InvalidData,
                    format!("failed to parse {field} as f64: {e}"),
                )
            })?))
        }

        if data.len() < crate::binary_header::VantaHeader::SIZE + 8 {
            return Err(Error::new(ErrorKind::InvalidData, "Index file too small"));
        }

        let mut pos = 0;

        let header = match crate::binary_header::VantaHeader::deserialize(
            &data[pos..pos + crate::binary_header::VantaHeader::SIZE],
        ) {
            Ok(h) => h,
            Err(e) => {
                return Err(Error::new(
                    ErrorKind::InvalidData,
                    format!("Failed to parse binary header: {:?}", e),
                ))
            }
        };
        pos += crate::binary_header::VantaHeader::SIZE;

        if let Err(e) = header.validate(*b"VNDX", VECTOR_INDEX_VERSION, "Index format mismatch") {
            return Err(Error::new(ErrorKind::InvalidData, format!("{}", e)));
        }

        let version = header.format_version as u32;

        let max_layer = read_le_u64(data, &mut pos, "max_layer")? as usize;

        let mut config = HnswConfig::default();
        if version >= 2 {
            config.m = read_le_u64(data, &mut pos, "config.m")? as usize;
            config.m_max0 = read_le_u64(data, &mut pos, "config.m_max0")? as usize;
            config.ef_construction =
                read_le_u64(data, &mut pos, "config.ef_construction")? as usize;
            config.ef_search = read_le_u64(data, &mut pos, "config.ef_search")? as usize;
            config.ml = read_le_f64(data, &mut pos, "config.ml")?;
        }
        // V3+: distance metric byte
        if version >= 3 && pos < data.len() {
            config.distance_metric = match take_bytes(data, &mut pos, 1, "distance_metric")?[0] {
                1 => DistanceMetric::Euclidean,
                _ => DistanceMetric::Cosine,
            };
        }

        let ep_exists = take_bytes(data, &mut pos, 1, "ep_exists")?[0];
        let ep_id = read_le_u64(data, &mut pos, "ep_id")?;
        let entry_point = if ep_exists == 1 { Some(ep_id) } else { None };

        let node_count = read_le_u64(data, &mut pos, "node_count")? as usize;

        // Sanity: each node needs at least 49 bytes (id + bitset + offset + type + vec_len + layer_count).
        const MIN_BYTES_PER_NODE: usize = 8 + 16 + 8 + 1 + 8 + 8;
        let remaining = data.len().saturating_sub(pos);
        if node_count > remaining / MIN_BYTES_PER_NODE {
            return Err(Error::new(
                ErrorKind::InvalidData,
                format!(
                    "node_count ({node_count}) exceeds plausible limit for {remaining} remaining bytes",
                ),
            ));
        }

        let nodes = DashMap::with_capacity_and_hasher(node_count, BuildHasherDefault::default());

        for _ in 0..node_count {
            let id = read_le_u64(data, &mut pos, "node id")?;

            let bitset_bytes = take_bytes(data, &mut pos, 16, "bitset")?;
            let bitset = u128::from_le_bytes(bitset_bytes.try_into().map_err(|e| {
                std::io::Error::new(
                    std::io::ErrorKind::InvalidData,
                    format!("bitset field expected 16 bytes: {e}"),
                )
            })?);

            let storage_offset = read_le_u64(data, &mut pos, "storage_offset")?;

            let vec_type = take_bytes(data, &mut pos, 1, "vec_type")?[0];

            let vec_len = read_le_u64(data, &mut pos, "vec_len")? as usize;

            let vec_data = match vec_type {
                1 => {
                    let byte_len = vec_len.checked_mul(4).ok_or_else(|| {
                        Error::new(ErrorKind::InvalidData, "vec_len overflow (f32)")
                    })?;
                    if version >= 4 {
                        let padding = (4 - (pos % 4)) % 4;
                        pos += padding;
                    }
                    let vec_bytes = take_bytes(data, &mut pos, byte_len, "f32 vec")?;
                    if force_copy {
                        let mut v = Vec::with_capacity(vec_len);
                        for i in 0..vec_len {
                            let start = i * 4;
                            v.push(f32::from_le_bytes(
                                vec_bytes[start..start + 4].try_into().map_err(|e| {
                                    std::io::Error::new(
                                        std::io::ErrorKind::InvalidData,
                                        format!(
                                            "f32 vec chunk at byte {start} expected 4 bytes: {e}"
                                        ),
                                    )
                                })?,
                            ));
                        }
                        VectorRepresentations::Full(v)
                    } else {
                        let ptr = vec_bytes.as_ptr() as *const f32;
                        VectorRepresentations::MmapFull(SendPtr(ptr), vec_len)
                    }
                }
                2 => {
                    let byte_len = vec_len.checked_mul(8).ok_or_else(|| {
                        Error::new(ErrorKind::InvalidData, "vec_len overflow (binary)")
                    })?;
                    let vec_bytes = take_bytes(data, &mut pos, byte_len, "binary vec")?;
                    let mut v = Vec::with_capacity(vec_len);
                    for i in 0..vec_len {
                        let start = i * 8;
                        v.push(u64::from_le_bytes(
                            vec_bytes[start..start + 8].try_into().map_err(|e| {
                                std::io::Error::new(
                                    std::io::ErrorKind::InvalidData,
                                    format!(
                                        "binary vec chunk at byte {start} expected 8 bytes: {e}"
                                    ),
                                )
                            })?,
                        ));
                    }
                    VectorRepresentations::Binary(v.into_boxed_slice())
                }
                3 => {
                    let vec_bytes = take_bytes(data, &mut pos, vec_len, "turbo vec")?;
                    VectorRepresentations::Turbo(vec_bytes.to_vec().into_boxed_slice())
                }
                4 => {
                    let sq8_bytes = take_bytes(data, &mut pos, vec_len, "sq8 vec")?;
                    let sq8_data: Vec<i8> = sq8_bytes.iter().map(|&b| b as i8).collect();
                    let scale_bytes = take_bytes(data, &mut pos, 4, "sq8 scale")?;
                    let scale = f32::from_le_bytes(scale_bytes.try_into().map_err(|e| {
                        Error::new(ErrorKind::InvalidData, format!("sq8 scale: {e}"))
                    })?);
                    VectorRepresentations::SQ8(sq8_data.into_boxed_slice(), scale)
                }
                _ => VectorRepresentations::None,
            };

            let layer_count = read_le_u64(data, &mut pos, "layer_count")? as usize;
            let layer_remaining = data.len().saturating_sub(pos);
            if layer_count > layer_remaining / 8 {
                return Err(Error::new(
                    ErrorKind::InvalidData,
                    format!("layer_count ({layer_count}) exceeds remaining data"),
                ));
            }

            let mut neighbors = Vec::with_capacity(layer_count);
            for _ in 0..layer_count {
                let neighbor_count = read_le_u64(data, &mut pos, "neighbor_count")? as usize;

                let byte_len = neighbor_count
                    .checked_mul(8)
                    .ok_or_else(|| Error::new(ErrorKind::InvalidData, "neighbor_count overflow"))?;
                let nbr_bytes = take_bytes(data, &mut pos, byte_len, "neighbor ids")?;
                let mut layer_neighbors = Vec::with_capacity(neighbor_count);
                for i in 0..neighbor_count {
                    let start = i * 8;
                    layer_neighbors.push(u64::from_le_bytes(
                        nbr_bytes[start..start + 8].try_into().map_err(|e| {
                            std::io::Error::new(
                                std::io::ErrorKind::InvalidData,
                                format!("neighbor id at byte {start} expected 8 bytes: {e}"),
                            )
                        })?,
                    ));
                }
                neighbors.push(layer_neighbors);
            }

            // Compute inv_cached_norm at load time for Cosine fast-path
            let inv_cached_norm = match config.distance_metric {
                DistanceMetric::Cosine => match &vec_data {
                    VectorRepresentations::Full(f) => {
                        let norm = f32_l2_norm(f);
                        if norm > f32::EPSILON {
                            1.0 / norm
                        } else {
                            0.0
                        }
                    }
                    VectorRepresentations::MmapFull(ptr, len) => {
                        let s = unsafe { std::slice::from_raw_parts(ptr.0, *len) };
                        let norm = f32_l2_norm(s);
                        if norm > f32::EPSILON {
                            1.0 / norm
                        } else {
                            0.0
                        }
                    }
                    _ => 0.0,
                },
                DistanceMetric::Euclidean => 0.0,
            };
            nodes.insert(
                id,
                HnswNode {
                    id,
                    bitset,
                    vec_data,
                    neighbors,
                    storage_offset,
                    inv_cached_norm,
                },
            );
        }

        Ok(Self {
            nodes,
            max_layer: AtomicUsize::new(max_layer),
            entry_point: AtomicU64::new(entry_point.unwrap_or(ENTRY_POINT_NONE)),
            backend: IndexBackend::InMemory,
            config,
            rng: parking_lot::Mutex::new(rand::rngs::StdRng::seed_from_u64(42)),
        })
    }

    pub fn persist_to_file(&self, path: &Path) -> std::io::Result<()> {
        #[cfg(feature = "failpoints")]
        {
            fail::fail_point!("hnsw_serialize_fail", |_| {
                Err(std::io::Error::other(
                    "Injected HNSW persist serialization failure",
                ))
            });
        }
        let data = self.serialize_to_bytes();
        let file = File::create(path)?;
        let mut writer = BufWriter::new(file);
        writer.write_all(&data)?;
        writer.flush()?;
        info!(path = %path.display(), node_count = self.nodes.len(), bytes = data.len(), "HNSW index persisted");
        Ok(())
    }

    pub fn load_from_file(path: &Path, use_mmap: bool) -> Option<Self> {
        if !path.exists() {
            return None;
        }

        if use_mmap {
            let file = match OpenOptions::new().read(true).write(true).open(path) {
                Ok(f) => f,
                Err(_) => return None,
            };

            let mmap = match unsafe { memmap2::MmapMut::map_mut(&file) } {
                Ok(m) => m,
                Err(e) => {
                    warn!(err = %e, "Failed to mmap HNSW index file — will rebuild");
                    return None;
                }
            };

            match Self::deserialize_from_bytes(&mmap, false) {
                Ok(mut index) => {
                    info!(path = %path.display(), node_count = index.nodes.len(), "HNSW cold-start: loaded zero-copy index from file");
                    index.backend = IndexBackend::MMapFile {
                        path: path.to_path_buf(),
                        mmap: Some(mmap),
                    };
                    if let Err(violations) = index.validate_index() {
                        warn!(
                            violation_count = violations.len(),
                            "HNSW index has integrity violations after deserialization"
                        );
                        for v in &violations[..violations.len().min(5)] {
                            warn!(violation = %v, "HNSW integrity violation");
                        }
                    }
                    Some(index)
                }
                Err(e) => {
                    warn!(err = %e, "Corrupt vector_index.bin — will rebuild and overwrite");
                    None
                }
            }
        } else {
            let data = match std::fs::read(path) {
                Ok(d) => d,
                Err(_) => return None,
            };

            match Self::deserialize_from_bytes(&data, true) {
                Ok(index) => {
                    info!(path = %path.display(), node_count = index.nodes.len(), "HNSW cold-start: loaded memory-copied index from file");
                    if let Err(violations) = index.validate_index() {
                        warn!(
                            violation_count = violations.len(),
                            "HNSW index has integrity violations after deserialization"
                        );
                        for v in &violations[..violations.len().min(5)] {
                            warn!(violation = %v, "HNSW integrity violation");
                        }
                    }
                    Some(index)
                }
                Err(e) => {
                    warn!(err = %e, "Corrupt vector_index.bin — will rebuild and overwrite");
                    None
                }
            }
        }
    }

    pub fn sync_to_mmap(&mut self) -> std::io::Result<()> {
        #[cfg(feature = "failpoints")]
        {
            fail::fail_point!("hnsw_serialize_fail", |_| {
                Err(std::io::Error::other(
                    "Injected HNSW sync mmap serialization failure",
                ))
            });
        }
        let path = match &self.backend {
            IndexBackend::MMapFile { path, .. } => path.clone(),
            _ => return Ok(()),
        };

        let data = self.serialize_to_bytes();
        let temp_path = path.with_extension("bin.tmp");

        let file = OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .truncate(true)
            .open(&temp_path)?;
        file.set_len(data.len() as u64)?;

        let mut mapped = unsafe { MmapMut::map_mut(&file)? };
        mapped.copy_from_slice(&data);
        mapped.flush()?;

        // Remapear todos los nodos a la nueva dirección de memoria virtual para evitar dangling pointers
        let new_index = Self::deserialize_from_bytes(&mapped, false)?;
        self.nodes = new_index.nodes;
        self.entry_point = new_index.entry_point;

        if let IndexBackend::MMapFile { ref mut mmap, .. } = self.backend {
            *mmap = Some(mapped);
        }

        // Swap atómico de archivos: temp -> final
        drop(file);
        std::fs::rename(&temp_path, &path)?;

        info!(path = %path.display(), node_count = self.nodes.len(), bytes = data.len(), "HNSW MMap synced & zero-copy pointers re-mapped via atomic rename");
        Ok(())
    }
}

// ─── Phase 1.1: Index Stats & Integrity Validation ──────────────────────────

/// Snapshot of HNSW index health metrics
#[derive(Debug, Clone)]
pub struct IndexStats {
    /// Total nodes in the index
    pub node_count: usize,
    /// Maximum layer height in the graph
    pub max_layer: usize,
    /// Nodes with zero neighbors on layer 0 (potential orphans)
    pub orphan_count: usize,
    /// Average outgoing connections on layer 0
    pub avg_connections_l0: f32,
    /// Total number of graph integrity violations found
    pub violation_count: usize,
}

impl CPIndex {
    /// Compute a snapshot of index health metrics.
    pub fn stats(&self) -> IndexStats {
        let node_count = self.nodes.len();
        let orphan_count = self
            .nodes
            .iter()
            .filter(|r| r.value().neighbors.is_empty() || r.value().neighbors[0].is_empty())
            .count();
        let total_l0_connections: usize = self
            .nodes
            .iter()
            .map(|r| r.value().neighbors.first().map(|l| l.len()).unwrap_or(0))
            .sum();
        let avg_connections_l0 = if node_count > 0 {
            total_l0_connections as f32 / node_count as f32
        } else {
            0.0
        };

        IndexStats {
            node_count,
            max_layer: self.max_layer.load(Ordering::Acquire),
            orphan_count,
            avg_connections_l0,
            violation_count: 0, // Updated by validate_index()
        }
    }

    /// Validate the structural integrity of the HNSW graph.
    ///
    /// Checks:
    /// 1. Every neighbor reference points to an existing node
    /// 2. No self-loops
    /// 3. Layer count is consistent with node's reported level
    ///
    /// Returns `Ok(())` if the graph is clean, or a list of violation messages.
    ///
    /// # Performance
    /// O(N × M) where N = node count, M = max neighbors per layer.
    /// Run at startup after deserialization, not in hot paths.
    pub fn validate_index(&self) -> Result<(), Vec<String>> {
        let mut violations = Vec::new();

        for r in self.nodes.iter() {
            let id = *r.key();
            let node = r.value();
            // Check: layer count should be ≥ 1
            if node.neighbors.is_empty() {
                violations.push(format!(
                    "Node {} has empty neighbors array (expected ≥1 layer)",
                    id
                ));
                continue;
            }

            // Check each layer's neighbor list
            for (layer_idx, layer) in node.neighbors.iter().enumerate() {
                for &neighbor_id in layer {
                    // Self-loop check
                    if neighbor_id == id {
                        violations.push(format!(
                            "Node {} has a self-loop at layer {}",
                            id, layer_idx
                        ));
                        continue;
                    }
                    // Dangling reference check
                    if !self.nodes.contains_key(&neighbor_id) {
                        violations.push(format!(
                            "Node {} references non-existent neighbor {} at layer {}",
                            id, neighbor_id, layer_idx
                        ));
                    }
                }
            }
        }

        // Check entry point validity
        if let Some(ep) = self.get_entry_point() {
            if !self.nodes.contains_key(&ep) {
                violations.push(format!("Entry point {} does not exist in the node map", ep));
            }
        }

        if violations.is_empty() {
            Ok(())
        } else {
            Err(violations)
        }
    }
}

impl Default for CPIndex {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::node::DistanceMetric;

    #[test]
    fn cosine_with_precomputed_query_norm_matches_full_path() {
        let a = vec![0.12, 0.88, 0.54, 0.31];
        let b = vec![0.11, 0.89, 0.55, 0.30];
        let norm_a = f32_l2_norm(&a);
        let expected = cosine_sim_f32(&a, &b);
        let optimized = cosine_sim_with_query_norm(&a, norm_a, &b);
        assert!(
            (expected - optimized).abs() < 1e-6,
            "expected {expected}, got {optimized}"
        );
    }

    #[test]
    fn serialization_order_preserves_search_results() {
        let index = CPIndex::new_with_config(HnswConfig {
            m: 8,
            m_max0: 16,
            ef_construction: 64,
            ef_search: 32,
            ml: 1.0 / (8_f64).ln(),
            distance_metric: DistanceMetric::Cosine,
        });

        for i in 0..64u64 {
            let raw = [
                (i as f32 * 0.01).sin(),
                (i as f32 * 0.02).cos(),
                (i as f32 * 0.03).sin(),
                (i as f32 * 0.04).cos(),
            ];
            let norm = f32_l2_norm(&raw);
            let normalized: Vec<f32> = raw.iter().map(|v| v / norm).collect();
            index.add(i + 1, 0, VectorRepresentations::Full(normalized), 0);
        }

        let query = vec![0.1, 0.9, 0.2, 0.4];
        let before = index.search_nearest(&query, None, None, 0, 5, None);

        let bytes = index.serialize_to_bytes();
        let restored = CPIndex::deserialize_from_bytes(&bytes, true).expect("deserialize");
        let after = restored.search_nearest(&query, None, None, 0, 5, None);

        assert_eq!(before, after);
        assert_eq!(restored.nodes.len(), index.nodes.len());
    }

    #[test]
    fn concurrent_search_during_insert() {
        use std::sync::atomic::{AtomicBool, Ordering};
        use std::sync::Arc;
        use std::sync::Mutex;
        use std::thread;
        use std::time::Duration;

        let index = Arc::new(CPIndex::new_with_config(HnswConfig {
            m: 16,
            m_max0: 32,
            ef_construction: 64,
            ef_search: 32,
            ml: 1.0 / (16_f64).ln(),
            distance_metric: DistanceMetric::Cosine,
        }));

        let stop = Arc::new(AtomicBool::new(false));
        let insert_mutex = Arc::new(Mutex::new(())); // Imita el insert_lock de StorageEngine
        let mut handles = Vec::new();

        // Lanzar 2 hilos que insertan nodos de manera sincronizada
        for t in 0..2 {
            let index = index.clone();
            let stop = stop.clone();
            let insert_mutex = insert_mutex.clone();
            handles.push(thread::spawn(move || {
                let mut rng = rand::rng();
                let start_id = t * 1000;
                for i in 0..1000 {
                    if stop.load(Ordering::Relaxed) {
                        break;
                    }
                    let id = start_id + i;
                    let raw_vec: Vec<f32> = (0..32).map(|_| rng.random::<f32>()).collect();
                    let norm = f32_l2_norm(&raw_vec);
                    let vec: Vec<f32> = if norm > 0.0 {
                        raw_vec.iter().map(|v| v / norm).collect()
                    } else {
                        raw_vec
                    };

                    // Adquirir lock para cumplir el contrato de CPIndex::add
                    let _guard = insert_mutex.lock().unwrap();
                    index.add(id, u128::MAX, VectorRepresentations::Full(vec), 0);
                }
            }));
        }

        // Lanzar 4 hilos de búsqueda
        for _ in 0..4 {
            let index = index.clone();
            let stop = stop.clone();
            handles.push(thread::spawn(move || {
                let mut rng = rand::rng();
                while !stop.load(Ordering::Relaxed) {
                    let query: Vec<f32> = (0..32).map(|_| rng.random::<f32>()).collect();
                    let norm = f32_l2_norm(&query);
                    let q_vec = if norm > 0.0 {
                        query.iter().map(|v| v / norm).collect()
                    } else {
                        query
                    };
                    // Búsqueda concurrente sin adquirir insert_lock
                    let _res = index.search_nearest(&q_vec, None, None, u128::MAX, 5, None);
                    thread::sleep(Duration::from_micros(10));
                }
            }));
        }

        // Dejar correr por 1 segundo
        thread::sleep(Duration::from_millis(1000));
        stop.store(true, Ordering::Relaxed);

        // Join de todos los hilos
        for handle in handles {
            let _ = handle.join();
        }

        // Validar integridad estructural del grafo
        assert!(index.validate_index().is_ok());
    }

    #[test]
    fn concurrent_insert_preserves_hnsw_invariants() {
        use crate::node::UnifiedNode;
        use crate::storage::StorageEngine;
        use std::sync::Arc;
        use std::thread;
        use tempfile::tempdir;

        let dir = tempdir().unwrap();
        let db_path = dir.path().to_str().unwrap();
        let storage = Arc::new(StorageEngine::open(db_path).unwrap());

        let mut handles = Vec::new();
        // 4 hilos insertando de forma concurrente
        for t in 0..4 {
            let storage = storage.clone();
            handles.push(thread::spawn(move || {
                let mut rng = rand::rng();
                let start_id = t * 500 + 1; // Evitar ID 0 que a veces es entry point
                for i in 0..500 {
                    let id = start_id + i;
                    let raw_vec: Vec<f32> = (0..32).map(|_| rng.random::<f32>()).collect();
                    let norm = f32_l2_norm(&raw_vec);
                    let vec: Vec<f32> = if norm > 0.0 {
                        raw_vec.iter().map(|v| v / norm).collect()
                    } else {
                        raw_vec
                    };

                    let mut node = UnifiedNode::new(id);
                    node.vector = VectorRepresentations::Full(vec);
                    storage.insert(&node).unwrap();
                }
            }));
        }

        for handle in handles {
            let _ = handle.join();
        }

        // Validar integridad
        let hnsw = storage.hnsw.load();
        assert!(hnsw.validate_index().is_ok());

        // Validar que todos los nodos sean alcanzables BFS desde el entry point
        let ep = hnsw.get_entry_point().expect("Should have entry point");
        let mut visited = std::collections::HashSet::new();
        let mut queue = std::collections::VecDeque::new();
        queue.push_back(ep);
        visited.insert(ep);

        while let Some(node_id) = queue.pop_front() {
            if let Some(node) = hnsw.nodes.get(&node_id) {
                // BFS en todos los vecinos de todas las capas
                for layer in &node.neighbors {
                    for &neighbor in layer {
                        if visited.insert(neighbor) {
                            queue.push_back(neighbor);
                        }
                    }
                }
            }
        }

        // Todos los nodos en HNSW deben ser alcanzables
        assert_eq!(
            visited.len(),
            hnsw.nodes.len(),
            "Not all nodes are reachable from the entry point!"
        );
    }

    fn build_small_test_index() -> CPIndex {
        let index = CPIndex::new_with_config(HnswConfig {
            m: 8,
            m_max0: 16,
            ef_construction: 64,
            ef_search: 32,
            ml: 1.0 / (8_f64).ln(),
            distance_metric: DistanceMetric::Cosine,
        });
        for i in 0..16u64 {
            let raw = [
                (i as f32 * 0.01).sin(),
                (i as f32 * 0.02).cos(),
                (i as f32 * 0.03).sin(),
                (i as f32 * 0.04).cos(),
            ];
            let norm = f32_l2_norm(&raw);
            let normalized: Vec<f32> = raw.iter().map(|v| v / norm).collect();
            index.add(i + 1, 0, VectorRepresentations::Full(normalized), 0);
        }
        index
    }

    #[test]
    fn deserialize_truncated_never_panics() {
        let index = build_small_test_index();
        let bytes = index.serialize_to_bytes();
        for len in 0..bytes.len() {
            let result = CPIndex::deserialize_from_bytes(&bytes[..len], true);
            assert!(
                result.is_err(),
                "Expected Err for truncated input at {len}/{} bytes, got Ok",
                bytes.len()
            );
        }
        let full = CPIndex::deserialize_from_bytes(&bytes, true);
        assert!(
            full.is_ok(),
            "Full bytes must deserialize: {:?}",
            full.err()
        );
    }

    #[test]
    fn deserialize_garbage_after_valid_header() {
        let mut garbage = vec![0u8; 512];
        let header = crate::binary_header::VantaHeader::new(*b"VNDX", VECTOR_INDEX_VERSION, 0);
        let hdr = header.serialize();
        garbage[..hdr.len()].copy_from_slice(&hdr);
        let result = CPIndex::deserialize_from_bytes(&garbage, true);
        assert!(result.is_err() || result.unwrap().nodes.is_empty());
    }

    #[test]
    fn deserialize_absurd_node_count() {
        let index = build_small_test_index();
        let mut bytes = index.serialize_to_bytes();

        let header_size = crate::binary_header::VantaHeader::SIZE;
        // max_layer(8) + config v2 (m, m_max0, ef_construction, ef_search, ml = 5*8=40)
        // + distance_metric(1) + ep_exists(1) + ep_id(8) = 58 bytes after header
        let node_count_offset = header_size + 8 + 40 + 1 + 1 + 8;
        if node_count_offset + 8 <= bytes.len() {
            bytes[node_count_offset..node_count_offset + 8]
                .copy_from_slice(&u64::MAX.to_le_bytes());
            let result = CPIndex::deserialize_from_bytes(&bytes, true);
            assert!(result.is_err(), "Absurd node_count must return Err");
        }
    }
}