MySQL 8.0.39
Source Code Documentation
element_map.h
Go to the documentation of this file.
1/* Copyright (c) 2015, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef DD_CACHE__ELEMENT_MAP_INCLUDED
25#define DD_CACHE__ELEMENT_MAP_INCLUDED
26
27#include <assert.h>
28#include <cstddef> // size_t
29#include <map> // std::map
30#include <set> // std::set
31
32#include "sql/malloc_allocator.h" // Malloc_allocator.
33#include "sql/psi_memory_key.h" // key_memory_DD_cache_infrastructure
34
35namespace dd {
36namespace cache {
37
38/**
39 Implementation of a map between a key type and an element type.
40
41 The map supports storing associations between instances of the key
42 type K and pointers to the element type E. Additionally, the map provides
43 support for registering keys that have been searched for in the map
44 without being present. This allows registering a cache miss to avoid
45 situations where several similar cache misses are handled simultaneously.
46 Instead, only the first cache miss needs to be processed, while the
47 subsequent ones can just wait for the first one to complete.
48
49 There are no expectations as far as the element type E is concerned.
50 Tracking object usage, memory management, loading and evicting elements,
51 locking and synchronization, as well as registering cache misses are all
52 issues that are handled by users of this interface.
53
54 Basic assumptions regarding correct usage is implemented in terms of
55 asserts to verify that, e.g., a key does not already exist in the map
56 when it is added. There is, however, no assumptions regarding the
57 relationship between the set of missed keys and the state of the map.
58
59 There is support for representing missed keys, but the usage of this
60 feature is left to the outer layer; e.g., this class does not assert
61 that an element has been registered as missing before it is added to
62 the map.
63
64 @note The element pointer in a key/value pair may not be NULL.
65
66 @tparam K Key type.
67 @tparam E Element type (a Cache_element wrapping some dictionary
68 object type).
69*/
70
71template <typename K, typename E>
73 public:
74 typedef std::map<K, E *, std::less<K>,
76 Element_map_type; // Real map type.
77 typedef typename Element_map_type::const_iterator
78 Const_iterator; // Const iterator type.
79 typedef typename Element_map_type::iterator Iterator; // Iterator type.
80
81 private:
82 Element_map_type m_map; // The real map instance.
83 std::set<K, std::less<K>,
85 m_missed; // Cache misses being handled.
86
87 public:
89 : m_map(std::less<K>(), Malloc_allocator<std::pair<const K, E *>>(
91 m_missed(std::less<K>(),
93 } /* purecov: tested */
94
95 /**
96 Get an iterator to the beginning of the map.
97
98 Const and non-const variants.
99
100 @return Iterator to the beginning of the map.
101 */
102
103 /* purecov: begin inspected */
104 Const_iterator begin() const { return m_map.begin(); }
105 /* purecov: end */
106
107 Iterator begin() { return m_map.begin(); }
108
109 /**
110 Get an iterator to one past the end of the map.
111
112 Const and non-const variants.
113
114 @return Iterator to one past the end of the map.
115 */
116
117 /* purecov: begin inspected */
118 Const_iterator end() const { return m_map.end(); }
119 /* purecov: end */
120
121 Iterator end() { return m_map.end(); }
122
123 /**
124 Return the number of elements in the map.
125
126 @return Number of elements in the map.
127 */
128
129 size_t size() const { return m_map.size(); }
130
131 /**
132 Check if the given key is present in the map.
133
134 If the key is present in the map, return true, otherwise false.
135
136 @param key The key to look for.
137
138 @return true if present, otherwise false.
139 */
140
141 bool is_present(const K &key) const { return m_map.find(key) != m_map.end(); }
142
143 /**
144 Get the element associated with the given key.
145
146 If the element is present in the map, return a pointer to it. If the
147 element is not present, return NULL as the element pointer.
148
149 @param key The key to use for looking up the element.
150 @param [out] element The element associated with the key (if present),
151 or NULL (if not present).
152 */
153
154 void get(const K &key, E **element) const {
155 assert(element);
156 typename Element_map_type::const_iterator it = m_map.find(key);
157 if (it == m_map.end())
158 *element = nullptr;
159 else {
160 assert(it->second);
161 *element = it->second;
162 }
163 }
164
165 /**
166 Put the element into the map and associate it with the given key.
167
168 The element may not be NULL, and the key may not be already present.
169
170 @param key The key to be associated with the element.
171 @param element The element to be associated with the key.
172 */
173
174 void put(const K &key, E *element) {
175 assert(element);
176 assert(m_map.find(key) == m_map.end());
177 m_map.insert(typename Element_map_type::value_type(key, element));
178 }
179
180 /**
181 Remove an element from the map.
182
183 The key/value pair, as indicated by the key, is removed from the map.
184 The element being pointed to is not deleted. The key must be present in
185 the map.
186
187 @param key The key to be removed.
188 */
189
190 void remove(const K &key) {
191 assert(m_map.find(key) != m_map.end());
192 m_map.erase(key);
193 }
194
195 /**
196 Check if the given key has been missed in the cache.
197
198 @param key Key representing element to check for recent cache miss.
199 @return true if the element has been missed, otherwise false.
200 */
201
202 bool is_missed(const K &key) const {
203 return m_missed.find(key) != m_missed.end();
204 }
205
206 /**
207 Register the given key as being missed in the cache.
208
209 The key cannot already be reported as missing.
210
211 @param key Key representing element missed in the cache.
212 */
213
214 void set_missed(const K &key) {
215 assert(m_missed.find(key) == m_missed.end());
216 m_missed.insert(key);
217 }
218
219 /**
220 Register that the miss of a key has been handled.
221
222 The key must have been reported as missing.
223
224 @param key Key representing element previously missed in the cache.
225 */
226
227 void set_miss_handled(const K &key) {
228 assert(m_missed.find(key) != m_missed.end());
229 m_missed.erase(key);
230 }
231
232 /**
233 Debug dump of the element map to stderr.
234
235 Iterates over all elements and dumps debug information for each
236 of them.
237 */
238
239 /* purecov: begin inspected */
240 void dump() const {
241#ifndef NDEBUG
243 for (it = m_map.begin(); it != m_map.end(); it++) it->second->dump();
244#endif
245 }
246 /* purecov: end */
247};
248
249} // namespace cache
250} // namespace dd
251
252#endif // DD_CACHE__ELEMENT_MAP_INCLUDED
Malloc_allocator is a C++ STL memory allocator based on my_malloc/my_free.
Definition: malloc_allocator.h:63
Implementation of a map between a key type and an element type.
Definition: element_map.h:72
Element_map()
Definition: element_map.h:88
Element_map_type::iterator Iterator
Definition: element_map.h:79
bool is_missed(const K &key) const
Check if the given key has been missed in the cache.
Definition: element_map.h:202
bool is_present(const K &key) const
Check if the given key is present in the map.
Definition: element_map.h:141
size_t size() const
Return the number of elements in the map.
Definition: element_map.h:129
void set_missed(const K &key)
Register the given key as being missed in the cache.
Definition: element_map.h:214
Const_iterator begin() const
Get an iterator to the beginning of the map.
Definition: element_map.h:104
void get(const K &key, E **element) const
Get the element associated with the given key.
Definition: element_map.h:154
std::map< K, E *, std::less< K >, Malloc_allocator< std::pair< const K, E * > > > Element_map_type
Definition: element_map.h:76
std::set< K, std::less< K >, Malloc_allocator< K > > m_missed
Definition: element_map.h:85
Const_iterator end() const
Get an iterator to one past the end of the map.
Definition: element_map.h:118
Element_map_type::const_iterator Const_iterator
Definition: element_map.h:78
void remove(const K &key)
Remove an element from the map.
Definition: element_map.h:190
Element_map_type m_map
Definition: element_map.h:82
void put(const K &key, E *element)
Put the element into the map and associate it with the given key.
Definition: element_map.h:174
Iterator begin()
Definition: element_map.h:107
void dump() const
Debug dump of the element map to stderr.
Definition: element_map.h:240
Iterator end()
Definition: element_map.h:121
void set_miss_handled(const K &key)
Register that the miss of a key has been handled.
Definition: element_map.h:227
uint16_t value_type
Definition: vt100.h:184
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:43
Definition: gcs_xcom_synode.h:64
PSI_memory_key key_memory_DD_cache_infrastructure
Definition: psi_memory_key.cc:37
required string key
Definition: replication_asynchronous_connection_failover.proto:60