souffle  2.0.2-371-g6315b36
Public Member Functions | Data Fields | Static Public Attributes | Private Member Functions
souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node Struct Reference

The actual, generic node implementation covering the operations for both, inner and leaf nodes. More...

#include <BTree.h>

Inheritance diagram for souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node:
Inheritance graph
Collaboration diagram for souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node:
Collaboration graph

Public Member Functions

inner_nodeasInnerNode ()
 A utility function providing a reference to this node as an inner node. More...
 
const inner_nodeasInnerNode () const
 A utility function providing a reference to this node as a const inner node. More...
 
template<typename Comp >
bool check (Comp &comp, const node *root) const
 A function to verify the consistency of this node. More...
 
nodeclone () const
 A deep-copy operation creating a clone of this node. More...
 
std::vector< chunk > & collectChunks (std::vector< chunk > &res, size_type num, const iterator &begin, const iterator &end) const
 A function decomposing the sub-tree rooted by this node into approximately equally sized chunks. More...
 
size_type countEntries () const
 Counts the number of entries contained in the sub-tree rooted by this node. More...
 
size_type countNodes () const
 Counts the number of nodes contained in the sub-tree rooted by this node. More...
 
nodegetChild (size_type s) const
 Obtains a reference to the child of the given index. More...
 
node ** getChildren ()
 Obtains a pointer to the array of child-pointers of this node – if it is an inner node. More...
 
node *const * getChildren () const
 Obtains a pointer to the array of const child-pointers of this node – if it is an inner node. More...
 
size_type getDepth () const
 Computes the number of nested levels of the tree rooted by this node. More...
 
size_type getMemoryUsage () const
 Determines the amount of memory used by the sub-tree rooted by this node. More...
 
int getSplitPoint (int)
 Obtains the point at which full nodes should be split. More...
 
bool isEmpty () const
 Checks whether this node is empty – can happen due to biased insertion. More...
 
bool isFull () const
 Checks whether this node is full. More...
 
 node (bool inner)
 
void printTree (std::ostream &out, const std::string &prefix) const
 Prints a textual representation of this tree to the given output stream. More...
 
int rebalance_or_split (node **root, lock_type &root_lock, int idx)
 Moves keys from this node to one of its siblings or splits this node to make some space for the insertion of an element at position idx. More...
 
void split (node **root, lock_type &root_lock, int idx)
 Splits this node. More...
 
- Public Member Functions inherited from souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::base
 base (bool inner)
 A simple constructor for nodes. More...
 
size_type getNumElements () const
 
nodegetParent () const
 
field_index_type getPositionInParent () const
 
bool isInner () const
 
bool isLeaf () const
 

Data Fields

Key keys [maxKeys]
 
- Data Fields inherited from souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::base
const bool inner
 
size_type numElements
 
nodeparent
 
field_index_type position
 

Static Public Attributes

static constexpr size_t desiredNumKeys
 The number of keys/node desired by the user. More...
 
static constexpr size_t maxKeys = (desiredNumKeys > 3) ? desiredNumKeys : 3
 The actual number of keys/node corrected by functional requirements. More...
 

Private Member Functions

void grow_parent (node **root, lock_type &root_lock, node *sibling)
 Inserts a new sibling into the parent of this node utilizing the last key of this node as a separation key. More...
 
void insert_inner (node **root, lock_type &root_lock, unsigned pos, node *predecessor, const Key &key, node *newNode)
 Inserts a new element into an inner node (for internal use only). More...
 

Detailed Description

template<typename Key, typename Comparator, typename Allocator, unsigned blockSize, typename SearchStrategy, bool isSet, typename WeakComparator = Comparator, typename Updater = detail::updater<Key>>
struct souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node

The actual, generic node implementation covering the operations for both, inner and leaf nodes.

Definition at line 380 of file BTree.h.

Constructor & Destructor Documentation

◆ node()

template<typename Key , typename Comparator , typename Allocator , unsigned blockSize, typename SearchStrategy , bool isSet, typename WeakComparator = Comparator, typename Updater = detail::updater<Key>>
souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::node ( bool  inner)
inline

Member Function Documentation

◆ asInnerNode() [1/2]

