2222#include < algorithm>
2323#include < iterator>
2424#include < mutex>
25+ #include < memory>
2526
2627#include " iceberg/table.h"
28+ #include " iceberg/table_identifier.h"
2729#include " iceberg/table_metadata.h"
30+ #include " iceberg/table_requirement.h"
31+ #include " iceberg/table_update.h"
2832#include " iceberg/util/macros.h"
2933
3034namespace iceberg {
@@ -121,6 +125,13 @@ class ICEBERG_EXPORT InMemoryNamespace {
121125 // / \return The metadata location if the table exists; error otherwise.
122126 Result<std::string> GetTableMetadataLocation (const TableIdentifier& table_ident) const ;
123127
128+ // / \brief Updates the metadata location for the specified table.
129+ // /
130+ // / \param table_ident The identifier of the table.
131+ // / \param metadata_location The new metadata location.
132+ Status UpdateTableMetadataLocation (const TableIdentifier& table_ident,
133+ const std::string& metadata_location);
134+
124135 // / \brief Internal utility for retrieving a namespace node pointer from the tree.
125136 // /
126137 // / \tparam NamespacePtr The type of the namespace node pointer.
@@ -279,7 +290,7 @@ Result<std::vector<std::string>> InMemoryNamespace::ListTables(
279290 return table_names;
280291}
281292
282- Status InMemoryNamespace::RegisterTable (TableIdentifier const & table_ident,
293+ Status InMemoryNamespace::RegisterTable (const TableIdentifier & table_ident,
283294 const std::string& metadata_location) {
284295 const auto ns = GetNamespace (this , table_ident.ns );
285296 ICEBERG_RETURN_UNEXPECTED (ns);
@@ -290,21 +301,21 @@ Status InMemoryNamespace::RegisterTable(TableIdentifier const& table_ident,
290301 return {};
291302}
292303
293- Status InMemoryNamespace::UnregisterTable (TableIdentifier const & table_ident) {
304+ Status InMemoryNamespace::UnregisterTable (const TableIdentifier & table_ident) {
294305 const auto ns = GetNamespace (this , table_ident.ns );
295306 ICEBERG_RETURN_UNEXPECTED (ns);
296307 ns.value ()->table_metadata_locations_ .erase (table_ident.name );
297308 return {};
298309}
299310
300- Result<bool > InMemoryNamespace::TableExists (TableIdentifier const & table_ident) const {
311+ Result<bool > InMemoryNamespace::TableExists (const TableIdentifier & table_ident) const {
301312 const auto ns = GetNamespace (this , table_ident.ns );
302313 ICEBERG_RETURN_UNEXPECTED (ns);
303314 return ns.value ()->table_metadata_locations_ .contains (table_ident.name );
304315}
305316
306317Result<std::string> InMemoryNamespace::GetTableMetadataLocation (
307- TableIdentifier const & table_ident) const {
318+ const TableIdentifier & table_ident) const {
308319 const auto ns = GetNamespace (this , table_ident.ns );
309320 ICEBERG_RETURN_UNEXPECTED (ns);
310321 const auto it = ns.value ()->table_metadata_locations_ .find (table_ident.name );
@@ -314,17 +325,24 @@ Result<std::string> InMemoryNamespace::GetTableMetadataLocation(
314325 return it->second ;
315326}
316327
328+ Status InMemoryNamespace::UpdateTableMetadataLocation (
329+ const TableIdentifier& table_ident, const std::string& metadata_location) {
330+ ICEBERG_ASSIGN_OR_RAISE (auto ns, GetNamespace (this , table_ident.ns ));
331+ ns->table_metadata_locations_ [table_ident.name ] = metadata_location;
332+ return {};
333+ }
334+
317335std::shared_ptr<InMemoryCatalog> InMemoryCatalog::Make (
318- std::string const & name, std::shared_ptr<FileIO> const & file_io,
319- std::string const & warehouse_location,
320- std::unordered_map<std::string, std::string> const & properties) {
336+ const std::string& name, const std::shared_ptr<FileIO>& file_io,
337+ const std::string& warehouse_location,
338+ const std::unordered_map<std::string, std::string>& properties) {
321339 return std::make_shared<InMemoryCatalog>(name, file_io, warehouse_location, properties);
322340}
323341
324342InMemoryCatalog::InMemoryCatalog (
325- std::string const & name, std::shared_ptr<FileIO> const & file_io,
326- std::string const & warehouse_location,
327- std::unordered_map<std::string, std::string> const & properties)
343+ const std::string& name, const std::shared_ptr<FileIO>& file_io,
344+ const std::string& warehouse_location,
345+ const std::unordered_map<std::string, std::string>& properties)
328346 : catalog_name_(std::move(name)),
329347 properties_ (std::move(properties)),
330348 file_io_(std::move(file_io)),
@@ -394,7 +412,32 @@ Result<std::unique_ptr<Table>> InMemoryCatalog::UpdateTable(
394412 const TableIdentifier& identifier,
395413 const std::vector<std::unique_ptr<TableRequirement>>& requirements,
396414 const std::vector<std::unique_ptr<TableUpdate>>& updates) {
397- return NotImplemented (" update table" );
415+ std::lock_guard lock (mutex_);
416+ ICEBERG_ASSIGN_OR_RAISE (auto metadata_location,
417+ root_namespace_->GetTableMetadataLocation (identifier));
418+
419+ ICEBERG_ASSIGN_OR_RAISE (auto base,
420+ TableMetadataUtil::Read (*file_io_, metadata_location));
421+ base->metadata_file_location = std::move (metadata_location);
422+
423+ for (const auto & requirement : requirements) {
424+ ICEBERG_RETURN_UNEXPECTED (requirement->Validate (base.get ()));
425+ }
426+
427+ auto builder = TableMetadataBuilder::BuildFrom (base.get ());
428+ for (const auto & update : updates) {
429+ update->ApplyTo (*builder);
430+ }
431+ ICEBERG_ASSIGN_OR_RAISE (auto updated, builder->Build ());
432+
433+ ICEBERG_RETURN_UNEXPECTED (
434+ TableMetadataUtil::Write (*file_io_, base.get (), updated.get ()));
435+ ICEBERG_RETURN_UNEXPECTED (root_namespace_->UpdateTableMetadataLocation (
436+ identifier, updated->metadata_file_location ));
437+ TableMetadataUtil::DeleteRemovedMetadataFiles (*file_io_, base.get (), updated.get ());
438+
439+ return std::make_unique<Table>(identifier, std::move (updated), file_io_,
440+ std::static_pointer_cast<Catalog>(shared_from_this ()));
398441}
399442
400443Result<std::shared_ptr<Transaction>> InMemoryCatalog::StageCreateTable (
@@ -435,9 +478,9 @@ Result<std::unique_ptr<Table>> InMemoryCatalog::LoadTable(
435478
436479 ICEBERG_ASSIGN_OR_RAISE (auto metadata,
437480 TableMetadataUtil::Read (*file_io_, metadata_location.value ()));
481+ metadata->metadata_file_location = metadata_location.value ();
438482
439- return std::make_unique<Table>(identifier, std::move (metadata),
440- metadata_location.value (), file_io_,
483+ return std::make_unique<Table>(identifier, std::move (metadata), file_io_,
441484 std::static_pointer_cast<Catalog>(shared_from_this ()));
442485}
443486
0 commit comments