diff --git a/ahnlich/server/src/algorithm/mod.rs b/ahnlich/server/src/algorithm/mod.rs index c61d6cbd..e8526f27 100644 --- a/ahnlich/server/src/algorithm/mod.rs +++ b/ahnlich/server/src/algorithm/mod.rs @@ -32,7 +32,7 @@ impl<'a> Eq for SimilarityVector<'a> {} impl PartialOrd for SimilarityVector<'_> { fn partial_cmp(&self, other: &Self) -> Option { - (self.0).1.partial_cmp(&(other.0).1) + Some(self.cmp(other)) } } diff --git a/ahnlich/server/src/algorithm/similarity.rs b/ahnlich/server/src/algorithm/similarity.rs index 37b20458..3b2c0d34 100644 --- a/ahnlich/server/src/algorithm/similarity.rs +++ b/ahnlich/server/src/algorithm/similarity.rs @@ -76,13 +76,7 @@ fn cosine_similarity(first: &StoreKey, second: &StoreKey) -> f64 { // the magnitude can be calculated using the arr.norm method. let mag_first = &first.0.iter().map(|x| x * x).sum::().sqrt(); - let mag_second = &second - .0 - .iter() - .map(|x| x * x) - .map(|x| x) - .sum::() - .sqrt(); + let mag_second = &second.0.iter().map(|x| x * x).sum::().sqrt(); dot_product / (mag_first * mag_second) } diff --git a/ahnlich/server/src/engine/predicate.rs b/ahnlich/server/src/engine/predicate.rs index 5129bf2a..c6bc935f 100644 --- a/ahnlich/server/src/engine/predicate.rs +++ b/ahnlich/server/src/engine/predicate.rs @@ -98,13 +98,12 @@ impl PredicateIndices { for new_predicate in new_predicates { let val = new_values .iter() - .map(|(store_key_id, store_value)| { + .flat_map(|(store_key_id, store_value)| { store_value .iter() .filter(|(key, _)| **key == new_predicate) .map(|(_, val)| (val.clone(), store_key_id.clone())) }) - .flatten() .collect::>(); let pred = PredicateIndex::init(val.clone()); if let Err(existing_predicate) = @@ -130,7 +129,6 @@ impl PredicateIndices { }) }) .flatten() - .into_iter() .map(|(store_key_id, key, val)| (key, (val, store_key_id))) .into_group_map(); @@ -156,7 +154,7 @@ impl PredicateIndices { if let Some(predicate) = predicate_values.get(key) { // retrieve the precise predicate if it exists and check against it return Ok(predicate.matches(op, value)); - } else if pinned_keys.contains(&key) { + } else if pinned_keys.contains(key) { // predicate does not exist because perhaps we have not been passing a value // but it is within allowed so this isn't an error return Ok(StdHashSet::new()); @@ -256,8 +254,7 @@ impl PredicateIndex { PredicateOp::NotEquals => pinned .iter() .filter(|(key, _)| **key != *value) - .map(|(_, value)| value.pin().iter().cloned().collect::>()) - .flatten() + .flat_map(|(_, value)| value.pin().iter().cloned().collect::>()) .collect(), } } diff --git a/ahnlich/server/src/engine/store.rs b/ahnlich/server/src/engine/store.rs index 5913f633..f5dc28ad 100644 --- a/ahnlich/server/src/engine/store.rs +++ b/ahnlich/server/src/engine/store.rs @@ -7,6 +7,7 @@ use sha2::Digest; use sha2::Sha256; use std::collections::HashMap as StdHashMap; use std::collections::HashSet as StdHashSet; +use std::fmt::Write; use std::mem::size_of_val; use std::num::NonZeroUsize; use std::sync::Arc; @@ -49,10 +50,11 @@ impl From<&StoreKey> for StoreKeyId { } let result = hasher.finalize(); // Convert the hash bytes to a hexadecimal string - let hash_string = result - .iter() - .map(|byte| format!("{:02x}", byte)) - .collect::(); + + let hash_string = result.iter().fold(String::new(), |mut acc, byte| { + let _ = write!(acc, "{byte:02x}"); + acc + }); Self(hash_string) } } @@ -336,7 +338,7 @@ impl Store { }; let res: Vec<(StoreKeyId, (StoreKey, StoreValue))> = new .into_iter() - .map(|item| check_bounds(item)) + .map(check_bounds) .collect::>()?; let predicate_insert = res .iter()