|
|
@ -16,9 +16,23 @@ mod util; |
|
|
|
|
|
|
|
|
|
|
|
use pretty_assertions::assert_eq; |
|
|
|
use pretty_assertions::assert_eq; |
|
|
|
|
|
|
|
|
|
|
|
use rocksdb::DB; |
|
|
|
use rocksdb::{DBAccess, DBRawIteratorWithThreadMode, DB}; |
|
|
|
use util::DBPath; |
|
|
|
use util::DBPath; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn assert_item<D: DBAccess>(iter: &DBRawIteratorWithThreadMode<'_, D>, key: &[u8], value: &[u8]) { |
|
|
|
|
|
|
|
assert!(iter.valid()); |
|
|
|
|
|
|
|
assert_eq!(iter.key(), Some(key)); |
|
|
|
|
|
|
|
assert_eq!(iter.value(), Some(value)); |
|
|
|
|
|
|
|
assert_eq!(iter.item(), Some((key, value))); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn assert_no_item<D: DBAccess>(iter: &DBRawIteratorWithThreadMode<'_, D>) { |
|
|
|
|
|
|
|
assert!(!iter.valid()); |
|
|
|
|
|
|
|
assert_eq!(iter.key(), None); |
|
|
|
|
|
|
|
assert_eq!(iter.value(), None); |
|
|
|
|
|
|
|
assert_eq!(iter.item(), None); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
pub fn test_forwards_iteration() { |
|
|
|
pub fn test_forwards_iteration() { |
|
|
|
let n = DBPath::new("forwards_iteration"); |
|
|
|
let n = DBPath::new("forwards_iteration"); |
|
|
@ -31,24 +45,16 @@ pub fn test_forwards_iteration() { |
|
|
|
|
|
|
|
|
|
|
|
let mut iter = db.raw_iterator(); |
|
|
|
let mut iter = db.raw_iterator(); |
|
|
|
iter.seek_to_first(); |
|
|
|
iter.seek_to_first(); |
|
|
|
|
|
|
|
assert_item(&iter, b"k1", b"v1"); |
|
|
|
assert!(iter.valid()); |
|
|
|
|
|
|
|
assert_eq!(iter.key(), Some(b"k1".as_ref())); |
|
|
|
|
|
|
|
assert_eq!(iter.value(), Some(b"v1".as_ref())); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
iter.next(); |
|
|
|
iter.next(); |
|
|
|
|
|
|
|
assert_item(&iter, b"k2", b"v2"); |
|
|
|
assert!(iter.valid()); |
|
|
|
|
|
|
|
assert_eq!(iter.key(), Some(b"k2".as_ref())); |
|
|
|
|
|
|
|
assert_eq!(iter.value(), Some(b"v2".as_ref())); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
iter.next(); // k3
|
|
|
|
iter.next(); // k3
|
|
|
|
iter.next(); // k4
|
|
|
|
iter.next(); // k4
|
|
|
|
iter.next(); // invalid!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert!(!iter.valid()); |
|
|
|
iter.next(); // invalid!
|
|
|
|
assert_eq!(iter.key(), None); |
|
|
|
assert_no_item(&iter); |
|
|
|
assert_eq!(iter.value(), None); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -64,24 +70,16 @@ pub fn test_seek_last() { |
|
|
|
|
|
|
|
|
|
|
|
let mut iter = db.raw_iterator(); |
|
|
|
let mut iter = db.raw_iterator(); |
|
|
|
iter.seek_to_last(); |
|
|
|
iter.seek_to_last(); |
|
|
|
|
|
|
|
assert_item(&iter, b"k4", b"v4"); |
|
|
|
assert!(iter.valid()); |
|
|
|
|
|
|
|
assert_eq!(iter.key(), Some(b"k4".as_ref())); |
|
|
|
|
|
|
|
assert_eq!(iter.value(), Some(b"v4".as_ref())); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
iter.prev(); |
|
|
|
iter.prev(); |
|
|
|
|
|
|
|
assert_item(&iter, b"k3", b"v3"); |
|
|
|
assert!(iter.valid()); |
|
|
|
|
|
|
|
assert_eq!(iter.key(), Some(b"k3".as_ref())); |
|
|
|
|
|
|
|
assert_eq!(iter.value(), Some(b"v3".as_ref())); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
iter.prev(); // k2
|
|
|
|
iter.prev(); // k2
|
|
|
|
iter.prev(); // k1
|
|
|
|
iter.prev(); // k1
|
|
|
|
iter.prev(); // invalid!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert!(!iter.valid()); |
|
|
|
iter.prev(); // invalid!
|
|
|
|
assert_eq!(iter.key(), None); |
|
|
|
assert_no_item(&iter); |
|
|
|
assert_eq!(iter.value(), None); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -97,16 +95,11 @@ pub fn test_seek() { |
|
|
|
let mut iter = db.raw_iterator(); |
|
|
|
let mut iter = db.raw_iterator(); |
|
|
|
iter.seek(b"k2"); |
|
|
|
iter.seek(b"k2"); |
|
|
|
|
|
|
|
|
|
|
|
assert!(iter.valid()); |
|
|
|
assert_item(&iter, b"k2", b"v2"); |
|
|
|
assert_eq!(iter.key(), Some(b"k2".as_ref())); |
|
|
|
|
|
|
|
assert_eq!(iter.value(), Some(b"v2".as_ref())); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check it gets the next key when the key doesn't exist
|
|
|
|
// Check it gets the next key when the key doesn't exist
|
|
|
|
iter.seek(b"k3"); |
|
|
|
iter.seek(b"k3"); |
|
|
|
|
|
|
|
assert_item(&iter, b"k4", b"v4"); |
|
|
|
assert!(iter.valid()); |
|
|
|
|
|
|
|
assert_eq!(iter.key(), Some(b"k4".as_ref())); |
|
|
|
|
|
|
|
assert_eq!(iter.value(), Some(b"v4".as_ref())); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -121,10 +114,7 @@ pub fn test_seek_to_nonexistant() { |
|
|
|
|
|
|
|
|
|
|
|
let mut iter = db.raw_iterator(); |
|
|
|
let mut iter = db.raw_iterator(); |
|
|
|
iter.seek(b"k2"); |
|
|
|
iter.seek(b"k2"); |
|
|
|
|
|
|
|
assert_item(&iter, b"k3", b"v3"); |
|
|
|
assert!(iter.valid()); |
|
|
|
|
|
|
|
assert_eq!(iter.key(), Some(b"k3".as_ref())); |
|
|
|
|
|
|
|
assert_eq!(iter.value(), Some(b"v3".as_ref())); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -139,16 +129,10 @@ pub fn test_seek_for_prev() { |
|
|
|
|
|
|
|
|
|
|
|
let mut iter = db.raw_iterator(); |
|
|
|
let mut iter = db.raw_iterator(); |
|
|
|
iter.seek(b"k2"); |
|
|
|
iter.seek(b"k2"); |
|
|
|
|
|
|
|
assert_item(&iter, b"k2", b"v2"); |
|
|
|
assert!(iter.valid()); |
|
|
|
|
|
|
|
assert_eq!(iter.key(), Some(b"k2".as_ref())); |
|
|
|
|
|
|
|
assert_eq!(iter.value(), Some(b"v2".as_ref())); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check it gets the previous key when the key doesn't exist
|
|
|
|
// Check it gets the previous key when the key doesn't exist
|
|
|
|
iter.seek_for_prev(b"k3"); |
|
|
|
iter.seek_for_prev(b"k3"); |
|
|
|
|
|
|
|
assert_item(&iter, b"k2", b"v2"); |
|
|
|
assert!(iter.valid()); |
|
|
|
|
|
|
|
assert_eq!(iter.key(), Some(b"k2".as_ref())); |
|
|
|
|
|
|
|
assert_eq!(iter.value(), Some(b"v2".as_ref())); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|