rrag 0.1.0-alpha.2

High-performance Rust framework for Retrieval-Augmented Generation with pluggable components, async-first design, and comprehensive observability
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
//! # Incremental Index Manager
//!
//! Manages incremental updates to document indexes without requiring full rebuilds.
//! Handles conflict resolution, operation queuing, and index consistency.

use crate::incremental::change_detection::{ChangeResult, ChangeType};
use crate::{Document, DocumentChunk, Embedding, RragError, RragResult};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet, VecDeque};
use std::sync::Arc;
use tokio::sync::{Mutex, RwLock};
use uuid::Uuid;

/// Index manager configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndexManagerConfig {
    /// Maximum pending operations
    pub max_pending_operations: usize,

    /// Operation batch size
    pub batch_size: usize,

    /// Operation timeout in seconds
    pub operation_timeout_secs: u64,

    /// Enable conflict resolution
    pub enable_conflict_resolution: bool,

    /// Conflict resolution strategy
    pub conflict_resolution: ConflictResolutionStrategy,

    /// Enable operation logging
    pub enable_operation_log: bool,

    /// Maximum operation log size
    pub max_operation_log: usize,

    /// Enable automatic cleanup
    pub enable_auto_cleanup: bool,

    /// Cleanup interval in seconds
    pub cleanup_interval_secs: u64,
}

/// Conflict resolution strategies
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ConflictResolutionStrategy {
    /// Last write wins
    LastWriteWins,
    /// First write wins
    FirstWriteWins,
    /// Merge changes when possible
    Merge,
    /// Manual resolution required
    Manual,
    /// Use version timestamps
    Timestamp,
    /// Use custom resolution logic
    Custom(String),
}

impl Default for IndexManagerConfig {
    fn default() -> Self {
        Self {
            max_pending_operations: 10000,
            batch_size: 100,
            operation_timeout_secs: 300, // 5 minutes
            enable_conflict_resolution: true,
            conflict_resolution: ConflictResolutionStrategy::LastWriteWins,
            enable_operation_log: true,
            max_operation_log: 10000,
            enable_auto_cleanup: true,
            cleanup_interval_secs: 3600, // 1 hour
        }
    }
}

/// Types of index operations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum IndexOperation {
    /// Add new document and its chunks
    Add {
        document: Document,
        chunks: Vec<DocumentChunk>,
        embeddings: Vec<Embedding>,
    },

    /// Update existing document
    Update {
        document_id: String,
        document: Document,
        chunks: Vec<DocumentChunk>,
        embeddings: Vec<Embedding>,
        change_result: ChangeResult,
    },

    /// Delete document and all associated data
    Delete { document_id: String },

    /// Update only embeddings
    UpdateEmbeddings {
        document_id: String,
        embeddings: Vec<Embedding>,
    },

    /// Update only chunks
    UpdateChunks {
        document_id: String,
        chunks: Vec<DocumentChunk>,
    },

    /// Batch operation containing multiple operations
    Batch { operations: Vec<IndexOperation> },

    /// Rebuild specific index
    Rebuild {
        index_name: String,
        document_ids: Vec<String>,
    },
}

/// Index update specification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndexUpdate {
    /// Unique operation ID
    pub operation_id: String,

    /// Operation to perform
    pub operation: IndexOperation,

    /// Priority level (0-10, higher = more priority)
    pub priority: u8,

    /// Operation timestamp
    pub timestamp: chrono::DateTime<chrono::Utc>,

    /// Source of the operation
    pub source: String,

    /// Operation metadata
    pub metadata: HashMap<String, serde_json::Value>,

    /// Dependencies on other operations
    pub dependencies: Vec<String>,

    /// Maximum retry attempts
    pub max_retries: u32,

    /// Current retry count
    pub retry_count: u32,
}

/// Result of an update operation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateResult {
    /// Operation ID
    pub operation_id: String,

    /// Whether the operation succeeded
    pub success: bool,

    /// Operations performed
    pub operations_completed: Vec<String>,

    /// Conflicts encountered
    pub conflicts: Vec<ConflictInfo>,

    /// Processing time in milliseconds
    pub processing_time_ms: u64,

    /// Items affected
    pub items_affected: usize,

    /// Error details if failed
    pub error: Option<String>,

    /// Metadata about the operation
    pub metadata: HashMap<String, serde_json::Value>,
}

