~drizzle-trunk/drizzle/development

1 by brian
clean slate
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
   */
130 by Brian Aker
ulong cleanup
61
  table_def(field_type *types, uint32_t size, unsigned char *field_metadata, 
62
      int metadata_size, unsigned char *null_bitmap)
1 by brian
clean slate
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
  {
130 by Brian Aker
ulong cleanup
66
    m_memory= (unsigned char *)my_multi_malloc(MYF(MY_WME),
1 by brian
clean slate
67
                                       &m_type, size,
68
                                       &m_field_metadata,
130 by Brian Aker
ulong cleanup
69
                                       size * sizeof(uint16_t),
1 by brian
clean slate
70
                                       &m_null_bits, (size + 7) / 8,
71
                                       NULL);
72
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
73
    memset(m_field_metadata, 0, size * sizeof(uint16_t));
1 by brian
clean slate
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]) {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
92
        case DRIZZLE_TYPE_BLOB:
93
        case DRIZZLE_TYPE_DOUBLE:
1 by brian
clean slate
94
        {
95
          /*
96
            These types store a single byte.
97
          */
98
          m_field_metadata[i]= field_metadata[index];
99
          index++;
100
          break;
101
        }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
102
        case DRIZZLE_TYPE_SET:
103
        case DRIZZLE_TYPE_ENUM:
104
        case DRIZZLE_TYPE_STRING:
1 by brian
clean slate
105
        {
130 by Brian Aker
ulong cleanup
106
          uint16_t x= field_metadata[index++] << 8U; // real_type
1 by brian
clean slate
107
          x+= field_metadata[index++];            // pack or field length
108
          m_field_metadata[i]= x;
109
          break;
110
        }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
111
        case DRIZZLE_TYPE_VARCHAR:
1 by brian
clean slate
112
        {
113
          /*
114
            These types store two bytes.
115
          */
116
          char *ptr= (char *)&field_metadata[index];
117
          m_field_metadata[i]= uint2korr(ptr);
118
          index= index + 2;
119
          break;
120
        }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
121
        case DRIZZLE_TYPE_NEWDECIMAL:
1 by brian
clean slate
122
        {
130 by Brian Aker
ulong cleanup
123
          uint16_t x= field_metadata[index++] << 8U; // precision
1 by brian
clean slate
124
          x+= field_metadata[index++];            // decimals
125
          m_field_metadata[i]= x;
126
          break;
127
        }
128
        default:
129
          m_field_metadata[i]= 0;
130
          break;
131
        }
132
      }
133
    }
134
    if (m_size && null_bitmap)
135
       memcpy(m_null_bits, null_bitmap, (m_size + 7) / 8);
136
  }
137
138
  ~table_def() {
139
    my_free(m_memory, MYF(0));
140
    m_type= 0;
141
    m_size= 0;
142
  }
143
144
  /**
145
    Return the number of fields there is type data for.
146
147
    @return The number of fields that there is type data for.
148
   */
130 by Brian Aker
ulong cleanup
149
  uint32_t size() const { return m_size; }
1 by brian
clean slate
150
151
152
  /*
153
    Return a representation of the type data for one field.
154
155
    @param index Field index to return data for
156
157
    @return Will return a representation of the type data for field
158
    <code>index</code>. Currently, only the type identifier is
159
    returned.
160
   */
130 by Brian Aker
ulong cleanup
161
  field_type type(uint32_t index) const
1 by brian
clean slate
162
  {
51.1.44 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
163
    assert(index < m_size);
1 by brian
clean slate
164
    return m_type[index];
165
  }
166
167
168
  /*
169
    This function allows callers to get the extra field data from the
170
    table map for a given field. If there is no metadata for that field
171
    or there is no extra metadata at all, the function returns 0.
172
173
    The function returns the value for the field metadata for column at 
174
    position indicated by index. As mentioned, if the field was a type 
175
    that stores field metadata, that value is returned else zero (0) is 
176
    returned. This method is used in the unpack() methods of the 
177
    corresponding fields to properly extract the data from the binary log 
178
    in the event that the master's field is smaller than the slave.
179
  */
