-
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.
refactor: folder structure and files arranged in proper folder
- Loading branch information
Showing
11 changed files
with
500 additions
and
445 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 super::super::{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,172 @@ | ||
use crate::data_store::errors::ValueError; | ||
|
||
use super::super::{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, | ||
} | ||
} | ||
} |
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,68 @@ | ||
use crate::data_store::{ | ||
errors::{TypeConversionError, ValueError}, | ||
types::ValueType, | ||
}; | ||
|
||
use super::super::{store::KeyValueStore, value_entry::ValueEntry}; | ||
use std::time::{Duration, Instant}; | ||
|
||
impl KeyValueStore { | ||
/// Inserts a Key-Value(in i64 type) pair in the KeyValueStore | ||
pub fn set_i64(&mut self, key: String, value: i64, ttl: Option<u64>) { | ||
let expiration = Instant::now() + Duration::from_millis(ttl.unwrap_or(self.default_ttl)); | ||
let value_entry = ValueEntry::from_i64(value, expiration); | ||
self._insert(&key, &value_entry); | ||
} | ||
|
||
/// Gets a Value (converted to String type) associated to the Key in the KeyValueStore | ||
pub fn get_i64(&self, key: String) -> Option<Result<i64, ValueError>> { | ||
match self._get_or_none_if_expired(&key) { | ||
Some(value_entry) => Some(value_entry.get_value_as_i64()), | ||
_ => None, | ||
} | ||
} | ||
|
||
/// Removes the Key-Value pair for the given Key in the KeyValueStore | ||
/// and returns the Value (converted to i64 type) | ||
pub fn pop_i64(&mut self, key: String) -> Option<Result<i64, ValueError>> { | ||
match self._remove_and_none_if_expired(&key) { | ||
Some(value_entry) => Some(value_entry.get_value_as_i64()), | ||
_ => None, | ||
} | ||
} | ||
|
||
fn _add(&mut self, key: String, value: i64) -> Option<Result<i64, ValueError>> { | ||
if let Some(value_entry) = self._get_mut_or_none_if_expired(&key) { | ||
match value_entry.get_value_as_i64() { | ||
Ok(old_value) => { | ||
let updated_integer_value = old_value + value; | ||
value_entry.value = ValueType::Integer64(updated_integer_value); | ||
Some(Ok(updated_integer_value)) | ||
} | ||
Err(e) => Some(Err(e)), | ||
} | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
/// decrement an existing value associated to key by a certain number. | ||
pub fn decr(&mut self, key: String, by: Option<u64>) -> Option<Result<i64, ValueError>> { | ||
match i64::try_from(by.unwrap_or(1)) { | ||
Ok(value) => self._add(key, -value), | ||
Err(e) => Some(Err(ValueError::TypeConversionError( | ||
TypeConversionError::TryFromIntError(e), | ||
))), | ||
} | ||
} | ||
|
||
/// increment an existing value associated to a key by a certain number. | ||
pub fn incr(&mut self, key: String, by: Option<u64>) -> Option<Result<i64, ValueError>> { | ||
match i64::try_from(by.unwrap_or(1)) { | ||
Ok(value) => self._add(key, value), | ||
Err(e) => Some(Err(ValueError::TypeConversionError( | ||
TypeConversionError::TryFromIntError(e), | ||
))), | ||
} | ||
} | ||
} |
Oops, something went wrong.