Drizzled Public API Documentation

dict0mem.cc
1 /*****************************************************************************
2 
3 Copyright (C) 1996, 2010, 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., 51 Franklin
15 St, Fifth Floor, Boston, MA 02110-1301 USA
16 
17 *****************************************************************************/
18 
19 /******************************************************************/
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 #include "srv0srv.h" /* srv_lower_case_table_names */
37 #include "ha_prototypes.h" /* innobase_casedn_str()*/
38 #ifndef UNIV_HOTBACKUP
39 # include "lock0lock.h"
40 #endif /* !UNIV_HOTBACKUP */
41 #ifdef UNIV_BLOB_DEBUG
42 # include "ut0rbt.h"
43 #endif /* UNIV_BLOB_DEBUG */
44 
45 #define DICT_HEAP_SIZE 100
48 #ifdef UNIV_PFS_MUTEX
49 /* Key to register autoinc_mutex with performance schema */
50 UNIV_INTERN mysql_pfs_key_t autoinc_mutex_key;
51 #endif /* UNIV_PFS_MUTEX */
52 
53 /**********************************************************************/
56 UNIV_INTERN
59 /*==================*/
60  const char* name,
61  ulint space,
65  ulint n_cols,
66  ulint flags)
67 {
68  dict_table_t* table;
69  mem_heap_t* heap;
70 
71  ut_ad(name);
72  ut_a(!(flags & (SIZE_MAX << DICT_TF2_BITS)));
73 
74  heap = mem_heap_create(DICT_HEAP_SIZE);
75 
76  table = static_cast<dict_table_struct *>(mem_heap_zalloc(heap, sizeof(dict_table_t)));
77 
78  table->heap = heap;
79 
80  table->flags = (unsigned int) flags;
81  table->name = static_cast<char *>(ut_malloc(strlen(name) + 1));
82  memcpy(table->name, name, strlen(name) + 1);
83  table->space = (unsigned int) space;
84  table->n_cols = (unsigned int) (n_cols + DATA_N_SYS_COLS);
85 
86  table->cols = static_cast<dict_col_t *>(mem_heap_alloc(heap, (n_cols + DATA_N_SYS_COLS)
87  * sizeof(dict_col_t)));
88 
89 #ifndef UNIV_HOTBACKUP
90  table->autoinc_lock = static_cast<lock_t *>(mem_heap_alloc(heap, lock_get_size()));
91 
92  mutex_create(autoinc_mutex_key,
93  &table->autoinc_mutex, SYNC_DICT_AUTOINC_MUTEX);
94 
95  table->autoinc = 0;
96 
97  /* The number of transactions that are either waiting on the
98  AUTOINC lock or have been granted the lock. */
100 #endif /* !UNIV_HOTBACKUP */
101 
102  ut_d(table->magic_n = DICT_TABLE_MAGIC_N);
103  return(table);
104 }
105 
106 /****************************************************************/
108 UNIV_INTERN
109 void
111 /*================*/
112  dict_table_t* table)
113 {
114  ut_ad(table);
115  ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
116  ut_d(table->cached = FALSE);
117 
118 #ifndef UNIV_HOTBACKUP
119  mutex_free(&(table->autoinc_mutex));
120 #endif /* UNIV_HOTBACKUP */
121  ut_free(table->name);
122  mem_heap_free(table->heap);
123 }
124 
125 /****************************************************************/
128 static
129 const char*
130 dict_add_col_name(
131 /*==============*/
132  const char* col_names,
134  ulint cols,
135  const char* name,
136  mem_heap_t* heap)
137 {
138  ulint old_len;
139  ulint new_len;
140  ulint total_len;
141  char* res;
142 
143  ut_ad(!cols == !col_names);
144 
145  /* Find out length of existing array. */
146  if (col_names) {
147  const char* s = col_names;
148  ulint i;
149 
150  for (i = 0; i < cols; i++) {
151  s += strlen(s) + 1;
152  }
153 
154  old_len = s - col_names;
155  } else {
156  old_len = 0;
157  }
158 
159  new_len = strlen(name) + 1;
160  total_len = old_len + new_len;
161 
162  res = static_cast<char *>(mem_heap_alloc(heap, total_len));
163 
164  if (old_len > 0) {
165  memcpy(res, col_names, old_len);
166  }
167 
168  memcpy(res + old_len, name, new_len);
169 
170  return(res);
171 }
172 
173 /**********************************************************************/
175 UNIV_INTERN
176 void
178 /*===================*/
179  dict_table_t* table,
180  mem_heap_t* heap,
181  const char* name,
182  ulint mtype,
183  ulint prtype,
184  ulint len)
185 {
186  dict_col_t* col;
187  ulint i;
188 
189  ut_ad(table);
190  ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
191  ut_ad(!heap == !name);
192 
193  i = table->n_def++;
194 
195  if (name) {
196  if (UNIV_UNLIKELY(table->n_def == table->n_cols)) {
197  heap = table->heap;
198  }
199  if (UNIV_LIKELY(i) && UNIV_UNLIKELY(!table->col_names)) {
200  /* All preceding column names are empty. */
201  char* s = static_cast<char *>(mem_heap_zalloc(heap, table->n_def));
202  table->col_names = s;
203  }
204 
205  table->col_names = dict_add_col_name(table->col_names,
206  i, name, heap);
207  }
208 
209  col = dict_table_get_nth_col(table, i);
210 
211  dict_mem_fill_column_struct(col, i, mtype, prtype, len);
212 }
213 
214 
215 /**********************************************************************/
218 UNIV_INTERN
219 void
221 /*========================*/
222  dict_col_t* column,
224  ulint col_pos,
225  ulint mtype,
226  ulint prtype,
227  ulint col_len)
228 {
229 #ifndef UNIV_HOTBACKUP
230  ulint mbminlen;
231  ulint mbmaxlen;
232 #endif /* !UNIV_HOTBACKUP */
233 
234  column->ind = (unsigned int) col_pos;
235  column->ord_part = 0;
236  column->mtype = (unsigned int) mtype;
237  column->prtype = (unsigned int) prtype;
238  column->len = (unsigned int) col_len;
239 #ifndef UNIV_HOTBACKUP
240  dtype_get_mblen(mtype, prtype, &mbminlen, &mbmaxlen);
241  dict_col_set_mbminmaxlen(column, mbminlen, mbmaxlen);
242 #endif /* !UNIV_HOTBACKUP */
243 }
244 
245 /**********************************************************************/
248 UNIV_INTERN
251 /*==================*/
252  const char* table_name,
253  const char* index_name,
254  ulint space,
257  ulint type,
259  ulint n_fields)
260 {
261  dict_index_t* index;
262  mem_heap_t* heap;
263 
264  ut_ad(table_name && index_name);
265 
266  heap = mem_heap_create(DICT_HEAP_SIZE);
267  index = static_cast<dict_index_t *>(mem_heap_zalloc(heap, sizeof(dict_index_t)));
268 
269  dict_mem_fill_index_struct(index, heap, table_name, index_name,
270  space, type, n_fields);
271 
272  return(index);
273 }
274 
275 /**********************************************************************/
278 UNIV_INTERN
281 /*=========================*/
282 {
283  dict_foreign_t* foreign;
284  mem_heap_t* heap;
285 
286  heap = mem_heap_create(100);
287 
288  foreign = static_cast<dict_foreign_t *>(mem_heap_zalloc(heap, sizeof(dict_foreign_t)));
289 
290  foreign->heap = heap;
291 
292  return(foreign);
293 }
294 
295 /**********************************************************************/
300 UNIV_INTERN
301 void
303 /*===================================*/
304  dict_foreign_t* foreign,
305  ibool do_alloc)
306 {
307  if (srv_lower_case_table_names == 2) {
308  if (do_alloc) {
309  foreign->foreign_table_name_lookup = static_cast<char *>(mem_heap_alloc(
310  foreign->heap,
311  strlen(foreign->foreign_table_name) + 1));
312  }
313  strcpy(foreign->foreign_table_name_lookup,
314  foreign->foreign_table_name);
316  } else {
318  = foreign->foreign_table_name;
319  }
320 }
321 
322 /**********************************************************************/
327 UNIV_INTERN
328 void
330 /*======================================*/
331  dict_foreign_t* foreign,
332  ibool do_alloc)
333 {
334  if (srv_lower_case_table_names == 2) {
335  if (do_alloc) {
336  foreign->referenced_table_name_lookup = static_cast<char *>(mem_heap_alloc(
337  foreign->heap,
338  strlen(foreign->referenced_table_name) + 1));
339  }
340  strcpy(foreign->referenced_table_name_lookup,
341  foreign->referenced_table_name);
343  } else {
345  = foreign->referenced_table_name;
346  }
347 }
348 
349 /**********************************************************************/
353 UNIV_INTERN
354 void
356 /*=====================*/
357  dict_index_t* index,
358  const char* name,
359  ulint prefix_len)
362 {
363  dict_field_t* field;
364 
365  ut_ad(index);
366  ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
367 
368  index->n_def++;
369 
370  field = dict_index_get_nth_field(index, index->n_def - 1);
371 
372  field->name = name;
373  field->prefix_len = (unsigned int) prefix_len;
374 }
375 
376 /**********************************************************************/
378 UNIV_INTERN
379 void
381 /*================*/
382  dict_index_t* index)
383 {
384  ut_ad(index);
385  ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
386 #ifdef UNIV_BLOB_DEBUG
387  if (index->blobs) {
388  mutex_free(&index->blobs_mutex);
389  rbt_free(index->blobs);
390  }
391 #endif /* UNIV_BLOB_DEBUG */
392 
393  mem_heap_free(index->heap);
394 }
dict_col_t * cols
Definition: dict0mem.h:506
UNIV_INTERN void dict_mem_index_add_field(dict_index_t *index, const char *name, ulint prefix_len)
Definition: dict0mem.cc:356
char * foreign_table_name_lookup
Definition: dict0mem.h:436
unsigned n_def
Definition: dict0mem.h:502
char * foreign_table_name
Definition: dict0mem.h:435
UNIV_INLINE void dict_mem_fill_index_struct(dict_index_t *index, mem_heap_t *heap, const char *table_name, const char *index_name, ulint space, ulint type, ulint n_fields)
char * referenced_table_name_lookup
Definition: dict0mem.h:442
UNIV_INLINE void dtype_get_mblen(ulint mtype, ulint prtype, ulint *mbminlen, ulint *mbmaxlen)
UNIV_INTERN void * ut_malloc(ulint n)
Definition: ut0mem.cc:235
mem_heap_t * heap
Definition: dict0mem.h:423
UNIV_INTERN dict_index_t * dict_mem_index_create(const char *table_name, const char *index_name, ulint space, ulint type, ulint n_fields)
Definition: dict0mem.cc:251
UNIV_INTERN void dict_mem_table_free(dict_table_t *table)
Definition: dict0mem.cc:111
unsigned prtype
Definition: dict0mem.h:273
unsigned space
Definition: dict0mem.h:486
ulong n_waiting_or_granted_auto_inc_locks
Definition: dict0mem.h:630
#define ut_d(EXPR)
Definition: ut0dbg.h:129
#define mem_heap_free(heap)
Definition: mem0mem.h:117
mem_heap_t * heap
Definition: dict0mem.h:478
unsigned len
Definition: dict0mem.h:283
char * referenced_table_name
Definition: dict0mem.h:441
unsigned ord_part
Definition: dict0mem.h:303
UNIV_INTERN void dict_mem_fill_column_struct(dict_col_t *column, ulint col_pos, ulint mtype, ulint prtype, ulint col_len)
Definition: dict0mem.cc:221
unsigned prefix_len
Definition: dict0mem.h:322
UNIV_INTERN dict_table_t * dict_mem_table_create(const char *name, ulint space, ulint n_cols, ulint flags)
Definition: dict0mem.cc:59
const char * name
Definition: dict0mem.h:321
UNIV_INLINE void dict_col_set_mbminmaxlen(dict_col_t *col, ulint mbminlen, ulint mbmaxlen)
unsigned cached
Definition: dict0mem.h:500
#define ut_a(EXPR)
Definition: ut0dbg.h:105
UNIV_INLINE void * mem_heap_alloc(mem_heap_t *heap, ulint n)
#define mem_heap_create(N)
Definition: mem0mem.h:97
UNIV_INLINE void * mem_heap_zalloc(mem_heap_t *heap, ulint n)
unsigned ind
Definition: dict0mem.h:301
UNIV_INTERN void dict_mem_table_add_col(dict_table_t *table, mem_heap_t *heap, const char *name, ulint mtype, ulint prtype, ulint len)
Definition: dict0mem.cc:178
#define DICT_TF2_BITS
Definition: dict0mem.h:119
mem_heap_t * heap
Definition: dict0mem.h:338
UNIV_INTERN void dict_mem_foreign_table_name_lookup_set(dict_foreign_t *foreign, ibool do_alloc)
Definition: dict0mem.cc:303
UNIV_INTERN void dict_mem_index_free(dict_index_t *index)
Definition: dict0mem.cc:381
#define ut_ad(EXPR)
Definition: ut0dbg.h:127
UNIV_INTERN void ut_free(void *ptr)
Definition: ut0mem.cc:294
lock_t * autoinc_lock
Definition: dict0mem.h:617
unsigned flags
Definition: dict0mem.h:489
ib_uint64_t autoinc
Definition: dict0mem.h:628
UNIV_INTERN void innobase_casedn_str(char *a)
Definition: ha_innodb.cc:1370
UNIV_INTERN dict_foreign_t * dict_mem_foreign_create(void)
Definition: dict0mem.cc:281
mutex_t autoinc_mutex
Definition: dict0mem.h:625
const char * col_names
Definition: dict0mem.h:507
UNIV_INTERN void rbt_free(ib_rbt_t *tree)
Definition: ut0rbt.cc:743
unsigned n_def
Definition: dict0mem.h:360
UNIV_INTERN void dict_mem_referenced_table_name_lookup_set(dict_foreign_t *foreign, ibool do_alloc)
Definition: dict0mem.cc:330
unsigned n_cols
Definition: dict0mem.h:503
unsigned mtype
Definition: dict0mem.h:272
UNIV_INTERN ulint lock_get_size(void)
Definition: lock0lock.cc:601