Created
May 2, 2013 15:02
-
-
Save renctan/5502835 to your computer and use it in GitHub Desktop.
Proof of concept for generalized chunk range squashing. Note: T compiles for both ChunkType (needs to define specialized rangeIsNewer) and TagsType.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace mongo { | |
template <typename T> | |
bool rangeIsNewer(const T* lhs, const T* rhs); | |
/** | |
* Given a list of ranges (note: must be sorted by min), outputs the operations needed | |
* to squash the the overlapping ranges such that the list of ranges will not overlap | |
* one another. | |
* | |
* The caller owns the pointer to both the input and output vectors. The method guarantees | |
* that none of the vectors will share the same pointer. | |
*/ | |
template <typename T> | |
void squash(const std::vector<T*>& ranges, | |
std::vector<T*>* toDelete, | |
std::vector<T*>* toInsert) { | |
const T* rangeBefore = *ranges.begin(); | |
for (typename std::vector<T*>::const_iterator it = ranges.begin() + 1; | |
it != ranges.end(); ++it) { | |
const T* rangeNow = *it; | |
if (rangeBefore->getMin().woCompare(rangeNow->getMin()) == 0) { | |
if (rangeIsNewer(rangeBefore, rangeNow)) { | |
T* newRange = new T; | |
rangeNow->cloneTo(newRange); | |
toDelete->push_back(newRange); | |
} | |
else { | |
T* newRange = new T; | |
rangeBefore->cloneTo(newRange); | |
toDelete->push_back(newRange); | |
} | |
} | |
else if (rangeBefore->getMax().woCompare(rangeNow->getMin()) > 0) { | |
do { | |
T* newRange = new T; | |
rangeNow->cloneTo(newRange); | |
toDelete->push_back(newRange); | |
++it; | |
} while (it != ranges.end() && | |
rangeBefore->getMax().woCompare(rangeNow->getMin()) > 0); | |
T* newRange = new T; | |
rangeNow->cloneTo(newRange); | |
newRange->setMin(rangeBefore->getMax()); | |
toInsert->push_back(newRange); | |
} | |
rangeBefore = *it; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That could work as well. The motivation for using the TagsType/ChunkType instead of an intermediate TaggedRange class is it needs to have the superset of the information both the class have in order not to lose information when converting ranges. I do agree that it's a pain to manage the memory and would like to avoid to manually do it as much as possible.