oxirs-core 0.2.4

Core RDF and SPARQL functionality for OxiRS - native Rust implementation with zero dependencies
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
//! Multi-Version Concurrency Control (MVCC) for RDF storage
//!
//! This module implements MVCC to enable high-concurrency read/write operations
//! by maintaining multiple timestamped versions of RDF triples.

use crate::model::{Object, Predicate, Subject, Triple};
use anyhow::{anyhow, Result};
use dashmap::DashMap;
use parking_lot::{Mutex, RwLock};
use std::collections::{BTreeMap, HashMap, HashSet};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

/// Transaction timestamp (logical timestamp)
pub type Timestamp = u64;

/// Transaction ID
pub type TransactionId = u64;

/// Version identifier
pub type VersionId = u64;

/// MVCC configuration
#[derive(Debug, Clone)]
pub struct MvccConfig {
    /// Maximum number of versions to keep per triple
    pub max_versions_per_triple: usize,

    /// Garbage collection interval
    pub gc_interval: Duration,

    /// Minimum age for version cleanup (prevents cleaning active versions)
    pub min_version_age: Duration,

    /// Enable snapshot isolation
    pub enable_snapshot_isolation: bool,

    /// Enable read-your-writes consistency
    pub enable_read_your_writes: bool,

    /// Conflict detection strategy
    pub conflict_detection: ConflictDetection,
}

impl Default for MvccConfig {
    fn default() -> Self {
        Self {
            max_versions_per_triple: 100,
            gc_interval: Duration::from_secs(60),
            min_version_age: Duration::from_secs(300), // 5 minutes
            enable_snapshot_isolation: true,
            enable_read_your_writes: true,
            conflict_detection: ConflictDetection::Optimistic,
        }
    }
}

/// Conflict detection strategy
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConflictDetection {
    /// Optimistic concurrency control
    Optimistic,
    /// Optimistic two-phase locking
    OptimisticTwoPhase,
    /// Pessimistic locking
    Pessimistic,
    /// Multi-version timestamp ordering
    TimestampOrdering,
}

/// MVCC store for RDF triples
pub struct MvccStore {
    /// Configuration
    config: MvccConfig,

    /// Version storage (triple hash -> versions)
    versions: Arc<DashMap<TripleKey, VersionChain>>,

    /// Active transactions
    transactions: Arc<DashMap<TransactionId, TransactionState>>,

    /// Global timestamp counter
    timestamp_counter: Arc<AtomicU64>,

    /// Transaction ID counter
    transaction_counter: Arc<AtomicU64>,

    /// Snapshot registry
    snapshots: Arc<RwLock<BTreeMap<Timestamp, SnapshotInfo>>>,

    /// Garbage collection state
    gc_state: Arc<Mutex<GarbageCollectionState>>,

    /// Index for efficient queries
    indexes: Arc<MvccIndexes>,
}

/// Key for identifying a triple
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct TripleKey {
    subject: String,
    predicate: String,
    object: String,
}

impl TripleKey {
    fn from_triple(triple: &Triple) -> Self {
        Self {
            subject: triple.subject().to_string(),
            predicate: triple.predicate().to_string(),
            object: triple.object().to_string(),
        }
    }
}

/// Version chain for a triple
#[derive(Debug, Clone)]
pub struct VersionChain {
    /// Versions sorted by timestamp (newest first)
    versions: Vec<Version>,
}

impl VersionChain {
    fn new() -> Self {
        Self {
            versions: Vec::new(),
        }
    }

    /// Add a new version
    fn add_version(&mut self, version: Version) {
        // Insert in sorted order (newest first)
        let pos = self
            .versions
            .binary_search_by_key(&std::cmp::Reverse(version.timestamp), |v| {
                std::cmp::Reverse(v.timestamp)
            })
            .unwrap_or_else(|pos| pos);
        self.versions.insert(pos, version);
    }

    /// Get visible version for a timestamp
    fn get_visible_version(&self, timestamp: Timestamp) -> Option<&Version> {
        self.versions
            .iter()
            .find(|v| v.timestamp <= timestamp && v.is_visible_at(timestamp))
    }

