wgtmac commented on code in PR #418:
URL: https://github.com/apache/iceberg-cpp/pull/418#discussion_r2633292729


##########
src/iceberg/transaction.cc:
##########
@@ -0,0 +1,111 @@
+
+/*
+ * 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.
+ */
+#include "iceberg/transaction.h"
+
+#include "iceberg/catalog.h"
+#include "iceberg/table.h"
+#include "iceberg/table_metadata.h"
+#include "iceberg/table_requirement.h"
+#include "iceberg/table_requirements.h"
+#include "iceberg/table_update.h"
+#include "iceberg/update/update_properties.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+Transaction::Transaction(std::shared_ptr<Table> table, Kind kind, bool 
auto_commit)
+    : table_(std::move(table)),
+      kind_(kind),
+      auto_commit_(auto_commit),
+      
metadata_builder_(TableMetadataBuilder::BuildFrom(table_->metadata().get())) {}
+
+Transaction::~Transaction() = default;
+
+Result<std::shared_ptr<Transaction>> Transaction::Make(std::shared_ptr<Table> 
table,
+                                                       Kind kind, bool 
auto_commit) {
+  if (!table || !table->catalog()) [[unlikely]] {
+    return InvalidArgument("Table and catalog cannot be null");
+  }
+  return std::shared_ptr<Transaction>(
+      new Transaction(std::move(table), kind, auto_commit));
+}
+
+const TableMetadata* Transaction::base() const { return 
metadata_builder_->base(); }
+
+const TableMetadata* Transaction::current() const { return 
metadata_builder_->current(); }
+
+Status Transaction::AddUpdate(const std::shared_ptr<PendingUpdate>& update) {
+  if (!last_update_committed_) {
+    return InvalidArgument("Cannot add update when previous update is not 
committed");
+  }
+  pending_updates_.emplace_back(std::weak_ptr<PendingUpdate>(update));
+  last_update_committed_ = false;
+  return {};
+}
+
+Status Transaction::Apply(std::vector<std::unique_ptr<TableUpdate>> updates) {
+  for (const auto& update : updates) {
+    update->ApplyTo(*metadata_builder_);
+  }
+
+  last_update_committed_ = true;
+
+  if (auto_commit_) {
+    ICEBERG_RETURN_UNEXPECTED(Commit());
+  }
+
+  return {};
+}
+
+Result<std::shared_ptr<Table>> Transaction::Commit() {
+  if (!last_update_committed_) {
+    return InvalidArgument(
+        "Cannot commit transaction when previous update is not committed");
+  }
+
+  const auto& updates = metadata_builder_->changes();
+  if (updates.empty()) {
+    return table_;
+  }

Review Comment:
   Now I have a clearer picture of it. For the rest catalog, committing the 
staged table requires adding all initial updates to it: 
https://github.com/apache/iceberg/blob/05998edb8808ae21739072cca17b621925a2081a/core/src/main/java/org/apache/iceberg/rest/RESTSessionCatalog.java#L890.
 We can implement this later before committing the create transaction.



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