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

Support format options for JSON.GET #1840

Merged
merged 7 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
48 changes: 46 additions & 2 deletions src/commands/cmd_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
*
*/

#include <algorithm>

#include "commander.h"
#include "commands/command_parser.h"
#include "server/redis_reply.h"
#include "server/server.h"
#include "types/redis_json.h"
Expand All @@ -40,16 +43,57 @@ class CommandJsonSet : public Commander {

class CommandJsonGet : public Commander {
public:
Status Parse(const std::vector<std::string> &args) override {
CommandParser parser(args, 2);

while (parser.Good()) {
if (parser.EatEqICase("INDENT")) {
auto indent = GET_OR_RET(parser.TakeStr());

if (std::any_of(indent.begin(), indent.end(), [](char v) { return v != ' '; })) {
return {Status::RedisParseErr, "Currently only all-space INDENT is supported"};
}

indent_size_ = indent.size();
} else if (parser.EatEqICase("NEWLINE")) {
new_line_chars_ = GET_OR_RET(parser.TakeStr());
} else if (parser.EatEqICase("SPACE")) {
auto space = GET_OR_RET(parser.TakeStr());

if (space != "" && space != " ") {
return {Status::RedisParseErr, "Currently only SPACE ' ' is supported"};
}

spaces_after_colon_ = !space.empty();
} else {
break;
}
}

while (parser.Good()) {
paths_.push_back(GET_OR_RET(parser.TakeStr()));
}

return Status::OK();
}

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

JsonValue result;
auto s = json.Get(args_[1], {args_.begin() + 2, args_.end()}, &result);
auto s = json.Get(args_[1], paths_, &result);
if (!s.ok()) return {Status::RedisExecErr, s.ToString()};

*output = redis::BulkString(result.Dump());
*output = redis::BulkString(GET_OR_RET(result.Print(indent_size_, spaces_after_colon_, new_line_chars_)));
return Status::OK();
}

private:
uint8_t indent_size_ = 0;
bool spaces_after_colon_ = false;
std::string new_line_chars_;

std::vector<std::string> paths_;
};

