wHBPTree.hpp 49.5 KB
Newer Older
1
/*
2
 * Copyright (C) 2017-2020 DBIS Group - TU Ilmenau, All Rights Reserved.
3
 *
4
 * This file is part of our NVM-based Data Structures repository.
5
 *
6
7
8
 * This program is free software: you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
9
 *
10
11
12
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
13
 *
14
15
 * You should have received a copy of the GNU General Public License along with this program.
 * If not, see <http://www.gnu.org/licenses/>.
16
17
18
19
20
 */

#ifndef DBIS_wBPTree_hpp_
#define DBIS_wBPTree_hpp_

21
22
#include <libpmemobj/ctl.h>

23
#include <array>
24
#include <cmath>
25
26
27
28
29
30
31
32
#include <iostream>
#include <libpmemobj++/make_persistent.hpp>
#include <libpmemobj++/p.hpp>
#include <libpmemobj++/persistent_ptr.hpp>
#include <libpmemobj++/transaction.hpp>
#include <libpmemobj++/utils.hpp>

#include "config.h"
33
#include "utils/Bitmap.hpp"
34
35
#include "utils/PersistEmulation.hpp"
#include "utils/SearchFunctions.hpp"
36

37
namespace dbis::pbptrees {
38

39
using pmem::obj::allocation_flag;
40
41
42
43
44
using pmem::obj::delete_persistent;
using pmem::obj::make_persistent;
using pmem::obj::p;
using pmem::obj::persistent_ptr;
using pmem::obj::transaction;
45
46
template<typename Object>
using pptr = persistent_ptr<Object>;
47
48
49
50
51
52
53
54
55
56
57

/**
 * A persistent memory implementation of a FPTree.
 *
 * @tparam KeyType the data type of the key
 * @tparam ValueType the data type of the values associated with the key
 * @tparam N the maximum number of keys on a branch node
 * @tparam M the maximum number of keys on a leaf node
 */
template<typename KeyType, typename ValueType, int N, int M>
class wHBPTree {
58
  /// we need at least two keys on a branch node to be able to split
59
  static_assert(N > 2, "number of branch keys has to be >2.");
60
61
62
  /// we need an even order for branch nodes to be able to merge
  static_assert(N % 2 == 0, "order of branch nodes must be even.");
  /// we need at least one key on a leaf node
63
64
65
66
67
68
69
70
  static_assert(M > 0, "number of leaf keys should be >0.");

#ifndef UNIT_TESTS
  private:
#else
  public:
#endif

71
  /// Forward declarations
72
73
74
75
76
77
  struct LeafNode;
  struct BranchNode;

  struct Node {
    Node() : tag(BLANK) {};

78
79
    explicit Node(pptr<LeafNode> leaf_) : tag(LEAF), leaf(leaf_) {};
    explicit Node(BranchNode branch_) : tag(BRANCH), branch(branch_) {};
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    Node(const Node &other) { copy(other); };

    void copy(const Node &other) throw() {
      tag = other.tag;

      switch (tag) {
        case LEAF: {
          leaf = other.leaf;
          break;
        }
        case BRANCH: {
          branch = other.branch;
          break;
        }
        default: break;
      }
    }

    Node &operator=(const Node &other) {
      copy(other);
      return *this;
    }
Philipp Götze's avatar
Philipp Götze committed
102

103
104
105
106
107
108
    Node &operator=(BranchNode *branch_) {
      tag = BRANCH;
      branch = branch_;
      return *this;
    }

109
    Node &operator=(const pptr<LeafNode> &leaf_) {
110
111
112
113
114
115
116
117
118
      tag = LEAF;
      leaf = leaf_;
      return *this;
    }

    enum NodeType {
      BLANK, LEAF, BRANCH
    } tag;
    union {
119
      pptr<LeafNode> leaf;
120
121
122
123
124
125
126
      BranchNode *branch;
    };
  };

  /**
   * A structure for representing a leaf node of a B+ tree.
   */
127
  struct alignas(64) LeafNode {
128
129
130
    /**
     * Constructor for creating a new empty leaf node.
     */
131
132
133
134
135
136
137
138
    LeafNode() : nextLeaf(nullptr), prevLeaf(nullptr) { slot.get_rw()[0] = 0; }

    static constexpr auto SlotSize = ((M + 1 + 7) / 8) * 8;  ///< round to 8 Byte
    static constexpr auto BitsetSize = ((M + 63) / 64) * 8;  ///< number * size of words
    static constexpr auto SearchSize = SlotSize + BitsetSize + 32;
    static constexpr auto PaddingSize = (64 - SearchSize % 64) % 64;

    p<std::array<uint8_t, M + 1>> slot;  ///< slot array for indirection, first = num
139
    p<dbis::Bitmap<M>> bits;             ///< bitmap for valid entries
140
141
142
143
144
    pptr<LeafNode> nextLeaf;             ///< pointer to the subsequent sibling
    pptr<LeafNode> prevLeaf;             ///< pointer to the preceeding sibling
    char padding[PaddingSize];           ///< padding to align keys to 64 bytes
    p<std::array<KeyType, M>> keys;      ///< the actual keys
    p<std::array<ValueType, M>> values;  ///< the actual values
145
146
147
148
149
  };

