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

Replace use of Range with RangeBounds in DataCache trait #579

Merged
merged 1 commit into from
Oct 26, 2023
Merged
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
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(),
passaro marked this conversation as resolved.
Show resolved Hide resolved
};
result.sort();

Ok(result)
}
Expand Down
Loading