diff --git a/src/cms/cms_command_handler.rs b/src/cms/cms_command_handler.rs index ccbf39f0..c87e90de 100644 --- a/src/cms/cms_command_handler.rs +++ b/src/cms/cms_command_handler.rs @@ -179,7 +179,8 @@ pub fn cms_increment_by(ctx: &Context, args: Vec) -> 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] @@ -195,7 +196,7 @@ pub fn cms_increment_by(ctx: &Context, args: Vec) -> 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) => { diff --git a/src/cms/utils.rs b/src/cms/utils.rs index 37dc73bd..2e7a5fc7 100644 --- a/src/cms/utils.rs +++ b/src/cms/utils.rs @@ -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 { if width < 1 { return Err(CMSError::InvalidWidth); @@ -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, @@ -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) }