reth_storage_api/
database_provider.rs1use alloc::vec::Vec;
2use core::ops::{Bound, RangeBounds};
3use reth_db_api::{
4 common::KeyValue,
5 cursor::DbCursorRO,
6 database::Database,
7 table::Table,
8 transaction::{DbTx, DbTxMut},
9 DatabaseError,
10};
11use reth_prune_types::PruneModes;
12use reth_storage_errors::provider::ProviderResult;
13
14#[auto_impl::auto_impl(&)]
16pub trait DbTxProvider {
17 type Tx: DbTx + 'static;
19
20 fn tx(&self) -> &Self::Tx;
22}
23
24pub trait DBProvider: DbTxProvider + Sized {
26 fn tx_ref(&self) -> &Self::Tx {
28 self.tx()
29 }
30
31 fn tx_mut(&mut self) -> &mut Self::Tx;
33
34 fn into_tx(self) -> Self::Tx;
36
37 fn disable_long_read_transaction_safety(mut self) -> Self {
44 self.tx_mut().disable_long_read_transaction_safety();
45 self
46 }
47
48 fn commit(self) -> ProviderResult<()>;
50
51 fn prune_modes_ref(&self) -> &PruneModes;
53
54 fn table<T: Table>(&self) -> Result<Vec<KeyValue<T>>, DatabaseError>
56 where
57 T::Key: Default + Ord,
58 {
59 self.tx_ref()
60 .cursor_read::<T>()?
61 .walk(Some(T::Key::default()))?
62 .collect::<Result<Vec<_>, DatabaseError>>()
63 }
64
65 #[inline]
67 fn get<T: Table>(
68 &self,
69 range: impl RangeBounds<T::Key>,
70 ) -> Result<Vec<KeyValue<T>>, DatabaseError> {
71 self.tx_ref().cursor_read::<T>()?.walk_range(range)?.collect::<Result<Vec<_>, _>>()
72 }
73
74 fn cursor_read_collect<T: Table<Key = u64>>(
78 &self,
79 range: impl RangeBounds<T::Key>,
80 ) -> ProviderResult<Vec<T::Value>> {
81 let capacity = match range_size_hint(&range) {
82 Some(0) | None => return Ok(Vec::new()),
83 Some(capacity) => capacity,
84 };
85 let mut cursor = self.tx_ref().cursor_read::<T>()?;
86 let mut items = Vec::with_capacity(capacity);
87 for entry in cursor.walk_range(range)? {
88 items.push(entry?.1);
89 }
90 Ok(items)
91 }
92
93 #[inline]
95 fn remove<T: Table>(&self, range: impl RangeBounds<T::Key>) -> Result<usize, DatabaseError>
96 where
97 Self::Tx: DbTxMut,
98 {
99 let mut entries = 0;
100 let mut cursor_write = self.tx_ref().cursor_write::<T>()?;
101 let mut walker = cursor_write.walk_range(range)?;
102 while walker.next().transpose()?.is_some() {
103 walker.delete_current()?;
104 entries += 1;
105 }
106 Ok(entries)
107 }
108
109 #[inline]
111 fn take<T: Table>(
112 &self,
113 range: impl RangeBounds<T::Key>,
114 ) -> Result<Vec<KeyValue<T>>, DatabaseError>
115 where
116 Self::Tx: DbTxMut,
117 {
118 let mut cursor_write = self.tx_ref().cursor_write::<T>()?;
119 let mut walker = cursor_write.walk_range(range)?;
120 let mut items = Vec::new();
121 while let Some(i) = walker.next().transpose()? {
122 walker.delete_current()?;
123 items.push(i)
124 }
125 Ok(items)
126 }
127}
128
129#[auto_impl::auto_impl(&, Arc)]
131pub trait DatabaseProviderFactory: Send + Sync {
132 type DB: Database;
134
135 type Provider: DBProvider<Tx = <Self::DB as Database>::TX>;
137
138 type ProviderRW: DBProvider<Tx = <Self::DB as Database>::TXMut>;
140
141 fn database_provider_ro(&self) -> ProviderResult<Self::Provider>;
143
144 fn database_provider_rw(&self) -> ProviderResult<Self::ProviderRW>;
146}
147
148pub trait DatabaseProviderROFactory {
150 type Provider;
155
156 fn database_provider_ro(&self) -> ProviderResult<Self::Provider>;
158}
159
160impl<T> DatabaseProviderROFactory for T
161where
162 T: DatabaseProviderFactory,
163{
164 type Provider = T::Provider;
165
166 fn database_provider_ro(&self) -> ProviderResult<Self::Provider> {
167 <T as DatabaseProviderFactory>::database_provider_ro(self)
168 }
169}
170
171pub fn range_size_hint(range: &impl RangeBounds<u64>) -> Option<usize> {
173 let start = match range.start_bound().cloned() {
174 Bound::Included(start) => start,
175 Bound::Excluded(start) => start.checked_add(1)?,
176 Bound::Unbounded => 0,
177 };
178 let end = match range.end_bound().cloned() {
179 Bound::Included(end) => end.saturating_add(1),
180 Bound::Excluded(end) => end,
181 Bound::Unbounded => return None,
182 };
183 end.checked_sub(start).map(|x| x as _)
184}