~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/innobase/dict/dict0crea.c

  • Committer: Monty Taylor
  • Date: 2008-12-18 07:42:01 UTC
  • mto: This revision was merged to the branch mainline in revision 714.
  • Revision ID: monty@inaugust.com-20081218074201-4m7d63t72qfuplwx
Fixed max() problem for solaris

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 dict/dict0crea.c
 
1
/******************************************************
21
2
Database object creation
22
3
 
 
4
(c) 1996 Innobase Oy
 
5
 
23
6
Created 1/8/1996 Heikki Tuuri
24
7
*******************************************************/
25
8
 
43
26
#include "usr0sess.h"
44
27
#include "ut0vec.h"
45
28
 
46
 
/*****************************************************************//**
 
29
/*********************************************************************
47
30
Based on a table object, this function builds the entry to be inserted
48
 
in the SYS_TABLES system table.
49
 
@return the tuple which should be inserted */
 
31
in the SYS_TABLES system table. */
50
32
static
51
33
dtuple_t*
52
34
dict_create_sys_tables_tuple(
53
35
/*=========================*/
54
 
        const dict_table_t*     table,  /*!< in: table */
55
 
        mem_heap_t*             heap)   /*!< in: memory heap from
56
 
                                        which the memory for the built
57
 
                                        tuple is allocated */
 
36
                                /* out: the tuple which should be inserted */
 
37
        dict_table_t*   table,  /* in: table */
 
38
        mem_heap_t*     heap)   /* in: memory heap from which the memory for
 
39
                                the built tuple is allocated */
58
40
{
59
41
        dict_table_t*   sys_tables;
60
42
        dtuple_t*       entry;
61
43
        dfield_t*       dfield;
62
44
        byte*           ptr;
63
45
 
64
 
        ut_ad(table);
65
 
        ut_ad(heap);
 
46
        ut_ad(table && heap);
66
47
 
67
48
        sys_tables = dict_sys->sys_tables;
68
49
 
71
52
        dict_table_copy_types(entry, sys_tables);
72
53
 
73
54
        /* 0: NAME -----------------------------*/
74
 
        dfield = dtuple_get_nth_field(entry, 0/*NAME*/);
 
55
        dfield = dtuple_get_nth_field(entry, 0);
75
56
 
76
57
        dfield_set_data(dfield, table->name, ut_strlen(table->name));
77
58
        /* 3: ID -------------------------------*/
78
 
        dfield = dtuple_get_nth_field(entry, 1/*ID*/);
 
59
        dfield = dtuple_get_nth_field(entry, 1);
79
60
 
80
 
        ptr = static_cast<unsigned char *>(mem_heap_alloc(heap, 8));
 
61
        ptr = mem_heap_alloc(heap, 8);
81
62
        mach_write_to_8(ptr, table->id);
82
63
 
83
64
        dfield_set_data(dfield, ptr, 8);
84
65
        /* 4: N_COLS ---------------------------*/
85
 
        dfield = dtuple_get_nth_field(entry, 2/*N_COLS*/);
 
66
        dfield = dtuple_get_nth_field(entry, 2);
86
67
 
87
68
#if DICT_TF_COMPACT != 1
88
69
#error
89
70
#endif
90
71
 
91
 
        ptr = static_cast<unsigned char *>(mem_heap_alloc(heap, 4));
 
72
        ptr = mem_heap_alloc(heap, 4);
92
73
        mach_write_to_4(ptr, table->n_def
93
74
                        | ((table->flags & DICT_TF_COMPACT) << 31));
94
75
        dfield_set_data(dfield, ptr, 4);
95
76
        /* 5: TYPE -----------------------------*/
96
 
        dfield = dtuple_get_nth_field(entry, 3/*TYPE*/);
 
77
        dfield = dtuple_get_nth_field(entry, 3);
97
78
 
98
 
        ptr = static_cast<unsigned char *>(mem_heap_alloc(heap, 4));
99
 
        if (table->flags & (~DICT_TF_COMPACT & ~(~0 << DICT_TF_BITS))) {
 
79
        ptr = mem_heap_alloc(heap, 4);
 
80
        if (table->flags & ~DICT_TF_COMPACT) {
100
81
                ut_a(table->flags & DICT_TF_COMPACT);
101
82
                ut_a(dict_table_get_format(table) >= DICT_TF_FORMAT_ZIP);
102
83
                ut_a((table->flags & DICT_TF_ZSSIZE_MASK)
103
84
                     <= (DICT_TF_ZSSIZE_MAX << DICT_TF_ZSSIZE_SHIFT));
104
 
                ut_a(!(table->flags & (~0 << DICT_TF2_BITS)));
105
 
                mach_write_to_4(ptr, table->flags & ~(~0 << DICT_TF_BITS));
 
85
                ut_a(!(table->flags & (~0 << DICT_TF_BITS)));
 
86
                mach_write_to_4(ptr, table->flags);
106
87
        } else {
107
88
                mach_write_to_4(ptr, DICT_TABLE_ORDINARY);
108
89
        }
109
90
 
110
91
        dfield_set_data(dfield, ptr, 4);
111
92
        /* 6: MIX_ID (obsolete) ---------------------------*/
112
 
        dfield = dtuple_get_nth_field(entry, 4/*MIX_ID*/);
 
93
        dfield = dtuple_get_nth_field(entry, 4);
113
94
 
114
 
        ptr = static_cast<unsigned char *>(mem_heap_zalloc(heap, 8));
 
95
        ptr = mem_heap_zalloc(heap, 8);
115
96
 
116
97
        dfield_set_data(dfield, ptr, 8);
117
 
        /* 7: MIX_LEN (additional flags) --------------------------*/
118
 
 
119
 
        dfield = dtuple_get_nth_field(entry, 5/*MIX_LEN*/);
120
 
 
121
 
        ptr = static_cast<unsigned char *>(mem_heap_alloc(heap, 4));
122
 
        mach_write_to_4(ptr, table->flags >> DICT_TF2_SHIFT);
 
98
        /* 7: MIX_LEN (obsolete) --------------------------*/
 
99
 
 
100
        dfield = dtuple_get_nth_field(entry, 5);
 
101
 
 
102
        ptr = mem_heap_zalloc(heap, 4);
123
103
 
124
104
        dfield_set_data(dfield, ptr, 4);
125
105
        /* 8: CLUSTER_NAME ---------------------*/
126
 
        dfield = dtuple_get_nth_field(entry, 6/*CLUSTER_NAME*/);
 
106
        dfield = dtuple_get_nth_field(entry, 6);
127
107
        dfield_set_null(dfield); /* not supported */
128
108
 
129
109
        /* 9: SPACE ----------------------------*/
130
 
        dfield = dtuple_get_nth_field(entry, 7/*SPACE*/);
 
110
        dfield = dtuple_get_nth_field(entry, 7);
131
111
 
132
 
        ptr = static_cast<unsigned char *>(mem_heap_alloc(heap, 4));
 
112
        ptr = mem_heap_alloc(heap, 4);
133
113
        mach_write_to_4(ptr, table->space);
134
114
 
135
115
        dfield_set_data(dfield, ptr, 4);
138
118
        return(entry);
139
119
}
140
120
 
141
 
/*****************************************************************//**
 
121
/*********************************************************************
142
122
Based on a table object, this function builds the entry to be inserted
143
 
in the SYS_COLUMNS system table.
144
 
@return the tuple which should be inserted */
 
123
in the SYS_COLUMNS system table. */
145
124
static
146
125
dtuple_t*
147
126
dict_create_sys_columns_tuple(
148
127
/*==========================*/
149
 
        const dict_table_t*     table,  /*!< in: table */
150
 
        ulint                   i,      /*!< in: column number */
151
 
        mem_heap_t*             heap)   /*!< in: memory heap from
152
 
                                        which the memory for the built
153
 
                                        tuple is allocated */
 
128
                                /* out: the tuple which should be inserted */
 
129
        dict_table_t*   table,  /* in: table */
 
130
        ulint           i,      /* in: column number */
 
131
        mem_heap_t*     heap)   /* in: memory heap from which the memory for
 
132
                                the built tuple is allocated */
