@@ -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