~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/innobase/row/row0upd.c

  • Committer: Stewart Smith
  • Date: 2010-03-02 06:41:59 UTC
  • mto: (1309.2.13 build)
  • mto: This revision was merged to the branch mainline in revision 1318.
  • Revision ID: stewart@flamingspork.com-20100302064159-gktw6hcbs3u0fflm
move Item_result out to its own header file and out of common.h

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*****************************************************************************
2
 
 
3
 
Copyright (c) 1996, 2010, Innobase Oy. All Rights Reserved.
4
 
 
5
 
This program is free software; you can redistribute it and/or modify it under
6
 
the terms of the GNU General Public License as published by the Free Software
7
 
Foundation; version 2 of the License.
8
 
 
9
 
This program is distributed in the hope that it will be useful, but WITHOUT
10
 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
 
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
 
 
13
 
You should have received a copy of the GNU General Public License along with
14
 
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
15
 
St, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
*****************************************************************************/
18
 
 
19
 
/**************************************************//**
20
 
@file row/row0upd.c
21
 
Update of a row
22
 
 
23
 
Created 12/27/1996 Heikki Tuuri
24
 
*******************************************************/
25
 
 
26
 
#include "row0upd.h"
27
 
 
28
 
#ifdef UNIV_NONINL
29
 
#include "row0upd.ic"
30
 
#endif
31
 
 
32
 
#include "dict0dict.h"
33
 
#include "trx0undo.h"
34
 
#include "rem0rec.h"
35
 
#ifndef UNIV_HOTBACKUP
36
 
#include "dict0boot.h"
37
 
#include "dict0crea.h"
38
 
#include "mach0data.h"
39
 
#include "btr0btr.h"
40
 
#include "btr0cur.h"
41
 
#include "que0que.h"
42
 
#include "row0ext.h"
43
 
#include "row0ins.h"
44
 
#include "row0sel.h"
45
 
#include "row0row.h"
46
 
#include "rem0cmp.h"
47
 
#include "lock0lock.h"
48
 
#include "log0log.h"
49
 
#include "pars0sym.h"
50
 
#include "eval0eval.h"
51
 
#include "buf0lru.h"
52
 
 
53
 
 
54
 
/* What kind of latch and lock can we assume when the control comes to
55
 
   -------------------------------------------------------------------
56
 
an update node?
57
 
--------------
58
 
Efficiency of massive updates would require keeping an x-latch on a
59
 
clustered index page through many updates, and not setting an explicit
60
 
x-lock on clustered index records, as they anyway will get an implicit
61
 
x-lock when they are updated. A problem is that the read nodes in the
62
 
graph should know that they must keep the latch when passing the control
63
 
up to the update node, and not set any record lock on the record which
64
 
will be updated. Another problem occurs if the execution is stopped,
65
 
as the kernel switches to another query thread, or the transaction must
66
 
wait for a lock. Then we should be able to release the latch and, maybe,
67
 
acquire an explicit x-lock on the record.
68
 
        Because this seems too complicated, we conclude that the less
69
 
efficient solution of releasing all the latches when the control is
70
 
transferred to another node, and acquiring explicit x-locks, is better. */
71
 
 
72
 
/* How is a delete performed? If there is a delete without an
73
 
explicit cursor, i.e., a searched delete, there are at least
74
 
two different situations:
75
 
the implicit select cursor may run on (1) the clustered index or
76
 
on (2) a secondary index. The delete is performed by setting
77
 
the delete bit in the record and substituting the id of the
78
 
deleting transaction for the original trx id, and substituting a
79
 
new roll ptr for previous roll ptr. The old trx id and roll ptr
80
 
are saved in the undo log record. Thus, no physical changes occur
81
 
in the index tree structure at the time of the delete. Only
82
 
when the undo log is purged, the index records will be physically
83
 
deleted from the index trees.
84
 
 
85
 
The query graph executing a searched delete would consist of
86
 
a delete node which has as a subtree a select subgraph.
87
 
The select subgraph should return a (persistent) cursor
88
 
in the clustered index, placed on page which is x-latched.
89
 
The delete node should look for all secondary index records for
90
 
this clustered index entry and mark them as deleted. When is
91
 
the x-latch freed? The most efficient way for performing a
92
 
searched delete is obviously to keep the x-latch for several
93
 
steps of query graph execution. */
94
 
 
95
 
/*************************************************************************
96
 
IMPORTANT NOTE: Any operation that generates redo MUST check that there
97
 
is enough space in the redo log before for that operation. This is
98
 
done by calling log_free_check(). The reason for checking the
99
 
availability of the redo log space before the start of the operation is
100
 
that we MUST not hold any synchonization objects when performing the
101
 
check.
102
 
If you make a change in this module make sure that no codepath is
103
 
introduced where a call to log_free_check() is bypassed. */
104
 
 
105
 
/*************************************************************************
106
 
IMPORTANT NOTE: Any operation that generates redo MUST check that there
107
 
is enough space in the redo log before for that operation. This is
108
 
done by calling log_free_check(). The reason for checking the
109
 
availability of the redo log space before the start of the operation is
110
 
that we MUST not hold any synchonization objects when performing the
111
 
check.
112
 
If you make a change in this module make sure that no codepath is
113
 
introduced where a call to log_free_check() is bypassed. */
114
 
 
115
 
/***********************************************************//**
116
 
Checks if an update vector changes some of the first ordering fields of an
117
 
index record. This is only used in foreign key checks and we can assume
118
 
that index does not contain column prefixes.
119
 
@return TRUE if changes */
120
 
static
121
 
ibool
122
 
row_upd_changes_first_fields_binary(
123
 
/*================================*/
124
 
        dtuple_t*       entry,  /*!< in: old value of index entry */
125
 
        dict_index_t*   index,  /*!< in: index of entry */
126
 
        const upd_t*    update, /*!< in: update vector for the row */
127
 
        ulint           n);     /*!< in: how many first fields to check */
128
 
 
129
 
 
130
 
/*********************************************************************//**
131
 
Checks if index currently is mentioned as a referenced index in a foreign
132
 
key constraint.
133
 
 
134
 
NOTE that since we do not hold dict_operation_lock when leaving the
135
 
function, it may be that the referencing table has been dropped when
136
 
we leave this function: this function is only for heuristic use!
137
 
 
138
 
@return TRUE if referenced */
139
 
static
140
 
ibool
141
 
row_upd_index_is_referenced(
142
 
/*========================*/
143
 
        dict_index_t*   index,  /*!< in: index */
144
 
        trx_t*          trx)    /*!< in: transaction */
145
 
{
146
 
        dict_table_t*   table           = index->table;
147
 
        dict_foreign_t* foreign;
148
 
        ibool           froze_data_dict = FALSE;
149
 
        ibool           is_referenced   = FALSE;
150
 
 
151
 
        if (!UT_LIST_GET_FIRST(table->referenced_list)) {
152
 
 
153
 
                return(FALSE);
154
 
        }
155
 
 
156
 
        if (trx->dict_operation_lock_mode == 0) {
157
 
                row_mysql_freeze_data_dictionary(trx);
158
 
                froze_data_dict = TRUE;
159
 
        }
160
 
 
161
 
        foreign = UT_LIST_GET_FIRST(table->referenced_list);
162
 
 
163
 
        while (foreign) {
164
 
                if (foreign->referenced_index == index) {
165
 
 
166
 
                        is_referenced = TRUE;
167
 
                        goto func_exit;
168
 
                }
169
 
 
170
 
                foreign = UT_LIST_GET_NEXT(referenced_list, foreign);
171
 
        }
172
 
 
173
 
func_exit:
174
 
        if (froze_data_dict) {
175
 
                row_mysql_unfreeze_data_dictionary(trx);
176
 
        }
177
 
 
178
 
        return(is_referenced);
179
 
}
180
 
 
181
 
/*********************************************************************//**
182
 
Checks if possible foreign key constraints hold after a delete of the record
183
 
under pcur.
184
 
 
185
 
NOTE that this function will temporarily commit mtr and lose the
186
 
pcur position!
187
 
 
188
 
@return DB_SUCCESS or an error code */
189
 
static
190
 
ulint
191
 
row_upd_check_references_constraints(
192
 
/*=================================*/
193
 
        upd_node_t*     node,   /*!< in: row update node */
194
 
        btr_pcur_t*     pcur,   /*!< in: cursor positioned on a record; NOTE: the
195
 
                                cursor position is lost in this function! */
196
 
        dict_table_t*   table,  /*!< in: table in question */
197
 
        dict_index_t*   index,  /*!< in: index of the cursor */
198
 
        ulint*          offsets,/*!< in/out: rec_get_offsets(pcur.rec, index) */
199
 
        que_thr_t*      thr,    /*!< in: query thread */
200
 
        mtr_t*          mtr)    /*!< in: mtr */
201
 
{
202
 
        dict_foreign_t* foreign;
203
 
        mem_heap_t*     heap;
204
 
        dtuple_t*       entry;
205
 
        trx_t*          trx;
206
 
        const rec_t*    rec;
207
 
        ulint           n_ext;
208
 
        ulint           err;
209
 
        ibool           got_s_lock      = FALSE;
210
 
 
211
 
        if (UT_LIST_GET_FIRST(table->referenced_list) == NULL) {
212
 
 
213
 
                return(DB_SUCCESS);
214
 
        }
215
 
 
216
 
        trx = thr_get_trx(thr);
217
 
 
218
 
        rec = btr_pcur_get_rec(pcur);
219
 
        ut_ad(rec_offs_validate(rec, index, offsets));
220
 
 
221
 
        heap = mem_heap_create(500);
222
 
 
223
 
        entry = row_rec_to_index_entry(ROW_COPY_DATA, rec, index, offsets,
224
 
                                       &n_ext, heap);
225
 
 
226
 
        mtr_commit(mtr);
227
 
 
228
 
        mtr_start(mtr);
229
 
 
230
 
        if (trx->dict_operation_lock_mode == 0) {
231
 
                got_s_lock = TRUE;
232
 
 
233
 
                row_mysql_freeze_data_dictionary(trx);
234
 
        }
235
 
 
236
 
        foreign = UT_LIST_GET_FIRST(table->referenced_list);
237
 
 
238
 
        while (foreign) {
239
 
                /* Note that we may have an update which updates the index
240
 
                record, but does NOT update the first fields which are
241
 
                referenced in a foreign key constraint. Then the update does
242
 
                NOT break the constraint. */
243
 
 
244
 
                if (foreign->referenced_index == index
245
 
                    && (node->is_delete
246
 
                        || row_upd_changes_first_fields_binary(
247
 
                                entry, index, node->update,
248
 
                                foreign->n_fields))) {
249
 
 
250
 
                        if (foreign->foreign_table == NULL) {
251
 
                                dict_table_get(foreign->foreign_table_name,
252
 
                                               FALSE);
253
 
                        }
254
 
 
255
 
                        if (foreign->foreign_table) {
256
 
                                mutex_enter(&(dict_sys->mutex));
257
 
 
258
 
                                (foreign->foreign_table
259
 
                                 ->n_foreign_key_checks_running)++;
260
 
 
261
 
                                mutex_exit(&(dict_sys->mutex));
262
 
                        }
263
 
 
264
 
                        /* NOTE that if the thread ends up waiting for a lock
265
 
                        we will release dict_operation_lock temporarily!
266
 
                        But the counter on the table protects 'foreign' from
267
 
                        being dropped while the check is running. */
268
 
 
269
 
                        err = row_ins_check_foreign_constraint(
270
 
                                FALSE, foreign, table, entry, thr);
271
 
 
272
 
                        if (foreign->foreign_table) {
273
 
                                mutex_enter(&(dict_sys->mutex));
274
 
 
275
 
                                ut_a(foreign->foreign_table
276
 
                                     ->n_foreign_key_checks_running > 0);
277
 
 
278
 
                                (foreign->foreign_table
279
 
                                 ->n_foreign_key_checks_running)--;
280
 
 
281
 
                                mutex_exit(&(dict_sys->mutex));
282
 
                        }
283
 
 
284
 
                        if (err != DB_SUCCESS) {
285
 
 
286
 
                                goto func_exit;
287
 
                        }
288
 
                }
289
 
 
290
 
                foreign = UT_LIST_GET_NEXT(referenced_list, foreign);
291
 
        }
292
 
 
293
 
        err = DB_SUCCESS;
294
 
 
295
 
func_exit:
296
 
        if (got_s_lock) {
297
 
                row_mysql_unfreeze_data_dictionary(trx);
298
 
        }
299
 
 
300
 
        mem_heap_free(heap);
301
 
 
302
 
        return(err);
303
 
}
304
 
 
305
 
