manifoldb 0.1.4

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

#![allow(clippy::map_entry)]
#![allow(clippy::set_contains_or_insert)]
#![allow(clippy::explicit_iter_loop)]

use std::collections::{HashMap, HashSet, VecDeque};

use manifoldb::{Database, EntityId, Value};

// ============================================================================
// Reference Graph Algorithms
// ============================================================================

/// Reference implementation of BFS
fn reference_bfs(
    adjacency: &HashMap<EntityId, Vec<EntityId>>,
    start: EntityId,
) -> Vec<(EntityId, usize)> {
    let mut visited = HashMap::new();
    let mut queue = VecDeque::new();

    visited.insert(start, 0);
    queue.push_back((start, 0));

    while let Some((node, depth)) = queue.pop_front() {
        if let Some(neighbors) = adjacency.get(&node) {
            for &neighbor in neighbors {
                if !visited.contains_key(&neighbor) {
                    visited.insert(neighbor, depth + 1);
                    queue.push_back((neighbor, depth + 1));
                }
            }
        }
    }

    let mut result: Vec<_> = visited.into_iter().collect();
    result.sort_by_key(|(id, _)| id.as_u64());
    result
}

/// Reference implementation of DFS to find all reachable nodes
fn reference_dfs_reachable(
    adjacency: &HashMap<EntityId, Vec<EntityId>>,
    start: EntityId,
) -> HashSet<EntityId> {
    let mut visited = HashSet::new();
    let mut stack = vec![start];

    while let Some(node) = stack.pop() {
        if visited.insert(node) {
            if let Some(neighbors) = adjacency.get(&node) {
                for &neighbor in neighbors {
                    if !visited.contains(&neighbor) {
                        stack.push(neighbor);
                    }
                }
            }
        }
    }

    visited
}

/// Reference implementation for path existence
fn reference_path_exists(
    adjacency: &HashMap<EntityId, Vec<EntityId>>,
    start: EntityId,
    end: EntityId,
) -> bool {
    if start == end {
        return true;
    }

    let mut visited = HashSet::new();
    let mut stack = vec![start];

    while let Some(node) = stack.pop() {
        if node == end {
            return true;
        }

        if visited.insert(node) {
            if let Some(neighbors) = adjacency.get(&node) {
                stack.extend(neighbors.iter().copied());
            }
        }
    }

    false
}

/// Reference implementation for shortest path (BFS-based for unweighted graphs)
fn reference_shortest_path(
    adjacency: &HashMap<EntityId, Vec<EntityId>>,
    start: EntityId,
    end: EntityId,
) -> Option<Vec<EntityId>> {
    if start == end {
        return Some(vec![start]);
    }

    let mut visited = HashSet::new();
    let mut parent: HashMap<EntityId, EntityId> = HashMap::new();
    let mut queue = VecDeque::new();

    visited.insert(start);
    queue.push_back(start);

    while let Some(node) = queue.pop_front() {
        if let Some(neighbors) = adjacency.get(&node) {
            for &neighbor in neighbors {
                if !visited.contains(&neighbor) {
                    visited.insert(neighbor);
                    parent.insert(neighbor, node);
                    queue.push_back(neighbor);

                    if neighbor == end {
                        // Reconstruct path
                        let mut path = vec![end];
                        let mut current = end;
                        while let Some(&p) = parent.get(&current) {
                            path.push(p);
                            current = p;
                            if current == start {
                                break;
                            }
                        }
                        path.reverse();
                        return Some(path);
                    }
                }
            }
        }
    }

    None
}

// ============================================================================
// Reference Vector Algorithms
// ============================================================================

/// Reference Euclidean distance
fn reference_euclidean_distance(a: &[f32], b: &[f32]) -> f32 {
    a.iter().zip(b.iter()).map(|(x, y)| (x - y).powi(2)).sum::<f32>().sqrt()
}

