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

« back to all changes in this revision

Viewing changes to storage/ndb/include/util/SimpleProperties.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) 2003 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#ifndef SIMPLE_PROPERTIES_HPP
 
17
#define SIMPLE_PROPERTIES_HPP
 
18
 
 
19
#include <ndb_global.h>
 
20
#include <NdbOut.hpp>
 
21
 
 
22
/**
 
23
 * @class SimpleProperties
 
24
 * @brief Key-value-pair container.  Actully a list of named elements.
 
25
 *
 
26
 * SimpleProperties:
 
27
 * - The keys are Uint16
 
28
 * - The values are either Uint32 or null terminated c-strings
 
29
 * 
 
30
 * @note  Keys may be repeated.
 
31
 * 
 
32
 * Examples of things that can be stored in a SimpleProperties object:
 
33
 * - Lists like: ((1, "foo"), (2, "bar"), (3, 32), (2, "baz"))
 
34
 */
 
35
class SimpleProperties {
 
36
public:
 
37
  /**
 
38
   * Value types
 
39
   */
 
40
   enum ValueType {
 
41
    Uint32Value  = 0,
 
42
    StringValue  = 1,
 
43
    BinaryValue  = 2,
 
44
    InvalidValue = 3
 
45
   };
 
46
 
 
47
  /**
 
48
   * Struct for defining mapping to be used with unpack
 
49
   */
 
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
 
58
  };
 
59
 
 
60
  /**
 
61
   * UnpackStatus - Value returned from unpack
 
62
   */
 
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
 
71
  };
 
72
 
 
73
  /**
 
74
   * Unpack
 
75
   */
 
76
  class Reader;
 
77
  static UnpackStatus unpack(class Reader & it, 
 
78
                             void * dst, 
 
79
                             const SP2StructMapping[], Uint32 mapSz,
 
80
                             bool ignoreMinMax,
 
81
                             bool ignoreUnknownKeys);
 
82
  
 
83
  class Writer;
 
84
  static UnpackStatus pack(class Writer &,
 
85
                           const void * src,
 
86
                           const SP2StructMapping[], Uint32 mapSz, 
 
87
                           bool ignoreMinMax);
 
88
  
 
89
  /**
 
90
   * Reader class
 
91
   */
 
92
  class Reader {
 
93
  public:
 
94
    virtual ~Reader() {}
 
95
 
 
96
    /**
 
97
     * Move to first element
 
98
     *   Return true if element exist
 
99
     */
 
100
    bool first();
 
101
    
 
102
    /**
 
103
     * Move to next element
 
104
     *   Return true if element exist
 
105
     */
 
106
    bool next();
 
107
    
 
108
    /**
 
109
     * Is this valid
 
110
     */
 
111
    bool valid() const;
 
112
 
 
113
    /**
 
114
     * Get key
 
115
     *  Note only valid is valid() == true
 
116
     */
 
117
    Uint16 getKey() const;
 
118
 
 
119
    /**
 
120
     * Get value length in bytes - (including terminating 0 for strings)
 
121
     *  Note only valid is valid() == true
 
122
     */
 
123
    Uint16 getValueLen() const;
 
124
 
 
125
    /**
 
126
     * Get value type
 
127
     *  Note only valid is valid() == true
 
128
     */
 
129
    ValueType getValueType() const;
 
130
    
 
131
    /**
 
132
     * Get value
 
133
     *  Note only valid is valid() == true
 
134
     */
 
135
    Uint32 getUint32() const;
 
136
    char * getString(char * dst) const;
 
137
    
 
138
    /**
 
139
     * Print the complete simple properties (for debugging)
 
140
     */
 
141
    void printAll(NdbOut& ndbout);
 
142
 
 
143
  private:
 
144
    bool readValue();
 
145
    
 
146
    Uint16 m_key;
 
147
    Uint16 m_itemLen;
 
148
    union {
 
149
      Uint32 m_ui32_value;
 
150
      Uint32 m_strLen; // Including 0-byte in words
 
151
    };
 
152
    ValueType m_type;
 
153
  protected:
 
154
    Reader();
 
155
    virtual void reset() = 0;
 
156
    
 
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;
 
161
  };
 
162
 
 
163
  /**
 
164
   * Writer class
 
165
   */
 
166
  class Writer {
 
167
  public:
 
168
    Writer() {}
 
169
 
 
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);
 
181
  };
 
182
};
 
183
 
 
184
/**
 
185
 * Reader for linear memory
 
186
 */
 
187
class SimplePropertiesLinearReader : public SimpleProperties::Reader {
 
188
public:
 
189
  SimplePropertiesLinearReader(const Uint32 * src, Uint32 len);
 
190
  virtual ~SimplePropertiesLinearReader() {}
 
191
  
 
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;
 
201
};
 
202
 
 
203
/**
 
204
 * Writer for linear memory
 
205
 */
 
206
class LinearWriter : public SimpleProperties::Writer {
 
207
public:
 
208
  LinearWriter(Uint32 * src, Uint32 len);
 
209
  virtual ~LinearWriter() {}
 
210
 
 
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;
 
219
};
 
220
 
 
221
/**
 
222
 * Writer for UtilBuffer
 
223
 */
 
224
class UtilBufferWriter : public SimpleProperties::Writer {
 
225
public:
 
226
  UtilBufferWriter(class UtilBuffer & buf);
 
227
  virtual ~UtilBufferWriter() {}
 
228
  
 
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;
 
235
};
 
236
 
 
237
/**
 
238
 * Reader for long signal section memory
 
239
 *
 
240
 *
 
241
 * Implemented in kernel/vm/SimplePropertiesSection.cpp
 
242
 */
 
243
class SimplePropertiesSectionReader : public SimpleProperties::Reader {
 
244
public:
 
245
  SimplePropertiesSectionReader(struct SegmentedSectionPtr &,
 
246
                                class SectionSegmentPool &);
 
247
  virtual ~SimplePropertiesSectionReader() {}
 
248
  
 
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);
 
256
 
 
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;
 
263
};
 
264
 
 
265
inline
 
266
Uint32 SimplePropertiesSectionReader::getSize() const
 
267
{
 
268
  return m_len;
 
269
}
 
270
 
 
271
/**
 
272
 * Writer for long signal section memory
 
273
 *
 
274
 *
 
275
 * Implemented in kernel/vm/SimplePropertiesSection.cpp
 
276
 */
 
277
class SimplePropertiesSectionWriter : public SimpleProperties::Writer {
 
278
public:
 
279
  SimplePropertiesSectionWriter(class SectionSegmentPool &);
 
280
  virtual ~SimplePropertiesSectionWriter() {}
 
281
 
 
282
  virtual bool reset();
 
283
  virtual bool putWord(Uint32 val);
 
284
  virtual bool putWords(const Uint32 * src, Uint32 len);
 
285
 
 
286
  /**
 
287
   * This "unlinks" the writer from the memory
 
288
   */
 
289
  void getPtr(struct SegmentedSectionPtr & dst);
 
290
  
 
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;
 
298
};
 
299
 
 
300
#endif