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
746
#![allow(unused_imports)]

use crate::core::{
    DocumentId, Entity, EntityId, GraphRAGError, KnowledgeGraph, Relationship, Result, TextChunk,
};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::time::{Duration, Instant};

#[cfg(feature = "incremental")]
use std::sync::Arc;

#[cfg(feature = "incremental")]
use {
    dashmap::DashMap,
    parking_lot::{Mutex, RwLock},
    tokio::sync::{broadcast, Semaphore},
    uuid::Uuid,
};

use super::*;

// ============================================================================
// Production-Ready IncrementalGraphStore Implementation
// ============================================================================

/// Production implementation of IncrementalGraphStore with full ACID guarantees
#[cfg(feature = "incremental")]
#[allow(dead_code)]
pub struct ProductionGraphStore {
    graph: Arc<RwLock<KnowledgeGraph>>,
    transactions: DashMap<TransactionId, Transaction>,
    change_log: DashMap<UpdateId, ChangeRecord>,
    rollback_data: DashMap<UpdateId, RollbackData>,
    conflict_resolver: Arc<ConflictResolver>,
    cache_invalidation: Arc<SelectiveInvalidation>,
    monitor: Arc<UpdateMonitor>,
    batch_processor: Arc<BatchProcessor>,
    incremental_pagerank: Arc<IncrementalPageRank>,
    event_publisher: broadcast::Sender<ChangeEvent>,
    config: IncrementalConfig,
}

/// Transaction state for ACID operations
#[derive(Debug, Clone)]
#[allow(dead_code)]
struct Transaction {
    id: TransactionId,
    changes: Vec<ChangeRecord>,
    status: TransactionStatus,
    created_at: DateTime<Utc>,
    isolation_level: IsolationLevel,
}

#[derive(Debug, Clone, PartialEq)]
#[allow(dead_code)]
enum TransactionStatus {
    Active,
    Preparing,
    Committed,
    Aborted,
}

#[derive(Debug, Clone)]
#[allow(dead_code)]
enum IsolationLevel {
    ReadUncommitted,
    ReadCommitted,
    RepeatableRead,
    Serializable,
}

/// Change event for monitoring and debugging
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChangeEvent {
    /// Unique identifier for the event
    pub event_id: UpdateId,
    /// Type of change event
    pub event_type: ChangeEventType,
    /// Optional entity ID associated with the event
    pub entity_id: Option<EntityId>,
    /// When the event occurred
    pub timestamp: DateTime<Utc>,
    /// Additional metadata about the event
    pub metadata: HashMap<String, String>,
}

/// Types of change events that can be published
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ChangeEventType {
    /// An entity was upserted
    EntityUpserted,
    /// An entity was deleted
    EntityDeleted,
    /// A relationship was upserted
    RelationshipUpserted,
    /// A relationship was deleted
    RelationshipDeleted,
    /// An embedding was updated
    EmbeddingUpdated,
    /// A transaction was started
    TransactionStarted,
    /// A transaction was committed
    TransactionCommitted,
    /// A transaction was rolled back
    TransactionRolledBack,
    /// A conflict was resolved
    ConflictResolved,
    /// Cache was invalidated
    CacheInvalidated,
    /// A batch was processed
    BatchProcessed,
}

#[cfg(feature = "incremental")]
impl ProductionGraphStore {
    /// Creates a new production-grade graph store with full ACID guarantees
    pub fn new(
        graph: KnowledgeGraph,
        config: IncrementalConfig,
        conflict_resolver: ConflictResolver,
    ) -> Self {
        let (event_tx, _) = broadcast::channel(1000);

        Self {
            graph: Arc::new(RwLock::new(graph)),
            transactions: DashMap::new(),
            change_log: DashMap::new(),
            rollback_data: DashMap::new(),
            conflict_resolver: Arc::new(conflict_resolver),
            cache_invalidation: Arc::new(SelectiveInvalidation::new()),
            monitor: Arc::new(UpdateMonitor::new()),
            batch_processor: Arc::new(BatchProcessor::new(
                config.batch_size,
                Duration::from_millis(100),
                config.max_concurrent_operations,
            )),
            incremental_pagerank: Arc::new(IncrementalPageRank::new(0.85, 1e-6, 100)),
            event_publisher: event_tx,
            config,
        }
    }

