You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@kvrocks.apache.org by "torwig (via GitHub)" <gi...@apache.org> on 2023/06/19 10:23:49 UTC

[GitHub] [incubator-kvrocks] torwig commented on a diff in pull request #1502: Feat: Implement zunion

torwig commented on code in PR #1502:
URL: https://github.com/apache/incubator-kvrocks/pull/1502#discussion_r1233856234


##########
src/commands/cmd_zset.cc:
##########
@@ -819,6 +819,81 @@ class CommandZMScore : public Commander {
   }
 };
 
+class CommandZUnion : public Commander {
+ public:
+  Status Parse(const std::vector<std::string> &args) override {
+    auto parse_result = ParseInt<int>(args[1], 10);
+    if (!parse_result) {
+      return {Status::RedisParseErr, errValueNotInteger};
+    }
+
+    numkeys_ = *parse_result;
+    if (numkeys_ > args.size() - 2) {
+      return {Status::RedisParseErr, errInvalidSyntax};
+    }
+
+    size_t key_iterator = 0;
+    while (key_iterator < numkeys_) {
+      keys_weights_.emplace_back(KeyWeight{args[key_iterator + 2], 1});
+      key_iterator++;
+    }
+
+    size_t option_iterator = 2 + numkeys_;
+    while (option_iterator < args.size()) {
+      if (util::ToLower(args[option_iterator]) == "aggregate" && option_iterator + 1 < args.size()) {
+        if (util::ToLower(args[option_iterator + 1]) == "sum") {
+          aggregate_method_ = kAggregateSum;
+        } else if (util::ToLower(args[option_iterator + 1]) == "min") {
+          aggregate_method_ = kAggregateMin;
+        } else if (util::ToLower(args[option_iterator + 1]) == "max") {
+          aggregate_method_ = kAggregateMax;
+        } else {
+          return {Status::RedisParseErr, "aggregate param error"};
+        }
+        option_iterator += 2;
+      } else if (util::ToLower(args[option_iterator]) == "weights" && option_iterator + numkeys_ < args.size()) {
+        size_t k = 0;
+        while (k < numkeys_) {
+          auto weight = ParseFloat(args[option_iterator + k + 1]);
+          if (!weight || std::isnan(*weight)) {
+            return {Status::RedisParseErr, "weight is not a double or out of range"};
+          }
+          keys_weights_[k].weight = *weight;
+          k++;
+        }
+        option_iterator += numkeys_ + 1;
+      } else if (util::ToLower(args[option_iterator]) == "withscores") {
+        with_scores_ = true;
+      } else {
+        return {Status::RedisParseErr, errInvalidSyntax};
+      }
+    }
+    return Commander::Parse(args);
+  }
+
+  Status Execute(Server *svr, Connection *conn, std::string *output) override {
+    redis::ZSet zset_db(svr->storage, conn->GetNamespace());
+    std::vector<MemberScore> member_scores;
+    uint64_t size = 0;
+    auto s = zset_db.Union(keys_weights_, aggregate_method_, &size, &member_scores);

Review Comment:
   Since you don't use the `size` variable you could remove it and pass a `nullptr` to the `Union` function.



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