mapleFU commented on code in PR #1837:
URL: https://github.com/apache/kvrocks/pull/1837#discussion_r1365398131


##########
src/commands/cmd_json.cc:
##########
@@ -52,7 +52,35 @@ 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()) {

Review Comment:
   So this is to make a "nothing updated" `ArrAppend` to return a `Nil`?



##########
src/types/redis_json.cc:
##########
@@ -117,4 +117,41 @@ 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;
+  append_values.reserve(values.size());
+  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);

Review Comment:
   You can just `emplace_back( ... the moved value )`, I guess current copy 
might copy the value?



##########
tests/cppunit/types/json_test.cc:
##########
@@ -119,3 +119,42 @@ 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, ArrAppend) {
+  std::vector<uint64_t> res;
+
+  ASSERT_TRUE(json_->Set(key_, "$", R"({"x":1,"y":[]})").ok());
+  ASSERT_TRUE(json_->ArrAppend(key_, "$.x", {"1"}, &res).ok());
+  ASSERT_EQ(res.size(), 1);
+  ASSERT_EQ(res[0], 0);
+  ASSERT_TRUE(json_->Get(key_, {}, &json_val_).ok());
+  ASSERT_EQ(json_val_.Dump(), R"({"x":1,"y":[]})");
+  res.clear();
+
+  ASSERT_TRUE(json_->Set(key_, "$", R"({"x":[1,2,{"z":3}],"y":[]})").ok());
+  ASSERT_TRUE(json_->ArrAppend(key_, "$.x", {"1"}, &res).ok());
+  ASSERT_EQ(res.size(), 1);
+  ASSERT_EQ(res[0], 4);
+  ASSERT_TRUE(json_->Get(key_, {}, &json_val_).ok());
+  ASSERT_EQ(json_val_.Dump(), R"({"x":[1,2,{"z":3},1],"y":[]})");
+  res.clear();
+
+  ASSERT_TRUE(json_->ArrAppend(key_, "$..y", {"1", "2", "3"}, &res).ok());
+  ASSERT_EQ(res.size(), 1);
+  ASSERT_EQ(res[0], 3);
+  ASSERT_TRUE(json_->Get(key_, {}, &json_val_).ok());
+  ASSERT_EQ(json_val_.Dump(), R"({"x":[1,2,{"z":3},1],"y":[1,2,3]})");
+  res.clear();
+

Review Comment:
   Do we need add a test for key is not exist?



-- 
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]

Reply via email to