Get rid of SendRespBlob in RedisReplyBuilder

This commit is contained in:
Roman Gershman 2022-02-03 08:45:41 +02:00
parent 501dc4208d
commit ce721ced90
5 changed files with 27 additions and 28 deletions

View File

@ -38,29 +38,29 @@ CommandRegistry::CommandRegistry() {
} }
void CommandRegistry::Command(CmdArgList args, ConnectionContext* cntx) { void CommandRegistry::Command(CmdArgList args, ConnectionContext* cntx) {
size_t sz = cmd_map_.size(); size_t len = cmd_map_.size();
string resp = StrCat("*", sz, "\r\n");
(*cntx)->StartArray(len);
for (const auto& val : cmd_map_) { for (const auto& val : cmd_map_) {
const CommandId& cd = val.second; const CommandId& cd = val.second;
StrAppend(&resp, "*6\r\n$", strlen(cd.name()), "\r\n", cd.name(), "\r\n"); (*cntx)->StartArray(6);
StrAppend(&resp, ":", int(cd.arity()), "\r\n"); (*cntx)->SendSimpleString(cd.name());
StrAppend(&resp, "*", CommandId::OptCount(cd.opt_mask()), "\r\n"); (*cntx)->SendLong(cd.arity());
(*cntx)->StartArray(CommandId::OptCount(cd.opt_mask()));
for (uint32_t i = 0; i < 32; ++i) { for (uint32_t i = 0; i < 32; ++i) {
unsigned obit = (1u << i); unsigned obit = (1u << i);
if (cd.opt_mask() & obit) { if (cd.opt_mask() & obit) {
const char* name = CO::OptName(CO::CommandOpt{obit}); const char* name = CO::OptName(CO::CommandOpt{obit});
StrAppend(&resp, "+", name, "\r\n"); (*cntx)->SendSimpleString(name);
} }
} }
StrAppend(&resp, ":", cd.first_key_pos(), "\r\n"); (*cntx)->SendLong(cd.first_key_pos());
StrAppend(&resp, ":", cd.last_key_pos(), "\r\n"); (*cntx)->SendLong(cd.last_key_pos());
StrAppend(&resp, ":", cd.key_arg_step(), "\r\n"); (*cntx)->SendLong(cd.key_arg_step());
} }
(*cntx)->SendRespBlob(resp);
} }
CommandRegistry& CommandRegistry::operator<<(CommandId cmd) { CommandRegistry& CommandRegistry::operator<<(CommandId cmd) {

View File

@ -406,14 +406,13 @@ void GenericFamily::Scan(CmdArgList args, ConnectionContext* cntx) {
DCHECK_EQ(0u, cursor); DCHECK_EQ(0u, cursor);
} }
(*cntx)->StartArray(2);
string res("*2\r\n$"); string res("*2\r\n$");
string curs_str = absl::StrCat(cursor); (*cntx)->SendSimpleString(absl::StrCat(cursor));
absl::StrAppend(&res, curs_str.size(), "\r\n", curs_str, "\r\n*", keys.size(), "\r\n"); (*cntx)->StartArray(keys.size());
for (const auto& k : keys) { for (const auto& k : keys) {
absl::StrAppend(&res, "$", k.size(), "\r\n", k, "\r\n"); (*cntx)->SendBulkString(k);
} }
return (*cntx)->SendRespBlob(res);
} }

View File

