HuaHuaY commented on code in PR #418: URL: https://github.com/apache/iceberg-cpp/pull/418#discussion_r2629362279
########## 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)); Review Comment: ```suggestion return std::make_shared<Transaction>(std::move(table), kind, auto_commit); ``` ########## src/iceberg/table_metadata.cc: ########## @@ -790,4 +790,12 @@ Result<std::unique_ptr<TableMetadata>> TableMetadataBuilder::Build() { return std::make_unique<TableMetadata>(std::move(impl_->metadata)); } +const std::vector<std::unique_ptr<TableUpdate>>& TableMetadataBuilder::changes() const { + return impl_->changes; +} + +const TableMetadata* TableMetadataBuilder::base() const { return impl_->base; } + +const TableMetadata* TableMetadataBuilder::current() const { return &impl_->metadata; } Review Comment: Return reference instead of pointer so caller doesn't need to think about whether result will be nullptr. `Transaction::current() const` also need to be modified. ########## src/iceberg/table.h: ########## @@ -143,4 +145,39 @@ class ICEBERG_EXPORT Table { std::unique_ptr<class TableMetadataCache> metadata_cache_; }; +class ICEBERG_EXPORT StagedTable : public Table { Review Comment: add `final`? ########## src/iceberg/table.cc: ########## @@ -110,18 +130,78 @@ const std::vector<SnapshotLogEntry>& Table::history() const { return metadata_->snapshot_log; } -std::unique_ptr<UpdateProperties> Table::UpdateProperties() const { - return std::make_unique<iceberg::UpdateProperties>(identifier_, catalog_, metadata_); +const std::shared_ptr<FileIO>& Table::io() const { return io_; } + +const std::shared_ptr<TableMetadata>& Table::metadata() const { return metadata_; } + +const std::shared_ptr<Catalog>& Table::catalog() const { return catalog_; } + +Result<std::unique_ptr<TableScanBuilder>> Table::NewScan() const { + return std::make_unique<TableScanBuilder>(metadata_, io_); } -std::unique_ptr<Transaction> Table::NewTransaction() const { - throw NotImplemented("Table::NewTransaction is not implemented"); +Result<std::shared_ptr<Transaction>> Table::NewTransaction() { + // Create a brand new transaction object for the table. Users are expected to commit the + // transaction manually. + return Transaction::Make(shared_from_this(), Transaction::Kind::kUpdate, + /*auto_commit=*/false); } -const std::shared_ptr<FileIO>& Table::io() const { return io_; } +Result<std::shared_ptr<UpdateProperties>> Table::NewUpdateProperties() { + ICEBERG_ASSIGN_OR_RAISE( + auto transaction, Transaction::Make(shared_from_this(), Transaction::Kind::kUpdate, + /*auto_commit=*/true)); + return transaction->NewUpdateProperties(); +} -std::unique_ptr<TableScanBuilder> Table::NewScan() const { - return std::make_unique<TableScanBuilder>(metadata_, io_); +Result<std::shared_ptr<StagedTable>> StagedTable::Make( + TableIdentifier identifier, std::shared_ptr<TableMetadata> metadata, + std::string metadata_location, std::shared_ptr<FileIO> io, + std::shared_ptr<Catalog> catalog) { + if (metadata == nullptr) [[unlikely]] { + return InvalidArgument("Metadata cannot be null"); + } + if (io == nullptr) [[unlikely]] { + return InvalidArgument("FileIO cannot be null"); + } + if (catalog == nullptr) [[unlikely]] { + return InvalidArgument("Catalog cannot be null"); + } + return std::shared_ptr<StagedTable>( + new StagedTable(std::move(identifier), std::move(metadata), + std::move(metadata_location), std::move(io), std::move(catalog))); +} + +StagedTable::~StagedTable() = default; + +Result<std::unique_ptr<TableScanBuilder>> StagedTable::NewScan() const { + return NotSupported("Cannot scan a staged table"); +} + +Result<std::shared_ptr<StaticTable>> StaticTable::Make( + TableIdentifier identifier, std::shared_ptr<TableMetadata> metadata, + std::string metadata_location, std::shared_ptr<FileIO> io) { + if (metadata == nullptr) [[unlikely]] { + return InvalidArgument("Metadata cannot be null"); + } + if (io == nullptr) [[unlikely]] { + return InvalidArgument("FileIO cannot be null"); + } + return std::shared_ptr<StaticTable>( + new StaticTable(std::move(identifier), std::move(metadata), + std::move(metadata_location), std::move(io), /*catalog=*/nullptr)); Review Comment: ```suggestion return std::make_shared<StaticTable>(std::move(identifier), std::move(metadata), std::move(metadata_location), std::move(io), /*catalog=*/nullptr); ``` ########## src/iceberg/table.cc: ########## @@ -21,16 +21,40 @@ #include "iceberg/catalog.h" #include "iceberg/partition_spec.h" +#include "iceberg/result.h" #include "iceberg/schema.h" #include "iceberg/sort_order.h" #include "iceberg/table_metadata.h" #include "iceberg/table_properties.h" #include "iceberg/table_scan.h" +#include "iceberg/transaction.h" #include "iceberg/update/update_properties.h" #include "iceberg/util/macros.h" namespace iceberg { +Result<std::shared_ptr<Table>> Table::Make(TableIdentifier identifier, + std::shared_ptr<TableMetadata> metadata, + std::string metadata_location, + std::shared_ptr<FileIO> io, + std::shared_ptr<Catalog> catalog) { + if (metadata == nullptr) [[unlikely]] { + return InvalidArgument("Metadata cannot be null"); + } + if (metadata_location.empty()) [[unlikely]] { + return InvalidArgument("Metadata location cannot be empty"); + } + if (io == nullptr) [[unlikely]] { + return InvalidArgument("FileIO cannot be null"); + } + if (catalog == nullptr) [[unlikely]] { + return InvalidArgument("Catalog cannot be null"); + } + return std::shared_ptr<Table>(new Table(std::move(identifier), std::move(metadata), + std::move(metadata_location), std::move(io), + std::move(catalog))); Review Comment: ```suggestion return std::make_shared<Table>(std::move(identifier), std::move(metadata), std::move(metadata_location), std::move(io), std::move(catalog)); ``` ########## src/iceberg/table.cc: ########## @@ -110,18 +130,78 @@ const std::vector<SnapshotLogEntry>& Table::history() const { return metadata_->snapshot_log; } -std::unique_ptr<UpdateProperties> Table::UpdateProperties() const { - return std::make_unique<iceberg::UpdateProperties>(identifier_, catalog_, metadata_); +const std::shared_ptr<FileIO>& Table::io() const { return io_; } + +const std::shared_ptr<TableMetadata>& Table::metadata() const { return metadata_; } + +const std::shared_ptr<Catalog>& Table::catalog() const { return catalog_; } + +Result<std::unique_ptr<TableScanBuilder>> Table::NewScan() const { + return std::make_unique<TableScanBuilder>(metadata_, io_); } -std::unique_ptr<Transaction> Table::NewTransaction() const { - throw NotImplemented("Table::NewTransaction is not implemented"); +Result<std::shared_ptr<Transaction>> Table::NewTransaction() { + // Create a brand new transaction object for the table. Users are expected to commit the + // transaction manually. + return Transaction::Make(shared_from_this(), Transaction::Kind::kUpdate, + /*auto_commit=*/false); } -const std::shared_ptr<FileIO>& Table::io() const { return io_; } +Result<std::shared_ptr<UpdateProperties>> Table::NewUpdateProperties() { + ICEBERG_ASSIGN_OR_RAISE( + auto transaction, Transaction::Make(shared_from_this(), Transaction::Kind::kUpdate, + /*auto_commit=*/true)); + return transaction->NewUpdateProperties(); +} -std::unique_ptr<TableScanBuilder> Table::NewScan() const { - return std::make_unique<TableScanBuilder>(metadata_, io_); +Result<std::shared_ptr<StagedTable>> StagedTable::Make( + TableIdentifier identifier, std::shared_ptr<TableMetadata> metadata, + std::string metadata_location, std::shared_ptr<FileIO> io, + std::shared_ptr<Catalog> catalog) { + if (metadata == nullptr) [[unlikely]] { + return InvalidArgument("Metadata cannot be null"); + } + if (io == nullptr) [[unlikely]] { + return InvalidArgument("FileIO cannot be null"); + } + if (catalog == nullptr) [[unlikely]] { + return InvalidArgument("Catalog cannot be null"); + } + return std::shared_ptr<StagedTable>( + new StagedTable(std::move(identifier), std::move(metadata), + std::move(metadata_location), std::move(io), std::move(catalog))); Review Comment: ```suggestion return std::make_shared<StagedTable>(std::move(identifier), std::move(metadata), std::move(metadata_location), std::move(io), std::move(catalog)); ``` ########## src/iceberg/update/update_properties.cc: ########## @@ -19,28 +19,33 @@ #include "iceberg/update/update_properties.h" +#include <charconv> #include <cstdint> #include <memory> +#include <system_error> -#include "iceberg/catalog.h" #include "iceberg/metrics_config.h" #include "iceberg/result.h" -#include "iceberg/table.h" -#include "iceberg/table_identifier.h" #include "iceberg/table_metadata.h" #include "iceberg/table_properties.h" -#include "iceberg/table_requirements.h" #include "iceberg/table_update.h" +#include "iceberg/transaction.h" #include "iceberg/util/macros.h" namespace iceberg { -UpdateProperties::UpdateProperties(TableIdentifier identifier, - std::shared_ptr<Catalog> catalog, - std::shared_ptr<TableMetadata> base) - : identifier_(std::move(identifier)), - catalog_(std::move(catalog)), - base_metadata_(std::move(base)) {} +Result<std::shared_ptr<UpdateProperties>> UpdateProperties::Make( + std::shared_ptr<Transaction> transaction) { + if (!transaction) [[unlikely]] { + return InvalidArgument("Cannot create UpdateProperties without a transaction"); + } + return std::shared_ptr<UpdateProperties>(new UpdateProperties(std::move(transaction))); Review Comment: I suggest to add some comments if we prefer to use `std::shared_ptr` rather than `std::make_shared` 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: [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]
