From 1e7ce6e7f8bbd36846cec19174852696086278d6 Mon Sep 17 00:00:00 2001 From: chejinge Date: Thu, 5 Dec 2024 19:55:41 +0800 Subject: [PATCH] zopmax && zpopmin --- src/cache/include/cache.h | 2 ++ src/pika_cache.cc | 6 +++-- tests/integration/zset_test.go | 42 +++++++++++++++++++--------------- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/cache/include/cache.h b/src/cache/include/cache.h index 5f5d6a2959..20c93715f0 100644 --- a/src/cache/include/cache.h +++ b/src/cache/include/cache.h @@ -147,6 +147,8 @@ class RedisCache { std::vector *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* score_members); + Status ZPopMax(std::string& key, int64_t count, std::vector* score_members); // Bit Commands Status SetBit(std::string& key, size_t offset, int64_t value); diff --git a/src/pika_cache.cc b/src/pika_cache.cc index 1c92c32c71..03badab177 100644 --- a/src/pika_cache.cc +++ b/src/pika_cache.cc @@ -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* score_members, const std::shared_ptr& db) { +Status PikaCache::ZPopMin(std::string &key, int64_t count, std::vector *score_members, + const std::shared_ptr &db) { int cache_index = CacheIndex(key); std::lock_guard lm(*cache_mutexs_[cache_index]); @@ -1479,7 +1480,8 @@ Status PikaCache::ZPopMin(std::string& key, int64_t count, std::vector* score_members, const std::shared_ptr& db) { +Status PikaCache::ZPopMax(std::string &key, int64_t count, std::vector *score_members, + const std::shared_ptr &db) { int cache_index = CacheIndex(key); std::lock_guard lm(*cache_mutexs_[cache_index]); diff --git a/tests/integration/zset_test.go b/tests/integration/zset_test.go index 89fc4b202b..0de6676aa5 100644 --- a/tests/integration/zset_test.go +++ b/tests/integration/zset_test.go @@ -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())