From 520acd3b6ea090ff925e62141320cf5c1fc96a4b Mon Sep 17 00:00:00 2001 From: Daniel Carl Jones Date: Thu, 26 Oct 2023 14:48:11 +0100 Subject: [PATCH] Replace use of Range with RangeBounds for DataCache trait Signed-off-by: Daniel Carl Jones --- mountpoint-s3/src/data_cache.rs | 8 ++++++-- .../src/data_cache/in_memory_data_cache.rs | 13 +++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/mountpoint-s3/src/data_cache.rs b/mountpoint-s3/src/data_cache.rs index 8ea85a00a..eb82e5b10 100644 --- a/mountpoint-s3/src/data_cache.rs +++ b/mountpoint-s3/src/data_cache.rs @@ -6,7 +6,7 @@ pub mod in_memory_data_cache; -use std::ops::Range; +use std::ops::RangeBounds; use thiserror::Error; @@ -50,5 +50,9 @@ pub trait DataCache { /// 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) -> DataCacheResult>; + fn cached_block_indices>( + &self, + cache_key: &Key, + range: R, + ) -> DataCacheResult>; } diff --git a/mountpoint-s3/src/data_cache/in_memory_data_cache.rs b/mountpoint-s3/src/data_cache/in_memory_data_cache.rs index 6843b44e2..38404ebe4 100644 --- a/mountpoint-s3/src/data_cache/in_memory_data_cache.rs +++ b/mountpoint-s3/src/data_cache/in_memory_data_cache.rs @@ -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; @@ -42,12 +42,17 @@ impl DataCache for InMemoryDataCache { self.block_size } - fn cached_block_indices(&self, cache_key: &Key, range: Range) -> DataCacheResult> { + fn cached_block_indices>( + &self, + cache_key: &Key, + range: R, + ) -> DataCacheResult> { 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) }