Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
480e984
gh-249 Remove MDB_UNSIGNEDKEY, let CursorIterable call mdb_cmp
at055612 Mar 6, 2025
46f8d08
gh-249 Remove non-J8 features, use indent size 2
at055612 Mar 6, 2025
f92012e
gh-249 Fix indents
at055612 Mar 6, 2025
e1756d6
gh-249 Tidy code and refactor RangeComparator impls
at055612 Mar 6, 2025
9509f6b
gh-249 Remove commented code
at055612 Mar 6, 2025
67e2df1
gh-249 Add CursorIterableIntegerKeyTest
at055612 Mar 7, 2025
620a89f
gh-249 Remove MaskedFlag.isPropagatedToLmdb, add DbiBuilder WIP
at055612 Jun 5, 2025
75d87d0
Merge branch 'master' into mdb_cmp
at055612 Oct 26, 2025
47b4c4f
Merge branch 'master' into mdb_cmp
at055612 Oct 26, 2025
bfbf223
Add FlagSet, DbiFlagSet, PutFlagSet
at055612 Oct 27, 2025
0f66aaf
Rename FlagSet to AbstractFlagSet
at055612 Oct 27, 2025
aa000a1
Add remaining FlagSet impls
at055612 Oct 27, 2025
667dab3
Fix Javadoc
at055612 Oct 27, 2025
0234d32
Replace get(Uns|S)ignedComparator with getComparator(DbiFlagSet)
at055612 Oct 28, 2025
ef0c852
Add missing txn commit in DbiBuilder
at055612 Oct 28, 2025
1b3f94d
Change CursorIterableTest to use Parameterized
at055612 Oct 28, 2025
c0bbe73
Deprecate methods using varargs flags
at055612 Oct 28, 2025
4fd89ff
Add int key compare method to (Direct|Byte)BufferProxy
at055612 Oct 29, 2025
58dcc6e
Tidy code
at055612 Oct 29, 2025
26665ba
Fix byte order issues with compareAsIntegerKeys
at055612 Oct 29, 2025
c427801
Add/refactor tests
at055612 Nov 4, 2025
f606e7e
Merge branch 'master' into mdb_cmp
at055612 Nov 4, 2025
e2be6bf
Add integer key comparator tests
at055612 Nov 5, 2025
1acd971
Add ComparatorFactory
at055612 Nov 5, 2025
dea7975
Merge branch 'master' into mdb_cmp
at055612 Nov 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Change CursorIterableTest to use Parameterized
  • Loading branch information
at055612 committed Oct 28, 2025
commit 1b3f94df197b0889e6612418d3a43a9f496ee452
205 changes: 115 additions & 90 deletions src/test/java/org/lmdbjava/CursorIterableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import static org.lmdbjava.TestUtils.DB_1;
import static org.lmdbjava.TestUtils.DB_2;
import static org.lmdbjava.TestUtils.DB_3;
import static org.lmdbjava.TestUtils.DB_4;
import static org.lmdbjava.TestUtils.POSIX_MODE;
import static org.lmdbjava.TestUtils.bb;

Expand All @@ -59,25 +60,83 @@
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.function.Function;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.lmdbjava.CursorIterable.KeyVal;

