diff --git a/src/data_store/store.rs b/src/data_store/store.rs index 3b0251a..52570c1 100644 --- a/src/data_store/store.rs +++ b/src/data_store/store.rs @@ -179,7 +179,7 @@ impl KeyValueStore { } /// Gets a Value (converted to set type) associated to the Key in the KeyValueStore - pub fn get_hset(&self, key: String) -> Option, ValueError>> { + pub fn get_hset(&self, key: String) -> Option, ValueError>> { match self._get_or_none_if_expired(&key) { Some(value_entry) => Some(value_entry.get_value_as_hset()), _ => None, diff --git a/src/data_store/value_entry.rs b/src/data_store/value_entry.rs index 8df6b72..732b3bf 100644 --- a/src/data_store/value_entry.rs +++ b/src/data_store/value_entry.rs @@ -150,9 +150,12 @@ impl ValueEntry { } } - pub fn get_value_as_hset(&self) -> Result<&HashSet, ValueError> { + pub fn get_value_as_hset(&self) -> Result, ValueError> { match &self.value { - ValueType::Set(hash_set) => Ok(hash_set), + ValueType::Set(hash_set) => Ok(hash_set.to_owned()), + ValueType::String(string) => Ok(HashSet::from_iter( + string.chars().map(|item| item.to_string()), + )), _ => Err(ValueError::TypeConversionImpossible), } } diff --git a/src/public_tests.rs b/src/public_tests.rs index 8f472f6..561a1df 100644 --- a/src/public_tests.rs +++ b/src/public_tests.rs @@ -1,6 +1,6 @@ use crate::KeyValueStore; use serde::{Deserialize, Serialize}; -use std::time::Duration; +use std::{collections::HashSet, time::Duration}; #[test] fn test_contains() { @@ -214,7 +214,7 @@ fn test_incr_decr() { } #[test] -fn test_set_get_list() { +fn test_set_get_deque() { let mut store = KeyValueStore::new(5000); store.set_list( "ABC".to_string(), @@ -234,7 +234,7 @@ fn test_set_get_list() { } #[test] -fn test_list_methods() { +fn test_deque_methods() { let mut store = KeyValueStore::new(5000); store.set_list("ABC".to_string(), vec!["X".to_string()], None); assert_eq!( @@ -317,3 +317,143 @@ fn test_list_methods() { ); assert_eq!(store.list_size("ABC".to_string()).unwrap().unwrap(), 0); } + +#[test] +fn test_set_get_hset() { + let mut store = KeyValueStore::new(5000); + store.set_hset( + "ABC".to_string(), + vec![ + "X".to_string(), + "Y".to_string(), + "Z".to_string(), + "Y".to_string(), + "Z".to_string(), + ], + None, + ); + + let mut sorted_list_in_store = store.get_list("ABC".to_string()).unwrap().unwrap(); + sorted_list_in_store.sort(); + assert_eq!( + sorted_list_in_store, + vec!["X".to_string(), "Y".to_string(), "Z".to_string()] + ); + + store.set_string("DEF".to_string(), "XYZYZYZ".to_string(), None); + assert_eq!( + store.get_hset("DEF".to_string()).unwrap().unwrap(), + HashSet::from_iter(vec!["X".to_string(), "Y".to_string(), "Z".to_string()]) + ); +} + +#[test] +fn test_add_remove_hset() { + let mut store = KeyValueStore::new(5000); + store.set_hset( + "ABC".to_string(), + vec![ + "X".to_string(), + "Y".to_string(), + "Z".to_string(), + "Y".to_string(), + "Z".to_string(), + ], + None, + ); + + assert_eq!(store.hset_size("ABC".to_string()).unwrap().unwrap(), 3); + + assert_eq!( + store + .hset_add("ABC".to_string(), "Q".to_string()) + .unwrap() + .unwrap(), + 4 + ); + assert!(store + .hset_contains("ABC".to_string(), "Q".to_string()) + .unwrap() + .unwrap()); + + assert!(!store + .hset_contains("ABC".to_string(), "A".to_string()) + .unwrap() + .unwrap()); + assert_eq!( + store + .hset_remove("ABC".to_string(), "A".to_string()) + .unwrap() + .unwrap(), + 4 + ); + + assert_eq!( + store + .hset_remove("ABC".to_string(), "X".to_string()) + .unwrap() + .unwrap(), + 3 + ); + + let mut sorted_list_in_store = store.get_list("ABC".to_string()).unwrap().unwrap(); + sorted_list_in_store.sort(); + assert_eq!( + sorted_list_in_store, + vec!["Q".to_string(), "Y".to_string(), "Z".to_string()] + ); +} + +#[test] +fn test_hset_union_intersection_difference() { + let mut store = KeyValueStore::new(5000); + store.set_hset( + "ABC".to_string(), + vec![ + "X".to_string(), + "Y".to_string(), + "Z".to_string(), + "A".to_string(), + "B".to_string(), + "B".to_string(), + ], + None, + ); + + store.set_hset( + "DEF".to_string(), + vec![ + "A".to_string(), + "B".to_string(), + "C".to_string(), + "D".to_string(), + "E".to_string(), + ], + None, + ); + + assert_eq!(store.hset_size("ABC".to_string()).unwrap().unwrap(), 5); + assert_eq!(store.hset_size("DEF".to_string()).unwrap().unwrap(), 5); + + let mut intersection = store + .hset_intersection("ABC".to_string(), "DEF".to_string()) + .unwrap() + .unwrap(); + intersection.sort(); + + let mut union = store + .hset_union("ABC".to_string(), "DEF".to_string()) + .unwrap() + .unwrap(); + union.sort(); + + let mut difference = store + .hset_difference("ABC".to_string(), "DEF".to_string()) + .unwrap() + .unwrap(); + difference.sort(); + + assert_eq!(intersection, vec!["A", "B"]); + assert_eq!(union, vec!["A", "B", "C", "D", "E", "X", "Y", "Z"]); + assert_eq!(difference, vec!["X", "Y", "Z"]); +}