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 package org.rocksdb;
6 
7 /**
8  * The config for plain table sst format.
9  *
10  * <p>PlainTable is a RocksDB's SST file format optimized for low query
11  * latency on pure-memory or really low-latency media.</p>
12  *
13  * <p>It also support prefix hash feature.</p>
14  */
15 public class PlainTableConfig extends TableFormatConfig {
16   public static final int VARIABLE_LENGTH = 0;
17   public static final int DEFAULT_BLOOM_BITS_PER_KEY = 10;
18   public static final double DEFAULT_HASH_TABLE_RATIO = 0.75;
19   public static final int DEFAULT_INDEX_SPARSENESS = 16;
20   public static final int DEFAULT_HUGE_TLB_SIZE = 0;
21   public static final EncodingType DEFAULT_ENCODING_TYPE =
22       EncodingType.kPlain;
23   public static final boolean DEFAULT_FULL_SCAN_MODE = false;
24   public static final boolean DEFAULT_STORE_INDEX_IN_FILE
25       = false;
26 
PlainTableConfig()27   public PlainTableConfig() {
28     keySize_ = VARIABLE_LENGTH;
29     bloomBitsPerKey_ = DEFAULT_BLOOM_BITS_PER_KEY;
30     hashTableRatio_ = DEFAULT_HASH_TABLE_RATIO;
31     indexSparseness_ = DEFAULT_INDEX_SPARSENESS;
32     hugePageTlbSize_ = DEFAULT_HUGE_TLB_SIZE;
33     encodingType_ = DEFAULT_ENCODING_TYPE;
34     fullScanMode_ = DEFAULT_FULL_SCAN_MODE;
35     storeIndexInFile_ = DEFAULT_STORE_INDEX_IN_FILE;
36   }
37 
38   /**
39    * <p>Set the length of the user key. If it is set to be
40    * VARIABLE_LENGTH, then it indicates the user keys are
41    * of variable length.</p>
42    *
43    * <p>Otherwise,all the keys need to have the same length
44    * in byte.</p>
45    *
46    * <p>DEFAULT: VARIABLE_LENGTH</p>
47    *
48    * @param keySize the length of the user key.
49    * @return the reference to the current config.
50    */
setKeySize(int keySize)51   public PlainTableConfig setKeySize(int keySize) {
52     keySize_ = keySize;
53     return this;
54   }
55 
56   /**
57    * @return the specified size of the user key.  If VARIABLE_LENGTH,
58    *     then it indicates variable-length key.
59    */
keySize()60   public int keySize() {
61     return keySize_;
62   }
63 
64   /**
65    * Set the number of bits per key used by the internal bloom filter
66    * in the plain table sst format.
67    *
68    * @param bitsPerKey the number of bits per key for bloom filer.
69    * @return the reference to the current config.
70    */
setBloomBitsPerKey(int bitsPerKey)71   public PlainTableConfig setBloomBitsPerKey(int bitsPerKey) {
72     bloomBitsPerKey_ = bitsPerKey;
73     return this;
74   }
75 
76   /**
77    * @return the number of bits per key used for the bloom filter.
78    */
bloomBitsPerKey()79   public int bloomBitsPerKey() {
80     return bloomBitsPerKey_;
81   }
82 
83   /**
84    * hashTableRatio is the desired utilization of the hash table used
85    * for prefix hashing.  The ideal ratio would be the number of
86    * prefixes / the number of hash buckets.  If this value is set to
87    * zero, then hash table will not be used.
88    *
89    * @param ratio the hash table ratio.
90    * @return the reference to the current config.
91    */
setHashTableRatio(double ratio)92   public PlainTableConfig setHashTableRatio(double ratio) {
93     hashTableRatio_ = ratio;
94     return this;
95   }
96 
97   /**
98    * @return the hash table ratio.
99    */
hashTableRatio()100   public double hashTableRatio() {
101     return hashTableRatio_;
102   }
103 
104   /**
105    * Index sparseness determines the index interval for keys inside the
106    * same prefix.  This number is equal to the maximum number of linear
107    * search required after hash and binary search.  If it's set to 0,
108    * then each key will be indexed.
109    *
110    * @param sparseness the index sparseness.
111    * @return the reference to the current config.
112    */
setIndexSparseness(int sparseness)113   public PlainTableConfig setIndexSparseness(int sparseness) {
114     indexSparseness_ = sparseness;
115     return this;
116   }
117 
118   /**
119    * @return the index sparseness.
120    */
indexSparseness()121   public long indexSparseness() {
122     return indexSparseness_;
123   }
124 
125   /**
126    * <p>huge_page_tlb_size: if &le;0, allocate hash indexes and blooms
127    * from malloc otherwise from huge page TLB.</p>
128    *
129    * <p>The user needs to reserve huge pages for it to be allocated,
130    * like: {@code sysctl -w vm.nr_hugepages=20}</p>
131    *
132    * <p>See linux doc Documentation/vm/hugetlbpage.txt</p>
133    *
134    * @param hugePageTlbSize huge page tlb size
135    * @return the reference to the current config.
136    */
setHugePageTlbSize(int hugePageTlbSize)137   public PlainTableConfig setHugePageTlbSize(int hugePageTlbSize) {
138     this.hugePageTlbSize_ = hugePageTlbSize;
139     return this;
140   }
141 
142   /**
143    * Returns the value for huge page tlb size
144    *
145    * @return hugePageTlbSize
146    */
hugePageTlbSize()147   public int hugePageTlbSize() {
148     return hugePageTlbSize_;
149   }
150 
151   /**
152    * Sets the encoding type.
153    *
154    * <p>This setting determines how to encode
155    * the keys. See enum {@link EncodingType} for
156    * the choices.</p>
157    *
158    * <p>The value will determine how to encode keys
159    * when writing to a new SST file. This value will be stored
160    * inside the SST file which will be used when reading from
161    * the file, which makes it possible for users to choose
162    * different encoding type when reopening a DB. Files with
163    * different encoding types can co-exist in the same DB and
164    * can be read.</p>
165    *
166    * @param encodingType {@link org.rocksdb.EncodingType} value.
167    * @return the reference to the current config.
168    */
setEncodingType(EncodingType encodingType)169   public PlainTableConfig setEncodingType(EncodingType encodingType) {
170     this.encodingType_ = encodingType;
171     return this;
172   }
173 
174   /**
175    * Returns the active EncodingType
176    *
177    * @return currently set encoding type
178    */
encodingType()179   public EncodingType encodingType() {
180     return encodingType_;
181   }
182 
183   /**
184    * Set full scan mode, if true the whole file will be read
185    * one record by one without using the index.
186    *
187    * @param fullScanMode boolean value indicating if full
188    *     scan mode shall be enabled.
189    * @return the reference to the current config.
190    */
setFullScanMode(boolean fullScanMode)191   public PlainTableConfig setFullScanMode(boolean fullScanMode) {
192     this.fullScanMode_ = fullScanMode;
193     return this;
194   }
195 
196   /**
197    * Return if full scan mode is active
198    * @return boolean value indicating if the full scan mode is
199    *     enabled.
200    */
fullScanMode()201   public boolean fullScanMode() {
202     return fullScanMode_;
203   }
204 
205   /**
206    * <p>If set to true: compute plain table index and bloom
207    * filter during file building and store it in file.
208    * When reading file, index will be mmaped instead
209    * of doing recomputation.</p>
210    *
211    * @param storeIndexInFile value indicating if index shall
212    *     be stored in a file
213    * @return the reference to the current config.
214    */
setStoreIndexInFile(boolean storeIndexInFile)215   public PlainTableConfig setStoreIndexInFile(boolean storeIndexInFile) {
216     this.storeIndexInFile_ = storeIndexInFile;
217     return this;
218   }
219 
220   /**
221    * Return a boolean value indicating if index shall be stored
222    * in a file.
223    *
224    * @return currently set value for store index in file.
225    */
storeIndexInFile()226   public boolean storeIndexInFile() {
227     return storeIndexInFile_;
228   }
229 
newTableFactoryHandle()230   @Override protected long newTableFactoryHandle() {
231     return newTableFactoryHandle(keySize_, bloomBitsPerKey_,
232         hashTableRatio_, indexSparseness_, hugePageTlbSize_,
233         encodingType_.getValue(), fullScanMode_,
234         storeIndexInFile_);
235   }
236 
newTableFactoryHandle( int keySize, int bloomBitsPerKey, double hashTableRatio, int indexSparseness, int hugePageTlbSize, byte encodingType, boolean fullScanMode, boolean storeIndexInFile)237   private native long newTableFactoryHandle(
238       int keySize, int bloomBitsPerKey,
239       double hashTableRatio, int indexSparseness,
240       int hugePageTlbSize, byte encodingType,
241       boolean fullScanMode, boolean storeIndexInFile);
242 
243   private int keySize_;
244   private int bloomBitsPerKey_;
245   private double hashTableRatio_;
246   private int indexSparseness_;
247   private int hugePageTlbSize_;
248   private EncodingType encodingType_;
249   private boolean fullScanMode_;
250   private boolean storeIndexInFile_;
251 }
252