Skip to content

Commit c8a49c3

Browse files
committed
refactor: optimize with shared_lock/unique_lock for InMemoryCatalog
1 parent 428a171 commit c8a49c3

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

src/iceberg/catalog/memory/in_memory_catalog.cc

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
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

338337
Status 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

344343
Result<std::unordered_map<std::string, std::string>>
345344
InMemoryCatalog::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

350349
Result<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

356355
Status 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

361360
Result<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

366365
Status 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

373372
Result<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

393393
Result<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

400401
Result<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

407409
Result<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

412414
Status 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

418420
Status 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

444447
Result<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

src/iceberg/catalog/memory/in_memory_catalog.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#pragma once
2121

22-
#include <mutex>
22+
#include <shared_mutex>
2323

2424
#include "iceberg/catalog.h"
2525

@@ -103,7 +103,7 @@ class ICEBERG_EXPORT InMemoryCatalog
103103
std::shared_ptr<FileIO> file_io_;
104104
std::string warehouse_location_;
105105
std::unique_ptr<class InMemoryNamespace> root_namespace_;
106-
mutable std::recursive_mutex mutex_;
106+
mutable std::shared_mutex mutex_;
107107
};
108108

109109
} // namespace iceberg

0 commit comments

Comments
 (0)