mirror of
https://github.com/hinto-janai/cuprate.git
synced 2024-12-23 03:59:37 +00:00
find/replace cuprate_blockchain
-> database
, add create_db()
This commit is contained in:
parent
1a6d86d3b2
commit
60f7fd41d6
16 changed files with 56 additions and 47 deletions
|
@ -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]
|
||||
fn clear_db<T: Table>(
|
||||
&self,
|
||||
|
|
|
@ -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<heed::Error> 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
|
||||
|
|
|
@ -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<T>(PhantomData<T>)
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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]`...
|
||||
|
|
|
@ -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)]
|
||||
|
|
|
@ -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},
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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(()) }
|
||||
//! ```
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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<impl TablesIter, RuntimeError> {
|
||||
// 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<T: Table>(&self, tx_rw: &Rw) -> Result<(), RuntimeError>;
|
||||
|
||||
// /// Open all tables in read-write mode.
|
||||
// ///
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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!(
|
||||
/// <u64 as Key>::compare([0].as_slice(), [1].as_slice()),
|
||||
/// std::cmp::Ordering::Less,
|
||||
|
|
|
@ -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::<Table>(&tx_rw)?;
|
||||
//! let mut table = env_inner.open_db_rw::<Table>(&tx_rw)?;
|
||||
//!
|
||||
//! // Write data to the table.
|
||||
|
|
|
@ -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);
|
||||
/// ```
|
||||
|
|
|
@ -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<u8> = StorableVec(vec![0,1]);
|
||||
///
|
||||
|
@ -203,7 +203,7 @@ impl<T> Borrow<[T]> for StorableVec<T> {
|
|||
/// A [`Storable`] version of [`Bytes`].
|
||||
///
|
||||
/// ```rust
|
||||
/// # use cuprate_blockchain::*;
|
||||
/// # use database::*;
|
||||
/// # use bytes::Bytes;
|
||||
/// let bytes: StorableBytes = StorableBytes(Bytes::from_static(&[0,1]));
|
||||
///
|
||||
|
|
Loading…
Reference in a new issue