Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
B
brpc
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
brpc
Commits
cb3460be
Commit
cb3460be
authored
Dec 10, 2019
by
zhujiashun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
redis_server_protocol: refine RedisCommandParser member func name
parent
24fe5946
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
34 additions
and
25 deletions
+34
-25
redis_protocol.cpp
src/brpc/policy/redis_protocol.cpp
+2
-2
redis_command.cpp
src/brpc/redis_command.cpp
+9
-4
redis_command.h
src/brpc/redis_command.h
+8
-8
brpc_redis_unittest.cpp
test/brpc_redis_unittest.cpp
+15
-11
No files found.
src/brpc/policy/redis_protocol.cpp
View file @
cb3460be
...
...
@@ -194,12 +194,12 @@ ParseResult ParseRedisMessage(butil::IOBuf* source, Socket* socket,
}
socket
->
reset_parsing_context
(
ctx
);
}
ParseError
err
=
ctx
->
parser
.
Parse
Command
(
*
source
);
ParseError
err
=
ctx
->
parser
.
Parse
(
*
source
);
if
(
err
!=
PARSE_OK
)
{
return
MakeParseError
(
err
);
}
std
::
unique_ptr
<
std
::
string
>
command
(
new
std
::
string
);
c
ommand
->
swap
(
ctx
->
parser
.
Command
());
c
tx
->
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
);
...
...
src/brpc/redis_command.cpp
View file @
cb3460be
...
...
@@ -364,7 +364,7 @@ RedisCommandParser::RedisCommandParser() {
Reset
();
}
ParseError
RedisCommandParser
::
Parse
Command
(
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
Parse
Command
(
buf
);
return
Parse
(
buf
);
}
CHECK
(
_index
<
_length
)
<<
"a complete command has been parsed. "
"impl of RedisCommandParser::Parse
Command
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
Parse
Command
(
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
;
...
...
src/brpc/redis_command.h
View file @
cb3460be
...
...
@@ -46,20 +46,20 @@ public:
RedisCommandParser
();
// Parse raw message from `buf'. Return PARSE_OK if successful.
ParseError
Parse
Command
(
butil
::
IOBuf
&
buf
);
ParseError
Parse
(
butil
::
IOBuf
&
buf
);
// After Parse
Command 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
...
...
test/brpc_redis_unittest.cpp
View file @
cb3460be
...
...
@@ -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
.
Parse
Command
(
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
.
Parse
Command
(
buf
));
parser
.
Parse
(
buf
));
ASSERT_EQ
((
int
)
buf
.
size
(),
2
);
// left "$3"
buf
.
append
(
"
\r\n
set
\r\n
$3
\r\n
abc
\r\n
$3
\r\n
def
\r\n
"
);
ASSERT_EQ
(
brpc
::
PARSE_OK
,
parser
.
Parse
Command
(
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
.
Parse
Command
(
buf
));
ASSERT_EQ
(
brpc
::
PARSE_ERROR_NOT_ENOUGH_DATA
,
parser
.
Parse
(
buf
));
ASSERT_EQ
((
int
)
buf
.
size
(),
2
);
// left "$3"
buf
.
append
(
"
\r\n
set
\r\n
:123
\r\n
$3
\r\n
def
\r\n
"
);
ASSERT_EQ
(
brpc
::
PARSE_ERROR_ABSOLUTELY_WRONG
,
parser
.
Parse
Command
(
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
.
Parse
Command
(
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
.
Parse
Command
(
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
.
Parse
Command
(
buf
));
ASSERT_EQ
(
brpc
::
PARSE_ERROR_TRY_OTHERS
,
parser
.
Parse
(
buf
));
parser
.
Reset
();
}
{
// not array
buf
.
append
(
"$5
\r\n
hello
\r\n
"
);
ASSERT_EQ
(
brpc
::
PARSE_ERROR_TRY_OTHERS
,
parser
.
Parse
Command
(
buf
));
ASSERT_EQ
(
brpc
::
PARSE_ERROR_TRY_OTHERS
,
parser
.
Parse
(
buf
));
parser
.
Reset
();
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment