velocidb 0.1.0

A high-performance SQLite reimplementation in Rust optimized for modern hardware
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
// Multi-Version Concurrency Control (MVCC) implementation
// Enables non-blocking reads and concurrent writes through snapshot isolation

use crate::types::{Result, TransactionId, Value, VelociError};
use parking_lot::RwLock;
use std::collections::{BTreeMap, HashMap};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;

/// Global transaction ID counter
/// Uses SeqCst ordering to ensure all threads see a consistent view
static GLOBAL_TXN_ID: AtomicU64 = AtomicU64::new(1);

/// Timestamp for version visibility
pub type Timestamp = u64;

/// Version visibility information
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct VersionInfo {
    /// Transaction ID that created this version
    pub xmin: TransactionId,
    /// Transaction ID that deleted/updated this version (0 if still active)
    pub xmax: TransactionId,
    /// Creation timestamp
    pub created_at: Timestamp,
    /// Deletion/update timestamp (0 if still active)
    pub deleted_at: Timestamp,
}

impl VersionInfo {
    pub fn new(xmin: TransactionId, created_at: Timestamp) -> Self {
        Self {
            xmin,
            xmax: 0,
            created_at,
            deleted_at: 0,
        }
    }

    /// Check if this version is visible to a transaction with the given snapshot
    pub fn is_visible(&self, snapshot: &Snapshot) -> bool {
        // Version must be created before our snapshot
        if self.created_at > snapshot.timestamp {
            return false;
        }

        // Version must not be deleted before our snapshot
        if self.deleted_at > 0 && self.deleted_at <= snapshot.timestamp {
            return false;
        }

        // Check transaction visibility
        if !snapshot.is_transaction_visible(self.xmin) {
            return false;
        }

        // If version is deleted, check if deleting transaction is visible
        if self.xmax > 0 && snapshot.is_transaction_visible(self.xmax) {
            return false;
        }

        true
    }

    /// Mark this version as deleted by a transaction
    pub fn mark_deleted(&mut self, xmax: TransactionId, deleted_at: Timestamp) {
        self.xmax = xmax;
        self.deleted_at = deleted_at;
    }
}

/// A versioned record containing multiple versions of the same logical row
#[derive(Debug, Clone)]
pub struct VersionedRecord {
    /// Primary key of the record
    pub key: i64,
    /// List of versions ordered by timestamp (oldest to newest)
    pub versions: Vec<RecordVersion>,
}

/// A single version of a record
#[derive(Debug, Clone)]
pub struct RecordVersion {
    /// Version metadata
    pub version_info: VersionInfo,
    /// Actual data values
    pub data: Vec<Value>,
}

impl RecordVersion {
    pub fn new(xmin: TransactionId, created_at: Timestamp, data: Vec<Value>) -> Self {
        Self {
            version_info: VersionInfo::new(xmin, created_at),
            data,
        }
    }

    /// Check if this version is visible to the given snapshot
    pub fn is_visible(&self, snapshot: &Snapshot) -> bool {
        self.version_info.is_visible(snapshot)
    }
}

impl VersionedRecord {
    pub fn new(key: i64) -> Self {
        Self {
            key,
            versions: Vec::new(),
        }
    }

    /// Add a new version to this record
    pub fn add_version(&mut self, version: RecordVersion) {
        self.versions.push(version);
    }

    /// Get the visible version for the given snapshot
    pub fn get_visible_version(&self, snapshot: &Snapshot) -> Option<&RecordVersion> {
        // Iterate in reverse order to find the most recent visible version
        self.versions
            .iter()
            .rev()
            .find(|v| v.is_visible(snapshot))
    }

    /// Mark the latest version as deleted
    pub fn mark_deleted(&mut self, xmax: TransactionId, deleted_at: Timestamp) -> Result<()> {
        if let Some(version) = self.versions.last_mut() {
            version.version_info.mark_deleted(xmax, deleted_at);
            Ok(())
        } else {
            Err(VelociError::NotFound("No versions to delete".to_string()))
        }
    }

    /// Remove versions that are no longer needed (garbage collection)
    pub fn vacuum(&mut self, min_active_timestamp: Timestamp) {
        // Keep only versions that might still be visible to active transactions
        self.versions.retain(|v| {
            // Keep if not deleted, or deleted recently
            v.version_info.deleted_at == 0 || v.version_info.deleted_at >= min_active_timestamp
        });

        // Always keep at least one version
        if self.versions.is_empty() && !self.versions.is_empty() {
            // This shouldn't happen, but safeguard
            self.versions.truncate(1);
        }
    }
}