154
133
{
155
134
        dict_table_t*           sys_columns;
156
135
        dtuple_t*               entry;
157
136
        const dict_col_t*       column;
158
137
        dfield_t*               dfield;
159
138
        byte*                   ptr;
160
 
        const char*             col_name;
 
139
        const char*     col_name;
161
140
 
162
 
        ut_ad(table);
163
 
        ut_ad(heap);
 
141
        ut_ad(table && heap);
164
142
 
165
143
        column = dict_table_get_nth_col(table, i);
166
144
 
171
149
        dict_table_copy_types(entry, sys_columns);
172
150
 
173
151
        /* 0: TABLE_ID -----------------------*/
174
 
        dfield = dtuple_get_nth_field(entry, 0/*TABLE_ID*/);
 
152
        dfield = dtuple_get_nth_field(entry, 0);
175
153
 
176
 
        ptr = static_cast<unsigned char *>(mem_heap_alloc(heap, 8));
 
154
        ptr = mem_heap_alloc(heap, 8);
177
155
        mach_write_to_8(ptr, table->id);
178
156
 
179
157
        dfield_set_data(dfield, ptr, 8);
180
158
        /* 1: POS ----------------------------*/
181
 
        dfield = dtuple_get_nth_field(entry, 1/*POS*/);
 
159
        dfield = dtuple_get_nth_field(entry, 1);
182
160
 
183
 
        ptr = static_cast<unsigned char *>(mem_heap_alloc(heap, 4));
 
161
        ptr = mem_heap_alloc(heap, 4);
184
162
        mach_write_to_4(ptr, i);
185
163
 
186
164
        dfield_set_data(dfield, ptr, 4);
187
165
        /* 4: NAME ---------------------------*/
188
 
        dfield = dtuple_get_nth_field(entry, 2/*NAME*/);
 
166
        dfield = dtuple_get_nth_field(entry, 2);
189
167
 
190
168
        col_name = dict_table_get_col_name(table, i);
191
169
        dfield_set_data(dfield, col_name, ut_strlen(col_name));
192
170
        /* 5: MTYPE --------------------------*/
193
 
        dfield = dtuple_get_nth_field(entry, 3/*MTYPE*/);
 
171
        dfield = dtuple_get_nth_field(entry, 3);
194
172
 
195
 
        ptr = static_cast<unsigned char *>(mem_heap_alloc(heap, 4));
 
173
        ptr = mem_heap_alloc(heap, 4);
196
174
        mach_write_to_4(ptr, column->mtype);
197
175
 
198
176
        dfield_set_data(dfield, ptr, 4);
199
177
        /* 6: PRTYPE -------------------------*/
200
 
        dfield = dtuple_get_nth_field(entry, 4/*PRTYPE*/);
 
178
        dfield = dtuple_get_nth_field(entry, 4);
201
179
 
202
 
        ptr = static_cast<unsigned char *>(mem_heap_alloc(heap, 4));
 
180
        ptr = mem_heap_alloc(heap, 4);
203
181
        mach_write_to_4(ptr, column->prtype);
204
182
 
205
183
        dfield_set_data(dfield, ptr, 4);
206
184
        /* 7: LEN ----------------------------*/
207
 
        dfield = dtuple_get_nth_field(entry, 5/*LEN*/);
 
185
        dfield = dtuple_get_nth_field(entry, 5);
208
186
 
209
 
        ptr = static_cast<unsigned char *>(mem_heap_alloc(heap, 4));
 
187
        ptr = mem_heap_alloc(heap, 4);
210
188
        mach_write_to_4(ptr, column->len);
211
189
 
212
190
        dfield_set_data(dfield, ptr, 4);
213
191
        /* 8: PREC ---------------------------*/
214
 
        dfield = dtuple_get_nth_field(entry, 6/*PREC*/);
 
192
        dfield = dtuple_get_nth_field(entry, 6);
215
193
 
216
 
        ptr = static_cast<unsigned char *>(mem_heap_alloc(heap, 4));
 
194
        ptr = mem_heap_alloc(heap, 4);
217
195
        mach_write_to_4(ptr, 0/* unused */);
218
196
 
219
197
        dfield_set_data(dfield, ptr, 4);
222
200
        return(entry);
223
201
}
224
202
 
