puffin/file_format/
writer.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 file;
16mod footer;
17
18use std::collections::HashMap;
19
20use async_trait::async_trait;
21
22use crate::blob_metadata::CompressionCodec;
23use crate::error::Result;
24pub use crate::file_format::writer::file::PuffinFileWriter;
25
26/// Blob ready to be written
27pub struct Blob<R> {
28    // TODO(zhongzc): ignore `input_fields`, `snapshot_id`, `sequence_number`
29    // and `compression_codec` for now to keep thing simple
30    /// The type of the blob
31    pub blob_type: String,
32
33    /// The data of the blob
34    pub compressed_data: R,
35
36    /// The codec used to compress the blob.
37    pub compression_codec: Option<CompressionCodec>,
38
39    /// The properties of the blob
40    pub properties: HashMap<String, String>,
41}
42
43/// `SyncWriter` defines a synchronous writer for puffin data.
44pub trait SyncWriter {
45    /// Set the properties of the Puffin file
46    fn set_properties(&mut self, properties: HashMap<String, String>);
47
48    /// Sets whether the footer payload should be LZ4 compressed.
49    fn set_footer_lz4_compressed(&mut self, lz4_compressed: bool);
50
51    /// Add a blob to the Puffin file
52    fn add_blob<R: std::io::Read>(&mut self, blob: Blob<R>) -> Result<u64>;
53
54    /// Finish writing the Puffin file, returns the number of bytes written
55    fn finish(&mut self) -> Result<u64>;
56}
57
58/// `AsyncWriter` defines an asynchronous writer for puffin data.
59#[async_trait]
60pub trait AsyncWriter {
61    /// Set the properties of the Puffin file
62    fn set_properties(&mut self, properties: HashMap<String, String>);
63
64    /// Sets whether the footer payload should be LZ4 compressed.
65    fn set_footer_lz4_compressed(&mut self, lz4_compressed: bool);
66
67    /// Add a blob to the Puffin file
68    async fn add_blob<R: futures::AsyncRead + Send>(&mut self, blob: Blob<R>) -> Result<u64>;
69
70    /// Finish writing the Puffin file, returns the number of bytes written
71    async fn finish(&mut self) -> Result<u64>;
72}