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).
10 import org
.junit
.rules
.TemporaryFolder
;
11 import org
.rocksdb
.test
.RemoveEmptyValueCompactionFilterFactory
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Arrays
;
15 import java
.util
.List
;
17 import static org
.assertj
.core
.api
.Assertions
.assertThat
;
19 public class CompactionFilterFactoryTest
{
22 public TemporaryFolder dbFolder
= new TemporaryFolder();
25 public void columnFamilyOptions_setCompactionFilterFactory()
26 throws RocksDBException
{
27 try(final DBOptions options
= new DBOptions()
28 .setCreateIfMissing(true)
29 .setCreateMissingColumnFamilies(true);
30 final RemoveEmptyValueCompactionFilterFactory compactionFilterFactory
31 = new RemoveEmptyValueCompactionFilterFactory();
32 final ColumnFamilyOptions new_cf_opts
33 = new ColumnFamilyOptions()
34 .setCompactionFilterFactory(compactionFilterFactory
)) {
36 final List
<ColumnFamilyDescriptor
> cfNames
= Arrays
.asList(
37 new ColumnFamilyDescriptor(RocksDB
.DEFAULT_COLUMN_FAMILY
),
38 new ColumnFamilyDescriptor("new_cf".getBytes(), new_cf_opts
));
40 final List
<ColumnFamilyHandle
> cfHandles
= new ArrayList
<>();
42 try (final RocksDB rocksDb
=
43 RocksDB
.open(options
, dbFolder
.getRoot().getAbsolutePath(), cfNames
, cfHandles
)) {
44 final byte[] key1
= "key1".getBytes();
45 final byte[] key2
= "key2".getBytes();
47 final byte[] value1
= "value1".getBytes();
48 final byte[] value2
= new byte[0];
50 rocksDb
.put(cfHandles
.get(1), key1
, value1
);
51 rocksDb
.put(cfHandles
.get(1), key2
, value2
);
53 rocksDb
.compactRange(cfHandles
.get(1));
55 assertThat(rocksDb
.get(cfHandles
.get(1), key1
)).isEqualTo(value1
);
56 final boolean exists
= rocksDb
.keyMayExist(cfHandles
.get(1), key2
, null);
57 assertThat(exists
).isFalse();