~drizzle-trunk/drizzle/development

641.2.2 by Monty Taylor
InnoDB Plugin 1.0.3
1
/*****************************************************************************
2
1819.7.68 by Stewart Smith
Merge initial InnoDB+ import.
3
Copyright (c) 1996, 2010, Innobase Oy. All Rights Reserved.
641.2.2 by Monty Taylor
InnoDB Plugin 1.0.3
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
1802.10.2 by Monty Taylor
Update all of the copyright headers to include the correct address.
14
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
15
St, Fifth Floor, Boston, MA 02110-1301 USA
641.2.2 by Monty Taylor
InnoDB Plugin 1.0.3
16
17
*****************************************************************************/
18
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
19
/******************************************************************//**
20
@file dict/dict0mem.c
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
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"
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
36
#ifndef UNIV_HOTBACKUP
37
# include "lock0lock.h"
38
#endif /* !UNIV_HOTBACKUP */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
39
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
40
#define	DICT_HEAP_SIZE		100	/*!< initial memory heap size when
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
41
					creating a table or index object */
42
1819.7.68 by Stewart Smith
Merge initial InnoDB+ import.
43
#ifdef UNIV_PFS_MUTEX
44
/* Key to register autoinc_mutex with performance schema */
45
UNIV_INTERN mysql_pfs_key_t	autoinc_mutex_key;
46
#endif /* UNIV_PFS_MUTEX */
47
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
48
/**********************************************************************//**
49
Creates a table memory object.
50
@return	own: table object */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
51
UNIV_INTERN
52
dict_table_t*
53
dict_mem_table_create(
54
/*==================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
55
	const char*	name,	/*!< in: table name */
56
	ulint		space,	/*!< in: space where the clustered index of
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
57
				the table is placed; this parameter is
58
				ignored if the table is made a member of
59
				a cluster */
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
60
	ulint		n_cols,	/*!< in: number of columns */
61
	ulint		flags)	/*!< in: table flags */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
