~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**********************************************************************
 
2
Data dictionary memory object creation
 
3
 
 
4
(c) 1996 Innobase Oy
 
5
 
 
6
Created 1/8/1996 Heikki Tuuri
 
7
***********************************************************************/
 
8
 
 
9
#include "dict0mem.h"
 
10
 
 
11
#ifdef UNIV_NONINL
 
12
#include "dict0mem.ic"
 
13
#endif
 
14
 
 
15
#include "rem0rec.h"
 
16
#include "data0type.h"
 
17
#include "mach0data.h"
 
18
#include "dict0dict.h"
 
19
#include "que0que.h"
 
20
#include "pars0pars.h"
 
21
#include "lock0lock.h"
 
22
 
 
23
#define DICT_HEAP_SIZE          100     /* initial memory heap size when
 
24
                                        creating a table or index object */
 
25
 
 
26
/**************************************************************************
 
27
Creates a table memory object. */
 
28
 
 
29
dict_table_t*
 
30
dict_mem_table_create(
 
31
/*==================*/
 
32
                                /* out, own: table object */
 
33
        const char*     name,   /* in: table name */
 
34
        ulint           space,  /* in: space where the clustered index of
 
35
                                the table is placed; this parameter is
 
36
                                ignored if the table is made a member of
 
37
                                a cluster */
 
38
        ulint           n_cols, /* in: number of columns */
 
39
        ulint           flags)  /* in: table flags */
 
40
{
 
41
        dict_table_t*   table;
 
42
        mem_heap_t*     heap;
 
43
 
 
44
        ut_ad(name);
 
45
        ut_ad(!(flags & ~DICT_TF_COMPACT));
 
46
 
 
47
        heap = mem_heap_create(DICT_HEAP_SIZE);
 
48
 
 
49
        table = mem_heap_alloc(heap, sizeof(dict_table_t));
 
50
 
 
51
        table->heap = heap;
 
52
 
 
53
        table->flags = (unsigned int) flags;
 
54
        table->name = mem_heap_strdup(heap, name);
 
55
        table->dir_path_of_temp_table = NULL;
 
56
        table->space = (unsigned int) space;
 
57
        table->ibd_file_missing = FALSE;
 
58
        table->tablespace_discarded = FALSE;
 
59
        table->n_def = 0;
 
60
        table->n_cols = (unsigned int) (n_cols + DATA_N_SYS_COLS);
 
61
 
 
62
        table->n_mysql_handles_opened = 0;
 
63
        table->n_foreign_key_checks_running = 0;
 
64
 
 
65
        table->cached = FALSE;
 
66
 
 
67
        table->cols = mem_heap_alloc(heap, (n_cols + DATA_N_SYS_COLS)
 
68
                                     * sizeof(dict_col_t));
 
69
        table->col_names = NULL;
 
70
        UT_LIST_INIT(table->indexes);
 
71
 
 
72
        table->auto_inc_lock = mem_heap_alloc(heap, lock_get_size());
 
73
 
 
74
        table->query_cache_inv_trx_id = ut_dulint_zero;
 
75
 
 
76
        UT_LIST_INIT(table->locks);
 
77
        UT_LIST_INIT(table->foreign_list);
 
78
        UT_LIST_INIT(table->referenced_list);
 
79
 
 
80
#ifdef UNIV_DEBUG
 
81
        table->does_not_fit_in_memory = FALSE;
 
82
#endif /* UNIV_DEBUG */
 
83
 
 
84
        table->stat_initialized = FALSE;
 
85
 
 
86
        table->stat_modified_counter = 0;
 
87
 
 
88
        table->big_rows = 0;
 
89
 
 
90
        mutex_create(&table->autoinc_mutex, SYNC_DICT_AUTOINC_MUTEX);
 
91
 
 
92
        table->autoinc = 0;
 
93
 
 
94
        /* The number of transactions that are either waiting on the
 
95
        AUTOINC lock or have been granted the lock. */
 
96
        table->n_waiting_or_granted_auto_inc_locks = 0;
 
97
 
 
98
#ifdef UNIV_DEBUG
 
99
        table->magic_n = DICT_TABLE_MAGIC_N;
 
100
#endif /* UNIV_DEBUG */
 
101
        return(table);
 
102
}
 
103
 
 
104
/********************************************************************
 
105
Free a table memory object. */
 
106
 
 
107
void
 
108
dict_mem_table_free(
 
109
/*================*/
 
110
        dict_table_t*   table)          /* in: table */
 
111
{
 
112
        ut_ad(table);
 
113
        ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
 
114
 
 
115
        mutex_free(&(table->autoinc_mutex));
 
116
        mem_heap_free(table->heap);
 
117
}
 
118
 
 
119
/********************************************************************
 
120
Append 'name' to 'col_names' (@see dict_table_t::col_names). */
 
121
static
 
122
const char*
 
123
dict_add_col_name(
 
124
/*==============*/
 
125
                                        /* out: new column names array */
 
126
        const char*     col_names,      /* in: existing column names, or
 
127
                                        NULL */
 
128
        ulint           cols,           /* in: number of existing columns */
 
129
        const char*     name,           /* in: new column name */
 
130
        mem_heap_t*     heap)           /* in: heap */
 
131
{
 
132
        ulint   old_len;
 
133
        ulint   new_len;
 
134
        ulint   total_len;
 
135
        char*   res;
 
136
 
 
137
        ut_ad(!cols == !col_names);
 
138
 
 
139
        /* Find out length of existing array. */
 
140
        if (col_names) {
 
141
                const char*     s = col_names;
 
142
                ulint           i;
 
143
 
 
144
                for (i = 0; i < cols; i++) {
 
145
                        s += strlen(s) + 1;
 
146
                }
 
147
 
 
148
                old_len = s - col_names;
 
149
        } else {
 
150
                old_len = 0;
 
151
        }
 
152
 
 
153
        new_len = strlen(name) + 1;
 
154
        total_len = old_len + new_len;
 
155
 
 
156
        res = mem_heap_alloc(heap, total_len);
 
157
 
 
158
        if (old_len > 0) {
 
159
                memcpy(res, col_names, old_len);
 
160
        }
 
161
 
 
162
        memcpy(res + old_len, name, new_len);
 
163
 
 
164
        return(res);
 
165
}
 
166
 
 
167
/**************************************************************************
 
168
Adds a column definition to a table. */
 
169
 
 
170
void
 
171
dict_mem_table_add_col(
 
172
/*===================*/
 
173
        dict_table_t*   table,  /* in: table */
 
174
        mem_heap_t*     heap,   /* in: temporary memory heap, or NULL */
 
175
        const char*     name,   /* in: column name, or NULL */
 
176
        ulint           mtype,  /* in: main datatype */
 
177
        ulint           prtype, /* in: precise type */
 
178
        ulint           len)    /* in: precision */
 
179
{
 
180
        dict_col_t*     col;
 
181
        ulint           mbminlen;
 
182
        ulint           mbmaxlen;
 
183
        ulint           i;
 
184
 
 
185
        ut_ad(table);
 
186
        ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
 
187
        ut_ad(!heap == !name);
 
188
 
 
189
        i = table->n_def++;
 
190
 
 
191
        if (name) {
 
192
                if (UNIV_UNLIKELY(table->n_def == table->n_cols)) {
 
193
                        heap = table->heap;
 
194
                }
 
195
                if (UNIV_LIKELY(i) && UNIV_UNLIKELY(!table->col_names)) {
 
196
                        /* All preceding column names are empty. */
 
197
                        char* s = mem_heap_alloc(heap, table->n_def);
 
198
                        memset(s, 0, table->n_def);
 
199
                        table->col_names = s;
 
200
                }
 
201
 
 
202
                table->col_names = dict_add_col_name(table->col_names,
 
203
                                                     i, name, heap);
 
204
        }
 
205
 
 
206
        col = (dict_col_t*) dict_table_get_nth_col(table, i);
 
207
 
 
208
        col->ind = (unsigned int) i;
 
209
        col->ord_part = 0;
 
210
 
 
211
        col->mtype = (unsigned int) mtype;
 
212
        col->prtype = (unsigned int) prtype;
 
213
        col->len = (unsigned int) len;
 
214
 
 
215
        dtype_get_mblen(mtype, prtype, &mbminlen, &mbmaxlen);
 
216
 
 
217
        col->mbminlen = (unsigned int) mbminlen;
 
218
        col->mbmaxlen = (unsigned int) mbmaxlen;
 
219
}
 
220
 
 
221
/**************************************************************************
 
222
Creates an index memory object. */
 
223
 
 
224
dict_index_t*
 
