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/06/12 13:41:20 UTC

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

Yangsx-1 commented on code in PR #1490:
URL: https://github.com/apache/incubator-kvrocks/pull/1490#discussion_r1226686389


##########
src/commands/cmd_zset.cc:
##########
@@ -301,40 +450,149 @@ class CommandZMPop : public Commander {
   }
 
   Status Execute(Server *svr, Connection *conn, std::string *output) override {
-    redis::ZSet zset_db(svr->storage, conn->GetNamespace());
+    svr_ = svr;
+    conn_ = conn;
+
+    if (!block_) {
+      auto s = TryPopFromZset();
+      return Status::OK();
+    }
+
+    auto bev = conn->GetBufferEvent();
+
+    auto s = TryPopFromZset();
+
+    if (!s.ok() || reply_flag_) {
+      return Status::OK();  // error has already output or result has already output
+    }
+
+    // All Empty
+    if (conn->IsInExec()) {
+      *output = redis::MultiLen(-1);
+      return Status::OK();  // No blocking in multi-exec
+    }
+
+    for (const auto &key : keys_) {
+      svr_->BlockOnKey(key, conn_);
+    }
+
+    SetCB(bev);
+
+    if (timeout_) {
+      timer_.reset(NewTimer(bufferevent_get_base(bev)));
+      timeval tm = {timeout_, 0};
+      evtimer_add(timer_.get(), &tm);
+    }
+
+    return {Status::BlockingCmd};
+  }
+
+  rocksdb::Status TryPopFromZset() {
+    redis::ZSet zset_db(svr_->storage, conn_->GetNamespace());
+    rocksdb::Status s;
+    std::string output;
     for (auto &user_key : keys_) {
       std::vector<MemberScore> member_scores;
-      auto s = zset_db.Pop(user_key, count_, flag_ == ZSET_MIN, &member_scores);
+      s = zset_db.Pop(user_key, count_, flag_ == ZSET_MIN, &member_scores);
       if (!s.ok()) {
-        return {Status::RedisExecErr, s.ToString()};
+        conn_->Reply(redis::Error("ERR " + s.ToString()));
+        break;
       }
       if (member_scores.empty()) {
         continue;
       }
-
-      output->append(redis::MultiLen(2));
-      output->append(redis::BulkString(user_key));
-      output->append(redis::MultiLen(member_scores.size() * 2));
+      output.append(redis::MultiLen(2));
+      output.append(redis::BulkString(user_key));
+      output.append(redis::MultiLen(member_scores.size() * 2));
       for (const auto &ms : member_scores) {
-        output->append(redis::BulkString(ms.member));
-        output->append(redis::BulkString(util::Float2String(ms.score)));
+        output.append(redis::BulkString(ms.member));
+        output.append(redis::BulkString(util::Float2String(ms.score)));
       }
-      return Status::OK();
+      reply_flag_ = true;
+      break;
     }
-    *output = redis::NilString();
-    return Status::OK();
+    if (output.empty() && !block_) {
+      output = redis::NilString();
+    }
+    conn_->Reply(output);
+    return s;
+  }
+
+  void OnWrite(bufferevent *bev) {
+    auto s = TryPopFromZset();
+    if (!s.ok() || !reply_flag_) {

Review Comment:
   Yes, you are right. There shouldn't be a `!s.ok()`. I forgot to delete it.



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