Skip to content

Commit

Permalink
refactor: rename diskdb to rocksdb
Browse files Browse the repository at this point in the history
  • Loading branch information
quake committed Apr 10, 2019
1 parent f745bad commit 06b4fc9
Show file tree
Hide file tree
Showing 16 changed files with 129 additions and 245 deletions.
3 changes: 2 additions & 1 deletion chain/src/chain.rs
Expand Up @@ -6,7 +6,6 @@ use ckb_core::extras::BlockExt;
use ckb_core::service::{Request, DEFAULT_CHANNEL_SIZE, SIGNAL_CHANNEL_SIZE};
use ckb_core::transaction::ProposalShortId;
use ckb_core::BlockNumber;
use ckb_db::batch::Batch;
use ckb_notify::NotifyController;
use ckb_shared::cell_set::CellSetDiff;
use ckb_shared::chain_state::ChainState;
Expand Down Expand Up @@ -235,6 +234,8 @@ impl<CI: ChainIndex + 'static> ChainService<CI> {
batch.insert_block_ext(&block.header().hash(), &ext);
}

batch.commit();

// self.shared.store().save_with_batch(|batch| {
// self.shared.store().insert_block(batch, &block);

Expand Down
1 change: 0 additions & 1 deletion db/Cargo.toml
Expand Up @@ -6,7 +6,6 @@ authors = ["Nervos Core Dev <dev@nervos.org>"]
edition = "2018"

[dependencies]
bincode = "1.1"
ckb-util = { path = "../util" }
rocksdb = "0.12.1"
fnv = "1.0.3"
Expand Down
45 changes: 0 additions & 45 deletions db/src/batch.rs

This file was deleted.

54 changes: 0 additions & 54 deletions db/src/kvdb.rs

This file was deleted.

34 changes: 29 additions & 5 deletions db/src/lib.rs
Expand Up @@ -3,13 +3,37 @@
//! This Library contains the `KeyValueDB` traits
//! which provides key-value store interface

pub mod batch;
use failure::Fail;
use std::ops::Range;
use std::result;

pub mod config;
pub mod diskdb;
pub mod kvdb;
pub mod memorydb;
pub mod rocksdb;

pub use crate::config::DBConfig;
pub use crate::diskdb::RocksDB;
pub use crate::kvdb::KeyValueDB;
pub use crate::memorydb::MemoryKeyValueDB;
pub use crate::rocksdb::RocksDB;

pub type Col = Option<u32>;
pub type Error = ErrorKind;
pub type Result<T> = result::Result<T, Error>;

#[derive(Clone, Debug, PartialEq, Eq, Fail)]
pub enum ErrorKind {
#[fail(display = "DBError {}", _0)]
DBError(String),
}

pub trait KeyValueDB: Sync + Send {
type Batch: DbBatch;
fn read(&self, col: Col, key: &[u8]) -> Result<Option<Vec<u8>>>;
fn partial_read(&self, col: Col, key: &[u8], range: &Range<usize>) -> Result<Option<Vec<u8>>>;
fn batch(&self) -> Result<Self::Batch>;
}

pub trait DbBatch {
fn insert(&mut self, col: Col, key: &[u8], value: &[u8]) -> Result<()>;
fn delete(&mut self, col: Col, key: &[u8]) -> Result<()>;
fn commit(self) -> Result<()>;
}
79 changes: 41 additions & 38 deletions db/src/memorydb.rs
@@ -1,16 +1,17 @@
use crate::batch::{Batch, Col, Operation};
use crate::kvdb::{DbBatch, ErrorKind, KeyValueDB, Result};
// for unit test
use crate::{Col, DbBatch, ErrorKind, KeyValueDB, Result};
use ckb_util::RwLock;
use fnv::FnvHashMap;
use std::ops::Range;
use std::sync::Arc;

pub type MemoryKey = Vec<u8>;
pub type MemoryValue = Vec<u8>;
pub type MemoryTable = FnvHashMap<Col, FnvHashMap<MemoryKey, MemoryValue>>;

#[derive(Default, Debug)]
pub struct MemoryKeyValueDB {
db: RwLock<MemoryTable>,
db: Arc<RwLock<MemoryTable>>,
}

impl MemoryKeyValueDB {
Expand All @@ -21,31 +22,14 @@ impl MemoryKeyValueDB {
table.insert(Some(idx as u32), FnvHashMap::default());
}
MemoryKeyValueDB {
db: RwLock::new(table),
db: Arc::new(RwLock::new(table)),
}
}
}

