Skip to main content

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