Part 13 - Updating Parent Node After a Split

Part 12 - Scanning a Multi-Level B-Tree

For the next step on our epic b-tree implementation journey, we’re going to handle fixing up the parent node after splitting a leaf. I’m going to use the following example as a reference:

|Example of updating internal node

In this example, we add the key “3” to the tree. That causes the left leaf node to split. After the split we fix up the tree by doing the following:

  • Update the first key in the parent to be the maximum key in the left child (“3”)
  • Add a new child pointer / key pair after the updated key
    • The new pointer points to the new child node
    • The new key is the maximum key in the new child node (“5”)
      So first things first, replace our stub code with two new function calls: update_internal_node_key() for step 1 and internal_node_insert() for step 2
  1. @@ -670,9 +725,11 @@ void leaf_node_split_and_insert(Cursor* cursor, uint32_t key, Row* value) {
  2. */
  3. void* old_node = get_page(cursor->table->pager, cursor->page_num);
  4. + uint32_t old_max = get_node_max_key(old_node);
  5. uint32_t new_page_num = get_unused_page_num(cursor->table->pager);
  6. void* new_node = get_page(cursor->table->pager, new_page_num);
  7. initialize_leaf_node(new_node);
  8. + *node_parent(new_node) = *node_parent(old_node);
  9. *leaf_node_next_leaf(new_node) = *leaf_node_next_leaf(old_node);
  10. *leaf_node_next_leaf(old_node) = new_page_num;
  11. @@ -709,8 +766,12 @@ void leaf_node_split_and_insert(Cursor* cursor, uint32_t key, Row* value) {
  12. if (is_node_root(old_node)) {
  13. return create_new_root(cursor->table, new_page_num);
  14. } else {
  15. - printf("Need to implement updating parent after split\n");
  16. - exit(EXIT_FAILURE);
  17. + uint32_t parent_page_num = *node_parent(old_node);
  18. + uint32_t new_max = get_node_max_key(old_node);
  19. + void* parent = get_page(cursor->table->pager, parent_page_num);
  20. +
  21. + update_internal_node_key(parent, old_max, new_max);
  22. + internal_node_insert(cursor->table, parent_page_num, new_page_num);
  23. + return;
  24. }
  25. }

In order to get a reference to the parent, we need to start recording in each node a pointer to its parent node.

  1. +uint32_t* node_parent(void* node) { return node + PARENT_POINTER_OFFSET; }
  1. @@ -660,6 +675,48 @@ void create_new_root(Table* table, uint32_t right_child_page_num) {
  2. uint32_t left_child_max_key = get_node_max_key(left_child);
  3. *internal_node_key(root, 0) = left_child_max_key;
  4. *internal_node_right_child(root) = right_child_page_num;
  5. + *node_parent(left_child) = table->root_page_num;
  6. + *node_parent(right_child) = table->root_page_num;
  7. }

Now we need to find the affected cell in the parent node. The child doesn’t know its own page number, so we can’t look for that. But it does know its own maximum key, so we can search the parent for that key.

  1. +void update_internal_node_key(void* node, uint32_t old_key, uint32_t new_key) {
  2. + uint32_t old_child_index = internal_node_find_child(node, old_key);
  3. + *internal_node_key(node, old_child_index) = new_key;
  4. }

Inside internal_node_find_child() we’ll reuse some code we already have for finding a key in an internal node. Refactor internal_node_find() to use the new helper method.

  1. -Cursor* internal_node_find(Table* table, uint32_t page_num, uint32_t key) {
  2. - void* node = get_page(table->pager, page_num);
  3. +uint32_t internal_node_find_child(void* node, uint32_t key) {
  4. + /*
  5. + Return the index of the child which should contain
  6. + the given key.
  7. + */
  8. +
  9. uint32_t num_keys = *internal_node_num_keys(node);
  10. - /* Binary search to find index of child to search */
  11. + /* Binary search */
  12. uint32_t min_index = 0;
  13. uint32_t max_index = num_keys; /* there is one more child than key */
  14. @@ -386,7 +394,14 @@ Cursor* internal_node_find(Table* table, uint32_t page_num, uint32_t key) {
  15. }
  16. }
  17. - uint32_t child_num = *internal_node_child(node, min_index);
  18. + return min_index;
  19. +}
  20. +
  21. +Cursor* internal_node_find(Table* table, uint32_t page_num, uint32_t key) {
  22. + void* node = get_page(table->pager, page_num);
  23. +
  24. + uint32_t child_index = internal_node_find_child(node, key);
  25. + uint32_t child_num = *internal_node_child(node, child_index);
  26. void* child = get_page(table->pager, child_num);
  27. switch (get_node_type(child)) {
  28. case NODE_LEAF:

Now we get to the heart of this article, implementing internal_node_insert(). I’ll explain it in pieces.

  1. +void internal_node_insert(Table* table, uint32_t parent_page_num,
  2. + uint32_t child_page_num) {
  3. + /*
  4. + Add a new child/key pair to parent that corresponds to child
  5. + */
  6. +
  7. + void* parent = get_page(table->pager, parent_page_num);
  8. + void* child = get_page(table->pager, child_page_num);
  9. + uint32_t child_max_key = get_node_max_key(child);
  10. + uint32_t index = internal_node_find_child(parent, child_max_key);
  11. +
  12. + uint32_t original_num_keys = *internal_node_num_keys(parent);
  13. + *internal_node_num_keys(parent) = original_num_keys + 1;
  14. +
  15. + if (original_num_keys >= INTERNAL_NODE_MAX_CELLS) {
  16. + printf("Need to implement splitting internal node\n");
  17. + exit(EXIT_FAILURE);
  18. + }

The index where the new cell (child/key pair) should be inserted depends on the maximum key in the new child. In the example we looked at, child_max_key would be 5 and index would be 1.

If there’s no room in the internal node for another cell, throw an error. We’ll implement that later.

Now let’s look at the rest of the function:

  1. +
  2. + uint32_t right_child_page_num = *internal_node_right_child(parent);
  3. + void* right_child = get_page(table->pager, right_child_page_num);
  4. +
  5. + if (child_max_key > get_node_max_key(right_child)) {
  6. + /* Replace right child */
  7. + *internal_node_child(parent, original_num_keys) = right_child_page_num;
  8. + *internal_node_key(parent, original_num_keys) =
  9. + get_node_max_key(right_child);
  10. + *internal_node_right_child(parent) = child_page_num;
  11. + } else {
  12. + /* Make room for the new cell */
  13. + for (uint32_t i = original_num_keys; i > index; i--) {
  14. + void* destination = internal_node_cell(parent, i);
  15. + void* source = internal_node_cell(parent, i - 1);
  16. + memcpy(destination, source, INTERNAL_NODE_CELL_SIZE);
  17. + }
  18. + *internal_node_child(parent, index) = child_page_num;
  19. + *internal_node_key(parent, index) = child_max_key;
  20. + }
  21. +}

Because we store the rightmost child pointer separately from the rest of the child/key pairs, we have to handle things differently if the new child is going to become the rightmost child.

In our example, we would get into the else block. First we make room for the new cell by shifting other cells one space to the right. (Although in our example there are 0 cells to shift)

Next, we write the new child pointer and key into the cell determined by index.

To reduce the size of testcases needed, I’m hardcoding INTERNAL_NODE_MAX_CELLS for now

  1. @@ -126,6 +126,8 @@ const uint32_t INTERNAL_NODE_KEY_SIZE = sizeof(uint32_t);
  2. const uint32_t INTERNAL_NODE_CHILD_SIZE = sizeof(uint32_t);
  3. const uint32_t INTERNAL_NODE_CELL_SIZE =
  4. INTERNAL_NODE_CHILD_SIZE + INTERNAL_NODE_KEY_SIZE;
  5. +/* Keep this small for testing */
  6. +const uint32_t INTERNAL_NODE_MAX_CELLS = 3;

Speaking of tests, our large-dataset test gets past our old stub and gets to our new one:

  1. @@ -65,7 +65,7 @@ describe 'database' do
  2. result = run_script(script)
  3. expect(result.last(2)).to match_array([
  4. "db > Executed.",
  5. - "db > Need to implement updating parent after split",
  6. + "db > Need to implement splitting internal node",
  7. ])

Very satisfying, I know.

I’ll add another test that prints a four-node tree. Just so we test more cases than sequential ids, this test will add records in a pseudorandom order.

  1. + it 'allows printing out the structure of a 4-leaf-node btree' do
  2. + script = [
  3. + "insert 18 user18 person18@example.com",
  4. + "insert 7 user7 person7@example.com",
  5. + "insert 10 user10 person10@example.com",
  6. + "insert 29 user29 person29@example.com",
  7. + "insert 23 user23 person23@example.com",
  8. + "insert 4 user4 person4@example.com",
  9. + "insert 14 user14 person14@example.com",
  10. + "insert 30 user30 person30@example.com",
  11. + "insert 15 user15 person15@example.com",
  12. + "insert 26 user26 person26@example.com",
  13. + "insert 22 user22 person22@example.com",
  14. + "insert 19 user19 person19@example.com",
  15. + "insert 2 user2 person2@example.com",
  16. + "insert 1 user1 person1@example.com",
  17. + "insert 21 user21 person21@example.com",
  18. + "insert 11 user11 person11@example.com",
  19. + "insert 6 user6 person6@example.com",
  20. + "insert 20 user20 person20@example.com",
  21. + "insert 5 user5 person5@example.com",
  22. + "insert 8 user8 person8@example.com",
  23. + "insert 9 user9 person9@example.com",
  24. + "insert 3 user3 person3@example.com",
  25. + "insert 12 user12 person12@example.com",
  26. + "insert 27 user27 person27@example.com",
  27. + "insert 17 user17 person17@example.com",
  28. + "insert 16 user16 person16@example.com",
  29. + "insert 13 user13 person13@example.com",
  30. + "insert 24 user24 person24@example.com",
  31. + "insert 25 user25 person25@example.com",
  32. + "insert 28 user28 person28@example.com",
  33. + ".btree",
  34. + ".exit",
  35. + ]
  36. + result = run_script(script)

As-is, it will output this:

  1. - internal (size 3)
  2. - leaf (size 7)
  3. - 1
  4. - 2
  5. - 3
  6. - 4
  7. - 5
  8. - 6
  9. - 7
  10. - key 1
  11. - leaf (size 8)
  12. - 8
  13. - 9
  14. - 10
  15. - 11
  16. - 12
  17. - 13
  18. - 14
  19. - 15
  20. - key 15
  21. - leaf (size 7)
  22. - 16
  23. - 17
  24. - 18
  25. - 19
  26. - 20
  27. - 21
  28. - 22
  29. - key 22
  30. - leaf (size 8)
  31. - 23
  32. - 24
  33. - 25
  34. - 26
  35. - 27
  36. - 28
  37. - 29
  38. - 30
  39. db >

Look carefully and you’ll spot a bug:

  1. - 5
  2. - 6
  3. - 7
  4. - key 1

The key there should be 7, not 1!

After a bunch of debugging, I discovered this was due to some bad pointer arithmetic.

  1. uint32_t* internal_node_key(void* node, uint32_t key_num) {
  2. - return internal_node_cell(node, key_num) + INTERNAL_NODE_CHILD_SIZE;
  3. + return (void*)internal_node_cell(node, key_num) + INTERNAL_NODE_CHILD_SIZE;
  4. }

INTERNAL_NODE_CHILD_SIZE is 4. My intention here was to add 4 bytes to the result of internal_node_cell(), but since internal_node_cell() returns a uint32_t, this it was actually adding 4 sizeof(uint32_t) bytes. I fixed it by casting to a void* before doing the arithmetic.

NOTE! Pointer arithmetic on void pointers is not part of the C standard and may not work with your compiler. I may do an article in the future on portability, but I’m leaving my void pointer arithmetic for now.

Alright. One more step toward a fully-operational btree implementation. The next step should be splitting internal nodes. Until then!

Part 12 - Scanning a Multi-Level B-Tree

原文: https://cstack.github.io/db_tutorial/parts/part13.html