|
| 1 | +// Copyright (c) 2014, Facebook, Inc. All rights reserved. |
| 2 | +// This source code is licensed under the BSD-style license found in the |
| 3 | +// LICENSE file in the root directory of this source tree. An additional grant |
| 4 | +// of patent rights can be found in the PATENTS file in the same directory. |
| 5 | +package org.rocksdb; |
| 6 | + |
| 7 | +/** |
| 8 | + * The config for plain table sst format. |
| 9 | + * |
| 10 | + * BlockBasedTable is a RocksDB's default SST file format. |
| 11 | + */ |
| 12 | +public class BlockBasedTableConfig extends TableFormatConfig { |
| 13 | + |
| 14 | + public BlockBasedTableConfig() { |
| 15 | + noBlockCache_ = false; |
| 16 | + blockCacheSize_ = 8 * 1024 * 1024; |
| 17 | + blockSize_ = 4 * 1024; |
| 18 | + blockSizeDeviation_ =10; |
| 19 | + blockRestartInterval_ =16; |
| 20 | + wholeKeyFiltering_ = true; |
| 21 | + bitsPerKey_ = 0; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Disable block cache. If this is set to true, |
| 26 | + * then no block cache should be used, and the block_cache should |
| 27 | + * point to a nullptr object. |
| 28 | + * Default: false |
| 29 | + * |
| 30 | + * @param noBlockCache if use block cache |
| 31 | + * @return the reference to the current config. |
| 32 | + */ |
| 33 | + public BlockBasedTableConfig setNoBlockCache(boolean noBlockCache) { |
| 34 | + noBlockCache_ = noBlockCache; |
| 35 | + return this; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @return if block cache is disabled |
| 40 | + */ |
| 41 | + public boolean noBlockCache() { |
| 42 | + return noBlockCache_; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Set the amount of cache in bytes that will be used by RocksDB. |
| 47 | + * If cacheSize is non-positive, then cache will not be used. |
| 48 | + * DEFAULT: 8M |
| 49 | + * |
| 50 | + * @param blockCacheSize block cache size in bytes |
| 51 | + * @return the reference to the current config. |
| 52 | + */ |
| 53 | + public BlockBasedTableConfig setBlockCacheSize(long blockCacheSize) { |
| 54 | + blockCacheSize_ = blockCacheSize; |
| 55 | + return this; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @return block cache size in bytes |
| 60 | + */ |
| 61 | + public long blockCacheSize() { |
| 62 | + return blockCacheSize_; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Controls the number of shards for the block cache. |
| 67 | + * This is applied only if cacheSize is set to non-negative. |
| 68 | + * |
| 69 | + * @param numShardBits the number of shard bits. The resulting |
| 70 | + * number of shards would be 2 ^ numShardBits. Any negative |
| 71 | + * number means use default settings." |
| 72 | + * @return the reference to the current option. |
| 73 | + */ |
| 74 | + public BlockBasedTableConfig setCacheNumShardBits(int numShardBits) { |
| 75 | + numShardBits_ = numShardBits; |
| 76 | + return this; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Returns the number of shard bits used in the block cache. |
| 81 | + * The resulting number of shards would be 2 ^ (returned value). |
| 82 | + * Any negative number means use default settings. |
| 83 | + * |
| 84 | + * @return the number of shard bits used in the block cache. |
| 85 | + */ |
| 86 | + public int cacheNumShardBits() { |
| 87 | + return numShardBits_; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Approximate size of user data packed per block. Note that the |
| 92 | + * block size specified here corresponds to uncompressed data. The |
| 93 | + * actual size of the unit read from disk may be smaller if |
| 94 | + * compression is enabled. This parameter can be changed dynamically. |
| 95 | + * Default: 4K |
| 96 | + * |
| 97 | + * @param blockSize block size in bytes |
| 98 | + * @return the reference to the current config. |
| 99 | + */ |
| 100 | + public BlockBasedTableConfig setBlockSize(long blockSize) { |
| 101 | + blockSize_ = blockSize; |
| 102 | + return this; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @return block size in bytes |
| 107 | + */ |
| 108 | + public long blockSize() { |
| 109 | + return blockSize_; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * This is used to close a block before it reaches the configured |
| 114 | + * 'block_size'. If the percentage of free space in the current block is less |
| 115 | + * than this specified number and adding a new record to the block will |
| 116 | + * exceed the configured block size, then this block will be closed and the |
| 117 | + * new record will be written to the next block. |
| 118 | + * Default is 10. |
| 119 | + * |
| 120 | + * @param blockSizeDeviation the deviation to block size allowed |
| 121 | + * @return the reference to the current config. |
| 122 | + */ |
| 123 | + public BlockBasedTableConfig setBlockSizeDeviation(int blockSizeDeviation) { |
| 124 | + blockSizeDeviation_ = blockSizeDeviation; |
| 125 | + return this; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @return the hash table ratio. |
| 130 | + */ |
| 131 | + public int blockSizeDeviation() { |
| 132 | + return blockSizeDeviation_; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Set block restart interval |
| 137 | + * |
| 138 | + * @param restartInterval block restart interval. |
| 139 | + * @return the reference to the current config. |
| 140 | + */ |
| 141 | + public BlockBasedTableConfig setBlockRestartInterval(int restartInterval) { |
| 142 | + blockRestartInterval_ = restartInterval; |
| 143 | + return this; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * @return block restart interval |
| 148 | + */ |
| 149 | + public int blockRestartInterval() { |
| 150 | + return blockRestartInterval_; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * If true, place whole keys in the filter (not just prefixes). |
| 155 | + * This must generally be true for gets to be efficient. |
| 156 | + * Default: true |
| 157 | + * |
| 158 | + * @param wholeKeyFiltering if enable whole key filtering |
| 159 | + * @return the reference to the current config. |
| 160 | + */ |
| 161 | + public BlockBasedTableConfig setWholeKeyFiltering(boolean wholeKeyFiltering) { |
| 162 | + wholeKeyFiltering_ = wholeKeyFiltering; |
| 163 | + return this; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * @return if whole key filtering is enabled |
| 168 | + */ |
| 169 | + public boolean wholeKeyFiltering() { |
| 170 | + return wholeKeyFiltering_; |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Use the specified filter policy to reduce disk reads. |
| 175 | + * |
| 176 | + * Filter should not be disposed before options instances using this filter is |
| 177 | + * disposed. If dispose() function is not called, then filter object will be |
| 178 | + * GC'd automatically. |
| 179 | + * |
| 180 | + * Filter instance can be re-used in multiple options instances. |
| 181 | + * |
| 182 | + * @param Filter policy java instance. |
| 183 | + * @return the reference to the current config. |
| 184 | + */ |
| 185 | + public BlockBasedTableConfig setFilterBitsPerKey(int bitsPerKey) { |
| 186 | + bitsPerKey_ = bitsPerKey; |
| 187 | + return this; |
| 188 | + } |
| 189 | + |
| 190 | + @Override protected long newTableFactoryHandle() { |
| 191 | + return newTableFactoryHandle(noBlockCache_, blockCacheSize_, numShardBits_, |
| 192 | + blockSize_, blockSizeDeviation_, blockRestartInterval_, |
| 193 | + wholeKeyFiltering_, bitsPerKey_); |
| 194 | + } |
| 195 | + |
| 196 | + private native long newTableFactoryHandle( |
| 197 | + boolean noBlockCache, long blockCacheSize, int numShardbits, |
| 198 | + long blockSize, int blockSizeDeviation, int blockRestartInterval, |
| 199 | + boolean wholeKeyFiltering, int bitsPerKey); |
| 200 | + |
| 201 | + private boolean noBlockCache_; |
| 202 | + private long blockCacheSize_; |
| 203 | + private int numShardBits_; |
| 204 | + private long shard; |
| 205 | + private long blockSize_; |
| 206 | + private int blockSizeDeviation_; |
| 207 | + private int blockRestartInterval_; |
| 208 | + private boolean wholeKeyFiltering_; |
| 209 | + private int bitsPerKey_; |
| 210 | +} |
0 commit comments