]>
Commit | Line | Data |
---|---|---|
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 | |
6 | package org.rocksdb; | |
7 | ||
8 | import java.util.*; | |
9 | import java.io.IOException; | |
10 | import java.util.concurrent.atomic.AtomicInteger; | |
11 | import java.util.concurrent.atomic.AtomicReference; | |
12 | ||
13 | import org.rocksdb.util.Environment; | |
14 | ||
15 | /** | |
16 | * A RocksDB is a persistent ordered map from keys to values. It is safe for | |
17 | * concurrent access from multiple threads without any external synchronization. | |
18 | * All methods of this class could potentially throw RocksDBException, which | |
19 | * indicates sth wrong at the RocksDB library side and the call failed. | |
20 | */ | |
21 | public class RocksDB extends RocksObject { | |
22 | public static final byte[] DEFAULT_COLUMN_FAMILY = "default".getBytes(); | |
23 | public static final int NOT_FOUND = -1; | |
24 | ||
25 | private enum LibraryState { | |
26 | NOT_LOADED, | |
27 | LOADING, | |
28 | LOADED | |
29 | } | |
30 | ||
31 | private static AtomicReference<LibraryState> libraryLoaded | |
32 | = new AtomicReference<>(LibraryState.NOT_LOADED); | |
33 | ||
34 | static { | |
35 | RocksDB.loadLibrary(); | |
36 | } | |
37 | ||
38 | /** | |
39 | * Loads the necessary library files. | |
40 | * Calling this method twice will have no effect. | |
41 | * By default the method extracts the shared library for loading at | |
42 | * java.io.tmpdir, however, you can override this temporary location by | |
43 | * setting the environment variable ROCKSDB_SHAREDLIB_DIR. | |
44 | */ | |
45 | public static void loadLibrary() { | |
46 | if (libraryLoaded.get() == LibraryState.LOADED) { | |
47 | return; | |
48 | } | |
49 | ||
50 | if (libraryLoaded.compareAndSet(LibraryState.NOT_LOADED, | |
51 | LibraryState.LOADING)) { | |
52 | final String tmpDir = System.getenv("ROCKSDB_SHAREDLIB_DIR"); | |
53 | // loading possibly necessary libraries. | |
54 | for (final CompressionType compressionType : CompressionType.values()) { | |
55 | try { | |
56 | if (compressionType.getLibraryName() != null) { | |
57 | System.loadLibrary(compressionType.getLibraryName()); | |
58 | } | |
59 | } catch (UnsatisfiedLinkError e) { | |
60 | // since it may be optional, we ignore its loading failure here. | |
61 | } | |
62 | } | |
63 | try { | |
64 | NativeLibraryLoader.getInstance().loadLibrary(tmpDir); | |
65 | } catch (IOException e) { | |
66 | libraryLoaded.set(LibraryState.NOT_LOADED); | |
67 | throw new RuntimeException("Unable to load the RocksDB shared library" | |
68 | + e); | |
69 | } | |
70 | ||
71 | libraryLoaded.set(LibraryState.LOADED); | |
72 | return; | |
73 | } | |
74 | ||
75 | while (libraryLoaded.get() == LibraryState.LOADING) { | |
76 | try { | |
77 | Thread.sleep(10); | |
78 | } catch(final InterruptedException e) { | |
79 | //ignore | |
80 | } | |
81 | } | |
82 | } | |
83 | ||
84 | /** | |
85 | * Tries to load the necessary library files from the given list of | |
86 | * directories. | |
87 | * | |
88 | * @param paths a list of strings where each describes a directory | |
89 | * of a library. | |
90 | */ | |
91 | public static void loadLibrary(final List<String> paths) { | |
92 | if (libraryLoaded.get() == LibraryState.LOADED) { | |
93 | return; | |
94 | } | |
95 | ||
96 | if (libraryLoaded.compareAndSet(LibraryState.NOT_LOADED, | |
97 | LibraryState.LOADING)) { | |
98 | for (final CompressionType compressionType : CompressionType.values()) { | |
99 | if (compressionType.equals(CompressionType.NO_COMPRESSION)) { | |
100 | continue; | |
101 | } | |
102 | for (final String path : paths) { | |
103 | try { | |
104 | System.load(path + "/" + Environment.getSharedLibraryFileName( | |
105 | compressionType.getLibraryName())); | |
106 | break; | |
107 | } catch (UnsatisfiedLinkError e) { | |
108 | // since they are optional, we ignore loading fails. | |
109 | } | |
110 | } | |
111 | } | |
112 | boolean success = false; | |
113 | UnsatisfiedLinkError err = null; | |
114 | for (final String path : paths) { | |
115 | try { | |
116 | System.load(path + "/" + | |
117 | Environment.getJniLibraryFileName("rocksdbjni")); | |
118 | success = true; | |
119 | break; | |
120 | } catch (UnsatisfiedLinkError e) { | |
121 | err = e; | |
122 | } | |
123 | } | |
124 | if (!success) { | |
125 | libraryLoaded.set(LibraryState.NOT_LOADED); | |
126 | throw err; | |
127 | } | |
128 | ||
129 | libraryLoaded.set(LibraryState.LOADED); | |
130 | return; | |
131 | } | |
132 | ||
133 | while (libraryLoaded.get() == LibraryState.LOADING) { | |
134 | try { | |
135 | Thread.sleep(10); | |
136 | } catch(final InterruptedException e) { | |
137 | //ignore | |
138 | } | |
139 | } | |
140 | } | |
141 | ||
142 | /** | |
143 | * The factory constructor of RocksDB that opens a RocksDB instance given | |
144 | * the path to the database using the default options w/ createIfMissing | |
145 | * set to true. | |
146 | * | |
147 | * @param path the path to the rocksdb. | |
148 | * @return a {@link RocksDB} instance on success, null if the specified | |
149 | * {@link RocksDB} can not be opened. | |
150 | * | |
151 | * @throws RocksDBException thrown if error happens in underlying | |
152 | * native library. | |
153 | * @see Options#setCreateIfMissing(boolean) | |
154 | */ | |
155 | public static RocksDB open(final String path) throws RocksDBException { | |
156 | // This allows to use the rocksjni default Options instead of | |
157 | // the c++ one. | |
158 | Options options = new Options(); | |
159 | options.setCreateIfMissing(true); | |
160 | return open(options, path); | |
161 | } | |
162 | ||
163 | /** | |
164 | * The factory constructor of RocksDB that opens a RocksDB instance given | |
165 | * the path to the database using the specified options and db path and a list | |
166 | * of column family names. | |
167 | * <p> | |
168 | * If opened in read write mode every existing column family name must be | |
169 | * passed within the list to this method.</p> | |
170 | * <p> | |
171 | * If opened in read-only mode only a subset of existing column families must | |
172 | * be passed to this method.</p> | |
173 | * <p> | |
174 | * Options instance *should* not be disposed before all DBs using this options | |
175 | * instance have been closed. If user doesn't call options dispose explicitly, | |
176 | * then this options instance will be GC'd automatically</p> | |
177 | * <p> | |
178 | * ColumnFamily handles are disposed when the RocksDB instance is disposed. | |
179 | * </p> | |
180 | * | |
181 | * @param path the path to the rocksdb. | |
182 | * @param columnFamilyDescriptors list of column family descriptors | |
183 | * @param columnFamilyHandles will be filled with ColumnFamilyHandle instances | |
184 | * on open. | |
185 | * @return a {@link RocksDB} instance on success, null if the specified | |
186 | * {@link RocksDB} can not be opened. | |
187 | * | |
188 | * @throws RocksDBException thrown if error happens in underlying | |
189 | * native library. | |
190 | * @see DBOptions#setCreateIfMissing(boolean) | |
191 | */ | |
192 | public static RocksDB open(final String path, | |
193 | final List<ColumnFamilyDescriptor> columnFamilyDescriptors, | |
194 | final List<ColumnFamilyHandle> columnFamilyHandles) | |
195 | throws RocksDBException { | |
196 | // This allows to use the rocksjni default Options instead of | |
197 | // the c++ one. | |
198 | DBOptions options = new DBOptions(); | |
199 | return open(options, path, columnFamilyDescriptors, columnFamilyHandles); | |
200 | } | |
201 | ||
202 | /** | |
203 | * The factory constructor of RocksDB that opens a RocksDB instance given | |
204 | * the path to the database using the specified options and db path. | |
205 | * | |
206 | * <p> | |
207 | * Options instance *should* not be disposed before all DBs using this options | |
208 | * instance have been closed. If user doesn't call options dispose explicitly, | |
209 | * then this options instance will be GC'd automatically.</p> | |
210 | * <p> | |
211 | * Options instance can be re-used to open multiple DBs if DB statistics is | |
212 | * not used. If DB statistics are required, then its recommended to open DB | |
213 | * with new Options instance as underlying native statistics instance does not | |
214 | * use any locks to prevent concurrent updates.</p> | |
215 | * | |
216 | * @param options {@link org.rocksdb.Options} instance. | |
217 | * @param path the path to the rocksdb. | |
218 | * @return a {@link RocksDB} instance on success, null if the specified | |
219 | * {@link RocksDB} can not be opened. | |
220 | * | |
221 | * @throws RocksDBException thrown if error happens in underlying | |
222 | * native library. | |
223 | * | |
224 | * @see Options#setCreateIfMissing(boolean) | |
225 | */ | |
226 | public static RocksDB open(final Options options, final String path) | |
227 | throws RocksDBException { | |
228 | // when non-default Options is used, keeping an Options reference | |
229 | // in RocksDB can prevent Java to GC during the life-time of | |
230 | // the currently-created RocksDB. | |
231 | final RocksDB db = new RocksDB(open(options.nativeHandle_, path)); | |
232 | db.storeOptionsInstance(options); | |
233 | return db; | |
234 | } | |
235 | ||
236 | /** | |
237 | * The factory constructor of RocksDB that opens a RocksDB instance given | |
238 | * the path to the database using the specified options and db path and a list | |
239 | * of column family names. | |
240 | * <p> | |
241 | * If opened in read write mode every existing column family name must be | |
242 | * passed within the list to this method.</p> | |
243 | * <p> | |
244 | * If opened in read-only mode only a subset of existing column families must | |
245 | * be passed to this method.</p> | |
246 | * <p> | |
247 | * Options instance *should* not be disposed before all DBs using this options | |
248 | * instance have been closed. If user doesn't call options dispose explicitly, | |
249 | * then this options instance will be GC'd automatically.</p> | |
250 | * <p> | |
251 | * Options instance can be re-used to open multiple DBs if DB statistics is | |
252 | * not used. If DB statistics are required, then its recommended to open DB | |
253 | * with new Options instance as underlying native statistics instance does not | |
254 | * use any locks to prevent concurrent updates.</p> | |
255 | * <p> | |
256 | * ColumnFamily handles are disposed when the RocksDB instance is disposed. | |
257 | * </p> | |
258 | * | |
259 | * @param options {@link org.rocksdb.DBOptions} instance. | |
260 | * @param path the path to the rocksdb. | |
261 | * @param columnFamilyDescriptors list of column family descriptors | |
262 | * @param columnFamilyHandles will be filled with ColumnFamilyHandle instances | |
263 | * on open. | |
264 | * @return a {@link RocksDB} instance on success, null if the specified | |
265 | * {@link RocksDB} can not be opened. | |
266 | * | |
267 | * @throws RocksDBException thrown if error happens in underlying | |
268 | * native library. | |
269 | * | |
270 | * @see DBOptions#setCreateIfMissing(boolean) | |
271 | */ | |
272 | public static RocksDB open(final DBOptions options, final String path, | |
273 | final List<ColumnFamilyDescriptor> columnFamilyDescriptors, | |
274 | final List<ColumnFamilyHandle> columnFamilyHandles) | |
275 | throws RocksDBException { | |
276 | ||
277 | final byte[][] cfNames = new byte[columnFamilyDescriptors.size()][]; | |
278 | final long[] cfOptionHandles = new long[columnFamilyDescriptors.size()]; | |
279 | for (int i = 0; i < columnFamilyDescriptors.size(); i++) { | |
280 | final ColumnFamilyDescriptor cfDescriptor = columnFamilyDescriptors | |
281 | .get(i); | |
282 | cfNames[i] = cfDescriptor.columnFamilyName(); | |
283 | cfOptionHandles[i] = cfDescriptor.columnFamilyOptions().nativeHandle_; | |
284 | } | |
285 | ||
286 | final long[] handles = open(options.nativeHandle_, path, cfNames, | |
287 | cfOptionHandles); | |
288 | final RocksDB db = new RocksDB(handles[0]); | |
289 | db.storeOptionsInstance(options); | |
290 | ||
291 | for (int i = 1; i < handles.length; i++) { | |
292 | columnFamilyHandles.add(new ColumnFamilyHandle(db, handles[i])); | |
293 | } | |
294 | ||
295 | return db; | |
296 | } | |
297 | ||
298 | /** | |
299 | * The factory constructor of RocksDB that opens a RocksDB instance in | |
300 | * Read-Only mode given the path to the database using the default | |
301 | * options. | |
302 | * | |
303 | * @param path the path to the RocksDB. | |
304 | * @return a {@link RocksDB} instance on success, null if the specified | |
305 | * {@link RocksDB} can not be opened. | |
306 | * | |
307 | * @throws RocksDBException thrown if error happens in underlying | |
308 | * native library. | |
309 | */ | |
310 | public static RocksDB openReadOnly(final String path) | |
311 | throws RocksDBException { | |
312 | // This allows to use the rocksjni default Options instead of | |
313 | // the c++ one. | |
314 | Options options = new Options(); | |
315 | return openReadOnly(options, path); | |
316 | } | |
317 | ||
318 | /** | |
319 | * The factory constructor of RocksDB that opens a RocksDB instance in | |
320 | * Read-Only mode given the path to the database using the default | |
321 | * options. | |
322 | * | |
323 | * @param path the path to the RocksDB. | |
324 | * @param columnFamilyDescriptors list of column family descriptors | |
325 | * @param columnFamilyHandles will be filled with ColumnFamilyHandle instances | |
326 | * on open. | |
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 | final List<ColumnFamilyDescriptor> columnFamilyDescriptors, | |
335 | final List<ColumnFamilyHandle> columnFamilyHandles) | |
336 | throws RocksDBException { | |
337 | // This allows to use the rocksjni default Options instead of | |
338 | // the c++ one. | |
339 | final DBOptions options = new DBOptions(); | |
340 | return openReadOnly(options, path, columnFamilyDescriptors, | |
341 | columnFamilyHandles); | |
342 | } | |
343 | ||
344 | /** | |
345 | * The factory constructor of RocksDB that opens a RocksDB instance in | |
346 | * Read-Only mode given the path to the database using the specified | |
347 | * options and db path. | |
348 | * | |
349 | * Options instance *should* not be disposed before all DBs using this options | |
350 | * instance have been closed. If user doesn't call options dispose explicitly, | |
351 | * then this options instance will be GC'd automatically. | |
352 | * | |
353 | * @param options {@link Options} instance. | |
354 | * @param path the path to the RocksDB. | |
355 | * @return a {@link RocksDB} instance on success, null if the specified | |
356 | * {@link RocksDB} can not be opened. | |
357 | * | |
358 | * @throws RocksDBException thrown if error happens in underlying | |
359 | * native library. | |
360 | */ | |
361 | public static RocksDB openReadOnly(final Options options, final String path) | |
362 | throws RocksDBException { | |
363 | // when non-default Options is used, keeping an Options reference | |
364 | // in RocksDB can prevent Java to GC during the life-time of | |
365 | // the currently-created RocksDB. | |
366 | final RocksDB db = new RocksDB(openROnly(options.nativeHandle_, path)); | |
367 | db.storeOptionsInstance(options); | |
368 | return db; | |
369 | } | |
370 | ||
371 | /** | |
372 | * The factory constructor of RocksDB that opens a RocksDB instance in | |
373 | * Read-Only mode given the path to the database using the specified | |
374 | * options and db path. | |
375 | * | |
376 | * <p>This open method allows to open RocksDB using a subset of available | |
377 | * column families</p> | |
378 | * <p>Options instance *should* not be disposed before all DBs using this | |
379 | * options instance have been closed. If user doesn't call options dispose | |
380 | * explicitly,then this options instance will be GC'd automatically.</p> | |
381 | * | |
382 | * @param options {@link DBOptions} instance. | |
383 | * @param path the path to the RocksDB. | |
384 | * @param columnFamilyDescriptors list of column family descriptors | |
385 | * @param columnFamilyHandles will be filled with ColumnFamilyHandle instances | |
386 | * on open. | |
387 | * @return a {@link RocksDB} instance on success, null if the specified | |
388 | * {@link RocksDB} can not be opened. | |
389 | * | |
390 | * @throws RocksDBException thrown if error happens in underlying | |
391 | * native library. | |
392 | */ | |
393 | public static RocksDB openReadOnly(final DBOptions options, final String path, | |
394 | final List<ColumnFamilyDescriptor> columnFamilyDescriptors, | |
395 | final List<ColumnFamilyHandle> columnFamilyHandles) | |
396 | throws RocksDBException { | |
397 | // when non-default Options is used, keeping an Options reference | |
398 | // in RocksDB can prevent Java to GC during the life-time of | |
399 | // the currently-created RocksDB. | |
400 | ||
401 | final byte[][] cfNames = new byte[columnFamilyDescriptors.size()][]; | |
402 | final long[] cfOptionHandles = new long[columnFamilyDescriptors.size()]; | |
403 | for (int i = 0; i < columnFamilyDescriptors.size(); i++) { | |
404 | final ColumnFamilyDescriptor cfDescriptor = columnFamilyDescriptors | |
405 | .get(i); | |
406 | cfNames[i] = cfDescriptor.columnFamilyName(); | |
407 | cfOptionHandles[i] = cfDescriptor.columnFamilyOptions().nativeHandle_; | |
408 | } | |
409 | ||
410 | final long[] handles = openROnly(options.nativeHandle_, path, cfNames, | |
411 | cfOptionHandles); | |
412 | final RocksDB db = new RocksDB(handles[0]); | |
413 | db.storeOptionsInstance(options); | |
414 | ||
415 | for (int i = 1; i < handles.length; i++) { | |
416 | columnFamilyHandles.add(new ColumnFamilyHandle(db, handles[i])); | |
417 | } | |
418 | ||
419 | return db; | |
420 | } | |
421 | /** | |
422 | * Static method to determine all available column families for a | |
423 | * rocksdb database identified by path | |
424 | * | |
425 | * @param options Options for opening the database | |
426 | * @param path Absolute path to rocksdb database | |
427 | * @return List<byte[]> List containing the column family names | |
428 | * | |
429 | * @throws RocksDBException thrown if error happens in underlying | |
430 | * native library. | |
431 | */ | |
432 | public static List<byte[]> listColumnFamilies(final Options options, | |
433 | final String path) throws RocksDBException { | |
434 | return Arrays.asList(RocksDB.listColumnFamilies(options.nativeHandle_, | |
435 | path)); | |
436 | } | |
437 | ||
11fdf7f2 | 438 | protected void storeOptionsInstance(DBOptionsInterface options) { |
7c673cae FG |
439 | options_ = options; |
440 | } | |
441 | ||
442 | /** | |
443 | * Set the database entry for "key" to "value". | |
444 | * | |
445 | * @param key the specified key to be inserted. | |
446 | * @param value the value associated with the specified key. | |
447 | * | |
448 | * @throws RocksDBException thrown if error happens in underlying | |
449 | * native library. | |
450 | */ | |
451 | public void put(final byte[] key, final byte[] value) | |
452 | throws RocksDBException { | |
453 | put(nativeHandle_, key, 0, key.length, value, 0, value.length); | |
454 | } | |
455 | ||
456 | /** | |
457 | * Set the database entry for "key" to "value" in the specified | |
458 | * column family. | |
459 | * | |
460 | * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} | |
461 | * instance | |
462 | * @param key the specified key to be inserted. | |
463 | * @param value the value associated with the specified key. | |
464 | * | |
465 | * throws IllegalArgumentException if column family is not present | |
466 | * | |
467 | * @throws RocksDBException thrown if error happens in underlying | |
468 | * native library. | |
469 | */ | |
470 | public void put(final ColumnFamilyHandle columnFamilyHandle, | |
471 | final byte[] key, final byte[] value) throws RocksDBException { | |
472 | put(nativeHandle_, key, 0, key.length, value, 0, value.length, | |
473 | columnFamilyHandle.nativeHandle_); | |
474 | } | |
475 | ||
476 | /** | |
477 | * Set the database entry for "key" to "value". | |
478 | * | |
479 | * @param writeOpts {@link org.rocksdb.WriteOptions} instance. | |
480 | * @param key the specified key to be inserted. | |
481 | * @param value the value associated with the specified key. | |
482 | * | |
483 | * @throws RocksDBException thrown if error happens in underlying | |
484 | * native library. | |
485 | */ | |
486 | public void put(final WriteOptions writeOpts, final byte[] key, | |
487 | final byte[] value) throws RocksDBException { | |
488 | put(nativeHandle_, writeOpts.nativeHandle_, | |
489 | key, 0, key.length, value, 0, value.length); | |
490 | } | |
491 | ||
492 | /** | |
493 | * Set the database entry for "key" to "value" for the specified | |
494 | * column family. | |
495 | * | |
496 | * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} | |
497 | * instance | |
498 | * @param writeOpts {@link org.rocksdb.WriteOptions} instance. | |
499 | * @param key the specified key to be inserted. | |
500 | * @param value the value associated with the specified key. | |
501 | * | |
502 | * throws IllegalArgumentException if column family is not present | |
503 | * | |
504 | * @throws RocksDBException thrown if error happens in underlying | |
505 | * native library. | |
506 | * @see IllegalArgumentException | |
507 | */ | |
508 | public void put(final ColumnFamilyHandle columnFamilyHandle, | |
509 | final WriteOptions writeOpts, final byte[] key, | |
510 | final byte[] value) throws RocksDBException { | |
511 | put(nativeHandle_, writeOpts.nativeHandle_, key, 0, key.length, value, | |
512 | 0, value.length, columnFamilyHandle.nativeHandle_); | |
513 | } | |
514 | ||
515 | /** | |
516 | * If the key definitely does not exist in the database, then this method | |
517 | * returns false, else true. | |
518 | * | |
519 | * This check is potentially lighter-weight than invoking DB::Get(). One way | |
520 | * to make this lighter weight is to avoid doing any IOs. | |
521 | * | |
522 | * @param key byte array of a key to search for | |
523 | * @param value StringBuilder instance which is a out parameter if a value is | |
524 | * found in block-cache. | |
525 | * @return boolean value indicating if key does not exist or might exist. | |
526 | */ | |
527 | public boolean keyMayExist(final byte[] key, final StringBuilder value) { | |
528 | return keyMayExist(nativeHandle_, key, 0, key.length, value); | |
529 | } | |
530 | ||
531 | /** | |
532 | * If the key definitely does not exist in the database, then this method | |
533 | * returns false, else true. | |
534 | * | |
535 | * This check is potentially lighter-weight than invoking DB::Get(). One way | |
536 | * to make this lighter weight is to avoid doing any IOs. | |
537 | * | |
538 | * @param columnFamilyHandle {@link ColumnFamilyHandle} instance | |
539 | * @param key byte array of a key to search for | |
540 | * @param value StringBuilder instance which is a out parameter if a value is | |
541 | * found in block-cache. | |
542 | * @return boolean value indicating if key does not exist or might exist. | |
543 | */ | |
544 | public boolean keyMayExist(final ColumnFamilyHandle columnFamilyHandle, | |
545 | final byte[] key, final StringBuilder value) { | |
546 | return keyMayExist(nativeHandle_, key, 0, key.length, | |
547 | columnFamilyHandle.nativeHandle_, value); | |
548 | } | |
549 | ||
550 | /** | |
551 | * If the key definitely does not exist in the database, then this method | |
552 | * returns false, else true. | |
553 | * | |
554 | * This check is potentially lighter-weight than invoking DB::Get(). One way | |
555 | * to make this lighter weight is to avoid doing any IOs. | |
556 | * | |
557 | * @param readOptions {@link ReadOptions} instance | |
558 | * @param key byte array of a key to search for | |
559 | * @param value StringBuilder instance which is a out parameter if a value is | |
560 | * found in block-cache. | |
561 | * @return boolean value indicating if key does not exist or might exist. | |
562 | */ | |
563 | public boolean keyMayExist(final ReadOptions readOptions, | |
564 | final byte[] key, final StringBuilder value) { | |
565 | return keyMayExist(nativeHandle_, readOptions.nativeHandle_, | |
566 | key, 0, key.length, value); | |
567 | } | |
568 | ||
569 | /** | |
570 | * If the key definitely does not exist in the database, then this method | |
571 | * returns false, else true. | |
572 | * | |
573 | * This check is potentially lighter-weight than invoking DB::Get(). One way | |
574 | * to make this lighter weight is to avoid doing any IOs. | |
575 | * | |
576 | * @param readOptions {@link ReadOptions} instance | |
577 | * @param columnFamilyHandle {@link ColumnFamilyHandle} instance | |
578 | * @param key byte array of a key to search for | |
579 | * @param value StringBuilder instance which is a out parameter if a value is | |
580 | * found in block-cache. | |
581 | * @return boolean value indicating if key does not exist or might exist. | |
582 | */ | |
583 | public boolean keyMayExist(final ReadOptions readOptions, | |
584 | final ColumnFamilyHandle columnFamilyHandle, final byte[] key, | |
585 | final StringBuilder value) { | |
586 | return keyMayExist(nativeHandle_, readOptions.nativeHandle_, | |
587 | key, 0, key.length, columnFamilyHandle.nativeHandle_, | |
588 | value); | |
589 | } | |
590 | ||
591 | /** | |
592 | * Apply the specified updates to the database. | |
593 | * | |
594 | * @param writeOpts WriteOptions instance | |
595 | * @param updates WriteBatch instance | |
596 | * | |
597 | * @throws RocksDBException thrown if error happens in underlying | |
598 | * native library. | |
599 | */ | |
600 | public void write(final WriteOptions writeOpts, final WriteBatch updates) | |
601 | throws RocksDBException { | |
602 | write0(nativeHandle_, writeOpts.nativeHandle_, updates.nativeHandle_); | |
603 | } | |
604 | ||
605 | /** | |
606 | * Apply the specified updates to the database. | |
607 | * | |
608 | * @param writeOpts WriteOptions instance | |
609 | * @param updates WriteBatchWithIndex instance | |
610 | * | |
611 | * @throws RocksDBException thrown if error happens in underlying | |
612 | * native library. | |
613 | */ | |
614 | public void write(final WriteOptions writeOpts, | |
615 | final WriteBatchWithIndex updates) throws RocksDBException { | |
616 | write1(nativeHandle_, writeOpts.nativeHandle_, updates.nativeHandle_); | |
617 | } | |
618 | ||
619 | /** | |
620 | * Add merge operand for key/value pair. | |
621 | * | |
622 | * @param key the specified key to be merged. | |
623 | * @param value the value to be merged with the current value for | |
624 | * the specified key. | |
625 | * | |
626 | * @throws RocksDBException thrown if error happens in underlying | |
627 | * native library. | |
628 | */ | |
629 | public void merge(final byte[] key, final byte[] value) | |
630 | throws RocksDBException { | |
631 | merge(nativeHandle_, key, 0, key.length, value, 0, value.length); | |
632 | } | |
633 | ||
634 | /** | |
635 | * Add merge operand for key/value pair in a ColumnFamily. | |
636 | * | |
637 | * @param columnFamilyHandle {@link ColumnFamilyHandle} instance | |
638 | * @param key the specified key to be merged. | |
639 | * @param value the value to be merged with the current value for | |
640 | * the specified key. | |
641 | * | |
642 | * @throws RocksDBException thrown if error happens in underlying | |
643 | * native library. | |
644 | */ | |
645 | public void merge(final ColumnFamilyHandle columnFamilyHandle, | |
646 | final byte[] key, final byte[] value) throws RocksDBException { | |
647 | merge(nativeHandle_, key, 0, key.length, value, 0, value.length, | |
648 | columnFamilyHandle.nativeHandle_); | |
649 | } | |
650 | ||
651 | /** | |
652 | * Add merge operand for key/value pair. | |
653 | * | |
654 | * @param writeOpts {@link WriteOptions} for this write. | |
655 | * @param key the specified key to be merged. | |
656 | * @param value the value to be merged with the current value for | |
657 | * the specified key. | |
658 | * | |
659 | * @throws RocksDBException thrown if error happens in underlying | |
660 | * native library. | |
661 | */ | |
662 | public void merge(final WriteOptions writeOpts, final byte[] key, | |
663 | final byte[] value) throws RocksDBException { | |
664 | merge(nativeHandle_, writeOpts.nativeHandle_, | |
665 | key, 0, key.length, value, 0, value.length); | |
666 | } | |
667 | ||
668 | /** | |
669 | * Add merge operand for key/value pair. | |
670 | * | |
671 | * @param columnFamilyHandle {@link ColumnFamilyHandle} instance | |
672 | * @param writeOpts {@link WriteOptions} for this write. | |
673 | * @param key the specified key to be merged. | |
674 | * @param value the value to be merged with the current value for | |
675 | * the specified key. | |
676 | * | |
677 | * @throws RocksDBException thrown if error happens in underlying | |
678 | * native library. | |
679 | */ | |
680 | public void merge(final ColumnFamilyHandle columnFamilyHandle, | |
681 | final WriteOptions writeOpts, final byte[] key, | |
682 | final byte[] value) throws RocksDBException { | |
683 | merge(nativeHandle_, writeOpts.nativeHandle_, | |
684 | key, 0, key.length, value, 0, value.length, | |
685 | columnFamilyHandle.nativeHandle_); | |
686 | } | |
687 | ||
688 | // TODO(AR) we should improve the #get() API, returning -1 (RocksDB.NOT_FOUND) is not very nice | |
689 | // when we could communicate better status into, also the C++ code show that -2 could be returned | |
690 | ||
691 | /** | |
692 | * Get the value associated with the specified key within column family* | |
693 | * @param key the key to retrieve the value. | |
694 | * @param value the out-value to receive the retrieved value. | |
695 | * @return The size of the actual value that matches the specified | |
696 | * {@code key} in byte. If the return value is greater than the | |
697 | * length of {@code value}, then it indicates that the size of the | |
698 | * input buffer {@code value} is insufficient and partial result will | |
699 | * be returned. RocksDB.NOT_FOUND will be returned if the value not | |
700 | * found. | |
701 | * | |
702 | * @throws RocksDBException thrown if error happens in underlying | |
703 | * native library. | |
704 | */ | |
705 | public int get(final byte[] key, final byte[] value) throws RocksDBException { | |
706 | return get(nativeHandle_, key, 0, key.length, value, 0, value.length); | |
707 | } | |
708 | ||
709 | /** | |
710 | * Get the value associated with the specified key within column family. | |
711 | * | |
712 | * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} | |
713 | * instance | |
714 | * @param key the key to retrieve the value. | |
715 | * @param value the out-value to receive the retrieved value. | |
716 | * @return The size of the actual value that matches the specified | |
717 | * {@code key} in byte. If the return value is greater than the | |
718 | * length of {@code value}, then it indicates that the size of the | |
719 | * input buffer {@code value} is insufficient and partial result will | |
720 | * be returned. RocksDB.NOT_FOUND will be returned if the value not | |
721 | * found. | |
722 | * | |
723 | * @throws RocksDBException thrown if error happens in underlying | |
724 | * native library. | |
725 | */ | |
726 | public int get(final ColumnFamilyHandle columnFamilyHandle, final byte[] key, | |
727 | final byte[] value) throws RocksDBException, IllegalArgumentException { | |
728 | return get(nativeHandle_, key, 0, key.length, value, 0, value.length, | |
729 | columnFamilyHandle.nativeHandle_); | |
730 | } | |
731 | ||
732 | /** | |
733 | * Get the value associated with the specified key. | |
734 | * | |
735 | * @param opt {@link org.rocksdb.ReadOptions} instance. | |
736 | * @param key the key to retrieve the value. | |
737 | * @param value the out-value to receive the retrieved value. | |
738 | * @return The size of the actual value that matches the specified | |
739 | * {@code key} in byte. If the return value is greater than the | |
740 | * length of {@code value}, then it indicates that the size of the | |
741 | * input buffer {@code value} is insufficient and partial result will | |
742 | * be returned. RocksDB.NOT_FOUND will be returned if the value not | |
743 | * found. | |
744 | * | |
745 | * @throws RocksDBException thrown if error happens in underlying | |
746 | * native library. | |
747 | */ | |
748 | public int get(final ReadOptions opt, final byte[] key, | |
749 | final byte[] value) throws RocksDBException { | |
750 | return get(nativeHandle_, opt.nativeHandle_, | |
751 | key, 0, key.length, value, 0, value.length); | |
752 | } | |
753 | /** | |
754 | * Get the value associated with the specified key within column family. | |
755 | * | |
756 | * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} | |
757 | * instance | |
758 | * @param opt {@link org.rocksdb.ReadOptions} instance. | |
759 | * @param key the key to retrieve the value. | |
760 | * @param value the out-value to receive the retrieved value. | |
761 | * @return The size of the actual value that matches the specified | |
762 | * {@code key} in byte. If the return value is greater than the | |
763 | * length of {@code value}, then it indicates that the size of the | |
764 | * input buffer {@code value} is insufficient and partial result will | |
765 | * be returned. RocksDB.NOT_FOUND will be returned if the value not | |
766 | * found. | |
767 | * | |
768 | * @throws RocksDBException thrown if error happens in underlying | |
769 | * native library. | |
770 | */ | |
771 | public int get(final ColumnFamilyHandle columnFamilyHandle, | |
772 | final ReadOptions opt, final byte[] key, final byte[] value) | |
773 | throws RocksDBException { | |
774 | return get(nativeHandle_, opt.nativeHandle_, key, 0, key.length, value, | |
775 | 0, value.length, columnFamilyHandle.nativeHandle_); | |
776 | } | |
777 | ||
778 | /** | |
779 | * The simplified version of get which returns a new byte array storing | |
780 | * the value associated with the specified input key if any. null will be | |
781 | * returned if the specified key is not found. | |
782 | * | |
783 | * @param key the key retrieve the value. | |
784 | * @return a byte array storing the value associated with the input key if | |
785 | * any. null if it does not find the specified key. | |
786 | * | |
787 | * @throws RocksDBException thrown if error happens in underlying | |
788 | * native library. | |
789 | */ | |
790 | public byte[] get(final byte[] key) throws RocksDBException { | |
791 | return get(nativeHandle_, key, 0, key.length); | |
792 | } | |
793 | ||
794 | /** | |
795 | * The simplified version of get which returns a new byte array storing | |
796 | * the value associated with the specified input key if any. null will be | |
797 | * returned if the specified key is not found. | |
798 | * | |
799 | * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} | |
800 | * instance | |
801 | * @param key the key retrieve the value. | |
802 | * @return a byte array storing the value associated with the input key if | |
803 | * any. null if it does not find the specified key. | |
804 | * | |
805 | * @throws RocksDBException thrown if error happens in underlying | |
806 | * native library. | |
807 | */ | |
808 | public byte[] get(final ColumnFamilyHandle columnFamilyHandle, | |
809 | final byte[] key) throws RocksDBException { | |
810 | return get(nativeHandle_, key, 0, key.length, | |
811 | columnFamilyHandle.nativeHandle_); | |
812 | } | |
813 | ||
814 | /** | |
815 | * The simplified version of get which returns a new byte array storing | |
816 | * the value associated with the specified input key if any. null will be | |
817 | * returned if the specified key is not found. | |
818 | * | |
819 | * @param key the key retrieve the value. | |
820 | * @param opt Read options. | |
821 | * @return a byte array storing the value associated with the input key if | |
822 | * any. null if it does not find the specified key. | |
823 | * | |
824 | * @throws RocksDBException thrown if error happens in underlying | |
825 | * native library. | |
826 | */ | |
827 | public byte[] get(final ReadOptions opt, final byte[] key) | |
828 | throws RocksDBException { | |
829 | return get(nativeHandle_, opt.nativeHandle_, key, 0, key.length); | |
830 | } | |
831 | ||
832 | /** | |
833 | * The simplified version of get which returns a new byte array storing | |
834 | * the value associated with the specified input key if any. null will be | |
835 | * returned if the specified key is not found. | |
836 | * | |
837 | * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} | |
838 | * instance | |
839 | * @param key the key retrieve the value. | |
840 | * @param opt Read options. | |
841 | * @return a byte array storing the value associated with the input key if | |
842 | * any. null if it does not find the specified key. | |
843 | * | |
844 | * @throws RocksDBException thrown if error happens in underlying | |
845 | * native library. | |
846 | */ | |
847 | public byte[] get(final ColumnFamilyHandle columnFamilyHandle, | |
848 | final ReadOptions opt, final byte[] key) throws RocksDBException { | |
849 | return get(nativeHandle_, opt.nativeHandle_, key, 0, key.length, | |
850 | columnFamilyHandle.nativeHandle_); | |
851 | } | |
852 | ||
853 | /** | |
854 | * Returns a map of keys for which values were found in DB. | |
855 | * | |
856 | * @param keys List of keys for which values need to be retrieved. | |
857 | * @return Map where key of map is the key passed by user and value for map | |
858 | * entry is the corresponding value in DB. | |
859 | * | |
860 | * @throws RocksDBException thrown if error happens in underlying | |
861 | * native library. | |
862 | */ | |
863 | public Map<byte[], byte[]> multiGet(final List<byte[]> keys) | |
864 | throws RocksDBException { | |
865 | assert(keys.size() != 0); | |
866 | ||
867 | final byte[][] keysArray = keys.toArray(new byte[keys.size()][]); | |
868 | final int keyOffsets[] = new int[keysArray.length]; | |
869 | final int keyLengths[] = new int[keysArray.length]; | |
870 | for(int i = 0; i < keyLengths.length; i++) { | |
871 | keyLengths[i] = keysArray[i].length; | |
872 | } | |
873 | ||
874 | final byte[][] values = multiGet(nativeHandle_, keysArray, keyOffsets, | |
875 | keyLengths); | |
876 | ||
877 | final Map<byte[], byte[]> keyValueMap = | |
878 | new HashMap<>(computeCapacityHint(values.length)); | |
879 | for(int i = 0; i < values.length; i++) { | |
880 | if(values[i] == null) { | |
881 | continue; | |
882 | } | |
883 | ||
884 | keyValueMap.put(keys.get(i), values[i]); | |
885 | } | |
886 | ||
887 | return keyValueMap; | |
888 | } | |
889 | ||
890 | private static int computeCapacityHint(final int estimatedNumberOfItems) { | |
891 | // Default load factor for HashMap is 0.75, so N * 1.5 will be at the load | |
892 | // limit. We add +1 for a buffer. | |
893 | return (int)Math.ceil(estimatedNumberOfItems * 1.5 + 1.0); | |
894 | } | |
895 | ||
896 | /** | |
897 | * Returns a map of keys for which values were found in DB. | |
898 | * <p> | |
899 | * Note: Every key needs to have a related column family name in | |
900 | * {@code columnFamilyHandleList}. | |
901 | * </p> | |
902 | * | |
903 | * @param columnFamilyHandleList {@link java.util.List} containing | |
904 | * {@link org.rocksdb.ColumnFamilyHandle} instances. | |
905 | * @param keys List of keys for which values need to be retrieved. | |
906 | * @return Map where key of map is the key passed by user and value for map | |
907 | * entry is the corresponding value in DB. | |
908 | * | |
909 | * @throws RocksDBException thrown if error happens in underlying | |
910 | * native library. | |
911 | * @throws IllegalArgumentException thrown if the size of passed keys is not | |
912 | * equal to the amount of passed column family handles. | |
913 | */ | |
914 | public Map<byte[], byte[]> multiGet( | |
915 | final List<ColumnFamilyHandle> columnFamilyHandleList, | |
916 | final List<byte[]> keys) throws RocksDBException, | |
917 | IllegalArgumentException { | |
918 | assert(keys.size() != 0); | |
919 | // Check if key size equals cfList size. If not a exception must be | |
920 | // thrown. If not a Segmentation fault happens. | |
921 | if (keys.size() != columnFamilyHandleList.size()) { | |
922 | throw new IllegalArgumentException( | |
923 | "For each key there must be a ColumnFamilyHandle."); | |
924 | } | |
925 | final long[] cfHandles = new long[columnFamilyHandleList.size()]; | |
926 | for (int i = 0; i < columnFamilyHandleList.size(); i++) { | |
927 | cfHandles[i] = columnFamilyHandleList.get(i).nativeHandle_; | |
928 | } | |
929 | ||
930 | final byte[][] keysArray = keys.toArray(new byte[keys.size()][]); | |
931 | final int keyOffsets[] = new int[keysArray.length]; | |
932 | final int keyLengths[] = new int[keysArray.length]; | |
933 | for(int i = 0; i < keyLengths.length; i++) { | |
934 | keyLengths[i] = keysArray[i].length; | |
935 | } | |
936 | ||
937 | final byte[][] values = multiGet(nativeHandle_, keysArray, keyOffsets, | |
938 | keyLengths, cfHandles); | |
939 | ||
940 | final Map<byte[], byte[]> keyValueMap = | |
941 | new HashMap<>(computeCapacityHint(values.length)); | |
942 | for(int i = 0; i < values.length; i++) { | |
943 | if (values[i] == null) { | |
944 | continue; | |
945 | } | |
946 | keyValueMap.put(keys.get(i), values[i]); | |
947 | } | |
948 | return keyValueMap; | |
949 | } | |
950 | ||
951 | /** | |
952 | * Returns a map of keys for which values were found in DB. | |
953 | * | |
954 | * @param opt Read options. | |
955 | * @param keys of keys for which values need to be retrieved. | |
956 | * @return Map where key of map is the key passed by user and value for map | |
957 | * entry is the corresponding value in DB. | |
958 | * | |
959 | * @throws RocksDBException thrown if error happens in underlying | |
960 | * native library. | |
961 | */ | |
962 | public Map<byte[], byte[]> multiGet(final ReadOptions opt, | |
963 | final List<byte[]> keys) throws RocksDBException { | |
964 | assert(keys.size() != 0); | |
965 | ||
966 | final byte[][] keysArray = keys.toArray(new byte[keys.size()][]); | |
967 | final int keyOffsets[] = new int[keysArray.length]; | |
968 | final int keyLengths[] = new int[keysArray.length]; | |
969 | for(int i = 0; i < keyLengths.length; i++) { | |
970 | keyLengths[i] = keysArray[i].length; | |
971 | } | |
972 | ||
973 | final byte[][] values = multiGet(nativeHandle_, opt.nativeHandle_, | |
974 | keysArray, keyOffsets, keyLengths); | |
975 | ||
976 | final Map<byte[], byte[]> keyValueMap = | |
977 | new HashMap<>(computeCapacityHint(values.length)); | |
978 | for(int i = 0; i < values.length; i++) { | |
979 | if(values[i] == null) { | |
980 | continue; | |
981 | } | |
982 | ||
983 | keyValueMap.put(keys.get(i), values[i]); | |
984 | } | |
985 | ||
986 | return keyValueMap; | |
987 | } | |
988 | ||
989 | /** | |
990 | * Returns a map of keys for which values were found in DB. | |
991 | * <p> | |
992 | * Note: Every key needs to have a related column family name in | |
993 | * {@code columnFamilyHandleList}. | |
994 | * </p> | |
995 | * | |
996 | * @param opt Read options. | |
997 | * @param columnFamilyHandleList {@link java.util.List} containing | |
998 | * {@link org.rocksdb.ColumnFamilyHandle} instances. | |
999 | * @param keys of keys for which values need to be retrieved. | |
1000 | * @return Map where key of map is the key passed by user and value for map | |
1001 | * entry is the corresponding value in DB. | |
1002 | * | |
1003 | * @throws RocksDBException thrown if error happens in underlying | |
1004 | * native library. | |
1005 | * @throws IllegalArgumentException thrown if the size of passed keys is not | |
1006 | * equal to the amount of passed column family handles. | |
1007 | */ | |
1008 | public Map<byte[], byte[]> multiGet(final ReadOptions opt, | |
1009 | final List<ColumnFamilyHandle> columnFamilyHandleList, | |
1010 | final List<byte[]> keys) throws RocksDBException { | |
1011 | assert(keys.size() != 0); | |
1012 | // Check if key size equals cfList size. If not a exception must be | |
1013 | // thrown. If not a Segmentation fault happens. | |
1014 | if (keys.size()!=columnFamilyHandleList.size()){ | |
1015 | throw new IllegalArgumentException( | |
1016 | "For each key there must be a ColumnFamilyHandle."); | |
1017 | } | |
1018 | final long[] cfHandles = new long[columnFamilyHandleList.size()]; | |
1019 | for (int i = 0; i < columnFamilyHandleList.size(); i++) { | |
1020 | cfHandles[i] = columnFamilyHandleList.get(i).nativeHandle_; | |
1021 | } | |
1022 | ||
1023 | final byte[][] keysArray = keys.toArray(new byte[keys.size()][]); | |
1024 | final int keyOffsets[] = new int[keysArray.length]; | |
1025 | final int keyLengths[] = new int[keysArray.length]; | |
1026 | for(int i = 0; i < keyLengths.length; i++) { | |
1027 | keyLengths[i] = keysArray[i].length; | |
1028 | } | |
1029 | ||
1030 | final byte[][] values = multiGet(nativeHandle_, opt.nativeHandle_, | |
1031 | keysArray, keyOffsets, keyLengths, cfHandles); | |
1032 | ||
1033 | final Map<byte[], byte[]> keyValueMap | |
1034 | = new HashMap<>(computeCapacityHint(values.length)); | |
1035 | for(int i = 0; i < values.length; i++) { | |
1036 | if(values[i] == null) { | |
1037 | continue; | |
1038 | } | |
1039 | keyValueMap.put(keys.get(i), values[i]); | |
1040 | } | |
1041 | ||
1042 | return keyValueMap; | |
1043 | } | |
1044 | ||
1045 | /** | |
1046 | * Remove the database entry (if any) for "key". Returns OK on | |
1047 | * success, and a non-OK status on error. It is not an error if "key" | |
1048 | * did not exist in the database. | |
1049 | * | |
1050 | * @param key Key to delete within database | |
1051 | * | |
1052 | * @throws RocksDBException thrown if error happens in underlying | |
1053 | * native library. | |
1054 | * | |
1055 | * @deprecated Use {@link #delete(byte[])} | |
1056 | */ | |
1057 | @Deprecated | |
1058 | public void remove(final byte[] key) throws RocksDBException { | |
1059 | delete(key); | |
1060 | } | |
1061 | ||
1062 | /** | |
1063 | * Delete the database entry (if any) for "key". Returns OK on | |
1064 | * success, and a non-OK status on error. It is not an error if "key" | |
1065 | * did not exist in the database. | |
1066 | * | |
1067 | * @param key Key to delete within database | |
1068 | * | |
1069 | * @throws RocksDBException thrown if error happens in underlying | |
1070 | * native library. | |
1071 | */ | |
1072 | public void delete(final byte[] key) throws RocksDBException { | |
1073 | delete(nativeHandle_, key, 0, key.length); | |
1074 | } | |
1075 | ||
1076 | /** | |
1077 | * Remove the database entry (if any) for "key". Returns OK on | |
1078 | * success, and a non-OK status on error. It is not an error if "key" | |
1079 | * did not exist in the database. | |
1080 | * | |
1081 | * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} | |
1082 | * instance | |
1083 | * @param key Key to delete within database | |
1084 | * | |
1085 | * @throws RocksDBException thrown if error happens in underlying | |
1086 | * native library. | |
1087 | * | |
1088 | * @deprecated Use {@link #delete(ColumnFamilyHandle, byte[])} | |
1089 | */ | |
1090 | @Deprecated | |
1091 | public void remove(final ColumnFamilyHandle columnFamilyHandle, | |
1092 | final byte[] key) throws RocksDBException { | |
1093 | delete(columnFamilyHandle, key); | |
1094 | } | |
1095 | ||
1096 | /** | |
1097 | * Delete the database entry (if any) for "key". Returns OK on | |
1098 | * success, and a non-OK status on error. It is not an error if "key" | |
1099 | * did not exist in the database. | |
1100 | * | |
1101 | * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} | |
1102 | * instance | |
1103 | * @param key Key to delete within database | |
1104 | * | |
1105 | * @throws RocksDBException thrown if error happens in underlying | |
1106 | * native library. | |
1107 | */ | |
1108 | public void delete(final ColumnFamilyHandle columnFamilyHandle, | |
1109 | final byte[] key) throws RocksDBException { | |
1110 | delete(nativeHandle_, key, 0, key.length, columnFamilyHandle.nativeHandle_); | |
1111 | } | |
1112 | ||
1113 | /** | |
1114 | * Remove 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. | |
1117 | * | |
1118 | * @param writeOpt WriteOptions to be used with delete operation | |
1119 | * @param key Key to delete within database | |
1120 | * | |
1121 | * @throws RocksDBException thrown if error happens in underlying | |
1122 | * native library. | |
1123 | * | |
1124 | * @deprecated Use {@link #delete(WriteOptions, byte[])} | |
1125 | */ | |
1126 | @Deprecated | |
1127 | public void remove(final WriteOptions writeOpt, final byte[] key) | |
1128 | throws RocksDBException { | |
1129 | delete(writeOpt, key); | |
1130 | } | |
1131 | ||
1132 | /** | |
1133 | * Delete the database entry (if any) for "key". Returns OK on | |
1134 | * success, and a non-OK status on error. It is not an error if "key" | |
1135 | * did not exist in the database. | |
1136 | * | |
1137 | * @param writeOpt WriteOptions to be used with delete operation | |
1138 | * @param key Key to delete within database | |
1139 | * | |
1140 | * @throws RocksDBException thrown if error happens in underlying | |
1141 | * native library. | |
1142 | */ | |
1143 | public void delete(final WriteOptions writeOpt, final byte[] key) | |
1144 | throws RocksDBException { | |
1145 | delete(nativeHandle_, writeOpt.nativeHandle_, key, 0, key.length); | |
1146 | } | |
1147 | ||
1148 | /** | |
1149 | * Remove the database entry (if any) for "key". Returns OK on | |
1150 | * success, and a non-OK status on error. It is not an error if "key" | |
1151 | * did not exist in the database. | |
1152 | * | |
1153 | * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} | |
1154 | * instance | |
1155 | * @param writeOpt WriteOptions to be used with delete operation | |
1156 | * @param key Key to delete within database | |
1157 | * | |
1158 | * @throws RocksDBException thrown if error happens in underlying | |
1159 | * native library. | |
1160 | * | |
1161 | * @deprecated Use {@link #delete(ColumnFamilyHandle, WriteOptions, byte[])} | |
1162 | */ | |
1163 | @Deprecated | |
1164 | public void remove(final ColumnFamilyHandle columnFamilyHandle, | |
1165 | final WriteOptions writeOpt, final byte[] key) | |
1166 | throws RocksDBException { | |
1167 | delete(columnFamilyHandle, writeOpt, key); | |
1168 | } | |
1169 | ||
1170 | /** | |
1171 | * Delete the database entry (if any) for "key". Returns OK on | |
1172 | * success, and a non-OK status on error. It is not an error if "key" | |
1173 | * did not exist in the database. | |
1174 | * | |
1175 | * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} | |
1176 | * instance | |
1177 | * @param writeOpt WriteOptions to be used with delete operation | |
1178 | * @param key Key to delete within database | |
1179 | * | |
1180 | * @throws RocksDBException thrown if error happens in underlying | |
1181 | * native library. | |
1182 | */ | |
1183 | public void delete(final ColumnFamilyHandle columnFamilyHandle, | |
1184 | final WriteOptions writeOpt, final byte[] key) | |
1185 | throws RocksDBException { | |
1186 | delete(nativeHandle_, writeOpt.nativeHandle_, key, 0, key.length, | |
1187 | columnFamilyHandle.nativeHandle_); | |
1188 | } | |
1189 | ||
1190 | /** | |
1191 | * Remove the database entry for {@code key}. Requires that the key exists | |
1192 | * and was not overwritten. It is not an error if the key did not exist | |
1193 | * in the database. | |
1194 | * | |
1195 | * If a key is overwritten (by calling {@link #put(byte[], byte[])} multiple | |
1196 | * times), then the result of calling SingleDelete() on this key is undefined. | |
1197 | * SingleDelete() only behaves correctly if there has been only one Put() | |
1198 | * for this key since the previous call to SingleDelete() for this key. | |
1199 | * | |
1200 | * This feature is currently an experimental performance optimization | |
1201 | * for a very specific workload. It is up to the caller to ensure that | |
1202 | * SingleDelete is only used for a key that is not deleted using Delete() or | |
1203 | * written using Merge(). Mixing SingleDelete operations with Deletes and | |
1204 | * Merges can result in undefined behavior. | |
1205 | * | |
1206 | * @param key Key to delete within database | |
1207 | * | |
1208 | * @throws RocksDBException thrown if error happens in underlying | |
1209 | * native library. | |
1210 | */ | |
1211 | @Experimental("Performance optimization for a very specific workload") | |
1212 | public void singleDelete(final byte[] key) throws RocksDBException { | |
1213 | singleDelete(nativeHandle_, key, key.length); | |
1214 | } | |
1215 | ||
1216 | /** | |
1217 | * Remove the database entry for {@code key}. Requires that the key exists | |
1218 | * and was not overwritten. It is not an error if the key did not exist | |
1219 | * in the database. | |
1220 | * | |
1221 | * If a key is overwritten (by calling {@link #put(byte[], byte[])} multiple | |
1222 | * times), then the result of calling SingleDelete() on this key is undefined. | |
1223 | * SingleDelete() only behaves correctly if there has been only one Put() | |
1224 | * for this key since the previous call to SingleDelete() for this key. | |
1225 | * | |
1226 | * This feature is currently an experimental performance optimization | |
1227 | * for a very specific workload. It is up to the caller to ensure that | |
1228 | * SingleDelete is only used for a key that is not deleted using Delete() or | |
1229 | * written using Merge(). Mixing SingleDelete operations with Deletes and | |
1230 | * Merges can result in undefined behavior. | |
1231 | * | |
1232 | * @param columnFamilyHandle The column family to delete the key from | |
1233 | * @param key Key to delete within database | |
1234 | * | |
1235 | * @throws RocksDBException thrown if error happens in underlying | |
1236 | * native library. | |
1237 | */ | |
1238 | @Experimental("Performance optimization for a very specific workload") | |
1239 | public void singleDelete(final ColumnFamilyHandle columnFamilyHandle, | |
1240 | final byte[] key) throws RocksDBException { | |
1241 | singleDelete(nativeHandle_, key, key.length, | |
1242 | columnFamilyHandle.nativeHandle_); | |
1243 | } | |
1244 | ||
1245 | /** | |
1246 | * Remove the database entry for {@code key}. Requires that the key exists | |
1247 | * and was not overwritten. It is not an error if the key did not exist | |
1248 | * in the database. | |
1249 | * | |
1250 | * If a key is overwritten (by calling {@link #put(byte[], byte[])} multiple | |
1251 | * times), then the result of calling SingleDelete() on this key is undefined. | |
1252 | * SingleDelete() only behaves correctly if there has been only one Put() | |
1253 | * for this key since the previous call to SingleDelete() for this key. | |
1254 | * | |
1255 | * This feature is currently an experimental performance optimization | |
1256 | * for a very specific workload. It is up to the caller to ensure that | |
1257 | * SingleDelete is only used for a key that is not deleted using Delete() or | |
1258 | * written using Merge(). Mixing SingleDelete operations with Deletes and | |
1259 | * Merges can result in undefined behavior. | |
1260 | * | |
1261 | * Note: consider setting {@link WriteOptions#setSync(boolean)} true. | |
1262 | * | |
1263 | * @param writeOpt Write options for the delete | |
1264 | * @param key Key to delete within database | |
1265 | * | |
1266 | * @throws RocksDBException thrown if error happens in underlying | |
1267 | * native library. | |
1268 | */ | |
1269 | @Experimental("Performance optimization for a very specific workload") | |
1270 | public void singleDelete(final WriteOptions writeOpt, final byte[] key) | |
1271 | throws RocksDBException { | |
1272 | singleDelete(nativeHandle_, writeOpt.nativeHandle_, key, key.length); | |
1273 | } | |
1274 | ||
1275 | /** | |
1276 | * Remove the database entry for {@code key}. Requires that the key exists | |
1277 | * and was not overwritten. It is not an error if the key did not exist | |
1278 | * in the database. | |
1279 | * | |
1280 | * If a key is overwritten (by calling {@link #put(byte[], byte[])} multiple | |
1281 | * times), then the result of calling SingleDelete() on this key is undefined. | |
1282 | * SingleDelete() only behaves correctly if there has been only one Put() | |
1283 | * for this key since the previous call to SingleDelete() for this key. | |
1284 | * | |
1285 | * This feature is currently an experimental performance optimization | |
1286 | * for a very specific workload. It is up to the caller to ensure that | |
1287 | * SingleDelete is only used for a key that is not deleted using Delete() or | |
1288 | * written using Merge(). Mixing SingleDelete operations with Deletes and | |
1289 | * Merges can result in undefined behavior. | |
1290 | * | |
1291 | * Note: consider setting {@link WriteOptions#setSync(boolean)} true. | |
1292 | * | |
1293 | * @param columnFamilyHandle The column family to delete the key from | |
1294 | * @param writeOpt Write options for the delete | |
1295 | * @param key Key to delete within database | |
1296 | * | |
1297 | * @throws RocksDBException thrown if error happens in underlying | |
1298 | * native library. | |
1299 | */ | |
1300 | @Experimental("Performance optimization for a very specific workload") | |
1301 | public void singleDelete(final ColumnFamilyHandle columnFamilyHandle, | |
1302 | final WriteOptions writeOpt, final byte[] key) throws RocksDBException { | |
1303 | singleDelete(nativeHandle_, writeOpt.nativeHandle_, key, key.length, | |
1304 | columnFamilyHandle.nativeHandle_); | |
1305 | } | |
1306 | ||
1307 | /** | |
1308 | * DB implements can export properties about their state | |
1309 | * via this method on a per column family level. | |
1310 | * | |
1311 | * <p>If {@code property} is a valid property understood by this DB | |
1312 | * implementation, fills {@code value} with its current value and | |
1313 | * returns true. Otherwise returns false.</p> | |
1314 | * | |
1315 | * <p>Valid property names include: | |
1316 | * <ul> | |
1317 | * <li>"rocksdb.num-files-at-level<N>" - return the number of files at | |
1318 | * level <N>, where <N> is an ASCII representation of a level | |
1319 | * number (e.g. "0").</li> | |
1320 | * <li>"rocksdb.stats" - returns a multi-line string that describes statistics | |
1321 | * about the internal operation of the DB.</li> | |
1322 | * <li>"rocksdb.sstables" - returns a multi-line string that describes all | |
1323 | * of the sstables that make up the db contents.</li> | |
1324 | * </ul> | |
1325 | * | |
1326 | * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} | |
1327 | * instance | |
1328 | * @param property to be fetched. See above for examples | |
1329 | * @return property value | |
1330 | * | |
1331 | * @throws RocksDBException thrown if error happens in underlying | |
1332 | * native library. | |
1333 | */ | |
1334 | public String getProperty(final ColumnFamilyHandle columnFamilyHandle, | |
1335 | final String property) throws RocksDBException { | |
1336 | return getProperty0(nativeHandle_, columnFamilyHandle.nativeHandle_, | |
1337 | property, property.length()); | |
1338 | } | |
1339 | ||
1340 | /** | |
1341 | * Removes the database entries in the range ["beginKey", "endKey"), i.e., | |
1342 | * including "beginKey" and excluding "endKey". a non-OK status on error. It | |
1343 | * is not an error if no keys exist in the range ["beginKey", "endKey"). | |
1344 | * | |
1345 | * Delete the database entry (if any) for "key". Returns OK on success, and a | |
1346 | * non-OK status on error. It is not an error if "key" did not exist in the | |
1347 | * database. | |
1348 | * | |
1349 | * @param beginKey | |
1350 | * First key to delete within database (included) | |
1351 | * @param endKey | |
1352 | * Last key to delete within database (excluded) | |
1353 | * | |
1354 | * @throws RocksDBException | |
1355 | * thrown if error happens in underlying native library. | |
1356 | */ | |
1357 | public void deleteRange(final byte[] beginKey, final byte[] endKey) throws RocksDBException { | |
1358 | deleteRange(nativeHandle_, beginKey, 0, beginKey.length, endKey, 0, endKey.length); | |
1359 | } | |
1360 | ||
1361 | /** | |
1362 | * Removes the database entries in the range ["beginKey", "endKey"), i.e., | |
1363 | * including "beginKey" and excluding "endKey". a non-OK status on error. It | |
1364 | * is not an error if no keys exist in the range ["beginKey", "endKey"). | |
1365 | * | |
1366 | * Delete the database entry (if any) for "key". Returns OK on success, and a | |
1367 | * non-OK status on error. It is not an error if "key" did not exist in the | |
1368 | * database. | |
1369 | * | |
1370 | * @param columnFamilyHandle | |
1371 | * {@link org.rocksdb.ColumnFamilyHandle} instance | |
1372 | * @param beginKey | |
1373 | * First key to delete within database (included) | |
1374 | * @param endKey | |
1375 | * Last key to delete within database (excluded) | |
1376 | * | |
1377 | * @throws RocksDBException | |
1378 | * thrown if error happens in underlying native library. | |
1379 | */ | |
1380 | public void deleteRange(final ColumnFamilyHandle columnFamilyHandle, final byte[] beginKey, | |
1381 | final byte[] endKey) throws RocksDBException { | |
1382 | deleteRange(nativeHandle_, beginKey, 0, beginKey.length, endKey, 0, endKey.length, | |
1383 | columnFamilyHandle.nativeHandle_); | |
1384 | } | |
1385 | ||
1386 | /** | |
1387 | * Removes the database entries in the range ["beginKey", "endKey"), i.e., | |
1388 | * including "beginKey" and excluding "endKey". a non-OK status on error. It | |
1389 | * is not an error if no keys exist in the range ["beginKey", "endKey"). | |
1390 | * | |
1391 | * Delete the database entry (if any) for "key". Returns OK on success, and a | |
1392 | * non-OK status on error. It is not an error if "key" did not exist in the | |
1393 | * database. | |
1394 | * | |
1395 | * @param writeOpt | |
1396 | * WriteOptions to be used with delete operation | |
1397 | * @param beginKey | |
1398 | * First key to delete within database (included) | |
1399 | * @param endKey | |
1400 | * Last key to delete within database (excluded) | |
1401 | * | |
1402 | * @throws RocksDBException | |
1403 | * thrown if error happens in underlying native library. | |
1404 | */ | |
1405 | public void deleteRange(final WriteOptions writeOpt, final byte[] beginKey, final byte[] endKey) | |
1406 | throws RocksDBException { | |
1407 | deleteRange(nativeHandle_, writeOpt.nativeHandle_, beginKey, 0, beginKey.length, endKey, 0, | |
1408 | endKey.length); | |
1409 | } | |
1410 | ||
1411 | /** | |
1412 | * Removes the database entries in the range ["beginKey", "endKey"), i.e., | |
1413 | * including "beginKey" and excluding "endKey". a non-OK status on error. It | |
1414 | * is not an error if no keys exist in the range ["beginKey", "endKey"). | |
1415 | * | |
1416 | * Delete the database entry (if any) for "key". Returns OK on success, and a | |
1417 | * non-OK status on error. It is not an error if "key" did not exist in the | |
1418 | * database. | |
1419 | * | |
1420 | * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} | |
1421 | * instance | |
1422 | * @param writeOpt | |
1423 | * WriteOptions to be used with delete operation | |
1424 | * @param beginKey | |
1425 | * First key to delete within database (included) | |
1426 | * @param endKey | |
1427 | * Last key to delete within database (excluded) | |
1428 | * | |
1429 | * @throws RocksDBException | |
1430 | * thrown if error happens in underlying native library. | |
1431 | */ | |
1432 | public void deleteRange(final ColumnFamilyHandle columnFamilyHandle, final WriteOptions writeOpt, | |
1433 | final byte[] beginKey, final byte[] endKey) throws RocksDBException { | |
1434 | deleteRange(nativeHandle_, writeOpt.nativeHandle_, beginKey, 0, beginKey.length, endKey, 0, | |
1435 | endKey.length, columnFamilyHandle.nativeHandle_); | |
1436 | } | |
1437 | ||
1438 | /** | |
1439 | * DB implementations can export properties about their state | |
1440 | * via this method. If "property" is a valid property understood by this | |
1441 | * DB implementation, fills "*value" with its current value and returns | |
1442 | * true. Otherwise returns false. | |
1443 | * | |
1444 | * <p>Valid property names include: | |
1445 | * <ul> | |
1446 | * <li>"rocksdb.num-files-at-level<N>" - return the number of files at | |
1447 | * level <N>, where <N> is an ASCII representation of a level | |
1448 | * number (e.g. "0").</li> | |
1449 | * <li>"rocksdb.stats" - returns a multi-line string that describes statistics | |
1450 | * about the internal operation of the DB.</li> | |
1451 | * <li>"rocksdb.sstables" - returns a multi-line string that describes all | |
1452 | * of the sstables that make up the db contents.</li> | |
1453 | *</ul> | |
1454 | * | |
1455 | * @param property to be fetched. See above for examples | |
1456 | * @return property value | |
1457 | * | |
1458 | * @throws RocksDBException thrown if error happens in underlying | |
1459 | * native library. | |
1460 | */ | |
1461 | public String getProperty(final String property) throws RocksDBException { | |
1462 | return getProperty0(nativeHandle_, property, property.length()); | |
1463 | } | |
1464 | ||
1465 | /** | |
1466 | * <p> Similar to GetProperty(), but only works for a subset of properties | |
1467 | * whose return value is a numerical value. Return the value as long.</p> | |
1468 | * | |
1469 | * <p><strong>Note</strong>: As the returned property is of type | |
1470 | * {@code uint64_t} on C++ side the returning value can be negative | |
1471 | * because Java supports in Java 7 only signed long values.</p> | |
1472 | * | |
1473 | * <p><strong>Java 7</strong>: To mitigate the problem of the non | |
1474 | * existent unsigned long tpye, values should be encapsulated using | |
1475 | * {@link java.math.BigInteger} to reflect the correct value. The correct | |
1476 | * behavior is guaranteed if {@code 2^64} is added to negative values.</p> | |
1477 | * | |
1478 | * <p><strong>Java 8</strong>: In Java 8 the value should be treated as | |
1479 | * unsigned long using provided methods of type {@link Long}.</p> | |
1480 | * | |
1481 | * @param property to be fetched. | |
1482 | * | |
1483 | * @return numerical property value. | |
1484 | * | |
1485 | * @throws RocksDBException if an error happens in the underlying native code. | |
1486 | */ | |
1487 | public long getLongProperty(final String property) throws RocksDBException { | |
1488 | return getLongProperty(nativeHandle_, property, property.length()); | |
1489 | } | |
1490 | ||
1491 | /** | |
1492 | * <p> Similar to GetProperty(), but only works for a subset of properties | |
1493 | * whose return value is a numerical value. Return the value as long.</p> | |
1494 | * | |
1495 | * <p><strong>Note</strong>: As the returned property is of type | |
1496 | * {@code uint64_t} on C++ side the returning value can be negative | |
1497 | * because Java supports in Java 7 only signed long values.</p> | |
1498 | * | |
1499 | * <p><strong>Java 7</strong>: To mitigate the problem of the non | |
1500 | * existent unsigned long tpye, values should be encapsulated using | |
1501 | * {@link java.math.BigInteger} to reflect the correct value. The correct | |
1502 | * behavior is guaranteed if {@code 2^64} is added to negative values.</p> | |
1503 | * | |
1504 | * <p><strong>Java 8</strong>: In Java 8 the value should be treated as | |
1505 | * unsigned long using provided methods of type {@link Long}.</p> | |
1506 | * | |
1507 | * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} | |
1508 | * instance | |
1509 | * @param property to be fetched. | |
1510 | * | |
1511 | * @return numerical property value | |
1512 | * | |
1513 | * @throws RocksDBException if an error happens in the underlying native code. | |
1514 | */ | |
1515 | public long getLongProperty(final ColumnFamilyHandle columnFamilyHandle, | |
1516 | final String property) throws RocksDBException { | |
1517 | return getLongProperty(nativeHandle_, columnFamilyHandle.nativeHandle_, | |
1518 | property, property.length()); | |
1519 | } | |
1520 | ||
11fdf7f2 TL |
1521 | /** |
1522 | * <p> Return sum of the getLongProperty of all the column families</p> | |
1523 | * | |
1524 | * <p><strong>Note</strong>: As the returned property is of type | |
1525 | * {@code uint64_t} on C++ side the returning value can be negative | |
1526 | * because Java supports in Java 7 only signed long values.</p> | |
1527 | * | |
1528 | * <p><strong>Java 7</strong>: To mitigate the problem of the non | |
1529 | * existent unsigned long tpye, values should be encapsulated using | |
1530 | * {@link java.math.BigInteger} to reflect the correct value. The correct | |
1531 | * behavior is guaranteed if {@code 2^64} is added to negative values.</p> | |
1532 | * | |
1533 | * <p><strong>Java 8</strong>: In Java 8 the value should be treated as | |
1534 | * unsigned long using provided methods of type {@link Long}.</p> | |
1535 | * | |
1536 | * @param property to be fetched. | |
1537 | * | |
1538 | * @return numerical property value | |
1539 | * | |
1540 | * @throws RocksDBException if an error happens in the underlying native code. | |
1541 | */ | |
1542 | public long getAggregatedLongProperty(final String property) throws RocksDBException { | |
1543 | return getAggregatedLongProperty(nativeHandle_, property, property.length()); | |
1544 | } | |
1545 | ||
7c673cae FG |
1546 | /** |
1547 | * <p>Return a heap-allocated iterator over the contents of the | |
1548 | * database. The result of newIterator() is initially invalid | |
1549 | * (caller must call one of the Seek methods on the iterator | |
1550 | * before using it).</p> | |
1551 | * | |
1552 | * <p>Caller should close the iterator when it is no longer needed. | |
1553 | * The returned iterator should be closed before this db is closed. | |
1554 | * </p> | |
1555 | * | |
1556 | * @return instance of iterator object. | |
1557 | */ | |
1558 | public RocksIterator newIterator() { | |
1559 | return new RocksIterator(this, iterator(nativeHandle_)); | |
1560 | } | |
1561 | ||
1562 | /** | |
1563 | * <p>Return a heap-allocated iterator over the contents of the | |
1564 | * database. The result of newIterator() is initially invalid | |
1565 | * (caller must call one of the Seek methods on the iterator | |
1566 | * before using it).</p> | |
1567 | * | |
1568 | * <p>Caller should close the iterator when it is no longer needed. | |
1569 | * The returned iterator should be closed before this db is closed. | |
1570 | * </p> | |
1571 | * | |
1572 | * @param readOptions {@link ReadOptions} instance. | |
1573 | * @return instance of iterator object. | |
1574 | */ | |
1575 | public RocksIterator newIterator(final ReadOptions readOptions) { | |
1576 | return new RocksIterator(this, iterator(nativeHandle_, | |
1577 | readOptions.nativeHandle_)); | |
1578 | } | |
1579 | ||
1580 | /** | |
1581 | * <p>Return a handle to the current DB state. Iterators created with | |
1582 | * this handle will all observe a stable snapshot of the current DB | |
1583 | * state. The caller must call ReleaseSnapshot(result) when the | |
1584 | * snapshot is no longer needed.</p> | |
1585 | * | |
1586 | * <p>nullptr will be returned if the DB fails to take a snapshot or does | |
1587 | * not support snapshot.</p> | |
1588 | * | |
1589 | * @return Snapshot {@link Snapshot} instance | |
1590 | */ | |
1591 | public Snapshot getSnapshot() { | |
1592 | long snapshotHandle = getSnapshot(nativeHandle_); | |
1593 | if (snapshotHandle != 0) { | |
1594 | return new Snapshot(snapshotHandle); | |
1595 | } | |
1596 | return null; | |
1597 | } | |
1598 | ||
1599 | /** | |
1600 | * Release a previously acquired snapshot. The caller must not | |
1601 | * use "snapshot" after this call. | |
1602 | * | |
1603 | * @param snapshot {@link Snapshot} instance | |
1604 | */ | |
1605 | public void releaseSnapshot(final Snapshot snapshot) { | |
1606 | if (snapshot != null) { | |
1607 | releaseSnapshot(nativeHandle_, snapshot.nativeHandle_); | |
1608 | } | |
1609 | } | |
1610 | ||
1611 | /** | |
1612 | * <p>Return a heap-allocated iterator over the contents of the | |
1613 | * database. The result of newIterator() is initially invalid | |
1614 | * (caller must call one of the Seek methods on the iterator | |
1615 | * before using it).</p> | |
1616 | * | |
1617 | * <p>Caller should close the iterator when it is no longer needed. | |
1618 | * The returned iterator should be closed before this db is closed. | |
1619 | * </p> | |
1620 | * | |
1621 | * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} | |
1622 | * instance | |
1623 | * @return instance of iterator object. | |
1624 | */ | |
1625 | public RocksIterator newIterator( | |
1626 | final ColumnFamilyHandle columnFamilyHandle) { | |
1627 | return new RocksIterator(this, iteratorCF(nativeHandle_, | |
1628 | columnFamilyHandle.nativeHandle_)); | |
1629 | } | |
1630 | ||
1631 | /** | |
1632 | * <p>Return a heap-allocated iterator over the contents of the | |
1633 | * database. The result of newIterator() is initially invalid | |
1634 | * (caller must call one of the Seek methods on the iterator | |
1635 | * before using it).</p> | |
1636 | * | |
1637 | * <p>Caller should close the iterator when it is no longer needed. | |
1638 | * The returned iterator should be closed before this db is closed. | |
1639 | * </p> | |
1640 | * | |
1641 | * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} | |
1642 | * instance | |
1643 | * @param readOptions {@link ReadOptions} instance. | |
1644 | * @return instance of iterator object. | |
1645 | */ | |
1646 | public RocksIterator newIterator(final ColumnFamilyHandle columnFamilyHandle, | |
1647 | final ReadOptions readOptions) { | |
1648 | return new RocksIterator(this, iteratorCF(nativeHandle_, | |
1649 | columnFamilyHandle.nativeHandle_, readOptions.nativeHandle_)); | |
1650 | } | |
1651 | ||
1652 | /** | |
1653 | * Returns iterators from a consistent database state across multiple | |
1654 | * column families. Iterators are heap allocated and need to be deleted | |
1655 | * before the db is deleted | |
1656 | * | |
1657 | * @param columnFamilyHandleList {@link java.util.List} containing | |
1658 | * {@link org.rocksdb.ColumnFamilyHandle} instances. | |
1659 | * @return {@link java.util.List} containing {@link org.rocksdb.RocksIterator} | |
1660 | * instances | |
1661 | * | |
1662 | * @throws RocksDBException thrown if error happens in underlying | |
1663 | * native library. | |
1664 | */ | |
1665 | public List<RocksIterator> newIterators( | |
1666 | final List<ColumnFamilyHandle> columnFamilyHandleList) | |
1667 | throws RocksDBException { | |
1668 | return newIterators(columnFamilyHandleList, new ReadOptions()); | |
1669 | } | |
1670 | ||
1671 | /** | |
1672 | * Returns iterators from a consistent database state across multiple | |
1673 | * column families. Iterators are heap allocated and need to be deleted | |
1674 | * before the db is deleted | |
1675 | * | |
1676 | * @param columnFamilyHandleList {@link java.util.List} containing | |
1677 | * {@link org.rocksdb.ColumnFamilyHandle} instances. | |
1678 | * @param readOptions {@link ReadOptions} instance. | |
1679 | * @return {@link java.util.List} containing {@link org.rocksdb.RocksIterator} | |
1680 | * instances | |
1681 | * | |
1682 | * @throws RocksDBException thrown if error happens in underlying | |
1683 | * native library. | |
1684 | */ | |
1685 | public List<RocksIterator> newIterators( | |
1686 | final List<ColumnFamilyHandle> columnFamilyHandleList, | |
1687 | final ReadOptions readOptions) throws RocksDBException { | |
1688 | ||
1689 | final long[] columnFamilyHandles = new long[columnFamilyHandleList.size()]; | |
1690 | for (int i = 0; i < columnFamilyHandleList.size(); i++) { | |
1691 | columnFamilyHandles[i] = columnFamilyHandleList.get(i).nativeHandle_; | |
1692 | } | |
1693 | ||
1694 | final long[] iteratorRefs = iterators(nativeHandle_, columnFamilyHandles, | |
1695 | readOptions.nativeHandle_); | |
1696 | ||
1697 | final List<RocksIterator> iterators = new ArrayList<>( | |
1698 | columnFamilyHandleList.size()); | |
1699 | for (int i=0; i<columnFamilyHandleList.size(); i++){ | |
1700 | iterators.add(new RocksIterator(this, iteratorRefs[i])); | |
1701 | } | |
1702 | return iterators; | |
1703 | } | |
1704 | ||
1705 | /** | |
1706 | * Gets the handle for the default column family | |
1707 | * | |
1708 | * @return The handle of the default column family | |
1709 | */ | |
1710 | public ColumnFamilyHandle getDefaultColumnFamily() { | |
11fdf7f2 | 1711 | final ColumnFamilyHandle cfHandle = new ColumnFamilyHandle(this, |
7c673cae FG |
1712 | getDefaultColumnFamily(nativeHandle_)); |
1713 | cfHandle.disOwnNativeHandle(); | |
1714 | return cfHandle; | |
1715 | } | |
1716 | ||
1717 | /** | |
1718 | * Creates a new column family with the name columnFamilyName and | |
1719 | * allocates a ColumnFamilyHandle within an internal structure. | |
1720 | * The ColumnFamilyHandle is automatically disposed with DB disposal. | |
1721 | * | |
1722 | * @param columnFamilyDescriptor column family to be created. | |
1723 | * @return {@link org.rocksdb.ColumnFamilyHandle} instance. | |
1724 | * | |
1725 | * @throws RocksDBException thrown if error happens in underlying | |
1726 | * native library. | |
1727 | */ | |
1728 | public ColumnFamilyHandle createColumnFamily( | |
1729 | final ColumnFamilyDescriptor columnFamilyDescriptor) | |
1730 | throws RocksDBException { | |
1731 | return new ColumnFamilyHandle(this, createColumnFamily(nativeHandle_, | |
1732 | columnFamilyDescriptor.columnFamilyName(), | |
1733 | columnFamilyDescriptor.columnFamilyOptions().nativeHandle_)); | |
1734 | } | |
1735 | ||
1736 | /** | |
1737 | * Drops the column family identified by columnFamilyName. Internal | |
1738 | * handles to this column family will be disposed. If the column family | |
1739 | * is not known removal will fail. | |
1740 | * | |
1741 | * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} | |
1742 | * instance | |
1743 | * | |
1744 | * @throws RocksDBException thrown if error happens in underlying | |
1745 | * native library. | |
1746 | */ | |
1747 | public void dropColumnFamily(final ColumnFamilyHandle columnFamilyHandle) | |
1748 | throws RocksDBException, IllegalArgumentException { | |
1749 | // throws RocksDBException if something goes wrong | |
1750 | dropColumnFamily(nativeHandle_, columnFamilyHandle.nativeHandle_); | |
1751 | // After the drop the native handle is not valid anymore | |
1752 | columnFamilyHandle.disOwnNativeHandle(); | |
1753 | } | |
1754 | ||
1755 | /** | |
1756 | * <p>Flush all memory table data.</p> | |
1757 | * | |
1758 | * <p>Note: it must be ensured that the FlushOptions instance | |
1759 | * is not GC'ed before this method finishes. If the wait parameter is | |
1760 | * set to false, flush processing is asynchronous.</p> | |
1761 | * | |
1762 | * @param flushOptions {@link org.rocksdb.FlushOptions} instance. | |
1763 | * @throws RocksDBException thrown if an error occurs within the native | |
1764 | * part of the library. | |
1765 | */ | |
1766 | public void flush(final FlushOptions flushOptions) | |
1767 | throws RocksDBException { | |
1768 | flush(nativeHandle_, flushOptions.nativeHandle_); | |
1769 | } | |
1770 | ||
1771 | /** | |
1772 | * <p>Flush all memory table data.</p> | |
1773 | * | |
1774 | * <p>Note: it must be ensured that the FlushOptions instance | |
1775 | * is not GC'ed before this method finishes. If the wait parameter is | |
1776 | * set to false, flush processing is asynchronous.</p> | |
1777 | * | |
1778 | * @param flushOptions {@link org.rocksdb.FlushOptions} instance. | |
1779 | * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} instance. | |
1780 | * @throws RocksDBException thrown if an error occurs within the native | |
1781 | * part of the library. | |
1782 | */ | |
1783 | public void flush(final FlushOptions flushOptions, | |
1784 | final ColumnFamilyHandle columnFamilyHandle) throws RocksDBException { | |
1785 | flush(nativeHandle_, flushOptions.nativeHandle_, | |
1786 | columnFamilyHandle.nativeHandle_); | |
1787 | } | |
1788 | ||
1789 | /** | |
1790 | * <p>Range compaction of database.</p> | |
1791 | * <p><strong>Note</strong>: After the database has been compacted, | |
1792 | * all data will have been pushed down to the last level containing | |
1793 | * any data.</p> | |
1794 | * | |
1795 | * <p><strong>See also</strong></p> | |
1796 | * <ul> | |
1797 | * <li>{@link #compactRange(boolean, int, int)}</li> | |
1798 | * <li>{@link #compactRange(byte[], byte[])}</li> | |
1799 | * <li>{@link #compactRange(byte[], byte[], boolean, int, int)}</li> | |
1800 | * </ul> | |
1801 | * | |
1802 | * @throws RocksDBException thrown if an error occurs within the native | |
1803 | * part of the library. | |
1804 | */ | |
1805 | public void compactRange() throws RocksDBException { | |
1806 | compactRange0(nativeHandle_, false, -1, 0); | |
1807 | } | |
1808 | ||
1809 | /** | |
1810 | * <p>Range compaction of database.</p> | |
1811 | * <p><strong>Note</strong>: After the database has been compacted, | |
1812 | * all data will have been pushed down to the last level containing | |
1813 | * any data.</p> | |
1814 | * | |
1815 | * <p><strong>See also</strong></p> | |
1816 | * <ul> | |
1817 | * <li>{@link #compactRange()}</li> | |
1818 | * <li>{@link #compactRange(boolean, int, int)}</li> | |
1819 | * <li>{@link #compactRange(byte[], byte[], boolean, int, int)}</li> | |
1820 | * </ul> | |
1821 | * | |
1822 | * @param begin start of key range (included in range) | |
1823 | * @param end end of key range (excluded from range) | |
1824 | * | |
1825 | * @throws RocksDBException thrown if an error occurs within the native | |
1826 | * part of the library. | |
1827 | */ | |
1828 | public void compactRange(final byte[] begin, final byte[] end) | |
1829 | throws RocksDBException { | |
1830 | compactRange0(nativeHandle_, begin, begin.length, end, | |
1831 | end.length, false, -1, 0); | |
1832 | } | |
1833 | ||
1834 | /** | |
1835 | * <p>Range compaction of database.</p> | |
1836 | * <p><strong>Note</strong>: After the database has been compacted, | |
1837 | * all data will have been pushed down to the last level containing | |
1838 | * any data.</p> | |
1839 | * | |
1840 | * <p>Compaction outputs should be placed in options.db_paths | |
1841 | * [target_path_id]. Behavior is undefined if target_path_id is | |
1842 | * out of range.</p> | |
1843 | * | |
1844 | * <p><strong>See also</strong></p> | |
1845 | * <ul> | |
1846 | * <li>{@link #compactRange()}</li> | |
1847 | * <li>{@link #compactRange(byte[], byte[])}</li> | |
1848 | * <li>{@link #compactRange(byte[], byte[], boolean, int, int)}</li> | |
1849 | * </ul> | |
1850 | * | |
11fdf7f2 TL |
1851 | * @deprecated Use {@link #compactRange(ColumnFamilyHandle, byte[], byte[], CompactRangeOptions)} instead |
1852 | * | |
7c673cae FG |
1853 | * @param reduce_level reduce level after compaction |
1854 | * @param target_level target level to compact to | |
1855 | * @param target_path_id the target path id of output path | |
1856 | * | |
1857 | * @throws RocksDBException thrown if an error occurs within the native | |
1858 | * part of the library. | |
1859 | */ | |
11fdf7f2 | 1860 | @Deprecated |
7c673cae FG |
1861 | public void compactRange(final boolean reduce_level, |
1862 | final int target_level, final int target_path_id) | |
1863 | throws RocksDBException { | |
1864 | compactRange0(nativeHandle_, reduce_level, | |
1865 | target_level, target_path_id); | |
1866 | } | |
1867 | ||
1868 | ||
1869 | /** | |
1870 | * <p>Range compaction of database.</p> | |
1871 | * <p><strong>Note</strong>: After the database has been compacted, | |
1872 | * all data will have been pushed down to the last level containing | |
1873 | * any data.</p> | |
1874 | * | |
1875 | * <p>Compaction outputs should be placed in options.db_paths | |
1876 | * [target_path_id]. Behavior is undefined if target_path_id is | |
1877 | * out of range.</p> | |
1878 | * | |
1879 | * <p><strong>See also</strong></p> | |
1880 | * <ul> | |
1881 | * <li>{@link #compactRange()}</li> | |
1882 | * <li>{@link #compactRange(boolean, int, int)}</li> | |
1883 | * <li>{@link #compactRange(byte[], byte[])}</li> | |
1884 | * </ul> | |
1885 | * | |
11fdf7f2 TL |
1886 | * @deprecated Use {@link #compactRange(ColumnFamilyHandle, byte[], byte[], CompactRangeOptions)} instead |
1887 | * | |
7c673cae FG |
1888 | * @param begin start of key range (included in range) |
1889 | * @param end end of key range (excluded from range) | |
1890 | * @param reduce_level reduce level after compaction | |
1891 | * @param target_level target level to compact to | |
1892 | * @param target_path_id the target path id of output path | |
1893 | * | |
1894 | * @throws RocksDBException thrown if an error occurs within the native | |
1895 | * part of the library. | |
1896 | */ | |
11fdf7f2 | 1897 | @Deprecated |
7c673cae FG |
1898 | public void compactRange(final byte[] begin, final byte[] end, |
1899 | final boolean reduce_level, final int target_level, | |
1900 | final int target_path_id) throws RocksDBException { | |
1901 | compactRange0(nativeHandle_, begin, begin.length, end, end.length, | |
1902 | reduce_level, target_level, target_path_id); | |
1903 | } | |
1904 | ||
1905 | /** | |
1906 | * <p>Range compaction of column family.</p> | |
1907 | * <p><strong>Note</strong>: After the database has been compacted, | |
1908 | * all data will have been pushed down to the last level containing | |
1909 | * any data.</p> | |
1910 | * | |
1911 | * <p><strong>See also</strong></p> | |
1912 | * <ul> | |
1913 | * <li> | |
1914 | * {@link #compactRange(ColumnFamilyHandle, boolean, int, int)} | |
1915 | * </li> | |
1916 | * <li> | |
1917 | * {@link #compactRange(ColumnFamilyHandle, byte[], byte[])} | |
1918 | * </li> | |
1919 | * <li> | |
1920 | * {@link #compactRange(ColumnFamilyHandle, byte[], byte[], | |
1921 | * boolean, int, int)} | |
1922 | * </li> | |
1923 | * </ul> | |
1924 | * | |
1925 | * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} | |
1926 | * instance. | |
1927 | * | |
1928 | * @throws RocksDBException thrown if an error occurs within the native | |
1929 | * part of the library. | |
1930 | */ | |
1931 | public void compactRange(final ColumnFamilyHandle columnFamilyHandle) | |
1932 | throws RocksDBException { | |
1933 | compactRange(nativeHandle_, false, -1, 0, | |
1934 | columnFamilyHandle.nativeHandle_); | |
1935 | } | |
1936 | ||
1937 | /** | |
1938 | * <p>Range compaction of column family.</p> | |
1939 | * <p><strong>Note</strong>: After the database has been compacted, | |
1940 | * all data will have been pushed down to the last level containing | |
1941 | * any data.</p> | |
1942 | * | |
1943 | * <p><strong>See also</strong></p> | |
1944 | * <ul> | |
1945 | * <li>{@link #compactRange(ColumnFamilyHandle)}</li> | |
1946 | * <li> | |
1947 | * {@link #compactRange(ColumnFamilyHandle, boolean, int, int)} | |
1948 | * </li> | |
1949 | * <li> | |
1950 | * {@link #compactRange(ColumnFamilyHandle, byte[], byte[], | |
1951 | * boolean, int, int)} | |
1952 | * </li> | |
1953 | * </ul> | |
1954 | * | |
1955 | * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} | |
1956 | * instance. | |
1957 | * @param begin start of key range (included in range) | |
1958 | * @param end end of key range (excluded from range) | |
1959 | * | |
1960 | * @throws RocksDBException thrown if an error occurs within the native | |
1961 | * part of the library. | |
1962 | */ | |
1963 | public void compactRange(final ColumnFamilyHandle columnFamilyHandle, | |
1964 | final byte[] begin, final byte[] end) throws RocksDBException { | |
1965 | compactRange(nativeHandle_, begin, begin.length, end, end.length, | |
1966 | false, -1, 0, columnFamilyHandle.nativeHandle_); | |
1967 | } | |
1968 | ||
11fdf7f2 TL |
1969 | |
1970 | /** | |
1971 | * <p>Range compaction of column family.</p> | |
1972 | * <p><strong>Note</strong>: After the database has been compacted, | |
1973 | * all data will have been pushed down to the last level containing | |
1974 | * any data.</p> | |
1975 | * | |
1976 | * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} instance. | |
1977 | * @param begin start of key range (included in range) | |
1978 | * @param end end of key range (excluded from range) | |
1979 | * @param compactRangeOptions options for the compaction | |
1980 | * | |
1981 | * @throws RocksDBException thrown if an error occurs within the native | |
1982 | * part of the library. | |
1983 | */ | |
1984 | public void compactRange(final ColumnFamilyHandle columnFamilyHandle, | |
1985 | final byte[] begin, final byte[] end, CompactRangeOptions compactRangeOptions) throws RocksDBException { | |
1986 | compactRange(nativeHandle_, begin, begin.length, end, end.length, | |
1987 | compactRangeOptions.nativeHandle_, columnFamilyHandle.nativeHandle_); | |
1988 | } | |
1989 | ||
7c673cae FG |
1990 | /** |
1991 | * <p>Range compaction of column family.</p> | |
1992 | * <p><strong>Note</strong>: After the database has been compacted, | |
1993 | * all data will have been pushed down to the last level containing | |
1994 | * any data.</p> | |
1995 | * | |
1996 | * <p>Compaction outputs should be placed in options.db_paths | |
1997 | * [target_path_id]. Behavior is undefined if target_path_id is | |
1998 | * out of range.</p> | |
1999 | * | |
2000 | * <p><strong>See also</strong></p> | |
2001 | * <ul> | |
2002 | * <li>{@link #compactRange(ColumnFamilyHandle)}</li> | |
2003 | * <li> | |
2004 | * {@link #compactRange(ColumnFamilyHandle, byte[], byte[])} | |
2005 | * </li> | |
2006 | * <li> | |
2007 | * {@link #compactRange(ColumnFamilyHandle, byte[], byte[], | |
2008 | * boolean, int, int)} | |
2009 | * </li> | |
2010 | * </ul> | |
2011 | * | |
11fdf7f2 TL |
2012 | * @deprecated Use {@link #compactRange(ColumnFamilyHandle, byte[], byte[], CompactRangeOptions)} instead |
2013 | * | |
7c673cae FG |
2014 | * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} |
2015 | * instance. | |
2016 | * @param reduce_level reduce level after compaction | |
2017 | * @param target_level target level to compact to | |
2018 | * @param target_path_id the target path id of output path | |
2019 | * | |
2020 | * @throws RocksDBException thrown if an error occurs within the native | |
2021 | * part of the library. | |
2022 | */ | |
11fdf7f2 | 2023 | @Deprecated |
7c673cae FG |
2024 | public void compactRange(final ColumnFamilyHandle columnFamilyHandle, |
2025 | final boolean reduce_level, final int target_level, | |
2026 | final int target_path_id) throws RocksDBException { | |
2027 | compactRange(nativeHandle_, reduce_level, target_level, | |
2028 | target_path_id, columnFamilyHandle.nativeHandle_); | |
2029 | } | |
2030 | ||
2031 | /** | |
2032 | * <p>Range compaction of column family.</p> | |
2033 | * <p><strong>Note</strong>: After the database has been compacted, | |
2034 | * all data will have been pushed down to the last level containing | |
2035 | * any data.</p> | |
2036 | * | |
2037 | * <p>Compaction outputs should be placed in options.db_paths | |
2038 | * [target_path_id]. Behavior is undefined if target_path_id is | |
2039 | * out of range.</p> | |
2040 | * | |
2041 | * <p><strong>See also</strong></p> | |
2042 | * <ul> | |
2043 | * <li>{@link #compactRange(ColumnFamilyHandle)}</li> | |
2044 | * <li> | |
2045 | * {@link #compactRange(ColumnFamilyHandle, boolean, int, int)} | |
2046 | * </li> | |
2047 | * <li> | |
2048 | * {@link #compactRange(ColumnFamilyHandle, byte[], byte[])} | |
2049 | * </li> | |
2050 | * </ul> | |
2051 | * | |
11fdf7f2 TL |
2052 | * @deprecated Use {@link #compactRange(ColumnFamilyHandle, byte[], byte[], CompactRangeOptions)} instead |
2053 | * | |
7c673cae FG |
2054 | * @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle} |
2055 | * instance. | |
2056 | * @param begin start of key range (included in range) | |
2057 | * @param end end of key range (excluded from range) | |
2058 | * @param reduce_level reduce level after compaction | |
2059 | * @param target_level target level to compact to | |
2060 | * @param target_path_id the target path id of output path | |
2061 | * | |
2062 | * @throws RocksDBException thrown if an error occurs within the native | |
2063 | * part of the library. | |
2064 | */ | |
11fdf7f2 | 2065 | @Deprecated |
7c673cae FG |
2066 | public void compactRange(final ColumnFamilyHandle columnFamilyHandle, |
2067 | final byte[] begin, final byte[] end, final boolean reduce_level, | |
2068 | final int target_level, final int target_path_id) | |
2069 | throws RocksDBException { | |
2070 | compactRange(nativeHandle_, begin, begin.length, end, end.length, | |
2071 | reduce_level, target_level, target_path_id, | |
2072 | columnFamilyHandle.nativeHandle_); | |
2073 | } | |
2074 | ||
2075 | /** | |
2076 | * This function will wait until all currently running background processes | |
2077 | * finish. After it returns, no background process will be run until | |
2078 | * {@link #continueBackgroundWork()} is called | |
2079 | * | |
2080 | * @throws RocksDBException If an error occurs when pausing background work | |
2081 | */ | |
2082 | public void pauseBackgroundWork() throws RocksDBException { | |
2083 | pauseBackgroundWork(nativeHandle_); | |
2084 | } | |
2085 | ||
2086 | /** | |
2087 | * Resumes backround work which was suspended by | |
2088 | * previously calling {@link #pauseBackgroundWork()} | |
2089 | * | |
2090 | * @throws RocksDBException If an error occurs when resuming background work | |
2091 | */ | |
2092 | public void continueBackgroundWork() throws RocksDBException { | |
2093 | continueBackgroundWork(nativeHandle_); | |
2094 | } | |
2095 | ||
2096 | /** | |
2097 | * <p>The sequence number of the most recent transaction.</p> | |
2098 | * | |
2099 | * @return sequence number of the most | |
2100 | * recent transaction. | |
2101 | */ | |
2102 | public long getLatestSequenceNumber() { | |
2103 | return getLatestSequenceNumber(nativeHandle_); | |
2104 | } | |
2105 | ||
2106 | /** | |
2107 | * <p>Prevent file deletions. Compactions will continue to occur, | |
2108 | * but no obsolete files will be deleted. Calling this multiple | |
2109 | * times have the same effect as calling it once.</p> | |
2110 | * | |
2111 | * @throws RocksDBException thrown if operation was not performed | |
2112 | * successfully. | |
2113 | */ | |
2114 | public void disableFileDeletions() throws RocksDBException { | |
2115 | disableFileDeletions(nativeHandle_); | |
2116 | } | |
2117 | ||
2118 | /** | |
2119 | * <p>Allow compactions to delete obsolete files. | |
2120 | * If force == true, the call to EnableFileDeletions() | |
2121 | * will guarantee that file deletions are enabled after | |
2122 | * the call, even if DisableFileDeletions() was called | |
2123 | * multiple times before.</p> | |
2124 | * | |
2125 | * <p>If force == false, EnableFileDeletions will only | |
2126 | * enable file deletion after it's been called at least | |
2127 | * as many times as DisableFileDeletions(), enabling | |
2128 | * the two methods to be called by two threads | |
2129 | * concurrently without synchronization | |
2130 | * -- i.e., file deletions will be enabled only after both | |
2131 | * threads call EnableFileDeletions()</p> | |
2132 | * | |
2133 | * @param force boolean value described above. | |
2134 | * | |
2135 | * @throws RocksDBException thrown if operation was not performed | |
2136 | * successfully. | |
2137 | */ | |
2138 | public void enableFileDeletions(final boolean force) | |
2139 | throws RocksDBException { | |
2140 | enableFileDeletions(nativeHandle_, force); | |
2141 | } | |
2142 | ||
2143 | /** | |
2144 | * <p>Returns an iterator that is positioned at a write-batch containing | |
2145 | * seq_number. If the sequence number is non existent, it returns an iterator | |
2146 | * at the first available seq_no after the requested seq_no.</p> | |
2147 | * | |
2148 | * <p>Must set WAL_ttl_seconds or WAL_size_limit_MB to large values to | |
2149 | * use this api, else the WAL files will get | |
2150 | * cleared aggressively and the iterator might keep getting invalid before | |
2151 | * an update is read.</p> | |
2152 | * | |
2153 | * @param sequenceNumber sequence number offset | |
2154 | * | |
2155 | * @return {@link org.rocksdb.TransactionLogIterator} instance. | |
2156 | * | |
2157 | * @throws org.rocksdb.RocksDBException if iterator cannot be retrieved | |
2158 | * from native-side. | |
2159 | */ | |
2160 | public TransactionLogIterator getUpdatesSince(final long sequenceNumber) | |
2161 | throws RocksDBException { | |
2162 | return new TransactionLogIterator( | |
2163 | getUpdatesSince(nativeHandle_, sequenceNumber)); | |
2164 | } | |
2165 | ||
2166 | public void setOptions(final ColumnFamilyHandle columnFamilyHandle, | |
2167 | final MutableColumnFamilyOptions mutableColumnFamilyOptions) | |
2168 | throws RocksDBException { | |
2169 | setOptions(nativeHandle_, columnFamilyHandle.nativeHandle_, | |
2170 | mutableColumnFamilyOptions.getKeys(), | |
2171 | mutableColumnFamilyOptions.getValues()); | |
2172 | } | |
2173 | ||
2174 | private long[] toNativeHandleList(final List<? extends RocksObject> objectList) { | |
2175 | final int len = objectList.size(); | |
2176 | final long[] handleList = new long[len]; | |
2177 | for (int i = 0; i < len; i++) { | |
2178 | handleList[i] = objectList.get(i).nativeHandle_; | |
2179 | } | |
2180 | return handleList; | |
2181 | } | |
2182 | ||
11fdf7f2 TL |
2183 | /** |
2184 | * ingestExternalFile will load a list of external SST files (1) into the DB | |
2185 | * We will try to find the lowest possible level that the file can fit in, and | |
2186 | * ingest the file into this level (2). A file that have a key range that | |
2187 | * overlap with the memtable key range will require us to Flush the memtable | |
2188 | * first before ingesting the file. | |
2189 | * | |
2190 | * (1) External SST files can be created using {@link SstFileWriter} | |
2191 | * (2) We will try to ingest the files to the lowest possible level | |
2192 | * even if the file compression doesn't match the level compression | |
2193 | * | |
2194 | * @param filePathList The list of files to ingest | |
2195 | * @param ingestExternalFileOptions the options for the ingestion | |
2196 | * | |
2197 | * @throws RocksDBException thrown if error happens in underlying | |
2198 | * native library. | |
2199 | */ | |
2200 | public void ingestExternalFile(final List<String> filePathList, | |
2201 | final IngestExternalFileOptions ingestExternalFileOptions) | |
7c673cae | 2202 | throws RocksDBException { |
11fdf7f2 TL |
2203 | ingestExternalFile(nativeHandle_, getDefaultColumnFamily().nativeHandle_, |
2204 | filePathList.toArray(new String[filePathList.size()]), | |
2205 | filePathList.size(), ingestExternalFileOptions.nativeHandle_); | |
7c673cae FG |
2206 | } |
2207 | ||
11fdf7f2 TL |
2208 | /** |
2209 | * ingestExternalFile will load a list of external SST files (1) into the DB | |
2210 | * We will try to find the lowest possible level that the file can fit in, and | |
2211 | * ingest the file into this level (2). A file that have a key range that | |
2212 | * overlap with the memtable key range will require us to Flush the memtable | |
2213 | * first before ingesting the file. | |
2214 | * | |
2215 | * (1) External SST files can be created using {@link SstFileWriter} | |
2216 | * (2) We will try to ingest the files to the lowest possible level | |
2217 | * even if the file compression doesn't match the level compression | |
2218 | * | |
2219 | * @param columnFamilyHandle The column family for the ingested files | |
2220 | * @param filePathList The list of files to ingest | |
2221 | * @param ingestExternalFileOptions the options for the ingestion | |
2222 | * | |
2223 | * @throws RocksDBException thrown if error happens in underlying | |
2224 | * native library. | |
2225 | */ | |
2226 | public void ingestExternalFile(final ColumnFamilyHandle columnFamilyHandle, | |
2227 | final List<String> filePathList, | |
2228 | final IngestExternalFileOptions ingestExternalFileOptions) | |
7c673cae | 2229 | throws RocksDBException { |
11fdf7f2 TL |
2230 | ingestExternalFile(nativeHandle_, columnFamilyHandle.nativeHandle_, |
2231 | filePathList.toArray(new String[filePathList.size()]), | |
2232 | filePathList.size(), ingestExternalFileOptions.nativeHandle_); | |
7c673cae FG |
2233 | } |
2234 | ||
11fdf7f2 TL |
2235 | /** |
2236 | * Static method to destroy the contents of the specified database. | |
2237 | * Be very careful using this method. | |
2238 | * | |
2239 | * @param path the path to the Rocksdb database. | |
2240 | * @param options {@link org.rocksdb.Options} instance. | |
2241 | * | |
2242 | * @throws RocksDBException thrown if error happens in underlying | |
2243 | * native library. | |
2244 | */ | |
2245 | public static void destroyDB(final String path, final Options options) | |
7c673cae | 2246 | throws RocksDBException { |
11fdf7f2 | 2247 | destroyDB(path, options.nativeHandle_); |
7c673cae FG |
2248 | } |
2249 | ||
2250 | /** | |
2251 | * Private constructor. | |
2252 | * | |
2253 | * @param nativeHandle The native handle of the C++ RocksDB object | |
2254 | */ | |
2255 | protected RocksDB(final long nativeHandle) { | |
2256 | super(nativeHandle); | |
2257 | } | |
2258 | ||
2259 | // native methods | |
2260 | protected native static long open(final long optionsHandle, | |
2261 | final String path) throws RocksDBException; | |
2262 | ||
2263 | /** | |
2264 | * @param optionsHandle Native handle pointing to an Options object | |
2265 | * @param path The directory path for the database files | |
2266 | * @param columnFamilyNames An array of column family names | |
2267 | * @param columnFamilyOptions An array of native handles pointing to | |
2268 | * ColumnFamilyOptions objects | |
2269 | * | |
2270 | * @return An array of native handles, [0] is the handle of the RocksDB object | |
2271 | * [1..1+n] are handles of the ColumnFamilyReferences | |
2272 | * | |
2273 | * @throws RocksDBException thrown if the database could not be opened | |
2274 | */ | |
2275 | protected native static long[] open(final long optionsHandle, | |
2276 | final String path, final byte[][] columnFamilyNames, | |
2277 | final long[] columnFamilyOptions) throws RocksDBException; | |
2278 | ||
2279 | protected native static long openROnly(final long optionsHandle, | |
2280 | final String path) throws RocksDBException; | |
2281 | ||
2282 | /** | |
2283 | * @param optionsHandle Native handle pointing to an Options object | |
2284 | * @param path The directory path for the database files | |
2285 | * @param columnFamilyNames An array of column family names | |
2286 | * @param columnFamilyOptions An array of native handles pointing to | |
2287 | * ColumnFamilyOptions objects | |
2288 | * | |
2289 | * @return An array of native handles, [0] is the handle of the RocksDB object | |
2290 | * [1..1+n] are handles of the ColumnFamilyReferences | |
2291 | * | |
2292 | * @throws RocksDBException thrown if the database could not be opened | |
2293 | */ | |
2294 | protected native static long[] openROnly(final long optionsHandle, | |
2295 | final String path, final byte[][] columnFamilyNames, | |
2296 | final long[] columnFamilyOptions | |
2297 | ) throws RocksDBException; | |
2298 | ||
2299 | protected native static byte[][] listColumnFamilies(long optionsHandle, | |
2300 | String path) throws RocksDBException; | |
2301 | protected native void put(long handle, byte[] key, int keyOffset, | |
2302 | int keyLength, byte[] value, int valueOffset, int valueLength) | |
2303 | throws RocksDBException; | |
2304 | protected native void put(long handle, byte[] key, int keyOffset, | |
2305 | int keyLength, byte[] value, int valueOffset, int valueLength, | |
2306 | long cfHandle) throws RocksDBException; | |
2307 | protected native void put(long handle, long writeOptHandle, byte[] key, | |
2308 | int keyOffset, int keyLength, byte[] value, int valueOffset, | |
2309 | int valueLength) throws RocksDBException; | |
2310 | protected native void put(long handle, long writeOptHandle, byte[] key, | |
2311 | int keyOffset, int keyLength, byte[] value, int valueOffset, | |
2312 | int valueLength, long cfHandle) throws RocksDBException; | |
2313 | protected native void write0(final long handle, long writeOptHandle, | |
2314 | long wbHandle) throws RocksDBException; | |
2315 | protected native void write1(final long handle, long writeOptHandle, | |
2316 | long wbwiHandle) throws RocksDBException; | |
2317 | protected native boolean keyMayExist(final long handle, final byte[] key, | |
2318 | final int keyOffset, final int keyLength, | |
2319 | final StringBuilder stringBuilder); | |
2320 | protected native boolean keyMayExist(final long handle, final byte[] key, | |
2321 | final int keyOffset, final int keyLength, final long cfHandle, | |
2322 | final StringBuilder stringBuilder); | |
2323 | protected native boolean keyMayExist(final long handle, | |
2324 | final long optionsHandle, final byte[] key, final int keyOffset, | |
2325 | final int keyLength, final StringBuilder stringBuilder); | |
2326 | protected native boolean keyMayExist(final long handle, | |
2327 | final long optionsHandle, final byte[] key, final int keyOffset, | |
2328 | final int keyLength, final long cfHandle, | |
2329 | final StringBuilder stringBuilder); | |
2330 | protected native void merge(long handle, byte[] key, int keyOffset, | |
2331 | int keyLength, byte[] value, int valueOffset, int valueLength) | |
2332 | throws RocksDBException; | |
2333 | protected native void merge(long handle, byte[] key, int keyOffset, | |
2334 | int keyLength, byte[] value, int valueOffset, int valueLength, | |
2335 | long cfHandle) throws RocksDBException; | |
2336 | protected native void merge(long handle, long writeOptHandle, byte[] key, | |
2337 | int keyOffset, int keyLength, byte[] value, int valueOffset, | |
2338 | int valueLength) throws RocksDBException; | |
2339 | protected native void merge(long handle, long writeOptHandle, byte[] key, | |
2340 | int keyOffset, int keyLength, byte[] value, int valueOffset, | |
2341 | int valueLength, long cfHandle) throws RocksDBException; | |
2342 | protected native int get(long handle, byte[] key, int keyOffset, | |
2343 | int keyLength, byte[] value, int valueOffset, int valueLength) | |
2344 | throws RocksDBException; | |
2345 | protected native int get(long handle, byte[] key, int keyOffset, | |
2346 | int keyLength, byte[] value, int valueOffset, int valueLength, | |
2347 | long cfHandle) throws RocksDBException; | |
2348 | protected native int get(long handle, long readOptHandle, byte[] key, | |
2349 | int keyOffset, int keyLength, byte[] value, int valueOffset, | |
2350 | int valueLength) throws RocksDBException; | |
2351 | protected native int get(long handle, long readOptHandle, byte[] key, | |
2352 | int keyOffset, int keyLength, byte[] value, int valueOffset, | |
2353 | int valueLength, long cfHandle) throws RocksDBException; | |
2354 | protected native byte[][] multiGet(final long dbHandle, final byte[][] keys, | |
2355 | final int[] keyOffsets, final int[] keyLengths); | |
2356 | protected native byte[][] multiGet(final long dbHandle, final byte[][] keys, | |
2357 | final int[] keyOffsets, final int[] keyLengths, | |
2358 | final long[] columnFamilyHandles); | |
2359 | protected native byte[][] multiGet(final long dbHandle, final long rOptHandle, | |
2360 | final byte[][] keys, final int[] keyOffsets, final int[] keyLengths); | |
2361 | protected native byte[][] multiGet(final long dbHandle, final long rOptHandle, | |
2362 | final byte[][] keys, final int[] keyOffsets, final int[] keyLengths, | |
2363 | final long[] columnFamilyHandles); | |
2364 | protected native byte[] get(long handle, byte[] key, int keyOffset, | |
2365 | int keyLength) throws RocksDBException; | |
2366 | protected native byte[] get(long handle, byte[] key, int keyOffset, | |
2367 | int keyLength, long cfHandle) throws RocksDBException; | |
2368 | protected native byte[] get(long handle, long readOptHandle, | |
2369 | byte[] key, int keyOffset, int keyLength) throws RocksDBException; | |
2370 | protected native byte[] get(long handle, long readOptHandle, byte[] key, | |
2371 | int keyOffset, int keyLength, long cfHandle) throws RocksDBException; | |
2372 | protected native void delete(long handle, byte[] key, int keyOffset, | |
2373 | int keyLength) throws RocksDBException; | |
2374 | protected native void delete(long handle, byte[] key, int keyOffset, | |
2375 | int keyLength, long cfHandle) throws RocksDBException; | |
2376 | protected native void delete(long handle, long writeOptHandle, byte[] key, | |
2377 | int keyOffset, int keyLength) throws RocksDBException; | |
2378 | protected native void delete(long handle, long writeOptHandle, byte[] key, | |
2379 | int keyOffset, int keyLength, long cfHandle) throws RocksDBException; | |
2380 | protected native void singleDelete( | |
2381 | long handle, byte[] key, int keyLen) throws RocksDBException; | |
2382 | protected native void singleDelete( | |
2383 | long handle, byte[] key, int keyLen, long cfHandle) | |
2384 | throws RocksDBException; | |
2385 | protected native void singleDelete( | |
2386 | long handle, long writeOptHandle, | |
2387 | byte[] key, int keyLen) throws RocksDBException; | |
2388 | protected native void singleDelete( | |
2389 | long handle, long writeOptHandle, | |
2390 | byte[] key, int keyLen, long cfHandle) throws RocksDBException; | |
2391 | protected native void deleteRange(long handle, byte[] beginKey, int beginKeyOffset, | |
2392 | int beginKeyLength, byte[] endKey, int endKeyOffset, int endKeyLength) | |
2393 | throws RocksDBException; | |
2394 | protected native void deleteRange(long handle, byte[] beginKey, int beginKeyOffset, | |
2395 | int beginKeyLength, byte[] endKey, int endKeyOffset, int endKeyLength, long cfHandle) | |
2396 | throws RocksDBException; | |
2397 | protected native void deleteRange(long handle, long writeOptHandle, byte[] beginKey, | |
2398 | int beginKeyOffset, int beginKeyLength, byte[] endKey, int endKeyOffset, int endKeyLength) | |
2399 | throws RocksDBException; | |
2400 | protected native void deleteRange(long handle, long writeOptHandle, byte[] beginKey, | |
2401 | int beginKeyOffset, int beginKeyLength, byte[] endKey, int endKeyOffset, int endKeyLength, | |
2402 | long cfHandle) throws RocksDBException; | |
2403 | protected native String getProperty0(long nativeHandle, | |
2404 | String property, int propertyLength) throws RocksDBException; | |
2405 | protected native String getProperty0(long nativeHandle, long cfHandle, | |
2406 | String property, int propertyLength) throws RocksDBException; | |
2407 | protected native long getLongProperty(long nativeHandle, String property, | |
2408 | int propertyLength) throws RocksDBException; | |
2409 | protected native long getLongProperty(long nativeHandle, long cfHandle, | |
2410 | String property, int propertyLength) throws RocksDBException; | |
11fdf7f2 TL |
2411 | protected native long getAggregatedLongProperty(long nativeHandle, String property, |
2412 | int propertyLength) throws RocksDBException; | |
7c673cae FG |
2413 | protected native long iterator(long handle); |
2414 | protected native long iterator(long handle, long readOptHandle); | |
2415 | protected native long iteratorCF(long handle, long cfHandle); | |
2416 | protected native long iteratorCF(long handle, long cfHandle, | |
2417 | long readOptHandle); | |
2418 | protected native long[] iterators(final long handle, | |
2419 | final long[] columnFamilyHandles, final long readOptHandle) | |
2420 | throws RocksDBException; | |
2421 | protected native long getSnapshot(long nativeHandle); | |
11fdf7f2 TL |
2422 | protected native void releaseSnapshot( |
2423 | long nativeHandle, long snapshotHandle); | |
2424 | @Override protected native void disposeInternal(final long handle); | |
7c673cae FG |
2425 | private native long getDefaultColumnFamily(long handle); |
2426 | private native long createColumnFamily(final long handle, | |
2427 | final byte[] columnFamilyName, final long columnFamilyOptions) | |
2428 | throws RocksDBException; | |
2429 | private native void dropColumnFamily(long handle, long cfHandle) | |
2430 | throws RocksDBException; | |
2431 | private native void flush(long handle, long flushOptHandle) | |
2432 | throws RocksDBException; | |
2433 | private native void flush(long handle, long flushOptHandle, long cfHandle) | |
2434 | throws RocksDBException; | |
2435 | private native void compactRange0(long handle, boolean reduce_level, | |
2436 | int target_level, int target_path_id) throws RocksDBException; | |
2437 | private native void compactRange0(long handle, byte[] begin, int beginLen, | |
2438 | byte[] end, int endLen, boolean reduce_level, int target_level, | |
2439 | int target_path_id) throws RocksDBException; | |
11fdf7f2 TL |
2440 | private native void compactRange(long handle, byte[] begin, int beginLen, |
2441 | byte[] end, int endLen, long compactRangeOptHandle, long cfHandle) | |
2442 | throws RocksDBException; | |
7c673cae FG |
2443 | private native void compactRange(long handle, boolean reduce_level, |
2444 | int target_level, int target_path_id, long cfHandle) | |
2445 | throws RocksDBException; | |
2446 | private native void compactRange(long handle, byte[] begin, int beginLen, | |
2447 | byte[] end, int endLen, boolean reduce_level, int target_level, | |
2448 | int target_path_id, long cfHandle) throws RocksDBException; | |
2449 | private native void pauseBackgroundWork(long handle) throws RocksDBException; | |
2450 | private native void continueBackgroundWork(long handle) throws RocksDBException; | |
2451 | private native long getLatestSequenceNumber(long handle); | |
2452 | private native void disableFileDeletions(long handle) throws RocksDBException; | |
2453 | private native void enableFileDeletions(long handle, boolean force) | |
2454 | throws RocksDBException; | |
2455 | private native long getUpdatesSince(long handle, long sequenceNumber) | |
2456 | throws RocksDBException; | |
2457 | private native void setOptions(long handle, long cfHandle, String[] keys, | |
2458 | String[] values) throws RocksDBException; | |
11fdf7f2 TL |
2459 | private native void ingestExternalFile(long handle, long cfHandle, |
2460 | String[] filePathList, int filePathListLen, | |
2461 | long ingest_external_file_options_handle) throws RocksDBException; | |
2462 | private native static void destroyDB(final String path, | |
2463 | final long optionsHandle) throws RocksDBException; | |
7c673cae FG |
2464 | protected DBOptionsInterface options_; |
2465 | } |