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.
150 lines
4.9 KiB
150 lines
4.9 KiB
9 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, MergeOperands, Options};
|
||
10 years ago
|
|
||
|
#[test]
|
||
|
pub fn test_column_family() {
|
||
|
let path = "_rust_rocksdb_cftest";
|
||
|
|
||
|
// should be able to create column families
|
||
|
{
|
||
9 years ago
|
let mut opts = Options::default();
|
||
10 years ago
|
opts.create_if_missing(true);
|
||
8 years ago
|
opts.set_merge_operator("test operator", test_provided_merge);
|
||
9 years ago
|
let mut db = DB::open(&opts, path).unwrap();
|
||
9 years ago
|
let opts = Options::default();
|
||
10 years ago
|
match db.create_cf("cf1", &opts) {
|
||
|
Ok(_) => println!("cf1 created successfully"),
|
||
|
Err(e) => {
|
||
|
panic!("could not create column family: {}", e);
|
||
9 years ago
|
}
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// should fail to open db without specifying same column families
|
||
|
{
|
||
9 years ago
|
let mut opts = Options::default();
|
||
8 years ago
|
opts.set_merge_operator("test operator", test_provided_merge);
|
||
9 years ago
|
match DB::open(&opts, path) {
|
||
9 years ago
|
Ok(_) => {
|
||
|
panic!("should not have opened DB successfully without \
|
||
|
specifying column
|
||
|
families")
|
||
|
}
|
||
|
Err(e) => {
|
||
8 years ago
|
assert!(e.to_string()
|
||
|
.starts_with("Invalid argument: You have to open all \
|
||
|
column families."))
|
||
9 years ago
|
}
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// should properly open db when specyfing all column families
|
||
|
{
|
||
9 years ago
|
let mut opts = Options::default();
|
||
8 years ago
|
opts.set_merge_operator("test operator", test_provided_merge);
|
||
9 years ago
|
match DB::open_cf(&opts, path, &["cf1"]) {
|
||
10 years ago
|
Ok(_) => println!("successfully opened db with column family"),
|
||
10 years ago
|
Err(e) => panic!("failed to open db with column family: {}", e),
|
||
10 years ago
|
}
|
||
|
}
|
||
8 years ago
|
// TODO should be able to use writebatch ops with a cf
|
||
|
{
|
||
|
}
|
||
|
// TODO should be able to iterate over a cf
|
||
|
{
|
||
|
}
|
||
|
// should b able to drop a cf
|
||
|
{
|
||
|
let mut db = DB::open_cf(&Options::default(), path, &["cf1"]).unwrap();
|
||
|
match db.drop_cf("cf1") {
|
||
|
Ok(_) => println!("cf1 successfully dropped."),
|
||
|
Err(e) => panic!("failed to drop column family: {}", e),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
assert!(DB::destroy(&Options::default(), path).is_ok());
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
#[ignore]
|
||
|
fn test_merge_operator() {
|
||
|
let path = "_rust_rocksdb_cftest_merge";
|
||
9 years ago
|
// TODO should be able to write, read, merge, batch, and iterate over a cf
|
||
10 years ago
|
{
|
||
9 years ago
|
let mut opts = Options::default();
|
||
8 years ago
|
opts.set_merge_operator("test operator", test_provided_merge);
|
||
9 years ago
|
let db = match DB::open_cf(&opts, path, &["cf1"]) {
|
||
10 years ago
|
Ok(db) => {
|
||
|
println!("successfully opened db with column family");
|
||
|
db
|
||
9 years ago
|
}
|
||
10 years ago
|
Err(e) => panic!("failed to open db with column family: {}", e),
|
||
|
};
|
||
8 years ago
|
let cf1 = db.cf_handle("cf1").unwrap();
|
||
9 years ago
|
assert!(db.put_cf(cf1, b"k1", b"v1").is_ok());
|
||
8 years ago
|
assert!(db.get_cf(cf1, b"k1").unwrap().unwrap().to_utf8().unwrap() == "v1");
|
||
9 years ago
|
let p = db.put_cf(cf1, b"k1", b"a");
|
||
10 years ago
|
assert!(p.is_ok());
|
||
9 years ago
|
db.merge_cf(cf1, b"k1", b"b").unwrap();
|
||
|
db.merge_cf(cf1, b"k1", b"c").unwrap();
|
||
|
db.merge_cf(cf1, b"k1", b"d").unwrap();
|
||
|
db.merge_cf(cf1, b"k1", b"efg").unwrap();
|
||
9 years ago
|
let m = db.merge_cf(cf1, b"k1", b"h");
|
||
10 years ago
|
println!("m is {:?}", m);
|
||
9 years ago
|
// TODO assert!(m.is_ok());
|
||
9 years ago
|
match db.get(b"k1") {
|
||
|
Ok(Some(value)) => {
|
||
10 years ago
|
match value.to_utf8() {
|
||
9 years ago
|
Some(v) => println!("retrieved utf8 value: {}", v),
|
||
|
None => println!("did not read valid utf-8 out of the db"),
|
||
10 years ago
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
Err(_) => println!("error reading value"),
|
||
9 years ago
|
_ => panic!("value not present!"),
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
let _ = db.get_cf(cf1, b"k1");
|
||
9 years ago
|
// TODO assert!(r.unwrap().to_utf8().unwrap() == "abcdefgh");
|
||
10 years ago
|
assert!(db.delete(b"k1").is_ok());
|
||
9 years ago
|
assert!(db.get(b"k1").unwrap().is_none());
|
||
10 years ago
|
}
|
||
|
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
fn test_provided_merge(_: &[u8],
|
||
10 years ago
|
existing_val: Option<&[u8]>,
|
||
9 years ago
|
operands: &mut MergeOperands)
|
||
10 years ago
|
-> Vec<u8> {
|
||
|
let nops = operands.size_hint().0;
|
||
|
let mut result: Vec<u8> = Vec::with_capacity(nops);
|
||
|
match existing_val {
|
||
|
Some(v) => {
|
||
|
for e in v {
|
||
|
result.push(*e);
|
||
|
}
|
||
9 years ago
|
}
|
||
10 years ago
|
None => (),
|
||
|
}
|
||
|
for op in operands {
|
||
|
for e in op {
|
||
|
result.push(*e);
|
||
|
}
|
||
|
}
|
||
|
result
|
||
|
}
|