Skip to content

Commit

Permalink
Allow zero copy put by reserving space directly in LMDB, #51
Browse files Browse the repository at this point in the history
  • Loading branch information
krisskross committed Dec 15, 2015
1 parent d1b74a5 commit 0b628e3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
20 changes: 20 additions & 0 deletions lmdbjni/src/main/java/org/fusesource/lmdbjni/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.fusesource.hawtjni.runtime.Callback;
import org.fusesource.lmdbjni.EntryIterator.IteratorType;

import java.nio.ByteBuffer;
import java.util.Comparator;

import static org.fusesource.lmdbjni.JNI.*;
Expand Down Expand Up @@ -345,6 +346,25 @@ public int put(Transaction tx, DirectBuffer key, DirectBuffer value, int flags)
return rc;
}

/**
* Just reserve space for data in the database, don't copy it. Return a pointer to the reserved space.
*/
public DirectBuffer reserve(Transaction tx, DirectBuffer key, int size) {
checkArgNotNull(key, "key");
long address = tx.getBufferAddress();
Unsafe.putLong(address, 0, key.capacity());
Unsafe.putLong(address, 1, key.addressOffset());
Unsafe.putLong(address, 2, size);

int rc = mdb_put_address(tx.pointer(), pointer(), address, address + 2 * Unsafe.ADDRESS_SIZE, Constants.RESERVE);
checkErrorCode(rc);
int valSize = (int) Unsafe.getLong(address, 2);
long valAddress = Unsafe.getAddress(address, 3);
DirectBuffer empty = new DirectBuffer(0, 0);
empty.wrap(valAddress, valSize);
return empty;
}

/**
* @see org.fusesource.lmdbjni.Database#put(Transaction, byte[], byte[], int)
*/
Expand Down
23 changes: 22 additions & 1 deletion lmdbjni/src/test/java/org/fusesource/lmdbjni/ZeroCopyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.fusesource.lmdbjni.Constants.FIRST;
import static org.fusesource.lmdbjni.Constants.NEXT;
import static org.fusesource.lmdbjni.LMDBException.NOTFOUND;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertThat;

public class ZeroCopyTest {
Expand Down Expand Up @@ -159,5 +161,24 @@ public void testIteration() {
assertThat(result.size(), is(2));
assertThat(result.get(0), is(18L));
assertThat(result.get(1), is(20L));
}
}

@Test
public void testReserve() {
byte[] key = new byte[] {1,2,3};
byte[] val = new byte[] {3,2,1};

try (Transaction tx = env.createWriteTransaction()) {
DirectBuffer keyBuf = new DirectBuffer(ByteBuffer.allocateDirect(key.length));
keyBuf.putBytes(0, key);
DirectBuffer valBuf = db.reserve(tx, keyBuf, val.length);
valBuf.putBytes(0, val);
tx.commit();
}

try (Transaction tx = env.createReadTransaction()) {
byte[] result = db.get(tx, key);
assertArrayEquals(result, val);
}
}
}

0 comments on commit 0b628e3

Please sign in to comment.