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 /// Commits the transaction and returns once the commit is durable on disk.
40 ///
41 /// The default calls [`DbTx::commit`]; backends with weak commits must override it. An error
42 /// after a successful commit means the commit may be visible but not durable.
43 fn commit_durable(self) -> Result<(), DatabaseError>
44 where
45 Self: Sized,
46 {
47 self.commit()
48 }
49 /// Aborts transaction
50 fn abort(self);
51 /// Iterate over read only values in table.
52 fn cursor_read<T: Table>(&self) -> Result<Self::Cursor<T>, DatabaseError>;
53 /// Iterate over read only values in dup sorted table.
54 fn cursor_dup_read<T: DupSort>(&self) -> Result<Self::DupCursor<T>, DatabaseError>;
55 /// Returns number of entries in the table.
56 fn entries<T: Table>(&self) -> Result<usize, DatabaseError>;
57 /// Disables long-lived read transaction safety guarantees.
58 fn disable_long_read_transaction_safety(&mut self);
59
60 /// Enables or disables best-effort read-ahead for this transaction.
61 ///
62 /// Defaults to disabled and persists across operations until explicitly changed. Applies to
63 /// gets in every table. New read and write cursors inherit the setting; existing cursors keep
64 /// their own setting, configurable with [`DbCursorRO::prefetch`]. Returns the transaction for
65 /// chaining. Backends without support ignore the setting.
66 #[inline]
67 fn prefetch(&self, _enabled: bool) -> &Self {
68 self
69 }
70}
71
72/// Read write transaction that allows writing to database
73pub trait DbTxMut: Send {
74 /// Read-Write Cursor type
75 type CursorMut<T: Table>: DbCursorRW<T> + DbCursorRO<T> + Send;
76 /// Read-Write `DupCursor` type
77 type DupCursorMut<T: DupSort>: DbDupCursorRW<T>
78 + DbCursorRW<T>
79 + DbDupCursorRO<T>
80 + DbCursorRO<T>
81 + Send;
82
83 /// Put value to database
84 fn put<T: Table>(&self, key: T::Key, value: T::Value) -> Result<(), DatabaseError>;
85 /// Append value with the largest key to database. This should have the same
86 /// outcome as `put`, but databases like MDBX provide dedicated modes to make
87 /// it much faster, typically from O(logN) down to O(1) thanks to no lookup.
88 fn append<T: Table>(&self, key: T::Key, value: T::Value) -> Result<(), DatabaseError> {
89 self.put::<T>(key, value)
90 }
91 /// Delete value from database
92 fn delete<T: Table>(&self, key: T::Key, value: Option<T::Value>)
93 -> Result<bool, DatabaseError>;
94 /// Clears database.
95 fn clear<T: Table>(&self) -> Result<(), DatabaseError>;
96 /// Cursor mut
97 fn cursor_write<T: Table>(&self) -> Result<Self::CursorMut<T>, DatabaseError>;
98 /// `DupCursor` mut.
99 fn cursor_dup_write<T: DupSort>(&self) -> Result<Self::DupCursorMut<T>, DatabaseError>;
100}