template<typename Key , typename Comparator , typename Allocator , unsigned blockSize, typename SearchStrategy , bool isSet, typename WeakComparator = Comparator, typename Updater = detail::updater<Key>>
inner_node& souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::asInnerNode ( )
inline

◆ asInnerNode() [2/2]

template<typename Key , typename Comparator , typename Allocator , unsigned blockSize, typename SearchStrategy , bool isSet, typename WeakComparator = Comparator, typename Updater = detail::updater<Key>>
const inner_node& souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::asInnerNode ( ) const
inline

A utility function providing a reference to this node as a const inner node.

Definition at line 443 of file BTree.h.

449  {

◆ check()

template<typename Key , typename Comparator , typename Allocator , unsigned blockSize, typename SearchStrategy , bool isSet, typename WeakComparator = Comparator, typename Updater = detail::updater<Key>>
template<typename Comp >
bool souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::check ( Comp &  comp,
const node root 
) const
inline

A function to verify the consistency of this node.

Parameters
root... a reference to the root of the enclosing tree.
Returns
true if valid, false otherwise

Definition at line 994 of file BTree.h.

995  {
996  // check parent relation
997  if (!this->parent) {
998  std::cout << "Invalid null-parent!\n";
999  valid = false;
1000  } else {
1001  if (this->parent->getChildren()[this->position] != this) {
1002  std::cout << "Parent reference invalid!\n";
1003  std::cout << " Node: " << this << "\n";
1004  std::cout << " Parent: " << this->parent << "\n";
1005  std::cout << " Position: " << ((int)this->position) << "\n";
1006  valid = false;
1007  }
1008 
1009  // check parent key
1010  if (valid && this->position != 0 &&
1011  !(comp(this->parent->keys[this->position - 1], keys[0]) < ((isSet) ? 0 : 1))) {
1012  std::cout << "Left parent key not lower bound!\n";
1013  std::cout << " Node: " << this << "\n";
1014  std::cout << " Parent: " << this->parent << "\n";
1015  std::cout << " Position: " << ((int)this->position) << "\n";
1016  std::cout << " Key: " << (this->parent->keys[this->position]) << "\n";
1017  std::cout << " Lower: " << (keys[0]) << "\n";
1018  valid = false;
1019  }
1020 
1021  // check parent key
1022  if (valid && this->position != this->parent->numElements &&
1023  !(comp(keys[this->numElements - 1], this->parent->keys[this->position]) <
1024  ((isSet) ? 0 : 1))) {
1025  std::cout << "Right parent key not lower bound!\n";
1026  std::cout << " Node: " << this << "\n";
1027  std::cout << " Parent: " << this->parent << "\n";
1028  std::cout << " Position: " << ((int)this->position) << "\n";
1029  std::cout << " Key: " << (this->parent->keys[this->position]) << "\n";
1030  std::cout << " Upper: " << (keys[0]) << "\n";
1031  valid = false;
1032  }
1033  }
1034  }
1035 
1036  // check element order
1037  if (this->numElements > 0) {
1038  for (unsigned i = 0; i < this->numElements - 1; i++) {
1039  if (valid && !(comp(keys[i], keys[i + 1]) < ((isSet) ? 0 : 1))) {
1040  std::cout << "Element order invalid!\n";
1041  std::cout << " @" << this << " key " << i << " is " << keys[i] << " vs "
1042  << keys[i + 1] << "\n";
1043  valid = false;
1044  }
1045  }
1046  }
1047 
1048  // check state of sub-nodes
1049  if (this->inner) {
1050  for (unsigned i = 0; i <= this->numElements; i++) {
1051  valid &= getChildren()[i]->check(comp, root);
1052  }
1053  }
1054 
1055  return valid;
1056  }
1057  }; // namespace detail
1058 
1059  /**
1060  * The data type representing inner nodes of the b-tree. It extends
1061  * the generic implementation of a node by the storage locations
1062  * of child pointers.
1063  */
1064  struct inner_node : public node {
1065  // references to child nodes owned by this node
1066  node* children[node::maxKeys + 1];
1067 
1068  // a simple default constructor initializing member fields
1069  inner_node() : node(true) {}
1070 

◆ clone()

template<typename Key , typename Comparator , typename Allocator , unsigned blockSize, typename SearchStrategy , bool isSet, typename WeakComparator = Comparator, typename Updater = detail::updater<Key>>
node* souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::clone ( ) const
inline

A deep-copy operation creating a clone of this node.

Definition at line 401 of file BTree.h.

401  {
402  return res;
403  }
404 
405  // copy child nodes recursively
406  auto* ires = (inner_node*)res;
407  for (size_type i = 0; i <= this->numElements; ++i) {
408  ires->children[i] = this->getChild(i)->clone();
409  ires->children[i]->parent = res;
410  }
411 
412  // that's it
413  return res;
414  }
415 
416  /**
417  * A utility function providing a reference to this node as
418  * an inner node.
419  */
420  inner_node& asInnerNode() {
421  assert(this->inner && "Invalid cast!");
422  return *static_cast<inner_node*>(this);
423  }
424 
425  /**
426  * A utility function providing a reference to this node as
427  * a const inner node.
428  */