/// Conflict information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConflictInfo {
    /// Document ID where conflict occurred
    pub document_id: String,

    /// Type of conflict
    pub conflict_type: ConflictType,

    /// Conflicting operations
    pub conflicting_operations: Vec<String>,

    /// Resolution applied
    pub resolution: ConflictResolution,

    /// Additional context
    pub context: HashMap<String, serde_json::Value>,
}

/// Types of conflicts
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ConflictType {
    /// Multiple updates to the same document
    ConcurrentUpdate,
    /// Version mismatch
    VersionMismatch,
    /// Dependency conflict
    DependencyConflict,
    /// Resource lock conflict
    ResourceLock,
    /// Schema conflict
    SchemaConflict,
}

/// Conflict resolution applied
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ConflictResolution {
    /// Automatically resolved
    AutoResolved(String),
    /// Manually resolved
    ManuallyResolved(String),
    /// Deferred for later resolution
    Deferred,
    /// Failed to resolve
    Failed(String),
}

/// Operation status tracking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum OperationStatus {
    /// Queued for processing
    Queued,
    /// Currently being processed
    Processing,
    /// Successfully completed
    Completed,
    /// Failed with error
    Failed(String),
    /// Cancelled
    Cancelled,
    /// Waiting for dependencies
    Waiting,
    /// Conflict resolution required
    ConflictResolution,
}

/// Tracked operation state
#[derive(Debug, Clone)]
struct TrackedOperation {
    /// Update specification
    update: IndexUpdate,

    /// Current status
    status: OperationStatus,

    /// Start time
    start_time: Option<chrono::DateTime<chrono::Utc>>,

    /// End time
    end_time: Option<chrono::DateTime<chrono::Utc>>,

    /// Result if completed
    result: Option<UpdateResult>,
}

/// Incremental index manager
pub struct IncrementalIndexManager {
    /// Configuration
    config: IndexManagerConfig,

    /// Pending operations queue
    pending_operations: Arc<Mutex<VecDeque<TrackedOperation>>>,

    /// Currently processing operations
    processing_operations: Arc<RwLock<HashMap<String, TrackedOperation>>>,

    /// Completed operations history
    completed_operations: Arc<RwLock<VecDeque<TrackedOperation>>>,

    /// Index state tracking
    index_state: Arc<RwLock<IndexState>>,

    /// Conflict resolution system
    conflict_resolver: Arc<ConflictResolver>,

    /// Operation statistics
    stats: Arc<RwLock<IndexManagerStats>>,

    /// Background task handles
    task_handles: Arc<Mutex<Vec<tokio::task::JoinHandle<()>>>>,
}

/// Index state tracking
#[derive(Debug)]
struct IndexState {
    /// Documents currently indexed
    indexed_documents: HashSet<String>,

    /// Document versions
    document_versions: HashMap<String, u64>,

    /// Document locks for concurrent access
    document_locks: HashMap<String, tokio::sync::Mutex<()>>,

    /// Index metadata
    metadata: HashMap<String, serde_json::Value>,

    /// Last update timestamp
    last_updated: chrono::DateTime<chrono::Utc>,
}

/// Conflict resolution system
struct ConflictResolver {
    /// Resolution strategy
    strategy: ConflictResolutionStrategy,

    /// Manual resolution queue
    manual_queue: Arc<Mutex<VecDeque<ConflictInfo>>>,

    /// Resolution history
    resolution_history: Arc<RwLock<Vec<ConflictInfo>>>,
}

/// Index manager statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndexManagerStats {
    /// Total operations processed
    pub total_operations: u64,

    /// Operations by type
    pub operations_by_type: HashMap<String, u64>,

    /// Success rate
    pub success_rate: f64,

    /// Average processing time
    pub avg_processing_time_ms: f64,

    /// Conflicts encountered
    pub total_conflicts: u64,

    /// Conflicts resolved automatically
    pub auto_resolved_conflicts: u64,

    /// Queue depth statistics
    pub current_queue_depth: usize,
    pub max_queue_depth: usize,

    /// Performance metrics
    pub throughput_ops_per_second: f64,

    /// Last updated
    pub last_updated: chrono::DateTime<chrono::Utc>,
}