/// Reference cosine distance
fn reference_cosine_distance(a: &[f32], b: &[f32]) -> f32 {
    let dot: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
    let norm_a: f32 = a.iter().map(|x| x * x).sum::<f32>().sqrt();
    let norm_b: f32 = b.iter().map(|x| x * x).sum::<f32>().sqrt();

    if norm_a == 0.0 || norm_b == 0.0 {
        return 1.0; // Maximum distance for zero vectors
    }

    1.0 - (dot / (norm_a * norm_b))
}

/// Reference k-NN search (brute force)
fn reference_knn_euclidean(
    vectors: &[(EntityId, Vec<f32>)],
    query: &[f32],
    k: usize,
) -> Vec<(EntityId, f32)> {
    let mut distances: Vec<_> =
        vectors.iter().map(|(id, v)| (*id, reference_euclidean_distance(v, query))).collect();

    distances.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap());
    distances.truncate(k);
    distances
}

// ============================================================================
// Graph Correctness Tests
// ============================================================================

/// Create a simple graph and verify BFS traversal
#[test]
fn test_bfs_correctness() {
    let db = Database::in_memory().expect("failed to create db");

    // Create a simple graph:
    //     1 -> 2 -> 4
    //     |    |
    //     v    v
    //     3 -> 5
    let mut tx = db.begin().expect("failed");

    let mut ids = Vec::new();
    for i in 1..=5 {
        let entity = tx.create_entity().expect("failed").with_property("id", i);
        ids.push(entity.id);
        tx.put_entity(&entity).expect("failed");
    }

    // Edges
    let edges = [(0, 1), (0, 2), (1, 3), (1, 4), (2, 4)];
    for (src, dst) in edges {
        let edge = tx.create_edge(ids[src], ids[dst], "CONNECTS").expect("failed");
        tx.put_edge(&edge).expect("failed");
    }

    tx.commit().expect("failed");

    // Build reference adjacency list
    let mut adjacency: HashMap<EntityId, Vec<EntityId>> = HashMap::new();
    for (src, dst) in edges {
        adjacency.entry(ids[src]).or_default().push(ids[dst]);
    }

    // Perform BFS using database
    let tx = db.begin_read().expect("failed");
    let mut db_visited = HashMap::new();
    let mut queue = VecDeque::new();

    db_visited.insert(ids[0], 0);
    queue.push_back((ids[0], 0));

    while let Some((node, depth)) = queue.pop_front() {
        let edges = tx.get_outgoing_edges(node).expect("failed");
        for edge in edges {
            if !db_visited.contains_key(&edge.target) {
                db_visited.insert(edge.target, depth + 1);
                queue.push_back((edge.target, depth + 1));
            }
        }
    }

    // Compare with reference
    let ref_result = reference_bfs(&adjacency, ids[0]);

    for (id, expected_depth) in ref_result {
        let actual_depth = db_visited.get(&id);
        assert_eq!(actual_depth, Some(&expected_depth), "BFS depth mismatch for entity {id:?}");
    }
}

/// Verify reachability using DFS
#[test]
fn test_reachability_correctness() {
    let db = Database::in_memory().expect("failed to create db");

    // Create a graph with two disconnected components:
    // Component 1: 1 -> 2 -> 3
    // Component 2: 4 -> 5
    let mut tx = db.begin().expect("failed");

    let mut ids = Vec::new();
    for i in 1..=5 {
        let entity = tx.create_entity().expect("failed").with_property("id", i);
        ids.push(entity.id);
        tx.put_entity(&entity).expect("failed");
    }

    let edges = [(0, 1), (1, 2), (3, 4)];
    for (src, dst) in edges {
        let edge = tx.create_edge(ids[src], ids[dst], "CONNECTS").expect("failed");
        tx.put_edge(&edge).expect("failed");
    }

    tx.commit().expect("failed");

    // Build reference adjacency
    let mut adjacency: HashMap<EntityId, Vec<EntityId>> = HashMap::new();
    for (src, dst) in edges {
        adjacency.entry(ids[src]).or_default().push(ids[dst]);
    }

    // Verify reachability from node 1
    let ref_reachable = reference_dfs_reachable(&adjacency, ids[0]);

    let tx = db.begin_read().expect("failed");
    let mut db_reachable = HashSet::new();
    let mut stack = vec![ids[0]];

    while let Some(node) = stack.pop() {
        if db_reachable.insert(node) {
            let edges = tx.get_outgoing_edges(node).expect("failed");
            for edge in edges {
                stack.push(edge.target);
            }
        }
    }

    assert_eq!(db_reachable, ref_reachable, "reachable sets should match");

    // Node 4 should not be reachable from node 1
    assert!(!db_reachable.contains(&ids[3]));
    assert!(!db_reachable.contains(&ids[4]));
}

