Skip to content

Commit bb6d2a6

Browse files
committed
Update tutorial to reflect CursorIterator changes (lmdbjava#7)
1 parent 7cb3ca2 commit bb6d2a6

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/test/java/org/lmdbjava/TutorialTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
import org.junit.Rule;
4141
import org.junit.Test;
4242
import org.junit.rules.TemporaryFolder;
43-
import static org.lmdbjava.CursorIterator.IteratorType.BACKWARD;
44-
import static org.lmdbjava.CursorIterator.IteratorType.FORWARD;
4543
import org.lmdbjava.CursorIterator.KeyVal;
4644
import static org.lmdbjava.DbiFlags.MDB_CREATE;
4745
import static org.lmdbjava.DbiFlags.MDB_DUPSORT;
@@ -322,25 +320,27 @@ public void tutorial4() throws IOException {
322320
key.clear();
323321

324322
// 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())) {
327325
for (final KeyVal<ByteBuffer> kv : it.iterable()) {
328326
assertThat(kv.key(), notNullValue());
329327
assertThat(kv.val(), notNullValue());
330328
}
331329
}
332330

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())) {
335333
for (final KeyVal<ByteBuffer> kv : it.iterable()) {
336334
assertThat(kv.key(), notNullValue());
337335
assertThat(kv.val(), notNullValue());
338336
}
339337
}
340338

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.
342341
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)) {
344344
for (final KeyVal<ByteBuffer> kv : it.iterable()) {
345345
assertThat(kv.key(), notNullValue());
346346
assertThat(kv.val(), notNullValue());

0 commit comments

Comments
 (0)