impl IncrementalIndexManager {
    /// Create a new index manager
    pub async fn new(config: IndexManagerConfig) -> RragResult<Self> {
        let pending_operations = Arc::new(Mutex::new(VecDeque::new()));
        let processing_operations = Arc::new(RwLock::new(HashMap::new()));
        let completed_operations = Arc::new(RwLock::new(VecDeque::new()));

        let index_state = Arc::new(RwLock::new(IndexState {
            indexed_documents: HashSet::new(),
            document_versions: HashMap::new(),
            document_locks: HashMap::new(),
            metadata: HashMap::new(),
            last_updated: chrono::Utc::now(),
        }));

        let conflict_resolver = Arc::new(ConflictResolver {
            strategy: config.conflict_resolution.clone(),
            manual_queue: Arc::new(Mutex::new(VecDeque::new())),
            resolution_history: Arc::new(RwLock::new(Vec::new())),
        });

        let stats = Arc::new(RwLock::new(IndexManagerStats {
            total_operations: 0,
            operations_by_type: HashMap::new(),
            success_rate: 0.0,
            avg_processing_time_ms: 0.0,
            total_conflicts: 0,
            auto_resolved_conflicts: 0,
            current_queue_depth: 0,
            max_queue_depth: 0,
            throughput_ops_per_second: 0.0,
            last_updated: chrono::Utc::now(),
        }));

        let task_handles = Arc::new(Mutex::new(Vec::new()));

        let manager = Self {
            config,
            pending_operations,
            processing_operations,
            completed_operations,
            index_state,
            conflict_resolver,
            stats,
            task_handles,
        };

        // Start background processing tasks
        manager.start_background_tasks().await?;

        Ok(manager)
    }

    /// Submit an update operation
    pub async fn submit_update(&self, update: IndexUpdate) -> RragResult<String> {
        // Validate update
        self.validate_update(&update).await?;

        // Create tracked operation
        let tracked_op = TrackedOperation {
            update: update.clone(),
            status: OperationStatus::Queued,
            start_time: None,
            end_time: None,
            result: None,
        };

        // Add to queue
        {
            let mut queue = self.pending_operations.lock().await;

            // Check queue capacity
            if queue.len() >= self.config.max_pending_operations {
                return Err(RragError::storage(
                    "queue_full",
                    std::io::Error::new(std::io::ErrorKind::Other, "Operation queue is full"),
                ));
            }

            queue.push_back(tracked_op);
        }

        // Update statistics
        {
            let mut stats = self.stats.write().await;
            stats.current_queue_depth = {
                let queue = self.pending_operations.lock().await;
                queue.len()
            };
            stats.max_queue_depth = std::cmp::max(stats.max_queue_depth, stats.current_queue_depth);
        }

        Ok(update.operation_id)
    }

    /// Submit multiple operations as a batch
    pub async fn submit_batch(&self, operations: Vec<IndexUpdate>) -> RragResult<Vec<String>> {
        if operations.is_empty() {
            return Ok(Vec::new());
        }

        // Create batch operation
        let batch_id = Uuid::new_v4().to_string();
        let batch_operation = IndexOperation::Batch {
            operations: operations.iter().map(|op| op.operation.clone()).collect(),
        };

        let batch_update = IndexUpdate {
            operation_id: batch_id.clone(),
            operation: batch_operation,
            priority: operations.iter().map(|op| op.priority).max().unwrap_or(5),
            timestamp: chrono::Utc::now(),
            source: "batch_processor".to_string(),
            metadata: HashMap::new(),
            dependencies: Vec::new(),
            max_retries: 3,
            retry_count: 0,
        };

        // Submit individual operations
        let mut operation_ids = Vec::new();
        for operation in operations {
            let op_id = self.submit_update(operation).await?;
            operation_ids.push(op_id);
        }

        // Submit batch operation
        self.submit_update(batch_update).await?;
        operation_ids.push(batch_id);

        Ok(operation_ids)
    }

    /// Get operation status
    pub async fn get_operation_status(
        &self,
        operation_id: &str,
    ) -> RragResult<Option<OperationStatus>> {
        // Check processing operations
        {
            let processing = self.processing_operations.read().await;
            if let Some(op) = processing.get(operation_id) {
                return Ok(Some(op.status.clone()));
            }
        }

        // Check pending operations
        {
            let queue = self.pending_operations.lock().await;
            for op in queue.iter() {
                if op.update.operation_id == operation_id {
                    return Ok(Some(op.status.clone()));
                }
            }
        }

        // Check completed operations
        {
            let completed = self.completed_operations.read().await;
            for op in completed.iter() {
                if op.update.operation_id == operation_id {
                    return Ok(Some(op.status.clone()));
                }
            }
        }

        Ok(None)
    }

