Skip to main content

vortex_file/segments/
cache.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::sync::Arc;
5
6use async_trait::async_trait;
7use vortex_buffer::ByteBuffer;
8use vortex_error::VortexResult;
9use vortex_layout::segments::SegmentCache;
10use vortex_layout::segments::SegmentId;
11use vortex_utils::aliases::hash_map::HashMap;
12
13/// Segment cache containing the initial read segments.
14pub struct InitialReadSegmentCache {
15    /// Segments that were already covered by the footer initial read.
16    pub initial: HashMap<SegmentId, ByteBuffer>,
17    /// Delegate cache used for all misses and stores.
18    pub fallback: Arc<dyn SegmentCache>,
19}
20
21#[async_trait]
22impl SegmentCache for InitialReadSegmentCache {
23    async fn get(&self, id: SegmentId) -> VortexResult<Option<ByteBuffer>> {
24        if let Some(buffer) = self.initial.get(&id) {
25            return Ok(Some(buffer.clone()));
26        }
27        self.fallback.get(id).await
28    }
29
30    async fn put(&self, id: SegmentId, buffer: ByteBuffer) -> VortexResult<()> {
31        self.fallback.put(id, buffer).await
32    }
33}