Skip to content

Commit

Permalink
refactor: change Col from Option to u32
Browse files Browse the repository at this point in the history
  • Loading branch information
quake committed Apr 10, 2019
1 parent bee5bb9 commit ecb4ec0
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 84 deletions.
2 changes: 1 addition & 1 deletion db/src/lib.rs
Expand Up @@ -15,7 +15,7 @@ pub use crate::config::DBConfig;
pub use crate::memorydb::MemoryKeyValueDB;
pub use crate::rocksdb::RocksDB;

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

Expand Down
29 changes: 14 additions & 15 deletions db/src/memorydb.rs
Expand Up @@ -17,9 +17,8 @@ pub struct MemoryKeyValueDB {
impl MemoryKeyValueDB {
pub fn open(cols: usize) -> MemoryKeyValueDB {
let mut table = FnvHashMap::with_capacity_and_hasher(cols, Default::default());
table.insert(None, FnvHashMap::default());
for idx in 0..cols {
table.insert(Some(idx as u32), FnvHashMap::default());
table.insert(idx as u32, FnvHashMap::default());
}
MemoryKeyValueDB {
db: Arc::new(RwLock::new(table)),
Expand Down Expand Up @@ -120,38 +119,38 @@ mod tests {
fn write_and_read() {
let db = MemoryKeyValueDB::open(2);
let mut batch = db.batch().unwrap();
batch.insert(None, &[0, 0], &[0, 0, 0]).unwrap();
batch.insert(Some(1), &[1, 1], &[1, 1, 1]).unwrap();
batch.insert(0, &[0, 0], &[0, 0, 0]).unwrap();
batch.insert(1, &[1, 1], &[1, 1, 1]).unwrap();
batch.commit().unwrap();

assert_eq!(Some(vec![0, 0, 0]), db.read(None, &[0, 0]).unwrap());
assert_eq!(None, db.read(None, &[1, 1]).unwrap());
assert_eq!(Some(vec![0, 0, 0]), db.read(0, &[0, 0]).unwrap());
assert_eq!(None, db.read(0, &[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());
assert_eq!(None, db.read(1, &[0, 0]).unwrap());
assert_eq!(Some(vec![1, 1, 1]), db.read(1, &[1, 1]).unwrap());
}

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

assert_eq!(
Some(vec![2, 3, 4]),
db.partial_read(Some(1), &[1, 1], &(1..4)).unwrap()
db.partial_read(1, &[1, 1], &(1..4)).unwrap()
);
assert_eq!(None, db.partial_read(Some(1), &[0, 0], &(1..4)).unwrap());
assert_eq!(None, db.partial_read(1, &[0, 0], &(1..4)).unwrap());
// return None when invalid range is passed
assert_eq!(None, db.partial_read(Some(1), &[1, 1], &(2..8)).unwrap());
assert_eq!(None, db.partial_read(1, &[1, 1], &(2..8)).unwrap());
// range must be increasing
assert_eq!(None, db.partial_read(Some(1), &[1, 1], &(3..0)).unwrap());
assert_eq!(None, db.partial_read(1, &[1, 1], &(3..0)).unwrap());

assert_eq!(
Some(vec![4, 3, 2]),
db.partial_read(None, &[0, 0], &(1..4)).unwrap()
db.partial_read(0, &[0, 0], &(1..4)).unwrap()
);
}
}
96 changes: 39 additions & 57 deletions db/src/rocksdb.rs
Expand Up @@ -51,33 +51,25 @@ impl KeyValueDB for RocksDB {
type Batch = RocksdbBatch;

fn read(&self, col: Col, key: &[u8]) -> Result<Option<Vec<u8>>> {
match col {
Some(col) => {
let cf = self
.inner
.cf_handle(&col.to_string())
.expect("column not found");
self.inner.get_cf(cf, &key)
}
None => self.inner.get(&key),
}
.map(|v| v.map(|vi| vi.to_vec()))
.map_err(Into::into)
let cf = self
.inner
.cf_handle(&col.to_string())
.expect("column not found");
self.inner
.get_cf(cf, &key)
.map(|v| v.map(|vi| vi.to_vec()))
.map_err(Into::into)
}

fn partial_read(&self, col: Col, key: &[u8], range: &Range<usize>) -> Result<Option<Vec<u8>>> {
match col {
Some(col) => {
let cf = self
.inner
.cf_handle(&col.to_string())
.expect("column not found");
self.inner.get_pinned_cf(cf, &key)
}
None => self.inner.get_pinned(&key),
}
.map(|v| v.and_then(|vi| vi.get(range.start..range.end).map(|slice| slice.to_vec())))
.map_err(Into::into)
let cf = self
.inner
.cf_handle(&col.to_string())
.expect("column not found");
self.inner
.get_pinned_cf(cf, &key)
.map(|v| v.and_then(|vi| vi.get(range.start..range.end).map(|slice| slice.to_vec())))
.map_err(Into::into)
}

fn batch(&self) -> Result<Self::Batch> {
Expand All @@ -95,30 +87,20 @@ pub struct RocksdbBatch {

impl DbBatch for RocksdbBatch {
fn insert(&mut self, col: Col, key: &[u8], value: &[u8]) -> Result<()> {
match col {
Some(col) => {
let cf = self
.db
.cf_handle(&col.to_string())
.expect("column not found");
self.wb.put_cf(cf, key, value)?
}
None => self.wb.put(key, value)?,
}
let cf = self
.db
.cf_handle(&col.to_string())
.expect("column not found");
self.wb.put_cf(cf, key, value)?;
Ok(())
}

fn delete(&mut self, col: Col, key: &[u8]) -> Result<()> {
match col {
Some(col) => {
let cf = self
.db
.cf_handle(&col.to_string())
.expect("column not found");
self.wb.delete_cf(cf, &key)?
}
None => self.wb.delete(key)?,
}
let cf = self
.db
.cf_handle(&col.to_string())
.expect("column not found");
self.wb.delete_cf(cf, &key)?;
Ok(())
}

Expand Down Expand Up @@ -190,39 +172,39 @@ mod tests {
let db = setup_db("write_and_read", 2);

let mut batch = db.batch().unwrap();
batch.insert(None, &[0, 0], &[0, 0, 0]).unwrap();
batch.insert(Some(1), &[1, 1], &[1, 1, 1]).unwrap();
batch.insert(0, &[0, 0], &[0, 0, 0]).unwrap();
batch.insert(1, &[1, 1], &[1, 1, 1]).unwrap();
batch.commit().unwrap();

assert_eq!(Some(vec![0, 0, 0]), db.read(None, &[0, 0]).unwrap());
assert_eq!(None, db.read(None, &[1, 1]).unwrap());
assert_eq!(Some(vec![0, 0, 0]), db.read(0, &[0, 0]).unwrap());
assert_eq!(None, db.read(0, &[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());
assert_eq!(None, db.read(1, &[0, 0]).unwrap());
assert_eq!(Some(vec![1, 1, 1]), db.read(1, &[1, 1]).unwrap());
}

#[test]
fn write_and_partial_read() {
let db = setup_db("write_and_partial_read", 2);

let mut batch = db.batch().unwrap();
batch.insert(None, &[0, 0], &[5, 4, 3, 2]).unwrap();
batch.insert(Some(1), &[1, 1], &[1, 2, 3, 4, 5]).unwrap();
batch.insert(0, &[0, 0], &[5, 4, 3, 2]).unwrap();
batch.insert(1, &[1, 1], &[1, 2, 3, 4, 5]).unwrap();
batch.commit().unwrap();

assert_eq!(
Some(vec![2, 3, 4]),
db.partial_read(Some(1), &[1, 1], &(1..4)).unwrap()
db.partial_read(1, &[1, 1], &(1..4)).unwrap()
);
assert_eq!(None, db.partial_read(Some(1), &[0, 0], &(1..4)).unwrap());
assert_eq!(None, db.partial_read(1, &[0, 0], &(1..4)).unwrap());
// return None when invalid range is passed
assert_eq!(None, db.partial_read(Some(1), &[1, 1], &(2..8)).unwrap());
assert_eq!(None, db.partial_read(1, &[1, 1], &(2..8)).unwrap());
// range must be increasing
assert_eq!(None, db.partial_read(Some(1), &[1, 1], &(3..0)).unwrap());
assert_eq!(None, db.partial_read(1, &[1, 1], &(3..0)).unwrap());

assert_eq!(
Some(vec![4, 3, 2]),
db.partial_read(None, &[0, 0], &(1..4)).unwrap()
db.partial_read(0, &[0, 0], &(1..4)).unwrap()
);
}
}
2 changes: 1 addition & 1 deletion shared/src/cachedb.rs
Expand Up @@ -22,7 +22,7 @@ where
pub fn new(db: T, cols: &[CacheCols]) -> Self {
let mut table = FnvHashMap::with_capacity_and_hasher(cols.len(), Default::default());
for (idx, capacity) in cols {
table.insert(Some(*idx), LruCache::new(*capacity));
table.insert(*idx, LruCache::new(*capacity));
}
CacheDB {
db,
Expand Down
18 changes: 9 additions & 9 deletions shared/src/lib.rs
Expand Up @@ -24,12 +24,12 @@ mod tests;
use ckb_db::Col;

pub const COLUMNS: u32 = 9;
pub const COLUMN_INDEX: Col = Some(0);
pub const COLUMN_BLOCK_HEADER: Col = Some(1);
pub const COLUMN_BLOCK_BODY: Col = Some(2);
pub const COLUMN_BLOCK_UNCLE: Col = Some(3);
pub const COLUMN_META: Col = Some(4);
pub const COLUMN_TRANSACTION_ADDR: Col = Some(5);
pub const COLUMN_EXT: Col = Some(6);
pub const COLUMN_BLOCK_TRANSACTION_ADDRESSES: Col = Some(7);
pub const COLUMN_BLOCK_PROPOSAL_IDS: Col = Some(8);
pub const COLUMN_INDEX: Col = 0;
pub const COLUMN_BLOCK_HEADER: Col = 1;
pub const COLUMN_BLOCK_BODY: Col = 2;
pub const COLUMN_BLOCK_UNCLE: Col = 3;
pub const COLUMN_META: Col = 4;
pub const COLUMN_TRANSACTION_ADDR: Col = 5;
pub const COLUMN_EXT: Col = 6;
pub const COLUMN_BLOCK_TRANSACTION_ADDRESSES: Col = 7;
pub const COLUMN_BLOCK_PROPOSAL_IDS: Col = 8;
2 changes: 1 addition & 1 deletion shared/src/shared.rs
Expand Up @@ -359,7 +359,7 @@ impl SharedBuilder<CacheDB<RocksDB>> {
pub fn db(mut self, config: &DBConfig) -> Self {
self.db = Some(CacheDB::new(
RocksDB::open(config, COLUMNS),
&[(COLUMN_BLOCK_HEADER.unwrap(), 4096)],
&[(COLUMN_BLOCK_HEADER, 4096)],
));
self
}
Expand Down

0 comments on commit ecb4ec0

Please sign in to comment.