Skip to content

Commit

Permalink
delete clippy warning
Browse files Browse the repository at this point in the history
  • Loading branch information
goldwind-ting committed May 22, 2022
1 parent 05bf1bc commit bc13b0e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 56 deletions.
6 changes: 3 additions & 3 deletions src/cache/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ where
.map_err(|e| {
CacheError::ChannelError(format!(
"failed to send message to the insert buffer: {}",
e.to_string()
&e
))
})?;

Expand Down Expand Up @@ -625,7 +625,7 @@ where
&mut self,
res: Result<Instant, RecvError>,
) -> 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| {
Expand All @@ -634,7 +634,7 @@ where
self.callback.on_evict(victim);
});
})
})?)
})
}
}

Expand Down
11 changes: 5 additions & 6 deletions src/cache/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
49 changes: 15 additions & 34 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -219,18 +215,10 @@ impl<
}
}

pub fn update(&self, key: u64, val: V, conflict: u64, expiration: Time) -> UpdateResult<V> {
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<StoreItem<V>> {
self.try_remove(key, conflict).unwrap()
}

pub fn try_remove(&self, key: &u64, conflict: u64) -> Result<Option<StoreItem<V>>, CacheError> {
let mut data = self.shards[(*key as usize) % NUM_OF_SHARDS].write();

Expand All @@ -257,13 +245,6 @@ impl<
.map(|val| val.expiration)
}

pub fn cleanup<PS: BuildHasher + Clone + 'static>(
&self,
policy: Arc<LFUPolicy<PS>>,
) -> Vec<CrateItem<V>> {
self.try_cleanup(policy).unwrap()
}

pub fn try_cleanup<PS: BuildHasher + Clone + 'static>(
&self,
policy: Arc<LFUPolicy<PS>>,
Expand Down Expand Up @@ -414,7 +395,7 @@ mod test {
fn test_store_set_get() {
let s: ShardedMap<u64> = 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();
Expand All @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -488,28 +469,28 @@ mod test {
fn test_store_remove() {
let s: ShardedMap<u64> = 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());
Expand All @@ -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());
Expand All @@ -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);
}
}
12 changes: 0 additions & 12 deletions src/ttl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,6 @@ impl<S: BuildHasher + Clone + 'static> ExpirationMap<S> {
}
}

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() {
Expand Down Expand Up @@ -198,10 +194,6 @@ impl<S: BuildHasher + Clone + 'static> ExpirationMap<S> {
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();
Expand All @@ -216,10 +208,6 @@ impl<S: BuildHasher + Clone + 'static> ExpirationMap<S> {
Ok(())
}

pub fn cleanup(&self, now: Time) -> Option<HashMap<u64, u64, S>> {
self.try_cleanup(now).unwrap()
}

pub fn try_cleanup(&self, now: Time) -> Result<Option<HashMap<u64, u64, S>>, CacheError> {
let bucket_num = cleanup_bucket(now);
Ok(self
Expand Down

0 comments on commit bc13b0e

Please sign in to comment.