-
Notifications
You must be signed in to change notification settings - Fork 704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix merge fn closure from pr385 #481
Changes from 3 commits
a5ea9fc
9a2dcad
ed222f1
3529aa0
6426655
9881373
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,8 @@ | |
//!let mut opts = Options::default(); | ||
//! | ||
//!opts.create_if_missing(true); | ||
//!opts.set_merge_operator("test operator", concat_merge, None); | ||
//!opts.set_merge_operator_associative("test operator", concat_merge); | ||
//!//opts.set_merge_operator("test operator", concat_merge, partial_concat_merge); // if your merge is not associative | ||
//!{ | ||
//! let db = DB::open(&opts, path).unwrap(); | ||
//! let p = db.put(b"k1", b"a"); | ||
|
@@ -61,16 +62,25 @@ use std::mem; | |
use std::ptr; | ||
use std::slice; | ||
|
||
pub type MergeFn = fn(&[u8], Option<&[u8]>, &mut MergeOperands) -> Option<Vec<u8>>; | ||
//pub type MergeFn = fn(&[u8], Option<&[u8]>, &mut MergeOperands) -> Option<Vec<u8>>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this comment really needed ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Obviously not, it can be deleted There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I suspect things may get stuck here. I think this was a suggestion for you to update the PR @BoOTheFurious . But from your comment it seems like you expect someone else to do it 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry… I've done it |
||
pub trait MergeFn: | ||
Fn(&[u8], Option<&[u8]>, &mut MergeOperands) -> Option<Vec<u8>> + Send + Sync + 'static | ||
{ | ||
} | ||
impl<F> MergeFn for F where | ||
F: Fn(&[u8], Option<&[u8]>, &mut MergeOperands) -> Option<Vec<u8>> + Send + Sync + 'static | ||
{ | ||
} | ||
|
||
pub struct MergeOperatorCallback { | ||
pub struct MergeOperatorCallback<F: MergeFn, PF: MergeFn> { | ||
pub name: CString, | ||
pub full_merge_fn: MergeFn, | ||
pub partial_merge_fn: MergeFn, | ||
pub full_merge_fn: F, | ||
pub partial_merge_fn: PF, | ||
} | ||
|
||
pub unsafe extern "C" fn destructor_callback(raw_cb: *mut c_void) { | ||
let _: Box<MergeOperatorCallback> = mem::transmute(raw_cb); | ||
pub unsafe extern "C" fn destructor_callback<F: MergeFn, PF: MergeFn>(raw_cb: *mut c_void) { | ||
let _: Box<MergeOperatorCallback<F, PF>> = | ||
Box::from_raw(raw_cb as *mut MergeOperatorCallback<F, PF>); | ||
} | ||
|
||
pub unsafe extern "C" fn delete_callback( | ||
|
@@ -86,12 +96,14 @@ pub unsafe extern "C" fn delete_callback( | |
} | ||
} | ||
|
||
pub unsafe extern "C" fn name_callback(raw_cb: *mut c_void) -> *const c_char { | ||
let cb = &mut *(raw_cb as *mut MergeOperatorCallback); | ||
pub unsafe extern "C" fn name_callback<F: MergeFn, PF: MergeFn>( | ||
raw_cb: *mut c_void, | ||
) -> *const c_char { | ||
let cb = &mut *(raw_cb as *mut MergeOperatorCallback<F, PF>); | ||
cb.name.as_ptr() | ||
} | ||
|
||
pub unsafe extern "C" fn full_merge_callback( | ||
pub unsafe extern "C" fn full_merge_callback<F: MergeFn, PF: MergeFn>( | ||
raw_cb: *mut c_void, | ||
raw_key: *const c_char, | ||
key_len: size_t, | ||
|
@@ -103,7 +115,7 @@ pub unsafe extern "C" fn full_merge_callback( | |
success: *mut u8, | ||
new_value_length: *mut size_t, | ||
) -> *mut c_char { | ||
let cb = &mut *(raw_cb as *mut MergeOperatorCallback); | ||
let cb = &mut *(raw_cb as *mut MergeOperatorCallback<F, PF>); | ||
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 oldval = if existing_value.is_null() { | ||
|
@@ -128,7 +140,7 @@ pub unsafe extern "C" fn full_merge_callback( | |
) | ||
} | ||
|
||
pub unsafe extern "C" fn partial_merge_callback( | ||
pub unsafe extern "C" fn partial_merge_callback<F: MergeFn, PF: MergeFn>( | ||
raw_cb: *mut c_void, | ||
raw_key: *const c_char, | ||
key_len: size_t, | ||
|
@@ -138,7 +150,7 @@ pub unsafe extern "C" fn partial_merge_callback( | |
success: *mut u8, | ||
new_value_length: *mut size_t, | ||
) -> *mut c_char { | ||
let cb = &mut *(raw_cb as *mut MergeOperatorCallback); | ||
let cb = &mut *(raw_cb as *mut MergeOperatorCallback<F, PF>); | ||
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); | ||
(cb.partial_merge_fn)(key, None, operands).map_or_else( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose this comment could be deleted as well ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops…
done !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks.