fqaiser94 commented on code in PR #959:
URL: https://github.com/apache/iceberg-rust/pull/959#discussion_r2021944915


##########
crates/iceberg/src/puffin/writer.rs:
##########
@@ -0,0 +1,394 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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, HashSet};
+
+use bytes::Bytes;
+use futures::lock::Mutex;
+
+use crate::io::{FileWrite, OutputFile};
+use crate::puffin::blob::Blob;
+use crate::puffin::compression::CompressionCodec;
+use crate::puffin::metadata::{BlobMetadata, FileMetadata, Flag};
+use crate::Result;
+
+struct WriterState {
+    writer: Box<dyn FileWrite>,
+    is_header_written: bool,
+    num_bytes_written: u64,
+}
+
+/// Puffin writer
+pub(crate) struct PuffinWriter {
+    writer_state: Mutex<WriterState>,
+    written_blobs_metadata: Vec<BlobMetadata>,
+    properties: HashMap<String, String>,
+    footer_compression_codec: CompressionCodec,
+    flags: HashSet<Flag>,
+}
+
+impl PuffinWriter {
+    /// Returns a new Puffin writer
+    pub(crate) async fn new(
+        output_file: &OutputFile,
+        properties: HashMap<String, String>,
+        compress_footer: bool,
+    ) -> Result<Self> {
+        let mut flags = HashSet::<Flag>::new();
+        let footer_compression_codec = if compress_footer {
+            flags.insert(Flag::FooterPayloadCompressed);
+            CompressionCodec::Lz4
+        } else {
+            CompressionCodec::None
+        };
+
+        let initial_state = WriterState {
+            writer: output_file.writer().await?,
+            is_header_written: false,
+            num_bytes_written: 0,
+        };
+
+        Ok(Self {
+            writer_state: Mutex::new(initial_state),
+            written_blobs_metadata: Vec::new(),
+            properties,
+            footer_compression_codec,
+            flags,
+        })
+    }
+
+    /// Adds blob to Puffin file
+    pub(crate) async fn add(
+        &mut self,
+        blob: Blob,
+        compression_codec: CompressionCodec,
+    ) -> Result<()> {
+        let mut writer_state = self.writer_state.lock().await;

Review Comment:
   You're right, I don't think the lock is necessary after all. 
   
   I think I started overthinking/getting-confused when I saw [`TrackWriter` 
implementation](https://github.com/apache/iceberg-rust/blob/5cdd6eb1d6f7352e6da6e63406bd488c7d322858/crates/iceberg/src/writer/file_writer/track_writer.rs#L27)
 which is using an [atomic 
variable](https://github.com/apache/iceberg-rust/blob/5cdd6eb1d6f7352e6da6e63406bd488c7d322858/crates/iceberg/src/writer/file_writer/track_writer.rs#L29)
 to track the bytes written to disk already and wondered if the atomic 
variables were being used to avoid concurrency issues. Seems to me that we 
could have just used a plain i64 instead of AtomicI64 for that usecase as well? 
   
   EDIT: Ah, went back in the code history and I see we're using an AtomicI64 
there deliberately to be able to access that variable outside of `TrackWriter` 
as well (see comment 
[here](https://github.com/apache/iceberg-rust/pull/176/files#r1484005310)). 
Coolcoolcool, we don't need that here, can go with just a plain u64 and rely on 
`&mut` to prevent concurrent calls to `add` method. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to