Skip to main content

common/database/
transactions.rs

1// Generated by Qleany v1.7.3 from transactions.tera
2
3use crate::database::Store;
4use crate::database::db_context::DbContext;
5use crate::snapshot::StoreSnapshot;
6use anyhow::{Ok, Result, bail};
7use std::sync::Arc;
8
9use crate::types;
10
11pub struct Transaction {
12    store: Arc<Store>,
13    is_write: bool,
14    savepoint: Option<u64>,
15}
16
17impl Transaction {
18    pub fn begin_write_transaction(db_context: &DbContext) -> Result<Transaction> {
19        let store = Arc::clone(db_context.get_store());
20        // Refused, before anything is written, over outstanding transient edits unless
21        // this is the transient write making them: see `RopeStore::begin_transient_write`.
22        store.check_write_allowed()?;
23        let savepoint = Some(store.create_savepoint());
24        Ok(Transaction {
25            store,
26            is_write: true,
27            savepoint,
28        })
29    }
30
31    pub fn begin_read_transaction(db_context: &DbContext) -> Result<Transaction> {
32        Ok(Transaction {
33            store: Arc::clone(db_context.get_store()),
34            is_write: false,
35            savepoint: None,
36        })
37    }
38
39    pub fn commit(&mut self) -> Result<()> {
40        if !self.is_write {
41            bail!("Cannot commit a read transaction");
42        }
43        // Plan §1.6: refresh Frame.byte_range from current rope state
44        // before the mutations become permanent. No-op under default.
45        crate::database::rope_helpers::recompute_all_frame_byte_ranges(&self.store);
46        // Discard the auto-savepoint — mutations are now permanent
47        if let Some(sp) = self.savepoint.take() {
48            self.store.discard_savepoint(sp);
49        }
50        Ok(())
51    }
52
53    pub fn rollback(&mut self) -> Result<()> {
54        if !self.is_write {
55            bail!("Cannot rollback a read transaction");
56        }
57        // Restore the auto-savepoint, undoing all mutations, then discard it, so that a
58        // rolled-back transaction does not leave its snapshot of the whole store behind.
59        if let Some(sp) = self.savepoint.take() {
60            self.store.restore_savepoint(sp);
61            self.store.discard_savepoint(sp);
62        }
63        Ok(())
64    }
65
66    pub fn end_read_transaction(&mut self) -> Result<()> {
67        if self.is_write {
68            bail!("Cannot end a write transaction as read");
69        }
70        Ok(())
71    }
72
73    pub fn get_store(&self) -> &Store {
74        &self.store
75    }
76
77    pub fn create_savepoint(&self) -> Result<types::Savepoint> {
78        if !self.is_write {
79            bail!("Cannot create savepoint on a read transaction");
80        }
81        Ok(self.store.create_savepoint())
82    }
83
84    pub fn restore_to_savepoint(&mut self, savepoint: types::Savepoint) -> Result<()> {
85        if !self.is_write {
86            bail!("Cannot restore savepoint on a read transaction");
87        }
88        self.store.restore_savepoint(savepoint);
89        Ok(())
90    }
91
92    /// Snapshot the entire store for undo. O(1) thanks to im::HashMap.
93    pub fn snapshot_store(&self) -> StoreSnapshot {
94        self.store.store_snapshot()
95    }
96
97    /// Restore the entire store from an undo snapshot (preserves counters).
98    pub fn restore_store(&self, snap: &StoreSnapshot) {
99        self.store.restore_store_snapshot(snap);
100    }
101}
102
103impl Drop for Transaction {
104    fn drop(&mut self) {
105        // Safety net: if the transaction was not committed or rolled back,
106        // restore the auto-savepoint to undo any partial mutations, then discard it, so
107        // that the snapshot is not left behind in the store's savepoints.
108        if let Some(sp) = self.savepoint.take() {
109            self.store.restore_savepoint(sp);
110            self.store.discard_savepoint(sp);
111        }
112    }
113}