Part 11 - Recursively Searching the B-Tree

    Part 12 - Scanning a Multi-Level B-Tree

    Last time we ended with an error inserting our 15th row:

    First, replace the code stub with a new function call.

    1. return leaf_node_find(table, root_page_num, key);
    2. } else {
    3. - printf("Need to implement searching an internal node\n");
    4. - exit(EXIT_FAILURE);
    5. + return internal_node_find(table, root_page_num, key);
    6. }
    7. }

    This function will perform binary search to find the child that should contain the given key. Remember that the key to the right of each child pointer is the maximum key contained by that child.

    So our binary search compares the key to find and the key to the right of the child pointer:

    Also remember that the children of an internal node can be either leaf nodes or more internal nodes. After we find the correct child, call the appropriate search function on it:

    1. + switch (get_node_type(child)) {
    2. + case NODE_LEAF:
    3. + return leaf_node_find(table, child_num, key);
    4. + case NODE_INTERNAL:
    5. + return internal_node_find(table, child_num, key);
    6. + }
    7. +}

    Tests

    Now inserting a key into a multi-node btree no longer results in an error. And we can update our test:

    I also think it’s time we revisit another test. The one that tries inserting 1400 rows. It still errors, but the error message is new. Right now, our tests don’t handle it very well when the program crashes. If that happens, let’s just use the output we’ve gotten so far:

    1. raw_output = nil
    2. commands.each do |command|
    3. - pipe.puts command
    4. + begin
    5. + pipe.puts command
    6. + rescue Errno::EPIPE
    7. + break
    8. + end
    9. end

    Looks like that’s next on our to-do list!

    Part 12 - Scanning a Multi-Level B-Tree