forked from facebook/rocksdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecondary_cache_test_util.cc
96 lines (81 loc) · 3.38 KB
/
secondary_cache_test_util.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// 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 <array>
namespace ROCKSDB_NAMESPACE {
namespace secondary_cache_test_util {
namespace {
using TestItem = WithCacheType::TestItem;
size_t SizeCallback(Cache::ObjectPtr obj) {
return static_cast<TestItem*>(obj)->Size();
}
Status SaveToCallback(Cache::ObjectPtr from_obj, size_t from_offset,
size_t length, char* out) {
auto item = static_cast<TestItem*>(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<TestItem*>(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<TestCreateContext*>(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<Cache::CacheItemHelper, kNumCacheEntryRoles>*
without_secondary,
bool fail) {
std::array<Cache::CacheItemHelper, kNumCacheEntryRoles> a;
for (uint32_t i = 0; i < kNumCacheEntryRoles; ++i) {
if (without_secondary) {
a[i] =
Cache::CacheItemHelper{static_cast<CacheEntryRole>(i),
&DeletionCallback,
&SizeCallback,
fail ? &SaveToCallbackFail : &SaveToCallback,
&CreateCallback,
&(*without_secondary)[i]};
} else {
a[i] = Cache::CacheItemHelper{static_cast<CacheEntryRole>(i),
&DeletionCallback};
}
}
return a;
}
} // namespace
const Cache::CacheItemHelper* WithCacheType::GetHelper(
CacheEntryRole r, bool secondary_compatible, bool fail) {
static const std::array<Cache::CacheItemHelper, kNumCacheEntryRoles>
without_secondary = GenerateHelpersByRole(nullptr, false);
static const std::array<Cache::CacheItemHelper, kNumCacheEntryRoles>
with_secondary = GenerateHelpersByRole(&without_secondary, false);
static const std::array<Cache::CacheItemHelper, kNumCacheEntryRoles>
with_secondary_fail = GenerateHelpersByRole(&without_secondary, true);
return &(fail ? with_secondary_fail
: secondary_compatible ? with_secondary
: without_secondary)[static_cast<int>(r)];
}
const Cache::CacheItemHelper* WithCacheType::GetHelperFail(CacheEntryRole r) {
return GetHelper(r, true, true);
}
} // namespace secondary_cache_test_util
} // namespace ROCKSDB_NAMESPACE