Skip to content

Commit

Permalink
Add percentage printout
Browse files Browse the repository at this point in the history
  • Loading branch information
moticless committed Jul 19, 2024
1 parent 989ecb5 commit 2cf1327
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ destruction, or when newer block replacing old one.
Usage: rdb-cli /path/to/dump.rdb [OPTIONS] {print|json|resp|redis} [FORMAT_OPTIONS]
OPTIONS:
-l, --log-file <PATH> Path to the log file or stdout (Default: './rdb-cli.log')
-s, --show-progress <INT> Show progress after every <INT> megabytes processed
-s, --show-progress <MBytes> Show progress to STDOUT after every <MBytes> processed
-k, --key <REGEX> Include only keys that match REGEX
-K --no-key <REGEX> Exclude all keys that match REGEX
-t, --type <TYPE> Include only selected TYPE {str|list|set|zset|hash|module|func}
Expand Down
30 changes: 24 additions & 6 deletions src/cli/rdb-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <sys/stat.h>

/* Rely only on API (and not internal parser headers) */
#include "../../api/librdb-api.h"
Expand Down Expand Up @@ -101,7 +102,7 @@ static void printUsage(int shortUsage) {
printf("Usage: rdb-cli /path/to/dump.rdb [OPTIONS] {print|json|resp|redis} [FORMAT_OPTIONS]\n");
printf("OPTIONS:\n");
printf("\t-l, --log-file <PATH> Path to the log file or stdout (Default: './rdb-cli.log')\n");
printf("\t-s, --show-progress <INT> Show progress after every <INT> megabytes processed\n");
printf("\t-s, --show-progress <MBytes> Show progress to STDOUT after every <MBytes> processed\n");
printf("\t-k, --key <REGEX> Include only keys that match REGEX\n");
printf("\t-K --no-key <REGEX> Exclude all keys that match REGEX\n");
printf("\t-t, --type <TYPE> Include only selected TYPE {str|list|set|zset|hash|module|func}\n");
Expand Down Expand Up @@ -433,6 +434,7 @@ void closeLogFileOnExit() {

int main(int argc, char **argv)
{
size_t fileSize = 0;
Options options;
RdbStatus status;
int at;
Expand Down Expand Up @@ -476,6 +478,15 @@ int main(int argc, char **argv)
} else {
if (RDBX_createReaderFile(parser, input /*file*/) == NULL)
return RDB_ERR_GENERAL;

/* If input is a file, then get its size */
struct stat st;
if (stat(input, &st) == 0) {
fileSize = st.st_size;
} else {
printf("Error getting file size: %s\n", strerror(errno));
return RDB_ERR_GENERAL;
}
}

if (RDB_OK != (res = options.formatFunc(parser, argc - at, argv + at)))
Expand All @@ -494,11 +505,18 @@ int main(int argc, char **argv)
status = RDB_parse(parser);
if (status == RDB_STATUS_WAIT_MORE_DATA)
continue;
else if (status == RDB_STATUS_PAUSED)
printf("... Processed %zuMBytes ...\n",
RDB_getBytesProcessed(parser) / (1024 * 1024));
else
break;
else if (status == RDB_STATUS_PAUSED) {
size_t bytes = RDB_getBytesProcessed(parser);
/* If file size is known, print percentage */
if (fileSize != 0)
printf("... Processed %zuMBytes (%.2f%%) ...\n",
bytes / (1024 * 1024), (bytes * 100.0) / fileSize);
else
printf("... Processed %zuMBytes ...\n", bytes / (1024 * 1024));
continue;
}

break; /* RDB_STATUS_ERROR */
}
} else {
while ((status = RDB_parse(parser)) == RDB_STATUS_WAIT_MORE_DATA);
Expand Down

0 comments on commit 2cf1327

Please sign in to comment.