diff options
| author | zy <[email protected]> | 2023-09-25 08:21:12 +0000 |
|---|---|---|
| committer | zy <[email protected]> | 2023-09-25 08:21:12 +0000 |
| commit | 28b32e79596d479ca3c1f9fa94de72e4c0d99913 (patch) | |
| tree | d367656d218bff5a9f6015e74639361b70945586 | |
| parent | 30c56a0e00919124c4606eda0bb24c78f6d3ebb0 (diff) | |
Revert "fix ExpiryBloomFilter::check bug"
Push error from binding-rs-dablooms branch
This reverts commit 30c56a0e00919124c4606eda0bb24c78f6d3ebb0.
| -rw-r--r-- | bindings/rs-dablooms/src/dablooms.rs | 58 |
1 files changed, 31 insertions, 27 deletions
diff --git a/bindings/rs-dablooms/src/dablooms.rs b/bindings/rs-dablooms/src/dablooms.rs index a920724..d6262de 100644 --- a/bindings/rs-dablooms/src/dablooms.rs +++ b/bindings/rs-dablooms/src/dablooms.rs @@ -2,12 +2,9 @@ use libc::time_t; use crate::dablooms_bind::*; use std::ffi::{CStr, CString}; -use std::os::raw::{c_int, c_uint}; +use std::os::raw::{c_char, c_int, c_uint}; use std::time::{SystemTime, UNIX_EPOCH}; -// String form rust not contain '\0' in the end, that may cause some problem -// - pub struct CountingBloomFilter { bloom: *mut counting_bloom_t, } @@ -38,7 +35,7 @@ impl CountingBloomFilter { } pub fn check(&self, key: &[u8]) -> bool { - unsafe { counting_bloom_check(self.bloom, key.as_ptr() as *const i8, key.len()) > 0 } + unsafe { counting_bloom_check(self.bloom, key.as_ptr() as *const i8, key.len()) != 0 } } } @@ -83,7 +80,7 @@ impl ScalingBloomFilter { } pub fn check(&self, key: &[u8], id: u64) -> bool { - unsafe { scaling_bloom_check(self.bloom, key.as_ptr() as *const i8, key.len()) > 0 } + unsafe { scaling_bloom_check(self.bloom, key.as_ptr() as *const i8, key.len()) != 0 } } pub fn flush(&mut self) -> Result<(), ()> { @@ -123,6 +120,23 @@ pub struct ExpiryBloomFilter { } impl ExpiryBloomFilter { + // pub fn new(capacity: u32, error_rate: f64, expiry_time: i32) -> Result<Self, String> { + // let cur_time = get_current_time(); + // let handle = unsafe { + // expiry_dablooms_init( + // capacity as c_uint, + // error_rate, + // cur_time.unwrap(), + // expiry_time as c_int, + // ) + // }; + // if handle.is_null() { + // Err("Failed to create Expiry Bloom Filter".to_string()) + // } else { + // Ok(ExpiryBloomFilter { handle }) + // } + // } + pub fn new(capacity: u32, error_rate: f64, expiry_time: i32) -> Self { let cur_time = get_current_time(); let handle = unsafe { @@ -150,23 +164,19 @@ impl ExpiryBloomFilter { if res == 0 { Ok(()) } else { - let err_cstr = unsafe { expiry_dablooms_errno_trans(res) }; - let err_msg = unsafe { CStr::from_ptr(err_cstr).to_string_lossy().into_owned() }; - Err(err_msg) + let errno = unsafe { expiry_dablooms_errno_trans(res) }; + let errmsg = unsafe { CStr::from_ptr(errno).to_string_lossy().into_owned() }; + Err(errmsg) } } pub fn check(&self, key: &[u8]) -> bool { - // let key_cstr = CString::new(key).unwrap(); - let cur_time = get_current_time(); - unsafe { - expiry_dablooms_search( - self.handle, - key.as_ptr() as *const i8, - key.len(), - cur_time.unwrap(), - ) > 0 - } + let key_cstr = CString::new(key).unwrap(); + let cur_time = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_secs() as i64; + unsafe { expiry_dablooms_search(self.handle, key_cstr.as_ptr(), key.len(), cur_time) == 0 } } pub fn count(&self) -> i32 { @@ -233,7 +243,7 @@ mod tests { fn test_expiry_dablooms_filter() { let capacity: u32 = 100; let error_rate: f64 = 0.05; - let expiry_secs: i32 = 3; // expiry time 1s + let expiry_secs: i32 = 1; // expiry time 1s let key1 = b"aaa"; let key2 = b"bbb"; @@ -243,16 +253,10 @@ mod tests { bloom.add(key1).unwrap(); assert!(bloom.count() == _i); } - assert!(bloom.check(key1)); unsafe { - sleep(2); // sleep 2 sec | all key1's value not expired - } - assert!(bloom.check(key1)); - unsafe { - sleep(2); // sleep 4 sec | all key1's value expired + sleep(4); // sleep 4 sec | all key1's value expired } - assert!(!bloom.check(key1)); for _i in 1..7 { bloom.add(key2).unwrap(); |
