Skip to content

Commit 311d682

Browse files
committed
pass in size to reserve instead of val buffer
1 parent 3b21c23 commit 311d682

File tree

9 files changed

+63
-17
lines changed

9 files changed

+63
-17
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ public abstract class BufferProxy<T> {
7070
*/
7171
protected abstract void in(T buffer, Pointer ptr, long ptrAddr);
7272

73+
/**
74+
* Called when the <code>MDB_val</code> should be set to reflect the passed
75+
* buffer. This buffer will have been created by end users, not
76+
* {@link #allocate()}.
77+
*
78+
* @param buffer the buffer to write to <code>MDB_val</code>
79+
* @param size the buffer size to write to <code>MDB_val</code>
80+
* @param ptr the pointer to the <code>MDB_val</code>
81+
* @param ptrAddr the address of the <code>MDB_val</code> pointer
82+
*/
83+
protected abstract void in(T buffer, int size, Pointer ptr, long ptrAddr);
84+
7385
/**
7486
* Called when the <code>MDB_val</code> may have changed and the passed buffer
7587
* should be modified to reflect the new <code>MDB_val</code>.

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ protected void in(final ByteBuffer buffer, final Pointer ptr,
135135
ptr.putLong(STRUCT_FIELD_OFFSET_DATA, addr);
136136
}
137137

138+
@Override
139+
protected void in(ByteBuffer buffer, int size, Pointer ptr, long ptrAddr) {
140+
final long addr = ((sun.nio.ch.DirectBuffer) buffer).address();
141+
ptr.putLong(STRUCT_FIELD_OFFSET_SIZE, size);
142+
ptr.putLong(STRUCT_FIELD_OFFSET_DATA, addr);
143+
}
144+
138145
@Override
139146
protected void out(final ByteBuffer buffer, final Pointer ptr,
140147
final long ptrAddr) {
@@ -198,6 +205,13 @@ protected void in(final ByteBuffer buffer, final Pointer ptr,
198205
UNSAFE.putLong(ptrAddr + STRUCT_FIELD_OFFSET_DATA, addr);
199206
}
200207

208+
@Override
209+
protected void in(ByteBuffer buffer, int size, Pointer ptr, long ptrAddr) {
210+
final long addr = ((sun.nio.ch.DirectBuffer) buffer).address();
211+
UNSAFE.putLong(ptrAddr + STRUCT_FIELD_OFFSET_SIZE, size);
212+
UNSAFE.putLong(ptrAddr + STRUCT_FIELD_OFFSET_DATA, addr);
213+
}
214+
201215
@Override
202216
protected void out(final ByteBuffer buffer, final Pointer ptr,
203217
final long ptrAddr) {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,21 +187,21 @@ public void renew(final Txn<T> txn) {
187187
* This flag must not be specified if the database was opened with MDB_DUPSORT
188188
*
189189
* @param key key to store in the database (not null)
190-
* @param val size of the value to be stored in the database (not null)
190+
* @param size size of the value to be stored in the database (not null)
191191
*/
192-
public void reserve(final T key, final T val) {
192+
public T reserve(final T key, final int size) {
193193
if (SHOULD_CHECK) {
194194
requireNonNull(key);
195-
requireNonNull(val);
196195
checkNotClosed();
197196
txn.checkNotCommitted();
198197
txn.checkWritesAllowed();
199198
}
200199
txn.keyIn(key);
201-
txn.valIn(val);
200+
txn.valIn(size);
202201
final int flags = mask(MDB_RESERVE);
203202
checkRc(LIB.mdb_cursor_put(ptrCursor, txn.ptrKey, txn.ptrVal, flags));
204-
txn.valOut(val);
203+
txn.valOut();
204+
return txn.val();
205205
}
206206

207207
/**

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,20 +234,21 @@ public void put(final Txn<T> txn, final T key, final T val,
234234
*
235235
* @param txn transaction handle (not null; not committed; must be R-W)
236236
* @param key key to store in the database (not null)
237-
* @param val size of the value to be stored in the database (not null)
237+
* @param size size of the value to be stored in the database
238238
*/
239-
public void reserve(Txn<T> txn, final T key, final T val) {
239+
public T reserve(Txn<T> txn, final T key, int size) {
240240
if (SHOULD_CHECK) {
241241
requireNonNull(txn);
242242
requireNonNull(key);
243243
txn.checkNotCommitted();
244244
txn.checkWritesAllowed();
245245
}
246246
txn.keyIn(key);
247-
txn.valIn(val);
247+
txn.valIn(size);
248248
final int mask = mask(MDB_RESERVE);
249249
checkRc(LIB.mdb_put(txn.ptr, dbi, txn.ptrKey, txn.ptrVal, mask));
250-
txn.valOut(val); // marked as in,out in LMDB C docs
250+
txn.valOut(); // marked as in,out in LMDB C docs
251+
return txn.val();
251252
}
252253

253254
/**

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ protected void in(final MutableDirectBuffer buffer, final Pointer ptr,
7676
UNSAFE.putLong(ptrAddr + STRUCT_FIELD_OFFSET_SIZE, size);
7777
}
7878

79+
@Override
80+
protected void in(MutableDirectBuffer buffer, int size, Pointer ptr, long ptrAddr) {
81+
final long addr = buffer.addressOffset();
82+
UNSAFE.putLong(ptrAddr + STRUCT_FIELD_OFFSET_DATA, addr);
83+
UNSAFE.putLong(ptrAddr + STRUCT_FIELD_OFFSET_SIZE, size);
84+
}
85+
7986
@Override
8087
protected void out(final MutableDirectBuffer buffer, final Pointer ptr,
8188
final long ptrAddr) {

src/main/java/org/lmdbjava/Txn.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@ void valIn(T val) {
250250
proxy.in(val, ptrVal, ptrValAddr);
251251
}
252252

253+
void valIn(int size) {
254+
proxy.in(val, size, ptrVal, ptrValAddr);
255+
}
256+
253257
void valOut() {
254258
proxy.out(val, ptrVal, ptrValAddr);
255259
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ protected void in(ByteBuf buffer, Pointer ptr, long ptrAddr) {
9090
buffer.memoryAddress() + buffer.readerIndex());
9191
}
9292

93+
@Override
94+
protected void in(ByteBuf buffer, int size, Pointer ptr, long ptrAddr) {
95+
UNSAFE.putLong(ptrAddr + STRUCT_FIELD_OFFSET_SIZE,
96+
size);
97+
UNSAFE.putLong(ptrAddr + STRUCT_FIELD_OFFSET_DATA,
98+
buffer.memoryAddress() + buffer.readerIndex());
99+
}
100+
93101
@Override
94102
protected void out(ByteBuf buffer, Pointer ptr, long ptrAddr) {
95103
final long addr = UNSAFE.getLong(ptrAddr + STRUCT_FIELD_OFFSET_DATA);

src/test/java/org/lmdbjava/CursorTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,16 +324,16 @@ public void reserve() {
324324
final ByteBuffer key = createBb(5);
325325
try (Txn<ByteBuffer> txn = env.txnWrite()) {
326326
final Cursor<ByteBuffer> c = db.openCursor(txn);
327-
final ByteBuffer val = createBb(MAX_VALUE);
328327
assertNull(db.get(txn, key));
329-
c.reserve(key, val);
328+
final ByteBuffer val = c.reserve(key, Long.BYTES * 2);
330329
assertNotNull(db.get(txn, key));
331-
val.putInt(16).flip();
330+
val.putLong(Long.MIN_VALUE).flip();
332331
txn.commit();
333332
}
334333
try (final Txn<ByteBuffer> txn = env.txnWrite()) {
335334
final ByteBuffer val = db.get(txn, key);
336-
assertThat(val.getInt(), is(16));
335+
assertThat(val.capacity(), is(Long.BYTES * 2));
336+
assertThat(val.getLong(), is(Long.MIN_VALUE));
337337
}
338338
}
339339

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,16 @@ public void putReserve() {
169169

170170
final ByteBuffer key = createBb(5);
171171
try (final Txn<ByteBuffer> txn = env.txnWrite()) {
172-
final ByteBuffer val = createBb(MAX_VALUE);
173172
assertNull(db.get(txn, key));
174-
db.reserve(txn, key, val);
173+
final ByteBuffer val = db.reserve(txn, key, 8);
174+
val.putLong(Long.MAX_VALUE);
175175
assertNotNull(db.get(txn, key));
176-
val.putInt(16).flip();
177176
txn.commit();
178177
}
179178
try (final Txn<ByteBuffer> txn = env.txnWrite()) {
180179
final ByteBuffer val = db.get(txn, key);
181-
assertThat(val.getInt(), is(16));
180+
assertThat(val.capacity(), is(8));
181+
assertThat(val.getLong(), is(Long.MAX_VALUE));
182182
}
183183
}
184184

0 commit comments

Comments
 (0)