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

fix(stream): add KeyRangeGen for XRead and XReadGroup #2657

Merged
merged 9 commits into from
Nov 15, 2024

Conversation

LindaSummer
Copy link
Contributor

Issue

Close #2625 .

Proposed Changes

  • Refactor Parse function for XRead and XReadGroup.
  • Add KeyRangeGen for XRead and XReadGroup.

@@ -1715,6 +1684,27 @@ class CommandXReadGroup : public Commander,
bufferevent_enable(bev, EV_READ);
}

static CommandKeyRangeGen KeyRangeGen() {
static constexpr auto keyRangeGenFunc = [](const std::vector<std::string> &args) {
auto parsed_args = *parse(args);
Copy link
Member

@PragmaTwice PragmaTwice Nov 11, 2024

Choose a reason for hiding this comment

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

Hmm actually from my side I don't think we need to reuse the code of Parse(), since:

  • many fields in parsed_args is useless in key range gen, so we made lots of useless string copies.
  • in key range gen we can assume that the syntax of the input args is valid, so we can omit many checks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @PragmaTwice ,

Thanks for your suggestions! 😊

I have reverted the original Parse function and add a simple search function for parsing stream key counts.

Best Regards,
Edward

Comment on lines 1687 to 1688
static CommandKeyRangeGen KeyRangeGen() {
static constexpr auto keyRangeGenFunc = [](const std::vector<std::string> &args) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
static CommandKeyRangeGen KeyRangeGen() {
static constexpr auto keyRangeGenFunc = [](const std::vector<std::string> &args) {
static CommandKeyRange KeyRangeGen(const std::vector<std::string> &args) {

Also we don't need to make a function that returns a function here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @PragmaTwice ,

Got it!

I have changed it to a static variable. 😊

Best Regards,
Edward

Comment on lines 40 to 41
auto stream_keyword_iter =
std::find_if(args.cbegin(), args.cend(), [](const std::string &arg) { return util::ToLower(arg) == "streams"; });
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
auto stream_keyword_iter =
std::find_if(args.cbegin(), args.cend(), [](const std::string &arg) { return util::ToLower(arg) == "streams"; });
auto stream_keyword_iter =
std::find_if(args.cbegin(), args.cend(), [](const std::string &arg) { return util::EqualICase(arg, "streams"); });

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @PragmaTwice ,

Thanks very much for your suggestion! 😊

I have replaced the comparison operator to util::EqualICase.

Best Regards,
Edward

Comment on lines 40 to 41
auto stream_keyword_iter =
std::find_if(args.cbegin(), args.cend(), [](const std::string &arg) { return util::EqualICase(arg, "streams"); });
Copy link
Member

Choose a reason for hiding this comment

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

For XREADGROUP, I think we need to find from begin + 4, to avoid a group or consumer named streams.

refer to https://redis.io/docs/latest/commands/xreadgroup/ .

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @PragmaTwice ,

Thanks very much for your review! 😊

I have updated the ParseStreamReadRange with a begin offset.

Best Regards,
Edward

Copy link
Member

@PragmaTwice PragmaTwice left a comment

Choose a reason for hiding this comment

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

The code looks good to me.

Would you like to write a go test case that calls redis command COMMAND GETKEYS XREAD/XREADGROUP ... to confirm the returned keys is correct?

You can add a new test like https://github.com/apache/kvrocks/blob/unstable/tests/gocase/unit/command/command_test.go#L125.

@LindaSummer
Copy link
Contributor Author

The code looks good to me.

Would you like to write a go test case that calls redis command COMMAND GETKEYS XREAD/XREADGROUP ... to confirm the returned keys is correct?

You can add a new test like https://github.com/apache/kvrocks/blob/unstable/tests/gocase/unit/command/command_test.go#L125.

Of course!

I will add unittest later today.😄

Best Regards,
Edward

@LindaSummer
Copy link
Contributor Author

Hi @PragmaTwice ,

I have added related unit tests for different combinations of XREAD and XREADGROUP. 😊

Best Regards,
Edward

@aleksraiden aleksraiden requested a review from torwig November 14, 2024 12:56
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
47.2% Coverage on New Code (required ≥ 50%)

See analysis details on SonarQube Cloud

@PragmaTwice PragmaTwice merged commit 91b5478 into apache:unstable Nov 15, 2024
31 of 32 checks passed
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.

Key range of XREAD/XREADGROUP is not correct
2 participants