Skip to content

Commit 480e984

Browse files
committed
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.
1 parent 154df33 commit 480e984

20 files changed

Lines changed: 768 additions & 315 deletions

src/main/java/org/lmdbjava/BufferProxy.java

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616
package org.lmdbjava;
1717

1818
import static java.lang.Long.BYTES;
19-
import static org.lmdbjava.DbiFlags.MDB_INTEGERKEY;
20-
import static org.lmdbjava.DbiFlags.MDB_UNSIGNEDKEY;
21-
import static org.lmdbjava.MaskedFlag.isSet;
22-
import static org.lmdbjava.MaskedFlag.mask;
2319

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

73-
/**
74-
* Get a suitable default {@link Comparator} given the provided flags.
75-
*
76-
* <p>The provided comparator must strictly match the lexicographical order of keys in the native
77-
* LMDB database.
78-
*
79-
* @param flags for the database
80-
* @return a comparator that can be used (never null)
81-
*/
82-
protected Comparator<T> getComparator(DbiFlags... flags) {
83-
final int intFlag = mask(flags);
84-
85-
return isSet(intFlag, MDB_INTEGERKEY) || isSet(intFlag, MDB_UNSIGNEDKEY)
86-
? getUnsignedComparator()
87-
: getSignedComparator();
88-
}
89-
9069
/**
9170
* Get a suitable default {@link Comparator} to compare numeric key values as signed.
9271
*
72+
* <p>
73+
* Note: LMDB's default comparator is unsigned so if this is used only for the {@link CursorIterable}
74+
* start/stop key comparisons then its behaviour will differ from the iteration order. Use
75+
* with caution.
76+
* </p>
77+
*
9378
* @return a comparator that can be used (never null)
9479
*/
95-
protected abstract Comparator<T> getSignedComparator();
80+
public abstract Comparator<T> getSignedComparator();
9681

9782
/**
9883
* Get a suitable default {@link Comparator} to compare numeric key values as unsigned.
9984
*
10085
* @return a comparator that can be used (never null)
10186
*/
102-
protected abstract Comparator<T> getUnsignedComparator();
87+
public abstract Comparator<T> getUnsignedComparator();
10388

10489
/**
10590
* Called when the <code>MDB_val</code> should be set to reflect the passed buffer. This buffer
@@ -140,4 +125,13 @@ protected Comparator<T> getComparator(DbiFlags... flags) {
140125
final KeyVal<T> keyVal() {
141126
return new KeyVal<>(this);
142127
}
128+
129+
/**
130+
* Create a new {@link Key} to hold pointers for this buffer proxy.
131+
*
132+
* @return a non-null key holder
133+
*/
134+
final Key<T> key() {
135+
return new Key<>(this);
136+
}
143137
}

src/main/java/org/lmdbjava/ByteArrayProxy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ protected byte[] getBytes(final byte[] buffer) {
104104
}
105105

106106
@Override
107-
protected Comparator<byte[]> getSignedComparator() {
107+
public Comparator<byte[]> getSignedComparator() {
108108
return signedComparator;
109109
}
110110

111111
@Override
112-
protected Comparator<byte[]> getUnsignedComparator() {
112+
public Comparator<byte[]> getUnsignedComparator() {
113113
return unsignedComparator;
114114
}
115115

src/main/java/org/lmdbjava/ByteBufProxy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ protected ByteBuf allocate() {
114114
}
115115

116116
@Override
117-
protected Comparator<ByteBuf> getSignedComparator() {
117+
public Comparator<ByteBuf> getSignedComparator() {
118118
return comparator;
119119
}
120120

121121
@Override
122-
protected Comparator<ByteBuf> getUnsignedComparator() {
122+
public Comparator<ByteBuf> getUnsignedComparator() {
123123
return comparator;
124124
}
125125

src/main/java/org/lmdbjava/ByteBufferProxy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,12 @@ protected final ByteBuffer allocate() {
182182
}
183183

184184
@Override
185-
protected Comparator<ByteBuffer> getSignedComparator() {
185+
public Comparator<ByteBuffer> getSignedComparator() {
186186
return signedComparator;
187187
}
188188

189189
@Override
190-
protected Comparator<ByteBuffer> getUnsignedComparator() {
190+
public Comparator<ByteBuffer> getUnsignedComparator() {
191191
return unsignedComparator;
192192
}
193193

src/main/java/org/lmdbjava/Cursor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ public T key() {
196196
return kv.key();
197197
}
198198

199+
KeyVal<T> keyVal() {
200+
return kv;
201+
}
202+
199203
/**
200204
* Position at last key/data item.
201205
*

0 commit comments

Comments
 (0)