sombra 0.3.6

High-performance graph database with ACID transactions, single-file storage, and bindings for Rust, TypeScript, and Python
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
use super::*;
use crate::model::{Edge, Node, PropertyValue};
use tempfile::NamedTempFile;

fn create_temp_db(name: &str) -> std::path::PathBuf {
    let tmp = tempfile::Builder::new()
        .prefix(name)
        .suffix(".db")
        .tempfile()
        .expect("create temp file");
    tmp.path().to_path_buf()
}

#[test]
fn graphdb_round_trip() {
    let tmp = NamedTempFile::new().expect("temp file");
    let path = tmp.path().to_path_buf();

    {
        let mut db = GraphDB::open(&path).expect("open db");
        let mut tx = db.begin_transaction().expect("begin transaction");

        let mut node_a = Node::new(0);
        node_a.labels.push("Alpha".into());
        let mut node_b = Node::new(0);
        node_b.labels.push("Beta".into());

        let a_id = tx.add_node(node_a).expect("add node a");
        let b_id = tx.add_node(node_b).expect("add node b");

        let edge = Edge::new(0, a_id, b_id, "LINKS");
        tx.add_edge(edge).expect("add edge");

        tx.commit().expect("commit transaction");
        db.checkpoint().expect("checkpoint");
    }

    let mut db = GraphDB::open(&path).expect("reopen db");
    let node_a = db.get_node(1).expect("get node a").expect("node exists");
    assert_eq!(node_a.labels, vec!["Alpha".to_string()]);
    let neighbors = db.get_neighbors(1).expect("neighbors");
    assert_eq!(neighbors, vec![2]);
}

#[test]
fn delete_edge_updates_adjacency_lists() {
    let tmp = NamedTempFile::new().expect("temp file");
    let path = tmp.path().to_path_buf();

    let mut db = GraphDB::open(&path).expect("open db");
    let mut tx = db.begin_transaction().expect("begin transaction");

    let a = tx.add_node(Node::new(0)).expect("add node A");
    let b = tx.add_node(Node::new(0)).expect("add node B");
    let c = tx.add_node(Node::new(0)).expect("add node C");

    let edge_ab = Edge::new(0, a, b, "link");
    let edge_ac = Edge::new(0, a, c, "link");
    let ab_id = tx.add_edge(edge_ab).expect("add edge ab");
    tx.add_edge(edge_ac).expect("add edge ac");

    tx.delete_edge(ab_id).expect("delete edge");
    tx.commit().expect("commit");
    db.checkpoint().expect("checkpoint");
    let mut neighbors = db.get_neighbors(a).expect("neighbors");
    neighbors.sort_unstable();
    assert_eq!(neighbors, vec![c]);

    {
        let mut tx = db.begin_transaction().expect("begin transaction");
        tx.add_edge(Edge::new(0, a, b, "link"))
            .expect("re-add edge");
        tx.commit().expect("commit");
        db.checkpoint().expect("checkpoint");
    }
    let mut neighbors = db.get_neighbors(a).expect("neighbors after reinsert");
    neighbors.sort_unstable();
    assert_eq!(neighbors, vec![b, c]);
}

#[test]
fn delete_node_cascades_edge_removal() {
    let tmp = NamedTempFile::new().expect("temp file");
    let path = tmp.path().to_path_buf();

    let mut db = GraphDB::open(&path).expect("open db");
    let mut tx = db.begin_transaction().expect("begin transaction");

    let a = tx.add_node(Node::new(0)).expect("add node A");
    let b = tx.add_node(Node::new(0)).expect("add node B");
    let c = tx.add_node(Node::new(0)).expect("add node C");

    tx.add_edge(Edge::new(0, a, b, "link"))
        .expect("add edge a->b");
    tx.add_edge(Edge::new(0, c, a, "link"))
        .expect("add edge c->a");

    tx.delete_node(a).expect("delete node a");
    tx.commit().expect("commit");
    db.checkpoint().expect("checkpoint");
    assert!(
        db.get_node(a).expect("get_node succeeds").is_none(),
        "deleted node should not exist"
    );
    assert!(db.get_neighbors(c).expect("neighbors of c").is_empty());
    {
        let mut tx = db.begin_transaction().expect("begin transaction");
        tx.add_edge(Edge::new(0, c, b, "link"))
            .expect("add new edge without deleted node");
        tx.commit().expect("commit");
        db.checkpoint().expect("checkpoint");
    }
}

