Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main/java/org/lmdbjava/Env.java
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,15 @@ Pointer pointer() {
return ptr;
}

/**
* Set the size of the data memory map.
*
* @param mapSize the new size, in bytes
*/
public void setMapSize(final long mapSize) {
checkRc(LIB.mdb_env_set_mapsize(ptr, mapSize));
}

/**
* Object has already been closed and the operation is therefore prohibited.
*/
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/org/lmdbjava/EnvTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,54 @@ public void mapFull() throws IOException {
}
}

@Test
public void setMapSize() throws IOException {
final File path = tmp.newFolder();
final byte[] k = new byte[500];
final ByteBuffer key = allocateDirect(500);
final ByteBuffer val = allocateDirect(1_024);
final Random rnd = new Random();
try (Env<ByteBuffer> env = create().setMapSize(50_000)
.setMaxDbs(1).open(path)) {
final Dbi<ByteBuffer> db = env.openDbi(DB_1, MDB_CREATE);

db.put(bb(1), bb(42));
boolean mapFullExThrown = false;
try {
for (int i = 0; i < 30; i++) {
rnd.nextBytes(k);
key.clear();
key.put(k).flip();
val.clear();
db.put(key, val);
}
} catch (final MapFullException mfE) {
mapFullExThrown = true;
}
assertThat(mapFullExThrown, is(true));

env.setMapSize(500_000);

try (Txn<ByteBuffer> roTxn = env.txnRead()) {
assertThat(db.get(roTxn, bb(1)), is(bb(42)));
}

mapFullExThrown = false;
try {
for (int i = 0; i < 30; i++) {
rnd.nextBytes(k);
key.clear();
key.put(k).flip();
val.clear();
db.put(key, val);
}
} catch (final MapFullException mfE) {
mapFullExThrown = true;
}
assertThat(mapFullExThrown, is(false));
}
}

@Test
public void readOnlySupported() throws IOException {
final File path = tmp.newFolder();
Expand Down