source: mainline/kernel/generic/src/adt/btree.c@ 314f4b59

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 314f4b59 was 850fd32, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix mischievious semicolons.

  • Property mode set to 100644
File size: 26.8 KB
RevLine 
[018d957e]1/*
[df4ed85]2 * Copyright (c) 2006 Jakub Jermar
[018d957e]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
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.
16 *
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.
27 */
28
[cc73a8a1]29/** @addtogroup genericadt
[b45c443]30 * @{
31 */
32
[9179d0a]33/**
[7257021e]34 * @file
[e3ee9b9]35 * @brief B+tree implementation.
[9179d0a]36 *
37 * This file implements B+tree type and operations.
38 *
39 * The B+tree has the following properties:
[1ab4aca]40 * @li it is a balanced 3-4-5 tree (i.e. BTREE_M = 5)
[9179d0a]41 * @li values (i.e. pointers to values) are stored only in leaves
42 * @li leaves are linked in a list
[018d957e]43 *
[1ab4aca]44 * Be careful when using these trees. They need to allocate
[c715e9b]45 * and deallocate memory for their index nodes and as such
46 * can sleep.
[018d957e]47 */
48
49#include <adt/btree.h>
50#include <adt/list.h>
[63e27ef]51#include <assert.h>
[018d957e]52#include <mm/slab.h>
53#include <panic.h>
54#include <print.h>
[7a0359b]55#include <trace.h>
[018d957e]56
[82d515e9]57static slab_cache_t *btree_node_cache;
[2810636]58
[e3ee9b9]59#define ROOT_NODE(n) (!(n)->parent)
60#define INDEX_NODE(n) ((n)->subtree[0] != NULL)
61#define LEAF_NODE(n) ((n)->subtree[0] == NULL)
[c715e9b]62
[e3ee9b9]63#define FILL_FACTOR ((BTREE_M - 1) / 2)
[c715e9b]64
[e3ee9b9]65#define MEDIAN_LOW_INDEX(n) (((n)->keys-1) / 2)
66#define MEDIAN_HIGH_INDEX(n) ((n)->keys / 2)
67#define MEDIAN_LOW(n) ((n)->key[MEDIAN_LOW_INDEX((n))]);
68#define MEDIAN_HIGH(n) ((n)->key[MEDIAN_HIGH_INDEX((n))]);
[018d957e]69
[e3ee9b9]70/** Initialize B-trees. */
71void btree_init(void)
[252127e]72{
[82d515e9]73 btree_node_cache = slab_cache_create("btree_node_t",
[e3ee9b9]74 sizeof(btree_node_t), 0, NULL, NULL, SLAB_CACHE_MAGDEFERRED);
[252127e]75}
76
[018d957e]77/** Initialize B-tree node.
78 *
79 * @param node B-tree node.
[e3ee9b9]80 *
[018d957e]81 */
[7a0359b]82NO_TRACE static void node_initialize(btree_node_t *node)
[018d957e]83{
[e3ee9b9]84 unsigned int i;
[a35b458]85
[018d957e]86 node->keys = 0;
[a35b458]87
[018d957e]88 /* Clean also space for the extra key. */
89 for (i = 0; i < BTREE_MAX_KEYS + 1; i++) {
90 node->key[i] = 0;
91 node->value[i] = NULL;
92 node->subtree[i] = NULL;
93 }
[a35b458]94
[e3ee9b9]95 node->subtree[i] = NULL;
[018d957e]96 node->parent = NULL;
[a35b458]97
[018d957e]98 link_initialize(&node->leaf_link);
99 link_initialize(&node->bfs_link);
100 node->depth = 0;
101}
102
[e3ee9b9]103/** Create empty B-tree.
[cc27ae48]104 *
[e3ee9b9]105 * @param t B-tree.
[cc27ae48]106 *
[e3ee9b9]107 */
108void btree_create(btree_t *t)
[cc27ae48]109{
[55b77d9]110 list_initialize(&t->leaf_list);
[82d515e9]111 t->root = (btree_node_t *) slab_alloc(btree_node_cache, 0);
[e3ee9b9]112 node_initialize(t->root);
[55b77d9]113 list_append(&t->root->leaf_link, &t->leaf_list);
[e3ee9b9]114}
[cc27ae48]115
[e3ee9b9]116/** Destroy subtree rooted in a node.
117 *
118 * @param root Root of the subtree.
119 *
120 */
[7a0359b]121NO_TRACE static void btree_destroy_subtree(btree_node_t *root)
[e3ee9b9]122{
123 size_t i;
[a35b458]124
[e3ee9b9]125 if (root->keys) {
[1b20da0]126 for (i = 0; i < root->keys + 1; i++) {
[e3ee9b9]127 if (root->subtree[i])
128 btree_destroy_subtree(root->subtree[i]);
[cc27ae48]129 }
130 }
[a35b458]131
[82d515e9]132 slab_free(btree_node_cache, root);
[e3ee9b9]133}
134
135/** Destroy empty B-tree. */
136void btree_destroy(btree_t *t)
137{
138 btree_destroy_subtree(t->root);
[cc27ae48]139}
140
141/** Insert key-value-rsubtree triplet into B-tree node.
[018d957e]142 *
143 * It is actually possible to have more keys than BTREE_MAX_KEYS.
144 * This feature is used during splitting the node when the
[cc27ae48]145 * number of keys is BTREE_MAX_KEYS + 1. Insert by left rotation
146 * also makes use of this feature.
[018d957e]147 *
[1ab4aca]148 * @param node B-tree node into which the new key is to be inserted.
[e3ee9b9]149 * @param key The key to be inserted.
150 * @param value Pointer to value to be inserted.
[018d957e]151 * @param rsubtree Pointer to the right subtree.
[e3ee9b9]152 *
153 */
[7a0359b]154NO_TRACE static void node_insert_key_and_rsubtree(btree_node_t *node,
155 btree_key_t key, void *value, btree_node_t *rsubtree)
[018d957e]156{
[98000fb]157 size_t i;
[a35b458]158
[018d957e]159 for (i = 0; i < node->keys; i++) {
160 if (key < node->key[i]) {
[98000fb]161 size_t j;
[a35b458]162
[018d957e]163 for (j = node->keys; j > i; j--) {
164 node->key[j] = node->key[j - 1];
165 node->value[j] = node->value[j - 1];
166 node->subtree[j + 1] = node->subtree[j];
167 }
[a35b458]168
[e3ee9b9]169 break;
[018d957e]170 }
171 }
[a35b458]172
[018d957e]173 node->key[i] = key;
174 node->value[i] = value;
175 node->subtree[i + 1] = rsubtree;
176 node->keys++;
177}
178
[e3ee9b9]179/** Find key by its left or right subtree.
180 *
181 * @param node B-tree node.
182 * @param subtree Left or right subtree of a key found in node.
183 * @param right If true, subtree is a right subtree. If false,
184 * subtree is a left subtree.
185 *
186 * @return Index of the key associated with the subtree.
187 *
188 */
[7a0359b]189NO_TRACE static size_t find_key_by_subtree(btree_node_t *node,
190 btree_node_t *subtree, bool right)
[e3ee9b9]191{
192 size_t i;
[a35b458]193
[e3ee9b9]194 for (i = 0; i < node->keys + 1; i++) {
195 if (subtree == node->subtree[i])
196 return i - (int) (right != false);
197 }
[a35b458]198
[e3ee9b9]199 panic("Node %p does not contain subtree %p.", node, subtree);
200}
201
[5b04fc7]202/** Remove key and its left subtree pointer from B-tree node.
203 *
204 * Remove the key and eliminate gaps in node->key array.
205 * Note that the value pointer and the left subtree pointer
206 * is removed from the node as well.
207 *
208 * @param node B-tree node.
[e3ee9b9]209 * @param key Key to be removed.
210 *
[5b04fc7]211 */
[7a0359b]212NO_TRACE static void node_remove_key_and_lsubtree(btree_node_t *node,
213 btree_key_t key)
[5b04fc7]214{
[e3ee9b9]215 size_t i;
216 size_t j;
[a35b458]217
[5b04fc7]218 for (i = 0; i < node->keys; i++) {
219 if (key == node->key[i]) {
220 for (j = i + 1; j < node->keys; j++) {
221 node->key[j - 1] = node->key[j];
222 node->value[j - 1] = node->value[j];
223 node->subtree[j - 1] = node->subtree[j];
224 }
[a35b458]225
[5b04fc7]226 node->subtree[j - 1] = node->subtree[j];
227 node->keys--;
[a35b458]228
[5b04fc7]229 return;
230 }
231 }
[a35b458]232
[f651e80]233 panic("Node %p does not contain key %" PRIu64 ".", node, key);
[5b04fc7]234}
235
236/** Remove key and its right subtree pointer from B-tree node.
237 *
238 * Remove the key and eliminate gaps in node->key array.
239 * Note that the value pointer and the right subtree pointer
240 * is removed from the node as well.
241 *
242 * @param node B-tree node.
[e3ee9b9]243 * @param key Key to be removed.
244 *
[5b04fc7]245 */
[7a0359b]246NO_TRACE static void node_remove_key_and_rsubtree(btree_node_t *node,
247 btree_key_t key)
[5b04fc7]248{
[98000fb]249 size_t i, j;
[a35b458]250
[5b04fc7]251 for (i = 0; i < node->keys; i++) {
252 if (key == node->key[i]) {
253 for (j = i + 1; j < node->keys; j++) {
254 node->key[j - 1] = node->key[j];
255 node->value[j - 1] = node->value[j];
256 node->subtree[j] = node->subtree[j + 1];
257 }
[a35b458]258
[5b04fc7]259 node->keys--;
260 return;
261 }
262 }
[a35b458]263
[e3ee9b9]264 panic("Node %p does not contain key %" PRIu64 ".", node, key);
265}
266
267/** Insert key-value-lsubtree triplet into B-tree node.
268 *
269 * It is actually possible to have more keys than BTREE_MAX_KEYS.
270 * This feature is used during insert by right rotation.
271 *
[1ab4aca]272 * @param node B-tree node into which the new key is to be inserted.
[e3ee9b9]273 * @param key The key to be inserted.
274 * @param value Pointer to value to be inserted.
275 * @param lsubtree Pointer to the left subtree.
276 *
277 */
[7a0359b]278NO_TRACE static void node_insert_key_and_lsubtree(btree_node_t *node,
279 btree_key_t key, void *value, btree_node_t *lsubtree)
[e3ee9b9]280{
281 size_t i;
[a35b458]282
[e3ee9b9]283 for (i = 0; i < node->keys; i++) {
284 if (key < node->key[i]) {
285 size_t j;
[a35b458]286
[e3ee9b9]287 for (j = node->keys; j > i; j--) {
288 node->key[j] = node->key[j - 1];
289 node->value[j] = node->value[j - 1];
290 node->subtree[j + 1] = node->subtree[j];
291 }
[a35b458]292
[e3ee9b9]293 node->subtree[j + 1] = node->subtree[j];
294 break;
295 }
296 }
[a35b458]297
[e3ee9b9]298 node->key[i] = key;
299 node->value[i] = value;
300 node->subtree[i] = lsubtree;
[a35b458]301
[e3ee9b9]302 node->keys++;
303}
304
305/** Rotate one key-value-rsubtree triplet from the left sibling to the right sibling.
306 *
307 * The biggest key and its value and right subtree is rotated
308 * from the left node to the right. If the node is an index node,
309 * than the parent node key belonging to the left node takes part
310 * in the rotation.
311 *
312 * @param lnode Left sibling.
313 * @param rnode Right sibling.
314 * @param idx Index of the parent node key that is taking part
315 * in the rotation.
316 *
317 */
[7a0359b]318NO_TRACE static void rotate_from_left(btree_node_t *lnode, btree_node_t *rnode,
319 size_t idx)
[e3ee9b9]320{
321 btree_key_t key = lnode->key[lnode->keys - 1];
[a35b458]322
[e3ee9b9]323 if (LEAF_NODE(lnode)) {
324 void *value = lnode->value[lnode->keys - 1];
[a35b458]325
[e3ee9b9]326 node_remove_key_and_rsubtree(lnode, key);
327 node_insert_key_and_lsubtree(rnode, key, value, NULL);
328 lnode->parent->key[idx] = key;
329 } else {
330 btree_node_t *rsubtree = lnode->subtree[lnode->keys];
[a35b458]331
[e3ee9b9]332 node_remove_key_and_rsubtree(lnode, key);
333 node_insert_key_and_lsubtree(rnode, lnode->parent->key[idx], NULL, rsubtree);
334 lnode->parent->key[idx] = key;
[a35b458]335
[e3ee9b9]336 /* Fix parent link of the reconnected right subtree. */
337 rsubtree->parent = rnode;
338 }
339}
340
341/** Rotate one key-value-lsubtree triplet from the right sibling to the left sibling.
342 *
343 * The smallest key and its value and left subtree is rotated
344 * from the right node to the left. If the node is an index node,
345 * than the parent node key belonging to the right node takes part
346 * in the rotation.
347 *
348 * @param lnode Left sibling.
349 * @param rnode Right sibling.
350 * @param idx Index of the parent node key that is taking part
351 * in the rotation.
352 *
353 */
[7a0359b]354NO_TRACE static void rotate_from_right(btree_node_t *lnode, btree_node_t *rnode,
355 size_t idx)
[e3ee9b9]356{
357 btree_key_t key = rnode->key[0];
[a35b458]358
[e3ee9b9]359 if (LEAF_NODE(rnode)) {
360 void *value = rnode->value[0];
[a35b458]361
[e3ee9b9]362 node_remove_key_and_lsubtree(rnode, key);
363 node_insert_key_and_rsubtree(lnode, key, value, NULL);
364 rnode->parent->key[idx] = rnode->key[0];
365 } else {
366 btree_node_t *lsubtree = rnode->subtree[0];
[a35b458]367
[e3ee9b9]368 node_remove_key_and_lsubtree(rnode, key);
369 node_insert_key_and_rsubtree(lnode, rnode->parent->key[idx], NULL, lsubtree);
370 rnode->parent->key[idx] = key;
[a35b458]371
[e3ee9b9]372 /* Fix parent link of the reconnected left subtree. */
373 lsubtree->parent = lnode;
374 }
375}
376
377/** Insert key-value-rsubtree triplet and rotate the node to the left, if this operation can be done.
378 *
379 * Left sibling of the node (if it exists) is checked for free space.
380 * If there is free space, the key is inserted and the smallest key of
381 * the node is moved there. The index node which is the parent of both
382 * nodes is fixed.
383 *
384 * @param node B-tree node.
385 * @param inskey Key to be inserted.
386 * @param insvalue Value to be inserted.
387 * @param rsubtree Right subtree of inskey.
388 *
389 * @return True if the rotation was performed, false otherwise.
390 *
391 */
[7a0359b]392NO_TRACE static bool try_insert_by_rotation_to_left(btree_node_t *node,
[e3ee9b9]393 btree_key_t inskey, void *insvalue, btree_node_t *rsubtree)
394{
395 size_t idx;
396 btree_node_t *lnode;
[a35b458]397
[e3ee9b9]398 /*
399 * If this is root node, the rotation can not be done.
400 */
401 if (ROOT_NODE(node))
402 return false;
[a35b458]403
[e3ee9b9]404 idx = find_key_by_subtree(node->parent, node, true);
405 if ((int) idx == -1) {
406 /*
407 * If this node is the leftmost subtree of its parent,
408 * the rotation can not be done.
409 */
410 return false;
411 }
[a35b458]412
[e3ee9b9]413 lnode = node->parent->subtree[idx];
414 if (lnode->keys < BTREE_MAX_KEYS) {
415 /*
416 * The rotaion can be done. The left sibling has free space.
417 */
418 node_insert_key_and_rsubtree(node, inskey, insvalue, rsubtree);
419 rotate_from_right(lnode, node, idx);
420 return true;
421 }
[a35b458]422
[e3ee9b9]423 return false;
424}
425
426/** Insert key-value-rsubtree triplet and rotate the node to the right, if this operation can be done.
427 *
428 * Right sibling of the node (if it exists) is checked for free space.
429 * If there is free space, the key is inserted and the biggest key of
430 * the node is moved there. The index node which is the parent of both
431 * nodes is fixed.
432 *
433 * @param node B-tree node.
434 * @param inskey Key to be inserted.
435 * @param insvalue Value to be inserted.
436 * @param rsubtree Right subtree of inskey.
437 *
438 * @return True if the rotation was performed, false otherwise.
439 *
440 */
[7a0359b]441NO_TRACE static bool try_insert_by_rotation_to_right(btree_node_t *node,
[e3ee9b9]442 btree_key_t inskey, void *insvalue, btree_node_t *rsubtree)
443{
444 size_t idx;
445 btree_node_t *rnode;
[a35b458]446
[e3ee9b9]447 /*
448 * If this is root node, the rotation can not be done.
449 */
450 if (ROOT_NODE(node))
451 return false;
[a35b458]452
[e3ee9b9]453 idx = find_key_by_subtree(node->parent, node, false);
454 if (idx == node->parent->keys) {
455 /*
456 * If this node is the rightmost subtree of its parent,
457 * the rotation can not be done.
458 */
459 return false;
460 }
[a35b458]461
[e3ee9b9]462 rnode = node->parent->subtree[idx + 1];
463 if (rnode->keys < BTREE_MAX_KEYS) {
464 /*
[1ab4aca]465 * The rotation can be done. The right sibling has free space.
[e3ee9b9]466 */
467 node_insert_key_and_rsubtree(node, inskey, insvalue, rsubtree);
468 rotate_from_left(node, rnode, idx);
469 return true;
470 }
[a35b458]471
[e3ee9b9]472 return false;
[5b04fc7]473}
474
[c715e9b]475/** Split full B-tree node and insert new key-value-right-subtree triplet.
[018d957e]476 *
[cc73a8a1]477 * This function will split a node and return a pointer to a newly created
[c715e9b]478 * node containing keys greater than or equal to the greater of medians
479 * (or median) of the old keys and the newly added key. It will also write
480 * the median key to a memory address supplied by the caller.
[018d957e]481 *
[c715e9b]482 * If the node being split is an index node, the median will not be
483 * included in the new node. If the node is a leaf node,
484 * the median will be copied there.
[018d957e]485 *
[1ab4aca]486 * @param node B-tree node which is going to be split.
[e3ee9b9]487 * @param key The key to be inserted.
488 * @param value Pointer to the value to be inserted.
[018d957e]489 * @param rsubtree Pointer to the right subtree of the key being added.
[e3ee9b9]490 * @param median Address in memory, where the median key will be stored.
[018d957e]491 *
492 * @return Newly created right sibling of node.
[e3ee9b9]493 *
494 */
[7a0359b]495NO_TRACE static btree_node_t *node_split(btree_node_t *node, btree_key_t key,
[e3ee9b9]496 void *value, btree_node_t *rsubtree, btree_key_t *median)
[018d957e]497{
498 btree_node_t *rnode;
[e3ee9b9]499 size_t i;
500 size_t j;
[a35b458]501
[63e27ef]502 assert(median);
503 assert(node->keys == BTREE_MAX_KEYS);
[a35b458]504
[018d957e]505 /*
506 * Use the extra space to store the extra node.
507 */
[0cb56f5d]508 node_insert_key_and_rsubtree(node, key, value, rsubtree);
[a35b458]509
[018d957e]510 /*
511 * Compute median of keys.
512 */
[c715e9b]513 *median = MEDIAN_HIGH(node);
[a35b458]514
[c715e9b]515 /*
516 * Allocate and initialize new right sibling.
517 */
[82d515e9]518 rnode = (btree_node_t *) slab_alloc(btree_node_cache, 0);
[018d957e]519 node_initialize(rnode);
520 rnode->parent = node->parent;
521 rnode->depth = node->depth;
[a35b458]522
[018d957e]523 /*
524 * Copy big keys, values and subtree pointers to the new right sibling.
[c715e9b]525 * If this is an index node, do not copy the median.
[018d957e]526 */
[98000fb]527 i = (size_t) INDEX_NODE(node);
[c715e9b]528 for (i += MEDIAN_HIGH_INDEX(node), j = 0; i < node->keys; i++, j++) {
[018d957e]529 rnode->key[j] = node->key[i];
530 rnode->value[j] = node->value[i];
531 rnode->subtree[j] = node->subtree[i];
[a35b458]532
[018d957e]533 /*
534 * Fix parent links in subtrees.
535 */
536 if (rnode->subtree[j])
537 rnode->subtree[j]->parent = rnode;
538 }
[a35b458]539
[018d957e]540 rnode->subtree[j] = node->subtree[i];
541 if (rnode->subtree[j])
542 rnode->subtree[j]->parent = rnode;
[a35b458]543
[e3ee9b9]544 rnode->keys = j; /* Set number of keys of the new node. */
545 node->keys /= 2; /* Shrink the old node. */
[a35b458]546
[e3ee9b9]547 return rnode;
548}
[c715e9b]549
[e3ee9b9]550/** Recursively insert into B-tree.
551 *
552 * @param t B-tree.
553 * @param key Key to be inserted.
554 * @param value Value to be inserted.
555 * @param rsubtree Right subtree of the inserted key.
556 * @param node Start inserting into this node.
557 *
558 */
[7a0359b]559NO_TRACE static void _btree_insert(btree_t *t, btree_key_t key, void *value,
[e3ee9b9]560 btree_node_t *rsubtree, btree_node_t *node)
561{
562 if (node->keys < BTREE_MAX_KEYS) {
563 /*
[1ab4aca]564 * Node contains enough space, the key can be stored immediately.
[e3ee9b9]565 */
566 node_insert_key_and_rsubtree(node, key, value, rsubtree);
567 } else if (try_insert_by_rotation_to_left(node, key, value, rsubtree)) {
568 /*
569 * The key-value-rsubtree triplet has been inserted because
570 * some keys could have been moved to the left sibling.
571 */
572 } else if (try_insert_by_rotation_to_right(node, key, value, rsubtree)) {
573 /*
574 * The key-value-rsubtree triplet has been inserted because
575 * some keys could have been moved to the right sibling.
576 */
577 } else {
578 btree_node_t *rnode;
579 btree_key_t median;
[a35b458]580
[e3ee9b9]581 /*
582 * Node is full and both siblings (if both exist) are full too.
583 * Split the node and insert the smallest key from the node containing
584 * bigger keys (i.e. the new node) into its parent.
585 */
[a35b458]586
[e3ee9b9]587 rnode = node_split(node, key, value, rsubtree, &median);
[a35b458]588
[e3ee9b9]589 if (LEAF_NODE(node)) {
[55b77d9]590 list_insert_after(&rnode->leaf_link, &node->leaf_link);
[e3ee9b9]591 }
[a35b458]592
[e3ee9b9]593 if (ROOT_NODE(node)) {
594 /*
595 * We split the root node. Create new root.
596 */
[82d515e9]597 t->root = (btree_node_t *) slab_alloc(btree_node_cache, 0);
[e3ee9b9]598 node->parent = t->root;
599 rnode->parent = t->root;
600 node_initialize(t->root);
[a35b458]601
[e3ee9b9]602 /*
603 * Left-hand side subtree will be the old root (i.e. node).
604 * Right-hand side subtree will be rnode.
605 */
606 t->root->subtree[0] = node;
[a35b458]607
[e3ee9b9]608 t->root->depth = node->depth + 1;
609 }
610 _btree_insert(t, median, NULL, rnode, node->parent);
611 }
612}
613
614/** Insert key-value pair into B-tree.
615 *
616 * @param t B-tree.
617 * @param key Key to be inserted.
618 * @param value Value to be inserted.
619 * @param leaf_node Leaf node where the insertion should begin.
620 *
621 */
622void btree_insert(btree_t *t, btree_key_t key, void *value,
623 btree_node_t *leaf_node)
624{
625 btree_node_t *lnode;
[a35b458]626
[63e27ef]627 assert(value);
[a35b458]628
[e3ee9b9]629 lnode = leaf_node;
630 if (!lnode) {
631 if (btree_search(t, key, &lnode))
632 panic("B-tree %p already contains key %" PRIu64 ".", t, key);
633 }
[a35b458]634
[e3ee9b9]635 _btree_insert(t, key, value, NULL, lnode);
636}
637
638/** Rotate in a key from the left sibling or from the index node, if this operation can be done.
639 *
640 * @param rnode Node into which to add key from its left sibling
641 * or from the index node.
642 *
643 * @return True if the rotation was performed, false otherwise.
644 *
645 */
[7a0359b]646NO_TRACE static bool try_rotation_from_left(btree_node_t *rnode)
[e3ee9b9]647{
648 size_t idx;
649 btree_node_t *lnode;
[a35b458]650
[e3ee9b9]651 /*
652 * If this is root node, the rotation can not be done.
653 */
654 if (ROOT_NODE(rnode))
655 return false;
[a35b458]656
[e3ee9b9]657 idx = find_key_by_subtree(rnode->parent, rnode, true);
658 if ((int) idx == -1) {
659 /*
660 * If this node is the leftmost subtree of its parent,
661 * the rotation can not be done.
662 */
663 return false;
664 }
[a35b458]665
[e3ee9b9]666 lnode = rnode->parent->subtree[idx];
667 if (lnode->keys > FILL_FACTOR) {
668 rotate_from_left(lnode, rnode, idx);
669 return true;
670 }
[a35b458]671
[e3ee9b9]672 return false;
673}
674
675/** Rotate in a key from the right sibling or from the index node, if this operation can be done.
676 *
677 * @param lnode Node into which to add key from its right sibling
678 * or from the index node.
679 *
680 * @return True if the rotation was performed, false otherwise.
681 *
682 */
[7a0359b]683NO_TRACE static bool try_rotation_from_right(btree_node_t *lnode)
[e3ee9b9]684{
685 size_t idx;
686 btree_node_t *rnode;
[a35b458]687
[e3ee9b9]688 /*
689 * If this is root node, the rotation can not be done.
690 */
691 if (ROOT_NODE(lnode))
692 return false;
[a35b458]693
[e3ee9b9]694 idx = find_key_by_subtree(lnode->parent, lnode, false);
695 if (idx == lnode->parent->keys) {
696 /*
697 * If this node is the rightmost subtree of its parent,
698 * the rotation can not be done.
699 */
700 return false;
701 }
[a35b458]702
[e3ee9b9]703 rnode = lnode->parent->subtree[idx + 1];
704 if (rnode->keys > FILL_FACTOR) {
705 rotate_from_right(lnode, rnode, idx);
706 return true;
707 }
[a35b458]708
[e3ee9b9]709 return false;
[018d957e]710}
711
[0cb56f5d]712/** Combine node with any of its siblings.
713 *
714 * The siblings are required to be below the fill factor.
715 *
716 * @param node Node to combine with one of its siblings.
717 *
718 * @return Pointer to the rightmost of the two nodes.
[e3ee9b9]719 *
[0cb56f5d]720 */
[7a0359b]721NO_TRACE static btree_node_t *node_combine(btree_node_t *node)
[0cb56f5d]722{
[98000fb]723 size_t idx;
[0cb56f5d]724 btree_node_t *rnode;
[98000fb]725 size_t i;
[a35b458]726
[63e27ef]727 assert(!ROOT_NODE(node));
[a35b458]728
[0cb56f5d]729 idx = find_key_by_subtree(node->parent, node, false);
730 if (idx == node->parent->keys) {
731 /*
732 * Rightmost subtree of its parent, combine with the left sibling.
733 */
734 idx--;
735 rnode = node;
736 node = node->parent->subtree[idx];
[e3ee9b9]737 } else
[0cb56f5d]738 rnode = node->parent->subtree[idx + 1];
[a35b458]739
[0cb56f5d]740 /* Index nodes need to insert parent node key in between left and right node. */
741 if (INDEX_NODE(node))
742 node->key[node->keys++] = node->parent->key[idx];
[a35b458]743
[0cb56f5d]744 /* Copy the key-value-subtree triplets from the right node. */
745 for (i = 0; i < rnode->keys; i++) {
746 node->key[node->keys + i] = rnode->key[i];
747 node->value[node->keys + i] = rnode->value[i];
[a35b458]748
[0cb56f5d]749 if (INDEX_NODE(node)) {
750 node->subtree[node->keys + i] = rnode->subtree[i];
751 rnode->subtree[i]->parent = node;
752 }
753 }
[a35b458]754
[0cb56f5d]755 if (INDEX_NODE(node)) {
756 node->subtree[node->keys + i] = rnode->subtree[i];
757 rnode->subtree[i]->parent = node;
758 }
[a35b458]759
[0cb56f5d]760 node->keys += rnode->keys;
761 return rnode;
762}
763
[e3ee9b9]764/** Recursively remove B-tree node.
[0cb56f5d]765 *
[e3ee9b9]766 * @param t B-tree.
767 * @param key Key to be removed from the B-tree along with its associated value.
768 * @param node Node where the key being removed resides.
[0cb56f5d]769 *
770 */
[7a0359b]771NO_TRACE static void _btree_remove(btree_t *t, btree_key_t key,
772 btree_node_t *node)
[0cb56f5d]773{
[e3ee9b9]774 if (ROOT_NODE(node)) {
775 if ((node->keys == 1) && (node->subtree[0])) {
776 /*
777 * Free the current root and set new root.
778 */
779 t->root = node->subtree[0];
780 t->root->parent = NULL;
[82d515e9]781 slab_free(btree_node_cache, node);
[e3ee9b9]782 } else {
783 /*
784 * Remove the key from the root node.
785 * Note that the right subtree is removed because when
786 * combining two nodes, the left-side sibling is preserved
787 * and the right-side sibling is freed.
788 */
789 node_remove_key_and_rsubtree(node, key);
790 }
[a35b458]791
[e3ee9b9]792 return;
[0cb56f5d]793 }
[a35b458]794
[e3ee9b9]795 if (node->keys <= FILL_FACTOR) {
796 /*
797 * If the node is below the fill factor,
798 * try to borrow keys from left or right sibling.
799 */
800 if (!try_rotation_from_left(node))
801 try_rotation_from_right(node);
802 }
[a35b458]803
[e3ee9b9]804 if (node->keys > FILL_FACTOR) {
805 size_t i;
[a35b458]806
[e3ee9b9]807 /*
[1ab4aca]808 * The key can be immediately removed.
[e3ee9b9]809 *
810 * Note that the right subtree is removed because when
811 * combining two nodes, the left-side sibling is preserved
812 * and the right-side sibling is freed.
813 */
814 node_remove_key_and_rsubtree(node, key);
[a35b458]815
[e3ee9b9]816 for (i = 0; i < node->parent->keys; i++) {
817 if (node->parent->key[i] == key)
818 node->parent->key[i] = node->key[0];
819 }
[0cb56f5d]820 } else {
[e3ee9b9]821 size_t idx;
822 btree_node_t *rnode;
823 btree_node_t *parent;
[a35b458]824
[e3ee9b9]825 /*
826 * The node is below the fill factor as well as its left and right sibling.
827 * Resort to combining the node with one of its siblings.
828 * The node which is on the left is preserved and the node on the right is
829 * freed.
830 */
831 parent = node->parent;
832 node_remove_key_and_rsubtree(node, key);
833 rnode = node_combine(node);
[a35b458]834
[e3ee9b9]835 if (LEAF_NODE(rnode))
836 list_remove(&rnode->leaf_link);
[a35b458]837
[e3ee9b9]838 idx = find_key_by_subtree(parent, rnode, true);
[63e27ef]839 assert((int) idx != -1);
[82d515e9]840 slab_free(btree_node_cache, rnode);
[e3ee9b9]841 _btree_remove(t, parent->key[idx], parent);
[0cb56f5d]842 }
843}
844
[e3ee9b9]845/** Remove B-tree node.
[cc27ae48]846 *
[e3ee9b9]847 * @param t B-tree.
848 * @param key Key to be removed from the B-tree along
849 * with its associated value.
850 * @param leaf_node If not NULL, pointer to the leaf node where
851 * the key is found.
[cc27ae48]852 *
853 */
[e3ee9b9]854void btree_remove(btree_t *t, btree_key_t key, btree_node_t *leaf_node)
[cc27ae48]855{
856 btree_node_t *lnode;
[a35b458]857
[e3ee9b9]858 lnode = leaf_node;
859 if (!lnode) {
860 if (!btree_search(t, key, &lnode))
861 panic("B-tree %p does not contain key %" PRIu64 ".", t, key);
[cc27ae48]862 }
[a35b458]863
[e3ee9b9]864 _btree_remove(t, key, lnode);
[cc27ae48]865}
866
[e3ee9b9]867/** Search key in a B-tree.
[cc27ae48]868 *
[e3ee9b9]869 * @param t B-tree.
870 * @param key Key to be searched.
871 * @param leaf_node Address where to put pointer to visited leaf node.
[cc27ae48]872 *
[e3ee9b9]873 * @return Pointer to value or NULL if there is no such key.
[cc27ae48]874 *
875 */
[e3ee9b9]876void *btree_search(btree_t *t, btree_key_t key, btree_node_t **leaf_node)
[cc27ae48]877{
[e3ee9b9]878 btree_node_t *cur, *next;
[850fd32]879 bool descend;
[a35b458]880
[cc27ae48]881 /*
[e3ee9b9]882 * Iteratively descend to the leaf that can contain the searched key.
[cc27ae48]883 */
[e3ee9b9]884 for (cur = t->root; cur; cur = next) {
[cc27ae48]885 /*
[e3ee9b9]886 * Last iteration will set this with proper
887 * leaf node address.
[cc27ae48]888 */
[e3ee9b9]889 *leaf_node = cur;
[a35b458]890
[8b3bff5]891 if (cur->keys == 0)
892 return NULL;
893
[cc27ae48]894 /*
[e3ee9b9]895 * The key can be in the leftmost subtree.
896 * Test it separately.
[cc27ae48]897 */
[e3ee9b9]898 if (key < cur->key[0]) {
899 next = cur->subtree[0];
900 continue;
901 } else {
902 void *val;
903 size_t i;
[a35b458]904
[e3ee9b9]905 /*
906 * Now if the key is smaller than cur->key[i]
907 * it can only mean that the value is in cur->subtree[i]
908 * or it is not in the tree at all.
909 */
[850fd32]910 descend = false;
[e3ee9b9]911 for (i = 1; i < cur->keys; i++) {
912 if (key < cur->key[i]) {
913 next = cur->subtree[i];
914 val = cur->value[i - 1];
[a35b458]915
[e3ee9b9]916 if (LEAF_NODE(cur))
917 return key == cur->key[i - 1] ? val : NULL;
[a35b458]918
[850fd32]919 descend = true;
920 break;
[e3ee9b9]921 }
922 }
[a35b458]923
[850fd32]924 if (descend)
925 continue;
926
[e3ee9b9]927 /*
928 * Last possibility is that the key is
929 * in the rightmost subtree.
930 */
931 next = cur->subtree[i];
932 val = cur->value[i - 1];
[a35b458]933
[e3ee9b9]934 if (LEAF_NODE(cur))
935 return key == cur->key[i - 1] ? val : NULL;
936 }
[0cb56f5d]937 }
[a35b458]938
[e3ee9b9]939 /*
940 * The key was not found in the *leaf_node and
941 * is smaller than any of its keys.
942 */
943 return NULL;
[0cb56f5d]944}
[cc27ae48]945
[e3ee9b9]946/** Return pointer to B-tree leaf node's left neighbour.
947 *
948 * @param t B-tree.
949 * @param node Node whose left neighbour will be returned.
[0cb56f5d]950 *
[e3ee9b9]951 * @return Left neighbour of the node or NULL if the node
952 * does not have the left neighbour.
[0cb56f5d]953 *
954 */
[e3ee9b9]955btree_node_t *btree_leaf_node_left_neighbour(btree_t *t, btree_node_t *node)
[0cb56f5d]956{
[63e27ef]957 assert(LEAF_NODE(node));
[a35b458]958
[55b77d9]959 if (node->leaf_link.prev != &t->leaf_list.head)
[e3ee9b9]960 return list_get_instance(node->leaf_link.prev, btree_node_t, leaf_link);
961 else
962 return NULL;
[0cb56f5d]963}
964
[e3ee9b9]965/** Return pointer to B-tree leaf node's right neighbour.
[0cb56f5d]966 *
[e3ee9b9]967 * @param t B-tree.
968 * @param node Node whose right neighbour will be returned.
969 *
970 * @return Right neighbour of the node or NULL if the node
971 * does not have the right neighbour.
[0cb56f5d]972 *
973 */
[e3ee9b9]974btree_node_t *btree_leaf_node_right_neighbour(btree_t *t, btree_node_t *node)
[0cb56f5d]975{
[63e27ef]976 assert(LEAF_NODE(node));
[a35b458]977
[55b77d9]978 if (node->leaf_link.next != &t->leaf_list.head)
[e3ee9b9]979 return list_get_instance(node->leaf_link.next, btree_node_t, leaf_link);
980 else
981 return NULL;
[c715e9b]982}
983
[018d957e]984/** Print B-tree.
985 *
986 * @param t Print out B-tree.
[e3ee9b9]987 *
[018d957e]988 */
989void btree_print(btree_t *t)
990{
[98000fb]991 size_t i;
[7d307e7]992 int depth = t->root->depth;
[55b77d9]993 list_t list;
[a35b458]994
[5b04fc7]995 printf("Printing B-tree:\n");
[55b77d9]996 list_initialize(&list);
997 list_append(&t->root->bfs_link, &list);
[a35b458]998
[018d957e]999 /*
1000 * Use BFS search to print out the tree.
1001 * Levels are distinguished from one another by node->depth.
[e3ee9b9]1002 */
[55b77d9]1003 while (!list_empty(&list)) {
[018d957e]1004 link_t *hlp;
1005 btree_node_t *node;
[a35b458]1006
[55b77d9]1007 hlp = list_first(&list);
[63e27ef]1008 assert(hlp != NULL);
[018d957e]1009 node = list_get_instance(hlp, btree_node_t, bfs_link);
1010 list_remove(hlp);
[a35b458]1011
[63e27ef]1012 assert(node);
[a35b458]1013
[018d957e]1014 if (node->depth != depth) {
1015 printf("\n");
1016 depth = node->depth;
1017 }
[a35b458]1018
[018d957e]1019 printf("(");
[a35b458]1020
[018d957e]1021 for (i = 0; i < node->keys; i++) {
[93a3348]1022 printf("%" PRIu64 "%s", node->key[i], i < node->keys - 1 ? "," : "");
[018d957e]1023 if (node->depth && node->subtree[i]) {
[55b77d9]1024 list_append(&node->subtree[i]->bfs_link, &list);
[018d957e]1025 }
1026 }
[a35b458]1027
[e3ee9b9]1028 if (node->depth && node->subtree[i])
[55b77d9]1029 list_append(&node->subtree[i]->bfs_link, &list);
[a35b458]1030
[018d957e]1031 printf(")");
1032 }
[a35b458]1033
[018d957e]1034 printf("\n");
[a35b458]1035
[5b04fc7]1036 printf("Printing list of leaves:\n");
[feeac0d]1037 list_foreach(t->leaf_list, leaf_link, btree_node_t, node) {
[63e27ef]1038 assert(node);
[a35b458]1039
[5b04fc7]1040 printf("(");
[a35b458]1041
[5b04fc7]1042 for (i = 0; i < node->keys; i++)
[93a3348]1043 printf("%" PRIu64 "%s", node->key[i], i < node->keys - 1 ? "," : "");
[a35b458]1044
[5b04fc7]1045 printf(")");
1046 }
[a35b458]1047
[5b04fc7]1048 printf("\n");
[018d957e]1049}
[b45c443]1050
[c1b8ad4]1051/** Return number of B-tree elements.
1052 *
[e98f1c3e]1053 * @param t B-tree to count.
1054 *
[c1b8ad4]1055 * @return Return number of B-tree elements.
1056 *
1057 */
1058unsigned long btree_count(btree_t *t)
1059{
1060 unsigned long count = 0;
1061
1062 list_foreach(t->leaf_list, leaf_link, btree_node_t, node) {
1063 count += node->keys;
1064 }
1065
1066 return count;
1067}
1068
[cc73a8a1]1069/** @}
[b45c443]1070 */
Note: See TracBrowser for help on using the repository browser.