Merge pull request #151 from urbint/prefix-iterators

Preliminary: Allow creating iterators over prefixes
master
Tyler Neely 7 years ago committed by GitHub
commit 4f1dd006a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 22
      src/db.rs
  2. 32
      tests/test_iterator.rs

@ -885,6 +885,12 @@ impl DB {
DBIterator::new(self, &opts, mode)
}
pub fn prefix_iterator<'a>(&self, prefix: &'a [u8]) -> DBIterator {
let mut opts = ReadOptions::default();
opts.set_prefix_same_as_start(true);
DBIterator::new(self, &opts, IteratorMode::From(prefix, Direction::Forward))
}
pub fn iterator_cf(
&self,
cf_handle: ColumnFamily,
@ -894,6 +900,16 @@ impl DB {
DBIterator::new_cf(self, cf_handle, &opts, mode)
}
pub fn prefix_iterator_cf<'a>(
&self,
cf_handle: ColumnFamily,
prefix: &'a [u8]
) -> Result<DBIterator, Error> {
let mut opts = ReadOptions::default();
opts.set_prefix_same_as_start(true);
DBIterator::new_cf(self, cf_handle, &opts, IteratorMode::From(prefix, Direction::Forward))
}
pub fn raw_iterator(&self) -> DBRawIterator {
let opts = ReadOptions::default();
DBRawIterator::new(self, &opts)
@ -1215,6 +1231,12 @@ impl ReadOptions {
);
}
}
pub fn set_prefix_same_as_start(&mut self, v: bool) {
unsafe {
ffi::rocksdb_readoptions_set_prefix_same_as_start(self.inner, v as c_uchar)
}
}
}
impl Default for ReadOptions {

@ -151,3 +151,35 @@ pub fn test_iterator() {
let opts = Options::default();
assert!(DB::destroy(&opts, path).is_ok());
}
fn key(k: &[u8]) -> Box<[u8]> { k.to_vec().into_boxed_slice() }
#[test]
pub fn test_prefix_iterator() {
let path = "_rust_rocksdb_prefixiteratortest";
{
let a1: Box<[u8]> = key(b"aaa1");
let a2: Box<[u8]> = key(b"aaa2");
let b1: Box<[u8]> = key(b"bbb1");
let b2: Box<[u8]> = key(b"bbb2");
let db = DB::open_default(path).unwrap();
assert!(db.put(&*a1, &*a1).is_ok());
assert!(db.put(&*a2, &*a2).is_ok());
assert!(db.put(&*b1, &*b1).is_ok());
assert!(db.put(&*b2, &*b2).is_ok());
{
let expected = vec![(cba(&a1), cba(&a1)), (cba(&a2), cba(&a2))];
let a_iterator = db.prefix_iterator(b"aaa");
assert_eq!(a_iterator.collect::<Vec<_>>(), expected)
}
{
let expected = vec![(cba(&b1), cba(&b1)), (cba(&b2), cba(&b2))];
let b_iterator = db.prefix_iterator(b"bbb");
assert_eq!(b_iterator.collect::<Vec<_>>(), expected)
}
}
}

Loading…
Cancel
Save