225
 
/***************************************************************//**
226
 
Builds a table definition to insert.
227
 
@return DB_SUCCESS or error code */
 
203
/*******************************************************************
 
204
Builds a table definition to insert. */
228
205
static
229
206
ulint
230
207
dict_build_table_def_step(
231
208
/*======================*/
232
 
        que_thr_t*      thr,    /*!< in: query thread */
233
 
        tab_node_t*     node)   /*!< in: table create node */
 
209
                                /* out: DB_SUCCESS or error code */
 
210
        que_thr_t*      thr,    /* in: query thread */
 
211
        tab_node_t*     node)   /* in: table create node */
234
212
{
235
213
        dict_table_t*   table;
236
214
        dtuple_t*       row;
237
215
        ulint           error;
238
 
        ulint           flags;
239
216
        const char*     path_or_name;
240
217
        ibool           is_path;
241
218
        mtr_t           mtr;
242
 
        ulint           space = 0;
243
 
        ibool           file_per_table;
244
219
 
245
220
        ut_ad(mutex_own(&(dict_sys->mutex)));
246
221
 
247
222
        table = node->table;
248
223
 
249
 
        /* Cache the global variable "srv_file_per_table" to
250
 
        a local variable before using it. Please note
251
 
        "srv_file_per_table" is not under dict_sys mutex
252
 
        protection, and could be changed while executing
253
 
        this function. So better to cache the current value
254
 
        to a local variable, and all future reference to
255
 
        "srv_file_per_table" should use this local variable. */
256
 
        file_per_table = srv_file_per_table;
257
 
 
258
 
        dict_hdr_get_new_id(&table->id, NULL, NULL);
 
224
        table->id = dict_hdr_get_new_id(DICT_HDR_TABLE_ID);
259
225
 
260
226
        thr_get_trx(thr)->table_id = table->id;
261
227
 
262
 
        if (file_per_table) {
263
 
                /* Get a new space id if srv_file_per_table is set */
264
 
                dict_hdr_get_new_id(NULL, NULL, &space);
265
 
 
266
 
                if (UNIV_UNLIKELY(space == ULINT_UNDEFINED)) {
267
 
                        return(DB_ERROR);
268
 
                }
269
 
 
 
228
        if (srv_file_per_table) {
270
229
                /* We create a new single-table tablespace for the table.
271
230
                We initially let it be 4 pages:
272
231
                - page 0 is the fsp header and an extent descriptor page,
275
234
                - page 3 will contain the root of the clustered index of the
276
235
                table we create here. */
277
236
 
 
237
                ulint   space = 0;      /* reset to zero for the call below */
 
238
 
278
239
                if (table->dir_path_of_temp_table) {
279
240
                        /* We place tables created with CREATE TEMPORARY
280
241
                        TABLE in the tmp dir of mysqld server */
290
251
                ut_ad(!dict_table_zip_size(table)
291
252
                      || dict_table_get_format(table) >= DICT_TF_FORMAT_ZIP);
292
253
 
293
 
                flags = table->flags & ~(~0 << DICT_TF_BITS);
294
254
                error = fil_create_new_single_table_tablespace(
295
 
                        space, path_or_name, is_path,
296
 
                        flags == DICT_TF_COMPACT ? 0 : flags,
 
255
                        &space, path_or_name, is_path,
 
256
                        table->flags == DICT_TF_COMPACT ? 0 : table->flags,
297
257
                        FIL_IBD_FILE_INITIAL_SIZE);
298
258
                table->space = (unsigned int) space;
299
259
 
309
269
                mtr_commit(&mtr);
310
270
        } else {
311
271
                /* Create in the system tablespace: disallow new features */
312
 
                table->flags &= (~0 << DICT_TF_BITS) | DICT_TF_COMPACT;
 
272
                table->flags &= DICT_TF_COMPACT;
313
273
        }
314
274
 
315
275
        row = dict_create_sys_tables_tuple(table, node->heap);
319
279
        return(DB_SUCCESS);
320
280
}
321
281
 
322
 
/***************************************************************//**
323
 
Builds a column definition to insert.
324
 
@return DB_SUCCESS */
 
282
/*******************************************************************
 
283
Builds a column definition to insert. */
325
284
static
326
285
ulint
327
286
dict_build_col_def_step(
328
287
/*====================*/
329
 
        tab_node_t*     node)   /*!< in: table create node */
 
288
                                /* out: DB_SUCCESS */
 
289
        tab_node_t*     node)   /* in: table create node */
330
290
{
331
291
        dtuple_t*       row;
332
292
 
337
297
        return(DB_SUCCESS);
338
298
}
339
299
 
340
 
/*****************************************************************//**
 
300
/*********************************************************************
341
301
Based on an index object, this function builds the entry to be inserted
342
 
in the SYS_INDEXES system table.
343
 
@return the tuple which should be inserted */
 
302
in the SYS_INDEXES system table. */
344
303
static
345
304
dtuple_t*
346
305
dict_create_sys_indexes_tuple(
347
306
/*==========================*/
348
 
        const dict_index_t*     index,  /*!< in: index */
349
 
        mem_heap_t*             heap)   /*!< in: memory heap from
350
 
                                        which the memory for the built
351
 
                                        tuple is allocated */
 
307
                                /* out: the tuple which should be inserted */
 
308
        dict_index_t*   index,  /* in: index */
 
309
        mem_heap_t*     heap)   /* in: memory heap from which the memory for
 
310
                                the built tuple is allocated */
