From 1d784578dfea5d9a33a7b0fc0d989cc929c98f2b Mon Sep 17 00:00:00 2001 From: ofekshenawa <104765379+ofekshenawa@users.noreply.github.com> Date: Sun, 17 Dec 2023 15:19:27 +0200 Subject: [PATCH] Add BITFIELD_RO Command (#2820) Co-authored-by: Chayim --- bitmap_commands.go | 22 +++++++++++++++++++++- commands_test.go | 14 ++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/bitmap_commands.go b/bitmap_commands.go index 1436b02a..550d7f52 100644 --- a/bitmap_commands.go +++ b/bitmap_commands.go @@ -1,6 +1,8 @@ package redis -import "context" +import ( + "context" +) type BitMapCmdable interface { GetBit(ctx context.Context, key string, offset int64) *IntCmd @@ -127,3 +129,21 @@ func (c cmdable) BitField(ctx context.Context, key string, values ...interface{} _ = c(ctx, cmd) return cmd } + +// BitFieldRO - Read-only variant of the BITFIELD command. +// It is like the original BITFIELD but only accepts GET subcommand and can safely be used in read-only replicas. +// - BitFieldRO(ctx, key, "", "", "","") +func (c cmdable) BitFieldRO(ctx context.Context, key string, values ...interface{}) *IntSliceCmd { + args := make([]interface{}, 2, 2+len(values)) + args[0] = "BITFIELD_RO" + args[1] = key + if len(values)%2 != 0 { + panic("BitFieldRO: invalid number of arguments, must be even") + } + for i := 0; i < len(values); i += 2 { + args = append(args, "GET", values[i], values[i+1]) + } + cmd := NewIntSliceCmd(ctx, args...) + _ = c(ctx, cmd) + return cmd +} diff --git a/commands_test.go b/commands_test.go index fdc41c5a..f36e9d94 100644 --- a/commands_test.go +++ b/commands_test.go @@ -1279,6 +1279,20 @@ var _ = Describe("Commands", func() { Expect(nn).To(Equal([]int64{0, 4})) }) + It("should BitFieldRO", func() { + nn, err := client.BitField(ctx, "mykey", "SET", "u8", 8, 255).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(nn).To(Equal([]int64{0})) + + nn, err = client.BitFieldRO(ctx, "mykey", "u8", 0).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(nn).To(Equal([]int64{0})) + + nn, err = client.BitFieldRO(ctx, "mykey", "u8", 0, "u4", 8, "u4", 12, "u4", 13).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(nn).To(Equal([]int64{0, 15, 15, 14})) + }) + It("should Decr", func() { set := client.Set(ctx, "key", "10", 0) Expect(set.Err()).NotTo(HaveOccurred())