sqlitegraph 2.2.2

Embedded graph database with full ACID transactions, HNSW vector search, dual backend support, and comprehensive graph algorithms library
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
//! Tests for cache-enabled fast-path pattern matching.
//!
//! This test suite validates that the fast-path optimization produces
//! identical results to the SQL path while improving performance.

use serde_json::json;
use sqlitegraph::{
    GraphEdge, GraphEntity, PatternTriple, SqliteGraph,
    backend::BackendDirection,
    index::{add_label, add_property},
};

/// Create a test graph with sample data for fast-path testing
fn create_test_graph() -> SqliteGraph {
    let graph = SqliteGraph::open_in_memory().expect("Failed to create test graph");

    // Insert functions
    let f1 = insert_entity(&graph, "Function", "process_data");
    let f2 = insert_entity(&graph, "Function", "validate_input");
    let f3 = insert_entity(&graph, "Function", "handle_error");
    let f4 = insert_entity(&graph, "Function", "log_result");

    // Insert structs
    let s1 = insert_entity(&graph, "Struct", "DataProcessor");
    let s2 = insert_entity(&graph, "Struct", "ErrorHandler");

    // Add labels for better filtering
    add_label_to_entity(&graph, f1, "public");
    add_label_to_entity(&graph, f2, "private");
    add_label_to_entity(&graph, f3, "private");
    add_label_to_entity(&graph, f4, "public");
    add_label_to_entity(&graph, s1, "exported");
    add_label_to_entity(&graph, s2, "internal");

    // Add properties for filtering
    add_property_to_entity(&graph, f1, "language", "rust");
    add_property_to_entity(&graph, f2, "language", "rust");
    add_property_to_entity(&graph, f3, "language", "rust");
    add_property_to_entity(&graph, f4, "language", "rust");
    add_property_to_entity(&graph, f1, "async", "true");
    add_property_to_entity(&graph, f2, "async", "false");
    add_property_to_entity(&graph, f3, "async", "false");
    add_property_to_entity(&graph, f4, "async", "true");

    add_property_to_entity(&graph, s1, "thread_safe", "true");
    add_property_to_entity(&graph, s2, "thread_safe", "false");

    // Insert edges (CALLS relationships)
    insert_edge(&graph, f1, f2, "CALLS");
    insert_edge(&graph, f1, f3, "CALLS");
    insert_edge(&graph, f2, s1, "USES");
    insert_edge(&graph, f3, s2, "USES");
    insert_edge(&graph, f4, s1, "USES");
    insert_edge(&graph, f1, f4, "CALLS");

    // Insert some edges with different types
    insert_edge(&graph, f1, s1, "BELONGS_TO");
    insert_edge(&graph, f2, s2, "BELONGS_TO");

    graph
}

fn insert_entity(graph: &SqliteGraph, kind: &str, name: &str) -> i64 {
    graph
        .insert_entity(&GraphEntity {
            id: 0,
            kind: kind.into(),
            name: name.into(),
            file_path: None,
            data: json!({"name": name, "type": kind}),
        })
        .expect("Failed to insert entity")
}

fn insert_edge(graph: &SqliteGraph, from: i64, to: i64, edge_type: &str) -> i64 {
    graph
        .insert_edge(&GraphEdge {
            id: 0,
            from_id: from,
            to_id: to,
            edge_type: edge_type.into(),
            data: json!({"type": edge_type}),
        })
        .expect("Failed to insert edge")
}

fn add_label_to_entity(graph: &SqliteGraph, entity_id: i64, label: &str) {
    add_label(graph, entity_id, label).expect("Failed to add label");
}

fn add_property_to_entity(graph: &SqliteGraph, entity_id: i64, key: &str, value: &str) {
    add_property(graph, entity_id, key, value).expect("Failed to add property");
}

fn get_entity_by_name(graph: &SqliteGraph, name: &str) -> i64 {
    let entity_ids = graph.list_entity_ids().expect("Failed to get entity IDs");
    for id in entity_ids {
        let entity = graph.get_entity(id).expect("Failed to get entity");
        if entity.name == name {
            return id;
        }
    }
    panic!("Entity with name '{}' not found", name);
}

