|
40 | 40 | import org.junit.Rule; |
41 | 41 | import org.junit.Test; |
42 | 42 | import org.junit.rules.TemporaryFolder; |
43 | | -import static org.lmdbjava.CursorIterator.IteratorType.BACKWARD; |
44 | | -import static org.lmdbjava.CursorIterator.IteratorType.FORWARD; |
45 | 43 | import org.lmdbjava.CursorIterator.KeyVal; |
46 | 44 | import static org.lmdbjava.DbiFlags.MDB_CREATE; |
47 | 45 | import static org.lmdbjava.DbiFlags.MDB_DUPSORT; |
@@ -322,25 +320,27 @@ public void tutorial4() throws IOException { |
322 | 320 | key.clear(); |
323 | 321 |
|
324 | 322 | // Each iterator uses a cursor and must be closed when finished. |
325 | | - // iterate forward in terms of key ordering starting with the first key |
326 | | - try (CursorIterator<ByteBuffer> it = db.iterate(txn, FORWARD)) { |
| 323 | + // Iterate forward in terms of key ordering starting with the first key. |
| 324 | + try (CursorIterator<ByteBuffer> it = db.iterate(txn, KeyRange.forward())) { |
327 | 325 | for (final KeyVal<ByteBuffer> kv : it.iterable()) { |
328 | 326 | assertThat(kv.key(), notNullValue()); |
329 | 327 | assertThat(kv.val(), notNullValue()); |
330 | 328 | } |
331 | 329 | } |
332 | 330 |
|
333 | | - // iterate backward in terms of key ordering starting with the first key |
334 | | - try (CursorIterator<ByteBuffer> it = db.iterate(txn, BACKWARD)) { |
| 331 | + // Iterate backward in terms of key ordering starting with the last key. |
| 332 | + try (CursorIterator<ByteBuffer> it = db.iterate(txn, KeyRange.backward())) { |
335 | 333 | for (final KeyVal<ByteBuffer> kv : it.iterable()) { |
336 | 334 | assertThat(kv.key(), notNullValue()); |
337 | 335 | assertThat(kv.val(), notNullValue()); |
338 | 336 | } |
339 | 337 | } |
340 | 338 |
|
341 | | - // search for key and iterate forwards/backward from there til the last/first key. |
| 339 | + // There are many ways to control the desired key range via KeyRange, such |
| 340 | + // as arbitrary start and stop values, directions etc. |
342 | 341 | key.putInt(1); |
343 | | - try (CursorIterator<ByteBuffer> it = db.iterate(txn, key, FORWARD)) { |
| 342 | + final KeyRange<ByteBuffer> range = KeyRange.atLeastBackward(key); |
| 343 | + try (CursorIterator<ByteBuffer> it = db.iterate(txn, range)) { |
344 | 344 | for (final KeyVal<ByteBuffer> kv : it.iterable()) { |
345 | 345 | assertThat(kv.key(), notNullValue()); |
346 | 346 | assertThat(kv.val(), notNullValue()); |
|
0 commit comments