MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
rpl_utility.h
1 /* Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
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 "sql_priv.h"
24 #include "m_string.h"
25 #ifdef MYSQL_SERVER
26 #include "table.h" /* TABLE_LIST */
27 #endif
28 #include "mysql_com.h"
29 #include <hash.h>
30 
31 
32 class Relay_log_info;
33 class Log_event;
34 #ifndef MYSQL_CLIENT
35 
41 typedef struct hash_row_pos_st
42 {
48  const uchar *bi_start;
49  const uchar *bi_ends;
50 
51 } HASH_ROW_POS;
52 
53 
61 typedef struct hash_row_preamble_st
62 {
63  /*
64  The actual key.
65  */
66  my_hash_value_type hash_value;
67 
71  uint length;
72 
77  HASH_SEARCH_STATE search_state;
78 
83 
85 
86 typedef struct hash_row_entry_st
87 {
88  HASH_ROW_PREAMBLE *preamble;
89  HASH_ROW_POS *positions;
91 
93 {
94 public:
95 
103 
114  HASH_ROW_ENTRY* make_entry(const uchar *bi_start, const uchar *bi_ends);
115 
116 
128  bool put(TABLE* table, MY_BITMAP *cols, HASH_ROW_ENTRY* entry);
129 
143  HASH_ROW_ENTRY* get(TABLE *table, MY_BITMAP *cols);
144 
161  bool next(HASH_ROW_ENTRY** entry);
162 
172  bool del(HASH_ROW_ENTRY* entry);
173 
179  bool init(void);
180 
186  bool deinit(void);
187 
193  bool is_empty(void);
194 
200  int size();
201 
202 private:
203 
207  HASH m_hash;
208 
218  my_hash_value_type make_hash_key(TABLE *table, MY_BITMAP* cols);
219 };
220 
221 #endif
222 
233 {
234 public:
244  table_def(unsigned char *types, ulong size, uchar *field_metadata,
245  int metadata_size, uchar *null_bitmap, uint16 flags);
246 
247  ~table_def();
248 
254  ulong size() const { return m_size; }
255 
256 
257  /*
258  Returns internal binlog type code for one field,
259  without translation to real types.
260  */
261  enum_field_types binlog_type(ulong index) const
262  {
263  return static_cast<enum_field_types>(m_type[index]);
264  }
265  /*
266  Return a representation of the type data for one field.
267 
268  @param index Field index to return data for
269 
270  @return Will return a representation of the type data for field
271  <code>index</code>. Currently, only the type identifier is
272  returned.
273  */
274  enum_field_types type(ulong index) const
275  {
276  DBUG_ASSERT(index < m_size);
277  /*
278  If the source type is MYSQL_TYPE_STRING, it can in reality be
279  either MYSQL_TYPE_STRING, MYSQL_TYPE_ENUM, or MYSQL_TYPE_SET, so
280  we might need to modify the type to get the real type.
281  */
282  enum_field_types source_type= binlog_type(index);
283  uint16 source_metadata= m_field_metadata[index];
284  switch (source_type)
285  {
286  case MYSQL_TYPE_STRING:
287  {
288  int real_type= source_metadata >> 8;
289  if (real_type == MYSQL_TYPE_ENUM || real_type == MYSQL_TYPE_SET)
290  source_type= static_cast<enum_field_types>(real_type);
291  break;
292  }
293 
294  /*
295  This type has not been used since before row-based replication,
296  so we can safely assume that it really is MYSQL_TYPE_NEWDATE.
297  */
298  case MYSQL_TYPE_DATE:
299  source_type= MYSQL_TYPE_NEWDATE;
300  break;
301 
302  default:
303  /* Do nothing */
304  break;
305  }
306 
307  return source_type;
308  }
309 
310 
311  /*
312  This function allows callers to get the extra field data from the
313  table map for a given field. If there is no metadata for that field
314  or there is no extra metadata at all, the function returns 0.
315 
316  The function returns the value for the field metadata for column at
317  position indicated by index. As mentioned, if the field was a type
318  that stores field metadata, that value is returned else zero (0) is
319  returned. This method is used in the unpack() methods of the
320  corresponding fields to properly extract the data from the binary log
321  in the event that the master's field is smaller than the slave.
322  */
323  uint16 field_metadata(uint index) const
324  {
325  DBUG_ASSERT(index < m_size);
326  if (m_field_metadata_size)
327  return m_field_metadata[index];
328  else
329  return 0;
330  }
331 
332  /*
333  This function returns whether the field on the master can be null.
334  This value is derived from field->maybe_null().
335  */
336  my_bool maybe_null(uint index) const
337  {
338  DBUG_ASSERT(index < m_size);
339  return ((m_null_bits[(index / 8)] &
340  (1 << (index % 8))) == (1 << (index %8)));
341  }
342 
343  /*
344  This function returns the field size in raw bytes based on the type
345  and the encoded field data from the master's raw data. This method can
346  be used for situations where the slave needs to skip a column (e.g.,
347  WL#3915) or needs to advance the pointer for the fields in the raw
348  data from the master to a specific column.
349  */
350  uint32 calc_field_size(uint col, uchar *master_data) const;
351 
378 #ifndef MYSQL_CLIENT
379  bool compatible_with(THD *thd, Relay_log_info *rli, TABLE *table,
380  TABLE **conv_table_var) const;
381 
404  TABLE *create_conversion_table(THD *thd, Relay_log_info *rli, TABLE *target_table) const;
405 #endif
406 
407 
408 private:
409  ulong m_size; // Number of elements in the types array
410  unsigned char *m_type; // Array of type descriptors
411  uint m_field_metadata_size;
412  uint16 *m_field_metadata;
413  uchar *m_null_bits;
414  uint16 m_flags; // Table flags
415  uchar *m_memory;
416 };
417 
418 
419 #ifndef MYSQL_CLIENT
420 
425  : public TABLE_LIST
426 {
427  bool m_tabledef_valid;
428  table_def m_tabledef;
429  TABLE *m_conv_table;
430 };
431 
432 
433 /* Anonymous namespace for template functions/classes */
434 CPP_UNNAMED_NS_START
435 
436  /*
437  Smart pointer that will automatically call my_afree (a macro) when
438  the pointer goes out of scope. This is used so that I do not have
439  to remember to call my_afree() before each return. There is no
440  overhead associated with this, since all functions are inline.
441 
442  I (Matz) would prefer to use the free function as a template
443  parameter, but that is not possible when the "function" is a
444  macro.
445  */
446  template <class Obj>
448  {
449  Obj* m_ptr;
450  public:
451  auto_afree_ptr(Obj* ptr) : m_ptr(ptr) { }
452  ~auto_afree_ptr() { if (m_ptr) my_afree(m_ptr); }
453  void assign(Obj* ptr) {
454  /* Only to be called if it hasn't been given a value before. */
455  DBUG_ASSERT(m_ptr == NULL);
456  m_ptr= ptr;
457  }
458  Obj* get() { return m_ptr; }
459  };
460 
461 CPP_UNNAMED_NS_END
462 
464 {
465 private:
466  DYNAMIC_ARRAY array;
467 
468 public:
471  /* queue for exection at Query-log-event time prior the Query */
472  int add(Log_event *ev);
473  bool is_empty();
474  bool execute(Relay_log_info *rli);
475  void rewind();
476 };
477 
478 #endif
479 
480 // NB. number of printed bit values is limited to sizeof(buf) - 1
481 #define DBUG_PRINT_BITSET(N,FRM,BS) \
482  do { \
483  char buf[256]; \
484  uint i; \
485  for (i = 0 ; i < MY_MIN(sizeof(buf) - 1, (BS)->n_bits) ; i++) \
486  buf[i] = bitmap_is_set((BS), i) ? '1' : '0'; \
487  buf[i] = '\0'; \
488  DBUG_PRINT((N), ((FRM), buf)); \
489  } while (0)
490 
491 #endif /* RPL_UTILITY_H */
492