-
Notifications
You must be signed in to change notification settings - Fork 998
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(generic_family): implement RANDOMKEY command #2639
Conversation
Sorry, I hit the submit button too early: thank you so much for your contribution! |
87034e7
to
4ddb130
Compare
@chakaz the new solution:
|
837cd13
to
321803a
Compare
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 again for your contribution, we're very close!
src/server/generic_family.cc
Outdated
size_t attempts = 0; | ||
uint64_t cursor = 0; | ||
do { | ||
cursor = prime_table->GetRandomCursor().value(); | ||
OpScan({shard, 0u, db_cntx}, scan_opts, &cursor, candidates); | ||
} while (candidates->empty() && ++attempts < kMaxAttemps); | ||
|
||
if (candidates->empty()) { // try to scan again from the start of the shard | ||
OpScan({shard, 0u, db_cntx}, scan_opts, &cursor, candidates); | ||
} |
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.
Here's a proposal:
size_t attempts = 0; | |
uint64_t cursor = 0; | |
do { | |
cursor = prime_table->GetRandomCursor().value(); | |
OpScan({shard, 0u, db_cntx}, scan_opts, &cursor, candidates); | |
} while (candidates->empty() && ++attempts < kMaxAttemps); | |
if (candidates->empty()) { // try to scan again from the start of the shard | |
OpScan({shard, 0u, db_cntx}, scan_opts, &cursor, candidates); | |
} | |
for (size_t i = 0; i <= kMaxAttempts; ++i) { | |
if (candidates->empty()) { | |
break; | |
} | |
uint64_t cursor = 0; | |
if (i < kMaxAttempts) { | |
prime_table->GetRandomCursor().value(); | |
} | |
OpScan({shard, 0u, db_cntx}, scan_opts, &cursor, candidates); | |
} |
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.
Not a fan of do while
s? Hahahaha, just kidding, it actually reads better with the for
loop.
I thought about a more optimal approach, but that will require a kind of "NextNearestIterator" that also looks iterates back on the slots, is it worth implementing it? (Is it even possible @romange? iterate backward on the dashtable?)
Signed-off-by: Leonardo Mello <[email protected]>
@chakaz I did the changes as you suggested |
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.
Please see my nit proposal, I think it's slightly more readable :)
And thank you for bearing with me!
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.
Awesome work, thanks again! I'll merge the changes now.
Resolves #443.
Redis claims to have
O(1)
time complexity because:dict
then:So for this solution I set a threshold of 15 entries, and loop through all shards in parallel collecting 2 entries per child, then I take one random index of these collection.Sometimes the
RANDOMKEY
command throws the following error on thestd::vector<>::~vector()
: