vibesql-server 0.1.3

Network server with PostgreSQL wire protocol for VibeSQL
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
//! Transaction isolation support for server sessions.
//!
//! This module provides transaction state management for individual sessions,
//! enabling READ COMMITTED isolation level for shared databases.
//!
//! # Architecture
//!
//! When multiple sessions share a database via `DatabaseRegistry`, each session
//! needs its own transaction state to provide proper isolation:
//!
//! - **READ COMMITTED**: Uncommitted changes in one transaction are NOT visible
//!   to other sessions. Only committed changes propagate to other sessions.
//!
//! # Implementation
//!
//! We use a copy-on-write approach:
//! 1. On BEGIN: Create a snapshot of affected tables as writes occur
//! 2. During transaction: Writes go to the session's local buffer
//! 3. On COMMIT: Atomically merge buffer changes into shared database
//! 4. On ROLLBACK: Discard the buffer (no changes to shared database)
//!
//! This provides READ COMMITTED semantics where:
//! - Other sessions always read committed data
//! - A session in a transaction sees its own uncommitted changes
//! - Committed changes become visible to all sessions

use std::collections::HashMap;
use vibesql_storage::Row;

/// A change made during a transaction that needs to be applied on commit.
#[derive(Debug, Clone, PartialEq)]
#[allow(clippy::large_enum_variant)]
pub enum TransactionChange {
    /// A row was inserted
    Insert {
        table_name: String,
        row: Row,
    },
    /// A row was updated (old values for rollback reference)
    Update {
        table_name: String,
        row_index: usize,
        old_row: Row,
        new_row: Row,
    },
    /// A row was deleted
    Delete {
        table_name: String,
        row_index: usize,
        row: Row,
    },
    /// A table was created
    CreateTable {
        table_name: String,
    },
    /// A table was dropped
    DropTable {
        table_name: String,
    },
    /// An index was created
    CreateIndex {
        index_name: String,
        table_name: String,
    },
    /// An index was dropped
    DropIndex {
        index_name: String,
    },
}

/// Transaction state for a session.
///
/// Tracks uncommitted changes during an active transaction, providing
/// READ COMMITTED isolation when multiple sessions share a database.
#[derive(Debug)]
pub struct TransactionState {
    /// Transaction ID (monotonically increasing)
    pub id: u64,
    /// Whether we're in an active transaction block
    pub active: bool,
    /// Changes made during this transaction (in order)
    changes: Vec<TransactionChange>,
    /// Inserted rows indexed by table name (for reads during transaction)
    inserted_rows: HashMap<String, Vec<Row>>,
    /// Deleted row indices indexed by table name (to filter from reads)
    deleted_indices: HashMap<String, Vec<usize>>,
    /// Updated rows: table_name -> (row_index -> new_row)
    updated_rows: HashMap<String, HashMap<usize, Row>>,
}

impl TransactionState {
    /// Create a new transaction state with the given ID.
    pub fn new(id: u64) -> Self {
        Self {
            id,
            active: true,
            changes: Vec::new(),
            inserted_rows: HashMap::new(),
            deleted_indices: HashMap::new(),
            updated_rows: HashMap::new(),
        }
    }

    /// Record an insert operation.
    pub fn record_insert(&mut self, table_name: String, row: Row) {
        self.changes.push(TransactionChange::Insert {
            table_name: table_name.clone(),
            row: row.clone(),
        });
        self.inserted_rows.entry(table_name).or_default().push(row);
    }

    /// Record an update operation.
    pub fn record_update(
        &mut self,
        table_name: String,
        row_index: usize,
        old_row: Row,
        new_row: Row,
    ) {
        self.changes.push(TransactionChange::Update {
            table_name: table_name.clone(),
            row_index,
            old_row,
            new_row: new_row.clone(),
        });
        self.updated_rows
            .entry(table_name)
            .or_default()
            .insert(row_index, new_row);
    }

    /// Record a delete operation.
    pub fn record_delete(&mut self, table_name: String, row_index: usize, row: Row) {
        self.changes.push(TransactionChange::Delete {
            table_name: table_name.clone(),
            row_index,
            row,
        });
        self.deleted_indices.entry(table_name).or_default().push(row_index);
    }

    /// Record a table creation.
    pub fn record_create_table(&mut self, table_name: String) {
        self.changes.push(TransactionChange::CreateTable { table_name });
    }

    /// Record a table drop.
    pub fn record_drop_table(&mut self, table_name: String) {
        self.changes.push(TransactionChange::DropTable { table_name });
    }

    /// Record an index creation.
    pub fn record_create_index(&mut self, index_name: String, table_name: String) {
        self.changes.push(TransactionChange::CreateIndex { index_name, table_name });
    }

    /// Record an index drop.
    pub fn record_drop_index(&mut self, index_name: String) {
        self.changes.push(TransactionChange::DropIndex { index_name });
    }

    /// Get rows inserted in this transaction for a table.
    pub fn get_inserted_rows(&self, table_name: &str) -> Option<&Vec<Row>> {
        self.inserted_rows.get(table_name)
    }

