1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
//! Current-state storage engine.
//!
//! This module implements the "hot path" storage for the current state of the
//! graph. It provides O(1) lookups and cache-friendly traversals optimized for
//! non-temporal queries.
use crate::core::error::{Result, StorageError};
use crate::core::graph::{Edge, Node};
use crate::core::id::{EdgeId, IdGenerator, NodeId, VersionId};
use crate::core::interning::{GLOBAL_INTERNER, InternedString};
use crate::core::property::{PropertyMap, PropertyValue};
use crate::core::temporal::Timestamp;
use crate::index::current::CurrentIndexes;
use crate::index::vector::hnsw::{HnswConfig, HnswIndex};
use crate::index::vector::temporal::{TemporalVectorConfig, TemporalVectorIndex};
use crate::index::vector::{TemporalSearchResults, VectorIndex};
use dashmap::DashMap;
use dashmap::mapref::entry::Entry;
use parking_lot::RwLock;
use std::sync::Arc;
mod iterators;
mod stats;
mod vector;
pub use iterators::*;
pub use stats::CurrentStats;
use stats::FilterStats;
pub use vector::VectorIndexInfo;
use vector::{TemporalVectorIndexEntry, TemporalVectorIndexState, VectorIndexEntry};
/// Maximum number of vector-indexed properties allowed per database.
///
/// This limit prevents resource exhaustion from enabling too many vector indexes,
/// as each index consumes significant memory (1.5-2x the raw vector data size
/// due to HNSW graph overhead).
///
/// Override via configuration if your use case requires more properties.
pub const DEFAULT_MAX_VECTOR_PROPERTIES: usize = 10;
/// Current-state storage engine.
///
/// This storage engine maintains the current version of all nodes and edges,
/// optimized for fast queries without temporal overhead. This is the "fast path"
/// that should achieve <1µs single-hop traversals.
pub struct CurrentStorage {
/// Indexes for nodes and edges
indexes: CurrentIndexes,
/// ID generator for nodes
node_id_gen: IdGenerator,
/// ID generator for edges
edge_id_gen: IdGenerator,
/// ID generator for versions
version_id_gen: IdGenerator,
/// Multi-property vector indexes (Issue #389)
/// Maps property name -> VectorIndexEntry
vector_indexes: DashMap<String, VectorIndexEntry>,
/// Multi-property temporal vector indexes (Issue #389 fix)
/// Maps property name -> TemporalVectorIndexEntry
temporal_vector_indexes: DashMap<String, TemporalVectorIndexEntry>,
/// Legacy temporal vector index state (for backward compatibility)
temporal_vector_index_state: Arc<RwLock<TemporalVectorIndexState>>,
/// Adaptive over-fetch statistics per label (Issue #334)
/// Maps label -> FilterStats for tracking label-specific filter pass rates
///
/// **Memory Considerations**: This map grows unbounded as new labels are encountered.
/// For workloads with bounded label sets (typical case), memory usage is negligible
/// (~100 bytes per unique label). For unbounded labels (e.g., using UUIDs as labels),
/// memory usage scales linearly with unique label count (~50MB per 1M labels).
///
/// **Recommendation**: Use a bounded set of labels for optimal performance.
/// Future enhancement could add LRU eviction for unbounded scenarios.
filter_stats: DashMap<String, Arc<FilterStats>>,
/// Lock to synchronize snapshot creation with concurrent writes
pub(crate) snapshot_lock: RwLock<()>,
}
impl CurrentStorage {
/// Create a new empty current storage.
pub fn new() -> Self {
CurrentStorage {
indexes: CurrentIndexes::new(),
node_id_gen: IdGenerator::new(),
edge_id_gen: IdGenerator::new(),
version_id_gen: IdGenerator::new(),
vector_indexes: DashMap::new(),
temporal_vector_indexes: DashMap::new(),
temporal_vector_index_state: Arc::new(RwLock::new(TemporalVectorIndexState::new())),
filter_stats: DashMap::new(),
snapshot_lock: RwLock::new(()),
}
}
/// Initialize the node ID generator with a specific starting value.
///
/// This is used during recovery to ensure the ID generator continues from
/// the maximum ID found in the WAL, preventing ID conflicts.
///
/// # Arguments
///
/// * `start` - The next ID to generate (typically max_id + 1)
pub(crate) fn init_node_id_generator(&self, start: u64) {
self.node_id_gen.reset_to(start);
}
/// Initialize the edge ID generator with a specific starting value.
///
/// This is used during recovery to ensure the ID generator continues from
/// the maximum ID found in the WAL, preventing ID conflicts.
///
/// # Arguments
///
/// * `start` - The next ID to generate (typically max_id + 1)
pub(crate) fn init_edge_id_generator(&self, start: u64) {
self.edge_id_gen.reset_to(start);
}
/// Initialize the version ID generator with a specific starting value.
///
/// This is used during recovery to ensure the ID generator continues from
/// the maximum ID found in the WAL, preventing ID conflicts.
///
/// # Arguments
///
/// * `start` - The next ID to generate (typically max_id + 1)
#[inline]
pub(crate) fn init_version_id_generator(&self, start: u64) {
self.version_id_gen.reset_to(start);
}
/// Ensure the version ID generator's next value is at least the specified minimum.
///
/// This is used during recovery when we need to account for version IDs from multiple
/// sources (e.g., current storage and historical storage) without overwriting
/// a higher value that was already set.
///
/// # Arguments
///
/// * `min_value` - The minimum next version ID to generate
#[inline]
pub(crate) fn ensure_version_id_generator_at_least(&self, min_value: u64) {
self.version_id_gen.ensure_at_least(min_value);
}
/// Get the current value of the version ID generator.
///
/// This is used during recovery to determine the starting version ID for
/// WAL replay.
#[inline]
pub(crate) fn get_version_id_generator_current(&self) -> u64 {
self.version_id_gen.current()
}
/// Enable vector indexing for a specific property.
///
/// Multiple properties can be indexed simultaneously with different configurations.
/// Each property can have its own dimensions and distance metric.
///
/// # Arguments
///
/// * `property_name` - Name of the property containing vectors
/// * `config` - HNSW index configuration
///
/// # Errors
///
/// Returns an error if:
/// - This specific property already has a vector index enabled
/// - The maximum number of indexed properties ([`DEFAULT_MAX_VECTOR_PROPERTIES`]) is reached
///
/// # Memory Usage
///
/// Each vector index consumes approximately **1.5-2x** the raw vector data size due to
/// HNSW graph overhead (neighbor lists, layer structure). For example:
///
/// | Vectors | Dimensions | Raw Size | Index Size |
/// |---------|------------|----------|------------|
/// | 100K | 384 | ~150 MB | ~225-300 MB |
/// | 1M | 768 | ~3 GB | ~4.5-6 GB |
/// | 1M | 1536 | ~6 GB | ~9-12 GB |
///
/// Plan capacity accordingly when enabling multiple property indexes.
///
/// # Persistence Limitation
///
/// **Warning**: Currently, the checkpoint format only supports persisting a single
/// vector index configuration (the "default" one, which is alphabetically first).
/// If you enable multiple vector indexes, **only one will be persisted** in checkpoints.
/// The others will be lost upon recovery from a checkpoint. This limitation will be
/// addressed in a future update to the checkpoint format.
///
/// # Example
///
/// ```ignore
/// // Index title embeddings (384 dimensions)
/// storage.enable_vector_index("title_embedding", config_384)?;
/// // Index body embeddings (1536 dimensions) - different property, OK!
/// storage.enable_vector_index("body_embedding", config_1536)?;
/// ```
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn enable_vector_index(&self, property_name: &str, config: HnswConfig) -> Result<()> {
// Check property limit before attempting to add
if self.vector_indexes.len() >= DEFAULT_MAX_VECTOR_PROPERTIES {
return Err(crate::core::error::Error::Vector(
crate::core::error::VectorError::IndexError(format!(
"Maximum number of vector-indexed properties ({}) reached. \
Cannot enable index for property '{}'",
DEFAULT_MAX_VECTOR_PROPERTIES, property_name
)),
));
}
// Use atomic entry() API to avoid TOCTOU race condition
match self.vector_indexes.entry(property_name.to_string()) {
Entry::Occupied(_) => {
return Err(crate::core::error::Error::Vector(
crate::core::error::VectorError::IndexError(format!(
"Vector index already enabled for property '{}'",
property_name
)),
));
}
Entry::Vacant(vacant) => {
// Create the HNSW index
let index = HnswIndex::new(config.clone())?;
let entry = VectorIndexEntry {
index: Arc::new(index),
config: config.clone(),
};
vacant.insert(entry);
}
}
Ok(())
}
/// Check if any vector indexing is enabled.
///
/// Returns `true` if at least one property has a vector index.
pub fn is_vector_index_enabled(&self) -> bool {
!self.vector_indexes.is_empty()
}
/// Check if vector indexing is enabled for a specific property.
///
/// Returns `true` if the property has a vector index configured.
pub fn is_vector_index_enabled_for(&self, property_name: &str) -> bool {
self.vector_indexes.contains_key(property_name)
}
/// Get the first/default property name that is currently indexed.
///
/// Equivalent to `get_default_vector_property_name` (internal).
/// For multi-property setups, use [`list_vector_indexes`](Self::list_vector_indexes) instead.
pub fn get_indexed_property_name(&self) -> Option<String> {
self.get_default_vector_property_name()
}
/// List all configured vector indexes.
///
/// Returns information about each enabled vector index including
/// property name, dimensions, and distance metric.
pub fn list_vector_indexes(&self) -> Vec<VectorIndexInfo> {
self.vector_indexes
.iter()
.map(|entry| VectorIndexInfo {
property_name: entry.key().clone(),
dimensions: entry.value().config.dimensions,
distance_metric: entry.value().config.metric,
})
.collect()
}
/// Check if a vector index is enabled for a specific property.
///
/// Equivalent to [`is_vector_index_enabled_for`](Self::is_vector_index_enabled_for).
pub fn has_vector_index(&self, property_name: &str) -> bool {
self.is_vector_index_enabled_for(property_name)
}
/// Get the HNSW configuration for a specific property's vector index.
///
/// # Arguments
///
/// * `property_name` - The property name to get configuration for
///
/// # Returns
///
/// `Some(HnswConfig)` if a vector index exists for this property, `None` otherwise.
pub fn get_hnsw_config_for(&self, property_name: &str) -> Option<HnswConfig> {
self.vector_indexes
.get(property_name)
.map(|entry| entry.config.clone())
}
/// Register a vector index (used during index loading from disk).
///
/// This directly inserts the index without any initialization logic.
pub(crate) fn register_vector_index(
&self,
property_name: &str,
index: crate::index::vector::HnswIndex,
config: crate::index::vector::HnswConfig,
) {
let index_arc = Arc::new(index);
// Insert into multi-property map
self.vector_indexes.insert(
property_name.to_string(),
VectorIndexEntry {
index: Arc::clone(&index_arc),
config: config.clone(),
},
);
}
/// Get a reference to the HNSW index and its config for a specific property.
///
/// Used for persistence operations. Returns (index, config, vector_count, mappings).
#[allow(clippy::type_complexity)]
pub(crate) fn get_vector_index_for_persistence(
&self,
property_name: &str,
) -> Option<(
Arc<crate::index::vector::HnswIndex>,
crate::index::vector::HnswConfig,
usize,
Vec<(u64, u64)>,
)> {
use crate::index::vector::VectorIndex;
self.vector_indexes.get(property_name).map(|entry| {
let index = entry.value().index.clone();
let config = entry.value().config.clone();
let count = index.len();
// Extract ID mappings from the index
let mappings = index.get_id_mappings();
(index, config, count, mappings)
})
}
/// Try to add a node's vectors to all enabled indexes.
///
/// For each enabled vector index, checks if the node has that property
/// and adds it to the index if so.
///
/// Returns Ok(true) if any vector was indexed, Ok(false) if none applicable,
/// Err on failure (will have already done partial work).
fn try_index_vector(&self, node_id: NodeId, properties: &PropertyMap) -> Result<bool> {
let mut indexed_any = false;
// Index in all multi-property indexes
for entry in self.vector_indexes.iter() {
let prop_name = entry.key();
if let Some(vector) = properties.get(prop_name).and_then(|v| v.as_vector()) {
let index = Arc::clone(&entry.value().index);
index.add(node_id, vector)?;
indexed_any = true;
}
}
Ok(indexed_any)
}
/// Try to remove a node from all vector indexes.
///
/// Returns Ok(true) if removed from any index, Ok(false) if not applicable.
fn try_remove_from_index(&self, node_id: NodeId) -> Result<bool> {
let mut removed_any = false;
// Remove from all multi-property indexes
for entry in self.vector_indexes.iter() {
let index = Arc::clone(&entry.value().index);
// Remove may fail if node wasn't in this index, which is OK
if index.remove(node_id).is_ok() {
removed_any = true;
}
}
Ok(removed_any)
}
/// Update the vector indexes when node properties change.
///
/// For each enabled vector index, handles add/remove/update based on
/// whether the property changed.
fn update_vector_index(
&self,
node_id: NodeId,
new_props: &PropertyMap,
old_props: &PropertyMap,
) -> Result<()> {
// Update all multi-property indexes
for entry in self.vector_indexes.iter() {
let prop_name = entry.key();
let index = Arc::clone(&entry.value().index);
let old_vec = old_props.get(prop_name).and_then(|v| v.as_vector());
let new_vec = new_props.get(prop_name).and_then(|v| v.as_vector());
match (old_vec, new_vec) {
(None, None) => {} // No change for this property
(None, Some(v)) => {
index.add(node_id, v)?;
}
(Some(_), None) => {
let _ = index.remove(node_id); // May not exist, ignore error
}
(Some(o), Some(n)) => {
if o != n {
// Note: HnswIndex::add() is an upsert operation (remove + add internally)
index.add(node_id, n)?;
}
}
}
}
Ok(())
}
/// Create a node with the given label and properties.
///
/// Returns the ID of the newly created node.
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn create_node(&self, label: &str, properties: PropertyMap) -> Result<NodeId> {
// Synchronize with snapshot creation
let _lock = self.snapshot_lock.read();
let node_id = NodeId::new_unchecked(self.node_id_gen.next()?);
let version_id = VersionId::new_unchecked(self.version_id_gen.next()?);
let label_interned = GLOBAL_INTERNER.intern(label)?;
// CRITICAL: Index vector BEFORE inserting node. If vector indexing fails,
// we have not modified any graph state, so we can safely return error without rollback.
// This also avoids unnecessary clones of Node and PropertyMap.
self.try_index_vector(node_id, &properties)?;
let node = Node::new(node_id, label_interned, properties, version_id);
self.indexes.insert_node(node);
Ok(node_id)
}
/// Create an edge between two nodes.
///
/// Returns the ID of the newly created edge.
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn create_edge(
&self,
source: NodeId,
target: NodeId,
label: &str,
properties: PropertyMap,
) -> Result<EdgeId> {
// Synchronize with snapshot creation
let _lock = self.snapshot_lock.read();
// Verify nodes exist
if !self.indexes.contains_node(source) {
return Err(StorageError::NodeNotFound(source).into());
}
if !self.indexes.contains_node(target) {
return Err(StorageError::NodeNotFound(target).into());
}
let edge_id = EdgeId::new_unchecked(self.edge_id_gen.next()?);
let version_id = VersionId::new_unchecked(self.version_id_gen.next()?);
let label_interned = GLOBAL_INTERNER.intern(label)?;
let edge = Edge::new(
edge_id,
label_interned,
source,
target,
properties,
version_id,
);
self.indexes.insert_edge(edge);
// Adjacency indexes are now rebuilt lazily on first access (see CurrentIndexes)
// This eliminates O(n² log n) performance regression for batch operations
Ok(edge_id)
}
/// Get a node by ID.
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn get_node(&self, id: NodeId) -> Result<Node> {
self.indexes
.get_node(id)
.ok_or_else(|| StorageError::NodeNotFound(id).into())
}
/// Access a node without cloning, executing a closure on the node data.
///
/// This method provides zero-copy read access to node data for hot paths
/// where only specific fields are needed.
///
/// # Performance
///
/// - **No allocation**: Does not clone the Node
/// - **No Arc increment**: Does not increment PropertyMap reference count (unless cloned in closure)
/// - **Lock duration**: Holds DashMap read lock only during closure execution
///
/// # Safety & Deadlocks
///
/// **WARNING**: The closure is executed while holding a read lock on the node shard.
/// Do NOT attempt to modify the graph or perform operations that might acquire a
/// write lock on the same shard (e.g., `update_node`, `delete_node`) within the closure.
/// Doing so will cause a deadlock (lock re-entrancy hazard).
#[inline]
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn with_node<F, R>(&self, id: NodeId, f: F) -> Result<R>
where
F: FnOnce(&Node) -> R,
{
self.indexes
.with_node(id, f)
.ok_or_else(|| StorageError::NodeNotFound(id).into())
}
/// Get an edge by ID.
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn get_edge(&self, id: EdgeId) -> Result<Edge> {
self.indexes
.get_edge(id)
.ok_or_else(|| StorageError::EdgeNotFound(id).into())
}
// ========================================================================
// Zero-copy access methods (Issue #190)
//
// These methods provide efficient access to node/edge data without cloning
// the entire structure. Use these for hot paths where you only need to
// read specific fields.
// ========================================================================
/// Get the target node of an edge without cloning the entire edge.
///
/// # Performance
///
/// - **Zero-copy**: Only reads and returns the target NodeId (8 bytes)
/// - **No allocation**: Does not clone Edge or PropertyMap
#[inline]
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn get_edge_target(&self, id: EdgeId) -> Result<NodeId> {
self.indexes
.get_edge_target(id)
.ok_or_else(|| StorageError::EdgeNotFound(id).into())
}
/// Get the source node of an edge without cloning the entire edge.
///
/// # Performance
///
/// - **Zero-copy**: Only reads and returns the source NodeId (8 bytes)
/// - **No allocation**: Does not clone Edge or PropertyMap
#[inline]
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn get_edge_source(&self, id: EdgeId) -> Result<NodeId> {
self.indexes
.get_edge_source(id)
.ok_or_else(|| StorageError::EdgeNotFound(id).into())
}
/// Get the endpoints (source, target) of an edge without cloning.
///
/// # Performance
///
/// - **Zero-copy**: Only reads and returns two NodeIds (16 bytes)
/// - **No allocation**: Does not clone Edge or PropertyMap
#[inline]
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn get_edge_endpoints(&self, id: EdgeId) -> Result<(NodeId, NodeId)> {
self.indexes
.get_edge_endpoints(id)
.ok_or_else(|| StorageError::EdgeNotFound(id).into())
}
/// Get the label of an edge without cloning the entire edge.
///
/// # Performance
///
/// - **Zero-copy**: Only reads and returns the label (8 bytes)
/// - **No allocation**: Does not clone Edge or PropertyMap
#[inline]
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn get_edge_label(&self, id: EdgeId) -> Result<InternedString> {
self.indexes
.get_edge_label(id)
.ok_or_else(|| StorageError::EdgeNotFound(id).into())
}
/// Get the label of a node without cloning the entire node.
///
/// # Performance
///
/// - **Zero-copy**: Only reads and returns the label (8 bytes)
/// - **No allocation**: Does not clone Node or PropertyMap
#[inline]
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn get_node_label(&self, id: NodeId) -> Result<InternedString> {
self.indexes
.get_node_label(id)
.ok_or_else(|| StorageError::NodeNotFound(id).into())
}
/// Delete a node.
///
/// # Important
///
/// This method does NOT delete edges connected to the node. This may leave
/// orphaned edges in the graph. For most use cases, prefer using
/// [`crate::api::transaction::WriteOps::delete_node_cascade`] which automatically removes
/// all connected edges to maintain referential integrity.
///
/// Only use this method if you explicitly need to preserve edges for some
/// specialized use case (e.g., maintaining edge history for audit purposes).
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn delete_node(&self, id: NodeId) -> Result<Node> {
// Synchronize with snapshot creation
let _lock = self.snapshot_lock.read();
let node = self
.indexes
.remove_node(id)
.ok_or(StorageError::NodeNotFound(id))?;
// Best-effort vector index removal (ignore errors) - Issue #323
// This prevents memory leaks and ensures deleted nodes don't appear in similarity searches
// Note: Temporal vector index cleanup is only available in delete_node_direct()
// which has timestamp context from the transaction. Non-transactional deletes
// via this method don't have temporal semantics.
let _ = self.try_remove_from_index(id);
Ok(node)
}
/// Delete an edge.
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn delete_edge(&self, id: EdgeId) -> Result<Edge> {
// Synchronize with snapshot creation
let _lock = self.snapshot_lock.read();
let edge = self
.indexes
.remove_edge(id)
.ok_or(StorageError::EdgeNotFound(id))?;
// Adjacency indexes are now rebuilt lazily on first access (see CurrentIndexes)
// This eliminates O(n² log n) performance regression for batch operations
Ok(edge)
}
// Direct insert/update/delete methods for transaction commit
// These methods are used by WriteTransaction to apply buffered changes
/// Insert a node directly (used by WriteTransaction).
/// Does not generate IDs - caller must provide them.
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn insert_node_direct(&self, node: Node, timestamp: Timestamp) -> Result<()> {
// Synchronize with snapshot creation
let _lock = self.snapshot_lock.read();
// CRITICAL: Index vector BEFORE inserting node. If vector indexing fails,
// we have not modified any graph state, so we can safely return error without rollback.
// This prevents the VS-030 bug where transaction-created nodes bypassed indexing,
// causing them to be missing from HNSW index and invisible to find_similar queries.
self.try_index_vector(node.id, &node.properties)?;
// Index in temporal vector index if enabled
self.try_index_temporal_vector(node.id, &node.properties, timestamp)?;
// Vector indexing succeeded, now insert the node into the main indexes.
self.indexes.insert_node(node);
Ok(())
}
/// Insert an edge directly (used by WriteTransaction).
/// Does not generate IDs or rebuild adjacency - caller must handle.
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn insert_edge_direct(&self, edge: Edge) -> Result<()> {
// Synchronize with snapshot creation
let _lock = self.snapshot_lock.read();
self.indexes.insert_edge(edge);
Ok(())
}
/// Update a node directly (used by WriteTransaction).
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn update_node_direct(&self, node: Node, timestamp: Timestamp) -> Result<()> {
// Synchronize with snapshot creation
let _lock = self.snapshot_lock.read();
// Save old node for vector index update
let old_props = self.indexes.with_node(node.id, |n| n.properties.clone());
// Update vector index BEFORE updating node in main indexes.
// If vector indexing fails, we haven't modified any state yet.
if let Some(old_p) = old_props {
self.update_vector_index(node.id, &node.properties, &old_p)?;
}
// Update temporal vector index if enabled
self.try_index_temporal_vector(node.id, &node.properties, timestamp)?;
// Finally, insert node into main indexes. This avoids node.clone().
self.indexes.insert_node(node);
Ok(())
}
/// Finalize the commit timestamp on a stored node after successful `apply_changes`.
///
/// During `apply_changes`, nodes are written with `commit_timestamp: None` (pending).
/// This method is called after the full apply succeeds to atomically mark the node
/// as committed, making it visible to future snapshot readers.
pub fn set_node_commit_timestamp(&self, node_id: NodeId, ts: Timestamp) {
self.indexes
.with_node_mut(node_id, |n| n.metadata.commit_timestamp = Some(ts));
}
/// Finalize the commit timestamp on a stored edge after successful `apply_changes`.
pub fn set_edge_commit_timestamp(&self, edge_id: EdgeId, ts: Timestamp) {
self.indexes
.with_edge_mut(edge_id, |e| e.metadata.commit_timestamp = Some(ts));
}
/// Update an edge directly (used by WriteTransaction).
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn update_edge_direct(&self, edge: Edge) -> Result<()> {
// Synchronize with snapshot creation
let _lock = self.snapshot_lock.read();
// Remove old version and insert new
self.indexes.insert_edge(edge);
Ok(())
}
/// Delete a node directly (used by WriteTransaction).
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn delete_node_direct(&self, id: NodeId, timestamp: Timestamp) -> Result<()> {
// Synchronize with snapshot creation
let _lock = self.snapshot_lock.read();
self.indexes
.remove_node(id)
.ok_or(StorageError::NodeNotFound(id))?;
// Best-effort vector index removal (ignore errors)
let _ = self.try_remove_from_index(id);
// Remove from temporal vector index if enabled
let _ = self.try_remove_temporal_vector(id, timestamp);
Ok(())
}
/// Delete an edge directly (used by WriteTransaction).
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn delete_edge_direct(&self, id: EdgeId) -> Result<()> {
// Synchronize with snapshot creation
let _lock = self.snapshot_lock.read();
self.indexes
.remove_edge(id)
.ok_or(StorageError::EdgeNotFound(id))?;
Ok(())
}
/// Rebuild adjacency indexes from current edges.
///
/// This should be called after batch edge operations to update the
/// adjacency indexes for efficient graph traversal.
///
/// # Concurrency Safety
///
/// This method is safe to call concurrently with read operations:
/// - Uses `RwLock` on adjacency indexes for safe concurrent access
/// - Readers can continue traversing old indexes while rebuild occurs
/// - New index is swapped in atomically when rebuild completes
/// - No stale reads: readers either see old (consistent) or new (consistent) state
///
/// However, concurrent writes should be serialized at a higher level
/// (e.g., through transaction isolation) to prevent race conditions.
///
/// Compact adjacency indexes to merge delta buffer into frozen CSR.
///
/// With incremental adjacency, edges are immediately visible without compaction.
/// This method improves read performance by moving delta edges to frozen CSR.
///
/// # When to Call
///
/// - After bulk inserts (to move many edges from delta to frozen)
/// - To reduce delta size before persistence
/// - Usually not needed if background compaction is enabled
///
/// # Performance
///
/// Complexity: O(D log D) where D is the number of delta edges.
/// Typically much faster than the old O(E log E) rebuild where E is total edges.
pub fn compact_adjacency(&self) {
self.indexes.compact_adjacency();
}
/// Get the current number of delta edges (test-only).
///
/// Delta edges are insertions not yet compacted into frozen CSR.
#[cfg(test)]
pub(crate) fn delta_edge_count(&self) -> usize {
self.indexes.delta_edge_count()
}
/// Rebuild adjacency indexes (deprecated - use `compact_adjacency` instead).
///
/// This method is kept for backward compatibility and simply calls `compact_adjacency`.
/// Edges are always immediately visible without calling this method.
#[deprecated(since = "0.1.0", note = "Use compact_adjacency() instead")]
pub fn rebuild_adjacency(&self) {
self.compact_adjacency();
}
/// Get a frozen view for outgoing adjacency (read transaction hot path).
///
/// Returns `Some(FrozenAdjacencyView)` if the index is in a clean state
/// (no pending delta edges or tombstones), allowing direct slice access.
/// Returns `None` if there are pending delta operations.
///
/// # Performance
///
/// When available, frozen view provides ~8-14ns access vs ~16-17ns for
/// the merged path. Use this for read-only workloads.
#[inline]
pub fn frozen_outgoing_view(
&self,
) -> Option<crate::index::incremental_adjacency::FrozenAdjacencyView> {
self.indexes.frozen_outgoing_view()
}
/// Get a frozen view for incoming adjacency (read transaction hot path).
///
/// Same as `frozen_outgoing_view()` but for incoming edges.
#[inline]
pub fn frozen_incoming_view(
&self,
) -> Option<crate::index::incremental_adjacency::FrozenAdjacencyView> {
self.indexes.frozen_incoming_view()
}
/// Get all outgoing edges from a node.
///
/// This is the critical "hot path" operation that must be fast.
/// Uses frozen view (~8-14ns) when available, falls back to merged guard (~16-17ns).
#[inline]
pub fn get_outgoing_edges(&self, source: NodeId) -> Vec<EdgeId> {
// HOT PATH: Use frozen view for direct slice access when no delta/tombstones
if let Some(frozen) = self.indexes.frozen_outgoing_view() {
return frozen
.get_adjacency(source)
.iter()
.map(|entry| entry.edge_id)
.collect();
}
// SLOW PATH: Use merged guard when delta/tombstones exist.
// Pre-allocate using capacity hint to avoid multiple reallocations.
let guard = self.indexes.get_outgoing(source);
let mut result = Vec::with_capacity(guard.capacity_hint());
result.extend(guard.iter().map(|entry| entry.edge_id));
result
}
/// Get all incoming edges to a node.
///
/// Uses frozen view (~8-14ns) when available, falls back to merged guard (~16-17ns).
#[inline]
pub fn get_incoming_edges(&self, target: NodeId) -> Vec<EdgeId> {
// HOT PATH: Use frozen view for direct slice access when no delta/tombstones
if let Some(frozen) = self.indexes.frozen_incoming_view() {
return frozen
.get_adjacency(target)
.iter()
.map(|entry| entry.edge_id)
.collect();
}
// SLOW PATH: Use merged guard when delta/tombstones exist.
// Pre-allocate using capacity hint to avoid multiple reallocations.
let guard = self.indexes.get_incoming(target);
let mut result = Vec::with_capacity(guard.capacity_hint());
result.extend(guard.iter().map(|entry| entry.edge_id));
result
}
/// Get outgoing edges with a specific label.
#[inline]
pub fn get_outgoing_edges_with_label(&self, source: NodeId, label: &str) -> Vec<EdgeId> {
let label_id = match GLOBAL_INTERNER.get_id(label) {
Some(id) => id,
None => return Vec::new(), // Label doesn't exist
};
// Optimized to avoid intermediate Vec allocation and pre-allocate result
let guard = self.indexes.get_outgoing(source);
let mut result = Vec::with_capacity(guard.capacity_hint());
result.extend(
guard
.iter()
.filter(|entry| entry.label == label_id)
.map(|entry| entry.edge_id),
);
result
}
/// Get incoming edges with a specific label.
#[inline]
pub fn get_incoming_edges_with_label(&self, target: NodeId, label: &str) -> Vec<EdgeId> {
let label_id = match GLOBAL_INTERNER.get_id(label) {
Some(id) => id,
None => return Vec::new(),
};
// Optimized to avoid intermediate Vec allocation and pre-allocate result
let guard = self.indexes.get_incoming(target);
let mut result = Vec::with_capacity(guard.capacity_hint());
result.extend(
guard
.iter()
.filter(|entry| entry.label == label_id)
.map(|entry| entry.edge_id),
);
result
}
/// Get all outgoing edges from a node as an iterator.
///
/// This is a zero-allocation alternative to [`Self::get_outgoing_edges`] that returns
/// an iterator instead of collecting into a `Vec`. Use this for performance-critical
/// traversals where you don't need to store all edges.
///
/// # Performance
///
/// - No `Vec` allocation (saves 100-500ns per call)
/// - Lazy evaluation - only computes what you consume
/// - Can be short-circuited with `.take()`, `.find()`, etc.
///
/// # Example
///
/// ```ignore
/// // Get first outgoing edge without allocating a Vec
/// let first_edge = storage.get_outgoing_edges_iter(node).next();
///
/// // Count edges without allocation
/// let count = storage.get_outgoing_edges_iter(node).count();
/// ```
#[inline]
pub fn get_outgoing_edges_iter(&self, source: NodeId) -> OutgoingEdgesIter<'_> {
OutgoingEdgesIter::new(self.indexes.get_outgoing(source))
}
/// Get all incoming edges to a node as an iterator.
///
/// This is a zero-allocation alternative to [`Self::get_incoming_edges`] that returns
/// an iterator instead of collecting into a `Vec`.
///
/// See [`Self::get_outgoing_edges_iter`] for performance details and usage examples.
#[inline]
pub fn get_incoming_edges_iter(&self, target: NodeId) -> IncomingEdgesIter<'_> {
IncomingEdgesIter::new(self.indexes.get_incoming(target))
}
/// Get outgoing edges with a specific label as an iterator.
///
/// This is a zero-allocation alternative to [`Self::get_outgoing_edges_with_label`].
///
/// Returns an empty iterator if the label doesn't exist.
#[inline]
pub fn get_outgoing_edges_with_label_iter(
&self,
source: NodeId,
label: &str,
) -> OutgoingEdgesWithLabelIter<'_> {
let label_id = GLOBAL_INTERNER.get_id(label);
OutgoingEdgesWithLabelIter::new(self.indexes.get_outgoing(source), label_id)
}
/// Get incoming edges with a specific label as an iterator.
///
/// This is a zero-allocation alternative to [`Self::get_incoming_edges_with_label`].
///
/// Returns an empty iterator if the label doesn't exist.
#[inline]
pub fn get_incoming_edges_with_label_iter(
&self,
target: NodeId,
label: &str,
) -> IncomingEdgesWithLabelIter<'_> {
let label_id = GLOBAL_INTERNER.get_id(label);
IncomingEdgesWithLabelIter::new(self.indexes.get_incoming(target), label_id)
}
/// Export outgoing CSR adjacency data for persistence.
pub fn export_outgoing_csr(&self) -> (Vec<u64>, Vec<u64>, Vec<u64>) {
self.indexes.export_outgoing_csr()
}
/// Export incoming CSR adjacency data for persistence.
pub fn export_incoming_csr(&self) -> (Vec<u64>, Vec<u64>, Vec<u64>) {
self.indexes.export_incoming_csr()
}
/// Import CSR adjacency data from persistence.
///
/// This bypasses the need to rebuild adjacency structures from scratch.
pub fn import_csr(
&self,
outgoing_node_ids: Vec<u64>,
outgoing_offsets: Vec<u64>,
outgoing_edge_ids: Vec<u64>,
incoming_node_ids: Vec<u64>,
incoming_offsets: Vec<u64>,
incoming_edge_ids: Vec<u64>,
) {
self.indexes.import_csr(
outgoing_node_ids,
outgoing_offsets,
outgoing_edge_ids,
incoming_node_ids,
incoming_offsets,
incoming_edge_ids,
);
}
/// Get the number of nodes.
#[inline]
pub fn node_count(&self) -> usize {
self.indexes.node_count()
}
/// Get the number of edges.
#[inline]
pub fn edge_count(&self) -> usize {
self.indexes.edge_count()
}
/// Get the out-degree of a node.
#[inline]
pub fn out_degree(&self, node: NodeId) -> usize {
self.indexes.out_degree(node)
}
/// Get the in-degree of a node.
#[inline]
pub fn in_degree(&self, node: NodeId) -> usize {
self.indexes.in_degree(node)
}
/// Get the default property name for vector indexing.
///
/// This is used to maintain backward compatibility for legacy APIs that
/// don't specify a property name. It deterministically selects the
/// alphabetically first property name from the enabled vector indexes.
///
/// Returns `None` if no vector indexes are enabled.
fn get_default_vector_property_name(&self) -> Option<String> {
if self.vector_indexes.is_empty() {
return None;
}
// Optimization: if len == 1, just take it (no sorting needed)
if self.vector_indexes.len() == 1 {
return self.vector_indexes.iter().next().map(|r| r.key().clone());
}
// Find min key alphabetically
// Note: DashMap iteration order is not guaranteed, so we must scan all keys
// We use `.fold` instead of `.min_by` to prevent deadlocks. `.min_by` holds
// multiple `RefMulti` read guards across iteration steps, which can block
// concurrent writers indefinitely. `.fold` immediately drops the guard for
// each element after extracting the necessary data.
self.vector_indexes
.iter()
.fold(None, |min: Option<String>, current| {
let key = current.key();
match min {
Some(m) if m.as_str() <= key.as_str() => Some(m),
_ => Some(key.clone()),
}
})
}
/// Get or create filter statistics for a label (Issue #334).
///
/// Returns an Arc to the FilterStats for the given label, creating it if needed.
/// This enables adaptive over-fetch multiplier calculation based on historical
/// filter pass rates.
fn get_or_create_filter_stats(&self, label: &str) -> Arc<FilterStats> {
self.filter_stats
.entry(label.to_string())
.or_insert_with(|| Arc::new(FilterStats::new()))
.value()
.clone()
}
/// Calculate adaptive over-fetch candidates for filtered search (Issue #334).
///
/// Returns the number of candidates to fetch and the FilterStats for recording results.
/// This method centralizes the adaptive over-fetch logic used by all filtered search methods.
///
/// # Arguments
///
/// * `k` - Number of results requested by the user
/// * `label` - Label to filter by
///
/// # Returns
///
/// Tuple of (candidates_to_fetch, stats) where:
/// - `candidates_to_fetch` is the adaptive number of candidates to retrieve from HNSW
/// - `stats` is the FilterStats for recording search results
///
/// # Algorithm
///
/// - Uses adaptive multiplier based on historical pass rate (5x to 50x)
/// - Guarantees minimum of k + 20 candidates
/// - Caps maximum at k + 1000 to prevent excessive memory usage
fn calculate_adaptive_candidates(&self, k: usize, label: &str) -> (usize, Arc<FilterStats>) {
const MIN_ABSOLUTE_OVERFETCH: usize = 20;
const MAX_ABSOLUTE_OVERFETCH: usize = 1000;
let stats = self.get_or_create_filter_stats(label);
let multiplier = stats.get_adaptive_multiplier();
let candidates = ((k as f64 * multiplier) as usize)
.max(k + MIN_ABSOLUTE_OVERFETCH)
.min(k + MAX_ABSOLUTE_OVERFETCH);
(candidates, stats)
}
/// Get filter statistics for a label (test-only helper).
///
/// Returns the current statistics (search_count, total_candidates, total_results)
/// for the given label, or None if no searches have been performed yet.
///
/// This is used for testing to verify that adaptive learning is working correctly.
pub(crate) fn get_filter_stats(&self, label: &str) -> Option<(u64, u64, u64)> {
use std::sync::atomic::Ordering;
self.filter_stats.get(label).map(|entry| {
let stats = entry.value();
(
stats.search_count.load(Ordering::Relaxed),
stats.total_candidates.load(Ordering::Relaxed),
stats.total_results.load(Ordering::Relaxed),
)
})
}
/// Find k most similar nodes to the query node based on vector similarity.
///
/// Returns a list of (NodeId, score) pairs sorted by similarity (highest first).
/// The query node itself is excluded from results.
///
/// # Errors
///
/// Returns an error if:
/// - Vector index is not enabled
/// - Query node is not found
/// - Query node does not have the indexed vector property
/// - The property is not a vector
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn find_similar(&self, query_node_id: NodeId, k: usize) -> Result<Vec<(NodeId, f32)>> {
let (prop_name, index, _) = self.get_vector_index_internal(None)?;
let query_vector = self.get_node_vector(query_node_id, &prop_name)?;
self.run_vector_search(&index, &query_vector, k, None, Some(query_node_id))
}
/// Find k most similar nodes with a specific label.
///
/// Returns a list of (NodeId, score) pairs sorted by similarity (highest first).
/// Only nodes with the specified label are returned. The query node is excluded.
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn find_similar_with_label(
&self,
query_node_id: NodeId,
label: &str,
k: usize,
) -> Result<Vec<(NodeId, f32)>> {
let (prop_name, index, _) = self.get_vector_index_internal(None)?;
let query_vector = self.get_node_vector(query_node_id, &prop_name)?;
self.run_vector_search(&index, &query_vector, k, Some(label), Some(query_node_id))
}
/// Find k most similar nodes to a raw embedding vector.
///
/// This is useful when searching with embeddings that don't correspond to any
/// existing node in the graph (e.g., query embeddings from external sources).
///
/// # Arguments
///
/// * `embedding` - The query embedding vector
/// * `k` - Maximum number of results to return
///
/// # Returns
///
/// A list of (NodeId, similarity_score) pairs sorted by similarity (highest first).
///
/// # Errors
///
/// Returns an error if:
/// - Vector index is not enabled
/// - Embedding dimensions don't match the indexed property
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn find_similar_by_embedding(
&self,
embedding: &[f32],
k: usize,
) -> Result<Vec<(NodeId, f32)>> {
let (_, index, config) = self.get_vector_index_internal(None)?;
Self::validate_embedding_dimensions(embedding, &config)?;
self.run_vector_search(&index, embedding, k, None, None)
}
/// Find k most similar nodes with a specific label to a raw embedding vector.
///
/// Like `find_similar_by_embedding()`, but filters results to only include
/// nodes with the specified label.
///
/// # Arguments
///
/// * `embedding` - The query embedding vector
/// * `label` - Only return nodes with this label
/// * `k` - Maximum number of results to return
///
/// # Returns
///
/// A list of (NodeId, similarity_score) pairs sorted by similarity (highest first).
/// All results have the specified label.
///
/// # Errors
///
/// Returns an error if:
/// - Vector index is not enabled
/// - Embedding dimensions don't match the indexed property
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn find_similar_by_embedding_with_label(
&self,
embedding: &[f32],
label: &str,
k: usize,
) -> Result<Vec<(NodeId, f32)>> {
let (_, index, config) = self.get_vector_index_internal(None)?;
Self::validate_embedding_dimensions(embedding, &config)?;
self.run_vector_search(&index, embedding, k, Some(label), None)
}
/// Find k most similar nodes to a raw embedding in a specific property's index.
///
/// This is the property-specific version of `find_similar_by_embedding()`.
/// Use this when you have multiple vector indexes and need to search a specific one.
///
/// # Arguments
///
/// * `property_name` - The property with the vector index to search
/// * `embedding` - The query embedding vector
/// * `k` - Maximum number of results to return
///
/// # Returns
///
/// A list of (NodeId, similarity_score) pairs sorted by similarity (highest first).
///
/// # Errors
///
/// Returns an error if:
/// - No vector index is enabled for the specified property
/// - Embedding dimensions don't match the indexed property
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn find_similar_by_embedding_in(
&self,
property_name: &str,
embedding: &[f32],
k: usize,
) -> Result<Vec<(NodeId, f32)>> {
let (_, index, config) = self.get_vector_index_internal(Some(property_name))?;
Self::validate_embedding_dimensions(embedding, &config)?;
self.run_vector_search(&index, embedding, k, None, None)
}
/// Find k most similar nodes with a label to a raw embedding in a specific property's index.
///
/// This is the property-specific version of `find_similar_by_embedding_with_label()`.
///
/// # Arguments
///
/// * `property_name` - The property with the vector index to search
/// * `embedding` - The query embedding vector
/// * `label` - Only return nodes with this label
/// * `k` - Maximum number of results to return
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn find_similar_by_embedding_in_with_label(
&self,
property_name: &str,
embedding: &[f32],
label: &str,
k: usize,
) -> Result<Vec<(NodeId, f32)>> {
let (_, index, config) = self.get_vector_index_internal(Some(property_name))?;
Self::validate_embedding_dimensions(embedding, &config)?;
self.run_vector_search(&index, embedding, k, Some(label), None)
}
/// Find k most similar nodes with a custom predicate.
///
/// This allows filtering candidates based on arbitrary criteria (e.g., property values).
/// The predicate is called for each candidate node. If it returns true, the node is included.
///
/// # Arguments
///
/// * `property_name` - The property with the vector index to search
/// * `query` - The query embedding vector
/// * `k` - Maximum number of results to return
/// * `predicate` - A closure that takes a `NodeId` and returns `true` if the node should be included
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn find_similar_with_predicate<F>(
&self,
property_name: &str,
query: &[f32],
k: usize,
predicate: F,
) -> Result<Vec<(NodeId, f32)>>
where
F: Fn(&NodeId) -> bool + Send + Sync,
{
let (_, index, config) = self.get_vector_index_internal(Some(property_name))?;
Self::validate_embedding_dimensions(query, &config)?;
index.search_with_filter(query, k, predicate)
}
// ========================================================================
// Multi-Property Vector Search Methods (Issue #389)
// ========================================================================
/// Find k most similar nodes in a specific property's vector index.
///
/// Use this method when you have multiple vector indexes and need to search
/// a specific one. The property must have a vector index enabled.
///
/// # Arguments
///
/// * `property_name` - The indexed property to search
/// * `query_node_id` - The node to find similar nodes for
/// * `k` - Maximum number of results to return
///
/// # Returns
///
/// A list of (NodeId, similarity_score) pairs sorted by similarity (highest first).
/// The query node itself is excluded from results.
///
/// # Errors
///
/// Returns an error if:
/// - No vector index is enabled for the specified property
/// - Query node is not found
/// - Query node does not have the specified property
/// - The property value is not a vector
///
/// # Example
///
/// ```ignore
/// // Search title embeddings for similar nodes
/// let similar = storage.find_similar_in("title_embedding", node_id, 10)?;
///
/// // Search body embeddings (different property, potentially different results)
/// let similar_body = storage.find_similar_in("body_embedding", node_id, 10)?;
/// ```
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn find_similar_in(
&self,
property_name: &str,
query_node_id: NodeId,
k: usize,
) -> Result<Vec<(NodeId, f32)>> {
let (_, index, _) = self.get_vector_index_internal(Some(property_name))?;
let query_vector = self.get_node_vector(query_node_id, property_name)?;
self.run_vector_search(&index, &query_vector, k, None, Some(query_node_id))
}
/// Search a specific property's vector index with a raw embedding.
/// Search a specific property's vector index with a raw embedding.
///
/// Equivalent to [`find_similar_by_embedding_in`](Self::find_similar_by_embedding_in).
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn search_vectors_in(
&self,
property_name: &str,
embedding: &[f32],
k: usize,
) -> Result<Vec<(NodeId, f32)>> {
self.find_similar_by_embedding_in(property_name, embedding, k)
}
// ========================================================================
// Temporal Vector Indexing (Phase 3)
// ========================================================================
/// Enable temporal vector indexing for a specific property.
///
/// Once enabled, vector changes will be tracked over time using snapshot-based
/// indexing, enabling point-in-time vector queries and semantic drift tracking.
///
/// # Arguments
///
/// * `property_name` - Name of the property containing vectors
/// * `config` - Temporal vector index configuration
///
/// # Errors
///
/// Returns an error if temporal vector indexing is already enabled.
///
/// # Example
///
/// ```ignore
/// use aletheiadb::index::vector::temporal::{TemporalVectorConfig, SnapshotStrategy};
/// use aletheiadb::index::vector::HnswConfig;
///
/// let hnsw_config = HnswConfig::new(384, DistanceMetric::Cosine);
/// let temporal_config = TemporalVectorConfig::default_with_hnsw(hnsw_config);
/// storage.enable_temporal_vector_index("embedding", temporal_config)?;
/// ```
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn enable_temporal_vector_index(
&self,
property_name: &str,
config: TemporalVectorConfig,
) -> Result<()> {
// Check if already enabled for this specific property
if self.temporal_vector_indexes.contains_key(property_name) {
return Err(crate::core::error::Error::Vector(
crate::core::error::VectorError::IndexError(format!(
"Temporal vector index is already enabled for property '{}'",
property_name
)),
));
}
// Create temporal vector index wrapped in Arc for sharing
let index = Arc::new(TemporalVectorIndex::new(config.clone())?);
// Insert into multi-property DashMap
self.temporal_vector_indexes.insert(
property_name.to_string(),
TemporalVectorIndexEntry {
index: Arc::clone(&index),
config: config.clone(),
},
);
// Also update legacy state for backward compatibility
// (uses the most recently added index)
let mut state = self.temporal_vector_index_state.write();
state.index = Some(index);
state.property_name = Some(property_name.to_string());
state.config = Some(config);
Ok(())
}
/// Check if temporal vector indexing is enabled for any property.
pub fn is_temporal_vector_index_enabled(&self) -> bool {
!self.temporal_vector_indexes.is_empty()
}
/// Check if temporal vector indexing is enabled for a specific property.
pub fn is_temporal_vector_index_enabled_for(&self, property_name: &str) -> bool {
self.temporal_vector_indexes.contains_key(property_name)
}
/// List all property names that have temporal vector indexes enabled.
///
/// Returns a vector of property names that have temporal vector indexing configured.
pub fn list_temporal_vector_indexes(&self) -> Vec<String> {
self.temporal_vector_indexes
.iter()
.map(|entry| entry.key().clone())
.collect()
}
/// Get a reference to the temporal vector index for a specific property.
///
/// Returns `None` if temporal vector indexing is not enabled for that property.
pub(crate) fn get_temporal_vector_index_for(
&self,
property_name: &str,
) -> Option<Arc<TemporalVectorIndex>> {
self.temporal_vector_indexes
.get(property_name)
.map(|entry| Arc::clone(&entry.index))
}
/// Get a reference to the temporal vector index if enabled (legacy single-property API).
///
/// Returns `None` if temporal vector indexing is not enabled.
/// For multi-property support, use `get_temporal_vector_index_for` instead.
pub(crate) fn get_temporal_vector_index(&self) -> Option<Arc<TemporalVectorIndex>> {
let state = self.temporal_vector_index_state.read();
state.index.clone()
}
/// Find k most similar nodes at a specific point in time.
///
/// Returns nodes similar to the query embedding as they existed at the given timestamp.
///
/// # Arguments
///
/// * `embedding` - Query vector
/// * `k` - Number of results
/// * `timestamp` - Point in time to query
///
/// # Returns
///
/// Vector of (NodeId, similarity) pairs sorted by similarity (descending).
///
/// # Errors
///
/// Returns an error if:
/// - Temporal vector index is not enabled
/// - No snapshot exists at or before the timestamp
/// - Embedding dimensions don't match the indexed property
///
/// # Example
///
/// ```ignore
/// // Find similar documents as they existed in the past
/// let query_embedding = vec![0.1; 384];
/// let timestamp = 1234567890000000; // microseconds since epoch
/// let results = storage.find_similar_as_of(&query_embedding, 10, timestamp)?;
/// ```
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn find_similar_as_of(
&self,
embedding: &[f32],
k: usize,
timestamp: Timestamp,
) -> Result<Vec<(NodeId, f32)>> {
let state = self.temporal_vector_index_state.read();
let index = state.index.as_ref().ok_or_else(|| {
crate::core::error::Error::Vector(crate::core::error::VectorError::IndexError(
"Temporal vector index is not enabled. Call enable_temporal_vector_index() first."
.to_string(),
))
})?;
index.find_similar_as_of(embedding, k, timestamp)
}
/// Find k most similar nodes at a specific point in time for a specific property.
///
/// This is the property-specific version of [`find_similar_as_of`](Self::find_similar_as_of).
/// It validates that the requested property matches the property for which
/// the temporal vector index was enabled.
///
/// # Arguments
///
/// * `property_name` - The property containing the vector embeddings
/// * `embedding` - Query vector to find similar vectors to
/// * `k` - Number of results to return
/// * `timestamp` - The point in time to query
///
/// # Errors
///
/// Returns an error if:
/// - Temporal vector index is not enabled
/// - The property name doesn't match the indexed property
/// - Query embedding dimensions don't match
/// - No snapshot exists at the given timestamp
///
/// # Example
///
/// ```ignore
/// // Find similar documents as they existed in the past for a specific property
/// let query_embedding = vec![0.1; 384];
/// let timestamp = 1234567890000000;
/// let results = storage.find_similar_as_of_in("content_embedding", &query_embedding, 10, timestamp)?;
/// ```
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn find_similar_as_of_in(
&self,
property_name: &str,
embedding: &[f32],
k: usize,
timestamp: crate::core::temporal::Timestamp,
) -> Result<Vec<(crate::core::NodeId, f32)>> {
// Look up the temporal index for this specific property
let index = self
.get_temporal_vector_index_for(property_name)
.ok_or_else(|| {
crate::core::error::Error::Vector(crate::core::error::VectorError::IndexError(
format!(
"No temporal vector index enabled for property '{}'. \
Call db.vector_index(\"{}\").temporal(...).enable() first.",
property_name, property_name
),
))
})?;
index.find_similar_as_of(embedding, k, timestamp)
}
/// Track semantic drift for a node over time in a specific property's temporal index.
///
/// This method tracks how a node's embedding has changed relative to a reference
/// embedding over time. It validates that the requested property matches the
/// property for which the temporal vector index was enabled.
///
/// # Arguments
///
/// * `property_name` - The property containing the vector embeddings
/// * `node_id` - The node to track drift for
/// * `reference_embedding` - Reference vector to measure drift against
/// * `time_range` - The time range to search for drift
///
/// # Returns
///
/// A vector of (timestamp, drift_score) pairs showing how the node's embedding
/// drifted from the reference at each snapshot time.
///
/// # Errors
///
/// Returns an error if:
/// - Temporal vector index is not enabled
/// - The property name doesn't match the indexed property
/// - Reference embedding dimensions don't match
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn track_drift_in(
&self,
property_name: &str,
node_id: crate::core::NodeId,
reference_embedding: &[f32],
time_range: crate::core::temporal::TimeRange,
) -> Result<Vec<(crate::core::temporal::Timestamp, f32)>> {
// Look up the temporal index for this specific property (multi-property support)
let index = self
.get_temporal_vector_index_for(property_name)
.ok_or_else(|| {
crate::core::error::Error::Vector(crate::core::error::VectorError::IndexError(
format!(
"No temporal vector index enabled for property '{}'. \
Call db.vector_index(\"{}\").temporal(...).enable() first.",
property_name, property_name
),
))
})?;
index.track_semantic_drift(node_id, reference_embedding, time_range)
}
/// Get the semantic evolution of a node's embedding over time in a specific property.
///
/// Returns the actual embedding vectors at each snapshot timestamp.
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn semantic_evolution_in(
&self,
property_name: &str,
node_id: crate::core::NodeId,
time_range: crate::core::temporal::TimeRange,
) -> Result<Vec<(crate::core::temporal::Timestamp, std::sync::Arc<[f32]>)>> {
// Look up the temporal index for this specific property (multi-property support)
let index = self
.get_temporal_vector_index_for(property_name)
.ok_or_else(|| {
crate::core::error::Error::Vector(crate::core::error::VectorError::IndexError(
format!(
"No temporal vector index enabled for property '{}'. \
Call db.vector_index(\"{}\").temporal(...).enable() first.",
property_name, property_name
),
))
})?;
index.semantic_evolution(node_id, time_range)
}
/// Find all nodes with semantic drift above a threshold in a specific property.
///
/// Scans all nodes and identifies those whose embeddings have changed
/// by more than the specified threshold over the time range.
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn find_drift_in(
&self,
property_name: &str,
threshold: f32,
time_range: crate::core::temporal::TimeRange,
metric: crate::index::vector::temporal::DriftMetric,
) -> Result<Vec<(crate::core::NodeId, f32)>> {
// Look up the temporal index for this specific property (multi-property support)
let index = self
.get_temporal_vector_index_for(property_name)
.ok_or_else(|| {
crate::core::error::Error::Vector(crate::core::error::VectorError::IndexError(
format!(
"No temporal vector index enabled for property '{}'. \
Call db.vector_index(\"{}\").temporal(...).enable() first.",
property_name, property_name
),
))
})?;
index.find_semantic_drift(threshold, time_range, metric)
}
/// Find k most similar nodes across a time range.
///
/// Returns results for each snapshot within the time range, showing how
/// semantic similarity evolved over time.
///
/// # Arguments
///
/// * `embedding` - Query vector
/// * `k` - Number of results per snapshot
/// * `time_range` - Time range to query
///
/// # Returns
///
/// Vector of (timestamp, results) pairs where results are Vec<(NodeId, similarity)>.
///
/// # Example
///
/// ```ignore
/// use aletheiadb::core::temporal::TimeRange;
///
/// // Track how similar documents changed over time
/// let query = vec![0.1; 384];
/// let time_range = TimeRange::between(start_ts, end_ts);
/// let results = storage.find_similar_in_range(&query, 10, time_range)?;
/// for (timestamp, similar_nodes) in results {
/// println!("At timestamp {}: {} similar nodes", timestamp, similar_nodes.len());
/// }
/// ```
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn find_similar_in_range(
&self,
embedding: &[f32],
k: usize,
time_range: crate::core::temporal::TimeRange,
) -> Result<TemporalSearchResults> {
let state = self.temporal_vector_index_state.read();
let index = state.index.as_ref().ok_or_else(|| {
crate::core::error::Error::Vector(crate::core::error::VectorError::IndexError(
"Temporal vector index is not enabled. Call enable_temporal_vector_index() first."
.to_string(),
))
})?;
index.find_similar_in_range(embedding, k, time_range)
}
/// Notify the temporal vector index of a transaction.
///
/// This should be called after committing a transaction to trigger snapshot
/// creation based on the configured strategy.
#[must_use = "this Result must be used; ignoring errors can lead to silent failures"]
pub fn on_temporal_vector_transaction(&self) -> Result<()> {
let state = self.temporal_vector_index_state.read();
if let Some(index) = &state.index {
index.on_transaction()?;
}
Ok(())
}
/// Helper to index a vector in the temporal index.
fn try_index_temporal_vector(
&self,
node_id: NodeId,
properties: &PropertyMap,
timestamp: Timestamp,
) -> Result<()> {
let state = self.temporal_vector_index_state.read();
if let Some(index) = &state.index
&& let Some(prop_name) = &state.property_name
&& let Some(vector) = properties.get(prop_name).and_then(|v| v.as_vector())
{
index.add(node_id, vector, timestamp)?;
}
Ok(())
}
/// Helper to remove a vector from the temporal index.
fn try_remove_temporal_vector(&self, node_id: NodeId, timestamp: Timestamp) -> Result<()> {
let state = self.temporal_vector_index_state.read();
if let Some(index) = &state.index {
index.remove(node_id, timestamp)?;
}
Ok(())
}
/// Get statistics about the current storage
pub fn stats(&self) -> CurrentStats {
CurrentStats {
node_count: self.node_count(),
edge_count: self.edge_count(),
}
}
// ========================================================================
// Query Executor Support Methods (VS-060)
// ========================================================================
/// Get all node IDs in the current storage.
///
/// This is used by the query executor for full node scans.
/// For large graphs, prefer using label-filtered scans instead.
pub fn get_all_node_ids(&self) -> Vec<NodeId> {
self.indexes.iter_node_ids().collect()
}
/// Get all edge IDs in the current storage.
///
/// This is used by recovery tests and query executor for full edge scans.
/// For large graphs, prefer using filtered scans instead.
pub fn get_all_edge_ids(&self) -> Vec<EdgeId> {
self.indexes.iter_edge_ids().collect()
}
/// Get all nodes in the current storage.
///
/// Used by recovery property tests to verify invariants.
pub fn get_all_nodes(&self) -> Vec<crate::Node> {
let mut nodes = Vec::with_capacity(self.indexes.node_count());
nodes.extend(self.indexes.iter_nodes().map(|n| n.clone()));
nodes
}
/// Get all edges in the current storage.
///
/// Used by recovery property tests to verify invariants.
pub fn get_all_edges(&self) -> Vec<crate::Edge> {
let mut edges = Vec::with_capacity(self.indexes.edge_count());
edges.extend(self.indexes.iter_edges().map(|e| e.clone()));
edges
}
/// Get node IDs by label.
///
/// Returns the IDs of all nodes with the given label.
/// This is more memory-efficient than `get_nodes_by_label` when only IDs are needed,
/// as it avoids cloning the full node data.
pub fn get_node_ids_by_label(&self, label: &str) -> Vec<NodeId> {
let label_id = match crate::core::interning::GLOBAL_INTERNER.get_id(label) {
Some(id) => id,
None => return Vec::new(),
};
self.indexes
.iter_nodes()
.filter(|n| n.label == label_id)
.map(|n| n.id)
.collect()
}
/// Get nodes by label.
///
/// Returns an iterator over all nodes with the given label.
/// This is more efficient than scanning all nodes and filtering.
pub fn get_nodes_by_label(&self, label: &str) -> Vec<Node> {
let label_id = match crate::core::interning::GLOBAL_INTERNER.get_id(label) {
Some(id) => id,
None => return Vec::new(), // Label doesn't exist
};
self.indexes
.iter_nodes()
.filter(|n| n.label == label_id)
.map(|n| n.clone())
.collect()
}
/// Find nodes by label and property value.
///
/// Returns the IDs of all nodes with the given label whose specified property
/// equals the given value. Returns an empty `Vec` if no matches are found,
/// or if the label or property key has never been interned.
///
/// This is more efficient than `get_nodes_by_label` followed by manual filtering
/// because it avoids cloning non-matching nodes.
///
/// # Performance
///
/// - **Time**: O(N) where N = nodes with the given label
/// - **Space**: O(M) where M = number of matching nodes
pub fn find_nodes_by_property(
&self,
label: &str,
property_key: &str,
property_value: &PropertyValue,
) -> Vec<NodeId> {
let label_id = match crate::core::interning::GLOBAL_INTERNER.get_id(label) {
Some(id) => id,
None => return Vec::new(),
};
let key_id = match crate::core::interning::GLOBAL_INTERNER.get_id(property_key) {
Some(id) => id,
None => return Vec::new(),
};
self.indexes
.iter_nodes()
.filter(|n| n.label == label_id)
.filter(|n| {
n.properties
.get_by_interned_key(&key_id)
.is_some_and(|v| v == property_value)
})
.map(|n| n.id)
.collect()
}
/// Get the name of the property used for vector indexing.
///
/// Equivalent to `get_default_vector_property_name` (internal).
/// Used by the query executor for vector reranking operations.
pub fn get_vector_property_name(&self) -> Option<String> {
self.get_default_vector_property_name()
}
/// Get node counts grouped by label.
///
/// Returns an iterator of (interned_label, count) pairs.
/// Used by the query planner for cost estimation and cardinality estimation.
pub fn label_counts(&self) -> Vec<(crate::core::interning::InternedString, usize)> {
use crate::core::hasher::IdentityHasher;
use std::collections::HashMap;
use std::hash::BuildHasherDefault;
// âš¡ Bolt: Use `IdentityHasher` instead of default SipHash.
// `InternedString` is already a high-quality unique integer ID (u32),
// so computing SipHash per node (potentially millions) is pure overhead.
// Bypassing hashing reduces cardinality estimation time significantly.
let mut counts: HashMap<
crate::core::interning::InternedString,
usize,
BuildHasherDefault<IdentityHasher>,
> = HashMap::default();
for node in self.indexes.iter_nodes() {
*counts.entry(node.label).or_insert(0) += 1;
}
counts.into_iter().collect()
}
/// Get average out-degree across all nodes.
///
/// Used by the query planner for cost estimation.
pub fn avg_out_degree(&self) -> f64 {
let node_count = self.node_count();
if node_count == 0 {
return 0.0;
}
self.edge_count() as f64 / node_count as f64
}
/// Get target node IDs from outgoing edges (used for traversal iterators).
///
/// Returns the target node IDs of all outgoing edges from the source node.
pub fn get_outgoing_targets(&self, source: NodeId) -> Vec<NodeId> {
let guard = self.indexes.get_outgoing(source);
let mut result = Vec::with_capacity(guard.capacity_hint());
result.extend(guard.iter().map(|entry| entry.target));
result
}
/// Get target node IDs from outgoing edges with a specific label.
pub fn get_outgoing_targets_with_label(&self, source: NodeId, label: &str) -> Vec<NodeId> {
let label_id = match GLOBAL_INTERNER.get_id(label) {
Some(id) => id,
None => return Vec::new(),
};
// Optimized to avoid intermediate Vec allocation and pre-allocate result
let guard = self.indexes.get_outgoing(source);
let mut result = Vec::with_capacity(guard.capacity_hint());
result.extend(
guard
.iter()
.filter(|entry| entry.label == label_id)
.map(|entry| entry.target),
);
result
}
/// Get source node IDs from incoming edges (used for traversal iterators).
///
/// Returns the source node IDs of all incoming edges to the target node.
/// Note: For incoming edges, the "target" field in AdjacencyEntry represents
/// the source node (the node the edge is coming from).
pub fn get_incoming_sources(&self, target: NodeId) -> Vec<NodeId> {
let guard = self.indexes.get_incoming(target);
let mut result = Vec::with_capacity(guard.capacity_hint());
result.extend(guard.iter().map(|entry| entry.target));
result
}
/// Get source node IDs from incoming edges with a specific label.
pub fn get_incoming_sources_with_label(&self, target: NodeId, label: &str) -> Vec<NodeId> {
let label_id = match GLOBAL_INTERNER.get_id(label) {
Some(id) => id,
None => return Vec::new(),
};
// Optimized to avoid intermediate Vec allocation and pre-allocate result
let guard = self.indexes.get_incoming(target);
let mut result = Vec::with_capacity(guard.capacity_hint());
result.extend(
guard
.iter()
.filter(|entry| entry.label == label_id)
.map(|entry| entry.target),
);
result
}
/// Get the number of vectors in the HNSW index.
///
/// Used by the query planner for statistics.
pub fn vector_count(&self) -> usize {
self.get_default_vector_property_name()
.and_then(|prop_name| self.vector_indexes.get(&prop_name))
.map(|entry| entry.value().index.len())
.unwrap_or(0)
}
/// Iterate over all nodes (for persistence).
///
/// This is a helper method for index persistence that provides
/// access to all nodes in the current storage.
///
/// Returns an iterator to avoid allocating a Vec for large graphs,
/// improving memory efficiency during persistence operations.
pub(crate) fn all_nodes(&self) -> impl Iterator<Item = Node> + '_ {
self.indexes.iter_nodes().map(|n| n.clone())
}
/// Iterate over all edges (for persistence).
///
/// This is a helper method for index persistence that provides
/// access to all edges in the current storage.
///
/// Returns an iterator to avoid allocating a Vec for large graphs,
/// improving memory efficiency during persistence operations.
pub(crate) fn all_edges(&self) -> impl Iterator<Item = Edge> + '_ {
self.indexes.iter_edges().map(|e| e.clone())
}
/// Scan nodes by label, returning an iterator over matching node IDs.
///
/// This method efficiently filters the node collection to find all nodes
/// with the specified label.
///
/// # Arguments
///
/// * `label` - The label/type to filter by (e.g., "Person", "Product")
///
/// # Returns
///
/// An iterator yielding `NodeId` for all nodes matching the label.
///
/// # Performance
///
/// - **Time Complexity**: O(n) where n is the total number of nodes
/// - **Space Complexity**: O(1) - iterator, no allocation
/// - **Filtering**: Uses interned string comparison (pointer equality)
///
/// Note: This operation scans all nodes. For workloads with frequent label scans,
/// consider using the query engine's optimized label index lookups.
///
/// # Example
///
/// ```ignore
/// // Scan all Person nodes
/// for node_id in storage.scan_nodes_by_label("Person") {
/// println!("Found Person: {}", node_id);
/// }
/// ```
pub fn scan_nodes_by_label(&self, label: &str) -> impl Iterator<Item = NodeId> + '_ {
// Look up the label in the interner without creating a new entry
// If the label was never interned, no nodes with that label exist
let interned_label = GLOBAL_INTERNER.get_id(label);
self.indexes
.iter_nodes()
.filter(move |node_ref| {
interned_label.is_some_and(|label_id| node_ref.label == label_id)
})
.map(|node_ref| node_ref.id)
}
/// Create an MVCC snapshot at the specified LSN.
///
/// This provides snapshot isolation for checkpoint operations, preventing
/// fuzzy checkpointing (mixed state from different LSNs) that can lead to
/// data corruption.
///
/// # Snapshot Isolation
///
/// The snapshot captures Arc references to all nodes and edges at the time
/// of snapshot creation. Concurrent modifications after snapshot creation
/// do NOT affect the snapshot's iteration.
///
/// # Memory Overhead
///
/// - Does ONE iteration over DashMap to collect Arc references
/// - Memory: ~8 bytes per entity (just Arc pointers, not full clones)
/// - For 10M nodes: ~80MB overhead
///
/// # Performance
///
/// - Snapshot creation: ~100ms for 10M nodes (DashMap iteration)
/// - Concurrent writes: Unaffected, continue during snapshot iteration
///
/// # Arguments
///
/// * `lsn` - LSN at which snapshot is taken (for tracking)
///
/// # Returns
///
/// A snapshot that provides isolated iteration over nodes and edges.
///
/// # Example
///
/// ```ignore
/// let snapshot = current.create_snapshot(LSN(100));
///
/// // Concurrent writes after this point don't affect snapshot
/// for node in snapshot.iter_nodes() {
/// // Stream to disk without loading entire DB
/// persist_node(&node)?;
/// }
/// ```
pub fn create_snapshot(
&self,
lsn: crate::storage::wal::LSN,
) -> crate::storage::snapshot::CurrentStorageSnapshot {
use crate::storage::snapshot::CurrentStorageSnapshot;
use std::sync::Arc;
let mut n = Vec::with_capacity(self.indexes.node_count());
n.extend(self.indexes.iter_nodes().map(|n| n.clone()));
let nodes: Arc<Vec<Node>> = Arc::new(n);
let mut e = Vec::with_capacity(self.indexes.edge_count());
e.extend(self.indexes.iter_edges().map(|e| e.clone()));
let edges: Arc<Vec<Edge>> = Arc::new(e);
CurrentStorageSnapshot::new(lsn, nodes, edges)
}
/// Internal helper to get vector index and configuration.
///
/// If `property_name` is Some, looks up that specific property.
/// If None, looks up the default property.
///
/// Returns (property_name, index, config).
fn get_node_vector(&self, node_id: NodeId, prop_name: &str) -> Result<Arc<[f32]>> {
self.indexes
.with_node(node_id, |node| {
node.properties
.get(prop_name)
.ok_or_else(|| {
crate::core::error::Error::Storage(StorageError::PropertyNotFound(
prop_name.to_string(),
))
})?
.as_arc_vector()
.ok_or_else(|| {
crate::core::error::Error::Vector(
crate::core::error::VectorError::InvalidVector {
reason: "Property is not a vector".to_string(),
},
)
})
})
.transpose()?
.ok_or(crate::core::error::Error::Storage(
StorageError::NodeNotFound(node_id),
))
}
/// Validate that an embedding's dimensions match the index configuration.
fn validate_embedding_dimensions(embedding: &[f32], config: &HnswConfig) -> Result<()> {
if embedding.len() != config.dimensions {
return Err(crate::core::error::Error::Vector(
crate::core::error::VectorError::DimensionMismatch {
expected: config.dimensions,
actual: embedding.len(),
},
));
}
Ok(())
}
fn get_vector_index_internal(
&self,
property_name: Option<&str>,
) -> Result<(String, Arc<HnswIndex>, HnswConfig)> {
let prop_name = if let Some(name) = property_name {
name.to_string()
} else {
self.get_default_vector_property_name().ok_or_else(|| {
crate::core::error::Error::Vector(crate::core::error::VectorError::IndexError(
"Vector index is not enabled. Call db.vector_index(\"...\").hnsw(...).enable() first.".to_string(),
))
})?
};
let entry = self.vector_indexes.get(&prop_name).ok_or_else(|| {
crate::core::error::Error::Vector(crate::core::error::VectorError::IndexError(format!(
"Vector index not found for property '{}'",
prop_name
)))
})?;
Ok((
prop_name,
entry.value().index.clone(),
entry.value().config.clone(),
))
}
/// Internal helper to execute vector search with common logic.
///
/// Handles:
/// - Adaptive over-fetch candidate calculation
/// - Filtering by label
/// - Excluding specific nodes
/// - Recording statistics
fn run_vector_search(
&self,
index: &Arc<HnswIndex>,
query: &[f32],
k: usize,
label_filter: Option<&str>,
exclude_node: Option<NodeId>,
) -> Result<Vec<(NodeId, f32)>> {
let mut results = if let Some(label) = label_filter {
let label_id = GLOBAL_INTERNER.intern(label)?;
let (candidates, stats) = self.calculate_adaptive_candidates(k, label);
// Important: apply adaptive over-fetch exactly once.
//
// `search_with_filter` already performs its own iterative over-fetch expansion to satisfy
// the requested `k`. Passing our already-overfetched candidate count into that API causes
// redundant expansion under contention. Instead, fetch `candidates` once and filter locally.
let candidate_results = index.search(query, candidates)?;
let candidate_count = candidate_results.len();
let mut results = Vec::with_capacity(candidate_count);
for (node_id, similarity) in candidate_results {
// HOT PATH: check_node_label avoids cloning Node and Option overhead (Issue #339)
if self.indexes.check_node_label(node_id, label_id) {
results.push((node_id, similarity));
}
}
if let Some(exclude_id) = exclude_node {
results.retain(|(id, _)| *id != exclude_id);
}
// Track observed pass rate from actual candidates examined.
stats.record_search(candidate_count, results.len());
results
} else {
// If we need to exclude a node, we might need one more result
let search_k = if exclude_node.is_some() { k + 1 } else { k };
let mut results = index.search(query, search_k)?;
if let Some(exclude_id) = exclude_node {
results.retain(|(id, _)| *id != exclude_id);
}
results
};
results.truncate(k);
Ok(results)
}
}
impl Default for CurrentStorage {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests;
#[cfg(test)]
mod coverage_tests {
use super::*;
#[test]
fn test_vector_search_index_not_found() {
let storage = CurrentStorage::new();
// Don't enable vector index
let node_id = NodeId::new(1).unwrap();
// Try search
let result = storage.find_similar(node_id, 5);
assert!(result.is_err());
assert!(format!("{}", result.unwrap_err()).contains("Vector index is not enabled"));
// Try search with specific property
let result = storage.find_similar_in("embedding", node_id, 5);
assert!(result.is_err());
assert!(format!("{}", result.unwrap_err()).contains("Vector index not found"));
}
}