You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kvrocks.apache.org by hu...@apache.org on 2024/01/06 11:06:56 UTC

(kvrocks) branch unstable updated: Add support of BITFIELD_RO (#1990)

This is an automated email from the ASF dual-hosted git repository.

hulk pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/kvrocks.git


The following commit(s) were added to refs/heads/unstable by this push:
     new 74a1f42a Add support of BITFIELD_RO (#1990)
74a1f42a is described below

commit 74a1f42a3fe671e1f8732f74a13e5ed289d2d646
Author: jyf111 <64...@users.noreply.github.com>
AuthorDate: Sat Jan 6 19:06:50 2024 +0800

    Add support of BITFIELD_RO (#1990)
---
 src/commands/cmd_bit.cc                      | 10 +++++++++-
 tests/gocase/unit/type/bitmap/bitmap_test.go | 14 +++++++++++++-
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/src/commands/cmd_bit.cc b/src/commands/cmd_bit.cc
index 88341c84..6e9fe5ba 100644
--- a/src/commands/cmd_bit.cc
+++ b/src/commands/cmd_bit.cc
@@ -242,6 +242,7 @@ class CommandBitOp : public Commander {
   BitOpFlags op_flag_;
 };
 
+template <bool ReadOnly>
 class CommandBitfield : public Commander {
  public:
   Status Parse(const std::vector<std::string> &args) override {
@@ -314,6 +315,12 @@ class CommandBitfield : public Commander {
       cmds_.push_back(cmd);
     }
 
+    if constexpr (ReadOnly) {
+      if (!read_only_) {
+        return {Status::RedisParseErr, "BITFIELD_RO only supports the GET subcommand"};
+      }
+    }
+
     return Commander::Parse(args);
   }
 
@@ -394,6 +401,7 @@ REDIS_REGISTER_COMMANDS(MakeCmdAttr<CommandGetBit>("getbit", 3, "read-only", 1,
                         MakeCmdAttr<CommandBitCount>("bitcount", -2, "read-only", 1, 1, 1),
                         MakeCmdAttr<CommandBitPos>("bitpos", -3, "read-only", 1, 1, 1),
                         MakeCmdAttr<CommandBitOp>("bitop", -4, "write", 2, -1, 1),
-                        MakeCmdAttr<CommandBitfield>("bitfield", -2, "write", 1, 1, 1), )
+                        MakeCmdAttr<CommandBitfield<false>>("bitfield", -2, "write", 1, 1, 1),
+                        MakeCmdAttr<CommandBitfield<true>>("bitfield_ro", -2, "read-only", 1, 1, 1), )
 
 }  // namespace redis
diff --git a/tests/gocase/unit/type/bitmap/bitmap_test.go b/tests/gocase/unit/type/bitmap/bitmap_test.go
index eb778622..a8ee50d0 100644
--- a/tests/gocase/unit/type/bitmap/bitmap_test.go
+++ b/tests/gocase/unit/type/bitmap/bitmap_test.go
@@ -303,13 +303,25 @@ func TestBitmap(t *testing.T) {
 		require.EqualValues(t, 32, rdb.BitOpOr(ctx, "x", "a", "b").Val())
 	})
 
-	t.Run("BITFIELD on string type", func(t *testing.T) {
+	t.Run("BITFIELD and BITFIELD_RO on string type", func(t *testing.T) {
 		str := "zhe ge ren hen lan, shen me dou mei you liu xia."
 		require.NoError(t, rdb.Set(ctx, "str", str, 0).Err())
+		for _, command := range []string{"BITFIELD", "BITFIELD_RO"} {
+			res := rdb.Do(ctx, command, "str", "GET", "u8", "32", "GET", "u8", "40")
+			require.NoError(t, res.Err())
+			require.EqualValues(t, []interface{}{int64(str[4]), int64(str[5])}, res.Val())
+		}
+
 		res := rdb.BitField(ctx, "str", "GET", "u8", "32", "SET", "u8", "32", 'r', "GET", "u8", "32")
 		require.NoError(t, res.Err())
 		require.EqualValues(t, str[4], res.Val()[0])
 		require.EqualValues(t, str[4], res.Val()[1])
 		require.EqualValues(t, 'r', res.Val()[2])
+		require.ErrorContains(t, rdb.Do(ctx, "BITFIELD_RO", "str", "GET", "u8", "32", "SET", "u8", "32", 'r', "GET", "u8", "32").Err(), "BITFIELD_RO only supports the GET subcommand")
+
+		res = rdb.BitField(ctx, "str", "INCRBY", "u8", "32", 2)
+		require.NoError(t, res.Err())
+		require.EqualValues(t, 't', res.Val()[0])
+		require.ErrorContains(t, rdb.Do(ctx, "BITFIELD_RO", "str", "INCRBY", "u8", "32", 2).Err(), "BITFIELD_RO only supports the GET subcommand")
 	})
 }