You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

344 lines
8.2 KiB

20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
dict_col_t: Copy the fields of "dtype_t type" directly to this structure, so that all integer fields can be packed into 64 bits. (Bug #20877) dtype_t: Change the type of all bit-fields to unsigned. dict_table_get_nth_col(), dict_table_get_sys_col_noninline(), dict_table_get_sys_col(), dict_field_get_col(): Return const dict_col_t*, so that changes to dict_col_t can be detected more easily. Add const to many dict_col_t* declarations. dict_index_get_nth_type(): Replace with dict_index_get_nth_col(). dict_col_get_type(): Replace with dict_col_copy_type(). dict_col_get_min_size(), dict_col_get_max_size(), dict_col_get_fixed_size(), dict_col_get_sql_null_size(): New functions. dtype_get_at_most_n_mbchars(): Replace the parameter dtype with the parameters prtype, mbminlen, mbmaxlen. dtype_get_pad_char(), cmp_data_data(), cmp_data_data_slow(), cmp_whole_field(): Replace the dtype_t* parameter with the ulint parameters mtype, prtype. dtype_copy(): Add a const qualifier to type2 (the one being copied from). dtype_set_mblen(): Replaced with dtype_get_mblen(). dtype_get_fixed_size_low(), dtype_get_min_size_low(), dtype_get_fixed_max_low(): Replace dtype_get_fixed_size(), dtype_get_min_size(), and dtype_get_max_size(). These are used by the dict_col_get_{fixed,min,max}_size() functions. cmp_types_are_equal(): Replace with cmp_cols_are_equal(). dict_table_get_col_name(): Add a const qualifier parameter to the parameter "table". dtype_binary, dtype_binary_val: Remove. dtype_is_fixed_size(): Remove.
19 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
dict_col_t: Copy the fields of "dtype_t type" directly to this structure, so that all integer fields can be packed into 64 bits. (Bug #20877) dtype_t: Change the type of all bit-fields to unsigned. dict_table_get_nth_col(), dict_table_get_sys_col_noninline(), dict_table_get_sys_col(), dict_field_get_col(): Return const dict_col_t*, so that changes to dict_col_t can be detected more easily. Add const to many dict_col_t* declarations. dict_index_get_nth_type(): Replace with dict_index_get_nth_col(). dict_col_get_type(): Replace with dict_col_copy_type(). dict_col_get_min_size(), dict_col_get_max_size(), dict_col_get_fixed_size(), dict_col_get_sql_null_size(): New functions. dtype_get_at_most_n_mbchars(): Replace the parameter dtype with the parameters prtype, mbminlen, mbmaxlen. dtype_get_pad_char(), cmp_data_data(), cmp_data_data_slow(), cmp_whole_field(): Replace the dtype_t* parameter with the ulint parameters mtype, prtype. dtype_copy(): Add a const qualifier to type2 (the one being copied from). dtype_set_mblen(): Replaced with dtype_get_mblen(). dtype_get_fixed_size_low(), dtype_get_min_size_low(), dtype_get_fixed_max_low(): Replace dtype_get_fixed_size(), dtype_get_min_size(), and dtype_get_max_size(). These are used by the dict_col_get_{fixed,min,max}_size() functions. cmp_types_are_equal(): Replace with cmp_cols_are_equal(). dict_table_get_col_name(): Add a const qualifier parameter to the parameter "table". dtype_binary, dtype_binary_val: Remove. dtype_is_fixed_size(): Remove.
19 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
  1. /**********************************************************************
  2. Data dictionary memory object creation
  3. (c) 1996 Innobase Oy
  4. Created 1/8/1996 Heikki Tuuri
  5. ***********************************************************************/
  6. #include "dict0mem.h"
  7. #ifdef UNIV_NONINL
  8. #include "dict0mem.ic"
  9. #endif
  10. #include "rem0rec.h"
  11. #include "data0type.h"
  12. #include "mach0data.h"
  13. #include "dict0dict.h"
  14. #include "que0que.h"
  15. #include "pars0pars.h"
  16. #include "lock0lock.h"
  17. #define DICT_HEAP_SIZE 100 /* initial memory heap size when
  18. creating a table or index object */
  19. /**************************************************************************
  20. Creates a table memory object. */
  21. dict_table_t*
  22. dict_mem_table_create(
  23. /*==================*/
  24. /* out, own: table object */
  25. const char* name, /* in: table name */
  26. ulint space, /* in: space where the clustered index of
  27. the table is placed; this parameter is
  28. ignored if the table is made a member of
  29. a cluster */
  30. ulint n_cols, /* in: number of columns */
  31. ulint flags) /* in: table flags */
  32. {
  33. dict_table_t* table;
  34. mem_heap_t* heap;
  35. ut_ad(name);
  36. ut_ad(!(flags & ~DICT_TF_COMPACT));
  37. heap = mem_heap_create(DICT_HEAP_SIZE);
  38. table = mem_heap_alloc(heap, sizeof(dict_table_t));
  39. table->heap = heap;
  40. table->flags = (unsigned int) flags;
  41. table->name = mem_heap_strdup(heap, name);
  42. table->dir_path_of_temp_table = NULL;
  43. table->space = (unsigned int) space;
  44. table->ibd_file_missing = FALSE;
  45. table->tablespace_discarded = FALSE;
  46. table->n_def = 0;
  47. table->n_cols = (unsigned int) (n_cols + DATA_N_SYS_COLS);
  48. table->n_mysql_handles_opened = 0;
  49. table->n_foreign_key_checks_running = 0;
  50. table->cached = FALSE;
  51. table->cols = mem_heap_alloc(heap, (n_cols + DATA_N_SYS_COLS)
  52. * sizeof(dict_col_t));
  53. table->col_names = NULL;
  54. UT_LIST_INIT(table->indexes);
  55. table->auto_inc_lock = mem_heap_alloc(heap, lock_get_size());
  56. table->query_cache_inv_trx_id = ut_dulint_zero;
  57. UT_LIST_INIT(table->locks);
  58. UT_LIST_INIT(table->foreign_list);
  59. UT_LIST_INIT(table->referenced_list);
  60. #ifdef UNIV_DEBUG
  61. table->does_not_fit_in_memory = FALSE;
  62. #endif /* UNIV_DEBUG */
  63. table->stat_initialized = FALSE;
  64. table->stat_modified_counter = 0;
  65. table->big_rows = 0;
  66. mutex_create(&table->autoinc_mutex, SYNC_DICT_AUTOINC_MUTEX);
  67. table->autoinc = 0;
  68. /* The number of transactions that are either waiting on the
  69. AUTOINC lock or have been granted the lock. */
  70. table->n_waiting_or_granted_auto_inc_locks = 0;
  71. #ifdef UNIV_DEBUG
  72. table->magic_n = DICT_TABLE_MAGIC_N;
  73. #endif /* UNIV_DEBUG */
  74. return(table);
  75. }
  76. /********************************************************************
  77. Free a table memory object. */
  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. mutex_free(&(table->autoinc_mutex));
  86. mem_heap_free(table->heap);
  87. }
  88. /********************************************************************
  89. Append 'name' to 'col_names' (@see dict_table_t::col_names). */
  90. static
  91. const char*
  92. dict_add_col_name(
  93. /*==============*/
  94. /* out: new column names array */
  95. const char* col_names, /* in: existing column names, or
  96. NULL */
  97. ulint cols, /* in: number of existing columns */
  98. const char* name, /* in: new column name */
  99. mem_heap_t* heap) /* in: heap */
  100. {
  101. ulint old_len;
  102. ulint new_len;
  103. ulint total_len;
  104. char* res;
  105. ut_ad(!cols == !col_names);
  106. /* Find out length of existing array. */
  107. if (col_names) {
  108. const char* s = col_names;
  109. ulint i;
  110. for (i = 0; i < cols; i++) {
  111. s += strlen(s) + 1;
  112. }
  113. old_len = s - col_names;
  114. } else {
  115. old_len = 0;
  116. }
  117. new_len = strlen(name) + 1;
  118. total_len = old_len + new_len;
  119. res = mem_heap_alloc(heap, total_len);
  120. if (old_len > 0) {
  121. memcpy(res, col_names, old_len);
  122. }
  123. memcpy(res + old_len, name, new_len);
  124. return(res);
  125. }
  126. /**************************************************************************
  127. Adds a column definition to a table. */
  128. void
  129. dict_mem_table_add_col(
  130. /*===================*/
  131. dict_table_t* table, /* in: table */
  132. mem_heap_t* heap, /* in: temporary memory heap, or NULL */
  133. const char* name, /* in: column name, or NULL */
  134. ulint mtype, /* in: main datatype */
  135. ulint prtype, /* in: precise type */
  136. ulint len) /* in: precision */
  137. {
  138. dict_col_t* col;
  139. ulint mbminlen;
  140. ulint mbmaxlen;
  141. ulint i;
  142. ut_ad(table);
  143. ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
  144. ut_ad(!heap == !name);
  145. i = table->n_def++;
  146. if (name) {
  147. if (UNIV_UNLIKELY(table->n_def == table->n_cols)) {
  148. heap = table->heap;
  149. }
  150. if (UNIV_LIKELY(i) && UNIV_UNLIKELY(!table->col_names)) {
  151. /* All preceding column names are empty. */
  152. char* s = mem_heap_alloc(heap, table->n_def);
  153. memset(s, 0, table->n_def);
  154. table->col_names = s;
  155. }
  156. table->col_names = dict_add_col_name(table->col_names,
  157. i, name, heap);
  158. }
  159. col = (dict_col_t*) dict_table_get_nth_col(table, i);
  160. col->ind = (unsigned int) i;
  161. col->ord_part = 0;
  162. col->mtype = (unsigned int) mtype;
  163. col->prtype = (unsigned int) prtype;
  164. col->len = (unsigned int) len;
  165. dtype_get_mblen(mtype, prtype, &mbminlen, &mbmaxlen);
  166. col->mbminlen = (unsigned int) mbminlen;
  167. col->mbmaxlen = (unsigned int) mbmaxlen;
  168. }
  169. /**************************************************************************
  170. Creates an index memory object. */
  171. dict_index_t*
  172. dict_mem_index_create(
  173. /*==================*/
  174. /* out, own: index object */
  175. const char* table_name, /* in: table name */
  176. const char* index_name, /* in: index name */
  177. ulint space, /* in: space where the index tree is
  178. placed, ignored if the index is of
  179. the clustered type */
  180. ulint type, /* in: DICT_UNIQUE,
  181. DICT_CLUSTERED, ... ORed */
  182. ulint n_fields) /* in: number of fields */
  183. {
  184. dict_index_t* index;
  185. mem_heap_t* heap;
  186. ut_ad(table_name && index_name);
  187. heap = mem_heap_create(DICT_HEAP_SIZE);
  188. index = mem_heap_alloc(heap, sizeof(dict_index_t));
  189. index->heap = heap;
  190. index->type = type;
  191. index->space = (unsigned int) space;
  192. index->page = 0;
  193. index->name = mem_heap_strdup(heap, index_name);
  194. index->table_name = table_name;
  195. index->table = NULL;
  196. index->n_def = index->n_nullable = 0;
  197. index->n_fields = (unsigned int) n_fields;
  198. index->fields = mem_heap_alloc(heap, 1 + n_fields
  199. * sizeof(dict_field_t));
  200. /* The '1 +' above prevents allocation
  201. of an empty mem block */
  202. index->stat_n_diff_key_vals = NULL;
  203. index->cached = FALSE;
  204. memset(&index->lock, 0, sizeof index->lock);
  205. #ifdef UNIV_DEBUG
  206. index->magic_n = DICT_INDEX_MAGIC_N;
  207. #endif /* UNIV_DEBUG */
  208. return(index);
  209. }
  210. /**************************************************************************
  211. Creates and initializes a foreign constraint memory object. */
  212. dict_foreign_t*
  213. dict_mem_foreign_create(void)
  214. /*=========================*/
  215. /* out, own: foreign constraint struct */
  216. {
  217. dict_foreign_t* foreign;
  218. mem_heap_t* heap;
  219. heap = mem_heap_create(100);
  220. foreign = mem_heap_alloc(heap, sizeof(dict_foreign_t));
  221. foreign->heap = heap;
  222. foreign->id = NULL;
  223. foreign->type = 0;
  224. foreign->foreign_table_name = NULL;
  225. foreign->foreign_table = NULL;
  226. foreign->foreign_col_names = NULL;
  227. foreign->referenced_table_name = NULL;
  228. foreign->referenced_table = NULL;
  229. foreign->referenced_col_names = NULL;
  230. foreign->n_fields = 0;
  231. foreign->foreign_index = NULL;
  232. foreign->referenced_index = NULL;
  233. return(foreign);
  234. }
  235. /**************************************************************************
  236. Adds a field definition to an index. NOTE: does not take a copy
  237. of the column name if the field is a column. The memory occupied
  238. by the column name may be released only after publishing the index. */
  239. void
  240. dict_mem_index_add_field(
  241. /*=====================*/
  242. dict_index_t* index, /* in: index */
  243. const char* name, /* in: column name */
  244. ulint prefix_len) /* in: 0 or the column prefix length
  245. in a MySQL index like
  246. INDEX (textcol(25)) */
  247. {
  248. dict_field_t* field;
  249. ut_ad(index);
  250. ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
  251. index->n_def++;
  252. field = dict_index_get_nth_field(index, index->n_def - 1);
  253. field->name = name;
  254. field->prefix_len = (unsigned int) prefix_len;
  255. }
  256. /**************************************************************************
  257. Frees an index memory object. */
  258. void
  259. dict_mem_index_free(
  260. /*================*/
  261. dict_index_t* index) /* in: index */
  262. {
  263. ut_ad(index);
  264. ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
  265. mem_heap_free(index->heap);
  266. }