Skip to content

Commit

Permalink
Replace use of Range with RangeBounds for DataCache trait (#579)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Carl Jones <djonesoa@amazon.com>
  • Loading branch information
dannycjones authored Oct 26, 2023
1 parent fa0d516 commit 71277e7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
8 changes: 6 additions & 2 deletions mountpoint-s3/src/data_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

pub mod in_memory_data_cache;

use std::ops::Range;
use std::ops::RangeBounds;

use thiserror::Error;

Expand Down Expand Up @@ -50,5 +50,9 @@ pub trait DataCache<Key> {
/// It is possible that the **blocks may be deleted before reading**, or may be corrupted or inaccessible.
/// This method only indicates that a cache entry was present at the time of calling.
/// There is no guarantee that the data will still be available at the time of reading.
fn cached_block_indices(&self, cache_key: &Key, range: Range<BlockIndex>) -> DataCacheResult<Vec<BlockIndex>>;
fn cached_block_indices<R: RangeBounds<BlockIndex>>(
&self,
cache_key: &Key,
range: R,
) -> DataCacheResult<Vec<BlockIndex>>;
}
13 changes: 9 additions & 4 deletions mountpoint-s3/src/data_cache/in_memory_data_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::collections::HashMap;
use std::default::Default;
use std::hash::Hash;
use std::ops::Range;
use std::ops::RangeBounds;

use super::{BlockIndex, ChecksummedBytes, DataCache, DataCacheResult};
use crate::sync::RwLock;
Expand Down Expand Up @@ -42,12 +42,17 @@ impl<Key: Eq + Hash> DataCache<Key> for InMemoryDataCache<Key> {
self.block_size
}

fn cached_block_indices(&self, cache_key: &Key, range: Range<BlockIndex>) -> DataCacheResult<Vec<BlockIndex>> {
fn cached_block_indices<R: RangeBounds<BlockIndex>>(
&self,
cache_key: &Key,
range: R,
) -> DataCacheResult<Vec<BlockIndex>> {
let data = self.data.read().unwrap();
let result = match data.get(cache_key) {
let mut result = match data.get(cache_key) {
None => Vec::new(),
Some(blocks) => range.into_iter().filter(|idx| blocks.contains_key(idx)).collect(),
Some(blocks) => blocks.keys().filter(|idx| range.contains(idx)).copied().collect(),
};
result.sort();

Ok(result)
}
Expand Down

0 comments on commit 71277e7

Please sign in to comment.