Skip to content

Commit

Permalink
Fix HashOperation.HMSET Cannot Set On Existing Data (#172)
Browse files Browse the repository at this point in the history
* Fix HashOperation.HMSET Cannot Set On Existing Key

* Add CanDoHMSetMultipleTimes Test
  • Loading branch information
KimJuHeok authored Apr 1, 2024
1 parent 6aef208 commit ec721bc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion libs/server/Objects/Hash/HashObjectImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ private void SetOrSetNX(HashOperation op, byte* input, int length, byte* output)
this.UpdateSize(key, value);
_output->opsDone++;
}
else if (_input->header.HashOp == HashOperation.HSET && _value != default && !_value.SequenceEqual(value))
else if ((_input->header.HashOp == HashOperation.HSET || _input->header.HashOp == HashOperation.HMSET) && _value != default && !_value.SequenceEqual(value))
{
hash[key] = value;
this.Size += Utility.RoundUp(value.Length, IntPtr.Size) - Utility.RoundUp(_value.Length, IntPtr.Size); // Skip overhead as existing item is getting replaced.
Expand Down
24 changes: 24 additions & 0 deletions test/Garnet.test/RespHashTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,30 @@ public async Task CanDoHGETALL()
Assert.AreEqual("abc", result["foo"]);
Assert.AreEqual("def", result["bar"]);
}
[Test]
public async Task CanDoHMSETMultipleTimes()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
var hashKey = "testCanDoHMSET";
var hashMapKey = "TestKey";

await db.KeyDeleteAsync(hashKey);

var val0 = await db.HashGetAsync(hashKey, hashMapKey);
await db.HashSetAsync(hashKey, [new HashEntry(hashMapKey, "TestValue1")]);

var val1 = await db.HashGetAsync(hashKey, hashMapKey);
await db.HashSetAsync(hashKey, [new HashEntry(hashMapKey, "TestValue2")]);

var val2 = await db.HashGetAsync(hashKey, hashMapKey);

#nullable enable
Assert.Null((string?)val0);
Assert.AreEqual("TestValue1", (string?)val1);
Assert.AreEqual("TestValue2", (string?)val2);
#nullable disable
}

[Test]
public async Task CanDoHSETWhenAlwaysAsync()
Expand Down

0 comments on commit ec721bc

Please sign in to comment.