    /// Garbage collect old versions
    fn gc_versions(&mut self, min_timestamp: Timestamp, max_versions: usize) {
        // Keep at least one version
        if self.versions.len() <= 1 {
            return;
        }

        // Remove old deleted versions
        self.versions
            .retain(|v| !(v.deleted && v.timestamp < min_timestamp));

        // Limit number of versions
        if self.versions.len() > max_versions {
            self.versions.truncate(max_versions);
        }
    }
}

/// A version of a triple
#[derive(Debug, Clone)]
pub struct Version {
    /// Version ID
    pub id: VersionId,

    /// Creation timestamp
    pub timestamp: Timestamp,

    /// Transaction that created this version
    pub transaction_id: TransactionId,

    /// Whether this version represents a deletion
    pub deleted: bool,

    /// The triple data (None if deleted)
    pub triple: Option<Triple>,

    /// Commit timestamp (None if not yet committed)
    pub commit_timestamp: Option<Timestamp>,
}

impl Version {
    /// Check if this version is visible at a given timestamp
    fn is_visible_at(&self, timestamp: Timestamp) -> bool {
        if let Some(commit_ts) = self.commit_timestamp {
            commit_ts <= timestamp
        } else {
            false // Uncommitted versions are not visible
        }
    }
}

/// Transaction state
#[derive(Debug, Clone)]
pub struct TransactionState {
    /// Transaction ID
    pub id: TransactionId,

    /// Start timestamp
    pub start_timestamp: Timestamp,

    /// Commit timestamp (if committed)
    pub commit_timestamp: Option<Timestamp>,

    /// Transaction status
    pub status: TransactionStatus,

    /// Read set (for conflict detection)
    pub read_set: HashSet<TripleKey>,

    /// Write set
    pub write_set: HashMap<TripleKey, WriteOperation>,

    /// Isolation level
    pub isolation_level: IsolationLevel,
}

/// Transaction status
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransactionStatus {
    Active,
    Preparing,
    Committed,
    Aborted,
}

/// Write operation type
#[derive(Debug, Clone)]
pub enum WriteOperation {
    Insert(Triple),
    Delete,
}

/// Isolation level
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IsolationLevel {
    /// Read uncommitted (dirty reads allowed)
    ReadUncommitted,
    /// Read committed (no dirty reads)
    ReadCommitted,
    /// Repeatable read (no dirty or non-repeatable reads)
    RepeatableRead,
    /// Serializable (full isolation)
    Serializable,
    /// Snapshot isolation
    Snapshot,
    /// Snapshot isolation (alias for Snapshot)
    SnapshotIsolation,
}

/// Snapshot information
#[derive(Debug, Clone)]
pub struct SnapshotInfo {
    /// Snapshot timestamp
    pub timestamp: Timestamp,

    /// Active transactions at snapshot time
    pub active_transactions: HashSet<TransactionId>,

    /// Reference count
    pub ref_count: usize,
}

/// Garbage collection state
#[derive(Debug)]
pub struct GarbageCollectionState {
    /// Last GC run time
    last_gc: Instant,

    /// Number of versions collected
    versions_collected: u64,

    /// Number of GC runs
    gc_runs: u64,
}

/// MVCC indexes for efficient queries
pub struct MvccIndexes {
    /// Subject index
    subject_index: DashMap<String, HashSet<TripleKey>>,

    /// Predicate index
    predicate_index: DashMap<String, HashSet<TripleKey>>,

    /// Object index
    object_index: DashMap<String, HashSet<TripleKey>>,
}

impl MvccStore {
    /// Create a new MVCC store
    pub fn new(config: MvccConfig) -> Self {
        Self {
            config,
            versions: Arc::new(DashMap::new()),
            transactions: Arc::new(DashMap::new()),
            timestamp_counter: Arc::new(AtomicU64::new(1)),
            transaction_counter: Arc::new(AtomicU64::new(1)),
            snapshots: Arc::new(RwLock::new(BTreeMap::new())),
            gc_state: Arc::new(Mutex::new(GarbageCollectionState {
                last_gc: Instant::now(),
                versions_collected: 0,
                gc_runs: 0,
            })),
            indexes: Arc::new(MvccIndexes {
                subject_index: DashMap::new(),
                predicate_index: DashMap::new(),
                object_index: DashMap::new(),
            }),
        }
    }

