Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
b9c84d4a
Commit
b9c84d4a
authored
Jan 22, 2014
by
antirez
Browse files
redis-cli: support for --scan option.
parent
248e9165
Changes
1
Show whitespace changes
Inline
Side-by-side
src/redis-cli.c
View file @
b9c84d4a
...
...
@@ -81,6 +81,8 @@ static struct config {
int
pipe_timeout
;
int
getrdb_mode
;
int
stat_mode
;
int
scan_mode
;
char
*
pattern
;
char
*
rdb_filename
;
int
bigkeys
;
int
stdinarg
;
/* get last arg from stdin. (-x option) */
...
...
@@ -711,6 +713,10 @@ static int parseOptions(int argc, char **argv) {
config
.
slave_mode
=
1
;
}
else
if
(
!
strcmp
(
argv
[
i
],
"--stat"
))
{
config
.
stat_mode
=
1
;
}
else
if
(
!
strcmp
(
argv
[
i
],
"--scan"
))
{
config
.
scan_mode
=
1
;
}
else
if
(
!
strcmp
(
argv
[
i
],
"--pattern"
))
{
config
.
pattern
=
argv
[
++
i
];
}
else
if
(
!
strcmp
(
argv
[
i
],
"--rdb"
)
&&
!
lastarg
)
{
config
.
getrdb_mode
=
1
;
config
.
rdb_filename
=
argv
[
++
i
];
...
...
@@ -794,6 +800,8 @@ static void usage() {
" no reply is received within <n> seconds.
\n
"
" Default timeout: %d. Use 0 to wait forever.
\n
"
" --bigkeys Sample Redis keys looking for big keys
\n
"
" --scan List all keys using the SCAN command
\n
"
" --pattern <pat> Useful with --scan to specify a SCAN pattern
\n
"
" --eval <file> Send an EVAL command using the Lua script at <file>
\n
"
" --help Output this help and exit
\n
"
" --version Output version and exit
\n
"
...
...
@@ -1493,6 +1501,35 @@ static void statMode() {
}
}
static
void
scanMode
()
{
redisReply
*
reply
;
unsigned
long
long
cur
=
0
;
do
{
if
(
config
.
pattern
)
reply
=
redisCommand
(
context
,
"SCAN %llu MATCH %s"
,
cur
,
config
.
pattern
);
else
reply
=
redisCommand
(
context
,
"SCAN %llu"
,
cur
);
if
(
reply
==
NULL
)
{
printf
(
"I/O error
\n
"
);
exit
(
1
);
}
else
if
(
reply
->
type
==
REDIS_REPLY_ERROR
)
{
printf
(
"ERROR: %s
\n
"
,
reply
->
str
);
exit
(
1
);
}
else
{
int
j
;
cur
=
strtoull
(
reply
->
element
[
0
]
->
str
,
NULL
,
10
);
for
(
j
=
0
;
j
<
reply
->
element
[
1
]
->
elements
;
j
++
)
printf
(
"%s
\n
"
,
reply
->
element
[
1
]
->
element
[
j
]
->
str
);
}
freeReplyObject
(
reply
);
}
while
(
cur
!=
0
);
exit
(
0
);
}
int
main
(
int
argc
,
char
**
argv
)
{
int
firstarg
;
...
...
@@ -1511,6 +1548,9 @@ int main(int argc, char **argv) {
config
.
cluster_mode
=
0
;
config
.
slave_mode
=
0
;
config
.
getrdb_mode
=
0
;
config
.
stat_mode
=
0
;
config
.
scan_mode
=
0
;
config
.
pattern
=
NULL
;
config
.
rdb_filename
=
NULL
;
config
.
pipe_mode
=
0
;
config
.
pipe_timeout
=
REDIS_DEFAULT_PIPE_TIMEOUT
;
...
...
@@ -1566,6 +1606,12 @@ int main(int argc, char **argv) {
statMode
();
}
/* Scan mode */
if
(
config
.
scan_mode
)
{
if
(
cliConnect
(
0
)
==
REDIS_ERR
)
exit
(
1
);
scanMode
();
}
/* Start interactive mode when no command is provided */
if
(
argc
==
0
&&
!
config
.
eval
)
{
/* Note that in repl mode we don't abort on connection error.
...
...
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