1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
8 import org
.junit
.rules
.ExpectedException
;
9 import org
.junit
.rules
.TemporaryFolder
;
11 import java
.nio
.ByteBuffer
;
14 import static java
.nio
.charset
.StandardCharsets
.UTF_8
;
15 import static org
.assertj
.core
.api
.Assertions
.assertThat
;
16 import static org
.junit
.Assert
.fail
;
18 public class RocksDBTest
{
21 public static final RocksMemoryResource rocksMemoryResource
=
22 new RocksMemoryResource();
25 public TemporaryFolder dbFolder
= new TemporaryFolder();
27 public static final Random rand
= PlatformRandomHelper
.
28 getPlatformSpecificRandomFactory();
31 public void open() throws RocksDBException
{
32 try (final RocksDB db
=
33 RocksDB
.open(dbFolder
.getRoot().getAbsolutePath())) {
34 assertThat(db
).isNotNull();
39 public void open_opt() throws RocksDBException
{
40 try (final Options opt
= new Options().setCreateIfMissing(true);
41 final RocksDB db
= RocksDB
.open(opt
,
42 dbFolder
.getRoot().getAbsolutePath())) {
43 assertThat(db
).isNotNull();
48 public void openWhenOpen() throws RocksDBException
{
49 final String dbPath
= dbFolder
.getRoot().getAbsolutePath();
51 try (final RocksDB db1
= RocksDB
.open(dbPath
)) {
52 try (final RocksDB db2
= RocksDB
.open(dbPath
)) {
53 fail("Should have thrown an exception when opening the same db twice");
54 } catch (final RocksDBException e
) {
55 assertThat(e
.getStatus().getCode()).isEqualTo(Status
.Code
.IOError
);
56 assertThat(e
.getStatus().getSubCode()).isEqualTo(Status
.SubCode
.None
);
57 assertThat(e
.getStatus().getState()).contains("lock ");
63 public void createColumnFamily() throws RocksDBException
{
64 final byte[] col1Name
= "col1".getBytes(UTF_8
);
66 try (final RocksDB db
= RocksDB
.open(dbFolder
.getRoot().getAbsolutePath());
67 final ColumnFamilyOptions cfOpts
= new ColumnFamilyOptions()
69 try (final ColumnFamilyHandle col1
=
70 db
.createColumnFamily(new ColumnFamilyDescriptor(col1Name
, cfOpts
))) {
71 assertThat(col1
).isNotNull();
72 assertThat(col1
.getName()).isEqualTo(col1Name
);
76 final List
<ColumnFamilyHandle
> cfHandles
= new ArrayList
<>();
77 try (final RocksDB db
= RocksDB
.open(dbFolder
.getRoot().getAbsolutePath(),
79 new ColumnFamilyDescriptor(RocksDB
.DEFAULT_COLUMN_FAMILY
),
80 new ColumnFamilyDescriptor(col1Name
)),
83 assertThat(cfHandles
.size()).isEqualTo(2);
84 assertThat(cfHandles
.get(1)).isNotNull();
85 assertThat(cfHandles
.get(1).getName()).isEqualTo(col1Name
);
87 for (final ColumnFamilyHandle cfHandle
:
97 public void createColumnFamilies() throws RocksDBException
{
98 final byte[] col1Name
= "col1".getBytes(UTF_8
);
99 final byte[] col2Name
= "col2".getBytes(UTF_8
);
101 List
<ColumnFamilyHandle
> cfHandles
;
102 try (final RocksDB db
= RocksDB
.open(dbFolder
.getRoot().getAbsolutePath());
103 final ColumnFamilyOptions cfOpts
= new ColumnFamilyOptions()
106 db
.createColumnFamilies(cfOpts
, Arrays
.asList(col1Name
, col2Name
));
108 assertThat(cfHandles
).isNotNull();
109 assertThat(cfHandles
.size()).isEqualTo(2);
110 assertThat(cfHandles
.get(0).getName()).isEqualTo(col1Name
);
111 assertThat(cfHandles
.get(1).getName()).isEqualTo(col2Name
);
113 for (final ColumnFamilyHandle cfHandle
: cfHandles
) {
119 cfHandles
= new ArrayList
<>();
120 try (final RocksDB db
= RocksDB
.open(dbFolder
.getRoot().getAbsolutePath(),
122 new ColumnFamilyDescriptor(RocksDB
.DEFAULT_COLUMN_FAMILY
),
123 new ColumnFamilyDescriptor(col1Name
),
124 new ColumnFamilyDescriptor(col2Name
)),
127 assertThat(cfHandles
.size()).isEqualTo(3);
128 assertThat(cfHandles
.get(1)).isNotNull();
129 assertThat(cfHandles
.get(1).getName()).isEqualTo(col1Name
);
130 assertThat(cfHandles
.get(2)).isNotNull();
131 assertThat(cfHandles
.get(2).getName()).isEqualTo(col2Name
);
133 for (final ColumnFamilyHandle cfHandle
: cfHandles
) {
141 public void createColumnFamiliesfromDescriptors() throws RocksDBException
{
142 final byte[] col1Name
= "col1".getBytes(UTF_8
);
143 final byte[] col2Name
= "col2".getBytes(UTF_8
);
145 List
<ColumnFamilyHandle
> cfHandles
;
146 try (final RocksDB db
= RocksDB
.open(dbFolder
.getRoot().getAbsolutePath());
147 final ColumnFamilyOptions cfOpts
= new ColumnFamilyOptions()
150 db
.createColumnFamilies(Arrays
.asList(
151 new ColumnFamilyDescriptor(col1Name
, cfOpts
),
152 new ColumnFamilyDescriptor(col2Name
, cfOpts
)));
154 assertThat(cfHandles
).isNotNull();
155 assertThat(cfHandles
.size()).isEqualTo(2);
156 assertThat(cfHandles
.get(0).getName()).isEqualTo(col1Name
);
157 assertThat(cfHandles
.get(1).getName()).isEqualTo(col2Name
);
159 for (final ColumnFamilyHandle cfHandle
: cfHandles
) {
165 cfHandles
= new ArrayList
<>();
166 try (final RocksDB db
= RocksDB
.open(dbFolder
.getRoot().getAbsolutePath(),
168 new ColumnFamilyDescriptor(RocksDB
.DEFAULT_COLUMN_FAMILY
),
169 new ColumnFamilyDescriptor(col1Name
),
170 new ColumnFamilyDescriptor(col2Name
)),
173 assertThat(cfHandles
.size()).isEqualTo(3);
174 assertThat(cfHandles
.get(1)).isNotNull();
175 assertThat(cfHandles
.get(1).getName()).isEqualTo(col1Name
);
176 assertThat(cfHandles
.get(2)).isNotNull();
177 assertThat(cfHandles
.get(2).getName()).isEqualTo(col2Name
);
179 for (final ColumnFamilyHandle cfHandle
: cfHandles
) {
187 public void put() throws RocksDBException
{
188 try (final RocksDB db
= RocksDB
.open(dbFolder
.getRoot().getAbsolutePath());
189 final WriteOptions opt
= new WriteOptions()) {
190 db
.put("key1".getBytes(), "value".getBytes());
191 db
.put(opt
, "key2".getBytes(), "12345678".getBytes());
192 assertThat(db
.get("key1".getBytes())).isEqualTo(
194 assertThat(db
.get("key2".getBytes())).isEqualTo(
195 "12345678".getBytes());
199 Segment key3
= sliceSegment("key3");
200 Segment key4
= sliceSegment("key4");
201 Segment value0
= sliceSegment("value 0");
202 Segment value1
= sliceSegment("value 1");
203 db
.put(key3
.data
, key3
.offset
, key3
.len
, value0
.data
, value0
.offset
, value0
.len
);
204 db
.put(opt
, key4
.data
, key4
.offset
, key4
.len
, value1
.data
, value1
.offset
, value1
.len
);
207 Assert
.assertTrue(value0
.isSamePayload(db
.get(key3
.data
, key3
.offset
, key3
.len
)));
208 Assert
.assertTrue(value1
.isSamePayload(db
.get(key4
.data
, key4
.offset
, key4
.len
)));
212 private static Segment
sliceSegment(String key
) {
213 ByteBuffer rawKey
= ByteBuffer
.allocate(key
.length() + 4);
216 rawKey
.put(key
.getBytes());
218 return new Segment(rawKey
.array(), 2, key
.length());
221 private static class Segment
{
226 public boolean isSamePayload(byte[] value
) {
230 if (value
.length
!= len
) {
234 for (int i
= 0; i
< value
.length
; i
++) {
235 if (data
[i
+ offset
] != value
[i
]) {
243 public Segment(byte[] value
, int offset
, int len
) {
245 this.offset
= offset
;
251 public void write() throws RocksDBException
{
252 try (final StringAppendOperator stringAppendOperator
= new StringAppendOperator();
253 final Options options
= new Options()
254 .setMergeOperator(stringAppendOperator
)
255 .setCreateIfMissing(true);
256 final RocksDB db
= RocksDB
.open(options
,
257 dbFolder
.getRoot().getAbsolutePath());
258 final WriteOptions opts
= new WriteOptions()) {
260 try (final WriteBatch wb1
= new WriteBatch()) {
261 wb1
.put("key1".getBytes(), "aa".getBytes());
262 wb1
.merge("key1".getBytes(), "bb".getBytes());
264 try (final WriteBatch wb2
= new WriteBatch()) {
265 wb2
.put("key2".getBytes(), "xx".getBytes());
266 wb2
.merge("key2".getBytes(), "yy".getBytes());
272 assertThat(db
.get("key1".getBytes())).isEqualTo(
274 assertThat(db
.get("key2".getBytes())).isEqualTo(
280 public void getWithOutValue() throws RocksDBException
{
281 try (final RocksDB db
=
282 RocksDB
.open(dbFolder
.getRoot().getAbsolutePath())) {
283 db
.put("key1".getBytes(), "value".getBytes());
284 db
.put("key2".getBytes(), "12345678".getBytes());
285 byte[] outValue
= new byte[5];
287 int getResult
= db
.get("keyNotFound".getBytes(), outValue
);
288 assertThat(getResult
).isEqualTo(RocksDB
.NOT_FOUND
);
289 // found value which fits in outValue
290 getResult
= db
.get("key1".getBytes(), outValue
);
291 assertThat(getResult
).isNotEqualTo(RocksDB
.NOT_FOUND
);
292 assertThat(outValue
).isEqualTo("value".getBytes());
293 // found value which fits partially
294 getResult
= db
.get("key2".getBytes(), outValue
);
295 assertThat(getResult
).isNotEqualTo(RocksDB
.NOT_FOUND
);
296 assertThat(outValue
).isEqualTo("12345".getBytes());
301 public void getWithOutValueReadOptions() throws RocksDBException
{
302 try (final RocksDB db
= RocksDB
.open(dbFolder
.getRoot().getAbsolutePath());
303 final ReadOptions rOpt
= new ReadOptions()) {
304 db
.put("key1".getBytes(), "value".getBytes());
305 db
.put("key2".getBytes(), "12345678".getBytes());
306 byte[] outValue
= new byte[5];
308 int getResult
= db
.get(rOpt
, "keyNotFound".getBytes(),
310 assertThat(getResult
).isEqualTo(RocksDB
.NOT_FOUND
);
311 // found value which fits in outValue
312 getResult
= db
.get(rOpt
, "key1".getBytes(), outValue
);
313 assertThat(getResult
).isNotEqualTo(RocksDB
.NOT_FOUND
);
314 assertThat(outValue
).isEqualTo("value".getBytes());
315 // found value which fits partially
316 getResult
= db
.get(rOpt
, "key2".getBytes(), outValue
);
317 assertThat(getResult
).isNotEqualTo(RocksDB
.NOT_FOUND
);
318 assertThat(outValue
).isEqualTo("12345".getBytes());
323 public ExpectedException thrown
= ExpectedException
.none();
326 public void getOutOfArrayMaxSizeValue() throws RocksDBException
{
327 final int numberOfValueSplits
= 10;
328 final int splitSize
= Integer
.MAX_VALUE
/ numberOfValueSplits
;
330 Runtime runtime
= Runtime
.getRuntime();
331 long neededMemory
= ((long)(splitSize
)) * (((long)numberOfValueSplits
) + 3);
332 boolean isEnoughMemory
= runtime
.maxMemory() - runtime
.totalMemory() > neededMemory
;
333 Assume
.assumeTrue(isEnoughMemory
);
335 final byte[] valueSplit
= new byte[splitSize
];
336 final byte[] key
= "key".getBytes();
338 thrown
.expect(RocksDBException
.class);
339 thrown
.expectMessage("Requested array size exceeds VM limit");
341 // merge (numberOfValueSplits + 1) valueSplit's to get value size exceeding Integer.MAX_VALUE
342 try (final StringAppendOperator stringAppendOperator
= new StringAppendOperator();
343 final Options opt
= new Options()
344 .setCreateIfMissing(true)
345 .setMergeOperator(stringAppendOperator
);
346 final RocksDB db
= RocksDB
.open(opt
, dbFolder
.getRoot().getAbsolutePath())) {
347 db
.put(key
, valueSplit
);
348 for (int i
= 0; i
< numberOfValueSplits
; i
++) {
349 db
.merge(key
, valueSplit
);
356 public void multiGet() throws RocksDBException
, InterruptedException
{
357 try (final RocksDB db
= RocksDB
.open(dbFolder
.getRoot().getAbsolutePath());
358 final ReadOptions rOpt
= new ReadOptions()) {
359 db
.put("key1".getBytes(), "value".getBytes());
360 db
.put("key2".getBytes(), "12345678".getBytes());
361 List
<byte[]> lookupKeys
= new ArrayList
<>();
362 lookupKeys
.add("key1".getBytes());
363 lookupKeys
.add("key2".getBytes());
364 Map
<byte[], byte[]> results
= db
.multiGet(lookupKeys
);
365 assertThat(results
).isNotNull();
366 assertThat(results
.values()).isNotNull();
367 assertThat(results
.values()).
368 contains("value".getBytes(), "12345678".getBytes());
369 // test same method with ReadOptions
370 results
= db
.multiGet(rOpt
, lookupKeys
);
371 assertThat(results
).isNotNull();
372 assertThat(results
.values()).isNotNull();
373 assertThat(results
.values()).
374 contains("value".getBytes(), "12345678".getBytes());
376 // remove existing key
377 lookupKeys
.remove("key2".getBytes());
378 // add non existing key
379 lookupKeys
.add("key3".getBytes());
380 results
= db
.multiGet(lookupKeys
);
381 assertThat(results
).isNotNull();
382 assertThat(results
.values()).isNotNull();
383 assertThat(results
.values()).
384 contains("value".getBytes());
385 // test same call with readOptions
386 results
= db
.multiGet(rOpt
, lookupKeys
);
387 assertThat(results
).isNotNull();
388 assertThat(results
.values()).isNotNull();
389 assertThat(results
.values()).
390 contains("value".getBytes());
395 public void multiGetAsList() throws RocksDBException
, InterruptedException
{
396 try (final RocksDB db
= RocksDB
.open(dbFolder
.getRoot().getAbsolutePath());
397 final ReadOptions rOpt
= new ReadOptions()) {
398 db
.put("key1".getBytes(), "value".getBytes());
399 db
.put("key2".getBytes(), "12345678".getBytes());
400 List
<byte[]> lookupKeys
= new ArrayList
<>();
401 lookupKeys
.add("key1".getBytes());
402 lookupKeys
.add("key2".getBytes());
403 List
<byte[]> results
= db
.multiGetAsList(lookupKeys
);
404 assertThat(results
).isNotNull();
405 assertThat(results
).hasSize(lookupKeys
.size());
407 containsExactly("value".getBytes(), "12345678".getBytes());
408 // test same method with ReadOptions
409 results
= db
.multiGetAsList(rOpt
, lookupKeys
);
410 assertThat(results
).isNotNull();
412 contains("value".getBytes(), "12345678".getBytes());
414 // remove existing key
415 lookupKeys
.remove(1);
416 // add non existing key
417 lookupKeys
.add("key3".getBytes());
418 results
= db
.multiGetAsList(lookupKeys
);
419 assertThat(results
).isNotNull();
421 containsExactly("value".getBytes(), null);
422 // test same call with readOptions
423 results
= db
.multiGetAsList(rOpt
, lookupKeys
);
424 assertThat(results
).isNotNull();
425 assertThat(results
).contains("value".getBytes());
430 public void merge() throws RocksDBException
{
431 try (final StringAppendOperator stringAppendOperator
= new StringAppendOperator();
432 final Options opt
= new Options()
433 .setCreateIfMissing(true)
434 .setMergeOperator(stringAppendOperator
);
435 final WriteOptions wOpt
= new WriteOptions();
436 final RocksDB db
= RocksDB
.open(opt
,
437 dbFolder
.getRoot().getAbsolutePath())
439 db
.put("key1".getBytes(), "value".getBytes());
440 assertThat(db
.get("key1".getBytes())).isEqualTo(
442 // merge key1 with another value portion
443 db
.merge("key1".getBytes(), "value2".getBytes());
444 assertThat(db
.get("key1".getBytes())).isEqualTo(
445 "value,value2".getBytes());
446 // merge key1 with another value portion
447 db
.merge(wOpt
, "key1".getBytes(), "value3".getBytes());
448 assertThat(db
.get("key1".getBytes())).isEqualTo(
449 "value,value2,value3".getBytes());
450 // merge on non existent key shall insert the value
451 db
.merge(wOpt
, "key2".getBytes(), "xxxx".getBytes());
452 assertThat(db
.get("key2".getBytes())).isEqualTo(
455 Segment key3
= sliceSegment("key3");
456 Segment key4
= sliceSegment("key4");
457 Segment value0
= sliceSegment("value 0");
458 Segment value1
= sliceSegment("value 1");
460 db
.merge(key3
.data
, key3
.offset
, key3
.len
, value0
.data
, value0
.offset
, value0
.len
);
461 db
.merge(wOpt
, key4
.data
, key4
.offset
, key4
.len
, value1
.data
, value1
.offset
, value1
.len
);
464 Assert
.assertTrue(value0
.isSamePayload(db
.get(key3
.data
, key3
.offset
, key3
.len
)));
465 Assert
.assertTrue(value1
.isSamePayload(db
.get(key4
.data
, key4
.offset
, key4
.len
)));
470 public void delete() throws RocksDBException
{
471 try (final RocksDB db
= RocksDB
.open(dbFolder
.getRoot().getAbsolutePath());
472 final WriteOptions wOpt
= new WriteOptions()) {
473 db
.put("key1".getBytes(), "value".getBytes());
474 db
.put("key2".getBytes(), "12345678".getBytes());
475 assertThat(db
.get("key1".getBytes())).isEqualTo(
477 assertThat(db
.get("key2".getBytes())).isEqualTo(
478 "12345678".getBytes());
479 db
.delete("key1".getBytes());
480 db
.delete(wOpt
, "key2".getBytes());
481 assertThat(db
.get("key1".getBytes())).isNull();
482 assertThat(db
.get("key2".getBytes())).isNull();
485 Segment key3
= sliceSegment("key3");
486 Segment key4
= sliceSegment("key4");
487 db
.put("key3".getBytes(), "key3 value".getBytes());
488 db
.put("key4".getBytes(), "key4 value".getBytes());
490 db
.delete(key3
.data
, key3
.offset
, key3
.len
);
491 db
.delete(wOpt
, key4
.data
, key4
.offset
, key4
.len
);
493 assertThat(db
.get("key3".getBytes())).isNull();
494 assertThat(db
.get("key4".getBytes())).isNull();
499 public void singleDelete() throws RocksDBException
{
500 try (final RocksDB db
= RocksDB
.open(dbFolder
.getRoot().getAbsolutePath());
501 final WriteOptions wOpt
= new WriteOptions()) {
502 db
.put("key1".getBytes(), "value".getBytes());
503 db
.put("key2".getBytes(), "12345678".getBytes());
504 assertThat(db
.get("key1".getBytes())).isEqualTo(
506 assertThat(db
.get("key2".getBytes())).isEqualTo(
507 "12345678".getBytes());
508 db
.singleDelete("key1".getBytes());
509 db
.singleDelete(wOpt
, "key2".getBytes());
510 assertThat(db
.get("key1".getBytes())).isNull();
511 assertThat(db
.get("key2".getBytes())).isNull();
516 public void singleDelete_nonExisting() throws RocksDBException
{
517 try (final RocksDB db
= RocksDB
.open(dbFolder
.getRoot().getAbsolutePath());
518 final WriteOptions wOpt
= new WriteOptions()) {
519 db
.singleDelete("key1".getBytes());
520 db
.singleDelete(wOpt
, "key2".getBytes());
521 assertThat(db
.get("key1".getBytes())).isNull();
522 assertThat(db
.get("key2".getBytes())).isNull();
527 public void deleteRange() throws RocksDBException
{
528 try (final RocksDB db
= RocksDB
.open(dbFolder
.getRoot().getAbsolutePath())) {
529 db
.put("key1".getBytes(), "value".getBytes());
530 db
.put("key2".getBytes(), "12345678".getBytes());
531 db
.put("key3".getBytes(), "abcdefg".getBytes());
532 db
.put("key4".getBytes(), "xyz".getBytes());
533 assertThat(db
.get("key1".getBytes())).isEqualTo("value".getBytes());
534 assertThat(db
.get("key2".getBytes())).isEqualTo("12345678".getBytes());
535 assertThat(db
.get("key3".getBytes())).isEqualTo("abcdefg".getBytes());
536 assertThat(db
.get("key4".getBytes())).isEqualTo("xyz".getBytes());
537 db
.deleteRange("key2".getBytes(), "key4".getBytes());
538 assertThat(db
.get("key1".getBytes())).isEqualTo("value".getBytes());
539 assertThat(db
.get("key2".getBytes())).isNull();
540 assertThat(db
.get("key3".getBytes())).isNull();
541 assertThat(db
.get("key4".getBytes())).isEqualTo("xyz".getBytes());
546 public void getIntProperty() throws RocksDBException
{
548 final Options options
= new Options()
549 .setCreateIfMissing(true)
550 .setMaxWriteBufferNumber(10)
551 .setMinWriteBufferNumberToMerge(10);
552 final RocksDB db
= RocksDB
.open(options
,
553 dbFolder
.getRoot().getAbsolutePath());
554 final WriteOptions wOpt
= new WriteOptions().setDisableWAL(true)
556 db
.put(wOpt
, "key1".getBytes(), "value1".getBytes());
557 db
.put(wOpt
, "key2".getBytes(), "value2".getBytes());
558 db
.put(wOpt
, "key3".getBytes(), "value3".getBytes());
559 db
.put(wOpt
, "key4".getBytes(), "value4".getBytes());
560 assertThat(db
.getLongProperty("rocksdb.num-entries-active-mem-table"))
562 assertThat(db
.getLongProperty("rocksdb.cur-size-active-mem-table"))
568 public void fullCompactRange() throws RocksDBException
{
569 try (final Options opt
= new Options().
570 setCreateIfMissing(true).
571 setDisableAutoCompactions(true).
572 setCompactionStyle(CompactionStyle
.LEVEL
).
574 setWriteBufferSize(100 << 10).
575 setLevelZeroFileNumCompactionTrigger(3).
576 setTargetFileSizeBase(200 << 10).
577 setTargetFileSizeMultiplier(1).
578 setMaxBytesForLevelBase(500 << 10).
579 setMaxBytesForLevelMultiplier(1).
580 setDisableAutoCompactions(false);
581 final RocksDB db
= RocksDB
.open(opt
,
582 dbFolder
.getRoot().getAbsolutePath())) {
583 // fill database with key/value pairs
584 byte[] b
= new byte[10000];
585 for (int i
= 0; i
< 200; i
++) {
587 db
.put((String
.valueOf(i
)).getBytes(), b
);
594 public void fullCompactRangeColumnFamily()
595 throws RocksDBException
{
597 final DBOptions opt
= new DBOptions().
598 setCreateIfMissing(true).
599 setCreateMissingColumnFamilies(true);
600 final ColumnFamilyOptions new_cf_opts
= new ColumnFamilyOptions().
601 setDisableAutoCompactions(true).
602 setCompactionStyle(CompactionStyle
.LEVEL
).
604 setWriteBufferSize(100 << 10).
605 setLevelZeroFileNumCompactionTrigger(3).
606 setTargetFileSizeBase(200 << 10).
607 setTargetFileSizeMultiplier(1).
608 setMaxBytesForLevelBase(500 << 10).
609 setMaxBytesForLevelMultiplier(1).
610 setDisableAutoCompactions(false)
612 final List
<ColumnFamilyDescriptor
> columnFamilyDescriptors
=
614 new ColumnFamilyDescriptor(RocksDB
.DEFAULT_COLUMN_FAMILY
),
615 new ColumnFamilyDescriptor("new_cf".getBytes(), new_cf_opts
));
618 final List
<ColumnFamilyHandle
> columnFamilyHandles
= new ArrayList
<>();
619 try (final RocksDB db
= RocksDB
.open(opt
,
620 dbFolder
.getRoot().getAbsolutePath(),
621 columnFamilyDescriptors
,
622 columnFamilyHandles
)) {
624 // fill database with key/value pairs
625 byte[] b
= new byte[10000];
626 for (int i
= 0; i
< 200; i
++) {
628 db
.put(columnFamilyHandles
.get(1),
629 String
.valueOf(i
).getBytes(), b
);
631 db
.compactRange(columnFamilyHandles
.get(1));
633 for (final ColumnFamilyHandle handle
: columnFamilyHandles
) {
642 public void compactRangeWithKeys()
643 throws RocksDBException
{
644 try (final Options opt
= new Options().
645 setCreateIfMissing(true).
646 setDisableAutoCompactions(true).
647 setCompactionStyle(CompactionStyle
.LEVEL
).
649 setWriteBufferSize(100 << 10).
650 setLevelZeroFileNumCompactionTrigger(3).
651 setTargetFileSizeBase(200 << 10).
652 setTargetFileSizeMultiplier(1).
653 setMaxBytesForLevelBase(500 << 10).
654 setMaxBytesForLevelMultiplier(1).
655 setDisableAutoCompactions(false);
656 final RocksDB db
= RocksDB
.open(opt
,
657 dbFolder
.getRoot().getAbsolutePath())) {
658 // fill database with key/value pairs
659 byte[] b
= new byte[10000];
660 for (int i
= 0; i
< 200; i
++) {
662 db
.put((String
.valueOf(i
)).getBytes(), b
);
664 db
.compactRange("0".getBytes(), "201".getBytes());
669 public void compactRangeWithKeysReduce()
670 throws RocksDBException
{
672 final Options opt
= new Options().
673 setCreateIfMissing(true).
674 setDisableAutoCompactions(true).
675 setCompactionStyle(CompactionStyle
.LEVEL
).
677 setWriteBufferSize(100 << 10).
678 setLevelZeroFileNumCompactionTrigger(3).
679 setTargetFileSizeBase(200 << 10).
680 setTargetFileSizeMultiplier(1).
681 setMaxBytesForLevelBase(500 << 10).
682 setMaxBytesForLevelMultiplier(1).
683 setDisableAutoCompactions(false);
684 final RocksDB db
= RocksDB
.open(opt
,
685 dbFolder
.getRoot().getAbsolutePath())) {
686 // fill database with key/value pairs
687 byte[] b
= new byte[10000];
688 for (int i
= 0; i
< 200; i
++) {
690 db
.put((String
.valueOf(i
)).getBytes(), b
);
692 db
.flush(new FlushOptions().setWaitForFlush(true));
693 db
.compactRange("0".getBytes(), "201".getBytes(),
699 public void compactRangeWithKeysColumnFamily()
700 throws RocksDBException
{
701 try (final DBOptions opt
= new DBOptions().
702 setCreateIfMissing(true).
703 setCreateMissingColumnFamilies(true);
704 final ColumnFamilyOptions new_cf_opts
= new ColumnFamilyOptions().
705 setDisableAutoCompactions(true).
706 setCompactionStyle(CompactionStyle
.LEVEL
).
708 setWriteBufferSize(100 << 10).
709 setLevelZeroFileNumCompactionTrigger(3).
710 setTargetFileSizeBase(200 << 10).
711 setTargetFileSizeMultiplier(1).
712 setMaxBytesForLevelBase(500 << 10).
713 setMaxBytesForLevelMultiplier(1).
714 setDisableAutoCompactions(false)
716 final List
<ColumnFamilyDescriptor
> columnFamilyDescriptors
=
718 new ColumnFamilyDescriptor(RocksDB
.DEFAULT_COLUMN_FAMILY
),
719 new ColumnFamilyDescriptor("new_cf".getBytes(), new_cf_opts
)
723 final List
<ColumnFamilyHandle
> columnFamilyHandles
=
725 try (final RocksDB db
= RocksDB
.open(opt
,
726 dbFolder
.getRoot().getAbsolutePath(),
727 columnFamilyDescriptors
,
728 columnFamilyHandles
)) {
730 // fill database with key/value pairs
731 byte[] b
= new byte[10000];
732 for (int i
= 0; i
< 200; i
++) {
734 db
.put(columnFamilyHandles
.get(1),
735 String
.valueOf(i
).getBytes(), b
);
737 db
.compactRange(columnFamilyHandles
.get(1),
738 "0".getBytes(), "201".getBytes());
740 for (final ColumnFamilyHandle handle
: columnFamilyHandles
) {
749 public void compactRangeWithKeysReduceColumnFamily()
750 throws RocksDBException
{
751 try (final DBOptions opt
= new DBOptions().
752 setCreateIfMissing(true).
753 setCreateMissingColumnFamilies(true);
754 final ColumnFamilyOptions new_cf_opts
= new ColumnFamilyOptions().
755 setDisableAutoCompactions(true).
756 setCompactionStyle(CompactionStyle
.LEVEL
).
758 setWriteBufferSize(100 << 10).
759 setLevelZeroFileNumCompactionTrigger(3).
760 setTargetFileSizeBase(200 << 10).
761 setTargetFileSizeMultiplier(1).
762 setMaxBytesForLevelBase(500 << 10).
763 setMaxBytesForLevelMultiplier(1).
764 setDisableAutoCompactions(false)
766 final List
<ColumnFamilyDescriptor
> columnFamilyDescriptors
=
768 new ColumnFamilyDescriptor(RocksDB
.DEFAULT_COLUMN_FAMILY
),
769 new ColumnFamilyDescriptor("new_cf".getBytes(), new_cf_opts
)
772 final List
<ColumnFamilyHandle
> columnFamilyHandles
= new ArrayList
<>();
774 try (final RocksDB db
= RocksDB
.open(opt
,
775 dbFolder
.getRoot().getAbsolutePath(),
776 columnFamilyDescriptors
,
777 columnFamilyHandles
)) {
779 // fill database with key/value pairs
780 byte[] b
= new byte[10000];
781 for (int i
= 0; i
< 200; i
++) {
783 db
.put(columnFamilyHandles
.get(1),
784 String
.valueOf(i
).getBytes(), b
);
786 db
.compactRange(columnFamilyHandles
.get(1), "0".getBytes(),
787 "201".getBytes(), true, -1, 0);
789 for (final ColumnFamilyHandle handle
: columnFamilyHandles
) {
798 public void compactRangeToLevel()
799 throws RocksDBException
, InterruptedException
{
800 final int NUM_KEYS_PER_L0_FILE
= 100;
801 final int KEY_SIZE
= 20;
802 final int VALUE_SIZE
= 300;
803 final int L0_FILE_SIZE
=
804 NUM_KEYS_PER_L0_FILE
* (KEY_SIZE
+ VALUE_SIZE
);
805 final int NUM_L0_FILES
= 10;
806 final int TEST_SCALE
= 5;
807 final int KEY_INTERVAL
= 100;
808 try (final Options opt
= new Options().
809 setCreateIfMissing(true).
810 setCompactionStyle(CompactionStyle
.LEVEL
).
812 // a slightly bigger write buffer than L0 file
813 // so that we can ensure manual flush always
814 // go before background flush happens.
815 setWriteBufferSize(L0_FILE_SIZE
* 2).
816 // Disable auto L0 -> L1 compaction
817 setLevelZeroFileNumCompactionTrigger(20).
818 setTargetFileSizeBase(L0_FILE_SIZE
* 100).
819 setTargetFileSizeMultiplier(1).
820 // To disable auto compaction
821 setMaxBytesForLevelBase(NUM_L0_FILES
* L0_FILE_SIZE
* 100).
822 setMaxBytesForLevelMultiplier(2).
823 setDisableAutoCompactions(true);
824 final RocksDB db
= RocksDB
.open(opt
,
825 dbFolder
.getRoot().getAbsolutePath())
827 // fill database with key/value pairs
828 byte[] value
= new byte[VALUE_SIZE
];
830 for (int round
= 0; round
< 5; ++round
) {
831 int initial_key
= int_key
;
832 for (int f
= 1; f
<= NUM_L0_FILES
; ++f
) {
833 for (int i
= 0; i
< NUM_KEYS_PER_L0_FILE
; ++i
) {
834 int_key
+= KEY_INTERVAL
;
835 rand
.nextBytes(value
);
837 db
.put(String
.format("%020d", int_key
).getBytes(),
840 db
.flush(new FlushOptions().setWaitForFlush(true));
841 // Make sure we do create one more L0 files.
843 db
.getProperty("rocksdb.num-files-at-level0")).
847 // Compact all L0 files we just created
849 String
.format("%020d", initial_key
).getBytes(),
850 String
.format("%020d", int_key
- 1).getBytes());
851 // Making sure there isn't any L0 files.
853 db
.getProperty("rocksdb.num-files-at-level0")).
855 // Making sure there are some L1 files.
856 // Here we only use != 0 instead of a specific number
857 // as we don't want the test make any assumption on
858 // how compaction works.
860 db
.getProperty("rocksdb.num-files-at-level1")).
862 // Because we only compacted those keys we issued
863 // in this round, there shouldn't be any L1 -> L2
864 // compaction. So we expect zero L2 files here.
866 db
.getProperty("rocksdb.num-files-at-level2")).
873 public void compactRangeToLevelColumnFamily()
874 throws RocksDBException
{
875 final int NUM_KEYS_PER_L0_FILE
= 100;
876 final int KEY_SIZE
= 20;
877 final int VALUE_SIZE
= 300;
878 final int L0_FILE_SIZE
=
879 NUM_KEYS_PER_L0_FILE
* (KEY_SIZE
+ VALUE_SIZE
);
880 final int NUM_L0_FILES
= 10;
881 final int TEST_SCALE
= 5;
882 final int KEY_INTERVAL
= 100;
884 try (final DBOptions opt
= new DBOptions().
885 setCreateIfMissing(true).
886 setCreateMissingColumnFamilies(true);
887 final ColumnFamilyOptions new_cf_opts
= new ColumnFamilyOptions().
888 setCompactionStyle(CompactionStyle
.LEVEL
).
890 // a slightly bigger write buffer than L0 file
891 // so that we can ensure manual flush always
892 // go before background flush happens.
893 setWriteBufferSize(L0_FILE_SIZE
* 2).
894 // Disable auto L0 -> L1 compaction
895 setLevelZeroFileNumCompactionTrigger(20).
896 setTargetFileSizeBase(L0_FILE_SIZE
* 100).
897 setTargetFileSizeMultiplier(1).
898 // To disable auto compaction
899 setMaxBytesForLevelBase(NUM_L0_FILES
* L0_FILE_SIZE
* 100).
900 setMaxBytesForLevelMultiplier(2).
901 setDisableAutoCompactions(true)
903 final List
<ColumnFamilyDescriptor
> columnFamilyDescriptors
=
905 new ColumnFamilyDescriptor(RocksDB
.DEFAULT_COLUMN_FAMILY
),
906 new ColumnFamilyDescriptor("new_cf".getBytes(), new_cf_opts
)
909 final List
<ColumnFamilyHandle
> columnFamilyHandles
= new ArrayList
<>();
911 try (final RocksDB db
= RocksDB
.open(opt
,
912 dbFolder
.getRoot().getAbsolutePath(),
913 columnFamilyDescriptors
,
914 columnFamilyHandles
)) {
916 // fill database with key/value pairs
917 byte[] value
= new byte[VALUE_SIZE
];
919 for (int round
= 0; round
< 5; ++round
) {
920 int initial_key
= int_key
;
921 for (int f
= 1; f
<= NUM_L0_FILES
; ++f
) {
922 for (int i
= 0; i
< NUM_KEYS_PER_L0_FILE
; ++i
) {
923 int_key
+= KEY_INTERVAL
;
924 rand
.nextBytes(value
);
926 db
.put(columnFamilyHandles
.get(1),
927 String
.format("%020d", int_key
).getBytes(),
930 db
.flush(new FlushOptions().setWaitForFlush(true),
931 columnFamilyHandles
.get(1));
932 // Make sure we do create one more L0 files.
934 db
.getProperty(columnFamilyHandles
.get(1),
935 "rocksdb.num-files-at-level0")).
939 // Compact all L0 files we just created
941 columnFamilyHandles
.get(1),
942 String
.format("%020d", initial_key
).getBytes(),
943 String
.format("%020d", int_key
- 1).getBytes());
944 // Making sure there isn't any L0 files.
946 db
.getProperty(columnFamilyHandles
.get(1),
947 "rocksdb.num-files-at-level0")).
949 // Making sure there are some L1 files.
950 // Here we only use != 0 instead of a specific number
951 // as we don't want the test make any assumption on
952 // how compaction works.
954 db
.getProperty(columnFamilyHandles
.get(1),
955 "rocksdb.num-files-at-level1")).
957 // Because we only compacted those keys we issued
958 // in this round, there shouldn't be any L1 -> L2
959 // compaction. So we expect zero L2 files here.
961 db
.getProperty(columnFamilyHandles
.get(1),
962 "rocksdb.num-files-at-level2")).
966 for (final ColumnFamilyHandle handle
: columnFamilyHandles
) {
975 public void pauseContinueBackgroundWork() throws RocksDBException
{
976 try (final Options options
= new Options().setCreateIfMissing(true);
977 final RocksDB db
= RocksDB
.open(options
,
978 dbFolder
.getRoot().getAbsolutePath())
980 db
.pauseBackgroundWork();
981 db
.continueBackgroundWork();
982 db
.pauseBackgroundWork();
983 db
.continueBackgroundWork();
988 public void enableDisableFileDeletions() throws RocksDBException
{
989 try (final Options options
= new Options().setCreateIfMissing(true);
990 final RocksDB db
= RocksDB
.open(options
,
991 dbFolder
.getRoot().getAbsolutePath())
993 db
.disableFileDeletions();
994 db
.enableFileDeletions(false);
995 db
.disableFileDeletions();
996 db
.enableFileDeletions(true);
1001 public void setOptions() throws RocksDBException
{
1002 try (final DBOptions options
= new DBOptions()
1003 .setCreateIfMissing(true)
1004 .setCreateMissingColumnFamilies(true);
1005 final ColumnFamilyOptions new_cf_opts
= new ColumnFamilyOptions()
1006 .setWriteBufferSize(4096)) {
1008 final List
<ColumnFamilyDescriptor
> columnFamilyDescriptors
=
1010 new ColumnFamilyDescriptor(RocksDB
.DEFAULT_COLUMN_FAMILY
),
1011 new ColumnFamilyDescriptor("new_cf".getBytes(), new_cf_opts
));
1014 final List
<ColumnFamilyHandle
> columnFamilyHandles
= new ArrayList
<>();
1015 try (final RocksDB db
= RocksDB
.open(options
,
1016 dbFolder
.getRoot().getAbsolutePath(), columnFamilyDescriptors
, columnFamilyHandles
)) {
1018 final MutableColumnFamilyOptions mutableOptions
=
1019 MutableColumnFamilyOptions
.builder()
1020 .setWriteBufferSize(2048)
1023 db
.setOptions(columnFamilyHandles
.get(1), mutableOptions
);
1026 for (final ColumnFamilyHandle handle
: columnFamilyHandles
) {
1035 public void destroyDB() throws RocksDBException
{
1036 try (final Options options
= new Options().setCreateIfMissing(true)) {
1037 String dbPath
= dbFolder
.getRoot().getAbsolutePath();
1038 try (final RocksDB db
= RocksDB
.open(options
, dbPath
)) {
1039 db
.put("key1".getBytes(), "value".getBytes());
1041 assertThat(dbFolder
.getRoot().exists()).isTrue();
1042 RocksDB
.destroyDB(dbPath
, options
);
1043 assertThat(dbFolder
.getRoot().exists()).isFalse();
1047 @Test(expected
= RocksDBException
.class)
1048 public void destroyDBFailIfOpen() throws RocksDBException
{
1049 try (final Options options
= new Options().setCreateIfMissing(true)) {
1050 String dbPath
= dbFolder
.getRoot().getAbsolutePath();
1051 try (final RocksDB db
= RocksDB
.open(options
, dbPath
)) {
1052 // Fails as the db is open and locked.
1053 RocksDB
.destroyDB(dbPath
, options
);
1058 @Ignore("This test crashes. Re-enable after fixing.")
1060 public void getApproximateSizes() throws RocksDBException
{
1061 final byte key1
[] = "key1".getBytes(UTF_8
);
1062 final byte key2
[] = "key2".getBytes(UTF_8
);
1063 final byte key3
[] = "key3".getBytes(UTF_8
);
1064 try (final Options options
= new Options().setCreateIfMissing(true)) {
1065 final String dbPath
= dbFolder
.getRoot().getAbsolutePath();
1066 try (final RocksDB db
= RocksDB
.open(options
, dbPath
)) {
1071 final long[] sizes
= db
.getApproximateSizes(
1073 new Range(new Slice(key1
), new Slice(key2
)),
1074 new Range(new Slice(key2
), new Slice(key3
))
1076 SizeApproximationFlag
.INCLUDE_FILES
,
1077 SizeApproximationFlag
.INCLUDE_MEMTABLES
);
1079 assertThat(sizes
.length
).isEqualTo(2);
1080 assertThat(sizes
[0]).isEqualTo(0);
1081 assertThat(sizes
[1]).isGreaterThanOrEqualTo(1);
1087 public void getApproximateMemTableStats() throws RocksDBException
{
1088 final byte key1
[] = "key1".getBytes(UTF_8
);
1089 final byte key2
[] = "key2".getBytes(UTF_8
);
1090 final byte key3
[] = "key3".getBytes(UTF_8
);
1091 try (final Options options
= new Options().setCreateIfMissing(true)) {
1092 final String dbPath
= dbFolder
.getRoot().getAbsolutePath();
1093 try (final RocksDB db
= RocksDB
.open(options
, dbPath
)) {
1098 final RocksDB
.CountAndSize stats
=
1099 db
.getApproximateMemTableStats(
1100 new Range(new Slice(key1
), new Slice(key3
)));
1102 assertThat(stats
).isNotNull();
1103 assertThat(stats
.count
).isGreaterThan(1);
1104 assertThat(stats
.size
).isGreaterThan(1);
1109 @Ignore("TODO(AR) re-enable when ready!")
1111 public void compactFiles() throws RocksDBException
{
1112 final int kTestKeySize
= 16;
1113 final int kTestValueSize
= 984;
1114 final int kEntrySize
= kTestKeySize
+ kTestValueSize
;
1115 final int kEntriesPerBuffer
= 100;
1116 final int writeBufferSize
= kEntrySize
* kEntriesPerBuffer
;
1117 final byte[] cfName
= "pikachu".getBytes(UTF_8
);
1119 try (final Options options
= new Options()
1120 .setCreateIfMissing(true)
1121 .setWriteBufferSize(writeBufferSize
)
1122 .setCompactionStyle(CompactionStyle
.LEVEL
)
1123 .setTargetFileSizeBase(writeBufferSize
)
1124 .setMaxBytesForLevelBase(writeBufferSize
* 2)
1125 .setLevel0StopWritesTrigger(2)
1126 .setMaxBytesForLevelMultiplier(2)
1127 .setCompressionType(CompressionType
.NO_COMPRESSION
)
1128 .setMaxSubcompactions(4)) {
1129 final String dbPath
= dbFolder
.getRoot().getAbsolutePath();
1130 try (final RocksDB db
= RocksDB
.open(options
, dbPath
);
1131 final ColumnFamilyOptions cfOptions
= new ColumnFamilyOptions(options
)) {
1132 db
.createColumnFamily(new ColumnFamilyDescriptor(cfName
,
1133 cfOptions
)).close();
1136 try (final ColumnFamilyOptions cfOptions
= new ColumnFamilyOptions(options
)) {
1137 final List
<ColumnFamilyDescriptor
> cfDescriptors
= Arrays
.asList(
1138 new ColumnFamilyDescriptor(RocksDB
.DEFAULT_COLUMN_FAMILY
, cfOptions
),
1139 new ColumnFamilyDescriptor(cfName
, cfOptions
)
1141 final List
<ColumnFamilyHandle
> cfHandles
= new ArrayList
<>();
1142 try (final DBOptions dbOptions
= new DBOptions(options
);
1143 final RocksDB db
= RocksDB
.open(dbOptions
, dbPath
, cfDescriptors
,
1146 try (final FlushOptions flushOptions
= new FlushOptions()
1147 .setWaitForFlush(true)
1148 .setAllowWriteStall(true);
1149 final CompactionOptions compactionOptions
= new CompactionOptions()) {
1150 final Random rnd
= new Random(301);
1151 for (int key
= 64 * kEntriesPerBuffer
; key
>= 0; --key
) {
1152 final byte[] value
= new byte[kTestValueSize
];
1153 rnd
.nextBytes(value
);
1154 db
.put(cfHandles
.get(1), Integer
.toString(key
).getBytes(UTF_8
),
1157 db
.flush(flushOptions
, cfHandles
);
1159 final RocksDB
.LiveFiles liveFiles
= db
.getLiveFiles();
1160 final List
<String
> compactedFiles
=
1161 db
.compactFiles(compactionOptions
, cfHandles
.get(1),
1162 liveFiles
.files
, 1, -1, null);
1163 assertThat(compactedFiles
).isNotEmpty();
1165 for (final ColumnFamilyHandle cfHandle
: cfHandles
) {
1175 public void enableAutoCompaction() throws RocksDBException
{
1176 try (final DBOptions options
= new DBOptions()
1177 .setCreateIfMissing(true)) {
1178 final List
<ColumnFamilyDescriptor
> cfDescs
= Arrays
.asList(
1179 new ColumnFamilyDescriptor(RocksDB
.DEFAULT_COLUMN_FAMILY
)
1181 final List
<ColumnFamilyHandle
> cfHandles
= new ArrayList
<>();
1182 final String dbPath
= dbFolder
.getRoot().getAbsolutePath();
1183 try (final RocksDB db
= RocksDB
.open(options
, dbPath
, cfDescs
, cfHandles
)) {
1185 db
.enableAutoCompaction(cfHandles
);
1187 for (final ColumnFamilyHandle cfHandle
: cfHandles
) {
1196 public void numberLevels() throws RocksDBException
{
1197 try (final Options options
= new Options().setCreateIfMissing(true)) {
1198 final String dbPath
= dbFolder
.getRoot().getAbsolutePath();
1199 try (final RocksDB db
= RocksDB
.open(options
, dbPath
)) {
1200 assertThat(db
.numberLevels()).isEqualTo(7);
1206 public void maxMemCompactionLevel() throws RocksDBException
{
1207 try (final Options options
= new Options().setCreateIfMissing(true)) {
1208 final String dbPath
= dbFolder
.getRoot().getAbsolutePath();
1209 try (final RocksDB db
= RocksDB
.open(options
, dbPath
)) {
1210 assertThat(db
.maxMemCompactionLevel()).isEqualTo(0);
1216 public void level0StopWriteTrigger() throws RocksDBException
{
1217 try (final Options options
= new Options().setCreateIfMissing(true)) {
1218 final String dbPath
= dbFolder
.getRoot().getAbsolutePath();
1219 try (final RocksDB db
= RocksDB
.open(options
, dbPath
)) {
1220 assertThat(db
.level0StopWriteTrigger()).isEqualTo(36);
1226 public void getName() throws RocksDBException
{
1227 try (final Options options
= new Options().setCreateIfMissing(true)) {
1228 final String dbPath
= dbFolder
.getRoot().getAbsolutePath();
1229 try (final RocksDB db
= RocksDB
.open(options
, dbPath
)) {
1230 assertThat(db
.getName()).isEqualTo(dbPath
);
1236 public void getEnv() throws RocksDBException
{
1237 try (final Options options
= new Options().setCreateIfMissing(true)) {
1238 final String dbPath
= dbFolder
.getRoot().getAbsolutePath();
1239 try (final RocksDB db
= RocksDB
.open(options
, dbPath
)) {
1240 assertThat(db
.getEnv()).isEqualTo(Env
.getDefault());
1246 public void flush() throws RocksDBException
{
1247 try (final Options options
= new Options().setCreateIfMissing(true)) {
1248 final String dbPath
= dbFolder
.getRoot().getAbsolutePath();
1249 try (final RocksDB db
= RocksDB
.open(options
, dbPath
);
1250 final FlushOptions flushOptions
= new FlushOptions()) {
1251 db
.flush(flushOptions
);
1257 public void flushWal() throws RocksDBException
{
1258 try (final Options options
= new Options().setCreateIfMissing(true)) {
1259 final String dbPath
= dbFolder
.getRoot().getAbsolutePath();
1260 try (final RocksDB db
= RocksDB
.open(options
, dbPath
)) {
1267 public void syncWal() throws RocksDBException
{
1268 try (final Options options
= new Options().setCreateIfMissing(true)) {
1269 final String dbPath
= dbFolder
.getRoot().getAbsolutePath();
1270 try (final RocksDB db
= RocksDB
.open(options
, dbPath
)) {
1277 public void setPreserveDeletesSequenceNumber() throws RocksDBException
{
1278 try (final Options options
= new Options().setCreateIfMissing(true)) {
1279 final String dbPath
= dbFolder
.getRoot().getAbsolutePath();
1280 try (final RocksDB db
= RocksDB
.open(options
, dbPath
)) {
1281 assertThat(db
.setPreserveDeletesSequenceNumber(db
.getLatestSequenceNumber()))
1288 public void getLiveFiles() throws RocksDBException
{
1289 try (final Options options
= new Options().setCreateIfMissing(true)) {
1290 final String dbPath
= dbFolder
.getRoot().getAbsolutePath();
1291 try (final RocksDB db
= RocksDB
.open(options
, dbPath
)) {
1292 final RocksDB
.LiveFiles livefiles
= db
.getLiveFiles(true);
1293 assertThat(livefiles
).isNotNull();
1294 assertThat(livefiles
.manifestFileSize
).isEqualTo(13);
1295 assertThat(livefiles
.files
.size()).isEqualTo(3);
1296 assertThat(livefiles
.files
.get(0)).isEqualTo("/CURRENT");
1297 assertThat(livefiles
.files
.get(1)).isEqualTo("/MANIFEST-000001");
1298 assertThat(livefiles
.files
.get(2)).isEqualTo("/OPTIONS-000005");
1304 public void getSortedWalFiles() throws RocksDBException
{
1305 try (final Options options
= new Options().setCreateIfMissing(true)) {
1306 final String dbPath
= dbFolder
.getRoot().getAbsolutePath();
1307 try (final RocksDB db
= RocksDB
.open(options
, dbPath
)) {
1308 db
.put("key1".getBytes(UTF_8
), "value1".getBytes(UTF_8
));
1309 final List
<LogFile
> logFiles
= db
.getSortedWalFiles();
1310 assertThat(logFiles
).isNotNull();
1311 assertThat(logFiles
.size()).isEqualTo(1);
1312 assertThat(logFiles
.get(0).type())
1313 .isEqualTo(WalFileType
.kAliveLogFile
);
1319 public void deleteFile() throws RocksDBException
{
1320 try (final Options options
= new Options().setCreateIfMissing(true)) {
1321 final String dbPath
= dbFolder
.getRoot().getAbsolutePath();
1322 try (final RocksDB db
= RocksDB
.open(options
, dbPath
)) {
1323 db
.deleteFile("unknown");
1329 public void getLiveFilesMetaData() throws RocksDBException
{
1330 try (final Options options
= new Options().setCreateIfMissing(true)) {
1331 final String dbPath
= dbFolder
.getRoot().getAbsolutePath();
1332 try (final RocksDB db
= RocksDB
.open(options
, dbPath
)) {
1333 db
.put("key1".getBytes(UTF_8
), "value1".getBytes(UTF_8
));
1334 final List
<LiveFileMetaData
> liveFilesMetaData
1335 = db
.getLiveFilesMetaData();
1336 assertThat(liveFilesMetaData
).isEmpty();
1342 public void getColumnFamilyMetaData() throws RocksDBException
{
1343 try (final DBOptions options
= new DBOptions()
1344 .setCreateIfMissing(true)) {
1345 final List
<ColumnFamilyDescriptor
> cfDescs
= Arrays
.asList(
1346 new ColumnFamilyDescriptor(RocksDB
.DEFAULT_COLUMN_FAMILY
)
1348 final List
<ColumnFamilyHandle
> cfHandles
= new ArrayList
<>();
1349 final String dbPath
= dbFolder
.getRoot().getAbsolutePath();
1350 try (final RocksDB db
= RocksDB
.open(options
, dbPath
, cfDescs
, cfHandles
)) {
1351 db
.put(cfHandles
.get(0), "key1".getBytes(UTF_8
), "value1".getBytes(UTF_8
));
1353 final ColumnFamilyMetaData cfMetadata
=
1354 db
.getColumnFamilyMetaData(cfHandles
.get(0));
1355 assertThat(cfMetadata
).isNotNull();
1356 assertThat(cfMetadata
.name()).isEqualTo(RocksDB
.DEFAULT_COLUMN_FAMILY
);
1357 assertThat(cfMetadata
.levels().size()).isEqualTo(7);
1359 for (final ColumnFamilyHandle cfHandle
: cfHandles
) {
1368 public void verifyChecksum() throws RocksDBException
{
1369 try (final Options options
= new Options().setCreateIfMissing(true)) {
1370 final String dbPath
= dbFolder
.getRoot().getAbsolutePath();
1371 try (final RocksDB db
= RocksDB
.open(options
, dbPath
)) {
1372 db
.verifyChecksum();
1378 public void getPropertiesOfAllTables() throws RocksDBException
{
1379 try (final DBOptions options
= new DBOptions()
1380 .setCreateIfMissing(true)) {
1381 final List
<ColumnFamilyDescriptor
> cfDescs
= Arrays
.asList(
1382 new ColumnFamilyDescriptor(RocksDB
.DEFAULT_COLUMN_FAMILY
)
1384 final List
<ColumnFamilyHandle
> cfHandles
= new ArrayList
<>();
1385 final String dbPath
= dbFolder
.getRoot().getAbsolutePath();
1386 try (final RocksDB db
= RocksDB
.open(options
, dbPath
, cfDescs
, cfHandles
)) {
1387 db
.put(cfHandles
.get(0), "key1".getBytes(UTF_8
), "value1".getBytes(UTF_8
));
1389 final Map
<String
, TableProperties
> properties
=
1390 db
.getPropertiesOfAllTables(cfHandles
.get(0));
1391 assertThat(properties
).isNotNull();
1393 for (final ColumnFamilyHandle cfHandle
: cfHandles
) {
1402 public void getPropertiesOfTablesInRange() throws RocksDBException
{
1403 try (final DBOptions options
= new DBOptions()
1404 .setCreateIfMissing(true)) {
1405 final List
<ColumnFamilyDescriptor
> cfDescs
= Arrays
.asList(
1406 new ColumnFamilyDescriptor(RocksDB
.DEFAULT_COLUMN_FAMILY
)
1408 final List
<ColumnFamilyHandle
> cfHandles
= new ArrayList
<>();
1409 final String dbPath
= dbFolder
.getRoot().getAbsolutePath();
1410 try (final RocksDB db
= RocksDB
.open(options
, dbPath
, cfDescs
, cfHandles
)) {
1411 db
.put(cfHandles
.get(0), "key1".getBytes(UTF_8
), "value1".getBytes(UTF_8
));
1412 db
.put(cfHandles
.get(0), "key2".getBytes(UTF_8
), "value2".getBytes(UTF_8
));
1413 db
.put(cfHandles
.get(0), "key3".getBytes(UTF_8
), "value3".getBytes(UTF_8
));
1415 final Range range
= new Range(
1416 new Slice("key1".getBytes(UTF_8
)),
1417 new Slice("key3".getBytes(UTF_8
)));
1418 final Map
<String
, TableProperties
> properties
=
1419 db
.getPropertiesOfTablesInRange(
1420 cfHandles
.get(0), Arrays
.asList(range
));
1421 assertThat(properties
).isNotNull();
1423 for (final ColumnFamilyHandle cfHandle
: cfHandles
) {
1432 public void suggestCompactRange() throws RocksDBException
{
1433 try (final DBOptions options
= new DBOptions()
1434 .setCreateIfMissing(true)) {
1435 final List
<ColumnFamilyDescriptor
> cfDescs
= Arrays
.asList(
1436 new ColumnFamilyDescriptor(RocksDB
.DEFAULT_COLUMN_FAMILY
)
1438 final List
<ColumnFamilyHandle
> cfHandles
= new ArrayList
<>();
1439 final String dbPath
= dbFolder
.getRoot().getAbsolutePath();
1440 try (final RocksDB db
= RocksDB
.open(options
, dbPath
, cfDescs
, cfHandles
)) {
1441 db
.put(cfHandles
.get(0), "key1".getBytes(UTF_8
), "value1".getBytes(UTF_8
));
1442 db
.put(cfHandles
.get(0), "key2".getBytes(UTF_8
), "value2".getBytes(UTF_8
));
1443 db
.put(cfHandles
.get(0), "key3".getBytes(UTF_8
), "value3".getBytes(UTF_8
));
1445 final Range range
= db
.suggestCompactRange(cfHandles
.get(0));
1446 assertThat(range
).isNotNull();
1448 for (final ColumnFamilyHandle cfHandle
: cfHandles
) {
1457 public void promoteL0() throws RocksDBException
{
1458 try (final Options options
= new Options().setCreateIfMissing(true)) {
1459 final String dbPath
= dbFolder
.getRoot().getAbsolutePath();
1460 try (final RocksDB db
= RocksDB
.open(options
, dbPath
)) {
1467 public void startTrace() throws RocksDBException
{
1468 try (final Options options
= new Options().setCreateIfMissing(true)) {
1469 final String dbPath
= dbFolder
.getRoot().getAbsolutePath();
1470 try (final RocksDB db
= RocksDB
.open(options
, dbPath
)) {
1471 final TraceOptions traceOptions
= new TraceOptions();
1473 try (final InMemoryTraceWriter traceWriter
= new InMemoryTraceWriter()) {
1474 db
.startTrace(traceOptions
, traceWriter
);
1476 db
.put("key1".getBytes(UTF_8
), "value1".getBytes(UTF_8
));
1480 final List
<byte[]> writes
= traceWriter
.getWrites();
1481 assertThat(writes
.size()).isGreaterThan(0);
1488 public void setDBOptions() throws RocksDBException
{
1489 try (final DBOptions options
= new DBOptions()
1490 .setCreateIfMissing(true)
1491 .setCreateMissingColumnFamilies(true);
1492 final ColumnFamilyOptions new_cf_opts
= new ColumnFamilyOptions()
1493 .setWriteBufferSize(4096)) {
1495 final List
<ColumnFamilyDescriptor
> columnFamilyDescriptors
=
1497 new ColumnFamilyDescriptor(RocksDB
.DEFAULT_COLUMN_FAMILY
),
1498 new ColumnFamilyDescriptor("new_cf".getBytes(), new_cf_opts
));
1501 final List
<ColumnFamilyHandle
> columnFamilyHandles
= new ArrayList
<>();
1502 try (final RocksDB db
= RocksDB
.open(options
,
1503 dbFolder
.getRoot().getAbsolutePath(), columnFamilyDescriptors
, columnFamilyHandles
)) {
1505 final MutableDBOptions mutableOptions
=
1506 MutableDBOptions
.builder()
1507 .setBytesPerSync(1024 * 1027 * 7)
1508 .setAvoidFlushDuringShutdown(false)
1511 db
.setDBOptions(mutableOptions
);
1513 for (final ColumnFamilyHandle handle
: columnFamilyHandles
) {
1521 private static class InMemoryTraceWriter
extends AbstractTraceWriter
{
1522 private final List
<byte[]> writes
= new ArrayList
<>();
1523 private volatile boolean closed
= false;
1526 public void write(final Slice slice
) {
1530 final byte[] data
= slice
.data();
1531 final byte[] dataCopy
= new byte[data
.length
];
1532 System
.arraycopy(data
, 0, dataCopy
, 0, data
.length
);
1533 writes
.add(dataCopy
);
1537 public void closeWriter() {
1542 public long getFileSize() {
1544 for (int i
= 0; i
< writes
.size(); i
++) {
1545 size
+= writes
.get(i
).length
;
1550 public List
<byte[]> getWrites() {