]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/table/block_based/block_like_traits.h
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / rocksdb / table / block_based / block_like_traits.h
1 // Copyright (c) Facebook, Inc. and its affiliates. 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
6 #pragma once
7
8 #include "cache/cache_entry_roles.h"
9 #include "port/lang.h"
10 #include "table/block_based/block.h"
11 #include "table/block_based/block_type.h"
12 #include "table/block_based/parsed_full_filter_block.h"
13 #include "table/format.h"
14
15 namespace ROCKSDB_NAMESPACE {
16
17 template <typename TBlocklike>
18 class BlocklikeTraits;
19
20 template <typename T, CacheEntryRole R>
21 Cache::CacheItemHelper* GetCacheItemHelperForRole();
22
23 template <typename TBlocklike>
24 Cache::CreateCallback GetCreateCallback(size_t read_amp_bytes_per_bit,
25 Statistics* statistics, bool using_zstd,
26 const FilterPolicy* filter_policy) {
27 return [read_amp_bytes_per_bit, statistics, using_zstd, filter_policy](
28 const void* buf, size_t size, void** out_obj,
29 size_t* charge) -> Status {
30 assert(buf != nullptr);
31 std::unique_ptr<char[]> buf_data(new char[size]());
32 memcpy(buf_data.get(), buf, size);
33 BlockContents bc = BlockContents(std::move(buf_data), size);
34 TBlocklike* ucd_ptr = BlocklikeTraits<TBlocklike>::Create(
35 std::move(bc), read_amp_bytes_per_bit, statistics, using_zstd,
36 filter_policy);
37 *out_obj = reinterpret_cast<void*>(ucd_ptr);
38 *charge = size;
39 return Status::OK();
40 };
41 }
42
43 template <>
44 class BlocklikeTraits<ParsedFullFilterBlock> {
45 public:
46 static ParsedFullFilterBlock* Create(BlockContents&& contents,
47 size_t /* read_amp_bytes_per_bit */,
48 Statistics* /* statistics */,
49 bool /* using_zstd */,
50 const FilterPolicy* filter_policy) {
51 return new ParsedFullFilterBlock(filter_policy, std::move(contents));
52 }
53
54 static uint32_t GetNumRestarts(const ParsedFullFilterBlock& /* block */) {
55 return 0;
56 }
57
58 static size_t SizeCallback(void* obj) {
59 assert(obj != nullptr);
60 ParsedFullFilterBlock* ptr = static_cast<ParsedFullFilterBlock*>(obj);
61 return ptr->GetBlockContentsData().size();
62 }
63
64 static Status SaveToCallback(void* from_obj, size_t from_offset,
65 size_t length, void* out) {
66 assert(from_obj != nullptr);
67 ParsedFullFilterBlock* ptr = static_cast<ParsedFullFilterBlock*>(from_obj);
68 const char* buf = ptr->GetBlockContentsData().data();
69 assert(length == ptr->GetBlockContentsData().size());
70 (void)from_offset;
71 memcpy(out, buf, length);
72 return Status::OK();
73 }
74
75 static Cache::CacheItemHelper* GetCacheItemHelper(BlockType block_type) {
76 (void)block_type;
77 assert(block_type == BlockType::kFilter);
78 return GetCacheItemHelperForRole<ParsedFullFilterBlock,
79 CacheEntryRole::kFilterBlock>();
80 }
81 };
82
83 template <>
84 class BlocklikeTraits<Block> {
85 public:
86 static Block* Create(BlockContents&& contents, size_t read_amp_bytes_per_bit,
87 Statistics* statistics, bool /* using_zstd */,
88 const FilterPolicy* /* filter_policy */) {
89 return new Block(std::move(contents), read_amp_bytes_per_bit, statistics);
90 }
91
92 static uint32_t GetNumRestarts(const Block& block) {
93 return block.NumRestarts();
94 }
95
96 static size_t SizeCallback(void* obj) {
97 assert(obj != nullptr);
98 Block* ptr = static_cast<Block*>(obj);
99 return ptr->size();
100 }
101
102 static Status SaveToCallback(void* from_obj, size_t from_offset,
103 size_t length, void* out) {
104 assert(from_obj != nullptr);
105 Block* ptr = static_cast<Block*>(from_obj);
106 const char* buf = ptr->data();
107 assert(length == ptr->size());
108 (void)from_offset;
109 memcpy(out, buf, length);
110 return Status::OK();
111 }
112
113 static Cache::CacheItemHelper* GetCacheItemHelper(BlockType block_type) {
114 switch (block_type) {
115 case BlockType::kData:
116 return GetCacheItemHelperForRole<Block, CacheEntryRole::kDataBlock>();
117 case BlockType::kIndex:
118 return GetCacheItemHelperForRole<Block, CacheEntryRole::kIndexBlock>();
119 case BlockType::kFilterPartitionIndex:
120 return GetCacheItemHelperForRole<Block,
121 CacheEntryRole::kFilterMetaBlock>();
122 default:
123 // Not a recognized combination
124 assert(false);
125 FALLTHROUGH_INTENDED;
126 case BlockType::kRangeDeletion:
127 return GetCacheItemHelperForRole<Block, CacheEntryRole::kOtherBlock>();
128 }
129 }
130 };
131
132 template <>
133 class BlocklikeTraits<UncompressionDict> {
134 public:
135 static UncompressionDict* Create(BlockContents&& contents,
136 size_t /* read_amp_bytes_per_bit */,
137 Statistics* /* statistics */,
138 bool using_zstd,
139 const FilterPolicy* /* filter_policy */) {
140 return new UncompressionDict(contents.data, std::move(contents.allocation),
141 using_zstd);
142 }
143
144 static uint32_t GetNumRestarts(const UncompressionDict& /* dict */) {
145 return 0;
146 }
147
148 static size_t SizeCallback(void* obj) {
149 assert(obj != nullptr);
150 UncompressionDict* ptr = static_cast<UncompressionDict*>(obj);
151 return ptr->slice_.size();
152 }
153
154 static Status SaveToCallback(void* from_obj, size_t from_offset,
155 size_t length, void* out) {
156 assert(from_obj != nullptr);
157 UncompressionDict* ptr = static_cast<UncompressionDict*>(from_obj);
158 const char* buf = ptr->slice_.data();
159 assert(length == ptr->slice_.size());
160 (void)from_offset;
161 memcpy(out, buf, length);
162 return Status::OK();
163 }
164
165 static Cache::CacheItemHelper* GetCacheItemHelper(BlockType block_type) {
166 (void)block_type;
167 assert(block_type == BlockType::kCompressionDictionary);
168 return GetCacheItemHelperForRole<UncompressionDict,
169 CacheEntryRole::kOtherBlock>();
170 }
171 };
172
173 // Get an CacheItemHelper pointer for value type T and role R.
174 template <typename T, CacheEntryRole R>
175 Cache::CacheItemHelper* GetCacheItemHelperForRole() {
176 static Cache::CacheItemHelper cache_helper(
177 BlocklikeTraits<T>::SizeCallback, BlocklikeTraits<T>::SaveToCallback,
178 GetCacheEntryDeleterForRole<T, R>());
179 return &cache_helper;
180 }
181
182 } // namespace ROCKSDB_NAMESPACE