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
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
//! Async Batch Update Pipeline for Incremental Graphs
//!
//! This module implements high-throughput asynchronous batch processing for graph updates,
//! achieving 1000+ documents/second ingestion with parallelization strategies.
//!
//! Key features:
//! - Tokio-based async queue for non-blocking ingestion
//! - Rayon parallelization for CPU-bound graph operations
//! - Smart batching with adaptive sizing
//! - Back-pressure handling for stable throughput
//! - Streaming updates with zero-downtime

use chrono::{DateTime, Utc};
use parking_lot::RwLock;
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::sync::Arc;
use tokio::sync::mpsc::{channel, Receiver, Sender};
use tokio::sync::Notify;

use crate::core::Result;

/// Configuration for async batch updates
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AsyncBatchConfig {
    /// Maximum batch size before triggering automatic processing
    pub max_batch_size: usize,

    /// Maximum time to wait before processing incomplete batch (milliseconds)
    pub max_batch_delay_ms: u64,

    /// Channel buffer size for incoming update requests
    pub channel_buffer_size: usize,

    /// Number of concurrent batch processors
    pub num_workers: usize,

    /// Enable parallel processing within batches (rayon)
    pub parallel_within_batch: bool,

    /// Minimum batch size for parallel processing
    pub parallel_threshold: usize,

    /// Enable back-pressure when queue is full
    pub enable_backpressure: bool,

    /// Maximum queue size before applying back-pressure
    pub max_queue_size: usize,
}

impl Default for AsyncBatchConfig {
    fn default() -> Self {
        Self {
            max_batch_size: 100,
            max_batch_delay_ms: 1000,
            channel_buffer_size: 1000,
            num_workers: 4,
            parallel_within_batch: true,
            parallel_threshold: 10,
            enable_backpressure: true,
            max_queue_size: 10000,
        }
    }
}

/// Represents a single update operation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateOperation {
    /// Unique ID for this operation
    pub operation_id: String,

    /// Type of operation
    pub operation_type: OperationType,

    /// Node or edge data to update
    pub data: UpdateData,

    /// Priority for ordering (higher = more urgent)
    pub priority: u8,

    /// Timestamp when operation was created
    pub created_at: DateTime<Utc>,
}

/// Type of graph update operation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum OperationType {
    /// Add a new node to the graph
    AddNode,
    /// Update an existing node's properties
    UpdateNode,
    /// Remove a node from the graph
    RemoveNode,
    /// Add a new edge between nodes
    AddEdge,
    /// Update an existing edge's properties
    UpdateEdge,
    /// Remove an edge from the graph
    RemoveEdge,
}

/// Data payload for update operations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum UpdateData {
    /// Node data
    Node {
        /// Node identifier
        node_id: String,
        /// Node properties
        properties: HashMap<String, String>,
        /// Optional embedding vector
        embeddings: Option<Vec<f32>>,
    },
    /// Edge data
    Edge {
        /// Source node identifier
        source_id: String,
        /// Target node identifier
        target_id: String,
        /// Type of edge/relationship
        edge_type: String,
        /// Edge weight for graph algorithms
        weight: f32,
        /// Edge properties
        properties: HashMap<String, String>,
    },
}

/// Batch of update operations to process together
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateBatch {
    /// Unique batch ID
    pub batch_id: String,

    /// All operations in this batch
    pub operations: Vec<UpdateOperation>,

    /// Batch creation timestamp
    pub created_at: DateTime<Utc>,

    /// Batch processing started timestamp
    pub started_at: Option<DateTime<Utc>>,

    /// Batch completion timestamp
    pub completed_at: Option<DateTime<Utc>>,

    /// Batch status
    pub status: BatchStatus,
}

/// Status of batch processing
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum BatchStatus {
    /// Batch is waiting to be processed
    Pending,
    /// Batch is currently being processed
    Processing,
    /// Batch has been processed successfully
    Completed,
    /// Batch processing failed
    Failed,
}

