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

Extract a rand sample helper and support negative sample count in "Set" #2113

Merged
merged 6 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 18 additions & 9 deletions src/types/redis_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,14 @@ rocksdb::Status Set::MIsMember(const Slice &user_key, const std::vector<Slice> &
}

rocksdb::Status Set::Take(const Slice &user_key, std::vector<std::string> *members, int count, bool pop) {
int n = 0;
members->clear();
if (count <= 0) return rocksdb::Status::OK();
if (count == 0) return rocksdb::Status::OK();
if (count < 0) {
DCHECK(!pop);
// NOTE: Currently, for SRANDMEMBER, we don't
// make duplicate members to be returned.
torwig marked this conversation as resolved.
Show resolved Hide resolved
count = -count;
}

std::string ns_key = AppendNamespacePrefix(user_key);

Expand All @@ -210,9 +215,11 @@ rocksdb::Status Set::Take(const Slice &user_key, std::vector<std::string> *membe
rocksdb::Status s = GetMetadata(ns_key, &metadata);
if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s;

auto batch = storage_->GetWriteBatchBase();
WriteBatchLogData log_data(kRedisSet);
batch->PutLogData(log_data.Encode());
ObserverOrUniquePtr<rocksdb::WriteBatchBase> batch = storage_->GetWriteBatchBase();
if (pop) {
WriteBatchLogData log_data(kRedisSet);
batch->PutLogData(log_data.Encode());
}

std::string prefix = InternalKey(ns_key, "", metadata.version, storage_->IsSlotIdEncoded()).Encode();
std::string next_version_prefix = InternalKey(ns_key, "", metadata.version + 1, storage_->IsSlotIdEncoded()).Encode();
Expand All @@ -229,11 +236,10 @@ rocksdb::Status Set::Take(const Slice &user_key, std::vector<std::string> *membe
std::mt19937 gen(rd());
auto iter = util::UniqueIterator(storage_, read_options);
for (iter->Seek(prefix); iter->Valid() && iter->key().starts_with(prefix); iter->Next()) {
++n;
if (n <= count) {
if (iter_keys.size() < count) {
iter_keys.push_back(iter->key().ToString());
} else { // n > count
std::uniform_int_distribution<> distrib(0, n - 1);
std::uniform_int_distribution<> distrib(0, static_cast<int>(iter_keys.size()));
int random = distrib(gen); // [0,n-1]
if (random < count) {
iter_keys[random] = iter->key().ToString();
Expand All @@ -247,7 +253,10 @@ rocksdb::Status Set::Take(const Slice &user_key, std::vector<std::string> *membe
batch->Delete(key);
}
}
if (pop && !iter_keys.empty()) {
if (!pop) {
return rocksdb::Status::OK();
}
if (!iter_keys.empty()) {
metadata.size -= iter_keys.size();
std::string bytes;
metadata.Encode(&bytes);
Expand Down
3 changes: 3 additions & 0 deletions tests/cppunit/types/set_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ TEST_F(RedisSetTest, TakeWithoutPop) {
s = set_->Take(key_, &members, int(fields_.size() - 1), false);
EXPECT_TRUE(s.ok());
EXPECT_EQ(members.size(), fields_.size() - 1);
s = set_->Take(key_, &members, -int(fields_.size() - 1), false);
EXPECT_TRUE(s.ok());
EXPECT_EQ(members.size(), fields_.size() - 1);
s = set_->Remove(key_, fields_, &ret);
EXPECT_TRUE(s.ok() && fields_.size() == ret);
s = set_->Del(key_);
Expand Down
Loading