/// Snapshot isolation state for a transaction
#[derive(Debug, Clone)]
pub struct Snapshot {
    /// Timestamp when this snapshot was taken
    pub timestamp: Timestamp,
    /// Set of active transaction IDs at snapshot time
    pub active_transactions: Vec<TransactionId>,
    /// Transaction ID of this snapshot's transaction
    pub txn_id: TransactionId,
}

impl Snapshot {
    pub fn new(timestamp: Timestamp, txn_id: TransactionId, active_transactions: Vec<TransactionId>) -> Self {
        Self {
            timestamp,
            txn_id,
            active_transactions,
        }
    }

    /// Check if a transaction is visible to this snapshot
    pub fn is_transaction_visible(&self, txn_id: TransactionId) -> bool {
        // Our own changes are always visible
        if txn_id == self.txn_id {
            return true;
        }

        // Transaction must have started before our snapshot
        if txn_id >= self.txn_id {
            return false;
        }

        // Transaction must not be in our active set (must be committed)
        !self.active_transactions.contains(&txn_id)
    }
}

/// MVCC Manager coordinates snapshot isolation and version management
///
/// LOCK ORDERING (STRICT):
/// 1. active_snapshots (Level 1 - Transaction metadata)
/// 2. committed_transactions (Level 2 - Transaction history)
/// 3. version_store (Level 3 - Data)
///
/// NEVER acquire in reverse order to prevent deadlocks
///
/// IMPROVEMENTS:
/// - Use DashMap for version_store to enable per-table concurrency
/// - Atomic timestamp management
/// - Consistent lock ordering enforced throughout
pub struct MvccManager {
    /// Current timestamp (monotonically increasing)
    current_timestamp: AtomicU64,
    /// Active transactions and their snapshots
    /// LOCK LEVEL 1: Acquire this first if needed with other locks
    active_snapshots: Arc<RwLock<HashMap<TransactionId, Snapshot>>>,
    /// Version store: maps table_name -> key -> versioned record
    /// LOCK LEVEL 3: Acquire this last, per-table locking via DashMap
    version_store: Arc<dashmap::DashMap<String, Arc<RwLock<BTreeMap<i64, VersionedRecord>>>>>,
    /// Committed transaction timestamps for visibility
    /// LOCK LEVEL 2: Acquire after active_snapshots, before version_store
    committed_transactions: Arc<RwLock<BTreeMap<TransactionId, Timestamp>>>,
}

impl MvccManager {
    pub fn new() -> Self {
        Self {
            current_timestamp: AtomicU64::new(1),
            active_snapshots: Arc::new(RwLock::new(HashMap::new())),
            version_store: Arc::new(dashmap::DashMap::new()),
            committed_transactions: Arc::new(RwLock::new(BTreeMap::new())),
        }
    }

    /// Begin a new transaction and create a snapshot
    pub fn begin_transaction(&self) -> Snapshot {
        let txn_id = GLOBAL_TXN_ID.fetch_add(1, Ordering::SeqCst);
        let timestamp = self.current_timestamp.fetch_add(1, Ordering::SeqCst);

        // LOCK ORDERING: Only acquire active_snapshots (Level 1)
        // Get list of currently active transactions
        let snapshot = {
            let snapshots = self.active_snapshots.read();
            let active_transactions: Vec<TransactionId> = snapshots.keys().copied().collect();
            Snapshot::new(timestamp, txn_id, active_transactions)
        }; // Release read lock

        // Register this snapshot as active
        self.active_snapshots.write().insert(txn_id, snapshot.clone());

        snapshot
    }

    /// Commit a transaction
    pub fn commit_transaction(&self, snapshot: &Snapshot) -> Result<()> {
        let commit_timestamp = self.current_timestamp.fetch_add(1, Ordering::SeqCst);

        // LOCK ORDERING: Level 1 (active_snapshots) → Level 2 (committed_transactions)
        // This is consistent and safe
        
        // First update committed_transactions (Level 2)
        self.committed_transactions
            .write()
            .insert(snapshot.txn_id, commit_timestamp);

        // Then update active_snapshots (Level 1)
        self.active_snapshots.write().remove(&snapshot.txn_id);

        Ok(())
    }

    /// Abort a transaction
    pub fn abort_transaction(&self, snapshot: &Snapshot) -> Result<()> {
        // LOCK ORDERING: Only acquire active_snapshots (Level 1)
        self.active_snapshots.write().remove(&snapshot.txn_id);

        // TODO: Rollback any changes made by this transaction
        // This requires tracking undo information

        Ok(())
    }

