~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to storage/ndb/include/util/ConfigValues.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

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