Skip to content

Commit

Permalink
feat: add DBIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
quake committed Apr 10, 2019
1 parent 7901f50 commit d7d658e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
9 changes: 9 additions & 0 deletions db/src/diskdb.rs
Expand Up @@ -103,6 +103,15 @@ impl KeyValueDB for RocksDB {
.map(|v| v.and_then(|vi| vi.get(range.start..range.end).map(|slice| slice.to_vec())))
.map_err(Into::into)
}

fn iter(&self, col: Col, key: &[u8]) -> Option<DBIterator> {
self.cf_handle(col).expect("invalid col").map(|cf| {
self.inner
.db
.iterator_cf(cf, IteratorMode::From(key, Direction::Forward))
.expect("invalid iterator")
})
}
}

#[cfg(test)]
Expand Down
8 changes: 7 additions & 1 deletion db/src/kvdb.rs
@@ -1,13 +1,14 @@
use crate::batch::{Batch, Col};
use bincode::Error as BcError;
use failure::Fail;
use rocksdb::Error as RdbError;
use rocksdb::{DBIterator as RdbIterator, Error as RdbError};
use std::error::Error as StdError;
use std::ops::Range;
use std::result;

pub type Error = ErrorKind;
pub type Result<T> = result::Result<T, Error>;
pub type DBIterator<'a> = RdbIterator<'a>;

#[derive(Clone, Debug, PartialEq, Eq, Fail)]
pub enum ErrorKind {
Expand Down Expand Up @@ -36,4 +37,9 @@ pub trait KeyValueDB: Sync + Send {
fn batch(&self) -> Batch {
Batch::new()
}
/// returns an iterator over a column, starts from a key in forward direction.
/// TODO use Rocksdb's DBIterator as a temp soluction, refactor it to associated type, same as Batch
fn iter(&self, col: Col, key: &[u8]) -> Option<DBIterator> {
None
}
}
6 changes: 5 additions & 1 deletion shared/src/cachedb.rs
@@ -1,5 +1,5 @@
use ckb_db::batch::{Batch, Col, Operation};
use ckb_db::kvdb::{KeyValueDB, Result};
use ckb_db::kvdb::{DBIterator, KeyValueDB, Result};
use ckb_util::RwLock;
use fnv::FnvHashMap;
use lru_cache::LruCache;
Expand Down Expand Up @@ -72,4 +72,8 @@ where
}
self.db.partial_read(col, key, range)
}

fn iter(&self, col: Col, key: &[u8]) -> Option<DBIterator> {
self.db.iter(col, key)
}
}

0 comments on commit d7d658e

Please sign in to comment.