Skip to content

Commit de98542

Browse files
committed
Remove Env.open field and related NotOpenException
(no longer needed now the Builder guarantees only an open Env can be made available to callers)
1 parent fb52e15 commit de98542

5 files changed

Lines changed: 14 additions & 73 deletions

File tree

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

Lines changed: 7 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,9 @@ public static Env<ByteBuffer> open(
9393
}
9494

9595
private boolean closed = false;
96-
private boolean open = false;
9796
private final BufferProxy<T> proxy;
9897
final Pointer ptr;
9998

100-
/**
101-
* Creates a new environment handle.
102-
*/
10399
private Env(final BufferProxy<T> proxy) {
104100
requireNonNull(proxy);
105101
this.proxy = proxy;
@@ -119,9 +115,6 @@ public void close() {
119115
return;
120116
}
121117
closed = true;
122-
if (!open) {
123-
return;
124-
}
125118
LIB.mdb_env_close(ptr);
126119
}
127120

@@ -162,9 +155,6 @@ public EnvInfo info() {
162155
if (closed) {
163156
throw new AlreadyClosedException();
164157
}
165-
if (!open) {
166-
throw new NotOpenException();
167-
}
168158
final MDB_envinfo info = new MDB_envinfo(RUNTIME);
169159
checkRc(LIB.mdb_env_info(ptr, info));
170160

@@ -193,15 +183,6 @@ public boolean isClosed() {
193183
return closed;
194184
}
195185

196-
/**
197-
* Indicates whether this environment has been opened.
198-
*
199-
* @return true if opened
200-
*/
201-
public boolean isOpen() {
202-
return open;
203-
}
204-
205186
/**
206187
* Open the {@link Dbi}.
207188
*
@@ -228,9 +209,6 @@ public EnvStat stat() {
228209
if (closed) {
229210
throw new AlreadyClosedException();
230211
}
231-
if (!open) {
232-
throw new NotOpenException();
233-
}
234212
final MDB_stat stat = new MDB_stat(RUNTIME);
235213
checkRc(LIB.mdb_env_stat(ptr, stat));
236214
return new EnvStat(
@@ -253,9 +231,6 @@ public void sync(final boolean force) {
253231
if (closed) {
254232
throw new AlreadyClosedException();
255233
}
256-
if (!open) {
257-
throw new NotOpenException();
258-
}
259234
final int f = force ? 1 : 0;
260235
checkRc(LIB.mdb_env_sync(ptr, f));
261236
}
@@ -326,7 +301,8 @@ public AlreadyOpenException() {
326301
*/
327302
public static final class Builder<T> {
328303

329-
Env<T> env;
304+
private final Env<T> env;
305+
private boolean opened = false;
330306

331307
private Builder(Env<T> env) {
332308
this.env = env;
@@ -346,12 +322,12 @@ public Env<T> open(final File path, final int mode,
346322
if (env.closed) {
347323
throw new AlreadyClosedException();
348324
}
349-
if (env.open) {
325+
if (opened) {
350326
throw new AlreadyOpenException();
351327
}
352328
final int flagsMask = mask(flags);
353329
checkRc(LIB.mdb_env_open(env.ptr, path.getAbsolutePath(), flagsMask, mode));
354-
this.env.open = true;
330+
opened = true;
355331
return this.env;
356332
}
357333

@@ -373,12 +349,9 @@ public Env<T> open(final File path, final EnvFlags... flags) {
373349
* @return the builder
374350
*/
375351
public Builder<T> setMapSize(final long mapSize) {
376-
if (env.open) {
352+
if (opened) {
377353
throw new AlreadyOpenException();
378354
}
379-
if (env.closed) {
380-
throw new AlreadyClosedException();
381-
}
382355
checkRc(LIB.mdb_env_set_mapsize(env.ptr, mapSize));
383356
return this;
384357
}
@@ -401,12 +374,9 @@ public Builder<T> setMapSize(final int size, ByteUnit unit) {
401374
* @return the builder
402375
*/
403376
public Builder<T> setMaxDbs(final int dbs) {
404-
if (env.open) {
377+
if (opened) {
405378
throw new AlreadyOpenException();
406379
}
407-
if (env.closed) {
408-
throw new AlreadyClosedException();
409-
}
410380
checkRc(LIB.mdb_env_set_maxdbs(env.ptr, dbs));
411381
return this;
412382
}
@@ -418,12 +388,9 @@ public Builder<T> setMaxDbs(final int dbs) {
418388
* @return the builder
419389
*/
420390
public Builder<T> setMaxReaders(final int readers) {
421-
if (env.open) {
391+
if (opened) {
422392
throw new AlreadyOpenException();
423393
}
424-
if (env.closed) {
425-
throw new AlreadyClosedException();
426-
}
427394
checkRc(LIB.mdb_env_set_maxreaders(env.ptr, readers));
428395
return this;
429396
}
@@ -472,21 +439,6 @@ public static final class MapFullException extends LmdbNativeException {
472439
}
473440
}
474441

475-
/**
476-
* Object has is not open (eg never opened, or since closed).
477-
*/
478-
public static class NotOpenException extends LmdbException {
479-
480-
private static final long serialVersionUID = 1L;
481-
482-
/**
483-
* Creates a new instance.
484-
*/
485-
public NotOpenException() {
486-
super("Environment is not open");
487-
}
488-
}
489-
490442
/**
491443
* Environment maxreaders reached.
492444
*/

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import jnr.ffi.Pointer;
2222
import jnr.ffi.provider.MemoryManager;
2323
import static org.lmdbjava.BufferProxy.MDB_VAL_STRUCT_SIZE;
24-
import org.lmdbjava.Env.NotOpenException;
24+
import org.lmdbjava.Env.AlreadyClosedException;
2525
import static org.lmdbjava.Library.LIB;
2626
import static org.lmdbjava.Library.RUNTIME;
2727
import static org.lmdbjava.MaskedFlag.isSet;
@@ -52,12 +52,11 @@ public final class Txn<T> implements AutoCloseable {
5252
final Pointer ptrVal;
5353

5454
Txn(final Env<T> env, final Txn<T> parent, final BufferProxy<T> proxy,
55-
final TxnFlags... flags)
56-
throws NotOpenException, IncompatibleParent, LmdbNativeException {
55+
final TxnFlags... flags) {
5756
requireNonNull(env);
5857
requireNonNull(proxy);
59-
if (!env.isOpen() || env.isClosed()) {
60-
throw new NotOpenException();
58+
if (env.isClosed()) {
59+
throw new AlreadyClosedException();
6160
}
6261
this.env = env;
6362
this.proxy = proxy;

src/test/java/org/lmdbjava/DbiTest.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,12 @@
3636
import static org.lmdbjava.DbiFlags.MDB_CREATE;
3737
import static org.lmdbjava.DbiFlags.MDB_DUPSORT;
3838
import org.lmdbjava.Env.MapFullException;
39-
import org.lmdbjava.Env.NotOpenException;
4039
import static org.lmdbjava.Env.create;
4140
import static org.lmdbjava.EnvFlags.MDB_NOSUBDIR;
4241
import static org.lmdbjava.GetOp.MDB_SET_KEY;
4342
import static org.lmdbjava.PutFlags.MDB_NOOVERWRITE;
4443
import static org.lmdbjava.TestUtils.DB_1;
4544
import static org.lmdbjava.TestUtils.bb;
46-
import org.lmdbjava.Txn.CommittedException;
47-
import org.lmdbjava.Txn.ReadWriteRequiredException;
4845

4946
public class DbiTest {
5047

@@ -211,12 +208,7 @@ public void testParallelWritesStress() {
211208
.forEach(ignored -> {
212209
Random random = new Random();
213210
for (int i = 0; i < 15_000; i++) {
214-
try {
215-
db.put(bb(random.nextInt()), bb(random.nextInt()));
216-
} catch (CommittedException | LmdbNativeException | NotOpenException |
217-
ReadWriteRequiredException e) {
218-
throw new RuntimeException(e);
219-
}
211+
db.put(bb(random.nextInt()), bb(random.nextInt()));
220212
}
221213
});
222214
}

src/test/java/org/lmdbjava/EnvTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ public void copyRejectsNonEmptyDestination() throws IOException {
122122
public void createAsDirectory() throws IOException {
123123
final File path = tmp.newFolder();
124124
final Env<ByteBuffer> env = create().open(path);
125-
assertThat(env.isOpen(), is(true));
126125
assertThat(path.isDirectory(), is(true));
127126
env.sync(false);
128127
env.close();
@@ -139,7 +138,6 @@ public void createAsFile() throws IOException {
139138
.setMaxReaders(1)
140139
.open(path, MDB_NOSUBDIR)) {
141140
env.sync(true);
142-
assertThat(env.isOpen(), is(true));
143141
assertThat(path.isFile(), is(true));
144142
}
145143
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import org.junit.rules.TemporaryFolder;
3030
import static org.lmdbjava.ByteUnit.KIBIBYTES;
3131
import static org.lmdbjava.DbiFlags.MDB_CREATE;
32-
import org.lmdbjava.Env.NotOpenException;
32+
import org.lmdbjava.Env.AlreadyClosedException;
3333
import static org.lmdbjava.Env.create;
3434
import static org.lmdbjava.EnvFlags.MDB_NOSUBDIR;
3535
import static org.lmdbjava.TestUtils.DB_1;
@@ -124,7 +124,7 @@ public void txCannotCommitTwice() {
124124
txn.commit(); // error
125125
}
126126

127-
@Test(expected = NotOpenException.class)
127+
@Test(expected = AlreadyClosedException.class)
128128
@SuppressWarnings("ResultOfObjectAllocationIgnored")
129129
public void txConstructionDeniedIfEnvClosed() {
130130
env.close();

0 commit comments

Comments
 (0)