liurenjie1024 commented on code in PR #1400:
URL: https://github.com/apache/iceberg-rust/pull/1400#discussion_r2131841166


##########
crates/iceberg/src/transaction/action/mod.rs:
##########
@@ -0,0 +1,72 @@
+// 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::sync::Arc;
+
+use async_trait::async_trait;
+
+use crate::transaction::Transaction;
+use crate::{Error, ErrorKind, Result, TableRequirement, TableUpdate};
+
+/// TODO doc
+pub type BoxedTransactionAction = Arc<dyn TransactionAction>;
+
+/// TODO doc
+#[async_trait]
+pub trait TransactionAction: Sync + Send {
+    /// Commit the changes and apply the changes to the transaction
+    async fn commit(self: Arc<Self>, tx: &mut Transaction) -> Result<()>;

Review Comment:
   ```suggestion
       async fn commit(self: Arc<Self>, tx: &Table) -> Result<ActionCommit>;
   ```
   
   We should not change transaction here. Remember that we may need to do this 
commit more than once(due to commit confliction). Returning `ActionCommit` make 
this api easier to be idempotent



##########
crates/iceberg/src/transaction/append.rs:
##########
@@ -16,43 +16,45 @@
 // under the License.
 
 use std::collections::{HashMap, HashSet};
+use std::sync::Arc;
 
 use arrow_array::StringArray;
+use async_trait::async_trait;
 use futures::TryStreamExt;
+use futures::lock::Mutex;
 use uuid::Uuid;
 
 use crate::error::Result;
 use crate::spec::{DataFile, ManifestEntry, ManifestFile, Operation};
 use crate::transaction::Transaction;
+use crate::transaction::action::TransactionAction;
 use crate::transaction::snapshot::{
     DefaultManifestProcess, SnapshotProduceAction, SnapshotProduceOperation,
 };
 use crate::writer::file_writer::ParquetWriter;
 use crate::{Error, ErrorKind};
 
 /// FastAppendAction is a transaction action for fast append data files to the 
table.
-pub struct FastAppendAction<'a> {
-    snapshot_produce_action: SnapshotProduceAction<'a>,
+pub struct FastAppendAction {
+    snapshot_produce_action: Mutex<SnapshotProduceAction>,

Review Comment:
   I don't think we should do it this way. Remember that `commit` may be called 
several times, and should be idempotent. You can store the changes like 
`added_files: Vec<DataFile>`, `props: HashMap<String, String>` in 
`FastAppendAction`. And create a new `SnapshotProduceAction`, executing these 
changes when commit.



##########
crates/iceberg/src/transaction/action/mod.rs:
##########
@@ -0,0 +1,67 @@
+// 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::sync::Arc;
+
+use async_trait::async_trait;
+
+use crate::transaction::Transaction;
+use crate::{Error, ErrorKind, Result, TableRequirement, TableUpdate};
+
+pub type BoxedTransactionAction = Arc<dyn TransactionAction>;
+
+#[async_trait]
+pub(crate) trait TransactionAction: Sync + Send {
+    /// Commit the changes and apply the changes to the transaction,
+    /// return the transaction with the updated current_table
+    fn commit(self: Arc<Self>, tx: &mut Transaction) -> Result<()>;

Review Comment:
   I don't think so. `Transaction` doesn't need to keep the stagin table, we 
just need to keep a staging table as a temporary variable. Please remember that 
the `commit` method will be called several times due to retry.



##########
crates/iceberg/src/transaction/snapshot.rs:
##########
@@ -72,16 +72,14 @@ pub(crate) struct SnapshotProduceAction<'a> {
     manifest_counter: RangeFrom<u64>,
 }
 
-impl<'a> SnapshotProduceAction<'a> {
+impl SnapshotProduceAction {
     pub(crate) fn new(
-        tx: Transaction<'a>,

Review Comment:
   `TransactionAction`'s `commit` method should not be called by user.



##########
crates/iceberg/src/transaction/action/mod.rs:
##########
@@ -0,0 +1,72 @@
+// 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::sync::Arc;
+
+use async_trait::async_trait;
+
+use crate::transaction::Transaction;
+use crate::{Error, ErrorKind, Result, TableRequirement, TableUpdate};
+
+/// TODO doc
+pub type BoxedTransactionAction = Arc<dyn TransactionAction>;
+
+/// TODO doc
+#[async_trait]
+pub trait TransactionAction: Sync + Send {
+    /// Commit the changes and apply the changes to the transaction
+    async fn commit(self: Arc<Self>, tx: &mut Transaction) -> Result<()>;
+}
+
+/// TODO doc
+pub struct UpdateLocationAction {
+    location: Option<String>,
+}
+
+impl UpdateLocationAction {
+    /// TODO doc
+    pub fn new() -> Self {
+        UpdateLocationAction { location: None }
+    }
+
+    /// TODO doc
+    pub fn set_location(mut self, location: String) -> Self {
+        self.location = Some(location);
+        self
+    }
+}
+
+#[async_trait]
+impl TransactionAction for UpdateLocationAction {
+    async fn commit(self: Arc<Self>, tx: &mut Transaction) -> Result<()> {
+        let updates: Vec<TableUpdate>;
+        let requirements: Vec<TableRequirement>;
+        if let Some(location) = self.location.clone() {
+            updates = vec![TableUpdate::SetLocation { location }];
+            requirements = vec![];
+        } else {
+            return Err(Error::new(
+                ErrorKind::DataInvalid,
+                "Location is not set for SetLocation!",
+            ));
+        }
+
+        tx.actions.push(self);
+
+        tx.apply(updates, requirements)

Review Comment:
   This is incorrect, please see the comment above. We just need to return 
`ActionCommit` here.



-- 
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