    /// Begin a new transaction
    pub fn begin_transaction(&self, isolation_level: IsolationLevel) -> Result<TransactionId> {
        let tx_id = self.transaction_counter.fetch_add(1, Ordering::SeqCst);
        let start_timestamp = self.get_next_timestamp();

        let tx_state = TransactionState {
            id: tx_id,
            start_timestamp,
            commit_timestamp: None,
            status: TransactionStatus::Active,
            read_set: HashSet::new(),
            write_set: HashMap::new(),
            isolation_level,
        };

        self.transactions.insert(tx_id, tx_state);

        // Create snapshot if needed
        if isolation_level == IsolationLevel::Snapshot {
            self.create_snapshot(start_timestamp)?;
        }

        Ok(tx_id)
    }

    /// Insert a triple in a transaction
    pub fn insert(&self, tx_id: TransactionId, triple: Triple) -> Result<()> {
        let mut tx = self.get_active_transaction(tx_id)?;

        let key = TripleKey::from_triple(&triple);

        // Check for write-write conflicts
        if self.config.conflict_detection == ConflictDetection::Pessimistic {
            self.check_write_conflict(&key, tx_id)?;
        }

        // Add to write set
        tx.write_set
            .insert(key.clone(), WriteOperation::Insert(triple.clone()));

        // Update indexes
        self.update_indexes_for_insert(&key);

        Ok(())
    }

    /// Delete a triple in a transaction
    pub fn delete(&self, tx_id: TransactionId, triple: &Triple) -> Result<()> {
        let mut tx = self.get_active_transaction(tx_id)?;

        let key = TripleKey::from_triple(triple);

        // Check if triple exists
        if !self.exists_at_timestamp(&key, tx.start_timestamp)? {
            return Err(anyhow!("Triple does not exist"));
        }

        // Add to write set
        tx.write_set.insert(key.clone(), WriteOperation::Delete);

        // Update indexes
        self.update_indexes_for_delete(&key);

        Ok(())
    }

    /// Query triples at transaction's snapshot
    pub fn query(
        &self,
        tx_id: TransactionId,
        subject: Option<&Subject>,
        predicate: Option<&Predicate>,
        object: Option<&Object>,
    ) -> Result<Vec<Triple>> {
        let mut tx = self.get_active_transaction(tx_id)?;
        let timestamp = tx.start_timestamp;

        // Get candidate keys from indexes
        let candidates = self.get_candidate_keys(subject, predicate, object);

        let mut results = Vec::new();
        let mut processed_keys = HashSet::new();

        for key in candidates {
            processed_keys.insert(key.clone());

            // Add to read set for conflict detection
            tx.read_set.insert(key.clone());

            // Check if visible at timestamp
            if let Some(version_chain) = self.versions.get(&key) {
                if let Some(version) = version_chain.get_visible_version(timestamp) {
                    if !version.deleted {
                        if let Some(triple) = &version.triple {
                            // Apply predicate filters
                            if self.matches_pattern(triple, subject, predicate, object) {
                                results.push(triple.clone());
                            }
                        }
                    }
                }
            }

            // Check uncommitted writes in same transaction
            if self.config.enable_read_your_writes {
                if let Some(write_op) = tx.write_set.get(&key) {
                    match write_op {
                        WriteOperation::Insert(triple) => {
                            if self.matches_pattern(triple, subject, predicate, object) {
                                results.push(triple.clone());
                            }
                        }
                        WriteOperation::Delete => {
                            // Remove from results if it was added
                            results.retain(|t| TripleKey::from_triple(t) != key);
                        }
                    }
                }
            }
        }

        // Also check write set for new keys not in main indexes (read-your-writes)
        if self.config.enable_read_your_writes {
            for (key, write_op) in &tx.write_set {
                if !processed_keys.contains(key) {
                    match write_op {
                        WriteOperation::Insert(triple) => {
                            if self.matches_pattern(triple, subject, predicate, object) {
                                results.push(triple.clone());
                            }
                        }
                        WriteOperation::Delete => {
                            // Already handled above
                        }
                    }
                }
            }
        }

        Ok(results)
    }

