~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/rpl_utility.h

  • Committer: Stewart Smith
  • Date: 2008-07-13 08:00:16 UTC
  • mto: (210.1.1 drizzle)
  • mto: This revision was merged to the branch mainline in revision 211.
  • Revision ID: stewart@flamingspork.com-20080713080016-8qtjv9ypt42azzr6
CRC32() as UDF

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 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 RPL_UTILITY_H
 
17
#define RPL_UTILITY_H
 
18
 
 
19
#ifndef __cplusplus
 
20
#error "Don't include this C++ header file from a non-C++ file!"
 
21
#endif
 
22
 
 
23
#include "mysql_priv.h"
 
24
 
 
25
class Relay_log_info;
 
26
 
 
27
 
 
28
/**
 
29
  A table definition from the master.
 
30
 
 
31
  The responsibilities of this class is:
 
32
  - Extract and decode table definition data from the table map event
 
33
  - Check if table definition in table map is compatible with table
 
34
    definition on slave
 
35
 
 
36
  Currently, the only field type data available is an array of the
 
37
  type operators that are present in the table map event.
 
38
 
 
39
  @todo Add type operands to this structure to allow detection of
 
40
     difference between, e.g., BIT(5) and BIT(10).
 
41
 */
 
42
 
 
43
class table_def
 
44
{
 
45
public:
 
46
  /**
 
47
    Convenience declaration of the type of the field type data in a
 
48
    table map event.
 
49
  */
 
50
  typedef unsigned char field_type;
 
51
 
 
52
  /**
 
53
    Constructor.
 
54
 
 
55
    @param types Array of types
 
56
    @param size  Number of elements in array 'types'
 
57
    @param field_metadata Array of extra information about fields
 
58
    @param metadata_size Size of the field_metadata array
 
59
    @param null_bitmap The bitmap of fields that can be null
 
60
   */
 
61
  table_def(field_type *types, uint32_t size, unsigned char *field_metadata, 
 
62
      int metadata_size, unsigned char *null_bitmap)
 
63
    : m_size(size), m_type(0), m_field_metadata_size(metadata_size),
 
64
      m_field_metadata(0), m_null_bits(0), m_memory(NULL)
 
65
  {
 
66
    m_memory= (unsigned char *)my_multi_malloc(MYF(MY_WME),
 
67
                                       &m_type, size,
 
68
                                       &m_field_metadata,
 
69
                                       size * sizeof(uint16_t),
 
70
                                       &m_null_bits, (size + 7) / 8,
 
71
                                       NULL);
 
72
 
 
73
    bzero(m_field_metadata, size * sizeof(uint16_t));
 
74
 
 
75
    if (m_type)
 
76
      memcpy(m_type, types, size);
 
77
    else
 
78
      m_size= 0;
 
79
    /*
 
80
      Extract the data from the table map into the field metadata array
 
81
      iff there is field metadata. The variable metadata_size will be
 
82
      0 if we are replicating from an older version server since no field
 
83
      metadata was written to the table map. This can also happen if 
 
84
      there were no fields in the master that needed extra metadata.
 
85
    */
 
86
    if (m_size && metadata_size)
 
87
    { 
 
88
      int index= 0;
 
89
      for (unsigned int i= 0; i < m_size; i++)
 
90
      {
 
91
        switch (m_type[i]) {
 
92
        case MYSQL_TYPE_BLOB:
 
93
        case MYSQL_TYPE_DOUBLE:
 
94
        case MYSQL_TYPE_FLOAT:
 
95
        {
 
96
          /*
 
97
            These types store a single byte.
 
98
          */
 
99
          m_field_metadata[i]= field_metadata[index];
 
100
          index++;
 
101
          break;
 
102
        }
 
103
        case MYSQL_TYPE_SET:
 
104
        case MYSQL_TYPE_ENUM:
 
105
        case MYSQL_TYPE_STRING:
 
106
        {
 
107
          uint16_t x= field_metadata[index++] << 8U; // real_type
 
108
          x+= field_metadata[index++];            // pack or field length
 
109
          m_field_metadata[i]= x;
 
110
          break;
 
111
        }
 
112
        case MYSQL_TYPE_VARCHAR:
 
113
        {
 
114
          /*
 
115
            These types store two bytes.
 
116
          */
 
117
          char *ptr= (char *)&field_metadata[index];
 
118
          m_field_metadata[i]= uint2korr(ptr);
 
119
          index= index + 2;
 
120
          break;
 
121
        }
 
122
        case MYSQL_TYPE_NEWDECIMAL:
 
123
        {
 
124
          uint16_t x= field_metadata[index++] << 8U; // precision
 
125
          x+= field_metadata[index++];            // decimals
 
126
          m_field_metadata[i]= x;
 
127
          break;
 
128
        }
 
129
        default:
 
130
          m_field_metadata[i]= 0;
 
131
          break;
 
132
        }
 
133
      }
 
134
    }
 
135
    if (m_size && null_bitmap)
 
136
       memcpy(m_null_bits, null_bitmap, (m_size + 7) / 8);
 
137
  }
 
138
 
 
139
  ~table_def() {
 
140
    my_free(m_memory, MYF(0));
 
141
    m_type= 0;
 
142
    m_size= 0;
 
143
  }
 
144
 
 
145
  /**
 
146
    Return the number of fields there is type data for.
 
147
 
 
148
    @return The number of fields that there is type data for.
 
149
   */
 
150
  uint32_t size() const { return m_size; }
 
151
 
 
152
 
 
153
  /*
 
154
    Return a representation of the type data for one field.
 
155
 
 
156
    @param index Field index to return data for
 
157
 
 
158
    @return Will return a representation of the type data for field
 
159
    <code>index</code>. Currently, only the type identifier is
 
160
    returned.
 
161
   */
 
162
  field_type type(uint32_t index) const
 
163
  {
 
164
    assert(index < m_size);
 
165
    return m_type[index];
 
166
  }
 
167
 
 
168
 
 
169
  /*
 
170
    This function allows callers to get the extra field data from the
 
171
    table map for a given field. If there is no metadata for that field
 
172
    or there is no extra metadata at all, the function returns 0.
 
173
 
 
174
    The function returns the value for the field metadata for column at 
 
175
    position indicated by index. As mentioned, if the field was a type 
 
176
    that stores field metadata, that value is returned else zero (0) is 
 
177
    returned. This method is used in the unpack() methods of the 
 
178
    corresponding fields to properly extract the data from the binary log 
 
179
    in the event that the master's field is smaller than the slave.
 
180
  */
 
181
  uint16_t field_metadata(uint32_t index) const
 
182
  {
 
183
    assert(index < m_size);
 
184
    if (m_field_metadata_size)
 
185
      return m_field_metadata[index];
 
186
    else
 
187
      return 0;
 
188
  }
 
189
 
 
190
  /*
 
191
    This function returns whether the field on the master can be null.
 
192
    This value is derived from field->maybe_null().
 
193
  */
 
194
  my_bool maybe_null(uint32_t index) const
 
195
  {
 
196
    assert(index < m_size);
 
197
    return ((m_null_bits[(index / 8)] & 
 
198
            (1 << (index % 8))) == (1 << (index %8)));
 
199
  }
 
200
 
 
201
  /*
 
202
    This function returns the field size in raw bytes based on the type
 
203
    and the encoded field data from the master's raw data. This method can 
 
204
    be used for situations where the slave needs to skip a column (e.g., 
 
205
    WL#3915) or needs to advance the pointer for the fields in the raw 
 
206
    data from the master to a specific column.
 
207
  */
 
208
  uint32 calc_field_size(uint32_t col, unsigned char *master_data) const;
 
209
 
 
210
  /**
 
211
    Decide if the table definition is compatible with a table.
 
212
 
 
213
    Compare the definition with a table to see if it is compatible
 
214
    with it.
 
215
 
 
216
    A table definition is compatible with a table if:
 
217
      - the columns types of the table definition is a (not
 
218
        necessarily proper) prefix of the column type of the table, or
 
219
      - the other way around
 
220
 
 
221
    @param rli   Pointer to relay log info
 
222
    @param table Pointer to table to compare with.
 
223
 
 
224
    @retval 1  if the table definition is not compatible with @c table
 
225
    @retval 0  if the table definition is compatible with @c table
 
226
  */
 
227
  int compatible_with(Relay_log_info const *rli, TABLE *table) const;
 
228
 
 
229
private:
 
230
  uint32_t m_size;           // Number of elements in the types array
 
231
  field_type *m_type;                     // Array of type descriptors
 
232
  uint32_t m_field_metadata_size;
 
233
  uint16_t *m_field_metadata;
 
234
  unsigned char *m_null_bits;
 
235
  unsigned char *m_memory;
 
236
};
 