// ============================================
// Test Group A — Correctness
// ============================================

#[test]
fn test_fastpath_vs_sqlpath_equality_for_simple_patterns() {
    let graph = create_test_graph();

    // Test simple edge type only pattern (Case 1 - Fast Path)
    let pattern = PatternTriple::new("CALLS");

    let sql_results = graph.match_triples(&pattern).expect("SQL path failed");
    let fast_results = graph
        .match_triples_fast(&pattern)
        .expect("Fast path failed");

    // Results must be identical
    assert_eq!(sql_results.len(), fast_results.len());
    assert_eq!(sql_results, fast_results);

    // Verify deterministic ordering
    for (i, (sql_match, fast_match)) in sql_results.iter().zip(fast_results.iter()).enumerate() {
        assert_eq!(sql_match, fast_match, "Mismatch at index {}", i);
    }
}

#[test]
fn test_identical_ordering_guarantees() {
    let graph = create_test_graph();

    // Create a pattern that matches multiple edges
    let pattern = PatternTriple::new("CALLS");

    let mut results1 = graph
        .match_triples_fast(&pattern)
        .expect("Fast path failed");
    let mut results2 = graph
        .match_triples_fast(&pattern)
        .expect("Fast path failed");

    // Sort both to ensure they're identical regardless of internal ordering
    results1.sort_by(|a, b| {
        a.start_id
            .cmp(&b.start_id)
            .then_with(|| a.edge_id.cmp(&b.edge_id))
            .then_with(|| a.end_id.cmp(&b.end_id))
    });
    results2.sort_by(|a, b| {
        a.start_id
            .cmp(&b.start_id)
            .then_with(|| a.edge_id.cmp(&b.edge_id))
            .then_with(|| a.end_id.cmp(&b.end_id))
    });

    assert_eq!(
        results1, results2,
        "Results must be identical across multiple calls"
    );
}

#[test]
fn test_fastpath_must_not_skip_sql_validation() {
    let graph = create_test_graph();

    // Test pattern with property filters (Case 2 - Partial Fast Path)
    let pattern = PatternTriple::new("CALLS").start_property("language", "rust");

    let sql_results = graph.match_triples(&pattern).expect("SQL path failed");
    let fast_results = graph
        .match_triples_fast(&pattern)
        .expect("Fast path failed");

    // Results must be identical - fast path must validate via SQL
    assert_eq!(sql_results.len(), fast_results.len());
    assert_eq!(sql_results, fast_results);
}

#[test]
fn test_fastpath_must_work_with_mvcc_snapshots() {
    let graph = create_test_graph();

    // Warm up the cache by doing a fast-path query first
    let pattern = PatternTriple::new("CALLS");
    let _warmup = graph.match_triples_fast(&pattern).expect("Warmup failed");

    // Acquire a snapshot
    let snapshot = graph
        .acquire_snapshot()
        .expect("Failed to acquire snapshot");

    // Test pattern matching on snapshot
    let sql_results = graph.match_triples(&pattern).expect("SQL path failed");
    let fast_results = graph
        .match_triples_fast(&pattern)
        .expect("Fast path failed");

    // Results must be identical even with snapshots
    assert_eq!(sql_results.len(), fast_results.len());
    assert_eq!(sql_results, fast_results);

    // Verify snapshot contains expected data
    assert!(snapshot.node_count() > 0);
    assert!(snapshot.edge_count() > 0);
}

// ============================================
// Test Group B — Cache correctness
// ============================================

