From 888705990f3cb614c8d656b933ee1b7865540149 Mon Sep 17 00:00:00 2001 From: mwish Date: Tue, 16 Jan 2024 23:48:42 +0800 Subject: [PATCH 1/2] cleanup-zset-random --- src/types/redis_hash.cc | 5 +++-- src/types/redis_zset.cc | 12 ++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/types/redis_hash.cc b/src/types/redis_hash.cc index 0ee9c16e3d2..2eb71d41b57 100644 --- a/src/types/redis_hash.cc +++ b/src/types/redis_hash.cc @@ -421,8 +421,9 @@ 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 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::random_device rd; + std::mt19937 gen(rd()); + 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); diff --git a/src/types/redis_zset.cc b/src/types/redis_zset.cc index 57c154acc8d..631d3517b42 100644 --- a/src/types/redis_zset.cc +++ b/src/types/redis_zset.cc @@ -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 samples; s = GetAllMemberScores(user_key, &samples); if (!s.ok() || samples.empty()) return s; - auto size = static_cast(samples.size()); + uint64_t size = samples.size(); member_scores->reserve(std::min(size, count)); if (!unique || count == 1) { @@ -915,15 +916,14 @@ 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::shuffle(samples.begin(), samples.end(), std::mt19937{std::random_device{}()}); // 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])); } } From fec65dbd5039660faff8c191410b13ce2595aa08 Mon Sep 17 00:00:00 2001 From: mwish Date: Wed, 17 Jan 2024 00:05:57 +0800 Subject: [PATCH 2/2] trying to fix sonar --- src/types/redis_hash.cc | 6 ++---- src/types/redis_zset.cc | 3 ++- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/types/redis_hash.cc b/src/types/redis_hash.cc index 2eb71d41b57..d7a1ad8b7bc 100644 --- a/src/types/redis_hash.cc +++ b/src/types/redis_hash.cc @@ -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 dis(0, size - 1); for (uint64_t i = 0; i < count; i++) { uint64_t index = dis(gen); @@ -421,8 +420,7 @@ 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 indices(size); std::iota(indices.begin(), indices.end(), 0); - std::random_device rd; - std::mt19937 gen(rd()); + 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]; diff --git a/src/types/redis_zset.cc b/src/types/redis_zset.cc index 631d3517b42..9215d6211f5 100644 --- a/src/types/redis_zset.cc +++ b/src/types/redis_zset.cc @@ -920,7 +920,8 @@ rocksdb::Status ZSet::RandMember(const Slice &user_key, int64_t command_count, } } else { // first shuffle the samples - std::shuffle(samples.begin(), samples.end(), std::mt19937{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(std::move(samples[i]));