352
311
{
353
312
        dict_table_t*   sys_indexes;
354
313
        dict_table_t*   table;
357
316
        byte*           ptr;
358
317
 
359
318
        ut_ad(mutex_own(&(dict_sys->mutex)));
360
 
        ut_ad(index);
361
 
        ut_ad(heap);
 
319
        ut_ad(index && heap);
362
320
 
363
321
        sys_indexes = dict_sys->sys_indexes;
364
322
 
369
327
        dict_table_copy_types(entry, sys_indexes);
370
328
 
371
329
        /* 0: TABLE_ID -----------------------*/
372
 
        dfield = dtuple_get_nth_field(entry, 0/*TABLE_ID*/);
 
330
        dfield = dtuple_get_nth_field(entry, 0);
373
331
 
374
 
        ptr = static_cast<unsigned char *>(mem_heap_alloc(heap, 8));
 
332
        ptr = mem_heap_alloc(heap, 8);
375
333
        mach_write_to_8(ptr, table->id);
376
334
 
377
335
        dfield_set_data(dfield, ptr, 8);
378
336
        /* 1: ID ----------------------------*/
379
 
        dfield = dtuple_get_nth_field(entry, 1/*ID*/);
 
337
        dfield = dtuple_get_nth_field(entry, 1);
380
338
 
381
 
        ptr = static_cast<unsigned char *>(mem_heap_alloc(heap, 8));
 
339
        ptr = mem_heap_alloc(heap, 8);
382
340
        mach_write_to_8(ptr, index->id);
383
341
 
384
342
        dfield_set_data(dfield, ptr, 8);
385
343
        /* 4: NAME --------------------------*/
386
 
        dfield = dtuple_get_nth_field(entry, 2/*NAME*/);
 
344
        dfield = dtuple_get_nth_field(entry, 2);
387
345
 
388
346
        dfield_set_data(dfield, index->name, ut_strlen(index->name));
389
347
        /* 5: N_FIELDS ----------------------*/
390
 
        dfield = dtuple_get_nth_field(entry, 3/*N_FIELDS*/);
 
348
        dfield = dtuple_get_nth_field(entry, 3);
391
349
 
392
 
        ptr = static_cast<unsigned char *>(mem_heap_alloc(heap, 4));
 
350
        ptr = mem_heap_alloc(heap, 4);
393
351
        mach_write_to_4(ptr, index->n_fields);
394
352
 
395
353
        dfield_set_data(dfield, ptr, 4);
396
354
        /* 6: TYPE --------------------------*/
397
 
        dfield = dtuple_get_nth_field(entry, 4/*TYPE*/);
 
355
        dfield = dtuple_get_nth_field(entry, 4);
398
356
 
399
 
        ptr = static_cast<unsigned char *>(mem_heap_alloc(heap, 4));
 
357
        ptr = mem_heap_alloc(heap, 4);
400
358
        mach_write_to_4(ptr, index->type);
401
359
 
402
360
        dfield_set_data(dfield, ptr, 4);
406
364
#error "DICT_SYS_INDEXES_SPACE_NO_FIELD != 7"
407
365
#endif
408
366
 
409
 
        dfield = dtuple_get_nth_field(entry, 5/*SPACE*/);
 
367
        dfield = dtuple_get_nth_field(entry, 5);
410
368
 
411
 
        ptr = static_cast<unsigned char *>(mem_heap_alloc(heap, 4));
 
369
        ptr = mem_heap_alloc(heap, 4);
412
370
        mach_write_to_4(ptr, index->space);
413
371
 
414
372
        dfield_set_data(dfield, ptr, 4);
418
376
#error "DICT_SYS_INDEXES_PAGE_NO_FIELD != 8"
419
377
#endif
420
378
 
421
 
        dfield = dtuple_get_nth_field(entry, 6/*PAGE_NO*/);
 
379
        dfield = dtuple_get_nth_field(entry, 6);
422
380
 
423
 
        ptr = static_cast<unsigned char *>(mem_heap_alloc(heap, 4));
 
381
        ptr = mem_heap_alloc(heap, 4);
424
382
        mach_write_to_4(ptr, FIL_NULL);
425
383
 
426
384
        dfield_set_data(dfield, ptr, 4);
429
387
        return(entry);
430
388
}
431
389
 
432
 
/*****************************************************************//**
 
390
/*********************************************************************
433
391
Based on an index object, this function builds the entry to be inserted
434
 
in the SYS_FIELDS system table.
435
 
@return the tuple which should be inserted */
 
392
in the SYS_FIELDS system table. */
436
393
static
437
394
dtuple_t*
438
395
dict_create_sys_fields_tuple(
439
396
/*=========================*/
440
 
        const dict_index_t*     index,  /*!< in: index */
441
 
        ulint                   i,      /*!< in: field number */
442
 
        mem_heap_t*             heap)   /*!< in: memory heap from
443
 
                                        which the memory for the built
444
 
                                        tuple is allocated */
 
397
                                /* out: the tuple which should be inserted */
 
398
        dict_index_t*   index,  /* in: index */
 
399
        ulint           i,      /* in: field number */
 
400
        mem_heap_t*     heap)   /* in: memory heap from which the memory for
 
401
                                the built tuple is allocated */
