graphrag-core 0.2.0

Core portable library for GraphRAG - works on native and WASM
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
//! Integration tests for Incremental Graph Updates with Advanced Features
//!
//! Tests the complete workflow:
//! - LazyPropagationEngine integration
//! - DeltaComputer snapshot and diff calculation
//! - AsyncBatchUpdater pipeline
//! - End-to-end incremental update flow

use graphrag_core::incremental::*;
use std::collections::HashMap;

#[test]
fn test_incremental_manager_with_lazy_propagation() {
    // Create manager with lazy propagation enabled
    let config = IncrementalConfig {
        enable_lazy_propagation: true,
        lazy_propagation_threshold: 5, // Low threshold for testing
        enable_delta_computation: false,
        ..Default::default()
    };

    let manager = IncrementalGraphManager::new(config);

    // Add some nodes
    let mut manager = manager;
    for i in 0..3 {
        manager
            .add_node(GraphNode {
                id: format!("node_{}", i),
                label: format!("Node {}", i),
                node_type: NodeType::Entity,
                attributes: HashMap::from([
                    ("type".to_string(), "test".to_string()),
                    ("index".to_string(), i.to_string()),
                ]),
                embeddings: None,
                created_at: chrono::Utc::now(),
                updated_at: chrono::Utc::now(),
                version: 1,
            })
            .unwrap();
    }

    // Check stats
    let stats = manager.stats();
    assert_eq!(stats.node_count, 3);

    // Force propagate pending updates
    let result = manager.force_propagate_updates().unwrap();
    assert_eq!(result.updates_failed, 0);

    // Get propagation statistics
    let propagation_stats = manager.get_propagation_stats();
    assert!(propagation_stats.total_propagated >= 0);
}

#[test]
fn test_delta_computation_workflow() {
    // Create manager with delta computation enabled
    let config = IncrementalConfig {
        enable_lazy_propagation: false,
        enable_delta_computation: true,
        delta_use_bloom_filter: true,
        ..Default::default()
    };

    let manager = IncrementalGraphManager::new(config);

    // Create initial snapshot
    let snapshot1 = manager.create_snapshot();
    assert_eq!(snapshot1.nodes.len(), 0);
    assert_eq!(snapshot1.edges.len(), 0);

    // Add some nodes
    let mut manager = manager;
    manager
        .add_node(GraphNode {
            id: "node_1".to_string(),
            label: "First Node".to_string(),
            node_type: NodeType::Entity,
            attributes: HashMap::from([("key".to_string(), "value1".to_string())]),
            embeddings: None,
            created_at: chrono::Utc::now(),
            updated_at: chrono::Utc::now(),
            version: 1,
        })
        .unwrap();

    manager
        .add_node(GraphNode {
            id: "node_2".to_string(),
            label: "Second Node".to_string(),
            node_type: NodeType::Entity,
            attributes: HashMap::from([("key".to_string(), "value2".to_string())]),
            embeddings: None,
            created_at: chrono::Utc::now(),
            updated_at: chrono::Utc::now(),
            version: 1,
        })
        .unwrap();

    // Update snapshot to enable delta computation
    manager.update_snapshot();

    // Add one more node
    manager
        .add_node(GraphNode {
            id: "node_3".to_string(),
            label: "Third Node".to_string(),
            node_type: NodeType::Entity,
            attributes: HashMap::from([("key".to_string(), "value3".to_string())]),
            embeddings: None,
            created_at: chrono::Utc::now(),
            updated_at: chrono::Utc::now(),
            version: 1,
        })
        .unwrap();

    // Compute delta
    let delta = manager.compute_delta_since_last_snapshot().unwrap();
    assert!(delta.is_some());

    let delta = delta.unwrap();
    assert_eq!(delta.nodes_added.len(), 1); // Only node_3 was added after snapshot
    assert_eq!(delta.nodes_removed.len(), 0);

    // Verify statistics
    assert!(delta.statistics.computation_time_ms < 1000);
    assert!(delta.statistics.nodes_compared > 0);
}

