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 int key compare method to (Direct|Byte)BufferProxy
  • Loading branch information
at055612 committed Oct 29, 2025
commit 4fd89fff1755e1572929da3097809dfdad5fa1bb
27 changes: 17 additions & 10 deletions src/main/java/org/lmdbjava/AbstractFlagSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public boolean isSet(final T flag) {
// Probably cheaper to compare the masks than to use EnumSet.contains()
return flag != null
&& MaskedFlag.isSet(mask, flag);

}

/**
Expand Down Expand Up @@ -93,9 +92,7 @@ public Iterator<T> iterator() {

@Override
public boolean equals(Object object) {
Comment thread Fixed
if (this == object) return true;
// if (object == null || getClass() != object.getClass()) return false;
return FlagSet.equals(this, (FlagSet<?>) object);
return FlagSet.equals(this, object);
}

@Override
Expand Down Expand Up @@ -141,6 +138,15 @@ public boolean isSet(final T flag) {
return this.flag == flag;
}

@Override
public boolean areAnySet(FlagSet<T> flags) {
if (flags == null) {
return false;
} else {
return flags.isSet(this.flag);
}
}

@Override
public int size() {
return 1;
Expand All @@ -167,9 +173,7 @@ public String toString() {

@Override
public boolean equals(Object object) {
Comment thread Fixed
if (this == object) return true;
// if (object == null || getClass() != object.getClass()) return false;
return FlagSet.equals(this, (FlagSet<?>) object);
return FlagSet.equals(this, object);
}

@Override
Expand Down Expand Up @@ -205,6 +209,11 @@ public boolean isSet(final T flag) {
return false;
}

@Override
public boolean areAnySet(final FlagSet<T> flags) {
return false;
}

@Override
public int size() {
return 0;
Expand All @@ -227,9 +236,7 @@ public String toString() {

@Override
public boolean equals(Object object) {
Comment thread Fixed
if (this == object) return true;
// if (object == null || getClass() != object.getClass()) return false;
return FlagSet.equals(this, (FlagSet<?>) object);
return FlagSet.equals(this, object);
}

@Override
Expand Down
27 changes: 5 additions & 22 deletions src/main/java/org/lmdbjava/BufferProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public abstract class BufferProxy<T> {
/** Offset from a pointer of the <code>MDB_val.mv_size</code> field. */
protected static final int STRUCT_FIELD_OFFSET_SIZE = 0;

/** The set of {@link DbiFlags} that indicate unsigned integer keys are being used. */
protected static final DbiFlagSet INTEGER_KEY_FLAGS = DbiFlagSet.of(
DbiFlags.MDB_INTEGERKEY,
DbiFlags.MDB_INTEGERDUP);

/** Explicitly-defined default constructor to avoid warnings. */
protected BufferProxy() {}

