bool split_branch (tree_s *tree, branch_s *parent, branch_s *child, s64 k) { branch_s *sibling; snint upper; snint midpoint; u32 upper_key; if (!branch_is_full(child)) return FALSE; FN; sibling = new_branch(tree); midpoint = child->br_num / 2; upper = midpoint + 1; sibling->br_first = child->br_key[midpoint].k_node; upper_key = child->br_key[midpoint].k_key; memmove(sibling->br_key, &child->br_key[upper], sizeof(key_s) * (child->br_num - upper)); sibling->br_num = child->br_num - upper; child->br_num = midpoint; insert_sibling(parent, sibling, k, upper_key); return TRUE; }
bool split_branch (void *self, branch_s *parent, int k) { branch_s *child = self; branch_s *sibling; int upper; int midpoint; u64 midpoint_key; FN; if (!branch_is_full(child)) return FALSE; sibling = new_branch(bdev(child)); midpoint = child->br_num / 2; upper = midpoint + 1; sibling->br_first = child->br_key[midpoint].k_block; midpoint_key = child->br_key[midpoint].k_key; memmove(sibling->br_key, &child->br_key[upper], sizeof(key_s) * (child->br_num - upper)); sibling->br_num = child->br_num - upper; child->br_num = midpoint; bdirty(child); bput(child); insert_sibling(parent, sibling, k, midpoint_key); bput(sibling); return TRUE; }
void *grow_tree (tree_s *tree, unint len) { void *root; branch_s *branch; int rc; FN; if (root_blkno(tree)) { root = bget(tree->t_dev, root_blkno(tree)); if (!root) return NULL; switch (type(root)) { case LEAF: if (!leaf_is_full(root, len)) { return root; } break; case BRANCH: if (!branch_is_full(root)) { return root; } break; default: bput(root); error("Bad block"); exit(1); break; } branch = new_branch(tree->t_dev); branch->br_first = bblkno(root); bput(root); root = branch; } else { root = new_leaf(tree->t_dev); } rc = change_root(tree, root); if (rc) { error("Couldn't grow tree"); return NULL; } return root; }
static void *grow_tree (tree_s *tree, unint size) { head_s *head; branch_s *branch; int rc; FN; if (root(tree)) { head = tau_bget(tree_inode(tree), root(tree)); if (!head) return NULL; switch (head->h_magic) { case LEAF: if (!leaf_is_full((leaf_s *)head, size)) { return head; } break; case BRANCH: if (!branch_is_full((branch_s *)head)) { return head; } break; default: tau_bput(head); eprintk("Bad block"); BUG(); break; } branch = new_branch(tree); branch->br_first = tau_bnum(head); tau_bput(head); head = (head_s *)branch; } else { head = (head_s *)new_leaf(tree); } rc = change_root(tree, head); if (rc) { error("Couldn't grow tree"); return NULL; } return head; }
void *grow_tree (tree_s *tree, unint size) { void *node; branch_s *branch; int rc; FN; node = root(tree); if (node) { switch (magic(node)) { case LEAF: if (!leaf_is_full(node, size)) { return node; } break; case BRANCH: if (!branch_is_full(node)) { return node; } break; default: eprintf("Bad node"); break; } branch = new_branch(tree); branch->br_first = node; node = branch; } else { node = new_leaf(tree); } rc = change_root(tree, node); if (rc) { eprintf("Couldn't grow tree"); return NULL; } return node; }