~drizzle-trunk/drizzle/development

641.2.2 by Monty Taylor
InnoDB Plugin 1.0.3
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
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
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
43
/**********************************************************************//**
44
Creates a table memory object.
45
@return	own: table object */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
46
UNIV_INTERN
47
dict_table_t*
48
dict_mem_table_create(
49
/*==================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
50
	const char*	name,	/*!< in: table name */
51
	ulint		space,	/*!< in: space where the clustered index of
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
52
				the table is placed; this parameter is
53
				ignored if the table is made a member of
54
				a cluster */
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
55
	ulint		n_cols,	/*!< in: number of columns */
56
	ulint		flags)	/*!< in: table flags */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
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
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
78
#ifndef UNIV_HOTBACKUP
641.2.1 by Monty Taylor
InnoDB Plugin 1.0.2
79
	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.
80
81
	mutex_create(&table->autoinc_mutex, SYNC_DICT_AUTOINC_MUTEX);
82
641.2.1 by Monty Taylor
InnoDB Plugin 1.0.2
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;
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
88
#endif /* !UNIV_HOTBACKUP */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
89
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
90
	ut_d(table->magic_n = DICT_TABLE_MAGIC_N);
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
91
	return(table);
92
}
93
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
94
/****************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
95
Free a table memory object. */
96
UNIV_INTERN
97
void
98
dict_mem_table_free(
99
/*================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
100
	dict_table_t*	table)		/*!< in: table */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
101
{
102
	ut_ad(table);
103
	ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
641.2.2 by Monty Taylor
InnoDB Plugin 1.0.3
104
	ut_d(table->cached = FALSE);
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
105
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
106
#ifndef UNIV_HOTBACKUP
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
107
	mutex_free(&(table->autoinc_mutex));
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
108
#endif /* UNIV_HOTBACKUP */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
109
	mem_heap_free(table->heap);
110
}
111
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
112
/****************************************************************//**
113
Append 'name' to 'col_names'.  @see dict_table_t::col_names
114
@return	new column names array */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
115
static
116
const char*
117
dict_add_col_name(
118
/*==============*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
119
	const char*	col_names,	/*!< in: existing column names, or
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
120
					NULL */
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
121
	ulint		cols,		/*!< in: number of existing columns */
122
	const char*	name,		/*!< in: new column name */
123
	mem_heap_t*	heap)		/*!< in: heap */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
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
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
160
/**********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
161
Adds a column definition to a table. */
162
UNIV_INTERN
163
void
164
dict_mem_table_add_col(
165
/*===================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
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 */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
172
{
173
	dict_col_t*	col;
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
174
#ifndef UNIV_HOTBACKUP
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
175
	ulint		mbminlen;
176
	ulint		mbmaxlen;
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
177
#endif /* !UNIV_HOTBACKUP */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
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
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
209
#ifndef UNIV_HOTBACKUP
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
210
	dtype_get_mblen(mtype, prtype, &mbminlen, &mbmaxlen);
211
212
	col->mbminlen = (unsigned int) mbminlen;
213
	col->mbmaxlen = (unsigned int) mbmaxlen;
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
214
#endif /* !UNIV_HOTBACKUP */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
215
}
216
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
217
/**********************************************************************//**
218
Creates an index memory object.
219
@return	own: index object */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
220
UNIV_INTERN
221
dict_index_t*
222
dict_mem_index_create(
223
/*==================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
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
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
227
					placed, ignored if the index is of
228
					the clustered type */
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
229
	ulint		type,		/*!< in: DICT_UNIQUE,
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
230
					DICT_CLUSTERED, ... ORed */
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
231
	ulint		n_fields)	/*!< in: number of fields */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
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;
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
244
#ifndef UNIV_HOTBACKUP
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
245
	index->space = (unsigned int) space;
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
246
#endif /* !UNIV_HOTBACKUP */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
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
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
260
/**********************************************************************//**
261
Creates and initializes a foreign constraint memory object.
262
@return	own: foreign constraint struct */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
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
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
280
/**********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
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
/*=====================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
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
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
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
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
307
/**********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
308
Frees an index memory object. */
309
UNIV_INTERN
310
void
311
dict_mem_index_free(
312
/*================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
313
	dict_index_t*	index)	/*!< in: index */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
314
{
315
	ut_ad(index);
316
	ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
317
318
	mem_heap_free(index->heap);
319
}