Skip to content
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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

Vijay-Nirmal
Copy link
Contributor

@Vijay-Nirmal Vijay-Nirmal commented Dec 18, 2024

Adding the COMMAND GETKEYS and GETKEYSANDFLAGS command to garnet

  • Add COMMAND GETKEYS and GETKEYSANDFLAGS commands
  • Add Integration Test cases and ACL Test
  • Add documentation

Copy link
Contributor

@TalZaccai TalZaccai left a 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)
Copy link
Contributor

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)
Copy link
Contributor

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)
Copy link
Contributor

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)))
Copy link
Contributor

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[]>();
Copy link
Contributor

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();
Copy link
Contributor

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();
Copy link
Contributor

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);
Copy link
Contributor

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;
Copy link
Contributor

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")))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mistake?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants