4
4
#include <string.h>
5
5
#include <errno.h>
6
6
#include <limits.h>
7
+ #include <sys/stat.h>
7
8
8
9
/* Rely only on API (and not internal parser headers) */
9
10
#include "../../api/librdb-api.h"
@@ -17,6 +18,7 @@ FILE* logfile = NULL;
17
18
/* common options to all FORMATTERS */
18
19
typedef struct Options {
19
20
const char * logfilePath ;
21
+ int progressMb ; /* print progress every <progressMb> MB. Set to 0 if disabled */
20
22
RdbRes (* formatFunc )(RdbParser * p , int argc , char * * argv );
21
23
} Options ;
22
24
@@ -100,6 +102,7 @@ static void printUsage(int shortUsage) {
100
102
printf ("Usage: rdb-cli /path/to/dump.rdb [OPTIONS] {print|json|resp|redis} [FORMAT_OPTIONS]\n" );
101
103
printf ("OPTIONS:\n" );
102
104
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" );
103
106
printf ("\t-k, --key <REGEX> Include only keys that match REGEX\n" );
104
107
printf ("\t-K --no-key <REGEX> Exclude all keys that match REGEX\n" );
105
108
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
350
353
int at ;
351
354
352
355
/* default */
356
+ options -> progressMb = 0 ;
353
357
options -> logfilePath = LOG_FILE_PATH_DEF ;
354
358
options -> formatFunc = formatJson ;
355
359
@@ -360,6 +364,9 @@ int readCommonOptions(RdbParser *p, int argc, char* argv[], Options *options, in
360
364
if (getOptArg (argc , argv , & at , "-l" , "--log-file" , NULL , & (options -> logfilePath )))
361
365
continue ;
362
366
367
+ if (getOptArgVal (argc , argv , & at , "-s" , "--show-progress" , NULL , & options -> progressMb , 0 , INT_MAX ))
368
+ continue ;
369
+
363
370
if (getOptArg (argc , argv , & at , "-k" , "--key" , NULL , & keyFilter )) {
364
371
if (applyFilters && (!RDBX_createHandlersFilterKey (p , keyFilter , 0 )))
365
372
exit (1 );
@@ -427,6 +434,7 @@ void closeLogFileOnExit() {
427
434
428
435
int main (int argc , char * * argv )
429
436
{
437
+ size_t fileSize = 0 ;
430
438
Options options ;
431
439
RdbStatus status ;
432
440
int at ;
@@ -470,6 +478,15 @@ int main(int argc, char **argv)
470
478
} else {
471
479
if (RDBX_createReaderFile (parser , input /*file*/ ) == NULL )
472
480
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
+ }
473
490
}
474
491
475
492
if (RDB_OK != (res = options .formatFunc (parser , argc - at , argv + at )))
@@ -481,7 +498,29 @@ int main(int argc, char **argv)
481
498
/* now that the formatter got registered, attach filters */
482
499
readCommonOptions (parser , argc , argv , & options , 1 );
483
500
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
+ }
485
524
486
525
if (status != RDB_STATUS_OK )
487
526
return RDB_getErrorCode (parser );
0 commit comments