Skip to content

Commit

Permalink
feat(resp): optimize simple string "OK" construction (#2627)
Browse files Browse the repository at this point in the history
  • Loading branch information
RiversJin authored Oct 31, 2024
1 parent 4aa36ec commit 3a51869
Show file tree
Hide file tree
Showing 19 changed files with 75 additions and 65 deletions.
2 changes: 1 addition & 1 deletion src/cluster/replication.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Status FeedSlaveThread::Start() {
sigaddset(&mask, SIGHUP);
sigaddset(&mask, SIGPIPE);
pthread_sigmask(SIG_BLOCK, &mask, &omask);
auto s = util::SockSend(conn_->GetFD(), redis::SimpleString("OK"), conn_->GetBufferEvent());
auto s = util::SockSend(conn_->GetFD(), redis::RESP_OK, conn_->GetBufferEvent());
if (!s.IsOK()) {
LOG(ERROR) << "failed to send OK response to the replica: " << s.Msg();
return;
Expand Down
2 changes: 1 addition & 1 deletion src/cluster/sync_migrate_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void SyncMigrateContext::TimerCB(int, [[maybe_unused]] int16_t events) {

void SyncMigrateContext::OnWrite(bufferevent *bev) {
if (migrate_result_) {
conn_->Reply(redis::SimpleString("OK"));
conn_->Reply(redis::RESP_OK);
} else {
conn_->Reply(redis::Error(migrate_result_));
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/cmd_bloom_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class CommandBFReserve : public Commander {
auto s = bloomfilter_db.Reserve(ctx, args_[1], capacity_, error_rate_, expansion_);
if (!s.ok()) return {Status::RedisExecErr, s.ToString()};

*output = redis::SimpleString("OK");
*output = redis::RESP_OK;
return Status::OK();
}

Expand Down
18 changes: 9 additions & 9 deletions src/commands/cmd_cluster.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ class CommandCluster : public Commander {
// TODO: support multiple slot ranges
Status s = srv->cluster->ImportSlotRange(conn, slot_ranges_[0], state_);
if (s.IsOK()) {
*output = redis::SimpleString("OK");
*output = redis::RESP_OK;
} else {
return s;
}
} else if (subcommand_ == "reset") {
Status s = srv->cluster->Reset();
if (s.IsOK()) {
*output = redis::SimpleString("OK");
*output = redis::RESP_OK;
} else {
return s;
}
Expand Down Expand Up @@ -258,23 +258,23 @@ class CommandClusterX : public Commander {
Status s = srv->cluster->SetClusterNodes(nodes_str_, set_version_, force_);
if (s.IsOK()) {
need_persist_nodes_info = true;
*output = redis::SimpleString("OK");
*output = redis::RESP_OK;
} else {
return s;
}
} else if (subcommand_ == "setnodeid") {
Status s = srv->cluster->SetNodeId(args_[2]);
if (s.IsOK()) {
need_persist_nodes_info = true;
*output = redis::SimpleString("OK");
*output = redis::RESP_OK;
} else {
return s;
}
} else if (subcommand_ == "setslot") {
Status s = srv->cluster->SetSlotRanges(slot_ranges_, args_[4], set_version_);
if (s.IsOK()) {
need_persist_nodes_info = true;
*output = redis::SimpleString("OK");
*output = redis::RESP_OK;
} else {
return s;
}
Expand All @@ -293,7 +293,7 @@ class CommandClusterX : public Commander {
if (sync_migrate_) {
return {Status::BlockingCmd};
}
*output = redis::SimpleString("OK");
*output = redis::RESP_OK;
} else {
return s;
}
Expand Down Expand Up @@ -331,7 +331,7 @@ class CommandReadOnly : public Commander {
public:
Status Execute([[maybe_unused]] engine::Context &ctx, [[maybe_unused]] Server *srv, Connection *conn,
std::string *output) override {
*output = redis::SimpleString("OK");
*output = redis::RESP_OK;
conn->EnableFlag(redis::Connection::kReadOnly);
return Status::OK();
}
Expand All @@ -341,7 +341,7 @@ class CommandReadWrite : public Commander {
public:
Status Execute([[maybe_unused]] engine::Context &ctx, [[maybe_unused]] Server *srv, Connection *conn,
std::string *output) override {
*output = redis::SimpleString("OK");
*output = redis::RESP_OK;
conn->DisableFlag(redis::Connection::kReadOnly);
return Status::OK();
}
Expand All @@ -352,7 +352,7 @@ class CommandAsking : public Commander {
Status Execute([[maybe_unused]] engine::Context &ctx, [[maybe_unused]] Server *srv, Connection *conn,
std::string *output) override {
conn->EnableFlag(redis::Connection::kAsking);
*output = redis::SimpleString("OK");
*output = redis::RESP_OK;
return Status::OK();
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/commands/cmd_function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ struct CommandFunction : Commander {
auto s = lua::FunctionDelete(ctx, srv, libname);
if (!s) return s;

*output = SimpleString("OK");
*output = RESP_OK;
return Status::OK();
} else {
return {Status::NotOK, "no such subcommand"};
Expand Down
2 changes: 1 addition & 1 deletion src/commands/cmd_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ class CommandHMSet : public Commander {
if (GetAttributes()->name == "hset") {
*output = redis::Integer(ret);
} else {
*output = redis::SimpleString("OK");
*output = redis::RESP_OK;
}
return Status::OK();
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/cmd_hll.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class CommandPfMerge final : public Commander {
if (!s.ok() && !s.IsNotFound()) {
return {Status::RedisExecErr, s.ToString()};
}
*output = redis::SimpleString("OK");
*output = redis::RESP_OK;
return Status::OK();
}
};
Expand Down
6 changes: 3 additions & 3 deletions src/commands/cmd_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class CommandJsonSet : public Commander {
auto s = json.Set(ctx, args_[1], args_[2], args_[3]);
if (!s.ok()) return {Status::RedisExecErr, s.ToString()};

*output = redis::SimpleString("OK");
*output = redis::RESP_OK;
return Status::OK();
}
};
Expand Down Expand Up @@ -337,7 +337,7 @@ class CommandJsonMerge : public Commander {
if (!result) {
*output = conn->NilString();
} else {
*output = redis::SimpleString("OK");
*output = redis::RESP_OK;
}

return Status::OK();
Expand Down Expand Up @@ -648,7 +648,7 @@ class CommandJsonMSet : public Commander {

if (auto s = json.MSet(ctx, user_keys, paths, values); !s.ok()) return {Status::RedisExecErr, s.ToString()};

*output = redis::SimpleString("OK");
*output = redis::RESP_OK;
return Status::OK();
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/commands/cmd_key.cc
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ class CommandRename : public Commander {
auto s = redis.Copy(ctx, ns_key, new_ns_key, false, true, &res);
if (!s.ok()) return {Status::RedisExecErr, s.ToString()};
if (res == Database::CopyResult::KEY_NOT_EXIST) return {Status::RedisExecErr, "no such key"};
*output = redis::SimpleString("OK");
*output = redis::RESP_OK;
return Status::OK();
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/commands/cmd_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ class CommandLSet : public Commander {
return {Status::RedisExecErr, errNoSuchKey};
}

*output = redis::SimpleString("OK");
*output = redis::RESP_OK;
return Status::OK();
}

Expand Down Expand Up @@ -656,7 +656,7 @@ class CommandLTrim : public Commander {
return {Status::RedisExecErr, s.ToString()};
}

*output = redis::SimpleString("OK");
*output = redis::RESP_OK;
return Status::OK();
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/cmd_replication.cc
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class CommandReplConf : public Commander {
if (!ip_address_.empty()) {
conn->SetAnnounceIP(ip_address_);
}
*output = redis::SimpleString("OK");
*output = redis::RESP_OK;
return Status::OK();
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/cmd_script.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class CommandScript : public Commander {
LOG(ERROR) << "Failed to propagate script command: " << s.Msg();
return s;
}
*output = redis::SimpleString("OK");
*output = redis::RESP_OK;
} else if (args_.size() >= 3 && subcommand_ == "exists") {
*output = redis::MultiLen(args_.size() - 2);
for (size_t j = 2; j < args_.size(); j++) {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/cmd_search.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class CommandFTCreate : public Commander {

GET_OR_RET(srv->index_mgr.Create(ctx, std::move(index_info_)));

output->append(redis::SimpleString("OK"));
output->append(redis::RESP_OK);
return Status::OK();
};

Expand Down Expand Up @@ -488,7 +488,7 @@ class CommandFTDrop : public Commander {

GET_OR_RET(srv->index_mgr.Drop(ctx, index_name, conn->GetNamespace()));

output->append(SimpleString("OK"));
output->append(redis::RESP_OK);

return Status::OK();
};
Expand Down
Loading

0 comments on commit 3a51869

Please sign in to comment.