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,
}
socket->reset_parsing_context(ctx);
}
ParseError err = ctx->parser.ParseCommand(*source);
ParseError err = ctx->parser.Parse(*source);
if (err != PARSE_OK) {
return MakeParseError(err);
}
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) {
LOG(ERROR) << "Fail to push execution queue";
return MakeParseError(PARSE_ERROR_NO_RESOURCE);
......
......@@ -364,7 +364,7 @@ RedisCommandParser::RedisCommandParser() {
Reset();
}
ParseError RedisCommandParser::ParseCommand(butil::IOBuf& buf) {
ParseError RedisCommandParser::Parse(butil::IOBuf& buf) {
const char* pfc = (const char*)buf.fetch1();
if (pfc == NULL) {
return PARSE_ERROR_NOT_ENOUGH_DATA;
......@@ -396,10 +396,10 @@ ParseError RedisCommandParser::ParseCommand(butil::IOBuf& buf) {
_length = value;
_index = 0;
_command.clear();
return ParseCommand(buf);
return Parse(buf);
}
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
if (len < 0) {
LOG(ERROR) << "string in command is nil!";
......@@ -425,12 +425,17 @@ ParseError RedisCommandParser::ParseCommand(butil::IOBuf& buf) {
return PARSE_ERROR_ABSOLUTELY_WRONG;
}
if (++_index < _length) {
return ParseCommand(buf);
return Parse(buf);
}
Reset();
return PARSE_OK;
}
void RedisCommandParser::SwapCommandTo(std::string* out) {
out->clear();
out->swap(_command);
}
void RedisCommandParser::Reset() {
_parsing_array = false;
_length = 0;
......
......@@ -46,20 +46,20 @@ public:
RedisCommandParser();
// 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
// the parsed command string.
std::string& Command() { return _command; }
// After Parse returns PARSE_OK, call this function to swap
// the parsed command string to `out'.
void SwapCommandTo(std::string* out);
private:
// Reset parser to the initial state.
void Reset();
bool _parsing_array; // if the parser has met array indicator '*'
int _length; // array length
int _index; // current parsing array index
std::string _command; // parsed command string
bool _parsing_array; // if the parser has met array indicator '*'
int _length; // array length
int _index; // current parsing array index
std::string _command; // parsed command string
};
} // namespace brpc
......
......@@ -557,52 +557,56 @@ TEST_F(RedisTest, command_parser) {
// parse from whole command
std::string command = "set abc edc";
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_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
buf.append("*3\r\n$3");
ASSERT_EQ(brpc::PARSE_ERROR_NOT_ENOUGH_DATA,
parser.ParseCommand(buf));
parser.Parse(buf));
ASSERT_EQ((int)buf.size(), 2); // left "$3"
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_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
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"
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();
}
{
// not array
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();
}
{
// not array
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();
}
{
// not array
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();
}
{
// not array
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();
}
}
......
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