Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Next Next commit
gh-249 Remove MDB_UNSIGNEDKEY, let CursorIterable call mdb_cmp
There are now essentially three ways of configuring comparators
when creating a Dbi.

**null comparator**
LMDB will use its own comparator & CursorIterable will call down
to mdb_cmp for comparisons between the current cursor key and the
range start/stop key.

**provided comparator**
LMDB will use its own comparator & CursorIterable will use the
provided comparator for comparisons between the current cursor
key and the range start/stop key.

**provided comparator with nativeCb==true**
LMDB will call back to java for all comparator duties.
CursorIterable will use the same provided comparator for
comparisons between the current cursor key and the range
start/stop key.

The methods `getSignedComparator()` and `getUnsignedComparator()`
have been made public so users of this library can access them.
  • Loading branch information
at055612 committed Mar 6, 2025
commit 480e984417c17915b31e33de193ff08e118fcec9
40 changes: 17 additions & 23 deletions src/main/java/org/lmdbjava/BufferProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
package org.lmdbjava;

import static java.lang.Long.BYTES;
import static org.lmdbjava.DbiFlags.MDB_INTEGERKEY;
import static org.lmdbjava.DbiFlags.MDB_UNSIGNEDKEY;
import static org.lmdbjava.MaskedFlag.isSet;
import static org.lmdbjava.MaskedFlag.mask;

import java.util.Comparator;
import jnr.ffi.Pointer;
Expand Down Expand Up @@ -70,36 +66,25 @@ protected BufferProxy() {}
*/
protected abstract byte[] getBytes(T buffer);

/**
* Get a suitable default {@link Comparator} given the provided flags.
*
* <p>The provided comparator must strictly match the lexicographical order of keys in the native
* LMDB database.
*
* @param flags for the database
* @return a comparator that can be used (never null)
*/
protected Comparator<T> getComparator(DbiFlags... flags) {
final int intFlag = mask(flags);

return isSet(intFlag, MDB_INTEGERKEY) || isSet(intFlag, MDB_UNSIGNEDKEY)
? getUnsignedComparator()
: getSignedComparator();
}

/**
* Get a suitable default {@link Comparator} to compare numeric key values as signed.
*
* <p>
* Note: LMDB's default comparator is unsigned so if this is used only for the {@link CursorIterable}
* start/stop key comparisons then its behaviour will differ from the iteration order. Use
* with caution.
* </p>
*
* @return a comparator that can be used (never null)
*/
protected abstract Comparator<T> getSignedComparator();
public abstract Comparator<T> getSignedComparator();

/**
* Get a suitable default {@link Comparator} to compare numeric key values as unsigned.
*
* @return a comparator that can be used (never null)
*/
protected abstract Comparator<T> getUnsignedComparator();
public abstract Comparator<T> getUnsignedComparator();

/**
* Called when the <code>MDB_val</code> should be set to reflect the passed buffer. This buffer
Expand Down Expand Up @@ -140,4 +125,13 @@ protected Comparator<T> getComparator(DbiFlags... flags) {
final KeyVal<T> keyVal() {
return new KeyVal<>(this);
}

/**
* Create a new {@link Key} to hold pointers for this buffer proxy.
*
* @return a non-null key holder
*/
final Key<T> key() {
return new Key<>(this);
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/lmdbjava/ByteArrayProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ protected byte[] getBytes(final byte[] buffer) {
}

@Override
protected Comparator<byte[]> getSignedComparator() {
public Comparator<byte[]> getSignedComparator() {
return signedComparator;
}

@Override
protected Comparator<byte[]> getUnsignedComparator() {
public Comparator<byte[]> getUnsignedComparator() {
return unsignedComparator;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/lmdbjava/ByteBufProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ protected ByteBuf allocate() {
}

@Override
protected Comparator<ByteBuf> getSignedComparator() {
public Comparator<ByteBuf> getSignedComparator() {
return comparator;
}

@Override
protected Comparator<ByteBuf> getUnsignedComparator() {
public Comparator<ByteBuf> getUnsignedComparator() {
return comparator;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/lmdbjava/ByteBufferProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ protected final ByteBuffer allocate() {
}

@Override
protected Comparator<ByteBuffer> getSignedComparator() {
public Comparator<ByteBuffer> getSignedComparator() {
return signedComparator;
}

@Override
protected Comparator<ByteBuffer> getUnsignedComparator() {
public Comparator<ByteBuffer> getUnsignedComparator() {
return unsignedComparator;
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/lmdbjava/Cursor.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ public T key() {
return kv.key();
}

KeyVal<T> keyVal() {
return kv;
}

/**
* Position at last key/data item.
*
Expand Down
Loading
Loading