/// Result of batch processing
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchResult {
    /// Unique identifier for the batch
    pub batch_id: String,
    /// Total number of operations processed
    pub operations_processed: usize,
    /// Number of operations that succeeded
    pub operations_succeeded: usize,
    /// Number of operations that failed
    pub operations_failed: usize,
    /// Time taken to process the batch in milliseconds
    pub processing_time_ms: u64,
    /// Error messages from failed operations
    pub errors: Vec<String>,
}

/// Statistics for batch processing
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchStatistics {
    /// Total number of batches processed since start
    pub total_batches_processed: usize,
    /// Total number of operations processed
    pub total_operations_processed: usize,
    /// Total number of operations that succeeded
    pub total_operations_succeeded: usize,
    /// Total number of operations that failed
    pub total_operations_failed: usize,
    /// Average number of operations per batch
    pub avg_batch_size: f32,
    /// Average time to process a batch in milliseconds
    pub avg_processing_time_ms: f32,
    /// Processing throughput in operations per second
    pub throughput_ops_per_sec: f32,
    /// Current number of operations in the queue
    pub queue_size: usize,
    /// Timestamp of the last processed batch
    pub last_batch_at: Option<DateTime<Utc>>,
}

/// Async batch updater for high-throughput graph updates
pub struct AsyncBatchUpdater {
    config: AsyncBatchConfig,

    /// Channel for sending update operations
    sender: Sender<UpdateOperation>,

    /// Channel for receiving update operations
    receiver: Arc<RwLock<Option<Receiver<UpdateOperation>>>>,

    /// Queue of pending batches
    pending_batches: Arc<RwLock<VecDeque<UpdateBatch>>>,

    /// Currently processing batches
    processing_batches: Arc<RwLock<HashMap<String, UpdateBatch>>>,

    /// Completed batches (with limited history)
    completed_batches: Arc<RwLock<VecDeque<BatchResult>>>,

    /// Statistics tracker
    stats: Arc<RwLock<BatchStatistics>>,

    /// Notify signal for batch ready
    batch_ready_notify: Arc<Notify>,

    /// Shutdown signal
    shutdown: Arc<RwLock<bool>>,
}

impl AsyncBatchUpdater {
    /// Create a new async batch updater
    pub fn new(config: AsyncBatchConfig) -> Self {
        let (sender, receiver) = channel(config.channel_buffer_size);

        Self {
            config,
            sender,
            receiver: Arc::new(RwLock::new(Some(receiver))),
            pending_batches: Arc::new(RwLock::new(VecDeque::new())),
            processing_batches: Arc::new(RwLock::new(HashMap::new())),
            completed_batches: Arc::new(RwLock::new(VecDeque::new())),
            stats: Arc::new(RwLock::new(BatchStatistics {
                total_batches_processed: 0,
                total_operations_processed: 0,
                total_operations_succeeded: 0,
                total_operations_failed: 0,
                avg_batch_size: 0.0,
                avg_processing_time_ms: 0.0,
                throughput_ops_per_sec: 0.0,
                queue_size: 0,
                last_batch_at: None,
            })),
            batch_ready_notify: Arc::new(Notify::new()),
            shutdown: Arc::new(RwLock::new(false)),
        }
    }

    /// Get a cloned sender for submitting operations
    pub fn get_sender(&self) -> Sender<UpdateOperation> {
        self.sender.clone()
    }

    /// Submit a single update operation (non-blocking)
    pub async fn submit_operation(&self, operation: UpdateOperation) -> Result<()> {
        // Check back-pressure
        if self.config.enable_backpressure {
            let queue_size = self.pending_batches.read().len();
            if queue_size >= self.config.max_queue_size {
                // Apply back-pressure: wait until queue size reduces
                while self.pending_batches.read().len() >= self.config.max_queue_size {
                    tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
                }
            }
        }

        self.sender
            .send(operation)
            .await
            .map_err(|e| crate::GraphRAGError::IncrementalUpdate {
                message: format!("Failed to submit operation: {}", e),
            })?;

        Ok(())
    }

