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

feat(zset_family): support WITHSCORE in zrevrank/zrank commands (#3921) #4001

Merged
merged 1 commit into from
Oct 29, 2024

Conversation

Diskein
Copy link
Contributor

@Diskein Diskein commented Oct 27, 2024

Hey, it adds WITHSCORE support to zrank/zrevrank.

I actually wrote another one version with templated RankResult and constexpr'ed ifs, but i couldn't see performance differences with a simple 'boolean and if' solution. Since the other commands implemented with the last approach I've decided to go this way.

Another change it a new method in SortedMap. It could be implemented without this invention but it helps to avoid double lookup in the 'score_map'. The method returns std::pair which doesn't have self-explanatory fields with its 'first' and 'second' but I've hesitated to add a new struct for this purpose.

@romange romange self-requested a review October 27, 2024 11:31
@romange
Copy link
Collaborator

romange commented Oct 27, 2024

Thank you @Diskein 🙏🏼 , will take a look later this week.

Copy link
Collaborator

@romange romange left a comment

Choose a reason for hiding this comment

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

Looks good! Gave just a few minor comments.

if (obj == nullptr)
return std::nullopt;

optional rank = score_tree->GetRank(obj);
Copy link
Collaborator

Choose a reason for hiding this comment

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

you can pass reverse into GetRank to avoid copying the reversing logic below.
Similarly DCHECK is redundant as is it is checked by GetRank

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, I've added the 'reverse' param to ScoreTree::GetRank

src/server/zset_family.cc Outdated Show resolved Hide resolved
@@ -1979,17 +2000,35 @@ void ZRangeGeneric(CmdArgList args, Transaction* tx, SinkReplyBuilder* builder,
}

void ZRankGeneric(CmdArgList args, bool reverse, Transaction* tx, SinkReplyBuilder* builder) {
if (args.size() > 3) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

would you like to try replacing this parsing logic with facade::CmdArgParser ?
we usually replace the old parsing code with this as it's more clear this way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It turns out that redis does this check first. so i left this check in favor to throw the same error messages.

Providing this check by using the parser isn't obvious.
I tried to write a clean code by using parser for that and failed =D

@Diskein Diskein force-pushed the zrank-withscore-no-templates branch from e3f8608 to 6a7b8e6 Compare October 28, 2024 21:42
DCHECK(rank);
return reverse ? score_map->UpperBoundSize() - *rank - 1 : *rank;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

why was UpperBoundSize() used here? It isn't obvious, perhaps something slipped away from me.

IMHO: I'd like to keep DCHECK. It's true that score_map->GetRank shouldn't return std::nullopt in this codepath. It can do it in general though. So it wouldn't hurt.

Copy link
Collaborator

Choose a reason for hiding this comment

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

dense_set has the ability to expire items but it does not do it proactively, so its size method may include items that are already expired. We do not use the feature in sorted set though.

@Diskein Diskein force-pushed the zrank-withscore-no-templates branch from 6a7b8e6 to d27e00b Compare October 28, 2024 22:08
@romange
Copy link
Collaborator

romange commented Oct 29, 2024

Looks good, thank you very much!

@romange romange merged commit f16a325 into dragonflydb:main Oct 29, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants