plain_table_index.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #pragma once
  6. #include <string>
  7. #include <vector>
  8. #include "memory/arena.h"
  9. #include "monitoring/histogram.h"
  10. #include "options/cf_options.h"
  11. #include "rocksdb/options.h"
  12. namespace ROCKSDB_NAMESPACE {
  13. // The file contains two classes PlainTableIndex and PlainTableIndexBuilder
  14. // The two classes implement the index format of PlainTable.
  15. // For description of PlainTable format, see comments of class
  16. // PlainTableFactory
  17. //
  18. //
  19. // PlainTableIndex contains buckets size of index_size_, each is a
  20. // 32-bit integer. The lower 31 bits contain an offset value (explained below)
  21. // and the first bit of the integer indicates type of the offset.
  22. //
  23. // +--------------+------------------------------------------------------+
  24. // | Flag (1 bit) | Offset to binary search buffer or file (31 bits) +
  25. // +--------------+------------------------------------------------------+
  26. //
  27. // Explanation for the "flag bit":
  28. //
  29. // 0 indicates that the bucket contains only one prefix (no conflict when
  30. // hashing this prefix), whose first row starts from this offset of the
  31. // file.
  32. // 1 indicates that the bucket contains more than one prefixes, or there
  33. // are too many rows for one prefix so we need a binary search for it. In
  34. // this case, the offset indicates the offset of sub_index_ holding the
  35. // binary search indexes of keys for those rows. Those binary search indexes
  36. // are organized in this way:
  37. //
  38. // The first 4 bytes, indicate how many indexes (N) are stored after it. After
  39. // it, there are N 32-bit integers, each points of an offset of the file,
  40. // which
  41. // points to starting of a row. Those offsets need to be guaranteed to be in
  42. // ascending order so the keys they are pointing to are also in ascending
  43. // order
  44. // to make sure we can use them to do binary searches. Below is visual
  45. // presentation of a bucket.
  46. //
  47. // <begin>
  48. // number_of_records: varint32
  49. // record 1 file offset: fixedint32
  50. // record 2 file offset: fixedint32
  51. // ....
  52. // record N file offset: fixedint32
  53. // <end>
  54. // The class loads the index block from a PlainTable SST file, and executes
  55. // the index lookup.
  56. // The class is used by PlainTableReader class.
  57. class PlainTableIndex {
  58. public:
  59. enum IndexSearchResult {
  60. kNoPrefixForBucket = 0,
  61. kDirectToFile = 1,
  62. kSubindex = 2
  63. };
  64. explicit PlainTableIndex(Slice data) { InitFromRawData(data); }
  65. PlainTableIndex()
  66. : index_size_(0),
  67. sub_index_size_(0),
  68. num_prefixes_(0),
  69. index_(nullptr),
  70. sub_index_(nullptr) {}
  71. // The function that executes the lookup the hash table.
  72. // The hash key is `prefix_hash`. The function fills the hash bucket
  73. // content in `bucket_value`, which is up to the caller to interpret.
  74. IndexSearchResult GetOffset(uint32_t prefix_hash,
  75. uint32_t* bucket_value) const;
  76. // Initialize data from `index_data`, which points to raw data for
  77. // index stored in the SST file.
  78. Status InitFromRawData(Slice index_data);
  79. // Decode the sub index for specific hash bucket.
  80. // The `offset` is the value returned as `bucket_value` by GetOffset()
  81. // and is only valid when the return value is `kSubindex`.
  82. // The return value is the pointer to the starting address of the
  83. // sub-index. `upper_bound` is filled with the value indicating how many
  84. // entries the sub-index has.
  85. const char* GetSubIndexBasePtrAndUpperBound(uint32_t offset,
  86. uint32_t* upper_bound) const {
  87. const char* index_ptr = &sub_index_[offset];
  88. return GetVarint32Ptr(index_ptr, index_ptr + 4, upper_bound);
  89. }
  90. uint32_t GetIndexSize() const { return index_size_; }
  91. uint32_t GetSubIndexSize() const { return sub_index_size_; }
  92. uint32_t GetNumPrefixes() const { return num_prefixes_; }
  93. static const uint64_t kMaxFileSize = (1u << 31) - 1;
  94. static const uint32_t kSubIndexMask = 0x80000000;
  95. static const size_t kOffsetLen = sizeof(uint32_t);
  96. private:
  97. uint32_t index_size_;
  98. uint32_t sub_index_size_;
  99. uint32_t num_prefixes_;
  100. uint32_t* index_;
  101. char* sub_index_;
  102. };
  103. // PlainTableIndexBuilder is used to create plain table index.
  104. // After calling Finish(), it returns Slice, which is usually
  105. // used either to initialize PlainTableIndex or
  106. // to save index to sst file.
  107. // For more details about the index, please refer to:
  108. // https://github.com/facebook/rocksdb/wiki/PlainTable-Format
  109. // #wiki-in-memory-index-format
  110. // The class is used by PlainTableBuilder class.
  111. class PlainTableIndexBuilder {
  112. public:
  113. PlainTableIndexBuilder(Arena* arena, const ImmutableOptions& ioptions,
  114. const SliceTransform* prefix_extractor,
  115. size_t index_sparseness, double hash_table_ratio,
  116. size_t huge_page_tlb_size)
  117. : arena_(arena),
  118. ioptions_(ioptions),
  119. record_list_(kRecordsPerGroup),
  120. is_first_record_(true),
  121. due_index_(false),
  122. num_prefixes_(0),
  123. num_keys_per_prefix_(0),
  124. prev_key_prefix_hash_(0),
  125. index_sparseness_(index_sparseness),
  126. index_size_(0),
  127. sub_index_size_(0),
  128. prefix_extractor_(prefix_extractor),
  129. hash_table_ratio_(hash_table_ratio),
  130. huge_page_tlb_size_(huge_page_tlb_size) {}
  131. void AddKeyPrefix(Slice key_prefix_slice, uint32_t key_offset);
  132. Slice Finish();
  133. uint32_t GetTotalSize() const {
  134. return VarintLength(index_size_) + VarintLength(num_prefixes_) +
  135. PlainTableIndex::kOffsetLen * index_size_ + sub_index_size_;
  136. }
  137. static const std::string kPlainTableIndexBlock;
  138. private:
  139. struct IndexRecord {
  140. uint32_t hash; // hash of the prefix
  141. uint32_t offset; // offset of a row
  142. IndexRecord* next;
  143. };
  144. // Helper class to track all the index records
  145. class IndexRecordList {
  146. public:
  147. explicit IndexRecordList(size_t num_records_per_group)
  148. : kNumRecordsPerGroup(num_records_per_group),
  149. current_group_(nullptr),
  150. num_records_in_current_group_(num_records_per_group) {}
  151. ~IndexRecordList() {
  152. for (size_t i = 0; i < groups_.size(); i++) {
  153. delete[] groups_[i];
  154. }
  155. }
  156. void AddRecord(uint32_t hash, uint32_t offset);
  157. size_t GetNumRecords() const {
  158. return (groups_.size() - 1) * kNumRecordsPerGroup +
  159. num_records_in_current_group_;
  160. }
  161. IndexRecord* At(size_t index) {
  162. return &(
  163. groups_[index / kNumRecordsPerGroup][index % kNumRecordsPerGroup]);
  164. }
  165. private:
  166. IndexRecord* AllocateNewGroup() {
  167. IndexRecord* result = new IndexRecord[kNumRecordsPerGroup];
  168. groups_.push_back(result);
  169. return result;
  170. }
  171. // Each group in `groups_` contains fix-sized records (determined by
  172. // kNumRecordsPerGroup). Which can help us minimize the cost if resizing
  173. // occurs.
  174. const size_t kNumRecordsPerGroup;
  175. IndexRecord* current_group_;
  176. // List of arrays allocated
  177. std::vector<IndexRecord*> groups_;
  178. size_t num_records_in_current_group_;
  179. };
  180. void AllocateIndex();
  181. // Internal helper function to bucket index record list to hash buckets.
  182. void BucketizeIndexes(std::vector<IndexRecord*>* hash_to_offsets,
  183. std::vector<uint32_t>* entries_per_bucket);
  184. // Internal helper class to fill the indexes and bloom filters to internal
  185. // data structures.
  186. Slice FillIndexes(const std::vector<IndexRecord*>& hash_to_offsets,
  187. const std::vector<uint32_t>& entries_per_bucket);
  188. Arena* arena_;
  189. const ImmutableOptions ioptions_;
  190. HistogramImpl keys_per_prefix_hist_;
  191. IndexRecordList record_list_;
  192. bool is_first_record_;
  193. bool due_index_;
  194. uint32_t num_prefixes_;
  195. uint32_t num_keys_per_prefix_;
  196. uint32_t prev_key_prefix_hash_;
  197. size_t index_sparseness_;
  198. uint32_t index_size_;
  199. uint32_t sub_index_size_;
  200. const SliceTransform* prefix_extractor_;
  201. double hash_table_ratio_;
  202. size_t huge_page_tlb_size_;
  203. std::string prev_key_prefix_;
  204. static const size_t kRecordsPerGroup = 256;
  205. };
  206. } // namespace ROCKSDB_NAMESPACE