    /// Get indices of rows deleted in this transaction for a table.
    pub fn get_deleted_indices(&self, table_name: &str) -> Option<&Vec<usize>> {
        self.deleted_indices.get(table_name)
    }

    /// Get updated rows for a table (index -> new_row).
    pub fn get_updated_rows(&self, table_name: &str) -> Option<&HashMap<usize, Row>> {
        self.updated_rows.get(table_name)
    }

    /// Check if a row at a given index was deleted in this transaction.
    pub fn is_deleted(&self, table_name: &str, row_index: usize) -> bool {
        self.deleted_indices
            .get(table_name)
            .is_some_and(|indices| indices.contains(&row_index))
    }

    /// Get the updated version of a row if it was updated in this transaction.
    pub fn get_updated_row(&self, table_name: &str, row_index: usize) -> Option<&Row> {
        self.updated_rows
            .get(table_name)
            .and_then(|updates| updates.get(&row_index))
    }

    /// Consume the transaction state and return all changes for commit.
    pub fn take_changes(self) -> Vec<TransactionChange> {
        self.changes
    }

    /// Get all changes (for inspection without consuming).
    pub fn changes(&self) -> &[TransactionChange] {
        &self.changes
    }

    /// Check if there are any uncommitted changes.
    pub fn has_changes(&self) -> bool {
        !self.changes.is_empty()
    }

    /// Clear all changes (for rollback).
    pub fn clear(&mut self) {
        self.changes.clear();
        self.inserted_rows.clear();
        self.deleted_indices.clear();
        self.updated_rows.clear();
    }
}

/// Manager for session transaction state.
///
/// Each session has its own `SessionTransactionManager` to track
/// its transaction state independently of other sessions.
#[derive(Debug, Default)]
pub struct SessionTransactionManager {
    /// Current transaction state (None if no active transaction)
    current: Option<TransactionState>,
    /// Next transaction ID to assign
    next_id: u64,
}

impl SessionTransactionManager {
    /// Create a new session transaction manager.
    pub fn new() -> Self {
        Self { current: None, next_id: 1 }
    }

    /// Begin a new transaction.
    ///
    /// Returns an error if a transaction is already active.
    pub fn begin(&mut self) -> Result<u64, TransactionError> {
        if self.current.is_some() {
            return Err(TransactionError::AlreadyInTransaction);
        }

        let id = self.next_id;
        self.next_id += 1;
        self.current = Some(TransactionState::new(id));
        Ok(id)
    }

    /// Commit the current transaction.
    ///
    /// Returns the changes to be applied to the shared database.
    pub fn commit(&mut self) -> Result<Vec<TransactionChange>, TransactionError> {
        let state = self.current.take().ok_or(TransactionError::NoActiveTransaction)?;
        Ok(state.take_changes())
    }

    /// Rollback the current transaction.
    ///
    /// Discards all uncommitted changes.
    pub fn rollback(&mut self) -> Result<(), TransactionError> {
        self.current.take().ok_or(TransactionError::NoActiveTransaction)?;
        Ok(())
    }

    /// Check if a transaction is currently active.
    pub fn in_transaction(&self) -> bool {
        self.current.as_ref().is_some_and(|s| s.active)
    }

    /// Get the current transaction ID, if any.
    pub fn transaction_id(&self) -> Option<u64> {
        self.current.as_ref().map(|s| s.id)
    }

    /// Get mutable access to the current transaction state.
    pub fn current_mut(&mut self) -> Option<&mut TransactionState> {
        self.current.as_mut()
    }

    /// Get read access to the current transaction state.
    pub fn current(&self) -> Option<&TransactionState> {
        self.current.as_ref()
    }

    /// Record an insert in the current transaction.
    ///
    /// No-op if not in a transaction.
    pub fn record_insert(&mut self, table_name: String, row: Row) {
        if let Some(state) = &mut self.current {
            state.record_insert(table_name, row);
        }
    }

    /// Record an update in the current transaction.
    ///
    /// No-op if not in a transaction.
    pub fn record_update(
        &mut self,
        table_name: String,
        row_index: usize,
        old_row: Row,
        new_row: Row,
    ) {
        if let Some(state) = &mut self.current {
            state.record_update(table_name, row_index, old_row, new_row);
        }
    }

    /// Record a delete in the current transaction.
    ///
    /// No-op if not in a transaction.
    pub fn record_delete(&mut self, table_name: String, row_index: usize, row: Row) {
        if let Some(state) = &mut self.current {
            state.record_delete(table_name, row_index, row);
        }
    }
}

/// Errors that can occur during transaction management.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TransactionError {
    /// Attempted to begin a transaction when one is already active.
    AlreadyInTransaction,
    /// Attempted to commit/rollback when no transaction is active.
    NoActiveTransaction,
    /// A conflict was detected during commit.
    CommitConflict(String),
}

impl std::fmt::Display for TransactionError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TransactionError::AlreadyInTransaction => {
                write!(f, "Transaction already in progress")
            }
            TransactionError::NoActiveTransaction => {
                write!(f, "No transaction in progress")
            }
            TransactionError::CommitConflict(msg) => {
                write!(f, "Commit conflict: {}", msg)
            }
        }
    }
}

impl std::error::Error for TransactionError {}

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

    fn make_row(values: Vec<SqlValue>) -> Row {
        Row::new(values)
    }

    #[test]
    fn test_begin_transaction() {
        let mut mgr = SessionTransactionManager::new();

        assert!(!mgr.in_transaction());
        assert_eq!(mgr.transaction_id(), None);

        let id = mgr.begin().unwrap();
        assert_eq!(id, 1);
        assert!(mgr.in_transaction());
        assert_eq!(mgr.transaction_id(), Some(1));
    }

    #[test]
    fn test_double_begin_fails() {
        let mut mgr = SessionTransactionManager::new();

        mgr.begin().unwrap();
        let result = mgr.begin();
        assert_eq!(result, Err(TransactionError::AlreadyInTransaction));
    }

    #[test]
    fn test_commit_without_transaction_fails() {
        let mut mgr = SessionTransactionManager::new();

        let result = mgr.commit();
        assert_eq!(result, Err(TransactionError::NoActiveTransaction));
    }

    #[test]
    fn test_rollback_without_transaction_fails() {
        let mut mgr = SessionTransactionManager::new();

        let result = mgr.rollback();
        assert_eq!(result, Err(TransactionError::NoActiveTransaction));
    }

    #[test]
    fn test_record_insert() {
        let mut mgr = SessionTransactionManager::new();
        mgr.begin().unwrap();

        let row = make_row(vec![SqlValue::Integer(1), SqlValue::Varchar(arcstr::ArcStr::from("test"))]);
        mgr.record_insert("users".to_string(), row.clone());

        let state = mgr.current().unwrap();
        assert!(state.has_changes());

        let inserted = state.get_inserted_rows("users").unwrap();
        assert_eq!(inserted.len(), 1);
        assert_eq!(inserted[0].values, row.values);
    }

    #[test]
    fn test_record_delete() {
        let mut mgr = SessionTransactionManager::new();
        mgr.begin().unwrap();

        let row = make_row(vec![SqlValue::Integer(1)]);
        mgr.record_delete("users".to_string(), 5, row);

        let state = mgr.current().unwrap();
        assert!(state.is_deleted("users", 5));
        assert!(!state.is_deleted("users", 6));
        assert!(!state.is_deleted("other_table", 5));
    }

    #[test]
    fn test_record_update() {
        let mut mgr = SessionTransactionManager::new();
        mgr.begin().unwrap();

        let old_row = make_row(vec![SqlValue::Integer(1), SqlValue::Varchar(arcstr::ArcStr::from("old"))]);
        let new_row = make_row(vec![SqlValue::Integer(1), SqlValue::Varchar(arcstr::ArcStr::from("new"))]);
        mgr.record_update("users".to_string(), 3, old_row, new_row.clone());

        let state = mgr.current().unwrap();
        let updated = state.get_updated_row("users", 3).unwrap();
        assert_eq!(updated.values, new_row.values);
        assert!(state.get_updated_row("users", 4).is_none());
    }

    #[test]
    fn test_commit_returns_changes() {
        let mut mgr = SessionTransactionManager::new();
        mgr.begin().unwrap();

        let row1 = make_row(vec![SqlValue::Integer(1)]);
        let row2 = make_row(vec![SqlValue::Integer(2)]);
        mgr.record_insert("users".to_string(), row1);
        mgr.record_insert("users".to_string(), row2);

        let changes = mgr.commit().unwrap();
        assert_eq!(changes.len(), 2);
        assert!(!mgr.in_transaction());
    }

    #[test]
    fn test_rollback_discards_changes() {
        let mut mgr = SessionTransactionManager::new();
        mgr.begin().unwrap();

        let row = make_row(vec![SqlValue::Integer(1)]);
        mgr.record_insert("users".to_string(), row);

        mgr.rollback().unwrap();
        assert!(!mgr.in_transaction());

        // Can start a new transaction after rollback
        mgr.begin().unwrap();
        assert!(mgr.in_transaction());
        assert_eq!(mgr.transaction_id(), Some(2)); // ID incremented
    }

    #[test]
    fn test_transaction_id_increments() {
        let mut mgr = SessionTransactionManager::new();

        let id1 = mgr.begin().unwrap();
        mgr.commit().unwrap();

        let id2 = mgr.begin().unwrap();
        mgr.rollback().unwrap();

        let id3 = mgr.begin().unwrap();

        assert_eq!(id1, 1);
        assert_eq!(id2, 2);
        assert_eq!(id3, 3);
    }

    #[test]
    fn test_no_op_when_not_in_transaction() {
        let mut mgr = SessionTransactionManager::new();

        // These should not panic and should be no-ops
        let row = make_row(vec![SqlValue::Integer(1)]);
        mgr.record_insert("users".to_string(), row.clone());
        mgr.record_delete("users".to_string(), 0, row.clone());
        mgr.record_update("users".to_string(), 0, row.clone(), row);

        // Should not be in transaction
        assert!(!mgr.in_transaction());
    }
}