#[test]
fn transaction_commit_persists_changes() {
    let tmp = NamedTempFile::new().expect("temp file");
    let path = tmp.path().to_path_buf();

    {
        let mut db = GraphDB::open(&path).expect("open db");
        let mut tx = db.begin_transaction().expect("begin transaction");
        let node_id = tx.add_node(Node::new(0)).expect("add node in tx");
        assert_eq!(node_id, 1);
        tx.commit().expect("commit transaction");
    }

    let mut db = GraphDB::open(&path).expect("reopen db");
    let node = db
        .get_node(1)
        .expect("read committed node")
        .expect("node exists");
    assert_eq!(node.id, 1);
}

#[test]
fn dropping_active_transaction_panics() {
    let tmp = NamedTempFile::new().expect("temp file");
    let path = tmp.path().to_path_buf();
    let result = std::panic::catch_unwind(|| {
        let mut db = GraphDB::open(&path).expect("open db");
        let _tx = db.begin_transaction().expect("begin transaction");
    });
    assert!(result.is_err(), "dropping active transaction should panic");
}

#[test]
fn rollback_restores_state() {
    let tmp = NamedTempFile::new().expect("temp file");
    let path = tmp.path().to_path_buf();
    let mut db = GraphDB::open(&path).expect("open db");
    {
        let mut tx = db.begin_transaction().expect("begin tx");
        let node_id = tx.add_node(Node::new(0)).expect("add node");
        assert_eq!(node_id, 1);
        tx.rollback().expect("rollback should succeed");
    }

    assert!(
        db.get_node(1).expect("get_node succeeds").is_none(),
        "rolled back node should not exist"
    );

    {
        let mut tx = db.begin_transaction().expect("begin second tx");
        let node_id = tx.add_node(Node::new(0)).expect("add node after rollback");
        assert_eq!(
            node_id, 1,
            "node IDs should reset after rollback to previous state"
        );
        dbg!(&tx.dirty_pages);
        assert!(
            tx.dirty_pages.iter().any(|&page_id| page_id > 0),
            "expected data pages to be tracked as dirty"
        );
        tx.commit().expect("commit second tx");
    }

    drop(db);
    let mut reopened = GraphDB::open(&path).expect("reopen db");
    let node = reopened
        .get_node(1)
        .expect("node committed after rollback")
        .expect("node exists");
    assert_eq!(node.id, 1);
}

#[test]
fn rollback_prevents_eviction_corruption() {
    use crate::db::config::Config;

    let tmp = NamedTempFile::new().expect("temp file");
    let path = tmp.path().to_path_buf();

    let config = Config {
        page_cache_size: 3,
        ..Config::default()
    };
    let mut db = GraphDB::open_with_config(&path, config).expect("open db");

    let initial_node_id = {
        let mut tx = db.begin_transaction().expect("begin tx");
        let node_id = tx.add_node(Node::new(0)).expect("add initial node");
        tx.commit().expect("commit initial node");
        node_id
    };

    {
        let mut tx = db.begin_transaction().expect("begin tx");
        let node = tx
            .get_node(initial_node_id)
            .expect("get initial node")
            .expect("node exists");
        assert_eq!(node.id, initial_node_id);

        for i in 0..5 {
            let mut node = Node::new(0);
            node.labels.push(format!("Label{}", i));
            tx.add_node(node).expect("add node");
        }

        tx.rollback().expect("rollback should succeed");
    }

    let node = db
        .get_node(initial_node_id)
        .expect("initial node should still exist")
        .expect("node exists");
    assert_eq!(node.id, initial_node_id);

    for i in 0..5 {
        let result = db
            .get_node(initial_node_id + 1 + i as u64)
            .expect("get_node succeeds");
        assert!(
            result.is_none(),
            "rolled back node {} should not exist",
            initial_node_id + 1 + i as u64
        );
    }
}

