Skip to content

Commit 62062bb

Browse files
committed
Automatic formatting only
1 parent 8cac6e1 commit 62062bb

File tree

9 files changed

+198
-196
lines changed

9 files changed

+198
-196
lines changed

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2016 The LmdbJava Project, http://lmdbjava.org/.
2+
* Copyright 2016 The LmdbJava Project, http://lmdbjava.org/
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -15,15 +15,13 @@
1515
*/
1616
package org.lmdbjava;
1717

18+
import static java.lang.ThreadLocal.withInitial;
1819
import java.lang.reflect.Field;
1920
import java.nio.Buffer;
2021
import java.nio.ByteBuffer;
21-
import java.util.ArrayDeque;
22-
2322
import static java.nio.ByteBuffer.allocateDirect;
24-
23+
import java.util.ArrayDeque;
2524
import jnr.ffi.Pointer;
26-
2725
import static org.lmdbjava.UnsafeAccess.UNSAFE;
2826

2927
/**
@@ -41,13 +39,6 @@
4139
*/
4240
public final class ByteBufferProxy {
4341

44-
/**
45-
* A thread-safe pool for a given length. If the buffer found is bigger then the
46-
* buffer in the pool creates a new buffer. If no buffer is found creates a new buffer.
47-
*/
48-
private static final ThreadLocal<ArrayDeque<ByteBuffer>> BUFFERS
49-
= ThreadLocal.withInitial(() -> new ArrayDeque<>(16));
50-
5142
/**
5243
* The fastest {@link ByteBuffer} proxy that is available on this platform.
5344
* This will always be the same instance as {@link #PROXY_SAFE} if the
@@ -61,6 +52,13 @@ public final class ByteBufferProxy {
6152
* to never be null.
6253
*/
6354
public static final BufferProxy<ByteBuffer> PROXY_SAFE;
55+
/**
56+
* A thread-safe pool for a given length. If the buffer found is bigger then
57+
* the buffer in the pool creates a new buffer. If no buffer is found creates
58+
* a new buffer.
59+
*/
60+
private static final ThreadLocal<ArrayDeque<ByteBuffer>> BUFFERS
61+
= withInitial(() -> new ArrayDeque<>(16));
6462

6563
private static final String FIELD_NAME_ADDRESS = "address";
6664
private static final String FIELD_NAME_CAPACITY = "capacity";

src/main/java/org/lmdbjava/Cursor.java

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.lmdbjava.Env.SHOULD_CHECK;
2323
import static org.lmdbjava.Library.LIB;
2424
import static org.lmdbjava.MaskedFlag.mask;
25+
import static org.lmdbjava.PutFlags.MDB_RESERVE;
2526
import static org.lmdbjava.ResultCodeMapper.checkRc;
2627
import org.lmdbjava.Txn.CommittedException;
2728
import org.lmdbjava.Txn.ReadOnlyRequiredException;
@@ -176,39 +177,6 @@ public void put(final T key, final T val, final PutFlags... op)
176177
txn.valOut();
177178
}
178179