/// Verify path existence
#[test]
fn test_path_existence_correctness() {
    let db = Database::in_memory().expect("failed to create db");

    // Create graph:
    // 1 -> 2 -> 3 -> 4
    //      |
    //      v
    //      5
    let mut tx = db.begin().expect("failed");

    let mut ids = Vec::new();
    for i in 1..=5 {
        let entity = tx.create_entity().expect("failed").with_property("id", i);
        ids.push(entity.id);
        tx.put_entity(&entity).expect("failed");
    }

    let edges = [(0, 1), (1, 2), (2, 3), (1, 4)];
    for (src, dst) in edges {
        let edge = tx.create_edge(ids[src], ids[dst], "CONNECTS").expect("failed");
        tx.put_edge(&edge).expect("failed");
    }

    tx.commit().expect("failed");

    let mut adjacency: HashMap<EntityId, Vec<EntityId>> = HashMap::new();
    for (src, dst) in edges {
        adjacency.entry(ids[src]).or_default().push(ids[dst]);
    }

    // Helper to check path in database
    let db_path_exists = |start: EntityId, end: EntityId| -> bool {
        let tx = db.begin_read().expect("failed");
        let mut visited = HashSet::new();
        let mut stack = vec![start];

        while let Some(node) = stack.pop() {
            if node == end {
                return true;
            }
            if visited.insert(node) {
                let edges = tx.get_outgoing_edges(node).expect("failed");
                for edge in edges {
                    stack.push(edge.target);
                }
            }
        }
        false
    };

    // Test various path queries
    let test_cases = [
        (ids[0], ids[3], true),  // 1 -> 4 (exists)
        (ids[0], ids[4], true),  // 1 -> 5 (exists)
        (ids[3], ids[0], false), // 4 -> 1 (no path)
        (ids[4], ids[3], false), // 5 -> 4 (no path)
        (ids[0], ids[0], true),  // 1 -> 1 (self)
    ];

    for (start, end, expected) in test_cases {
        let ref_result = reference_path_exists(&adjacency, start, end);
        let db_result = db_path_exists(start, end);

        assert_eq!(ref_result, expected, "reference path check for {start:?} -> {end:?}");
        assert_eq!(db_result, expected, "database path check for {start:?} -> {end:?}");
        assert_eq!(ref_result, db_result, "results should match");
    }
}

/// Verify shortest path computation
#[test]
fn test_shortest_path_correctness() {
    let db = Database::in_memory().expect("failed to create db");

    // Create a graph with multiple paths:
    // 1 -> 2 -> 3 -> 5
    // 1 -> 4 -> 5
    // Shortest: 1 -> 4 -> 5 (2 edges vs 3 edges)
    let mut tx = db.begin().expect("failed");

    let mut ids = Vec::new();
    for i in 1..=5 {
        let entity = tx.create_entity().expect("failed").with_property("id", i);
        ids.push(entity.id);
        tx.put_entity(&entity).expect("failed");
    }

    let edges = [(0, 1), (1, 2), (2, 4), (0, 3), (3, 4)];
    for (src, dst) in edges {
        let edge = tx.create_edge(ids[src], ids[dst], "CONNECTS").expect("failed");
        tx.put_edge(&edge).expect("failed");
    }

    tx.commit().expect("failed");

    let mut adjacency: HashMap<EntityId, Vec<EntityId>> = HashMap::new();
    for (src, dst) in edges {
        adjacency.entry(ids[src]).or_default().push(ids[dst]);
    }

    // Reference shortest path
    let ref_path = reference_shortest_path(&adjacency, ids[0], ids[4]);

    // Database BFS shortest path
    let db_path = {
        let tx = db.begin_read().expect("failed");
        let mut parent: HashMap<EntityId, EntityId> = HashMap::new();
        let mut queue = VecDeque::new();

        queue.push_back(ids[0]);

        while let Some(node) = queue.pop_front() {
            if node == ids[4] {
                // Found - reconstruct
                let mut path = vec![ids[4]];
                let mut current = ids[4];
                while let Some(&p) = parent.get(&current) {
                    path.push(p);
                    current = p;
                }
                path.reverse();
                break;
            }

            let edges = tx.get_outgoing_edges(node).expect("failed");
            for edge in edges {
                if !parent.contains_key(&edge.target) && edge.target != ids[0] {
                    parent.insert(edge.target, node);
                    queue.push_back(edge.target);
                }
            }
        }

        let mut path = vec![ids[4]];
        let mut current = ids[4];
        while let Some(&p) = parent.get(&current) {
            path.push(p);
            current = p;
        }
        path.reverse();
        Some(path)
    };

    // Both should find a path of length 3 (1 -> 4 -> 5)
    assert!(ref_path.is_some());
    assert!(db_path.is_some());
    assert_eq!(
        ref_path.as_ref().unwrap().len(),
        db_path.as_ref().unwrap().len(),
        "path lengths should match"
    );
    // Note: actual path may differ if there are equal-length alternatives
}

// ============================================================================
// Vector Correctness Tests
// ============================================================================

/// Verify Euclidean distance correctness
#[test]
fn test_euclidean_distance_correctness() {
    // Hand-computed test cases
    let test_cases = [
        // 3-4-5 triangle
        (vec![0.0, 0.0], vec![3.0, 4.0], 5.0),
        // Same point
        (vec![1.0, 2.0, 3.0], vec![1.0, 2.0, 3.0], 0.0),
        // Unit distance
        (vec![0.0], vec![1.0], 1.0),
        // Higher dimension
        (
            vec![1.0, 2.0, 3.0, 4.0],
            vec![5.0, 6.0, 7.0, 8.0],
            8.0, // sqrt(16 + 16 + 16 + 16) = sqrt(64) = 8
        ),
    ];

    for (a, b, expected) in test_cases {
        let result = reference_euclidean_distance(&a, &b);
        assert!(
            (result - expected).abs() < 0.0001,
            "Euclidean({a:?}, {b:?}) = {result}, expected {expected}"
        );
    }
}

/// Verify cosine distance correctness
#[test]
fn test_cosine_distance_correctness() {
    let test_cases = [
        // Same direction (distance = 0)
        (vec![1.0, 0.0], vec![2.0, 0.0], 0.0),
        // Opposite direction (distance = 2)
        (vec![1.0, 0.0], vec![-1.0, 0.0], 2.0),
        // Orthogonal (distance = 1)
        (vec![1.0, 0.0], vec![0.0, 1.0], 1.0),
        // 45 degrees (distance ≈ 0.293)
        (vec![1.0, 0.0], vec![1.0, 1.0], 1.0 - (1.0 / 2.0_f32.sqrt())),
    ];

    for (a, b, expected) in test_cases {
        let result = reference_cosine_distance(&a, &b);
        assert!(
            (result - expected).abs() < 0.001,
            "Cosine({a:?}, {b:?}) = {result}, expected {expected}"
        );
    }
}

/// Verify k-NN results
#[test]
fn test_knn_correctness() {
    let db = Database::in_memory().expect("failed to create db");

    // Create vectors with known distances from origin
    let vectors = vec![
        (1, vec![1.0f32, 0.0, 0.0]), // distance 1
        (2, vec![0.0, 2.0, 0.0]),    // distance 2
        (3, vec![0.0, 0.0, 3.0]),    // distance 3
        (4, vec![2.0, 2.0, 0.0]),    // distance ~2.83
        (5, vec![0.5, 0.5, 0.5]),    // distance ~0.87
    ];

    let mut tx = db.begin().expect("failed");
    let mut ids = Vec::new();

    for (i, v) in &vectors {
        let entity = tx
            .create_entity()
            .expect("failed")
            .with_property("id", *i as i64)
            .with_property("embedding", v.clone());
        ids.push(entity.id);
        tx.put_entity(&entity).expect("failed");
    }

    tx.commit().expect("failed");

    // Query: find 3 nearest to origin
    let query = vec![0.0f32, 0.0, 0.0];

    // Reference k-NN
    let ref_vectors: Vec<_> = ids.iter().zip(vectors.iter().map(|(_, v)| v.clone())).collect();
    let ref_knn = reference_knn_euclidean(
        &ref_vectors.iter().map(|(id, v)| (**id, v.clone())).collect::<Vec<_>>(),
        &query,
        3,
    );

    // Database k-NN
    let tx = db.begin_read().expect("failed");
    let mut db_distances: Vec<(EntityId, f32)> = Vec::new();

    for &id in &ids {
        let entity = tx.get_entity(id).expect("failed").expect("should exist");
        if let Some(Value::Vector(v)) = entity.get_property("embedding") {
            let dist = reference_euclidean_distance(v, &query);
            db_distances.push((id, dist));
        }
    }

    db_distances.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap());
    db_distances.truncate(3);

    // Verify top 3 match
    assert_eq!(ref_knn.len(), 3);
    assert_eq!(db_distances.len(), 3);

    for ((ref_id, ref_dist), (db_id, db_dist)) in ref_knn.iter().zip(db_distances.iter()) {
        assert_eq!(ref_id, db_id, "k-NN order should match");
        assert!((ref_dist - db_dist).abs() < 0.0001, "distances should match");
    }

    // Verify entity 5 is closest (distance ~0.87)
    assert_eq!(db_distances[0].0, ids[4]); // entity 5
}

// ============================================================================
// Property Semantics Tests
// ============================================================================

/// Verify property update semantics
#[test]
fn test_property_update_semantics() {
    let db = Database::in_memory().expect("failed to create db");

    let entity_id: EntityId;

    // Create entity with properties
    {
        let mut tx = db.begin().expect("failed");
        let entity = tx
            .create_entity()
            .expect("failed")
            .with_property("a", 1i64)
            .with_property("b", 2i64)
            .with_property("c", 3i64);
        entity_id = entity.id;
        tx.put_entity(&entity).expect("failed");
        tx.commit().expect("failed");
    }

    // Update: change one, remove one, add one
    {
        let mut tx = db.begin().expect("failed");
        let mut entity = tx.get_entity(entity_id).expect("failed").expect("should exist");

        // Change a
        entity.set_property("a", 10i64);
        // Remove b (by not including it - we need to check if this is supported)
        // Add d
        entity.set_property("d", 4i64);

        tx.put_entity(&entity).expect("failed");
        tx.commit().expect("failed");
    }

    // Verify
    let tx = db.begin_read().expect("failed");
    let entity = tx.get_entity(entity_id).expect("failed").expect("should exist");

    assert_eq!(entity.get_property("a"), Some(&Value::Int(10)));
    assert_eq!(entity.get_property("c"), Some(&Value::Int(3))); // Unchanged
    assert_eq!(entity.get_property("d"), Some(&Value::Int(4)));
    // b should still exist since we didn't explicitly remove it
    assert_eq!(entity.get_property("b"), Some(&Value::Int(2)));
}

