Skip to content

Commit

Permalink
refactor(db): unify table macros (paradigmxyz#6552)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Feb 12, 2024
1 parent 17eca36 commit 2a5efb2
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 265 deletions.
6 changes: 3 additions & 3 deletions bin/reth/src/commands/db/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ impl Command {
DatabaseArguments::default().log_level(self.second_db.log_level),
)?;

let tables = match self.table {
Some(table) => vec![table],
None => Tables::ALL.to_vec(),
let tables = match &self.table {
Some(table) => std::slice::from_ref(table),
None => Tables::ALL,
};

for table in tables {
Expand Down
9 changes: 7 additions & 2 deletions crates/storage/db/src/abstraction/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,17 @@ impl<T> Value for T where T: Compress + Decompress + Serialize {}
/// It allows for the use of codecs. See [`crate::models::ShardedKey`] for a custom
/// implementation.
pub trait Table: Send + Sync + Debug + 'static {
/// Return table name as it is present inside the MDBX.
const NAME: &'static str;
/// The dynamic type of the table.
const TABLE: crate::Tables;

/// The table's name.
const NAME: &'static str = Self::TABLE.name();

/// Key element of `Table`.
///
/// Sorting should be taken into account when encoding this.
type Key: Key;

/// Value element of `Table`.
type Value: Value;
}
Expand Down
4 changes: 2 additions & 2 deletions crates/storage/db/src/implementation/mdbx/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
DatabaseEnvMetrics, Operation, TransactionMetrics, TransactionMode, TransactionOutcome,
},
table::{Compress, DupSort, Encode, Table, TableImporter},
tables::{utils::decode_one, Tables, NUM_TABLES},
tables::{utils::decode_one, Tables},
transaction::{DbTx, DbTxMut},
DatabaseError,
};
Expand Down Expand Up @@ -34,7 +34,7 @@ pub struct Tx<K: TransactionKind> {
/// Libmdbx-sys transaction.
pub inner: Transaction<K>,
/// Database table handle cache.
pub(crate) db_handles: Arc<RwLock<[Option<DBI>; NUM_TABLES]>>,
pub(crate) db_handles: Arc<RwLock<[Option<DBI>; Tables::COUNT]>>,
/// Handler for metrics with its own [Drop] implementation for cases when the transaction isn't
/// closed by [Tx::commit] or [Tx::abort], but we still need to report it in the metrics.
///
Expand Down
4 changes: 2 additions & 2 deletions crates/storage/db/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Tables, NUM_TABLES};
use crate::Tables;
use dashmap::DashMap;
use metrics::{Gauge, Histogram};
use reth_libmdbx::CommitLatency;
Expand All @@ -25,7 +25,7 @@ impl DatabaseEnvMetrics {
pub(crate) fn new() -> Self {
Self {
operations: DashMap::with_capacity_and_hasher(
NUM_TABLES * Operation::COUNT,
Tables::COUNT * Operation::COUNT,
BuildHasherDefault::<FxHasher>::default(),
),
}
Expand Down
Loading

0 comments on commit 2a5efb2

Please sign in to comment.