Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support of the command JSON.DEBUG MEMORY #2323

Merged
merged 27 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/commands/cmd_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,37 @@ class CommandJsonMSet : public Commander {
}
};

class CommandJsonDebug : public Commander {
public:
Status Execute(Server *svr, Connection *conn, std::string *output) override {
redis::Json json(svr->storage, conn->GetNamespace());

std::string path = "$";

if (!util::EqualICase(args_[1], "memory")) {
return {Status::RedisExecErr, "ERR wrong number of arguments for 'json.debug' command"};
}

if (args_.size() == 4) {
path = args_[3];
} else if (args_.size() > 4) {
return {Status::RedisExecErr, "The number of arguments is more than expected"};
}

Optionals<uint64_t> results;
auto s = json.DebugMemory(args_[2], path, &results);

if (s.IsNotFound()) {
*output = conn->NilString();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's wrong. For nonexistent key, the result is 0.

Please confirm with redis-stack and redis-cli by yourself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you again. The latest version of Redis Stack returns all cases, and I will make the necessary modifications later
image

return Status::OK();
}

if (!s.ok()) return {Status::RedisExecErr, s.ToString()};

*output = OptionalsToString(conn, results);
return Status::OK();
}
};
REDIS_REGISTER_COMMANDS(MakeCmdAttr<CommandJsonSet>("json.set", 4, "write", 1, 1, 1),
MakeCmdAttr<CommandJsonGet>("json.get", -2, "read-only", 1, 1, 1),
MakeCmdAttr<CommandJsonInfo>("json.info", 2, "read-only", 1, 1, 1),
Expand All @@ -647,6 +678,7 @@ REDIS_REGISTER_COMMANDS(MakeCmdAttr<CommandJsonSet>("json.set", 4, "write", 1, 1
MakeCmdAttr<CommandJsonStrAppend>("json.strappend", -3, "write", 1, 1, 1),
MakeCmdAttr<CommandJsonStrLen>("json.strlen", -2, "read-only", 1, 1, 1),
MakeCmdAttr<CommandJsonMGet>("json.mget", -3, "read-only", 1, -2, 1),
MakeCmdAttr<CommandJsonMSet>("json.mset", -4, "write", 1, -3, 3), );
MakeCmdAttr<CommandJsonMSet>("json.mset", -4, "write", 1, -3, 3),
MakeCmdAttr<CommandJsonDebug>("json.debug", -3, "read-only", 1, 2, 1));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
MakeCmdAttr<CommandJsonDebug>("json.debug", -3, "read-only", 1, 2, 1));
MakeCmdAttr<CommandJsonDebug>("json.debug", -3, "read-only", 2, 2, 1));


} // namespace redis
36 changes: 36 additions & 0 deletions src/types/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,42 @@ struct JsonValue {
return results;
}

StatusOr<Optionals<uint64_t>> StrBytes(std::string_view path, JsonStorageFormat format,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is optional useful here? Could we just use vector?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
StatusOr<Optionals<uint64_t>> StrBytes(std::string_view path, JsonStorageFormat format,
StatusOr<Optionals<uint64_t>> GetBytes(std::string_view path, JsonStorageFormat format,

int max_nesting_depth = std::numeric_limits<int>::max()) const {
Optionals<uint64_t> results;
try {
jsoncons::jsonpath::json_query(
value, path,
[&results, max_nesting_depth, format](const std::string & /*path*/, const jsoncons::json &origin) {
if (!origin.empty()) {
Status s;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it useful here?

std::string buffer;
std::error_code ec;
if (format == JsonStorageFormat::JSON) {
jsoncons::json_options options;
options.max_nesting_depth(max_nesting_depth);
jsoncons::compact_json_string_encoder encoder{buffer, options};
origin.dump(encoder, ec);
} else if (format == JsonStorageFormat::CBOR) {
jsoncons::cbor::cbor_options options;
options.max_nesting_depth(max_nesting_depth);
jsoncons::cbor::basic_cbor_encoder<jsoncons::string_sink<std::string>> encoder{buffer, options};
origin.dump(encoder, ec);
Copy link
Member

@PragmaTwice PragmaTwice May 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the exception version of dump and catch it later.

}
if (ec) {
throw jsoncons::jsonpath::jsonpath_error(ec);
}
results.emplace_back(buffer.size());
} else {
results.emplace_back(0);
}
});
} catch (const jsoncons::jsonpath::jsonpath_error &e) {
return {Status::NotOK, e.what()};
}
return results;
}

StatusOr<JsonValue> Get(std::string_view path) const {
try {
return jsoncons::jsonpath::json_query(value, path);
Expand Down
20 changes: 20 additions & 0 deletions src/types/redis_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -627,4 +627,24 @@ std::vector<rocksdb::Status> Json::readMulti(const std::vector<Slice> &ns_keys,
return statuses;
}

rocksdb::Status Json::DebugMemory(const std::string &user_key, const std::string &path, Optionals<uint64_t> *results) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

format or adding a space between them?

auto ns_key = AppendNamespacePrefix(user_key);
JsonMetadata metadata;
if (path == "$") {
std::string bytes;
Slice rest;
auto s = GetMetadata(GetOptions{}, {kRedisJson}, ns_key, &bytes, &metadata, &rest);
if (!s.ok()) return s;
results->emplace_back(bytes.size());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
results->emplace_back(bytes.size());
results->emplace_back(rest.size());

} else {
JsonValue json_val;
auto s = read(ns_key, &metadata, &json_val);
if (!s.ok()) return s;
auto str_bytes = json_val.StrBytes(path, metadata.format, storage_->GetConfig()->json_max_nesting_depth);
if (!str_bytes) return rocksdb::Status::InvalidArgument(str_bytes.Msg());
*results = std::move(*str_bytes);
}
return rocksdb::Status::OK();
}

} // namespace redis
1 change: 1 addition & 0 deletions src/types/redis_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class Json : public Database {
std::vector<JsonValue> &results);
rocksdb::Status MSet(const std::vector<std::string> &user_keys, const std::vector<std::string> &paths,
const std::vector<std::string> &values);
rocksdb::Status DebugMemory(const std::string &user_key, const std::string &path, Optionals<uint64_t> *results);

private:
rocksdb::Status write(Slice ns_key, JsonMetadata *metadata, const JsonValue &json_val);
Expand Down
11 changes: 11 additions & 0 deletions tests/gocase/unit/type/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,17 @@ func TestJson(t *testing.T) {
EqualJSON(t, `[{"a": 4, "b": 5, "nested": {"a": 6}, "c": null}]`, rdb.Do(ctx, "JSON.GET", "a1", "$").Val())
EqualJSON(t, `[4]`, rdb.Do(ctx, "JSON.GET", "a1", "$.a").Val())
})

t.Run("JSON.DEBUG MEMORY basics", func(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.DEBUG", "MEMORY", "a", "$.a").Val(), result1)
var result2 = make([]interface{}, 0)
result2 = append(result2, int64(5), int64(7), int64(2))
require.Equal(t, rdb.Do(ctx, "JSON.DEBUG", "MEMORY", "a", "$..a").Val(), result2)
require.ErrorIs(t, rdb.Do(ctx, "JSON.DEBUG", "MEMORY", "not_exists", "$").Err(), redis.Nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add more test cases? e.g. for a valid $, or a number/array/object, or a non-existent path.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I will add all the testcases

})
}

func EqualJSON(t *testing.T, expected string, actual interface{}) {
Expand Down