Skip to content

Commit 0d571ae

Browse files
authored
Fixes KeyRange Query IndexOutOfBoundsException (lmdbjava#101)
* add range search test * fix assert * fixes key range search by using limit instead of capacity in ByteBufferProxy compare * fix checkstyle build errors * few more checkstyles changes * remove pmd violate system.out.println which violates
2 parents cba1a3e + 8d072b8 commit 0d571ae

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/main/java/org/lmdbjava/ByteBufferProxy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public static int compareBuff(final ByteBuffer o1, final ByteBuffer o2) {
128128
if (o1.equals(o2)) {
129129
return 0;
130130
}
131-
final int minLength = Math.min(o1.capacity(), o2.capacity());
131+
final int minLength = Math.min(o1.limit(), o2.limit());
132132
final int minWords = minLength / Long.BYTES;
133133

134134
final boolean reverse1 = o1.order() == LITTLE_ENDIAN;

src/test/java/org/lmdbjava/TxnTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@
2525
import java.io.IOException;
2626
import java.nio.ByteBuffer;
2727
import static java.nio.ByteBuffer.allocateDirect;
28+
import static java.nio.charset.StandardCharsets.UTF_8;
29+
import java.util.ArrayList;
30+
import java.util.List;
2831
import java.util.concurrent.atomic.AtomicLong;
2932
import static org.hamcrest.CoreMatchers.is;
3033
import static org.hamcrest.CoreMatchers.not;
3134
import static org.hamcrest.CoreMatchers.notNullValue;
3235
import static org.hamcrest.CoreMatchers.nullValue;
3336
import static org.hamcrest.MatcherAssert.assertThat;
3437
import org.junit.After;
38+
import static org.junit.Assert.assertEquals;
3539
import org.junit.Before;
3640
import org.junit.Rule;
3741
import org.junit.Test;
@@ -262,4 +266,39 @@ public void zeroByteKeysRejected() throws IOException {
262266
dbi.put(key, bb(2));
263267
}
264268

269+
270+
@Test
271+
public void rangeSearch() {
272+
final Dbi<ByteBuffer> db = env.openDbi(DB_1, MDB_CREATE);
273+
274+
final ByteBuffer key = allocateDirect(env.getMaxKeySize());
275+
key.put("cherry".getBytes(UTF_8)).flip();
276+
db.put(key, bb(1));
277+
278+
key.clear();
279+
key.put("strawberry".getBytes(UTF_8)).flip();
280+
db.put(key, bb(3));
281+
282+
key.clear();
283+
key.put("pineapple".getBytes(UTF_8)).flip();
284+
db.put(key, bb(2));
285+
286+
try (Txn<ByteBuffer> txn = env.txnRead()) {
287+
final ByteBuffer start = allocateDirect(env.getMaxKeySize());
288+
start.put("a".getBytes(UTF_8)).flip();
289+
290+
final ByteBuffer end = allocateDirect(env.getMaxKeySize());
291+
end.put("z".getBytes(UTF_8)).flip();
292+
293+
final List<String> keysFound = new ArrayList<>();
294+
final CursorIterator<ByteBuffer> ckr = db.iterate(txn, KeyRange.closed(start, end));
295+
for (final CursorIterator.KeyVal<ByteBuffer> kv : ckr.iterable()) {
296+
keysFound.add(UTF_8.decode(kv.key()).toString());
297+
}
298+
299+
assertEquals(3, keysFound.size());
300+
301+
}
302+
}
303+
265304
}

0 commit comments

Comments
 (0)