Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 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
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
58dcc6e
Tidy code
at055612 Oct 29, 2025
26665ba
Fix byte order issues with compareAsIntegerKeys
at055612 Oct 29, 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
1acd971
Add ComparatorFactory
at055612 Nov 5, 2025
dea7975
Merge branch 'master' into mdb_cmp
at055612 Nov 5, 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 @@

/**
Comment thread Fixed
* 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