-
Notifications
You must be signed in to change notification settings - Fork 483
feat(puffin): add file writer #2776
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
Merged
zhongzc
merged 10 commits into
GreptimeTeam:develop
from
zhongzc:zhongzc/puffin-format-writer
Nov 21, 2023
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8b1b918
feat(puffin): add file writer
zhongzc 831290c
Update src/puffin/src/file_format/writer/file.rs
zhongzc ba73e11
Update src/puffin/src/file_format/writer/file.rs
zhongzc 0ab1fe0
feat: footer bytes with capacity
zhongzc e58859f
feat: footer bytes with capacity
zhongzc a2a2f44
Update src/puffin/src/file_format/writer.rs
zhongzc 7c89849
feat: add flush
zhongzc b39f0ff
chore: specify default flags
zhongzc cbf2799
feat: close async writer
zhongzc daa8b46
Merge remote-tracking branch 'origin/develop' into zhongzc/puffin-for…
zhongzc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // 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://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. | ||
|
|
||
| mod file; | ||
| mod footer; | ||
|
|
||
| use std::collections::HashMap; | ||
|
|
||
| use async_trait::async_trait; | ||
|
|
||
| use crate::error::Result; | ||
| pub use crate::file_format::writer::file::PuffinFileWriter; | ||
|
|
||
| /// Blob ready to be written | ||
| pub struct Blob<R> { | ||
| // TODO(zhongzc): ignore `input_fields`, `snapshot_id`, `sequence_number` | ||
| // and `compression_codec` for now to keep thing simple | ||
| /// The type of the blob | ||
| pub blob_type: String, | ||
|
|
||
| /// The data of the blob | ||
| pub data: R, | ||
|
|
||
| /// The properties of the blob | ||
| pub properties: HashMap<String, String>, | ||
| } | ||
|
|
||
| /// The trait for writing Puffin files synchronously | ||
| pub trait PuffinSyncWriter { | ||
| /// Set the properties of the Puffin file | ||
| fn set_properties(&mut self, properties: HashMap<String, String>); | ||
|
|
||
| /// Add a blob to the Puffin file | ||
| fn add_blob<R: std::io::Read>(&mut self, blob: Blob<R>) -> Result<()>; | ||
|
|
||
| /// Finish writing the Puffin file | ||
| fn finish(&mut self) -> Result<()>; | ||
| } | ||
|
|
||
| /// The trait for writing Puffin files asynchronously | ||
| #[async_trait] | ||
| pub trait PuffinAsyncWriter { | ||
| /// Set the properties of the Puffin file | ||
| fn set_properties(&mut self, properties: HashMap<String, String>); | ||
|
|
||
| /// Add a blob to the Puffin file | ||
| async fn add_blob<R: futures::AsyncRead + Send>(&mut self, blob: Blob<R>) -> Result<()>; | ||
|
|
||
| /// Finish writing the Puffin file | ||
| async fn finish(&mut self) -> Result<()>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| // 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://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::collections::HashMap; | ||
| use std::{io, mem}; | ||
|
|
||
| use async_trait::async_trait; | ||
| use futures::{AsyncRead, AsyncWrite, AsyncWriteExt}; | ||
| use snafu::ResultExt; | ||
|
|
||
| use crate::blob_metadata::{BlobMetadata, BlobMetadataBuilder}; | ||
| use crate::error::{CloseSnafu, FlushSnafu, Result, WriteSnafu}; | ||
| use crate::file_format::writer::footer::FooterWriter; | ||
| use crate::file_format::writer::{Blob, PuffinAsyncWriter, PuffinSyncWriter}; | ||
| use crate::file_format::MAGIC; | ||
|
|
||
| /// Puffin file writer, implements both [`PuffinSyncWriter`] and [`PuffinAsyncWriter`] | ||
| pub struct PuffinFileWriter<W> { | ||
| /// The writer to write to | ||
| writer: W, | ||
|
|
||
| /// The properties of the file | ||
| properties: HashMap<String, String>, | ||
|
|
||
| /// The metadata of the blobs | ||
| blob_metadata: Vec<BlobMetadata>, | ||
|
|
||
| /// The offset of the next blob | ||
| next_blob_offset: u64, | ||
| } | ||
|
|
||
| impl<W> PuffinFileWriter<W> { | ||
| pub fn new(writer: W) -> Self { | ||
| Self { | ||
| writer, | ||
| properties: HashMap::new(), | ||
| blob_metadata: Vec::new(), | ||
| next_blob_offset: 0, | ||
| } | ||
| } | ||
|
|
||
| fn create_blob_metadata( | ||
| &self, | ||
| typ: String, | ||
| properties: HashMap<String, String>, | ||
| size: u64, | ||
| ) -> BlobMetadata { | ||
| BlobMetadataBuilder::default() | ||
| .blob_type(typ) | ||
| .properties(properties) | ||
| .offset(self.next_blob_offset as _) | ||
| .length(size as _) | ||
| .build() | ||
| .expect("Required fields are not set") | ||
| } | ||
| } | ||
|
|
||
| impl<W: io::Write> PuffinSyncWriter for PuffinFileWriter<W> { | ||
| fn set_properties(&mut self, properties: HashMap<String, String>) { | ||
| self.properties = properties; | ||
| } | ||
|
|
||
| fn add_blob<R: io::Read>(&mut self, mut blob: Blob<R>) -> Result<()> { | ||
| self.write_header_if_needed_sync()?; | ||
|
|
||
| let size = io::copy(&mut blob.data, &mut self.writer).context(WriteSnafu)?; | ||
|
|
||
| let blob_metadata = self.create_blob_metadata(blob.blob_type, blob.properties, size); | ||
| self.blob_metadata.push(blob_metadata); | ||
|
|
||
| self.next_blob_offset += size; | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn finish(&mut self) -> Result<()> { | ||
|
killme2008 marked this conversation as resolved.
|
||
| self.write_header_if_needed_sync()?; | ||
| self.write_footer_sync()?; | ||
| self.writer.flush().context(FlushSnafu) | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl<W: AsyncWrite + Unpin + Send> PuffinAsyncWriter for PuffinFileWriter<W> { | ||
| fn set_properties(&mut self, properties: HashMap<String, String>) { | ||
| self.properties = properties; | ||
| } | ||
|
|
||
| async fn add_blob<R: AsyncRead + Send>(&mut self, blob: Blob<R>) -> Result<()> { | ||
| self.write_header_if_needed_async().await?; | ||
|
|
||
| let size = futures::io::copy(blob.data, &mut self.writer) | ||
| .await | ||
| .context(WriteSnafu)?; | ||
|
|
||
| let blob_metadata = self.create_blob_metadata(blob.blob_type, blob.properties, size); | ||
| self.blob_metadata.push(blob_metadata); | ||
|
|
||
| self.next_blob_offset += size; | ||
| Ok(()) | ||
| } | ||
|
|
||
| async fn finish(&mut self) -> Result<()> { | ||
| self.write_header_if_needed_async().await?; | ||
| self.write_footer_async().await?; | ||
| self.writer.flush().await.context(FlushSnafu)?; | ||
| self.writer.close().await.context(CloseSnafu) | ||
| } | ||
| } | ||
|
|
||
| impl<W: io::Write> PuffinFileWriter<W> { | ||
| fn write_header_if_needed_sync(&mut self) -> Result<()> { | ||
| if self.next_blob_offset == 0 { | ||
| self.writer.write_all(&MAGIC).context(WriteSnafu)?; | ||
| self.next_blob_offset += MAGIC.len() as u64; | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn write_footer_sync(&mut self) -> Result<()> { | ||
| let bytes = FooterWriter::new( | ||
| mem::take(&mut self.blob_metadata), | ||
| mem::take(&mut self.properties), | ||
| ) | ||
| .into_footer_bytes()?; | ||
|
|
||
| self.writer.write_all(&bytes).context(WriteSnafu) | ||
| } | ||
| } | ||
|
|
||
| impl<W: AsyncWrite + Unpin> PuffinFileWriter<W> { | ||
| async fn write_header_if_needed_async(&mut self) -> Result<()> { | ||
| if self.next_blob_offset == 0 { | ||
| self.writer.write_all(&MAGIC).await.context(WriteSnafu)?; | ||
| self.next_blob_offset += MAGIC.len() as u64; | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| async fn write_footer_async(&mut self) -> Result<()> { | ||
| let bytes = FooterWriter::new( | ||
| mem::take(&mut self.blob_metadata), | ||
| mem::take(&mut self.properties), | ||
| ) | ||
| .into_footer_bytes()?; | ||
|
|
||
| self.writer.write_all(&bytes).await.context(WriteSnafu) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.