Skip to content

Commit

Permalink
zopmax && zpopmin
Browse files Browse the repository at this point in the history
  • Loading branch information
brother-jin committed Dec 5, 2024
1 parent 8397ae9 commit 1e7ce6e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/cache/include/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ class RedisCache {
std::vector<std::string> *members);
Status ZLexcount(std::string& key, std::string &min, std::string &max, uint64_t *len);
Status ZRemrangebylex(std::string& key, std::string &min, std::string &max);
Status ZPopMin(std::string& key, int64_t count, std::vector<storage::ScoreMember>* score_members);
Status ZPopMax(std::string& key, int64_t count, std::vector<storage::ScoreMember>* score_members);

// Bit Commands
Status SetBit(std::string& key, size_t offset, int64_t value);
Expand Down
6 changes: 4 additions & 2 deletions src/pika_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1465,7 +1465,8 @@ Status PikaCache::ZRemrangebylex(std::string& key, std::string &min, std::string
}
}

Status PikaCache::ZPopMin(std::string& key, int64_t count, std::vector<storage::ScoreMember>* score_members, const std::shared_ptr<DB>& db) {
Status PikaCache::ZPopMin(std::string &key, int64_t count, std::vector<storage::ScoreMember> *score_members,
const std::shared_ptr<DB> &db) {
int cache_index = CacheIndex(key);
std::lock_guard lm(*cache_mutexs_[cache_index]);

Expand All @@ -1479,7 +1480,8 @@ Status PikaCache::ZPopMin(std::string& key, int64_t count, std::vector<storage::
}
}

Status PikaCache::ZPopMax(std::string& key, int64_t count, std::vector<storage::ScoreMember>* score_members, const std::shared_ptr<DB>& db) {
Status PikaCache::ZPopMax(std::string &key, int64_t count, std::vector<storage::ScoreMember> *score_members,
const std::shared_ptr<DB> &db) {
int cache_index = CacheIndex(key);
std::lock_guard lm(*cache_mutexs_[cache_index]);

Expand Down
42 changes: 24 additions & 18 deletions tests/integration/zset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1440,32 +1440,38 @@ var _ = Describe("Zset Commands", func() {
}}))
})

It("should ZRem multiple members", func() {

err := client.ZAdd(ctx, "zset", redis.Z{Score: 1, Member: "one"}).Err()
It("should Zpopmin test", func() {
err := client.ZAdd(ctx, "zpopzset1", redis.Z{
Score: 1,
Member: "m1",
}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", redis.Z{Score: 2, Member: "two"}).Err()

err = client.ZAdd(ctx, "zpopzset1", redis.Z{
Score: 3,
Member: "m3",
}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", redis.Z{Score: 3, Member: "three"}).Err()

err = client.ZAdd(ctx, "zpopzset1", redis.Z{
Score: 4,
Member: "m4",
}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", redis.Z{Score: 4, Member: "four"}).Err()

max, err := client.ZPopMax(ctx, "zpopzset1", 1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(max).To(Equal([]redis.Z{{Score: 4, Member: "m4"}}))

zRem := client.ZRem(ctx, "zset", "two", "four")
Expect(zRem.Err()).NotTo(HaveOccurred())
Expect(zRem.Val()).To(Equal(int64(2)))
min, err := client.ZPopMin(ctx, "zpopzset1", 1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(min).To(Equal([]redis.Z{{Score: 1, Member: "m1"}}))

vals, err := client.ZRangeWithScores(ctx, "zset", 0, -1).Result()
rangeResult, err := client.ZRange(ctx, "zpopzset1", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]redis.Z{{
Score: 1,
Member: "one",
}, {
Score: 3,
Member: "three",
}}))
Expect(rangeResult).To(Equal([]string{"m3"}))
})

It("should ZRemRangeByRank", func() {
err := client.ZAdd(ctx, "zset", redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
Expand Down

0 comments on commit 1e7ce6e

Please sign in to comment.