diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bf0c52..9856c32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ## [Unreleased] - DESCRIPTION - YYYY-MM-DD -- add SetLoad Type, which implements a counter based clock ordering +- add ClockOrdered Type, which implements a counter based clock ordering - ## 0.2 - Lock and ADT - 2023-09-22 diff --git a/src/redis/set_load.rs b/src/redis/clock.rs similarity index 75% rename from src/redis/set_load.rs rename to src/redis/clock.rs index 3be0ae6..30d2a56 100644 --- a/src/redis/set_load.rs +++ b/src/redis/clock.rs @@ -4,7 +4,7 @@ use std::ops::{Deref, DerefMut}; use thiserror::Error; #[derive(Debug, Error)] -pub enum SetLoadError { +pub enum ClockOrderedError { #[error("Ordering number is not greater than current number stored in redis.")] OrderError, } @@ -40,7 +40,7 @@ local key = ARGV[1] return {redis.call("GET", key), redis.call("GET", key .. ":order")} "#; -/// The SetLoad type. +/// The ClockOrdered type. /// /// It is used to store a value in redis and load it in sync. /// It tracks automatically an ordering number to ensure that the value is only stored if the order is greater than the current order, mostly from other instances. @@ -52,16 +52,16 @@ return {redis.call("GET", key), redis.call("GET", key .. ":order")} /// Another use case is, when it is okay for you, that the value could be not the latest or /// computing a derived value multiple times is acceptable. #[derive(Debug)] -pub struct SetLoad { +pub struct ClockOrdered { data: Generic, counter: usize, } -impl SetLoad +impl ClockOrdered where T: serde::Serialize + serde::de::DeserializeOwned, { - /// Creates a new SetLoad. + /// Creates a new ClockOrdered. /// The value is loaded from redis directly. pub fn new(data: Generic) -> Self { let mut s = Self { data, counter: 0 }; @@ -76,13 +76,13 @@ where /// # Example /// ``` /// use dtypes::redis::Generic; - /// use dtypes::redis::SetLoad; + /// use dtypes::redis::ClockOrdered; /// /// let client = redis::Client::open("redis://localhost:6379").unwrap(); - /// let mut i32 = Generic::with_value(1, "test_add_setload_example1", client.clone()); - /// let mut setload = SetLoad::new(i32); - /// setload.store(2).unwrap(); - /// assert_eq!(*setload, 2); + /// let mut i32 = Generic::with_value(1, "test_add_clock_ordered_example1", client.clone()); + /// let mut clock_ordered = ClockOrdered::new(i32); + /// clock_ordered.store(2).unwrap(); + /// assert_eq!(*clock_ordered, 2); /// ``` /// /// The store can fail if the order is not greater than the current order. @@ -92,26 +92,26 @@ where /// ``` /// use std::thread; /// use dtypes::redis::Generic; - /// use dtypes::redis::SetLoad; + /// use dtypes::redis::ClockOrdered; /// /// let client = redis::Client::open("redis://localhost:6379").unwrap(); /// let client2 = client.clone(); /// /// thread::scope(|s| { /// let t1 = s.spawn(|| { - /// let mut i32: Generic = Generic::new("test_add_setload_example2", client2); - /// let mut setload = SetLoad::new(i32); - /// while let Err(_) = setload.store(2) {} - /// assert_eq!(*setload, 2); + /// let mut i32: Generic = Generic::new("test_add_clock_ordered_example2", client2); + /// let mut clock_ordered = ClockOrdered::new(i32); + /// while let Err(_) = clock_ordered.store(2) {} + /// assert_eq!(*clock_ordered, 2); /// }); - /// let mut i32: Generic = Generic::new("test_add_setload_example2", client); - /// let mut setload = SetLoad::new(i32); - /// while let Err(_) = setload.store(3) {} - /// assert_eq!(*setload, 3); + /// let mut i32: Generic = Generic::new("test_add_clock_ordered_example2", client); + /// let mut clock_ordered = ClockOrdered::new(i32); + /// while let Err(_) = clock_ordered.store(3) {} + /// assert_eq!(*clock_ordered, 3); /// t1.join().unwrap(); /// }); /// ``` - pub fn store(&mut self, val: T) -> Result<(), SetLoadError> { + pub fn store(&mut self, val: T) -> Result<(), ClockOrderedError> { self.counter += 1; let val_json = serde_json::to_string(&val).unwrap(); let (v, order) = self.store_redis(&val_json); @@ -122,36 +122,36 @@ where return Ok(()); } } - Err(SetLoadError::OrderError) + Err(ClockOrderedError::OrderError) } /// Stores the value in the redis server and blocks until succeeds. - /// Everything else is equal to [SetLoad::store]. + /// Everything else is equal to [ClockOrdered::store]. /// /// # Example /// ``` /// use std::thread; /// use dtypes::redis::Generic; - /// use dtypes::redis::SetLoad; + /// use dtypes::redis::ClockOrdered; /// /// let client = redis::Client::open("redis://localhost:6379").unwrap(); /// let client2 = client.clone(); /// /// thread::scope(|s| { /// let t1 = s.spawn(|| { - /// let mut i32: Generic = Generic::new("test_add_setload_example3", client2); - /// let mut setload = SetLoad::new(i32); - /// setload.store_blocking(2).unwrap(); - /// assert_eq!(*setload, 2); + /// let mut i32: Generic = Generic::new("test_add_clock_ordered_example3", client2); + /// let mut clock_ordered = ClockOrdered::new(i32); + /// clock_ordered.store_blocking(2).unwrap(); + /// assert_eq!(*clock_ordered, 2); /// }); - /// let mut i32: Generic = Generic::new("test_add_setload_example3", client); - /// let mut setload = SetLoad::new(i32); - /// setload.store_blocking(3).unwrap(); - /// assert_eq!(*setload, 3); + /// let mut i32: Generic = Generic::new("test_add_clock_ordered_example3", client); + /// let mut clock_ordered = ClockOrdered::new(i32); + /// clock_ordered.store_blocking(3).unwrap(); + /// assert_eq!(*clock_ordered, 3); /// t1.join().unwrap(); /// }); /// ``` - pub fn store_blocking(&mut self, val: T) -> Result<(), SetLoadError> { + pub fn store_blocking(&mut self, val: T) -> Result<(), ClockOrderedError> { let val_json = serde_json::to_string(&val).unwrap(); let mut res = self.store_redis(&val_json); @@ -169,7 +169,7 @@ where redis::Script::new(SET_LOAD_SCRIPT) .arg(&self.data.key) .arg(self.counter) - .arg(&val) + .arg(val) .invoke(&mut conn) .expect("Could not execute script") } @@ -205,7 +205,7 @@ where } } -impl Deref for SetLoad { +impl Deref for ClockOrdered { type Target = Generic; fn deref(&self) -> &Self::Target { @@ -213,7 +213,7 @@ impl Deref for SetLoad { } } -impl DerefMut for SetLoad { +impl DerefMut for ClockOrdered { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.data } @@ -223,13 +223,13 @@ impl DerefMut for SetLoad { mod tests { #[test] fn test_set_load() { + use crate::redis::ClockOrdered; use crate::redis::Generic; - use crate::redis::SetLoad; let client = redis::Client::open("redis://localhost:6379").unwrap(); - let i32: Generic = Generic::new("test_add_setload", client.clone()); - let mut setload = SetLoad::new(i32); - setload.store(2).unwrap(); - assert_eq!(*setload, 2); + let i32: Generic = Generic::new("test_add_clock_ordered", client.clone()); + let mut clock_ordered = ClockOrdered::new(i32); + clock_ordered.store(2).unwrap(); + assert_eq!(*clock_ordered, 2); } } diff --git a/src/redis/mod.rs b/src/redis/mod.rs index a3a72bc..8fc46e1 100644 --- a/src/redis/mod.rs +++ b/src/redis/mod.rs @@ -10,7 +10,7 @@ //! * [List](redis::List) //! * Sync types: //! * [Mutex](redis::Mutex) -//! * [SetLoad](redis::SetLoad) +//! * [ClockOrdered](redis::ClockOrdered) //! //! This crate implements the most common traits for the primitive types, so it is frictionless to use them in place. //! The methods of the types can be seen in the documentation of [Generic](redis::Generic). @@ -37,17 +37,18 @@ //! More examples can be found on the doc pages of the types. //! mod bool_type; +mod clock; mod generic; mod helper; mod integer; mod list; mod mutex; -mod set_load; mod string; pub(crate) use helper::apply_operator; pub use bool_type::TBool as Dbool; +pub use clock::ClockOrdered; pub use generic::Generic; pub use integer::{ Ti16 as Di16, Ti32 as Di32, Ti64 as Di64, Ti8 as Di8, Tisize as Disize, Tu16 as Du16, @@ -55,5 +56,4 @@ pub use integer::{ }; pub use list::{List, ListCache, ListIter}; pub use mutex::{Guard, LockError, Mutex}; -pub use set_load::{SetLoad, SetLoadError}; pub use string::TString as DString;