/*********************************************************************//**
306
 
Creates an update node for a query graph.
307
 
@return own: update node */
308
 
UNIV_INTERN
309
 
upd_node_t*
310
 
upd_node_create(
311
 
/*============*/
312
 
        mem_heap_t*     heap)   /*!< in: mem heap where created */
313
 
{
314
 
        upd_node_t*     node;
315
 
 
316
 
        node = mem_heap_alloc(heap, sizeof(upd_node_t));
317
 
        node->common.type = QUE_NODE_UPDATE;
318
 
 
319
 
        node->state = UPD_NODE_UPDATE_CLUSTERED;
320
 
        node->in_mysql_interface = FALSE;
321
 
 
322
 
        node->row = NULL;
323
 
        node->ext = NULL;
324
 
        node->upd_row = NULL;
325
 
        node->upd_ext = NULL;
326
 
        node->index = NULL;
327
 
        node->update = NULL;
328
 
 
329
 
        node->foreign = NULL;
330
 
        node->cascade_heap = NULL;
331
 
        node->cascade_node = NULL;
332
 
 
333
 
        node->select = NULL;
334
 
 
335
 
        node->heap = mem_heap_create(128);
336
 
        node->magic_n = UPD_NODE_MAGIC_N;
337
 
 
338
 
        node->cmpl_info = 0;
339
 
 
340
 
        return(node);
341
 
}
342
 
#endif /* !UNIV_HOTBACKUP */
343
 
 
344
 
/*********************************************************************//**
345
 
Updates the trx id and roll ptr field in a clustered index record in database
346
 
recovery. */
347
 
UNIV_INTERN
348
 
void
349
 
row_upd_rec_sys_fields_in_recovery(
350
 
/*===============================*/
351
 
        rec_t*          rec,    /*!< in/out: record */
352
 
        page_zip_des_t* page_zip,/*!< in/out: compressed page, or NULL */
353
 
        const ulint*    offsets,/*!< in: array returned by rec_get_offsets() */
354
 
        ulint           pos,    /*!< in: TRX_ID position in rec */
355
 
        trx_id_t        trx_id, /*!< in: transaction id */
356
 
        roll_ptr_t      roll_ptr)/*!< in: roll ptr of the undo log record */
357
 
{
358
 
        ut_ad(rec_offs_validate(rec, NULL, offsets));
359
 
 
360
 
        if (UNIV_LIKELY_NULL(page_zip)) {
361
 
                page_zip_write_trx_id_and_roll_ptr(
362
 
                        page_zip, rec, offsets, pos, trx_id, roll_ptr);
363
 
        } else {
364
 
                byte*   field;
365
 
                ulint   len;
366
 
 
367
 
                field = rec_get_nth_field(rec, offsets, pos, &len);
368
 
                ut_ad(len == DATA_TRX_ID_LEN);
369
 
#if DATA_TRX_ID + 1 != DATA_ROLL_PTR
370
 
# error "DATA_TRX_ID + 1 != DATA_ROLL_PTR"
371
 
#endif
372
 
                trx_write_trx_id(field, trx_id);
373
 
                trx_write_roll_ptr(field + DATA_TRX_ID_LEN, roll_ptr);
374
 
        }
375
 
}
376
 
 
377
 
#ifndef UNIV_HOTBACKUP
378
 
/*********************************************************************//**
379
 
Sets the trx id or roll ptr field of a clustered index entry. */
380
 
UNIV_INTERN
381
 
void
382
 
row_upd_index_entry_sys_field(
383
 
/*==========================*/
384
 
        const dtuple_t* entry,  /*!< in: index entry, where the memory buffers
385
 
                                for sys fields are already allocated:
386
 
                                the function just copies the new values to
387
 
                                them */
388
 
        dict_index_t*   index,  /*!< in: clustered index */
389
 
        ulint           type,   /*!< in: DATA_TRX_ID or DATA_ROLL_PTR */
390
 
        ib_uint64_t     val)    /*!< in: value to write */
391
 
{
392
 
        dfield_t*       dfield;
393
 
        byte*           field;
394
 
        ulint           pos;
395
 
 
396
 
        ut_ad(dict_index_is_clust(index));
397
 
 
398
 
        pos = dict_index_get_sys_col_pos(index, type);
399
 
 
400
 
        dfield = dtuple_get_nth_field(entry, pos);
401
 
        field = dfield_get_data(dfield);
402
 
 
403
 
        if (type == DATA_TRX_ID) {
404
 
                trx_write_trx_id(field, val);
405
 
        } else {
406
 
                ut_ad(type == DATA_ROLL_PTR);
407
 
                trx_write_roll_ptr(field, val);
408
 
        }
409
 
}
410
 
 
411
 
/***********************************************************//**
412
 
Returns TRUE if row update changes size of some field in index or if some
413
 
field to be updated is stored externally in rec or update.
414
 
@return TRUE if the update changes the size of some field in index or
415
 
the field is external in rec or update */
416
 
UNIV_INTERN
417
 
ibool
418
 
row_upd_changes_field_size_or_external(
419
 
/*===================================*/
420
 
        dict_index_t*   index,  /*!< in: index */
421
 
        const ulint*    offsets,/*!< in: rec_get_offsets(rec, index) */
422
 
        const upd_t*    update) /*!< in: update vector */
423
 
{
424
 
        const upd_field_t*      upd_field;
425
 
        const dfield_t*         new_val;
426
 
        ulint                   old_len;
427
 
        ulint                   new_len;
428
 
        ulint                   n_fields;
429
 
        ulint                   i;
430
 
 
431
 
        ut_ad(rec_offs_validate(NULL, index, offsets));
432
 
        n_fields = upd_get_n_fields(update);
433
 
 
434
 
        for (i = 0; i < n_fields; i++) {
435
 
                upd_field = upd_get_nth_field(update, i);
436
 
 
437
 
                new_val = &(upd_field->new_val);
438
 
                new_len = dfield_get_len(new_val);
439
 
 
440
 
                if (dfield_is_null(new_val) && !rec_offs_comp(offsets)) {
441
 
                        /* A bug fixed on Dec 31st, 2004: we looked at the
442
 
                        SQL NULL size from the wrong field! We may backport
443
 
                        this fix also to 4.0. The merge to 5.0 will be made
444
 
                        manually immediately after we commit this to 4.1. */
445
 
 
446
 
                        new_len = dict_col_get_sql_null_size(
447
 
                                dict_index_get_nth_col(index,
448
 
                                                       upd_field->field_no),
449
 
                                0);
450
 
                }
451
 
 
452
 
                old_len = rec_offs_nth_size(offsets, upd_field->field_no);
453
 
 
454
 
                if (rec_offs_comp(offsets)
455
 
                    && rec_offs_nth_sql_null(offsets,
456
 
                                             upd_field->field_no)) {
457
 
                        /* Note that in the compact table format, for a
458
 
                        variable length field, an SQL NULL will use zero
459
 
                        bytes in the offset array at the start of the physical
460
 
                        record, but a zero-length value (empty string) will
461
 
                        use one byte! Thus, we cannot use update-in-place
462
 
                        if we update an SQL NULL varchar to an empty string! */
463
 
 
464
 
                        old_len = UNIV_SQL_NULL;
465
 
                }
466
 
 
467
 
                if (dfield_is_ext(new_val) || old_len != new_len
468
 
                    || rec_offs_nth_extern(offsets, upd_field->field_no)) {
469
 
 
470
 
                        return(TRUE);
471
 
                }
472
 
        }
473
 
 
474
 
        return(FALSE);
475
 
}
476
 
#endif /* !UNIV_HOTBACKUP */
477
 
 
478
 
/***********************************************************//**
479
 
Replaces the new column values stored in the update vector to the record
480
 
given. No field size changes are allowed. */
481
 
UNIV_INTERN
482
 
void
483
 
row_upd_rec_in_place(
484
 
/*=================*/
485
 
        rec_t*          rec,    /*!< in/out: record where replaced */
486
 
        dict_index_t*   index,  /*!< in: the index the record belongs to */
487
 
        const ulint*    offsets,/*!< in: array returned by rec_get_offsets() */
488
 
        const upd_t*    update, /*!< in: update vector */
489
 
        page_zip_des_t* page_zip)/*!< in: compressed page with enough space
490
 
                                available, or NULL */
491
 
{
492
 
        const upd_field_t*      upd_field;
493
 
        const dfield_t*         new_val;
494
 
        ulint                   n_fields;
495
 
        ulint                   i;
496
 
 
497
 
        ut_ad(rec_offs_validate(rec, index, offsets));
498
 
 
499
 
        if (rec_offs_comp(offsets)) {
500
 
                rec_set_info_bits_new(rec, update->info_bits);
501
 
        } else {
502
 
                rec_set_info_bits_old(rec, update->info_bits);
503
 
        }
504
 
 
505
 
        n_fields = upd_get_n_fields(update);
506
 
 
507
 
        for (i = 0; i < n_fields; i++) {
508
 
                upd_field = upd_get_nth_field(update, i);
509
 
                new_val = &(upd_field->new_val);
510
 
                ut_ad(!dfield_is_ext(new_val) ==
511
 
                      !rec_offs_nth_extern(offsets, upd_field->field_no));
512
 
 
513
 
                rec_set_nth_field(rec, offsets, upd_field->field_no,
514
 
                                  dfield_get_data(new_val),
515
 
                                  dfield_get_len(new_val));
516
 
        }
517
 
 
518
 
        if (UNIV_LIKELY_NULL(page_zip)) {
519
 
                page_zip_write_rec(page_zip, rec, index, offsets, 0);
520
 
        }
521
 
}
522
 
 
523
 
#ifndef UNIV_HOTBACKUP
524
 
/*********************************************************************//**
525
 
Writes into the redo log the values of trx id and roll ptr and enough info
526
 
to determine their positions within a clustered index record.
527
 
@return new pointer to mlog */
528
 
UNIV_INTERN
529
 
byte*
530
 
row_upd_write_sys_vals_to_log(
531
 
/*==========================*/
532
 
        dict_index_t*   index,  /*!< in: clustered index */
533
 
        trx_t*          trx,    /*!< in: transaction */
534
 
        roll_ptr_t      roll_ptr,/*!< in: roll ptr of the undo log record */
535
 
        byte*           log_ptr,/*!< pointer to a buffer of size > 20 opened
536
 
                                in mlog */
537
 
        mtr_t*          mtr __attribute__((unused))) /*!< in: mtr */
538
 
{
539
 
        ut_ad(dict_index_is_clust(index));
540
 
        ut_ad(mtr);
541
 
 
542
 
        log_ptr += mach_write_compressed(log_ptr,
543
 
                                         dict_index_get_sys_col_pos(
544
 
                                                 index, DATA_TRX_ID));
545
 
 
546
 
        trx_write_roll_ptr(log_ptr, roll_ptr);
547
 
        log_ptr += DATA_ROLL_PTR_LEN;
548
 
 
549
 
        log_ptr += mach_ull_write_compressed(log_ptr, trx->id);
550
 
 
551
 
        return(log_ptr);
552
 
}
553
 
#endif /* !UNIV_HOTBACKUP */
554
 
 
555
 