    /// Start the batch processor
    ///
    /// This spawns worker tasks that continuously process incoming operations.
    /// Call this once during initialization.
    pub async fn start(&self) {
        // Take the receiver out of the option (only once)
        let receiver = {
            let mut recv_opt = self.receiver.write();
            recv_opt.take()
        };

        if let Some(mut receiver) = receiver {
            // Spawn batch collector task
            let collector_handle = {
                let config = self.config.clone();
                let pending_batches = Arc::clone(&self.pending_batches);
                let batch_ready_notify = Arc::clone(&self.batch_ready_notify);
                let stats = Arc::clone(&self.stats);
                let shutdown = Arc::clone(&self.shutdown);

                tokio::spawn(async move {
                    let mut current_batch_operations = Vec::new();
                    let mut last_batch_time = std::time::Instant::now();

                    loop {
                        if *shutdown.read() {
                            break;
                        }

                        // Try to receive operation with timeout
                        let timeout = tokio::time::Duration::from_millis(config.max_batch_delay_ms);
                        match tokio::time::timeout(timeout, receiver.recv()).await {
                            Ok(Some(operation)) => {
                                current_batch_operations.push(operation);

                                // Check if batch is full
                                if current_batch_operations.len() >= config.max_batch_size {
                                    Self::create_and_queue_batch(
                                        &mut current_batch_operations,
                                        &pending_batches,
                                        &batch_ready_notify,
                                        &stats,
                                    );
                                    last_batch_time = std::time::Instant::now();
                                }
                            },
                            Ok(None) => {
                                // Channel closed
                                break;
                            },
                            Err(_) => {
                                // Timeout: create batch if we have operations
                                if !current_batch_operations.is_empty()
                                    && last_batch_time.elapsed().as_millis()
                                        >= config.max_batch_delay_ms as u128
                                {
                                    Self::create_and_queue_batch(
                                        &mut current_batch_operations,
                                        &pending_batches,
                                        &batch_ready_notify,
                                        &stats,
                                    );
                                    last_batch_time = std::time::Instant::now();
                                }
                            },
                        }
                    }

                    // Process remaining operations
                    if !current_batch_operations.is_empty() {
                        Self::create_and_queue_batch(
                            &mut current_batch_operations,
                            &pending_batches,
                            &batch_ready_notify,
                            &stats,
                        );
                    }
                })
            };

            // Spawn processor workers
            for _ in 0..self.config.num_workers {
                let config = self.config.clone();
                let pending_batches = Arc::clone(&self.pending_batches);
                let processing_batches = Arc::clone(&self.processing_batches);
                let completed_batches = Arc::clone(&self.completed_batches);
                let stats = Arc::clone(&self.stats);
                let batch_ready_notify = Arc::clone(&self.batch_ready_notify);
                let shutdown = Arc::clone(&self.shutdown);

                tokio::spawn(async move {
                    loop {
                        if *shutdown.read() {
                            break;
                        }

                        // Wait for batch ready notification
                        batch_ready_notify.notified().await;

                        // Try to get a batch from queue
                        let batch = {
                            let mut queue = pending_batches.write();
                            queue.pop_front()
                        };

                        if let Some(mut batch) = batch {
                            // Mark as processing
                            batch.status = BatchStatus::Processing;
                            batch.started_at = Some(Utc::now());

                            let batch_id = batch.batch_id.clone();
                            processing_batches
                                .write()
                                .insert(batch_id.clone(), batch.clone());

                            // Process the batch
                            let result = Self::process_batch_operations(&config, &batch).await;

                            // Mark as completed
                            batch.status = if result.operations_failed == 0 {
                                BatchStatus::Completed
                            } else {
                                BatchStatus::Failed
                            };
                            batch.completed_at = Some(Utc::now());

                            // Move to completed
                            processing_batches.write().remove(&batch_id);

                            let mut completed = completed_batches.write();
                            completed.push_back(result.clone());

                            // Keep only recent history (last 100 batches)
                            if completed.len() > 100 {
                                completed.pop_front();
                            }

                            // Update statistics
                            Self::update_statistics(&stats, &result);
                        }
                    }
                });
            }

            // Keep collector handle alive (in production, store and await on shutdown)
            drop(collector_handle);
        }
    }

