From 1a6d86d3b2de945e04083ac4377c01026a458545 Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Wed, 12 Jun 2024 19:46:51 -0400 Subject: [PATCH] database: remove old `cuprate-blockchain` type references --- storage/database/src/backend/heed/env.rs | 17 +---- storage/database/src/backend/tests.rs | 2 +- storage/database/src/config/reader_threads.rs | 3 +- storage/database/src/constants.rs | 12 ---- storage/database/src/env.rs | 3 +- storage/database/src/lib.rs | 62 +++++++------------ storage/database/src/storable.rs | 3 +- storage/database/src/tests.rs | 2 +- 8 files changed, 31 insertions(+), 73 deletions(-) diff --git a/storage/database/src/backend/heed/env.rs b/storage/database/src/backend/heed/env.rs index d4d0385..32676ba 100644 --- a/storage/database/src/backend/heed/env.rs +++ b/storage/database/src/backend/heed/env.rs @@ -7,12 +7,11 @@ use std::{ sync::{RwLock, RwLockReadGuard}, }; -use heed::{DatabaseOpenOptions, EnvFlags, EnvOpenOptions}; +use heed::{EnvFlags, EnvOpenOptions}; use crate::{ backend::heed::{ database::{HeedTableRo, HeedTableRw}, - storable::StorableHeed, types::HeedDb, }, config::{Config, SyncMode}, @@ -26,7 +25,7 @@ use crate::{ //---------------------------------------------------------------------------------------------------- Consts /// Panic message when there's a table missing. 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 /// A strongly typed, concrete database environment, backed by `heed`. @@ -198,18 +197,6 @@ impl Env for ConcreteEnv { // let env = unsafe { env_open_options.open(config.db_directory())? }; - /// Function that creates the tables based off the passed `T: Table`. - fn create_table( - env: &heed::Env, - tx_rw: &mut heed::RwTxn<'_>, - ) -> Result<(), InitError> { - DatabaseOpenOptions::new(env) - .name(::NAME) - .types::::Key>, StorableHeed<::Value>>() - .create(tx_rw)?; - Ok(()) - } - Ok(Self { env: RwLock::new(env), config, diff --git a/storage/database/src/backend/tests.rs b/storage/database/src/backend/tests.rs index 8034271..99e0b2d 100644 --- a/storage/database/src/backend/tests.rs +++ b/storage/database/src/backend/tests.rs @@ -1,4 +1,4 @@ -// //! Tests for `cuprate_blockchain`'s backends. +// //! Tests for `database`'s backends. // //! // //! These tests are fully trait-based, meaning there // //! is no reference to `backend/`-specific types. diff --git a/storage/database/src/config/reader_threads.rs b/storage/database/src/config/reader_threads.rs index 04216e3..59ef45e 100644 --- a/storage/database/src/config/reader_threads.rs +++ b/storage/database/src/config/reader_threads.rs @@ -15,7 +15,8 @@ use std::num::NonZeroUsize; use serde::{Deserialize, Serialize}; //---------------------------------------------------------------------------------------------------- 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 /// thread-pool will spawn to receive and send requests/responses. diff --git a/storage/database/src/constants.rs b/storage/database/src/constants.rs index 25837c2..903f5cd 100644 --- a/storage/database/src/constants.rs +++ b/storage/database/src/constants.rs @@ -3,18 +3,6 @@ //---------------------------------------------------------------------------------------------------- Import 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`: -/// -pub const DATABASE_VERSION: u64 = 0; - //---------------------------------------------------------------------------------------------------- Error Messages /// Corrupt database error message. /// diff --git a/storage/database/src/env.rs b/storage/database/src/env.rs index 1459f88..fbb948e 100644 --- a/storage/database/src/env.rs +++ b/storage/database/src/env.rs @@ -80,7 +80,8 @@ pub trait Env: Sized { /// Open the database environment, using the passed [`Config`]. /// /// # 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 /// they already exist, or else they will panic. diff --git a/storage/database/src/lib.rs b/storage/database/src/lib.rs index ec579bc..a48cf23 100644 --- a/storage/database/src/lib.rs +++ b/storage/database/src/lib.rs @@ -1,22 +1,15 @@ //! 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, -//! see [`database/README.md`](https://github.com/Cuprate/cuprate/blob/main/database/README.md). +//! For a high-level overview, see the database section in +//! [Cuprate's architecture book](https://architecture.cuprate.org). //! //! # Purpose -//! This crate does 3 things: -//! 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 +//! This crate does 3 things abstracts various database backends with traits. //! -//! Each layer builds on-top of the previous. -//! -//! 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. +//! If you need blockchain specific capabilities, consider using the higher-level +//! `cuprate-blockchain` crate which builds upon this one. //! //! # Terminology //! 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. //! //! -//! # 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 -//! The below is an example of using `cuprate_blockchain`'s -//! lowest API, i.e. using the database directly. -//! -//! For examples of the higher-level APIs, see: -//! - [`ops`] -//! - [`service`] +//! The below is an example of using `database`. //! //! ```rust -//! use cuprate_blockchain::{ +//! use database::{ //! ConcreteEnv, //! config::ConfigBuilder, //! Env, EnvInner, -//! tables::{Tables, TablesMut}, //! DatabaseRo, DatabaseRw, TxRo, TxRw, //! }; //! @@ -119,24 +96,28 @@ //! let env = ConcreteEnv::open(config)?; //! //! // 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 tx_rw = env_inner.tx_rw()?; -//! let mut tables = env_inner.open_tables_mut(&tx_rw)?; +//! let mut table = env_inner.open_db_rw::(&tx_rw)?; //! -//! // ⚠️ Write data to the tables directly. -//! // (not recommended, use `ops` or `service`). -//! const KEY_IMAGE: [u8; 32] = [88; 32]; -//! tables.key_images_mut().put(&KEY_IMAGE, &())?; +//! // Write data to the table. +//! table.put(&0, &1)?; //! //! // Commit the data written. -//! drop(tables); +//! drop(table); //! TxRw::commit(tx_rw)?; //! //! // Read the data, assert it is correct. //! let tx_ro = env_inner.tx_ro()?; -//! let tables = env_inner.open_tables(&tx_ro)?; -//! let (key_image, _) = tables.key_images().first()?; -//! assert_eq!(key_image, KEY_IMAGE); +//! let table = env_inner.open_db_ro::
(&tx_ro)?; +//! assert_eq!(table.first()?, (0, 1)); //! # Ok(()) } //! ``` @@ -245,7 +226,6 @@ pub mod config; mod constants; pub use constants::{ DATABASE_BACKEND, DATABASE_CORRUPT_MSG, DATABASE_DATA_FILENAME, DATABASE_LOCK_FILENAME, - DATABASE_VERSION, }; mod database; diff --git a/storage/database/src/storable.rs b/storage/database/src/storable.rs index 80d010c..2450129 100644 --- a/storage/database/src/storable.rs +++ b/storage/database/src/storable.rs @@ -24,7 +24,8 @@ use bytes::Bytes; /// /// This includes: /// - 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`. /// diff --git a/storage/database/src/tests.rs b/storage/database/src/tests.rs index c57b969..d9a72f1 100644 --- a/storage/database/src/tests.rs +++ b/storage/database/src/tests.rs @@ -1,4 +1,4 @@ -// //! Utilities for `cuprate_blockchain` testing. +// //! Utilities for `database` testing. // //! // //! These types/fn's are only: // //! - enabled on #[cfg(test)]