/*********************************************************************//**
556
 
Parses the log data of system field values.
557
 
@return log data end or NULL */
558
 
UNIV_INTERN
559
 
byte*
560
 
row_upd_parse_sys_vals(
561
 
/*===================*/
562
 
        byte*           ptr,    /*!< in: buffer */
563
 
        byte*           end_ptr,/*!< in: buffer end */
564
 
        ulint*          pos,    /*!< out: TRX_ID position in record */
565
 
        trx_id_t*       trx_id, /*!< out: trx id */
566
 
        roll_ptr_t*     roll_ptr)/*!< out: roll ptr */
567
 
{
568
 
        ptr = mach_parse_compressed(ptr, end_ptr, pos);
569
 
 
570
 
        if (ptr == NULL) {
571
 
 
572
 
                return(NULL);
573
 
        }
574
 
 
575
 
        if (end_ptr < ptr + DATA_ROLL_PTR_LEN) {
576
 
 
577
 
                return(NULL);
578
 
        }
579
 
 
580
 
        *roll_ptr = trx_read_roll_ptr(ptr);
581
 
        ptr += DATA_ROLL_PTR_LEN;
582
 
 
583
 
        ptr = mach_ull_parse_compressed(ptr, end_ptr, trx_id);
584
 
 
585
 
        return(ptr);
586
 
}
587
 
 
588
 
#ifndef UNIV_HOTBACKUP
589
 
/***********************************************************//**
590
 
Writes to the redo log the new values of the fields occurring in the index. */
591
 
UNIV_INTERN
592
 
void
593
 
row_upd_index_write_log(
594
 
/*====================*/
595
 
        const upd_t*    update, /*!< in: update vector */
596
 
        byte*           log_ptr,/*!< in: pointer to mlog buffer: must
597
 
                                contain at least MLOG_BUF_MARGIN bytes
598
 
                                of free space; the buffer is closed
599
 
                                within this function */
600
 
        mtr_t*          mtr)    /*!< in: mtr into whose log to write */
601
 
{
602
 
        const upd_field_t*      upd_field;
603
 
        const dfield_t*         new_val;
604
 
        ulint                   len;
605
 
        ulint                   n_fields;
606
 
        byte*                   buf_end;
607
 
        ulint                   i;
608
 
 
609
 
        n_fields = upd_get_n_fields(update);
610
 
 
611
 
        buf_end = log_ptr + MLOG_BUF_MARGIN;
612
 
 
613
 
        mach_write_to_1(log_ptr, update->info_bits);
614
 
        log_ptr++;
615
 
        log_ptr += mach_write_compressed(log_ptr, n_fields);
616
 
 
617
 
        for (i = 0; i < n_fields; i++) {
618
 
 
619
 
#if MLOG_BUF_MARGIN <= 30
620
 
# error "MLOG_BUF_MARGIN <= 30"
621
 
#endif
622
 
 
623
 
                if (log_ptr + 30 > buf_end) {
624
 
                        mlog_close(mtr, log_ptr);
625
 
 
626
 
                        log_ptr = mlog_open(mtr, MLOG_BUF_MARGIN);
627
 
                        buf_end = log_ptr + MLOG_BUF_MARGIN;
628
 
                }
629
 
 
630
 
                upd_field = upd_get_nth_field(update, i);
631
 
 
632
 
                new_val = &(upd_field->new_val);
633
 
 
634
 
                len = dfield_get_len(new_val);
635
 
 
636
 
                log_ptr += mach_write_compressed(log_ptr, upd_field->field_no);
637
 
                log_ptr += mach_write_compressed(log_ptr, len);
638
 
 
639
 
                if (len != UNIV_SQL_NULL) {
640
 
                        if (log_ptr + len < buf_end) {
641
 
                                memcpy(log_ptr, dfield_get_data(new_val), len);
642
 
 
643
 
                                log_ptr += len;
644
 
                        } else {
645
 
                                mlog_close(mtr, log_ptr);
646
 
 
647
 
                                mlog_catenate_string(mtr,
648
 
                                                     dfield_get_data(new_val),
649
 
                                                     len);
650
 
 
651
 
                                log_ptr = mlog_open(mtr, MLOG_BUF_MARGIN);
652
 
                                buf_end = log_ptr + MLOG_BUF_MARGIN;
653
 
                        }
654
 
                }
655
 
        }
656
 
 
657
 
        mlog_close(mtr, log_ptr);
658
 
}
659
 
#endif /* !UNIV_HOTBACKUP */
660
 
 
661
 
/*********************************************************************//**
662
 
Parses the log data written by row_upd_index_write_log.
663
 
@return log data end or NULL */
664
 
UNIV_INTERN
665
 
byte*
666
 
row_upd_index_parse(
667
 
/*================*/
668
 
        byte*           ptr,    /*!< in: buffer */
669
 
        byte*           end_ptr,/*!< in: buffer end */
670
 
        mem_heap_t*     heap,   /*!< in: memory heap where update vector is
671
 
                                built */
672
 
        upd_t**         update_out)/*!< out: update vector */
673
 
{
674
 
        upd_t*          update;
675
 
        upd_field_t*    upd_field;
676
 
        dfield_t*       new_val;
677
 
        ulint           len;
678
 
        ulint           n_fields;
679
 
        ulint           info_bits;
680
 
        ulint           i;
681
 
 
682
 
        if (end_ptr < ptr + 1) {
683
 
 
684
 
                return(NULL);
685
 
        }
686
 
 
687
 
        info_bits = mach_read_from_1(ptr);
688
 
        ptr++;
689
 
        ptr = mach_parse_compressed(ptr, end_ptr, &n_fields);
690
 
 
691
 
        if (ptr == NULL) {
692
 
 
693
 
                return(NULL);
694
 
        }
695
 
 
696
 
        update = upd_create(n_fields, heap);
697
 
        update->info_bits = info_bits;
698
 
 
699
 
        for (i = 0; i < n_fields; i++) {
700
 
                ulint   field_no;
701
 
                upd_field = upd_get_nth_field(update, i);
702
 
                new_val = &(upd_field->new_val);
703
 
 
704
 
                ptr = mach_parse_compressed(ptr, end_ptr, &field_no);
705
 
 
706
 
                if (ptr == NULL) {
707
 
 
708
 
                        return(NULL);
709
 
                }
710
 
 
711
 
                upd_field->field_no = field_no;
712
 
 
713
 
                ptr = mach_parse_compressed(ptr, end_ptr, &len);
714
 
 
715
 
                if (ptr == NULL) {
716
 
 
717
 
                        return(NULL);
718
 
                }
719
 
 
720
 
                if (len != UNIV_SQL_NULL) {
721
 
 
722
 
                        if (end_ptr < ptr + len) {
723
 
 
724
 
                                return(NULL);
725
 
                        }
726
 
 
727
 
                        dfield_set_data(new_val,
728
 
                                        mem_heap_dup(heap, ptr, len), len);
729
 
                        ptr += len;
730
 
                } else {
731
 
                        dfield_set_null(new_val);
732
 
                }
733
 
        }
734
 
 
735
 
        *update_out = update;
736
 
 
737
 
        return(ptr);
738
 
}
739
 
 
740
 
#ifndef UNIV_HOTBACKUP
741
 
/***************************************************************//**
742
 
Builds an update vector from those fields which in a secondary index entry
743
 
differ from a record that has the equal ordering fields. NOTE: we compare
744
 
the fields as binary strings!
745
 
@return own: update vector of differing fields */
746
 
UNIV_INTERN
747
 
upd_t*
748
 
row_upd_build_sec_rec_difference_binary(
749
 
/*====================================*/
750
 
        dict_index_t*   index,  /*!< in: index */
751
 
        const dtuple_t* entry,  /*!< in: entry to insert */
752
 
        const rec_t*    rec,    /*!< in: secondary index record */
753
 
        trx_t*          trx,    /*!< in: transaction */
754
 
        mem_heap_t*     heap)   /*!< in: memory heap from which allocated */
755
 
{
756
 
        upd_field_t*    upd_field;
757
 
        const dfield_t* dfield;
758
 
        const byte*     data;
759
 
        ulint           len;
760
 
        upd_t*          update;
761
 
        ulint           n_diff;
762
 
        ulint           i;
763
 
        ulint           offsets_[REC_OFFS_SMALL_SIZE];
764
 
        const ulint*    offsets;
765
 
        rec_offs_init(offsets_);
766
 
 
767
 
        /* This function is used only for a secondary index */
768
 
        ut_a(!dict_index_is_clust(index));
769
 
 
770
 
        update = upd_create(dtuple_get_n_fields(entry), heap);
771
 
 
772
 
        n_diff = 0;
773
 
        offsets = rec_get_offsets(rec, index, offsets_,
774
 
                                  ULINT_UNDEFINED, &heap);
775
 
 
776
 
        for (i = 0; i < dtuple_get_n_fields(entry); i++) {
777
 
 
778
 
                data = rec_get_nth_field(rec, offsets, i, &len);
779
 
 
780
 
                dfield = dtuple_get_nth_field(entry, i);
781
 
 
782
 
                /* NOTE that it may be that len != dfield_get_len(dfield) if we
783
 
                are updating in a character set and collation where strings of
784
 
                different length can be equal in an alphabetical comparison,
785
 
                and also in the case where we have a column prefix index
786
 
                and the last characters in the index field are spaces; the
787
 
                latter case probably caused the assertion failures reported at
788
 
                row0upd.c line 713 in versions 4.0.14 - 4.0.16. */
789
 
 
790
 
                /* NOTE: we compare the fields as binary strings!
791
 
                (No collation) */
792
 
 
793
 
                if (!dfield_data_is_binary_equal(dfield, len, data)) {
794
 
 
795
 
                        upd_field = upd_get_nth_field(update, n_diff);
796
 
 
797
 
                        dfield_copy(&(upd_field->new_val), dfield);
798
 
 
799
 
                        upd_field_set_field_no(upd_field, i, index, trx);
800
 
 
801
 
                        n_diff++;
802
 
                }
803
 
        }
804
 
 
805
 
        update->n_fields = n_diff;
806
 
 
807
 
        return(update);
808
 
}
809
 
 
810
 
/***************************************************************//**
811
 
Builds an update vector from those fields, excluding the roll ptr and
812
 
trx id fields, which in an index entry differ from a record that has
813
 
the equal ordering fields. NOTE: we compare the fields as binary strings!
814
 
@return own: update vector of differing fields, excluding roll ptr and
815
 
trx id */
816
 
UNIV_INTERN
817
 
upd_t*
818
 
row_upd_build_difference_binary(
819
 
/*============================*/
820
 
        dict_index_t*   index,  /*!< in: clustered index */
821
 
        const dtuple_t* entry,  /*!< in: entry to insert */
822
 
        const rec_t*    rec,    /*!< in: clustered index record */
823
 
        trx_t*          trx,    /*!< in: transaction */
824
 
        mem_heap_t*     heap)   /*!< in: memory heap from which allocated */
825
 
