mirror of
https://github.com/hinto-janai/cuprate.git
synced 2024-12-23 03:59:37 +00:00
database: remove old cuprate-blockchain
type references
This commit is contained in:
parent
fd09dfad87
commit
1a6d86d3b2
8 changed files with 31 additions and 73 deletions
|
@ -7,12 +7,11 @@ use std::{
|
||||||
sync::{RwLock, RwLockReadGuard},
|
sync::{RwLock, RwLockReadGuard},
|
||||||
};
|
};
|
||||||
|
|
||||||
use heed::{DatabaseOpenOptions, EnvFlags, EnvOpenOptions};
|
use heed::{EnvFlags, EnvOpenOptions};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
backend::heed::{
|
backend::heed::{
|
||||||
database::{HeedTableRo, HeedTableRw},
|
database::{HeedTableRo, HeedTableRw},
|
||||||
storable::StorableHeed,
|
|
||||||
types::HeedDb,
|
types::HeedDb,
|
||||||
},
|
},
|
||||||
config::{Config, SyncMode},
|
config::{Config, SyncMode},
|
||||||
|
@ -26,7 +25,7 @@ use crate::{
|
||||||
//---------------------------------------------------------------------------------------------------- Consts
|
//---------------------------------------------------------------------------------------------------- Consts
|
||||||
/// Panic message when there's a table missing.
|
/// Panic message when there's a table missing.
|
||||||
const PANIC_MSG_MISSING_TABLE: &str =
|
const PANIC_MSG_MISSING_TABLE: &str =
|
||||||
"cuprate_blockchain::Env should uphold the invariant that all tables are already created";
|
"database::Env should uphold the invariant that all tables are already created";
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------- ConcreteEnv
|
//---------------------------------------------------------------------------------------------------- ConcreteEnv
|
||||||
/// A strongly typed, concrete database environment, backed by `heed`.
|
/// A strongly typed, concrete database environment, backed by `heed`.
|
||||||
|
@ -198,18 +197,6 @@ impl Env for ConcreteEnv {
|
||||||
// <https://docs.rs/heed/0.20.0/heed/struct.EnvOpenOptions.html#method.open>
|
// <https://docs.rs/heed/0.20.0/heed/struct.EnvOpenOptions.html#method.open>
|
||||||
let env = unsafe { env_open_options.open(config.db_directory())? };
|
let env = unsafe { env_open_options.open(config.db_directory())? };
|
||||||
|
|
||||||
/// Function that creates the tables based off the passed `T: Table`.
|
|
||||||
fn create_table<T: Table>(
|
|
||||||
env: &heed::Env,
|
|
||||||
tx_rw: &mut heed::RwTxn<'_>,
|
|
||||||
) -> Result<(), InitError> {
|
|
||||||
DatabaseOpenOptions::new(env)
|
|
||||||
.name(<T as Table>::NAME)
|
|
||||||
.types::<StorableHeed<<T as Table>::Key>, StorableHeed<<T as Table>::Value>>()
|
|
||||||
.create(tx_rw)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
env: RwLock::new(env),
|
env: RwLock::new(env),
|
||||||
config,
|
config,
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
// //! Tests for `cuprate_blockchain`'s backends.
|
// //! Tests for `database`'s backends.
|
||||||
// //!
|
// //!
|
||||||
// //! These tests are fully trait-based, meaning there
|
// //! These tests are fully trait-based, meaning there
|
||||||
// //! is no reference to `backend/`-specific types.
|
// //! is no reference to `backend/`-specific types.
|
||||||
|
|
|
@ -15,7 +15,8 @@ use std::num::NonZeroUsize;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------- ReaderThreads
|
//---------------------------------------------------------------------------------------------------- ReaderThreads
|
||||||
/// Amount of database reader threads to spawn when using [`service`](crate::service).
|
/// TODO: fix me
|
||||||
|
// Amount of database reader threads to spawn when using [`service`](crate::service).
|
||||||
///
|
///
|
||||||
/// This controls how many reader thread `service`'s
|
/// This controls how many reader thread `service`'s
|
||||||
/// thread-pool will spawn to receive and send requests/responses.
|
/// thread-pool will spawn to receive and send requests/responses.
|
||||||
|
|
|
@ -3,18 +3,6 @@
|
||||||
//---------------------------------------------------------------------------------------------------- Import
|
//---------------------------------------------------------------------------------------------------- Import
|
||||||
use cfg_if::cfg_if;
|
use cfg_if::cfg_if;
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------- Version
|
|
||||||
/// Current major version of the database.
|
|
||||||
///
|
|
||||||
/// Returned by [`crate::ops::property::db_version`].
|
|
||||||
///
|
|
||||||
/// This is incremented by 1 when `cuprate_blockchain`'s
|
|
||||||
/// structure/schema/tables change.
|
|
||||||
///
|
|
||||||
/// This is akin to `VERSION` in `monerod`:
|
|
||||||
/// <https://github.com/monero-project/monero/blob/c8214782fb2a769c57382a999eaf099691c836e7/src/blockchain_db/lmdb/db_lmdb.cpp#L57>
|
|
||||||
pub const DATABASE_VERSION: u64 = 0;
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------- Error Messages
|
//---------------------------------------------------------------------------------------------------- Error Messages
|
||||||
/// Corrupt database error message.
|
/// Corrupt database error message.
|
||||||
///
|
///
|
||||||
|
|
|
@ -80,7 +80,8 @@ pub trait Env: Sized {
|
||||||
/// Open the database environment, using the passed [`Config`].
|
/// Open the database environment, using the passed [`Config`].
|
||||||
///
|
///
|
||||||
/// # Invariants
|
/// # Invariants
|
||||||
/// This function **must** create all tables listed in [`crate::tables`].
|
/// TODO: fix me
|
||||||
|
// This function **must** create all tables listed in [`crate::tables`].
|
||||||
///
|
///
|
||||||
/// The rest of the functions depend on the fact
|
/// The rest of the functions depend on the fact
|
||||||
/// they already exist, or else they will panic.
|
/// they already exist, or else they will panic.
|
||||||
|
|
|
@ -1,22 +1,15 @@
|
||||||
//! Cuprate's database abstraction.
|
//! Cuprate's database abstraction.
|
||||||
//!
|
//!
|
||||||
//! This documentation is mostly for practical usage of `cuprate_blockchain`.
|
//! This documentation is mostly for practical usage of `cuprate_database`.
|
||||||
//!
|
//!
|
||||||
//! For a high-level overview,
|
//! For a high-level overview, see the database section in
|
||||||
//! see [`database/README.md`](https://github.com/Cuprate/cuprate/blob/main/database/README.md).
|
//! [Cuprate's architecture book](https://architecture.cuprate.org).
|
||||||
//!
|
//!
|
||||||
//! # Purpose
|
//! # Purpose
|
||||||
//! This crate does 3 things:
|
//! This crate does 3 things abstracts various database backends with traits.
|
||||||
//! 1. Abstracts various database backends with traits
|
|
||||||
//! 2. Implements various `Monero` related [operations](ops), [tables], and [types]
|
|
||||||
//! 3. Exposes a [`tower::Service`] backed by a thread-pool
|
|
||||||
//!
|
//!
|
||||||
//! Each layer builds on-top of the previous.
|
//! If you need blockchain specific capabilities, consider using the higher-level
|
||||||
//!
|
//! `cuprate-blockchain` crate which builds upon this one.
|
||||||
//! As a user of `cuprate_blockchain`, consider using the higher-level [`service`] module,
|
|
||||||
//! or at the very least the [`ops`] module instead of interacting with the database traits directly.
|
|
||||||
//!
|
|
||||||
//! With that said, many database traits and internals (like [`DatabaseRo::get`]) are exposed.
|
|
||||||
//!
|
//!
|
||||||
//! # Terminology
|
//! # Terminology
|
||||||
//! To be more clear on some terms used in this crate:
|
//! To be more clear on some terms used in this crate:
|
||||||
|
@ -81,30 +74,14 @@
|
||||||
//! `tracing` is always enabled and cannot be disabled via feature-flag.
|
//! `tracing` is always enabled and cannot be disabled via feature-flag.
|
||||||
//! <!-- FIXME: tracing should be behind a feature flag -->
|
//! <!-- FIXME: tracing should be behind a feature flag -->
|
||||||
//!
|
//!
|
||||||
//! # Invariants when not using `service`
|
|
||||||
//! `cuprate_blockchain` can be used without the `service` feature enabled but
|
|
||||||
//! there are some things that must be kept in mind when doing so.
|
|
||||||
//!
|
|
||||||
//! Failing to uphold these invariants may cause panics.
|
|
||||||
//!
|
|
||||||
//! 1. `LMDB` requires the user to resize the memory map resizing (see [`RuntimeError::ResizeNeeded`]
|
|
||||||
//! 1. `LMDB` has a maximum reader transaction count, currently it is set to `128`
|
|
||||||
//! 1. `LMDB` has [maximum key/value byte size](http://www.lmdb.tech/doc/group__internal.html#gac929399f5d93cef85f874b9e9b1d09e0) which must not be exceeded
|
|
||||||
//!
|
|
||||||
//! # Examples
|
//! # Examples
|
||||||
//! The below is an example of using `cuprate_blockchain`'s
|
//! The below is an example of using `database`.
|
||||||
//! lowest API, i.e. using the database directly.
|
|
||||||
//!
|
|
||||||
//! For examples of the higher-level APIs, see:
|
|
||||||
//! - [`ops`]
|
|
||||||
//! - [`service`]
|
|
||||||
//!
|
//!
|
||||||
//! ```rust
|
//! ```rust
|
||||||
//! use cuprate_blockchain::{
|
//! use database::{
|
||||||
//! ConcreteEnv,
|
//! ConcreteEnv,
|
||||||
//! config::ConfigBuilder,
|
//! config::ConfigBuilder,
|
||||||
//! Env, EnvInner,
|
//! Env, EnvInner,
|
||||||
//! tables::{Tables, TablesMut},
|
|
||||||
//! DatabaseRo, DatabaseRw, TxRo, TxRw,
|
//! DatabaseRo, DatabaseRw, TxRo, TxRw,
|
||||||
//! };
|
//! };
|
||||||
//!
|
//!
|
||||||
|
@ -119,24 +96,28 @@
|
||||||
//! let env = ConcreteEnv::open(config)?;
|
//! let env = ConcreteEnv::open(config)?;
|
||||||
//!
|
//!
|
||||||
//! // Open up a transaction + tables for writing.
|
//! // Open up a transaction + tables for writing.
|
||||||
|
//! struct Table;
|
||||||
|
//! impl database::Table for Table {
|
||||||
|
//! const NAME: &'static str = "table"
|
||||||
|
//! type Key = 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()?;
|
||||||
//! let mut tables = env_inner.open_tables_mut(&tx_rw)?;
|
//! let mut table = env_inner.open_db_rw::<Table>(&tx_rw)?;
|
||||||
//!
|
//!
|
||||||
//! // ⚠️ Write data to the tables directly.
|
//! // Write data to the table.
|
||||||
//! // (not recommended, use `ops` or `service`).
|
//! table.put(&0, &1)?;
|
||||||
//! const KEY_IMAGE: [u8; 32] = [88; 32];
|
|
||||||
//! tables.key_images_mut().put(&KEY_IMAGE, &())?;
|
|
||||||
//!
|
//!
|
||||||
//! // Commit the data written.
|
//! // Commit the data written.
|
||||||
//! drop(tables);
|
//! drop(table);
|
||||||
//! TxRw::commit(tx_rw)?;
|
//! TxRw::commit(tx_rw)?;
|
||||||
//!
|
//!
|
||||||
//! // Read the data, assert it is correct.
|
//! // Read the data, assert it is correct.
|
||||||
//! let tx_ro = env_inner.tx_ro()?;
|
//! let tx_ro = env_inner.tx_ro()?;
|
||||||
//! let tables = env_inner.open_tables(&tx_ro)?;
|
//! let table = env_inner.open_db_ro::<Table>(&tx_ro)?;
|
||||||
//! let (key_image, _) = tables.key_images().first()?;
|
//! assert_eq!(table.first()?, (0, 1));
|
||||||
//! assert_eq!(key_image, KEY_IMAGE);
|
|
||||||
//! # Ok(()) }
|
//! # Ok(()) }
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
|
@ -245,7 +226,6 @@ pub mod config;
|
||||||
mod constants;
|
mod constants;
|
||||||
pub use constants::{
|
pub use constants::{
|
||||||
DATABASE_BACKEND, DATABASE_CORRUPT_MSG, DATABASE_DATA_FILENAME, DATABASE_LOCK_FILENAME,
|
DATABASE_BACKEND, DATABASE_CORRUPT_MSG, DATABASE_DATA_FILENAME, DATABASE_LOCK_FILENAME,
|
||||||
DATABASE_VERSION,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
mod database;
|
mod database;
|
||||||
|
|
|
@ -24,7 +24,8 @@ use bytes::Bytes;
|
||||||
///
|
///
|
||||||
/// This includes:
|
/// This includes:
|
||||||
/// - Most primitive types
|
/// - Most primitive types
|
||||||
/// - All types in [`tables`](crate::tables)
|
// TODO: fix me
|
||||||
|
// - All types in [`tables`](crate::tables)
|
||||||
///
|
///
|
||||||
/// See [`StorableVec`] & [`StorableBytes`] for storing slices of `T: Storable`.
|
/// See [`StorableVec`] & [`StorableBytes`] for storing slices of `T: Storable`.
|
||||||
///
|
///
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
// //! Utilities for `cuprate_blockchain` testing.
|
// //! Utilities for `database` testing.
|
||||||
// //!
|
// //!
|
||||||
// //! These types/fn's are only:
|
// //! These types/fn's are only:
|
||||||
// //! - enabled on #[cfg(test)]
|
// //! - enabled on #[cfg(test)]
|
||||||
|
|
Loading…
Reference in a new issue