Skip to content

Commit

Permalink
Merge pull request #24 from goldwind-ting/feat-clippy
Browse files Browse the repository at this point in the history
delete clippy warning
  • Loading branch information
al8n authored May 22, 2022
2 parents 85c551b + bc13b0e commit b69afdf
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 49 deletions.
6 changes: 3 additions & 3 deletions src/cache/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ where
.map_err(|e| {
CacheError::ChannelError(format!(
"failed to send message to the insert buffer: {}",
e.to_string()
&e
))
})?;

Expand Down Expand Up @@ -610,7 +610,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 @@ -619,7 +619,7 @@ where
self.callback.on_evict(victim);
});
})
})?)
})
}
}

Expand Down
49 changes: 15 additions & 34 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,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 @@ -214,18 +210,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 @@ -252,13 +240,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 @@ -409,7 +390,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 @@ -428,7 +409,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 @@ -448,7 +429,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 @@ -483,28 +464,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 @@ -514,14 +495,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 @@ -545,14 +526,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 b69afdf

Please sign in to comment.