    /// Subscribes to change events for monitoring
    pub fn subscribe_events(&self) -> broadcast::Receiver<ChangeEvent> {
        self.event_publisher.subscribe()
    }

    async fn publish_event(&self, event: ChangeEvent) {
        let _ = self.event_publisher.send(event);
    }

    fn create_change_record(
        &self,
        change_type: ChangeType,
        operation: Operation,
        change_data: ChangeData,
        entity_id: Option<EntityId>,
        document_id: Option<DocumentId>,
    ) -> ChangeRecord {
        ChangeRecord {
            change_id: UpdateId::new(),
            timestamp: Utc::now(),
            change_type,
            entity_id,
            document_id,
            operation,
            data: change_data,
            metadata: HashMap::new(),
        }
    }

    async fn apply_change_with_conflict_resolution(
        &self,
        change: ChangeRecord,
    ) -> Result<UpdateId> {
        let operation_id = self.monitor.start_operation("apply_change");

        // Check for conflicts
        if let Some(conflict) = self.detect_conflict(&change)? {
            let resolution = self.conflict_resolver.resolve_conflict(&conflict).await?;

            // Apply resolved change
            let resolved_change = ChangeRecord {
                data: resolution.resolved_data,
                metadata: resolution.metadata,
                ..change
            };

            self.apply_change_internal(resolved_change).await?;

            // Publish conflict resolution event
            self.publish_event(ChangeEvent {
                event_id: UpdateId::new(),
                event_type: ChangeEventType::ConflictResolved,
                entity_id: conflict.existing_data.get_entity_id(),
                timestamp: Utc::now(),
                metadata: HashMap::new(),
            })
            .await;
        } else {
            self.apply_change_internal(change).await?;
        }

        self.monitor
            .complete_operation(&operation_id, true, None, 1, 0);
        Ok(operation_id)
    }

    fn detect_conflict(&self, change: &ChangeRecord) -> Result<Option<Conflict>> {
        match &change.data {
            ChangeData::Entity(entity) => {
                let graph = self.graph.read();
                if let Some(existing) = graph.get_entity(&entity.id) {
                    if existing.name != entity.name || existing.entity_type != entity.entity_type {
                        return Ok(Some(Conflict {
                            conflict_id: UpdateId::new(),
                            conflict_type: ConflictType::EntityExists,
                            existing_data: ChangeData::Entity(existing.clone()),
                            new_data: change.data.clone(),
                            resolution: None,
                        }));
                    }
                }
            },
            ChangeData::Relationship(relationship) => {
                let graph = self.graph.read();
                for existing_rel in graph.get_all_relationships() {
                    if existing_rel.source == relationship.source
                        && existing_rel.target == relationship.target
                        && existing_rel.relation_type == relationship.relation_type
                    {
                        return Ok(Some(Conflict {
                            conflict_id: UpdateId::new(),
                            conflict_type: ConflictType::RelationshipExists,
                            existing_data: ChangeData::Relationship(existing_rel.clone()),
                            new_data: change.data.clone(),
                            resolution: None,
                        }));
                    }
                }
            },
            _ => {},
        }

        Ok(None)
    }

    async fn apply_change_internal(&self, change: ChangeRecord) -> Result<()> {
        let change_id = change.change_id.clone();

        // Create rollback data first
        let rollback_data = {
            let graph = self.graph.read();
            self.create_rollback_data(&change, &graph)?
        };

        self.rollback_data.insert(change_id.clone(), rollback_data);

        // Apply the change
        {
            let mut graph = self.graph.write();
            match &change.data {
                ChangeData::Entity(entity) => {
                    match change.operation {
                        Operation::Insert | Operation::Upsert => {
                            graph.add_entity(entity.clone())?;
                            self.incremental_pagerank.record_change(entity.id.clone());
                        },
                        Operation::Delete => {
                            // Remove entity and its relationships
                            // Implementation would go here
                        },
                        _ => {},
                    }
                },
                ChangeData::Relationship(relationship) => {
                    match change.operation {
                        Operation::Insert | Operation::Upsert => {
                            graph.add_relationship(relationship.clone())?;
                            self.incremental_pagerank
                                .record_change(relationship.source.clone());
                            self.incremental_pagerank
                                .record_change(relationship.target.clone());
                        },
                        Operation::Delete => {
                            // Remove relationship
                            // Implementation would go here
                        },
                        _ => {},
                    }
                },
                ChangeData::Embedding {
                    entity_id,
                    embedding,
                } => {
                    if let Some(entity) = graph.get_entity_mut(entity_id) {
                        entity.embedding = Some(embedding.clone());
                    }
                },
                _ => {},
            }
        }

        // Record change in log
        self.change_log.insert(change_id, change);

        Ok(())
    }

