From b485f6a48c75cfe101a90fac07c08b4fae5386c0 Mon Sep 17 00:00:00 2001 From: OrangeSmokingJacket Date: Mon, 18 Nov 2024 11:13:52 +0100 Subject: [PATCH] btree RAII lock where possible --- core/b_plus_tree/b_plus_tree.cpp | 32 ++++++++++++++------------------ core/b_plus_tree/b_plus_tree.hpp | 13 +++---------- 2 files changed, 17 insertions(+), 28 deletions(-) diff --git a/core/b_plus_tree/b_plus_tree.cpp b/core/b_plus_tree/b_plus_tree.cpp index 973e98c4..94e4ed20 100644 --- a/core/b_plus_tree/b_plus_tree.cpp +++ b/core/b_plus_tree/b_plus_tree.cpp @@ -719,21 +719,20 @@ namespace core::b_plus_tree { if (!first_leaf) { return; } - - tree_mutex_.lock_shared(); - first_leaf->unlock_shared(); - - result.reserve(item_count_); - while (first_leaf) { - for (auto block = first_leaf->begin(); block != first_leaf->end(); block++) { - for (auto it = block->begin(); it != block->end(); it++) { - result.push_back(it->index); + { + std::shared_lock l(tree_mutex_); + first_leaf->unlock_shared(); + + result.reserve(item_count_); + while (first_leaf) { + for (auto block = first_leaf->begin(); block != first_leaf->end(); block++) { + for (auto it = block->begin(); it != block->end(); it++) { + result.push_back(it->index); + } } + first_leaf = static_cast(first_leaf->right_node_); } - first_leaf = static_cast(first_leaf->right_node_); } - - tree_mutex_.unlock_shared(); result.erase(std::unique(result.begin(), result.end()), result.end()); } @@ -744,7 +743,7 @@ namespace core::b_plus_tree { std::filesystem::path file_name = storage_directory_; file_name /= std::filesystem::path(metadata_file_name_); - tree_mutex_.lock(); + std::unique_lock l(tree_mutex_); // got root mutex, no need to lock nodes or save parent node base_node_t* current_node = root_; @@ -774,7 +773,6 @@ namespace core::b_plus_tree { file->write(static_cast(buffer), METADATA_SIZE, 0); resource_->deallocate(static_cast(buffer), METADATA_SIZE); - tree_mutex_.unlock(); } void btree_t::load() { @@ -783,7 +781,7 @@ namespace core::b_plus_tree { return; } - tree_mutex_.lock(); + std::unique_lock l(tree_mutex_); if (root_) { delete root_; root_ = nullptr; @@ -862,7 +860,6 @@ namespace core::b_plus_tree { resource_->deallocate(static_cast(buffer), METADATA_SIZE); resource_->deallocate(static_cast(nodes_layer), leaf_nodes_count_ * sizeof(base_node_t*)); - tree_mutex_.unlock(); } bool btree_t::contains_index(const index_t& index) { @@ -940,7 +937,7 @@ namespace core::b_plus_tree { return 0; } - tree_mutex_.lock_shared(); + std::shared_lock l(tree_mutex_); first_leaf->unlock_shared(); size_t result = 0; @@ -949,7 +946,6 @@ namespace core::b_plus_tree { first_leaf = static_cast(first_leaf->right_node_); } - tree_mutex_.unlock_shared(); return result; } diff --git a/core/b_plus_tree/b_plus_tree.hpp b/core/b_plus_tree/b_plus_tree.hpp index d6a66880..db967f2f 100644 --- a/core/b_plus_tree/b_plus_tree.hpp +++ b/core/b_plus_tree/b_plus_tree.hpp @@ -243,7 +243,7 @@ namespace core::b_plus_tree { return false; } - tree_mutex_.lock_shared(); + std::shared_lock l(tree_mutex_); first_leaf->unlock_shared(); while (first_leaf) { @@ -258,7 +258,6 @@ namespace core::b_plus_tree { first_leaf = static_cast(first_leaf->right_node_); } - tree_mutex_.unlock_shared(); return true; } @@ -285,7 +284,7 @@ namespace core::b_plus_tree { return false; } - tree_mutex_.lock_shared(); + std::shared_lock l(tree_mutex_); first_leaf->unlock_shared(); while (first_leaf) { @@ -296,7 +295,6 @@ namespace core::b_plus_tree { for (auto block = first_leaf->begin(); block != first_leaf->end(); block++) { for (auto it = block->begin(); it != block->end(); it++) { if (it->index > max_index) { - tree_mutex_.unlock_shared(); return true; } else if (it->index < min_index) { continue; @@ -306,7 +304,6 @@ namespace core::b_plus_tree { result->emplace_back(std::move(t)); limit--; if (limit == 0) { - tree_mutex_.unlock_shared(); return true; } } @@ -315,7 +312,6 @@ namespace core::b_plus_tree { first_leaf = static_cast(first_leaf->right_node_); } - tree_mutex_.unlock_shared(); return true; } @@ -342,7 +338,7 @@ namespace core::b_plus_tree { return false; } - tree_mutex_.lock_shared(); + std::shared_lock l(tree_mutex_); last_leaf->unlock_shared(); while (last_leaf) { @@ -353,7 +349,6 @@ namespace core::b_plus_tree { for (auto block = last_leaf->rbegin(); block != last_leaf->rend(); block++) { for (auto it = block->rbegin(); it != block->rend(); it++) { if (it->index < min_index) { - tree_mutex_.unlock_shared(); return true; } else if (it->index > max_index) { continue; @@ -363,7 +358,6 @@ namespace core::b_plus_tree { result->emplace_back(std::move(t)); limit--; if (limit == 0) { - tree_mutex_.unlock_shared(); return true; } } @@ -372,7 +366,6 @@ namespace core::b_plus_tree { last_leaf = static_cast(last_leaf->left_node_); } - tree_mutex_.unlock_shared(); return true; }