Allow creating iterators over prefixes

Allow creating both db and column family iterators that are specific to
a key prefix, by setting the `prefix_same_as_start` read option when
creating the iterator.

Currently this only supports `Direction::Forward`, but it'd likely be
trivial to support `Backward` as well, by incrementing the given prefix
by one and seeking to the key before that key.
master
Griffin Smith 7 years ago
parent 4d48b70bc2
commit 15ad3666f5
  1. 22
      src/db.rs
  2. 32
      tests/test_iterator.rs

@ -872,6 +872,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,
@ -881,6 +887,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)
@ -1202,6 +1218,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