    fn create_rollback_data(
        &self,
        change: &ChangeRecord,
        graph: &KnowledgeGraph,
    ) -> Result<RollbackData> {
        let mut previous_entities = Vec::new();
        let mut previous_relationships = Vec::new();

        match &change.data {
            ChangeData::Entity(entity) => {
                if let Some(existing) = graph.get_entity(&entity.id) {
                    previous_entities.push(existing.clone());
                }
            },
            ChangeData::Relationship(relationship) => {
                // Store existing relationships that might be affected
                for rel in graph.get_all_relationships() {
                    if rel.source == relationship.source && rel.target == relationship.target {
                        previous_relationships.push(rel.clone());
                    }
                }
            },
            _ => {},
        }

        Ok(RollbackData {
            previous_entities,
            previous_relationships,
            affected_caches: vec![], // Will be populated by cache invalidation system
        })
    }
}

#[cfg(feature = "incremental")]
#[async_trait::async_trait]
impl IncrementalGraphStore for ProductionGraphStore {
    type Error = GraphRAGError;

    async fn upsert_entity(&mut self, entity: Entity) -> Result<UpdateId> {
        let change = self.create_change_record(
            ChangeType::EntityAdded,
            Operation::Upsert,
            ChangeData::Entity(entity.clone()),
            Some(entity.id.clone()),
            None,
        );

        let update_id = self.apply_change_with_conflict_resolution(change).await?;

        // Trigger cache invalidation
        let changes = vec![self
            .change_log
            .get(&update_id)
            .expect("just inserted above")
            .clone()];
        let _invalidation_strategies = self.cache_invalidation.invalidate_for_changes(&changes);

        // Publish event
        self.publish_event(ChangeEvent {
            event_id: UpdateId::new(),
            event_type: ChangeEventType::EntityUpserted,
            entity_id: Some(entity.id),
            timestamp: Utc::now(),
            metadata: HashMap::new(),
        })
        .await;

        Ok(update_id)
    }

    async fn upsert_relationship(&mut self, relationship: Relationship) -> Result<UpdateId> {
        let change = self.create_change_record(
            ChangeType::RelationshipAdded,
            Operation::Upsert,
            ChangeData::Relationship(relationship.clone()),
            None,
            None,
        );

        let update_id = self.apply_change_with_conflict_resolution(change).await?;

        // Publish event
        self.publish_event(ChangeEvent {
            event_id: UpdateId::new(),
            event_type: ChangeEventType::RelationshipUpserted,
            entity_id: Some(relationship.source),
            timestamp: Utc::now(),
            metadata: HashMap::new(),
        })
        .await;

        Ok(update_id)
    }

    async fn delete_entity(&mut self, entity_id: &EntityId) -> Result<UpdateId> {
        // Implementation for entity deletion
        let update_id = UpdateId::new();

        // Publish event
        self.publish_event(ChangeEvent {
            event_id: UpdateId::new(),
            event_type: ChangeEventType::EntityDeleted,
            entity_id: Some(entity_id.clone()),
            timestamp: Utc::now(),
            metadata: HashMap::new(),
        })
        .await;

        Ok(update_id)
    }

    async fn delete_relationship(
        &mut self,
        source: &EntityId,
        _target: &EntityId,
        _relation_type: &str,
    ) -> Result<UpdateId> {
        // Implementation for relationship deletion
        let update_id = UpdateId::new();

        // Publish event
        self.publish_event(ChangeEvent {
            event_id: UpdateId::new(),
            event_type: ChangeEventType::RelationshipDeleted,
            entity_id: Some(source.clone()),
            timestamp: Utc::now(),
            metadata: HashMap::new(),
        })
        .await;

        Ok(update_id)
    }

