|
|
@ -167,9 +167,8 @@ pub struct DBRawIterator { |
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
/// ```
|
|
|
|
pub struct DBIterator { |
|
|
|
pub struct DBIterator { |
|
|
|
inner: *mut ffi::rocksdb_iterator_t, |
|
|
|
raw: DBRawIterator, |
|
|
|
direction: Direction, |
|
|
|
direction: Direction, |
|
|
|
just_seeked: bool, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub enum Direction { |
|
|
|
pub enum Direction { |
|
|
@ -183,28 +182,14 @@ impl Iterator for DBIterator { |
|
|
|
type Item = KVBytes; |
|
|
|
type Item = KVBytes; |
|
|
|
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<KVBytes> { |
|
|
|
fn next(&mut self) -> Option<KVBytes> { |
|
|
|
let native_iter = self.inner; |
|
|
|
let valid = match self.direction { |
|
|
|
if !self.just_seeked { |
|
|
|
Direction::Forward => self.raw.next(), |
|
|
|
match self.direction { |
|
|
|
Direction::Reverse => self.raw.prev(), |
|
|
|
Direction::Forward => unsafe { ffi::rocksdb_iter_next(native_iter) }, |
|
|
|
}; |
|
|
|
Direction::Reverse => unsafe { ffi::rocksdb_iter_prev(native_iter) }, |
|
|
|
|
|
|
|
} |
|
|
|
if valid { |
|
|
|
} else { |
|
|
|
// .key() and .value() only ever return None if valid == false, which we've just cheked
|
|
|
|
self.just_seeked = false; |
|
|
|
Some((self.raw.key().unwrap().into_boxed_slice(), self.raw.value().unwrap().into_boxed_slice())) |
|
|
|
} |
|
|
|
|
|
|
|
if unsafe { ffi::rocksdb_iter_valid(native_iter) != 0 } { |
|
|
|
|
|
|
|
let mut key_len: size_t = 0; |
|
|
|
|
|
|
|
let key_len_ptr: *mut size_t = &mut key_len; |
|
|
|
|
|
|
|
let mut val_len: size_t = 0; |
|
|
|
|
|
|
|
let val_len_ptr: *mut size_t = &mut val_len; |
|
|
|
|
|
|
|
let key_ptr = |
|
|
|
|
|
|
|
unsafe { ffi::rocksdb_iter_key(native_iter, key_len_ptr) as *const c_uchar }; |
|
|
|
|
|
|
|
let key = unsafe { slice::from_raw_parts(key_ptr, key_len as usize) }; |
|
|
|
|
|
|
|
let val_ptr = |
|
|
|
|
|
|
|
unsafe { ffi::rocksdb_iter_value(native_iter, val_len_ptr) as *const c_uchar }; |
|
|
|
|
|
|
|
let val = unsafe { slice::from_raw_parts(val_ptr, val_len as usize) }; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Some((key.to_vec().into_boxed_slice(), val.to_vec().into_boxed_slice())) |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
None |
|
|
|
None |
|
|
|
} |
|
|
|
} |
|
|
@ -485,43 +470,34 @@ impl Drop for DBRawIterator { |
|
|
|
|
|
|
|
|
|
|
|
impl DBIterator { |
|
|
|
impl DBIterator { |
|
|
|
fn new(db: &DB, readopts: &ReadOptions, mode: IteratorMode) -> DBIterator { |
|
|
|
fn new(db: &DB, readopts: &ReadOptions, mode: IteratorMode) -> DBIterator { |
|
|
|
unsafe { |
|
|
|
let mut rv = DBIterator { |
|
|
|
let iterator = ffi::rocksdb_create_iterator(db.inner, readopts.inner); |
|
|
|
raw: DBRawIterator::new(db, readopts), |
|
|
|
|
|
|
|
direction: Direction::Forward, // blown away by set_mode()
|
|
|
|
let mut rv = DBIterator { |
|
|
|
}; |
|
|
|
inner: iterator, |
|
|
|
rv.set_mode(mode); |
|
|
|
direction: Direction::Forward, // blown away by set_mode()
|
|
|
|
rv |
|
|
|
just_seeked: false, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
rv.set_mode(mode); |
|
|
|
|
|
|
|
rv |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn set_mode(&mut self, mode: IteratorMode) { |
|
|
|
pub fn set_mode(&mut self, mode: IteratorMode) { |
|
|
|
unsafe { |
|
|
|
match mode { |
|
|
|
match mode { |
|
|
|
IteratorMode::Start => { |
|
|
|
IteratorMode::Start => { |
|
|
|
self.raw.seek_to_first(); |
|
|
|
ffi::rocksdb_iter_seek_to_first(self.inner); |
|
|
|
self.direction = Direction::Forward; |
|
|
|
self.direction = Direction::Forward; |
|
|
|
} |
|
|
|
} |
|
|
|
IteratorMode::End => { |
|
|
|
IteratorMode::End => { |
|
|
|
self.raw.seek_to_last(); |
|
|
|
ffi::rocksdb_iter_seek_to_last(self.inner); |
|
|
|
self.direction = Direction::Reverse; |
|
|
|
self.direction = Direction::Reverse; |
|
|
|
} |
|
|
|
} |
|
|
|
IteratorMode::From(key, dir) => { |
|
|
|
IteratorMode::From(key, dir) => { |
|
|
|
// TODO: Should use seek_for_prev when reversing
|
|
|
|
ffi::rocksdb_iter_seek(self.inner, |
|
|
|
self.raw.seek(key); |
|
|
|
key.as_ptr() as *const c_char, |
|
|
|
self.direction = dir; |
|
|
|
key.len() as size_t); |
|
|
|
} |
|
|
|
self.direction = dir; |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
self.just_seeked = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn valid(&self) -> bool { |
|
|
|
pub fn valid(&self) -> bool { |
|
|
|
unsafe { ffi::rocksdb_iter_valid(self.inner) != 0 } |
|
|
|
self.raw.valid() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn new_cf(db: &DB, |
|
|
|
fn new_cf(db: &DB, |
|
|
@ -529,25 +505,12 @@ impl DBIterator { |
|
|
|
readopts: &ReadOptions, |
|
|
|
readopts: &ReadOptions, |
|
|
|
mode: IteratorMode) |
|
|
|
mode: IteratorMode) |
|
|
|
-> Result<DBIterator, Error> { |
|
|
|
-> Result<DBIterator, Error> { |
|
|
|
unsafe { |
|
|
|
let mut rv = DBIterator { |
|
|
|
let iterator = ffi::rocksdb_create_iterator_cf(db.inner, readopts.inner, cf_handle.inner); |
|
|
|
raw: try!(DBRawIterator::new_cf(db, cf_handle, readopts)), |
|
|
|
|
|
|
|
direction: Direction::Forward, // blown away by set_mode()
|
|
|
|
let mut rv = DBIterator { |
|
|
|
}; |
|
|
|
inner: iterator, |
|
|
|
rv.set_mode(mode); |
|
|
|
direction: Direction::Forward, // blown away by set_mode()
|
|
|
|
Ok(rv) |
|
|
|
just_seeked: false, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
rv.set_mode(mode); |
|
|
|
|
|
|
|
Ok(rv) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Drop for DBIterator { |
|
|
|
|
|
|
|
fn drop(&mut self) { |
|
|
|
|
|
|
|
unsafe { |
|
|
|
|
|
|
|
ffi::rocksdb_iter_destroy(self.inner); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|