Skip to content

Commit e307c3e

Browse files
committed
Make ListReadKeysWithCount use idiomatic code instead of needless readHead and direct input parsing.
1 parent 15cbd16 commit e307c3e

File tree

2 files changed

+11
-45
lines changed

2 files changed

+11
-45
lines changed

libs/server/Resp/RespServerSession.cs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,43 +1188,6 @@ ReadOnlySpan<byte> GetUpperCaseCommand(out bool success)
11881188
return result;
11891189
}
11901190

1191-
public ArgSlice GetCommandAsArgSlice(out bool success)
1192-
{
1193-
if (bytesRead - readHead < 6)
1194-
{
1195-
success = false;
1196-
return default;
1197-
}
1198-
1199-
Debug.Assert(*(recvBufferPtr + readHead) == '$');
1200-
int psize = *(recvBufferPtr + readHead + 1) - '0';
1201-
readHead += 2;
1202-
while (*(recvBufferPtr + readHead) != '\r')
1203-
{
1204-
psize = psize * 10 + *(recvBufferPtr + readHead) - '0';
1205-
if (bytesRead - readHead < 1)
1206-
{
1207-
success = false;
1208-
return default;
1209-
}
1210-
readHead++;
1211-
}
1212-
if (bytesRead - readHead < 2 + psize + 2)
1213-
{
1214-
success = false;
1215-
return default;
1216-
}
1217-
Debug.Assert(*(recvBufferPtr + readHead + 1) == '\n');
1218-
1219-
var result = new ArgSlice(recvBufferPtr + readHead + 2, psize);
1220-
Debug.Assert(*(recvBufferPtr + readHead + 2 + psize) == '\r');
1221-
Debug.Assert(*(recvBufferPtr + readHead + 2 + psize + 1) == '\n');
1222-
1223-
readHead += 2 + psize + 2;
1224-
success = true;
1225-
return result;
1226-
}
1227-
12281191
/// <summary>
12291192
/// Attempt to kill this session.
12301193
///

libs/server/Transaction/TxnKeyManager.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ private int ListObjectKeys(RespCommand command)
378378
RespCommand.LINSERT => SingleKey(StoreType.Object, LockType.Exclusive),
379379
RespCommand.LLEN => SingleKey(StoreType.Object, LockType.Shared),
380380
RespCommand.LMOVE => ListKeys(2, StoreType.Object, LockType.Exclusive),
381-
RespCommand.LMPOP => ListReadKeysWithCount(LockType.Exclusive),
381+
RespCommand.LMPOP => ListReadKeysWithCount(LockType.Exclusive, mandatoryArgs: 1),
382382
RespCommand.LPOP => SingleKey(StoreType.Object, LockType.Exclusive),
383383
RespCommand.LPOS => SingleKey(StoreType.Object, LockType.Shared),
384384
RespCommand.LPUSH => SingleKey(StoreType.Object, LockType.Exclusive),
@@ -487,17 +487,20 @@ private int ListKeys(int inputCount, StoreType storeType, LockType type)
487487
/// <summary>
488488
/// Returns a list of keys for LMPOP command
489489
/// </summary>
490-
private int ListReadKeysWithCount(LockType type, bool isObject = true)
490+
private int ListReadKeysWithCount(LockType type, bool isObject = true, int mandatoryArgs = 0)
491491
{
492-
var numKeysArg = respSession.GetCommandAsArgSlice(out var success);
493-
if (!success) return -2;
492+
if (respSession.parseState.Count == 0)
493+
return -2;
494494

495-
if (!NumUtils.TryParse(numKeysArg.ReadOnlySpan, out int numKeys)) return -2;
495+
if (!respSession.parseState.TryGetInt(0, out var numKeys))
496+
return -2;
496497

497-
for (var i = 0; i < numKeys; i++)
498+
if (numKeys + mandatoryArgs + 1 > respSession.parseState.Count)
499+
return -2;
500+
501+
for (var i = 1; i < numKeys + 1; i++)
498502
{
499-
var key = respSession.GetCommandAsArgSlice(out success);
500-
if (!success) return -2;
503+
var key = respSession.parseState.GetArgSliceByRef(i);
501504
SaveKeyEntryToLock(key, isObject, type);
502505
SaveKeyArgSlice(key);
503506
}

0 commit comments

Comments
 (0)