summaryrefslogtreecommitdiff
path: root/Algorithms/bplus-tree/src/bplus_insert.c
blob: 9cd3ea789354b7dd3c0f40c4053080732fdcf7ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
 * Distributed under the Boost Software License, Version 1.0.
 * See accompanying file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt
 */

#include "bplus_insert.h"

#include "bplus_node.h"
#include "bplus_search.h"
#include "bplus_rebalance.h"

#include <string.h>

static void bplus_leaf_insert_at(BplusTree const* tree, BplusNode* node, size_t const index, BplusKey const key, BplusValue const value)
{
    g_return_if_fail(node != NULL);
    g_return_if_fail(index <= node->length);

    bplus_node_move_and_resize_data(tree, node, index + 1, index);
    bplus_key_at(node, index)   = key;
    bplus_value_at(node, index) = value;
}

void bplus_node_insert_at(BplusTree const* tree, BplusNode* node, size_t const index, size_t const length, BplusItem const* const items)
{
    g_return_if_fail(node != NULL);
    g_return_if_fail(index <= node->length);
    g_return_if_fail(node->length + length <= BPLUS_TREE_ORDER);

    bplus_node_move_and_resize_data(tree, node, index + length, index);
    memcpy(node->items + index, items, length * sizeof(BplusItem));

    if (node->is_leaf)
        return;

    for (size_t i = index; i < index + length; ++i)
        bplus_node_at(node, i)->parent = node;
}

void bplus_tree_insert(BplusTree* tree, BplusKey const key, BplusValue const value)
{
    BplusPath path;
    bplus_tree_get_path_to_key(tree, key, &path);

    size_t     index = path.index[0];
    BplusNode* node  = (BplusNode*) path.leaf;

    /* If the node is not empty, we will insert it after the given index */
    if ((index < node->length) && bplus_key_lte(tree, bplus_key_at(node, index), key))
        index++;

    bplus_leaf_insert_at(tree, node, index, key, value);
    if (index == 0)
        bplus_rebalance_propagate(tree, &path);

    if (bplus_node_overfilled(node))
        bplus_rebalance_overfilled(tree, &path);
}