Module rocksdb::merge_operator

source ·
Expand description

rustic merge operator

use rocksdb::{Options, DB, MergeOperands};

fn concat_merge(new_key: &[u8],
                existing_val: Option<&[u8]>,
                operands: &MergeOperands)
                -> Option<Vec<u8>> {

   let mut result: Vec<u8> = Vec::with_capacity(operands.len());
   existing_val.map(|v| {
       for e in v {
           result.push(*e)
       }
   });
   for op in operands {
       for e in op {
           result.push(*e)
       }
   }
   Some(result)
}

let path = "_rust_path_to_rocksdb";
let mut opts = Options::default();

opts.create_if_missing(true);
opts.set_merge_operator_associative("test operator", concat_merge);
{
   let db = DB::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_eq!(r.unwrap().unwrap(), b"abcdefg");
}
let _ = DB::destroy(&opts, path);

Structs§

Traits§

Functions§