{
826
 
        upd_field_t*    upd_field;
827
 
        const dfield_t* dfield;
828
 
        const byte*     data;
829
 
        ulint           len;
830
 
        upd_t*          update;
831
 
        ulint           n_diff;
832
 
        ulint           roll_ptr_pos;
833
 
        ulint           trx_id_pos;
834
 
        ulint           i;
835
 
        ulint           offsets_[REC_OFFS_NORMAL_SIZE];
836
 
        const ulint*    offsets;
837
 
        rec_offs_init(offsets_);
838
 
 
839
 
        /* This function is used only for a clustered index */
840
 
        ut_a(dict_index_is_clust(index));
841
 
 
842
 
        update = upd_create(dtuple_get_n_fields(entry), heap);
843
 
 
844
 
        n_diff = 0;
845
 
 
846
 
        roll_ptr_pos = dict_index_get_sys_col_pos(index, DATA_ROLL_PTR);
847
 
        trx_id_pos = dict_index_get_sys_col_pos(index, DATA_TRX_ID);
848
 
 
849
 
        offsets = rec_get_offsets(rec, index, offsets_,
850
 
                                  ULINT_UNDEFINED, &heap);
851
 
 
852
 
        for (i = 0; i < dtuple_get_n_fields(entry); i++) {
853
 
 
854
 
                data = rec_get_nth_field(rec, offsets, i, &len);
855
 
 
856
 
                dfield = dtuple_get_nth_field(entry, i);
857
 
 
858
 
                /* NOTE: we compare the fields as binary strings!
859
 
                (No collation) */
860
 
 
861
 
                if (i == trx_id_pos || i == roll_ptr_pos) {
862
 
 
863
 
                        goto skip_compare;
864
 
                }
865
 
 
866
 
                if (UNIV_UNLIKELY(!dfield_is_ext(dfield)
867
 
                                  != !rec_offs_nth_extern(offsets, i))
868
 
                    || !dfield_data_is_binary_equal(dfield, len, data)) {
869
 
 
870
 
                        upd_field = upd_get_nth_field(update, n_diff);
871
 
 
872
 
                        dfield_copy(&(upd_field->new_val), dfield);
873
 
 
874
 
                        upd_field_set_field_no(upd_field, i, index, trx);
875
 
 
876
 
                        n_diff++;
877
 
                }
878
 
skip_compare:
879
 
                ;
880
 
        }
881
 
 
882
 
        update->n_fields = n_diff;
883
 
 
884
 
        return(update);
885
 
}
886
 
 
887
 
/***********************************************************//**
888
 
Fetch a prefix of an externally stored column.  This is similar
889
 
to row_ext_lookup(), but the row_ext_t holds the old values
890
 
of the column and must not be poisoned with the new values.
891
 
@return BLOB prefix */
892
 
static
893
 
byte*
894
 
row_upd_ext_fetch(
895
 
/*==============*/
896
 
        const byte*     data,           /*!< in: 'internally' stored part of the
897
 
                                        field containing also the reference to
898
 
                                        the external part */
899
 
        ulint           local_len,      /*!< in: length of data, in bytes */
900
 
        ulint           zip_size,       /*!< in: nonzero=compressed BLOB
901
 
                                        page size, zero for uncompressed
902
 
                                        BLOBs */
903
 
        ulint*          len,            /*!< in: length of prefix to fetch;
904
 
                                        out: fetched length of the prefix */
905
 
        mem_heap_t*     heap)           /*!< in: heap where to allocate */
906
 
{
907
 
        byte*   buf = mem_heap_alloc(heap, *len);
908
 
 
909
 
        *len = btr_copy_externally_stored_field_prefix(buf, *len,
910
 
                                                       zip_size,
911
 
                                                       data, local_len);
912
 
        /* We should never update records containing a half-deleted BLOB. */
913
 
        ut_a(*len);
914
 
 
915
 
        return(buf);
916
 
}
917
 
 
918
 
/***********************************************************//**
919
 
Replaces the new column value stored in the update vector in
920
 
the given index entry field. */
921
 
static
922
 
void
923
 
row_upd_index_replace_new_col_val(
924
 
/*==============================*/
925
 
        dfield_t*               dfield, /*!< in/out: data field
926
 
                                        of the index entry */
927
 
        const dict_field_t*     field,  /*!< in: index field */
928
 
        const dict_col_t*       col,    /*!< in: field->col */
929
 
        const upd_field_t*      uf,     /*!< in: update field */
930
 
        mem_heap_t*             heap,   /*!< in: memory heap for allocating
931
 
                                        and copying the new value */
932
 
        ulint                   zip_size)/*!< in: compressed page
933
 
                                         size of the table, or 0 */
934
 
{
935
 
        ulint           len;
936
 
        const byte*     data;
937
 
 
938
 
        dfield_copy_data(dfield, &uf->new_val);
939
 
 
940
 
        if (dfield_is_null(dfield)) {
941
 
                return;
942
 
        }
943
 
 
944
 
        len = dfield_get_len(dfield);
945
 
        data = dfield_get_data(dfield);
946
 
 
947
 
        if (field->prefix_len > 0) {
948
 
                ibool           fetch_ext = dfield_is_ext(dfield)
949
 
                        && len < (ulint) field->prefix_len
950
 
                        + BTR_EXTERN_FIELD_REF_SIZE;
951
 
 
952
 
                if (fetch_ext) {
953
 
                        ulint   l = len;
954
 
 
955
 
                        len = field->prefix_len;
956
 
 
957
 
                        data = row_upd_ext_fetch(data, l, zip_size,
958
 
                                                 &len, heap);
959
 
                }
960
 
 
961
 
                len = dtype_get_at_most_n_mbchars(col->prtype,
962
 
                                                  col->mbminmaxlen,
963
 
                                                  field->prefix_len, len,
964
 
                                                  (const char*) data);
965
 
 
966
 
                dfield_set_data(dfield, data, len);
967
 
 
968
 
                if (!fetch_ext) {
969
 
                        dfield_dup(dfield, heap);
970
 
                }
971
 
 
972
 
                return;
973
 
        }
974
 
 
975
 
        switch (uf->orig_len) {
976
 
                byte*   buf;
977
 
        case BTR_EXTERN_FIELD_REF_SIZE:
978
 
                /* Restore the original locally stored
979
 
                part of the column.  In the undo log,
980
 
                InnoDB writes a longer prefix of externally
981
 
                stored columns, so that column prefixes
982
 
                in secondary indexes can be reconstructed. */
983
 
                dfield_set_data(dfield,
984
 
                                data + len - BTR_EXTERN_FIELD_REF_SIZE,
985
 
                                BTR_EXTERN_FIELD_REF_SIZE);
986
 
                dfield_set_ext(dfield);
987
 
                /* fall through */
988
 
        case 0:
989
 
                dfield_dup(dfield, heap);
990
 
                break;
991
 
        default:
992
 
                /* Reconstruct the original locally
993
 
                stored part of the column.  The data
994
 
                will have to be copied. */
995
 
                ut_a(uf->orig_len > BTR_EXTERN_FIELD_REF_SIZE);
996
 
                buf = mem_heap_alloc(heap, uf->orig_len);
997
 
                /* Copy the locally stored prefix. */
998
 
                memcpy(buf, data,
999
 
                       uf->orig_len - BTR_EXTERN_FIELD_REF_SIZE);
1000
 
                /* Copy the BLOB pointer. */
1001
 
                memcpy(buf + uf->orig_len - BTR_EXTERN_FIELD_REF_SIZE,
1002
 
                       data + len - BTR_EXTERN_FIELD_REF_SIZE,
1003
 
                       BTR_EXTERN_FIELD_REF_SIZE);
1004
 
 
1005
 
                dfield_set_data(dfield, buf, uf->orig_len);
1006
 
                dfield_set_ext(dfield);
1007
 
                break;
1008
 
        }
1009
 
}
1010
 
 
1011
 
/***********************************************************//**
1012
 
Replaces the new column values stored in the update vector to the index entry
1013
 
given. */
1014
 
UNIV_INTERN
1015
 
void
1016
 
row_upd_index_replace_new_col_vals_index_pos(
1017
 
/*=========================================*/
1018
 
        dtuple_t*       entry,  /*!< in/out: index entry where replaced;
1019
 
                                the clustered index record must be
1020
 
                                covered by a lock or a page latch to
1021
 
                                prevent deletion (rollback or purge) */
1022
 
        dict_index_t*   index,  /*!< in: index; NOTE that this may also be a
1023
 
                                non-clustered index */
1024
 
        const upd_t*    update, /*!< in: an update vector built for the index so
1025
 
                                that the field number in an upd_field is the
1026
 
                                index position */
1027
 
        ibool           order_only,
1028
 
                                /*!< in: if TRUE, limit the replacement to
1029
 
                                ordering fields of index; note that this
1030
 
                                does not work for non-clustered indexes. */
1031
 
        mem_heap_t*     heap)   /*!< in: memory heap for allocating and
1032
 
                                copying the new values */
1033
 
{
1034
 
        ulint           i;
1035
 
        ulint           n_fields;
1036
 
        const ulint     zip_size        = dict_table_zip_size(index->table);
1037
 
 
1038
 
        ut_ad(index);
1039
 
 
1040
 
        dtuple_set_info_bits(entry, update->info_bits);
1041
 
 
1042
 
        if (order_only) {
1043
 
                n_fields = dict_index_get_n_unique(index);
1044
 
        } else {
1045
 
                n_fields = dict_index_get_n_fields(index);
1046
 
        }
1047
 
 
1048
 
        for (i = 0; i < n_fields; i++) {
1049
 
                const dict_field_t*     field;
1050
 
                const dict_col_t*       col;
1051
 
                const upd_field_t*      uf;
1052
 
 
1053
 
                field = dict_index_get_nth_field(index, i);
1054
 
                col = dict_field_get_col(field);
1055
 
                uf = upd_get_field_by_field_no(update, i);
1056
 
 
1057
 
                if (uf) {
1058
 
                        row_upd_index_replace_new_col_val(
1059
 
                                dtuple_get_nth_field(entry, i),
1060
 
                                field, col, uf, heap, zip_size);
1061
 
                }
1062
 
        }
1063
 
}
1064
 
 
1065
 
/***********************************************************//**
1066
 
Replaces the new column values stored in the update vector to the index entry
1067
 
given. */
1068
 
UNIV_INTERN
1069
 
void
1070
 
row_upd_index_replace_new_col_vals(
1071
 
/*===============================*/
1072
 
        dtuple_t*       entry,  /*!< in/out: index entry where replaced;
1073
 
                                the clustered index record must be
1074
 
                                covered by a lock or a page latch to
1075
 
                                prevent deletion (rollback or purge) */
1076
 
        dict_index_t*   index,  /*!< in: index; NOTE that this may also be a
1077
 
                                non-clustered index */
1078
 
        const upd_t*    update, /*!< in: an update vector built for the
1079
 
                                CLUSTERED index so that the field number in
1080
 
                                an upd_field is the clustered index position */
1081
 
        mem_heap_t*     heap)   /*!< in: memory heap for allocating and
1082
 
                                copying the new values */
1083
 
{
1084
 
        ulint                   i;
1085
 
        const dict_index_t*     clust_index
1086
 
                = dict_table_get_first_index(index->table);
1087
 
        const ulint             zip_size
1088
 
                = dict_table_zip_size(index->table);
1089
 
 
1090
 
        dtuple_set_info_bits(entry, update->info_bits);
1091
 
 
1092
 
        for (i = 0; i < dict_index_get_n_fields(index); i++) {
1093
 
                const dict_field_t*     field;
1094
 
                const dict_col_t*       col;
1095
 
                const upd_field_t*      uf;
1096
 
 
1097
 
                field = dict_index_get_nth_field(index, i);
1098
 
                col = dict_field_get_col(field);
1099
 
                uf = upd_get_field_by_field_no(
1100
 
                        update, dict_col_get_clust_pos(col, clust_index));
1101
 
 
1102
 
                if (uf) {
1103
 
                        row_upd_index_replace_new_col_val(
1104
 
                                dtuple_get_nth_field(entry, i),
1105
 
                                field, col, uf, heap, zip_size);
1106
 
                }
1107
 
        }
1108
 
}
1109
 
 
1110
 
/***********************************************************//**
1111
 
Replaces the new column values stored in the update vector. */
1112
 
UNIV_INTERN
1113
 
void
1114
 
row_upd_replace(
1115
 
/*============*/
1116
 
        dtuple_t*               row,    /*!< in/out: row where replaced,
1117
 
                                        indexed by col_no;
1118
 
                                        the clustered index record must be
1119
 
                                        covered by a lock or a page latch to
1120
 
                                        prevent deletion (rollback or purge) */
1121
 
        row_ext_t**             ext,    /*!< out, own: NULL, or externally
1122
 
                                        stored column prefixes */
1123
 
        const dict_index_t*     index,  /*!< in: clustered index */
1124
 
        const upd_t*            update, /*!< in: an update vector built for the
1125
 
                                        clustered index */
1126
 
        mem_heap_t*             heap)   /*!< in: memory heap */
