Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
s3w3nofficial committed Jan 16, 2025
1 parent eed596f commit 85d0136
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
2 changes: 1 addition & 1 deletion libs/common/RespLengthEncodingUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static class RespLengthEncodingUtils
/// <param name="buff"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static (long length, byte payloadStart) DecodeLength(ref ReadOnlySpan<byte> buff)
public static (long Length, byte PayloadStart) DecodeLength(ref ReadOnlySpan<byte> buff)
{
// remove the value type byte
var encoded = buff.Slice(1);
Expand Down
33 changes: 18 additions & 15 deletions libs/server/Resp/KeyAdminCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

using System;
using System.Buffers;
using System.Diagnostics;
using Garnet.common;
using Garnet.server.Auth;
Expand Down Expand Up @@ -43,12 +44,11 @@ bool NetworkRESTORE<TGarnetApi>(ref TGarnetApi storageApi)

// return error if key already exists
var keyExists = storageApi.EXISTS(key);
switch (keyExists)
if (keyExists is GarnetStatus.OK)
{
case GarnetStatus.OK:
while (!RespWriteUtils.WriteDirect(CmdStrings.RESP_ERR_KEY_ALREADY_EXISTS, ref dcurr, dend))
SendAndReset();
return true;
while (!RespWriteUtils.WriteDirect(CmdStrings.RESP_ERR_KEY_ALREADY_EXISTS, ref dcurr, dend))
SendAndReset();
return true;
}

var valueSpan = value.ReadOnlySpan;
Expand Down Expand Up @@ -128,24 +128,27 @@ bool NetworkDUMP<TGarnetApi>(ref TGarnetApi storageApi)

var status = storageApi.GET(key, out var value);

switch (status)
if (status is GarnetStatus.NOTFOUND)
{
case GarnetStatus.NOTFOUND:
while (!RespWriteUtils.WriteDirect(CmdStrings.RESP_ERRNOTFOUND, ref dcurr, dend))
SendAndReset();
return true;
while (!RespWriteUtils.WriteDirect(CmdStrings.RESP_ERRNOTFOUND, ref dcurr, dend))
SendAndReset();
return true;
}

var encodedLength = RespLengthEncodingUtils.EncodeLength(value.ReadOnlySpan.Length);

// Len of the dump (payload type + redis encoded payload len + payload len + rdb version + crc64)
var len = 1 + encodedLength.Length + value.ReadOnlySpan.Length + 2 + 8;
var lengthInASCIIBytes = new Span<byte>(new byte[NumUtils.NumDigitsInLong(len)]);
Span<byte> lengthInASCIIBytes = stackalloc byte[NumUtils.NumDigitsInLong(len)];
var lengthInASCIIBytesLen = NumUtils.LongToSpanByte(len, lengthInASCIIBytes);

// Total len (% + length of ascii bytes + CR LF + payload type + redis encoded payload len + payload len + rdb version + crc64 + CR LF)
var totalLength = 1 + lengthInASCIIBytesLen + 2 + 1 + encodedLength.Length + value.ReadOnlySpan.Length + 2 + 8 + 2;
Span<byte> buffer = stackalloc byte[totalLength];

var buffer = totalLength <= 128
? stackalloc byte[totalLength]
: new byte[totalLength];

var offset = 0;

// Write RESP bulk string prefix and length
Expand All @@ -159,8 +162,8 @@ bool NetworkDUMP<TGarnetApi>(ref TGarnetApi storageApi)
buffer[offset++] = 0x00;

// length of the span
foreach (var b in encodedLength)
buffer[offset++] = b;
encodedLength.CopyTo(buffer[offset..]);
offset += encodedLength.Length;

// copy value to buffer
value.ReadOnlySpan.CopyTo(buffer[offset..]);
Expand All @@ -182,7 +185,7 @@ bool NetworkDUMP<TGarnetApi>(ref TGarnetApi storageApi)
buffer[offset++] = 0x0D; // CR
buffer[offset++] = 0x0A; // LF

while (!RespWriteUtils.WriteDirect(buffer, ref dcurr, dend))
while (!RespWriteUtils.WriteDirect(buffer.Slice(0, totalLength), ref dcurr, dend))
SendAndReset();

return true;
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 @@ -6439,8 +6439,8 @@ async Task DoRestoreAsync(GarnetClient client)
var val = await client.ExecuteForStringResultAsync(
"$7\r\nRESTORE\r\n"u8.ToArray(),
[
Encoding.UTF8.GetBytes($"foo-{count}"),
"0"u8.ToArray(),
Encoding.UTF8.GetBytes($"foo-{count}"),
"0"u8.ToArray(),
payload
]);

Expand Down

0 comments on commit 85d0136

Please sign in to comment.