225
dict_mem_index_create(
 
226
/*==================*/
 
227
                                        /* out, own: index object */
 
228
        const char*     table_name,     /* in: table name */
 
229
        const char*     index_name,     /* in: index name */
 
230
        ulint           space,          /* in: space where the index tree is
 
231
                                        placed, ignored if the index is of
 
232
                                        the clustered type */
 
233
        ulint           type,           /* in: DICT_UNIQUE,
 
234
                                        DICT_CLUSTERED, ... ORed */
 
235
        ulint           n_fields)       /* in: number of fields */
 
236
{
 
237
        dict_index_t*   index;
 
238
        mem_heap_t*     heap;
 
239
 
 
240
        ut_ad(table_name && index_name);
 
241
 
 
242
        heap = mem_heap_create(DICT_HEAP_SIZE);
 
243
        index = mem_heap_alloc(heap, sizeof(dict_index_t));
 
244
 
 
245
        index->heap = heap;
 
246
 
 
247
        index->type = type;
 
248
        index->space = (unsigned int) space;
 
249
        index->page = 0;
 
250
        index->name = mem_heap_strdup(heap, index_name);
 
251
        index->table_name = table_name;
 
252
        index->table = NULL;
 
253
        index->n_def = index->n_nullable = 0;
 
254
        index->n_fields = (unsigned int) n_fields;
 
255
        index->fields = mem_heap_alloc(heap, 1 + n_fields
 
256
                                       * sizeof(dict_field_t));
 
257
        /* The '1 +' above prevents allocation
 
258
        of an empty mem block */
 
259
        index->stat_n_diff_key_vals = NULL;
 
260
 
 
261
        index->cached = FALSE;
 
262
        memset(&index->lock, 0, sizeof index->lock);
 
263
#ifdef UNIV_DEBUG
 
264
        index->magic_n = DICT_INDEX_MAGIC_N;
 
265
#endif /* UNIV_DEBUG */
 
266
        return(index);
 
267
}
 
268
 
 
269
/**************************************************************************
 
270
Creates and initializes a foreign constraint memory object. */
 
271
 
 
272
dict_foreign_t*
 
273
dict_mem_foreign_create(void)
 
274
/*=========================*/
 
275
                                /* out, own: foreign constraint struct */
 
276
{
 
277
        dict_foreign_t* foreign;
 
278
        mem_heap_t*     heap;
 
279
 
 
280
        heap = mem_heap_create(100);
 
281
 
 
282
        foreign = mem_heap_alloc(heap, sizeof(dict_foreign_t));
 
283
 
 
284
        foreign->heap = heap;
 
285
 
 
286
        foreign->id = NULL;
 
287
 
 
288
        foreign->type = 0;
 
289
        foreign->foreign_table_name = NULL;
 
290
        foreign->foreign_table = NULL;
 
291
        foreign->foreign_col_names = NULL;
 
292
 
 
293
        foreign->referenced_table_name = NULL;
 
294
        foreign->referenced_table = NULL;
 
295
        foreign->referenced_col_names = NULL;
 
296
 
 
297
        foreign->n_fields = 0;
 
298
 
 
299
        foreign->foreign_index = NULL;
 
300
        foreign->referenced_index = NULL;
 
301
 
 
302
        return(foreign);
 
303
}
 
304
 
 
305
/**************************************************************************
 
306
Adds a field definition to an index. NOTE: does not take a copy
 
307
of the column name if the field is a column. The memory occupied
 
308
by the column name may be released only after publishing the index. */
 
309
 
 
310
void
 
311
dict_mem_index_add_field(
 
312
/*=====================*/
 
313
        dict_index_t*   index,          /* in: index */
 
314
        const char*     name,           /* in: column name */
 
315
        ulint           prefix_len)     /* in: 0 or the column prefix length
 
316
                                        in a MySQL index like
 
317
                                        INDEX (textcol(25)) */
 
318
{
 
319
        dict_field_t*   field;
 
320
 
 
321
        ut_ad(index);
 
322
        ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
 
323
 
 
324
        index->n_def++;
 
325
 
 
326
        field = dict_index_get_nth_field(index, index->n_def - 1);
 
327
 
 
328
        field->name = name;
 
329
        field->prefix_len = (unsigned int) prefix_len;
 
330
}
 
331
 
 
332
/**************************************************************************
 
333
Frees an index memory object. */
 
334
 
 
335
void
 
336
dict_mem_index_free(
 
337
/*================*/
 
338
        dict_index_t*   index)  /* in: index */
 
339
{
 
340
        ut_ad(index);
 
341
        ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
 
342
 
 
343
        mem_heap_free(index->heap);
 
344
}