  /**
   * A structure for representing an branch node (branch node) of a B+ tree.
   */
150
  struct alignas(64) BranchNode {
151
152
153
    /**
     * Constructor for creating a new empty branch node.
     */
154
155
156
157
158
159
160
161
    BranchNode() { slot[0] = 0; }

    static constexpr auto SlotSize = ((N + 1 + 7) / 8) * 8;  ///< round to 8 Byte
    static constexpr auto BitsetSize = ((N + 63) / 64) * 8;  ///< number * size of words
    static constexpr auto SearchSize = SlotSize + BitsetSize;
    static constexpr auto PaddingSize = (64 - SearchSize % 64) % 64;

    std::array<uint8_t, N + 1> slot;   ///< slot array for indirection, first = num
162
    dbis::Bitmap<N> bits;              ///< bitmap for valid entries
163
164
165
    char padding[PaddingSize];         ///< padding to align keys to 64 bytes
    std::array<KeyType, N> keys;       ///< the actual keys
    std::array<Node, N + 1> children;  ///< pointers to child nodes (BranchNode or LeafNode)
166
167
168
169
170
  };

  /**
   * Create a new empty leaf node
   */
171
  pptr<LeafNode> newLeafNode() {
172
    auto pop = pmem::obj::pool_by_vptr(this);
173
    pptr<LeafNode> newNode = nullptr;
174
175
176
    transaction::run(pop, [&] {
      newNode = make_persistent<LeafNode>(allocation_flag::class_id(alloc_class.class_id));
    });
177
178
179
    return newNode;
  }

180
  pptr<LeafNode> newLeafNode(const pptr<LeafNode> &other) {
181
    auto pop = pmem::obj::pool_by_vptr(this);
182
    pptr<LeafNode> newNode = nullptr;
183
184
185
    transaction::run(pop, [&] {
      newNode = make_persistent<LeafNode>(allocation_flag::class_id(alloc_class.class_id), *other);
    });
186
187
188
    return newNode;
  }

189
  void deleteLeafNode(pptr<LeafNode> &node) {
190
    auto pop = pmem::obj::pool_by_vptr(this);
191
    transaction::run(pop, [&] { delete_persistent<LeafNode>(node); });
192
    node = nullptr;
193
194
195
196
197
198
199
200
201
  }

  /**
   * Create a new empty branch node
   */
  BranchNode *newBranchNode() {
    return new BranchNode();
  }

202
203
204
205
206
  BranchNode *newBranchNode(const BranchNode *other) {
    return new BranchNode(*other);
  }

  void deleteBranchNode(BranchNode *&node) {
207
    delete node;
208
    node = nullptr;
209
210
211
212
213
214
215
  }

  /**
   * A structure for passing information about a node split to
   * the caller.
   */
  struct SplitInfo {
216
217
218
    KeyType     key; ///< the key at which the node was split
    Node  leftChild; ///< the resulting lhs child node
    Node rightChild; ///< the resulting rhs child node
219
220
  };

221
222
  static constexpr pobj_alloc_class_desc AllocClass{256, 64, 1, POBJ_HEADER_COMPACT};
  pobj_alloc_class_desc alloc_class;
223
224
  unsigned int      depth; ///< the depth of the tree, i.e. the number of levels (0 => rootNode is LeafNode)
  Node           rootNode; ///< pointer to the root node
225
  pptr<LeafNode> leafList; ///< pointer to the most left leaf node. Necessary for recovery
226
227
228
229
230
231
232
233
234
235

  public:
  /**
   * Typedef for a function passed to the scan method.
   */
  using ScanFunc = std::function<void(const KeyType &key, const ValueType &val)>;
  /**
   * Iterator for iterating over the leaf nodes
   */
  class iterator {
236
    pptr<LeafNode> currentNode;
237
238
239
240
241
    std::size_t currentPosition;

    public:
    iterator() : currentNode(nullptr), currentPosition(0) {}
    iterator(const Node &root, std::size_t d) {
242
      /// traverse to left-most key
243
244
245
246
247
248
249
      auto node = root;
      while (d-- > 0) {
        auto n = node.branch;
        node = n->children[0];
      }
      currentNode = node.leaf;
      currentPosition = 0;
250
      const auto &nodeBits = currentNode->bits.get_ro();
251
      /// Can not overflow as there are at least M/2 entries
252
      while(!nodeBits.test(currentPosition)) ++currentPosition;
253
254
255
256
257
258
259
    }

    iterator& operator++() {
      if (currentPosition >= M-1) {
        currentNode = currentNode->nextLeaf;
        currentPosition = 0;
        if (currentNode == nullptr) return *this;
260
        const auto &nodeBits = currentNode->bits.get_ro();
261
        while(!nodeBits.test(currentPosition)) ++currentPosition;
262
      } else if (!currentNode->bits.get_ro().test(++currentPosition)) ++(*this);
263
264
265
266
      return *this;
    }
    iterator operator++(int) {iterator retval = *this; ++(*this); return retval;}

267
268
269
270
    bool operator==(iterator other) const {
      return (currentNode == other.currentNode &&
              currentPosition == other.currentPosition);
    }
271

272
    bool operator!=(iterator other) const { return !(*this == other); }
273

274
    std::pair<KeyType, ValueType> operator*() {
275
276
277
      const auto &nodeRef = *currentNode;
      return std::make_pair(nodeRef.keys.get_ro()[currentPosition],
                            nodeRef.values.get_ro()[currentPosition]);
278
279
    }

280
    /// iterator traits
281
282
283
284
285
286
287
288
289
290
291
    using difference_type = long;
    using value_type = std::pair<KeyType, ValueType>;
    using pointer = const std::pair<KeyType, ValueType>*;
    using reference = const std::pair<KeyType, ValueType>&;
    using iterator_category = std::forward_iterator_tag;
  };
  iterator begin() { return iterator(rootNode, depth); }
  iterator end() { return iterator(); }
  /**
   * Constructor for creating a new  tree.
   */
292
293
  explicit wHBPTree(struct pobj_alloc_class_desc _alloc) : depth(0), alloc_class(_alloc) {
    // wHBPTree() : depth(0){
294
295
    rootNode = newLeafNode();
    leafList = rootNode.leaf;
296
297
    LOG("created new wHBPTree with sizeof(BranchNode) = "
        << sizeof(BranchNode) << ", sizeof(LeafNode) = " << sizeof(LeafNode));
298
299
300
301
302
303
  }

