mirror of
https://github.com/hinto-janai/cuprate.git
synced 2024-12-23 03:59:37 +00:00
database: remove Tables
references
This commit is contained in:
parent
a156abf36a
commit
fd09dfad87
4 changed files with 650 additions and 666 deletions
|
@ -21,7 +21,6 @@ use crate::{
|
|||
error::{InitError, RuntimeError},
|
||||
resize::ResizeAlgorithm,
|
||||
table::Table,
|
||||
tables::call_fn_on_all_tables_or_early_return,
|
||||
};
|
||||
|
||||
//---------------------------------------------------------------------------------------------------- Consts
|
||||
|
@ -211,22 +210,6 @@ impl Env for ConcreteEnv {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
let mut tx_rw = env.write_txn()?;
|
||||
// Create all tables.
|
||||
// FIXME: this macro is kinda awkward.
|
||||
{
|
||||
let env = &env;
|
||||
let tx_rw = &mut tx_rw;
|
||||
match call_fn_on_all_tables_or_early_return!(create_table(env, tx_rw)) {
|
||||
Ok(_) => (),
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
// INVARIANT: this should never return `ResizeNeeded` due to adding
|
||||
// some tables since we added some leeway to the memory map above.
|
||||
tx_rw.commit()?;
|
||||
|
||||
Ok(Self {
|
||||
env: RwLock::new(env),
|
||||
config,
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -9,7 +9,6 @@ use crate::{
|
|||
error::{InitError, RuntimeError},
|
||||
resize::ResizeAlgorithm,
|
||||
table::Table,
|
||||
tables::{call_fn_on_all_tables_or_early_return, TablesIter, TablesMut},
|
||||
transaction::{TxRo, TxRw},
|
||||
};
|
||||
|
||||
|
@ -249,29 +248,31 @@ where
|
|||
#[doc = doc_table_error!()]
|
||||
fn open_db_rw<T: Table>(&self, tx_rw: &Rw) -> Result<impl DatabaseRw<T>, RuntimeError>;
|
||||
|
||||
/// 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)
|
||||
}
|
||||
}
|
||||
// TODO: make equivalent in `cuprate-blockchain`.
|
||||
|
||||
/// Open all tables in read-write mode.
|
||||
///
|
||||
/// This calls [`EnvInner::open_db_rw`] on all database tables
|
||||
/// and returns a structure that allows access to all tables.
|
||||
///
|
||||
#[doc = doc_table_error!()]
|
||||
fn open_tables_mut(&self, tx_rw: &Rw) -> Result<impl TablesMut, RuntimeError> {
|
||||
call_fn_on_all_tables_or_early_return! {
|
||||
Self::open_db_rw(self, tx_rw)
|
||||
}
|
||||
}
|
||||
// /// 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)
|
||||
// }
|
||||
// }
|
||||
|
||||
// /// Open all tables in read-write mode.
|
||||
// ///
|
||||
// /// This calls [`EnvInner::open_db_rw`] on all database tables
|
||||
// /// and returns a structure that allows access to all tables.
|
||||
// ///
|
||||
// #[doc = doc_table_error!()]
|
||||
// fn open_tables_mut(&self, tx_rw: &Rw) -> Result<impl TablesMut, RuntimeError> {
|
||||
// call_fn_on_all_tables_or_early_return! {
|
||||
// Self::open_db_rw(self, tx_rw)
|
||||
// }
|
||||
// }
|
||||
|
||||
/// Clear all `(key, value)`'s from a database table.
|
||||
///
|
||||
|
|
|
@ -1,85 +1,85 @@
|
|||
//! Utilities for `cuprate_blockchain` testing.
|
||||
//!
|
||||
//! These types/fn's are only:
|
||||
//! - enabled on #[cfg(test)]
|
||||
//! - only used internally
|
||||
// //! Utilities for `cuprate_blockchain` testing.
|
||||
// //!
|
||||
// //! These types/fn's are only:
|
||||
// //! - enabled on #[cfg(test)]
|
||||
// //! - only used internally
|
||||
|
||||
//---------------------------------------------------------------------------------------------------- Import
|
||||
use std::fmt::Debug;
|
||||
// //---------------------------------------------------------------------------------------------------- Import
|
||||
// use std::fmt::Debug;
|
||||
|
||||
use pretty_assertions::assert_eq;
|
||||
// use pretty_assertions::assert_eq;
|
||||
|
||||
use crate::{config::ConfigBuilder, tables::Tables, ConcreteEnv, DatabaseRo, Env, EnvInner};
|
||||
// use crate::{config::ConfigBuilder, tables::Tables, ConcreteEnv, DatabaseRo, Env, EnvInner};
|
||||
|
||||
//---------------------------------------------------------------------------------------------------- Struct
|
||||
/// Named struct to assert the length of all tables.
|
||||
///
|
||||
/// This is a struct with fields instead of a function
|
||||
/// so that callers can name arguments, otherwise the call-site
|
||||
/// is a little confusing, i.e. `assert_table_len(0, 25, 1, 123)`.
|
||||
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub(crate) struct AssertTableLen {
|
||||
pub(crate) block_infos: u64,
|
||||
pub(crate) block_blobs: u64,
|
||||
pub(crate) block_heights: u64,
|
||||
pub(crate) key_images: u64,
|
||||
pub(crate) num_outputs: u64,
|
||||
pub(crate) pruned_tx_blobs: u64,
|
||||
pub(crate) prunable_hashes: u64,
|
||||
pub(crate) outputs: u64,
|
||||
pub(crate) prunable_tx_blobs: u64,
|
||||
pub(crate) rct_outputs: u64,
|
||||
pub(crate) tx_blobs: u64,
|
||||
pub(crate) tx_ids: u64,
|
||||
pub(crate) tx_heights: u64,
|
||||
pub(crate) tx_unlock_time: u64,
|
||||
}
|
||||
// //---------------------------------------------------------------------------------------------------- Struct
|
||||
// /// Named struct to assert the length of all tables.
|
||||
// ///
|
||||
// /// This is a struct with fields instead of a function
|
||||
// /// so that callers can name arguments, otherwise the call-site
|
||||
// /// is a little confusing, i.e. `assert_table_len(0, 25, 1, 123)`.
|
||||
// #[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
// pub(crate) struct AssertTableLen {
|
||||
// pub(crate) block_infos: u64,
|
||||
// pub(crate) block_blobs: u64,
|
||||
// pub(crate) block_heights: u64,
|
||||
// pub(crate) key_images: u64,
|
||||
// pub(crate) num_outputs: u64,
|
||||
// pub(crate) pruned_tx_blobs: u64,
|
||||
// pub(crate) prunable_hashes: u64,
|
||||
// pub(crate) outputs: u64,
|
||||
// pub(crate) prunable_tx_blobs: u64,
|
||||
// pub(crate) rct_outputs: u64,
|
||||
// pub(crate) tx_blobs: u64,
|
||||
// pub(crate) tx_ids: u64,
|
||||
// pub(crate) tx_heights: u64,
|
||||
// pub(crate) tx_unlock_time: u64,
|
||||
// }
|
||||
|
||||
impl AssertTableLen {
|
||||
/// Assert the length of all tables.
|
||||
pub(crate) fn assert(self, tables: &impl Tables) {
|
||||
let other = Self {
|
||||
block_infos: tables.block_infos().len().unwrap(),
|
||||
block_blobs: tables.block_blobs().len().unwrap(),
|
||||
block_heights: tables.block_heights().len().unwrap(),
|
||||
key_images: tables.key_images().len().unwrap(),
|
||||
num_outputs: tables.num_outputs().len().unwrap(),
|
||||
pruned_tx_blobs: tables.pruned_tx_blobs().len().unwrap(),
|
||||
prunable_hashes: tables.prunable_hashes().len().unwrap(),
|
||||
outputs: tables.outputs().len().unwrap(),
|
||||
prunable_tx_blobs: tables.prunable_tx_blobs().len().unwrap(),
|
||||
rct_outputs: tables.rct_outputs().len().unwrap(),
|
||||
tx_blobs: tables.tx_blobs().len().unwrap(),
|
||||
tx_ids: tables.tx_ids().len().unwrap(),
|
||||
tx_heights: tables.tx_heights().len().unwrap(),
|
||||
tx_unlock_time: tables.tx_unlock_time().len().unwrap(),
|
||||
};
|
||||
// impl AssertTableLen {
|
||||
// /// Assert the length of all tables.
|
||||
// pub(crate) fn assert(self, tables: &impl Tables) {
|
||||
// let other = Self {
|
||||
// block_infos: tables.block_infos().len().unwrap(),
|
||||
// block_blobs: tables.block_blobs().len().unwrap(),
|
||||
// block_heights: tables.block_heights().len().unwrap(),
|
||||
// key_images: tables.key_images().len().unwrap(),
|
||||
// num_outputs: tables.num_outputs().len().unwrap(),
|
||||
// pruned_tx_blobs: tables.pruned_tx_blobs().len().unwrap(),
|
||||
// prunable_hashes: tables.prunable_hashes().len().unwrap(),
|
||||
// outputs: tables.outputs().len().unwrap(),
|
||||
// prunable_tx_blobs: tables.prunable_tx_blobs().len().unwrap(),
|
||||
// rct_outputs: tables.rct_outputs().len().unwrap(),
|
||||
// tx_blobs: tables.tx_blobs().len().unwrap(),
|
||||
// tx_ids: tables.tx_ids().len().unwrap(),
|
||||
// tx_heights: tables.tx_heights().len().unwrap(),
|
||||
// tx_unlock_time: tables.tx_unlock_time().len().unwrap(),
|
||||
// };
|
||||
|
||||
assert_eq!(self, other);
|
||||
}
|
||||
}
|
||||
// assert_eq!(self, other);
|
||||
// }
|
||||
// }
|
||||
|
||||
//---------------------------------------------------------------------------------------------------- fn
|
||||
/// Create an `Env` in a temporarily directory.
|
||||
/// The directory is automatically removed after the `TempDir` is dropped.
|
||||
///
|
||||
/// FIXME: changing this to `-> impl Env` causes lifetime errors...
|
||||
pub(crate) fn tmp_concrete_env() -> (ConcreteEnv, tempfile::TempDir) {
|
||||
let tempdir = tempfile::tempdir().unwrap();
|
||||
let config = ConfigBuilder::new()
|
||||
.db_directory(tempdir.path().into())
|
||||
.low_power()
|
||||
.build();
|
||||
let env = ConcreteEnv::open(config).unwrap();
|
||||
// //---------------------------------------------------------------------------------------------------- fn
|
||||
// /// Create an `Env` in a temporarily directory.
|
||||
// /// The directory is automatically removed after the `TempDir` is dropped.
|
||||
// ///
|
||||
// /// FIXME: changing this to `-> impl Env` causes lifetime errors...
|
||||
// pub(crate) fn tmp_concrete_env() -> (ConcreteEnv, tempfile::TempDir) {
|
||||
// let tempdir = tempfile::tempdir().unwrap();
|
||||
// let config = ConfigBuilder::new()
|
||||
// .db_directory(tempdir.path().into())
|
||||
// .low_power()
|
||||
// .build();
|
||||
// let env = ConcreteEnv::open(config).unwrap();
|
||||
|
||||
(env, tempdir)
|
||||
}
|
||||
// (env, tempdir)
|
||||
// }
|
||||
|
||||
/// Assert all the tables in the environment are empty.
|
||||
pub(crate) fn assert_all_tables_are_empty(env: &ConcreteEnv) {
|
||||
let env_inner = env.env_inner();
|
||||
let tx_ro = env_inner.tx_ro().unwrap();
|
||||
let tables = env_inner.open_tables(&tx_ro).unwrap();
|
||||
assert!(tables.all_tables_empty().unwrap());
|
||||
assert_eq!(crate::ops::tx::get_num_tx(tables.tx_ids()).unwrap(), 0);
|
||||
}
|
||||
// /// Assert all the tables in the environment are empty.
|
||||
// pub(crate) fn assert_all_tables_are_empty(env: &ConcreteEnv) {
|
||||
// let env_inner = env.env_inner();
|
||||
// let tx_ro = env_inner.tx_ro().unwrap();
|
||||
// let tables = env_inner.open_tables(&tx_ro).unwrap();
|
||||
// assert!(tables.all_tables_empty().unwrap());
|
||||
// assert_eq!(crate::ops::tx::get_num_tx(tables.tx_ids()).unwrap(), 0);
|
||||
// }
|
||||
|
|
Loading…
Reference in a new issue