From 2b924524ecd58e7973f8df45df1c1d10f41526dc Mon Sep 17 00:00:00 2001 From: Leo Yuriev Date: Wed, 15 Feb 2017 20:16:07 +0300 Subject: [PATCH] mdbx: initial mdbx_cursor_on_ first/last(). --- mdbx.c | 41 +++++++++++++++++++++++++++++++++++++++++ mdbx.h | 13 +++++++++++-- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/mdbx.c b/mdbx.c index 3599d14b..d32394a5 100644 --- a/mdbx.c +++ b/mdbx.c @@ -354,6 +354,47 @@ size_t mdbx_canary_get(MDB_txn *txn, mdbx_canary* canary) return txn->mt_txnid; } +int mdbx_cursor_on_first(MDB_cursor *mc) +{ + if (unlikely(mc == NULL)) + return EINVAL; + + if (unlikely(mc->mc_signature != MDBX_MC_SIGNATURE)) + return MDB_VERSION_MISMATCH; + + if (!(mc->mc_flags & C_INITIALIZED)) + return MDBX_RESULT_FALSE; + + unsigned i; + for(i = 0; i < mc->mc_snum; ++i) { + if (mc->mc_ki[i]) + return MDBX_RESULT_FALSE; + } + + return MDBX_RESULT_TRUE; +} + +int mdbx_cursor_on_last(MDB_cursor *mc) +{ + if (unlikely(mc == NULL)) + return EINVAL; + + if (unlikely(mc->mc_signature != MDBX_MC_SIGNATURE)) + return MDB_VERSION_MISMATCH; + + if (!(mc->mc_flags & C_INITIALIZED)) + return MDBX_RESULT_FALSE; + + unsigned i; + for(i = 0; i < mc->mc_snum; ++i) { + unsigned nkeys = NUMKEYS(mc->mc_pg[i]); + if (mc->mc_ki[i] != nkeys - 1) + return MDBX_RESULT_FALSE; + } + + return MDBX_RESULT_TRUE; +} + int mdbx_cursor_eof(MDB_cursor *mc) { if (unlikely(mc == NULL)) diff --git a/mdbx.h b/mdbx.h index 6a266395..515e819e 100644 --- a/mdbx.h +++ b/mdbx.h @@ -219,10 +219,19 @@ typedef struct mdbx_canary { int mdbx_canary_put(MDB_txn *txn, const mdbx_canary* canary); size_t mdbx_canary_get(MDB_txn *txn, mdbx_canary* canary); -/* Returns 1 when no more data available or cursor not positioned, - * 0 otherwise or less that zero in error case. */ +/* Returns: + * - MDBX_RESULT_TRUE when no more data available + * or cursor not positioned; + * - MDBX_RESULT_FALSE when data available; + * - Otherwise the error code. */ int mdbx_cursor_eof(MDB_cursor *mc); +/* Returns: MDBX_RESULT_TRUE, MDBX_RESULT_FALSE or Error code. */ +int mdbx_cursor_on_first(MDB_cursor *mc); + +/* Returns: MDBX_RESULT_TRUE, MDBX_RESULT_FALSE or Error code. */ +int mdbx_cursor_on_last(MDB_cursor *mc); + #define MDBX_EMULTIVAL (MDB_LAST_ERRCODE - 42) #define MDBX_RESULT_FALSE MDB_SUCCESS #define MDBX_RESULT_TRUE (-1)