2323#include < iterator>
2424
2525#include " iceberg/table.h"
26+ #include " iceberg/table_identifier.h"
2627#include " iceberg/table_metadata.h"
28+ #include " iceberg/table_requirement.h"
29+ #include " iceberg/table_update.h"
2730#include " iceberg/util/macros.h"
2831
2932namespace iceberg {
@@ -120,6 +123,13 @@ class ICEBERG_EXPORT InMemoryNamespace {
120123 // / \return The metadata location if the table exists; error otherwise.
121124 Result<std::string> GetTableMetadataLocation (const TableIdentifier& table_ident) const ;
122125
126+ // / \brief Updates the metadata location for the specified table.
127+ // /
128+ // / \param table_ident The identifier of the table.
129+ // / \param metadata_location The new metadata location.
130+ Status UpdateTableMetadataLocation (const TableIdentifier& table_ident,
131+ const std::string& metadata_location);
132+
123133 // / \brief Internal utility for retrieving a namespace node pointer from the tree.
124134 // /
125135 // / \tparam NamespacePtr The type of the namespace node pointer.
@@ -278,7 +288,7 @@ Result<std::vector<std::string>> InMemoryNamespace::ListTables(
278288 return table_names;
279289}
280290
281- Status InMemoryNamespace::RegisterTable (TableIdentifier const & table_ident,
291+ Status InMemoryNamespace::RegisterTable (const TableIdentifier & table_ident,
282292 const std::string& metadata_location) {
283293 const auto ns = GetNamespace (this , table_ident.ns );
284294 ICEBERG_RETURN_UNEXPECTED (ns);
@@ -289,21 +299,21 @@ Status InMemoryNamespace::RegisterTable(TableIdentifier const& table_ident,
289299 return {};
290300}
291301
292- Status InMemoryNamespace::UnregisterTable (TableIdentifier const & table_ident) {
302+ Status InMemoryNamespace::UnregisterTable (const TableIdentifier & table_ident) {
293303 const auto ns = GetNamespace (this , table_ident.ns );
294304 ICEBERG_RETURN_UNEXPECTED (ns);
295305 ns.value ()->table_metadata_locations_ .erase (table_ident.name );
296306 return {};
297307}
298308
299- Result<bool > InMemoryNamespace::TableExists (TableIdentifier const & table_ident) const {
309+ Result<bool > InMemoryNamespace::TableExists (const TableIdentifier & table_ident) const {
300310 const auto ns = GetNamespace (this , table_ident.ns );
301311 ICEBERG_RETURN_UNEXPECTED (ns);
302312 return ns.value ()->table_metadata_locations_ .contains (table_ident.name );
303313}
304314
305315Result<std::string> InMemoryNamespace::GetTableMetadataLocation (
306- TableIdentifier const & table_ident) const {
316+ const TableIdentifier & table_ident) const {
307317 const auto ns = GetNamespace (this , table_ident.ns );
308318 ICEBERG_RETURN_UNEXPECTED (ns);
309319 const auto it = ns.value ()->table_metadata_locations_ .find (table_ident.name );
@@ -313,17 +323,24 @@ Result<std::string> InMemoryNamespace::GetTableMetadataLocation(
313323 return it->second ;
314324}
315325
326+ Status InMemoryNamespace::UpdateTableMetadataLocation (
327+ const TableIdentifier& table_ident, const std::string& metadata_location) {
328+ ICEBERG_ASSIGN_OR_RAISE (auto ns, GetNamespace (this , table_ident.ns ));
329+ ns->table_metadata_locations_ [table_ident.name ] = metadata_location;
330+ return {};
331+ }
332+
316333std::shared_ptr<InMemoryCatalog> InMemoryCatalog::Make (
317- std::string const & name, std::shared_ptr<FileIO> const & file_io,
318- std::string const & warehouse_location,
319- std::unordered_map<std::string, std::string> const & properties) {
334+ const std::string& name, const std::shared_ptr<FileIO>& file_io,
335+ const std::string& warehouse_location,
336+ const std::unordered_map<std::string, std::string>& properties) {
320337 return std::make_shared<InMemoryCatalog>(name, file_io, warehouse_location, properties);
321338}
322339
323340InMemoryCatalog::InMemoryCatalog (
324- std::string const & name, std::shared_ptr<FileIO> const & file_io,
325- std::string const & warehouse_location,
326- std::unordered_map<std::string, std::string> const & properties)
341+ const std::string& name, const std::shared_ptr<FileIO>& file_io,
342+ const std::string& warehouse_location,
343+ const std::unordered_map<std::string, std::string>& properties)
327344 : catalog_name_(std::move(name)),
328345 properties_ (std::move(properties)),
329346 file_io_(std::move(file_io)),
@@ -395,7 +412,31 @@ Result<std::unique_ptr<Table>> InMemoryCatalog::UpdateTable(
395412 const std::vector<std::unique_ptr<TableRequirement>>& requirements,
396413 const std::vector<std::unique_ptr<TableUpdate>>& updates) {
397414 std::unique_lock lock (mutex_);
398- return NotImplemented (" update table" );
415+ ICEBERG_ASSIGN_OR_RAISE (auto base_metadata_location,
416+ root_namespace_->GetTableMetadataLocation (identifier));
417+
418+ ICEBERG_ASSIGN_OR_RAISE (auto base,
419+ TableMetadataUtil::Read (*file_io_, base_metadata_location));
420+
421+ for (const auto & requirement : requirements) {
422+ ICEBERG_RETURN_UNEXPECTED (requirement->Validate (base.get ()));
423+ }
424+
425+ auto builder = TableMetadataBuilder::BuildFrom (base.get ());
426+ for (const auto & update : updates) {
427+ update->ApplyTo (*builder);
428+ }
429+ ICEBERG_ASSIGN_OR_RAISE (auto updated, builder->Build ());
430+ ICEBERG_ASSIGN_OR_RAISE (
431+ auto new_metadata_location,
432+ TableMetadataUtil::Write (*file_io_, base.get (), base_metadata_location, *updated));
433+ ICEBERG_RETURN_UNEXPECTED (
434+ root_namespace_->UpdateTableMetadataLocation (identifier, new_metadata_location));
435+ TableMetadataUtil::DeleteRemovedMetadataFiles (*file_io_, base.get (), *updated);
436+
437+ return std::make_unique<Table>(identifier, std::move (updated),
438+ std::move (new_metadata_location), file_io_,
439+ std::static_pointer_cast<Catalog>(shared_from_this ()));
399440}
400441
401442Result<std::shared_ptr<Transaction>> InMemoryCatalog::StageCreateTable (
@@ -438,9 +479,8 @@ Result<std::unique_ptr<Table>> InMemoryCatalog::LoadTable(
438479
439480 ICEBERG_ASSIGN_OR_RAISE (auto metadata,
440481 TableMetadataUtil::Read (*file_io_, metadata_location));
441-
442- return std::make_unique<Table>(identifier, std::move (metadata), metadata_location,
443- file_io_,
482+ return std::make_unique<Table>(identifier, std::move (metadata),
483+ std::move (metadata_location), file_io_,
444484 std::static_pointer_cast<Catalog>(shared_from_this ()));
445485}
446486
0 commit comments