mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / storage / ndb / include / util / SimpleProperties.hpp
blobd7df4a03e2a0ea2778aaea76338bf26821e9840b
1 /* Copyright (c) 2003-2007 MySQL AB
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.
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.
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 Street, Fifth Floor, Boston, MA 02110-1301, USA */
16 #ifndef SIMPLE_PROPERTIES_HPP
17 #define SIMPLE_PROPERTIES_HPP
19 #include <ndb_global.h>
20 #include <NdbOut.hpp>
22 /**
23 * @class SimpleProperties
24 * @brief Key-value-pair container. Actully a list of named elements.
26 * SimpleProperties:
27 * - The keys are Uint16
28 * - The values are either Uint32 or null terminated c-strings
30 * @note Keys may be repeated.
32 * Examples of things that can be stored in a SimpleProperties object:
33 * - Lists like: ((1, "foo"), (2, "bar"), (3, 32), (2, "baz"))
35 class SimpleProperties {
36 public:
37 /**
38 * Value types
40 enum ValueType {
41 Uint32Value = 0,
42 StringValue = 1,
43 BinaryValue = 2,
44 InvalidValue = 3
47 /**
48 * Struct for defining mapping to be used with unpack
50 struct SP2StructMapping {
51 Uint16 Key;
52 Uint32 Offset;
53 ValueType Type;
54 Uint32 minValue;
55 Uint32 maxValue;
56 Uint32 Length_Offset; // Offset used for looking up length of
57 // data if Type = BinaryValue
60 /**
61 * UnpackStatus - Value returned from unpack
63 enum UnpackStatus {
64 Eof = 0, // Success, end of SimpleProperties object reached
65 Break = 1, // Success
66 TypeMismatch = 2,
67 ValueTooLow = 3,
68 ValueTooHigh = 4,
69 UnknownKey = 5,
70 OutOfMemory = 6 // Only used when packing
73 /**
74 * Unpack
76 class Reader;
77 static UnpackStatus unpack(class Reader & it,
78 void * dst,
79 const SP2StructMapping[], Uint32 mapSz,
80 bool ignoreMinMax,
81 bool ignoreUnknownKeys);
83 class Writer;
84 static UnpackStatus pack(class Writer &,
85 const void * src,
86 const SP2StructMapping[], Uint32 mapSz,
87 bool ignoreMinMax);
89 /**
90 * Reader class
92 class Reader {
93 public:
94 virtual ~Reader() {}
96 /**
97 * Move to first element
98 * Return true if element exist
100 bool first();
103 * Move to next element
104 * Return true if element exist
106 bool next();
109 * Is this valid
111 bool valid() const;
114 * Get key
115 * Note only valid is valid() == true
117 Uint16 getKey() const;
120 * Get value length in bytes - (including terminating 0 for strings)
121 * Note only valid is valid() == true
123 Uint16 getValueLen() const;
126 * Get value type
127 * Note only valid is valid() == true
129 ValueType getValueType() const;
132 * Get value
133 * Note only valid is valid() == true
135 Uint32 getUint32() const;
136 char * getString(char * dst) const;
139 * Print the complete simple properties (for debugging)
141 void printAll(NdbOut& ndbout);
143 private:
144 bool readValue();
146 Uint16 m_key;
147 Uint16 m_itemLen;
148 union {
149 Uint32 m_ui32_value;
150 Uint32 m_strLen; // Including 0-byte in words
152 ValueType m_type;
153 protected:
154 Reader();
155 virtual void reset() = 0;
157 virtual bool step(Uint32 len) = 0;
158 virtual bool getWord(Uint32 * dst) = 0;
159 virtual bool peekWord(Uint32 * dst) const = 0;
160 virtual bool peekWords(Uint32 * dst, Uint32 len) const = 0;
164 * Writer class
166 class Writer {
167 public:
168 Writer() {}
170 bool first();
171 bool add(Uint16 key, Uint32 value);
172 bool add(Uint16 key, const char * value);
173 bool add(Uint16 key, const void* value, int len);
174 protected:
175 virtual ~Writer() {}
176 virtual bool reset() = 0;
177 virtual bool putWord(Uint32 val) = 0;
178 virtual bool putWords(const Uint32 * src, Uint32 len) = 0;
179 private:
180 bool add(const char* value, int len);
185 * Reader for linear memory
187 class SimplePropertiesLinearReader : public SimpleProperties::Reader {
188 public:
189 SimplePropertiesLinearReader(const Uint32 * src, Uint32 len);
190 virtual ~SimplePropertiesLinearReader() {}
192 virtual void reset();
193 virtual bool step(Uint32 len);
194 virtual bool getWord(Uint32 * dst);
195 virtual bool peekWord(Uint32 * dst) const ;
196 virtual bool peekWords(Uint32 * dst, Uint32 len) const;
197 private:
198 Uint32 m_len;
199 Uint32 m_pos;
200 const Uint32 * m_src;
204 * Writer for linear memory
206 class LinearWriter : public SimpleProperties::Writer {
207 public:
208 LinearWriter(Uint32 * src, Uint32 len);
209 virtual ~LinearWriter() {}
211 virtual bool reset();
212 virtual bool putWord(Uint32 val);
213 virtual bool putWords(const Uint32 * src, Uint32 len);
214 Uint32 getWordsUsed() const;
215 private:
216 Uint32 m_len;
217 Uint32 m_pos;
218 Uint32 * m_src;
222 * Writer for UtilBuffer
224 class UtilBufferWriter : public SimpleProperties::Writer {
225 public:
226 UtilBufferWriter(class UtilBuffer & buf);
227 virtual ~UtilBufferWriter() {}
229 virtual bool reset();
230 virtual bool putWord(Uint32 val);
231 virtual bool putWords(const Uint32 * src, Uint32 len);
232 Uint32 getWordsUsed() const;
233 private:
234 class UtilBuffer & m_buf;
238 * Reader for long signal section memory
241 * Implemented in kernel/vm/SimplePropertiesSection.cpp
243 class SimplePropertiesSectionReader : public SimpleProperties::Reader {
244 public:
245 SimplePropertiesSectionReader(struct SegmentedSectionPtr &,
246 class SectionSegmentPool &);
247 virtual ~SimplePropertiesSectionReader() {}
249 virtual void reset();
250 virtual bool step(Uint32 len);
251 virtual bool getWord(Uint32 * dst);
252 virtual bool peekWord(Uint32 * dst) const ;
253 virtual bool peekWords(Uint32 * dst, Uint32 len) const;
254 Uint32 getSize() const;
255 bool getWords(Uint32 * dst, Uint32 len);
257 private:
258 Uint32 m_pos;
259 Uint32 m_len;
260 class SectionSegmentPool & m_pool;
261 struct SectionSegment * m_head;
262 struct SectionSegment * m_currentSegment;
265 inline
266 Uint32 SimplePropertiesSectionReader::getSize() const
268 return m_len;
272 * Writer for long signal section memory
275 * Implemented in kernel/vm/SimplePropertiesSection.cpp
277 class SimplePropertiesSectionWriter : public SimpleProperties::Writer {
278 public:
279 SimplePropertiesSectionWriter(class SectionSegmentPool &);
280 virtual ~SimplePropertiesSectionWriter() {}
282 virtual bool reset();
283 virtual bool putWord(Uint32 val);
284 virtual bool putWords(const Uint32 * src, Uint32 len);
287 * This "unlinks" the writer from the memory
289 void getPtr(struct SegmentedSectionPtr & dst);
291 private:
292 Int32 m_pos;
293 Uint32 m_sz;
294 class SectionSegmentPool & m_pool;
295 struct SectionSegment * m_head;
296 Uint32 m_prevPtrI; // Prev to m_currentSegment
297 struct SectionSegment * m_currentSegment;
300 #endif