|
|
@ -53,7 +53,6 @@ |
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//! ```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use libc::{self, c_char, c_int, c_void, size_t}; |
|
|
|
use libc::{self, c_char, c_int, c_void, size_t}; |
|
|
|
use std::ffi::CString; |
|
|
|
use std::ffi::CString; |
|
|
|
use std::mem; |
|
|
|
use std::mem; |
|
|
@ -88,15 +87,17 @@ pub unsafe extern "C" fn full_merge_callback( |
|
|
|
num_operands: c_int, |
|
|
|
num_operands: c_int, |
|
|
|
success: *mut u8, |
|
|
|
success: *mut u8, |
|
|
|
new_value_length: *mut size_t, |
|
|
|
new_value_length: *mut size_t, |
|
|
|
) -> *mut c_char { |
|
|
|
) -> *mut c_char { |
|
|
|
let cb = &mut *(raw_cb as *mut MergeOperatorCallback); |
|
|
|
let cb = &mut *(raw_cb as *mut MergeOperatorCallback); |
|
|
|
let operands = &mut MergeOperands::new(operands_list, operands_list_len, num_operands); |
|
|
|
let operands = &mut MergeOperands::new(operands_list, operands_list_len, num_operands); |
|
|
|
let key = slice::from_raw_parts(raw_key as *const u8, key_len as usize); |
|
|
|
let key = slice::from_raw_parts(raw_key as *const u8, key_len as usize); |
|
|
|
let oldval =
|
|
|
|
let oldval = if existing_value == ptr::null() { |
|
|
|
if existing_value == ptr::null() { |
|
|
|
|
|
|
|
None |
|
|
|
None |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
Some(slice::from_raw_parts(existing_value as *const u8, existing_value_len as usize)) |
|
|
|
Some(slice::from_raw_parts( |
|
|
|
|
|
|
|
existing_value as *const u8, |
|
|
|
|
|
|
|
existing_value_len as usize, |
|
|
|
|
|
|
|
)) |
|
|
|
}; |
|
|
|
}; |
|
|
|
if let Some(mut result) = (cb.full_merge_fn)(key, oldval, operands) { |
|
|
|
if let Some(mut result) = (cb.full_merge_fn)(key, oldval, operands) { |
|
|
|
result.shrink_to_fit(); |
|
|
|
result.shrink_to_fit(); |
|
|
@ -122,7 +123,7 @@ pub unsafe extern "C" fn partial_merge_callback( |
|
|
|
num_operands: c_int, |
|
|
|
num_operands: c_int, |
|
|
|
success: *mut u8, |
|
|
|
success: *mut u8, |
|
|
|
new_value_length: *mut size_t, |
|
|
|
new_value_length: *mut size_t, |
|
|
|
) -> *mut c_char { |
|
|
|
) -> *mut c_char { |
|
|
|
let cb = &mut *(raw_cb as *mut MergeOperatorCallback); |
|
|
|
let cb = &mut *(raw_cb as *mut MergeOperatorCallback); |
|
|
|
let operands = &mut MergeOperands::new(operands_list, operands_list_len, num_operands); |
|
|
|
let operands = &mut MergeOperands::new(operands_list, operands_list_len, num_operands); |
|
|
|
let key = slice::from_raw_parts(raw_key as *const u8, key_len as usize); |
|
|
|
let key = slice::from_raw_parts(raw_key as *const u8, key_len as usize); |
|
|
@ -141,7 +142,6 @@ pub unsafe extern "C" fn partial_merge_callback( |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct MergeOperands { |
|
|
|
pub struct MergeOperands { |
|
|
|
operands_list: *const *const c_char, |
|
|
|
operands_list: *const *const c_char, |
|
|
|
operands_list_len: *const size_t, |
|
|
|
operands_list_len: *const size_t, |
|
|
@ -220,9 +220,9 @@ mod test { |
|
|
|
Some(result) |
|
|
|
Some(result) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
fn mergetest() { |
|
|
|
fn mergetest() { |
|
|
|
use {DB, Options}; |
|
|
|
use {Options, DB}; |
|
|
|
|
|
|
|
|
|
|
|
let path = "_rust_rocksdb_mergetest"; |
|
|
|
let path = "_rust_rocksdb_mergetest"; |
|
|
|
let mut opts = Options::default(); |
|
|
|
let mut opts = Options::default(); |
|
|
@ -239,12 +239,10 @@ mod test { |
|
|
|
let m = db.merge(b"k1", b"h"); |
|
|
|
let m = db.merge(b"k1", b"h"); |
|
|
|
assert!(m.is_ok()); |
|
|
|
assert!(m.is_ok()); |
|
|
|
match db.get(b"k1") { |
|
|
|
match db.get(b"k1") { |
|
|
|
Ok(Some(value)) => { |
|
|
|
Ok(Some(value)) => match value.to_utf8() { |
|
|
|
match value.to_utf8() { |
|
|
|
|
|
|
|
Some(v) => println!("retrieved utf8 value: {}", v), |
|
|
|
Some(v) => println!("retrieved utf8 value: {}", v), |
|
|
|
None => println!("did not read valid utf-8 out of the db"), |
|
|
|
None => println!("did not read valid utf-8 out of the db"), |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
Err(_) => println!("error reading value"), |
|
|
|
Err(_) => println!("error reading value"), |
|
|
|
_ => panic!("value not present"), |
|
|
|
_ => panic!("value not present"), |
|
|
|
} |
|
|
|
} |
|
|
@ -259,25 +257,24 @@ mod test { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
unsafe fn to_slice<T: Sized>(p: &T) -> &[u8] { |
|
|
|
unsafe fn to_slice<T: Sized>(p: &T) -> &[u8] { |
|
|
|
::std::slice::from_raw_parts( |
|
|
|
::std::slice::from_raw_parts((p as *const T) as *const u8, ::std::mem::size_of::<T>()) |
|
|
|
(p as *const T) as *const u8, |
|
|
|
|
|
|
|
::std::mem::size_of::<T>(), |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn from_slice<T: Sized>(s: &[u8]) -> Option<&T> { |
|
|
|
fn from_slice<T: Sized>(s: &[u8]) -> Option<&T> { |
|
|
|
if ::std::mem::size_of::<T>() != s.len() { |
|
|
|
if ::std::mem::size_of::<T>() != s.len() { |
|
|
|
println!("slice {:?} is len {}, but T is size {}", s, s.len(), ::std::mem::size_of::<T>()); |
|
|
|
println!( |
|
|
|
|
|
|
|
"slice {:?} is len {}, but T is size {}", |
|
|
|
|
|
|
|
s, |
|
|
|
|
|
|
|
s.len(), |
|
|
|
|
|
|
|
::std::mem::size_of::<T>() |
|
|
|
|
|
|
|
); |
|
|
|
None |
|
|
|
None |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
unsafe { |
|
|
|
unsafe { Some(::std::mem::transmute(s.as_ptr())) } |
|
|
|
Some(::std::mem::transmute(s.as_ptr())) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[repr(packed)] |
|
|
|
#[repr(packed)] |
|
|
|
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug)] |
|
|
|
#[derive(Copy, Clone, Debug)] |
|
|
|
struct ValueCounts { |
|
|
|
struct ValueCounts { |
|
|
|
num_a: u32, |
|
|
|
num_a: u32, |
|
|
@ -306,16 +303,15 @@ mod test { |
|
|
|
existing_val: Option<&[u8]>, |
|
|
|
existing_val: Option<&[u8]>, |
|
|
|
operands: &mut MergeOperands, |
|
|
|
operands: &mut MergeOperands, |
|
|
|
) -> Option<Vec<u8>> { |
|
|
|
) -> Option<Vec<u8>> { |
|
|
|
|
|
|
|
let mut counts: ValueCounts = if let Some(v) = existing_val { |
|
|
|
let mut counts : ValueCounts =
|
|
|
|
|
|
|
|
if let Some(v) = existing_val { |
|
|
|
|
|
|
|
from_slice::<ValueCounts>(v).unwrap().clone() |
|
|
|
from_slice::<ValueCounts>(v).unwrap().clone() |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
ValueCounts { |
|
|
|
ValueCounts { |
|
|
|
num_a: 0, |
|
|
|
num_a: 0, |
|
|
|
num_b: 0, |
|
|
|
num_b: 0, |
|
|
|
num_c: 0, |
|
|
|
num_c: 0, |
|
|
|
num_d: 0 } |
|
|
|
num_d: 0, |
|
|
|
|
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
for op in operands { |
|
|
|
for op in operands { |
|
|
@ -333,11 +329,11 @@ mod test { |
|
|
|
Some(slc.to_vec()) |
|
|
|
Some(slc.to_vec()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
fn counting_mergetest() { |
|
|
|
fn counting_mergetest() { |
|
|
|
use std::thread; |
|
|
|
|
|
|
|
use std::sync::Arc; |
|
|
|
use std::sync::Arc; |
|
|
|
use {DB, Options, DBCompactionStyle}; |
|
|
|
use std::thread; |
|
|
|
|
|
|
|
use {DBCompactionStyle, Options, DB}; |
|
|
|
|
|
|
|
|
|
|
|
let path = "_rust_rocksdb_partial_mergetest"; |
|
|
|
let path = "_rust_rocksdb_partial_mergetest"; |
|
|
|
let mut opts = Options::default(); |
|
|
|
let mut opts = Options::default(); |
|
|
@ -345,7 +341,11 @@ mod test { |
|
|
|
opts.set_compaction_style(DBCompactionStyle::Universal); |
|
|
|
opts.set_compaction_style(DBCompactionStyle::Universal); |
|
|
|
opts.set_min_write_buffer_number_to_merge(10); |
|
|
|
opts.set_min_write_buffer_number_to_merge(10); |
|
|
|
|
|
|
|
|
|
|
|
opts.set_merge_operator("sort operator", test_counting_full_merge, Some(test_counting_partial_merge)); |
|
|
|
opts.set_merge_operator( |
|
|
|
|
|
|
|
"sort operator", |
|
|
|
|
|
|
|
test_counting_full_merge, |
|
|
|
|
|
|
|
Some(test_counting_partial_merge), |
|
|
|
|
|
|
|
); |
|
|
|
{ |
|
|
|
{ |
|
|
|
let db = Arc::new(DB::open(&opts, path).unwrap()); |
|
|
|
let db = Arc::new(DB::open(&opts, path).unwrap()); |
|
|
|
let _ = db.delete(b"k1"); |
|
|
|
let _ = db.delete(b"k1"); |
|
|
@ -421,8 +421,7 @@ mod test { |
|
|
|
h3.join().unwrap(); |
|
|
|
h3.join().unwrap(); |
|
|
|
h1.join().unwrap(); |
|
|
|
h1.join().unwrap(); |
|
|
|
match db.get(b"k2") { |
|
|
|
match db.get(b"k2") { |
|
|
|
Ok(Some(value)) => { |
|
|
|
Ok(Some(value)) => match from_slice::<ValueCounts>(&*value) { |
|
|
|
match from_slice::<ValueCounts>(&*value) { |
|
|
|
|
|
|
|
Some(v) => unsafe { |
|
|
|
Some(v) => unsafe { |
|
|
|
assert_eq!(v.num_a, 1000); |
|
|
|
assert_eq!(v.num_a, 1000); |
|
|
|
assert_eq!(v.num_b, 500); |
|
|
|
assert_eq!(v.num_b, 500); |
|
|
@ -430,14 +429,12 @@ mod test { |
|
|
|
assert_eq!(v.num_d, 500); |
|
|
|
assert_eq!(v.num_d, 500); |
|
|
|
}, |
|
|
|
}, |
|
|
|
None => panic!("Failed to get ValueCounts from db"), |
|
|
|
None => panic!("Failed to get ValueCounts from db"), |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
Err(e) => panic!("error reading value {:?}", e), |
|
|
|
Err(e) => panic!("error reading value {:?}", e), |
|
|
|
_ => panic!("value not present"), |
|
|
|
_ => panic!("value not present"), |
|
|
|
} |
|
|
|
} |
|
|
|
match db.get(b"k1") { |
|
|
|
match db.get(b"k1") { |
|
|
|
Ok(Some(value)) => { |
|
|
|
Ok(Some(value)) => match from_slice::<ValueCounts>(&*value) { |
|
|
|
match from_slice::<ValueCounts>(&*value) { |
|
|
|
|
|
|
|
Some(v) => unsafe { |
|
|
|
Some(v) => unsafe { |
|
|
|
assert_eq!(v.num_a, 3); |
|
|
|
assert_eq!(v.num_a, 3); |
|
|
|
assert_eq!(v.num_b, 2); |
|
|
|
assert_eq!(v.num_b, 2); |
|
|
@ -445,8 +442,7 @@ mod test { |
|
|
|
assert_eq!(v.num_d, 1); |
|
|
|
assert_eq!(v.num_d, 1); |
|
|
|
}, |
|
|
|
}, |
|
|
|
None => panic!("Failed to get ValueCounts from db"), |
|
|
|
None => panic!("Failed to get ValueCounts from db"), |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
Err(e) => panic!("error reading value {:?}", e), |
|
|
|
Err(e) => panic!("error reading value {:?}", e), |
|
|
|
_ => panic!("value not present"), |
|
|
|
_ => panic!("value not present"), |
|
|
|
} |
|
|
|
} |
|
|
|