MySQL 8.0.30
Source Code Documentation
properties_impl.h
Go to the documentation of this file.
1/* Copyright (c) 2014, 2022, 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 also distributed 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 included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23#ifndef DD__PROPERTIES_IMPL_INCLUDED
24#define DD__PROPERTIES_IMPL_INCLUDED
25
26#include <assert.h>
27#include <map>
28#include <memory>
29#include <set>
30#include <string>
31#include <utility>
32
33#include "lex_string.h"
34
35#include "my_inttypes.h"
36#include "sql/dd/properties.h" // dd::Properties
37#include "sql/dd/string_type.h" // dd::String_type
38
39namespace dd {
40
41///////////////////////////////////////////////////////////////////////////
42
43/**
44 The Properties_impl class implements the Properties interface.
45
46 The key=value pairs are stored in a std::map. An instance can be created
47 either by means of the default constructor, which creates an object
48 with an empty map, or alternatively, it can be created by means of the
49 static parse_properties function with a String_type argument. The string
50 is supposed to contain a semicolon separated list of key=value pairs,
51 where the characters '=' and ';' also may be part of key or value by
52 escaping using the '\' as an escape character. The escape character
53 itself must also be escaped if being part of key or value. All characters
54 between '=' and ';' are considered part of key or value, whitespace is
55 not ignored.
56
57 Escaping is removed during parsing so the strings in the map are not
58 escaped. Escaping is only relevant in the context of raw strings that
59 are to be parsed, and raw strings that are returned containing all
60 key=value pairs.
61
62 Example (note \\ due to escaping of C string literals):
63 parse_properties("a=b;b = c") -> ("a", "b"), ("b ", " c")
64 parse_properties("a\\==b;b=\\;c") -> ("a=", "b"), ("b", ";c")
65
66 get("a=") == "b"
67 get("b") == ";c"
68
69 Additional key=value pairs may be added by means of the set function,
70 which takes a string argument that is assumed to be unescaped.
71
72 Please also refer to the comments in the file properties.h where the
73 interface is defined; the functions in the interface are commented there.
74*/
75
77 private:
78 /* Map containing the actual key-value pairs. */
80
81 /* Set containing the valid keys. An empty set means any key is valid. */
82 std::set<String_type> m_keys;
83
84 public:
85 Properties_impl() = default;
86
87 /* Constructor accepting a set of valid keys. */
88 Properties_impl(const std::set<String_type> &keys) : m_keys(keys) {}
89
90 virtual const Properties_impl *impl() const { return this; }
91
92 iterator begin() override { return m_map.begin(); }
93
94 const_iterator begin() const override { return m_map.begin(); }
95
96 iterator end() override { return m_map.end(); }
97
98 const_iterator end() const override { return m_map.end(); }
99
100 size_type size() const override { return m_map.size(); }
101
102 bool empty() const override { return m_map.empty(); }
103
104 void clear() override { return m_map.clear(); }
105
106 bool valid_key(const String_type &key) const override {
107 return (m_keys.empty() || m_keys.find(key) != m_keys.end());
108 }
109
110 bool exists(const String_type &key) const override {
111 return m_map.find(key) != m_map.end();
112 }
113
114 bool remove(const String_type &key) override {
115 iterator it = m_map.find(key);
116
117 if (it == m_map.end()) return true;
118
119 m_map.erase(it);
120 return false;
121 }
122
123 /**
124 Iterate over all entries in the private hash table. For each
125 key value pair, escape both key and value, and append the strings
126 to the result. Use '=' to separate key and value, and use ';'
127 to separate pairs.
128
129 Invalid keys are not included in the output. However, there should
130 never be a situation where invalid keys are present, so we just assert
131 that the keys are valid.
132
133 @return string containing all escaped key value pairs
134 */
135 const String_type raw_string() const override;
136
137 /**
138 Get the string value for a given key.
139
140 Return true if the operation fails, i.e., if the key does not exist
141 or if the key is invalid. Assert that the key exists in debug builds.
142
143 @param key key to lookup the value for
144 @param[out] value string value
145 @return Operation outcome, false if success, otherwise true
146 */
147 bool get(const String_type &key, String_type *value) const override;
148
149 /**
150 Set the key/value. If the key is invalid, a warning is written
151 to the error log. Assert that the key exists in debug builds.
152
153 @param key Key to set.
154 @param value Value to set.
155 @return Operation outcome, false if success, otherwise true
156 */
157 bool set(const String_type &key, const String_type &value) override;
158
159 /**
160 Insert key/value pairs from a different property object.
161
162 The set of valid keys is not copied, instead, the existing
163 set in the destination object is used to ignore all invalid
164 keys.
165
166 @param properties Source object.
167
168 @retval Operation outcome, false if no error, otherwise true.
169 */
170 bool insert_values(const Properties &properties) override;
171
172 /**
173 Insert key/value pairs from a string.
174
175 Parse the string and add key/value pairs to this object.
176 The existing set of valid keys in the destination object
177 is used to ignore all invalid keys.
178
179 @param raw_string String to be parsed.
180
181 @retval Operation outcome, false if no error, otherwise true.
182 */
183 bool insert_values(const String_type &raw_string) override;
184
185#ifdef EXTRA_CODE_FOR_UNIT_TESTING
186 /**
187 Extend the set of valid keys after the property object is
188 created. This can be used e.g. for the SE private data.
189
190 @pre There must be a set of valid keys already, or the
191 map of key-value pairs must be empty. Otherwise,
192 we risk making existing keys invalid, thus hiding
193 their values.
194
195 @param keys Set of additional keys to insert into
196 the set of valid keys.
197 */
198 void add_valid_keys(const std::set<String_type> &keys) {
199 assert(!m_keys.empty() || m_map.empty());
200 m_keys.insert(keys.begin(), keys.end());
201 }
202
203 /**
204 Remove the set of valid keys after the property object is
205 created. Convenience method used by unit tests.
206 */
207 void clear_valid_keys() { m_keys.clear(); }
208
209 /**
210 Get valid key at a certain index.
211
212 If the key set is empty, return a string representation of
213 the index is returned. If the index is out of bounds, return
214 the last key.
215
216 @note This is needed by unit tests to fill in
217 random key/value pairs without breaking the
218 check for valid keys.
219
220 @param index Index at which to get the valid key.
221
222 @retval Key at the given index, a string containing the index,
223 or the last key.
224 */
225 const String_type valid_key_at(size_t index) const {
226 if (m_keys.empty()) {
227 Stringstream_type ostream;
228 ostream << index;
229 return ostream.str();
230 }
231 if (m_keys.size() <= index) {
232 return *std::next(m_keys.begin(), m_keys.size() - 1);
233 }
234 return *std::next(m_keys.begin(), index);
235 }
236#endif
237};
238
239///////////////////////////////////////////////////////////////////////////
240
241} // namespace dd
242
243#endif // DD__PROPERTIES_IMPL_INCLUDED
The Properties_impl class implements the Properties interface.
Definition: properties_impl.h:76
bool empty() const override
Are there any key=value pairs?
Definition: properties_impl.h:102
virtual const Properties_impl * impl() const
Definition: properties_impl.h:90
const_iterator begin() const override
Definition: properties_impl.h:94
std::set< String_type > m_keys
Definition: properties_impl.h:82
bool insert_values(const Properties &properties) override
Insert key/value pairs from a different property object.
Definition: properties_impl.cc:103
iterator end() override
Definition: properties_impl.h:96
bool remove(const String_type &key) override
Remove the key=value pair for the given key if it exists.
Definition: properties_impl.h:114
void clear() override
Remove all key=value pairs.
Definition: properties_impl.h:104
const String_type raw_string() const override
Iterate over all entries in the private hash table.
Definition: properties_impl.cc:59
bool set(const String_type &key, const String_type &value) override
Set the key/value.
Definition: properties_impl.cc:93
const_iterator end() const override
Definition: properties_impl.h:98
Properties::Map m_map
Definition: properties_impl.h:79
bool exists(const String_type &key) const override
Check for the existence of a key=value pair given the key.
Definition: properties_impl.h:110
bool valid_key(const String_type &key) const override
Check if the submitted key is valid.
Definition: properties_impl.h:106
iterator begin() override
Definition: properties_impl.h:92
size_type size() const override
Get the number of key=value pairs.
Definition: properties_impl.h:100
bool get(const String_type &key, String_type *value) const override
Get the string value for a given key.
Definition: properties_impl.cc:76
Properties_impl(const std::set< String_type > &keys)
Definition: properties_impl.h:88
Properties_impl()=default
The Properties class defines an interface for storing key=value pairs, where both key and value may b...
Definition: properties.h:73
std::map< String_type, String_type > Map
Definition: properties.h:127
Map::iterator iterator
Definition: properties.h:129
std::map< String_type, String_type >::size_type size_type
Definition: properties.h:128
Map::const_iterator const_iterator
Definition: properties.h:130
static uint keys
Definition: hp_test2.cc:45
Some integer typedefs for easier portability.
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:42
Char_stringstream_template< String_type_allocator > Stringstream_type
Instantiation of std::basic_stringstream with the same allocator as String_type.
Definition: string_type.h:71
Char_string_template< String_type_allocator > String_type
Definition: string_type.h:50
required string key
Definition: replication_asynchronous_connection_failover.proto:59