Skip to content

Commit

Permalink
more cleanup based on clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
Iamdavidonuh committed May 15, 2024
1 parent d35caf1 commit db8c76f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 19 deletions.
2 changes: 1 addition & 1 deletion ahnlich/server/src/algorithm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl<'a> Eq for SimilarityVector<'a> {}

impl PartialOrd for SimilarityVector<'_> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
(self.0).1.partial_cmp(&(other.0).1)
Some(self.cmp(other))
}
}

Expand Down
8 changes: 1 addition & 7 deletions ahnlich/server/src/algorithm/similarity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<f64>().sqrt();

let mag_second = &second
.0
.iter()
.map(|x| x * x)
.map(|x| x)
.sum::<f64>()
.sqrt();
let mag_second = &second.0.iter().map(|x| x * x).sum::<f64>().sqrt();

dot_product / (mag_first * mag_second)
}
Expand Down
9 changes: 3 additions & 6 deletions ahnlich/server/src/engine/predicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();
let pred = PredicateIndex::init(val.clone());
if let Err(existing_predicate) =
Expand All @@ -130,7 +129,6 @@ impl PredicateIndices {
})
})
.flatten()
.into_iter()
.map(|(store_key_id, key, val)| (key, (val, store_key_id)))
.into_group_map();

Expand All @@ -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());
Expand Down Expand Up @@ -256,8 +254,7 @@ impl PredicateIndex {
PredicateOp::NotEquals => pinned
.iter()
.filter(|(key, _)| **key != *value)
.map(|(_, value)| value.pin().iter().cloned().collect::<Vec<_>>())
.flatten()
.flat_map(|(_, value)| value.pin().iter().cloned().collect::<Vec<_>>())
.collect(),
}
}
Expand Down
12 changes: 7 additions & 5 deletions ahnlich/server/src/engine/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<String>();

let hash_string = result.iter().fold(String::new(), |mut acc, byte| {
let _ = write!(acc, "{byte:02x}");
acc
});
Self(hash_string)
}
}
Expand Down Expand Up @@ -336,7 +338,7 @@ impl Store {
};
let res: Vec<(StoreKeyId, (StoreKey, StoreValue))> = new
.into_iter()
.map(|item| check_bounds(item))
.map(check_bounds)
.collect::<Result<_, _>>()?;
let predicate_insert = res
.iter()
Expand Down

0 comments on commit db8c76f

Please sign in to comment.