From acdc248f57306edc00046c43e5af0126e06de50b Mon Sep 17 00:00:00 2001 From: Howard Chu Date: Fri, 9 Sep 2011 21:43:22 -0700 Subject: [PATCH] If put(NOOVERWRITE) exists, return existing data --- libraries/libmdb/mdb.c | 4 +++- libraries/libmdb/mdb.h | 5 +++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/libraries/libmdb/mdb.c b/libraries/libmdb/mdb.c index 1363ca4f2a..b392826d07 100644 --- a/libraries/libmdb/mdb.c +++ b/libraries/libmdb/mdb.c @@ -3283,9 +3283,11 @@ mdb_cursor_put(MDB_cursor *mc, MDB_val *key, MDB_val *data, goto top; } else { int exact = 0; - rc = mdb_cursor_set(mc, key, NULL, MDB_SET, &exact); + MDB_val d2; + rc = mdb_cursor_set(mc, key, &d2, MDB_SET, &exact); if (flags == MDB_NOOVERWRITE && rc == 0) { DPRINTF("duplicate key [%s]", DKEY(key)); + *data = d2; return MDB_KEYEXIST; } if (rc && rc != MDB_NOTFOUND) diff --git a/libraries/libmdb/mdb.h b/libraries/libmdb/mdb.h index 3b5d74f00e..c0d4edcf4a 100644 --- a/libraries/libmdb/mdb.h +++ b/libraries/libmdb/mdb.h @@ -683,7 +683,7 @@ int mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data); * @param[in] txn A transaction handle returned by #mdb_txn_begin() * @param[in] dbi A database handle returned by #mdb_open() * @param[in] key The key to store in the database - * @param[in] data The data to store + * @param[in,out] data The data to store * @param[in] flags Special options for this operation. This parameter * must be set to 0 or by bitwise OR'ing together one or more of the * values described here. @@ -696,7 +696,8 @@ int mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data); *
  • #MDB_NOOVERWRITE - enter the new key/data pair only if the key * does not already appear in the database. The function will return * #MDB_KEYEXIST if the key already appears in the database, even if - * the database supports duplicates (#MDB_DUPSORT). + * the database supports duplicates (#MDB_DUPSORT). The \b data + * parameter will be set to point to the existing item. * * @return A non-zero error value on failure and 0 on success. Some possible * errors are: -- 2.39.5