Skip to content

Fix AbsatractByteBufferProxy.compareBuff #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions src/main/java/org/lmdbjava/ByteBufferProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public static int compareBuff(final ByteBuffer o1, final ByteBuffer o2) {
final int minWords = minLength / Long.BYTES;

final boolean reverse1 = o1.order() == LITTLE_ENDIAN;
final boolean reverse2 = o1.order() == LITTLE_ENDIAN;
final boolean reverse2 = o2.order() == LITTLE_ENDIAN;
for (int i = 0; i < minWords * Long.BYTES; i += Long.BYTES) {
final long lw = reverse1 ? reverseBytes(o1.getLong(i)) : o1.getLong(i);
final long rw = reverse2 ? reverseBytes(o2.getLong(i)) : o2.getLong(i);
Expand All @@ -152,7 +152,7 @@ public static int compareBuff(final ByteBuffer o1, final ByteBuffer o2) {
}
}

return o1.capacity() - o2.capacity();
return o1.remaining() - o2.remaining();
}

static Field findField(final Class<?> c, final String name) {
Expand Down
36 changes: 33 additions & 3 deletions src/test/java/org/lmdbjava/ComparatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import static org.lmdbjava.DirectBufferProxy.PROXY_DB;

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Comparator;

import com.google.common.primitives.SignedBytes;
Expand Down Expand Up @@ -156,9 +157,38 @@ private static class ByteBufferRunner implements ComparatorRunner {

@Override
public int compare(final byte[] o1, final byte[] o2) {
final ByteBuffer o1b = ByteBuffer.wrap(o1);
final ByteBuffer o2b = ByteBuffer.wrap(o2);
return PROXY_OPTIMAL.compare(o1b, o2b);
// Convert arrays to buffers that are larger than the array, with
// limit set at the array length. One buffer bigger than the other.
ByteBuffer o1b = arrayToBuffer(o1, o1.length * 3);
ByteBuffer o2b = arrayToBuffer(o2, o2.length * 2);
final int result = PROXY_OPTIMAL.compare(o1b, o2b);

// Now swap which buffer is bigger
o1b = arrayToBuffer(o1, o1.length * 2);
o2b = arrayToBuffer(o2, o2.length * 3);
final int result2 = PROXY_OPTIMAL.compare(o1b, o2b);

assertThat(result2, is(result));

// Now try with buffers sized to the array.
o1b = ByteBuffer.wrap(o1);
o2b = ByteBuffer.wrap(o2);
final int result3 = PROXY_OPTIMAL.compare(o1b, o2b);

assertThat(result3, is(result));

return result;
}

private ByteBuffer arrayToBuffer(final byte[] arr, final int bufferCapacity) {
if (bufferCapacity < arr.length) {
throw new IllegalArgumentException("bufferCapacity < arr.length");
}
final byte[] newArr = Arrays.copyOf(arr, bufferCapacity);
final ByteBuffer byteBuffer = ByteBuffer.wrap(newArr);
byteBuffer.limit(arr.length);
byteBuffer.position(0);
return byteBuffer;
}
}

Expand Down