Skip to content

Commit

Permalink
reuse code smartly
Browse files Browse the repository at this point in the history
  • Loading branch information
hamdaankhalidmsft committed Dec 20, 2024
1 parent 003a660 commit 4d38f77
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 17 deletions.
14 changes: 2 additions & 12 deletions libs/server/Storage/Functions/MainStore/RMWMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,7 @@ private bool InPlaceUpdaterWorker(ref SpanByte key, ref RawStringInput input, re

if (oldEtag != etagFromClient)
{
// write back array of the format [etag, value]
var valueToWrite = value.AsReadOnlySpan(etagIgnoredOffset);
var digitsInLenOfValue = NumUtils.NumDigitsInLong(valueToWrite.Length);
// *2\r\n: + <numDigitsInEtag> + \r\n + $ + <digitsInLenOfValue> + \r\n + <valueToWrite.Length> + \r\n
var numDigitsInEtag = NumUtils.NumDigitsInLong(oldEtag);
WriteValAndEtagToDst(4 + 1 + numDigitsInEtag + 2 + 1 + digitsInLenOfValue + 2 + valueToWrite.Length + 2, ref valueToWrite, oldEtag, ref output, writeDirect: false);
CopyRespToWithInput(ref input, ref value, ref output, isFromPending: false, etagIgnoredOffset, etagIgnoredEnd, hasEtagInVal: recordInfo.ETag);
return true;
}

Expand Down Expand Up @@ -786,12 +781,7 @@ public bool NeedCopyUpdate(ref SpanByte key, ref RawStringInput input, ref SpanB

if (existingEtag != etagToCheckWith)
{
// write back array of the format [etag, value]
var valueToWrite = oldValue.AsReadOnlySpan(etagIgnoredOffset);
var digitsInLenOfValue = NumUtils.NumDigitsInLong(valueToWrite.Length);
// *2\r\n: + <numDigitsInEtag> + \r\n + $ + <digitsInLenOfValue> + \r\n <valueToWrite.Length> + \r\n
var numDigitsInEtag = NumUtils.NumDigitsInLong(existingEtag);
WriteValAndEtagToDst(4 + 1 + numDigitsInEtag + 2 + 1 + digitsInLenOfValue + 2 + valueToWrite.Length + 2, ref valueToWrite, existingEtag, ref output, writeDirect: false);
CopyRespToWithInput(ref input, ref oldValue, ref output, isFromPending: false, etagIgnoredOffset, etagIgnoredEnd, hasEtagInVal: rmwInfo.RecordInfo.ETag);
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions test/Garnet.test/RespEtagTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void SetIfMatchWorksWithExpiration()
// TTL should be retained
ttl = db.KeyTimeToLive(key);
ClassicAssert.IsTrue(ttl.HasValue);

db.KeyDelete(key); // cleanup

// Scenario: Key existed before and had expiration
Expand Down Expand Up @@ -207,7 +207,7 @@ public void SetIfMatchWorksWithExpiration()

// confirm expiration retained -> TTL should exist
ttl = db.KeyTimeToLive(key);
ClassicAssert.IsTrue(ttl.HasValue);
ClassicAssert.IsTrue(ttl.HasValue);
}

#endregion
Expand Down
5 changes: 2 additions & 3 deletions website/docs/commands/garnet-specific.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ One of the following:
#### **Syntax**

```bash
SETIFMATCH key value etag
SETIFMATCH key value etag [EX || PX expiration]
```

Updates the value of a key if the provided ETag matches the current ETag of the key.
Expand All @@ -164,9 +164,8 @@ Updates the value of a key if the provided ETag matches the current ETag of the

One of the following:

- **Integer reply**: The updated ETag if the value was successfully updated.
- **Array reply**: The updated ETag if the value was successfully updated, and no a
- **Nil reply**: If the key does not exist.
- **Simple string reply**: If the provided ETag does not match the current ETag returns VAL_NOT_FOUND

---

Expand Down

0 comments on commit 4d38f77

Please sign in to comment.