Skip to content

Commit 40f00bb

Browse files
committed
Txn package-visible check methods and test
1 parent ae0e988 commit 40f00bb

2 files changed

Lines changed: 60 additions & 3 deletions

File tree

src/main/java/org/lmdbjava/Txn.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*/
3333
public final class Txn implements AutoCloseable {
3434

35-
private boolean committed;
35+
private boolean committed = false;
3636
private final boolean readOnly;
3737
private boolean reset = false;
3838
final Env env;
@@ -214,6 +214,24 @@ public void reset() throws ReadOnlyRequiredException, ResetException {
214214
reset = true;
215215
}
216216

217+
void checkNotCommitted() throws CommittedException {
218+
if (committed) {
219+
throw new CommittedException();
220+
}
221+
}
222+
223+
void checkReadOnly() throws ReadOnlyRequiredException {
224+
if (!readOnly) {
225+
throw new ReadOnlyRequiredException();
226+
}
227+
}
228+
229+
void checkWritesAllowed() throws ReadWriteRequiredException {
230+
if (readOnly) {
231+
throw new ReadWriteRequiredException();
232+
}
233+
}
234+
217235
/**
218236
* Transaction must abort, has a child, or is invalid.
219237
*/
@@ -251,7 +269,7 @@ public static final class CommittedException extends LmdbException {
251269
* Creates a new instance.
252270
*/
253271
public CommittedException() {
254-
super("Transaction has already been opened");
272+
super("Transaction has already been committed");
255273
}
256274
}
257275

@@ -285,6 +303,21 @@ public ReadOnlyRequiredException() {
285303
}
286304
}
287305

306+
/**
307+
* The current transaction is not a read-write transaction.
308+
*/
309+
public static class ReadWriteRequiredException extends LmdbException {
310+
311+
private static final long serialVersionUID = 1L;
312+
313+
/**
314+
* Creates a new instance.
315+
*/
316+
public ReadWriteRequiredException() {
317+
super("Not a read-write transaction");
318+
}
319+
}
320+
288321
/**
289322
* The current transaction has already been reset.
290323
*/

src/test/java/org/lmdbjava/TxnTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.lmdbjava.Txn.CommittedException;
2121
import org.lmdbjava.Txn.NotResetException;
2222
import org.lmdbjava.Txn.ReadOnlyRequiredException;
23+
import org.lmdbjava.Txn.ReadWriteRequiredException;
2324
import org.lmdbjava.Txn.ResetException;
2425
import static org.lmdbjava.TxnFlags.MDB_RDONLY;
2526

@@ -41,8 +42,27 @@ public void before() throws Exception {
4142

4243
}
4344

45+
@Test(expected = CommittedException.class)
46+
public void testCheckNotCommitted() throws Exception {
47+
final Txn tx = new Txn(env, MDB_RDONLY);
48+
tx.commit();
49+
tx.checkNotCommitted();
50+
}
51+
52+
@Test(expected = ReadOnlyRequiredException.class)
53+
public void testCheckReadOnly() throws Exception {
54+
final Txn tx = new Txn(env);
55+
tx.checkReadOnly();
56+
}
57+
58+
@Test(expected = ReadWriteRequiredException.class)
59+
public void testCheckWritesAllowed() throws Exception {
60+
final Txn tx = new Txn(env, MDB_RDONLY);
61+
tx.checkWritesAllowed();
62+
}
63+
4464
@Test
45-
@Ignore
65+
@Ignore(value = "Travis CI failure; suspect older liblmdb version")
4666
public void testGetId() throws Exception {
4767
Txn tx = new Txn(env);
4868
Dbi db = new Dbi(tx, DB_1, MDB_CREATE);
@@ -112,6 +132,8 @@ public void txReadOnly() throws Exception {
112132
assertThat(tx.isCommitted(), is(false));
113133
assertThat(tx.isReadOnly(), is(true));
114134
assertThat(tx.isReset(), is(false));
135+
tx.checkNotCommitted();
136+
tx.checkReadOnly();
115137
tx.reset();
116138
assertThat(tx.isReset(), is(true));
117139
tx.renew();
@@ -126,6 +148,8 @@ public void txReadWrite() throws Exception {
126148
assertThat(tx.getParent(), is(nullValue()));
127149
assertThat(tx.isCommitted(), is(false));
128150
assertThat(tx.isReadOnly(), is(false));
151+
tx.checkNotCommitted();
152+
tx.checkWritesAllowed();
129153
tx.commit();
130154
assertThat(tx.isCommitted(), is(true));
131155
}

0 commit comments

Comments
 (0)