    /// Get operation result
    pub async fn get_operation_result(
        &self,
        operation_id: &str,
    ) -> RragResult<Option<UpdateResult>> {
        // Check processing operations first
        {
            let processing = self.processing_operations.read().await;
            if let Some(op) = processing.get(operation_id) {
                return Ok(op.result.clone());
            }
        }

        // Check completed operations
        {
            let completed = self.completed_operations.read().await;
            for op in completed.iter() {
                if op.update.operation_id == operation_id {
                    return Ok(op.result.clone());
                }
            }
        }

        Ok(None)
    }

    /// Cancel a pending operation
    pub async fn cancel_operation(&self, operation_id: &str) -> RragResult<bool> {
        // Try to cancel from pending queue
        {
            let mut queue = self.pending_operations.lock().await;
            if let Some(pos) = queue
                .iter()
                .position(|op| op.update.operation_id == operation_id)
            {
                queue.remove(pos);
                return Ok(true);
            }
        }

        // Try to cancel from processing (if not too far along)
        {
            let mut processing = self.processing_operations.write().await;
            if let Some(mut op) = processing.remove(operation_id) {
                op.status = OperationStatus::Cancelled;
                op.end_time = Some(chrono::Utc::now());

                // Move to completed
                let mut completed = self.completed_operations.write().await;
                completed.push_back(op);

                return Ok(true);
            }
        }

        Ok(false)
    }

    /// Get current statistics
    pub async fn get_stats(&self) -> IndexManagerStats {
        let mut stats = self.stats.read().await.clone();
        stats.current_queue_depth = {
            let queue = self.pending_operations.lock().await;
            queue.len()
        };
        stats.last_updated = chrono::Utc::now();
        stats
    }

    /// Get index state information
    pub async fn get_index_state(&self) -> RragResult<HashMap<String, serde_json::Value>> {
        let state = self.index_state.read().await;
        let mut info = HashMap::new();

        info.insert(
            "indexed_documents_count".to_string(),
            serde_json::Value::Number(state.indexed_documents.len().into()),
        );
        info.insert(
            "last_updated".to_string(),
            serde_json::Value::String(state.last_updated.to_rfc3339()),
        );
        info.insert(
            "metadata".to_string(),
            serde_json::Value::Object(state.metadata.clone().into_iter().collect()),
        );

        Ok(info)
    }

    /// Health check
    pub async fn health_check(&self) -> RragResult<bool> {
        // Check if background tasks are running
        let handles = self.task_handles.lock().await;
        let all_running = handles.iter().all(|handle| !handle.is_finished());

        // Check queue health
        let queue_size = {
            let queue = self.pending_operations.lock().await;
            queue.len()
        };
        let queue_healthy = queue_size < self.config.max_pending_operations;

        Ok(all_running && queue_healthy)
    }

    /// Start background processing tasks
    async fn start_background_tasks(&self) -> RragResult<()> {
        let mut handles = self.task_handles.lock().await;

        // Operation processor task
        let processor_handle = self.start_operation_processor().await;
        handles.push(processor_handle);

        // Cleanup task
        if self.config.enable_auto_cleanup {
            let cleanup_handle = self.start_cleanup_task().await;
            handles.push(cleanup_handle);
        }

        Ok(())
    }