impl KeyValueDB for MemoryKeyValueDB {
type Batch = MemoryDbBatch;

fn write(&self, batch: Batch) -> Result<()> {
let mut db = self.db.write();
batch.operations.into_iter().for_each(|op| match op {
Operation::Insert { col, key, value } => {
if let Some(map) = db.get_mut(&col) {
map.insert(key, value);
}
}
Operation::Delete { col, key } => {
if let Some(map) = db.get_mut(&col) {
map.remove(&key);
}
}
});
Ok(())
}

fn read(&self, col: Col, key: &[u8]) -> Result<Option<MemoryValue>> {
let db = self.db.read();

Expand All @@ -67,15 +51,17 @@ impl KeyValueDB for MemoryKeyValueDB {
}
}

fn db_batch(&self) -> Result<Self::Batch> {
fn batch(&self) -> Result<Self::Batch> {
Ok(Self::Batch {
operations: Vec::new(),
db: Arc::clone(&self.db),
})
}
}

pub struct MemoryDbBatch {
operations: Vec<BatchOperation>,
db: Arc<RwLock<MemoryTable>>,
}

enum BatchOperation {
Expand All @@ -92,15 +78,37 @@ enum BatchOperation {

impl DbBatch for MemoryDbBatch {
fn insert(&mut self, col: Col, key: &[u8], value: &[u8]) -> Result<()> {
unimplemented!()
self.operations.push(BatchOperation::Insert {
col,
key: key.to_vec(),
value: value.to_vec(),
});
Ok(())
}

fn delete(&mut self, col: Col, key: &[u8]) -> Result<()> {
unimplemented!()
self.operations.push(BatchOperation::Delete {
col,
key: key.to_vec(),
});
Ok(())
}

fn commit(self) -> Result<()> {
unimplemented!()
let mut db = self.db.write();
self.operations.into_iter().for_each(|op| match op {
BatchOperation::Insert { col, key, value } => {
if let Some(map) = db.get_mut(&col) {
map.insert(key, value);
}
}
BatchOperation::Delete { col, key } => {
if let Some(map) = db.get_mut(&col) {
map.remove(&key);
}
}
});
Ok(())
}
}

Expand All @@ -111,28 +119,25 @@ mod tests {
#[test]
fn write_and_read() {
let db = MemoryKeyValueDB::open(2);
let mut batch = Batch::default();
batch.insert(None, vec![0, 0], vec![0, 0, 0]);
batch.insert(Some(1), vec![1, 1], vec![1, 1, 1]);
db.write(batch).unwrap();
let mut batch = db.batch().unwrap();
batch.insert(None, &[0, 0], &[0, 0, 0]);
batch.insert(Some(1), &[1, 1], &[1, 1, 1]);
batch.commit();

assert_eq!(Some(vec![0, 0, 0]), db.read(None, &[0, 0]).unwrap());
assert_eq!(None, db.read(None, &[1, 1]).unwrap());

assert_eq!(None, db.read(Some(1), &[0, 0]).unwrap());
assert_eq!(Some(vec![1, 1, 1]), db.read(Some(1), &[1, 1]).unwrap());

// return err when col doesn't exist
assert!(db.read(Some(2), &[0, 0]).is_err());
}

#[test]
fn write_and_partial_read() {
let db = MemoryKeyValueDB::open(2);
let mut batch = Batch::default();
batch.insert(None, vec![0, 0], vec![5, 4, 3, 2]);
batch.insert(Some(1), vec![1, 1], vec![1, 2, 3, 4, 5]);
db.write(batch).unwrap();
let mut batch = db.batch().unwrap();
batch.insert(None, &[0, 0], &[5, 4, 3, 2]);
batch.insert(Some(1), &[1, 1], &[1, 2, 3, 4, 5]);
batch.commit();

assert_eq!(
Some(vec![2, 3, 4]),
Expand All @@ -143,8 +148,6 @@ mod tests {
assert_eq!(None, db.partial_read(Some(1), &[1, 1], &(2..8)).unwrap());
// range must be increasing
assert_eq!(None, db.partial_read(Some(1), &[1, 1], &(3..0)).unwrap());
// return err when col doesn't exist
assert!(db.partial_read(Some(2), &[0, 0], &(0..1)).is_err());

assert_eq!(
Some(vec![4, 3, 2]),
Expand Down

0 comments on commit 06b4fc9

Please sign in to comment.