2121
2222#include < algorithm>
2323#include < iterator>
24- #include < mutex>
2524
2625#include " iceberg/table.h"
2726#include " iceberg/table_metadata.h"
@@ -337,42 +336,42 @@ std::string_view InMemoryCatalog::name() const { return catalog_name_; }
337336
338337Status InMemoryCatalog::CreateNamespace (
339338 const Namespace& ns, const std::unordered_map<std::string, std::string>& properties) {
340- std::lock_guard guard (mutex_);
339+ std::unique_lock lock (mutex_);
341340 return root_namespace_->CreateNamespace (ns, properties);
342341}
343342
344343Result<std::unordered_map<std::string, std::string>>
345344InMemoryCatalog::GetNamespaceProperties (const Namespace& ns) const {
346- std::lock_guard guard (mutex_);
345+ std::shared_lock lock (mutex_);
347346 return root_namespace_->GetProperties (ns);
348347}
349348
350349Result<std::vector<Namespace>> InMemoryCatalog::ListNamespaces (
351350 const Namespace& ns) const {
352- std::lock_guard guard (mutex_);
351+ std::shared_lock lock (mutex_);
353352 return root_namespace_->ListNamespaces (ns);
354353}
355354
356355Status InMemoryCatalog::DropNamespace (const Namespace& ns) {
357- std::lock_guard guard (mutex_);
356+ std::unique_lock lock (mutex_);
358357 return root_namespace_->DropNamespace (ns);
359358}
360359
361360Result<bool > InMemoryCatalog::NamespaceExists (const Namespace& ns) const {
362- std::lock_guard guard (mutex_);
361+ std::shared_lock lock (mutex_);
363362 return root_namespace_->NamespaceExists (ns);
364363}
365364
366365Status InMemoryCatalog::UpdateNamespaceProperties (
367366 const Namespace& ns, const std::unordered_map<std::string, std::string>& updates,
368367 const std::unordered_set<std::string>& removals) {
369- std::lock_guard guard (mutex_);
368+ std::unique_lock lock (mutex_);
370369 return root_namespace_->UpdateNamespaceProperties (ns, updates, removals);
371370}
372371
373372Result<std::vector<TableIdentifier>> InMemoryCatalog::ListTables (
374373 const Namespace& ns) const {
375- std::lock_guard guard (mutex_);
374+ std::shared_lock lock (mutex_);
376375 const auto & table_names = root_namespace_->ListTables (ns);
377376 ICEBERG_RETURN_UNEXPECTED (table_names);
378377 std::vector<TableIdentifier> table_idents;
@@ -387,36 +386,40 @@ Result<std::unique_ptr<Table>> InMemoryCatalog::CreateTable(
387386 const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec,
388387 const std::string& location,
389388 const std::unordered_map<std::string, std::string>& properties) {
389+ std::unique_lock lock (mutex_);
390390 return NotImplemented (" create table" );
391391}
392392
393393Result<std::unique_ptr<Table>> InMemoryCatalog::UpdateTable (
394394 const TableIdentifier& identifier,
395395 const std::vector<std::unique_ptr<TableRequirement>>& requirements,
396396 const std::vector<std::unique_ptr<TableUpdate>>& updates) {
397+ std::unique_lock lock (mutex_);
397398 return NotImplemented (" update table" );
398399}
399400
400401Result<std::shared_ptr<Transaction>> InMemoryCatalog::StageCreateTable (
401402 const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec,
402403 const std::string& location,
403404 const std::unordered_map<std::string, std::string>& properties) {
405+ std::unique_lock lock (mutex_);
404406 return NotImplemented (" stage create table" );
405407}
406408
407409Result<bool > InMemoryCatalog::TableExists (const TableIdentifier& identifier) const {
408- std::lock_guard guard (mutex_);
410+ std::shared_lock lock (mutex_);
409411 return root_namespace_->TableExists (identifier);
410412}
411413
412414Status InMemoryCatalog::DropTable (const TableIdentifier& identifier, bool purge) {
413- std::lock_guard guard (mutex_);
415+ std::unique_lock lock (mutex_);
414416 // TODO(Guotao): Delete all metadata files if purge is true.
415417 return root_namespace_->UnregisterTable (identifier);
416418}
417419
418420Status InMemoryCatalog::RenameTable (const TableIdentifier& from,
419421 const TableIdentifier& to) {
422+ std::unique_lock lock (mutex_);
420423 return NotImplemented (" rename table" );
421424}
422425
@@ -426,31 +429,40 @@ Result<std::unique_ptr<Table>> InMemoryCatalog::LoadTable(
426429 return InvalidArgument (" file_io is not set for catalog {}" , catalog_name_);
427430 }
428431
429- Result< std::string> metadata_location;
432+ std::string metadata_location;
430433 {
431- std::lock_guard guard (mutex_);
434+ std::shared_lock lock (mutex_);
432435 ICEBERG_ASSIGN_OR_RAISE (metadata_location,
433436 root_namespace_->GetTableMetadataLocation (identifier));
434437 }
435438
436439 ICEBERG_ASSIGN_OR_RAISE (auto metadata,
437- TableMetadataUtil::Read (*file_io_, metadata_location. value () ));
440+ TableMetadataUtil::Read (*file_io_, metadata_location));
438441
439- return std::make_unique<Table>(identifier, std::move (metadata),
440- metadata_location. value (), file_io_,
442+ return std::make_unique<Table>(identifier, std::move (metadata), metadata_location,
443+ file_io_,
441444 std::static_pointer_cast<Catalog>(shared_from_this ()));
442445}
443446
444447Result<std::shared_ptr<Table>> InMemoryCatalog::RegisterTable (
445448 const TableIdentifier& identifier, const std::string& metadata_file_location) {
446- std::lock_guard guard (mutex_);
449+ if (!file_io_) [[unlikely]] {
450+ return InvalidArgument (" file_io is not set for catalog {}" , catalog_name_);
451+ }
452+
453+ ICEBERG_ASSIGN_OR_RAISE (auto metadata,
454+ TableMetadataUtil::Read (*file_io_, metadata_file_location));
455+
456+ std::unique_lock lock (mutex_);
447457 if (!root_namespace_->NamespaceExists (identifier.ns )) {
448458 return NoSuchNamespace (" table namespace does not exist." );
449459 }
450460 if (!root_namespace_->RegisterTable (identifier, metadata_file_location)) {
451461 return UnknownError (" The registry failed." );
452462 }
453- return LoadTable (identifier);
463+ return std::make_unique<Table>(identifier, std::move (metadata), metadata_file_location,
464+ file_io_,
465+ std::static_pointer_cast<Catalog>(shared_from_this ()));
454466}
455467
456468} // namespace iceberg
0 commit comments