chore: use Table::TABLE in more places (#6563)

This commit is contained in:
DaniPopes
2024-02-12 16:53:16 +02:00
committed by GitHub
parent 2a5efb2a99
commit 613b6f08a2
7 changed files with 39 additions and 46 deletions

View File

@ -158,19 +158,19 @@ where
T::Key: Hash,
T::Value: PartialEq,
{
let table_name = T::NAME;
let table = T::TABLE;
info!("Analyzing table {table_name}...");
info!("Analyzing table {table}...");
let result = find_diffs_advanced::<T>(&primary_tx, &secondary_tx)?;
info!("Done analyzing table {table_name}!");
info!("Done analyzing table {table}!");
// Pretty info summary header: newline then header
info!("");
info!("Diff results for {table_name}:");
info!("Diff results for {table}:");
// create directory and open file
fs::create_dir_all(output_dir.as_ref())?;
let file_name = format!("{table_name}.txt");
let file_name = format!("{table}.txt");
let mut file = File::create(output_dir.as_ref().join(file_name.clone()))?;
// analyze the result and print some stats
@ -178,36 +178,36 @@ where
let extra_elements = result.extra_elements.len();
// Make a pretty summary header for the table
writeln!(file, "Diff results for {table_name}")?;
writeln!(file, "Diff results for {table}")?;
if discrepancies > 0 {
// write to file
writeln!(file, "Found {discrepancies} discrepancies in table {table_name}")?;
writeln!(file, "Found {discrepancies} discrepancies in table {table}")?;
// also print to info
info!("Found {discrepancies} discrepancies in table {table_name}");
info!("Found {discrepancies} discrepancies in table {table}");
} else {
// write to file
writeln!(file, "No discrepancies found in table {table_name}")?;
writeln!(file, "No discrepancies found in table {table}")?;
// also print to info
info!("No discrepancies found in table {table_name}");
info!("No discrepancies found in table {table}");
}
if extra_elements > 0 {
// write to file
writeln!(file, "Found {extra_elements} extra elements in table {table_name}")?;
writeln!(file, "Found {extra_elements} extra elements in table {table}")?;
// also print to info
info!("Found {extra_elements} extra elements in table {table_name}");
info!("Found {extra_elements} extra elements in table {table}");
} else {
writeln!(file, "No extra elements found in table {table_name}")?;
writeln!(file, "No extra elements found in table {table}")?;
// also print to info
info!("No extra elements found in table {table_name}");
info!("No extra elements found in table {table}");
}
info!("Writing diff results for {table_name} to {file_name}...");
info!("Writing diff results for {table} to {file_name}...");
if discrepancies > 0 {
writeln!(file, "Discrepancies:")?;
@ -226,7 +226,7 @@ where
}
let full_file_name = output_dir.as_ref().join(file_name);
info!("Done writing diff results for {table_name} to {}", full_file_name.display());
info!("Done writing diff results for {table} to {}", full_file_name.display());
Ok(())
}

View File

@ -31,23 +31,20 @@ pub struct Command {
impl Command {
/// Execute `db get` command
pub fn execute<DB: Database>(self, tool: &DbTool<'_, DB>) -> eyre::Result<()> {
self.table.view(&GetValueViewer { tool, args: &self })?;
Ok(())
self.table.view(&GetValueViewer { tool, args: &self })
}
/// Get an instance of key for given table
pub fn table_key<T: Table>(&self) -> Result<T::Key, eyre::Error> {
assert_eq!(T::NAME, self.table.name());
serde_json::from_str::<T::Key>(&self.key).map_err(|e| eyre::eyre!(e))
assert_eq!(T::TABLE, self.table);
serde_json::from_str::<T::Key>(&self.key).map_err(Into::into)
}
/// Get an instance of subkey for given dupsort table
fn table_subkey<T: DupSort>(&self) -> Result<T::SubKey, eyre::Error> {
assert_eq!(T::NAME, self.table.name());
assert_eq!(T::TABLE, self.table);
serde_json::from_str::<T::SubKey>(&self.subkey.clone().unwrap_or_default())
.map_err(|e| eyre::eyre!(e))
.map_err(Into::into)
}
}

View File

@ -102,7 +102,7 @@ impl Headers {
|_| false,
|row| last_pruned_block = row.0,
)?;
trace!(target: "pruner", %pruned, %done, table = %T::NAME, "Pruned headers");
trace!(target: "pruner", %pruned, %done, table = %T::TABLE, "Pruned headers");
Ok((done, pruned, last_pruned_block))
}

View File

@ -52,7 +52,7 @@ impl<K: TransactionKind, T: Table> Cursor<K, T> {
f: impl FnOnce(&mut Self) -> R,
) -> R {
if let Some(metrics) = self.metrics.as_ref().cloned() {
metrics.record_operation(T::NAME, operation, value_size, || f(self))
metrics.record_operation(T::TABLE, operation, value_size, || f(self))
} else {
f(self)
}

View File

@ -17,7 +17,6 @@ use reth_tracing::tracing::{trace, warn};
use std::{
backtrace::Backtrace,
marker::PhantomData,
str::FromStr,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
@ -74,7 +73,7 @@ impl<K: TransactionKind> Tx<K> {
pub fn get_dbi<T: Table>(&self) -> Result<DBI, DatabaseError> {
let mut handles = self.db_handles.write();
let table = Tables::from_str(T::NAME).expect("Requested table should be part of `Tables`.");
let table = T::TABLE;
let dbi_handle = handles.get_mut(table as usize).expect("should exist");
if dbi_handle.is_none() {
@ -145,7 +144,7 @@ impl<K: TransactionKind> Tx<K> {
metrics_handler.log_backtrace_on_long_read_transaction();
metrics_handler
.env_metrics
.record_operation(T::NAME, operation, value_size, || f(&self.inner))
.record_operation(T::TABLE, operation, value_size, || f(&self.inner))
} else {
f(&self.inner)
}

View File

@ -6,7 +6,6 @@ use reth_metrics::{metrics::Counter, Metrics};
use rustc_hash::FxHasher;
use std::{
hash::BuildHasherDefault,
str::FromStr,
time::{Duration, Instant},
};
use strum::EnumCount;
@ -34,20 +33,17 @@ impl DatabaseEnvMetrics {
/// Record a metric for database operation executed in `f`. Panics if the table name is unknown.
pub(crate) fn record_operation<R>(
&self,
table: &'static str,
table: Tables,
operation: Operation,
value_size: Option<usize>,
f: impl FnOnce() -> R,
) -> R {
let handle = self
.operations
.entry((Tables::from_str(table).expect("unknown table name"), operation))
.or_insert_with(|| {
OperationMetrics::new_with_labels(&[
(Labels::Table.as_str(), table),
(Labels::Operation.as_str(), operation.as_str()),
])
});
let handle = self.operations.entry((table, operation)).or_insert_with(|| {
OperationMetrics::new_with_labels(&[
(Labels::Table.as_str(), table.name()),
(Labels::Operation.as_str(), operation.as_str()),
])
});
handle.record(value_size, f)
}
}

View File

@ -60,10 +60,6 @@ pub enum TableType {
/// table::{DupSort, Table},
/// TableViewer, Tables,
/// };
/// use std::str::FromStr;
///
/// let headers = Tables::from_str("Headers").unwrap();
/// let transactions = Tables::from_str("Transactions").unwrap();
///
/// struct MyTableViewer;
///
@ -83,8 +79,8 @@ pub enum TableType {
///
/// let viewer = MyTableViewer {};
///
/// let _ = headers.view(&viewer);
/// let _ = transactions.view(&viewer);
/// let _ = Tables::Headers.view(&viewer);
/// let _ = Tables::Transactions.view(&viewer);
/// ```
pub trait TableViewer<R> {
/// The error type returned by the viewer.
@ -144,6 +140,11 @@ macro_rules! tables {
)?
)*
// Tables enum.
// NOTE: the ordering of the enum does not matter, but it is assumed that the discriminants
// start at 0 and increment by 1 for each variant (the default behavior).
// See for example `reth_db::implementation::mdbx::tx::Tx::db_handles`.
/// A table in the database.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub enum Tables {