Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
480e984
gh-249 Remove MDB_UNSIGNEDKEY, let CursorIterable call mdb_cmp
at055612 Mar 6, 2025
46f8d08
gh-249 Remove non-J8 features, use indent size 2
at055612 Mar 6, 2025
f92012e
gh-249 Fix indents
at055612 Mar 6, 2025
e1756d6
gh-249 Tidy code and refactor RangeComparator impls
at055612 Mar 6, 2025
9509f6b
gh-249 Remove commented code
at055612 Mar 6, 2025
67e2df1
gh-249 Add CursorIterableIntegerKeyTest
at055612 Mar 7, 2025
620a89f
gh-249 Remove MaskedFlag.isPropagatedToLmdb, add DbiBuilder WIP
at055612 Jun 5, 2025
75d87d0
Merge branch 'master' into mdb_cmp
at055612 Oct 26, 2025
47b4c4f
Merge branch 'master' into mdb_cmp
at055612 Oct 26, 2025
bfbf223
Add FlagSet, DbiFlagSet, PutFlagSet
at055612 Oct 27, 2025
0f66aaf
Rename FlagSet to AbstractFlagSet
at055612 Oct 27, 2025
aa000a1
Add remaining FlagSet impls
at055612 Oct 27, 2025
667dab3
Fix Javadoc
at055612 Oct 27, 2025
b46c916
Add more cursor tests to protect against future regression
stroomdev66 Oct 28, 2025
0234d32
Replace get(Uns|S)ignedComparator with getComparator(DbiFlagSet)
at055612 Oct 28, 2025
ef0c852
Add missing txn commit in DbiBuilder
at055612 Oct 28, 2025
1b3f94d
Change CursorIterableTest to use Parameterized
at055612 Oct 28, 2025
c0bbe73
Deprecate methods using varargs flags
at055612 Oct 28, 2025
4fd89ff
Add int key compare method to (Direct|Byte)BufferProxy
at055612 Oct 29, 2025
916b441
Merge branch 'master' of github.com:lmdbjava/lmdbjava into more-curso…
stroomdev66 Oct 29, 2025
58dcc6e
Tidy code
at055612 Oct 29, 2025
26665ba
Fix byte order issues with compareAsIntegerKeys
at055612 Oct 29, 2025
dc0b96b
#267 Fix to establish start key when iterating backwards from a start…
stroomdev66 Oct 29, 2025
9db91a5
Merge branch 'master' of github.com:lmdbjava/lmdbjava into gh-267
stroomdev66 Oct 30, 2025
c427801
Add/refactor tests
at055612 Nov 4, 2025
f606e7e
Merge branch 'master' into mdb_cmp
at055612 Nov 4, 2025
e2be6bf
Add integer key comparator tests
at055612 Nov 5, 2025
7374fde
#267 Fix to establish start key when iterating backwards from a start…
stroomdev66 Nov 5, 2025
79c0df6
Merge branch 'master' of github.com:lmdbjava/lmdbjava into gh-267
stroomdev66 Nov 5, 2025
787d6d3
#267 Fix to establish start key when iterating backwards from a start…
stroomdev66 Nov 5, 2025
c289763
#267 Fix to establish start key when iterating backwards from a start…
stroomdev66 Nov 5, 2025
1acd971
Add ComparatorFactory
at055612 Nov 5, 2025
69eb5b7
#269 Initial iterator performance enhancements and testing.
stroomdev66 Nov 5, 2025
915edce
#269 Initial iterator performance enhancements and testing.
stroomdev66 Nov 5, 2025
dea7975
Merge branch 'master' into mdb_cmp
at055612 Nov 5, 2025
5a62965
Merged new comparator code
stroomdev66 Nov 5, 2025
484a772
Merged new comparator code
stroomdev66 Nov 5, 2025
ad75cb2
Fix CodeQL issues, add setMapSizeXx methods
at055612 Nov 5, 2025
77a3494
Fix failing test
at055612 Nov 5, 2025
1d46e0b
Refactor deprecated method calls
at055612 Nov 6, 2025
577062f
Fix compile issue
at055612 Nov 6, 2025
12a9679
Improve FlagSet tests
at055612 Nov 6, 2025
7a6910b
Add licence header
at055612 Nov 6, 2025
167cdad
Rename buildDbi to createDbi to be consistent with Env.create()
at055612 Nov 6, 2025
d47694e
Remove ByteUnits dep, add ByteUnit enum, add setMapSize() overload
at055612 Nov 6, 2025
fb08779
Fix compile errors
at055612 Nov 6, 2025
a82d7f2
Add more test coverage
at055612 Nov 6, 2025
534b76b
Fix codeQL issue
at055612 Nov 6, 2025
e9f9ac4
Add test coverage on FlagSet
at055612 Nov 6, 2025
b0112bb
Add tests
at055612 Nov 6, 2025
c25a7d2
Run format plugin
at055612 Nov 7, 2025
cf3a11e
Remove class separators
at055612 Nov 7, 2025
0c97cf4
Run format plugin
at055612 Nov 7, 2025
72d4e3f
Remove binaries accidentally added, fix test
at055612 Nov 7, 2025
18013fb
Merged new comparator code
stroomdev66 Nov 7, 2025
6b40d0b
Merge branch 'gh-267_plus_new_comparators' into gh-249-remove-unsigne…
at055612 Nov 7, 2025
48aa0fd
Fix javadoc warnings and class visibility, fix licence warnings
at055612 Nov 7, 2025
72b5234
Auto-format
at055612 Nov 7, 2025
dd95dea
Add error handling for parse(Int|Long)
at055612 Nov 7, 2025
b53378a
Remove unused code
at055612 Nov 7, 2025
deb1f1b
Remove unused code
at055612 Nov 7, 2025
2bfe07c
Remove unused hashCode methods
at055612 Nov 7, 2025
a84ceaa
Change CursorIterableRangeTest to do default+native modes
at055612 Nov 9, 2025
a5e4cf2
Remove separator comments
at055612 Nov 9, 2025
f8e6e07
Add more tests to CursorIterableRangeTest
at055612 Nov 9, 2025
6982aca
Remove unused test, auto format
at055612 Nov 9, 2025
6740e2b
Add tests to stop CodeQL warnings
at055612 Nov 12, 2025
0103f13
Auto format
at055612 Nov 12, 2025
4249566
Fix typo in javadoc
at055612 Nov 12, 2025
35b0a9d
Tidy code
at055612 Nov 12, 2025
9a35718
Add in test classes for deprecated methods
at055612 Nov 14, 2025
8c19bf4
Add test coverage
at055612 Nov 14, 2025
496e5ea
Add test coverage
at055612 Nov 14, 2025
7bd3332
Auto format
at055612 Nov 14, 2025
06523f9
Add more tests
at055612 Nov 14, 2025
fe41338
Add test coverage
at055612 Nov 14, 2025
38eac0d
Add ByteBufProxyTest, refactor some test classes
at055612 Nov 14, 2025
53ac34d
Add missing licences
at055612 Nov 14, 2025
5ad4f0c
Add test assert
at055612 Nov 14, 2025
a0503d3
Add TxnDeprecatedTest for more coverage
at055612 Nov 17, 2025
29d5ba2
Auto format
at055612 Nov 17, 2025
1312e10
Add tests for XxxFlags.isSet
at055612 Nov 17, 2025
0216c92
Add more test coverage
at055612 Nov 17, 2025
8b60f29
Change FlagSet args to be requireNonNull
at055612 Nov 17, 2025
e53956d
Add requireNonNull checks to Dbi
at055612 Nov 17, 2025
654a5bc
Add more coverage
at055612 Nov 17, 2025
850c4e2
Add DirectBufferProxyTest
at055612 Nov 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add integer key comparator tests
  • Loading branch information
