mdbx-chk: rework/fix read-write mode.

This commit is contained in:
Leo Yuriev 2018-03-19 18:06:31 +03:00
parent b2213c86fe
commit d0e1c02a8a

View File

@ -77,7 +77,7 @@ int exclusive = 2;
int envflags = MDBX_RDONLY; int envflags = MDBX_RDONLY;
MDBX_env *env; MDBX_env *env;
MDBX_txn *txn, *locktxn; MDBX_txn *txn;
MDBX_envinfo envinfo; MDBX_envinfo envinfo;
MDBX_stat envstat; MDBX_stat envstat;
size_t maxkeysize, userdb_count, skipped_subdb; size_t maxkeysize, userdb_count, skipped_subdb;
@ -836,8 +836,8 @@ int main(int argc, char *argv[]) {
#endif /* !WINDOWS */ #endif /* !WINDOWS */
envname = argv[optind]; envname = argv[optind];
print("Running mdbx_chk for '%s' in %s mode...\n", envname, print("Running mdbx_chk for 'read-%s' in %s mode...\n", envname,
(envflags & MDBX_RDONLY) ? "read-only" : "write-lock"); (envflags & MDBX_RDONLY) ? "read" : "write");
fflush(NULL); fflush(NULL);
rc = mdbx_env_create(&env); rc = mdbx_env_create(&env);
@ -862,14 +862,12 @@ int main(int argc, char *argv[]) {
if (verbose) if (verbose)
print(" - %s mode\n", exclusive ? "monopolistic" : "cooperative"); print(" - %s mode\n", exclusive ? "monopolistic" : "cooperative");
if (!(envflags & MDBX_RDONLY)) { rc = mdbx_txn_begin(env, NULL, envflags & MDBX_RDONLY, &txn);
rc = mdbx_txn_begin(env, NULL, 0, &locktxn);
if (rc) { if (rc) {
error("mdbx_txn_begin(lock-write) failed, error %d %s\n", rc, error("mdbx_txn_begin(read-%s) failed, error %d %s\n",
mdbx_strerror(rc)); (envflags & MDBX_RDONLY) ? "only" : "write", rc, mdbx_strerror(rc));
goto bailout; goto bailout;
} }
}
rc = mdbx_env_get_maxkeysize(env); rc = mdbx_env_get_maxkeysize(env);
if (rc < 0) { if (rc < 0) {
@ -879,13 +877,6 @@ int main(int argc, char *argv[]) {
} }
maxkeysize = rc; maxkeysize = rc;
rc = mdbx_txn_begin(env, NULL, MDBX_RDONLY, &txn);
if (rc) {
error("mdbx_txn_begin(read-only) failed, error %d %s\n", rc,
mdbx_strerror(rc));
goto bailout;
}
rc = mdbx_env_info(env, &envinfo, sizeof(envinfo)); rc = mdbx_env_info(env, &envinfo, sizeof(envinfo));
if (rc) { if (rc) {
error("mdbx_env_info failed, error %d %s\n", rc, mdbx_strerror(rc)); error("mdbx_env_info failed, error %d %s\n", rc, mdbx_strerror(rc));
@ -950,14 +941,14 @@ int main(int argc, char *argv[]) {
if (verbose) if (verbose)
print(" - performs full check recent-txn-id with meta-pages\n"); print(" - performs full check recent-txn-id with meta-pages\n");
problems_meta += check_meta_head(true); problems_meta += check_meta_head(true);
} else if (locktxn) { } else if (mdbx_txn_flags(txn) & MDBX_RDONLY) {
print(" - skip check recent-txn-id with meta-pages (monopolistic or "
"read-write mode only)\n");
} else if (verbose) {
if (verbose) if (verbose)
print(" - performs lite check recent-txn-id with meta-pages (not a " print(" - performs lite check recent-txn-id with meta-pages (not a "
"monopolistic mode)\n"); "monopolistic mode)\n");
problems_meta += check_meta_head(false); problems_meta += check_meta_head(false);
} else if (verbose) {
print(" - skip check recent-txn-id with meta-pages (monopolistic or "
"write-lock mode only)\n");
} }
if (!dont_traversal) { if (!dont_traversal) {
@ -1079,7 +1070,7 @@ int main(int argc, char *argv[]) {
} }
if (problems_maindb == 0 && problems_freedb == 0) { if (problems_maindb == 0 && problems_freedb == 0) {
if (!dont_traversal && (exclusive || locktxn)) { if (!dont_traversal && (exclusive || (envflags & MDBX_RDONLY) == 0)) {
if (walk.pgcount != lastpgno - freedb_pages) { if (walk.pgcount != lastpgno - freedb_pages) {
error("used pages mismatch (%" PRIu64 " != %" PRIu64 ")\n", error("used pages mismatch (%" PRIu64 " != %" PRIu64 ")\n",
walk.pgcount, lastpgno - freedb_pages); walk.pgcount, lastpgno - freedb_pages);
@ -1090,7 +1081,7 @@ int main(int argc, char *argv[]) {
} }
} else if (verbose) { } else if (verbose) {
print(" - skip check used and gc pages (btree-traversal with " print(" - skip check used and gc pages (btree-traversal with "
"monopolistic or write-lock mode only)\n"); "monopolistic or read-write mode only)\n");
} }
if (!process_db(MAIN_DBI, NULL, handle_maindb, true)) { if (!process_db(MAIN_DBI, NULL, handle_maindb, true)) {
@ -1102,8 +1093,6 @@ int main(int argc, char *argv[]) {
bailout: bailout:
if (txn) if (txn)
mdbx_txn_abort(txn); mdbx_txn_abort(txn);
if (locktxn)
mdbx_txn_abort(locktxn);
if (env) if (env)
mdbx_env_close(env); mdbx_env_close(env);
fflush(NULL); fflush(NULL);