Skip to content

Commit

Permalink
Fix SPOP bug
Browse files Browse the repository at this point in the history
Return nil when key exists and set is empty.
  • Loading branch information
vazois authored Mar 26, 2024
1 parent 5c516c5 commit 2740b49
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions libs/server/Objects/Set/SetObjectImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,21 @@ private void SetPop(byte* input, int length, ref SpanByteAndMemory output)
else if (count == int.MinValue) // no count parameter is present, we just pop and return a random item of the set
{
// Write a bulk string value of a random field from the hash value stored at key.
int index = RandomNumberGenerator.GetInt32(0, set.Count);
var item = set.ElementAt(index);
set.Remove(item);
this.UpdateSize(item, false);
while (!RespWriteUtils.WriteBulkString(item, ref curr, end))
ObjectUtils.ReallocateOutput(ref output, ref isMemory, ref ptr, ref ptrHandle, ref curr, ref end);
if (set.Count > 0)
{
int index = RandomNumberGenerator.GetInt32(0, set.Count);
var item = set.ElementAt(index);
set.Remove(item);
this.UpdateSize(item, false);
while (!RespWriteUtils.WriteBulkString(item, ref curr, end))
ObjectUtils.ReallocateOutput(ref output, ref isMemory, ref ptr, ref ptrHandle, ref curr, ref end);
}
else
{
// If set empty return nil
while (!RespWriteUtils.WriteResponse(CmdStrings.RESP_ERRNOTFOUND, ref curr, end))
ObjectUtils.ReallocateOutput(ref output, ref isMemory, ref ptr, ref ptrHandle, ref curr, ref end);
}
countDone++;
}

Expand Down

0 comments on commit 2740b49

Please sign in to comment.