CTTY commented on code in PR #1606:
URL: https://github.com/apache/iceberg-rust/pull/1606#discussion_r2493237541


##########
crates/iceberg/src/transaction/rewrite_files.rs:
##########
@@ -0,0 +1,375 @@
+// 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 std::sync::Arc;
+
+use async_trait::async_trait;
+use uuid::Uuid;
+
+use super::snapshot::{DefaultManifestProcess, SnapshotProduceOperation, 
SnapshotProducer};
+use super::{ActionCommit, TransactionAction};
+use crate::error::{Error, ErrorKind, Result};
+use crate::spec::{
+    DataContentType, DataFile, ManifestContentType, ManifestEntry, 
ManifestEntryRef, ManifestFile,
+    ManifestStatus, Operation,
+};
+use crate::table::Table;
+use crate::transaction::validate::SnapshotValidator;
+
+/// Transaction action for rewriting files.
+pub struct RewriteFilesAction {
+    commit_uuid: Option<Uuid>,
+    key_metadata: Option<Vec<u8>>,
+    snapshot_properties: HashMap<String, String>,
+    added_data_files: Vec<DataFile>,
+    added_delete_files: Vec<DataFile>,
+    deleted_data_files: Vec<DataFile>,
+    deleted_delete_files: Vec<DataFile>,
+    data_sequence_number: Option<i64>,
+    starting_snapshot_id: Option<i64>,
+}
+
+pub struct RewriteFilesOperation {
+    added_data_files: Vec<DataFile>,
+    added_delete_files: Vec<DataFile>,
+    deleted_data_files: Vec<DataFile>,
+    deleted_delete_files: Vec<DataFile>,
+    starting_snapshot_id: Option<i64>,
+    data_sequence_number: Option<i64>,
+}
+
+impl RewriteFilesAction {
+    pub fn new() -> Self {
+        Self {
+            commit_uuid: None,
+            key_metadata: None,
+            snapshot_properties: Default::default(),
+            added_data_files: vec![],
+            added_delete_files: vec![],
+            deleted_data_files: vec![],
+            deleted_delete_files: vec![],
+            data_sequence_number: None,
+            starting_snapshot_id: None,
+        }
+    }
+
+    /// Add added data files to the snapshot.
+    pub fn add_data_files(
+        mut self,
+        data_files: impl IntoIterator<Item = DataFile>,
+    ) -> Result<Self> {
+        for data_file in data_files {
+            match data_file.content {
+                DataContentType::Data => self.added_data_files.push(data_file),
+                DataContentType::PositionDeletes | 
DataContentType::EqualityDeletes => {
+                    self.added_delete_files.push(data_file)
+                }
+            }
+        }
+        Ok(self)
+    }
+
+    /// Add deleted data files to the snapshot.
+    pub fn delete_data_files(
+        mut self,
+        data_files: impl IntoIterator<Item = DataFile>,
+    ) -> Result<Self> {
+        for data_file in data_files {
+            match data_file.content {
+                DataContentType::Data => 
self.deleted_data_files.push(data_file),
+                DataContentType::PositionDeletes | 
DataContentType::EqualityDeletes => {
+                    self.deleted_delete_files.push(data_file)
+                }
+            }
+        }
+
+        Ok(self)
+    }
+
+    /// Set commit UUID for the snapshot.
+    pub fn set_commit_uuid(mut self, commit_uuid: Uuid) -> Self {
+        self.commit_uuid = Some(commit_uuid);
+        self
+    }
+
+    /// Set key metadata for manifest files.
+    pub fn set_key_metadata(mut self, key_metadata: Vec<u8>) -> Self {
+        self.key_metadata = Some(key_metadata);
+        self
+    }
+
+    /// Set snapshot summary properties.
+    pub fn set_snapshot_properties(mut self, snapshot_properties: 
HashMap<String, String>) -> Self {
+        self.snapshot_properties = snapshot_properties;
+        self
+    }
+
+    /// Set the data sequence number for this rewrite operation.
+    /// The number will be used for all new data files that are added in this 
rewrite.
+    pub fn set_data_sequence_number(mut self, sequence_number: i64) -> Self {
+        self.data_sequence_number = Some(sequence_number);
+        self
+    }
+
+    /// Set the snapshot ID used in any reads for this operation.
+    pub fn set_starting_snapshot_id(mut self, snapshot_id: i64) -> Self {
+        self.starting_snapshot_id = Some(snapshot_id);
+        self
+    }
+}
+
+impl Default for RewriteFilesAction {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+#[async_trait]
+impl TransactionAction for RewriteFilesAction {
+    async fn commit(self: Arc<Self>, table: &Table) -> Result<ActionCommit> {
+        let snapshot_producer = SnapshotProducer::new(
+            table,
+            self.commit_uuid.unwrap_or_else(Uuid::now_v7),
+            self.key_metadata.clone(),
+            self.snapshot_properties.clone(),
+            self.added_data_files.clone(),
+            self.added_delete_files.clone(),
+            self.deleted_data_files.clone(),
+            self.deleted_delete_files.clone(),
+        );
+
+        let rewrite_operation = RewriteFilesOperation {
+            added_data_files: self.added_data_files.clone(),
+            added_delete_files: self.added_delete_files.clone(),
+            deleted_data_files: self.deleted_data_files.clone(),
+            deleted_delete_files: self.deleted_delete_files.clone(),
+            starting_snapshot_id: self.starting_snapshot_id,
+            data_sequence_number: self.data_sequence_number,
+        };
+
+        // todo should be able to configure to use the merge manifest process
+        snapshot_producer
+            .commit(rewrite_operation, DefaultManifestProcess)
+            .await
+    }
+}
+
+fn copy_with_deleted_status(entry: &ManifestEntryRef) -> Result<ManifestEntry> 
{

Review Comment:
   Good point!



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to