Skip to content

Commit

Permalink
reduce ditto
Browse files Browse the repository at this point in the history
  • Loading branch information
guojidan committed Nov 3, 2023
1 parent c021e50 commit 756f193
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 17 deletions.
15 changes: 2 additions & 13 deletions src/types/redis_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -205,22 +205,11 @@ rocksdb::Status Json::ArrLen(const std::string &user_key, const std::string &pat
rocksdb::Status Json::StrAppend(const std::string &user_key, const std::string &path, const std::string &value,
std::vector<uint64_t> &append_cnt) {
auto ns_key = AppendNamespacePrefix(user_key);

LockGuard guard(storage_->GetLockManager(), ns_key);

std::string bytes;
JsonMetadata metadata;
Slice rest;
auto s = GetMetadata(kRedisJson, ns_key, &bytes, &metadata, &rest);
JsonValue json_val;
auto s = read(ns_key, &metadata, &json_val);
if (!s.ok()) return s;

if (metadata.format != JsonStorageFormat::JSON)
return rocksdb::Status::NotSupported("JSON storage format not supported");

auto json_res = JsonValue::FromString(rest.ToStringView());
if (!json_res) return rocksdb::Status::Corruption(json_res.Msg());
auto json_val = *std::move(json_res);

auto append_res = json_val.StrAppend(path, value, append_cnt);
if (!append_res) return rocksdb::Status::InvalidArgument(append_res.Msg());
// need Reverse order
Expand Down
4 changes: 2 additions & 2 deletions tests/cppunit/types/json_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,13 @@ TEST_F(RedisJsonTest, ArrLen) {

TEST_F(RedisJsonTest, StrAppend) {
ASSERT_TRUE(json_->Set(key_, "$", R"({"a":"foo", "nested": {"a": "hello"}, "nested2": {"a": 31}})").ok());
ASSERT_TRUE(json_->StrAppend(key_, "$.a", "be", append_cnt_).ok());
ASSERT_TRUE(json_->StrAppend(key_, "$.a", "\"be\"", append_cnt_).ok());
ASSERT_EQ(append_cnt_.size(), 1);
ASSERT_EQ(append_cnt_[0], 5);

append_cnt_.clear();
ASSERT_TRUE(json_->Set(key_, "$", R"({"a":"foo", "nested": {"a": "hello"}, "nested2": {"a": 31}})").ok());
ASSERT_TRUE(json_->StrAppend(key_, "$..a", "be", append_cnt_).ok());
ASSERT_TRUE(json_->StrAppend(key_, "$..a", "\"be\"", append_cnt_).ok());
ASSERT_EQ(append_cnt_.size(), 3);
int64_t result1[] = {5, 7, -1};
for (int i = 0; i < 3; ++i) {
Expand Down
4 changes: 2 additions & 2 deletions tests/gocase/unit/type/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ func TestJson(t *testing.T) {
var result1 = make([]interface{}, 0)
result1 = append(result1, int64(5))
require.NoError(t, rdb.Do(ctx, "JSON.SET", "a", "$", `{"a":"foo", "nested": {"a": "hello"}, "nested2": {"a": 31}}`).Err())
require.Equal(t, rdb.Do(ctx, "JSON.STRAPPEND", "a", "$.a", "be").Val(), result1)
require.Equal(t, rdb.Do(ctx, "JSON.STRAPPEND", "a", "$.a", "\"be\"").Val(), result1)

var result2 = make([]interface{}, 0)
result2 = append(result2, int64(5), int64(7), interface{}(nil))
require.NoError(t, rdb.Do(ctx, "JSON.SET", "a", "$", `{"a":"foo", "nested": {"a": "hello"}, "nested2": {"a": 31}}`).Err())
require.Equal(t, rdb.Do(ctx, "JSON.STRAPPEND", "a", "$..a", "be").Val(), result2)
require.Equal(t, rdb.Do(ctx, "JSON.STRAPPEND", "a", "$..a", "\"be\"").Val(), result2)
})

t.Run("JSON.STRLEN basics", func(t *testing.T) {
Expand Down

0 comments on commit 756f193

Please sign in to comment.