◆ collectChunks()

template<typename Key , typename Comparator , typename Allocator , unsigned blockSize, typename SearchStrategy , bool isSet, typename WeakComparator = Comparator, typename Updater = detail::updater<Key>>
std::vector<chunk>& souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::collectChunks ( std::vector< chunk > &  res,
size_type  num,
const iterator begin,
const iterator end 
) const
inline

A function decomposing the sub-tree rooted by this node into approximately equally sized chunks.

To minimize computational overhead, no strict load balance nor limit on the number of actual chunks is given.

See also
btree::getChunks()
Parameters
res.. the list of chunks to be extended
num.. the number of chunks to be produced
begin.. the iterator to start the first chunk with
end.. the iterator to end the last chunk with
Returns
the handed in list of chunks extended by generated chunks

Definition at line 927 of file BTree.h.

932  {
933  auto step = this->numElements / num;
934  if (step == 0) {
935  step = 1;
936  }
937 
938  size_type i = 0;
939 
940  // the first chunk starts at the begin
941  res.push_back(chunk(begin, iterator(this, static_cast<field_index_type>(step) - 1)));
942 
943  // split up the main part
944  for (i = step - 1; i < this->numElements - step; i += step) {
945  res.push_back(chunk(iterator(this, static_cast<field_index_type>(i)),
946  iterator(this, static_cast<field_index_type>(i + step))));
947  }
948 
949  // the last chunk runs to the end
950  res.push_back(chunk(iterator(this, static_cast<field_index_type>(i)), end));
951 
952  // done
953  return res;
954  }
955 
956  // else: collect chunks of sub-set elements
957 
958  auto part = num / (this->numElements + 1);
959  assert(part > 0);
960  getChild(0)->collectChunks(res, part, begin, iterator(this, 0));
961  for (size_type i = 1; i < this->numElements; i++) {
962  getChild(i)->collectChunks(res, part, iterator(this, static_cast<field_index_type>(i - 1)),
963  iterator(this, static_cast<field_index_type>(i)));
964  }
965  getChild(this->numElements)
966  ->collectChunks(res, num - (part * this->numElements),
967  iterator(this, static_cast<field_index_type>(this->numElements) - 1), end);
968 
969  // done
970  return res;
971  }
972 
973  /**
974  * A function to verify the consistency of this node.
975  *
976  * @param root ... a reference to the root of the enclosing tree.
977  * @return true if valid, false otherwise
978  */
979  template <typename Comp>
980  bool check(Comp& comp, const node* root) const {
981  bool valid = true;
982 
983  // check fill-state
984  if (this->numElements > maxKeys) {
985  std::cout << "Node with " << this->numElements << "/" << maxKeys << " encountered!\n";

◆ countEntries()

template<typename Key , typename Comparator , typename Allocator , unsigned blockSize, typename SearchStrategy , bool isSet, typename WeakComparator = Comparator, typename Updater = detail::updater<Key>>
size_type souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::countEntries ( ) const
inline

◆ countNodes()

template<typename Key , typename Comparator , typename Allocator , unsigned blockSize, typename SearchStrategy , bool isSet, typename WeakComparator = Comparator, typename Updater = detail::updater<Key>>
size_type souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::countNodes ( ) const
inline

◆ getChild()

template<typename Key , typename Comparator , typename Allocator , unsigned blockSize, typename SearchStrategy , bool isSet, typename WeakComparator = Comparator, typename Updater = detail::updater<Key>>
node* souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::getChild ( size_type  s) const
inline

◆ getChildren() [1/2]

template<typename Key , typename Comparator , typename Allocator , unsigned blockSize, typename SearchStrategy , bool isSet, typename WeakComparator = Comparator, typename Updater = detail::updater<Key>>
node** souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::getChildren ( )
inline

◆ getChildren() [2/2]

template<typename Key , typename Comparator , typename Allocator , unsigned blockSize, typename SearchStrategy , bool isSet, typename WeakComparator = Comparator, typename Updater = detail::updater<Key>>
node* const* souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::getChildren ( ) const
inline

Obtains a pointer to the array of const child-pointers of this node – if it is an inner node.

Definition at line 516 of file BTree.h.

516  {
517  return this->numElements == 0;
518  }

◆ getDepth()

template<typename Key , typename Comparator , typename Allocator , unsigned blockSize, typename SearchStrategy , bool isSet, typename WeakComparator = Comparator, typename Updater = detail::updater<Key>>
size_type souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::getDepth ( ) const
inline

Computes the number of nested levels of the tree rooted by this node.

Definition at line 452 of file BTree.h.

454  {
455  sum += getChild(i)->countNodes();
456  }
457  return sum;

Referenced by souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::asInnerNode().

◆ getMemoryUsage()

template<typename Key , typename Comparator , typename Allocator , unsigned blockSize, typename SearchStrategy , bool isSet, typename WeakComparator = Comparator, typename Updater = detail::updater<Key>>
size_type souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::getMemoryUsage ( ) const
inline

Determines the amount of memory used by the sub-tree rooted by this node.

Definition at line 493 of file BTree.h.

494  {
495  return asInnerNode().children;
496  }
497 
498  /**
499  * Obtains a pointer to the array of const child-pointers
500  * of this node -- if it is an inner node.
501  */
502  node* const* getChildren() const {

References souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::asInnerNode(), and souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::inner_node::children.

Referenced by souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::countEntries().

Here is the call graph for this function:

◆ getSplitPoint()

template<typename Key , typename Comparator , typename Allocator , unsigned blockSize, typename SearchStrategy , bool isSet, typename WeakComparator = Comparator, typename Updater = detail::updater<Key>>
int souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::getSplitPoint ( int  )
inline

Obtains the point at which full nodes should be split.

Conventional b-trees always split in half. However, in cases where in-order insertions are frequent, a split assigning larger portions to the right fragment provide higher performance and a better node-filling rate.

Definition at line 548 of file BTree.h.

◆ grow_parent()

template<typename Key , typename Comparator , typename Allocator , unsigned blockSize, typename SearchStrategy , bool isSet, typename WeakComparator = Comparator, typename Updater = detail::updater<Key>>
void souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::grow_parent ( node **  root,
lock_type root_lock,
node sibling 
)
inlineprivate

Inserts a new sibling into the parent of this node utilizing the last key of this node as a separation key.

(for internal use only)

Parameters
root.. a pointer to the root-pointer of the containing tree
sibling.. the new right-sibling to be add to the parent node

Definition at line 747 of file BTree.h.

755  {
756  // insert new element in parent element
757  auto parent = this->parent;
758  auto pos = this->position;
759 
760 #ifdef IS_PARALLEL
762  root, root_lock, pos, this, keys[this->numElements], sibling, locked_nodes);
763 #else
764  parent->insert_inner(root, root_lock, pos, this, keys[this->numElements], sibling);
765 #endif
766  }
767  }
768 
769  /**
770  * Inserts a new element into an inner node (for internal use only).
771  *
772  * @param root .. a pointer to the root-pointer of the containing tree
773  * @param pos .. the position to insert the new key
774  * @param key .. the key to insert
775  * @param newNode .. the new right-child of the inserted key
776  */
777 #ifdef IS_PARALLEL
778  void insert_inner(node** root, lock_type& root_lock, unsigned pos, node* predecessor, const Key& key,
779  node* newNode, std::vector<node*>& locked_nodes) {
780  assert(this->lock.is_write_locked());
781  assert(souffle::contains(locked_nodes, this));

◆ insert_inner()

template<typename Key , typename Comparator , typename Allocator , unsigned blockSize, typename SearchStrategy , bool isSet, typename WeakComparator = Comparator, typename Updater = detail::updater<Key>>
void souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::insert_inner ( node **  root,
lock_type root_lock,
unsigned  pos,
node predecessor,
const Key &  key,
node newNode 
)
inlineprivate

Inserts a new element into an inner node (for internal use only).

Parameters
root.. a pointer to the root-pointer of the containing tree
pos.. the position to insert the new key
key.. the key to insert
newNode.. the new right-child of the inserted key

Definition at line 797 of file BTree.h.

803  {
804  // correct position
805  pos = pos - static_cast<unsigned int>(this->numElements) - 1;
806 
807  // get new sibling
808  auto other = this->parent->getChild(this->position + 1);
809 
810 #ifdef IS_PARALLEL
811  // make sure other side is write locked
812  assert(other->lock.is_write_locked());
813  assert(souffle::contains(locked_nodes, other));
814 
815  // search for new position (since other may have been altered in the meanwhile)
816  size_type i = 0;
817  for (; i <= other->numElements; ++i) {
818  if (other->getChild(i) == predecessor) {
819  break;
820  }
821  }
822 
823  pos = (i > other->numElements) ? 0 : i;
824  other->insert_inner(root, root_lock, pos, predecessor, key, newNode, locked_nodes);
825 #else
826  other->insert_inner(root, root_lock, pos, predecessor, key, newNode);
827 #endif
828  return;
829  }
830  }
831 
832  // move bigger keys one forward
833  for (int i = static_cast<int>(this->numElements) - 1; i >= (int)pos; --i) {
834  keys[i + 1] = keys[i];
835  getChildren()[i + 2] = getChildren()[i + 1];
836  ++getChildren()[i + 2]->position;
837  }
838 
839  // ensure proper position
840  assert(getChild(pos) == predecessor);
841 
842  // insert new element
843  keys[pos] = key;
844  getChildren()[pos + 1] = newNode;
845  newNode->parent = this;
846  newNode->position = static_cast<field_index_type>(pos) + 1;
847  ++this->numElements;
848  }
849 
850  public:
851  /**
852  * Prints a textual representation of this tree to the given output stream.
853  * This feature is mainly intended for debugging and tuning purposes.
854  *
855  * @see btree::printTree
856  */
857  void printTree(std::ostream& out, const std::string& prefix) const {
858  // print the header
859  out << prefix << "@" << this << "[" << ((int)(this->position)) << "] - "
860  << (this->inner ? "i" : "") << "node : " << this->numElements << "/" << maxKeys << " [";
861 
862  // print the keys

◆ isEmpty()

template<typename Key , typename Comparator , typename Allocator , unsigned blockSize, typename SearchStrategy , bool isSet, typename WeakComparator = Comparator, typename Updater = detail::updater<Key>>
bool souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::isEmpty ( ) const
inline

Checks whether this node is empty – can happen due to biased insertion.

Definition at line 530 of file BTree.h.

534  {

References souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::maxKeys.

◆ isFull()

template<typename Key , typename Comparator , typename Allocator , unsigned blockSize, typename SearchStrategy , bool isSet, typename WeakComparator = Comparator, typename Updater = detail::updater<Key>>
bool souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::isFull ( ) const
inline

Checks whether this node is full.

Definition at line 537 of file BTree.h.

◆ printTree()

template<typename Key , typename Comparator , typename Allocator , unsigned blockSize, typename SearchStrategy , bool isSet, typename WeakComparator = Comparator, typename Updater = detail::updater<Key>>
void souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::printTree ( std::ostream &  out,
const std::string &  prefix 
) const
inline

Prints a textual representation of this tree to the given output stream.

This feature is mainly intended for debugging and tuning purposes.

See also
btree::printTree

Definition at line 871 of file BTree.h.

872  {
873  out << " - [";
874  for (unsigned i = 0; i <= this->numElements; i++) {
875  out << getChildren()[i];
876  if (i != this->numElements) {
877  out << ",";
878  }
879  }
880  out << "]";
881  }
882 
883 #ifdef IS_PARALLEL
884  // print the lock state
885  if (this->lock.is_write_locked()) {
886  std::cout << " locked";
887  }
888 #endif
889 
890  out << "\n";
891 
892  // print the children recursively
893  if (this->inner) {
894  for (unsigned i = 0; i < this->numElements + 1; ++i) {
895  static_cast<const inner_node*>(this)->children[i]->printTree(out, prefix + " ");
896  }
897  }
898  }
899 
900  /**
901  * A function decomposing the sub-tree rooted by this node into approximately equally
902  * sized chunks. To minimize computational overhead, no strict load balance nor limit
903  * on the number of actual chunks is given.
904  *
905  * @see btree::getChunks()
906  *
907  * @param res .. the list of chunks to be extended
908  * @param num .. the number of chunks to be produced
909  * @param begin .. the iterator to start the first chunk with
910  * @param end .. the iterator to end the last chunk with
911  * @return the handed in list of chunks extended by generated chunks
912  */

References souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::getChildren(), i, and souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::base::numElements.

Here is the call graph for this function:

◆ rebalance_or_split()

template<typename Key , typename Comparator , typename Allocator , unsigned blockSize, typename SearchStrategy , bool isSet, typename WeakComparator = Comparator, typename Updater = detail::updater<Key>>
int souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::rebalance_or_split ( node **  root,
lock_type root_lock,
int  idx 
)
inline

Moves keys from this node to one of its siblings or splits this node to make some space for the insertion of an element at position idx.

Returns the number of elements moved to the left side, 0 in case of a split. The number of moved elements will be <= the given idx.

Parameters
root.. the root node of the b-tree being part of
idx.. the position of the insert triggering this operation

Definition at line 632 of file BTree.h.

634  {
635  // left node is currently updated => skip balancing and split
636  split(root, root_lock, idx, locked_nodes);
637  return 0;
638  }
639 #endif
640 
641  // compute number of elements to be movable to left
642  // space available in left vs. insertion index
643  size_type num = static_cast<size_type>(
644  std::min<int>(static_cast<int>(maxKeys - left->numElements), idx));
645 
646  // if there are elements to move ..
647  if (num > 0) {
648  Key* splitter = &(parent->keys[this->position - 1]);
649 
650  // .. move keys to left node
651  left->keys[left->numElements] = *splitter;
652  for (size_type i = 0; i < num - 1; ++i) {
653  left->keys[left->numElements + 1 + i] = keys[i];
654  }
655  *splitter = keys[num - 1];
656 
657  // shift keys in this node to the left
658  for (size_type i = 0; i < this->numElements - num; ++i) {
659  keys[i] = keys[i + num];
660  }
661 
662  // .. and children if necessary
663  if (this->isInner()) {
664  auto* ileft = static_cast<inner_node*>(left);
665  auto* iright = static_cast<inner_node*>(this);
666 
667  // move children
668  for (field_index_type i = 0; i < num; ++i) {
669  ileft->children[left->numElements + i + 1] = iright->children[i];
670  }
671 
672  // update moved children
673  for (size_type i = 0; i < num; ++i) {
674  iright->children[i]->parent = ileft;
675  iright->children[i]->position =
676  static_cast<field_index_type>(left->numElements + i) + 1;
677  }
678 
679  // shift child-pointer to the left
680  for (size_type i = 0; i < this->numElements - num + 1; ++i) {
681  iright->children[i] = iright->children[i + num];
682  }
683 
684  // update position of children
685  for (size_type i = 0; i < this->numElements - num + 1; ++i) {
686  iright->children[i]->position = static_cast<field_index_type>(i);
687  }
688  }
689 
690  // update node sizes
691  left->numElements += num;
692  this->numElements -= num;
693 
694 #ifdef IS_PARALLEL
695  left->lock.end_write();
696 #endif
697 
698  // done
699  return static_cast<int>(num);
700  }
701 
702 #ifdef IS_PARALLEL
703  left->lock.abort_write();
704 #endif
705  }
706 
707  // Option B) split node
708 #ifdef IS_PARALLEL
709  split(root, root_lock, idx, locked_nodes);
710 #else
711  split(root, root_lock, idx);
712 #endif
713  return 0; // = no re-balancing
714  }
715 
716  private:
717  /**
718  * Inserts a new sibling into the parent of this node utilizing
719  * the last key of this node as a separation key. (for internal
720  * use only)
721  *
722  * @param root .. a pointer to the root-pointer of the containing tree
723  * @param sibling .. the new right-sibling to be add to the parent node
724  */
725 #ifdef IS_PARALLEL
726  void grow_parent(node** root, lock_type& root_lock, node* sibling, std::vector<node*>& locked_nodes) {
727  assert(this->lock.is_write_locked());
728  assert(!this->parent || this->parent->lock.is_write_locked());

References souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::root, souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::root_lock, and souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::split().

Here is the call graph for this function:

◆ split()

template<typename Key , typename Comparator , typename Allocator , unsigned blockSize, typename SearchStrategy , bool isSet, typename WeakComparator = Comparator, typename Updater = detail::updater<Key>>
void souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::split ( node **  root,
lock_type root_lock,
int  idx 
)
inline

Splits this node.

Parameters
root.. a pointer to the root-pointer of the enclosing b-tree (might have to be updated if the root-node needs to be split)
idx.. the position of the insert causing the split

Definition at line 567 of file BTree.h.

571  {
572  sibling->keys[j] = keys[i];
573  }
574 
575  // move child pointers
576  if (this->inner) {
577  // move pointers to sibling
578  auto* other = static_cast<inner_node*>(sibling);
579  for (unsigned i = split_point + 1, j = 0; i <= maxKeys; ++i, ++j) {
580  other->children[j] = getChildren()[i];
581  other->children[j]->parent = other;
582  other->children[j]->position = static_cast<field_index_type>(j);
583  }
584  }
585 
586  // update number of elements
587  this->numElements = split_point;
588  sibling->numElements = maxKeys - split_point - 1;
589 
590  // update parent
591 #ifdef IS_PARALLEL
592  grow_parent(root, root_lock, sibling, locked_nodes);
593 #else
594  grow_parent(root, root_lock, sibling);
595 #endif
596  }
597 
598  /**
599  * Moves keys from this node to one of its siblings or splits
600  * this node to make some space for the insertion of an element at
601  * position idx.
602  *
603  * Returns the number of elements moved to the left side, 0 in case
604  * of a split. The number of moved elements will be <= the given idx.
605  *
606  * @param root .. the root node of the b-tree being part of
607  * @param idx .. the position of the insert triggering this operation
608  */
609  // TODO: remove root_lock ... no longer needed
610 #ifdef IS_PARALLEL

