Skip to content

Commit

Permalink
Switched to some iterators in PredicateIndices for #26
Browse files Browse the repository at this point in the history
  • Loading branch information
deven96 committed Jun 6, 2024
1 parent 93a7aa2 commit 4315a47
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 20 deletions.
8 changes: 4 additions & 4 deletions ahnlich/db/src/algorithm/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ impl<'a> MinHeap<'a> {
}

pub(crate) fn output(&mut self) -> Vec<(&'a StoreKey, f64)> {
let mut result: Vec<_> = vec![];
let mut result: Vec<_> = Vec::with_capacity(self.max_capacity.get());

loop {
match self.pop() {
Some(value) if result.len() < self.max_capacity.into() => {
Some(value) if result.len() < self.max_capacity.get() => {
let vector_sim = value.0;
result.push((vector_sim.0, vector_sim.1));
}
Expand Down Expand Up @@ -66,11 +66,11 @@ impl<'a> MaxHeap<'a> {
}

fn output(&mut self) -> Vec<(&'a StoreKey, f64)> {
let mut result: Vec<_> = vec![];
let mut result: Vec<_> = Vec::with_capacity(self.max_capacity.get());

loop {
match self.heap.pop() {
Some(value) if result.len() < self.max_capacity.into() => {
Some(value) if result.len() < self.max_capacity.get() => {
let vector_sim = value.0;
result.push((vector_sim.0, vector_sim.1));
}
Expand Down
17 changes: 7 additions & 10 deletions ahnlich/db/src/engine/predicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,9 @@ impl PredicateIndices {
error_if_not_exists,
predicates
.iter()
.filter(|a| !pinned_keys.contains(a))
.cloned()
.collect::<Vec<_>>()
.pop(),
.find(|a| !pinned_keys.contains(a))
) {
return Err(ServerError::PredicateNotFound(non_existing_index));
return Err(ServerError::PredicateNotFound(non_existing_index.clone()));
}
let mut deleted = 0;
for predicate in predicates {
Expand All @@ -111,13 +108,13 @@ impl PredicateIndices {
let pinned_inner = self.inner.pin();
// `insert` implicity adds it to allowed_predicates which is what lets us to be able to
// search again
let new_predicates: StdHashSet<_> = predicates
let mut new_predicates = predicates
.into_iter()
.filter(|pred| pinned_keys.insert(pred.clone()))
.unique()
.collect();
.peekable();
// Only update for new predicates
if let Some(new_values) = (!new_predicates.is_empty())
if let Some(new_values) = (new_predicates.peek().is_some())
.then_some(refresh_with_values)
.flatten()
{
Expand All @@ -133,7 +130,7 @@ impl PredicateIndices {
.collect::<Vec<_>>();
let pred = PredicateIndex::init(val.clone());
if let Err(existing_predicate) =
pinned_inner.try_insert(new_predicate.clone(), pred)
pinned_inner.try_insert(new_predicate, pred)
{
existing_predicate.current.add(val)
}
Expand Down Expand Up @@ -252,7 +249,7 @@ impl PredicateIndex {
// was not previously there as it has been inserted on a different thread
let new_hashset = ConcurrentHashSet::new();
new_hashset.insert(store_key_id.clone(), &new_hashset.guard());
if let Err(error_current) = pinned.try_insert(predicate_value.clone(), new_hashset)
if let Err(error_current) = pinned.try_insert(predicate_value, new_hashset)
{
error_current
.current
Expand Down
8 changes: 4 additions & 4 deletions ahnlich/db/src/engine/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl StoreHandler {
predicates: Vec<MetadataKey>,
) -> Result<usize, ServerError> {
let store = self.get(store_name)?;
Ok(store.create_index(predicates.into_iter().collect()))
Ok(store.create_index(predicates))
}

/// Matches DELKEY - removes keys from a store
Expand Down Expand Up @@ -164,7 +164,7 @@ impl StoreHandler {
.flat_map(|(store_key, similarity)| {
keys_to_value_map
.remove(&StoreKeyId::from(store_key))
.map(|value| (store_key.clone(), value.clone(), Similarity(similarity)))
.map(|value| (store_key.to_owned(), value.clone(), Similarity(similarity)))
})
.collect())
}
Expand Down Expand Up @@ -422,9 +422,9 @@ impl Store {
}

#[tracing::instrument(skip(self))]
fn create_index(&self, requested_predicates: StdHashSet<MetadataKey>) -> usize {
fn create_index(&self, requested_predicates: Vec<MetadataKey>) -> usize {
let current_predicates = self.predicate_indices.current_predicates();
let new_predicates: Vec<_> = requested_predicates
let new_predicates: Vec<_> = StdHashSet::from_iter(requested_predicates)
.difference(&current_predicates)
.cloned()
.collect();
Expand Down
2 changes: 1 addition & 1 deletion ahnlich/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl ServerTask {
let mut data = Vec::new();
if data.try_reserve(data_length as usize).is_err() {
tracing::error!("{}", self.prefix_log(format!("Failed to reserve buffer of length {data_length}")));
continue
break
};
data.resize(data_length as usize, 0u8);
self.reader.read_exact(&mut data).await?;
Expand Down
2 changes: 1 addition & 1 deletion ahnlich/types/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::Serialize;

pub static VERSION: Lazy<Version> = Lazy::new(|| {
let version_string: &str = env!("CARGO_PKG_VERSION");
match version_string.split(".").collect::<Vec<_>>()[..] {
match version_string.split('.').collect::<Vec<_>>()[..] {
[major, minor, patch] => Some(Version {
major: major
.parse()
Expand Down

0 comments on commit 4315a47

Please sign in to comment.