From d9bd306da3f2e9c9470ec266b151ef15181fca4a Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Thu, 3 Dec 2020 21:52:22 +0300 Subject: [PATCH] mdbx-load: add `-p` option (purge subDB). Change-Id: I678950c99c8b4aea1add4ce548c9b4fe4ab8bfe6 --- ChangeLog.md | 3 ++- src/mdbx_load.c | 23 ++++++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 38c15a4d..1622a622 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -21,11 +21,12 @@ Removed options and features: - Drop `MDBX_HUGE_TRANSACTIONS` build-option (now no longer required). -Added features: +New features: - Package for FreeBSD is available now by Mahlon E. Smith. - New API functions to get/set various options (https://github.com/erthink/libmdbx/issues/128). - Unlimited/Dynamic size of retired and dirty page lists (https://github.com/erthink/libmdbx/issues/123). + - Added `-p` option (purge subDB before loading) to `mdbx_load` tool. Fixes: diff --git a/src/mdbx_load.c b/src/mdbx_load.c index 080920a2..81a8daa8 100644 --- a/src/mdbx_load.c +++ b/src/mdbx_load.c @@ -448,16 +448,17 @@ static int readline(MDBX_val *out, MDBX_val *buf) { static void usage(void) { fprintf(stderr, - "usage: %s [-V] [-q] [-a] [-f file] [-s name] [-N] [-T] [-r] [-n]" - " dbpath\n" + "usage: %s " + "[-V] [-q] [-a] [-f file] [-s name] [-N] [-p] [-T] [-r] [-n] dbpath\n" " -V\t\tprint version and exit\n" " -q\t\tbe quiet\n" " -a\t\tappend records in input order (required for custom " "comparators)\n" " -f file\tread from file instead of stdin\n" " -s name\tload into named subDB\n" - " -N\t\tdon't overwrite existing records when loading (), just skip " - "them\n" + " -N\t\tdon't overwrite existing records when loading, just skip " + "ones\n" + " -p\t\tpurge subDB before loading\n" " -T\t\tread plaintext\n" " -r\t\trescue mode (ignore errors to load corrupted DB dump)\n" " -n\t\tdon't use subdirectory for newly created database " @@ -483,12 +484,13 @@ int main(int argc, char *argv[]) { int envflags = MDBX_SAFE_NOSYNC | MDBX_ACCEDE, putflags = MDBX_UPSERT; bool quiet = false; bool rescue = false; + bool purge = false; prog = argv[0]; if (argc < 2) usage(); - while ((i = getopt(argc, argv, "af:ns:NTVrq")) != EOF) { + while ((i = getopt(argc, argv, "af:ns:NpTVrq")) != EOF) { switch (i) { case 'V': printf("mdbx_load version %d.%d.%d.%d\n" @@ -523,6 +525,9 @@ int main(int argc, char *argv[]) { case 'N': putflags |= MDBX_NOOVERWRITE | MDBX_NODUPDATA; break; + case 'p': + purge = true; + break; case 'T': mode |= NOHDR | PRINT; break; @@ -696,6 +701,14 @@ int main(int argc, char *argv[]) { } } + if (purge) { + rc = mdbx_drop(txn, dbi, false); + if (unlikely(rc != MDBX_SUCCESS)) { + error("mdbx_drop", rc); + goto txn_abort; + } + } + if (putflags & MDBX_APPEND) putflags = (dbi_flags & MDBX_DUPSORT) ? putflags | MDBX_APPENDDUP : putflags & ~MDBX_APPENDDUP;