Skip to content

Commit c0bbe73

Browse files
committed
Deprecate methods using varargs flags
1 parent 1b3f94d commit c0bbe73

File tree

10 files changed

+392
-130
lines changed

10 files changed

+392
-130
lines changed

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

Lines changed: 160 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import static org.lmdbjava.Dbi.KeyNotFoundException.MDB_NOTFOUND;
2121
import static org.lmdbjava.Env.SHOULD_CHECK;
2222
import static org.lmdbjava.Library.LIB;
23-
import static org.lmdbjava.MaskedFlag.isSet;
24-
import static org.lmdbjava.MaskedFlag.mask;
2523
import static org.lmdbjava.PutFlags.MDB_MULTIPLE;
2624
import static org.lmdbjava.PutFlags.MDB_NODUPDATA;
2725
import static org.lmdbjava.PutFlags.MDB_NOOVERWRITE;
@@ -97,23 +95,49 @@ public long count() {
9795
checkRc(LIB.mdb_cursor_count(ptrCursor, longByReference));
9896
return longByReference.longValue();
9997
}
98+
/**
99+
* @deprecated Instead use {@link Cursor#delete(PutFlagSet)}.
100+
* <hr>
101+
* Delete current key/data pair.
102+
*
103+
* <p>This function deletes the key/data pair to which the cursor refers.
104+
*
105+
* @param flags flags (either null or {@link PutFlags#MDB_NODUPDATA}
106+
*/
107+
@Deprecated
108+
public void delete(final PutFlags... flags) {
109+
delete(PutFlagSet.of(flags));
110+
}
111+
112+
/**
113+
* @deprecated Instead use {@link Cursor#delete(PutFlagSet)}.
114+
* <hr>
115+
* Delete current key/data pair.
116+
*
117+
* <p>This function deletes the key/data pair to which the cursor refers.
118+
*/
119+
public void delete() {
120+
delete(PutFlagSet.EMPTY);
121+
}
100122

