Skip to content

Commit 7cf8f02

Browse files
committed
Merge branch 'lfoppiano-defaultMaxReaders'
2 parents 6cec06c + 5ccb6c5 commit 7cf8f02

File tree

6 files changed

+140
-78
lines changed

6 files changed

+140
-78
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,9 +429,10 @@ public AlreadyOpenException() {
429429
*/
430430
public static final class Builder<T> {
431431

432+
static final int MAX_READERS_DEFAULT = 126;
432433
private long mapSize = 1_024 * 1_024;
433434
private int maxDbs = 1;
434-
private int maxReaders = 1;
435+
private int maxReaders = MAX_READERS_DEFAULT;
435436
private boolean opened;
436437
private final BufferProxy<T> proxy;
437438

src/test/java/org/lmdbjava/ByteBufferProxyTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ public final class ByteBufferProxyTest {
6565
@Test(expected = BufferMustBeDirectException.class)
6666
public void buffersMustBeDirect() throws IOException {
6767
final File path = tmp.newFolder();
68-
try (Env<ByteBuffer> env = create().open(path)) {
68+
try (Env<ByteBuffer> env = create()
69+
.setMaxReaders(1)
70+
.open(path)) {
6971
final Dbi<ByteBuffer> db = env.openDbi(DB_1, MDB_CREATE);
7072
final ByteBuffer key = allocate(100);
7173
key.putInt(1).flip();

src/test/java/org/lmdbjava/CursorIteratorTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package org.lmdbjava;
2222

2323
import com.google.common.primitives.UnsignedBytes;
24+
import static com.jakewharton.byteunits.BinaryByteUnit.KIBIBYTES;
2425
import java.io.File;
2526
import java.io.IOException;
2627
import java.nio.ByteBuffer;
@@ -43,7 +44,7 @@
4344
import static org.lmdbjava.CursorIterator.IteratorType.FORWARD;
4445
import org.lmdbjava.CursorIterator.KeyVal;
4546
import static org.lmdbjava.DbiFlags.MDB_CREATE;
46-
import static org.lmdbjava.Env.open;
47+
import static org.lmdbjava.Env.create;
4748
import static org.lmdbjava.EnvFlags.MDB_NOSUBDIR;
4849
import static org.lmdbjava.KeyRange.all;
4950
import static org.lmdbjava.KeyRange.allBackward;
@@ -65,6 +66,7 @@
6566
import static org.lmdbjava.KeyRange.openClosedBackward;
6667
import static org.lmdbjava.PutFlags.MDB_NOOVERWRITE;
6768
import static org.lmdbjava.TestUtils.DB_1;
69+
import static org.lmdbjava.TestUtils.POSIX_MODE;
6870
import static org.lmdbjava.TestUtils.bb;
6971

7072
/**
@@ -147,7 +149,11 @@ public void backwardSeekDeprecated() {
147149
@Before
148150
public void before() throws IOException {
149151
final File path = tmp.newFile();
150-
env = open(path, 10, MDB_NOSUBDIR);
152+
env = create()
153+
.setMapSize(KIBIBYTES.toBytes(100))
154+
.setMaxReaders(1)
155+
.setMaxDbs(1)
156+
.open(path, POSIX_MODE, MDB_NOSUBDIR);
151157
db = env.openDbi(DB_1, MDB_CREATE);
152158
list = new LinkedList<>();
153159
list.addAll(asList(2, 3, 4, 5, 6, 7, 8, 9));

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

Lines changed: 69 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.lmdbjava.Env.AlreadyClosedException;
3939
import org.lmdbjava.Env.AlreadyOpenException;
4040
import org.lmdbjava.Env.Builder;
41+
import static org.lmdbjava.Env.Builder.MAX_READERS_DEFAULT;
4142
import org.lmdbjava.Env.InvalidCopyDestination;
4243
import org.lmdbjava.Env.MapFullException;
4344
import static org.lmdbjava.Env.create;
@@ -59,9 +60,10 @@ public final class EnvTest {
5960
@Test
6061
public void byteUnit() throws IOException {
6162
final File path = tmp.newFile();
62-
try (Env<ByteBuffer> env
63-
= create().setMapSize(MEBIBYTES.toBytes(1)).open(
64-
path, MDB_NOSUBDIR)) {
63+
try (Env<ByteBuffer> env = create()
64+
.setMaxReaders(1)
65+
.setMapSize(MEBIBYTES.toBytes(1))
66+
.open(path, MDB_NOSUBDIR)) {
6567
final EnvInfo info = env.info();
6668
assertThat(info.mapSize, is(MEBIBYTES.toBytes(1)));
6769
}
@@ -70,31 +72,38 @@ public void byteUnit() throws IOException {
7072
@Test(expected = AlreadyOpenException.class)
7173
public void cannotChangeMapSizeAfterOpen() throws IOException {
7274
final File path = tmp.newFile();
73-
final Builder<ByteBuffer> builder = create();
74-
builder.open(path, MDB_NOSUBDIR);
75-
builder.setMapSize(1);
75+
final Builder<ByteBuffer> builder = create()
76+
.setMaxReaders(1);
77+
try (Env<ByteBuffer> env = builder.open(path, MDB_NOSUBDIR)) {
78+
builder.setMapSize(1);
79+
}
7680
}
7781

7882
@Test(expected = AlreadyOpenException.class)
7983
public void cannotChangeMaxDbsAfterOpen() throws IOException {
8084
final File path = tmp.newFile();
81-
final Builder<ByteBuffer> builder = create();
82-
builder.open(path, MDB_NOSUBDIR);
83-
builder.setMaxDbs(1);
85+
final Builder<ByteBuffer> builder = create()
86+
.setMaxReaders(1);
87+
try (Env<ByteBuffer> env = builder.open(path, MDB_NOSUBDIR)) {
88+
builder.setMaxDbs(1);
89+
}
8490
}
8591

8692
@Test(expected = AlreadyOpenException.class)
8793
public void cannotChangeMaxReadersAfterOpen() throws IOException {
8894
final File path = tmp.newFile();
89-
final Builder<ByteBuffer> builder = create();
90-
builder.open(path, MDB_NOSUBDIR);
91-
builder.setMaxReaders(1);
95+
final Builder<ByteBuffer> builder = create()
96+
.setMaxReaders(1);
97+
try (Env<ByteBuffer> env = builder.open(path, MDB_NOSUBDIR)) {
98+
builder.setMaxReaders(1);
99+
}
92100
}
93101

94102
@Test(expected = AlreadyClosedException.class)
95103
public void cannotInfoOnceClosed() throws IOException {
96104
final File path = tmp.newFile();
97105
final Env<ByteBuffer> env = create()
106+
.setMaxReaders(1)
98107
.open(path, MDB_NOSUBDIR);
99108
env.close();
100109
env.info();
@@ -103,14 +112,17 @@ public void cannotInfoOnceClosed() throws IOException {
103112
@Test(expected = AlreadyOpenException.class)
104113
public void cannotOpenTwice() throws IOException {
105114
final File path = tmp.newFile();
106-
final Builder<ByteBuffer> builder = create();
107-
builder.open(path, MDB_NOSUBDIR);
115+
final Builder<ByteBuffer> builder = create()
116+
.setMaxReaders(1);
117+
118+
builder.open(path, MDB_NOSUBDIR).close();
108119
builder.open(path, MDB_NOSUBDIR);
109120
}
110121

111122
@Test(expected = IllegalArgumentException.class)
112123
public void cannotOverflowMapSize() {
113-
final Builder<ByteBuffer> builder = create();
124+
final Builder<ByteBuffer> builder = create()
125+
.setMaxReaders(1);
114126
final int mb = 1_024 * 1_024;
115127
final int size = mb * 2_048; // as per issue 18
116128
builder.setMapSize(size);
@@ -120,6 +132,7 @@ public void cannotOverflowMapSize() {
120132
public void cannotStatOnceClosed() throws IOException {
121133
final File path = tmp.newFile();
122134
final Env<ByteBuffer> env = create()
135+
.setMaxReaders(1)
123136
.open(path, MDB_NOSUBDIR);
124137
env.close();
125138
env.stat();
@@ -129,6 +142,7 @@ public void cannotStatOnceClosed() throws IOException {
129142
public void cannotSyncOnceClosed() throws IOException {
130143
final File path = tmp.newFile();
131144
final Env<ByteBuffer> env = create()
145+
.setMaxReaders(1)
132146
.open(path, MDB_NOSUBDIR);
133147
env.close();
134148
env.sync(false);
@@ -141,7 +155,9 @@ public void copy() throws IOException {
141155
assertThat(dest.isDirectory(), is(true));
142156
assertThat(dest.list().length, is(0));
143157
final File src = tmp.newFolder();
144-
try (Env<ByteBuffer> env = create().open(src)) {
158+
try (Env<ByteBuffer> env = create()
159+
.setMaxReaders(1)
160+
.open(src)) {
145161
env.copy(dest, MDB_CP_COMPACT);
146162
assertThat(dest.list().length, is(1));
147163
}
@@ -151,7 +167,9 @@ public void copy() throws IOException {
151167
public void copyRejectsFileDestination() throws IOException {
152168
final File dest = tmp.newFile();
153169
final File src = tmp.newFolder();
154-
try (Env<ByteBuffer> env = create().open(src)) {
170+
try (Env<ByteBuffer> env = create()
171+
.setMaxReaders(1)
172+
.open(src)) {
155173
env.copy(dest, MDB_CP_COMPACT);
156174
}
157175
}
@@ -161,7 +179,9 @@ public void copyRejectsMissingDestination() throws IOException {
161179
final File dest = tmp.newFolder();
162180
assertThat(dest.delete(), is(true));
163181
final File src = tmp.newFolder();
164-
try (Env<ByteBuffer> env = create().open(src)) {
182+
try (Env<ByteBuffer> env = create()
183+
.setMaxReaders(1)
184+
.open(src)) {
165185
env.copy(dest, MDB_CP_COMPACT);
166186
}
167187
}
@@ -172,15 +192,19 @@ public void copyRejectsNonEmptyDestination() throws IOException {
172192
final File subDir = new File(dest, "hello");
173193
assertThat(subDir.mkdir(), is(true));
174194
final File src = tmp.newFolder();
175-
try (Env<ByteBuffer> env = create().open(src)) {
195+
try (Env<ByteBuffer> env = create()
196+
.setMaxReaders(1)
197+
.open(src)) {
176198
env.copy(dest, MDB_CP_COMPACT);
177199
}
178200
}
179201

180202
@Test
181203
public void createAsDirectory() throws IOException {
182204
final File path = tmp.newFolder();
183-
final Env<ByteBuffer> env = create().open(path);
205+
final Env<ByteBuffer> env = create()
206+
.setMaxReaders(1)
207+
.open(path);
184208
assertThat(path.isDirectory(), is(true));
185209
env.sync(false);
186210
env.close();
@@ -204,10 +228,12 @@ public void createAsFile() throws IOException {
204228
@Test(expected = BadReaderLockException.class)
205229
public void detectTransactionThreadViolation() throws IOException {
206230
final File path = tmp.newFile();
207-
final Env<ByteBuffer> env = create()
208-
.open(path, MDB_NOSUBDIR);
209-
env.txnRead();
210-
env.txnRead();
231+
try (Env<ByteBuffer> env = create()
232+
.setMaxReaders(1)
233+
.open(path, MDB_NOSUBDIR)) {
234+
env.txnRead();
235+
env.txnRead();
236+
}
211237
}
212238

213239
@Test
@@ -237,7 +263,9 @@ public void mapFull() throws IOException {
237263
final ByteBuffer key = allocateDirect(500);
238264
final ByteBuffer val = allocateDirect(1_024);
239265
final Random rnd = new Random();
240-
try (Env<ByteBuffer> env = create().setMapSize(MEBIBYTES.toBytes(8))
266+
try (Env<ByteBuffer> env = create()
267+
.setMaxReaders(1)
268+
.setMapSize(MEBIBYTES.toBytes(8))
241269
.setMaxDbs(1).open(path)) {
242270
final Dbi<ByteBuffer> db = env.openDbi(DB_1, MDB_CREATE);
243271
for (;;) {
@@ -253,11 +281,15 @@ public void mapFull() throws IOException {
253281
@Test
254282
public void readOnlySupported() throws IOException {
255283
final File path = tmp.newFolder();
256-
try (Env<ByteBuffer> rwEnv = create().open(path)) {
284+
try (Env<ByteBuffer> rwEnv = create()
285+
.setMaxReaders(1)
286+
.open(path)) {
257287
final Dbi<ByteBuffer> rwDb = rwEnv.openDbi(DB_1, MDB_CREATE);
258288
rwDb.put(bb(1), bb(42));
259289
}
260-
try (Env<ByteBuffer> roEnv = create().open(path, MDB_RDONLY_ENV)) {
290+
try (Env<ByteBuffer> roEnv = create()
291+
.setMaxReaders(1)
292+
.open(path, MDB_RDONLY_ENV)) {
261293
final Dbi<ByteBuffer> roDb = roEnv.openDbi(DB_1);
262294
try (Txn<ByteBuffer> roTxn = roEnv.txnRead()) {
263295
assertThat(roDb.get(roTxn, bb(1)), notNullValue());
@@ -272,8 +304,11 @@ public void setMapSize() throws IOException {
272304
final ByteBuffer key = allocateDirect(500);
273305
final ByteBuffer val = allocateDirect(1_024);
274306
final Random rnd = new Random();
275-
try (Env<ByteBuffer> env = create().setMapSize(50_000)
276-
.setMaxDbs(1).open(path)) {
307+
try (Env<ByteBuffer> env = create()
308+
.setMaxReaders(1)
309+
.setMapSize(50_000)
310+
.setMaxDbs(1)
311+
.open(path)) {
277312
final Dbi<ByteBuffer> db = env.openDbi(DB_1, MDB_CREATE);
278313

279314
db.put(bb(1), bb(42));
@@ -316,7 +351,9 @@ public void setMapSize() throws IOException {
316351
@Test
317352
public void stats() throws IOException {
318353
final File path = tmp.newFile();
319-
try (Env<ByteBuffer> env = create().open(path, MDB_NOSUBDIR)) {
354+
try (Env<ByteBuffer> env = create()
355+
.setMaxReaders(1)
356+
.open(path, MDB_NOSUBDIR)) {
320357
final Stat stat = env.stat();
321358
assertThat(stat, is(notNullValue()));
322359
assertThat(stat.branchPages, is(0L));
@@ -333,6 +370,8 @@ public void stats() throws IOException {
333370
public void testDefaultOpen() throws IOException {
334371
final File path = tmp.newFolder();
335372
try (Env<ByteBuffer> env = open(path, 10)) {
373+
final EnvInfo info = env.info();
374+
assertThat(info.maxReaders, is(MAX_READERS_DEFAULT));
336375
final Dbi<ByteBuffer> db = env.openDbi("test", MDB_CREATE);
337376
db.put(allocateDirect(1), allocateDirect(1));
338377
}

0 commit comments

Comments
 (0)