diff options
author | Tor Egge <Tor.Egge@yahoo-inc.com> | 2017-06-23 15:25:12 +0000 |
---|---|---|
committer | Tor Egge <Tor.Egge@yahoo-inc.com> | 2017-06-23 15:25:12 +0000 |
commit | 671fb64d9f7029e21b293245d9570da0187f08e6 (patch) | |
tree | a9a9b5342e6e8c2d1d3a3cffff0a3bcd9885f479 /searchlib | |
parent | d0d380f57e0cb0ad0b4ceba6ba3f2c5b68107a1c (diff) |
Rename variables and method for clarity.
Diffstat (limited to 'searchlib')
-rw-r--r-- | searchlib/src/vespa/searchlib/btree/btreeinserter.h | 4 | ||||
-rw-r--r-- | searchlib/src/vespa/searchlib/btree/btreeinserter.hpp | 73 | ||||
-rw-r--r-- | searchlib/src/vespa/searchlib/btree/btreeiterator.hpp | 58 |
3 files changed, 71 insertions, 64 deletions
diff --git a/searchlib/src/vespa/searchlib/btree/btreeinserter.h b/searchlib/src/vespa/searchlib/btree/btreeinserter.h index 620e64d3115..a3fa2916a88 100644 --- a/searchlib/src/vespa/searchlib/btree/btreeinserter.h +++ b/searchlib/src/vespa/searchlib/btree/btreeinserter.h @@ -44,8 +44,10 @@ public: typedef typename LeafNodeType::RefPair LeafNodeTypeRefPair; using Inserter = BTreeInserter<KeyT, DataT, AggrT, CompareT, TraitsT, AggrCalcT>; - static void giveLeafEntries(LeafNodeType *sNode, Iterator &itr, AggrCalcT aggrCalc); +private: + static void rebalanceLeafEntries(LeafNodeType *leafNode, Iterator &itr, AggrCalcT aggrCalc); +public: static void insert(BTreeNode::Ref &root, Iterator &itr, diff --git a/searchlib/src/vespa/searchlib/btree/btreeinserter.hpp b/searchlib/src/vespa/searchlib/btree/btreeinserter.hpp index 5be006ea0f7..d95579abe9c 100644 --- a/searchlib/src/vespa/searchlib/btree/btreeinserter.hpp +++ b/searchlib/src/vespa/searchlib/btree/btreeinserter.hpp @@ -10,64 +10,69 @@ namespace search { namespace btree { +namespace { + +template <typename NodeType, typename NodeAllocatorType> +void +considerThawNode(NodeType *&node, BTreeNode::Ref &ref, NodeAllocatorType &allocator) +{ + if (node->getFrozen()) { + auto thawed = allocator.thawNode(ref, node); + ref = thawed.ref; + node = thawed.data; + } +} + +} + template <typename KeyT, typename DataT, typename AggrT, typename CompareT, typename TraitsT, class AggrCalcT> void -BTreeInserter<KeyT, DataT, AggrT, CompareT, TraitsT, AggrCalcT>::giveLeafEntries(LeafNodeType *sNode, Iterator &itr, AggrCalcT aggrCalc) +BTreeInserter<KeyT, DataT, AggrT, CompareT, TraitsT, AggrCalcT>::rebalanceLeafEntries(LeafNodeType *leafNode, Iterator &itr, AggrCalcT aggrCalc) { NodeAllocatorType &allocator(itr.getAllocator()); - typename Iterator::PathElement &pe = itr.getPath(0); - InternalNodeType *pNode = pe.getWNode(); - uint32_t idx = pe.getIdx(); - BTreeNode::Ref sNodeRef = pNode->getChild(idx); + auto &pathElem = itr.getPath(0); + InternalNodeType *parentNode = pathElem.getWNode(); + uint32_t parentIdx = pathElem.getIdx(); + BTreeNode::Ref leafRef = parentNode->getChild(parentIdx); BTreeNode::Ref leftRef = BTreeNode::Ref(); - LeafNodeType * leftNode = nullptr; + LeafNodeType *leftNode = nullptr; BTreeNode::Ref rightRef = BTreeNode::Ref(); - LeafNodeType * rightNode = nullptr; - if (idx > 0) { - leftRef = pNode->getChild(idx - 1); + LeafNodeType *rightNode = nullptr; + if (parentIdx > 0) { + leftRef = parentNode->getChild(parentIdx - 1); leftNode = allocator.template mapLeafRef(leftRef); } - if (idx + 1 < pNode->validSlots()) { - rightRef = pNode->getChild(idx + 1); + if (parentIdx + 1 < parentNode->validSlots()) { + rightRef = parentNode->getChild(parentIdx + 1); rightNode = allocator.template mapLeafRef(rightRef); } if (leftNode != nullptr && leftNode->validSlots() < LeafNodeType::maxSlots() && (rightNode == nullptr || leftNode->validSlots() < rightNode->validSlots())) { - if (leftNode->getFrozen()) { - LeafNodeTypeRefPair thawed = - allocator.thawNode(leftRef, leftNode); - leftRef = thawed.ref; - leftNode = thawed.data; - } + considerThawNode(leftNode, leftRef, allocator); uint32_t oldLeftValid = leftNode->validSlots(); if (itr.getLeafNodeIdx() == 0 && (oldLeftValid + 1 == LeafNodeType::maxSlots())) { - pNode->update(idx - 1, leftNode->getLastKey(), leftRef); + parentNode->update(parentIdx - 1, leftNode->getLastKey(), leftRef); itr.adjustGivenNoEntriesToLeftLeafNode(); } else { - leftNode->stealSomeFromRightNode(sNode, allocator); + leftNode->stealSomeFromRightNode(leafNode, allocator); uint32_t given = leftNode->validSlots() - oldLeftValid; - pNode->update(idx, sNode->getLastKey(), sNodeRef); - pNode->update(idx - 1, leftNode->getLastKey(), leftRef); + parentNode->update(parentIdx, leafNode->getLastKey(), leafRef); + parentNode->update(parentIdx - 1, leftNode->getLastKey(), leftRef); if (AggrCalcT::hasAggregated()) { Aggregator::recalc(*leftNode, allocator, aggrCalc); - Aggregator::recalc(*sNode, allocator, aggrCalc); + Aggregator::recalc(*leafNode, allocator, aggrCalc); } itr.adjustGivenEntriesToLeftLeafNode(given); } } else if (rightNode != nullptr && rightNode->validSlots() < LeafNodeType::maxSlots()) { - if (rightNode->getFrozen()) { - LeafNodeTypeRefPair thawed = - allocator.thawNode(rightRef, rightNode); - rightRef = thawed.ref; - rightNode = thawed.data; - } - rightNode->stealSomeFromLeftNode(sNode, allocator); - pNode->update(idx, sNode->getLastKey(), sNodeRef); - pNode->update(idx + 1, rightNode->getLastKey(), rightRef); + considerThawNode(rightNode, rightRef, allocator); + rightNode->stealSomeFromLeftNode(leafNode, allocator); + parentNode->update(parentIdx, leafNode->getLastKey(), leafRef); + parentNode->update(parentIdx + 1, rightNode->getLastKey(), rightRef); if (AggrCalcT::hasAggregated()) { Aggregator::recalc(*rightNode, allocator, aggrCalc); - Aggregator::recalc(*sNode, allocator, aggrCalc); + Aggregator::recalc(*leafNode, allocator, aggrCalc); } itr.adjustGivenEntriesToRightLeafNode(); } @@ -92,9 +97,9 @@ insert(BTreeNode::Ref &root, --itr; } root = itr.thaw(root); - LeafNodeType * lnode = itr.getLeafNode(); + LeafNodeType *lnode = itr.getLeafNode(); if (lnode->isFull() && itr.getPathSize() > 0) { - giveLeafEntries(lnode, itr, aggrCalc); + rebalanceLeafEntries(lnode, itr, aggrCalc); lnode = itr.getLeafNode(); } uint32_t idx = itr.getLeafNodeIdx() + (inRange ? 0 : 1); diff --git a/searchlib/src/vespa/searchlib/btree/btreeiterator.hpp b/searchlib/src/vespa/searchlib/btree/btreeiterator.hpp index fca22d3e797..741121aebab 100644 --- a/searchlib/src/vespa/searchlib/btree/btreeiterator.hpp +++ b/searchlib/src/vespa/searchlib/btree/btreeiterator.hpp @@ -1329,30 +1329,30 @@ template <typename KeyT, typename DataT, typename AggrT, typename CompareT, type void BTreeIterator<KeyT, DataT, AggrT, CompareT, TraitsT>::adjustGivenNoEntriesToLeftLeafNode() { - auto &pe = _path[0]; - uint32_t pidx = pe.getIdx() - 1; - BTreeNode::Ref sRef = pe.getNode()->getChild(pidx); - const LeafNodeType *sNode = _allocator->mapLeafRef(sRef); - pe.setIdx(pidx); - _leaf.setNodeAndIdx(sNode, sNode->validSlots()); + auto &pathElem = _path[0]; + uint32_t parentIdx = pathElem.getIdx() - 1; + BTreeNode::Ref leafRef = pathElem.getNode()->getChild(parentIdx); + const LeafNodeType *leafNode = _allocator->mapLeafRef(leafRef); + pathElem.setIdx(parentIdx); + _leaf.setNodeAndIdx(leafNode, leafNode->validSlots()); } template <typename KeyT, typename DataT, typename AggrT, typename CompareT, typename TraitsT> void BTreeIterator<KeyT, DataT, AggrT, CompareT, TraitsT>::adjustGivenEntriesToLeftLeafNode(uint32_t given) { - uint32_t idx = _leaf.getIdx(); - if (idx >= given) { - _leaf.setIdx(idx - given); + uint32_t leafIdx = _leaf.getIdx(); + if (leafIdx >= given) { + _leaf.setIdx(leafIdx - given); } else { - auto &pe = _path[0]; - uint32_t pidx = pe.getIdx() - 1; - BTreeNode::Ref sRef = pe.getNode()->getChild(pidx); - const LeafNodeType *sNode = _allocator->mapLeafRef(sRef); - idx += sNode->validSlots(); - assert(given <= idx); - pe.setIdx(pidx); - _leaf.setNodeAndIdx(sNode, idx - given); + auto &pathElem = _path[0]; + uint32_t parentIdx = pathElem.getIdx() - 1; + BTreeNode::Ref leafRef = pathElem.getNode()->getChild(parentIdx); + const LeafNodeType *leafNode = _allocator->mapLeafRef(leafRef); + leafIdx += leafNode->validSlots(); + assert(given <= leafIdx); + pathElem.setIdx(parentIdx); + _leaf.setNodeAndIdx(leafNode, leafIdx - given); } } @@ -1360,18 +1360,18 @@ template <typename KeyT, typename DataT, typename AggrT, typename CompareT, type void BTreeIterator<KeyT, DataT, AggrT, CompareT, TraitsT>::adjustGivenEntriesToRightLeafNode() { - uint32_t idx = _leaf.getIdx(); - const LeafNodeType *sNode = _leaf.getNode(); - if (idx > sNode->validSlots()) { - auto &pe = _path[0]; - const InternalNodeType *pNode = pe.getNode(); - uint32_t pidx = pe.getIdx() + 1; - idx -= sNode->validSlots(); - BTreeNode::Ref sRef = pNode->getChild(pidx); - sNode = _allocator->mapLeafRef(sRef); - assert(idx <= sNode->validSlots()); - pe.setIdx(pidx); - _leaf.setNodeAndIdx(sNode, idx); + uint32_t leafIdx = _leaf.getIdx(); + const LeafNodeType *leafNode = _leaf.getNode(); + if (leafIdx > leafNode->validSlots()) { + auto &pathElem = _path[0]; + const InternalNodeType *parentNode = pathElem.getNode(); + uint32_t parentIdx = pathElem.getIdx() + 1; + leafIdx -= leafNode->validSlots(); + BTreeNode::Ref leafRef = parentNode->getChild(parentIdx); + leafNode = _allocator->mapLeafRef(leafRef); + assert(leafIdx <= leafNode->validSlots()); + pathElem.setIdx(parentIdx); + _leaf.setNodeAndIdx(leafNode, leafIdx); } } |