  /**
   * Destructor for the tree. Should delete all allocated nodes.
   */
  ~wHBPTree() {
304
    /// Nodes are deleted automatically by releasing leafPool and branchPool.
305
306
307
308
309
310
311
312
313
314
  }

  /**
   * Insert an element (a key-value pair) into the tree. If the key @c key
   * already exists, the corresponding value is replaced by @c val.
   *
   * @param key the key of the element to be inserted
   * @param val the value that is associated with the key
   */
  void insert(const KeyType &key, const ValueType &val) {
315
    SplitInfo splitInfo;
316

317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
    bool wasSplit = false;
    if (depth == 0) {
      /// the root node is a leaf node
      auto n = rootNode.leaf;
      wasSplit = insertInLeafNode(n, key, val, &splitInfo);
    } else {
      /// the root node is a branch node
      auto n = rootNode.branch;
      wasSplit = insertInBranchNode(n, depth, key, val, &splitInfo);
    }
    if (wasSplit) {
      /* we had an overflow in the node and therefore the node is split */
      const auto root = newBranchNode();
      auto &rootRef = *root;
      rootRef.keys[0] = splitInfo.key;
      rootRef.children[0] = splitInfo.leftChild;
      rootRef.children[N] = splitInfo.rightChild;
334
335
336
      rootRef.slot[1] = 0;
      rootRef.bits.set(0);
      rootRef.slot[0] = 1;
337
338
339
      rootNode.branch = root;
      ++depth;
    }
340
341
342
343
344
345
346
347
348
349
350
351
352
  }

  /**
   * Find the given @c key in the  tree and if found return the
   * corresponding value.
   *
   * @param key the key we are looking for
   * @param[out] val a pointer to memory where the value is stored
   *                 if the key was found
   * @return true if the key was found, false otherwise
   */
  bool lookup(const KeyType &key, ValueType *val)  {
    assert(val != nullptr);
353
354
355
    const auto leaf = findLeafNode(key);
    const auto pos = lookupPositionInLeafNode(leaf, key);
    const auto &leafRef = *leaf;
356
357
    const auto &leafSlots = leafRef.slot.get_ro();
    if (pos <= leafSlots[0] && leafRef.keys.get_ro()[leafSlots[pos]] == key) {
358
      /// we found it!
359
      *val = leafRef.values.get_ro()[leafSlots[pos]];
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
      return true;
    }
    return false;
  }

  /**
   * Delete the entry with the given key @c key from the tree.
   *
   * @param key the key of the entry to be deleted
   * @return true if the key was found and deleted
   */
  bool erase(const KeyType &key) {
    bool deleted = false;
      if (depth == 0) {
        /* special case: the root node is a leaf node and there is no need to
         * handle underflow */
        auto node = rootNode.leaf;
        assert(node != nullptr);
        deleted = eraseFromLeafNode(node, key);
      } else {
        auto node = rootNode.branch;
        assert(node != nullptr);
        deleted = eraseFromBranchNode(node, depth, key);
      }
    return deleted;
  }
386
387
388
389
390
 /**
  * Recover the wBPTree by iterating over the LeafList and using the recoveryInsert method.
  */
  void recover() {
    LOG("Starting RECOVERY of wHBPTree");
391
    pptr<LeafNode> currentLeaf = leafList;
392
393
394
395
396
397
398
399
400
401
    if (leafList == nullptr) {
      LOG("No data to recover wHBPTree");
      return;
    }
    /* counting leafs */
    auto leafs = 0u;
    while(currentLeaf != nullptr) {
      ++leafs;
      currentLeaf = currentLeaf->nextLeaf;
    }
402
    float x = std::log(leafs)/std::log1p(N);
403
404
405
406
407
    assert(x == int(x) && "Not supported for this amount of leafs, yet");

    /* actual recovery */
    currentLeaf = leafList;
    if (leafList->nextLeaf == nullptr) {
408
      /// The index has only one node, so the leaf node becomes the root node
409
410
411
412
413
414
415
416
417
418
419
420
421
422
      rootNode = leafList;
      depth = 0;
    } else {
      rootNode = newBranchNode();
      depth = 1;
      rootNode.branch->children[0] = currentLeaf;
      currentLeaf = currentLeaf->nextLeaf;
      while (currentLeaf != nullptr) {
        recoveryInsert(currentLeaf);
        currentLeaf = currentLeaf->nextLeaf;
      }
    }
    LOG("RECOVERY Done")
  }
423
424
425
426
427

  /**
   * Print the structure and content of the tree to stdout.
   */
  void print() const {
428
429
    if (depth == 0) printLeafNode(0, rootNode.leaf);
    else printBranchNode(0u, rootNode.branch);
430
431
432
433
434
435
436
437
438
  }

  /**
   * Perform a scan over all key-value pairs stored in the tree.
   * For each entry the given function @func is called.
   *
   * @param func the function called for each entry
   */
  void scan(ScanFunc func) const {
439
    /// we traverse to the leftmost leaf node
440
441
    auto node = rootNode;
    auto d = depth;
442
    while (d-- > 0) node = node.branch->children[0];
443
444
    auto leaf = node.leaf;
    while (leaf != nullptr) {
445
446
      const auto &leafRef = *leaf;
      /// for each key-value pair call func
447
      const auto &bits = leafRef.bits.get_ro();
448
449
450
      const auto &keys = leafRef.keys.get_ro();
      const auto &vals = leafRef.values.get_ro();
      for (auto i = 0u; i < M; ++i) {
451
        if (!bits.test(i)) continue;
452
453
        const auto &key = keys[i];
        const auto &val = vals[i];
454
455
        func(key, val);
      }
456
457
      /// move to the next leaf node
      leaf = leafRef.nextLeaf;
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
    }
  }

  /**
   * Perform a range scan over all elements within the range [minKey, maxKey]
   * and for each element call the given function @c func.
   *
   * @param minKey the lower boundary of the range
   * @param maxKey the upper boundary of the range
   * @param func the function called for each entry
   */
  void scan(const KeyType &minKey, const KeyType &maxKey, ScanFunc func) const {
    auto leaf = findLeafNode(minKey);

    bool higherThanMax = false;
    while (!higherThanMax && leaf != nullptr) {
474
475
      const auto &leafRef = *leaf;
      /// for each key-value pair within the range call func
476
      const auto &bits = leafRef.bits.get_ro();
477
478
479
      const auto &keys = leafRef.keys.get_ro();
      const auto &vals = leafRef.values.get_ro();
      for (auto i = 0u; i < M; ++i) {
480
        if (!bits.test(i)) continue;
481
        const auto &key = keys[i];
482
483
        if (key < minKey) continue;
        if (key > maxKey) { higherThanMax = true; continue; }
484
        const auto &val = vals[i];
485
486
        func(key, val);
      }
487
488
      /// move to the next leaf node
      leaf = leafRef.nextLeaf;
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
    }
  }

#ifndef UNIT_TESTS
  private:
#endif

