bool leaf_should_delete = CoalesceOrRedistribute(leaf_node, transaction, pointer_root_is_latched); // NOTE: unlock and unpin are finished in CoalesceOrRedistribute // NOTE: root node must be unlocked in CoalesceOrRedistribute assert((*pointer_root_is_latched) == false);
delete pointer_root_is_latched;
if (leaf_should_delete) { transaction->AddIntoDeletedPageSet(leaf_page->GetPageId()); }
INDEX_TEMPLATE_ARGUMENTS template <typename N> voidBPLUSTREE_TYPE::Redistribute(N *neighbor_node, N *node, int index){ Page *parent_page = buffer_pool_manager_->FetchPage(node->GetParentPageId()); InternalPage *parent = reinterpret_cast<InternalPage *>(parent_page->GetData());
if (node->IsLeafPage()) { LeafPage *leaf_node = reinterpret_cast<LeafPage *>(node); LeafPage *neighbor_leaf_node = reinterpret_cast<LeafPage *>(neighbor_node); if (index == 0) { // node -> neighbor // move neighbor's first to node's end neighbor_leaf_node->MoveFirstToEndOf(leaf_node); parent->SetKeyAt(1, neighbor_leaf_node->KeyAt(0)); } else { // neighbor -> node // move neighbor's last to node's front neighbor_leaf_node->MoveLastToFrontOf(leaf_node); parent->SetKeyAt(index, leaf_node->KeyAt(0)); } } else { InternalPage *internal_node = reinterpret_cast<InternalPage *>(node); InternalPage *neighbor_internal_node = reinterpret_cast<InternalPage *>(neighbor_node); if (index == 0) { // case: node(left) and neighbor(right) // set neighbor's first key to parent's second key(详见MoveFirstToEndOf函数) // move neighbor's first to node's end neighbor_internal_node->MoveFirstToEndOf(internal_node, parent->KeyAt(1), buffer_pool_manager_); // set parent's second key to neighbor's "new" first key parent->SetKeyAt(1, neighbor_internal_node->KeyAt(0)); } else { // case: neighbor(left) and node(right) // MoveLastToFrontOf do this: // set node's first key to parent's index key(详见MoveLastToFrontOf函数) // move neighbor's last to node's front neighbor_internal_node->MoveLastToFrontOf(internal_node, parent->KeyAt(index), buffer_pool_manager_); // set parent's index key to node's "new" first key parent->SetKeyAt(index, internal_node->KeyAt(0)); } }
$ ./test/b_plus_tree_delete_test Running main() from gmock_main.cc [==========] Running 2 tests from 1 test suite. [----------] Global test environment set-up. [----------] 2 tests from BPlusTreeTests [ RUN ] BPlusTreeTests.DeleteTest1 [ OK ] BPlusTreeTests.DeleteTest1 (0 ms) [ RUN ] BPlusTreeTests.DeleteTest2 [ OK ] BPlusTreeTests.DeleteTest2 (0 ms) [----------] 2 tests from BPlusTreeTests (1 ms total)
[----------] Global test environment tear-down [==========] 2 tests from 1 test suite ran. (1 ms total) [ PASSED ] 2 tests.