block_based_table_factory.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. //
  6. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. #pragma once
  10. #include <stdint.h>
  11. #include <memory>
  12. #include <string>
  13. #include "db/dbformat.h"
  14. #include "options/options_helper.h"
  15. #include "options/options_parser.h"
  16. #include "rocksdb/flush_block_policy.h"
  17. #include "rocksdb/table.h"
  18. namespace ROCKSDB_NAMESPACE {
  19. struct EnvOptions;
  20. class BlockBasedTableBuilder;
  21. // A class used to track actual bytes written from the tail in the recent SST
  22. // file opens, and provide a suggestion for following open.
  23. class TailPrefetchStats {
  24. public:
  25. void RecordEffectiveSize(size_t len);
  26. // 0 indicates no information to determine.
  27. size_t GetSuggestedPrefetchSize();
  28. private:
  29. const static size_t kNumTracked = 32;
  30. size_t records_[kNumTracked];
  31. port::Mutex mutex_;
  32. size_t next_ = 0;
  33. size_t num_records_ = 0;
  34. };
  35. class BlockBasedTableFactory : public TableFactory {
  36. public:
  37. explicit BlockBasedTableFactory(
  38. const BlockBasedTableOptions& table_options = BlockBasedTableOptions());
  39. ~BlockBasedTableFactory() {}
  40. const char* Name() const override { return kName.c_str(); }
  41. Status NewTableReader(
  42. const TableReaderOptions& table_reader_options,
  43. std::unique_ptr<RandomAccessFileReader>&& file, uint64_t file_size,
  44. std::unique_ptr<TableReader>* table_reader,
  45. bool prefetch_index_and_filter_in_cache = true) const override;
  46. TableBuilder* NewTableBuilder(
  47. const TableBuilderOptions& table_builder_options,
  48. uint32_t column_family_id, WritableFileWriter* file) const override;
  49. // Sanitizes the specified DB Options.
  50. Status SanitizeOptions(const DBOptions& db_opts,
  51. const ColumnFamilyOptions& cf_opts) const override;
  52. std::string GetPrintableTableOptions() const override;
  53. Status GetOptionString(std::string* opt_string,
  54. const std::string& delimiter) const override;
  55. const BlockBasedTableOptions& table_options() const;
  56. void* GetOptions() override { return &table_options_; }
  57. bool IsDeleteRangeSupported() const override { return true; }
  58. static const std::string kName;
  59. private:
  60. BlockBasedTableOptions table_options_;
  61. mutable TailPrefetchStats tail_prefetch_stats_;
  62. };
  63. extern const std::string kHashIndexPrefixesBlock;
  64. extern const std::string kHashIndexPrefixesMetadataBlock;
  65. extern const std::string kPropTrue;
  66. extern const std::string kPropFalse;
  67. #ifndef ROCKSDB_LITE
  68. extern Status VerifyBlockBasedTableFactory(
  69. const BlockBasedTableFactory* base_tf,
  70. const BlockBasedTableFactory* file_tf,
  71. OptionsSanityCheckLevel sanity_check_level);
  72. static std::unordered_map<std::string, OptionTypeInfo>
  73. block_based_table_type_info = {
  74. /* currently not supported
  75. std::shared_ptr<Cache> block_cache = nullptr;
  76. std::shared_ptr<Cache> block_cache_compressed = nullptr;
  77. */
  78. {"flush_block_policy_factory",
  79. {offsetof(struct BlockBasedTableOptions, flush_block_policy_factory),
  80. OptionType::kFlushBlockPolicyFactory, OptionVerificationType::kByName,
  81. false, 0}},
  82. {"cache_index_and_filter_blocks",
  83. {offsetof(struct BlockBasedTableOptions,
  84. cache_index_and_filter_blocks),
  85. OptionType::kBoolean, OptionVerificationType::kNormal, false, 0}},
  86. {"cache_index_and_filter_blocks_with_high_priority",
  87. {offsetof(struct BlockBasedTableOptions,
  88. cache_index_and_filter_blocks_with_high_priority),
  89. OptionType::kBoolean, OptionVerificationType::kNormal, false, 0}},
  90. {"pin_l0_filter_and_index_blocks_in_cache",
  91. {offsetof(struct BlockBasedTableOptions,
  92. pin_l0_filter_and_index_blocks_in_cache),
  93. OptionType::kBoolean, OptionVerificationType::kNormal, false, 0}},
  94. {"index_type",
  95. {offsetof(struct BlockBasedTableOptions, index_type),
  96. OptionType::kBlockBasedTableIndexType,
  97. OptionVerificationType::kNormal, false, 0}},
  98. {"hash_index_allow_collision",
  99. {offsetof(struct BlockBasedTableOptions, hash_index_allow_collision),
  100. OptionType::kBoolean, OptionVerificationType::kNormal, false, 0}},
  101. {"data_block_index_type",
  102. {offsetof(struct BlockBasedTableOptions, data_block_index_type),
  103. OptionType::kBlockBasedTableDataBlockIndexType,
  104. OptionVerificationType::kNormal, false, 0}},
  105. {"index_shortening",
  106. {offsetof(struct BlockBasedTableOptions, index_shortening),
  107. OptionType::kBlockBasedTableIndexShorteningMode,
  108. OptionVerificationType::kNormal, false, 0}},
  109. {"data_block_hash_table_util_ratio",
  110. {offsetof(struct BlockBasedTableOptions,
  111. data_block_hash_table_util_ratio),
  112. OptionType::kDouble, OptionVerificationType::kNormal, false, 0}},
  113. {"checksum",
  114. {offsetof(struct BlockBasedTableOptions, checksum),
  115. OptionType::kChecksumType, OptionVerificationType::kNormal, false,
  116. 0}},
  117. {"no_block_cache",
  118. {offsetof(struct BlockBasedTableOptions, no_block_cache),
  119. OptionType::kBoolean, OptionVerificationType::kNormal, false, 0}},
  120. {"block_size",
  121. {offsetof(struct BlockBasedTableOptions, block_size),
  122. OptionType::kSizeT, OptionVerificationType::kNormal, false, 0}},
  123. {"block_size_deviation",
  124. {offsetof(struct BlockBasedTableOptions, block_size_deviation),
  125. OptionType::kInt, OptionVerificationType::kNormal, false, 0}},
  126. {"block_restart_interval",
  127. {offsetof(struct BlockBasedTableOptions, block_restart_interval),
  128. OptionType::kInt, OptionVerificationType::kNormal, false, 0}},
  129. {"index_block_restart_interval",
  130. {offsetof(struct BlockBasedTableOptions, index_block_restart_interval),
  131. OptionType::kInt, OptionVerificationType::kNormal, false, 0}},
  132. {"index_per_partition",
  133. {0, OptionType::kUInt64T, OptionVerificationType::kDeprecated, false,
  134. 0}},
  135. {"metadata_block_size",
  136. {offsetof(struct BlockBasedTableOptions, metadata_block_size),
  137. OptionType::kUInt64T, OptionVerificationType::kNormal, false, 0}},
  138. {"partition_filters",
  139. {offsetof(struct BlockBasedTableOptions, partition_filters),
  140. OptionType::kBoolean, OptionVerificationType::kNormal, false, 0}},
  141. {"filter_policy",
  142. {offsetof(struct BlockBasedTableOptions, filter_policy),
  143. OptionType::kFilterPolicy, OptionVerificationType::kByName, false,
  144. 0}},
  145. {"whole_key_filtering",
  146. {offsetof(struct BlockBasedTableOptions, whole_key_filtering),
  147. OptionType::kBoolean, OptionVerificationType::kNormal, false, 0}},
  148. {"skip_table_builder_flush",
  149. {0, OptionType::kBoolean, OptionVerificationType::kDeprecated, false,
  150. 0}},
  151. {"format_version",
  152. {offsetof(struct BlockBasedTableOptions, format_version),
  153. OptionType::kUInt32T, OptionVerificationType::kNormal, false, 0}},
  154. {"verify_compression",
  155. {offsetof(struct BlockBasedTableOptions, verify_compression),
  156. OptionType::kBoolean, OptionVerificationType::kNormal, false, 0}},
  157. {"read_amp_bytes_per_bit",
  158. {offsetof(struct BlockBasedTableOptions, read_amp_bytes_per_bit),
  159. OptionType::kSizeT, OptionVerificationType::kNormal, false, 0}},
  160. {"enable_index_compression",
  161. {offsetof(struct BlockBasedTableOptions, enable_index_compression),
  162. OptionType::kBoolean, OptionVerificationType::kNormal, false, 0}},
  163. {"block_align",
  164. {offsetof(struct BlockBasedTableOptions, block_align),
  165. OptionType::kBoolean, OptionVerificationType::kNormal, false, 0}},
  166. {"pin_top_level_index_and_filter",
  167. {offsetof(struct BlockBasedTableOptions,
  168. pin_top_level_index_and_filter),
  169. OptionType::kBoolean, OptionVerificationType::kNormal, false, 0}}};
  170. #endif // !ROCKSDB_LITE
  171. } // namespace ROCKSDB_NAMESPACE