#[test]
fn transaction_tracks_dirty_pages_for_mutations() {
    let tmp = NamedTempFile::new().expect("temp file");
    let path = tmp.path().to_path_buf();

    let mut db = GraphDB::open(&path).expect("open db");
    let mut tx = db.begin_transaction().expect("begin tx");
    tx.add_node(Node::new(0)).expect("add node");
    assert!(
        !tx.dirty_pages.is_empty(),
        "expected dirty pages after node insertion"
    );
    assert!(
        tx.dirty_pages.iter().all(|&page_id| page_id > 0),
        "record pages should have positive IDs"
    );
    tx.commit().expect("commit tx");
}

#[test]
fn transaction_tracking_resets_between_transactions() {
    let tmp = NamedTempFile::new().expect("temp file");
    let path = tmp.path().to_path_buf();

    let mut db = GraphDB::open(&path).expect("open db");
    {
        let mut tx = db.begin_transaction().expect("begin tx");
        tx.add_node(Node::new(0)).expect("add node");
        tx.commit().expect("commit tx");
    }

    let tx = db.begin_transaction().expect("begin second tx");
    assert!(
        tx.dirty_pages.is_empty(),
        "dirty page list should be empty for new transaction"
    );
    tx.commit().expect("commit empty tx");
}

#[test]
fn get_nodes_by_label_returns_matching_nodes() {
    let tmp = NamedTempFile::new().expect("temp file");
    let path = tmp.path().to_path_buf();

    let mut db = GraphDB::open(&path).expect("open db");
    let mut tx = db.begin_transaction().expect("begin transaction");

    let mut person1 = Node::new(0);
    person1.labels.push("Person".into());
    let p1_id = tx.add_node(person1).expect("add person1");

    let mut person2 = Node::new(0);
    person2.labels.push("Person".into());
    let p2_id = tx.add_node(person2).expect("add person2");

    let mut company = Node::new(0);
    company.labels.push("Company".into());
    let _c_id = tx.add_node(company).expect("add company");

    tx.commit().expect("commit transaction");
    db.checkpoint().expect("checkpoint");

    let person_nodes = db.get_nodes_by_label("Person").expect("get Person nodes");
    assert_eq!(person_nodes.len(), 2);
    assert!(person_nodes.contains(&p1_id));
    assert!(person_nodes.contains(&p2_id));

    let company_nodes = db.get_nodes_by_label("Company").expect("get Company nodes");
    assert_eq!(company_nodes.len(), 1);

    let nonexistent = db
        .get_nodes_by_label("NonExistent")
        .expect("get nonexistent label");
    assert_eq!(nonexistent.len(), 0);
}

#[test]
fn get_nodes_by_label_works_across_transactions() {
    let tmp = NamedTempFile::new().expect("temp file");
    let path = tmp.path().to_path_buf();

    let mut db = GraphDB::open(&path).expect("open db");

    {
        let mut tx = db.begin_transaction().expect("begin transaction");
        let mut user = Node::new(0);
        user.labels.push("User".into());
        tx.add_node(user).expect("add user");
        tx.commit().expect("commit transaction");
        db.checkpoint().expect("checkpoint");
    }

    {
        let mut tx = db.begin_transaction().expect("begin transaction");
        let mut user = Node::new(0);
        user.labels.push("User".into());
        tx.add_node(user).expect("add user");
        tx.commit().expect("commit transaction");
        db.checkpoint().expect("checkpoint");
    }

    let users = db.get_nodes_by_label("User").expect("get User nodes");
    assert_eq!(users.len(), 2);
}