1127
 
{
1128
 
        ulint                   col_no;
1129
 
        ulint                   i;
1130
 
        ulint                   n_cols;
1131
 
        ulint                   n_ext_cols;
1132
 
        ulint*                  ext_cols;
1133
 
        const dict_table_t*     table;
1134
 
 
1135
 
        ut_ad(row);
1136
 
        ut_ad(ext);
1137
 
        ut_ad(index);
1138
 
        ut_ad(dict_index_is_clust(index));
1139
 
        ut_ad(update);
1140
 
        ut_ad(heap);
1141
 
 
1142
 
        n_cols = dtuple_get_n_fields(row);
1143
 
        table = index->table;
1144
 
        ut_ad(n_cols == dict_table_get_n_cols(table));
1145
 
 
1146
 
        ext_cols = mem_heap_alloc(heap, n_cols * sizeof *ext_cols);
1147
 
        n_ext_cols = 0;
1148
 
 
1149
 
        dtuple_set_info_bits(row, update->info_bits);
1150
 
 
1151
 
        for (col_no = 0; col_no < n_cols; col_no++) {
1152
 
 
1153
 
                const dict_col_t*       col
1154
 
                        = dict_table_get_nth_col(table, col_no);
1155
 
                const ulint             clust_pos
1156
 
                        = dict_col_get_clust_pos(col, index);
1157
 
                dfield_t*               dfield;
1158
 
 
1159
 
                if (UNIV_UNLIKELY(clust_pos == ULINT_UNDEFINED)) {
1160
 
 
1161
 
                        continue;
1162
 
                }
1163
 
 
1164
 
                dfield = dtuple_get_nth_field(row, col_no);
1165
 
 
1166
 
                for (i = 0; i < upd_get_n_fields(update); i++) {
1167
 
 
1168
 
                        const upd_field_t*      upd_field
1169
 
                                = upd_get_nth_field(update, i);
1170
 
 
1171
 
                        if (upd_field->field_no != clust_pos) {
1172
 
 
1173
 
                                continue;
1174
 
                        }
1175
 
 
1176
 
                        dfield_copy_data(dfield, &upd_field->new_val);
1177
 
                        break;
1178
 
                }
1179
 
 
1180
 
                if (dfield_is_ext(dfield) && col->ord_part) {
1181
 
                        ext_cols[n_ext_cols++] = col_no;
1182
 
                }
1183
 
        }
1184
 
 
1185
 
        if (n_ext_cols) {
1186
 
                *ext = row_ext_create(n_ext_cols, ext_cols, row,
1187
 
                                      dict_table_zip_size(table), heap);
1188
 
        } else {
1189
 
                *ext = NULL;
1190
 
        }
1191
 
}
1192
 
 
1193
 
/***********************************************************//**
1194
 
Checks if an update vector changes an ordering field of an index record.
1195
 
 
1196
 
This function is fast if the update vector is short or the number of ordering
1197
 
fields in the index is small. Otherwise, this can be quadratic.
1198
 
NOTE: we compare the fields as binary strings!
1199
 
@return TRUE if update vector changes an ordering field in the index record */
1200
 
UNIV_INTERN
1201
 
ibool
1202
 
row_upd_changes_ord_field_binary(
1203
 
/*=============================*/
1204
 
        const dtuple_t* row,    /*!< in: old value of row, or NULL if the
1205
 
                                row and the data values in update are not
1206
 
                                known when this function is called, e.g., at
1207
 
                                compile time */
1208
 
        dict_index_t*   index,  /*!< in: index of the record */
1209
 
        const upd_t*    update) /*!< in: update vector for the row; NOTE: the
1210
 
                                field numbers in this MUST be clustered index
1211
 
                                positions! */
1212
 
{
1213
 
        ulint           n_unique;
1214
 
        ulint           n_upd_fields;
1215
 
        ulint           i, j;
1216
 
        dict_index_t*   clust_index;
1217
 
 
1218
 
        ut_ad(update && index);
1219
 
 
1220
 
        n_unique = dict_index_get_n_unique(index);
1221
 
        n_upd_fields = upd_get_n_fields(update);
1222
 
 
1223
 
        clust_index = dict_table_get_first_index(index->table);
1224
 
 
1225
 
        for (i = 0; i < n_unique; i++) {
1226
 
 
1227
 
                const dict_field_t*     ind_field;
1228
 
                const dict_col_t*       col;
1229
 
                ulint                   col_pos;
1230
 
                ulint                   col_no;
1231
 
 
1232
 
                ind_field = dict_index_get_nth_field(index, i);
1233
 
                col = dict_field_get_col(ind_field);
1234
 
                col_pos = dict_col_get_clust_pos(col, clust_index);
1235
 
                col_no = dict_col_get_no(col);
1236
 
 
1237
 
                for (j = 0; j < n_upd_fields; j++) {
1238
 
 
1239
 
                        const upd_field_t*      upd_field
1240
 
                                = upd_get_nth_field(update, j);
1241
 
 
1242
 
                        /* Note that if the index field is a column prefix
1243
 
                        then it may be that row does not contain an externally
1244
 
                        stored part of the column value, and we cannot compare
1245
 
                        the datas */
1246
 
 
1247
 
                        if (col_pos == upd_field->field_no
1248
 
                            && (row == NULL
1249
 
                                || ind_field->prefix_len > 0
1250
 
                                || !dfield_datas_are_binary_equal(
1251
 
                                        dtuple_get_nth_field(row, col_no),
1252
 
                                        &(upd_field->new_val)))) {
1253
 
 
1254
 
                                return(TRUE);
1255
 
                        }
1256
 
                }
1257
 
        }
1258
 
 
1259
 
        return(FALSE);
1260
 
}
1261
 
 
1262
 
/***********************************************************//**
1263
 
Checks if an update vector changes an ordering field of an index record.
1264
 
NOTE: we compare the fields as binary strings!
1265
 
@return TRUE if update vector may change an ordering field in an index
1266
 
record */
1267
 
UNIV_INTERN
1268
 
ibool
1269
 
row_upd_changes_some_index_ord_field_binary(
1270
 
/*========================================*/
1271
 
        const dict_table_t*     table,  /*!< in: table */
1272
 
        const upd_t*            update) /*!< in: update vector for the row */
1273
 
{
1274
 
        upd_field_t*    upd_field;
1275
 
        dict_index_t*   index;
1276
 
        ulint           i;
1277
 
 
1278
 
        index = dict_table_get_first_index(table);
1279
 
 
1280
 
        for (i = 0; i < upd_get_n_fields(update); i++) {
1281
 
 
1282
 
                upd_field = upd_get_nth_field(update, i);
1283
 
 
1284
 
                if (dict_field_get_col(dict_index_get_nth_field(
1285
 
                                               index, upd_field->field_no))
1286
 
                    ->ord_part) {
1287
 
 
1288
 
                        return(TRUE);
1289
 
                }
1290
 
        }
1291
 
 
1292
 
        return(FALSE);
1293
 
}
1294
 
 
1295
 
/***********************************************************//**
1296
 
Checks if an update vector changes some of the first ordering fields of an
1297
 
index record. This is only used in foreign key checks and we can assume
1298
 
that index does not contain column prefixes.
1299
 
@return TRUE if changes */
1300
 
static
1301
 
ibool
1302
 
row_upd_changes_first_fields_binary(
1303
 
/*================================*/
1304
 
        dtuple_t*       entry,  /*!< in: index entry */
1305
 
        dict_index_t*   index,  /*!< in: index of entry */
1306
 
        const upd_t*    update, /*!< in: update vector for the row */
1307
 
        ulint           n)      /*!< in: how many first fields to check */
1308
 
{
1309
 
        ulint           n_upd_fields;
1310
 
        ulint           i, j;
1311
 
        dict_index_t*   clust_index;
1312
 
 
1313
 
        ut_ad(update && index);
1314
 
        ut_ad(n <= dict_index_get_n_fields(index));
1315
 
 
1316
 
        n_upd_fields = upd_get_n_fields(update);
1317
 
        clust_index = dict_table_get_first_index(index->table);
1318
 
 
1319
 
        for (i = 0; i < n; i++) {
1320
 
 
1321
 
                const dict_field_t*     ind_field;
1322
 
                const dict_col_t*       col;
1323
 
                ulint                   col_pos;
1324
 
 
1325
 
                ind_field = dict_index_get_nth_field(index, i);
1326
 
                col = dict_field_get_col(ind_field);
1327
 
                col_pos = dict_col_get_clust_pos(col, clust_index);
1328
 
 
1329
 
                ut_a(ind_field->prefix_len == 0);
1330
 
 
1331
 
                for (j = 0; j < n_upd_fields; j++) {
1332
 
 
1333
 
                        upd_field_t*    upd_field
1334
 
                                = upd_get_nth_field(update, j);
1335
 
 
1336
 
                        if (col_pos == upd_field->field_no
1337
 
                            && !dfield_datas_are_binary_equal(
1338
 
                                    dtuple_get_nth_field(entry, i),
1339
 
                                    &(upd_field->new_val))) {
1340
 
 
1341
 
                                return(TRUE);
1342
 
                        }
1343
 
                }
1344
 
        }
1345
 
 
1346
 
        return(FALSE);
1347
 
}
1348
 
 
1349
 
/*********************************************************************//**
1350
 
Copies the column values from a record. */
1351
 
UNIV_INLINE
1352
 
void
1353
 
row_upd_copy_columns(
1354
 
/*=================*/
1355
 
        rec_t*          rec,    /*!< in: record in a clustered index */
1356
 
        const ulint*    offsets,/*!< in: array returned by rec_get_offsets() */
1357
 
        sym_node_t*     column) /*!< in: first column in a column list, or
1358
 
                                NULL */
1359
 
{
1360
 
        byte*   data;
1361
 
        ulint   len;
1362
 
 
1363
 
        while (column) {
1364
 
                data = rec_get_nth_field(rec, offsets,
1365
 
                                         column->field_nos[SYM_CLUST_FIELD_NO],
1366
 
                                         &len);
1367
 
                eval_node_copy_and_alloc_val(column, data, len);
1368
 
 
1369
 
                column = UT_LIST_GET_NEXT(col_var_list, column);
1370
 
        }
1371
 
}
1372
 
 
1373
 
/*********************************************************************//**
1374
 
Calculates the new values for fields to update. Note that row_upd_copy_columns
1375
 
must have been called first. */
1376
 
UNIV_INLINE
1377
 
void
1378
 
row_upd_eval_new_vals(
1379
 
/*==================*/
1380
 
        upd_t*  update) /*!< in/out: update vector */
1381
 
{
1382
 
        que_node_t*     exp;
1383
 
        upd_field_t*    upd_field;
1384
 
        ulint           n_fields;
1385
 
        ulint           i;
1386
 
 
1387
 
        n_fields = upd_get_n_fields(update);
1388
 
 
1389
 
        for (i = 0; i < n_fields; i++) {
1390
 
                upd_field = upd_get_nth_field(update, i);
1391
 
 
1392
 
                exp = upd_field->exp;
1393
 
 
1394
 
                eval_exp(exp);
1395
 
 
1396
 
                dfield_copy_data(&(upd_field->new_val), que_node_get_val(exp));
1397
 
        }
1398
 
}
1399
 
 
1400
 
/***********************************************************//**
1401
 
Stores to the heap the row on which the node->pcur is positioned. */
1402
 
static
1403
 
void
1404
 
row_upd_store_row(
1405
 
/*==============*/
1406
 
        upd_node_t*     node)   /*!< in: row update node */
1407
 
