Skip to content

Commit

Permalink
btree RAII lock where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
OrangeSmokingJacket committed Nov 18, 2024
1 parent 0254916 commit b485f6a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 28 deletions.
32 changes: 14 additions & 18 deletions core/b_plus_tree/b_plus_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<leaf_node_t*>(first_leaf->right_node_);
}
first_leaf = static_cast<leaf_node_t*>(first_leaf->right_node_);
}

tree_mutex_.unlock_shared();
result.erase(std::unique(result.begin(), result.end()), result.end());
}

Expand All @@ -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_;
Expand Down Expand Up @@ -774,7 +773,6 @@ namespace core::b_plus_tree {
file->write(static_cast<void*>(buffer), METADATA_SIZE, 0);

resource_->deallocate(static_cast<void*>(buffer), METADATA_SIZE);
tree_mutex_.unlock();
}

void btree_t::load() {
Expand All @@ -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;
Expand Down Expand Up @@ -862,7 +860,6 @@ namespace core::b_plus_tree {

resource_->deallocate(static_cast<void*>(buffer), METADATA_SIZE);
resource_->deallocate(static_cast<void*>(nodes_layer), leaf_nodes_count_ * sizeof(base_node_t*));
tree_mutex_.unlock();
}

bool btree_t::contains_index(const index_t& index) {
Expand Down Expand Up @@ -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;
Expand All @@ -949,7 +946,6 @@ namespace core::b_plus_tree {
first_leaf = static_cast<leaf_node_t*>(first_leaf->right_node_);
}

tree_mutex_.unlock_shared();
return result;
}

Expand Down
13 changes: 3 additions & 10 deletions core/b_plus_tree/b_plus_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -258,7 +258,6 @@ namespace core::b_plus_tree {
first_leaf = static_cast<leaf_node_t*>(first_leaf->right_node_);
}

tree_mutex_.unlock_shared();
return true;
}

Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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;
}
}
Expand All @@ -315,7 +312,6 @@ namespace core::b_plus_tree {
first_leaf = static_cast<leaf_node_t*>(first_leaf->right_node_);
}

tree_mutex_.unlock_shared();
return true;
}

Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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;
}
}
Expand All @@ -372,7 +366,6 @@ namespace core::b_plus_tree {
last_leaf = static_cast<leaf_node_t*>(last_leaf->left_node_);
}

tree_mutex_.unlock_shared();
return true;
}

Expand Down

0 comments on commit b485f6a

Please sign in to comment.