#[test]
fn count_edges_returns_correct_counts() {
    let tmp = NamedTempFile::new().expect("temp file");
    let path = tmp.path().to_path_buf();

    let mut db = GraphDB::open(&path).expect("open db");
    let mut tx = db.begin_transaction().expect("begin transaction");

    let a = tx.add_node(Node::new(0)).expect("add node A");
    let b = tx.add_node(Node::new(0)).expect("add node B");
    let c = tx.add_node(Node::new(0)).expect("add node C");
    let d = tx.add_node(Node::new(0)).expect("add node D");

    tx.add_edge(Edge::new(0, a, b, "link"))
        .expect("add edge a->b");
    tx.add_edge(Edge::new(0, a, c, "link"))
        .expect("add edge a->c");
    tx.add_edge(Edge::new(0, a, d, "link"))
        .expect("add edge a->d");
    tx.add_edge(Edge::new(0, b, a, "link"))
        .expect("add edge b->a");

    tx.commit().expect("commit");
    db.checkpoint().expect("checkpoint");

    let outgoing_a = db.count_outgoing_edges(a).expect("count outgoing for a");
    assert_eq!(outgoing_a, 3);

    let incoming_a = db.count_incoming_edges(a).expect("count incoming for a");
    assert_eq!(incoming_a, 1);

    let outgoing_b = db.count_outgoing_edges(b).expect("count outgoing for b");
    assert_eq!(outgoing_b, 1);

    let incoming_b = db.count_incoming_edges(b).expect("count incoming for b");
    assert_eq!(incoming_b, 1);

    let outgoing_c = db.count_outgoing_edges(c).expect("count outgoing for c");
    assert_eq!(outgoing_c, 0);

    let incoming_c = db.count_incoming_edges(c).expect("count incoming for c");
    assert_eq!(incoming_c, 1);
}

#[test]
fn get_incoming_neighbors_returns_correct_nodes() {
    let tmp = NamedTempFile::new().expect("temp file");
    let path = tmp.path().to_path_buf();

    let mut db = GraphDB::open(&path).expect("open db");
    let mut tx = db.begin_transaction().expect("begin transaction");

    let hub = tx.add_node(Node::new(0)).expect("add hub");
    let n1 = tx.add_node(Node::new(0)).expect("add n1");
    let n2 = tx.add_node(Node::new(0)).expect("add n2");
    let n3 = tx.add_node(Node::new(0)).expect("add n3");

    tx.add_edge(Edge::new(0, n1, hub, "points_to"))
        .expect("add edge n1->hub");
    tx.add_edge(Edge::new(0, n2, hub, "points_to"))
        .expect("add edge n2->hub");
    tx.add_edge(Edge::new(0, n3, hub, "points_to"))
        .expect("add edge n3->hub");
    tx.add_edge(Edge::new(0, hub, n1, "points_to"))
        .expect("add edge hub->n1");

    tx.commit().expect("commit");
    db.checkpoint().expect("checkpoint");

    let mut incoming = db
        .get_incoming_neighbors(hub)
        .expect("get incoming neighbors");
    incoming.sort_unstable();
    let mut expected = vec![n1, n2, n3];
    expected.sort_unstable();
    assert_eq!(incoming, expected);

    let incoming_n1 = db
        .get_incoming_neighbors(n1)
        .expect("get incoming neighbors for n1");
    assert_eq!(incoming_n1.len(), 1);
    assert_eq!(incoming_n1[0], hub);
}