#[test]
fn test_cache_invalidation_during_writes() {
    let graph = create_test_graph();

    // Warm up cache with initial query
    let pattern = PatternTriple::new("CALLS");
    let initial_results = graph
        .match_triples_fast(&pattern)
        .expect("Fast path failed");

    // Add a new edge (should invalidate cache)
    let f5 = insert_entity(&graph, "Function", "new_function");
    let f6 = insert_entity(&graph, "Function", "another_function");
    insert_edge(&graph, f5, f6, "CALLS");

    // Query again - should see new edge
    let after_write_results = graph
        .match_triples_fast(&pattern)
        .expect("Fast path failed");

    assert_eq!(after_write_results.len(), initial_results.len() + 1);

    // Verify against SQL path
    let sql_results = graph.match_triples(&pattern).expect("SQL path failed");
    assert_eq!(after_write_results, sql_results);
}

#[test]
fn test_stale_cache_must_not_affect_results() {
    let graph = create_test_graph();

    // Perform a fast-path query to populate cache
    let pattern = PatternTriple::new("CALLS");
    let first_results = graph
        .match_triples_fast(&pattern)
        .expect("Fast path failed");

    // Get cache stats after first run
    let after_first_outgoing = graph.outgoing_cache_ref().stats();
    let after_first_incoming = graph.incoming_cache_ref().stats();

    // Perform the same query again - should use cache
    let second_results = graph
        .match_triples_fast(&pattern)
        .expect("Fast path failed");

    // Results should be identical
    assert_eq!(first_results, second_results);

    // Cache should have been used (more hits on second run)
    let final_outgoing_stats = graph.outgoing_cache_ref().stats();
    let final_incoming_stats = graph.incoming_cache_ref().stats();

    // At least one cache hit should have occurred on second run
    assert!(
        final_outgoing_stats.hits > after_first_outgoing.hits
            || final_incoming_stats.hits > after_first_incoming.hits,
        "Cache hits should increase: outgoing {}->{}, incoming {}->{}",
        after_first_outgoing.hits,
        final_outgoing_stats.hits,
        after_first_incoming.hits,
        final_incoming_stats.hits
    );
}

#[test]
fn test_snapshot_cache_isolation_preserved() {
    let graph = create_test_graph();

    // Warm up cache first
    let pattern = PatternTriple::new("CALLS");
    let _warmup = graph.match_triples_fast(&pattern).expect("Warmup failed");

    // Get original CALLS edge count
    let original_calls = graph
        .match_triples_fast(&pattern)
        .expect("Original query failed");
    let original_call_count = original_calls.len();

    // Acquire snapshot
    let snapshot = graph
        .acquire_snapshot()
        .expect("Failed to acquire snapshot");

    // Add new edge to main graph
    let f5 = insert_entity(&graph, "Function", "new_function");
    let f6 = insert_entity(&graph, "Function", "another_function");
    insert_edge(&graph, f5, f6, "CALLS");

    // Query on main graph should see new edge
    let current_results = graph
        .match_triples_fast(&pattern)
        .expect("Fast path failed");

    // Current results should have one more CALLS edge
    assert_eq!(current_results.len(), original_call_count + 1);

    // Snapshot should still have original total edge count (isolated from new writes)
    assert_eq!(snapshot.edge_count(), 8); // Original total edges in test graph
}

// ============================================
// Test Group C — Deterministic ordering
// ============================================

#[test]
fn test_sort_order_must_match_sql_exact_semantics() {
    let graph = create_test_graph();

    // Create pattern that matches multiple edges
    let pattern = PatternTriple::new("CALLS");

    let sql_results = graph.match_triples(&pattern).expect("SQL path failed");
    let fast_results = graph
        .match_triples_fast(&pattern)
        .expect("Fast path failed");

    // Both must be sorted by (start_id ASC, edge_id ASC, end_id ASC)
    for i in 1..sql_results.len() {
        assert!(
            sql_results[i - 1].start_id < sql_results[i].start_id
                || (sql_results[i - 1].start_id == sql_results[i].start_id
                    && sql_results[i - 1].edge_id < sql_results[i].edge_id)
                || (sql_results[i - 1].start_id == sql_results[i].start_id
                    && sql_results[i - 1].edge_id == sql_results[i].edge_id
                    && sql_results[i - 1].end_id <= sql_results[i].end_id),
            "SQL results not in deterministic order at index {}: {:?} vs {:?}",
            i,
            sql_results[i - 1],
            sql_results[i]
        );
    }

    for i in 1..fast_results.len() {
        assert!(
            fast_results[i - 1].start_id < fast_results[i].start_id
                || (fast_results[i - 1].start_id == fast_results[i].start_id
                    && fast_results[i - 1].edge_id < fast_results[i].edge_id)
                || (fast_results[i - 1].start_id == fast_results[i].start_id
                    && fast_results[i - 1].edge_id == fast_results[i].edge_id
                    && fast_results[i - 1].end_id <= fast_results[i].end_id),
            "Fast results not in deterministic order at index {}: {:?} vs {:?}",
            i,
            fast_results[i - 1],
            fast_results[i]
        );
    }

    // Ordering must be identical
    assert_eq!(sql_results, fast_results);
}

