From 46f933875e7b1c4b38437b758ac9a3f4007b8db1 Mon Sep 17 00:00:00 2001 From: gcxfd Date: Sat, 11 Dec 2021 20:31:08 +0800 Subject: [PATCH 1/2] add MDBX_SET_UPPERBOUND --- mdbx.h | 7 ++++++- src/core.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/mdbx.h b/mdbx.h index 4ec12c1ce..f30121bb6 100644 --- a/mdbx.h +++ b/mdbx.h @@ -1583,7 +1583,12 @@ enum MDBX_cursor_op { * i.e. for a pairs/tuples of a key and an each data value of duplicates. * Returns \ref MDBX_SUCCESS if key-value pair found exactly and * \ref MDBX_RESULT_TRUE if the next pair was returned. */ - MDBX_SET_LOWERBOUND + MDBX_SET_LOWERBOUND, + + /** Position at first key-value pair lesser than or equal to specified, + * return same as MDBX_SET_LOWERBOUND + */ + MDBX_SET_UPPERBOUND }; #ifndef __cplusplus /** \ingroup c_cursors */ diff --git a/src/core.c b/src/core.c index d5bcff153..0de538a21 100644 --- a/src/core.c +++ b/src/core.c @@ -14542,6 +14542,50 @@ int mdbx_cursor_get(MDBX_cursor *mc, MDBX_val *key, MDBX_val *data, rc = MDBX_RESULT_TRUE; break; } + case MDBX_SET_UPPERBOUND: { + if (unlikely(key == NULL || data == NULL)) + return MDBX_EINVAL; + MDBX_val save_data = *data; + struct cursor_set_result csr = + mdbx_cursor_set(mc, key, data, MDBX_SET_RANGE); + rc = csr.err; + if (rc == MDBX_NOTFOUND) { + rc = mdbx_cursor_last(mc, key, data); + if (rc == MDBX_SUCCESS) { + rc = MDBX_RESULT_TRUE; + } + } else if ( rc == MDBX_SUCCESS ){ + if (csr.exact) { + if (mc->mc_xcursor) { + mc->mc_flags &= ~C_DEL; + csr.exact = false; + if (!save_data.iov_base && (mc->mc_db->md_flags & MDBX_DUPFIXED)) { + mfunc = mdbx_cursor_last; + goto mmove; + } else if (mc->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED) { + *data = save_data; + csr = mdbx_cursor_set(&mc->mc_xcursor->mx_cursor, data, NULL, + MDBX_SET_RANGE); + rc = csr.err; + if (rc == MDBX_NOTFOUND) { + mdbx_cassert(mc, !csr.exact); + rc = mdbx_cursor_prev(mc, key, data, MDBX_PREV_NODUP); + } + } else { + int cmp = mc->mc_dbx->md_dcmp(&save_data, data); + csr.exact = (cmp == 0); + if (cmp > 0) + rc = mdbx_cursor_prev(mc, key, data, MDBX_PREV_NODUP); + } + } + } else { + rc = mdbx_cursor_prev(mc, key, data, MDBX_PREV); + } + if (rc == MDBX_SUCCESS && !csr.exact) + rc = MDBX_RESULT_TRUE; + } + break; + } default: mdbx_debug("unhandled/unimplemented cursor operation %u", op); return MDBX_EINVAL; From 74b62ceee5677a61fcad0be33ae105b5fd4495a3 Mon Sep 17 00:00:00 2001 From: gcxfd Date: Sat, 11 Dec 2021 20:32:18 +0800 Subject: [PATCH 2/2] .gitignore add *.err *.log --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 671b049c3..2c9daa332 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ *.[ao] +*.err +*.log *.bak *.exe *.gcda