Skip to content

feat: Added store option for sort command. #5095

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

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

Conversation

H4R5H1T-007
Copy link
Contributor

Added Store option in Sort command as mentioned in this comment which is in issue #3886. Please review this PR in the meanwhile I will start with SORT_RO command implementation.

@dragonflydb dragonflydb deleted a comment May 10, 2025
@dragonflydb dragonflydb deleted a comment May 10, 2025
@dragonflydb dragonflydb deleted a comment May 10, 2025
@H4R5H1T-007 H4R5H1T-007 force-pushed the main branch 2 times, most recently from 9300a91 to f54e3ac Compare May 15, 2025 05:54

// This would overwrite existing value if any with new list.
// Set the expiry at 300 seconds.
auto op_res = op_args.GetDbSlice().AddOrUpdate(op_args.db_cntx, key, std::move(pv), 300000);
Copy link
Collaborator

Choose a reason for hiding this comment

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

why do you set the expiry to 300s?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh Sorry for that. I read this in Valkey documents and thought we had to add some expiry earlier then forgot to remove it. Thanks for spotting it I will remove it.

An interesting pattern using SORT ... STORE consists in associating an EXPIRE timeout to the resulting key so that in applications where the result of a SORT operation can be cached for some time. Other clients will use the cached list instead of calling SORT for every request. When the key will timeout, an updated version of the cache can be created by calling SORT ... STORE again.

cmd_cntx.tx->Execute(std::move(fetch_cb), true);
}

// OpResultTyped<SortEntryList> fetch_result =
Copy link
Collaborator

Choose a reason for hiding this comment

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

remove the old code

OpResultTyped<SortEntryList> fetch_result;
auto fetch_cb = [&](Transaction* t, EngineShard* shard) {
ShardId shard_id = shard->shard_id();
if (shard_id == source_sid) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

pls add a comment: in case of SORT option, we fetch only on the source shard

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure


auto* rb = static_cast<RedisReplyBuilder*>(builder);
if (!fetch_result.ok())
if (!fetch_result.ok()) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would unify all the non-ok conditions here:

if (!fetch_result.ok()) {
   cmd_cntx.tx->Conclude();
   if (fetch_result.status() == OpStatus::INVALID_NUMERIC_RESULT) 
     return builder->SendError(...);
   ....
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yup that will be better. Will do that

@@ -1438,10 +1439,33 @@ OpResultTyped<SortEntryList> OpFetchSortEntries(const OpArgs& op_args, std::stri
return success ? res : OpStatus::INVALID_NUMERIC_RESULT;
}

template <typename IteratorBegin, typename IteratorEnd>
Copy link
Collaborator

Choose a reason for hiding this comment

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

why do you need a template here? what are the types of the iterators?

Copy link
Contributor Author

@H4R5H1T-007 H4R5H1T-007 May 28, 2025

Choose a reason for hiding this comment

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

There are two types of iterator present as SortedEntryList is variant of two vector and hence iterator type will also change based on the arguments so I added this template. Let me know if I should handle this using some different method.

@H4R5H1T-007
Copy link
Contributor Author

Hey @romange should I create Sort Generic like we have for other commands? We also have to make Sort_RO command it will be useful there.

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