From 19a773da47bf64e82caa213d96e2aa73e3d6da94 Mon Sep 17 00:00:00 2001 From: Adam Reichold Date: Mon, 11 Dec 2023 09:55:11 +0100 Subject: [PATCH] Allow cheaply cloning a StoreReader to enable user control over block cache usage. --- src/store/reader.rs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/store/reader.rs b/src/store/reader.rs index 16125a147..f2d652b56 100644 --- a/src/store/reader.rs +++ b/src/store/reader.rs @@ -40,6 +40,15 @@ struct BlockCache { } impl BlockCache { + fn new(cache_num_blocks: usize) -> Self { + Self { + cache: NonZeroUsize::new(cache_num_blocks) + .map(|cache_num_blocks| Mutex::new(LruCache::new(cache_num_blocks))), + cache_hits: Default::default(), + cache_misses: Default::default(), + } + } + fn get_from_cache(&self, pos: usize) -> Option { if let Some(block) = self .cache @@ -128,17 +137,23 @@ impl StoreReader { Ok(StoreReader { decompressor: footer.decompressor, data: data_file, - cache: BlockCache { - cache: NonZeroUsize::new(cache_num_blocks) - .map(|cache_num_blocks| Mutex::new(LruCache::new(cache_num_blocks))), - cache_hits: Default::default(), - cache_misses: Default::default(), - }, + cache: BlockCache::new(cache_num_blocks), skip_index: Arc::new(skip_index), space_usage, }) } + /// Clones the given store reader with an independent block cache of the given size. + pub fn fork_cache(&self, cache_num_blocks: usize) -> Self { + Self { + decompressor: self.decompressor, + data: self.data.clone(), + cache: BlockCache::new(cache_num_blocks), + skip_index: Arc::clone(&self.skip_index), + space_usage: self.space_usage.clone(), + } + } + pub(crate) fn block_checkpoints(&self) -> impl Iterator + '_ { self.skip_index.checkpoints() }