{
1408
 
        dict_index_t*   clust_index;
1409
 
        rec_t*          rec;
1410
 
        mem_heap_t*     heap            = NULL;
1411
 
        row_ext_t**     ext;
1412
 
        ulint           offsets_[REC_OFFS_NORMAL_SIZE];
1413
 
        const ulint*    offsets;
1414
 
        rec_offs_init(offsets_);
1415
 
 
1416
 
        ut_ad(node->pcur->latch_mode != BTR_NO_LATCHES);
1417
 
 
1418
 
        if (node->row != NULL) {
1419
 
                mem_heap_empty(node->heap);
1420
 
        }
1421
 
 
1422
 
        clust_index = dict_table_get_first_index(node->table);
1423
 
 
1424
 
        rec = btr_pcur_get_rec(node->pcur);
1425
 
 
1426
 
        offsets = rec_get_offsets(rec, clust_index, offsets_,
1427
 
                                  ULINT_UNDEFINED, &heap);
1428
 
 
1429
 
        if (dict_table_get_format(node->table) >= DICT_TF_FORMAT_ZIP) {
1430
 
                /* In DYNAMIC or COMPRESSED format, there is no prefix
1431
 
                of externally stored columns in the clustered index
1432
 
                record. Build a cache of column prefixes. */
1433
 
                ext = &node->ext;
1434
 
        } else {
1435
 
                /* REDUNDANT and COMPACT formats store a local
1436
 
                768-byte prefix of each externally stored column.
1437
 
                No cache is needed. */
1438
 
                ext = NULL;
1439
 
                node->ext = NULL;
1440
 
        }
1441
 
 
1442
 
        node->row = row_build(ROW_COPY_DATA, clust_index, rec, offsets,
1443
 
                              NULL, ext, node->heap);
1444
 
        if (node->is_delete) {
1445
 
                node->upd_row = NULL;
1446
 
                node->upd_ext = NULL;
1447
 
        } else {
1448
 
                node->upd_row = dtuple_copy(node->row, node->heap);
1449
 
                row_upd_replace(node->upd_row, &node->upd_ext,
1450
 
                                clust_index, node->update, node->heap);
1451
 
        }
1452
 
 
1453
 
        if (UNIV_LIKELY_NULL(heap)) {
1454
 
                mem_heap_free(heap);
1455
 
        }
1456
 
}
1457
 
 
1458
 
/***********************************************************//**
1459
 
Updates a secondary index entry of a row.
1460
 
@return DB_SUCCESS if operation successfully completed, else error
1461
 
code or DB_LOCK_WAIT */
1462
 
static
1463
 
ulint
1464
 
row_upd_sec_index_entry(
1465
 
/*====================*/
1466
 
        upd_node_t*     node,   /*!< in: row update node */
1467
 
        que_thr_t*      thr)    /*!< in: query thread */
1468
 
{
1469
 
        mtr_t                   mtr;
1470
 
        const rec_t*            rec;
1471
 
        btr_pcur_t              pcur;
1472
 
        mem_heap_t*             heap;
1473
 
        dtuple_t*               entry;
1474
 
        dict_index_t*           index;
1475
 
        btr_cur_t*              btr_cur;
1476
 
        ibool                   referenced;
1477
 
        ulint                   err     = DB_SUCCESS;
1478
 
        trx_t*                  trx     = thr_get_trx(thr);
1479
 
        ulint                   mode    = BTR_MODIFY_LEAF;
1480
 
        enum row_search_result  search_result;
1481
 
 
1482
 
        index = node->index;
1483
 
 
1484
 
        referenced = row_upd_index_is_referenced(index, trx);
1485
 
 
1486
 
        heap = mem_heap_create(1024);
1487
 
 
1488
 
        /* Build old index entry */
1489
 
        entry = row_build_index_entry(node->row, node->ext, index, heap);
1490
 
        ut_a(entry);
1491
 
 
1492
 
        mtr_start(&mtr);
1493
 
 
1494
 
        /* Set the query thread, so that ibuf_insert_low() will be
1495
 
        able to invoke thd_get_trx(). */
1496
 
        btr_pcur_get_btr_cur(&pcur)->thr = thr;
1497
 
 
1498
 
        /* We can only try to use the insert/delete buffer to buffer
1499
 
        delete-mark operations if the index we're modifying has no foreign
1500
 
        key constraints referring to it. */
1501
 
        if (!referenced) {
1502
 
                mode |= BTR_DELETE_MARK;
1503
 
        }
1504
 
 
1505
 
        search_result = row_search_index_entry(index, entry, mode,
1506
 
                                               &pcur, &mtr);
1507
 
 
1508
 
        btr_cur = btr_pcur_get_btr_cur(&pcur);
1509
 
 
1510
 
        rec = btr_cur_get_rec(btr_cur);
1511
 
 
1512
 
        switch (search_result) {
1513
 
        case ROW_NOT_DELETED_REF:       /* should only occur for BTR_DELETE */
1514
 
                ut_error;
1515
 
                break;
1516
 
        case ROW_BUFFERED:
1517
 
                /* Entry was delete marked already. */
1518
 
                break;
1519
 
 
1520
 
        case ROW_NOT_FOUND:
1521
 
                fputs("InnoDB: error in sec index entry update in\n"
1522
 
                      "InnoDB: ", stderr);
1523
 
                dict_index_name_print(stderr, trx, index);
1524
 
                fputs("\n"
1525
 
                      "InnoDB: tuple ", stderr);
1526
 
                dtuple_print(stderr, entry);
1527
 
                fputs("\n"
1528
 
                      "InnoDB: record ", stderr);
1529
 
                rec_print(stderr, rec, index);
1530
 
                putc('\n', stderr);
1531
 
 
1532
 
                trx_print(stderr, trx, 0);
1533
 
 
1534
 
                fputs("\n"
1535
 
                      "InnoDB: Submit a detailed bug report"
1536
 
                      " to http://bugs.mysql.com\n", stderr);
1537
 
                break;
1538
 
        case ROW_FOUND:
1539
 
                /* Delete mark the old index record; it can already be
1540
 
                delete marked if we return after a lock wait in
1541
 
                row_ins_index_entry below */
1542
 
 
1543
 
                if (!rec_get_deleted_flag(
1544
 
                        rec, dict_table_is_comp(index->table))) {
1545
 
 
1546
 
                        err = btr_cur_del_mark_set_sec_rec(
1547
 
                                0, btr_cur, TRUE, thr, &mtr);
1548
 
 
1549
 
                        if (err == DB_SUCCESS && referenced) {
1550
 
 
1551
 
                                ulint*  offsets;
1552
 
 
1553
 
                                offsets = rec_get_offsets(
1554
 
                                        rec, index, NULL, ULINT_UNDEFINED,
1555
 
                                        &heap);
1556
 
 
1557
 
                                /* NOTE that the following call loses
1558
 
                                the position of pcur ! */
1559
 
                                err = row_upd_check_references_constraints(
1560
 
                                        node, &pcur, index->table,
1561
 
                                        index, offsets, thr, &mtr);
1562
 
                        }
1563
 
                }
1564
 
                break;
1565
 
        }
1566
 
 
1567
 
        btr_pcur_close(&pcur);
1568
 
        mtr_commit(&mtr);
1569
 
 
1570
 
        if (node->is_delete || err != DB_SUCCESS) {
1571
 
 
1572
 
                goto func_exit;
1573
 
        }
1574
 
 
1575
 
        /* Build a new index entry */
1576
 
        entry = row_build_index_entry(node->upd_row, node->upd_ext,
1577
 
                                      index, heap);
1578
 
        ut_a(entry);
1579
 
 
1580
 
        /* Insert new index entry */
1581
 
        err = row_ins_index_entry(index, entry, 0, TRUE, thr);
1582
 
 
1583
 
func_exit:
1584
 
        mem_heap_free(heap);
1585
 
 
1586
 
        return(err);
1587
 
}
1588
 
 
1589
 
/***********************************************************//**
1590
 
Updates the secondary index record if it is changed in the row update or
1591
 
deletes it if this is a delete.
1592
 
@return DB_SUCCESS if operation successfully completed, else error
1593
 
code or DB_LOCK_WAIT */
1594
 
static
1595
 
ulint
1596
 
row_upd_sec_step(
1597
 
/*=============*/
1598
 
        upd_node_t*     node,   /*!< in: row update node */
1599
 
        que_thr_t*      thr)    /*!< in: query thread */
1600
 
{
1601
 
        ut_ad((node->state == UPD_NODE_UPDATE_ALL_SEC)
1602
 
              || (node->state == UPD_NODE_UPDATE_SOME_SEC));
1603
 
        ut_ad(!dict_index_is_clust(node->index));
1604
 
 
1605
 
        if (node->state == UPD_NODE_UPDATE_ALL_SEC
1606
 
            || row_upd_changes_ord_field_binary(node->row, node->index,
1607
 
                                                node->update)) {
1608
 
                return(row_upd_sec_index_entry(node, thr));
1609
 
        }
1610
 
 
1611
 
        return(DB_SUCCESS);
1612
 
}
1613
 
 
1614
 
/***********************************************************//**
1615
 
Marks the clustered index record deleted and inserts the updated version
1616
 
of the record to the index. This function should be used when the ordering
1617
 
fields of the clustered index record change. This should be quite rare in
1618
 
database applications.
1619
 
@return DB_SUCCESS if operation successfully completed, else error
1620
 
code or DB_LOCK_WAIT */
1621
 
static
1622
 
ulint
1623
 
row_upd_clust_rec_by_insert(
1624
 
/*========================*/
1625
 
        upd_node_t*     node,   /*!< in: row update node */
1626
 
        dict_index_t*   index,  /*!< in: clustered index of the record */
1627
 
        que_thr_t*      thr,    /*!< in: query thread */
1628
 
        ibool           referenced,/*!< in: TRUE if index may be referenced in
1629
 
                                a foreign key constraint */
1630
 
        mtr_t*          mtr)    /*!< in: mtr; gets committed here */
1631
 
