sombra 0.3.3

High-performance graph database with ACID transactions, single-file storage, and bindings for Rust, TypeScript, and Python
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
use super::core::GraphDB;
use super::group_commit::TxId;
use crate::error::{GraphError, Result};
use crate::model::{Edge, EdgeId, Node, NodeId};
use crate::pager::PageId;
use tracing::{debug, info, warn};

/// The state of a transaction.
///
/// Transactions progress through these states during their lifecycle.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TxState {
    /// Transaction is active and can accept operations
    Active,
    /// Transaction has been successfully committed
    Committed,
    /// Transaction has been rolled back
    RolledBack,
}

/// A database transaction providing ACID guarantees.
///
/// Transactions allow you to group multiple operations into a single
/// atomic unit. All operations within a transaction are either all
/// committed or all rolled back.
///
/// # Lifecycle
///
/// 1. Create transaction with `GraphDB::begin_transaction()`
/// 2. Perform operations (add nodes, edges, etc.)
/// 3. Either `commit()` to make changes permanent or `rollback()` to discard
///
/// # Important
///
/// Transactions must be explicitly committed or rolled back. If a
/// transaction is dropped without doing so, it will panic (unless
/// the thread is already panicking).
///
/// # Example
///
/// ```rust
/// use sombra::{GraphDB, Node, Edge};
///
/// let mut db = GraphDB::open("test.db")?;
/// {
///     let mut tx = db.begin_transaction()?;
///     let alice = tx.add_node(Node::new(1))?;
///     let bob = tx.add_node(Node::new(2))?;
///     let edge = Edge::new(1, alice, bob, "KNOWS");
///     tx.add_edge(edge)?;
///     tx.commit()?; // Make changes permanent
/// }
/// # Ok::<(), sombra::GraphError>(())
/// ```
#[derive(Debug)]
pub struct Transaction<'db> {
    db: &'db mut GraphDB,
    id: TxId,
    epoch: u64,
    state: TxState,
    pub dirty_pages: Vec<PageId>,
    start_time: std::time::Instant,
}

impl<'db> Transaction<'db> {
    pub(crate) fn new(db: &'db mut GraphDB, id: TxId) -> Result<Self> {
        db.enter_transaction(id)?;
        db.start_tracking();
        let epoch = db.increment_epoch();
        debug!(tx_id = id, epoch = epoch, "Transaction started");
        Ok(Self {
            db,
            id,
            epoch,
            state: TxState::Active,
            dirty_pages: Vec::new(),
            start_time: std::time::Instant::now(),
        })
    }

    /// Returns the unique identifier for this transaction.
    ///
    /// # Returns
    /// The transaction ID.
    pub fn id(&self) -> TxId {
        self.id
    }

    pub fn epoch(&self) -> u64 {
        self.epoch
    }

    /// Returns the current state of the transaction.
    ///
    /// # Returns
    /// The current `TxState`.
    pub fn state(&self) -> TxState {
        self.state
    }

    fn capture_dirty_pages(&mut self) -> Result<()> {
        let mut pages = self.db.take_recent_dirty_pages();
        if pages.is_empty() {
            return Ok(());
        }
        self.dirty_pages.append(&mut pages);
        self.dirty_pages.sort_unstable();
        self.dirty_pages.dedup();

        let max_tx_pages = self.db.config.max_transaction_pages;
        if self.dirty_pages.len() > max_tx_pages {
            warn!(
                tx_id = self.id,
                dirty_pages = self.dirty_pages.len(),
                max_pages = max_tx_pages,
                "Transaction exceeded page limit"
            );
            return Err(GraphError::InvalidArgument(format!(
                "Transaction exceeded maximum page limit of {max_tx_pages}"
            )));
        }

        if let Some(timeout_ms) = self.db.config.transaction_timeout_ms {
            let elapsed = self.start_time.elapsed().as_millis() as u64;
            if elapsed > timeout_ms {
                warn!(
                    tx_id = self.id,
                    elapsed_ms = elapsed,
                    timeout_ms,
                    "Transaction timeout exceeded"
                );
                return Err(GraphError::InvalidArgument(format!(
                    "Transaction timeout exceeded: {elapsed}ms > {timeout_ms}ms"
                )));
            }
        }

        Ok(())
    }

    /// Adds a node to the graph within this transaction.
    ///
    /// The node is not visible to other transactions until this transaction
    /// is committed. If the transaction is rolled back, the node will not
    /// be added to the database.
    ///
    /// # Arguments
    /// * `node` - The node to add
    ///
    /// # Returns
    /// The ID assigned to the new node.
    ///
    /// # Errors
    /// * `GraphError::InvalidArgument` - Transaction limits exceeded
    ///
    /// # Example
    /// ```rust
    /// use sombra::{GraphDB, Node};
    ///
    /// let mut db = GraphDB::open("test.db")?;
    /// let mut tx = db.begin_transaction()?;
    /// let node = Node::new(1);
    /// let node_id = tx.add_node(node)?;
    /// tx.commit()?;
    /// # Ok::<(), sombra::GraphError>(())
    /// ```
    pub fn add_node(&mut self, node: Node) -> Result<NodeId> {
        let node_id = self.db.add_node_internal(node)?;
        self.capture_dirty_pages()?;
        Ok(node_id)
    }

