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

Add the support of JSON.TYPE command #1847

Merged
merged 4 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
26 changes: 26 additions & 0 deletions src/commands/cmd_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,34 @@ class CommandJsonArrAppend : public Commander {
}
};

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

std::vector<std::string> types;

std::string path = "$";
if (args_.size() == 2) {
path = args_[2];
} else if (args_.size() > 2) {
return {Status::RedisExecErr, "The number of arguments is more than expected"};
}
git-hulk marked this conversation as resolved.
Show resolved Hide resolved
auto s = json.Type(args_[1], path, &types);
if (!s.ok() && !s.IsNotFound()) return {Status::RedisExecErr, s.ToString()};
if (s.IsNotFound()) {
*output = redis::NilString();
return Status::OK();
}

*output = redis::MultiBulkString(types);
return Status::OK();
}
};

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

} // namespace redis
38 changes: 38 additions & 0 deletions src/types/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,44 @@ struct JsonValue {
return Status::OK();
}

Status Type(std::string_view path, std::vector<std::string> *types) const {
types->clear();
try {
jsoncons::jsonpath::json_query(value, path, [&types](const std::string & /*path*/, const jsoncons::json &val) {
switch (val.type()) {
case jsoncons::json_type::null_value:
types->emplace_back("null");
break;
case jsoncons::json_type::bool_value:
types->emplace_back("boolean");
break;
case jsoncons::json_type::int64_value:
case jsoncons::json_type::uint64_value:
types->emplace_back("integer");
break;
case jsoncons::json_type::double_value:
types->emplace_back("number");
Comment on lines +150 to +151
Copy link
Member

Choose a reason for hiding this comment

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

Should we support half here?

Copy link
Member

Choose a reason for hiding this comment

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

break;
case jsoncons::json_type::string_value:
Copy link
Member

Choose a reason for hiding this comment

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

So binary is not supported?

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 it is not a type in JSON documents.

types->emplace_back("string");
break;
case jsoncons::json_type::array_value:
types->emplace_back("array");
break;
case jsoncons::json_type::object_value:
types->emplace_back("object");
break;
default:
types->emplace_back("unknown");
break;
}
});
} catch (const jsoncons::jsonpath::jsonpath_error &e) {
return {Status::NotOK, e.what()};
}
return Status::OK();
}

JsonValue(const JsonValue &) = default;
JsonValue(JsonValue &&) = default;

Expand Down
12 changes: 12 additions & 0 deletions src/types/redis_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,16 @@ rocksdb::Status Json::ArrAppend(const std::string &user_key, const std::string &
return write(ns_key, &metadata, value);
}

rocksdb::Status Json::Type(const std::string &user_key, const std::string &path, std::vector<std::string> *results) {
auto ns_key = AppendNamespacePrefix(user_key);

JsonMetadata metadata;
JsonValue json_val;
auto s = read(ns_key, &metadata, &json_val);
if (!s.ok()) return s;

auto res = json_val.Type(path, results);
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 @@ -35,6 +35,7 @@ class Json : public Database {

rocksdb::Status Set(const std::string &user_key, const std::string &path, const std::string &value);
rocksdb::Status Get(const std::string &user_key, const std::vector<std::string> &paths, JsonValue *result);
rocksdb::Status Type(const std::string &user_key, const std::string &path, std::vector<std::string> *results);
rocksdb::Status ArrAppend(const std::string &user_key, const std::string &path,
const std::vector<std::string> &values, std::vector<uint64_t> *result_count);

Expand Down
33 changes: 33 additions & 0 deletions tests/gocase/unit/type/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"context"
"testing"

"github.com/redis/go-redis/v9"

"github.com/apache/kvrocks/tests/gocase/util"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -95,4 +97,35 @@ func TestJson(t *testing.T) {
require.Equal(t, `[]`, rdb.Do(ctx, "JSON.GET", "a", "$.x.x").Val())
require.Equal(t, `[{"x":[1,1,2,3,1,2],"y":[{"x":[1,2],"y":[]}]}]`, rdb.Do(ctx, "JSON.GET", "a", "$").Val())
})

t.Run("JSON.TYPE basics", func(t *testing.T) {
require.NoError(t, rdb.Do(ctx, "JSON.SET", "a", "$", `{"b":true,"x":1, "y":1.2, "z": {"x":[1,2,3], "y": null}, "v":{"x":"y"}}`).Err())

types, err := rdb.Do(ctx, "JSON.TYPE", "a").StringSlice()
require.NoError(t, err)
require.Equal(t, []string{"object"}, types)

types, err = rdb.Do(ctx, "JSON.TYPE", "a", "$").StringSlice()
require.NoError(t, err)
require.Equal(t, []string{"object"}, types)

types, err = rdb.Do(ctx, "JSON.TYPE", "a", "$..x").StringSlice()
require.NoError(t, err)
require.EqualValues(t, []string{"integer", "string", "array"}, types)

types, err = rdb.Do(ctx, "JSON.TYPE", "a", "$..y").StringSlice()
require.NoError(t, err)
require.EqualValues(t, []string{"number", "null"}, types)

types, err = rdb.Do(ctx, "JSON.TYPE", "a", "$.b").StringSlice()
require.NoError(t, err)
require.EqualValues(t, []string{"boolean"}, types)

types, err = rdb.Do(ctx, "JSON.TYPE", "a", "$.no_exists").StringSlice()
require.NoError(t, err)
require.EqualValues(t, []string{}, types)

_, err = rdb.Do(ctx, "JSON.TYPE", "not_exists", "$").StringSlice()
require.EqualError(t, err, redis.Nil.Error())
})
}
Loading