~drizzle-trunk/drizzle/development

390.1.2 by Monty Taylor
Fixed copyright headers in drizzled/
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2008 Sun Microsystems
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; version 2 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
1 by brian
clean slate
19
20
#ifndef RPL_UTILITY_H
21
#define RPL_UTILITY_H
22
23
#ifndef __cplusplus
24
#error "Don't include this C++ header file from a non-C++ file!"
25
#endif
26
243.1.17 by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.)
27
#include <drizzled/server_includes.h>
1 by brian
clean slate
28
29
class Relay_log_info;
30
31
32
/**
33
  A table definition from the master.
34
35
  The responsibilities of this class is:
36
  - Extract and decode table definition data from the table map event
37
  - Check if table definition in table map is compatible with table
38
    definition on slave
39
40
  Currently, the only field type data available is an array of the
41
  type operators that are present in the table map event.
42
43
  @todo Add type operands to this structure to allow detection of
44
     difference between, e.g., BIT(5) and BIT(10).
45
 */
46
47
class table_def
48
{
49
public:
50
  /**
51
    Convenience declaration of the type of the field type data in a
52
    table map event.
53
  */
54
  typedef unsigned char field_type;
55
56
  /**
57
    Constructor.
58
59
    @param types Array of types
60
    @param size  Number of elements in array 'types'
61
    @param field_metadata Array of extra information about fields
62
    @param metadata_size Size of the field_metadata array
63
    @param null_bitmap The bitmap of fields that can be null
64
   */
130 by Brian Aker
ulong cleanup
65
  table_def(field_type *types, uint32_t size, unsigned char *field_metadata, 
66
      int metadata_size, unsigned char *null_bitmap)
1 by brian
clean slate
67
    : m_size(size), m_type(0), m_field_metadata_size(metadata_size),
68
      m_field_metadata(0), m_null_bits(0), m_memory(NULL)
69
  {
130 by Brian Aker
ulong cleanup
70
    m_memory= (unsigned char *)my_multi_malloc(MYF(MY_WME),
1 by brian
clean slate
71
                                       &m_type, size,
72
                                       &m_field_metadata,
130 by Brian Aker
ulong cleanup
73
                                       size * sizeof(uint16_t),
1 by brian
clean slate
74
                                       &m_null_bits, (size + 7) / 8,
75
                                       NULL);
76
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
77
    memset(m_field_metadata, 0, size * sizeof(uint16_t));
1 by brian
clean slate
78
79
    if (m_type)
80
      memcpy(m_type, types, size);
81
    else
82
      m_size= 0;
83
    /*
84
      Extract the data from the table map into the field metadata array
85
      iff there is field metadata. The variable metadata_size will be
86
      0 if we are replicating from an older version server since no field
87
      metadata was written to the table map. This can also happen if 
88
      there were no fields in the master that needed extra metadata.
89
    */
90
    if (m_size && metadata_size)
91
    { 
92
      int index= 0;
93
      for (unsigned int i= 0; i < m_size; i++)
94
      {
95
        switch (m_type[i]) {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
96
        case DRIZZLE_TYPE_BLOB:
97
        case DRIZZLE_TYPE_DOUBLE:
1 by brian
clean slate
98
        {
99
          /*
100
            These types store a single byte.
101
          */
102
          m_field_metadata[i]= field_metadata[index];
103
          index++;
104
          break;
105
        }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
106
        case DRIZZLE_TYPE_ENUM:
1 by brian
clean slate
107
        {
130 by Brian Aker
ulong cleanup
108
          uint16_t x= field_metadata[index++] << 8U; // real_type
1 by brian
clean slate
109
          x+= field_metadata[index++];            // pack or field length
110
          m_field_metadata[i]= x;
111
          break;
112
        }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
113
        case DRIZZLE_TYPE_VARCHAR:
1 by brian
clean slate
114
        {
115
          /*
116
            These types store two bytes.
117
          */
118
          char *ptr= (char *)&field_metadata[index];
119
          m_field_metadata[i]= uint2korr(ptr);
120
          index= index + 2;
121
          break;
122
        }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
123
        case DRIZZLE_TYPE_NEWDECIMAL:
1 by brian
clean slate
124
        {
130 by Brian Aker
ulong cleanup
125
          uint16_t x= field_metadata[index++] << 8U; // precision
1 by brian
clean slate
126
          x+= field_metadata[index++];            // decimals
127
          m_field_metadata[i]= x;
128
          break;
129
        }
130
        default:
131
          m_field_metadata[i]= 0;
132
          break;
133
        }
134
      }
135
    }
136
    if (m_size && null_bitmap)
137
       memcpy(m_null_bits, null_bitmap, (m_size + 7) / 8);
138
  }
139
140
  ~table_def() {
141
    my_free(m_memory, MYF(0));
142
    m_type= 0;
143
    m_size= 0;
144
  }
145
146
  /**
147
    Return the number of fields there is type data for.
148
149
    @return The number of fields that there is type data for.
150
   */
130 by Brian Aker
ulong cleanup
151
  uint32_t size() const { return m_size; }
1 by brian
clean slate
152
153
154
  /*
155
    Return a representation of the type data for one field.
156
157
    @param index Field index to return data for
158
159
    @return Will return a representation of the type data for field
160
    <code>index</code>. Currently, only the type identifier is
161
    returned.
162
   */
130 by Brian Aker
ulong cleanup
163
  field_type type(uint32_t index) const
1 by brian
clean slate
164
  {
51.1.44 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
165
    assert(index < m_size);
1 by brian
clean slate
166
    return m_type[index];
167
  }
168
169
170
  /*
171
    This function allows callers to get the extra field data from the
172
    table map for a given field. If there is no metadata for that field
173
    or there is no extra metadata at all, the function returns 0.
174
175
    The function returns the value for the field metadata for column at 
176
    position indicated by index. As mentioned, if the field was a type 
177
    that stores field metadata, that value is returned else zero (0) is 
178
    returned. This method is used in the unpack() methods of the 
179
    corresponding fields to properly extract the data from the binary log 
180
    in the event that the master's field is smaller than the slave.
181
  */
130 by Brian Aker
ulong cleanup
182
  uint16_t field_metadata(uint32_t index) const
1 by brian
clean slate
183
  {
51.1.44 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
184
    assert(index < m_size);
1 by brian
clean slate
185
    if (m_field_metadata_size)
186
      return m_field_metadata[index];
187
    else
188
      return 0;
189
  }
190
191
  /*
192
    This function returns whether the field on the master can be null.
193
    This value is derived from field->maybe_null().
194
  */
275 by Brian Aker
Full removal of my_bool from central server.
195
  bool maybe_null(uint32_t index) const
1 by brian
clean slate
196
  {
51.1.44 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
197
    assert(index < m_size);
1 by brian
clean slate
198
    return ((m_null_bits[(index / 8)] & 
199
            (1 << (index % 8))) == (1 << (index %8)));
200
  }
201
202
  /*
203
    This function returns the field size in raw bytes based on the type
204
    and the encoded field data from the master's raw data. This method can 
205
    be used for situations where the slave needs to skip a column (e.g., 
206
    WL#3915) or needs to advance the pointer for the fields in the raw 
207
    data from the master to a specific column.
208
  */
205 by Brian Aker
uint32 -> uin32_t
209
  uint32_t calc_field_size(uint32_t col, unsigned char *master_data) const;
1 by brian
clean slate
210
211
  /**
212
    Decide if the table definition is compatible with a table.
213
214
    Compare the definition with a table to see if it is compatible
215
    with it.
216
217
    A table definition is compatible with a table if:
218
      - the columns types of the table definition is a (not
219
        necessarily proper) prefix of the column type of the table, or
220
      - the other way around
221
222
    @param rli   Pointer to relay log info
223
    @param table Pointer to table to compare with.
224
225
    @retval 1  if the table definition is not compatible with @c table
226
    @retval 0  if the table definition is compatible with @c table
227
  */
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
228
  int compatible_with(Relay_log_info const *rli, Table *table) const;
1 by brian
clean slate
229
230
private:
130 by Brian Aker
ulong cleanup
231
  uint32_t m_size;           // Number of elements in the types array
1 by brian
clean slate
232
  field_type *m_type;                     // Array of type descriptors
130 by Brian Aker
ulong cleanup
233
  uint32_t m_field_metadata_size;
234
  uint16_t *m_field_metadata;
235
  unsigned char *m_null_bits;
236
  unsigned char *m_memory;
1 by brian
clean slate
237
};
238
239
/**
240
   Extend the normal table list with a few new fields needed by the
241
   slave thread, but nowhere else.
242
 */