Expand Down Expand Up @@ -88,28 +93,6 @@ 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
* will have been created by end users, not {@link #allocate()}.
Expand Down
31 changes: 0 additions & 31 deletions src/main/java/org/lmdbjava/ByteArrayProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public final class ByteArrayProxy extends BufferProxy<byte[]> {

private static final MemoryManager MEM_MGR = RUNTIME.getMemoryManager();

private static final Comparator<byte[]> signedComparator = ByteArrayProxy::compareArraysSigned;
private static final Comparator<byte[]> unsignedComparator = ByteArrayProxy::compareArrays;

private ByteArrayProxy() {}
Expand Down Expand Up @@ -68,26 +67,6 @@ public static int compareArrays(final byte[] o1, final byte[] o2) {
return o1.length - o2.length;
}

/**
* Compare two byte arrays.
*
* @param b1 left operand (required)
* @param b2 right operand (required)
* @return as specified by {@link Comparable} interface
*/
public static int compareArraysSigned(final byte[] b1, final byte[] b2) {
requireNonNull(b1);
requireNonNull(b2);

if (b1 == b2) return 0;

for (int i = 0; i < min(b1.length, b2.length); ++i) {
if (b1[i] != b2[i]) return b1[i] - b2[i];
}

return b1.length - b2.length;
}

@Override
protected byte[] allocate() {
return new byte[0];
Expand All @@ -108,16 +87,6 @@ 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
10 changes: 0 additions & 10 deletions src/main/java/org/lmdbjava/ByteBufProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,6 @@ public Comparator<ByteBuf> getComparator(final DbiFlagSet dbiFlagSet) {
return comparator;
}

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

@Override
protected void deallocate(final ByteBuf buff) {
buff.release();
Expand Down
122 changes: 67 additions & 55 deletions src/main/java/org/lmdbjava/ByteBufferProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,19 @@ public final class ByteBufferProxy {
*/
public static final BufferProxy<ByteBuffer> PROXY_OPTIMAL;

/** The safe, reflective {@link ByteBuffer} proxy for this system. Guaranteed to never be null. */
/**
* The safe, reflective {@link ByteBuffer} proxy for this system. Guaranteed to never be null.
*/
public static final BufferProxy<ByteBuffer> PROXY_SAFE;


static {
PROXY_SAFE = new ReflectiveProxy();
PROXY_OPTIMAL = getProxyOptimal();
}

private ByteBufferProxy() {}
private ByteBufferProxy() {
}

private static BufferProxy<ByteBuffer> getProxyOptimal() {
try {
Expand All @@ -72,17 +76,25 @@ private static BufferProxy<ByteBuffer> getProxyOptimal() {
}
}

/** The buffer must be a direct buffer (not heap allocated). */
/**
* The buffer must be a direct buffer (not heap allocated).
*/
public static final class BufferMustBeDirectException extends LmdbException {

private static final long serialVersionUID = 1L;

/** Creates a new instance. */
/**
* Creates a new instance.
*/
public BufferMustBeDirectException() {
super("The buffer must be a direct buffer (not heap allocated");
}
}


// --------------------------------------------------------------------------------


/**
* Provides {@link ByteBuffer} pooling and address resolution for concrete {@link BufferProxy}
* implementations.
Expand All @@ -92,16 +104,6 @@ abstract static class AbstractByteBufferProxy extends BufferProxy<ByteBuffer> {
protected static final String FIELD_NAME_ADDRESS = "address";
protected static final String FIELD_NAME_CAPACITY = "capacity";

private static final Comparator<ByteBuffer> signedComparator =
(o1, o2) -> {
requireNonNull(o1);
requireNonNull(o2);

return o1.compareTo(o2);
};
private static final Comparator<ByteBuffer> unsignedComparator =
AbstractByteBufferProxy::compareBuff;

/**
* A thread-safe pool for a given length. If the buffer found is valid (ie not of a negative
* length) then that buffer is used. If no valid buffer is found, a new buffer is created.
Expand All @@ -116,7 +118,7 @@ abstract static class AbstractByteBufferProxy extends BufferProxy<ByteBuffer> {
* @param o2 right operand (required)
* @return as specified by {@link Comparable} interface
*/
public static int compareBuff(final ByteBuffer o1, final ByteBuffer o2) {
public static int compareLexicographically(final ByteBuffer o1, final ByteBuffer o2) {
requireNonNull(o1);
requireNonNull(o2);
if (o1.equals(o2)) {
Expand Down Expand Up @@ -148,34 +150,42 @@ 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.");
// }
// }
/**
* 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.
* <p>
* Both buffer must have 4 or 8 bytes remaining
* </p>
*
* @param o1 left operand (required)
* @param o2 right operand (required)
* @return as specified by {@link Comparable} interface
*/
public static int compareAsIntegerKeys(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(0)) : o1.getLong(0);
final long rw = reverse2 ? Long.reverseBytes(o2.getLong(0)) : o2.getLong(0);
return Long.compareUnsigned(lw, rw);
} else if (len1 == 4) {
final int lw = reverse1 ? Integer.reverseBytes(o1.getInt(0)) : o1.getInt(0);
final int rw = reverse2 ? Integer.reverseBytes(o2.getInt(0)) : o2.getInt(0);
return Integer.compareUnsigned(lw, rw);
} else {
throw new RuntimeException("Unexpected length1: " + len1
+ ". Lengths must be identical and either 4 or 8 bytes.");
}
}

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

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

// @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 All @@ -240,6 +244,10 @@ protected byte[] getBytes(final ByteBuffer buffer) {
}
}


// --------------------------------------------------------------------------------


/**
* A proxy that uses Java reflection to modify byte buffer fields, and official JNR-FFF methods to
* manipulate native pointers.
Expand Down Expand Up @@ -284,6 +292,10 @@ protected ByteBuffer out(final ByteBuffer buffer, final Pointer ptr) {
}
}


// --------------------------------------------------------------------------------


/**
* A proxy that uses Java's "unsafe" class to directly manipulate byte buffer fields and JNR-FFF
* allocated memory pointers.
Expand Down
Loading