2
* Copyright (c) 2006 Jakub Jermar
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
9
* - Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* - Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
* - The name of the author may not be used to endorse or promote products
15
* derived from this software without specific prior written permission.
17
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
/** @addtogroup genericadt
35
* @brief B+tree implementation.
37
* This file implements B+tree type and operations.
39
* The B+tree has the following properties:
40
* @li it is a ballanced 3-4-5 tree (i.e. BTREE_M = 5)
41
* @li values (i.e. pointers to values) are stored only in leaves
42
* @li leaves are linked in a list
44
* Be carefull when using these trees. They need to allocate
45
* and deallocate memory for their index nodes and as such
49
#include <adt/btree.h>
56
static void btree_destroy_subtree(btree_node_t *root);
57
static void _btree_insert(btree_t *t, btree_key_t key, void *value, btree_node_t *rsubtree, btree_node_t *node);
58
static void _btree_remove(btree_t *t, btree_key_t key, btree_node_t *node);
59
static void node_initialize(btree_node_t *node);
60
static void node_insert_key_and_lsubtree(btree_node_t *node, btree_key_t key, void *value, btree_node_t *lsubtree);
61
static void node_insert_key_and_rsubtree(btree_node_t *node, btree_key_t key, void *value, btree_node_t *rsubtree);
62
static void node_remove_key_and_lsubtree(btree_node_t *node, btree_key_t key);
63
static void node_remove_key_and_rsubtree(btree_node_t *node, btree_key_t key);
64
static btree_node_t *node_split(btree_node_t *node, btree_key_t key, void *value, btree_node_t *rsubtree, btree_key_t *median);
65
static btree_node_t *node_combine(btree_node_t *node);
66
static size_t find_key_by_subtree(btree_node_t *node, btree_node_t *subtree, bool right);
67
static void rotate_from_right(btree_node_t *lnode, btree_node_t *rnode, size_t idx);
68
static void rotate_from_left(btree_node_t *lnode, btree_node_t *rnode, size_t idx);
69
static bool try_insert_by_rotation_to_left(btree_node_t *node, btree_key_t key, void *value, btree_node_t *rsubtree);
70
static bool try_insert_by_rotation_to_right(btree_node_t *node, btree_key_t key, void *value, btree_node_t *rsubtree);
71
static bool try_rotation_from_left(btree_node_t *rnode);
72
static bool try_rotation_from_right(btree_node_t *lnode);
74
#define ROOT_NODE(n) (!(n)->parent)
75
#define INDEX_NODE(n) ((n)->subtree[0] != NULL)
76
#define LEAF_NODE(n) ((n)->subtree[0] == NULL)
78
#define FILL_FACTOR ((BTREE_M-1)/2)
80
#define MEDIAN_LOW_INDEX(n) (((n)->keys-1)/2)
81
#define MEDIAN_HIGH_INDEX(n) ((n)->keys/2)
82
#define MEDIAN_LOW(n) ((n)->key[MEDIAN_LOW_INDEX((n))]);
83
#define MEDIAN_HIGH(n) ((n)->key[MEDIAN_HIGH_INDEX((n))]);
85
static slab_cache_t *btree_node_slab;
87
/** Initialize B-trees. */
90
btree_node_slab = slab_cache_create("btree_node_slab", sizeof(btree_node_t), 0, NULL, NULL, SLAB_CACHE_MAGDEFERRED);
93
/** Create empty B-tree.
97
void btree_create(btree_t *t)
99
list_initialize(&t->leaf_head);
100
t->root = (btree_node_t *) slab_alloc(btree_node_slab, 0);
101
node_initialize(t->root);
102
list_append(&t->root->leaf_link, &t->leaf_head);
105
/** Destroy empty B-tree. */
106
void btree_destroy(btree_t *t)
108
btree_destroy_subtree(t->root);
111
/** Insert key-value pair into B-tree.
114
* @param key Key to be inserted.
115
* @param value Value to be inserted.
116
* @param leaf_node Leaf node where the insertion should begin.
118
void btree_insert(btree_t *t, btree_key_t key, void *value, btree_node_t *leaf_node)
126
if (btree_search(t, key, &lnode)) {
127
panic("B-tree %p already contains key %" PRIu64 ".", t, key);
131
_btree_insert(t, key, value, NULL, lnode);
134
/** Destroy subtree rooted in a node.
136
* @param root Root of the subtree.
138
void btree_destroy_subtree(btree_node_t *root)
143
for (i = 0; i < root->keys + 1; i++) {
144
if (root->subtree[i])
145
btree_destroy_subtree(root->subtree[i]);
148
slab_free(btree_node_slab, root);
151
/** Recursively insert into B-tree.
154
* @param key Key to be inserted.
155
* @param value Value to be inserted.
156
* @param rsubtree Right subtree of the inserted key.
157
* @param node Start inserting into this node.
159
void _btree_insert(btree_t *t, btree_key_t key, void *value, btree_node_t *rsubtree, btree_node_t *node)
161
if (node->keys < BTREE_MAX_KEYS) {
163
* Node conatins enough space, the key can be stored immediately.
165
node_insert_key_and_rsubtree(node, key, value, rsubtree);
166
} else if (try_insert_by_rotation_to_left(node, key, value, rsubtree)) {
168
* The key-value-rsubtree triplet has been inserted because
169
* some keys could have been moved to the left sibling.
171
} else if (try_insert_by_rotation_to_right(node, key, value, rsubtree)) {
173
* The key-value-rsubtree triplet has been inserted because
174
* some keys could have been moved to the right sibling.
181
* Node is full and both siblings (if both exist) are full too.
182
* Split the node and insert the smallest key from the node containing
183
* bigger keys (i.e. the new node) into its parent.
186
rnode = node_split(node, key, value, rsubtree, &median);
188
if (LEAF_NODE(node)) {
189
list_prepend(&rnode->leaf_link, &node->leaf_link);
192
if (ROOT_NODE(node)) {
194
* We split the root node. Create new root.
196
t->root = (btree_node_t *) slab_alloc(btree_node_slab, 0);
197
node->parent = t->root;
198
rnode->parent = t->root;
199
node_initialize(t->root);
202
* Left-hand side subtree will be the old root (i.e. node).
203
* Right-hand side subtree will be rnode.
205
t->root->subtree[0] = node;
207
t->root->depth = node->depth + 1;
209
_btree_insert(t, median, NULL, rnode, node->parent);
214
/** Remove B-tree node.
217
* @param key Key to be removed from the B-tree along with its associated value.
218
* @param leaf_node If not NULL, pointer to the leaf node where the key is found.
220
void btree_remove(btree_t *t, btree_key_t key, btree_node_t *leaf_node)
226
if (!btree_search(t, key, &lnode)) {
227
panic("B-tree %p does not contain key %" PRIu64 ".", t, key);
231
_btree_remove(t, key, lnode);
234
/** Recursively remove B-tree node.
237
* @param key Key to be removed from the B-tree along with its associated value.
238
* @param node Node where the key being removed resides.
240
void _btree_remove(btree_t *t, btree_key_t key, btree_node_t *node)
242
if (ROOT_NODE(node)) {
243
if (node->keys == 1 && node->subtree[0]) {
245
* Free the current root and set new root.
247
t->root = node->subtree[0];
248
t->root->parent = NULL;
249
slab_free(btree_node_slab, node);
252
* Remove the key from the root node.
253
* Note that the right subtree is removed because when
254
* combining two nodes, the left-side sibling is preserved
255
* and the right-side sibling is freed.
257
node_remove_key_and_rsubtree(node, key);
262
if (node->keys <= FILL_FACTOR) {
264
* If the node is below the fill factor,
265
* try to borrow keys from left or right sibling.
267
if (!try_rotation_from_left(node))
268
try_rotation_from_right(node);
271
if (node->keys > FILL_FACTOR) {
275
* The key can be immediatelly removed.
277
* Note that the right subtree is removed because when
278
* combining two nodes, the left-side sibling is preserved
279
* and the right-side sibling is freed.
281
node_remove_key_and_rsubtree(node, key);
282
for (i = 0; i < node->parent->keys; i++) {
283
if (node->parent->key[i] == key)
284
node->parent->key[i] = node->key[0];
289
btree_node_t *rnode, *parent;
292
* The node is below the fill factor as well as its left and right sibling.
293
* Resort to combining the node with one of its siblings.
294
* The node which is on the left is preserved and the node on the right is
297
parent = node->parent;
298
node_remove_key_and_rsubtree(node, key);
299
rnode = node_combine(node);
300
if (LEAF_NODE(rnode))
301
list_remove(&rnode->leaf_link);
302
idx = find_key_by_subtree(parent, rnode, true);
303
ASSERT((int) idx != -1);
304
slab_free(btree_node_slab, rnode);
305
_btree_remove(t, parent->key[idx], parent);
309
/** Search key in a B-tree.
312
* @param key Key to be searched.
313
* @param leaf_node Address where to put pointer to visited leaf node.
315
* @return Pointer to value or NULL if there is no such key.
317
void *btree_search(btree_t *t, btree_key_t key, btree_node_t **leaf_node)
319
btree_node_t *cur, *next;
322
* Iteratively descend to the leaf that can contain the searched key.
324
for (cur = t->root; cur; cur = next) {
326
/* Last iteration will set this with proper leaf node address. */
330
* The key can be in the leftmost subtree.
331
* Test it separately.
333
if (key < cur->key[0]) {
334
next = cur->subtree[0];
341
* Now if the key is smaller than cur->key[i]
342
* it can only mean that the value is in cur->subtree[i]
343
* or it is not in the tree at all.
345
for (i = 1; i < cur->keys; i++) {
346
if (key < cur->key[i]) {
347
next = cur->subtree[i];
348
val = cur->value[i - 1];
351
return key == cur->key[i - 1] ? val : NULL;
358
* Last possibility is that the key is in the rightmost subtree.
360
next = cur->subtree[i];
361
val = cur->value[i - 1];
363
return key == cur->key[i - 1] ? val : NULL;
370
* The key was not found in the *leaf_node and is smaller than any of its keys.
375
/** Return pointer to B-tree leaf node's left neighbour.
378
* @param node Node whose left neighbour will be returned.
380
* @return Left neighbour of the node or NULL if the node does not have the left neighbour.
382
btree_node_t *btree_leaf_node_left_neighbour(btree_t *t, btree_node_t *node)
384
ASSERT(LEAF_NODE(node));
385
if (node->leaf_link.prev != &t->leaf_head)
386
return list_get_instance(node->leaf_link.prev, btree_node_t, leaf_link);
391
/** Return pointer to B-tree leaf node's right neighbour.
394
* @param node Node whose right neighbour will be returned.
396
* @return Right neighbour of the node or NULL if the node does not have the right neighbour.
398
btree_node_t *btree_leaf_node_right_neighbour(btree_t *t, btree_node_t *node)
400
ASSERT(LEAF_NODE(node));
401
if (node->leaf_link.next != &t->leaf_head)
402
return list_get_instance(node->leaf_link.next, btree_node_t, leaf_link);
407
/** Initialize B-tree node.
409
* @param node B-tree node.
411
void node_initialize(btree_node_t *node)
417
/* Clean also space for the extra key. */
418
for (i = 0; i < BTREE_MAX_KEYS + 1; i++) {
420
node->value[i] = NULL;
421
node->subtree[i] = NULL;
423
node->subtree[i] = NULL;
427
link_initialize(&node->leaf_link);
429
link_initialize(&node->bfs_link);
433
/** Insert key-value-lsubtree triplet into B-tree node.
435
* It is actually possible to have more keys than BTREE_MAX_KEYS.
436
* This feature is used during insert by right rotation.
438
* @param node B-tree node into wich the new key is to be inserted.
439
* @param key The key to be inserted.
440
* @param value Pointer to value to be inserted.
441
* @param lsubtree Pointer to the left subtree.
443
void node_insert_key_and_lsubtree(btree_node_t *node, btree_key_t key, void *value, btree_node_t *lsubtree)
447
for (i = 0; i < node->keys; i++) {
448
if (key < node->key[i]) {
451
for (j = node->keys; j > i; j--) {
452
node->key[j] = node->key[j - 1];
453
node->value[j] = node->value[j - 1];
454
node->subtree[j + 1] = node->subtree[j];
456
node->subtree[j + 1] = node->subtree[j];
461
node->value[i] = value;
462
node->subtree[i] = lsubtree;
467
/** Insert key-value-rsubtree triplet into B-tree node.
469
* It is actually possible to have more keys than BTREE_MAX_KEYS.
470
* This feature is used during splitting the node when the
471
* number of keys is BTREE_MAX_KEYS + 1. Insert by left rotation
472
* also makes use of this feature.
474
* @param node B-tree node into wich the new key is to be inserted.
475
* @param key The key to be inserted.
476
* @param value Pointer to value to be inserted.
477
* @param rsubtree Pointer to the right subtree.
479
void node_insert_key_and_rsubtree(btree_node_t *node, btree_key_t key, void *value, btree_node_t *rsubtree)
483
for (i = 0; i < node->keys; i++) {
484
if (key < node->key[i]) {
487
for (j = node->keys; j > i; j--) {
488
node->key[j] = node->key[j - 1];
489
node->value[j] = node->value[j - 1];
490
node->subtree[j + 1] = node->subtree[j];
496
node->value[i] = value;
497
node->subtree[i + 1] = rsubtree;
502
/** Remove key and its left subtree pointer from B-tree node.
504
* Remove the key and eliminate gaps in node->key array.
505
* Note that the value pointer and the left subtree pointer
506
* is removed from the node as well.
508
* @param node B-tree node.
509
* @param key Key to be removed.
511
void node_remove_key_and_lsubtree(btree_node_t *node, btree_key_t key)
515
for (i = 0; i < node->keys; i++) {
516
if (key == node->key[i]) {
517
for (j = i + 1; j < node->keys; j++) {
518
node->key[j - 1] = node->key[j];
519
node->value[j - 1] = node->value[j];
520
node->subtree[j - 1] = node->subtree[j];
522
node->subtree[j - 1] = node->subtree[j];
527
panic("Node %p does not contain key %" PRIu64 ".", node, key);
530
/** Remove key and its right subtree pointer from B-tree node.
532
* Remove the key and eliminate gaps in node->key array.
533
* Note that the value pointer and the right subtree pointer
534
* is removed from the node as well.
536
* @param node B-tree node.
537
* @param key Key to be removed.
539
void node_remove_key_and_rsubtree(btree_node_t *node, btree_key_t key)
543
for (i = 0; i < node->keys; i++) {
544
if (key == node->key[i]) {
545
for (j = i + 1; j < node->keys; j++) {
546
node->key[j - 1] = node->key[j];
547
node->value[j - 1] = node->value[j];
548
node->subtree[j] = node->subtree[j + 1];
554
panic("Node %p does not contain key %" PRIu64 ".", node, key);
557
/** Split full B-tree node and insert new key-value-right-subtree triplet.
559
* This function will split a node and return a pointer to a newly created
560
* node containing keys greater than or equal to the greater of medians
561
* (or median) of the old keys and the newly added key. It will also write
562
* the median key to a memory address supplied by the caller.
564
* If the node being split is an index node, the median will not be
565
* included in the new node. If the node is a leaf node,
566
* the median will be copied there.
568
* @param node B-tree node wich is going to be split.
569
* @param key The key to be inserted.
570
* @param value Pointer to the value to be inserted.
571
* @param rsubtree Pointer to the right subtree of the key being added.
572
* @param median Address in memory, where the median key will be stored.
574
* @return Newly created right sibling of node.
576
btree_node_t *node_split(btree_node_t *node, btree_key_t key, void *value, btree_node_t *rsubtree, btree_key_t *median)
582
ASSERT(node->keys == BTREE_MAX_KEYS);
585
* Use the extra space to store the extra node.
587
node_insert_key_and_rsubtree(node, key, value, rsubtree);
590
* Compute median of keys.
592
*median = MEDIAN_HIGH(node);
595
* Allocate and initialize new right sibling.
597
rnode = (btree_node_t *) slab_alloc(btree_node_slab, 0);
598
node_initialize(rnode);
599
rnode->parent = node->parent;
600
rnode->depth = node->depth;
603
* Copy big keys, values and subtree pointers to the new right sibling.
604
* If this is an index node, do not copy the median.
606
i = (size_t) INDEX_NODE(node);
607
for (i += MEDIAN_HIGH_INDEX(node), j = 0; i < node->keys; i++, j++) {
608
rnode->key[j] = node->key[i];
609
rnode->value[j] = node->value[i];
610
rnode->subtree[j] = node->subtree[i];
613
* Fix parent links in subtrees.
615
if (rnode->subtree[j])
616
rnode->subtree[j]->parent = rnode;
619
rnode->subtree[j] = node->subtree[i];
620
if (rnode->subtree[j])
621
rnode->subtree[j]->parent = rnode;
623
rnode->keys = j; /* Set number of keys of the new node. */
624
node->keys /= 2; /* Shrink the old node. */
629
/** Combine node with any of its siblings.
631
* The siblings are required to be below the fill factor.
633
* @param node Node to combine with one of its siblings.
635
* @return Pointer to the rightmost of the two nodes.
637
btree_node_t *node_combine(btree_node_t *node)
643
ASSERT(!ROOT_NODE(node));
645
idx = find_key_by_subtree(node->parent, node, false);
646
if (idx == node->parent->keys) {
648
* Rightmost subtree of its parent, combine with the left sibling.
652
node = node->parent->subtree[idx];
654
rnode = node->parent->subtree[idx + 1];
657
/* Index nodes need to insert parent node key in between left and right node. */
658
if (INDEX_NODE(node))
659
node->key[node->keys++] = node->parent->key[idx];
661
/* Copy the key-value-subtree triplets from the right node. */
662
for (i = 0; i < rnode->keys; i++) {
663
node->key[node->keys + i] = rnode->key[i];
664
node->value[node->keys + i] = rnode->value[i];
665
if (INDEX_NODE(node)) {
666
node->subtree[node->keys + i] = rnode->subtree[i];
667
rnode->subtree[i]->parent = node;
670
if (INDEX_NODE(node)) {
671
node->subtree[node->keys + i] = rnode->subtree[i];
672
rnode->subtree[i]->parent = node;
675
node->keys += rnode->keys;
680
/** Find key by its left or right subtree.
682
* @param node B-tree node.
683
* @param subtree Left or right subtree of a key found in node.
684
* @param right If true, subtree is a right subtree. If false, subtree is a left subtree.
686
* @return Index of the key associated with the subtree.
688
size_t find_key_by_subtree(btree_node_t *node, btree_node_t *subtree, bool right)
692
for (i = 0; i < node->keys + 1; i++) {
693
if (subtree == node->subtree[i])
694
return i - (int) (right != false);
696
panic("Node %p does not contain subtree %p.", node, subtree);
699
/** Rotate one key-value-rsubtree triplet from the left sibling to the right sibling.
701
* The biggest key and its value and right subtree is rotated from the left node
702
* to the right. If the node is an index node, than the parent node key belonging to
703
* the left node takes part in the rotation.
705
* @param lnode Left sibling.
706
* @param rnode Right sibling.
707
* @param idx Index of the parent node key that is taking part in the rotation.
709
void rotate_from_left(btree_node_t *lnode, btree_node_t *rnode, size_t idx)
713
key = lnode->key[lnode->keys - 1];
715
if (LEAF_NODE(lnode)) {
718
value = lnode->value[lnode->keys - 1];
719
node_remove_key_and_rsubtree(lnode, key);
720
node_insert_key_and_lsubtree(rnode, key, value, NULL);
721
lnode->parent->key[idx] = key;
723
btree_node_t *rsubtree;
725
rsubtree = lnode->subtree[lnode->keys];
726
node_remove_key_and_rsubtree(lnode, key);
727
node_insert_key_and_lsubtree(rnode, lnode->parent->key[idx], NULL, rsubtree);
728
lnode->parent->key[idx] = key;
730
/* Fix parent link of the reconnected right subtree. */
731
rsubtree->parent = rnode;
736
/** Rotate one key-value-lsubtree triplet from the right sibling to the left sibling.
738
* The smallest key and its value and left subtree is rotated from the right node
739
* to the left. If the node is an index node, than the parent node key belonging to
740
* the right node takes part in the rotation.
742
* @param lnode Left sibling.
743
* @param rnode Right sibling.
744
* @param idx Index of the parent node key that is taking part in the rotation.
746
void rotate_from_right(btree_node_t *lnode, btree_node_t *rnode, size_t idx)
752
if (LEAF_NODE(rnode)) {
755
value = rnode->value[0];
756
node_remove_key_and_lsubtree(rnode, key);
757
node_insert_key_and_rsubtree(lnode, key, value, NULL);
758
rnode->parent->key[idx] = rnode->key[0];
760
btree_node_t *lsubtree;
762
lsubtree = rnode->subtree[0];
763
node_remove_key_and_lsubtree(rnode, key);
764
node_insert_key_and_rsubtree(lnode, rnode->parent->key[idx], NULL, lsubtree);
765
rnode->parent->key[idx] = key;
767
/* Fix parent link of the reconnected left subtree. */
768
lsubtree->parent = lnode;
773
/** Insert key-value-rsubtree triplet and rotate the node to the left, if this operation can be done.
775
* Left sibling of the node (if it exists) is checked for free space.
776
* If there is free space, the key is inserted and the smallest key of
777
* the node is moved there. The index node which is the parent of both
780
* @param node B-tree node.
781
* @param inskey Key to be inserted.
782
* @param insvalue Value to be inserted.
783
* @param rsubtree Right subtree of inskey.
785
* @return True if the rotation was performed, false otherwise.
787
bool try_insert_by_rotation_to_left(btree_node_t *node, btree_key_t inskey, void *insvalue, btree_node_t *rsubtree)
793
* If this is root node, the rotation can not be done.
798
idx = find_key_by_subtree(node->parent, node, true);
799
if ((int) idx == -1) {
801
* If this node is the leftmost subtree of its parent,
802
* the rotation can not be done.
807
lnode = node->parent->subtree[idx];
808
if (lnode->keys < BTREE_MAX_KEYS) {
810
* The rotaion can be done. The left sibling has free space.
812
node_insert_key_and_rsubtree(node, inskey, insvalue, rsubtree);
813
rotate_from_right(lnode, node, idx);
820
/** Insert key-value-rsubtree triplet and rotate the node to the right, if this operation can be done.
822
* Right sibling of the node (if it exists) is checked for free space.
823
* If there is free space, the key is inserted and the biggest key of
824
* the node is moved there. The index node which is the parent of both
827
* @param node B-tree node.
828
* @param inskey Key to be inserted.
829
* @param insvalue Value to be inserted.
830
* @param rsubtree Right subtree of inskey.
832
* @return True if the rotation was performed, false otherwise.
834
bool try_insert_by_rotation_to_right(btree_node_t *node, btree_key_t inskey, void *insvalue, btree_node_t *rsubtree)
840
* If this is root node, the rotation can not be done.
845
idx = find_key_by_subtree(node->parent, node, false);
846
if (idx == node->parent->keys) {
848
* If this node is the rightmost subtree of its parent,
849
* the rotation can not be done.
854
rnode = node->parent->subtree[idx + 1];
855
if (rnode->keys < BTREE_MAX_KEYS) {
857
* The rotaion can be done. The right sibling has free space.
859
node_insert_key_and_rsubtree(node, inskey, insvalue, rsubtree);
860
rotate_from_left(node, rnode, idx);
867
/** Rotate in a key from the left sibling or from the index node, if this operation can be done.
869
* @param rnode Node into which to add key from its left sibling or from the index node.
871
* @return True if the rotation was performed, false otherwise.
873
bool try_rotation_from_left(btree_node_t *rnode)
879
* If this is root node, the rotation can not be done.
881
if (ROOT_NODE(rnode))
884
idx = find_key_by_subtree(rnode->parent, rnode, true);
885
if ((int) idx == -1) {
887
* If this node is the leftmost subtree of its parent,
888
* the rotation can not be done.
893
lnode = rnode->parent->subtree[idx];
894
if (lnode->keys > FILL_FACTOR) {
895
rotate_from_left(lnode, rnode, idx);
902
/** Rotate in a key from the right sibling or from the index node, if this operation can be done.
904
* @param lnode Node into which to add key from its right sibling or from the index node.
906
* @return True if the rotation was performed, false otherwise.
908
bool try_rotation_from_right(btree_node_t *lnode)
914
* If this is root node, the rotation can not be done.
916
if (ROOT_NODE(lnode))
919
idx = find_key_by_subtree(lnode->parent, lnode, false);
920
if (idx == lnode->parent->keys) {
922
* If this node is the rightmost subtree of its parent,
923
* the rotation can not be done.
928
rnode = lnode->parent->subtree[idx + 1];
929
if (rnode->keys > FILL_FACTOR) {
930
rotate_from_right(lnode, rnode, idx);
939
* @param t Print out B-tree.
941
void btree_print(btree_t *t)
944
int depth = t->root->depth;
947
printf("Printing B-tree:\n");
948
list_initialize(&head);
949
list_append(&t->root->bfs_link, &head);
952
* Use BFS search to print out the tree.
953
* Levels are distinguished from one another by node->depth.
955
while (!list_empty(&head)) {
960
ASSERT(hlp != &head);
961
node = list_get_instance(hlp, btree_node_t, bfs_link);
966
if (node->depth != depth) {
972
for (i = 0; i < node->keys; i++) {
973
printf("%" PRIu64 "%s", node->key[i], i < node->keys - 1 ? "," : "");
974
if (node->depth && node->subtree[i]) {
975
list_append(&node->subtree[i]->bfs_link, &head);
978
if (node->depth && node->subtree[i]) {
979
list_append(&node->subtree[i]->bfs_link, &head);
985
printf("Printing list of leaves:\n");
986
for (cur = t->leaf_head.next; cur != &t->leaf_head; cur = cur->next) {
989
node = list_get_instance(cur, btree_node_t, leaf_link);
994
for (i = 0; i < node->keys; i++)
995
printf("%" PRIu64 "%s", node->key[i], i < node->keys - 1 ? "," : "");