You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Adding optional arg to BF.INSERT to allow users to check if their bloom filter can reach the desired size (#41)
* Adding optional arg to BF.INSERT to allow users to check if their bloom filter can reach the desired size
Signed-off-by: zackcam <zackcam@amazon.com>
* Fixing ATLEASTCAPACITY calculation as well as adding MAXCAPACITY functionality for info
Signed-off-by: zackcam <zackcam@amazon.com>
---------
Signed-off-by: zackcam <zackcam@amazon.com>
.expect("Every BloomObject is expected to have at least one filter")
242
259
.seed()
243
260
}
261
+
/// Return the starting capacity used by the Bloom object. This capacity is held within the first filter
262
+
pubfnstarting_capacity(&self) -> i64{
263
+
self.filters
264
+
.first()
265
+
.expect("Every BloomObject is expected to have at least one filter")
266
+
.capacity()
267
+
}
244
268
245
269
/// Return the expansion of the bloom object.
246
270
pubfnexpansion(&self) -> u32{
@@ -311,8 +335,8 @@ impl BloomObject {
311
335
let new_capacity = match filter.capacity.checked_mul(self.expansion.into()){
312
336
Some(new_capacity) => new_capacity,
313
337
None => {
314
-
// u32:max cannot be reached with 64MB memory usage limit per filter even with a high fp rate (e.g. 0.9).
315
-
returnErr(BloomError::MaxNumScalingFilters);
338
+
// With a 128MB memory limit for a bloom object overall, it is not possible to reach u32:max capacity.
339
+
returnErr(BloomError::BadCapacity);
316
340
}
317
341
};
318
342
// Reject the request, if the operation will result in creation of a filter of size greater than what is allowed.
@@ -366,7 +390,7 @@ impl BloomObject {
366
390
) -> Result<f64,BloomError>{
367
391
match fp_rate * tightening_ratio.powi(num_filters){
368
392
x if x > f64::MIN_POSITIVE => Ok(x),
369
-
_ => Err(BloomError::MaxNumScalingFilters),
393
+
_ => Err(BloomError::FalsePositiveReachesZero),
370
394
}
371
395
}
372
396
@@ -455,6 +479,78 @@ impl BloomObject {
455
479
_ => Err(BloomError::DecodeUnsupportedVersion),
456
480
}
457
481
}
482
+
483
+
/// This method is called from two different bloom commands: BF.INFO and BF.INSERT. The functionality varies slightly on which command it
484
+
/// is called from. When called from BF.INFO, this method is used to find the maximum possible size that the bloom object could scale to
485
+
/// without throwing an error. When called from BF.INSERT, this method is used to determine if it is possible to reach the provided `validate_scale_to`.
486
+
///
487
+
/// # Arguments
488
+
///
489
+
/// * `capacity` - The size of the initial filter in the bloom object.
490
+
/// * `fp_rate` - the false positive rate for the bloom object
491
+
/// * `validate_scale_to` - the capacity we check to see if it can scale to. If this method is called from BF.INFO this is set as -1 as we
492
+
/// want to check the maximum size we could scale up till
493
+
/// * `tightening_ratio` - The tightening ratio of the object
494
+
/// * `expansion` - The expanison rate of the object
495
+
///
496
+
/// # Returns
497
+
/// * i64 - The maximum capacity that can be reached if called from BF.INFO. If called from BF.INSERT the size it reached when it became greater than `validate_scale_to`
498
+
/// * ValkeyError - Can return two different errors:
499
+
/// VALIDATE_SCALE_TO_EXCEEDS_MAX_SIZE: When scaling to the wanted capacity would go over the bloom object memory limit
500
+
/// VALIDATE_SCALE_TO_FALSE_POSITIVE_INVALID: When scaling to the wanted capacity would cause the false positive rate to reach 0
501
+
pubfncalculate_max_scaled_capacity(
502
+
capacity:i64,
503
+
fp_rate:f64,
504
+
validate_scale_to:i64,
505
+
tightening_ratio:f64,
506
+
expansion:u32,
507
+
) -> Result<i64,BloomError>{
508
+
letmut curr_filter_capacity = capacity;
509
+
letmut curr_total_capacity = 0;
510
+
letmut curr_num_filters:u64 = 0;
511
+
letmut filters_memory_usage = 0;
512
+
while curr_total_capacity < validate_scale_to || validate_scale_to == -1{
513
+
// Check to see if scaling to the next filter will cause a degradation in FP to 0
514
+
let curr_fp_rate = matchBloomObject::calculate_fp_rate(
0 commit comments