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 /// Enables or disables best-effort read-ahead for this transaction.
51 ///
52 /// Defaults to disabled and persists across operations until explicitly changed. Applies to
53 /// gets in every table. New read and write cursors inherit the setting; existing cursors keep
54 /// their own setting, configurable with [`DbCursorRO::prefetch`]. Returns the transaction for
55 /// chaining. Backends without support ignore the setting.
56 #[inline]
57 fn prefetch(&self, _enabled: bool) -> &Self {
58 self
59 }
60}
61
62/// Read write transaction that allows writing to database
63pub trait DbTxMut: Send {
64 /// Read-Write Cursor type
65 type CursorMut<T: Table>: DbCursorRW<T> + DbCursorRO<T> + Send;
66 /// Read-Write `DupCursor` type
67 type DupCursorMut<T: DupSort>: DbDupCursorRW<T>
68 + DbCursorRW<T>
69 + DbDupCursorRO<T>
70 + DbCursorRO<T>
71 + Send;
72
73 /// Put value to database
74 fn put<T: Table>(&self, key: T::Key, value: T::Value) -> Result<(), DatabaseError>;
75 /// Append value with the largest key to database. This should have the same
76 /// outcome as `put`, but databases like MDBX provide dedicated modes to make
77 /// it much faster, typically from O(logN) down to O(1) thanks to no lookup.
78 fn append<T: Table>(&self, key: T::Key, value: T::Value) -> Result<(), DatabaseError> {
79 self.put::<T>(key, value)
80 }
81 /// Delete value from database
82 fn delete<T: Table>(&self, key: T::Key, value: Option<T::Value>)
83 -> Result<bool, DatabaseError>;
84 /// Clears database.
85 fn clear<T: Table>(&self) -> Result<(), DatabaseError>;
86 /// Cursor mut
87 fn cursor_write<T: Table>(&self) -> Result<Self::CursorMut<T>, DatabaseError>;
88 /// `DupCursor` mut.
89 fn cursor_dup_write<T: DupSort>(&self) -> Result<Self::DupCursorMut<T>, DatabaseError>;
90}