  /**
   * Insert a (key, value) pair into the corresponding leaf node. It is the
   * responsibility of the caller to make sure that the node @c node is
   * the correct node. The key is inserted at the correct position.
   *
   * @param node the node where the key-value pair is inserted.
   * @param key the key to be inserted
   * @param val the value associated with the key
   * @param splitInfo information about a possible split of the node
   */
506
507
508
  bool insertInLeafNode(const pptr<LeafNode> &node, const KeyType &key, const ValueType &val,
                        SplitInfo *splitInfo) {
    auto &nodeRef = *node;
509
510
    bool split = false;
    const auto slotPos = lookupPositionInLeafNode(node, key);
511
    const auto slotArray = nodeRef.slot.get_ro();
512
513
514
    if (slotPos <= slotArray[0] && nodeRef.keys.get_ro()[slotArray[slotPos]] == key) {
      /// handle insert of duplicates
      nodeRef.values.get_rw()[slotArray[slotPos]] = val;
515
516
517
518
      return false;
    }

    if (slotArray[0] == M) {
519
      /// the node is full, so we must split it
520
521
      splitLeafNode(node, splitInfo);
      auto &splitRef = *splitInfo;
522

523
      /// insert the new entry
524
      if (slotPos < (M + 1) / 2)
525
        insertInLeafNodeAtPosition(splitRef.leftChild.leaf, slotPos, key, val);
526
      else
527
528
        insertInLeafNodeAtPosition(splitRef.rightChild.leaf,
            lookupPositionInLeafNode(splitRef.rightChild.leaf, key), key, val);
529

530
531
      /// inform the caller about the split
      splitRef.key =
532
        splitRef.rightChild.leaf->keys.get_ro()[splitRef.rightChild.leaf->slot.get_ro()[1]];
533
534
      split = true;
    } else {
535
      /// otherwise, we can simply insert the new entry at the given position
536
537
538
539
540
      insertInLeafNodeAtPosition(node, slotPos, key, val);
    }
    return split;
  }

541
542
543
544
545
546
547
548
549
550
551
552
553
  /**
   * Split the given leaf node @c node in the middle and move half of the
   * sibling node.
   *
   * @param node the leaf node to be split
   * @param splitInfo[out] information about the split
   */
  void splitLeafNode(const pptr<LeafNode> &node, SplitInfo *splitInfo) {
    auto &nodeRef = *node;
    /// determine the split position by finding median in unsorted array of keys
    constexpr auto middle = (M + 1) / 2;

    /// move all entries behind this position to a new sibling node
554
    // /*
555
556
    const auto sibling = newLeafNode();
    auto &sibRef = *sibling;
557
558
    auto &sibSlots = sibRef.slot.get_rw();
    auto &sibBits = sibRef.bits.get_rw();
559
560
    auto &sibKeys = sibRef.keys.get_rw();
    auto &sibVals = sibRef.values.get_rw();
561
562
    auto &nodeSlots = nodeRef.slot.get_rw();
    auto &nodeBits = nodeRef.bits.get_rw();
563
564
    const auto &nodeKeys = nodeRef.keys.get_ro();
    const auto &nodeVals = nodeRef.values.get_ro();
565
566
567
568
569
570
571
    sibSlots[0] = nodeSlots[0] - middle;
    nodeSlots[0] = middle;
    for (auto i = 1u; i < sibSlots[0] + 1; ++i) {
      sibSlots[i] = i - 1;
      sibBits.set(sibSlots[i]);
      sibKeys[sibSlots[i]] = nodeKeys[nodeSlots[i + middle]];
      sibVals[sibSlots[i]] = nodeVals[nodeSlots[i + middle]];
572
    }
573
574
575
576
577
    for (auto i = middle; i < M; ++i) nodeBits.reset(nodeSlots[i + 1]);
    PersistEmulation::writeBytes(((M - middle + sibSlots[0] + 7) >> 3) +
                                 (sizeof(KeyType) + sizeof(ValueType) + 1) * sibSlots[0] +
                                 2);  // bits ceiled + entries/slots + numKeys
    // */
578
579
580
581
582

    /// Alternative: copy node, inverse bitmap and shift slots
    /*
    const auto sibling = newLeafNode(node);
    auto &sibRef = *sibling;
583
584
585
586
587
588
589
590
591
592
593
594
    auto &sibSlots = sibRef.slot.get_rw();
    auto &sibBits = sibRef.bits.get_rw();
    auto &nodeSlots = nodeRef.slot.get_rw();
    auto &nodeBits = nodeRef.bits.get_rw();
    sibSlots[0] = sibSlots[0] - middle;
    nodeSlots[0] = middle;
    for (auto i = middle; i < M; ++i) nodeBits.reset(sibSlots[i + 1]);
    sibBits = nodeBits;
    sibBits.flip();
    for (auto i = 1u; i < sibSlots[0] + 1; ++i) sibSlots[i] = sibSlots[middle + i];
    PersistEmulation::writeBytes(sizeof(LeafNode) + ((M - middle + M + 7) >> 3) + sibSlots[0] +
                                 2);  // copy leaf + bits ceiled + slots + numKeys
595
596
    */

597
    /// setup the list of leaf nodes
598
599
600
601
602
603
604
605
606
607
608
609
    if (nodeRef.nextLeaf != nullptr) {
      sibRef.nextLeaf = nodeRef.nextLeaf;
      nodeRef.nextLeaf->prevLeaf = sibling;
      PersistEmulation::writeBytes<16*2>();
    }
    nodeRef.nextLeaf = sibling;
    sibRef.prevLeaf = node;
    PersistEmulation::writeBytes<16*2>();

    auto &splitRef = *splitInfo;
    splitRef.leftChild = node;
    splitRef.rightChild = sibling;
610
    splitRef.key = sibRef.keys.get_ro()[sibSlots[1]];
611
612
  }

613
  /**
614
615
   * Insert a (key, value) pair at the given position @c pos into the leaf node @c node.
   * The caller has to ensure that
616
   * - there is enough space to insert the element
617
   * - the key is inserted at the correct position according to the order of keys
618
   *
619
   * @param node the leaf node where the element is to be inserted
620
621
622
623
   * @param pos the position in the leaf node (0 <= pos <= numKeys < M)
   * @param key the key of the element
   * @param val the actual value corresponding to the key
   */
624
625
  void insertInLeafNodeAtPosition(const pptr<LeafNode> &node, unsigned int pos, const KeyType &key,
                                  const ValueType &val) {
626
    assert(pos <= M);
627
    auto &nodeRef = *node;
628
629
    auto &slots = nodeRef.slot.get_rw();
    auto &bits = nodeRef.bits.get_rw();
630
    const auto u = bits.getFreeZero();  ///< unused Entry
631
632

    /* insert the new entry at unused position */
633
634
    nodeRef.keys.get_rw()[u] = key;
    nodeRef.values.get_rw()[u] = val;
635
636

    /* adapt slot array */
637
638
639
640
641
    for (auto j = slots[0]; j >= pos; --j) slots[j + 1] = slots[j];
    PersistEmulation::writeBytes(slots[0] - pos + 1);
    slots[pos] = u;
    ++slots[0];
    bits.set(u);
642
    PersistEmulation::writeBytes<3>();
643
644
645
646
647
648
649
650
651
652
653
654
655
  }

