-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathCursor.java
More file actions
547 lines (500 loc) · 17.7 KB
/
Cursor.java
File metadata and controls
547 lines (500 loc) · 17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
* Copyright © 2016-2025 The LmdbJava Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.lmdbjava;
import static java.util.Objects.requireNonNull;
import static org.lmdbjava.Dbi.KeyExistsException.MDB_KEYEXIST;
import static org.lmdbjava.Dbi.KeyNotFoundException.MDB_NOTFOUND;
import static org.lmdbjava.Env.SHOULD_CHECK;
import static org.lmdbjava.Library.LIB;
import static org.lmdbjava.PutFlags.MDB_MULTIPLE;
import static org.lmdbjava.PutFlags.MDB_NODUPDATA;
import static org.lmdbjava.PutFlags.MDB_NOOVERWRITE;
import static org.lmdbjava.PutFlags.MDB_RESERVE;
import static org.lmdbjava.ResultCodeMapper.checkRc;
import static org.lmdbjava.SeekOp.MDB_FIRST;
import static org.lmdbjava.SeekOp.MDB_LAST;
import static org.lmdbjava.SeekOp.MDB_NEXT;
import static org.lmdbjava.SeekOp.MDB_PREV;
import jnr.ffi.Pointer;
import jnr.ffi.byref.NativeLongByReference;
/**
* A cursor handle.
*
* @param <T> buffer type
*/
public final class Cursor<T> implements AutoCloseable {
private boolean closed;
private final KeyVal<T> kv;
private final Pointer ptrCursor;
private Txn<T> txn;
private final Env<T> env;
Cursor(final Pointer ptr, final Txn<T> txn, final Env<T> env) {
requireNonNull(ptr);
requireNonNull(txn);
this.ptrCursor = ptr;
this.txn = txn;
this.kv = txn.newKeyVal();
this.env = env;
}
/**
* Close a cursor handle.
*
* <p>The cursor handle will be freed and must not be used again after this call. Its transaction
* must still be live if it is a write-transaction.
*/
@Override
public void close() {
if (closed) {
return;
}
kv.close();
if (SHOULD_CHECK) {
env.checkNotClosed();
if (!txn.isReadOnly()) {
txn.checkReady();
}
}
LIB.mdb_cursor_close(ptrCursor);
closed = true;
}
/**
* Return count of duplicates for current key.
*
* <p>This call is only valid on databases that support sorted duplicate data items {@link
* DbiFlags#MDB_DUPSORT}.
*
* @return count of duplicates for current key
*/
public long count() {
if (SHOULD_CHECK) {
env.checkNotClosed();
checkNotClosed();
txn.checkReady();
}
final NativeLongByReference longByReference = new NativeLongByReference();
checkRc(LIB.mdb_cursor_count(ptrCursor, longByReference));
return longByReference.longValue();
}
/**
* @deprecated Instead use {@link Cursor#delete(PutFlagSet)}. <hr> Delete current key/data pair.
* <p>This function deletes the key/data pair to which the cursor refers.
* @param flags flags (either null or {@link PutFlags#MDB_NODUPDATA}
*/
@Deprecated
public void delete(final PutFlags... flags) {
delete(PutFlagSet.of(flags));
}
/**
* Delete current key/data pair.
*
* <p>This function deletes the key/data pair to which the cursor refers.
*/
public void delete() {
delete(PutFlagSet.EMPTY);
}
/**
* Delete current key/data pair.
*
* <p>This function deletes the key/data pair to which the cursor refers.
*
* @param flags flags (either null or {@link PutFlags#MDB_NODUPDATA}
*/
public void delete(final PutFlagSet flags) {
if (SHOULD_CHECK) {
env.checkNotClosed();
checkNotClosed();
txn.checkReady();
txn.checkWritesAllowed();
}
final PutFlagSet putFlagSet = flags != null ? flags : PutFlagSet.EMPTY;
checkRc(LIB.mdb_cursor_del(ptrCursor, putFlagSet.getMask()));
}
/**
* Position at first key/data item.
*
* @return false if requested position not found
*/
public boolean first() {
return seek(MDB_FIRST);
}
/**
* Reposition the key/value buffers based on the passed key and operation.
*
* @param key to search for
* @param data to search for
* @param op options for this operation
* @return false if key not found
*/
public boolean get(final T key, final T data, final SeekOp op) {
if (SHOULD_CHECK) {
requireNonNull(key);
requireNonNull(op);
env.checkNotClosed();
checkNotClosed();
txn.checkReady();
}
final Pointer transientKey = kv.keyIn(key);
final Pointer transientVal = kv.valIn(data);
final int rc = LIB.mdb_cursor_get(ptrCursor, kv.pointerKey(), kv.pointerVal(), op.getCode());
if (rc == MDB_NOTFOUND) {
return false;
}
checkRc(rc);
kv.keyOut();
kv.valOut();
ReferenceUtil.reachabilityFence0(transientKey);
ReferenceUtil.reachabilityFence0(transientVal);
ReferenceUtil.reachabilityFence0(kv.key());
ReferenceUtil.reachabilityFence0(kv.val());
ReferenceUtil.reachabilityFence0(key);
return true;
}
/**
* Reposition the key/value buffers based on the passed key and operation.
*
* @param key to search for
* @param op options for this operation
* @return false if key not found
*/
public boolean get(final T key, final GetOp op) {
if (SHOULD_CHECK) {
requireNonNull(key);
requireNonNull(op);
env.checkNotClosed();
checkNotClosed();
txn.checkReady();
}
final Pointer transientKey = kv.keyIn(key);
final int rc = LIB.mdb_cursor_get(ptrCursor, kv.pointerKey(), kv.pointerVal(), op.getCode());
if (rc == MDB_NOTFOUND) {
return false;
}
checkRc(rc);
kv.keyOut();
kv.valOut();
ReferenceUtil.reachabilityFence0(transientKey);
ReferenceUtil.reachabilityFence0(kv.key());
ReferenceUtil.reachabilityFence0(kv.val());
ReferenceUtil.reachabilityFence0(key);
return true;
}
/**
* Obtain the key.
*
* @return the key that the cursor is located at.
*/
public T key() {
return kv.key();
}
KeyVal<T> keyVal() {
return kv;
}
/**
* Position at last key/data item.
*
* @return false if requested position not found
*/
public boolean last() {
return seek(MDB_LAST);
}
/**
* Position at next data item.
*
* @return false if requested position not found
*/
public boolean next() {
return seek(MDB_NEXT);
}
/**
* Position at previous data item.
*
* @return false if requested position not found
*/
public boolean prev() {
return seek(MDB_PREV);
}
/**
* @deprecated Use {@link Cursor#put(Object, Object, PutFlagSet)} instead. <hr> Store by cursor.
* <p>This function stores key/data pairs into the database.
* @param key key to store
* @param val data to store
* @param flags options for this operation
* @return true if the value was put, false if MDB_NOOVERWRITE or MDB_NODUPDATA were set and the
* key/value existed already.
*/
@Deprecated
public boolean put(final T key, final T val, final PutFlags... flags) {
return put(key, val, PutFlagSet.of(flags));
}
/**
* Store by cursor.
*
* <p>This function stores key/data pairs into the database.
*
* @param key key to store
* @param val data to store
* @return true if the value was put, false if MDB_NOOVERWRITE or MDB_NODUPDATA were set and the
* key/value existed already.
*/
public boolean put(final T key, final T val) {
return put(key, val, PutFlagSet.EMPTY);
}
/**
* Store by cursor.
*
* <p>This function stores key/data pairs into the database.
*
* @param key key to store
* @param val data to store
* @param flags options for this operation
* @return true if the value was put, false if MDB_NOOVERWRITE or MDB_NODUPDATA were set and the
* key/value existed already.
*/
public boolean put(final T key, final T val, final PutFlagSet flags) {
if (SHOULD_CHECK) {
requireNonNull(key);
requireNonNull(val);
requireNonNull(flags);
env.checkNotClosed();
checkNotClosed();
txn.checkReady();
txn.checkWritesAllowed();
}
final Pointer transientKey = kv.keyIn(key);
final Pointer transientVal = kv.valIn(val);
final int rc = LIB.mdb_cursor_put(ptrCursor, kv.pointerKey(), kv.pointerVal(), flags.getMask());
if (rc == MDB_KEYEXIST) {
if (flags.isSet(MDB_NOOVERWRITE)) {
kv.valOut(); // marked as in,out in LMDB C docs
} else if (!flags.isSet(MDB_NODUPDATA)) {
checkRc(rc);
}
return false;
}
checkRc(rc);
ReferenceUtil.reachabilityFence0(transientKey);
ReferenceUtil.reachabilityFence0(transientVal);
ReferenceUtil.reachabilityFence0(key);
ReferenceUtil.reachabilityFence0(val);
return true;
}
/**
* @deprecated Use {@link Cursor#put(Object, Object, PutFlagSet)} instead. <hr> Put multiple
* values into the database in one <code>MDB_MULTIPLE</code> operation.
* <p>The database must have been opened with {@link DbiFlags#MDB_DUPFIXED}. The buffer must
* contain fixed-sized values to be inserted. The size of each element is calculated from the
* buffer's size divided by the given element count. For example, to populate 10 X 4 byte
* integers at once, present a buffer of 40 bytes and specify the element as 10.
* @param key key to store in the database (not null)
* @param val value to store in the database (not null)
* @param elements number of elements contained in the passed value buffer
* @param flags options for operation (must set <code>MDB_MULTIPLE</code>)
*/
@Deprecated
public void putMultiple(final T key, final T val, final int elements, final PutFlags... flags) {
putMultiple(key, val, elements, PutFlagSet.of(flags));
}
/**
* Put multiple values into the database in one <code>MDB_MULTIPLE</code> operation.
*
* <p>The database must have been opened with {@link DbiFlags#MDB_DUPFIXED}. The buffer must
* contain fixed-sized values to be inserted. The size of each element is calculated from the
* buffer's size divided by the given element count. For example, to populate 10 X 4 byte integers
* at once, present a buffer of 40 bytes and specify the element as 10.
*
* @param key key to store in the database (not null)
* @param val value to store in the database (not null)
* @param elements number of elements contained in the passed value buffer
*/
public void putMultiple(final T key, final T val, final int elements) {
putMultiple(key, val, elements, PutFlagSet.EMPTY);
}
/**
* Put multiple values into the database in one <code>MDB_MULTIPLE</code> operation.
*
* <p>The database must have been opened with {@link DbiFlags#MDB_DUPFIXED}. The buffer must
* contain fixed-sized values to be inserted. The size of each element is calculated from the
* buffer's size divided by the given element count. For example, to populate 10 X 4 byte integers
* at once, present a buffer of 40 bytes and specify the element as 10.
*
* @param key key to store in the database (not null)
* @param val value to store in the database (not null)
* @param elements number of elements contained in the passed value buffer
* @param flags options for operation (must set <code>MDB_MULTIPLE</code>) Either a {@link
* PutFlagSet} or a single {@link PutFlags}.
*/
public void putMultiple(final T key, final T val, final int elements, final PutFlagSet flags) {
if (SHOULD_CHECK) {
requireNonNull(txn);
requireNonNull(key);
requireNonNull(val);
env.checkNotClosed();
txn.checkReady();
txn.checkWritesAllowed();
if (!flags.isSet(MDB_MULTIPLE)) {
throw new IllegalArgumentException("Must set " + MDB_MULTIPLE + " flag");
}
}
final Pointer transientKey = txn.kv().keyIn(key);
final Pointer dataPtr = txn.kv().valInMulti(val, elements);
final int rc = LIB.mdb_cursor_put(ptrCursor, txn.kv().pointerKey(), dataPtr, flags.getMask());
checkRc(rc);
ReferenceUtil.reachabilityFence0(transientKey);
ReferenceUtil.reachabilityFence0(dataPtr);
ReferenceUtil.reachabilityFence0(key);
ReferenceUtil.reachabilityFence0(val);
}
/**
* Renew a cursor handle.
*
* <p>A cursor is associated with a specific transaction and database. Cursors that are only used
* in read-only transactions may be re-used, to avoid unnecessary malloc/free overhead. The cursor
* may be associated with a new read-only transaction, and referencing the same database handle as
* it was created with. This may be done whether the previous transaction is live or dead.
*
* @param newTxn transaction handle
*/
public void renew(final Txn<T> newTxn) {
if (SHOULD_CHECK) {
requireNonNull(newTxn);
env.checkNotClosed();
checkNotClosed();
this.txn.checkReadOnly(); // existing
newTxn.checkReadOnly();
newTxn.checkReady();
}
checkRc(LIB.mdb_cursor_renew(newTxn.pointer(), ptrCursor));
this.txn = newTxn;
}
/**
* @deprecated Use {@link Cursor#reserve(Object, int, PutFlagSet)} instead. <hr> Reserve space for
* data of the given size, but don't copy the given val. Instead, return a pointer to the
* reserved space, which the caller can fill in later - before the next update operation or
* the transaction ends. This saves an extra memcpy if the data is being generated later. LMDB
* does nothing else with this memory, the caller is expected to modify all of the space
* requested.
* <p>This flag must not be specified if the database was opened with MDB_DUPSORT
* @param key key to store in the database (not null)
* @param size size of the value to be stored in the database (not null)
* @param flags options for this operation
* @return a buffer that can be used to modify the value
*/
@Deprecated
public T reserve(final T key, final int size, final PutFlags... flags) {
return reserve(key, size, PutFlagSet.of(flags));
}
/**
* Reserve space for data of the given size, but don't copy the given val. Instead, return a
* pointer to the reserved space, which the caller can fill in later - before the next update
* operation or the transaction ends. This saves an extra {@code memcpy} if the data is being
* generated later. LMDB does nothing else with this memory, the caller is expected to modify all
* the space requested.
*
* <p>This flag must not be specified if the database was opened with MDB_DUPSORT
*
* @param key key to store in the database (not null)
* @param size size of the value to be stored in the database (not null)
* @return a buffer that can be used to modify the value
*/
public T reserve(final T key, final int size) {
return reserve(key, size, PutFlagSet.EMPTY);
}
/**
* Reserve space for data of the given size, but don't copy the given val. Instead, return a
* pointer to the reserved space, which the caller can fill in later - before the next update
* operation or the transaction ends. This saves an extra memcpy if the data is being generated
* later. LMDB does nothing else with this memory, the caller is expected to modify all the space
* requested.
*
* <p>This flag must not be specified if the database was opened with MDB_DUPSORT
*
* @param key key to store in the database (not null)
* @param size size of the value to be stored in the database (not null)
* @param flags options for this operation
* @return a buffer that can be used to modify the value
*/
public T reserve(final T key, final int size, final PutFlagSet flags) {
if (SHOULD_CHECK) {
requireNonNull(key);
requireNonNull(flags);
env.checkNotClosed();
checkNotClosed();
txn.checkReady();
txn.checkWritesAllowed();
}
final Pointer transientKey = kv.keyIn(key);
final Pointer transientVal = kv.valIn(size);
// This is inconsistent with putMultiple which require MDB_MULTIPLE to be in the set.
final int flagsMask = flags.getMaskWith(MDB_RESERVE);
checkRc(LIB.mdb_cursor_put(ptrCursor, kv.pointerKey(), kv.pointerVal(), flagsMask));
kv.valOut();
ReferenceUtil.reachabilityFence0(transientKey);
ReferenceUtil.reachabilityFence0(transientVal);
ReferenceUtil.reachabilityFence0(key);
return val();
}
/**
* Reposition the key/value buffers based on the passed operation.
*
* @param op options for this operation
* @return false if requested position not found
*/
public boolean seek(final SeekOp op) {
if (SHOULD_CHECK) {
requireNonNull(op);
env.checkNotClosed();
checkNotClosed();
txn.checkReady();
}
final int rc = LIB.mdb_cursor_get(ptrCursor, kv.pointerKey(), kv.pointerVal(), op.getCode());
if (rc == MDB_NOTFOUND) {
return false;
}
checkRc(rc);
kv.keyOut();
kv.valOut();
return true;
}
/**
* Obtain the value.
*
* @return the value that the cursor is located at.
*/
public T val() {
return kv.val();
}
private void checkNotClosed() {
if (closed) {
throw new ClosedException();
}
}
/** Cursor has already been closed. */
public static final class ClosedException extends LmdbException {
private static final long serialVersionUID = 1L;
/** Creates a new instance. */
public ClosedException() {
super("Cursor has already been closed");
}
}
/** Cursor stack too deep - internal error. */
public static final class FullException extends LmdbNativeException {
static final int MDB_CURSOR_FULL = -30_787;
private static final long serialVersionUID = 1L;
FullException() {
super(MDB_CURSOR_FULL, "Cursor stack too deep - internal error");
}
}
}