block_prefix_index.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #include "table/block_based/block_prefix_index.h"
  6. #include <vector>
  7. #include "memory/arena.h"
  8. #include "rocksdb/comparator.h"
  9. #include "rocksdb/slice.h"
  10. #include "rocksdb/slice_transform.h"
  11. #include "util/coding.h"
  12. #include "util/hash.h"
  13. namespace ROCKSDB_NAMESPACE {
  14. inline uint32_t Hash(const Slice& s) {
  15. return ROCKSDB_NAMESPACE::Hash(s.data(), s.size(), 0);
  16. }
  17. inline uint32_t PrefixToBucket(const Slice& prefix, uint32_t num_buckets) {
  18. return Hash(prefix) % num_buckets;
  19. }
  20. // The prefix block index is simply a bucket array, with each entry pointing to
  21. // the blocks that span the prefixes hashed to this bucket.
  22. //
  23. // To reduce memory footprint, if there is only one block per bucket, the entry
  24. // stores the block id directly. If there are more than one blocks per bucket,
  25. // because of hash collision or a single prefix spanning multiple blocks,
  26. // the entry points to an array of block ids. The block array is an array of
  27. // uint32_t's. The first uint32_t indicates the total number of blocks, followed
  28. // by the block ids.
  29. //
  30. // To differentiate the two cases, the high order bit of the entry indicates
  31. // whether it is a 'pointer' into a separate block array.
  32. // 0x7FFFFFFF is reserved for empty bucket.
  33. const uint32_t kNoneBlock = 0x7FFFFFFF;
  34. const uint32_t kBlockArrayMask = 0x80000000;
  35. inline bool IsNone(uint32_t block_id) { return block_id == kNoneBlock; }
  36. inline bool IsBlockId(uint32_t block_id) {
  37. return (block_id & kBlockArrayMask) == 0;
  38. }
  39. inline uint32_t DecodeIndex(uint32_t block_id) {
  40. uint32_t index = block_id ^ kBlockArrayMask;
  41. assert(index < kBlockArrayMask);
  42. return index;
  43. }
  44. inline uint32_t EncodeIndex(uint32_t index) {
  45. assert(index < kBlockArrayMask);
  46. return index | kBlockArrayMask;
  47. }
  48. // temporary storage for prefix information during index building
  49. struct PrefixRecord {
  50. Slice prefix;
  51. uint32_t start_block;
  52. uint32_t end_block;
  53. uint32_t num_blocks;
  54. PrefixRecord* next;
  55. };
  56. class BlockPrefixIndex::Builder {
  57. public:
  58. void Add(const Slice& key_prefix, uint32_t start_block, uint32_t num_blocks) {
  59. PrefixRecord* record = reinterpret_cast<PrefixRecord*>(
  60. arena_.AllocateAligned(sizeof(PrefixRecord)));
  61. record->prefix = key_prefix;
  62. record->start_block = start_block;
  63. record->end_block = start_block + num_blocks - 1;
  64. record->num_blocks = num_blocks;
  65. prefixes_.push_back(record);
  66. }
  67. BlockPrefixIndex* Finish(const SliceTransform* prefix_extractor) {
  68. // For now, use roughly 1:1 prefix to bucket ratio.
  69. uint32_t num_buckets = static_cast<uint32_t>(prefixes_.size()) + 1;
  70. // Collect prefix records that hash to the same bucket, into a single
  71. // linklist.
  72. std::vector<PrefixRecord*> prefixes_per_bucket(num_buckets, nullptr);
  73. std::vector<uint32_t> num_blocks_per_bucket(num_buckets, 0);
  74. for (PrefixRecord* current : prefixes_) {
  75. uint32_t bucket = PrefixToBucket(current->prefix, num_buckets);
  76. // merge the prefix block span if the first block of this prefix is
  77. // connected to the last block of the previous prefix.
  78. PrefixRecord* prev = prefixes_per_bucket[bucket];
  79. if (prev) {
  80. assert(current->start_block >= prev->end_block);
  81. auto distance = current->start_block - prev->end_block;
  82. if (distance <= 1) {
  83. prev->end_block = current->end_block;
  84. prev->num_blocks = prev->end_block - prev->start_block + 1;
  85. num_blocks_per_bucket[bucket] += (current->num_blocks + distance - 1);
  86. continue;
  87. }
  88. }
  89. current->next = prev;
  90. prefixes_per_bucket[bucket] = current;
  91. num_blocks_per_bucket[bucket] += current->num_blocks;
  92. }
  93. // Calculate the block array buffer size
  94. uint32_t total_block_array_entries = 0;
  95. for (uint32_t i = 0; i < num_buckets; i++) {
  96. uint32_t num_blocks = num_blocks_per_bucket[i];
  97. if (num_blocks > 1) {
  98. total_block_array_entries += (num_blocks + 1);
  99. }
  100. }
  101. // Populate the final prefix block index
  102. uint32_t* block_array_buffer = new uint32_t[total_block_array_entries];
  103. uint32_t* buckets = new uint32_t[num_buckets];
  104. uint32_t offset = 0;
  105. for (uint32_t i = 0; i < num_buckets; i++) {
  106. uint32_t num_blocks = num_blocks_per_bucket[i];
  107. if (num_blocks == 0) {
  108. assert(prefixes_per_bucket[i] == nullptr);
  109. buckets[i] = kNoneBlock;
  110. } else if (num_blocks == 1) {
  111. assert(prefixes_per_bucket[i] != nullptr);
  112. assert(prefixes_per_bucket[i]->next == nullptr);
  113. buckets[i] = prefixes_per_bucket[i]->start_block;
  114. } else {
  115. assert(total_block_array_entries > 0);
  116. assert(prefixes_per_bucket[i] != nullptr);
  117. buckets[i] = EncodeIndex(offset);
  118. block_array_buffer[offset] = num_blocks;
  119. uint32_t* last_block = &block_array_buffer[offset + num_blocks];
  120. auto current = prefixes_per_bucket[i];
  121. // populate block ids from largest to smallest
  122. while (current != nullptr) {
  123. for (uint32_t iter = 0; iter < current->num_blocks; iter++) {
  124. *last_block = current->end_block - iter;
  125. last_block--;
  126. }
  127. current = current->next;
  128. }
  129. assert(last_block == &block_array_buffer[offset]);
  130. offset += (num_blocks + 1);
  131. }
  132. }
  133. assert(offset == total_block_array_entries);
  134. return new BlockPrefixIndex(prefix_extractor, num_buckets, buckets,
  135. total_block_array_entries, block_array_buffer);
  136. }
  137. private:
  138. std::vector<PrefixRecord*> prefixes_;
  139. Arena arena_;
  140. };
  141. Status BlockPrefixIndex::Create(const SliceTransform* prefix_extractor,
  142. const Slice& prefixes, const Slice& prefix_meta,
  143. BlockPrefixIndex** prefix_index) {
  144. uint64_t pos = 0;
  145. auto meta_pos = prefix_meta;
  146. Status s;
  147. Builder builder;
  148. while (!meta_pos.empty()) {
  149. uint32_t prefix_size = 0;
  150. uint32_t entry_index = 0;
  151. uint32_t num_blocks = 0;
  152. if (!GetVarint32(&meta_pos, &prefix_size) ||
  153. !GetVarint32(&meta_pos, &entry_index) ||
  154. !GetVarint32(&meta_pos, &num_blocks)) {
  155. s = Status::Corruption(
  156. "Corrupted prefix meta block: unable to read from it.");
  157. break;
  158. }
  159. if (pos + prefix_size > prefixes.size()) {
  160. s = Status::Corruption(
  161. "Corrupted prefix meta block: size inconsistency.");
  162. break;
  163. }
  164. Slice prefix(prefixes.data() + pos, prefix_size);
  165. builder.Add(prefix, entry_index, num_blocks);
  166. pos += prefix_size;
  167. }
  168. if (s.ok() && pos != prefixes.size()) {
  169. s = Status::Corruption("Corrupted prefix meta block");
  170. }
  171. if (s.ok()) {
  172. *prefix_index = builder.Finish(prefix_extractor);
  173. }
  174. return s;
  175. }
  176. uint32_t BlockPrefixIndex::GetBlocks(const Slice& key, uint32_t** blocks) {
  177. Slice prefix = internal_prefix_extractor_.Transform(key);
  178. uint32_t bucket = PrefixToBucket(prefix, num_buckets_);
  179. uint32_t block_id = buckets_[bucket];
  180. if (IsNone(block_id)) {
  181. return 0;
  182. } else if (IsBlockId(block_id)) {
  183. *blocks = &buckets_[bucket];
  184. return 1;
  185. } else {
  186. uint32_t index = DecodeIndex(block_id);
  187. assert(index < num_block_array_buffer_entries_);
  188. *blocks = &block_array_buffer_[index + 1];
  189. uint32_t num_blocks = block_array_buffer_[index];
  190. assert(num_blocks > 1);
  191. assert(index + num_blocks < num_block_array_buffer_entries_);
  192. return num_blocks;
  193. }
  194. }
  195. } // namespace ROCKSDB_NAMESPACE