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