|
19 | 19 |
|
20 | 20 | #include "iceberg/base_transaction.h" |
21 | 21 |
|
| 22 | +#include <utility> |
| 23 | + |
22 | 24 | #include "iceberg/catalog.h" |
23 | 25 | #include "iceberg/pending_update.h" |
24 | 26 | #include "iceberg/table.h" |
25 | | -#include "iceberg/table_metadata.h" |
26 | | -#include "iceberg/table_requirements.h" |
27 | | -#include "iceberg/table_update.h" |
| 27 | +#include "iceberg/update/update_properties.h" |
| 28 | +#include "iceberg/util/macros.h" |
28 | 29 |
|
29 | 30 | namespace iceberg { |
30 | 31 |
|
31 | 32 | BaseTransaction::BaseTransaction(std::shared_ptr<const Table> table, |
32 | 33 | std::shared_ptr<Catalog> catalog) |
33 | | - : table_(std::move(table)), catalog_(std::move(catalog)) { |
| 34 | + : table_(std::move(table)) { |
34 | 35 | ICEBERG_DCHECK(table_ != nullptr, "table must not be null"); |
35 | | - ICEBERG_DCHECK(catalog_ != nullptr, "catalog must not be null"); |
| 36 | + ICEBERG_DCHECK(catalog != nullptr, "catalog must not be null"); |
| 37 | + context_.identifier = table_->name(); |
| 38 | + context_.current_metadata = table_->metadata(); |
| 39 | + catalog_ = std::make_shared<TransactionCatalog>(std::move(catalog), this); |
36 | 40 | } |
37 | 41 |
|
38 | 42 | const std::shared_ptr<const Table>& BaseTransaction::table() const { return table_; } |
39 | 43 |
|
40 | | -std::shared_ptr<PropertiesUpdate> BaseTransaction::UpdateProperties() { |
41 | | - return RegisterUpdate<PropertiesUpdate>(); |
| 44 | +std::unique_ptr<UpdateProperties> BaseTransaction::UpdateProperties() { |
| 45 | + auto update = CheckAndCreateUpdate<::iceberg::UpdateProperties>( |
| 46 | + table_->name(), catalog_, CurrentMetadata()); |
| 47 | + if (!update.has_value()) { |
| 48 | + ERROR_TO_EXCEPTION(update.error()); |
| 49 | + } |
| 50 | + |
| 51 | + return std::move(update).value(); |
42 | 52 | } |
43 | 53 |
|
44 | | -std::shared_ptr<AppendFiles> BaseTransaction::NewAppend() { |
| 54 | +std::unique_ptr<AppendFiles> BaseTransaction::NewAppend() { |
45 | 55 | throw NotImplemented("BaseTransaction::NewAppend not implemented"); |
46 | 56 | } |
47 | 57 |
|
48 | 58 | Status BaseTransaction::CommitTransaction() { |
49 | | - const auto& metadata = table_->metadata(); |
50 | | - if (!metadata) { |
51 | | - return InvalidArgument("Table metadata is null"); |
| 59 | + if (!HasLastOperationCommitted()) { |
| 60 | + return InvalidState("Cannot commit transaction: last operation has not committed"); |
52 | 61 | } |
53 | 62 |
|
54 | | - auto builder = TableMetadataBuilder::BuildFrom(metadata.get()); |
55 | | - for (const auto& pending_update : pending_updates_) { |
56 | | - if (!pending_update) { |
57 | | - continue; |
58 | | - } |
59 | | - ICEBERG_RETURN_UNEXPECTED(pending_update->Apply(*builder)); |
| 63 | + auto pending_updates = ConsumePendingUpdates(); |
| 64 | + if (pending_updates.empty()) { |
| 65 | + return {}; |
60 | 66 | } |
61 | 67 |
|
62 | | - auto table_updates = builder->GetChanges(); |
63 | | - TableUpdateContext context(metadata.get(), /*is_replace=*/false); |
64 | | - for (const auto& update : table_updates) { |
65 | | - ICEBERG_RETURN_UNEXPECTED(update->GenerateRequirements(context)); |
66 | | - } |
67 | | - ICEBERG_ASSIGN_OR_RAISE(auto table_requirements, context.Build()); |
| 68 | + auto pending_requirements = ConsumePendingRequirements(); |
68 | 69 |
|
69 | 70 | ICEBERG_ASSIGN_OR_RAISE( |
70 | 71 | auto updated_table, |
71 | | - catalog_->UpdateTable(table_->name(), table_requirements, table_updates)); |
| 72 | + catalog_->catalog_impl()->UpdateTable( |
| 73 | + table_->name(), std::move(pending_requirements), std::move(pending_updates))); |
72 | 74 |
|
| 75 | + // update table to the new version |
73 | 76 | if (updated_table) { |
74 | 77 | table_ = std::shared_ptr<Table>(std::move(updated_table)); |
75 | 78 | } |
76 | 79 |
|
77 | | - pending_updates_.clear(); |
78 | 80 | return {}; |
79 | 81 | } |
80 | 82 |
|
| 83 | +Result<std::unique_ptr<Table>> BaseTransaction::StageUpdates( |
| 84 | + const TableIdentifier& identifier, |
| 85 | + std::vector<std::unique_ptr<TableRequirement>> requirements, |
| 86 | + std::vector<std::unique_ptr<TableUpdate>> updates) { |
| 87 | + if (identifier != context_.identifier) { |
| 88 | + return InvalidArgument("Transaction only supports table '{}'", |
| 89 | + context_.identifier.name); |
| 90 | + } |
| 91 | + |
| 92 | + if (!context_.current_metadata) { |
| 93 | + return InvalidState("Transaction metadata is not initialized"); |
| 94 | + } |
| 95 | + |
| 96 | + if (updates.empty()) { |
| 97 | + return std::make_unique<Table>( |
| 98 | + context_.identifier, std::make_shared<TableMetadata>(*context_.current_metadata), |
| 99 | + table_->location(), table_->io(), catalog_->catalog_impl()); |
| 100 | + } |
| 101 | + |
| 102 | + ICEBERG_RETURN_UNEXPECTED(ApplyUpdates(updates)); |
| 103 | + |
| 104 | + for (auto& requirement : requirements) { |
| 105 | + context_.pending_requirements.emplace_back(std::move(requirement)); |
| 106 | + } |
| 107 | + for (auto& update : updates) { |
| 108 | + context_.pending_updates.emplace_back(std::move(update)); |
| 109 | + } |
| 110 | + |
| 111 | + return std::make_unique<Table>( |
| 112 | + context_.identifier, std::make_shared<TableMetadata>(*context_.current_metadata), |
| 113 | + table_->location(), table_->io(), catalog_->catalog_impl()); |
| 114 | +} |
| 115 | + |
| 116 | +Status BaseTransaction::ApplyUpdates( |
| 117 | + const std::vector<std::unique_ptr<TableUpdate>>& updates) { |
| 118 | + if (updates.empty()) { |
| 119 | + return {}; |
| 120 | + } |
| 121 | + |
| 122 | + auto builder = TableMetadataBuilder::BuildFrom(context_.current_metadata.get()); |
| 123 | + for (const auto& update : updates) { |
| 124 | + if (!update) { |
| 125 | + continue; |
| 126 | + } |
| 127 | + update->ApplyTo(*builder); |
| 128 | + } |
| 129 | + |
| 130 | + ICEBERG_ASSIGN_OR_RAISE(auto new_metadata, builder->Build()); |
| 131 | + context_.current_metadata = std::shared_ptr<TableMetadata>(std::move(new_metadata)); |
| 132 | + return {}; |
| 133 | +} |
| 134 | + |
| 135 | +std::vector<std::unique_ptr<TableRequirement>> |
| 136 | +BaseTransaction::ConsumePendingRequirements() { |
| 137 | + return std::exchange(context_.pending_requirements, {}); |
| 138 | +} |
| 139 | + |
| 140 | +std::vector<std::unique_ptr<TableUpdate>> BaseTransaction::ConsumePendingUpdates() { |
| 141 | + return std::exchange(context_.pending_updates, {}); |
| 142 | +} |
| 143 | + |
81 | 144 | } // namespace iceberg |
0 commit comments