From b2675dd6cff24e4fb98d038360ea4d51d34fe85d Mon Sep 17 00:00:00 2001 From: zackcam Date: Thu, 10 Oct 2024 18:33:31 +0000 Subject: [PATCH] Updating _ to -in lib.rs. Also updating loading from rdb to use a trait instead Signed-off-by: zackcam --- src/bloom/data_type.rs | 107 +++++++++++++++++----------------- src/lib.rs | 4 +- src/wrapper/bloom_callback.rs | 3 +- 3 files changed, 59 insertions(+), 55 deletions(-) diff --git a/src/bloom/data_type.rs b/src/bloom/data_type.rs index 7bb34aa..326333c 100644 --- a/src/bloom/data_type.rs +++ b/src/bloom/data_type.rs @@ -46,67 +46,70 @@ pub static BLOOM_FILTER_TYPE: ValkeyType = ValkeyType::new( }, ); -/// Callback to load and parse RDB data of a bloom item and create it. -pub fn bloom_rdb_load_data_object( - rdb: *mut raw::RedisModuleIO, - encver: i32, -) -> Option { - if encver > BLOOM_FILTER_TYPE_ENCODING_VERSION { - logging::log_warning(format!("{}: Cannot load bloomfltr data type of version {} because it is higher than the loaded module's bloomfltr supported version {}", MODULE_NAME, encver, BLOOM_FILTER_TYPE_ENCODING_VERSION).as_str()); - return None; - } - let Ok(num_filters) = raw::load_unsigned(rdb) else { - return None; - }; - let Ok(expansion) = raw::load_unsigned(rdb) else { - return None; - }; - let Ok(fp_rate) = raw::load_float(rdb) else { - return None; - }; - let mut filters = Vec::new(); - for i in 0..num_filters { - let Ok(bitmap) = raw::load_string_buffer(rdb) else { +pub trait ValkeyDataType { + fn load_from_rdb(rdb: *mut raw::RedisModuleIO, encver: i32) -> Option; +} + +impl ValkeyDataType for BloomFilterType { + /// Callback to load and parse RDB data of a bloom item and create it. + fn load_from_rdb(rdb: *mut raw::RedisModuleIO, encver: i32) -> Option { + let mut filters = Vec::new(); + if encver > BLOOM_FILTER_TYPE_ENCODING_VERSION { + logging::log_warning(format!("{}: Cannot load bloomfltr data type of version {} because it is higher than the loaded module's bloomfltr supported version {}", MODULE_NAME, encver, BLOOM_FILTER_TYPE_ENCODING_VERSION).as_str()); return None; - }; - let Ok(number_of_bits) = raw::load_unsigned(rdb) else { + } + let Ok(num_filters) = raw::load_unsigned(rdb) else { return None; }; - let Ok(number_of_hash_functions) = raw::load_unsigned(rdb) else { + let Ok(expansion) = raw::load_unsigned(rdb) else { return None; }; - let Ok(capacity) = raw::load_unsigned(rdb) else { + let Ok(fp_rate) = raw::load_float(rdb) else { return None; }; - // Only load num_items when it's the last filter - let num_items = if i == num_filters - 1 { - match raw::load_unsigned(rdb) { - Ok(num_items) => num_items, - Err(_) => return None, - } - } else { - capacity + for i in 0..num_filters { + let Ok(bitmap) = raw::load_string_buffer(rdb) else { + return None; + }; + let Ok(number_of_bits) = raw::load_unsigned(rdb) else { + return None; + }; + let Ok(number_of_hash_functions) = raw::load_unsigned(rdb) else { + return None; + }; + let Ok(capacity) = raw::load_unsigned(rdb) else { + return None; + }; + // Only load num_items when it's the last filter + let num_items = if i == num_filters - 1 { + match raw::load_unsigned(rdb) { + Ok(num_items) => num_items, + Err(_) => return None, + } + } else { + capacity + }; + let sip_keys = [ + (FIXED_SIP_KEY_ONE_A, FIXED_SIP_KEY_ONE_B), + (FIXED_SIP_KEY_TWO_A, FIXED_SIP_KEY_TWO_B), + ]; + let filter = BloomFilter::from_existing( + bitmap.as_ref(), + number_of_bits, + number_of_hash_functions as u32, + sip_keys, + num_items as u32, + capacity as u32, + ); + filters.push(filter); + } + let item = BloomFilterType { + expansion: expansion as u32, + fp_rate, + filters, }; - let sip_keys = [ - (FIXED_SIP_KEY_ONE_A, FIXED_SIP_KEY_ONE_B), - (FIXED_SIP_KEY_TWO_A, FIXED_SIP_KEY_TWO_B), - ]; - let filter = BloomFilter::from_existing( - bitmap.as_ref(), - number_of_bits, - number_of_hash_functions as u32, - sip_keys, - num_items as u32, - capacity as u32, - ); - filters.push(filter); + Some(item) } - let item = BloomFilterType { - expansion: expansion as u32, - fp_rate, - filters, - }; - Some(item) } /// Load the auxiliary data outside of the regular keyspace from the RDB file diff --git a/src/lib.rs b/src/lib.rs index 850e81a..bb3a3a7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -80,8 +80,8 @@ valkey_module! { ], configurations: [ i64: [ - ["bloom_max_item_size", &*configs::BLOOM_CAPACITY, configs::BLOOM_CAPACITY_DEFAULT, configs::BLOOM_CAPACITY_MIN as i64, configs::BLOOM_CAPACITY_MAX as i64, ConfigurationFlags::DEFAULT, None], - ["bloom_expansion_rate", &*configs::BLOOM_EXPANSION, configs::BLOOM_EXPANSION_DEFAULT, configs::BLOOM_EXPANSION_MIN as i64, configs::BLOOM_EXPANSION_MAX as i64, ConfigurationFlags::DEFAULT, None], + ["bloom-max-item-size", &*configs::BLOOM_CAPACITY, configs::BLOOM_CAPACITY_DEFAULT, configs::BLOOM_CAPACITY_MIN as i64, configs::BLOOM_CAPACITY_MAX as i64, ConfigurationFlags::DEFAULT, None], + ["bloom-expansion-rate", &*configs::BLOOM_EXPANSION, configs::BLOOM_EXPANSION_DEFAULT, configs::BLOOM_EXPANSION_MIN as i64, configs::BLOOM_EXPANSION_MAX as i64, ConfigurationFlags::DEFAULT, None], ], string: [ ], diff --git a/src/wrapper/bloom_callback.rs b/src/wrapper/bloom_callback.rs index 3aa03a3..89489d5 100644 --- a/src/wrapper/bloom_callback.rs +++ b/src/wrapper/bloom_callback.rs @@ -1,4 +1,5 @@ use crate::bloom; +use crate::bloom::data_type::ValkeyDataType; use crate::bloom::utils::BloomFilterType; use std::os::raw::{c_char, c_int, c_void}; use std::ptr::null_mut; @@ -39,7 +40,7 @@ pub unsafe extern "C" fn bloom_rdb_load( rdb: *mut raw::RedisModuleIO, encver: c_int, ) -> *mut c_void { - if let Some(item) = bloom::data_type::bloom_rdb_load_data_object(rdb, encver) { + if let Some(item) = ::load_from_rdb(rdb, encver) { let bb = Box::new(item); Box::into_raw(bb).cast::() } else {