forked from lmdbjava/lmdbjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbi.java
More file actions
470 lines (427 loc) · 14.9 KB
/
Dbi.java
File metadata and controls
470 lines (427 loc) · 14.9 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
/*-
* #%L
* LmdbJava
* %%
* Copyright (C) 2016 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.
* #L%
*/
package org.lmdbjava;
import static java.util.Objects.requireNonNull;
import static jnr.ffi.Memory.allocateDirect;
import static jnr.ffi.NativeType.ADDRESS;
import jnr.ffi.Pointer;
import jnr.ffi.byref.PointerByReference;
import org.lmdbjava.CursorIterator.IteratorType;
import static org.lmdbjava.CursorIterator.IteratorType.FORWARD;
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 org.lmdbjava.Library.MDB_stat;
import static org.lmdbjava.Library.RUNTIME;
import static org.lmdbjava.MaskedFlag.isSet;
import static org.lmdbjava.MaskedFlag.mask;
import static org.lmdbjava.PutFlags.MDB_NOOVERWRITE;
import static org.lmdbjava.PutFlags.MDB_RESERVE;
import static org.lmdbjava.ResultCodeMapper.checkRc;
/**
* LMDB Database.
*
* @param <T> buffer type
*/
public final class Dbi<T> {
private final Env<T> env;
private final String name;
private final Pointer ptr;
Dbi(final Env<T> env, final Txn<T> txn, final String name,
final DbiFlags... flags) {
this.env = env;
this.name = name;
final int flagsMask = mask(flags);
final Pointer dbiPtr = allocateDirect(RUNTIME, ADDRESS);
checkRc(LIB.mdb_dbi_open(txn.pointer(), name, flagsMask, dbiPtr));
ptr = dbiPtr.getPointer(0);
}
/**
* Close the database handle (normally unnecessary; use with caution).
*
* <p>
* It is very rare that closing a database handle is useful. There are also
* many warnings/restrictions if closing a database handle (refer to the LMDB
* C documentation). As such this is non-routine usage and this class does not
* track the open/closed state of the {@link Dbi}. Advanced users are expected
* to have specific reasons for using this method and will manage their own
* state accordingly.
*/
public void close() {
LIB.mdb_dbi_close(env.pointer(), ptr);
}
/**
* Starts a new read-write transaction and deletes the key.
*
* @param key key to delete from the database (not null)
* @see #delete(org.lmdbjava.Txn, java.lang.Object, java.lang.Object)
*/
public void delete(final T key) {
try (Txn<T> txn = env.txnWrite()) {
delete(txn, key);
txn.commit();
}
}
/**
* Deletes the key using the passed transaction.
*
* @param txn transaction handle (not null; not committed; must be R-W)
* @param key key to delete from the database (not null)
* @see #delete(org.lmdbjava.Txn, java.lang.Object, java.lang.Object)
*/
public void delete(final Txn<T> txn, final T key) {
delete(txn, key, null);
}
/**
* Removes key/data pairs from the database.
*
* <p>
* If the database does not support sorted duplicate data items
* ({@link DbiFlags#MDB_DUPSORT}) the value parameter is ignored. If the
* database supports sorted duplicates and the value parameter is null, all of
* the duplicate data items for the key will be deleted. Otherwise, if the
* data parameter is non-null only the matching data item will be deleted.
*
* <p>
* This function will throw {@link KeyNotFoundException} if the key/data pair
* is not found.
*
* @param txn transaction handle (not null; not committed; must be R-W)
* @param key key to delete from the database (not null)
* @param val value to delete from the database (null permitted)
*/
public void delete(final Txn<T> txn, final T key, final T val) {
if (SHOULD_CHECK) {
requireNonNull(txn);
requireNonNull(key);
txn.checkReady();
txn.checkWritesAllowed();
}
txn.keyIn(key);
if (val == null) {
checkRc(LIB.mdb_del(txn.pointer(), ptr, txn.pointerKey(), null));
} else {
txn.valIn(val);
checkRc(LIB
.mdb_del(txn.pointer(), ptr, txn.pointerKey(), txn.pointerVal()));
}
}
/**
* Drops the data in this database, leaving the database open for further use.
*
* <p>
* This method slightly differs from the LMDB C API in that it does not
* provide support for also closing the DB handle. If closing the DB handle is
* required, please see {@link #close()}.
*
* @param txn transaction handle (not null; not committed; must be R-W)
*/
public void drop(final Txn<T> txn) {
if (SHOULD_CHECK) {
requireNonNull(txn);
txn.checkReady();
txn.checkWritesAllowed();
}
checkRc(LIB.mdb_drop(txn.pointer(), ptr, 0));
}
/**
* Get items from a database, moving the {@link Txn#val()} to the value.
*
* <p>
* This function retrieves key/data pairs from the database. The address and
* length of the data associated with the specified \b key are returned in the
* structure to which \b data refers. If the database supports duplicate keys
* ({@link org.lmdbjava.DbiFlags#MDB_DUPSORT}) then the first data item for
* the key will be returned. Retrieval of other items requires the use of
* #mdb_cursor_get().
*
* @param txn transaction handle (not null; not committed)
* @param key key to search for in the database (not null)
* @return the data or null if not found
*/
public T get(final Txn<T> txn, final T key) {
if (SHOULD_CHECK) {
requireNonNull(txn);
requireNonNull(key);
txn.checkReady();
}
txn.keyIn(key);
final int rc = LIB.mdb_get(txn.pointer(), ptr, txn.pointerKey(), txn.
pointerVal());
if (rc == MDB_NOTFOUND) {
return null;
}
checkRc(rc);
return txn.valOut(); // marked as out in LMDB C docs
}
/**
* Obtains the name of this database.
*
* @return the name (may be null or empty)
*/
public String getName() {
return name;
}
/**
* Iterate the database from the first item and forwards.
*
* @param txn transaction handle (not null; not committed)
* @return iterator
*/
public CursorIterator<T> iterate(final Txn<T> txn) {
return iterate(txn, null, FORWARD);
}
/**
* Iterate the database from the first/last item and forwards/backwards.
*
* @param txn transaction handle (not null; not committed)
* @param type direction of iterator
* @return iterator
*/
public CursorIterator<T> iterate(final Txn<T> txn, final IteratorType type) {
return iterate(txn, null, type);
}
/**
* Iterate the database from the first/last item and forwards/backwards by
* first seeking to the provided key.
*
* @param txn transaction handle (not null; not committed)
* @param key the key to search from.
* @param type direction of iterator
* @return iterator
*/
public CursorIterator<T> iterate(final Txn<T> txn, final T key,
final IteratorType type) {
if (SHOULD_CHECK) {
requireNonNull(txn);
txn.checkReady();
}
return new CursorIterator<>(openCursor(txn), key, type);
}
/**
* Create a cursor handle.
*
* <p>
* A cursor is associated with a specific transaction and database. A cursor
* cannot be used when its database handle is closed. Nor when its transaction
* has ended, except with {@link Cursor#renew(org.lmdbjava.Txn)}. It can be
* discarded with {@link Cursor#close()}. A cursor in a write-transaction can
* be closed before its transaction ends, and will otherwise be closed when
* its transaction ends. A cursor in a read-only transaction must be closed
* explicitly, before or after its transaction ends. It can be reused with
* {@link Cursor#renew(org.lmdbjava.Txn)} before finally closing it.
*
* @param txn transaction handle (not null; not committed)
* @return cursor handle
*/
public Cursor<T> openCursor(final Txn<T> txn) {
if (SHOULD_CHECK) {
requireNonNull(txn);
txn.checkReady();
}
final PointerByReference cursorPtr = new PointerByReference();
checkRc(LIB.mdb_cursor_open(txn.pointer(), ptr, cursorPtr));
return new Cursor<>(cursorPtr.getValue(), txn);
}
/**
* Starts a new read-write transaction and puts the key/data pair.
*
* @param key key to store in the database (not null)
* @param val value to store in the database (not null)
* @see #put(org.lmdbjava.Txn, java.lang.Object, java.lang.Object,
* org.lmdbjava.PutFlags...)
*/
public void put(final T key, final T val) {
try (Txn<T> txn = env.txnWrite()) {
put(txn, key, val);
txn.commit();
}
}
/**
* Store a key/value pair in the database.
*
* <p>
* This function stores key/data pairs in the database. The default behavior
* is to enter the new key/data pair, replacing any previously existing key if
* duplicates are disallowed, or adding a duplicate data item if duplicates
* are allowed ({@link DbiFlags#MDB_DUPSORT}).
*
* @param txn transaction handle (not null; not committed; must be R-W)
* @param key key to store in the database (not null)
* @param val value to store in the database (not null)
* @param flags Special options for this operation
*/
public void put(final Txn<T> txn, final T key, final T val,
final PutFlags... flags) {
if (SHOULD_CHECK) {
requireNonNull(txn);
requireNonNull(key);
requireNonNull(val);
txn.checkReady();
txn.checkWritesAllowed();
}
txn.keyIn(key);
txn.valIn(val);
final int mask = mask(flags);
final int rc = LIB.mdb_put(txn.pointer(), ptr, txn.pointerKey(), txn
.pointerVal(), mask);
if (rc == MDB_KEYEXIST && isSet(mask, MDB_NOOVERWRITE)) {
txn.valOut(); // marked as in,out in LMDB C docs
}
checkRc(rc);
}
/**
* 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 txn transaction handle (not null; not committed; must be R-W)
* @param key key to store in the database (not null)
* @param size size of the value to be stored in the database
* @param op options for this operation
* @return a buffer that can be used to modify the value
*/
public T reserve(final Txn<T> txn, final T key, final int size,
final PutFlags... op) {
if (SHOULD_CHECK) {
requireNonNull(txn);
requireNonNull(key);
txn.checkReady();
txn.checkWritesAllowed();
}
txn.keyIn(key);
txn.valIn(size);
final int flags = mask(op) | MDB_RESERVE.getMask();
checkRc(LIB.mdb_put(txn.pointer(), ptr, txn.pointerKey(), txn.pointerVal(),
flags));
txn.valOut(); // marked as in,out in LMDB C docs
return txn.val();
}
/**
* Return statistics about this database.
*
* @param txn transaction handle (not null; not committed)
* @return an immutable statistics object.
*/
public Stat stat(final Txn<T> txn) {
if (SHOULD_CHECK) {
requireNonNull(txn);
txn.checkReady();
}
final MDB_stat stat = new MDB_stat(RUNTIME);
checkRc(LIB.mdb_stat(txn.pointer(), ptr, stat));
return new Stat(
stat.f0_ms_psize.intValue(),
stat.f1_ms_depth.intValue(),
stat.f2_ms_branch_pages.longValue(),
stat.f3_ms_leaf_pages.longValue(),
stat.f4_ms_overflow_pages.longValue(),
stat.f5_ms_entries.longValue());
}
/**
* The specified DBI was changed unexpectedly.
*/
public static final class BadDbiException extends LmdbNativeException {
static final int MDB_BAD_DBI = -30_780;
private static final long serialVersionUID = 1L;
BadDbiException() {
super(MDB_BAD_DBI, "The specified DBI was changed unexpectedly");
}
}
/**
* Unsupported size of key/DB name/data, or wrong DUPFIXED size.
*/
public static final class BadValueSizeException extends LmdbNativeException {
static final int MDB_BAD_VALSIZE = -30_781;
private static final long serialVersionUID = 1L;
BadValueSizeException() {
super(MDB_BAD_VALSIZE,
"Unsupported size of key/DB name/data, or wrong DUPFIXED size");
}
}
/**
* Environment maxdbs reached.
*/
public static final class DbFullException extends LmdbNativeException {
static final int MDB_DBS_FULL = -30_791;
private static final long serialVersionUID = 1L;
DbFullException() {
super(MDB_DBS_FULL, "Environment maxdbs reached");
}
}
/**
* Operation and DB incompatible, or DB type changed.
*
* <p>
* This can mean:
* <ul>
* <li>The operation expects an MDB_DUPSORT / MDB_DUPFIXED database.</li>
* <li>Opening a named DB when the unnamed DB has MDB_DUPSORT /
* MDB_INTEGERKEY.</li>
* <li>Accessing a data record as a database, or vice versa.</li>
* <li>The database was dropped and recreated with different flags.</li>
* </ul>
*/
public static final class IncompatibleException extends LmdbNativeException {
static final int MDB_INCOMPATIBLE = -30_784;
private static final long serialVersionUID = 1L;
IncompatibleException() {
super(MDB_INCOMPATIBLE,
"Operation and DB incompatible, or DB type changed");
}
}
/**
* Key/data pair already exists.
*/
public static final class KeyExistsException extends LmdbNativeException {
static final int MDB_KEYEXIST = -30_799;
private static final long serialVersionUID = 1L;
KeyExistsException() {
super(MDB_KEYEXIST, "key/data pair already exists");
}
}
/**
* Key/data pair not found (EOF).
*/
public static final class KeyNotFoundException extends LmdbNativeException {
static final int MDB_NOTFOUND = -30_798;
private static final long serialVersionUID = 1L;
KeyNotFoundException() {
super(MDB_NOTFOUND, "key/data pair not found (EOF)");
}
}
/**
* Database contents grew beyond environment mapsize.
*/
public static final class MapResizedException extends LmdbNativeException {
static final int MDB_MAP_RESIZED = -30_785;
private static final long serialVersionUID = 1L;
MapResizedException() {
super(MDB_MAP_RESIZED, "Database contents grew beyond environment mapsize");
}
}
}