    /// Insert a new version of a record
    pub fn insert_version(
        &self,
        table_name: &str,
        key: i64,
        data: Vec<Value>,
        snapshot: &Snapshot,
    ) -> Result<()> {
        let created_at = self.current_timestamp.load(Ordering::SeqCst);
        let version = RecordVersion::new(snapshot.txn_id, created_at, data);

        // LOCK ORDERING: Only acquire version_store (Level 3 - Data)
        // Per-table locking via DashMap reduces contention
        let table_store = self.version_store
            .entry(table_name.to_string())
            .or_insert_with(|| Arc::new(RwLock::new(BTreeMap::new())));
        
        let mut table_map = table_store.write();
        let record = table_map.entry(key).or_insert_with(|| VersionedRecord::new(key));
        record.add_version(version);

        Ok(())
    }

    /// Read a record with snapshot isolation
    pub fn read_version(
        &self,
        table_name: &str,
        key: i64,
        snapshot: &Snapshot,
    ) -> Result<Option<Vec<Value>>> {
        // LOCK ORDERING: Only acquire version_store (Level 3)
        if let Some(table_store) = self.version_store.get(table_name) {
            let table_map = table_store.read();
            if let Some(record) = table_map.get(&key) {
                if let Some(version) = record.get_visible_version(snapshot) {
                    return Ok(Some(version.data.clone()));
                }
            }
        }

        Ok(None)
    }

    /// Delete a record (creates a new version marked as deleted)
    pub fn delete_version(
        &self,
        table_name: &str,
        key: i64,
        snapshot: &Snapshot,
    ) -> Result<()> {
        let deleted_at = self.current_timestamp.fetch_add(1, Ordering::SeqCst);

        // LOCK ORDERING: Only acquire version_store (Level 3)
        if let Some(table_store) = self.version_store.get(table_name) {
            let mut table_map = table_store.write();
            if let Some(record) = table_map.get_mut(&key) {
                record.mark_deleted(snapshot.txn_id, deleted_at)?;
                return Ok(());
            }
        }

        Err(VelociError::NotFound(format!("Record {} not found", key)))
    }

    /// Scan all visible records in a table
    pub fn scan_table(
        &self,
        table_name: &str,
        snapshot: &Snapshot,
    ) -> Result<Vec<(i64, Vec<Value>)>> {
        // LOCK ORDERING: Only acquire version_store (Level 3)
        if let Some(table_store) = self.version_store.get(table_name) {
            let table_map = table_store.read();
            let mut results = Vec::new();

            for (key, record) in table_map.iter() {
                if let Some(version) = record.get_visible_version(snapshot) {
                    results.push((*key, version.data.clone()));
                }
            }

            Ok(results)
        } else {
            Ok(Vec::new())
        }
    }

    /// Vacuum old versions (garbage collection)
    /// Should be called periodically by a background thread
    /// 
    /// LOCK ORDERING (STRICT):
    /// 1. active_snapshots (read) - Level 1
    /// 2. committed_transactions (write) - Level 2  
    /// 3. version_store (write per table) - Level 3
    pub fn vacuum(&self) {
        // STEP 1: Get minimum active timestamp (Level 1 - read only)
        let min_active_timestamp = {
            let snapshots = self.active_snapshots.read();
            snapshots
                .values()
                .map(|s| s.timestamp)
                .min()
                .unwrap_or(self.current_timestamp.load(Ordering::SeqCst))
        }; // Release Level 1 lock

        // STEP 2: Clean up committed transactions (Level 2)
        {
            let mut committed = self.committed_transactions.write();
            committed.retain(|_, &mut ts| ts >= min_active_timestamp);
        } // Release Level 2 lock

        // STEP 3: Clean up version store (Level 3) - per-table locking
        for table_entry in self.version_store.iter() {
            let mut table_map = table_entry.value().write();
            for record in table_map.values_mut() {
                record.vacuum(min_active_timestamp);
            }
        } // Locks released automatically per table
    }

    /// Get statistics about the MVCC system
    /// LOCK ORDERING: Level 1 (active_snapshots) → Level 3 (version_store)
    pub fn get_stats(&self) -> MvccStats {
        // Level 1: Active snapshots
        let active_snapshots = self.active_snapshots.read().len();

        // Level 3: Version store (per-table)
        let mut total_records = 0;
        let mut total_versions = 0;

        for table_entry in self.version_store.iter() {
            let table_map = table_entry.value().read();
            total_records += table_map.len();
            for record in table_map.values() {
                total_versions += record.versions.len();
            }
        }

        MvccStats {
            active_snapshots,
            total_records,
            total_versions,
            version_bloat: if total_records > 0 {
                total_versions as f64 / total_records as f64
            } else {
                0.0
            },
        }
    }
}

