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).
5 
6 package org.rocksdb;
7 
8 import org.junit.Rule;
9 import org.junit.Test;
10 import org.junit.rules.TemporaryFolder;
11 import org.rocksdb.test.RemoveEmptyValueCompactionFilterFactory;
12 
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.List;
16 
17 import static org.assertj.core.api.Assertions.assertThat;
18 
19 public class CompactionFilterFactoryTest {
20 
21   @Rule
22   public TemporaryFolder dbFolder = new TemporaryFolder();
23 
24   @Test
columnFamilyOptions_setCompactionFilterFactory()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)) {
35 
36       final List<ColumnFamilyDescriptor> cfNames = Arrays.asList(
37           new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
38           new ColumnFamilyDescriptor("new_cf".getBytes(), new_cf_opts));
39 
40       final List<ColumnFamilyHandle> cfHandles = new ArrayList<>();
41 
42       try (final RocksDB rocksDb = RocksDB.open(options,
43                dbFolder.getRoot().getAbsolutePath(), cfNames, cfHandles);
44       ) {
45         try {
46           final byte[] key1 = "key1".getBytes();
47           final byte[] key2 = "key2".getBytes();
48 
49           final byte[] value1 = "value1".getBytes();
50           final byte[] value2 = new byte[0];
51 
52           rocksDb.put(cfHandles.get(1), key1, value1);
53           rocksDb.put(cfHandles.get(1), key2, value2);
54 
55           rocksDb.compactRange(cfHandles.get(1));
56 
57           assertThat(rocksDb.get(cfHandles.get(1), key1)).isEqualTo(value1);
58           final boolean exists = rocksDb.keyMayExist(cfHandles.get(1), key2, null);
59           assertThat(exists).isFalse();
60         } finally {
61           for (final ColumnFamilyHandle cfHandle : cfHandles) {
62             cfHandle.close();
63           }
64         }
65       }
66     }
67   }
68 }
69