persistent_cache_helper.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. const PersistentCacheOptions PersistentCacheOptions::kEmpty;
  10. void PersistentCacheHelper::InsertSerialized(
  11. const PersistentCacheOptions& cache_options, const BlockHandle& handle,
  12. const char* data, const size_t size) {
  13. assert(cache_options.persistent_cache);
  14. assert(cache_options.persistent_cache->IsCompressed());
  15. CacheKey key =
  16. BlockBasedTable::GetCacheKey(cache_options.base_cache_key, handle);
  17. cache_options.persistent_cache->Insert(key.AsSlice(), data, size)
  18. .PermitUncheckedError();
  19. }
  20. void PersistentCacheHelper::InsertUncompressed(
  21. const PersistentCacheOptions& cache_options, const BlockHandle& handle,
  22. const BlockContents& contents) {
  23. assert(cache_options.persistent_cache);
  24. assert(!cache_options.persistent_cache->IsCompressed());
  25. // Precondition:
  26. // (1) content is cacheable
  27. // (2) content is not compressed
  28. CacheKey key =
  29. BlockBasedTable::GetCacheKey(cache_options.base_cache_key, handle);
  30. cache_options.persistent_cache
  31. ->Insert(key.AsSlice(), contents.data.data(), contents.data.size())
  32. .PermitUncheckedError();
  33. }
  34. Status PersistentCacheHelper::LookupSerialized(
  35. const PersistentCacheOptions& cache_options, const BlockHandle& handle,
  36. std::unique_ptr<char[]>* out_data, const size_t expected_data_size) {
  37. #ifdef NDEBUG
  38. (void)expected_data_size;
  39. #endif
  40. assert(cache_options.persistent_cache);
  41. assert(cache_options.persistent_cache->IsCompressed());
  42. CacheKey key =
  43. BlockBasedTable::GetCacheKey(cache_options.base_cache_key, handle);
  44. size_t size;
  45. Status s =
  46. cache_options.persistent_cache->Lookup(key.AsSlice(), out_data, &size);
  47. if (!s.ok()) {
  48. // cache miss
  49. RecordTick(cache_options.statistics, PERSISTENT_CACHE_MISS);
  50. return s;
  51. }
  52. // cache hit
  53. // Block-based table is assumed
  54. assert(expected_data_size ==
  55. handle.size() + BlockBasedTable::kBlockTrailerSize);
  56. assert(size == expected_data_size);
  57. RecordTick(cache_options.statistics, PERSISTENT_CACHE_HIT);
  58. return Status::OK();
  59. }
  60. Status PersistentCacheHelper::LookupUncompressed(
  61. const PersistentCacheOptions& cache_options, const BlockHandle& handle,
  62. BlockContents* contents) {
  63. assert(cache_options.persistent_cache);
  64. assert(!cache_options.persistent_cache->IsCompressed());
  65. if (!contents) {
  66. // We shouldn't lookup in the cache. Either
  67. // (1) Nowhere to store
  68. return Status::NotFound();
  69. }
  70. CacheKey key =
  71. BlockBasedTable::GetCacheKey(cache_options.base_cache_key, handle);
  72. std::unique_ptr<char[]> data;
  73. size_t size;
  74. Status s =
  75. cache_options.persistent_cache->Lookup(key.AsSlice(), &data, &size);
  76. if (!s.ok()) {
  77. // cache miss
  78. RecordTick(cache_options.statistics, PERSISTENT_CACHE_MISS);
  79. return s;
  80. }
  81. // please note we are potentially comparing compressed data size with
  82. // uncompressed data size
  83. assert(handle.size() <= size);
  84. // update stats
  85. RecordTick(cache_options.statistics, PERSISTENT_CACHE_HIT);
  86. // construct result and return
  87. *contents = BlockContents(std::move(data), size);
  88. return Status::OK();
  89. }
  90. } // namespace ROCKSDB_NAMESPACE