REDIS_REGISTER_COMMANDS(MakeCmdAttr<CommandJsonSet>("json.set", -3, "write", 1, 1, 1),
Expand Down
1 change: 1 addition & 0 deletions src/config/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ Config::Config() {
{"persist-cluster-nodes-enabled", false, new YesNoField(&persist_cluster_nodes_enabled, true)},
{"redis-cursor-compatible", false, new YesNoField(&redis_cursor_compatible, false)},
{"repl-namespace-enabled", false, new YesNoField(&repl_namespace_enabled, false)},
{"json-max-nesting-depth", false, new IntField(&json_max_nesting_depth, 1024, 0, INT_MAX)},

/* rocksdb options */
{"rocksdb.compression", false,
Expand Down
3 changes: 3 additions & 0 deletions src/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ struct Config {
std::set<std::string> profiling_sample_commands;
bool profiling_sample_all_commands = false;

// json
int json_max_nesting_depth = 1024;

struct RocksDB {
int block_size;
bool cache_index_and_filter_blocks;
Expand Down
57 changes: 49 additions & 8 deletions src/types/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,76 @@

#include <jsoncons/json.hpp>
#include <jsoncons/json_error.hpp>
#include <jsoncons/json_options.hpp>
#include <jsoncons_ext/jsonpath/json_query.hpp>
#include <jsoncons_ext/jsonpath/jsonpath_error.hpp>
#include <limits>

#include "jsoncons_ext/jsonpath/jsonpath_error.hpp"
#include "status.h"

struct JsonValue {
JsonValue() = default;
explicit JsonValue(jsoncons::basic_json<char> value) : value(std::move(value)) {}

static StatusOr<JsonValue> FromString(std::string_view str) {
static StatusOr<JsonValue> FromString(std::string_view str, int max_nesting_depth = std::numeric_limits<int>::max()) {
jsoncons::json val;

jsoncons::json_options options;
options.max_nesting_depth(max_nesting_depth);

try {
val = jsoncons::json::parse(str);
val = jsoncons::json::parse(str, options);
} catch (const jsoncons::ser_error &e) {
return {Status::NotOK, e.what()};
}

return JsonValue(std::move(val));
}

std::string Dump() const {
StatusOr<std::string> Dump(int max_nesting_depth = std::numeric_limits<int>::max()) const {
std::string res;
GET_OR_RET(Dump(&res, max_nesting_depth));
return res;
}

Status Dump(std::string *buffer, int max_nesting_depth = std::numeric_limits<int>::max()) const {
jsoncons::json_options options;
options.max_nesting_depth(max_nesting_depth);

jsoncons::compact_json_string_encoder encoder{*buffer, options};
std::error_code ec;
value.dump(encoder, ec);
if (ec) {
return {Status::NotOK, ec.message()};
}

return Status::OK();
}

StatusOr<std::string> Print(uint8_t indent_size = 0, bool spaces_after_colon = false,
const std::string &new_line_chars = "") const {
std::string res;
Dump(&res);
GET_OR_RET(Print(&res, indent_size, spaces_after_colon, new_line_chars));
return res;
}

void Dump(std::string *buffer) const {
jsoncons::compact_json_string_encoder encoder{*buffer};
value.dump(encoder);
Status Print(std::string *buffer, uint8_t indent_size = 0, bool spaces_after_colon = false,
const std::string &new_line_chars = "") const {
jsoncons::json_options options;
options.indent_size(indent_size);
options.spaces_around_colon(spaces_after_colon ? jsoncons::spaces_option::space_after
: jsoncons::spaces_option::no_spaces);
options.spaces_around_comma(jsoncons::spaces_option::no_spaces);
options.new_line_chars(new_line_chars);

jsoncons::json_string_encoder encoder{*buffer, options};
std::error_code ec;
value.dump(encoder, ec);
if (ec) {
return {Status::NotOK, ec.message()};
}

return Status::OK();
}

Status Set(std::string_view path, JsonValue &&new_value) {
Expand Down
9 changes: 6 additions & 3 deletions src/types/redis_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ rocksdb::Status Json::write(Slice ns_key, JsonMetadata *metadata, const JsonValu

std::string val;
metadata->Encode(&val);
json_val.Dump(&val);
auto s = json_val.Dump(&val, storage_->GetConfig()->json_max_nesting_depth);
if (!s) {
return rocksdb::Status::InvalidArgument("Failed to encode JSON into storage: " + s.Msg());
}

batch->Put(metadata_cf_handle_, ns_key, val);

Expand All @@ -55,7 +58,7 @@ rocksdb::Status Json::Set(const std::string &user_key, const std::string &path,
if (s.IsNotFound()) {
if (path != "$") return rocksdb::Status::InvalidArgument("new objects must be created at the root");

auto json_res = JsonValue::FromString(value);
auto json_res = JsonValue::FromString(value, storage_->GetConfig()->json_max_nesting_depth);
if (!json_res) return rocksdb::Status::InvalidArgument(json_res.Msg());
auto json_val = *std::move(json_res);

Expand All @@ -67,7 +70,7 @@ rocksdb::Status Json::Set(const std::string &user_key, const std::string &path,
if (metadata.format != JsonStorageFormat::JSON)
return rocksdb::Status::NotSupported("JSON storage format not supported");

auto new_res = JsonValue::FromString(value);
auto new_res = JsonValue::FromString(value, storage_->GetConfig()->json_max_nesting_depth);
if (!new_res) return rocksdb::Status::InvalidArgument(new_res.Msg());
auto new_val = *std::move(new_res);

Expand Down
54 changes: 36 additions & 18 deletions tests/cppunit/types/json_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,75 +47,93 @@ TEST_F(RedisJsonTest, Set) {

ASSERT_TRUE(json_->Set(key_, "$", " \t{\n } ").ok());
ASSERT_TRUE(json_->Get(key_, {}, &json_val_).ok());
ASSERT_EQ(json_val_.Dump(), "{}");
ASSERT_EQ(json_val_.Dump().GetValue(), "{}");

ASSERT_TRUE(json_->Set(key_, "$", "1").ok());
ASSERT_TRUE(json_->Get(key_, {}, &json_val_).ok());
ASSERT_EQ(json_val_.Dump(), "1");
ASSERT_EQ(json_val_.Dump().GetValue(), "1");

ASSERT_TRUE(json_->Set(key_, "$", "[1, 2, 3]").ok());
ASSERT_TRUE(json_->Get(key_, {}, &json_val_).ok());
ASSERT_EQ(json_val_.Dump(), "[1,2,3]");
ASSERT_EQ(json_val_.Dump().GetValue(), "[1,2,3]");

ASSERT_TRUE(json_->Set(key_, "$[1]", "233").ok());
ASSERT_TRUE(json_->Get(key_, {}, &json_val_).ok());
ASSERT_EQ(json_val_.Dump(), "[1,233,3]");
ASSERT_EQ(json_val_.Dump().GetValue(), "[1,233,3]");

ASSERT_TRUE(json_->Set(key_, "$", "[[1,2],[3,4],[5,6]]").ok());
ASSERT_TRUE(json_->Set(key_, "$[*][1]", R"("x")").ok());
ASSERT_TRUE(json_->Get(key_, {}, &json_val_).ok());
ASSERT_EQ(json_val_.Dump(), R"([[1,"x"],[3,"x"],[5,"x"]])");
ASSERT_EQ(json_val_.Dump().GetValue(), R"([[1,"x"],[3,"x"],[5,"x"]])");

ASSERT_TRUE(json_->Set(key_, "$", R"({"x":1,"y":2, "z":3})").ok());
ASSERT_TRUE(json_->Set(key_, "$.x", "[1,2,3]").ok());
ASSERT_TRUE(json_->Get(key_, {}, &json_val_).ok());
ASSERT_EQ(json_val_.Dump(), R"({"x":[1,2,3],"y":2,"z":3})");
ASSERT_EQ(json_val_.Dump().GetValue(), R"({"x":[1,2,3],"y":2,"z":3})");

ASSERT_TRUE(json_->Set(key_, "$.y", R"({"a":"xxx","x":2})").ok());
ASSERT_TRUE(json_->Get(key_, {}, &json_val_).ok());
ASSERT_EQ(json_val_.Dump(), R"({"x":[1,2,3],"y":{"a":"xxx","x":2},"z":3})");
ASSERT_EQ(json_val_.Dump().GetValue(), R"({"x":[1,2,3],"y":{"a":"xxx","x":2},"z":3})");

ASSERT_TRUE(json_->Set(key_, "$..x", "true").ok());
ASSERT_TRUE(json_->Get(key_, {}, &json_val_).ok());
ASSERT_EQ(json_val_.Dump(), R"({"x":true,"y":{"a":"xxx","x":true},"z":3})");
ASSERT_EQ(json_val_.Dump().GetValue(), R"({"x":true,"y":{"a":"xxx","x":true},"z":3})");

ASSERT_THAT(json_->Set(key_, "...", "1").ToString(), MatchesRegex("Invalid.*"));
ASSERT_THAT(json_->Set(key_, "[", "1").ToString(), MatchesRegex("Invalid.*"));

ASSERT_TRUE(json_->Set(key_, "$", "[[1,2],[[5,6],4]] ").ok());
ASSERT_TRUE(json_->Set(key_, "$..[0]", "{}").ok());
ASSERT_TRUE(json_->Get(key_, {}, &json_val_).ok());
ASSERT_EQ(json_val_.Dump(), R"([{},[{},4]])");
ASSERT_EQ(json_val_.Dump().GetValue(), R"([{},[{},4]])");

ASSERT_TRUE(json_->Del(key_).ok());
ASSERT_TRUE(json_->Set(key_, "$", "[{ }, [ ]]").ok());
ASSERT_TRUE(json_->Get(key_, {}, &json_val_).ok());
ASSERT_EQ(json_val_.Dump(), "[{},[]]");
ASSERT_EQ(json_val_.Dump().GetValue(), "[{},[]]");
ASSERT_THAT(json_->Set(key_, "$[1]", "invalid").ToString(), MatchesRegex(".*syntax_error.*"));
ASSERT_TRUE(json_->Del(key_).ok());
}

TEST_F(RedisJsonTest, Get) {
ASSERT_TRUE(json_->Set(key_, "$", R"({"x":[1,2,{"z":3}],"y":[]})").ok());
ASSERT_TRUE(json_->Get(key_, {}, &json_val_).ok());
ASSERT_EQ(json_val_.Dump(), R"({"x":[1,2,{"z":3}],"y":[]})");
ASSERT_EQ(json_val_.Dump().GetValue(), R"({"x":[1,2,{"z":3}],"y":[]})");
ASSERT_TRUE(json_->Get(key_, {"$"}, &json_val_).ok());
ASSERT_EQ(json_val_.Dump(), R"([{"x":[1,2,{"z":3}],"y":[]}])");
ASSERT_EQ(json_val_.Dump().GetValue(), R"([{"x":[1,2,{"z":3}],"y":[]}])");
ASSERT_TRUE(json_->Get(key_, {"$.y"}, &json_val_).ok());
ASSERT_EQ(json_val_.Dump(), R"([[]])");
ASSERT_EQ(json_val_.Dump().GetValue(), R"([[]])");
ASSERT_TRUE(json_->Get(key_, {"$.y[0]"}, &json_val_).ok());
ASSERT_EQ(json_val_.Dump(), R"([])");
ASSERT_EQ(json_val_.Dump().GetValue(), R"([])");
ASSERT_TRUE(json_->Get(key_, {"$.z"}, &json_val_).ok());
ASSERT_EQ(json_val_.Dump(), R"([])");
ASSERT_EQ(json_val_.Dump().GetValue(), R"([])");
ASSERT_THAT(json_->Get(key_, {"[[["}, &json_val_).ToString(), MatchesRegex("Invalid.*"));

ASSERT_TRUE(json_->Set(key_, "$", R"([[[1,2],[3]],[4,5]])").ok());
ASSERT_TRUE(json_->Get(key_, {"$..[0]"}, &json_val_).ok());
ASSERT_EQ(json_val_.Dump(), R"([[[1,2],[3]],[1,2],1,3,4])");
ASSERT_EQ(json_val_.Dump().GetValue(), R"([[[1,2],[3]],[1,2],1,3,4])");
ASSERT_TRUE(json_->Get(key_, {"$[0][1][0]", "$[1][1]"}, &json_val_).ok());
ASSERT_EQ(json_val_.Dump(), R"({"$[0][1][0]":[3],"$[1][1]":[5]})");
ASSERT_EQ(json_val_.Dump().GetValue(), R"({"$[0][1][0]":[3],"$[1][1]":[5]})");

ASSERT_TRUE(json_->Set(key_, "$", R"({"x":{"y":1},"y":[2,{"z":3}],"z":{"a":{"x":4}}})").ok());
ASSERT_TRUE(json_->Get(key_, {"$..x", "$..y", "$..z"}, &json_val_).ok());
ASSERT_EQ(json_val_.Dump(), R"({"$..x":[{"y":1},4],"$..y":[[2,{"z":3}],1],"$..z":[{"a":{"x":4}},3]})");
ASSERT_EQ(json_val_.Dump().GetValue(), R"({"$..x":[{"y":1},4],"$..y":[[2,{"z":3}],1],"$..z":[{"a":{"x":4}},3]})");
}

TEST_F(RedisJsonTest, Print) {
auto json = *JsonValue::FromString("[1,2,3]");
ASSERT_EQ(json.Print().GetValue(), "[1,2,3]");
ASSERT_EQ(json.Print(1).GetValue(), "[ 1, 2, 3]");
ASSERT_EQ(json.Print(0, true).GetValue(), "[1,2,3]");
ASSERT_EQ(json.Print(0, false, std::string("\n")).GetValue(), "[\n1,\n2,\n3\n]");
ASSERT_EQ(json.Print(1, false, std::string("\n")).GetValue(), "[\n 1,\n 2,\n 3\n]");
ASSERT_EQ(json.Print(1, true, std::string("\n")).GetValue(), "[\n 1,\n 2,\n 3\n]");

json = *JsonValue::FromString(R"({"a":1 ,"b":2})");
ASSERT_EQ(json.Print().GetValue(), R"({"a":1,"b":2})");
ASSERT_EQ(json.Print(1).GetValue(), R"({ "a":1, "b":2})");
ASSERT_EQ(json.Print(0, true).GetValue(), R"({"a": 1,"b": 2})");
ASSERT_EQ(json.Print(0, false, std::string("\n")).GetValue(), "{\n\"a\":1,\n\"b\":2\n}");
ASSERT_EQ(json.Print(1, false, std::string("\n")).GetValue(), "{\n \"a\":1,\n \"b\":2\n}");
ASSERT_EQ(json.Print(1, true, std::string("\n")).GetValue(), "{\n \"a\": 1,\n \"b\": 2\n}");
}
9 changes: 9 additions & 0 deletions tests/gocase/unit/type/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,13 @@ func TestJson(t *testing.T) {
require.Equal(t, rdb.Do(ctx, "JSON.GET", "a", "$..x").Val(), `[1,{"y":2}]`)
require.Equal(t, rdb.Do(ctx, "JSON.GET", "a", "$..x", "$..y").Val(), `{"$..x":[1,{"y":2}],"$..y":[{"x":{"y":2},"y":3},3,2]}`)
})

t.Run("JSON.GET with options", func(t *testing.T) {
require.NoError(t, rdb.Do(ctx, "JSON.SET", "a", "$", ` {"x":1, "y":2} `).Err())
require.Equal(t, rdb.Do(ctx, "JSON.GET", "a", "INDENT", " ").Val(), `{ "x":1, "y":2}`)
require.Equal(t, rdb.Do(ctx, "JSON.GET", "a", "INDENT", " ", "SPACE", " ").Val(), `{ "x": 1, "y": 2}`)
require.Equal(t, rdb.Do(ctx, "JSON.GET", "a", "NEWLINE", "\n").Val(), "{\n\"x\":1,\n\"y\":2\n}")
require.Equal(t, rdb.Do(ctx, "JSON.GET", "a", "NEWLINE", "\n", "INDENT", " ", "SPACE", " ").Val(), "{\n \"x\": 1,\n \"y\": 2\n}")
require.Equal(t, rdb.Do(ctx, "JSON.GET", "a", "INDENT", " ", "$").Val(), `[ { "x":1, "y":2 }]`)
})
}