at055612 committed Nov 5, 2025
commit e2be6bf145a100fb2b8d40446dd3b70a54f51e2d
2 changes: 0 additions & 2 deletions src/main/java/org/lmdbjava/AbstractFlagSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

/**
* Encapsulates an immutable set of flags and the associated bit mask for the flags in the set.
*
* @param <T>
*/
public abstract class AbstractFlagSet<T extends Enum<T> & MaskedFlag> implements FlagSet<T> {

Expand Down
74 changes: 66 additions & 8 deletions src/main/java/org/lmdbjava/ByteBufProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import java.lang.reflect.Field;
import java.nio.ByteOrder;
import java.util.Comparator;
import jnr.ffi.Pointer;

Expand All @@ -44,13 +45,6 @@ public final class ByteBufProxy extends BufferProxy<ByteBuf> {
private static final String FIELD_NAME_ADDRESS = "memoryAddress";
private static final String FIELD_NAME_LENGTH = "length";
private static final String NAME = "io.netty.buffer.PooledUnsafeDirectByteBuf";
private static final Comparator<ByteBuf> comparator =
(o1, o2) -> {
requireNonNull(o1);
requireNonNull(o2);

return o1.compareTo(o2);
};
private final long lengthOffset;
private final long addressOffset;

Expand Down Expand Up @@ -81,6 +75,66 @@ public ByteBufProxy(final PooledByteBufAllocator allocator) {
}
}

/**
* Lexicographically compare two buffers.
*
* @param o1 left operand (required)
* @param o2 right operand (required)
* @return as specified by {@link Comparable} interface
*/
public static int compareLexicographically(final ByteBuf o1, final ByteBuf o2) {
requireNonNull(o1);
requireNonNull(o2);
return o1.compareTo(o2);
}

/**
* Buffer comparator specifically for 4/8 byte keys that are unsigned ints/longs,
* i.e. when using MDB_INTEGER_KEY/MDB_INTEGERDUP. Compares the buffers numerically.
*
* @param o1 left operand (required)
* @param o2 right operand (required)
* @return as specified by {@link Comparable} interface
*/
public static int compareAsIntegerKeys(final ByteBuf o1, final ByteBuf o2) {
requireNonNull(o1);
requireNonNull(o2);
// Both buffers should be same length according to LMDB API.
// From the LMDB docs for MDB_INTEGER_KEY
// numeric keys in native byte order: either unsigned int or size_t. The keys must all be of the same size.
final int len1 = o1.readableBytes();
final int len2 = o2.readableBytes();
if (len1 != len2) {
throw new RuntimeException("Length mismatch, len1: " + len1 + ", len2: " + len2
+ ". Lengths must be identical and either 4 or 8 bytes.");
}
if (len1 == 8) {
final long lw;
final long rw;
if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
lw = o1.readLongLE();
rw = o2.readLongLE();
} else {
lw = o1.readLong();
rw = o2.readLong();
}
return Long.compareUnsigned(lw, rw);
} else if (len1 == 4) {
final int lw;
final int rw;
if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
lw = o1.readIntLE();
rw = o2.readIntLE();
} else {
lw = o1.readInt();
rw = o2.readInt();
}
return Integer.compareUnsigned(lw, rw);
} else {
return compareLexicographically(o1, o2);
}
}

