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
Add ComparatorFactory
  • Loading branch information
at055612 committed Nov 5, 2025
commit 1acd9711efb752013b6588e5e3d106474afa5822
50 changes: 35 additions & 15 deletions src/main/java/org/lmdbjava/DbiBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@
* <p>
* The name will be converted into bytes using {@link StandardCharsets#UTF_8}.
* </p>
*
* @param name The name of the database or null for the unnamed database
* (see also {@link DbiBuilder#withoutDbName()})
* (see also {@link DbiBuilder#withoutDbName()})
* @return The next builder stage.
*/
public DbiBuilderStage2<T> setDbName(final String name) {
Expand All @@ -63,6 +64,7 @@

/**
* Create the {@link Dbi} with the passed name in byte[] form.
*
* @param name The name of the database in byte form.
* @return The next builder stage.
*/
Expand All @@ -83,6 +85,7 @@
* <p>Note: The 'unnamed database' is used by LMDB to store the names of named databases, with
* the database name being the key. Use of the unnamed database is intended for simple applications
* with only one database.</p>
*
* @return The next builder stage.
*/
public DbiBuilderStage2<T> withoutDbName() {
Expand All @@ -102,7 +105,7 @@

private final DbiBuilder<T> dbiBuilder;

private java.util.Comparator<T> customComparator;
private ComparatorFactory<T> comparatorFactory;
private ComparatorType comparatorType;

private DbiBuilderStage2(final DbiBuilder<T> dbiBuilder) {
Expand Down Expand Up @@ -130,7 +133,7 @@
* If you do not intend to use {@link CursorIterable} then it doesn't matter whether
* you choose {@link DbiBuilderStage2#withNativeComparator()},
* {@link DbiBuilderStage2#withDefaultComparator()} or
* {@link DbiBuilderStage2#withIteratorComparator(Comparator)} as these comparators will
* {@link DbiBuilderStage2#withIteratorComparator(ComparatorFactory)} as these comparators will
* never be used.
* </p>
*
Expand All @@ -157,7 +160,7 @@
* If you do not intend to use {@link CursorIterable} then it doesn't matter whether
* you choose {@link DbiBuilderStage2#withNativeComparator()},
* {@link DbiBuilderStage2#withDefaultComparator()} or
* {@link DbiBuilderStage2#withIteratorComparator(Comparator)} as these comparators will
* {@link DbiBuilderStage2#withIteratorComparator(ComparatorFactory)} as these comparators will
* never be used.
* </p>
*
Expand Down Expand Up @@ -186,11 +189,13 @@
* are stored in the database.
* </p>
*
* @param comparator for all key comparison operations.
* @param comparatorFactory A factory to create a comparator. {@link ComparatorFactory#create(DbiFlagSet)}
* will be called once during the initialisation of the {@link Dbi}. It must
* not return null.
* @return The next builder stage.
*/
public DbiBuilderStage3<T> withCallbackComparator(final Comparator<T> comparator) {
this.customComparator = Objects.requireNonNull(comparator);
public DbiBuilderStage3<T> withCallbackComparator(final ComparatorFactory<T> comparatorFactory) {
this.comparatorFactory = Objects.requireNonNull(comparatorFactory);
this.comparatorType = ComparatorType.CALLBACK;
return new DbiBuilderStage3<>(this);
}
Expand All @@ -215,15 +220,17 @@
* If you do not intend to use {@link CursorIterable} then it doesn't matter whether
* you choose {@link DbiBuilderStage2#withNativeComparator()},
* {@link DbiBuilderStage2#withDefaultComparator()} or
* {@link DbiBuilderStage2#withIteratorComparator(Comparator)} as these comparators will
* {@link DbiBuilderStage2#withIteratorComparator(ComparatorFactory)} as these comparators will
* never be used.
* </p>
*
* @param comparator The comparator to use with {@link CursorIterable}.
* @param comparatorFactory The comparator to use with {@link CursorIterable}.
* {@link ComparatorFactory#create(DbiFlagSet)} will be called once during the
* initialisation of the {@link Dbi}. It must not return null.
* @return The next builder stage.
*/
public DbiBuilderStage3<T> withIteratorComparator(final Comparator<T> comparator) {
this.customComparator = Objects.requireNonNull(comparator);
public DbiBuilderStage3<T> withIteratorComparator(final ComparatorFactory<T> comparatorFactory) {
this.comparatorFactory = Objects.requireNonNull(comparatorFactory);
this.comparatorType = ComparatorType.ITERATOR;
return new DbiBuilderStage3<>(this);
}
Expand Down Expand Up @@ -267,8 +274,8 @@
flagSetBuilder.clear();
if (dbiFlags != null) {
dbiFlags.stream()
.filter(Objects::nonNull)
.forEach(dbiFlags::add);
.filter(Objects::nonNull)
.forEach(dbiFlags::add);
}
return this;
}
Expand Down Expand Up @@ -310,7 +317,7 @@
* </p>
*
* @param dbiFlagSet to open the database with.
* A null value will just clear all set flags.
* A null value will just clear all set flags.
*/
public DbiBuilderStage3<T> setDbiFlags(final DbiFlagSet dbiFlagSet) {
flagSetBuilder.clear();
Expand Down Expand Up @@ -359,7 +366,7 @@
* <p>
* If you don't call this method to supply a {@link Txn}, a {@link Txn} will be opened for the purpose
* of creating and opening the {@link Dbi}, then closed. Therefore, if you already have a transaction
* open, you should supply that to avoid one blocking the other.
* </p>
*
* @param txn transaction to use (required; not closed). If the {@link Env} was opened
Expand Down Expand Up @@ -413,7 +420,9 @@
break;
case CALLBACK:
case ITERATOR:
comparator = dbiBuilderStage2.customComparator;
comparator = Objects.requireNonNull(
dbiBuilderStage2.comparatorFactory.create(dbiFlagSet),
() -> "comparatorFactory returned null");
break;
case NATIVE:
break;
Expand Down Expand Up @@ -465,4 +474,15 @@
ITERATOR,
;
}


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


@FunctionalInterface
public interface ComparatorFactory<T> {

Comparator<T> create(final DbiFlagSet dbiFlagSet);

}
}
109 changes: 46 additions & 63 deletions src/test/java/org/lmdbjava/ComparatorIntegerKeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,10 @@
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;
import java.util.stream.Stream;
import org.agrona.DirectBuffer;
import org.agrona.concurrent.UnsafeBuffer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand Down Expand Up @@ -98,13 +95,10 @@ void testInt(final ComparatorRunner comparator) {
assertThat(get(comparator.compare(Integer.MAX_VALUE, 0))).isEqualTo(GREATER_THAN);
}

@Test
void testRandomLong() {
@ParameterizedTest
@MethodSource("comparatorProvider")
void testRandomLong(final ComparatorRunner runner) {
final Random random = new Random(3239480);
final Map<String, ComparatorRunner> nameToRunnerMap = new LinkedHashMap<>();
nameToRunnerMap.put("DirectBufferRunner", new DirectBufferRunner());
nameToRunnerMap.put("ByteBufferRunner", new ByteBufferRunner());
nameToRunnerMap.put("NettyRunner", new NettyRunner());

// 5mil random longs to compare
final long[] values = random.longs()
Expand All @@ -115,40 +109,33 @@ void testRandomLong() {
for (int i = 1; i < values.length; i++) {
final long long1 = values[i - 1];
final long long2 = values[i];
for (Map.Entry<String, ComparatorRunner> entry : nameToRunnerMap.entrySet()) {
final String name = entry.getKey();
final ComparatorRunner runner = entry.getValue();
// Make sure the comparator under test gives the same outcome as just comparing two longs
final ComparatorTest.ComparatorResult result = get(runner.compare(long1, long2));
final ComparatorTest.ComparatorResult expectedResult = get(Long.compare(long1, long2));

assertThat(result)
.withFailMessage(() -> "Compare mismatch for " + name + " - long1: " + long1
+ ", long2: " + long2
+ ", expected: " + expectedResult
+ ", actual: " + result)
.isEqualTo(expectedResult);

final ComparatorTest.ComparatorResult result2 = get(runner.compare(long2, long1));
final ComparatorTest.ComparatorResult expectedResult2 = expectedResult.opposite();

assertThat(result)
.withFailMessage(() -> "Compare mismatch for " + name + " - long2: " + long2
+ ", long1: " + long1
+ ", expected2: " + expectedResult2
+ ", actual2: " + result2)
.isEqualTo(expectedResult);
}
// Make sure the comparator under test gives the same outcome as just comparing two longs
final ComparatorTest.ComparatorResult result = get(runner.compare(long1, long2));
final ComparatorTest.ComparatorResult expectedResult = get(Long.compare(long1, long2));

assertThat(result)
.withFailMessage(() -> "Compare mismatch - long1: " + long1
+ ", long2: " + long2
+ ", expected: " + expectedResult
+ ", actual: " + result)
.isEqualTo(expectedResult);

final ComparatorTest.ComparatorResult result2 = get(runner.compare(long2, long1));
final ComparatorTest.ComparatorResult expectedResult2 = expectedResult.opposite();

assertThat(result)
.withFailMessage(() -> "Compare mismatch for - long2: " + long2
+ ", long1: " + long1
+ ", expected2: " + expectedResult2
+ ", actual2: " + result2)
.isEqualTo(expectedResult);
}
}

@Test
void testRandomInt() {
@ParameterizedTest
@MethodSource("comparatorProvider")
void testRandomInt(final ComparatorRunner runner) {
final Random random = new Random(3239480);
final Map<String, ComparatorRunner> nameToRunnerMap = new LinkedHashMap<>();
nameToRunnerMap.put("DirectBufferRunner", new DirectBufferRunner());
nameToRunnerMap.put("ByteBufferRunner", new ByteBufferRunner());
nameToRunnerMap.put("NettyRunner", new NettyRunner());

// 5mil random ints to compare
final int[] values = random.ints()
Expand All @@ -159,30 +146,26 @@ void testRandomInt() {
for (int i = 1; i < values.length; i++) {
final int int1 = values[i - 1];
final int int2 = values[i];
for (Map.Entry<String, ComparatorRunner> entry : nameToRunnerMap.entrySet()) {
final String name = entry.getKey();
final ComparatorRunner runner = entry.getValue();
// Make sure the comparator under test gives the same outcome as just comparing two ints
final ComparatorTest.ComparatorResult result = get(runner.compare(int1, int2));
final ComparatorTest.ComparatorResult expectedResult = get(Integer.compare(int1, int2));

assertThat(result)
.withFailMessage(() -> "Compare mismatch for " + name + " - int1: " + int1
+ ", int2: " + int2
+ ", expected: " + expectedResult
+ ", actual: " + result)
.isEqualTo(expectedResult);

final ComparatorTest.ComparatorResult result2 = get(runner.compare(int2, int1));
final ComparatorTest.ComparatorResult expectedResult2 = expectedResult.opposite();

assertThat(result)
.withFailMessage(() -> "Compare mismatch for " + name + " - int2: " + int2
+ ", int1: " + int1
+ ", expected2: " + expectedResult2
+ ", actual2: " + result2)
.isEqualTo(expectedResult);
}
// Make sure the comparator under test gives the same outcome as just comparing two ints
final ComparatorTest.ComparatorResult result = get(runner.compare(int1, int2));
final ComparatorTest.ComparatorResult expectedResult = get(Integer.compare(int1, int2));

assertThat(result)
.withFailMessage(() -> "Compare mismatch for - int1: " + int1
+ ", int2: " + int2
+ ", expected: " + expectedResult
+ ", actual: " + result)
.isEqualTo(expectedResult);

final ComparatorTest.ComparatorResult result2 = get(runner.compare(int2, int1));
final ComparatorTest.ComparatorResult expectedResult2 = expectedResult.opposite();

assertThat(result)
.withFailMessage(() -> "Compare mismatch for - int2: " + int2
+ ", int1: " + int1
+ ", expected2: " + expectedResult2
+ ", actual2: " + result2)
.isEqualTo(expectedResult);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/lmdbjava/CursorIterableIntegerDupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@
file = FileUtil.createTempFile();
final BufferProxy<ByteBuffer> bufferProxy = ByteBufferProxy.PROXY_OPTIMAL;
env =
create(bufferProxy)
.setMapSize(KIBIBYTES.toBytes(256))
.setMaxReaders(1)
.setMaxDbs(3)
.open(file.toFile(), POSIX_MODE, MDB_NOSUBDIR);
Comment on lines +127 to +131

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation Note test

Invoking
Builder.open
should be avoided because it has been deprecated.

populateExpectedEntriesDeque();
}
Expand Down Expand Up @@ -355,7 +355,7 @@
bb2.reset();
return guava.compare(array1, array2);
};
final Dbi<ByteBuffer> guavaDbi = env.openDbi(DB_1, comparator, MDB_CREATE);

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation Note test

Invoking
Env.openDbi
should be avoided because it has been deprecated.
populateDatabase(guavaDbi);
verify(openClosedBackward(bbNative(7), bbNative(2)), guavaDbi, 6, 4, 2);
verify(openClosedBackward(bbNative(8), bbNative(4)), guavaDbi, 6, 4);
Expand Down Expand Up @@ -557,13 +557,13 @@
final DbiFactory callbackComparatorDb = new DbiFactory("callbackComparator", env ->
env.buildDbi()
.setDbName(DB_3)
.withCallbackComparator(buildComparator())
.withCallbackComparator(MyArgumentProvider::buildComparator)
.setDbiFlags(DBI_FLAGS)
.open());
final DbiFactory iteratorComparatorDb = new DbiFactory("iteratorComparator", env ->
env.buildDbi()
.setDbName(DB_4)
.withIteratorComparator(buildComparator())
.withIteratorComparator(MyArgumentProvider::buildComparator)
.setDbiFlags(DBI_FLAGS)
.open());
return Stream.of(
Expand All @@ -574,7 +574,7 @@
.map(Arguments::of);
}

private static Comparator<ByteBuffer> buildComparator() {
private static Comparator<ByteBuffer> buildComparator(final DbiFlagSet dbiFlagSet) {
final Comparator<ByteBuffer> baseComparator = BUFFER_PROXY.getComparator(DBI_FLAGS);
return (o1, o2) -> {
if (o1.remaining() != o2.remaining()) {
Expand Down
8 changes: 3 additions & 5 deletions src/test/java/org/lmdbjava/CursorIterableIntegerKeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@
try (Txn<ByteBuffer> txn = env.txnRead()) {
try (CursorIterable<ByteBuffer> iterable = db.iterate(txn)) {
for (KeyVal<ByteBuffer> keyVal : iterable) {
final String val = getString(keyVal.val());

Check notice

Code scanning / CodeQL

Unread local variable Note test

Variable 'String val' is never read.
final long key = getNativeLong(keyVal.key());

Check notice

Code scanning / CodeQL

Unread local variable Note test

Variable 'long key' is never read.
final int remaining = keyVal.key().remaining();

Check notice

Code scanning / CodeQL

Unread local variable Note test

Variable 'int remaining' is never read.
// System.out.println("key: " + key + ", val: " + val + ", remaining: " + remaining);
}
}
Expand Down Expand Up @@ -309,7 +309,7 @@
@Test
public void closedOpenBackwardTest() {
verify(closedOpenBackward(bbNative(8), bbNative(3)), 8, 6, 4);
verify(closedOpenBackward(bbNative(7), bbNative(2)), 6, 4);
verify(closedOpenBackward(bbNative(9), bbNative(3)), 8, 6, 4);
}

Expand Down Expand Up @@ -611,18 +611,16 @@
.withNativeComparator()
.setDbiFlags(DBI_FLAGS)
.open());
final Comparator<ByteBuffer> comparator = buildComparator();

final DbiFactory callbackComparatorDb = new DbiFactory("callbackComparator", env ->
env.buildDbi()
.setDbName(DB_3)
.withCallbackComparator(comparator)
.withCallbackComparator(MyArgumentProvider::buildComparator)
.setDbiFlags(DBI_FLAGS)
.open());
final DbiFactory iteratorComparatorDb = new DbiFactory("iteratorComparator", env ->
env.buildDbi()
.setDbName(DB_4)
.withIteratorComparator(comparator)
.withIteratorComparator(MyArgumentProvider::buildComparator)
.setDbiFlags(DBI_FLAGS)
.open());
return Stream.of(
Expand All @@ -633,7 +631,7 @@
.map(Arguments::of);
}

private static Comparator<ByteBuffer> buildComparator() {
private static Comparator<ByteBuffer> buildComparator(final DbiFlagSet dbiFlagSet) {
final Comparator<ByteBuffer> baseComparator = BUFFER_PROXY.getComparator(DBI_FLAGS);
return (o1, o2) -> {
if (o1.remaining() != o2.remaining()) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/lmdbjava/CursorIterablePerfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@
file = FileUtil.createTempFile();
final BufferProxy<ByteBuffer> bufferProxy = ByteBufferProxy.PROXY_OPTIMAL;
env =
create(bufferProxy)
.setMapSize(GIBIBYTES.toBytes(1))
.setMaxReaders(1)
.setMaxDbs(3)
.open(file.toFile(), POSIX_MODE, MDB_NOSUBDIR);
Comment on lines +59 to +63

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation Note test

Invoking
Builder.open
should be avoided because it has been deprecated.

final DbiFlagSet dbiFlagSet = MDB_CREATE;
// Use a java comparator for start/stop keys only
Expand All @@ -79,7 +79,7 @@
// Use a java comparator for start/stop keys and as a callback comparator
dbCallbackComparator = env.buildDbi()
.setDbName("CallBackComparator")
.withCallbackComparator(bufferProxy.getComparator(dbiFlagSet))
.withCallbackComparator(bufferProxy::getComparator)
.setDbiFlags(dbiFlagSet)
.open();

Expand Down Expand Up @@ -120,7 +120,7 @@

for (final Dbi<ByteBuffer> db : dbs) {
// Clean out the db first
try (Txn<ByteBuffer> txn = env.txnWrite();
final Cursor<ByteBuffer> cursor = db.openCursor(txn)) {
while (cursor.next()) {
cursor.delete();

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation Note test

Invoking
Cursor.delete
should be avoided because it has been deprecated.
Expand Down Expand Up @@ -152,7 +152,7 @@
}

@Test
public void comparePerf_sequential() {
comparePerf(false);
}

Expand Down
11 changes: 7 additions & 4 deletions src/test/java/org/lmdbjava/CursorIterableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public final class CursorIterableTest {
private static final BufferProxy<ByteBuffer> BUFFER_PROXY = ByteBufferProxy.PROXY_OPTIMAL;

private Path file;
private Dbi<ByteBuffer> db;
private Env<ByteBuffer> env;
private Deque<Integer> list;

Expand Down Expand Up @@ -309,7 +308,11 @@ void openClosedBackwardTestWithGuava() {
bb2.reset();
return guava.compare(array1, array2);
};
final Dbi<ByteBuffer> guavaDbi = env.openDbi(DB_1, comparator, MDB_CREATE);
final Dbi<ByteBuffer> guavaDbi = env.buildDbi()
.setDbName(DB_1)
.withDefaultComparator()
.setDbiFlags(MDB_CREATE)
.open();
populateDatabase(guavaDbi);
verify(openClosedBackward(bb(7), bb(2)), guavaDbi, 6, 4, 2);
verify(openClosedBackward(bb(8), bb(4)), guavaDbi, 6, 4);
Expand Down Expand Up @@ -548,13 +551,13 @@ public Stream<? extends Arguments> provideArguments(ParameterDeclarations parame
final DbiFactory callbackComparatorDb = new DbiFactory("callbackComparator", env ->
env.buildDbi()
.setDbName(DB_3)
.withCallbackComparator(BUFFER_PROXY.getComparator(DBI_FLAGS))
.withCallbackComparator(BUFFER_PROXY::getComparator)
.setDbiFlags(DBI_FLAGS)
.open());
final DbiFactory iteratorComparatorDb = new DbiFactory("iteratorComparator", env ->
env.buildDbi()
.setDbName(DB_4)
.withIteratorComparator(BUFFER_PROXY.getComparator(DBI_FLAGS))
.withIteratorComparator(BUFFER_PROXY::getComparator)
.setDbiFlags(DBI_FLAGS)
.open());
return Stream.of(
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/lmdbjava/DbiBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void callback() {

final Dbi<ByteBuffer> dbi = env.buildDbi()
.setDbName("foo")
.withCallbackComparator(comparator)
.withCallbackComparator(ignored -> comparator)
.addDbiFlags(DbiFlags.MDB_CREATE)
.open();

Expand Down
Loading