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
Replace get(Uns|S)ignedComparator with getComparator(DbiFlagSet)
Also improve javadoc and refactor some tests to use DbiBuilder.

Some tests are failing.
  • Loading branch information
at055612 committed Oct 28, 2025
commit 0234d323244f874ccf12945949e6352879c5a3b3
45 changes: 34 additions & 11 deletions src/main/java/org/lmdbjava/BufferProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,49 @@
protected abstract byte[] getBytes(T buffer);

/**
* Get a suitable default {@link Comparator} to compare numeric key values as signed.
* Get a suitable default {@link Comparator} given the provided flags.
*
* <p>Note: LMDB's default comparator is unsigned so if this is used only for the {@link
* CursorIterable} start/stop key comparisons then its behaviour will differ from the iteration
* order. Use with caution.
* <p>The provided comparator must strictly match the lexicographical order of keys in the native
* LMDB database.
*
* @param dbiFlagSet The {@link DbiFlags} set for the database.
* @return a comparator that can be used (never null)
*/
public abstract Comparator<T> getSignedComparator();
public abstract Comparator<T> getComparator(final DbiFlagSet dbiFlagSet);
Comment thread Fixed

/**
* Get a suitable default {@link Comparator} to compare numeric key values as unsigned.
* <p>
* This should match the behaviour of the LMDB's mdb_cmp comparator as it may be used for
* {@link CursorIterable} start/stop keys comparisons, which must match LMDB's insertion order.
* </p>
* Get a suitable default {@link Comparator}
*
* <p>The provided comparator must strictly match the lexicographical order of keys in the native
* LMDB database.
*
* @return a comparator that can be used (never null)
*/
public abstract Comparator<T> getUnsignedComparator();
public Comparator<T> getComparator() {
return getComparator(DbiFlagSet.empty());
}

// /**
// * Get a suitable default {@link Comparator} to compare numeric key values as signed.
// *
// * <p>Note: LMDB's default comparator is unsigned so if this is used only for the {@link
// * CursorIterable} start/stop key comparisons then its behaviour will differ from the iteration
// * order. Use with caution.
// *
// * @return a comparator that can be used (never null)
// */
// public abstract Comparator<T> getSignedComparator();
//
// /**
// * Get a suitable default {@link Comparator} to compare numeric key values as unsigned.
// * <p>
// * This should match the behaviour of the LMDB's mdb_cmp comparator as it may be used for
// * {@link CursorIterable} start/stop keys comparisons, which must match LMDB's insertion order.
// * </p>
// *
// * @return a comparator that can be used (never null)
// */
// public abstract Comparator<T> getUnsignedComparator(final DbiFlagSet dbiFlagSet);

/**
* Called when the <code>MDB_val</code> should be set to reflect the passed buffer. This buffer
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/org/lmdbjava/ByteArrayProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,20 @@ protected byte[] getBytes(final byte[] buffer) {
}

@Override
public Comparator<byte[]> getSignedComparator() {
return signedComparator;
}

@Override
public Comparator<byte[]> getUnsignedComparator() {
public Comparator<byte[]> getComparator(final DbiFlagSet dbiFlagSet) {
return unsignedComparator;
}

// @Override
// public Comparator<byte[]> getSignedComparator() {
// return signedComparator;
// }
//
// @Override
// public Comparator<byte[]> getUnsignedComparator() {
// return unsignedComparator;
// }

@Override
protected Pointer in(final byte[] buffer, final Pointer ptr) {
final Pointer pointer = MEM_MGR.allocateDirect(buffer.length);
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/org/lmdbjava/ByteBufProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,19 @@ protected ByteBuf allocate() {
}

@Override
public Comparator<ByteBuf> getSignedComparator() {
public Comparator<ByteBuf> getComparator(final DbiFlagSet dbiFlagSet) {
return comparator;
}

@Override
public Comparator<ByteBuf> getUnsignedComparator() {
return comparator;
}
// @Override
// public Comparator<ByteBuf> getSignedComparator() {
// return comparator;
// }
//
// @Override
// public Comparator<ByteBuf> getUnsignedComparator() {
// return comparator;
// }

@Override
protected void deallocate(final ByteBuf buff) {
Expand Down
46 changes: 40 additions & 6 deletions src/main/java/org/lmdbjava/ByteBufferProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,35 @@ public static int compareBuff(final ByteBuffer o1, final ByteBuffer o2) {
return o1.remaining() - o2.remaining();
}

// /**
// * Possible compareBuff method specifically for 4/8 byte keys when using MDB_INTEGER_KEY
// */
// public static int compareBuff(final ByteBuffer o1, final ByteBuffer o2) {
// requireNonNull(o1);
// requireNonNull(o2);
// // Both buffers should be same len
// final int len1 = o1.limit();
// final int len2 = o2.limit();
// if (len1 != len2) {
// throw new RuntimeException("Length mismatch, len1: " + len1 + ", len2: " + len2
// + ". Lengths must be identical and either 4 or 8 bytes.");
// }
// final boolean reverse1 = o1.order() == LITTLE_ENDIAN;
// final boolean reverse2 = o2.order() == LITTLE_ENDIAN;
// if (len1 == 8) {
// final long lw = reverse1 ? Long.reverseBytes(o1.getLong()) : o1.getLong();
// final long rw = reverse2 ? Long.reverseBytes(o2.getLong()) : o2.getLong();
// return Long.compareUnsigned(lw, rw);
// } else if (len1 == 4) {
// final int lw = reverse1 ? Integer.reverseBytes(o1.getInt()) : o1.getInt();
// final int rw = reverse2 ? Integer.reverseBytes(o2.getInt()) : o2.getInt();
// 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.");
// }
// }

