1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
8 import static java
.nio
.charset
.StandardCharsets
.UTF_8
;
10 import java
.io
.IOException
;
11 import java
.nio
.ByteBuffer
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Arrays
;
14 import java
.util
.List
;
16 import java
.util
.concurrent
.atomic
.AtomicReference
;
17 import org
.rocksdb
.util
.Environment
;
20 * A RocksDB is a persistent ordered map from keys to values. It is safe for
21 * concurrent access from multiple threads without any external synchronization.
22 * All methods of this class could potentially throw RocksDBException, which
23 * indicates sth wrong at the RocksDB library side and the call failed.
25 public class RocksDB
extends RocksObject
{
26 public static final byte[] DEFAULT_COLUMN_FAMILY
= "default".getBytes(UTF_8
);
27 public static final int NOT_FOUND
= -1;
29 private enum LibraryState
{
35 private static final AtomicReference
<LibraryState
> libraryLoaded
=
36 new AtomicReference
<>(LibraryState
.NOT_LOADED
);
39 RocksDB
.loadLibrary();
42 private final List
<ColumnFamilyHandle
> ownedColumnFamilyHandles
= new ArrayList
<>();
45 * Loads the necessary library files.
46 * Calling this method twice will have no effect.
47 * By default the method extracts the shared library for loading at
48 * java.io.tmpdir, however, you can override this temporary location by
49 * setting the environment variable ROCKSDB_SHAREDLIB_DIR.
51 public static void loadLibrary() {
52 if (libraryLoaded
.get() == LibraryState
.LOADED
) {
56 if (libraryLoaded
.compareAndSet(LibraryState
.NOT_LOADED
,
57 LibraryState
.LOADING
)) {
58 final String tmpDir
= System
.getenv("ROCKSDB_SHAREDLIB_DIR");
59 // loading possibly necessary libraries.
60 for (final CompressionType compressionType
: CompressionType
.values()) {
62 if (compressionType
.getLibraryName() != null) {
63 System
.loadLibrary(compressionType
.getLibraryName());
65 } catch (final UnsatisfiedLinkError e
) {
66 // since it may be optional, we ignore its loading failure here.
70 NativeLibraryLoader
.getInstance().loadLibrary(tmpDir
);
71 } catch (final IOException e
) {
72 libraryLoaded
.set(LibraryState
.NOT_LOADED
);
73 throw new RuntimeException("Unable to load the RocksDB shared library",
77 final int encodedVersion
= version();
78 version
= Version
.fromEncodedVersion(encodedVersion
);
80 libraryLoaded
.set(LibraryState
.LOADED
);
84 while (libraryLoaded
.get() == LibraryState
.LOADING
) {
87 } catch(final InterruptedException e
) {
94 * Tries to load the necessary library files from the given list of
97 * @param paths a list of strings where each describes a directory
100 public static void loadLibrary(final List
<String
> paths
) {
101 if (libraryLoaded
.get() == LibraryState
.LOADED
) {
105 if (libraryLoaded
.compareAndSet(LibraryState
.NOT_LOADED
,
106 LibraryState
.LOADING
)) {
107 for (final CompressionType compressionType
: CompressionType
.values()) {
108 if (compressionType
.equals(CompressionType
.NO_COMPRESSION
)) {
111 for (final String path
: paths
) {
113 System
.load(path
+ "/" + Environment
.getSharedLibraryFileName(
114 compressionType
.getLibraryName()));
116 } catch (final UnsatisfiedLinkError e
) {
117 // since they are optional, we ignore loading fails.
121 boolean success
= false;
122 UnsatisfiedLinkError err
= null;
123 for (final String path
: paths
) {
125 System
.load(path
+ "/" +
126 Environment
.getJniLibraryFileName("rocksdbjni"));
129 } catch (final UnsatisfiedLinkError e
) {
134 libraryLoaded
.set(LibraryState
.NOT_LOADED
);
138 final int encodedVersion
= version();
139 version
= Version
.fromEncodedVersion(encodedVersion
);
141 libraryLoaded
.set(LibraryState
.LOADED
);
145 while (libraryLoaded
.get() == LibraryState
.LOADING
) {
148 } catch(final InterruptedException e
) {
154 public static Version
rocksdbVersion() {
159 * Private constructor.
161 * @param nativeHandle The native handle of the C++ RocksDB object
163 protected RocksDB(final long nativeHandle
) {
168 * The factory constructor of RocksDB that opens a RocksDB instance given
169 * the path to the database using the default options w/ createIfMissing
172 * @param path the path to the rocksdb.
173 * @return a {@link RocksDB} instance on success, null if the specified
174 * {@link RocksDB} can not be opened.
176 * @throws RocksDBException thrown if error happens in underlying
178 * @see Options#setCreateIfMissing(boolean)
180 public static RocksDB
open(final String path
) throws RocksDBException
{
181 final Options options
= new Options();
182 options
.setCreateIfMissing(true);
183 return open(options
, path
);
187 * The factory constructor of RocksDB that opens a RocksDB instance given
188 * the path to the database using the specified options and db path and a list
189 * of column family names.
191 * If opened in read write mode every existing column family name must be
192 * passed within the list to this method.</p>
194 * If opened in read-only mode only a subset of existing column families must
195 * be passed to this method.</p>
197 * Options instance *should* not be disposed before all DBs using this options
198 * instance have been closed. If user doesn't call options dispose explicitly,
199 * then this options instance will be GC'd automatically</p>
201 * ColumnFamily handles are disposed when the RocksDB instance is disposed.
204 * @param path the path to the rocksdb.
205 * @param columnFamilyDescriptors list of column family descriptors
206 * @param columnFamilyHandles will be filled with ColumnFamilyHandle instances
208 * @return a {@link RocksDB} instance on success, null if the specified
209 * {@link RocksDB} can not be opened.
211 * @throws RocksDBException thrown if error happens in underlying
213 * @see DBOptions#setCreateIfMissing(boolean)
215 public static RocksDB
open(final String path
,
216 final List
<ColumnFamilyDescriptor
> columnFamilyDescriptors
,
217 final List
<ColumnFamilyHandle
> columnFamilyHandles
)
218 throws RocksDBException
{
219 final DBOptions options
= new DBOptions();
220 return open(options
, path
, columnFamilyDescriptors
, columnFamilyHandles
);
224 * The factory constructor of RocksDB that opens a RocksDB instance given
225 * the path to the database using the specified options and db path.
228 * Options instance *should* not be disposed before all DBs using this options
229 * instance have been closed. If user doesn't call options dispose explicitly,
230 * then this options instance will be GC'd automatically.</p>
232 * Options instance can be re-used to open multiple DBs if DB statistics is
233 * not used. If DB statistics are required, then its recommended to open DB
234 * with new Options instance as underlying native statistics instance does not
235 * use any locks to prevent concurrent updates.</p>
237 * @param options {@link org.rocksdb.Options} instance.
238 * @param path the path to the rocksdb.
239 * @return a {@link RocksDB} instance on success, null if the specified
240 * {@link RocksDB} can not be opened.
242 * @throws RocksDBException thrown if error happens in underlying
245 * @see Options#setCreateIfMissing(boolean)
247 public static RocksDB
open(final Options options
, final String path
)
248 throws RocksDBException
{
249 // when non-default Options is used, keeping an Options reference
250 // in RocksDB can prevent Java to GC during the life-time of
251 // the currently-created RocksDB.
252 final RocksDB db
= new RocksDB(open(options
.nativeHandle_
, path
));
253 db
.storeOptionsInstance(options
);
258 * The factory constructor of RocksDB that opens a RocksDB instance given
259 * the path to the database using the specified options and db path and a list
260 * of column family names.
262 * If opened in read write mode every existing column family name must be
263 * passed within the list to this method.</p>
265 * If opened in read-only mode only a subset of existing column families must
266 * be passed to this method.</p>
268 * Options instance *should* not be disposed before all DBs using this options
269 * instance have been closed. If user doesn't call options dispose explicitly,
270 * then this options instance will be GC'd automatically.</p>
272 * Options instance can be re-used to open multiple DBs if DB statistics is
273 * not used. If DB statistics are required, then its recommended to open DB
274 * with new Options instance as underlying native statistics instance does not
275 * use any locks to prevent concurrent updates.</p>
277 * ColumnFamily handles are disposed when the RocksDB instance is disposed.
280 * @param options {@link org.rocksdb.DBOptions} instance.
281 * @param path the path to the rocksdb.
282 * @param columnFamilyDescriptors list of column family descriptors
283 * @param columnFamilyHandles will be filled with ColumnFamilyHandle instances
285 * @return a {@link RocksDB} instance on success, null if the specified
286 * {@link RocksDB} can not be opened.
288 * @throws RocksDBException thrown if error happens in underlying
291 * @see DBOptions#setCreateIfMissing(boolean)
293 public static RocksDB
open(final DBOptions options
, final String path
,
294 final List
<ColumnFamilyDescriptor
> columnFamilyDescriptors
,
295 final List
<ColumnFamilyHandle
> columnFamilyHandles
)
296 throws RocksDBException
{
298 final byte[][] cfNames
= new byte[columnFamilyDescriptors
.size()][];
299 final long[] cfOptionHandles
= new long[columnFamilyDescriptors
.size()];
300 for (int i
= 0; i
< columnFamilyDescriptors
.size(); i
++) {
301 final ColumnFamilyDescriptor cfDescriptor
= columnFamilyDescriptors
303 cfNames
[i
] = cfDescriptor
.getName();
304 cfOptionHandles
[i
] = cfDescriptor
.getOptions().nativeHandle_
;
307 final long[] handles
= open(options
.nativeHandle_
, path
, cfNames
,
309 final RocksDB db
= new RocksDB(handles
[0]);
310 db
.storeOptionsInstance(options
);
312 for (int i
= 1; i
< handles
.length
; i
++) {
313 final ColumnFamilyHandle columnFamilyHandle
= new ColumnFamilyHandle(db
, handles
[i
]);
314 columnFamilyHandles
.add(columnFamilyHandle
);
317 db
.ownedColumnFamilyHandles
.addAll(columnFamilyHandles
);
323 * The factory constructor of RocksDB that opens a RocksDB instance in
324 * Read-Only mode given the path to the database using the default
327 * @param path the path to the RocksDB.
328 * @return a {@link RocksDB} instance on success, null if the specified
329 * {@link RocksDB} can not be opened.
331 * @throws RocksDBException thrown if error happens in underlying
334 public static RocksDB
openReadOnly(final String path
)
335 throws RocksDBException
{
336 // This allows to use the rocksjni default Options instead of
338 final Options options
= new Options();
339 return openReadOnly(options
, path
);
343 * The factory constructor of RocksDB that opens a RocksDB instance in
344 * Read-Only mode given the path to the database using the specified
345 * options and db path.
347 * Options instance *should* not be disposed before all DBs using this options
348 * instance have been closed. If user doesn't call options dispose explicitly,
349 * then this options instance will be GC'd automatically.
351 * @param options {@link Options} instance.
352 * @param path the path to the RocksDB.
353 * @return a {@link RocksDB} instance on success, null if the specified
354 * {@link RocksDB} can not be opened.
356 * @throws RocksDBException thrown if error happens in underlying
359 public static RocksDB
openReadOnly(final Options options
, final String path
)
360 throws RocksDBException
{
361 return openReadOnly(options
, path
, false);
365 * The factory constructor of RocksDB that opens a RocksDB instance in
366 * Read-Only mode given the path to the database using the specified
367 * options and db path.
369 * Options instance *should* not be disposed before all DBs using this options
370 * instance have been closed. If user doesn't call options dispose explicitly,
371 * then this options instance will be GC'd automatically.
373 * @param options {@link Options} instance.
374 * @param path the path to the RocksDB.
375 * @param errorIfWalFileExists true to raise an error when opening the db
376 * if a Write Ahead Log file exists, false otherwise.
377 * @return a {@link RocksDB} instance on success, null if the specified
378 * {@link RocksDB} can not be opened.
380 * @throws RocksDBException thrown if error happens in underlying
383 public static RocksDB
openReadOnly(final Options options
, final String path
,
384 final boolean errorIfWalFileExists
) throws RocksDBException
{
385 // when non-default Options is used, keeping an Options reference
386 // in RocksDB can prevent Java to GC during the life-time of
387 // the currently-created RocksDB.
388 final RocksDB db
= new RocksDB(openROnly(options
.nativeHandle_
, path
, errorIfWalFileExists
));
389 db
.storeOptionsInstance(options
);
394 * The factory constructor of RocksDB that opens a RocksDB instance in
395 * Read-Only mode given the path to the database using the default
398 * @param path the path to the RocksDB.
399 * @param columnFamilyDescriptors list of column family descriptors
400 * @param columnFamilyHandles will be filled with ColumnFamilyHandle instances
402 * @return a {@link RocksDB} instance on success, null if the specified
403 * {@link RocksDB} can not be opened.
405 * @throws RocksDBException thrown if error happens in underlying
408 public static RocksDB
openReadOnly(final String path
,
409 final List
<ColumnFamilyDescriptor
> columnFamilyDescriptors
,
410 final List
<ColumnFamilyHandle
> columnFamilyHandles
)
411 throws RocksDBException
{
412 // This allows to use the rocksjni default Options instead of
414 final DBOptions options
= new DBOptions();
415 return openReadOnly(options
, path
, columnFamilyDescriptors
, columnFamilyHandles
, false);
419 * The factory constructor of RocksDB that opens a RocksDB instance in
420 * Read-Only mode given the path to the database using the specified
421 * options and db path.
423 * <p>This open method allows to open RocksDB using a subset of available
424 * column families</p>
425 * <p>Options instance *should* not be disposed before all DBs using this
426 * options instance have been closed. If user doesn't call options dispose
427 * explicitly,then this options instance will be GC'd automatically.</p>
429 * @param options {@link DBOptions} instance.
430 * @param path the path to the RocksDB.
431 * @param columnFamilyDescriptors list of column family descriptors
432 * @param columnFamilyHandles will be filled with ColumnFamilyHandle instances
434 * @return a {@link RocksDB} instance on success, null if the specified
435 * {@link RocksDB} can not be opened.
437 * @throws RocksDBException thrown if error happens in underlying
440 public static RocksDB
openReadOnly(final DBOptions options
, final String path
,
441 final List
<ColumnFamilyDescriptor
> columnFamilyDescriptors
,
442 final List
<ColumnFamilyHandle
> columnFamilyHandles
) throws RocksDBException
{
443 return openReadOnly(options
, path
, columnFamilyDescriptors
, columnFamilyHandles
, false);
447 * The factory constructor of RocksDB that opens a RocksDB instance in
448 * Read-Only mode given the path to the database using the specified
449 * options and db path.
451 * <p>This open method allows to open RocksDB using a subset of available
452 * column families</p>
453 * <p>Options instance *should* not be disposed before all DBs using this
454 * options instance have been closed. If user doesn't call options dispose
455 * explicitly,then this options instance will be GC'd automatically.</p>
457 * @param options {@link DBOptions} instance.
458 * @param path the path to the RocksDB.
459 * @param columnFamilyDescriptors list of column family descriptors
460 * @param columnFamilyHandles will be filled with ColumnFamilyHandle instances
462 * @param errorIfWalFileExists true to raise an error when opening the db
463 * if a Write Ahead Log file exists, false otherwise.
464 * @return a {@link RocksDB} instance on success, null if the specified
465 * {@link RocksDB} can not be opened.
467 * @throws RocksDBException thrown if error happens in underlying
470 public static RocksDB
openReadOnly(final DBOptions options
, final String path
,
471 final List
<ColumnFamilyDescriptor
> columnFamilyDescriptors
,
472 final List
<ColumnFamilyHandle
> columnFamilyHandles
, final boolean errorIfWalFileExists
)
473 throws RocksDBException
{
474 // when non-default Options is used, keeping an Options reference
475 // in RocksDB can prevent Java to GC during the life-time of
476 // the currently-created RocksDB.
478 final byte[][] cfNames
= new byte[columnFamilyDescriptors
.size()][];
479 final long[] cfOptionHandles
= new long[columnFamilyDescriptors
.size()];
480 for (int i
= 0; i
< columnFamilyDescriptors
.size(); i
++) {
481 final ColumnFamilyDescriptor cfDescriptor
= columnFamilyDescriptors
483 cfNames
[i
] = cfDescriptor
.getName();
484 cfOptionHandles
[i
] = cfDescriptor
.getOptions().nativeHandle_
;
487 final long[] handles
=
488 openROnly(options
.nativeHandle_
, path
, cfNames
, cfOptionHandles
, errorIfWalFileExists
);
489 final RocksDB db
= new RocksDB(handles
[0]);
490 db
.storeOptionsInstance(options
);
492 for (int i
= 1; i
< handles
.length
; i
++) {
493 final ColumnFamilyHandle columnFamilyHandle
= new ColumnFamilyHandle(db
, handles
[i
]);
494 columnFamilyHandles
.add(columnFamilyHandle
);
497 db
.ownedColumnFamilyHandles
.addAll(columnFamilyHandles
);
503 * Open DB as secondary instance with only the default column family.
505 * The secondary instance can dynamically tail the MANIFEST of
506 * a primary that must have already been created. User can call
507 * {@link #tryCatchUpWithPrimary()} to make the secondary instance catch up
508 * with primary (WAL tailing is NOT supported now) whenever the user feels
509 * necessary. Column families created by the primary after the secondary
510 * instance starts are currently ignored by the secondary instance.
511 * Column families opened by secondary and dropped by the primary will be
512 * dropped by secondary as well. However the user of the secondary instance
513 * can still access the data of such dropped column family as long as they
514 * do not destroy the corresponding column family handle.
515 * WAL tailing is not supported at present, but will arrive soon.
517 * @param options the options to open the secondary instance.
518 * @param path the path to the primary RocksDB instance.
519 * @param secondaryPath points to a directory where the secondary instance
520 * stores its info log
522 * @return a {@link RocksDB} instance on success, null if the specified
523 * {@link RocksDB} can not be opened.
525 * @throws RocksDBException thrown if error happens in underlying
528 public static RocksDB
openAsSecondary(final Options options
, final String path
,
529 final String secondaryPath
) throws RocksDBException
{
530 // when non-default Options is used, keeping an Options reference
531 // in RocksDB can prevent Java to GC during the life-time of
532 // the currently-created RocksDB.
533 final RocksDB db
= new RocksDB(openAsSecondary(options
.nativeHandle_
, path
, secondaryPath
));
534 db
.storeOptionsInstance(options
);
539 * Open DB as secondary instance with column families.
540 * You can open a subset of column families in secondary mode.
542 * The secondary instance can dynamically tail the MANIFEST of
543 * a primary that must have already been created. User can call
544 * {@link #tryCatchUpWithPrimary()} to make the secondary instance catch up
545 * with primary (WAL tailing is NOT supported now) whenever the user feels
546 * necessary. Column families created by the primary after the secondary
547 * instance starts are currently ignored by the secondary instance.
548 * Column families opened by secondary and dropped by the primary will be
549 * dropped by secondary as well. However the user of the secondary instance
550 * can still access the data of such dropped column family as long as they
551 * do not destroy the corresponding column family handle.
552 * WAL tailing is not supported at present, but will arrive soon.
554 * @param options the options to open the secondary instance.
555 * @param path the path to the primary RocksDB instance.
556 * @param secondaryPath points to a directory where the secondary instance
557 * stores its info log.
558 * @param columnFamilyDescriptors list of column family descriptors
559 * @param columnFamilyHandles will be filled with ColumnFamilyHandle instances
562 * @return a {@link RocksDB} instance on success, null if the specified
563 * {@link RocksDB} can not be opened.
565 * @throws RocksDBException thrown if error happens in underlying
568 public static RocksDB
openAsSecondary(final DBOptions options
, final String path
,
569 final String secondaryPath
, final List
<ColumnFamilyDescriptor
> columnFamilyDescriptors
,
570 final List
<ColumnFamilyHandle
> columnFamilyHandles
) throws RocksDBException
{
571 // when non-default Options is used, keeping an Options reference
572 // in RocksDB can prevent Java to GC during the life-time of
573 // the currently-created RocksDB.
575 final byte[][] cfNames
= new byte[columnFamilyDescriptors
.size()][];
576 final long[] cfOptionHandles
= new long[columnFamilyDescriptors
.size()];
577 for (int i
= 0; i
< columnFamilyDescriptors
.size(); i
++) {
578 final ColumnFamilyDescriptor cfDescriptor
= columnFamilyDescriptors
.get(i
);
579 cfNames
[i
] = cfDescriptor
.getName();
580 cfOptionHandles
[i
] = cfDescriptor
.getOptions().nativeHandle_
;
583 final long[] handles
=
584 openAsSecondary(options
.nativeHandle_
, path
, secondaryPath
, cfNames
, cfOptionHandles
);
585 final RocksDB db
= new RocksDB(handles
[0]);
586 db
.storeOptionsInstance(options
);
588 for (int i
= 1; i
< handles
.length
; i
++) {
589 final ColumnFamilyHandle columnFamilyHandle
= new ColumnFamilyHandle(db
, handles
[i
]);
590 columnFamilyHandles
.add(columnFamilyHandle
);
593 db
.ownedColumnFamilyHandles
.addAll(columnFamilyHandles
);
599 * This is similar to {@link #close()} except that it
600 * throws an exception if any error occurs.
602 * This will not fsync the WAL files.
603 * If syncing is required, the caller must first call {@link #syncWal()}
604 * or {@link #write(WriteOptions, WriteBatch)} using an empty write batch
605 * with {@link WriteOptions#setSync(boolean)} set to true.
607 * See also {@link #close()}.
609 * @throws RocksDBException if an error occurs whilst closing.
611 public void closeE() throws RocksDBException
{
612 for (final ColumnFamilyHandle columnFamilyHandle
: ownedColumnFamilyHandles
) {
613 columnFamilyHandle
.close();
615 ownedColumnFamilyHandles
.clear();
617 if (owningHandle_
.compareAndSet(true, false)) {
619 closeDatabase(nativeHandle_
);
627 * This is similar to {@link #closeE()} except that it
628 * silently ignores any errors.
630 * This will not fsync the WAL files.
631 * If syncing is required, the caller must first call {@link #syncWal()}
632 * or {@link #write(WriteOptions, WriteBatch)} using an empty write batch
633 * with {@link WriteOptions#setSync(boolean)} set to true.
635 * See also {@link #close()}.
638 public void close() {
639 for (final ColumnFamilyHandle columnFamilyHandle
: ownedColumnFamilyHandles
) {
640 columnFamilyHandle
.close();
642 ownedColumnFamilyHandles
.clear();
644 if (owningHandle_
.compareAndSet(true, false)) {
646 closeDatabase(nativeHandle_
);
647 } catch (final RocksDBException e
) {
648 // silently ignore the error report
656 * Static method to determine all available column families for a
657 * rocksdb database identified by path
659 * @param options Options for opening the database
660 * @param path Absolute path to rocksdb database
661 * @return List<byte[]> List containing the column family names
663 * @throws RocksDBException thrown if error happens in underlying
666 public static List
<byte[]> listColumnFamilies(final Options options
,
667 final String path
) throws RocksDBException
{
668 return Arrays
.asList(RocksDB
.listColumnFamilies(options
.nativeHandle_
,
673 * Creates a new column family with the name columnFamilyName and
674 * allocates a ColumnFamilyHandle within an internal structure.
675 * The ColumnFamilyHandle is automatically disposed with DB disposal.
677 * @param columnFamilyDescriptor column family to be created.
678 * @return {@link org.rocksdb.ColumnFamilyHandle} instance.
680 * @throws RocksDBException thrown if error happens in underlying
683 public ColumnFamilyHandle
createColumnFamily(
684 final ColumnFamilyDescriptor columnFamilyDescriptor
)
685 throws RocksDBException
{
686 final ColumnFamilyHandle columnFamilyHandle
= new ColumnFamilyHandle(this,
687 createColumnFamily(nativeHandle_
, columnFamilyDescriptor
.getName(),
688 columnFamilyDescriptor
.getName().length
,
689 columnFamilyDescriptor
.getOptions().nativeHandle_
));
690 ownedColumnFamilyHandles
.add(columnFamilyHandle
);
691 return columnFamilyHandle
;
695 * Bulk create column families with the same column family options.
697 * @param columnFamilyOptions the options for the column families.
698 * @param columnFamilyNames the names of the column families.
700 * @return the handles to the newly created column families.
702 * @throws RocksDBException if an error occurs whilst creating
703 * the column families
705 public List
<ColumnFamilyHandle
> createColumnFamilies(
706 final ColumnFamilyOptions columnFamilyOptions
,
707 final List
<byte[]> columnFamilyNames
) throws RocksDBException
{
708 final byte[][] cfNames
= columnFamilyNames
.toArray(
710 final long[] cfHandles
= createColumnFamilies(nativeHandle_
,
711 columnFamilyOptions
.nativeHandle_
, cfNames
);
712 final List
<ColumnFamilyHandle
> columnFamilyHandles
=
713 new ArrayList
<>(cfHandles
.length
);
714 for (int i
= 0; i
< cfHandles
.length
; i
++) {
715 final ColumnFamilyHandle columnFamilyHandle
= new ColumnFamilyHandle(this, cfHandles
[i
]);
716 columnFamilyHandles
.add(columnFamilyHandle
);
718 ownedColumnFamilyHandles
.addAll(columnFamilyHandles
);
719 return columnFamilyHandles
;
723 * Bulk create column families with the same column family options.
725 * @param columnFamilyDescriptors the descriptions of the column families.
727 * @return the handles to the newly created column families.
729 * @throws RocksDBException if an error occurs whilst creating
730 * the column families
732 public List
<ColumnFamilyHandle
> createColumnFamilies(
733 final List
<ColumnFamilyDescriptor
> columnFamilyDescriptors
)
734 throws RocksDBException
{
735 final long[] cfOptsHandles
= new long[columnFamilyDescriptors
.size()];
736 final byte[][] cfNames
= new byte[columnFamilyDescriptors
.size()][];
737 for (int i
= 0; i
< columnFamilyDescriptors
.size(); i
++) {
738 final ColumnFamilyDescriptor columnFamilyDescriptor
739 = columnFamilyDescriptors
.get(i
);
740 cfOptsHandles
[i
] = columnFamilyDescriptor
.getOptions().nativeHandle_
;
741 cfNames
[i
] = columnFamilyDescriptor
.getName();
743 final long[] cfHandles
= createColumnFamilies(nativeHandle_
,
744 cfOptsHandles
, cfNames
);
745 final List
<ColumnFamilyHandle
> columnFamilyHandles
=
746 new ArrayList
<>(cfHandles
.length
);
747 for (int i
= 0; i
< cfHandles
.length
; i
++) {
748 final ColumnFamilyHandle columnFamilyHandle
= new ColumnFamilyHandle(this, cfHandles
[i
]);
749 columnFamilyHandles
.add(columnFamilyHandle
);
751 ownedColumnFamilyHandles
.addAll(columnFamilyHandles
);
752 return columnFamilyHandles
;
756 * Drops the column family specified by {@code columnFamilyHandle}. This call
757 * only records a drop record in the manifest and prevents the column
758 * family from flushing and compacting.
760 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
763 * @throws RocksDBException thrown if error happens in underlying
766 public void dropColumnFamily(final ColumnFamilyHandle columnFamilyHandle
)
767 throws RocksDBException
{
768 dropColumnFamily(nativeHandle_
, columnFamilyHandle
.nativeHandle_
);
771 // Bulk drop column families. This call only records drop records in the
772 // manifest and prevents the column families from flushing and compacting.
773 // In case of error, the request may succeed partially. User may call
774 // ListColumnFamilies to check the result.
775 public void dropColumnFamilies(
776 final List
<ColumnFamilyHandle
> columnFamilies
) throws RocksDBException
{
777 final long[] cfHandles
= new long[columnFamilies
.size()];
778 for (int i
= 0; i
< columnFamilies
.size(); i
++) {
779 cfHandles
[i
] = columnFamilies
.get(i
).nativeHandle_
;
781 dropColumnFamilies(nativeHandle_
, cfHandles
);
785 * Deletes native column family handle of given {@link ColumnFamilyHandle} Java object
786 * and removes reference from {@link RocksDB#ownedColumnFamilyHandles}.
788 * @param columnFamilyHandle column family handle object.
790 public void destroyColumnFamilyHandle(final ColumnFamilyHandle columnFamilyHandle
) {
791 for (int i
= 0; i
< ownedColumnFamilyHandles
.size(); ++i
) {
792 final ColumnFamilyHandle ownedHandle
= ownedColumnFamilyHandles
.get(i
);
793 if (ownedHandle
.equals(columnFamilyHandle
)) {
794 columnFamilyHandle
.close();
795 ownedColumnFamilyHandles
.remove(i
);
802 * Set the database entry for "key" to "value".
804 * @param key the specified key to be inserted.
805 * @param value the value associated with the specified key.
807 * @throws RocksDBException thrown if error happens in underlying
810 public void put(final byte[] key
, final byte[] value
)
811 throws RocksDBException
{
812 put(nativeHandle_
, key
, 0, key
.length
, value
, 0, value
.length
);
816 * Set the database entry for "key" to "value".
818 * @param key The specified key to be inserted
819 * @param offset the offset of the "key" array to be used, must be
820 * non-negative and no larger than "key".length
821 * @param len the length of the "key" array to be used, must be non-negative
822 * and no larger than ("key".length - offset)
823 * @param value the value associated with the specified key
824 * @param vOffset the offset of the "value" array to be used, must be
825 * non-negative and no longer than "key".length
826 * @param vLen the length of the "value" array to be used, must be
827 * non-negative and no larger than ("value".length - offset)
829 * @throws RocksDBException thrown if errors happens in underlying native
831 * @throws IndexOutOfBoundsException if an offset or length is out of bounds
833 public void put(final byte[] key
, final int offset
, final int len
,
834 final byte[] value
, final int vOffset
, final int vLen
)
835 throws RocksDBException
{
836 checkBounds(offset
, len
, key
.length
);
837 checkBounds(vOffset
, vLen
, value
.length
);
838 put(nativeHandle_
, key
, offset
, len
, value
, vOffset
, vLen
);
842 * Set the database entry for "key" to "value" in the specified
845 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
847 * @param key the specified key to be inserted.
848 * @param value the value associated with the specified key.
850 * throws IllegalArgumentException if column family is not present
852 * @throws RocksDBException thrown if error happens in underlying
855 public void put(final ColumnFamilyHandle columnFamilyHandle
,
856 final byte[] key
, final byte[] value
) throws RocksDBException
{
857 put(nativeHandle_
, key
, 0, key
.length
, value
, 0, value
.length
,
858 columnFamilyHandle
.nativeHandle_
);
862 * Set the database entry for "key" to "value" in the specified
865 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
867 * @param key The specified key to be inserted
868 * @param offset the offset of the "key" array to be used, must
869 * be non-negative and no larger than "key".length
870 * @param len the length of the "key" array to be used, must be non-negative
871 * and no larger than ("key".length - offset)
872 * @param value the value associated with the specified key
873 * @param vOffset the offset of the "value" array to be used, must be
874 * non-negative and no longer than "key".length
875 * @param vLen the length of the "value" array to be used, must be
876 * non-negative and no larger than ("value".length - offset)
878 * @throws RocksDBException thrown if errors happens in underlying native
880 * @throws IndexOutOfBoundsException if an offset or length is out of bounds
882 public void put(final ColumnFamilyHandle columnFamilyHandle
,
883 final byte[] key
, final int offset
, final int len
,
884 final byte[] value
, final int vOffset
, final int vLen
)
885 throws RocksDBException
{
886 checkBounds(offset
, len
, key
.length
);
887 checkBounds(vOffset
, vLen
, value
.length
);
888 put(nativeHandle_
, key
, offset
, len
, value
, vOffset
, vLen
,
889 columnFamilyHandle
.nativeHandle_
);
893 * Set the database entry for "key" to "value".
895 * @param writeOpts {@link org.rocksdb.WriteOptions} instance.
896 * @param key the specified key to be inserted.
897 * @param value the value associated with the specified key.
899 * @throws RocksDBException thrown if error happens in underlying
902 public void put(final WriteOptions writeOpts
, final byte[] key
,
903 final byte[] value
) throws RocksDBException
{
904 put(nativeHandle_
, writeOpts
.nativeHandle_
,
905 key
, 0, key
.length
, value
, 0, value
.length
);
909 * Set the database entry for "key" to "value".
911 * @param writeOpts {@link org.rocksdb.WriteOptions} instance.
912 * @param key The specified key to be inserted
913 * @param offset the offset of the "key" array to be used, must be
914 * non-negative and no larger than "key".length
915 * @param len the length of the "key" array to be used, must be non-negative
916 * and no larger than ("key".length - offset)
917 * @param value the value associated with the specified key
918 * @param vOffset the offset of the "value" array to be used, must be
919 * non-negative and no longer than "key".length
920 * @param vLen the length of the "value" array to be used, must be
921 * non-negative and no larger than ("value".length - offset)
923 * @throws RocksDBException thrown if error happens in underlying
925 * @throws IndexOutOfBoundsException if an offset or length is out of bounds
927 public void put(final WriteOptions writeOpts
,
928 final byte[] key
, final int offset
, final int len
,
929 final byte[] value
, final int vOffset
, final int vLen
)
930 throws RocksDBException
{
931 checkBounds(offset
, len
, key
.length
);
932 checkBounds(vOffset
, vLen
, value
.length
);
933 put(nativeHandle_
, writeOpts
.nativeHandle_
,
934 key
, offset
, len
, value
, vOffset
, vLen
);
938 * Set the database entry for "key" to "value" for the specified
941 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
943 * @param writeOpts {@link org.rocksdb.WriteOptions} instance.
944 * @param key the specified key to be inserted.
945 * @param value the value associated with the specified key.
947 * throws IllegalArgumentException if column family is not present
949 * @throws RocksDBException thrown if error happens in underlying
951 * @see IllegalArgumentException
953 public void put(final ColumnFamilyHandle columnFamilyHandle
,
954 final WriteOptions writeOpts
, final byte[] key
,
955 final byte[] value
) throws RocksDBException
{
956 put(nativeHandle_
, writeOpts
.nativeHandle_
, key
, 0, key
.length
, value
,
957 0, value
.length
, columnFamilyHandle
.nativeHandle_
);
961 * Set the database entry for "key" to "value" for the specified
964 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
966 * @param writeOpts {@link org.rocksdb.WriteOptions} instance.
967 * @param key the specified key to be inserted. Position and limit is used.
968 * Supports direct buffer only.
969 * @param value the value associated with the specified key. Position and limit is used.
970 * Supports direct buffer only.
972 * throws IllegalArgumentException if column family is not present
974 * @throws RocksDBException thrown if error happens in underlying
976 * @see IllegalArgumentException
978 public void put(final ColumnFamilyHandle columnFamilyHandle
, final WriteOptions writeOpts
,
979 final ByteBuffer key
, final ByteBuffer value
) throws RocksDBException
{
980 assert key
.isDirect() && value
.isDirect();
981 putDirect(nativeHandle_
, writeOpts
.nativeHandle_
, key
, key
.position(), key
.remaining(), value
,
982 value
.position(), value
.remaining(), columnFamilyHandle
.nativeHandle_
);
983 key
.position(key
.limit());
984 value
.position(value
.limit());
988 * Set the database entry for "key" to "value".
990 * @param writeOpts {@link org.rocksdb.WriteOptions} instance.
991 * @param key the specified key to be inserted. Position and limit is used.
992 * Supports direct buffer only.
993 * @param value the value associated with the specified key. Position and limit is used.
994 * Supports direct buffer only.
996 * throws IllegalArgumentException if column family is not present
998 * @throws RocksDBException thrown if error happens in underlying
1000 * @see IllegalArgumentException
1002 public void put(final WriteOptions writeOpts
, final ByteBuffer key
, final ByteBuffer value
)
1003 throws RocksDBException
{
1004 assert key
.isDirect() && value
.isDirect();
1005 putDirect(nativeHandle_
, writeOpts
.nativeHandle_
, key
, key
.position(), key
.remaining(), value
,
1006 value
.position(), value
.remaining(), 0);
1007 key
.position(key
.limit());
1008 value
.position(value
.limit());
1012 * Set the database entry for "key" to "value" for the specified
1015 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
1017 * @param writeOpts {@link org.rocksdb.WriteOptions} instance.
1018 * @param key The specified key to be inserted
1019 * @param offset the offset of the "key" array to be used, must be
1020 * non-negative and no larger than "key".length
1021 * @param len the length of the "key" array to be used, must be non-negative
1022 * and no larger than ("key".length - offset)
1023 * @param value the value associated with the specified key
1024 * @param vOffset the offset of the "value" array to be used, must be
1025 * non-negative and no longer than "key".length
1026 * @param vLen the length of the "value" array to be used, must be
1027 * non-negative and no larger than ("value".length - offset)
1029 * @throws RocksDBException thrown if error happens in underlying
1031 * @throws IndexOutOfBoundsException if an offset or length is out of bounds
1033 public void put(final ColumnFamilyHandle columnFamilyHandle
,
1034 final WriteOptions writeOpts
,
1035 final byte[] key
, final int offset
, final int len
,
1036 final byte[] value
, final int vOffset
, final int vLen
)
1037 throws RocksDBException
{
1038 checkBounds(offset
, len
, key
.length
);
1039 checkBounds(vOffset
, vLen
, value
.length
);
1040 put(nativeHandle_
, writeOpts
.nativeHandle_
, key
, offset
, len
, value
,
1041 vOffset
, vLen
, columnFamilyHandle
.nativeHandle_
);
1045 * Delete the database entry (if any) for "key". Returns OK on
1046 * success, and a non-OK status on error. It is not an error if "key"
1047 * did not exist in the database.
1049 * @param key Key to delete within database
1051 * @throws RocksDBException thrown if error happens in underlying
1054 public void delete(final byte[] key
) throws RocksDBException
{
1055 delete(nativeHandle_
, key
, 0, key
.length
);
1059 * Delete the database entry (if any) for "key". Returns OK on
1060 * success, and a non-OK status on error. It is not an error if "key"
1061 * did not exist in the database.
1063 * @param key Key to delete within database
1064 * @param offset the offset of the "key" array to be used, must be
1065 * non-negative and no larger than "key".length
1066 * @param len the length of the "key" array to be used, must be
1067 * non-negative and no larger than ("key".length - offset)
1069 * @throws RocksDBException thrown if error happens in underlying
1072 public void delete(final byte[] key
, final int offset
, final int len
)
1073 throws RocksDBException
{
1074 delete(nativeHandle_
, key
, offset
, len
);
1078 * Delete the database entry (if any) for "key". Returns OK on
1079 * success, and a non-OK status on error. It is not an error if "key"
1080 * did not exist in the database.
1082 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
1084 * @param key Key to delete within database
1086 * @throws RocksDBException thrown if error happens in underlying
1089 public void delete(final ColumnFamilyHandle columnFamilyHandle
,
1090 final byte[] key
) throws RocksDBException
{
1091 delete(nativeHandle_
, key
, 0, key
.length
, columnFamilyHandle
.nativeHandle_
);
1095 * Delete the database entry (if any) for "key". Returns OK on
1096 * success, and a non-OK status on error. It is not an error if "key"
1097 * did not exist in the database.
1099 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
1101 * @param key Key to delete within database
1102 * @param offset the offset of the "key" array to be used,
1103 * must be non-negative and no larger than "key".length
1104 * @param len the length of the "key" array to be used, must be non-negative
1105 * and no larger than ("value".length - offset)
1107 * @throws RocksDBException thrown if error happens in underlying
1110 public void delete(final ColumnFamilyHandle columnFamilyHandle
,
1111 final byte[] key
, final int offset
, final int len
)
1112 throws RocksDBException
{
1113 delete(nativeHandle_
, key
, offset
, len
, columnFamilyHandle
.nativeHandle_
);
1117 * Delete the database entry (if any) for "key". Returns OK on
1118 * success, and a non-OK status on error. It is not an error if "key"
1119 * did not exist in the database.
1121 * @param writeOpt WriteOptions to be used with delete operation
1122 * @param key Key to delete within database
1124 * @throws RocksDBException thrown if error happens in underlying
1127 public void delete(final WriteOptions writeOpt
, final byte[] key
)
1128 throws RocksDBException
{
1129 delete(nativeHandle_
, writeOpt
.nativeHandle_
, key
, 0, key
.length
);
1133 * Delete the database entry (if any) for "key". Returns OK on
1134 * success, and a non-OK status on error. It is not an error if "key"
1135 * did not exist in the database.
1137 * @param writeOpt WriteOptions to be used with delete operation
1138 * @param key Key to delete within database
1139 * @param offset the offset of the "key" array to be used, must be
1140 * non-negative and no larger than "key".length
1141 * @param len the length of the "key" array to be used, must be
1142 * non-negative and no larger than ("key".length - offset)
1144 * @throws RocksDBException thrown if error happens in underlying
1147 public void delete(final WriteOptions writeOpt
, final byte[] key
,
1148 final int offset
, final int len
) throws RocksDBException
{
1149 delete(nativeHandle_
, writeOpt
.nativeHandle_
, key
, offset
, len
);
1153 * Delete the database entry (if any) for "key". Returns OK on
1154 * success, and a non-OK status on error. It is not an error if "key"
1155 * did not exist in the database.
1157 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
1159 * @param writeOpt WriteOptions to be used with delete operation
1160 * @param key Key to delete within database
1162 * @throws RocksDBException thrown if error happens in underlying
1165 public void delete(final ColumnFamilyHandle columnFamilyHandle
,
1166 final WriteOptions writeOpt
, final byte[] key
)
1167 throws RocksDBException
{
1168 delete(nativeHandle_
, writeOpt
.nativeHandle_
, key
, 0, key
.length
,
1169 columnFamilyHandle
.nativeHandle_
);
1173 * Delete the database entry (if any) for "key". Returns OK on
1174 * success, and a non-OK status on error. It is not an error if "key"
1175 * did not exist in the database.
1177 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
1179 * @param writeOpt WriteOptions to be used with delete operation
1180 * @param key Key to delete within database
1181 * @param offset the offset of the "key" array to be used, must be
1182 * non-negative and no larger than "key".length
1183 * @param len the length of the "key" array to be used, must be
1184 * non-negative and no larger than ("key".length - offset)
1186 * @throws RocksDBException thrown if error happens in underlying
1189 public void delete(final ColumnFamilyHandle columnFamilyHandle
,
1190 final WriteOptions writeOpt
, final byte[] key
, final int offset
,
1191 final int len
) throws RocksDBException
{
1192 delete(nativeHandle_
, writeOpt
.nativeHandle_
, key
, offset
, len
,
1193 columnFamilyHandle
.nativeHandle_
);
1197 * Get the value associated with the specified key within column family.
1199 * @param opt {@link org.rocksdb.ReadOptions} instance.
1200 * @param key the key to retrieve the value. It is using position and limit.
1201 * Supports direct buffer only.
1202 * @param value the out-value to receive the retrieved value.
1203 * It is using position and limit. Limit is set according to value size.
1204 * Supports direct buffer only.
1205 * @return The size of the actual value that matches the specified
1206 * {@code key} in byte. If the return value is greater than the
1207 * length of {@code value}, then it indicates that the size of the
1208 * input buffer {@code value} is insufficient and partial result will
1209 * be returned. RocksDB.NOT_FOUND will be returned if the value not
1212 * @throws RocksDBException thrown if error happens in underlying
1215 public int get(final ReadOptions opt
, final ByteBuffer key
, final ByteBuffer value
)
1216 throws RocksDBException
{
1217 assert key
.isDirect() && value
.isDirect();
1218 int result
= getDirect(nativeHandle_
, opt
.nativeHandle_
, key
, key
.position(), key
.remaining(),
1219 value
, value
.position(), value
.remaining(), 0);
1220 if (result
!= NOT_FOUND
) {
1221 value
.limit(Math
.min(value
.limit(), value
.position() + result
));
1223 key
.position(key
.limit());
1228 * Get the value associated with the specified key within column family.
1230 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
1232 * @param opt {@link org.rocksdb.ReadOptions} instance.
1233 * @param key the key to retrieve the value. It is using position and limit.
1234 * Supports direct buffer only.
1235 * @param value the out-value to receive the retrieved value.
1236 * It is using position and limit. Limit is set according to value size.
1237 * Supports direct buffer only.
1238 * @return The size of the actual value that matches the specified
1239 * {@code key} in byte. If the return value is greater than the
1240 * length of {@code value}, then it indicates that the size of the
1241 * input buffer {@code value} is insufficient and partial result will
1242 * be returned. RocksDB.NOT_FOUND will be returned if the value not
1245 * @throws RocksDBException thrown if error happens in underlying
1248 public int get(final ColumnFamilyHandle columnFamilyHandle
, final ReadOptions opt
,
1249 final ByteBuffer key
, final ByteBuffer value
) throws RocksDBException
{
1250 assert key
.isDirect() && value
.isDirect();
1251 int result
= getDirect(nativeHandle_
, opt
.nativeHandle_
, key
, key
.position(), key
.remaining(),
1252 value
, value
.position(), value
.remaining(), columnFamilyHandle
.nativeHandle_
);
1253 if (result
!= NOT_FOUND
) {
1254 value
.limit(Math
.min(value
.limit(), value
.position() + result
));
1256 key
.position(key
.limit());
1261 * Remove the database entry for {@code key}. Requires that the key exists
1262 * and was not overwritten. It is not an error if the key did not exist
1265 * If a key is overwritten (by calling {@link #put(byte[], byte[])} multiple
1266 * times), then the result of calling SingleDelete() on this key is undefined.
1267 * SingleDelete() only behaves correctly if there has been only one Put()
1268 * for this key since the previous call to SingleDelete() for this key.
1270 * This feature is currently an experimental performance optimization
1271 * for a very specific workload. It is up to the caller to ensure that
1272 * SingleDelete is only used for a key that is not deleted using Delete() or
1273 * written using Merge(). Mixing SingleDelete operations with Deletes and
1274 * Merges can result in undefined behavior.
1276 * @param key Key to delete within database
1278 * @throws RocksDBException thrown if error happens in underlying
1281 @Experimental("Performance optimization for a very specific workload")
1282 public void singleDelete(final byte[] key
) throws RocksDBException
{
1283 singleDelete(nativeHandle_
, key
, key
.length
);
1287 * Remove the database entry for {@code key}. Requires that the key exists
1288 * and was not overwritten. It is not an error if the key did not exist
1291 * If a key is overwritten (by calling {@link #put(byte[], byte[])} multiple
1292 * times), then the result of calling SingleDelete() on this key is undefined.
1293 * SingleDelete() only behaves correctly if there has been only one Put()
1294 * for this key since the previous call to SingleDelete() for this key.
1296 * This feature is currently an experimental performance optimization
1297 * for a very specific workload. It is up to the caller to ensure that
1298 * SingleDelete is only used for a key that is not deleted using Delete() or
1299 * written using Merge(). Mixing SingleDelete operations with Deletes and
1300 * Merges can result in undefined behavior.
1302 * @param columnFamilyHandle The column family to delete the key from
1303 * @param key Key to delete within database
1305 * @throws RocksDBException thrown if error happens in underlying
1308 @Experimental("Performance optimization for a very specific workload")
1309 public void singleDelete(final ColumnFamilyHandle columnFamilyHandle
,
1310 final byte[] key
) throws RocksDBException
{
1311 singleDelete(nativeHandle_
, key
, key
.length
,
1312 columnFamilyHandle
.nativeHandle_
);
1316 * Remove the database entry for {@code key}. Requires that the key exists
1317 * and was not overwritten. It is not an error if the key did not exist
1320 * If a key is overwritten (by calling {@link #put(byte[], byte[])} multiple
1321 * times), then the result of calling SingleDelete() on this key is undefined.
1322 * SingleDelete() only behaves correctly if there has been only one Put()
1323 * for this key since the previous call to SingleDelete() for this key.
1325 * This feature is currently an experimental performance optimization
1326 * for a very specific workload. It is up to the caller to ensure that
1327 * SingleDelete is only used for a key that is not deleted using Delete() or
1328 * written using Merge(). Mixing SingleDelete operations with Deletes and
1329 * Merges can result in undefined behavior.
1331 * Note: consider setting {@link WriteOptions#setSync(boolean)} true.
1333 * @param writeOpt Write options for the delete
1334 * @param key Key to delete within database
1336 * @throws RocksDBException thrown if error happens in underlying
1339 @Experimental("Performance optimization for a very specific workload")
1340 public void singleDelete(final WriteOptions writeOpt
, final byte[] key
)
1341 throws RocksDBException
{
1342 singleDelete(nativeHandle_
, writeOpt
.nativeHandle_
, key
, key
.length
);
1346 * Remove the database entry for {@code key}. Requires that the key exists
1347 * and was not overwritten. It is not an error if the key did not exist
1350 * If a key is overwritten (by calling {@link #put(byte[], byte[])} multiple
1351 * times), then the result of calling SingleDelete() on this key is undefined.
1352 * SingleDelete() only behaves correctly if there has been only one Put()
1353 * for this key since the previous call to SingleDelete() for this key.
1355 * This feature is currently an experimental performance optimization
1356 * for a very specific workload. It is up to the caller to ensure that
1357 * SingleDelete is only used for a key that is not deleted using Delete() or
1358 * written using Merge(). Mixing SingleDelete operations with Deletes and
1359 * Merges can result in undefined behavior.
1361 * Note: consider setting {@link WriteOptions#setSync(boolean)} true.
1363 * @param columnFamilyHandle The column family to delete the key from
1364 * @param writeOpt Write options for the delete
1365 * @param key Key to delete within database
1367 * @throws RocksDBException thrown if error happens in underlying
1370 @Experimental("Performance optimization for a very specific workload")
1371 public void singleDelete(final ColumnFamilyHandle columnFamilyHandle
,
1372 final WriteOptions writeOpt
, final byte[] key
) throws RocksDBException
{
1373 singleDelete(nativeHandle_
, writeOpt
.nativeHandle_
, key
, key
.length
,
1374 columnFamilyHandle
.nativeHandle_
);
1379 * Removes the database entries in the range ["beginKey", "endKey"), i.e.,
1380 * including "beginKey" and excluding "endKey". a non-OK status on error. It
1381 * is not an error if no keys exist in the range ["beginKey", "endKey").
1383 * Delete the database entry (if any) for "key". Returns OK on success, and a
1384 * non-OK status on error. It is not an error if "key" did not exist in the
1387 * @param beginKey First key to delete within database (inclusive)
1388 * @param endKey Last key to delete within database (exclusive)
1390 * @throws RocksDBException thrown if error happens in underlying native
1393 public void deleteRange(final byte[] beginKey
, final byte[] endKey
)
1394 throws RocksDBException
{
1395 deleteRange(nativeHandle_
, beginKey
, 0, beginKey
.length
, endKey
, 0,
1400 * Removes the database entries in the range ["beginKey", "endKey"), i.e.,
1401 * including "beginKey" and excluding "endKey". a non-OK status on error. It
1402 * is not an error if no keys exist in the range ["beginKey", "endKey").
1404 * Delete the database entry (if any) for "key". Returns OK on success, and a
1405 * non-OK status on error. It is not an error if "key" did not exist in the
1408 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} instance
1409 * @param beginKey First key to delete within database (inclusive)
1410 * @param endKey Last key to delete within database (exclusive)
1412 * @throws RocksDBException thrown if error happens in underlying native
1415 public void deleteRange(final ColumnFamilyHandle columnFamilyHandle
,
1416 final byte[] beginKey
, final byte[] endKey
) throws RocksDBException
{
1417 deleteRange(nativeHandle_
, beginKey
, 0, beginKey
.length
, endKey
, 0,
1418 endKey
.length
, columnFamilyHandle
.nativeHandle_
);
1422 * Removes the database entries in the range ["beginKey", "endKey"), i.e.,
1423 * including "beginKey" and excluding "endKey". a non-OK status on error. It
1424 * is not an error if no keys exist in the range ["beginKey", "endKey").
1426 * Delete the database entry (if any) for "key". Returns OK on success, and a
1427 * non-OK status on error. It is not an error if "key" did not exist in the
1430 * @param writeOpt WriteOptions to be used with delete operation
1431 * @param beginKey First key to delete within database (inclusive)
1432 * @param endKey Last key to delete within database (exclusive)
1434 * @throws RocksDBException thrown if error happens in underlying
1437 public void deleteRange(final WriteOptions writeOpt
, final byte[] beginKey
,
1438 final byte[] endKey
) throws RocksDBException
{
1439 deleteRange(nativeHandle_
, writeOpt
.nativeHandle_
, beginKey
, 0,
1440 beginKey
.length
, endKey
, 0, endKey
.length
);
1444 * Removes the database entries in the range ["beginKey", "endKey"), i.e.,
1445 * including "beginKey" and excluding "endKey". a non-OK status on error. It
1446 * is not an error if no keys exist in the range ["beginKey", "endKey").
1448 * Delete the database entry (if any) for "key". Returns OK on success, and a
1449 * non-OK status on error. It is not an error if "key" did not exist in the
1452 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} instance
1453 * @param writeOpt WriteOptions to be used with delete operation
1454 * @param beginKey First key to delete within database (included)
1455 * @param endKey Last key to delete within database (excluded)
1457 * @throws RocksDBException thrown if error happens in underlying native
1460 public void deleteRange(final ColumnFamilyHandle columnFamilyHandle
,
1461 final WriteOptions writeOpt
, final byte[] beginKey
, final byte[] endKey
)
1462 throws RocksDBException
{
1463 deleteRange(nativeHandle_
, writeOpt
.nativeHandle_
, beginKey
, 0,
1464 beginKey
.length
, endKey
, 0, endKey
.length
,
1465 columnFamilyHandle
.nativeHandle_
);
1470 * Add merge operand for key/value pair.
1472 * @param key the specified key to be merged.
1473 * @param value the value to be merged with the current value for the
1476 * @throws RocksDBException thrown if error happens in underlying
1479 public void merge(final byte[] key
, final byte[] value
)
1480 throws RocksDBException
{
1481 merge(nativeHandle_
, key
, 0, key
.length
, value
, 0, value
.length
);
1485 * Add merge operand for key/value pair.
1487 * @param key the specified key to be merged.
1488 * @param offset the offset of the "key" array to be used, must be
1489 * non-negative and no larger than "key".length
1490 * @param len the length of the "key" array to be used, must be non-negative
1491 * and no larger than ("key".length - offset)
1492 * @param value the value to be merged with the current value for the
1494 * @param vOffset the offset of the "value" array to be used, must be
1495 * non-negative and no longer than "key".length
1496 * @param vLen the length of the "value" array to be used, must be
1497 * non-negative and must be non-negative and no larger than
1498 * ("value".length - offset)
1500 * @throws RocksDBException thrown if error happens in underlying
1502 * @throws IndexOutOfBoundsException if an offset or length is out of bounds
1504 public void merge(final byte[] key
, int offset
, int len
, final byte[] value
,
1505 final int vOffset
, final int vLen
) throws RocksDBException
{
1506 checkBounds(offset
, len
, key
.length
);
1507 checkBounds(vOffset
, vLen
, value
.length
);
1508 merge(nativeHandle_
, key
, offset
, len
, value
, vOffset
, vLen
);
1512 * Add merge operand for key/value pair in a ColumnFamily.
1514 * @param columnFamilyHandle {@link ColumnFamilyHandle} instance
1515 * @param key the specified key to be merged.
1516 * @param value the value to be merged with the current value for
1517 * the specified key.
1519 * @throws RocksDBException thrown if error happens in underlying
1522 public void merge(final ColumnFamilyHandle columnFamilyHandle
,
1523 final byte[] key
, final byte[] value
) throws RocksDBException
{
1524 merge(nativeHandle_
, key
, 0, key
.length
, value
, 0, value
.length
,
1525 columnFamilyHandle
.nativeHandle_
);
1529 * Add merge operand for key/value pair in a ColumnFamily.
1531 * @param columnFamilyHandle {@link ColumnFamilyHandle} instance
1532 * @param key the specified key to be merged.
1533 * @param offset the offset of the "key" array to be used, must be
1534 * non-negative and no larger than "key".length
1535 * @param len the length of the "key" array to be used, must be non-negative
1536 * and no larger than ("key".length - offset)
1537 * @param value the value to be merged with the current value for
1538 * the specified key.
1539 * @param vOffset the offset of the "value" array to be used, must be
1540 * non-negative and no longer than "key".length
1541 * @param vLen the length of the "value" array to be used, must be
1542 * must be non-negative and no larger than ("value".length - offset)
1544 * @throws RocksDBException thrown if error happens in underlying
1546 * @throws IndexOutOfBoundsException if an offset or length is out of bounds
1548 public void merge(final ColumnFamilyHandle columnFamilyHandle
,
1549 final byte[] key
, final int offset
, final int len
, final byte[] value
,
1550 final int vOffset
, final int vLen
) throws RocksDBException
{
1551 checkBounds(offset
, len
, key
.length
);
1552 checkBounds(vOffset
, vLen
, value
.length
);
1553 merge(nativeHandle_
, key
, offset
, len
, value
, vOffset
, vLen
,
1554 columnFamilyHandle
.nativeHandle_
);
1558 * Add merge operand for key/value pair.
1560 * @param writeOpts {@link WriteOptions} for this write.
1561 * @param key the specified key to be merged.
1562 * @param value the value to be merged with the current value for
1563 * the specified key.
1565 * @throws RocksDBException thrown if error happens in underlying
1568 public void merge(final WriteOptions writeOpts
, final byte[] key
,
1569 final byte[] value
) throws RocksDBException
{
1570 merge(nativeHandle_
, writeOpts
.nativeHandle_
,
1571 key
, 0, key
.length
, value
, 0, value
.length
);
1575 * Add merge operand for key/value pair.
1577 * @param writeOpts {@link WriteOptions} for this write.
1578 * @param key the specified key to be merged.
1579 * @param offset the offset of the "key" array to be used, must be
1580 * non-negative and no larger than "key".length
1581 * @param len the length of the "key" array to be used, must be non-negative
1582 * and no larger than ("value".length - offset)
1583 * @param value the value to be merged with the current value for
1584 * the specified key.
1585 * @param vOffset the offset of the "value" array to be used, must be
1586 * non-negative and no longer than "key".length
1587 * @param vLen the length of the "value" array to be used, must be
1588 * non-negative and no larger than ("value".length - offset)
1590 * @throws RocksDBException thrown if error happens in underlying
1592 * @throws IndexOutOfBoundsException if an offset or length is out of bounds
1594 public void merge(final WriteOptions writeOpts
,
1595 final byte[] key
, final int offset
, final int len
,
1596 final byte[] value
, final int vOffset
, final int vLen
)
1597 throws RocksDBException
{
1598 checkBounds(offset
, len
, key
.length
);
1599 checkBounds(vOffset
, vLen
, value
.length
);
1600 merge(nativeHandle_
, writeOpts
.nativeHandle_
,
1601 key
, offset
, len
, value
, vOffset
, vLen
);
1605 * Delete the database entry (if any) for "key". Returns OK on
1606 * success, and a non-OK status on error. It is not an error if "key"
1607 * did not exist in the database.
1609 * @param writeOpt WriteOptions to be used with delete operation
1610 * @param key Key to delete within database. It is using position and limit.
1611 * Supports direct buffer only.
1613 * @throws RocksDBException thrown if error happens in underlying
1616 public void delete(final WriteOptions writeOpt
, final ByteBuffer key
) throws RocksDBException
{
1617 assert key
.isDirect();
1618 deleteDirect(nativeHandle_
, writeOpt
.nativeHandle_
, key
, key
.position(), key
.remaining(), 0);
1619 key
.position(key
.limit());
1623 * Delete the database entry (if any) for "key". Returns OK on
1624 * success, and a non-OK status on error. It is not an error if "key"
1625 * did not exist in the database.
1627 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
1629 * @param writeOpt WriteOptions to be used with delete operation
1630 * @param key Key to delete within database. It is using position and limit.
1631 * Supports direct buffer only.
1633 * @throws RocksDBException thrown if error happens in underlying
1636 public void delete(final ColumnFamilyHandle columnFamilyHandle
, final WriteOptions writeOpt
,
1637 final ByteBuffer key
) throws RocksDBException
{
1638 assert key
.isDirect();
1639 deleteDirect(nativeHandle_
, writeOpt
.nativeHandle_
, key
, key
.position(), key
.remaining(),
1640 columnFamilyHandle
.nativeHandle_
);
1641 key
.position(key
.limit());
1645 * Add merge operand for key/value pair.
1647 * @param columnFamilyHandle {@link ColumnFamilyHandle} instance
1648 * @param writeOpts {@link WriteOptions} for this write.
1649 * @param key the specified key to be merged.
1650 * @param value the value to be merged with the current value for the
1653 * @throws RocksDBException thrown if error happens in underlying
1656 public void merge(final ColumnFamilyHandle columnFamilyHandle
,
1657 final WriteOptions writeOpts
, final byte[] key
, final byte[] value
)
1658 throws RocksDBException
{
1659 merge(nativeHandle_
, writeOpts
.nativeHandle_
,
1660 key
, 0, key
.length
, value
, 0, value
.length
,
1661 columnFamilyHandle
.nativeHandle_
);
1665 * Add merge operand for key/value pair.
1667 * @param columnFamilyHandle {@link ColumnFamilyHandle} instance
1668 * @param writeOpts {@link WriteOptions} for this write.
1669 * @param key the specified key to be merged.
1670 * @param offset the offset of the "key" array to be used, must be
1671 * non-negative and no larger than "key".length
1672 * @param len the length of the "key" array to be used, must be non-negative
1673 * and no larger than ("key".length - offset)
1674 * @param value the value to be merged with the current value for
1675 * the specified key.
1676 * @param vOffset the offset of the "value" array to be used, must be
1677 * non-negative and no longer than "key".length
1678 * @param vLen the length of the "value" array to be used, must be
1679 * non-negative and no larger than ("value".length - offset)
1681 * @throws RocksDBException thrown if error happens in underlying
1683 * @throws IndexOutOfBoundsException if an offset or length is out of bounds
1686 final ColumnFamilyHandle columnFamilyHandle
, final WriteOptions writeOpts
,
1687 final byte[] key
, final int offset
, final int len
,
1688 final byte[] value
, final int vOffset
, final int vLen
)
1689 throws RocksDBException
{
1690 checkBounds(offset
, len
, key
.length
);
1691 checkBounds(vOffset
, vLen
, value
.length
);
1692 merge(nativeHandle_
, writeOpts
.nativeHandle_
,
1693 key
, offset
, len
, value
, vOffset
, vLen
,
1694 columnFamilyHandle
.nativeHandle_
);
1698 * Apply the specified updates to the database.
1700 * @param writeOpts WriteOptions instance
1701 * @param updates WriteBatch instance
1703 * @throws RocksDBException thrown if error happens in underlying
1706 public void write(final WriteOptions writeOpts
, final WriteBatch updates
)
1707 throws RocksDBException
{
1708 write0(nativeHandle_
, writeOpts
.nativeHandle_
, updates
.nativeHandle_
);
1712 * Apply the specified updates to the database.
1714 * @param writeOpts WriteOptions instance
1715 * @param updates WriteBatchWithIndex instance
1717 * @throws RocksDBException thrown if error happens in underlying
1720 public void write(final WriteOptions writeOpts
,
1721 final WriteBatchWithIndex updates
) throws RocksDBException
{
1722 write1(nativeHandle_
, writeOpts
.nativeHandle_
, updates
.nativeHandle_
);
1725 // TODO(AR) we should improve the #get() API, returning -1 (RocksDB.NOT_FOUND) is not very nice
1726 // when we could communicate better status into, also the C++ code show that -2 could be returned
1729 * Get the value associated with the specified key within column family*
1731 * @param key the key to retrieve the value.
1732 * @param value the out-value to receive the retrieved value.
1734 * @return The size of the actual value that matches the specified
1735 * {@code key} in byte. If the return value is greater than the
1736 * length of {@code value}, then it indicates that the size of the
1737 * input buffer {@code value} is insufficient and partial result will
1738 * be returned. RocksDB.NOT_FOUND will be returned if the value not
1741 * @throws RocksDBException thrown if error happens in underlying
1744 public int get(final byte[] key
, final byte[] value
) throws RocksDBException
{
1745 return get(nativeHandle_
, key
, 0, key
.length
, value
, 0, value
.length
);
1749 * Get the value associated with the specified key within column family*
1751 * @param key the key to retrieve the value.
1752 * @param offset the offset of the "key" array to be used, must be
1753 * non-negative and no larger than "key".length
1754 * @param len the length of the "key" array to be used, must be non-negative
1755 * and no larger than ("key".length - offset)
1756 * @param value the out-value to receive the retrieved value.
1757 * @param vOffset the offset of the "value" array to be used, must be
1758 * non-negative and no longer than "value".length
1759 * @param vLen the length of the "value" array to be used, must be
1760 * non-negative and and no larger than ("value".length - offset)
1762 * @return The size of the actual value that matches the specified
1763 * {@code key} in byte. If the return value is greater than the
1764 * length of {@code value}, then it indicates that the size of the
1765 * input buffer {@code value} is insufficient and partial result will
1766 * be returned. RocksDB.NOT_FOUND will be returned if the value not
1769 * @throws RocksDBException thrown if error happens in underlying
1772 public int get(final byte[] key
, final int offset
, final int len
,
1773 final byte[] value
, final int vOffset
, final int vLen
)
1774 throws RocksDBException
{
1775 checkBounds(offset
, len
, key
.length
);
1776 checkBounds(vOffset
, vLen
, value
.length
);
1777 return get(nativeHandle_
, key
, offset
, len
, value
, vOffset
, vLen
);
1781 * Get the value associated with the specified key within column family.
1783 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
1785 * @param key the key to retrieve the value.
1786 * @param value the out-value to receive the retrieved value.
1787 * @return The size of the actual value that matches the specified
1788 * {@code key} in byte. If the return value is greater than the
1789 * length of {@code value}, then it indicates that the size of the
1790 * input buffer {@code value} is insufficient and partial result will
1791 * be returned. RocksDB.NOT_FOUND will be returned if the value not
1794 * @throws RocksDBException thrown if error happens in underlying
1797 public int get(final ColumnFamilyHandle columnFamilyHandle
, final byte[] key
,
1798 final byte[] value
) throws RocksDBException
, IllegalArgumentException
{
1799 return get(nativeHandle_
, key
, 0, key
.length
, value
, 0, value
.length
,
1800 columnFamilyHandle
.nativeHandle_
);
1804 * Get the value associated with the specified key within column family.
1806 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
1808 * @param key the key to retrieve the value.
1809 * @param offset the offset of the "key" array to be used, must be
1810 * non-negative and no larger than "key".length
1811 * @param len the length of the "key" array to be used, must be non-negative
1812 * an no larger than ("key".length - offset)
1813 * @param value the out-value to receive the retrieved value.
1814 * @param vOffset the offset of the "value" array to be used, must be
1815 * non-negative and no longer than "key".length
1816 * @param vLen the length of the "value" array to be used, must be
1817 * non-negative and no larger than ("value".length - offset)
1819 * @return The size of the actual value that matches the specified
1820 * {@code key} in byte. If the return value is greater than the
1821 * length of {@code value}, then it indicates that the size of the
1822 * input buffer {@code value} is insufficient and partial result will
1823 * be returned. RocksDB.NOT_FOUND will be returned if the value not
1826 * @throws RocksDBException thrown if error happens in underlying
1829 public int get(final ColumnFamilyHandle columnFamilyHandle
, final byte[] key
,
1830 final int offset
, final int len
, final byte[] value
, final int vOffset
,
1831 final int vLen
) throws RocksDBException
, IllegalArgumentException
{
1832 checkBounds(offset
, len
, key
.length
);
1833 checkBounds(vOffset
, vLen
, value
.length
);
1834 return get(nativeHandle_
, key
, offset
, len
, value
, vOffset
, vLen
,
1835 columnFamilyHandle
.nativeHandle_
);
1839 * Get the value associated with the specified key.
1841 * @param opt {@link org.rocksdb.ReadOptions} instance.
1842 * @param key the key to retrieve the value.
1843 * @param value the out-value to receive the retrieved value.
1844 * @return The size of the actual value that matches the specified
1845 * {@code key} in byte. If the return value is greater than the
1846 * length of {@code value}, then it indicates that the size of the
1847 * input buffer {@code value} is insufficient and partial result will
1848 * be returned. RocksDB.NOT_FOUND will be returned if the value not
1851 * @throws RocksDBException thrown if error happens in underlying
1854 public int get(final ReadOptions opt
, final byte[] key
,
1855 final byte[] value
) throws RocksDBException
{
1856 return get(nativeHandle_
, opt
.nativeHandle_
,
1857 key
, 0, key
.length
, value
, 0, value
.length
);
1861 * Get the value associated with the specified key.
1863 * @param opt {@link org.rocksdb.ReadOptions} instance.
1864 * @param key the key to retrieve the value.
1865 * @param offset the offset of the "key" array to be used, must be
1866 * non-negative and no larger than "key".length
1867 * @param len the length of the "key" array to be used, must be non-negative
1868 * and no larger than ("key".length - offset)
1869 * @param value the out-value to receive the retrieved value.
1870 * @param vOffset the offset of the "value" array to be used, must be
1871 * non-negative and no longer than "key".length
1872 * @param vLen the length of the "value" array to be used, must be
1873 * non-negative and no larger than ("value".length - offset)
1874 * @return The size of the actual value that matches the specified
1875 * {@code key} in byte. If the return value is greater than the
1876 * length of {@code value}, then it indicates that the size of the
1877 * input buffer {@code value} is insufficient and partial result will
1878 * be returned. RocksDB.NOT_FOUND will be returned if the value not
1881 * @throws RocksDBException thrown if error happens in underlying
1884 public int get(final ReadOptions opt
, final byte[] key
, final int offset
,
1885 final int len
, final byte[] value
, final int vOffset
, final int vLen
)
1886 throws RocksDBException
{
1887 checkBounds(offset
, len
, key
.length
);
1888 checkBounds(vOffset
, vLen
, value
.length
);
1889 return get(nativeHandle_
, opt
.nativeHandle_
,
1890 key
, offset
, len
, value
, vOffset
, vLen
);
1894 * Get the value associated with the specified key within column family.
1896 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
1898 * @param opt {@link org.rocksdb.ReadOptions} instance.
1899 * @param key the key to retrieve the value.
1900 * @param value the out-value to receive the retrieved value.
1901 * @return The size of the actual value that matches the specified
1902 * {@code key} in byte. If the return value is greater than the
1903 * length of {@code value}, then it indicates that the size of the
1904 * input buffer {@code value} is insufficient and partial result will
1905 * be returned. RocksDB.NOT_FOUND will be returned if the value not
1908 * @throws RocksDBException thrown if error happens in underlying
1911 public int get(final ColumnFamilyHandle columnFamilyHandle
,
1912 final ReadOptions opt
, final byte[] key
, final byte[] value
)
1913 throws RocksDBException
{
1914 return get(nativeHandle_
, opt
.nativeHandle_
, key
, 0, key
.length
, value
,
1915 0, value
.length
, columnFamilyHandle
.nativeHandle_
);
1919 * Get the value associated with the specified key within column family.
1921 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
1923 * @param opt {@link org.rocksdb.ReadOptions} instance.
1924 * @param key the key to retrieve the value.
1925 * @param offset the offset of the "key" array to be used, must be
1926 * non-negative and no larger than "key".length
1927 * @param len the length of the "key" array to be used, must be
1928 * non-negative and and no larger than ("key".length - offset)
1929 * @param value the out-value to receive the retrieved value.
1930 * @param vOffset the offset of the "value" array to be used, must be
1931 * non-negative and no longer than "key".length
1932 * @param vLen the length of the "value" array to be used, and must be
1933 * non-negative and no larger than ("value".length - offset)
1934 * @return The size of the actual value that matches the specified
1935 * {@code key} in byte. If the return value is greater than the
1936 * length of {@code value}, then it indicates that the size of the
1937 * input buffer {@code value} is insufficient and partial result will
1938 * be returned. RocksDB.NOT_FOUND will be returned if the value not
1941 * @throws RocksDBException thrown if error happens in underlying
1944 public int get(final ColumnFamilyHandle columnFamilyHandle
,
1945 final ReadOptions opt
, final byte[] key
, final int offset
, final int len
,
1946 final byte[] value
, final int vOffset
, final int vLen
)
1947 throws RocksDBException
{
1948 checkBounds(offset
, len
, key
.length
);
1949 checkBounds(vOffset
, vLen
, value
.length
);
1950 return get(nativeHandle_
, opt
.nativeHandle_
, key
, offset
, len
, value
,
1951 vOffset
, vLen
, columnFamilyHandle
.nativeHandle_
);
1955 * The simplified version of get which returns a new byte array storing
1956 * the value associated with the specified input key if any. null will be
1957 * returned if the specified key is not found.
1959 * @param key the key retrieve the value.
1960 * @return a byte array storing the value associated with the input key if
1961 * any. null if it does not find the specified key.
1963 * @throws RocksDBException thrown if error happens in underlying
1966 public byte[] get(final byte[] key
) throws RocksDBException
{
1967 return get(nativeHandle_
, key
, 0, key
.length
);
1971 * The simplified version of get which returns a new byte array storing
1972 * the value associated with the specified input key if any. null will be
1973 * returned if the specified key is not found.
1975 * @param key the key retrieve the value.
1976 * @param offset the offset of the "key" array to be used, must be
1977 * non-negative and no larger than "key".length
1978 * @param len the length of the "key" array to be used, must be non-negative
1979 * and no larger than ("key".length - offset)
1980 * @return a byte array storing the value associated with the input key if
1981 * any. null if it does not find the specified key.
1983 * @throws RocksDBException thrown if error happens in underlying
1986 public byte[] get(final byte[] key
, final int offset
,
1987 final int len
) throws RocksDBException
{
1988 checkBounds(offset
, len
, key
.length
);
1989 return get(nativeHandle_
, key
, offset
, len
);
1993 * The simplified version of get which returns a new byte array storing
1994 * the value associated with the specified input key if any. null will be
1995 * returned if the specified key is not found.
1997 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
1999 * @param key the key retrieve the value.
2000 * @return a byte array storing the value associated with the input key if
2001 * any. null if it does not find the specified key.
2003 * @throws RocksDBException thrown if error happens in underlying
2006 public byte[] get(final ColumnFamilyHandle columnFamilyHandle
,
2007 final byte[] key
) throws RocksDBException
{
2008 return get(nativeHandle_
, key
, 0, key
.length
,
2009 columnFamilyHandle
.nativeHandle_
);
2013 * The simplified version of get which returns a new byte array storing
2014 * the value associated with the specified input key if any. null will be
2015 * returned if the specified key is not found.
2017 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
2019 * @param key the key retrieve the value.
2020 * @param offset the offset of the "key" array to be used, must be
2021 * non-negative and no larger than "key".length
2022 * @param len the length of the "key" array to be used, must be non-negative
2023 * and no larger than ("key".length - offset)
2024 * @return a byte array storing the value associated with the input key if
2025 * any. null if it does not find the specified key.
2027 * @throws RocksDBException thrown if error happens in underlying
2030 public byte[] get(final ColumnFamilyHandle columnFamilyHandle
,
2031 final byte[] key
, final int offset
, final int len
)
2032 throws RocksDBException
{
2033 checkBounds(offset
, len
, key
.length
);
2034 return get(nativeHandle_
, key
, offset
, len
,
2035 columnFamilyHandle
.nativeHandle_
);
2039 * The simplified version of get which returns a new byte array storing
2040 * the value associated with the specified input key if any. null will be
2041 * returned if the specified key is not found.
2043 * @param key the key retrieve the value.
2044 * @param opt Read options.
2045 * @return a byte array storing the value associated with the input key if
2046 * any. null if it does not find the specified key.
2048 * @throws RocksDBException thrown if error happens in underlying
2051 public byte[] get(final ReadOptions opt
, final byte[] key
)
2052 throws RocksDBException
{
2053 return get(nativeHandle_
, opt
.nativeHandle_
, key
, 0, key
.length
);
2057 * The simplified version of get which returns a new byte array storing
2058 * the value associated with the specified input key if any. null will be
2059 * returned if the specified key is not found.
2061 * @param key the key retrieve the value.
2062 * @param offset the offset of the "key" array to be used, must be
2063 * non-negative and no larger than "key".length
2064 * @param len the length of the "key" array to be used, must be non-negative
2065 * and no larger than ("key".length - offset)
2066 * @param opt Read options.
2067 * @return a byte array storing the value associated with the input key if
2068 * any. null if it does not find the specified key.
2070 * @throws RocksDBException thrown if error happens in underlying
2073 public byte[] get(final ReadOptions opt
, final byte[] key
, final int offset
,
2074 final int len
) throws RocksDBException
{
2075 checkBounds(offset
, len
, key
.length
);
2076 return get(nativeHandle_
, opt
.nativeHandle_
, key
, offset
, len
);
2080 * The simplified version of get which returns a new byte array storing
2081 * the value associated with the specified input key if any. null will be
2082 * returned if the specified key is not found.
2084 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
2086 * @param key the key retrieve the value.
2087 * @param opt Read options.
2088 * @return a byte array storing the value associated with the input key if
2089 * any. null if it does not find the specified key.
2091 * @throws RocksDBException thrown if error happens in underlying
2094 public byte[] get(final ColumnFamilyHandle columnFamilyHandle
,
2095 final ReadOptions opt
, final byte[] key
) throws RocksDBException
{
2096 return get(nativeHandle_
, opt
.nativeHandle_
, key
, 0, key
.length
,
2097 columnFamilyHandle
.nativeHandle_
);
2101 * The simplified version of get which returns a new byte array storing
2102 * the value associated with the specified input key if any. null will be
2103 * returned if the specified key is not found.
2105 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
2107 * @param key the key retrieve the value.
2108 * @param offset the offset of the "key" array to be used, must be
2109 * non-negative and no larger than "key".length
2110 * @param len the length of the "key" array to be used, must be non-negative
2111 * and no larger than ("key".length - offset)
2112 * @param opt Read options.
2113 * @return a byte array storing the value associated with the input key if
2114 * any. null if it does not find the specified key.
2116 * @throws RocksDBException thrown if error happens in underlying
2119 public byte[] get(final ColumnFamilyHandle columnFamilyHandle
,
2120 final ReadOptions opt
, final byte[] key
, final int offset
, final int len
)
2121 throws RocksDBException
{
2122 checkBounds(offset
, len
, key
.length
);
2123 return get(nativeHandle_
, opt
.nativeHandle_
, key
, offset
, len
,
2124 columnFamilyHandle
.nativeHandle_
);
2128 * Takes a list of keys, and returns a list of values for the given list of
2129 * keys. List will contain null for keys which could not be found.
2131 * @param keys List of keys for which values need to be retrieved.
2132 * @return List of values for the given list of keys. List will contain
2133 * null for keys which could not be found.
2135 * @throws RocksDBException thrown if error happens in underlying
2138 public List
<byte[]> multiGetAsList(final List
<byte[]> keys
)
2139 throws RocksDBException
{
2140 assert(keys
.size() != 0);
2142 final byte[][] keysArray
= keys
.toArray(new byte[keys
.size()][]);
2143 final int[] keyOffsets
= new int[keysArray
.length
];
2144 final int[] keyLengths
= new int[keysArray
.length
];
2145 for(int i
= 0; i
< keyLengths
.length
; i
++) {
2146 keyLengths
[i
] = keysArray
[i
].length
;
2149 return Arrays
.asList(multiGet(nativeHandle_
, keysArray
, keyOffsets
,
2154 * Returns a list of values for the given list of keys. List will contain
2155 * null for keys which could not be found.
2157 * Note: Every key needs to have a related column family name in
2158 * {@code columnFamilyHandleList}.
2161 * @param columnFamilyHandleList {@link java.util.List} containing
2162 * {@link org.rocksdb.ColumnFamilyHandle} instances.
2163 * @param keys List of keys for which values need to be retrieved.
2164 * @return List of values for the given list of keys. List will contain
2165 * null for keys which could not be found.
2167 * @throws RocksDBException thrown if error happens in underlying
2169 * @throws IllegalArgumentException thrown if the size of passed keys is not
2170 * equal to the amount of passed column family handles.
2172 public List
<byte[]> multiGetAsList(
2173 final List
<ColumnFamilyHandle
> columnFamilyHandleList
,
2174 final List
<byte[]> keys
) throws RocksDBException
,
2175 IllegalArgumentException
{
2176 assert(keys
.size() != 0);
2177 // Check if key size equals cfList size. If not a exception must be
2178 // thrown. If not a Segmentation fault happens.
2179 if (keys
.size() != columnFamilyHandleList
.size()) {
2180 throw new IllegalArgumentException(
2181 "For each key there must be a ColumnFamilyHandle.");
2183 final long[] cfHandles
= new long[columnFamilyHandleList
.size()];
2184 for (int i
= 0; i
< columnFamilyHandleList
.size(); i
++) {
2185 cfHandles
[i
] = columnFamilyHandleList
.get(i
).nativeHandle_
;
2188 final byte[][] keysArray
= keys
.toArray(new byte[keys
.size()][]);
2189 final int[] keyOffsets
= new int[keysArray
.length
];
2190 final int[] keyLengths
= new int[keysArray
.length
];
2191 for(int i
= 0; i
< keyLengths
.length
; i
++) {
2192 keyLengths
[i
] = keysArray
[i
].length
;
2195 return Arrays
.asList(multiGet(nativeHandle_
, keysArray
, keyOffsets
,
2196 keyLengths
, cfHandles
));
2200 * Returns a list of values for the given list of keys. List will contain
2201 * null for keys which could not be found.
2203 * @param opt Read options.
2204 * @param keys of keys for which values need to be retrieved.
2205 * @return List of values for the given list of keys. List will contain
2206 * null for keys which could not be found.
2208 * @throws RocksDBException thrown if error happens in underlying
2211 public List
<byte[]> multiGetAsList(final ReadOptions opt
,
2212 final List
<byte[]> keys
) throws RocksDBException
{
2213 assert(keys
.size() != 0);
2215 final byte[][] keysArray
= keys
.toArray(new byte[keys
.size()][]);
2216 final int[] keyOffsets
= new int[keysArray
.length
];
2217 final int[] keyLengths
= new int[keysArray
.length
];
2218 for(int i
= 0; i
< keyLengths
.length
; i
++) {
2219 keyLengths
[i
] = keysArray
[i
].length
;
2222 return Arrays
.asList(multiGet(nativeHandle_
, opt
.nativeHandle_
,
2223 keysArray
, keyOffsets
, keyLengths
));
2227 * Returns a list of values for the given list of keys. List will contain
2228 * null for keys which could not be found.
2230 * Note: Every key needs to have a related column family name in
2231 * {@code columnFamilyHandleList}.
2234 * @param opt Read options.
2235 * @param columnFamilyHandleList {@link java.util.List} containing
2236 * {@link org.rocksdb.ColumnFamilyHandle} instances.
2237 * @param keys of keys for which values need to be retrieved.
2238 * @return List of values for the given list of keys. List will contain
2239 * null for keys which could not be found.
2241 * @throws RocksDBException thrown if error happens in underlying
2243 * @throws IllegalArgumentException thrown if the size of passed keys is not
2244 * equal to the amount of passed column family handles.
2246 public List
<byte[]> multiGetAsList(final ReadOptions opt
,
2247 final List
<ColumnFamilyHandle
> columnFamilyHandleList
,
2248 final List
<byte[]> keys
) throws RocksDBException
{
2249 assert(keys
.size() != 0);
2250 // Check if key size equals cfList size. If not a exception must be
2251 // thrown. If not a Segmentation fault happens.
2252 if (keys
.size()!=columnFamilyHandleList
.size()){
2253 throw new IllegalArgumentException(
2254 "For each key there must be a ColumnFamilyHandle.");
2256 final long[] cfHandles
= new long[columnFamilyHandleList
.size()];
2257 for (int i
= 0; i
< columnFamilyHandleList
.size(); i
++) {
2258 cfHandles
[i
] = columnFamilyHandleList
.get(i
).nativeHandle_
;
2261 final byte[][] keysArray
= keys
.toArray(new byte[keys
.size()][]);
2262 final int[] keyOffsets
= new int[keysArray
.length
];
2263 final int[] keyLengths
= new int[keysArray
.length
];
2264 for(int i
= 0; i
< keyLengths
.length
; i
++) {
2265 keyLengths
[i
] = keysArray
[i
].length
;
2268 return Arrays
.asList(multiGet(nativeHandle_
, opt
.nativeHandle_
,
2269 keysArray
, keyOffsets
, keyLengths
, cfHandles
));
2273 * Fetches a list of values for the given list of keys, all from the default column family.
2275 * @param keys list of keys for which values need to be retrieved.
2276 * @param values list of buffers to return retrieved values in
2277 * @return list of number of bytes in DB for each requested key
2278 * this can be more than the size of the corresponding buffer; then the buffer will be filled
2279 * with the appropriate truncation of the database value.
2280 * @throws RocksDBException if error happens in underlying native library.
2281 * @throws IllegalArgumentException thrown if the number of passed keys and passed values
2284 public List
<ByteBufferGetStatus
> multiGetByteBuffers(
2285 final List
<ByteBuffer
> keys
, final List
<ByteBuffer
> values
) throws RocksDBException
{
2286 final ReadOptions readOptions
= new ReadOptions();
2287 final List
<ColumnFamilyHandle
> columnFamilyHandleList
= new ArrayList
<>(1);
2288 columnFamilyHandleList
.add(getDefaultColumnFamily());
2289 return multiGetByteBuffers(readOptions
, columnFamilyHandleList
, keys
, values
);
2293 * Fetches a list of values for the given list of keys, all from the default column family.
2295 * @param readOptions Read options
2296 * @param keys list of keys for which values need to be retrieved.
2297 * @param values list of buffers to return retrieved values in
2298 * @throws RocksDBException if error happens in underlying native library.
2299 * @throws IllegalArgumentException thrown if the number of passed keys and passed values
2301 * @return the list of values for the given list of keys
2303 public List
<ByteBufferGetStatus
> multiGetByteBuffers(final ReadOptions readOptions
,
2304 final List
<ByteBuffer
> keys
, final List
<ByteBuffer
> values
) throws RocksDBException
{
2305 final List
<ColumnFamilyHandle
> columnFamilyHandleList
= new ArrayList
<>(1);
2306 columnFamilyHandleList
.add(getDefaultColumnFamily());
2307 return multiGetByteBuffers(readOptions
, columnFamilyHandleList
, keys
, values
);
2311 * Fetches a list of values for the given list of keys.
2313 * Note: Every key needs to have a related column family name in
2314 * {@code columnFamilyHandleList}.
2317 * @param columnFamilyHandleList {@link java.util.List} containing
2318 * {@link org.rocksdb.ColumnFamilyHandle} instances.
2319 * @param keys list of keys for which values need to be retrieved.
2320 * @param values list of buffers to return retrieved values in
2321 * @throws RocksDBException if error happens in underlying native library.
2322 * @throws IllegalArgumentException thrown if the number of passed keys, passed values and
2323 * passed column family handles do not match.
2324 * @return the list of values for the given list of keys
2326 public List
<ByteBufferGetStatus
> multiGetByteBuffers(
2327 final List
<ColumnFamilyHandle
> columnFamilyHandleList
, final List
<ByteBuffer
> keys
,
2328 final List
<ByteBuffer
> values
) throws RocksDBException
{
2329 final ReadOptions readOptions
= new ReadOptions();
2330 return multiGetByteBuffers(readOptions
, columnFamilyHandleList
, keys
, values
);
2334 * Fetches a list of values for the given list of keys.
2336 * Note: Every key needs to have a related column family name in
2337 * {@code columnFamilyHandleList}.
2340 * @param readOptions Read options
2341 * @param columnFamilyHandleList {@link java.util.List} containing
2342 * {@link org.rocksdb.ColumnFamilyHandle} instances.
2343 * @param keys list of keys for which values need to be retrieved.
2344 * @param values list of buffers to return retrieved values in
2345 * @throws RocksDBException if error happens in underlying native library.
2346 * @throws IllegalArgumentException thrown if the number of passed keys, passed values and
2347 * passed column family handles do not match.
2348 * @return the list of values for the given list of keys
2350 public List
<ByteBufferGetStatus
> multiGetByteBuffers(final ReadOptions readOptions
,
2351 final List
<ColumnFamilyHandle
> columnFamilyHandleList
, final List
<ByteBuffer
> keys
,
2352 final List
<ByteBuffer
> values
) throws RocksDBException
{
2353 assert (keys
.size() != 0);
2355 // Check if key size equals cfList size. If not a exception must be
2356 // thrown. If not a Segmentation fault happens.
2357 if (keys
.size() != columnFamilyHandleList
.size() && columnFamilyHandleList
.size() > 1) {
2358 throw new IllegalArgumentException(
2359 "Wrong number of ColumnFamilyHandle(s) supplied. Provide 0, 1, or as many as there are key/value(s)");
2362 // Check if key size equals cfList size. If not a exception must be
2363 // thrown. If not a Segmentation fault happens.
2364 if (values
.size() != keys
.size()) {
2365 throw new IllegalArgumentException("For each key there must be a corresponding value.");
2368 // TODO (AP) support indirect buffers
2369 for (final ByteBuffer key
: keys
) {
2370 if (!key
.isDirect()) {
2371 throw new IllegalArgumentException("All key buffers must be direct byte buffers");
2375 // TODO (AP) support indirect buffers, though probably via a less efficient code path
2376 for (final ByteBuffer value
: values
) {
2377 if (!value
.isDirect()) {
2378 throw new IllegalArgumentException("All value buffers must be direct byte buffers");
2382 final int numCFHandles
= columnFamilyHandleList
.size();
2383 final long[] cfHandles
= new long[numCFHandles
];
2384 for (int i
= 0; i
< numCFHandles
; i
++) {
2385 cfHandles
[i
] = columnFamilyHandleList
.get(i
).nativeHandle_
;
2388 final int numValues
= keys
.size();
2390 final ByteBuffer
[] keysArray
= keys
.toArray(new ByteBuffer
[0]);
2391 final int[] keyOffsets
= new int[numValues
];
2392 final int[] keyLengths
= new int[numValues
];
2393 for (int i
= 0; i
< numValues
; i
++) {
2394 // TODO (AP) add keysArray[i].arrayOffset() if the buffer is indirect
2395 // TODO (AP) because in that case we have to pass the array directly,
2396 // so that the JNI C++ code will not know to compensate for the array offset
2397 keyOffsets
[i
] = keysArray
[i
].position();
2398 keyLengths
[i
] = keysArray
[i
].limit();
2400 final ByteBuffer
[] valuesArray
= values
.toArray(new ByteBuffer
[0]);
2401 final int[] valuesSizeArray
= new int[numValues
];
2402 final Status
[] statusArray
= new Status
[numValues
];
2404 multiGet(nativeHandle_
, readOptions
.nativeHandle_
, cfHandles
, keysArray
, keyOffsets
, keyLengths
,
2405 valuesArray
, valuesSizeArray
, statusArray
);
2407 final List
<ByteBufferGetStatus
> results
= new ArrayList
<>();
2408 for (int i
= 0; i
< numValues
; i
++) {
2409 final Status status
= statusArray
[i
];
2410 if (status
.getCode() == Status
.Code
.Ok
) {
2411 final ByteBuffer value
= valuesArray
[i
];
2412 value
.position(Math
.min(valuesSizeArray
[i
], value
.capacity()));
2413 value
.flip(); // prepare for read out
2414 results
.add(new ByteBufferGetStatus(status
, valuesSizeArray
[i
], value
));
2416 results
.add(new ByteBufferGetStatus(status
));
2424 * If the key definitely does not exist in the database, then this method
2425 * returns false, otherwise it returns true if the key might exist.
2426 * That is to say that this method is probabilistic and may return false
2427 * positives, but never a false negative.
2429 * If the caller wants to obtain value when the key
2430 * is found in memory, then {@code valueHolder} must be set.
2432 * This check is potentially lighter-weight than invoking
2433 * {@link #get(byte[])}. One way to make this lighter weight is to avoid
2436 * @param key byte array of a key to search for
2437 * @param valueHolder non-null to retrieve the value if it is found, or null
2438 * if the value is not needed. If non-null, upon return of the function,
2439 * the {@code value} will be set if it could be retrieved.
2441 * @return false if the key definitely does not exist in the database,
2444 public boolean keyMayExist(final byte[] key
,
2445 /* @Nullable */ final Holder
<byte[]> valueHolder
) {
2446 return keyMayExist(key
, 0, key
.length
, valueHolder
);
2450 * If the key definitely does not exist in the database, then this method
2451 * returns false, otherwise it returns true if the key might exist.
2452 * That is to say that this method is probabilistic and may return false
2453 * positives, but never a false negative.
2455 * If the caller wants to obtain value when the key
2456 * is found in memory, then {@code valueHolder} must be set.
2458 * This check is potentially lighter-weight than invoking
2459 * {@link #get(byte[], int, int)}. One way to make this lighter weight is to
2460 * avoid doing any IOs.
2462 * @param key byte array of a key to search for
2463 * @param offset the offset of the "key" array to be used, must be
2464 * non-negative and no larger than "key".length
2465 * @param len the length of the "key" array to be used, must be non-negative
2466 * and no larger than "key".length
2467 * @param valueHolder non-null to retrieve the value if it is found, or null
2468 * if the value is not needed. If non-null, upon return of the function,
2469 * the {@code value} will be set if it could be retrieved.
2471 * @return false if the key definitely does not exist in the database,
2474 public boolean keyMayExist(final byte[] key
,
2475 final int offset
, final int len
,
2476 /* @Nullable */ final Holder
<byte[]> valueHolder
) {
2477 return keyMayExist((ColumnFamilyHandle
)null, key
, offset
, len
, valueHolder
);
2481 * If the key definitely does not exist in the database, then this method
2482 * returns false, otherwise it returns true if the key might exist.
2483 * That is to say that this method is probabilistic and may return false
2484 * positives, but never a false negative.
2486 * If the caller wants to obtain value when the key
2487 * is found in memory, then {@code valueHolder} must be set.
2489 * This check is potentially lighter-weight than invoking
2490 * {@link #get(ColumnFamilyHandle,byte[])}. One way to make this lighter
2491 * weight is to avoid doing any IOs.
2493 * @param columnFamilyHandle {@link ColumnFamilyHandle} instance
2494 * @param key byte array of a key to search for
2495 * @param valueHolder non-null to retrieve the value if it is found, or null
2496 * if the value is not needed. If non-null, upon return of the function,
2497 * the {@code value} will be set if it could be retrieved.
2499 * @return false if the key definitely does not exist in the database,
2502 public boolean keyMayExist(
2503 final ColumnFamilyHandle columnFamilyHandle
, final byte[] key
,
2504 /* @Nullable */ final Holder
<byte[]> valueHolder
) {
2505 return keyMayExist(columnFamilyHandle
, key
, 0, key
.length
,
2510 * If the key definitely does not exist in the database, then this method
2511 * returns false, otherwise it returns true if the key might exist.
2512 * That is to say that this method is probabilistic and may return false
2513 * positives, but never a false negative.
2515 * If the caller wants to obtain value when the key
2516 * is found in memory, then {@code valueHolder} must be set.
2518 * This check is potentially lighter-weight than invoking
2519 * {@link #get(ColumnFamilyHandle, byte[], int, int)}. One way to make this
2520 * lighter weight is to avoid doing any IOs.
2522 * @param columnFamilyHandle {@link ColumnFamilyHandle} instance
2523 * @param key byte array of a key to search for
2524 * @param offset the offset of the "key" array to be used, must be
2525 * non-negative and no larger than "key".length
2526 * @param len the length of the "key" array to be used, must be non-negative
2527 * and no larger than "key".length
2528 * @param valueHolder non-null to retrieve the value if it is found, or null
2529 * if the value is not needed. If non-null, upon return of the function,
2530 * the {@code value} will be set if it could be retrieved.
2532 * @return false if the key definitely does not exist in the database,
2535 public boolean keyMayExist(
2536 final ColumnFamilyHandle columnFamilyHandle
,
2537 final byte[] key
, int offset
, int len
,
2538 /* @Nullable */ final Holder
<byte[]> valueHolder
) {
2539 return keyMayExist(columnFamilyHandle
, null, key
, offset
, len
,
2544 * If the key definitely does not exist in the database, then this method
2545 * returns false, otherwise it returns true if the key might exist.
2546 * That is to say that this method is probabilistic and may return false
2547 * positives, but never a true negative.
2549 * If the caller wants to obtain value when the key
2550 * is found in memory, then {@code valueHolder} must be set.
2552 * This check is potentially lighter-weight than invoking
2553 * {@link #get(ReadOptions, byte[])}. One way to make this
2554 * lighter weight is to avoid doing any IOs.
2556 * @param readOptions {@link ReadOptions} instance
2557 * @param key byte array of a key to search for
2558 * @param valueHolder non-null to retrieve the value if it is found, or null
2559 * if the value is not needed. If non-null, upon return of the function,
2560 * the {@code value} will be set if it could be retrieved.
2562 * @return false if the key definitely does not exist in the database,
2565 public boolean keyMayExist(
2566 final ReadOptions readOptions
, final byte[] key
,
2567 /* @Nullable */ final Holder
<byte[]> valueHolder
) {
2568 return keyMayExist(readOptions
, key
, 0, key
.length
,
2573 * If the key definitely does not exist in the database, then this method
2574 * returns false, otherwise it returns true if the key might exist.
2575 * That is to say that this method is probabilistic and may return false
2576 * positives, but never a true negative.
2578 * If the caller wants to obtain value when the key
2579 * is found in memory, then {@code valueHolder} must be set.
2581 * This check is potentially lighter-weight than invoking
2582 * {@link #get(ReadOptions, byte[], int, int)}. One way to make this
2583 * lighter weight is to avoid doing any IOs.
2585 * @param readOptions {@link ReadOptions} instance
2586 * @param key byte array of a key to search for
2587 * @param offset the offset of the "key" array to be used, must be
2588 * non-negative and no larger than "key".length
2589 * @param len the length of the "key" array to be used, must be non-negative
2590 * and no larger than "key".length
2591 * @param valueHolder non-null to retrieve the value if it is found, or null
2592 * if the value is not needed. If non-null, upon return of the function,
2593 * the {@code value} will be set if it could be retrieved.
2595 * @return false if the key definitely does not exist in the database,
2598 public boolean keyMayExist(
2599 final ReadOptions readOptions
,
2600 final byte[] key
, final int offset
, final int len
,
2601 /* @Nullable */ final Holder
<byte[]> valueHolder
) {
2602 return keyMayExist(null, readOptions
,
2603 key
, offset
, len
, valueHolder
);
2607 * If the key definitely does not exist in the database, then this method
2608 * returns false, otherwise it returns true if the key might exist.
2609 * That is to say that this method is probabilistic and may return false
2610 * positives, but never a true negative.
2612 * If the caller wants to obtain value when the key
2613 * is found in memory, then {@code valueHolder} must be set.
2615 * This check is potentially lighter-weight than invoking
2616 * {@link #get(ColumnFamilyHandle, ReadOptions, byte[])}. One way to make this
2617 * lighter weight is to avoid doing any IOs.
2619 * @param columnFamilyHandle {@link ColumnFamilyHandle} instance
2620 * @param readOptions {@link ReadOptions} instance
2621 * @param key byte array of a key to search for
2622 * @param valueHolder non-null to retrieve the value if it is found, or null
2623 * if the value is not needed. If non-null, upon return of the function,
2624 * the {@code value} will be set if it could be retrieved.
2626 * @return false if the key definitely does not exist in the database,
2629 public boolean keyMayExist(
2630 final ColumnFamilyHandle columnFamilyHandle
,
2631 final ReadOptions readOptions
, final byte[] key
,
2632 /* @Nullable */ final Holder
<byte[]> valueHolder
) {
2633 return keyMayExist(columnFamilyHandle
, readOptions
,
2634 key
, 0, key
.length
, valueHolder
);
2638 * If the key definitely does not exist in the database, then this method
2639 * returns false, otherwise it returns true if the key might exist.
2640 * That is to say that this method is probabilistic and may return false
2641 * positives, but never a false negative.
2643 * If the caller wants to obtain value when the key
2644 * is found in memory, then {@code valueHolder} must be set.
2646 * This check is potentially lighter-weight than invoking
2647 * {@link #get(ColumnFamilyHandle, ReadOptions, byte[], int, int)}.
2648 * One way to make this lighter weight is to avoid doing any IOs.
2650 * @param columnFamilyHandle {@link ColumnFamilyHandle} instance
2651 * @param readOptions {@link ReadOptions} instance
2652 * @param key byte array of a key to search for
2653 * @param offset the offset of the "key" array to be used, must be
2654 * non-negative and no larger than "key".length
2655 * @param len the length of the "key" array to be used, must be non-negative
2656 * and no larger than "key".length
2657 * @param valueHolder non-null to retrieve the value if it is found, or null
2658 * if the value is not needed. If non-null, upon return of the function,
2659 * the {@code value} will be set if it could be retrieved.
2661 * @return false if the key definitely does not exist in the database,
2664 public boolean keyMayExist(
2665 final ColumnFamilyHandle columnFamilyHandle
,
2666 final ReadOptions readOptions
,
2667 final byte[] key
, final int offset
, final int len
,
2668 /* @Nullable */ final Holder
<byte[]> valueHolder
) {
2669 checkBounds(offset
, len
, key
.length
);
2670 if (valueHolder
== null) {
2671 return keyMayExist(nativeHandle_
,
2672 columnFamilyHandle
== null ?
0 : columnFamilyHandle
.nativeHandle_
,
2673 readOptions
== null ?
0 : readOptions
.nativeHandle_
,
2676 final byte[][] result
= keyMayExistFoundValue(
2678 columnFamilyHandle
== null ?
0 : columnFamilyHandle
.nativeHandle_
,
2679 readOptions
== null ?
0 : readOptions
.nativeHandle_
,
2681 if (result
[0][0] == 0x0) {
2682 valueHolder
.setValue(null);
2684 } else if (result
[0][0] == 0x1) {
2685 valueHolder
.setValue(null);
2688 valueHolder
.setValue(result
[1]);
2695 * If the key definitely does not exist in the database, then this method
2696 * returns false, otherwise it returns true if the key might exist.
2697 * That is to say that this method is probabilistic and may return false
2698 * positives, but never a false negative.
2700 * @param key bytebuffer containing the value of the key
2701 * @return false if the key definitely does not exist in the database,
2704 public boolean keyMayExist(final ByteBuffer key
) {
2705 return keyMayExist(null, (ReadOptions
) null, key
);
2709 * If the key definitely does not exist in the database, then this method
2710 * returns false, otherwise it returns true if the key might exist.
2711 * That is to say that this method is probabilistic and may return false
2712 * positives, but never a false negative.
2714 * @param columnFamilyHandle the {@link ColumnFamilyHandle} to look for the key in
2715 * @param key bytebuffer containing the value of the key
2716 * @return false if the key definitely does not exist in the database,
2719 public boolean keyMayExist(final ColumnFamilyHandle columnFamilyHandle
, final ByteBuffer key
) {
2720 return keyMayExist(columnFamilyHandle
, (ReadOptions
) null, key
);
2724 * If the key definitely does not exist in the database, then this method
2725 * returns false, otherwise it returns true if the key might exist.
2726 * That is to say that this method is probabilistic and may return false
2727 * positives, but never a false negative.
2729 * @param readOptions the {@link ReadOptions} to use when reading the key/value
2730 * @param key bytebuffer containing the value of the key
2731 * @return false if the key definitely does not exist in the database,
2734 public boolean keyMayExist(final ReadOptions readOptions
, final ByteBuffer key
) {
2735 return keyMayExist(null, readOptions
, key
);
2739 * If the key definitely does not exist in the database, then this method
2740 * returns {@link KeyMayExist.KeyMayExistEnum#kNotExist},
2741 * otherwise if it can with best effort retreive the value, it returns {@link
2742 * KeyMayExist.KeyMayExistEnum#kExistsWithValue} otherwise it returns {@link
2743 * KeyMayExist.KeyMayExistEnum#kExistsWithoutValue}. The choice not to return a value which might
2744 * exist is at the discretion of the implementation; the only guarantee is that {@link
2745 * KeyMayExist.KeyMayExistEnum#kNotExist} is an assurance that the key does not exist.
2747 * @param key bytebuffer containing the value of the key
2748 * @param value bytebuffer which will receive a value if the key exists and a value is known
2749 * @return a {@link KeyMayExist} object reporting if key may exist and if a value is provided
2751 public KeyMayExist
keyMayExist(final ByteBuffer key
, final ByteBuffer value
) {
2752 return keyMayExist(null, null, key
, value
);
2756 * If the key definitely does not exist in the database, then this method
2757 * returns {@link KeyMayExist.KeyMayExistEnum#kNotExist},
2758 * otherwise if it can with best effort retreive the value, it returns {@link
2759 * KeyMayExist.KeyMayExistEnum#kExistsWithValue} otherwise it returns {@link
2760 * KeyMayExist.KeyMayExistEnum#kExistsWithoutValue}. The choice not to return a value which might
2761 * exist is at the discretion of the implementation; the only guarantee is that {@link
2762 * KeyMayExist.KeyMayExistEnum#kNotExist} is an assurance that the key does not exist.
2764 * @param columnFamilyHandle the {@link ColumnFamilyHandle} to look for the key in
2765 * @param key bytebuffer containing the value of the key
2766 * @param value bytebuffer which will receive a value if the key exists and a value is known
2767 * @return a {@link KeyMayExist} object reporting if key may exist and if a value is provided
2769 public KeyMayExist
keyMayExist(
2770 final ColumnFamilyHandle columnFamilyHandle
, final ByteBuffer key
, final ByteBuffer value
) {
2771 return keyMayExist(columnFamilyHandle
, null, key
, value
);
2775 * If the key definitely does not exist in the database, then this method
2776 * returns {@link KeyMayExist.KeyMayExistEnum#kNotExist},
2777 * otherwise if it can with best effort retreive the value, it returns {@link
2778 * KeyMayExist.KeyMayExistEnum#kExistsWithValue} otherwise it returns {@link
2779 * KeyMayExist.KeyMayExistEnum#kExistsWithoutValue}. The choice not to return a value which might
2780 * exist is at the discretion of the implementation; the only guarantee is that {@link
2781 * KeyMayExist.KeyMayExistEnum#kNotExist} is an assurance that the key does not exist.
2783 * @param readOptions the {@link ReadOptions} to use when reading the key/value
2784 * @param key bytebuffer containing the value of the key
2785 * @param value bytebuffer which will receive a value if the key exists and a value is known
2786 * @return a {@link KeyMayExist} object reporting if key may exist and if a value is provided
2788 public KeyMayExist
keyMayExist(
2789 final ReadOptions readOptions
, final ByteBuffer key
, final ByteBuffer value
) {
2790 return keyMayExist(null, readOptions
, key
, value
);
2794 * If the key definitely does not exist in the database, then this method
2795 * returns false, otherwise it returns true if the key might exist.
2796 * That is to say that this method is probabilistic and may return false
2797 * positives, but never a false negative.
2799 * @param columnFamilyHandle the {@link ColumnFamilyHandle} to look for the key in
2800 * @param readOptions the {@link ReadOptions} to use when reading the key/value
2801 * @param key bytebuffer containing the value of the key
2802 * @return false if the key definitely does not exist in the database,
2805 public boolean keyMayExist(final ColumnFamilyHandle columnFamilyHandle
,
2806 final ReadOptions readOptions
, final ByteBuffer key
) {
2807 assert key
!= null : "key ByteBuffer parameter cannot be null";
2808 assert key
.isDirect() : "key parameter must be a direct ByteBuffer";
2809 return keyMayExistDirect(nativeHandle_
,
2810 columnFamilyHandle
== null ?
0 : columnFamilyHandle
.nativeHandle_
,
2811 readOptions
== null ?
0 : readOptions
.nativeHandle_
, key
, key
.position(), key
.limit());
2815 * If the key definitely does not exist in the database, then this method
2816 * returns {@link KeyMayExist.KeyMayExistEnum#kNotExist},
2817 * otherwise if it can with best effort retreive the value, it returns {@link
2818 * KeyMayExist.KeyMayExistEnum#kExistsWithValue} otherwise it returns {@link
2819 * KeyMayExist.KeyMayExistEnum#kExistsWithoutValue}. The choice not to return a value which might
2820 * exist is at the discretion of the implementation; the only guarantee is that {@link
2821 * KeyMayExist.KeyMayExistEnum#kNotExist} is an assurance that the key does not exist.
2823 * @param columnFamilyHandle the {@link ColumnFamilyHandle} to look for the key in
2824 * @param readOptions the {@link ReadOptions} to use when reading the key/value
2825 * @param key bytebuffer containing the value of the key
2826 * @param value bytebuffer which will receive a value if the key exists and a value is known
2827 * @return a {@link KeyMayExist} object reporting if key may exist and if a value is provided
2829 public KeyMayExist
keyMayExist(final ColumnFamilyHandle columnFamilyHandle
,
2830 final ReadOptions readOptions
, final ByteBuffer key
, final ByteBuffer value
) {
2831 assert key
!= null : "key ByteBuffer parameter cannot be null";
2832 assert key
.isDirect() : "key parameter must be a direct ByteBuffer";
2835 : "value ByteBuffer parameter cannot be null. If you do not need the value, use a different version of the method";
2836 assert value
.isDirect() : "value parameter must be a direct ByteBuffer";
2838 final int[] result
= keyMayExistDirectFoundValue(nativeHandle_
,
2839 columnFamilyHandle
== null ?
0 : columnFamilyHandle
.nativeHandle_
,
2840 readOptions
== null ?
0 : readOptions
.nativeHandle_
, key
, key
.position(), key
.remaining(),
2841 value
, value
.position(), value
.remaining());
2842 final int valueLength
= result
[1];
2843 value
.limit(value
.position() + Math
.min(valueLength
, value
.remaining()));
2844 return new KeyMayExist(KeyMayExist
.KeyMayExistEnum
.values()[result
[0]], valueLength
);
2848 * <p>Return a heap-allocated iterator over the contents of the
2849 * database. The result of newIterator() is initially invalid
2850 * (caller must call one of the Seek methods on the iterator
2851 * before using it).</p>
2853 * <p>Caller should close the iterator when it is no longer needed.
2854 * The returned iterator should be closed before this db is closed.
2857 * @return instance of iterator object.
2859 public RocksIterator
newIterator() {
2860 return new RocksIterator(this, iterator(nativeHandle_
));
2864 * <p>Return a heap-allocated iterator over the contents of the
2865 * database. The result of newIterator() is initially invalid
2866 * (caller must call one of the Seek methods on the iterator
2867 * before using it).</p>
2869 * <p>Caller should close the iterator when it is no longer needed.
2870 * The returned iterator should be closed before this db is closed.
2873 * @param readOptions {@link ReadOptions} instance.
2874 * @return instance of iterator object.
2876 public RocksIterator
newIterator(final ReadOptions readOptions
) {
2877 return new RocksIterator(this, iterator(nativeHandle_
,
2878 readOptions
.nativeHandle_
));
2882 * <p>Return a heap-allocated iterator over the contents of a
2883 * ColumnFamily. The result of newIterator() is initially invalid
2884 * (caller must call one of the Seek methods on the iterator
2885 * before using it).</p>
2887 * <p>Caller should close the iterator when it is no longer needed.
2888 * The returned iterator should be closed before this db is closed.
2891 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
2893 * @return instance of iterator object.
2895 public RocksIterator
newIterator(
2896 final ColumnFamilyHandle columnFamilyHandle
) {
2897 return new RocksIterator(this, iteratorCF(nativeHandle_
,
2898 columnFamilyHandle
.nativeHandle_
));
2902 * <p>Return a heap-allocated iterator over the contents of a
2903 * ColumnFamily. The result of newIterator() is initially invalid
2904 * (caller must call one of the Seek methods on the iterator
2905 * before using it).</p>
2907 * <p>Caller should close the iterator when it is no longer needed.
2908 * The returned iterator should be closed before this db is closed.
2911 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
2913 * @param readOptions {@link ReadOptions} instance.
2914 * @return instance of iterator object.
2916 public RocksIterator
newIterator(final ColumnFamilyHandle columnFamilyHandle
,
2917 final ReadOptions readOptions
) {
2918 return new RocksIterator(this, iteratorCF(nativeHandle_
,
2919 columnFamilyHandle
.nativeHandle_
, readOptions
.nativeHandle_
));
2923 * Returns iterators from a consistent database state across multiple
2924 * column families. Iterators are heap allocated and need to be deleted
2925 * before the db is deleted
2927 * @param columnFamilyHandleList {@link java.util.List} containing
2928 * {@link org.rocksdb.ColumnFamilyHandle} instances.
2929 * @return {@link java.util.List} containing {@link org.rocksdb.RocksIterator}
2932 * @throws RocksDBException thrown if error happens in underlying
2935 public List
<RocksIterator
> newIterators(
2936 final List
<ColumnFamilyHandle
> columnFamilyHandleList
)
2937 throws RocksDBException
{
2938 return newIterators(columnFamilyHandleList
, new ReadOptions());
2942 * Returns iterators from a consistent database state across multiple
2943 * column families. Iterators are heap allocated and need to be deleted
2944 * before the db is deleted
2946 * @param columnFamilyHandleList {@link java.util.List} containing
2947 * {@link org.rocksdb.ColumnFamilyHandle} instances.
2948 * @param readOptions {@link ReadOptions} instance.
2949 * @return {@link java.util.List} containing {@link org.rocksdb.RocksIterator}
2952 * @throws RocksDBException thrown if error happens in underlying
2955 public List
<RocksIterator
> newIterators(
2956 final List
<ColumnFamilyHandle
> columnFamilyHandleList
,
2957 final ReadOptions readOptions
) throws RocksDBException
{
2959 final long[] columnFamilyHandles
= new long[columnFamilyHandleList
.size()];
2960 for (int i
= 0; i
< columnFamilyHandleList
.size(); i
++) {
2961 columnFamilyHandles
[i
] = columnFamilyHandleList
.get(i
).nativeHandle_
;
2964 final long[] iteratorRefs
= iterators(nativeHandle_
, columnFamilyHandles
,
2965 readOptions
.nativeHandle_
);
2967 final List
<RocksIterator
> iterators
= new ArrayList
<>(
2968 columnFamilyHandleList
.size());
2969 for (int i
=0; i
<columnFamilyHandleList
.size(); i
++){
2970 iterators
.add(new RocksIterator(this, iteratorRefs
[i
]));
2977 * <p>Return a handle to the current DB state. Iterators created with
2978 * this handle will all observe a stable snapshot of the current DB
2979 * state. The caller must call ReleaseSnapshot(result) when the
2980 * snapshot is no longer needed.</p>
2982 * <p>nullptr will be returned if the DB fails to take a snapshot or does
2983 * not support snapshot.</p>
2985 * @return Snapshot {@link Snapshot} instance
2987 public Snapshot
getSnapshot() {
2988 long snapshotHandle
= getSnapshot(nativeHandle_
);
2989 if (snapshotHandle
!= 0) {
2990 return new Snapshot(snapshotHandle
);
2996 * Release a previously acquired snapshot.
2998 * The caller must not use "snapshot" after this call.
3000 * @param snapshot {@link Snapshot} instance
3002 public void releaseSnapshot(final Snapshot snapshot
) {
3003 if (snapshot
!= null) {
3004 releaseSnapshot(nativeHandle_
, snapshot
.nativeHandle_
);
3009 * DB implements can export properties about their state
3010 * via this method on a per column family level.
3012 * <p>If {@code property} is a valid property understood by this DB
3013 * implementation, fills {@code value} with its current value and
3014 * returns true. Otherwise returns false.</p>
3016 * <p>Valid property names include:
3018 * <li>"rocksdb.num-files-at-level<N>" - return the number of files at
3019 * level <N>, where <N> is an ASCII representation of a level
3020 * number (e.g. "0").</li>
3021 * <li>"rocksdb.stats" - returns a multi-line string that describes statistics
3022 * about the internal operation of the DB.</li>
3023 * <li>"rocksdb.sstables" - returns a multi-line string that describes all
3024 * of the sstables that make up the db contents.</li>
3027 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
3028 * instance, or null for the default column family.
3029 * @param property to be fetched. See above for examples
3030 * @return property value
3032 * @throws RocksDBException thrown if error happens in underlying
3035 public String
getProperty(
3036 /* @Nullable */ final ColumnFamilyHandle columnFamilyHandle
,
3037 final String property
) throws RocksDBException
{
3038 return getProperty(nativeHandle_
,
3039 columnFamilyHandle
== null ?
0 : columnFamilyHandle
.nativeHandle_
,
3040 property
, property
.length());
3044 * DB implementations can export properties about their state
3045 * via this method. If "property" is a valid property understood by this
3046 * DB implementation, fills "*value" with its current value and returns
3047 * true. Otherwise returns false.
3049 * <p>Valid property names include:
3051 * <li>"rocksdb.num-files-at-level<N>" - return the number of files at
3052 * level <N>, where <N> is an ASCII representation of a level
3053 * number (e.g. "0").</li>
3054 * <li>"rocksdb.stats" - returns a multi-line string that describes statistics
3055 * about the internal operation of the DB.</li>
3056 * <li>"rocksdb.sstables" - returns a multi-line string that describes all
3057 * of the sstables that make up the db contents.</li>
3060 * @param property to be fetched. See above for examples
3061 * @return property value
3063 * @throws RocksDBException thrown if error happens in underlying
3066 public String
getProperty(final String property
) throws RocksDBException
{
3067 return getProperty(null, property
);
3072 * Gets a property map.
3074 * @param property to be fetched.
3076 * @return the property map
3078 * @throws RocksDBException if an error happens in the underlying native code.
3080 public Map
<String
, String
> getMapProperty(final String property
)
3081 throws RocksDBException
{
3082 return getMapProperty(null, property
);
3086 * Gets a property map.
3088 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
3089 * instance, or null for the default column family.
3090 * @param property to be fetched.
3092 * @return the property map
3094 * @throws RocksDBException if an error happens in the underlying native code.
3096 public Map
<String
, String
> getMapProperty(
3097 /* @Nullable */ final ColumnFamilyHandle columnFamilyHandle
,
3098 final String property
) throws RocksDBException
{
3099 return getMapProperty(nativeHandle_
,
3100 columnFamilyHandle
== null ?
0 : columnFamilyHandle
.nativeHandle_
,
3101 property
, property
.length());
3105 * <p> Similar to GetProperty(), but only works for a subset of properties
3106 * whose return value is a numerical value. Return the value as long.</p>
3108 * <p><strong>Note</strong>: As the returned property is of type
3109 * {@code uint64_t} on C++ side the returning value can be negative
3110 * because Java supports in Java 7 only signed long values.</p>
3112 * <p><strong>Java 7</strong>: To mitigate the problem of the non
3113 * existent unsigned long tpye, values should be encapsulated using
3114 * {@link java.math.BigInteger} to reflect the correct value. The correct
3115 * behavior is guaranteed if {@code 2^64} is added to negative values.</p>
3117 * <p><strong>Java 8</strong>: In Java 8 the value should be treated as
3118 * unsigned long using provided methods of type {@link Long}.</p>
3120 * @param property to be fetched.
3122 * @return numerical property value.
3124 * @throws RocksDBException if an error happens in the underlying native code.
3126 public long getLongProperty(final String property
) throws RocksDBException
{
3127 return getLongProperty(null, property
);
3131 * <p> Similar to GetProperty(), but only works for a subset of properties
3132 * whose return value is a numerical value. Return the value as long.</p>
3134 * <p><strong>Note</strong>: As the returned property is of type
3135 * {@code uint64_t} on C++ side the returning value can be negative
3136 * because Java supports in Java 7 only signed long values.</p>
3138 * <p><strong>Java 7</strong>: To mitigate the problem of the non
3139 * existent unsigned long tpye, values should be encapsulated using
3140 * {@link java.math.BigInteger} to reflect the correct value. The correct
3141 * behavior is guaranteed if {@code 2^64} is added to negative values.</p>
3143 * <p><strong>Java 8</strong>: In Java 8 the value should be treated as
3144 * unsigned long using provided methods of type {@link Long}.</p>
3146 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
3147 * instance, or null for the default column family
3148 * @param property to be fetched.
3150 * @return numerical property value
3152 * @throws RocksDBException if an error happens in the underlying native code.
3154 public long getLongProperty(
3155 /* @Nullable */ final ColumnFamilyHandle columnFamilyHandle
,
3156 final String property
) throws RocksDBException
{
3157 return getLongProperty(nativeHandle_
,
3158 columnFamilyHandle
== null ?
0 : columnFamilyHandle
.nativeHandle_
,
3159 property
, property
.length());
3163 * Reset internal stats for DB and all column families.
3165 * Note this doesn't reset {@link Options#statistics()} as it is not
3168 * @throws RocksDBException if an error occurs whilst reseting the stats
3170 public void resetStats() throws RocksDBException
{
3171 resetStats(nativeHandle_
);
3175 * <p> Return sum of the getLongProperty of all the column families</p>
3177 * <p><strong>Note</strong>: As the returned property is of type
3178 * {@code uint64_t} on C++ side the returning value can be negative
3179 * because Java supports in Java 7 only signed long values.</p>
3181 * <p><strong>Java 7</strong>: To mitigate the problem of the non
3182 * existent unsigned long tpye, values should be encapsulated using
3183 * {@link java.math.BigInteger} to reflect the correct value. The correct
3184 * behavior is guaranteed if {@code 2^64} is added to negative values.</p>
3186 * <p><strong>Java 8</strong>: In Java 8 the value should be treated as
3187 * unsigned long using provided methods of type {@link Long}.</p>
3189 * @param property to be fetched.
3191 * @return numerical property value
3193 * @throws RocksDBException if an error happens in the underlying native code.
3195 public long getAggregatedLongProperty(final String property
)
3196 throws RocksDBException
{
3197 return getAggregatedLongProperty(nativeHandle_
, property
,
3202 * Get the approximate file system space used by keys in each range.
3204 * Note that the returned sizes measure file system space usage, so
3205 * if the user data compresses by a factor of ten, the returned
3206 * sizes will be one-tenth the size of the corresponding user data size.
3208 * If {@code sizeApproximationFlags} defines whether the returned size
3209 * should include the recently written data in the mem-tables (if
3210 * the mem-table type supports it), data serialized to disk, or both.
3212 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
3213 * instance, or null for the default column family
3214 * @param ranges the ranges over which to approximate sizes
3215 * @param sizeApproximationFlags flags to determine what to include in the
3220 public long[] getApproximateSizes(
3221 /*@Nullable*/ final ColumnFamilyHandle columnFamilyHandle
,
3222 final List
<Range
> ranges
,
3223 final SizeApproximationFlag
... sizeApproximationFlags
) {
3226 for (final SizeApproximationFlag sizeApproximationFlag
3227 : sizeApproximationFlags
) {
3228 flags
|= sizeApproximationFlag
.getValue();
3231 return getApproximateSizes(nativeHandle_
,
3232 columnFamilyHandle
== null ?
0 : columnFamilyHandle
.nativeHandle_
,
3233 toRangeSliceHandles(ranges
), flags
);
3237 * Get the approximate file system space used by keys in each range for
3238 * the default column family.
3240 * Note that the returned sizes measure file system space usage, so
3241 * if the user data compresses by a factor of ten, the returned
3242 * sizes will be one-tenth the size of the corresponding user data size.
3244 * If {@code sizeApproximationFlags} defines whether the returned size
3245 * should include the recently written data in the mem-tables (if
3246 * the mem-table type supports it), data serialized to disk, or both.
3248 * @param ranges the ranges over which to approximate sizes
3249 * @param sizeApproximationFlags flags to determine what to include in the
3252 * @return the sizes.
3254 public long[] getApproximateSizes(final List
<Range
> ranges
,
3255 final SizeApproximationFlag
... sizeApproximationFlags
) {
3256 return getApproximateSizes(null, ranges
, sizeApproximationFlags
);
3259 public static class CountAndSize
{
3260 public final long count
;
3261 public final long size
;
3263 public CountAndSize(final long count
, final long size
) {
3270 * This method is similar to
3271 * {@link #getApproximateSizes(ColumnFamilyHandle, List, SizeApproximationFlag...)},
3272 * except that it returns approximate number of records and size in memtables.
3274 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
3275 * instance, or null for the default column family
3276 * @param range the ranges over which to get the memtable stats
3278 * @return the count and size for the range
3280 public CountAndSize
getApproximateMemTableStats(
3281 /*@Nullable*/ final ColumnFamilyHandle columnFamilyHandle
,
3282 final Range range
) {
3283 final long[] result
= getApproximateMemTableStats(nativeHandle_
,
3284 columnFamilyHandle
== null ?
0 : columnFamilyHandle
.nativeHandle_
,
3285 range
.start
.getNativeHandle(),
3286 range
.limit
.getNativeHandle());
3287 return new CountAndSize(result
[0], result
[1]);
3291 * This method is similar to
3292 * {@link #getApproximateSizes(ColumnFamilyHandle, List, SizeApproximationFlag...)},
3293 * except that it returns approximate number of records and size in memtables.
3295 * @param range the ranges over which to get the memtable stats
3297 * @return the count and size for the range
3299 public CountAndSize
getApproximateMemTableStats(
3300 final Range range
) {
3301 return getApproximateMemTableStats(null, range
);
3305 * <p>Range compaction of database.</p>
3306 * <p><strong>Note</strong>: After the database has been compacted,
3307 * all data will have been pushed down to the last level containing
3310 * <p><strong>See also</strong></p>
3312 * <li>{@link #compactRange(byte[], byte[])}</li>
3315 * @throws RocksDBException thrown if an error occurs within the native
3316 * part of the library.
3318 public void compactRange() throws RocksDBException
{
3323 * <p>Range compaction of column family.</p>
3324 * <p><strong>Note</strong>: After the database has been compacted,
3325 * all data will have been pushed down to the last level containing
3328 * <p><strong>See also</strong></p>
3331 * {@link #compactRange(ColumnFamilyHandle, byte[], byte[])}
3335 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
3336 * instance, or null for the default column family.
3338 * @throws RocksDBException thrown if an error occurs within the native
3339 * part of the library.
3341 public void compactRange(
3342 /* @Nullable */ final ColumnFamilyHandle columnFamilyHandle
)
3343 throws RocksDBException
{
3344 compactRange(nativeHandle_
, null, -1, null, -1, 0,
3345 columnFamilyHandle
== null ?
0 : columnFamilyHandle
.nativeHandle_
);
3349 * <p>Range compaction of database.</p>
3350 * <p><strong>Note</strong>: After the database has been compacted,
3351 * all data will have been pushed down to the last level containing
3354 * <p><strong>See also</strong></p>
3356 * <li>{@link #compactRange()}</li>
3359 * @param begin start of key range (included in range)
3360 * @param end end of key range (excluded from range)
3362 * @throws RocksDBException thrown if an error occurs within the native
3363 * part of the library.
3365 public void compactRange(final byte[] begin
, final byte[] end
)
3366 throws RocksDBException
{
3367 compactRange(null, begin
, end
);
3371 * <p>Range compaction of column family.</p>
3372 * <p><strong>Note</strong>: After the database has been compacted,
3373 * all data will have been pushed down to the last level containing
3376 * <p><strong>See also</strong></p>
3378 * <li>{@link #compactRange(ColumnFamilyHandle)}</li>
3381 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
3382 * instance, or null for the default column family.
3383 * @param begin start of key range (included in range)
3384 * @param end end of key range (excluded from range)
3386 * @throws RocksDBException thrown if an error occurs within the native
3387 * part of the library.
3389 public void compactRange(
3390 /* @Nullable */ final ColumnFamilyHandle columnFamilyHandle
,
3391 final byte[] begin
, final byte[] end
) throws RocksDBException
{
3392 compactRange(nativeHandle_
,
3393 begin
, begin
== null ?
-1 : begin
.length
,
3394 end
, end
== null ?
-1 : end
.length
,
3395 0, columnFamilyHandle
== null ?
0: columnFamilyHandle
.nativeHandle_
);
3399 * <p>Range compaction of column family.</p>
3400 * <p><strong>Note</strong>: After the database has been compacted,
3401 * all data will have been pushed down to the last level containing
3404 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} instance.
3405 * @param begin start of key range (included in range)
3406 * @param end end of key range (excluded from range)
3407 * @param compactRangeOptions options for the compaction
3409 * @throws RocksDBException thrown if an error occurs within the native
3410 * part of the library.
3412 public void compactRange(
3413 /* @Nullable */ final ColumnFamilyHandle columnFamilyHandle
,
3414 final byte[] begin
, final byte[] end
,
3415 final CompactRangeOptions compactRangeOptions
) throws RocksDBException
{
3416 compactRange(nativeHandle_
,
3417 begin
, begin
== null ?
-1 : begin
.length
,
3418 end
, end
== null ?
-1 : end
.length
,
3419 compactRangeOptions
.nativeHandle_
,
3420 columnFamilyHandle
== null ?
0 : columnFamilyHandle
.nativeHandle_
);
3424 * Change the options for the column family handle.
3426 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
3427 * instance, or null for the default column family.
3428 * @param mutableColumnFamilyOptions the options.
3430 * @throws RocksDBException if an error occurs whilst setting the options
3432 public void setOptions(
3433 /* @Nullable */final ColumnFamilyHandle columnFamilyHandle
,
3434 final MutableColumnFamilyOptions mutableColumnFamilyOptions
)
3435 throws RocksDBException
{
3436 setOptions(nativeHandle_
, columnFamilyHandle
== null ?
0 : columnFamilyHandle
.nativeHandle_
,
3437 mutableColumnFamilyOptions
.getKeys(), mutableColumnFamilyOptions
.getValues());
3441 * Get the options for the column family handle
3443 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
3444 * instance, or null for the default column family.
3446 * @return the options parsed from the options string return by RocksDB
3448 * @throws RocksDBException if an error occurs while getting the options string, or parsing the
3449 * resulting options string into options
3451 public MutableColumnFamilyOptions
.MutableColumnFamilyOptionsBuilder
getOptions(
3452 /* @Nullable */ final ColumnFamilyHandle columnFamilyHandle
) throws RocksDBException
{
3453 String optionsString
= getOptions(
3454 nativeHandle_
, columnFamilyHandle
== null ?
0 : columnFamilyHandle
.nativeHandle_
);
3455 return MutableColumnFamilyOptions
.parse(optionsString
, true);
3459 * Default column family options
3461 * @return the options parsed from the options string return by RocksDB
3463 * @throws RocksDBException if an error occurs while getting the options string, or parsing the
3464 * resulting options string into options
3466 public MutableColumnFamilyOptions
.MutableColumnFamilyOptionsBuilder
getOptions()
3467 throws RocksDBException
{
3468 return getOptions(null);
3472 * Get the database options
3474 * @return the DB options parsed from the options string return by RocksDB
3476 * @throws RocksDBException if an error occurs while getting the options string, or parsing the
3477 * resulting options string into options
3479 public MutableDBOptions
.MutableDBOptionsBuilder
getDBOptions() throws RocksDBException
{
3480 String optionsString
= getDBOptions(nativeHandle_
);
3481 return MutableDBOptions
.parse(optionsString
, true);
3485 * Change the options for the default column family handle.
3487 * @param mutableColumnFamilyOptions the options.
3489 * @throws RocksDBException if an error occurs whilst setting the options
3491 public void setOptions(
3492 final MutableColumnFamilyOptions mutableColumnFamilyOptions
)
3493 throws RocksDBException
{
3494 setOptions(null, mutableColumnFamilyOptions
);
3498 * Set the options for the column family handle.
3500 * @param mutableDBoptions the options.
3502 * @throws RocksDBException if an error occurs whilst setting the options
3504 public void setDBOptions(final MutableDBOptions mutableDBoptions
)
3505 throws RocksDBException
{
3506 setDBOptions(nativeHandle_
,
3507 mutableDBoptions
.getKeys(),
3508 mutableDBoptions
.getValues());
3512 * Takes a list of files specified by file names and
3513 * compacts them to the specified level.
3515 * Note that the behavior is different from
3516 * {@link #compactRange(ColumnFamilyHandle, byte[], byte[])}
3517 * in that CompactFiles() performs the compaction job using the CURRENT
3520 * @param compactionOptions compaction options
3521 * @param inputFileNames the name of the files to compact
3522 * @param outputLevel the level to which they should be compacted
3523 * @param outputPathId the id of the output path, or -1
3524 * @param compactionJobInfo the compaction job info, this parameter
3525 * will be updated with the info from compacting the files,
3526 * can just be null if you don't need it.
3528 * @return the list of compacted files
3530 * @throws RocksDBException if an error occurs during compaction
3532 public List
<String
> compactFiles(
3533 final CompactionOptions compactionOptions
,
3534 final List
<String
> inputFileNames
,
3535 final int outputLevel
,
3536 final int outputPathId
,
3537 /* @Nullable */ final CompactionJobInfo compactionJobInfo
)
3538 throws RocksDBException
{
3539 return compactFiles(compactionOptions
, null, inputFileNames
, outputLevel
,
3540 outputPathId
, compactionJobInfo
);
3544 * Takes a list of files specified by file names and
3545 * compacts them to the specified level.
3547 * Note that the behavior is different from
3548 * {@link #compactRange(ColumnFamilyHandle, byte[], byte[])}
3549 * in that CompactFiles() performs the compaction job using the CURRENT
3552 * @param compactionOptions compaction options
3553 * @param columnFamilyHandle columnFamilyHandle, or null for the
3554 * default column family
3555 * @param inputFileNames the name of the files to compact
3556 * @param outputLevel the level to which they should be compacted
3557 * @param outputPathId the id of the output path, or -1
3558 * @param compactionJobInfo the compaction job info, this parameter
3559 * will be updated with the info from compacting the files,
3560 * can just be null if you don't need it.
3562 * @return the list of compacted files
3564 * @throws RocksDBException if an error occurs during compaction
3566 public List
<String
> compactFiles(
3567 final CompactionOptions compactionOptions
,
3568 /* @Nullable */ final ColumnFamilyHandle columnFamilyHandle
,
3569 final List
<String
> inputFileNames
,
3570 final int outputLevel
,
3571 final int outputPathId
,
3572 /* @Nullable */ final CompactionJobInfo compactionJobInfo
)
3573 throws RocksDBException
{
3574 return Arrays
.asList(compactFiles(nativeHandle_
, compactionOptions
.nativeHandle_
,
3575 columnFamilyHandle
== null ?
0 : columnFamilyHandle
.nativeHandle_
,
3576 inputFileNames
.toArray(new String
[0]),
3579 compactionJobInfo
== null ?
0 : compactionJobInfo
.nativeHandle_
));
3583 * This function will cancel all currently running background processes.
3585 * @param wait if true, wait for all background work to be cancelled before
3589 public void cancelAllBackgroundWork(boolean wait
) {
3590 cancelAllBackgroundWork(nativeHandle_
, wait
);
3594 * This function will wait until all currently running background processes
3595 * finish. After it returns, no background process will be run until
3596 * {@link #continueBackgroundWork()} is called
3598 * @throws RocksDBException if an error occurs when pausing background work
3600 public void pauseBackgroundWork() throws RocksDBException
{
3601 pauseBackgroundWork(nativeHandle_
);
3605 * Resumes background work which was suspended by
3606 * previously calling {@link #pauseBackgroundWork()}
3608 * @throws RocksDBException if an error occurs when resuming background work
3610 public void continueBackgroundWork() throws RocksDBException
{
3611 continueBackgroundWork(nativeHandle_
);
3615 * Enable automatic compactions for the given column
3616 * families if they were previously disabled.
3618 * The function will first set the
3619 * {@link ColumnFamilyOptions#disableAutoCompactions()} option for each
3620 * column family to false, after which it will schedule a flush/compaction.
3622 * NOTE: Setting disableAutoCompactions to 'false' through
3623 * {@link #setOptions(ColumnFamilyHandle, MutableColumnFamilyOptions)}
3624 * does NOT schedule a flush/compaction afterwards, and only changes the
3625 * parameter itself within the column family option.
3627 * @param columnFamilyHandles the column family handles
3629 * @throws RocksDBException if an error occurs whilst enabling auto-compaction
3631 public void enableAutoCompaction(
3632 final List
<ColumnFamilyHandle
> columnFamilyHandles
)
3633 throws RocksDBException
{
3634 enableAutoCompaction(nativeHandle_
,
3635 toNativeHandleList(columnFamilyHandles
));
3639 * Number of levels used for this DB.
3641 * @return the number of levels
3643 public int numberLevels() {
3644 return numberLevels(null);
3648 * Number of levels used for a column family in this DB.
3650 * @param columnFamilyHandle the column family handle, or null
3651 * for the default column family
3653 * @return the number of levels
3655 public int numberLevels(/* @Nullable */final ColumnFamilyHandle columnFamilyHandle
) {
3656 return numberLevels(nativeHandle_
,
3657 columnFamilyHandle
== null ?
0 : columnFamilyHandle
.nativeHandle_
);
3661 * Maximum level to which a new compacted memtable is pushed if it
3662 * does not create overlap.
3664 * @return the maximum level
3666 public int maxMemCompactionLevel() {
3667 return maxMemCompactionLevel(null);
3671 * Maximum level to which a new compacted memtable is pushed if it
3672 * does not create overlap.
3674 * @param columnFamilyHandle the column family handle
3676 * @return the maximum level
3678 public int maxMemCompactionLevel(
3679 /* @Nullable */ final ColumnFamilyHandle columnFamilyHandle
) {
3680 return maxMemCompactionLevel(nativeHandle_
,
3681 columnFamilyHandle
== null ?
0 : columnFamilyHandle
.nativeHandle_
);
3685 * Number of files in level-0 that would stop writes.
3687 * @return the number of files
3689 public int level0StopWriteTrigger() {
3690 return level0StopWriteTrigger(null);
3694 * Number of files in level-0 that would stop writes.
3696 * @param columnFamilyHandle the column family handle
3698 * @return the number of files
3700 public int level0StopWriteTrigger(
3701 /* @Nullable */final ColumnFamilyHandle columnFamilyHandle
) {
3702 return level0StopWriteTrigger(nativeHandle_
,
3703 columnFamilyHandle
== null ?
0 : columnFamilyHandle
.nativeHandle_
);
3707 * Get DB name -- the exact same name that was provided as an argument to
3708 * as path to {@link #open(Options, String)}.
3710 * @return the DB name
3712 public String
getName() {
3713 return getName(nativeHandle_
);
3717 * Get the Env object from the DB
3721 public Env
getEnv() {
3722 final long envHandle
= getEnv(nativeHandle_
);
3723 if (envHandle
== Env
.getDefault().nativeHandle_
) {
3724 return Env
.getDefault();
3726 final Env env
= new RocksEnv(envHandle
);
3727 env
.disOwnNativeHandle(); // we do not own the Env!
3733 * <p>Flush all memory table data.</p>
3735 * <p>Note: it must be ensured that the FlushOptions instance
3736 * is not GC'ed before this method finishes. If the wait parameter is
3737 * set to false, flush processing is asynchronous.</p>
3739 * @param flushOptions {@link org.rocksdb.FlushOptions} instance.
3740 * @throws RocksDBException thrown if an error occurs within the native
3741 * part of the library.
3743 public void flush(final FlushOptions flushOptions
)
3744 throws RocksDBException
{
3745 flush(flushOptions
, (List
<ColumnFamilyHandle
>) null);
3749 * <p>Flush all memory table data.</p>
3751 * <p>Note: it must be ensured that the FlushOptions instance
3752 * is not GC'ed before this method finishes. If the wait parameter is
3753 * set to false, flush processing is asynchronous.</p>
3755 * @param flushOptions {@link org.rocksdb.FlushOptions} instance.
3756 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} instance.
3757 * @throws RocksDBException thrown if an error occurs within the native
3758 * part of the library.
3760 public void flush(final FlushOptions flushOptions
,
3761 /* @Nullable */ final ColumnFamilyHandle columnFamilyHandle
)
3762 throws RocksDBException
{
3764 columnFamilyHandle
== null ?
null : Arrays
.asList(columnFamilyHandle
));
3768 * Flushes multiple column families.
3770 * If atomic flush is not enabled, this is equivalent to calling
3771 * {@link #flush(FlushOptions, ColumnFamilyHandle)} multiple times.
3773 * If atomic flush is enabled, this will flush all column families
3774 * specified up to the latest sequence number at the time when flush is
3777 * @param flushOptions {@link org.rocksdb.FlushOptions} instance.
3778 * @param columnFamilyHandles column family handles.
3779 * @throws RocksDBException thrown if an error occurs within the native
3780 * part of the library.
3782 public void flush(final FlushOptions flushOptions
,
3783 /* @Nullable */ final List
<ColumnFamilyHandle
> columnFamilyHandles
)
3784 throws RocksDBException
{
3785 flush(nativeHandle_
, flushOptions
.nativeHandle_
,
3786 toNativeHandleList(columnFamilyHandles
));
3790 * Flush the WAL memory buffer to the file. If {@code sync} is true,
3791 * it calls {@link #syncWal()} afterwards.
3793 * @param sync true to also fsync to disk.
3795 * @throws RocksDBException if an error occurs whilst flushing
3797 public void flushWal(final boolean sync
) throws RocksDBException
{
3798 flushWal(nativeHandle_
, sync
);
3804 * Note that {@link #write(WriteOptions, WriteBatch)} followed by
3805 * {@link #syncWal()} is not exactly the same as
3806 * {@link #write(WriteOptions, WriteBatch)} with
3807 * {@link WriteOptions#sync()} set to true; In the latter case the changes
3808 * won't be visible until the sync is done.
3810 * Currently only works if {@link Options#allowMmapWrites()} is set to false.
3812 * @throws RocksDBException if an error occurs whilst syncing
3814 public void syncWal() throws RocksDBException
{
3815 syncWal(nativeHandle_
);
3819 * <p>The sequence number of the most recent transaction.</p>
3821 * @return sequence number of the most
3822 * recent transaction.
3824 public long getLatestSequenceNumber() {
3825 return getLatestSequenceNumber(nativeHandle_
);
3829 * <p>Prevent file deletions. Compactions will continue to occur,
3830 * but no obsolete files will be deleted. Calling this multiple
3831 * times have the same effect as calling it once.</p>
3833 * @throws RocksDBException thrown if operation was not performed
3836 public void disableFileDeletions() throws RocksDBException
{
3837 disableFileDeletions(nativeHandle_
);
3841 * <p>Allow compactions to delete obsolete files.
3842 * If force == true, the call to EnableFileDeletions()
3843 * will guarantee that file deletions are enabled after
3844 * the call, even if DisableFileDeletions() was called
3845 * multiple times before.</p>
3847 * <p>If force == false, EnableFileDeletions will only
3848 * enable file deletion after it's been called at least
3849 * as many times as DisableFileDeletions(), enabling
3850 * the two methods to be called by two threads
3851 * concurrently without synchronization
3852 * -- i.e., file deletions will be enabled only after both
3853 * threads call EnableFileDeletions()</p>
3855 * @param force boolean value described above.
3857 * @throws RocksDBException thrown if operation was not performed
3860 public void enableFileDeletions(final boolean force
)
3861 throws RocksDBException
{
3862 enableFileDeletions(nativeHandle_
, force
);
3865 public static class LiveFiles
{
3867 * The valid size of the manifest file. The manifest file is an ever growing
3868 * file, but only the portion specified here is valid for this snapshot.
3870 public final long manifestFileSize
;
3873 * The files are relative to the {@link #getName()} and are not
3874 * absolute paths. Despite being relative paths, the file names begin
3877 public final List
<String
> files
;
3879 LiveFiles(final long manifestFileSize
, final List
<String
> files
) {
3880 this.manifestFileSize
= manifestFileSize
;
3886 * Retrieve the list of all files in the database after flushing the memtable.
3888 * See {@link #getLiveFiles(boolean)}.
3890 * @return the live files
3892 * @throws RocksDBException if an error occurs whilst retrieving the list
3895 public LiveFiles
getLiveFiles() throws RocksDBException
{
3896 return getLiveFiles(true);
3900 * Retrieve the list of all files in the database.
3902 * In case you have multiple column families, even if {@code flushMemtable}
3903 * is true, you still need to call {@link #getSortedWalFiles()}
3904 * after {@link #getLiveFiles(boolean)} to compensate for new data that
3905 * arrived to already-flushed column families while other column families
3908 * NOTE: Calling {@link #getLiveFiles(boolean)} followed by
3909 * {@link #getSortedWalFiles()} can generate a lossless backup.
3911 * @param flushMemtable set to true to flush before recoding the live
3912 * files. Setting to false is useful when we don't want to wait for flush
3913 * which may have to wait for compaction to complete taking an
3914 * indeterminate time.
3916 * @return the live files
3918 * @throws RocksDBException if an error occurs whilst retrieving the list
3921 public LiveFiles
getLiveFiles(final boolean flushMemtable
)
3922 throws RocksDBException
{
3923 final String
[] result
= getLiveFiles(nativeHandle_
, flushMemtable
);
3924 if (result
== null) {
3927 final String
[] files
= Arrays
.copyOf(result
, result
.length
- 1);
3928 final long manifestFileSize
= Long
.parseLong(result
[result
.length
- 1]);
3930 return new LiveFiles(manifestFileSize
, Arrays
.asList(files
));
3934 * Retrieve the sorted list of all wal files with earliest file first.
3936 * @return the log files
3938 * @throws RocksDBException if an error occurs whilst retrieving the list
3939 * of sorted WAL files
3941 public List
<LogFile
> getSortedWalFiles() throws RocksDBException
{
3942 final LogFile
[] logFiles
= getSortedWalFiles(nativeHandle_
);
3943 return Arrays
.asList(logFiles
);
3947 * <p>Returns an iterator that is positioned at a write-batch containing
3948 * seq_number. If the sequence number is non existent, it returns an iterator
3949 * at the first available seq_no after the requested seq_no.</p>
3951 * <p>Must set WAL_ttl_seconds or WAL_size_limit_MB to large values to
3952 * use this api, else the WAL files will get
3953 * cleared aggressively and the iterator might keep getting invalid before
3954 * an update is read.</p>
3956 * @param sequenceNumber sequence number offset
3958 * @return {@link org.rocksdb.TransactionLogIterator} instance.
3960 * @throws org.rocksdb.RocksDBException if iterator cannot be retrieved
3963 public TransactionLogIterator
getUpdatesSince(final long sequenceNumber
)
3964 throws RocksDBException
{
3965 return new TransactionLogIterator(
3966 getUpdatesSince(nativeHandle_
, sequenceNumber
));
3970 * Delete the file name from the db directory and update the internal state to
3971 * reflect that. Supports deletion of sst and log files only. 'name' must be
3972 * path relative to the db directory. eg. 000001.sst, /archive/000003.log
3974 * @param name the file name
3976 * @throws RocksDBException if an error occurs whilst deleting the file
3978 public void deleteFile(final String name
) throws RocksDBException
{
3979 deleteFile(nativeHandle_
, name
);
3983 * Gets a list of all table files metadata.
3985 * @return table files metadata.
3987 public List
<LiveFileMetaData
> getLiveFilesMetaData() {
3988 return Arrays
.asList(getLiveFilesMetaData(nativeHandle_
));
3992 * Obtains the meta data of the specified column family of the DB.
3994 * @param columnFamilyHandle the column family
3996 * @return the column family metadata
3998 public ColumnFamilyMetaData
getColumnFamilyMetaData(
3999 /* @Nullable */ final ColumnFamilyHandle columnFamilyHandle
) {
4000 return getColumnFamilyMetaData(nativeHandle_
,
4001 columnFamilyHandle
== null ?
0 : columnFamilyHandle
.nativeHandle_
);
4005 * Obtains the meta data of the default column family of the DB.
4007 * @return the column family metadata
4009 public ColumnFamilyMetaData
getColumnFamilyMetaData() {
4010 return getColumnFamilyMetaData(null);
4014 * ingestExternalFile will load a list of external SST files (1) into the DB
4015 * We will try to find the lowest possible level that the file can fit in, and
4016 * ingest the file into this level (2). A file that have a key range that
4017 * overlap with the memtable key range will require us to Flush the memtable
4018 * first before ingesting the file.
4020 * (1) External SST files can be created using {@link SstFileWriter}
4021 * (2) We will try to ingest the files to the lowest possible level
4022 * even if the file compression doesn't match the level compression
4024 * @param filePathList The list of files to ingest
4025 * @param ingestExternalFileOptions the options for the ingestion
4027 * @throws RocksDBException thrown if error happens in underlying
4030 public void ingestExternalFile(final List
<String
> filePathList
,
4031 final IngestExternalFileOptions ingestExternalFileOptions
)
4032 throws RocksDBException
{
4033 ingestExternalFile(nativeHandle_
, getDefaultColumnFamily().nativeHandle_
,
4034 filePathList
.toArray(new String
[0]),
4035 filePathList
.size(), ingestExternalFileOptions
.nativeHandle_
);
4039 * ingestExternalFile will load a list of external SST files (1) into the DB
4040 * We will try to find the lowest possible level that the file can fit in, and
4041 * ingest the file into this level (2). A file that have a key range that
4042 * overlap with the memtable key range will require us to Flush the memtable
4043 * first before ingesting the file.
4045 * (1) External SST files can be created using {@link SstFileWriter}
4046 * (2) We will try to ingest the files to the lowest possible level
4047 * even if the file compression doesn't match the level compression
4049 * @param columnFamilyHandle The column family for the ingested files
4050 * @param filePathList The list of files to ingest
4051 * @param ingestExternalFileOptions the options for the ingestion
4053 * @throws RocksDBException thrown if error happens in underlying
4056 public void ingestExternalFile(final ColumnFamilyHandle columnFamilyHandle
,
4057 final List
<String
> filePathList
,
4058 final IngestExternalFileOptions ingestExternalFileOptions
)
4059 throws RocksDBException
{
4060 ingestExternalFile(nativeHandle_
, columnFamilyHandle
.nativeHandle_
,
4061 filePathList
.toArray(new String
[0]),
4062 filePathList
.size(), ingestExternalFileOptions
.nativeHandle_
);
4068 * @throws RocksDBException if the checksum is not valid
4070 public void verifyChecksum() throws RocksDBException
{
4071 verifyChecksum(nativeHandle_
);
4075 * Gets the handle for the default column family
4077 * @return The handle of the default column family
4079 public ColumnFamilyHandle
getDefaultColumnFamily() {
4080 final ColumnFamilyHandle cfHandle
= new ColumnFamilyHandle(this,
4081 getDefaultColumnFamily(nativeHandle_
));
4082 cfHandle
.disOwnNativeHandle();
4087 * Get the properties of all tables.
4089 * @param columnFamilyHandle the column family handle, or null for the default
4092 * @return the properties
4094 * @throws RocksDBException if an error occurs whilst getting the properties
4096 public Map
<String
, TableProperties
> getPropertiesOfAllTables(
4097 /* @Nullable */final ColumnFamilyHandle columnFamilyHandle
)
4098 throws RocksDBException
{
4099 return getPropertiesOfAllTables(nativeHandle_
,
4100 columnFamilyHandle
== null ?
0 : columnFamilyHandle
.nativeHandle_
);
4104 * Get the properties of all tables in the default column family.
4106 * @return the properties
4108 * @throws RocksDBException if an error occurs whilst getting the properties
4110 public Map
<String
, TableProperties
> getPropertiesOfAllTables()
4111 throws RocksDBException
{
4112 return getPropertiesOfAllTables(null);
4116 * Get the properties of tables in range.
4118 * @param columnFamilyHandle the column family handle, or null for the default
4120 * @param ranges the ranges over which to get the table properties
4122 * @return the properties
4124 * @throws RocksDBException if an error occurs whilst getting the properties
4126 public Map
<String
, TableProperties
> getPropertiesOfTablesInRange(
4127 /* @Nullable */final ColumnFamilyHandle columnFamilyHandle
,
4128 final List
<Range
> ranges
) throws RocksDBException
{
4129 return getPropertiesOfTablesInRange(nativeHandle_
,
4130 columnFamilyHandle
== null ?
0 : columnFamilyHandle
.nativeHandle_
,
4131 toRangeSliceHandles(ranges
));
4135 * Get the properties of tables in range for the default column family.
4137 * @param ranges the ranges over which to get the table properties
4139 * @return the properties
4141 * @throws RocksDBException if an error occurs whilst getting the properties
4143 public Map
<String
, TableProperties
> getPropertiesOfTablesInRange(
4144 final List
<Range
> ranges
) throws RocksDBException
{
4145 return getPropertiesOfTablesInRange(null, ranges
);
4149 * Suggest the range to compact.
4151 * @param columnFamilyHandle the column family handle, or null for the default
4154 * @return the suggested range.
4156 * @throws RocksDBException if an error occurs whilst suggesting the range
4158 public Range
suggestCompactRange(
4159 /* @Nullable */final ColumnFamilyHandle columnFamilyHandle
)
4160 throws RocksDBException
{
4161 final long[] rangeSliceHandles
= suggestCompactRange(nativeHandle_
,
4162 columnFamilyHandle
== null ?
0 : columnFamilyHandle
.nativeHandle_
);
4163 return new Range(new Slice(rangeSliceHandles
[0]),
4164 new Slice(rangeSliceHandles
[1]));
4168 * Suggest the range to compact for the default column family.
4170 * @return the suggested range.
4172 * @throws RocksDBException if an error occurs whilst suggesting the range
4174 public Range
suggestCompactRange()
4175 throws RocksDBException
{
4176 return suggestCompactRange(null);
4182 * @param columnFamilyHandle the column family handle,
4183 * or null for the default column family.
4184 * @param targetLevel the target level for L0
4186 * @throws RocksDBException if an error occurs whilst promoting L0
4188 public void promoteL0(
4189 /* @Nullable */final ColumnFamilyHandle columnFamilyHandle
,
4190 final int targetLevel
) throws RocksDBException
{
4191 promoteL0(nativeHandle_
,
4192 columnFamilyHandle
== null ?
0 : columnFamilyHandle
.nativeHandle_
,
4197 * Promote L0 for the default column family.
4199 * @param targetLevel the target level for L0
4201 * @throws RocksDBException if an error occurs whilst promoting L0
4203 public void promoteL0(final int targetLevel
)
4204 throws RocksDBException
{
4205 promoteL0(null, targetLevel
);
4209 * Trace DB operations.
4211 * Use {@link #endTrace()} to stop tracing.
4213 * @param traceOptions the options
4214 * @param traceWriter the trace writer
4216 * @throws RocksDBException if an error occurs whilst starting the trace
4218 public void startTrace(final TraceOptions traceOptions
,
4219 final AbstractTraceWriter traceWriter
) throws RocksDBException
{
4220 startTrace(nativeHandle_
, traceOptions
.getMaxTraceFileSize(),
4221 traceWriter
.nativeHandle_
);
4223 * NOTE: {@link #startTrace(long, long, long) transfers the ownership
4224 * from Java to C++, so we must disown the native handle here.
4226 traceWriter
.disOwnNativeHandle();
4230 * Stop tracing DB operations.
4232 * See {@link #startTrace(TraceOptions, AbstractTraceWriter)}
4234 * @throws RocksDBException if an error occurs whilst ending the trace
4236 public void endTrace() throws RocksDBException
{
4237 endTrace(nativeHandle_
);
4241 * Make the secondary instance catch up with the primary by tailing and
4242 * replaying the MANIFEST and WAL of the primary.
4243 * Column families created by the primary after the secondary instance starts
4244 * will be ignored unless the secondary instance closes and restarts with the
4245 * newly created column families.
4246 * Column families that exist before secondary instance starts and dropped by
4247 * the primary afterwards will be marked as dropped. However, as long as the
4248 * secondary instance does not delete the corresponding column family
4249 * handles, the data of the column family is still accessible to the
4252 * @throws RocksDBException thrown if error happens in underlying
4255 public void tryCatchUpWithPrimary() throws RocksDBException
{
4256 tryCatchUpWithPrimary(nativeHandle_
);
4260 * Delete files in multiple ranges at once.
4261 * Delete files in a lot of ranges one at a time can be slow, use this API for
4262 * better performance in that case.
4264 * @param columnFamily - The column family for operation (null for default)
4265 * @param includeEnd - Whether ranges should include end
4266 * @param ranges - pairs of ranges (from1, to1, from2, to2, ...)
4268 * @throws RocksDBException thrown if error happens in underlying
4271 public void deleteFilesInRanges(final ColumnFamilyHandle columnFamily
,
4272 final List
<byte[]> ranges
, final boolean includeEnd
)
4273 throws RocksDBException
{
4274 if (ranges
.size() == 0) {
4277 if ((ranges
.size() % 2) != 0) {
4278 throw new IllegalArgumentException("Ranges size needs to be multiple of 2 "
4279 + "(from1, to1, from2, to2, ...), but is " + ranges
.size());
4282 final byte[][] rangesArray
= ranges
.toArray(new byte[ranges
.size()][]);
4284 deleteFilesInRanges(nativeHandle_
, columnFamily
== null ?
0 : columnFamily
.nativeHandle_
,
4285 rangesArray
, includeEnd
);
4289 * Static method to destroy the contents of the specified database.
4290 * Be very careful using this method.
4292 * @param path the path to the Rocksdb database.
4293 * @param options {@link org.rocksdb.Options} instance.
4295 * @throws RocksDBException thrown if error happens in underlying
4298 public static void destroyDB(final String path
, final Options options
)
4299 throws RocksDBException
{
4300 destroyDB(path
, options
.nativeHandle_
);
4303 private /* @Nullable */ long[] toNativeHandleList(
4304 /* @Nullable */ final List
<?
extends RocksObject
> objectList
) {
4305 if (objectList
== null) {
4308 final int len
= objectList
.size();
4309 final long[] handleList
= new long[len
];
4310 for (int i
= 0; i
< len
; i
++) {
4311 handleList
[i
] = objectList
.get(i
).nativeHandle_
;
4316 private static long[] toRangeSliceHandles(final List
<Range
> ranges
) {
4317 final long rangeSliceHandles
[] = new long [ranges
.size() * 2];
4318 for (int i
= 0, j
= 0; i
< ranges
.size(); i
++) {
4319 final Range range
= ranges
.get(i
);
4320 rangeSliceHandles
[j
++] = range
.start
.getNativeHandle();
4321 rangeSliceHandles
[j
++] = range
.limit
.getNativeHandle();
4323 return rangeSliceHandles
;
4326 protected void storeOptionsInstance(DBOptionsInterface
<?
> options
) {
4330 private static void checkBounds(int offset
, int len
, int size
) {
4331 if ((offset
| len
| (offset
+ len
) | (size
- (offset
+ len
))) < 0) {
4332 throw new IndexOutOfBoundsException(String
.format("offset(%d), len(%d), size(%d)", offset
, len
, size
));
4336 private static int computeCapacityHint(final int estimatedNumberOfItems
) {
4337 // Default load factor for HashMap is 0.75, so N * 1.5 will be at the load
4338 // limit. We add +1 for a buffer.
4339 return (int)Math
.ceil(estimatedNumberOfItems
* 1.5 + 1.0);
4343 private native static long open(final long optionsHandle
,
4344 final String path
) throws RocksDBException
;
4347 * @param optionsHandle Native handle pointing to an Options object
4348 * @param path The directory path for the database files
4349 * @param columnFamilyNames An array of column family names
4350 * @param columnFamilyOptions An array of native handles pointing to
4351 * ColumnFamilyOptions objects
4353 * @return An array of native handles, [0] is the handle of the RocksDB object
4354 * [1..1+n] are handles of the ColumnFamilyReferences
4356 * @throws RocksDBException thrown if the database could not be opened
4358 private native static long[] open(final long optionsHandle
,
4359 final String path
, final byte[][] columnFamilyNames
,
4360 final long[] columnFamilyOptions
) throws RocksDBException
;
4362 private native static long openROnly(final long optionsHandle
, final String path
,
4363 final boolean errorIfWalFileExists
) throws RocksDBException
;
4366 * @param optionsHandle Native handle pointing to an Options object
4367 * @param path The directory path for the database files
4368 * @param columnFamilyNames An array of column family names
4369 * @param columnFamilyOptions An array of native handles pointing to
4370 * ColumnFamilyOptions objects
4372 * @return An array of native handles, [0] is the handle of the RocksDB object
4373 * [1..1+n] are handles of the ColumnFamilyReferences
4375 * @throws RocksDBException thrown if the database could not be opened
4377 private native static long[] openROnly(final long optionsHandle
, final String path
,
4378 final byte[][] columnFamilyNames
, final long[] columnFamilyOptions
,
4379 final boolean errorIfWalFileExists
) throws RocksDBException
;
4381 private native static long openAsSecondary(final long optionsHandle
, final String path
,
4382 final String secondaryPath
) throws RocksDBException
;
4384 private native static long[] openAsSecondary(final long optionsHandle
, final String path
,
4385 final String secondaryPath
, final byte[][] columnFamilyNames
,
4386 final long[] columnFamilyOptions
) throws RocksDBException
;
4388 @Override protected native void disposeInternal(final long handle
);
4390 private native static void closeDatabase(final long handle
)
4391 throws RocksDBException
;
4392 private native static byte[][] listColumnFamilies(final long optionsHandle
,
4393 final String path
) throws RocksDBException
;
4394 private native long createColumnFamily(final long handle
,
4395 final byte[] columnFamilyName
, final int columnFamilyNamelen
,
4396 final long columnFamilyOptions
) throws RocksDBException
;
4397 private native long[] createColumnFamilies(final long handle
,
4398 final long columnFamilyOptionsHandle
, final byte[][] columnFamilyNames
)
4399 throws RocksDBException
;
4400 private native long[] createColumnFamilies(final long handle
,
4401 final long columnFamilyOptionsHandles
[], final byte[][] columnFamilyNames
)
4402 throws RocksDBException
;
4403 private native void dropColumnFamily(
4404 final long handle
, final long cfHandle
) throws RocksDBException
;
4405 private native void dropColumnFamilies(final long handle
,
4406 final long[] cfHandles
) throws RocksDBException
;
4407 private native void put(final long handle
, final byte[] key
,
4408 final int keyOffset
, final int keyLength
, final byte[] value
,
4409 final int valueOffset
, int valueLength
) throws RocksDBException
;
4410 private native void put(final long handle
, final byte[] key
, final int keyOffset
,
4411 final int keyLength
, final byte[] value
, final int valueOffset
,
4412 final int valueLength
, final long cfHandle
) throws RocksDBException
;
4413 private native void put(final long handle
, final long writeOptHandle
,
4414 final byte[] key
, final int keyOffset
, final int keyLength
,
4415 final byte[] value
, final int valueOffset
, final int valueLength
)
4416 throws RocksDBException
;
4417 private native void put(final long handle
, final long writeOptHandle
,
4418 final byte[] key
, final int keyOffset
, final int keyLength
,
4419 final byte[] value
, final int valueOffset
, final int valueLength
,
4420 final long cfHandle
) throws RocksDBException
;
4421 private native void delete(final long handle
, final byte[] key
,
4422 final int keyOffset
, final int keyLength
) throws RocksDBException
;
4423 private native void delete(final long handle
, final byte[] key
,
4424 final int keyOffset
, final int keyLength
, final long cfHandle
)
4425 throws RocksDBException
;
4426 private native void delete(final long handle
, final long writeOptHandle
,
4427 final byte[] key
, final int keyOffset
, final int keyLength
)
4428 throws RocksDBException
;
4429 private native void delete(final long handle
, final long writeOptHandle
,
4430 final byte[] key
, final int keyOffset
, final int keyLength
,
4431 final long cfHandle
) throws RocksDBException
;
4432 private native void singleDelete(
4433 final long handle
, final byte[] key
, final int keyLen
)
4434 throws RocksDBException
;
4435 private native void singleDelete(
4436 final long handle
, final byte[] key
, final int keyLen
,
4437 final long cfHandle
) throws RocksDBException
;
4438 private native void singleDelete(
4439 final long handle
, final long writeOptHandle
, final byte[] key
,
4440 final int keyLen
) throws RocksDBException
;
4441 private native void singleDelete(
4442 final long handle
, final long writeOptHandle
,
4443 final byte[] key
, final int keyLen
, final long cfHandle
)
4444 throws RocksDBException
;
4445 private native void deleteRange(final long handle
, final byte[] beginKey
,
4446 final int beginKeyOffset
, final int beginKeyLength
, final byte[] endKey
,
4447 final int endKeyOffset
, final int endKeyLength
) throws RocksDBException
;
4448 private native void deleteRange(final long handle
, final byte[] beginKey
,
4449 final int beginKeyOffset
, final int beginKeyLength
, final byte[] endKey
,
4450 final int endKeyOffset
, final int endKeyLength
, final long cfHandle
)
4451 throws RocksDBException
;
4452 private native void deleteRange(final long handle
, final long writeOptHandle
,
4453 final byte[] beginKey
, final int beginKeyOffset
, final int beginKeyLength
,
4454 final byte[] endKey
, final int endKeyOffset
, final int endKeyLength
)
4455 throws RocksDBException
;
4456 private native void deleteRange(
4457 final long handle
, final long writeOptHandle
, final byte[] beginKey
,
4458 final int beginKeyOffset
, final int beginKeyLength
, final byte[] endKey
,
4459 final int endKeyOffset
, final int endKeyLength
, final long cfHandle
)
4460 throws RocksDBException
;
4461 private native void merge(final long handle
, final byte[] key
,
4462 final int keyOffset
, final int keyLength
, final byte[] value
,
4463 final int valueOffset
, final int valueLength
) throws RocksDBException
;
4464 private native void merge(final long handle
, final byte[] key
,
4465 final int keyOffset
, final int keyLength
, final byte[] value
,
4466 final int valueOffset
, final int valueLength
, final long cfHandle
)
4467 throws RocksDBException
;
4468 private native void merge(final long handle
, final long writeOptHandle
,
4469 final byte[] key
, final int keyOffset
, final int keyLength
,
4470 final byte[] value
, final int valueOffset
, final int valueLength
)
4471 throws RocksDBException
;
4472 private native void merge(final long handle
, final long writeOptHandle
,
4473 final byte[] key
, final int keyOffset
, final int keyLength
,
4474 final byte[] value
, final int valueOffset
, final int valueLength
,
4475 final long cfHandle
) throws RocksDBException
;
4476 private native void write0(final long handle
, final long writeOptHandle
,
4477 final long wbHandle
) throws RocksDBException
;
4478 private native void write1(final long handle
, final long writeOptHandle
,
4479 final long wbwiHandle
) throws RocksDBException
;
4480 private native int get(final long handle
, final byte[] key
,
4481 final int keyOffset
, final int keyLength
, final byte[] value
,
4482 final int valueOffset
, final int valueLength
) throws RocksDBException
;
4483 private native int get(final long handle
, final byte[] key
,
4484 final int keyOffset
, final int keyLength
, byte[] value
,
4485 final int valueOffset
, final int valueLength
, final long cfHandle
)
4486 throws RocksDBException
;
4487 private native int get(final long handle
, final long readOptHandle
,
4488 final byte[] key
, final int keyOffset
, final int keyLength
,
4489 final byte[] value
, final int valueOffset
, final int valueLength
)
4490 throws RocksDBException
;
4491 private native int get(final long handle
, final long readOptHandle
,
4492 final byte[] key
, final int keyOffset
, final int keyLength
,
4493 final byte[] value
, final int valueOffset
, final int valueLength
,
4494 final long cfHandle
) throws RocksDBException
;
4495 private native byte[] get(final long handle
, byte[] key
, final int keyOffset
,
4496 final int keyLength
) throws RocksDBException
;
4497 private native byte[] get(final long handle
, final byte[] key
,
4498 final int keyOffset
, final int keyLength
, final long cfHandle
)
4499 throws RocksDBException
;
4500 private native byte[] get(final long handle
, final long readOptHandle
,
4501 final byte[] key
, final int keyOffset
, final int keyLength
)
4502 throws RocksDBException
;
4503 private native byte[] get(final long handle
,
4504 final long readOptHandle
, final byte[] key
, final int keyOffset
,
4505 final int keyLength
, final long cfHandle
) throws RocksDBException
;
4506 private native byte[][] multiGet(final long dbHandle
, final byte[][] keys
,
4507 final int[] keyOffsets
, final int[] keyLengths
);
4508 private native byte[][] multiGet(final long dbHandle
, final byte[][] keys
,
4509 final int[] keyOffsets
, final int[] keyLengths
,
4510 final long[] columnFamilyHandles
);
4511 private native byte[][] multiGet(final long dbHandle
, final long rOptHandle
,
4512 final byte[][] keys
, final int[] keyOffsets
, final int[] keyLengths
);
4513 private native byte[][] multiGet(final long dbHandle
, final long rOptHandle
,
4514 final byte[][] keys
, final int[] keyOffsets
, final int[] keyLengths
,
4515 final long[] columnFamilyHandles
);
4517 private native void multiGet(final long dbHandle
, final long rOptHandle
,
4518 final long[] columnFamilyHandles
, final ByteBuffer
[] keysArray
, final int[] keyOffsets
,
4519 final int[] keyLengths
, final ByteBuffer
[] valuesArray
, final int[] valuesSizeArray
,
4520 final Status
[] statusArray
);
4522 private native boolean keyMayExist(
4523 final long handle
, final long cfHandle
, final long readOptHandle
,
4524 final byte[] key
, final int keyOffset
, final int keyLength
);
4525 private native byte[][] keyMayExistFoundValue(
4526 final long handle
, final long cfHandle
, final long readOptHandle
,
4527 final byte[] key
, final int keyOffset
, final int keyLength
);
4528 private native void putDirect(long handle
, long writeOptHandle
, ByteBuffer key
, int keyOffset
,
4529 int keyLength
, ByteBuffer value
, int valueOffset
, int valueLength
, long cfHandle
)
4530 throws RocksDBException
;
4531 private native long iterator(final long handle
);
4532 private native long iterator(final long handle
, final long readOptHandle
);
4533 private native long iteratorCF(final long handle
, final long cfHandle
);
4534 private native long iteratorCF(final long handle
, final long cfHandle
,
4535 final long readOptHandle
);
4536 private native long[] iterators(final long handle
,
4537 final long[] columnFamilyHandles
, final long readOptHandle
)
4538 throws RocksDBException
;
4539 private native long getSnapshot(final long nativeHandle
);
4540 private native void releaseSnapshot(
4541 final long nativeHandle
, final long snapshotHandle
);
4542 private native String
getProperty(final long nativeHandle
,
4543 final long cfHandle
, final String property
, final int propertyLength
)
4544 throws RocksDBException
;
4545 private native Map
<String
, String
> getMapProperty(final long nativeHandle
,
4546 final long cfHandle
, final String property
, final int propertyLength
)
4547 throws RocksDBException
;
4548 private native int getDirect(long handle
, long readOptHandle
, ByteBuffer key
, int keyOffset
,
4549 int keyLength
, ByteBuffer value
, int valueOffset
, int valueLength
, long cfHandle
)
4550 throws RocksDBException
;
4551 private native boolean keyMayExistDirect(final long handle
, final long cfHhandle
,
4552 final long readOptHandle
, final ByteBuffer key
, final int keyOffset
, final int keyLength
);
4553 private native int[] keyMayExistDirectFoundValue(final long handle
, final long cfHhandle
,
4554 final long readOptHandle
, final ByteBuffer key
, final int keyOffset
, final int keyLength
,
4555 final ByteBuffer value
, final int valueOffset
, final int valueLength
);
4556 private native void deleteDirect(long handle
, long optHandle
, ByteBuffer key
, int keyOffset
,
4557 int keyLength
, long cfHandle
) throws RocksDBException
;
4558 private native long getLongProperty(final long nativeHandle
,
4559 final long cfHandle
, final String property
, final int propertyLength
)
4560 throws RocksDBException
;
4561 private native void resetStats(final long nativeHandle
)
4562 throws RocksDBException
;
4563 private native long getAggregatedLongProperty(final long nativeHandle
,
4564 final String property
, int propertyLength
) throws RocksDBException
;
4565 private native long[] getApproximateSizes(final long nativeHandle
,
4566 final long columnFamilyHandle
, final long[] rangeSliceHandles
,
4567 final byte includeFlags
);
4568 private native long[] getApproximateMemTableStats(final long nativeHandle
,
4569 final long columnFamilyHandle
, final long rangeStartSliceHandle
,
4570 final long rangeLimitSliceHandle
);
4571 private native void compactRange(final long handle
,
4572 /* @Nullable */ final byte[] begin
, final int beginLen
,
4573 /* @Nullable */ final byte[] end
, final int endLen
,
4574 final long compactRangeOptHandle
, final long cfHandle
)
4575 throws RocksDBException
;
4576 private native void setOptions(final long handle
, final long cfHandle
,
4577 final String
[] keys
, final String
[] values
) throws RocksDBException
;
4578 private native String
getOptions(final long handle
, final long cfHandle
);
4579 private native void setDBOptions(final long handle
,
4580 final String
[] keys
, final String
[] values
) throws RocksDBException
;
4581 private native String
getDBOptions(final long handle
);
4582 private native String
[] compactFiles(final long handle
,
4583 final long compactionOptionsHandle
,
4584 final long columnFamilyHandle
,
4585 final String
[] inputFileNames
,
4586 final int outputLevel
,
4587 final int outputPathId
,
4588 final long compactionJobInfoHandle
) throws RocksDBException
;
4589 private native void cancelAllBackgroundWork(final long handle
,
4590 final boolean wait
);
4591 private native void pauseBackgroundWork(final long handle
)
4592 throws RocksDBException
;
4593 private native void continueBackgroundWork(final long handle
)
4594 throws RocksDBException
;
4595 private native void enableAutoCompaction(final long handle
,
4596 final long[] columnFamilyHandles
) throws RocksDBException
;
4597 private native int numberLevels(final long handle
,
4598 final long columnFamilyHandle
);
4599 private native int maxMemCompactionLevel(final long handle
,
4600 final long columnFamilyHandle
);
4601 private native int level0StopWriteTrigger(final long handle
,
4602 final long columnFamilyHandle
);
4603 private native String
getName(final long handle
);
4604 private native long getEnv(final long handle
);
4605 private native void flush(final long handle
, final long flushOptHandle
,
4606 /* @Nullable */ final long[] cfHandles
) throws RocksDBException
;
4607 private native void flushWal(final long handle
, final boolean sync
)
4608 throws RocksDBException
;
4609 private native void syncWal(final long handle
) throws RocksDBException
;
4610 private native long getLatestSequenceNumber(final long handle
);
4611 private native void disableFileDeletions(long handle
) throws RocksDBException
;
4612 private native void enableFileDeletions(long handle
, boolean force
)
4613 throws RocksDBException
;
4614 private native String
[] getLiveFiles(final long handle
,
4615 final boolean flushMemtable
) throws RocksDBException
;
4616 private native LogFile
[] getSortedWalFiles(final long handle
)
4617 throws RocksDBException
;
4618 private native long getUpdatesSince(final long handle
,
4619 final long sequenceNumber
) throws RocksDBException
;
4620 private native void deleteFile(final long handle
, final String name
)
4621 throws RocksDBException
;
4622 private native LiveFileMetaData
[] getLiveFilesMetaData(final long handle
);
4623 private native ColumnFamilyMetaData
getColumnFamilyMetaData(
4624 final long handle
, final long columnFamilyHandle
);
4625 private native void ingestExternalFile(final long handle
,
4626 final long columnFamilyHandle
, final String
[] filePathList
,
4627 final int filePathListLen
, final long ingestExternalFileOptionsHandle
)
4628 throws RocksDBException
;
4629 private native void verifyChecksum(final long handle
) throws RocksDBException
;
4630 private native long getDefaultColumnFamily(final long handle
);
4631 private native Map
<String
, TableProperties
> getPropertiesOfAllTables(
4632 final long handle
, final long columnFamilyHandle
) throws RocksDBException
;
4633 private native Map
<String
, TableProperties
> getPropertiesOfTablesInRange(
4634 final long handle
, final long columnFamilyHandle
,
4635 final long[] rangeSliceHandles
);
4636 private native long[] suggestCompactRange(final long handle
,
4637 final long columnFamilyHandle
) throws RocksDBException
;
4638 private native void promoteL0(final long handle
,
4639 final long columnFamilyHandle
, final int tragetLevel
)
4640 throws RocksDBException
;
4641 private native void startTrace(final long handle
, final long maxTraceFileSize
,
4642 final long traceWriterHandle
) throws RocksDBException
;
4643 private native void endTrace(final long handle
) throws RocksDBException
;
4644 private native void tryCatchUpWithPrimary(final long handle
) throws RocksDBException
;
4645 private native void deleteFilesInRanges(long handle
, long cfHandle
, final byte[][] ranges
,
4646 boolean include_end
) throws RocksDBException
;
4648 private native static void destroyDB(final String path
,
4649 final long optionsHandle
) throws RocksDBException
;
4651 private native static int version();
4653 protected DBOptionsInterface
<?
> options_
;
4654 private static Version version
;
4656 public static class Version
{
4657 private final byte major
;
4658 private final byte minor
;
4659 private final byte patch
;
4661 public Version(final byte major
, final byte minor
, final byte patch
) {
4667 public int getMajor() {
4671 public int getMinor() {
4675 public int getPatch() {
4680 public String
toString() {
4681 return getMajor() + "." + getMinor() + "." + getPatch();
4684 private static Version
fromEncodedVersion(int encodedVersion
) {
4685 final byte patch
= (byte) (encodedVersion
& 0xff);
4686 encodedVersion
>>= 8;
4687 final byte minor
= (byte) (encodedVersion
& 0xff);
4688 encodedVersion
>>= 8;
4689 final byte major
= (byte) (encodedVersion
& 0xff);
4691 return new Version(major
, minor
, patch
);