Skip to content

Commit 226c26c

Browse files
committed
Remove byte[] conveniences (issue lmdbjava#3)
1 parent cc5670e commit 226c26c

3 files changed

Lines changed: 14 additions & 63 deletions

File tree

src/main/java/org/lmdbjava/ByteBufferProxy.java

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.nio.ByteBuffer;
2727
import static java.nio.ByteBuffer.allocateDirect;
2828
import java.util.ArrayDeque;
29-
import static java.util.Objects.requireNonNull;
3029
import jnr.ffi.Pointer;
3130
import static org.lmdbjava.Env.SHOULD_CHECK;
3231
import static org.lmdbjava.UnsafeAccess.UNSAFE;
@@ -65,36 +64,6 @@ public final class ByteBufferProxy {
6564
PROXY_OPTIMAL = getProxyOptimal();
6665
}
6766

68-
/**
69-
* Convenience method to copy the passed {@link ByteBuffer} into a byte array.
70-
* This method is not optimized and use is discouraged (use a proper
71-
* {@link BufferProxy} instead).
72-
*
73-
* @param buffer to copy into a byte array (not null)
74-
* @return a byte array of the same length as the passed buffer's capacity
75-
*/
76-
public static byte[] array(final ByteBuffer buffer) {
77-
requireNonNull(buffer, "A non-null input ByteArray is required");
78-
final byte[] dest = new byte[buffer.capacity()];
79-
buffer.get(dest);
80-
return dest;
81-
}
82-
83-
/**
84-
* Convenience method to create a direct {@link ByteBuffer} and copy the
85-
* passed byte array into it. This method is not optimized and use is
86-
* discouraged (use a proper {@link BufferProxy} instead).
87-
*
88-
* @param src to copy into a byte buffer (not null)
89-
* @return a byte buffer that contains the passed bytes
90-
*/
91-
public static ByteBuffer buffer(final byte[] src) {
92-
requireNonNull(src, "A non-null input byte[] is required");
93-
final ByteBuffer buff = allocateDirect(src.length);
94-
buff.put(src);
95-
return buff;
96-
}
97-
9867
private static BufferProxy<ByteBuffer> getProxyOptimal() {
9968
try {
10069
return new UnsafeProxy();

src/test/java/org/lmdbjava/DbiTest.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import static java.lang.System.getProperty;
2727
import java.nio.ByteBuffer;
2828
import static java.nio.ByteBuffer.allocateDirect;
29-
import static java.nio.charset.StandardCharsets.UTF_8;
3029
import static java.util.Collections.nCopies;
3130
import java.util.Random;
3231
import static org.hamcrest.CoreMatchers.is;
@@ -40,8 +39,6 @@
4039
import org.junit.Rule;
4140
import org.junit.Test;
4241
import org.junit.rules.TemporaryFolder;
43-
import static org.lmdbjava.ByteBufferProxy.array;
44-
import static org.lmdbjava.ByteBufferProxy.buffer;
4542
import static org.lmdbjava.ByteUnit.MEBIBYTES;
4643
import org.lmdbjava.Dbi.DbFullException;
4744
import org.lmdbjava.Dbi.KeyExistsException;
@@ -62,20 +59,6 @@ public class DbiTest {
6259
public final TemporaryFolder tmp = new TemporaryFolder();
6360
private Env<ByteBuffer> env;
6461

65-
@Test
66-
public void arrayConvenienceMethods() {
67-
final Dbi<ByteBuffer> db = env.openDbi(DB_1, MDB_CREATE);
68-
try (final Txn<ByteBuffer> txn = env.txnWrite()) {
69-
final byte[] key = "Hello world".getBytes(UTF_8);
70-
final byte[] val = "Need a new greeting".getBytes(UTF_8);
71-
db.put(txn, buffer(key), buffer(val));
72-
73-
final byte[] found = array(db.get(txn, buffer(key)));
74-
assertNotNull(found);
75-
assertThat(found, is("Need a new greeting".getBytes(UTF_8)));
76-
}
77-
}
78-
7962
@Before
8063
public void before() throws IOException {
8164
final File path = tmp.newFile();

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@
3737
import org.junit.Rule;
3838
import org.junit.Test;
3939
import org.junit.rules.TemporaryFolder;
40-
import static org.lmdbjava.ByteBufferProxy.array;
41-
import static org.lmdbjava.ByteBufferProxy.buffer;
4240
import static org.lmdbjava.ByteUnit.MEBIBYTES;
4341
import static org.lmdbjava.CursorIterator.IteratorType.BACKWARD;
4442
import static org.lmdbjava.CursorIterator.IteratorType.FORWARD;
@@ -302,22 +300,24 @@ public void tutorial4() throws IOException {
302300
final Dbi<ByteBuffer> db = env.openDbi(DB_NAME, MDB_CREATE);
303301

304302
try (final Txn<ByteBuffer> txn = env.txnWrite()) {
303+
final ByteBuffer key = allocateDirect(511);
304+
final ByteBuffer val = allocateDirect(700);
305305

306-
// Let's put some data in. We'll use our byte[] convenience methods.
307-
// These aren't recommended, but can be useful for legacy migration.
308-
final byte[] value = "Val".getBytes(UTF_8);
309-
db.put(txn, buffer("key1".getBytes(UTF_8)), buffer(value));
310-
db.put(txn, buffer("key2".getBytes(UTF_8)), buffer(value));
311-
db.put(txn, buffer("key3".getBytes(UTF_8)), buffer(value));
306+
// Insert some data
307+
val.putInt(100);
308+
key.putInt(1);
309+
db.put(txn, key, val);
310+
key.clear();
311+
key.putInt(2);
312+
db.put(txn, key, val);
313+
key.clear();
312314

313315
// Each iterator uses a cursor and must be closed when finished.
314316
// iterate forward in terms of key ordering starting with the first key
315317
try (final CursorIterator<ByteBuffer> it = db.iterate(txn, FORWARD)) {
316318
for (final KeyVal<ByteBuffer> kv : it.iterable()) {
317-
final ByteBuffer k = kv.key;
318-
final ByteBuffer v = kv.val;
319-
assertThat(k, notNullValue());
320-
assertThat(array(v), is(value));
319+
assertThat(kv.key, notNullValue());
320+
assertThat(kv.val, notNullValue());
321321
}
322322
}
323323

@@ -330,9 +330,8 @@ public void tutorial4() throws IOException {
330330
}
331331

332332
// search for key and iterate forwards/backward from there til the last/first key.
333-
final ByteBuffer searchKey = buffer("key2".getBytes(UTF_8));
334-
try (final CursorIterator<ByteBuffer> it = db.iterate(txn, searchKey,
335-
FORWARD)) {
333+
key.putInt(1);
334+
try (final CursorIterator<ByteBuffer> it = db.iterate(txn, key, FORWARD)) {
336335
for (final KeyVal<ByteBuffer> kv : it.iterable()) {
337336
assertThat(kv.key, notNullValue());
338337
assertThat(kv.val, notNullValue());

0 commit comments

Comments
 (0)