more compat fixes

This commit is contained in:
hinto.janai 2024-06-17 16:40:21 -04:00
parent 5841841674
commit 36feea0b53
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
10 changed files with 79 additions and 44 deletions

View file

@ -66,13 +66,14 @@ use cuprate_blockchain::{
# fn main() -> Result<(), Box<dyn std::error::Error>> { # fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a configuration for the database environment. // 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() let config = ConfigBuilder::new()
.db_directory(db_dir.path().to_path_buf()) .db_directory(db_dir.into())
.build(); .build();
// Initialize the database environment. // Initialize the database environment.
let env = ConcreteEnv::open(config)?; let env = cuprate_blockchain::open(config)?;
// Open up a transaction + tables for writing. // Open up a transaction + tables for writing.
let env_inner = env.env_inner(); let env_inner = env.env_inner();

View file

@ -77,6 +77,27 @@ impl ConfigBuilder {
self 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, /// Tune the [`ConfigBuilder`] for the highest performing,
/// but also most resource-intensive & maybe risky settings. /// but also most resource-intensive & maybe risky settings.
/// ///
@ -85,8 +106,7 @@ impl ConfigBuilder {
pub fn fast(mut self) -> Self { pub fn fast(mut self) -> Self {
self.db_config = self.db_config =
cuprate_database::config::ConfigBuilder::new(Cow::Borrowed(cuprate_blockchain_dir())) cuprate_database::config::ConfigBuilder::new(Cow::Borrowed(cuprate_blockchain_dir()))
.sync_mode(SyncMode::Fast) .fast();
.resize_algorithm(ResizeAlgorithm::default());
self.reader_threads = Some(ReaderThreads::OnePerThread); self.reader_threads = Some(ReaderThreads::OnePerThread);
self self
@ -100,19 +120,11 @@ impl ConfigBuilder {
pub fn low_power(mut self) -> Self { pub fn low_power(mut self) -> Self {
self.db_config = self.db_config =
cuprate_database::config::ConfigBuilder::new(Cow::Borrowed(cuprate_blockchain_dir())) cuprate_database::config::ConfigBuilder::new(Cow::Borrowed(cuprate_blockchain_dir()))
.sync_mode(SyncMode::default()) .low_power();
.resize_algorithm(ResizeAlgorithm::default());
self.reader_threads = Some(ReaderThreads::One); self.reader_threads = Some(ReaderThreads::One);
self 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 { impl Default for ConfigBuilder {
@ -155,17 +167,23 @@ impl Config {
/// Same as [`Config::default`]. /// Same as [`Config::default`].
/// ///
/// ```rust /// ```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_helper::fs::*;
/// ///
/// use cuprate_blockchain::config::*;
///
/// let config = Config::new(); /// let config = Config::new();
/// ///
/// assert_eq!(config.db_directory(), cuprate_blockchain_dir()); /// assert_eq!(config.db_config.db_directory(), cuprate_blockchain_dir());
/// assert!(config.db_file().starts_with(cuprate_blockchain_dir())); /// assert!(config.db_config.db_file().starts_with(cuprate_blockchain_dir()));
/// assert!(config.db_file().ends_with(DATABASE_DATA_FILENAME)); /// assert!(config.db_config.db_file().ends_with(DATABASE_DATA_FILENAME));
/// assert_eq!(config.sync_mode, SyncMode::default()); /// 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.reader_threads, ReaderThreads::default());
/// assert_eq!(config.resize_algorithm, ResizeAlgorithm::default());
/// ``` /// ```
pub fn new() -> Self { pub fn new() -> Self {
ConfigBuilder::default().build() ConfigBuilder::default().build()

View file

@ -14,17 +14,17 @@
//! //!
//! # Example //! # Example
//! ```rust //! ```rust
//! use cuprate_blockchain::{ //! use cuprate_database::{Env, config::SyncMode};
//! Env, //!
//! config::{ConfigBuilder, ReaderThreads, SyncMode} //! use cuprate_blockchain::config::{ConfigBuilder, ReaderThreads};
//! };
//! //!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> { //! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let db_dir = tempfile::tempdir()?; //! let tmp_dir = tempfile::tempdir()?;
//! let db_dir = tmp_dir.path().to_owned();
//! //!
//! let config = ConfigBuilder::new() //! let config = ConfigBuilder::new()
//! // Use a custom database directory. //! // 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`). //! // Use as many reader threads as possible (when using `service`).
//! .reader_threads(ReaderThreads::OnePerThread) //! .reader_threads(ReaderThreads::OnePerThread)
//! // Use the fastest sync mode. //! // Use the fastest sync mode.
@ -35,7 +35,7 @@
//! // Start a database `service` using this configuration. //! // Start a database `service` using this configuration.
//! let (reader_handle, _) = cuprate_blockchain::service::init(config.clone())?; //! let (reader_handle, _) = cuprate_blockchain::service::init(config.clone())?;
//! // It's using the config we provided. //! // It's using the config we provided.
//! assert_eq!(reader_handle.env().config(), &config); //! assert_eq!(reader_handle.env().config(), &config.db_config);
//! # Ok(()) } //! # Ok(()) }
//! ``` //! ```

View file

@ -10,6 +10,8 @@ use crate::{config::Config, open_tables::OpenTables};
/// ///
/// # Errors /// # Errors
/// TODO /// TODO
#[cold]
#[inline(never)] // only called once
pub fn open(config: Config) -> Result<ConcreteEnv, InitError> { pub fn open(config: Config) -> Result<ConcreteEnv, InitError> {
// Attempt to open the database environment. // Attempt to open the database environment.
let env = <ConcreteEnv as Env>::open(config.db_config)?; let env = <ConcreteEnv as Env>::open(config.db_config)?;

View file

@ -55,25 +55,29 @@
//! use hex_literal::hex; //! use hex_literal::hex;
//! //!
//! use cuprate_test_utils::data::block_v16_tx0; //! use cuprate_test_utils::data::block_v16_tx0;
//! use cuprate_database::{
//! ConcreteEnv,
//! Env, EnvInner,
//! DatabaseRo, DatabaseRw, TxRo, TxRw,
//! };
//! //!
//! use cuprate_blockchain::{ //! use cuprate_blockchain::{
//! ConcreteEnv, //! OpenTables,
//! config::ConfigBuilder, //! config::ConfigBuilder,
//! Env, EnvInner,
//! tables::{Tables, TablesMut}, //! tables::{Tables, TablesMut},
//! DatabaseRo, DatabaseRw, TxRo, TxRw,
//! ops::block::{add_block, pop_block}, //! ops::block::{add_block, pop_block},
//! }; //! };
//! //!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> { //! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Create a configuration for the database environment. //! // 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() //! let config = ConfigBuilder::new()
//! .db_directory(db_dir.path().to_path_buf()) //! .db_directory(db_dir.into())
//! .build(); //! .build();
//! //!
//! // Initialize the database environment. //! // Initialize the database environment.
//! let env = ConcreteEnv::open(config)?; //! let env = cuprate_blockchain::open(config)?;
//! //!
//! // Open up a transaction + tables for writing. //! // Open up a transaction + tables for writing.
//! let env_inner = env.env_inner(); //! let env_inner = env.env_inner();

View file

@ -3,7 +3,7 @@
//---------------------------------------------------------------------------------------------------- Import //---------------------------------------------------------------------------------------------------- Import
use std::sync::Arc; use std::sync::Arc;
use cuprate_database::{ConcreteEnv, Env, InitError}; use cuprate_database::InitError;
use crate::{ use crate::{
config::Config, config::Config,
@ -19,15 +19,12 @@ use crate::{
/// thread-pool and writer thread will exit automatically. /// thread-pool and writer thread will exit automatically.
/// ///
/// # Errors /// # 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> { pub fn init(config: Config) -> Result<(DatabaseReadHandle, DatabaseWriteHandle), InitError> {
let Config { let reader_threads = config.reader_threads;
db_config,
reader_threads,
} = config;
// Initialize the database itself. // 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. // Spawn the Reader thread pool and Writer.
let readers = DatabaseReadHandle::init(&db, reader_threads); let readers = DatabaseReadHandle::init(&db, reader_threads);

View file

@ -65,15 +65,17 @@
//! //!
//! use cuprate_types::blockchain::{BCReadRequest, BCWriteRequest, BCResponse}; //! use cuprate_types::blockchain::{BCReadRequest, BCWriteRequest, BCResponse};
//! use cuprate_test_utils::data::block_v16_tx0; //! 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] //! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> { //! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Create a configuration for the database environment. //! // 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() //! let config = ConfigBuilder::new()
//! .db_directory(db_dir.path().to_path_buf()) //! .db_directory(db_dir.into())
//! .build(); //! .build();
//! //!
//! // Initialize the database thread-pool. //! // Initialize the database thread-pool.

View file

@ -292,6 +292,7 @@ macro_rules! tables {
/// ## Table Name /// ## Table Name
/// ```rust /// ```rust
/// # use cuprate_blockchain::{*,tables::*}; /// # use cuprate_blockchain::{*,tables::*};
/// use cuprate_database::Table;
#[doc = concat!( #[doc = concat!(
"assert_eq!(", "assert_eq!(",
stringify!([<$table:camel>]), stringify!([<$table:camel>]),

View file

@ -72,7 +72,7 @@ pub(crate) fn tmp_concrete_env() -> (ConcreteEnv, tempfile::TempDir) {
.db_directory(Cow::Owned(tempdir.path().into())) .db_directory(Cow::Owned(tempdir.path().into()))
.low_power() .low_power()
.build(); .build();
let env = ConcreteEnv::open(config.db_config).unwrap(); let env = crate::open(config).unwrap();
(env, tempdir) (env, tempdir)
} }

View file

@ -106,6 +106,8 @@ pub type UnlockTime = u64;
/// ```rust /// ```rust
/// # use std::borrow::*; /// # use std::borrow::*;
/// # use cuprate_blockchain::{*, types::*}; /// # use cuprate_blockchain::{*, types::*};
/// use cuprate_database::Storable;
///
/// // Assert Storable is correct. /// // Assert Storable is correct.
/// let a = PreRctOutputId { /// let a = PreRctOutputId {
/// amount: 1, /// amount: 1,
@ -149,6 +151,8 @@ pub struct PreRctOutputId {
/// ```rust /// ```rust
/// # use std::borrow::*; /// # use std::borrow::*;
/// # use cuprate_blockchain::{*, types::*}; /// # use cuprate_blockchain::{*, types::*};
/// use cuprate_database::Storable;
///
/// // Assert Storable is correct. /// // Assert Storable is correct.
/// let a = BlockInfo { /// let a = BlockInfo {
/// timestamp: 1, /// timestamp: 1,
@ -208,6 +212,8 @@ bitflags::bitflags! {
/// ```rust /// ```rust
/// # use std::borrow::*; /// # use std::borrow::*;
/// # use cuprate_blockchain::{*, types::*}; /// # use cuprate_blockchain::{*, types::*};
/// use cuprate_database::Storable;
///
/// // Assert Storable is correct. /// // Assert Storable is correct.
/// let a = OutputFlags::NON_ZERO_UNLOCK_TIME; /// let a = OutputFlags::NON_ZERO_UNLOCK_TIME;
/// let b = Storable::as_bytes(&a); /// let b = Storable::as_bytes(&a);
@ -237,6 +243,8 @@ bitflags::bitflags! {
/// ```rust /// ```rust
/// # use std::borrow::*; /// # use std::borrow::*;
/// # use cuprate_blockchain::{*, types::*}; /// # use cuprate_blockchain::{*, types::*};
/// use cuprate_database::Storable;
///
/// // Assert Storable is correct. /// // Assert Storable is correct.
/// let a = Output { /// let a = Output {
/// key: [1; 32], /// key: [1; 32],
@ -278,6 +286,8 @@ pub struct Output {
/// ```rust /// ```rust
/// # use std::borrow::*; /// # use std::borrow::*;
/// # use cuprate_blockchain::{*, types::*}; /// # use cuprate_blockchain::{*, types::*};
/// use cuprate_database::Storable;
///
/// // Assert Storable is correct. /// // Assert Storable is correct.
/// let a = RctOutput { /// let a = RctOutput {
/// key: [1; 32], /// key: [1; 32],