Skip to content

Commit

Permalink
Minor refactor the implementation of the command ZRANDMEMBER (#2025)
Browse files Browse the repository at this point in the history
  • Loading branch information
mapleFU authored Jan 17, 2024
1 parent d1acbcb commit d9acbba
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
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

0 comments on commit d9acbba

Please sign in to comment.