Commit c84cccfe authored by goroutine's avatar goroutine Committed by GitHub

Merge pull request #31 from zhangjinpeng1987/master

add set_iterate_upper_bound api for readoptions
parents 0d41f716 30463dab
...@@ -143,7 +143,7 @@ fn main() { ...@@ -143,7 +143,7 @@ fn main() {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use rocksdb::{BlockBasedOptions, DB, DBCompressionType, Options}; use rocksdb::{BlockBasedOptions, DB, DBCompressionType, Options, ReadOptions, WriteOptions, SeekKey};
use rocksdb::DBCompactionStyle::DBUniversal; use rocksdb::DBCompactionStyle::DBUniversal;
#[allow(dead_code)] #[allow(dead_code)]
...@@ -192,6 +192,33 @@ mod tests { ...@@ -192,6 +192,33 @@ mod tests {
DB::open(&opts, path).unwrap() DB::open(&opts, path).unwrap()
} }
#[test]
fn read_with_upper_bound() {
let path = "_rust_rocksdb_read_with_upper_bound_test";
let mut opts = Options::new();
opts.create_if_missing(true);
{
let db = DB::open(&opts, path).unwrap();
let writeopts = WriteOptions::new();
db.put_opt(b"k1-0", b"a", &writeopts).unwrap();
db.put_opt(b"k1-1", b"b", &writeopts).unwrap();
db.put_opt(b"k2-0", b"c", &writeopts).unwrap();
let mut readopts = ReadOptions::new();
readopts.set_iterate_upper_bound(b"k2");
let mut iter = db.iter_opt(readopts);
iter.seek(SeekKey::Start);
let mut count = 0;
while iter.valid() {
count += 1;
if !iter.next() {
break;
}
}
assert_eq!(count, 2);
}
}
// TODO(tyler) unstable // TODO(tyler) unstable
// #[bench] // #[bench]
// fn a_writes(b: &mut Bencher) { // fn a_writes(b: &mut Bencher) {
......
...@@ -42,6 +42,7 @@ pub struct WriteBatch { ...@@ -42,6 +42,7 @@ pub struct WriteBatch {
pub struct ReadOptions { pub struct ReadOptions {
inner: rocksdb_ffi::DBReadOptions, inner: rocksdb_ffi::DBReadOptions,
upper_bound: Vec<u8>,
} }
/// The UnsafeSnap must be destroyed by db, it maybe be leaked /// The UnsafeSnap must be destroyed by db, it maybe be leaked
...@@ -62,6 +63,7 @@ pub struct Snapshot<'a> { ...@@ -62,6 +63,7 @@ pub struct Snapshot<'a> {
#[allow(dead_code)] #[allow(dead_code)]
pub struct DBIterator<'a> { pub struct DBIterator<'a> {
db: &'a DB, db: &'a DB,
readopts: ReadOptions,
inner: rocksdb_ffi::DBIterator, inner: rocksdb_ffi::DBIterator,
} }
...@@ -78,13 +80,14 @@ impl<'a> From<&'a [u8]> for SeekKey<'a> { ...@@ -78,13 +80,14 @@ impl<'a> From<&'a [u8]> for SeekKey<'a> {
} }
impl<'a> DBIterator<'a> { impl<'a> DBIterator<'a> {
pub fn new(db: &'a DB, readopts: &ReadOptions) -> DBIterator<'a> { pub fn new(db: &'a DB, readopts: ReadOptions) -> DBIterator<'a> {
unsafe { unsafe {
let iterator = rocksdb_ffi::rocksdb_create_iterator(db.inner, let iterator = rocksdb_ffi::rocksdb_create_iterator(db.inner,
readopts.inner); readopts.inner);
DBIterator { DBIterator {
db: db, db: db,
readopts: readopts,
inner: iterator, inner: iterator,
} }
} }
...@@ -159,7 +162,7 @@ impl<'a> DBIterator<'a> { ...@@ -159,7 +162,7 @@ impl<'a> DBIterator<'a> {
pub fn new_cf(db: &'a DB, pub fn new_cf(db: &'a DB,
cf_handle: DBCFHandle, cf_handle: DBCFHandle,
readopts: &ReadOptions) readopts: ReadOptions)
-> DBIterator<'a> { -> DBIterator<'a> {
unsafe { unsafe {
let iterator = let iterator =
...@@ -168,6 +171,7 @@ impl<'a> DBIterator<'a> { ...@@ -168,6 +171,7 @@ impl<'a> DBIterator<'a> {
cf_handle); cf_handle);
DBIterator { DBIterator {
db: db, db: db,
readopts: readopts,
inner: iterator, inner: iterator,
} }
} }
...@@ -215,7 +219,7 @@ impl<'a> Snapshot<'a> { ...@@ -215,7 +219,7 @@ impl<'a> Snapshot<'a> {
unsafe { unsafe {
opt.set_snapshot(&self.snap); opt.set_snapshot(&self.snap);
} }
DBIterator::new(self.db, &opt) DBIterator::new(self.db, opt)
} }
pub fn get(&self, key: &[u8]) -> Result<Option<DBVector>, String> { pub fn get(&self, key: &[u8]) -> Result<Option<DBVector>, String> {
...@@ -590,16 +594,16 @@ impl DB { ...@@ -590,16 +594,16 @@ impl DB {
pub fn iter(&self) -> DBIterator { pub fn iter(&self) -> DBIterator {
let opts = ReadOptions::new(); let opts = ReadOptions::new();
self.iter_opt(&opts) self.iter_opt(opts)
} }
pub fn iter_opt(&self, opt: &ReadOptions) -> DBIterator { pub fn iter_opt(&self, opt: ReadOptions) -> DBIterator {
DBIterator::new(&self, opt) DBIterator::new(&self, opt)
} }
pub fn iter_cf(&self, cf_handle: DBCFHandle) -> DBIterator { pub fn iter_cf(&self, cf_handle: DBCFHandle) -> DBIterator {
let opts = ReadOptions::new(); let opts = ReadOptions::new();
DBIterator::new_cf(&self, cf_handle, &opts) DBIterator::new_cf(&self, cf_handle, opts)
} }
pub fn snapshot(&self) -> Snapshot { pub fn snapshot(&self) -> Snapshot {
...@@ -1100,7 +1104,7 @@ impl Drop for ReadOptions { ...@@ -1100,7 +1104,7 @@ impl Drop for ReadOptions {
impl Default for ReadOptions { impl Default for ReadOptions {
fn default() -> ReadOptions { fn default() -> ReadOptions {
unsafe { unsafe {
ReadOptions { inner: rocksdb_ffi::rocksdb_readoptions_create() } ReadOptions { inner: rocksdb_ffi::rocksdb_readoptions_create(), upper_bound: vec![] }
} }
} }
} }
...@@ -1123,6 +1127,15 @@ impl ReadOptions { ...@@ -1123,6 +1127,15 @@ impl ReadOptions {
rocksdb_ffi::rocksdb_readoptions_set_snapshot(self.inner, rocksdb_ffi::rocksdb_readoptions_set_snapshot(self.inner,
snapshot.inner); snapshot.inner);
} }
pub fn set_iterate_upper_bound(&mut self, key: &[u8]) {
self.upper_bound = Vec::from(key);
unsafe {
rocksdb_ffi::rocksdb_readoptions_set_iterate_upper_bound(self.inner,
self.upper_bound.as_ptr(),
self.upper_bound.len() as size_t);
}
}
} }
pub struct DBVector { pub struct DBVector {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment