From 8baceb07b3db51dafccc3d44251fb30f4e54741e Mon Sep 17 00:00:00 2001 From: leviathan1995 Date: Wed, 27 Dec 2017 14:47:21 +0800 Subject: [PATCH] optimize code --- pink/include/pink_conn.h | 14 +------------- pink/include/redis_conn.h | 7 +------ pink/src/pink_pubsub.cc | 27 +++++++++++++++++++++++++-- pink/src/redis_conn.cc | 38 ++------------------------------------ 4 files changed, 29 insertions(+), 57 deletions(-) diff --git a/pink/include/pink_conn.h b/pink/include/pink_conn.h index 7685f1b5..7299633b 100644 --- a/pink/include/pink_conn.h +++ b/pink/include/pink_conn.h @@ -37,19 +37,7 @@ class PinkConn { virtual ReadStatus GetRequest() = 0; virtual WriteStatus SendReply() = 0; - - virtual int ConstructPublishResp( - const std::string& subscribe_channel, - const std::string& channel, - const std::string& msg, - const bool pattern) { - return 0; - } - virtual std::string ConstructPubSubResp( - const std::string& cmd, - const std::vector>& result) { - return nullptr; - } + virtual void WriteResp(const std::string& resp) { } virtual void TryResizeBuffer() {} diff --git a/pink/include/redis_conn.h b/pink/include/redis_conn.h index 4ee13fc4..65844b58 100644 --- a/pink/include/redis_conn.h +++ b/pink/include/redis_conn.h @@ -25,12 +25,7 @@ class RedisConn: public PinkConn { virtual ReadStatus GetRequest(); virtual WriteStatus SendReply(); - virtual int ConstructPublishResp(const std::string& subscribe_channel, - const std::string& channel, - const std::string& msg, - const bool pattern); - virtual std::string ConstructPubSubResp(const std::string& cmd, - const std::vector>& result); + virtual void WriteResp(const std::string& resp); void TryResizeBuffer() override; diff --git a/pink/src/pink_pubsub.cc b/pink/src/pink_pubsub.cc index 385228d2..a2b079c7 100644 --- a/pink/src/pink_pubsub.cc +++ b/pink/src/pink_pubsub.cc @@ -5,6 +5,7 @@ #include #include +#include #include "pink/src/worker_thread.h" @@ -15,6 +16,26 @@ namespace pink { +static std::string ConstructPublishResp(const std::string& subscribe_channel, + const std::string& publish_channel, + const std::string& msg, + const bool pattern) { + std::stringstream resp; + std::string common_msg = "message"; + std::string pattern_msg = "pmessage"; + if (pattern) { + resp << "*4\r\n" << "$" << pattern_msg.length() << "\r\n" << pattern_msg << "\r\n" << + "$" << subscribe_channel.length() << "\r\n" << subscribe_channel << "\r\n" << + "$" << publish_channel.length() << "\r\n" << publish_channel << "\r\n" << + "$" << msg.length() << "\r\n" << msg << "\r\n"; + } else { + resp << "*3\r\n" << "$" << common_msg.length() << "\r\n" << common_msg << "\r\n" << + "$" << publish_channel.length() << "\r\n" << publish_channel << "\r\n" << + "$" << msg.length() << "\r\n" << msg << "\r\n"; + } + return resp.str(); +} + void CloseFd(PinkConn* conn) { close(conn->fd()); } @@ -355,7 +376,8 @@ void *PubSubThread::ThreadMain() { for (auto it = pubsub_channel_.begin(); it != pubsub_channel_.end(); it++) { if (channel == it->first) { for (size_t i = 0; i < it->second.size(); i++) { - it->second[i]->ConstructPublishResp(it->first, channel, msg, false); + std::string resp = ConstructPublishResp(it->first, channel, msg, false); + it->second[i]->WriteResp(resp); WriteStatus write_status = it->second[i]->SendReply(); if (write_status == kWriteHalf) { pink_epoll_->PinkModEvent(it->second[i]->fd(), @@ -380,7 +402,8 @@ void *PubSubThread::ThreadMain() { if (slash::stringmatchlen(it->first.c_str(), it->first.size(), channel.c_str(), channel.size(), 0)) { for (size_t i = 0; i < it->second.size(); i++) { - it->second[i]->ConstructPublishResp(it->first, channel, msg, true); + std::string resp = ConstructPublishResp(it->first, channel, msg, true); + it->second[i]->WriteResp(resp); WriteStatus write_status = it->second[i]->SendReply(); if (write_status == kWriteHalf) { pink_epoll_->PinkModEvent(it->second[i]->fd(), diff --git a/pink/src/redis_conn.cc b/pink/src/redis_conn.cc index e6d12a67..094447ef 100644 --- a/pink/src/redis_conn.cc +++ b/pink/src/redis_conn.cc @@ -351,43 +351,9 @@ WriteStatus RedisConn::SendReply() { } } -int RedisConn::ConstructPublishResp(const std::string& subscribe_channel, - const std::string& publish_channel, - const std::string& msg, - const bool pattern) { - std::stringstream resp; - std::string common_msg = "message"; - std::string pattern_msg = "pmessage"; - if (pattern) { - resp << "*4\r\n" << "$" << pattern_msg.length() << "\r\n" << pattern_msg << "\r\n" << - "$" << subscribe_channel.length() << "\r\n" << subscribe_channel << "\r\n" << - "$" << publish_channel.length() << "\r\n" << publish_channel << "\r\n" << - "$" << msg.length() << "\r\n" << msg << "\r\n"; - } else { - resp << "*3\r\n" << "$" << common_msg.length() << "\r\n" << common_msg << "\r\n" << - "$" << publish_channel.length() << "\r\n" << publish_channel << "\r\n" << - "$" << msg.length() << "\r\n" << msg << "\r\n"; - } - - response_.append(resp.str()); +void RedisConn::WriteResp(const std::string& resp) { + response_.append(resp); set_is_reply(true); - return 0; -} - -std::string RedisConn::ConstructPubSubResp( - const std::string& cmd, - const std::vector>& result) { - std::stringstream resp; - if (result.size() == 0) { - resp << "*3\r\n" << "$" << cmd.length() << "\r\n" << cmd << "\r\n" << - "$" << -1 << "\r\n" << ":" << 0 << "\r\n"; - } - for (auto it = result.begin(); it != result.end(); it++) { - resp << "*3\r\n" << "$" << cmd.length() << "\r\n" << cmd << "\r\n" << - "$" << it->first.length() << "\r\n" << it->first << "\r\n" << - ":" << it->second << "\r\n"; - } - return resp.str(); } void RedisConn::TryResizeBuffer() {