impl Default for MvccManager {
    fn default() -> Self {
        Self::new()
    }
}

/// MVCC system statistics
#[derive(Debug, Clone)]
pub struct MvccStats {
    pub active_snapshots: usize,
    pub total_records: usize,
    pub total_versions: usize,
    pub version_bloat: f64,
}

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

    #[test]
    fn test_mvcc_snapshot_isolation() {
        let mvcc = MvccManager::new();

        // Start transaction 1
        let snapshot1 = mvcc.begin_transaction();

        // Insert a record in transaction 1
        mvcc.insert_version(
            "users",
            1,
            vec![Value::Integer(1), Value::Text("Alice".to_string())],
            &snapshot1,
        ).unwrap();

        // Commit transaction 1
        mvcc.commit_transaction(&snapshot1).unwrap();

        // Start transaction 2
        let snapshot2 = mvcc.begin_transaction();

        // Transaction 2 should see the record from transaction 1
        let result = mvcc.read_version("users", 1, &snapshot2).unwrap();
        assert!(result.is_some());

        let data = result.unwrap();
        assert_eq!(data.len(), 2);
        assert_eq!(data[1], Value::Text("Alice".to_string()));
    }

    #[test]
    fn test_mvcc_concurrent_reads() {
        let mvcc = MvccManager::new();

        // Setup: Insert initial data
        let snapshot_setup = mvcc.begin_transaction();
        mvcc.insert_version(
            "products",
            1,
            vec![Value::Integer(1), Value::Text("Laptop".to_string())],
            &snapshot_setup,
        ).unwrap();
        mvcc.commit_transaction(&snapshot_setup).unwrap();

        // Start two concurrent read transactions
        let snapshot_r1 = mvcc.begin_transaction();
        let snapshot_r2 = mvcc.begin_transaction();

        // Both should see the same data
        let result1 = mvcc.read_version("products", 1, &snapshot_r1).unwrap();
        let result2 = mvcc.read_version("products", 1, &snapshot_r2).unwrap();

        assert!(result1.is_some());
        assert!(result2.is_some());
        assert_eq!(result1, result2);
    }

    #[test]
    fn test_mvcc_version_visibility() {
        let mvcc = MvccManager::new();

        // Transaction 1: Insert record
        let snapshot1 = mvcc.begin_transaction();
        mvcc.insert_version(
            "test",
            1,
            vec![Value::Integer(100)],
            &snapshot1,
        ).unwrap();

        // Transaction 2 starts before T1 commits
        let snapshot2 = mvcc.begin_transaction();

        // Commit T1
        mvcc.commit_transaction(&snapshot1).unwrap();

        // T2 should NOT see T1's changes (snapshot isolation)
        let result = mvcc.read_version("test", 1, &snapshot2).unwrap();
        assert!(result.is_none());

        // Transaction 3 starts after T1 commits
        let snapshot3 = mvcc.begin_transaction();

        // T3 should see T1's changes
        let result = mvcc.read_version("test", 1, &snapshot3).unwrap();
        assert!(result.is_some());
    }

    #[test]
    fn test_mvcc_vacuum() {
        let mvcc = MvccManager::new();

        // Create multiple versions
        let snapshot1 = mvcc.begin_transaction();
        mvcc.insert_version("test", 1, vec![Value::Integer(1)], &snapshot1).unwrap();
        mvcc.commit_transaction(&snapshot1).unwrap();

        let snapshot2 = mvcc.begin_transaction();
        mvcc.insert_version("test", 1, vec![Value::Integer(2)], &snapshot2).unwrap();
        mvcc.commit_transaction(&snapshot2).unwrap();

        let snapshot3 = mvcc.begin_transaction();
        mvcc.insert_version("test", 1, vec![Value::Integer(3)], &snapshot3).unwrap();
        mvcc.commit_transaction(&snapshot3).unwrap();

        let stats_before = mvcc.get_stats();
        assert!(stats_before.total_versions >= 3);

        // Run vacuum
        mvcc.vacuum();

        // Stats should show cleanup (in a real scenario with old snapshots)
        let stats_after = mvcc.get_stats();
        assert!(stats_after.total_versions >= 1); // At least one version should remain
    }
}