find/replace cuprate_blockchain -> database, add create_db()

This commit is contained in:
hinto.janai 2024-06-12 20:22:53 -04:00
parent 1a6d86d3b2
commit 60f7fd41d6
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
16 changed files with 56 additions and 47 deletions

View file

@ -293,6 +293,17 @@ where
}) })
} }
fn create_db<T: Table>(&self, tx_rw: &RefCell<heed::RwTxn<'env>>) -> Result<(), RuntimeError> {
use crate::backend::heed::storable::StorableHeed;
self.create_database::<StorableHeed<<T as Table>::Key>, StorableHeed<<T as Table>::Value>>(
&mut tx_rw.borrow_mut(),
Some(T::NAME),
)?;
Ok(())
}
#[inline] #[inline]
fn clear_db<T: Table>( fn clear_db<T: Table>(
&self, &self,

View file

@ -1,4 +1,4 @@
//! Conversion from `heed::Error` -> `cuprate_blockchain`'s errors. //! Conversion from `heed::Error` -> `database`'s errors.
//---------------------------------------------------------------------------------------------------- Use //---------------------------------------------------------------------------------------------------- Use
use crate::constants::DATABASE_CORRUPT_MSG; use crate::constants::DATABASE_CORRUPT_MSG;
@ -85,7 +85,7 @@ impl From<heed::Error> for crate::RuntimeError {
E2::Corrupted | E2::PageNotFound => panic!("{mdb_error:#?}\n{DATABASE_CORRUPT_MSG}"), E2::Corrupted | E2::PageNotFound => panic!("{mdb_error:#?}\n{DATABASE_CORRUPT_MSG}"),
// These errors should not occur, and if they do, // 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. // safety is to panic right here.
E2::Panic E2::Panic
| E2::PageFull | E2::PageFull

View file

@ -1,4 +1,4 @@
//! `cuprate_blockchain::Storable` <-> `heed` serde trait compatibility layer. //! `database::Storable` <-> `heed` serde trait compatibility layer.
//---------------------------------------------------------------------------------------------------- Use //---------------------------------------------------------------------------------------------------- Use
use std::{borrow::Cow, marker::PhantomData}; use std::{borrow::Cow, marker::PhantomData};
@ -9,7 +9,7 @@ use crate::storable::Storable;
//---------------------------------------------------------------------------------------------------- StorableHeed //---------------------------------------------------------------------------------------------------- StorableHeed
/// The glue struct that implements `heed`'s (de)serialization /// 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. /// Never actually gets constructed, just used for trait bound translations.
pub(super) struct StorableHeed<T>(PhantomData<T>) pub(super) struct StorableHeed<T>(PhantomData<T>)

View file

@ -22,7 +22,7 @@ pub struct ConcreteEnv {
/// (and in current use). /// (and in current use).
config: Config, 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 /// `redb` needs the sync mode to be set _per_ TX, so we
/// will continue to use this value every `Env::tx_rw`. /// will continue to use this value every `Env::tx_rw`.
durability: redb::Durability, durability: redb::Durability,

View file

@ -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 //! HACK: There's a lot of `_ =>` usage here because
//! `redb`'s errors are `#[non_exhaustive]`... //! `redb`'s errors are `#[non_exhaustive]`...

View file

@ -1,4 +1,4 @@
//! `cuprate_blockchain::Storable` <-> `redb` serde trait compatibility layer. //! `database::Storable` <-> `redb` serde trait compatibility layer.
//---------------------------------------------------------------------------------------------------- Use //---------------------------------------------------------------------------------------------------- Use
use std::{cmp::Ordering, fmt::Debug, marker::PhantomData}; use std::{cmp::Ordering, fmt::Debug, marker::PhantomData};
@ -9,7 +9,7 @@ use crate::{key::Key, storable::Storable};
//---------------------------------------------------------------------------------------------------- StorableRedb //---------------------------------------------------------------------------------------------------- StorableRedb
/// The glue structs that implements `redb`'s (de)serialization /// 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. /// Never actually get constructed, just used for trait bound translations.
#[derive(Debug)] #[derive(Debug)]

View file

@ -10,7 +10,7 @@ use std::{
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use cuprate_helper::fs::cuprate_blockchain_dir; use cuprate_helper::fs::database_dir;
use crate::{ use crate::{
config::{ReaderThreads, SyncMode}, config::{ReaderThreads, SyncMode},

View file

@ -61,6 +61,7 @@ impl ConfigBuilder {
pub fn build(self) -> Config { pub fn build(self) -> Config {
// INVARIANT: all PATH safety checks are done // INVARIANT: all PATH safety checks are done
// in `helper::fs`. No need to do them here. // in `helper::fs`. No need to do them here.
// TODO: fix me
let db_directory = self let db_directory = self
.db_directory .db_directory
.unwrap_or_else(|| Cow::Borrowed(cuprate_blockchain_dir())); .unwrap_or_else(|| Cow::Borrowed(cuprate_blockchain_dir()));
@ -137,6 +138,7 @@ impl ConfigBuilder {
impl Default for ConfigBuilder { impl Default for ConfigBuilder {
fn default() -> Self { fn default() -> Self {
Self { Self {
// TODO: fix me
db_directory: Some(Cow::Borrowed(cuprate_blockchain_dir())), db_directory: Some(Cow::Borrowed(cuprate_blockchain_dir())),
sync_mode: Some(SyncMode::default()), sync_mode: Some(SyncMode::default()),
reader_threads: Some(ReaderThreads::default()), reader_threads: Some(ReaderThreads::default()),
@ -197,7 +199,7 @@ impl Config {
/// Same as [`Config::default`]. /// Same as [`Config::default`].
/// ///
/// ```rust /// ```rust
/// use cuprate_blockchain::{config::*, resize::*, DATABASE_DATA_FILENAME}; /// use database::{config::*, resize::*, DATABASE_DATA_FILENAME};
/// use cuprate_helper::fs::*; /// use cuprate_helper::fs::*;
/// ///
/// let config = Config::new(); /// let config = Config::new();
@ -228,7 +230,7 @@ impl Default for Config {
/// Same as [`Config::new`]. /// Same as [`Config::new`].
/// ///
/// ```rust /// ```rust
/// # use cuprate_blockchain::config::*; /// # use database::config::*;
/// assert_eq!(Config::default(), Config::new()); /// assert_eq!(Config::default(), Config::new());
/// ``` /// ```
fn default() -> Self { fn default() -> Self {

View file

@ -12,8 +12,8 @@
//! //!
//! # Example //! # Example
//! ```rust //! ```rust
//! use cuprate_blockchain::{ //! use database::{
//! Env, //! ConcreteEnv, Env,
//! config::{ConfigBuilder, ReaderThreads, SyncMode} //! config::{ConfigBuilder, ReaderThreads, SyncMode}
//! }; //! };
//! //!
@ -30,10 +30,10 @@
//! // Build into `Config` //! // Build into `Config`
//! .build(); //! .build();
//! //!
//! // Start a database `service` using this configuration. //! // Open the database `service` using this configuration.
//! let (reader_handle, _) = cuprate_blockchain::service::init(config.clone())?; //! let env = ConcreteEnv::open(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!(env.config(), &config);
//! # Ok(()) } //! # Ok(()) }
//! ``` //! ```

View file

@ -50,7 +50,7 @@ pub enum ReaderThreads {
/// as such, it is equal to [`ReaderThreads::OnePerThread`]. /// as such, it is equal to [`ReaderThreads::OnePerThread`].
/// ///
/// ```rust /// ```rust
/// # use cuprate_blockchain::config::*; /// # use database::config::*;
/// let reader_threads = ReaderThreads::from(0_usize); /// let reader_threads = ReaderThreads::from(0_usize);
/// assert!(matches!(reader_threads, ReaderThreads::OnePerThread)); /// 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. /// non-zero, but not 1 thread, the minimum value 1 will be returned.
/// ///
/// ```rust /// ```rust
/// # use cuprate_blockchain::config::*; /// # use database::config::*;
/// assert_eq!(ReaderThreads::Percent(0.000000001).as_threads().get(), 1); /// assert_eq!(ReaderThreads::Percent(0.000000001).as_threads().get(), 1);
/// ``` /// ```
Percent(f32), Percent(f32),
@ -98,7 +98,7 @@ impl ReaderThreads {
/// ///
/// # Example /// # Example
/// ```rust /// ```rust
/// use cuprate_blockchain::config::ReaderThreads as Rt; /// use database::config::ReaderThreads as Rt;
/// ///
/// let total_threads: std::num::NonZeroUsize = /// let total_threads: std::num::NonZeroUsize =
/// cuprate_helper::thread::threads(); /// cuprate_helper::thread::threads();

View file

@ -251,17 +251,12 @@ where
// TODO: make equivalent in `cuprate-blockchain`. // TODO: make equivalent in `cuprate-blockchain`.
// /// Open all tables in read/iter mode. /// Create a database table.
// /// ///
// /// This calls [`EnvInner::open_db_ro`] on all database tables /// This will create the database [`Table`]
// /// and returns a structure that allows access to all tables. /// passed as a generic to this function.
// /// #[doc = doc_table_error!()]
// #[doc = doc_table_error!()] fn create_db<T: Table>(&self, tx_rw: &Rw) -> Result<(), RuntimeError>;
// fn open_tables(&self, tx_ro: &Ro) -> Result<impl TablesIter, RuntimeError> {
// call_fn_on_all_tables_or_early_return! {
// Self::open_db_ro(self, tx_ro)
// }
// }
// /// Open all tables in read-write mode. // /// Open all tables in read-write mode.
// /// // ///

View file

@ -66,7 +66,7 @@ pub enum InitError {
/// 2. (De)serialization /// 2. (De)serialization
/// 3. Shutdown errors /// 3. Shutdown errors
/// ///
/// as `cuprate_blockchain` upholds the invariant that: /// as `database` upholds the invariant that:
/// ///
/// 1. All tables exist /// 1. All tables exist
/// 2. (De)serialization never fails /// 2. (De)serialization never fails

View file

@ -23,7 +23,7 @@ pub trait Key: Storable + Sized {
/// not a comparison of the key's value. /// not a comparison of the key's value.
/// ///
/// ```rust /// ```rust
/// # use cuprate_blockchain::*; /// # use database::*;
/// assert_eq!( /// assert_eq!(
/// <u64 as Key>::compare([0].as_slice(), [1].as_slice()), /// <u64 as Key>::compare([0].as_slice(), [1].as_slice()),
/// std::cmp::Ordering::Less, /// std::cmp::Ordering::Less,

View file

@ -1,6 +1,6 @@
//! Cuprate's database abstraction. //! 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 //! For a high-level overview, see the database section in
//! [Cuprate's architecture book](https://architecture.cuprate.org). //! [Cuprate's architecture book](https://architecture.cuprate.org).
@ -98,13 +98,14 @@
//! // Open up a transaction + tables for writing. //! // Open up a transaction + tables for writing.
//! struct Table; //! struct Table;
//! impl database::Table for Table { //! impl database::Table for Table {
//! const NAME: &'static str = "table" //! const NAME: &'static str = "table";
//! type Key = u8; //! type Key = u8;
//! type Value = u8; //! type Value = u8;
//! } //! }
//! //!
//! let env_inner = env.env_inner(); //! let env_inner = env.env_inner();
//! let tx_rw = env_inner.tx_rw()?; //! let tx_rw = env_inner.tx_rw()?;
//! env_inner.create_db::<Table>(&tx_rw)?;
//! let mut table = env_inner.open_db_rw::<Table>(&tx_rw)?; //! let mut table = env_inner.open_db_rw::<Table>(&tx_rw)?;
//! //!
//! // Write data to the table. //! // Write data to the table.

View file

@ -50,7 +50,7 @@ impl ResizeAlgorithm {
/// Returns [`Self::Monero`]. /// Returns [`Self::Monero`].
/// ///
/// ```rust /// ```rust
/// # use cuprate_blockchain::resize::*; /// # use database::resize::*;
/// assert!(matches!(ResizeAlgorithm::new(), ResizeAlgorithm::Monero)); /// assert!(matches!(ResizeAlgorithm::new(), ResizeAlgorithm::Monero));
/// ``` /// ```
#[inline] #[inline]
@ -75,7 +75,7 @@ impl Default for ResizeAlgorithm {
/// Calls [`Self::new`]. /// Calls [`Self::new`].
/// ///
/// ```rust /// ```rust
/// # use cuprate_blockchain::resize::*; /// # use database::resize::*;
/// assert_eq!(ResizeAlgorithm::new(), ResizeAlgorithm::default()); /// assert_eq!(ResizeAlgorithm::new(), ResizeAlgorithm::default());
/// ``` /// ```
#[inline] #[inline]
@ -113,7 +113,7 @@ pub fn page_size() -> NonZeroUsize {
/// [^2]: `1_073_745_920` /// [^2]: `1_073_745_920`
/// ///
/// ```rust /// ```rust
/// # use cuprate_blockchain::resize::*; /// # use database::resize::*;
/// // The value this function will increment by /// // The value this function will increment by
/// // (assuming page multiple of 4096). /// // (assuming page multiple of 4096).
/// const N: usize = 1_073_741_824; /// 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`]. /// This function will panic if adding onto `current_size_bytes` overflows [`usize::MAX`].
/// ///
/// ```rust,should_panic /// ```rust,should_panic
/// # use cuprate_blockchain::resize::*; /// # use database::resize::*;
/// // Ridiculous large numbers panic. /// // Ridiculous large numbers panic.
/// monero(usize::MAX); /// monero(usize::MAX);
/// ``` /// ```
@ -166,7 +166,7 @@ pub fn monero(current_size_bytes: usize) -> NonZeroUsize {
/// and then round up to nearest OS page size. /// and then round up to nearest OS page size.
/// ///
/// ```rust /// ```rust
/// # use cuprate_blockchain::resize::*; /// # use database::resize::*;
/// let page_size: usize = page_size().get(); /// let page_size: usize = page_size().get();
/// ///
/// // Anything below the page size will round up to the page size. /// // 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`]. /// This function will panic if adding onto `current_size_bytes` overflows [`usize::MAX`].
/// ///
/// ```rust,should_panic /// ```rust,should_panic
/// # use cuprate_blockchain::resize::*; /// # use database::resize::*;
/// // Ridiculous large numbers panic. /// // Ridiculous large numbers panic.
/// fixed_bytes(1, usize::MAX); /// 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). /// (rounded up to the OS page size).
/// ///
/// ```rust /// ```rust
/// # use cuprate_blockchain::resize::*; /// # use database::resize::*;
/// let page_size: usize = page_size().get(); /// let page_size: usize = page_size().get();
/// ///
/// // Anything below the page size will round up to the page size. /// // 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. /// is closer to [`usize::MAX`] than the OS page size.
/// ///
/// ```rust,should_panic /// ```rust,should_panic
/// # use cuprate_blockchain::resize::*; /// # use database::resize::*;
/// // Ridiculous large numbers panic. /// // Ridiculous large numbers panic.
/// percent(usize::MAX, 1.001); /// percent(usize::MAX, 1.001);
/// ``` /// ```

View file

@ -30,7 +30,7 @@ use bytes::Bytes;
/// See [`StorableVec`] & [`StorableBytes`] for storing slices of `T: Storable`. /// See [`StorableVec`] & [`StorableBytes`] for storing slices of `T: Storable`.
/// ///
/// ```rust /// ```rust
/// # use cuprate_blockchain::*; /// # use database::*;
/// # use std::borrow::*; /// # use std::borrow::*;
/// let number: u64 = 0; /// let number: u64 = 0;
/// ///
@ -78,7 +78,7 @@ pub trait Storable: Debug {
/// ///
/// # Examples /// # Examples
/// ```rust /// ```rust
/// # use cuprate_blockchain::*; /// # use database::*;
/// assert_eq!(<()>::BYTE_LENGTH, Some(0)); /// assert_eq!(<()>::BYTE_LENGTH, Some(0));
/// assert_eq!(u8::BYTE_LENGTH, Some(1)); /// assert_eq!(u8::BYTE_LENGTH, Some(1));
/// assert_eq!(u16::BYTE_LENGTH, Some(2)); /// assert_eq!(u16::BYTE_LENGTH, Some(2));
@ -100,7 +100,7 @@ pub trait Storable: Debug {
/// ///
/// # Blanket implementation /// # Blanket implementation
/// The blanket implementation that covers all types used /// 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`. /// into `Self`.
/// ///
/// The bytes do not have be correctly aligned. /// The bytes do not have be correctly aligned.
@ -137,7 +137,7 @@ where
/// ///
/// # Example /// # Example
/// ```rust /// ```rust
/// # use cuprate_blockchain::*; /// # use database::*;
/// //---------------------------------------------------- u8 /// //---------------------------------------------------- u8
/// let vec: StorableVec<u8> = StorableVec(vec![0,1]); /// let vec: StorableVec<u8> = StorableVec(vec![0,1]);
/// ///
@ -203,7 +203,7 @@ impl<T> Borrow<[T]> for StorableVec<T> {
/// A [`Storable`] version of [`Bytes`]. /// A [`Storable`] version of [`Bytes`].
/// ///
/// ```rust /// ```rust
/// # use cuprate_blockchain::*; /// # use database::*;
/// # use bytes::Bytes; /// # use bytes::Bytes;
/// let bytes: StorableBytes = StorableBytes(Bytes::from_static(&[0,1])); /// let bytes: StorableBytes = StorableBytes(Bytes::from_static(&[0,1]));
/// ///