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::cache::PuffinMetadataCacheRef;
25use crate::puffin_manager::file_accessor::PuffinFileAccessor;
26use crate::puffin_manager::stager::Stager;
27use crate::puffin_manager::PuffinManager;
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
61#[async_trait]
62impl<S, F> PuffinManager for FsPuffinManager<S, F>
63where
64    F: PuffinFileAccessor + Clone,
65    S: Stager<FileHandle = F::FileHandle> + Clone + 'static,
66{
67    type Reader = FsPuffinReader<S, F>;
68    type Writer = FsPuffinWriter<S, F::Writer>;
69    type FileHandle = F::FileHandle;
70
71    async fn reader(&self, handle: &Self::FileHandle) -> Result<Self::Reader> {
72        Ok(FsPuffinReader::new(
73            handle.clone(),
74            self.stager.clone(),
75            self.puffin_file_accessor.clone(),
76            self.puffin_metadata_cache.clone(),
77        ))
78    }
79
80    async fn writer(&self, handle: &Self::FileHandle) -> Result<Self::Writer> {
81        let writer = self.puffin_file_accessor.writer(handle).await?;
82        Ok(FsPuffinWriter::new(
83            handle.clone(),
84            self.stager.clone(),
85            writer,
86        ))
87    }
88}