begeekmyfriend / bplustree

A minimal but extreme fast B+ tree indexing structure demo for billions of key-value storage
MIT License
1.85k stars 316 forks source link

分裂策略是否有问题? #19

Closed CJLUzjj closed 2 years ago

CJLUzjj commented 2 years ago

image

如上图,此时是否应该将34作为split_key提到根节点比较合适? 问题逻辑如下:

static key_t non_leaf_split_right2(struct bplus_tree *tree, struct bplus_node *node,
                               struct bplus_node *right, struct bplus_node *l_ch,
                               struct bplus_node *r_ch, key_t key, int insert)
{
        int i;

        /* split = [m/2] */
        int split = (_max_order + 1) / 2;

        /* split as right sibling */
        right_node_add(tree, node, right);

        /* split key is key[split] */
        key_t split_key = key(node)[split];  // it's here, split_key = key(node)[split - 1] maybe better?

        /* calculate split nodes' children (sum as (order + 1))*/
        int pivot = insert - split - 1;
        node->children = split + 1;
        right->children = _max_order - split;

        /* sum = right->children = pivot + 2 + (_max_order - insert - 1) */
        /* replicate from key[split + 1] to key[insert] */
        memmove(&key(right)[0], &key(node)[split + 1], pivot * sizeof(key_t));
        memmove(&sub(right)[0], &sub(node)[split + 1], pivot * sizeof(off_t));

        /* insert new key and sub-node */
        key(right)[pivot] = key;
        sub_node_update(tree, right, pivot, l_ch);
        sub_node_update(tree, right, pivot + 1, r_ch);

        /* replicate from key[insert] to key[order - 1] */
        memmove(&key(right)[pivot + 1], &key(node)[insert], (_max_order - insert - 1) * sizeof(key_t));
        memmove(&sub(right)[pivot + 2], &sub(node)[insert + 1], (_max_order - insert - 1) * sizeof(off_t));

        /* flush sub-nodes of the new splitted right node */
        for (i = 0; i < right->children; i++) {
                if (i != pivot && i != pivot + 1) {
                        sub_node_flush(tree, right, sub(right)[i]);
                }
        }

        return split_key;
}
begeekmyfriend commented 2 years ago

感谢关注,你是对的,已修正:https://github.com/begeekmyfriend/bplustree/commit/0d55dadb0687974e63b36e808b3ab4276d05b344

CJLUzjj commented 2 years ago

不好意思再打扰一下,您的修改好像还是有些问题,虽然non_leaf_split_right2返回的split_key正确的,但是non_leaf_split_left和non_leaf_split_right1的左右非叶子节点的个数没有进行修改,导致了不平衡。 image

begeekmyfriend commented 2 years ago

已经修正了https://github.com/begeekmyfriend/bplustree/commit/ff6d20912d35426cab4e847d16c7f9606cb2d744 https://github.com/begeekmyfriend/bplustree/commit/9f1ab1f56eb81d4e628344185255cebe7a8a8e14 ,只要split+1即可,年代久远,之前的代码有点生疏了。