You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@kvrocks.apache.org by "Yangsx-1 (via GitHub)" <gi...@apache.org> on 2023/07/08 14:37:55 UTC

[GitHub] [kvrocks] Yangsx-1 commented on a diff in pull request #1565: Add the support of the HRANDFIELD command

Yangsx-1 commented on code in PR #1565:
URL: https://github.com/apache/kvrocks/pull/1565#discussion_r1257281914


##########
src/commands/cmd_hash.cc:
##########
@@ -376,6 +376,52 @@ class CommandHScan : public CommandSubkeyScanBase {
     return Status::OK();
   }
 };
+class CommandHRandField : public Commander {
+ public:
+  Status Parse(const std::vector<std::string> &args) override {
+    if (args.size() >= 3) {
+      auto parse_result = ParseInt<int64_t>(args[2], 10);
+      if (!parse_result) {
+        return {Status::RedisParseErr, errValueNotInteger};
+      }
+      if (*parse_result >= 0) {
+        count_ = *parse_result;
+      } else {
+        count_ = -*parse_result;
+        uniq_ = false;
+      }
+      //如果有withvalue这个参数,就一定有前面的count
+      if (args.size() > 4 || (args.size() == 4 && strcasecmp(args[3].c_str(), "withvalues"))) {
+        return {Status::RedisParseErr, errInvalidSyntax};
+      } else if (args.size() == 4) {
+        withvalues_ = true;
+      }
+    }
+    return Commander::Parse(args);
+  }
+  Status Execute(Server *svr, Connection *conn, std::string *output) override {
+    redis::Hash hash_db(svr->storage, conn->GetNamespace());
+    std::vector<FieldValue> field_values;
+    auto s = hash_db.RandField(args_[1], &field_values, count_, uniq_,
+                               withvalues_ ? HashFetchType::kAll : HashFetchType::kOnlyKey);
+    if (!s.ok()) {

Review Comment:
   It should return `nil` if the 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: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org