mapleFU commented on code in PR #1837:
URL: https://github.com/apache/kvrocks/pull/1837#discussion_r1364885509
##########
src/types/json.h:
##########
@@ -73,6 +73,25 @@ struct JsonValue {
}
}
+ Status ArrAppend(std::string_view path, const std::vector<jsoncons::json>
&append_values,
+ std::vector<uint64_t> &result_count) {
Review Comment:
Prefer `std::vector<uint64_t>* result_count` to specify it's an output type
##########
src/types/redis_json.cc:
##########
@@ -117,4 +117,38 @@ rocksdb::Status Json::Get(const std::string &user_key,
const std::vector<std::st
return rocksdb::Status::OK();
}
+rocksdb::Status Json::ArrAppend(const std::string &user_key, const std::string
&path,
+ const std::vector<std::string> &values,
+ std::vector<uint64_t> &result_count) {
+ auto ns_key = AppendNamespacePrefix(user_key);
+
+ std::vector<jsoncons::json> append_values;
+ for(auto &v : values) {
+ auto value_res = JsonValue::FromString(v);
+ if(!value_res) return rocksdb::Status::InvalidArgument(value_res.Msg());
+ auto value = *std::move(value_res);
+ append_values.emplace_back(value.value);
+ }
+
+ LockGuard guard(storage_->GetLockManager(), ns_key);
+
+ std::string bytes;
+ JsonMetadata metadata;
+ Slice rest;
+ auto s = GetMetadata(kRedisJson, ns_key, &bytes, &metadata, &rest);
+ if (!s.ok()) return s;
+
+ if (metadata.format != JsonStorageFormat::JSON)
+ return rocksdb::Status::NotSupported("JSON storage format not supported");
+
+ auto value_res = JsonValue::FromString(rest.ToStringView());
+ if(!value_res) return rocksdb::Status::Corruption(value_res.Msg());
+ auto value = *std::move(value_res);
+
+ auto append_res = value.ArrAppend(path, append_values, result_count);
+ if(!append_res) return rocksdb::Status::InvalidArgument(append_res.Msg());
+
+ return write(ns_key, &metadata, value);
Review Comment:
Can we optimize the case when nothing in `value` has been changed?
##########
src/commands/cmd_json.cc:
##########
@@ -52,7 +52,37 @@ class CommandJsonGet : public Commander {
}
};
+class CommandJsonArrAppend : public Commander {
+ public:
+ Status Execute(Server *svr, Connection *conn, std::string *output) override {
+ redis::Json json(svr->storage, conn->GetNamespace());
+
+ std::vector<uint64_t> result_count;
+
+ auto s = json.ArrAppend(args_[1], args_[2], {args_.begin() + 3,
args_.end()}, result_count);
+ if (!s.ok()) return {Status::RedisExecErr, s.ToString()};
+
+ if(result_count.empty()) {
+ result_count.emplace_back(0);
+ }
+
+ std::vector<std::string> result_values;
+ result_values.reserve(result_count.size());
+ for(auto c : result_count) {
Review Comment:
```suggestion
for(uint64_t c : result_count) {
```
Since `uint64_t` is not a too complex type, maybe we'd better using literal
rather than auto
##########
src/types/json.h:
##########
@@ -73,6 +73,25 @@ struct JsonValue {
}
}
+ Status ArrAppend(std::string_view path, const std::vector<jsoncons::json>
&append_values,
+ std::vector<uint64_t> &result_count) {
+ try {
+ std::vector<std::string> paths;
Review Comment:
`paths` Not used?
##########
src/types/redis_json.cc:
##########
@@ -117,4 +117,38 @@ rocksdb::Status Json::Get(const std::string &user_key,
const std::vector<std::st
return rocksdb::Status::OK();
}
+rocksdb::Status Json::ArrAppend(const std::string &user_key, const std::string
&path,
+ const std::vector<std::string> &values,
+ std::vector<uint64_t> &result_count) {
+ auto ns_key = AppendNamespacePrefix(user_key);
+
+ std::vector<jsoncons::json> append_values;
Review Comment:
better `reserve` here?
--
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]