You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brpc.apache.org by ja...@apache.org on 2020/01/16 07:11:29 UTC
[incubator-brpc] branch master updated: Rename
RedisCommandHandler::Result to make related code cleaner
This is an automated email from the ASF dual-hosted git repository.
jamesge pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-brpc.git
The following commit(s) were added to refs/heads/master by this push:
new 91a4376 Rename RedisCommandHandler::Result to make related code cleaner
91a4376 is described below
commit 91a437650974575752f9770c3f66b3a80f6e0820
Author: jamesge <jg...@gmail.com>
AuthorDate: Thu Jan 16 15:11:12 2020 +0800
Rename RedisCommandHandler::Result to make related code cleaner
---
example/redis_c++/redis_server.cpp | 12 ++++++------
src/brpc/policy/redis_protocol.cpp | 16 ++++++++--------
src/brpc/redis.h | 29 +++++++++++++++--------------
test/brpc_redis_unittest.cpp | 38 +++++++++++++++++++-------------------
4 files changed, 48 insertions(+), 47 deletions(-)
diff --git a/example/redis_c++/redis_server.cpp b/example/redis_c++/redis_server.cpp
index abe4d21..6dddcac 100644
--- a/example/redis_c++/redis_server.cpp
+++ b/example/redis_c++/redis_server.cpp
@@ -64,12 +64,12 @@ public:
explicit GetCommandHandler(RedisServiceImpl* rsimpl)
: _rsimpl(rsimpl) {}
- brpc::RedisCommandHandler::Result Run(const std::vector<const char*>& args,
+ brpc::RedisCommandHandlerResult Run(const std::vector<const char*>& args,
brpc::RedisReply* output,
bool /*flush_batched*/) override {
if (args.size() != 2ul) {
output->FormatError("Expect 1 arg for 'get', actually %lu", args.size()-1);
- return brpc::RedisCommandHandler::OK;
+ return brpc::REDIS_CMD_HANDLED;
}
const std::string key(args[1]);
std::string value;
@@ -78,7 +78,7 @@ public:
} else {
output->SetNullString();
}
- return brpc::RedisCommandHandler::OK;
+ return brpc::REDIS_CMD_HANDLED;
}
private:
@@ -90,18 +90,18 @@ public:
explicit SetCommandHandler(RedisServiceImpl* rsimpl)
: _rsimpl(rsimpl) {}
- brpc::RedisCommandHandler::Result Run(const std::vector<const char*>& args,
+ brpc::RedisCommandHandlerResult Run(const std::vector<const char*>& args,
brpc::RedisReply* output,
bool /*flush_batched*/) override {
if (args.size() != 3ul) {
output->FormatError("Expect 2 args for 'set', actually %lu", args.size()-1);
- return brpc::RedisCommandHandler::OK;
+ return brpc::REDIS_CMD_HANDLED;
}
const std::string key(args[1]);
const std::string value(args[2]);
_rsimpl->Set(key, value);
output->SetStatus("OK");
- return brpc::RedisCommandHandler::OK;
+ return brpc::REDIS_CMD_HANDLED;
}
private:
diff --git a/src/brpc/policy/redis_protocol.cpp b/src/brpc/policy/redis_protocol.cpp
index 64bd28c..1e92bf5 100644
--- a/src/brpc/policy/redis_protocol.cpp
+++ b/src/brpc/policy/redis_protocol.cpp
@@ -80,12 +80,12 @@ int ConsumeCommand(RedisConnContext* ctx,
bool flush_batched,
butil::IOBufAppender* appender) {
RedisReply output(&ctx->arena);
- RedisCommandHandler::Result result = RedisCommandHandler::OK;
+ RedisCommandHandlerResult result = REDIS_CMD_HANDLED;
if (ctx->transaction_handler) {
result = ctx->transaction_handler->Run(commands, &output, flush_batched);
- if (result == RedisCommandHandler::OK) {
+ if (result == REDIS_CMD_HANDLED) {
ctx->transaction_handler.reset(NULL);
- } else if (result == RedisCommandHandler::BATCHED) {
+ } else if (result == REDIS_CMD_BATCHED) {
LOG(ERROR) << "BATCHED should not be returned by a transaction handler.";
return -1;
}
@@ -97,18 +97,18 @@ int ConsumeCommand(RedisConnContext* ctx,
output.SetError(buf);
} else {
result = ch->Run(commands, &output, flush_batched);
- if (result == RedisCommandHandler::CONTINUE) {
+ if (result == REDIS_CMD_CONTINUE) {
if (ctx->batched_size != 0) {
LOG(ERROR) << "CONTINUE should not be returned in a batched process.";
return -1;
}
ctx->transaction_handler.reset(ch->NewTransactionHandler());
- } else if (result == RedisCommandHandler::BATCHED) {
+ } else if (result == REDIS_CMD_BATCHED) {
ctx->batched_size++;
}
}
}
- if (result == RedisCommandHandler::OK) {
+ if (result == REDIS_CMD_HANDLED) {
if (ctx->batched_size) {
if ((int)output.size() != (ctx->batched_size + 1)) {
LOG(ERROR) << "reply array size can't be matched with batched size, "
@@ -122,9 +122,9 @@ int ConsumeCommand(RedisConnContext* ctx,
} else {
output.SerializeTo(appender);
}
- } else if (result == RedisCommandHandler::CONTINUE) {
+ } else if (result == REDIS_CMD_CONTINUE) {
output.SerializeTo(appender);
- } else if (result == RedisCommandHandler::BATCHED) {
+ } else if (result == REDIS_CMD_BATCHED) {
// just do nothing and wait handler to return OK.
} else {
LOG(ERROR) << "unknown status=" << result;
diff --git a/src/brpc/redis.h b/src/brpc/redis.h
index 9454c42..d3f09e8 100644
--- a/src/brpc/redis.h
+++ b/src/brpc/redis.h
@@ -231,14 +231,15 @@ private:
CommandMap _command_map;
};
+enum RedisCommandHandlerResult {
+ REDIS_CMD_HANDLED = 0,
+ REDIS_CMD_CONTINUE = 1,
+ REDIS_CMD_BATCHED = 2,
+};
+
// The Command handler for a redis request. User should impletement Run().
class RedisCommandHandler {
public:
- enum Result {
- OK = 0,
- CONTINUE = 1,
- BATCHED = 2,
- };
~RedisCommandHandler() {}
// Once Server receives commands, it will first find the corresponding handlers and
@@ -250,24 +251,24 @@ public:
// Read brpc/src/redis_reply.h for more usage.
// `flush_batched' indicates whether the user should flush all the results of
// batched commands. If user want to do some batch processing, user should buffer
- // the commands and return RedisCommandHandler::BATCHED. Once `flush_batched' is true,
+ // the commands and return REDIS_CMD_BATCHED. Once `flush_batched' is true,
// run all the commands, set `output' to be an array in which every element is the
- // result of batched commands and return RedisCommandHandler::OK.
+ // result of batched commands and return REDIS_CMD_HANDLED.
//
- // The return value should be RedisCommandHandler::OK for normal cases. If you want
- // to implement transaction, return RedisCommandHandler::CONTINUE once server receives
+ // The return value should be REDIS_CMD_HANDLED for normal cases. If you want
+ // to implement transaction, return REDIS_CMD_CONTINUE once server receives
// an start marker and brpc will call MultiTransactionHandler() to new a transaction
// handler that all the following commands are sent to this tranction handler until
- // it returns RedisCommandHandler::OK. Read the comment below.
- virtual RedisCommandHandler::Result Run(const std::vector<const char*>& args,
- brpc::RedisReply* output,
- bool flush_batched) = 0;
+ // it returns REDIS_CMD_HANDLED. Read the comment below.
+ virtual RedisCommandHandlerResult Run(const std::vector<const char*>& args,
+ brpc::RedisReply* output,
+ bool flush_batched) = 0;
// The Run() returns CONTINUE for "multi", which makes brpc call this method to
// create a transaction_handler to process following commands until transaction_handler
// returns OK. For example, for command "multi; set k1 v1; set k2 v2; set k3 v3;
// exec":
- // 1) First command is "multi" and Run() should return RedisCommandHandler::CONTINUE,
+ // 1) First command is "multi" and Run() should return REDIS_CMD_CONTINUE,
// then brpc calls NewTransactionHandler() to new a transaction_handler.
// 2) brpc calls transaction_handler.Run() with command "set k1 v1",
// which should return CONTINUE.
diff --git a/test/brpc_redis_unittest.cpp b/test/brpc_redis_unittest.cpp
index c9d4b0d..49b01e4 100644
--- a/test/brpc_redis_unittest.cpp
+++ b/test/brpc_redis_unittest.cpp
@@ -812,7 +812,7 @@ public:
RedisServiceImpl()
: _batch_count(0) {}
- brpc::RedisCommandHandler::Result OnBatched(const std::vector<const char*> args,
+ brpc::RedisCommandHandlerResult OnBatched(const std::vector<const char*> args,
brpc::RedisReply* output, bool flush_batched) {
if (_batched_command.empty() && flush_batched) {
if (strcmp(args[0], "set") == 0) {
@@ -820,7 +820,7 @@ public:
} else if (strcmp(args[0], "get") == 0) {
DoGet(args[1], output);
}
- return brpc::RedisCommandHandler::OK;
+ return brpc::REDIS_CMD_HANDLED;
}
std::vector<std::string> comm;
for (int i = 0; i < (int)args.size(); ++i) {
@@ -838,9 +838,9 @@ public:
}
_batch_count++;
_batched_command.clear();
- return brpc::RedisCommandHandler::OK;
+ return brpc::REDIS_CMD_HANDLED;
} else {
- return brpc::RedisCommandHandler::BATCHED;
+ return brpc::REDIS_CMD_BATCHED;
}
}
@@ -869,18 +869,18 @@ public:
: _rs(rs)
, _batch_process(batch_process) {}
- brpc::RedisCommandHandler::Result Run(const std::vector<const char*>& args,
+ brpc::RedisCommandHandlerResult Run(const std::vector<const char*>& args,
brpc::RedisReply* output,
bool flush_batched) {
if (args.size() < 3) {
output->SetError("ERR wrong number of arguments for 'set' command");
- return brpc::RedisCommandHandler::OK;
+ return brpc::REDIS_CMD_HANDLED;
}
if (_batch_process) {
return _rs->OnBatched(args, output, flush_batched);
} else {
DoSet(args[1], args[2], output);
- return brpc::RedisCommandHandler::OK;
+ return brpc::REDIS_CMD_HANDLED;
}
}
@@ -900,18 +900,18 @@ public:
: _rs(rs)
, _batch_process(batch_process) {}
- brpc::RedisCommandHandler::Result Run(const std::vector<const char*>& args,
+ brpc::RedisCommandHandlerResult Run(const std::vector<const char*>& args,
brpc::RedisReply* output,
bool flush_batched) {
if (args.size() < 2) {
output->SetError("ERR wrong number of arguments for 'get' command");
- return brpc::RedisCommandHandler::OK;
+ return brpc::REDIS_CMD_HANDLED;
}
if (_batch_process) {
return _rs->OnBatched(args, output, flush_batched);
} else {
DoGet(args[1], output);
- return brpc::RedisCommandHandler::OK;
+ return brpc::REDIS_CMD_HANDLED;
}
}
@@ -933,12 +933,12 @@ class IncrCommandHandler : public brpc::RedisCommandHandler {
public:
IncrCommandHandler() {}
- brpc::RedisCommandHandler::Result Run(const std::vector<const char*>& args,
+ brpc::RedisCommandHandlerResult Run(const std::vector<const char*>& args,
brpc::RedisReply* output,
bool flush_batched) {
if (args.size() < 2) {
output->SetError("ERR wrong number of arguments for 'incr' command");
- return brpc::RedisCommandHandler::OK;
+ return brpc::REDIS_CMD_HANDLED;
}
const std::string& key = args[1];
int64_t value;
@@ -946,7 +946,7 @@ public:
value = ++int_map[key];
s_mutex.unlock();
output->SetInteger(value);
- return brpc::RedisCommandHandler::OK;
+ return brpc::REDIS_CMD_HANDLED;
}
};
@@ -1047,11 +1047,11 @@ class MultiCommandHandler : public brpc::RedisCommandHandler {
public:
MultiCommandHandler() {}
- brpc::RedisCommandHandler::Result Run(const std::vector<const char*>& args,
+ brpc::RedisCommandHandlerResult Run(const std::vector<const char*>& args,
brpc::RedisReply* output,
bool flush_batched) {
output->SetStatus("OK");
- return brpc::RedisCommandHandler::CONTINUE;
+ return brpc::REDIS_CMD_CONTINUE;
}
RedisCommandHandler* NewTransactionHandler() override {
@@ -1060,12 +1060,12 @@ public:
class MultiTransactionHandler : public brpc::RedisCommandHandler {
public:
- brpc::RedisCommandHandler::Result Run(const std::vector<const char*>& args,
+ brpc::RedisCommandHandlerResult Run(const std::vector<const char*>& args,
brpc::RedisReply* output,
bool flush_batched) {
if (strcmp(args[0], "multi") == 0) {
output->SetError("ERR duplicate multi");
- return brpc::RedisCommandHandler::CONTINUE;
+ return brpc::REDIS_CMD_CONTINUE;
}
if (strcmp(args[0], "exec") != 0) {
std::vector<std::string> comm;
@@ -1074,7 +1074,7 @@ public:
}
_commands.push_back(comm);
output->SetStatus("QUEUED");
- return brpc::RedisCommandHandler::CONTINUE;
+ return brpc::REDIS_CMD_CONTINUE;
}
output->SetArray(_commands.size());
s_mutex.lock();
@@ -1088,7 +1088,7 @@ public:
}
}
s_mutex.unlock();
- return brpc::RedisCommandHandler::OK;
+ return brpc::REDIS_CMD_HANDLED;
}
private:
std::vector<std::vector<std::string> > _commands;
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org