#[test]
fn test_repeatability_test_3_consecutive_runs() {
    let graph = create_test_graph();

    let pattern = PatternTriple::new("CALLS");

    let results1 = graph
        .match_triples_fast(&pattern)
        .expect("Fast path failed");
    let results2 = graph
        .match_triples_fast(&pattern)
        .expect("Fast path failed");
    let results3 = graph
        .match_triples_fast(&pattern)
        .expect("Fast path failed");

    // All three runs must produce identical results
    assert_eq!(results1, results2);
    assert_eq!(results2, results3);
    assert_eq!(results1, results3);

    // All must be deterministically ordered
    for results in [&results1, &results2, &results3] {
        for i in 1..results.len() {
            assert!(
                results[i - 1].start_id < results[i].start_id
                    || (results[i - 1].start_id == results[i].start_id
                        && results[i - 1].edge_id < results[i].edge_id)
                    || (results[i - 1].start_id == results[i].start_id
                        && results[i - 1].edge_id == results[i].edge_id
                        && results[i - 1].end_id <= results[i].end_id),
                "Results not in deterministic order at index {}: {:?} vs {:?}",
                i,
                results[i - 1],
                results[i]
            );
        }
    }
}

// ============================================
// Test Group D — Mixed patterns
// ============================================

#[test]
fn test_patterns_requiring_fallback() {
    let graph = create_test_graph();

    // Test complex pattern that should fallback to SQL (Case 3)
    let pattern = PatternTriple::new("CALLS")
        .start_label("public")
        .end_label("private")
        .start_property("async", "true")
        .end_property("async", "false");

    let sql_results = graph.match_triples(&pattern).expect("SQL path failed");
    let fast_results = graph
        .match_triples_fast(&pattern)
        .expect("Fast path failed");

    // Results must be identical
    assert_eq!(sql_results, fast_results);
}

#[test]
fn test_patterns_with_label_filters() {
    let graph = create_test_graph();

    // Test pattern with label filters (Case 2)
    let pattern = PatternTriple::new("CALLS").start_label("public");

    let sql_results = graph.match_triples(&pattern).expect("SQL path failed");
    let fast_results = graph
        .match_triples_fast(&pattern)
        .expect("Fast path failed");

    // Results must be identical
    assert_eq!(sql_results, fast_results);
}

#[test]
fn test_patterns_with_property_filters() {
    let graph = create_test_graph();

    // Test pattern with property filters (Case 2)
    let pattern = PatternTriple::new("CALLS").start_property("language", "rust");

    let sql_results = graph.match_triples(&pattern).expect("SQL path failed");
    let fast_results = graph
        .match_triples_fast(&pattern)
        .expect("Fast path failed");

    // Results must be identical
    assert_eq!(sql_results, fast_results);
}

