Skip to content

Commit efbea4c

Browse files
committed
rename Transaction to Txn plus more constructors
1 parent d81a21d commit efbea4c

14 files changed

Lines changed: 124 additions & 97 deletions

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public class Cursor {
2020
private ByteBuffer buffer;
2121
private boolean closed;
2222
private final Pointer ptr;
23-
private final Transaction tx;
23+
private final Txn tx;
2424

25-
Cursor(Pointer ptr, Transaction tx) {
25+
Cursor(Pointer ptr, Txn tx) {
2626
this.ptr = ptr;
2727
this.tx = tx;
2828
}
@@ -145,7 +145,7 @@ public void put(ByteBuffer key, ByteBuffer val, PutFlags... op)
145145
*
146146
* @param tx transaction handle
147147
*/
148-
public void renew(Transaction tx) throws LmdbNativeException {
148+
public void renew(Txn tx) throws LmdbNativeException {
149149
if (!tx.isReadOnly()) {
150150
throw new IllegalArgumentException("cannot renew write transactions");
151151
}

src/main/java/org/lmdbjava/Database.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import static org.lmdbjava.Library.runtime;
1212
import static org.lmdbjava.MaskedFlag.mask;
1313
import static org.lmdbjava.ResultCodeMapper.checkRc;
14-
import static org.lmdbjava.TransactionFlags.MDB_RDONLY;
14+
import static org.lmdbjava.TxnFlags.MDB_RDONLY;
1515
import static org.lmdbjava.ValueBuffers.createVal;
1616
import static org.lmdbjava.ValueBuffers.wrap;
1717

@@ -33,14 +33,14 @@ public final class Database {
3333
* @param tx transaction to open and commit this database within (required)
3434
* @param name name of the database (or null if no name is required)
3535
* @param flags to open the database with
36-
* @throws AlreadyCommittedException if already committed
36+
* @throws TxnAlreadyCommittedException if already committed
3737
* @throws LmdbNativeException if a native C error occurred
3838
*/
39-
public Database(Transaction tx, String name, DatabaseFlags... flags)
40-
throws AlreadyCommittedException, LmdbNativeException {
39+
public Database(Txn tx, String name, DatabaseFlags... flags)
40+
throws TxnAlreadyCommittedException, LmdbNativeException {
4141
requireNonNull(tx);
4242
if (tx.isCommitted()) {
43-
throw new AlreadyCommittedException();
43+
throw new TxnAlreadyCommittedException();
4444
}
4545
this.env = tx.env;
4646
this.name = name;
@@ -51,21 +51,21 @@ public Database(Transaction tx, String name, DatabaseFlags... flags)
5151
}
5252

5353
/**
54-
* @see org.lmdbjava.Database#delete(Transaction, ByteBuffer, ByteBuffer)
54+
* @see org.lmdbjava.Database#delete(Txn, ByteBuffer, ByteBuffer)
5555
*/
5656
public void delete(ByteBuffer key) throws
57-
AlreadyCommittedException, LmdbNativeException, NotOpenException {
58-
try (Transaction tx = new Transaction(env, null)) {
57+
TxnAlreadyCommittedException, LmdbNativeException, NotOpenException {
58+
try (Txn tx = new Txn(env, null)) {
5959
delete(tx, key);
6060
tx.commit();
6161
}
6262
}
6363

6464
/**
65-
* @see org.lmdbjava.Database#delete(Transaction, ByteBuffer, ByteBuffer)
65+
* @see org.lmdbjava.Database#delete(Txn, ByteBuffer, ByteBuffer)
6666
*/
67-
public void delete(Transaction tx, ByteBuffer key) throws
68-
AlreadyCommittedException, LmdbNativeException {
67+
public void delete(Txn tx, ByteBuffer key) throws
68+
TxnAlreadyCommittedException, LmdbNativeException {
6969
delete(tx, key, null);
7070
}
7171

@@ -88,8 +88,8 @@ public void delete(Transaction tx, ByteBuffer key) throws
8888
* @param val The value to delete from the database
8989
* @return true if the key/value was deleted.
9090
*/
91-
public void delete(Transaction tx, ByteBuffer key, ByteBuffer val) throws
92-
AlreadyCommittedException, LmdbNativeException {
91+
public void delete(Txn tx, ByteBuffer key, ByteBuffer val) throws
92+
TxnAlreadyCommittedException, LmdbNativeException {
9393

9494
final MDB_val k = createVal(key);
9595
final MDB_val v = val == null ? null : createVal(key);
@@ -98,11 +98,11 @@ public void delete(Transaction tx, ByteBuffer key, ByteBuffer val) throws
9898
}
9999

100100
/**
101-
* @see org.lmdbjava.Database#get(Transaction, ByteBuffer)
101+
* @see org.lmdbjava.Database#get(Txn, ByteBuffer)
102102
*/
103103
public ByteBuffer get(ByteBuffer key) throws
104-
AlreadyCommittedException, LmdbNativeException, NotOpenException {
105-
try (Transaction tx = new Transaction(env, null, MDB_RDONLY)) {
104+
TxnAlreadyCommittedException, LmdbNativeException, NotOpenException {
105+
try (Txn tx = new Txn(env, MDB_RDONLY)) {
106106
return get(tx, key);
107107
}
108108
}
@@ -123,8 +123,8 @@ public ByteBuffer get(ByteBuffer key) throws
123123
* @param key The key to search for in the database
124124
* @return A value placeholder for the memory address to be wrapped if found by key.
125125
*/
126-
public ByteBuffer get(Transaction tx, ByteBuffer key) throws
127-
AlreadyCommittedException, LmdbNativeException {
126+
public ByteBuffer get(Txn tx, ByteBuffer key) throws
127+
TxnAlreadyCommittedException, LmdbNativeException {
128128
assert key.isDirect();
129129

130130
final MDB_val k = createVal(key);
@@ -168,18 +168,18 @@ public String getName() {
168168
* @param tx transaction handle
169169
* @return cursor handle
170170
*/
171-
public Cursor openCursor(Transaction tx) throws LmdbNativeException {
171+
public Cursor openCursor(Txn tx) throws LmdbNativeException {
172172
PointerByReference ptr = new PointerByReference();
173173
checkRc(lib.mdb_cursor_open(tx.ptr, dbi, ptr));
174174
return new Cursor(ptr.getValue(), tx);
175175
}
176176

177177
/**
178-
* @see org.lmdbjava.Database#put(Transaction, ByteBuffer, ByteBuffer, DatabaseFlags...)
178+
* @see org.lmdbjava.Database#put(Txn, ByteBuffer, ByteBuffer, DatabaseFlags...)
179179
*/
180180
public void put(ByteBuffer key, ByteBuffer val) throws
181-
AlreadyCommittedException, LmdbNativeException, NotOpenException {
182-
try (Transaction tx = new Transaction(env, null)) {
181+
TxnAlreadyCommittedException, LmdbNativeException, NotOpenException {
182+
try (Txn tx = new Txn(env, null)) {
183183
put(tx, key, val);
184184
tx.commit();
185185
}
@@ -202,8 +202,8 @@ public void put(ByteBuffer key, ByteBuffer val) throws
202202
*
203203
* @return the existing value if it was a dup insert attempt.
204204
*/
205-
public void put(Transaction tx, ByteBuffer key, ByteBuffer val, DatabaseFlags... flags) throws
206-
AlreadyCommittedException, LmdbNativeException {
205+
public void put(Txn tx, ByteBuffer key, ByteBuffer val, DatabaseFlags... flags) throws
206+
TxnAlreadyCommittedException, LmdbNativeException {
207207

208208
final MDB_val k = createVal(key);
209209
final MDB_val v = createVal(val);

src/main/java/org/lmdbjava/ResultCodeMapper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import static jnr.constants.ConstantSet.getConstantSet;
66
import static org.lmdbjava.BadDatabaseIdException.MDB_BAD_DBI;
77
import static org.lmdbjava.BadReaderLockTableSlotException.MDB_BAD_RSLOT;
8-
import static org.lmdbjava.BadTransactionException.MDB_BAD_TXN;
8+
import static org.lmdbjava.TxnBadException.MDB_BAD_TXN;
99
import static org.lmdbjava.BadValueSizeException.MDB_BAD_VALSIZE;
1010
import static org.lmdbjava.CorruptedException.MDB_CORRUPTED;
1111
import static org.lmdbjava.CursorFullException.MDB_CURSOR_FULL;
@@ -21,7 +21,7 @@
2121
import static org.lmdbjava.PanicException.MDB_PANIC;
2222
import static org.lmdbjava.ReadersFullException.MDB_READERS_FULL;
2323
import static org.lmdbjava.TlsFullException.MDB_TLS_FULL;
24-
import static org.lmdbjava.TransactionFullException.MDB_TXN_FULL;
24+
import static org.lmdbjava.TxnFullException.MDB_TXN_FULL;
2525
import static org.lmdbjava.VersionMismatchException.MDB_VERSION_MISMATCH;
2626

2727
/**
@@ -88,7 +88,7 @@ static LmdbNativeException rcException(final int rc) throws
8888
case MDB_BAD_RSLOT:
8989
return new BadReaderLockTableSlotException();
9090
case MDB_BAD_TXN:
91-
return new BadTransactionException();
91+
return new TxnBadException();
9292
case MDB_BAD_VALSIZE:
9393
return new BadValueSizeException();
9494
case MDB_CORRUPTED:
@@ -120,7 +120,7 @@ static LmdbNativeException rcException(final int rc) throws
120120
case MDB_TLS_FULL:
121121
return new TlsFullException();
122122
case MDB_TXN_FULL:
123-
return new TransactionFullException();
123+
return new TxnFullException();
124124
case MDB_VERSION_MISMATCH:
125125
return new VersionMismatchException();
126126
}

src/main/java/org/lmdbjava/Transaction.java renamed to src/main/java/org/lmdbjava/Txn.java

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,26 @@
33
import static java.util.Objects.requireNonNull;
44
import static jnr.ffi.Memory.allocateDirect;
55
import static jnr.ffi.NativeType.ADDRESS;
6+
67
import jnr.ffi.Pointer;
8+
79
import static org.lmdbjava.Library.lib;
810
import static org.lmdbjava.Library.runtime;
911
import static org.lmdbjava.MaskedFlag.isSet;
1012
import static org.lmdbjava.MaskedFlag.mask;
1113
import static org.lmdbjava.ResultCodeMapper.checkRc;
12-
import static org.lmdbjava.TransactionFlags.MDB_RDONLY;
14+
import static org.lmdbjava.TxnFlags.MDB_RDONLY;
1315

1416
/**
1517
* LMDB transaction.
1618
*/
17-
public final class Transaction implements AutoCloseable {
19+
public final class Txn implements AutoCloseable {
1820

1921
private boolean committed;
2022
private final boolean readOnly;
2123
private boolean reset = false;
2224
final Env env;
23-
final Transaction parent;
25+
final Txn parent;
2426
final Pointer ptr;
2527

2628
/**
@@ -40,9 +42,9 @@ public final class Transaction implements AutoCloseable {
4042
* @throws NotOpenException if the environment is not currently open
4143
* @throws LmdbNativeException if a native C error occurred
4244
*/
43-
public Transaction(final Env env, final Transaction parent,
44-
final TransactionFlags... flags) throws NotOpenException,
45-
LmdbNativeException {
45+
public Txn(final Env env, final Txn parent,
46+
final TxnFlags... flags) throws NotOpenException,
47+
LmdbNativeException {
4648
requireNonNull(env);
4749
if (!env.isOpen() || env.isClosed()) {
4850
throw new NotOpenException(Env.class.getSimpleName());
@@ -57,14 +59,39 @@ public Transaction(final Env env, final Transaction parent,
5759
ptr = txnPtr.getPointer(0);
5860
}
5961

62+
/**
63+
* Create a write transaction handle without a parent transaction.
64+
*
65+
* @param env the owning environment (required)
66+
* @throws NotOpenException if the environment is not currently open
67+
* @throws LmdbNativeException if a native C error occurred
68+
*/
69+
public Txn(final Env env)
70+
throws NotOpenException, LmdbNativeException {
71+
this(env, null, (TxnFlags[]) null);
72+
}
73+
74+
/**
75+
* Create a read or write transaction handle without a parent transaction.
76+
*
77+
* @param env the owning environment (required)
78+
* @param flags applicable flags (eg for a reusable, read-only transaction)
79+
* @throws NotOpenException if the environment is not currently open
80+
* @throws LmdbNativeException if a native C error occurred
81+
*/
82+
public Txn(final Env env, TxnFlags... flags)
83+
throws NotOpenException, LmdbNativeException {
84+
this(env, null, flags);
85+
}
86+
6087
/**
6188
* Aborts this transaction.
6289
*
63-
* @throws AlreadyCommittedException if already committed
90+
* @throws TxnAlreadyCommittedException if already committed
6491
*/
65-
public void abort() throws AlreadyCommittedException {
92+
public void abort() throws TxnAlreadyCommittedException {
6693
if (committed) {
67-
throw new AlreadyCommittedException();
94+
throw new TxnAlreadyCommittedException();
6895
}
6996
lib.mdb_txn_abort(ptr);
7097
this.committed = true;
@@ -85,12 +112,12 @@ public void close() {
85112
/**
86113
* Commits this transaction.
87114
*
88-
* @throws AlreadyCommittedException if already committed
115+
* @throws TxnAlreadyCommittedException if already committed
89116
* @throws LmdbNativeException if a native C error occurred
90117
*/
91-
public void commit() throws AlreadyCommittedException, LmdbNativeException {
118+
public void commit() throws TxnAlreadyCommittedException, LmdbNativeException {
92119
if (committed) {
93-
throw new AlreadyCommittedException();
120+
throw new TxnAlreadyCommittedException();
94121
}
95122
checkRc(lib.mdb_txn_commit(ptr));
96123
this.committed = true;
@@ -110,7 +137,7 @@ public long getId() {
110137
*
111138
* @return the parent transaction (may be null)
112139
*/
113-
public Transaction getParent() {
140+
public Txn getParent() {
114141
return parent;
115142
}
116143

@@ -144,13 +171,13 @@ public boolean isReset() {
144171
/**
145172
* Renews a read-only transaction previously released by {@link #reset()}.
146173
*
147-
* @throws TransactionHasNotBeenResetException if reset not called
174+
* @throws TxnHasNotBeenResetException if reset not called
148175
* @throws LmdbNativeException if a native C error occurred
149176
*/
150-
public void renew() throws TransactionHasNotBeenResetException,
151-
LmdbNativeException {
177+
public void renew() throws TxnHasNotBeenResetException,
178+
LmdbNativeException {
152179
if (!reset) {
153-
throw new TransactionHasNotBeenResetException();
180+
throw new TxnHasNotBeenResetException();
154181
}
155182
reset = false;
156183
checkRc(lib.mdb_txn_renew(ptr));
@@ -161,15 +188,15 @@ public void renew() throws TransactionHasNotBeenResetException,
161188
* can be reused upon calling {@link #renew()}.
162189
*
163190
* @throws ReadOnlyTransactionRequiredException if a read-write transaction
164-
* @throws TransactionAlreadyResetException if reset already performed
191+
* @throws TxnAlreadyResetException if reset already performed
165192
*/
166193
public void reset() throws ReadOnlyTransactionRequiredException,
167-
TransactionAlreadyResetException {
194+
TxnAlreadyResetException {
168195
if (!isReadOnly()) {
169196
throw new ReadOnlyTransactionRequiredException();
170197
}
171198
if (reset) {
172-
throw new TransactionAlreadyResetException();
199+
throw new TxnAlreadyResetException();
173200
}
174201
lib.mdb_txn_reset(ptr);
175202
reset = true;

src/main/java/org/lmdbjava/AlreadyCommittedException.java renamed to src/main/java/org/lmdbjava/TxnAlreadyCommittedException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
/**
44
* Transaction has already been committed.
55
*/
6-
public final class AlreadyCommittedException extends LmdbException {
6+
public final class TxnAlreadyCommittedException extends LmdbException {
77

88
private static final long serialVersionUID = 1L;
99

1010
/**
1111
* Creates a new instance.
1212
*/
13-
public AlreadyCommittedException() {
13+
public TxnAlreadyCommittedException() {
1414
super("Transaction has already been opened");
1515
}
1616
}

src/main/java/org/lmdbjava/TransactionAlreadyResetException.java renamed to src/main/java/org/lmdbjava/TxnAlreadyResetException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
/**
44
* The current transaction has already been reset.
55
*/
6-
public class TransactionAlreadyResetException extends LmdbException {
6+
public class TxnAlreadyResetException extends LmdbException {
77

88
private static final long serialVersionUID = 1L;
99

1010
/**
1111
* Creates a new instance.
1212
*
1313
*/
14-
public TransactionAlreadyResetException() {
14+
public TxnAlreadyResetException() {
1515
super("Transaction has already been reset");
1616
}
1717

src/main/java/org/lmdbjava/BadTransactionException.java renamed to src/main/java/org/lmdbjava/TxnBadException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
/**
44
* Transaction must abort, has a child, or is invalid.
55
*/
6-
public final class BadTransactionException extends LmdbNativeException {
6+
public final class TxnBadException extends LmdbNativeException {
77

88
private static final long serialVersionUID = 1L;
99
static final int MDB_BAD_TXN = -30_782;
1010

11-
BadTransactionException() {
11+
TxnBadException() {
1212
super(MDB_BAD_TXN, "Transaction must abort, has a child, or is invalid");
1313
}
1414
}

0 commit comments

Comments
 (0)