#[test]
fn bfs_traversal_explores_graph_by_depth() {
    let tmp = NamedTempFile::new().expect("temp file");
    let path = tmp.path().to_path_buf();

    let mut db = GraphDB::open(&path).expect("open db");
    let mut tx = db.begin_transaction().expect("begin transaction");

    let root = tx.add_node(Node::new(0)).expect("add root");
    let l1_a = tx.add_node(Node::new(0)).expect("add l1_a");
    let l1_b = tx.add_node(Node::new(0)).expect("add l1_b");
    let l2_a = tx.add_node(Node::new(0)).expect("add l2_a");
    let l2_b = tx.add_node(Node::new(0)).expect("add l2_b");

    tx.add_edge(Edge::new(0, root, l1_a, "link"))
        .expect("root->l1_a");
    tx.add_edge(Edge::new(0, root, l1_b, "link"))
        .expect("root->l1_b");
    tx.add_edge(Edge::new(0, l1_a, l2_a, "link"))
        .expect("l1_a->l2_a");
    tx.add_edge(Edge::new(0, l1_b, l2_b, "link"))
        .expect("l1_b->l2_b");

    tx.commit().expect("commit");
    db.checkpoint().expect("checkpoint");

    let results = db.bfs_traversal(root, 2).expect("bfs traversal");

    assert_eq!(results.len(), 5);

    let root_result = results
        .iter()
        .find(|(id, _)| *id == root)
        .expect("find root");
    assert_eq!(root_result.1, 0);

    let l1_a_result = results
        .iter()
        .find(|(id, _)| *id == l1_a)
        .expect("find l1_a");
    assert_eq!(l1_a_result.1, 1);

    let l1_b_result = results
        .iter()
        .find(|(id, _)| *id == l1_b)
        .expect("find l1_b");
    assert_eq!(l1_b_result.1, 1);

    let l2_a_result = results
        .iter()
        .find(|(id, _)| *id == l2_a)
        .expect("find l2_a");
    assert_eq!(l2_a_result.1, 2);

    let l2_b_result = results
        .iter()
        .find(|(id, _)| *id == l2_b)
        .expect("find l2_b");
    assert_eq!(l2_b_result.1, 2);

    let shallow_results = db.bfs_traversal(root, 0).expect("shallow bfs");
    assert_eq!(shallow_results.len(), 1);
}

#[test]
fn property_index_basic_operations() {
    let temp_file = create_temp_db("property_index_basic");
    let mut db = GraphDB::open(&temp_file).expect("open db");

    let mut alice = Node::new(0);
    alice.labels.push("Person".to_string());
    alice.properties.insert(
        "name".to_string(),
        PropertyValue::String("Alice".to_string()),
    );
    alice
        .properties
        .insert("age".to_string(), PropertyValue::Int(30));

    let mut bob = Node::new(0);
    bob.labels.push("Person".to_string());
    bob.properties
        .insert("name".to_string(), PropertyValue::String("Bob".to_string()));
    bob.properties
        .insert("age".to_string(), PropertyValue::Int(25));

    let mut charlie = Node::new(0);
    charlie.labels.push("Person".to_string());
    charlie.properties.insert(
        "name".to_string(),
        PropertyValue::String("Charlie".to_string()),
    );
    charlie
        .properties
        .insert("age".to_string(), PropertyValue::Int(30));

    let alice_id = db.add_node(alice).expect("add alice");
    let bob_id = db.add_node(bob).expect("add bob");
    let charlie_id = db.add_node(charlie).expect("add charlie");

    db.create_property_index("Person", "age")
        .expect("create property index");

    let age_30_nodes = db
        .find_nodes_by_property("Person", "age", &PropertyValue::Int(30))
        .expect("find nodes with age 30");

    assert_eq!(age_30_nodes.len(), 2);
    assert!(age_30_nodes.contains(&alice_id));
    assert!(age_30_nodes.contains(&charlie_id));

    let age_25_nodes = db
        .find_nodes_by_property("Person", "age", &PropertyValue::Int(25))
        .expect("find nodes with age 25");

    assert_eq!(age_25_nodes.len(), 1);
    assert_eq!(age_25_nodes[0], bob_id);

    let age_40_nodes = db
        .find_nodes_by_property("Person", "age", &PropertyValue::Int(40))
        .expect("find nodes with age 40");

    assert_eq!(age_40_nodes.len(), 0);
}

