]> git.proxmox.com Git - ceph.git/blame - ceph/src/rocksdb/java/src/main/java/org/rocksdb/RocksDB.java
import quincy beta 17.1.0
[ceph.git] / ceph / src / rocksdb / java / src / main / java / org / rocksdb / RocksDB.java
CommitLineData
7c673cae 1// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
11fdf7f2
TL
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).
7c673cae
FG
5
6package org.rocksdb;
7
7c673cae 8import java.io.IOException;
f67539c2
TL
9import java.nio.ByteBuffer;
10import java.util.ArrayList;
11import java.util.Arrays;
12import java.util.HashMap;
13import java.util.List;
14import java.util.Map;
7c673cae 15import java.util.concurrent.atomic.AtomicReference;
7c673cae
FG
16import org.rocksdb.util.Environment;
17
18/**
19 * A RocksDB is a persistent ordered map from keys to values. It is safe for
20 * concurrent access from multiple threads without any external synchronization.
21 * All methods of this class could potentially throw RocksDBException, which
22 * indicates sth wrong at the RocksDB library side and the call failed.
23 */
24public class RocksDB extends RocksObject {
25 public static final byte[] DEFAULT_COLUMN_FAMILY = "default".getBytes();
26 public static final int NOT_FOUND = -1;
27
28 private enum LibraryState {
29 NOT_LOADED,
30 LOADING,
31 LOADED
32 }
33
34 private static AtomicReference<LibraryState> libraryLoaded
35 = new AtomicReference<>(LibraryState.NOT_LOADED);
36
37 static {
38 RocksDB.loadLibrary();
39 }
40
20effc67
TL
41 private List<ColumnFamilyHandle> ownedColumnFamilyHandles = new ArrayList<>();
42
7c673cae
FG
43 /**
44 * Loads the necessary library files.
45 * Calling this method twice will have no effect.
46 * By default the method extracts the shared library for loading at
47 * java.io.tmpdir, however, you can override this temporary location by
48 * setting the environment variable ROCKSDB_SHAREDLIB_DIR.
49 */
50 public static void loadLibrary() {
51 if (libraryLoaded.get() == LibraryState.LOADED) {
52 return;
53 }
54
55 if (libraryLoaded.compareAndSet(LibraryState.NOT_LOADED,
56 LibraryState.LOADING)) {
57 final String tmpDir = System.getenv("ROCKSDB_SHAREDLIB_DIR");
58 // loading possibly necessary libraries.
59 for (final CompressionType compressionType : CompressionType.values()) {
60 try {
61 if (compressionType.getLibraryName() != null) {
62 System.loadLibrary(compressionType.getLibraryName());
63 }
20effc67 64 } catch (final UnsatisfiedLinkError e) {
7c673cae
FG
65 // since it may be optional, we ignore its loading failure here.
66 }
67 }
68 try {
69 NativeLibraryLoader.getInstance().loadLibrary(tmpDir);
20effc67 70 } catch (final IOException e) {
7c673cae 71 libraryLoaded.set(LibraryState.NOT_LOADED);
494da23a
TL
72 throw new RuntimeException("Unable to load the RocksDB shared library",
73 e);
7c673cae
FG
74 }
75
20effc67
TL
76 final int encodedVersion = version();
77 version = Version.fromEncodedVersion(encodedVersion);
78
7c673cae
FG
79 libraryLoaded.set(LibraryState.LOADED);
80 return;
81 }
82
83 while (libraryLoaded.get() == LibraryState.LOADING) {
84 try {
85 Thread.sleep(10);
86 } catch(final InterruptedException e) {
87 //ignore
88 }
89 }
90 }
91
92 /**
93 * Tries to load the necessary library files from the given list of
94 * directories.
95 *
96 * @param paths a list of strings where each describes a directory
97 * of a library.
98 */
99 public static void loadLibrary(final List<String> paths) {
100 if (libraryLoaded.get() == LibraryState.LOADED) {
101 return;
102 }
103
104 if (libraryLoaded.compareAndSet(LibraryState.NOT_LOADED,
105 LibraryState.LOADING)) {
106 for (final CompressionType compressionType : CompressionType.values()) {
107 if (compressionType.equals(CompressionType.NO_COMPRESSION)) {
108 continue;
109 }
110 for (final String path : paths) {
111 try {
112 System.load(path + "/" + Environment.getSharedLibraryFileName(
113 compressionType.getLibraryName()));
114 break;
20effc67 115 } catch (final UnsatisfiedLinkError e) {
7c673cae
FG
116 // since they are optional, we ignore loading fails.
117 }
118 }
119 }
120 boolean success = false;
121 UnsatisfiedLinkError err = null;
122 for (final String path : paths) {
123 try {
124 System.load(path + "/" +
125 Environment.getJniLibraryFileName("rocksdbjni"));
126 success = true;
127 break;
20effc67 128 } catch (final UnsatisfiedLinkError e) {
7c673cae
FG
129 err = e;
130 }
131 }
132 if (!success) {
133 libraryLoaded.set(LibraryState.NOT_LOADED);
134 throw err;
135 }
136
20effc67
TL
137 final int encodedVersion = version();
138 version = Version.fromEncodedVersion(encodedVersion);
139
7c673cae
FG
140 libraryLoaded.set(LibraryState.LOADED);
141 return;
142 }
143
144 while (libraryLoaded.get() == LibraryState.LOADING) {
145 try {
146 Thread.sleep(10);
147 } catch(final InterruptedException e) {
148 //ignore
149 }
150 }
151 }
152
20effc67
TL
153 public static Version rocksdbVersion() {
154 return version;
155 }
156
494da23a
TL
157 /**
158 * Private constructor.
159 *
160 * @param nativeHandle The native handle of the C++ RocksDB object
161 */
162 protected RocksDB(final long nativeHandle) {
163 super(nativeHandle);
164 }
165
7c673cae
FG
166 /**
167 * The factory constructor of RocksDB that opens a RocksDB instance given
168 * the path to the database using the default options w/ createIfMissing
169 * set to true.
170 *
171 * @param path the path to the rocksdb.
172 * @return a {@link RocksDB} instance on success, null if the specified
173 * {@link RocksDB} can not be opened.
174 *
175 * @throws RocksDBException thrown if error happens in underlying
176 * native library.
177 * @see Options#setCreateIfMissing(boolean)
178 */
179 public static RocksDB open(final String path) throws RocksDBException {
494da23a 180 final Options options = new Options();
7c673cae
FG
181 options.setCreateIfMissing(true);
182 return open(options, path);
183 }
184
185 /**
186 * The factory constructor of RocksDB that opens a RocksDB instance given
187 * the path to the database using the specified options and db path and a list
188 * of column family names.
189 * <p>
190 * If opened in read write mode every existing column family name must be
191 * passed within the list to this method.</p>
192 * <p>
193 * If opened in read-only mode only a subset of existing column families must
194 * be passed to this method.</p>
195 * <p>
196 * Options instance *should* not be disposed before all DBs using this options
197 * instance have been closed. If user doesn't call options dispose explicitly,
198 * then this options instance will be GC'd automatically</p>
199 * <p>
200 * ColumnFamily handles are disposed when the RocksDB instance is disposed.
201 * </p>
202 *
203 * @param path the path to the rocksdb.
204 * @param columnFamilyDescriptors list of column family descriptors
205 * @param columnFamilyHandles will be filled with ColumnFamilyHandle instances
206 * on open.
207 * @return a {@link RocksDB} instance on success, null if the specified
208 * {@link RocksDB} can not be opened.
209 *
210 * @throws RocksDBException thrown if error happens in underlying
211 * native library.
212 * @see DBOptions#setCreateIfMissing(boolean)
213 */
214 public static RocksDB open(final String path,
215 final List<ColumnFamilyDescriptor> columnFamilyDescriptors,
216 final List<ColumnFamilyHandle> columnFamilyHandles)
217 throws RocksDBException {
494da23a 218 final DBOptions options = new DBOptions();
7c673cae
FG
219 return open(options, path, columnFamilyDescriptors, columnFamilyHandles);
220 }
221
222 /**
223 * The factory constructor of RocksDB that opens a RocksDB instance given
224 * the path to the database using the specified options and db path.
225 *
226 * <p>
227 * Options instance *should* not be disposed before all DBs using this options
228 * instance have been closed. If user doesn't call options dispose explicitly,
229 * then this options instance will be GC'd automatically.</p>
230 * <p>
231 * Options instance can be re-used to open multiple DBs if DB statistics is
232 * not used. If DB statistics are required, then its recommended to open DB
233 * with new Options instance as underlying native statistics instance does not
234 * use any locks to prevent concurrent updates.</p>
235 *
236 * @param options {@link org.rocksdb.Options} instance.
237 * @param path the path to the rocksdb.
238 * @return a {@link RocksDB} instance on success, null if the specified
239 * {@link RocksDB} can not be opened.
240 *
241 * @throws RocksDBException thrown if error happens in underlying
242 * native library.
243 *
244 * @see Options#setCreateIfMissing(boolean)
245 */
246 public static RocksDB open(final Options options, final String path)
247 throws RocksDBException {
248 // when non-default Options is used, keeping an Options reference
249 // in RocksDB can prevent Java to GC during the life-time of
250 // the currently-created RocksDB.
251 final RocksDB db = new RocksDB(open(options.nativeHandle_, path));
252 db.storeOptionsInstance(options);
253 return db;
254 }
255
256 /**
257 * The factory constructor of RocksDB that opens a RocksDB instance given
258 * the path to the database using the specified options and db path and a list
259 * of column family names.
260 * <p>
261 * If opened in read write mode every existing column family name must be
262 * passed within the list to this method.</p>
263 * <p>
264 * If opened in read-only mode only a subset of existing column families must
265 * be passed to this method.</p>
266 * <p>
267 * Options instance *should* not be disposed before all DBs using this options
268 * instance have been closed. If user doesn't call options dispose explicitly,
269 * then this options instance will be GC'd automatically.</p>
270 * <p>
271 * Options instance can be re-used to open multiple DBs if DB statistics is
272 * not used. If DB statistics are required, then its recommended to open DB
273 * with new Options instance as underlying native statistics instance does not
274 * use any locks to prevent concurrent updates.</p>
275 * <p>
276 * ColumnFamily handles are disposed when the RocksDB instance is disposed.
277 * </p>
278 *
279 * @param options {@link org.rocksdb.DBOptions} instance.
280 * @param path the path to the rocksdb.
281 * @param columnFamilyDescriptors list of column family descriptors
282 * @param columnFamilyHandles will be filled with ColumnFamilyHandle instances
283 * on open.
284 * @return a {@link RocksDB} instance on success, null if the specified
285 * {@link RocksDB} can not be opened.
286 *
287 * @throws RocksDBException thrown if error happens in underlying
288 * native library.
289 *
290 * @see DBOptions#setCreateIfMissing(boolean)
291 */
292 public static RocksDB open(final DBOptions options, final String path,
293 final List<ColumnFamilyDescriptor> columnFamilyDescriptors,
294 final List<ColumnFamilyHandle> columnFamilyHandles)
295 throws RocksDBException {
296
297 final byte[][] cfNames = new byte[columnFamilyDescriptors.size()][];
298 final long[] cfOptionHandles = new long[columnFamilyDescriptors.size()];
299 for (int i = 0; i < columnFamilyDescriptors.size(); i++) {
300 final ColumnFamilyDescriptor cfDescriptor = columnFamilyDescriptors
301 .get(i);
f67539c2
TL
302 cfNames[i] = cfDescriptor.getName();
303 cfOptionHandles[i] = cfDescriptor.getOptions().nativeHandle_;
7c673cae
FG
304 }
305
306 final long[] handles = open(options.nativeHandle_, path, cfNames,
307 cfOptionHandles);
308 final RocksDB db = new RocksDB(handles[0]);
309 db.storeOptionsInstance(options);
310
311 for (int i = 1; i < handles.length; i++) {
20effc67
TL
312 final ColumnFamilyHandle columnFamilyHandle = new ColumnFamilyHandle(db, handles[i]);
313 columnFamilyHandles.add(columnFamilyHandle);
7c673cae
FG
314 }
315
20effc67
TL
316 db.ownedColumnFamilyHandles.addAll(columnFamilyHandles);
317
7c673cae
FG
318 return db;
319 }
320
321 /**
322 * The factory constructor of RocksDB that opens a RocksDB instance in
323 * Read-Only mode given the path to the database using the default
324 * options.
325 *
326 * @param path the path to the RocksDB.
327 * @return a {@link RocksDB} instance on success, null if the specified
328 * {@link RocksDB} can not be opened.
329 *
330 * @throws RocksDBException thrown if error happens in underlying
331 * native library.
332 */
333 public static RocksDB openReadOnly(final String path)
334 throws RocksDBException {
335 // This allows to use the rocksjni default Options instead of
336 // the c++ one.
20effc67 337 final Options options = new Options();
7c673cae
FG
338 return openReadOnly(options, path);
339 }
340
20effc67
TL
341 /**
342 * The factory constructor of RocksDB that opens a RocksDB instance in
343 * Read-Only mode given the path to the database using the specified
344 * options and db path.
345 *
346 * Options instance *should* not be disposed before all DBs using this options
347 * instance have been closed. If user doesn't call options dispose explicitly,
348 * then this options instance will be GC'd automatically.
349 *
350 * @param options {@link Options} instance.
351 * @param path the path to the RocksDB.
352 * @return a {@link RocksDB} instance on success, null if the specified
353 * {@link RocksDB} can not be opened.
354 *
355 * @throws RocksDBException thrown if error happens in underlying
356 * native library.
357 */
358 public static RocksDB openReadOnly(final Options options, final String path)
359 throws RocksDBException {
360 return openReadOnly(options, path, false);
361 }
362
363 /**
364 * The factory constructor of RocksDB that opens a RocksDB instance in
365 * Read-Only mode given the path to the database using the specified
366 * options and db path.
367 *
368 * Options instance *should* not be disposed before all DBs using this options
369 * instance have been closed. If user doesn't call options dispose explicitly,
370 * then this options instance will be GC'd automatically.
371 *
372 * @param options {@link Options} instance.
373 * @param path the path to the RocksDB.
374 * @param errorIfWalFileExists true to raise an error when opening the db
375 * if a Write Ahead Log file exists, false otherwise.
376 * @return a {@link RocksDB} instance on success, null if the specified
377 * {@link RocksDB} can not be opened.
378 *
379 * @throws RocksDBException thrown if error happens in underlying
380 * native library.
381 */
382 public static RocksDB openReadOnly(final Options options, final String path,
383 final boolean errorIfWalFileExists) throws RocksDBException {
384 // when non-default Options is used, keeping an Options reference
385 // in RocksDB can prevent Java to GC during the life-time of
386 // the currently-created RocksDB.
387 final RocksDB db = new RocksDB(openROnly(options.nativeHandle_, path, errorIfWalFileExists));
388 db.storeOptionsInstance(options);
389 return db;
390 }
391
7c673cae
FG
392 /**
393 * The factory constructor of RocksDB that opens a RocksDB instance in
394 * Read-Only mode given the path to the database using the default
395 * options.
396 *
397 * @param path the path to the RocksDB.
398 * @param columnFamilyDescriptors list of column family descriptors
399 * @param columnFamilyHandles will be filled with ColumnFamilyHandle instances
400 * on open.
401 * @return a {@link RocksDB} instance on success, null if the specified
402 * {@link RocksDB} can not be opened.
403 *
404 * @throws RocksDBException thrown if error happens in underlying
405 * native library.
406 */
407 public static RocksDB openReadOnly(final String path,
408 final List<ColumnFamilyDescriptor> columnFamilyDescriptors,
409 final List<ColumnFamilyHandle> columnFamilyHandles)
410 throws RocksDBException {
411 // This allows to use the rocksjni default Options instead of
412 // the c++ one.
413 final DBOptions options = new DBOptions();
20effc67 414 return openReadOnly(options, path, columnFamilyDescriptors, columnFamilyHandles, false);
7c673cae
FG
415 }
416
417 /**
418 * The factory constructor of RocksDB that opens a RocksDB instance in
419 * Read-Only mode given the path to the database using the specified
420 * options and db path.
421 *
20effc67
TL
422 * <p>This open method allows to open RocksDB using a subset of available
423 * column families</p>
424 * <p>Options instance *should* not be disposed before all DBs using this
425 * options instance have been closed. If user doesn't call options dispose
426 * explicitly,then this options instance will be GC'd automatically.</p>
7c673cae 427 *
20effc67 428 * @param options {@link DBOptions} instance.
7c673cae 429 * @param path the path to the RocksDB.
20effc67
TL
430 * @param columnFamilyDescriptors list of column family descriptors
431 * @param columnFamilyHandles will be filled with ColumnFamilyHandle instances
432 * on open.
7c673cae
FG
433 * @return a {@link RocksDB} instance on success, null if the specified
434 * {@link RocksDB} can not be opened.
435 *
436 * @throws RocksDBException thrown if error happens in underlying
437 * native library.
438 */
20effc67
TL
439 public static RocksDB openReadOnly(final DBOptions options, final String path,
440 final List<ColumnFamilyDescriptor> columnFamilyDescriptors,
441 final List<ColumnFamilyHandle> columnFamilyHandles) throws RocksDBException {
442 return openReadOnly(options, path, columnFamilyDescriptors, columnFamilyHandles, false);
7c673cae
FG
443 }
444
445 /**
446 * The factory constructor of RocksDB that opens a RocksDB instance in
447 * Read-Only mode given the path to the database using the specified
448 * options and db path.
449 *
450 * <p>This open method allows to open RocksDB using a subset of available
451 * column families</p>
452 * <p>Options instance *should* not be disposed before all DBs using this
453 * options instance have been closed. If user doesn't call options dispose
454 * explicitly,then this options instance will be GC'd automatically.</p>
455 *
456 * @param options {@link DBOptions} instance.
457 * @param path the path to the RocksDB.
458 * @param columnFamilyDescriptors list of column family descriptors
459 * @param columnFamilyHandles will be filled with ColumnFamilyHandle instances
460 * on open.
20effc67
TL
461 * @param errorIfWalFileExists true to raise an error when opening the db
462 * if a Write Ahead Log file exists, false otherwise.
7c673cae
FG
463 * @return a {@link RocksDB} instance on success, null if the specified
464 * {@link RocksDB} can not be opened.
465 *
466 * @throws RocksDBException thrown if error happens in underlying
467 * native library.
468 */
469 public static RocksDB openReadOnly(final DBOptions options, final String path,
470 final List<ColumnFamilyDescriptor> columnFamilyDescriptors,
20effc67 471 final List<ColumnFamilyHandle> columnFamilyHandles, final boolean errorIfWalFileExists)
7c673cae
FG
472 throws RocksDBException {
473 // when non-default Options is used, keeping an Options reference
474 // in RocksDB can prevent Java to GC during the life-time of
475 // the currently-created RocksDB.
476
477 final byte[][] cfNames = new byte[columnFamilyDescriptors.size()][];
478 final long[] cfOptionHandles = new long[columnFamilyDescriptors.size()];
479 for (int i = 0; i < columnFamilyDescriptors.size(); i++) {
480 final ColumnFamilyDescriptor cfDescriptor = columnFamilyDescriptors
481 .get(i);
f67539c2
TL
482 cfNames[i] = cfDescriptor.getName();
483 cfOptionHandles[i] = cfDescriptor.getOptions().nativeHandle_;
7c673cae
FG
484 }
485
20effc67
TL
486 final long[] handles =
487 openROnly(options.nativeHandle_, path, cfNames, cfOptionHandles, errorIfWalFileExists);
488 final RocksDB db = new RocksDB(handles[0]);
489 db.storeOptionsInstance(options);
490
491 for (int i = 1; i < handles.length; i++) {
492 final ColumnFamilyHandle columnFamilyHandle = new ColumnFamilyHandle(db, handles[i]);
493 columnFamilyHandles.add(columnFamilyHandle);
494 }
495
496 db.ownedColumnFamilyHandles.addAll(columnFamilyHandles);
497
498 return db;
499 }
500
501 /**
502 * Open DB as secondary instance with only the default column family.
503 *
504 * The secondary instance can dynamically tail the MANIFEST of
505 * a primary that must have already been created. User can call
506 * {@link #tryCatchUpWithPrimary()} to make the secondary instance catch up
507 * with primary (WAL tailing is NOT supported now) whenever the user feels
508 * necessary. Column families created by the primary after the secondary
509 * instance starts are currently ignored by the secondary instance.
510 * Column families opened by secondary and dropped by the primary will be
511 * dropped by secondary as well. However the user of the secondary instance
512 * can still access the data of such dropped column family as long as they
513 * do not destroy the corresponding column family handle.
514 * WAL tailing is not supported at present, but will arrive soon.
515 *
516 * @param options the options to open the secondary instance.
517 * @param path the path to the primary RocksDB instance.
518 * @param secondaryPath points to a directory where the secondary instance
519 * stores its info log
520 *
521 * @return a {@link RocksDB} instance on success, null if the specified
522 * {@link RocksDB} can not be opened.
523 *
524 * @throws RocksDBException thrown if error happens in underlying
525 * native library.
526 */
527 public static RocksDB openAsSecondary(final Options options, final String path,
528 final String secondaryPath) throws RocksDBException {
529 // when non-default Options is used, keeping an Options reference
530 // in RocksDB can prevent Java to GC during the life-time of
531 // the currently-created RocksDB.
532 final RocksDB db = new RocksDB(openAsSecondary(options.nativeHandle_, path, secondaryPath));
533 db.storeOptionsInstance(options);
534 return db;
535 }
536
537 /**
538 * Open DB as secondary instance with column families.
539 * You can open a subset of column families in secondary mode.
540 *
541 * The secondary instance can dynamically tail the MANIFEST of
542 * a primary that must have already been created. User can call
543 * {@link #tryCatchUpWithPrimary()} to make the secondary instance catch up
544 * with primary (WAL tailing is NOT supported now) whenever the user feels
545 * necessary. Column families created by the primary after the secondary
546 * instance starts are currently ignored by the secondary instance.
547 * Column families opened by secondary and dropped by the primary will be
548 * dropped by secondary as well. However the user of the secondary instance
549 * can still access the data of such dropped column family as long as they
550 * do not destroy the corresponding column family handle.
551 * WAL tailing is not supported at present, but will arrive soon.
552 *
553 * @param options the options to open the secondary instance.
554 * @param path the path to the primary RocksDB instance.
555 * @param secondaryPath points to a directory where the secondary instance
556 * stores its info log.
557 * @param columnFamilyDescriptors list of column family descriptors
558 * @param columnFamilyHandles will be filled with ColumnFamilyHandle instances
559 * on open.
560 *
561 * @return a {@link RocksDB} instance on success, null if the specified
562 * {@link RocksDB} can not be opened.
563 *
564 * @throws RocksDBException thrown if error happens in underlying
565 * native library.
566 */
567 public static RocksDB openAsSecondary(final DBOptions options, final String path,
568 final String secondaryPath, final List<ColumnFamilyDescriptor> columnFamilyDescriptors,
569 final List<ColumnFamilyHandle> columnFamilyHandles) throws RocksDBException {
570 // when non-default Options is used, keeping an Options reference
571 // in RocksDB can prevent Java to GC during the life-time of
572 // the currently-created RocksDB.
573
574 final byte[][] cfNames = new byte[columnFamilyDescriptors.size()][];
575 final long[] cfOptionHandles = new long[columnFamilyDescriptors.size()];
576 for (int i = 0; i < columnFamilyDescriptors.size(); i++) {
577 final ColumnFamilyDescriptor cfDescriptor = columnFamilyDescriptors.get(i);
578 cfNames[i] = cfDescriptor.getName();
579 cfOptionHandles[i] = cfDescriptor.getOptions().nativeHandle_;
580 }
581
582 final long[] handles =
583 openAsSecondary(options.nativeHandle_, path, secondaryPath, cfNames, cfOptionHandles);
7c673cae
FG
584 final RocksDB db = new RocksDB(handles[0]);
585 db.storeOptionsInstance(options);
586
587 for (int i = 1; i < handles.length; i++) {
20effc67
TL
588 final ColumnFamilyHandle columnFamilyHandle = new ColumnFamilyHandle(db, handles[i]);
589 columnFamilyHandles.add(columnFamilyHandle);
7c673cae
FG
590 }
591
20effc67
TL
592 db.ownedColumnFamilyHandles.addAll(columnFamilyHandles);
593
7c673cae
FG
594 return db;
595 }
494da23a
TL
596
597 /**
598 * This is similar to {@link #close()} except that it
599 * throws an exception if any error occurs.
600 *
601 * This will not fsync the WAL files.
602 * If syncing is required, the caller must first call {@link #syncWal()}
603 * or {@link #write(WriteOptions, WriteBatch)} using an empty write batch
604 * with {@link WriteOptions#setSync(boolean)} set to true.
605 *
606 * See also {@link #close()}.
607 *
608 * @throws RocksDBException if an error occurs whilst closing.
609 */
610 public void closeE() throws RocksDBException {
20effc67
TL
611 for (final ColumnFamilyHandle columnFamilyHandle : ownedColumnFamilyHandles) {
612 columnFamilyHandle.close();
613 }
614 ownedColumnFamilyHandles.clear();
615
494da23a
TL
616 if (owningHandle_.compareAndSet(true, false)) {
617 try {
618 closeDatabase(nativeHandle_);
619 } finally {
620 disposeInternal();
621 }
622 }
623 }
624
625 /**
626 * This is similar to {@link #closeE()} except that it
627 * silently ignores any errors.
628 *
629 * This will not fsync the WAL files.
630 * If syncing is required, the caller must first call {@link #syncWal()}
631 * or {@link #write(WriteOptions, WriteBatch)} using an empty write batch
632 * with {@link WriteOptions#setSync(boolean)} set to true.
633 *
634 * See also {@link #close()}.
635 */
636 @Override
637 public void close() {
20effc67
TL
638 for (final ColumnFamilyHandle columnFamilyHandle : ownedColumnFamilyHandles) {
639 columnFamilyHandle.close();
640 }
641 ownedColumnFamilyHandles.clear();
642
494da23a
TL
643 if (owningHandle_.compareAndSet(true, false)) {
644 try {
645 closeDatabase(nativeHandle_);
646 } catch (final RocksDBException e) {
647 // silently ignore the error report
648 } finally {
649 disposeInternal();
650 }
651 }
652 }
653
7c673cae
FG
654 /**
655 * Static method to determine all available column families for a
656 * rocksdb database identified by path
657 *
658 * @param options Options for opening the database
659 * @param path Absolute path to rocksdb database
660 * @return List&lt;byte[]&gt; List containing the column family names
661 *
662 * @throws RocksDBException thrown if error happens in underlying
663 * native library.
664 */
665 public static List<byte[]> listColumnFamilies(final Options options,
666 final String path) throws RocksDBException {
667 return Arrays.asList(RocksDB.listColumnFamilies(options.nativeHandle_,
668 path));
669 }
670
494da23a
TL
671 /**
672 * Creates a new column family with the name columnFamilyName and
673 * allocates a ColumnFamilyHandle within an internal structure.
674 * The ColumnFamilyHandle is automatically disposed with DB disposal.
675 *
676 * @param columnFamilyDescriptor column family to be created.
677 * @return {@link org.rocksdb.ColumnFamilyHandle} instance.
678 *
679 * @throws RocksDBException thrown if error happens in underlying
680 * native library.
681 */
682 public ColumnFamilyHandle createColumnFamily(
683 final ColumnFamilyDescriptor columnFamilyDescriptor)
684 throws RocksDBException {
20effc67
TL
685 final ColumnFamilyHandle columnFamilyHandle = new ColumnFamilyHandle(this,
686 createColumnFamily(nativeHandle_, columnFamilyDescriptor.getName(),
687 columnFamilyDescriptor.getName().length,
688 columnFamilyDescriptor.getOptions().nativeHandle_));
689 ownedColumnFamilyHandles.add(columnFamilyHandle);
690 return columnFamilyHandle;
494da23a
TL
691 }
692
693 /**
694 * Bulk create column families with the same column family options.
695 *
696 * @param columnFamilyOptions the options for the column families.
697 * @param columnFamilyNames the names of the column families.
698 *
699 * @return the handles to the newly created column families.
f67539c2
TL
700 *
701 * @throws RocksDBException if an error occurs whilst creating
702 * the column families
494da23a
TL
703 */
704 public List<ColumnFamilyHandle> createColumnFamilies(
705 final ColumnFamilyOptions columnFamilyOptions,
706 final List<byte[]> columnFamilyNames) throws RocksDBException {
707 final byte[][] cfNames = columnFamilyNames.toArray(
708 new byte[0][]);
709 final long[] cfHandles = createColumnFamilies(nativeHandle_,
710 columnFamilyOptions.nativeHandle_, cfNames);
711 final List<ColumnFamilyHandle> columnFamilyHandles =
712 new ArrayList<>(cfHandles.length);
713 for (int i = 0; i < cfHandles.length; i++) {
20effc67
TL
714 final ColumnFamilyHandle columnFamilyHandle = new ColumnFamilyHandle(this, cfHandles[i]);
715 columnFamilyHandles.add(columnFamilyHandle);
494da23a 716 }
20effc67 717 ownedColumnFamilyHandles.addAll(columnFamilyHandles);
494da23a
TL
718 return columnFamilyHandles;
719 }
720
721 /**
722 * Bulk create column families with the same column family options.
723 *
724 * @param columnFamilyDescriptors the descriptions of the column families.
725 *
726 * @return the handles to the newly created column families.
f67539c2
TL
727 *
728 * @throws RocksDBException if an error occurs whilst creating
729 * the column families
494da23a
TL
730 */
731 public List<ColumnFamilyHandle> createColumnFamilies(
732 final List<ColumnFamilyDescriptor> columnFamilyDescriptors)
733 throws RocksDBException {
734 final long[] cfOptsHandles = new long[columnFamilyDescriptors.size()];
735 final byte[][] cfNames = new byte[columnFamilyDescriptors.size()][];
736 for (int i = 0; i < columnFamilyDescriptors.size(); i++) {
737 final ColumnFamilyDescriptor columnFamilyDescriptor
738 = columnFamilyDescriptors.get(i);
739 cfOptsHandles[i] = columnFamilyDescriptor.getOptions().nativeHandle_;
740 cfNames[i] = columnFamilyDescriptor.getName();
741 }
742 final long[] cfHandles = createColumnFamilies(nativeHandle_,
743 cfOptsHandles, cfNames);
744 final List<ColumnFamilyHandle> columnFamilyHandles =
745 new ArrayList<>(cfHandles.length);
746 for (int i = 0; i < cfHandles.length; i++) {
20effc67
TL
747 final ColumnFamilyHandle columnFamilyHandle = new ColumnFamilyHandle(this, cfHandles[i]);
748 columnFamilyHandles.add(columnFamilyHandle);
494da23a 749 }
20effc67 750 ownedColumnFamilyHandles.addAll(columnFamilyHandles);
494da23a
TL
751 return columnFamilyHandles;
752 }
753
754 /**
755 * Drops the column family specified by {@code columnFamilyHandle}. This call
756 * only records a drop record in the manifest and prevents the column
757 * family from flushing and compacting.
758 *
759 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
760 * instance
761 *
762 * @throws RocksDBException thrown if error happens in underlying
763 * native library.
764 */
765 public void dropColumnFamily(final ColumnFamilyHandle columnFamilyHandle)
766 throws RocksDBException {
767 dropColumnFamily(nativeHandle_, columnFamilyHandle.nativeHandle_);
768 }
769
770 // Bulk drop column families. This call only records drop records in the
771 // manifest and prevents the column families from flushing and compacting.
772 // In case of error, the request may succeed partially. User may call
773 // ListColumnFamilies to check the result.
774 public void dropColumnFamilies(
775 final List<ColumnFamilyHandle> columnFamilies) throws RocksDBException {
776 final long[] cfHandles = new long[columnFamilies.size()];
777 for (int i = 0; i < columnFamilies.size(); i++) {
778 cfHandles[i] = columnFamilies.get(i).nativeHandle_;
779 }
780 dropColumnFamilies(nativeHandle_, cfHandles);
7c673cae
FG
781 }
782
20effc67
TL
783 /**
784 * Deletes native column family handle of given {@link ColumnFamilyHandle} Java object
785 * and removes reference from {@link RocksDB#ownedColumnFamilyHandles}.
786 *
787 * @param columnFamilyHandle column family handle object.
788 */
789 public void destroyColumnFamilyHandle(final ColumnFamilyHandle columnFamilyHandle) {
790 for (int i = 0; i < ownedColumnFamilyHandles.size(); ++i) {
791 final ColumnFamilyHandle ownedHandle = ownedColumnFamilyHandles.get(i);
792 if (ownedHandle.equals(columnFamilyHandle)) {
793 columnFamilyHandle.close();
794 ownedColumnFamilyHandles.remove(i);
795 return;
796 }
797 }
798 }
494da23a 799
7c673cae
FG
800 /**
801 * Set the database entry for "key" to "value".
802 *
803 * @param key the specified key to be inserted.
804 * @param value the value associated with the specified key.
805 *
806 * @throws RocksDBException thrown if error happens in underlying
807 * native library.
808 */
809 public void put(final byte[] key, final byte[] value)
810 throws RocksDBException {
811 put(nativeHandle_, key, 0, key.length, value, 0, value.length);
812 }
813
494da23a
TL
814 /**
815 * Set the database entry for "key" to "value".
816 *
817 * @param key The specified key to be inserted
818 * @param offset the offset of the "key" array to be used, must be
819 * non-negative and no larger than "key".length
820 * @param len the length of the "key" array to be used, must be non-negative
821 * and no larger than ("key".length - offset)
822 * @param value the value associated with the specified key
823 * @param vOffset the offset of the "value" array to be used, must be
824 * non-negative and no longer than "key".length
825 * @param vLen the length of the "value" array to be used, must be
826 * non-negative and no larger than ("value".length - offset)
827 *
828 * @throws RocksDBException thrown if errors happens in underlying native
829 * library.
830 * @throws IndexOutOfBoundsException if an offset or length is out of bounds
831 */
832 public void put(final byte[] key, final int offset, final int len,
833 final byte[] value, final int vOffset, final int vLen)
834 throws RocksDBException {
835 checkBounds(offset, len, key.length);
836 checkBounds(vOffset, vLen, value.length);
837 put(nativeHandle_, key, offset, len, value, vOffset, vLen);
838 }
839
7c673cae
FG
840 /**
841 * Set the database entry for "key" to "value" in the specified
842 * column family.
843 *
844 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
845 * instance
846 * @param key the specified key to be inserted.
847 * @param value the value associated with the specified key.
848 *
849 * throws IllegalArgumentException if column family is not present
850 *
851 * @throws RocksDBException thrown if error happens in underlying
852 * native library.
853 */
854 public void put(final ColumnFamilyHandle columnFamilyHandle,
855 final byte[] key, final byte[] value) throws RocksDBException {
856 put(nativeHandle_, key, 0, key.length, value, 0, value.length,
857 columnFamilyHandle.nativeHandle_);
858 }
859
494da23a
TL
860 /**
861 * Set the database entry for "key" to "value" in the specified
862 * column family.
863 *
864 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
865 * instance
866 * @param key The specified key to be inserted
867 * @param offset the offset of the "key" array to be used, must
868 * be non-negative and no larger than "key".length
869 * @param len the length of the "key" array to be used, must be non-negative
870 * and no larger than ("key".length - offset)
871 * @param value the value associated with the specified key
872 * @param vOffset the offset of the "value" array to be used, must be
873 * non-negative and no longer than "key".length
874 * @param vLen the length of the "value" array to be used, must be
875 * non-negative and no larger than ("value".length - offset)
876 *
877 * @throws RocksDBException thrown if errors happens in underlying native
878 * library.
879 * @throws IndexOutOfBoundsException if an offset or length is out of bounds
880 */
881 public void put(final ColumnFamilyHandle columnFamilyHandle,
882 final byte[] key, final int offset, final int len,
883 final byte[] value, final int vOffset, final int vLen)
884 throws RocksDBException {
885 checkBounds(offset, len, key.length);
886 checkBounds(vOffset, vLen, value.length);
887 put(nativeHandle_, key, offset, len, value, vOffset, vLen,
888 columnFamilyHandle.nativeHandle_);
889 }
890
7c673cae
FG
891 /**
892 * Set the database entry for "key" to "value".
893 *
894 * @param writeOpts {@link org.rocksdb.WriteOptions} instance.
895 * @param key the specified key to be inserted.
896 * @param value the value associated with the specified key.
897 *
898 * @throws RocksDBException thrown if error happens in underlying
899 * native library.
900 */
901 public void put(final WriteOptions writeOpts, final byte[] key,
902 final byte[] value) throws RocksDBException {
903 put(nativeHandle_, writeOpts.nativeHandle_,
904 key, 0, key.length, value, 0, value.length);
905 }
906
494da23a
TL
907 /**
908 * Set the database entry for "key" to "value".
909 *
910 * @param writeOpts {@link org.rocksdb.WriteOptions} instance.
911 * @param key The specified key to be inserted
912 * @param offset the offset of the "key" array to be used, must be
913 * non-negative and no larger than "key".length
914 * @param len the length of the "key" array to be used, must be non-negative
915 * and no larger than ("key".length - offset)
916 * @param value the value associated with the specified key
917 * @param vOffset the offset of the "value" array to be used, must be
918 * non-negative and no longer than "key".length
919 * @param vLen the length of the "value" array to be used, must be
920 * non-negative and no larger than ("value".length - offset)
921 *
922 * @throws RocksDBException thrown if error happens in underlying
923 * native library.
924 * @throws IndexOutOfBoundsException if an offset or length is out of bounds
925 */
926 public void put(final WriteOptions writeOpts,
927 final byte[] key, final int offset, final int len,
928 final byte[] value, final int vOffset, final int vLen)
929 throws RocksDBException {
930 checkBounds(offset, len, key.length);
931 checkBounds(vOffset, vLen, value.length);
932 put(nativeHandle_, writeOpts.nativeHandle_,
933 key, offset, len, value, vOffset, vLen);
934 }
935
7c673cae
FG
936 /**
937 * Set the database entry for "key" to "value" for the specified
938 * column family.
939 *
940 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
941 * instance
942 * @param writeOpts {@link org.rocksdb.WriteOptions} instance.
943 * @param key the specified key to be inserted.
944 * @param value the value associated with the specified key.
945 *
946 * throws IllegalArgumentException if column family is not present
947 *
948 * @throws RocksDBException thrown if error happens in underlying
949 * native library.
950 * @see IllegalArgumentException
951 */
952 public void put(final ColumnFamilyHandle columnFamilyHandle,
953 final WriteOptions writeOpts, final byte[] key,
954 final byte[] value) throws RocksDBException {
955 put(nativeHandle_, writeOpts.nativeHandle_, key, 0, key.length, value,
956 0, value.length, columnFamilyHandle.nativeHandle_);
957 }
958
f67539c2
TL
959 /**
960 * Set the database entry for "key" to "value" for the specified
961 * column family.
962 *
963 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
964 * instance
965 * @param writeOpts {@link org.rocksdb.WriteOptions} instance.
966 * @param key the specified key to be inserted. Position and limit is used.
967 * Supports direct buffer only.
968 * @param value the value associated with the specified key. Position and limit is used.
969 * Supports direct buffer only.
970 *
971 * throws IllegalArgumentException if column family is not present
972 *
973 * @throws RocksDBException thrown if error happens in underlying
974 * native library.
975 * @see IllegalArgumentException
976 */
977 public void put(final ColumnFamilyHandle columnFamilyHandle, final WriteOptions writeOpts,
978 final ByteBuffer key, final ByteBuffer value) throws RocksDBException {
979 assert key.isDirect() && value.isDirect();
980 putDirect(nativeHandle_, writeOpts.nativeHandle_, key, key.position(), key.remaining(), value,
981 value.position(), value.remaining(), columnFamilyHandle.nativeHandle_);
982 key.position(key.limit());
983 value.position(value.limit());
984 }
985
986 /**
987 * Set the database entry for "key" to "value".
988 *
989 * @param writeOpts {@link org.rocksdb.WriteOptions} instance.
990 * @param key the specified key to be inserted. Position and limit is used.
991 * Supports direct buffer only.
992 * @param value the value associated with the specified key. Position and limit is used.
993 * Supports direct buffer only.
994 *
995 * throws IllegalArgumentException if column family is not present
996 *
997 * @throws RocksDBException thrown if error happens in underlying
998 * native library.
999 * @see IllegalArgumentException
1000 */
1001 public void put(final WriteOptions writeOpts, final ByteBuffer key, final ByteBuffer value)
1002 throws RocksDBException {
1003 assert key.isDirect() && value.isDirect();
1004 putDirect(nativeHandle_, writeOpts.nativeHandle_, key, key.position(), key.remaining(), value,
1005 value.position(), value.remaining(), 0);
1006 key.position(key.limit());
1007 value.position(value.limit());
1008 }
1009
7c673cae 1010 /**
494da23a
TL
1011 * Set the database entry for "key" to "value" for the specified
1012 * column family.
7c673cae 1013 *
494da23a
TL
1014 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
1015 * instance
1016 * @param writeOpts {@link org.rocksdb.WriteOptions} instance.
1017 * @param key The specified key to be inserted
1018 * @param offset the offset of the "key" array to be used, must be
1019 * non-negative and no larger than "key".length
1020 * @param len the length of the "key" array to be used, must be non-negative
1021 * and no larger than ("key".length - offset)
1022 * @param value the value associated with the specified key
1023 * @param vOffset the offset of the "value" array to be used, must be
1024 * non-negative and no longer than "key".length
1025 * @param vLen the length of the "value" array to be used, must be
1026 * non-negative and no larger than ("value".length - offset)
7c673cae 1027 *
494da23a
TL
1028 * @throws RocksDBException thrown if error happens in underlying
1029 * native library.
1030 * @throws IndexOutOfBoundsException if an offset or length is out of bounds
7c673cae 1031 */
494da23a
TL
1032 public void put(final ColumnFamilyHandle columnFamilyHandle,
1033 final WriteOptions writeOpts,
1034 final byte[] key, final int offset, final int len,
1035 final byte[] value, final int vOffset, final int vLen)
1036 throws RocksDBException {
1037 checkBounds(offset, len, key.length);
1038 checkBounds(vOffset, vLen, value.length);
1039 put(nativeHandle_, writeOpts.nativeHandle_, key, offset, len, value,
1040 vOffset, vLen, columnFamilyHandle.nativeHandle_);
7c673cae
FG
1041 }
1042
1043 /**
494da23a
TL
1044 * Remove the database entry (if any) for "key". Returns OK on
1045 * success, and a non-OK status on error. It is not an error if "key"
1046 * did not exist in the database.
7c673cae 1047 *
494da23a 1048 * @param key Key to delete within database
7c673cae 1049 *
494da23a
TL
1050 * @throws RocksDBException thrown if error happens in underlying
1051 * native library.
1052 *
1053 * @deprecated Use {@link #delete(byte[])}
7c673cae 1054 */
494da23a
TL
1055 @Deprecated
1056 public void remove(final byte[] key) throws RocksDBException {
1057 delete(key);
7c673cae
FG
1058 }
1059
1060 /**
494da23a
TL
1061 * Delete the database entry (if any) for "key". Returns OK on
1062 * success, and a non-OK status on error. It is not an error if "key"
1063 * did not exist in the database.
7c673cae 1064 *
494da23a 1065 * @param key Key to delete within database
7c673cae 1066 *
494da23a
TL
1067 * @throws RocksDBException thrown if error happens in underlying
1068 * native library.
7c673cae 1069 */
494da23a
TL
1070 public void delete(final byte[] key) throws RocksDBException {
1071 delete(nativeHandle_, key, 0, key.length);
7c673cae
FG
1072 }
1073
1074 /**
494da23a
TL
1075 * Delete the database entry (if any) for "key". Returns OK on
1076 * success, and a non-OK status on error. It is not an error if "key"
1077 * did not exist in the database.
7c673cae 1078 *
494da23a
TL
1079 * @param key Key to delete within database
1080 * @param offset the offset of the "key" array to be used, must be
1081 * non-negative and no larger than "key".length
1082 * @param len the length of the "key" array to be used, must be
1083 * non-negative and no larger than ("key".length - offset)
7c673cae 1084 *
494da23a
TL
1085 * @throws RocksDBException thrown if error happens in underlying
1086 * native library.
7c673cae 1087 */
494da23a
TL
1088 public void delete(final byte[] key, final int offset, final int len)
1089 throws RocksDBException {
1090 delete(nativeHandle_, key, offset, len);
7c673cae
FG
1091 }
1092
1093 /**
494da23a
TL
1094 * Remove the database entry (if any) for "key". Returns OK on
1095 * success, and a non-OK status on error. It is not an error if "key"
1096 * did not exist in the database.
7c673cae 1097 *
494da23a
TL
1098 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
1099 * instance
1100 * @param key Key to delete within database
7c673cae
FG
1101 *
1102 * @throws RocksDBException thrown if error happens in underlying
1103 * native library.
494da23a
TL
1104 *
1105 * @deprecated Use {@link #delete(ColumnFamilyHandle, byte[])}
7c673cae 1106 */
494da23a
TL
1107 @Deprecated
1108 public void remove(final ColumnFamilyHandle columnFamilyHandle,
1109 final byte[] key) throws RocksDBException {
1110 delete(columnFamilyHandle, key);
7c673cae
FG
1111 }
1112
1113 /**
494da23a
TL
1114 * Delete the database entry (if any) for "key". Returns OK on
1115 * success, and a non-OK status on error. It is not an error if "key"
1116 * did not exist in the database.
7c673cae 1117 *
494da23a
TL
1118 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
1119 * instance
1120 * @param key Key to delete within database
7c673cae
FG
1121 *
1122 * @throws RocksDBException thrown if error happens in underlying
1123 * native library.
1124 */
494da23a
TL
1125 public void delete(final ColumnFamilyHandle columnFamilyHandle,
1126 final byte[] key) throws RocksDBException {
1127 delete(nativeHandle_, key, 0, key.length, columnFamilyHandle.nativeHandle_);
7c673cae
FG
1128 }
1129
1130 /**
494da23a
TL
1131 * Delete the database entry (if any) for "key". Returns OK on
1132 * success, and a non-OK status on error. It is not an error if "key"
1133 * did not exist in the database.
7c673cae 1134 *
494da23a
TL
1135 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
1136 * instance
1137 * @param key Key to delete within database
1138 * @param offset the offset of the "key" array to be used,
1139 * must be non-negative and no larger than "key".length
1140 * @param len the length of the "key" array to be used, must be non-negative
1141 * and no larger than ("value".length - offset)
7c673cae
FG
1142 *
1143 * @throws RocksDBException thrown if error happens in underlying
1144 * native library.
1145 */
494da23a
TL
1146 public void delete(final ColumnFamilyHandle columnFamilyHandle,
1147 final byte[] key, final int offset, final int len)
7c673cae 1148 throws RocksDBException {
494da23a 1149 delete(nativeHandle_, key, offset, len, columnFamilyHandle.nativeHandle_);
7c673cae
FG
1150 }
1151
1152 /**
494da23a
TL
1153 * Remove 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.
7c673cae 1156 *
494da23a
TL
1157 * @param writeOpt WriteOptions to be used with delete operation
1158 * @param key Key to delete within database
7c673cae
FG
1159 *
1160 * @throws RocksDBException thrown if error happens in underlying
1161 * native library.
494da23a
TL
1162 *
1163 * @deprecated Use {@link #delete(WriteOptions, byte[])}
7c673cae 1164 */
494da23a
TL
1165 @Deprecated
1166 public void remove(final WriteOptions writeOpt, final byte[] key)
1167 throws RocksDBException {
1168 delete(writeOpt, key);
7c673cae
FG
1169 }
1170
1171 /**
494da23a
TL
1172 * Delete the database entry (if any) for "key". Returns OK on
1173 * success, and a non-OK status on error. It is not an error if "key"
1174 * did not exist in the database.
7c673cae 1175 *
494da23a
TL
1176 * @param writeOpt WriteOptions to be used with delete operation
1177 * @param key Key to delete within database
7c673cae
FG
1178 *
1179 * @throws RocksDBException thrown if error happens in underlying
1180 * native library.
1181 */
494da23a
TL
1182 public void delete(final WriteOptions writeOpt, final byte[] key)
1183 throws RocksDBException {
1184 delete(nativeHandle_, writeOpt.nativeHandle_, key, 0, key.length);
7c673cae
FG
1185 }
1186
1187 /**
494da23a
TL
1188 * Delete the database entry (if any) for "key". Returns OK on
1189 * success, and a non-OK status on error. It is not an error if "key"
1190 * did not exist in the database.
7c673cae 1191 *
494da23a
TL
1192 * @param writeOpt WriteOptions to be used with delete operation
1193 * @param key Key to delete within database
1194 * @param offset the offset of the "key" array to be used, must be
1195 * non-negative and no larger than "key".length
1196 * @param len the length of the "key" array to be used, must be
1197 * non-negative and no larger than ("key".length - offset)
7c673cae
FG
1198 *
1199 * @throws RocksDBException thrown if error happens in underlying
1200 * native library.
1201 */
494da23a
TL
1202 public void delete(final WriteOptions writeOpt, final byte[] key,
1203 final int offset, final int len) throws RocksDBException {
1204 delete(nativeHandle_, writeOpt.nativeHandle_, key, offset, len);
7c673cae
FG
1205 }
1206
7c673cae 1207 /**
494da23a
TL
1208 * Remove the database entry (if any) for "key". Returns OK on
1209 * success, and a non-OK status on error. It is not an error if "key"
1210 * did not exist in the database.
1211 *
1212 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
1213 * instance
1214 * @param writeOpt WriteOptions to be used with delete operation
1215 * @param key Key to delete within database
7c673cae
FG
1216 *
1217 * @throws RocksDBException thrown if error happens in underlying
1218 * native library.
494da23a
TL
1219 *
1220 * @deprecated Use {@link #delete(ColumnFamilyHandle, WriteOptions, byte[])}
7c673cae 1221 */
494da23a
TL
1222 @Deprecated
1223 public void remove(final ColumnFamilyHandle columnFamilyHandle,
1224 final WriteOptions writeOpt, final byte[] key) throws RocksDBException {
1225 delete(columnFamilyHandle, writeOpt, key);
7c673cae
FG
1226 }
1227
1228 /**
494da23a
TL
1229 * Delete the database entry (if any) for "key". Returns OK on
1230 * success, and a non-OK status on error. It is not an error if "key"
1231 * did not exist in the database.
7c673cae
FG
1232 *
1233 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
1234 * instance
494da23a
TL
1235 * @param writeOpt WriteOptions to be used with delete operation
1236 * @param key Key to delete within database
7c673cae
FG
1237 *
1238 * @throws RocksDBException thrown if error happens in underlying
1239 * native library.
1240 */
494da23a
TL
1241 public void delete(final ColumnFamilyHandle columnFamilyHandle,
1242 final WriteOptions writeOpt, final byte[] key)
1243 throws RocksDBException {
1244 delete(nativeHandle_, writeOpt.nativeHandle_, key, 0, key.length,
7c673cae
FG
1245 columnFamilyHandle.nativeHandle_);
1246 }
1247
1248 /**
494da23a
TL
1249 * Delete the database entry (if any) for "key". Returns OK on
1250 * success, and a non-OK status on error. It is not an error if "key"
1251 * did not exist in the database.
7c673cae
FG
1252 *
1253 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
1254 * instance
494da23a
TL
1255 * @param writeOpt WriteOptions to be used with delete operation
1256 * @param key Key to delete within database
1257 * @param offset the offset of the "key" array to be used, must be
1258 * non-negative and no larger than "key".length
1259 * @param len the length of the "key" array to be used, must be
1260 * non-negative and no larger than ("key".length - offset)
7c673cae
FG
1261 *
1262 * @throws RocksDBException thrown if error happens in underlying
1263 * native library.
1264 */
494da23a
TL
1265 public void delete(final ColumnFamilyHandle columnFamilyHandle,
1266 final WriteOptions writeOpt, final byte[] key, final int offset,
1267 final int len) throws RocksDBException {
1268 delete(nativeHandle_, writeOpt.nativeHandle_, key, offset, len,
1269 columnFamilyHandle.nativeHandle_);
7c673cae
FG
1270 }
1271
f67539c2
TL
1272 /**
1273 * Get the value associated with the specified key within column family.
1274 *
1275 * @param opt {@link org.rocksdb.ReadOptions} instance.
1276 * @param key the key to retrieve the value. It is using position and limit.
1277 * Supports direct buffer only.
1278 * @param value the out-value to receive the retrieved value.
1279 * It is using position and limit. Limit is set according to value size.
1280 * Supports direct buffer only.
1281 * @return The size of the actual value that matches the specified
1282 * {@code key} in byte. If the return value is greater than the
1283 * length of {@code value}, then it indicates that the size of the
1284 * input buffer {@code value} is insufficient and partial result will
1285 * be returned. RocksDB.NOT_FOUND will be returned if the value not
1286 * found.
1287 *
1288 * @throws RocksDBException thrown if error happens in underlying
1289 * native library.
1290 */
1291 public int get(final ReadOptions opt, final ByteBuffer key, final ByteBuffer value)
1292 throws RocksDBException {
1293 assert key.isDirect() && value.isDirect();
1294 int result = getDirect(nativeHandle_, opt.nativeHandle_, key, key.position(), key.remaining(),
1295 value, value.position(), value.remaining(), 0);
1296 if (result != NOT_FOUND) {
1297 value.limit(Math.min(value.limit(), value.position() + result));
1298 }
1299 key.position(key.limit());
1300 return result;
1301 }
1302
1303 /**
1304 * Get the value associated with the specified key within column family.
1305 *
1306 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
1307 * instance
1308 * @param opt {@link org.rocksdb.ReadOptions} instance.
1309 * @param key the key to retrieve the value. It is using position and limit.
1310 * Supports direct buffer only.
1311 * @param value the out-value to receive the retrieved value.
1312 * It is using position and limit. Limit is set according to value size.
1313 * Supports direct buffer only.
1314 * @return The size of the actual value that matches the specified
1315 * {@code key} in byte. If the return value is greater than the
1316 * length of {@code value}, then it indicates that the size of the
1317 * input buffer {@code value} is insufficient and partial result will
1318 * be returned. RocksDB.NOT_FOUND will be returned if the value not
1319 * found.
1320 *
1321 * @throws RocksDBException thrown if error happens in underlying
1322 * native library.
1323 */
1324 public int get(final ColumnFamilyHandle columnFamilyHandle, final ReadOptions opt,
1325 final ByteBuffer key, final ByteBuffer value) throws RocksDBException {
1326 assert key.isDirect() && value.isDirect();
1327 int result = getDirect(nativeHandle_, opt.nativeHandle_, key, key.position(), key.remaining(),
1328 value, value.position(), value.remaining(), columnFamilyHandle.nativeHandle_);
1329 if (result != NOT_FOUND) {
1330 value.limit(Math.min(value.limit(), value.position() + result));
1331 }
1332 key.position(key.limit());
1333 return result;
1334 }
1335
7c673cae 1336 /**
494da23a
TL
1337 * Remove the database entry for {@code key}. Requires that the key exists
1338 * and was not overwritten. It is not an error if the key did not exist
1339 * in the database.
7c673cae 1340 *
494da23a
TL
1341 * If a key is overwritten (by calling {@link #put(byte[], byte[])} multiple
1342 * times), then the result of calling SingleDelete() on this key is undefined.
1343 * SingleDelete() only behaves correctly if there has been only one Put()
1344 * for this key since the previous call to SingleDelete() for this key.
1345 *
1346 * This feature is currently an experimental performance optimization
1347 * for a very specific workload. It is up to the caller to ensure that
1348 * SingleDelete is only used for a key that is not deleted using Delete() or
1349 * written using Merge(). Mixing SingleDelete operations with Deletes and
1350 * Merges can result in undefined behavior.
1351 *
1352 * @param key Key to delete within database
7c673cae
FG
1353 *
1354 * @throws RocksDBException thrown if error happens in underlying
494da23a 1355 * native library.
7c673cae 1356 */
494da23a
TL
1357 @Experimental("Performance optimization for a very specific workload")
1358 public void singleDelete(final byte[] key) throws RocksDBException {
1359 singleDelete(nativeHandle_, key, key.length);
7c673cae
FG
1360 }
1361
1362 /**
494da23a
TL
1363 * Remove the database entry for {@code key}. Requires that the key exists
1364 * and was not overwritten. It is not an error if the key did not exist
1365 * in the database.
7c673cae 1366 *
494da23a
TL
1367 * If a key is overwritten (by calling {@link #put(byte[], byte[])} multiple
1368 * times), then the result of calling SingleDelete() on this key is undefined.
1369 * SingleDelete() only behaves correctly if there has been only one Put()
1370 * for this key since the previous call to SingleDelete() for this key.
1371 *
1372 * This feature is currently an experimental performance optimization
1373 * for a very specific workload. It is up to the caller to ensure that
1374 * SingleDelete is only used for a key that is not deleted using Delete() or
1375 * written using Merge(). Mixing SingleDelete operations with Deletes and
1376 * Merges can result in undefined behavior.
1377 *
1378 * @param columnFamilyHandle The column family to delete the key from
1379 * @param key Key to delete within database
7c673cae
FG
1380 *
1381 * @throws RocksDBException thrown if error happens in underlying
494da23a 1382 * native library.
7c673cae 1383 */
494da23a
TL
1384 @Experimental("Performance optimization for a very specific workload")
1385 public void singleDelete(final ColumnFamilyHandle columnFamilyHandle,
7c673cae 1386 final byte[] key) throws RocksDBException {
494da23a 1387 singleDelete(nativeHandle_, key, key.length,
7c673cae
FG
1388 columnFamilyHandle.nativeHandle_);
1389 }
1390
1391 /**
494da23a
TL
1392 * Remove the database entry for {@code key}. Requires that the key exists
1393 * and was not overwritten. It is not an error if the key did not exist
1394 * in the database.
7c673cae 1395 *
494da23a
TL
1396 * If a key is overwritten (by calling {@link #put(byte[], byte[])} multiple
1397 * times), then the result of calling SingleDelete() on this key is undefined.
1398 * SingleDelete() only behaves correctly if there has been only one Put()
1399 * for this key since the previous call to SingleDelete() for this key.
7c673cae 1400 *
494da23a
TL
1401 * This feature is currently an experimental performance optimization
1402 * for a very specific workload. It is up to the caller to ensure that
1403 * SingleDelete is only used for a key that is not deleted using Delete() or
1404 * written using Merge(). Mixing SingleDelete operations with Deletes and
1405 * Merges can result in undefined behavior.
7c673cae 1406 *
494da23a
TL
1407 * Note: consider setting {@link WriteOptions#setSync(boolean)} true.
1408 *
1409 * @param writeOpt Write options for the delete
1410 * @param key Key to delete within database
7c673cae
FG
1411 *
1412 * @throws RocksDBException thrown if error happens in underlying
494da23a 1413 * native library.
7c673cae 1414 */
494da23a
TL
1415 @Experimental("Performance optimization for a very specific workload")
1416 public void singleDelete(final WriteOptions writeOpt, final byte[] key)
1417 throws RocksDBException {
1418 singleDelete(nativeHandle_, writeOpt.nativeHandle_, key, key.length);
7c673cae
FG
1419 }
1420
1421 /**
494da23a
TL
1422 * Remove the database entry for {@code key}. Requires that the key exists
1423 * and was not overwritten. It is not an error if the key did not exist
1424 * in the database.
7c673cae 1425 *
494da23a
TL
1426 * If a key is overwritten (by calling {@link #put(byte[], byte[])} multiple
1427 * times), then the result of calling SingleDelete() on this key is undefined.
1428 * SingleDelete() only behaves correctly if there has been only one Put()
1429 * for this key since the previous call to SingleDelete() for this key.
1430 *
1431 * This feature is currently an experimental performance optimization
1432 * for a very specific workload. It is up to the caller to ensure that
1433 * SingleDelete is only used for a key that is not deleted using Delete() or
1434 * written using Merge(). Mixing SingleDelete operations with Deletes and
1435 * Merges can result in undefined behavior.
1436 *
1437 * Note: consider setting {@link WriteOptions#setSync(boolean)} true.
1438 *
1439 * @param columnFamilyHandle The column family to delete the key from
1440 * @param writeOpt Write options for the delete
1441 * @param key Key to delete within database
7c673cae
FG
1442 *
1443 * @throws RocksDBException thrown if error happens in underlying
494da23a 1444 * native library.
7c673cae 1445 */
494da23a
TL
1446 @Experimental("Performance optimization for a very specific workload")
1447 public void singleDelete(final ColumnFamilyHandle columnFamilyHandle,
1448 final WriteOptions writeOpt, final byte[] key) throws RocksDBException {
1449 singleDelete(nativeHandle_, writeOpt.nativeHandle_, key, key.length,
1450 columnFamilyHandle.nativeHandle_);
7c673cae
FG
1451 }
1452
7c673cae
FG
1453
1454 /**
494da23a
TL
1455 * Removes the database entries in the range ["beginKey", "endKey"), i.e.,
1456 * including "beginKey" and excluding "endKey". a non-OK status on error. It
1457 * is not an error if no keys exist in the range ["beginKey", "endKey").
7c673cae 1458 *
494da23a
TL
1459 * Delete the database entry (if any) for "key". Returns OK on success, and a
1460 * non-OK status on error. It is not an error if "key" did not exist in the
1461 * database.
7c673cae 1462 *
494da23a
TL
1463 * @param beginKey First key to delete within database (inclusive)
1464 * @param endKey Last key to delete within database (exclusive)
1465 *
1466 * @throws RocksDBException thrown if error happens in underlying native
1467 * library.
7c673cae 1468 */
494da23a
TL
1469 public void deleteRange(final byte[] beginKey, final byte[] endKey)
1470 throws RocksDBException {
1471 deleteRange(nativeHandle_, beginKey, 0, beginKey.length, endKey, 0,
1472 endKey.length);
7c673cae
FG
1473 }
1474
1475 /**
494da23a
TL
1476 * Removes the database entries in the range ["beginKey", "endKey"), i.e.,
1477 * including "beginKey" and excluding "endKey". a non-OK status on error. It
1478 * is not an error if no keys exist in the range ["beginKey", "endKey").
7c673cae 1479 *
494da23a
TL
1480 * Delete the database entry (if any) for "key". Returns OK on success, and a
1481 * non-OK status on error. It is not an error if "key" did not exist in the
1482 * database.
7c673cae 1483 *
494da23a
TL
1484 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} instance
1485 * @param beginKey First key to delete within database (inclusive)
1486 * @param endKey Last key to delete within database (exclusive)
1487 *
1488 * @throws RocksDBException thrown if error happens in underlying native
1489 * library.
7c673cae 1490 */
494da23a
TL
1491 public void deleteRange(final ColumnFamilyHandle columnFamilyHandle,
1492 final byte[] beginKey, final byte[] endKey) throws RocksDBException {
1493 deleteRange(nativeHandle_, beginKey, 0, beginKey.length, endKey, 0,
1494 endKey.length, columnFamilyHandle.nativeHandle_);
7c673cae
FG
1495 }
1496
1497 /**
494da23a
TL
1498 * Removes the database entries in the range ["beginKey", "endKey"), i.e.,
1499 * including "beginKey" and excluding "endKey". a non-OK status on error. It
1500 * is not an error if no keys exist in the range ["beginKey", "endKey").
7c673cae 1501 *
494da23a
TL
1502 * Delete the database entry (if any) for "key". Returns OK on success, and a
1503 * non-OK status on error. It is not an error if "key" did not exist in the
1504 * database.
1505 *
1506 * @param writeOpt WriteOptions to be used with delete operation
1507 * @param beginKey First key to delete within database (inclusive)
1508 * @param endKey Last key to delete within database (exclusive)
7c673cae
FG
1509 *
1510 * @throws RocksDBException thrown if error happens in underlying
494da23a 1511 * native library.
7c673cae 1512 */
494da23a
TL
1513 public void deleteRange(final WriteOptions writeOpt, final byte[] beginKey,
1514 final byte[] endKey) throws RocksDBException {
1515 deleteRange(nativeHandle_, writeOpt.nativeHandle_, beginKey, 0,
1516 beginKey.length, endKey, 0, endKey.length);
7c673cae
FG
1517 }
1518
1519 /**
494da23a
TL
1520 * Removes the database entries in the range ["beginKey", "endKey"), i.e.,
1521 * including "beginKey" and excluding "endKey". a non-OK status on error. It
1522 * is not an error if no keys exist in the range ["beginKey", "endKey").
7c673cae 1523 *
494da23a
TL
1524 * Delete the database entry (if any) for "key". Returns OK on success, and a
1525 * non-OK status on error. It is not an error if "key" did not exist in the
1526 * database.
7c673cae 1527 *
494da23a
TL
1528 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} instance
1529 * @param writeOpt WriteOptions to be used with delete operation
1530 * @param beginKey First key to delete within database (included)
1531 * @param endKey Last key to delete within database (excluded)
7c673cae 1532 *
494da23a
TL
1533 * @throws RocksDBException thrown if error happens in underlying native
1534 * library.
7c673cae 1535 */
494da23a
TL
1536 public void deleteRange(final ColumnFamilyHandle columnFamilyHandle,
1537 final WriteOptions writeOpt, final byte[] beginKey, final byte[] endKey)
1538 throws RocksDBException {
1539 deleteRange(nativeHandle_, writeOpt.nativeHandle_, beginKey, 0,
1540 beginKey.length, endKey, 0, endKey.length,
1541 columnFamilyHandle.nativeHandle_);
7c673cae
FG
1542 }
1543
494da23a 1544
7c673cae 1545 /**
494da23a 1546 * Add merge operand for key/value pair.
7c673cae 1547 *
494da23a
TL
1548 * @param key the specified key to be merged.
1549 * @param value the value to be merged with the current value for the
1550 * specified key.
7c673cae
FG
1551 *
1552 * @throws RocksDBException thrown if error happens in underlying
1553 * native library.
1554 */
494da23a
TL
1555 public void merge(final byte[] key, final byte[] value)
1556 throws RocksDBException {
1557 merge(nativeHandle_, key, 0, key.length, value, 0, value.length);
7c673cae
FG
1558 }
1559
1560 /**
494da23a 1561 * Add merge operand for key/value pair.
7c673cae 1562 *
494da23a
TL
1563 * @param key the specified key to be merged.
1564 * @param offset the offset of the "key" array to be used, must be
1565 * non-negative and no larger than "key".length
1566 * @param len the length of the "key" array to be used, must be non-negative
1567 * and no larger than ("key".length - offset)
1568 * @param value the value to be merged with the current value for the
1569 * specified key.
1570 * @param vOffset the offset of the "value" array to be used, must be
1571 * non-negative and no longer than "key".length
1572 * @param vLen the length of the "value" array to be used, must be
1573 * non-negative and must be non-negative and no larger than
1574 * ("value".length - offset)
7c673cae
FG
1575 *
1576 * @throws RocksDBException thrown if error happens in underlying
1577 * native library.
494da23a 1578 * @throws IndexOutOfBoundsException if an offset or length is out of bounds
7c673cae 1579 */
494da23a
TL
1580 public void merge(final byte[] key, int offset, int len, final byte[] value,
1581 final int vOffset, final int vLen) throws RocksDBException {
1582 checkBounds(offset, len, key.length);
1583 checkBounds(vOffset, vLen, value.length);
1584 merge(nativeHandle_, key, offset, len, value, vOffset, vLen);
7c673cae
FG
1585 }
1586
1587 /**
494da23a 1588 * Add merge operand for key/value pair in a ColumnFamily.
7c673cae 1589 *
494da23a
TL
1590 * @param columnFamilyHandle {@link ColumnFamilyHandle} instance
1591 * @param key the specified key to be merged.
1592 * @param value the value to be merged with the current value for
1593 * the specified key.
7c673cae
FG
1594 *
1595 * @throws RocksDBException thrown if error happens in underlying
1596 * native library.
1597 */
494da23a
TL
1598 public void merge(final ColumnFamilyHandle columnFamilyHandle,
1599 final byte[] key, final byte[] value) throws RocksDBException {
1600 merge(nativeHandle_, key, 0, key.length, value, 0, value.length,
1601 columnFamilyHandle.nativeHandle_);
7c673cae
FG
1602 }
1603
1604 /**
494da23a 1605 * Add merge operand for key/value pair in a ColumnFamily.
7c673cae 1606 *
494da23a
TL
1607 * @param columnFamilyHandle {@link ColumnFamilyHandle} instance
1608 * @param key the specified key to be merged.
1609 * @param offset the offset of the "key" array to be used, must be
1610 * non-negative and no larger than "key".length
1611 * @param len the length of the "key" array to be used, must be non-negative
1612 * and no larger than ("key".length - offset)
1613 * @param value the value to be merged with the current value for
1614 * the specified key.
1615 * @param vOffset the offset of the "value" array to be used, must be
1616 * non-negative and no longer than "key".length
1617 * @param vLen the length of the "value" array to be used, must be
1618 * must be non-negative and no larger than ("value".length - offset)
7c673cae
FG
1619 *
1620 * @throws RocksDBException thrown if error happens in underlying
1621 * native library.
494da23a
TL
1622 * @throws IndexOutOfBoundsException if an offset or length is out of bounds
1623 */
1624 public void merge(final ColumnFamilyHandle columnFamilyHandle,
1625 final byte[] key, final int offset, final int len, final byte[] value,
1626 final int vOffset, final int vLen) throws RocksDBException {
1627 checkBounds(offset, len, key.length);
1628 checkBounds(vOffset, vLen, value.length);
1629 merge(nativeHandle_, key, offset, len, value, vOffset, vLen,
1630 columnFamilyHandle.nativeHandle_);
1631 }
1632
1633 /**
1634 * Add merge operand for key/value pair.
7c673cae 1635 *
494da23a
TL
1636 * @param writeOpts {@link WriteOptions} for this write.
1637 * @param key the specified key to be merged.
1638 * @param value the value to be merged with the current value for
1639 * the specified key.
1640 *
1641 * @throws RocksDBException thrown if error happens in underlying
1642 * native library.
7c673cae 1643 */
494da23a
TL
1644 public void merge(final WriteOptions writeOpts, final byte[] key,
1645 final byte[] value) throws RocksDBException {
1646 merge(nativeHandle_, writeOpts.nativeHandle_,
1647 key, 0, key.length, value, 0, value.length);
7c673cae
FG
1648 }
1649
1650 /**
494da23a 1651 * Add merge operand for key/value pair.
7c673cae 1652 *
494da23a
TL
1653 * @param writeOpts {@link WriteOptions} for this write.
1654 * @param key the specified key to be merged.
1655 * @param offset the offset of the "key" array to be used, must be
1656 * non-negative and no larger than "key".length
1657 * @param len the length of the "key" array to be used, must be non-negative
1658 * and no larger than ("value".length - offset)
1659 * @param value the value to be merged with the current value for
1660 * the specified key.
1661 * @param vOffset the offset of the "value" array to be used, must be
1662 * non-negative and no longer than "key".length
1663 * @param vLen the length of the "value" array to be used, must be
1664 * non-negative and no larger than ("value".length - offset)
7c673cae
FG
1665 *
1666 * @throws RocksDBException thrown if error happens in underlying
1667 * native library.
494da23a 1668 * @throws IndexOutOfBoundsException if an offset or length is out of bounds
7c673cae 1669 */
494da23a
TL
1670 public void merge(final WriteOptions writeOpts,
1671 final byte[] key, final int offset, final int len,
1672 final byte[] value, final int vOffset, final int vLen)
7c673cae 1673 throws RocksDBException {
494da23a
TL
1674 checkBounds(offset, len, key.length);
1675 checkBounds(vOffset, vLen, value.length);
1676 merge(nativeHandle_, writeOpts.nativeHandle_,
1677 key, offset, len, value, vOffset, vLen);
7c673cae
FG
1678 }
1679
f67539c2
TL
1680 /**
1681 * Delete the database entry (if any) for "key". Returns OK on
1682 * success, and a non-OK status on error. It is not an error if "key"
1683 * did not exist in the database.
1684 *
1685 * @param writeOpt WriteOptions to be used with delete operation
1686 * @param key Key to delete within database. It is using position and limit.
1687 * Supports direct buffer only.
1688 *
1689 * @throws RocksDBException thrown if error happens in underlying
1690 * native library.
1691 */
1692 public void delete(final WriteOptions writeOpt, final ByteBuffer key) throws RocksDBException {
1693 assert key.isDirect();
1694 deleteDirect(nativeHandle_, writeOpt.nativeHandle_, key, key.position(), key.remaining(), 0);
1695 key.position(key.limit());
1696 }
1697
1698 /**
1699 * Delete the database entry (if any) for "key". Returns OK on
1700 * success, and a non-OK status on error. It is not an error if "key"
1701 * did not exist in the database.
1702 *
1703 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
1704 * instance
1705 * @param writeOpt WriteOptions to be used with delete operation
1706 * @param key Key to delete within database. It is using position and limit.
1707 * Supports direct buffer only.
1708 *
1709 * @throws RocksDBException thrown if error happens in underlying
1710 * native library.
1711 */
1712 public void delete(final ColumnFamilyHandle columnFamilyHandle, final WriteOptions writeOpt,
1713 final ByteBuffer key) throws RocksDBException {
1714 assert key.isDirect();
1715 deleteDirect(nativeHandle_, writeOpt.nativeHandle_, key, key.position(), key.remaining(),
1716 columnFamilyHandle.nativeHandle_);
1717 key.position(key.limit());
1718 }
1719
7c673cae 1720 /**
494da23a 1721 * Add merge operand for key/value pair.
7c673cae 1722 *
494da23a
TL
1723 * @param columnFamilyHandle {@link ColumnFamilyHandle} instance
1724 * @param writeOpts {@link WriteOptions} for this write.
1725 * @param key the specified key to be merged.
1726 * @param value the value to be merged with the current value for the
1727 * specified key.
7c673cae
FG
1728 *
1729 * @throws RocksDBException thrown if error happens in underlying
1730 * native library.
7c673cae 1731 */
494da23a
TL
1732 public void merge(final ColumnFamilyHandle columnFamilyHandle,
1733 final WriteOptions writeOpts, final byte[] key, final byte[] value)
7c673cae 1734 throws RocksDBException {
494da23a
TL
1735 merge(nativeHandle_, writeOpts.nativeHandle_,
1736 key, 0, key.length, value, 0, value.length,
1737 columnFamilyHandle.nativeHandle_);
7c673cae
FG
1738 }
1739
1740 /**
494da23a 1741 * Add merge operand for key/value pair.
7c673cae 1742 *
494da23a
TL
1743 * @param columnFamilyHandle {@link ColumnFamilyHandle} instance
1744 * @param writeOpts {@link WriteOptions} for this write.
1745 * @param key the specified key to be merged.
1746 * @param offset the offset of the "key" array to be used, must be
1747 * non-negative and no larger than "key".length
1748 * @param len the length of the "key" array to be used, must be non-negative
1749 * and no larger than ("key".length - offset)
1750 * @param value the value to be merged with the current value for
1751 * the specified key.
1752 * @param vOffset the offset of the "value" array to be used, must be
1753 * non-negative and no longer than "key".length
1754 * @param vLen the length of the "value" array to be used, must be
1755 * non-negative and no larger than ("value".length - offset)
7c673cae
FG
1756 *
1757 * @throws RocksDBException thrown if error happens in underlying
1758 * native library.
494da23a 1759 * @throws IndexOutOfBoundsException if an offset or length is out of bounds
7c673cae 1760 */
494da23a
TL
1761 public void merge(
1762 final ColumnFamilyHandle columnFamilyHandle, final WriteOptions writeOpts,
1763 final byte[] key, final int offset, final int len,
1764 final byte[] value, final int vOffset, final int vLen)
7c673cae 1765 throws RocksDBException {
494da23a
TL
1766 checkBounds(offset, len, key.length);
1767 checkBounds(vOffset, vLen, value.length);
1768 merge(nativeHandle_, writeOpts.nativeHandle_,
1769 key, offset, len, value, vOffset, vLen,
7c673cae
FG
1770 columnFamilyHandle.nativeHandle_);
1771 }
1772
1773 /**
494da23a 1774 * Apply the specified updates to the database.
7c673cae 1775 *
494da23a
TL
1776 * @param writeOpts WriteOptions instance
1777 * @param updates WriteBatch instance
7c673cae 1778 *
494da23a
TL
1779 * @throws RocksDBException thrown if error happens in underlying
1780 * native library.
1781 */
1782 public void write(final WriteOptions writeOpts, final WriteBatch updates)
1783 throws RocksDBException {
1784 write0(nativeHandle_, writeOpts.nativeHandle_, updates.nativeHandle_);
1785 }
1786
1787 /**
1788 * Apply the specified updates to the database.
7c673cae 1789 *
494da23a
TL
1790 * @param writeOpts WriteOptions instance
1791 * @param updates WriteBatchWithIndex instance
7c673cae
FG
1792 *
1793 * @throws RocksDBException thrown if error happens in underlying
494da23a 1794 * native library.
7c673cae 1795 */
494da23a
TL
1796 public void write(final WriteOptions writeOpts,
1797 final WriteBatchWithIndex updates) throws RocksDBException {
1798 write1(nativeHandle_, writeOpts.nativeHandle_, updates.nativeHandle_);
7c673cae
FG
1799 }
1800
494da23a
TL
1801 // TODO(AR) we should improve the #get() API, returning -1 (RocksDB.NOT_FOUND) is not very nice
1802 // when we could communicate better status into, also the C++ code show that -2 could be returned
1803
7c673cae 1804 /**
494da23a 1805 * Get the value associated with the specified key within column family*
7c673cae 1806 *
494da23a
TL
1807 * @param key the key to retrieve the value.
1808 * @param value the out-value to receive the retrieved value.
7c673cae 1809 *
494da23a
TL
1810 * @return The size of the actual value that matches the specified
1811 * {@code key} in byte. If the return value is greater than the
1812 * length of {@code value}, then it indicates that the size of the
1813 * input buffer {@code value} is insufficient and partial result will
1814 * be returned. RocksDB.NOT_FOUND will be returned if the value not
1815 * found.
7c673cae
FG
1816 *
1817 * @throws RocksDBException thrown if error happens in underlying
494da23a 1818 * native library.
7c673cae 1819 */
494da23a
TL
1820 public int get(final byte[] key, final byte[] value) throws RocksDBException {
1821 return get(nativeHandle_, key, 0, key.length, value, 0, value.length);
7c673cae
FG
1822 }
1823
1824 /**
494da23a 1825 * Get the value associated with the specified key within column family*
7c673cae 1826 *
494da23a
TL
1827 * @param key the key to retrieve the value.
1828 * @param offset the offset of the "key" array to be used, must be
1829 * non-negative and no larger than "key".length
1830 * @param len the length of the "key" array to be used, must be non-negative
1831 * and no larger than ("key".length - offset)
1832 * @param value the out-value to receive the retrieved value.
1833 * @param vOffset the offset of the "value" array to be used, must be
1834 * non-negative and no longer than "value".length
1835 * @param vLen the length of the "value" array to be used, must be
1836 * non-negative and and no larger than ("value".length - offset)
7c673cae 1837 *
494da23a
TL
1838 * @return The size of the actual value that matches the specified
1839 * {@code key} in byte. If the return value is greater than the
1840 * length of {@code value}, then it indicates that the size of the
1841 * input buffer {@code value} is insufficient and partial result will
1842 * be returned. RocksDB.NOT_FOUND will be returned if the value not
1843 * found.
7c673cae
FG
1844 *
1845 * @throws RocksDBException thrown if error happens in underlying
494da23a 1846 * native library.
7c673cae 1847 */
494da23a
TL
1848 public int get(final byte[] key, final int offset, final int len,
1849 final byte[] value, final int vOffset, final int vLen)
7c673cae 1850 throws RocksDBException {
494da23a
TL
1851 checkBounds(offset, len, key.length);
1852 checkBounds(vOffset, vLen, value.length);
1853 return get(nativeHandle_, key, offset, len, value, vOffset, vLen);
7c673cae
FG
1854 }
1855
1856 /**
494da23a 1857 * Get the value associated with the specified key within column family.
7c673cae 1858 *
494da23a
TL
1859 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
1860 * instance
1861 * @param key the key to retrieve the value.
1862 * @param value the out-value to receive the retrieved value.
1863 * @return The size of the actual value that matches the specified
1864 * {@code key} in byte. If the return value is greater than the
1865 * length of {@code value}, then it indicates that the size of the
1866 * input buffer {@code value} is insufficient and partial result will
1867 * be returned. RocksDB.NOT_FOUND will be returned if the value not
1868 * found.
7c673cae
FG
1869 *
1870 * @throws RocksDBException thrown if error happens in underlying
494da23a 1871 * native library.
7c673cae 1872 */
494da23a
TL
1873 public int get(final ColumnFamilyHandle columnFamilyHandle, final byte[] key,
1874 final byte[] value) throws RocksDBException, IllegalArgumentException {
1875 return get(nativeHandle_, key, 0, key.length, value, 0, value.length,
7c673cae
FG
1876 columnFamilyHandle.nativeHandle_);
1877 }
1878
1879 /**
494da23a 1880 * Get the value associated with the specified key within column family.
7c673cae 1881 *
494da23a
TL
1882 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
1883 * instance
1884 * @param key the key to retrieve the value.
1885 * @param offset the offset of the "key" array to be used, must be
1886 * non-negative and no larger than "key".length
1887 * @param len the length of the "key" array to be used, must be non-negative
1888 * an no larger than ("key".length - offset)
1889 * @param value the out-value to receive the retrieved value.
1890 * @param vOffset the offset of the "value" array to be used, must be
1891 * non-negative and no longer than "key".length
1892 * @param vLen the length of the "value" array to be used, must be
1893 * non-negative and no larger than ("value".length - offset)
7c673cae 1894 *
494da23a
TL
1895 * @return The size of the actual value that matches the specified
1896 * {@code key} in byte. If the return value is greater than the
1897 * length of {@code value}, then it indicates that the size of the
1898 * input buffer {@code value} is insufficient and partial result will
1899 * be returned. RocksDB.NOT_FOUND will be returned if the value not
1900 * found.
1901 *
1902 * @throws RocksDBException thrown if error happens in underlying
1903 * native library.
1904 */
1905 public int get(final ColumnFamilyHandle columnFamilyHandle, final byte[] key,
1906 final int offset, final int len, final byte[] value, final int vOffset,
1907 final int vLen) throws RocksDBException, IllegalArgumentException {
1908 checkBounds(offset, len, key.length);
1909 checkBounds(vOffset, vLen, value.length);
1910 return get(nativeHandle_, key, offset, len, value, vOffset, vLen,
1911 columnFamilyHandle.nativeHandle_);
1912 }
1913
1914 /**
1915 * Get the value associated with the specified key.
1916 *
1917 * @param opt {@link org.rocksdb.ReadOptions} instance.
1918 * @param key the key to retrieve the value.
1919 * @param value the out-value to receive the retrieved value.
1920 * @return The size of the actual value that matches the specified
1921 * {@code key} in byte. If the return value is greater than the
1922 * length of {@code value}, then it indicates that the size of the
1923 * input buffer {@code value} is insufficient and partial result will
1924 * be returned. RocksDB.NOT_FOUND will be returned if the value not
1925 * found.
1926 *
1927 * @throws RocksDBException thrown if error happens in underlying
1928 * native library.
1929 */
1930 public int get(final ReadOptions opt, final byte[] key,
1931 final byte[] value) throws RocksDBException {
1932 return get(nativeHandle_, opt.nativeHandle_,
1933 key, 0, key.length, value, 0, value.length);
1934 }
1935
1936 /**
1937 * Get the value associated with the specified key.
1938 *
1939 * @param opt {@link org.rocksdb.ReadOptions} instance.
1940 * @param key the key to retrieve the value.
1941 * @param offset the offset of the "key" array to be used, must be
1942 * non-negative and no larger than "key".length
1943 * @param len the length of the "key" array to be used, must be non-negative
1944 * and no larger than ("key".length - offset)
1945 * @param value the out-value to receive the retrieved value.
1946 * @param vOffset the offset of the "value" array to be used, must be
1947 * non-negative and no longer than "key".length
1948 * @param vLen the length of the "value" array to be used, must be
1949 * non-negative and no larger than ("value".length - offset)
1950 * @return The size of the actual value that matches the specified
1951 * {@code key} in byte. If the return value is greater than the
1952 * length of {@code value}, then it indicates that the size of the
1953 * input buffer {@code value} is insufficient and partial result will
1954 * be returned. RocksDB.NOT_FOUND will be returned if the value not
1955 * found.
1956 *
1957 * @throws RocksDBException thrown if error happens in underlying
1958 * native library.
1959 */
1960 public int get(final ReadOptions opt, final byte[] key, final int offset,
1961 final int len, final byte[] value, final int vOffset, final int vLen)
1962 throws RocksDBException {
1963 checkBounds(offset, len, key.length);
1964 checkBounds(vOffset, vLen, value.length);
1965 return get(nativeHandle_, opt.nativeHandle_,
1966 key, offset, len, value, vOffset, vLen);
1967 }
1968
1969 /**
1970 * Get the value associated with the specified key within column family.
7c673cae
FG
1971 *
1972 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
1973 * instance
494da23a
TL
1974 * @param opt {@link org.rocksdb.ReadOptions} instance.
1975 * @param key the key to retrieve the value.
1976 * @param value the out-value to receive the retrieved value.
1977 * @return The size of the actual value that matches the specified
1978 * {@code key} in byte. If the return value is greater than the
1979 * length of {@code value}, then it indicates that the size of the
1980 * input buffer {@code value} is insufficient and partial result will
1981 * be returned. RocksDB.NOT_FOUND will be returned if the value not
1982 * found.
1983 *
1984 * @throws RocksDBException thrown if error happens in underlying
1985 * native library.
1986 */
1987 public int get(final ColumnFamilyHandle columnFamilyHandle,
1988 final ReadOptions opt, final byte[] key, final byte[] value)
1989 throws RocksDBException {
1990 return get(nativeHandle_, opt.nativeHandle_, key, 0, key.length, value,
1991 0, value.length, columnFamilyHandle.nativeHandle_);
1992 }
1993
1994 /**
1995 * Get the value associated with the specified key within column family.
1996 *
1997 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
1998 * instance
1999 * @param opt {@link org.rocksdb.ReadOptions} instance.
2000 * @param key the key to retrieve the value.
2001 * @param offset the offset of the "key" array to be used, must be
2002 * non-negative and no larger than "key".length
2003 * @param len the length of the "key" array to be used, must be
2004 * non-negative and and no larger than ("key".length - offset)
2005 * @param value the out-value to receive the retrieved value.
2006 * @param vOffset the offset of the "value" array to be used, must be
2007 * non-negative and no longer than "key".length
2008 * @param vLen the length of the "value" array to be used, and must be
2009 * non-negative and no larger than ("value".length - offset)
2010 * @return The size of the actual value that matches the specified
2011 * {@code key} in byte. If the return value is greater than the
2012 * length of {@code value}, then it indicates that the size of the
2013 * input buffer {@code value} is insufficient and partial result will
2014 * be returned. RocksDB.NOT_FOUND will be returned if the value not
2015 * found.
2016 *
2017 * @throws RocksDBException thrown if error happens in underlying
2018 * native library.
2019 */
2020 public int get(final ColumnFamilyHandle columnFamilyHandle,
2021 final ReadOptions opt, final byte[] key, final int offset, final int len,
2022 final byte[] value, final int vOffset, final int vLen)
2023 throws RocksDBException {
2024 checkBounds(offset, len, key.length);
2025 checkBounds(vOffset, vLen, value.length);
2026 return get(nativeHandle_, opt.nativeHandle_, key, offset, len, value,
2027 vOffset, vLen, columnFamilyHandle.nativeHandle_);
2028 }
2029
2030 /**
2031 * The simplified version of get which returns a new byte array storing
2032 * the value associated with the specified input key if any. null will be
2033 * returned if the specified key is not found.
2034 *
2035 * @param key the key retrieve the value.
2036 * @return a byte array storing the value associated with the input key if
2037 * any. null if it does not find the specified key.
2038 *
2039 * @throws RocksDBException thrown if error happens in underlying
2040 * native library.
2041 */
2042 public byte[] get(final byte[] key) throws RocksDBException {
2043 return get(nativeHandle_, key, 0, key.length);
2044 }
2045
2046 /**
2047 * The simplified version of get which returns a new byte array storing
2048 * the value associated with the specified input key if any. null will be
2049 * returned if the specified key is not found.
2050 *
2051 * @param key the key retrieve the value.
2052 * @param offset the offset of the "key" array to be used, must be
2053 * non-negative and no larger than "key".length
2054 * @param len the length of the "key" array to be used, must be non-negative
2055 * and no larger than ("key".length - offset)
2056 * @return a byte array storing the value associated with the input key if
2057 * any. null if it does not find the specified key.
2058 *
2059 * @throws RocksDBException thrown if error happens in underlying
2060 * native library.
2061 */
2062 public byte[] get(final byte[] key, final int offset,
2063 final int len) throws RocksDBException {
2064 checkBounds(offset, len, key.length);
2065 return get(nativeHandle_, key, offset, len);
2066 }
2067
2068 /**
2069 * The simplified version of get which returns a new byte array storing
2070 * the value associated with the specified input key if any. null will be
2071 * returned if the specified key is not found.
2072 *
2073 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
2074 * instance
2075 * @param key the key retrieve the value.
2076 * @return a byte array storing the value associated with the input key if
2077 * any. null if it does not find the specified key.
2078 *
2079 * @throws RocksDBException thrown if error happens in underlying
2080 * native library.
2081 */
2082 public byte[] get(final ColumnFamilyHandle columnFamilyHandle,
2083 final byte[] key) throws RocksDBException {
2084 return get(nativeHandle_, key, 0, key.length,
2085 columnFamilyHandle.nativeHandle_);
2086 }
2087
2088 /**
2089 * The simplified version of get which returns a new byte array storing
2090 * the value associated with the specified input key if any. null will be
2091 * returned if the specified key is not found.
2092 *
2093 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
2094 * instance
2095 * @param key the key retrieve the value.
2096 * @param offset the offset of the "key" array to be used, must be
2097 * non-negative and no larger than "key".length
2098 * @param len the length of the "key" array to be used, must be non-negative
2099 * and no larger than ("key".length - offset)
2100 * @return a byte array storing the value associated with the input key if
2101 * any. null if it does not find the specified key.
2102 *
2103 * @throws RocksDBException thrown if error happens in underlying
2104 * native library.
2105 */
2106 public byte[] get(final ColumnFamilyHandle columnFamilyHandle,
2107 final byte[] key, final int offset, final int len)
2108 throws RocksDBException {
2109 checkBounds(offset, len, key.length);
2110 return get(nativeHandle_, key, offset, len,
2111 columnFamilyHandle.nativeHandle_);
2112 }
2113
2114 /**
2115 * The simplified version of get which returns a new byte array storing
2116 * the value associated with the specified input key if any. null will be
2117 * returned if the specified key is not found.
2118 *
2119 * @param key the key retrieve the value.
2120 * @param opt Read options.
2121 * @return a byte array storing the value associated with the input key if
2122 * any. null if it does not find the specified key.
2123 *
2124 * @throws RocksDBException thrown if error happens in underlying
2125 * native library.
2126 */
2127 public byte[] get(final ReadOptions opt, final byte[] key)
2128 throws RocksDBException {
2129 return get(nativeHandle_, opt.nativeHandle_, key, 0, key.length);
2130 }
2131
2132 /**
2133 * The simplified version of get which returns a new byte array storing
2134 * the value associated with the specified input key if any. null will be
2135 * returned if the specified key is not found.
2136 *
2137 * @param key the key retrieve the value.
2138 * @param offset the offset of the "key" array to be used, must be
2139 * non-negative and no larger than "key".length
2140 * @param len the length of the "key" array to be used, must be non-negative
2141 * and no larger than ("key".length - offset)
2142 * @param opt Read options.
2143 * @return a byte array storing the value associated with the input key if
2144 * any. null if it does not find the specified key.
2145 *
2146 * @throws RocksDBException thrown if error happens in underlying
2147 * native library.
2148 */
2149 public byte[] get(final ReadOptions opt, final byte[] key, final int offset,
2150 final int len) throws RocksDBException {
2151 checkBounds(offset, len, key.length);
2152 return get(nativeHandle_, opt.nativeHandle_, key, offset, len);
2153 }
2154
2155 /**
2156 * The simplified version of get which returns a new byte array storing
2157 * the value associated with the specified input key if any. null will be
2158 * returned if the specified key is not found.
2159 *
2160 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
2161 * instance
2162 * @param key the key retrieve the value.
2163 * @param opt Read options.
2164 * @return a byte array storing the value associated with the input key if
2165 * any. null if it does not find the specified key.
2166 *
2167 * @throws RocksDBException thrown if error happens in underlying
2168 * native library.
2169 */
2170 public byte[] get(final ColumnFamilyHandle columnFamilyHandle,
2171 final ReadOptions opt, final byte[] key) throws RocksDBException {
2172 return get(nativeHandle_, opt.nativeHandle_, key, 0, key.length,
2173 columnFamilyHandle.nativeHandle_);
2174 }
2175
2176 /**
2177 * The simplified version of get which returns a new byte array storing
2178 * the value associated with the specified input key if any. null will be
2179 * returned if the specified key is not found.
2180 *
2181 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
2182 * instance
2183 * @param key the key retrieve the value.
2184 * @param offset the offset of the "key" array to be used, must be
2185 * non-negative and no larger than "key".length
2186 * @param len the length of the "key" array to be used, must be non-negative
2187 * and no larger than ("key".length - offset)
2188 * @param opt Read options.
2189 * @return a byte array storing the value associated with the input key if
2190 * any. null if it does not find the specified key.
2191 *
2192 * @throws RocksDBException thrown if error happens in underlying
2193 * native library.
2194 */
2195 public byte[] get(final ColumnFamilyHandle columnFamilyHandle,
2196 final ReadOptions opt, final byte[] key, final int offset, final int len)
2197 throws RocksDBException {
2198 checkBounds(offset, len, key.length);
2199 return get(nativeHandle_, opt.nativeHandle_, key, offset, len,
2200 columnFamilyHandle.nativeHandle_);
2201 }
2202
2203 /**
2204 * Returns a map of keys for which values were found in DB.
2205 *
2206 * @param keys List of keys for which values need to be retrieved.
2207 * @return Map where key of map is the key passed by user and value for map
2208 * entry is the corresponding value in DB.
2209 *
2210 * @throws RocksDBException thrown if error happens in underlying
2211 * native library.
2212 *
2213 * @deprecated Consider {@link #multiGetAsList(List)} instead.
2214 */
2215 @Deprecated
2216 public Map<byte[], byte[]> multiGet(final List<byte[]> keys)
2217 throws RocksDBException {
2218 assert(keys.size() != 0);
2219
2220 final byte[][] keysArray = keys.toArray(new byte[0][]);
2221 final int keyOffsets[] = new int[keysArray.length];
2222 final int keyLengths[] = new int[keysArray.length];
2223 for(int i = 0; i < keyLengths.length; i++) {
2224 keyLengths[i] = keysArray[i].length;
2225 }
2226
2227 final byte[][] values = multiGet(nativeHandle_, keysArray, keyOffsets,
2228 keyLengths);
2229
2230 final Map<byte[], byte[]> keyValueMap =
2231 new HashMap<>(computeCapacityHint(values.length));
2232 for(int i = 0; i < values.length; i++) {
2233 if(values[i] == null) {
2234 continue;
2235 }
2236
2237 keyValueMap.put(keys.get(i), values[i]);
2238 }
2239
2240 return keyValueMap;
2241 }
2242
2243 /**
2244 * Returns a map of keys for which values were found in DB.
2245 * <p>
2246 * Note: Every key needs to have a related column family name in
2247 * {@code columnFamilyHandleList}.
2248 * </p>
2249 *
2250 * @param columnFamilyHandleList {@link java.util.List} containing
2251 * {@link org.rocksdb.ColumnFamilyHandle} instances.
2252 * @param keys List of keys for which values need to be retrieved.
2253 * @return Map where key of map is the key passed by user and value for map
2254 * entry is the corresponding value in DB.
2255 *
2256 * @throws RocksDBException thrown if error happens in underlying
2257 * native library.
2258 * @throws IllegalArgumentException thrown if the size of passed keys is not
2259 * equal to the amount of passed column family handles.
2260 *
2261 * @deprecated Consider {@link #multiGetAsList(List, List)} instead.
2262 */
2263 @Deprecated
2264 public Map<byte[], byte[]> multiGet(
2265 final List<ColumnFamilyHandle> columnFamilyHandleList,
2266 final List<byte[]> keys) throws RocksDBException,
2267 IllegalArgumentException {
2268 assert(keys.size() != 0);
2269 // Check if key size equals cfList size. If not a exception must be
2270 // thrown. If not a Segmentation fault happens.
2271 if (keys.size() != columnFamilyHandleList.size()) {
2272 throw new IllegalArgumentException(
2273 "For each key there must be a ColumnFamilyHandle.");
2274 }
2275 final long[] cfHandles = new long[columnFamilyHandleList.size()];
2276 for (int i = 0; i < columnFamilyHandleList.size(); i++) {
2277 cfHandles[i] = columnFamilyHandleList.get(i).nativeHandle_;
2278 }
2279
2280 final byte[][] keysArray = keys.toArray(new byte[0][]);
2281 final int keyOffsets[] = new int[keysArray.length];
2282 final int keyLengths[] = new int[keysArray.length];
2283 for(int i = 0; i < keyLengths.length; i++) {
2284 keyLengths[i] = keysArray[i].length;
2285 }
2286
2287 final byte[][] values = multiGet(nativeHandle_, keysArray, keyOffsets,
2288 keyLengths, cfHandles);
2289
2290 final Map<byte[], byte[]> keyValueMap =
2291 new HashMap<>(computeCapacityHint(values.length));
2292 for(int i = 0; i < values.length; i++) {
2293 if (values[i] == null) {
2294 continue;
2295 }
2296 keyValueMap.put(keys.get(i), values[i]);
2297 }
2298 return keyValueMap;
2299 }
2300
2301 /**
2302 * Returns a map of keys for which values were found in DB.
2303 *
2304 * @param opt Read options.
2305 * @param keys of keys for which values need to be retrieved.
2306 * @return Map where key of map is the key passed by user and value for map
2307 * entry is the corresponding value in DB.
2308 *
2309 * @throws RocksDBException thrown if error happens in underlying
2310 * native library.
2311 *
2312 * @deprecated Consider {@link #multiGetAsList(ReadOptions, List)} instead.
2313 */
2314 @Deprecated
2315 public Map<byte[], byte[]> multiGet(final ReadOptions opt,
2316 final List<byte[]> keys) throws RocksDBException {
2317 assert(keys.size() != 0);
2318
2319 final byte[][] keysArray = keys.toArray(new byte[0][]);
2320 final int keyOffsets[] = new int[keysArray.length];
2321 final int keyLengths[] = new int[keysArray.length];
2322 for(int i = 0; i < keyLengths.length; i++) {
2323 keyLengths[i] = keysArray[i].length;
2324 }
2325
2326 final byte[][] values = multiGet(nativeHandle_, opt.nativeHandle_,
2327 keysArray, keyOffsets, keyLengths);
2328
2329 final Map<byte[], byte[]> keyValueMap =
2330 new HashMap<>(computeCapacityHint(values.length));
2331 for(int i = 0; i < values.length; i++) {
2332 if(values[i] == null) {
2333 continue;
2334 }
2335
2336 keyValueMap.put(keys.get(i), values[i]);
2337 }
2338
2339 return keyValueMap;
2340 }
2341
2342 /**
2343 * Returns a map of keys for which values were found in DB.
2344 * <p>
2345 * Note: Every key needs to have a related column family name in
2346 * {@code columnFamilyHandleList}.
2347 * </p>
2348 *
2349 * @param opt Read options.
2350 * @param columnFamilyHandleList {@link java.util.List} containing
2351 * {@link org.rocksdb.ColumnFamilyHandle} instances.
2352 * @param keys of keys for which values need to be retrieved.
2353 * @return Map where key of map is the key passed by user and value for map
2354 * entry is the corresponding value in DB.
2355 *
2356 * @throws RocksDBException thrown if error happens in underlying
2357 * native library.
2358 * @throws IllegalArgumentException thrown if the size of passed keys is not
2359 * equal to the amount of passed column family handles.
2360 *
2361 * @deprecated Consider {@link #multiGetAsList(ReadOptions, List, List)}
2362 * instead.
2363 */
2364 @Deprecated
2365 public Map<byte[], byte[]> multiGet(final ReadOptions opt,
2366 final List<ColumnFamilyHandle> columnFamilyHandleList,
2367 final List<byte[]> keys) throws RocksDBException {
2368 assert(keys.size() != 0);
2369 // Check if key size equals cfList size. If not a exception must be
2370 // thrown. If not a Segmentation fault happens.
2371 if (keys.size()!=columnFamilyHandleList.size()){
2372 throw new IllegalArgumentException(
2373 "For each key there must be a ColumnFamilyHandle.");
2374 }
2375 final long[] cfHandles = new long[columnFamilyHandleList.size()];
2376 for (int i = 0; i < columnFamilyHandleList.size(); i++) {
2377 cfHandles[i] = columnFamilyHandleList.get(i).nativeHandle_;
2378 }
2379
2380 final byte[][] keysArray = keys.toArray(new byte[0][]);
2381 final int keyOffsets[] = new int[keysArray.length];
2382 final int keyLengths[] = new int[keysArray.length];
2383 for(int i = 0; i < keyLengths.length; i++) {
2384 keyLengths[i] = keysArray[i].length;
2385 }
2386
2387 final byte[][] values = multiGet(nativeHandle_, opt.nativeHandle_,
2388 keysArray, keyOffsets, keyLengths, cfHandles);
2389
2390 final Map<byte[], byte[]> keyValueMap
2391 = new HashMap<>(computeCapacityHint(values.length));
2392 for(int i = 0; i < values.length; i++) {
2393 if(values[i] == null) {
2394 continue;
2395 }
2396 keyValueMap.put(keys.get(i), values[i]);
2397 }
2398
2399 return keyValueMap;
2400 }
2401
2402 /**
2403 * Takes a list of keys, and returns a list of values for the given list of
2404 * keys. List will contain null for keys which could not be found.
2405 *
2406 * @param keys List of keys for which values need to be retrieved.
2407 * @return List of values for the given list of keys. List will contain
2408 * null for keys which could not be found.
2409 *
2410 * @throws RocksDBException thrown if error happens in underlying
2411 * native library.
2412 */
2413 public List<byte[]> multiGetAsList(final List<byte[]> keys)
2414 throws RocksDBException {
2415 assert(keys.size() != 0);
2416
2417 final byte[][] keysArray = keys.toArray(new byte[keys.size()][]);
2418 final int keyOffsets[] = new int[keysArray.length];
2419 final int keyLengths[] = new int[keysArray.length];
2420 for(int i = 0; i < keyLengths.length; i++) {
2421 keyLengths[i] = keysArray[i].length;
2422 }
2423
2424 return Arrays.asList(multiGet(nativeHandle_, keysArray, keyOffsets,
2425 keyLengths));
2426 }
2427
2428 /**
2429 * Returns a list of values for the given list of keys. List will contain
2430 * null for keys which could not be found.
2431 * <p>
2432 * Note: Every key needs to have a related column family name in
2433 * {@code columnFamilyHandleList}.
2434 * </p>
2435 *
2436 * @param columnFamilyHandleList {@link java.util.List} containing
2437 * {@link org.rocksdb.ColumnFamilyHandle} instances.
2438 * @param keys List of keys for which values need to be retrieved.
2439 * @return List of values for the given list of keys. List will contain
2440 * null for keys which could not be found.
2441 *
2442 * @throws RocksDBException thrown if error happens in underlying
2443 * native library.
2444 * @throws IllegalArgumentException thrown if the size of passed keys is not
2445 * equal to the amount of passed column family handles.
2446 */
2447 public List<byte[]> multiGetAsList(
2448 final List<ColumnFamilyHandle> columnFamilyHandleList,
2449 final List<byte[]> keys) throws RocksDBException,
2450 IllegalArgumentException {
2451 assert(keys.size() != 0);
2452 // Check if key size equals cfList size. If not a exception must be
2453 // thrown. If not a Segmentation fault happens.
2454 if (keys.size() != columnFamilyHandleList.size()) {
2455 throw new IllegalArgumentException(
2456 "For each key there must be a ColumnFamilyHandle.");
2457 }
2458 final long[] cfHandles = new long[columnFamilyHandleList.size()];
2459 for (int i = 0; i < columnFamilyHandleList.size(); i++) {
2460 cfHandles[i] = columnFamilyHandleList.get(i).nativeHandle_;
2461 }
2462
2463 final byte[][] keysArray = keys.toArray(new byte[keys.size()][]);
2464 final int keyOffsets[] = new int[keysArray.length];
2465 final int keyLengths[] = new int[keysArray.length];
2466 for(int i = 0; i < keyLengths.length; i++) {
2467 keyLengths[i] = keysArray[i].length;
2468 }
2469
2470 return Arrays.asList(multiGet(nativeHandle_, keysArray, keyOffsets,
2471 keyLengths, cfHandles));
2472 }
2473
2474 /**
2475 * Returns a list of values for the given list of keys. List will contain
2476 * null for keys which could not be found.
2477 *
2478 * @param opt Read options.
2479 * @param keys of keys for which values need to be retrieved.
2480 * @return List of values for the given list of keys. List will contain
2481 * null for keys which could not be found.
7c673cae
FG
2482 *
2483 * @throws RocksDBException thrown if error happens in underlying
2484 * native library.
2485 */
494da23a
TL
2486 public List<byte[]> multiGetAsList(final ReadOptions opt,
2487 final List<byte[]> keys) throws RocksDBException {
2488 assert(keys.size() != 0);
2489
2490 final byte[][] keysArray = keys.toArray(new byte[keys.size()][]);
2491 final int keyOffsets[] = new int[keysArray.length];
2492 final int keyLengths[] = new int[keysArray.length];
2493 for(int i = 0; i < keyLengths.length; i++) {
2494 keyLengths[i] = keysArray[i].length;
2495 }
2496
2497 return Arrays.asList(multiGet(nativeHandle_, opt.nativeHandle_,
2498 keysArray, keyOffsets, keyLengths));
7c673cae
FG
2499 }
2500
2501 /**
494da23a
TL
2502 * Returns a list of values for the given list of keys. List will contain
2503 * null for keys which could not be found.
2504 * <p>
2505 * Note: Every key needs to have a related column family name in
2506 * {@code columnFamilyHandleList}.
2507 * </p>
7c673cae 2508 *
494da23a
TL
2509 * @param opt Read options.
2510 * @param columnFamilyHandleList {@link java.util.List} containing
2511 * {@link org.rocksdb.ColumnFamilyHandle} instances.
2512 * @param keys of keys for which values need to be retrieved.
2513 * @return List of values for the given list of keys. List will contain
2514 * null for keys which could not be found.
7c673cae 2515 *
494da23a
TL
2516 * @throws RocksDBException thrown if error happens in underlying
2517 * native library.
2518 * @throws IllegalArgumentException thrown if the size of passed keys is not
2519 * equal to the amount of passed column family handles.
7c673cae 2520 */
494da23a
TL
2521 public List<byte[]> multiGetAsList(final ReadOptions opt,
2522 final List<ColumnFamilyHandle> columnFamilyHandleList,
2523 final List<byte[]> keys) throws RocksDBException {
2524 assert(keys.size() != 0);
2525 // Check if key size equals cfList size. If not a exception must be
2526 // thrown. If not a Segmentation fault happens.
2527 if (keys.size()!=columnFamilyHandleList.size()){
2528 throw new IllegalArgumentException(
2529 "For each key there must be a ColumnFamilyHandle.");
2530 }
2531 final long[] cfHandles = new long[columnFamilyHandleList.size()];
2532 for (int i = 0; i < columnFamilyHandleList.size(); i++) {
2533 cfHandles[i] = columnFamilyHandleList.get(i).nativeHandle_;
2534 }
2535
2536 final byte[][] keysArray = keys.toArray(new byte[keys.size()][]);
2537 final int keyOffsets[] = new int[keysArray.length];
2538 final int keyLengths[] = new int[keysArray.length];
2539 for(int i = 0; i < keyLengths.length; i++) {
2540 keyLengths[i] = keysArray[i].length;
2541 }
2542
2543 return Arrays.asList(multiGet(nativeHandle_, opt.nativeHandle_,
2544 keysArray, keyOffsets, keyLengths, cfHandles));
7c673cae
FG
2545 }
2546
2547 /**
494da23a 2548 * If the key definitely does not exist in the database, then this method
f67539c2
TL
2549 * returns null, else it returns an instance of KeyMayExistResult
2550 *
2551 * If the caller wants to obtain value when the key
2552 * is found in memory, then {@code valueHolder} must be set.
7c673cae 2553 *
f67539c2
TL
2554 * This check is potentially lighter-weight than invoking
2555 * {@link #get(byte[])}. One way to make this lighter weight is to avoid
2556 * doing any IOs.
7c673cae 2557 *
494da23a 2558 * @param key byte array of a key to search for
f67539c2
TL
2559 * @param valueHolder non-null to retrieve the value if it is found, or null
2560 * if the value is not needed. If non-null, upon return of the function,
2561 * the {@code value} will be set if it could be retrieved.
2562 *
2563 * @return false if the key definitely does not exist in the database,
2564 * otherwise true.
7c673cae 2565 */
f67539c2
TL
2566 public boolean keyMayExist(final byte[] key,
2567 /* @Nullable */ final Holder<byte[]> valueHolder) {
2568 return keyMayExist(key, 0, key.length, valueHolder);
7c673cae
FG
2569 }
2570
2571 /**
494da23a 2572 * If the key definitely does not exist in the database, then this method
f67539c2
TL
2573 * returns null, else it returns an instance of KeyMayExistResult
2574 *
2575 * If the caller wants to obtain value when the key
2576 * is found in memory, then {@code valueHolder} must be set.
7c673cae 2577 *
f67539c2
TL
2578 * This check is potentially lighter-weight than invoking
2579 * {@link #get(byte[], int, int)}. One way to make this lighter weight is to
2580 * avoid doing any IOs.
7c673cae 2581 *
494da23a
TL
2582 * @param key byte array of a key to search for
2583 * @param offset the offset of the "key" array to be used, must be
2584 * non-negative and no larger than "key".length
2585 * @param len the length of the "key" array to be used, must be non-negative
2586 * and no larger than "key".length
f67539c2
TL
2587 * @param valueHolder non-null to retrieve the value if it is found, or null
2588 * if the value is not needed. If non-null, upon return of the function,
2589 * the {@code value} will be set if it could be retrieved.
7c673cae 2590 *
f67539c2
TL
2591 * @return false if the key definitely does not exist in the database,
2592 * otherwise true.
7c673cae 2593 */
f67539c2
TL
2594 public boolean keyMayExist(final byte[] key,
2595 final int offset, final int len,
2596 /* @Nullable */ final Holder<byte[]> valueHolder) {
2597 return keyMayExist((ColumnFamilyHandle)null, key, offset, len, valueHolder);
7c673cae
FG
2598 }
2599
2600 /**
494da23a 2601 * If the key definitely does not exist in the database, then this method
f67539c2 2602 * returns null, else it returns an instance of KeyMayExistResult
7c673cae 2603 *
f67539c2
TL
2604 * If the caller wants to obtain value when the key
2605 * is found in memory, then {@code valueHolder} must be set.
2606 *
2607 * This check is potentially lighter-weight than invoking
2608 * {@link #get(ColumnFamilyHandle,byte[])}. One way to make this lighter
2609 * weight is to avoid doing any IOs.
7c673cae 2610 *
494da23a
TL
2611 * @param columnFamilyHandle {@link ColumnFamilyHandle} instance
2612 * @param key byte array of a key to search for
f67539c2
TL
2613 * @param valueHolder non-null to retrieve the value if it is found, or null
2614 * if the value is not needed. If non-null, upon return of the function,
2615 * the {@code value} will be set if it could be retrieved.
2616 *
2617 * @return false if the key definitely does not exist in the database,
2618 * otherwise true.
7c673cae 2619 */
f67539c2
TL
2620 public boolean keyMayExist(
2621 final ColumnFamilyHandle columnFamilyHandle, final byte[] key,
2622 /* @Nullable */ final Holder<byte[]> valueHolder) {
2623 return keyMayExist(columnFamilyHandle, key, 0, key.length,
2624 valueHolder);
7c673cae
FG
2625 }
2626
2627 /**
494da23a 2628 * If the key definitely does not exist in the database, then this method
f67539c2 2629 * returns null, else it returns an instance of KeyMayExistResult
7c673cae 2630 *
f67539c2
TL
2631 * If the caller wants to obtain value when the key
2632 * is found in memory, then {@code valueHolder} must be set.
2633 *
2634 * This check is potentially lighter-weight than invoking
2635 * {@link #get(ColumnFamilyHandle, byte[], int, int)}. One way to make this
2636 * lighter weight is to avoid doing any IOs.
7c673cae 2637 *
494da23a
TL
2638 * @param columnFamilyHandle {@link ColumnFamilyHandle} instance
2639 * @param key byte array of a key to search for
2640 * @param offset the offset of the "key" array to be used, must be
2641 * non-negative and no larger than "key".length
2642 * @param len the length of the "key" array to be used, must be non-negative
2643 * and no larger than "key".length
f67539c2
TL
2644 * @param valueHolder non-null to retrieve the value if it is found, or null
2645 * if the value is not needed. If non-null, upon return of the function,
2646 * the {@code value} will be set if it could be retrieved.
2647 *
2648 * @return false if the key definitely does not exist in the database,
2649 * otherwise true.
7c673cae 2650 */
f67539c2
TL
2651 public boolean keyMayExist(
2652 final ColumnFamilyHandle columnFamilyHandle,
2653 final byte[] key, int offset, int len,
2654 /* @Nullable */ final Holder<byte[]> valueHolder) {
2655 return keyMayExist(columnFamilyHandle, null, key, offset, len,
2656 valueHolder);
7c673cae
FG
2657 }
2658
2659 /**
494da23a 2660 * If the key definitely does not exist in the database, then this method
f67539c2 2661 * returns null, else it returns an instance of KeyMayExistResult
7c673cae 2662 *
f67539c2
TL
2663 * If the caller wants to obtain value when the key
2664 * is found in memory, then {@code valueHolder} must be set.
2665 *
2666 * This check is potentially lighter-weight than invoking
2667 * {@link #get(ReadOptions, byte[])}. One way to make this
2668 * lighter weight is to avoid doing any IOs.
7c673cae 2669 *
494da23a
TL
2670 * @param readOptions {@link ReadOptions} instance
2671 * @param key byte array of a key to search for
f67539c2
TL
2672 * @param valueHolder non-null to retrieve the value if it is found, or null
2673 * if the value is not needed. If non-null, upon return of the function,
2674 * the {@code value} will be set if it could be retrieved.
2675 *
2676 * @return false if the key definitely does not exist in the database,
2677 * otherwise true.
7c673cae 2678 */
f67539c2
TL
2679 public boolean keyMayExist(
2680 final ReadOptions readOptions, final byte[] key,
2681 /* @Nullable */ final Holder<byte[]> valueHolder) {
2682 return keyMayExist(readOptions, key, 0, key.length,
2683 valueHolder);
7c673cae
FG
2684 }
2685
2686 /**
494da23a 2687 * If the key definitely does not exist in the database, then this method
f67539c2
TL
2688 * returns null, else it returns an instance of KeyMayExistResult
2689 *
2690 * If the caller wants to obtain value when the key
2691 * is found in memory, then {@code valueHolder} must be set.
7c673cae 2692 *
f67539c2
TL
2693 * This check is potentially lighter-weight than invoking
2694 * {@link #get(ReadOptions, byte[], int, int)}. One way to make this
2695 * lighter weight is to avoid doing any IOs.
7c673cae 2696 *
494da23a
TL
2697 * @param readOptions {@link ReadOptions} instance
2698 * @param key byte array of a key to search for
2699 * @param offset the offset of the "key" array to be used, must be
2700 * non-negative and no larger than "key".length
2701 * @param len the length of the "key" array to be used, must be non-negative
2702 * and no larger than "key".length
f67539c2
TL
2703 * @param valueHolder non-null to retrieve the value if it is found, or null
2704 * if the value is not needed. If non-null, upon return of the function,
2705 * the {@code value} will be set if it could be retrieved.
2706 *
2707 * @return false if the key definitely does not exist in the database,
2708 * otherwise true.
7c673cae 2709 */
f67539c2
TL
2710 public boolean keyMayExist(
2711 final ReadOptions readOptions,
494da23a 2712 final byte[] key, final int offset, final int len,
f67539c2
TL
2713 /* @Nullable */ final Holder<byte[]> valueHolder) {
2714 return keyMayExist(null, readOptions,
2715 key, offset, len, valueHolder);
7c673cae
FG
2716 }
2717
494da23a
TL
2718 /**
2719 * If the key definitely does not exist in the database, then this method
f67539c2 2720 * returns null, else it returns an instance of KeyMayExistResult
11fdf7f2 2721 *
f67539c2
TL
2722 * If the caller wants to obtain value when the key
2723 * is found in memory, then {@code valueHolder} must be set.
2724 *
2725 * This check is potentially lighter-weight than invoking
2726 * {@link #get(ColumnFamilyHandle, ReadOptions, byte[])}. One way to make this
2727 * lighter weight is to avoid doing any IOs.
11fdf7f2 2728 *
494da23a 2729 * @param columnFamilyHandle {@link ColumnFamilyHandle} instance
f67539c2 2730 * @param readOptions {@link ReadOptions} instance
494da23a 2731 * @param key byte array of a key to search for
f67539c2
TL
2732 * @param valueHolder non-null to retrieve the value if it is found, or null
2733 * if the value is not needed. If non-null, upon return of the function,
2734 * the {@code value} will be set if it could be retrieved.
2735 *
2736 * @return false if the key definitely does not exist in the database,
2737 * otherwise true.
494da23a 2738 */
f67539c2
TL
2739 public boolean keyMayExist(
2740 final ColumnFamilyHandle columnFamilyHandle,
2741 final ReadOptions readOptions, final byte[] key,
2742 /* @Nullable */ final Holder<byte[]> valueHolder) {
2743 return keyMayExist(columnFamilyHandle, readOptions,
2744 key, 0, key.length, valueHolder);
494da23a
TL
2745 }
2746
2747 /**
2748 * If the key definitely does not exist in the database, then this method
f67539c2 2749 * returns null, else it returns an instance of KeyMayExistResult
11fdf7f2 2750 *
f67539c2
TL
2751 * If the caller wants to obtain value when the key
2752 * is found in memory, then {@code valueHolder} must be set.
2753 *
2754 * This check is potentially lighter-weight than invoking
2755 * {@link #get(ColumnFamilyHandle, ReadOptions, byte[], int, int)}.
2756 * One way to make this lighter weight is to avoid doing any IOs.
11fdf7f2 2757 *
494da23a 2758 * @param columnFamilyHandle {@link ColumnFamilyHandle} instance
f67539c2 2759 * @param readOptions {@link ReadOptions} instance
494da23a
TL
2760 * @param key byte array of a key to search for
2761 * @param offset the offset of the "key" array to be used, must be
2762 * non-negative and no larger than "key".length
2763 * @param len the length of the "key" array to be used, must be non-negative
2764 * and no larger than "key".length
f67539c2
TL
2765 * @param valueHolder non-null to retrieve the value if it is found, or null
2766 * if the value is not needed. If non-null, upon return of the function,
2767 * the {@code value} will be set if it could be retrieved.
2768 *
2769 * @return false if the key definitely does not exist in the database,
2770 * otherwise true.
11fdf7f2 2771 */
f67539c2
TL
2772 public boolean keyMayExist(
2773 final ColumnFamilyHandle columnFamilyHandle,
2774 final ReadOptions readOptions,
2775 final byte[] key, final int offset, final int len,
2776 /* @Nullable */ final Holder<byte[]> valueHolder) {
494da23a 2777 checkBounds(offset, len, key.length);
f67539c2
TL
2778 if (valueHolder == null) {
2779 return keyMayExist(nativeHandle_,
2780 columnFamilyHandle == null ? 0 : columnFamilyHandle.nativeHandle_,
2781 readOptions == null ? 0 : readOptions.nativeHandle_,
2782 key, offset, len);
2783 } else {
2784 final byte[][] result = keyMayExistFoundValue(
2785 nativeHandle_,
2786 columnFamilyHandle == null ? 0 : columnFamilyHandle.nativeHandle_,
2787 readOptions == null ? 0 : readOptions.nativeHandle_,
2788 key, offset, len);
2789 if (result[0][0] == 0x0) {
2790 valueHolder.setValue(null);
2791 return false;
2792 } else if (result[0][0] == 0x1) {
2793 valueHolder.setValue(null);
2794 return true;
2795 } else {
2796 valueHolder.setValue(result[1]);
2797 return true;
2798 }
2799 }
11fdf7f2
TL
2800 }
2801
7c673cae
FG
2802 /**
2803 * <p>Return a heap-allocated iterator over the contents of the
2804 * database. The result of newIterator() is initially invalid
2805 * (caller must call one of the Seek methods on the iterator
2806 * before using it).</p>
2807 *
2808 * <p>Caller should close the iterator when it is no longer needed.
2809 * The returned iterator should be closed before this db is closed.
2810 * </p>
2811 *
2812 * @return instance of iterator object.
2813 */
2814 public RocksIterator newIterator() {
2815 return new RocksIterator(this, iterator(nativeHandle_));
2816 }
2817
2818 /**
2819 * <p>Return a heap-allocated iterator over the contents of the
2820 * database. The result of newIterator() is initially invalid
2821 * (caller must call one of the Seek methods on the iterator
2822 * before using it).</p>
2823 *
2824 * <p>Caller should close the iterator when it is no longer needed.
2825 * The returned iterator should be closed before this db is closed.
2826 * </p>
2827 *
2828 * @param readOptions {@link ReadOptions} instance.
2829 * @return instance of iterator object.
2830 */
2831 public RocksIterator newIterator(final ReadOptions readOptions) {
2832 return new RocksIterator(this, iterator(nativeHandle_,
2833 readOptions.nativeHandle_));
2834 }
2835
7c673cae
FG
2836 /**
2837 * <p>Return a heap-allocated iterator over the contents of the
2838 * database. The result of newIterator() is initially invalid
2839 * (caller must call one of the Seek methods on the iterator
2840 * before using it).</p>
2841 *
2842 * <p>Caller should close the iterator when it is no longer needed.
2843 * The returned iterator should be closed before this db is closed.
2844 * </p>
2845 *
2846 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
2847 * instance
2848 * @return instance of iterator object.
2849 */
2850 public RocksIterator newIterator(
2851 final ColumnFamilyHandle columnFamilyHandle) {
2852 return new RocksIterator(this, iteratorCF(nativeHandle_,
2853 columnFamilyHandle.nativeHandle_));
2854 }
2855
2856 /**
2857 * <p>Return a heap-allocated iterator over the contents of the
2858 * database. The result of newIterator() is initially invalid
2859 * (caller must call one of the Seek methods on the iterator
2860 * before using it).</p>
2861 *
2862 * <p>Caller should close the iterator when it is no longer needed.
2863 * The returned iterator should be closed before this db is closed.
2864 * </p>
2865 *
2866 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
2867 * instance
2868 * @param readOptions {@link ReadOptions} instance.
2869 * @return instance of iterator object.
2870 */
2871 public RocksIterator newIterator(final ColumnFamilyHandle columnFamilyHandle,
2872 final ReadOptions readOptions) {
2873 return new RocksIterator(this, iteratorCF(nativeHandle_,
2874 columnFamilyHandle.nativeHandle_, readOptions.nativeHandle_));
2875 }
2876
2877 /**
2878 * Returns iterators from a consistent database state across multiple
2879 * column families. Iterators are heap allocated and need to be deleted
2880 * before the db is deleted
2881 *
2882 * @param columnFamilyHandleList {@link java.util.List} containing
2883 * {@link org.rocksdb.ColumnFamilyHandle} instances.
2884 * @return {@link java.util.List} containing {@link org.rocksdb.RocksIterator}
2885 * instances
2886 *
2887 * @throws RocksDBException thrown if error happens in underlying
2888 * native library.
2889 */
2890 public List<RocksIterator> newIterators(
2891 final List<ColumnFamilyHandle> columnFamilyHandleList)
2892 throws RocksDBException {
2893 return newIterators(columnFamilyHandleList, new ReadOptions());
2894 }
2895
2896 /**
2897 * Returns iterators from a consistent database state across multiple
2898 * column families. Iterators are heap allocated and need to be deleted
2899 * before the db is deleted
2900 *
2901 * @param columnFamilyHandleList {@link java.util.List} containing
2902 * {@link org.rocksdb.ColumnFamilyHandle} instances.
2903 * @param readOptions {@link ReadOptions} instance.
2904 * @return {@link java.util.List} containing {@link org.rocksdb.RocksIterator}
2905 * instances
2906 *
2907 * @throws RocksDBException thrown if error happens in underlying
2908 * native library.
2909 */
2910 public List<RocksIterator> newIterators(
2911 final List<ColumnFamilyHandle> columnFamilyHandleList,
2912 final ReadOptions readOptions) throws RocksDBException {
2913
2914 final long[] columnFamilyHandles = new long[columnFamilyHandleList.size()];
2915 for (int i = 0; i < columnFamilyHandleList.size(); i++) {
2916 columnFamilyHandles[i] = columnFamilyHandleList.get(i).nativeHandle_;
2917 }
2918
2919 final long[] iteratorRefs = iterators(nativeHandle_, columnFamilyHandles,
2920 readOptions.nativeHandle_);
2921
2922 final List<RocksIterator> iterators = new ArrayList<>(
2923 columnFamilyHandleList.size());
2924 for (int i=0; i<columnFamilyHandleList.size(); i++){
2925 iterators.add(new RocksIterator(this, iteratorRefs[i]));
2926 }
2927 return iterators;
2928 }
2929
494da23a 2930
7c673cae 2931 /**
494da23a
TL
2932 * <p>Return a handle to the current DB state. Iterators created with
2933 * this handle will all observe a stable snapshot of the current DB
2934 * state. The caller must call ReleaseSnapshot(result) when the
2935 * snapshot is no longer needed.</p>
7c673cae 2936 *
494da23a
TL
2937 * <p>nullptr will be returned if the DB fails to take a snapshot or does
2938 * not support snapshot.</p>
2939 *
2940 * @return Snapshot {@link Snapshot} instance
7c673cae 2941 */
494da23a
TL
2942 public Snapshot getSnapshot() {
2943 long snapshotHandle = getSnapshot(nativeHandle_);
2944 if (snapshotHandle != 0) {
2945 return new Snapshot(snapshotHandle);
2946 }
2947 return null;
7c673cae
FG
2948 }
2949
2950 /**
494da23a 2951 * Release a previously acquired snapshot.
7c673cae 2952 *
494da23a
TL
2953 * The caller must not use "snapshot" after this call.
2954 *
2955 * @param snapshot {@link Snapshot} instance
2956 */
2957 public void releaseSnapshot(final Snapshot snapshot) {
2958 if (snapshot != null) {
2959 releaseSnapshot(nativeHandle_, snapshot.nativeHandle_);
2960 }
2961 }
2962
2963 /**
2964 * DB implements can export properties about their state
2965 * via this method on a per column family level.
2966 *
2967 * <p>If {@code property} is a valid property understood by this DB
2968 * implementation, fills {@code value} with its current value and
2969 * returns true. Otherwise returns false.</p>
2970 *
2971 * <p>Valid property names include:
2972 * <ul>
2973 * <li>"rocksdb.num-files-at-level&lt;N&gt;" - return the number of files at
2974 * level &lt;N&gt;, where &lt;N&gt; is an ASCII representation of a level
2975 * number (e.g. "0").</li>
2976 * <li>"rocksdb.stats" - returns a multi-line string that describes statistics
2977 * about the internal operation of the DB.</li>
2978 * <li>"rocksdb.sstables" - returns a multi-line string that describes all
2979 * of the sstables that make up the db contents.</li>
2980 * </ul>
2981 *
2982 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
2983 * instance, or null for the default column family.
2984 * @param property to be fetched. See above for examples
2985 * @return property value
7c673cae
FG
2986 *
2987 * @throws RocksDBException thrown if error happens in underlying
2988 * native library.
2989 */
494da23a
TL
2990 public String getProperty(
2991 /* @Nullable */ final ColumnFamilyHandle columnFamilyHandle,
2992 final String property) throws RocksDBException {
2993 return getProperty(nativeHandle_,
2994 columnFamilyHandle == null ? 0 : columnFamilyHandle.nativeHandle_,
2995 property, property.length());
7c673cae
FG
2996 }
2997
2998 /**
494da23a
TL
2999 * DB implementations can export properties about their state
3000 * via this method. If "property" is a valid property understood by this
3001 * DB implementation, fills "*value" with its current value and returns
3002 * true. Otherwise returns false.
7c673cae 3003 *
494da23a
TL
3004 * <p>Valid property names include:
3005 * <ul>
3006 * <li>"rocksdb.num-files-at-level&lt;N&gt;" - return the number of files at
3007 * level &lt;N&gt;, where &lt;N&gt; is an ASCII representation of a level
3008 * number (e.g. "0").</li>
3009 * <li>"rocksdb.stats" - returns a multi-line string that describes statistics
3010 * about the internal operation of the DB.</li>
3011 * <li>"rocksdb.sstables" - returns a multi-line string that describes all
3012 * of the sstables that make up the db contents.</li>
3013 *</ul>
3014 *
3015 * @param property to be fetched. See above for examples
3016 * @return property value
7c673cae
FG
3017 *
3018 * @throws RocksDBException thrown if error happens in underlying
3019 * native library.
3020 */
494da23a
TL
3021 public String getProperty(final String property) throws RocksDBException {
3022 return getProperty(null, property);
7c673cae
FG
3023 }
3024
494da23a 3025
7c673cae 3026 /**
494da23a 3027 * Gets a property map.
7c673cae 3028 *
494da23a 3029 * @param property to be fetched.
7c673cae 3030 *
494da23a
TL
3031 * @return the property map
3032 *
3033 * @throws RocksDBException if an error happens in the underlying native code.
7c673cae 3034 */
494da23a
TL
3035 public Map<String, String> getMapProperty(final String property)
3036 throws RocksDBException {
3037 return getMapProperty(null, property);
3038 }
3039
3040 /**
3041 * Gets a property map.
3042 *
3043 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
3044 * instance, or null for the default column family.
3045 * @param property to be fetched.
3046 *
3047 * @return the property map
3048 *
3049 * @throws RocksDBException if an error happens in the underlying native code.
3050 */
3051 public Map<String, String> getMapProperty(
3052 /* @Nullable */ final ColumnFamilyHandle columnFamilyHandle,
3053 final String property) throws RocksDBException {
3054 return getMapProperty(nativeHandle_,
3055 columnFamilyHandle == null ? 0 : columnFamilyHandle.nativeHandle_,
3056 property, property.length());
3057 }
3058
3059 /**
3060 * <p> Similar to GetProperty(), but only works for a subset of properties
3061 * whose return value is a numerical value. Return the value as long.</p>
3062 *
3063 * <p><strong>Note</strong>: As the returned property is of type
3064 * {@code uint64_t} on C++ side the returning value can be negative
3065 * because Java supports in Java 7 only signed long values.</p>
3066 *
3067 * <p><strong>Java 7</strong>: To mitigate the problem of the non
3068 * existent unsigned long tpye, values should be encapsulated using
3069 * {@link java.math.BigInteger} to reflect the correct value. The correct
3070 * behavior is guaranteed if {@code 2^64} is added to negative values.</p>
3071 *
3072 * <p><strong>Java 8</strong>: In Java 8 the value should be treated as
3073 * unsigned long using provided methods of type {@link Long}.</p>
3074 *
3075 * @param property to be fetched.
3076 *
3077 * @return numerical property value.
3078 *
3079 * @throws RocksDBException if an error happens in the underlying native code.
3080 */
3081 public long getLongProperty(final String property) throws RocksDBException {
3082 return getLongProperty(null, property);
3083 }
3084
3085 /**
3086 * <p> Similar to GetProperty(), but only works for a subset of properties
3087 * whose return value is a numerical value. Return the value as long.</p>
3088 *
3089 * <p><strong>Note</strong>: As the returned property is of type
3090 * {@code uint64_t} on C++ side the returning value can be negative
3091 * because Java supports in Java 7 only signed long values.</p>
3092 *
3093 * <p><strong>Java 7</strong>: To mitigate the problem of the non
3094 * existent unsigned long tpye, values should be encapsulated using
3095 * {@link java.math.BigInteger} to reflect the correct value. The correct
3096 * behavior is guaranteed if {@code 2^64} is added to negative values.</p>
3097 *
3098 * <p><strong>Java 8</strong>: In Java 8 the value should be treated as
3099 * unsigned long using provided methods of type {@link Long}.</p>
3100 *
3101 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
3102 * instance, or null for the default column family
3103 * @param property to be fetched.
3104 *
3105 * @return numerical property value
3106 *
3107 * @throws RocksDBException if an error happens in the underlying native code.
3108 */
3109 public long getLongProperty(
3110 /* @Nullable */ final ColumnFamilyHandle columnFamilyHandle,
3111 final String property) throws RocksDBException {
3112 return getLongProperty(nativeHandle_,
3113 columnFamilyHandle == null ? 0 : columnFamilyHandle.nativeHandle_,
3114 property, property.length());
3115 }
3116
3117 /**
3118 * Reset internal stats for DB and all column families.
3119 *
3120 * Note this doesn't reset {@link Options#statistics()} as it is not
3121 * owned by DB.
f67539c2
TL
3122 *
3123 * @throws RocksDBException if an error occurs whilst reseting the stats
494da23a
TL
3124 */
3125 public void resetStats() throws RocksDBException {
3126 resetStats(nativeHandle_);
3127 }
3128
3129 /**
3130 * <p> Return sum of the getLongProperty of all the column families</p>
3131 *
3132 * <p><strong>Note</strong>: As the returned property is of type
3133 * {@code uint64_t} on C++ side the returning value can be negative
3134 * because Java supports in Java 7 only signed long values.</p>
3135 *
3136 * <p><strong>Java 7</strong>: To mitigate the problem of the non
3137 * existent unsigned long tpye, values should be encapsulated using
3138 * {@link java.math.BigInteger} to reflect the correct value. The correct
3139 * behavior is guaranteed if {@code 2^64} is added to negative values.</p>
3140 *
3141 * <p><strong>Java 8</strong>: In Java 8 the value should be treated as
3142 * unsigned long using provided methods of type {@link Long}.</p>
3143 *
3144 * @param property to be fetched.
3145 *
3146 * @return numerical property value
3147 *
3148 * @throws RocksDBException if an error happens in the underlying native code.
3149 */
3150 public long getAggregatedLongProperty(final String property)
7c673cae 3151 throws RocksDBException {
494da23a
TL
3152 return getAggregatedLongProperty(nativeHandle_, property,
3153 property.length());
3154 }
3155
3156 /**
3157 * Get the approximate file system space used by keys in each range.
3158 *
3159 * Note that the returned sizes measure file system space usage, so
3160 * if the user data compresses by a factor of ten, the returned
3161 * sizes will be one-tenth the size of the corresponding user data size.
3162 *
3163 * If {@code sizeApproximationFlags} defines whether the returned size
3164 * should include the recently written data in the mem-tables (if
3165 * the mem-table type supports it), data serialized to disk, or both.
3166 *
3167 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
3168 * instance, or null for the default column family
3169 * @param ranges the ranges over which to approximate sizes
3170 * @param sizeApproximationFlags flags to determine what to include in the
3171 * approximation.
3172 *
3173 * @return the sizes
3174 */
3175 public long[] getApproximateSizes(
3176 /*@Nullable*/ final ColumnFamilyHandle columnFamilyHandle,
3177 final List<Range> ranges,
3178 final SizeApproximationFlag... sizeApproximationFlags) {
3179
3180 byte flags = 0x0;
3181 for (final SizeApproximationFlag sizeApproximationFlag
3182 : sizeApproximationFlags) {
3183 flags |= sizeApproximationFlag.getValue();
3184 }
3185
3186 return getApproximateSizes(nativeHandle_,
3187 columnFamilyHandle == null ? 0 : columnFamilyHandle.nativeHandle_,
3188 toRangeSliceHandles(ranges), flags);
3189 }
3190
3191 /**
3192 * Get the approximate file system space used by keys in each range for
3193 * the default column family.
3194 *
3195 * Note that the returned sizes measure file system space usage, so
3196 * if the user data compresses by a factor of ten, the returned
3197 * sizes will be one-tenth the size of the corresponding user data size.
3198 *
3199 * If {@code sizeApproximationFlags} defines whether the returned size
3200 * should include the recently written data in the mem-tables (if
3201 * the mem-table type supports it), data serialized to disk, or both.
3202 *
3203 * @param ranges the ranges over which to approximate sizes
3204 * @param sizeApproximationFlags flags to determine what to include in the
3205 * approximation.
3206 *
3207 * @return the sizes.
3208 */
3209 public long[] getApproximateSizes(final List<Range> ranges,
3210 final SizeApproximationFlag... sizeApproximationFlags) {
3211 return getApproximateSizes(null, ranges, sizeApproximationFlags);
3212 }
3213
3214 public static class CountAndSize {
3215 public final long count;
3216 public final long size;
3217
3218 public CountAndSize(final long count, final long size) {
3219 this.count = count;
3220 this.size = size;
3221 }
3222 }
3223
3224 /**
3225 * This method is similar to
3226 * {@link #getApproximateSizes(ColumnFamilyHandle, List, SizeApproximationFlag...)},
3227 * except that it returns approximate number of records and size in memtables.
3228 *
3229 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
3230 * instance, or null for the default column family
3231 * @param range the ranges over which to get the memtable stats
3232 *
3233 * @return the count and size for the range
3234 */
3235 public CountAndSize getApproximateMemTableStats(
3236 /*@Nullable*/ final ColumnFamilyHandle columnFamilyHandle,
3237 final Range range) {
3238 final long[] result = getApproximateMemTableStats(nativeHandle_,
3239 columnFamilyHandle == null ? 0 : columnFamilyHandle.nativeHandle_,
3240 range.start.getNativeHandle(),
3241 range.limit.getNativeHandle());
3242 return new CountAndSize(result[0], result[1]);
7c673cae
FG
3243 }
3244
3245 /**
494da23a
TL
3246 * This method is similar to
3247 * {@link #getApproximateSizes(ColumnFamilyHandle, List, SizeApproximationFlag...)},
3248 * except that it returns approximate number of records and size in memtables.
7c673cae 3249 *
494da23a 3250 * @param range the ranges over which to get the memtable stats
7c673cae 3251 *
494da23a 3252 * @return the count and size for the range
7c673cae 3253 */
494da23a
TL
3254 public CountAndSize getApproximateMemTableStats(
3255 final Range range) {
3256 return getApproximateMemTableStats(null, range);
7c673cae
FG
3257 }
3258
3259 /**
3260 * <p>Range compaction of database.</p>
3261 * <p><strong>Note</strong>: After the database has been compacted,
3262 * all data will have been pushed down to the last level containing
3263 * any data.</p>
3264 *
3265 * <p><strong>See also</strong></p>
3266 * <ul>
3267 * <li>{@link #compactRange(boolean, int, int)}</li>
3268 * <li>{@link #compactRange(byte[], byte[])}</li>
3269 * <li>{@link #compactRange(byte[], byte[], boolean, int, int)}</li>
3270 * </ul>
3271 *
3272 * @throws RocksDBException thrown if an error occurs within the native
3273 * part of the library.
3274 */
3275 public void compactRange() throws RocksDBException {
494da23a
TL
3276 compactRange(null);
3277 }
3278
3279 /**
3280 * <p>Range compaction of column family.</p>
3281 * <p><strong>Note</strong>: After the database has been compacted,
3282 * all data will have been pushed down to the last level containing
3283 * any data.</p>
3284 *
3285 * <p><strong>See also</strong></p>
3286 * <ul>
3287 * <li>
3288 * {@link #compactRange(ColumnFamilyHandle, boolean, int, int)}
3289 * </li>
3290 * <li>
3291 * {@link #compactRange(ColumnFamilyHandle, byte[], byte[])}
3292 * </li>
3293 * <li>
3294 * {@link #compactRange(ColumnFamilyHandle, byte[], byte[],
3295 * boolean, int, int)}
3296 * </li>
3297 * </ul>
3298 *
3299 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
3300 * instance, or null for the default column family.
3301 *
3302 * @throws RocksDBException thrown if an error occurs within the native
3303 * part of the library.
3304 */
3305 public void compactRange(
3306 /* @Nullable */ final ColumnFamilyHandle columnFamilyHandle)
3307 throws RocksDBException {
3308 compactRange(nativeHandle_, null, -1, null, -1, 0,
3309 columnFamilyHandle == null ? 0 : columnFamilyHandle.nativeHandle_);
7c673cae
FG
3310 }
3311
3312 /**
3313 * <p>Range compaction of database.</p>
3314 * <p><strong>Note</strong>: After the database has been compacted,
3315 * all data will have been pushed down to the last level containing
3316 * any data.</p>
3317 *
3318 * <p><strong>See also</strong></p>
3319 * <ul>
3320 * <li>{@link #compactRange()}</li>
3321 * <li>{@link #compactRange(boolean, int, int)}</li>
3322 * <li>{@link #compactRange(byte[], byte[], boolean, int, int)}</li>
3323 * </ul>
3324 *
3325 * @param begin start of key range (included in range)
3326 * @param end end of key range (excluded from range)
3327 *
3328 * @throws RocksDBException thrown if an error occurs within the native
3329 * part of the library.
3330 */
3331 public void compactRange(final byte[] begin, final byte[] end)
3332 throws RocksDBException {
494da23a 3333 compactRange(null, begin, end);
7c673cae
FG
3334 }
3335
3336 /**
494da23a 3337 * <p>Range compaction of column family.</p>
7c673cae
FG
3338 * <p><strong>Note</strong>: After the database has been compacted,
3339 * all data will have been pushed down to the last level containing
3340 * any data.</p>
3341 *
7c673cae
FG
3342 * <p><strong>See also</strong></p>
3343 * <ul>
494da23a
TL
3344 * <li>{@link #compactRange(ColumnFamilyHandle)}</li>
3345 * <li>
3346 * {@link #compactRange(ColumnFamilyHandle, boolean, int, int)}
3347 * </li>
3348 * <li>
3349 * {@link #compactRange(ColumnFamilyHandle, byte[], byte[],
3350 * boolean, int, int)}
3351 * </li>
7c673cae
FG
3352 * </ul>
3353 *
494da23a
TL
3354 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
3355 * instance, or null for the default column family.
3356 * @param begin start of key range (included in range)
3357 * @param end end of key range (excluded from range)
7c673cae
FG
3358 *
3359 * @throws RocksDBException thrown if an error occurs within the native
3360 * part of the library.
3361 */
494da23a
TL
3362 public void compactRange(
3363 /* @Nullable */ final ColumnFamilyHandle columnFamilyHandle,
3364 final byte[] begin, final byte[] end) throws RocksDBException {
3365 compactRange(nativeHandle_,
3366 begin, begin == null ? -1 : begin.length,
3367 end, end == null ? -1 : end.length,
3368 0, columnFamilyHandle == null ? 0: columnFamilyHandle.nativeHandle_);
7c673cae
FG
3369 }
3370
7c673cae
FG
3371 /**
3372 * <p>Range compaction of database.</p>
3373 * <p><strong>Note</strong>: After the database has been compacted,
3374 * all data will have been pushed down to the last level containing
3375 * any data.</p>
3376 *
3377 * <p>Compaction outputs should be placed in options.db_paths
3378 * [target_path_id]. Behavior is undefined if target_path_id is
3379 * out of range.</p>
3380 *
3381 * <p><strong>See also</strong></p>
3382 * <ul>
3383 * <li>{@link #compactRange()}</li>
7c673cae 3384 * <li>{@link #compactRange(byte[], byte[])}</li>
494da23a 3385 * <li>{@link #compactRange(byte[], byte[], boolean, int, int)}</li>
7c673cae
FG
3386 * </ul>
3387 *
11fdf7f2
TL
3388 * @deprecated Use {@link #compactRange(ColumnFamilyHandle, byte[], byte[], CompactRangeOptions)} instead
3389 *
494da23a
TL
3390 * @param changeLevel reduce level after compaction
3391 * @param targetLevel target level to compact to
3392 * @param targetPathId the target path id of output path
7c673cae
FG
3393 *
3394 * @throws RocksDBException thrown if an error occurs within the native
3395 * part of the library.
3396 */
11fdf7f2 3397 @Deprecated
494da23a
TL
3398 public void compactRange(final boolean changeLevel, final int targetLevel,
3399 final int targetPathId) throws RocksDBException {
3400 compactRange(null, changeLevel, targetLevel, targetPathId);
7c673cae
FG
3401 }
3402
3403 /**
3404 * <p>Range compaction of column family.</p>
3405 * <p><strong>Note</strong>: After the database has been compacted,
3406 * all data will have been pushed down to the last level containing
3407 * any data.</p>
3408 *
494da23a
TL
3409 * <p>Compaction outputs should be placed in options.db_paths
3410 * [target_path_id]. Behavior is undefined if target_path_id is
3411 * out of range.</p>
3412 *
7c673cae
FG
3413 * <p><strong>See also</strong></p>
3414 * <ul>
494da23a 3415 * <li>{@link #compactRange(ColumnFamilyHandle)}</li>
7c673cae
FG
3416 * <li>
3417 * {@link #compactRange(ColumnFamilyHandle, byte[], byte[])}
3418 * </li>
3419 * <li>
3420 * {@link #compactRange(ColumnFamilyHandle, byte[], byte[],
3421 * boolean, int, int)}
3422 * </li>
3423 * </ul>
3424 *
494da23a
TL
3425 * @deprecated Use {@link #compactRange(ColumnFamilyHandle, byte[], byte[], CompactRangeOptions)} instead
3426 *
7c673cae 3427 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
494da23a
TL
3428 * instance, or null for the default column family.
3429 * @param changeLevel reduce level after compaction
3430 * @param targetLevel target level to compact to
3431 * @param targetPathId the target path id of output path
7c673cae
FG
3432 *
3433 * @throws RocksDBException thrown if an error occurs within the native
3434 * part of the library.
3435 */
494da23a
TL
3436 @Deprecated
3437 public void compactRange(
3438 /* @Nullable */ final ColumnFamilyHandle columnFamilyHandle,
3439 final boolean changeLevel, final int targetLevel, final int targetPathId)
7c673cae 3440 throws RocksDBException {
494da23a
TL
3441 final CompactRangeOptions options = new CompactRangeOptions();
3442 options.setChangeLevel(changeLevel);
3443 options.setTargetLevel(targetLevel);
3444 options.setTargetPathId(targetPathId);
3445 compactRange(nativeHandle_,
3446 null, -1,
3447 null, -1,
3448 options.nativeHandle_,
3449 columnFamilyHandle == null ? 0 : columnFamilyHandle.nativeHandle_);
3450 }
3451
3452 /**
3453 * <p>Range compaction of database.</p>
3454 * <p><strong>Note</strong>: After the database has been compacted,
3455 * all data will have been pushed down to the last level containing
3456 * any data.</p>
3457 *
3458 * <p>Compaction outputs should be placed in options.db_paths
3459 * [target_path_id]. Behavior is undefined if target_path_id is
3460 * out of range.</p>
3461 *
3462 * <p><strong>See also</strong></p>
3463 * <ul>
3464 * <li>{@link #compactRange()}</li>
3465 * <li>{@link #compactRange(boolean, int, int)}</li>
3466 * <li>{@link #compactRange(byte[], byte[])}</li>
3467 * </ul>
3468 *
3469 * @deprecated Use {@link #compactRange(ColumnFamilyHandle, byte[], byte[], CompactRangeOptions)}
3470 * instead
3471 *
3472 * @param begin start of key range (included in range)
3473 * @param end end of key range (excluded from range)
3474 * @param changeLevel reduce level after compaction
3475 * @param targetLevel target level to compact to
3476 * @param targetPathId the target path id of output path
3477 *
3478 * @throws RocksDBException thrown if an error occurs within the native
3479 * part of the library.
3480 */
3481 @Deprecated
3482 public void compactRange(final byte[] begin, final byte[] end,
3483 final boolean changeLevel, final int targetLevel,
3484 final int targetPathId) throws RocksDBException {
3485 compactRange(null, begin, end, changeLevel, targetLevel, targetPathId);
7c673cae
FG
3486 }
3487
3488 /**
3489 * <p>Range compaction of column family.</p>
3490 * <p><strong>Note</strong>: After the database has been compacted,
3491 * all data will have been pushed down to the last level containing
3492 * any data.</p>
3493 *
494da23a
TL
3494 * <p>Compaction outputs should be placed in options.db_paths
3495 * [target_path_id]. Behavior is undefined if target_path_id is
3496 * out of range.</p>
3497 *
7c673cae
FG
3498 * <p><strong>See also</strong></p>
3499 * <ul>
3500 * <li>{@link #compactRange(ColumnFamilyHandle)}</li>
3501 * <li>
3502 * {@link #compactRange(ColumnFamilyHandle, boolean, int, int)}
3503 * </li>
3504 * <li>
494da23a 3505 * {@link #compactRange(ColumnFamilyHandle, byte[], byte[])}
7c673cae
FG
3506 * </li>
3507 * </ul>
3508 *
494da23a
TL
3509 * @deprecated Use {@link #compactRange(ColumnFamilyHandle, byte[], byte[], CompactRangeOptions)} instead
3510 *
7c673cae
FG
3511 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
3512 * instance.
3513 * @param begin start of key range (included in range)
3514 * @param end end of key range (excluded from range)
494da23a
TL
3515 * @param changeLevel reduce level after compaction
3516 * @param targetLevel target level to compact to
3517 * @param targetPathId the target path id of output path
7c673cae
FG
3518 *
3519 * @throws RocksDBException thrown if an error occurs within the native
3520 * part of the library.
3521 */
494da23a
TL
3522 @Deprecated
3523 public void compactRange(
3524 /* @Nullable */ final ColumnFamilyHandle columnFamilyHandle,
3525 final byte[] begin, final byte[] end, final boolean changeLevel,
3526 final int targetLevel, final int targetPathId)
3527 throws RocksDBException {
3528 final CompactRangeOptions options = new CompactRangeOptions();
3529 options.setChangeLevel(changeLevel);
3530 options.setTargetLevel(targetLevel);
3531 options.setTargetPathId(targetPathId);
3532 compactRange(nativeHandle_,
3533 begin, begin == null ? -1 : begin.length,
3534 end, end == null ? -1 : end.length,
3535 options.nativeHandle_,
3536 columnFamilyHandle == null ? 0 : columnFamilyHandle.nativeHandle_);
7c673cae
FG
3537 }
3538
11fdf7f2
TL
3539 /**
3540 * <p>Range compaction of column family.</p>
3541 * <p><strong>Note</strong>: After the database has been compacted,
3542 * all data will have been pushed down to the last level containing
3543 * any data.</p>
3544 *
3545 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} instance.
3546 * @param begin start of key range (included in range)
3547 * @param end end of key range (excluded from range)
3548 * @param compactRangeOptions options for the compaction
3549 *
3550 * @throws RocksDBException thrown if an error occurs within the native
3551 * part of the library.
3552 */
f67539c2
TL
3553 public void compactRange(
3554 /* @Nullable */ final ColumnFamilyHandle columnFamilyHandle,
494da23a
TL
3555 final byte[] begin, final byte[] end,
3556 final CompactRangeOptions compactRangeOptions) throws RocksDBException {
3557 compactRange(nativeHandle_,
3558 begin, begin == null ? -1 : begin.length,
3559 end, end == null ? -1 : end.length,
f67539c2
TL
3560 compactRangeOptions.nativeHandle_,
3561 columnFamilyHandle == null ? 0 : columnFamilyHandle.nativeHandle_);
11fdf7f2
TL
3562 }
3563
7c673cae 3564 /**
494da23a 3565 * Change the options for the column family handle.
7c673cae 3566 *
494da23a
TL
3567 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
3568 * instance, or null for the default column family.
3569 * @param mutableColumnFamilyOptions the options.
f67539c2
TL
3570 *
3571 * @throws RocksDBException if an error occurs whilst setting the options
494da23a
TL
3572 */
3573 public void setOptions(
3574 /* @Nullable */final ColumnFamilyHandle columnFamilyHandle,
3575 final MutableColumnFamilyOptions mutableColumnFamilyOptions)
3576 throws RocksDBException {
3577 setOptions(nativeHandle_, columnFamilyHandle.nativeHandle_,
3578 mutableColumnFamilyOptions.getKeys(),
3579 mutableColumnFamilyOptions.getValues());
3580 }
3581
3582 /**
3583 * Change the options for the default column family handle.
7c673cae 3584 *
494da23a 3585 * @param mutableColumnFamilyOptions the options.
f67539c2
TL
3586 *
3587 * @throws RocksDBException if an error occurs whilst setting the options
494da23a
TL
3588 */
3589 public void setOptions(
3590 final MutableColumnFamilyOptions mutableColumnFamilyOptions)
3591 throws RocksDBException {
3592 setOptions(null, mutableColumnFamilyOptions);
3593 }
3594
3595 /**
3596 * Set the options for the column family handle.
7c673cae 3597 *
494da23a 3598 * @param mutableDBoptions the options.
f67539c2
TL
3599 *
3600 * @throws RocksDBException if an error occurs whilst setting the options
494da23a
TL
3601 */
3602 public void setDBOptions(final MutableDBOptions mutableDBoptions)
3603 throws RocksDBException {
3604 setDBOptions(nativeHandle_,
3605 mutableDBoptions.getKeys(),
3606 mutableDBoptions.getValues());
3607 }
3608
3609 /**
f67539c2 3610 * Takes a list of files specified by file names and
494da23a
TL
3611 * compacts them to the specified level.
3612 *
3613 * Note that the behavior is different from
3614 * {@link #compactRange(ColumnFamilyHandle, byte[], byte[])}
3615 * in that CompactFiles() performs the compaction job using the CURRENT
3616 * thread.
3617 *
3618 * @param compactionOptions compaction options
3619 * @param inputFileNames the name of the files to compact
3620 * @param outputLevel the level to which they should be compacted
3621 * @param outputPathId the id of the output path, or -1
3622 * @param compactionJobInfo the compaction job info, this parameter
3623 * will be updated with the info from compacting the files,
3624 * can just be null if you don't need it.
f67539c2
TL
3625 *
3626 * @return the list of compacted files
3627 *
3628 * @throws RocksDBException if an error occurs during compaction
494da23a
TL
3629 */
3630 public List<String> compactFiles(
3631 final CompactionOptions compactionOptions,
3632 final List<String> inputFileNames,
3633 final int outputLevel,
3634 final int outputPathId,
3635 /* @Nullable */ final CompactionJobInfo compactionJobInfo)
3636 throws RocksDBException {
3637 return compactFiles(compactionOptions, null, inputFileNames, outputLevel,
3638 outputPathId, compactionJobInfo);
3639 }
3640
3641 /**
3642 * Takes a list of files specified by file names and
3643 * compacts them to the specified level.
3644 *
3645 * Note that the behavior is different from
3646 * {@link #compactRange(ColumnFamilyHandle, byte[], byte[])}
3647 * in that CompactFiles() performs the compaction job using the CURRENT
3648 * thread.
3649 *
3650 * @param compactionOptions compaction options
3651 * @param columnFamilyHandle columnFamilyHandle, or null for the
3652 * default column family
3653 * @param inputFileNames the name of the files to compact
3654 * @param outputLevel the level to which they should be compacted
3655 * @param outputPathId the id of the output path, or -1
3656 * @param compactionJobInfo the compaction job info, this parameter
3657 * will be updated with the info from compacting the files,
3658 * can just be null if you don't need it.
f67539c2
TL
3659 *
3660 * @return the list of compacted files
3661 *
3662 * @throws RocksDBException if an error occurs during compaction
494da23a
TL
3663 */
3664 public List<String> compactFiles(
3665 final CompactionOptions compactionOptions,
3666 /* @Nullable */ final ColumnFamilyHandle columnFamilyHandle,
3667 final List<String> inputFileNames,
3668 final int outputLevel,
3669 final int outputPathId,
3670 /* @Nullable */ final CompactionJobInfo compactionJobInfo)
3671 throws RocksDBException {
3672 return Arrays.asList(compactFiles(nativeHandle_, compactionOptions.nativeHandle_,
3673 columnFamilyHandle == null ? 0 : columnFamilyHandle.nativeHandle_,
3674 inputFileNames.toArray(new String[0]),
3675 outputLevel,
3676 outputPathId,
3677 compactionJobInfo == null ? 0 : compactionJobInfo.nativeHandle_));
3678 }
3679
20effc67
TL
3680 /**
3681 * This function will cancel all currently running background processes.
3682 *
3683 * @param wait if true, wait for all background work to be cancelled before
3684 * returning.
3685 *
3686 */
3687 public void cancelAllBackgroundWork(boolean wait) {
3688 cancelAllBackgroundWork(nativeHandle_, wait);
3689 }
3690
494da23a
TL
3691 /**
3692 * This function will wait until all currently running background processes
3693 * finish. After it returns, no background process will be run until
3694 * {@link #continueBackgroundWork()} is called
3695 *
f67539c2 3696 * @throws RocksDBException if an error occurs when pausing background work
494da23a
TL
3697 */
3698 public void pauseBackgroundWork() throws RocksDBException {
3699 pauseBackgroundWork(nativeHandle_);
3700 }
3701
3702 /**
3703 * Resumes background work which was suspended by
3704 * previously calling {@link #pauseBackgroundWork()}
3705 *
f67539c2 3706 * @throws RocksDBException if an error occurs when resuming background work
494da23a
TL
3707 */
3708 public void continueBackgroundWork() throws RocksDBException {
3709 continueBackgroundWork(nativeHandle_);
3710 }
3711
3712 /**
3713 * Enable automatic compactions for the given column
3714 * families if they were previously disabled.
3715 *
3716 * The function will first set the
3717 * {@link ColumnFamilyOptions#disableAutoCompactions()} option for each
3718 * column family to false, after which it will schedule a flush/compaction.
3719 *
3720 * NOTE: Setting disableAutoCompactions to 'false' through
3721 * {@link #setOptions(ColumnFamilyHandle, MutableColumnFamilyOptions)}
3722 * does NOT schedule a flush/compaction afterwards, and only changes the
3723 * parameter itself within the column family option.
3724 *
3725 * @param columnFamilyHandles the column family handles
f67539c2
TL
3726 *
3727 * @throws RocksDBException if an error occurs whilst enabling auto-compaction
494da23a
TL
3728 */
3729 public void enableAutoCompaction(
3730 final List<ColumnFamilyHandle> columnFamilyHandles)
3731 throws RocksDBException {
3732 enableAutoCompaction(nativeHandle_,
3733 toNativeHandleList(columnFamilyHandles));
3734 }
3735
3736 /**
3737 * Number of levels used for this DB.
3738 *
3739 * @return the number of levels
3740 */
3741 public int numberLevels() {
3742 return numberLevels(null);
3743 }
3744
3745 /**
3746 * Number of levels used for a column family in this DB.
3747 *
3748 * @param columnFamilyHandle the column family handle, or null
3749 * for the default column family
3750 *
3751 * @return the number of levels
3752 */
3753 public int numberLevels(/* @Nullable */final ColumnFamilyHandle columnFamilyHandle) {
3754 return numberLevels(nativeHandle_,
3755 columnFamilyHandle == null ? 0 : columnFamilyHandle.nativeHandle_);
3756 }
3757
3758 /**
3759 * Maximum level to which a new compacted memtable is pushed if it
3760 * does not create overlap.
f67539c2
TL
3761 *
3762 * @return the maximum level
494da23a
TL
3763 */
3764 public int maxMemCompactionLevel() {
3765 return maxMemCompactionLevel(null);
3766 }
3767
3768 /**
3769 * Maximum level to which a new compacted memtable is pushed if it
3770 * does not create overlap.
3771 *
3772 * @param columnFamilyHandle the column family handle
f67539c2
TL
3773 *
3774 * @return the maximum level
494da23a
TL
3775 */
3776 public int maxMemCompactionLevel(
f67539c2 3777 /* @Nullable */ final ColumnFamilyHandle columnFamilyHandle) {
494da23a
TL
3778 return maxMemCompactionLevel(nativeHandle_,
3779 columnFamilyHandle == null ? 0 : columnFamilyHandle.nativeHandle_);
3780 }
3781
3782 /**
3783 * Number of files in level-0 that would stop writes.
f67539c2
TL
3784 *
3785 * @return the number of files
494da23a
TL
3786 */
3787 public int level0StopWriteTrigger() {
3788 return level0StopWriteTrigger(null);
3789 }
3790
3791 /**
3792 * Number of files in level-0 that would stop writes.
3793 *
3794 * @param columnFamilyHandle the column family handle
f67539c2
TL
3795 *
3796 * @return the number of files
494da23a
TL
3797 */
3798 public int level0StopWriteTrigger(
3799 /* @Nullable */final ColumnFamilyHandle columnFamilyHandle) {
3800 return level0StopWriteTrigger(nativeHandle_,
3801 columnFamilyHandle == null ? 0 : columnFamilyHandle.nativeHandle_);
3802 }
3803
3804 /**
3805 * Get DB name -- the exact same name that was provided as an argument to
3806 * as path to {@link #open(Options, String)}.
3807 *
3808 * @return the DB name
3809 */
3810 public String getName() {
3811 return getName(nativeHandle_);
3812 }
3813
3814 /**
3815 * Get the Env object from the DB
3816 *
3817 * @return the env
3818 */
3819 public Env getEnv() {
3820 final long envHandle = getEnv(nativeHandle_);
3821 if (envHandle == Env.getDefault().nativeHandle_) {
3822 return Env.getDefault();
3823 } else {
3824 final Env env = new RocksEnv(envHandle);
3825 env.disOwnNativeHandle(); // we do not own the Env!
3826 return env;
3827 }
3828 }
3829
3830 /**
3831 * <p>Flush all memory table data.</p>
3832 *
3833 * <p>Note: it must be ensured that the FlushOptions instance
3834 * is not GC'ed before this method finishes. If the wait parameter is
3835 * set to false, flush processing is asynchronous.</p>
3836 *
3837 * @param flushOptions {@link org.rocksdb.FlushOptions} instance.
3838 * @throws RocksDBException thrown if an error occurs within the native
3839 * part of the library.
3840 */
3841 public void flush(final FlushOptions flushOptions)
3842 throws RocksDBException {
3843 flush(flushOptions, (List<ColumnFamilyHandle>) null);
3844 }
3845
3846 /**
3847 * <p>Flush all memory table data.</p>
11fdf7f2 3848 *
494da23a
TL
3849 * <p>Note: it must be ensured that the FlushOptions instance
3850 * is not GC'ed before this method finishes. If the wait parameter is
3851 * set to false, flush processing is asynchronous.</p>
7c673cae 3852 *
494da23a
TL
3853 * @param flushOptions {@link org.rocksdb.FlushOptions} instance.
3854 * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} instance.
7c673cae
FG
3855 * @throws RocksDBException thrown if an error occurs within the native
3856 * part of the library.
3857 */
494da23a
TL
3858 public void flush(final FlushOptions flushOptions,
3859 /* @Nullable */ final ColumnFamilyHandle columnFamilyHandle)
3860 throws RocksDBException {
3861 flush(flushOptions,
3862 columnFamilyHandle == null ? null : Arrays.asList(columnFamilyHandle));
7c673cae
FG
3863 }
3864
3865 /**
494da23a 3866 * Flushes multiple column families.
7c673cae 3867 *
494da23a
TL
3868 * If atomic flush is not enabled, this is equivalent to calling
3869 * {@link #flush(FlushOptions, ColumnFamilyHandle)} multiple times.
11fdf7f2 3870 *
494da23a
TL
3871 * If atomic flush is enabled, this will flush all column families
3872 * specified up to the latest sequence number at the time when flush is
3873 * requested.
7c673cae 3874 *
494da23a
TL
3875 * @param flushOptions {@link org.rocksdb.FlushOptions} instance.
3876 * @param columnFamilyHandles column family handles.
7c673cae
FG
3877 * @throws RocksDBException thrown if an error occurs within the native
3878 * part of the library.
3879 */
494da23a
TL
3880 public void flush(final FlushOptions flushOptions,
3881 /* @Nullable */ final List<ColumnFamilyHandle> columnFamilyHandles)
7c673cae 3882 throws RocksDBException {
494da23a
TL
3883 flush(nativeHandle_, flushOptions.nativeHandle_,
3884 toNativeHandleList(columnFamilyHandles));
7c673cae
FG
3885 }
3886
3887 /**
494da23a
TL
3888 * Flush the WAL memory buffer to the file. If {@code sync} is true,
3889 * it calls {@link #syncWal()} afterwards.
7c673cae 3890 *
494da23a 3891 * @param sync true to also fsync to disk.
f67539c2
TL
3892 *
3893 * @throws RocksDBException if an error occurs whilst flushing
7c673cae 3894 */
494da23a
TL
3895 public void flushWal(final boolean sync) throws RocksDBException {
3896 flushWal(nativeHandle_, sync);
7c673cae
FG
3897 }
3898
3899 /**
494da23a 3900 * Sync the WAL.
7c673cae 3901 *
494da23a
TL
3902 * Note that {@link #write(WriteOptions, WriteBatch)} followed by
3903 * {@link #syncWal()} is not exactly the same as
3904 * {@link #write(WriteOptions, WriteBatch)} with
3905 * {@link WriteOptions#sync()} set to true; In the latter case the changes
3906 * won't be visible until the sync is done.
3907 *
3908 * Currently only works if {@link Options#allowMmapWrites()} is set to false.
f67539c2
TL
3909 *
3910 * @throws RocksDBException if an error occurs whilst syncing
7c673cae 3911 */
494da23a
TL
3912 public void syncWal() throws RocksDBException {
3913 syncWal(nativeHandle_);
7c673cae
FG
3914 }
3915
3916 /**
3917 * <p>The sequence number of the most recent transaction.</p>
3918 *
3919 * @return sequence number of the most
3920 * recent transaction.
3921 */
3922 public long getLatestSequenceNumber() {
3923 return getLatestSequenceNumber(nativeHandle_);
3924 }
3925
494da23a
TL
3926 /**
3927 * Instructs DB to preserve deletes with sequence numbers &gt;= sequenceNumber.
3928 *
3929 * Has no effect if DBOptions#preserveDeletes() is set to false.
3930 *
3931 * This function assumes that user calls this function with monotonically
3932 * increasing seqnums (otherwise we can't guarantee that a particular delete
3933 * hasn't been already processed).
3934 *
3935 * @param sequenceNumber the minimum sequence number to preserve
3936 *
3937 * @return true if the value was successfully updated,
3938 * false if user attempted to call if with
3939 * sequenceNumber &lt;= current value.
3940 */
3941 public boolean setPreserveDeletesSequenceNumber(final long sequenceNumber) {
3942 return setPreserveDeletesSequenceNumber(nativeHandle_, sequenceNumber);
3943 }
3944
7c673cae
FG
3945 /**
3946 * <p>Prevent file deletions. Compactions will continue to occur,
3947 * but no obsolete files will be deleted. Calling this multiple
3948 * times have the same effect as calling it once.</p>
3949 *
3950 * @throws RocksDBException thrown if operation was not performed
3951 * successfully.
3952 */
3953 public void disableFileDeletions() throws RocksDBException {
3954 disableFileDeletions(nativeHandle_);
3955 }
3956
3957 /**
3958 * <p>Allow compactions to delete obsolete files.
3959 * If force == true, the call to EnableFileDeletions()
3960 * will guarantee that file deletions are enabled after
3961 * the call, even if DisableFileDeletions() was called
3962 * multiple times before.</p>
3963 *
3964 * <p>If force == false, EnableFileDeletions will only
3965 * enable file deletion after it's been called at least
3966 * as many times as DisableFileDeletions(), enabling
3967 * the two methods to be called by two threads
3968 * concurrently without synchronization
3969 * -- i.e., file deletions will be enabled only after both
3970 * threads call EnableFileDeletions()</p>
3971 *
3972 * @param force boolean value described above.
3973 *
3974 * @throws RocksDBException thrown if operation was not performed
3975 * successfully.
3976 */
3977 public void enableFileDeletions(final boolean force)
3978 throws RocksDBException {
3979 enableFileDeletions(nativeHandle_, force);
3980 }
3981
494da23a
TL
3982 public static class LiveFiles {
3983 /**
3984 * The valid size of the manifest file. The manifest file is an ever growing
3985 * file, but only the portion specified here is valid for this snapshot.
3986 */
3987 public final long manifestFileSize;
3988
3989 /**
3990 * The files are relative to the {@link #getName()} and are not
3991 * absolute paths. Despite being relative paths, the file names begin
3992 * with "/".
3993 */
3994 public final List<String> files;
3995
3996 LiveFiles(final long manifestFileSize, final List<String> files) {
3997 this.manifestFileSize = manifestFileSize;
3998 this.files = files;
3999 }
4000 }
4001
4002 /**
4003 * Retrieve the list of all files in the database after flushing the memtable.
4004 *
4005 * See {@link #getLiveFiles(boolean)}.
4006 *
4007 * @return the live files
f67539c2
TL
4008 *
4009 * @throws RocksDBException if an error occurs whilst retrieving the list
4010 * of live files
494da23a
TL
4011 */
4012 public LiveFiles getLiveFiles() throws RocksDBException {
4013 return getLiveFiles(true);
4014 }
4015
4016 /**
4017 * Retrieve the list of all files in the database.
4018 *
4019 * In case you have multiple column families, even if {@code flushMemtable}
4020 * is true, you still need to call {@link #getSortedWalFiles()}
4021 * after {@link #getLiveFiles(boolean)} to compensate for new data that
4022 * arrived to already-flushed column families while other column families
4023 * were flushing.
4024 *
4025 * NOTE: Calling {@link #getLiveFiles(boolean)} followed by
4026 * {@link #getSortedWalFiles()} can generate a lossless backup.
4027 *
4028 * @param flushMemtable set to true to flush before recoding the live
4029 * files. Setting to false is useful when we don't want to wait for flush
4030 * which may have to wait for compaction to complete taking an
4031 * indeterminate time.
4032 *
4033 * @return the live files
f67539c2
TL
4034 *
4035 * @throws RocksDBException if an error occurs whilst retrieving the list
4036 * of live files
494da23a
TL
4037 */
4038 public LiveFiles getLiveFiles(final boolean flushMemtable)
4039 throws RocksDBException {
4040 final String[] result = getLiveFiles(nativeHandle_, flushMemtable);
4041 if (result == null) {
4042 return null;
4043 }
4044 final String[] files = Arrays.copyOf(result, result.length - 1);
4045 final long manifestFileSize = Long.parseLong(result[result.length - 1]);
4046
4047 return new LiveFiles(manifestFileSize, Arrays.asList(files));
4048 }
4049
4050 /**
4051 * Retrieve the sorted list of all wal files with earliest file first.
4052 *
4053 * @return the log files
f67539c2
TL
4054 *
4055 * @throws RocksDBException if an error occurs whilst retrieving the list
4056 * of sorted WAL files
494da23a
TL
4057 */
4058 public List<LogFile> getSortedWalFiles() throws RocksDBException {
4059 final LogFile[] logFiles = getSortedWalFiles(nativeHandle_);
4060 return Arrays.asList(logFiles);
4061 }
4062
7c673cae
FG
4063 /**
4064 * <p>Returns an iterator that is positioned at a write-batch containing
4065 * seq_number. If the sequence number is non existent, it returns an iterator
4066 * at the first available seq_no after the requested seq_no.</p>
4067 *
4068 * <p>Must set WAL_ttl_seconds or WAL_size_limit_MB to large values to
4069 * use this api, else the WAL files will get
4070 * cleared aggressively and the iterator might keep getting invalid before
4071 * an update is read.</p>
4072 *
4073 * @param sequenceNumber sequence number offset
4074 *
4075 * @return {@link org.rocksdb.TransactionLogIterator} instance.
4076 *
4077 * @throws org.rocksdb.RocksDBException if iterator cannot be retrieved
4078 * from native-side.
4079 */
4080 public TransactionLogIterator getUpdatesSince(final long sequenceNumber)
4081 throws RocksDBException {
4082 return new TransactionLogIterator(
4083 getUpdatesSince(nativeHandle_, sequenceNumber));
4084 }
4085
494da23a
TL
4086 /**
4087 * Delete the file name from the db directory and update the internal state to
4088 * reflect that. Supports deletion of sst and log files only. 'name' must be
4089 * path relative to the db directory. eg. 000001.sst, /archive/000003.log
4090 *
4091 * @param name the file name
f67539c2
TL
4092 *
4093 * @throws RocksDBException if an error occurs whilst deleting the file
494da23a
TL
4094 */
4095 public void deleteFile(final String name) throws RocksDBException {
4096 deleteFile(nativeHandle_, name);
7c673cae
FG
4097 }
4098
494da23a
TL
4099 /**
4100 * Gets a list of all table files metadata.
4101 *
4102 * @return table files metadata.
4103 */
4104 public List<LiveFileMetaData> getLiveFilesMetaData() {
4105 return Arrays.asList(getLiveFilesMetaData(nativeHandle_));
4106 }
4107
4108 /**
4109 * Obtains the meta data of the specified column family of the DB.
4110 *
4111 * @param columnFamilyHandle the column family
4112 *
4113 * @return the column family metadata
4114 */
4115 public ColumnFamilyMetaData getColumnFamilyMetaData(
4116 /* @Nullable */ final ColumnFamilyHandle columnFamilyHandle) {
4117 return getColumnFamilyMetaData(nativeHandle_,
4118 columnFamilyHandle == null ? 0 : columnFamilyHandle.nativeHandle_);
4119 }
4120
4121 /**
4122 * Obtains the meta data of the default column family of the DB.
4123 *
4124 * @return the column family metadata
4125 */
20effc67 4126 public ColumnFamilyMetaData getColumnFamilyMetaData() {
494da23a 4127 return getColumnFamilyMetaData(null);
7c673cae
FG
4128 }
4129
11fdf7f2
TL
4130 /**
4131 * ingestExternalFile will load a list of external SST files (1) into the DB
4132 * We will try to find the lowest possible level that the file can fit in, and
4133 * ingest the file into this level (2). A file that have a key range that
4134 * overlap with the memtable key range will require us to Flush the memtable
4135 * first before ingesting the file.
4136 *
4137 * (1) External SST files can be created using {@link SstFileWriter}
4138 * (2) We will try to ingest the files to the lowest possible level
4139 * even if the file compression doesn't match the level compression
4140 *
4141 * @param filePathList The list of files to ingest
4142 * @param ingestExternalFileOptions the options for the ingestion
4143 *
4144 * @throws RocksDBException thrown if error happens in underlying
4145 * native library.
4146 */
4147 public void ingestExternalFile(final List<String> filePathList,
4148 final IngestExternalFileOptions ingestExternalFileOptions)
7c673cae 4149 throws RocksDBException {
11fdf7f2 4150 ingestExternalFile(nativeHandle_, getDefaultColumnFamily().nativeHandle_,
494da23a 4151 filePathList.toArray(new String[0]),
11fdf7f2 4152 filePathList.size(), ingestExternalFileOptions.nativeHandle_);
7c673cae
FG
4153 }
4154
11fdf7f2
TL
4155 /**
4156 * ingestExternalFile will load a list of external SST files (1) into the DB
4157 * We will try to find the lowest possible level that the file can fit in, and
4158 * ingest the file into this level (2). A file that have a key range that
4159 * overlap with the memtable key range will require us to Flush the memtable
4160 * first before ingesting the file.
4161 *
4162 * (1) External SST files can be created using {@link SstFileWriter}
4163 * (2) We will try to ingest the files to the lowest possible level
4164 * even if the file compression doesn't match the level compression
4165 *
4166 * @param columnFamilyHandle The column family for the ingested files
4167 * @param filePathList The list of files to ingest
4168 * @param ingestExternalFileOptions the options for the ingestion
4169 *
4170 * @throws RocksDBException thrown if error happens in underlying
4171 * native library.
4172 */
4173 public void ingestExternalFile(final ColumnFamilyHandle columnFamilyHandle,
4174 final List<String> filePathList,
4175 final IngestExternalFileOptions ingestExternalFileOptions)
7c673cae 4176 throws RocksDBException {
11fdf7f2 4177 ingestExternalFile(nativeHandle_, columnFamilyHandle.nativeHandle_,
494da23a 4178 filePathList.toArray(new String[0]),
11fdf7f2 4179 filePathList.size(), ingestExternalFileOptions.nativeHandle_);
7c673cae
FG
4180 }
4181
494da23a
TL
4182 /**
4183 * Verify checksum
4184 *
4185 * @throws RocksDBException if the checksum is not valid
4186 */
4187 public void verifyChecksum() throws RocksDBException {
4188 verifyChecksum(nativeHandle_);
4189 }
4190
4191 /**
4192 * Gets the handle for the default column family
4193 *
4194 * @return The handle of the default column family
4195 */
4196 public ColumnFamilyHandle getDefaultColumnFamily() {
4197 final ColumnFamilyHandle cfHandle = new ColumnFamilyHandle(this,
4198 getDefaultColumnFamily(nativeHandle_));
4199 cfHandle.disOwnNativeHandle();
4200 return cfHandle;
4201 }
4202
4203 /**
4204 * Get the properties of all tables.
4205 *
4206 * @param columnFamilyHandle the column family handle, or null for the default
4207 * column family.
4208 *
4209 * @return the properties
f67539c2
TL
4210 *
4211 * @throws RocksDBException if an error occurs whilst getting the properties
494da23a
TL
4212 */
4213 public Map<String, TableProperties> getPropertiesOfAllTables(
4214 /* @Nullable */final ColumnFamilyHandle columnFamilyHandle)
4215 throws RocksDBException {
4216 return getPropertiesOfAllTables(nativeHandle_,
4217 columnFamilyHandle == null ? 0 : columnFamilyHandle.nativeHandle_);
4218 }
4219
4220 /**
4221 * Get the properties of all tables in the default column family.
4222 *
4223 * @return the properties
f67539c2
TL
4224 *
4225 * @throws RocksDBException if an error occurs whilst getting the properties
494da23a
TL
4226 */
4227 public Map<String, TableProperties> getPropertiesOfAllTables()
4228 throws RocksDBException {
4229 return getPropertiesOfAllTables(null);
4230 }
4231
4232 /**
4233 * Get the properties of tables in range.
4234 *
4235 * @param columnFamilyHandle the column family handle, or null for the default
4236 * column family.
4237 * @param ranges the ranges over which to get the table properties
4238 *
4239 * @return the properties
f67539c2
TL
4240 *
4241 * @throws RocksDBException if an error occurs whilst getting the properties
494da23a
TL
4242 */
4243 public Map<String, TableProperties> getPropertiesOfTablesInRange(
4244 /* @Nullable */final ColumnFamilyHandle columnFamilyHandle,
4245 final List<Range> ranges) throws RocksDBException {
4246 return getPropertiesOfTablesInRange(nativeHandle_,
4247 columnFamilyHandle == null ? 0 : columnFamilyHandle.nativeHandle_,
4248 toRangeSliceHandles(ranges));
4249 }
4250
4251 /**
4252 * Get the properties of tables in range for the default column family.
4253 *
4254 * @param ranges the ranges over which to get the table properties
4255 *
4256 * @return the properties
f67539c2
TL
4257 *
4258 * @throws RocksDBException if an error occurs whilst getting the properties
494da23a
TL
4259 */
4260 public Map<String, TableProperties> getPropertiesOfTablesInRange(
4261 final List<Range> ranges) throws RocksDBException {
4262 return getPropertiesOfTablesInRange(null, ranges);
4263 }
4264
4265 /**
4266 * Suggest the range to compact.
4267 *
4268 * @param columnFamilyHandle the column family handle, or null for the default
4269 * column family.
4270 *
4271 * @return the suggested range.
f67539c2
TL
4272 *
4273 * @throws RocksDBException if an error occurs whilst suggesting the range
494da23a
TL
4274 */
4275 public Range suggestCompactRange(
4276 /* @Nullable */final ColumnFamilyHandle columnFamilyHandle)
4277 throws RocksDBException {
4278 final long[] rangeSliceHandles = suggestCompactRange(nativeHandle_,
4279 columnFamilyHandle == null ? 0 : columnFamilyHandle.nativeHandle_);
4280 return new Range(new Slice(rangeSliceHandles[0]),
4281 new Slice(rangeSliceHandles[1]));
4282 }
4283
4284 /**
4285 * Suggest the range to compact for the default column family.
4286 *
4287 * @return the suggested range.
f67539c2
TL
4288 *
4289 * @throws RocksDBException if an error occurs whilst suggesting the range
494da23a
TL
4290 */
4291 public Range suggestCompactRange()
4292 throws RocksDBException {
4293 return suggestCompactRange(null);
4294 }
4295
4296 /**
4297 * Promote L0.
4298 *
4299 * @param columnFamilyHandle the column family handle,
4300 * or null for the default column family.
f67539c2
TL
4301 * @param targetLevel the target level for L0
4302 *
4303 * @throws RocksDBException if an error occurs whilst promoting L0
494da23a
TL
4304 */
4305 public void promoteL0(
4306 /* @Nullable */final ColumnFamilyHandle columnFamilyHandle,
4307 final int targetLevel) throws RocksDBException {
4308 promoteL0(nativeHandle_,
4309 columnFamilyHandle == null ? 0 : columnFamilyHandle.nativeHandle_,
4310 targetLevel);
4311 }
4312
4313 /**
4314 * Promote L0 for the default column family.
f67539c2
TL
4315 *
4316 * @param targetLevel the target level for L0
4317 *
4318 * @throws RocksDBException if an error occurs whilst promoting L0
494da23a
TL
4319 */
4320 public void promoteL0(final int targetLevel)
4321 throws RocksDBException {
4322 promoteL0(null, targetLevel);
4323 }
4324
4325 /**
4326 * Trace DB operations.
4327 *
4328 * Use {@link #endTrace()} to stop tracing.
4329 *
4330 * @param traceOptions the options
4331 * @param traceWriter the trace writer
f67539c2
TL
4332 *
4333 * @throws RocksDBException if an error occurs whilst starting the trace
494da23a
TL
4334 */
4335 public void startTrace(final TraceOptions traceOptions,
4336 final AbstractTraceWriter traceWriter) throws RocksDBException {
4337 startTrace(nativeHandle_, traceOptions.getMaxTraceFileSize(),
4338 traceWriter.nativeHandle_);
4339 /**
4340 * NOTE: {@link #startTrace(long, long, long) transfers the ownership
4341 * from Java to C++, so we must disown the native handle here.
4342 */
4343 traceWriter.disOwnNativeHandle();
4344 }
4345
4346 /**
4347 * Stop tracing DB operations.
4348 *
4349 * See {@link #startTrace(TraceOptions, AbstractTraceWriter)}
f67539c2
TL
4350 *
4351 * @throws RocksDBException if an error occurs whilst ending the trace
494da23a
TL
4352 */
4353 public void endTrace() throws RocksDBException {
4354 endTrace(nativeHandle_);
4355 }
4356
20effc67
TL
4357 /**
4358 * Make the secondary instance catch up with the primary by tailing and
4359 * replaying the MANIFEST and WAL of the primary.
4360 * Column families created by the primary after the secondary instance starts
4361 * will be ignored unless the secondary instance closes and restarts with the
4362 * newly created column families.
4363 * Column families that exist before secondary instance starts and dropped by
4364 * the primary afterwards will be marked as dropped. However, as long as the
4365 * secondary instance does not delete the corresponding column family
4366 * handles, the data of the column family is still accessible to the
4367 * secondary.
4368 *
4369 * @throws RocksDBException thrown if error happens in underlying
4370 * native library.
4371 */
4372 public void tryCatchUpWithPrimary() throws RocksDBException {
4373 tryCatchUpWithPrimary(nativeHandle_);
4374 }
4375
f67539c2
TL
4376 /**
4377 * Delete files in multiple ranges at once.
4378 * Delete files in a lot of ranges one at a time can be slow, use this API for
4379 * better performance in that case.
4380 *
4381 * @param columnFamily - The column family for operation (null for default)
4382 * @param includeEnd - Whether ranges should include end
4383 * @param ranges - pairs of ranges (from1, to1, from2, to2, ...)
4384 *
4385 * @throws RocksDBException thrown if error happens in underlying
4386 * native library.
4387 */
4388 public void deleteFilesInRanges(final ColumnFamilyHandle columnFamily,
4389 final List<byte[]> ranges, final boolean includeEnd)
4390 throws RocksDBException {
4391 if (ranges.size() == 0) {
4392 return;
4393 }
4394 if ((ranges.size() % 2) != 0) {
4395 throw new IllegalArgumentException("Ranges size needs to be multiple of 2 "
4396 + "(from1, to1, from2, to2, ...), but is " + ranges.size());
4397 }
4398
4399 final byte[][] rangesArray = ranges.toArray(new byte[ranges.size()][]);
4400
4401 deleteFilesInRanges(nativeHandle_, columnFamily == null ? 0 : columnFamily.nativeHandle_,
4402 rangesArray, includeEnd);
4403 }
4404
11fdf7f2
TL
4405 /**
4406 * Static method to destroy the contents of the specified database.
4407 * Be very careful using this method.
4408 *
4409 * @param path the path to the Rocksdb database.
4410 * @param options {@link org.rocksdb.Options} instance.
4411 *
4412 * @throws RocksDBException thrown if error happens in underlying
4413 * native library.
4414 */
4415 public static void destroyDB(final String path, final Options options)
7c673cae 4416 throws RocksDBException {
11fdf7f2 4417 destroyDB(path, options.nativeHandle_);
7c673cae
FG
4418 }
4419
494da23a
TL
4420 private /* @Nullable */ long[] toNativeHandleList(
4421 /* @Nullable */ final List<? extends RocksObject> objectList) {
4422 if (objectList == null) {
4423 return null;
4424 }
4425 final int len = objectList.size();
4426 final long[] handleList = new long[len];
4427 for (int i = 0; i < len; i++) {
4428 handleList[i] = objectList.get(i).nativeHandle_;
4429 }
4430 return handleList;
4431 }
4432
4433 private static long[] toRangeSliceHandles(final List<Range> ranges) {
4434 final long rangeSliceHandles[] = new long [ranges.size() * 2];
4435 for (int i = 0, j = 0; i < ranges.size(); i++) {
4436 final Range range = ranges.get(i);
4437 rangeSliceHandles[j++] = range.start.getNativeHandle();
4438 rangeSliceHandles[j++] = range.limit.getNativeHandle();
4439 }
4440 return rangeSliceHandles;
4441 }
4442
4443 protected void storeOptionsInstance(DBOptionsInterface options) {
4444 options_ = options;
4445 }
4446
4447 private static void checkBounds(int offset, int len, int size) {
4448 if ((offset | len | (offset + len) | (size - (offset + len))) < 0) {
4449 throw new IndexOutOfBoundsException(String.format("offset(%d), len(%d), size(%d)", offset, len, size));
4450 }
4451 }
4452
4453 private static int computeCapacityHint(final int estimatedNumberOfItems) {
4454 // Default load factor for HashMap is 0.75, so N * 1.5 will be at the load
4455 // limit. We add +1 for a buffer.
4456 return (int)Math.ceil(estimatedNumberOfItems * 1.5 + 1.0);
7c673cae
FG
4457 }
4458
4459 // native methods
494da23a 4460 private native static long open(final long optionsHandle,
7c673cae
FG
4461 final String path) throws RocksDBException;
4462
4463 /**
4464 * @param optionsHandle Native handle pointing to an Options object
4465 * @param path The directory path for the database files
4466 * @param columnFamilyNames An array of column family names
4467 * @param columnFamilyOptions An array of native handles pointing to
4468 * ColumnFamilyOptions objects
4469 *
4470 * @return An array of native handles, [0] is the handle of the RocksDB object
4471 * [1..1+n] are handles of the ColumnFamilyReferences
4472 *
4473 * @throws RocksDBException thrown if the database could not be opened
4474 */
494da23a 4475 private native static long[] open(final long optionsHandle,
7c673cae
FG
4476 final String path, final byte[][] columnFamilyNames,
4477 final long[] columnFamilyOptions) throws RocksDBException;
4478
20effc67
TL
4479 private native static long openROnly(final long optionsHandle, final String path,
4480 final boolean errorIfWalFileExists) throws RocksDBException;
7c673cae
FG
4481
4482 /**
4483 * @param optionsHandle Native handle pointing to an Options object
4484 * @param path The directory path for the database files
4485 * @param columnFamilyNames An array of column family names
4486 * @param columnFamilyOptions An array of native handles pointing to
4487 * ColumnFamilyOptions objects
4488 *
4489 * @return An array of native handles, [0] is the handle of the RocksDB object
4490 * [1..1+n] are handles of the ColumnFamilyReferences
4491 *
4492 * @throws RocksDBException thrown if the database could not be opened
4493 */
20effc67
TL
4494 private native static long[] openROnly(final long optionsHandle, final String path,
4495 final byte[][] columnFamilyNames, final long[] columnFamilyOptions,
4496 final boolean errorIfWalFileExists) throws RocksDBException;
4497
4498 private native static long openAsSecondary(final long optionsHandle, final String path,
4499 final String secondaryPath) throws RocksDBException;
4500
4501 private native static long[] openAsSecondary(final long optionsHandle, final String path,
4502 final String secondaryPath, final byte[][] columnFamilyNames,
4503 final long[] columnFamilyOptions) throws RocksDBException;
7c673cae 4504
494da23a
TL
4505 @Override protected native void disposeInternal(final long handle);
4506
4507 private native static void closeDatabase(final long handle)
4508 throws RocksDBException;
4509 private native static byte[][] listColumnFamilies(final long optionsHandle,
4510 final String path) throws RocksDBException;
4511 private native long createColumnFamily(final long handle,
4512 final byte[] columnFamilyName, final int columnFamilyNamelen,
4513 final long columnFamilyOptions) throws RocksDBException;
4514 private native long[] createColumnFamilies(final long handle,
4515 final long columnFamilyOptionsHandle, final byte[][] columnFamilyNames)
4516 throws RocksDBException;
4517 private native long[] createColumnFamilies(final long handle,
4518 final long columnFamilyOptionsHandles[], final byte[][] columnFamilyNames)
4519 throws RocksDBException;
4520 private native void dropColumnFamily(
4521 final long handle, final long cfHandle) throws RocksDBException;
4522 private native void dropColumnFamilies(final long handle,
4523 final long[] cfHandles) throws RocksDBException;
494da23a
TL
4524 private native void put(final long handle, final byte[] key,
4525 final int keyOffset, final int keyLength, final byte[] value,
4526 final int valueOffset, int valueLength) throws RocksDBException;
4527 private native void put(final long handle, final byte[] key, final int keyOffset,
4528 final int keyLength, final byte[] value, final int valueOffset,
4529 final int valueLength, final long cfHandle) throws RocksDBException;
4530 private native void put(final long handle, final long writeOptHandle,
4531 final byte[] key, final int keyOffset, final int keyLength,
4532 final byte[] value, final int valueOffset, final int valueLength)
4533 throws RocksDBException;
4534 private native void put(final long handle, final long writeOptHandle,
4535 final byte[] key, final int keyOffset, final int keyLength,
4536 final byte[] value, final int valueOffset, final int valueLength,
4537 final long cfHandle) throws RocksDBException;
4538 private native void delete(final long handle, final byte[] key,
4539 final int keyOffset, final int keyLength) throws RocksDBException;
4540 private native void delete(final long handle, final byte[] key,
4541 final int keyOffset, final int keyLength, final long cfHandle)
4542 throws RocksDBException;
4543 private native void delete(final long handle, final long writeOptHandle,
4544 final byte[] key, final int keyOffset, final int keyLength)
4545 throws RocksDBException;
4546 private native void delete(final long handle, final long writeOptHandle,
4547 final byte[] key, final int keyOffset, final int keyLength,
4548 final long cfHandle) throws RocksDBException;
4549 private native void singleDelete(
4550 final long handle, final byte[] key, final int keyLen)
4551 throws RocksDBException;
4552 private native void singleDelete(
4553 final long handle, final byte[] key, final int keyLen,
4554 final long cfHandle) throws RocksDBException;
4555 private native void singleDelete(
4556 final long handle, final long writeOptHandle, final byte[] key,
4557 final int keyLen) throws RocksDBException;
4558 private native void singleDelete(
4559 final long handle, final long writeOptHandle,
4560 final byte[] key, final int keyLen, final long cfHandle)
7c673cae 4561 throws RocksDBException;
494da23a
TL
4562 private native void deleteRange(final long handle, final byte[] beginKey,
4563 final int beginKeyOffset, final int beginKeyLength, final byte[] endKey,
4564 final int endKeyOffset, final int endKeyLength) throws RocksDBException;
4565 private native void deleteRange(final long handle, final byte[] beginKey,
4566 final int beginKeyOffset, final int beginKeyLength, final byte[] endKey,
4567 final int endKeyOffset, final int endKeyLength, final long cfHandle)
4568 throws RocksDBException;
4569 private native void deleteRange(final long handle, final long writeOptHandle,
4570 final byte[] beginKey, final int beginKeyOffset, final int beginKeyLength,
4571 final byte[] endKey, final int endKeyOffset, final int endKeyLength)
4572 throws RocksDBException;
4573 private native void deleteRange(
4574 final long handle, final long writeOptHandle, final byte[] beginKey,
4575 final int beginKeyOffset, final int beginKeyLength, final byte[] endKey,
4576 final int endKeyOffset, final int endKeyLength, final long cfHandle)
4577 throws RocksDBException;
4578 private native void merge(final long handle, final byte[] key,
4579 final int keyOffset, final int keyLength, final byte[] value,
4580 final int valueOffset, final int valueLength) throws RocksDBException;
4581 private native void merge(final long handle, final byte[] key,
4582 final int keyOffset, final int keyLength, final byte[] value,
4583 final int valueOffset, final int valueLength, final long cfHandle)
4584 throws RocksDBException;
4585 private native void merge(final long handle, final long writeOptHandle,
4586 final byte[] key, final int keyOffset, final int keyLength,
4587 final byte[] value, final int valueOffset, final int valueLength)
4588 throws RocksDBException;
4589 private native void merge(final long handle, final long writeOptHandle,
4590 final byte[] key, final int keyOffset, final int keyLength,
4591 final byte[] value, final int valueOffset, final int valueLength,
4592 final long cfHandle) throws RocksDBException;
4593 private native void write0(final long handle, final long writeOptHandle,
4594 final long wbHandle) throws RocksDBException;
4595 private native void write1(final long handle, final long writeOptHandle,
4596 final long wbwiHandle) throws RocksDBException;
4597 private native int get(final long handle, final byte[] key,
4598 final int keyOffset, final int keyLength, final byte[] value,
4599 final int valueOffset, final int valueLength) throws RocksDBException;
4600 private native int get(final long handle, final byte[] key,
4601 final int keyOffset, final int keyLength, byte[] value,
4602 final int valueOffset, final int valueLength, final long cfHandle)
4603 throws RocksDBException;
4604 private native int get(final long handle, final long readOptHandle,
4605 final byte[] key, final int keyOffset, final int keyLength,
4606 final byte[] value, final int valueOffset, final int valueLength)
4607 throws RocksDBException;
4608 private native int get(final long handle, final long readOptHandle,
4609 final byte[] key, final int keyOffset, final int keyLength,
4610 final byte[] value, final int valueOffset, final int valueLength,
4611 final long cfHandle) throws RocksDBException;
4612 private native byte[] get(final long handle, byte[] key, final int keyOffset,
4613 final int keyLength) throws RocksDBException;
4614 private native byte[] get(final long handle, final byte[] key,
4615 final int keyOffset, final int keyLength, final long cfHandle)
4616 throws RocksDBException;
4617 private native byte[] get(final long handle, final long readOptHandle,
4618 final byte[] key, final int keyOffset, final int keyLength)
4619 throws RocksDBException;
4620 private native byte[] get(final long handle,
4621 final long readOptHandle, final byte[] key, final int keyOffset,
4622 final int keyLength, final long cfHandle) throws RocksDBException;
4623 private native byte[][] multiGet(final long dbHandle, final byte[][] keys,
4624 final int[] keyOffsets, final int[] keyLengths);
4625 private native byte[][] multiGet(final long dbHandle, final byte[][] keys,
4626 final int[] keyOffsets, final int[] keyLengths,
4627 final long[] columnFamilyHandles);
4628 private native byte[][] multiGet(final long dbHandle, final long rOptHandle,
4629 final byte[][] keys, final int[] keyOffsets, final int[] keyLengths);
4630 private native byte[][] multiGet(final long dbHandle, final long rOptHandle,
4631 final byte[][] keys, final int[] keyOffsets, final int[] keyLengths,
4632 final long[] columnFamilyHandles);
f67539c2
TL
4633 private native boolean keyMayExist(
4634 final long handle, final long cfHandle, final long readOptHandle,
4635 final byte[] key, final int keyOffset, final int keyLength);
4636 private native byte[][] keyMayExistFoundValue(
4637 final long handle, final long cfHandle, final long readOptHandle,
4638 final byte[] key, final int keyOffset, final int keyLength);
4639 private native void putDirect(long handle, long writeOptHandle, ByteBuffer key, int keyOffset,
4640 int keyLength, ByteBuffer value, int valueOffset, int valueLength, long cfHandle)
4641 throws RocksDBException;
494da23a
TL
4642 private native long iterator(final long handle);
4643 private native long iterator(final long handle, final long readOptHandle);
4644 private native long iteratorCF(final long handle, final long cfHandle);
4645 private native long iteratorCF(final long handle, final long cfHandle,
4646 final long readOptHandle);
4647 private native long[] iterators(final long handle,
4648 final long[] columnFamilyHandles, final long readOptHandle)
7c673cae 4649 throws RocksDBException;
494da23a
TL
4650 private native long getSnapshot(final long nativeHandle);
4651 private native void releaseSnapshot(
4652 final long nativeHandle, final long snapshotHandle);
4653 private native String getProperty(final long nativeHandle,
4654 final long cfHandle, final String property, final int propertyLength)
7c673cae 4655 throws RocksDBException;
494da23a
TL
4656 private native Map<String, String> getMapProperty(final long nativeHandle,
4657 final long cfHandle, final String property, final int propertyLength)
7c673cae 4658 throws RocksDBException;
f67539c2
TL
4659 private native int getDirect(long handle, long readOptHandle, ByteBuffer key, int keyOffset,
4660 int keyLength, ByteBuffer value, int valueOffset, int valueLength, long cfHandle)
4661 throws RocksDBException;
4662 private native void deleteDirect(long handle, long optHandle, ByteBuffer key, int keyOffset,
4663 int keyLength, long cfHandle) throws RocksDBException;
494da23a
TL
4664 private native long getLongProperty(final long nativeHandle,
4665 final long cfHandle, final String property, final int propertyLength)
7c673cae 4666 throws RocksDBException;
494da23a 4667 private native void resetStats(final long nativeHandle)
7c673cae 4668 throws RocksDBException;
494da23a
TL
4669 private native long getAggregatedLongProperty(final long nativeHandle,
4670 final String property, int propertyLength) throws RocksDBException;
4671 private native long[] getApproximateSizes(final long nativeHandle,
4672 final long columnFamilyHandle, final long[] rangeSliceHandles,
4673 final byte includeFlags);
4674 private final native long[] getApproximateMemTableStats(
4675 final long nativeHandle, final long columnFamilyHandle,
4676 final long rangeStartSliceHandle, final long rangeLimitSliceHandle);
4677 private native void compactRange(final long handle,
4678 /* @Nullable */ final byte[] begin, final int beginLen,
4679 /* @Nullable */ final byte[] end, final int endLen,
4680 final long compactRangeOptHandle, final long cfHandle)
7c673cae 4681 throws RocksDBException;
494da23a
TL
4682 private native void setOptions(final long handle, final long cfHandle,
4683 final String[] keys, final String[] values) throws RocksDBException;
4684 private native void setDBOptions(final long handle,
4685 final String[] keys, final String[] values) throws RocksDBException;
4686 private native String[] compactFiles(final long handle,
4687 final long compactionOptionsHandle,
4688 final long columnFamilyHandle,
4689 final String[] inputFileNames,
4690 final int outputLevel,
4691 final int outputPathId,
4692 final long compactionJobInfoHandle) throws RocksDBException;
20effc67
TL
4693 private native void cancelAllBackgroundWork(final long handle,
4694 final boolean wait);
494da23a 4695 private native void pauseBackgroundWork(final long handle)
7c673cae 4696 throws RocksDBException;
494da23a 4697 private native void continueBackgroundWork(final long handle)
7c673cae 4698 throws RocksDBException;
494da23a
TL
4699 private native void enableAutoCompaction(final long handle,
4700 final long[] columnFamilyHandles) throws RocksDBException;
4701 private native int numberLevels(final long handle,
4702 final long columnFamilyHandle);
4703 private native int maxMemCompactionLevel(final long handle,
4704 final long columnFamilyHandle);
4705 private native int level0StopWriteTrigger(final long handle,
4706 final long columnFamilyHandle);
4707 private native String getName(final long handle);
4708 private native long getEnv(final long handle);
4709 private native void flush(final long handle, final long flushOptHandle,
4710 /* @Nullable */ final long[] cfHandles) throws RocksDBException;
4711 private native void flushWal(final long handle, final boolean sync)
7c673cae 4712 throws RocksDBException;
494da23a
TL
4713 private native void syncWal(final long handle) throws RocksDBException;
4714 private native long getLatestSequenceNumber(final long handle);
4715 private native boolean setPreserveDeletesSequenceNumber(final long handle,
4716 final long sequenceNumber);
4717 private native void disableFileDeletions(long handle) throws RocksDBException;
4718 private native void enableFileDeletions(long handle, boolean force)
7c673cae 4719 throws RocksDBException;
494da23a
TL
4720 private native String[] getLiveFiles(final long handle,
4721 final boolean flushMemtable) throws RocksDBException;
4722 private native LogFile[] getSortedWalFiles(final long handle)
7c673cae 4723 throws RocksDBException;
494da23a
TL
4724 private native long getUpdatesSince(final long handle,
4725 final long sequenceNumber) throws RocksDBException;
4726 private native void deleteFile(final long handle, final String name)
7c673cae 4727 throws RocksDBException;
494da23a
TL
4728 private native LiveFileMetaData[] getLiveFilesMetaData(final long handle);
4729 private native ColumnFamilyMetaData getColumnFamilyMetaData(
4730 final long handle, final long columnFamilyHandle);
4731 private native void ingestExternalFile(final long handle,
4732 final long columnFamilyHandle, final String[] filePathList,
4733 final int filePathListLen, final long ingestExternalFileOptionsHandle)
7c673cae 4734 throws RocksDBException;
494da23a
TL
4735 private native void verifyChecksum(final long handle) throws RocksDBException;
4736 private native long getDefaultColumnFamily(final long handle);
4737 private native Map<String, TableProperties> getPropertiesOfAllTables(
4738 final long handle, final long columnFamilyHandle) throws RocksDBException;
4739 private native Map<String, TableProperties> getPropertiesOfTablesInRange(
4740 final long handle, final long columnFamilyHandle,
4741 final long[] rangeSliceHandles);
4742 private native long[] suggestCompactRange(final long handle,
4743 final long columnFamilyHandle) throws RocksDBException;
4744 private native void promoteL0(final long handle,
4745 final long columnFamilyHandle, final int tragetLevel)
7c673cae 4746 throws RocksDBException;
494da23a
TL
4747 private native void startTrace(final long handle, final long maxTraceFileSize,
4748 final long traceWriterHandle) throws RocksDBException;
4749 private native void endTrace(final long handle) throws RocksDBException;
20effc67 4750 private native void tryCatchUpWithPrimary(final long handle) throws RocksDBException;
f67539c2
TL
4751 private native void deleteFilesInRanges(long handle, long cfHandle, final byte[][] ranges,
4752 boolean include_end) throws RocksDBException;
494da23a 4753
11fdf7f2
TL
4754 private native static void destroyDB(final String path,
4755 final long optionsHandle) throws RocksDBException;
494da23a 4756
20effc67
TL
4757 private native static int version();
4758
7c673cae 4759 protected DBOptionsInterface options_;
20effc67
TL
4760 private static Version version;
4761
4762 public static class Version {
4763 private final byte major;
4764 private final byte minor;
4765 private final byte patch;
4766
4767 public Version(final byte major, final byte minor, final byte patch) {
4768 this.major = major;
4769 this.minor = minor;
4770 this.patch = patch;
4771 }
4772
4773 public int getMajor() {
4774 return major;
4775 }
4776
4777 public int getMinor() {
4778 return minor;
4779 }
4780
4781 public int getPatch() {
4782 return patch;
4783 }
4784
4785 @Override
4786 public String toString() {
4787 return getMajor() + "." + getMinor() + "." + getPatch();
4788 }
4789
4790 private static Version fromEncodedVersion(int encodedVersion) {
4791 final byte patch = (byte) (encodedVersion & 0xff);
4792 encodedVersion >>= 8;
4793 final byte minor = (byte) (encodedVersion & 0xff);
4794 encodedVersion >>= 8;
4795 final byte major = (byte) (encodedVersion & 0xff);
4796
4797 return new Version(major, minor, patch);
4798 }
4799 }
7c673cae 4800}