Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
TalZaccai committed Sep 5, 2024
1 parent b1309af commit ea1e337
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 119 deletions.
5 changes: 3 additions & 2 deletions libs/server/Resp/ArrayCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ private bool NetworkSELECT()
return AbortWithWrongNumberOfArguments(nameof(RespCommand.SELECT));
}

// Read index
// Validate index
if (!parseState.TryGetInt(0, out var index))
{
while (!RespWriteUtils.WriteError(CmdStrings.RESP_ERR_GENERIC_VALUE_IS_NOT_INTEGER, ref dcurr, dend))
Expand Down Expand Up @@ -323,7 +323,7 @@ private bool NetworkSCAN<TGarnetApi>(ref TGarnetApi storageApi)
if (parseState.Count < 1)
return AbortWithWrongNumberOfArguments("SCAN");

// Scan cursor [MATCH pattern] [COUNT count] [TYPE type]
// Validate scan cursor
if (!parseState.TryGetLong(0, out var cursorFromInput))
{
while (!RespWriteUtils.WriteError(CmdStrings.RESP_ERR_GENERIC_INVALIDCURSOR, ref dcurr, dend))
Expand Down Expand Up @@ -352,6 +352,7 @@ private bool NetworkSCAN<TGarnetApi>(ref TGarnetApi storageApi)
}
else if (parameterWord.EqualsUpperCaseSpanIgnoringCase(CmdStrings.COUNT))
{
// Validate count
if (!parseState.TryGetLong(tokenIdx++, out countValue))
{
while (!RespWriteUtils.WriteError(CmdStrings.RESP_ERR_GENERIC_VALUE_IS_NOT_INTEGER, ref dcurr, dend))
Expand Down
7 changes: 7 additions & 0 deletions libs/server/Resp/BasicCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ private bool NetworkSetRange<TGarnetApi>(ref TGarnetApi storageApi)
{
var key = parseState.GetArgSliceByRef(0);

// Validate offset
if (!parseState.TryGetInt(1, out var offset))
{
while (!RespWriteUtils.WriteError(CmdStrings.RESP_ERR_GENERIC_VALUE_IS_NOT_INTEGER, ref dcurr, dend))
Expand Down Expand Up @@ -269,6 +270,7 @@ private bool NetworkGetRange<TGarnetApi>(ref TGarnetApi storageApi)
var key = parseState.GetArgSliceByRef(0);
var sbKey = key.SpanByte;

// Validate range
if (!parseState.TryGetInt(1, out _) || !parseState.TryGetInt(2, out _))
{
while (!RespWriteUtils.WriteError(CmdStrings.RESP_ERR_GENERIC_VALUE_IS_NOT_INTEGER, ref dcurr, dend))
Expand Down Expand Up @@ -314,6 +316,7 @@ private bool NetworkSETEX<TGarnetApi>(bool highPrecision, ref TGarnetApi storage
{
var key = parseState.GetArgSliceByRef(0).SpanByte;

// Validate expiry
if (!parseState.TryGetInt(1, out var expiry))
{
while (!RespWriteUtils.WriteError(CmdStrings.RESP_ERR_GENERIC_VALUE_IS_NOT_INTEGER, ref dcurr, dend))
Expand Down Expand Up @@ -395,6 +398,7 @@ private bool NetworkSETEXNX<TGarnetApi>(ref TGarnetApi storageApi)

if (nextOpt.SequenceEqual(CmdStrings.EX))
{
// Validate expiry
if (!parseState.TryGetInt(tokenIdx++, out expiry))
{
errorMessage = CmdStrings.RESP_ERR_GENERIC_VALUE_IS_NOT_INTEGER;
Expand All @@ -416,6 +420,7 @@ private bool NetworkSETEXNX<TGarnetApi>(ref TGarnetApi storageApi)
}
else if (nextOpt.SequenceEqual(CmdStrings.PX))
{
// Validate expiry
if (!parseState.TryGetInt(tokenIdx++, out expiry))
{
errorMessage = CmdStrings.RESP_ERR_GENERIC_VALUE_IS_NOT_INTEGER;
Expand Down Expand Up @@ -1021,6 +1026,7 @@ private bool NetworkHELLO()
if (count > 0)
{
var tokenIdx = 0;
// Validate protocol version
if (!parseState.TryGetInt(tokenIdx++, out var localRespProtocolVersion))
{
while (!RespWriteUtils.WriteError(CmdStrings.RESP_ERR_PROTOCOL_VALUE_IS_NOT_INTEGER, ref dcurr, dend))
Expand Down Expand Up @@ -1172,6 +1178,7 @@ private bool NetworkMemoryUsage<TGarnetApi>(ref TGarnetApi storageApi)
return true;
}

// Validate samples count
if (!parseState.TryGetInt(2, out _))
{
while (!RespWriteUtils.WriteError(CmdStrings.RESP_ERR_GENERIC_VALUE_IS_NOT_INTEGER, ref dcurr, dend))
Expand Down
23 changes: 12 additions & 11 deletions libs/server/Resp/Bitmap/BitmapCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private bool NetworkStringSetBit<TGarnetApi>(ref TGarnetApi storageApi)
return true;
}

var inputHeader = new RawStringInput
var input = new RawStringInput
{
header = new RespInputHeader { cmd = RespCommand.SETBIT },
parseState = parseState,
Expand All @@ -160,7 +160,7 @@ private bool NetworkStringSetBit<TGarnetApi>(ref TGarnetApi storageApi)
var o = new SpanByteAndMemory(dcurr, (int)(dend - dcurr));
var status = storageApi.StringSetBit(
ref sbKey,
ref inputHeader,
ref input,
ref o);

if (status == GarnetStatus.OK)
Expand Down Expand Up @@ -191,15 +191,15 @@ private bool NetworkStringGetBit<TGarnetApi>(ref TGarnetApi storageApi)
return true;
}

var inputHeader = new RawStringInput
var input = new RawStringInput
{
header = new RespInputHeader { cmd = RespCommand.GETBIT },
parseState = parseState,
parseStateStartIdx = 1,
};

var o = new SpanByteAndMemory(dcurr, (int)(dend - dcurr));
var status = storageApi.StringGetBit(ref sbKey, ref inputHeader, ref o);
var status = storageApi.StringGetBit(ref sbKey, ref input, ref o);

if (status == GarnetStatus.NOTFOUND)
while (!RespWriteUtils.WriteDirect(CmdStrings.RESP_RETURN_VAL_0, ref dcurr, dend))
Expand Down Expand Up @@ -238,7 +238,7 @@ private bool NetworkStringBitCount<TGarnetApi>(ref TGarnetApi storageApi)
}
}

var inputHeader = new RawStringInput
var input = new RawStringInput
{
header = new RespInputHeader { cmd = RespCommand.BITCOUNT },
parseState = parseState,
Expand All @@ -247,7 +247,7 @@ private bool NetworkStringBitCount<TGarnetApi>(ref TGarnetApi storageApi)

var o = new SpanByteAndMemory(dcurr, (int)(dend - dcurr));

var status = storageApi.StringBitCount(ref sbKey, ref inputHeader, ref o);
var status = storageApi.StringBitCount(ref sbKey, ref input, ref o);

if (status == GarnetStatus.OK)
{
Expand Down Expand Up @@ -317,7 +317,7 @@ private bool NetworkStringBitPosition<TGarnetApi>(ref TGarnetApi storageApi)
}
}

var inputHeader = new RawStringInput
var input = new RawStringInput
{
header = new RespInputHeader { cmd = RespCommand.BITPOS },
parseState = parseState,
Expand All @@ -326,7 +326,7 @@ private bool NetworkStringBitPosition<TGarnetApi>(ref TGarnetApi storageApi)

var o = new SpanByteAndMemory(dcurr, (int)(dend - dcurr));

var status = storageApi.StringBitPosition(ref sbKey, ref inputHeader, ref o);
var status = storageApi.StringBitPosition(ref sbKey, ref input, ref o);

if (status == GarnetStatus.OK)
{
Expand Down Expand Up @@ -415,6 +415,7 @@ private bool StringBitField<TGarnetApi>(ref TGarnetApi storageApi, bool readOnly
overflowTypeSlice = parseState.GetArgSliceByRef(currTokenIdx);
isOverflowTypeSet = true;

// Validate overflow type
if (!parseState.TryGetEnum(currTokenIdx, true, out BitFieldOverflow _))
{
while (!RespWriteUtils.WriteError(
Expand Down Expand Up @@ -510,7 +511,7 @@ private bool StringBitField<TGarnetApi>(ref TGarnetApi storageApi, bool readOnly
while (!RespWriteUtils.WriteArrayLength(secondaryCommandArgs.Count, ref dcurr, dend))
SendAndReset();

var inputHeader = new RawStringInput
var input = new RawStringInput
{
header = new RespInputHeader { cmd = RespCommand.BITFIELD },
parseStateStartIdx = 0,
Expand All @@ -535,10 +536,10 @@ private bool StringBitField<TGarnetApi>(ref TGarnetApi storageApi, bool readOnly
secParseStateBuffer[^1] = overflowTypeSlice;
}

inputHeader.parseState = secParseState;
input.parseState = secParseState;

var output = new SpanByteAndMemory(dcurr, (int)(dend - dcurr));
var status = storageApi.StringBitField(ref sbKey, ref inputHeader, opCode,
var status = storageApi.StringBitField(ref sbKey, ref input, opCode,
ref output);

if (status == GarnetStatus.NOTFOUND && opCode == (byte)RespCommand.GET)
Expand Down
11 changes: 0 additions & 11 deletions libs/server/Resp/Bitmap/BitmapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,6 @@ public static byte UpdateBitmap(byte* value, long offset, byte set)
return oldVal;
}

/// <summary>
/// Get bit value from value ptr at offset specified at input ptr.
/// </summary>
/// <param name="input"></param>
/// <param name="value"></param>
/// <param name="valLen"></param>
public static byte GetBit(byte* input, byte* value, int valLen)
{
return GetBit(*(long*)(input), value, valLen);
}

/// <summary>
/// Get bit value from value ptr at offset specified at offset.
/// </summary>
Expand Down
16 changes: 0 additions & 16 deletions libs/server/Resp/Bitmap/BitmapManagerBitCount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,6 @@ private static long BitIndexCount(byte* value, long startOffset, long endOffset)
BitIndexCount(value[endByte], endBitOffset: rightBitIndex);
}

/// <summary>
/// Main driver of BitCount Command.
/// </summary>
/// <param name="input">Command input containing startOffset,endOffset and offsetType (i.e. Bit, Byte)/</param>
/// <param name="value">Value containing bits to count.</param>
/// <param name="valLen">Value length</param>
/// <returns>Integer count of all bits set to one.</returns>
public static long BitCountDriver(byte* input, byte* value, int valLen)
{
var startOffset = *(long*)(input);
var endOffset = *(long*)(input + sizeof(long));
var offsetType = *(input + sizeof(long) * 2);

return BitCountDriver(startOffset, endOffset, offsetType, value, valLen);
}

/// <summary>
/// Main driver of BitCount Command.
/// </summary>
Expand Down
22 changes: 0 additions & 22 deletions libs/server/Resp/Bitmap/BitmapManagerBitPos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,6 @@ private static long BitPosIndexBitSearch(byte* value, byte bSetVal, long offset
return (long)Lzcnt.X64.LeadingZeroCount((ulong)payload);
}

/// <summary>
/// Main driver for bit position command.
/// </summary>
/// <param name="input">Input properties for bitmap operation.</param>
/// <param name="value">Pointer to start of bitmap.</param>
/// <param name="valLen">Length of bitmap.</param>
/// <returns></returns>
public static long BitPosDriver(byte* input, byte* value, int valLen)
{
//4 byte: length
//1 byte: op-code
//1 byte: setVal
//4 byte: startOffset // offset are byte indices not bits, therefore int is sufficient because max will be at most offset >> 3
//4 byte: endOffset
var bSetVal = *(input);
var startOffset = *(long*)(input + sizeof(byte));
var endOffset = *(long*)(input + sizeof(byte) + sizeof(long));
var offsetType = *(input + sizeof(byte) + sizeof(long) * 2);

return BitPosDriver(bSetVal, startOffset, endOffset, offsetType, value, valLen);
}

/// <summary>
/// Main driver for bit position command.
/// </summary>
Expand Down
57 changes: 0 additions & 57 deletions libs/server/Resp/Bitmap/BitmapManagerBitfield.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,33 +49,6 @@ public static int LengthFromType(BitFieldCmdArgs args)
return LengthInBytes(offset + bitCount);
}

/// <summary>
/// Check if bitmap is large enough to apply bitfield op.
/// </summary>
/// <param name="input">Command input parameters.</param>
/// <param name="vlen">Length of bitfield value.</param>
/// <returns>True if need to grow value otherwise false.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsLargeEnoughForType(byte* input, int vlen)
{
int len = LengthFromType(input);
return len <= vlen;
}

/// <summary>
/// Length in bytes based on offset calculated as raw bit offset or from typeInfo bitCount.
/// </summary>
/// <param name="input">Command input parameters.</param>
/// <returns>Integer number of bytes required to perform bitfield op.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int LengthFromType(byte* input)
{
long offset = GetBitFieldOffset(input);
byte bitCount = (byte)(GetBitFieldType(input) & 0x7F);
int len = LengthInBytes(offset + bitCount);
return len;
}

/// <summary>
/// Get allocation size for bitfield command.
/// </summary>
Expand Down Expand Up @@ -476,36 +449,6 @@ private static (long, bool) CheckSignedBitfieldOverflow(long value, long incrBy,
return (0, true);
}

/// <summary>
/// Execute bitfield operation described at input on bitmap stored within value.
/// </summary>
/// <param name="input"></param>
/// <param name="value"></param>
/// <param name="valLen"></param>
/// <returns></returns>
public static (long, bool) BitFieldExecute(byte* input, byte* value, int valLen)
{
var secondaryOpCode = GetBitFieldSecondaryOp(input);
var typeInfo = GetBitFieldType(input);
var bitCount = (byte)(typeInfo & 0x7F);
var offset = GetBitFieldOffset(input);
var overflowType = GetBitFieldOverflowType(input);

switch (secondaryOpCode)
{
case (byte)RespCommand.SET:
var newVal = GetBitFieldValue(input);
return SetBitfieldValue(value, valLen, offset, bitCount, typeInfo, newVal, overflowType);
case (byte)RespCommand.INCRBY:
var incrByValue = GetBitFieldValue(input);
return IncrByBitfieldValue(value, valLen, offset, bitCount, typeInfo, incrByValue, overflowType);
case (byte)RespCommand.GET:
return (GetBitfieldValue(value, valLen, offset, bitCount, typeInfo), false);
default:
throw new GarnetException("BITFIELD secondary op not supported");
}
}

/// <summary>
/// Execute bitfield operation described at input on bitmap stored within value.
/// </summary>
Expand Down

0 comments on commit ea1e337

Please sign in to comment.