-
Notifications
You must be signed in to change notification settings - Fork 543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Compatibility] Added COMMAND GETKEYS and GETKEYSANDFLAGS command #888
base: main
Are you sure you want to change the base?
[Compatibility] Added COMMAND GETKEYS and GETKEYSANDFLAGS command #888
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for contributing this! Added a few comments.
return true; | ||
} | ||
|
||
private void ExtractKeys(RespCommandKeySpecification spec, List<byte[]> keyList) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I would probably do is have this as an extension method to SessionParseState (e.g. TryGetKeysFromKeySpecs) that takes an array of RespCommandKeySpecifications.
Then the internal implementation of extracting the keys from a single key spec could a class method inside RespCommandKeySpecification.
{ | ||
int startIndex = 0; | ||
|
||
if (spec.BeginSearch is BeginSearchIndex bsIndex) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can take advantage of the OO design here and handle each class-specific scenario in the class implementation itself.
{ | ||
// Handle negative StartFrom by converting to positive index from end | ||
int searchStartIndex = bsKeyword.StartFrom; | ||
if (searchStartIndex < 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can probably avoid having 2 loops that do the same thing here, just have a condition for the index if the order is descending (this path is probably not as performance-critical).
// Search backwards from the calculated start position for the keyword | ||
for (int i = searchStartIndex; i >= 0; i--) | ||
{ | ||
if (parseState.GetArgSliceByRef(i).ReadOnlySpan.EqualsUpperCaseSpanIgnoringCase(Encoding.ASCII.GetBytes(bsKeyword.Keyword))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can store Encoding.ASCII.GetBytes(bsKeyword.Keyword) prior to the loop instead of re-evaluating it each time.
return AbortWithErrorMessage(CmdStrings.RESP_COMMAND_HAS_NO_KEY_ARGS); | ||
} | ||
|
||
var keyList = new List<byte[]>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we have to allocate a byte[] for each key? The keys already exist in the parse state.
return AbortWithWrongNumberOfArguments(nameof(RespCommand.COMMAND_GETKEYS)); | ||
} | ||
|
||
var cmdName = parseState.GetString(0).ToUpperInvariant(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for ToUpperInvariant(), the TryGet... methods already take care of that.
return AbortWithWrongNumberOfArguments(nameof(RespCommand.COMMAND_GETKEYSANDFLAGS)); | ||
} | ||
|
||
var cmdName = parseState.GetString(0).ToUpperInvariant(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See same comments as in GETKEYS
{ | ||
var keyCount = keyList.Count; | ||
ExtractKeys(spec, keyList); | ||
var flags = EnumUtils.GetEnumDescriptions(spec.Flags); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
spec.respFormatFlags should already contain this info, you can create a public getter than returns this field.
|
||
foreach (var spec in cmdInfo.KeySpecifications) | ||
{ | ||
var keyCount = keyList.Count; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: just for clarity, should probably be named prevKeyCount
@@ -233,7 +233,7 @@ private static IReadOnlyDictionary<string, RespCommandDocs> GetUpdatedCommandsDo | |||
} | |||
|
|||
// Update commands docs with commands to add | |||
foreach (var command in commandsToAdd.Keys) | |||
foreach (var command in commandsToAdd.Keys.Where(x => x.Command.StartsWith("COMMAND"))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mistake?
Adding the COMMAND GETKEYS and GETKEYSANDFLAGS command to garnet