Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Debug. Add entry_count and weighted_size methods #134

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/dash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
mod base_cache;
mod builder;
mod cache;
mod debug_fmt;
mod iter;
mod mapref;

Expand Down
32 changes: 15 additions & 17 deletions src/dash/base_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ impl<K, V, S> Drop for BaseCache<K, V, S> {
}
}

impl<K, V, S> BaseCache<K, V, S> {
pub(crate) fn policy(&self) -> Policy {
self.inner.policy()
}

pub(crate) fn entry_count(&self) -> u64 {
self.inner.entry_count()
}

pub(crate) fn weighted_size(&self) -> u64 {
self.inner.weighted_size()
}
}

impl<K, V, S> BaseCache<K, V, S>
where
K: Hash + Eq + Send + Sync + 'static,
Expand Down Expand Up @@ -203,20 +217,6 @@ where
let now = self.inner.current_time_from_expiration_clock();
self.inner.set_valid_after(now);
}

pub(crate) fn policy(&self) -> Policy {
self.inner.policy()
}

#[cfg(test)]
pub(crate) fn estimated_entry_count(&self) -> u64 {
self.inner.estimated_entry_count()
}

#[cfg(test)]
pub(crate) fn weighted_size(&self) -> u64 {
self.inner.weighted_size()
}
}

impl<'a, K, V, S> BaseCache<K, V, S>
Expand Down Expand Up @@ -555,13 +555,11 @@ impl<K, V, S> Inner<K, V, S> {
self.time_to_idle
}

#[cfg(test)]
#[inline]
fn estimated_entry_count(&self) -> u64 {
fn entry_count(&self) -> u64 {
self.entry_count.load()
}

#[cfg(test)]
#[inline]
pub(crate) fn weighted_size(&self) -> u64 {
self.weighted_size.load()
Expand Down
106 changes: 70 additions & 36 deletions src/dash/cache.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{
base_cache::{BaseCache, HouseKeeperArc, MAX_SYNC_REPEATS, WRITE_RETRY_INTERVAL_MICROS},
debug_fmt::DebugFmt,
CacheBuilder, ConcurrentCacheExt, EntryRef, Iter,
};
use crate::{
Expand Down Expand Up @@ -268,14 +269,7 @@ where
S: BuildHasher + Clone,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut d_map = f.debug_map();

for r in self.iter() {
let (k, v) = r.pair();
d_map.entry(k, v);
}

d_map.finish()
self.debug_fmt().default_fmt().fmt(f)
}
}

Expand Down Expand Up @@ -304,6 +298,28 @@ where
}
}

impl<K, V, S> Cache<K, V, S> {
/// Returns a read-only cache policy of this cache.
///
/// At this time, cache policy cannot be modified after cache creation.
/// A future version may support to modify it.
pub fn policy(&self) -> Policy {
self.base.policy()
}

pub fn debug_fmt(&self) -> DebugFmt<'_, K, V, S> {
DebugFmt::new(self)
}

pub fn entry_count(&self) -> u64 {
self.base.entry_count()
}

pub fn weighted_size(&self) -> u64 {
self.base.weighted_size()
}
}

impl<K, V, S> Cache<K, V, S>
where
K: Hash + Eq + Send + Sync + 'static,
Expand Down Expand Up @@ -419,24 +435,6 @@ where
pub fn invalidate_all(&self) {
self.base.invalidate_all();
}

/// Returns a read-only cache policy of this cache.
///
/// At this time, cache policy cannot be modified after cache creation.
/// A future version may support to modify it.
pub fn policy(&self) -> Policy {
self.base.policy()
}

#[cfg(test)]
pub(crate) fn estimated_entry_count(&self) -> u64 {
self.base.estimated_entry_count()
}

#[cfg(test)]
pub(crate) fn weighted_size(&self) -> u64 {
self.base.weighted_size()
}
}

