Commit cb3460be authored by zhujiashun's avatar zhujiashun

redis_server_protocol: refine RedisCommandParser member func name

parent 24fe5946
...@@ -194,12 +194,12 @@ ParseResult ParseRedisMessage(butil::IOBuf* source, Socket* socket, ...@@ -194,12 +194,12 @@ ParseResult ParseRedisMessage(butil::IOBuf* source, Socket* socket,
} }
socket->reset_parsing_context(ctx); socket->reset_parsing_context(ctx);
} }
ParseError err = ctx->parser.ParseCommand(*source); ParseError err = ctx->parser.Parse(*source);
if (err != PARSE_OK) { if (err != PARSE_OK) {
return MakeParseError(err); return MakeParseError(err);
} }
std::unique_ptr<std::string> command(new std::string); std::unique_ptr<std::string> command(new std::string);
command->swap(ctx->parser.Command()); ctx->parser.SwapCommandTo(command.get());
if (bthread::execution_queue_execute(ctx->queue, command.get()) != 0) { if (bthread::execution_queue_execute(ctx->queue, command.get()) != 0) {
LOG(ERROR) << "Fail to push execution queue"; LOG(ERROR) << "Fail to push execution queue";
return MakeParseError(PARSE_ERROR_NO_RESOURCE); return MakeParseError(PARSE_ERROR_NO_RESOURCE);
......
...@@ -364,7 +364,7 @@ RedisCommandParser::RedisCommandParser() { ...@@ -364,7 +364,7 @@ RedisCommandParser::RedisCommandParser() {
Reset(); Reset();
} }
ParseError RedisCommandParser::ParseCommand(butil::IOBuf& buf) { ParseError RedisCommandParser::Parse(butil::IOBuf& buf) {
const char* pfc = (const char*)buf.fetch1(); const char* pfc = (const char*)buf.fetch1();
if (pfc == NULL) { if (pfc == NULL) {
return PARSE_ERROR_NOT_ENOUGH_DATA; return PARSE_ERROR_NOT_ENOUGH_DATA;
...@@ -396,10 +396,10 @@ ParseError RedisCommandParser::ParseCommand(butil::IOBuf& buf) { ...@@ -396,10 +396,10 @@ ParseError RedisCommandParser::ParseCommand(butil::IOBuf& buf) {
_length = value; _length = value;
_index = 0; _index = 0;
_command.clear(); _command.clear();
return ParseCommand(buf); return Parse(buf);
} }
CHECK(_index < _length) << "a complete command has been parsed. " CHECK(_index < _length) << "a complete command has been parsed. "
"impl of RedisCommandParser::ParseCommand is buggy"; "impl of RedisCommandParser::Parse is buggy";
const int64_t len = value; // `value' is length of the string const int64_t len = value; // `value' is length of the string
if (len < 0) { if (len < 0) {
LOG(ERROR) << "string in command is nil!"; LOG(ERROR) << "string in command is nil!";
...@@ -425,12 +425,17 @@ ParseError RedisCommandParser::ParseCommand(butil::IOBuf& buf) { ...@@ -425,12 +425,17 @@ ParseError RedisCommandParser::ParseCommand(butil::IOBuf& buf) {
return PARSE_ERROR_ABSOLUTELY_WRONG; return PARSE_ERROR_ABSOLUTELY_WRONG;
} }
if (++_index < _length) { if (++_index < _length) {
return ParseCommand(buf); return Parse(buf);
} }
Reset(); Reset();
return PARSE_OK; return PARSE_OK;
} }
void RedisCommandParser::SwapCommandTo(std::string* out) {
out->clear();
out->swap(_command);
}
void RedisCommandParser::Reset() { void RedisCommandParser::Reset() {
_parsing_array = false; _parsing_array = false;
_length = 0; _length = 0;
......
...@@ -46,11 +46,11 @@ public: ...@@ -46,11 +46,11 @@ public:
RedisCommandParser(); RedisCommandParser();
// Parse raw message from `buf'. Return PARSE_OK if successful. // Parse raw message from `buf'. Return PARSE_OK if successful.
ParseError ParseCommand(butil::IOBuf& buf); ParseError Parse(butil::IOBuf& buf);
// After ParseCommand returns PARSE_OK, call this function to get // After Parse returns PARSE_OK, call this function to swap
// the parsed command string. // the parsed command string to `out'.
std::string& Command() { return _command; } void SwapCommandTo(std::string* out);
private: private:
// Reset parser to the initial state. // Reset parser to the initial state.
......
...@@ -557,52 +557,56 @@ TEST_F(RedisTest, command_parser) { ...@@ -557,52 +557,56 @@ TEST_F(RedisTest, command_parser) {
// parse from whole command // parse from whole command
std::string command = "set abc edc"; std::string command = "set abc edc";
ASSERT_TRUE(brpc::RedisCommandNoFormat(&buf, command.c_str()).ok()); ASSERT_TRUE(brpc::RedisCommandNoFormat(&buf, command.c_str()).ok());
ASSERT_EQ(brpc::PARSE_OK, parser.ParseCommand(buf)); ASSERT_EQ(brpc::PARSE_OK, parser.Parse(buf));
ASSERT_TRUE(buf.empty()); ASSERT_TRUE(buf.empty());
ASSERT_STREQ(command.c_str(), parser.Command().c_str()); std::string command_out;
parser.SwapCommandTo(&command_out);
ASSERT_STREQ(command.c_str(), command_out.c_str());
} }
{ {
// parse from two consecutive buf // parse from two consecutive buf
buf.append("*3\r\n$3"); buf.append("*3\r\n$3");
ASSERT_EQ(brpc::PARSE_ERROR_NOT_ENOUGH_DATA, ASSERT_EQ(brpc::PARSE_ERROR_NOT_ENOUGH_DATA,
parser.ParseCommand(buf)); parser.Parse(buf));
ASSERT_EQ((int)buf.size(), 2); // left "$3" ASSERT_EQ((int)buf.size(), 2); // left "$3"
buf.append("\r\nset\r\n$3\r\nabc\r\n$3\r\ndef\r\n"); buf.append("\r\nset\r\n$3\r\nabc\r\n$3\r\ndef\r\n");
ASSERT_EQ(brpc::PARSE_OK, parser.ParseCommand(buf)); ASSERT_EQ(brpc::PARSE_OK, parser.Parse(buf));
ASSERT_TRUE(buf.empty()); ASSERT_TRUE(buf.empty());
ASSERT_STREQ(parser.Command().c_str(), "set abc def"); std::string command_out;
parser.SwapCommandTo(&command_out);
ASSERT_STREQ(command_out.c_str(), "set abc def");
} }
{ {
// there is a non-string message in command and parse should fail // there is a non-string message in command and parse should fail
buf.append("*3\r\n$3"); buf.append("*3\r\n$3");
ASSERT_EQ(brpc::PARSE_ERROR_NOT_ENOUGH_DATA, parser.ParseCommand(buf)); ASSERT_EQ(brpc::PARSE_ERROR_NOT_ENOUGH_DATA, parser.Parse(buf));
ASSERT_EQ((int)buf.size(), 2); // left "$3" ASSERT_EQ((int)buf.size(), 2); // left "$3"
buf.append("\r\nset\r\n:123\r\n$3\r\ndef\r\n"); buf.append("\r\nset\r\n:123\r\n$3\r\ndef\r\n");
ASSERT_EQ(brpc::PARSE_ERROR_ABSOLUTELY_WRONG, parser.ParseCommand(buf)); ASSERT_EQ(brpc::PARSE_ERROR_ABSOLUTELY_WRONG, parser.Parse(buf));
parser.Reset(); parser.Reset();
} }
{ {
// not array // not array
buf.append(":123456\r\n"); buf.append(":123456\r\n");
ASSERT_EQ(brpc::PARSE_ERROR_TRY_OTHERS, parser.ParseCommand(buf)); ASSERT_EQ(brpc::PARSE_ERROR_TRY_OTHERS, parser.Parse(buf));
parser.Reset(); parser.Reset();
} }
{ {
// not array // not array
buf.append("+Error\r\n"); buf.append("+Error\r\n");
ASSERT_EQ(brpc::PARSE_ERROR_TRY_OTHERS, parser.ParseCommand(buf)); ASSERT_EQ(brpc::PARSE_ERROR_TRY_OTHERS, parser.Parse(buf));
parser.Reset(); parser.Reset();
} }
{ {
// not array // not array
buf.append("+OK\r\n"); buf.append("+OK\r\n");
ASSERT_EQ(brpc::PARSE_ERROR_TRY_OTHERS, parser.ParseCommand(buf)); ASSERT_EQ(brpc::PARSE_ERROR_TRY_OTHERS, parser.Parse(buf));
parser.Reset(); parser.Reset();
} }
{ {
// not array // not array
buf.append("$5\r\nhello\r\n"); buf.append("$5\r\nhello\r\n");
ASSERT_EQ(brpc::PARSE_ERROR_TRY_OTHERS, parser.ParseCommand(buf)); ASSERT_EQ(brpc::PARSE_ERROR_TRY_OTHERS, parser.Parse(buf));
parser.Reset(); parser.Reset();
} }
} }
......
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