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 * Options for Universal Compaction 10 */ 11 public class CompactionOptionsUniversal extends RocksObject { 12 CompactionOptionsUniversal()13 public CompactionOptionsUniversal() { 14 super(newCompactionOptionsUniversal()); 15 } 16 17 /** 18 * Percentage flexibility while comparing file size. If the candidate file(s) 19 * size is 1% smaller than the next file's size, then include next file into 20 * this candidate set. 21 * 22 * Default: 1 23 * 24 * @param sizeRatio The size ratio to use 25 * 26 * @return the reference to the current options. 27 */ setSizeRatio(final int sizeRatio)28 public CompactionOptionsUniversal setSizeRatio(final int sizeRatio) { 29 setSizeRatio(nativeHandle_, sizeRatio); 30 return this; 31 } 32 33 /** 34 * Percentage flexibility while comparing file size. If the candidate file(s) 35 * size is 1% smaller than the next file's size, then include next file into 36 * this candidate set. 37 * 38 * Default: 1 39 * 40 * @return The size ratio in use 41 */ sizeRatio()42 public int sizeRatio() { 43 return sizeRatio(nativeHandle_); 44 } 45 46 /** 47 * The minimum number of files in a single compaction run. 48 * 49 * Default: 2 50 * 51 * @param minMergeWidth minimum number of files in a single compaction run 52 * 53 * @return the reference to the current options. 54 */ setMinMergeWidth(final int minMergeWidth)55 public CompactionOptionsUniversal setMinMergeWidth(final int minMergeWidth) { 56 setMinMergeWidth(nativeHandle_, minMergeWidth); 57 return this; 58 } 59 60 /** 61 * The minimum number of files in a single compaction run. 62 * 63 * Default: 2 64 * 65 * @return minimum number of files in a single compaction run 66 */ minMergeWidth()67 public int minMergeWidth() { 68 return minMergeWidth(nativeHandle_); 69 } 70 71 /** 72 * The maximum number of files in a single compaction run. 73 * 74 * Default: {@link Long#MAX_VALUE} 75 * 76 * @param maxMergeWidth maximum number of files in a single compaction run 77 * 78 * @return the reference to the current options. 79 */ setMaxMergeWidth(final int maxMergeWidth)80 public CompactionOptionsUniversal setMaxMergeWidth(final int maxMergeWidth) { 81 setMaxMergeWidth(nativeHandle_, maxMergeWidth); 82 return this; 83 } 84 85 /** 86 * The maximum number of files in a single compaction run. 87 * 88 * Default: {@link Long#MAX_VALUE} 89 * 90 * @return maximum number of files in a single compaction run 91 */ maxMergeWidth()92 public int maxMergeWidth() { 93 return maxMergeWidth(nativeHandle_); 94 } 95 96 /** 97 * The size amplification is defined as the amount (in percentage) of 98 * additional storage needed to store a single byte of data in the database. 99 * For example, a size amplification of 2% means that a database that 100 * contains 100 bytes of user-data may occupy upto 102 bytes of 101 * physical storage. By this definition, a fully compacted database has 102 * a size amplification of 0%. Rocksdb uses the following heuristic 103 * to calculate size amplification: it assumes that all files excluding 104 * the earliest file contribute to the size amplification. 105 * 106 * Default: 200, which means that a 100 byte database could require upto 107 * 300 bytes of storage. 108 * 109 * @param maxSizeAmplificationPercent the amount of additional storage needed 110 * (as a percentage) to store a single byte in the database 111 * 112 * @return the reference to the current options. 113 */ setMaxSizeAmplificationPercent( final int maxSizeAmplificationPercent)114 public CompactionOptionsUniversal setMaxSizeAmplificationPercent( 115 final int maxSizeAmplificationPercent) { 116 setMaxSizeAmplificationPercent(nativeHandle_, maxSizeAmplificationPercent); 117 return this; 118 } 119 120 /** 121 * The size amplification is defined as the amount (in percentage) of 122 * additional storage needed to store a single byte of data in the database. 123 * For example, a size amplification of 2% means that a database that 124 * contains 100 bytes of user-data may occupy upto 102 bytes of 125 * physical storage. By this definition, a fully compacted database has 126 * a size amplification of 0%. Rocksdb uses the following heuristic 127 * to calculate size amplification: it assumes that all files excluding 128 * the earliest file contribute to the size amplification. 129 * 130 * Default: 200, which means that a 100 byte database could require upto 131 * 300 bytes of storage. 132 * 133 * @return the amount of additional storage needed (as a percentage) to store 134 * a single byte in the database 135 */ maxSizeAmplificationPercent()136 public int maxSizeAmplificationPercent() { 137 return maxSizeAmplificationPercent(nativeHandle_); 138 } 139 140 /** 141 * If this option is set to be -1 (the default value), all the output files 142 * will follow compression type specified. 143 * 144 * If this option is not negative, we will try to make sure compressed 145 * size is just above this value. In normal cases, at least this percentage 146 * of data will be compressed. 147 * 148 * When we are compacting to a new file, here is the criteria whether 149 * it needs to be compressed: assuming here are the list of files sorted 150 * by generation time: 151 * A1...An B1...Bm C1...Ct 152 * where A1 is the newest and Ct is the oldest, and we are going to compact 153 * B1...Bm, we calculate the total size of all the files as total_size, as 154 * well as the total size of C1...Ct as total_C, the compaction output file 155 * will be compressed iff 156 * total_C / total_size < this percentage 157 * 158 * Default: -1 159 * 160 * @param compressionSizePercent percentage of size for compression 161 * 162 * @return the reference to the current options. 163 */ setCompressionSizePercent( final int compressionSizePercent)164 public CompactionOptionsUniversal setCompressionSizePercent( 165 final int compressionSizePercent) { 166 setCompressionSizePercent(nativeHandle_, compressionSizePercent); 167 return this; 168 } 169 170 /** 171 * If this option is set to be -1 (the default value), all the output files 172 * will follow compression type specified. 173 * 174 * If this option is not negative, we will try to make sure compressed 175 * size is just above this value. In normal cases, at least this percentage 176 * of data will be compressed. 177 * 178 * When we are compacting to a new file, here is the criteria whether 179 * it needs to be compressed: assuming here are the list of files sorted 180 * by generation time: 181 * A1...An B1...Bm C1...Ct 182 * where A1 is the newest and Ct is the oldest, and we are going to compact 183 * B1...Bm, we calculate the total size of all the files as total_size, as 184 * well as the total size of C1...Ct as total_C, the compaction output file 185 * will be compressed iff 186 * total_C / total_size < this percentage 187 * 188 * Default: -1 189 * 190 * @return percentage of size for compression 191 */ compressionSizePercent()192 public int compressionSizePercent() { 193 return compressionSizePercent(nativeHandle_); 194 } 195 196 /** 197 * The algorithm used to stop picking files into a single compaction run 198 * 199 * Default: {@link CompactionStopStyle#CompactionStopStyleTotalSize} 200 * 201 * @param compactionStopStyle The compaction algorithm 202 * 203 * @return the reference to the current options. 204 */ setStopStyle( final CompactionStopStyle compactionStopStyle)205 public CompactionOptionsUniversal setStopStyle( 206 final CompactionStopStyle compactionStopStyle) { 207 setStopStyle(nativeHandle_, compactionStopStyle.getValue()); 208 return this; 209 } 210 211 /** 212 * The algorithm used to stop picking files into a single compaction run 213 * 214 * Default: {@link CompactionStopStyle#CompactionStopStyleTotalSize} 215 * 216 * @return The compaction algorithm 217 */ stopStyle()218 public CompactionStopStyle stopStyle() { 219 return CompactionStopStyle.getCompactionStopStyle(stopStyle(nativeHandle_)); 220 } 221 222 /** 223 * Option to optimize the universal multi level compaction by enabling 224 * trivial move for non overlapping files. 225 * 226 * Default: false 227 * 228 * @param allowTrivialMove true if trivial move is allowed 229 * 230 * @return the reference to the current options. 231 */ setAllowTrivialMove( final boolean allowTrivialMove)232 public CompactionOptionsUniversal setAllowTrivialMove( 233 final boolean allowTrivialMove) { 234 setAllowTrivialMove(nativeHandle_, allowTrivialMove); 235 return this; 236 } 237 238 /** 239 * Option to optimize the universal multi level compaction by enabling 240 * trivial move for non overlapping files. 241 * 242 * Default: false 243 * 244 * @return true if trivial move is allowed 245 */ allowTrivialMove()246 public boolean allowTrivialMove() { 247 return allowTrivialMove(nativeHandle_); 248 } 249 newCompactionOptionsUniversal()250 private native static long newCompactionOptionsUniversal(); disposeInternal(final long handle)251 @Override protected final native void disposeInternal(final long handle); 252 setSizeRatio(final long handle, final int sizeRatio)253 private native void setSizeRatio(final long handle, final int sizeRatio); sizeRatio(final long handle)254 private native int sizeRatio(final long handle); setMinMergeWidth( final long handle, final int minMergeWidth)255 private native void setMinMergeWidth( 256 final long handle, final int minMergeWidth); minMergeWidth(final long handle)257 private native int minMergeWidth(final long handle); setMaxMergeWidth( final long handle, final int maxMergeWidth)258 private native void setMaxMergeWidth( 259 final long handle, final int maxMergeWidth); maxMergeWidth(final long handle)260 private native int maxMergeWidth(final long handle); setMaxSizeAmplificationPercent( final long handle, final int maxSizeAmplificationPercent)261 private native void setMaxSizeAmplificationPercent( 262 final long handle, final int maxSizeAmplificationPercent); maxSizeAmplificationPercent(final long handle)263 private native int maxSizeAmplificationPercent(final long handle); setCompressionSizePercent( final long handle, final int compressionSizePercent)264 private native void setCompressionSizePercent( 265 final long handle, final int compressionSizePercent); compressionSizePercent(final long handle)266 private native int compressionSizePercent(final long handle); setStopStyle( final long handle, final byte stopStyle)267 private native void setStopStyle( 268 final long handle, final byte stopStyle); stopStyle(final long handle)269 private native byte stopStyle(final long handle); setAllowTrivialMove( final long handle, final boolean allowTrivialMove)270 private native void setAllowTrivialMove( 271 final long handle, final boolean allowTrivialMove); allowTrivialMove(final long handle)272 private native boolean allowTrivialMove(final long handle); 273 } 274