237
 
 
238
/**
 
239
   Extend the normal table list with a few new fields needed by the
 
240
   slave thread, but nowhere else.
 
241
 */
 
242
struct RPL_TABLE_LIST
 
243
  : public TABLE_LIST
 
244
{
 
245
  bool m_tabledef_valid;
 
246
  table_def m_tabledef;
 
247
};
 
248
 
 
249
 
 
250
/* Anonymous namespace for template functions/classes */
 
251
namespace {
 
252
 
 
253
  /*
 
254
    Smart pointer that will automatically call my_afree (a macro) when
 
255
    the pointer goes out of scope.  This is used so that I do not have
 
256
    to remember to call my_afree() before each return.  There is no
 
257
    overhead associated with this, since all functions are inline.
 
258
 
 
259
    I (Matz) would prefer to use the free function as a template
 
260
    parameter, but that is not possible when the "function" is a
 
261
    macro.
 
262
  */
 
263
  template <class Obj>
 
264
  class auto_afree_ptr
 
265
  {
 
266
    Obj* m_ptr;
 
267
  public:
 
268
    auto_afree_ptr(Obj* ptr) : m_ptr(ptr) { }
 
269
    ~auto_afree_ptr() { if (m_ptr) my_afree(m_ptr); }
 
270
    void assign(Obj* ptr) {
 
271
      /* Only to be called if it hasn't been given a value before. */
 
272
      assert(m_ptr == NULL);
 
273
      m_ptr= ptr;
 
274
    }
 
275
    Obj* get() { return m_ptr; }
 
276
  };
 
277
 
 
278
}
 
279
 
 
280
#endif /* RPL_UTILITY_H */