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 /**
9  * Each compaction will create a new {@link AbstractCompactionFilter}
10  * allowing the application to know about different compactions
11  *
12  * @param <T> The concrete type of the compaction filter
13  */
14 public abstract class AbstractCompactionFilterFactory<T extends AbstractCompactionFilter<?>>
15     extends RocksCallbackObject {
16 
AbstractCompactionFilterFactory()17   public AbstractCompactionFilterFactory() {
18     super(null);
19   }
20 
21   @Override
initializeNative(final long... nativeParameterHandles)22   protected long initializeNative(final long... nativeParameterHandles) {
23     return createNewCompactionFilterFactory0();
24   }
25 
26   /**
27    * Called from JNI, see compaction_filter_factory_jnicallback.cc
28    *
29    * @param fullCompaction {@link AbstractCompactionFilter.Context#fullCompaction}
30    * @param manualCompaction {@link AbstractCompactionFilter.Context#manualCompaction}
31    *
32    * @return native handle of the CompactionFilter
33    */
createCompactionFilter(final boolean fullCompaction, final boolean manualCompaction)34   private long createCompactionFilter(final boolean fullCompaction,
35       final boolean manualCompaction) {
36     final T filter = createCompactionFilter(
37         new AbstractCompactionFilter.Context(fullCompaction, manualCompaction));
38 
39     // CompactionFilterFactory::CreateCompactionFilter returns a std::unique_ptr
40     // which therefore has ownership of the underlying native object
41     filter.disOwnNativeHandle();
42 
43     return filter.nativeHandle_;
44   }
45 
46   /**
47    * Create a new compaction filter
48    *
49    * @param context The context describing the need for a new compaction filter
50    *
51    * @return A new instance of {@link AbstractCompactionFilter}
52    */
createCompactionFilter( final AbstractCompactionFilter.Context context)53   public abstract T createCompactionFilter(
54       final AbstractCompactionFilter.Context context);
55 
56   /**
57    * A name which identifies this compaction filter
58    *
59    * The name will be printed to the LOG file on start up for diagnosis
60    *
61    * @return name which identifies this compaction filter.
62    */
name()63   public abstract String name();
64 
65   /**
66    * We override {@link RocksCallbackObject#disposeInternal()}
67    * as disposing of a rocksdb::AbstractCompactionFilterFactory requires
68    * a slightly different approach as it is a std::shared_ptr
69    */
70   @Override
disposeInternal()71   protected void disposeInternal() {
72     disposeInternal(nativeHandle_);
73   }
74 
createNewCompactionFilterFactory0()75   private native long createNewCompactionFilterFactory0();
disposeInternal(final long handle)76   private native void disposeInternal(final long handle);
77 }
78