-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathCursorIterableIntegerKeyTest.java
More file actions
661 lines (593 loc) · 21.9 KB
/
Copy pathCursorIterableIntegerKeyTest.java
File metadata and controls
661 lines (593 loc) · 21.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/*
* Copyright © 2016-2025 The LmdbJava Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.lmdbjava;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.lmdbjava.DbiFlags.MDB_CREATE;
import static org.lmdbjava.DbiFlags.MDB_INTEGERKEY;
import static org.lmdbjava.EnvFlags.MDB_NOSUBDIR;
import static org.lmdbjava.KeyRange.all;
import static org.lmdbjava.KeyRange.allBackward;
import static org.lmdbjava.KeyRange.atLeast;
import static org.lmdbjava.KeyRange.atLeastBackward;
import static org.lmdbjava.KeyRange.atMost;
import static org.lmdbjava.KeyRange.atMostBackward;
import static org.lmdbjava.KeyRange.closed;
import static org.lmdbjava.KeyRange.closedBackward;
import static org.lmdbjava.KeyRange.closedOpen;
import static org.lmdbjava.KeyRange.closedOpenBackward;
import static org.lmdbjava.KeyRange.greaterThan;
import static org.lmdbjava.KeyRange.greaterThanBackward;
import static org.lmdbjava.KeyRange.lessThan;
import static org.lmdbjava.KeyRange.lessThanBackward;
import static org.lmdbjava.KeyRange.open;
import static org.lmdbjava.KeyRange.openBackward;
import static org.lmdbjava.KeyRange.openClosed;
import static org.lmdbjava.KeyRange.openClosedBackward;
import static org.lmdbjava.PutFlags.MDB_NOOVERWRITE;
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.bb;
import static org.lmdbjava.TestUtils.bbNative;
import static org.lmdbjava.TestUtils.getNativeInt;
import static org.lmdbjava.TestUtils.getNativeIntOrLong;
import static org.lmdbjava.TestUtils.getNativeLong;
import static org.lmdbjava.TestUtils.getString;
import com.google.common.primitives.UnsignedBytes;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.Parameter;
import org.junit.jupiter.params.ParameterizedClass;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.support.ParameterDeclarations;
import org.lmdbjava.CursorIterable.KeyVal;
/**
* Test {@link CursorIterable} using {@link DbiFlags#MDB_INTEGERKEY} to ensure that comparators work
* with native order integer keys.
*/
@ParameterizedClass(name = "{index}: dbi: {0}")
@ArgumentsSource(CursorIterableIntegerKeyTest.MyArgumentProvider.class)
public final class CursorIterableIntegerKeyTest {
private static final DbiFlagSet DBI_FLAGS = DbiFlagSet.of(MDB_CREATE, MDB_INTEGERKEY);
private static final BufferProxy<ByteBuffer> BUFFER_PROXY = ByteBufferProxy.PROXY_OPTIMAL;
private TempDir tempDir;
private Env<ByteBuffer> env;
private Deque<Integer> list;
@Parameter public DbiFactory dbiFactory;
@BeforeEach
public void before() throws IOException {
tempDir = new TempDir();
final BufferProxy<ByteBuffer> bufferProxy = ByteBufferProxy.PROXY_OPTIMAL;
env =
Env.create(bufferProxy)
.setMapSize(256, ByteUnit.KIBIBYTES)
.setMaxReaders(1)
.setMaxDbs(3)
.setEnvFlags(MDB_NOSUBDIR)
.open(tempDir.createTempFile());
populateTestDataList();
}
@AfterEach
public void after() {
env.close();
tempDir.cleanup();
}
@Test
public void testNumericOrderLong() {
final Dbi<ByteBuffer> dbi = dbiFactory.factory.apply(env);
try (Txn<ByteBuffer> txn = env.txnWrite()) {
final Cursor<ByteBuffer> c = dbi.openCursor(txn);
long i = 1;
while (true) {
// System.out.println("putting " + i);
c.put(bbNative(i), bb(i + "-long"));
final long i2 = i * 10;
if (i2 < i) {
// Overflowed
break;
}
i = i2;
}
txn.commit();
}
final List<Map.Entry<Long, String>> entries = new ArrayList<>();
try (Txn<ByteBuffer> txn = env.txnRead()) {
try (CursorIterable<ByteBuffer> iterable = dbi.iterate(txn)) {
for (KeyVal<ByteBuffer> keyVal : iterable) {
assertThat(keyVal.key().remaining()).isEqualTo(Long.BYTES);
final String val = getString(keyVal.val());
final long key = getNativeLong(keyVal.key());
entries.add(new AbstractMap.SimpleEntry<>(key, val));
// System.out.println(val);
}
}
}
final List<Long> dbKeys = entries.stream().map(Map.Entry::getKey).collect(Collectors.toList());
final List<Long> dbKeysSorted =
entries.stream().map(Map.Entry::getKey).sorted().collect(Collectors.toList());
for (int i = 0; i < dbKeys.size(); i++) {
final long dbKey1 = dbKeys.get(i);
final long dbKey2 = dbKeysSorted.get(i);
assertThat(dbKey1).isEqualTo(dbKey2);
}
}
@Test
public void testNumericOrderInt() {
final Dbi<ByteBuffer> dbi = dbiFactory.factory.apply(env);
try (Txn<ByteBuffer> txn = env.txnWrite()) {
final Cursor<ByteBuffer> c = dbi.openCursor(txn);
int i = 1;
while (true) {
// System.out.println("putting " + i);
c.put(bbNative(i), bb(i + "-int"));
final int i2 = i * 10;
if (i2 < i) {
// Overflowed
break;
}
i = i2;
}
txn.commit();
}
final List<Map.Entry<Integer, String>> entries = new ArrayList<>();
try (Txn<ByteBuffer> txn = env.txnRead()) {
try (CursorIterable<ByteBuffer> iterable = dbi.iterate(txn)) {
for (KeyVal<ByteBuffer> keyVal : iterable) {
assertThat(keyVal.key().remaining()).isEqualTo(Integer.BYTES);
final String val = getString(keyVal.val());
final int key = TestUtils.getNativeInt(keyVal.key());
entries.add(new AbstractMap.SimpleEntry<>(key, val));
// System.out.println(val);
}
}
}
final List<Integer> dbKeys =
entries.stream().map(Map.Entry::getKey).collect(Collectors.toList());
final List<Integer> dbKeysSorted =
entries.stream().map(Map.Entry::getKey).sorted().collect(Collectors.toList());
for (int i = 0; i < dbKeys.size(); i++) {
final long dbKey1 = dbKeys.get(i);
final long dbKey2 = dbKeysSorted.get(i);
assertThat(dbKey1).isEqualTo(dbKey2);
}
}
@Test
public void testIntegerKeyKeySize() {
final Dbi<ByteBuffer> db = dbiFactory.factory.apply(env);
long maxIntAsLong = Integer.MAX_VALUE;
try (Txn<ByteBuffer> txn = env.txnWrite()) {
// System.out.println("Flags: " + db.listFlags(txn));
int val = 0;
db.put(txn, bbNative(0L), bb("val_" + ++val));
db.put(txn, bbNative(10L), bb("val_" + ++val));
db.put(txn, bbNative(maxIntAsLong - 1_111_111_111), bb("val_" + ++val));
db.put(txn, bbNative(maxIntAsLong - 111_111_111), bb("val_" + ++val));
db.put(txn, bbNative(maxIntAsLong - 111_111), bb("val_" + ++val));
db.put(txn, bbNative(maxIntAsLong - 111), bb("val_" + ++val));
db.put(txn, bbNative(maxIntAsLong + 111), bb("val_" + ++val));
db.put(txn, bbNative(maxIntAsLong + 111_111), bb("val_" + ++val));
db.put(txn, bbNative(maxIntAsLong + 111_111_111), bb("val_" + ++val));
db.put(txn, bbNative(maxIntAsLong + 1_111_111_111), bb("val_" + ++val));
db.put(txn, bbNative(Long.MAX_VALUE), bb("val_" + ++val));
txn.commit();
}
// try (Txn<ByteBuffer> txn = env.txnRead()) {
// try (CursorIterable<ByteBuffer> iterable = db.iterate(txn)) {
// for (KeyVal<ByteBuffer> keyVal : iterable) {
// final String val = getString(keyVal.val());
// final long key = getNativeLong(keyVal.key());
// final int remaining = keyVal.key().remaining();
// System.out.println("key: " + key + ", val: " + val + ", remaining: " + remaining);
// }
// }
// }
}
@Test
public void allBackwardTest() {
verify(allBackward(), 8, 6, 4, 2);
}
@Test
public void allTest() {
verify(all(), 2, 4, 6, 8);
}
@Test
public void atLeastBackwardTest() {
verify(atLeastBackward(bbNative(5)), 4, 2);
verify(atLeastBackward(bbNative(6)), 6, 4, 2);
verify(atLeastBackward(bbNative(9)), 8, 6, 4, 2);
}
@Test
public void atLeastTest() {
verify(atLeast(bbNative(5)), 6, 8);
verify(atLeast(bbNative(6)), 6, 8);
}
@Test
public void atMostBackwardTest() {
verify(atMostBackward(bbNative(5)), 8, 6);
verify(atMostBackward(bbNative(6)), 8, 6);
}
@Test
public void atMostTest() {
verify(atMost(bbNative(5)), 2, 4);
verify(atMost(bbNative(6)), 2, 4, 6);
}
private void populateTestDataList() {
list = new LinkedList<>();
list.addAll(asList(2, 3, 4, 5, 6, 7, 8, 9));
}
private void populateDatabase(final Dbi<ByteBuffer> dbi) {
try (Txn<ByteBuffer> txn = env.txnWrite()) {
final Cursor<ByteBuffer> c = dbi.openCursor(txn);
c.put(bbNative(2), bb(3), MDB_NOOVERWRITE);
c.put(bbNative(4), bb(5));
c.put(bbNative(6), bb(7));
c.put(bbNative(8), bb(9));
txn.commit();
}
}
@Test
public void closedBackwardTest() {
verify(closedBackward(bbNative(7), bbNative(3)), 6, 4);
verify(closedBackward(bbNative(6), bbNative(2)), 6, 4, 2);
verify(closedBackward(bbNative(9), bbNative(3)), 8, 6, 4);
}
@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);
}
@Test
public void closedOpenTest() {
verify(closedOpen(bbNative(3), bbNative(8)), 4, 6);
verify(closedOpen(bbNative(2), bbNative(6)), 2, 4);
}
@Test
public void closedTest() {
verify(closed(bbNative(3), bbNative(7)), 4, 6);
verify(closed(bbNative(2), bbNative(6)), 2, 4, 6);
verify(closed(bbNative(1), bbNative(7)), 2, 4, 6);
}
@Test
public void greaterThanBackwardTest() {
verify(greaterThanBackward(bbNative(6)), 4, 2);
verify(greaterThanBackward(bbNative(7)), 6, 4, 2);
verify(greaterThanBackward(bbNative(9)), 8, 6, 4, 2);
}
@Test
public void greaterThanTest() {
verify(greaterThan(bbNative(4)), 6, 8);
verify(greaterThan(bbNative(3)), 4, 6, 8);
}
public void iterableOnlyReturnedOnce() {
Assertions.assertThatThrownBy(
() -> {
final Dbi<ByteBuffer> db = getDb();
try (Txn<ByteBuffer> txn = env.txnRead();
CursorIterable<ByteBuffer> c = db.iterate(txn)) {
c.iterator(); // ok
c.iterator(); // fails
}
})
.isInstanceOf(IllegalStateException.class);
}
@Test
public void iterate() {
populateTestDataList();
final Dbi<ByteBuffer> db = getDb();
try (Txn<ByteBuffer> txn = env.txnRead();
CursorIterable<ByteBuffer> c = db.iterate(txn)) {
for (final KeyVal<ByteBuffer> kv : c) {
assertThat(getNativeInt(kv.key())).isEqualTo(list.pollFirst());
assertThat(kv.val().getInt()).isEqualTo(list.pollFirst());
}
}
}
public void iteratorOnlyReturnedOnce() {
Assertions.assertThatThrownBy(
() -> {
final Dbi<ByteBuffer> db = getDb();
try (Txn<ByteBuffer> txn = env.txnRead();
CursorIterable<ByteBuffer> c = db.iterate(txn)) {
c.iterator(); // ok
c.iterator(); // fails
}
})
.isInstanceOf(IllegalStateException.class);
}
@Test
public void lessThanBackwardTest() {
verify(lessThanBackward(bbNative(5)), 8, 6);
verify(lessThanBackward(bbNative(2)), 8, 6, 4);
}
@Test
public void lessThanTest() {
verify(lessThan(bbNative(5)), 2, 4);
verify(lessThan(bbNative(8)), 2, 4, 6);
}
public void nextThrowsNoSuchElementExceptionIfNoMoreElements() {
Assertions.assertThatThrownBy(
() -> {
populateTestDataList();
final Dbi<ByteBuffer> db = getDb();
try (Txn<ByteBuffer> txn = env.txnRead();
CursorIterable<ByteBuffer> c = db.iterate(txn)) {
final Iterator<KeyVal<ByteBuffer>> i = c.iterator();
while (i.hasNext()) {
final KeyVal<ByteBuffer> kv = i.next();
assertThat(getNativeInt(kv.key())).isEqualTo(list.pollFirst());
assertThat(kv.val().getInt()).isEqualTo(list.pollFirst());
}
assertThat(i.hasNext()).isEqualTo(false);
i.next();
}
})
.isInstanceOf(NoSuchElementException.class);
}
@Test
public void openBackwardTest() {
verify(openBackward(bbNative(7), bbNative(2)), 6, 4);
verify(openBackward(bbNative(8), bbNative(1)), 6, 4, 2);
verify(openBackward(bbNative(9), bbNative(4)), 8, 6);
}
@Test
public void openClosedBackwardTest() {
verify(openClosedBackward(bbNative(7), bbNative(2)), 6, 4, 2);
verify(openClosedBackward(bbNative(8), bbNative(4)), 6, 4);
verify(openClosedBackward(bbNative(9), bbNative(4)), 8, 6, 4);
}
@Test
public void openClosedBackwardTestWithGuava() {
final Comparator<byte[]> guava = UnsignedBytes.lexicographicalComparator();
final Comparator<ByteBuffer> comparator =
(bb1, bb2) -> {
final byte[] array1 = new byte[bb1.remaining()];
final byte[] array2 = new byte[bb2.remaining()];
bb1.mark();
bb2.mark();
bb1.get(array1);
bb2.get(array2);
bb1.reset();
bb2.reset();
return guava.compare(array1, array2);
};
final Dbi<ByteBuffer> guavaDbi =
env.createDbi()
.setDbName(DB_1)
.withIteratorComparator(ignored -> comparator)
.setDbiFlags(MDB_CREATE)
.open();
populateDatabase(guavaDbi);
verify(openClosedBackward(bbNative(7), bbNative(2)), guavaDbi, 6, 4, 2);
verify(openClosedBackward(bbNative(8), bbNative(4)), guavaDbi, 6, 4);
}
@Test
public void openClosedTest() {
verify(openClosed(bbNative(3), bbNative(8)), 4, 6, 8);
verify(openClosed(bbNative(2), bbNative(6)), 4, 6);
}
@Test
public void openTest() {
verify(open(bbNative(3), bbNative(7)), 4, 6);
verify(open(bbNative(2), bbNative(8)), 4, 6);
}
@Test
public void removeOddElements() {
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)) {
final Iterator<KeyVal<ByteBuffer>> c = ci.iterator();
while (c.hasNext()) {
c.next();
idx++;
if (idx % 2 == 0) {
c.remove();
}
}
}
txn.commit();
}
verify(db, all(), 4, 8);
}
public void nextWithClosedEnvTest() {
Assertions.assertThatThrownBy(
() -> {
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.next();
}
}
})
.isInstanceOf(Env.AlreadyClosedException.class);
}
public void removeWithClosedEnvTest() {
Assertions.assertThatThrownBy(
() -> {
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();
final KeyVal<ByteBuffer> keyVal = c.next();
assertThat(keyVal).isNotNull();
env.close();
c.remove();
}
}
})
.isInstanceOf(Env.AlreadyClosedException.class);
}
public void hasNextWithClosedEnvTest() {
Assertions.assertThatThrownBy(
() -> {
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.hasNext();
}
}
})
.isInstanceOf(Env.AlreadyClosedException.class);
}
public void forEachRemainingWithClosedEnvTest() {
Assertions.assertThatThrownBy(
() -> {
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 -> {});
}
}
})
.isInstanceOf(Env.AlreadyClosedException.class);
}
private void verify(final KeyRange<ByteBuffer> range, final int... expected) {
// Verify using all comparator types
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) {
final List<Integer> results = new ArrayList<>();
try (Txn<ByteBuffer> txn = env.txnRead();
CursorIterable<ByteBuffer> c = dbi.iterate(txn, range)) {
for (final KeyVal<ByteBuffer> kv : c) {
final int key = kv.key().order(ByteOrder.nativeOrder()).getInt();
final int val = kv.val().getInt();
results.add(key);
assertThat(val).isEqualTo(key + 1);
}
}
assertThat(results).hasSize(expected.length);
for (int idx = 0; idx < results.size(); idx++) {
assertThat(results.get(idx)).isEqualTo(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;
}
}
static class MyArgumentProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(
ParameterDeclarations parameters, ExtensionContext context) throws Exception {
final DbiFactory defaultComparatorDb =
new DbiFactory(
"defaultComparator",
env ->
env.createDbi()
.setDbName(DB_1)
.withDefaultComparator()
.setDbiFlags(DBI_FLAGS)
.open());
final DbiFactory nativeComparatorDb =
new DbiFactory(
"nativeComparator",
env ->
env.createDbi()
.setDbName(DB_2)
.withNativeComparator()
.setDbiFlags(DBI_FLAGS)
.open());
final DbiFactory callbackComparatorDb =
new DbiFactory(
"callbackComparator",
env ->
env.createDbi()
.setDbName(DB_3)
.withCallbackComparator(MyArgumentProvider::buildComparator)
.setDbiFlags(DBI_FLAGS)
.open());
final DbiFactory iteratorComparatorDb =
new DbiFactory(
"iteratorComparator",
env ->
env.createDbi()
.setDbName(DB_4)
.withIteratorComparator(MyArgumentProvider::buildComparator)
.setDbiFlags(DBI_FLAGS)
.open());
return Stream.of(
defaultComparatorDb, nativeComparatorDb, callbackComparatorDb, iteratorComparatorDb)
.map(Arguments::of);
}
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()) {
// Make sure LMDB is always giving us consistent key lengths.
Assertions.fail(
"o1: "
+ o1
+ " "
+ getNativeIntOrLong(o1)
+ ", o2: "
+ o2
+ " "
+ getNativeIntOrLong(o2));
}
return baseComparator.compare(o1, o2);
};
}
}
}