Set MemoryDB to use constant DB columns
This is instead of needing to be passed them each time open() is called.
This commit is contained in:
parent
7f01ec7c27
commit
69c97745d2
@ -1,6 +1,7 @@
|
||||
use std::collections::{ HashSet, HashMap };
|
||||
use std::sync::RwLock;
|
||||
use super::blake2::blake2b::blake2b;
|
||||
use super::COLUMNS;
|
||||
use super::{
|
||||
ClientDB,
|
||||
DBValue,
|
||||
@ -24,13 +25,11 @@ impl MemoryDB {
|
||||
///
|
||||
/// All columns must be supplied initially, you will get an error if you try to access a column
|
||||
/// that was not declared here. This condition is enforced artificially to simulate RocksDB.
|
||||
pub fn open(columns: Option<&[&str]>) -> Self {
|
||||
pub fn open() -> Self {
|
||||
let db: DBHashMap = HashMap::new();
|
||||
let mut known_columns: ColumnHashSet = HashSet::new();
|
||||
if let Some(columns) = columns {
|
||||
for col in columns {
|
||||
known_columns.insert(col.to_string());
|
||||
}
|
||||
for col in COLUMNS.iter() {
|
||||
known_columns.insert(col.to_string());
|
||||
}
|
||||
Self {
|
||||
db: RwLock::new(db),
|
||||
@ -103,18 +102,22 @@ mod tests {
|
||||
use super::super::ClientDB;
|
||||
use std::thread;
|
||||
use std::sync::Arc;
|
||||
use super::super::stores::{
|
||||
BLOCKS_DB_COLUMN,
|
||||
VALIDATOR_DB_COLUMN,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn test_memorydb_column_access() {
|
||||
let col_a: &str = "ColumnA";
|
||||
let col_b: &str = "ColumnB";
|
||||
let col_a: &str = BLOCKS_DB_COLUMN;
|
||||
let col_b: &str = VALIDATOR_DB_COLUMN;
|
||||
|
||||
let column_families = vec![
|
||||
col_a,
|
||||
col_b,
|
||||
];
|
||||
|
||||
let db = MemoryDB::open(Some(&column_families));
|
||||
let db = MemoryDB::open();
|
||||
|
||||
/*
|
||||
* Testing that if we write to the same key in different columns that
|
||||
@ -131,7 +134,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_memorydb_unknown_column_access() {
|
||||
let col_a: &str = "ColumnA";
|
||||
let col_a: &str = BLOCKS_DB_COLUMN;
|
||||
let col_x: &str = "ColumnX";
|
||||
|
||||
let column_families = vec![
|
||||
@ -139,7 +142,7 @@ mod tests {
|
||||
// col_x is excluded on purpose
|
||||
];
|
||||
|
||||
let db = MemoryDB::open(Some(&column_families));
|
||||
let db = MemoryDB::open();
|
||||
|
||||
/*
|
||||
* Test that we get errors when using undeclared columns
|
||||
@ -153,15 +156,15 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_memorydb_exists() {
|
||||
let col_a: &str = "ColumnA";
|
||||
let col_b: &str = "ColumnB";
|
||||
let col_a: &str = BLOCKS_DB_COLUMN;
|
||||
let col_b: &str = VALIDATOR_DB_COLUMN;
|
||||
|
||||
let column_families = vec![
|
||||
col_a,
|
||||
col_b,
|
||||
];
|
||||
|
||||
let db = MemoryDB::open(Some(&column_families));
|
||||
let db = MemoryDB::open();
|
||||
|
||||
/*
|
||||
* Testing that if we write to the same key in different columns that
|
||||
@ -178,10 +181,9 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_memorydb_threading() {
|
||||
let col_name: &str = "TestColumn";
|
||||
let column_families = vec![col_name];
|
||||
let col_name: &str = BLOCKS_DB_COLUMN;
|
||||
|
||||
let db = Arc::new(MemoryDB::open(Some(&column_families)));
|
||||
let db = Arc::new(MemoryDB::open());
|
||||
|
||||
let thread_count = 10;
|
||||
let write_count = 10;
|
||||
|
@ -7,7 +7,7 @@ mod traits;
|
||||
pub mod stores;
|
||||
|
||||
use super::bls;
|
||||
|
||||
use self::stores::COLUMNS;
|
||||
|
||||
pub use self::disk_db::DiskDB;
|
||||
pub use self::memory_db::MemoryDB;
|
||||
|
@ -46,9 +46,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_block_store_on_disk_db() {
|
||||
let column_families = vec![DB_COLUMN];
|
||||
|
||||
let db = Arc::new(MemoryDB::open(Some(&column_families)));
|
||||
let db = Arc::new(MemoryDB::open());
|
||||
let bs = Arc::new(BlockStore::new(db.clone()));
|
||||
|
||||
let thread_count = 10;
|
||||
|
@ -13,15 +13,20 @@ pub use self::validator_store::ValidatorStore;
|
||||
|
||||
use super::bls;
|
||||
|
||||
const BLOCKS_DB_COLUMN: &str = "blocks";
|
||||
const POW_CHAIN_DB_COLUMN: &str = "powchain";
|
||||
const VALIDATOR_DB_COLUMN: &str = "validator";
|
||||
pub const BLOCKS_DB_COLUMN: &str = "blocks";
|
||||
pub const POW_CHAIN_DB_COLUMN: &str = "powchain";
|
||||
pub const VALIDATOR_DB_COLUMN: &str = "validator";
|
||||
|
||||
pub const COLUMNS: [&str; 3] = [
|
||||
BLOCKS_DB_COLUMN,
|
||||
POW_CHAIN_DB_COLUMN,
|
||||
VALIDATOR_DB_COLUMN,
|
||||
];
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum StoreError {
|
||||
DBError(String),
|
||||
DecodeError,
|
||||
EncodeError,
|
||||
}
|
||||
|
||||
impl From<DBError> for StoreError {
|
||||
|
@ -76,20 +76,12 @@ impl<T: ClientDB> ValidatorStore<T> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use super::super::super::{
|
||||
MemoryDB,
|
||||
ClientDB,
|
||||
};
|
||||
use super::super::super::MemoryDB;
|
||||
use super::super::bls::Keypair;
|
||||
|
||||
fn open_client_db() -> MemoryDB {
|
||||
let columns = vec![DB_COLUMN];
|
||||
MemoryDB::open(Some(&columns))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validator_store_put_get() {
|
||||
let db = Arc::new(open_client_db());
|
||||
let db = Arc::new(MemoryDB::open());
|
||||
let store = ValidatorStore::new(db);
|
||||
|
||||
let keys = vec![
|
||||
@ -122,7 +114,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_validator_store_bad_key() {
|
||||
let db = Arc::new(open_client_db());
|
||||
let db = Arc::new(MemoryDB::open());
|
||||
let store = ValidatorStore::new(db.clone());
|
||||
|
||||
let key = store.get_db_key_for_index(KeyPrefixes::PublicKey, 42);
|
||||
|
Loading…
Reference in New Issue
Block a user