~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/rpl_utility.h

  • Committer: Monty Taylor
  • Date: 2008-10-10 23:04:21 UTC
  • mto: (509.1.1 codestyle)
  • mto: This revision was merged to the branch mainline in revision 511.
  • Revision ID: monty@inaugust.com-20081010230421-zohe1eppxievpw8d
Removed O_NOFOLLOW

Show diffs side-by-side

added added

removed removed

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