Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(MANAGER): added store manager and inf key support #40

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
mod data_store;
use data_store::store::KeyValueStore;
mod managers;
mod stores;
use managers::manager::RusticManager;
use stores::store::KeyValueStore;

fn main() {
println!("Hello, rustics!");
let _store = KeyValueStore::new(0);
let mut manager = RusticManager::new();
manager.create_store("new_store_0".to_owned(), Some(5000));
let store = manager.get_store_mut("new_store_0".to_owned()).unwrap();
store.set_i64("key".to_owned(), 500, None);
}
50 changes: 50 additions & 0 deletions src/managers/manager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::collections::HashMap;

use crate::stores::store::KeyValueStore;

pub struct RusticManager {
_stores_map: HashMap<String, KeyValueStore>,
}

impl RusticManager {
pub fn new() -> RusticManager {
return RusticManager {
_stores_map: HashMap::new(),
};
}

/// Creates a data store and returns a mutable reference to it.
pub fn create_store(&mut self, name: String, default_ttl: Option<u64>) -> &mut KeyValueStore {
if self._stores_map.contains_key(&name) {
panic!("Store with this name already exists.");
}
let new_kv_store: KeyValueStore = KeyValueStore::new(name.to_owned(), default_ttl);
self._stores_map.insert(name.to_owned(), new_kv_store);
self._stores_map.get_mut(&name).unwrap()
}

/// Removes the data store and returns True or False based on if it existed.
pub fn remove_store(&mut self, name: String) -> bool {
if self._stores_map.contains_key(&name) {
self._stores_map.remove(&name);
true
} else {
false
}
}

/// Get optional mutable reference to data store
pub fn get_store_mut(&mut self, name: String) -> Option<&mut KeyValueStore> {
self._stores_map.get_mut(&name)
}

/// Get optional immutable reference to data store
pub fn get_store(&self, name: String) -> Option<&KeyValueStore> {
self._stores_map.get(&name)
}

/// List names of all the stores
pub fn list_store_names(&self) -> Vec<&String> {
self._stores_map.keys().collect()
}
}
2 changes: 2 additions & 0 deletions src/managers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod manager;
mod tests;
70 changes: 70 additions & 0 deletions src/managers/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use super::manager::RusticManager;

#[test]
fn test_create_store() {
let mut manager = RusticManager::new();
// create a store with infinite TTL
let store = manager.create_store("store0".to_owned(), None);
store.set_i64("key".to_owned(), 5, None);
assert_eq!(store.get_i64("key".to_owned()).unwrap().unwrap(), 5);
}

#[test]
fn test_get_store() {
let mut manager = RusticManager::new();
// create a store with infinite TTL
manager.create_store("store0".to_owned(), None);

let mut_store = manager.get_store_mut("store0".to_owned()).unwrap();
mut_store.set_i64("key".to_owned(), 5, None);

let immut_store = manager.get_store("store0".to_owned()).unwrap();
assert_eq!(immut_store.get_i64("key".to_owned()).unwrap().unwrap(), 5);
}

#[test]
fn test_remove_store() {
let mut manager = RusticManager::new();
manager.create_store("store0".to_owned(), None);

let store = manager.get_store_mut("store0".to_owned()).unwrap();
store.set_i64("key".to_owned(), 5, None);
assert_eq!(store.get_i64("key".to_owned()).unwrap().unwrap(), 5);

manager.remove_store("store0".to_owned());
let store = manager.get_store_mut("store0".to_owned());
assert!(store.is_none());
}

#[test]
#[should_panic]
fn test_duplicate_store_names() {
let mut manager = RusticManager::new();
// create a store with infinite TTL
manager.create_store("store0".to_owned(), None);
manager.create_store("store0".to_owned(), None);
}

