|
|
@ -377,22 +377,26 @@ pub type DBIterator<'a> = DBIteratorWithThreadMode<'a, DB>; |
|
|
|
/// {
|
|
|
|
/// {
|
|
|
|
/// let db = DB::open_default(path).unwrap();
|
|
|
|
/// let db = DB::open_default(path).unwrap();
|
|
|
|
/// let mut iter = db.iterator(IteratorMode::Start); // Always iterates forward
|
|
|
|
/// let mut iter = db.iterator(IteratorMode::Start); // Always iterates forward
|
|
|
|
/// for (key, value) in iter {
|
|
|
|
/// for item in iter {
|
|
|
|
|
|
|
|
/// let (key, value) = item.unwrap();
|
|
|
|
/// println!("Saw {:?} {:?}", key, value);
|
|
|
|
/// println!("Saw {:?} {:?}", key, value);
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// iter = db.iterator(IteratorMode::End); // Always iterates backward
|
|
|
|
/// iter = db.iterator(IteratorMode::End); // Always iterates backward
|
|
|
|
/// for (key, value) in iter {
|
|
|
|
/// for item in iter {
|
|
|
|
|
|
|
|
/// let (key, value) = item.unwrap();
|
|
|
|
/// println!("Saw {:?} {:?}", key, value);
|
|
|
|
/// println!("Saw {:?} {:?}", key, value);
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// iter = db.iterator(IteratorMode::From(b"my key", Direction::Forward)); // From a key in Direction::{forward,reverse}
|
|
|
|
/// iter = db.iterator(IteratorMode::From(b"my key", Direction::Forward)); // From a key in Direction::{forward,reverse}
|
|
|
|
/// for (key, value) in iter {
|
|
|
|
/// for item in iter {
|
|
|
|
|
|
|
|
/// let (key, value) = item.unwrap();
|
|
|
|
/// println!("Saw {:?} {:?}", key, value);
|
|
|
|
/// println!("Saw {:?} {:?}", key, value);
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// // You can seek with an existing Iterator instance, too
|
|
|
|
/// // You can seek with an existing Iterator instance, too
|
|
|
|
/// iter = db.iterator(IteratorMode::Start);
|
|
|
|
/// iter = db.iterator(IteratorMode::Start);
|
|
|
|
/// iter.set_mode(IteratorMode::From(b"another key", Direction::Reverse));
|
|
|
|
/// iter.set_mode(IteratorMode::From(b"another key", Direction::Reverse));
|
|
|
|
/// for (key, value) in iter {
|
|
|
|
/// for item in iter {
|
|
|
|
|
|
|
|
/// let (key, value) = item.unwrap();
|
|
|
|
/// println!("Saw {:?} {:?}", key, value);
|
|
|
|
/// println!("Saw {:?} {:?}", key, value);
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
@ -401,9 +405,10 @@ pub type DBIterator<'a> = DBIteratorWithThreadMode<'a, DB>; |
|
|
|
pub struct DBIteratorWithThreadMode<'a, D: DBAccess> { |
|
|
|
pub struct DBIteratorWithThreadMode<'a, D: DBAccess> { |
|
|
|
raw: DBRawIteratorWithThreadMode<'a, D>, |
|
|
|
raw: DBRawIteratorWithThreadMode<'a, D>, |
|
|
|
direction: Direction, |
|
|
|
direction: Direction, |
|
|
|
just_seeked: bool, |
|
|
|
done: bool, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)] |
|
|
|
pub enum Direction { |
|
|
|
pub enum Direction { |
|
|
|
Forward, |
|
|
|
Forward, |
|
|
|
Reverse, |
|
|
|
Reverse, |
|
|
@ -411,6 +416,7 @@ pub enum Direction { |
|
|
|
|
|
|
|
|
|
|
|
pub type KVBytes = (Box<[u8]>, Box<[u8]>); |
|
|
|
pub type KVBytes = (Box<[u8]>, Box<[u8]>); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)] |
|
|
|
pub enum IteratorMode<'a> { |
|
|
|
pub enum IteratorMode<'a> { |
|
|
|
Start, |
|
|
|
Start, |
|
|
|
End, |
|
|
|
End, |
|
|
@ -419,13 +425,7 @@ pub enum IteratorMode<'a> { |
|
|
|
|
|
|
|
|
|
|
|
impl<'a, D: DBAccess> DBIteratorWithThreadMode<'a, D> { |
|
|
|
impl<'a, D: DBAccess> DBIteratorWithThreadMode<'a, D> { |
|
|
|
pub(crate) fn new(db: &D, readopts: ReadOptions, mode: IteratorMode) -> Self { |
|
|
|
pub(crate) fn new(db: &D, readopts: ReadOptions, mode: IteratorMode) -> Self { |
|
|
|
let mut rv = DBIteratorWithThreadMode { |
|
|
|
Self::from_raw(DBRawIteratorWithThreadMode::new(db, readopts), mode) |
|
|
|
raw: DBRawIteratorWithThreadMode::new(db, readopts), |
|
|
|
|
|
|
|
direction: Direction::Forward, // blown away by set_mode()
|
|
|
|
|
|
|
|
just_seeked: false, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
rv.set_mode(mode); |
|
|
|
|
|
|
|
rv |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub(crate) fn new_cf( |
|
|
|
pub(crate) fn new_cf( |
|
|
@ -434,76 +434,67 @@ impl<'a, D: DBAccess> DBIteratorWithThreadMode<'a, D> { |
|
|
|
readopts: ReadOptions, |
|
|
|
readopts: ReadOptions, |
|
|
|
mode: IteratorMode, |
|
|
|
mode: IteratorMode, |
|
|
|
) -> Self { |
|
|
|
) -> Self { |
|
|
|
|
|
|
|
Self::from_raw( |
|
|
|
|
|
|
|
DBRawIteratorWithThreadMode::new_cf(db, cf_handle, readopts), |
|
|
|
|
|
|
|
mode, |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn from_raw(raw: DBRawIteratorWithThreadMode<'a, D>, mode: IteratorMode) -> Self { |
|
|
|
let mut rv = DBIteratorWithThreadMode { |
|
|
|
let mut rv = DBIteratorWithThreadMode { |
|
|
|
raw: DBRawIteratorWithThreadMode::new_cf(db, cf_handle, readopts), |
|
|
|
raw, |
|
|
|
direction: Direction::Forward, // blown away by set_mode()
|
|
|
|
direction: Direction::Forward, // blown away by set_mode()
|
|
|
|
just_seeked: false, |
|
|
|
done: false, |
|
|
|
}; |
|
|
|
}; |
|
|
|
rv.set_mode(mode); |
|
|
|
rv.set_mode(mode); |
|
|
|
rv |
|
|
|
rv |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn set_mode(&mut self, mode: IteratorMode) { |
|
|
|
pub fn set_mode(&mut self, mode: IteratorMode) { |
|
|
|
match mode { |
|
|
|
self.done = false; |
|
|
|
|
|
|
|
self.direction = match mode { |
|
|
|
IteratorMode::Start => { |
|
|
|
IteratorMode::Start => { |
|
|
|
self.raw.seek_to_first(); |
|
|
|
self.raw.seek_to_first(); |
|
|
|
self.direction = Direction::Forward; |
|
|
|
Direction::Forward |
|
|
|
} |
|
|
|
} |
|
|
|
IteratorMode::End => { |
|
|
|
IteratorMode::End => { |
|
|
|
self.raw.seek_to_last(); |
|
|
|
self.raw.seek_to_last(); |
|
|
|
self.direction = Direction::Reverse; |
|
|
|
Direction::Reverse |
|
|
|
} |
|
|
|
} |
|
|
|
IteratorMode::From(key, Direction::Forward) => { |
|
|
|
IteratorMode::From(key, Direction::Forward) => { |
|
|
|
self.raw.seek(key); |
|
|
|
self.raw.seek(key); |
|
|
|
self.direction = Direction::Forward; |
|
|
|
Direction::Forward |
|
|
|
} |
|
|
|
} |
|
|
|
IteratorMode::From(key, Direction::Reverse) => { |
|
|
|
IteratorMode::From(key, Direction::Reverse) => { |
|
|
|
self.raw.seek_for_prev(key); |
|
|
|
self.raw.seek_for_prev(key); |
|
|
|
self.direction = Direction::Reverse; |
|
|
|
Direction::Reverse |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
self.just_seeked = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// See [`valid`](DBRawIteratorWithThreadMode::valid)
|
|
|
|
|
|
|
|
pub fn valid(&self) -> bool { |
|
|
|
|
|
|
|
self.raw.valid() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// See [`status`](DBRawIteratorWithThreadMode::status)
|
|
|
|
|
|
|
|
pub fn status(&self) -> Result<(), Error> { |
|
|
|
|
|
|
|
self.raw.status() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl<'a, D: DBAccess> Iterator for DBIteratorWithThreadMode<'a, D> { |
|
|
|
impl<'a, D: DBAccess> Iterator for DBIteratorWithThreadMode<'a, D> { |
|
|
|
type Item = KVBytes; |
|
|
|
type Item = Result<KVBytes, Error>; |
|
|
|
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<KVBytes> { |
|
|
|
|
|
|
|
if !self.raw.valid() { |
|
|
|
|
|
|
|
return None; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Initial call to next() after seeking should not move the iterator
|
|
|
|
fn next(&mut self) -> Option<Result<KVBytes, Error>> { |
|
|
|
// or the first item will not be returned
|
|
|
|
if self.done { |
|
|
|
if self.just_seeked { |
|
|
|
None |
|
|
|
self.just_seeked = false; |
|
|
|
} else if let Some((key, value)) = self.raw.item() { |
|
|
|
} else { |
|
|
|
let item = (Box::from(key), Box::from(value)); |
|
|
|
match self.direction { |
|
|
|
match self.direction { |
|
|
|
Direction::Forward => self.raw.next(), |
|
|
|
Direction::Forward => self.raw.next(), |
|
|
|
Direction::Reverse => self.raw.prev(), |
|
|
|
Direction::Reverse => self.raw.prev(), |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
Some(Ok(item)) |
|
|
|
|
|
|
|
|
|
|
|
if let Some((key, value)) = self.raw.item() { |
|
|
|
|
|
|
|
Some((Box::from(key), Box::from(value))) |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
None |
|
|
|
self.done = true; |
|
|
|
|
|
|
|
self.raw.status().err().map(Result::Err) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a, D: DBAccess> std::iter::FusedIterator for DBIteratorWithThreadMode<'a, D> {} |
|
|
|
|
|
|
|
|
|
|
|
impl<'a, D: DBAccess> Into<DBRawIteratorWithThreadMode<'a, D>> for DBIteratorWithThreadMode<'a, D> { |
|
|
|
impl<'a, D: DBAccess> Into<DBRawIteratorWithThreadMode<'a, D>> for DBIteratorWithThreadMode<'a, D> { |
|
|
|
fn into(self) -> DBRawIteratorWithThreadMode<'a, D> { |
|
|
|
fn into(self) -> DBRawIteratorWithThreadMode<'a, D> { |
|
|
|
self.raw |
|
|
|
self.raw |
|
|
@ -548,9 +539,9 @@ impl DBWALIterator { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl Iterator for DBWALIterator { |
|
|
|
impl Iterator for DBWALIterator { |
|
|
|
type Item = (u64, WriteBatch); |
|
|
|
type Item = Result<(u64, WriteBatch), Error>; |
|
|
|
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<(u64, WriteBatch)> { |
|
|
|
fn next(&mut self) -> Option<Self::Item> { |
|
|
|
if !self.valid() { |
|
|
|
if !self.valid() { |
|
|
|
return None; |
|
|
|
return None; |
|
|
|
} |
|
|
|
} |
|
|
@ -562,9 +553,9 @@ impl Iterator for DBWALIterator { |
|
|
|
if self.valid() { |
|
|
|
if self.valid() { |
|
|
|
let mut seq: u64 = 0; |
|
|
|
let mut seq: u64 = 0; |
|
|
|
let inner = unsafe { ffi::rocksdb_wal_iter_get_batch(self.inner, &mut seq) }; |
|
|
|
let inner = unsafe { ffi::rocksdb_wal_iter_get_batch(self.inner, &mut seq) }; |
|
|
|
Some((seq, WriteBatch { inner })) |
|
|
|
Some(Ok((seq, WriteBatch { inner }))) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
None |
|
|
|
self.status().err().map(Result::Err) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|