    /// Commit a transaction
    pub fn commit_transaction(&self, tx_id: TransactionId) -> Result<()> {
        let mut tx = self.get_active_transaction(tx_id)?;

        // Change status to preparing
        tx.status = TransactionStatus::Preparing;

        // Validate transaction
        self.validate_transaction(&tx)?;

        // Get commit timestamp
        let commit_timestamp = self.get_next_timestamp();

        // Apply writes
        for (key, operation) in &tx.write_set {
            let version = match operation {
                WriteOperation::Insert(triple) => Version {
                    id: self.get_next_timestamp(), // Use timestamp as version ID
                    timestamp: commit_timestamp,
                    transaction_id: tx_id,
                    deleted: false,
                    triple: Some(triple.clone()),
                    commit_timestamp: Some(commit_timestamp),
                },
                WriteOperation::Delete => Version {
                    id: self.get_next_timestamp(),
                    timestamp: commit_timestamp,
                    transaction_id: tx_id,
                    deleted: true,
                    triple: None,
                    commit_timestamp: Some(commit_timestamp),
                },
            };

            self.versions
                .entry(key.clone())
                .or_insert_with(VersionChain::new)
                .add_version(version);
        }

        // Update transaction state
        tx.commit_timestamp = Some(commit_timestamp);
        tx.status = TransactionStatus::Committed;

        // Trigger GC if needed
        self.maybe_run_gc();

        Ok(())
    }

    /// Abort a transaction
    pub fn abort_transaction(&self, tx_id: TransactionId) -> Result<()> {
        if let Some(mut tx) = self.transactions.get_mut(&tx_id) {
            tx.status = TransactionStatus::Aborted;

            // Clean up any locks or resources
            // In optimistic mode, no cleanup needed
        }

        Ok(())
    }

    /// Validate transaction for conflicts
    fn validate_transaction(&self, tx: &TransactionState) -> Result<()> {
        match self.config.conflict_detection {
            ConflictDetection::Optimistic => {
                // Check read set for modifications
                for key in &tx.read_set {
                    if let Some(version_chain) = self.versions.get(key) {
                        if let Some(latest) = version_chain.versions.first() {
                            if latest.timestamp > tx.start_timestamp {
                                return Err(anyhow!("Read conflict detected"));
                            }
                        }
                    }
                }

                // Check write-write conflicts
                for key in tx.write_set.keys() {
                    if let Some(version_chain) = self.versions.get(key) {
                        if let Some(latest) = version_chain.versions.first() {
                            if latest.timestamp > tx.start_timestamp
                                && latest.transaction_id != tx.id
                            {
                                return Err(anyhow!("Write conflict detected"));
                            }
                        }
                    }
                }
            }

            ConflictDetection::Pessimistic => {
                // Conflicts already prevented during operations
            }

            ConflictDetection::TimestampOrdering => {
                // Ensure timestamp ordering is maintained
                for key in tx.write_set.keys() {
                    if let Some(version_chain) = self.versions.get(key) {
                        for version in &version_chain.versions {
                            if version.transaction_id != tx.id
                                && version.timestamp > tx.start_timestamp
                                && version.commit_timestamp.is_some()
                            {
                                return Err(anyhow!("Timestamp ordering violation"));
                            }
                        }
                    }
                }
            }
            ConflictDetection::OptimisticTwoPhase => {
                // Two-phase optimistic validation
                // Phase 1: Read validation (similar to optimistic)
                for key in &tx.read_set {
                    if let Some(version_chain) = self.versions.get(key) {
                        if let Some(latest) = version_chain.versions.first() {
                            if latest.timestamp > tx.start_timestamp {
                                return Err(anyhow!("Read conflict detected in phase 1"));
                            }
                        }
                    }
                }

                // Phase 2: Write validation
                for key in tx.write_set.keys() {
                    if let Some(version_chain) = self.versions.get(key) {
                        for version in &version_chain.versions {
                            if version.transaction_id != tx.id
                                && version.timestamp > tx.start_timestamp
                                && version.commit_timestamp.is_some()
                            {
                                return Err(anyhow!("Write conflict detected in phase 2"));
                            }
                        }
                    }
                }
            }
        }

        Ok(())
    }

