Skip to content

Commit 2b30700

Browse files
committed
CursorIteratorTest to use Guava comparator (lmdbjava#7)
This is an example of how users could pass their own Comparator, subject of course to the comparator reflecting the key order returned by LMDB by Cursor previous / next invocations (if a comparator would result in a different key order than LMDB will return from its B+ tree index, we cannot efficiently use LMDB to iterate the key space).
1 parent e806a1c commit 2b30700

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/test/java/org/lmdbjava/CursorIteratorTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
package org.lmdbjava;
2222

23+
import com.google.common.primitives.UnsignedBytes;
2324
import java.io.File;
2425
import java.io.IOException;
2526
import java.nio.ByteBuffer;
@@ -269,6 +270,24 @@ public void openClosedBackwardTest() {
269270
verify(openClosedBackward(bb(8), bb(4)), 6, 4);
270271
}
271272

273+
@Test
274+
public void openClosedBackwardTestWithGuava() {
275+
final Comparator<byte[]> guava = UnsignedBytes.lexicographicalComparator();
276+
final Comparator<ByteBuffer> comparator = (bb1, bb2) -> {
277+
final byte[] array1 = new byte[bb1.remaining()];
278+
final byte[] array2 = new byte[bb2.remaining()];
279+
bb1.mark();
280+
bb2.mark();
281+
bb1.get(array1);
282+
bb2.get(array2);
283+
bb1.reset();
284+
bb2.reset();
285+
return guava.compare(array1, array2);
286+
};
287+
verify(openClosedBackward(bb(7), bb(2)), comparator, 6, 4, 2);
288+
verify(openClosedBackward(bb(8), bb(4)), comparator, 6, 4);
289+
}
290+
272291
@Test
273292
public void openClosedTest() {
274293
verify(openClosed(bb(3), bb(8)), 4, 6, 8);

0 commit comments

Comments
 (0)