git-hulk commented on code in PR #1890:
URL: https://github.com/apache/kvrocks/pull/1890#discussion_r1389597563
##########
src/commands/cmd_json.cc:
##########
@@ -378,6 +420,8 @@
REDIS_REGISTER_COMMANDS(MakeCmdAttr<CommandJsonSet>("json.set", 4, "write", 1, 1
MakeCmdAttr<CommandJsonArrLen>("json.arrlen", -2,
"read-only", 1, 1, 1),
MakeCmdAttr<CommandJsonObjkeys>("json.objkeys", -2,
"read-only", 1, 1, 1),
MakeCmdAttr<CommandJsonArrPop>("json.arrpop", -2,
"write", 1, 1, 1),
- MakeCmdAttr<CommanderJsonArrIndex>("json.arrindex",
-4, "read-only", 1, 1, 1), );
+ MakeCmdAttr<CommanderJsonArrIndex>("json.arrindex",
-4, "read-only", 1, 1, 1),
+ MakeCmdAttr<CommandJsonNumIncrBy>("json.numincrby",
-4, "write", 1, 1, 1),
+ MakeCmdAttr<CommandJsonNumMultBy>("json.nummultby",
-4, "write", 1, 1, 1), );
Review Comment:
```suggestion
MakeCmdAttr<CommandJsonNumMultBy>("json.nummultby",
4, "write", 1, 1, 1), );
```
##########
src/commands/cmd_json.cc:
##########
@@ -368,6 +368,48 @@ class CommanderJsonArrIndex : public Commander {
ssize_t end_;
};
+class CommandJsonNumIncrBy : public Commander {
+ public:
+ Status Execute(Server *svr, Connection *conn, std::string *output) override {
+ redis::Json json(svr->storage, conn->GetNamespace());
+
+ if (args_.size() != 4) {
+ return {Status::RedisExecErr, errWrongNumOfArguments};
+ }
+
+ JsonValue result = JsonValue::FromString("[]").GetValue();
+ auto s = json.NumIncrBy(args_[1], args_[2], args_[3], &result);
+ if (!s.ok()) {
+ return {Status::RedisExecErr, s.ToString()};
+ }
+
+ *output = redis::BulkString(result.value.to_string());
+
+ return Status::OK();
+ }
+};
+
+class CommandJsonNumMultBy : public Commander {
+ public:
+ Status Execute(Server *svr, Connection *conn, std::string *output) override {
+ redis::Json json(svr->storage, conn->GetNamespace());
+
+ if (args_.size() != 4) {
+ return {Status::RedisExecErr, errWrongNumOfArguments};
+ }
Review Comment:
```suggestion
```
Can remove those lines since the command parser has checked this.
##########
src/types/json.h:
##########
@@ -363,6 +368,55 @@ struct JsonValue {
return popped_values;
}
+ template <typename T>
+ static inline T NumOpIncrBy(T origin, T value) {
+ return origin + value;
+ }
+
+ template <typename T>
+ static inline T NumOpMulBy(T origin, T value) {
+ return origin * value;
+ }
+
+ Status NumOp(std::string_view path, const JsonValue &number, NumOpEnum op,
JsonValue *result) {
+ Status status = Status::OK();
+ try {
+ jsoncons::jsonpath::json_replace(value, path, [&](const std::string &
/*path*/, jsoncons::json &origin) {
+ if (!status.IsOK()) {
+ return;
+ }
+ if (!origin.is_number()) {
+ result->value.push_back(jsoncons::json::null());
+ return;
+ }
+ if (number.value.is_double() || origin.is_double()) {
+ double v = 0;
+ if (op == NumOpEnum::Incr) {
+ v = NumOpIncrBy<double>(origin.as_double(),
number.value.as_double());
+ } else if (op == NumOpEnum::Mul) {
+ v = NumOpMulBy<double>(origin.as_double(),
number.value.as_double());
+ }
+ if (std::isinf(v)) {
+ status = {Status::RedisExecErr, "result is not number"};
+ return;
+ } else {
+ origin = v;
+ }
Review Comment:
```suggestion
if (std::isinf(v)) {
status = {Status::RedisExecErr, "result is not a number"};
return;
}
origin = v;
```
##########
src/types/redis_json.cc:
##########
@@ -304,4 +304,38 @@ rocksdb::Status Json::ObjKeys(const std::string &user_key,
const std::string &pa
return rocksdb::Status::OK();
}
+rocksdb::Status Json::NumIncrBy(const std::string &user_key, const std::string
&path, const std::string &value,
+ JsonValue *result) {
+ return numop(NumOpEnum::Incr, user_key, path, value, result);
+}
+
+rocksdb::Status Json::NumMultBy(const std::string &user_key, const std::string
&path, const std::string &value,
+ JsonValue *result) {
+ return numop(NumOpEnum::Mul, user_key, path, value, result);
+}
+
+rocksdb::Status Json::numop(NumOpEnum op, const std::string &user_key, const
std::string &path,
+ const std::string &value, JsonValue *result) {
+ JsonValue number;
+ auto number_res = JsonValue::FromString(value);
+ if (!number_res || !number_res.GetValue().value.is_number()) {
+ return rocksdb::Status::InvalidArgument("should be number");
Review Comment:
```suggestion
return rocksdb::Status::InvalidArgument("should be a number");
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]