sqlitegraph 3.4.0

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
//! Integration tests for CSR sharding layer.
//!
//! Validates CSR consistency across CRUD operations, property store sync,
//! and pub/sub notifications.

use std::sync::{Arc, Mutex};
use std::time::Duration;

use crate::sharding::pubsub::{Change, ChangeType};
use crate::sharding::shard::{CsrEdge, CsrShard};
use crate::sharding::*;

/// Test CSR consistency after insertions.
#[test]
fn test_csr_consistency_after_insert() {
    let mut shard = CsrShard::new(0, 1000, 2000);

    // Insert edges
    shard.add_edge(CsrEdge {
        src: 1050,
        dst: 1500,
        weight: 0.8,
        flags: 0,
    });
    shard.add_edge(CsrEdge {
        src: 1050,
        dst: 1600,
        weight: 0.5,
        flags: 0,
    });

    // Verify edges in shard
    assert_eq!(shard.edge_count(), 2);

    // Sort for deterministic comparison
    shard.sort_edges();
    assert_eq!(shard.edges[0].dst, 1500);
    assert_eq!(shard.edges[1].dst, 1600);
}

/// Test property store sync with CSR.
#[test]
fn test_property_store_sync_with_csr() {
    let mut store = PropertyStore::in_memory().unwrap();
    let mut shard = CsrShard::new(0, 1000, 2000);

    // Set token properties
    store.set_token_text(1050, "test_token").unwrap();
    store.set_embedding(1050, &[0.1, 0.2, 0.3, 0.4]).unwrap();
    store.set_metadata(1050, r#"{"type": "test"}"#).unwrap();

    // Add CSR edge referencing token
    shard.add_edge(CsrEdge {
        src: 1050,
        dst: 1500,
        weight: 0.8,
        flags: 0,
    });

    // Verify property store consistency
    let text = store.get_token_text(1050).unwrap();
    assert_eq!(text, Some("test_token".to_string()));

    let embedding = store.get_embedding(1050).unwrap();
    assert!(embedding.is_some());
    assert_eq!(embedding.unwrap().len(), 4);

    let metadata = store.get_metadata(1050).unwrap();
    assert_eq!(metadata, Some(r#"{"type": "test"}"#.to_string()));
}

/// Test pub/sub notifications on CSR changes.
#[test]
fn test_pubsub_notifications_on_csr_changes() {
    let pubsub = PubSub::new();
    let notified = Arc::new(Mutex::new(Vec::new()));
    let notified_clone = notified.clone();

    pubsub.subscribe(
        vec!["graph.edge".to_string()],
        Box::new(move |change| {
            notified_clone.lock().unwrap().push(change.clone());
        }),
    );

    // Simulate edge insertion
    let change = Change::edge_inserted(1050, 1500, "graph.edge".to_string());
    pubsub.publish(change);

    std::thread::sleep(Duration::from_millis(10));

    let notifications = notified.lock().unwrap();
    assert_eq!(notifications.len(), 1);
    assert_eq!(notifications[0].change_type, ChangeType::EdgeInserted);
    assert_eq!(notifications[0].src_id, Some(1050));
    assert_eq!(notifications[0].dst_id, Some(1500));
}

/// Test semantic layer KNN fallback.
#[test]
fn test_semantic_layer_knn_fallback() {
    let mut layer = SemanticLayer::new(4);

    // Insert embeddings
    layer
        .insert_embedding(1050, vec![1.0, 0.0, 0.0, 0.0])
        .unwrap();
    layer
        .insert_embedding(1500, vec![0.9, 0.1, 0.0, 0.0])
        .unwrap();
    layer
        .insert_embedding(1600, vec![0.0, 1.0, 0.0, 0.0])
        .unwrap();

    // Query KNN
    let query = vec![1.0, 0.0, 0.0, 0.0];
    let results = layer.knn_search(&query, 2);

    assert_eq!(results.len(), 2);
    assert_eq!(results[0].node_id, 1050); // Closest match
    assert!(results[0].distance < results[1].distance);
}

/// Test property store persistence across operations.
#[test]
fn test_property_store_persistence() {
    let mut store = PropertyStore::in_memory().unwrap();

    // Insert token
    store.set_token_text(100, "test").unwrap();
    assert!(store.get_token_text(100).unwrap().is_some());

    // Update token
    store.set_token_text(100, "updated").unwrap();
    assert_eq!(
        store.get_token_text(100).unwrap(),
        Some("updated".to_string())
    );

    // Delete token
    store.delete_token(100).unwrap();
    assert!(store.get_token_text(100).unwrap().is_none());

    // Verify count
    assert_eq!(store.token_count().unwrap(), 0);
}

/// Test WAL replay after restart.
#[test]
fn test_wal_replay_after_restart() {
    let pubsub = PubSub::new();

    // Publish changes before restart
    pubsub.publish(Change::node_inserted(100, "graph.node".to_string()));
    pubsub.publish(Change::edge_inserted(100, 200, "graph.edge".to_string()));

    assert_eq!(pubsub.change_log_size(), 2);

    // Simulate restart by creating new subscriber and replaying
    let count = Arc::new(Mutex::new(0));
    let count_clone = count.clone();

    pubsub.subscribe(
        vec!["graph.node".to_string()],
        Box::new(move |_| {
            *count_clone.lock().unwrap() += 1;
        }),
    );

    let notified = pubsub.replay_wal().unwrap();
    assert_eq!(notified, 1); // One subscriber notified of one matching change
    assert_eq!(*count.lock().unwrap(), 1);
}

/// Test semantic layer incremental insert.
#[test]
fn test_semantic_layer_incremental_insert() {
    let mut layer = SemanticLayer::new(3);

    // Initial insert
    layer.insert_embedding(100, vec![1.0, 0.0, 0.0]).unwrap();
    assert_eq!(layer.embedding_count(), 1);

    // Update existing embedding (HNSW creates new vector_id for fine-tune)
    layer.insert_embedding(100, vec![0.9, 0.1, 0.0]).unwrap();
    // HNSW creates new vector_id, so count increases
    assert_eq!(layer.embedding_count(), 2);

    // Remove is no-op for HNSW (no deletion support)
    layer.remove_embedding(100);
    // Token still exists since HNSW can't delete efficiently
    assert!(layer.has_embedding(100));

    // Re-insert adds another vector
    layer.insert_embedding(100, vec![0.8, 0.2, 0.0]).unwrap();
    assert!(layer.has_embedding(100));
    assert_eq!(layer.embedding_count(), 3); // Count increased again
}

/// Test pub/sub topic filtering with wildcards.
#[test]
fn test_pubsub_topic_wildcards() {
    let pubsub = PubSub::new();

    let node_count = Arc::new(Mutex::new(0));
    let edge_count = Arc::new(Mutex::new(0));
    let all_count = Arc::new(Mutex::new(0));

    let node_count_clone = node_count.clone();
    let edge_count_clone = edge_count.clone();
    let all_count_clone = all_count.clone();

    // Subscribe to specific topics
    pubsub.subscribe(
        vec!["graph.node".to_string()],
        Box::new(move |_| {
            *node_count_clone.lock().unwrap() += 1;
        }),
    );

    pubsub.subscribe(
        vec!["graph.edge".to_string()],
        Box::new(move |_| {
            *edge_count_clone.lock().unwrap() += 1;
        }),
    );

    // Subscribe to wildcard
    pubsub.subscribe(
        vec!["*".to_string()],
        Box::new(move |_| {
            *all_count_clone.lock().unwrap() += 1;
        }),
    );

    // Publish changes
    pubsub.publish(Change::node_inserted(100, "graph.node".to_string()));
    pubsub.publish(Change::edge_inserted(100, 200, "graph.edge".to_string()));

    std::thread::sleep(Duration::from_millis(10));

    // Verify filtering
    assert_eq!(*node_count.lock().unwrap(), 1); // Only node change
    assert_eq!(*edge_count.lock().unwrap(), 1); // Only edge change
    assert_eq!(*all_count.lock().unwrap(), 2); // Both changes
}

/// Test property store all properties query.
#[test]
fn test_property_store_all_properties() {
    let mut store = PropertyStore::in_memory().unwrap();

    // Insert token with all properties
    store.set_token_text(100, "test").unwrap();
    store.set_embedding(100, &[0.1, 0.2, 0.3]).unwrap();
    store.set_metadata(100, r#"{"key": "value"}"#).unwrap();

    // Query all properties
    let result = store.get_all_properties(100).unwrap();
    assert!(result.is_some());

    let (text, embedding, metadata) = result.unwrap();
    assert_eq!(text, "test");
    assert_eq!(embedding.len(), 3);
    assert_eq!(metadata, r#"{"key": "value"}"#);
}

/// Test manifest with CSR shards.
#[test]
fn test_manifest_with_csr_shards() {
    let mut shard1 = CsrShard::new(0, 0, 1000);
    let mut shard2 = CsrShard::new(1, 1000, 2000);

    shard1.add_edge(CsrEdge {
        src: 100,
        dst: 200,
        weight: 0.5,
        flags: 0,
    });
    shard2.add_edge(CsrEdge {
        src: 1500,
        dst: 1700,
        weight: 0.7,
        flags: 0,
    });

    let metadata1 = ShardMetadata {
        shard_id: 0,
        source_start: 0,
        source_end: 1000,
        edge_count: 1,
        file_name: "shard_0000.csr".to_string(),
    };

    let metadata2 = ShardMetadata {
        shard_id: 1,
        source_start: 1000,
        source_end: 2000,
        edge_count: 1,
        file_name: "shard_0001.csr".to_string(),
    };

    let manifest = Manifest::new(vec![metadata1, metadata2], "1.0".to_string());

    assert_eq!(manifest.shards.len(), 2);
    assert_eq!(manifest.shards[0].edge_count, 1);
    assert_eq!(manifest.shards[1].source_start, 1000);
    assert_eq!(manifest.shards[1].source_end, 2000);
}

/// Test CSR edge storage and retrieval.
#[test]
fn test_csr_edge_storage() {
    let mut shard = CsrShard::new(0, 1000, 2000);

    // Create edges
    shard.add_edge(CsrEdge {
        src: 1050,
        dst: 1500,
        weight: 0.8,
        flags: 0,
    });
    shard.add_edge(CsrEdge {
        src: 1100,
        dst: 1500,
        weight: 0.6,
        flags: 0,
    });
    shard.add_edge(CsrEdge {
        src: 1200,
        dst: 1500,
        weight: 0.4,
        flags: 0,
    });

    shard.sort_edges();

    // Verify edge count
    assert_eq!(shard.edge_count(), 3);

    // Verify edges are sorted by source
    let mut prev_src = 0;
    for edge in &shard.edges {
        assert!(edge.src >= prev_src);
        prev_src = edge.src;
    }
}

/// Test semantic layer cosine distance - identical vectors.
#[test]
fn test_cosine_distance_identical() {
    let mut layer = SemanticLayer::new(3);

    layer.insert_embedding(100, vec![1.0, 2.0, 3.0]).unwrap();
    layer.insert_embedding(200, vec![1.0, 2.0, 3.0]).unwrap();

    let query = vec![1.0, 2.0, 3.0];
    let results = layer.knn_search(&query, 1);
    assert_eq!(results.len(), 1);
    assert!((results[0].distance - 0.0).abs() < 1e-6); // Identical = distance 0
}

/// Test semantic layer cosine distance - orthogonal vectors.
#[test]
fn test_cosine_distance_orthogonal() {
    let mut layer = SemanticLayer::new(3);

    // Only insert orthogonal vectors
    layer.insert_embedding(300, vec![1.0, 0.0, 0.0]).unwrap();
    layer.insert_embedding(400, vec![0.0, 1.0, 0.0]).unwrap();

    let query_orthogonal = vec![1.0, 0.0, 0.0];
    let results_orthogonal = layer.knn_search(&query_orthogonal, 2);
    assert_eq!(results_orthogonal.len(), 2);
    assert_eq!(results_orthogonal[0].node_id, 300); // Closest (distance 0)
    // Orthogonal vector should have distance around 1.0 (HNSW approximation varies)
    eprintln!(
        "Distance to orthogonal vector: {}",
        results_orthogonal[1].distance
    );
    assert!(results_orthogonal[1].distance > 0.1); // Should be some distance
    assert!(results_orthogonal[0].distance < results_orthogonal[1].distance); // Results sorted by distance
}

/// Test property store token count operations.
#[test]
fn test_property_store_token_count() {
    let mut store = PropertyStore::in_memory().unwrap();

    assert_eq!(store.token_count().unwrap(), 0);

    store.set_token_text(100, "one").unwrap();
    store.set_token_text(200, "two").unwrap();
    store.set_token_text(300, "three").unwrap();

    assert_eq!(store.token_count().unwrap(), 3);

    store.delete_token(200).unwrap();
    assert_eq!(store.token_count().unwrap(), 2);
}

/// Test pub/sub change log persistence.
#[test]
fn test_pubsub_change_log_persistence() {
    let pubsub = PubSub::new();

    assert_eq!(pubsub.change_log_size(), 0);

    pubsub.publish(Change::node_inserted(100, "graph.node".to_string()));
    assert_eq!(pubsub.change_log_size(), 1);

    pubsub.publish(Change::edge_inserted(100, 200, "graph.edge".to_string()));
    assert_eq!(pubsub.change_log_size(), 2);

    pubsub.clear_change_log();
    assert_eq!(pubsub.change_log_size(), 0);
}

/// Test pubsub subscriber lifecycle.
#[test]
fn test_pubsub_subscriber_lifecycle() {
    let pubsub = PubSub::new();

    assert_eq!(pubsub.subscriber_count(), 0);

    let id1 = pubsub.subscribe(vec!["graph.node".to_string()], Box::new(|_| {}));
    assert_eq!(pubsub.subscriber_count(), 1);

    let id2 = pubsub.subscribe(vec!["graph.edge".to_string()], Box::new(|_| {}));
    assert_eq!(pubsub.subscriber_count(), 2);

    pubsub.unsubscribe(id1);
    assert_eq!(pubsub.subscriber_count(), 1);

    pubsub.unsubscribe(id2);
    assert_eq!(pubsub.subscriber_count(), 0);
}