extern crate rocksdb;
use rocksdb::{DB, MergeOperands, Options};
#[test]
pub fn test_column_family() {
let path = "_rust_rocksdb_cftest";
{
let mut opts = Options::default();
opts.create_if_missing(true);
opts.set_merge_operator("test operator", test_provided_merge);
let mut db = DB::open(&opts, path).unwrap();
let opts = Options::default();
match db.create_cf("cf1", &opts) {
Ok(_) => println!("cf1 created successfully"),
Err(e) => {
panic!("could not create column family: {}", e);
}
}
}
{
let mut opts = Options::default();
opts.set_merge_operator("test operator", test_provided_merge);
match DB::open(&opts, path) {
Ok(_) => {
panic!("should not have opened DB successfully without \
specifying column
families")
}
Err(e) => {
assert!(e.to_string()
.starts_with("Invalid argument: You have to open all \
column families."))
}
}
}
{
let mut opts = Options::default();
opts.set_merge_operator("test operator", test_provided_merge);
match DB::open_cf(&opts, path, &["cf1"]) {
Ok(_) => println!("successfully opened db with column family"),
Err(e) => panic!("failed to open db with column family: {}", e),
}
}
{
}
{
}
{
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";
{
let mut opts = Options::default();
opts.set_merge_operator("test operator", test_provided_merge);
let db = match DB::open_cf(&opts, path, &["cf1"]) {
Ok(db) => {
println!("successfully opened db with column family");
db
}
Err(e) => panic!("failed to open db with column family: {}", e),
};
let cf1 = db.cf_handle("cf1").unwrap();
assert!(db.put_cf(cf1, b"k1", b"v1").is_ok());
assert!(db.get_cf(cf1, b"k1").unwrap().unwrap().to_utf8().unwrap() == "v1");
let p = db.put_cf(cf1, b"k1", b"a");
assert!(p.is_ok());
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();
let m = db.merge_cf(cf1, b"k1", b"h");
println!("m is {:?}", m);
match db.get(b"k1") {
Ok(Some(value)) => {
match value.to_utf8() {
Some(v) => println!("retrieved utf8 value: {}", v),
None => println!("did not read valid utf-8 out of the db"),
}
}
Err(_) => println!("error reading value"),
_ => panic!("value not present!"),
}
let _ = db.get_cf(cf1, b"k1");
assert!(db.delete(b"k1").is_ok());
assert!(db.get(b"k1").unwrap().is_none());
}
}
fn test_provided_merge(_: &[u8],
existing_val: Option<&[u8]>,
operands: &mut MergeOperands)
-> 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);
}
}
None => (),
}
for op in operands {
for e in op {
result.push(*e);
}
}
result
}