cleanup for merge operators

master
Tyler Neely 10 years ago
parent 0f0f5910f4
commit e98a85f4cc
  1. 36
      README.md
  2. 3
      src/lib.rs
  3. 44
      src/main.rs
  4. 6
      src/rocksdb.rs

@ -32,6 +32,42 @@ fn main() {
} }
``` ```
###### Rustic Merge Operator
```rust
extern crate rocksdb;
use rocksdb::{RocksDBOptions, RocksDB, MergeOperands};
fn concat_merge<'a>(new_key: &[u8], existing_val: Option<&[u8]>,
mut operands: &mut MergeOperands) -> Vec<u8> {
let mut result: Vec<u8> = Vec::with_capacity(operands.size_hint().val0());
match existing_val {
Some(v) => result.push_all(v),
None => (),
}
for op in operands {
result.push_all(op);
}
result
}
fn main() {
let path = "/path/to/rocksdb";
let opts = RocksDBOptions::new();
opts.create_if_missing(true);
opts.add_merge_operator("test operator", concat_merge);
let db = RocksDB::open(opts, path).unwrap();
let p = db.put(b"k1", b"a");
db.merge(b"k1", b"b");
db.merge(b"k1", b"c");
db.merge(b"k1", b"d");
db.merge(b"k1", b"efg");
let r = db.get(b"k1");
assert!(r.unwrap().to_utf8().unwrap() == "abcdefg");
db.close();
}
```
### status ### status
- [x] basic open/put/get/delete/close - [x] basic open/put/get/delete/close
- [x] linux support - [x] linux support

@ -5,7 +5,10 @@
pub use ffi as rocksdb_ffi; pub use ffi as rocksdb_ffi;
pub use rocksdb::{ pub use rocksdb::{
RocksDB, RocksDB,
MergeOperands,
RocksDBResult, RocksDBResult,
RocksDBOptions,
RocksDBVector,
}; };
pub mod rocksdb; pub mod rocksdb;
pub mod ffi; pub mod ffi;

@ -1,14 +1,13 @@
extern crate rocksdb; extern crate rocksdb;
extern crate test; extern crate test;
use rocksdb::RocksDB; use rocksdb::{RocksDBOptions, RocksDB, MergeOperands};
use test::Bencher; use test::Bencher;
#[allow(dead_code)] #[allow(dead_code)]
fn main() { fn main() {
match RocksDB::open_default("/tmp/rust-rocksdb") { let path = "/tmp/rust-rocksdb";
Ok(db) => { let db = RocksDB::open_default(path).unwrap();
assert!(db.put(b"my key", b"my value").is_ok()); assert!(db.put(b"my key", b"my value").is_ok());
db.get(b"my key").map( |value| { db.get(b"my key").map( |value| {
match value.to_utf8() { match value.to_utf8() {
Some(v) => Some(v) =>
@ -21,11 +20,42 @@ fn main() {
.on_error( |e| { println!("error retrieving value: {}", e) }); .on_error( |e| { println!("error retrieving value: {}", e) });
assert!(db.delete(b"my key").is_ok()); assert!(db.delete(b"my key").is_ok());
db.close(); db.close();
},
Err(e) => panic!(e), custom_merge();
}
#[allow(dead_code)]
fn concat_merge(new_key: &[u8], existing_val: Option<&[u8]>,
mut operands: &mut MergeOperands) -> Vec<u8> {
let mut result: Vec<u8> = Vec::with_capacity(operands.size_hint().val0());
match existing_val {
Some(v) => result.push_all(v),
None => (),
} }
for op in operands {
result.push_all(op);
}
result
}
#[allow(dead_code)]
fn custom_merge() {
let path = "_rust_rocksdb_mergetest";
let opts = RocksDBOptions::new();
opts.create_if_missing(true);
opts.add_merge_operator("test operator", concat_merge);
let db = RocksDB::open(opts, path).unwrap();
let p = db.put(b"k1", b"a");
db.merge(b"k1", b"b");
db.merge(b"k1", b"c");
db.merge(b"k1", b"d");
db.merge(b"k1", b"efg");
let m = db.merge(b"k1", b"h");
let r = db.get(b"k1");
assert!(r.unwrap().to_utf8().unwrap() == "abcdefgh");
db.close();
RocksDB::destroy(opts, path).is_ok();
} }
#[allow(dead_code)] #[allow(dead_code)]

@ -54,7 +54,7 @@ impl RocksDBOptions {
} }
} }
pub fn add_merge_operator<'a>(&self, name: &str, merge_fn: for <'a> fn (&[u8], Option<&[u8]>, &mut MergeOperands) -> Vec<u8>) { pub fn add_merge_operator<'a>(&self, name: &str, merge_fn: fn (&[u8], Option<&[u8]>, &mut MergeOperands) -> Vec<u8>) {
let cb = box MergeOperatorCallback { let cb = box MergeOperatorCallback {
name: name.to_c_str(), name: name.to_c_str(),
merge_fn: merge_fn, merge_fn: merge_fn,
@ -418,7 +418,7 @@ impl <'a> Iterator<&'a [u8]> for &'a mut MergeOperands<'a> {
struct MergeOperatorCallback { struct MergeOperatorCallback {
name: CString, name: CString,
merge_fn: for <'b> fn (&[u8], Option<&[u8]>, &mut MergeOperands) -> Vec<u8>, merge_fn: fn (&[u8], Option<&[u8]>, &mut MergeOperands) -> Vec<u8>,
} }
extern "C" fn destructor_callback(raw_cb: *mut c_void) { extern "C" fn destructor_callback(raw_cb: *mut c_void) {
@ -479,7 +479,7 @@ extern "C" fn partial_merge_callback(
} }
} }
fn test_provided_merge<'a>(new_key: &[u8], existing_val: Option<&[u8]>, fn test_provided_merge(new_key: &[u8], existing_val: Option<&[u8]>,
mut operands: &mut MergeOperands) -> Vec<u8> { mut operands: &mut MergeOperands) -> Vec<u8> {
let mut result: Vec<u8> = Vec::with_capacity(operands.size_hint().val0()); let mut result: Vec<u8> = Vec::with_capacity(operands.size_hint().val0());
match existing_val { match existing_val {

Loading…
Cancel
Save