|
25 | 25 | import java.io.IOException; |
26 | 26 | import java.nio.ByteBuffer; |
27 | 27 | import static java.nio.ByteBuffer.allocateDirect; |
| 28 | + |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.List; |
28 | 31 | import java.util.concurrent.atomic.AtomicLong; |
| 32 | + |
| 33 | +import static java.nio.charset.StandardCharsets.UTF_8; |
29 | 34 | import static org.hamcrest.CoreMatchers.is; |
30 | 35 | import static org.hamcrest.CoreMatchers.not; |
31 | 36 | import static org.hamcrest.CoreMatchers.notNullValue; |
32 | 37 | import static org.hamcrest.CoreMatchers.nullValue; |
33 | 38 | import static org.hamcrest.MatcherAssert.assertThat; |
| 39 | + |
34 | 40 | import org.junit.After; |
35 | 41 | import org.junit.Before; |
36 | 42 | import org.junit.Rule; |
37 | 43 | import org.junit.Test; |
38 | 44 | import org.junit.rules.TemporaryFolder; |
39 | 45 | import org.lmdbjava.Dbi.BadValueSizeException; |
| 46 | + |
| 47 | +import static org.junit.Assert.assertEquals; |
40 | 48 | import static org.lmdbjava.DbiFlags.MDB_CREATE; |
41 | 49 | import org.lmdbjava.Env.AlreadyClosedException; |
42 | 50 | import static org.lmdbjava.Env.create; |
@@ -262,4 +270,40 @@ public void zeroByteKeysRejected() throws IOException { |
262 | 270 | dbi.put(key, bb(2)); |
263 | 271 | } |
264 | 272 |
|
| 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 | + |
265 | 309 | } |
0 commit comments