Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SETNX command should return integer value #951

Merged
merged 8 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions libs/server/Resp/BasicCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -434,11 +434,17 @@ private bool NetworkSETNX<TGarnetApi>(bool highPrecision, ref TGarnetApi storage
{
Debug.Assert(parseState.Count == 2);
var key = parseState.GetArgSliceByRef(0);
var value = parseState.GetArgSliceByRef(1);
var getOption = ArgSlice.FromPinnedSpan(CmdStrings.NX);
parseState.InitializeWithArguments(key, value, getOption);
var sbKey = key.SpanByte;

return NetworkSETEXNX(ref storageApi);
var input = new RawStringInput(RespCommand.SETEXNX, ref parseState, startIdx: 1);
var status = storageApi.SET_Conditional(ref sbKey, ref input);

// The status returned for SETNX as NOTFOUND is the expected status in the happy path
var retVal = status == GarnetStatus.NOTFOUND ? 1 : 0;
while (!RespWriteUtils.TryWriteInt32(retVal, ref dcurr, dend))
SendAndReset();

return true;
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions test/Garnet.test/Resp/ACL/RespCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5369,8 +5369,8 @@ await CheckCommandsAsync(

async Task DoSetIfNotExistAsync(GarnetClient client)
{
string val = await client.ExecuteForStringResultAsync("SETNX", [$"foo-{keyIx++}", "bar"]);
ClassicAssert.AreEqual(val, "OK");
var val = await client.ExecuteForLongResultAsync("SETNX", [$"foo-{keyIx++}", "bar"]);
ClassicAssert.AreEqual(1, val);
}
}

Expand Down
1 change: 0 additions & 1 deletion test/Garnet.test/RespSetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1389,7 +1389,6 @@ public void CanDoSinterCardLC(string values1, string values2, string expectedCou
expectedResponse = ":0\r\n";
ClassicAssert.AreEqual(expectedResponse, response.AsSpan().Slice(0, expectedResponse.Length).ToArray());
}

#endregion


Expand Down
20 changes: 20 additions & 0 deletions test/Garnet.test/RespTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4581,6 +4581,26 @@ public void SetIfNotExistWithNewKey()
ClassicAssert.AreEqual(newValue, result);
}

[Test]
public void SetNXCorrectResponse()
{
var lightClientRequest = TestUtils.CreateRequest();

// Set key
var response = lightClientRequest.SendCommand("SETNX key1 2");
var expectedResponse = ":1\r\n";
ClassicAssert.AreEqual(expectedResponse, response.AsSpan().Slice(0, expectedResponse.Length).ToArray());

// Setnx command should fail since key exists
response = lightClientRequest.SendCommand("SETNX key1 3");
expectedResponse = ":0\r\n";
ClassicAssert.AreEqual(expectedResponse, response.AsSpan().Slice(0, expectedResponse.Length).ToArray());

// Be sure key wasn't modified
response = lightClientRequest.SendCommand("GET key1");
expectedResponse = "$1\r\n2\r\n";
ClassicAssert.AreEqual(expectedResponse, response.AsSpan().Slice(0, expectedResponse.Length).ToArray());
}
#endregion

#region SUBSTR
Expand Down
Loading