445
402
{
446
403
        dict_table_t*   sys_fields;
447
404
        dtuple_t*       entry;
451
408
        ibool           index_contains_column_prefix_field      = FALSE;
452
409
        ulint           j;
453
410
 
454
 
        ut_ad(index);
455
 
        ut_ad(heap);
 
411
        ut_ad(index && heap);
456
412
 
457
413
        for (j = 0; j < index->n_fields; j++) {
458
414
                if (dict_index_get_nth_field(index, j)->prefix_len > 0) {
470
426
        dict_table_copy_types(entry, sys_fields);
471
427
 
472
428
        /* 0: INDEX_ID -----------------------*/
473
 
        dfield = dtuple_get_nth_field(entry, 0/*INDEX_ID*/);
 
429
        dfield = dtuple_get_nth_field(entry, 0);
474
430
 
475
 
        ptr = static_cast<unsigned char *>(mem_heap_alloc(heap, 8));
 
431
        ptr = mem_heap_alloc(heap, 8);
476
432
        mach_write_to_8(ptr, index->id);
477
433
 
478
434
        dfield_set_data(dfield, ptr, 8);
479
435
        /* 1: POS + PREFIX LENGTH ----------------------------*/
480
436
 
481
 
        dfield = dtuple_get_nth_field(entry, 1/*POS*/);
 
437
        dfield = dtuple_get_nth_field(entry, 1);
482
438
 
483
 
        ptr = static_cast<unsigned char *>(mem_heap_alloc(heap, 4));
 
439
        ptr = mem_heap_alloc(heap, 4);
484
440
 
485
441
        if (index_contains_column_prefix_field) {
486
442
                /* If there are column prefix fields in the index, then
498
454
 
499
455
        dfield_set_data(dfield, ptr, 4);
500
456
        /* 4: COL_NAME -------------------------*/
501
 
        dfield = dtuple_get_nth_field(entry, 2/*COL_NAME*/);
 
457
        dfield = dtuple_get_nth_field(entry, 2);
502
458
 
503
459
        dfield_set_data(dfield, field->name,
504
460
                        ut_strlen(field->name));
507
463
        return(entry);
508
464
}
509
465
 
510
 
/*****************************************************************//**
 
466
/*********************************************************************
511
467
Creates the tuple with which the index entry is searched for writing the index
512
 
tree root page number, if such a tree is created.
513
 
@return the tuple for search */
 
468
tree root page number, if such a tree is created. */
514
469
static
515
470
dtuple_t*
516
471
dict_create_search_tuple(
517
472
/*=====================*/
518
 
        const dtuple_t* tuple,  /*!< in: the tuple inserted in the SYS_INDEXES
 
473
                                /* out: the tuple for search */
 
474
        const dtuple_t* tuple,  /* in: the tuple inserted in the SYS_INDEXES
519
475
                                table */
520
 
        mem_heap_t*     heap)   /*!< in: memory heap from which the memory for
 
476
        mem_heap_t*     heap)   /* in: memory heap from which the memory for
521
477
                                the built tuple is allocated */
522
478
{
523
479
        dtuple_t*       search_tuple;
543
499
        return(search_tuple);
544
500
}
545
501
 
546
 
/***************************************************************//**
547
 
Builds an index definition row to insert.
548
 
@return DB_SUCCESS or error code */
 
502
/*******************************************************************
 
503
Builds an index definition row to insert. */
549
504
static
550
505
ulint
551
506
dict_build_index_def_step(
552
507
/*======================*/
553
 
        que_thr_t*      thr,    /*!< in: query thread */
554
 
        ind_node_t*     node)   /*!< in: index create node */
 
508
                                /* out: DB_SUCCESS or error code */
 
509
        que_thr_t*      thr,    /* in: query thread */
 
510
        ind_node_t*     node)   /* in: index create node */
555
511
{
556
512
        dict_table_t*   table;
557
513
        dict_index_t*   index;
577
533
        ut_ad((UT_LIST_GET_LEN(table->indexes) > 0)
578
534
              || dict_index_is_clust(index));
579
535
 
580
 
        dict_hdr_get_new_id(NULL, &index->id, NULL);
 
536
        index->id = dict_hdr_get_new_id(DICT_HDR_INDEX_ID);
581
537
 
582
538
        /* Inherit the space id from the table; we store all indexes of a
583
539
        table in the same tablespace */
589
545
 
590
546
        ins_node_set_new_row(node->ind_def, row);
591
547
 
 
548
#ifdef ROW_MERGE_IS_INDEX_USABLE
592
549
        /* Note that the index was created by this transaction. */
593
550
        index->trx_id = trx->id;
 
551
#endif /* ROW_MERGE_IS_INDEX_USABLE */
594
552
 
595
553
        return(DB_SUCCESS);
596
554
}
597
555
 
598
 
/***************************************************************//**
599
 
Builds a field definition row to insert.
600
 
@return DB_SUCCESS */
 
556
/*******************************************************************
 
557
Builds a field definition row to insert. */
601
558
static
602
559
ulint
603
560
dict_build_field_def_step(
604
561
/*======================*/
605
 
        ind_node_t*     node)   /*!< in: index create node */
 
562
                                /* out: DB_SUCCESS */
 
563
        ind_node_t*     node)   /* in: index create node */
606
564
{
607
565
        dict_index_t*   index;
608
566
        dtuple_t*       row;
616
574
        return(DB_SUCCESS);
617
575
}
618
576
 
619
 
/***************************************************************//**
620
 
Creates an index tree for the index if it is not a member of a cluster.
621
 
@return DB_SUCCESS or DB_OUT_OF_FILE_SPACE */
 
577
/*******************************************************************
 
578
Creates an index tree for the index if it is not a member of a cluster. */
622
579
static
623
580
ulint
624
581
dict_create_index_tree_step(
625
582
/*========================*/
626
 
        ind_node_t*     node)   /*!< in: index create node */
 
583
                                /* out: DB_SUCCESS or DB_OUT_OF_FILE_SPACE */
 
584
        ind_node_t*     node)   /* in: index create node */
