Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3524995

Browse files
committed
Refactor Comparator handling (fixeslmdbjava#199)
1 parente4cf6e9 commit3524995

File tree

11 files changed

+157
-110
lines changed

11 files changed

+157
-110
lines changed

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
importstaticjava.lang.Long.BYTES;
2424

25+
importjava.util.Comparator;
26+
2527
importjnr.ffi.Pointer;
2628

2729
/**
@@ -61,17 +63,16 @@ public abstract class BufferProxy<T> {
6163
protectedabstractTallocate();
6264

6365
/**
64-
*Comparethetwo buffers.
66+
*Get a suitable default {@link Comparator} giventheprovided flags.
6567
*
6668
* <p>
67-
*Implemented as a protected method to discourage use ofthebuffer proxy
68-
* incollections etc (given by design it wraps a temporary value only).
69+
*The provided comparator must strictly matchthelexicographical order of
70+
*keysinthe native LMDB database.
6971
*
70-
* @param o1 left operand
71-
* @param o2 right operand
72-
* @return as per {@link Comparable}
72+
* @param flags for the database
73+
* @return a comparator that can be used (never null)
7374
*/
74-
protectedabstractintcompare(To1,To2);
75+
protectedabstractComparator<T>getComparator(DbiFlags...flags);
7576

7677
/**
7778
* Deallocate a buffer that was previously provided by {@link #allocate()}.

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
importstaticorg.lmdbjava.Library.RUNTIME;
2525

2626
importjava.util.Arrays;
27+
importjava.util.Comparator;
2728

2829
importjnr.ffi.Pointer;
2930
importjnr.ffi.provider.MemoryManager;
@@ -78,7 +79,6 @@ protected byte[] allocate() {
7879
returnnewbyte[0];
7980
}
8081

81-
@Override
8282
protectedintcompare(finalbyte[]o1,finalbyte[]o2) {
8383
returncompareArrays(o1,o2);
8484
}
@@ -93,6 +93,11 @@ protected byte[] getBytes(final byte[] buffer) {
9393
returnArrays.copyOf(buffer,buffer.length);
9494
}
9595

96+
@Override
97+
protectedComparator<byte[]>getComparator(finalDbiFlags...flags) {
98+
returnthis::compare;
99+
}
100+
96101
@Override
97102
protectedvoidin(finalbyte[]buffer,finalPointerptr,
98103
finallongptrAddr) {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
importstaticorg.lmdbjava.UnsafeAccess.UNSAFE;
2626

2727
importjava.lang.reflect.Field;
28+
importjava.util.Comparator;
2829

2930
importio.netty.buffer.ByteBuf;
3031
importio.netty.buffer.PooledByteBufAllocator;
@@ -106,11 +107,15 @@ protected ByteBuf allocate() {
106107
thrownewIllegalStateException("Netty buffer must be " +NAME);
107108
}
108109

109-
@Override
110110
protectedintcompare(finalByteBufo1,finalByteBufo2) {
111111
returno1.compareTo(o2);
112112
}
113113

114+
@Override
115+
protectedComparator<ByteBuf>getComparator(finalDbiFlags...flags) {
116+
returnthis::compare;
117+
}
118+
114119
@Override
115120
protectedvoiddeallocate(finalByteBufbuff) {
116121
buff.release();

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@
2626
importstaticjava.nio.ByteOrder.BIG_ENDIAN;
2727
importstaticjava.nio.ByteOrder.LITTLE_ENDIAN;
2828
importstaticjava.util.Objects.requireNonNull;
29+
importstaticorg.lmdbjava.DbiFlags.MDB_INTEGERKEY;
2930
importstaticorg.lmdbjava.Env.SHOULD_CHECK;
31+
importstaticorg.lmdbjava.MaskedFlag.isSet;
32+
importstaticorg.lmdbjava.MaskedFlag.mask;
3033
importstaticorg.lmdbjava.UnsafeAccess.UNSAFE;
3134

3235
importjava.lang.reflect.Field;
3336
importjava.nio.Buffer;
3437
importjava.nio.ByteBuffer;
3538
importjava.util.ArrayDeque;
39+
importjava.util.Comparator;
3640

3741
importjnr.ffi.Pointer;
3842

@@ -189,7 +193,21 @@ protected final ByteBuffer allocate() {
189193
}
190194

191195
@Override
192-
protectedfinalintcompare(finalByteBuffero1,finalByteBuffero2) {
196+
protectedComparator<ByteBuffer>getComparator(finalDbiFlags...flags) {
197+
finalintflagInt =mask(flags);
198+
if (isSet(flagInt,MDB_INTEGERKEY)) {
199+
returnthis::compareCustom;
200+
}
201+
returnthis::compareDefault;
202+
}
203+
204+
protectedfinalintcompareDefault(finalByteBuffero1,
205+
finalByteBuffero2) {
206+
returno1.compareTo(o2);
207+
}
208+
209+
protectedfinalintcompareCustom(finalByteBuffero1,
210+
finalByteBuffero2) {
193211
returncompareBuff(o1,o2);
194212
}
195213

‎src/main/java/org/lmdbjava/Dbi.java‎

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -56,38 +56,35 @@ public final class Dbi<T> {
5656

5757
privatefinalComparatorCallbackccb;
5858
privatebooleancleaned;
59-
privatefinalComparator<T>compFunc;
59+
privatefinalComparator<T>comparator;
6060
privatefinalEnv<T>env;
6161
privatefinalbyte[]name;
62-
privatefinalBufferProxy<T>proxy;
6362
privatefinalPointerptr;
6463

6564
Dbi(finalEnv<T>env,finalTxn<T>txn,finalbyte[]name,
66-
finalComparator<T>comparator,finalDbiFlags...flags) {
65+
finalComparator<T>comparator,finalbooleannativeCb,
66+
finalBufferProxy<T>proxy,finalDbiFlags...flags) {
6767
this.env =env;
6868
this.name =name ==null ?null :Arrays.copyOf(name,name.length);
69+
this.comparator =comparator;
6970
finalintflagsMask =mask(flags);
7071
finalPointerdbiPtr =allocateDirect(RUNTIME,ADDRESS);
7172
checkRc(LIB.mdb_dbi_open(txn.pointer(),name,flagsMask,dbiPtr));
7273
ptr =dbiPtr.getPointer(0);
73-
if (comparator ==null) {
74-
proxy =null;
75-
compFunc =null;
76-
ccb =null;
77-
}else {
78-
this.proxy =txn.getProxy();
79-
this.compFunc =comparator;
74+
if (nativeCb) {
8075
this.ccb = (keyA,keyB) -> {
8176
finalTcompKeyA =proxy.allocate();
8277
finalTcompKeyB =proxy.allocate();
8378
proxy.out(compKeyA,keyA,keyA.address());
8479
proxy.out(compKeyB,keyB,keyB.address());
85-
finalintresult =compFunc.compare(compKeyA,compKeyB);
80+
finalintresult =this.comparator.compare(compKeyA,compKeyB);
8681
proxy.deallocate(compKeyA);
8782
proxy.deallocate(compKeyB);
8883
returnresult;
8984
};
9085
LIB.mdb_set_compare(txn.pointer(),ptr,ccb);
86+
}else {
87+
ccb =null;
9188
}
9289
}
9390

@@ -265,51 +262,20 @@ public CursorIterable<T> iterate(final Txn<T> txn) {
265262
}
266263

267264
/**
268-
* Iterate the database in accordance with the provided {@link KeyRange} and
269-
* default {@link Comparator}.
265+
* Iterate the database in accordance with the provided {@link KeyRange}.
270266
*
271267
* @param txn transaction handle (not null; not committed)
272268
* @param range range of acceptable keys (not null)
273269
* @return iterator (never null)
274270
*/
275271
publicCursorIterable<T>iterate(finalTxn<T>txn,finalKeyRange<T>range) {
276-
returniterate(txn,range,null);
277-
}
278-
279-
/**
280-
* Iterate the database in accordance with the provided {@link KeyRange} and
281-
* {@link Comparator}.
282-
*
283-
* <p>
284-
* If a comparator is provided, it must reflect the same ordering as LMDB uses
285-
* for cursor operations (eg first, next, last, previous etc).
286-
*
287-
* <p>
288-
* If a null comparator is provided, any comparator provided when opening the
289-
* database is used. If no database comparator was specified, the buffer's
290-
* default comparator is used. Such buffer comparators reflect LMDB's default
291-
* lexicographical order.
292-
*
293-
* @param txn transaction handle (not null; not committed)
294-
* @param range range of acceptable keys (not null)
295-
* @param comparator custom comparator for keys (may be null)
296-
* @return iterator (never null)
297-
*/
298-
publicCursorIterable<T>iterate(finalTxn<T>txn,finalKeyRange<T>range,
299-
finalComparator<T>comparator) {
300272
if (SHOULD_CHECK) {
301273
requireNonNull(txn);
302274
requireNonNull(range);
303275
env.checkNotClosed();
304276
txn.checkReady();
305277
}
306-
finalComparator<T>useComp;
307-
if (comparator ==null) {
308-
useComp =compFunc ==null ?txn.comparator() :compFunc;
309-
}else {
310-
useComp =comparator;
311-
}
312-
returnnewCursorIterable<>(txn,this,range,useComp);
278+
returnnewCursorIterable<>(txn,this,range,comparator);
313279
}
314280

315281
/*

‎src/main/java/org/lmdbjava/DirectBufferProxy.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
importjava.nio.ByteBuffer;
3030
importjava.util.ArrayDeque;
31+
importjava.util.Comparator;
3132

3233
importjnr.ffi.Pointer;
3334
importorg.agrona.DirectBuffer;
@@ -111,7 +112,6 @@ protected DirectBuffer allocate() {
111112
}
112113
}
113114

114-
@Override
115115
protectedintcompare(finalDirectBuffero1,finalDirectBuffero2) {
116116
returncompareBuff(o1,o2);
117117
}
@@ -129,6 +129,11 @@ protected byte[] getBytes(final DirectBuffer buffer) {
129129
returndest;
130130
}
131131

132+
@Override
133+
protectedComparator<DirectBuffer>getComparator(finalDbiFlags...flags) {
134+
returnthis::compare;
135+
}
136+
132137
@Override
133138
protectedvoidin(finalDirectBufferbuffer,finalPointerptr,
134139
finallongptrAddr) {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp