Skip to content

Commit

Permalink
EvictingMap is now a template
Browse files Browse the repository at this point in the history
This is in prep to make it more generalized so file system store
can use it.
  • Loading branch information
allada committed Nov 11, 2021
1 parent 81fb378 commit 791dc67
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
4 changes: 2 additions & 2 deletions cas/store/memory_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use evicting_map::EvictingMap;
use traits::{ResultFuture, StoreTrait, UploadSizeInfo};

pub struct MemoryStore {
map: Mutex<EvictingMap<Instant>>,
map: Mutex<EvictingMap<Vec<u8>, Instant>>,
}

impl MemoryStore {
Expand All @@ -38,7 +38,7 @@ impl StoreTrait for MemoryStore {
fn has<'a>(self: std::pin::Pin<&'a Self>, digest: DigestInfo) -> ResultFuture<'a, Option<usize>> {
Box::pin(async move {
let mut map = self.map.lock().await;
Ok(map.size_for_key(&digest))
Ok(map.size_for_key(&digest).map(|v| v as usize))
})
}

Expand Down
28 changes: 19 additions & 9 deletions util/evicting_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,31 @@ impl InstantWrapper for Instant {
}
}

struct EvictionItem {
struct EvictionItem<T: LenEntry> {
seconds_since_anchor: u32,
data: Arc<Vec<u8>>,
data: Arc<T>,
}

pub struct EvictingMap<T: InstantWrapper> {
lru: LruCache<DigestInfo, EvictionItem>,
anchor_time: T,
pub trait LenEntry {
fn len(&self) -> usize;
}

impl LenEntry for Vec<u8> {
fn len(&self) -> usize {
<Vec<u8>>::len(self)
}
}

pub struct EvictingMap<T: LenEntry, I: InstantWrapper> {
lru: LruCache<DigestInfo, EvictionItem<T>>,
anchor_time: I,
sum_store_size: usize,
max_bytes: usize,
max_seconds: u32,
}

impl<T: InstantWrapper> EvictingMap<T> {
pub fn new(config: &EvictionPolicy, anchor_time: T) -> Self {
impl<T: LenEntry, I: InstantWrapper> EvictingMap<T, I> {
pub fn new(config: &EvictionPolicy, anchor_time: I) -> Self {
let mut lru = LruCache::unbounded();
if config.max_count != 0 {
lru = LruCache::new(config.max_count.try_into().expect("Could not convert max_count to u64"));
Expand Down Expand Up @@ -81,15 +91,15 @@ impl<T: InstantWrapper> EvictingMap<T> {
None
}

pub fn get<'a>(&'a mut self, hash: &DigestInfo) -> Option<&'a Arc<Vec<u8>>> {
pub fn get<'a>(&'a mut self, hash: &DigestInfo) -> Option<&'a Arc<T>> {
if let Some(mut entry) = self.lru.get_mut(hash) {
entry.seconds_since_anchor = self.anchor_time.elapsed().as_secs() as u32;
return Some(&entry.data);
}
None
}

pub fn insert(&mut self, hash: DigestInfo, data: Arc<Vec<u8>>) {
pub fn insert(&mut self, hash: DigestInfo, data: Arc<T>) {
let new_item_size = data.len();
let eviction_item = EvictionItem {
seconds_since_anchor: self.anchor_time.elapsed().as_secs() as u32,
Expand Down
12 changes: 6 additions & 6 deletions util/tests/evicting_map_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ mod evicting_map_tests {

#[tokio::test]
async fn insert_purges_at_max_count() -> Result<(), Error> {
let mut evicting_map = EvictingMap::new(
let mut evicting_map = EvictingMap::<Vec<u8>, Instant>::new(
&EvictionPolicy {
max_count: 3,
max_seconds: 0,
Expand Down Expand Up @@ -70,7 +70,7 @@ mod evicting_map_tests {

#[tokio::test]
async fn insert_purges_at_max_bytes() -> Result<(), Error> {
let mut evicting_map = EvictingMap::new(
let mut evicting_map = EvictingMap::<Vec<u8>, Instant>::new(
&EvictionPolicy {
max_count: 0,
max_seconds: 0,
Expand Down Expand Up @@ -110,7 +110,7 @@ mod evicting_map_tests {

#[tokio::test]
async fn insert_purges_at_max_seconds() -> Result<(), Error> {
let mut evicting_map = EvictingMap::new(
let mut evicting_map = EvictingMap::<Vec<u8>, MockInstantWrapped>::new(
&EvictionPolicy {
max_count: 0,
max_seconds: 5,
Expand Down Expand Up @@ -154,7 +154,7 @@ mod evicting_map_tests {

#[tokio::test]
async fn get_refreshes_time() -> Result<(), Error> {
let mut evicting_map = EvictingMap::new(
let mut evicting_map = EvictingMap::<Vec<u8>, MockInstantWrapped>::new(
&EvictionPolicy {
max_count: 0,
max_seconds: 3,
Expand Down Expand Up @@ -193,7 +193,7 @@ mod evicting_map_tests {

#[tokio::test]
async fn contains_key_refreshes_time() -> Result<(), Error> {
let mut evicting_map = EvictingMap::new(
let mut evicting_map = EvictingMap::<Vec<u8>, MockInstantWrapped>::new(
&EvictionPolicy {
max_count: 0,
max_seconds: 3,
Expand Down Expand Up @@ -232,7 +232,7 @@ mod evicting_map_tests {

#[tokio::test]
async fn hashes_equal_sizes_different_doesnt_override() -> Result<(), Error> {
let mut evicting_map = EvictingMap::new(
let mut evicting_map = EvictingMap::<Vec<u8>, Instant>::new(
&EvictionPolicy {
max_count: 0,
max_seconds: 0,
Expand Down

0 comments on commit 791dc67

Please sign in to comment.