    /// Get active transaction
    fn get_active_transaction(
        &self,
        tx_id: TransactionId,
    ) -> Result<dashmap::mapref::one::RefMut<'_, TransactionId, TransactionState>> {
        let tx = self
            .transactions
            .get_mut(&tx_id)
            .ok_or_else(|| anyhow!("Transaction not found"))?;

        if tx.status != TransactionStatus::Active {
            return Err(anyhow!("Transaction is not active"));
        }

        Ok(tx)
    }

    /// Check if triple exists at timestamp
    fn exists_at_timestamp(&self, key: &TripleKey, timestamp: Timestamp) -> Result<bool> {
        if let Some(version_chain) = self.versions.get(key) {
            if let Some(version) = version_chain.get_visible_version(timestamp) {
                return Ok(!version.deleted);
            }
        }
        Ok(false)
    }

    /// Get candidate keys from indexes
    fn get_candidate_keys(
        &self,
        subject: Option<&Subject>,
        predicate: Option<&Predicate>,
        object: Option<&Object>,
    ) -> HashSet<TripleKey> {
        let mut candidates = HashSet::new();

        // Use most selective index
        if let Some(subj) = subject {
            if let Some(keys) = self.indexes.subject_index.get(&subj.to_string()) {
                candidates.extend(keys.iter().cloned());
            }
        } else if let Some(pred) = predicate {
            if let Some(keys) = self.indexes.predicate_index.get(&pred.to_string()) {
                candidates.extend(keys.iter().cloned());
            }
        } else if let Some(obj) = object {
            if let Some(keys) = self.indexes.object_index.get(&obj.to_string()) {
                candidates.extend(keys.iter().cloned());
            }
        } else {
            // Full scan
            for entry in self.versions.iter() {
                candidates.insert(entry.key().clone());
            }
        }

        candidates
    }

    /// Check if triple matches pattern
    fn matches_pattern(
        &self,
        triple: &Triple,
        subject: Option<&Subject>,
        predicate: Option<&Predicate>,
        object: Option<&Object>,
    ) -> bool {
        if let Some(s) = subject {
            if triple.subject() != s {
                return false;
            }
        }
        if let Some(p) = predicate {
            if triple.predicate() != p {
                return false;
            }
        }
        if let Some(o) = object {
            if triple.object() != o {
                return false;
            }
        }
        true
    }

    /// Update indexes for insert
    fn update_indexes_for_insert(&self, key: &TripleKey) {
        self.indexes
            .subject_index
            .entry(key.subject.clone())
            .or_default()
            .insert(key.clone());

        self.indexes
            .predicate_index
            .entry(key.predicate.clone())
            .or_default()
            .insert(key.clone());

        self.indexes
            .object_index
            .entry(key.object.clone())
            .or_default()
            .insert(key.clone());
    }

    /// Update indexes for delete
    fn update_indexes_for_delete(&self, _key: &TripleKey) {
        // Note: We don't actually remove from indexes during transaction
        // This happens during GC when versions are cleaned up
    }

    /// Check for write conflicts (pessimistic mode)
    fn check_write_conflict(&self, _key: &TripleKey, _tx_id: TransactionId) -> Result<()> {
        // In a real implementation, would check locks
        Ok(())
    }

    /// Create a snapshot
    fn create_snapshot(&self, timestamp: Timestamp) -> Result<()> {
        let active_txs: HashSet<TransactionId> = self
            .transactions
            .iter()
            .filter(|entry| entry.value().status == TransactionStatus::Active)
            .map(|entry| *entry.key())
            .collect();

        let snapshot = SnapshotInfo {
            timestamp,
            active_transactions: active_txs,
            ref_count: 1,
        };

        self.snapshots.write().insert(timestamp, snapshot);

        Ok(())
    }

    /// Get current timestamp
    fn get_current_timestamp(&self) -> Timestamp {
        self.timestamp_counter.load(Ordering::SeqCst)
    }

    /// Get next timestamp
    fn get_next_timestamp(&self) -> Timestamp {
        self.timestamp_counter.fetch_add(1, Ordering::SeqCst)
    }

    /// Maybe run garbage collection
    fn maybe_run_gc(&self) {
        let mut gc_state = self.gc_state.lock();

        if gc_state.last_gc.elapsed() >= self.config.gc_interval {
            // Run GC in background
            let versions = self.versions.clone();
            let config = self.config.clone();
            let min_timestamp = self.calculate_min_timestamp();

            std::thread::spawn(move || {
                Self::run_gc_internal(versions, config, min_timestamp);
            });

            gc_state.last_gc = Instant::now();
            gc_state.gc_runs += 1;
        }
    }

    /// Run garbage collection
    fn run_gc_internal(
        versions: Arc<DashMap<TripleKey, VersionChain>>,
        config: MvccConfig,
        min_timestamp: Timestamp,
    ) {
        for mut entry in versions.iter_mut() {
            entry
                .value_mut()
                .gc_versions(min_timestamp, config.max_versions_per_triple);
        }
    }

    /// Calculate minimum timestamp to keep
    fn calculate_min_timestamp(&self) -> Timestamp {
        // Keep versions needed by active transactions
        let min_active = self
            .transactions
            .iter()
            .filter(|entry| entry.value().status == TransactionStatus::Active)
            .map(|entry| entry.value().start_timestamp)
            .min()
            .unwrap_or(self.get_current_timestamp());

        // Keep versions needed by snapshots
        let min_snapshot = self
            .snapshots
            .read()
            .keys()
            .next()
            .copied()
            .unwrap_or(self.get_current_timestamp());

        min_active.min(min_snapshot)
    }

    /// Run garbage collection manually
    pub fn garbage_collect(&self) -> Result<()> {
        let min_timestamp = self.calculate_min_timestamp();
        Self::run_gc_internal(self.versions.clone(), self.config.clone(), min_timestamp);
        Ok(())
    }

    /// Get store statistics
    pub fn get_stats(&self) -> MvccStats {
        let total_versions = self
            .versions
            .iter()
            .map(|entry| entry.value().versions.len())
            .sum();

        let gc_state = self.gc_state.lock();

        MvccStats {
            total_triples: self.versions.len(),
            total_versions,
            active_transactions: self
                .transactions
                .iter()
                .filter(|entry| entry.value().status == TransactionStatus::Active)
                .count(),
            gc_runs: gc_state.gc_runs,
            versions_collected: gc_state.versions_collected,
        }
    }
}

