fork of https://github.com/rust-rocksdb/rust-rocksdb for nextgraph
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
154 lines
6.1 KiB
154 lines
6.1 KiB
8 years ago
|
// Copyright 2014 Tyler Neely
|
||
|
//
|
||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
// you may not use this file except in compliance with the License.
|
||
|
// You may obtain a copy of the License at
|
||
|
//
|
||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||
|
//
|
||
|
// Unless required by applicable law or agreed to in writing, software
|
||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
// See the License for the specific language governing permissions and
|
||
|
// limitations under the License.
|
||
|
//
|
||
8 years ago
|
extern crate rocksdb;
|
||
8 years ago
|
|
||
8 years ago
|
use rocksdb::{DB, Direction, IteratorMode, Options};
|
||
10 years ago
|
|
||
10 years ago
|
fn cba(input: &Box<[u8]>) -> Box<[u8]> {
|
||
|
input.iter().cloned().collect::<Vec<_>>().into_boxed_slice()
|
||
|
}
|
||
10 years ago
|
|
||
|
#[test]
|
||
|
pub fn test_iterator() {
|
||
|
let path = "_rust_rocksdb_iteratortest";
|
||
|
{
|
||
9 years ago
|
let k1: Box<[u8]> = b"k1".to_vec().into_boxed_slice();
|
||
|
let k2: Box<[u8]> = b"k2".to_vec().into_boxed_slice();
|
||
|
let k3: Box<[u8]> = b"k3".to_vec().into_boxed_slice();
|
||
|
let k4: Box<[u8]> = b"k4".to_vec().into_boxed_slice();
|
||
|
let v1: Box<[u8]> = b"v1111".to_vec().into_boxed_slice();
|
||
|
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();
|
||
9 years ago
|
let db = DB::open_default(path).unwrap();
|
||
10 years ago
|
let p = db.put(&*k1, &*v1);
|
||
|
assert!(p.is_ok());
|
||
|
let p = db.put(&*k2, &*v2);
|
||
|
assert!(p.is_ok());
|
||
|
let p = db.put(&*k3, &*v3);
|
||
|
assert!(p.is_ok());
|
||
8 years ago
|
let expected = vec![(cba(&k1), cba(&v1)), (cba(&k2), cba(&v2)), (cba(&k3), cba(&v3))];
|
||
10 years ago
|
{
|
||
9 years ago
|
let iterator1 = db.iterator(IteratorMode::Start);
|
||
10 years ago
|
assert_eq!(iterator1.collect::<Vec<_>>(), expected);
|
||
|
}
|
||
9 years ago
|
// Test that it's idempotent
|
||
10 years ago
|
{
|
||
9 years ago
|
let iterator1 = db.iterator(IteratorMode::Start);
|
||
10 years ago
|
assert_eq!(iterator1.collect::<Vec<_>>(), expected);
|
||
|
}
|
||
|
{
|
||
9 years ago
|
let iterator1 = db.iterator(IteratorMode::Start);
|
||
10 years ago
|
assert_eq!(iterator1.collect::<Vec<_>>(), expected);
|
||
|
}
|
||
|
{
|
||
9 years ago
|
let iterator1 = db.iterator(IteratorMode::Start);
|
||
10 years ago
|
assert_eq!(iterator1.collect::<Vec<_>>(), expected);
|
||
|
}
|
||
|
// Test it in reverse a few times
|
||
|
{
|
||
9 years ago
|
let iterator1 = db.iterator(IteratorMode::End);
|
||
10 years ago
|
let mut tmp_vec = iterator1.collect::<Vec<_>>();
|
||
|
tmp_vec.reverse();
|
||
|
assert_eq!(tmp_vec, expected);
|
||
|
}
|
||
|
{
|
||
9 years ago
|
let iterator1 = db.iterator(IteratorMode::End);
|
||
10 years ago
|
let mut tmp_vec = iterator1.collect::<Vec<_>>();
|
||
|
tmp_vec.reverse();
|
||
|
assert_eq!(tmp_vec, expected);
|
||
|
}
|
||
|
{
|
||
9 years ago
|
let iterator1 = db.iterator(IteratorMode::End);
|
||
10 years ago
|
let mut tmp_vec = iterator1.collect::<Vec<_>>();
|
||
|
tmp_vec.reverse();
|
||
|
assert_eq!(tmp_vec, expected);
|
||
|
}
|
||
|
{
|
||
9 years ago
|
let iterator1 = db.iterator(IteratorMode::End);
|
||
10 years ago
|
let mut tmp_vec = iterator1.collect::<Vec<_>>();
|
||
|
tmp_vec.reverse();
|
||
|
assert_eq!(tmp_vec, expected);
|
||
|
}
|
||
|
{
|
||
9 years ago
|
let iterator1 = db.iterator(IteratorMode::End);
|
||
10 years ago
|
let mut tmp_vec = iterator1.collect::<Vec<_>>();
|
||
|
tmp_vec.reverse();
|
||
|
assert_eq!(tmp_vec, expected);
|
||
|
}
|
||
|
// Try it forward again
|
||
|
{
|
||
9 years ago
|
let iterator1 = db.iterator(IteratorMode::Start);
|
||
10 years ago
|
assert_eq!(iterator1.collect::<Vec<_>>(), expected);
|
||
|
}
|
||
|
{
|
||
9 years ago
|
let iterator1 = db.iterator(IteratorMode::Start);
|
||
10 years ago
|
assert_eq!(iterator1.collect::<Vec<_>>(), expected);
|
||
|
}
|
||
|
|
||
9 years ago
|
let old_iterator = db.iterator(IteratorMode::Start);
|
||
10 years ago
|
let p = db.put(&*k4, &*v4);
|
||
10 years ago
|
assert!(p.is_ok());
|
||
9 years ago
|
let expected2 = vec![(cba(&k1), cba(&v1)),
|
||
|
(cba(&k2), cba(&v2)),
|
||
|
(cba(&k3), cba(&v3)),
|
||
|
(cba(&k4), cba(&v4))];
|
||
10 years ago
|
{
|
||
9 years ago
|
assert_eq!(old_iterator.collect::<Vec<_>>(), expected);
|
||
10 years ago
|
}
|
||
10 years ago
|
{
|
||
9 years ago
|
let iterator1 = db.iterator(IteratorMode::Start);
|
||
10 years ago
|
assert_eq!(iterator1.collect::<Vec<_>>(), expected2);
|
||
10 years ago
|
}
|
||
10 years ago
|
{
|
||
8 years ago
|
let iterator1 = db.iterator(IteratorMode::From(b"k2", Direction::Forward));
|
||
|
let expected = vec![(cba(&k2), cba(&v2)), (cba(&k3), cba(&v3)), (cba(&k4), cba(&v4))];
|
||
10 years ago
|
assert_eq!(iterator1.collect::<Vec<_>>(), expected);
|
||
10 years ago
|
}
|
||
10 years ago
|
{
|
||
8 years ago
|
let iterator1 = db.iterator(IteratorMode::From(b"k2", Direction::Reverse));
|
||
10 years ago
|
let expected = vec![(cba(&k2), cba(&v2)), (cba(&k1), cba(&v1))];
|
||
|
assert_eq!(iterator1.collect::<Vec<_>>(), expected);
|
||
10 years ago
|
}
|
||
9 years ago
|
{
|
||
8 years ago
|
let iterator1 = db.iterator(IteratorMode::From(b"k0", Direction::Forward));
|
||
9 years ago
|
assert!(iterator1.valid());
|
||
8 years ago
|
let iterator2 = db.iterator(IteratorMode::From(b"k1", Direction::Forward));
|
||
9 years ago
|
assert!(iterator2.valid());
|
||
8 years ago
|
let iterator3 = db.iterator(IteratorMode::From(b"k11", Direction::Forward));
|
||
9 years ago
|
assert!(iterator3.valid());
|
||
8 years ago
|
let iterator4 = db.iterator(IteratorMode::From(b"k5", Direction::Forward));
|
||
9 years ago
|
assert!(!iterator4.valid());
|
||
8 years ago
|
let iterator5 = db.iterator(IteratorMode::From(b"k0", Direction::Reverse));
|
||
9 years ago
|
assert!(iterator5.valid());
|
||
8 years ago
|
let iterator6 = db.iterator(IteratorMode::From(b"k1", Direction::Reverse));
|
||
9 years ago
|
assert!(iterator6.valid());
|
||
8 years ago
|
let iterator7 = db.iterator(IteratorMode::From(b"k11", Direction::Reverse));
|
||
9 years ago
|
assert!(iterator7.valid());
|
||
8 years ago
|
let iterator8 = db.iterator(IteratorMode::From(b"k5", Direction::Reverse));
|
||
9 years ago
|
assert!(!iterator8.valid());
|
||
|
}
|
||
|
{
|
||
8 years ago
|
let mut iterator1 = db.iterator(IteratorMode::From(b"k4", Direction::Forward));
|
||
9 years ago
|
iterator1.next();
|
||
|
assert!(iterator1.valid());
|
||
|
iterator1.next();
|
||
|
assert!(!iterator1.valid());
|
||
|
}
|
||
10 years ago
|
}
|
||
9 years ago
|
let opts = Options::default();
|
||
9 years ago
|
assert!(DB::destroy(&opts, path).is_ok());
|
||
10 years ago
|
}
|