rpl_utility.h 9.27 KB
Newer Older
1
/*
2
   Copyright (c) 2006, 2010, Oracle and/or its affiliates.
3 4 5

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
unknown's avatar
unknown committed
6
   the Free Software Foundation; version 2 of the License.
7 8 9 10 11 12 13 14

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
unknown's avatar
unknown committed
15
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
16 17 18 19 20 21 22 23

#ifndef RPL_UTILITY_H
#define RPL_UTILITY_H

#ifndef __cplusplus
#error "Don't include this C++ header file from a non-C++ file!"
#endif

24 25 26 27 28
#include "sql_priv.h"
#include "m_string.h"                           /* bzero, memcpy */
#ifdef MYSQL_SERVER
#include "table.h"                              /* TABLE_LIST */
#endif
29
#include "mysql_com.h"
30

31
class Relay_log_info;
32
class Log_event;
33

34
/**
35 36
  A table definition from the master.

37
  The responsibilities of this class is:
38
  - Extract and decode table definition data from the table map event
39 40 41 42 43 44 45
  - Check if table definition in table map is compatible with table
    definition on slave
 */

class table_def
{
public:
46
  /**
47 48
    Constructor.

49
    @param types Array of types, each stored as a byte
50
    @param size  Number of elements in array 'types'
51 52 53
    @param field_metadata Array of extra information about fields
    @param metadata_size Size of the field_metadata array
    @param null_bitmap The bitmap of fields that can be null
54
   */
55
  table_def(unsigned char *types, ulong size, uchar *field_metadata,
56
            int metadata_size, uchar *null_bitmap, uint16 flags);
57

58
  ~table_def();
59

60 61
  /**
    Return the number of fields there is type data for.
62

63
    @return The number of fields that there is type data for.
64
   */
65
  ulong size() const { return m_size; }
66

67

68 69 70 71 72 73 74 75
  /**
    Returns internal binlog type code for one field,
    without translation to real types.
  */
  enum_field_types binlog_type(ulong index) const
  {
    return static_cast<enum_field_types>(m_type[index]);
  }
76 77 78
  /*
    Return a representation of the type data for one field.

79
    @param index Field index to return data for
80

81 82 83
    @return Will return a representation of the type data for field
    <code>index</code>. Currently, only the type identifier is
    returned.
84
   */
85
  enum_field_types type(ulong index) const
86
  {
87
    DBUG_ASSERT(index < m_size);
88 89 90 91 92
    /*
      If the source type is MYSQL_TYPE_STRING, it can in reality be
      either MYSQL_TYPE_STRING, MYSQL_TYPE_ENUM, or MYSQL_TYPE_SET, so
      we might need to modify the type to get the real type.
    */
93
    enum_field_types source_type= binlog_type(index);
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    uint16 source_metadata= m_field_metadata[index];
    switch (source_type)
    {
    case MYSQL_TYPE_STRING:
    {
      int real_type= source_metadata >> 8;
      if (real_type == MYSQL_TYPE_ENUM || real_type == MYSQL_TYPE_SET)
        source_type= static_cast<enum_field_types>(real_type);
      break;
    }

    /*
      This type has not been used since before row-based replication,
      so we can safely assume that it really is MYSQL_TYPE_NEWDATE.
    */
    case MYSQL_TYPE_DATE:
      source_type= MYSQL_TYPE_NEWDATE;
      break;

    default:
      /* Do nothing */
      break;
    }

    return source_type;
119
  }
120

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

  /*
    This function allows callers to get the extra field data from the
    table map for a given field. If there is no metadata for that field
    or there is no extra metadata at all, the function returns 0.

    The function returns the value for the field metadata for column at 
    position indicated by index. As mentioned, if the field was a type 
    that stores field metadata, that value is returned else zero (0) is 
    returned. This method is used in the unpack() methods of the 
    corresponding fields to properly extract the data from the binary log 
    in the event that the master's field is smaller than the slave.
  */
  uint16 field_metadata(uint index) const
  {
    DBUG_ASSERT(index < m_size);
137
    if (m_field_metadata_size)
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
      return m_field_metadata[index];
    else
      return 0;
  }

  /*
    This function returns whether the field on the master can be null.
    This value is derived from field->maybe_null().
  */
  my_bool maybe_null(uint index) const
  {
    DBUG_ASSERT(index < m_size);
    return ((m_null_bits[(index / 8)] & 
            (1 << (index % 8))) == (1 << (index %8)));
  }

