-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathlib.rs
More file actions
96 lines (75 loc) · 2.75 KB
/
lib.rs
File metadata and controls
96 lines (75 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//! Weighted in-memory buffer caches with LRU and S3-FIFO eviction.
mod builder;
mod lru;
mod s3_fifo;
mod thread_type;
use std::any::Any;
use std::sync::Arc;
pub use builder::BufferCacheBuilder;
use feldera_types::config::dev_tweaks::BufferCacheStrategy;
pub use lru::LruCache;
pub use s3_fifo::S3FifoCache;
pub use thread_type::ThreadType;
/// Cached values expose their memory cost directly to the cache backends.
pub trait CacheEntry: Any + Send + Sync {
/// Returns the cost of this value in bytes.
fn cost(&self) -> usize;
}
impl<T> CacheEntry for Arc<T>
where
T: CacheEntry + ?Sized + 'static,
{
fn cost(&self) -> usize {
(**self).cost()
}
}
impl dyn CacheEntry {
/// Attempts to downcast an `Arc<dyn CacheEntry>` to a concrete entry type.
pub fn downcast<T>(self: Arc<Self>) -> Option<Arc<T>>
where
T: Send + Sync + 'static,
{
(self as Arc<dyn Any + Send + Sync>).downcast().ok()
}
}
/// Shared trait object used to pass buffer-cache backends around.
pub type SharedBufferCache<K, V> = Arc<dyn BufferCache<K, V>>;
/// Common object-safe API implemented by all buffer-cache backends.
pub trait BufferCache<K, V>: Any + Send + Sync {
/// Returns this backend as [`Any`] for backend-specific downcasts.
fn as_any(&self) -> &dyn Any;
/// Returns the eviction strategy used by this cache.
fn strategy(&self) -> BufferCacheStrategy;
/// Inserts or replaces `key` with `value`.
///
/// When a key, value is inserted with a the cost that exceeds
/// the capacity of a shard in the cache, it is up to the
/// implementation to decided if it wants to store/accept
/// the key, value pair.
fn insert(&self, key: K, value: V);
/// Looks up `key` and returns a clone of the stored value.
fn get(&self, key: K) -> Option<V>;
/// Removes `key` if it is present and returns the removed value.
fn remove(&self, key: &K) -> Option<V>;
/// Removes every entry whose key matches `predicate`.
fn remove_if(&self, predicate: &dyn Fn(&K) -> bool);
/// Returns `true` if `key` is currently resident.
fn contains_key(&self, key: &K) -> bool;
/// Returns the number of resident entries.
fn len(&self) -> usize;
/// Returns `true` if the cache has no resident entries.
fn is_empty(&self) -> bool {
self.len() == 0
}
/// Returns the total resident weight.
fn total_charge(&self) -> usize;
/// Returns the configured total weight capacity.
fn total_capacity(&self) -> usize;
/// Returns the number of shards used by this backend.
fn shard_count(&self) -> usize;
/// Returns `(used_charge, capacity)` for shard `idx`.
#[cfg(test)]
fn shard_usage(&self, idx: usize) -> (usize, usize);
}
#[cfg(test)]
mod tests;