reth_db_api/
transaction.rs

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