101123
/**
102124
* Delete current key/data pair.
103125
*
104126
* <p>This function deletes the key/data pair to which the cursor refers.
105127
*
106-
* @param f flags (either null or {@link PutFlags#MDB_NODUPDATA}
128+
* @param flags flags (either null or {@link PutFlags#MDB_NODUPDATA}
107129
*/
108-
public void delete(final PutFlags... f) {
130+
public void delete(final PutFlagSet flags) {
109131
if (SHOULD_CHECK) {
110132
env.checkNotClosed();
111133
checkNotClosed();
112134
txn.checkReady();
113135
txn.checkWritesAllowed();
114136
}
115-
final int flags = mask(f);
116-
checkRc(LIB.mdb_cursor_del(ptrCursor, flags));
137+
final PutFlagSet putFlagSet = flags != null
138+
? flags
139+
: PutFlagSet.EMPTY;
140+
checkRc(LIB.mdb_cursor_del(ptrCursor, putFlagSet.getMask()));
117141
}
118142

119143
/**
@@ -235,17 +259,49 @@ public boolean prev() {
235259
}
236260

237261
/**
262+
* @deprecated Use {@link Cursor#put(Object, Object, PutFlagSet)} instead.
263+
* <hr>
238264
* Store by cursor.
239265
*
240266
* <p>This function stores key/data pairs into the database.
241267
*
242268
* @param key key to store
243269
* @param val data to store
244-
* @param op options for this operation
270+
* @param flags options for this operation
271+
* @return true if the value was put, false if MDB_NOOVERWRITE or MDB_NODUPDATA were set and the
272+
* key/value existed already.
273+
*/
274+
@Deprecated
275+
public boolean put(final T key, final T val, final PutFlags... flags) {
276+
return put(key, val, PutFlagSet.of(flags));
277+
}
278+
279+
/**
280+
* Store by cursor.
281+
*
282+
* <p>This function stores key/data pairs into the database.
283+
*
284+
* @param key key to store
285+
* @param val data to store
286+
* @return true if the value was put, false if MDB_NOOVERWRITE or MDB_NODUPDATA were set and the
287+
* key/value existed already.
288+
*/
289+
public boolean put(final T key, final T val) {
290+
return put(key, val, PutFlagSet.EMPTY);
291+
}
292+
293+
/**
294+
* Store by cursor.
295+
*
296+
* <p>This function stores key/data pairs into the database.
297+
*
298+
* @param key key to store
299+
* @param val data to store
300+
* @param flags options for this operation
245301
* @return true if the value was put, false if MDB_NOOVERWRITE or MDB_NODUPDATA were set and the
246302
* key/value existed already.
247303
*/
248-
public boolean put(final T key, final T val, final PutFlags... op) {
304+
public boolean put(final T key, final T val, final PutFlagSet flags) {
249305
if (SHOULD_CHECK) {
250306
requireNonNull(key);
251307
requireNonNull(val);
@@ -256,12 +312,14 @@ public boolean put(final T key, final T val, final PutFlags... op) {
256312
}
257313
final Pointer transientKey = kv.keyIn(key);
258314
final Pointer transientVal = kv.valIn(val);
259-
final int mask = mask(op);
260-
final int rc = LIB.mdb_cursor_put(ptrCursor, kv.pointerKey(), kv.pointerVal(), mask);
315+
final PutFlagSet putFlagSet = flags != null
316+
? flags
317+
: PutFlagSet.EMPTY;
318+
final int rc = LIB.mdb_cursor_put(ptrCursor, kv.pointerKey(), kv.pointerVal(), putFlagSet.getMask());
261319
if (rc == MDB_KEYEXIST) {
262-
if (isSet(mask, MDB_NOOVERWRITE)) {
320+
if (putFlagSet.isSet(MDB_NOOVERWRITE)) {
263321
kv.valOut(); // marked as in,out in LMDB C docs
264-
} else if (!isSet(mask, MDB_NODUPDATA)) {
322+
} else if (!putFlagSet.isSet(MDB_NODUPDATA)) {
265323
checkRc(rc);
266324
}
267325
return false;
@@ -274,6 +332,42 @@ public boolean put(final T key, final T val, final PutFlags... op) {
274332
return true;
275333
}
276334

335+
/**
336+
* @deprecated Use {@link Cursor#put(Object, Object, PutFlagSet)} instead.
337+
* <hr>
338+
* Put multiple values into the database in one <code>MDB_MULTIPLE</code> operation.
339+
*
340+
* <p>The database must have been opened with {@link DbiFlags#MDB_DUPFIXED}. The buffer must
341+
* contain fixed-sized values to be inserted. The size of each element is calculated from the
342+
* buffer's size divided by the given element count. For example, to populate 10 X 4 byte integers
343+
* at once, present a buffer of 40 bytes and specify the element as 10.
344+
*
345+
* @param key key to store in the database (not null)
346+
* @param val value to store in the database (not null)
347+
* @param elements number of elements contained in the passed value buffer
348+
* @param flags options for operation (must set <code>MDB_MULTIPLE</code>)
349+
*/
350+
@Deprecated
351+
public void putMultiple(final T key, final T val, final int elements, final PutFlags... flags) {
352+
putMultiple(key, val, elements, PutFlagSet.of(flags));
353+
}
354+
355+
/**
356+
* Put multiple values into the database in one <code>MDB_MULTIPLE</code> operation.
357+
*
358+
* <p>The database must have been opened with {@link DbiFlags#MDB_DUPFIXED}. The buffer must
359+
* contain fixed-sized values to be inserted. The size of each element is calculated from the
360+
* buffer's size divided by the given element count. For example, to populate 10 X 4 byte integers
361+
* at once, present a buffer of 40 bytes and specify the element as 10.
362+
*
363+
* @param key key to store in the database (not null)
364+
* @param val value to store in the database (not null)
365+
* @param elements number of elements contained in the passed value buffer
366+
*/
367+
public void putMultiple(final T key, final T val, final int elements) {
368+
putMultiple(key, val, elements, PutFlagSet.EMPTY);
369+
}
370+
277371
/**
278372
* Put multiple values into the database in one <code>MDB_MULTIPLE</code> operation.
279373
*
@@ -285,9 +379,10 @@ public boolean put(final T key, final T val, final PutFlags... op) {
285379
* @param key key to store in the database (not null)
286380
* @param val value to store in the database (not null)
287381
* @param elements number of elements contained in the passed value buffer
288-
* @param op options for operation (must set <code>MDB_MULTIPLE</code>)
382+
* @param flags options for operation (must set <code>MDB_MULTIPLE</code>)
383+
* Either a {@link PutFlagSet} or a single {@link PutFlags}.
289384
*/
290-
public void putMultiple(final T key, final T val, final int elements, final PutFlags... op) {
385+
public void putMultiple(final T key, final T val, final int elements, final PutFlagSet flags) {
291386
if (SHOULD_CHECK) {
292387
requireNonNull(txn);
293388
requireNonNull(key);
@@ -296,13 +391,15 @@ public void putMultiple(final T key, final T val, final int elements, final PutF
296391
txn.checkReady();
297392
txn.checkWritesAllowed();
298393
}
299-
final int mask = mask(op);
300-
if (SHOULD_CHECK && !isSet(mask, MDB_MULTIPLE)) {
394+
final PutFlagSet putFlagSet = flags != null
395+
? flags
396+
: PutFlagSet.EMPTY;
397+
if (SHOULD_CHECK && !putFlagSet.isSet(MDB_MULTIPLE)) {
301398
throw new IllegalArgumentException("Must set " + MDB_MULTIPLE + " flag");
302399
}
303400
final Pointer transientKey = txn.kv().keyIn(key);
304401
final Pointer dataPtr = txn.kv().valInMulti(val, elements);
305-
final int rc = LIB.mdb_cursor_put(ptrCursor, txn.kv().pointerKey(), dataPtr, mask);
402+
final int rc = LIB.mdb_cursor_put(ptrCursor, txn.kv().pointerKey(), dataPtr, putFlagSet.getMask());
306403
checkRc(rc);
307404
ReferenceUtil.reachabilityFence0(transientKey);
308405
ReferenceUtil.reachabilityFence0(dataPtr);
@@ -334,6 +431,8 @@ public void renew(final Txn<T> newTxn) {
334431
}
335432

336433
/**
434+
* @deprecated Use {@link Cursor#reserve(Object, int, PutFlagSet)} instead.
435+
* <hr>
337436
* Reserve space for data of the given size, but don't copy the given val. Instead, return a
338437
* pointer to the reserved space, which the caller can fill in later - before the next update
339438
* operation or the transaction ends. This saves an extra memcpy if the data is being generated
@@ -344,10 +443,46 @@ public void renew(final Txn<T> newTxn) {
344443
*
345444
* @param key key to store in the database (not null)
346445
* @param size size of the value to be stored in the database (not null)
347-
* @param op options for this operation
446+
* @param flags options for this operation
447+
* @return a buffer that can be used to modify the value
448+
*/
449+
@Deprecated
450+
public T reserve(final T key, final int size, final PutFlags... flags) {
451+
return reserve(key, size, PutFlagSet.of(flags));
452+
}
453+
454+
/**
455+
* Reserve space for data of the given size, but don't copy the given val. Instead, return a
456+
* pointer to the reserved space, which the caller can fill in later - before the next update
457+
* operation or the transaction ends. This saves an extra {@code memcpy} if the data is being generated
458+
* later. LMDB does nothing else with this memory, the caller is expected to modify all the
459+
* space requested.
460+
*
461+
* <p>This flag must not be specified if the database was opened with MDB_DUPSORT
462+
*
463+
* @param key key to store in the database (not null)
464+
* @param size size of the value to be stored in the database (not null)
465+
* @return a buffer that can be used to modify the value
466+
*/
467+
public T reserve(final T key, final int size) {
468+
return reserve(key, size, PutFlagSet.EMPTY);
469+
}
470+
471+
/**
472+
* Reserve space for data of the given size, but don't copy the given val. Instead, return a
473+
* pointer to the reserved space, which the caller can fill in later - before the next update
474+
* operation or the transaction ends. This saves an extra memcpy if the data is being generated
475+
* later. LMDB does nothing else with this memory, the caller is expected to modify all of the
476+
* space requested.
477+
*
478+
* <p>This flag must not be specified if the database was opened with MDB_DUPSORT
479+
*
480+
* @param key key to store in the database (not null)
481+
* @param size size of the value to be stored in the database (not null)
482+
* @param flags options for this operation
348483
* @return a buffer that can be used to modify the value
349484
*/
350-
public T reserve(final T key, final int size, final PutFlags... op) {
485+
public T reserve(final T key, final int size, final PutFlagSet flags) {
351486
if (SHOULD_CHECK) {
352487
requireNonNull(key);
353488
env.checkNotClosed();
@@ -357,8 +492,12 @@ public T reserve(final T key, final int size, final PutFlags... op) {
357492
}
358493
final Pointer transientKey = kv.keyIn(key);
359494
final Pointer transientVal = kv.valIn(size);
360-
final int flags = mask(op) | MDB_RESERVE.getMask();
361-
checkRc(LIB.mdb_cursor_put(ptrCursor, kv.pointerKey(), kv.pointerVal(), flags));
495+
final PutFlagSet putFlagSet = flags != null
496+
? flags
497+
: PutFlagSet.EMPTY;
498+
// This is inconsistent with putMultiple which require MDB_MULTIPLE to be in the set.
499+
final int flagsMask = putFlagSet.getMaskWith(MDB_RESERVE);
500+
checkRc(LIB.mdb_cursor_put(ptrCursor, kv.pointerKey(), kv.pointerVal(), flagsMask));
362501
kv.valOut();
363502
ReferenceUtil.reachabilityFence0(transientKey);
364503
ReferenceUtil.reachabilityFence0(transientVal);

0 commit comments

Comments
 (0)