Skip to content

Commit

Permalink
RavenDB-21488 Fixing handling of posting list removal when the old te…
Browse files Browse the repository at this point in the history
…rm and the new term don't have the same frequency
  • Loading branch information
ayende committed Sep 29, 2023
1 parent 9160dcf commit db3d209
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/Voron/Data/PostingLists/NativeIntegersList.cs
Expand Up @@ -127,8 +127,6 @@ public void SortAndRemoveDuplicatesAndRemovals()

var bufferPtr = outputBufferPtr;
var bufferEndPtr = bufferPtr + Count - 1;
Debug.Assert((*bufferPtr & 1) == 0,
"Removal as first item means that we have an orphaned removal, not supposed to happen!");
while (bufferPtr < bufferEndPtr)
{
// here we check equality without caring if this is removal or not, skipping moving
Expand All @@ -141,8 +139,21 @@ public void SortAndRemoveDuplicatesAndRemovals()

bufferPtr++;
}

Count = (int)(outputBufferPtr - RawItems + 1);
var count = (int)(outputBufferPtr - RawItems + 1);

// Need to handle scenario where we have removal without addition, which can happen if the frequency
// is different, so we'll remove the item with the old frequency and add the new one
// This code assumes that the branch predictor would work well here, since those are fairly rare scenarios
var idx = 0;
outputBufferPtr = RawItems;
for (int i = 0; i < count; i++)
{
if ((outputBufferPtr[i] & 1) != 0)
continue;

outputBufferPtr[idx++] = outputBufferPtr[i];
}
Count = idx;
}

public int MoveTo(Span<long> matches)
Expand Down

0 comments on commit db3d209

Please sign in to comment.