@ -380,19 +380,20 @@ void Service::Eval(CmdArgList args, ConnectionContext* cntx) {
} }
void Service::Exec(CmdArgList args, ConnectionContext* cntx) { void Service::Exec(CmdArgList args, ConnectionContext* cntx) {
RedisReplyBuilder* rb = (*cntx).operator->();
if (cntx->conn_state.exec_state == ConnectionState::EXEC_INACTIVE) { if (cntx->conn_state.exec_state == ConnectionState::EXEC_INACTIVE) {
return (*cntx)->SendError("EXEC without MULTI"); return rb->SendError("EXEC without MULTI");
} }
if (cntx->conn_state.exec_state == ConnectionState::EXEC_ERROR) { if (cntx->conn_state.exec_state == ConnectionState::EXEC_ERROR) {
cntx->conn_state.exec_state = ConnectionState::EXEC_INACTIVE; cntx->conn_state.exec_state = ConnectionState::EXEC_INACTIVE;
cntx->conn_state.exec_body.clear(); cntx->conn_state.exec_body.clear();
return (*cntx)->SendError("-EXECABORT Transaction discarded because of previous errors"); return rb->SendError("-EXECABORT Transaction discarded because of previous errors");
} }
(*cntx)->SendRespBlob(absl::StrCat("*", cntx->conn_state.exec_body.size(), "\r\n")); rb->StartArray(cntx->conn_state.exec_body.size());
if (!cntx->conn_state.exec_body.empty()) {
if (!(*cntx)->GetError() && !cntx->conn_state.exec_body.empty()) {
CmdArgVec str_list; CmdArgVec str_list;
for (auto& scmd : cntx->conn_state.exec_body) { for (auto& scmd : cntx->conn_state.exec_body) {
@ -406,7 +407,7 @@ void Service::Exec(CmdArgList args, ConnectionContext* cntx) {
CmdArgList cmd_arg_list{str_list.data(), str_list.size()}; CmdArgList cmd_arg_list{str_list.data(), str_list.size()};
cntx->transaction->InitByArgs(cntx->conn_state.db_index, cmd_arg_list); cntx->transaction->InitByArgs(cntx->conn_state.db_index, cmd_arg_list);
scmd.descr->Invoke(cmd_arg_list, cntx); scmd.descr->Invoke(cmd_arg_list, cntx);
if ((*cntx)->GetError()) if (rb->GetError())
break; break;
} }

View File

@ -78,14 +78,12 @@ void MCReplyBuilder::SendStored() {
SendDirect("STORED\r\n"); SendDirect("STORED\r\n");
} }
void MCReplyBuilder::SendGetReply(std::string_view key, uint32_t flags, std::string_view value) { void MCReplyBuilder::SendGetReply(std::string_view key, uint32_t flags, std::string_view value) {
string first = absl::StrCat("VALUE ", key, " ", flags, " ", value.size(), "\r\n"); string first = absl::StrCat("VALUE ", key, " ", flags, " ", value.size(), "\r\n");
iovec v[] = {IoVec(first), IoVec(value), IoVec(kCRLF)}; iovec v[] = {IoVec(first), IoVec(value), IoVec(kCRLF)};
Send(v, ABSL_ARRAYSIZE(v)); Send(v, ABSL_ARRAYSIZE(v));
} }
void MCReplyBuilder::SendError(string_view str) { void MCReplyBuilder::SendError(string_view str) {
SendDirect("ERROR\r\n"); SendDirect("ERROR\r\n");
} }
@ -211,6 +209,10 @@ void RedisReplyBuilder::SendStringArr(absl::Span<const std::string_view> arr) {
SendDirect(res); SendDirect(res);
} }
void RedisReplyBuilder::StartArray(unsigned len) {
SendDirect(absl::StrCat("*", len, kCRLF));
}
void ReqSerializer::SendCommand(std::string_view str) { void ReqSerializer::SendCommand(std::string_view str) {
VLOG(1) << "SendCommand: " << str; VLOG(1) << "SendCommand: " << str;

View File

@ -120,10 +120,7 @@ class RedisReplyBuilder : public SinkReplyBuilder {
virtual void SendBulkString(std::string_view str); virtual void SendBulkString(std::string_view str);
// TODO: to get rid of it. We should only send high-level data. virtual void StartArray(unsigned len);
void SendRespBlob(std::string_view str) {
SendDirect(str);
}
private: private:
}; };