62
{
63
	dict_table_t*	table;
64
	mem_heap_t*	heap;
65
66
	ut_ad(name);
1819.5.129 by stewart at flamingspork
[patch 129/129] Merge patch for revision 1947 from InnoDB SVN:
67
	ut_a(!(flags & (~0 << DICT_TF2_BITS)));
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
68
69
	heap = mem_heap_create(DICT_HEAP_SIZE);
70
71
	table = mem_heap_zalloc(heap, sizeof(dict_table_t));
72
73
	table->heap = heap;
74
75
	table->flags = (unsigned int) flags;
1819.9.19 by Vasil Dimov
Merge Revision revid:vasil.dimov@oracle.com-20100622163043-dc0lxy0byg74viet from MySQL InnoDB
76
	table->name = ut_malloc(strlen(name) + 1);
77
	memcpy(table->name, name, strlen(name) + 1);
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
78
	table->space = (unsigned int) space;
79
	table->n_cols = (unsigned int) (n_cols + DATA_N_SYS_COLS);
80
81
	table->cols = mem_heap_alloc(heap, (n_cols + DATA_N_SYS_COLS)
82
				     * sizeof(dict_col_t));
83
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
84
#ifndef UNIV_HOTBACKUP
641.2.1 by Monty Taylor
InnoDB Plugin 1.0.2
85
	table->autoinc_lock = mem_heap_alloc(heap, lock_get_size());
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
86
1819.7.68 by Stewart Smith
Merge initial InnoDB+ import.
87
	mutex_create(autoinc_mutex_key,
88
		     &table->autoinc_mutex, SYNC_DICT_AUTOINC_MUTEX);
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
89
641.2.1 by Monty Taylor
InnoDB Plugin 1.0.2
90
	table->autoinc = 0;
91
92
	/* The number of transactions that are either waiting on the
93
	AUTOINC lock or have been granted the lock. */
94
	table->n_waiting_or_granted_auto_inc_locks = 0;
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
95
#endif /* !UNIV_HOTBACKUP */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
96
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
97
	ut_d(table->magic_n = DICT_TABLE_MAGIC_N);
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
98
	return(table);
99
}
100
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
101
/****************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
102
Free a table memory object. */
103
UNIV_INTERN
104
void
105
dict_mem_table_free(
106
/*================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
107
	dict_table_t*	table)		/*!< in: table */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
108
{
109
	ut_ad(table);
110
	ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
641.2.2 by Monty Taylor
InnoDB Plugin 1.0.3
111
	ut_d(table->cached = FALSE);
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
112
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
113
#ifndef UNIV_HOTBACKUP
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
114
	mutex_free(&(table->autoinc_mutex));
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
115
#endif /* UNIV_HOTBACKUP */
1819.9.19 by Vasil Dimov
Merge Revision revid:vasil.dimov@oracle.com-20100622163043-dc0lxy0byg74viet from MySQL InnoDB
116
	ut_free(table->name);
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
117
	mem_heap_free(table->heap);
118
}
119
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
120
/****************************************************************//**
121
Append 'name' to 'col_names'.  @see dict_table_t::col_names
122
@return	new column names array */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
123
static
124
const char*
125
dict_add_col_name(
126
/*==============*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
127
	const char*	col_names,	/*!< in: existing column names, or
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
128
					NULL */
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
129
	ulint		cols,		/*!< in: number of existing columns */
130
	const char*	name,		/*!< in: new column name */
131
	mem_heap_t*	heap)		/*!< in: heap */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
132
{
133
	ulint	old_len;
134
	ulint	new_len;
135
	ulint	total_len;
136
	char*	res;
137
138
	ut_ad(!cols == !col_names);
139
140
	/* Find out length of existing array. */
141
	if (col_names) {
142
		const char*	s = col_names;
143
		ulint		i;
144
145
		for (i = 0; i < cols; i++) {
146
			s += strlen(s) + 1;
147
		}
148
149
		old_len = s - col_names;
150
	} else {
151
		old_len = 0;
152
	}
153
154
	new_len = strlen(name) + 1;
155
	total_len = old_len + new_len;
156
157
	res = mem_heap_alloc(heap, total_len);
158
159
	if (old_len > 0) {
160
		memcpy(res, col_names, old_len);
161
	}
162
163
	memcpy(res + old_len, name, new_len);
164
165
	return(res);
166
}
167
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
168
/**********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
169
Adds a column definition to a table. */
170
UNIV_INTERN
171
void
172
dict_mem_table_add_col(
173
/*===================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
174
	dict_table_t*	table,	/*!< in: table */
175
	mem_heap_t*	heap,	/*!< in: temporary memory heap, or NULL */
176
	const char*	name,	/*!< in: column name, or NULL */
177
	ulint		mtype,	/*!< in: main datatype */
178
	ulint		prtype,	/*!< in: precise type */
179
	ulint		len)	/*!< in: precision */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
180
{
181
	dict_col_t*	col;
182
	ulint		i;
183
184
	ut_ad(table);
185
	ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
186
	ut_ad(!heap == !name);
187
188
	i = table->n_def++;
189
190
	if (name) {
191
		if (UNIV_UNLIKELY(table->n_def == table->n_cols)) {
192
			heap = table->heap;
193
		}
194
		if (UNIV_LIKELY(i) && UNIV_UNLIKELY(!table->col_names)) {
195
			/* All preceding column names are empty. */
196
			char* s = mem_heap_zalloc(heap, table->n_def);
197
			table->col_names = s;
198
		}
199
200
		table->col_names = dict_add_col_name(table->col_names,
201
						     i, name, heap);
202
	}
203
204
	col = dict_table_get_nth_col(table, i);
205
1819.7.143 by Jimmy Yang, Stewart Smith
Merge Revision revid:jimmy.yang@oracle.com-20100526014433-bx0t9794mnvkiaft from MySQL InnoDB
206
	dict_mem_fill_column_struct(col, i, mtype, prtype, len);
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
207
}
208
1819.9.36 by Marko Mäkelä, Stewart Smith
Merge Revision revid:marko.makela@oracle.com-20100629113248-fvl48lnzr44z94gg from MySQL InnoDB
209
210
/**********************************************************************//**
211
This function populates a dict_col_t memory structure with
212
supplied information. */
213
UNIV_INTERN
214
void
215
dict_mem_fill_column_struct(
216
/*========================*/
217
	dict_col_t*	column,		/*!< out: column struct to be
218
					filled */
219
	ulint		col_pos,	/*!< in: column position */
220
	ulint		mtype,		/*!< in: main data type */
221
	ulint		prtype,		/*!< in: precise type */
222
	ulint		col_len)	/*!< in: column length */
223
{
224
#ifndef UNIV_HOTBACKUP
225
	ulint	mbminlen;
226
	ulint	mbmaxlen;
227
#endif /* !UNIV_HOTBACKUP */
228
229
	column->ind = (unsigned int) col_pos;
230
	column->ord_part = 0;
231
	column->mtype = (unsigned int) mtype;
232
	column->prtype = (unsigned int) prtype;
233
	column->len = (unsigned int) col_len;
234
#ifndef UNIV_HOTBACKUP
235
        dtype_get_mblen(mtype, prtype, &mbminlen, &mbmaxlen);
236
	dict_col_set_mbminmaxlen(column, mbminlen, mbmaxlen);
237
#endif /* !UNIV_HOTBACKUP */
238
}
239
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
240
/**********************************************************************//**
241
Creates an index memory object.
242
@return	own: index object */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
243
UNIV_INTERN
244
dict_index_t*
245
dict_mem_index_create(
246
/*==================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
247
	const char*	table_name,	/*!< in: table name */
248
	const char*	index_name,	/*!< in: index name */
249
	ulint		space,		/*!< in: space where the index tree is
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
250
					placed, ignored if the index is of
251
					the clustered type */
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
252
	ulint		type,		/*!< in: DICT_UNIQUE,
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
253
					DICT_CLUSTERED, ... ORed */
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
254
	ulint		n_fields)	/*!< in: number of fields */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
255
{
256
	dict_index_t*	index;
257
	mem_heap_t*	heap;
258
259
	ut_ad(table_name && index_name);
260
261
	heap = mem_heap_create(DICT_HEAP_SIZE);
262
	index = mem_heap_zalloc(heap, sizeof(dict_index_t));
263
1819.7.143 by Jimmy Yang, Stewart Smith
Merge Revision revid:jimmy.yang@oracle.com-20100526014433-bx0t9794mnvkiaft from MySQL InnoDB
264
	dict_mem_fill_index_struct(index, heap, table_name, index_name,
265
				   space, type, n_fields);
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
266
267
	return(index);
268
}
269
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
270
/**********************************************************************//**
271
Creates and initializes a foreign constraint memory object.
272
@return	own: foreign constraint struct */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
273
UNIV_INTERN
274
dict_foreign_t*
275
dict_mem_foreign_create(void)
276
/*=========================*/
277
{
278
	dict_foreign_t*	foreign;
279
	mem_heap_t*	heap;
280
281
	heap = mem_heap_create(100);
282
283
	foreign = mem_heap_zalloc(heap, sizeof(dict_foreign_t));
284
285
	foreign->heap = heap;
286
287
	return(foreign);
288
}
289
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
290
/**********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
291
Adds a field definition to an index. NOTE: does not take a copy
292
of the column name if the field is a column. The memory occupied
293
by the column name may be released only after publishing the index. */
294
UNIV_INTERN
295
void
296
dict_mem_index_add_field(
297
/*=====================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
298
	dict_index_t*	index,		/*!< in: index */
299
	const char*	name,		/*!< in: column name */
300
	ulint		prefix_len)	/*!< in: 0 or the column prefix length
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
301
					in a MySQL index like
302
					INDEX (textcol(25)) */
303
{
304
	dict_field_t*	field;
305
306
	ut_ad(index);
307
	ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
308
309
	index->n_def++;
310
311
	field = dict_index_get_nth_field(index, index->n_def - 1);
312
313
	field->name = name;
314
	field->prefix_len = (unsigned int) prefix_len;
315
}
316
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
317
/**********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
318
Frees an index memory object. */
319
UNIV_INTERN
320
void
321
dict_mem_index_free(
322
/*================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
323
	dict_index_t*	index)	/*!< in: index */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
324
{
325
	ut_ad(index);
326
	ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
327
328
	mem_heap_free(index->heap);
329
}