FPTree.hpp 43.8 KB
Newer Older
1
/*
2
 * Copyright (C) 2017-2019 DBIS Group - TU Ilmenau, All Rights Reserved.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * This file is part of our NVM-based Data Structure Repository.
 *
 * 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.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef DBIS_FPTree_hpp_
#define DBIS_FPTree_hpp_

#include <array>
24
#include <bitset>
25
#include <cmath>
26
27
28
29
30
31
32
33
#include <iostream>

#include <libpmemobj++/make_persistent.hpp>
#include <libpmemobj++/p.hpp>
#include <libpmemobj++/persistent_ptr.hpp>
#include <libpmemobj++/transaction.hpp>
#include <libpmemobj++/utils.hpp>

34
#include "config.h"
35
#include "utils/ElementOfRankK.hpp"
36
#include "utils/PersistEmulation.hpp"
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

namespace dbis::fptree {

using pmem::obj::delete_persistent;
using pmem::obj::make_persistent;
using pmem::obj::p;
using pmem::obj::persistent_ptr;
using pmem::obj::transaction;

/**
 * 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 FPTree {
  // we need at least two keys on a branch node to be able to split
  static_assert(N > 2, "number of branch keys has to be >2.");
  // we need at least one key on a leaf node
  static_assert(M > 0, "number of leaf keys should be >0.");

#ifndef UNIT_TESTS
  private:
#else
  public:
#endif

  // Forward declarations
  struct LeafNode;
  struct BranchNode;

71
72
  struct Node {
    Node() : tag(BLANK) {};
73

74
    Node(persistent_ptr<LeafNode> leaf_) : tag(LEAF), leaf(leaf_) {};
75

76
    Node(BranchNode *branch_) : tag(BRANCH), branch(branch_) {};
77

78
    Node(const Node &other) { copy(other); };
79

80
    void copy(const Node &other) throw() {
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
      tag = other.tag;

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

96
    Node &operator=(Node other) {
97
98
99
100
101
      copy(other);
      return *this;
    }

    enum NodeType {
102
      BLANK, LEAF, BRANCH
103
104
105
106
107
108
109
    } tag;
    union {
      persistent_ptr<LeafNode> leaf;
      BranchNode *branch;
    };
  };

110
111
112
  struct alignas(64) LeafSearch {
    std::bitset<M> b;          //< bitset for valid entries
    std::array<uint8_t, M> fp; //< fingerprint array (n & 0xFF)
113
114
115
116
117
118
119
120

    unsigned int getFreeZero() const {
      unsigned int idx = 0;
      while (idx < M && b.test(idx)) ++idx;
      return idx;
    }
  };

121
122
123
124
125
126
127
  /**
   * A structure for representing a leaf node of a B+ tree.
   */
  struct LeafNode {
    /**
     * Constructor for creating a new empty leaf node.
     */
128
129
    LeafNode() : nextLeaf(nullptr), prevLeaf(nullptr) {}

130
    p<LeafSearch> search;                  //< helper structure for faster searches
131
132
    p<std::array<KeyType, M>> keys;        //< the actual keys
    p<std::array<ValueType, M>> values;    //< the actual values
133
134
    persistent_ptr<LeafNode> nextLeaf;     //< pointer to the subsequent sibling
    persistent_ptr<LeafNode> prevLeaf;     //< pointer to the preceeding sibling
135
136
137
138
139
140
141
142
143
144
145
146
147
  };

  /**
   * A structure for representing an branch node (branch node) of a B+ tree.
   */
  struct BranchNode {
    /**
     * Constructor for creating a new empty branch node.
     */
    BranchNode() : numKeys(0) {}

    unsigned int numKeys;                         //< the number of currently stored keys
    std::array<KeyType, N> keys;                  //< the actual keys
148
    std::array<Node, N + 1> children; //< pointers to child nodes (BranchNode or LeafNode)
149
150
151
152
153
154
155
156
157
  };

  /**
   * Create a new empty leaf node
   */
  persistent_ptr<LeafNode> newLeafNode() {
    auto pop = pmem::obj::pool_by_vptr(this);
    persistent_ptr<LeafNode> newNode = nullptr;
    transaction::run(pop, [&] {
158
159
160
161
162
163
164
165
166
167
168
      newNode = make_persistent<LeafNode>();
    });
    return newNode;
  }

  persistent_ptr<LeafNode> newLeafNode(const persistent_ptr<LeafNode> &other) {
    auto pop = pmem::obj::pool_by_vptr(this);
    persistent_ptr<LeafNode> newNode = nullptr;
    transaction::run(pop, [&] {
      newNode = make_persistent<LeafNode>(*other);
    });
169
170
171
    return newNode;
  }

