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

View File

@ -1,14 +1,21 @@
extern crate rocksdb; extern crate rocksdb;
use std::fs; mod disk_db;
use std::path::Path;
pub use self::rocksdb::DB;
pub fn open_db(path: &Path) -> DB { pub use self::disk_db::DiskDB;
let db_path = path.join("rocksdb");
fs::create_dir_all(&db_path) #[derive(Debug)]
.expect(&format!("Unable to create {:?}", &db_path)); pub struct DBError {
let db = DB::open_default(db_path.join("lighthouse.rdb")) message: String
.expect("Unable to open local database."); }
db
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; use slog::Logger;
pub enum BlockStatus { pub enum BlockStatus {
@ -14,7 +15,7 @@ pub enum BlockStatus {
pub fn process_unverified_blocks( pub fn process_unverified_blocks(
_serialized_block: &[u8], _serialized_block: &[u8],
_db: &DB, _db: Arc<ClientDB>,
_log: Logger) _log: Logger)
{ {
// //

View File

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

View File

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