4646import static org .lmdbjava .KeyRange .atMost ;
4747import static org .lmdbjava .PutFlags .MDB_NODUPDATA ;
4848import static org .lmdbjava .PutFlags .MDB_NOOVERWRITE ;
49- import static org .lmdbjava .TestUtils .DB_1 ;
50- import static org .lmdbjava .TestUtils .ba ;
51- import static org .lmdbjava .TestUtils .bb ;
49+ import static org .lmdbjava .TestUtils .*;
5250
5351import java .io .File ;
5452import java .io .IOException ;
6361import java .util .concurrent .Future ;
6462import java .util .concurrent .TimeoutException ;
6563import java .util .concurrent .atomic .AtomicBoolean ;
66- import java .util .function .BiConsumer ;
67- import org .agrona .concurrent .UnsafeBuffer ;
64+ import java .util .function .*;
6865import org .hamcrest .Matchers ;
6966import org .junit .After ;
7067import org .junit .Before ;
@@ -82,6 +79,7 @@ public final class DbiTest {
8279
8380 @ Rule public final TemporaryFolder tmp = new TemporaryFolder ();
8481 private Env <ByteBuffer > env ;
82+ private Env <byte []> envBa ;
8583
8684 @ After
8785 public void after () {
@@ -97,6 +95,13 @@ public void before() throws IOException {
9795 .setMaxReaders (2 )
9896 .setMaxDbs (2 )
9997 .open (path , MDB_NOSUBDIR );
98+ final File pathBa = tmp .newFile ();
99+ envBa =
100+ create (PROXY_BA )
101+ .setMapSize (MEBIBYTES .toBytes (64 ))
102+ .setMaxReaders (2 )
103+ .setMaxDbs (2 )
104+ .open (pathBa , MDB_NOSUBDIR );
100105 }
101106
102107 @ Test (expected = ConstantDerivedException .class )
@@ -117,20 +122,41 @@ public void customComparator() {
117122 }
118123 return lexical * -1 ;
119124 };
120- final Dbi <ByteBuffer > db = env .openDbi (DB_1 , reverseOrder , true , MDB_CREATE );
121- try (Txn <ByteBuffer > txn = env .txnWrite ()) {
122- assertThat (db .put (txn , bb (2 ), bb (3 )), is (true ));
123- assertThat (db .put (txn , bb (4 ), bb (6 )), is (true ));
124- assertThat (db .put (txn , bb (6 ), bb (7 )), is (true ));
125- assertThat (db .put (txn , bb (8 ), bb (7 )), is (true ));
125+ doCustomComparator (env , reverseOrder , TestUtils ::bb , ByteBuffer ::getInt );
126+ }
127+
128+ @ Test
129+ public void customComparatorByteArray () {
130+ final Comparator <byte []> reverseOrder =
131+ (o1 , o2 ) -> {
132+ final int lexical = PROXY_BA .getComparator ().compare (o1 , o2 );
133+ if (lexical == 0 ) {
134+ return 0 ;
135+ }
136+ return lexical * -1 ;
137+ };
138+ doCustomComparator (envBa , reverseOrder , TestUtils ::ba , TestUtils ::fromBa );
139+ }
140+
141+ private <T > void doCustomComparator (
142+ Env <T > env ,
143+ Comparator <T > comparator ,
144+ IntFunction <T > serializer ,
145+ ToIntFunction <T > deserializer ) {
146+ final Dbi <T > db = env .openDbi (DB_1 , comparator , true , MDB_CREATE );
147+ try (Txn <T > txn = env .txnWrite ()) {
148+ assertThat (db .put (txn , serializer .apply (2 ), serializer .apply (3 )), is (true ));
149+ assertThat (db .put (txn , serializer .apply (4 ), serializer .apply (6 )), is (true ));
150+ assertThat (db .put (txn , serializer .apply (6 ), serializer .apply (7 )), is (true ));
151+ assertThat (db .put (txn , serializer .apply (8 ), serializer .apply (7 )), is (true ));
126152 txn .commit ();
127153 }
128- try (Txn <ByteBuffer > txn = env .txnRead ();
129- CursorIterable <ByteBuffer > ci = db .iterate (txn , atMost (bb (4 )))) {
130- final Iterator <KeyVal <ByteBuffer >> iter = ci .iterator ();
131- assertThat (iter .next ().key (). getInt ( ), is (8 ));
132- assertThat (iter .next ().key (). getInt ( ), is (6 ));
133- assertThat (iter .next ().key (). getInt ( ), is (4 ));
154+ try (Txn <T > txn = env .txnRead ();
155+ CursorIterable <T > ci = db .iterate (txn , atMost (serializer . apply (4 )))) {
156+ final Iterator <KeyVal <T >> iter = ci .iterator ();
157+ assertThat (deserializer . applyAsInt ( iter .next ().key ()), is (8 ));
158+ assertThat (deserializer . applyAsInt ( iter .next ().key ()), is (6 ));
159+ assertThat (deserializer . applyAsInt ( iter .next ().key ()), is (4 ));
134160 }
135161 }
136162
@@ -143,9 +169,24 @@ public void dbOpenMaxDatabases() {
143169
144170 @ Test
145171 public void dbiWithComparatorThreadSafety () {
172+ doDbiWithComparatorThreadSafety (
173+ env , PROXY_OPTIMAL ::getComparator , TestUtils ::bb , ByteBuffer ::getInt );
174+ }
175+
176+ @ Test
177+ public void dbiWithComparatorThreadSafetyByteArray () {
178+ doDbiWithComparatorThreadSafety (
179+ envBa , PROXY_BA ::getComparator , TestUtils ::ba , TestUtils ::fromBa );
180+ }
181+
182+ public <T > void doDbiWithComparatorThreadSafety (
183+ Env <T > env ,
184+ Function <DbiFlags [], Comparator <T >> comparator ,
185+ IntFunction <T > serializer ,
186+ ToIntFunction <T > deserializer ) {
146187 final DbiFlags [] flags = new DbiFlags [] {MDB_CREATE , MDB_INTEGERKEY };
147- final Comparator <ByteBuffer > c = PROXY_OPTIMAL . getComparator (flags );
148- final Dbi <ByteBuffer > db = env .openDbi (DB_1 , c , true , flags );
188+ final Comparator <T > c = comparator . apply (flags );
189+ final Dbi <T > db = env .openDbi (DB_1 , c , true , flags );
149190
150191 final List <Integer > keys = range (0 , 1_000 ).boxed ().collect (toList ());
151192
@@ -155,25 +196,25 @@ public void dbiWithComparatorThreadSafety() {
155196 pool .submit (
156197 () -> {
157198 while (proceed .get ()) {
158- try (Txn <ByteBuffer > txn = env .txnRead ()) {
159- db .get (txn , bb (50 ));
199+ try (Txn <T > txn = env .txnRead ()) {
200+ db .get (txn , serializer . apply (50 ));
160201 }
161202 }
162203 });
163204
164205 for (final Integer key : keys ) {
165- try (Txn <ByteBuffer > txn = env .txnWrite ()) {
166- db .put (txn , bb (key ), bb (3 ));
206+ try (Txn <T > txn = env .txnWrite ()) {
207+ db .put (txn , serializer . apply (key ), serializer . apply (3 ));
167208 txn .commit ();
168209 }
169210 }
170211
171- try (Txn <ByteBuffer > txn = env .txnRead ();
172- CursorIterable <ByteBuffer > ci = db .iterate (txn )) {
173- final Iterator <KeyVal <ByteBuffer >> iter = ci .iterator ();
212+ try (Txn <T > txn = env .txnRead ();
213+ CursorIterable <T > ci = db .iterate (txn )) {
214+ final Iterator <KeyVal <T >> iter = ci .iterator ();
174215 final List <Integer > result = new ArrayList <>();
175216 while (iter .hasNext ()) {
176- result .add (iter .next ().key (). getInt ( ));
217+ result .add (deserializer . applyAsInt ( iter .next ().key ()));
177218 }
178219
179220 assertThat (result , Matchers .contains (keys .toArray (new Integer [0 ])));
@@ -339,7 +380,7 @@ public void putCommitGetByteArray() throws IOException {
339380 try (Txn <byte []> txn = envBa .txnWrite ()) {
340381 final byte [] found = db .get (txn , ba (5 ));
341382 assertNotNull (found );
342- assertThat (new UnsafeBuffer (txn .val ()). getInt ( 0 ), is (5 ));
383+ assertThat (fromBa (txn .val ()), is (5 ));
343384 }
344385 }
345386 }
0 commit comments