#[test]
fn property_index_survives_reopen() {
    let temp_file = create_temp_db("property_index_reopen");

    let (alice_id, bob_id) = {
        let mut db = GraphDB::open(&temp_file).expect("open db");

        let mut alice = Node::new(0);
        alice.labels.push("Person".to_string());
        alice
            .properties
            .insert("age".to_string(), PropertyValue::Int(30));

        let mut bob = Node::new(0);
        bob.labels.push("Person".to_string());
        bob.properties
            .insert("age".to_string(), PropertyValue::Int(25));

        let alice_id = db.add_node(alice).expect("add alice");
        let bob_id = db.add_node(bob).expect("add bob");

        db.create_property_index("Person", "age")
            .expect("create property index");

        db.checkpoint().expect("checkpoint");

        (alice_id, bob_id)
    };

    let mut db = GraphDB::open(&temp_file).expect("reopen db");

    db.create_property_index("Person", "age")
        .expect("recreate property index");

    let age_30_nodes = db
        .find_nodes_by_property("Person", "age", &PropertyValue::Int(30))
        .expect("find nodes with age 30");

    assert_eq!(age_30_nodes.len(), 1);
    assert_eq!(age_30_nodes[0], alice_id);

    let age_25_nodes = db
        .find_nodes_by_property("Person", "age", &PropertyValue::Int(25))
        .expect("find nodes with age 25");

    assert_eq!(age_25_nodes.len(), 1);
    assert_eq!(age_25_nodes[0], bob_id);
}

#[test]
fn property_index_with_string_values() {
    let temp_file = create_temp_db("property_index_string");
    let mut db = GraphDB::open(&temp_file).expect("open db");

    let mut alice = Node::new(0);
    alice.labels.push("Person".to_string());
    alice
        .properties
        .insert("city".to_string(), PropertyValue::String("NYC".to_string()));

    let mut bob = Node::new(0);
    bob.labels.push("Person".to_string());
    bob.properties
        .insert("city".to_string(), PropertyValue::String("SF".to_string()));

    let mut charlie = Node::new(0);
    charlie.labels.push("Person".to_string());
    charlie
        .properties
        .insert("city".to_string(), PropertyValue::String("NYC".to_string()));

    let alice_id = db.add_node(alice).expect("add alice");
    let _bob_id = db.add_node(bob).expect("add bob");
    let charlie_id = db.add_node(charlie).expect("add charlie");

    db.create_property_index("Person", "city")
        .expect("create property index");

    let nyc_nodes = db
        .find_nodes_by_property("Person", "city", &PropertyValue::String("NYC".to_string()))
        .expect("find nodes in NYC");

    assert_eq!(nyc_nodes.len(), 2);
    assert!(nyc_nodes.contains(&alice_id));
    assert!(nyc_nodes.contains(&charlie_id));
}

#[test]
fn property_index_updated_on_node_deletion() {
    let temp_file = create_temp_db("property_index_deletion");
    let mut db = GraphDB::open(&temp_file).expect("open db");

    let mut alice = Node::new(0);
    alice.labels.push("Person".to_string());
    alice
        .properties
        .insert("age".to_string(), PropertyValue::Int(30));

    let mut bob = Node::new(0);
    bob.labels.push("Person".to_string());
    bob.properties
        .insert("age".to_string(), PropertyValue::Int(30));

    let alice_id = db.add_node(alice).expect("add alice");
    let bob_id = db.add_node(bob).expect("add bob");

    db.create_property_index("Person", "age")
        .expect("create property index");

    let age_30_nodes = db
        .find_nodes_by_property("Person", "age", &PropertyValue::Int(30))
        .expect("find nodes with age 30");
    assert_eq!(age_30_nodes.len(), 2);

    db.delete_node(alice_id).expect("delete alice");

    let age_30_nodes_after = db
        .find_nodes_by_property("Person", "age", &PropertyValue::Int(30))
        .expect("find nodes with age 30 after deletion");

    assert_eq!(age_30_nodes_after.len(), 1);
    assert_eq!(age_30_nodes_after[0], bob_id);
}