/// MVCC statistics
#[derive(Debug, Clone)]
pub struct MvccStats {
    pub total_triples: usize,
    pub total_versions: usize,
    pub active_transactions: usize,
    pub gc_runs: u64,
    pub versions_collected: u64,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::{Literal, NamedNode};

    #[test]
    fn test_basic_mvcc_operations() {
        let config = MvccConfig::default();
        let store = MvccStore::new(config);

        // Begin transaction
        let tx1 = store
            .begin_transaction(IsolationLevel::Snapshot)
            .expect("store operation should succeed");

        // Insert triple
        let triple = Triple::new(
            NamedNode::new("http://example.org/s").expect("valid IRI"),
            NamedNode::new("http://example.org/p").expect("valid IRI"),
            Literal::new("value"),
        );

        store
            .insert(tx1, triple.clone())
            .expect("MVCC insert should succeed");

        // Query should see the triple (read-your-writes)
        let results = store
            .query(tx1, None, None, None)
            .expect("store operation should succeed");
        assert_eq!(results.len(), 1);

        // Commit transaction
        store
            .commit_transaction(tx1)
            .expect("store operation should succeed");

        // New transaction should see committed data
        let tx2 = store
            .begin_transaction(IsolationLevel::Snapshot)
            .expect("store operation should succeed");
        let results = store
            .query(tx2, None, None, None)
            .expect("store operation should succeed");
        assert_eq!(results.len(), 1);
    }

