1 // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
2 package org.rocksdb;
3 
4 /**
5  * The config for hash linked list memtable representation
6  * Such memtable contains a fix-sized array of buckets, where
7  * each bucket points to a sorted singly-linked
8  * list (or null if the bucket is empty).
9  *
10  * Note that since this mem-table representation relies on the
11  * key prefix, it is required to invoke one of the usePrefixExtractor
12  * functions to specify how to extract key prefix given a key.
13  * If proper prefix-extractor is not set, then RocksDB will
14  * use the default memtable representation (SkipList) instead
15  * and post a warning in the LOG.
16  */
17 public class HashLinkedListMemTableConfig extends MemTableConfig {
18   public static final long DEFAULT_BUCKET_COUNT = 50000;
19   public static final long DEFAULT_HUGE_PAGE_TLB_SIZE = 0;
20   public static final int DEFAULT_BUCKET_ENTRIES_LOG_THRES = 4096;
21   public static final boolean
22       DEFAULT_IF_LOG_BUCKET_DIST_WHEN_FLUSH = true;
23   public static final int DEFAUL_THRESHOLD_USE_SKIPLIST = 256;
24 
25   /**
26    * HashLinkedListMemTableConfig constructor
27    */
HashLinkedListMemTableConfig()28   public HashLinkedListMemTableConfig() {
29     bucketCount_ = DEFAULT_BUCKET_COUNT;
30     hugePageTlbSize_ = DEFAULT_HUGE_PAGE_TLB_SIZE;
31     bucketEntriesLoggingThreshold_ = DEFAULT_BUCKET_ENTRIES_LOG_THRES;
32     ifLogBucketDistWhenFlush_ = DEFAULT_IF_LOG_BUCKET_DIST_WHEN_FLUSH;
33     thresholdUseSkiplist_ = DEFAUL_THRESHOLD_USE_SKIPLIST;
34   }
35 
36   /**
37    * Set the number of buckets in the fixed-size array used
38    * in the hash linked-list mem-table.
39    *
40    * @param count the number of hash buckets.
41    * @return the reference to the current HashLinkedListMemTableConfig.
42    */
setBucketCount( final long count)43   public HashLinkedListMemTableConfig setBucketCount(
44       final long count) {
45     bucketCount_ = count;
46     return this;
47   }
48 
49   /**
50    * Returns the number of buckets that will be used in the memtable
51    * created based on this config.
52    *
53    * @return the number of buckets
54    */
bucketCount()55   public long bucketCount() {
56     return bucketCount_;
57   }
58 
59   /**
60    * <p>Set the size of huge tlb or allocate the hashtable bytes from
61    * malloc if {@code size <= 0}.</p>
62    *
63    * <p>The user needs to reserve huge pages for it to be allocated,
64    * like: {@code sysctl -w vm.nr_hugepages=20}</p>
65    *
66    * <p>See linux documentation/vm/hugetlbpage.txt</p>
67    *
68    * @param size if set to {@code <= 0} hashtable bytes from malloc
69    * @return the reference to the current HashLinkedListMemTableConfig.
70    */
setHugePageTlbSize( final long size)71   public HashLinkedListMemTableConfig setHugePageTlbSize(
72       final long size) {
73     hugePageTlbSize_ = size;
74     return this;
75   }
76 
77   /**
78    * Returns the size value of hugePageTlbSize.
79    *
80    * @return the hugePageTlbSize.
81    */
hugePageTlbSize()82   public long hugePageTlbSize() {
83     return hugePageTlbSize_;
84   }
85 
86   /**
87    * If number of entries in one bucket exceeds that setting, log
88    * about it.
89    *
90    * @param threshold - number of entries in a single bucket before
91    *     logging starts.
92    * @return the reference to the current HashLinkedListMemTableConfig.
93    */
94   public HashLinkedListMemTableConfig
setBucketEntriesLoggingThreshold(final int threshold)95       setBucketEntriesLoggingThreshold(final int threshold) {
96     bucketEntriesLoggingThreshold_ = threshold;
97     return this;
98   }
99 
100   /**
101    * Returns the maximum number of entries in one bucket before
102    * logging starts.
103    *
104    * @return maximum number of entries in one bucket before logging
105    *     starts.
106    */
bucketEntriesLoggingThreshold()107   public int bucketEntriesLoggingThreshold() {
108     return bucketEntriesLoggingThreshold_;
109   }
110 
111   /**
112    * If true the distrubition of number of entries will be logged.
113    *
114    * @param logDistribution - boolean parameter indicating if number
115    *     of entry distribution shall be logged.
116    * @return the reference to the current HashLinkedListMemTableConfig.
117    */
118   public HashLinkedListMemTableConfig
setIfLogBucketDistWhenFlush(final boolean logDistribution)119       setIfLogBucketDistWhenFlush(final boolean logDistribution) {
120     ifLogBucketDistWhenFlush_ = logDistribution;
121     return this;
122   }
123 
124   /**
125    * Returns information about logging the distribution of
126    *  number of entries on flush.
127    *
128    * @return if distrubtion of number of entries shall be logged.
129    */
ifLogBucketDistWhenFlush()130   public boolean ifLogBucketDistWhenFlush() {
131     return ifLogBucketDistWhenFlush_;
132   }
133 
134   /**
135    * Set maximum number of entries in one bucket. Exceeding this val
136    * leads to a switch from LinkedList to SkipList.
137    *
138    * @param threshold maximum number of entries before SkipList is
139    *     used.
140    * @return the reference to the current HashLinkedListMemTableConfig.
141    */
142   public HashLinkedListMemTableConfig
setThresholdUseSkiplist(final int threshold)143       setThresholdUseSkiplist(final int threshold) {
144     thresholdUseSkiplist_ = threshold;
145     return this;
146   }
147 
148   /**
149    * Returns entries per bucket threshold before LinkedList is
150    * replaced by SkipList usage for that bucket.
151    *
152    * @return entries per bucket threshold before SkipList is used.
153    */
thresholdUseSkiplist()154   public int thresholdUseSkiplist() {
155     return thresholdUseSkiplist_;
156   }
157 
newMemTableFactoryHandle()158   @Override protected long newMemTableFactoryHandle() {
159     return newMemTableFactoryHandle(bucketCount_, hugePageTlbSize_,
160         bucketEntriesLoggingThreshold_, ifLogBucketDistWhenFlush_,
161         thresholdUseSkiplist_);
162   }
163 
newMemTableFactoryHandle(long bucketCount, long hugePageTlbSize, int bucketEntriesLoggingThreshold, boolean ifLogBucketDistWhenFlush, int thresholdUseSkiplist)164   private native long newMemTableFactoryHandle(long bucketCount,
165       long hugePageTlbSize, int bucketEntriesLoggingThreshold,
166       boolean ifLogBucketDistWhenFlush, int thresholdUseSkiplist)
167       throws IllegalArgumentException;
168 
169   private long bucketCount_;
170   private long hugePageTlbSize_;
171   private int bucketEntriesLoggingThreshold_;
172   private boolean ifLogBucketDistWhenFlush_;
173   private int thresholdUseSkiplist_;
174 }
175