#[test]
fn test_patterns_with_different_directions() {
    let graph = create_test_graph();

    let f1 = get_entity_by_name(&graph, "process_data");
    let f2 = get_entity_by_name(&graph, "validate_input");

    // Test outgoing direction
    let pattern_outgoing = PatternTriple::new("CALLS").direction(BackendDirection::Outgoing);

    let sql_outgoing = graph
        .match_triples(&pattern_outgoing)
        .expect("SQL path failed");
    let fast_outgoing = graph
        .match_triples_fast(&pattern_outgoing)
        .expect("Fast path failed");

    assert_eq!(sql_outgoing, fast_outgoing);

    // Test incoming direction
    let pattern_incoming = PatternTriple::new("CALLS").direction(BackendDirection::Incoming);

    let sql_incoming = graph
        .match_triples(&pattern_incoming)
        .expect("SQL path failed");
    let fast_incoming = graph
        .match_triples_fast(&pattern_incoming)
        .expect("Fast path failed");

    assert_eq!(sql_incoming, fast_incoming);

    // Verify direction semantics
    assert_eq!(fast_outgoing[0].start_id, f1);
    assert_eq!(fast_outgoing[0].end_id, f2);
    assert_eq!(fast_incoming[0].start_id, f2); // Reversed
    assert_eq!(fast_incoming[0].end_id, f1); // Reversed
}

// ============================================
// Test Group E — Performance & Safety
// ============================================

#[test]
fn test_fastpath_must_use_cache_for_90_percent_hits() {
    let graph = create_test_graph();

    // Perform multiple queries to warm up cache
    let pattern = PatternTriple::new("CALLS");

    for _ in 0..10 {
        let _results = graph
            .match_triples_fast(&pattern)
            .expect("Fast path failed");
    }

    // Check cache stats - should have high hit rate
    let outgoing_stats = graph.outgoing_cache_ref().stats();
    let incoming_stats = graph.incoming_cache_ref().stats();

    let total_hits = outgoing_stats.hits + incoming_stats.hits;
    let total_requests = total_hits + outgoing_stats.misses + incoming_stats.misses;

    if total_requests > 0 {
        let hit_rate = (total_hits as f64) / (total_requests as f64);
        assert!(
            hit_rate >= 0.9,
            "Cache hit rate {:.2}% is below 90%",
            hit_rate * 100.0
        );
    }
}

#[test]
fn test_no_panics_unwraps_or_non_determinism() {
    let graph = create_test_graph();

    // Test various patterns - none should panic
    let patterns = vec![
        PatternTriple::new("CALLS"),
        PatternTriple::new("USES"),
        PatternTriple::new("BELONGS_TO"),
        PatternTriple::new("CALLS").start_label("public"),
        PatternTriple::new("CALLS").start_property("language", "rust"),
        PatternTriple::new("NONEXISTENT"),
    ];

    for pattern in patterns {
        // Should not panic or unwrap
        let result = graph.match_triples_fast(&pattern);
        assert!(result.is_ok(), "Pattern failed: {:?}", pattern);

        // Results should be deterministic
        let results1 = result.unwrap();
        let results2 = graph
            .match_triples_fast(&pattern)
            .expect("Second call failed");
        assert_eq!(
            results1, results2,
            "Non-deterministic results for: {:?}",
            pattern
        );
    }
}

#[test]
fn test_no_behavior_change_in_existing_queries() {
    let graph = create_test_graph();

    // Test that existing match_triples behavior is unchanged
    let pattern = PatternTriple::new("CALLS");

    let original_results = graph
        .match_triples(&pattern)
        .expect("Original query failed");

    // Multiple calls should produce same results
    let repeat_results = graph.match_triples(&pattern).expect("Repeat query failed");
    assert_eq!(original_results, repeat_results);

    // Results should be deterministic
    for i in 1..original_results.len() {
        assert!(
            original_results[i - 1].start_id < original_results[i].start_id
                || (original_results[i - 1].start_id == original_results[i].start_id
                    && original_results[i - 1].edge_id < original_results[i].edge_id)
                || (original_results[i - 1].start_id == original_results[i].start_id
                    && original_results[i - 1].edge_id == original_results[i].edge_id
                    && original_results[i - 1].end_id <= original_results[i].end_id),
            "Original results not in deterministic order at index {}: {:?} vs {:?}",
            i,
            original_results[i - 1],
            original_results[i]
        );
    }
}