uncompression_dict_reader.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #include "table/block_based/uncompression_dict_reader.h"
  7. #include "monitoring/perf_context_imp.h"
  8. #include "table/block_based/block_based_table_reader.h"
  9. #include "util/compression.h"
  10. namespace ROCKSDB_NAMESPACE {
  11. Status UncompressionDictReader::Create(
  12. const BlockBasedTable* table, FilePrefetchBuffer* prefetch_buffer,
  13. bool use_cache, bool prefetch, bool pin,
  14. BlockCacheLookupContext* lookup_context,
  15. std::unique_ptr<UncompressionDictReader>* uncompression_dict_reader) {
  16. assert(table);
  17. assert(table->get_rep());
  18. assert(!pin || prefetch);
  19. assert(uncompression_dict_reader);
  20. CachableEntry<UncompressionDict> uncompression_dict;
  21. if (prefetch || !use_cache) {
  22. const Status s = ReadUncompressionDictionary(
  23. table, prefetch_buffer, ReadOptions(), use_cache,
  24. nullptr /* get_context */, lookup_context, &uncompression_dict);
  25. if (!s.ok()) {
  26. return s;
  27. }
  28. if (use_cache && !pin) {
  29. uncompression_dict.Reset();
  30. }
  31. }
  32. uncompression_dict_reader->reset(
  33. new UncompressionDictReader(table, std::move(uncompression_dict)));
  34. return Status::OK();
  35. }
  36. Status UncompressionDictReader::ReadUncompressionDictionary(
  37. const BlockBasedTable* table, FilePrefetchBuffer* prefetch_buffer,
  38. const ReadOptions& read_options, bool use_cache, GetContext* get_context,
  39. BlockCacheLookupContext* lookup_context,
  40. CachableEntry<UncompressionDict>* uncompression_dict) {
  41. // TODO: add perf counter for compression dictionary read time
  42. assert(table);
  43. assert(uncompression_dict);
  44. assert(uncompression_dict->IsEmpty());
  45. const BlockBasedTable::Rep* const rep = table->get_rep();
  46. assert(rep);
  47. assert(!rep->compression_dict_handle.IsNull());
  48. const Status s = table->RetrieveBlock(
  49. prefetch_buffer, read_options, rep->compression_dict_handle,
  50. UncompressionDict::GetEmptyDict(), uncompression_dict,
  51. BlockType::kCompressionDictionary, get_context, lookup_context,
  52. /* for_compaction */ false, use_cache);
  53. if (!s.ok()) {
  54. ROCKS_LOG_WARN(
  55. rep->ioptions.info_log,
  56. "Encountered error while reading data from compression dictionary "
  57. "block %s",
  58. s.ToString().c_str());
  59. }
  60. return s;
  61. }
  62. Status UncompressionDictReader::GetOrReadUncompressionDictionary(
  63. FilePrefetchBuffer* prefetch_buffer, bool no_io, GetContext* get_context,
  64. BlockCacheLookupContext* lookup_context,
  65. CachableEntry<UncompressionDict>* uncompression_dict) const {
  66. assert(uncompression_dict);
  67. if (!uncompression_dict_.IsEmpty()) {
  68. uncompression_dict->SetUnownedValue(uncompression_dict_.GetValue());
  69. return Status::OK();
  70. }
  71. ReadOptions read_options;
  72. if (no_io) {
  73. read_options.read_tier = kBlockCacheTier;
  74. }
  75. return ReadUncompressionDictionary(table_, prefetch_buffer, read_options,
  76. cache_dictionary_blocks(), get_context,
  77. lookup_context, uncompression_dict);
  78. }
  79. size_t UncompressionDictReader::ApproximateMemoryUsage() const {
  80. assert(!uncompression_dict_.GetOwnValue() ||
  81. uncompression_dict_.GetValue() != nullptr);
  82. size_t usage = uncompression_dict_.GetOwnValue()
  83. ? uncompression_dict_.GetValue()->ApproximateMemoryUsage()
  84. : 0;
  85. #ifdef ROCKSDB_MALLOC_USABLE_SIZE
  86. usage += malloc_usable_size(const_cast<UncompressionDictReader*>(this));
  87. #else
  88. usage += sizeof(*this);
  89. #endif // ROCKSDB_MALLOC_USABLE_SIZE
  90. return usage;
  91. }
  92. bool UncompressionDictReader::cache_dictionary_blocks() const {
  93. assert(table_);
  94. assert(table_->get_rep());
  95. return table_->get_rep()->table_options.cache_index_and_filter_blocks;
  96. }
  97. } // namespace ROCKSDB_NAMESPACE