/** Test {@link CursorIterable}. */
/**
* Test {@link CursorIterable}.
*/
@RunWith(Parameterized.class)
public final class CursorIterableTest {

@Rule public final TemporaryFolder tmp = new TemporaryFolder();
private Dbi<ByteBuffer> dbJavaComparator;
private Dbi<ByteBuffer> dbLmdbComparator;
private Dbi<ByteBuffer> dbCallbackComparator;
private List<Dbi<ByteBuffer>> dbs = new ArrayList<>();
private static final DbiFlagSet dbiFlagSet = MDB_CREATE;
private static final BufferProxy<ByteBuffer> bufferProxy = ByteBufferProxy.PROXY_OPTIMAL;

@Rule
public final TemporaryFolder tmp = new TemporaryFolder();

private Env<ByteBuffer> env;
private Deque<Integer> list;

/** Injected by {@link #data()} with appropriate runner. */
@Parameterized.Parameter
public DbiFactory dbiFactory;

@Before
public void before() throws IOException {
final File path = tmp.newFile();
final BufferProxy<ByteBuffer> bufferProxy = ByteBufferProxy.PROXY_OPTIMAL;
env =
create(bufferProxy)
.setMapSize(KIBIBYTES.toBytes(256))
.setMaxReaders(1)
.setMaxDbs(3)
.open(path, POSIX_MODE, MDB_NOSUBDIR);

populateTestDataList();
}

@Parameterized.Parameters(name = "{index}: dbi: {0}")
public static Object[] data() {
final DbiFactory defaultComparator = new DbiFactory("defaultComparator", env ->
env.buildDbi()
.withDbName(DB_1)
.withDefaultComparator()
.withDbiFlags(dbiFlagSet)
.open());
final DbiFactory nativeComparator = new DbiFactory("nativeComparator", env ->
env.buildDbi()
.withDbName(DB_2)
.withNativeComparator()
.withDbiFlags(dbiFlagSet)
.open());
final DbiFactory callbackComparator = new DbiFactory("callbackComparator", env ->
env.buildDbi()
.withDbName(DB_3)
.withCallbackComparator(bufferProxy.getComparator(dbiFlagSet))
.withDbiFlags(dbiFlagSet)
.open());
final DbiFactory iteratorComparator = new DbiFactory("iteratorComparator", env ->
env.buildDbi()
.withDbName(DB_4)
.withIteratorComparator(bufferProxy.getComparator(dbiFlagSet))
.withDbiFlags(dbiFlagSet)
.open());
return new Object[] {
defaultComparator,
nativeComparator,
callbackComparator,
iteratorComparator};
}

@After
public void after() {
env.close();
Expand Down Expand Up @@ -118,49 +177,8 @@
verify(atMost(bb(6)), 2, 4, 6);
}

@Before
public void before() throws IOException {
final File path = tmp.newFile();
final BufferProxy<ByteBuffer> bufferProxy = ByteBufferProxy.PROXY_OPTIMAL;
env =
create(bufferProxy)
.setMapSize(KIBIBYTES.toBytes(256))
.setMaxReaders(1)
.setMaxDbs(3)
.open(path, POSIX_MODE, MDB_NOSUBDIR);

final DbiFlagSet dbiFlagSet = MDB_CREATE;
// Use a java comparator for start/stop keys only
dbJavaComparator = env.buildDbi()
.withDbName(DB_1)
.withDefaultComparator()
.withDbiFlags(dbiFlagSet)
.open();
// Use LMDB comparator for start/stop keys
dbLmdbComparator = env.buildDbi()
.withDbName(DB_2)
.withNativeComparator()
.withDbiFlags(dbiFlagSet)
.open();
// Use a java comparator for start/stop keys and as a callback comparaotr
dbCallbackComparator = env.buildDbi()
.withDbName(DB_3)
.withCallbackComparator(bufferProxy.getComparator(dbiFlagSet))
.withDbiFlags(dbiFlagSet)
.open();

populateList();

populateDatabase(dbJavaComparator);
populateDatabase(dbLmdbComparator);
populateDatabase(dbCallbackComparator);

dbs.add(dbJavaComparator);
dbs.add(dbLmdbComparator);
dbs.add(dbCallbackComparator);
}

private void populateList() {
private void populateTestDataList() {
list = new LinkedList<>();
list.addAll(asList(2, 3, 4, 5, 6, 7, 8, 9));
}
Expand Down Expand Up @@ -203,14 +221,6 @@
verify(closed(bb(1), bb(7)), 2, 4, 6);
}

public void closedTest1() {
verify(dbLmdbComparator, closed(bb(3), bb(7)), 4, 6);
}

public void closedTest2() {
verify(dbJavaComparator, closed(bb(3), bb(7)), 4, 6);
}

@Test
public void greaterThanBackwardTest() {
verify(greaterThanBackward(bb(6)), 4, 2);
Expand All @@ -219,47 +229,43 @@
}

@Test
public void greaterThanTest() {
verify(greaterThan(bb(4)), 6, 8);
verify(greaterThan(bb(3)), 4, 6, 8);
}

@Test(expected = IllegalStateException.class)
public void iterableOnlyReturnedOnce() {
for (final Dbi<ByteBuffer> db : dbs) {
try (Txn<ByteBuffer> txn = env.txnRead();
CursorIterable<ByteBuffer> c = db.iterate(txn)) {
final Dbi<ByteBuffer> db = getDb();
try (Txn<ByteBuffer> txn = env.txnRead();
CursorIterable<ByteBuffer> c = db.iterate(txn)) {
c.iterator(); // ok
c.iterator(); // fails
}
}
}

@Test
public void iterate() {
for (final Dbi<ByteBuffer> db : dbs) {
populateList();
final Dbi<ByteBuffer> db = getDb();
try (Txn<ByteBuffer> txn = env.txnRead();
CursorIterable<ByteBuffer> c = db.iterate(txn)) {
CursorIterable<ByteBuffer> c = db.iterate(txn)) {

Comment thread Fixed
int cnt = 0;
for (final KeyVal<ByteBuffer> kv : c) {
assertThat(kv.key().getInt(), is(list.pollFirst()));
assertThat(kv.val().getInt(), is(list.pollFirst()));
}
}
}
}

@Test(expected = IllegalStateException.class)
public void iteratorOnlyReturnedOnce() {
for (final Dbi<ByteBuffer> db : dbs) {
final Dbi<ByteBuffer> db = getDb();
try (Txn<ByteBuffer> txn = env.txnRead();
CursorIterable<ByteBuffer> c = db.iterate(txn)) {
CursorIterable<ByteBuffer> c = db.iterate(txn)) {
c.iterator(); // ok
c.iterator(); // fails
}
}
}

@Test
Expand All @@ -276,10 +282,10 @@

@Test(expected = NoSuchElementException.class)
public void nextThrowsNoSuchElementExceptionIfNoMoreElements() {
for (final Dbi<ByteBuffer> db : dbs) {
populateList();
final Dbi<ByteBuffer> db = getDb();
populateTestDataList();
try (Txn<ByteBuffer> txn = env.txnRead();
CursorIterable<ByteBuffer> c = db.iterate(txn)) {
CursorIterable<ByteBuffer> c = db.iterate(txn)) {
final Iterator<KeyVal<ByteBuffer>> i = c.iterator();
while (i.hasNext()) {
final KeyVal<ByteBuffer> kv = i.next();
Expand All @@ -289,7 +295,6 @@
assertThat(i.hasNext(), is(false));
i.next();
}
}
}

@Test
Expand Down Expand Up @@ -341,8 +346,8 @@

@Test
public void removeOddElements() {
for (final Dbi<ByteBuffer> db : dbs) {
verify(db, all(), 2, 4, 6, 8);
final Dbi<ByteBuffer> db = getDb();
verify(db, all(), 2, 4, 6, 8);
int idx = -1;
try (Txn<ByteBuffer> txn = env.txnWrite()) {
try (CursorIterable<ByteBuffer> ci = db.iterate(txn)) {
Expand All @@ -358,12 +363,11 @@
txn.commit();
}
verify(db, all(), 4, 8);
}
}

@Test(expected = Env.AlreadyClosedException.class)
public void nextWithClosedEnvTest() {
for (final Dbi<ByteBuffer> db : dbs) {
final Dbi<ByteBuffer> db = getDb();
try (Txn<ByteBuffer> txn = env.txnRead()) {
try (CursorIterable<ByteBuffer> ci = db.iterate(txn, KeyRange.all())) {
final Iterator<KeyVal<ByteBuffer>> c = ci.iterator();
Expand All @@ -372,12 +376,11 @@
c.next();
}
}
}
}

@Test(expected = Env.AlreadyClosedException.class)
public void removeWithClosedEnvTest() {
for (final Dbi<ByteBuffer> db : dbs) {
final Dbi<ByteBuffer> db = getDb();
try (Txn<ByteBuffer> txn = env.txnWrite()) {
try (CursorIterable<ByteBuffer> ci = db.iterate(txn, KeyRange.all())) {
final Iterator<KeyVal<ByteBuffer>> c = ci.iterator();
Expand All @@ -389,12 +392,11 @@
c.remove();
}
}
}
}

@Test(expected = Env.AlreadyClosedException.class)
public void hasNextWithClosedEnvTest() {
for (final Dbi<ByteBuffer> db : dbs) {
final Dbi<ByteBuffer> db = getDb();
try (Txn<ByteBuffer> txn = env.txnRead()) {
try (CursorIterable<ByteBuffer> ci = db.iterate(txn, KeyRange.all())) {
final Iterator<KeyVal<ByteBuffer>> c = ci.iterator();
Expand All @@ -403,21 +405,20 @@
c.hasNext();
}
}
}
}

@Test(expected = Env.AlreadyClosedException.class)
public void forEachRemainingWithClosedEnvTest() {
for (final Dbi<ByteBuffer> db : dbs) {
final Dbi<ByteBuffer> db = getDb();
try (Txn<ByteBuffer> txn = env.txnRead()) {
try (CursorIterable<ByteBuffer> ci = db.iterate(txn, KeyRange.all())) {
final Iterator<KeyVal<ByteBuffer>> c = ci.iterator();

env.close();
c.forEachRemaining(keyVal -> {});
c.forEachRemaining(keyVal -> {
});
}
}
}
}

// @Test
Expand Down Expand Up @@ -468,24 +469,23 @@
// }

private void verify(final KeyRange<ByteBuffer> range, final int... expected) {
// Verify using all comparator types
for (final Dbi<ByteBuffer> db : dbs) {
verify(range, db, expected);
}
final Dbi<ByteBuffer> db = getDb();
verify(range, db, expected);
}

private void verify(
final Dbi<ByteBuffer> dbi, final KeyRange<ByteBuffer> range, final int... expected) {
verify(range, dbi, expected);
}

private void verify(
final KeyRange<ByteBuffer> range, final Dbi<ByteBuffer> dbi, final int... expected) {
private void verify(final KeyRange<ByteBuffer> range,
final Dbi<ByteBuffer> dbi,
final int... expected) {

final List<Integer> results = new ArrayList<>();

try (Txn<ByteBuffer> txn = env.txnRead();
CursorIterable<ByteBuffer> c = dbi.iterate(txn, range)) {
CursorIterable<ByteBuffer> c = dbi.iterate(txn, range)) {
for (final KeyVal<ByteBuffer> kv : c) {
final int key = kv.key().getInt();
final int val = kv.val().getInt();
Expand All @@ -499,4 +499,29 @@
assertThat(results.get(idx), is(expected[idx]));
}
}

private Dbi<ByteBuffer> getDb() {
final Dbi<ByteBuffer> dbi = dbiFactory.factory.apply(env);
populateDatabase(dbi);
return dbi;
}


// --------------------------------------------------------------------------------


private static class DbiFactory {
private final String name;
private final Function<Env<ByteBuffer>, Dbi<ByteBuffer>> factory;

private DbiFactory(String name, Function<Env<ByteBuffer>, Dbi<ByteBuffer>> factory) {
this.name = name;
this.factory = factory;
}

@Override
public String toString() {
return name;
}
}
}
1 change: 1 addition & 0 deletions src/test/java/org/lmdbjava/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ final class TestUtils {
public static final String DB_1 = "test-db-1";
public static final String DB_2 = "test-db-2";
public static final String DB_3 = "test-db-3";
public static final String DB_4 = "test-db-2";

public static final int POSIX_MODE = 0664;

Expand Down
Loading