Fix slice transformers with no in_domain callback (#438)

master
Nelson Elhage 4 years ago committed by GitHub
parent 22fc1faca4
commit 49966e99b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      src/slice_transform.rs
  2. 29
      tests/test_slice_transform.rs

@ -50,14 +50,7 @@ impl SliceTransform {
cb as *mut c_void,
Some(slice_transform_destructor_callback),
Some(transform_callback),
// this is ugly, but I can't get the compiler
// not to barf with "expected fn pointer, found fn item"
// without this. sorry.
if in_domain_fn.is_some() {
Some(in_domain_callback)
} else {
None
},
Some(in_domain_callback),
// this None points to the deprecated InRange callback
None,
Some(slice_transform_name_callback),
@ -118,6 +111,9 @@ pub unsafe extern "C" fn in_domain_callback(
) -> u8 {
let cb = &mut *(raw_cb as *mut TransformCallback);
let key = slice::from_raw_parts(raw_key as *const u8, key_len as usize);
let in_domain = cb.in_domain_fn.unwrap();
in_domain(key) as u8
if let Some(in_domain) = cb.in_domain_fn {
in_domain(key) as u8
} else {
0xff
}
}

@ -64,3 +64,32 @@ pub fn test_slice_transform() {
}
}
}
#[test]
fn test_no_in_domain() {
fn extract_suffix(slice: &[u8]) -> &[u8] {
if slice.len() > 4 {
&slice[slice.len() - 4..slice.len()]
} else {
slice
}
}
let db_path = DBPath::new("_rust_rocksdb_prefix_test");
{
let mut opts = Options::default();
opts.create_if_missing(true);
opts.set_prefix_extractor(SliceTransform::create(
"test slice transform",
extract_suffix,
None,
));
opts.set_memtable_prefix_bloom_ratio(0.1);
let db = DB::open(&opts, &db_path).unwrap();
db.put(b"key_sfx1", b"a").unwrap();
db.put(b"key_sfx2", b"b").unwrap();
assert_eq!(db.get(b"key_sfx1").unwrap().unwrap(), b"a");
}
}

Loading…
Cancel
Save