-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Implementing BinCodeSerAndDeser for queries and responses
- Loading branch information
Showing
10 changed files
with
181 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,21 @@ | ||
use thiserror::Error; | ||
use types::keyval::StoreName; | ||
use types::metadata::MetadataKey; | ||
|
||
/// TODO: Move to shared rust types so library can deserialize it from the TCP response | ||
#[derive(Debug, Eq, PartialEq, PartialOrd, Ord)] | ||
#[derive(Error, Debug, Eq, PartialEq, PartialOrd, Ord)] | ||
pub enum ServerError { | ||
#[error("Predicate {0} not found in store, attempt reindexing with predicate")] | ||
PredicateNotFound(MetadataKey), | ||
#[error("Store {0} not found")] | ||
StoreNotFound(StoreName), | ||
#[error("Store {0} already exists")] | ||
StoreAlreadyExists(StoreName), | ||
#[error("Store dimension is [{store_dimension}], input dimension of [{input_dimension}] was specified")] | ||
StoreDimensionMismatch { | ||
store_dimension: usize, | ||
input_dimension: usize, | ||
}, | ||
#[error("Could not deserialize query, error is {0}")] | ||
QueryDeserializeError(String), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use bincode::config::DefaultOptions; | ||
use bincode::config::Options; | ||
use serde::Deserialize; | ||
use serde::Serialize; | ||
|
||
pub const LENGTH_HEADER_SIZE: usize = 8; | ||
|
||
/// - Length encoding must use fixed int and not var int | ||
/// - Endianess must be Big Endian. | ||
/// - First 8 bytes must contain length of the entire vec of response or queries | ||
/// | ||
/// Used to serialize and deserialize queries and responses into bincode | ||
pub trait BinCodeSerAndDeser<'a> | ||
where | ||
Self: Serialize + Deserialize<'a>, | ||
{ | ||
fn serialize(&self) -> Result<Vec<u8>, bincode::Error> { | ||
let config = DefaultOptions::new() | ||
.with_fixint_encoding() | ||
.with_big_endian(); | ||
let serialized_data = config.serialize(self)?; | ||
let data_length = serialized_data.len() as u64; | ||
// serialization appends the length buffer to be read first | ||
let mut buffer = Vec::with_capacity(LENGTH_HEADER_SIZE + serialized_data.len()); | ||
buffer.extend(&data_length.to_be_bytes()); | ||
buffer.extend(&serialized_data); | ||
Ok(buffer) | ||
} | ||
|
||
fn deserialize(has_length_header: bool, bytes: &'a [u8]) -> Result<Self, bincode::Error> { | ||
let config = DefaultOptions::new() | ||
.with_fixint_encoding() | ||
.with_big_endian(); | ||
if has_length_header { | ||
return config.deserialize(&bytes[LENGTH_HEADER_SIZE..]); | ||
} | ||
config.deserialize(bytes) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,7 @@ | ||
pub mod bincode; | ||
pub mod keyval; | ||
pub mod metadata; | ||
pub mod predicate; | ||
pub mod query; | ||
pub mod server; | ||
pub mod similarity; | ||
|
||
pub fn add(left: usize, right: usize) -> usize { | ||
left + right | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn it_works() { | ||
let result = add(2, 2); | ||
assert_eq!(result, 4); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use crate::bincode::BinCodeSerAndDeser; | ||
use serde::Deserialize; | ||
use serde::Serialize; | ||
use std::collections::HashSet; | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] | ||
pub enum ServerResponse { | ||
// Unit variant for no action | ||
Unit, | ||
Pong, | ||
// List of connected clients. Potentially outdated at the point of read | ||
ClientList(HashSet<ConnectedClient>), | ||
// TODO: Define return types for queries, e.t.c | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)] | ||
pub struct ConnectedClient { | ||
pub address: String, | ||
} | ||
|
||
// ServerResult: Given that an array of queries are sent in, we expect that an array of responses | ||
// be returned each being a potential error | ||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] | ||
pub struct ServerResult { | ||
results: Vec<Result<ServerResponse, String>>, | ||
} | ||
|
||
impl BinCodeSerAndDeser<'_> for ServerResult {} | ||
|
||
impl ServerResult { | ||
pub fn with_capacity(len: usize) -> Self { | ||
Self { | ||
results: Vec::with_capacity(len), | ||
} | ||
} | ||
|
||
pub fn push(&mut self, entry: Result<ServerResponse, String>) { | ||
self.results.push(entry) | ||
} | ||
} |