627
585
{
628
586
        dict_index_t*   index;
629
587
        dict_table_t*   sys_indexes;
 
588
        dict_table_t*   table;
630
589
        dtuple_t*       search_tuple;
631
 
        ulint           zip_size;
632
590
        btr_pcur_t      pcur;
633
591
        mtr_t           mtr;
634
592
 
635
593
        ut_ad(mutex_own(&(dict_sys->mutex)));
636
594
 
637
595
        index = node->index;
 
596
        table = node->table;
638
597
 
639
598
        sys_indexes = dict_sys->sys_indexes;
640
599
 
652
611
 
653
612
        btr_pcur_move_to_next_user_rec(&pcur, &mtr);
654
613
 
655
 
        zip_size = dict_table_zip_size(index->table);
656
 
 
657
 
        node->page_no = btr_create(index->type, index->space, zip_size,
 
614
        node->page_no = btr_create(index->type, index->space,
 
615
                                   dict_table_zip_size(index->table),
658
616
                                   index->id, index, &mtr);
659
617
        /* printf("Created a new index tree in space %lu root page %lu\n",
660
618
        index->space, index->page_no); */
673
631
        return(DB_SUCCESS);
674
632
}
675
633
 
676
 
/*******************************************************************//**
 
634
/***********************************************************************
677
635
Drops the index tree associated with a row in SYS_INDEXES table. */
678
636
UNIV_INTERN
679
637
void
680
638
dict_drop_index_tree(
681
639
/*=================*/
682
 
        rec_t*  rec,    /*!< in/out: record in the clustered index
 
640
        rec_t*  rec,    /* in/out: record in the clustered index
683
641
                        of SYS_INDEXES table */
684
 
        mtr_t*  mtr)    /*!< in: mtr having the latch on the record page */
 
642
        mtr_t*  mtr)    /* in: mtr having the latch on the record page */
685
643
{
686
644
        ulint           root_page_no;
687
645
        ulint           space;
736
694
                                     FIL_NULL, mtr);
737
695
}
738
696
 
739
 
/*******************************************************************//**
740
 
Truncates the index tree associated with a row in SYS_INDEXES table.
741
 
@return new root page number, or FIL_NULL on failure */
 
697
/***********************************************************************
 
698
Truncates the index tree associated with a row in SYS_INDEXES table. */
742
699
UNIV_INTERN
743
700
ulint
744
701
dict_truncate_index_tree(
745
702
/*=====================*/
746
 
        dict_table_t*   table,  /*!< in: the table the index belongs to */
747
 
        ulint           space,  /*!< in: 0=truncate,
 
703
                                /* out: new root page number, or
 
704
                                FIL_NULL on failure */
 
705
        dict_table_t*   table,  /* in: the table the index belongs to */
 
706
        ulint           space,  /* in: 0=truncate,
748
707
                                nonzero=create the index tree in the
749
708
                                given tablespace */
750
 
        btr_pcur_t*     pcur,   /*!< in/out: persistent cursor pointing to
 
709
        btr_pcur_t*     pcur,   /* in/out: persistent cursor pointing to
751
710
                                record in the clustered index of
752
711
                                SYS_INDEXES table. The cursor may be
753
712
                                repositioned in this call. */
754
 
        mtr_t*          mtr)    /*!< in: mtr having the latch
 
713
        mtr_t*          mtr)    /* in: mtr having the latch
755
714
                                on the record page. The mtr may be
756
715
                                committed and restarted in this call. */
757
716
{
759
718
        ibool           drop = !space;
760
719
        ulint           zip_size;
761
720
        ulint           type;
762
 
        index_id_t      index_id;
 
721
        dulint          index_id;
763
722
        rec_t*          rec;
764
723
        const byte*     ptr;
765
724
        ulint           len;
828
787
        appropriate field in the SYS_INDEXES record: this mini-transaction
829
788
        marks the B-tree totally truncated */
830
789
 
831
 
        btr_block_get(space, zip_size, root_page_no, RW_X_LATCH, mtr);
 
790
        btr_page_get(space, zip_size, root_page_no, RW_X_LATCH, mtr);
832
791
 
833
792
        btr_free_root(space, zip_size, root_page_no, mtr);
834
793
create:
852
811
        for (index = UT_LIST_GET_FIRST(table->indexes);
853
812
             index;
854
813
             index = UT_LIST_GET_NEXT(indexes, index)) {
855
 
                if (index->id == index_id) {
 
814
                if (!ut_dulint_cmp(index->id, index_id)) {
856
815
                        root_page_no = btr_create(type, space, zip_size,
857
816
                                                  index_id, index, mtr);
858
817
                        index->page = (unsigned int) root_page_no;
862
821
 
863
822
        ut_print_timestamp(stderr);
864
823
        fprintf(stderr,
865
 
                "  InnoDB: Index %llu of table %s is missing\n"
 
824
                "  InnoDB: Index %lu %lu of table %s is missing\n"
866
825
                "InnoDB: from the data dictionary during TRUNCATE!\n",
867
 
                (ullint) index_id,
 
826
                ut_dulint_get_high(index_id),
 
827
                ut_dulint_get_low(index_id),
868
828
                table->name);
869
829
 
870
830
        return(FIL_NULL);
871
831
}
872
832
 
873
 
/*********************************************************************//**
874
 
Creates a table create graph.
875
 
@return own: table create node */
 
833
/*************************************************************************
 
834
Creates a table create graph. */
876
835
UNIV_INTERN
877
836
tab_node_t*
878
837
tab_create_graph_create(
879
838
/*====================*/
880
 
        dict_table_t*   table,  /*!< in: table to create, built as a memory data
 
839
                                /* out, own: table create node */
 
840
        dict_table_t*   table,  /* in: table to create, built as a memory data
881
841
                                structure */
882
 
        mem_heap_t*     heap)   /*!< in: heap where created */
 
842
        mem_heap_t*     heap)   /* in: heap where created */
883
843
{
884
844
        tab_node_t*     node;
885
845
 
886
 
        node = static_cast<tab_node_t *>(mem_heap_alloc(heap, sizeof(tab_node_t)));
 
846
        node = mem_heap_alloc(heap, sizeof(tab_node_t));
887
847
 
888
848
        node->common.type = QUE_NODE_CREATE_TABLE;
889
849
 
906
866
        return(node);
907
867
}
908
868
 
909
 
/*********************************************************************//**
910
 
Creates an index create graph.
911
 
@return own: index create node */
 
869
/*************************************************************************
 
870
Creates an index create graph. */
912
871
UNIV_INTERN
913
872
ind_node_t*
914
873
ind_create_graph_create(
915
874
/*====================*/
916
 
        dict_index_t*   index,  /*!< in: index to create, built as a memory data
 
875
                                /* out, own: index create node */
 
876
        dict_index_t*   index,  /* in: index to create, built as a memory data
917
877
                                structure */
918
 
        mem_heap_t*     heap)   /*!< in: heap where created */
 
878
        mem_heap_t*     heap)   /* in: heap where created */
919
879
{
920
880
        ind_node_t*     node;
921
881
 
922
 
        node = static_cast<ind_node_t *>(mem_heap_alloc(heap, sizeof(ind_node_t)));
 
882
        node = mem_heap_alloc(heap, sizeof(ind_node_t));
923
883
 
924
884
        node->common.type = QUE_NODE_CREATE_INDEX;
925
885
 
943
903
        return(node);
944
904
}
945
905
 
946
 
/***********************************************************//**
947
 
Creates a table. This is a high-level function used in SQL execution graphs.
948
 
@return query thread to run next or NULL */
 
906
/***************************************************************
 
907
Creates a table. This is a high-level function used in SQL execution graphs. */
949
908
UNIV_INTERN
950
909
que_thr_t*
951
910
dict_create_table_step(
952
911
/*===================*/
953
 
        que_thr_t*      thr)    /*!< in: query thread */
 
912
                                /* out: query thread to run next or NULL */
 
913
        que_thr_t*      thr)    /* in: query thread */
