Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions kvdb-rocksdb/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use crate::DBAndColumns;
use owning_ref::{OwningHandle, StableAddress};
use parking_lot::RwLockReadGuard;
use rocksdb::{DBIterator, IteratorMode};
use rocksdb::{DBIterator, Direction, IteratorMode, ReadOptions};
use std::ops::{Deref, DerefMut};

/// A tuple holding key and value data, used as the iterator item type.
Expand Down Expand Up @@ -76,22 +76,27 @@ pub trait IterationHandler {

/// Create an `Iterator` over the default DB column or over a `ColumnFamily` if a column number
/// is passed.
fn iter(&self, col: Option<u32>) -> Self::Iterator;
fn iter(&self, col: Option<u32>, read_opts: &ReadOptions) -> Self::Iterator;
/// Create an `Iterator` over the default DB column or over a `ColumnFamily` if a column number
/// is passed. The iterator starts from the first key having the provided `prefix`.
fn iter_from_prefix(&self, col: Option<u32>, prefix: &[u8]) -> Self::Iterator;
fn iter_from_prefix(&self, col: Option<u32>, prefix: &[u8], read_opts: &ReadOptions) -> Self::Iterator;
}

impl<'a, T> ReadGuardedIterator<'a, <&'a T as IterationHandler>::Iterator, T>
where
&'a T: IterationHandler,
{
pub fn new(read_lock: RwLockReadGuard<'a, Option<T>>, col: Option<u32>) -> Self {
Self { inner: Self::new_inner(read_lock, |db| db.iter(col)) }
pub fn new(read_lock: RwLockReadGuard<'a, Option<T>>, col: Option<u32>, read_opts: &ReadOptions) -> Self {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docs

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<insert illuminating example here>. – needs some editing... ;)

…and there are other methods that now take ReadOptions that needs some docs?

Self { inner: Self::new_inner(read_lock, |db| db.iter(col, read_opts)) }
}

pub fn new_from_prefix(read_lock: RwLockReadGuard<'a, Option<T>>, col: Option<u32>, prefix: &[u8]) -> Self {
Self { inner: Self::new_inner(read_lock, |db| db.iter_from_prefix(col, prefix)) }
pub fn new_from_prefix(
Comment thread
ordian marked this conversation as resolved.
read_lock: RwLockReadGuard<'a, Option<T>>,
col: Option<u32>,
prefix: &[u8],
read_opts: &ReadOptions,
) -> Self {
Self { inner: Self::new_inner(read_lock, |db| db.iter_from_prefix(col, prefix, read_opts)) }
}

fn new_inner(
Expand All @@ -108,21 +113,25 @@ where
impl<'a> IterationHandler for &'a DBAndColumns {
type Iterator = DBIterator<'a>;

fn iter(&self, col: Option<u32>) -> Self::Iterator {
fn iter(&self, col: Option<u32>, read_opts: &ReadOptions) -> Self::Iterator {
col.map_or_else(
|| self.db.iterator(IteratorMode::Start),
|| self.db.iterator_opt(IteratorMode::Start, read_opts),
|c| {
self.db
.iterator_cf(self.get_cf(c as usize), IteratorMode::Start)
.iterator_cf_opt(self.get_cf(c as usize), read_opts, IteratorMode::Start)
.expect("iterator params are valid; qed")
},
)
}

fn iter_from_prefix(&self, col: Option<u32>, prefix: &[u8]) -> Self::Iterator {
fn iter_from_prefix(&self, col: Option<u32>, prefix: &[u8], read_opts: &ReadOptions) -> Self::Iterator {
col.map_or_else(
|| self.db.prefix_iterator(prefix),
|c| self.db.prefix_iterator_cf(self.get_cf(c as usize), prefix).expect("iterator params are valid; qed"),
|| self.db.iterator_opt(IteratorMode::From(prefix, Direction::Forward), read_opts),
|c| {
self.db
.iterator_cf_opt(self.get_cf(c as usize), read_opts, IteratorMode::From(prefix, Direction::Forward))
.expect("iterator params are valid; qed")
},
)
}
}
8 changes: 5 additions & 3 deletions kvdb-rocksdb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ impl Database {
overlay_data
};

let guarded = iter::ReadGuardedIterator::new(read_lock, col);
let guarded = iter::ReadGuardedIterator::new(read_lock, col, &self.read_opts);
Some(interleave_ordered(overlay_data, guarded))
} else {
None
Expand All @@ -588,12 +588,14 @@ impl Database {
) -> impl Iterator<Item = iter::KeyValuePair> + 'a {
let read_lock = self.db.read();
let optional = if read_lock.is_some() {
let guarded = iter::ReadGuardedIterator::new_from_prefix(read_lock, col, prefix);
let guarded = iter::ReadGuardedIterator::new_from_prefix(read_lock, col, prefix, &self.read_opts);
Some(interleave_ordered(Vec::new(), guarded))
} else {
None
};
// workaround for https://github.com/facebook/rocksdb/issues/2343
// We're not using "Prefix Seek" mode, so the iterator will return
Comment thread
ordian marked this conversation as resolved.
// keys not starting with the given prefix as well,
// see https://github.com/facebook/rocksdb/wiki/Prefix-Seek-API-Changes
optional.into_iter().flat_map(identity).filter(move |(k, _)| k.starts_with(prefix))
}

Expand Down