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/// Helper adapter type for accessing [`DbTx`] cursor.
9pub type CursorTy<TX, T> = <TX as DbTx>::Cursor<T>;
10
11/// Helper adapter type for accessing [`DbTxMut`] mutable cursor.
12pub type CursorMutTy<TX, T> = <TX as DbTxMut>::CursorMut<T>;
13
14/// Read only transaction
15pub trait DbTx: Debug + Send + Sync {
16    /// Cursor type for this read-only transaction
17    type Cursor<T: Table>: DbCursorRO<T> + Send + Sync;
18    /// `DupCursor` type for this read-only transaction
19    type DupCursor<T: DupSort>: DbDupCursorRO<T> + DbCursorRO<T> + Send + Sync;
20
21    /// Get value by an owned key
22    fn get<T: Table>(&self, key: T::Key) -> Result<Option<T::Value>, DatabaseError>;
23    /// Get value by a reference to the encoded key, especially useful for "raw" keys
24    /// that encode to themselves like Address and B256. Doesn't need to clone a
25    /// reference key like `get`.
26    fn get_by_encoded_key<T: Table>(
27        &self,
28        key: &<T::Key as Encode>::Encoded,
29    ) -> Result<Option<T::Value>, DatabaseError>;
30    /// Commit for read only transaction will consume and free transaction and allows
31    /// freeing of memory pages
32    fn commit(self) -> Result<bool, DatabaseError>;
33    /// Aborts transaction
34    fn abort(self);
35    /// Iterate over read only values in table.
36    fn cursor_read<T: Table>(&self) -> Result<Self::Cursor<T>, DatabaseError>;
37    /// Iterate over read only values in dup sorted table.
38    fn cursor_dup_read<T: DupSort>(&self) -> Result<Self::DupCursor<T>, DatabaseError>;
39    /// Returns number of entries in the table.
40    fn entries<T: Table>(&self) -> Result<usize, DatabaseError>;
41    /// Disables long-lived read transaction safety guarantees.
42    fn disable_long_read_transaction_safety(&mut self);
43}
44
45/// Read write transaction that allows writing to database
46pub trait DbTxMut: Send + Sync {
47    /// Read-Write Cursor type
48    type CursorMut<T: Table>: DbCursorRW<T> + DbCursorRO<T> + Send + Sync;
49    /// Read-Write `DupCursor` type
50    type DupCursorMut<T: DupSort>: DbDupCursorRW<T>
51        + DbCursorRW<T>
52        + DbDupCursorRO<T>
53        + DbCursorRO<T>
54        + Send
55        + Sync;
56
57    /// Put value to database
58    fn put<T: Table>(&self, key: T::Key, value: T::Value) -> Result<(), DatabaseError>;
59    /// Append value with the largest key to database. This should have the same
60    /// outcome as `put`, but databases like MDBX provide dedicated modes to make
61    /// it much faster, typically from O(logN) down to O(1) thanks to no lookup.
62    fn append<T: Table>(&self, key: T::Key, value: T::Value) -> Result<(), DatabaseError> {
63        self.put::<T>(key, value)
64    }
65    /// Delete value from database
66    fn delete<T: Table>(&self, key: T::Key, value: Option<T::Value>)
67        -> Result<bool, DatabaseError>;
68    /// Clears database.
69    fn clear<T: Table>(&self) -> Result<(), DatabaseError>;
70    /// Cursor mut
71    fn cursor_write<T: Table>(&self) -> Result<Self::CursorMut<T>, DatabaseError>;
72    /// `DupCursor` mut.
73    fn cursor_dup_write<T: DupSort>(&self) -> Result<Self::DupCursorMut<T>, DatabaseError>;
74}