Allow to pass in owned variable for Decision::Change variant in compaction filter (#725)

master
Victor Farazdagi 2 years ago committed by GitHub
parent 3b44ee922c
commit 5ed1043488
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      src/compaction_filter.rs
  2. 2
      tests/test_compationfilter.rs

@ -28,7 +28,7 @@ pub enum Decision {
/// Remove the object from the database /// Remove the object from the database
Remove, Remove,
/// Change the value for the key /// Change the value for the key
Change(&'static [u8]), Change(Vec<u8>),
} }
/// CompactionFilter allows an application to modify/delete a key-value at /// CompactionFilter allows an application to modify/delete a key-value at
@ -73,6 +73,7 @@ pub trait CompactionFilter {
/// ///
/// [set_compaction_filter]: ../struct.Options.html#method.set_compaction_filter /// [set_compaction_filter]: ../struct.Options.html#method.set_compaction_filter
pub trait CompactionFilterFn: FnMut(u32, &[u8], &[u8]) -> Decision {} pub trait CompactionFilterFn: FnMut(u32, &[u8], &[u8]) -> Decision {}
impl<F> CompactionFilterFn for F where F: FnMut(u32, &[u8], &[u8]) -> Decision + Send + 'static {} impl<F> CompactionFilterFn for F where F: FnMut(u32, &[u8], &[u8]) -> Decision + Send + 'static {}
pub struct CompactionFilterCallback<F> pub struct CompactionFilterCallback<F>
@ -135,8 +136,8 @@ where
Keep => 0, Keep => 0,
Remove => 1, Remove => 1,
Change(newval) => { Change(newval) => {
*new_value = newval.as_ptr() as *mut c_char;
*new_value_length = newval.len() as size_t; *new_value_length = newval.len() as size_t;
*new_value = Box::into_raw(newval.into_boxed_slice()) as *mut c_char;
*value_changed = 1_u8; *value_changed = 1_u8;
0 0
} }
@ -149,7 +150,7 @@ fn test_filter(level: u32, key: &[u8], value: &[u8]) -> Decision {
use self::Decision::{Change, Keep, Remove}; use self::Decision::{Change, Keep, Remove};
match key.first() { match key.first() {
Some(&b'_') => Remove, Some(&b'_') => Remove,
Some(&b'%') => Change(b"secret"), Some(&b'%') => Change(b"secret".to_vec()),
_ => Keep, _ => Keep,
} }
} }

@ -25,7 +25,7 @@ fn test_filter(level: u32, key: &[u8], value: &[u8]) -> CompactionDecision {
use self::CompactionDecision::*; use self::CompactionDecision::*;
match key.first() { match key.first() {
Some(&b'_') => Remove, Some(&b'_') => Remove,
Some(&b'%') => Change(b"secret"), Some(&b'%') => Change(b"secret".to_vec()),
_ => Keep, _ => Keep,
} }
} }

Loading…
Cancel
Save