static Field findField(final Class<?> c, final String name) {
Class<?> clazz = c;
do {
Expand Down Expand Up @@ -182,15 +211,20 @@ protected final ByteBuffer allocate() {
}

@Override
public Comparator<ByteBuffer> getSignedComparator() {
return signedComparator;
}

@Override
public Comparator<ByteBuffer> getUnsignedComparator() {
public Comparator<ByteBuffer> getComparator(DbiFlagSet dbiFlagSet) {
return unsignedComparator;
}

// @Override
// public Comparator<ByteBuffer> getSignedComparator() {
// return signedComparator;
// }
//
// @Override
// public Comparator<ByteBuffer> getUnsignedComparator(final DbiFlagSet dbiFlagSet) {
// return unsignedComparator;
// }

@Override
protected final void deallocate(final ByteBuffer buff) {
buff.order(BIG_ENDIAN);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/lmdbjava/CursorIterable.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public final class CursorIterable<T> implements Iterable<CursorIterable.KeyVal<T
this.entry = new KeyVal<>();

if (comparator != null) {
// User supplied java-side comparator so use that
// User supplied Java-side comparator so use that
this.rangeComparator = new JavaRangeComparator<>(range, comparator, entry::key);
} else {
// No java-side comparator so call down to LMDB to do the comparison
// No Java-side comparator, so call down to LMDB to do the comparison
this.rangeComparator = new LmdbRangeComparator<>(txn, dbi, cursor, range, proxy);
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/lmdbjava/Dbi.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public final class Dbi<T> {

private final ComparatorCallback ccb;
private boolean cleaned;
// Used for CursorIterable KeyRange testing and/or native callbacks
private final Comparator<T> comparator;
private final Env<T> env;
private final byte[] name;
Expand Down Expand Up @@ -80,6 +81,7 @@ public final class Dbi<T> {
ptr = dbiPtr.getPointer(0);
if (nativeCb) {
requireNonNull(comparator, "comparator cannot be null if nativeCb is set");
// LMDB will call back to this comparator for insertion/iteration order
this.ccb =
(keyA, keyB) -> {
final T compKeyA = proxy.out(proxy.allocate(), keyA);
Expand Down Expand Up @@ -465,6 +467,7 @@ private String getNameAsString() {
return "";
} else {
try {
// Assume a UTF8 encoding as we don't know, thus swallow if it fails
return new String(name, StandardCharsets.UTF_8);
} catch (Exception e) {
return "?";
Expand All @@ -475,8 +478,8 @@ private String getNameAsString() {
@Override
public String toString() {
return "Dbi{" +
"name=" + getNameAsString() +
", dbiFlagSet=" + dbiFlagSet +
"name='" + getNameAsString() +
"', dbiFlagSet=" + dbiFlagSet +
'}';
}

Expand Down
Loading
Loading