130 by Brian Aker
ulong cleanup
180
  uint16_t field_metadata(uint32_t index) const
1 by brian
clean slate
181
  {
51.1.44 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
182
    assert(index < m_size);
1 by brian
clean slate
183
    if (m_field_metadata_size)
184
      return m_field_metadata[index];
185
    else
186
      return 0;
187
  }
188
189
  /*
190
    This function returns whether the field on the master can be null.
191
    This value is derived from field->maybe_null().
192
  */
130 by Brian Aker
ulong cleanup
193
  my_bool maybe_null(uint32_t index) const
1 by brian
clean slate
194
  {
51.1.44 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
195
    assert(index < m_size);
1 by brian
clean slate
196
    return ((m_null_bits[(index / 8)] & 
197
            (1 << (index % 8))) == (1 << (index %8)));
198
  }
199
200
  /*
201
    This function returns the field size in raw bytes based on the type
202
    and the encoded field data from the master's raw data. This method can 
203
    be used for situations where the slave needs to skip a column (e.g., 
204
    WL#3915) or needs to advance the pointer for the fields in the raw 
205
    data from the master to a specific column.
206
  */
205 by Brian Aker
uint32 -> uin32_t
207
  uint32_t calc_field_size(uint32_t col, unsigned char *master_data) const;
1 by brian
clean slate
208
209
  /**
210
    Decide if the table definition is compatible with a table.
211
212
    Compare the definition with a table to see if it is compatible
213
    with it.
214
215
    A table definition is compatible with a table if:
216
      - the columns types of the table definition is a (not
217
        necessarily proper) prefix of the column type of the table, or
218
      - the other way around
219
220
    @param rli   Pointer to relay log info
221
    @param table Pointer to table to compare with.
222
223
    @retval 1  if the table definition is not compatible with @c table
224
    @retval 0  if the table definition is compatible with @c table
225
  */
226
  int compatible_with(Relay_log_info const *rli, TABLE *table) const;
227
228
private:
130 by Brian Aker
ulong cleanup
229
  uint32_t m_size;           // Number of elements in the types array
1 by brian
clean slate
230
  field_type *m_type;                     // Array of type descriptors
130 by Brian Aker
ulong cleanup
231
  uint32_t m_field_metadata_size;
232
  uint16_t *m_field_metadata;
233
  unsigned char *m_null_bits;
234
  unsigned char *m_memory;
1 by brian
clean slate
235
};
236
237
/**
238
   Extend the normal table list with a few new fields needed by the
239
   slave thread, but nowhere else.
240
 */
241
struct RPL_TABLE_LIST
242
  : public TABLE_LIST
243
{
244
  bool m_tabledef_valid;
245
  table_def m_tabledef;
246
};
247
248
249
/* Anonymous namespace for template functions/classes */
250
namespace {
251
252
  /*
253
    Smart pointer that will automatically call my_afree (a macro) when
254
    the pointer goes out of scope.  This is used so that I do not have
255
    to remember to call my_afree() before each return.  There is no
256
    overhead associated with this, since all functions are inline.
257
258
    I (Matz) would prefer to use the free function as a template
259
    parameter, but that is not possible when the "function" is a
260
    macro.
261
  */
262
  template <class Obj>
263
  class auto_afree_ptr
264
  {
265
    Obj* m_ptr;
266
  public:
267
    auto_afree_ptr(Obj* ptr) : m_ptr(ptr) { }
268
    ~auto_afree_ptr() { if (m_ptr) my_afree(m_ptr); }
269
    void assign(Obj* ptr) {
270
      /* Only to be called if it hasn't been given a value before. */
51.1.44 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
271
      assert(m_ptr == NULL);
1 by brian
clean slate
272
      m_ptr= ptr;
273
    }
274
    Obj* get() { return m_ptr; }
275
  };
276
277
}
278
279
#endif /* RPL_UTILITY_H */