954
914
{
955
915
        tab_node_t*     node;
956
916
        ulint           err     = DB_ERROR;
961
921
 
962
922
        trx = thr_get_trx(thr);
963
923
 
964
 
        node = static_cast<tab_node_t *>(thr->run_node);
 
924
        node = thr->run_node;
965
925
 
966
926
        ut_ad(que_node_get_type(node) == QUE_NODE_CREATE_TABLE);
967
927
 
1049
1009
        return(thr);
1050
1010
}
1051
1011
 
1052
 
/***********************************************************//**
 
1012
/***************************************************************
1053
1013
Creates an index. This is a high-level function used in SQL execution
1054
 
graphs.
1055
 
@return query thread to run next or NULL */
 
1014
graphs. */
1056
1015
UNIV_INTERN
1057
1016
que_thr_t*
1058
1017
dict_create_index_step(
1059
1018
/*===================*/
1060
 
        que_thr_t*      thr)    /*!< in: query thread */
 
1019
                                /* out: query thread to run next or NULL */
 
1020
        que_thr_t*      thr)    /* in: query thread */
1061
1021
{
1062
1022
        ind_node_t*     node;
1063
1023
        ulint           err     = DB_ERROR;
1068
1028
 
1069
1029
        trx = thr_get_trx(thr);
1070
1030
 
1071
 
        node = static_cast<ind_node_t *>(thr->run_node);
 
1031
        node = thr->run_node;
1072
1032
 
1073
1033
        ut_ad(que_node_get_type(node) == QUE_NODE_CREATE_INDEX);
1074
1034
 
1116
1076
 
1117
1077
        if (node->state == INDEX_ADD_TO_CACHE) {
1118
1078
 
1119
 
                index_id_t      index_id = node->index->id;
 
1079
                dulint  index_id = node->index->id;
1120
1080
 
1121
 
                err = dict_index_add_to_cache(
1122
 
                        node->table, node->index, FIL_NULL,
1123
 
                        trx_is_strict(trx)
1124
 
                        || dict_table_get_format(node->table)
1125
 
                        >= DICT_TF_FORMAT_ZIP);
 
1081
                err = dict_index_add_to_cache(node->table, node->index,
 
1082
                                              FIL_NULL, trx_is_strict(trx));
1126
1083
 
1127
1084
                node->index = dict_index_get_if_in_cache_low(index_id);
1128
1085
                ut_a(!node->index == (err != DB_SUCCESS));
1183
1140
        return(thr);
1184
1141
}
1185
1142
 
1186
 
/****************************************************************//**
 
1143
#ifndef UNIV_HOTBACKUP
 
1144
/********************************************************************
1187
1145
Creates the foreign key constraints system tables inside InnoDB
1188
1146
at database creation or database start if they are not found or are
1189
 
not of the right form.
1190
 
@return DB_SUCCESS or error code */
 
1147
not of the right form. */
1191
1148
UNIV_INTERN
1192
1149
ulint
1193
1150
dict_create_or_check_foreign_constraint_tables(void)
1194
1151
/*================================================*/
 
1152
                                /* out: DB_SUCCESS or error code */
1195
1153
{
1196
1154
        dict_table_t*   table1;
1197
1155
        dict_table_t*   table2;
1250
1208
        VARBINARY, like in other InnoDB system tables, to get a clean
1251
1209
        design. */
1252
1210
 
1253
 
        pars_info_t *info = pars_info_create();
1254
 
        error = que_eval_sql(info,
 
1211
        error = que_eval_sql(NULL,
1255
1212
                             "PROCEDURE CREATE_FOREIGN_SYS_TABLES_PROC () IS\n"
1256
1213
                             "BEGIN\n"
1257
1214
                             "CREATE TABLE\n"
1305
1262
        return(error);
1306
1263
}
1307
1264
 
1308
 
/****************************************************************//**
1309
 
Evaluate the given foreign key SQL statement.
1310
 
@return error code or DB_SUCCESS */
 
1265
/********************************************************************
 
1266
Evaluate the given foreign key SQL statement. */
1311
1267
static
1312
1268
ulint
1313
1269
dict_foreign_eval_sql(
1314
1270
/*==================*/
1315
 
        pars_info_t*    info,   /*!< in: info struct, or NULL */
1316
 
        const char*     sql,    /*!< in: SQL string to evaluate */
1317
 
        dict_table_t*   table,  /*!< in: table */
1318
 
        dict_foreign_t* foreign,/*!< in: foreign */
1319
 
        trx_t*          trx)    /*!< in: transaction */
 
1271
                                /* out: error code or DB_SUCCESS */
 
1272
        pars_info_t*    info,   /* in: info struct, or NULL */
 
1273
        const char*     sql,    /* in: SQL string to evaluate */
 
1274
        dict_table_t*   table,  /* in: table */
 
1275
        dict_foreign_t* foreign,/* in: foreign */
 
1276
        trx_t*          trx)    /* in: transaction */
1320
1277
{
1321
1278
        ulint           error;
1322
1279
        FILE*           ef      = dict_foreign_err_file;
1370
1327
        return(DB_SUCCESS);
1371
1328
}
1372
1329
 
1373
 
/********************************************************************//**
 
1330
/************************************************************************
1374
1331
Add a single foreign key field definition to the data dictionary tables in
1375
 
the database.
1376
 
@return error code or DB_SUCCESS */
 
1332
the database.  */
1377
1333
static
1378
1334
ulint
1379
1335
dict_create_add_foreign_field_to_dictionary(
1380
1336
/*========================================*/
1381
 
        ulint           field_nr,       /*!< in: foreign field number */
1382
 
        dict_table_t*   table,          /*!< in: table */
1383
 
        dict_foreign_t* foreign,        /*!< in: foreign */
1384
 
        trx_t*          trx)            /*!< in: transaction */
 
1337
                                        /* out: error code or DB_SUCCESS */
 
1338
        ulint           field_nr,       /* in: foreign field number */
 
1339
        dict_table_t*   table,          /* in: table */
 
1340
        dict_foreign_t* foreign,        /* in: foreign */
 
1341
        trx_t*          trx)            /* in: transaction */
1385
1342
{
1386
1343
        pars_info_t*    info = pars_info_create();
1387
1344
 
1405
1362
                       table, foreign, trx));