#[test]
fn test_list_store_names() {
let mut manager = RusticManager::new();
// create a store with infinite TTL
manager.create_store("store0".to_owned(), None);
manager.create_store("store1".to_owned(), None);
manager.create_store("store2".to_owned(), None);

let mut expected_keys: Vec<String> = manager
.list_store_names()
.iter()
.map(|key| key.to_string())
.collect();
expected_keys.sort();
assert_eq!(
expected_keys,
vec![
"store0".to_owned(),
"store1".to_owned(),
"store2".to_owned()
]
)
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::data_store::{errors::ValueError, store::KeyValueStore, value_entry::ValueEntry};
use std::time::{Duration, Instant};
use crate::stores::{errors::ValueError, store::KeyValueStore, value_entry::ValueEntry};

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 expiration = self._get_expiration_instant(ttl);
let value_entry = ValueEntry::from_bytes(value, expiration);
self._insert(&key, &value_entry);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use serde::{Deserialize, Serialize};

use crate::data_store::store::KeyValueStore;
use crate::stores::store::KeyValueStore;

#[test]
fn test_set_get_bytes() {
let mut store = KeyValueStore::new(0);
let mut store = KeyValueStore::new("new_store".to_owned(), None);
store.set_bytes("ABC".to_string(), "HELLO".as_bytes().to_vec(), Some(5000));
assert_eq!(
store.get_bytes("ABC".to_string()).unwrap().unwrap(),
Expand All @@ -14,7 +14,7 @@ fn test_set_get_bytes() {

#[test]
fn test_pop_bytes() {
let mut store = KeyValueStore::new(0);
let mut store = KeyValueStore::new("new_store".to_owned(), None);
store.set_bytes("ABC".to_string(), "HELLO".as_bytes().to_vec(), Some(5000));
assert_eq!(
store.pop_bytes("ABC".to_string()).unwrap().unwrap(),
Expand All @@ -34,7 +34,7 @@ fn test_get_set_binary_data() {
test1: 3.1415,
test2: "Hey there".to_string(),
};
let mut store = KeyValueStore::new(0);
let mut store = KeyValueStore::new("new_store".to_owned(), None);
let bin_code = bincode::serialize(&local_struct_instance).unwrap();
store.set_bytes("ABC".to_string(), bin_code, Some(5000));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use crate::data_store::{errors::ValueError, store::KeyValueStore, value_entry::ValueEntry};
use std::{
collections::HashMap,
time::{Duration, Instant},
};
use crate::stores::{errors::ValueError, store::KeyValueStore, value_entry::ValueEntry};
use std::collections::HashMap;

impl KeyValueStore {
/// Inserts a Key-Value(in HashMap<(String, String)> type) pair in the KeyValueStore
pub fn set_hmap(&mut self, key: String, value: Vec<(String, String)>, ttl: Option<u64>) {
let expiration = Instant::now() + Duration::from_millis(ttl.unwrap_or(self.default_ttl));
let expiration = self._get_expiration_instant(ttl);
let value_entry = ValueEntry::from_hashmap(HashMap::from_iter(value), expiration);
self._insert(&key, &value_entry);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::data_store::store::KeyValueStore;
use crate::stores::store::KeyValueStore;

#[test]
fn test_insert_get_hmap() {
let mut store = KeyValueStore::new(5000);
let mut store = KeyValueStore::new("new_store".to_owned(), None);
store.set_hmap("ABC".to_string(), vec![], None);

assert_eq!(
Expand Down Expand Up @@ -38,7 +38,7 @@ fn test_insert_get_hmap() {

#[test]
fn test_hmap_keys_values_items() {
let mut store = KeyValueStore::new(5000);
let mut store = KeyValueStore::new("new_store".to_owned(), None);
store.set_hmap("ABC".to_string(), vec![], None);

assert_eq!(
Expand Down Expand Up @@ -100,7 +100,7 @@ fn test_hmap_keys_values_items() {

#[test]
fn test_remove_hmap() {
let mut store = KeyValueStore::new(5000);
let mut store = KeyValueStore::new("new_store".to_owned(), None);
store.set_hmap("ABC".to_string(), vec![], None);

assert_eq!(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use crate::data_store::{errors::ValueError, store::KeyValueStore, value_entry::ValueEntry};
use std::{
collections::HashSet,
time::{Duration, Instant},
};
use crate::stores::{errors::ValueError, store::KeyValueStore, value_entry::ValueEntry};
use std::collections::HashSet;

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 expiration = self._get_expiration_instant(ttl);
let value_entry = ValueEntry::from_hset(HashSet::from_iter(value), expiration);
self._insert(&key, &value_entry);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::collections::HashSet;

use crate::data_store::store::KeyValueStore;
use crate::stores::store::KeyValueStore;

#[test]
fn test_set_get_hset() {
let mut store = KeyValueStore::new(5000);
let mut store = KeyValueStore::new("new_store".to_owned(), None);
store.set_hset(
"ABC".to_string(),
vec![
Expand Down Expand Up @@ -38,7 +38,7 @@ fn test_set_get_hset() {

#[test]
fn test_add_remove_hset() {
let mut store = KeyValueStore::new(5000);
let mut store = KeyValueStore::new("new_store".to_owned(), None);
store.set_hset(
"ABC".to_string(),
vec![
Expand Down Expand Up @@ -95,7 +95,7 @@ fn test_add_remove_hset() {

#[test]
fn test_hset_union_intersection_difference() {
let mut store = KeyValueStore::new(5000);
let mut store = KeyValueStore::new("new_store".to_owned(), None);
store.set_hset(
"ABC".to_string(),
vec![
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use crate::data_store::{
use crate::stores::{
errors::{TypeConversionError, ValueError},
store::KeyValueStore,
types::ValueType,
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 expiration = self._get_expiration_instant(ttl);
let value_entry = ValueEntry::from_i64(value, expiration);
self._insert(&key, &value_entry);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::data_store::store::KeyValueStore;
use crate::stores::store::KeyValueStore;

#[test]
fn test_get_set_i64() {
let mut store = KeyValueStore::new(0);
let mut store = KeyValueStore::new("new_store".to_owned(), None);
store.set_i64("ABC".to_string(), 999, Some(5000));
assert_eq!(store.get_i64("ABC".to_string()).unwrap().unwrap(), 999);

Expand All @@ -21,7 +21,7 @@ fn test_get_set_i64() {

#[test]
fn test_incr_decr() {
let mut store = KeyValueStore::new(5000);
let mut store = KeyValueStore::new("new_store".to_owned(), None);
store.set_string("ABC".to_string(), "68".to_string(), None);
assert_eq!(store.incr("ABC".to_string(), None).unwrap().unwrap(), 69);
assert_eq!(store.get_i64("ABC".to_string()).unwrap().unwrap(), 69);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use crate::data_store::{errors::ValueError, store::KeyValueStore, value_entry::ValueEntry};
use std::{
collections::VecDeque,
time::{Duration, Instant},
};
use crate::stores::{errors::ValueError, store::KeyValueStore, value_entry::ValueEntry};
use std::collections::VecDeque;

impl KeyValueStore {
/// Inserts a Key-Value(in Vec<String> type) pair in the KeyValueStore
pub fn set_list(&mut self, key: String, value: Vec<String>, ttl: Option<u64>) {
let expiration = Instant::now() + Duration::from_millis(ttl.unwrap_or(self.default_ttl));
let expiration = self._get_expiration_instant(ttl);
let value_entry = ValueEntry::from_list(value, expiration);
self._insert(&key, &value_entry);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::data_store::store::KeyValueStore;
use crate::stores::store::KeyValueStore;

#[test]
fn test_set_get_deque() {
let mut store = KeyValueStore::new(5000);
let mut store = KeyValueStore::new("new_store".to_owned(), None);
store.set_list(
"ABC".to_string(),
vec!["X".to_string(), "Y".to_string(), "Z".to_string()],
Expand All @@ -27,7 +27,7 @@ fn test_set_get_deque() {

#[test]
fn test_deque_methods() {
let mut store = KeyValueStore::new(5000);
let mut store = KeyValueStore::new("new_store".to_owned(), None);
store.set_list("ABC".to_string(), vec!["X".to_string()], None);
assert_eq!(
store.get_list("ABC".to_string()).unwrap().unwrap(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::data_store::{errors::ValueError, store::KeyValueStore, value_entry::ValueEntry};
use std::time::{Duration, Instant};
use crate::stores::{errors::ValueError, store::KeyValueStore, value_entry::ValueEntry};

impl KeyValueStore {
/// Inserts a Key-Value(in String type) pair in the KeyValueStore
pub fn set_string(&mut self, key: String, value: String, ttl: Option<u64>) {
let expiration = Instant::now() + Duration::from_millis(ttl.unwrap_or(self.default_ttl));
let expiration = self._get_expiration_instant(ttl);
let value_entry = ValueEntry::from_string(value, expiration);
self._insert(&key, &value_entry);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::data_store::store::KeyValueStore;
use crate::stores::store::KeyValueStore;

#[test]
fn test_set_get_string() {
let mut store = KeyValueStore::new(0);
let mut store = KeyValueStore::new("new_store".to_owned(), None);
store.set_string("ABC".to_string(), "HELLO".to_string(), Some(5000));
assert_eq!(
store.get_string("ABC".to_string()).unwrap().unwrap(),
Expand All @@ -28,7 +28,7 @@ fn test_set_get_string() {

#[test]
fn test_pop_string() {
let mut store = KeyValueStore::new(0);
let mut store = KeyValueStore::new("new_store".to_owned(), None);
store.set_string("ABC".to_string(), "HELLO".to_string(), Some(5000));
assert_eq!(
store.pop_string("ABC".to_string()).unwrap().unwrap(),
Expand Down
File renamed without changes.
Loading
Loading