~drizzle-trunk/drizzle/development

1 by brian
clean slate
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. */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
28
UNIV_INTERN
1 by brian
clean slate
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);
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
45
	ut_a(!(flags & (~0 << DICT_TF_BITS)));
1 by brian
clean slate
46
47
	heap = mem_heap_create(DICT_HEAP_SIZE);
48
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
49
	table = mem_heap_zalloc(heap, sizeof(dict_table_t));
1 by brian
clean slate
50
51
	table->heap = heap;
52
53
	table->flags = (unsigned int) flags;
54
	table->name = mem_heap_strdup(heap, name);
55
	table->space = (unsigned int) space;
56
	table->n_cols = (unsigned int) (n_cols + DATA_N_SYS_COLS);
57
58
	table->cols = mem_heap_alloc(heap, (n_cols + DATA_N_SYS_COLS)
59
				     * sizeof(dict_col_t));
60
61
	table->auto_inc_lock = mem_heap_alloc(heap, lock_get_size());
62
63
	mutex_create(&table->autoinc_mutex, SYNC_DICT_AUTOINC_MUTEX);
64
65
	/* The actual increment value will be set by MySQL, we simply
66
	default to 1 here.*/
67
	table->autoinc_increment = 1;
68
69
#ifdef UNIV_DEBUG
70
	table->magic_n = DICT_TABLE_MAGIC_N;
71
#endif /* UNIV_DEBUG */
72
	return(table);
73
}
74
75
/********************************************************************
76
Free a table memory object. */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
77
UNIV_INTERN
1 by brian
clean slate
78
void
79
dict_mem_table_free(
80
/*================*/
81
	dict_table_t*	table)		/* in: table */
82
{
83
	ut_ad(table);
84
	ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
85
86
	mutex_free(&(table->autoinc_mutex));
87
	mem_heap_free(table->heap);
88
}
89
90
/********************************************************************
91
Append 'name' to 'col_names' (@see dict_table_t::col_names). */
92
static
93
const char*
94
dict_add_col_name(
95
/*==============*/
96
					/* out: new column names array */
97
	const char*	col_names,	/* in: existing column names, or
98
					NULL */
99
	ulint		cols,		/* in: number of existing columns */
100
	const char*	name,		/* in: new column name */
101
	mem_heap_t*	heap)		/* in: heap */
102
{
103
	ulint	old_len;
104
	ulint	new_len;
105
	ulint	total_len;
106
	char*	res;
107
108
	ut_ad(!cols == !col_names);
109
110
	/* Find out length of existing array. */
111
	if (col_names) {
112
		const char*	s = col_names;
113
		ulint		i;
114
115
		for (i = 0; i < cols; i++) {
116
			s += strlen(s) + 1;
117
		}
118
119
		old_len = s - col_names;
120
	} else {
121
		old_len = 0;
122
	}
123
124
	new_len = strlen(name) + 1;
125
	total_len = old_len + new_len;
126
127
	res = mem_heap_alloc(heap, total_len);
128
129
	if (old_len > 0) {
130
		memcpy(res, col_names, old_len);
131
	}
132
133
	memcpy(res + old_len, name, new_len);
134
135
	return(res);
136
}
137
138
/**************************************************************************
139
Adds a column definition to a table. */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
140
UNIV_INTERN
1 by brian
clean slate
141
void
142
dict_mem_table_add_col(
143
/*===================*/
144
	dict_table_t*	table,	/* in: table */
145
	mem_heap_t*	heap,	/* in: temporary memory heap, or NULL */
146
	const char*	name,	/* in: column name, or NULL */
147
	ulint		mtype,	/* in: main datatype */
148
	ulint		prtype,	/* in: precise type */
149
	ulint		len)	/* in: precision */
