mdbx: using enum instead of #define for flags/modes.

Resolve https://github.com/erthink/libmdbx/issues/108

Change-Id: I45897300375d2b5b9361aaba81dadcf9801fe3cf
This commit is contained in:
Leonid Yuriev 2020-07-08 02:26:46 +03:00
parent 2e0d2e65af
commit 9720ed39f5
8 changed files with 955 additions and 743 deletions

1505
mdbx.h

File diff suppressed because it is too large Load Diff

View File

@ -9254,7 +9254,7 @@ int __cold mdbx_env_set_mapsize(MDBX_env *env, size_t size) {
} }
int __cold mdbx_env_set_maxdbs(MDBX_env *env, MDBX_dbi dbs) { int __cold mdbx_env_set_maxdbs(MDBX_env *env, MDBX_dbi dbs) {
if (unlikely(dbs > MAX_DBI)) if (unlikely(dbs > MDBX_MAX_DBI))
return MDBX_EINVAL; return MDBX_EINVAL;
if (unlikely(!env)) if (unlikely(!env))
@ -10028,26 +10028,6 @@ __cold int mdbx_is_readahead_reasonable(size_t volume, intptr_t redundancy) {
: MDBX_RESULT_TRUE; : MDBX_RESULT_TRUE;
} }
/* Only a subset of the mdbx_env flags can be changed
* at runtime. Changing other flags requires closing the
* environment and re-opening it with the new flags. */
#define ENV_CHANGEABLE_FLAGS \
(MDBX_SAFE_NOSYNC | MDBX_NOMETASYNC | MDBX_MAPASYNC | MDBX_NOMEMINIT | \
MDBX_COALESCE | MDBX_PAGEPERTURB | MDBX_ACCEDE)
#define ENV_CHANGELESS_FLAGS \
(MDBX_NOSUBDIR | MDBX_RDONLY | MDBX_WRITEMAP | MDBX_NOTLS | MDBX_NORDAHEAD | \
MDBX_LIFORECLAIM | MDBX_EXCLUSIVE)
#define ENV_USABLE_FLAGS (ENV_CHANGEABLE_FLAGS | ENV_CHANGELESS_FLAGS)
#if ENV_INTERNAL_FLAGS & ENV_USABLE_FLAGS
#error "Oops, some flags overlapped or wrong"
#endif
#if (MDBX_ACCEDE | MDBX_CREATE) != ((DB_USABLE_FLAGS | DB_INTERNAL_FLAGS) & \
(ENV_USABLE_FLAGS | ENV_INTERNAL_FLAGS))
#error "Oops, some flags overlapped or wrong"
#endif
/* Merge flags and avoid false MDBX_UTTERLY_NOSYNC */ /* Merge flags and avoid false MDBX_UTTERLY_NOSYNC */
static uint32_t merge_flags(const uint32_t a, const uint32_t b) { static uint32_t merge_flags(const uint32_t a, const uint32_t b) {
uint32_t r = a | b; uint32_t r = a | b;

View File

@ -195,10 +195,6 @@ extern LIBMDBX_API const char *const mdbx_sourcery_anchor;
#define MAIN_DBI 1 #define MAIN_DBI 1
/* Number of DBs in metapage (free and main) - also hardcoded elsewhere */ /* Number of DBs in metapage (free and main) - also hardcoded elsewhere */
#define CORE_DBS 2 #define CORE_DBS 2
#define MAX_DBI (INT16_MAX - CORE_DBS)
#if MAX_DBI != MDBX_MAX_DBI
#error "Oops, MAX_DBI != MDBX_MAX_DBI"
#endif
/* Number of meta pages - also hardcoded elsewhere */ /* Number of meta pages - also hardcoded elsewhere */
#define NUM_METAS 3 #define NUM_METAS 3
@ -1379,3 +1375,25 @@ static __pure_function __always_inline __maybe_unused size_t
ceil_powerof2(size_t value, size_t granularity) { ceil_powerof2(size_t value, size_t granularity) {
return floor_powerof2(value + granularity - 1, granularity); return floor_powerof2(value + granularity - 1, granularity);
} }
/* Only a subset of the mdbx_env flags can be changed
* at runtime. Changing other flags requires closing the
* environment and re-opening it with the new flags. */
#define ENV_CHANGEABLE_FLAGS \
(MDBX_SAFE_NOSYNC | MDBX_NOMETASYNC | MDBX_MAPASYNC | MDBX_NOMEMINIT | \
MDBX_COALESCE | MDBX_PAGEPERTURB | MDBX_ACCEDE)
#define ENV_CHANGELESS_FLAGS \
(MDBX_NOSUBDIR | MDBX_RDONLY | MDBX_WRITEMAP | MDBX_NOTLS | MDBX_NORDAHEAD | \
MDBX_LIFORECLAIM | MDBX_EXCLUSIVE)
#define ENV_USABLE_FLAGS (ENV_CHANGEABLE_FLAGS | ENV_CHANGELESS_FLAGS)
static __maybe_unused void static_checks(void) {
STATIC_ASSERT_MSG(INT16_MAX - CORE_DBS == MDBX_MAX_DBI,
"Oops, MDBX_MAX_DBI or CORE_DBS?");
STATIC_ASSERT_MSG((MDBX_ACCEDE | MDBX_CREATE) ==
((DB_USABLE_FLAGS | DB_INTERNAL_FLAGS) &
(ENV_USABLE_FLAGS | ENV_INTERNAL_FLAGS)),
"Oops, some flags overlapped or wrong");
STATIC_ASSERT_MSG((ENV_INTERNAL_FLAGS & ENV_USABLE_FLAGS) == 0,
"Oops, some flags overlapped or wrong");
}

View File

@ -77,7 +77,8 @@ struct {
short *pagemap; short *pagemap;
uint64_t total_payload_bytes; uint64_t total_payload_bytes;
uint64_t pgcount; uint64_t pgcount;
walk_dbi_t dbi[MAX_DBI + CORE_DBS + /* account pseudo-entry for meta */ 1]; walk_dbi_t
dbi[MDBX_MAX_DBI + CORE_DBS + /* account pseudo-entry for meta */ 1];
} walk; } walk;
#define dbi_free walk.dbi[FREE_DBI] #define dbi_free walk.dbi[FREE_DBI]
@ -1032,7 +1033,7 @@ int main(int argc, char *argv[]) {
return rc < 0 ? EXIT_FAILURE_MDB : EXIT_FAILURE_SYS; return rc < 0 ? EXIT_FAILURE_MDB : EXIT_FAILURE_SYS;
} }
rc = mdbx_env_set_maxdbs(env, MAX_DBI); rc = mdbx_env_set_maxdbs(env, MDBX_MAX_DBI);
if (rc) { if (rc) {
error("mdbx_env_set_maxdbs failed, error %d %s\n", rc, mdbx_strerror(rc)); error("mdbx_env_set_maxdbs failed, error %d %s\n", rc, mdbx_strerror(rc));
goto bailout; goto bailout;

View File

@ -80,8 +80,10 @@ bool parse_option(int argc, char *const argv[], int &narg, const char *option,
return true; return true;
} }
bool parse_option(int argc, char *const argv[], int &narg, const char *option, template <>
unsigned &mask, const option_verb *verbs) { bool parse_option<unsigned>(int argc, char *const argv[], int &narg,
const char *option, unsigned &mask,
const option_verb *verbs) {
const char *list; const char *list;
if (!parse_option(argc, argv, narg, option, &list)) if (!parse_option(argc, argv, narg, option, &list))
return false; return false;
@ -213,7 +215,7 @@ bool parse_option(int argc, char *const argv[], int &narg, const char *option,
if (!parse_option(argc, argv, narg, option, huge, scale, minval, maxval, if (!parse_option(argc, argv, narg, option, huge, scale, minval, maxval,
default_value)) default_value))
return false; return false;
value = (unsigned)huge; value = unsigned(huge);
return true; return true;
} }
@ -225,18 +227,18 @@ bool parse_option(int argc, char *const argv[], int &narg, const char *option,
if (!parse_option(argc, argv, narg, option, huge, no_scale, minval, maxval, if (!parse_option(argc, argv, narg, option, huge, no_scale, minval, maxval,
default_value)) default_value))
return false; return false;
value = (uint8_t)huge; value = uint8_t(huge);
return true; return true;
} }
bool parse_option(int argc, char *const argv[], int &narg, const char *option, bool parse_option(int argc, char *const argv[], int &narg, const char *option,
int64_t &value, const int64_t minval, const int64_t maxval, int64_t &value, const int64_t minval, const int64_t maxval,
const int64_t default_value) { const int64_t default_value) {
uint64_t proxy = (uint64_t)value; uint64_t proxy = uint64_t(value);
if (parse_option(argc, argv, narg, option, proxy, config::binary, if (parse_option(argc, argv, narg, option, proxy, config::binary,
(uint64_t)minval, (uint64_t)maxval, uint64_t(minval), uint64_t(maxval),
(uint64_t)default_value)) { uint64_t(default_value))) {
value = (int64_t)proxy; value = int64_t(proxy);
return true; return true;
} }
return false; return false;
@ -245,11 +247,11 @@ bool parse_option(int argc, char *const argv[], int &narg, const char *option,
bool parse_option(int argc, char *const argv[], int &narg, const char *option, bool parse_option(int argc, char *const argv[], int &narg, const char *option,
int32_t &value, const int32_t minval, const int32_t maxval, int32_t &value, const int32_t minval, const int32_t maxval,
const int32_t default_value) { const int32_t default_value) {
uint64_t proxy = (uint64_t)value; uint64_t proxy = uint64_t(value);
if (parse_option(argc, argv, narg, option, proxy, config::binary, if (parse_option(argc, argv, narg, option, proxy, config::binary,
(uint64_t)minval, (uint64_t)maxval, uint64_t(minval), uint64_t(maxval),
(uint64_t)default_value)) { uint64_t(default_value))) {
value = (int32_t)proxy; value = int32_t(proxy);
return true; return true;
} }
return false; return false;
@ -294,29 +296,30 @@ bool parse_option(int argc, char *const argv[], int &narg, const char *option,
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
const struct option_verb mode_bits[] = {{"rdonly", MDBX_RDONLY}, const struct option_verb mode_bits[] = {
{"mapasync", MDBX_MAPASYNC}, {"rdonly", unsigned(MDBX_RDONLY)},
{"nosync-utterly", MDBX_UTTERLY_NOSYNC}, {"mapasync", unsigned(MDBX_MAPASYNC)},
{"nosubdir", MDBX_NOSUBDIR}, {"nosync-utterly", unsigned(MDBX_UTTERLY_NOSYNC)},
{"nosync-safe", MDBX_SAFE_NOSYNC}, {"nosubdir", unsigned(MDBX_NOSUBDIR)},
{"nometasync", MDBX_NOMETASYNC}, {"nosync-safe", unsigned(MDBX_SAFE_NOSYNC)},
{"writemap", MDBX_WRITEMAP}, {"nometasync", unsigned(MDBX_NOMETASYNC)},
{"notls", MDBX_NOTLS}, {"writemap", unsigned(MDBX_WRITEMAP)},
{"nordahead", MDBX_NORDAHEAD}, {"notls", unsigned(MDBX_NOTLS)},
{"nomeminit", MDBX_NOMEMINIT}, {"nordahead", unsigned(MDBX_NORDAHEAD)},
{"coalesce", MDBX_COALESCE}, {"nomeminit", unsigned(MDBX_NOMEMINIT)},
{"lifo", MDBX_LIFORECLAIM}, {"coalesce", unsigned(MDBX_COALESCE)},
{"perturb", MDBX_PAGEPERTURB}, {"lifo", unsigned(MDBX_LIFORECLAIM)},
{"accede", MDBX_ACCEDE}, {"perturb", unsigned(MDBX_PAGEPERTURB)},
{nullptr, 0}}; {"accede", unsigned(MDBX_ACCEDE)},
{nullptr, 0}};
const struct option_verb table_bits[] = { const struct option_verb table_bits[] = {
{"key.reverse", MDBX_REVERSEKEY}, {"key.reverse", unsigned(MDBX_REVERSEKEY)},
{"key.integer", MDBX_INTEGERKEY}, {"key.integer", unsigned(MDBX_INTEGERKEY)},
{"data.integer", MDBX_INTEGERDUP | MDBX_DUPFIXED | MDBX_DUPSORT}, {"data.integer", unsigned(MDBX_INTEGERDUP | MDBX_DUPFIXED | MDBX_DUPSORT)},
{"data.fixed", MDBX_DUPFIXED | MDBX_DUPSORT}, {"data.fixed", unsigned(MDBX_DUPFIXED | MDBX_DUPSORT)},
{"data.reverse", MDBX_REVERSEDUP | MDBX_DUPSORT}, {"data.reverse", unsigned(MDBX_REVERSEDUP | MDBX_DUPSORT)},
{"data.dups", MDBX_DUPSORT}, {"data.dups", unsigned(MDBX_DUPSORT)},
{nullptr, 0}}; {nullptr, 0}};
static void dump_verbs(const char *caption, size_t bits, static void dump_verbs(const char *caption, size_t bits,
@ -590,7 +593,7 @@ unsigned actor_params::mdbx_keylen_min() const {
} }
unsigned actor_params::mdbx_keylen_max() const { unsigned actor_params::mdbx_keylen_max() const {
return (unsigned)mdbx_limits_keysize_max(pagesize, table_flags); return unsigned(mdbx_limits_keysize_max(pagesize, table_flags));
} }
unsigned actor_params::mdbx_datalen_min() const { unsigned actor_params::mdbx_datalen_min() const {
@ -598,6 +601,6 @@ unsigned actor_params::mdbx_datalen_min() const {
} }
unsigned actor_params::mdbx_datalen_max() const { unsigned actor_params::mdbx_datalen_max() const {
return std::min((unsigned)UINT16_MAX, return std::min(unsigned(UINT16_MAX),
(unsigned)mdbx_limits_valsize_max(pagesize, table_flags)); unsigned(mdbx_limits_valsize_max(pagesize, table_flags)));
} }

View File

@ -79,8 +79,22 @@ struct option_verb {
unsigned mask; unsigned mask;
}; };
template <typename MASK>
bool parse_option(int argc, char *const argv[], int &narg, const char *option, bool parse_option(int argc, char *const argv[], int &narg, const char *option,
unsigned &mask, const option_verb *verbs); MASK &mask, const option_verb *verbs) {
static_assert(sizeof(MASK) <= sizeof(unsigned), "WTF?");
unsigned u = unsigned(mask);
if (parse_option<unsigned>(argc, argv, narg, option, u, verbs)) {
mask = MASK(u);
return true;
}
return false;
}
template <>
bool parse_option<unsigned>(int argc, char *const argv[], int &narg,
const char *option, unsigned &mask,
const option_verb *verbs);
bool parse_option(int argc, char *const argv[], int &narg, const char *option, bool parse_option(int argc, char *const argv[], int &narg, const char *option,
uint64_t &value, const scale_mode scale, uint64_t &value, const scale_mode scale,
@ -236,8 +250,8 @@ struct keygen_params_pod {
}; };
struct actor_params_pod { struct actor_params_pod {
unsigned mode_flags{0}; MDBX_env_flags_t mode_flags{MDBX_ENV_DEFAULTS};
unsigned table_flags{0}; MDBX_db_flags_t table_flags{MDBX_DB_DEFAULTS};
intptr_t size_lower{0}; intptr_t size_lower{0};
intptr_t size_now{0}; intptr_t size_now{0};
intptr_t size_upper{0}; intptr_t size_upper{0};

View File

@ -78,11 +78,12 @@ void __hot maker::pair(serial_t serial, const buffer &key, buffer &value,
assert(mapping.mesh <= mapping.width); assert(mapping.mesh <= mapping.width);
assert(mapping.rotate <= mapping.width); assert(mapping.rotate <= mapping.width);
assert(mapping.offset <= mask(mapping.width)); assert(mapping.offset <= mask(mapping.width));
assert( assert(!(key_essentials.flags &
!(key_essentials.flags & ~(essentials::prng_fill_flag | MDBX_INTEGERKEY | ~(essentials::prng_fill_flag |
MDBX_REVERSEKEY | MDBX_DUPSORT))); unsigned(MDBX_INTEGERKEY | MDBX_REVERSEKEY | MDBX_DUPSORT))));
assert(!(value_essentials.flags & assert(!(value_essentials.flags &
~(essentials::prng_fill_flag | MDBX_INTEGERDUP | MDBX_REVERSEDUP))); ~(essentials::prng_fill_flag |
unsigned(MDBX_INTEGERDUP | MDBX_REVERSEDUP))));
log_trace("keygen-pair: serial %" PRIu64 ", data-age %" PRIu64, serial, log_trace("keygen-pair: serial %" PRIu64 ", data-age %" PRIu64, serial,
value_age); value_age);
@ -197,8 +198,17 @@ void __hot maker::pair(serial_t serial, const buffer &key, buffer &value,
void maker::setup(const config::actor_params_pod &actor, unsigned actor_id, void maker::setup(const config::actor_params_pod &actor, unsigned actor_id,
unsigned thread_number) { unsigned thread_number) {
#if defined(_MSC_VER) && _MSC_VER < 1900
assert(unsigned(MDBX_INTEGERKEY | MDBX_REVERSEKEY | MDBX_DUPSORT |
MDBX_INTEGERDUP | MDBX_REVERSEDUP) < UINT16_MAX);
#else
static_assert(unsigned(MDBX_INTEGERKEY | MDBX_REVERSEKEY | MDBX_DUPSORT |
MDBX_INTEGERDUP | MDBX_REVERSEDUP) < UINT16_MAX,
"WTF?");
#endif
key_essentials.flags = key_essentials.flags =
actor.table_flags & (MDBX_INTEGERKEY | MDBX_REVERSEKEY | MDBX_DUPSORT); actor.table_flags &
uint16_t(MDBX_INTEGERKEY | MDBX_REVERSEKEY | MDBX_DUPSORT);
assert(actor.keylen_min <= UINT16_MAX); assert(actor.keylen_min <= UINT16_MAX);
key_essentials.minlen = (uint16_t)actor.keylen_min; key_essentials.minlen = (uint16_t)actor.keylen_min;
assert(actor.keylen_max <= UINT32_MAX); assert(actor.keylen_max <= UINT32_MAX);
@ -207,7 +217,7 @@ void maker::setup(const config::actor_params_pod &actor, unsigned actor_id,
(uint32_t)mdbx_limits_keysize_max(actor.pagesize, key_essentials.flags)); (uint32_t)mdbx_limits_keysize_max(actor.pagesize, key_essentials.flags));
value_essentials.flags = value_essentials.flags =
actor.table_flags & (MDBX_INTEGERDUP | MDBX_REVERSEDUP); actor.table_flags & uint16_t(MDBX_INTEGERDUP | MDBX_REVERSEDUP);
assert(actor.datalen_min <= UINT16_MAX); assert(actor.datalen_min <= UINT16_MAX);
value_essentials.minlen = (uint16_t)actor.datalen_min; value_essentials.minlen = (uint16_t)actor.datalen_min;
assert(actor.datalen_max <= UINT32_MAX); assert(actor.datalen_max <= UINT32_MAX);
@ -305,10 +315,17 @@ void __hot maker::mk_begin(const serial_t serial, const essentials &params,
void __hot maker::mk_continue(const serial_t serial, const essentials &params, void __hot maker::mk_continue(const serial_t serial, const essentials &params,
result &out) { result &out) {
static_assert((essentials::prng_fill_flag & #if defined(_MSC_VER) && _MSC_VER < 1900
(MDBX_DUPSORT | MDBX_DUPFIXED | MDBX_INTEGERKEY | assert((essentials::prng_fill_flag &
MDBX_INTEGERDUP | MDBX_REVERSEKEY | MDBX_REVERSEDUP)) == 0, unsigned(MDBX_DUPSORT | MDBX_DUPFIXED | MDBX_INTEGERKEY |
"WTF?"); MDBX_INTEGERDUP | MDBX_REVERSEKEY | MDBX_REVERSEDUP)) == 0);
#else
static_assert(
(essentials::prng_fill_flag &
unsigned(MDBX_DUPSORT | MDBX_DUPFIXED | MDBX_INTEGERKEY |
MDBX_INTEGERDUP | MDBX_REVERSEKEY | MDBX_REVERSEDUP)) == 0,
"WTF?");
#endif
out.value.iov_base = out.bytes; out.value.iov_base = out.bytes;
if (params.flags & (MDBX_INTEGERKEY | MDBX_INTEGERDUP)) { if (params.flags & (MDBX_INTEGERKEY | MDBX_INTEGERDUP)) {
assert(params.maxlen == params.minlen); assert(params.maxlen == params.minlen);
@ -317,7 +334,7 @@ void __hot maker::mk_continue(const serial_t serial, const essentials &params,
out.u64 = serial; out.u64 = serial;
else else
out.u32 = (uint32_t)serial; out.u32 = (uint32_t)serial;
} else if (params.flags & (MDBX_REVERSEKEY | MDBX_REVERSEDUP)) { } else if (params.flags & unsigned(MDBX_REVERSEKEY | MDBX_REVERSEDUP)) {
if (out.value.iov_len > 8) { if (out.value.iov_len > 8) {
if (params.flags & essentials::prng_fill_flag) { if (params.flags & essentials::prng_fill_flag) {
uint64_t state = serial ^ UINT64_C(0x41803711c9b75f19); uint64_t state = serial ^ UINT64_C(0x41803711c9b75f19);

View File

@ -37,8 +37,8 @@ void __noreturn failure_perror(const char *what, int errnum) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void mdbx_logger(int priority, const char *function, int line, static void mdbx_logger(MDBX_log_level_t priority, const char *function,
const char *msg, va_list args) { int line, const char *msg, va_list args) {
if (!function) if (!function)
function = "unknown"; function = "unknown";
@ -60,7 +60,7 @@ static FILE *last;
void setlevel(loglevel priority) { void setlevel(loglevel priority) {
level = priority; level = priority;
int rc = mdbx_setup_debug(priority, int rc = mdbx_setup_debug(MDBX_log_level_t(priority),
MDBX_DBG_ASSERT | MDBX_DBG_AUDIT | MDBX_DBG_JITTER, MDBX_DBG_ASSERT | MDBX_DBG_AUDIT | MDBX_DBG_JITTER,
mdbx_logger); mdbx_logger);
log_trace("set mdbx debug-opts: 0x%02x", rc); log_trace("set mdbx debug-opts: 0x%02x", rc);