|
|
|
@ -1,5 +1,4 @@ |
|
|
|
|
use rocksdb::{Options, DB, Writable, Direction}; |
|
|
|
|
use std; |
|
|
|
|
|
|
|
|
|
fn cba(input: &Box<[u8]>) -> Box<[u8]> { |
|
|
|
|
input.iter().cloned().collect::<Vec<_>>().into_boxed_slice() |
|
|
|
@ -17,7 +16,7 @@ pub fn test_iterator() { |
|
|
|
|
let v2:Box<[u8]> = b"v2222".to_vec().into_boxed_slice(); |
|
|
|
|
let v3:Box<[u8]> = b"v3333".to_vec().into_boxed_slice(); |
|
|
|
|
let v4:Box<[u8]> = b"v4444".to_vec().into_boxed_slice(); |
|
|
|
|
let mut db = DB::open_default(path).unwrap(); |
|
|
|
|
let db = DB::open_default(path).unwrap(); |
|
|
|
|
let p = db.put(&*k1, &*v1); |
|
|
|
|
assert!(p.is_ok()); |
|
|
|
|
let p = db.put(&*k2, &*v2); |
|
|
|
@ -27,83 +26,82 @@ pub fn test_iterator() { |
|
|
|
|
let mut view1 = db.iterator(); |
|
|
|
|
let expected = vec![(cba(&k1), cba(&v1)), (cba(&k2), cba(&v2)), (cba(&k3), cba(&v3))]; |
|
|
|
|
{ |
|
|
|
|
let mut iterator1 = view1.from_start(); |
|
|
|
|
let iterator1 = view1.from_start(); |
|
|
|
|
assert_eq!(iterator1.collect::<Vec<_>>(), expected); |
|
|
|
|
} |
|
|
|
|
// Test that it's reusable a few times
|
|
|
|
|
{ |
|
|
|
|
let mut iterator1 = view1.from_start(); |
|
|
|
|
let iterator1 = view1.from_start(); |
|
|
|
|
assert_eq!(iterator1.collect::<Vec<_>>(), expected); |
|
|
|
|
} |
|
|
|
|
{ |
|
|
|
|
let mut iterator1 = view1.from_start(); |
|
|
|
|
let iterator1 = view1.from_start(); |
|
|
|
|
assert_eq!(iterator1.collect::<Vec<_>>(), expected); |
|
|
|
|
} |
|
|
|
|
{ |
|
|
|
|
let mut iterator1 = view1.from_start(); |
|
|
|
|
let iterator1 = view1.from_start(); |
|
|
|
|
assert_eq!(iterator1.collect::<Vec<_>>(), expected); |
|
|
|
|
} |
|
|
|
|
// Test it in reverse a few times
|
|
|
|
|
{ |
|
|
|
|
let mut iterator1 = view1.from_end(); |
|
|
|
|
let iterator1 = view1.from_end(); |
|
|
|
|
let mut tmp_vec = iterator1.collect::<Vec<_>>(); |
|
|
|
|
tmp_vec.reverse(); |
|
|
|
|
assert_eq!(tmp_vec, expected); |
|
|
|
|
} |
|
|
|
|
{ |
|
|
|
|
let mut iterator1 = view1.from_end(); |
|
|
|
|
let iterator1 = view1.from_end(); |
|
|
|
|
let mut tmp_vec = iterator1.collect::<Vec<_>>(); |
|
|
|
|
tmp_vec.reverse(); |
|
|
|
|
assert_eq!(tmp_vec, expected); |
|
|
|
|
} |
|
|
|
|
{ |
|
|
|
|
let mut iterator1 = view1.from_end(); |
|
|
|
|
let iterator1 = view1.from_end(); |
|
|
|
|
let mut tmp_vec = iterator1.collect::<Vec<_>>(); |
|
|
|
|
tmp_vec.reverse(); |
|
|
|
|
assert_eq!(tmp_vec, expected); |
|
|
|
|
} |
|
|
|
|
{ |
|
|
|
|
let mut iterator1 = view1.from_end(); |
|
|
|
|
let iterator1 = view1.from_end(); |
|
|
|
|
let mut tmp_vec = iterator1.collect::<Vec<_>>(); |
|
|
|
|
tmp_vec.reverse(); |
|
|
|
|
assert_eq!(tmp_vec, expected); |
|
|
|
|
} |
|
|
|
|
{ |
|
|
|
|
let mut iterator1 = view1.from_end(); |
|
|
|
|
let iterator1 = view1.from_end(); |
|
|
|
|
let mut tmp_vec = iterator1.collect::<Vec<_>>(); |
|
|
|
|
tmp_vec.reverse(); |
|
|
|
|
assert_eq!(tmp_vec, expected); |
|
|
|
|
} |
|
|
|
|
// Try it forward again
|
|
|
|
|
{ |
|
|
|
|
let mut iterator1 = view1.from_start(); |
|
|
|
|
let iterator1 = view1.from_start(); |
|
|
|
|
assert_eq!(iterator1.collect::<Vec<_>>(), expected); |
|
|
|
|
} |
|
|
|
|
{ |
|
|
|
|
let mut iterator1 = view1.from_start(); |
|
|
|
|
let iterator1 = view1.from_start(); |
|
|
|
|
assert_eq!(iterator1.collect::<Vec<_>>(), expected); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let mut view2 = db.iterator(); |
|
|
|
|
let p = db.put(&*k4, &*v4); |
|
|
|
|
assert!(p.is_ok()); |
|
|
|
|
let mut view3 = db.iterator(); |
|
|
|
|
let expected2 = vec![(cba(&k1), cba(&v1)), (cba(&k2), cba(&v2)), (cba(&k3), cba(&v3)), (cba(&k4), cba(&v4))]; |
|
|
|
|
{ |
|
|
|
|
let mut iterator1 = view1.from_start(); |
|
|
|
|
let iterator1 = view1.from_start(); |
|
|
|
|
assert_eq!(iterator1.collect::<Vec<_>>(), expected); |
|
|
|
|
} |
|
|
|
|
{ |
|
|
|
|
let mut iterator1 = view3.from_start(); |
|
|
|
|
let iterator1 = view3.from_start(); |
|
|
|
|
assert_eq!(iterator1.collect::<Vec<_>>(), expected2); |
|
|
|
|
} |
|
|
|
|
{ |
|
|
|
|
let mut iterator1 = view3.from(b"k2", Direction::forward); |
|
|
|
|
let iterator1 = view3.from(b"k2", Direction::forward); |
|
|
|
|
let expected = vec![(cba(&k2), cba(&v2)), (cba(&k3), cba(&v3)), (cba(&k4), cba(&v4))]; |
|
|
|
|
assert_eq!(iterator1.collect::<Vec<_>>(), expected); |
|
|
|
|
} |
|
|
|
|
{ |
|
|
|
|
let mut iterator1 = view3.from(b"k2", Direction::reverse); |
|
|
|
|
let iterator1 = view3.from(b"k2", Direction::reverse); |
|
|
|
|
let expected = vec![(cba(&k2), cba(&v2)), (cba(&k1), cba(&v1))]; |
|
|
|
|
assert_eq!(iterator1.collect::<Vec<_>>(), expected); |
|
|
|
|
} |
|
|
|
|