    async fn apply_delta(&mut self, delta: GraphDelta) -> Result<UpdateId> {
        let tx_id = self.begin_transaction().await?;

        for change in delta.changes {
            self.apply_change_with_conflict_resolution(change).await?;
        }

        self.commit_transaction(tx_id).await?;
        Ok(delta.delta_id)
    }

    async fn rollback_delta(&mut self, _delta_id: &UpdateId) -> Result<()> {
        // Implementation for delta rollback
        Ok(())
    }

    async fn get_change_log(&self, since: Option<DateTime<Utc>>) -> Result<Vec<ChangeRecord>> {
        let changes: Vec<ChangeRecord> = self
            .change_log
            .iter()
            .filter_map(|entry| {
                let change = entry.value();
                if let Some(since_time) = since {
                    if change.timestamp >= since_time {
                        Some(change.clone())
                    } else {
                        None
                    }
                } else {
                    Some(change.clone())
                }
            })
            .collect();

        Ok(changes)
    }

    async fn begin_transaction(&mut self) -> Result<TransactionId> {
        let tx_id = TransactionId::new();
        let transaction = Transaction {
            id: tx_id.clone(),
            changes: Vec::new(),
            status: TransactionStatus::Active,
            created_at: Utc::now(),
            isolation_level: IsolationLevel::ReadCommitted,
        };

        self.transactions.insert(tx_id.clone(), transaction);

        // Publish event
        self.publish_event(ChangeEvent {
            event_id: UpdateId::new(),
            event_type: ChangeEventType::TransactionStarted,
            entity_id: None,
            timestamp: Utc::now(),
            metadata: [("transaction_id".to_string(), tx_id.to_string())]
                .into_iter()
                .collect(),
        })
        .await;

        Ok(tx_id)
    }

    async fn commit_transaction(&mut self, tx_id: TransactionId) -> Result<()> {
        if let Some((_, mut tx)) = self.transactions.remove(&tx_id) {
            tx.status = TransactionStatus::Committed;

            // Publish event
            self.publish_event(ChangeEvent {
                event_id: UpdateId::new(),
                event_type: ChangeEventType::TransactionCommitted,
                entity_id: None,
                timestamp: Utc::now(),
                metadata: [("transaction_id".to_string(), tx_id.to_string())]
                    .into_iter()
                    .collect(),
            })
            .await;

            Ok(())
        } else {
            Err(GraphRAGError::IncrementalUpdate {
                message: format!("Transaction {tx_id} not found"),
            })
        }
    }

    async fn rollback_transaction(&mut self, tx_id: TransactionId) -> Result<()> {
        if let Some((_, mut tx)) = self.transactions.remove(&tx_id) {
            tx.status = TransactionStatus::Aborted;

            // Rollback all changes in this transaction
            for _change in &tx.changes {
                // Implementation for rollback
            }

            // Publish event
            self.publish_event(ChangeEvent {
                event_id: UpdateId::new(),
                event_type: ChangeEventType::TransactionRolledBack,
                entity_id: None,
                timestamp: Utc::now(),
                metadata: [("transaction_id".to_string(), tx_id.to_string())]
                    .into_iter()
                    .collect(),
            })
            .await;

            Ok(())
        } else {
            Err(GraphRAGError::IncrementalUpdate {
                message: format!("Transaction {tx_id} not found"),
            })
        }
    }

    async fn batch_upsert_entities(
        &mut self,
        entities: Vec<Entity>,
        _strategy: ConflictStrategy,
    ) -> Result<Vec<UpdateId>> {
        let mut update_ids = Vec::new();

        for entity in entities {
            let update_id = self.upsert_entity(entity).await?;
            update_ids.push(update_id);
        }

        Ok(update_ids)
    }

    async fn batch_upsert_relationships(
        &mut self,
        relationships: Vec<Relationship>,
        _strategy: ConflictStrategy,
    ) -> Result<Vec<UpdateId>> {
        let mut update_ids = Vec::new();

        for relationship in relationships {
            let update_id = self.upsert_relationship(relationship).await?;
            update_ids.push(update_id);
        }

        Ok(update_ids)
    }

