Skip to content

Commit 5453c24

Browse files
committed
Automatic formatting only
1 parent 8c867d4 commit 5453c24

15 files changed

Lines changed: 195 additions & 138 deletions

src/main/java/org/lmdbjava/BufferProxy.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ public interface BufferProxy<T> {
4444
* Called when the <code>MDB_val</code> may have changed and the passed buffer
4545
* should be modified to reflect the new <code>MDB_val</code>.
4646
*
47-
* @param buffer the buffer to modify to reflect the <code>MDB_val</code>
48-
* @param ptr the pointer to the <code>MDB_val</code>
49-
* @param ptrAddr the address of the <code>MDB_val</code> pointer
47+
* @param ptr the pointer to the <code>MDB_val</code>
48+
* @param ptrAddr the address of the <code>MDB_val</code> pointer
5049
*/
5150
void out(Pointer ptr, long ptrAddr);
5251

@@ -68,6 +67,10 @@ public interface BufferProxy<T> {
6867
*/
6968
T allocate(int bytes);
7069

70+
/**
71+
*
72+
* @return
73+
*/
7174
T buffer();
7275

7376
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
package org.lmdbjava;
22

3+
/**
4+
*
5+
* @param <T>
6+
*/
37
public interface BufferProxyFactory<T> {
8+
9+
/**
10+
*
11+
* @return
12+
*/
413
BufferProxy<T> allocate();
14+
15+
/**
16+
*
17+
* @param proxy
18+
*/
519
void deallocate(BufferProxy<T> proxy);
620
}

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

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ static final class ReflectiveProxy implements BufferProxy<ByteBuffer> {
100100
ByteBuffer buffer;
101101

102102
ReflectiveProxy() {
103-
this.buffer = ByteBuffer.allocateDirect(0);
103+
this.buffer = allocateDirect(0);
104104
}
105105

106106
@Override
@@ -109,16 +109,8 @@ public ByteBuffer allocate(int bytes) {
109109
}
110110

111111
@Override
112-
public void out(Pointer ptr, long ptrAddr) {
113-
final long addr = ptr.getLong(STRUCT_FIELD_OFFSET_DATA);
114-
final long size = ptr.getLong(STRUCT_FIELD_OFFSET_SIZE);
115-
try {
116-
ADDRESS_FIELD.set(buffer, addr);
117-
CAPACITY_FIELD.set(buffer, (int) size);
118-
} catch (IllegalArgumentException | IllegalAccessException ex) {
119-
throw new RuntimeException("Cannot modify buffer", ex);
120-
}
121-
buffer.clear();
112+
public ByteBuffer buffer() {
113+
return buffer;
122114
}
123115

124116
@Override
@@ -129,16 +121,24 @@ public void in(ByteBuffer buffer, Pointer ptr, long ptrAddr) {
129121
}
130122

131123
@Override
132-
public ByteBuffer buffer() {
133-
return buffer;
124+
public void out(Pointer ptr, long ptrAddr) {
125+
final long addr = ptr.getLong(STRUCT_FIELD_OFFSET_DATA);
126+
final long size = ptr.getLong(STRUCT_FIELD_OFFSET_SIZE);
127+
try {
128+
ADDRESS_FIELD.set(buffer, addr);
129+
CAPACITY_FIELD.set(buffer, (int) size);
130+
} catch (IllegalArgumentException | IllegalAccessException ex) {
131+
throw new RuntimeException("Cannot modify buffer", ex);
132+
}
133+
buffer.clear();
134134
}
135135
}
136136

137137
/**
138138
* A proxy that uses Java's "unsafe" class to directly manipulate byte buffer
139139
* fields and JNR-FFF allocated memory pointers.
140140
*/
141-
private static final class UnsafeProxy implements BufferProxy<ByteBuffer> {
141+
static final class UnsafeProxy implements BufferProxy<ByteBuffer> {
142142

143143
static final long ADDRESS_OFFSET;
144144
static final long CAPACITY_OFFSET;
@@ -156,7 +156,7 @@ private static final class UnsafeProxy implements BufferProxy<ByteBuffer> {
156156
ByteBuffer buffer;
157157

158158
UnsafeProxy() {
159-
buffer = ByteBuffer.allocateDirect(0);
159+
buffer = allocateDirect(0);
160160
}
161161

162162
@Override
@@ -165,12 +165,8 @@ public ByteBuffer allocate(int bytes) {
165165
}
166166

167167
@Override
168-
public void out(Pointer ptr, long ptrAddr) {
169-
final long addr = UNSAFE.getLong(ptrAddr + STRUCT_FIELD_OFFSET_DATA);
170-
final long size = UNSAFE.getLong(ptrAddr + STRUCT_FIELD_OFFSET_SIZE);
171-
UNSAFE.putLong(buffer, ADDRESS_OFFSET, addr);
172-
UNSAFE.putInt(buffer, CAPACITY_OFFSET, (int) size);
173-
buffer.clear();
168+
public ByteBuffer buffer() {
169+
return buffer;
174170
}
175171

176172
@Override
@@ -181,8 +177,12 @@ public void in(ByteBuffer buffer, Pointer ptr, long ptrAddr) {
181177
}
182178

183179
@Override
184-
public ByteBuffer buffer() {
185-
return buffer;
180+
public void out(Pointer ptr, long ptrAddr) {
181+
final long addr = UNSAFE.getLong(ptrAddr + STRUCT_FIELD_OFFSET_DATA);
182+
final long size = UNSAFE.getLong(ptrAddr + STRUCT_FIELD_OFFSET_SIZE);
183+
UNSAFE.putLong(buffer, ADDRESS_OFFSET, addr);
184+
UNSAFE.putInt(buffer, CAPACITY_OFFSET, (int) size);
185+
buffer.clear();
186186
}
187187
}
188188
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import static java.util.Objects.requireNonNull;
1919
import jnr.ffi.Pointer;
2020
import jnr.ffi.byref.NativeLongByReference;
21-
2221
import static org.lmdbjava.CursorOp.MDB_SET;
2322
import static org.lmdbjava.CursorOp.MDB_SET_KEY;
2423
import static org.lmdbjava.CursorOp.MDB_SET_RANGE;
@@ -143,7 +142,8 @@ public boolean get(final T key, final CursorOp op)
143142
txc.keyIn(key);
144143
}
145144

146-
final int rc = LIB.mdb_cursor_get(ptrCursor, txc.ptrKey, txc.ptrVal, op.getCode());
145+
final int rc = LIB.mdb_cursor_get(ptrCursor, txc.ptrKey, txc.ptrVal,
146+
op.getCode());
147147

148148
if (rc == MDB_NOTFOUND) {
149149
return false;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import static org.lmdbjava.ResultCodeMapper.checkRc;
3030
import org.lmdbjava.Txn.CommittedException;
3131
import org.lmdbjava.Txn.ReadWriteRequiredException;
32-
import static org.lmdbjava.TxnFlags.MDB_RDONLY;
3332

3433
/**
3534
* LMDB Database.
@@ -64,7 +63,7 @@ public final class Dbi<T> {
6463
* @throws ReadWriteRequiredException if a read-only transaction presented
6564
*/
6665
Dbi(final Txn tx, final String name,
67-
final BufferProxyFactory<T> proxyFactory, final DbiFlags... flags)
66+
final BufferProxyFactory<T> proxyFactory, final DbiFlags... flags)
6867
throws CommittedException, LmdbNativeException, ReadWriteRequiredException {
6968
requireNonNull(tx);
7069
requireNonNull(proxyFactory);

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

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@
2121
import static java.util.Objects.requireNonNull;
2222
import jnr.ffi.Pointer;
2323
import jnr.ffi.byref.PointerByReference;
24-
import static org.lmdbjava.ByteBufferProxy.PROXY_OPTIMAL;
2524
import static org.lmdbjava.Library.LIB;
2625
import org.lmdbjava.Library.MDB_envinfo;
2726
import org.lmdbjava.Library.MDB_stat;
2827
import static org.lmdbjava.Library.RUNTIME;
2928
import static org.lmdbjava.MaskedFlag.mask;
3029
import static org.lmdbjava.ResultCodeMapper.checkRc;
3130
import org.lmdbjava.Txn.CommittedException;
31+
import static org.lmdbjava.TxnFlags.MDB_RDONLY;
3232

3333
/**
3434
* LMDB environment.
35+
*
36+
* @param <T>
3537
*/
3638
public final class Env<T> implements AutoCloseable {
3739

@@ -48,23 +50,10 @@ public final class Env<T> implements AutoCloseable {
4850
*/
4951
public static final boolean SHOULD_CHECK = !getBoolean(DISABLE_CHECKS_PROP);
5052

51-
private boolean closed = false;
52-
private boolean open = false;
53-
final Pointer ptr;
54-
final BufferProxyFactory<T> factory;
55-
5653
/**
57-
* Creates a new environment handle.
5854
*
59-
* @throws LmdbNativeException if a native C error occurred
55+
* @return @throws LmdbNativeException
6056
*/
61-
Env(BufferProxyFactory<T> factory) throws LmdbNativeException {
62-
final PointerByReference envPtr = new PointerByReference();
63-
checkRc(LIB.mdb_env_create(envPtr));
64-
ptr = envPtr.getValue();
65-
this.factory = factory;
66-
}
67-
6857
public static Env<ByteBuffer> create() throws LmdbNativeException {
6958
return new Env<>(new BufferProxyFactory<ByteBuffer>() {
7059
@Override
@@ -78,11 +67,34 @@ public void deallocate(BufferProxy<ByteBuffer> proxy) {
7867
});
7968
}
8069

81-
public static <T> Env<T> create(BufferProxyFactory<T> factory)
82-
throws LmdbNativeException {
70+
/**
71+
*
72+
* @param <T>
73+
* @param factory
74+
* @return
75+
* @throws LmdbNativeException
76+
*/
77+
public static <T> Env<T> create(BufferProxyFactory<T> factory) throws
78+
LmdbNativeException {
8379
return new Env<>(factory);
8480
}
8581

82+
private boolean closed = false;
83+
private boolean open = false;
84+
final BufferProxyFactory<T> factory;
85+
final Pointer ptr;
86+
87+
/**
88+
* Creates a new environment handle.
89+
*
90+
* @throws LmdbNativeException if a native C error occurred
91+
*/
92+
Env(BufferProxyFactory<T> factory) throws LmdbNativeException {
93+
final PointerByReference envPtr = new PointerByReference();
94+
checkRc(LIB.mdb_env_create(envPtr));
95+
ptr = envPtr.getValue();
96+
this.factory = factory;
97+
}
8698

8799
/**
88100
* Close the handle.
@@ -274,7 +286,6 @@ public void open(final File path, final int mode, final EnvFlags... flags)
274286
*
275287
* @param <T> buffer type that used by {@link Dbi} and its {@link Cursor}s
276288
* @param name name of the database (or null if no name is required)
277-
* @param proxy the proxy to use for buffer management
278289
* @param flags to open the database with
279290
* @return a database that is ready to use
280291
* @throws NotOpenException
@@ -291,14 +302,6 @@ public <T> Dbi<T> openDbi(String name, DbiFlags... flags)
291302
}
292303
}
293304

294-
public Txn txnWrite() throws NotOpenException, LmdbNativeException {
295-
return new Txn(this, factory, null);
296-
}
297-
298-
public Txn txnRead() throws NotOpenException, LmdbNativeException {
299-
return new Txn(this, factory, null, TxnFlags.MDB_RDONLY);
300-
}
301-
302305
/**
303306
* Return statistics about this environment.
304307
*
@@ -349,6 +352,25 @@ public void sync(final boolean force) throws AlreadyClosedException,
349352
checkRc(LIB.mdb_env_sync(ptr, f));
350353
}
351354

355+
/**
356+
*
357+
* @return @throws NotOpenException
358+
* @throws LmdbNativeException
359+
*/
360+
public Txn txnRead() throws NotOpenException, LmdbNativeException {
361+
return new Txn(this, factory, null, MDB_RDONLY);
362+
}
363+
364+
/**
365+
*
366+
* @return @throws NotOpenException
367+
* @throws LmdbNativeException
368+
*/
369+
public Txn txnWrite() throws NotOpenException,
370+
LmdbNativeException {
371+
return new Txn(this, factory, null);
372+
}
373+
352374
/**
353375
* Object has already been closed and the operation is therefore prohibited.
354376
*/

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

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ public final class MutableDirectBufferProxy implements
3838
public static final BufferProxy<MutableDirectBuffer> FACTORY_MDB
3939
= new MutableDirectBufferProxy();
4040

41+
/**
42+
*
43+
*/
44+
public static MutableDirectBufferProxyFactory MDB_FACTORY
45+
= new MutableDirectBufferProxyFactory();
46+
4147
private static MutableDirectBuffer alloc(int bytes) {
4248
ByteBuffer bb = allocateDirect(bytes);
4349
return new UnsafeBuffer(bb);
@@ -53,18 +59,15 @@ public MutableDirectBuffer allocate(int bytes) {
5359
return alloc(bytes);
5460
}
5561

62+
/**
63+
*
64+
* @return
65+
*/
5666
@Override
5767
public MutableDirectBuffer buffer() {
5868
return buffer;
5969
}
6070

61-
@Override
62-
public void out(Pointer ptr, long ptrAddr) {
63-
final long addr = UNSAFE.getLong(ptrAddr + STRUCT_FIELD_OFFSET_DATA);
64-
final long size = UNSAFE.getLong(ptrAddr + STRUCT_FIELD_OFFSET_SIZE);
65-
buffer.wrap(addr, (int) size);
66-
}
67-
6871
@Override
6972
public void in(MutableDirectBuffer buffer, Pointer ptr, long ptrAddr) {
7073
final long addr = buffer.addressOffset();
@@ -73,16 +76,32 @@ public void in(MutableDirectBuffer buffer, Pointer ptr, long ptrAddr) {
7376
UNSAFE.putLong(ptrAddr + STRUCT_FIELD_OFFSET_SIZE, size);
7477
}
7578

76-
public static MutableDirectBufferProxyFactory MDB_FACTORY = new MutableDirectBufferProxyFactory();
79+
@Override
80+
public void out(Pointer ptr, long ptrAddr) {
81+
final long addr = UNSAFE.getLong(ptrAddr + STRUCT_FIELD_OFFSET_DATA);
82+
final long size = UNSAFE.getLong(ptrAddr + STRUCT_FIELD_OFFSET_SIZE);
83+
buffer.wrap(addr, (int) size);
84+
}
7785

86+
/**
87+
*
88+
*/
7889
public static class MutableDirectBufferProxyFactory
79-
implements BufferProxyFactory<MutableDirectBuffer> {
90+
implements BufferProxyFactory<MutableDirectBuffer> {
8091

92+
/**
93+
*
94+
* @return
95+
*/
8196
@Override
8297
public MutableDirectBufferProxy allocate() {
8398
return new MutableDirectBufferProxy();
8499
}
85100

101+
/**
102+
*
103+
* @param proxy
104+
*/
86105
@Override
87106
public void deallocate(BufferProxy proxy) {
88107

0 commit comments

Comments
 (0)