-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathelement_map.h
252 lines (188 loc) · 7.72 KB
/
element_map.h
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* Copyright (c) 2015, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#ifndef DD_CACHE__ELEMENT_MAP_INCLUDED
#define DD_CACHE__ELEMENT_MAP_INCLUDED
#include <assert.h>
#include <cstddef> // size_t
#include <map> // std::map
#include <set> // std::set
#include "sql/malloc_allocator.h" // Malloc_allocator.
#include "sql/psi_memory_key.h" // key_memory_DD_cache_infrastructure
namespace dd {
namespace cache {
/**
Implementation of a map between a key type and an element type.
The map supports storing associations between instances of the key
type K and pointers to the element type E. Additionally, the map provides
support for registering keys that have been searched for in the map
without being present. This allows registering a cache miss to avoid
situations where several similar cache misses are handled simultaneously.
Instead, only the first cache miss needs to be processed, while the
subsequent ones can just wait for the first one to complete.
There are no expectations as far as the element type E is concerned.
Tracking object usage, memory management, loading and evicting elements,
locking and synchronization, as well as registering cache misses are all
issues that are handled by users of this interface.
Basic assumptions regarding correct usage is implemented in terms of
asserts to verify that, e.g., a key does not already exist in the map
when it is added. There is, however, no assumptions regarding the
relationship between the set of missed keys and the state of the map.
There is support for representing missed keys, but the usage of this
feature is left to the outer layer; e.g., this class does not assert
that an element has been registered as missing before it is added to
the map.
@note The element pointer in a key/value pair may not be NULL.
@tparam K Key type.
@tparam E Element type (a Cache_element wrapping some dictionary
object type).
*/
template <typename K, typename E>
class Element_map {
public:
typedef std::map<K, E *, std::less<K>,
Malloc_allocator<std::pair<const K, E *>>>
Element_map_type; // Real map type.
typedef typename Element_map_type::const_iterator
Const_iterator; // Const iterator type.
typedef typename Element_map_type::iterator Iterator; // Iterator type.
private:
Element_map_type m_map; // The real map instance.
std::set<K, std::less<K>,
Malloc_allocator<K>>
m_missed; // Cache misses being handled.
public:
Element_map()
: m_map(std::less<K>(), Malloc_allocator<std::pair<const K, E *>>(
key_memory_DD_cache_infrastructure)),
m_missed(std::less<K>(),
Malloc_allocator<K>(key_memory_DD_cache_infrastructure)) {
} /* purecov: tested */
/**
Get an iterator to the beginning of the map.
Const and non-const variants.
@return Iterator to the beginning of the map.
*/
/* purecov: begin inspected */
Const_iterator begin() const { return m_map.begin(); }
/* purecov: end */
Iterator begin() { return m_map.begin(); }
/**
Get an iterator to one past the end of the map.
Const and non-const variants.
@return Iterator to one past the end of the map.
*/
/* purecov: begin inspected */
Const_iterator end() const { return m_map.end(); }
/* purecov: end */
Iterator end() { return m_map.end(); }
/**
Return the number of elements in the map.
@return Number of elements in the map.
*/
size_t size() const { return m_map.size(); }
/**
Check if the given key is present in the map.
If the key is present in the map, return true, otherwise false.
@param key The key to look for.
@return true if present, otherwise false.
*/
bool is_present(const K &key) const { return m_map.find(key) != m_map.end(); }
/**
Get the element associated with the given key.
If the element is present in the map, return a pointer to it. If the
element is not present, return NULL as the element pointer.
@param key The key to use for looking up the element.
@param [out] element The element associated with the key (if present),
or NULL (if not present).
*/
void get(const K &key, E **element) const {
assert(element);
typename Element_map_type::const_iterator it = m_map.find(key);
if (it == m_map.end())
*element = nullptr;
else {
assert(it->second);
*element = it->second;
}
}
/**
Put the element into the map and associate it with the given key.
The element may not be NULL, and the key may not be already present.
@param key The key to be associated with the element.
@param element The element to be associated with the key.
*/
void put(const K &key, E *element) {
assert(element);
assert(m_map.find(key) == m_map.end());
m_map.insert(typename Element_map_type::value_type(key, element));
}
/**
Remove an element from the map.
The key/value pair, as indicated by the key, is removed from the map.
The element being pointed to is not deleted. The key must be present in
the map.
@param key The key to be removed.
*/
void remove(const K &key) {
assert(m_map.find(key) != m_map.end());
m_map.erase(key);
}
/**
Check if the given key has been missed in the cache.
@param key Key representing element to check for recent cache miss.
@return true if the element has been missed, otherwise false.
*/
bool is_missed(const K &key) const {
return m_missed.find(key) != m_missed.end();
}
/**
Register the given key as being missed in the cache.
The key cannot already be reported as missing.
@param key Key representing element missed in the cache.
*/
void set_missed(const K &key) {
assert(m_missed.find(key) == m_missed.end());
m_missed.insert(key);
}
/**
Register that the miss of a key has been handled.
The key must have been reported as missing.
@param key Key representing element previously missed in the cache.
*/
void set_miss_handled(const K &key) {
assert(m_missed.find(key) != m_missed.end());
m_missed.erase(key);
}
/**
Debug dump of the element map to stderr.
Iterates over all elements and dumps debug information for each
of them.
*/
/* purecov: begin inspected */
void dump() const {
#ifndef NDEBUG
Const_iterator it;
for (it = m_map.begin(); it != m_map.end(); it++) it->second->dump();
#endif
}
/* purecov: end */
};
} // namespace cache
} // namespace dd
#endif // DD_CACHE__ELEMENT_MAP_INCLUDED