Commit 91a43765 authored by jamesge's avatar jamesge

Rename RedisCommandHandler::Result to make related code cleaner

parent d81ba3f1
...@@ -64,12 +64,12 @@ public: ...@@ -64,12 +64,12 @@ public:
explicit GetCommandHandler(RedisServiceImpl* rsimpl) explicit GetCommandHandler(RedisServiceImpl* rsimpl)
: _rsimpl(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, brpc::RedisReply* output,
bool /*flush_batched*/) override { bool /*flush_batched*/) override {
if (args.size() != 2ul) { if (args.size() != 2ul) {
output->FormatError("Expect 1 arg for 'get', actually %lu", args.size()-1); 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]); const std::string key(args[1]);
std::string value; std::string value;
...@@ -78,7 +78,7 @@ public: ...@@ -78,7 +78,7 @@ public:
} else { } else {
output->SetNullString(); output->SetNullString();
} }
return brpc::RedisCommandHandler::OK; return brpc::REDIS_CMD_HANDLED;
} }
private: private:
...@@ -90,18 +90,18 @@ public: ...@@ -90,18 +90,18 @@ public:
explicit SetCommandHandler(RedisServiceImpl* rsimpl) explicit SetCommandHandler(RedisServiceImpl* rsimpl)
: _rsimpl(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, brpc::RedisReply* output,
bool /*flush_batched*/) override { bool /*flush_batched*/) override {
if (args.size() != 3ul) { if (args.size() != 3ul) {
output->FormatError("Expect 2 args for 'set', actually %lu", args.size()-1); 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 key(args[1]);
const std::string value(args[2]); const std::string value(args[2]);
_rsimpl->Set(key, value); _rsimpl->Set(key, value);
output->SetStatus("OK"); output->SetStatus("OK");
return brpc::RedisCommandHandler::OK; return brpc::REDIS_CMD_HANDLED;
} }
private: private:
......
...@@ -80,12 +80,12 @@ int ConsumeCommand(RedisConnContext* ctx, ...@@ -80,12 +80,12 @@ int ConsumeCommand(RedisConnContext* ctx,
bool flush_batched, bool flush_batched,
butil::IOBufAppender* appender) { butil::IOBufAppender* appender) {
RedisReply output(&ctx->arena); RedisReply output(&ctx->arena);
RedisCommandHandler::Result result = RedisCommandHandler::OK; RedisCommandHandlerResult result = REDIS_CMD_HANDLED;
if (ctx->transaction_handler) { if (ctx->transaction_handler) {
result = ctx->transaction_handler->Run(commands, &output, flush_batched); result = ctx->transaction_handler->Run(commands, &output, flush_batched);
if (result == RedisCommandHandler::OK) { if (result == REDIS_CMD_HANDLED) {
ctx->transaction_handler.reset(NULL); 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."; LOG(ERROR) << "BATCHED should not be returned by a transaction handler.";
return -1; return -1;
} }
...@@ -97,18 +97,18 @@ int ConsumeCommand(RedisConnContext* ctx, ...@@ -97,18 +97,18 @@ int ConsumeCommand(RedisConnContext* ctx,
output.SetError(buf); output.SetError(buf);
} else { } else {
result = ch->Run(commands, &output, flush_batched); result = ch->Run(commands, &output, flush_batched);
if (result == RedisCommandHandler::CONTINUE) { if (result == REDIS_CMD_CONTINUE) {
if (ctx->batched_size != 0) { if (ctx->batched_size != 0) {
LOG(ERROR) << "CONTINUE should not be returned in a batched process."; LOG(ERROR) << "CONTINUE should not be returned in a batched process.";
return -1; return -1;
} }
ctx->transaction_handler.reset(ch->NewTransactionHandler()); ctx->transaction_handler.reset(ch->NewTransactionHandler());
} else if (result == RedisCommandHandler::BATCHED) { } else if (result == REDIS_CMD_BATCHED) {
ctx->batched_size++; ctx->batched_size++;
} }
} }
} }
if (result == RedisCommandHandler::OK) { if (result == REDIS_CMD_HANDLED) {
if (ctx->batched_size) { if (ctx->batched_size) {
if ((int)output.size() != (ctx->batched_size + 1)) { if ((int)output.size() != (ctx->batched_size + 1)) {
LOG(ERROR) << "reply array size can't be matched with batched size, " LOG(ERROR) << "reply array size can't be matched with batched size, "
...@@ -122,9 +122,9 @@ int ConsumeCommand(RedisConnContext* ctx, ...@@ -122,9 +122,9 @@ int ConsumeCommand(RedisConnContext* ctx,
} else { } else {
output.SerializeTo(appender); output.SerializeTo(appender);
} }
} else if (result == RedisCommandHandler::CONTINUE) { } else if (result == REDIS_CMD_CONTINUE) {
output.SerializeTo(appender); output.SerializeTo(appender);
} else if (result == RedisCommandHandler::BATCHED) { } else if (result == REDIS_CMD_BATCHED) {
// just do nothing and wait handler to return OK. // just do nothing and wait handler to return OK.
} else { } else {
LOG(ERROR) << "unknown status=" << result; LOG(ERROR) << "unknown status=" << result;
......
...@@ -231,14 +231,15 @@ private: ...@@ -231,14 +231,15 @@ private:
CommandMap _command_map; 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(). // The Command handler for a redis request. User should impletement Run().
class RedisCommandHandler { class RedisCommandHandler {
public: public:
enum Result {
OK = 0,
CONTINUE = 1,
BATCHED = 2,
};
~RedisCommandHandler() {} ~RedisCommandHandler() {}
// Once Server receives commands, it will first find the corresponding handlers and // Once Server receives commands, it will first find the corresponding handlers and
...@@ -250,24 +251,24 @@ public: ...@@ -250,24 +251,24 @@ public:
// Read brpc/src/redis_reply.h for more usage. // Read brpc/src/redis_reply.h for more usage.
// `flush_batched' indicates whether the user should flush all the results of // `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 // 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 // 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 // The return value should be REDIS_CMD_HANDLED for normal cases. If you want
// to implement transaction, return RedisCommandHandler::CONTINUE once server receives // to implement transaction, return REDIS_CMD_CONTINUE once server receives
// an start marker and brpc will call MultiTransactionHandler() to new a transaction // 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 // handler that all the following commands are sent to this tranction handler until
// it returns RedisCommandHandler::OK. Read the comment below. // it returns REDIS_CMD_HANDLED. Read the comment below.
virtual RedisCommandHandler::Result Run(const std::vector<const char*>& args, virtual RedisCommandHandlerResult Run(const std::vector<const char*>& args,
brpc::RedisReply* output, brpc::RedisReply* output,
bool flush_batched) = 0; bool flush_batched) = 0;
// The Run() returns CONTINUE for "multi", which makes brpc call this method to // The Run() returns CONTINUE for "multi", which makes brpc call this method to
// create a transaction_handler to process following commands until transaction_handler // 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; // returns OK. For example, for command "multi; set k1 v1; set k2 v2; set k3 v3;
// exec": // 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. // then brpc calls NewTransactionHandler() to new a transaction_handler.
// 2) brpc calls transaction_handler.Run() with command "set k1 v1", // 2) brpc calls transaction_handler.Run() with command "set k1 v1",
// which should return CONTINUE. // which should return CONTINUE.
......
...@@ -812,7 +812,7 @@ public: ...@@ -812,7 +812,7 @@ public:
RedisServiceImpl() RedisServiceImpl()
: _batch_count(0) {} : _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) { brpc::RedisReply* output, bool flush_batched) {
if (_batched_command.empty() && flush_batched) { if (_batched_command.empty() && flush_batched) {
if (strcmp(args[0], "set") == 0) { if (strcmp(args[0], "set") == 0) {
...@@ -820,7 +820,7 @@ public: ...@@ -820,7 +820,7 @@ public:
} else if (strcmp(args[0], "get") == 0) { } else if (strcmp(args[0], "get") == 0) {
DoGet(args[1], output); DoGet(args[1], output);
} }
return brpc::RedisCommandHandler::OK; return brpc::REDIS_CMD_HANDLED;
} }
std::vector<std::string> comm; std::vector<std::string> comm;
for (int i = 0; i < (int)args.size(); ++i) { for (int i = 0; i < (int)args.size(); ++i) {
...@@ -838,9 +838,9 @@ public: ...@@ -838,9 +838,9 @@ public:
} }
_batch_count++; _batch_count++;
_batched_command.clear(); _batched_command.clear();
return brpc::RedisCommandHandler::OK; return brpc::REDIS_CMD_HANDLED;
} else { } else {
return brpc::RedisCommandHandler::BATCHED; return brpc::REDIS_CMD_BATCHED;
} }
} }
...@@ -869,18 +869,18 @@ public: ...@@ -869,18 +869,18 @@ public:
: _rs(rs) : _rs(rs)
, _batch_process(batch_process) {} , _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, brpc::RedisReply* output,
bool flush_batched) { bool flush_batched) {
if (args.size() < 3) { if (args.size() < 3) {
output->SetError("ERR wrong number of arguments for 'set' command"); output->SetError("ERR wrong number of arguments for 'set' command");
return brpc::RedisCommandHandler::OK; return brpc::REDIS_CMD_HANDLED;
} }
if (_batch_process) { if (_batch_process) {
return _rs->OnBatched(args, output, flush_batched); return _rs->OnBatched(args, output, flush_batched);
} else { } else {
DoSet(args[1], args[2], output); DoSet(args[1], args[2], output);
return brpc::RedisCommandHandler::OK; return brpc::REDIS_CMD_HANDLED;
} }
} }
...@@ -900,18 +900,18 @@ public: ...@@ -900,18 +900,18 @@ public:
: _rs(rs) : _rs(rs)
, _batch_process(batch_process) {} , _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, brpc::RedisReply* output,
bool flush_batched) { bool flush_batched) {
if (args.size() < 2) { if (args.size() < 2) {
output->SetError("ERR wrong number of arguments for 'get' command"); output->SetError("ERR wrong number of arguments for 'get' command");
return brpc::RedisCommandHandler::OK; return brpc::REDIS_CMD_HANDLED;
} }
if (_batch_process) { if (_batch_process) {
return _rs->OnBatched(args, output, flush_batched); return _rs->OnBatched(args, output, flush_batched);
} else { } else {
DoGet(args[1], output); DoGet(args[1], output);
return brpc::RedisCommandHandler::OK; return brpc::REDIS_CMD_HANDLED;
} }
} }
...@@ -933,12 +933,12 @@ class IncrCommandHandler : public brpc::RedisCommandHandler { ...@@ -933,12 +933,12 @@ class IncrCommandHandler : public brpc::RedisCommandHandler {
public: public:
IncrCommandHandler() {} IncrCommandHandler() {}
brpc::RedisCommandHandler::Result Run(const std::vector<const char*>& args, brpc::RedisCommandHandlerResult Run(const std::vector<const char*>& args,
brpc::RedisReply* output, brpc::RedisReply* output,
bool flush_batched) { bool flush_batched) {
if (args.size() < 2) { if (args.size() < 2) {
output->SetError("ERR wrong number of arguments for 'incr' command"); 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]; const std::string& key = args[1];
int64_t value; int64_t value;
...@@ -946,7 +946,7 @@ public: ...@@ -946,7 +946,7 @@ public:
value = ++int_map[key]; value = ++int_map[key];
s_mutex.unlock(); s_mutex.unlock();
output->SetInteger(value); output->SetInteger(value);
return brpc::RedisCommandHandler::OK; return brpc::REDIS_CMD_HANDLED;
} }
}; };
...@@ -1047,11 +1047,11 @@ class MultiCommandHandler : public brpc::RedisCommandHandler { ...@@ -1047,11 +1047,11 @@ class MultiCommandHandler : public brpc::RedisCommandHandler {
public: public:
MultiCommandHandler() {} MultiCommandHandler() {}
brpc::RedisCommandHandler::Result Run(const std::vector<const char*>& args, brpc::RedisCommandHandlerResult Run(const std::vector<const char*>& args,
brpc::RedisReply* output, brpc::RedisReply* output,
bool flush_batched) { bool flush_batched) {
output->SetStatus("OK"); output->SetStatus("OK");
return brpc::RedisCommandHandler::CONTINUE; return brpc::REDIS_CMD_CONTINUE;
} }
RedisCommandHandler* NewTransactionHandler() override { RedisCommandHandler* NewTransactionHandler() override {
...@@ -1060,12 +1060,12 @@ public: ...@@ -1060,12 +1060,12 @@ public:
class MultiTransactionHandler : public brpc::RedisCommandHandler { class MultiTransactionHandler : public brpc::RedisCommandHandler {
public: public:
brpc::RedisCommandHandler::Result Run(const std::vector<const char*>& args, brpc::RedisCommandHandlerResult Run(const std::vector<const char*>& args,
brpc::RedisReply* output, brpc::RedisReply* output,
bool flush_batched) { bool flush_batched) {
if (strcmp(args[0], "multi") == 0) { if (strcmp(args[0], "multi") == 0) {
output->SetError("ERR duplicate multi"); output->SetError("ERR duplicate multi");
return brpc::RedisCommandHandler::CONTINUE; return brpc::REDIS_CMD_CONTINUE;
} }
if (strcmp(args[0], "exec") != 0) { if (strcmp(args[0], "exec") != 0) {
std::vector<std::string> comm; std::vector<std::string> comm;
...@@ -1074,7 +1074,7 @@ public: ...@@ -1074,7 +1074,7 @@ public:
} }
_commands.push_back(comm); _commands.push_back(comm);
output->SetStatus("QUEUED"); output->SetStatus("QUEUED");
return brpc::RedisCommandHandler::CONTINUE; return brpc::REDIS_CMD_CONTINUE;
} }
output->SetArray(_commands.size()); output->SetArray(_commands.size());
s_mutex.lock(); s_mutex.lock();
...@@ -1088,7 +1088,7 @@ public: ...@@ -1088,7 +1088,7 @@ public:
} }
} }
s_mutex.unlock(); s_mutex.unlock();
return brpc::RedisCommandHandler::OK; return brpc::REDIS_CMD_HANDLED;
} }
private: private:
std::vector<std::vector<std::string> > _commands; std::vector<std::vector<std::string> > _commands;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment