diff --git a/storage/blockchain/README.md b/storage/blockchain/README.md index 492c719..6c2a177 100644 --- a/storage/blockchain/README.md +++ b/storage/blockchain/README.md @@ -66,13 +66,14 @@ use cuprate_blockchain::{ # fn main() -> Result<(), Box> { // Create a configuration for the database environment. -let db_dir = tempfile::tempdir()?; +let tmp_dir = tempfile::tempdir()?; +let db_dir = tmp_dir.path().to_owned(); let config = ConfigBuilder::new() - .db_directory(db_dir.path().to_path_buf()) + .db_directory(db_dir.into()) .build(); // Initialize the database environment. -let env = ConcreteEnv::open(config)?; +let env = cuprate_blockchain::open(config)?; // Open up a transaction + tables for writing. let env_inner = env.env_inner(); diff --git a/storage/blockchain/src/config/config.rs b/storage/blockchain/src/config/config.rs index 8528003..c58e292 100644 --- a/storage/blockchain/src/config/config.rs +++ b/storage/blockchain/src/config/config.rs @@ -77,6 +77,27 @@ impl ConfigBuilder { self } + /// Calls [`cuprate_database::config::ConfigBuilder::sync_mode`]. + #[must_use] + pub fn sync_mode(mut self, sync_mode: SyncMode) -> Self { + self.db_config = self.db_config.sync_mode(sync_mode); + self + } + + /// Calls [`cuprate_database::config::ConfigBuilder::resize_algorithm`]. + #[must_use] + pub fn resize_algorithm(mut self, resize_algorithm: ResizeAlgorithm) -> Self { + self.db_config = self.db_config.resize_algorithm(resize_algorithm); + self + } + + /// Set a custom [`ReaderThreads`]. + #[must_use] + pub const fn reader_threads(mut self, reader_threads: ReaderThreads) -> Self { + self.reader_threads = Some(reader_threads); + self + } + /// Tune the [`ConfigBuilder`] for the highest performing, /// but also most resource-intensive & maybe risky settings. /// @@ -85,8 +106,7 @@ impl ConfigBuilder { pub fn fast(mut self) -> Self { self.db_config = cuprate_database::config::ConfigBuilder::new(Cow::Borrowed(cuprate_blockchain_dir())) - .sync_mode(SyncMode::Fast) - .resize_algorithm(ResizeAlgorithm::default()); + .fast(); self.reader_threads = Some(ReaderThreads::OnePerThread); self @@ -100,19 +120,11 @@ impl ConfigBuilder { pub fn low_power(mut self) -> Self { self.db_config = cuprate_database::config::ConfigBuilder::new(Cow::Borrowed(cuprate_blockchain_dir())) - .sync_mode(SyncMode::default()) - .resize_algorithm(ResizeAlgorithm::default()); + .low_power(); self.reader_threads = Some(ReaderThreads::One); self } - - /// Set a custom [`ReaderThreads`]. - #[must_use] - pub const fn reader_threads(mut self, reader_threads: ReaderThreads) -> Self { - self.reader_threads = Some(reader_threads); - self - } } impl Default for ConfigBuilder { @@ -155,17 +167,23 @@ impl Config { /// Same as [`Config::default`]. /// /// ```rust - /// use cuprate_blockchain::{config::*, resize::*, DATABASE_DATA_FILENAME}; + /// use cuprate_database::{ + /// config::SyncMode, + /// resize::ResizeAlgorithm, + /// DATABASE_DATA_FILENAME, + /// }; /// use cuprate_helper::fs::*; /// + /// use cuprate_blockchain::config::*; + /// /// let config = Config::new(); /// - /// assert_eq!(config.db_directory(), cuprate_blockchain_dir()); - /// assert!(config.db_file().starts_with(cuprate_blockchain_dir())); - /// assert!(config.db_file().ends_with(DATABASE_DATA_FILENAME)); - /// assert_eq!(config.sync_mode, SyncMode::default()); + /// assert_eq!(config.db_config.db_directory(), cuprate_blockchain_dir()); + /// assert!(config.db_config.db_file().starts_with(cuprate_blockchain_dir())); + /// assert!(config.db_config.db_file().ends_with(DATABASE_DATA_FILENAME)); + /// assert_eq!(config.db_config.sync_mode, SyncMode::default()); + /// assert_eq!(config.db_config.resize_algorithm, ResizeAlgorithm::default()); /// assert_eq!(config.reader_threads, ReaderThreads::default()); - /// assert_eq!(config.resize_algorithm, ResizeAlgorithm::default()); /// ``` pub fn new() -> Self { ConfigBuilder::default().build() diff --git a/storage/blockchain/src/config/mod.rs b/storage/blockchain/src/config/mod.rs index 11897ca..fd3ff72 100644 --- a/storage/blockchain/src/config/mod.rs +++ b/storage/blockchain/src/config/mod.rs @@ -14,17 +14,17 @@ //! //! # Example //! ```rust -//! use cuprate_blockchain::{ -//! Env, -//! config::{ConfigBuilder, ReaderThreads, SyncMode} -//! }; +//! use cuprate_database::{Env, config::SyncMode}; +//! +//! use cuprate_blockchain::config::{ConfigBuilder, ReaderThreads}; //! //! # fn main() -> Result<(), Box> { -//! let db_dir = tempfile::tempdir()?; +//! let tmp_dir = tempfile::tempdir()?; +//! let db_dir = tmp_dir.path().to_owned(); //! //! let config = ConfigBuilder::new() //! // Use a custom database directory. -//! .db_directory(db_dir.path().to_path_buf()) +//! .db_directory(db_dir.into()) //! // Use as many reader threads as possible (when using `service`). //! .reader_threads(ReaderThreads::OnePerThread) //! // Use the fastest sync mode. @@ -35,7 +35,7 @@ //! // Start a database `service` using this configuration. //! let (reader_handle, _) = cuprate_blockchain::service::init(config.clone())?; //! // It's using the config we provided. -//! assert_eq!(reader_handle.env().config(), &config); +//! assert_eq!(reader_handle.env().config(), &config.db_config); //! # Ok(()) } //! ``` diff --git a/storage/blockchain/src/free.rs b/storage/blockchain/src/free.rs index 54003db..dac4e22 100644 --- a/storage/blockchain/src/free.rs +++ b/storage/blockchain/src/free.rs @@ -10,6 +10,8 @@ use crate::{config::Config, open_tables::OpenTables}; /// /// # Errors /// TODO +#[cold] +#[inline(never)] // only called once pub fn open(config: Config) -> Result { // Attempt to open the database environment. let env = ::open(config.db_config)?; diff --git a/storage/blockchain/src/ops/mod.rs b/storage/blockchain/src/ops/mod.rs index 125ab91..fc234fa 100644 --- a/storage/blockchain/src/ops/mod.rs +++ b/storage/blockchain/src/ops/mod.rs @@ -55,25 +55,29 @@ //! use hex_literal::hex; //! //! use cuprate_test_utils::data::block_v16_tx0; +//! use cuprate_database::{ +//! ConcreteEnv, +//! Env, EnvInner, +//! DatabaseRo, DatabaseRw, TxRo, TxRw, +//! }; //! //! use cuprate_blockchain::{ -//! ConcreteEnv, +//! OpenTables, //! config::ConfigBuilder, -//! Env, EnvInner, //! tables::{Tables, TablesMut}, -//! DatabaseRo, DatabaseRw, TxRo, TxRw, //! ops::block::{add_block, pop_block}, //! }; //! //! # fn main() -> Result<(), Box> { //! // Create a configuration for the database environment. -//! let db_dir = tempfile::tempdir()?; +//! let tmp_dir = tempfile::tempdir()?; +//! let db_dir = tmp_dir.path().to_owned(); //! let config = ConfigBuilder::new() -//! .db_directory(db_dir.path().to_path_buf()) +//! .db_directory(db_dir.into()) //! .build(); //! //! // Initialize the database environment. -//! let env = ConcreteEnv::open(config)?; +//! let env = cuprate_blockchain::open(config)?; //! //! // Open up a transaction + tables for writing. //! let env_inner = env.env_inner(); diff --git a/storage/blockchain/src/service/free.rs b/storage/blockchain/src/service/free.rs index acdb9f4..3ff8d6e 100644 --- a/storage/blockchain/src/service/free.rs +++ b/storage/blockchain/src/service/free.rs @@ -3,7 +3,7 @@ //---------------------------------------------------------------------------------------------------- Import use std::sync::Arc; -use cuprate_database::{ConcreteEnv, Env, InitError}; +use cuprate_database::InitError; use crate::{ config::Config, @@ -19,15 +19,12 @@ use crate::{ /// thread-pool and writer thread will exit automatically. /// /// # Errors -/// This will forward the error if [`Env::open`] failed. +/// This will forward the error if [`crate::open`] failed. pub fn init(config: Config) -> Result<(DatabaseReadHandle, DatabaseWriteHandle), InitError> { - let Config { - db_config, - reader_threads, - } = config; + let reader_threads = config.reader_threads; // Initialize the database itself. - let db = Arc::new(ConcreteEnv::open(db_config)?); + let db = Arc::new(crate::open(config)?); // Spawn the Reader thread pool and Writer. let readers = DatabaseReadHandle::init(&db, reader_threads); diff --git a/storage/blockchain/src/service/mod.rs b/storage/blockchain/src/service/mod.rs index e32935d..105a224 100644 --- a/storage/blockchain/src/service/mod.rs +++ b/storage/blockchain/src/service/mod.rs @@ -65,15 +65,17 @@ //! //! use cuprate_types::blockchain::{BCReadRequest, BCWriteRequest, BCResponse}; //! use cuprate_test_utils::data::block_v16_tx0; +//! use cuprate_database::Env; //! -//! use cuprate_blockchain::{ConcreteEnv, config::ConfigBuilder, Env}; +//! use cuprate_blockchain::config::ConfigBuilder; //! //! # #[tokio::main] //! # async fn main() -> Result<(), Box> { //! // Create a configuration for the database environment. -//! let db_dir = tempfile::tempdir()?; +//! let tmp_dir = tempfile::tempdir()?; +//! let db_dir = tmp_dir.path().to_owned(); //! let config = ConfigBuilder::new() -//! .db_directory(db_dir.path().to_path_buf()) +//! .db_directory(db_dir.into()) //! .build(); //! //! // Initialize the database thread-pool. diff --git a/storage/blockchain/src/tables.rs b/storage/blockchain/src/tables.rs index 2f49be0..a87b83b 100644 --- a/storage/blockchain/src/tables.rs +++ b/storage/blockchain/src/tables.rs @@ -292,6 +292,7 @@ macro_rules! tables { /// ## Table Name /// ```rust /// # use cuprate_blockchain::{*,tables::*}; + /// use cuprate_database::Table; #[doc = concat!( "assert_eq!(", stringify!([<$table:camel>]), diff --git a/storage/blockchain/src/tests.rs b/storage/blockchain/src/tests.rs index 5d033ea..ec2f18e 100644 --- a/storage/blockchain/src/tests.rs +++ b/storage/blockchain/src/tests.rs @@ -72,7 +72,7 @@ pub(crate) fn tmp_concrete_env() -> (ConcreteEnv, tempfile::TempDir) { .db_directory(Cow::Owned(tempdir.path().into())) .low_power() .build(); - let env = ConcreteEnv::open(config.db_config).unwrap(); + let env = crate::open(config).unwrap(); (env, tempdir) } diff --git a/storage/blockchain/src/types.rs b/storage/blockchain/src/types.rs index 8d7bdaf..f931944 100644 --- a/storage/blockchain/src/types.rs +++ b/storage/blockchain/src/types.rs @@ -106,6 +106,8 @@ pub type UnlockTime = u64; /// ```rust /// # use std::borrow::*; /// # use cuprate_blockchain::{*, types::*}; +/// use cuprate_database::Storable; +/// /// // Assert Storable is correct. /// let a = PreRctOutputId { /// amount: 1, @@ -149,6 +151,8 @@ pub struct PreRctOutputId { /// ```rust /// # use std::borrow::*; /// # use cuprate_blockchain::{*, types::*}; +/// use cuprate_database::Storable; +/// /// // Assert Storable is correct. /// let a = BlockInfo { /// timestamp: 1, @@ -208,6 +212,8 @@ bitflags::bitflags! { /// ```rust /// # use std::borrow::*; /// # use cuprate_blockchain::{*, types::*}; + /// use cuprate_database::Storable; + /// /// // Assert Storable is correct. /// let a = OutputFlags::NON_ZERO_UNLOCK_TIME; /// let b = Storable::as_bytes(&a); @@ -237,6 +243,8 @@ bitflags::bitflags! { /// ```rust /// # use std::borrow::*; /// # use cuprate_blockchain::{*, types::*}; +/// use cuprate_database::Storable; +/// /// // Assert Storable is correct. /// let a = Output { /// key: [1; 32], @@ -278,6 +286,8 @@ pub struct Output { /// ```rust /// # use std::borrow::*; /// # use cuprate_blockchain::{*, types::*}; +/// use cuprate_database::Storable; +/// /// // Assert Storable is correct. /// let a = RctOutput { /// key: [1; 32],