  /**
   * Insert a (key, value) pair into the tree recursively by following the path
   * down to the leaf level starting at node @c node at depth @c depth.
   *
   * @param node the starting node for the insert
   * @param depth the current depth of the tree (0 == leaf level)
   * @param key the key of the element
   * @param val the actual value corresponding to the key
   * @param splitInfo information about the split
   * @return true if a split was performed
   */
656
657
  bool insertInBranchNode(BranchNode *node, unsigned int depth, const KeyType &key,
                          const ValueType &val, SplitInfo *splitInfo) {
658
659
    SplitInfo childSplitInfo;
    bool split = false, hasSplit = false;
660
    auto &nodeRef = *node;
661
662
663
664

    auto pos = lookupPositionInBranchNode(node, key);
    if (depth == 1) {
      /* case #1: our children are leaf node */
665
666
      auto child = (pos == nodeRef.slot[0] + 1) ? nodeRef.children[N].leaf
                                : nodeRef.children[nodeRef.slot[pos]].leaf;
667
668
669
      hasSplit = insertInLeafNode(child, key, val, &childSplitInfo);
    } else {
      /* case #2: our children are branch nodes */
670
671
      auto child = (pos == nodeRef.slot[0] + 1) ? nodeRef.children[N].branch
                                : nodeRef.children[nodeRef.slot[pos]].branch;
672
673
674
675
676
      hasSplit = insertInBranchNode(child, depth - 1, key, val, &childSplitInfo);
    }

    if (hasSplit) {
      auto host = node;
677
      /// the child node was split, thus we have to add a new entry to our branch node
678
      if (nodeRef.slot[0] == N) {
679
        /// this node is also full and needs to be split
680
        splitBranchNode(node, childSplitInfo.key, splitInfo);
681
        const auto &splitRef = *splitInfo;
682
        host = (key < splitRef.key ? splitRef.leftChild : splitRef.rightChild).branch;
683
684
685
        split = true;
        pos = lookupPositionInBranchNode(host, key);
      }
686
687
      /// Insert new key and children
      auto &hostRef = *host;
688
      const auto u = hostRef.bits.getFreeZero();
689
690
691
692
      hostRef.keys[u] = childSplitInfo.key;
      hostRef.children[u] = childSplitInfo.leftChild;

      /// adapt slot array
693
      if (pos <= hostRef.slot[0]) {
694
        /// if the child isn't inserted at the rightmost position then we have to make space for it
695
696
697
        for(auto j = hostRef.slot[0]; j >= pos; --j)
          hostRef.slot[j+1] = hostRef.slot[j];
        hostRef.children[hostRef.slot[pos+1]] = childSplitInfo.rightChild;
698
      } else {
699
        hostRef.children[N] = childSplitInfo.rightChild;
700
      }
701
702
703
      hostRef.slot[pos] = u;
      hostRef.slot[0] = hostRef.slot[0] + 1;
      hostRef.bits.set(u);
704
705
706
707
708
709
710
711
712
713
714
715
    }
    return split;
  }

  /**
   * Split the given branch node @c node in the middle and move
   * half of the keys/children to the new sibling node.
   *
   * @param node the branch node to be split
   * @param splitKey the key on which the split of the child occured
   * @param splitInfo information about the split
   */
716
  void splitBranchNode(BranchNode *node, const KeyType &splitKey, SplitInfo *splitInfo) {
717
    /// determine the split position
718
    auto middle = (N + 1) / 2;
719
    auto &nodeRef = *node;
720
    if (splitKey > nodeRef.keys[nodeRef.slot[middle]]) ++middle;
721

722
723
724
    /// move all entries behind this position to a new sibling node
    const auto sibling = newBranchNode();
    auto &sibRef = *sibling;
725
726
727
728
    sibRef.slot[0] = nodeRef.slot[0] - middle;
    for (auto i = 0u; i < sibRef.slot[0]; ++i) {
      sibRef.slot[i + 1] = i;  ///< set slot
      sibRef.bits.set(i);  ///< set bit
729
      /// set key and children
730
731
      sibRef.keys[i] = nodeRef.keys[nodeRef.slot[middle + i + 1]];
      sibRef.children[i] = nodeRef.children[nodeRef.slot[middle + i + 1]];
732
    }
733
734
    for (auto i = middle; i <= N; ++i) nodeRef.bits.reset(nodeRef.slot[i]);
    nodeRef.slot[0] = middle - 1;
735
736
737

    /// set new most right children
    sibRef.children[N] = nodeRef.children[N];
738
    nodeRef.children[N] = nodeRef.children[nodeRef.slot[middle]];
739
740
741

    /// set split information
    auto &splitRef = *splitInfo;
742
    splitRef.key = nodeRef.keys[nodeRef.slot[middle]];
743
744
    splitRef.leftChild = node;
    splitRef.rightChild = sibling;
745
746
747
748
749
750
751
752
753
754
  }

  /**
   * Traverse the tree starting at the root until the leaf node is found that
   * could contain the given @key. Note, that always a leaf node is returned
   * even if the key doesn't exist on this node.
   *
   * @param key the key we are looking for
   * @return the leaf node that would store the key
   */
755
  pptr<LeafNode> findLeafNode(const KeyType &key) const {
756
757
758
    auto node = rootNode;
    auto d = depth;
    while (d-- > 0) {
759
      auto pos = lookupPositionInBranchNode(node.branch, key);
760
      node = node.branch->children[node.branch->slot[pos]];
761
762
763
764
765
766
767
768
769
770
771
    }
    return node.leaf;
  }
  /**
   * Lookup the search key @c key in the given leaf node and return the
   * position.
   *
   * @param node the leaf node where we search
   * @param key the search key
   * @return the position of the key  (or @c M if not found)
   */
772
  auto lookupPositionInLeafNode(const pptr<LeafNode> &node, const KeyType &key) const {
773
774
    const auto &nodeRef = *node;
    const auto &keys = nodeRef.keys.get_ro();
775
    const auto &slots = nodeRef.slot.get_ro();
776
    return binarySearch<false>(keys, slots, 1, slots[0], key);
777
778
779
780
781
782
783
784
785
786
787
788
789
790
  }

