secondary_cache_test_util.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 "test_util/secondary_cache_test_util.h"
  6. #include <array>
  7. namespace ROCKSDB_NAMESPACE::secondary_cache_test_util {
  8. namespace {
  9. using TestItem = WithCacheType::TestItem;
  10. size_t SizeCallback(Cache::ObjectPtr obj) {
  11. return static_cast<TestItem*>(obj)->Size();
  12. }
  13. Status SaveToCallback(Cache::ObjectPtr from_obj, size_t from_offset,
  14. size_t length, char* out) {
  15. auto item = static_cast<TestItem*>(from_obj);
  16. const char* buf = item->Buf();
  17. EXPECT_EQ(length, item->Size());
  18. EXPECT_EQ(from_offset, 0);
  19. memcpy(out, buf, length);
  20. return Status::OK();
  21. }
  22. void DeletionCallback(Cache::ObjectPtr obj, MemoryAllocator* /*alloc*/) {
  23. delete static_cast<TestItem*>(obj);
  24. }
  25. Status SaveToCallbackFail(Cache::ObjectPtr /*obj*/, size_t /*offset*/,
  26. size_t /*size*/, char* /*out*/) {
  27. return Status::NotSupported();
  28. }
  29. Status CreateCallback(const Slice& data, CompressionType /*type*/,
  30. CacheTier /*source*/, Cache::CreateContext* context,
  31. MemoryAllocator* /*allocator*/, Cache::ObjectPtr* out_obj,
  32. size_t* out_charge) {
  33. auto t = static_cast<TestCreateContext*>(context);
  34. if (t->fail_create_) {
  35. return Status::NotSupported();
  36. }
  37. *out_obj = new TestItem(data.data(), data.size());
  38. *out_charge = data.size();
  39. return Status::OK();
  40. }
  41. // If helpers without_secondary are provided, returns helpers with secondary
  42. // support. If not provided, returns helpers without secondary support.
  43. auto GenerateHelpersByRole(
  44. const std::array<Cache::CacheItemHelper, kNumCacheEntryRoles>*
  45. without_secondary,
  46. bool fail) {
  47. std::array<Cache::CacheItemHelper, kNumCacheEntryRoles> a;
  48. for (uint32_t i = 0; i < kNumCacheEntryRoles; ++i) {
  49. if (without_secondary) {
  50. a[i] =
  51. Cache::CacheItemHelper{static_cast<CacheEntryRole>(i),
  52. &DeletionCallback,
  53. &SizeCallback,
  54. fail ? &SaveToCallbackFail : &SaveToCallback,
  55. &CreateCallback,
  56. &(*without_secondary)[i]};
  57. } else {
  58. a[i] = Cache::CacheItemHelper{static_cast<CacheEntryRole>(i),
  59. &DeletionCallback};
  60. }
  61. }
  62. return a;
  63. }
  64. } // namespace
  65. const Cache::CacheItemHelper* WithCacheType::GetHelper(
  66. CacheEntryRole r, bool secondary_compatible, bool fail) {
  67. static const std::array<Cache::CacheItemHelper, kNumCacheEntryRoles>
  68. without_secondary = GenerateHelpersByRole(nullptr, false);
  69. static const std::array<Cache::CacheItemHelper, kNumCacheEntryRoles>
  70. with_secondary = GenerateHelpersByRole(&without_secondary, false);
  71. static const std::array<Cache::CacheItemHelper, kNumCacheEntryRoles>
  72. with_secondary_fail = GenerateHelpersByRole(&without_secondary, true);
  73. return &(fail ? with_secondary_fail
  74. : secondary_compatible ? with_secondary
  75. : without_secondary)[static_cast<int>(r)];
  76. }
  77. const Cache::CacheItemHelper* WithCacheType::GetHelperFail(CacheEntryRole r) {
  78. return GetHelper(r, true, true);
  79. }
  80. } // namespace ROCKSDB_NAMESPACE::secondary_cache_test_util