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
//! Tests for the lightweight pattern engine.
//!
//! This test suite validates the deterministic triple pattern matching functionality
//! using TDD approach to ensure correctness and performance.

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

/// Create a test graph with sample data for pattern matching tests
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");

    // Insert modules
    let m1 = insert_entity(&graph, "Module", "core");
    let m2 = insert_entity(&graph, "Module", "utils");

    // 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, m1, "BELONGS_TO");
    insert_edge(&graph, f2, m1, "BELONGS_TO");
    insert_edge(&graph, f3, m2, "BELONGS_TO");
    insert_edge(&graph, f4, m2, "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 {
    // Use query API to find entity by name
    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]
fn test_pattern_triple_basic_functionality() {
    let graph = create_test_graph();

    // Test basic edge type matching
    let pattern = PatternTriple::new("CALLS");
    let matches = match_triples(&graph, &pattern).expect("Failed to match triples");

    // Should find all CALLS edges
    assert_eq!(matches.len(), 3); // f1->f2, f1->f3, f1->f4

    // Verify deterministic ordering
    let f1 = get_entity_by_name(&graph, "process_data");
    let f2 = get_entity_by_name(&graph, "validate_input");
    let f3 = get_entity_by_name(&graph, "handle_error");
    let f4 = get_entity_by_name(&graph, "log_result");

    // Should be ordered by start_id, then edge_id, then end_id
    assert_eq!(matches[0].start_id, f1);
    assert_eq!(matches[0].end_id, f2);
    assert_eq!(matches[1].start_id, f1);
    assert_eq!(matches[1].end_id, f3);
    assert_eq!(matches[2].start_id, f1);
    assert_eq!(matches[2].end_id, f4);
}

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

    // Test with start and end label filters
    let pattern = PatternTriple::new("USES")
        .start_label("private")
        .end_label("internal");

    let matches = match_triples(&graph, &pattern).expect("Failed to match triples");

    // Should find only f3->s2 (private function uses internal struct)
    assert_eq!(matches.len(), 1);

    let f3 = get_entity_by_name(&graph, "handle_error");
    let s2 = get_entity_by_name(&graph, "ErrorHandler");

    assert_eq!(matches[0].start_id, f3);
    assert_eq!(matches[0].end_id, s2);
}

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

    // Test with property filters
    let pattern = PatternTriple::new("CALLS")
        .start_property("async", "true")
        .end_property("async", "false");

    let matches = match_triples(&graph, &pattern).expect("Failed to match triples");

    // Should find async functions calling non-async functions
    // f1 (async=true) -> f2 (async=false) and f1 (async=true) -> f3 (async=false)
    assert_eq!(matches.len(), 2);

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

    // Both matches should start from f1
    assert_eq!(matches[0].start_id, f1);
    assert_eq!(matches[1].start_id, f1);

    // Ends should be f2 and f3 in deterministic order
    let mut end_ids = vec![matches[0].end_id, matches[1].end_id];
    end_ids.sort();
    assert_eq!(end_ids, vec![f2, f3]);
}

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

    // Test with combined label and property filters
    let pattern = PatternTriple::new("USES")
        .start_label("public")
        .start_property("async", "true")
        .end_label("exported")
        .end_property("thread_safe", "true");

    let matches = match_triples(&graph, &pattern).expect("Failed to match triples");

    // Should find f4 (public, async=true) -> s1 (exported, thread_safe=true)
    assert_eq!(matches.len(), 1);

    let f4 = get_entity_by_name(&graph, "log_result");
    let s1 = get_entity_by_name(&graph, "DataProcessor");

    assert_eq!(matches[0].start_id, f4);
    assert_eq!(matches[0].end_id, s1);
}

