From 60f7fd41d603e3d3fa80e7170e31a21055bfaf68 Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Wed, 12 Jun 2024 20:22:53 -0400 Subject: [PATCH] find/replace `cuprate_blockchain` -> `database`, add `create_db()` --- storage/database/src/backend/heed/env.rs | 11 +++++++++++ storage/database/src/backend/heed/error.rs | 4 ++-- storage/database/src/backend/heed/storable.rs | 4 ++-- storage/database/src/backend/redb/env.rs | 2 +- storage/database/src/backend/redb/error.rs | 2 +- storage/database/src/backend/redb/storable.rs | 4 ++-- storage/database/src/config/backend.rs | 2 +- storage/database/src/config/config.rs | 6 ++++-- storage/database/src/config/mod.rs | 10 +++++----- storage/database/src/config/reader_threads.rs | 6 +++--- storage/database/src/env.rs | 17 ++++++----------- storage/database/src/error.rs | 2 +- storage/database/src/key.rs | 2 +- storage/database/src/lib.rs | 5 +++-- storage/database/src/resize.rs | 16 ++++++++-------- storage/database/src/storable.rs | 10 +++++----- 16 files changed, 56 insertions(+), 47 deletions(-) diff --git a/storage/database/src/backend/heed/env.rs b/storage/database/src/backend/heed/env.rs index 32676ba..30e7b05 100644 --- a/storage/database/src/backend/heed/env.rs +++ b/storage/database/src/backend/heed/env.rs @@ -293,6 +293,17 @@ where }) } + fn create_db(&self, tx_rw: &RefCell>) -> Result<(), RuntimeError> { + use crate::backend::heed::storable::StorableHeed; + + self.create_database::::Key>, StorableHeed<::Value>>( + &mut tx_rw.borrow_mut(), + Some(T::NAME), + )?; + + Ok(()) + } + #[inline] fn clear_db( &self, diff --git a/storage/database/src/backend/heed/error.rs b/storage/database/src/backend/heed/error.rs index c809e51..93207f5 100644 --- a/storage/database/src/backend/heed/error.rs +++ b/storage/database/src/backend/heed/error.rs @@ -1,4 +1,4 @@ -//! Conversion from `heed::Error` -> `cuprate_blockchain`'s errors. +//! Conversion from `heed::Error` -> `database`'s errors. //---------------------------------------------------------------------------------------------------- Use use crate::constants::DATABASE_CORRUPT_MSG; @@ -85,7 +85,7 @@ impl From for crate::RuntimeError { E2::Corrupted | E2::PageNotFound => panic!("{mdb_error:#?}\n{DATABASE_CORRUPT_MSG}"), // These errors should not occur, and if they do, - // the best thing `cuprate_blockchain` can do for + // the best thing `database` can do for // safety is to panic right here. E2::Panic | E2::PageFull diff --git a/storage/database/src/backend/heed/storable.rs b/storage/database/src/backend/heed/storable.rs index ebd8f6e..a69b923 100644 --- a/storage/database/src/backend/heed/storable.rs +++ b/storage/database/src/backend/heed/storable.rs @@ -1,4 +1,4 @@ -//! `cuprate_blockchain::Storable` <-> `heed` serde trait compatibility layer. +//! `database::Storable` <-> `heed` serde trait compatibility layer. //---------------------------------------------------------------------------------------------------- Use use std::{borrow::Cow, marker::PhantomData}; @@ -9,7 +9,7 @@ use crate::storable::Storable; //---------------------------------------------------------------------------------------------------- StorableHeed /// The glue struct that implements `heed`'s (de)serialization -/// traits on any type that implements `cuprate_blockchain::Storable`. +/// traits on any type that implements `database::Storable`. /// /// Never actually gets constructed, just used for trait bound translations. pub(super) struct StorableHeed(PhantomData) diff --git a/storage/database/src/backend/redb/env.rs b/storage/database/src/backend/redb/env.rs index 67e430f..0e05065 100644 --- a/storage/database/src/backend/redb/env.rs +++ b/storage/database/src/backend/redb/env.rs @@ -22,7 +22,7 @@ pub struct ConcreteEnv { /// (and in current use). config: Config, - /// A cached, redb version of `cuprate_blockchain::config::SyncMode`. + /// A cached, redb version of `database::config::SyncMode`. /// `redb` needs the sync mode to be set _per_ TX, so we /// will continue to use this value every `Env::tx_rw`. durability: redb::Durability, diff --git a/storage/database/src/backend/redb/error.rs b/storage/database/src/backend/redb/error.rs index 1cc1456..7f6ccb3 100644 --- a/storage/database/src/backend/redb/error.rs +++ b/storage/database/src/backend/redb/error.rs @@ -1,4 +1,4 @@ -//! Conversion from `redb`'s errors -> `cuprate_blockchain`'s errors. +//! Conversion from `redb`'s errors -> `database`'s errors. //! //! HACK: There's a lot of `_ =>` usage here because //! `redb`'s errors are `#[non_exhaustive]`... diff --git a/storage/database/src/backend/redb/storable.rs b/storage/database/src/backend/redb/storable.rs index efe77dc..8413731 100644 --- a/storage/database/src/backend/redb/storable.rs +++ b/storage/database/src/backend/redb/storable.rs @@ -1,4 +1,4 @@ -//! `cuprate_blockchain::Storable` <-> `redb` serde trait compatibility layer. +//! `database::Storable` <-> `redb` serde trait compatibility layer. //---------------------------------------------------------------------------------------------------- Use use std::{cmp::Ordering, fmt::Debug, marker::PhantomData}; @@ -9,7 +9,7 @@ use crate::{key::Key, storable::Storable}; //---------------------------------------------------------------------------------------------------- StorableRedb /// The glue structs that implements `redb`'s (de)serialization -/// traits on any type that implements `cuprate_blockchain::Key`. +/// traits on any type that implements `database::Key`. /// /// Never actually get constructed, just used for trait bound translations. #[derive(Debug)] diff --git a/storage/database/src/config/backend.rs b/storage/database/src/config/backend.rs index ee72b3d..ea92b35 100644 --- a/storage/database/src/config/backend.rs +++ b/storage/database/src/config/backend.rs @@ -10,7 +10,7 @@ use std::{ #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; -use cuprate_helper::fs::cuprate_blockchain_dir; +use cuprate_helper::fs::database_dir; use crate::{ config::{ReaderThreads, SyncMode}, diff --git a/storage/database/src/config/config.rs b/storage/database/src/config/config.rs index 9d932ab..27895ea 100644 --- a/storage/database/src/config/config.rs +++ b/storage/database/src/config/config.rs @@ -61,6 +61,7 @@ impl ConfigBuilder { pub fn build(self) -> Config { // INVARIANT: all PATH safety checks are done // in `helper::fs`. No need to do them here. + // TODO: fix me let db_directory = self .db_directory .unwrap_or_else(|| Cow::Borrowed(cuprate_blockchain_dir())); @@ -137,6 +138,7 @@ impl ConfigBuilder { impl Default for ConfigBuilder { fn default() -> Self { Self { + // TODO: fix me db_directory: Some(Cow::Borrowed(cuprate_blockchain_dir())), sync_mode: Some(SyncMode::default()), reader_threads: Some(ReaderThreads::default()), @@ -197,7 +199,7 @@ impl Config { /// Same as [`Config::default`]. /// /// ```rust - /// use cuprate_blockchain::{config::*, resize::*, DATABASE_DATA_FILENAME}; + /// use database::{config::*, resize::*, DATABASE_DATA_FILENAME}; /// use cuprate_helper::fs::*; /// /// let config = Config::new(); @@ -228,7 +230,7 @@ impl Default for Config { /// Same as [`Config::new`]. /// /// ```rust - /// # use cuprate_blockchain::config::*; + /// # use database::config::*; /// assert_eq!(Config::default(), Config::new()); /// ``` fn default() -> Self { diff --git a/storage/database/src/config/mod.rs b/storage/database/src/config/mod.rs index 141790b..ba39bd9 100644 --- a/storage/database/src/config/mod.rs +++ b/storage/database/src/config/mod.rs @@ -12,8 +12,8 @@ //! //! # Example //! ```rust -//! use cuprate_blockchain::{ -//! Env, +//! use database::{ +//! ConcreteEnv, Env, //! config::{ConfigBuilder, ReaderThreads, SyncMode} //! }; //! @@ -30,10 +30,10 @@ //! // Build into `Config` //! .build(); //! -//! // Start a database `service` using this configuration. -//! let (reader_handle, _) = cuprate_blockchain::service::init(config.clone())?; +//! // Open the database `service` using this configuration. +//! let env = ConcreteEnv::open(config.clone())?; //! // It's using the config we provided. -//! assert_eq!(reader_handle.env().config(), &config); +//! assert_eq!(env.config(), &config); //! # Ok(()) } //! ``` diff --git a/storage/database/src/config/reader_threads.rs b/storage/database/src/config/reader_threads.rs index 59ef45e..91627f1 100644 --- a/storage/database/src/config/reader_threads.rs +++ b/storage/database/src/config/reader_threads.rs @@ -50,7 +50,7 @@ pub enum ReaderThreads { /// as such, it is equal to [`ReaderThreads::OnePerThread`]. /// /// ```rust - /// # use cuprate_blockchain::config::*; + /// # use database::config::*; /// let reader_threads = ReaderThreads::from(0_usize); /// assert!(matches!(reader_threads, ReaderThreads::OnePerThread)); /// ``` @@ -82,7 +82,7 @@ pub enum ReaderThreads { /// non-zero, but not 1 thread, the minimum value 1 will be returned. /// /// ```rust - /// # use cuprate_blockchain::config::*; + /// # use database::config::*; /// assert_eq!(ReaderThreads::Percent(0.000000001).as_threads().get(), 1); /// ``` Percent(f32), @@ -98,7 +98,7 @@ impl ReaderThreads { /// /// # Example /// ```rust - /// use cuprate_blockchain::config::ReaderThreads as Rt; + /// use database::config::ReaderThreads as Rt; /// /// let total_threads: std::num::NonZeroUsize = /// cuprate_helper::thread::threads(); diff --git a/storage/database/src/env.rs b/storage/database/src/env.rs index fbb948e..56fd1ed 100644 --- a/storage/database/src/env.rs +++ b/storage/database/src/env.rs @@ -251,17 +251,12 @@ where // TODO: make equivalent in `cuprate-blockchain`. - // /// Open all tables in read/iter mode. - // /// - // /// This calls [`EnvInner::open_db_ro`] on all database tables - // /// and returns a structure that allows access to all tables. - // /// - // #[doc = doc_table_error!()] - // fn open_tables(&self, tx_ro: &Ro) -> Result { - // call_fn_on_all_tables_or_early_return! { - // Self::open_db_ro(self, tx_ro) - // } - // } + /// Create a database table. + /// + /// This will create the database [`Table`] + /// passed as a generic to this function. + #[doc = doc_table_error!()] + fn create_db(&self, tx_rw: &Rw) -> Result<(), RuntimeError>; // /// Open all tables in read-write mode. // /// diff --git a/storage/database/src/error.rs b/storage/database/src/error.rs index 6112d92..14b5235 100644 --- a/storage/database/src/error.rs +++ b/storage/database/src/error.rs @@ -66,7 +66,7 @@ pub enum InitError { /// 2. (De)serialization /// 3. Shutdown errors /// -/// as `cuprate_blockchain` upholds the invariant that: +/// as `database` upholds the invariant that: /// /// 1. All tables exist /// 2. (De)serialization never fails diff --git a/storage/database/src/key.rs b/storage/database/src/key.rs index daafc6b..d66c86f 100644 --- a/storage/database/src/key.rs +++ b/storage/database/src/key.rs @@ -23,7 +23,7 @@ pub trait Key: Storable + Sized { /// not a comparison of the key's value. /// /// ```rust - /// # use cuprate_blockchain::*; + /// # use database::*; /// assert_eq!( /// ::compare([0].as_slice(), [1].as_slice()), /// std::cmp::Ordering::Less, diff --git a/storage/database/src/lib.rs b/storage/database/src/lib.rs index a48cf23..6a8d540 100644 --- a/storage/database/src/lib.rs +++ b/storage/database/src/lib.rs @@ -1,6 +1,6 @@ //! Cuprate's database abstraction. //! -//! This documentation is mostly for practical usage of `cuprate_database`. +//! This documentation is mostly for practical usage of `database`. //! //! For a high-level overview, see the database section in //! [Cuprate's architecture book](https://architecture.cuprate.org). @@ -98,13 +98,14 @@ //! // Open up a transaction + tables for writing. //! struct Table; //! impl database::Table for Table { -//! const NAME: &'static str = "table" +//! const NAME: &'static str = "table"; //! type Key = u8; //! type Value = u8; //! } //! //! let env_inner = env.env_inner(); //! let tx_rw = env_inner.tx_rw()?; +//! env_inner.create_db::(&tx_rw)?; //! let mut table = env_inner.open_db_rw::
(&tx_rw)?; //! //! // Write data to the table. diff --git a/storage/database/src/resize.rs b/storage/database/src/resize.rs index 488325b..9bd95f4 100644 --- a/storage/database/src/resize.rs +++ b/storage/database/src/resize.rs @@ -50,7 +50,7 @@ impl ResizeAlgorithm { /// Returns [`Self::Monero`]. /// /// ```rust - /// # use cuprate_blockchain::resize::*; + /// # use database::resize::*; /// assert!(matches!(ResizeAlgorithm::new(), ResizeAlgorithm::Monero)); /// ``` #[inline] @@ -75,7 +75,7 @@ impl Default for ResizeAlgorithm { /// Calls [`Self::new`]. /// /// ```rust - /// # use cuprate_blockchain::resize::*; + /// # use database::resize::*; /// assert_eq!(ResizeAlgorithm::new(), ResizeAlgorithm::default()); /// ``` #[inline] @@ -113,7 +113,7 @@ pub fn page_size() -> NonZeroUsize { /// [^2]: `1_073_745_920` /// /// ```rust -/// # use cuprate_blockchain::resize::*; +/// # use database::resize::*; /// // The value this function will increment by /// // (assuming page multiple of 4096). /// const N: usize = 1_073_741_824; @@ -129,7 +129,7 @@ pub fn page_size() -> NonZeroUsize { /// This function will panic if adding onto `current_size_bytes` overflows [`usize::MAX`]. /// /// ```rust,should_panic -/// # use cuprate_blockchain::resize::*; +/// # use database::resize::*; /// // Ridiculous large numbers panic. /// monero(usize::MAX); /// ``` @@ -166,7 +166,7 @@ pub fn monero(current_size_bytes: usize) -> NonZeroUsize { /// and then round up to nearest OS page size. /// /// ```rust -/// # use cuprate_blockchain::resize::*; +/// # use database::resize::*; /// let page_size: usize = page_size().get(); /// /// // Anything below the page size will round up to the page size. @@ -185,7 +185,7 @@ pub fn monero(current_size_bytes: usize) -> NonZeroUsize { /// This function will panic if adding onto `current_size_bytes` overflows [`usize::MAX`]. /// /// ```rust,should_panic -/// # use cuprate_blockchain::resize::*; +/// # use database::resize::*; /// // Ridiculous large numbers panic. /// fixed_bytes(1, usize::MAX); /// ``` @@ -221,7 +221,7 @@ pub fn fixed_bytes(current_size_bytes: usize, add_bytes: usize) -> NonZeroUsize /// (rounded up to the OS page size). /// /// ```rust -/// # use cuprate_blockchain::resize::*; +/// # use database::resize::*; /// let page_size: usize = page_size().get(); /// /// // Anything below the page size will round up to the page size. @@ -247,7 +247,7 @@ pub fn fixed_bytes(current_size_bytes: usize, add_bytes: usize) -> NonZeroUsize /// is closer to [`usize::MAX`] than the OS page size. /// /// ```rust,should_panic -/// # use cuprate_blockchain::resize::*; +/// # use database::resize::*; /// // Ridiculous large numbers panic. /// percent(usize::MAX, 1.001); /// ``` diff --git a/storage/database/src/storable.rs b/storage/database/src/storable.rs index 2450129..1997c33 100644 --- a/storage/database/src/storable.rs +++ b/storage/database/src/storable.rs @@ -30,7 +30,7 @@ use bytes::Bytes; /// See [`StorableVec`] & [`StorableBytes`] for storing slices of `T: Storable`. /// /// ```rust -/// # use cuprate_blockchain::*; +/// # use database::*; /// # use std::borrow::*; /// let number: u64 = 0; /// @@ -78,7 +78,7 @@ pub trait Storable: Debug { /// /// # Examples /// ```rust - /// # use cuprate_blockchain::*; + /// # use database::*; /// assert_eq!(<()>::BYTE_LENGTH, Some(0)); /// assert_eq!(u8::BYTE_LENGTH, Some(1)); /// assert_eq!(u16::BYTE_LENGTH, Some(2)); @@ -100,7 +100,7 @@ pub trait Storable: Debug { /// /// # Blanket implementation /// The blanket implementation that covers all types used - /// by `cuprate_blockchain` will simply bitwise copy `bytes` + /// by `database` will simply bitwise copy `bytes` /// into `Self`. /// /// The bytes do not have be correctly aligned. @@ -137,7 +137,7 @@ where /// /// # Example /// ```rust -/// # use cuprate_blockchain::*; +/// # use database::*; /// //---------------------------------------------------- u8 /// let vec: StorableVec = StorableVec(vec![0,1]); /// @@ -203,7 +203,7 @@ impl Borrow<[T]> for StorableVec { /// A [`Storable`] version of [`Bytes`]. /// /// ```rust -/// # use cuprate_blockchain::*; +/// # use database::*; /// # use bytes::Bytes; /// let bytes: StorableBytes = StorableBytes(Bytes::from_static(&[0,1])); ///