2019-02-21 18:46:30 +08:00
|
|
|
/*
|
2019-09-02 14:03:31 +08:00
|
|
|
* DeltaTree.h
|
2019-02-21 18:46:30 +08:00
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "flow/flow.h"
|
|
|
|
#include "flow/Arena.h"
|
|
|
|
#include "fdbclient/FDBTypes.h"
|
|
|
|
#include "fdbserver/Knobs.h"
|
|
|
|
#include <string.h>
|
|
|
|
|
2021-06-10 12:18:58 +08:00
|
|
|
#define DELTATREE_DEBUG 0
|
|
|
|
|
|
|
|
#if DELTATREE_DEBUG
|
|
|
|
#define deltatree_printf(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
2021-04-19 18:12:30 +08:00
|
|
|
#define deltatree_printf(...)
|
2021-06-10 12:18:58 +08:00
|
|
|
#endif
|
2021-04-19 14:58:24 +08:00
|
|
|
|
2019-11-11 16:46:05 +08:00
|
|
|
typedef uint64_t Word;
|
2020-04-01 19:24:40 +08:00
|
|
|
// Get the number of prefix bytes that are the same between a and b, up to their common length of cl
|
2019-11-11 16:46:05 +08:00
|
|
|
static inline int commonPrefixLength(uint8_t const* ap, uint8_t const* bp, int cl) {
|
|
|
|
int i = 0;
|
|
|
|
const int wordEnd = cl - sizeof(Word) + 1;
|
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
for (; i < wordEnd; i += sizeof(Word)) {
|
|
|
|
Word a = *(Word*)ap;
|
|
|
|
Word b = *(Word*)bp;
|
|
|
|
if (a != b) {
|
2019-11-11 16:46:05 +08:00
|
|
|
return i + ctzll(a ^ b) / 8;
|
|
|
|
}
|
|
|
|
ap += sizeof(Word);
|
|
|
|
bp += sizeof(Word);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; i < cl; i++) {
|
|
|
|
if (*ap != *bp) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
++ap;
|
|
|
|
++bp;
|
|
|
|
}
|
|
|
|
return cl;
|
|
|
|
}
|
|
|
|
|
2020-05-10 19:24:11 +08:00
|
|
|
static inline int commonPrefixLength(const StringRef& a, const StringRef& b) {
|
2019-11-11 16:46:05 +08:00
|
|
|
return commonPrefixLength(a.begin(), b.begin(), std::min(a.size(), b.size()));
|
|
|
|
}
|
|
|
|
|
2020-05-10 19:24:11 +08:00
|
|
|
static inline int commonPrefixLength(const StringRef& a, const StringRef& b, int skipLen) {
|
|
|
|
return commonPrefixLength(a.begin() + skipLen, b.begin() + skipLen, std::min(a.size(), b.size()) - skipLen);
|
|
|
|
}
|
|
|
|
|
2019-11-11 16:46:05 +08:00
|
|
|
// This appears to be the fastest version
|
|
|
|
static int lessOrEqualPowerOfTwo(int n) {
|
|
|
|
int p;
|
2020-04-25 05:12:40 +08:00
|
|
|
for (p = 1; p + p <= n; p += p)
|
|
|
|
;
|
2019-11-11 16:46:05 +08:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
static int _lessOrEqualPowerOfTwo(uint32_t n) {
|
2020-04-25 05:12:40 +08:00
|
|
|
if(n == 0)
|
|
|
|
return n;
|
|
|
|
int trailing = __builtin_ctz(n);
|
|
|
|
int leading = __builtin_clz(n);
|
|
|
|
if(trailing + leading == ((sizeof(n) * 8) - 1))
|
|
|
|
return n;
|
|
|
|
return 1 << ( (sizeof(n) * 8) - leading - 1);
|
2019-11-11 16:46:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int __lessOrEqualPowerOfTwo(unsigned int n) {
|
2020-04-25 05:12:40 +08:00
|
|
|
int p = 1;
|
|
|
|
for(; p <= n; p <<= 1);
|
|
|
|
return p >> 1;
|
2019-11-11 16:46:05 +08:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2021-10-29 03:42:24 +08:00
|
|
|
static inline int perfectSubtreeSplitPoint(int subtree_size) {
|
2019-11-11 16:46:05 +08:00
|
|
|
// return the inorder index of the root node in a subtree of the given size
|
2020-04-25 05:12:40 +08:00
|
|
|
// consistent with the resulting binary search tree being "perfect" (having minimal height
|
2019-11-11 16:46:05 +08:00
|
|
|
// and all missing nodes as far right as possible).
|
|
|
|
// There has to be a simpler way to do this.
|
|
|
|
int s = lessOrEqualPowerOfTwo((subtree_size - 1) / 2 + 1) - 1;
|
|
|
|
return std::min(s * 2 + 1, subtree_size - s - 1);
|
|
|
|
}
|
|
|
|
|
2021-10-29 03:42:24 +08:00
|
|
|
static inline int perfectSubtreeSplitPointCached(int subtree_size) {
|
2020-04-25 05:12:40 +08:00
|
|
|
static uint16_t* points = nullptr;
|
2019-11-11 16:46:05 +08:00
|
|
|
static const int max = 500;
|
2020-04-25 05:12:40 +08:00
|
|
|
if (points == nullptr) {
|
2019-11-11 16:46:05 +08:00
|
|
|
points = new uint16_t[max];
|
2021-03-11 02:06:03 +08:00
|
|
|
for (int i = 0; i < max; ++i)
|
|
|
|
points[i] = perfectSubtreeSplitPoint(i);
|
2019-11-11 16:46:05 +08:00
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
if (subtree_size < max)
|
|
|
|
return points[subtree_size];
|
2019-11-11 16:46:05 +08:00
|
|
|
return perfectSubtreeSplitPoint(subtree_size);
|
|
|
|
}
|
|
|
|
|
2019-02-21 18:46:30 +08:00
|
|
|
// Delta Tree is a memory mappable binary tree of T objects such that each node's item is
|
|
|
|
// stored as a Delta which can reproduce the node's T item given the node's greatest
|
|
|
|
// lesser ancestor and the node's least greater ancestor.
|
|
|
|
//
|
|
|
|
// The Delta type is intended to make use of ordered prefix compression and borrow all
|
|
|
|
// available prefix bytes from the ancestor T which shares the most prefix bytes with
|
|
|
|
// the item T being encoded.
|
|
|
|
//
|
|
|
|
// T requirements
|
|
|
|
//
|
|
|
|
// Must be compatible with Standalone<T> and must implement the following additional methods:
|
|
|
|
//
|
2019-05-29 21:23:32 +08:00
|
|
|
// // Writes to d a delta which can create *this from base
|
|
|
|
// // commonPrefix can be passed in if known
|
|
|
|
// void writeDelta(dT &d, const T &base, int commonPrefix = -1) const;
|
2019-02-21 18:46:30 +08:00
|
|
|
//
|
|
|
|
// // Compare *this to t, returns < 0 for less than, 0 for equal, > 0 for greater than
|
2020-01-31 16:32:48 +08:00
|
|
|
// // The first skipLen bytes can be assumed to be equal
|
|
|
|
// int compare(const T &rhs, int skipLen) const;
|
2019-02-21 18:46:30 +08:00
|
|
|
//
|
2019-05-29 21:23:32 +08:00
|
|
|
// // Get the common prefix bytes between *this and base
|
|
|
|
// // skip is a hint of how many prefix bytes are already known to be the same
|
|
|
|
// int getCommonPrefixLen(const T &base, int skip) const;
|
|
|
|
//
|
|
|
|
// // Returns the size of the delta object needed to make *this from base
|
2020-04-25 05:12:40 +08:00
|
|
|
// // TODO: Explain contract required for deltaSize to be used to predict final
|
2019-02-21 18:46:30 +08:00
|
|
|
// // balanced tree size incrementally while adding sorted items to a build set
|
2019-05-29 21:23:32 +08:00
|
|
|
// int deltaSize(const T &base) const;
|
2019-02-21 18:46:30 +08:00
|
|
|
//
|
|
|
|
// DeltaT requirements
|
|
|
|
//
|
|
|
|
// // Returns the size of this dT instance
|
|
|
|
// int size();
|
|
|
|
//
|
|
|
|
// // Returns the T created by applying the delta to prev or next
|
2019-05-29 21:23:32 +08:00
|
|
|
// T apply(const T &base, Arena &localStorage) const;
|
|
|
|
//
|
|
|
|
// // Stores a boolean which DeltaTree will later use to determine the base node for a node's delta
|
|
|
|
// void setPrefixSource(bool val);
|
|
|
|
//
|
|
|
|
// // Retrieves the previously stored boolean
|
|
|
|
// bool getPrefixSource() const;
|
2019-02-21 18:46:30 +08:00
|
|
|
//
|
2020-04-25 05:12:40 +08:00
|
|
|
#pragma pack(push, 1)
|
2020-04-01 19:24:40 +08:00
|
|
|
template <typename T, typename DeltaT = typename T::Delta>
|
2019-02-21 18:46:30 +08:00
|
|
|
struct DeltaTree {
|
|
|
|
struct Node {
|
2020-04-01 19:24:40 +08:00
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
uint32_t left;
|
|
|
|
uint32_t right;
|
|
|
|
} largeOffsets;
|
|
|
|
struct {
|
|
|
|
uint16_t left;
|
|
|
|
uint16_t right;
|
|
|
|
} smallOffsets;
|
|
|
|
};
|
2020-04-25 05:12:40 +08:00
|
|
|
|
|
|
|
static int headerSize(bool large) { return large ? sizeof(largeOffsets) : sizeof(smallOffsets); }
|
|
|
|
|
|
|
|
inline DeltaT& delta(bool large) {
|
|
|
|
return large ? *(DeltaT*)(&largeOffsets + 1) : *(DeltaT*)(&smallOffsets + 1);
|
2019-07-02 15:58:43 +08:00
|
|
|
};
|
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
inline const DeltaT& delta(bool large) const {
|
|
|
|
return large ? *(const DeltaT*)(&largeOffsets + 1) : *(const DeltaT*)(&smallOffsets + 1);
|
2019-07-02 15:58:43 +08:00
|
|
|
};
|
2019-02-21 18:46:30 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
Node* resolvePointer(int offset) const { return offset == 0 ? nullptr : (Node*)((uint8_t*)this + offset); }
|
2019-02-21 18:46:30 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
Node* rightChild(bool large) const { return resolvePointer(large ? largeOffsets.right : smallOffsets.right); }
|
2019-02-21 18:46:30 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
Node* leftChild(bool large) const { return resolvePointer(large ? largeOffsets.left : smallOffsets.left); }
|
2020-04-01 19:24:40 +08:00
|
|
|
|
|
|
|
void setRightChildOffset(bool large, int offset) {
|
2020-04-25 05:12:40 +08:00
|
|
|
if (large) {
|
2020-04-01 19:24:40 +08:00
|
|
|
largeOffsets.right = offset;
|
2020-04-25 05:12:40 +08:00
|
|
|
} else {
|
2020-04-01 19:24:40 +08:00
|
|
|
smallOffsets.right = offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setLeftChildOffset(bool large, int offset) {
|
2020-04-25 05:12:40 +08:00
|
|
|
if (large) {
|
2020-04-01 19:24:40 +08:00
|
|
|
largeOffsets.left = offset;
|
2020-04-25 05:12:40 +08:00
|
|
|
} else {
|
2020-04-01 19:24:40 +08:00
|
|
|
smallOffsets.left = offset;
|
|
|
|
}
|
|
|
|
}
|
2019-02-21 18:46:30 +08:00
|
|
|
};
|
|
|
|
|
2020-04-07 11:27:30 +08:00
|
|
|
static constexpr int SmallSizeLimit = std::numeric_limits<uint16_t>::max();
|
|
|
|
static constexpr int LargeTreePerNodeExtraOverhead = sizeof(Node::largeOffsets) - sizeof(Node::smallOffsets);
|
|
|
|
|
2019-02-21 18:46:30 +08:00
|
|
|
struct {
|
2020-04-25 05:12:40 +08:00
|
|
|
uint16_t numItems; // Number of items in the tree.
|
|
|
|
uint32_t nodeBytesUsed; // Bytes used by nodes (everything after the tree header)
|
|
|
|
uint32_t nodeBytesFree; // Bytes left at end of tree to expand into
|
|
|
|
uint32_t nodeBytesDeleted; // Delta bytes deleted from tree. Note that some of these bytes could be borrowed by
|
|
|
|
// descendents.
|
|
|
|
uint8_t initialHeight; // Height of tree as originally built
|
|
|
|
uint8_t maxHeight; // Maximum height of tree after any insertion. Value of 0 means no insertions done.
|
|
|
|
bool largeNodes; // Node size, can be calculated as capacity > SmallSizeLimit but it will be used a lot
|
2019-02-21 18:46:30 +08:00
|
|
|
};
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
inline Node& root() { return *(Node*)(this + 1); }
|
2019-07-02 15:58:43 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
inline const Node& root() const { return *(const Node*)(this + 1); }
|
2019-07-02 15:58:43 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
int size() const { return sizeof(DeltaTree) + nodeBytesUsed; }
|
2019-02-21 18:46:30 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
int capacity() const { return size() + nodeBytesFree; }
|
2020-04-01 19:24:40 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
inline Node& newNode() { return *(Node*)((uint8_t*)this + size()); }
|
2019-11-23 16:09:11 +08:00
|
|
|
|
2019-02-21 18:46:30 +08:00
|
|
|
public:
|
|
|
|
struct DecodedNode {
|
2020-02-06 07:30:04 +08:00
|
|
|
DecodedNode() {}
|
|
|
|
|
2020-01-31 16:32:48 +08:00
|
|
|
// construct root node
|
2020-04-25 05:12:40 +08:00
|
|
|
DecodedNode(Node* raw, const T* prev, const T* next, Arena& arena, bool large)
|
2021-07-23 13:48:27 +08:00
|
|
|
: large(large), raw(raw), parent(nullptr), otherAncestor(nullptr), leftChild(nullptr), rightChild(nullptr),
|
|
|
|
prev(prev), next(next),
|
|
|
|
item(raw->delta(large).apply(raw->delta(large).getPrefixSource() ? *prev : *next, arena)) {
|
2020-04-25 05:12:40 +08:00
|
|
|
// printf("DecodedNode1 raw=%p delta=%s\n", raw, raw->delta(large).toString().c_str());
|
|
|
|
}
|
|
|
|
|
2020-01-31 16:32:48 +08:00
|
|
|
// Construct non-root node
|
2020-04-25 05:12:40 +08:00
|
|
|
// wentLeft indicates that we've gone left to get to the raw node.
|
|
|
|
DecodedNode(Node* raw, DecodedNode* parent, bool wentLeft, Arena& arena)
|
2021-07-23 13:48:27 +08:00
|
|
|
: large(parent->large), raw(raw), parent(parent),
|
|
|
|
otherAncestor(wentLeft ? parent->getPrevAncestor() : parent->getNextAncestor()), leftChild(nullptr),
|
|
|
|
rightChild(nullptr), prev(wentLeft ? parent->prev : &parent->item),
|
|
|
|
next(wentLeft ? &parent->item : parent->next),
|
2020-04-25 05:12:40 +08:00
|
|
|
item(raw->delta(large).apply(raw->delta(large).getPrefixSource() ? *prev : *next, arena)) {
|
|
|
|
// printf("DecodedNode2 raw=%p delta=%s\n", raw, raw->delta(large).toString().c_str());
|
2019-02-21 18:46:30 +08:00
|
|
|
}
|
|
|
|
|
2020-01-31 16:32:48 +08:00
|
|
|
// Returns true if otherAncestor is the previous ("greatest lesser") ancestor
|
2020-04-25 05:12:40 +08:00
|
|
|
bool otherAncestorPrev() const { return parent && parent->leftChild == this; }
|
2020-01-31 16:32:48 +08:00
|
|
|
|
|
|
|
// Returns true if otherAncestor is the next ("least greator") ancestor
|
2020-04-25 05:12:40 +08:00
|
|
|
bool otherAncestorNext() const { return parent && parent->rightChild == this; }
|
2020-01-31 16:32:48 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
DecodedNode* getPrevAncestor() const { return otherAncestorPrev() ? otherAncestor : parent; }
|
2020-01-31 16:32:48 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
DecodedNode* getNextAncestor() const { return otherAncestorNext() ? otherAncestor : parent; }
|
2020-01-31 16:32:48 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
DecodedNode* jumpUpNext(DecodedNode* root, bool& othersChild) const {
|
|
|
|
if (parent != nullptr) {
|
|
|
|
if (parent->rightChild == this) {
|
2020-04-07 11:27:30 +08:00
|
|
|
return otherAncestor;
|
|
|
|
}
|
2020-04-25 05:12:40 +08:00
|
|
|
if (otherAncestor != nullptr) {
|
2020-04-07 11:27:30 +08:00
|
|
|
othersChild = true;
|
|
|
|
return otherAncestor->rightChild;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
DecodedNode* jumpUpPrev(DecodedNode* root, bool& othersChild) const {
|
|
|
|
if (parent != nullptr) {
|
|
|
|
if (parent->leftChild == this) {
|
2020-04-07 11:27:30 +08:00
|
|
|
return otherAncestor;
|
|
|
|
}
|
2020-04-25 05:12:40 +08:00
|
|
|
if (otherAncestor != nullptr) {
|
2020-04-07 11:27:30 +08:00
|
|
|
othersChild = true;
|
|
|
|
return otherAncestor->leftChild;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
DecodedNode* jumpNext(DecodedNode* root) const {
|
|
|
|
if (otherAncestorNext()) {
|
2020-01-31 16:32:48 +08:00
|
|
|
return (otherAncestor != nullptr) ? otherAncestor : rightChild;
|
2020-04-25 05:12:40 +08:00
|
|
|
} else {
|
|
|
|
if (this == root) {
|
2020-01-31 16:32:48 +08:00
|
|
|
return rightChild;
|
|
|
|
}
|
|
|
|
return (otherAncestor != nullptr) ? otherAncestor->rightChild : root;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
DecodedNode* jumpPrev(DecodedNode* root) const {
|
|
|
|
if (otherAncestorPrev()) {
|
2020-01-31 16:32:48 +08:00
|
|
|
return (otherAncestor != nullptr) ? otherAncestor : leftChild;
|
2020-04-25 05:12:40 +08:00
|
|
|
} else {
|
|
|
|
if (this == root) {
|
2020-01-31 16:32:48 +08:00
|
|
|
return leftChild;
|
|
|
|
}
|
|
|
|
return (otherAncestor != nullptr) ? otherAncestor->leftChild : root;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
void setDeleted(bool deleted) { raw->delta(large).setDeleted(deleted); }
|
2020-02-04 17:22:27 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
bool isDeleted() const { return raw->delta(large).getDeleted(); }
|
2020-02-04 17:22:27 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
bool large; // Node size
|
|
|
|
Node* raw;
|
|
|
|
DecodedNode* parent;
|
|
|
|
DecodedNode* otherAncestor;
|
|
|
|
DecodedNode* leftChild;
|
|
|
|
DecodedNode* rightChild;
|
|
|
|
const T* prev; // greatest ancestor to the left, or tree lower bound
|
|
|
|
const T* next; // least ancestor to the right, or tree upper bound
|
2019-02-21 18:46:30 +08:00
|
|
|
T item;
|
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
DecodedNode* getRightChild(Arena& arena) {
|
|
|
|
if (rightChild == nullptr) {
|
|
|
|
Node* n = raw->rightChild(large);
|
|
|
|
if (n != nullptr) {
|
2020-01-31 16:32:48 +08:00
|
|
|
rightChild = new (arena) DecodedNode(n, this, false, arena);
|
2019-02-21 18:46:30 +08:00
|
|
|
}
|
|
|
|
}
|
2020-01-31 16:32:48 +08:00
|
|
|
return rightChild;
|
2019-02-21 18:46:30 +08:00
|
|
|
}
|
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
DecodedNode* getLeftChild(Arena& arena) {
|
|
|
|
if (leftChild == nullptr) {
|
|
|
|
Node* n = raw->leftChild(large);
|
|
|
|
if (n != nullptr) {
|
2020-01-31 16:32:48 +08:00
|
|
|
leftChild = new (arena) DecodedNode(n, this, true, arena);
|
2019-02-21 18:46:30 +08:00
|
|
|
}
|
|
|
|
}
|
2020-01-31 16:32:48 +08:00
|
|
|
return leftChild;
|
2019-02-21 18:46:30 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-29 05:16:14 +08:00
|
|
|
class Cursor;
|
2019-02-21 18:46:30 +08:00
|
|
|
|
2019-11-23 16:09:11 +08:00
|
|
|
// A Mirror is an accessor for a DeltaTree which allows insertion and reading. Both operations are done
|
|
|
|
// using cursors which point to and share nodes in an tree that is built on-demand and mirrors the compressed
|
|
|
|
// structure but with fully reconstituted items (which reference DeltaTree bytes or Arena bytes, based
|
|
|
|
// on the behavior of T::Delta::apply())
|
|
|
|
struct Mirror : FastAllocated<Mirror> {
|
2020-02-22 08:26:44 +08:00
|
|
|
friend class Cursor;
|
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
Mirror(const void* treePtr = nullptr, const T* lowerBound = nullptr, const T* upperBound = nullptr)
|
|
|
|
: tree((DeltaTree*)treePtr), lower(lowerBound), upper(upperBound) {
|
|
|
|
lower = new (arena) T(arena, *lower);
|
|
|
|
upper = new (arena) T(arena, *upper);
|
2019-02-21 18:46:30 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
root = (tree->nodeBytesUsed == 0) ? nullptr
|
|
|
|
: new (arena)
|
|
|
|
DecodedNode(&tree->root(), lower, upper, arena, tree->largeNodes);
|
2019-02-21 18:46:30 +08:00
|
|
|
}
|
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
const T* lowerBound() const { return lower; }
|
2019-02-21 18:46:30 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
const T* upperBound() const { return upper; }
|
2019-02-21 18:46:30 +08:00
|
|
|
|
2020-05-08 22:11:51 +08:00
|
|
|
DeltaTree* tree;
|
2021-05-06 11:55:48 +08:00
|
|
|
Arena arena;
|
2020-05-08 22:11:51 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
private:
|
|
|
|
DecodedNode* root;
|
|
|
|
const T* lower;
|
|
|
|
const T* upper;
|
2019-02-21 18:46:30 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
public:
|
|
|
|
Cursor getCursor() { return Cursor(this); }
|
2019-11-23 16:09:11 +08:00
|
|
|
|
2020-02-07 10:17:09 +08:00
|
|
|
// Try to insert k into the DeltaTree, updating byte counts and initialHeight if they
|
|
|
|
// have changed (they won't if k already exists in the tree but was deleted).
|
|
|
|
// Returns true if successful, false if k does not fit in the space available
|
|
|
|
// or if k is already in the tree (and was not already deleted).
|
2021-03-09 09:40:59 +08:00
|
|
|
// Insertion on an empty tree returns false as well.
|
2020-04-25 05:12:40 +08:00
|
|
|
bool insert(const T& k, int skipLen = 0, int maxHeightAllowed = std::numeric_limits<int>::max()) {
|
2021-03-09 09:40:59 +08:00
|
|
|
if (root == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-23 16:09:11 +08:00
|
|
|
int height = 1;
|
2020-04-25 05:12:40 +08:00
|
|
|
DecodedNode* n = root;
|
2020-02-06 07:30:04 +08:00
|
|
|
bool addLeftChild = false;
|
2019-11-23 16:09:11 +08:00
|
|
|
|
2021-03-09 02:50:28 +08:00
|
|
|
while (true) {
|
2020-01-31 16:32:48 +08:00
|
|
|
int cmp = k.compare(n->item, skipLen);
|
2019-11-23 16:09:11 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
if (cmp >= 0) {
|
2020-02-07 10:17:09 +08:00
|
|
|
// If we found an item identical to k then if it is deleted, undeleted it,
|
|
|
|
// otherwise fail
|
2020-04-25 05:12:40 +08:00
|
|
|
if (cmp == 0) {
|
|
|
|
auto& d = n->raw->delta(tree->largeNodes);
|
|
|
|
if (d.getDeleted()) {
|
2020-02-07 10:17:09 +08:00
|
|
|
d.setDeleted(false);
|
2020-02-21 14:20:27 +08:00
|
|
|
++tree->numItems;
|
2020-02-07 10:17:09 +08:00
|
|
|
return true;
|
2020-04-25 05:12:40 +08:00
|
|
|
} else {
|
2020-02-07 10:17:09 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
DecodedNode* right = n->getRightChild(arena);
|
2019-11-23 16:09:11 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
if (right == nullptr) {
|
2020-02-06 07:30:04 +08:00
|
|
|
break;
|
2019-11-23 16:09:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
n = right;
|
2020-04-25 05:12:40 +08:00
|
|
|
} else {
|
|
|
|
DecodedNode* left = n->getLeftChild(arena);
|
2019-11-23 16:09:11 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
if (left == nullptr) {
|
2020-02-06 07:30:04 +08:00
|
|
|
addLeftChild = true;
|
|
|
|
break;
|
2019-11-23 16:09:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
n = left;
|
|
|
|
}
|
|
|
|
++height;
|
|
|
|
}
|
2020-02-06 07:30:04 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
if (height > maxHeightAllowed) {
|
2020-02-22 08:26:44 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-06 07:30:04 +08:00
|
|
|
// Insert k as the left or right child of n, depending on the value of addLeftChild
|
|
|
|
// First, see if it will fit.
|
2020-04-25 05:12:40 +08:00
|
|
|
const T* prev = addLeftChild ? n->prev : &n->item;
|
|
|
|
const T* next = addLeftChild ? &n->item : n->next;
|
2020-02-06 07:30:04 +08:00
|
|
|
|
|
|
|
int common = prev->getCommonPrefixLen(*next, skipLen);
|
|
|
|
int commonWithPrev = k.getCommonPrefixLen(*prev, common);
|
|
|
|
int commonWithNext = k.getCommonPrefixLen(*next, common);
|
|
|
|
bool basePrev = commonWithPrev >= commonWithNext;
|
|
|
|
|
|
|
|
int commonPrefix = basePrev ? commonWithPrev : commonWithNext;
|
2020-04-25 05:12:40 +08:00
|
|
|
const T* base = basePrev ? prev : next;
|
2020-02-06 07:30:04 +08:00
|
|
|
|
2020-04-09 11:18:41 +08:00
|
|
|
int deltaSize = k.deltaSize(*base, commonPrefix, false);
|
2020-04-01 19:24:40 +08:00
|
|
|
int nodeSpace = deltaSize + Node::headerSize(tree->largeNodes);
|
2020-04-25 05:12:40 +08:00
|
|
|
if (nodeSpace > tree->nodeBytesFree) {
|
2020-02-06 07:30:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
DecodedNode* newNode = new (arena) DecodedNode();
|
|
|
|
Node* raw = &tree->newNode();
|
2020-04-01 19:24:40 +08:00
|
|
|
raw->setLeftChildOffset(tree->largeNodes, 0);
|
|
|
|
raw->setRightChildOffset(tree->largeNodes, 0);
|
2020-04-25 05:12:40 +08:00
|
|
|
int newOffset = (uint8_t*)raw - (uint8_t*)n->raw;
|
|
|
|
// printf("Inserting %s at offset %d\n", k.toString().c_str(), newOffset);
|
2020-02-06 07:30:04 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
if (addLeftChild) {
|
2020-02-06 07:30:04 +08:00
|
|
|
n->leftChild = newNode;
|
2020-04-01 19:24:40 +08:00
|
|
|
n->raw->setLeftChildOffset(tree->largeNodes, newOffset);
|
2020-04-25 05:12:40 +08:00
|
|
|
} else {
|
2020-02-06 07:30:04 +08:00
|
|
|
n->rightChild = newNode;
|
2020-04-01 19:24:40 +08:00
|
|
|
n->raw->setRightChildOffset(tree->largeNodes, newOffset);
|
2020-02-06 07:30:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
newNode->parent = n;
|
2020-04-01 19:24:40 +08:00
|
|
|
newNode->large = tree->largeNodes;
|
2020-02-06 07:30:04 +08:00
|
|
|
newNode->leftChild = nullptr;
|
|
|
|
newNode->rightChild = nullptr;
|
|
|
|
newNode->raw = raw;
|
|
|
|
newNode->otherAncestor = addLeftChild ? n->getPrevAncestor() : n->getNextAncestor();
|
|
|
|
newNode->prev = prev;
|
|
|
|
newNode->next = next;
|
|
|
|
|
2020-05-08 22:11:51 +08:00
|
|
|
int written = k.writeDelta(raw->delta(tree->largeNodes), *base, commonPrefix);
|
|
|
|
ASSERT(deltaSize == written);
|
2020-04-01 19:24:40 +08:00
|
|
|
raw->delta(tree->largeNodes).setPrefixSource(basePrev);
|
2020-02-06 07:30:04 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
// Initialize node's item from the delta (instead of copying into arena) to avoid unnecessary arena space
|
|
|
|
// usage
|
2020-04-01 19:24:40 +08:00
|
|
|
newNode->item = raw->delta(tree->largeNodes).apply(*base, arena);
|
2020-02-06 07:30:04 +08:00
|
|
|
|
|
|
|
tree->nodeBytesUsed += nodeSpace;
|
|
|
|
tree->nodeBytesFree -= nodeSpace;
|
|
|
|
++tree->numItems;
|
|
|
|
|
|
|
|
// Update max height of the tree if necessary
|
2020-04-25 05:12:40 +08:00
|
|
|
if (height > tree->maxHeight) {
|
2020-02-06 07:30:04 +08:00
|
|
|
tree->maxHeight = height;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2019-11-23 16:09:11 +08:00
|
|
|
}
|
2020-02-07 10:17:09 +08:00
|
|
|
|
|
|
|
// Erase k by setting its deleted flag to true. Returns true only if k existed
|
2020-04-25 05:12:40 +08:00
|
|
|
bool erase(const T& k, int skipLen = 0) {
|
2020-02-07 10:17:09 +08:00
|
|
|
Cursor c = getCursor();
|
2020-04-07 11:27:30 +08:00
|
|
|
int cmp = c.seek(k);
|
|
|
|
// If exactly k is found
|
2020-04-25 05:12:40 +08:00
|
|
|
if (cmp == 0 && !c.node->isDeleted()) {
|
2020-02-07 10:17:09 +08:00
|
|
|
c.erase();
|
2020-04-07 11:27:30 +08:00
|
|
|
return true;
|
2020-02-07 10:17:09 +08:00
|
|
|
}
|
2020-04-07 11:27:30 +08:00
|
|
|
return false;
|
2020-02-07 10:17:09 +08:00
|
|
|
}
|
2019-02-21 18:46:30 +08:00
|
|
|
};
|
|
|
|
|
2019-09-02 14:03:31 +08:00
|
|
|
// Cursor provides a way to seek into a DeltaTree and iterate over its contents
|
2020-01-31 16:32:48 +08:00
|
|
|
// All Cursors from a Mirror share the same decoded node 'cache' (tree of DecodedNodes)
|
2021-10-29 05:16:14 +08:00
|
|
|
class Cursor {
|
|
|
|
public:
|
2020-04-25 05:12:40 +08:00
|
|
|
Cursor() : mirror(nullptr), node(nullptr) {}
|
2019-02-21 18:46:30 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
Cursor(Mirror* r) : mirror(r), node(mirror->root) {}
|
2019-02-21 18:46:30 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
Mirror* mirror;
|
|
|
|
DecodedNode* node;
|
2019-02-21 18:46:30 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
bool valid() const { return node != nullptr; }
|
2019-02-21 18:46:30 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
const T& get() const { return node->item; }
|
2019-02-21 18:46:30 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
const T& getOrUpperBound() const { return valid() ? node->item : *mirror->upperBound(); }
|
2019-02-21 18:46:30 +08:00
|
|
|
|
2021-03-03 05:06:01 +08:00
|
|
|
const T& lowerBound() const { return *mirror->lowerBound(); }
|
|
|
|
|
|
|
|
const T& upperBound() const { return *mirror->upperBound(); }
|
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
bool operator==(const Cursor& rhs) const { return node == rhs.node; }
|
2019-12-02 14:28:50 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
bool operator!=(const Cursor& rhs) const { return node != rhs.node; }
|
2019-12-02 14:28:50 +08:00
|
|
|
|
2020-02-04 17:22:27 +08:00
|
|
|
void erase() {
|
|
|
|
node->setDeleted(true);
|
2020-02-21 14:20:27 +08:00
|
|
|
--mirror->tree->numItems;
|
2020-02-04 17:22:27 +08:00
|
|
|
moveNext();
|
|
|
|
}
|
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
// TODO: Make hint-based seek() use the hint logic in this, which is better and actually improves seek times,
|
|
|
|
// then remove this function.
|
|
|
|
bool seekLessThanOrEqualOld(const T& s, int skipLen, const Cursor* pHint, int initialCmp) {
|
|
|
|
DecodedNode* n;
|
2020-01-31 16:32:48 +08:00
|
|
|
|
|
|
|
// If there's a hint position, use it
|
|
|
|
// At the end of using the hint, if n is valid it should point to a node which has not yet been compared to.
|
2020-04-25 05:12:40 +08:00
|
|
|
if (pHint != nullptr && pHint->node != nullptr) {
|
2020-01-31 16:32:48 +08:00
|
|
|
n = pHint->node;
|
2020-04-25 05:12:40 +08:00
|
|
|
if (initialCmp == 0) {
|
2019-02-21 18:46:30 +08:00
|
|
|
node = n;
|
2020-02-04 17:22:27 +08:00
|
|
|
return _hideDeletedBackward();
|
2019-02-21 18:46:30 +08:00
|
|
|
}
|
2020-04-25 05:12:40 +08:00
|
|
|
if (initialCmp > 0) {
|
2020-01-31 16:32:48 +08:00
|
|
|
node = n;
|
2020-04-25 05:12:40 +08:00
|
|
|
while (n != nullptr) {
|
2020-01-31 16:32:48 +08:00
|
|
|
n = n->jumpNext(mirror->root);
|
2020-04-25 05:12:40 +08:00
|
|
|
if (n == nullptr) {
|
2020-01-31 16:32:48 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
int cmp = s.compare(n->item, skipLen);
|
2020-04-25 05:12:40 +08:00
|
|
|
if (cmp > 0) {
|
2020-01-31 16:32:48 +08:00
|
|
|
node = n;
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-25 05:12:40 +08:00
|
|
|
if (cmp == 0) {
|
2020-01-31 16:32:48 +08:00
|
|
|
node = n;
|
|
|
|
n = nullptr;
|
2020-04-25 05:12:40 +08:00
|
|
|
} else {
|
2020-01-31 16:32:48 +08:00
|
|
|
n = n->leftChild;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-04-25 05:12:40 +08:00
|
|
|
} else {
|
|
|
|
while (n != nullptr) {
|
2020-01-31 16:32:48 +08:00
|
|
|
n = n->jumpPrev(mirror->root);
|
2020-04-25 05:12:40 +08:00
|
|
|
if (n == nullptr) {
|
2020-01-31 16:32:48 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
int cmp = s.compare(n->item, skipLen);
|
2020-04-25 05:12:40 +08:00
|
|
|
if (cmp >= 0) {
|
2020-01-31 16:32:48 +08:00
|
|
|
node = n;
|
|
|
|
n = (cmp == 0) ? nullptr : n->rightChild;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-25 05:12:40 +08:00
|
|
|
} else {
|
2020-01-31 16:32:48 +08:00
|
|
|
// Start at root, clear current position
|
|
|
|
n = mirror->root;
|
|
|
|
node = nullptr;
|
|
|
|
}
|
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
while (n != nullptr) {
|
2020-01-31 16:32:48 +08:00
|
|
|
int cmp = s.compare(n->item, skipLen);
|
2019-02-21 18:46:30 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
if (cmp < 0) {
|
2020-01-31 16:32:48 +08:00
|
|
|
n = n->getLeftChild(mirror->arena);
|
2020-04-25 05:12:40 +08:00
|
|
|
} else {
|
2020-01-31 16:32:48 +08:00
|
|
|
// n <= s so store it in node as a potential result
|
|
|
|
node = n;
|
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
if (cmp == 0) {
|
2020-02-04 17:22:27 +08:00
|
|
|
break;
|
2020-01-31 16:32:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
n = n->getRightChild(mirror->arena);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 17:22:27 +08:00
|
|
|
return _hideDeletedBackward();
|
2020-01-31 16:32:48 +08:00
|
|
|
}
|
|
|
|
|
2020-04-07 11:27:30 +08:00
|
|
|
// The seek methods, of the form seek[Less|Greater][orEqual](...) are very similar.
|
|
|
|
// They attempt move the cursor to the [Greatest|Least] item, based on the name of the function.
|
|
|
|
// Then will not "see" erased records.
|
|
|
|
// If successful, they return true, and if not then false a while making the cursor invalid.
|
|
|
|
// These methods forward arguments to the seek() overloads, see those for argument descriptions.
|
2020-04-25 05:12:40 +08:00
|
|
|
template <typename... Args>
|
2020-04-07 11:27:30 +08:00
|
|
|
bool seekLessThan(Args... args) {
|
|
|
|
int cmp = seek(args...);
|
2020-04-25 05:12:40 +08:00
|
|
|
if (cmp < 0 || (cmp == 0 && node != nullptr)) {
|
2020-04-07 11:27:30 +08:00
|
|
|
movePrev();
|
|
|
|
}
|
|
|
|
return _hideDeletedBackward();
|
|
|
|
}
|
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
template <typename... Args>
|
2020-04-07 11:27:30 +08:00
|
|
|
bool seekLessThanOrEqual(Args... args) {
|
|
|
|
int cmp = seek(args...);
|
2020-04-25 05:12:40 +08:00
|
|
|
if (cmp < 0) {
|
2020-04-07 11:27:30 +08:00
|
|
|
movePrev();
|
|
|
|
}
|
|
|
|
return _hideDeletedBackward();
|
|
|
|
}
|
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
template <typename... Args>
|
2020-04-07 11:27:30 +08:00
|
|
|
bool seekGreaterThan(Args... args) {
|
|
|
|
int cmp = seek(args...);
|
2020-04-25 05:12:40 +08:00
|
|
|
if (cmp > 0 || (cmp == 0 && node != nullptr)) {
|
2020-04-07 11:27:30 +08:00
|
|
|
moveNext();
|
|
|
|
}
|
|
|
|
return _hideDeletedForward();
|
|
|
|
}
|
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
template <typename... Args>
|
2020-04-07 11:27:30 +08:00
|
|
|
bool seekGreaterThanOrEqual(Args... args) {
|
|
|
|
int cmp = seek(args...);
|
2020-04-25 05:12:40 +08:00
|
|
|
if (cmp > 0) {
|
2020-04-07 11:27:30 +08:00
|
|
|
moveNext();
|
|
|
|
}
|
|
|
|
return _hideDeletedForward();
|
|
|
|
}
|
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
// seek() moves the cursor to a node containing s or the node that would be the parent of s if s were to be
|
|
|
|
// added to the tree. If the tree was empty, the cursor will be invalid and the return value will be 0.
|
2020-04-07 11:27:30 +08:00
|
|
|
// Otherwise, returns the result of s.compare(item at cursor position)
|
|
|
|
// Does not skip/avoid deleted nodes.
|
2020-04-25 05:12:40 +08:00
|
|
|
int seek(const T& s, int skipLen = 0) {
|
|
|
|
DecodedNode* n = mirror->root;
|
2020-01-31 16:32:48 +08:00
|
|
|
node = nullptr;
|
2020-04-07 11:27:30 +08:00
|
|
|
int cmp = 0;
|
2020-04-25 05:12:40 +08:00
|
|
|
while (n != nullptr) {
|
2020-04-07 11:27:30 +08:00
|
|
|
node = n;
|
|
|
|
cmp = s.compare(n->item, skipLen);
|
2020-04-25 05:12:40 +08:00
|
|
|
if (cmp == 0) {
|
2020-04-07 11:27:30 +08:00
|
|
|
break;
|
|
|
|
}
|
2020-01-31 16:32:48 +08:00
|
|
|
|
2020-04-07 11:27:30 +08:00
|
|
|
n = (cmp > 0) ? n->getRightChild(mirror->arena) : n->getLeftChild(mirror->arena);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same usage as seek() but with a hint of a cursor, which can't be null, whose starting position
|
|
|
|
// should be close to s in the tree to improve seek time.
|
2020-04-25 05:12:40 +08:00
|
|
|
// initialCmp should be logically equivalent to s.compare(pHint->get()) or 0, in which
|
2020-04-07 11:27:30 +08:00
|
|
|
// case the comparison will be done in this method.
|
2020-04-25 05:12:40 +08:00
|
|
|
// TODO: This is broken, it's not faster than not using a hint. See Make thisUnfortunately in a microbenchmark
|
|
|
|
// attempting to approximate a common use case, this version of using a cursor hint is actually slower than not
|
|
|
|
// using a hint.
|
|
|
|
int seek(const T& s, int skipLen, const Cursor* pHint, int initialCmp = 0) {
|
|
|
|
DecodedNode* n = mirror->root;
|
2020-04-07 11:27:30 +08:00
|
|
|
node = nullptr;
|
|
|
|
int cmp;
|
|
|
|
|
|
|
|
// If there's a hint position, use it
|
|
|
|
// At the end of using the hint, if n is valid it should point to a node which has not yet been compared to.
|
2020-04-25 05:12:40 +08:00
|
|
|
if (pHint->node != nullptr) {
|
2020-04-07 11:27:30 +08:00
|
|
|
n = pHint->node;
|
2020-04-25 05:12:40 +08:00
|
|
|
if (initialCmp == 0) {
|
2020-04-07 11:27:30 +08:00
|
|
|
initialCmp = s.compare(pHint->get());
|
2019-02-21 18:46:30 +08:00
|
|
|
}
|
2020-04-07 11:27:30 +08:00
|
|
|
cmp = initialCmp;
|
2020-01-31 16:32:48 +08:00
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
while (true) {
|
2020-04-07 11:27:30 +08:00
|
|
|
node = n;
|
2020-04-25 05:12:40 +08:00
|
|
|
if (cmp == 0) {
|
2020-04-07 11:27:30 +08:00
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to jump up and past s
|
|
|
|
bool othersChild = false;
|
2020-04-25 05:12:40 +08:00
|
|
|
n = (initialCmp > 0) ? n->jumpUpNext(mirror->root, othersChild)
|
|
|
|
: n->jumpUpPrev(mirror->root, othersChild);
|
|
|
|
if (n == nullptr) {
|
2020-04-07 11:27:30 +08:00
|
|
|
n = (cmp > 0) ? node->rightChild : node->leftChild;
|
2020-02-04 17:22:27 +08:00
|
|
|
break;
|
2020-01-31 16:32:48 +08:00
|
|
|
}
|
|
|
|
|
2020-04-07 11:27:30 +08:00
|
|
|
// Compare s to the node jumped to
|
|
|
|
cmp = s.compare(n->item, skipLen);
|
|
|
|
|
|
|
|
// n is on the oposite side of s than node is, then n is too far.
|
2020-04-25 05:12:40 +08:00
|
|
|
if (cmp != 0 && ((initialCmp ^ cmp) < 0)) {
|
|
|
|
if (!othersChild) {
|
2020-04-07 11:27:30 +08:00
|
|
|
n = (cmp < 0) ? node->rightChild : node->leftChild;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-02-21 18:46:30 +08:00
|
|
|
}
|
2020-04-25 05:12:40 +08:00
|
|
|
} else {
|
2020-04-07 11:27:30 +08:00
|
|
|
// Start at root, clear current position
|
|
|
|
n = mirror->root;
|
|
|
|
node = nullptr;
|
|
|
|
cmp = 0;
|
|
|
|
}
|
2019-02-21 18:46:30 +08:00
|
|
|
|
2020-04-07 11:27:30 +08:00
|
|
|
// Search starting from n, which is either the root or the result of applying the hint
|
2020-04-25 05:12:40 +08:00
|
|
|
while (n != nullptr) {
|
2020-04-07 11:27:30 +08:00
|
|
|
node = n;
|
|
|
|
cmp = s.compare(n->item, skipLen);
|
2020-04-25 05:12:40 +08:00
|
|
|
if (cmp == 0) {
|
2020-04-07 11:27:30 +08:00
|
|
|
break;
|
2020-02-07 10:17:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
n = (cmp > 0) ? n->getRightChild(mirror->arena) : n->getLeftChild(mirror->arena);
|
|
|
|
}
|
|
|
|
|
2020-04-07 11:27:30 +08:00
|
|
|
return cmp;
|
2020-02-07 10:17:09 +08:00
|
|
|
}
|
|
|
|
|
2019-02-21 18:46:30 +08:00
|
|
|
bool moveFirst() {
|
2020-04-25 05:12:40 +08:00
|
|
|
DecodedNode* n = mirror->root;
|
2019-02-21 18:46:30 +08:00
|
|
|
node = n;
|
2020-04-25 05:12:40 +08:00
|
|
|
while (n != nullptr) {
|
2020-01-31 16:32:48 +08:00
|
|
|
n = n->getLeftChild(mirror->arena);
|
2021-03-11 02:06:03 +08:00
|
|
|
if (n != nullptr)
|
|
|
|
node = n;
|
2019-02-21 18:46:30 +08:00
|
|
|
}
|
2020-02-04 17:22:27 +08:00
|
|
|
return _hideDeletedForward();
|
2019-02-21 18:46:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool moveLast() {
|
2020-04-25 05:12:40 +08:00
|
|
|
DecodedNode* n = mirror->root;
|
2019-02-21 18:46:30 +08:00
|
|
|
node = n;
|
2020-04-25 05:12:40 +08:00
|
|
|
while (n != nullptr) {
|
2020-01-31 16:32:48 +08:00
|
|
|
n = n->getRightChild(mirror->arena);
|
2021-03-11 02:06:03 +08:00
|
|
|
if (n != nullptr)
|
|
|
|
node = n;
|
2019-02-21 18:46:30 +08:00
|
|
|
}
|
2020-02-04 17:22:27 +08:00
|
|
|
return _hideDeletedBackward();
|
2019-02-21 18:46:30 +08:00
|
|
|
}
|
|
|
|
|
2020-02-04 17:22:27 +08:00
|
|
|
// Try to move to next node, sees deleted nodes.
|
|
|
|
void _moveNext() {
|
2019-02-21 18:46:30 +08:00
|
|
|
// Try to go right
|
2020-04-25 05:12:40 +08:00
|
|
|
DecodedNode* n = node->getRightChild(mirror->arena);
|
2019-02-21 18:46:30 +08:00
|
|
|
|
2020-01-31 16:32:48 +08:00
|
|
|
// If we couldn't go right, then the answer is our next ancestor
|
2020-04-25 05:12:40 +08:00
|
|
|
if (n == nullptr) {
|
2020-01-31 16:32:48 +08:00
|
|
|
node = node->getNextAncestor();
|
2020-04-25 05:12:40 +08:00
|
|
|
} else {
|
2020-02-04 17:22:27 +08:00
|
|
|
// Go left as far as possible
|
2020-04-25 05:12:40 +08:00
|
|
|
while (n != nullptr) {
|
2020-02-04 17:22:27 +08:00
|
|
|
node = n;
|
|
|
|
n = n->getLeftChild(mirror->arena);
|
|
|
|
}
|
2020-01-31 16:32:48 +08:00
|
|
|
}
|
2019-02-21 18:46:30 +08:00
|
|
|
}
|
|
|
|
|
2020-02-04 17:22:27 +08:00
|
|
|
// Try to move to previous node, sees deleted nodes.
|
|
|
|
void _movePrev() {
|
2019-02-21 18:46:30 +08:00
|
|
|
// Try to go left
|
2020-04-25 05:12:40 +08:00
|
|
|
DecodedNode* n = node->getLeftChild(mirror->arena);
|
2019-02-21 18:46:30 +08:00
|
|
|
|
2020-01-31 16:32:48 +08:00
|
|
|
// If we couldn't go left, then the answer is our prev ancestor
|
2020-04-25 05:12:40 +08:00
|
|
|
if (n == nullptr) {
|
2020-01-31 16:32:48 +08:00
|
|
|
node = node->getPrevAncestor();
|
2020-04-25 05:12:40 +08:00
|
|
|
} else {
|
2020-02-04 17:22:27 +08:00
|
|
|
// Go right as far as possible
|
2020-04-25 05:12:40 +08:00
|
|
|
while (n != nullptr) {
|
2020-02-04 17:22:27 +08:00
|
|
|
node = n;
|
|
|
|
n = n->getRightChild(mirror->arena);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-21 18:46:30 +08:00
|
|
|
|
2020-02-04 17:22:27 +08:00
|
|
|
bool moveNext() {
|
|
|
|
_moveNext();
|
|
|
|
return _hideDeletedForward();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool movePrev() {
|
|
|
|
_movePrev();
|
|
|
|
return _hideDeletedBackward();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool _hideDeletedBackward() {
|
2020-04-25 05:12:40 +08:00
|
|
|
while (node != nullptr && node->isDeleted()) {
|
2020-02-04 17:22:27 +08:00
|
|
|
_movePrev();
|
2020-01-31 16:32:48 +08:00
|
|
|
}
|
2020-02-04 17:22:27 +08:00
|
|
|
return node != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool _hideDeletedForward() {
|
2020-04-25 05:12:40 +08:00
|
|
|
while (node != nullptr && node->isDeleted()) {
|
2020-02-04 17:22:27 +08:00
|
|
|
_moveNext();
|
|
|
|
}
|
|
|
|
return node != nullptr;
|
2019-02-21 18:46:30 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Returns number of bytes written
|
2020-04-25 05:12:40 +08:00
|
|
|
int build(int spaceAvailable, const T* begin, const T* end, const T* prev, const T* next) {
|
2020-04-01 19:24:40 +08:00
|
|
|
largeNodes = spaceAvailable > SmallSizeLimit;
|
2019-02-21 18:46:30 +08:00
|
|
|
int count = end - begin;
|
2019-11-23 16:09:11 +08:00
|
|
|
numItems = count;
|
2020-02-06 07:30:04 +08:00
|
|
|
nodeBytesDeleted = 0;
|
2021-06-05 00:43:50 +08:00
|
|
|
initialHeight = count ? (uint8_t)log2(count) + 1 : 0;
|
2019-11-23 16:09:11 +08:00
|
|
|
maxHeight = 0;
|
2019-02-21 18:46:30 +08:00
|
|
|
|
|
|
|
// The boundary leading to the new page acts as the last time we branched right
|
2020-04-25 05:12:40 +08:00
|
|
|
if (begin != end) {
|
2020-04-01 19:24:40 +08:00
|
|
|
nodeBytesUsed = buildSubtree(root(), begin, end, prev, next, prev->getCommonPrefixLen(*next, 0));
|
2020-04-25 05:12:40 +08:00
|
|
|
} else {
|
2020-02-06 07:30:04 +08:00
|
|
|
nodeBytesUsed = 0;
|
2019-02-21 18:46:30 +08:00
|
|
|
}
|
2020-02-06 07:30:04 +08:00
|
|
|
nodeBytesFree = spaceAvailable - size();
|
2019-02-21 18:46:30 +08:00
|
|
|
return size();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-04-25 05:12:40 +08:00
|
|
|
int buildSubtree(Node& node, const T* begin, const T* end, const T* prev, const T* next, int subtreeCommon) {
|
|
|
|
// printf("build: %s to %s\n", begin->toString().c_str(), (end - 1)->toString().c_str());
|
|
|
|
// printf("build: root at %p Node::headerSize %d delta at %p \n", &root, Node::headerSize(largeNodes),
|
|
|
|
// &node.delta(largeNodes));
|
2019-02-21 18:46:30 +08:00
|
|
|
ASSERT(end != begin);
|
|
|
|
int count = end - begin;
|
|
|
|
|
|
|
|
// Find key to be stored in root
|
|
|
|
int mid = perfectSubtreeSplitPointCached(count);
|
2020-04-25 05:12:40 +08:00
|
|
|
const T& item = begin[mid];
|
2019-02-21 18:46:30 +08:00
|
|
|
|
2019-09-28 13:56:33 +08:00
|
|
|
int commonWithPrev = item.getCommonPrefixLen(*prev, subtreeCommon);
|
|
|
|
int commonWithNext = item.getCommonPrefixLen(*next, subtreeCommon);
|
2019-05-29 21:23:32 +08:00
|
|
|
|
|
|
|
bool prefixSourcePrev;
|
|
|
|
int commonPrefix;
|
2020-04-25 05:12:40 +08:00
|
|
|
const T* base;
|
|
|
|
if (commonWithPrev >= commonWithNext) {
|
2019-05-29 21:23:32 +08:00
|
|
|
prefixSourcePrev = true;
|
|
|
|
commonPrefix = commonWithPrev;
|
|
|
|
base = prev;
|
2020-04-25 05:12:40 +08:00
|
|
|
} else {
|
2019-05-29 21:23:32 +08:00
|
|
|
prefixSourcePrev = false;
|
|
|
|
commonPrefix = commonWithNext;
|
|
|
|
base = next;
|
|
|
|
}
|
|
|
|
|
2020-04-01 19:24:40 +08:00
|
|
|
int deltaSize = item.writeDelta(node.delta(largeNodes), *base, commonPrefix);
|
|
|
|
node.delta(largeNodes).setPrefixSource(prefixSourcePrev);
|
2021-04-19 16:18:48 +08:00
|
|
|
deltatree_printf("Serialized %s to offset %d data: %s\n",
|
2021-04-19 18:12:30 +08:00
|
|
|
item.toString().c_str(),
|
|
|
|
(uint8_t*)&node - (uint8_t*)this,
|
|
|
|
StringRef((uint8_t*)&node.delta(largeNodes), deltaSize).toHexString().c_str());
|
2019-02-21 18:46:30 +08:00
|
|
|
|
2019-05-29 21:23:32 +08:00
|
|
|
// Continue writing after the serialized Delta.
|
2020-04-25 05:12:40 +08:00
|
|
|
uint8_t* wptr = (uint8_t*)&node.delta(largeNodes) + deltaSize;
|
2019-02-21 18:46:30 +08:00
|
|
|
|
|
|
|
// Serialize left child
|
2020-04-25 05:12:40 +08:00
|
|
|
if (count > 1) {
|
|
|
|
wptr += buildSubtree(*(Node*)wptr, begin, begin + mid, prev, &item, commonWithPrev);
|
2020-04-01 19:24:40 +08:00
|
|
|
node.setLeftChildOffset(largeNodes, Node::headerSize(largeNodes) + deltaSize);
|
2020-04-25 05:12:40 +08:00
|
|
|
} else {
|
2020-04-01 19:24:40 +08:00
|
|
|
node.setLeftChildOffset(largeNodes, 0);
|
2019-02-21 18:46:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize right child
|
2020-04-25 05:12:40 +08:00
|
|
|
if (count > 2) {
|
|
|
|
node.setRightChildOffset(largeNodes, wptr - (uint8_t*)&node);
|
|
|
|
wptr += buildSubtree(*(Node*)wptr, begin + mid + 1, end, &item, next, commonWithNext);
|
|
|
|
} else {
|
2020-04-01 19:24:40 +08:00
|
|
|
node.setRightChildOffset(largeNodes, 0);
|
2019-02-21 18:46:30 +08:00
|
|
|
}
|
|
|
|
|
2020-04-25 05:12:40 +08:00
|
|
|
return wptr - (uint8_t*)&node;
|
2019-02-21 18:46:30 +08:00
|
|
|
}
|
|
|
|
};
|
2021-04-19 14:58:24 +08:00
|
|
|
|
2021-06-10 17:29:17 +08:00
|
|
|
// DeltaTree2 is a memory mappable binary tree of T objects such that each node's item is
|
|
|
|
// stored as a Delta which can reproduce the node's T item given either
|
|
|
|
// - The node's greatest lesser ancestor, called the "left parent"
|
|
|
|
// - The node's least greater ancestor, called the "right parent"
|
|
|
|
// One of these ancestors will also happen to be the node's direct parent.
|
|
|
|
//
|
|
|
|
// The Delta type is intended to make use of ordered prefix compression and borrow all
|
|
|
|
// available prefix bytes from the ancestor T which shares the most prefix bytes with
|
|
|
|
// the item T being encoded. If T is implemented properly, this results in perfect
|
|
|
|
// prefix compression while performing O(log n) comparisons for a seek.
|
|
|
|
//
|
|
|
|
// T requirements
|
|
|
|
//
|
|
|
|
// Must be compatible with Standalone<T> and must implement the following additional things:
|
|
|
|
//
|
|
|
|
// // Return the common prefix length between *this and T
|
|
|
|
// // skipLen is a hint, representing the length that is already known to be common.
|
|
|
|
// int getCommonPrefixLen(const T& other, int skipLen) const;
|
|
|
|
//
|
|
|
|
// // Compare *this to rhs, returns < 0 for less than, 0 for equal, > 0 for greater than
|
|
|
|
// // skipLen is a hint, representing the length that is already known to be common.
|
|
|
|
// int compare(const T &rhs, int skipLen) const;
|
|
|
|
//
|
|
|
|
// // Writes to d a delta which can create *this from base
|
|
|
|
// // commonPrefix is a hint, representing the length that is already known to be common.
|
|
|
|
// // DeltaT's size need not be static, for more details see below.
|
|
|
|
// void writeDelta(DeltaT &d, const T &base, int commonPrefix) const;
|
|
|
|
//
|
|
|
|
// // Returns the size in bytes of the DeltaT required to recreate *this from base
|
|
|
|
// int deltaSize(const T &base) const;
|
|
|
|
//
|
|
|
|
// // A type which represents the parts of T that either borrowed from the base T
|
|
|
|
// // or can be borrowed by other T's using the first T as a base
|
|
|
|
// // Partials must allocate any heap storage in the provided Arena for any operation.
|
|
|
|
// typedef Partial;
|
|
|
|
//
|
|
|
|
// // Update cache with the Partial for *this, storing any heap memory for the Partial in arena
|
|
|
|
// void updateCache(Optional<Partial> cache, Arena& arena) const;
|
|
|
|
//
|
|
|
|
// // For debugging, return a useful human-readable string representation of *this
|
|
|
|
// std::string toString() const;
|
|
|
|
//
|
|
|
|
// DeltaT requirements
|
|
|
|
//
|
|
|
|
// DeltaT can be variable sized, larger than sizeof(DeltaT), and implement the following:
|
|
|
|
//
|
|
|
|
// // Returns the size in bytes of this specific DeltaT instance
|
|
|
|
// int size();
|
|
|
|
//
|
|
|
|
// // Apply *this to base and return the resulting T
|
|
|
|
// // Store the Partial for T into cache, allocating any heap memory for the Partial in arena
|
|
|
|
// T apply(Arena& arena, const T& base, Optional<T::Partial>& cache);
|
|
|
|
//
|
|
|
|
// // Recreate T from *this and the Partial for T
|
|
|
|
// T apply(const T::Partial& cache);
|
|
|
|
//
|
|
|
|
// // Set or retrieve a boolean flag representing which base ancestor the DeltaT is to be applied to
|
|
|
|
// void setPrefixSource(bool val);
|
|
|
|
// bool getPrefixSource() const;
|
|
|
|
//
|
|
|
|
// // Set of retrieve a boolean flag representing that a DeltaTree node has been erased
|
|
|
|
// void setDeleted(bool val);
|
|
|
|
// bool getDeleted() const;
|
|
|
|
//
|
|
|
|
// // For debugging, return a useful human-readable string representation of *this
|
|
|
|
// std::string toString() const;
|
|
|
|
//
|
2021-04-19 14:58:24 +08:00
|
|
|
#pragma pack(push, 1)
|
|
|
|
template <typename T, typename DeltaT = typename T::Delta>
|
|
|
|
struct DeltaTree2 {
|
|
|
|
typedef typename T::Partial Partial;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
uint16_t numItems; // Number of items in the tree.
|
|
|
|
uint32_t nodeBytesUsed; // Bytes used by nodes (everything after the tree header)
|
|
|
|
uint32_t nodeBytesFree; // Bytes left at end of tree to expand into
|
|
|
|
uint32_t nodeBytesDeleted; // Delta bytes deleted from tree. Note that some of these bytes could be borrowed by
|
|
|
|
// descendents.
|
|
|
|
uint8_t initialHeight; // Height of tree as originally built
|
|
|
|
uint8_t maxHeight; // Maximum height of tree after any insertion. Value of 0 means no insertions done.
|
|
|
|
bool largeNodes; // Node size, can be calculated as capacity > SmallSizeLimit but it will be used a lot
|
|
|
|
};
|
2021-06-10 17:29:17 +08:00
|
|
|
|
|
|
|
// Node is not fixed size. Most node methods require the context of whether the node is in small or large
|
|
|
|
// offset mode, passed as a boolean
|
2021-04-19 14:58:24 +08:00
|
|
|
struct Node {
|
2021-06-10 17:29:17 +08:00
|
|
|
// Offsets are relative to the start of the DeltaTree
|
2021-04-19 14:58:24 +08:00
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
uint32_t leftChild;
|
|
|
|
uint32_t rightChild;
|
|
|
|
|
|
|
|
} largeOffsets;
|
|
|
|
struct {
|
|
|
|
uint16_t leftChild;
|
|
|
|
uint16_t rightChild;
|
|
|
|
} smallOffsets;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int headerSize(bool large) { return large ? sizeof(largeOffsets) : sizeof(smallOffsets); }
|
|
|
|
|
|
|
|
// Delta is located after the offsets, which differs by node size
|
|
|
|
DeltaT& delta(bool large) { return large ? *(DeltaT*)(&largeOffsets + 1) : *(DeltaT*)(&smallOffsets + 1); };
|
|
|
|
|
|
|
|
// Delta is located after the offsets, which differs by node size
|
|
|
|
const DeltaT& delta(bool large) const {
|
|
|
|
return large ? *(DeltaT*)(&largeOffsets + 1) : *(DeltaT*)(&smallOffsets + 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string toString(DeltaTree2* tree) const {
|
2021-04-24 13:47:23 +08:00
|
|
|
return format("Node{offset=%d leftChild=%d rightChild=%d delta=%s}",
|
2021-04-19 14:58:24 +08:00
|
|
|
tree->nodeOffset(this),
|
|
|
|
getLeftChildOffset(tree->largeNodes),
|
|
|
|
getRightChildOffset(tree->largeNodes),
|
|
|
|
delta(tree->largeNodes).toString().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
#define getMember(m) (large ? largeOffsets.m : smallOffsets.m)
|
|
|
|
#define setMember(m, v) \
|
|
|
|
if (large) { \
|
|
|
|
largeOffsets.m = v; \
|
|
|
|
} else { \
|
|
|
|
smallOffsets.m = v; \
|
|
|
|
}
|
|
|
|
|
|
|
|
void setRightChildOffset(bool large, int offset) { setMember(rightChild, offset); }
|
|
|
|
void setLeftChildOffset(bool large, int offset) { setMember(leftChild, offset); }
|
|
|
|
|
|
|
|
int getRightChildOffset(bool large) const { return getMember(rightChild); }
|
|
|
|
int getLeftChildOffset(bool large) const { return getMember(leftChild); }
|
|
|
|
|
|
|
|
int size(bool large) const { return delta(large).size() + headerSize(large); }
|
|
|
|
#undef getMember
|
|
|
|
#undef setMember
|
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr int SmallSizeLimit = std::numeric_limits<uint16_t>::max();
|
|
|
|
static constexpr int LargeTreePerNodeExtraOverhead = sizeof(Node::largeOffsets) - sizeof(Node::smallOffsets);
|
|
|
|
|
|
|
|
int nodeOffset(const Node* n) const { return (uint8_t*)n - (uint8_t*)this; }
|
|
|
|
Node* nodeAt(int offset) { return offset == 0 ? nullptr : (Node*)((uint8_t*)this + offset); }
|
|
|
|
Node* root() { return numItems == 0 ? nullptr : (Node*)(this + 1); }
|
2021-04-24 13:47:23 +08:00
|
|
|
int rootOffset() { return sizeof(DeltaTree2); }
|
2021-04-19 14:58:24 +08:00
|
|
|
|
|
|
|
int size() const { return sizeof(DeltaTree2) + nodeBytesUsed; }
|
|
|
|
int capacity() const { return size() + nodeBytesFree; }
|
|
|
|
|
|
|
|
public:
|
2021-06-10 17:29:17 +08:00
|
|
|
// DecodedNode represents a Node of a DeltaTree and its T::Partial.
|
|
|
|
// DecodedNodes are created on-demand, as DeltaTree Nodes are visited by a Cursor.
|
|
|
|
// DecodedNodes link together to form a binary tree with the same Node relationships as their
|
|
|
|
// corresponding DeltaTree Nodes. Additionally, DecodedNodes store links to their left and
|
|
|
|
// right ancestors which correspond to possible base Nodes on which the Node's Delta is based.
|
|
|
|
//
|
|
|
|
// DecodedNode links are not pointers, but rather indices to be looked up in the DecodeCache
|
|
|
|
// defined below. An index value of -1 is uninitialized, meaning it is not yet known whether
|
|
|
|
// the corresponding DeltaTree Node link is non-null in any version of the DeltaTree which is
|
|
|
|
// using or has used the DecodeCache.
|
2021-04-24 13:47:23 +08:00
|
|
|
struct DecodedNode {
|
|
|
|
DecodedNode(int nodeOffset, int leftParentIndex, int rightParentIndex)
|
|
|
|
: nodeOffset(nodeOffset), leftParentIndex(leftParentIndex), rightParentIndex(rightParentIndex),
|
|
|
|
leftChildIndex(-1), rightChildIndex(-1) {}
|
|
|
|
int nodeOffset;
|
|
|
|
int16_t leftParentIndex;
|
|
|
|
int16_t rightParentIndex;
|
|
|
|
int16_t leftChildIndex;
|
|
|
|
int16_t rightChildIndex;
|
|
|
|
Optional<Partial> partial;
|
|
|
|
|
|
|
|
Node* node(DeltaTree2* tree) const { return tree->nodeAt(nodeOffset); }
|
|
|
|
|
|
|
|
std::string toString() {
|
|
|
|
return format("DecodedNode{nodeOffset=%d leftChildIndex=%d rightChildIndex=%d leftParentIndex=%d "
|
|
|
|
"rightParentIndex=%d}",
|
|
|
|
(int)nodeOffset,
|
|
|
|
(int)leftChildIndex,
|
|
|
|
(int)rightChildIndex,
|
|
|
|
(int)leftParentIndex,
|
|
|
|
(int)rightParentIndex);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#pragma pack(pop)
|
2021-06-10 17:29:17 +08:00
|
|
|
|
|
|
|
// The DecodeCache is a reference counted structure that stores DecodedNodes by an integer index
|
|
|
|
// and can be shared across a series of updated copies of a DeltaTree.
|
|
|
|
//
|
|
|
|
// DecodedNodes are stored in a contiguous vector, which sometimes must be expanded, so care
|
|
|
|
// must be taken to resolve DecodedNode pointers again after the DecodeCache has new entries added.
|
2021-04-27 14:14:04 +08:00
|
|
|
struct DecodeCache : FastAllocated<DecodeCache>, ReferenceCounted<DecodeCache> {
|
2021-04-19 14:58:24 +08:00
|
|
|
DecodeCache(const T& lowerBound = T(), const T& upperBound = T())
|
2021-04-23 05:55:28 +08:00
|
|
|
: lowerBound(arena, lowerBound), upperBound(arena, upperBound) {
|
2021-04-24 13:47:23 +08:00
|
|
|
decodedNodes.reserve(10);
|
2021-04-27 14:14:04 +08:00
|
|
|
deltatree_printf("DecodedNode size: %d\n", sizeof(DecodedNode));
|
2021-04-23 05:55:28 +08:00
|
|
|
}
|
2021-04-19 14:58:24 +08:00
|
|
|
|
|
|
|
Arena arena;
|
|
|
|
T lowerBound;
|
|
|
|
T upperBound;
|
2021-04-24 13:47:23 +08:00
|
|
|
|
|
|
|
// Index 0 is always the root
|
|
|
|
std::vector<DecodedNode> decodedNodes;
|
|
|
|
|
|
|
|
DecodedNode& get(int index) { return decodedNodes[index]; }
|
|
|
|
|
|
|
|
template <class... Args>
|
|
|
|
int emplace_new(Args&&... args) {
|
|
|
|
int index = decodedNodes.size();
|
|
|
|
decodedNodes.emplace_back(args...);
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const { return decodedNodes.empty(); }
|
2021-04-19 14:58:24 +08:00
|
|
|
|
|
|
|
void clear() {
|
2021-04-24 13:47:23 +08:00
|
|
|
decodedNodes.clear();
|
2021-04-19 14:58:24 +08:00
|
|
|
Arena a;
|
|
|
|
lowerBound = T(a, lowerBound);
|
|
|
|
upperBound = T(a, upperBound);
|
|
|
|
arena = a;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Cursor provides a way to seek into a DeltaTree and iterate over its contents
|
|
|
|
// The cursor needs a DeltaTree pointer and a DecodeCache, which can be shared
|
|
|
|
// with other DeltaTrees which were incrementally modified to produce the the
|
|
|
|
// tree that this cursor is referencing.
|
|
|
|
struct Cursor {
|
2021-04-24 13:47:23 +08:00
|
|
|
Cursor() : cache(nullptr), nodeIndex(-1) {}
|
2021-04-19 14:58:24 +08:00
|
|
|
|
2021-07-23 13:48:27 +08:00
|
|
|
Cursor(DecodeCache* cache, DeltaTree2* tree) : tree(tree), cache(cache), nodeIndex(-1) {}
|
2021-04-24 13:47:23 +08:00
|
|
|
|
2021-07-23 13:48:27 +08:00
|
|
|
Cursor(DecodeCache* cache, DeltaTree2* tree, int nodeIndex) : tree(tree), cache(cache), nodeIndex(nodeIndex) {}
|
2021-05-25 16:31:50 +08:00
|
|
|
|
|
|
|
// Copy constructor does not copy item because normally a copied cursor will be immediately moved.
|
2021-07-23 13:48:27 +08:00
|
|
|
Cursor(const Cursor& c) : tree(c.tree), cache(c.cache), nodeIndex(c.nodeIndex) {}
|
2021-05-20 17:08:17 +08:00
|
|
|
|
2021-06-03 17:25:57 +08:00
|
|
|
Cursor next() const {
|
|
|
|
Cursor c = *this;
|
|
|
|
c.moveNext();
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cursor previous() const {
|
|
|
|
Cursor c = *this;
|
|
|
|
c.movePrev();
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2021-04-24 13:47:23 +08:00
|
|
|
int rootIndex() {
|
|
|
|
if (!cache->empty()) {
|
|
|
|
return 0;
|
|
|
|
} else if (tree->numItems != 0) {
|
|
|
|
return cache->emplace_new(tree->rootOffset(), -1, -1);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2021-04-19 14:58:24 +08:00
|
|
|
|
|
|
|
DeltaTree2* tree;
|
|
|
|
DecodeCache* cache;
|
2021-04-24 13:47:23 +08:00
|
|
|
int nodeIndex;
|
2021-05-25 16:31:50 +08:00
|
|
|
mutable Optional<T> item;
|
2021-04-24 13:47:23 +08:00
|
|
|
|
|
|
|
Node* node() const { return tree->nodeAt(cache->get(nodeIndex).nodeOffset); }
|
2021-04-19 14:58:24 +08:00
|
|
|
|
|
|
|
std::string toString() const {
|
2021-04-24 13:47:23 +08:00
|
|
|
if (nodeIndex == -1) {
|
|
|
|
return format("Cursor{nodeIndex=-1}");
|
|
|
|
}
|
2021-05-18 16:33:11 +08:00
|
|
|
return format("Cursor{item=%s indexItem=%s nodeIndex=%d decodedNode=%s node=%s ",
|
2021-05-25 16:31:50 +08:00
|
|
|
item.present() ? item.get().toString().c_str() : "<absent>",
|
2021-05-18 16:33:11 +08:00
|
|
|
get(cache->get(nodeIndex)).toString().c_str(),
|
2021-04-24 13:47:23 +08:00
|
|
|
nodeIndex,
|
|
|
|
cache->get(nodeIndex).toString().c_str(),
|
|
|
|
node()->toString(tree).c_str());
|
2021-04-19 14:58:24 +08:00
|
|
|
}
|
|
|
|
|
2021-04-24 13:47:23 +08:00
|
|
|
bool valid() const { return nodeIndex != -1; }
|
2021-04-19 14:58:24 +08:00
|
|
|
|
|
|
|
// Get T for Node n, and provide to n's delta the base and local decode cache entries to use/modify
|
2021-04-24 13:47:23 +08:00
|
|
|
const T get(DecodedNode& decoded) const {
|
|
|
|
DeltaT& delta = decoded.node(tree)->delta(tree->largeNodes);
|
2021-04-19 14:58:24 +08:00
|
|
|
|
2021-04-24 13:47:23 +08:00
|
|
|
// If this node's cached partial is populated, then the delta can create T from that alone
|
|
|
|
if (decoded.partial.present()) {
|
|
|
|
return delta.apply(decoded.partial.get());
|
2021-04-19 14:58:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, get the base T
|
|
|
|
bool basePrev = delta.getPrefixSource();
|
2021-04-24 13:47:23 +08:00
|
|
|
int baseIndex = basePrev ? decoded.leftParentIndex : decoded.rightParentIndex;
|
2021-04-19 14:58:24 +08:00
|
|
|
|
2021-04-24 13:47:23 +08:00
|
|
|
// If baseOffset is -1, then base T is DecodeCache's lower or upper bound
|
|
|
|
if (baseIndex == -1) {
|
|
|
|
return delta.apply(cache->arena, basePrev ? cache->lowerBound : cache->upperBound, decoded.partial);
|
2021-04-19 14:58:24 +08:00
|
|
|
}
|
|
|
|
|
2021-05-26 16:21:02 +08:00
|
|
|
// Otherwise, get the base's decoded node
|
|
|
|
DecodedNode& baseDecoded = cache->get(baseIndex);
|
|
|
|
|
|
|
|
// If the base's partial is present, apply delta to it to get result
|
|
|
|
if (baseDecoded.partial.present()) {
|
|
|
|
return delta.apply(cache->arena, baseDecoded.partial.get(), decoded.partial);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise apply delta to base T
|
|
|
|
return delta.apply(cache->arena, get(baseDecoded), decoded.partial);
|
2021-04-19 14:58:24 +08:00
|
|
|
}
|
|
|
|
|
2021-05-18 16:33:11 +08:00
|
|
|
public:
|
2021-05-18 16:58:30 +08:00
|
|
|
// Get the item at the cursor
|
|
|
|
// Behavior is undefined if the cursor is not valid.
|
|
|
|
// If the cursor is moved, the reference object returned will be modified to
|
|
|
|
// the cursor's new current item.
|
2021-05-25 16:31:50 +08:00
|
|
|
const T& get() const {
|
|
|
|
if (!item.present()) {
|
|
|
|
item = get(cache->get(nodeIndex));
|
2021-05-20 17:08:17 +08:00
|
|
|
}
|
2021-05-25 16:31:50 +08:00
|
|
|
return item.get();
|
2021-05-20 17:08:17 +08:00
|
|
|
}
|
|
|
|
|
2021-11-11 00:22:57 +08:00
|
|
|
void switchTree(DeltaTree2* newTree) {
|
|
|
|
tree = newTree;
|
|
|
|
// Reset item because it may point into tree memory
|
|
|
|
item.reset();
|
|
|
|
}
|
2021-05-25 16:31:50 +08:00
|
|
|
|
2021-05-18 16:58:30 +08:00
|
|
|
// If the cursor is valid, return a reference to the cursor's internal T.
|
|
|
|
// Otherwise, returns a reference to the cache's upper boundary.
|
|
|
|
const T& getOrUpperBound() const { return valid() ? get() : cache->upperBound; }
|
2021-04-19 14:58:24 +08:00
|
|
|
|
2021-04-24 13:47:23 +08:00
|
|
|
bool operator==(const Cursor& rhs) const { return nodeIndex == rhs.nodeIndex; }
|
|
|
|
bool operator!=(const Cursor& rhs) const { return nodeIndex != rhs.nodeIndex; }
|
2021-04-19 14:58:24 +08:00
|
|
|
|
|
|
|
// The seek methods, of the form seek[Less|Greater][orEqual](...) are very similar.
|
|
|
|
// They attempt move the cursor to the [Greatest|Least] item, based on the name of the function.
|
|
|
|
// Then will not "see" erased records.
|
|
|
|
// If successful, they return true, and if not then false a while making the cursor invalid.
|
|
|
|
// These methods forward arguments to the seek() overloads, see those for argument descriptions.
|
|
|
|
template <typename... Args>
|
|
|
|
bool seekLessThan(Args... args) {
|
|
|
|
int cmp = seek(args...);
|
2021-04-24 13:47:23 +08:00
|
|
|
if (cmp < 0 || (cmp == 0 && nodeIndex != -1)) {
|
2021-04-19 14:58:24 +08:00
|
|
|
movePrev();
|
|
|
|
}
|
|
|
|
return _hideDeletedBackward();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
bool seekLessThanOrEqual(Args... args) {
|
|
|
|
int cmp = seek(args...);
|
|
|
|
if (cmp < 0) {
|
|
|
|
movePrev();
|
|
|
|
}
|
|
|
|
return _hideDeletedBackward();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
bool seekGreaterThan(Args... args) {
|
|
|
|
int cmp = seek(args...);
|
2021-04-24 13:47:23 +08:00
|
|
|
if (cmp > 0 || (cmp == 0 && nodeIndex != -1)) {
|
2021-04-19 14:58:24 +08:00
|
|
|
moveNext();
|
|
|
|
}
|
|
|
|
return _hideDeletedForward();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
bool seekGreaterThanOrEqual(Args... args) {
|
|
|
|
int cmp = seek(args...);
|
|
|
|
if (cmp > 0) {
|
|
|
|
moveNext();
|
|
|
|
}
|
|
|
|
return _hideDeletedForward();
|
|
|
|
}
|
|
|
|
|
2021-04-24 13:47:23 +08:00
|
|
|
// Get the right child index for parentIndex
|
|
|
|
int getRightChildIndex(int parentIndex) {
|
|
|
|
DecodedNode* parent = &cache->get(parentIndex);
|
|
|
|
|
|
|
|
// The cache may have a child index, but since cache covers multiple versions of a DeltaTree
|
|
|
|
// it can't be used unless the node in the tree has a child.
|
|
|
|
int childOffset = parent->node(tree)->getRightChildOffset(tree->largeNodes);
|
|
|
|
|
|
|
|
if (childOffset == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// parent has this child so return the index if it is in DecodedNode
|
|
|
|
if (parent->rightChildIndex != -1) {
|
|
|
|
return parent->rightChildIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the child's DecodedNode and get its index
|
|
|
|
int childIndex = cache->emplace_new(childOffset, parentIndex, parent->rightParentIndex);
|
|
|
|
|
|
|
|
// Set the index in the parent. The cache lookup is repeated because the cache has changed.
|
|
|
|
cache->get(parentIndex).rightChildIndex = childIndex;
|
|
|
|
return childIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the left child index for parentIndex
|
|
|
|
int getLeftChildIndex(int parentIndex) {
|
|
|
|
DecodedNode* parent = &cache->get(parentIndex);
|
|
|
|
|
|
|
|
// The cache may have a child index, but since cache covers multiple versions of a DeltaTree
|
|
|
|
// it can't be used unless the node in the tree has a child.
|
|
|
|
int childOffset = parent->node(tree)->getLeftChildOffset(tree->largeNodes);
|
|
|
|
|
|
|
|
if (childOffset == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// parent has this child so return the index if it is in DecodedNode
|
|
|
|
if (parent->leftChildIndex != -1) {
|
|
|
|
return parent->leftChildIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the child's DecodedNode and get its index
|
|
|
|
int childIndex = cache->emplace_new(childOffset, parent->leftParentIndex, parentIndex);
|
|
|
|
|
|
|
|
// Set the index in the parent. The cache lookup is repeated because the cache has changed.
|
|
|
|
cache->get(parentIndex).leftChildIndex = childIndex;
|
|
|
|
return childIndex;
|
|
|
|
}
|
|
|
|
|
2021-04-19 14:58:24 +08:00
|
|
|
// seek() moves the cursor to a node containing s or the node that would be the parent of s if s were to be
|
|
|
|
// added to the tree. If the tree was empty, the cursor will be invalid and the return value will be 0.
|
|
|
|
// Otherwise, returns the result of s.compare(item at cursor position)
|
|
|
|
// Does not skip/avoid deleted nodes.
|
|
|
|
int seek(const T& s, int skipLen = 0) {
|
2021-04-24 13:47:23 +08:00
|
|
|
nodeIndex = -1;
|
2021-05-25 16:31:50 +08:00
|
|
|
item.reset();
|
2021-04-19 14:58:24 +08:00
|
|
|
deltatree_printf("seek(%s) start %s\n", s.toString().c_str(), toString().c_str());
|
2021-04-24 13:47:23 +08:00
|
|
|
int nIndex = rootIndex();
|
2021-04-19 14:58:24 +08:00
|
|
|
int cmp = 0;
|
|
|
|
|
2021-04-24 13:47:23 +08:00
|
|
|
while (nIndex != -1) {
|
|
|
|
nodeIndex = nIndex;
|
2021-05-25 16:31:50 +08:00
|
|
|
item.reset();
|
2021-04-19 14:58:24 +08:00
|
|
|
cmp = s.compare(get(), skipLen);
|
2021-04-24 13:47:23 +08:00
|
|
|
deltatree_printf("seek(%s) loop cmp=%d %s\n", s.toString().c_str(), cmp, toString().c_str());
|
2021-04-19 14:58:24 +08:00
|
|
|
if (cmp == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-04-24 13:47:23 +08:00
|
|
|
if (cmp > 0) {
|
|
|
|
nIndex = getRightChildIndex(nIndex);
|
|
|
|
} else {
|
|
|
|
nIndex = getLeftChildIndex(nIndex);
|
|
|
|
}
|
2021-04-19 14:58:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool moveFirst() {
|
2021-04-24 13:47:23 +08:00
|
|
|
nodeIndex = -1;
|
2021-05-25 16:31:50 +08:00
|
|
|
item.reset();
|
2021-04-24 13:47:23 +08:00
|
|
|
int nIndex = rootIndex();
|
2021-04-19 14:58:24 +08:00
|
|
|
deltatree_printf("moveFirst start %s\n", toString().c_str());
|
2021-04-24 13:47:23 +08:00
|
|
|
while (nIndex != -1) {
|
2021-04-27 14:14:04 +08:00
|
|
|
nodeIndex = nIndex;
|
|
|
|
deltatree_printf("moveFirst moved %s\n", toString().c_str());
|
2021-04-24 13:47:23 +08:00
|
|
|
nIndex = getLeftChildIndex(nIndex);
|
2021-04-19 14:58:24 +08:00
|
|
|
}
|
|
|
|
return _hideDeletedForward();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool moveLast() {
|
2021-04-24 13:47:23 +08:00
|
|
|
nodeIndex = -1;
|
2021-05-25 16:31:50 +08:00
|
|
|
item.reset();
|
2021-04-24 13:47:23 +08:00
|
|
|
int nIndex = rootIndex();
|
2021-04-19 14:58:24 +08:00
|
|
|
deltatree_printf("moveLast start %s\n", toString().c_str());
|
2021-04-24 13:47:23 +08:00
|
|
|
while (nIndex != -1) {
|
2021-04-27 14:14:04 +08:00
|
|
|
nodeIndex = nIndex;
|
|
|
|
deltatree_printf("moveLast moved %s\n", toString().c_str());
|
2021-04-24 13:47:23 +08:00
|
|
|
nIndex = getRightChildIndex(nIndex);
|
2021-04-19 14:58:24 +08:00
|
|
|
}
|
|
|
|
return _hideDeletedBackward();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to move to next node, sees deleted nodes.
|
|
|
|
void _moveNext() {
|
|
|
|
deltatree_printf("_moveNext start %s\n", toString().c_str());
|
2021-05-25 16:31:50 +08:00
|
|
|
item.reset();
|
2021-04-19 14:58:24 +08:00
|
|
|
// Try to go right
|
2021-04-24 13:47:23 +08:00
|
|
|
int nIndex = getRightChildIndex(nodeIndex);
|
2021-04-19 14:58:24 +08:00
|
|
|
|
|
|
|
// If we couldn't go right, then the answer is our next ancestor
|
2021-04-24 13:47:23 +08:00
|
|
|
if (nIndex == -1) {
|
|
|
|
nodeIndex = cache->get(nodeIndex).rightParentIndex;
|
2021-04-19 14:58:24 +08:00
|
|
|
deltatree_printf("_moveNext move1 %s\n", toString().c_str());
|
|
|
|
} else {
|
|
|
|
// Go left as far as possible
|
|
|
|
do {
|
2021-04-24 13:47:23 +08:00
|
|
|
nodeIndex = nIndex;
|
2021-04-19 14:58:24 +08:00
|
|
|
deltatree_printf("_moveNext move2 %s\n", toString().c_str());
|
2021-04-24 13:47:23 +08:00
|
|
|
nIndex = getLeftChildIndex(nodeIndex);
|
|
|
|
} while (nIndex != -1);
|
2021-04-19 14:58:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to move to previous node, sees deleted nodes.
|
|
|
|
void _movePrev() {
|
|
|
|
deltatree_printf("_movePrev start %s\n", toString().c_str());
|
2021-05-25 16:31:50 +08:00
|
|
|
item.reset();
|
2021-04-19 14:58:24 +08:00
|
|
|
// Try to go left
|
2021-04-24 13:47:23 +08:00
|
|
|
int nIndex = getLeftChildIndex(nodeIndex);
|
2021-04-19 14:58:24 +08:00
|
|
|
// If we couldn't go left, then the answer is our prev ancestor
|
2021-04-24 13:47:23 +08:00
|
|
|
if (nIndex == -1) {
|
|
|
|
nodeIndex = cache->get(nodeIndex).leftParentIndex;
|
2021-04-19 14:58:24 +08:00
|
|
|
deltatree_printf("_movePrev move1 %s\n", toString().c_str());
|
|
|
|
} else {
|
|
|
|
// Go right as far as possible
|
|
|
|
do {
|
2021-04-24 13:47:23 +08:00
|
|
|
nodeIndex = nIndex;
|
2021-04-19 14:58:24 +08:00
|
|
|
deltatree_printf("_movePrev move2 %s\n", toString().c_str());
|
2021-04-24 13:47:23 +08:00
|
|
|
nIndex = getRightChildIndex(nodeIndex);
|
|
|
|
} while (nIndex != -1);
|
2021-04-19 14:58:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool moveNext() {
|
|
|
|
_moveNext();
|
|
|
|
return _hideDeletedForward();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool movePrev() {
|
|
|
|
_movePrev();
|
|
|
|
return _hideDeletedBackward();
|
|
|
|
}
|
|
|
|
|
2021-04-24 13:47:23 +08:00
|
|
|
DeltaT& getDelta() const { return cache->get(nodeIndex).node(tree)->delta(tree->largeNodes); }
|
|
|
|
|
|
|
|
bool isErased() const { return getDelta().getDeleted(); }
|
2021-04-19 14:58:24 +08:00
|
|
|
|
|
|
|
// Erase current item by setting its deleted flag to true.
|
|
|
|
// Tree header is updated if a change is made.
|
2021-05-18 16:33:11 +08:00
|
|
|
// Cursor is then moved forward to the next non-deleted node.
|
2021-04-19 14:58:24 +08:00
|
|
|
void erase() {
|
2021-04-24 13:47:23 +08:00
|
|
|
auto& delta = getDelta();
|
2021-04-19 14:58:24 +08:00
|
|
|
if (!delta.getDeleted()) {
|
|
|
|
delta.setDeleted(true);
|
|
|
|
--tree->numItems;
|
|
|
|
tree->nodeBytesDeleted += (delta.size() + Node::headerSize(tree->largeNodes));
|
|
|
|
}
|
2021-05-18 16:33:11 +08:00
|
|
|
moveNext();
|
2021-04-19 14:58:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Erase k by setting its deleted flag to true. Returns true only if k existed
|
|
|
|
bool erase(const T& k, int skipLen = 0) {
|
2021-05-18 16:33:11 +08:00
|
|
|
Cursor c(cache, tree);
|
2021-04-19 14:58:24 +08:00
|
|
|
if (c.seek(k, skipLen) == 0 && !c.isErased()) {
|
|
|
|
c.erase();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to insert k into the DeltaTree, updating byte counts and initialHeight if they
|
|
|
|
// have changed (they won't if k already exists in the tree but was deleted).
|
|
|
|
// Returns true if successful, false if k does not fit in the space available
|
|
|
|
// or if k is already in the tree (and was not already deleted).
|
|
|
|
// Insertion on an empty tree returns false as well.
|
2021-05-20 17:08:17 +08:00
|
|
|
// Insert does NOT change the cursor position.
|
2021-04-19 14:58:24 +08:00
|
|
|
bool insert(const T& k, int skipLen = 0, int maxHeightAllowed = std::numeric_limits<int>::max()) {
|
|
|
|
deltatree_printf("insert %s\n", k.toString().c_str());
|
|
|
|
|
2021-04-24 13:47:23 +08:00
|
|
|
int nIndex = rootIndex();
|
|
|
|
int parentIndex = nIndex;
|
|
|
|
DecodedNode* parentDecoded;
|
|
|
|
// Result of comparing node at parentIndex
|
|
|
|
int cmp = 0;
|
|
|
|
// Height of the inserted node
|
2021-04-19 14:58:24 +08:00
|
|
|
int height = 0;
|
|
|
|
|
2021-04-24 13:47:23 +08:00
|
|
|
// Find the parent to add the node to
|
|
|
|
// This is just seek but modifies parentIndex instead of nodeIndex and tracks the insertion height
|
|
|
|
deltatree_printf(
|
|
|
|
"insert(%s) start %s\n", k.toString().c_str(), Cursor(cache, tree, parentIndex).toString().c_str());
|
|
|
|
while (nIndex != -1) {
|
|
|
|
++height;
|
|
|
|
parentIndex = nIndex;
|
|
|
|
parentDecoded = &cache->get(parentIndex);
|
|
|
|
cmp = k.compare(get(*parentDecoded), skipLen);
|
|
|
|
deltatree_printf("insert(%s) moved cmp=%d %s\n",
|
|
|
|
k.toString().c_str(),
|
|
|
|
cmp,
|
|
|
|
Cursor(cache, tree, parentIndex).toString().c_str());
|
|
|
|
|
|
|
|
if (cmp == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmp > 0) {
|
|
|
|
deltatree_printf("insert(%s) move right\n", k.toString().c_str());
|
|
|
|
nIndex = getRightChildIndex(nIndex);
|
|
|
|
} else {
|
|
|
|
deltatree_printf("insert(%s) move left\n", k.toString().c_str());
|
|
|
|
nIndex = getLeftChildIndex(nIndex);
|
|
|
|
}
|
|
|
|
}
|
2021-04-19 14:58:24 +08:00
|
|
|
|
|
|
|
// If the item is found, mark it erased if it isn't already
|
|
|
|
if (cmp == 0) {
|
2021-04-24 13:47:23 +08:00
|
|
|
DeltaT& delta = tree->nodeAt(parentDecoded->nodeOffset)->delta(tree->largeNodes);
|
|
|
|
if (delta.getDeleted()) {
|
|
|
|
delta.setDeleted(false);
|
|
|
|
++tree->numItems;
|
|
|
|
tree->nodeBytesDeleted -= (delta.size() + Node::headerSize(tree->largeNodes));
|
|
|
|
deltatree_printf("insert(%s) deleted item restored %s\n",
|
|
|
|
k.toString().c_str(),
|
|
|
|
Cursor(cache, tree, parentIndex).toString().c_str());
|
2021-04-19 14:58:24 +08:00
|
|
|
return true;
|
|
|
|
}
|
2021-04-24 13:47:23 +08:00
|
|
|
deltatree_printf("insert(%s) item exists %s\n",
|
|
|
|
k.toString().c_str(),
|
|
|
|
Cursor(cache, tree, parentIndex).toString().c_str());
|
2021-04-19 14:58:24 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-24 13:47:23 +08:00
|
|
|
// If the tree was empty or the max insertion height is exceeded then fail
|
|
|
|
if (parentIndex == -1 || height > maxHeightAllowed) {
|
2021-04-19 14:58:24 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-24 13:47:23 +08:00
|
|
|
// Find the base base to borrow from, see if the resulting delta fits into the tree
|
|
|
|
int leftBaseIndex, rightBaseIndex;
|
2021-04-19 14:58:24 +08:00
|
|
|
bool addingRight = cmp > 0;
|
|
|
|
if (addingRight) {
|
2021-04-24 13:47:23 +08:00
|
|
|
leftBaseIndex = parentIndex;
|
|
|
|
rightBaseIndex = parentDecoded->rightParentIndex;
|
2021-04-19 14:58:24 +08:00
|
|
|
} else {
|
2021-04-24 13:47:23 +08:00
|
|
|
leftBaseIndex = parentDecoded->leftParentIndex;
|
|
|
|
rightBaseIndex = parentIndex;
|
2021-04-19 14:58:24 +08:00
|
|
|
}
|
|
|
|
|
2021-04-24 13:47:23 +08:00
|
|
|
T leftBase = leftBaseIndex == -1 ? cache->lowerBound : get(cache->get(leftBaseIndex));
|
|
|
|
T rightBase = rightBaseIndex == -1 ? cache->upperBound : get(cache->get(rightBaseIndex));
|
2021-04-19 14:58:24 +08:00
|
|
|
|
|
|
|
int common = leftBase.getCommonPrefixLen(rightBase, skipLen);
|
|
|
|
int commonWithLeftParent = k.getCommonPrefixLen(leftBase, common);
|
|
|
|
int commonWithRightParent = k.getCommonPrefixLen(rightBase, common);
|
|
|
|
bool borrowFromLeft = commonWithLeftParent >= commonWithRightParent;
|
|
|
|
|
2021-04-24 13:47:23 +08:00
|
|
|
const T* base;
|
|
|
|
int commonPrefix;
|
|
|
|
if (borrowFromLeft) {
|
|
|
|
base = &leftBase;
|
|
|
|
commonPrefix = commonWithLeftParent;
|
|
|
|
} else {
|
|
|
|
base = &rightBase;
|
|
|
|
commonPrefix = commonWithRightParent;
|
|
|
|
}
|
|
|
|
|
|
|
|
int deltaSize = k.deltaSize(*base, commonPrefix, false);
|
2021-04-19 14:58:24 +08:00
|
|
|
int nodeSpace = deltaSize + Node::headerSize(tree->largeNodes);
|
|
|
|
|
|
|
|
if (nodeSpace > tree->nodeBytesFree) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-24 13:47:23 +08:00
|
|
|
int childOffset = tree->size();
|
|
|
|
Node* childNode = tree->nodeAt(childOffset);
|
|
|
|
childNode->setLeftChildOffset(tree->largeNodes, 0);
|
|
|
|
childNode->setRightChildOffset(tree->largeNodes, 0);
|
|
|
|
|
|
|
|
// Create the decoded node and link it to the parent
|
|
|
|
// Link the parent's decodednode to the child's decodednode
|
|
|
|
// Link the parent node in the tree to the new child node
|
|
|
|
// true if node is being added to right child
|
|
|
|
int childIndex = cache->emplace_new(childOffset, leftBaseIndex, rightBaseIndex);
|
|
|
|
|
|
|
|
// Get a new parentDecoded pointer as the cache may have changed allocations
|
|
|
|
parentDecoded = &cache->get(parentIndex);
|
|
|
|
|
2021-04-19 14:58:24 +08:00
|
|
|
if (addingRight) {
|
2021-04-24 13:47:23 +08:00
|
|
|
// Adding child to right of parent
|
|
|
|
parentDecoded->rightChildIndex = childIndex;
|
|
|
|
parentDecoded->node(tree)->setRightChildOffset(tree->largeNodes, childOffset);
|
2021-04-19 14:58:24 +08:00
|
|
|
} else {
|
2021-04-24 13:47:23 +08:00
|
|
|
// Adding child to left of parent
|
|
|
|
parentDecoded->leftChildIndex = childIndex;
|
|
|
|
parentDecoded->node(tree)->setLeftChildOffset(tree->largeNodes, childOffset);
|
2021-04-19 14:58:24 +08:00
|
|
|
}
|
|
|
|
|
2021-04-24 13:47:23 +08:00
|
|
|
// Give k opportunity to populate its cache partial record
|
|
|
|
k.updateCache(cache->get(childIndex).partial, cache->arena);
|
|
|
|
|
|
|
|
DeltaT& childDelta = childNode->delta(tree->largeNodes);
|
|
|
|
deltatree_printf("insert(%s) writing delta from %s\n", k.toString().c_str(), base->toString().c_str());
|
|
|
|
int written = k.writeDelta(childDelta, *base, commonPrefix);
|
2021-04-19 14:58:24 +08:00
|
|
|
ASSERT(deltaSize == written);
|
|
|
|
childDelta.setPrefixSource(borrowFromLeft);
|
|
|
|
|
|
|
|
tree->nodeBytesUsed += nodeSpace;
|
|
|
|
tree->nodeBytesFree -= nodeSpace;
|
|
|
|
++tree->numItems;
|
|
|
|
|
|
|
|
// Update max height of the tree if necessary
|
|
|
|
if (height > tree->maxHeight) {
|
|
|
|
tree->maxHeight = height;
|
|
|
|
}
|
|
|
|
|
2021-04-24 13:47:23 +08:00
|
|
|
deltatree_printf("insert(%s) done parent=%s\n",
|
|
|
|
k.toString().c_str(),
|
|
|
|
Cursor(cache, tree, parentIndex).toString().c_str());
|
|
|
|
deltatree_printf("insert(%s) done child=%s\n",
|
|
|
|
k.toString().c_str(),
|
|
|
|
Cursor(cache, tree, childIndex).toString().c_str());
|
|
|
|
|
2021-04-19 14:58:24 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool _hideDeletedBackward() {
|
2021-04-24 13:47:23 +08:00
|
|
|
while (nodeIndex != -1 && getDelta().getDeleted()) {
|
2021-04-19 14:58:24 +08:00
|
|
|
_movePrev();
|
|
|
|
}
|
2021-04-24 13:47:23 +08:00
|
|
|
return nodeIndex != -1;
|
2021-04-19 14:58:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool _hideDeletedForward() {
|
2021-04-24 13:47:23 +08:00
|
|
|
while (nodeIndex != -1 && getDelta().getDeleted()) {
|
2021-04-19 14:58:24 +08:00
|
|
|
_moveNext();
|
|
|
|
}
|
2021-04-24 13:47:23 +08:00
|
|
|
return nodeIndex != -1;
|
2021-04-19 14:58:24 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Returns number of bytes written
|
|
|
|
int build(int spaceAvailable, const T* begin, const T* end, const T* lowerBound, const T* upperBound) {
|
|
|
|
largeNodes = spaceAvailable > SmallSizeLimit;
|
|
|
|
int count = end - begin;
|
|
|
|
numItems = count;
|
|
|
|
nodeBytesDeleted = 0;
|
|
|
|
initialHeight = (uint8_t)log2(count) + 1;
|
|
|
|
maxHeight = 0;
|
|
|
|
|
|
|
|
// The boundary leading to the new page acts as the last time we branched right
|
|
|
|
if (count > 0) {
|
|
|
|
nodeBytesUsed = buildSubtree(
|
2021-04-24 13:47:23 +08:00
|
|
|
*root(), begin, end, lowerBound, upperBound, lowerBound->getCommonPrefixLen(*upperBound, 0));
|
2021-04-19 14:58:24 +08:00
|
|
|
} else {
|
|
|
|
nodeBytesUsed = 0;
|
|
|
|
}
|
|
|
|
nodeBytesFree = spaceAvailable - size();
|
|
|
|
return size();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int buildSubtree(Node& node,
|
|
|
|
const T* begin,
|
|
|
|
const T* end,
|
|
|
|
const T* leftParent,
|
|
|
|
const T* rightParent,
|
|
|
|
int subtreeCommon) {
|
|
|
|
|
|
|
|
int count = end - begin;
|
|
|
|
|
|
|
|
// Find key to be stored in root
|
|
|
|
int mid = perfectSubtreeSplitPointCached(count);
|
|
|
|
const T& item = begin[mid];
|
|
|
|
|
|
|
|
int commonWithPrev = item.getCommonPrefixLen(*leftParent, subtreeCommon);
|
|
|
|
int commonWithNext = item.getCommonPrefixLen(*rightParent, subtreeCommon);
|
|
|
|
|
|
|
|
bool prefixSourcePrev;
|
|
|
|
int commonPrefix;
|
|
|
|
const T* base;
|
|
|
|
if (commonWithPrev >= commonWithNext) {
|
|
|
|
prefixSourcePrev = true;
|
|
|
|
commonPrefix = commonWithPrev;
|
|
|
|
base = leftParent;
|
|
|
|
} else {
|
|
|
|
prefixSourcePrev = false;
|
|
|
|
commonPrefix = commonWithNext;
|
|
|
|
base = rightParent;
|
|
|
|
}
|
|
|
|
|
|
|
|
int deltaSize = item.writeDelta(node.delta(largeNodes), *base, commonPrefix);
|
|
|
|
node.delta(largeNodes).setPrefixSource(prefixSourcePrev);
|
|
|
|
|
|
|
|
// Continue writing after the serialized Delta.
|
|
|
|
uint8_t* wptr = (uint8_t*)&node.delta(largeNodes) + deltaSize;
|
|
|
|
|
|
|
|
int leftChildOffset;
|
|
|
|
// Serialize left subtree
|
|
|
|
if (count > 1) {
|
|
|
|
leftChildOffset = wptr - (uint8_t*)this;
|
|
|
|
deltatree_printf("%p: offset=%d count=%d serialize left subtree leftChildOffset=%d\n",
|
|
|
|
this,
|
|
|
|
nodeOffset(&node),
|
|
|
|
count,
|
|
|
|
leftChildOffset);
|
|
|
|
|
2021-04-24 13:47:23 +08:00
|
|
|
wptr += buildSubtree(*(Node*)wptr, begin, begin + mid, leftParent, &item, commonWithPrev);
|
2021-04-19 14:58:24 +08:00
|
|
|
} else {
|
|
|
|
leftChildOffset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rightChildOffset;
|
|
|
|
// Serialize right subtree
|
|
|
|
if (count > 2) {
|
|
|
|
rightChildOffset = wptr - (uint8_t*)this;
|
|
|
|
deltatree_printf("%p: offset=%d count=%d serialize right subtree rightChildOffset=%d\n",
|
|
|
|
this,
|
|
|
|
nodeOffset(&node),
|
|
|
|
count,
|
|
|
|
rightChildOffset);
|
|
|
|
|
2021-04-24 13:47:23 +08:00
|
|
|
wptr += buildSubtree(*(Node*)wptr, begin + mid + 1, end, &item, rightParent, commonWithNext);
|
2021-04-19 14:58:24 +08:00
|
|
|
} else {
|
|
|
|
rightChildOffset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
node.setLeftChildOffset(largeNodes, leftChildOffset);
|
|
|
|
node.setRightChildOffset(largeNodes, rightChildOffset);
|
|
|
|
|
|
|
|
deltatree_printf("%p: Serialized %s as %s\n", this, item.toString().c_str(), node.toString(this).c_str());
|
|
|
|
|
|
|
|
return wptr - (uint8_t*)&node;
|
|
|
|
}
|
|
|
|
};
|