~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/replication/utility.h

  • Committer: Brian Aker
  • Date: 2009-01-23 02:15:04 UTC
  • mfrom: (798.2.32 drizzle)
  • Revision ID: brian@tangent.org-20090123021504-2j99e6hxab1ew601
Merge for replication removal.

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
 
#include <drizzled/table_list.h>
25
 
 
26
 
class Relay_log_info;
27
 
 
28
 
 
29
 
/**
30
 
  A table definition from the master.
31
 
 
32
 
  The responsibilities of this class is:
33
 
  - Extract and decode table definition data from the table map event
34
 
  - Check if table definition in table map is compatible with table
35
 
    definition on slave
36
 
 
37
 
  Currently, the only field type data available is an array of the
38
 
  type operators that are present in the table map event.
39
 
 
40
 
  @todo Add type operands to this structure to allow detection of
41
 
     difference between, e.g., BIT(5) and BIT(10).
42
 
 */
43
 
 
44
 
class table_def
45
 
{
46
 
public:
47
 
  /**
48
 
    Convenience declaration of the type of the field type data in a
49
 
    table map event.
50
 
  */
51
 
  typedef unsigned char field_type;
52
 
 
53
 
  /**
54
 
    Constructor.
55
 
 
56
 
    @param types Array of types
57
 
    @param size  Number of elements in array 'types'
58
 
    @param field_metadata Array of extra information about fields
59
 
    @param metadata_size Size of the field_metadata array
60
 
    @param null_bitmap The bitmap of fields that can be null
61
 
   */
62
 
  table_def(field_type *types, uint32_t size, unsigned char *field_metadata,
63
 
      int metadata_size, unsigned char *null_bitmap)
64
 
    : m_size(size), m_type(0), m_field_metadata_size(metadata_size),
65
 
      m_field_metadata(0), m_null_bits(0), m_memory(NULL)
66
 
  {
67
 
    m_memory= (unsigned char *)my_multi_malloc(MYF(MY_WME),
68
 
                                       &m_type, size,
69
 
                                       &m_field_metadata,
70
 
                                       size * sizeof(uint16_t),
71
 
                                       &m_null_bits, (size + 7) / 8,
72
 
                                       NULL);
73
 
 
74
 
    memset(m_field_metadata, 0, size * sizeof(uint16_t));
75
 
 
76
 
    if (m_type)
77
 
      memcpy(m_type, types, size);
78
 
    else
79
 
      m_size= 0;
80
 
    /*
81
 
      Extract the data from the table map into the field metadata array
82
 
      iff there is field metadata. The variable metadata_size will be
83
 
      0 if we are replicating from an older version server since no field
84
 
      metadata was written to the table map. This can also happen if
85
 
      there were no fields in the master that needed extra metadata.
86
 
    */
87
 
    if (m_size && metadata_size)
88
 
    {
89
 
      int index= 0;
90
 
      for (unsigned int i= 0; i < m_size; i++)
91
 
      {
92
 
        switch (m_type[i]) {
93
 
        case DRIZZLE_TYPE_BLOB:
94
 
        case DRIZZLE_TYPE_DOUBLE:
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 DRIZZLE_TYPE_ENUM:
104
 
        {
105
 
          uint16_t x= field_metadata[index++] << 8U; // real_type
106
 
          x+= field_metadata[index++];            // pack or field length
107
 
          m_field_metadata[i]= x;
108
 
          break;
109
 
        }
110
 
        case DRIZZLE_TYPE_VARCHAR:
111
 
        {
112
 
          /*
113
 
            These types store two bytes.
114
 
          */
115
 
          char *ptr= (char *)&field_metadata[index];
116
 
          m_field_metadata[i]= uint2korr(ptr);
117
 
          index= index + 2;
118
 
          break;
119
 
        }
120
 
        case DRIZZLE_TYPE_NEWDECIMAL:
121
 
        {
122
 
          uint16_t x= field_metadata[index++] << 8U; // precision
123
 
          x+= field_metadata[index++];            // decimals
124
 
          m_field_metadata[i]= x;
125
 
          break;
126
 
        }
127
 
        default:
128
 
          m_field_metadata[i]= 0;
129
 
          break;
130
 
        }
131
 
      }
132
 
    }
133
 
    if (m_size && null_bitmap)
134
 
       memcpy(m_null_bits, null_bitmap, (m_size + 7) / 8);
135
 
  }
136
 
 
137
 
  ~table_def() {
138
 
    free(m_memory);
139
 
    m_type= 0;
140
 
    m_size= 0;
141
 
  }
142
 
 
143
 
  /**
144
 
    Return the number of fields there is type data for.
145
 
 
146
 
    @return The number of fields that there is type data for.
147
 
   */
148
 
  uint32_t size() const { return m_size; }
149
 
 
150
 
 
151
 
  /*
152
 
    Return a representation of the type data for one field.
153
 
 
154
 
    @param index Field index to return data for
155
 
 
156
 
    @return Will return a representation of the type data for field
157
 
    <code>index</code>. Currently, only the type identifier is
158
 
    returned.
159
 
   */
160
 
  field_type type(uint32_t index) const
161
 
  {
162
 
    assert(index < m_size);
163
 
    return m_type[index];
164
 
  }
165
 
 
166
 
 
167
 
  /*
168
 
    This function allows callers to get the extra field data from the
169
 
    table map for a given field. If there is no metadata for that field
170
 
    or there is no extra metadata at all, the function returns 0.
171
 
 
172
 
    The function returns the value for the field metadata for column at
173
 
    position indicated by index. As mentioned, if the field was a type
174
 
    that stores field metadata, that value is returned else zero (0) is
175
 
    returned. This method is used in the unpack() methods of the
176
 
    corresponding fields to properly extract the data from the binary log
177
 
    in the event that the master's field is smaller than the slave.
178
 
  */
179
 
  uint16_t field_metadata(uint32_t index) const
180
 
  {
181
 
    assert(index < m_size);
182
 
    if (m_field_metadata_size)
183
 
      return m_field_metadata[index];
184
 
    else
185
 
      return 0;
186
 
  }
187
 
 
188
 
  /*
189
 
    This function returns whether the field on the master can be null.
190
 
    This value is derived from field->maybe_null().
191
 
  */
192
 
  bool maybe_null(uint32_t index) const
193
 
  {
194
 
    assert(index < m_size);
195
 
    return ((m_null_bits[(index / 8)] &
196
 
            (1 << (index % 8))) == (1 << (index %8)));
197
 
  }
198
 
 
199
 
  /*
200
 
    This function returns the field size in raw bytes based on the type
201
 
    and the encoded field data from the master's raw data. This method can
202
 
    be used for situations where the slave needs to skip a column (e.g.,
203
 
    WL#3915) or needs to advance the pointer for the fields in the raw
204
 
    data from the master to a specific column.
205
 
  */
206
 
  uint32_t calc_field_size(uint32_t col, unsigned char *master_data) const;
207
 
 
208
 
  /**
209
 
    Decide if the table definition is compatible with a table.
210
 
 
211
 
    Compare the definition with a table to see if it is compatible
212
 
    with it.
213
 
 
214
 
    A table definition is compatible with a table if:
215
 
      - the columns types of the table definition is a (not
216
 
        necessarily proper) prefix of the column type of the table, or
217
 
      - the other way around
218
 
 
219
 
    @param rli   Pointer to relay log info
220
 
    @param table Pointer to table to compare with.
221
 
 
222
 
    @retval 1  if the table definition is not compatible with @c table
223
 
    @retval 0  if the table definition is compatible with @c table
224
 
  */
225
 
  int compatible_with(Relay_log_info const *rli, Table *table) const;
226
 
 
227
 
private:
228
 
  uint32_t m_size;           // Number of elements in the types array
229
 
  field_type *m_type;                     // Array of type descriptors
230
 
  uint32_t m_field_metadata_size;
231
 
  uint16_t *m_field_metadata;
232
 
  unsigned char *m_null_bits;
233
 
  unsigned char *m_memory;
234
 
};
235
 
 
236
 
/**
237
 
   Extend the normal table list with a few new fields needed by the
238
 
   slave thread, but nowhere else.
239
 
 */
240
 
struct RPL_TableList
241
 
  : public TableList
242
 
{
243
 
  bool m_tabledef_valid;
244
 
  table_def m_tabledef;
245
 
};
246
 
 
247
 
#endif /* RPL_UTILITY_H */