Skip to content

Commit b401df1

Browse files
authored
Add --show-progress to rdb-cli options (redis#50)
1 parent 5e4f7a2 commit b401df1

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ destruction, or when newer block replacing old one.
199199
Usage: rdb-cli /path/to/dump.rdb [OPTIONS] {print|json|resp|redis} [FORMAT_OPTIONS]
200200
OPTIONS:
201201
-l, --log-file <PATH> Path to the log file or stdout (Default: './rdb-cli.log')
202+
-s, --show-progress <MBytes> Show progress to STDOUT after every <MBytes> processed
202203
-k, --key <REGEX> Include only keys that match REGEX
203204
-K --no-key <REGEX> Exclude all keys that match REGEX
204205
-t, --type <TYPE> Include only selected TYPE {str|list|set|zset|hash|module|func}

src/cli/rdb-cli.c

+40-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <string.h>
55
#include <errno.h>
66
#include <limits.h>
7+
#include <sys/stat.h>
78

89
/* Rely only on API (and not internal parser headers) */
910
#include "../../api/librdb-api.h"
@@ -17,6 +18,7 @@ FILE* logfile = NULL;
1718
/* common options to all FORMATTERS */
1819
typedef struct Options {
1920
const char *logfilePath;
21+
int progressMb; /* print progress every <progressMb> MB. Set to 0 if disabled */
2022
RdbRes (*formatFunc)(RdbParser *p, int argc, char **argv);
2123
} Options;
2224

@@ -100,6 +102,7 @@ static void printUsage(int shortUsage) {
100102
printf("Usage: rdb-cli /path/to/dump.rdb [OPTIONS] {print|json|resp|redis} [FORMAT_OPTIONS]\n");
101103
printf("OPTIONS:\n");
102104
printf("\t-l, --log-file <PATH> Path to the log file or stdout (Default: './rdb-cli.log')\n");
105+
printf("\t-s, --show-progress <MBytes> Show progress to STDOUT after every <MBytes> processed\n");
103106
printf("\t-k, --key <REGEX> Include only keys that match REGEX\n");
104107
printf("\t-K --no-key <REGEX> Exclude all keys that match REGEX\n");
105108
printf("\t-t, --type <TYPE> Include only selected TYPE {str|list|set|zset|hash|module|func}\n");
@@ -350,6 +353,7 @@ int readCommonOptions(RdbParser *p, int argc, char* argv[], Options *options, in
350353
int at;
351354

352355
/* default */
356+
options->progressMb = 0;
353357
options->logfilePath = LOG_FILE_PATH_DEF;
354358
options->formatFunc = formatJson;
355359

@@ -360,6 +364,9 @@ int readCommonOptions(RdbParser *p, int argc, char* argv[], Options *options, in
360364
if (getOptArg(argc, argv, &at, "-l", "--log-file", NULL, &(options->logfilePath)))
361365
continue;
362366

367+
if (getOptArgVal(argc, argv, &at, "-s", "--show-progress", NULL, &options->progressMb, 0, INT_MAX))
368+
continue;
369+
363370
if (getOptArg(argc, argv, &at, "-k", "--key", NULL, &keyFilter)) {
364371
if (applyFilters && (!RDBX_createHandlersFilterKey(p, keyFilter, 0)))
365372
exit(1);
@@ -427,6 +434,7 @@ void closeLogFileOnExit() {
427434

428435
int main(int argc, char **argv)
429436
{
437+
size_t fileSize = 0;
430438
Options options;
431439
RdbStatus status;
432440
int at;
@@ -470,6 +478,15 @@ int main(int argc, char **argv)
470478
} else {
471479
if (RDBX_createReaderFile(parser, input /*file*/) == NULL)
472480
return RDB_ERR_GENERAL;
481+
482+
/* If input is a file, then get its size */
483+
struct stat st;
484+
if (stat(input, &st) == 0) {
485+
fileSize = st.st_size;
486+
} else {
487+
printf("Error getting file size: %s\n", strerror(errno));
488+
return RDB_ERR_GENERAL;
489+
}
473490
}
474491

475492
if (RDB_OK != (res = options.formatFunc(parser, argc - at, argv + at)))
@@ -481,7 +498,29 @@ int main(int argc, char **argv)
481498
/* now that the formatter got registered, attach filters */
482499
readCommonOptions(parser, argc, argv, &options, 1);
483500

484-
while ((status = RDB_parse(parser)) == RDB_STATUS_WAIT_MORE_DATA);
501+
/* If requested to print progress */
502+
if (options.progressMb) {
503+
RDB_setPauseInterval(parser, options.progressMb * 1024 * 1024);
504+
while (1) {
505+
status = RDB_parse(parser);
506+
if (status == RDB_STATUS_WAIT_MORE_DATA)
507+
continue;
508+
else if (status == RDB_STATUS_PAUSED) {
509+
size_t bytes = RDB_getBytesProcessed(parser);
510+
/* If file size is known, print percentage */
511+
if (fileSize != 0)
512+
printf("... Processed %zuMBytes (%.2f%%) ...\n",
513+
bytes / (1024 * 1024), (bytes * 100.0) / fileSize);
514+
else
515+
printf("... Processed %zuMBytes ...\n", bytes / (1024 * 1024));
516+
continue;
517+
}
518+
519+
break; /* RDB_STATUS_ERROR */
520+
}
521+
} else {
522+
while ((status = RDB_parse(parser)) == RDB_STATUS_WAIT_MORE_DATA);
523+
}
485524

486525
if (status != RDB_STATUS_OK)
487526
return RDB_getErrorCode(parser);

0 commit comments

Comments
 (0)