    /// Shutdown the batch processor
    pub async fn shutdown(&self) {
        *self.shutdown.write() = true;
        self.batch_ready_notify.notify_waiters();
    }

    /// Get current statistics
    pub fn get_statistics(&self) -> BatchStatistics {
        self.stats.read().clone()
    }

    /// Get completed batch results
    pub fn get_completed_batches(&self) -> Vec<BatchResult> {
        self.completed_batches.read().iter().cloned().collect()
    }

    fn create_and_queue_batch(
        operations: &mut Vec<UpdateOperation>,
        pending_batches: &Arc<RwLock<VecDeque<UpdateBatch>>>,
        batch_ready_notify: &Arc<Notify>,
        stats: &Arc<RwLock<BatchStatistics>>,
    ) {
        let batch = UpdateBatch {
            batch_id: uuid::Uuid::new_v4().to_string(),
            operations: std::mem::take(operations),
            created_at: Utc::now(),
            started_at: None,
            completed_at: None,
            status: BatchStatus::Pending,
        };

        let batch_size = batch.operations.len();

        pending_batches.write().push_back(batch);

        // Update queue size
        let mut stats_lock = stats.write();
        stats_lock.queue_size += batch_size;

        // Notify workers
        batch_ready_notify.notify_one();
    }

    async fn process_batch_operations(
        config: &AsyncBatchConfig,
        batch: &UpdateBatch,
    ) -> BatchResult {
        let start_time = std::time::Instant::now();

        let operations_count = batch.operations.len();
        let mut succeeded = 0;
        let mut failed = 0;
        let mut errors = Vec::new();

        // Process operations in parallel if configured and above threshold
        if config.parallel_within_batch && operations_count >= config.parallel_threshold {
            // Use rayon for CPU-bound operations
            let results: Vec<_> = batch
                .operations
                .par_iter()
                .map(Self::process_single_operation)
                .collect();

            for result in results {
                match result {
                    Ok(_) => succeeded += 1,
                    Err(e) => {
                        failed += 1;
                        errors.push(e);
                    },
                }
            }
        } else {
            // Sequential processing
            for operation in &batch.operations {
                match Self::process_single_operation(operation) {
                    Ok(_) => succeeded += 1,
                    Err(e) => {
                        failed += 1;
                        errors.push(e);
                    },
                }
            }
        }

        let processing_time = start_time.elapsed().as_millis() as u64;

        BatchResult {
            batch_id: batch.batch_id.clone(),
            operations_processed: operations_count,
            operations_succeeded: succeeded,
            operations_failed: failed,
            processing_time_ms: processing_time,
            errors,
        }
    }

    fn process_single_operation(operation: &UpdateOperation) -> std::result::Result<(), String> {
        // Placeholder: In production, this would call the actual graph update methods
        match &operation.operation_type {
            OperationType::AddNode => {
                // graph.add_node(...)
                Ok(())
            },
            OperationType::UpdateNode => {
                // graph.update_node(...)
                Ok(())
            },
            OperationType::RemoveNode => {
                // graph.remove_node(...)
                Ok(())
            },
            OperationType::AddEdge => {
                // graph.add_edge(...)
                Ok(())
            },
            OperationType::UpdateEdge => {
                // graph.update_edge(...)
                Ok(())
            },
            OperationType::RemoveEdge => {
                // graph.remove_edge(...)
                Ok(())
            },
        }
    }