{
1632
 
        mem_heap_t*     heap    = NULL;
1633
 
        btr_pcur_t*     pcur;
1634
 
        btr_cur_t*      btr_cur;
1635
 
        trx_t*          trx;
1636
 
        dict_table_t*   table;
1637
 
        dtuple_t*       entry;
1638
 
        ulint           err;
1639
 
        ibool           change_ownership = FALSE;
1640
 
 
1641
 
        ut_ad(node);
1642
 
        ut_ad(dict_index_is_clust(index));
1643
 
 
1644
 
        trx = thr_get_trx(thr);
1645
 
        table = node->table;
1646
 
        pcur = node->pcur;
1647
 
        btr_cur = btr_pcur_get_btr_cur(pcur);
1648
 
 
1649
 
        if (node->state != UPD_NODE_INSERT_CLUSTERED) {
1650
 
                rec_t*          rec;
1651
 
                dict_index_t*   index;
1652
 
                ulint           offsets_[REC_OFFS_NORMAL_SIZE];
1653
 
                ulint*          offsets;
1654
 
                rec_offs_init(offsets_);
1655
 
 
1656
 
                err = btr_cur_del_mark_set_clust_rec(BTR_NO_LOCKING_FLAG,
1657
 
                                                     btr_cur, TRUE, thr, mtr);
1658
 
                if (err != DB_SUCCESS) {
1659
 
                        mtr_commit(mtr);
1660
 
                        return(err);
1661
 
                }
1662
 
 
1663
 
                /* Mark as not-owned the externally stored fields which the new
1664
 
                row inherits from the delete marked record: purge should not
1665
 
                free those externally stored fields even if the delete marked
1666
 
                record is removed from the index tree, or updated. */
1667
 
 
1668
 
                rec = btr_cur_get_rec(btr_cur);
1669
 
                index = dict_table_get_first_index(table);
1670
 
                offsets = rec_get_offsets(rec, index, offsets_,
1671
 
                                          ULINT_UNDEFINED, &heap);
1672
 
                change_ownership = btr_cur_mark_extern_inherited_fields(
1673
 
                        btr_cur_get_page_zip(btr_cur), rec, index, offsets,
1674
 
                        node->update, mtr);
1675
 
                if (referenced) {
1676
 
                        /* NOTE that the following call loses
1677
 
                        the position of pcur ! */
1678
 
 
1679
 
                        err = row_upd_check_references_constraints(
1680
 
                                node, pcur, table, index, offsets, thr, mtr);
1681
 
 
1682
 
                        if (err != DB_SUCCESS) {
1683
 
 
1684
 
                                mtr_commit(mtr);
1685
 
 
1686
 
                                if (UNIV_LIKELY_NULL(heap)) {
1687
 
                                        mem_heap_free(heap);
1688
 
                                }
1689
 
 
1690
 
                                return(err);
1691
 
                        }
1692
 
                }
1693
 
        }
1694
 
 
1695
 
        mtr_commit(mtr);
1696
 
 
1697
 
        if (!heap) {
1698
 
                heap = mem_heap_create(500);
1699
 
        }
1700
 
        node->state = UPD_NODE_INSERT_CLUSTERED;
1701
 
 
1702
 
        entry = row_build_index_entry(node->upd_row, node->upd_ext,
1703
 
                                      index, heap);
1704
 
        ut_a(entry);
1705
 
 
1706
 
        row_upd_index_entry_sys_field(entry, index, DATA_TRX_ID, trx->id);
1707
 
 
1708
 
        if (change_ownership) {
1709
 
                /* If we return from a lock wait, for example, we may have
1710
 
                extern fields marked as not-owned in entry (marked in the
1711
 
                if-branch above). We must unmark them, take the ownership
1712
 
                back. */
1713
 
 
1714
 
                btr_cur_unmark_dtuple_extern_fields(entry);
1715
 
 
1716
 
                /* We must mark non-updated extern fields in entry as
1717
 
                inherited, so that a possible rollback will not free them. */
1718
 
 
1719
 
                btr_cur_mark_dtuple_inherited_extern(entry, node->update);
1720
 
        }
1721
 
 
1722
 
        err = row_ins_index_entry(index, entry,
1723
 
                                  node->upd_ext ? node->upd_ext->n_ext : 0,
1724
 
                                  TRUE, thr);
1725
 
        mem_heap_free(heap);
1726
 
 
1727
 
        return(err);
1728
 
}
1729
 
 
1730
 
/***********************************************************//**
1731
 
Updates a clustered index record of a row when the ordering fields do
1732
 
not change.
1733
 
@return DB_SUCCESS if operation successfully completed, else error
1734
 
code or DB_LOCK_WAIT */
1735
 
static
1736
 
ulint
1737
 
row_upd_clust_rec(
1738
 
/*==============*/
1739
 
        upd_node_t*     node,   /*!< in: row update node */
1740
 
        dict_index_t*   index,  /*!< in: clustered index */
1741
 
        que_thr_t*      thr,    /*!< in: query thread */
1742
 
        mtr_t*          mtr)    /*!< in: mtr; gets committed here */
1743
 
{
1744
 
        mem_heap_t*     heap    = NULL;
1745
 
        big_rec_t*      big_rec = NULL;
1746
 
        btr_pcur_t*     pcur;
1747
 
        btr_cur_t*      btr_cur;
1748
 
        ulint           err;
1749
 
 
1750
 
        ut_ad(node);
1751
 
        ut_ad(dict_index_is_clust(index));
1752
 
 
1753
 
        pcur = node->pcur;
1754
 
        btr_cur = btr_pcur_get_btr_cur(pcur);
1755
 
 
1756
 
        ut_ad(!rec_get_deleted_flag(btr_pcur_get_rec(pcur),
1757
 
                                    dict_table_is_comp(index->table)));
1758
 
 
1759
 
        /* Try optimistic updating of the record, keeping changes within
1760
 
        the page; we do not check locks because we assume the x-lock on the
1761
 
        record to update */
1762
 
 
1763
 
        if (node->cmpl_info & UPD_NODE_NO_SIZE_CHANGE) {
1764
 
                err = btr_cur_update_in_place(BTR_NO_LOCKING_FLAG,
1765
 
                                              btr_cur, node->update,
1766
 
                                              node->cmpl_info, thr, mtr);
1767
 
        } else {
1768
 
                err = btr_cur_optimistic_update(BTR_NO_LOCKING_FLAG,
1769
 
                                                btr_cur, node->update,
1770
 
                                                node->cmpl_info, thr, mtr);
1771
 
        }
1772
 
 
1773
 
        mtr_commit(mtr);
1774
 
 
1775
 
        if (UNIV_LIKELY(err == DB_SUCCESS)) {
1776
 
 
1777
 
                return(DB_SUCCESS);
1778
 
        }
1779
 
 
1780
 
        if (buf_LRU_buf_pool_running_out()) {
1781
 
 
1782
 
                return(DB_LOCK_TABLE_FULL);
1783
 
        }
1784
 
        /* We may have to modify the tree structure: do a pessimistic descent
1785
 
        down the index tree */
1786
 
 
1787
 
        mtr_start(mtr);
1788
 
 
1789
 
        /* NOTE: this transaction has an s-lock or x-lock on the record and
1790
 
        therefore other transactions cannot modify the record when we have no
1791
 
        latch on the page. In addition, we assume that other query threads of
1792
 
        the same transaction do not modify the record in the meantime.
1793
 
        Therefore we can assert that the restoration of the cursor succeeds. */
1794
 
 
1795
 
        ut_a(btr_pcur_restore_position(BTR_MODIFY_TREE, pcur, mtr));
1796
 
 
1797
 
        ut_ad(!rec_get_deleted_flag(btr_pcur_get_rec(pcur),
1798
 
                                    dict_table_is_comp(index->table)));
1799
 
 
1800
 
        err = btr_cur_pessimistic_update(BTR_NO_LOCKING_FLAG, btr_cur,
1801
 
                                         &heap, &big_rec, node->update,
1802
 
                                         node->cmpl_info, thr, mtr);
1803
 
        mtr_commit(mtr);
1804
 
 
1805
 
        if (err == DB_SUCCESS && big_rec) {
1806
 
                ulint           offsets_[REC_OFFS_NORMAL_SIZE];
1807
 
                rec_t*          rec;
1808
 
                rec_offs_init(offsets_);
1809
 
 
1810
 
                mtr_start(mtr);
1811
 
 
1812
 
                ut_a(btr_pcur_restore_position(BTR_MODIFY_TREE, pcur, mtr));
1813
 
                rec = btr_cur_get_rec(btr_cur);
1814
 
                err = btr_store_big_rec_extern_fields(
1815
 
                        index, btr_cur_get_block(btr_cur), rec,
1816
 
                        rec_get_offsets(rec, index, offsets_,
1817
 
                                        ULINT_UNDEFINED, &heap),
1818
 
                        big_rec, mtr);
1819
 
                mtr_commit(mtr);
1820
 
        }
1821
 
 
1822
 
        if (UNIV_LIKELY_NULL(heap)) {
1823
 
                mem_heap_free(heap);
1824
 
        }
1825
 
 
1826
 
        if (big_rec) {
1827
 
                dtuple_big_rec_free(big_rec);
1828
 
        }
1829
 
 
1830
 
        return(err);
1831
 
}
1832
 
 
1833
 
/***********************************************************//**
1834
 
Delete marks a clustered index record.
1835
 
@return DB_SUCCESS if operation successfully completed, else error code */
1836
 
static
1837
 
ulint
1838
 
row_upd_del_mark_clust_rec(
1839
 
/*=======================*/
1840
 
        upd_node_t*     node,   /*!< in: row update node */
1841
 
        dict_index_t*   index,  /*!< in: clustered index */
1842
 
        ulint*          offsets,/*!< in/out: rec_get_offsets() for the
1843
 
                                record under the cursor */
1844
 
        que_thr_t*      thr,    /*!< in: query thread */
1845
 
        ibool           referenced,
1846
 
                                /*!< in: TRUE if index may be referenced in
1847
 
                                a foreign key constraint */
1848
 
        mtr_t*          mtr)    /*!< in: mtr; gets committed here */
1849
 
{
1850
 
        btr_pcur_t*     pcur;
1851
 
        btr_cur_t*      btr_cur;
1852
 
        ulint           err;
1853
 
 
1854
 
        ut_ad(node);
1855
 
        ut_ad(dict_index_is_clust(index));
1856
 
        ut_ad(node->is_delete);
1857
 
 
1858
 
        pcur = node->pcur;
1859
 
        btr_cur = btr_pcur_get_btr_cur(pcur);
1860
 
 
1861
 
        /* Store row because we have to build also the secondary index
1862
 
        entries */
1863
 
 
1864
 
        row_upd_store_row(node);
1865
 
 
1866
 
        /* Mark the clustered index record deleted; we do not have to check
1867
 
        locks, because we assume that we have an x-lock on the record */
1868
 
 
1869
 
        err = btr_cur_del_mark_set_clust_rec(BTR_NO_LOCKING_FLAG,
1870
 
                                             btr_cur, TRUE, thr, mtr);
1871
 
        if (err == DB_SUCCESS && referenced) {
1872
 
                /* NOTE that the following call loses the position of pcur ! */
1873
 
 
1874
 
                err = row_upd_check_references_constraints(
1875
 
                        node, pcur, index->table, index, offsets, thr, mtr);
1876
 
        }
1877
 
 
1878
 
        mtr_commit(mtr);
1879
 
 
1880
 
        return(err);
1881
 
}
1882
 
 
1883
 
/***********************************************************//**
1884
 
Updates the clustered index record.
1885
 
@return DB_SUCCESS if operation successfully completed, DB_LOCK_WAIT
1886
 
in case of a lock wait, else error code */
1887
 
static
1888
 
ulint
1889
 
row_upd_clust_step(
1890
 
/*===============*/
1891
 
        upd_node_t*     node,   /*!< in: row update node */
1892
 
        que_thr_t*      thr)    /*!< in: query thread */
1893
 