  /**
   * Lookup the search key @c key in the given branch node and return the
   * position which is the position in the list of keys + 1. in this way, the
   * position corresponds to the position of the child pointer in the
   * array @children.
   * If the search key is less than the smallest key, then @c 0 is returned.
   * If the key is greater than the largest key, then @c numKeys is returned.
   *
   * @param node the branch node where we search
   * @param key the search key
   * @return the position of the key + 1 (or 0 or @c numKey)
   */
791
  auto lookupPositionInBranchNode(const BranchNode *node, const KeyType &key) const {
792
793
    const auto &nodeRef = *node;
    const auto &keys = nodeRef.keys;
794
    const auto &slots = nodeRef.slot;
795
    return binarySearch<true>(keys, slots, 1, slots[0], key);
796
797
798
799
800
801
802
803
804
  }

  /**
   * Delete the element with the given key from the given leaf node.
   *
   * @param node the leaf node from which the element is deleted
   * @param key the key of the element to be deleted
   * @return true of the element was deleted
   */
805
  bool eraseFromLeafNode(const pptr<LeafNode> &node, const KeyType &key) {
806
    auto pos = lookupPositionInLeafNode(node, key);
807
808
809
810
811
812
813
814
815
816
817
818
819
    return eraseFromLeafNodeAtPosition(node, pos, key);
  }

  /**
   * Delete the element with the given position and key from the given leaf node.
   *
   * @param node the leaf node from which the element is deleted
   * @param pos the position of the key in the node
   * @param key the key of the element to be deleted
   * @return true of the element was deleted
   */
  bool eraseFromLeafNodeAtPosition(const pptr<LeafNode> &node, const unsigned int pos,
                                   const KeyType &key) {
820
    auto &nodeRef = *node;
821
822
823
824
825
826
    auto &nodeSlots = nodeRef.slot.get_rw();
    auto &nodeBits = nodeRef.bits.get_rw();
    if (nodeRef.keys.get_ro()[nodeSlots[pos]] == key) {
      nodeBits.reset(nodeSlots[pos]);
      for (auto i = pos; i < nodeSlots[0] + 1; ++i) {
        nodeSlots[i] = nodeSlots[i + 1];
827
      }
828
829
      --nodeSlots[0];
      PersistEmulation::writeBytes(1 + (nodeSlots[0] - pos) + 1);
830
831
832
833
834
835
836
837
838
839
840
841
842
843
      return true;
    }
    return false;
  }

  /**
   * Delete an entry from the tree by recursively going down to the leaf level
   * and handling the underflows.
   *
   * @param node the current branch node
   * @param d the current depth of the traversal
   * @param key the key to be deleted
   * @return true if the entry was deleted
   */
844
  bool eraseFromBranchNode(BranchNode *const node, const unsigned int d, const KeyType &key) {
845
846
    assert(d >= 1);
    bool deleted = false;
847
    const auto &nodeRef = *node;
848
849
850
851
    /* try to find the branch */
    auto pos = lookupPositionInBranchNode(node, key);
    if (d == 1) {
      /* the next level is the leaf level */
852
853
      auto leaf = (pos == nodeRef.slot[0] + 1) ? nodeRef.children[N].leaf
                                               : nodeRef.children[nodeRef.slot[pos]].leaf;
854
855
      assert(leaf != nullptr);
      deleted = eraseFromLeafNode(leaf, key);
856
      constexpr auto middle = (M + 1) / 2;
857
      /// handle possible underflow
858
      if (leaf->slot.get_ro()[0] < middle) underflowAtLeafLevel(node, pos, leaf);
859
    } else {
860
861
      auto child = (pos == nodeRef.slot[0] + 1) ? nodeRef.children[N].branch
                                                : nodeRef.children[nodeRef.slot[pos]].branch;
862
863
      deleted = eraseFromBranchNode(child, d - 1, key);
      pos = lookupPositionInBranchNode(node, key);
864
      constexpr auto middle = (N + 1) / 2;
865

866
      /// handle possible underflow
867
      if (child->slot[0] < middle) {
868
        child = underflowAtBranchLevel(node, pos, child);
869
        if (d == depth && nodeRef.slot[0] == 0) {
870
          /// special case: the root node is empty now
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
          rootNode = child;
          --depth;
        }
      }
    }
    return deleted;
  }

