persistent_cache_helper.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/persistent_cache_helper.h"
  6. #include "table/block_based/block_based_table_reader.h"
  7. #include "table/format.h"
  8. namespace ROCKSDB_NAMESPACE {
  9. void PersistentCacheHelper::InsertRawPage(
  10. const PersistentCacheOptions& cache_options, const BlockHandle& handle,
  11. const char* data, const size_t size) {
  12. assert(cache_options.persistent_cache);
  13. assert(cache_options.persistent_cache->IsCompressed());
  14. // construct the page key
  15. char cache_key[BlockBasedTable::kMaxCacheKeyPrefixSize + kMaxVarint64Length];
  16. auto key = BlockBasedTable::GetCacheKey(cache_options.key_prefix.c_str(),
  17. cache_options.key_prefix.size(),
  18. handle, cache_key);
  19. // insert content to cache
  20. cache_options.persistent_cache->Insert(key, data, size);
  21. }
  22. void PersistentCacheHelper::InsertUncompressedPage(
  23. const PersistentCacheOptions& cache_options, const BlockHandle& handle,
  24. const BlockContents& contents) {
  25. assert(cache_options.persistent_cache);
  26. assert(!cache_options.persistent_cache->IsCompressed());
  27. // Precondition:
  28. // (1) content is cacheable
  29. // (2) content is not compressed
  30. // construct the page key
  31. char cache_key[BlockBasedTable::kMaxCacheKeyPrefixSize + kMaxVarint64Length];
  32. auto key = BlockBasedTable::GetCacheKey(cache_options.key_prefix.c_str(),
  33. cache_options.key_prefix.size(),
  34. handle, cache_key);
  35. // insert block contents to page cache
  36. cache_options.persistent_cache->Insert(key, contents.data.data(),
  37. contents.data.size());
  38. }
  39. Status PersistentCacheHelper::LookupRawPage(
  40. const PersistentCacheOptions& cache_options, const BlockHandle& handle,
  41. std::unique_ptr<char[]>* raw_data, const size_t raw_data_size) {
  42. #ifdef NDEBUG
  43. (void)raw_data_size;
  44. #endif
  45. assert(cache_options.persistent_cache);
  46. assert(cache_options.persistent_cache->IsCompressed());
  47. // construct the page key
  48. char cache_key[BlockBasedTable::kMaxCacheKeyPrefixSize + kMaxVarint64Length];
  49. auto key = BlockBasedTable::GetCacheKey(cache_options.key_prefix.c_str(),
  50. cache_options.key_prefix.size(),
  51. handle, cache_key);
  52. // Lookup page
  53. size_t size;
  54. Status s = cache_options.persistent_cache->Lookup(key, raw_data, &size);
  55. if (!s.ok()) {
  56. // cache miss
  57. RecordTick(cache_options.statistics, PERSISTENT_CACHE_MISS);
  58. return s;
  59. }
  60. // cache hit
  61. assert(raw_data_size == handle.size() + kBlockTrailerSize);
  62. assert(size == raw_data_size);
  63. RecordTick(cache_options.statistics, PERSISTENT_CACHE_HIT);
  64. return Status::OK();
  65. }
  66. Status PersistentCacheHelper::LookupUncompressedPage(
  67. const PersistentCacheOptions& cache_options, const BlockHandle& handle,
  68. BlockContents* contents) {
  69. assert(cache_options.persistent_cache);
  70. assert(!cache_options.persistent_cache->IsCompressed());
  71. if (!contents) {
  72. // We shouldn't lookup in the cache. Either
  73. // (1) Nowhere to store
  74. return Status::NotFound();
  75. }
  76. // construct the page key
  77. char cache_key[BlockBasedTable::kMaxCacheKeyPrefixSize + kMaxVarint64Length];
  78. auto key = BlockBasedTable::GetCacheKey(cache_options.key_prefix.c_str(),
  79. cache_options.key_prefix.size(),
  80. handle, cache_key);
  81. // Lookup page
  82. std::unique_ptr<char[]> data;
  83. size_t size;
  84. Status s = cache_options.persistent_cache->Lookup(key, &data, &size);
  85. if (!s.ok()) {
  86. // cache miss
  87. RecordTick(cache_options.statistics, PERSISTENT_CACHE_MISS);
  88. return s;
  89. }
  90. // please note we are potentially comparing compressed data size with
  91. // uncompressed data size
  92. assert(handle.size() <= size);
  93. // update stats
  94. RecordTick(cache_options.statistics, PERSISTENT_CACHE_HIT);
  95. // construct result and return
  96. *contents = BlockContents(std::move(data), size);
  97. return Status::OK();
  98. }
  99. } // namespace ROCKSDB_NAMESPACE