reth_db_api/
transaction.rs

1use crate::{
2    cursor::{DbCursorRO, DbCursorRW, DbDupCursorRO, DbDupCursorRW},
3    table::{DupSort, Encode, Table},
4    DatabaseError,
5};
6use std::fmt::Debug;
7
8/// Read only transaction
9pub trait DbTx: Debug + Send + Sync {
10    /// Cursor type for this read-only transaction
11    type Cursor<T: Table>: DbCursorRO<T> + Send + Sync;
12    /// `DupCursor` type for this read-only transaction
13    type DupCursor<T: DupSort>: DbDupCursorRO<T> + DbCursorRO<T> + Send + Sync;
14
15    /// Get value by an owned key
16    fn get<T: Table>(&self, key: T::Key) -> Result<Option<T::Value>, DatabaseError>;
17    /// Get value by a reference to the encoded key, especially useful for "raw" keys
18    /// that encode to themselves like Address and B256. Doesn't need to clone a
19    /// reference key like `get`.
20    fn get_by_encoded_key<T: Table>(
21        &self,
22        key: &<T::Key as Encode>::Encoded,
23    ) -> Result<Option<T::Value>, DatabaseError>;
24    /// Commit for read only transaction will consume and free transaction and allows
25    /// freeing of memory pages
26    fn commit(self) -> Result<bool, DatabaseError>;
27    /// Aborts transaction
28    fn abort(self);
29    /// Iterate over read only values in table.
30    fn cursor_read<T: Table>(&self) -> Result<Self::Cursor<T>, DatabaseError>;
31    /// Iterate over read only values in dup sorted table.
32    fn cursor_dup_read<T: DupSort>(&self) -> Result<Self::DupCursor<T>, DatabaseError>;
33    /// Returns number of entries in the table.
34    fn entries<T: Table>(&self) -> Result<usize, DatabaseError>;
35    /// Disables long-lived read transaction safety guarantees.
36    fn disable_long_read_transaction_safety(&mut self);
37}
38
39/// Read write transaction that allows writing to database
40pub trait DbTxMut: Send + Sync {
41    /// Read-Write Cursor type
42    type CursorMut<T: Table>: DbCursorRW<T> + DbCursorRO<T> + Send + Sync;
43    /// Read-Write `DupCursor` type
44    type DupCursorMut<T: DupSort>: DbDupCursorRW<T>
45        + DbCursorRW<T>
46        + DbDupCursorRO<T>
47        + DbCursorRO<T>
48        + Send
49        + Sync;
50
51    /// Put value to database
52    fn put<T: Table>(&self, key: T::Key, value: T::Value) -> Result<(), DatabaseError>;
53    /// Delete value from database
54    fn delete<T: Table>(&self, key: T::Key, value: Option<T::Value>)
55        -> Result<bool, DatabaseError>;
56    /// Clears database.
57    fn clear<T: Table>(&self) -> Result<(), DatabaseError>;
58    /// Cursor mut
59    fn cursor_write<T: Table>(&self) -> Result<Self::CursorMut<T>, DatabaseError>;
60    /// `DupCursor` mut.
61    fn cursor_dup_write<T: DupSort>(&self) -> Result<Self::DupCursorMut<T>, DatabaseError>;
62}