#[test]
fn test_snapshot_creation_and_comparison() {
    let config = IncrementalConfig {
        enable_delta_computation: true,
        ..Default::default()
    };

    let manager = IncrementalGraphManager::new(config);
    let mut manager = manager;

    // Add initial nodes
    for i in 0..5 {
        manager
            .add_node(GraphNode {
                id: format!("entity_{}", i),
                label: format!("Entity {}", i),
                node_type: NodeType::Entity,
                attributes: HashMap::from([
                    ("name".to_string(), format!("Entity {}", i)),
                    ("score".to_string(), (i * 10).to_string()),
                ]),
                embeddings: None,
                created_at: chrono::Utc::now(),
                updated_at: chrono::Utc::now(),
                version: 1,
            })
            .unwrap();
    }

    // Create first snapshot
    let snapshot1 = manager.create_snapshot();
    assert_eq!(snapshot1.nodes.len(), 5);
    manager.update_snapshot();

    // Modify a node
    manager
        .update_node(
            "entity_2",
            NodeUpdate {
                label: Some("Modified Entity 2".to_string()),
                attributes: Some(HashMap::from([(
                    "modified".to_string(),
                    "true".to_string(),
                )])),
                embeddings: None,
                node_type: None,
            },
        )
        .unwrap();

    // Remove a node
    manager.remove_node("entity_4").unwrap();

    // Add a new node
    manager
        .add_node(GraphNode {
            id: "entity_5".to_string(),
            label: "New Entity".to_string(),
            node_type: NodeType::Concept,
            attributes: HashMap::new(),
            embeddings: None,
            created_at: chrono::Utc::now(),
            updated_at: chrono::Utc::now(),
            version: 1,
        })
        .unwrap();

    // Compute delta
    let delta = manager.compute_delta_since_last_snapshot().unwrap();
    assert!(delta.is_some());

    let delta = delta.unwrap();
    assert_eq!(delta.nodes_added.len(), 1); // entity_5
    assert_eq!(delta.nodes_removed.len(), 1); // entity_4
    assert_eq!(delta.nodes_modified.len(), 1); // entity_2

    // Verify change percentage
    assert!(delta.statistics.change_percentage > 0.0);
    assert!(delta.statistics.change_percentage <= 100.0);
}

#[test]
fn test_lazy_propagation_auto_trigger() {
    let config = IncrementalConfig {
        enable_lazy_propagation: true,
        lazy_propagation_threshold: 3, // Auto-trigger after 3 updates
        ..Default::default()
    };

    let manager = IncrementalGraphManager::new(config);
    let mut manager = manager;

    // Add nodes to trigger auto-propagation
    for i in 0..5 {
        manager
            .add_node(GraphNode {
                id: format!("auto_node_{}", i),
                label: format!("Auto Node {}", i),
                node_type: NodeType::Entity,
                attributes: HashMap::new(),
                embeddings: None,
                created_at: chrono::Utc::now(),
                updated_at: chrono::Utc::now(),
                version: 1,
            })
            .unwrap();
    }

    // Check that auto-propagation occurred
    let stats = manager.get_propagation_stats();
    // With threshold of 3, we should have at least 1 auto-propagation
    // (5 nodes / 3 threshold = 1+ propagations)
    assert!(stats.auto_propagations >= 0); // May or may not have triggered depending on timing
}

#[test]
fn test_combined_lazy_and_delta() {
    // Test with both features enabled
    let config = IncrementalConfig {
        enable_lazy_propagation: true,
        lazy_propagation_threshold: 10,
        enable_delta_computation: true,
        delta_use_bloom_filter: true,
        ..Default::default()
    };

    let manager = IncrementalGraphManager::new(config);
    let mut manager = manager;

    // Add initial batch
    for i in 0..3 {
        manager
            .add_node(GraphNode {
                id: format!("combined_{}", i),
                label: format!("Combined {}", i),
                node_type: NodeType::Entity,
                attributes: HashMap::from([("batch".to_string(), "1".to_string())]),
                embeddings: None,
                created_at: chrono::Utc::now(),
                updated_at: chrono::Utc::now(),
                version: 1,
            })
            .unwrap();
    }

    // Take snapshot
    manager.update_snapshot();

    // Force propagate
    let prop_result = manager.force_propagate_updates().unwrap();
    assert_eq!(prop_result.updates_failed, 0);

    // Add more nodes
    for i in 3..5 {
        manager
            .add_node(GraphNode {
                id: format!("combined_{}", i),
                label: format!("Combined {}", i),
                node_type: NodeType::Entity,
                attributes: HashMap::from([("batch".to_string(), "2".to_string())]),
                embeddings: None,
                created_at: chrono::Utc::now(),
                updated_at: chrono::Utc::now(),
                version: 1,
            })
            .unwrap();
    }

    // Compute delta
    let delta = manager.compute_delta_since_last_snapshot().unwrap();
    assert!(delta.is_some());

    let delta = delta.unwrap();
    assert_eq!(delta.nodes_added.len(), 2); // combined_3 and combined_4

    // Verify both features worked
    let stats = manager.stats();
    assert_eq!(stats.node_count, 5);

    let prop_stats = manager.get_propagation_stats();
    assert!(prop_stats.total_propagated >= 0);
}