/// Verify label semantics
#[test]
fn test_label_semantics() {
    let db = Database::in_memory().expect("failed to create db");

    let entity_id: EntityId;

    // Create entity with labels
    {
        let mut tx = db.begin().expect("failed");
        let entity = tx.create_entity().expect("failed").with_label("A").with_label("B");
        entity_id = entity.id;
        tx.put_entity(&entity).expect("failed");
        tx.commit().expect("failed");
    }

    // Verify
    let tx = db.begin_read().expect("failed");
    let entity = tx.get_entity(entity_id).expect("failed").expect("should exist");

    assert!(entity.has_label("A"));
    assert!(entity.has_label("B"));
    assert!(!entity.has_label("C"));
}

// ============================================================================
// Edge Property Correctness Tests
// ============================================================================

/// Verify edge properties are stored correctly
#[test]
fn test_edge_property_correctness() {
    let db = Database::in_memory().expect("failed to create db");

    let (src_id, dst_id): (EntityId, EntityId);

    // Create entities and edge with properties
    {
        let mut tx = db.begin().expect("failed");

        let src = tx.create_entity().expect("failed").with_label("Source");
        let dst = tx.create_entity().expect("failed").with_label("Destination");
        src_id = src.id;
        dst_id = dst.id;

        tx.put_entity(&src).expect("failed");
        tx.put_entity(&dst).expect("failed");

        let edge = tx
            .create_edge(src_id, dst_id, "WEIGHTED")
            .expect("failed")
            .with_property("weight", 42.5f64)
            .with_property("label", "important");
        tx.put_edge(&edge).expect("failed");

        tx.commit().expect("failed");
    }

    // Verify
    let tx = db.begin_read().expect("failed");
    let edges = tx.get_outgoing_edges(src_id).expect("failed");

    assert_eq!(edges.len(), 1);
    assert_eq!(edges[0].source, src_id);
    assert_eq!(edges[0].target, dst_id);
    assert_eq!(edges[0].edge_type.as_str(), "WEIGHTED");

    if let Some(Value::Float(w)) = edges[0].get_property("weight") {
        assert!((w - 42.5).abs() < 0.001);
    } else {
        panic!("expected weight property");
    }

    assert_eq!(edges[0].get_property("label"), Some(&Value::String("important".to_string())));
}

// ============================================================================
// Bidirectional Edge Tests
// ============================================================================

/// Verify incoming/outgoing edge consistency
#[test]
fn test_edge_bidirectional_consistency() {
    let db = Database::in_memory().expect("failed to create db");

    // Create a small graph
    let mut tx = db.begin().expect("failed");

    let mut ids = Vec::new();
    for i in 0..5 {
        let entity = tx.create_entity().expect("failed").with_property("id", i);
        ids.push(entity.id);
        tx.put_entity(&entity).expect("failed");
    }

    // Create edges
    let edge_specs = [(0, 1), (0, 2), (1, 2), (2, 3), (3, 4)];
    for (src, dst) in edge_specs {
        let edge = tx.create_edge(ids[src], ids[dst], "CONNECTS").expect("failed");
        tx.put_edge(&edge).expect("failed");
    }

    tx.commit().expect("failed");

    // Verify consistency
    let tx = db.begin_read().expect("failed");

    for (src_idx, dst_idx) in edge_specs {
        let src_id = ids[src_idx];
        let dst_id = ids[dst_idx];

        // Check outgoing from src
        let outgoing = tx.get_outgoing_edges(src_id).expect("failed");
        let has_outgoing = outgoing.iter().any(|e| e.target == dst_id);
        assert!(has_outgoing, "should have outgoing edge {src_idx} -> {dst_idx}");

        // Check incoming to dst
        let incoming = tx.get_incoming_edges(dst_id).expect("failed");
        let has_incoming = incoming.iter().any(|e| e.source == src_id);
        assert!(has_incoming, "should have incoming edge {src_idx} -> {dst_idx}");
    }
}

