Skip to content
Open
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
5 changes: 3 additions & 2 deletions src/cms/cms_command_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ pub fn cms_increment_by(ctx: &Context, args: Vec<ValkeyString>) -> ValkeyResult
}

let mut i = 2;
let mut pairs: Vec<(&[u8], u64)> = Vec::new();
let pairs_count = args_left / 2;
let mut pairs: Vec<(&[u8], u64)> = Vec::with_capacity(pairs_count);
while i < args_count {
let k = args[i].as_slice();
let v = args[i + 1]
Expand All @@ -195,7 +196,7 @@ pub fn cms_increment_by(ctx: &Context, args: Vec<ValkeyString>) -> ValkeyResult
Err(_) => return Err(ValkeyError::WrongType),
};

let mut results = Vec::new();
let mut results = Vec::with_capacity(pairs_count);
match value {
None => Err(ValkeyError::nonexistent_key()),
Some(v) => {
Expand Down
8 changes: 6 additions & 2 deletions src/cms/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub struct CMSObject {

impl CMSObject {
//TODO: Are there any MAX ranges that we should have for configuration?
/// Create a new CMSObject based on the size requirements given.
pub fn new_by_dimension(width: u64, depth: u64) -> Result<CMSObject, CMSError> {
if width < 1 {
return Err(CMSError::InvalidWidth);
Expand All @@ -61,8 +62,8 @@ impl CMSObject {
Ok(obj)
}

//Error_tolerance is max variance of the count
// probability is the false positive rate
/// Create a new CMSObject based on the error tolerance, max variance of the count,
/// and probabiilty, probability of false positive rates, the caller is okay with.
pub fn new_by_probability(
error_tolerance: f64,
probability: f64,
Expand All @@ -88,11 +89,14 @@ impl CMSObject {
self.cms.sketch.total_count()
}

/// Increases the count for the item given by the amount of the increment.
/// Note that this currently has to call estimate to get the count back for that item.
pub fn increment_by(&mut self, item: &[u8], increment: u64) -> u64 {
self.cms.increment_item(item, increment);
self.cms.estimate_item(item)
}

/// Gives back the estimated count, or frequency, for the given item.
pub fn estimate(&self, item: &[u8]) -> u64 {
self.cms.estimate_item(item)
}
Expand Down
Loading