Skip to content

Commit 6f0f6ee

Browse files
committed
expose maximum key size
511 bytes should not be a magic constant.
1 parent ea47b79 commit 6f0f6ee

4 files changed

Lines changed: 28 additions & 12 deletions

File tree

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,15 @@ public final class Env<T> implements AutoCloseable {
6262
private final BufferProxy<T> proxy;
6363
private final Pointer ptr;
6464
private final boolean readOnly;
65+
private final int maxKeySize;
6566

6667
private Env(final BufferProxy<T> proxy, final Pointer ptr,
6768
final boolean readOnly) {
6869
this.proxy = proxy;
6970
this.readOnly = readOnly;
7071
this.ptr = ptr;
72+
// cache max key size to avoid further JNI calls
73+
this.maxKeySize = LIB.mdb_env_get_maxkeysize(ptr);
7174
}
7275

7376
/**
@@ -152,6 +155,15 @@ public void copy(final File path, final CopyFlags... flags) {
152155
checkRc(LIB.mdb_env_copy2(ptr, path.getAbsolutePath(), flagsMask));
153156
}
154157

158+
/**
159+
* Get the maximum size of keys and MDB_DUPSORT data we can write.
160+
*
161+
* @return the maximum size of keys.
162+
*/
163+
public int getMaxKeySize() {
164+
return maxKeySize;
165+
}
166+
155167
/**
156168
* Return information about this environment.
157169
*

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ public void unsafeIsDefault() {
121121
}
122122

123123
private void checkInOut(final BufferProxy<ByteBuffer> v) {
124-
final ByteBuffer b = allocateDirect(511);
124+
// allocate a buffer larger than max key size
125+
final ByteBuffer b = allocateDirect(1000);
125126
b.putInt(1);
126127
b.putInt(2);
127128
b.putInt(3);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ public void info() throws IOException {
202202
assertThat(info.mapSize, is(123_456L));
203203
assertThat(info.maxReaders, is(4));
204204
assertThat(info.numReaders, is(0));
205+
206+
assertThat(env.getMaxKeySize(), is(511));
205207
}
206208

207209
@Test

src/test/java/org/lmdbjava/TutorialTest.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ public void tutorial1() throws IOException {
9898
final Dbi<ByteBuffer> db = env.openDbi(DB_NAME, MDB_CREATE);
9999

100100
// We want to store some data, so we will need a direct ByteBuffer.
101-
// Note that LMDB keys cannot exceed 511 bytes. Values can be larger.
102-
final ByteBuffer key = allocateDirect(511);
101+
// Note that LMDB keys cannot exceed maxKeySize bytes (511 bytes by default).
102+
// Values can be larger.
103+
final ByteBuffer key = allocateDirect(env.getMaxKeySize());
103104
final ByteBuffer val = allocateDirect(700);
104105
key.put("greeting".getBytes(UTF_8)).flip();
105106
val.put("Hello world".getBytes(UTF_8)).flip();
@@ -149,7 +150,7 @@ public void tutorial2() throws IOException {
149150
final Env<ByteBuffer> env = open(path, 10);
150151

151152
final Dbi<ByteBuffer> db = env.openDbi(DB_NAME, MDB_CREATE);
152-
final ByteBuffer key = allocateDirect(511);
153+
final ByteBuffer key = allocateDirect(env.getMaxKeySize());
153154
final ByteBuffer val = allocateDirect(700);
154155

155156
// Let's write and commit "key1" via a Txn. A Txn can include multiple Dbis.
@@ -217,7 +218,7 @@ public void tutorial3() throws IOException {
217218
final File path = tmp.newFolder();
218219
final Env<ByteBuffer> env = open(path, 10);
219220
final Dbi<ByteBuffer> db = env.openDbi(DB_NAME, MDB_CREATE);
220-
final ByteBuffer key = allocateDirect(511);
221+
final ByteBuffer key = allocateDirect(env.getMaxKeySize());
221222
final ByteBuffer val = allocateDirect(700);
222223

223224
try (final Txn<ByteBuffer> txn = env.txnWrite()) {
@@ -294,7 +295,7 @@ public void tutorial4() throws IOException {
294295
final Dbi<ByteBuffer> db = env.openDbi(DB_NAME, MDB_CREATE);
295296

296297
try (final Txn<ByteBuffer> txn = env.txnWrite()) {
297-
final ByteBuffer key = allocateDirect(511);
298+
final ByteBuffer key = allocateDirect(env.getMaxKeySize());
298299
final ByteBuffer val = allocateDirect(700);
299300

300301
// Insert some data
@@ -350,9 +351,9 @@ public void tutorial5() throws IOException {
350351
// There are other flags available if we're storing integers etc.
351352
final Dbi<ByteBuffer> db = env.openDbi(DB_NAME, MDB_CREATE, MDB_DUPSORT);
352353

353-
// Duplicate support requires both keys and values to be <= 511 bytes.
354-
final ByteBuffer key = allocateDirect(511);
355-
final ByteBuffer val = allocateDirect(511);
354+
// Duplicate support requires both keys and values to be <= max key size.
355+
final ByteBuffer key = allocateDirect(env.getMaxKeySize());
356+
final ByteBuffer val = allocateDirect(env.getMaxKeySize());
356357

357358
try (final Txn<ByteBuffer> txn = env.txnWrite()) {
358359
final Cursor<ByteBuffer> c = db.openCursor(txn);
@@ -406,7 +407,7 @@ public void tutorial6() throws IOException {
406407

407408
final Dbi<DirectBuffer> db = env.openDbi(DB_NAME, MDB_CREATE);
408409

409-
final MutableDirectBuffer key = new UnsafeBuffer(allocateDirect(511));
410+
final MutableDirectBuffer key = new UnsafeBuffer(allocateDirect(env.getMaxKeySize()));
410411
final MutableDirectBuffer val = new UnsafeBuffer(allocateDirect(700));
411412

412413
try (final Txn<DirectBuffer> txn = env.txnWrite()) {
@@ -421,10 +422,10 @@ public void tutorial6() throws IOException {
421422
c.put(key, val);
422423

423424
c.seek(MDB_FIRST);
424-
assertThat(txn.key().getStringWithoutLengthUtf8(0, 511), startsWith("ggg"));
425+
assertThat(txn.key().getStringWithoutLengthUtf8(0, env.getMaxKeySize()), startsWith("ggg"));
425426

426427
c.seek(MDB_LAST);
427-
assertThat(txn.key().getStringWithoutLengthUtf8(0, 511), startsWith("yyy"));
428+
assertThat(txn.key().getStringWithoutLengthUtf8(0, env.getMaxKeySize()), startsWith("yyy"));
428429

429430
c.close();
430431
txn.commit();

0 commit comments

Comments
 (0)