#[test]
fn property_index_falls_back_to_scan_when_not_indexed() {
    let temp_file = create_temp_db("property_index_fallback");
    let mut db = GraphDB::open(&temp_file).expect("open db");

    let mut alice = Node::new(0);
    alice.labels.push("Person".to_string());
    alice
        .properties
        .insert("age".to_string(), PropertyValue::Int(30));

    let alice_id = db.add_node(alice).expect("add alice");

    let age_30_nodes = db
        .find_nodes_by_property("Person", "age", &PropertyValue::Int(30))
        .expect("find nodes with age 30 without index");

    assert_eq!(age_30_nodes.len(), 1);
    assert_eq!(age_30_nodes[0], alice_id);
}

#[test]
fn property_index_persists_across_checkpoint_and_reopen() {
    let temp_file = create_temp_db("property_index_persistence");

    {
        let mut db = GraphDB::open(&temp_file).expect("open db");

        let mut alice = Node::new(0);
        alice.labels.push("Person".to_string());
        alice.properties.insert(
            "name".to_string(),
            PropertyValue::String("Alice".to_string()),
        );
        alice
            .properties
            .insert("age".to_string(), PropertyValue::Int(30));

        let mut bob = Node::new(0);
        bob.labels.push("Person".to_string());
        bob.properties
            .insert("name".to_string(), PropertyValue::String("Bob".to_string()));
        bob.properties
            .insert("age".to_string(), PropertyValue::Int(25));

        let mut charlie = Node::new(0);
        charlie.labels.push("Person".to_string());
        charlie.properties.insert(
            "name".to_string(),
            PropertyValue::String("Charlie".to_string()),
        );
        charlie
            .properties
            .insert("age".to_string(), PropertyValue::Int(30));

        let alice_id = db.add_node(alice).expect("add alice");
        let _bob_id = db.add_node(bob).expect("add bob");
        let charlie_id = db.add_node(charlie).expect("add charlie");

        db.create_property_index("Person", "age")
            .expect("create age index");
        db.create_property_index("Person", "name")
            .expect("create name index");

        let age_30_nodes = db
            .find_nodes_by_property("Person", "age", &PropertyValue::Int(30))
            .expect("find age 30");
        assert_eq!(age_30_nodes.len(), 2);
        assert!(age_30_nodes.contains(&alice_id));
        assert!(age_30_nodes.contains(&charlie_id));

        db.checkpoint().expect("checkpoint");
    }

    {
        let mut db = GraphDB::open(&temp_file).expect("reopen db");

        let age_30_nodes = db
            .find_nodes_by_property("Person", "age", &PropertyValue::Int(30))
            .expect("find age 30 after reopen");
        assert_eq!(age_30_nodes.len(), 2);

        let age_25_nodes = db
            .find_nodes_by_property("Person", "age", &PropertyValue::Int(25))
            .expect("find age 25 after reopen");
        assert_eq!(age_25_nodes.len(), 1);

        let alice_nodes = db
            .find_nodes_by_property(
                "Person",
                "name",
                &PropertyValue::String("Alice".to_string()),
            )
            .expect("find alice by name");
        assert_eq!(alice_nodes.len(), 1);

        let nonexistent_nodes = db
            .find_nodes_by_property("Person", "age", &PropertyValue::Int(99))
            .expect("find nonexistent age");
        assert_eq!(nonexistent_nodes.len(), 0);
    }
}