    fn update_statistics(stats: &Arc<RwLock<BatchStatistics>>, result: &BatchResult) {
        let mut stats_lock = stats.write();

        stats_lock.total_batches_processed += 1;
        stats_lock.total_operations_processed += result.operations_processed;
        stats_lock.total_operations_succeeded += result.operations_succeeded;
        stats_lock.total_operations_failed += result.operations_failed;

        // Update averages
        let total_batches = stats_lock.total_batches_processed as f32;
        stats_lock.avg_batch_size = stats_lock.total_operations_processed as f32 / total_batches;
        stats_lock.avg_processing_time_ms = ((stats_lock.avg_processing_time_ms
            * (total_batches - 1.0))
            + result.processing_time_ms as f32)
            / total_batches;

        // Calculate throughput (operations per second)
        if stats_lock.avg_processing_time_ms > 0.0 {
            stats_lock.throughput_ops_per_sec =
                (stats_lock.avg_batch_size / stats_lock.avg_processing_time_ms) * 1000.0;
        }

        stats_lock.last_batch_at = Some(Utc::now());
        stats_lock.queue_size = stats_lock
            .queue_size
            .saturating_sub(result.operations_processed);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_async_batch_creation() {
        let config = AsyncBatchConfig {
            max_batch_size: 10,
            max_batch_delay_ms: 100,
            ..Default::default()
        };

        let updater = AsyncBatchUpdater::new(config);

        // Submit operations
        for i in 0..5 {
            let operation = UpdateOperation {
                operation_id: format!("op_{}", i),
                operation_type: OperationType::AddNode,
                data: UpdateData::Node {
                    node_id: format!("node_{}", i),
                    properties: HashMap::new(),
                    embeddings: None,
                },
                priority: 0,
                created_at: Utc::now(),
            };

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

        // Check that operations were queued
        let stats = updater.get_statistics();
        assert!(stats.queue_size >= 0);
    }

    #[tokio::test]
    async fn test_batch_processing() {
        let config = AsyncBatchConfig {
            max_batch_size: 3,
            max_batch_delay_ms: 100,
            num_workers: 1,
            ..Default::default()
        };

        let updater = AsyncBatchUpdater::new(config);

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

        // Submit operations
        for i in 0..6 {
            let operation = UpdateOperation {
                operation_id: format!("op_{}", i),
                operation_type: OperationType::AddNode,
                data: UpdateData::Node {
                    node_id: format!("node_{}", i),
                    properties: HashMap::new(),
                    embeddings: None,
                },
                priority: 0,
                created_at: 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);

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

    #[test]
    fn test_batch_result_creation() {
        let result = BatchResult {
            batch_id: "batch_1".to_string(),
            operations_processed: 10,
            operations_succeeded: 9,
            operations_failed: 1,
            processing_time_ms: 150,
            errors: vec!["Error processing op_5".to_string()],
        };

        assert_eq!(result.operations_processed, 10);
        assert_eq!(result.operations_succeeded, 9);
        assert_eq!(result.operations_failed, 1);
    }

    #[test]
    fn test_operation_types() {
        let node_op = UpdateOperation {
            operation_id: "op_1".to_string(),
            operation_type: OperationType::AddNode,
            data: UpdateData::Node {
                node_id: "node_1".to_string(),
                properties: HashMap::from([("key".to_string(), "value".to_string())]),
                embeddings: None,
            },
            priority: 5,
            created_at: Utc::now(),
        };

        assert_eq!(node_op.operation_id, "op_1");
        assert_eq!(node_op.priority, 5);
    }

    #[tokio::test]
    async fn test_backpressure() {
        let config = AsyncBatchConfig {
            max_batch_size: 100,
            max_queue_size: 10,
            enable_backpressure: true,
            ..Default::default()
        };

        let updater = AsyncBatchUpdater::new(config);

        // Submit many operations (should trigger back-pressure)
        let mut handles = Vec::new();
        for i in 0..20 {
            let updater_clone = updater.get_sender();
            let handle = tokio::spawn(async move {
                let operation = UpdateOperation {
                    operation_id: format!("op_{}", i),
                    operation_type: OperationType::AddNode,
                    data: UpdateData::Node {
                        node_id: format!("node_{}", i),
                        properties: HashMap::new(),
                        embeddings: None,
                    },
                    priority: 0,
                    created_at: Utc::now(),
                };

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

        // All should eventually succeed (with back-pressure delay)
        for handle in handles {
            assert!(handle.await.is_ok());
        }
    }
}