Skip to content
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
2 changes: 1 addition & 1 deletion src/main/java/org/lmdbjava/ByteBufferProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static int compareBuff(final ByteBuffer o1, final ByteBuffer o2) {
if (o1.equals(o2)) {
return 0;
}
final int minLength = Math.min(o1.capacity(), o2.capacity());
final int minLength = Math.min(o1.limit(), o2.limit());
final int minWords = minLength / Long.BYTES;

final boolean reverse1 = o1.order() == LITTLE_ENDIAN;
Expand Down
39 changes: 39 additions & 0 deletions src/test/java/org/lmdbjava/TxnTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@
import java.io.IOException;
import java.nio.ByteBuffer;
import static java.nio.ByteBuffer.allocateDirect;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.After;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -262,4 +266,39 @@ public void zeroByteKeysRejected() throws IOException {
dbi.put(key, bb(2));
}


@Test
public void rangeSearch() {
final Dbi<ByteBuffer> db = env.openDbi(DB_1, MDB_CREATE);

final ByteBuffer key = allocateDirect(env.getMaxKeySize());
key.put("cherry".getBytes(UTF_8)).flip();
db.put(key, bb(1));

key.clear();
key.put("strawberry".getBytes(UTF_8)).flip();
db.put(key, bb(3));

key.clear();
key.put("pineapple".getBytes(UTF_8)).flip();
db.put(key, bb(2));

try (Txn<ByteBuffer> txn = env.txnRead()) {
final ByteBuffer start = allocateDirect(env.getMaxKeySize());
start.put("a".getBytes(UTF_8)).flip();

final ByteBuffer end = allocateDirect(env.getMaxKeySize());
end.put("z".getBytes(UTF_8)).flip();

final List<String> keysFound = new ArrayList<>();
final CursorIterator<ByteBuffer> ckr = db.iterate(txn, KeyRange.closed(start, end));
for (final CursorIterator.KeyVal<ByteBuffer> kv : ckr.iterable()) {
keysFound.add(UTF_8.decode(kv.key()).toString());
}

assertEquals(3, keysFound.size());

}
}

}