block_cache.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright (c) Meta Platforms, Inc. and affiliates.
  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_cache.h"
  6. #include "table/block_based/block_based_table_reader.h"
  7. namespace ROCKSDB_NAMESPACE {
  8. void BlockCreateContext::Create(std::unique_ptr<Block_kData>* parsed_out,
  9. BlockContents&& block) {
  10. parsed_out->reset(new Block_kData(
  11. std::move(block), table_options->read_amp_bytes_per_bit, statistics));
  12. parsed_out->get()->InitializeDataBlockProtectionInfo(protection_bytes_per_key,
  13. raw_ucmp);
  14. }
  15. void BlockCreateContext::Create(std::unique_ptr<Block_kIndex>* parsed_out,
  16. BlockContents&& block) {
  17. parsed_out->reset(new Block_kIndex(std::move(block),
  18. /*read_amp_bytes_per_bit*/ 0, statistics));
  19. parsed_out->get()->InitializeIndexBlockProtectionInfo(
  20. protection_bytes_per_key, raw_ucmp, index_value_is_full,
  21. index_has_first_key);
  22. }
  23. void BlockCreateContext::Create(
  24. std::unique_ptr<Block_kFilterPartitionIndex>* parsed_out,
  25. BlockContents&& block) {
  26. parsed_out->reset(new Block_kFilterPartitionIndex(
  27. std::move(block), /*read_amp_bytes_per_bit*/ 0, statistics));
  28. parsed_out->get()->InitializeIndexBlockProtectionInfo(
  29. protection_bytes_per_key, raw_ucmp, index_value_is_full,
  30. index_has_first_key);
  31. }
  32. void BlockCreateContext::Create(
  33. std::unique_ptr<Block_kRangeDeletion>* parsed_out, BlockContents&& block) {
  34. parsed_out->reset(new Block_kRangeDeletion(
  35. std::move(block), /*read_amp_bytes_per_bit*/ 0, statistics));
  36. }
  37. void BlockCreateContext::Create(std::unique_ptr<Block_kMetaIndex>* parsed_out,
  38. BlockContents&& block) {
  39. parsed_out->reset(new Block_kMetaIndex(
  40. std::move(block), /*read_amp_bytes_per_bit*/ 0, statistics));
  41. parsed_out->get()->InitializeMetaIndexBlockProtectionInfo(
  42. protection_bytes_per_key);
  43. }
  44. void BlockCreateContext::Create(
  45. std::unique_ptr<Block_kUserDefinedIndex>* parsed_out,
  46. BlockContents&& block) {
  47. parsed_out->reset(new Block_kUserDefinedIndex(std::move(block)));
  48. }
  49. void BlockCreateContext::Create(
  50. std::unique_ptr<ParsedFullFilterBlock>* parsed_out, BlockContents&& block) {
  51. parsed_out->reset(new ParsedFullFilterBlock(
  52. table_options->filter_policy.get(), std::move(block)));
  53. }
  54. void BlockCreateContext::Create(std::unique_ptr<DecompressorDict>* parsed_out,
  55. BlockContents&& block) {
  56. parsed_out->reset(new DecompressorDict(
  57. block.data, std::move(block.allocation), *decompressor));
  58. }
  59. namespace {
  60. // For getting SecondaryCache-compatible helpers from a BlockType. This is
  61. // useful for accessing block cache in untyped contexts, such as for generic
  62. // cache warming in table builder.
  63. const std::array<const Cache::CacheItemHelper*,
  64. static_cast<unsigned>(BlockType::kInvalid) + 1>
  65. kCacheItemFullHelperForBlockType{{
  66. BlockCacheInterface<Block_kData>::GetFullHelper(),
  67. BlockCacheInterface<ParsedFullFilterBlock>::GetFullHelper(),
  68. BlockCacheInterface<Block_kFilterPartitionIndex>::GetFullHelper(),
  69. nullptr, // kProperties
  70. BlockCacheInterface<DecompressorDict>::GetFullHelper(),
  71. BlockCacheInterface<Block_kRangeDeletion>::GetFullHelper(),
  72. nullptr, // kHashIndexPrefixes
  73. nullptr, // kHashIndexMetadata
  74. nullptr, // kMetaIndex (not yet stored in block cache)
  75. BlockCacheInterface<Block_kIndex>::GetFullHelper(),
  76. nullptr, // kInvalid
  77. }};
  78. // For getting basic helpers from a BlockType (no SecondaryCache support)
  79. const std::array<const Cache::CacheItemHelper*,
  80. static_cast<unsigned>(BlockType::kInvalid) + 1>
  81. kCacheItemBasicHelperForBlockType{{
  82. BlockCacheInterface<Block_kData>::GetBasicHelper(),
  83. BlockCacheInterface<ParsedFullFilterBlock>::GetBasicHelper(),
  84. BlockCacheInterface<Block_kFilterPartitionIndex>::GetBasicHelper(),
  85. nullptr, // kProperties
  86. BlockCacheInterface<DecompressorDict>::GetBasicHelper(),
  87. BlockCacheInterface<Block_kRangeDeletion>::GetBasicHelper(),
  88. nullptr, // kHashIndexPrefixes
  89. nullptr, // kHashIndexMetadata
  90. nullptr, // kMetaIndex (not yet stored in block cache)
  91. BlockCacheInterface<Block_kIndex>::GetBasicHelper(),
  92. nullptr, // kInvalid
  93. }};
  94. } // namespace
  95. const Cache::CacheItemHelper* GetCacheItemHelper(
  96. BlockType block_type, CacheTier lowest_used_cache_tier) {
  97. if (lowest_used_cache_tier > CacheTier::kVolatileTier) {
  98. return kCacheItemFullHelperForBlockType[static_cast<unsigned>(block_type)];
  99. } else {
  100. return kCacheItemBasicHelperForBlockType[static_cast<unsigned>(block_type)];
  101. }
  102. }
  103. } // namespace ROCKSDB_NAMESPACE