impl<'a, K, V, S> Cache<K, V, S>
Expand Down Expand Up @@ -552,7 +550,7 @@ where
S: BuildHasher + Clone + Send + Sync + 'static,
{
pub(crate) fn is_table_empty(&self) -> bool {
self.estimated_entry_count() == 0
self.entry_count() == 0
}

pub(crate) fn reconfigure_for_testing(&mut self) {
Expand Down Expand Up @@ -725,7 +723,7 @@ mod tests {
assert!(!cache.contains_key(&"d"));

// Verify the sizes.
assert_eq!(cache.estimated_entry_count(), 2);
assert_eq!(cache.entry_count(), 2);
assert_eq!(cache.weighted_size(), 25);
}

Expand Down Expand Up @@ -827,14 +825,14 @@ mod tests {
cache.insert("b", "bob");
cache.sync();

assert_eq!(cache.estimated_entry_count(), 1);
assert_eq!(cache.entry_count(), 1);

mock.increment(Duration::from_secs(5)); // 15 secs.
cache.sync();

assert_eq!(cache.get(&"b"), Some("bob"));
assert!(cache.contains_key(&"b"));
assert_eq!(cache.estimated_entry_count(), 1);
assert_eq!(cache.entry_count(), 1);

cache.insert("b", "bill");
cache.sync();
Expand All @@ -844,7 +842,7 @@ mod tests {

assert_eq!(cache.get(&"b"), Some("bill"));
assert!(cache.contains_key(&"b"));
assert_eq!(cache.estimated_entry_count(), 1);
assert_eq!(cache.entry_count(), 1);

mock.increment(Duration::from_secs(5)); // 25 secs
assert_eq!(cache.get(&"a"), None);
Expand Down Expand Up @@ -887,7 +885,7 @@ mod tests {
cache.insert("b", "bob");
cache.sync();

assert_eq!(cache.estimated_entry_count(), 2);
assert_eq!(cache.entry_count(), 2);

mock.increment(Duration::from_secs(2)); // 12 secs.
cache.sync();
Expand All @@ -897,7 +895,7 @@ mod tests {
assert!(cache.contains_key(&"b"));
cache.sync();

assert_eq!(cache.estimated_entry_count(), 2);
assert_eq!(cache.entry_count(), 2);

mock.increment(Duration::from_secs(3)); // 15 secs.
assert_eq!(cache.get(&"a"), None);
Expand All @@ -908,7 +906,7 @@ mod tests {
assert_eq!(cache.iter().count(), 1);

cache.sync();
assert_eq!(cache.estimated_entry_count(), 1);
assert_eq!(cache.entry_count(), 1);

mock.increment(Duration::from_secs(10)); // 25 secs
assert_eq!(cache.get(&"a"), None);
Expand Down Expand Up @@ -1042,16 +1040,52 @@ mod tests {

#[test]
fn test_debug_format() {
let cache = Cache::new(10);
let mut cache = Cache::builder().max_capacity(10).build();
cache.reconfigure_for_testing();

// Make the cache exterior immutable.
let cache = cache;

cache.insert('a', "alice");
cache.insert('b', "bob");
cache.insert('c', "cindy");
cache.sync();

assert_eq!(
format!("{:?}", cache),
"Cache { max_capacity: Some(10), entry_count: 3, weighted_size: 3 }"
);

let debug_str = format!("{:?}", cache);
let debug_str = format!("{:?}", cache.debug_fmt().entries());
assert!(debug_str.starts_with('{'));
assert!(debug_str.contains(r#"'a': "alice""#));
assert!(debug_str.contains(r#"'b': "bob""#));
assert!(debug_str.contains(r#"'c': "cindy""#));
assert!(debug_str.ends_with('}'));

let weigher = |_k: &char, v: &(&str, u32)| v.1;

let mut cache = Cache::builder().max_capacity(50).weigher(weigher).build();
cache.reconfigure_for_testing();

// Make the cache exterior immutable.
let cache = cache;

cache.insert('a', ("alice", 10));
cache.insert('b', ("bob", 15));
cache.insert('c', ("cindy", 5));
cache.sync();

assert_eq!(
format!("{:?}", cache),
"Cache { max_capacity: Some(50), entry_count: 3, weighted_size: 30 }"
);

let debug_str = format!("{:?}", cache.debug_fmt().entries());
assert!(debug_str.starts_with('{'));
assert!(debug_str.contains(r#"'a': ("alice", 10)"#));
assert!(debug_str.contains(r#"'b': ("bob", 15)"#));
assert!(debug_str.contains(r#"'c': ("cindy", 5)"#));
assert!(debug_str.ends_with('}'));
}
}
88 changes: 88 additions & 0 deletions src/dash/debug_fmt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use std::{
fmt,
hash::{BuildHasher, Hash},
};

use super::Cache;

pub struct DebugFmt<'a, K, V, S> {
cache: &'a Cache<K, V, S>,
}

impl<'a, K, V, S> DebugFmt<'a, K, V, S> {
pub(crate) fn new(cache: &'a Cache<K, V, S>) -> Self {
Self { cache }
}

pub fn default_fmt(&self) -> DefaultDebugFmt {
DefaultDebugFmt::new(
"Cache",
self.cache.policy().max_capacity(),
self.cache.entry_count(),
self.cache.weighted_size(),
)
}

pub fn entries(&self) -> EntriesDebugFmt<'a, K, V, S>
where
K: fmt::Debug,
V: fmt::Debug,
{
EntriesDebugFmt { cache: self.cache }
}
}

pub struct DefaultDebugFmt {
name: &'static str,
max_capacity: Option<u64>,
entry_count: u64,
weighted_size: u64,
}

impl DefaultDebugFmt {
fn new(
name: &'static str,
max_capacity: Option<u64>,
entry_count: u64,
weighted_size: u64,
) -> Self {
Self {
name,
max_capacity,
entry_count,
weighted_size,
}
}
}

impl fmt::Debug for DefaultDebugFmt {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct(self.name)
.field("max_capacity", &self.max_capacity)
.field("entry_count", &self.entry_count)
.field("weighted_size", &self.weighted_size)
.finish()
}
}

pub struct EntriesDebugFmt<'a, K, V, S> {
cache: &'a Cache<K, V, S>,
}

impl<'a, K, V, S> fmt::Debug for EntriesDebugFmt<'a, K, V, S>
where
K: fmt::Debug + Hash + Eq,
V: fmt::Debug,
S: BuildHasher + Clone,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut d_map = f.debug_map();

for r in self.cache.iter() {
let (k, v) = r.pair();
d_map.entry(k, v);
}

d_map.finish()
}
}
Loading