Skip to content

Commit ad75cb2

Browse files
committed
Fix CodeQL issues, add setMapSizeXx methods
1 parent dea7975 commit ad75cb2

File tree

12 files changed

+404
-290
lines changed

12 files changed

+404
-290
lines changed

src/main/java/org/lmdbjava/CursorIterable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public KeyVal<T> next() {
115115

116116
@Override
117117
public void remove() {
118-
cursor.delete();
118+
cursor.delete(PutFlags.EMPTY);
119119
}
120120
};
121121
}

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

Lines changed: 67 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
*/
5050
public final class Dbi<T> {
5151

52-
private final ComparatorCallback callbackComparator;
5352
private boolean cleaned;
5453
// Used for CursorIterable KeyRange testing and/or native callbacks
5554
private final Comparator<T> comparator;
@@ -59,6 +58,14 @@ public final class Dbi<T> {
5958
private final BufferProxy<T> proxy;
6059
private final DbiFlagSet dbiFlagSet;
6160

61+
Dbi(final Env<T> env,
62+
final Txn<T> txn,
63+
final byte[] name,
64+
final BufferProxy<T> proxy,
65+
final DbiFlagSet dbiFlagSet) {
66+
this(env, txn, name, null, false, proxy, dbiFlagSet);
67+
}
68+
6269
Dbi(
6370
final Env<T> env,
6471
final Txn<T> txn,
@@ -67,48 +74,44 @@ public final class Dbi<T> {
6774
final boolean nativeCb,
6875
final BufferProxy<T> proxy,
6976
final DbiFlagSet dbiFlagSet) {
77+
7078
if (SHOULD_CHECK) {
79+
if (nativeCb && comparator == null) {
80+
throw new IllegalArgumentException("Is nativeCb is true, you must supply a comparator");
81+
}
7182
requireNonNull(txn);
7283
txn.checkReady();
7384
}
7485
this.env = env;
7586
this.name = name == null ? null : Arrays.copyOf(name, name.length);
7687
this.proxy = proxy;
7788
this.comparator = comparator;
78-
this.dbiFlagSet = dbiFlagSet;
89+
this.dbiFlagSet = dbiFlagSet == null ? DbiFlagSet.EMPTY : dbiFlagSet;
7990
final Pointer dbiPtr = allocateDirect(RUNTIME, ADDRESS);
80-
checkRc(LIB.mdb_dbi_open(txn.pointer(), name, dbiFlagSet.getMask(), dbiPtr));
91+
checkRc(LIB.mdb_dbi_open(txn.pointer(), name, this.dbiFlagSet.getMask(), dbiPtr));
8192
ptr = dbiPtr.getPointer(0);
93+
ComparatorCallback callbackComparator;
8294
if (nativeCb) {
83-
requireNonNull(comparator, "comparator cannot be null if nativeCb is set");
8495
// LMDB will call back to this comparator for insertion/iteration order
85-
// if (dbiFlagSet.areAnySet(DbiFlagSet.INTEGER_KEY_FLAGS)) {
86-
// this.callbackComparator =
87-
// (keyA, keyB) -> {
88-
// final T compKeyA = proxy.out(proxy.allocate(), keyA);
89-
// final T compKeyB = proxy.out(proxy.allocate(), keyB);
90-
// final int result = this.comparator.compare(compKeyA, compKeyB);
91-
// proxy.deallocate(compKeyA);
92-
// proxy.deallocate(compKeyB);
93-
// return result;
94-
// };
95-
// } else {
96-
this.callbackComparator =
97-
(keyA, keyB) -> {
98-
final T compKeyA = proxy.out(proxy.allocate(), keyA);
99-
final T compKeyB = proxy.out(proxy.allocate(), keyB);
100-
final int result = this.comparator.compare(compKeyA, compKeyB);
101-
proxy.deallocate(compKeyA);
102-
proxy.deallocate(compKeyB);
103-
return result;
104-
};
105-
// }
96+
callbackComparator = createCallbackComparator(proxy);
10697
LIB.mdb_set_compare(txn.pointer(), ptr, callbackComparator);
107-
} else {
108-
callbackComparator = null;
10998
}
11099
}
111100

101+
private ComparatorCallback createCallbackComparator(BufferProxy<T> proxy) {
102+
ComparatorCallback callbackComparator;
103+
callbackComparator =
104+
(keyA, keyB) -> {
105+
final T compKeyA = proxy.out(proxy.allocate(), keyA);
106+
final T compKeyB = proxy.out(proxy.allocate(), keyB);
107+
final int result = this.comparator.compare(compKeyA, compKeyB);
108+
proxy.deallocate(compKeyA);
109+
proxy.deallocate(compKeyB);
110+
return result;
111+
};
112+
return callbackComparator;
113+
}
114+
112115
Pointer pointer() {
113116
return ptr;
114117
}
@@ -215,7 +218,7 @@ public void drop(final Txn<T> txn) {
215218
* closed. See {@link #close()} for implication of handle close. Otherwise, only the data in this
216219
* database will be dropped.
217220
*
218-
* @param txn transaction handle (not null; not committed; must be R-W)
221+
* @param txn transaction handle (not null; not committed; must be R-W)
219222
* @param delete whether database should be deleted.
220223
*/
221224
public void drop(final Txn<T> txn, final boolean delete) {
@@ -311,7 +314,7 @@ public CursorIterable<T> iterate(final Txn<T> txn) {
311314
/**
312315
* Iterate the database in accordance with the provided {@link KeyRange}.
313316
*
314-
* @param txn transaction handle (not null; not committed)
317+
* @param txn transaction handle (not null; not committed)
315318
* @param range range of acceptable keys (not null)
316319
* @return iterator (never null)
317320
*/
@@ -392,6 +395,12 @@ public void put(final T key, final T val) {
392395
}
393396

394397
/**
398+
* @param txn transaction handle (not null; not committed; must be R-W)
399+
* @param key key to store in the database (not null)
400+
* @param val value to store in the database (not null)
401+
* @param flags Special options for this operation
402+
* @return true if the value was put, false if MDB_NOOVERWRITE or MDB_NODUPDATA were set and the
403+
* key/value existed already.
395404
* @deprecated Use {@link Dbi#put(Txn, Object, Object, PutFlagSet)} instead, with a statically
396405
* held {@link PutFlagSet}.
397406
* <hr>
@@ -401,13 +410,6 @@ public void put(final T key, final T val) {
401410
* <p>This function stores key/data pairs in the database. The default behavior is to enter the
402411
* new key/data pair, replacing any previously existing key if duplicates are disallowed, or
403412
* adding a duplicate data item if duplicates are allowed ({@link DbiFlags#MDB_DUPSORT}).
404-
*
405-
* @param txn transaction handle (not null; not committed; must be R-W)
406-
* @param key key to store in the database (not null)
407-
* @param val value to store in the database (not null)
408-
* @param flags Special options for this operation
409-
* @return true if the value was put, false if MDB_NOOVERWRITE or MDB_NODUPDATA were set and the
410-
* key/value existed already.
411413
*/
412414
@Deprecated
413415
public boolean put(final Txn<T> txn, final T key, final T val, final PutFlags... flags) {
@@ -421,7 +423,7 @@ public boolean put(final Txn<T> txn, final T key, final T val, final PutFlags...
421423
* @param key key to store in the database (not null)
422424
* @param val value to store in the database (not null)
423425
* @return true if the value was put, false if MDB_NOOVERWRITE or MDB_NODUPDATA were set and the
424-
* key/value existed already.
426+
* key/value existed already.
425427
* @see #put(Txn, Object, Object, PutFlagSet)
426428
*/
427429
public boolean put(final Txn<T> txn, final T key, final T val) {
@@ -435,12 +437,12 @@ public boolean put(final Txn<T> txn, final T key, final T val) {
435437
* new key/data pair, replacing any previously existing key if duplicates are disallowed, or
436438
* adding a duplicate data item if duplicates are allowed ({@link DbiFlags#MDB_DUPSORT}).
437439
*
438-
* @param txn transaction handle (not null; not committed; must be R-W)
439-
* @param key key to store in the database (not null)
440-
* @param val value to store in the database (not null)
440+
* @param txn transaction handle (not null; not committed; must be R-W)
441+
* @param key key to store in the database (not null)
442+
* @param val value to store in the database (not null)
441443
* @param flags Special options for this operation.
442444
* @return true if the value was put, false if MDB_NOOVERWRITE or MDB_NODUPDATA were set and the
443-
* key/value existed already.
445+
* key/value existed already.
444446
*/
445447
public boolean put(final Txn<T> txn, final T key, final T val, final PutFlagSet flags) {
446448
if (SHOULD_CHECK) {
@@ -480,10 +482,10 @@ public boolean put(final Txn<T> txn, final T key, final T val, final PutFlagSet
480482
*
481483
* <p>This flag must not be specified if the database was opened with MDB_DUPSORT
482484
*
483-
* @param txn transaction handle (not null; not committed; must be R-W)
484-
* @param key key to store in the database (not null)
485+
* @param txn transaction handle (not null; not committed; must be R-W)
486+
* @param key key to store in the database (not null)
485487
* @param size size of the value to be stored in the database
486-
* @param op options for this operation
488+
* @param op options for this operation
487489
* @return a buffer that can be used to modify the value
488490
*/
489491
public T reserve(final Txn<T> txn, final T key, final int size, final PutFlags... op) {
@@ -544,12 +546,14 @@ public String toString() {
544546
name = "?";
545547
}
546548
return "Dbi{" +
547-
"name='" + name +
548-
"', dbiFlagSet=" + dbiFlagSet +
549-
'}';
549+
"name='" + name +
550+
"', dbiFlagSet=" + dbiFlagSet +
551+
'}';
550552
}
551553

552-
/** The specified DBI was changed unexpectedly. */
554+
/**
555+
* The specified DBI was changed unexpectedly.
556+
*/
553557
public static final class BadDbiException extends LmdbNativeException {
554558

555559
static final int MDB_BAD_DBI = -30_780;
@@ -560,7 +564,9 @@ public static final class BadDbiException extends LmdbNativeException {
560564
}
561565
}
562566

563-
/** Unsupported size of key/DB name/data, or wrong DUPFIXED size. */
567+
/**
568+
* Unsupported size of key/DB name/data, or wrong DUPFIXED size.
569+
*/
564570
public static final class BadValueSizeException extends LmdbNativeException {
565571

566572
static final int MDB_BAD_VALSIZE = -30_781;
@@ -571,7 +577,9 @@ public static final class BadValueSizeException extends LmdbNativeException {
571577
}
572578
}
573579

574-
/** Environment maxdbs reached. */
580+
/**
581+
* Environment maxdbs reached.
582+
*/
575583
public static final class DbFullException extends LmdbNativeException {
576584

577585
static final int MDB_DBS_FULL = -30_791;
@@ -604,7 +612,9 @@ public static final class IncompatibleException extends LmdbNativeException {
604612
}
605613
}
606614

607-
/** Key/data pair already exists. */
615+
/**
616+
* Key/data pair already exists.
617+
*/
608618
public static final class KeyExistsException extends LmdbNativeException {
609619

610620
static final int MDB_KEYEXIST = -30_799;
@@ -615,7 +625,9 @@ public static final class KeyExistsException extends LmdbNativeException {
615625
}
616626
}
617627

618-
/** Key/data pair not found (EOF). */
628+
/**
629+
* Key/data pair not found (EOF).
630+
*/
619631
public static final class KeyNotFoundException extends LmdbNativeException {
620632

621633
static final int MDB_NOTFOUND = -30_798;
@@ -626,7 +638,9 @@ public static final class KeyNotFoundException extends LmdbNativeException {
626638
}
627639
}
628640

629-
/** Database contents grew beyond environment mapsize. */
641+
/**
642+
* Database contents grew beyond environment mapsize.
643+
*/
630644
public static final class MapResizedException extends LmdbNativeException {
631645

632646
static final int MDB_MAP_RESIZED = -30_785;

src/main/java/org/lmdbjava/DbiBuilder.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,10 @@ public DbiBuilderStage3<T> setTxn(final Txn<T> txn) {
391391
public Dbi<T> open() {
392392
final DbiBuilder<T> dbiBuilder = dbiBuilderStage2.dbiBuilder;
393393
if (txn != null) {
394-
return open(txn, dbiBuilder);
394+
return openDbi(txn, dbiBuilder);
395395
} else {
396396
try (final Txn<T> txn = getTxn(dbiBuilder)) {
397-
final Dbi<T> dbi = open(txn, dbiBuilder);
397+
final Dbi<T> dbi = openDbi(txn, dbiBuilder);
398398
// even RO Txns require a commit to retain Dbi in Env
399399
txn.commit();
400400
return dbi;
@@ -432,8 +432,8 @@ private Comparator<T> getComparator(final DbiBuilder<T> dbiBuilder,
432432
return comparator;
433433
}
434434

435-
private Dbi<T> open(final Txn<T> txn,
436-
final DbiBuilder<T> dbiBuilder) {
435+
private Dbi<T> openDbi(final Txn<T> txn,
436+
final DbiBuilder<T> dbiBuilder) {
437437
final DbiFlagSet dbiFlagSet = flagSetBuilder.build();
438438
final ComparatorType comparatorType = dbiBuilderStage2.comparatorType;
439439
final Comparator<T> comparator = getComparator(dbiBuilder, comparatorType, dbiFlagSet);

0 commit comments

Comments
 (0)