~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright 2007 MySQL AB. 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
243.1.17 by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.)
16
#include <drizzled/server_includes.h>
550 by Monty Taylor
Moved error.h into just the files that need it.
17
#include <drizzled/rpl_rli.h>
18
#include <drizzled/rpl_record.h>
19
#include <drizzled/rpl_utility.h>
20
#include <drizzled/rpl_rli.h>
21
#include <drizzled/error.h>
1 by brian
clean slate
22
23
/**
24
   Pack a record of data for a table into a format suitable for
25
   transfer via the binary log.
26
27
   The format for a row in transfer with N fields is the following:
28
29
   ceil(N/8) null bytes:
30
       One null bit for every column *regardless of whether it can be
31
       null or not*. This simplifies the decoding. Observe that the
32
       number of null bits is equal to the number of set bits in the
33
       @c cols bitmap. The number of null bytes is the smallest number
34
       of bytes necessary to store the null bits.
35
36
       Padding bits are 1.
37
38
   N packets:
39
       Each field is stored in packed format.
40
41
42
   @param table    Table describing the format of the record
43
44
   @param cols     Bitmap with a set bit for each column that should
45
                   be stored in the row
46
47
   @param row_data Pointer to memory where row will be written
48
49
   @param record   Pointer to record that should be packed. It is
50
                   assumed that the pointer refers to either @c
51
                   record[0] or @c record[1], but no such check is
52
                   made since the code does not rely on that.
53
54
   @return The number of bytes written at @c row_data.
55
 */
56
size_t
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
57
pack_row(Table *table, MY_BITMAP const* cols,
481 by Brian Aker
Remove all of uchar.
58
         unsigned char *row_data, const unsigned char *record)
1 by brian
clean slate
59
{
60
  Field **p_field= table->field, *field;
61
  int const null_byte_count= (bitmap_bits_set(cols) + 7) / 8;
481 by Brian Aker
Remove all of uchar.
62
  unsigned char *pack_ptr = row_data + null_byte_count;
63
  unsigned char *null_ptr = row_data;
1 by brian
clean slate
64
  my_ptrdiff_t const rec_offset= record - table->record[0];
65
  my_ptrdiff_t const def_offset= table->s->default_values - table->record[0];
66
67
68
  /*
69
    We write the null bits and the packed records using one pass
70
    through all the fields. The null bytes are written little-endian,
71
    i.e., the first fields are in the first byte.
72
   */
73
  unsigned int null_bits= (1U << 8) - 1;
74
  // Mask to mask out the correct but among the null bits
75
  unsigned int null_mask= 1U;
76
  for ( ; (field= *p_field) ; p_field++)
77
  {
78
    if (bitmap_is_set(cols, p_field - table->field))
79
    {
80
      my_ptrdiff_t offset;
81
      if (field->is_null(rec_offset))
82
      {
83
        offset= def_offset;
84
        null_bits |= null_mask;
85
      }
86
      else
87
      {
88
        offset= rec_offset;
89
        null_bits &= ~null_mask;
90
91
        /*
92
          We only store the data of the field if it is non-null
93
94
          For big-endian machines, we have to make sure that the
95
          length is stored in little-endian format, since this is the
96
          format used for the binlog.
97
        */
98
        pack_ptr= field->pack(pack_ptr, field->ptr + offset,
51.1.40 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
99
                              field->max_data_length(), true);
1 by brian
clean slate
100
      }
101
102
      null_mask <<= 1;
103
      if ((null_mask & 0xFF) == 0)
104
      {
51.1.40 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
105
        assert(null_ptr < row_data + null_byte_count);
1 by brian
clean slate
106
        null_mask = 1U;
107
        *null_ptr++ = null_bits;
108
        null_bits= (1U << 8) - 1;
109
      }
110
    }
51.1.2 by Jay Pipes
Fixed unbalanced parentheses in pack_row due to merge conflict resolution mistake
111
  }
1 by brian
clean slate
112
113
  /*
114
    Write the last (partial) byte, if there is one
115
  */
116
  if ((null_mask & 0xFF) > 1)
117
  {
51.1.40 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
118
    assert(null_ptr < row_data + null_byte_count);
1 by brian
clean slate
119
    *null_ptr++ = null_bits;
120
  }
121
122
  /*
123
    The null pointer should now point to the first byte of the
124
    packed data. If it doesn't, something is very wrong.
125
  */
51.1.40 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
126
  assert(null_ptr == row_data + null_byte_count);
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
127
  return(static_cast<size_t>(pack_ptr - row_data));
1 by brian
clean slate
128
}
129
130
131
/**
132
   Unpack a row into @c table->record[0].
133
134
   The function will always unpack into the @c table->record[0]
135
   record.  This is because there are too many dependencies on where
136
   the various member functions of Field and subclasses expect to
137
   write.
138
139
   The row is assumed to only consist of the fields for which the corresponding
140
   bit in bitset @c cols is set; the other parts of the record are left alone.
141
142
   At most @c colcnt columns are read: if the table is larger than
143
   that, the remaining fields are not filled in.
144
145
   @param rli     Relay log info
146
   @param table   Table to unpack into
147
   @param colcnt  Number of columns to read from record
148
   @param row_data
149
                  Packed row data
150
   @param cols    Pointer to bitset describing columns to fill in
151
   @param row_end Pointer to variable that will hold the value of the
152
                  one-after-end position for the row
153
   @param master_reclength
154
                  Pointer to variable that will be set to the length of the
155
                  record on the master side
156
157
   @retval 0 No error
158
159
   @retval ER_NO_DEFAULT_FOR_FIELD
160
   Returned if one of the fields existing on the slave but not on the
161
   master does not have a default value (and isn't nullable)
162
163
 */
