Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
77 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
d6e0ca1
#269 Initial iterator performance enhancements and testing.
stroomdev66 Nov 4, 2025
71c53e7
Merge branch 'master' of github.com:lmdbjava/lmdbjava into iterator-p…
stroomdev66 Nov 4, 2025
d56b0d4
#269 Initial iterator performance enhancements and testing.
stroomdev66 Nov 5, 2025
8cecdd6
#269 Initial iterator performance enhancements and testing.
stroomdev66 Nov 5, 2025
e2be6bf
Add integer key comparator tests
at055612 Nov 5, 2025
aecdf51
#269 Initial iterator performance enhancements and testing.
stroomdev66 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
a3b9dea
Merge branch 'gh-267' of github.com:lmdbjava/lmdbjava into iterator-p…
stroomdev66 Nov 5, 2025
a7a41f4
#269 Initial iterator performance enhancements and testing.
stroomdev66 Nov 5, 2025
f23de09
#269 Initial iterator performance enhancements and testing.
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
7d04445
#269 Initial iterator performance enhancements and testing.
stroomdev66 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
6966a33
Merge branch 'gh-249-remove-unsignedkey' of github.com:lmdbjava/lmdbj…
stroomdev66 Nov 10, 2025
9541da3
Merge branch 'gh-249-remove-unsignedkey' of github.com:lmdbjava/lmdbj…
stroomdev66 Nov 10, 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
#269 Initial iterator performance enhancements and testing.
  • Loading branch information
stroomdev66 committed Nov 4, 2025
commit d6e0ca1d9f7bb4336c3a2d85c7b3a9d47e007e3d
56 changes: 42 additions & 14 deletions src/main/java/org/lmdbjava/BufferProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
*/
package org.lmdbjava;

import jnr.ffi.Pointer;

import java.util.Comparator;

import static java.lang.Long.BYTES;
import static org.lmdbjava.DbiFlags.MDB_INTEGERKEY;
import static org.lmdbjava.DbiFlags.MDB_UNSIGNEDKEY;
import static org.lmdbjava.MaskedFlag.isSet;
import static org.lmdbjava.MaskedFlag.mask;

import java.util.Comparator;
import jnr.ffi.Pointer;

/**
* The strategy for mapping memory address to a given buffer type.
*
Expand All @@ -35,17 +36,26 @@
*/
public abstract class BufferProxy<T> {

/** Size of a <code>MDB_val</code> pointer in bytes. */
/**
* Size of a <code>MDB_val</code> pointer in bytes.
*/
protected static final int MDB_VAL_STRUCT_SIZE = BYTES * 2;

/** Offset from a pointer of the <code>MDB_val.mv_data</code> field. */
/**
* Offset from a pointer of the <code>MDB_val.mv_data</code> field.
*/
protected static final int STRUCT_FIELD_OFFSET_DATA = BYTES;

/** Offset from a pointer of the <code>MDB_val.mv_size</code> field. */
/**
* Offset from a pointer of the <code>MDB_val.mv_size</code> field.
*/
protected static final int STRUCT_FIELD_OFFSET_SIZE = 0;

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

/**
* Allocate a new buffer suitable for passing to {@link #out(java.lang.Object, jnr.ffi.Pointer)}.
Expand Down Expand Up @@ -82,8 +92,8 @@ protected Comparator<T> getComparator(DbiFlags... flags) {
final int intFlag = mask(flags);

return isSet(intFlag, MDB_INTEGERKEY) || isSet(intFlag, MDB_UNSIGNEDKEY)
? getUnsignedComparator()
: getSignedComparator();
? getUnsignedComparator()
: getSignedComparator();
}

/**
Expand All @@ -105,7 +115,7 @@ protected Comparator<T> getComparator(DbiFlags... flags) {
* will have been created by end users, not {@link #allocate()}.
*
* @param buffer the buffer to write to <code>MDB_val</code>
* @param ptr the pointer to the <code>MDB_val</code>
* @param ptr the pointer to the <code>MDB_val</code>
* @return a transient pointer that must be kept alive, or null if none
*/
protected abstract Pointer in(T buffer, Pointer ptr);
Expand All @@ -114,8 +124,8 @@ protected Comparator<T> getComparator(DbiFlags... flags) {
* Called when the <code>MDB_val</code> should be set to reflect the passed buffer.
*
* @param buffer the buffer to write to <code>MDB_val</code>
* @param size the buffer size to write to <code>MDB_val</code>
* @param ptr the pointer to the <code>MDB_val</code>
* @param size the buffer size to write to <code>MDB_val</code>
* @param ptr the pointer to the <code>MDB_val</code>
* @return a transient pointer that must be kept alive, or null if none
*/
protected abstract Pointer in(T buffer, int size, Pointer ptr);
Expand All @@ -125,7 +135,7 @@ protected Comparator<T> getComparator(DbiFlags... flags) {
* to reflect the new <code>MDB_val</code>.
*
* @param buffer the buffer to write to <code>MDB_val</code>
* @param ptr the pointer to the <code>MDB_val</code>
* @param ptr the pointer to the <code>MDB_val</code>
* @return the buffer for <code>MDB_val</code>
*/
protected abstract T out(T buffer, Pointer ptr);
Expand All @@ -138,4 +148,22 @@ protected Comparator<T> getComparator(DbiFlags... flags) {
final KeyVal<T> keyVal() {
return new KeyVal<>(this);
}

/**
* Test if a supplied buffer contains the supplied prefix buffer.
*
* @param buffer The buffer to test.
* @param prefixBuffer The prefix to find.
* @return True if the key contains the prefix;
*/
abstract boolean containsPrefix(T buffer, T prefixBuffer);

/**
* Make a buffer that is one bit greater than the supplied buffer by incrementing the least significant byte that is
* not already 255. This is useful in situations where we want to move past a key range before iterating backward.
*
* @param buffer The buffer to increment.
* @return The incremented buffer or null if the buffer is already at max value.
*/
abstract T incrementLeastSignificantByte(T buffer);
}
78 changes: 71 additions & 7 deletions src/main/java/org/lmdbjava/ByteArrayProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
*/
package org.lmdbjava;

import static java.lang.Math.min;
import static java.util.Objects.requireNonNull;
import static org.lmdbjava.Library.RUNTIME;
import jnr.ffi.Pointer;
import jnr.ffi.provider.MemoryManager;

import java.util.Arrays;
import java.util.Comparator;
import jnr.ffi.Pointer;
import jnr.ffi.provider.MemoryManager;

import static java.lang.Math.min;
import static java.util.Objects.requireNonNull;
import static org.lmdbjava.Library.RUNTIME;

/**
* Byte array proxy.
Expand All @@ -31,15 +32,18 @@
*/
public final class ByteArrayProxy extends BufferProxy<byte[]> {

/** The byte array proxy. Guaranteed to never be null. */
/**
* The byte array proxy. Guaranteed to never be null.
*/
public static final BufferProxy<byte[]> PROXY_BA = new ByteArrayProxy();

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() {}
private ByteArrayProxy() {
}

/**
* Lexicographically compare two byte arrays.
Expand Down Expand Up @@ -68,6 +72,32 @@ public static int compareArrays(final byte[] o1, final byte[] o2) {
return o1.length - o2.length;
}

/**
* Lexicographically compare two byte arrays up to a max length.
*
* @param o1 left operand (required)
* @param o2 right operand (required)
* @param minLength The length to compare (required)
* @return as specified by {@link Comparable} interface
*/
public static int compareArrays(final byte[] o1, final byte[] o2, final int minLength) {
requireNonNull(o1);
requireNonNull(o2);
if (o1 == o2) {
return 0;
}
for (int i = 0; i < minLength; i++) {
final int lw = Byte.toUnsignedInt(o1[i]);
final int rw = Byte.toUnsignedInt(o2[i]);
final int result = Integer.compareUnsigned(lw, rw);
if (result != 0) {
return result;
}
}

return 0;
}

/**
* Compare two byte arrays.
*
Expand Down Expand Up @@ -137,4 +167,38 @@ protected byte[] out(final byte[] buffer, final Pointer ptr) {
pointer.get(0, bytes, 0, size);
return bytes;
}


@Override
boolean containsPrefix(final byte[] buffer, final byte[] prefixBuffer) {
if (buffer.length < prefixBuffer.length) {
return false;
}

// We don't care about signed or unsigned since we are checking for equality.
return compareArrays(buffer, prefixBuffer, prefixBuffer.length) == 0;
}

@Override
byte[] incrementLeastSignificantByte(final byte[] buffer) {
if (buffer == null || buffer.length == 0) {
return null;
}

// Start from the least significant byte (closest to limit)
for (int i = buffer.length - 1; i >= 0; i--) {
final byte b = buffer[i];

// Check if byte is not at max unsigned value (0xFF = 255 = -1 in signed)
if (b != (byte) 0xFF) {
final byte[] oneBigger = new byte[buffer.length];
System.arraycopy(buffer, 0, oneBigger, 0, buffer.length);
oneBigger[i] = (byte) (b + 1);
return oneBigger;
}
}

// All bytes are at maximum value
return null;
}
}
48 changes: 48 additions & 0 deletions src/main/java/org/lmdbjava/ByteBufProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

import static io.netty.buffer.PooledByteBufAllocator.DEFAULT;
import static java.lang.Class.forName;
import static java.nio.ByteOrder.LITTLE_ENDIAN;
import static java.util.Objects.requireNonNull;
import static org.lmdbjava.UnsafeAccess.UNSAFE;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.util.Comparator;
import jnr.ffi.Pointer;

Expand Down Expand Up @@ -163,4 +165,50 @@
buffer.clear().writerIndex((int) size);
return buffer;
}

@Override
boolean containsPrefix(final ByteBuf buffer, final ByteBuf prefixBuffer) {
if (buffer.capacity() < prefixBuffer.capacity()) {
return false;
}
return buffer.slice(0, prefixBuffer.capacity()).compareTo(prefixBuffer) == 0;
}

@Override
ByteBuf incrementLeastSignificantByte(final ByteBuf buffer) {
if (buffer == null || buffer.capacity() == 0) {
return null;
}

// TODO : Since endianness isn't a state for Netty ByteBuf we might not be able to do this.
if (LITTLE_ENDIAN.equals(buffer.order())) {
Comment thread Fixed
// Start from the least significant byte (closest to start)
for (int i = 0; i < buffer.capacity(); i++) {
final byte b = buffer.getByte(i);

// Check if byte is not at max unsigned value (0xFF = 255 = -1 in signed)
if (b != (byte) 0xFF) {
final ByteBuf oneBigger = buffer.copy();
oneBigger.setByte(i, (byte) (b + 1));
return oneBigger;
}
}

} else {
// Start from the least significant byte (closest to limit)
for (int i = buffer.capacity() - 1; i >= 0; i--) {
final byte b = buffer.getByte(i);

// Check if byte is not at max unsigned value (0xFF = 255 = -1 in signed)
if (b != (byte) 0xFF) {
final ByteBuf oneBigger = buffer.copy();
oneBigger.setByte(i, (byte) (b + 1));
return oneBigger;
}
}
}

// All bytes are at maximum value
return null;
}
}
Loading
Loading