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).
9 * CompactRangeOptions is used by CompactRange() call. In the documentation of the methods "the compaction" refers to
10 * any compaction that is using this CompactRangeOptions.
12 public class CompactRangeOptions
extends RocksObject
{
14 private final static byte VALUE_kSkip
= 0;
15 private final static byte VALUE_kIfHaveCompactionFilter
= 1;
16 private final static byte VALUE_kForce
= 2;
18 // For level based compaction, we can configure if we want to skip/force bottommost level compaction.
19 // The order of this neum MUST follow the C++ layer. See BottommostLevelCompaction in db/options.h
20 public enum BottommostLevelCompaction
{
22 * Skip bottommost level compaction
24 kSkip((byte)VALUE_kSkip
),
26 * Only compact bottommost level if there is a compaction filter. This is the default option
28 kIfHaveCompactionFilter(VALUE_kIfHaveCompactionFilter
),
30 * Always compact bottommost level
34 private final byte value
;
36 BottommostLevelCompaction(final byte value
) {
41 * <p>Returns the byte value of the enumerations value.</p>
43 * @return byte representation
45 public byte getValue() {
50 * Returns the BottommostLevelCompaction for the given C++ rocks enum value.
51 * @param bottommostLevelCompaction The value of the BottommostLevelCompaction
52 * @return BottommostLevelCompaction instance, or null if none matches
54 public static BottommostLevelCompaction
fromRocksId(final int bottommostLevelCompaction
) {
55 switch (bottommostLevelCompaction
) {
56 case VALUE_kSkip
: return kSkip
;
57 case VALUE_kIfHaveCompactionFilter
: return kIfHaveCompactionFilter
;
58 case VALUE_kForce
: return kForce
;
65 * Construct CompactRangeOptions.
67 public CompactRangeOptions() {
68 super(newCompactRangeOptions());
72 * Returns whether the compaction is exclusive or other compactions may run concurrently at the same time.
74 * @return true if exclusive, false if concurrent
76 public boolean exclusiveManualCompaction() {
77 return exclusiveManualCompaction(nativeHandle_
);
81 * Sets whether the compaction is exclusive or other compaction are allowed run concurrently at the same time.
83 * @param exclusiveCompaction true if compaction should be exclusive
84 * @return This CompactRangeOptions
86 public CompactRangeOptions
setExclusiveManualCompaction(final boolean exclusiveCompaction
) {
87 setExclusiveManualCompaction(nativeHandle_
, exclusiveCompaction
);
92 * Returns whether compacted files will be moved to the minimum level capable of holding the data or given level
93 * (specified non-negative target_level).
94 * @return true, if compacted files will be moved to the minimum level
96 public boolean changeLevel() {
97 return changeLevel(nativeHandle_
);
101 * Whether compacted files will be moved to the minimum level capable of holding the data or given level
102 * (specified non-negative target_level).
104 * @param changeLevel If true, compacted files will be moved to the minimum level
105 * @return This CompactRangeOptions
107 public CompactRangeOptions
setChangeLevel(final boolean changeLevel
) {
108 setChangeLevel(nativeHandle_
, changeLevel
);
113 * If change_level is true and target_level have non-negative value, compacted files will be moved to target_level.
114 * @return The target level for the compacted files
116 public int targetLevel() {
117 return targetLevel(nativeHandle_
);
122 * If change_level is true and target_level have non-negative value, compacted files will be moved to target_level.
124 * @param targetLevel target level for the compacted files
125 * @return This CompactRangeOptions
127 public CompactRangeOptions
setTargetLevel(final int targetLevel
) {
128 setTargetLevel(nativeHandle_
, targetLevel
);
133 * target_path_id for compaction output. Compaction outputs will be placed in options.db_paths[target_path_id].
135 * @return target_path_id
137 public int targetPathId() {
138 return targetPathId(nativeHandle_
);
142 * Compaction outputs will be placed in options.db_paths[target_path_id]. Behavior is undefined if target_path_id is
145 * @param targetPathId target path id
146 * @return This CompactRangeOptions
148 public CompactRangeOptions
setTargetPathId(final int targetPathId
) {
149 setTargetPathId(nativeHandle_
, targetPathId
);
154 * Returns the policy for compacting the bottommost level
155 * @return The BottommostLevelCompaction policy
157 public BottommostLevelCompaction
bottommostLevelCompaction() {
158 return BottommostLevelCompaction
.fromRocksId(bottommostLevelCompaction(nativeHandle_
));
162 * Sets the policy for compacting the bottommost level
164 * @param bottommostLevelCompaction The policy for compacting the bottommost level
165 * @return This CompactRangeOptions
167 public CompactRangeOptions
setBottommostLevelCompaction(final BottommostLevelCompaction bottommostLevelCompaction
) {
168 setBottommostLevelCompaction(nativeHandle_
, bottommostLevelCompaction
.getValue());
173 * If true, compaction will execute immediately even if doing so would cause the DB to
174 * enter write stall mode. Otherwise, it'll sleep until load is low enough.
175 * @return true if compaction will execute immediately
177 public boolean allowWriteStall() {
178 return allowWriteStall(nativeHandle_
);
183 * If true, compaction will execute immediately even if doing so would cause the DB to
184 * enter write stall mode. Otherwise, it'll sleep until load is low enough.
186 * @return This CompactRangeOptions
187 * @param allowWriteStall true if compaction should execute immediately
189 public CompactRangeOptions
setAllowWriteStall(final boolean allowWriteStall
) {
190 setAllowWriteStall(nativeHandle_
, allowWriteStall
);
195 * If > 0, it will replace the option in the DBOptions for this compaction
196 * @return number of subcompactions
198 public int maxSubcompactions() {
199 return maxSubcompactions(nativeHandle_
);
203 * If > 0, it will replace the option in the DBOptions for this compaction
205 * @param maxSubcompactions number of subcompactions
206 * @return This CompactRangeOptions
208 public CompactRangeOptions
setMaxSubcompactions(final int maxSubcompactions
) {
209 setMaxSubcompactions(nativeHandle_
, maxSubcompactions
);
213 private native static long newCompactRangeOptions();
214 @Override protected final native void disposeInternal(final long handle
);
216 private native boolean exclusiveManualCompaction(final long handle
);
217 private native void setExclusiveManualCompaction(final long handle
,
218 final boolean exclusive_manual_compaction
);
219 private native boolean changeLevel(final long handle
);
220 private native void setChangeLevel(final long handle
,
221 final boolean changeLevel
);
222 private native int targetLevel(final long handle
);
223 private native void setTargetLevel(final long handle
,
224 final int targetLevel
);
225 private native int targetPathId(final long handle
);
226 private native void setTargetPathId(final long handle
,
227 final int targetPathId
);
228 private native int bottommostLevelCompaction(final long handle
);
229 private native void setBottommostLevelCompaction(final long handle
,
230 final int bottommostLevelCompaction
);
231 private native boolean allowWriteStall(final long handle
);
232 private native void setAllowWriteStall(final long handle
,
233 final boolean allowWriteStall
);
234 private native void setMaxSubcompactions(final long handle
,
235 final int maxSubcompactions
);
236 private native int maxSubcompactions(final long handle
);