mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat(db): initialize db with tables (#13130)
Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
@ -100,6 +100,15 @@ pub trait Table: Send + Sync + Debug + 'static {
|
||||
type Value: Value;
|
||||
}
|
||||
|
||||
/// Trait that provides object-safe access to the table's metadata.
|
||||
pub trait TableInfo: Send + Sync + Debug + 'static {
|
||||
/// The table's name.
|
||||
fn name(&self) -> &'static str;
|
||||
|
||||
/// Whether the table is a `DUPSORT` table.
|
||||
fn is_dupsort(&self) -> bool;
|
||||
}
|
||||
|
||||
/// Tuple with `T::Key` and `T::Value`.
|
||||
pub type TableRow<T> = (<T as Table>::Key, <T as Table>::Value);
|
||||
|
||||
|
||||
@ -3,9 +3,9 @@
|
||||
use crate::{
|
||||
lockfile::StorageLock,
|
||||
metrics::DatabaseEnvMetrics,
|
||||
tables::{self, TableType, Tables},
|
||||
tables::{self, Tables},
|
||||
utils::default_page_size,
|
||||
DatabaseError,
|
||||
DatabaseError, TableSet,
|
||||
};
|
||||
use eyre::Context;
|
||||
use metrics::{gauge, Label};
|
||||
@ -444,15 +444,18 @@ impl DatabaseEnv {
|
||||
self
|
||||
}
|
||||
|
||||
/// Creates all the defined tables, if necessary.
|
||||
/// Creates all the tables defined in [`Tables`], if necessary.
|
||||
pub fn create_tables(&self) -> Result<(), DatabaseError> {
|
||||
self.create_tables_for::<Tables>()
|
||||
}
|
||||
|
||||
/// Creates all the tables defined in the given [`TableSet`], if necessary.
|
||||
pub fn create_tables_for<TS: TableSet>(&self) -> Result<(), DatabaseError> {
|
||||
let tx = self.inner.begin_rw_txn().map_err(|e| DatabaseError::InitTx(e.into()))?;
|
||||
|
||||
for table in Tables::ALL {
|
||||
let flags = match table.table_type() {
|
||||
TableType::Table => DatabaseFlags::default(),
|
||||
TableType::DupSort => DatabaseFlags::DUP_SORT,
|
||||
};
|
||||
for table in TS::tables() {
|
||||
let flags =
|
||||
if table.is_dupsort() { DatabaseFlags::DUP_SORT } else { DatabaseFlags::default() };
|
||||
|
||||
tx.create_db(Some(table.name()), flags)
|
||||
.map_err(|e| DatabaseError::CreateTable(e.into()))?;
|
||||
|
||||
@ -29,7 +29,7 @@ use reth_db_api::{
|
||||
AccountBeforeTx, ClientVersion, CompactU256, IntegerList, ShardedKey,
|
||||
StoredBlockBodyIndices, StoredBlockWithdrawals,
|
||||
},
|
||||
table::{Decode, DupSort, Encode, Table},
|
||||
table::{Decode, DupSort, Encode, Table, TableInfo},
|
||||
};
|
||||
use reth_primitives::{Receipt, StorageEntry, TransactionSigned};
|
||||
use reth_primitives_traits::{Account, Bytecode};
|
||||
@ -101,10 +101,8 @@ pub trait TableViewer<R> {
|
||||
/// General trait for defining the set of tables
|
||||
/// Used to initialize database
|
||||
pub trait TableSet {
|
||||
/// Returns all the table names in the database.
|
||||
fn table_names(&self) -> Vec<&'static str>;
|
||||
/// Returns `true` if the table at the given index is a `DUPSORT` table.
|
||||
fn is_dupsort(&self, idx: usize) -> bool;
|
||||
/// Returns an iterator over the tables
|
||||
fn tables() -> Box<dyn Iterator<Item = Box<dyn TableInfo>>>;
|
||||
}
|
||||
|
||||
/// Defines all the tables in the database.
|
||||
@ -252,15 +250,19 @@ macro_rules! tables {
|
||||
}
|
||||
}
|
||||
|
||||
impl TableSet for Tables {
|
||||
fn table_names(&self) -> Vec<&'static str> {
|
||||
//vec![$(table_names::$name,)*]
|
||||
Self::ALL.iter().map(|t| t.name()).collect()
|
||||
impl TableInfo for Tables {
|
||||
fn name(&self) -> &'static str {
|
||||
self.name()
|
||||
}
|
||||
|
||||
fn is_dupsort(&self, idx: usize) -> bool {
|
||||
let table: Self = self.table_names()[idx].parse().expect("should be valid table name");
|
||||
table.is_dupsort()
|
||||
fn is_dupsort(&self) -> bool {
|
||||
self.is_dupsort()
|
||||
}
|
||||
}
|
||||
|
||||
impl TableSet for Tables {
|
||||
fn tables() -> Box<dyn Iterator<Item = Box<dyn TableInfo>>> {
|
||||
Box::new(Self::ALL.iter().map(|table| Box::new(*table) as Box<dyn TableInfo>))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user