From 68fd9c9908d1836c9ca6de606852c1240983cd03 Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Mon, 17 Sep 2018 10:35:06 +0300 Subject: [PATCH] mdbx: add mdbx_get2() for testing. Change-Id: Icd0961e464936fa2d24fd3ed87b61de038e955c2 --- mdbx.h | 2 ++ src/mdbx.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/mdbx.h b/mdbx.h index b5f9282c..74f647d0 100644 --- a/mdbx.h +++ b/mdbx.h @@ -1246,6 +1246,8 @@ LIBMDBX_API int mdbx_drop(MDBX_txn *txn, MDBX_dbi dbi, int del); * - MDBX_EINVAL - an invalid parameter was specified. */ LIBMDBX_API int mdbx_get(MDBX_txn *txn, MDBX_dbi dbi, MDBX_val *key, MDBX_val *data); +LIBMDBX_API int mdbx_get2(MDBX_txn *txn, MDBX_dbi dbi, MDBX_val *key, + MDBX_val *data); /* Store items into a database. * diff --git a/src/mdbx.c b/src/mdbx.c index 78085e7c..a74f0e88 100644 --- a/src/mdbx.c +++ b/src/mdbx.c @@ -7153,6 +7153,41 @@ int mdbx_get(MDBX_txn *txn, MDBX_dbi dbi, MDBX_val *key, MDBX_val *data) { return mdbx_cursor_set(&cx.outer, key, data, MDBX_SET, &exact); } +int mdbx_get2(MDBX_txn *txn, MDBX_dbi dbi, MDBX_val *key, MDBX_val *data) { + int exact = 0; + DKBUF; + + mdbx_debug("===> get db %u key [%s]", dbi, DKEY(key)); + + if (unlikely(!key || !data || !txn)) + return MDBX_EINVAL; + + if (unlikely(txn->mt_signature != MDBX_MT_SIGNATURE)) + return MDBX_EBADSIGN; + + if (unlikely(txn->mt_owner != mdbx_thread_self())) + return MDBX_THREAD_MISMATCH; + + if (unlikely(!TXN_DBI_EXIST(txn, dbi, DB_USRVALID))) + return MDBX_EINVAL; + + if (unlikely(txn->mt_flags & MDBX_TXN_BLOCKED)) + return MDBX_BAD_TXN; + + MDBX_cursor_couple cx; + int rc = mdbx_cursor_init(&cx.outer, txn, dbi); + if (unlikely(rc != MDBX_SUCCESS)) + return rc; + + const int op = + (txn->mt_dbs[dbi].md_flags & MDBX_DUPSORT) ? MDBX_GET_BOTH : MDBX_SET_KEY; + rc = mdbx_cursor_set(&cx.outer, key, data, op, &exact); + if (unlikely(rc != MDBX_SUCCESS)) + return rc; + + return exact ? MDBX_SUCCESS : MDBX_RESULT_TRUE; +} + /* Find a sibling for a page. * Replaces the page at the top of the cursor's stack with the specified * sibling, if one exists.