puffin/puffin_manager/
fs_puffin_manager.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15mod dir_meta;
16mod reader;
17mod writer;
18
19use async_trait::async_trait;
20pub use reader::FsPuffinReader;
21pub use writer::FsPuffinWriter;
22
23use crate::error::Result;
24use crate::puffin_manager::PuffinManager;
25use crate::puffin_manager::cache::PuffinMetadataCacheRef;
26use crate::puffin_manager::file_accessor::PuffinFileAccessor;
27use crate::puffin_manager::stager::Stager;
28
29/// `FsPuffinManager` is a `PuffinManager` that provides readers and writers for puffin data in filesystem.
30#[derive(Clone)]
31pub struct FsPuffinManager<S, F> {
32    /// The stager.
33    stager: S,
34    /// The puffin file accessor.
35    puffin_file_accessor: F,
36    /// The puffin metadata cache.
37    puffin_metadata_cache: Option<PuffinMetadataCacheRef>,
38}
39
40impl<S, F> FsPuffinManager<S, F> {
41    /// Creates a new `FsPuffinManager` with the specified `stager` and `puffin_file_accessor`,
42    /// and optionally with a `puffin_metadata_cache`.
43    pub fn new(stager: S, puffin_file_accessor: F) -> Self {
44        Self {
45            stager,
46            puffin_file_accessor,
47            puffin_metadata_cache: None,
48        }
49    }
50
51    /// Sets the puffin metadata cache.
52    pub fn with_puffin_metadata_cache(
53        mut self,
54        puffin_metadata_cache: Option<PuffinMetadataCacheRef>,
55    ) -> Self {
56        self.puffin_metadata_cache = puffin_metadata_cache;
57        self
58    }
59
60    pub fn file_accessor(&self) -> &F {
61        &self.puffin_file_accessor
62    }
63}
64
65#[async_trait]
66impl<S, F> PuffinManager for FsPuffinManager<S, F>
67where
68    F: PuffinFileAccessor + Clone,
69    S: Stager<FileHandle = F::FileHandle> + Clone + 'static,
70{
71    type Reader = FsPuffinReader<S, F>;
72    type Writer = FsPuffinWriter<S, F::Writer>;
73    type FileHandle = F::FileHandle;
74
75    async fn reader(&self, handle: &Self::FileHandle) -> Result<Self::Reader> {
76        Ok(FsPuffinReader::new(
77            handle.clone(),
78            self.stager.clone(),
79            self.puffin_file_accessor.clone(),
80            self.puffin_metadata_cache.clone(),
81        ))
82    }
83
84    async fn writer(&self, handle: &Self::FileHandle) -> Result<Self::Writer> {
85        let writer = self.puffin_file_accessor.writer(handle).await?;
86        Ok(FsPuffinWriter::new(
87            handle.clone(),
88            self.stager.clone(),
89            writer,
90        ))
91    }
92}