PragmaTwice commented on code in PR #2238:
URL: https://github.com/apache/kvrocks/pull/2238#discussion_r1562285186


##########
src/commands/cmd_key.cc:
##########
@@ -363,20 +361,80 @@ class CommandRenameNX : public Commander {
  public:
   Status Execute(Server *srv, Connection *conn, std::string *output) override {
     redis::Database redis(srv->storage, conn->GetNamespace());
-    bool ret = true;
-    bool key_exist = true;
+    Database::CopyResult res = Database::CopyResult::DONE;
     std::string ns_key = redis.AppendNamespacePrefix(args_[1]);
     std::string new_ns_key = redis.AppendNamespacePrefix(args_[2]);
-    auto s = redis.Move(ns_key, new_ns_key, true, &ret, &key_exist);
+    auto s = redis.Copy(ns_key, new_ns_key, true, true, &res);
     if (!s.ok()) return {Status::RedisExecErr, s.ToString()};
-    if (!key_exist) return {Status::RedisExecErr, 
rocksdb::Status::InvalidArgument("ERR no such key").ToString()};
-    if (ret) {
-      *output = redis::Integer(1);
-    } else {
-      *output = redis::Integer(0);
+    switch (res) {
+      case Database::CopyResult::KEY_NOT_EXIST:
+        return {Status::RedisExecErr, rocksdb::Status::InvalidArgument("ERR no 
such key").ToString()};
+      case Database::CopyResult::DONE:
+        *output = redis::Integer(1);
+        break;
+      case Database::CopyResult::KEY_ALREADY_EXIST:
+        *output = redis::Integer(0);
+        break;
+    }
+    return Status::OK();
+  }
+};
+
+class CommandCopy : public Commander {
+ public:
+  Status Parse(const std::vector<std::string> &args) override {
+    if (args.size() > 6) {
+      return {Status::RedisParseErr, errWrongNumOfArguments};
+    }
+    if (args.size() == 4) {
+      if (util::ToLower(args[3]) == "replace") {
+        replace_ = true;
+      } else {
+        return {Status::RedisParseErr, errInvalidSyntax};
+      }
+    } else if (args.size() >= 5) {
+      if (util::ToLower(args[3]) != "db") {
+        return {Status::RedisParseErr, errInvalidSyntax};
+      }
+      auto db_num = ParseInt<int64_t>(args[4], 10);
+      // There's only one database in Kvrocks, so the DB must be 0 here.
+      if (!db_num.IsOK() || db_num.GetValue() != 0) {
+        return {Status::RedisParseErr, errInvalidSyntax};
+      }
+      if (args.size() == 6) {
+        if (util::ToLower(args[5]) == "replace") {
+          replace_ = true;
+        } else {
+          return {Status::RedisParseErr, errInvalidSyntax};
+        }
+      }

Review Comment:
   Please rewrite via `CommandParser`.



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