Skip to content

Commit 4182ad1

Browse files
committed
Dbi.openDbi(..) to support user-provided Txn (fixes #192)
1 parent 9f821c0 commit 4182ad1

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

src/main/java/org/lmdbjava/Env.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,11 @@ public boolean isReadOnly() {
265265
* @return a database that is ready to use
266266
*/
267267
public Dbi<T> openDbi(final String name, final DbiFlags... flags) {
268-
final byte[] nameBytes = name == null ? null : name.getBytes(UTF_8);
269-
return openDbi(nameBytes, flags);
268+
return openDbi(name, null, flags);
270269
}
271270

272271
/**
273-
* Convenience method that opens a {@link Dbi} with a UTF-8 database name
274-
* and custom comparator.
272+
* Convenience method that opens a {@link Dbi} with a UTF-8 database name.
275273
*
276274
* @param name name of the database (or null if no name is required)
277275
* @param comparator custom comparator callback (or null to use LMDB default)
@@ -285,22 +283,44 @@ public Dbi<T> openDbi(final String name, final Comparator<T> comparator,
285283
}
286284

287285
/**
288-
* Convenience method that opens a {@link Dbi} with a UTF-8 database name.
286+
* Convenience method that opens a {@link Dbi}.
289287
*
290288
* @param name name of the database (or null if no name is required)
291289
* @param flags to open the database with
292290
* @return a database that is ready to use
293291
*/
294292
public Dbi<T> openDbi(final byte[] name, final DbiFlags... flags) {
293+
return openDbi(name, null, flags);
294+
}
295+
296+
/**
297+
* Convenience method that opens a {@link Dbi} inside a private transaction.
298+
*
299+
* <p>
300+
* This method will automatically commit the private transaction before
301+
* returning. This ensures the <code>Dbi</code> is available in the
302+
* <code>Env</code>.
303+
*
304+
* @param name name of the database (or null if no name is required)
305+
* @param comparator custom comparator callback (or null to use LMDB default)
306+
* @param flags to open the database with
307+
* @return a database that is ready to use
308+
*/
309+
public Dbi<T> openDbi(final byte[] name, final Comparator<T> comparator,
310+
final DbiFlags... flags) {
295311
try (Txn<T> txn = readOnly ? txnRead() : txnWrite()) {
296-
final Dbi<T> dbi = new Dbi<>(this, txn, name, null, flags);
312+
final Dbi<T> dbi = openDbi(txn, name, comparator, flags);
297313
txn.commit(); // even RO Txns require a commit to retain Dbi in Env
298314
return dbi;
299315
}
300316
}
301317

302318
/**
303-
* Open the {@link Dbi}.
319+
* Open the {@link Dbi} using the passed {@link Txn}.
320+
*
321+
* <p>
322+
* The caller must commit the transaction after this method returns in order
323+
* to retain the <code>Dbi</code> in the <code>Env</code>.
304324
*
305325
* <p>
306326
* If a custom comparator is specified, this comparator is called from LMDB
@@ -316,18 +336,20 @@ public Dbi<T> openDbi(final byte[] name, final DbiFlags... flags) {
316336
* This method (and its overloaded convenience variants) must not be called
317337
* from concurrent threads.
318338
*
339+
* @param txn transaction to use (required; not closed)
319340
* @param name name of the database (or null if no name is required)
320341
* @param comparator custom comparator callback (or null to use LMDB default)
321342
* @param flags to open the database with
322343
* @return a database that is ready to use
323344
*/
324-
public Dbi<T> openDbi(final byte[] name, final Comparator<T> comparator,
345+
public Dbi<T> openDbi(final Txn<T> txn, final byte[] name,
346+
final Comparator<T> comparator,
325347
final DbiFlags... flags) {
326-
try (Txn<T> txn = readOnly ? txnRead() : txnWrite()) {
327-
final Dbi<T> dbi = new Dbi<>(this, txn, name, comparator, flags);
328-
txn.commit(); // even RO Txns require a commit to retain Dbi in Env
329-
return dbi;
348+
if (SHOULD_CHECK) {
349+
requireNonNull(txn);
350+
txn.checkReady();
330351
}
352+
return new Dbi<>(this, txn, name, comparator, flags);
331353
}
332354

333355
/**

0 commit comments

Comments
 (0)