References i, j, and souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::keys.

Referenced by souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::rebalance_or_split().

Field Documentation

◆ desiredNumKeys

template<typename Key , typename Comparator , typename Allocator , unsigned blockSize, typename SearchStrategy , bool isSet, typename WeakComparator = Comparator, typename Updater = detail::updater<Key>>
constexpr size_t souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::desiredNumKeys
staticconstexpr
Initial value:
=
((blockSize > sizeof(base)) ? blockSize - sizeof(base) : 0) / sizeof(Key)

The number of keys/node desired by the user.

Definition at line 384 of file BTree.h.

◆ keys

template<typename Key , typename Comparator , typename Allocator , unsigned blockSize, typename SearchStrategy , bool isSet, typename WeakComparator = Comparator, typename Updater = detail::updater<Key>>
Key souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::keys[maxKeys]

◆ maxKeys

template<typename Key , typename Comparator , typename Allocator , unsigned blockSize, typename SearchStrategy , bool isSet, typename WeakComparator = Comparator, typename Updater = detail::updater<Key>>
constexpr size_t souffle::detail::btree< Key, Comparator, Allocator, blockSize, SearchStrategy, isSet, WeakComparator, Updater >::node::maxKeys = (desiredNumKeys > 3) ? desiredNumKeys : 3
staticconstexpr

