wgtmac commented on code in PR #321: URL: https://github.com/apache/iceberg-cpp/pull/321#discussion_r2602600416
########## src/iceberg/base_transaction.h: ########## @@ -0,0 +1,134 @@ +/* + * 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. + */ + +#pragma once + +#include <memory> +#include <vector> + +#include "iceberg/table_identifier.h" +#include "iceberg/table_requirement.h" +#include "iceberg/table_update.h" +#include "iceberg/transaction.h" +#include "iceberg/type_fwd.h" + +namespace iceberg { + +/// \brief Base class for transaction implementations +class ICEBERG_EXPORT BaseTransaction : public Transaction { + public: + BaseTransaction(std::shared_ptr<const Table> table, std::shared_ptr<Catalog> catalog); + ~BaseTransaction() override = default; + + const std::shared_ptr<const Table>& table() const override; + + std::unique_ptr<::iceberg::UpdateProperties> UpdateProperties() override; + + std::unique_ptr<AppendFiles> NewAppend() override; Review Comment: ```suggestion std::shared_ptr<UpdateProperties> NewUpdateProperties() override; std::shared_ptr<AppendFiles> NewAppend() override; ``` It seems that returning shared_ptr will make it easier to collect and transform the update internally. Adding `New` prefix also can avoid `iceberg::` prefix in the return type. ########## src/iceberg/catalog.h: ########## @@ -123,8 +123,8 @@ class ICEBERG_EXPORT Catalog { /// \return a Table instance or ErrorKind::kAlreadyExists if the table already exists virtual Result<std::unique_ptr<Table>> UpdateTable( const TableIdentifier& identifier, - const std::vector<std::unique_ptr<TableRequirement>>& requirements, - const std::vector<std::unique_ptr<TableUpdate>>& updates) = 0; + std::vector<std::unique_ptr<TableRequirement>> requirements, Review Comment: Can we revert this change? If later we support retry on failed commits, these `requirements` and `updates` cannot be reused since they are moved away. ########## src/iceberg/base_transaction.cc: ########## @@ -0,0 +1,146 @@ +/* + * 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/base_transaction.h" + +#include <utility> + +#include "iceberg/catalog.h" +#include "iceberg/pending_update.h" +#include "iceberg/table.h" +#include "iceberg/table_metadata.h" +#include "iceberg/transaction_catalog.h" +#include "iceberg/update/update_properties.h" +#include "iceberg/util/macros.h" + +namespace iceberg { + +BaseTransaction::BaseTransaction(std::shared_ptr<const Table> table, + std::shared_ptr<Catalog> catalog) + : table_(std::move(table)) { + ICEBERG_DCHECK(table_ != nullptr, "table must not be null"); Review Comment: We need to add a `Transaction::Make` function to return `Result<Transaction>` and check these arguments explicitly. ########## src/iceberg/base_transaction.cc: ########## @@ -0,0 +1,146 @@ +/* + * 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/base_transaction.h" + +#include <utility> + +#include "iceberg/catalog.h" +#include "iceberg/pending_update.h" +#include "iceberg/table.h" +#include "iceberg/table_metadata.h" +#include "iceberg/transaction_catalog.h" +#include "iceberg/update/update_properties.h" +#include "iceberg/util/macros.h" + +namespace iceberg { + +BaseTransaction::BaseTransaction(std::shared_ptr<const Table> table, + std::shared_ptr<Catalog> catalog) + : table_(std::move(table)) { + ICEBERG_DCHECK(table_ != nullptr, "table must not be null"); + ICEBERG_DCHECK(catalog != nullptr, "catalog must not be null"); + context_.identifier = table_->name(); + context_.current_metadata = table_->metadata(); + catalog_ = std::make_shared<TransactionCatalog>(std::move(catalog), this); +} + +const std::shared_ptr<const Table>& BaseTransaction::table() const { return table_; } + +std::unique_ptr<UpdateProperties> BaseTransaction::UpdateProperties() { + auto update = CheckAndCreateUpdate<::iceberg::UpdateProperties>( + table_->name(), catalog_, CurrentMetadata()); + if (!update.has_value()) { + ERROR_TO_EXCEPTION(update.error()); + } + + return std::move(update).value(); +} + +std::unique_ptr<AppendFiles> BaseTransaction::NewAppend() { + throw NotImplemented("BaseTransaction::NewAppend not implemented"); +} + +Status BaseTransaction::CommitTransaction() { + if (!HasLastOperationCommitted()) { + return InvalidState("Cannot commit transaction: last operation has not committed"); + } + + auto pending_updates = ConsumePendingUpdates(); + if (pending_updates.empty()) { + return {}; + } + + auto pending_requirements = ConsumePendingRequirements(); + + ICEBERG_ASSIGN_OR_RAISE( + auto updated_table, + catalog_->catalog_impl()->UpdateTable( + table_->name(), std::move(pending_requirements), std::move(pending_updates))); + + // update table to the new version + if (updated_table) { + table_ = std::shared_ptr<Table>(std::move(updated_table)); Review Comment: Why do we need this `table_` after commit has been done? It seems useless? ########## src/iceberg/base_transaction.h: ########## @@ -0,0 +1,134 @@ +/* + * 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. + */ + +#pragma once + +#include <memory> +#include <vector> + +#include "iceberg/table_identifier.h" +#include "iceberg/table_requirement.h" +#include "iceberg/table_update.h" +#include "iceberg/transaction.h" +#include "iceberg/type_fwd.h" + +namespace iceberg { + +/// \brief Base class for transaction implementations +class ICEBERG_EXPORT BaseTransaction : public Transaction { Review Comment: BTW, do we want to introduce `TransactionType` from the Java impl as well? If would be good if we can add this to make it clear. ########## src/iceberg/catalog.h: ########## @@ -184,6 +184,11 @@ class ICEBERG_EXPORT Catalog { /// \return a Table instance or ErrorKind::kAlreadyExists if the table already exists virtual Result<std::shared_ptr<Table>> RegisterTable( const TableIdentifier& identifier, const std::string& metadata_file_location) = 0; + + /// \brief Set whether the last operation in a transaction has been committed + /// + /// \param committed true if the last operation has been committed, false otherwise + virtual void SetLastOperationCommitted(bool committed) = 0; Review Comment: This function is only used by transaction catalog so it should not appear here. ########## src/iceberg/base_transaction.h: ########## @@ -0,0 +1,134 @@ +/* + * 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. + */ + +#pragma once + +#include <memory> +#include <vector> + +#include "iceberg/table_identifier.h" +#include "iceberg/table_requirement.h" +#include "iceberg/table_update.h" +#include "iceberg/transaction.h" +#include "iceberg/type_fwd.h" + +namespace iceberg { + +/// \brief Base class for transaction implementations +class ICEBERG_EXPORT BaseTransaction : public Transaction { + public: + BaseTransaction(std::shared_ptr<const Table> table, std::shared_ptr<Catalog> catalog); + ~BaseTransaction() override = default; + + const std::shared_ptr<const Table>& table() const override; + + std::unique_ptr<::iceberg::UpdateProperties> UpdateProperties() override; + + std::unique_ptr<AppendFiles> NewAppend() override; + + Status CommitTransaction() override; + + /// \brief Stage updates to be applied upon commit + /// + /// \param identifier the table identifier + /// \param requirements the list of table requirements to validate + /// \param updates the list of table updates to apply + /// \return a new Table instance with staged updates applied + Result<std::unique_ptr<Table>> StageUpdates( + const TableIdentifier& identifier, + std::vector<std::unique_ptr<TableRequirement>> requirements, + std::vector<std::unique_ptr<TableUpdate>> updates); + + /// \brief Whether the last operation has been committed + /// + /// \return true if the last operation was committed, false otherwise + bool HasLastOperationCommitted() const { return context_.last_operation_committed; } + + /// \brief Mark the last operation as committed or not + /// + /// \param committed true if the last operation was committed, false otherwise + void SetLastOperationCommitted(bool committed) { + context_.last_operation_committed = committed; + } + + protected: + /// \brief Apply a list of table updates to the current metadata + /// + /// \param updates the list of table updates to apply + /// \return Status::OK if the updates were applied successfully, or an error status + Status ApplyUpdates(const std::vector<std::unique_ptr<TableUpdate>>& updates); + + /// \brief Create a new table update if the last operation has been committed + /// + /// \param args the arguments to forward to the update constructor + /// \return a new UpdateType instance or an error status + template <typename UpdateType, typename... Args> + Result<std::unique_ptr<UpdateType>> CheckAndCreateUpdate(Args&&... args) { Review Comment: This is over-complicated. Let's switch to `std::shared_ptr` for these update types. ########## src/iceberg/table.cc: ########## @@ -114,7 +115,7 @@ std::unique_ptr<UpdateProperties> Table::UpdateProperties() const { } std::unique_ptr<Transaction> Table::NewTransaction() const { - throw NotImplemented("Table::NewTransaction is not implemented"); + return std::make_unique<BaseTransaction>(shared_from_this(), catalog_); Review Comment: I'd recommend directly pass `Table*` to it. Similarly, for `catalog_` we can use `catalog_.get()` to just use pointer. The created transaction object should not outlive the table and catalog objects. It is unnecessary to increase the reference counter of them. I'm open to keep the current design. WDYT @HuaHuaY? ########## src/iceberg/base_transaction.cc: ########## @@ -0,0 +1,146 @@ +/* + * 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/base_transaction.h" + +#include <utility> + +#include "iceberg/catalog.h" +#include "iceberg/pending_update.h" +#include "iceberg/table.h" +#include "iceberg/table_metadata.h" +#include "iceberg/transaction_catalog.h" +#include "iceberg/update/update_properties.h" +#include "iceberg/util/macros.h" + +namespace iceberg { + +BaseTransaction::BaseTransaction(std::shared_ptr<const Table> table, + std::shared_ptr<Catalog> catalog) + : table_(std::move(table)) { + ICEBERG_DCHECK(table_ != nullptr, "table must not be null"); + ICEBERG_DCHECK(catalog != nullptr, "catalog must not be null"); + context_.identifier = table_->name(); + context_.current_metadata = table_->metadata(); + catalog_ = std::make_shared<TransactionCatalog>(std::move(catalog), this); +} + +const std::shared_ptr<const Table>& BaseTransaction::table() const { return table_; } + +std::unique_ptr<UpdateProperties> BaseTransaction::UpdateProperties() { + auto update = CheckAndCreateUpdate<::iceberg::UpdateProperties>( + table_->name(), catalog_, CurrentMetadata()); + if (!update.has_value()) { + ERROR_TO_EXCEPTION(update.error()); Review Comment: We cannot throw. Perhaps we should also use `Result` wrapper for these update return types. ########## src/iceberg/transaction_catalog.h: ########## @@ -0,0 +1,135 @@ +/* + * 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. + */ + +#pragma once + +#include <memory> + +#include "iceberg/catalog.h" +#include "iceberg/result.h" +#include "iceberg/table.h" +#include "iceberg/type_fwd.h" + +namespace iceberg { + +/** + * @brief Lightweight catalog wrapper for BaseTransaction. + * + * For read-only operations, TransactionCatalog simply forwards to the wrapped catalog. + * For mutating calls such as UpdateTable or HasLastOperationCommitted, it delegates back + * to the owning BaseTransaction so staged updates remain private until commit. + */ +class ICEBERG_EXPORT TransactionCatalog : public Catalog { Review Comment: Can we move this class to `base_transaction.cc`? ########## src/iceberg/base_transaction.h: ########## @@ -0,0 +1,134 @@ +/* + * 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. + */ + +#pragma once + +#include <memory> +#include <vector> + +#include "iceberg/table_identifier.h" +#include "iceberg/table_requirement.h" +#include "iceberg/table_update.h" +#include "iceberg/transaction.h" +#include "iceberg/type_fwd.h" + +namespace iceberg { + +/// \brief Base class for transaction implementations +class ICEBERG_EXPORT BaseTransaction : public Transaction { Review Comment: If `BaseTransaction` is the only subclass, we can just implement all features to `Transaction` just like `Table`. ########## src/iceberg/base_transaction.cc: ########## @@ -0,0 +1,146 @@ +/* + * 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/base_transaction.h" + +#include <utility> + +#include "iceberg/catalog.h" +#include "iceberg/pending_update.h" +#include "iceberg/table.h" +#include "iceberg/table_metadata.h" +#include "iceberg/transaction_catalog.h" +#include "iceberg/update/update_properties.h" +#include "iceberg/util/macros.h" + +namespace iceberg { + +BaseTransaction::BaseTransaction(std::shared_ptr<const Table> table, + std::shared_ptr<Catalog> catalog) + : table_(std::move(table)) { + ICEBERG_DCHECK(table_ != nullptr, "table must not be null"); + ICEBERG_DCHECK(catalog != nullptr, "catalog must not be null"); + context_.identifier = table_->name(); + context_.current_metadata = table_->metadata(); + catalog_ = std::make_shared<TransactionCatalog>(std::move(catalog), this); +} + +const std::shared_ptr<const Table>& BaseTransaction::table() const { return table_; } + +std::unique_ptr<UpdateProperties> BaseTransaction::UpdateProperties() { + auto update = CheckAndCreateUpdate<::iceberg::UpdateProperties>( + table_->name(), catalog_, CurrentMetadata()); + if (!update.has_value()) { + ERROR_TO_EXCEPTION(update.error()); + } + + return std::move(update).value(); +} + +std::unique_ptr<AppendFiles> BaseTransaction::NewAppend() { + throw NotImplemented("BaseTransaction::NewAppend not implemented"); +} + +Status BaseTransaction::CommitTransaction() { + if (!HasLastOperationCommitted()) { + return InvalidState("Cannot commit transaction: last operation has not committed"); + } + + auto pending_updates = ConsumePendingUpdates(); + if (pending_updates.empty()) { + return {}; + } + + auto pending_requirements = ConsumePendingRequirements(); + + ICEBERG_ASSIGN_OR_RAISE( + auto updated_table, + catalog_->catalog_impl()->UpdateTable( + table_->name(), std::move(pending_requirements), std::move(pending_updates))); + + // update table to the new version + if (updated_table) { + table_ = std::shared_ptr<Table>(std::move(updated_table)); + } + + return {}; +} + +Result<std::unique_ptr<Table>> BaseTransaction::StageUpdates( + const TableIdentifier& identifier, + std::vector<std::unique_ptr<TableRequirement>> requirements, + std::vector<std::unique_ptr<TableUpdate>> updates) { + if (identifier != context_.identifier) { + return InvalidArgument("Transaction only supports table '{}'", + context_.identifier.name); + } + + if (!context_.current_metadata) { + return InvalidState("Transaction metadata is not initialized"); + } + + if (updates.empty()) { + return std::make_unique<Table>( + context_.identifier, std::make_shared<TableMetadata>(*context_.current_metadata), + table_->location(), table_->io(), catalog_->catalog_impl()); + } + + ICEBERG_RETURN_UNEXPECTED(ApplyUpdates(updates)); + + for (auto& requirement : requirements) { + context_.pending_requirements.emplace_back(std::move(requirement)); + } + for (auto& update : updates) { + context_.pending_updates.emplace_back(std::move(update)); + } + + return std::make_unique<Table>( Review Comment: Why do we need to return a table? ########## src/iceberg/base_transaction.cc: ########## @@ -0,0 +1,146 @@ +/* + * 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/base_transaction.h" + +#include <utility> + +#include "iceberg/catalog.h" +#include "iceberg/pending_update.h" +#include "iceberg/table.h" +#include "iceberg/table_metadata.h" +#include "iceberg/transaction_catalog.h" +#include "iceberg/update/update_properties.h" +#include "iceberg/util/macros.h" + +namespace iceberg { + +BaseTransaction::BaseTransaction(std::shared_ptr<const Table> table, + std::shared_ptr<Catalog> catalog) + : table_(std::move(table)) { + ICEBERG_DCHECK(table_ != nullptr, "table must not be null"); + ICEBERG_DCHECK(catalog != nullptr, "catalog must not be null"); + context_.identifier = table_->name(); + context_.current_metadata = table_->metadata(); + catalog_ = std::make_shared<TransactionCatalog>(std::move(catalog), this); +} + +const std::shared_ptr<const Table>& BaseTransaction::table() const { return table_; } + +std::unique_ptr<UpdateProperties> BaseTransaction::UpdateProperties() { + auto update = CheckAndCreateUpdate<::iceberg::UpdateProperties>( + table_->name(), catalog_, CurrentMetadata()); + if (!update.has_value()) { + ERROR_TO_EXCEPTION(update.error()); + } + + return std::move(update).value(); +} + +std::unique_ptr<AppendFiles> BaseTransaction::NewAppend() { + throw NotImplemented("BaseTransaction::NewAppend not implemented"); +} + +Status BaseTransaction::CommitTransaction() { + if (!HasLastOperationCommitted()) { + return InvalidState("Cannot commit transaction: last operation has not committed"); + } + + auto pending_updates = ConsumePendingUpdates(); + if (pending_updates.empty()) { + return {}; + } + + auto pending_requirements = ConsumePendingRequirements(); Review Comment: What if we need to support retry on failed commits? In the current approach, these two lists are gone. -- 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]