// ============================================================================
// Value Type Correctness Tests
// ============================================================================

/// Verify all value types round-trip correctly
#[test]
fn test_value_type_roundtrip() {
    let db = Database::in_memory().expect("failed to create db");

    let test_values = [
        ("null", Value::Null),
        ("bool_true", Value::Bool(true)),
        ("bool_false", Value::Bool(false)),
        ("int_pos", Value::Int(42)),
        ("int_neg", Value::Int(-42)),
        ("int_max", Value::Int(i64::MAX)),
        ("int_min", Value::Int(i64::MIN)),
        ("float", Value::Float(3.14159)),
        ("float_neg", Value::Float(-273.15)),
        ("string", Value::String("hello world".to_string())),
        ("string_empty", Value::String(String::new())),
        ("string_unicode", Value::String("こんにちは".to_string())),
        ("vector", Value::Vector(vec![1.0, 2.0, 3.0, 4.0, 5.0])),
        ("vector_empty", Value::Vector(Vec::new())),
    ];

    let entity_id: EntityId;

    // Create entity with all types
    {
        let mut tx = db.begin().expect("failed");
        let mut entity = tx.create_entity().expect("failed");

        for (key, value) in &test_values {
            entity.set_property(*key, value.clone());
        }

        entity_id = entity.id;
        tx.put_entity(&entity).expect("failed");
        tx.commit().expect("failed");
    }

    // Verify all types
    let tx = db.begin_read().expect("failed");
    let entity = tx.get_entity(entity_id).expect("failed").expect("should exist");

    for (key, expected) in &test_values {
        let actual = entity.get_property(key);
        assert_eq!(
            actual,
            Some(expected),
            "value mismatch for key '{key}': expected {expected:?}, got {actual:?}"
        );
    }
}

// ============================================================================
// Transaction Semantics Tests
// ============================================================================

/// Verify read transaction sees consistent snapshot
#[test]
fn test_snapshot_consistency() {
    let db = Database::in_memory().expect("failed to create db");

    // Create initial state
    let mut entity_ids = Vec::new();
    {
        let mut tx = db.begin().expect("failed");
        for _ in 0..10 {
            let entity = tx.create_entity().expect("failed").with_property("version", 1i64);
            entity_ids.push(entity.id);
            tx.put_entity(&entity).expect("failed");
        }
        tx.commit().expect("failed");
    }

    // Start read transaction
    let read_tx = db.begin_read().expect("failed");

    // Capture initial state
    let initial_state: Vec<_> = entity_ids
        .iter()
        .map(|&id| {
            let entity = read_tx.get_entity(id).expect("failed").expect("should exist");
            entity.get_property("version").cloned()
        })
        .collect();

    // Update all entities in a new transaction
    {
        let mut tx = db.begin().expect("failed");
        for &id in &entity_ids {
            let mut entity = tx.get_entity(id).expect("failed").expect("should exist");
            entity.set_property("version", 2i64);
            tx.put_entity(&entity).expect("failed");
        }
        tx.commit().expect("failed");
    }

    // Read transaction should still see original values
    let final_state: Vec<_> = entity_ids
        .iter()
        .map(|&id| {
            let entity = read_tx.get_entity(id).expect("failed").expect("should exist");
            entity.get_property("version").cloned()
        })
        .collect();

    assert_eq!(initial_state, final_state, "snapshot should be consistent");

    // All should be version 1
    for state in &final_state {
        assert_eq!(state, &Some(Value::Int(1)));
    }

    // New read transaction should see version 2
    let new_tx = db.begin_read().expect("failed");
    for &id in &entity_ids {
        let entity = new_tx.get_entity(id).expect("failed").expect("should exist");
        assert_eq!(entity.get_property("version"), Some(&Value::Int(2)));
    }
}