    #[test]
    fn test_concurrent_transactions() {
        let config = MvccConfig::default();
        let store = MvccStore::new(config);

        // Insert initial data
        let tx0 = store
            .begin_transaction(IsolationLevel::Snapshot)
            .expect("store operation should succeed");
        let triple = Triple::new(
            NamedNode::new("http://example.org/s").expect("valid IRI"),
            NamedNode::new("http://example.org/p").expect("valid IRI"),
            Literal::new("initial"),
        );
        store
            .insert(tx0, triple.clone())
            .expect("MVCC insert should succeed");
        store
            .commit_transaction(tx0)
            .expect("store operation should succeed");

        // Start two concurrent transactions
        let tx1 = store
            .begin_transaction(IsolationLevel::Snapshot)
            .expect("store operation should succeed");
        let tx2 = store
            .begin_transaction(IsolationLevel::Snapshot)
            .expect("store operation should succeed");

        // Both should see initial data
        assert_eq!(
            store
                .query(tx1, None, None, None)
                .expect("store operation should succeed")
                .len(),
            1
        );
        assert_eq!(
            store
                .query(tx2, None, None, None)
                .expect("store operation should succeed")
                .len(),
            1
        );

        // TX1 modifies data
        store
            .delete(tx1, &triple)
            .expect("store operation should succeed");
        let new_triple = Triple::new(
            NamedNode::new("http://example.org/s").expect("valid IRI"),
            NamedNode::new("http://example.org/p").expect("valid IRI"),
            Literal::new("modified"),
        );
        store
            .insert(tx1, new_triple)
            .expect("store operation should succeed");

        // TX2 shouldn't see TX1's changes yet
        let tx2_results = store
            .query(tx2, None, None, None)
            .expect("store operation should succeed");
        assert_eq!(tx2_results.len(), 1);
        assert_eq!(tx2_results[0].object().to_string(), "\"initial\"");

        // Commit TX1
        store
            .commit_transaction(tx1)
            .expect("store operation should succeed");

        // TX2 still sees snapshot
        let tx2_results = store
            .query(tx2, None, None, None)
            .expect("store operation should succeed");
        assert_eq!(tx2_results.len(), 1);
        assert_eq!(tx2_results[0].object().to_string(), "\"initial\"");

        // New transaction sees committed changes
        let tx3 = store
            .begin_transaction(IsolationLevel::Snapshot)
            .expect("store operation should succeed");
        let tx3_results = store
            .query(tx3, None, None, None)
            .expect("store operation should succeed");
        assert_eq!(tx3_results.len(), 1);
        assert_eq!(tx3_results[0].object().to_string(), "\"modified\"");
    }

    #[test]
    fn test_write_conflict_detection() {
        let config = MvccConfig {
            conflict_detection: ConflictDetection::Optimistic,
            ..Default::default()
        };
        let store = MvccStore::new(config);

        // Insert initial data
        let tx0 = store
            .begin_transaction(IsolationLevel::Snapshot)
            .expect("store operation should succeed");
        let triple = Triple::new(
            NamedNode::new("http://example.org/s").expect("valid IRI"),
            NamedNode::new("http://example.org/p").expect("valid IRI"),
            Literal::new("initial"),
        );
        store
            .insert(tx0, triple.clone())
            .expect("MVCC insert should succeed");
        store
            .commit_transaction(tx0)
            .expect("store operation should succeed");

        // Start two transactions
        let tx1 = store
            .begin_transaction(IsolationLevel::Snapshot)
            .expect("store operation should succeed");
        let tx2 = store
            .begin_transaction(IsolationLevel::Snapshot)
            .expect("store operation should succeed");

        // Both modify the same triple
        store
            .delete(tx1, &triple)
            .expect("store operation should succeed");
        store
            .delete(tx2, &triple)
            .expect("store operation should succeed");

        // First commit succeeds
        assert!(store.commit_transaction(tx1).is_ok());

        // Second commit should fail due to conflict
        assert!(store.commit_transaction(tx2).is_err());
    }

    #[test]
    fn test_version_chain() {
        let mut chain = VersionChain::new();

        // Add versions
        for i in 0..5 {
            let version = Version {
                id: i,
                timestamp: i * 10,
                transaction_id: i,
                deleted: false,
                triple: None,
                commit_timestamp: Some(i * 10 + 5),
            };
            chain.add_version(version);
        }

        // Test visibility
        assert_eq!(
            chain
                .get_visible_version(25)
                .expect("operation should succeed")
                .id,
            2
        );
        assert_eq!(
            chain
                .get_visible_version(45)
                .expect("operation should succeed")
                .id,
            4
        );

        // Test GC
        chain.gc_versions(20, 3);
        assert!(chain.versions.len() <= 3);
    }
}