From f6edbd7793e5c01cb9089365872d01c11206c2ae Mon Sep 17 00:00:00 2001 From: Howard Chu Date: Sat, 3 Sep 2011 15:19:24 -0700 Subject: [PATCH 1/1] Add MDB_{FIRST,LAST}_DUP for cursor_get --- libraries/libmdb/mdb.c | 20 +++++++++++++++ libraries/libmdb/mdb.h | 56 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/libraries/libmdb/mdb.c b/libraries/libmdb/mdb.c index 431d1af5ae..4eff6ff8ed 100644 --- a/libraries/libmdb/mdb.c +++ b/libraries/libmdb/mdb.c @@ -2781,9 +2781,29 @@ fetchm: case MDB_FIRST: rc = mdb_cursor_first(cursor, key, data); break; + case MDB_FIRST_DUP: + if (data == NULL || + !(cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) || + !cursor->mc_initialized || + !cursor->mc_xcursor->mx_cursor.mc_initialized) { + rc = EINVAL; + break; + } + rc = mdb_cursor_first(&cursor->mc_xcursor->mx_cursor, data, NULL); + break; case MDB_LAST: rc = mdb_cursor_last(cursor, key, data); break; + case MDB_LAST_DUP: + if (data == NULL || + !(cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) || + !cursor->mc_initialized || + !cursor->mc_xcursor->mx_cursor.mc_initialized) { + rc = EINVAL; + break; + } + rc = mdb_cursor_last(&cursor->mc_xcursor->mx_cursor, data, NULL); + break; default: DPRINTF("unhandled/unimplemented cursor operation %u", op); rc = EINVAL; diff --git a/libraries/libmdb/mdb.h b/libraries/libmdb/mdb.h index 4a57ba02e2..0f564c5d82 100644 --- a/libraries/libmdb/mdb.h +++ b/libraries/libmdb/mdb.h @@ -175,11 +175,15 @@ typedef void (MDB_rel_func)(void *newptr, void *oldptr, size_t size); /** Cursor operations */ typedef enum MDB_cursor_op { MDB_FIRST, /**< Position at first key/data item */ + MDB_FIRST_DUP, /**< Position at first data item of current key. + Only for #MDB_DUPSORT */ MDB_GET_BOTH, /**< Position at key/data pair. Only for #MDB_DUPSORT */ MDB_GET_BOTH_RANGE, /**< position at key, nearest data. Only for #MDB_DUPSORT */ MDB_GET_MULTIPLE, /**< Return all the duplicate data items at the current cursor position. Only for #MDB_DUPFIXED */ MDB_LAST, /**< Position at last key/data item */ + MDB_LAST_DUP, /**< Position at last data item of current key. + Only for #MDB_DUPSORT */ MDB_NEXT, /**< Position at next data item */ MDB_NEXT_DUP, /**< Position at next data item of current key. Only for #MDB_DUPSORT */ @@ -761,6 +765,58 @@ void mdb_cursor_close(MDB_cursor *cursor); int mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data, MDB_cursor_op op); + /** Store by cursor. + * This function stores key/data pairs into the database. + * If the function fails for any reason, the state of the cursor will be + * unchanged. If the function succeeds and an item is inserted into the + * database, the cursor is always positioned to refer to the newly inserted item. + * @param[in] cursor A cursor handle returned by #mdb_cursor_open() + * @param[in] key The key operated on. + * @param[in] data The data operated on. + * @param[in] flags Options for this operation. This parameter + * must be set to 0 or one of the values described here. + * + * @return A non-zero error value on failure and 0 on success. Some possible + * errors are: + * + */ +int mdb_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data, + unsigned int flags); + + /** Delete current key/data pair + * This function deletes the key/data pair to which the cursor refers. + * @param[in] cursor A cursor handle returned by #mdb_cursor_open() + * @param[in] flags Options for this operation. This parameter + * must be set to 0 or one of the values described here. + * + * @return A non-zero error value on failure and 0 on success. Some possible + * errors are: + * + */ +int mdb_cursor_del(MDB_cursor *cursor, unsigned int flags); + /** Return count of duplicates for current key. * This call is only valid on databases that support sorted duplicate * data items #MDB_DUPSORT. -- 2.39.2