150
{
151
	dict_col_t*	col;
152
	ulint		mbminlen;
153
	ulint		mbmaxlen;
154
	ulint		i;
155
156
	ut_ad(table);
157
	ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
158
	ut_ad(!heap == !name);
159
160
	i = table->n_def++;
161
162
	if (name) {
163
		if (UNIV_UNLIKELY(table->n_def == table->n_cols)) {
164
			heap = table->heap;
165
		}
166
		if (UNIV_LIKELY(i) && UNIV_UNLIKELY(!table->col_names)) {
167
			/* All preceding column names are empty. */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
168
			char* s = mem_heap_zalloc(heap, table->n_def);
1 by brian
clean slate
169
			table->col_names = s;
170
		}
171
172
		table->col_names = dict_add_col_name(table->col_names,
173
						     i, name, heap);
174
	}
175
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
176
	col = dict_table_get_nth_col(table, i);
1 by brian
clean slate
177
178
	col->ind = (unsigned int) i;
179
	col->ord_part = 0;
180
181
	col->mtype = (unsigned int) mtype;
182
	col->prtype = (unsigned int) prtype;
183
	col->len = (unsigned int) len;
184
185
	dtype_get_mblen(mtype, prtype, &mbminlen, &mbmaxlen);
186
187
	col->mbminlen = (unsigned int) mbminlen;
188
	col->mbmaxlen = (unsigned int) mbmaxlen;
189
}
190
191
/**************************************************************************
192
Creates an index memory object. */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
193
UNIV_INTERN
1 by brian
clean slate
194
dict_index_t*
195
dict_mem_index_create(
196
/*==================*/
197
					/* out, own: index object */
198
	const char*	table_name,	/* in: table name */
199
	const char*	index_name,	/* in: index name */
200
	ulint		space,		/* in: space where the index tree is
201
					placed, ignored if the index is of
202
					the clustered type */
203
	ulint		type,		/* in: DICT_UNIQUE,
204
					DICT_CLUSTERED, ... ORed */
205
	ulint		n_fields)	/* in: number of fields */
206
{
207
	dict_index_t*	index;
208
	mem_heap_t*	heap;
209
210
	ut_ad(table_name && index_name);
211
212
	heap = mem_heap_create(DICT_HEAP_SIZE);
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
213
	index = mem_heap_zalloc(heap, sizeof(dict_index_t));
1 by brian
clean slate
214
215
	index->heap = heap;
216
217
	index->type = type;
218
	index->space = (unsigned int) space;
219
	index->name = mem_heap_strdup(heap, index_name);
220
	index->table_name = table_name;
221
	index->n_fields = (unsigned int) n_fields;
222
	index->fields = mem_heap_alloc(heap, 1 + n_fields
223
				       * sizeof(dict_field_t));
224
	/* The '1 +' above prevents allocation
225
	of an empty mem block */
226
#ifdef UNIV_DEBUG
227
	index->magic_n = DICT_INDEX_MAGIC_N;
228
#endif /* UNIV_DEBUG */
229
	return(index);
230
}
231
232
/**************************************************************************
233
Creates and initializes a foreign constraint memory object. */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
234
UNIV_INTERN
1 by brian
clean slate
235
dict_foreign_t*
236
dict_mem_foreign_create(void)
237
/*=========================*/
238
				/* out, own: foreign constraint struct */
239
{
240
	dict_foreign_t*	foreign;
241
	mem_heap_t*	heap;
242
243
	heap = mem_heap_create(100);
244
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
245
	foreign = mem_heap_zalloc(heap, sizeof(dict_foreign_t));
1 by brian
clean slate
246
247
	foreign->heap = heap;
248
249
	return(foreign);
250
}
251
252
/**************************************************************************
253
Adds a field definition to an index. NOTE: does not take a copy
254
of the column name if the field is a column. The memory occupied
255
by the column name may be released only after publishing the index. */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
256
UNIV_INTERN
1 by brian
clean slate
257
void
258
dict_mem_index_add_field(
259
/*=====================*/
260
	dict_index_t*	index,		/* in: index */
261
	const char*	name,		/* in: column name */
262
	ulint		prefix_len)	/* in: 0 or the column prefix length
263
					in a MySQL index like
264
					INDEX (textcol(25)) */
265
{
266
	dict_field_t*	field;
267
268
	ut_ad(index);
269
	ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
270
271
	index->n_def++;
272
273
	field = dict_index_get_nth_field(index, index->n_def - 1);
274
275
	field->name = name;
276
	field->prefix_len = (unsigned int) prefix_len;
277
}
278
279
/**************************************************************************
280
Frees an index memory object. */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
281
UNIV_INTERN
1 by brian
clean slate
282
void
283
dict_mem_index_free(
284
/*================*/
285
	dict_index_t*	index)	/* in: index */
286
{
287
	ut_ad(index);
288
	ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
289
290
	mem_heap_free(index->heap);
291
}