The documentation for this struct was generated from the following file:
souffle::detail::btree::root_lock
lock_type root_lock
Definition: BTree.h:1245
souffle::detail::btree::node::countNodes
size_type countNodes() const
Counts the number of nodes contained in the sub-tree rooted by this node.
Definition: BTree.h:463
souffle::detail::btree::node::collectChunks
std::vector< chunk > & collectChunks(std::vector< chunk > &res, size_type num, const iterator &begin, const iterator &end) const
A function decomposing the sub-tree rooted by this node into approximately equally sized chunks.
Definition: BTree.h:927
souffle::detail::btree::base::position
field_index_type position
Definition: BTree.h:342
souffle::detail::btree::node::check
bool check(Comp &comp, const node *root) const
A function to verify the consistency of this node.
Definition: BTree.h:994
souffle::detail::btree::end
iterator end() const
Definition: BTree.h:1729
souffle::detail::btree::field_index_type
uint8_t field_index_type
Definition: BTree.h:311
souffle::contains
bool contains(const C &container, const typename C::value_type &element)
A utility to check generically whether a given element is contained in a given container.
Definition: ContainerUtil.h:75
souffle::detail::btree::comp
Comparator comp
Definition: BTree.h:281
souffle::detail::btree::base::isInner
bool isInner() const
Definition: BTree.h:357
souffle::detail::btree::node::grow_parent
void grow_parent(node **root, lock_type &root_lock, node *sibling)
Inserts a new sibling into the parent of this node utilizing the last key of this node as a separatio...
Definition: BTree.h:747
souffle::detail::btree::size_type
std::size_t size_type
Definition: BTree.h:310
souffle::detail::btree::chunk
range< iterator > chunk
Definition: BTree.h:272
souffle::detail::btree::node::split
void split(node **root, lock_type &root_lock, int idx)
Splits this node.
Definition: BTree.h:567
j
var j
Definition: htmlJsChartistMin.h:15
souffle::detail::btree::node::getChildren
node ** getChildren()
Obtains a pointer to the array of child-pointers of this node – if it is an inner node.
Definition: BTree.h:508
souffle::detail::btree::begin
iterator begin() const
Definition: BTree.h:1724
souffle::detail::btree::node::keys
Key keys[maxKeys]
Definition: BTree.h:393
souffle::detail::btree::base::parent
node * parent
Definition: BTree.h:336
i
size_t i
Definition: json11.h:663
souffle::detail::btree::base::numElements
size_type numElements
Definition: BTree.h:339
souffle::detail::btree::node::countEntries
size_type countEntries() const
Counts the number of entries contained in the sub-tree rooted by this node.
Definition: BTree.h:478
souffle::detail::btree::root
node * root
Definition: BTree.h:1242
souffle::detail::btree::inner_node::children
node * children[node::maxKeys+1]
Definition: BTree.h:1080
souffle::detail::btree::lock_type
OptimisticReadWriteLock lock_type
Definition: BTree.h:312
souffle::detail::btree::node::insert_inner
void insert_inner(node **root, lock_type &root_lock, unsigned pos, node *predecessor, const Key &key, node *newNode)
Inserts a new element into an inner node (for internal use only).
Definition: BTree.h:797
souffle::detail::btree::node::getMemoryUsage
size_type getMemoryUsage() const
Determines the amount of memory used by the sub-tree rooted by this node.
Definition: BTree.h:493
souffle::detail::btree::node::clone
node * clone() const
A deep-copy operation creating a clone of this node.
Definition: BTree.h:401
souffle::detail::btree::base::isLeaf
bool isLeaf() const
Definition: BTree.h:353
souffle::detail::btree::node::getChild
node * getChild(size_type s) const
Obtains a reference to the child of the given index.
Definition: BTree.h:523
souffle::detail::btree::node::asInnerNode
inner_node & asInnerNode()
A utility function providing a reference to this node as an inner node.
Definition: BTree.h:434
souffle::detail::btree::node::printTree
void printTree(std::ostream &out, const std::string &prefix) const
Prints a textual representation of this tree to the given output stream.
Definition: BTree.h:871
souffle::detail::btree::base::inner
const bool inner
Definition: BTree.h:346
souffle::detail::btree::node::maxKeys
static constexpr size_t maxKeys
The actual number of keys/node corrected by functional requirements.
Definition: BTree.h:390
souffle::detail::btree::base::base
base(bool inner)
A simple constructor for nodes.
Definition: BTree.h:351
souffle::detail::btree::node::node
node(bool inner)
Definition: BTree.h:396