mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 19:09:54 +00:00
fix: make reth-db compile without default features (#8509)
This commit is contained in:
@ -20,7 +20,7 @@ pub type ValueOnlyResult<T> = Result<Option<<T as Table>::Value>, DatabaseError>
|
|||||||
|
|
||||||
// Sealed trait helper to prevent misuse of the Database API.
|
// Sealed trait helper to prevent misuse of the Database API.
|
||||||
mod sealed {
|
mod sealed {
|
||||||
use crate::{database::Database, mock::DatabaseMock, DatabaseEnv};
|
use crate::{database::Database, mock::DatabaseMock};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
/// Sealed trait to limit the implementers of the Database trait.
|
/// Sealed trait to limit the implementers of the Database trait.
|
||||||
@ -28,7 +28,9 @@ mod sealed {
|
|||||||
|
|
||||||
impl<DB: Database> Sealed for &DB {}
|
impl<DB: Database> Sealed for &DB {}
|
||||||
impl<DB: Database> Sealed for Arc<DB> {}
|
impl<DB: Database> Sealed for Arc<DB> {}
|
||||||
impl Sealed for DatabaseEnv {}
|
|
||||||
|
#[cfg(feature = "mdbx")]
|
||||||
|
impl Sealed for crate::DatabaseEnv {}
|
||||||
impl Sealed for DatabaseMock {}
|
impl Sealed for DatabaseMock {}
|
||||||
|
|
||||||
#[cfg(any(test, feature = "test-utils"))]
|
#[cfg(any(test, feature = "test-utils"))]
|
||||||
|
|||||||
@ -52,7 +52,7 @@ impl<K: TransactionKind> Tx<K> {
|
|||||||
/// Creates new `Tx` object with a `RO` or `RW` transaction and optionally enables metrics.
|
/// Creates new `Tx` object with a `RO` or `RW` transaction and optionally enables metrics.
|
||||||
#[inline]
|
#[inline]
|
||||||
#[track_caller]
|
#[track_caller]
|
||||||
pub fn new_with_metrics(
|
pub(crate) fn new_with_metrics(
|
||||||
inner: Transaction<K>,
|
inner: Transaction<K>,
|
||||||
env_metrics: Option<Arc<DatabaseEnvMetrics>>,
|
env_metrics: Option<Arc<DatabaseEnvMetrics>>,
|
||||||
) -> reth_libmdbx::Result<Self> {
|
) -> reth_libmdbx::Result<Self> {
|
||||||
|
|||||||
@ -74,11 +74,7 @@ mod utils;
|
|||||||
pub mod version;
|
pub mod version;
|
||||||
|
|
||||||
#[cfg(feature = "mdbx")]
|
#[cfg(feature = "mdbx")]
|
||||||
/// Bindings for [MDBX](https://libmdbx.dqdkfa.ru/).
|
pub mod mdbx;
|
||||||
pub mod mdbx {
|
|
||||||
pub use crate::implementation::mdbx::*;
|
|
||||||
pub use reth_libmdbx::*;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub use abstraction::*;
|
pub use abstraction::*;
|
||||||
pub use reth_storage_errors::db::{DatabaseError, DatabaseWriteOperation};
|
pub use reth_storage_errors::db::{DatabaseError, DatabaseWriteOperation};
|
||||||
@ -86,85 +82,7 @@ pub use tables::*;
|
|||||||
pub use utils::is_database_empty;
|
pub use utils::is_database_empty;
|
||||||
|
|
||||||
#[cfg(feature = "mdbx")]
|
#[cfg(feature = "mdbx")]
|
||||||
pub use mdbx::{DatabaseEnv, DatabaseEnvKind};
|
pub use mdbx::{create_db, init_db, open_db, open_db_read_only, DatabaseEnv, DatabaseEnvKind};
|
||||||
|
|
||||||
use crate::mdbx::DatabaseArguments;
|
|
||||||
use eyre::WrapErr;
|
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
/// Creates a new database at the specified path if it doesn't exist. Does NOT create tables. Check
|
|
||||||
/// [`init_db`].
|
|
||||||
pub fn create_db<P: AsRef<Path>>(path: P, args: DatabaseArguments) -> eyre::Result<DatabaseEnv> {
|
|
||||||
use crate::version::{check_db_version_file, create_db_version_file, DatabaseVersionError};
|
|
||||||
|
|
||||||
let rpath = path.as_ref();
|
|
||||||
if is_database_empty(rpath) {
|
|
||||||
reth_fs_util::create_dir_all(rpath)
|
|
||||||
.wrap_err_with(|| format!("Could not create database directory {}", rpath.display()))?;
|
|
||||||
create_db_version_file(rpath)?;
|
|
||||||
} else {
|
|
||||||
match check_db_version_file(rpath) {
|
|
||||||
Ok(_) => (),
|
|
||||||
Err(DatabaseVersionError::MissingFile) => create_db_version_file(rpath)?,
|
|
||||||
Err(err) => return Err(err.into()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "mdbx")]
|
|
||||||
{
|
|
||||||
Ok(DatabaseEnv::open(rpath, DatabaseEnvKind::RW, args)?)
|
|
||||||
}
|
|
||||||
#[cfg(not(feature = "mdbx"))]
|
|
||||||
{
|
|
||||||
unimplemented!();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Opens up an existing database or creates a new one at the specified path. Creates tables if
|
|
||||||
/// necessary. Read/Write mode.
|
|
||||||
pub fn init_db<P: AsRef<Path>>(path: P, args: DatabaseArguments) -> eyre::Result<DatabaseEnv> {
|
|
||||||
#[cfg(feature = "mdbx")]
|
|
||||||
{
|
|
||||||
let client_version = args.client_version().clone();
|
|
||||||
let db = create_db(path, args)?;
|
|
||||||
db.create_tables()?;
|
|
||||||
db.record_client_version(client_version)?;
|
|
||||||
Ok(db)
|
|
||||||
}
|
|
||||||
#[cfg(not(feature = "mdbx"))]
|
|
||||||
{
|
|
||||||
unimplemented!();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Opens up an existing database. Read only mode. It doesn't create it or create tables if missing.
|
|
||||||
pub fn open_db_read_only(path: &Path, args: DatabaseArguments) -> eyre::Result<DatabaseEnv> {
|
|
||||||
#[cfg(feature = "mdbx")]
|
|
||||||
{
|
|
||||||
DatabaseEnv::open(path, DatabaseEnvKind::RO, args)
|
|
||||||
.with_context(|| format!("Could not open database at path: {}", path.display()))
|
|
||||||
}
|
|
||||||
#[cfg(not(feature = "mdbx"))]
|
|
||||||
{
|
|
||||||
unimplemented!();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Opens up an existing database. Read/Write mode with WriteMap enabled. It doesn't create it or
|
|
||||||
/// create tables if missing.
|
|
||||||
pub fn open_db(path: &Path, args: DatabaseArguments) -> eyre::Result<DatabaseEnv> {
|
|
||||||
#[cfg(feature = "mdbx")]
|
|
||||||
{
|
|
||||||
let db = DatabaseEnv::open(path, DatabaseEnvKind::RW, args.clone())
|
|
||||||
.with_context(|| format!("Could not open database at path: {}", path.display()))?;
|
|
||||||
db.record_client_version(args.client_version().clone())?;
|
|
||||||
Ok(db)
|
|
||||||
}
|
|
||||||
#[cfg(not(feature = "mdbx"))]
|
|
||||||
{
|
|
||||||
unimplemented!();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Collection of database test utilities
|
/// Collection of database test utilities
|
||||||
#[cfg(any(test, feature = "test-utils"))]
|
#[cfg(any(test, feature = "test-utils"))]
|
||||||
@ -173,11 +91,15 @@ pub mod test_utils {
|
|||||||
use crate::{
|
use crate::{
|
||||||
database::Database,
|
database::Database,
|
||||||
database_metrics::{DatabaseMetadata, DatabaseMetadataValue, DatabaseMetrics},
|
database_metrics::{DatabaseMetadata, DatabaseMetadataValue, DatabaseMetrics},
|
||||||
|
mdbx::DatabaseArguments,
|
||||||
models::client_version::ClientVersion,
|
models::client_version::ClientVersion,
|
||||||
};
|
};
|
||||||
use reth_fs_util;
|
use reth_fs_util;
|
||||||
use reth_libmdbx::MaxReadTransactionDuration;
|
use reth_libmdbx::MaxReadTransactionDuration;
|
||||||
use std::{path::PathBuf, sync::Arc};
|
use std::{
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
sync::Arc,
|
||||||
|
};
|
||||||
use tempfile::TempDir;
|
use tempfile::TempDir;
|
||||||
|
|
||||||
/// Error during database open
|
/// Error during database open
|
||||||
|
|||||||
58
crates/storage/db/src/mdbx.rs
Normal file
58
crates/storage/db/src/mdbx.rs
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
//! Bindings for [MDBX](https://libmdbx.dqdkfa.ru/).
|
||||||
|
|
||||||
|
pub use crate::implementation::mdbx::*;
|
||||||
|
pub use reth_libmdbx::*;
|
||||||
|
|
||||||
|
use crate::is_database_empty;
|
||||||
|
use eyre::Context;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
/// Creates a new database at the specified path if it doesn't exist. Does NOT create tables. Check
|
||||||
|
/// [`init_db`].
|
||||||
|
pub fn create_db<P: AsRef<Path>>(path: P, args: DatabaseArguments) -> eyre::Result<DatabaseEnv> {
|
||||||
|
use crate::version::{check_db_version_file, create_db_version_file, DatabaseVersionError};
|
||||||
|
|
||||||
|
let rpath = path.as_ref();
|
||||||
|
if is_database_empty(rpath) {
|
||||||
|
reth_fs_util::create_dir_all(rpath)
|
||||||
|
.wrap_err_with(|| format!("Could not create database directory {}", rpath.display()))?;
|
||||||
|
create_db_version_file(rpath)?;
|
||||||
|
} else {
|
||||||
|
match check_db_version_file(rpath) {
|
||||||
|
Ok(_) => (),
|
||||||
|
Err(DatabaseVersionError::MissingFile) => create_db_version_file(rpath)?,
|
||||||
|
Err(err) => return Err(err.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(DatabaseEnv::open(rpath, DatabaseEnvKind::RW, args)?)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Opens up an existing database or creates a new one at the specified path. Creates tables if
|
||||||
|
/// necessary. Read/Write mode.
|
||||||
|
pub fn init_db<P: AsRef<Path>>(path: P, args: DatabaseArguments) -> eyre::Result<DatabaseEnv> {
|
||||||
|
{
|
||||||
|
let client_version = args.client_version().clone();
|
||||||
|
let db = create_db(path, args)?;
|
||||||
|
db.create_tables()?;
|
||||||
|
db.record_client_version(client_version)?;
|
||||||
|
Ok(db)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Opens up an existing database. Read only mode. It doesn't create it or create tables if missing.
|
||||||
|
pub fn open_db_read_only(path: &Path, args: DatabaseArguments) -> eyre::Result<DatabaseEnv> {
|
||||||
|
{
|
||||||
|
DatabaseEnv::open(path, DatabaseEnvKind::RO, args)
|
||||||
|
.with_context(|| format!("Could not open database at path: {}", path.display()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Opens up an existing database. Read/Write mode with WriteMap enabled. It doesn't create it or
|
||||||
|
/// create tables if missing.
|
||||||
|
pub fn open_db(path: &Path, args: DatabaseArguments) -> eyre::Result<DatabaseEnv> {
|
||||||
|
let db = DatabaseEnv::open(path, DatabaseEnvKind::RW, args.clone())
|
||||||
|
.with_context(|| format!("Could not open database at path: {}", path.display()))?;
|
||||||
|
db.record_client_version(args.client_version().clone())?;
|
||||||
|
Ok(db)
|
||||||
|
}
|
||||||
@ -1,6 +1,5 @@
|
|||||||
use crate::Tables;
|
use crate::Tables;
|
||||||
use metrics::{Gauge, Histogram};
|
use metrics::{Gauge, Histogram};
|
||||||
use reth_libmdbx::CommitLatency;
|
|
||||||
use reth_metrics::{metrics::Counter, Metrics};
|
use reth_metrics::{metrics::Counter, Metrics};
|
||||||
use rustc_hash::{FxHashMap, FxHasher};
|
use rustc_hash::{FxHashMap, FxHasher};
|
||||||
use std::{
|
use std::{
|
||||||
@ -18,7 +17,7 @@ const LARGE_VALUE_THRESHOLD_BYTES: usize = 4096;
|
|||||||
/// Requires a metric recorder to be registered before creating an instance of this struct.
|
/// Requires a metric recorder to be registered before creating an instance of this struct.
|
||||||
/// Otherwise, metric recording will no-op.
|
/// Otherwise, metric recording will no-op.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct DatabaseEnvMetrics {
|
pub(crate) struct DatabaseEnvMetrics {
|
||||||
/// Caches OperationMetrics handles for each table and operation tuple.
|
/// Caches OperationMetrics handles for each table and operation tuple.
|
||||||
operations: FxHashMap<(Tables, Operation), OperationMetrics>,
|
operations: FxHashMap<(Tables, Operation), OperationMetrics>,
|
||||||
/// Caches TransactionMetrics handles for counters grouped by only transaction mode.
|
/// Caches TransactionMetrics handles for counters grouped by only transaction mode.
|
||||||
@ -124,13 +123,14 @@ impl DatabaseEnvMetrics {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Record metrics for closing a database transactions.
|
/// Record metrics for closing a database transactions.
|
||||||
|
#[cfg(feature = "mdbx")]
|
||||||
pub(crate) fn record_closed_transaction(
|
pub(crate) fn record_closed_transaction(
|
||||||
&self,
|
&self,
|
||||||
mode: TransactionMode,
|
mode: TransactionMode,
|
||||||
outcome: TransactionOutcome,
|
outcome: TransactionOutcome,
|
||||||
open_duration: Duration,
|
open_duration: Duration,
|
||||||
close_duration: Option<Duration>,
|
close_duration: Option<Duration>,
|
||||||
commit_latency: Option<CommitLatency>,
|
commit_latency: Option<reth_libmdbx::CommitLatency>,
|
||||||
) {
|
) {
|
||||||
self.transactions
|
self.transactions
|
||||||
.get(&mode)
|
.get(&mode)
|
||||||
@ -152,6 +152,7 @@ pub(crate) enum TransactionMode {
|
|||||||
/// Read-write transaction mode.
|
/// Read-write transaction mode.
|
||||||
ReadWrite,
|
ReadWrite,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TransactionMode {
|
impl TransactionMode {
|
||||||
/// Returns the transaction mode as a string.
|
/// Returns the transaction mode as a string.
|
||||||
pub(crate) const fn as_str(&self) -> &'static str {
|
pub(crate) const fn as_str(&self) -> &'static str {
|
||||||
@ -304,11 +305,12 @@ pub(crate) struct TransactionOutcomeMetrics {
|
|||||||
impl TransactionOutcomeMetrics {
|
impl TransactionOutcomeMetrics {
|
||||||
/// Record transaction closing with the duration it was open and the duration it took to close
|
/// Record transaction closing with the duration it was open and the duration it took to close
|
||||||
/// it.
|
/// it.
|
||||||
|
#[cfg(feature = "mdbx")]
|
||||||
pub(crate) fn record(
|
pub(crate) fn record(
|
||||||
&self,
|
&self,
|
||||||
open_duration: Duration,
|
open_duration: Duration,
|
||||||
close_duration: Option<Duration>,
|
close_duration: Option<Duration>,
|
||||||
commit_latency: Option<CommitLatency>,
|
commit_latency: Option<reth_libmdbx::CommitLatency>,
|
||||||
) {
|
) {
|
||||||
self.open_duration_seconds.record(open_duration);
|
self.open_duration_seconds.record(open_duration);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user