// Copyright (c) Meta Platforms, Inc. and affiliates. // This source code is licensed under both the GPLv2 (found in the // COPYING file in the root directory) and Apache 2.0 License // (found in the LICENSE.Apache file in the root directory). #include "test_util/secondary_cache_test_util.h" #include namespace ROCKSDB_NAMESPACE { namespace secondary_cache_test_util { namespace { using TestItem = WithCacheType::TestItem; size_t SizeCallback(Cache::ObjectPtr obj) { return static_cast(obj)->Size(); } Status SaveToCallback(Cache::ObjectPtr from_obj, size_t from_offset, size_t length, char* out) { auto item = static_cast(from_obj); const char* buf = item->Buf(); EXPECT_EQ(length, item->Size()); EXPECT_EQ(from_offset, 0); memcpy(out, buf, length); return Status::OK(); } void DeletionCallback(Cache::ObjectPtr obj, MemoryAllocator* /*alloc*/) { delete static_cast(obj); } Status SaveToCallbackFail(Cache::ObjectPtr /*obj*/, size_t /*offset*/, size_t /*size*/, char* /*out*/) { return Status::NotSupported(); } Status CreateCallback(const Slice& data, Cache::CreateContext* context, MemoryAllocator* /*allocator*/, Cache::ObjectPtr* out_obj, size_t* out_charge) { auto t = static_cast(context); if (t->fail_create_) { return Status::NotSupported(); } *out_obj = new TestItem(data.data(), data.size()); *out_charge = data.size(); return Status::OK(); } // If helpers without_secondary are provided, returns helpers with secondary // support. If not provided, returns helpers without secondary support. auto GenerateHelpersByRole( const std::array* without_secondary, bool fail) { std::array a; for (uint32_t i = 0; i < kNumCacheEntryRoles; ++i) { if (without_secondary) { a[i] = Cache::CacheItemHelper{static_cast(i), &DeletionCallback, &SizeCallback, fail ? &SaveToCallbackFail : &SaveToCallback, &CreateCallback, &(*without_secondary)[i]}; } else { a[i] = Cache::CacheItemHelper{static_cast(i), &DeletionCallback}; } } return a; } } // namespace const Cache::CacheItemHelper* WithCacheType::GetHelper( CacheEntryRole r, bool secondary_compatible, bool fail) { static const std::array without_secondary = GenerateHelpersByRole(nullptr, false); static const std::array with_secondary = GenerateHelpersByRole(&without_secondary, false); static const std::array with_secondary_fail = GenerateHelpersByRole(&without_secondary, true); return &(fail ? with_secondary_fail : secondary_compatible ? with_secondary : without_secondary)[static_cast(r)]; } const Cache::CacheItemHelper* WithCacheType::GetHelperFail(CacheEntryRole r) { return GetHelper(r, true, true); } } // namespace secondary_cache_test_util } // namespace ROCKSDB_NAMESPACE