mdbx: использование const MDBX_txn где это возможно в API (backport).

This commit is contained in:
Леонид Юрьев (Leonid Yuriev) 2023-10-14 09:04:06 +03:00
parent ed8c7ead4e
commit 7eb2f4130e
3 changed files with 42 additions and 39 deletions

26
mdbx.h
View File

@ -4293,8 +4293,8 @@ mdbx_int64_from_key(const MDBX_val);
* \retval MDBX_THREAD_MISMATCH Given transaction is not owned
* by current thread.
* \retval MDBX_EINVAL An invalid parameter was specified. */
LIBMDBX_API int mdbx_dbi_stat(MDBX_txn *txn, MDBX_dbi dbi, MDBX_stat *stat,
size_t bytes);
LIBMDBX_API int mdbx_dbi_stat(const MDBX_txn *txn, MDBX_dbi dbi,
MDBX_stat *stat, size_t bytes);
/** \brief Retrieve depth (bitmask) information of nested dupsort (multi-value)
* B+trees for given database.
@ -4311,7 +4311,7 @@ LIBMDBX_API int mdbx_dbi_stat(MDBX_txn *txn, MDBX_dbi dbi, MDBX_stat *stat,
* by current thread.
* \retval MDBX_EINVAL An invalid parameter was specified.
* \retval MDBX_RESULT_TRUE The dbi isn't a dupsort (multi-value) database. */
LIBMDBX_API int mdbx_dbi_dupsort_depthmask(MDBX_txn *txn, MDBX_dbi dbi,
LIBMDBX_API int mdbx_dbi_dupsort_depthmask(const MDBX_txn *txn, MDBX_dbi dbi,
uint32_t *mask);
/** \brief DBI state bits returted by \ref mdbx_dbi_flags_ex()
@ -4343,13 +4343,13 @@ DEFINE_ENUM_FLAG_OPERATORS(MDBX_dbi_state_t)
* \param [out] state Address where the state will be returned.
*
* \returns A non-zero error value on failure and 0 on success. */
LIBMDBX_API int mdbx_dbi_flags_ex(MDBX_txn *txn, MDBX_dbi dbi, unsigned *flags,
unsigned *state);
LIBMDBX_API int mdbx_dbi_flags_ex(const MDBX_txn *txn, MDBX_dbi dbi,
unsigned *flags, unsigned *state);
/** \brief The shortcut to calling \ref mdbx_dbi_flags_ex() with `state=NULL`
* for discarding it result.
* \ingroup c_statinfo */
LIBMDBX_INLINE_API(int, mdbx_dbi_flags,
(MDBX_txn * txn, MDBX_dbi dbi, unsigned *flags)) {
(const MDBX_txn *txn, MDBX_dbi dbi, unsigned *flags)) {
unsigned state;
return mdbx_dbi_flags_ex(txn, dbi, flags, &state);
}
@ -4425,7 +4425,7 @@ LIBMDBX_API int mdbx_drop(MDBX_txn *txn, MDBX_dbi dbi, bool del);
* by current thread.
* \retval MDBX_NOTFOUND The key was not in the database.
* \retval MDBX_EINVAL An invalid parameter was specified. */
LIBMDBX_API int mdbx_get(MDBX_txn *txn, MDBX_dbi dbi, const MDBX_val *key,
LIBMDBX_API int mdbx_get(const MDBX_txn *txn, MDBX_dbi dbi, const MDBX_val *key,
MDBX_val *data);
/** \brief Get items from a database
@ -4458,7 +4458,7 @@ LIBMDBX_API int mdbx_get(MDBX_txn *txn, MDBX_dbi dbi, const MDBX_val *key,
* by current thread.
* \retval MDBX_NOTFOUND The key was not in the database.
* \retval MDBX_EINVAL An invalid parameter was specified. */
LIBMDBX_API int mdbx_get_ex(MDBX_txn *txn, MDBX_dbi dbi, MDBX_val *key,
LIBMDBX_API int mdbx_get_ex(const MDBX_txn *txn, MDBX_dbi dbi, MDBX_val *key,
MDBX_val *data, size_t *values_count);
/** \brief Get equal or great item from a database.
@ -4489,7 +4489,7 @@ LIBMDBX_API int mdbx_get_ex(MDBX_txn *txn, MDBX_dbi dbi, MDBX_val *key,
* by current thread.
* \retval MDBX_NOTFOUND The key was not in the database.
* \retval MDBX_EINVAL An invalid parameter was specified. */
LIBMDBX_API int mdbx_get_equal_or_great(MDBX_txn *txn, MDBX_dbi dbi,
LIBMDBX_API int mdbx_get_equal_or_great(const MDBX_txn *txn, MDBX_dbi dbi,
MDBX_val *key, MDBX_val *data);
/** \brief Store items into a database.
@ -4731,7 +4731,7 @@ mdbx_cursor_get_userctx(const MDBX_cursor *cursor);
* \retval MDBX_THREAD_MISMATCH Given transaction is not owned
* by current thread.
* \retval MDBX_EINVAL An invalid parameter was specified. */
LIBMDBX_API int mdbx_cursor_bind(MDBX_txn *txn, MDBX_cursor *cursor,
LIBMDBX_API int mdbx_cursor_bind(const MDBX_txn *txn, MDBX_cursor *cursor,
MDBX_dbi dbi);
/** \brief Create a cursor handle for the specified transaction and DBI handle.
@ -4764,7 +4764,7 @@ LIBMDBX_API int mdbx_cursor_bind(MDBX_txn *txn, MDBX_cursor *cursor,
* \retval MDBX_THREAD_MISMATCH Given transaction is not owned
* by current thread.
* \retval MDBX_EINVAL An invalid parameter was specified. */
LIBMDBX_API int mdbx_cursor_open(MDBX_txn *txn, MDBX_dbi dbi,
LIBMDBX_API int mdbx_cursor_open(const MDBX_txn *txn, MDBX_dbi dbi,
MDBX_cursor **cursor);
/** \brief Close a cursor handle.
@ -4807,7 +4807,7 @@ LIBMDBX_API void mdbx_cursor_close(MDBX_cursor *cursor);
* \retval MDBX_EINVAL An invalid parameter was specified.
* \retval MDBX_BAD_DBI The cursor was not bound to a DBI-handle
* or such a handle became invalid. */
LIBMDBX_API int mdbx_cursor_renew(MDBX_txn *txn, MDBX_cursor *cursor);
LIBMDBX_API int mdbx_cursor_renew(const MDBX_txn *txn, MDBX_cursor *cursor);
/** \brief Return the cursor's transaction handle.
* \ingroup c_cursors
@ -5186,7 +5186,7 @@ LIBMDBX_API int mdbx_estimate_move(const MDBX_cursor *cursor, MDBX_val *key,
* \param [out] distance_items A pointer to store range estimation result.
*
* \returns A non-zero error value on failure and 0 on success. */
LIBMDBX_API int mdbx_estimate_range(MDBX_txn *txn, MDBX_dbi dbi,
LIBMDBX_API int mdbx_estimate_range(const MDBX_txn *txn, MDBX_dbi dbi,
MDBX_val *begin_key, MDBX_val *begin_data,
MDBX_val *end_key, MDBX_val *end_data,
ptrdiff_t *distance_items);

View File

@ -3839,7 +3839,7 @@ public:
txn_managed start_nested();
/// \brief Opens cursor for specified key-value map handle.
inline cursor_managed open_cursor(map_handle map);
inline cursor_managed open_cursor(map_handle map) const;
/// \brief Open existing key-value map.
inline map_handle open_map(
@ -4217,11 +4217,11 @@ public:
/// \brief Renew/bind a cursor with a new transaction and previously used
/// key-value map handle.
inline void renew(::mdbx::txn &txn);
inline void renew(const ::mdbx::txn &txn);
/// \brief Bind/renew a cursor with a new transaction and specified key-value
/// map handle.
inline void bind(::mdbx::txn &txn, ::mdbx::map_handle map_handle);
inline void bind(const ::mdbx::txn &txn, ::mdbx::map_handle map_handle);
/// \brief Returns the cursor's transaction.
inline ::mdbx::txn txn() const;
@ -5457,7 +5457,7 @@ inline txn::info txn::get_info(bool scan_reader_lock_table) const {
return r;
}
inline cursor_managed txn::open_cursor(map_handle map) {
inline cursor_managed txn::open_cursor(map_handle map) const {
MDBX_cursor *ptr;
error::success_or_throw(::mdbx_cursor_open(handle_, map.dbi, &ptr));
return cursor_managed(ptr);
@ -6102,11 +6102,12 @@ inline ptrdiff_t cursor::estimate(move_operation operation) const {
return estimate(operation, &unused_key, nullptr);
}
inline void cursor::renew(::mdbx::txn &txn) {
inline void cursor::renew(const ::mdbx::txn &txn) {
error::success_or_throw(::mdbx_cursor_renew(txn, handle_));
}
inline void cursor::bind(::mdbx::txn &txn, ::mdbx::map_handle map_handle) {
inline void cursor::bind(const ::mdbx::txn &txn,
::mdbx::map_handle map_handle) {
error::success_or_throw(::mdbx_cursor_bind(txn, handle_, map_handle.dbi));
}

View File

@ -3386,7 +3386,7 @@ static int __must_check_result cursor_first(MDBX_cursor *mc, MDBX_val *key,
static int __must_check_result cursor_last(MDBX_cursor *mc, MDBX_val *key,
MDBX_val *data);
static int __must_check_result cursor_init(MDBX_cursor *mc, MDBX_txn *txn,
static int __must_check_result cursor_init(MDBX_cursor *mc, const MDBX_txn *txn,
size_t dbi);
static int __must_check_result cursor_xinit0(MDBX_cursor *mc);
static int __must_check_result cursor_xinit1(MDBX_cursor *mc, MDBX_node *node,
@ -9579,7 +9579,7 @@ int mdbx_txn_flags(const MDBX_txn *txn) {
}
/* Check for misused dbi handles */
static __inline bool dbi_changed(MDBX_txn *txn, size_t dbi) {
static __inline bool dbi_changed(const MDBX_txn *txn, size_t dbi) {
if (txn->mt_dbiseqs == txn->mt_env->me_dbiseqs)
return false;
if (likely(
@ -11170,7 +11170,7 @@ static int txn_write(MDBX_txn *txn, iov_ctx_t *ctx) {
}
/* Check txn and dbi arguments to a function */
static __always_inline bool check_dbi(MDBX_txn *txn, MDBX_dbi dbi,
static __always_inline bool check_dbi(const MDBX_txn *txn, MDBX_dbi dbi,
unsigned validity) {
if (likely(dbi < txn->mt_numdbs)) {
if (likely(!dbi_changed(txn, dbi))) {
@ -11181,7 +11181,7 @@ static __always_inline bool check_dbi(MDBX_txn *txn, MDBX_dbi dbi,
return false;
}
}
return dbi_import(txn, dbi);
return dbi_import((MDBX_txn *)txn, dbi);
}
/* Merge child txn into parent */
@ -16078,7 +16078,8 @@ static __always_inline int node_read(MDBX_cursor *mc, const MDBX_node *node,
return node_read_bigdata(mc, node, data, mp);
}
int mdbx_get(MDBX_txn *txn, MDBX_dbi dbi, const MDBX_val *key, MDBX_val *data) {
int mdbx_get(const MDBX_txn *txn, MDBX_dbi dbi, const MDBX_val *key,
MDBX_val *data) {
DKBUF_DEBUG;
DEBUG("===> get db %u key [%s]", dbi, DKEY_DEBUG(key));
@ -16100,7 +16101,7 @@ int mdbx_get(MDBX_txn *txn, MDBX_dbi dbi, const MDBX_val *key, MDBX_val *data) {
return cursor_set(&cx.outer, (MDBX_val *)key, data, MDBX_SET).err;
}
int mdbx_get_equal_or_great(MDBX_txn *txn, MDBX_dbi dbi, MDBX_val *key,
int mdbx_get_equal_or_great(const MDBX_txn *txn, MDBX_dbi dbi, MDBX_val *key,
MDBX_val *data) {
int rc = check_txn(txn, MDBX_TXN_BLOCKED);
if (unlikely(rc != MDBX_SUCCESS))
@ -16123,8 +16124,8 @@ int mdbx_get_equal_or_great(MDBX_txn *txn, MDBX_dbi dbi, MDBX_val *key,
return cursor_get(&cx.outer, key, data, MDBX_SET_LOWERBOUND);
}
int mdbx_get_ex(MDBX_txn *txn, MDBX_dbi dbi, MDBX_val *key, MDBX_val *data,
size_t *values_count) {
int mdbx_get_ex(const MDBX_txn *txn, MDBX_dbi dbi, MDBX_val *key,
MDBX_val *data, size_t *values_count) {
DKBUF_DEBUG;
DEBUG("===> get db %u key [%s]", dbi, DKEY_DEBUG(key));
@ -18754,13 +18755,13 @@ static int cursor_xinit2(MDBX_cursor *mc, MDBX_xcursor *src_mx,
}
static __inline int couple_init(MDBX_cursor_couple *couple, const size_t dbi,
MDBX_txn *const txn, MDBX_db *const db,
const MDBX_txn *const txn, MDBX_db *const db,
MDBX_dbx *const dbx, uint8_t *const dbstate) {
couple->outer.mc_signature = MDBX_MC_LIVE;
couple->outer.mc_next = NULL;
couple->outer.mc_backup = NULL;
couple->outer.mc_dbi = (MDBX_dbi)dbi;
couple->outer.mc_txn = txn;
couple->outer.mc_txn = (MDBX_txn *)txn;
couple->outer.mc_db = db;
couple->outer.mc_dbx = dbx;
couple->outer.mc_dbistate = dbstate;
@ -18798,7 +18799,7 @@ static __inline int couple_init(MDBX_cursor_couple *couple, const size_t dbi,
}
/* Initialize a cursor for a given transaction and database. */
static int cursor_init(MDBX_cursor *mc, MDBX_txn *txn, size_t dbi) {
static int cursor_init(MDBX_cursor *mc, const MDBX_txn *txn, size_t dbi) {
STATIC_ASSERT(offsetof(MDBX_cursor_couple, outer) == 0);
return couple_init(container_of(mc, MDBX_cursor_couple, outer), dbi, txn,
&txn->mt_dbs[dbi], &txn->mt_dbxs[dbi],
@ -18841,7 +18842,7 @@ void *mdbx_cursor_get_userctx(const MDBX_cursor *mc) {
return couple->mc_userctx;
}
int mdbx_cursor_bind(MDBX_txn *txn, MDBX_cursor *mc, MDBX_dbi dbi) {
int mdbx_cursor_bind(const MDBX_txn *txn, MDBX_cursor *mc, MDBX_dbi dbi) {
if (unlikely(!mc))
return MDBX_EINVAL;
@ -18913,7 +18914,7 @@ int mdbx_cursor_bind(MDBX_txn *txn, MDBX_cursor *mc, MDBX_dbi dbi) {
return MDBX_SUCCESS;
}
int mdbx_cursor_open(MDBX_txn *txn, MDBX_dbi dbi, MDBX_cursor **ret) {
int mdbx_cursor_open(const MDBX_txn *txn, MDBX_dbi dbi, MDBX_cursor **ret) {
if (unlikely(!ret))
return MDBX_EINVAL;
*ret = NULL;
@ -18932,7 +18933,7 @@ int mdbx_cursor_open(MDBX_txn *txn, MDBX_dbi dbi, MDBX_cursor **ret) {
return MDBX_SUCCESS;
}
int mdbx_cursor_renew(MDBX_txn *txn, MDBX_cursor *mc) {
int mdbx_cursor_renew(const MDBX_txn *txn, MDBX_cursor *mc) {
return likely(mc) ? mdbx_cursor_bind(txn, mc, mc->mc_dbi) : MDBX_EINVAL;
}
@ -22199,7 +22200,7 @@ __cold int mdbx_env_stat_ex(const MDBX_env *env, const MDBX_txn *txn,
return rc;
}
__cold int mdbx_dbi_dupsort_depthmask(MDBX_txn *txn, MDBX_dbi dbi,
__cold int mdbx_dbi_dupsort_depthmask(const MDBX_txn *txn, MDBX_dbi dbi,
uint32_t *mask) {
int rc = check_txn(txn, MDBX_TXN_BLOCKED);
if (unlikely(rc != MDBX_SUCCESS))
@ -22817,7 +22818,7 @@ int mdbx_dbi_open_ex2(MDBX_txn *txn, const MDBX_val *name,
return dbi_open(txn, name, flags, dbi, keycmp, datacmp);
}
__cold int mdbx_dbi_stat(MDBX_txn *txn, MDBX_dbi dbi, MDBX_stat *dest,
__cold int mdbx_dbi_stat(const MDBX_txn *txn, MDBX_dbi dbi, MDBX_stat *dest,
size_t bytes) {
int rc = check_txn(txn, MDBX_TXN_BLOCKED);
if (unlikely(rc != MDBX_SUCCESS))
@ -22837,7 +22838,7 @@ __cold int mdbx_dbi_stat(MDBX_txn *txn, MDBX_dbi dbi, MDBX_stat *dest,
return MDBX_BAD_TXN;
if (unlikely(txn->mt_dbistate[dbi] & DBI_STALE)) {
rc = fetch_sdb(txn, dbi);
rc = fetch_sdb((MDBX_txn *)txn, dbi);
if (unlikely(rc != MDBX_SUCCESS))
return rc;
}
@ -22898,7 +22899,7 @@ int mdbx_dbi_close(MDBX_env *env, MDBX_dbi dbi) {
return rc;
}
int mdbx_dbi_flags_ex(MDBX_txn *txn, MDBX_dbi dbi, unsigned *flags,
int mdbx_dbi_flags_ex(const MDBX_txn *txn, MDBX_dbi dbi, unsigned *flags,
unsigned *state) {
int rc = check_txn(txn, MDBX_TXN_BLOCKED - MDBX_TXN_ERROR);
if (unlikely(rc != MDBX_SUCCESS))
@ -24069,7 +24070,7 @@ int mdbx_estimate_move(const MDBX_cursor *cursor, MDBX_val *key, MDBX_val *data,
return mdbx_estimate_distance(cursor, &next.outer, distance_items);
}
int mdbx_estimate_range(MDBX_txn *txn, MDBX_dbi dbi, MDBX_val *begin_key,
int mdbx_estimate_range(const MDBX_txn *txn, MDBX_dbi dbi, MDBX_val *begin_key,
MDBX_val *begin_data, MDBX_val *end_key,
MDBX_val *end_data, ptrdiff_t *size_items) {
int rc = check_txn(txn, MDBX_TXN_BLOCKED);
@ -25411,7 +25412,8 @@ LIBMDBX_API __cold int mdbx_env_info(const MDBX_env *env, MDBX_envinfo *info,
return __inline_mdbx_env_info(env, info, bytes);
}
LIBMDBX_API int mdbx_dbi_flags(MDBX_txn *txn, MDBX_dbi dbi, unsigned *flags) {
LIBMDBX_API int mdbx_dbi_flags(const MDBX_txn *txn, MDBX_dbi dbi,
unsigned *flags) {
return __inline_mdbx_dbi_flags(txn, dbi, flags);
}