179-
/**
180-
* Reserve space for data of the given size, but don't copy the given val.
181-
* Instead, return a pointer to the reserved space, which the caller can fill in later -
182-
* before the next update operation or the transaction ends. This saves an extra memcpy
183-
* if the data is being generated later. LMDB does nothing else with this memory,
184-
* the caller is expected to modify all of the space requested.
185-
*
186-
* This flag must not be specified if the database was opened with MDB_DUPSORT
187-
*
188-
* @param key key to store in the database (not null)
189-
* @param val size of the value to be stored in the database (not null)
190-
* @throws CommittedException
191-
* @throws LmdbNativeException
192-
* @throws ReadWriteRequiredException
193-
*/
194-
public void reserve(final T key, final T val)
195-
throws LmdbNativeException, CommittedException, ClosedException,
196-
ReadWriteRequiredException {
197-
if (SHOULD_CHECK) {
198-
requireNonNull(key);
199-
requireNonNull(val);
200-
checkNotClosed();
201-
txn.checkNotCommitted();
202-
txn.checkWritesAllowed();
203-
}
204-
txn.keyIn(key);
205-
txn.valIn(val);
206-
final int flags = mask(PutFlags.MDB_RESERVE);
207-
checkRc(LIB.mdb_cursor_put(ptrCursor, txn.ptrKey, txn.ptrVal, flags));
208-
txn.keyOut();
209-
txn.valOut();
210-
}
211-
212180
/**
213181
* Renew a cursor handle.
214182
* <p>
@@ -239,6 +207,43 @@ public void renew(final Txn<T> txn)
239207
this.txn = txn;
240208
}
241209

210+
/**
211+
* Reserve space for data of the given size, but don't copy the given
212+
* val.Instead, return a pointer to the reserved space, which the caller can
213+
* fill in later - before the next update operation or the transaction ends.
214+
* This saves an extra memcpy if the data is being generated later. LMDB does
215+
* nothing else with this memory, the caller is expected to modify all of the
216+
* space requested.
217+
* <p>
218+
* This flag must not be specified if the database was opened with MDB_DUPSORT
219+
*
220+
* @param key key to store in the database (not null)
221+
* @param val size of the value to be stored in the database (not null)
222+
* @throws CommittedException if already committed
223+
* @throws LmdbNativeException if a native C error occurred
224+
* @throws ClosedException if the cursor is already closed
225+
* @throws ReadWriteRequiredException if cursor using a read-only transaction
226+
*/
227+
public void reserve(final T key, final T val) throws
228+
LmdbNativeException,
229+
CommittedException,
230+
ClosedException,
231+
ReadWriteRequiredException {
232+
if (SHOULD_CHECK) {
233+
requireNonNull(key);
234+
requireNonNull(val);
235+
checkNotClosed();
236+
txn.checkNotCommitted();
237+
txn.checkWritesAllowed();
238+
}
239+
txn.keyIn(key);
240+
txn.valIn(val);
241+
final int flags = mask(MDB_RESERVE);
242+
checkRc(LIB.mdb_cursor_put(ptrCursor, txn.ptrKey, txn.ptrVal, flags));
243+
txn.keyOut();
244+
txn.valOut();
245+
}
246+
242247
/**
243248
* Reposition the key/value buffers based on the passed operation.
244249
*

src/main/java/org/lmdbjava/Dbi.java

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -228,38 +228,6 @@ public void put(final T key, final T val) throws
228228
}
229229
}
230230

231-
/**
232-
* Reserve space for data of the given size, but don't copy the given val.
233-
* Instead, return a pointer to the reserved space, which the caller can fill in later -
234-
* before the next update operation or the transaction ends. This saves an extra memcpy
235-
* if the data is being generated later. LMDB does nothing else with this memory,
236-
* the caller is expected to modify all of the space requested.
237-
*
238-
* This flag must not be specified if the database was opened with MDB_DUPSORT
239-
*
240-
* @param txn transaction handle (not null; not committed; must be R-W)
241-
* @param key key to store in the database (not null)
242-
* @param val size of the value to be stored in the database (not null)
243-
* @throws CommittedException
244-
* @throws LmdbNativeException
245-
* @throws ReadWriteRequiredException
246-
*/
247-
public void reserve(Txn<T> txn, final T key, final T val)
248-
throws CommittedException, LmdbNativeException,
249-
ReadWriteRequiredException {
250-
if (SHOULD_CHECK) {
251-
requireNonNull(txn);
252-
requireNonNull(key);
253-
txn.checkNotCommitted();
254-
txn.checkWritesAllowed();
255-
}
256-
txn.keyIn(key);
257-
txn.valIn(val);
258-
final int mask = mask(MDB_RESERVE);
259-
checkRc(LIB.mdb_put(txn.ptr, dbi, txn.ptrKey, txn.ptrVal, mask));
260-
txn.valOut(val); // marked as in,out in LMDB C docs
261-
}
262-
263231
/**
264232
* Store a key/value pair in the database.
265233
* <p>
@@ -294,6 +262,39 @@ public void put(final Txn<T> txn, final T key, final T val,
294262
txn.valOut(); // marked as in,out in LMDB C docs
295263
}
296264

265+
/**
266+
* Reserve space for data of the given size, but don't copy the given val.
267+
* Instead, return a pointer to the reserved space, which the caller can fill
268+
* in later - before the next update operation or the transaction ends. This
269+
* saves an extra memcpy if the data is being generated later. LMDB does
270+
* nothing else with this memory, the caller is expected to modify all of the
271+
* space requested.
272+
* <p>
273+
* This flag must not be specified if the database was opened with MDB_DUPSORT
274+
*
275+
* @param txn transaction handle (not null; not committed; must be R-W)
276+
* @param key key to store in the database (not null)
277+
* @param val size of the value to be stored in the database (not null)
278+
* @throws CommittedException if already committed
279+
* @throws LmdbNativeException if a native C error occurred
280+
* @throws ReadWriteRequiredException if a read-only transaction presented
281+
*/
282+
public void reserve(Txn<T> txn, final T key, final T val)
283+
throws CommittedException, LmdbNativeException,
284+
ReadWriteRequiredException {
285+
if (SHOULD_CHECK) {
286+
requireNonNull(txn);
287+
requireNonNull(key);
288+
txn.checkNotCommitted();
289+
txn.checkWritesAllowed();
290+
}
291+
txn.keyIn(key);
292+
txn.valIn(val);
293+
final int mask = mask(MDB_RESERVE);
294+
checkRc(LIB.mdb_put(txn.ptr, dbi, txn.ptrKey, txn.ptrVal, mask));
295+
txn.valOut(val); // marked as in,out in LMDB C docs
296+
}
297+
297298
/**
298299
* The specified DBI was changed unexpectedly.
299300
*/

