MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ConfigValues.hpp
1 /*
2  Copyright (C) 2004-2006 MySQL AB
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #ifndef __CONFIG_VALUES_HPP
20 #define __CONFIG_VALUES_HPP
21 
22 #include <ndb_types.h>
23 #include <UtilBuffer.hpp>
24 
25 class ConfigValues {
26  friend class ConfigValuesFactory;
27  ConfigValues(Uint32 sz, Uint32 data);
28 
29 public:
30  ~ConfigValues();
31 
32  enum ValueType {
33  InvalidType = 0,
34  IntType = 1,
35  StringType = 2,
36  SectionType = 3,
37  Int64Type = 4
38  };
39 
40  struct Entry {
41  Uint32 m_key;
42  ValueType m_type;
43  union {
44  Uint32 m_int;
45  Uint64 m_int64;
46  const char * m_string;
47  };
48  };
49 
50  class ConstIterator {
51  friend class ConfigValuesFactory;
52  const ConfigValues & m_cfg;
53  public:
54  Uint32 m_currentSection;
55  ConstIterator(const ConfigValues&c) : m_cfg(c) { m_currentSection = 0;}
56 
57  bool openSection(Uint32 key, Uint32 no);
58  bool closeSection();
59 
60  bool get(Uint32 key, Entry *) const;
61 
62  bool get(Uint32 key, Uint32 * value) const;
63  bool get(Uint32 key, Uint64 * value) const;
64  bool get(Uint32 key, const char ** value) const;
65  bool getTypeOf(Uint32 key, ValueType * type) const;
66 
67  Uint32 get(Uint32 key, Uint32 notFound) const;
68  Uint64 get64(Uint32 key, Uint64 notFound) const;
69  const char * get(Uint32 key, const char * notFound) const;
70  ValueType getTypeOf(Uint32 key) const;
71  };
72 
73  class Iterator : public ConstIterator {
74  ConfigValues & m_cfg;
75  public:
76  Iterator(ConfigValues&c) : ConstIterator(c), m_cfg(c) {}
77  Iterator(ConfigValues&c, const ConstIterator& i):ConstIterator(c),m_cfg(c){
78  m_currentSection = i.m_currentSection;
79  }
80 
81  bool set(Uint32 key, Uint32 value);
82  bool set(Uint32 key, Uint64 value);
83  bool set(Uint32 key, const char * value);
84  };
85 
86  Uint32 getPackedSize() const; // get size in bytes needed to pack
87  Uint32 pack(UtilBuffer&) const;
88  Uint32 pack(void * dst, Uint32 len) const;// pack into dst(of len %d);
89 
90 private:
91  friend class Iterator;
92  friend class ConstIterator;
93 
94  bool getByPos(Uint32 pos, Entry *) const;
95  Uint64 * get64(Uint32 index) const;
96  char ** getString(Uint32 index) const;
97 
98  Uint32 m_size;
99  Uint32 m_dataSize;
100  Uint32 m_stringCount;
101  Uint32 m_int64Count;
102 
103  Uint32 m_values[1];
104  void * m_data[1];
105 };
106 
108  Uint32 m_currentSection;
109 public:
110  Uint32 m_sectionCounter;
111  Uint32 m_freeKeys;
112  Uint32 m_freeData;
113 
114 public:
115  ConfigValuesFactory(Uint32 keys = 50, Uint32 data = 10); // Initial
116  ConfigValuesFactory(ConfigValues * m_cfg); //
118 
119  ConfigValues * m_cfg;
120  ConfigValues * getConfigValues();
121 
122  bool openSection(Uint32 key, Uint32 no);
123  bool put(const ConfigValues::Entry & );
124  bool put(Uint32 key, Uint32 value);
125  bool put64(Uint32 key, Uint64 value);
126  bool put(Uint32 key, const char * value);
127  bool closeSection();
128 
129  void expand(Uint32 freeKeys, Uint32 freeData);
130  void shrink();
131 
132  bool unpack(const UtilBuffer&);
133  bool unpack(const void * src, Uint32 len);
134 
135  static ConfigValues * extractCurrentSection(const ConfigValues::ConstIterator &);
136 
137 private:
138  static ConfigValues * create(Uint32 keys, Uint32 data);
139  void put(const ConfigValues & src);
140 };
141 
142 inline
143 bool
144 ConfigValues::ConstIterator::get(Uint32 key, Uint32 * value) const {
145  Entry tmp;
146  if(get(key, &tmp) && tmp.m_type == IntType){
147  * value = tmp.m_int;
148  return true;
149  }
150  return false;
151 }
152 
153 inline
154 bool
155 ConfigValues::ConstIterator::get(Uint32 key, Uint64 * value) const {
156  Entry tmp;
157  if(get(key, &tmp) && tmp.m_type == Int64Type){
158  * value = tmp.m_int64;
159  return true;
160  }
161  return false;
162 }
163 
164 inline
165 bool
166 ConfigValues::ConstIterator::get(Uint32 key, const char ** value) const {
167  Entry tmp;
168  if(get(key, &tmp) && tmp.m_type == StringType){
169  * value = tmp.m_string;
170  return true;
171  }
172  return false;
173 }
174 
175 inline
176 bool
177 ConfigValues::ConstIterator::getTypeOf(Uint32 key, ValueType * type) const{
178  Entry tmp;
179  if(get(key, &tmp)){
180  * type = tmp.m_type;
181  return true;
182  }
183  return false;
184 }
185 
186 inline
187 Uint32
188 ConfigValues::ConstIterator::get(Uint32 key, Uint32 notFound) const {
189  Entry tmp;
190  if(get(key, &tmp) && tmp.m_type == IntType){
191  return tmp.m_int;
192  }
193  return notFound;
194 }
195 
196 inline
197 Uint64
198 ConfigValues::ConstIterator::get64(Uint32 key, Uint64 notFound) const {
199  Entry tmp;
200  if(get(key, &tmp) && tmp.m_type == Int64Type){
201  return tmp.m_int64;
202  }
203  return notFound;
204 }
205 
206 inline
207 const char *
208 ConfigValues::ConstIterator::get(Uint32 key, const char * notFound) const {
209  Entry tmp;
210  if(get(key, &tmp) && tmp.m_type == StringType){
211  return tmp.m_string;
212  }
213  return notFound;
214 }
215 
216 inline
217 ConfigValues::ValueType
218 ConfigValues::ConstIterator::getTypeOf(Uint32 key) const{
219  Entry tmp;
220  if(get(key, &tmp)){
221  return tmp.m_type;
222  }
223  return ConfigValues::InvalidType;
224 }
225 
226 inline
227 bool
228 ConfigValuesFactory::put(Uint32 key, Uint32 val){
230  tmp.m_key = key;
231  tmp.m_type = ConfigValues::IntType;
232  tmp.m_int = val;
233  return put(tmp);
234 }
235 
236 inline
237 bool
238 ConfigValuesFactory::put64(Uint32 key, Uint64 val){
240  tmp.m_key = key;
241  tmp.m_type = ConfigValues::Int64Type;
242  tmp.m_int64 = val;
243  return put(tmp);
244 }
245 
246 inline
247 bool
248 ConfigValuesFactory::put(Uint32 key, const char * val){
250  tmp.m_key = key;
251  tmp.m_type = ConfigValues::StringType;
252  tmp.m_string = val;
253  return put(tmp);
254 }
255 
256 inline
257 Uint32
258 ConfigValues::pack(UtilBuffer& buf) const {
259  Uint32 len = getPackedSize();
260  void * tmp = buf.append(len);
261  if(tmp == 0){
262  return 0;
263  }
264  return pack(tmp, len);
265 }
266 
267 inline
268 bool
269 ConfigValuesFactory::unpack(const UtilBuffer& buf){
270  return unpack(buf.get_data(), buf.length());
271 }
272 
273 #endif