Move db traits into own file

This commit is contained in:
Paul Hauner 2018-09-18 17:39:38 +10:00
parent 9b7463f68a
commit b6197ce04d
2 changed files with 36 additions and 24 deletions

View File

@ -1,29 +1,11 @@
extern crate rocksdb;
mod disk_db;
mod traits;
pub use self::disk_db::DiskDB;
type DBValue = Vec<u8>;
#[derive(Debug)]
pub struct DBError {
message: String
}
impl DBError {
pub fn new(message: String) -> Self {
Self { message }
}
}
pub trait ClientDB: Sync + Send {
fn create_col(&mut self, col: &str)
-> Result<(), DBError>;
fn get(&self, col: &str, key: &[u8])
-> Result<Option<DBValue>, DBError>;
fn put(&self, col: &str, key: &[u8], val: &[u8])
-> Result<(), DBError>;
}
pub use self::traits::{
DBError,
DBValue,
ClientDB,
};

30
lighthouse/db/traits.rs Normal file
View File

@ -0,0 +1,30 @@
pub type DBValue = Vec<u8>;
#[derive(Debug)]
pub struct DBError {
pub message: String
}
impl DBError {
pub fn new(message: String) -> Self {
Self { message }
}
}
/// A generic database to be used by the "client' (i.e.,
/// the lighthouse blockchain client).
///
/// The purpose of having this generic trait is to allow the
/// program to use a persistent on-disk database during production,
/// but use a transient database during tests.
pub trait ClientDB: Sync + Send {
fn create_col(&mut self, col: &str)
-> Result<(), DBError>;
fn get(&self, col: &str, key: &[u8])
-> Result<Option<DBValue>, DBError>;
fn put(&self, col: &str, key: &[u8], val: &[u8])
-> Result<(), DBError>;
}