Skip to content
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

Updating _ to -in lib.rs. Also updating loading from rdb to use a trait instead #14

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 55 additions & 52 deletions src/bloom/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BloomFilterType> {
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<BloomFilterType>;
}

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<BloomFilterType> {
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
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
],
Expand Down
3 changes: 2 additions & 1 deletion src/wrapper/bloom_callback.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) = <BloomFilterType as ValkeyDataType>::load_from_rdb(rdb, encver) {
let bb = Box::new(item);
Box::into_raw(bb).cast::<libc::c_void>()
} else {
Expand Down
Loading