Mercurial > lbo > hg > leveldb-rs
changeset 75:94ea2222178f
Add basic LdbIterator implementation for testing
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sun, 17 Jul 2016 16:37:14 +0200 |
parents | f05cafb37230 |
children | 015895a4bb84 |
files | src/lib.rs src/test_util.rs |
diffstat | 2 files changed, 72 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/src/lib.rs Sun Jul 17 16:36:56 2016 +0200 +++ b/src/lib.rs Sun Jul 17 16:37:14 2016 +0200 @@ -13,6 +13,7 @@ mod filter_block; mod log; mod memtable; +mod merging_iter; mod options; mod skipmap; mod snapshot; @@ -20,6 +21,8 @@ mod types; mod write_batch; +mod test_util; + pub use types::Comparator; #[cfg(test)]
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test_util.rs Sun Jul 17 16:37:14 2016 +0200 @@ -0,0 +1,69 @@ +use types::LdbIterator; +use types::Comparator; +use types::StandardComparator; + +use std::cmp::Ordering; + +pub struct TestLdbIter<'a> { + v: Vec<(&'a [u8], &'a [u8])>, + ix: usize, + init: bool, +} + +impl<'a> TestLdbIter<'a> { + pub fn new(c: Vec<(&'a [u8], &'a [u8])>) -> TestLdbIter<'a> { + return TestLdbIter { + v: c, + ix: 0, + init: false, + }; + } +} + +impl<'a> Iterator for TestLdbIter<'a> { + type Item = (&'a [u8], &'a [u8]); + + fn next(&mut self) -> Option<Self::Item> { + if self.ix == self.v.len() { + return None; + } else if !self.init { + self.init = true; + Some(self.v[self.ix]) + } else { + self.ix += 1; + Some(self.v[self.ix - 1]) + } + } +} + +impl<'a> LdbIterator for TestLdbIter<'a> { + fn reset(&mut self) { + self.ix = 0; + self.init = false; + } + fn current(&self) -> Option<Self::Item> { + if self.init && self.ix < self.v.len() { + Some(self.v[self.ix]) + } else { + None + } + } + fn valid(&self) -> bool { + self.init + } + fn seek(&mut self, k: &[u8]) { + self.ix = 0; + while self.ix < self.v.len() && + StandardComparator::cmp(self.v[self.ix].0, k) == Ordering::Less { + self.ix += 1; + } + } + fn prev(&mut self) -> Option<Self::Item> { + if !self.init || self.ix == 0 { + None + } else { + self.ix -= 1; + Some(self.v[self.ix]) + } + } +}