{
1894
 
        dict_index_t*   index;
1895
 
        btr_pcur_t*     pcur;
1896
 
        ibool           success;
1897
 
        ulint           err;
1898
 
        mtr_t*          mtr;
1899
 
        mtr_t           mtr_buf;
1900
 
        rec_t*          rec;
1901
 
        mem_heap_t*     heap            = NULL;
1902
 
        ulint           offsets_[REC_OFFS_NORMAL_SIZE];
1903
 
        ulint*          offsets;
1904
 
        ibool           referenced;
1905
 
        rec_offs_init(offsets_);
1906
 
 
1907
 
        index = dict_table_get_first_index(node->table);
1908
 
 
1909
 
        referenced = row_upd_index_is_referenced(index, thr_get_trx(thr));
1910
 
 
1911
 
        pcur = node->pcur;
1912
 
 
1913
 
        /* We have to restore the cursor to its position */
1914
 
        mtr = &mtr_buf;
1915
 
 
1916
 
        mtr_start(mtr);
1917
 
 
1918
 
        /* If the restoration does not succeed, then the same
1919
 
        transaction has deleted the record on which the cursor was,
1920
 
        and that is an SQL error. If the restoration succeeds, it may
1921
 
        still be that the same transaction has successively deleted
1922
 
        and inserted a record with the same ordering fields, but in
1923
 
        that case we know that the transaction has at least an
1924
 
        implicit x-lock on the record. */
1925
 
 
1926
 
        ut_a(pcur->rel_pos == BTR_PCUR_ON);
1927
 
 
1928
 
        success = btr_pcur_restore_position(BTR_MODIFY_LEAF, pcur, mtr);
1929
 
 
1930
 
        if (!success) {
1931
 
                err = DB_RECORD_NOT_FOUND;
1932
 
 
1933
 
                mtr_commit(mtr);
1934
 
 
1935
 
                return(err);
1936
 
        }
1937
 
 
1938
 
        /* If this is a row in SYS_INDEXES table of the data dictionary,
1939
 
        then we have to free the file segments of the index tree associated
1940
 
        with the index */
1941
 
 
1942
 
        if (node->is_delete && node->table->id == DICT_INDEXES_ID) {
1943
 
 
1944
 
                dict_drop_index_tree(btr_pcur_get_rec(pcur), mtr);
1945
 
 
1946
 
                mtr_commit(mtr);
1947
 
 
1948
 
                mtr_start(mtr);
1949
 
 
1950
 
                success = btr_pcur_restore_position(BTR_MODIFY_LEAF, pcur,
1951
 
                                                    mtr);
1952
 
                if (!success) {
1953
 
                        err = DB_ERROR;
1954
 
 
1955
 
                        mtr_commit(mtr);
1956
 
 
1957
 
                        return(err);
1958
 
                }
1959
 
        }
1960
 
 
1961
 
        rec = btr_pcur_get_rec(pcur);
1962
 
        offsets = rec_get_offsets(rec, index, offsets_,
1963
 
                                  ULINT_UNDEFINED, &heap);
1964
 
 
1965
 
        if (!node->has_clust_rec_x_lock) {
1966
 
                err = lock_clust_rec_modify_check_and_lock(
1967
 
                        0, btr_pcur_get_block(pcur),
1968
 
                        rec, index, offsets, thr);
1969
 
                if (err != DB_SUCCESS) {
1970
 
                        mtr_commit(mtr);
1971
 
                        goto exit_func;
1972
 
                }
1973
 
        }
1974
 
 
1975
 
        /* NOTE: the following function calls will also commit mtr */
1976
 
 
1977
 
        if (node->is_delete) {
1978
 
                err = row_upd_del_mark_clust_rec(
1979
 
                        node, index, offsets, thr, referenced, mtr);
1980
 
 
1981
 
                if (err == DB_SUCCESS) {
1982
 
                        node->state = UPD_NODE_UPDATE_ALL_SEC;
1983
 
                        node->index = dict_table_get_next_index(index);
1984
 
                }
1985
 
exit_func:
1986
 
                if (UNIV_LIKELY_NULL(heap)) {
1987
 
                        mem_heap_free(heap);
1988
 
                }
1989
 
                return(err);
1990
 
        }
1991
 
 
1992
 
        /* If the update is made for MySQL, we already have the update vector
1993
 
        ready, else we have to do some evaluation: */
1994
 
 
1995
 
        if (UNIV_UNLIKELY(!node->in_mysql_interface)) {
1996
 
                /* Copy the necessary columns from clust_rec and calculate the
1997
 
                new values to set */
1998
 
                row_upd_copy_columns(rec, offsets,
1999
 
                                     UT_LIST_GET_FIRST(node->columns));
2000
 
                row_upd_eval_new_vals(node->update);
2001
 
        }
2002
 
 
2003
 
        if (UNIV_LIKELY_NULL(heap)) {
2004
 
                mem_heap_free(heap);
2005
 
        }
2006
 
 
2007
 
        if (node->cmpl_info & UPD_NODE_NO_ORD_CHANGE) {
2008
 
 
2009
 
                err = row_upd_clust_rec(node, index, thr, mtr);
2010
 
                return(err);
2011
 
        }
2012
 
 
2013
 
        row_upd_store_row(node);
2014
 
 
2015
 
        if (row_upd_changes_ord_field_binary(node->row, index, node->update)) {
2016
 
 
2017
 
                /* Update causes an ordering field (ordering fields within
2018
 
                the B-tree) of the clustered index record to change: perform
2019
 
                the update by delete marking and inserting.
2020
 
 
2021
 
                TODO! What to do to the 'Halloween problem', where an update
2022
 
                moves the record forward in index so that it is again
2023
 
                updated when the cursor arrives there? Solution: the
2024
 
                read operation must check the undo record undo number when
2025
 
                choosing records to update. MySQL solves now the problem
2026
 
                externally! */
2027
 
 
2028
 
                err = row_upd_clust_rec_by_insert(
2029
 
                        node, index, thr, referenced, mtr);
2030
 
 
2031
 
                if (err != DB_SUCCESS) {
2032
 
 
2033
 
                        return(err);
2034
 
                }
2035
 
 
2036
 
                node->state = UPD_NODE_UPDATE_ALL_SEC;
2037
 
        } else {
2038
 
                err = row_upd_clust_rec(node, index, thr, mtr);
2039
 
 
2040
 
                if (err != DB_SUCCESS) {
2041
 
 
2042
 
                        return(err);
2043
 
                }
2044
 
 
2045
 
                node->state = UPD_NODE_UPDATE_SOME_SEC;
2046
 
        }
2047
 
 
2048
 
        node->index = dict_table_get_next_index(index);
2049
 
 
2050
 
        return(err);
2051
 
}
2052
 
 
2053
 
/***********************************************************//**
2054
 
Updates the affected index records of a row. When the control is transferred
2055
 
to this node, we assume that we have a persistent cursor which was on a
2056
 
record, and the position of the cursor is stored in the cursor.
2057
 
@return DB_SUCCESS if operation successfully completed, else error
2058
 
code or DB_LOCK_WAIT */
2059
 
static
2060
 
ulint
2061
 
row_upd(
2062
 
/*====*/
2063
 
        upd_node_t*     node,   /*!< in: row update node */
2064
 
        que_thr_t*      thr)    /*!< in: query thread */
2065
 
{
2066
 
        ulint   err     = DB_SUCCESS;
2067
 
 
2068
 
        ut_ad(node && thr);
2069
 
 
2070
 
        if (UNIV_LIKELY(node->in_mysql_interface)) {
2071
 
 
2072
 
                /* We do not get the cmpl_info value from the MySQL
2073
 
                interpreter: we must calculate it on the fly: */
2074
 
 
2075
 
                if (node->is_delete
2076
 
                    || row_upd_changes_some_index_ord_field_binary(
2077
 
                            node->table, node->update)) {
2078
 
                        node->cmpl_info = 0;
2079
 
                } else {
2080
 
                        node->cmpl_info = UPD_NODE_NO_ORD_CHANGE;
2081
 
                }
2082
 
        }
2083
 
 
2084
 
        if (node->state == UPD_NODE_UPDATE_CLUSTERED
2085
 
            || node->state == UPD_NODE_INSERT_CLUSTERED) {
2086
 
 
2087
 
                log_free_check();
2088
 
                err = row_upd_clust_step(node, thr);
2089
 
 
2090
 
                if (err != DB_SUCCESS) {
2091
 
 
2092
 
                        goto function_exit;
2093
 
                }
2094
 
        }
2095
 
 
2096
 
        if (!node->is_delete && (node->cmpl_info & UPD_NODE_NO_ORD_CHANGE)) {
2097
 
 
2098
 
                goto function_exit;
2099
 
        }
2100
 
 
2101
 
        while (node->index != NULL) {
2102
 
 
2103
 
                log_free_check();
2104
 
                err = row_upd_sec_step(node, thr);
2105
 
 
2106
 
                if (err != DB_SUCCESS) {
2107
 
 
2108
 
                        goto function_exit;
2109
 
                }
2110
 
 
2111
 
                node->index = dict_table_get_next_index(node->index);
2112
 
        }
2113
 
 
2114
 
function_exit:
2115
 
        if (err == DB_SUCCESS) {
2116
 
                /* Do some cleanup */
2117
 
 
2118
 
                if (node->row != NULL) {
2119
 
                        node->row = NULL;
2120
 
                        node->ext = NULL;
2121
 
                        node->upd_row = NULL;
2122
 
                        node->upd_ext = NULL;
2123
 
                        mem_heap_empty(node->heap);
2124
 
                }
2125
 
 
2126
 
                node->state = UPD_NODE_UPDATE_CLUSTERED;
2127
 
        }
2128
 
 
2129
 
        return(err);
2130
 
}
2131
 
 
2132
 
/***********************************************************//**
2133
 
Updates a row in a table. This is a high-level function used in SQL execution
2134
 
graphs.
2135
 
@return query thread to run next or NULL */
2136
 
UNIV_INTERN
2137
 
que_thr_t*
2138
 
row_upd_step(
2139
 
/*=========*/
2140
 
        que_thr_t*      thr)    /*!< in: query thread */
2141
 
{
2142
 
        upd_node_t*     node;
2143
 
        sel_node_t*     sel_node;
2144
 
        que_node_t*     parent;
2145
 
        ulint           err             = DB_SUCCESS;
2146
 
        trx_t*          trx;
2147
 
 
2148
 
        ut_ad(thr);
2149
 
 
2150
 
        trx = thr_get_trx(thr);
2151
 
 
2152
 
        trx_start_if_not_started(trx);
2153
 
 
2154
 
        node = thr->run_node;
2155
 
 
2156
 
        sel_node = node->select;
2157
 
 
2158
 
        parent = que_node_get_parent(node);
2159
 
 
2160
 
        ut_ad(que_node_get_type(node) == QUE_NODE_UPDATE);
2161
 
 
2162
 
        if (thr->prev_node == parent) {
2163
 
                node->state = UPD_NODE_SET_IX_LOCK;
2164
 
        }
2165
 
 
2166
 
        if (node->state == UPD_NODE_SET_IX_LOCK) {
2167
 
 
2168
 
                if (!node->has_clust_rec_x_lock) {
2169
 
                        /* It may be that the current session has not yet
2170
 
                        started its transaction, or it has been committed: */
2171
 
 
2172
 
                        err = lock_table(0, node->table, LOCK_IX, thr);
2173
 
 
2174
 
                        if (err != DB_SUCCESS) {
2175
 
 
2176
 
                                goto error_handling;
2177
 
                        }
2178
 
                }
2179
 
 
2180
 
                node->state = UPD_NODE_UPDATE_CLUSTERED;
2181
 
 
2182
 
                if (node->searched_update) {
2183
 
                        /* Reset the cursor */
2184
 
                        sel_node->state = SEL_NODE_OPEN;
2185
 
 
2186
 
                        /* Fetch a row to update */
2187
 
 
2188
 
                        thr->run_node = sel_node;
2189
 
 
2190
 
                        return(thr);
2191
 
                }
2192
 
        }
2193
 
 
2194
 
        /* sel_node is NULL if we are in the MySQL interface */
2195
 
 
2196
 
        if (sel_node && (sel_node->state != SEL_NODE_FETCH)) {
2197
 
 
2198
 
                if (!node->searched_update) {
2199
 
                        /* An explicit cursor should be positioned on a row
2200
 
                        to update */
2201
 
 
2202
 
                        ut_error;
2203
 
 
2204
 
                        err = DB_ERROR;
2205
 
 
2206
 
                        goto error_handling;
2207
 
                }
2208
 
 
2209
 
                ut_ad(sel_node->state == SEL_NODE_NO_MORE_ROWS);
2210
 
 
2211
 
                /* No more rows to update, or the select node performed the
2212
 
                updates directly in-place */
2213
 
 
2214
 
                thr->run_node = parent;
2215
 
 
2216
 
                return(thr);
2217
 
        }
2218
 
 
2219
 
        /* DO THE CHECKS OF THE CONSISTENCY CONSTRAINTS HERE */
2220
 
 
2221
 
        err = row_upd(node, thr);
2222
 
 
2223
 
error_handling:
2224
 
        trx->error_state = err;
2225
 
 
2226
 
        if (err != DB_SUCCESS) {
2227
 
                return(NULL);
2228
 
        }
2229
 
 
2230
 
        /* DO THE TRIGGER ACTIONS HERE */
2231
 
 
2232
 
        if (node->searched_update) {
2233
 
                /* Fetch next row to update */
2234
 
 
2235
 
                thr->run_node = sel_node;
2236
 
        } else {
2237
 
                /* It was an explicit cursor update */
2238
 
 
2239
 
                thr->run_node = parent;
2240
 
        }
2241
 
 
2242
 
        node->state = UPD_NODE_UPDATE_CLUSTERED;
2243
 
 
2244
 
        return(thr);
2245
 
}
2246
 
#endif /* !UNIV_HOTBACKUP */