Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
badrishc committed Oct 9, 2024
1 parent 57b8968 commit e1e5a7e
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions libs/server/Resp/BasicCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -535,12 +535,6 @@ private bool NetworkSETEXNX<TGarnetApi>(ref TGarnetApi storageApi)
return true;
}

// Make space for key header
var keyPtr = sbKey.ToPointer() - sizeof(int);

// Set key length
*(int*)keyPtr = sbKey.Length;

// Make space for value header
var valPtr = sbVal.ToPointer() - sizeof(int);
var vSize = sbVal.Length;
Expand All @@ -553,15 +547,15 @@ private bool NetworkSETEXNX<TGarnetApi>(ref TGarnetApi storageApi)
{
case ExistOptions.None:
return getValue
? NetworkSET_Conditional(RespCommand.SET, expiry, keyPtr, valPtr, vSize, true,
? NetworkSET_Conditional(RespCommand.SET, expiry, ref sbKey, valPtr, vSize, true,
false, ref storageApi)
: NetworkSET_EX(RespCommand.SET, expiry, keyPtr, valPtr, vSize, false,
: NetworkSET_EX(RespCommand.SET, expiry, ref sbKey, valPtr, vSize, false,
ref storageApi); // Can perform a blind update
case ExistOptions.XX:
return NetworkSET_Conditional(RespCommand.SETEXXX, expiry, keyPtr, valPtr, vSize,
return NetworkSET_Conditional(RespCommand.SETEXXX, expiry, ref sbKey, valPtr, vSize,
getValue, false, ref storageApi);
case ExistOptions.NX:
return NetworkSET_Conditional(RespCommand.SETEXNX, expiry, keyPtr, valPtr, vSize,
return NetworkSET_Conditional(RespCommand.SETEXNX, expiry, ref sbKey, valPtr, vSize,
getValue, false, ref storageApi);
}

Expand All @@ -571,15 +565,15 @@ private bool NetworkSETEXNX<TGarnetApi>(ref TGarnetApi storageApi)
{
case ExistOptions.None:
return getValue
? NetworkSET_Conditional(RespCommand.SET, expiry, keyPtr, valPtr, vSize, true,
? NetworkSET_Conditional(RespCommand.SET, expiry, ref sbKey, valPtr, vSize, true,
true, ref storageApi)
: NetworkSET_EX(RespCommand.SET, expiry, keyPtr, valPtr, vSize, true,
: NetworkSET_EX(RespCommand.SET, expiry, ref sbKey, valPtr, vSize, true,
ref storageApi); // Can perform a blind update
case ExistOptions.XX:
return NetworkSET_Conditional(RespCommand.SETEXXX, expiry, keyPtr, valPtr, vSize,
return NetworkSET_Conditional(RespCommand.SETEXXX, expiry, ref sbKey, valPtr, vSize,
getValue, true, ref storageApi);
case ExistOptions.NX:
return NetworkSET_Conditional(RespCommand.SETEXNX, expiry, keyPtr, valPtr, vSize,
return NetworkSET_Conditional(RespCommand.SETEXNX, expiry, ref sbKey, valPtr, vSize,
getValue, true, ref storageApi);
}

Expand All @@ -591,13 +585,13 @@ private bool NetworkSETEXNX<TGarnetApi>(ref TGarnetApi storageApi)
{
case ExistOptions.None:
// We can never perform a blind update due to KEEPTTL
return NetworkSET_Conditional(RespCommand.SETKEEPTTL, expiry, keyPtr, valPtr, vSize,
return NetworkSET_Conditional(RespCommand.SETKEEPTTL, expiry, ref sbKey, valPtr, vSize,
getValue, false, ref storageApi);
case ExistOptions.XX:
return NetworkSET_Conditional(RespCommand.SETKEEPTTLXX, expiry, keyPtr, valPtr, vSize,
return NetworkSET_Conditional(RespCommand.SETKEEPTTLXX, expiry, ref sbKey, valPtr, vSize,
getValue, false, ref storageApi);
case ExistOptions.NX:
return NetworkSET_Conditional(RespCommand.SETEXNX, expiry, keyPtr, valPtr, vSize,
return NetworkSET_Conditional(RespCommand.SETEXNX, expiry, ref sbKey, valPtr, vSize,
getValue, false, ref storageApi);
}

Expand All @@ -609,7 +603,7 @@ private bool NetworkSETEXNX<TGarnetApi>(ref TGarnetApi storageApi)
return true;
}

private bool NetworkSET_EX<TGarnetApi>(RespCommand cmd, int expiry, byte* keyPtr, byte* valPtr,
private bool NetworkSET_EX<TGarnetApi>(RespCommand cmd, int expiry, ref SpanByte key, byte* valPtr,
int vsize, bool highPrecision, ref TGarnetApi storageApi)
where TGarnetApi : IGarnetApi
{
Expand All @@ -630,19 +624,20 @@ private bool NetworkSET_EX<TGarnetApi>(RespCommand cmd, int expiry, byte* keyPtr
: TimeSpan.FromSeconds(expiry).Ticks);
}

storageApi.SET(ref Unsafe.AsRef<SpanByte>(keyPtr), ref Unsafe.AsRef<SpanByte>(valPtr));
storageApi.SET(ref key, ref Unsafe.AsRef<SpanByte>(valPtr));
while (!RespWriteUtils.WriteDirect(CmdStrings.RESP_OK, ref dcurr, dend))
SendAndReset();
return true;
}

private bool NetworkSET_Conditional<TGarnetApi>(RespCommand cmd, int expiry, byte* keyPtr,
private bool NetworkSET_Conditional<TGarnetApi>(RespCommand cmd, int expiry, ref SpanByte key,
byte* inputPtr, int isize, bool getValue, bool highPrecision, ref TGarnetApi storageApi)
where TGarnetApi : IGarnetApi
{
// Make space for RespCommand in input
inputPtr -= RespInputHeader.Size;

var save = *(long*)inputPtr;
if (expiry == 0) // no expiration provided
{
*(int*)inputPtr = RespInputHeader.Size + isize;
Expand Down Expand Up @@ -670,7 +665,7 @@ private bool NetworkSET_Conditional<TGarnetApi>(RespCommand cmd, int expiry, byt
if (getValue)
{
var o = new SpanByteAndMemory(dcurr, (int)(dend - dcurr));
var status = storageApi.SET_Conditional(ref Unsafe.AsRef<SpanByte>(keyPtr),
var status = storageApi.SET_Conditional(ref key,
ref Unsafe.AsRef<SpanByte>(inputPtr), ref o);

// Status tells us whether an old image was found during RMW or not
Expand All @@ -690,7 +685,7 @@ private bool NetworkSET_Conditional<TGarnetApi>(RespCommand cmd, int expiry, byt
}
else
{
var status = storageApi.SET_Conditional(ref Unsafe.AsRef<SpanByte>(keyPtr),
var status = storageApi.SET_Conditional(ref key,
ref Unsafe.AsRef<SpanByte>(inputPtr));

bool ok = status != GarnetStatus.NOTFOUND;
Expand All @@ -711,7 +706,7 @@ private bool NetworkSET_Conditional<TGarnetApi>(RespCommand cmd, int expiry, byt
SendAndReset();
}
}

*(long*)inputPtr = save;
return true;
}

Expand Down

0 comments on commit e1e5a7e

Please sign in to comment.