    /// Start the main operation processor
    async fn start_operation_processor(&self) -> tokio::task::JoinHandle<()> {
        let pending_ops = Arc::clone(&self.pending_operations);
        let processing_ops = Arc::clone(&self.processing_operations);
        let completed_ops = Arc::clone(&self.completed_operations);
        let index_state = Arc::clone(&self.index_state);
        let conflict_resolver = Arc::clone(&self.conflict_resolver);
        let stats = Arc::clone(&self.stats);
        let config = self.config.clone();

        tokio::spawn(async move {
            loop {
                // Process next operation
                let operation = {
                    let mut queue = pending_ops.lock().await;
                    queue.pop_front()
                };

                if let Some(mut tracked_op) = operation {
                    tracked_op.status = OperationStatus::Processing;
                    tracked_op.start_time = Some(chrono::Utc::now());

                    let operation_id = tracked_op.update.operation_id.clone();

                    // Move to processing
                    {
                        let mut processing = processing_ops.write().await;
                        processing.insert(operation_id.clone(), tracked_op.clone());
                    }

                    // Process the operation
                    let result = Self::process_operation(
                        &tracked_op.update,
                        &index_state,
                        &conflict_resolver,
                        &config,
                    )
                    .await;

                    // Update tracked operation
                    tracked_op.end_time = Some(chrono::Utc::now());
                    tracked_op.result = Some(result.clone());
                    tracked_op.status = if result.success {
                        OperationStatus::Completed
                    } else {
                        OperationStatus::Failed(result.error.unwrap_or_default())
                    };

                    // Save operation type for statistics before moving tracked_op
                    let op_type = format!("{:?}", tracked_op.update.operation)
                        .split('{')
                        .next()
                        .unwrap_or("Unknown")
                        .to_string();

                    // Move to completed
                    {
                        let mut processing = processing_ops.write().await;
                        processing.remove(&operation_id);
                    }
                    {
                        let mut completed = completed_ops.write().await;
                        completed.push_back(tracked_op);

                        // Limit completed operations history
                        if completed.len() > config.max_operation_log {
                            completed.pop_front();
                        }
                    }

                    // Update statistics
                    {
                        let mut stats_guard = stats.write().await;
                        stats_guard.total_operations += 1;

                        *stats_guard.operations_by_type.entry(op_type).or_insert(0) += 1;

                        stats_guard.success_rate = if stats_guard.total_operations > 0 {
                            let successful = stats_guard.operations_by_type.values().sum::<u64>();
                            successful as f64 / stats_guard.total_operations as f64
                        } else {
                            0.0
                        };

                        stats_guard.avg_processing_time_ms = (stats_guard.avg_processing_time_ms
                            + result.processing_time_ms as f64)
                            / 2.0;

                        stats_guard.last_updated = chrono::Utc::now();
                    }
                } else {
                    // No operations pending, sleep briefly
                    tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
                }
            }
        })
    }