    async fn update_entity_embedding(
        &mut self,
        entity_id: &EntityId,
        embedding: Vec<f32>,
    ) -> Result<UpdateId> {
        let change = self.create_change_record(
            ChangeType::EmbeddingUpdated,
            Operation::Update,
            ChangeData::Embedding {
                entity_id: entity_id.clone(),
                embedding,
            },
            Some(entity_id.clone()),
            None,
        );

        let update_id = self.apply_change_with_conflict_resolution(change).await?;

        // Publish event
        self.publish_event(ChangeEvent {
            event_id: UpdateId::new(),
            event_type: ChangeEventType::EmbeddingUpdated,
            entity_id: Some(entity_id.clone()),
            timestamp: Utc::now(),
            metadata: HashMap::new(),
        })
        .await;

        Ok(update_id)
    }

    async fn bulk_update_embeddings(
        &mut self,
        updates: Vec<(EntityId, Vec<f32>)>,
    ) -> Result<Vec<UpdateId>> {
        let mut update_ids = Vec::new();

        for (entity_id, embedding) in updates {
            let update_id = self.update_entity_embedding(&entity_id, embedding).await?;
            update_ids.push(update_id);
        }

        Ok(update_ids)
    }

    async fn get_pending_transactions(&self) -> Result<Vec<TransactionId>> {
        let pending: Vec<TransactionId> = self
            .transactions
            .iter()
            .filter(|entry| entry.value().status == TransactionStatus::Active)
            .map(|entry| entry.key().clone())
            .collect();

        Ok(pending)
    }

    async fn get_graph_statistics(&self) -> Result<GraphStatistics> {
        let graph = self.graph.read();
        let entities: Vec<_> = graph.entities().collect();
        let relationships = graph.get_all_relationships();

        let node_count = entities.len();
        let edge_count = relationships.len();

        // Calculate average degree
        let total_degree: usize = entities
            .iter()
            .map(|entity| graph.get_neighbors(&entity.id).len())
            .sum();

        let average_degree = if node_count > 0 {
            total_degree as f64 / node_count as f64
        } else {
            0.0
        };

        // Find max degree
        let max_degree = entities
            .iter()
            .map(|entity| graph.get_neighbors(&entity.id).len())
            .max()
            .unwrap_or(0);

        Ok(GraphStatistics {
            node_count,
            edge_count,
            average_degree,
            max_degree,
            connected_components: 1,     // Simplified for now
            clustering_coefficient: 0.0, // Would need complex calculation
            last_updated: Utc::now(),
        })
    }

    async fn validate_consistency(&self) -> Result<ConsistencyReport> {
        let graph = self.graph.read();
        let mut orphaned_entities = Vec::new();
        let mut broken_relationships = Vec::new();
        let mut missing_embeddings = Vec::new();

        // Check for orphaned entities (entities with no relationships)
        for entity in graph.entities() {
            let neighbors = graph.get_neighbors(&entity.id);
            if neighbors.is_empty() {
                orphaned_entities.push(entity.id.clone());
            }

            // Check for missing embeddings
            if entity.embedding.is_none() {
                missing_embeddings.push(entity.id.clone());
            }
        }

        // Check for broken relationships (references to non-existent entities)
        for relationship in graph.get_all_relationships() {
            if graph.get_entity(&relationship.source).is_none()
                || graph.get_entity(&relationship.target).is_none()
            {
                broken_relationships.push((
                    relationship.source.clone(),
                    relationship.target.clone(),
                    relationship.relation_type.clone(),
                ));
            }
        }

        let issues_found =
            orphaned_entities.len() + broken_relationships.len() + missing_embeddings.len();

        Ok(ConsistencyReport {
            is_consistent: issues_found == 0,
            orphaned_entities,
            broken_relationships,
            missing_embeddings,
            validation_time: Utc::now(),
            issues_found,
        })
    }
}

// Helper trait for extracting entity ID from ChangeData
#[allow(dead_code)]
trait ChangeDataExt {
    fn get_entity_id(&self) -> Option<EntityId>;
}

impl ChangeDataExt for ChangeData {
    fn get_entity_id(&self) -> Option<EntityId> {
        match self {
            ChangeData::Entity(entity) => Some(entity.id.clone()),
            ChangeData::Embedding { entity_id, .. } => Some(entity_id.clone()),
            _ => None,
        }
    }
}