Skip to content

Commit 4a278b3

Browse files
committed
Reserve methods to support PutFlags (fixes lmdbjava#30)
1 parent d81f062 commit 4a278b3

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

src/main/java/org/lmdbjava/Cursor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,10 @@ public void renew(final Txn<T> newTxn) {
246246
*
247247
* @param key key to store in the database (not null)
248248
* @param size size of the value to be stored in the database (not null)
249+
* @param op options for this operation
249250
* @return a buffer that can be used to modify the value
250251
*/
251-
public T reserve(final T key, final int size) {
252+
public T reserve(final T key, final int size, final PutFlags... op) {
252253
if (SHOULD_CHECK) {
253254
requireNonNull(key);
254255
checkNotClosed();
@@ -257,7 +258,7 @@ public T reserve(final T key, final int size) {
257258
}
258259
txn.keyIn(key);
259260
txn.valIn(size);
260-
final int flags = mask(MDB_RESERVE);
261+
final int flags = mask(op) | MDB_RESERVE.getMask();
261262
checkRc(LIB.mdb_cursor_put(ptrCursor, txn.pointerKey(), txn.pointerVal(),
262263
flags));
263264
txn.valOut();

src/main/java/org/lmdbjava/Dbi.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,11 @@ public void put(final Txn<T> txn, final T key, final T val,
318318
* @param txn transaction handle (not null; not committed; must be R-W)
319319
* @param key key to store in the database (not null)
320320
* @param size size of the value to be stored in the database
321+
* @param op options for this operation
321322
* @return a buffer that can be used to modify the value
322323
*/
323-
public T reserve(final Txn<T> txn, final T key, final int size) {
324+
public T reserve(final Txn<T> txn, final T key, final int size,
325+
final PutFlags... op) {
324326
if (SHOULD_CHECK) {
325327
requireNonNull(txn);
326328
requireNonNull(key);
@@ -329,9 +331,9 @@ public T reserve(final Txn<T> txn, final T key, final int size) {
329331
}
330332
txn.keyIn(key);
331333
txn.valIn(size);
332-
final int mask = mask(MDB_RESERVE);
334+
final int flags = mask(op) | MDB_RESERVE.getMask();
333335
checkRc(LIB.mdb_put(txn.pointer(), ptr, txn.pointerKey(), txn.pointerVal(),
334-
mask));
336+
flags));
335337
txn.valOut(); // marked as in,out in LMDB C docs
336338
return txn.val();
337339
}

src/test/java/org/lmdbjava/DbiTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public void putReserve() {
206206
final ByteBuffer key = bb(5);
207207
try (Txn<ByteBuffer> txn = env.txnWrite()) {
208208
assertNull(db.get(txn, key));
209-
final ByteBuffer val = db.reserve(txn, key, 32);
209+
final ByteBuffer val = db.reserve(txn, key, 32, MDB_NOOVERWRITE);
210210
val.putLong(MAX_VALUE);
211211
assertNotNull(db.get(txn, key));
212212
txn.commit();

0 commit comments

Comments
 (0)