static Field findField(final String c, final String name) {
Class<?> clazz;
try {
Expand Down Expand Up @@ -115,7 +169,11 @@ protected ByteBuf allocate() {

@Override
public Comparator<ByteBuf> getComparator(final DbiFlagSet dbiFlagSet) {
return comparator;
if (dbiFlagSet.areAnySet(DbiFlagSet.INTEGER_KEY_FLAGS)) {
return ByteBufProxy::compareAsIntegerKeys;
} else {
return ByteBufProxy::compareLexicographically;
}
}

@Override
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/lmdbjava/ByteBufferProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public final class ByteBufferProxy {
*/
public static final BufferProxy<ByteBuffer> PROXY_SAFE;

private static final ByteOrder NATIVE_ORDER = ByteOrder.nativeOrder();

static {
PROXY_SAFE = new ReflectiveProxy();
Expand Down Expand Up @@ -172,8 +173,8 @@ public static int compareAsIntegerKeys(final ByteBuffer o1, final ByteBuffer o2)
+ ". Lengths must be identical and either 4 or 8 bytes.");
}
// Keys for MDB_INTEGER_KEY are written in native order so ensure we read them in that order
o1.order(ByteOrder.nativeOrder());
o2.order(ByteOrder.nativeOrder());
o1.order(NATIVE_ORDER);
o2.order(NATIVE_ORDER);
// TODO it might be worth the DbiBuilder having a method to capture fixedKeyLength() or -1
// for variable length keys. This can be passed to getComparator(..) so it can return a
// comparator that doesn't need to test the length every time. There may be other benefits
Expand Down
38 changes: 19 additions & 19 deletions src/main/java/org/lmdbjava/DbiBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,20 @@
* (see also {@link DbiBuilder#withoutDbName()})
* @return The next builder stage.
*/
public DbiBuilderStage2<T> withDbName(final String name) {
public DbiBuilderStage2<T> setDbName(final String name) {
// Null name is allowed so no null check
final byte[] nameBytes = name == null
? null
: name.getBytes(Env.DEFAULT_NAME_CHARSET);
return withDbName(nameBytes);
return setDbName(nameBytes);
}

/**
* Create the {@link Dbi} with the passed name in byte[] form.
* @param name The name of the database in byte form.
* @return The next builder stage.
*/
public DbiBuilderStage2<T> withDbName(final byte[] name) {
public DbiBuilderStage2<T> setDbName(final byte[] name) {
// Null name is allowed so no null check
this.name = name;
return new DbiBuilderStage2<>(this);
Expand All @@ -78,15 +78,15 @@
* </p>
* <p>
* Equivalent to passing null to
* {@link DbiBuilder#withDbName(String)} or {@link DbiBuilder#withDbName(byte[])}.
* {@link DbiBuilder#setDbName(String)} or {@link DbiBuilder#setDbName(byte[])}.
* </p>
* <p>Note: The 'unnamed database' is used by LMDB to store the names of named databases, with
* the database name being the key. Use of the unnamed database is intended for simple applications
* with only one database.</p>
* @return The next builder stage.
*/
public DbiBuilderStage2<T> withoutDbName() {
return withDbName((byte[]) null);
return setDbName((byte[]) null);
}


Expand Down Expand Up @@ -254,16 +254,16 @@
* </p>
* <p>
* Clears all flags currently set by previous calls to
* {@link DbiBuilderStage3#withDbiFlags(Collection)},
* {@link DbiBuilderStage3#withDbiFlags(DbiFlags...)}
* {@link DbiBuilderStage3#setDbiFlags(Collection)},
* {@link DbiBuilderStage3#setDbiFlags(DbiFlags...)}
* or {@link DbiBuilderStage3#addDbiFlag(DbiFlags)}.
* </p>
*
* @param dbiFlags to open the database with.
* A null {@link Collection} will just clear all set flags.
* Null items are ignored.
*/
public DbiBuilderStage3<T> withDbiFlags(final Collection<DbiFlags> dbiFlags) {
public DbiBuilderStage3<T> setDbiFlags(final Collection<DbiFlags> dbiFlags) {
flagSetBuilder.clear();
if (dbiFlags != null) {
dbiFlags.stream()
Expand All @@ -279,16 +279,16 @@
* </p>
* <p>
* Clears all flags currently set by previous calls to
* {@link DbiBuilderStage3#withDbiFlags(Collection)},
* {@link DbiBuilderStage3#withDbiFlags(DbiFlags...)}
* {@link DbiBuilderStage3#setDbiFlags(Collection)},
* {@link DbiBuilderStage3#setDbiFlags(DbiFlags...)}
* or {@link DbiBuilderStage3#addDbiFlag(DbiFlags)}.
* </p>
*
* @param dbiFlags to open the database with.
* A null array will just clear all set flags.
* Null items are ignored.
*/
public DbiBuilderStage3<T> withDbiFlags(final DbiFlags... dbiFlags) {
public DbiBuilderStage3<T> setDbiFlags(final DbiFlags... dbiFlags) {
flagSetBuilder.clear();
if (dbiFlags != null) {
Arrays.stream(dbiFlags)
Expand All @@ -304,15 +304,15 @@
* </p>
* <p>
* Clears all flags currently set by previous calls to
* {@link DbiBuilderStage3#withDbiFlags(Collection)},
* {@link DbiBuilderStage3#withDbiFlags(DbiFlags...)}
* {@link DbiBuilderStage3#setDbiFlags(Collection)},
* {@link DbiBuilderStage3#setDbiFlags(DbiFlags...)}
* or {@link DbiBuilderStage3#addDbiFlag(DbiFlags)}.
* </p>
*
* @param dbiFlagSet to open the database with.
* A null value will just clear all set flags.
*/
public DbiBuilderStage3<T> withDbiFlags(final DbiFlagSet dbiFlagSet) {
public DbiBuilderStage3<T> setDbiFlags(final DbiFlagSet dbiFlagSet) {
flagSetBuilder.clear();
if (dbiFlagSet != null) {
this.flagSetBuilder.withFlags(dbiFlagSet.getFlags());
Expand All @@ -322,8 +322,8 @@

/**
* Adds a dbiFlag to those flags already added to this builder by
* {@link DbiBuilderStage3#withDbiFlags(DbiFlags...)},
* {@link DbiBuilderStage3#withDbiFlags(Collection)}
* {@link DbiBuilderStage3#setDbiFlags(DbiFlags...)},
* {@link DbiBuilderStage3#setDbiFlags(Collection)}
* or {@link DbiBuilderStage3#addDbiFlag(DbiFlags)}.
*
* @param dbiFlag to add to any existing flags. A null value is a no-op.
Expand All @@ -336,8 +336,8 @@

/**
* Adds a dbiFlag to those flags already added to this builder by
* {@link DbiBuilderStage3#withDbiFlags(DbiFlags...)},
* {@link DbiBuilderStage3#withDbiFlags(Collection)}
* {@link DbiBuilderStage3#setDbiFlags(DbiFlags...)},
* {@link DbiBuilderStage3#setDbiFlags(Collection)}
* or {@link DbiBuilderStage3#addDbiFlag(DbiFlags)}.
*
* @param dbiFlagSet to add to any existing flags. A null value is a no-op.
Expand Down Expand Up @@ -366,8 +366,8 @@
* with the {@link EnvFlags#MDB_RDONLY_ENV} flag, the {@link Txn} can be read-only,
* else it needs to be a read/write {@link Txn}.
* @return this builder instance.
*/
public DbiBuilderStage3<T> withTxn(final Txn<T> txn) {
public DbiBuilderStage3<T> setTxn(final Txn<T> txn) {
this.txn = Objects.requireNonNull(txn);
return this;
}
Expand Down
18 changes: 12 additions & 6 deletions src/main/java/org/lmdbjava/DirectBufferProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.lmdbjava.UnsafeAccess.UNSAFE;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayDeque;
import java.util.Comparator;
import jnr.ffi.Pointer;
Expand Down Expand Up @@ -50,6 +51,8 @@ public final class DirectBufferProxy extends BufferProxy<DirectBuffer> {
private static final ThreadLocal<ArrayDeque<DirectBuffer>> BUFFERS =
withInitial(() -> new ArrayDeque<>(16));

private static final ByteOrder NATIVE_ORDER = ByteOrder.nativeOrder();

private DirectBufferProxy() {}

/**
Expand Down Expand Up @@ -110,16 +113,19 @@ public static int compareAsIntegerKeys(final DirectBuffer o1, final DirectBuffer
+ ". Lengths must be identical and either 4 or 8 bytes.");
}
if (len1 == 8) {
final long lw = o1.getLong(0, BIG_ENDIAN);
final long rw = o2.getLong(0, BIG_ENDIAN);
final long lw = o1.getLong(0, NATIVE_ORDER);
final long rw = o2.getLong(0, NATIVE_ORDER);
return Long.compareUnsigned(lw, rw);
} else if (len1 == 4) {
final int lw = o1.getInt(0, BIG_ENDIAN);
final int rw = o2.getInt(0, BIG_ENDIAN);
final int lw = o1.getInt(0, NATIVE_ORDER);
final int rw = o2.getInt(0, NATIVE_ORDER);
return Integer.compareUnsigned(lw, rw);
} else {
throw new RuntimeException("Unexpected length len1: " + len1 + ", len2: " + len2
+ ". Lengths must be identical and either 4 or 8 bytes.");
// size_t and int are likely to be 8bytes and 4bytes respectively on 64bit.
// If 32bit then would be 4/2 respectively.
// Short.compareUnsigned is not available in Java8.
// For now just fall back to our standard comparator
return compareLexicographically(o1, o2);
}
}

Expand Down
Loading
Loading