Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(puffin): introduce puffin manager trait #4195

Merged
merged 4 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/puffin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub mod error;
pub mod file_format;
pub mod file_metadata;
pub mod partial_reader;
pub mod puffin_manager;

#[cfg(test)]
mod tests;
73 changes: 73 additions & 0 deletions src/puffin/src/puffin_manager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2023 Greptime Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::path::PathBuf;

use async_trait::async_trait;
use futures::{AsyncRead, AsyncSeek};

use crate::blob_metadata::CompressionCodec;
use crate::error::Result;

/// The `PuffinManager` trait provides a unified interface for creating `PuffinReader` and `PuffinWriter`.
#[async_trait]
pub trait PuffinManager {
type Reader: PuffinReader;
type Writer: PuffinWriter;

/// Creates a `PuffinReader` for the specified `puffin_file_name`.
async fn reader(&self, puffin_file_name: &str) -> Result<Self::Reader>;

/// Creates a `PuffinWriter` for the specified `puffin_file_name`.
async fn writer(&self, puffin_file_name: &str) -> Result<Self::Writer>;
}

/// The `PuffinWriter` trait provides methods for writing blobs and directories to a Puffin file.
#[async_trait]
pub trait PuffinWriter {
/// Writes a blob associated with the specified `key` to the Puffin file.
async fn put_blob<R>(&mut self, key: &str, raw_data: R, options: PutOptions) -> Result<u64>
where
R: AsyncRead + Send;

/// Writes a directory associated with the specified `key` to the Puffin file.
/// The specified `dir` should be accessible from the filesystem.
async fn put_dir(&mut self, key: &str, dir: PathBuf, options: PutOptions) -> Result<u64>;

/// Sets whether the footer should be LZ4 compressed.
fn set_footer_lz4_compressed(&mut self, lz4_compressed: bool);

/// Finalizes the Puffin file after writing.
async fn finish(self) -> Result<u64>;
}
zhongzc marked this conversation as resolved.
Show resolved Hide resolved

/// Options available for `put_blob` and `put_dir` methods.
#[derive(Debug, Clone, Default)]
pub struct PutOptions {
/// The compression codec to use for blob data.
pub compression: Option<CompressionCodec>,
}
zhongzc marked this conversation as resolved.
Show resolved Hide resolved

/// The `PuffinReader` trait provides methods for reading blobs and directories from a Puffin file.
#[async_trait]
pub trait PuffinReader {
type Reader: AsyncRead + AsyncSeek;

/// Reads a blob from the Puffin file.
async fn blob(&self, key: &str) -> Result<Self::Reader>;

/// Reads a directory from the Puffin file.
/// The returned `PathBuf` is used to access the directory in the filesystem.
async fn dir(&self, key: &str) -> Result<PathBuf>;
}
Loading