~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-03-18 12:12:31 UTC
  • Revision ID: james.westby@ubuntu.com-20100318121231-k6g1xe6cshbwa0f8
Tags: upstream-2010.03.1347
ImportĀ upstreamĀ versionĀ 2010.03.1347

Show diffs side-by-side

added added

removed removed

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