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 08194a83 Fix JSON.DEL should return 0 if the key was not found (#1918)
08194a83 is described below

commit 08194a83d4c636cfafa98c6ee212ea87b9f698c8
Author: hulk <[email protected]>
AuthorDate: Mon Dec 4 22:55:11 2023 +0800

    Fix JSON.DEL should return 0 if the key was not found (#1918)
---
 src/types/redis_json.cc                  | 17 +++++++++++------
 tests/gocase/unit/type/json/json_test.go |  3 +++
 2 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/src/types/redis_json.cc b/src/types/redis_json.cc
index 621d83f6..ee4f3678 100644
--- a/src/types/redis_json.cc
+++ b/src/types/redis_json.cc
@@ -401,18 +401,23 @@ rocksdb::Status Json::ArrTrim(const std::string 
&user_key, const std::string &pa
 }
 
 rocksdb::Status Json::Del(const std::string &user_key, const std::string 
&path, size_t *result) {
-  auto ns_key = AppendNamespacePrefix(user_key);
+  *result = 0;
 
+  auto ns_key = AppendNamespacePrefix(user_key);
   LockGuard guard(storage_->GetLockManager(), ns_key);
-  if (path == "$") {
-    *result = 1;
-    return del(ns_key);
-  }
   JsonValue json_val;
   JsonMetadata metadata;
   auto s = read(ns_key, &metadata, &json_val);
 
-  if (!s.ok()) return s;
+  if (!s.ok() && !s.IsNotFound()) return s;
+  if (s.IsNotFound()) {
+    return rocksdb::Status::OK();
+  }
+
+  if (path == "$") {
+    *result = 1;
+    return del(ns_key);
+  }
 
   auto res = json_val.Del(path);
   if (!res) return rocksdb::Status::InvalidArgument(res.Msg());
diff --git a/tests/gocase/unit/type/json/json_test.go 
b/tests/gocase/unit/type/json/json_test.go
index adf58001..a2d0485f 100644
--- a/tests/gocase/unit/type/json/json_test.go
+++ b/tests/gocase/unit/type/json/json_test.go
@@ -72,6 +72,9 @@ func TestJson(t *testing.T) {
                        require.NoError(t, rdb.Do(ctx, "JSON.SET", "a", "$", 
`{"x": 1, "nested": {"x": 2, "y": 3}}`).Err())
                        require.EqualValues(t, 1, rdb.Do(ctx, command, "a", 
"$.x").Val())
                        require.Equal(t, `[{"nested":{"x":2,"y":3}}]`, 
rdb.Do(ctx, "JSON.GET", "a", "$").Val())
+
+                       require.EqualValues(t, 1, rdb.Do(ctx, command, "a", 
"$").Val())
+                       require.EqualValues(t, 0, rdb.Do(ctx, command, 
"no-such-json-key", "$").Val())
                }
        })
 

Reply via email to