mirror of
https://github.com/isar/libmdbx.git
synced 2024-10-30 11:29:19 +08:00
mdbx-tools: несущественный рефакторинг mdbx_load
.
This commit is contained in:
parent
4d3f7e1edc
commit
0e4c6d61a4
235
src/mdbx_load.c
235
src/mdbx_load.c
@ -505,7 +505,7 @@ static int equal_or_greater(const MDBX_val *a, const MDBX_val *b) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
int i, rc;
|
int i, err;
|
||||||
MDBX_env *env = nullptr;
|
MDBX_env *env = nullptr;
|
||||||
MDBX_txn *txn = nullptr;
|
MDBX_txn *txn = nullptr;
|
||||||
MDBX_cursor *mc = nullptr;
|
MDBX_cursor *mc = nullptr;
|
||||||
@ -608,40 +608,45 @@ int main(int argc, char *argv[]) {
|
|||||||
dbuf.iov_len = 4096;
|
dbuf.iov_len = 4096;
|
||||||
dbuf.iov_base = osal_malloc(dbuf.iov_len);
|
dbuf.iov_base = osal_malloc(dbuf.iov_len);
|
||||||
if (!dbuf.iov_base) {
|
if (!dbuf.iov_base) {
|
||||||
rc = MDBX_ENOMEM;
|
err = MDBX_ENOMEM;
|
||||||
error("value-buffer", rc);
|
error("value-buffer", err);
|
||||||
goto env_close;
|
goto bailout;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* read first header for mapsize= */
|
/* read first header for mapsize= */
|
||||||
if (!(mode & NOHDR)) {
|
if (!(mode & NOHDR)) {
|
||||||
rc = readhdr();
|
err = readhdr();
|
||||||
if (unlikely(rc != MDBX_SUCCESS)) {
|
if (unlikely(err != MDBX_SUCCESS)) {
|
||||||
if (rc == EOF)
|
if (err == EOF)
|
||||||
rc = MDBX_ENODATA;
|
err = MDBX_ENODATA;
|
||||||
error("readheader", rc);
|
error("readheader", err);
|
||||||
goto env_close;
|
goto bailout;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = mdbx_env_create(&env);
|
err = mdbx_env_create(&env);
|
||||||
if (unlikely(rc != MDBX_SUCCESS)) {
|
if (unlikely(err != MDBX_SUCCESS)) {
|
||||||
error("mdbx_env_create", rc);
|
error("mdbx_env_create", err);
|
||||||
return EXIT_FAILURE;
|
goto bailout;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = mdbx_env_set_maxdbs(env, 2);
|
||||||
|
if (unlikely(err != MDBX_SUCCESS)) {
|
||||||
|
error("mdbx_env_set_maxdbs", err);
|
||||||
|
goto bailout;
|
||||||
}
|
}
|
||||||
|
|
||||||
mdbx_env_set_maxdbs(env, 2);
|
|
||||||
if (envinfo.mi_maxreaders) {
|
if (envinfo.mi_maxreaders) {
|
||||||
rc = mdbx_env_set_maxreaders(env, envinfo.mi_maxreaders);
|
err = mdbx_env_set_maxreaders(env, envinfo.mi_maxreaders);
|
||||||
if (unlikely(rc != MDBX_SUCCESS)) {
|
if (unlikely(err != MDBX_SUCCESS)) {
|
||||||
error("mdbx_env_set_maxreaders", rc);
|
error("mdbx_env_set_maxreaders", err);
|
||||||
goto env_close;
|
goto bailout;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (envinfo.mi_geo.current | envinfo.mi_mapsize) {
|
if (envinfo.mi_geo.current | envinfo.mi_mapsize) {
|
||||||
if (envinfo.mi_geo.current) {
|
if (envinfo.mi_geo.current) {
|
||||||
rc = mdbx_env_set_geometry(
|
err = mdbx_env_set_geometry(
|
||||||
env, (intptr_t)envinfo.mi_geo.lower, (intptr_t)envinfo.mi_geo.current,
|
env, (intptr_t)envinfo.mi_geo.lower, (intptr_t)envinfo.mi_geo.current,
|
||||||
(intptr_t)envinfo.mi_geo.upper, (intptr_t)envinfo.mi_geo.shrink,
|
(intptr_t)envinfo.mi_geo.upper, (intptr_t)envinfo.mi_geo.shrink,
|
||||||
(intptr_t)envinfo.mi_geo.grow,
|
(intptr_t)envinfo.mi_geo.grow,
|
||||||
@ -654,23 +659,23 @@ int main(int argc, char *argv[]) {
|
|||||||
"Database size is too large for current system (mapsize=%" PRIu64
|
"Database size is too large for current system (mapsize=%" PRIu64
|
||||||
" is great than system-limit %zu)\n",
|
" is great than system-limit %zu)\n",
|
||||||
envinfo.mi_mapsize, (size_t)MAX_MAPSIZE);
|
envinfo.mi_mapsize, (size_t)MAX_MAPSIZE);
|
||||||
goto env_close;
|
goto bailout;
|
||||||
}
|
}
|
||||||
rc = mdbx_env_set_geometry(
|
err = mdbx_env_set_geometry(
|
||||||
env, (intptr_t)envinfo.mi_mapsize, (intptr_t)envinfo.mi_mapsize,
|
env, (intptr_t)envinfo.mi_mapsize, (intptr_t)envinfo.mi_mapsize,
|
||||||
(intptr_t)envinfo.mi_mapsize, 0, 0,
|
(intptr_t)envinfo.mi_mapsize, 0, 0,
|
||||||
envinfo.mi_dxb_pagesize ? (intptr_t)envinfo.mi_dxb_pagesize : -1);
|
envinfo.mi_dxb_pagesize ? (intptr_t)envinfo.mi_dxb_pagesize : -1);
|
||||||
}
|
}
|
||||||
if (unlikely(rc != MDBX_SUCCESS)) {
|
if (unlikely(err != MDBX_SUCCESS)) {
|
||||||
error("mdbx_env_set_geometry", rc);
|
error("mdbx_env_set_geometry", err);
|
||||||
goto env_close;
|
goto bailout;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = mdbx_env_open(env, envname, envflags, 0664);
|
err = mdbx_env_open(env, envname, envflags, 0664);
|
||||||
if (unlikely(rc != MDBX_SUCCESS)) {
|
if (unlikely(err != MDBX_SUCCESS)) {
|
||||||
error("mdbx_env_open", rc);
|
error("mdbx_env_open", err);
|
||||||
goto env_close;
|
goto bailout;
|
||||||
}
|
}
|
||||||
|
|
||||||
kbuf.iov_len = mdbx_env_get_maxvalsize_ex(env, 0) + (size_t)1;
|
kbuf.iov_len = mdbx_env_get_maxvalsize_ex(env, 0) + (size_t)1;
|
||||||
@ -678,54 +683,54 @@ int main(int argc, char *argv[]) {
|
|||||||
if (!quiet)
|
if (!quiet)
|
||||||
fprintf(stderr, "mdbx_env_get_maxkeysize() failed, returns %zu\n",
|
fprintf(stderr, "mdbx_env_get_maxkeysize() failed, returns %zu\n",
|
||||||
kbuf.iov_len);
|
kbuf.iov_len);
|
||||||
goto env_close;
|
goto bailout;
|
||||||
}
|
}
|
||||||
|
|
||||||
kbuf.iov_base = malloc(kbuf.iov_len);
|
kbuf.iov_base = malloc(kbuf.iov_len);
|
||||||
if (!kbuf.iov_base) {
|
if (!kbuf.iov_base) {
|
||||||
rc = MDBX_ENOMEM;
|
err = MDBX_ENOMEM;
|
||||||
error("key-buffer", rc);
|
error("key-buffer", err);
|
||||||
goto env_close;
|
goto bailout;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (rc == MDBX_SUCCESS) {
|
while (err == MDBX_SUCCESS) {
|
||||||
if (user_break) {
|
if (user_break) {
|
||||||
rc = MDBX_EINTR;
|
err = MDBX_EINTR;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = mdbx_txn_begin(env, nullptr, 0, &txn);
|
err = mdbx_txn_begin(env, nullptr, 0, &txn);
|
||||||
if (unlikely(rc != MDBX_SUCCESS)) {
|
if (unlikely(err != MDBX_SUCCESS)) {
|
||||||
error("mdbx_txn_begin", rc);
|
error("mdbx_txn_begin", err);
|
||||||
goto env_close;
|
goto bailout;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mode & GLOBAL) {
|
if (mode & GLOBAL) {
|
||||||
mode -= GLOBAL;
|
mode -= GLOBAL;
|
||||||
if (canary.v | canary.x | canary.y | canary.z) {
|
if (canary.v | canary.x | canary.y | canary.z) {
|
||||||
rc = mdbx_canary_put(txn, &canary);
|
err = mdbx_canary_put(txn, &canary);
|
||||||
if (unlikely(rc != MDBX_SUCCESS)) {
|
if (unlikely(err != MDBX_SUCCESS)) {
|
||||||
error("mdbx_canary_put", rc);
|
error("mdbx_canary_put", err);
|
||||||
goto txn_abort;
|
goto bailout;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *const dbi_name = subname ? subname : "@MAIN";
|
const char *const dbi_name = subname ? subname : "@MAIN";
|
||||||
rc =
|
err =
|
||||||
mdbx_dbi_open_ex(txn, subname, dbi_flags | MDBX_CREATE, &dbi,
|
mdbx_dbi_open_ex(txn, subname, dbi_flags | MDBX_CREATE, &dbi,
|
||||||
(putflags & MDBX_APPEND) ? equal_or_greater : nullptr,
|
(putflags & MDBX_APPEND) ? equal_or_greater : nullptr,
|
||||||
(putflags & MDBX_APPEND) ? equal_or_greater : nullptr);
|
(putflags & MDBX_APPEND) ? equal_or_greater : nullptr);
|
||||||
if (unlikely(rc != MDBX_SUCCESS)) {
|
if (unlikely(err != MDBX_SUCCESS)) {
|
||||||
error("mdbx_dbi_open_ex", rc);
|
error("mdbx_dbi_open_ex", err);
|
||||||
goto txn_abort;
|
goto bailout;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t present_sequence;
|
uint64_t present_sequence;
|
||||||
rc = mdbx_dbi_sequence(txn, dbi, &present_sequence, 0);
|
err = mdbx_dbi_sequence(txn, dbi, &present_sequence, 0);
|
||||||
if (unlikely(rc != MDBX_SUCCESS)) {
|
if (unlikely(err != MDBX_SUCCESS)) {
|
||||||
error("mdbx_dbi_sequence", rc);
|
error("mdbx_dbi_sequence", err);
|
||||||
goto txn_abort;
|
goto bailout;
|
||||||
}
|
}
|
||||||
if (present_sequence > sequence) {
|
if (present_sequence > sequence) {
|
||||||
if (!quiet)
|
if (!quiet)
|
||||||
@ -733,22 +738,22 @@ int main(int argc, char *argv[]) {
|
|||||||
"present sequence for '%s' value (%" PRIu64
|
"present sequence for '%s' value (%" PRIu64
|
||||||
") is greater than loaded (%" PRIu64 ")\n",
|
") is greater than loaded (%" PRIu64 ")\n",
|
||||||
dbi_name, present_sequence, sequence);
|
dbi_name, present_sequence, sequence);
|
||||||
rc = MDBX_RESULT_TRUE;
|
err = MDBX_RESULT_TRUE;
|
||||||
goto txn_abort;
|
goto bailout;
|
||||||
}
|
}
|
||||||
if (present_sequence < sequence) {
|
if (present_sequence < sequence) {
|
||||||
rc = mdbx_dbi_sequence(txn, dbi, nullptr, sequence - present_sequence);
|
err = mdbx_dbi_sequence(txn, dbi, nullptr, sequence - present_sequence);
|
||||||
if (unlikely(rc != MDBX_SUCCESS)) {
|
if (unlikely(err != MDBX_SUCCESS)) {
|
||||||
error("mdbx_dbi_sequence", rc);
|
error("mdbx_dbi_sequence", err);
|
||||||
goto txn_abort;
|
goto bailout;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (purge) {
|
if (purge) {
|
||||||
rc = mdbx_drop(txn, dbi, false);
|
err = mdbx_drop(txn, dbi, false);
|
||||||
if (unlikely(rc != MDBX_SUCCESS)) {
|
if (unlikely(err != MDBX_SUCCESS)) {
|
||||||
error("mdbx_drop", rc);
|
error("mdbx_drop", err);
|
||||||
goto txn_abort;
|
goto bailout;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -756,85 +761,85 @@ int main(int argc, char *argv[]) {
|
|||||||
putflags = (dbi_flags & MDBX_DUPSORT) ? putflags | MDBX_APPENDDUP
|
putflags = (dbi_flags & MDBX_DUPSORT) ? putflags | MDBX_APPENDDUP
|
||||||
: putflags & ~MDBX_APPENDDUP;
|
: putflags & ~MDBX_APPENDDUP;
|
||||||
|
|
||||||
rc = mdbx_cursor_open(txn, dbi, &mc);
|
err = mdbx_cursor_open(txn, dbi, &mc);
|
||||||
if (unlikely(rc != MDBX_SUCCESS)) {
|
if (unlikely(err != MDBX_SUCCESS)) {
|
||||||
error("mdbx_cursor_open", rc);
|
error("mdbx_cursor_open", err);
|
||||||
goto txn_abort;
|
goto bailout;
|
||||||
}
|
}
|
||||||
|
|
||||||
int batch = 0;
|
int batch = 0;
|
||||||
while (rc == MDBX_SUCCESS) {
|
while (err == MDBX_SUCCESS) {
|
||||||
MDBX_val key, data;
|
MDBX_val key, data;
|
||||||
rc = readline(&key, &kbuf);
|
err = readline(&key, &kbuf);
|
||||||
if (rc == EOF)
|
if (err == EOF)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (rc == MDBX_SUCCESS)
|
if (err == MDBX_SUCCESS)
|
||||||
rc = readline(&data, &dbuf);
|
err = readline(&data, &dbuf);
|
||||||
if (rc) {
|
if (err) {
|
||||||
if (!quiet)
|
if (!quiet)
|
||||||
fprintf(stderr, "%s: line %" PRIiSIZE ": failed to read key value\n",
|
fprintf(stderr, "%s: line %" PRIiSIZE ": failed to read key value\n",
|
||||||
prog, lineno);
|
prog, lineno);
|
||||||
goto txn_abort;
|
goto bailout;
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = mdbx_cursor_put(mc, &key, &data, putflags);
|
err = mdbx_cursor_put(mc, &key, &data, putflags);
|
||||||
if (rc == MDBX_KEYEXIST && putflags)
|
if (err == MDBX_KEYEXIST && putflags)
|
||||||
continue;
|
continue;
|
||||||
if (rc == MDBX_BAD_VALSIZE && rescue) {
|
if (err == MDBX_BAD_VALSIZE && rescue) {
|
||||||
if (!quiet)
|
if (!quiet)
|
||||||
fprintf(stderr, "%s: skip line %" PRIiSIZE ": due %s\n", prog, lineno,
|
fprintf(stderr, "%s: skip line %" PRIiSIZE ": due %s\n", prog, lineno,
|
||||||
mdbx_strerror(rc));
|
mdbx_strerror(err));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (unlikely(rc != MDBX_SUCCESS)) {
|
if (unlikely(err != MDBX_SUCCESS)) {
|
||||||
error("mdbx_cursor_put", rc);
|
error("mdbx_cursor_put", err);
|
||||||
goto txn_abort;
|
goto bailout;
|
||||||
}
|
}
|
||||||
batch++;
|
batch++;
|
||||||
|
|
||||||
MDBX_txn_info txn_info;
|
MDBX_txn_info txn_info;
|
||||||
rc = mdbx_txn_info(txn, &txn_info, false);
|
err = mdbx_txn_info(txn, &txn_info, false);
|
||||||
if (unlikely(rc != MDBX_SUCCESS)) {
|
if (unlikely(err != MDBX_SUCCESS)) {
|
||||||
error("mdbx_txn_info", rc);
|
error("mdbx_txn_info", err);
|
||||||
goto txn_abort;
|
goto bailout;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (batch == 10000 || txn_info.txn_space_dirty > MEGABYTE * 256) {
|
if (batch == 10000 || txn_info.txn_space_dirty > MEGABYTE * 256) {
|
||||||
rc = mdbx_txn_commit(txn);
|
err = mdbx_txn_commit(txn);
|
||||||
if (unlikely(rc != MDBX_SUCCESS)) {
|
if (unlikely(err != MDBX_SUCCESS)) {
|
||||||
error("mdbx_txn_commit", rc);
|
error("mdbx_txn_commit", err);
|
||||||
goto env_close;
|
goto bailout;
|
||||||
}
|
}
|
||||||
batch = 0;
|
batch = 0;
|
||||||
|
|
||||||
rc = mdbx_txn_begin(env, nullptr, 0, &txn);
|
err = mdbx_txn_begin(env, nullptr, 0, &txn);
|
||||||
if (unlikely(rc != MDBX_SUCCESS)) {
|
if (unlikely(err != MDBX_SUCCESS)) {
|
||||||
error("mdbx_txn_begin", rc);
|
error("mdbx_txn_begin", err);
|
||||||
goto env_close;
|
goto bailout;
|
||||||
}
|
}
|
||||||
rc = mdbx_cursor_bind(txn, mc, dbi);
|
err = mdbx_cursor_bind(txn, mc, dbi);
|
||||||
if (unlikely(rc != MDBX_SUCCESS)) {
|
if (unlikely(err != MDBX_SUCCESS)) {
|
||||||
error("mdbx_cursor_bind", rc);
|
error("mdbx_cursor_bind", err);
|
||||||
goto txn_abort;
|
goto bailout;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mdbx_cursor_close(mc);
|
mdbx_cursor_close(mc);
|
||||||
mc = nullptr;
|
mc = nullptr;
|
||||||
rc = mdbx_txn_commit(txn);
|
err = mdbx_txn_commit(txn);
|
||||||
txn = nullptr;
|
txn = nullptr;
|
||||||
if (unlikely(rc != MDBX_SUCCESS)) {
|
if (unlikely(err != MDBX_SUCCESS)) {
|
||||||
error("mdbx_txn_commit", rc);
|
error("mdbx_txn_commit", err);
|
||||||
goto env_close;
|
goto bailout;
|
||||||
}
|
}
|
||||||
if (subname) {
|
if (subname) {
|
||||||
assert(dbi != MAIN_DBI);
|
assert(dbi != MAIN_DBI);
|
||||||
rc = mdbx_dbi_close(env, dbi);
|
err = mdbx_dbi_close(env, dbi);
|
||||||
if (unlikely(rc != MDBX_SUCCESS)) {
|
if (unlikely(err != MDBX_SUCCESS)) {
|
||||||
error("mdbx_dbi_close", rc);
|
error("mdbx_dbi_close", err);
|
||||||
goto env_close;
|
goto bailout;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
assert(dbi == MAIN_DBI);
|
assert(dbi == MAIN_DBI);
|
||||||
@ -842,14 +847,14 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
/* try read next header */
|
/* try read next header */
|
||||||
if (!(mode & NOHDR))
|
if (!(mode & NOHDR))
|
||||||
rc = readhdr();
|
err = readhdr();
|
||||||
else if (ferror(stdin) || feof(stdin))
|
else if (ferror(stdin) || feof(stdin))
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (rc) {
|
switch (err) {
|
||||||
case EOF:
|
case EOF:
|
||||||
rc = MDBX_SUCCESS;
|
err = MDBX_SUCCESS;
|
||||||
case MDBX_SUCCESS:
|
case MDBX_SUCCESS:
|
||||||
break;
|
break;
|
||||||
case MDBX_EINTR:
|
case MDBX_EINTR:
|
||||||
@ -857,17 +862,19 @@ int main(int argc, char *argv[]) {
|
|||||||
fprintf(stderr, "Interrupted by signal/user\n");
|
fprintf(stderr, "Interrupted by signal/user\n");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (unlikely(rc != MDBX_SUCCESS))
|
if (unlikely(err != MDBX_SUCCESS))
|
||||||
error("readline", rc);
|
error("readline", err);
|
||||||
}
|
}
|
||||||
|
|
||||||
txn_abort:
|
bailout:
|
||||||
mdbx_cursor_close(mc);
|
if (mc)
|
||||||
mdbx_txn_abort(txn);
|
mdbx_cursor_close(mc);
|
||||||
env_close:
|
if (txn)
|
||||||
mdbx_env_close(env);
|
mdbx_txn_abort(txn);
|
||||||
|
if (env)
|
||||||
|
mdbx_env_close(env);
|
||||||
free(kbuf.iov_base);
|
free(kbuf.iov_base);
|
||||||
free(dbuf.iov_base);
|
free(dbuf.iov_base);
|
||||||
|
|
||||||
return rc ? EXIT_FAILURE : EXIT_SUCCESS;
|
return err ? EXIT_FAILURE : EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user