summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2014-01-22 12:04:08 +0100
committerantirez <antirez@gmail.com>2014-01-22 12:08:04 +0100
commitb9c84d4aef89f8f6104453cbd2283934e0e042bf (patch)
tree4673856008e8f2a2af30a2aa8f6883efcfaec2d0
parent248e9165507821c22f4724194792ccfeb2c980cb (diff)
downloadredis-b9c84d4aef89f8f6104453cbd2283934e0e042bf.tar.gz
redis-cli: support for --scan option.
-rw-r--r--src/redis-cli.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/redis-cli.c b/src/redis-cli.c
index 265cea0c1..47798fdee 100644
--- a/src/redis-cli.c
+++ b/src/redis-cli.c
@@ -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.