    /// Adds an edge to the graph within this transaction.
    ///
    /// The edge is not visible to other transactions until this transaction
    /// is committed. Both source and target nodes must exist.
    ///
    /// # Arguments
    /// * `edge` - The edge to add
    ///
    /// # Returns
    /// The ID assigned to the new edge.
    ///
    /// # Errors
    /// * `GraphError::InvalidArgument` - Transaction limits exceeded
    /// * `GraphError::NotFound` - Source or target node doesn't exist
    ///
    /// # Example
    /// ```rust
    /// use sombra::{GraphDB, Node, Edge};
    ///
    /// let mut db = GraphDB::open("test.db")?;
    /// let mut tx = db.begin_transaction()?;
    /// let alice = tx.add_node(Node::new(1))?;
    /// let bob = tx.add_node(Node::new(2))?;
    /// let edge = Edge::new(1, alice, bob, "KNOWS");
    /// let edge_id = tx.add_edge(edge)?;
    /// tx.commit()?;
    /// # Ok::<(), sombra::GraphError>(())
    /// ```
    pub fn add_edge(&mut self, edge: Edge) -> Result<EdgeId> {
        let edge_id = self.db.add_edge_internal(edge)?;
        self.capture_dirty_pages()?;
        Ok(edge_id)
    }

    /// Deletes a node from the graph within this transaction.
    ///
    /// The node and all its incident edges will be marked as deleted.
    /// The deletion is not visible to other transactions until this
    /// transaction is committed.
    ///
    /// # Arguments
    /// * `node_id` - The ID of the node to delete
    ///
    /// # Errors
    /// * `GraphError::InvalidArgument` - Transaction limits exceeded
    /// * `GraphError::NotFound` - Node doesn't exist
    pub fn delete_node(&mut self, node_id: NodeId) -> Result<()> {
        self.db.delete_node_internal(node_id)?;
        self.capture_dirty_pages()?;
        Ok(())
    }

    /// Deletes an edge from the graph within this transaction.
    ///
    /// The edge will be marked as deleted. The deletion is not visible
    /// to other transactions until this transaction is committed.
    ///
    /// # Arguments
    /// * `edge_id` - The ID of the edge to delete
    ///
    /// # Errors
    /// * `GraphError::InvalidArgument` - Transaction limits exceeded
    /// * `GraphError::NotFound` - Edge doesn't exist
    pub fn delete_edge(&mut self, edge_id: EdgeId) -> Result<()> {
        self.db.delete_edge_internal(edge_id)?;
        self.capture_dirty_pages()?;
        Ok(())
    }

    /// Retrieves a node by ID within this transaction.
    ///
    /// Can see nodes that were added in this transaction as well as
    /// committed nodes from other transactions.
    ///
    /// # Arguments
    /// * `node_id` - The ID of the node to retrieve
    ///
    /// # Returns
    /// The node with the specified ID.
    ///
    /// # Errors
    /// * `GraphError::NotFound` - Node doesn't exist
    pub fn get_node(&mut self, node_id: NodeId) -> Result<Node> {
        self.db.get_node(node_id)
    }

    pub fn get_edge(&mut self, edge_id: EdgeId) -> Result<Edge> {
        self.db.load_edge(edge_id)
    }

    pub fn get_nodes_by_label(&mut self, label: &str) -> Result<Vec<NodeId>> {
        self.db.get_nodes_by_label(label)
    }

    /// Retrieves all neighboring nodes for a given node.
    ///
    /// Returns both incoming and outgoing neighbors. The result includes
    /// neighbors from this transaction's uncommitted changes as well as
    /// committed data.
    ///
    /// # Arguments
    /// * `node_id` - The ID of the node
    ///
    /// # Returns
    /// A vector of neighboring node IDs.
    ///
    /// # Errors
    /// * `GraphError::NotFound` - Node doesn't exist
    pub fn get_neighbors(&mut self, node_id: NodeId) -> Result<Vec<NodeId>> {
        self.db.get_neighbors(node_id)
    }

    /// Creates a property index (not supported within transactions).
    ///
    /// Property indexes must be created outside of transactions as they
    /// affect the global database state.
    ///
    /// # Arguments
    /// * `label` - The node label to index
    /// * `property_key` - The property key to index
    ///
    /// # Errors
    /// Always returns `GraphError::InvalidArgument` as this operation
    /// cannot be performed within a transaction.
    pub fn create_property_index(&mut self, _label: &str, _property_key: &str) -> Result<()> {
        Err(GraphError::InvalidArgument(
            "create_property_index cannot be called within a transaction".into(),
        ))
    }

    /// Finds nodes by property value using an index.
    ///
    /// Requires that a property index has been created for the specified
    /// label and property key. Only indexable property types (bool, int,
    /// string) can be searched.
    ///
    /// # Arguments
    /// * `label` - The node label to search
    /// * `property_key` - The property key to search
    /// * `value` - The property value to match
    ///
    /// # Returns
    /// A vector of node IDs matching the criteria.
    ///
    /// # Errors
    /// * `GraphError::NotFound` - No index exists for the label/property
    /// * `GraphError::InvalidArgument` - Property type is not indexable
    pub fn find_nodes_by_property(
        &mut self,
        label: &str,
        property_key: &str,
        value: &crate::model::PropertyValue,
    ) -> Result<Vec<NodeId>> {
        self.db.find_nodes_by_property(label, property_key, value)
    }

    /// Commits the transaction, making all changes permanent.
    ///
    /// This is an atomic operation - either all changes are committed
    /// or none are. The transaction cannot be used after committing.
    ///
    /// # Returns
    /// Ok(()) on successful commit.
    ///
    /// # Errors
    /// * `GraphError::Io` - Disk I/O error during commit
    /// * `GraphError::Corruption` - Data corruption detected
    ///
    /// # Example
    /// ```rust
    /// use sombra::{GraphDB, Node};
    ///
    /// let mut db = GraphDB::open("test.db")?;
    /// let mut tx = db.begin_transaction()?;
    /// let node_id = tx.add_node(Node::new(1))?;
    /// tx.commit()?; // Changes are now permanent
    /// # Ok::<(), sombra::GraphError>(())
    /// ```
    pub fn commit(mut self) -> Result<()> {
        self.ensure_active()?;
        self.capture_dirty_pages()?;
        let start = std::time::Instant::now();
        let dirty_page_count = self.dirty_pages.len();

        self.db.header.last_committed_tx_id = self.id;
        let write_header_result = self.db.write_header();
        if let Err(err) = write_header_result {
            let _ = self.db.rollback_transaction(&self.dirty_pages);
            self.db.stop_tracking();
            self.db.exit_transaction();
            self.state = TxState::RolledBack;
            return Err(err);
        }

        self.capture_dirty_pages()?;
        let pages = self.dirty_pages.clone();
        let result = self.db.commit_to_wal(self.id, &pages);
        match result {
            Ok(()) => {
                self.db.stop_tracking();
                self.db.exit_transaction();
                self.state = TxState::Committed;
                let duration = start.elapsed();
                info!(
                    tx_id = self.id,
                    dirty_pages = dirty_page_count,
                    duration_ms = duration.as_millis(),
                    "Transaction committed"
                );
                Ok(())
            }
            Err(err) => {
                let _ = self.db.rollback_transaction(&pages);
                self.db.stop_tracking();
                self.db.exit_transaction();
                self.state = TxState::RolledBack;
                Err(err)
            }
        }
    }

    /// Rolls back the transaction, discarding all changes.
    ///
    /// All operations performed in this transaction are undone and
    /// will not be visible to other transactions. The transaction
    /// cannot be used after rolling back.
    ///
    /// # Returns
    /// Ok(()) on successful rollback.
    ///
    /// # Example
    /// ```rust
    /// use sombra::{GraphDB, Node};
    ///
    /// let mut db = GraphDB::open("test.db")?;
    /// let mut tx = db.begin_transaction()?;
    /// let _node_id = tx.add_node(Node::new(1))?; // Will be discarded
    /// tx.rollback()?; // Changes are discarded
    /// # Ok::<(), sombra::GraphError>(())
    /// ```
    pub fn rollback(mut self) -> Result<()> {
        self.ensure_active()?;
        self.capture_dirty_pages()?;
        let pages = self.dirty_pages.clone();
        let result = self.db.rollback_transaction(&pages);
        self.db.stop_tracking();
        self.db.exit_transaction();
        self.state = TxState::RolledBack;
        warn!(tx_id = self.id, "Transaction rolled back");
        result
    }

    fn ensure_active(&self) -> Result<()> {
        if self.state != TxState::Active {
            return Err(GraphError::InvalidArgument(
                "transaction is no longer active".into(),
            ));
        }
        Ok(())
    }
}

impl<'db> Drop for Transaction<'db> {
    fn drop(&mut self) {
        self.db.stop_tracking();
        if self.state == TxState::Active {
            let _ = self.db.rollback_transaction(&self.dirty_pages);
            self.db.exit_transaction();
            if !std::thread::panicking() {
                panic!("transaction {} dropped without commit or rollback", self.id);
            }
        }
    }
}