Skip to main content

reth_db_common/db_tool/
mod.rs

1//! Common db operations
2
3use boyer_moore_magiclen::BMByte;
4use eyre::Result;
5use reth_db_api::{
6    cursor::{DbCursorRO, DbDupCursorRO},
7    database::Database,
8    table::{Decode, Decompress, DupSort, Table, TableRow},
9    transaction::{DbTx, DbTxMut},
10    DatabaseError, RawTable, TableRawRow,
11};
12use reth_fs_util as fs;
13use reth_node_types::NodeTypesWithDB;
14use reth_provider::{providers::ProviderNodeTypes, ChainSpecProvider, DBProvider, ProviderFactory};
15use std::{path::Path, rc::Rc, sync::Arc};
16use tracing::info;
17
18/// Wrapper over DB that implements many useful DB queries.
19#[derive(Debug)]
20pub struct DbTool<N: NodeTypesWithDB> {
21    /// The provider factory that the db tool will use.
22    pub provider_factory: ProviderFactory<N>,
23}
24
25impl<N: NodeTypesWithDB> DbTool<N> {
26    /// Get an [`Arc`] to the underlying chainspec.
27    pub fn chain(&self) -> Arc<N::ChainSpec> {
28        self.provider_factory.chain_spec()
29    }
30
31    /// Grabs the contents of the table within a certain index range and places the
32    /// entries into a [`HashMap`][std::collections::HashMap].
33    ///
34    /// [`ListFilter`] can be used to further
35    /// filter down the desired results. (eg. List only rows which include `0xd3adbeef`)
36    pub fn list<T: Table>(&self, filter: &ListFilter) -> Result<(Vec<TableRow<T>>, usize)> {
37        let bmb = Rc::new(BMByte::from(&filter.search));
38        if bmb.is_none() && filter.has_search() {
39            eyre::bail!("Invalid search.");
40        }
41
42        let mut hits = 0;
43
44        let data = self.provider_factory.db_ref().view(|tx| {
45            let mut cursor =
46                tx.cursor_read::<RawTable<T>>().expect("Was not able to obtain a cursor.");
47
48            let map_filter = |row: Result<TableRawRow<T>, _>| {
49                if let Ok((k, v)) = row {
50                    let (key, value) = (k.into_key(), v.into_value());
51
52                    if key.len() + value.len() < filter.min_row_size {
53                        return None
54                    }
55                    if key.len() < filter.min_key_size {
56                        return None
57                    }
58                    if value.len() < filter.min_value_size {
59                        return None
60                    }
61
62                    let result = || {
63                        if filter.only_count {
64                            return None
65                        }
66                        Some((
67                            <T as Table>::Key::decode(&key).unwrap(),
68                            <T as Table>::Value::decompress(&value).unwrap(),
69                        ))
70                    };
71
72                    match &*bmb {
73                        Some(searcher) => {
74                            if searcher.find_first_in(&value).is_some() ||
75                                searcher.find_first_in(&key).is_some()
76                            {
77                                hits += 1;
78                                return result()
79                            }
80                        }
81                        None => {
82                            hits += 1;
83                            return result()
84                        }
85                    }
86                }
87                None
88            };
89
90            if filter.reverse {
91                Ok(cursor
92                    .walk_back(None)?
93                    .skip(filter.skip)
94                    .filter_map(map_filter)
95                    .take(filter.len)
96                    .collect::<Vec<(_, _)>>())
97            } else {
98                Ok(cursor
99                    .walk(None)?
100                    .skip(filter.skip)
101                    .filter_map(map_filter)
102                    .take(filter.len)
103                    .collect::<Vec<(_, _)>>())
104            }
105        })?;
106
107        Ok((data.map_err(|e: DatabaseError| eyre::eyre!(e))?, hits))
108    }
109}
110
111impl<N: ProviderNodeTypes> DbTool<N> {
112    /// Takes a DB where the tables have already been created.
113    pub fn new(provider_factory: ProviderFactory<N>) -> eyre::Result<Self> {
114        // Disable timeout because we are entering a TUI which might read for a long time. We
115        // disable on the [`DbTool`] level since it's only used in the CLI.
116        provider_factory.provider()?.disable_long_read_transaction_safety();
117        Ok(Self { provider_factory })
118    }
119
120    /// Grabs the content of the table for the given key
121    pub fn get<T: Table>(&self, key: T::Key) -> Result<Option<T::Value>> {
122        self.provider_factory.db_ref().view(|tx| tx.get::<T>(key))?.map_err(|e| eyre::eyre!(e))
123    }
124
125    /// Returns the first value for `key` whose subkey is greater than or equal to `subkey`,
126    /// or `None` if there is no such value.
127    pub fn get_dup<T: DupSort>(&self, key: T::Key, subkey: T::SubKey) -> Result<Option<T::Value>> {
128        self.provider_factory
129            .db_ref()
130            .view(|tx| tx.cursor_dup_read::<T>()?.seek_by_key_subkey(key, subkey))?
131            .map_err(|e| eyre::eyre!(e))
132    }
133
134    /// Drops the database, the static files and ExEx WAL at the given paths.
135    pub fn drop<P: AsRef<Path>>(
136        &self,
137        db_path: P,
138        static_files_path: P,
139        exex_wal_path: P,
140    ) -> Result<()> {
141        let db_path = db_path.as_ref();
142        info!(target: "reth::cli", "Dropping database at {:?}", db_path);
143        fs::remove_dir_all(db_path)?;
144
145        let static_files_path = static_files_path.as_ref();
146        info!(target: "reth::cli", "Dropping static files at {:?}", static_files_path);
147        fs::remove_dir_all(static_files_path)?;
148        fs::create_dir_all(static_files_path)?;
149
150        if exex_wal_path.as_ref().exists() {
151            let exex_wal_path = exex_wal_path.as_ref();
152            info!(target: "reth::cli", "Dropping ExEx WAL at {:?}", exex_wal_path);
153            fs::remove_dir_all(exex_wal_path)?;
154        }
155
156        Ok(())
157    }
158
159    /// Drops the provided table from the database.
160    pub fn drop_table<T: Table>(&self) -> Result<()> {
161        self.provider_factory.db_ref().update(|tx| tx.clear::<T>())??;
162        Ok(())
163    }
164}
165
166/// Filters the results coming from the database.
167#[derive(Debug)]
168pub struct ListFilter {
169    /// Skip first N entries.
170    pub skip: usize,
171    /// Take N entries.
172    pub len: usize,
173    /// Sequence of bytes that will be searched on values and keys from the database.
174    pub search: Vec<u8>,
175    /// Minimum row size.
176    pub min_row_size: usize,
177    /// Minimum key size.
178    pub min_key_size: usize,
179    /// Minimum value size.
180    pub min_value_size: usize,
181    /// Reverse order of entries.
182    pub reverse: bool,
183    /// Only counts the number of filtered entries without decoding and returning them.
184    pub only_count: bool,
185}
186
187impl ListFilter {
188    /// If `search` has a list of bytes, then filter for rows that have this sequence.
189    pub const fn has_search(&self) -> bool {
190        !self.search.is_empty()
191    }
192
193    /// Updates the page with new `skip` and `len` values.
194    pub const fn update_page(&mut self, skip: usize, len: usize) {
195        self.skip = skip;
196        self.len = len;
197    }
198}