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:
Paul Hauner 2018-09-24 14:15:59 +10:00
parent 7f01ec7c27
commit 69c97745d2
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
5 changed files with 32 additions and 35 deletions

View File

@ -1,6 +1,7 @@
use std::collections::{ HashSet, HashMap }; use std::collections::{ HashSet, HashMap };
use std::sync::RwLock; use std::sync::RwLock;
use super::blake2::blake2b::blake2b; use super::blake2::blake2b::blake2b;
use super::COLUMNS;
use super::{ use super::{
ClientDB, ClientDB,
DBValue, 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 /// 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. /// 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 db: DBHashMap = HashMap::new();
let mut known_columns: ColumnHashSet = HashSet::new(); let mut known_columns: ColumnHashSet = HashSet::new();
if let Some(columns) = columns { for col in COLUMNS.iter() {
for col in columns { known_columns.insert(col.to_string());
known_columns.insert(col.to_string());
}
} }
Self { Self {
db: RwLock::new(db), db: RwLock::new(db),
@ -103,18 +102,22 @@ mod tests {
use super::super::ClientDB; use super::super::ClientDB;
use std::thread; use std::thread;
use std::sync::Arc; use std::sync::Arc;
use super::super::stores::{
BLOCKS_DB_COLUMN,
VALIDATOR_DB_COLUMN,
};
#[test] #[test]
fn test_memorydb_column_access() { fn test_memorydb_column_access() {
let col_a: &str = "ColumnA"; let col_a: &str = BLOCKS_DB_COLUMN;
let col_b: &str = "ColumnB"; let col_b: &str = VALIDATOR_DB_COLUMN;
let column_families = vec![ let column_families = vec![
col_a, col_a,
col_b, 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 * Testing that if we write to the same key in different columns that
@ -131,7 +134,7 @@ mod tests {
#[test] #[test]
fn test_memorydb_unknown_column_access() { fn test_memorydb_unknown_column_access() {
let col_a: &str = "ColumnA"; let col_a: &str = BLOCKS_DB_COLUMN;
let col_x: &str = "ColumnX"; let col_x: &str = "ColumnX";
let column_families = vec![ let column_families = vec![
@ -139,7 +142,7 @@ mod tests {
// col_x is excluded on purpose // 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 * Test that we get errors when using undeclared columns
@ -153,15 +156,15 @@ mod tests {
#[test] #[test]
fn test_memorydb_exists() { fn test_memorydb_exists() {
let col_a: &str = "ColumnA"; let col_a: &str = BLOCKS_DB_COLUMN;
let col_b: &str = "ColumnB"; let col_b: &str = VALIDATOR_DB_COLUMN;
let column_families = vec![ let column_families = vec![
col_a, col_a,
col_b, 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 * Testing that if we write to the same key in different columns that
@ -178,10 +181,9 @@ mod tests {
#[test] #[test]
fn test_memorydb_threading() { fn test_memorydb_threading() {
let col_name: &str = "TestColumn"; let col_name: &str = BLOCKS_DB_COLUMN;
let column_families = vec![col_name];
let db = Arc::new(MemoryDB::open(Some(&column_families))); let db = Arc::new(MemoryDB::open());
let thread_count = 10; let thread_count = 10;
let write_count = 10; let write_count = 10;

View File

@ -7,7 +7,7 @@ mod traits;
pub mod stores; pub mod stores;
use super::bls; use super::bls;
use self::stores::COLUMNS;
pub use self::disk_db::DiskDB; pub use self::disk_db::DiskDB;
pub use self::memory_db::MemoryDB; pub use self::memory_db::MemoryDB;

View File

@ -46,9 +46,7 @@ mod tests {
#[test] #[test]
fn test_block_store_on_disk_db() { fn test_block_store_on_disk_db() {
let column_families = vec![DB_COLUMN]; let db = Arc::new(MemoryDB::open());
let db = Arc::new(MemoryDB::open(Some(&column_families)));
let bs = Arc::new(BlockStore::new(db.clone())); let bs = Arc::new(BlockStore::new(db.clone()));
let thread_count = 10; let thread_count = 10;

View File

@ -13,15 +13,20 @@ pub use self::validator_store::ValidatorStore;
use super::bls; use super::bls;
const BLOCKS_DB_COLUMN: &str = "blocks"; pub const BLOCKS_DB_COLUMN: &str = "blocks";
const POW_CHAIN_DB_COLUMN: &str = "powchain"; pub const POW_CHAIN_DB_COLUMN: &str = "powchain";
const VALIDATOR_DB_COLUMN: &str = "validator"; 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)] #[derive(Debug, PartialEq)]
pub enum StoreError { pub enum StoreError {
DBError(String), DBError(String),
DecodeError, DecodeError,
EncodeError,
} }
impl From<DBError> for StoreError { impl From<DBError> for StoreError {

View File

@ -76,20 +76,12 @@ impl<T: ClientDB> ValidatorStore<T> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use super::super::super::{ use super::super::super::MemoryDB;
MemoryDB,
ClientDB,
};
use super::super::bls::Keypair; use super::super::bls::Keypair;
fn open_client_db() -> MemoryDB {
let columns = vec![DB_COLUMN];
MemoryDB::open(Some(&columns))
}
#[test] #[test]
fn test_validator_store_put_get() { 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 store = ValidatorStore::new(db);
let keys = vec![ let keys = vec![
@ -122,7 +114,7 @@ mod tests {
#[test] #[test]
fn test_validator_store_bad_key() { 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 store = ValidatorStore::new(db.clone());
let key = store.get_db_key_for_index(KeyPrefixes::PublicKey, 42); let key = store.get_db_key_for_index(KeyPrefixes::PublicKey, 42);