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

JSON: support basic legacy path #1829

Open
wants to merge 13 commits into
base: unstable
Choose a base branch
from
5 changes: 4 additions & 1 deletion src/commands/cmd_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

namespace redis {

///
/// JSON.SET <key> <path> <json>
///
class CommandJsonSet : public Commander {
public:
Status Execute(Server *svr, Connection *conn, std::string *output) override {
Expand Down Expand Up @@ -52,7 +55,7 @@ class CommandJsonGet : public Commander {
}
};

REDIS_REGISTER_COMMANDS(MakeCmdAttr<CommandJsonSet>("json.set", -3, "write", 1, 1, 1),
REDIS_REGISTER_COMMANDS(MakeCmdAttr<CommandJsonSet>("json.set", 4, "write", 1, 1, 1),
MakeCmdAttr<CommandJsonGet>("json.get", -2, "read-only", 1, 1, 1), );

} // namespace redis
66 changes: 66 additions & 0 deletions src/types/json.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/

#include "json.h"

StatusOr<JsonPath> JsonPath::BuildJsonPath(std::string_view path) {
std::string fixed_path;
std::optional<std::string> converted = tryConvertLegacyToJsonPath(path);
if (converted.has_value()) {
fixed_path = std::move(converted.value());
}
std::string_view json_string;
if (fixed_path.empty()) {
json_string = path;
} else {
json_string = fixed_path;
}

std::error_code json_parse_error;
auto path_expression = jsoncons::jsonpath::make_expression<JsonType>(json_string, json_parse_error);
if (json_parse_error) {
return {Status::NotOK, json_parse_error.message()};
}
return JsonPath(std::string(json_string), !fixed_path.empty(), std::move(path_expression));
Copy link
Member

Choose a reason for hiding this comment

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

I think maybe we can avoid re-construct json_string while it is fixed_path.

}

// https://redis.io/docs/data-types/json/path/#legacy-path-syntax
// The logic here is port from RedisJson `Path::new`, see
// https://github.com/RedisJSON/RedisJSON/blob/d4ba815fb13412c879b2cd3cd29ab60aee3c211b/redis_json/src/redisjson.rs#L109
std::optional<std::string> JsonPath::tryConvertLegacyToJsonPath(std::string_view path) {
if (path.empty()) {
return std::nullopt;
}
if (path[0] == '$') {
if (path.size() == 1) {
return std::nullopt;
}
if (path[1] == '.' || path[1] == '[') {
return std::nullopt;
}
}
if (path[0] == '.') {
if (path.size() == 1) {
return "$";
}
return std::string("$") + std::string(path);
}
return std::string("$.") + std::string(path);
}
61 changes: 55 additions & 6 deletions src/types/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,51 @@
#include <jsoncons/json.hpp>
#include <jsoncons/json_error.hpp>
#include <jsoncons_ext/jsonpath/json_query.hpp>
#include <jsoncons_ext/jsonpath/jsonpath_error.hpp>

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

class JsonPath {
public:
using JsonType = jsoncons::json;
using JsonPathExpression = jsoncons::jsonpath::jsonpath_expression<JsonType, const JsonType &>;

static constexpr std::string_view ROOT_PATH = "$";

static StatusOr<JsonPath> BuildJsonPath(std::string_view path);
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
static StatusOr<JsonPath> BuildJsonPath(std::string_view path);
static StatusOr<JsonPath> FromString(std::string_view path);


bool IsLegacy() const noexcept { return is_legacy_; }

std::string_view Path() const { return path_; }

bool IsRootPath() const { return Path() == ROOT_PATH; }

JsonType EvalJsonQuery(const JsonType &json_value) const {
return expression_.evaluate(const_cast<JsonType &>(json_value));
}
Comment on lines +45 to +47
Copy link
Member

Choose a reason for hiding this comment

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

Could it return multiple query result? e.g. query $..a from {"a":{"a":1}}, we'll get {"a":1} and 1.


template <typename BinaryJsonFunction>
void EvalJsonReplace(JsonType &json_value, const BinaryJsonFunction &callback) const {
auto wrapped_cb = [&callback](const std::string_view &path, const JsonType &json) {
// Though JsonPath supports mutable reference, `jsoncons::make_expression`
// only supports const reference, so const_cast is used as a workaround.
callback(path, const_cast<JsonType &>(json));
};
expression_.evaluate(json_value, wrapped_cb);
}
Comment on lines +50 to +57
Copy link
Member

Choose a reason for hiding this comment

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

Same as #1829 (comment), maybe json_replace has no const_cast?

Copy link
Member Author

Choose a reason for hiding this comment

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

json_replace has use a different expression 😅 I don't know how jsoncons consider it. I'll dive into it

Copy link
Member Author

Choose a reason for hiding this comment

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

danielaparker/jsoncons#466

@PragmaTwice The author says he will implement a mutable in next release. Let me make this patch small and waiting for next release


private:
static std::optional<std::string> tryConvertLegacyToJsonPath(std::string_view path);

JsonPath(std::string path, bool is_legacy, JsonPathExpression path_expression)
: path_(std::move(path)), is_legacy_(is_legacy), expression_(std::move(path_expression)) {}

std::string path_;
bool is_legacy_;
// Pre-build the expression to avoid built it multiple times.
JsonPathExpression expression_;
};

struct JsonValue {
JsonValue() = default;
explicit JsonValue(jsoncons::basic_json<char> value) : value(std::move(value)) {}
Expand Down Expand Up @@ -54,20 +95,28 @@ struct JsonValue {
}

Status Set(std::string_view path, JsonValue &&new_value) {
auto path_expression = GET_OR_RET(JsonPath::BuildJsonPath(path));
return Set(path_expression, std::move(new_value));
}

Status Set(const JsonPath &path, JsonValue &&new_value) {
try {
jsoncons::jsonpath::json_replace(value, path, [&new_value](const std::string & /*path*/, jsoncons::json &origin) {
origin = new_value.value;
});
path.EvalJsonReplace(
value, [&new_value](const std::string_view & /*path*/, jsoncons::json &origin) { origin = new_value.value; });
} catch (const jsoncons::jsonpath::jsonpath_error &e) {
return {Status::NotOK, e.what()};
}

return Status::OK();
}

StatusOr<JsonValue> Get(std::string_view path) const {
auto path_expression = GET_OR_RET(JsonPath::BuildJsonPath(path));
return Get(path_expression);
}

StatusOr<JsonValue> Get(const JsonPath &json_path) const {
try {
return jsoncons::jsonpath::json_query(value, path);
return json_path.EvalJsonQuery(value);
} catch (const jsoncons::jsonpath::jsonpath_error &e) {
return {Status::NotOK, e.what()};
}
Expand Down
11 changes: 9 additions & 2 deletions src/types/redis_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@ rocksdb::Status Json::Set(const std::string &user_key, const std::string &path,
Slice rest;
auto s = GetMetadata(kRedisJson, ns_key, &bytes, &metadata, &rest);

auto json_path_opt = JsonPath::BuildJsonPath(path);
if (!json_path_opt) {
return rocksdb::Status::InvalidArgument(json_path_opt.Msg());
}
const auto &json_path = json_path_opt.GetValue();

if (s.IsNotFound()) {
if (path != "$") return rocksdb::Status::InvalidArgument("new objects must be created at the root");
if (!json_path.IsRootPath()) return rocksdb::Status::InvalidArgument("new objects must be created at the root");

auto json_res = JsonValue::FromString(value);
if (!json_res) return rocksdb::Status::InvalidArgument(json_res.Msg());
Expand All @@ -75,9 +81,10 @@ rocksdb::Status Json::Set(const std::string &user_key, const std::string &path,
if (!origin_res) return rocksdb::Status::Corruption(origin_res.Msg());
auto origin = *std::move(origin_res);

auto set_res = origin.Set(path, std::move(new_val));
auto set_res = origin.Set(json_path, std::move(new_val));
if (!set_res) return rocksdb::Status::InvalidArgument(set_res.Msg());

std::cout << "Value after set: " << origin.Dump() << std::endl;
return write(ns_key, &metadata, origin);
}

Expand Down
36 changes: 36 additions & 0 deletions tests/cppunit/types/json_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,39 @@ TEST_F(RedisJsonTest, Get) {
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]})");
}

TEST_F(RedisJsonTest, LegacyPath) {
ASSERT_THAT(json_->Set(key_, ".", "invalid").ToString(), MatchesRegex(".*syntax_error.*"));
ASSERT_THAT(json_->Set(key_, ".", "{").ToString(), MatchesRegex(".*Unexpected end of file.*"));

// Get and Set using "."
{
ASSERT_TRUE(json_->Set(key_, ".", " \t{\n } ").ok());
ASSERT_TRUE(json_->Get(key_, {"."}, &json_val_).ok());
ASSERT_EQ(json_val_.Dump(), "[{}]");

auto s = json_->Set(key_, ".", "1");
ASSERT_TRUE(s.ok()) << s.ToString();
ASSERT_TRUE(json_->Get(key_, {"."}, &json_val_).ok());
ASSERT_EQ(json_val_.Dump(), "[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]]");
}

// NOTE: RedisJson support legacy path like "$a", but currently
// we didn't support that.
{
ASSERT_TRUE(json_->Set(key_, ".", R"({"a":"xxx","x":2})").ok());
ASSERT_TRUE(json_->Get(key_, {"."}, &json_val_).ok());
ASSERT_EQ(json_val_.Dump(), R"([{"a":"xxx","x":2}])");
EXPECT_TRUE(json_->Set(key_, "a", "12").ok());
EXPECT_TRUE(json_->Get(key_, {"a"}, &json_val_).ok());
ASSERT_EQ(json_val_.Dump(), "[12]");
ASSERT_TRUE(json_->Get(key_, {".a"}, &json_val_).ok());
ASSERT_EQ(json_val_.Dump(), "[12]");
ASSERT_TRUE(json_->Get(key_, {"$.a"}, &json_val_).ok());
ASSERT_EQ(json_val_.Dump(), "[12]");
}
}