redis-cli - add option --count for scan (#12042)

When using scan in redis-cli, the SCAN COUNT is fixed, which means the
full scan can take a long time if there are a lot of keys, this will let users specify
a bigger COUNT option.
This commit is contained in:
kell0gg 2023-05-11 14:14:59 +09:00 committed by GitHub
parent 8597991e8f
commit aac8105c9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -239,6 +239,7 @@ static struct config {
int get_functions_rdb_mode; int get_functions_rdb_mode;
int stat_mode; int stat_mode;
int scan_mode; int scan_mode;
int count;
int intrinsic_latency_mode; int intrinsic_latency_mode;
int intrinsic_latency_duration; int intrinsic_latency_duration;
sds pattern; sds pattern;
@ -2727,6 +2728,8 @@ static int parseOptions(int argc, char **argv) {
} else if (!strcmp(argv[i],"--pattern") && !lastarg) { } else if (!strcmp(argv[i],"--pattern") && !lastarg) {
sdsfree(config.pattern); sdsfree(config.pattern);
config.pattern = sdsnew(argv[++i]); config.pattern = sdsnew(argv[++i]);
} else if (!strcmp(argv[i],"--count") && !lastarg) {
config.count = atoi(argv[++i]);
} else if (!strcmp(argv[i],"--quoted-pattern") && !lastarg) { } else if (!strcmp(argv[i],"--quoted-pattern") && !lastarg) {
sdsfree(config.pattern); sdsfree(config.pattern);
config.pattern = unquoteCString(argv[++i]); config.pattern = unquoteCString(argv[++i]);
@ -3081,6 +3084,7 @@ version,tls_usage);
" --scan List all keys using the SCAN command.\n" " --scan List all keys using the SCAN command.\n"
" --pattern <pat> Keys pattern when using the --scan, --bigkeys or --hotkeys\n" " --pattern <pat> Keys pattern when using the --scan, --bigkeys or --hotkeys\n"
" options (default: *).\n" " options (default: *).\n"
" --count <count> Count option when using the --scan, --bigkeys or --hotkeys (default: 10).\n"
" --quoted-pattern <pat> Same as --pattern, but the specified string can be\n" " --quoted-pattern <pat> Same as --pattern, but the specified string can be\n"
" quoted, in order to pass an otherwise non binary-safe string.\n" " quoted, in order to pass an otherwise non binary-safe string.\n"
" --intrinsic-latency <sec> Run a test to measure intrinsic system latency.\n" " --intrinsic-latency <sec> Run a test to measure intrinsic system latency.\n"
@ -3111,6 +3115,7 @@ version,tls_usage);
" redis-cli --quoted-input set '\"null-\\x00-separated\"' value\n" " redis-cli --quoted-input set '\"null-\\x00-separated\"' value\n"
" redis-cli --eval myscript.lua key1 key2 , arg1 arg2 arg3\n" " redis-cli --eval myscript.lua key1 key2 , arg1 arg2 arg3\n"
" redis-cli --scan --pattern '*:12345*'\n" " redis-cli --scan --pattern '*:12345*'\n"
" redis-cli --scan --pattern '*:12345*' --count 100\n"
"\n" "\n"
" (Note: when using --eval the comma separates KEYS[] from ARGV[] items)\n" " (Note: when using --eval the comma separates KEYS[] from ARGV[] items)\n"
"\n" "\n"
@ -8826,8 +8831,8 @@ static redisReply *sendScan(unsigned long long *it) {
redisReply *reply; redisReply *reply;
if (config.pattern) if (config.pattern)
reply = redisCommand(context, "SCAN %llu MATCH %b", reply = redisCommand(context, "SCAN %llu MATCH %b COUNT %d",
*it, config.pattern, sdslen(config.pattern)); *it, config.pattern, sdslen(config.pattern), config.count);
else else
reply = redisCommand(context,"SCAN %llu",*it); reply = redisCommand(context,"SCAN %llu",*it);
@ -9770,6 +9775,7 @@ int main(int argc, char **argv) {
config.get_functions_rdb_mode = 0; config.get_functions_rdb_mode = 0;
config.stat_mode = 0; config.stat_mode = 0;
config.scan_mode = 0; config.scan_mode = 0;
config.count = 10;
config.intrinsic_latency_mode = 0; config.intrinsic_latency_mode = 0;
config.pattern = NULL; config.pattern = NULL;
config.rdb_filename = NULL; config.rdb_filename = NULL;