|
21 | 21 |
|
22 | 22 | #include <algorithm> |
23 | 23 | #include <iterator> // IWYU pragma: keep |
| 24 | +#include <memory> |
24 | 25 |
|
25 | | -#include "iceberg/exception.h" |
26 | 26 | #include "iceberg/table.h" |
| 27 | +#include "iceberg/table_identifier.h" |
27 | 28 | #include "iceberg/table_metadata.h" |
| 29 | +#include "iceberg/table_requirement.h" |
| 30 | +#include "iceberg/table_update.h" |
28 | 31 | #include "iceberg/util/macros.h" |
29 | 32 |
|
30 | 33 | namespace iceberg { |
@@ -121,6 +124,13 @@ class ICEBERG_EXPORT InMemoryNamespace { |
121 | 124 | /// \return The metadata location if the table exists; error otherwise. |
122 | 125 | Result<std::string> GetTableMetadataLocation(const TableIdentifier& table_ident) const; |
123 | 126 |
|
| 127 | + /// \brief Updates the metadata location for the specified table. |
| 128 | + /// |
| 129 | + /// \param table_ident The identifier of the table. |
| 130 | + /// \param metadata_location The new metadata location. |
| 131 | + Status UpdateTableMetadataLocation(const TableIdentifier& table_ident, |
| 132 | + const std::string& metadata_location); |
| 133 | + |
124 | 134 | /// \brief Internal utility for retrieving a namespace node pointer from the tree. |
125 | 135 | /// |
126 | 136 | /// \tparam NamespacePtr The type of the namespace node pointer. |
@@ -314,6 +324,13 @@ Result<std::string> InMemoryNamespace::GetTableMetadataLocation( |
314 | 324 | return it->second; |
315 | 325 | } |
316 | 326 |
|
| 327 | +Status InMemoryNamespace::UpdateTableMetadataLocation( |
| 328 | + TableIdentifier const& table_ident, std::string const& metadata_location) { |
| 329 | + ICEBERG_ASSIGN_OR_RAISE(auto ns, GetNamespace(this, table_ident.ns)); |
| 330 | + ns->table_metadata_locations_[table_ident.name] = metadata_location; |
| 331 | + return {}; |
| 332 | +} |
| 333 | + |
317 | 334 | std::shared_ptr<InMemoryCatalog> InMemoryCatalog::Make( |
318 | 335 | std::string const& name, std::shared_ptr<FileIO> const& file_io, |
319 | 336 | std::string const& warehouse_location, |
@@ -394,7 +411,32 @@ Result<std::unique_ptr<Table>> InMemoryCatalog::UpdateTable( |
394 | 411 | const TableIdentifier& identifier, |
395 | 412 | const std::vector<std::unique_ptr<TableRequirement>>& requirements, |
396 | 413 | const std::vector<std::unique_ptr<TableUpdate>>& updates) { |
397 | | - return NotImplemented("update table"); |
| 414 | + std::unique_lock lock(mutex_); |
| 415 | + ICEBERG_ASSIGN_OR_RAISE(auto metadata_location, |
| 416 | + root_namespace_->GetTableMetadataLocation(identifier)); |
| 417 | + |
| 418 | + ICEBERG_ASSIGN_OR_RAISE(auto base, |
| 419 | + TableMetadataUtil::Read(*file_io_, metadata_location)); |
| 420 | + base->metadata_file_location = metadata_location; |
| 421 | + |
| 422 | + for (const auto& requirement : requirements) { |
| 423 | + ICEBERG_RETURN_UNEXPECTED(requirement->Validate(base.get())); |
| 424 | + } |
| 425 | + |
| 426 | + auto builder = TableMetadataBuilder::BuildFrom(base.get()); |
| 427 | + for (const auto& update : updates) { |
| 428 | + update->ApplyTo(*builder); |
| 429 | + } |
| 430 | + ICEBERG_ASSIGN_OR_RAISE(auto updated, builder->Build()); |
| 431 | + |
| 432 | + ICEBERG_RETURN_UNEXPECTED( |
| 433 | + TableMetadataUtil::Write(*file_io_, base.get(), updated.get())); |
| 434 | + ICEBERG_RETURN_UNEXPECTED(root_namespace_->UpdateTableMetadataLocation( |
| 435 | + identifier, updated->metadata_file_location)); |
| 436 | + TableMetadataUtil::DeleteRemovedMetadataFiles(*file_io_, base.get(), updated.get()); |
| 437 | + |
| 438 | + return std::make_unique<Table>(identifier, std::move(updated), file_io_, |
| 439 | + std::static_pointer_cast<Catalog>(shared_from_this())); |
398 | 440 | } |
399 | 441 |
|
400 | 442 | Result<std::shared_ptr<Transaction>> InMemoryCatalog::StageCreateTable( |
@@ -435,9 +477,9 @@ Result<std::unique_ptr<Table>> InMemoryCatalog::LoadTable( |
435 | 477 |
|
436 | 478 | ICEBERG_ASSIGN_OR_RAISE(auto metadata, |
437 | 479 | TableMetadataUtil::Read(*file_io_, metadata_location.value())); |
| 480 | + metadata->metadata_file_location = metadata_location.value(); |
438 | 481 |
|
439 | | - return std::make_unique<Table>(identifier, std::move(metadata), |
440 | | - metadata_location.value(), file_io_, |
| 482 | + return std::make_unique<Table>(identifier, std::move(metadata), file_io_, |
441 | 483 | std::static_pointer_cast<Catalog>(shared_from_this())); |
442 | 484 | } |
443 | 485 |
|
|
0 commit comments