-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from yatharthmathur/ym_traits_impl
refactor: folder structure and files arranged in proper folder
- Loading branch information
Showing
24 changed files
with
980 additions
and
907 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use std::{ | ||
num::{ParseIntError, TryFromIntError}, | ||
string::FromUtf8Error, | ||
}; | ||
|
||
#[derive(Debug)] | ||
pub enum TypeConversionError { | ||
ParseIntError(ParseIntError), | ||
FromUtf8Error(FromUtf8Error), | ||
TryFromIntError(TryFromIntError), | ||
// Add other type cast error variants as needed | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum ValueError { | ||
TypeConversionImpossible, | ||
TypeConversionError(TypeConversionError), | ||
} |
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,30 @@ | ||
use crate::data_store::errors::ValueError; | ||
|
||
use crate::data_store::{store::KeyValueStore, value_entry::ValueEntry}; | ||
use std::time::{Duration, Instant}; | ||
|
||
impl KeyValueStore { | ||
/// Inserts a Key-Value(in Vec<u8> type) pair in the KeyValueStore | ||
pub fn set_bytes(&mut self, key: String, value: Vec<u8>, ttl: Option<u64>) { | ||
let expiration = Instant::now() + Duration::from_millis(ttl.unwrap_or(self.default_ttl)); | ||
let value_entry = ValueEntry::from_bytes(value, expiration); | ||
self._insert(&key, &value_entry); | ||
} | ||
|
||
/// Gets a Value (in Vec<u8> type) associated to the Key in the KeyValueStore | ||
pub fn get_bytes(&self, key: String) -> Option<Result<Vec<u8>, ValueError>> { | ||
match self._get_or_none_if_expired(&key) { | ||
Some(value_entry) => Some(value_entry.get_value_as_bytes()), | ||
_ => None, | ||
} | ||
} | ||
|
||
/// Removes the Key-Value pair for the given Key in the KeyValueStore | ||
/// and returns the Value (in Vec<u8> type) | ||
pub fn pop_bytes(&mut self, key: String) -> Option<Result<Vec<u8>, ValueError>> { | ||
match self._remove_and_none_if_expired(&key) { | ||
Some(value_entry) => Some(value_entry.get_value_as_bytes()), | ||
_ => None, | ||
} | ||
} | ||
} |
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,2 @@ | ||
mod bytes; | ||
mod tests; |
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,50 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::data_store::store::KeyValueStore; | ||
|
||
#[test] | ||
fn test_set_get_bytes() { | ||
let mut store = KeyValueStore::new(0); | ||
store.set_bytes("ABC".to_string(), "HELLO".as_bytes().to_vec(), Some(5000)); | ||
assert_eq!( | ||
store.get_bytes("ABC".to_string()).unwrap().unwrap(), | ||
"HELLO".as_bytes().to_vec() | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_pop_bytes() { | ||
let mut store = KeyValueStore::new(0); | ||
store.set_bytes("ABC".to_string(), "HELLO".as_bytes().to_vec(), Some(5000)); | ||
assert_eq!( | ||
store.pop_bytes("ABC".to_string()).unwrap().unwrap(), | ||
"HELLO".as_bytes().to_vec() | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_get_set_binary_data() { | ||
#[derive(Serialize, Deserialize)] | ||
struct LocalStruct { | ||
test1: f64, | ||
test2: String, | ||
} | ||
|
||
let local_struct_instance: LocalStruct = LocalStruct { | ||
test1: 3.1415, | ||
test2: "Hey there".to_string(), | ||
}; | ||
let mut store = KeyValueStore::new(0); | ||
let bin_code = bincode::serialize(&local_struct_instance).unwrap(); | ||
store.set_bytes("ABC".to_string(), bin_code, Some(5000)); | ||
|
||
if let Some(vec_val) = store.pop_bytes("ABC".to_string()) { | ||
let new_local_struct: LocalStruct = bincode::deserialize(&vec_val.unwrap()).unwrap(); | ||
let struct_to_compare = LocalStruct { | ||
test1: 3.1415, | ||
test2: "Hey there".to_string(), | ||
}; | ||
assert_eq!(new_local_struct.test1, struct_to_compare.test1); | ||
assert_eq!(new_local_struct.test2, struct_to_compare.test2); | ||
} | ||
} |
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,174 @@ | ||
use crate::data_store::errors::ValueError; | ||
|
||
use crate::data_store::{store::KeyValueStore, value_entry::ValueEntry}; | ||
use std::{ | ||
collections::HashSet, | ||
time::{Duration, Instant}, | ||
}; | ||
|
||
impl KeyValueStore { | ||
/// Inserts a Key-Value(in HashSet<String> type) pair in the KeyValueStore | ||
pub fn set_hset(&mut self, key: String, value: Vec<String>, ttl: Option<u64>) { | ||
let expiration = Instant::now() + Duration::from_millis(ttl.unwrap_or(self.default_ttl)); | ||
let value_entry = ValueEntry::from_set(HashSet::from_iter(value), expiration); | ||
self._insert(&key, &value_entry); | ||
} | ||
|
||
/// Gets a Value (converted to set<String> type) associated to the Key in the KeyValueStore | ||
pub fn get_hset(&self, key: String) -> Option<Result<HashSet<String>, ValueError>> { | ||
match self._get_or_none_if_expired(&key) { | ||
Some(value_entry) => Some(value_entry.get_value_as_hset()), | ||
_ => None, | ||
} | ||
} | ||
|
||
fn _get_mut_hset(&mut self, key: String) -> Option<Result<&mut HashSet<String>, ValueError>> { | ||
match self._get_mut_or_none_if_expired(&key) { | ||
Some(value_entry) => match value_entry.get_value_as_mut_hset() { | ||
Ok(hset) => Some(Ok(hset)), | ||
Err(e) => Some(Err(e)), | ||
}, | ||
None => None, | ||
} | ||
} | ||
|
||
/// Removes the Key-Value pair for the given Key in the KeyValueStore | ||
/// and returns the Value (converted to Vec<String> type) | ||
pub fn pop_hset(&mut self, key: String) -> Option<Result<Vec<String>, ValueError>> { | ||
self.pop_list(key) | ||
} | ||
|
||
/// Adds an item to the set and returns the cardinality of the set. | ||
pub fn hset_add(&mut self, key: String, value: String) -> Option<Result<usize, ValueError>> { | ||
match self._get_mut_hset(key) { | ||
Some(Ok(hset)) => { | ||
hset.insert(value); | ||
Some(Ok(hset.len())) | ||
} | ||
Some(Err(e)) => Some(Err(e)), | ||
None => None, | ||
} | ||
} | ||
|
||
/// Removes an item from the set and returns the cardinality of the set. | ||
pub fn hset_remove(&mut self, key: String, value: String) -> Option<Result<usize, ValueError>> { | ||
match self._get_mut_hset(key) { | ||
Some(Ok(hset)) => { | ||
hset.remove(&value); | ||
Some(Ok(hset.len())) | ||
} | ||
Some(Err(e)) => Some(Err(e)), | ||
None => None, | ||
} | ||
} | ||
|
||
/// Checks if the item exists in the given hset | ||
pub fn hset_contains(&self, key: String, value: String) -> Option<Result<bool, ValueError>> { | ||
match self.get_hset(key) { | ||
Some(Ok(hset)) => Some(Ok(hset.contains(&value))), | ||
Some(Err(e)) => Some(Err(e)), | ||
None => None, | ||
} | ||
} | ||
|
||
/// Get the intersection between two sets in the data store | ||
pub fn hset_intersection( | ||
&self, | ||
key1: String, | ||
key2: String, | ||
) -> Option<Result<Vec<String>, ValueError>> { | ||
let opt_res_h1 = self.get_hset(key1); | ||
let opt_res_h2 = self.get_hset(key2); | ||
|
||
let h1 = match opt_res_h1 { | ||
Some(Ok(hset1)) => hset1.to_owned(), | ||
Some(Err(e)) => { | ||
return Some(Err(e)); | ||
} | ||
None => HashSet::new(), | ||
}; | ||
|
||
let h2 = match opt_res_h2 { | ||
Some(Ok(hset2)) => hset2.to_owned(), | ||
Some(Err(e)) => { | ||
return Some(Err(e)); | ||
} | ||
None => HashSet::new(), | ||
}; | ||
|
||
Some(Ok(Vec::from_iter( | ||
h1.intersection(&h2).map(|item| item.to_owned()), | ||
))) | ||
} | ||
|
||
/// Get the intersection between two sets in the data store | ||
pub fn hset_union( | ||
&self, | ||
key1: String, | ||
key2: String, | ||
) -> Option<Result<Vec<String>, ValueError>> { | ||
let opt_res_h1 = self.get_hset(key1); | ||
let opt_res_h2 = self.get_hset(key2); | ||
|
||
let h1 = match opt_res_h1 { | ||
Some(Ok(hset1)) => hset1.to_owned(), | ||
Some(Err(e)) => { | ||
return Some(Err(e)); | ||
} | ||
None => HashSet::new(), | ||
}; | ||
|
||
let h2 = match opt_res_h2 { | ||
Some(Ok(hset2)) => hset2.to_owned(), | ||
Some(Err(e)) => { | ||
return Some(Err(e)); | ||
} | ||
None => HashSet::new(), | ||
}; | ||
|
||
Some(Ok(Vec::from_iter( | ||
h1.union(&h2).map(|item| item.to_owned()), | ||
))) | ||
} | ||
|
||
/// Get the difference between two sets in the data store | ||
pub fn hset_difference( | ||
&self, | ||
key1: String, | ||
key2: String, | ||
) -> Option<Result<Vec<String>, ValueError>> { | ||
let opt_res_h1 = self.get_hset(key1); | ||
let opt_res_h2 = self.get_hset(key2); | ||
|
||
let h1 = match opt_res_h1 { | ||
Some(Ok(hset1)) => hset1.to_owned(), | ||
Some(Err(e)) => { | ||
return Some(Err(e)); | ||
} | ||
None => HashSet::new(), | ||
}; | ||
|
||
let h2 = match opt_res_h2 { | ||
Some(Ok(hset2)) => hset2.to_owned(), | ||
Some(Err(e)) => { | ||
return Some(Err(e)); | ||
} | ||
None => HashSet::new(), | ||
}; | ||
|
||
Some(Ok(Vec::from_iter( | ||
h1.difference(&h2).map(|item| item.to_owned()), | ||
))) | ||
} | ||
|
||
/// cardinality of the set | ||
pub fn hset_size(&self, key: String) -> Option<Result<usize, ValueError>> { | ||
match self.get_hset(key) { | ||
Some(Ok(hset)) => Some(Ok(hset.len())), | ||
Some(Err(e)) => Some(Err(e)), | ||
None => None, | ||
} | ||
} | ||
} | ||
|
||
pub mod hsets_tests {} |
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,2 @@ | ||
mod hsets; | ||
mod tests; |
Oops, something went wrong.