src/main/java/org/lmdbjava/MutableDirectBufferProxy.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2016 The LmdbJava Project, http://lmdbjava.org/.
2+
* Copyright 2016 The LmdbJava Project, http://lmdbjava.org/
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -15,15 +15,13 @@
1515
*/
1616
package org.lmdbjava;
1717

18+
import static java.lang.ThreadLocal.withInitial;
1819
import java.nio.ByteBuffer;
19-
2020
import static java.nio.ByteBuffer.allocateDirect;
21-
2221
import jnr.ffi.Pointer;
2322
import org.agrona.MutableDirectBuffer;
2423
import org.agrona.concurrent.OneToOneConcurrentArrayQueue;
2524
import org.agrona.concurrent.UnsafeBuffer;
26-
2725
import static org.lmdbjava.UnsafeAccess.UNSAFE;
2826

2927
/**
@@ -32,22 +30,22 @@
3230
* This class requires {@link UnsafeAccess} and Agrona must be in the classpath.
3331
*/
3432
public final class MutableDirectBufferProxy extends
35-
BufferProxy<MutableDirectBuffer> {
36-
37-
/**
38-
* A thread-safe pool for a given length. If the buffer found is bigger then the
39-
* buffer in the pool creates a new buffer. If no buffer is found creates a new buffer.
40-
*/
41-
private static final ThreadLocal<OneToOneConcurrentArrayQueue<MutableDirectBuffer>> BUFFERS
42-
= ThreadLocal.withInitial(() -> new OneToOneConcurrentArrayQueue<>(16));
33+
BufferProxy<MutableDirectBuffer> {
4334

4435
/**
4536
* The {@link MutableDirectBuffer} proxy. Guaranteed to never be null,
4637
* although a class initialization exception will occur if an attempt is made
4738
* to access this field when unsafe or Agrona is unavailable.
4839
*/
4940
public static final BufferProxy<MutableDirectBuffer> PROXY_MDB
50-
= new MutableDirectBufferProxy();
41+
= new MutableDirectBufferProxy();
42+
/**
43+
* A thread-safe pool for a given length. If the buffer found is bigger then
44+
* the buffer in the pool creates a new buffer. If no buffer is found creates
45+
* a new buffer.
46+
*/
47+
private static final ThreadLocal<OneToOneConcurrentArrayQueue<MutableDirectBuffer>> BUFFERS
48+
= withInitial(() -> new OneToOneConcurrentArrayQueue<>(16));
5149

5250
@Override
5351
protected MutableDirectBuffer allocate() {

src/test/java/org/lmdbjava/ByteBufProxy.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,41 @@
11
package org.lmdbjava;
22

33
import io.netty.buffer.ByteBuf;
4-
import io.netty.buffer.PooledByteBufAllocator;
5-
import jnr.ffi.Pointer;
6-
4+
import static io.netty.buffer.PooledByteBufAllocator.DEFAULT;
5+
import static java.lang.Class.forName;
6+
import static java.lang.ThreadLocal.withInitial;
77
import java.lang.reflect.Field;
88
import java.util.ArrayDeque;
9-
9+
import jnr.ffi.Pointer;
1010
import static org.lmdbjava.UnsafeAccess.UNSAFE;
1111

1212
/**
1313
* A buffer proxy backed by Netty's {@link ByteBuf}.
1414
* <p>
15-
* This class requires {@link UnsafeAccess} and netty-buffer must be in the classpath.
15+
* This class requires {@link UnsafeAccess} and netty-buffer must be in the
16+
* classpath.
1617
*/
1718
public class ByteBufProxy extends BufferProxy<ByteBuf> {
1819

20+
private static final long ADDRESS_OFFSET;
21+
1922
/**
20-
* A thread-safe pool for a given length. If the buffer found is bigger then the
21-
* buffer in the pool creates a new buffer. If no buffer is found creates a new buffer.
23+
* A thread-safe pool for a given length. If the buffer found is bigger then
24+
* the buffer in the pool creates a new buffer. If no buffer is found creates
25+
* a new buffer.
2226
*/
2327
private static final ThreadLocal<ArrayDeque<ByteBuf>> BUFFERS
24-
= ThreadLocal.withInitial(() -> new ArrayDeque<>(16));
28+
= withInitial(() -> new ArrayDeque<>(16));
2529

2630
private static final String FIELD_NAME_ADDRESS = "memoryAddress";
2731
private static final String FIELD_NAME_LENGTH = "length";
28-
29-
private static final long ADDRESS_OFFSET;
3032
private static final long LENGTH_OFFSET;
33+
private static final String NAME = "io.netty.buffer.PooledUnsafeDirectByteBuf";
3134

3235
static {
3336
try {
34-
final Field address = findField("io.netty.buffer.PooledUnsafeDirectByteBuf", FIELD_NAME_ADDRESS);
35-
final Field length = findField("io.netty.buffer.PooledUnsafeDirectByteBuf", FIELD_NAME_LENGTH);
37+
final Field address = findField(NAME, FIELD_NAME_ADDRESS);
38+
final Field length = findField(NAME, FIELD_NAME_LENGTH);
3639
ADDRESS_OFFSET = UNSAFE.objectFieldOffset(address);
3740
LENGTH_OFFSET = UNSAFE.objectFieldOffset(length);
3841
} catch (SecurityException e) {
@@ -43,7 +46,7 @@ public class ByteBufProxy extends BufferProxy<ByteBuf> {
4346
static Field findField(final String c, final String name) {
4447
Class<?> clazz;
4548
try {
46-
clazz = Class.forName(c);
49+
clazz = forName(c);
4750
} catch (ClassNotFoundException e) {
4851
throw new RuntimeException(e);
4952
}
@@ -67,7 +70,7 @@ protected ByteBuf allocate() {
6770
if (buffer != null && buffer.capacity() >= 0) {
6871
return buffer;
6972
} else {
70-
return PooledByteBufAllocator.DEFAULT.directBuffer(0);
73+
return DEFAULT.directBuffer(0);
7174
}
7275
}
7376

@@ -81,8 +84,10 @@ protected void deallocate(ByteBuf buff) {
8184

8285
@Override
8386
protected void in(ByteBuf buffer, Pointer ptr, long ptrAddr) {
84-
UNSAFE.putLong(ptrAddr + STRUCT_FIELD_OFFSET_SIZE, buffer.writerIndex() - buffer.readerIndex());
85-
UNSAFE.putLong(ptrAddr + STRUCT_FIELD_OFFSET_DATA, buffer.memoryAddress() + buffer.readerIndex());
87+
UNSAFE.putLong(ptrAddr + STRUCT_FIELD_OFFSET_SIZE,
88+
buffer.writerIndex() - buffer.readerIndex());
89+
UNSAFE.putLong(ptrAddr + STRUCT_FIELD_OFFSET_DATA,
90+
buffer.memoryAddress() + buffer.readerIndex());
8691
}
8792

8893
@Override

0 commit comments

Comments
 (0)