From 1b9484284c018e37d86a1b933cf8e8b04d0e79b6 Mon Sep 17 00:00:00 2001 From: disksing Date: Tue, 19 Apr 2016 16:23:02 +0800 Subject: [PATCH] export `iterator.valid()` --- src/rocksdb.rs | 4 ++++ test/test_iterator.rs | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/rocksdb.rs b/src/rocksdb.rs index c455188..70167a3 100644 --- a/src/rocksdb.rs +++ b/src/rocksdb.rs @@ -153,6 +153,10 @@ impl DBIterator { } } + pub fn valid(&self) -> bool { + unsafe { rocksdb_ffi::rocksdb_iter_valid(self.inner) } + } + fn new_cf(db: &DB, cf_handle: DBCFHandle, readopts: &ReadOptions, diff --git a/test/test_iterator.rs b/test/test_iterator.rs index 1efe809..4617d29 100644 --- a/test/test_iterator.rs +++ b/test/test_iterator.rs @@ -112,6 +112,31 @@ pub fn test_iterator() { let expected = vec![(cba(&k2), cba(&v2)), (cba(&k1), cba(&v1))]; assert_eq!(iterator1.collect::>(), expected); } + { + let iterator1 = db.iterator(IteratorMode::From(b"k0", Direction::Forward)); + assert!(iterator1.valid()); + let iterator2 = db.iterator(IteratorMode::From(b"k1", Direction::Forward)); + assert!(iterator2.valid()); + let iterator3 = db.iterator(IteratorMode::From(b"k11", Direction::Forward)); + assert!(iterator3.valid()); + let iterator4 = db.iterator(IteratorMode::From(b"k5", Direction::Forward)); + assert!(!iterator4.valid()); + let iterator5 = db.iterator(IteratorMode::From(b"k0", Direction::Reverse)); + assert!(iterator5.valid()); + let iterator6 = db.iterator(IteratorMode::From(b"k1", Direction::Reverse)); + assert!(iterator6.valid()); + let iterator7 = db.iterator(IteratorMode::From(b"k11", Direction::Reverse)); + assert!(iterator7.valid()); + let iterator8 = db.iterator(IteratorMode::From(b"k5", Direction::Reverse)); + assert!(!iterator8.valid()); + } + { + let mut iterator1 = db.iterator(IteratorMode::From(b"k4", Direction::Forward)); + iterator1.next(); + assert!(iterator1.valid()); + iterator1.next(); + assert!(!iterator1.valid()); + } } let opts = Options::new(); assert!(DB::destroy(&opts, path).is_ok());