From 49966e99b643daef3f5610f990de5f598ace5743 Mon Sep 17 00:00:00 2001 From: Nelson Elhage Date: Sun, 14 Jun 2020 01:00:19 -0700 Subject: [PATCH] Fix slice transformers with no in_domain callback (#438) --- src/slice_transform.rs | 16 ++++++---------- tests/test_slice_transform.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/slice_transform.rs b/src/slice_transform.rs index 403303a..396012f 100644 --- a/src/slice_transform.rs +++ b/src/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 + } } diff --git a/tests/test_slice_transform.rs b/tests/test_slice_transform.rs index b2a3d3b..6886860 100644 --- a/tests/test_slice_transform.rs +++ b/tests/test_slice_transform.rs @@ -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"); + } +}