-
Notifications
You must be signed in to change notification settings - Fork 472
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
Changes from 12 commits
0e52a78
0ba0fb5
f4005e4
15ed6fe
c4d9759
cb10eca
d563233
895ea71
5a361a2
4a3cb79
e45dcd1
e0e9faf
eb639b9
e23d0b0
17b3971
5f2b03f
a955aa8
15db3ef
5fb1dac
31ca58d
847858e
e0d69eb
29cda5d
38521ca
e4e4f80
9062108
e5de0c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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"}; | ||||||
} | ||||||
|
||||||
std::vector<std::string> results; | ||||||
auto s = json.DebugMemory(args_[2], path, &results); | ||||||
|
||||||
if (s.IsNotFound()) { | ||||||
*output = conn->NilString(); | ||||||
return Status::OK(); | ||||||
} | ||||||
|
||||||
if (!s.ok()) return {Status::RedisExecErr, s.ToString()}; | ||||||
|
||||||
*output = redis::ArrayOfBulkStrings(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), | ||||||
|
@@ -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)); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
} // namespace redis |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -217,6 +217,42 @@ struct JsonValue { | |||||||
return results; | ||||||||
} | ||||||||
|
||||||||
StatusOr<std::vector<std::string>> GetBytes(std::string_view path, JsonStorageFormat format, | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because currently ArrayOfBulkStrings only supports string types, if modified to a template, it cannot meet the writing requirements of std:: initializer_list. Do you suggest that I add another method, std: vector<size_t>? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use a loop to dump RESP output. It doesn't matter whether you use ArrayOfBulkStrings or not. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok thanks |
||||||||
int max_nesting_depth = std::numeric_limits<int>::max()) const { | ||||||||
std::vector<std::string> results; | ||||||||
try { | ||||||||
jsoncons::jsonpath::json_query(value, path, [&](const std::string & /*path*/, const jsoncons::json &origin) { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hello, I have two questions I would like to ask. 1. When does the status initialization fail and needs to be returned in advance? 2. Currently, obtaining the byte is still inaccurate, and the actual value should be larger than the encoded string. size(). Does this meet the project's standards? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no initialization failure. We should check it since the lambda function can be executed more than once. |
||||||||
if (!origin.empty()) { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the meaning of this empty check? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty arrays and nulls actually take up space, and the test cases have been supplemented. Please see if there are any areas that can be optimized |
||||||||
Status s; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is used to traverse the internal structure of JSON and will loop through the callback function multiple times inside There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean the variable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i see |
||||||||
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); | ||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use |
||||||||
if (ec) { | ||||||||
throw std::system_error(ec); | ||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove it. |
||||||||
results.emplace_back(std::to_string(buffer.size())); | ||||||||
} else { | ||||||||
results.emplace_back(std::to_string(0)); | ||||||||
} | ||||||||
}); | ||||||||
} catch (const jsoncons::jsonpath::jsonpath_error &e) { | ||||||||
return {Status::NotOK, e.what()}; | ||||||||
} catch (const std::system_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); | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(4)) | ||
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(4), int64(6), 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add more test cases? e.g. for a valid There was a problem hiding this comment. Choose a reason for hiding this commentThe 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{}) { | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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