164
int
165
unpack_row(Relay_log_info const *rli,
482 by Brian Aker
Remove uint.
166
           Table *table, uint32_t const colcnt,
481 by Brian Aker
Remove all of uchar.
167
           unsigned char const *const row_data, MY_BITMAP const *cols,
168
           unsigned char const **const row_end, ulong *const master_reclength)
1 by brian
clean slate
169
{
51.1.40 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
170
  assert(row_data);
1 by brian
clean slate
171
  size_t const master_null_byte_count= (bitmap_bits_set(cols) + 7) / 8;
172
  int error= 0;
173
481 by Brian Aker
Remove all of uchar.
174
  unsigned char const *null_ptr= row_data;
175
  unsigned char const *pack_ptr= row_data + master_null_byte_count;
1 by brian
clean slate
176
177
  Field **const begin_ptr = table->field;
178
  Field **field_ptr;
179
  Field **const end_ptr= begin_ptr + colcnt;
180
51.1.40 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
181
  assert(null_ptr < row_data + master_null_byte_count);
1 by brian
clean slate
182
183
  // Mask to mask out the correct bit among the null bits
184
  unsigned int null_mask= 1U;
185
  // The "current" null bits
186
  unsigned int null_bits= *null_ptr++;
482 by Brian Aker
Remove uint.
187
  uint32_t i= 0;
1 by brian
clean slate
188
189
  /*
190
    Use the rli class to get the table's metadata. If tabledef is not NULL
191
    we are processing data from a master. If tabledef is NULL then it is
192
    assumed that the packed row comes from the table to which it is
193
    unpacked.
194
  */
195
  table_def *tabledef= rli ? ((Relay_log_info*)rli)->get_tabledef(table) : 0;
196
  for (field_ptr= begin_ptr ; field_ptr < end_ptr && *field_ptr ; ++field_ptr)
197
  {
198
    Field *const f= *field_ptr;
199
200
201
    /*
202
      No need to bother about columns that does not exist: they have
203
      gotten default values when being emptied above.
204
     */
205
    if (bitmap_is_set(cols, field_ptr -  begin_ptr))
206
    {
207
      if ((null_mask & 0xFF) == 0)
208
      {
51.1.40 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
209
        assert(null_ptr < row_data + master_null_byte_count);
1 by brian
clean slate
210
        null_mask= 1U;
211
        null_bits= *null_ptr++;
212
      }
213
51.1.40 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
214
      assert(null_mask & 0xFF); // One of the 8 LSB should be set
1 by brian
clean slate
215
216
      /* Field...::unpack() cannot return 0 */
51.1.40 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
217
      assert(pack_ptr != NULL);
1 by brian
clean slate
218
219
      if ((null_bits & null_mask) && f->maybe_null())
220
      {
221
        f->set_null();
222
      }
223
      else
224
      {
225
        f->set_notnull();
226
227
        /*
228
          We only unpack the field if it was non-null.
229
          Use the master's size information if available else call
230
          normal unpack operation.
231
        */
232
        /*
233
          Use the master's metadata if we are processing data from a slave
234
          (tabledef not NULL). If tabledef is NULL then it is assumed that
235
          the packed row comes from the table to which it is unpacked.
236
        */
206 by Brian Aker
Removed final uint dead types.
237
        uint16_t metadata= tabledef ? tabledef->field_metadata(i) : 0;
51.1.40 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
238
        pack_ptr= f->unpack(f->ptr, pack_ptr, metadata, true);
1 by brian
clean slate
239
      }
240
241
      null_mask <<= 1;
242
    }
243
    i++;
244
  }
245
246
  /*
247
    throw away master's extra fields
248
249
    Use the master's max_cols if we are processing data from a slave
250
    (tabledef not NULL). If tabledef is NULL then it is assumed that
251
    there are no extra columns.
252
  */
482 by Brian Aker
Remove uint.
253
  uint32_t max_cols= tabledef ? cmin(tabledef->size(), cols->n_bits) : 0;
1 by brian
clean slate
254
  for (; i < max_cols; i++)
255
  {
256
    if (bitmap_is_set(cols, i))
257
    {
258
      if ((null_mask & 0xFF) == 0)
259
      {
51.1.40 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
260
        assert(null_ptr < row_data + master_null_byte_count);
1 by brian
clean slate
261
        null_mask= 1U;
262
        null_bits= *null_ptr++;
263
      }
51.1.40 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
264
      assert(null_mask & 0xFF); // One of the 8 LSB should be set
1 by brian
clean slate
265
266
      if (!((null_bits & null_mask) && tabledef->maybe_null(i)))
481 by Brian Aker
Remove all of uchar.
267
        pack_ptr+= tabledef->calc_field_size(i, (unsigned char *) pack_ptr);
1 by brian
clean slate
268
      null_mask <<= 1;
269
    }
270
  }
271
272
  /*
273
    We should now have read all the null bytes, otherwise something is
274
    really wrong.
275
   */
51.1.40 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
276
  assert(null_ptr == row_data + master_null_byte_count);
1 by brian
clean slate
277
278
  *row_end = pack_ptr;
279
  if (master_reclength)
280
  {
281
    if (*field_ptr)
282
      *master_reclength = (*field_ptr)->ptr - table->record[0];
283
    else
284
      *master_reclength = table->s->reclength;
285
  }
286
  
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
287
  return(error);
1 by brian
clean slate
288
}
289
290
/**
291
  Fills @c table->record[0] with default values.
292
293
  First @c empty_record() is called and then, additionally, fields are
294
  initialized explicitly with a call to @c set_default().
295
296
  For optimization reasons, the explicit initialization can be skipped
297
  for fields that are not marked in the @c cols vector. These fields
298
  will be set later, and filling them with default values is
299
  unnecessary.
300
301
  If @c check is true, fields are explicitly initialized only if they
302
  have default value or can be NULL. Otherwise error is reported. If
303
  @c check is false, no error is reported and the field is not set to
304
  any value.
305
306
  @todo When flag is added to allow engine to handle default values
307
  itself, the record should not be emptied and default values not set.
308
309
  @param table[in,out] Table whose record[0] buffer is prepared. 
310
  @param cols[in]      Vector of bits denoting columns that will be set
311
                       elsewhere
312
  @param check[in]     Indicates if errors should be checked when setting default
313
                       values.
314
315
  @retval 0                       Success
316
  @retval ER_NO_DEFAULT_FOR_FIELD Default value could not be set for a field
77.1.45 by Monty Taylor
Warning fixes.
317
*/
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
318
int prepare_record(Table *const table, const MY_BITMAP *cols,
319
                   uint32_t, const bool check)
1 by brian
clean slate
320
{
321
322
  int error= 0;
323
  empty_record(table);
324
325
  /*
326
    Explicit initialization of fields. For fields that are not in the
327
    cols for the row, we set them to default. If the fields are in
328
    addition to what exists on the master, we give an error if the
329
    have no sensible default.
330
  */
331
332
  for (Field **field_ptr= table->field ; *field_ptr ; ++field_ptr)
333
  {
334
    if ((uint) (field_ptr - table->field) >= cols->n_bits ||
335
        !bitmap_is_set(cols, field_ptr - table->field))
336
    {
205 by Brian Aker
uint32 -> uin32_t
337
      uint32_t const mask= NOT_NULL_FLAG | NO_DEFAULT_VALUE_FLAG;
1 by brian
clean slate
338
      Field *const f= *field_ptr;
339
340
      if (check && ((f->flags & mask) == mask))
341
      {
342
        my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), f->field_name);
343
        error = HA_ERR_ROWS_EVENT_APPLY;
344
      }
345
      else
346
      {
347
        f->set_default();
348
      }
349
    }
350
  }
351
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
352
  return(error);
1 by brian
clean slate
353
}