You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@kvrocks.apache.org by GitBox <gi...@apache.org> on 2022/11/13 06:48:10 UTC

[GitHub] [incubator-kvrocks] boatrainlsz commented on a diff in pull request #1116: feat: add support for Redis sintercard command

boatrainlsz commented on code in PR #1116:
URL: https://github.com/apache/incubator-kvrocks/pull/1116#discussion_r1020850182


##########
src/commands/redis_cmd.cc:
##########
@@ -2270,6 +2277,54 @@ class CommandSInter : public Commander {
   }
 };
 
+class CommandSInterCard : public Commander {
+ public:
+  Status Execute(Server *svr, Connection *conn, std::string *output) override {
+    // args_ will be like: sintercard 2 key1 key2 LIMIT 1, where 2 is the number of keys, limit is optional
+    uint64_t num_keys = 0;
+    auto parse_num_keys = ParseInt<uint64_t>(args_[1], 10);
+    if (!parse_num_keys) {
+      return Status(Status::RedisParseErr, errValueNotInteger);
+    }
+    num_keys = *parse_num_keys;
+    if (num_keys <= 0) {
+      return Status(Status::NotOK, "numkeys should be greater than 0");
+    }
+    if (args_.size() < num_keys + 2) {
+      return Status(Status::NotOK, "numkeys should be equal to the number of keys");
+    }
+    if (args_.size() != num_keys + 2 && args_.size() != num_keys + 4) {
+      return Status(Status::NotOK, "invalid number of arguments");
+    }
+    int64_t limit = 0;
+    if (args_.size() == num_keys + 4) {
+      if (Util::ToLower(args_[num_keys + 2]) != "limit") {
+        return Status(Status::NotOK, "invalid number of arguments");
+      }
+      auto parse_limit = ParseInt<int64_t>(args_[num_keys + 3], 10);
+      if (!parse_limit) {
+        return Status(Status::RedisParseErr, errValueNotInteger);
+      }
+      limit = *parse_limit;
+      if (limit < 0) {
+        return Status(Status::NotOK, "limit can't be negative");
+      }
+    }
+    std::vector<Slice> keys;
+    for (uint64_t i = 2; i < num_keys + 2; i++) {
+      keys.emplace_back(args_[i]);
+    }
+    Redis::Set set_db(svr->storage_, conn->GetNamespace());
+    int64_t ret = 0;
+    auto s = set_db.InterCard(keys, limit, &ret);
+    if (!s.ok()) {
+      return Status(Status::RedisExecErr, s.ToString());
+    }
+    *output = Redis::Integer(ret);
+    return Status::OK();
+  }
+};

Review Comment:
   @tanruixiang Thanks for advice.



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