1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
//! The main database struct and operations.
//!
//! Start here with [`GrafeoDB`] - it's your handle to everything.
//!
//! Operations are split across focused submodules:
//! - `query` - Query execution (execute, execute_cypher, etc.)
//! - `crud` - Node/edge CRUD operations
//! - `index` - Property, vector, and text index management
//! - `search` - Vector, text, and hybrid search
//! - `embed` - Embedding model management
//! - `persistence` - Save, load, snapshots, iteration
//! - `admin` - Stats, introspection, diagnostics, CDC
mod admin;
#[cfg(feature = "async-storage")]
mod async_ops;
#[cfg(feature = "async-storage")]
pub(crate) mod async_wal_store;
#[cfg(feature = "cdc")]
pub(crate) mod cdc_store;
mod crud;
#[cfg(feature = "embed")]
mod embed;
mod index;
mod persistence;
mod query;
#[cfg(feature = "rdf")]
mod rdf_ops;
mod search;
#[cfg(feature = "wal")]
pub(crate) mod wal_store;
use grafeo_common::grafeo_error;
#[cfg(feature = "wal")]
use std::path::Path;
use std::sync::Arc;
use std::sync::atomic::AtomicUsize;
use parking_lot::RwLock;
#[cfg(feature = "grafeo-file")]
use grafeo_adapters::storage::file::GrafeoFileManager;
#[cfg(feature = "wal")]
use grafeo_adapters::storage::wal::{
DurabilityMode as WalDurabilityMode, LpgWal, WalConfig, WalRecord, WalRecovery,
};
use grafeo_common::memory::buffer::{BufferManager, BufferManagerConfig};
use grafeo_common::utils::error::Result;
use grafeo_core::graph::lpg::LpgStore;
#[cfg(feature = "rdf")]
use grafeo_core::graph::rdf::RdfStore;
use grafeo_core::graph::{GraphStore, GraphStoreMut};
use crate::catalog::Catalog;
use crate::config::Config;
use crate::query::cache::QueryCache;
use crate::session::Session;
use crate::transaction::TransactionManager;
/// Your handle to a Grafeo database.
///
/// Start here. Create one with [`new_in_memory()`](Self::new_in_memory) for
/// quick experiments, or [`open()`](Self::open) for persistent storage.
/// Then grab a [`session()`](Self::session) to start querying.
///
/// # Examples
///
/// ```
/// use grafeo_engine::GrafeoDB;
///
/// // Quick in-memory database
/// let db = GrafeoDB::new_in_memory();
///
/// // Add some data
/// db.create_node(&["Person"]);
///
/// // Query it
/// let session = db.session();
/// let result = session.execute("MATCH (p:Person) RETURN p")?;
/// # Ok::<(), grafeo_common::utils::error::Error>(())
/// ```
pub struct GrafeoDB {
/// Database configuration.
pub(super) config: Config,
/// The underlying graph store (None when using an external store).
pub(super) store: Option<Arc<LpgStore>>,
/// Schema and metadata catalog shared across sessions.
pub(super) catalog: Arc<Catalog>,
/// RDF triple store (if RDF feature is enabled).
#[cfg(feature = "rdf")]
pub(super) rdf_store: Arc<RdfStore>,
/// Transaction manager.
pub(super) transaction_manager: Arc<TransactionManager>,
/// Unified buffer manager.
pub(super) buffer_manager: Arc<BufferManager>,
/// Write-ahead log manager (if durability is enabled).
#[cfg(feature = "wal")]
pub(super) wal: Option<Arc<LpgWal>>,
/// Shared WAL graph context tracker. Tracks which named graph was last
/// written to the WAL, so concurrent sessions can emit `SwitchGraph`
/// records only when the context actually changes.
#[cfg(feature = "wal")]
pub(super) wal_graph_context: Arc<parking_lot::Mutex<Option<String>>>,
/// Query cache for parsed and optimized plans.
pub(super) query_cache: Arc<QueryCache>,
/// Shared commit counter for auto-GC across sessions.
pub(super) commit_counter: Arc<AtomicUsize>,
/// Whether the database is open.
pub(super) is_open: RwLock<bool>,
/// Change data capture log for tracking mutations.
#[cfg(feature = "cdc")]
pub(super) cdc_log: Arc<crate::cdc::CdcLog>,
/// Whether CDC is active for new sessions and direct CRUD (runtime-mutable).
#[cfg(feature = "cdc")]
cdc_enabled: std::sync::atomic::AtomicBool,
/// Registered embedding models for text-to-vector conversion.
#[cfg(feature = "embed")]
pub(super) embedding_models:
RwLock<hashbrown::HashMap<String, Arc<dyn crate::embedding::EmbeddingModel>>>,
/// Single-file database manager (when using `.grafeo` format).
#[cfg(feature = "grafeo-file")]
pub(super) file_manager: Option<Arc<GrafeoFileManager>>,
/// External read-only graph store (when using with_store() or with_read_store()).
/// When set, sessions route queries through this store instead of the built-in LpgStore.
pub(super) external_read_store: Option<Arc<dyn GraphStore>>,
/// External writable graph store (when using with_store()).
/// None for read-only databases created via with_read_store().
pub(super) external_write_store: Option<Arc<dyn GraphStoreMut>>,
/// Metrics registry shared across all sessions.
#[cfg(feature = "metrics")]
pub(crate) metrics: Option<Arc<crate::metrics::MetricsRegistry>>,
/// Persistent graph context for one-shot `execute()` calls.
/// When set, each call to `session()` pre-configures the session to this graph.
/// Updated after every one-shot `execute()` to reflect `USE GRAPH` / `SESSION RESET`.
current_graph: RwLock<Option<String>>,
/// Persistent schema context for one-shot `execute()` calls.
/// When set, each call to `session()` pre-configures the session to this schema.
/// Updated after every one-shot `execute()` to reflect `SESSION SET SCHEMA` / `SESSION RESET`.
current_schema: RwLock<Option<String>>,
/// Whether this database is open in read-only mode.
/// When true, sessions automatically enforce read-only transactions.
read_only: bool,
}
impl GrafeoDB {
/// Returns a reference to the built-in LPG store.
///
/// # Panics
///
/// Panics if the database was created with [`with_store()`](Self::with_store) or
/// [`with_read_store()`](Self::with_read_store), which use an external store
/// instead of the built-in LPG store.
fn lpg_store(&self) -> &Arc<LpgStore> {
self.store.as_ref().expect(
"no built-in LpgStore: this GrafeoDB was created with an external store \
(with_store / with_read_store). Use session() or graph_store() instead.",
)
}
/// Returns whether CDC is active (runtime check).
#[cfg(feature = "cdc")]
#[inline]
pub(super) fn cdc_active(&self) -> bool {
self.cdc_enabled.load(std::sync::atomic::Ordering::Relaxed)
}
/// Creates an in-memory database, fast to create, gone when dropped.
///
/// Use this for tests, experiments, or when you don't need persistence.
/// For data that survives restarts, use [`open()`](Self::open) instead.
///
/// # Panics
///
/// Panics if the internal arena allocator cannot be initialized (out of memory).
/// Use [`with_config()`](Self::with_config) for a fallible alternative.
///
/// # Examples
///
/// ```
/// use grafeo_engine::GrafeoDB;
///
/// let db = GrafeoDB::new_in_memory();
/// let session = db.session();
/// session.execute("INSERT (:Person {name: 'Alix'})")?;
/// # Ok::<(), grafeo_common::utils::error::Error>(())
/// ```
#[must_use]
pub fn new_in_memory() -> Self {
Self::with_config(Config::in_memory()).expect("In-memory database creation should not fail")
}
/// Opens a database at the given path, creating it if it doesn't exist.
///
/// If you've used this path before, Grafeo recovers your data from the
/// write-ahead log automatically. First open on a new path creates an
/// empty database.
///
/// # Errors
///
/// Returns an error if the path isn't writable or recovery fails.
///
/// # Examples
///
/// ```no_run
/// use grafeo_engine::GrafeoDB;
///
/// let db = GrafeoDB::open("./my_social_network")?;
/// # Ok::<(), grafeo_common::utils::error::Error>(())
/// ```
#[cfg(feature = "wal")]
pub fn open(path: impl AsRef<Path>) -> Result<Self> {
Self::with_config(Config::persistent(path.as_ref()))
}
/// Opens an existing database in read-only mode.
///
/// Uses a shared file lock, so multiple processes can read the same
/// `.grafeo` file concurrently. The database loads the last checkpoint
/// snapshot but does **not** replay the WAL or allow mutations.
///
/// Currently only supports the single-file (`.grafeo`) format.
///
/// # Errors
///
/// Returns an error if the file doesn't exist or can't be read.
///
/// # Examples
///
/// ```no_run
/// use grafeo_engine::GrafeoDB;
///
/// let db = GrafeoDB::open_read_only("./my_graph.grafeo")?;
/// let session = db.session();
/// let result = session.execute("MATCH (n) RETURN n LIMIT 10")?;
/// // Mutations will return an error:
/// // session.execute("INSERT (:Person)") => Err(ReadOnly)
/// # Ok::<(), grafeo_common::utils::error::Error>(())
/// ```
#[cfg(feature = "grafeo-file")]
pub fn open_read_only(path: impl AsRef<std::path::Path>) -> Result<Self> {
Self::with_config(Config::read_only(path.as_ref()))
}
/// Creates a database with custom configuration.
///
/// Use this when you need fine-grained control over memory limits,
/// thread counts, or persistence settings. For most cases,
/// [`new_in_memory()`](Self::new_in_memory) or [`open()`](Self::open)
/// are simpler.
///
/// # Errors
///
/// Returns an error if the database can't be created or recovery fails.
///
/// # Examples
///
/// ```
/// use grafeo_engine::{GrafeoDB, Config};
///
/// // In-memory with a 512MB limit
/// let config = Config::in_memory()
/// .with_memory_limit(512 * 1024 * 1024);
///
/// let db = GrafeoDB::with_config(config)?;
/// # Ok::<(), grafeo_common::utils::error::Error>(())
/// ```
pub fn with_config(config: Config) -> Result<Self> {
// Validate configuration before proceeding
config
.validate()
.map_err(|e| grafeo_common::utils::error::Error::Internal(e.to_string()))?;
let store = Arc::new(LpgStore::new()?);
#[cfg(feature = "rdf")]
let rdf_store = Arc::new(RdfStore::new());
let transaction_manager = Arc::new(TransactionManager::new());
// Create buffer manager with configured limits
let buffer_config = BufferManagerConfig {
budget: config.memory_limit.unwrap_or_else(|| {
(BufferManagerConfig::detect_system_memory() as f64 * 0.75) as usize
}),
spill_path: config
.spill_path
.clone()
.or_else(|| config.path.as_ref().map(|p| p.join("spill"))),
..BufferManagerConfig::default()
};
let buffer_manager = BufferManager::new(buffer_config);
// Create catalog early so WAL replay can restore schema definitions
let catalog = Arc::new(Catalog::new());
let is_read_only = config.access_mode == crate::config::AccessMode::ReadOnly;
// --- Single-file format (.grafeo) ---
#[cfg(feature = "grafeo-file")]
let file_manager: Option<Arc<GrafeoFileManager>> = if is_read_only {
// Read-only mode: open with shared lock, load snapshot, skip WAL
if let Some(ref db_path) = config.path {
if db_path.exists() && db_path.is_file() {
let fm = GrafeoFileManager::open_read_only(db_path)?;
let snapshot_data = fm.read_snapshot()?;
if !snapshot_data.is_empty() {
Self::apply_snapshot_data(
&store,
&catalog,
#[cfg(feature = "rdf")]
&rdf_store,
&snapshot_data,
)?;
}
Some(Arc::new(fm))
} else {
return Err(grafeo_common::utils::error::Error::Internal(format!(
"read-only open requires an existing .grafeo file: {}",
db_path.display()
)));
}
} else {
return Err(grafeo_common::utils::error::Error::Internal(
"read-only mode requires a database path".to_string(),
));
}
} else if let Some(ref db_path) = config.path {
// Initialize the file manager whenever single-file format is selected,
// regardless of whether WAL is enabled. Without this, a database opened
// with wal_enabled:false + StorageFormat::SingleFile would produce no
// output at all (the file manager was previously gated behind wal_enabled).
if Self::should_use_single_file(db_path, config.storage_format) {
let fm = if db_path.exists() && db_path.is_file() {
GrafeoFileManager::open(db_path)?
} else if !db_path.exists() {
GrafeoFileManager::create(db_path)?
} else {
// Path exists but is not a file (directory, etc.)
return Err(grafeo_common::utils::error::Error::Internal(format!(
"path exists but is not a file: {}",
db_path.display()
)));
};
// Load snapshot data from the file
let snapshot_data = fm.read_snapshot()?;
if !snapshot_data.is_empty() {
Self::apply_snapshot_data(
&store,
&catalog,
#[cfg(feature = "rdf")]
&rdf_store,
&snapshot_data,
)?;
}
// Recover sidecar WAL if WAL is enabled and a sidecar exists
#[cfg(feature = "wal")]
if config.wal_enabled && fm.has_sidecar_wal() {
let recovery = WalRecovery::new(fm.sidecar_wal_path());
let records = recovery.recover()?;
Self::apply_wal_records(
&store,
&catalog,
#[cfg(feature = "rdf")]
&rdf_store,
&records,
)?;
}
Some(Arc::new(fm))
} else {
None
}
} else {
None
};
// Determine whether to use the WAL directory path (legacy) or sidecar
// Read-only mode skips WAL entirely (no recovery, no creation).
#[cfg(feature = "wal")]
let wal = if is_read_only {
None
} else if config.wal_enabled {
if let Some(ref db_path) = config.path {
// When using single-file format, the WAL is a sidecar directory
#[cfg(feature = "grafeo-file")]
let wal_path = if let Some(ref fm) = file_manager {
let p = fm.sidecar_wal_path();
std::fs::create_dir_all(&p)?;
p
} else {
// Legacy: WAL inside the database directory
std::fs::create_dir_all(db_path)?;
db_path.join("wal")
};
#[cfg(not(feature = "grafeo-file"))]
let wal_path = {
std::fs::create_dir_all(db_path)?;
db_path.join("wal")
};
// For legacy WAL directory format, check if WAL exists and recover
#[cfg(feature = "grafeo-file")]
let is_single_file = file_manager.is_some();
#[cfg(not(feature = "grafeo-file"))]
let is_single_file = false;
if !is_single_file && wal_path.exists() {
let recovery = WalRecovery::new(&wal_path);
let records = recovery.recover()?;
Self::apply_wal_records(
&store,
&catalog,
#[cfg(feature = "rdf")]
&rdf_store,
&records,
)?;
}
// Open/create WAL manager with configured durability
let wal_durability = match config.wal_durability {
crate::config::DurabilityMode::Sync => WalDurabilityMode::Sync,
crate::config::DurabilityMode::Batch {
max_delay_ms,
max_records,
} => WalDurabilityMode::Batch {
max_delay_ms,
max_records,
},
crate::config::DurabilityMode::Adaptive { target_interval_ms } => {
WalDurabilityMode::Adaptive { target_interval_ms }
}
crate::config::DurabilityMode::NoSync => WalDurabilityMode::NoSync,
};
let wal_config = WalConfig {
durability: wal_durability,
..WalConfig::default()
};
let wal_manager = LpgWal::with_config(&wal_path, wal_config)?;
Some(Arc::new(wal_manager))
} else {
None
}
} else {
None
};
// Create query cache with default capacity (1000 queries)
let query_cache = Arc::new(QueryCache::default());
// After all snapshot/WAL recovery, sync TransactionManager epoch
// with the store so queries use the correct viewing epoch.
#[cfg(feature = "temporal")]
transaction_manager.sync_epoch(store.current_epoch());
#[cfg(feature = "cdc")]
let cdc_enabled_val = config.cdc_enabled;
Ok(Self {
config,
store: Some(store),
catalog,
#[cfg(feature = "rdf")]
rdf_store,
transaction_manager,
buffer_manager,
#[cfg(feature = "wal")]
wal,
#[cfg(feature = "wal")]
wal_graph_context: Arc::new(parking_lot::Mutex::new(None)),
query_cache,
commit_counter: Arc::new(AtomicUsize::new(0)),
is_open: RwLock::new(true),
#[cfg(feature = "cdc")]
cdc_log: Arc::new(crate::cdc::CdcLog::new()),
#[cfg(feature = "cdc")]
cdc_enabled: std::sync::atomic::AtomicBool::new(cdc_enabled_val),
#[cfg(feature = "embed")]
embedding_models: RwLock::new(hashbrown::HashMap::new()),
#[cfg(feature = "grafeo-file")]
file_manager,
external_read_store: None,
external_write_store: None,
#[cfg(feature = "metrics")]
metrics: Some(Arc::new(crate::metrics::MetricsRegistry::new())),
current_graph: RwLock::new(None),
current_schema: RwLock::new(None),
read_only: is_read_only,
})
}
/// Creates a database backed by a custom [`GraphStoreMut`] implementation.
///
/// The external store handles all data persistence. WAL, CDC, and index
/// management are the responsibility of the store implementation.
///
/// Query execution (all 6 languages, optimizer, planner) works through the
/// provided store. Admin operations (schema introspection, persistence,
/// vector/text indexes) are not available on external stores.
///
/// # Examples
///
/// ```no_run
/// use std::sync::Arc;
/// use grafeo_engine::{GrafeoDB, Config};
/// use grafeo_core::graph::GraphStoreMut;
///
/// fn example(store: Arc<dyn GraphStoreMut>) -> grafeo_common::utils::error::Result<()> {
/// let db = GrafeoDB::with_store(store, Config::in_memory())?;
/// let result = db.execute("MATCH (n) RETURN count(n)")?;
/// Ok(())
/// }
/// ```
///
/// [`GraphStoreMut`]: grafeo_core::graph::GraphStoreMut
pub fn with_store(store: Arc<dyn GraphStoreMut>, config: Config) -> Result<Self> {
config
.validate()
.map_err(|e| grafeo_common::utils::error::Error::Internal(e.to_string()))?;
let transaction_manager = Arc::new(TransactionManager::new());
let buffer_config = BufferManagerConfig {
budget: config.memory_limit.unwrap_or_else(|| {
(BufferManagerConfig::detect_system_memory() as f64 * 0.75) as usize
}),
spill_path: None,
..BufferManagerConfig::default()
};
let buffer_manager = BufferManager::new(buffer_config);
let query_cache = Arc::new(QueryCache::default());
#[cfg(feature = "cdc")]
let cdc_enabled_val = config.cdc_enabled;
Ok(Self {
config,
store: None,
catalog: Arc::new(Catalog::new()),
#[cfg(feature = "rdf")]
rdf_store: Arc::new(RdfStore::new()),
transaction_manager,
buffer_manager,
#[cfg(feature = "wal")]
wal: None,
#[cfg(feature = "wal")]
wal_graph_context: Arc::new(parking_lot::Mutex::new(None)),
query_cache,
commit_counter: Arc::new(AtomicUsize::new(0)),
is_open: RwLock::new(true),
#[cfg(feature = "cdc")]
cdc_log: Arc::new(crate::cdc::CdcLog::new()),
#[cfg(feature = "cdc")]
cdc_enabled: std::sync::atomic::AtomicBool::new(cdc_enabled_val),
#[cfg(feature = "embed")]
embedding_models: RwLock::new(hashbrown::HashMap::new()),
#[cfg(feature = "grafeo-file")]
file_manager: None,
external_read_store: Some(Arc::clone(&store) as Arc<dyn GraphStore>),
external_write_store: Some(store),
#[cfg(feature = "metrics")]
metrics: Some(Arc::new(crate::metrics::MetricsRegistry::new())),
current_graph: RwLock::new(None),
current_schema: RwLock::new(None),
read_only: false,
})
}
/// Creates a database backed by a read-only [`GraphStore`].
///
/// The database is set to read-only mode. Write queries (CREATE, SET,
/// DELETE) will return `TransactionError::ReadOnly`.
///
/// # Examples
///
/// ```no_run
/// use std::sync::Arc;
/// use grafeo_engine::{GrafeoDB, Config};
/// use grafeo_core::graph::GraphStore;
///
/// fn example(store: Arc<dyn GraphStore>) -> grafeo_common::utils::error::Result<()> {
/// let db = GrafeoDB::with_read_store(store, Config::in_memory())?;
/// let result = db.execute("MATCH (n) RETURN count(n)")?;
/// Ok(())
/// }
/// ```
///
/// [`GraphStore`]: grafeo_core::graph::GraphStore
pub fn with_read_store(store: Arc<dyn GraphStore>, config: Config) -> Result<Self> {
config
.validate()
.map_err(|e| grafeo_common::utils::error::Error::Internal(e.to_string()))?;
let transaction_manager = Arc::new(TransactionManager::new());
let buffer_config = BufferManagerConfig {
budget: config.memory_limit.unwrap_or_else(|| {
(BufferManagerConfig::detect_system_memory() as f64 * 0.75) as usize
}),
spill_path: None,
..BufferManagerConfig::default()
};
let buffer_manager = BufferManager::new(buffer_config);
let query_cache = Arc::new(QueryCache::default());
#[cfg(feature = "cdc")]
let cdc_enabled_val = config.cdc_enabled;
Ok(Self {
config,
store: None,
catalog: Arc::new(Catalog::new()),
#[cfg(feature = "rdf")]
rdf_store: Arc::new(RdfStore::new()),
transaction_manager,
buffer_manager,
#[cfg(feature = "wal")]
wal: None,
#[cfg(feature = "wal")]
wal_graph_context: Arc::new(parking_lot::Mutex::new(None)),
query_cache,
commit_counter: Arc::new(AtomicUsize::new(0)),
is_open: RwLock::new(true),
#[cfg(feature = "cdc")]
cdc_log: Arc::new(crate::cdc::CdcLog::new()),
#[cfg(feature = "cdc")]
cdc_enabled: std::sync::atomic::AtomicBool::new(cdc_enabled_val),
#[cfg(feature = "embed")]
embedding_models: RwLock::new(hashbrown::HashMap::new()),
#[cfg(feature = "grafeo-file")]
file_manager: None,
external_read_store: Some(store),
external_write_store: None,
#[cfg(feature = "metrics")]
metrics: Some(Arc::new(crate::metrics::MetricsRegistry::new())),
current_graph: RwLock::new(None),
current_schema: RwLock::new(None),
read_only: true,
})
}
/// Converts the database to a read-only [`CompactStore`] for faster queries.
///
/// Takes a snapshot of all nodes and edges from the current store, builds
/// a columnar `CompactStore` with CSR adjacency, and switches the database
/// to read-only mode. The original store is dropped to free memory.
///
/// After calling this, all write queries will fail with
/// `TransactionError::ReadOnly`. Read queries (across all supported
/// languages) continue to work and benefit from ~60x memory reduction
/// and 100x+ traversal speedup.
///
/// # Errors
///
/// Returns an error if the conversion fails (e.g. more than 32,767
/// distinct labels or edge types).
///
/// [`CompactStore`]: grafeo_core::graph::compact::CompactStore
#[cfg(feature = "compact-store")]
pub fn compact(&mut self) -> Result<()> {
use grafeo_core::graph::compact::from_graph_store;
let current_store = self.graph_store();
let compact = from_graph_store(current_store.as_ref())
.map_err(|e| grafeo_common::utils::error::Error::Internal(e.to_string()))?;
self.external_read_store = Some(Arc::new(compact) as Arc<dyn GraphStore>);
self.external_write_store = None;
self.store = None;
self.read_only = true;
self.query_cache = Arc::new(QueryCache::default());
Ok(())
}
/// Applies WAL records to restore the database state.
///
/// Data mutation records are routed through a graph cursor that tracks
/// `SwitchGraph` context markers, replaying mutations into the correct
/// named graph (or the default graph when cursor is `None`).
#[cfg(feature = "wal")]
fn apply_wal_records(
store: &Arc<LpgStore>,
catalog: &Catalog,
#[cfg(feature = "rdf")] rdf_store: &Arc<RdfStore>,
records: &[WalRecord],
) -> Result<()> {
use crate::catalog::{
EdgeTypeDefinition, NodeTypeDefinition, PropertyDataType, TypeConstraint, TypedProperty,
};
use grafeo_common::utils::error::Error;
// Graph cursor: tracks which named graph receives data mutations.
// `None` means the default graph.
let mut current_graph: Option<String> = None;
let mut target_store: Arc<LpgStore> = Arc::clone(store);
for record in records {
match record {
// --- Named graph lifecycle ---
WalRecord::CreateNamedGraph { name } => {
let _ = store.create_graph(name);
}
WalRecord::DropNamedGraph { name } => {
store.drop_graph(name);
// Reset cursor if the dropped graph was active
if current_graph.as_deref() == Some(name.as_str()) {
current_graph = None;
target_store = Arc::clone(store);
}
}
WalRecord::SwitchGraph { name } => {
current_graph.clone_from(name);
target_store = match ¤t_graph {
None => Arc::clone(store),
Some(graph_name) => store
.graph_or_create(graph_name)
.map_err(|e| Error::Internal(e.to_string()))?,
};
}
// --- Data mutations: routed through target_store ---
WalRecord::CreateNode { id, labels } => {
let label_refs: Vec<&str> = labels.iter().map(|s| s.as_str()).collect();
target_store.create_node_with_id(*id, &label_refs)?;
}
WalRecord::DeleteNode { id } => {
target_store.delete_node(*id);
}
WalRecord::CreateEdge {
id,
src,
dst,
edge_type,
} => {
target_store.create_edge_with_id(*id, *src, *dst, edge_type)?;
}
WalRecord::DeleteEdge { id } => {
target_store.delete_edge(*id);
}
WalRecord::SetNodeProperty { id, key, value } => {
target_store.set_node_property(*id, key, value.clone());
}
WalRecord::SetEdgeProperty { id, key, value } => {
target_store.set_edge_property(*id, key, value.clone());
}
WalRecord::AddNodeLabel { id, label } => {
target_store.add_label(*id, label);
}
WalRecord::RemoveNodeLabel { id, label } => {
target_store.remove_label(*id, label);
}
WalRecord::RemoveNodeProperty { id, key } => {
target_store.remove_node_property(*id, key);
}
WalRecord::RemoveEdgeProperty { id, key } => {
target_store.remove_edge_property(*id, key);
}
// --- Schema DDL replay (always on root catalog) ---
WalRecord::CreateNodeType {
name,
properties,
constraints,
} => {
let def = NodeTypeDefinition {
name: name.clone(),
properties: properties
.iter()
.map(|(n, t, nullable)| TypedProperty {
name: n.clone(),
data_type: PropertyDataType::from_type_name(t),
nullable: *nullable,
default_value: None,
})
.collect(),
constraints: constraints
.iter()
.map(|(kind, props)| match kind.as_str() {
"unique" => TypeConstraint::Unique(props.clone()),
"primary_key" => TypeConstraint::PrimaryKey(props.clone()),
"not_null" if !props.is_empty() => {
TypeConstraint::NotNull(props[0].clone())
}
_ => TypeConstraint::Unique(props.clone()),
})
.collect(),
parent_types: Vec::new(),
};
let _ = catalog.register_node_type(def);
}
WalRecord::DropNodeType { name } => {
let _ = catalog.drop_node_type(name);
}
WalRecord::CreateEdgeType {
name,
properties,
constraints,
} => {
let def = EdgeTypeDefinition {
name: name.clone(),
properties: properties
.iter()
.map(|(n, t, nullable)| TypedProperty {
name: n.clone(),
data_type: PropertyDataType::from_type_name(t),
nullable: *nullable,
default_value: None,
})
.collect(),
constraints: constraints
.iter()
.map(|(kind, props)| match kind.as_str() {
"unique" => TypeConstraint::Unique(props.clone()),
"primary_key" => TypeConstraint::PrimaryKey(props.clone()),
"not_null" if !props.is_empty() => {
TypeConstraint::NotNull(props[0].clone())
}
_ => TypeConstraint::Unique(props.clone()),
})
.collect(),
source_node_types: Vec::new(),
target_node_types: Vec::new(),
};
let _ = catalog.register_edge_type_def(def);
}
WalRecord::DropEdgeType { name } => {
let _ = catalog.drop_edge_type_def(name);
}
WalRecord::CreateIndex { .. } | WalRecord::DropIndex { .. } => {
// Index recreation is handled by the store on startup
// (indexes are rebuilt from data, not WAL)
}
WalRecord::CreateConstraint { .. } | WalRecord::DropConstraint { .. } => {
// Constraint definitions are part of type definitions
// and replayed via CreateNodeType/CreateEdgeType
}
WalRecord::CreateGraphType {
name,
node_types,
edge_types,
open,
} => {
use crate::catalog::GraphTypeDefinition;
let def = GraphTypeDefinition {
name: name.clone(),
allowed_node_types: node_types.clone(),
allowed_edge_types: edge_types.clone(),
open: *open,
};
let _ = catalog.register_graph_type(def);
}
WalRecord::DropGraphType { name } => {
let _ = catalog.drop_graph_type(name);
}
WalRecord::CreateSchema { name } => {
let _ = catalog.register_schema_namespace(name.clone());
}
WalRecord::DropSchema { name } => {
let _ = catalog.drop_schema_namespace(name);
}
WalRecord::AlterNodeType { name, alterations } => {
for (action, prop_name, type_name, nullable) in alterations {
match action.as_str() {
"add" => {
let prop = TypedProperty {
name: prop_name.clone(),
data_type: PropertyDataType::from_type_name(type_name),
nullable: *nullable,
default_value: None,
};
let _ = catalog.alter_node_type_add_property(name, prop);
}
"drop" => {
let _ = catalog.alter_node_type_drop_property(name, prop_name);
}
_ => {}
}
}
}
WalRecord::AlterEdgeType { name, alterations } => {
for (action, prop_name, type_name, nullable) in alterations {
match action.as_str() {
"add" => {
let prop = TypedProperty {
name: prop_name.clone(),
data_type: PropertyDataType::from_type_name(type_name),
nullable: *nullable,
default_value: None,
};
let _ = catalog.alter_edge_type_add_property(name, prop);
}
"drop" => {
let _ = catalog.alter_edge_type_drop_property(name, prop_name);
}
_ => {}
}
}
}
WalRecord::AlterGraphType { name, alterations } => {
for (action, type_name) in alterations {
match action.as_str() {
"add_node" => {
let _ =
catalog.alter_graph_type_add_node_type(name, type_name.clone());
}
"drop_node" => {
let _ = catalog.alter_graph_type_drop_node_type(name, type_name);
}
"add_edge" => {
let _ =
catalog.alter_graph_type_add_edge_type(name, type_name.clone());
}
"drop_edge" => {
let _ = catalog.alter_graph_type_drop_edge_type(name, type_name);
}
_ => {}
}
}
}
WalRecord::CreateProcedure {
name,
params,
returns,
body,
} => {
use crate::catalog::ProcedureDefinition;
let def = ProcedureDefinition {
name: name.clone(),
params: params.clone(),
returns: returns.clone(),
body: body.clone(),
};
let _ = catalog.register_procedure(def);
}
WalRecord::DropProcedure { name } => {
let _ = catalog.drop_procedure(name);
}
// --- RDF triple replay ---
#[cfg(feature = "rdf")]
WalRecord::InsertRdfTriple { .. }
| WalRecord::DeleteRdfTriple { .. }
| WalRecord::ClearRdfGraph { .. }
| WalRecord::CreateRdfGraph { .. }
| WalRecord::DropRdfGraph { .. } => {
rdf_ops::replay_rdf_wal_record(rdf_store, record);
}
#[cfg(not(feature = "rdf"))]
WalRecord::InsertRdfTriple { .. }
| WalRecord::DeleteRdfTriple { .. }
| WalRecord::ClearRdfGraph { .. }
| WalRecord::CreateRdfGraph { .. }
| WalRecord::DropRdfGraph { .. } => {}
WalRecord::TransactionCommit { .. } => {
// In temporal mode, advance the store epoch on each committed
// transaction so that subsequent property/label operations
// are recorded at the correct epoch in their VersionLogs.
#[cfg(feature = "temporal")]
{
target_store.new_epoch();
}
}
WalRecord::TransactionAbort { .. } | WalRecord::Checkpoint { .. } => {
// Transaction control records don't need replay action
// (recovery already filtered to only committed transactions)
}
}
}
Ok(())
}
// =========================================================================
// Single-file format helpers
// =========================================================================
/// Returns `true` if the given path should use single-file format.
#[cfg(feature = "grafeo-file")]
fn should_use_single_file(
path: &std::path::Path,
configured: crate::config::StorageFormat,
) -> bool {
use crate::config::StorageFormat;
match configured {
StorageFormat::SingleFile => true,
StorageFormat::WalDirectory => false,
StorageFormat::Auto => {
// Existing file: check magic bytes
if path.is_file() {
if let Ok(mut f) = std::fs::File::open(path) {
use std::io::Read;
let mut magic = [0u8; 4];
if f.read_exact(&mut magic).is_ok()
&& magic == grafeo_adapters::storage::file::MAGIC
{
return true;
}
}
return false;
}
// Existing directory: legacy format
if path.is_dir() {
return false;
}
// New path: check extension
path.extension().is_some_and(|ext| ext == "grafeo")
}
}
}
/// Applies snapshot data (from a `.grafeo` file) to restore the store and catalog.
#[cfg(feature = "grafeo-file")]
fn apply_snapshot_data(
store: &Arc<LpgStore>,
catalog: &Arc<crate::catalog::Catalog>,
#[cfg(feature = "rdf")] rdf_store: &Arc<RdfStore>,
data: &[u8],
) -> Result<()> {
persistence::load_snapshot_into_store(
store,
catalog,
#[cfg(feature = "rdf")]
rdf_store,
data,
)
}
// =========================================================================
// Session & Configuration
// =========================================================================
/// Opens a new session for running queries.
///
/// Sessions are cheap to create: spin up as many as you need. Each
/// gets its own transaction context, so concurrent sessions won't
/// block each other on reads.
///
/// # Panics
///
/// Panics if the database was configured with an external graph store and
/// the internal arena allocator cannot be initialized (out of memory).
///
/// # Examples
///
/// ```
/// use grafeo_engine::GrafeoDB;
///
/// let db = GrafeoDB::new_in_memory();
/// let session = db.session();
///
/// // Run queries through the session
/// let result = session.execute("MATCH (n) RETURN count(n)")?;
/// # Ok::<(), grafeo_common::utils::error::Error>(())
/// ```
#[must_use]
pub fn session(&self) -> Session {
self.create_session_inner(None)
}
/// Creates a session with an explicit CDC override.
///
/// When `cdc_enabled` is `true`, mutations in this session are tracked
/// regardless of the database default. When `false`, mutations are not
/// tracked regardless of the database default.
///
/// # Examples
///
/// ```
/// use grafeo_engine::GrafeoDB;
///
/// let db = GrafeoDB::new_in_memory();
///
/// // Opt in to CDC for just this session
/// let tracked = db.session_with_cdc(true);
/// tracked.execute("INSERT (:Person {name: 'Alix'})")?;
/// # Ok::<(), grafeo_common::utils::error::Error>(())
/// ```
#[cfg(feature = "cdc")]
#[must_use]
pub fn session_with_cdc(&self, cdc_enabled: bool) -> Session {
self.create_session_inner(Some(cdc_enabled))
}
/// Creates a read-only session regardless of the database's access mode.
///
/// Mutations executed through this session will fail with
/// `TransactionError::ReadOnly`. Useful for replication replicas where
/// the database itself must remain writable (for applying CDC changes)
/// but client-facing queries must be read-only.
#[must_use]
pub fn session_read_only(&self) -> Session {
self.create_session_inner_opts(None, true)
}
/// Shared session creation logic.
///
/// `cdc_override` overrides the database-wide `cdc_enabled` default when
/// `Some`. `None` falls back to the database default.
#[allow(unused_variables)] // cdc_override unused when cdc feature is off
fn create_session_inner(&self, cdc_override: Option<bool>) -> Session {
self.create_session_inner_opts(cdc_override, false)
}
/// Shared session creation with all overrides.
#[allow(unused_variables)]
fn create_session_inner_opts(
&self,
cdc_override: Option<bool>,
force_read_only: bool,
) -> Session {
let session_cfg = || crate::session::SessionConfig {
transaction_manager: Arc::clone(&self.transaction_manager),
query_cache: Arc::clone(&self.query_cache),
catalog: Arc::clone(&self.catalog),
adaptive_config: self.config.adaptive.clone(),
factorized_execution: self.config.factorized_execution,
graph_model: self.config.graph_model,
query_timeout: self.config.query_timeout,
commit_counter: Arc::clone(&self.commit_counter),
gc_interval: self.config.gc_interval,
read_only: self.read_only || force_read_only,
};
if let Some(ref ext_read) = self.external_read_store {
return Session::with_external_store(
Arc::clone(ext_read),
self.external_write_store.as_ref().map(Arc::clone),
session_cfg(),
)
.expect("arena allocation for external store session");
}
#[cfg(feature = "rdf")]
let mut session = Session::with_rdf_store_and_adaptive(
Arc::clone(self.lpg_store()),
Arc::clone(&self.rdf_store),
session_cfg(),
);
#[cfg(not(feature = "rdf"))]
let mut session = Session::with_adaptive(Arc::clone(self.lpg_store()), session_cfg());
#[cfg(feature = "wal")]
if let Some(ref wal) = self.wal {
session.set_wal(Arc::clone(wal), Arc::clone(&self.wal_graph_context));
}
#[cfg(feature = "cdc")]
{
let should_enable = cdc_override.unwrap_or_else(|| self.cdc_active());
if should_enable {
session.set_cdc_log(Arc::clone(&self.cdc_log));
}
}
#[cfg(feature = "metrics")]
{
if let Some(ref m) = self.metrics {
session.set_metrics(Arc::clone(m));
m.session_created
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
m.session_active
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}
}
// Propagate persistent graph context to the new session
if let Some(ref graph) = *self.current_graph.read() {
session.use_graph(graph);
}
// Propagate persistent schema context to the new session
if let Some(ref schema) = *self.current_schema.read() {
session.set_schema(schema);
}
// Suppress unused_mut when cdc/wal are disabled
let _ = &mut session;
session
}
/// Returns the current graph name, if any.
///
/// This is the persistent graph context used by one-shot `execute()` calls.
/// It is updated whenever `execute()` encounters `USE GRAPH`, `SESSION SET GRAPH`,
/// or `SESSION RESET`.
#[must_use]
pub fn current_graph(&self) -> Option<String> {
self.current_graph.read().clone()
}
/// Sets the current graph context for subsequent one-shot `execute()` calls.
///
/// This is equivalent to running `USE GRAPH <name>` but without creating a session.
/// Pass `None` to reset to the default graph.
pub fn set_current_graph(&self, name: Option<&str>) {
*self.current_graph.write() = name.map(ToString::to_string);
}
/// Returns the current schema name, if any.
///
/// This is the persistent schema context used by one-shot `execute()` calls.
/// It is updated whenever `execute()` encounters `SESSION SET SCHEMA` or `SESSION RESET`.
#[must_use]
pub fn current_schema(&self) -> Option<String> {
self.current_schema.read().clone()
}
/// Sets the current schema context for subsequent one-shot `execute()` calls.
///
/// This is equivalent to running `SESSION SET SCHEMA <name>` but without creating
/// a session. Pass `None` to clear the schema context.
pub fn set_current_schema(&self, name: Option<&str>) {
*self.current_schema.write() = name.map(ToString::to_string);
}
/// Returns the adaptive execution configuration.
#[must_use]
pub fn adaptive_config(&self) -> &crate::config::AdaptiveConfig {
&self.config.adaptive
}
/// Returns `true` if this database was opened in read-only mode.
#[must_use]
pub fn is_read_only(&self) -> bool {
self.read_only
}
/// Returns the configuration.
#[must_use]
pub fn config(&self) -> &Config {
&self.config
}
/// Returns the graph data model of this database.
#[must_use]
pub fn graph_model(&self) -> crate::config::GraphModel {
self.config.graph_model
}
/// Returns the configured memory limit in bytes, if any.
#[must_use]
pub fn memory_limit(&self) -> Option<usize> {
self.config.memory_limit
}
/// Returns a point-in-time snapshot of all metrics.
///
/// If the `metrics` feature is disabled or the registry is not
/// initialized, returns a default (all-zero) snapshot.
#[cfg(feature = "metrics")]
#[must_use]
pub fn metrics(&self) -> crate::metrics::MetricsSnapshot {
let mut snapshot = self
.metrics
.as_ref()
.map_or_else(crate::metrics::MetricsSnapshot::default, |m| m.snapshot());
// Augment with cache stats from the query cache (not tracked in the registry)
let cache_stats = self.query_cache.stats();
snapshot.cache_hits = cache_stats.parsed_hits + cache_stats.optimized_hits;
snapshot.cache_misses = cache_stats.parsed_misses + cache_stats.optimized_misses;
snapshot.cache_size = cache_stats.parsed_size + cache_stats.optimized_size;
snapshot.cache_invalidations = cache_stats.invalidations;
snapshot
}
/// Returns all metrics in Prometheus text exposition format.
///
/// The output is ready to serve from an HTTP `/metrics` endpoint.
#[cfg(feature = "metrics")]
#[must_use]
pub fn metrics_prometheus(&self) -> String {
self.metrics
.as_ref()
.map_or_else(String::new, |m| m.to_prometheus())
}
/// Resets all metrics counters and histograms to zero.
#[cfg(feature = "metrics")]
pub fn reset_metrics(&self) {
if let Some(ref m) = self.metrics {
m.reset();
}
self.query_cache.reset_stats();
}
/// Returns the underlying (default) store.
///
/// This provides direct access to the LPG store for algorithm implementations
/// and admin operations (index management, schema introspection, MVCC internals).
///
/// For code that only needs read/write graph operations, prefer
/// [`graph_store()`](Self::graph_store) which returns the trait interface.
#[must_use]
pub fn store(&self) -> &Arc<LpgStore> {
self.lpg_store()
}
/// Returns the LPG store for the currently active graph.
///
/// If [`current_graph`](Self::current_graph) is `None` or `"default"`, returns
/// the default store. Otherwise looks up the named graph in the root store.
/// Falls back to the default store if the named graph does not exist.
#[allow(dead_code)] // Reserved for future graph-aware CRUD methods
fn active_store(&self) -> Arc<LpgStore> {
let store = self.lpg_store();
let graph_name = self.current_graph.read().clone();
match graph_name {
None => Arc::clone(store),
Some(ref name) if name.eq_ignore_ascii_case("default") => Arc::clone(store),
Some(ref name) => store.graph(name).unwrap_or_else(|| Arc::clone(store)),
}
}
// === Named Graph Management ===
/// Creates a named graph. Returns `true` if created, `false` if it already exists.
///
/// # Errors
///
/// Returns an error if arena allocation fails.
pub fn create_graph(&self, name: &str) -> Result<bool> {
Ok(self.lpg_store().create_graph(name)?)
}
/// Drops a named graph. Returns `true` if dropped, `false` if it did not exist.
pub fn drop_graph(&self, name: &str) -> bool {
self.lpg_store().drop_graph(name)
}
/// Returns all named graph names.
#[must_use]
pub fn list_graphs(&self) -> Vec<String> {
self.lpg_store().graph_names()
}
/// Returns the graph store as a trait object.
///
/// Returns a read-only trait object for the active graph store.
///
/// This provides the [`GraphStore`] interface for code that only needs
/// read operations. For write access, use [`graph_store_mut()`](Self::graph_store_mut).
///
/// [`GraphStore`]: grafeo_core::graph::GraphStore
#[must_use]
pub fn graph_store(&self) -> Arc<dyn GraphStore> {
if let Some(ref ext_read) = self.external_read_store {
Arc::clone(ext_read)
} else {
Arc::clone(self.lpg_store()) as Arc<dyn GraphStore>
}
}
/// Returns the writable graph store, if available.
///
/// Returns `None` for read-only databases created via
/// [`with_read_store()`](Self::with_read_store).
#[must_use]
pub fn graph_store_mut(&self) -> Option<Arc<dyn GraphStoreMut>> {
if self.external_read_store.is_some() {
self.external_write_store.as_ref().map(Arc::clone)
} else {
Some(Arc::clone(self.lpg_store()) as Arc<dyn GraphStoreMut>)
}
}
/// Garbage collects old MVCC versions that are no longer visible.
///
/// Determines the minimum epoch required by active transactions and prunes
/// version chains older than that threshold. Also cleans up completed
/// transaction metadata in the transaction manager.
pub fn gc(&self) {
let min_epoch = self.transaction_manager.min_active_epoch();
self.lpg_store().gc_versions(min_epoch);
self.transaction_manager.gc();
}
/// Returns the buffer manager for memory-aware operations.
#[must_use]
pub fn buffer_manager(&self) -> &Arc<BufferManager> {
&self.buffer_manager
}
/// Returns the query cache.
#[must_use]
pub fn query_cache(&self) -> &Arc<QueryCache> {
&self.query_cache
}
/// Clears all cached query plans.
///
/// This is called automatically after DDL operations, but can also be
/// invoked manually after external schema changes (e.g., WAL replay,
/// import) or when you want to force re-optimization of all queries.
pub fn clear_plan_cache(&self) {
self.query_cache.clear();
}
// =========================================================================
// Lifecycle
// =========================================================================
/// Closes the database, flushing all pending writes.
///
/// For persistent databases, this ensures everything is safely on disk.
/// Called automatically when the database is dropped, but you can call
/// it explicitly if you need to guarantee durability at a specific point.
///
/// # Errors
///
/// Returns an error if the WAL can't be flushed (check disk space/permissions).
pub fn close(&self) -> Result<()> {
let mut is_open = self.is_open.write();
if !*is_open {
return Ok(());
}
// Read-only databases: just release the shared lock, no checkpointing
if self.read_only {
#[cfg(feature = "grafeo-file")]
if let Some(ref fm) = self.file_manager {
fm.close()?;
}
*is_open = false;
return Ok(());
}
// For single-file format: checkpoint to .grafeo file, then clean up sidecar WAL.
// We must do this BEFORE the WAL close path because checkpoint_to_file
// removes the sidecar WAL directory.
#[cfg(feature = "grafeo-file")]
let is_single_file = self.file_manager.is_some();
#[cfg(not(feature = "grafeo-file"))]
let is_single_file = false;
#[cfg(feature = "grafeo-file")]
if let Some(ref fm) = self.file_manager {
// Flush WAL first so all records are on disk before we snapshot
#[cfg(feature = "wal")]
if let Some(ref wal) = self.wal {
wal.sync()?;
}
self.checkpoint_to_file(fm)?;
// Release WAL file handles before removing sidecar directory.
// On Windows, open handles prevent directory deletion.
#[cfg(feature = "wal")]
if let Some(ref wal) = self.wal {
wal.close_active_log();
}
{
use grafeo_core::testing::crash::maybe_crash;
maybe_crash("close:before_remove_sidecar_wal");
}
fm.remove_sidecar_wal()?;
fm.close()?;
}
// Commit and sync WAL (legacy directory format only).
// We intentionally do NOT call wal.checkpoint() here. Directory format
// has no snapshot: the WAL files are the sole source of truth. Writing
// checkpoint.meta would cause recovery to skip older WAL files, losing
// all data that predates the current log sequence.
#[cfg(feature = "wal")]
if !is_single_file && let Some(ref wal) = self.wal {
// Use the last assigned transaction ID, or create one for the commit record
let commit_tx = self
.transaction_manager
.last_assigned_transaction_id()
.unwrap_or_else(|| self.transaction_manager.begin());
// Log a TransactionCommit to mark all pending records as committed
wal.log(&WalRecord::TransactionCommit {
transaction_id: commit_tx,
})?;
wal.sync()?;
}
*is_open = false;
Ok(())
}
/// Returns the typed WAL if available.
#[cfg(feature = "wal")]
#[must_use]
pub fn wal(&self) -> Option<&Arc<LpgWal>> {
self.wal.as_ref()
}
/// Logs a WAL record if WAL is enabled.
#[cfg(feature = "wal")]
pub(super) fn log_wal(&self, record: &WalRecord) -> Result<()> {
if let Some(ref wal) = self.wal {
wal.log(record)?;
}
Ok(())
}
/// Writes the current database snapshot to the `.grafeo` file.
///
/// Does NOT remove the sidecar WAL: callers that want to clean up
/// the sidecar (e.g. `close()`) should call `fm.remove_sidecar_wal()`
/// separately after this returns.
#[cfg(feature = "grafeo-file")]
fn checkpoint_to_file(&self, fm: &GrafeoFileManager) -> Result<()> {
use grafeo_core::testing::crash::maybe_crash;
maybe_crash("checkpoint_to_file:before_export");
let snapshot_data = self.export_snapshot()?;
maybe_crash("checkpoint_to_file:after_export");
let epoch = self.lpg_store().current_epoch();
let transaction_id = self
.transaction_manager
.last_assigned_transaction_id()
.map_or(0, |t| t.0);
let node_count = self.lpg_store().node_count() as u64;
let edge_count = self.lpg_store().edge_count() as u64;
fm.write_snapshot(
&snapshot_data,
epoch.0,
transaction_id,
node_count,
edge_count,
)?;
maybe_crash("checkpoint_to_file:after_write_snapshot");
Ok(())
}
/// Returns the file manager if using single-file format.
#[cfg(feature = "grafeo-file")]
#[must_use]
pub fn file_manager(&self) -> Option<&Arc<GrafeoFileManager>> {
self.file_manager.as_ref()
}
}
impl Drop for GrafeoDB {
fn drop(&mut self) {
if let Err(e) = self.close() {
grafeo_error!("Error closing database: {}", e);
}
}
}
impl crate::admin::AdminService for GrafeoDB {
fn info(&self) -> crate::admin::DatabaseInfo {
self.info()
}
fn detailed_stats(&self) -> crate::admin::DatabaseStats {
self.detailed_stats()
}
fn schema(&self) -> crate::admin::SchemaInfo {
self.schema()
}
fn validate(&self) -> crate::admin::ValidationResult {
self.validate()
}
fn wal_status(&self) -> crate::admin::WalStatus {
self.wal_status()
}
fn wal_checkpoint(&self) -> Result<()> {
self.wal_checkpoint()
}
}
// =========================================================================
// Query Result Types
// =========================================================================
/// The result of running a query.
///
/// Contains rows and columns, like a table. Use [`iter()`](Self::iter) to
/// loop through rows, or [`scalar()`](Self::scalar) if you expect a single value.
///
/// # Examples
///
/// ```
/// use grafeo_engine::GrafeoDB;
///
/// let db = GrafeoDB::new_in_memory();
/// db.create_node(&["Person"]);
///
/// let result = db.execute("MATCH (p:Person) RETURN count(p) AS total")?;
///
/// // Check what we got
/// println!("Columns: {:?}", result.columns);
/// println!("Rows: {}", result.row_count());
///
/// // Iterate through results
/// for row in result.iter() {
/// println!("{:?}", row);
/// }
/// # Ok::<(), grafeo_common::utils::error::Error>(())
/// ```
#[derive(Debug)]
pub struct QueryResult {
/// Column names from the RETURN clause.
pub columns: Vec<String>,
/// Column types - useful for distinguishing NodeId/EdgeId from plain integers.
pub column_types: Vec<grafeo_common::types::LogicalType>,
/// The actual result rows.
pub rows: Vec<Vec<grafeo_common::types::Value>>,
/// Query execution time in milliseconds (if timing was enabled).
pub execution_time_ms: Option<f64>,
/// Number of rows scanned during query execution (estimate).
pub rows_scanned: Option<u64>,
/// Status message for DDL and session commands (e.g., "Created node type 'Person'").
pub status_message: Option<String>,
/// GQLSTATUS code per ISO/IEC 39075:2024, sec 23.
pub gql_status: grafeo_common::utils::GqlStatus,
}
impl QueryResult {
/// Creates a fully empty query result (no columns, no rows).
#[must_use]
pub fn empty() -> Self {
Self {
columns: Vec::new(),
column_types: Vec::new(),
rows: Vec::new(),
execution_time_ms: None,
rows_scanned: None,
status_message: None,
gql_status: grafeo_common::utils::GqlStatus::SUCCESS,
}
}
/// Creates a query result with only a status message (for DDL commands).
#[must_use]
pub fn status(msg: impl Into<String>) -> Self {
Self {
columns: Vec::new(),
column_types: Vec::new(),
rows: Vec::new(),
execution_time_ms: None,
rows_scanned: None,
status_message: Some(msg.into()),
gql_status: grafeo_common::utils::GqlStatus::SUCCESS,
}
}
/// Creates a new empty query result.
#[must_use]
pub fn new(columns: Vec<String>) -> Self {
let len = columns.len();
Self {
columns,
column_types: vec![grafeo_common::types::LogicalType::Any; len],
rows: Vec::new(),
execution_time_ms: None,
rows_scanned: None,
status_message: None,
gql_status: grafeo_common::utils::GqlStatus::SUCCESS,
}
}
/// Creates a new empty query result with column types.
#[must_use]
pub fn with_types(
columns: Vec<String>,
column_types: Vec<grafeo_common::types::LogicalType>,
) -> Self {
Self {
columns,
column_types,
rows: Vec::new(),
execution_time_ms: None,
rows_scanned: None,
status_message: None,
gql_status: grafeo_common::utils::GqlStatus::SUCCESS,
}
}
/// Sets the execution metrics on this result.
pub fn with_metrics(mut self, execution_time_ms: f64, rows_scanned: u64) -> Self {
self.execution_time_ms = Some(execution_time_ms);
self.rows_scanned = Some(rows_scanned);
self
}
/// Returns the execution time in milliseconds, if available.
#[must_use]
pub fn execution_time_ms(&self) -> Option<f64> {
self.execution_time_ms
}
/// Returns the number of rows scanned, if available.
#[must_use]
pub fn rows_scanned(&self) -> Option<u64> {
self.rows_scanned
}
/// Returns the number of rows.
#[must_use]
pub fn row_count(&self) -> usize {
self.rows.len()
}
/// Returns the number of columns.
#[must_use]
pub fn column_count(&self) -> usize {
self.columns.len()
}
/// Returns true if the result is empty.
#[must_use]
pub fn is_empty(&self) -> bool {
self.rows.is_empty()
}
/// Extracts a single value from the result.
///
/// Use this when your query returns exactly one row with one column,
/// like `RETURN count(n)` or `RETURN sum(p.amount)`.
///
/// # Errors
///
/// Returns an error if the result has multiple rows or columns.
pub fn scalar<T: FromValue>(&self) -> Result<T> {
if self.rows.len() != 1 || self.columns.len() != 1 {
return Err(grafeo_common::utils::error::Error::InvalidValue(
"Expected single value".to_string(),
));
}
T::from_value(&self.rows[0][0])
}
/// Returns an iterator over the rows.
pub fn iter(&self) -> impl Iterator<Item = &Vec<grafeo_common::types::Value>> {
self.rows.iter()
}
}
impl std::fmt::Display for QueryResult {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let table = grafeo_common::fmt::format_result_table(
&self.columns,
&self.rows,
self.execution_time_ms,
self.status_message.as_deref(),
);
f.write_str(&table)
}
}
/// Converts a [`grafeo_common::types::Value`] to a concrete Rust type.
///
/// Implemented for common types like `i64`, `f64`, `String`, and `bool`.
/// Used by [`QueryResult::scalar()`] to extract typed values.
pub trait FromValue: Sized {
/// Attempts the conversion, returning an error on type mismatch.
fn from_value(value: &grafeo_common::types::Value) -> Result<Self>;
}
impl FromValue for i64 {
fn from_value(value: &grafeo_common::types::Value) -> Result<Self> {
value
.as_int64()
.ok_or_else(|| grafeo_common::utils::error::Error::TypeMismatch {
expected: "INT64".to_string(),
found: value.type_name().to_string(),
})
}
}
impl FromValue for f64 {
fn from_value(value: &grafeo_common::types::Value) -> Result<Self> {
value
.as_float64()
.ok_or_else(|| grafeo_common::utils::error::Error::TypeMismatch {
expected: "FLOAT64".to_string(),
found: value.type_name().to_string(),
})
}
}
impl FromValue for String {
fn from_value(value: &grafeo_common::types::Value) -> Result<Self> {
value.as_str().map(String::from).ok_or_else(|| {
grafeo_common::utils::error::Error::TypeMismatch {
expected: "STRING".to_string(),
found: value.type_name().to_string(),
}
})
}
}
impl FromValue for bool {
fn from_value(value: &grafeo_common::types::Value) -> Result<Self> {
value
.as_bool()
.ok_or_else(|| grafeo_common::utils::error::Error::TypeMismatch {
expected: "BOOL".to_string(),
found: value.type_name().to_string(),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_in_memory_database() {
let db = GrafeoDB::new_in_memory();
assert_eq!(db.node_count(), 0);
assert_eq!(db.edge_count(), 0);
}
#[test]
fn test_database_config() {
let config = Config::in_memory().with_threads(4).with_query_logging();
let db = GrafeoDB::with_config(config).unwrap();
assert_eq!(db.config().threads, 4);
assert!(db.config().query_logging);
}
#[test]
fn test_database_session() {
let db = GrafeoDB::new_in_memory();
let _session = db.session();
// Session should be created successfully
}
#[cfg(feature = "wal")]
#[test]
fn test_persistent_database_recovery() {
use grafeo_common::types::Value;
use tempfile::tempdir;
let dir = tempdir().unwrap();
let db_path = dir.path().join("test_db");
// Create database and add some data
{
let db = GrafeoDB::open(&db_path).unwrap();
let alix = db.create_node(&["Person"]);
db.set_node_property(alix, "name", Value::from("Alix"));
let gus = db.create_node(&["Person"]);
db.set_node_property(gus, "name", Value::from("Gus"));
let _edge = db.create_edge(alix, gus, "KNOWS");
// Explicitly close to flush WAL
db.close().unwrap();
}
// Reopen and verify data was recovered
{
let db = GrafeoDB::open(&db_path).unwrap();
assert_eq!(db.node_count(), 2);
assert_eq!(db.edge_count(), 1);
// Verify nodes exist
let node0 = db.get_node(grafeo_common::types::NodeId::new(0));
assert!(node0.is_some());
let node1 = db.get_node(grafeo_common::types::NodeId::new(1));
assert!(node1.is_some());
}
}
#[cfg(feature = "wal")]
#[test]
fn test_wal_logging() {
use tempfile::tempdir;
let dir = tempdir().unwrap();
let db_path = dir.path().join("wal_test_db");
let db = GrafeoDB::open(&db_path).unwrap();
// Create some data
let node = db.create_node(&["Test"]);
db.delete_node(node);
// WAL should have records
if let Some(wal) = db.wal() {
assert!(wal.record_count() > 0);
}
db.close().unwrap();
}
#[cfg(feature = "wal")]
#[test]
fn test_wal_recovery_multiple_sessions() {
// Tests that WAL recovery works correctly across multiple open/close cycles
use grafeo_common::types::Value;
use tempfile::tempdir;
let dir = tempdir().unwrap();
let db_path = dir.path().join("multi_session_db");
// Session 1: Create initial data
{
let db = GrafeoDB::open(&db_path).unwrap();
let alix = db.create_node(&["Person"]);
db.set_node_property(alix, "name", Value::from("Alix"));
db.close().unwrap();
}
// Session 2: Add more data
{
let db = GrafeoDB::open(&db_path).unwrap();
assert_eq!(db.node_count(), 1); // Previous data recovered
let gus = db.create_node(&["Person"]);
db.set_node_property(gus, "name", Value::from("Gus"));
db.close().unwrap();
}
// Session 3: Verify all data
{
let db = GrafeoDB::open(&db_path).unwrap();
assert_eq!(db.node_count(), 2);
// Verify properties were recovered correctly
let node0 = db.get_node(grafeo_common::types::NodeId::new(0)).unwrap();
assert!(node0.labels.iter().any(|l| l.as_str() == "Person"));
let node1 = db.get_node(grafeo_common::types::NodeId::new(1)).unwrap();
assert!(node1.labels.iter().any(|l| l.as_str() == "Person"));
}
}
#[cfg(feature = "wal")]
#[test]
fn test_database_consistency_after_mutations() {
// Tests that database remains consistent after a series of create/delete operations
use grafeo_common::types::Value;
use tempfile::tempdir;
let dir = tempdir().unwrap();
let db_path = dir.path().join("consistency_db");
{
let db = GrafeoDB::open(&db_path).unwrap();
// Create nodes
let a = db.create_node(&["Node"]);
let b = db.create_node(&["Node"]);
let c = db.create_node(&["Node"]);
// Create edges
let e1 = db.create_edge(a, b, "LINKS");
let _e2 = db.create_edge(b, c, "LINKS");
// Delete middle node and its edge
db.delete_edge(e1);
db.delete_node(b);
// Set properties on remaining nodes
db.set_node_property(a, "value", Value::Int64(1));
db.set_node_property(c, "value", Value::Int64(3));
db.close().unwrap();
}
// Reopen and verify consistency
{
let db = GrafeoDB::open(&db_path).unwrap();
// Should have 2 nodes (a and c), b was deleted
// Note: node_count includes deleted nodes in some implementations
// What matters is that the non-deleted nodes are accessible
let node_a = db.get_node(grafeo_common::types::NodeId::new(0));
assert!(node_a.is_some());
let node_c = db.get_node(grafeo_common::types::NodeId::new(2));
assert!(node_c.is_some());
// Middle node should be deleted
let node_b = db.get_node(grafeo_common::types::NodeId::new(1));
assert!(node_b.is_none());
}
}
#[cfg(feature = "wal")]
#[test]
fn test_close_is_idempotent() {
// Calling close() multiple times should not cause errors
use tempfile::tempdir;
let dir = tempdir().unwrap();
let db_path = dir.path().join("close_test_db");
let db = GrafeoDB::open(&db_path).unwrap();
db.create_node(&["Test"]);
// First close should succeed
assert!(db.close().is_ok());
// Second close should also succeed (idempotent)
assert!(db.close().is_ok());
}
#[test]
fn test_with_store_external_backend() {
use grafeo_core::graph::lpg::LpgStore;
let external = Arc::new(LpgStore::new().unwrap());
// Seed data on the external store directly
let n1 = external.create_node(&["Person"]);
external.set_node_property(n1, "name", grafeo_common::types::Value::from("Alix"));
let db = GrafeoDB::with_store(
Arc::clone(&external) as Arc<dyn GraphStoreMut>,
Config::in_memory(),
)
.unwrap();
let session = db.session();
// Session should see data from the external store via execute
#[cfg(feature = "gql")]
{
let result = session.execute("MATCH (p:Person) RETURN p.name").unwrap();
assert_eq!(result.rows.len(), 1);
}
}
#[test]
fn test_with_config_custom_memory_limit() {
let config = Config::in_memory().with_memory_limit(64 * 1024 * 1024); // 64 MB
let db = GrafeoDB::with_config(config).unwrap();
assert_eq!(db.config().memory_limit, Some(64 * 1024 * 1024));
assert_eq!(db.node_count(), 0);
}
#[cfg(feature = "metrics")]
#[test]
fn test_database_metrics_registry() {
let db = GrafeoDB::new_in_memory();
// Perform some operations
db.create_node(&["Person"]);
db.create_node(&["Person"]);
// Check that metrics snapshot returns data
let snap = db.metrics();
// Session created counter should reflect at least 0 (metrics is initialized)
assert_eq!(snap.query_count, 0); // No queries executed yet
}
#[test]
fn test_query_result_has_metrics() {
// Verifies that query results include execution metrics
let db = GrafeoDB::new_in_memory();
db.create_node(&["Person"]);
db.create_node(&["Person"]);
#[cfg(feature = "gql")]
{
let result = db.execute("MATCH (n:Person) RETURN n").unwrap();
// Metrics should be populated
assert!(result.execution_time_ms.is_some());
assert!(result.rows_scanned.is_some());
assert!(result.execution_time_ms.unwrap() >= 0.0);
assert_eq!(result.rows_scanned.unwrap(), 2);
}
}
#[test]
fn test_empty_query_result_metrics() {
// Verifies metrics are correct for queries returning no results
let db = GrafeoDB::new_in_memory();
db.create_node(&["Person"]);
#[cfg(feature = "gql")]
{
// Query that matches nothing
let result = db.execute("MATCH (n:NonExistent) RETURN n").unwrap();
assert!(result.execution_time_ms.is_some());
assert!(result.rows_scanned.is_some());
assert_eq!(result.rows_scanned.unwrap(), 0);
}
}
#[cfg(feature = "cdc")]
mod cdc_integration {
use super::*;
/// Helper: creates an in-memory database with CDC enabled.
fn cdc_db() -> GrafeoDB {
GrafeoDB::with_config(Config::in_memory().with_cdc()).unwrap()
}
#[test]
fn test_node_lifecycle_history() {
let db = cdc_db();
// Create
let id = db.create_node(&["Person"]);
// Update
db.set_node_property(id, "name", "Alix".into());
db.set_node_property(id, "name", "Gus".into());
// Delete
db.delete_node(id);
let history = db.history(id).unwrap();
assert_eq!(history.len(), 4); // create + 2 updates + delete
assert_eq!(history[0].kind, crate::cdc::ChangeKind::Create);
assert_eq!(history[1].kind, crate::cdc::ChangeKind::Update);
assert!(history[1].before.is_none()); // first set_node_property has no prior value
assert_eq!(history[2].kind, crate::cdc::ChangeKind::Update);
assert!(history[2].before.is_some()); // second update has prior "Alix"
assert_eq!(history[3].kind, crate::cdc::ChangeKind::Delete);
}
#[test]
fn test_edge_lifecycle_history() {
let db = cdc_db();
let alix = db.create_node(&["Person"]);
let gus = db.create_node(&["Person"]);
let edge = db.create_edge(alix, gus, "KNOWS");
db.set_edge_property(edge, "since", 2024i64.into());
db.delete_edge(edge);
let history = db.history(edge).unwrap();
assert_eq!(history.len(), 3); // create + update + delete
assert_eq!(history[0].kind, crate::cdc::ChangeKind::Create);
assert_eq!(history[1].kind, crate::cdc::ChangeKind::Update);
assert_eq!(history[2].kind, crate::cdc::ChangeKind::Delete);
}
#[test]
fn test_create_node_with_props_cdc() {
let db = cdc_db();
let id = db.create_node_with_props(
&["Person"],
vec![
("name", grafeo_common::types::Value::from("Alix")),
("age", grafeo_common::types::Value::from(30i64)),
],
);
let history = db.history(id).unwrap();
assert_eq!(history.len(), 1);
assert_eq!(history[0].kind, crate::cdc::ChangeKind::Create);
// Props should be captured
let after = history[0].after.as_ref().unwrap();
assert_eq!(after.len(), 2);
}
#[test]
fn test_changes_between() {
let db = cdc_db();
let id1 = db.create_node(&["A"]);
let _id2 = db.create_node(&["B"]);
db.set_node_property(id1, "x", 1i64.into());
// All events should be at the same epoch (in-memory, epoch doesn't advance without tx)
let changes = db
.changes_between(
grafeo_common::types::EpochId(0),
grafeo_common::types::EpochId(u64::MAX),
)
.unwrap();
assert_eq!(changes.len(), 3); // 2 creates + 1 update
}
#[test]
fn test_cdc_disabled_by_default() {
let db = GrafeoDB::new_in_memory();
assert!(!db.is_cdc_enabled());
let id = db.create_node(&["Person"]);
db.set_node_property(id, "name", "Alix".into());
let history = db.history(id).unwrap();
assert!(history.is_empty(), "CDC off by default: no events recorded");
}
#[test]
fn test_session_with_cdc_override_on() {
// Database default is off, but session opts in
let db = GrafeoDB::new_in_memory();
let session = db.session_with_cdc(true);
session.execute("INSERT (:Person {name: 'Alix'})").unwrap();
// The CDC log should have events from the opted-in session
let changes = db
.changes_between(
grafeo_common::types::EpochId(0),
grafeo_common::types::EpochId(u64::MAX),
)
.unwrap();
assert!(
!changes.is_empty(),
"session_with_cdc(true) should record events"
);
}
#[test]
fn test_session_with_cdc_override_off() {
// Database default is on, but session opts out
let db = cdc_db();
let session = db.session_with_cdc(false);
session.execute("INSERT (:Person {name: 'Alix'})").unwrap();
let changes = db
.changes_between(
grafeo_common::types::EpochId(0),
grafeo_common::types::EpochId(u64::MAX),
)
.unwrap();
assert!(
changes.is_empty(),
"session_with_cdc(false) should not record events"
);
}
#[test]
fn test_set_cdc_enabled_runtime() {
let db = GrafeoDB::new_in_memory();
assert!(!db.is_cdc_enabled());
// Enable at runtime
db.set_cdc_enabled(true);
assert!(db.is_cdc_enabled());
let id = db.create_node(&["Person"]);
let history = db.history(id).unwrap();
assert_eq!(history.len(), 1, "CDC enabled at runtime records events");
// Disable again
db.set_cdc_enabled(false);
let id2 = db.create_node(&["Person"]);
let history2 = db.history(id2).unwrap();
assert!(
history2.is_empty(),
"CDC disabled at runtime stops recording"
);
}
}
#[test]
fn test_with_store_basic() {
use grafeo_core::graph::lpg::LpgStore;
let store = Arc::new(LpgStore::new().unwrap());
let n1 = store.create_node(&["Person"]);
store.set_node_property(n1, "name", "Alix".into());
let graph_store = Arc::clone(&store) as Arc<dyn GraphStoreMut>;
let db = GrafeoDB::with_store(graph_store, Config::in_memory()).unwrap();
let result = db.execute("MATCH (n:Person) RETURN n.name").unwrap();
assert_eq!(result.rows.len(), 1);
}
#[test]
fn test_with_store_session() {
use grafeo_core::graph::lpg::LpgStore;
let store = Arc::new(LpgStore::new().unwrap());
let graph_store = Arc::clone(&store) as Arc<dyn GraphStoreMut>;
let db = GrafeoDB::with_store(graph_store, Config::in_memory()).unwrap();
let session = db.session();
let result = session.execute("MATCH (n) RETURN count(n)").unwrap();
assert_eq!(result.rows.len(), 1);
}
#[test]
fn test_with_store_mutations() {
use grafeo_core::graph::lpg::LpgStore;
let store = Arc::new(LpgStore::new().unwrap());
let graph_store = Arc::clone(&store) as Arc<dyn GraphStoreMut>;
let db = GrafeoDB::with_store(graph_store, Config::in_memory()).unwrap();
let mut session = db.session();
// Use an explicit transaction so INSERT and MATCH share the same
// transaction context. With PENDING epochs, uncommitted versions are
// only visible to the owning transaction.
session.begin_transaction().unwrap();
session.execute("INSERT (:Person {name: 'Alix'})").unwrap();
let result = session.execute("MATCH (n:Person) RETURN n.name").unwrap();
assert_eq!(result.rows.len(), 1);
session.commit().unwrap();
}
}