Extract consumePartialKey to its own function

This commit is contained in:
2024-08-06 13:20:44 -07:00
parent ed5589e4ed
commit 051eb5919d

View File

@@ -2883,23 +2883,19 @@ checkMaxBetweenExclusiveImpl<true>(Node *n, int begin, int end,
InternalVersionT readVersion, ReadContext *);
#endif
// Returns a pointer to the newly inserted node. Caller must set
// `entryPresent`, and `entry` fields. All nodes along the search path of the
// result will have `maxVersion` set to `writeVersion` as a postcondition. Nodes
// along the search path may be invalidated.
[[nodiscard]] Node *insert(Node **self, std::span<const uint8_t> key,
// Consume the partial key of `self` if it exists, and update `self` and `key`
// such that `self` is along the search path of `key`, and has maxVersion
// `writeVersion`
void consumePartialKey(Node *&self, std::span<const uint8_t> &key,
InternalVersionT writeVersion, WriteContext *tls,
ConflictSet::Impl *impl) {
for (;; ++tls->accum.insert_iterations) {
if ((*self)->partialKeyLen > 0) {
if (self->partialKeyLen > 0) {
// Handle an existing partial key
int commonLen = std::min<int>((*self)->partialKeyLen, key.size());
int commonLen = std::min<int>(self->partialKeyLen, key.size());
int partialKeyIndex =
longestCommonPrefix((*self)->partialKey(), key.data(), commonLen);
if (partialKeyIndex < (*self)->partialKeyLen) {
auto *old = *self;
longestCommonPrefix(self->partialKey(), key.data(), commonLen);
if (partialKeyIndex < self->partialKeyLen) {
auto *old = self;
InternalVersionT oldMaxVersion =
exchangeMaxVersion(old, writeVersion, impl);
@@ -2912,8 +2908,7 @@ checkMaxBetweenExclusiveImpl<true>(Node *n, int begin, int end,
newSelf->entryPresent = false;
newSelf->numChildren = 1;
memcpy(newSelf->partialKey(), old->partialKey(),
newSelf->partialKeyLen);
memcpy(newSelf->partialKey(), old->partialKey(), newSelf->partialKeyLen);
uint8_t oldDistinguishingByte = old->partialKey()[partialKeyIndex];
old->parent = newSelf;
@@ -2921,7 +2916,7 @@ checkMaxBetweenExclusiveImpl<true>(Node *n, int begin, int end,
newSelf->index[0] = oldDistinguishingByte;
newSelf->children[0] = old;
newSelf->childMaxVersion[0] = oldMaxVersion;
*self = newSelf;
self = newSelf;
memmove(old->partialKey(), old->partialKey() + partialKeyIndex + 1,
old->partialKeyLen - (partialKeyIndex + 1));
@@ -2933,6 +2928,20 @@ checkMaxBetweenExclusiveImpl<true>(Node *n, int begin, int end,
}
key = key.subspan(partialKeyIndex, key.size() - partialKeyIndex);
}
}
// Returns a pointer to the newly inserted node. Caller must set
// `entryPresent`, and `entry` fields. All nodes along the search path of the
// result will have `maxVersion` set to `writeVersion` as a postcondition. Nodes
// along the search path may be invalidated.
[[nodiscard]]
Node *insert(Node **self, std::span<const uint8_t> key,
InternalVersionT writeVersion, WriteContext *tls,
ConflictSet::Impl *impl) {
for (;; ++tls->accum.insert_iterations) {
consumePartialKey(*self, key, writeVersion, tls, impl);
assert(maxVersion(*self, impl) <= writeVersion);
setMaxVersion(*self, impl, writeVersion);