Partially implemented db wrapper

Addresses issue #12
This commit is contained in:
Paul Hauner 2018-09-17 17:52:32 +10:00
parent 513972b75c
commit 33b1e6ddf4
5 changed files with 31 additions and 21 deletions

View File

@ -1,6 +1,6 @@
use std::sync::Arc;
use std::thread;
use super::db::{ DB, open_db };
use super::db::{ DiskDB };
use super::config::LighthouseConfig;
use super::futures::sync::mpsc::{
unbounded,
@ -10,10 +10,12 @@ use super::network_libp2p::state::NetworkState;
use super::slog::Logger;
use super::sync::run_sync_future;
use super::db::ClientDB;
/// Represents the co-ordination of the
/// networking, syncing and RPC (not-yet-implemented) threads.
pub struct Client {
pub db: Arc<DB>,
pub db: Arc<ClientDB>,
pub network_thread: thread::JoinHandle<()>,
pub sync_thread: thread::JoinHandle<()>,
}
@ -29,7 +31,7 @@ impl Client {
{
// Open the local db
let db = {
let db = open_db(&config.data_dir);
let db = DiskDB::open(&config.data_dir);
Arc::new(db)
};

View File

@ -1,14 +1,21 @@
extern crate rocksdb;
use std::fs;
use std::path::Path;
pub use self::rocksdb::DB;
mod disk_db;
pub fn open_db(path: &Path) -> DB {
let db_path = path.join("rocksdb");
fs::create_dir_all(&db_path)
.expect(&format!("Unable to create {:?}", &db_path));
let db = DB::open_default(db_path.join("lighthouse.rdb"))
.expect("Unable to open local database.");
db
pub use self::disk_db::DiskDB;
#[derive(Debug)]
pub struct DBError {
message: String
}
impl DBError {
fn new(message: String) -> Self {
Self { message }
}
}
pub trait ClientDB: Sync + Send {
fn get(&self, col: &str, key: &[u8])
-> Result<Option<&[u8]>, DBError>;
}

View File

@ -1,4 +1,5 @@
use super::db::DB;
use std::sync::Arc;
use super::db::ClientDB;
use slog::Logger;
pub enum BlockStatus {
@ -14,7 +15,7 @@ pub enum BlockStatus {
pub fn process_unverified_blocks(
_serialized_block: &[u8],
_db: &DB,
_db: Arc<ClientDB>,
_log: Logger)
{
//

View File

@ -1,5 +1,5 @@
use std::sync::Arc;
use super::db::DB;
use super::db::ClientDB;
use slog::Logger;
use super::network_libp2p::message::{
@ -25,7 +25,7 @@ use super::futures::sync::mpsc::{
/// (e.g., libp2p) has an event to push up to the sync process.
pub fn handle_network_event(
event: NetworkEvent,
db: Arc<DB>,
db: Arc<ClientDB>,
network_tx: UnboundedSender<OutgoingMessage>,
log: Logger)
-> Result<(), ()>
@ -39,7 +39,7 @@ pub fn handle_network_event(
if let Some(data) = event.data {
handle_network_message(
data,
&db,
db,
network_tx,
log)
} else {
@ -56,7 +56,7 @@ pub fn handle_network_event(
/// (e.g., libp2p) has sent a message to us.
fn handle_network_message(
message: Vec<u8>,
db: &DB,
db: Arc<ClientDB>,
_network_tx: UnboundedSender<OutgoingMessage>,
log: Logger)
-> Result<(), ()>

View File

@ -10,7 +10,7 @@ use super::network_libp2p::message::{
};
use super::network::handle_network_event;
use std::sync::Arc;
use super::db::DB;
use super::db::ClientDB;
use slog::Logger;
type NetworkSender = UnboundedSender<OutgoingMessage>;
@ -25,7 +25,7 @@ type SyncReceiver = UnboundedReceiver<Vec<u8>>;
/// from the network and the RPC and update
/// the state.
pub fn run_sync_future(
db: Arc<DB>,
db: Arc<ClientDB>,
network_tx: NetworkSender,
network_rx: NetworkReceiver,
_sync_tx: SyncSender,