Skip to content

Commit 4e7b4f2

Browse files
committed
add range search test
1 parent 6a3f8ac commit 4e7b4f2

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,26 @@
2525
import java.io.IOException;
2626
import java.nio.ByteBuffer;
2727
import static java.nio.ByteBuffer.allocateDirect;
28+
29+
import java.util.ArrayList;
30+
import java.util.List;
2831
import java.util.concurrent.atomic.AtomicLong;
32+
33+
import static java.nio.charset.StandardCharsets.UTF_8;
2934
import static org.hamcrest.CoreMatchers.is;
3035
import static org.hamcrest.CoreMatchers.not;
3136
import static org.hamcrest.CoreMatchers.notNullValue;
3237
import static org.hamcrest.CoreMatchers.nullValue;
3338
import static org.hamcrest.MatcherAssert.assertThat;
39+
3440
import org.junit.After;
3541
import org.junit.Before;
3642
import org.junit.Rule;
3743
import org.junit.Test;
3844
import org.junit.rules.TemporaryFolder;
3945
import org.lmdbjava.Dbi.BadValueSizeException;
46+
47+
import static org.junit.Assert.assertEquals;
4048
import static org.lmdbjava.DbiFlags.MDB_CREATE;
4149
import org.lmdbjava.Env.AlreadyClosedException;
4250
import static org.lmdbjava.Env.create;
@@ -262,4 +270,40 @@ public void zeroByteKeysRejected() throws IOException {
262270
dbi.put(key, bb(2));
263271
}
264272

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

0 commit comments

Comments
 (0)