  /**
   * Handle the case that during a delete operation a underflow at node @c leaf
   * occured. If possible this is handled
   * (1) by rebalancing the elements among the leaf node and one of its siblings
   * (2) if not possible by merging with one of its siblings.
   *
   * @param node the parent node of the node where the underflow occured
   * @param pos the position in the slot array containing the position in turn
   *  of the key from the left child node @leaf in the @c children array of the branch node
   * @param leaf the node at which the underflow occured
   */
890
  void underflowAtLeafLevel(BranchNode *node, unsigned int pos, pptr<LeafNode> &leaf) {
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
    auto &nodeRef = *node;
    auto &leafRef = *leaf;
    assert(pos <= nodeRef.slot[0] + 1);
    constexpr auto middle = (M + 1) / 2;
    /* 1. we check whether we can rebalance with one of the siblings but only
     *    if both nodes have the same direct parent */
    if (pos > 1 && leafRef.prevLeaf->slot.get_ro()[0] > middle) {
      /* we have a sibling at the left for rebalancing the keys */
      balanceLeafNodes(leafRef.prevLeaf, leaf);
      const auto keyPos = (pos == nodeRef.slot[0] + 1)
                              ? nodeRef.slot[nodeRef.slot[0]]
                              : nodeRef.slot[pos];
      nodeRef.keys[keyPos] = leafRef.keys.get_ro()[nodeRef.slot[1]];
    } else if (pos <= nodeRef.slot[0] &&
               leafRef.nextLeaf->slot.get_ro()[0] > middle) {
      /* we have a sibling at the right for rebalancing the keys */
      balanceLeafNodes(leafRef.nextLeaf, leaf);
      nodeRef.keys[nodeRef.slot[pos + 1]] =
          leafRef.nextLeaf->keys.get_ro()[nodeRef.slot[1]];
    }
    /* 2. if this fails we have to merge two leaf nodes but only if both nodes
     *    have the same direct parent */
    else {
      pptr<LeafNode> survivor = nullptr;
      if (pos > 1 && leafRef.prevLeaf->slot.get_ro()[0] <= middle) {
        survivor = mergeLeafNodes(leafRef.prevLeaf, leaf);
        deleteLeafNode(leaf);
        --pos;
      } else if (pos <= nodeRef.slot[0] && leafRef.nextLeaf->slot.get_ro()[0] <= middle) {
        /* because we update the pointers in mergeLeafNodes we keep it here */
        auto l = leafRef.nextLeaf;
        survivor = mergeLeafNodes(leaf, l);
        deleteLeafNode(l);
      } else
        assert(false);  ///< this shouldn't happen?!

      if (nodeRef.slot[0] > 1) {
        /* just remove the child node from the current branch node */
        nodeRef.bits.reset(nodeRef.slot[pos]);
        for (auto i = pos; i < nodeRef.slot[0]; ++i) {
          nodeRef.slot[i] = nodeRef.slot[i + 1];
932
        }
933
934
935
936
937
938
939
940
941
        const auto surPos = (pos == nodeRef.slot[0]) ? N : nodeRef.slot[pos];
        nodeRef.children[surPos] = survivor;
        --nodeRef.slot[0];
      } else {
        /* This is a special case that happens only if the current node is the
         * root node. Now, we have to replace the branch root node by a leaf
         * node. */
        rootNode = survivor;
        --depth;
942
943
      }
    }
944
    }
945
946
947
948
949
950
951
952
953
954
955
956
957
958

  /**
   * Handle the case that during a delete operation a underflow at node @c child
   * occured where @c node is the parent node. If possible this is handled
   * (1) by rebalancing the elements among the node @c child and one of its
   * siblings
   * (2) if not possible by merging with one of its siblings.
   *
   * @param node the parent node of the node where the underflow occured
   * @param pos the position of the child node @child in the @c children array
   * of the branch node
   * @param child the node at which the underflow occured
   * @return the (possibly new) child node (in case of a merge)
   */
959
  BranchNode *underflowAtBranchLevel(BranchNode * const node, unsigned int pos,
960
961
962
                                     BranchNode *child) {
    assert(node != nullptr);
    assert(child != nullptr);
963
    auto &nodeRef = *node;
964
965
    auto prevKeys = 0u, nextKeys = 0u;
    constexpr auto middle = (N + 1) / 2;
966
    /* 1. we check whether we can rebalance with one of the siblings */
967
    if (pos > 1 && (prevKeys =
968
          nodeRef.children[nodeRef.slot[pos-1]].branch->slot[0]) > middle) {
969
      /* we have a sibling at the left for rebalancing the keys */
970
      auto sibling = nodeRef.children[nodeRef.slot[pos-1]].branch;
971
      balanceBranchNodes(sibling, child, node, pos-1);
972
      return child;
973
974
    } else if (pos < nodeRef.slot[0] && (nextKeys =
          nodeRef.children[nodeRef.slot[pos + 1]].branch->slot[0]) > middle) {
975
      /* we have a sibling at the right for rebalancing the keys */
976
      auto sibling = nodeRef.children[nodeRef.slot[pos+1]].branch;
977
      balanceBranchNodes(sibling, child, node, pos);
978
      return child;
979
980
    } else if (pos == nodeRef.slot[0] && (nextKeys =
          nodeRef.children[N].branch->slot[0]) > middle) {
981
      auto sibling = nodeRef.children[N].branch;
982
      balanceBranchNodes(sibling, child, node, pos);
983
      return child;
984
985
986
    }
    /* 2. if this fails we have to merge two branch nodes */
    else {
987
      auto newChild = child;
988
989
      auto ppos = pos;
      if (prevKeys > 0) {
990
991
        auto &lSibling = nodeRef.children[nodeRef.slot[pos - 1]].branch;
        mergeBranchNodes(lSibling, nodeRef.keys[nodeRef.slot[pos-1]], child);
992
        ppos = pos - 1;
993
        deleteBranchNode(child);
994
995
        newChild = lSibling;
      } else if (nextKeys > 0) {
996
        const auto rPos = (pos == nodeRef.slot[0]) ? N : nodeRef.slot[pos + 1];
997
        auto &rSibling = nodeRef.children[rPos].branch;
998
999
        mergeBranchNodes(child, nodeRef.keys[nodeRef.slot[pos]], rSibling);
        if (pos == nodeRef.slot[0])
1000
          nodeRef.children[N] = child; ///< new rightmost children