    /// Start cleanup task
    async fn start_cleanup_task(&self) -> tokio::task::JoinHandle<()> {
        let completed_ops = Arc::clone(&self.completed_operations);
        let config = self.config.clone();

        tokio::spawn(async move {
            let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(
                config.cleanup_interval_secs,
            ));

            loop {
                interval.tick().await;

                // Cleanup old completed operations
                {
                    let mut completed = completed_ops.write().await;
                    while completed.len() > config.max_operation_log {
                        completed.pop_front();
                    }
                }
            }
        })
    }

    /// Process a single operation (static method for background task)
    async fn process_operation(
        update: &IndexUpdate,
        index_state: &Arc<RwLock<IndexState>>,
        conflict_resolver: &Arc<ConflictResolver>,
        _config: &IndexManagerConfig,
    ) -> UpdateResult {
        let start_time = std::time::Instant::now();
        let mut conflicts = Vec::new();
        let mut items_affected = 0;
        let mut operations_completed = Vec::new();

        let success = match &update.operation {
            IndexOperation::Add {
                document,
                chunks,
                embeddings,
            } => {
                match Self::process_add_operation(document, chunks, embeddings, index_state).await {
                    Ok(count) => {
                        items_affected = count;
                        operations_completed.push("add".to_string());
                        true
                    }
                    Err(_) => false,
                }
            }

            IndexOperation::Update {
                document_id,
                document,
                chunks,
                embeddings,
                change_result,
            } => {
                match Self::process_update_operation(
                    document_id,
                    document,
                    chunks,
                    embeddings,
                    change_result,
                    index_state,
                    conflict_resolver,
                )
                .await
                {
                    Ok((count, detected_conflicts)) => {
                        items_affected = count;
                        conflicts = detected_conflicts;
                        operations_completed.push("update".to_string());
                        true
                    }
                    Err(_) => false,
                }
            }

            IndexOperation::Delete { document_id } => {
                match Self::process_delete_operation(document_id, index_state).await {
                    Ok(count) => {
                        items_affected = count;
                        operations_completed.push("delete".to_string());
                        true
                    }
                    Err(_) => false,
                }
            }

            IndexOperation::UpdateEmbeddings {
                document_id,
                embeddings,
            } => match Self::process_embedding_update(document_id, embeddings, index_state).await {
                Ok(count) => {
                    items_affected = count;
                    operations_completed.push("update_embeddings".to_string());
                    true
                }
                Err(_) => false,
            },

            IndexOperation::UpdateChunks {
                document_id,
                chunks,
            } => match Self::process_chunk_update(document_id, chunks, index_state).await {
                Ok(count) => {
                    items_affected = count;
                    operations_completed.push("update_chunks".to_string());
                    true
                }
                Err(_) => false,
            },

            IndexOperation::Batch { operations } => {
                operations_completed.push("batch".to_string());
                items_affected = operations.len();
                true // Simplified for batch operations
            }

            IndexOperation::Rebuild {
                index_name: _,
                document_ids,
            } => {
                operations_completed.push("rebuild".to_string());
                items_affected = document_ids.len();
                true // Simplified for rebuild operations
            }
        };

        UpdateResult {
            operation_id: update.operation_id.clone(),
            success,
            operations_completed,
            conflicts,
            processing_time_ms: start_time.elapsed().as_millis() as u64,
            items_affected,
            error: if success {
                None
            } else {
                Some("Operation failed".to_string())
            },
            metadata: HashMap::new(),
        }
    }

    /// Process add operation
    async fn process_add_operation(
        document: &Document,
        chunks: &[DocumentChunk],
        embeddings: &[Embedding],
        index_state: &Arc<RwLock<IndexState>>,
    ) -> RragResult<usize> {
        let mut state = index_state.write().await;

        // Add document to index
        state.indexed_documents.insert(document.id.clone());
        state.document_versions.insert(document.id.clone(), 1);
        state.last_updated = chrono::Utc::now();

        Ok(1 + chunks.len() + embeddings.len())
    }

    /// Process update operation
    async fn process_update_operation(
        document_id: &str,
        document: &Document,
        chunks: &[DocumentChunk],
        embeddings: &[Embedding],
        change_result: &ChangeResult,
        index_state: &Arc<RwLock<IndexState>>,
        _conflict_resolver: &Arc<ConflictResolver>,
    ) -> RragResult<(usize, Vec<ConflictInfo>)> {
        let mut state = index_state.write().await;
        let conflicts = Vec::new();

        // Check for conflicts
        if let Some(_current_version) = state.document_versions.get(document_id) {
            // Simple conflict detection - in production, would be more sophisticated
            if change_result.change_type == ChangeType::NoChange {
                // No actual conflict, but could indicate race condition
            }
        }

        // Update document in index
        state.indexed_documents.insert(document.id.clone());
        let new_version = state.document_versions.get(document_id).unwrap_or(&0) + 1;
        state
            .document_versions
            .insert(document_id.to_string(), new_version);
        state.last_updated = chrono::Utc::now();

        Ok((1 + chunks.len() + embeddings.len(), conflicts))
    }

    /// Process delete operation
    async fn process_delete_operation(
        document_id: &str,
        index_state: &Arc<RwLock<IndexState>>,
    ) -> RragResult<usize> {
        let mut state = index_state.write().await;

        let was_present = state.indexed_documents.remove(document_id);
        state.document_versions.remove(document_id);
        state.last_updated = chrono::Utc::now();

        Ok(if was_present { 1 } else { 0 })
    }

    /// Process embedding update
    async fn process_embedding_update(
        _document_id: &str,
        embeddings: &[Embedding],
        index_state: &Arc<RwLock<IndexState>>,
    ) -> RragResult<usize> {
        let mut state = index_state.write().await;
        state.last_updated = chrono::Utc::now();
        Ok(embeddings.len())
    }

    /// Process chunk update
    async fn process_chunk_update(
        _document_id: &str,
        chunks: &[DocumentChunk],
        index_state: &Arc<RwLock<IndexState>>,
    ) -> RragResult<usize> {
        let mut state = index_state.write().await;
        state.last_updated = chrono::Utc::now();
        Ok(chunks.len())
    }

    /// Validate an update operation
    async fn validate_update(&self, update: &IndexUpdate) -> RragResult<()> {
        // Basic validation
        if update.operation_id.is_empty() {
            return Err(RragError::validation("operation_id", "non-empty", "empty"));
        }

        if update.priority > 10 {
            return Err(RragError::validation(
                "priority",
                "0-10",
                &update.priority.to_string(),
            ));
        }

        // Validate operation-specific requirements
        match &update.operation {
            IndexOperation::Add { document, .. } => {
                if document.id.is_empty() {
                    return Err(RragError::validation("document.id", "non-empty", "empty"));
                }
            }
            IndexOperation::Update { document_id, .. } => {
                if document_id.is_empty() {
                    return Err(RragError::validation("document_id", "non-empty", "empty"));
                }
            }
            IndexOperation::Delete { document_id } => {
                if document_id.is_empty() {
                    return Err(RragError::validation("document_id", "non-empty", "empty"));
                }
            }
            _ => {} // Other validations as needed
        }

        Ok(())
    }
}

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

    #[tokio::test]
    async fn test_index_manager_creation() {
        let config = IndexManagerConfig::default();
        let manager = IncrementalIndexManager::new(config).await.unwrap();
        assert!(manager.health_check().await.unwrap());
    }

    #[tokio::test]
    async fn test_submit_add_operation() {
        let manager = IncrementalIndexManager::new(IndexManagerConfig::default())
            .await
            .unwrap();

        let doc = Document::new("Test content");
        let operation = IndexOperation::Add {
            document: doc.clone(),
            chunks: Vec::new(),
            embeddings: Vec::new(),
        };

        let update = IndexUpdate {
            operation_id: Uuid::new_v4().to_string(),
            operation,
            priority: 5,
            timestamp: chrono::Utc::now(),
            source: "test".to_string(),
            metadata: HashMap::new(),
            dependencies: Vec::new(),
            max_retries: 3,
            retry_count: 0,
        };

        let op_id = manager.submit_update(update).await.unwrap();
        assert!(!op_id.is_empty());

        // Check that operation was queued
        let status = manager.get_operation_status(&op_id).await.unwrap();
        assert!(status.is_some());
    }

    #[tokio::test]
    async fn test_batch_operations() {
        let manager = IncrementalIndexManager::new(IndexManagerConfig::default())
            .await
            .unwrap();

        let mut operations = Vec::new();
        for i in 0..3 {
            let doc = Document::new(format!("Test content {}", i));
            let operation = IndexOperation::Add {
                document: doc,
                chunks: Vec::new(),
                embeddings: Vec::new(),
            };

            let update = IndexUpdate {
                operation_id: Uuid::new_v4().to_string(),
                operation,
                priority: 5,
                timestamp: chrono::Utc::now(),
                source: "test".to_string(),
                metadata: HashMap::new(),
                dependencies: Vec::new(),
                max_retries: 3,
                retry_count: 0,
            };

            operations.push(update);
        }

        let op_ids = manager.submit_batch(operations).await.unwrap();
        assert_eq!(op_ids.len(), 4); // 3 individual + 1 batch operation
    }

    #[tokio::test]
    async fn test_operation_cancellation() {
        let manager = IncrementalIndexManager::new(IndexManagerConfig::default())
            .await
            .unwrap();

        let doc = Document::new("Test content");
        let operation = IndexOperation::Add {
            document: doc,
            chunks: Vec::new(),
            embeddings: Vec::new(),
        };

        let update = IndexUpdate {
            operation_id: Uuid::new_v4().to_string(),
            operation,
            priority: 5,
            timestamp: chrono::Utc::now(),
            source: "test".to_string(),
            metadata: HashMap::new(),
            dependencies: Vec::new(),
            max_retries: 3,
            retry_count: 0,
        };

        let op_id = manager.submit_update(update).await.unwrap();

        // Try to cancel the operation
        let cancelled = manager.cancel_operation(&op_id).await.unwrap();
        assert!(cancelled);
    }

    #[test]
    fn test_conflict_resolution_strategies() {
        let strategies = vec![
            ConflictResolutionStrategy::LastWriteWins,
            ConflictResolutionStrategy::FirstWriteWins,
            ConflictResolutionStrategy::Merge,
            ConflictResolutionStrategy::Manual,
            ConflictResolutionStrategy::Timestamp,
            ConflictResolutionStrategy::Custom("custom_logic".to_string()),
        ];

        // Ensure all strategies are different
        for (i, strategy1) in strategies.iter().enumerate() {
            for (j, strategy2) in strategies.iter().enumerate() {
                if i != j {
                    assert_ne!(format!("{:?}", strategy1), format!("{:?}", strategy2));
                }
            }
        }
    }
}