mdbx: initial mdbx_cursor_on_ first/last().

This commit is contained in:
Leo Yuriev 2017-02-15 20:16:07 +03:00
parent d47d8c25d8
commit 2b924524ec
2 changed files with 52 additions and 2 deletions

41
mdbx.c
View File

@ -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))

13
mdbx.h
View File

@ -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)