172

173
174
175
  void deleteLeafNode(persistent_ptr<LeafNode> node) {
    auto pop = pmem::obj::pool_by_vptr(this);
    transaction::run(pop, [&] {
176
177
      delete_persistent<LeafNode>(node);
    });
178
179
180
181
182
183
  }

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

  void deleteBranchNode(BranchNode *node) {
188
    delete node;
189
190
191
192
193
194
195
196
  }

  /**
   * A structure for passing information about a node split to
   * the caller.
   */
  struct SplitInfo {
    KeyType key;                 //< the key at which the node was split
197
198
    Node leftChild;  //< the resulting lhs child node
    Node rightChild; //< the resulting rhs child node
199
200
201
202
  };

  unsigned int depth;         //< the depth of the tree, i.e. the number of levels (0 => rootNode is LeafNode)

203
  Node rootNode;     //< pointer to the root node
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
  persistent_ptr<LeafNode> leafList; //<Pointer to the leaf at the most left position. Neccessary for recovery

  PROFILE_DECL

  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 {
    persistent_ptr<LeafNode> currentNode;
    std::size_t currentPosition;

    public:
    iterator() : currentNode(nullptr), currentPosition(0) {}
222
    iterator(const Node &root, std::size_t d) {
223
224
225
226
227
228
229
230
      // traverse to left-most key
      auto node = root;
      while (d-- > 0) {
        auto n = node.branch;
        node = n->children[0];
      }
      currentNode = node.leaf;
      currentPosition = 0;
231
      // Can not overflow as there are at least M/2 entries
232
      while(!currentNode->search.get_ro().b.test(currentPosition)) ++currentPosition;
233
234
235
    }

    iterator& operator++() {
236
      if (currentPosition >= M-1) {
237
238
        currentNode = currentNode->nextLeaf;
        currentPosition = 0;
239
        if (currentNode == nullptr) return *this;
240
241
        while(!currentNode->search.get_ro().b.test(currentPosition)) ++currentPosition;
      } else if (!currentNode->search.get_ro().b.test(++currentPosition)) ++(*this);
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
      return *this;
    }
    iterator operator++(int) {iterator retval = *this; ++(*this); return retval;}

    bool operator==(iterator other) const {return (currentNode == other.currentNode &&
        currentPosition == other.currentPosition);}
    bool operator!=(iterator other) const {return !(*this == other);}

    std::pair<KeyType, ValueType> operator*() {

      return std::make_pair(currentNode->keys.get_ro()[currentPosition], currentNode->values.get_ro()[currentPosition]);
    }

    // iterator traits
    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.
   */
  FPTree() {
    rootNode = newLeafNode();
269
270
    leafList = rootNode.leaf;
    depth = 0;
271
    PROFILE_INIT
272
273
    LOG("created new FPTree with sizeof(BranchNode) = " << sizeof(BranchNode)
                            <<  ", sizeof(LeafNode) = " << sizeof(LeafNode));
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
  }

  /**
   * Destructor for the tree. Should delete all allocated nodes.
   */
  ~FPTree() {
    // Nodes are deleted automatically by releasing leafPool and branchPool.
  }

  /**
   * 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) {
    auto pop = pmem::obj::pool_by_vptr(this);
    transaction::run(pop, [&] {
293
      SplitInfo splitInfo;
294

295
296
      bool wasSplit = false;
      if (depth == 0) {
297
298
299
        // the root node is a leaf node
        auto n = rootNode.leaf;
        wasSplit = insertInLeafNode(n, key, val, &splitInfo);
300
      } else {
301
302
303
        // the root node is a branch node
        auto n = rootNode.branch;
        wasSplit = insertInBranchNode(n, depth, key, val, &splitInfo);
304
305
      }
      if (wasSplit) {
306
        // we had an overflow in the node and therefore the node is split
307
308
309
310
311
312
313
314
        auto root = newBranchNode();

        root->keys[0] = splitInfo.key;
        root->children[0] = splitInfo.leftChild;
        root->children[1] = splitInfo.rightChild;
        ++root->numKeys;
        rootNode.branch = root;
        ++depth;
315
      }
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
    });
  }

  /**
   * 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);

    bool result = false;
    auto leafNode = findLeafNode(key);
    auto pos = lookupPositionInLeafNode(leafNode, key);
334
    if (pos < M) {
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
      // we found it!
      *val = leafNode->values.get_ro()[pos];
      result = true;
    }
    return result;
  }

  /**
   * 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) {
    auto pop = pmem::obj::pool_by_vptr(this);
    bool result;
    transaction::run(pop, [&] {
352
      if (depth == 0) {
353
354
355
356
357
        // special case: the root node is a leaf node and
        // there is no need to handle underflow
        auto node = rootNode.leaf;
        assert(node != nullptr);
        result=eraseFromLeafNode(node, key);
358
      } else {
359
360
361
362
        auto node = rootNode.branch;
        assert(node != nullptr);
        result=eraseFromBranchNode(node, depth, key);

363
364
      }
    });
365
366
367
368
369
    return result;
  }
  /**
   * Recover the FPTree by iterating over the LeafList and using the recoveryInsert method.
   */
370
  void recover() {
371
    LOG("Starting RECOVERY of FPTree");
372
    persistent_ptr<LeafNode> currentLeaf = leafList;
373
    if (leafList == nullptr) {
374
      LOG("No data to recover FPTree");
375
      return;
376
    }
377
378
379
380
381
382
383
384
385
386
387
388
    /* counting leafs */
    auto leafs = 0u;
    while(currentLeaf != nullptr) {
      ++leafs;
      currentLeaf = currentLeaf->nextLeaf;
    }
    float x = std::log(leafs)/std::log(N+1);
    assert(x == int(x) && "Not supported for this amount of leafs, yet");

    /* actual recovery */
    currentLeaf = leafList;
    if (leafList->nextLeaf == nullptr) {
389
      // The index has only one node, so the leaf node becomes the root node
390
391
      rootNode = leafList;
      depth = 0;
392
    } else {
393
      rootNode = newBranchNode();
394
      depth = 1;
395
396
      rootNode.branch->children[0] = currentLeaf;
      currentLeaf = currentLeaf->nextLeaf;
397
398
399
400
401
      while (currentLeaf != nullptr) {
        recoveryInsert(currentLeaf);
        currentLeaf = currentLeaf->nextLeaf;
      }
    }
402
    LOG("RECOVERY Done")
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
  }

  /**
   * Print the structure and content of the tree to stdout.
   */
  void print() const {
    if (depth == 0) {
      printLeafNode(0, rootNode.leaf);
    } else {
      auto n = rootNode;
      printBranchNode(0u, n.branch);
    }
  }

  PROFILE_PRINT

  /**
   * 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
   */
425
  void scan(ScanFunc func) const {
426
427
428
    // we traverse to the leftmost leaf node
    auto node = rootNode;
    auto d = depth;
429
    while ( d-- > 0) {
430
      // as long as we aren't at the leaf level we follow the path down
431
      node = node.branch->children[0];
432
433
434
435
436
    }
    auto leaf = node.leaf;
    while (leaf != nullptr) {
      // for each key-value pair call func
      for (auto i = 0u; i < leaf->numKeys.get_ro(); i++) {
437
        if (!leaf->search.get_ro().b.test(i)) continue;
438
439
440
441
442
443
444
        const auto &key = leaf->keys.get_ro()[i];
        const auto &val = leaf->values.get_ro()[i];
        func(key, val);
      }
      // move to the next leaf node
      leaf = leaf->nextLeaf;
    }
445
  }
446
447
448
449
450
451
452
453
454

  /**
   * 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
   */
455
  void scan(const KeyType &minKey, const KeyType &maxKey, ScanFunc func) const {
456
457
458
459
460
461
    auto leaf = findLeafNode(minKey);

    bool higherThanMax = false;
    while (!higherThanMax && leaf != nullptr) {
      // for each key-value pair within the range call func
      for (auto i = 0u; i < M; i++) {
462
        if (!leaf->search.get_ro().b.test(i)) continue;
463
464
465
466
467
468
469
470
471
472
        auto &key = leaf->keys.get_ro()[i];
        if (key < minKey) continue;
        if (key > maxKey) { higherThanMax = true; continue; }

        auto &val = leaf->values.get_ro()[i];
        func(key, val);
      }
      // move to the next leaf node
      leaf = leaf->nextLeaf;
    }
473
  }
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490

#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
   */
  bool insertInLeafNode(persistent_ptr<LeafNode> node, const KeyType &key,
      const ValueType &val, SplitInfo *splitInfo) {
491
    auto &nodeRef = *node;
492
493
494
    bool split = false;
    auto pos = lookupPositionInLeafNode(node, key);

495
    if (pos < M) {
496
      // handle insert of duplicates
497
      nodeRef.values.get_rw()[pos] = val;
498
      return false;
499
500
    }
    pos = nodeRef.search.get_ro().getFreeZero();
501
    if (pos == M) {
502
503
504
505
506
507
508
509
510
      /* split the node */
      splitLeafNode(node, splitInfo);
      auto &splitRef = *splitInfo;
      auto sibling= splitRef.rightChild.leaf;
      auto &sibRef= *sibling;

      /* insert the new entry */
      if (key < splitRef.key)
        insertInLeafNodeAtPosition(node, nodeRef.search.get_ro().getFreeZero(), key, val);
511
      else
512
        insertInLeafNodeAtPosition(sibling, sibRef.search.get_ro().getFreeZero(), key, val);
513
514


515
516
      /* inform the caller about the split */
      splitRef.key = sibRef.keys.get_ro()[findMinKeyAtLeafNode(sibling)];
517
518
      split = true;
    } else {
519
      /* otherwise, we can simply insert the new entry at the given position */
520
521
522
523
524
      insertInLeafNodeAtPosition(node, pos, key, val);
    }
    return split;
  }

525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
  void splitLeafNode(persistent_ptr<LeafNode> node, SplitInfo *splitInfo) {
      auto &nodeRef = *node;

      /* determine the split position by finding median in unsorted array of keys*/
      auto data = nodeRef.keys.get_ro();
      auto bAndKey = findSplitKey(data);
      auto &splitKey = bAndKey.second;

      // copy leaf
      persistent_ptr<LeafNode> sibling = newLeafNode(node);
      auto &sibRef = *sibling;
      nodeRef.search.get_rw().b = bAndKey.first;
      sibRef.search.get_rw().b = bAndKey.first.flip();
      //PersistEmulation::writeBytes(sizeof(LeafNode) + ((2*M+7)>>3)); // copy leaf + 2 bitmaps

      /* Alternative: move instead of complete copy *//*
      auto data = nodeRef.keys.get_ro();
      auto splitKey = ElementOfRankK::elementOfRankK((M+1)/2, data, 0, M);
      persistent_ptr<LeafNode> sibling = newLeafNode();
      auto &sibRef = *sibling;
      auto j = 0u;
      for(auto i = 0u; i < M; i++) {
        if(nodeRef.keys.get_ro()[i] > splitKey) {
          sibRef.keys.get_rw()[j] = nodeRef.keys.get_ro()[i];
          sibRef.values.get_rw()[j] = nodeRef.values.get_ro()[i];
          sibRef.search.get_rw().fp[j] = nodeRef.search.get_ro().fp[i];
          sibRef.search.get_rw().b.set(j);
          nodeRef.search.get_rw().b.reset(i);
          j++;
        }
      }
      PersistEmulation::writeBytes(j * (sizeof(KeyType) + sizeof(ValueType) + 1) + ((j*2+7)>>3)); // j entries/hashes + j*2 bits*/

      /* setup the list of leaf nodes */
      if (nodeRef.nextLeaf != nullptr) {
        sibRef.nextLeaf = nodeRef.nextLeaf;
        nodeRef.nextLeaf->prevLeaf = sibling;
      }
      nodeRef.nextLeaf = sibling;
      sibRef.prevLeaf = node;
      //PersistEmulation::writeBytes(16*2);

      /* set split information */
      auto &splitRef = *splitInfo;
      splitRef.leftChild = node;
      splitRef.rightChild = sibling;
      splitRef.key = splitKey;
  }


575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
  /**
   * Insert a (key, value) pair at the given position @c pos into the leaf node
   * @c node. The caller has to ensure that
   * - there is enough space to insert the element
   * - the key is inserted at the correct position according to the order of
   * keys
   *
   * @oaram node the leaf node where the element is to be inserted
   * @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
   */
  void insertInLeafNodeAtPosition(persistent_ptr<LeafNode> node, unsigned int pos,
      const KeyType &key, const ValueType &val) {
    assert(pos < M);

591
    /* insert the new entry at the given position */
592
593
    node->keys.get_rw()[pos] = key;
    node->values.get_rw()[pos] = val;
594
595
596
597
598
599
600
601
    //PersistEmulation::persistStall();

    /* set bit and hash */
    node->search.get_rw().b.set(pos);
    node->search.get_rw().fp[pos] = fpHash(key);
    //PersistEmulation::persistStall();
    if(sizeof(LeafSearch) > 64) PersistEmulation::persistStall();
    //PersistEmulation::writeBytes(sizeof(KeyType) + sizeof(ValueType) + 2);
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
  }

  /**
   * 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
   */
  bool insertInBranchNode(BranchNode *node, unsigned int depth,
      const KeyType &key, const ValueType &val,
      SplitInfo *splitInfo) {
    SplitInfo childSplitInfo;
    bool split = false, hasSplit = false;

    auto pos = lookupPositionInBranchNode(node, key);
622
623
624
625
    if (depth == 1) {
      //case #1: our children are leaf nodes
      auto child = node->children[pos].leaf;
      hasSplit = insertInLeafNode(child, key, val, &childSplitInfo);
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
    } else {
      // case #2: our children are branch nodes
      auto child = node->children[pos].branch;
      hasSplit = insertInBranchNode(child, depth - 1, key, val, &childSplitInfo);
    }
    if (hasSplit) {
      BranchNode *host = node;
      // the child node was split, thus we have to add a new entry
      // to our branch node
      if (node->numKeys == N) {
        splitBranchNode(node, childSplitInfo.key, splitInfo);

        host = (key < splitInfo->key ? splitInfo->leftChild
            : splitInfo->rightChild).branch;
        split = true;
        pos = lookupPositionInBranchNode(host, key);
      }
      if (pos < host->numKeys) {
        // if the child isn't inserted at the rightmost position
        // then we have to make space for it
        host->children[host->numKeys + 1] = host->children[host->numKeys];
        for (auto i = host->numKeys; i > pos; i--) {
          host->children[i] = host->children[i - 1];
          host->keys[i] = host->keys[i - 1];
        }
      }
      // finally, add the new entry at the given position
      host->keys[pos] = childSplitInfo.key;
      host->children[pos] = childSplitInfo.leftChild;
      host->children[pos + 1] = childSplitInfo.rightChild;
      host->numKeys = host->numKeys + 1;
    }
    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
   */
  void splitBranchNode(BranchNode *node, const KeyType &splitKey,
      SplitInfo *splitInfo) {
    // we have an overflow at the branch node, let's split it
    // determine the split position
    unsigned int middle = (N + 1) / 2;
    // adjust the middle based on the key we have to insert
    if (splitKey > node->keys[middle]) middle++;
676

677
678
679
680
681
682
683
684
685
    // move all entries behind this position to a new sibling node
    BranchNode *sibling = newBranchNode();
    sibling->numKeys = node->numKeys - middle;
    for (auto i = 0u; i < sibling->numKeys; i++) {
      sibling->keys[i] = node->keys[middle + i];
      sibling->children[i] = node->children[middle + i];
    }
    sibling->children[sibling->numKeys] = node->children[node->numKeys];
    node->numKeys = middle - 1;
686

687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
    splitInfo->key = node->keys[middle - 1];
    splitInfo->leftChild = node;
    splitInfo->rightChild = sibling;
  }

  /**
   * 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
   */
  persistent_ptr<LeafNode> findLeafNode(const KeyType &key) const {
    auto node = rootNode;

    auto d = depth;
704
    while (d-- > 0) {
705
706
707
708
709
710
711
712
713
      auto n = node.branch;
      auto pos = lookupPositionInBranchNode(n, key);
      node = n->children[pos];
    }
    return node.leaf;
  }
  /**
   * Lookup the search key @c key in the given leaf node and return the
   * position.
714
   * If the search key was not found, then @c M is returned.
715
716
717
   *
   * @param node the leaf node where we search
   * @param key the search key
718
   * @return the position of the key  (or @c M if not found)
719
   */
720
  unsigned int lookupPositionInLeafNode(const persistent_ptr<LeafNode> node,
721
      const KeyType &key) const {
722
    unsigned int pos = 0u;
Philipp Götze's avatar
Philipp Götze committed
723
    const auto &nodeRef = *node;
724
    const auto hash = fpHash(key);
Philipp Götze's avatar
Philipp Götze committed
725
726
    const auto &keys = nodeRef.keys.get_ro();
    const auto &search = nodeRef.search.get_ro();
727
    for (; pos < M ; pos++) {
728
729
730
      if(search.fp[pos] == hash &&
         search.b.test(pos) &&
         keys[pos] == key)
731
732
        break;
    }
733
734
735
736
    return pos;
  }

  /**
737
   * Lookup the search key @c key in the given branch node and return the
738
739
740
741
742
743
   * 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.
   *
744
   * @param node the branch node where we search
745
746
747
   * @param key the search key
   * @return the position of the key + 1 (or 0 or @c numKey)
   */
748
  unsigned int lookupPositionInBranchNode(BranchNode *node,
749
      const KeyType &key) const {
750
    auto pos = 0u;
751
752
753
754
755
    const auto &num = node->numKeys;
    //const auto &keys = node->keys;
    //for (; pos < num && keys[pos] <= key; pos++);
    //return pos;
    return binarySearch(node, 0, num-1, key);
756
757
  }

758
  unsigned int binarySearch(BranchNode *node, int l, int r,
759
                            KeyType const &key) const {
760
    auto pos = 0u;
761
    const auto &keys = node->keys;
762
763
    while (l <= r) {
      pos = (l + r) / 2;
764
765
      if (keys[pos] == key) return ++pos;
      if (keys[pos] < key) l = ++pos;
766
767
      else r = pos - 1;
    }
768
769
770
    return pos;
  }

771

772
773
774
775
776
777
778
779
780
  /**
   * 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
   */
  bool eraseFromLeafNode(persistent_ptr <LeafNode> node, const KeyType &key) {
    auto pos = lookupPositionInLeafNode(node, key);
781
    if (pos < M) {
782
      node->search.get_rw().b.reset(pos);
783
      return true;
784
    }
785
    return false;
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
  }

  /**
   * 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
   */
  bool eraseFromBranchNode(BranchNode *node, unsigned int d, const KeyType &key) {
    assert(d >= 1);
    bool deleted = false;
    // try to find the branch
    auto pos = lookupPositionInBranchNode(node, key);
802
803
804
805
806
    if (d == 1) {
      // the next level is the leaf level
      auto leaf = node->children[pos].leaf;
      assert(leaf != nullptr);
      deleted = eraseFromLeafNode(leaf, key);
807
      unsigned int middle = (M + 1) / 2;
808
      if (leaf->search.get_ro().b.count() < middle) {
809
        // handle underflow
810
        underflowAtLeafLevel(node, pos, leaf);
811
812
813
814
815
816
817
818
819
820
821
822
823
      }
    } else {
      auto child = node->children[pos].branch;
      deleted = eraseFromBranchNode(child, d - 1, key);

      pos = lookupPositionInBranchNode(node, key);
      unsigned int middle = (N + 1) / 2;
      if (child->numKeys < middle) {
        // handle underflow
        child = underflowAtBranchLevel(node, pos, child);
        if (d == depth && node->numKeys == 0) {
          // special case: the root node is empty now
          rootNode = child;
824
          --depth;
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
        }
      }
    }
    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 of the child node @leaf in the @c children array of
   * the lowest branch node
   * @param leaf the node at which the underflow occured
   */
842
843
  void underflowAtLeafLevel(BranchNode *node, unsigned int pos,
      persistent_ptr<LeafNode> leaf) {
844
845
846
847
848
849
850
    assert(pos <= node->numKeys);
    unsigned int 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 > 0 && leaf->prevLeaf->search.get_ro().b.count() > middle) {
      // we have a sibling at the left for rebalancing the keys
      balanceLeafNodes(leaf->prevLeaf, leaf);
851

852
853
854
855
      node->keys[pos - 1] = leaf->keys.get_ro()[findMinKeyAtLeafNode(leaf)];
    } else if (pos < node->numKeys && leaf->nextLeaf->search.get_ro().b.count() > middle) {
      // we have a sibling at the right for rebalancing the keys
      balanceLeafNodes(leaf->nextLeaf, leaf);
856

857
858
859
860
861
862
863
864
865
866
867
868
869
870
      node->keys[pos] = leaf->nextLeaf->keys.get_ro()[findMinKeyAtLeafNode(leaf->nextLeaf)];
    } else {
      // 2. if this fails we have to merge two leaf nodes
      // but only if both nodes have the same direct parent
      persistent_ptr <LeafNode> survivor = nullptr;
      if (pos > 0 && leaf->prevLeaf->search.get_ro().b.count() <= middle) {
        survivor = mergeLeafNodes(leaf->prevLeaf, leaf);
        deleteLeafNode(leaf);
      } else if (pos < node->numKeys && leaf->nextLeaf->search.get_ro().b.count() <= middle) {
        // because we update the pointers in mergeLeafNodes
        // we keep it here
        auto l = leaf->nextLeaf;
        survivor = mergeLeafNodes(leaf, l);
        deleteLeafNode(l);
871
      } else {
872
873
874
875
876
877
878
879
880
        // this shouldn't happen?!
        assert(false);
      }
      if (node->numKeys > 1) {
        if (pos > 0) pos--;
        // just remove the child node from the current lowest branch node
        for (auto i = pos; i < node->numKeys - 1; i++) {
          node->keys[i] = node->keys[i + 1];
          node->children[i + 1] = node->children[i + 2];
881
        }
882
883
884
885
886
887
888
889
        node->children[pos] = survivor;
        --node->numKeys;
      } 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;
890
891
      }
    }
892
  }
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907

  /**
   * 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)
   */
  BranchNode* underflowAtBranchLevel(BranchNode *node, unsigned int pos,
908
                                     BranchNode* child) {
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
    assert(node != nullptr);
    assert(child != nullptr);

    BranchNode *newChild = child;
    unsigned int middle = (N + 1) / 2;
    // 1. we check whether we can rebalance with one of the siblings

    if (pos > 0 &&
        node->children[pos - 1].branch->numKeys >middle) {
      // we have a sibling at the left for rebalancing the keys

      BranchNode *sibling = node->children[pos - 1].branch;
      balanceBranchNodes(sibling, child, node, pos - 1);
      // node->keys.get_rw()[pos] = child->keys.get_ro()[0];
      return newChild;
    } else if (pos < node->numKeys && node->children[pos + 1].branch->numKeys > middle) {
      // we have a sibling at the right for rebalancing the keys
      auto sibling = node->children[pos + 1].branch;
      balanceBranchNodes(sibling, child, node, pos);

      return newChild;
    } else {

      // 2. if this fails we have to merge two branch nodes
      BranchNode *lSibling = nullptr, *rSibling = nullptr;
      unsigned int prevKeys = 0, nextKeys = 0;

      if (pos > 0) {

        lSibling = node->children[pos - 1].branch;
        prevKeys = lSibling->numKeys;
      }
      if (pos < node->numKeys) {

        rSibling = node->children[pos + 1].branch;
        nextKeys = rSibling->numKeys;
      }

      BranchNode *witnessNode = nullptr;
      auto ppos = pos;
      if (prevKeys > 0) {

        mergeBranchNodes(lSibling, node->keys[pos - 1], child);
        ppos = pos - 1;
        witnessNode = child;
        newChild = lSibling;
        // pos -= 1;
      } else if (nextKeys > 0) {

        mergeBranchNodes(child, node->keys[pos], rSibling);
        witnessNode = rSibling;
      } else
        // shouldn't happen
        assert(false);

      // remove node->keys.get_ro()[pos] from node
      for (auto i = ppos; i < node->numKeys - 1; i++) {

        node->keys[i] = node->keys[i + 1];
      }
      if (pos == 0) pos++;
      for (auto i = pos; i < node->numKeys; i++) {
        if (i + 1 <= node->numKeys) {


          node->children[i] = node->children[i + 1];
        }
      }
      node->numKeys--;

      deleteBranchNode(witnessNode);
      return newChild;
    }
  }

  /**
   * Redistribute (key, value) pairs from the leaf node @c donor to
   * the leaf node @c receiver such that both nodes have approx. the same
   * number of elements. This method is used in case of an underflow
   * situation of a leaf node.
   *
   * @param donor the leaf node from which the elements are taken
   * @param receiver the sibling leaf node getting the elements from @c donor
   */
  void balanceLeafNodes(persistent_ptr <LeafNode> donor, persistent_ptr <LeafNode> receiver) {
994
995
    const auto dNumKeys = donor->search.get_ro().b.count();
    const auto rNumKeys = receiver->search.get_ro().b.count();
996
997
998
    assert(dNumKeys > rNumKeys);
    unsigned int balancedNum = (dNumKeys + rNumKeys) / 2;
    unsigned int toMove = dNumKeys - balancedNum;
999
1000
    if (toMove == 0) return;

For faster browsing, not all history is shown. View entire blame