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  * CompactRangeOptions is used by CompactRange() call. In the documentation of the methods "the compaction" refers to
10  * any compaction that is using this CompactRangeOptions.
11  */
12 public class CompactRangeOptions extends RocksObject {
13 
14   private final static byte VALUE_kSkip = 0;
15   private final static byte VALUE_kIfHaveCompactionFilter = 1;
16   private final static byte VALUE_kForce = 2;
17 
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 {
21     /**
22      * Skip bottommost level compaction
23      */
24     kSkip((byte)VALUE_kSkip),
25     /**
26      * Only compact bottommost level if there is a compaction filter. This is the default option
27      */
28     kIfHaveCompactionFilter(VALUE_kIfHaveCompactionFilter),
29     /**
30      * Always compact bottommost level
31      */
32     kForce(VALUE_kForce);
33 
34     private final byte value;
35 
BottommostLevelCompaction(final byte value)36     BottommostLevelCompaction(final byte value) {
37       this.value = value;
38     }
39 
40     /**
41      * <p>Returns the byte value of the enumerations value.</p>
42      *
43      * @return byte representation
44      */
getValue()45     public byte getValue() {
46       return value;
47     }
48 
49     /**
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
53      */
fromRocksId(final int bottommostLevelCompaction)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;
59         default: return null;
60       }
61     }
62   }
63 
64   /**
65    * Construct CompactRangeOptions.
66    */
CompactRangeOptions()67   public CompactRangeOptions() {
68     super(newCompactRangeOptions());
69   }
70 
71   /**
72    * Returns whether the compaction is exclusive or other compactions may run concurrently at the same time.
73    *
74    * @return true if exclusive, false if concurrent
75    */
exclusiveManualCompaction()76   public boolean exclusiveManualCompaction() {
77     return exclusiveManualCompaction(nativeHandle_);
78   }
79 
80   /**
81    * Sets whether the compaction is exclusive or other compaction are allowed run concurrently at the same time.
82    *
83    * @param exclusiveCompaction true if compaction should be exclusive
84    * @return This CompactRangeOptions
85    */
setExclusiveManualCompaction(final boolean exclusiveCompaction)86   public CompactRangeOptions setExclusiveManualCompaction(final boolean exclusiveCompaction) {
87     setExclusiveManualCompaction(nativeHandle_, exclusiveCompaction);
88     return this;
89   }
90 
91   /**
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
95    */
changeLevel()96   public boolean changeLevel() {
97     return changeLevel(nativeHandle_);
98   }
99 
100   /**
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).
103    *
104    * @param changeLevel If true, compacted files will be moved to the minimum level
105    * @return This CompactRangeOptions
106    */
setChangeLevel(final boolean changeLevel)107   public CompactRangeOptions setChangeLevel(final boolean changeLevel) {
108     setChangeLevel(nativeHandle_, changeLevel);
109     return this;
110   }
111 
112   /**
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
115    */
targetLevel()116   public int targetLevel() {
117     return targetLevel(nativeHandle_);
118   }
119 
120 
121   /**
122    * If change_level is true and target_level have non-negative value, compacted files will be moved to target_level.
123    *
124    * @param targetLevel target level for the compacted files
125    * @return This CompactRangeOptions
126    */
setTargetLevel(final int targetLevel)127   public CompactRangeOptions setTargetLevel(final int targetLevel) {
128     setTargetLevel(nativeHandle_, targetLevel);
129     return this;
130   }
131 
132   /**
133    * target_path_id for compaction output. Compaction outputs will be placed in options.db_paths[target_path_id].
134    *
135    * @return target_path_id
136    */
targetPathId()137   public int targetPathId() {
138     return targetPathId(nativeHandle_);
139   }
140 
141   /**
142    * Compaction outputs will be placed in options.db_paths[target_path_id]. Behavior is undefined if target_path_id is
143    * out of range.
144    *
145    * @param targetPathId target path id
146    * @return This CompactRangeOptions
147    */
setTargetPathId(final int targetPathId)148   public CompactRangeOptions setTargetPathId(final int targetPathId) {
149     setTargetPathId(nativeHandle_, targetPathId);
150     return this;
151   }
152 
153   /**
154    * Returns the policy for compacting the bottommost level
155    * @return The BottommostLevelCompaction policy
156    */
bottommostLevelCompaction()157   public BottommostLevelCompaction bottommostLevelCompaction() {
158     return BottommostLevelCompaction.fromRocksId(bottommostLevelCompaction(nativeHandle_));
159   }
160 
161   /**
162    * Sets the policy for compacting the bottommost level
163    *
164    * @param bottommostLevelCompaction The policy for compacting the bottommost level
165    * @return This CompactRangeOptions
166    */
setBottommostLevelCompaction(final BottommostLevelCompaction bottommostLevelCompaction)167   public CompactRangeOptions setBottommostLevelCompaction(final BottommostLevelCompaction bottommostLevelCompaction) {
168     setBottommostLevelCompaction(nativeHandle_, bottommostLevelCompaction.getValue());
169     return this;
170   }
171 
172   /**
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
176    */
allowWriteStall()177   public boolean allowWriteStall() {
178     return allowWriteStall(nativeHandle_);
179   }
180 
181 
182   /**
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.
185    *
186    * @return This CompactRangeOptions
187    * @param allowWriteStall true if compaction should execute immediately
188    */
setAllowWriteStall(final boolean allowWriteStall)189   public CompactRangeOptions setAllowWriteStall(final boolean allowWriteStall) {
190     setAllowWriteStall(nativeHandle_, allowWriteStall);
191     return this;
192   }
193 
194   /**
195    * If &gt; 0, it will replace the option in the DBOptions for this compaction
196    * @return number of subcompactions
197    */
maxSubcompactions()198   public int maxSubcompactions() {
199     return maxSubcompactions(nativeHandle_);
200   }
201 
202   /**
203    * If &gt; 0, it will replace the option in the DBOptions for this compaction
204    *
205    * @param maxSubcompactions number of subcompactions
206    * @return This CompactRangeOptions
207    */
setMaxSubcompactions(final int maxSubcompactions)208   public CompactRangeOptions setMaxSubcompactions(final int maxSubcompactions) {
209     setMaxSubcompactions(nativeHandle_, maxSubcompactions);
210     return this;
211   }
212 
newCompactRangeOptions()213   private native static long newCompactRangeOptions();
disposeInternal(final long handle)214   @Override protected final native void disposeInternal(final long handle);
215 
exclusiveManualCompaction(final long handle)216   private native boolean exclusiveManualCompaction(final long handle);
setExclusiveManualCompaction(final long handle, final boolean exclusive_manual_compaction)217   private native void setExclusiveManualCompaction(final long handle,
218       final boolean exclusive_manual_compaction);
changeLevel(final long handle)219   private native boolean changeLevel(final long handle);
setChangeLevel(final long handle, final boolean changeLevel)220   private native void setChangeLevel(final long handle,
221       final boolean changeLevel);
targetLevel(final long handle)222   private native int targetLevel(final long handle);
setTargetLevel(final long handle, final int targetLevel)223   private native void setTargetLevel(final long handle,
224       final int targetLevel);
targetPathId(final long handle)225   private native int targetPathId(final long handle);
setTargetPathId(final long handle, final int targetPathId)226   private native void setTargetPathId(final long handle,
227       final int targetPathId);
bottommostLevelCompaction(final long handle)228   private native int bottommostLevelCompaction(final long handle);
setBottommostLevelCompaction(final long handle, final int bottommostLevelCompaction)229   private native void setBottommostLevelCompaction(final long handle,
230       final int bottommostLevelCompaction);
allowWriteStall(final long handle)231   private native boolean allowWriteStall(final long handle);
setAllowWriteStall(final long handle, final boolean allowWriteStall)232   private native void setAllowWriteStall(final long handle,
233       final boolean allowWriteStall);
setMaxSubcompactions(final long handle, final int maxSubcompactions)234   private native void setMaxSubcompactions(final long handle,
235       final int maxSubcompactions);
maxSubcompactions(final long handle)236   private native int maxSubcompactions(final long handle);
237 }
238