#[test]
fn test_pattern_triple_direction() {
    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 matches_outgoing =
        match_triples(&graph, &pattern_outgoing).expect("Failed to match triples");

    // Should find f1->f2 in outgoing direction
    let f1_to_f2_match = matches_outgoing
        .iter()
        .find(|m| m.start_id == f1 && m.end_id == f2);
    assert!(f1_to_f2_match.is_some());

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

    let matches_incoming =
        match_triples(&graph, &pattern_incoming).expect("Failed to match triples");

    // Should find f2->f1 in incoming direction (reversed)
    let f2_to_f1_match = matches_incoming
        .iter()
        .find(|m| m.start_id == f2 && m.end_id == f1);
    assert!(f2_to_f1_match.is_some());
}

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

    // Test with non-existent edge type
    let pattern = PatternTriple::new("NONEXISTENT");
    let matches = match_triples(&graph, &pattern).expect("Failed to match triples");
    assert_eq!(matches.len(), 0);

    // Test with non-existent label
    let pattern = PatternTriple::new("CALLS").start_label("NONEXISTENT_LABEL");
    let matches = match_triples(&graph, &pattern).expect("Failed to match triples");
    assert_eq!(matches.len(), 0);

    // Test with non-existent property
    let pattern = PatternTriple::new("CALLS").start_property("nonexistent", "value");
    let matches = match_triples(&graph, &pattern).expect("Failed to match triples");
    assert_eq!(matches.len(), 0);
}

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

    // Test empty edge type validation
    let pattern = PatternTriple::new("");
    let result = match_triples(&graph, &pattern);
    assert!(result.is_err());

    // Test whitespace-only edge type validation
    let pattern = PatternTriple::new("   ");
    let result = match_triples(&graph, &pattern);
    assert!(result.is_err());
}

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

    // Create a pattern that matches multiple edges
    let pattern = PatternTriple::new("BELONGS_TO");
    let matches = match_triples(&graph, &pattern).expect("Failed to match triples");

    assert_eq!(matches.len(), 4);

    // Verify deterministic ordering: sorted by start_id, then edge_id, then end_id
    for i in 1..matches.len() {
        assert!(
            matches[i - 1].start_id < matches[i].start_id
                || (matches[i - 1].start_id == matches[i].start_id
                    && matches[i - 1].edge_id < matches[i].edge_id)
                || (matches[i - 1].start_id == matches[i].start_id
                    && matches[i - 1].edge_id == matches[i].edge_id
                    && matches[i - 1].end_id <= matches[i].end_id),
            "Matches not in deterministic order at index {}: {:?} vs {:?}",
            i,
            matches[i - 1],
            matches[i]
        );
    }
}

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

    // Test the SqliteGraph.match_triples() method
    let pattern = PatternTriple::new("CALLS");
    let matches = graph
        .match_triples(&pattern)
        .expect("Failed to match triples");

    // Should find all CALLS edges
    assert_eq!(matches.len(), 3);

    // Verify the matches are TripleMatch structs
    for triple_match in &matches {
        assert!(triple_match.start_id > 0);
        assert!(triple_match.end_id > 0);
        assert!(triple_match.edge_id > 0);
    }
}

#[test]
fn test_pattern_triple_performance_with_large_dataset() {
    let graph = SqliteGraph::open_in_memory().expect("Failed to create test graph");

    // Create a larger dataset for performance testing
    let mut entity_ids = Vec::new();
    for i in 0..100 {
        let id = insert_entity(&graph, "Node", &format!("node_{}", i));
        entity_ids.push(id);

        // Add some labels and properties
        if i % 2 == 0 {
            add_label_to_entity(&graph, id, "even");
        } else {
            add_label_to_entity(&graph, id, "odd");
        }

        add_property_to_entity(&graph, id, "index", &i.to_string());
        add_property_to_entity(
            &graph,
            id,
            "parity",
            if i % 2 == 0 { "even" } else { "odd" },
        );
    }

    // Create edges between consecutive nodes
    for i in 0..99 {
        insert_edge(&graph, entity_ids[i], entity_ids[i + 1], "NEXT");
    }

    // Test pattern matching performance
    let start = std::time::Instant::now();

    let pattern = PatternTriple::new("NEXT")
        .start_property("parity", "even")
        .end_property("parity", "odd");

    let matches = match_triples(&graph, &pattern).expect("Failed to match triples");

    let duration = start.elapsed();

    // Should find all even->odd transitions
    assert_eq!(matches.len(), 50); // 0->1, 2->3, ..., 96->97, 98->99

    // Performance should be reasonable (less than 1 second for this dataset)
    assert!(
        duration.as_secs() < 1,
        "Pattern matching took too long: {:?}",
        duration
    );

    // Verify deterministic ordering
    for i in 1..matches.len() {
        assert!(
            matches[i - 1].start_id < matches[i].start_id,
            "Matches not ordered by start_id"
        );
    }
}

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

    // Test multiple property filters on the same node
    let pattern = PatternTriple::new("CALLS")
        .start_property("language", "rust")
        .start_property("async", "true")
        .end_property("language", "rust")
        .end_property("async", "false");

    let matches = match_triples(&graph, &pattern).expect("Failed to match triples");

    // Should find f1 (rust, async=true) -> f2 and f3 (rust, async=false)
    assert_eq!(matches.len(), 2);

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

    // Both matches should start from f1
    assert_eq!(matches[0].start_id, f1);
    assert_eq!(matches[1].start_id, f1);

    // Ends should be f2 and f3
    let mut end_ids = vec![matches[0].end_id, matches[1].end_id];
    end_ids.sort();
    assert_eq!(end_ids, vec![f2, f3]);
}