|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/base_transaction.h" |
| 21 | + |
| 22 | +#include <utility> |
| 23 | + |
| 24 | +#include "iceberg/catalog.h" |
| 25 | +#include "iceberg/pending_update.h" |
| 26 | +#include "iceberg/table.h" |
| 27 | +#include "iceberg/table_metadata.h" |
| 28 | +#include "iceberg/table_requirements.h" |
| 29 | +#include "iceberg/table_update.h" |
| 30 | + |
| 31 | +namespace iceberg { |
| 32 | + |
| 33 | +BaseTransaction::BaseTransaction(std::shared_ptr<Table> table, |
| 34 | + std::shared_ptr<Catalog> catalog) |
| 35 | + : table_(std::move(table)), catalog_(std::move(catalog)) { |
| 36 | + ICEBERG_DCHECK(table_ != nullptr, "table must not be null"); |
| 37 | + ICEBERG_DCHECK(catalog_ != nullptr, "catalog must not be null"); |
| 38 | +} |
| 39 | + |
| 40 | +const std::shared_ptr<Table>& BaseTransaction::table() const { return table_; } |
| 41 | + |
| 42 | +std::shared_ptr<PropertiesUpdate> BaseTransaction::UpdateProperties() { |
| 43 | + return RegisterUpdate<PropertiesUpdate>(); |
| 44 | +} |
| 45 | + |
| 46 | +std::shared_ptr<AppendFiles> BaseTransaction::NewAppend() { |
| 47 | + throw NotImplemented("BaseTransaction::NewAppend not implemented"); |
| 48 | +} |
| 49 | + |
| 50 | +Status BaseTransaction::CommitTransaction() { |
| 51 | + const auto& metadata = table_->metadata(); |
| 52 | + if (!metadata) { |
| 53 | + return InvalidArgument("Table metadata is null"); |
| 54 | + } |
| 55 | + |
| 56 | + auto builder = TableMetadataBuilder::BuildFrom(metadata.get()); |
| 57 | + for (const auto& pending_update : pending_updates_) { |
| 58 | + if (!pending_update) { |
| 59 | + continue; |
| 60 | + } |
| 61 | + ICEBERG_RETURN_UNEXPECTED(pending_update->Apply(*builder)); |
| 62 | + } |
| 63 | + |
| 64 | + auto table_updates = builder->GetChanges(); |
| 65 | + TableUpdateContext context(metadata.get(), /*is_replace=*/false); |
| 66 | + for (const auto& update : table_updates) { |
| 67 | + ICEBERG_RETURN_UNEXPECTED(update->GenerateRequirements(context)); |
| 68 | + } |
| 69 | + ICEBERG_ASSIGN_OR_RAISE(auto table_requirements, context.Build()); |
| 70 | + |
| 71 | + ICEBERG_ASSIGN_OR_RAISE( |
| 72 | + auto updated_table, |
| 73 | + catalog_->UpdateTable(table_->name(), table_requirements, table_updates)); |
| 74 | + |
| 75 | + if (updated_table) { |
| 76 | + table_ = std::shared_ptr<Table>(std::move(updated_table)); |
| 77 | + } |
| 78 | + |
| 79 | + pending_updates_.clear(); |
| 80 | + return {}; |
| 81 | +} |
| 82 | + |
| 83 | +} // namespace iceberg |
0 commit comments