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

Minor refactor the implementation of the command ZRANDMEMBER #2025

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/types/redis_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,7 @@ rocksdb::Status Hash::RandField(const Slice &user_key, int64_t command_count, st
field_values->reserve(std::min(size, count));
if (!unique || count == 1) {
// Case 1: Negative count, randomly select elements or without parameter
std::random_device rd;
std::mt19937 gen(rd());
std::mt19937 gen(std::random_device{}());
std::uniform_int_distribution<uint64_t> dis(0, size - 1);
for (uint64_t i = 0; i < count; i++) {
uint64_t index = dis(gen);
Expand All @@ -421,8 +420,8 @@ rocksdb::Status Hash::RandField(const Slice &user_key, int64_t command_count, st
// Case 3: Requested count is less than the number of elements inside the hash
std::vector<uint64_t> indices(size);
std::iota(indices.begin(), indices.end(), 0);
std::shuffle(indices.begin(), indices.end(),
std::random_device{}); // use Fisher-Yates shuffle algorithm to randomize the order
std::mt19937 gen(std::random_device{}());
std::shuffle(indices.begin(), indices.end(), gen); // use Fisher-Yates shuffle algorithm to randomize the order
for (uint64_t i = 0; i < count; i++) {
uint64_t index = indices[i];
append_field_with_index(index);
Expand Down
13 changes: 7 additions & 6 deletions src/types/redis_zset.cc
Original file line number Diff line number Diff line change
Expand Up @@ -897,13 +897,14 @@ rocksdb::Status ZSet::RandMember(const Slice &user_key, int64_t command_count,
std::string ns_key = AppendNamespacePrefix(user_key);
ZSetMetadata metadata(false);
rocksdb::Status s = GetMetadata(ns_key, &metadata);
if (!s.ok() || metadata.size == 0) return s;
if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s;
if (metadata.size == 0) return rocksdb::Status::OK();

std::vector<MemberScore> samples;
s = GetAllMemberScores(user_key, &samples);
if (!s.ok() || samples.empty()) return s;

auto size = static_cast<uint64_t>(samples.size());
uint64_t size = samples.size();
member_scores->reserve(std::min(size, count));

if (!unique || count == 1) {
Expand All @@ -915,15 +916,15 @@ rocksdb::Status ZSet::RandMember(const Slice &user_key, int64_t command_count,
}
} else if (size <= count) {
for (auto &sample : samples) {
member_scores->push_back(sample);
member_scores->push_back(std::move(sample));
}
} else {
// first shuffle the samples
std::shuffle(samples.begin(), samples.end(), std::random_device{});

std::mt19937 gen(std::random_device{}());
std::shuffle(samples.begin(), samples.end(), gen);
// then pick the first `count` ones.
for (uint64_t i = 0; i < count; i++) {
member_scores->emplace_back(samples[i]);
member_scores->emplace_back(std::move(samples[i]));
}
}

Expand Down