Skip to content

Commit

Permalink
Merge pull request #12 from deven96/david/ci-cd
Browse files Browse the repository at this point in the history
setup workflow to run rust test
  • Loading branch information
deven96 authored May 15, 2024
2 parents f605e80 + 40439dc commit 2bbf3f0
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 42 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Rust Test

on:
push:
branches: ["main"]
paths:
- ahnlich/**
pull_request:
branches: ["main"]
paths:
- ahnlich/**

env:
CARGO_TERM_COLOR: always

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up Rust cache
uses: Swatinem/rust-cache@v2
with:
workspaces: ahnlich

- name: Set up cargo and rustup tools
run: |
which cargo-nextest || cargo install cargo-nextest
- name: Cache Docker images.
uses: ScribeMD/[email protected]
with:
key: ${{ runner.os }}-cargo-${{ hashFiles('ahnlich/Cargo.lock') }}
- name: Format and Lint
working-directory: ./ahnlich
run: |
make format
make clippy
- name: Run Test
working-directory: ./ahnlich
run: |
make test
20 changes: 10 additions & 10 deletions ahnlich/server/src/algorithm/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,38 +82,38 @@ impl<'a> MaxHeap<'a> {
}

pub(crate) enum AlgorithmHeapType<'a> {
MIN(MinHeap<'a>),
MAX(MaxHeap<'a>),
Min(MinHeap<'a>),
Max(MaxHeap<'a>),
}

impl<'a> AlgorithmHeapType<'a> {
pub(crate) fn push(&mut self, item: SimilarityVector<'a>) {
match self {
Self::MAX(h) => h.push(item),
Self::MIN(h) => h.push(item),
Self::Max(h) => h.push(item),
Self::Min(h) => h.push(item),
}
}
pub(crate) fn pop(&mut self) -> Option<SimilarityVector<'a>> {
match self {
Self::MAX(h) => h.pop(),
Self::MIN(h) => h.pop(),
Self::Max(h) => h.pop(),
Self::Min(h) => h.pop(),
}
}

pub(crate) fn output(&mut self) -> Vec<(&'a StoreKey, f64)> {
match self {
Self::MIN(h) => h.output(),
Self::MAX(h) => h.output(),
Self::Min(h) => h.output(),
Self::Max(h) => h.output(),
}
}
}

impl From<(&Algorithm, NonZeroUsize)> for AlgorithmHeapType<'_> {
fn from((value, capacity): (&Algorithm, NonZeroUsize)) -> Self {
match value {
Algorithm::EuclideanDistance => AlgorithmHeapType::MIN(MinHeap::new(capacity)),
Algorithm::EuclideanDistance => AlgorithmHeapType::Min(MinHeap::new(capacity)),
Algorithm::CosineSimilarity | Algorithm::DotProductSimilarity => {
AlgorithmHeapType::MAX(MaxHeap::new(capacity))
AlgorithmHeapType::Max(MaxHeap::new(capacity))
}
}
}
Expand Down
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
17 changes: 4 additions & 13 deletions ahnlich/server/src/algorithm/similarity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,14 @@ fn cosine_similarity(first: &StoreKey, second: &StoreKey) -> f64 {
//
//

let dot_product = dot_product(&first, &second);
let dot_product = dot_product(first, second);

// 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();

let cosine_sim = dot_product / (mag_first * mag_second);

cosine_sim
dot_product / (mag_first * mag_second)
}

///
Expand Down Expand Up @@ -130,8 +122,7 @@ fn euclidean_distance(first: &StoreKey, second: &StoreKey) -> f64 {
}

// Calculate the square root of the sum of squared differences
let distance = f64::sqrt(sum_of_squared_differences);
distance
f64::sqrt(sum_of_squared_differences)
}

#[cfg(test)]
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
29 changes: 18 additions & 11 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 @@ -103,7 +105,8 @@ impl StoreHandler {
predicates: Vec<MetadataKey>,
) -> Result<(), ServerError> {
let store = self.get(store_name)?;
Ok(store.reindex(predicates.into_iter().collect()))
store.reindex(predicates.into_iter().collect());
Ok(())
}

/// Matches DELKEY - removes keys from a store
Expand Down Expand Up @@ -215,11 +218,15 @@ impl StoreHandler {
dimension: NonZeroUsize,
predicates: Vec<MetadataKey>,
) -> Result<(), ServerError> {
if let Err(_) = self.stores.try_insert(
store_name.clone(),
Arc::new(Store::create(dimension, predicates)),
&self.stores.guard(),
) {
if self
.stores
.try_insert(
store_name.clone(),
Arc::new(Store::create(dimension, predicates)),
&self.stores.guard(),
)
.is_err()
{
return Err(ServerError::StoreAlreadyExists(store_name));
}
Ok(())
Expand Down Expand Up @@ -331,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
2 changes: 2 additions & 0 deletions ahnlich/server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(dead_code)]
#![allow(clippy::size_of_ref)]
mod algorithm;
mod engine;
mod errors;
Expand Down
1 change: 0 additions & 1 deletion ahnlich/types/src/keyval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::metadata::MetadataKey;
use crate::metadata::MetadataValue;
use ndarray::Array1;
use std::collections::HashMap as StdHashMap;
use std::ops::Deref;

/// Name of a Store
#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
Expand Down
1 change: 1 addition & 0 deletions ahnlich/types/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ pub enum Query {
InfoServer,
ListStores,
ListClients,
Ping,
}
2 changes: 2 additions & 0 deletions docs/draft.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@

- `LISTCLIENTS`: Returns a list of clients connected to the server

- `PING`: Test server if the server is reachable

### Clients

#### Language Clients
Expand Down

0 comments on commit 2bbf3f0

Please sign in to comment.