  /*
    This function returns the field size in raw bytes based on the type
    and the encoded field data from the master's raw data. This method can 
    be used for situations where the slave needs to skip a column (e.g., 
    WL#3915) or needs to advance the pointer for the fields in the raw 
    data from the master to a specific column.
  */
161
  uint32 calc_field_size(uint col, uchar *master_data) const;
162

163
  /**
164 165
    Decide if the table definition is compatible with a table.

166 167
    Compare the definition with a table to see if it is compatible
    with it.
168

169
    A table definition is compatible with a table if:
170 171
      - The columns types of the table definition is a (not
        necessarily proper) prefix of the column type of the table.
172

173 174 175 176 177 178 179
      - The other way around.

      - Each column on the master that also exists on the slave can be
        converted according to the current settings of @c
        SLAVE_TYPE_CONVERSIONS.

    @param thd
180 181 182
    @param rli   Pointer to relay log info
    @param table Pointer to table to compare with.

183 184 185
    @param[out] tmp_table_var Pointer to temporary table for holding
    conversion table.

186 187
    @retval 1  if the table definition is not compatible with @c table
    @retval 0  if the table definition is compatible with @c table
188
  */
189
#ifndef MYSQL_CLIENT
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
  bool compatible_with(THD *thd, Relay_log_info *rli, TABLE *table,
                      TABLE **conv_table_var) const;

  /**
   Create a virtual in-memory temporary table structure.

   The table structure has records and field array so that a row can
   be unpacked into the record for further processing.

   In the virtual table, each field that requires conversion will
   have a non-NULL value, while fields that do not require
   conversion will have a NULL value.

   Some information that is missing in the events, such as the
   character set for string types, are taken from the table that the
   field is going to be pushed into, so the target table that the data
   eventually need to be pushed into need to be supplied.

   @param thd Thread to allocate memory from.
   @param rli Relay log info structure, for error reporting.
   @param target_table Target table for fields.

   @return A pointer to a temporary table with memory allocated in the
   thread's memroot, NULL if the table could not be created
   */
  TABLE *create_conversion_table(THD *thd, Relay_log_info *rli, TABLE *target_table) const;
216
#endif
217

218

219
private:
220
  ulong m_size;           // Number of elements in the types array
221
  unsigned char *m_type;  // Array of type descriptors
222
  uint m_field_metadata_size;
223
  uint16 *m_field_metadata;
224
  uchar *m_null_bits;
225
  uint16 m_flags;         // Table flags
226
  uchar *m_memory;
227 228
};

229 230

#ifndef MYSQL_CLIENT
231 232 233 234 235
/**
   Extend the normal table list with a few new fields needed by the
   slave thread, but nowhere else.
 */
struct RPL_TABLE_LIST
236
  : public TABLE_LIST
237
{
238
  bool m_tabledef_valid;
239
  table_def m_tabledef;
240
  TABLE *m_conv_table;
241
  bool master_had_triggers;
242 243
};

244 245

/* Anonymous namespace for template functions/classes */
246
CPP_UNNAMED_NS_START
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272

  /*
    Smart pointer that will automatically call my_afree (a macro) when
    the pointer goes out of scope.  This is used so that I do not have
    to remember to call my_afree() before each return.  There is no
    overhead associated with this, since all functions are inline.

    I (Matz) would prefer to use the free function as a template
    parameter, but that is not possible when the "function" is a
    macro.
  */
  template <class Obj>
  class auto_afree_ptr
  {
    Obj* m_ptr;
  public:
    auto_afree_ptr(Obj* ptr) : m_ptr(ptr) { }
    ~auto_afree_ptr() { if (m_ptr) my_afree(m_ptr); }
    void assign(Obj* ptr) {
      /* Only to be called if it hasn't been given a value before. */
      DBUG_ASSERT(m_ptr == NULL);
      m_ptr= ptr;
    }
    Obj* get() { return m_ptr; }
  };

273
CPP_UNNAMED_NS_END
274 275 276 277 278 279 280 281 282 283

class Deferred_log_events
{
private:
  DYNAMIC_ARRAY array;
  Log_event *last_added;

public:
  Deferred_log_events(Relay_log_info *rli);
  ~Deferred_log_events();
284
  /* queue for exection at Query-log-event time prior the Query */
285 286
  int add(Log_event *ev);
  bool is_empty();
287
  bool execute(struct rpl_group_info *rgi);
288 289 290 291
  void rewind();
  bool is_last(Log_event *ev) { return ev == last_added; };
};

292
#endif
293

294
// NB. number of printed bit values is limited to sizeof(buf) - 1
295 296 297
#define DBUG_PRINT_BITSET(N,FRM,BS)                \
  do {                                             \
    char buf[256];                                 \
298
    uint i;                                        \
299
    for (i = 0 ; i < MY_MIN(sizeof(buf) - 1, (BS)->n_bits) ; i++) \
300
      buf[i] = bitmap_is_set((BS), i) ? '1' : '0'; \
301
    buf[i] = '\0';                                 \
302 303 304
    DBUG_PRINT((N), ((FRM), buf));                 \
  } while (0)

305
#endif /* RPL_UTILITY_H */