Skip to content

Commit db5d805

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

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

src/iceberg/catalog/memory/in_memory_catalog.cc

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
#include <algorithm>
2323
#include <iterator>
24-
#include <mutex>
24+
#include <memory>
25+
#include <shared_mutex>
2526

2627
#include "iceberg/table.h"
2728
#include "iceberg/table_metadata.h"
@@ -337,42 +338,42 @@ std::string_view InMemoryCatalog::name() const { return catalog_name_; }
337338

338339
Status InMemoryCatalog::CreateNamespace(
339340
const Namespace& ns, const std::unordered_map<std::string, std::string>& properties) {
340-
std::lock_guard guard(mutex_);
341+
std::unique_lock lock(mutex_);
341342
return root_namespace_->CreateNamespace(ns, properties);
342343
}
343344

344345
Result<std::unordered_map<std::string, std::string>>
345346
InMemoryCatalog::GetNamespaceProperties(const Namespace& ns) const {
346-
std::lock_guard guard(mutex_);
347+
std::shared_lock lock(mutex_);
347348
return root_namespace_->GetProperties(ns);
348349
}
349350

350351
Result<std::vector<Namespace>> InMemoryCatalog::ListNamespaces(
351352
const Namespace& ns) const {
352-
std::lock_guard guard(mutex_);
353+
std::shared_lock lock(mutex_);
353354
return root_namespace_->ListNamespaces(ns);
354355
}
355356

356357
Status InMemoryCatalog::DropNamespace(const Namespace& ns) {
357-
std::lock_guard guard(mutex_);
358+
std::unique_lock lock(mutex_);
358359
return root_namespace_->DropNamespace(ns);
359360
}
360361

361362
Result<bool> InMemoryCatalog::NamespaceExists(const Namespace& ns) const {
362-
std::lock_guard guard(mutex_);
363+
std::shared_lock lock(mutex_);
363364
return root_namespace_->NamespaceExists(ns);
364365
}
365366

366367
Status InMemoryCatalog::UpdateNamespaceProperties(
367368
const Namespace& ns, const std::unordered_map<std::string, std::string>& updates,
368369
const std::unordered_set<std::string>& removals) {
369-
std::lock_guard guard(mutex_);
370+
std::unique_lock lock(mutex_);
370371
return root_namespace_->UpdateNamespaceProperties(ns, updates, removals);
371372
}
372373

373374
Result<std::vector<TableIdentifier>> InMemoryCatalog::ListTables(
374375
const Namespace& ns) const {
375-
std::lock_guard guard(mutex_);
376+
std::shared_lock lock(mutex_);
376377
const auto& table_names = root_namespace_->ListTables(ns);
377378
ICEBERG_RETURN_UNEXPECTED(table_names);
378379
std::vector<TableIdentifier> table_idents;
@@ -387,36 +388,40 @@ Result<std::unique_ptr<Table>> InMemoryCatalog::CreateTable(
387388
const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec,
388389
const std::string& location,
389390
const std::unordered_map<std::string, std::string>& properties) {
391+
std::unique_lock lock(mutex_);
390392
return NotImplemented("create table");
391393
}
392394

393395
Result<std::unique_ptr<Table>> InMemoryCatalog::UpdateTable(
394396
const TableIdentifier& identifier,
395397
const std::vector<std::unique_ptr<TableRequirement>>& requirements,
396398
const std::vector<std::unique_ptr<TableUpdate>>& updates) {
399+
std::unique_lock lock(mutex_);
397400
return NotImplemented("update table");
398401
}
399402

400403
Result<std::shared_ptr<Transaction>> InMemoryCatalog::StageCreateTable(
401404
const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec,
402405
const std::string& location,
403406
const std::unordered_map<std::string, std::string>& properties) {
407+
std::unique_lock lock(mutex_);
404408
return NotImplemented("stage create table");
405409
}
406410

407411
Result<bool> InMemoryCatalog::TableExists(const TableIdentifier& identifier) const {
408-
std::lock_guard guard(mutex_);
412+
std::shared_lock lock(mutex_);
409413
return root_namespace_->TableExists(identifier);
410414
}
411415

412416
Status InMemoryCatalog::DropTable(const TableIdentifier& identifier, bool purge) {
413-
std::lock_guard guard(mutex_);
417+
std::unique_lock lock(mutex_);
414418
// TODO(Guotao): Delete all metadata files if purge is true.
415419
return root_namespace_->UnregisterTable(identifier);
416420
}
417421

418422
Status InMemoryCatalog::RenameTable(const TableIdentifier& from,
419423
const TableIdentifier& to) {
424+
std::unique_lock lock(mutex_);
420425
return NotImplemented("rename table");
421426
}
422427

@@ -428,7 +433,7 @@ Result<std::unique_ptr<Table>> InMemoryCatalog::LoadTable(
428433

429434
Result<std::string> metadata_location;
430435
{
431-
std::lock_guard guard(mutex_);
436+
std::shared_lock lock(mutex_);
432437
ICEBERG_ASSIGN_OR_RAISE(metadata_location,
433438
root_namespace_->GetTableMetadataLocation(identifier));
434439
}
@@ -443,14 +448,19 @@ Result<std::unique_ptr<Table>> InMemoryCatalog::LoadTable(
443448

444449
Result<std::shared_ptr<Table>> InMemoryCatalog::RegisterTable(
445450
const TableIdentifier& identifier, const std::string& metadata_file_location) {
446-
std::lock_guard guard(mutex_);
451+
ICEBERG_ASSIGN_OR_RAISE(auto metadata,
452+
TableMetadataUtil::Read(*file_io_, metadata_file_location));
453+
454+
std::unique_lock lock(mutex_);
447455
if (!root_namespace_->NamespaceExists(identifier.ns)) {
448456
return NoSuchNamespace("table namespace does not exist.");
449457
}
450458
if (!root_namespace_->RegisterTable(identifier, metadata_file_location)) {
451459
return UnknownError("The registry failed.");
452460
}
453-
return LoadTable(identifier);
461+
return std::make_unique<Table>(identifier, std::move(metadata), metadata_file_location,
462+
file_io_,
463+
std::static_pointer_cast<Catalog>(shared_from_this()));
454464
}
455465

456466
} // 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)