diff --git a/src/cache/sync.rs b/src/cache/sync.rs index 6d59384..0361f37 100644 --- a/src/cache/sync.rs +++ b/src/cache/sync.rs @@ -477,7 +477,7 @@ where .map_err(|e| { CacheError::ChannelError(format!( "failed to send message to the insert buffer: {}", - e.to_string() + &e )) })?; @@ -625,7 +625,7 @@ where &mut self, res: Result, ) -> Result<(), CacheError> { - Ok(res + res .map_err(|e| CacheError::RecvError(format!("fail to receive msg from ticker: {}", e))) .and_then(|_| { self.store.try_cleanup(self.policy.clone()).map(|items| { @@ -634,7 +634,7 @@ where self.callback.on_evict(victim); }); }) - })?) + }) } } diff --git a/src/cache/test.rs b/src/cache/test.rs index c4cbba3..849a246 100644 --- a/src/cache/test.rs +++ b/src/cache/test.rs @@ -696,15 +696,14 @@ mod sync_test { fn test_valueref_ttl() { let ttl = Duration::from_secs(1); let c = Cache::builder(100, 1000) - .set_key_builder(TransparentKeyBuilder::default()) - .set_metrics(true) - .finalize() - .unwrap(); + .set_key_builder(TransparentKeyBuilder::default()) + .set_metrics(true) + .finalize() + .unwrap(); c.try_insert_with_ttl(1, 1, 1, ttl).unwrap(); c.wait().unwrap(); let val = c.get(&1).unwrap(); - assert!(val.ttl()>Duration::from_millis(900)); - + assert!(val.ttl() > Duration::from_millis(900)); } } diff --git a/src/policy.rs b/src/policy.rs index ff0bc02..1bb8d52 100644 --- a/src/policy.rs +++ b/src/policy.rs @@ -23,8 +23,8 @@ const DEFAULT_SAMPLES: usize = 5; macro_rules! impl_policy { ($policy: ident) => { - use crate::policy::DEFAULT_SAMPLES; use crate::policy::PolicyPair; + use crate::policy::DEFAULT_SAMPLES; impl $policy { #[inline] diff --git a/src/store.rs b/src/store.rs index 734302d..4aa0df5 100644 --- a/src/store.rs +++ b/src/store.rs @@ -187,10 +187,6 @@ impl< Ok(()) } - pub fn insert(&self, key: u64, val: V, conflict: u64, expiration: Time) { - self.try_insert(key, val, conflict, expiration).unwrap(); - } - pub fn try_update( &self, key: u64, @@ -219,18 +215,10 @@ impl< } } - pub fn update(&self, key: u64, val: V, conflict: u64, expiration: Time) -> UpdateResult { - self.try_update(key, val, conflict, expiration).unwrap() - } - pub fn len(&self) -> usize { self.shards.iter().map(|l| l.read().len()).sum() } - pub fn remove(&self, key: &u64, conflict: u64) -> Option> { - self.try_remove(key, conflict).unwrap() - } - pub fn try_remove(&self, key: &u64, conflict: u64) -> Result>, CacheError> { let mut data = self.shards[(*key as usize) % NUM_OF_SHARDS].write(); @@ -257,13 +245,6 @@ impl< .map(|val| val.expiration) } - pub fn cleanup( - &self, - policy: Arc>, - ) -> Vec> { - self.try_cleanup(policy).unwrap() - } - pub fn try_cleanup( &self, policy: Arc>, @@ -414,7 +395,7 @@ mod test { fn test_store_set_get() { let s: ShardedMap = ShardedMap::new(); - s.insert(1, 2, 0, Time::now()); + s.try_insert(1, 2, 0, Time::now()).unwrap(); let val = s.get(&1, 0).unwrap(); assert_eq!(&2, val.value()); val.release(); @@ -433,7 +414,7 @@ mod test { let s1 = s.clone(); std::thread::spawn(move || { - s.insert(1, 2, 0, Time::now()); + s.try_insert(1, 2, 0, Time::now()).unwrap(); }); loop { @@ -453,7 +434,7 @@ mod test { let s1 = s.clone(); std::thread::spawn(move || { - s.insert(1, 2, 0, Time::now()); + s.try_insert(1, 2, 0, Time::now()).unwrap(); loop { match s.get(&1, 0) { None => continue, @@ -488,28 +469,28 @@ mod test { fn test_store_remove() { let s: ShardedMap = ShardedMap::new(); - s.insert(1, 2, 0, Time::now()); - assert_eq!(s.remove(&1, 0).unwrap().value.into_inner(), 2); + s.try_insert(1, 2, 0, Time::now()).unwrap(); + assert_eq!(s.try_remove(&1, 0).unwrap().unwrap().value.into_inner(), 2); let v = s.get(&1, 0); assert!(v.is_none()); - assert!(s.remove(&2, 0).is_none()); + assert!(s.try_remove(&2, 0).unwrap().is_none()); } #[test] fn test_store_update() { let s = ShardedMap::new(); - s.insert(1, 1, 0, Time::now()); - let v = s.update(1, 2, 0, Time::now()); + s.try_insert(1, 1, 0, Time::now()).unwrap(); + let v = s.try_update(1, 2, 0, Time::now()).unwrap(); assert_eq!(v.into_inner(), 1); assert_eq!(s.get(&1, 0).unwrap().read(), 2); - let v = s.update(1, 3, 0, Time::now()); + let v = s.try_update(1, 3, 0, Time::now()).unwrap(); assert_eq!(v.into_inner(), 2); assert_eq!(s.get(&1, 0).unwrap().read(), 3); - let v = s.update(2, 2, 0, Time::now()); + let v = s.try_update(2, 2, 0, Time::now()).unwrap(); assert_eq!(v.into_inner(), 2); let v = s.get(&2, 0); assert!(v.is_none()); @@ -519,14 +500,14 @@ mod test { fn test_store_expiration() { let exp = Time::now_with_expiration(Duration::from_secs(1)); let s = ShardedMap::new(); - s.insert(1, 1, 0, exp); + s.try_insert(1, 1, 0, exp).unwrap(); assert_eq!(s.get(&1, 0).unwrap().read(), 1); let ttl = s.expiration(&1); assert_eq!(exp, ttl.unwrap()); - s.remove(&1, 0); + s.try_remove(&1, 0).unwrap(); assert!(s.get(&1, 0).is_none()); let ttl = s.expiration(&1); assert!(ttl.is_none()); @@ -550,14 +531,14 @@ mod test { drop(data1); assert!(s.get(&1, 1).is_none()); - s.insert(1, 2, 1, Time::now()); + s.try_insert(1, 2, 1, Time::now()).unwrap(); assert_ne!(s.get(&1, 0).unwrap().read(), 2); - let v = s.update(1, 2, 1, Time::now()); + let v = s.try_update(1, 2, 1, Time::now()).unwrap(); assert_eq!(v.into_inner(), 2); assert_ne!(s.get(&1, 0).unwrap().read(), 2); - assert!(s.remove(&1, 1).is_none()); + assert!(s.try_remove(&1, 1).unwrap().is_none()); assert_eq!(s.get(&1, 0).unwrap().read(), 1); } } diff --git a/src/ttl.rs b/src/ttl.rs index 9002e35..8b1b49f 100644 --- a/src/ttl.rs +++ b/src/ttl.rs @@ -129,10 +129,6 @@ impl ExpirationMap { } } - pub fn insert(&self, key: u64, conflict: u64, expiration: Time) { - self.try_insert(key, conflict, expiration).unwrap(); - } - pub fn try_insert(&self, key: u64, conflict: u64, expiration: Time) -> Result<(), CacheError> { // Items that don't expire don't need to be in the expiration map. if expiration.is_zero() { @@ -198,10 +194,6 @@ impl ExpirationMap { Ok(()) } - pub fn remove(&self, key: &u64, expiration: Time) { - self.try_remove(key, expiration).unwrap() - } - pub fn try_remove(&self, key: &u64, expiration: Time) -> Result<(), CacheError> { let bucket_num = storage_bucket(expiration); let m = self.buckets.read(); @@ -216,10 +208,6 @@ impl ExpirationMap { Ok(()) } - pub fn cleanup(&self, now: Time) -> Option> { - self.try_cleanup(now).unwrap() - } - pub fn try_cleanup(&self, now: Time) -> Result>, CacheError> { let bucket_num = cleanup_bucket(now); Ok(self