327.2.4 by Brian Aker
Refactoring table.h
243
struct RPL_TableList
244
  : public TableList
1 by brian
clean slate
245
{
246
  bool m_tabledef_valid;
247
  table_def m_tabledef;
248
};
249
250
251
/* Anonymous namespace for template functions/classes */
252
namespace {
253
254
  /*
255
    Smart pointer that will automatically call my_afree (a macro) when
256
    the pointer goes out of scope.  This is used so that I do not have
257
    to remember to call my_afree() before each return.  There is no
258
    overhead associated with this, since all functions are inline.
259
260
    I (Matz) would prefer to use the free function as a template
261
    parameter, but that is not possible when the "function" is a
262
    macro.
263
  */
264
  template <class Obj>
265
  class auto_afree_ptr
266
  {
267
    Obj* m_ptr;
268
  public:
269
    auto_afree_ptr(Obj* ptr) : m_ptr(ptr) { }
270
    ~auto_afree_ptr() { if (m_ptr) my_afree(m_ptr); }
271
    void assign(Obj* ptr) {
272
      /* 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
273
      assert(m_ptr == NULL);
1 by brian
clean slate
274
      m_ptr= ptr;
275
    }
276
    Obj* get() { return m_ptr; }
277
  };
278
279
}
280
281
#endif /* RPL_UTILITY_H */