2019-09-09 18:40:24 +08:00
|
|
|
|
/*
|
2022-01-15 23:50:22 +08:00
|
|
|
|
* Copyright 2017-2022 Leonid Yuriev <leo@yuriev.ru>
|
2019-02-04 03:37:57 +08:00
|
|
|
|
* and other libmdbx authors: please see AUTHORS file.
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
|
* Public License.
|
|
|
|
|
*
|
|
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-11-08 21:17:14 +08:00
|
|
|
|
#include "test.h++"
|
2019-02-04 03:37:57 +08:00
|
|
|
|
|
2021-03-16 01:52:18 +08:00
|
|
|
|
class testcase_append : public testcase {
|
|
|
|
|
public:
|
|
|
|
|
testcase_append(const actor_config &config, const mdbx_pid_t pid)
|
|
|
|
|
: testcase(config, pid) {}
|
|
|
|
|
bool run() override;
|
2021-03-16 06:45:49 +08:00
|
|
|
|
|
|
|
|
|
static bool review_params(actor_params ¶ms) {
|
2021-03-16 07:44:28 +08:00
|
|
|
|
return testcase::review_params(params) && params.make_keygen_linear();
|
2021-03-16 06:45:49 +08:00
|
|
|
|
}
|
2021-03-16 01:52:18 +08:00
|
|
|
|
};
|
|
|
|
|
REGISTER_TESTCASE(append);
|
|
|
|
|
|
2019-02-04 03:37:57 +08:00
|
|
|
|
bool testcase_append::run() {
|
2021-03-27 21:47:08 +08:00
|
|
|
|
const bool reverse = flipcoin();
|
|
|
|
|
const char *const caption = reverse ? "ahead" : "append";
|
|
|
|
|
log_notice("the '%s' scenario is selected", caption);
|
|
|
|
|
|
2019-06-23 20:55:13 +08:00
|
|
|
|
int err = db_open__begin__table_create_open_clean(dbi);
|
|
|
|
|
if (unlikely(err != MDBX_SUCCESS)) {
|
2021-03-27 21:47:08 +08:00
|
|
|
|
log_notice("%s: bailout-prepare due '%s'", caption, mdbx_strerror(err));
|
2019-06-23 20:55:13 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2019-02-04 03:37:57 +08:00
|
|
|
|
|
2020-10-26 05:42:45 +08:00
|
|
|
|
cursor_open(dbi);
|
2019-02-04 03:37:57 +08:00
|
|
|
|
keyvalue_maker.setup(config.params, config.actor_id, 0 /* thread_number */);
|
|
|
|
|
/* LY: тест наполнения таблиц в append-режиме,
|
|
|
|
|
* при котором записи добавляются строго в конец (в порядке сортировки) */
|
2020-10-26 05:42:45 +08:00
|
|
|
|
const MDBX_put_flags_t flags =
|
2021-03-27 21:47:08 +08:00
|
|
|
|
reverse
|
|
|
|
|
? ((config.params.table_flags & MDBX_DUPSORT) ? MDBX_UPSERT
|
|
|
|
|
: MDBX_NOOVERWRITE)
|
|
|
|
|
: ((config.params.table_flags & MDBX_DUPSORT)
|
|
|
|
|
? (flipcoin() ? MDBX_APPEND | MDBX_APPENDDUP : MDBX_APPENDDUP)
|
|
|
|
|
: MDBX_APPEND);
|
2019-02-04 03:37:57 +08:00
|
|
|
|
|
|
|
|
|
key = keygen::alloc(config.params.keylen_max);
|
|
|
|
|
data = keygen::alloc(config.params.datalen_max);
|
|
|
|
|
|
|
|
|
|
simple_checksum inserted_checksum;
|
|
|
|
|
uint64_t inserted_number = 0;
|
|
|
|
|
uint64_t serial_count = 0;
|
2021-03-27 21:47:08 +08:00
|
|
|
|
if (reverse)
|
|
|
|
|
keyvalue_maker.seek2end(serial_count);
|
2019-06-23 20:55:13 +08:00
|
|
|
|
|
2019-02-04 03:37:57 +08:00
|
|
|
|
unsigned txn_nops = 0;
|
2020-10-20 20:42:50 +08:00
|
|
|
|
uint64_t committed_inserted_number = inserted_number;
|
|
|
|
|
simple_checksum committed_inserted_checksum = inserted_checksum;
|
2019-02-04 03:37:57 +08:00
|
|
|
|
while (should_continue()) {
|
|
|
|
|
const keygen::serial_t serial = serial_count;
|
2020-10-26 05:42:45 +08:00
|
|
|
|
const bool turn_key = (config.params.table_flags & MDBX_DUPSORT) == 0 ||
|
|
|
|
|
flipcoin_n(config.params.keygen.split);
|
2021-03-27 21:47:08 +08:00
|
|
|
|
if (turn_key
|
|
|
|
|
? !keyvalue_maker.increment_key_part(serial_count, reverse ? -1 : 1)
|
|
|
|
|
: !keyvalue_maker.increment(serial_count, reverse ? -1 : 1)) {
|
2019-02-04 03:37:57 +08:00
|
|
|
|
// дошли до границы пространства ключей
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-27 21:47:08 +08:00
|
|
|
|
log_trace("%s: insert-a %" PRIu64, caption, serial);
|
2019-11-25 00:04:21 +08:00
|
|
|
|
generate_pair(serial);
|
2020-10-26 05:42:45 +08:00
|
|
|
|
// keygen::log_pair(logging::verbose, "append.", key, data);
|
|
|
|
|
|
2021-03-27 21:47:08 +08:00
|
|
|
|
bool expect_key_mismatch = false;
|
|
|
|
|
if (flags & (MDBX_APPEND | MDBX_APPENDDUP)) {
|
|
|
|
|
MDBX_val ge_key = key->value;
|
|
|
|
|
MDBX_val ge_data = data->value;
|
|
|
|
|
err = mdbx_get_equal_or_great(txn_guard.get(), dbi, &ge_key, &ge_data);
|
|
|
|
|
|
|
|
|
|
if (err == MDBX_SUCCESS /* exact match */) {
|
|
|
|
|
expect_key_mismatch = true;
|
|
|
|
|
assert(inserted_number > 0);
|
|
|
|
|
assert(mdbx_cmp(txn_guard.get(), dbi, &key->value, &ge_key) == 0);
|
|
|
|
|
assert((config.params.table_flags & MDBX_DUPSORT) == 0 ||
|
|
|
|
|
mdbx_dcmp(txn_guard.get(), dbi, &data->value, &ge_data) == 0);
|
|
|
|
|
assert(inserted_number > 0);
|
|
|
|
|
} else if (err == MDBX_RESULT_TRUE /* have key-value pair great than */) {
|
|
|
|
|
assert(mdbx_cmp(txn_guard.get(), dbi, &key->value, &ge_key) < 0 ||
|
|
|
|
|
((config.params.table_flags & MDBX_DUPSORT) &&
|
|
|
|
|
mdbx_cmp(txn_guard.get(), dbi, &key->value, &ge_key) == 0 &&
|
|
|
|
|
mdbx_dcmp(txn_guard.get(), dbi, &data->value, &ge_data) < 0));
|
|
|
|
|
switch (int(flags)) {
|
|
|
|
|
default:
|
|
|
|
|
abort();
|
2020-10-26 05:42:45 +08:00
|
|
|
|
#if CONSTEXPR_ENUM_FLAGS_OPERATIONS
|
2021-03-27 21:47:08 +08:00
|
|
|
|
case MDBX_APPEND | MDBX_APPENDDUP:
|
2020-10-26 05:42:45 +08:00
|
|
|
|
#else
|
2021-03-27 21:47:08 +08:00
|
|
|
|
case int(MDBX_APPEND) | int(MDBX_APPENDDUP):
|
2020-10-26 05:42:45 +08:00
|
|
|
|
#endif
|
2021-03-27 21:47:08 +08:00
|
|
|
|
assert((config.params.table_flags & MDBX_DUPSORT) != 0);
|
|
|
|
|
__fallthrough;
|
|
|
|
|
// fall through
|
|
|
|
|
case MDBX_APPEND:
|
|
|
|
|
expect_key_mismatch = true;
|
|
|
|
|
break;
|
|
|
|
|
case MDBX_APPENDDUP:
|
|
|
|
|
assert((config.params.table_flags & MDBX_DUPSORT) != 0);
|
|
|
|
|
expect_key_mismatch =
|
|
|
|
|
mdbx_cmp(txn_guard.get(), dbi, &key->value, &ge_key) == 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else if (err == MDBX_NOTFOUND /* all pair are less than */) {
|
|
|
|
|
switch (int(flags)) {
|
|
|
|
|
default:
|
|
|
|
|
abort();
|
|
|
|
|
case MDBX_APPENDDUP:
|
2020-10-26 05:42:45 +08:00
|
|
|
|
#if CONSTEXPR_ENUM_FLAGS_OPERATIONS
|
2021-03-27 21:47:08 +08:00
|
|
|
|
case MDBX_APPEND | MDBX_APPENDDUP:
|
2020-10-26 05:42:45 +08:00
|
|
|
|
#else
|
2021-03-27 21:47:08 +08:00
|
|
|
|
case int(MDBX_APPEND) | int(MDBX_APPENDDUP):
|
2020-10-26 05:42:45 +08:00
|
|
|
|
#endif
|
2021-03-27 21:47:08 +08:00
|
|
|
|
assert((config.params.table_flags & MDBX_DUPSORT) != 0);
|
|
|
|
|
__fallthrough;
|
|
|
|
|
// fall through
|
|
|
|
|
case MDBX_APPEND:
|
|
|
|
|
expect_key_mismatch = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else
|
|
|
|
|
failure_perror("mdbx_get_equal_or_great()", err);
|
|
|
|
|
|
|
|
|
|
assert(!expect_key_mismatch);
|
|
|
|
|
}
|
2019-02-04 03:37:57 +08:00
|
|
|
|
|
2020-10-26 05:42:45 +08:00
|
|
|
|
err = mdbx_cursor_put(cursor_guard.get(), &key->value, &data->value, flags);
|
2019-06-23 20:55:13 +08:00
|
|
|
|
if (err == MDBX_MAP_FULL && config.params.ignore_dbfull) {
|
2021-03-27 21:47:08 +08:00
|
|
|
|
log_notice("%s: bailout-insert due '%s'", caption, mdbx_strerror(err));
|
2019-06-23 20:55:13 +08:00
|
|
|
|
txn_end(true);
|
2020-10-20 20:42:50 +08:00
|
|
|
|
inserted_number = committed_inserted_number;
|
|
|
|
|
inserted_checksum = committed_inserted_checksum;
|
2019-06-23 20:55:13 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-26 05:42:45 +08:00
|
|
|
|
if (!expect_key_mismatch) {
|
2019-06-22 18:13:15 +08:00
|
|
|
|
if (unlikely(err != MDBX_SUCCESS))
|
2021-03-27 21:47:08 +08:00
|
|
|
|
failure_perror("mdbx_cursor_put(insert-a)", err);
|
2019-02-04 03:37:57 +08:00
|
|
|
|
++inserted_number;
|
|
|
|
|
inserted_checksum.push((uint32_t)inserted_number, key->value);
|
|
|
|
|
inserted_checksum.push(10639, data->value);
|
2020-10-26 05:42:45 +08:00
|
|
|
|
} else if (unlikely(err != MDBX_EKEYMISMATCH))
|
2021-03-27 21:47:08 +08:00
|
|
|
|
failure_perror("mdbx_cursor_put(insert-a) != MDBX_EKEYMISMATCH", err);
|
2019-02-04 03:37:57 +08:00
|
|
|
|
|
|
|
|
|
if (++txn_nops >= config.params.batch_write) {
|
2019-06-23 20:55:13 +08:00
|
|
|
|
err = breakable_restart();
|
|
|
|
|
if (unlikely(err != MDBX_SUCCESS)) {
|
2021-03-27 21:47:08 +08:00
|
|
|
|
log_notice("%s: bailout-commit due '%s'", caption, mdbx_strerror(err));
|
2020-10-20 20:42:50 +08:00
|
|
|
|
inserted_number = committed_inserted_number;
|
|
|
|
|
inserted_checksum = committed_inserted_checksum;
|
2019-06-23 20:55:13 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
2020-10-20 20:42:50 +08:00
|
|
|
|
committed_inserted_number = inserted_number;
|
|
|
|
|
committed_inserted_checksum = inserted_checksum;
|
2019-02-04 03:37:57 +08:00
|
|
|
|
txn_nops = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
report(1);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-23 20:55:13 +08:00
|
|
|
|
if (txn_guard) {
|
|
|
|
|
err = breakable_commit();
|
|
|
|
|
if (unlikely(err != MDBX_SUCCESS)) {
|
2021-03-27 21:47:08 +08:00
|
|
|
|
log_notice("%s: bailout-commit due '%s'", caption, mdbx_strerror(err));
|
2020-10-20 20:42:50 +08:00
|
|
|
|
inserted_number = committed_inserted_number;
|
|
|
|
|
inserted_checksum = committed_inserted_checksum;
|
2019-06-23 20:55:13 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-04 03:37:57 +08:00
|
|
|
|
//----------------------------------------------------------------------------
|
2019-06-23 20:55:13 +08:00
|
|
|
|
txn_begin(true);
|
2020-10-26 05:42:45 +08:00
|
|
|
|
cursor_renew();
|
2019-02-04 03:37:57 +08:00
|
|
|
|
|
|
|
|
|
MDBX_val check_key, check_data;
|
2021-03-27 21:47:08 +08:00
|
|
|
|
err = mdbx_cursor_get(cursor_guard.get(), &check_key, &check_data,
|
|
|
|
|
reverse ? MDBX_LAST : MDBX_FIRST);
|
2019-06-23 20:55:13 +08:00
|
|
|
|
if (likely(inserted_number)) {
|
|
|
|
|
if (unlikely(err != MDBX_SUCCESS))
|
|
|
|
|
failure_perror("mdbx_cursor_get(MDBX_FIRST)", err);
|
|
|
|
|
}
|
2019-02-04 03:37:57 +08:00
|
|
|
|
|
|
|
|
|
simple_checksum read_checksum;
|
|
|
|
|
uint64_t read_count = 0;
|
2019-06-22 18:13:15 +08:00
|
|
|
|
while (err == MDBX_SUCCESS) {
|
2019-02-04 03:37:57 +08:00
|
|
|
|
++read_count;
|
|
|
|
|
read_checksum.push((uint32_t)read_count, check_key);
|
|
|
|
|
read_checksum.push(10639, check_data);
|
|
|
|
|
|
2021-03-27 21:47:08 +08:00
|
|
|
|
err = mdbx_cursor_get(cursor_guard.get(), &check_key, &check_data,
|
|
|
|
|
reverse ? MDBX_PREV : MDBX_NEXT);
|
2019-02-04 03:37:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-22 18:13:15 +08:00
|
|
|
|
if (unlikely(err != MDBX_NOTFOUND))
|
|
|
|
|
failure_perror("mdbx_cursor_get(MDBX_NEXT) != EOF", err);
|
2019-02-04 03:37:57 +08:00
|
|
|
|
|
|
|
|
|
if (unlikely(read_count != inserted_number))
|
|
|
|
|
failure("read_count(%" PRIu64 ") != inserted_number(%" PRIu64 ")",
|
|
|
|
|
read_count, inserted_number);
|
|
|
|
|
|
|
|
|
|
if (unlikely(read_checksum.value != inserted_checksum.value))
|
|
|
|
|
failure("read_checksum(0x%016" PRIu64 ") "
|
|
|
|
|
"!= inserted_checksum(0x%016" PRIu64 ")",
|
|
|
|
|
read_checksum.value, inserted_checksum.value);
|
|
|
|
|
|
|
|
|
|
cursor_close();
|
2019-06-23 20:55:13 +08:00
|
|
|
|
txn_end(true);
|
2019-02-04 03:37:57 +08:00
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
if (dbi) {
|
|
|
|
|
if (config.params.drop_table && !mode_readonly()) {
|
|
|
|
|
txn_begin(false);
|
|
|
|
|
db_table_drop(dbi);
|
2019-06-23 20:55:13 +08:00
|
|
|
|
err = breakable_commit();
|
|
|
|
|
if (unlikely(err != MDBX_SUCCESS)) {
|
2021-03-27 21:47:08 +08:00
|
|
|
|
log_notice("%s: bailout-clean due '%s'", caption, mdbx_strerror(err));
|
2019-06-23 20:55:13 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2019-02-04 03:37:57 +08:00
|
|
|
|
} else
|
|
|
|
|
db_table_close(dbi);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|