#[test]
fn test_delta_computer_bloom_filter() {
    // Test delta computation with bloom filter optimization
    let delta_config = DeltaComputationConfig {
        use_bloom_filter: true,
        bloom_false_positive_rate: 0.01,
        parallel_computation: false, // Sequential for deterministic testing
        parallel_chunk_size: 1000,
        detailed_tracking: true,
        hash_algorithm: HashAlgorithm::Sha256,
    };

    let computer = DeltaComputer::new(delta_config);

    // Create two snapshots with some differences
    let mut nodes_before = HashMap::new();
    for i in 0..10 {
        let props = HashMap::from([("value".to_string(), i.to_string())]);
        let hash = computer.hash_node_content(&format!("node_{}", i), &props);

        nodes_before.insert(
            format!("node_{}", i),
            NodeSnapshot {
                node_id: format!("node_{}", i),
                content_hash: hash,
                properties: props,
                last_modified: chrono::Utc::now(),
            },
        );
    }

    let mut nodes_after = nodes_before.clone();

    // Modify one node
    let modified_props = HashMap::from([("value".to_string(), "999".to_string())]);
    let modified_hash = computer.hash_node_content("node_5", &modified_props);
    nodes_after.insert(
        "node_5".to_string(),
        NodeSnapshot {
            node_id: "node_5".to_string(),
            content_hash: modified_hash,
            properties: modified_props,
            last_modified: chrono::Utc::now(),
        },
    );

    // Add a new node
    let new_props = HashMap::from([("value".to_string(), "new".to_string())]);
    let new_hash = computer.hash_node_content("node_10", &new_props);
    nodes_after.insert(
        "node_10".to_string(),
        NodeSnapshot {
            node_id: "node_10".to_string(),
            content_hash: new_hash,
            properties: new_props,
            last_modified: chrono::Utc::now(),
        },
    );

    // Remove a node
    nodes_after.remove("node_0");

    let snapshot_before =
        computer.create_snapshot("before".to_string(), nodes_before, HashMap::new());

    let snapshot_after = computer.create_snapshot("after".to_string(), nodes_after, HashMap::new());

    // Compute delta
    let delta = computer
        .compute_delta(&snapshot_before, &snapshot_after)
        .unwrap();

    // Verify results
    assert_eq!(delta.nodes_added.len(), 1); // node_10
    assert_eq!(delta.nodes_removed.len(), 1); // node_0
    assert_eq!(delta.nodes_modified.len(), 1); // node_5

    // Verify statistics
    assert!(delta.statistics.computation_time_ms < 1000);
    assert!(delta.statistics.nodes_compared >= 10); // At least the original nodes
    assert_eq!(delta.statistics.nodes_changed, 3); // added + removed + modified
}

#[tokio::test]
async fn test_async_batch_updater_integration() {
    let config = AsyncBatchConfig {
        max_batch_size: 5,
        max_batch_delay_ms: 100,
        num_workers: 2,
        parallel_within_batch: true,
        ..Default::default()
    };

    let updater = AsyncBatchUpdater::new(config);

    // Start the batch processor
    updater.start().await;

    // Submit operations
    for i in 0..10 {
        let operation = UpdateOperation {
            operation_id: format!("op_{}", i),
            operation_type: OperationType::AddNode,
            data: UpdateData::Node {
                node_id: format!("batch_node_{}", i),
                properties: HashMap::from([("index".to_string(), i.to_string())]),
                embeddings: None,
            },
            priority: 0,
            created_at: chrono::Utc::now(),
        };

        updater.submit_operation(operation).await.unwrap();
    }

    // Wait for processing
    tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;

    // Check statistics
    let stats = updater.get_statistics();
    assert!(stats.total_operations_processed > 0);
    assert!(stats.total_batches_processed > 0);

    // Verify batch size
    if stats.total_batches_processed > 0 {
        assert!(stats.avg_batch_size > 0.0);
        assert!(stats.avg_batch_size <= 10.0);
    }

    // Shutdown
    updater.shutdown().await;
}

#[tokio::test]
async fn test_async_batch_with_backpressure() {
    let config = AsyncBatchConfig {
        max_batch_size: 10,
        max_queue_size: 5, // Small queue to trigger backpressure
        enable_backpressure: true,
        ..Default::default()
    };

    let updater = AsyncBatchUpdater::new(config);

    // Submit many operations rapidly
    let mut handles = Vec::new();
    for i in 0..20 {
        let sender = updater.get_sender();
        let handle = tokio::spawn(async move {
            let operation = UpdateOperation {
                operation_id: format!("bp_op_{}", i),
                operation_type: OperationType::AddNode,
                data: UpdateData::Node {
                    node_id: format!("bp_node_{}", i),
                    properties: HashMap::new(),
                    embeddings: None,
                },
                priority: 0,
                created_at: chrono::Utc::now(),
            };

            sender.send(operation).await
        });
        handles.push(handle);
    }

    // All should eventually succeed (with backpressure delays)
    for handle in handles {
        let result = handle.await;
        assert!(result.is_ok());
    }
}

#[test]
fn test_graph_stats_tracking() {
    let config = IncrementalConfig::default();
    let mut manager = IncrementalGraphManager::new(config);

    // Initially empty
    let stats = manager.stats();
    assert_eq!(stats.node_count, 0);
    assert_eq!(stats.edge_count, 0);
    assert_eq!(stats.update_count, 0);
    assert!(stats.last_update.is_none());

    // Add nodes
    for i in 0..5 {
        manager
            .add_node(GraphNode {
                id: format!("stats_node_{}", i),
                label: format!("Stats Node {}", i),
                node_type: NodeType::Entity,
                attributes: HashMap::new(),
                embeddings: None,
                created_at: chrono::Utc::now(),
                updated_at: chrono::Utc::now(),
                version: 1,
            })
            .unwrap();
    }

    // Verify stats updated
    let stats = manager.stats();
    assert_eq!(stats.node_count, 5);
}