1406
1363
}
1407
1364
 
1408
 
/********************************************************************//**
 
1365
/************************************************************************
1409
1366
Add a single foreign key definition to the data dictionary tables in the
1410
1367
database. We also generate names to constraints that were not named by the
1411
1368
user. A generated constraint has a name of the format
1412
1369
databasename/tablename_ibfk_<number>, where the numbers start from 1, and
1413
1370
are given locally for this table, that is, the number is not global, as in
1414
 
the old format constraints < 4.0.18 it used to be.
1415
 
@return error code or DB_SUCCESS */
 
1371
the old format constraints < 4.0.18 it used to be. */
1416
1372
static
1417
1373
ulint
1418
1374
dict_create_add_foreign_to_dictionary(
1419
1375
/*==================================*/
1420
 
        ulint*          id_nr,  /*!< in/out: number to use in id generation;
 
1376
                                /* out: error code or DB_SUCCESS */
 
1377
        ulint*          id_nr,  /* in/out: number to use in id generation;
1421
1378
                                incremented if used */
1422
 
        dict_table_t*   table,  /*!< in: table */
1423
 
        dict_foreign_t* foreign,/*!< in: foreign */
1424
 
        trx_t*          trx)    /*!< in: transaction */
 
1379
        dict_table_t*   table,  /* in: table */
 
1380
        dict_foreign_t* foreign,/* in: foreign */
 
1381
        trx_t*          trx)    /* in: transaction */
1425
1382
{
1426
1383
        ulint           error;
1427
1384
        ulint           i;
1431
1388
        if (foreign->id == NULL) {
1432
1389
                /* Generate a new constraint id */
1433
1390
                ulint   namelen = strlen(table->name);
1434
 
                char*   id      = static_cast<char *>(mem_heap_alloc(foreign->heap, namelen + 20));
 
1391
                char*   id      = mem_heap_alloc(foreign->heap, namelen + 20);
1435
1392
                /* no overflow if number < 1e13 */
1436
1393
                sprintf(id, "%s_ibfk_%lu", table->name, (ulong) (*id_nr)++);
1437
1394
                foreign->id = id;
1480
1437
        return(error);
1481
1438
}
1482
1439
 
1483
 
/********************************************************************//**
1484
 
Adds foreign key definitions to data dictionary tables in the database.
1485
 
@return error code or DB_SUCCESS */
 
1440
/************************************************************************
 
1441
Adds foreign key definitions to data dictionary tables in the database. */
1486
1442
UNIV_INTERN
1487
1443
ulint
1488
1444
dict_create_add_foreigns_to_dictionary(
1489
1445
/*===================================*/
1490
 
        ulint           start_id,/*!< in: if we are actually doing ALTER TABLE
 
1446
                                /* out: error code or DB_SUCCESS */
 
1447
        ulint           start_id,/* in: if we are actually doing ALTER TABLE
1491
1448
                                ADD CONSTRAINT, we want to generate constraint
1492
1449
                                numbers which are bigger than in the table so
1493
1450
                                far; we number the constraints from
1495
1452
                                we are creating a new table, or if the table
1496
1453
                                so far has no constraints for which the name
1497
1454
                                was generated here */
1498
 
        dict_table_t*   table,  /*!< in: table */
1499
 
        trx_t*          trx)    /*!< in: transaction */
 
1455
        dict_table_t*   table,  /* in: table */
 
1456
        trx_t*          trx)    /* in: transaction */
1500
1457
{
1501
1458
        dict_foreign_t* foreign;
1502
1459
        ulint           number  = start_id + 1;
1527
1484
 
1528
1485
        return(DB_SUCCESS);
1529
1486
}
 
1487
#endif /* !UNIV_HOTBACKUP */