Add lifetimes for `DBIterator` return types (#294)

master
Nicolas Gotchac 5 years ago committed by Oleksandr Anyshchenko
parent 394238ff94
commit 2096f04092
  1. 16
      src/db.rs
  2. 18
      tests/test_iterator.rs

@ -647,7 +647,7 @@ impl<'a> Snapshot<'a> {
} }
} }
pub fn iterator(&self, mode: IteratorMode) -> DBIterator { pub fn iterator(&self, mode: IteratorMode) -> DBIterator<'a> {
let readopts = ReadOptions::default(); let readopts = ReadOptions::default();
self.iterator_opt(mode, readopts) self.iterator_opt(mode, readopts)
} }
@ -661,7 +661,7 @@ impl<'a> Snapshot<'a> {
self.iterator_cf_opt(cf_handle, readopts, mode) self.iterator_cf_opt(cf_handle, readopts, mode)
} }
pub fn iterator_opt(&self, mode: IteratorMode, mut readopts: ReadOptions) -> DBIterator { pub fn iterator_opt(&self, mode: IteratorMode, mut readopts: ReadOptions) -> DBIterator<'a> {
readopts.set_snapshot(self); readopts.set_snapshot(self);
DBIterator::new(self.db, &readopts, mode) DBIterator::new(self.db, &readopts, mode)
} }
@ -1171,12 +1171,16 @@ impl DB {
self.cfs.get(name) self.cfs.get(name)
} }
pub fn iterator(&self, mode: IteratorMode) -> DBIterator { pub fn iterator<'a, 'b: 'a>(&'a self, mode: IteratorMode) -> DBIterator<'b> {
let readopts = ReadOptions::default(); let readopts = ReadOptions::default();
self.iterator_opt(mode, &readopts) self.iterator_opt(mode, &readopts)
} }
pub fn iterator_opt(&self, mode: IteratorMode, readopts: &ReadOptions) -> DBIterator { pub fn iterator_opt<'a, 'b: 'a>(
&'a self,
mode: IteratorMode,
readopts: &ReadOptions,
) -> DBIterator<'b> {
DBIterator::new(self, &readopts, mode) DBIterator::new(self, &readopts, mode)
} }
@ -1194,13 +1198,13 @@ impl DB {
/// Opens an iterator with `set_total_order_seek` enabled. /// Opens an iterator with `set_total_order_seek` enabled.
/// This must be used to iterate across prefixes when `set_memtable_factory` has been called /// This must be used to iterate across prefixes when `set_memtable_factory` has been called
/// with a Hash-based implementation. /// with a Hash-based implementation.
pub fn full_iterator(&self, mode: IteratorMode) -> DBIterator { pub fn full_iterator<'a, 'b: 'a>(&'a self, mode: IteratorMode) -> DBIterator<'b> {
let mut opts = ReadOptions::default(); let mut opts = ReadOptions::default();
opts.set_total_order_seek(true); opts.set_total_order_seek(true);
DBIterator::new(self, &opts, mode) DBIterator::new(self, &opts, mode)
} }
pub fn prefix_iterator<P: AsRef<[u8]>>(&self, prefix: P) -> DBIterator { pub fn prefix_iterator<'a, 'b: 'a, P: AsRef<[u8]>>(&'a self, prefix: P) -> DBIterator<'b> {
let mut opts = ReadOptions::default(); let mut opts = ReadOptions::default();
opts.set_prefix_same_as_start(true); opts.set_prefix_same_as_start(true);
DBIterator::new( DBIterator::new(

@ -299,3 +299,21 @@ fn test_full_iterator() {
let opts = Options::default(); let opts = Options::default();
assert!(DB::destroy(&opts, path).is_ok()); assert!(DB::destroy(&opts, path).is_ok());
} }
fn custom_iter(db: &DB) -> impl Iterator<Item = usize> {
db.iterator(IteratorMode::Start)
.map(|(_, db_value)| db_value.len())
}
#[test]
fn test_custom_iterator() {
let path = DBPath::new("_rust_rocksdb_customiterator_test");
{
let mut opts = Options::default();
opts.create_if_missing(true);
let db = DB::open(&opts, &path).unwrap();
let _data = custom_iter(&db).collect::<Vec<usize>>();
}
let opts = Options::default();
assert!(DB::destroy(&opts, path).is_ok());
}

Loading…
Cancel
Save