📄 dict0dict.c
字号:
return(TRUE);}/**************************************************************************Removes an index from the dictionary cache. */staticvoiddict_index_remove_from_cache(/*=========================*/ dict_table_t* table, /* in: table */ dict_index_t* index) /* in, own: index */{ dict_field_t* field; ulint size; ulint i; ut_ad(table && index); ut_ad(table->magic_n == DICT_TABLE_MAGIC_N); ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);#ifdef UNIV_SYNC_DEBUG ut_ad(mutex_own(&(dict_sys->mutex)));#endif /* UNIV_SYNC_DEBUG */ ut_ad(UT_LIST_GET_LEN((index->tree)->tree_indexes) == 1); dict_tree_free(index->tree); /* Decrement the ord_part counts in columns which are ordering */ for (i = 0; i < dict_index_get_n_unique(index); i++) { field = dict_index_get_nth_field(index, i); ut_ad(dict_field_get_col(field)->ord_part > 0); (dict_field_get_col(field)->ord_part)--; } /* Remove the index from the list of indexes of the table */ UT_LIST_REMOVE(indexes, table->indexes, index); size = mem_heap_get_size(index->heap); ut_ad(dict_sys->size >= size); dict_sys->size -= size; mem_heap_free(index->heap);}/***********************************************************************Tries to find column names for the index in the column hash table andsets the col field of the index. */staticibooldict_index_find_cols(/*=================*/ /* out: TRUE if success */ dict_table_t* table, /* in: table */ dict_index_t* index) /* in: index */ { dict_col_t* col; dict_field_t* field; ulint fold; ulint i; ut_ad(table && index); ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);#ifdef UNIV_SYNC_DEBUG ut_ad(mutex_own(&(dict_sys->mutex)));#endif /* UNIV_SYNC_DEBUG */ for (i = 0; i < index->n_fields; i++) { field = dict_index_get_nth_field(index, i); fold = ut_fold_ulint_pair(ut_fold_string(table->name), ut_fold_string(field->name)); HASH_SEARCH(hash, dict_sys->col_hash, fold, col, (ut_strcmp(col->name, field->name) == 0) && (ut_strcmp((col->table)->name, table->name) == 0)); if (col == NULL) { return(FALSE); } else { field->col = col; } } return(TRUE);} /***********************************************************************Adds a column to index. */voiddict_index_add_col(/*===============*/ dict_index_t* index, /* in: index */ dict_col_t* col, /* in: column */ ulint order, /* in: order criterion */ ulint prefix_len) /* in: column prefix length */{ dict_field_t* field; dict_mem_index_add_field(index, col->name, order, prefix_len); field = dict_index_get_nth_field(index, index->n_def - 1); field->col = col; field->fixed_len = dtype_get_fixed_size(&col->type); if (prefix_len && field->fixed_len > prefix_len) { field->fixed_len = prefix_len; } /* Long fixed-length fields that need external storage are treated as variable-length fields, so that the extern flag can be embedded in the length word. */ if (field->fixed_len > DICT_MAX_INDEX_COL_LEN) { field->fixed_len = 0; } if (!(dtype_get_prtype(&col->type) & DATA_NOT_NULL)) { index->n_nullable++; } if (index->n_def > 1) { const dict_field_t* field2 = dict_index_get_nth_field(index, index->n_def - 2); field->fixed_offs = (!field2->fixed_len || field2->fixed_offs == ULINT_UNDEFINED) ? ULINT_UNDEFINED : field2->fixed_len + field2->fixed_offs; } else { field->fixed_offs = 0; }}/***********************************************************************Copies fields contained in index2 to index1. */staticvoiddict_index_copy(/*============*/ dict_index_t* index1, /* in: index to copy to */ dict_index_t* index2, /* in: index to copy from */ ulint start, /* in: first position to copy */ ulint end) /* in: last position to copy */{ dict_field_t* field; ulint i; /* Copy fields contained in index2 */ for (i = start; i < end; i++) { field = dict_index_get_nth_field(index2, i); dict_index_add_col(index1, field->col, field->order, field->prefix_len); }}/***********************************************************************Copies types of fields contained in index to tuple. */voiddict_index_copy_types(/*==================*/ dtuple_t* tuple, /* in: data tuple */ dict_index_t* index, /* in: index */ ulint n_fields) /* in: number of field types to copy */{ dtype_t* dfield_type; dtype_t* type; ulint i; if (UNIV_UNLIKELY(index->type & DICT_UNIVERSAL)) { dtuple_set_types_binary(tuple, n_fields); return; } for (i = 0; i < n_fields; i++) { dfield_type = dfield_get_type(dtuple_get_nth_field(tuple, i)); type = dict_col_get_type(dict_field_get_col( dict_index_get_nth_field(index, i))); *dfield_type = *type; }}/***********************************************************************Copies types of columns contained in table to tuple. */voiddict_table_copy_types(/*==================*/ dtuple_t* tuple, /* in: data tuple */ dict_table_t* table) /* in: index */{ dtype_t* dfield_type; dtype_t* type; ulint i; ut_ad(!(table->type & DICT_UNIVERSAL)); for (i = 0; i < dtuple_get_n_fields(tuple); i++) { dfield_type = dfield_get_type(dtuple_get_nth_field(tuple, i)); type = dict_col_get_type(dict_table_get_nth_col(table, i)); *dfield_type = *type; }}/***********************************************************************Builds the internal dictionary cache representation for a clusteredindex, containing also system fields not defined by the user. */staticdict_index_t*dict_index_build_internal_clust(/*============================*/ /* out, own: the internal representation of the clustered index */ dict_table_t* table, /* in: table */ dict_index_t* index) /* in: user representation of a clustered index */ { dict_index_t* new_index; dict_field_t* field; dict_col_t* col; ulint fixed_size; ulint trx_id_pos; ulint i; ut_ad(table && index); ut_ad(index->type & DICT_CLUSTERED);#ifdef UNIV_SYNC_DEBUG ut_ad(mutex_own(&(dict_sys->mutex)));#endif /* UNIV_SYNC_DEBUG */ ut_ad(table->magic_n == DICT_TABLE_MAGIC_N); /* Create a new index object with certainly enough fields */ new_index = dict_mem_index_create(table->name, index->name, table->space, index->type, index->n_fields + table->n_cols); /* Copy other relevant data from the old index struct to the new struct: it inherits the values */ new_index->n_user_defined_cols = index->n_fields; new_index->id = index->id; if (table->type != DICT_TABLE_ORDINARY) { /* The index is mixed: copy common key prefix fields */ dict_index_copy(new_index, index, 0, table->mix_len); /* Add the mix id column */ dict_index_add_col(new_index, dict_table_get_sys_col(table, DATA_MIX_ID), 0, 0); /* Copy the rest of fields */ dict_index_copy(new_index, index, table->mix_len, index->n_fields); } else { /* Copy the fields of index */ dict_index_copy(new_index, index, 0, index->n_fields); } if (UNIV_UNLIKELY(index->type & DICT_UNIVERSAL)) { /* No fixed number of fields determines an entry uniquely */ new_index->n_uniq = ULINT_MAX; } else if (index->type & DICT_UNIQUE) { /* Only the fields defined so far are needed to identify the index entry uniquely */ new_index->n_uniq = new_index->n_def; } else { /* Also the row id is needed to identify the entry */ new_index->n_uniq = 1 + new_index->n_def; } new_index->trx_id_offset = 0; if (!(index->type & DICT_IBUF)) { /* Add system columns, trx id first */ trx_id_pos = new_index->n_def; ut_ad(DATA_ROW_ID == 0); ut_ad(DATA_TRX_ID == 1); ut_ad(DATA_ROLL_PTR == 2); if (!(index->type & DICT_UNIQUE)) { dict_index_add_col(new_index, dict_table_get_sys_col(table, DATA_ROW_ID), 0, 0); trx_id_pos++; } dict_index_add_col(new_index, dict_table_get_sys_col(table, DATA_TRX_ID), 0, 0); dict_index_add_col(new_index, dict_table_get_sys_col(table, DATA_ROLL_PTR), 0, 0); for (i = 0; i < trx_id_pos; i++) { fixed_size = dtype_get_fixed_size( dict_index_get_nth_type(new_index, i)); if (fixed_size == 0) { new_index->trx_id_offset = 0; break; } if (dict_index_get_nth_field(new_index, i)->prefix_len > 0) { new_index->trx_id_offset = 0; break; } new_index->trx_id_offset += fixed_size; } } /* Set auxiliary variables in table columns as undefined */ for (i = 0; i < table->n_cols; i++) { col = dict_table_get_nth_col(table, i); col->aux = ULINT_UNDEFINED; } /* Mark with 0 the table columns already contained in new_index */ for (i = 0; i < new_index->n_def; i++) { field = dict_index_get_nth_field(new_index, i); /* If there is only a prefix of the column in the index field, do not mark the column as contained in the index */ if (field->prefix_len == 0) { field->col->aux = 0; } } /* Add to new_index non-system columns of table not yet included there */ for (i = 0; i < table->n_cols - DATA_N_SYS_COLS; i++) { col = dict_table_get_nth_col(table, i); ut_ad(col->type.mtype != DATA_SYS); if (col->aux == ULINT_UNDEFINED) { dict_index_add_col(new_index, col, 0, 0); } } ut_ad((index->type & DICT_IBUF) || (UT_LIST_GET_LEN(table->indexes) == 0)); /* Store to the column structs the position of the table columns in the clustered index */ for (i = 0; i < new_index->n_def; i++) { field = dict_index_get_nth_field(new_index, i); if (field->prefix_len == 0) { field->col->clust_pos = i; } } new_index->cached = TRUE; return(new_index);} /***********************************************************************Builds the internal dictionary cache representation for a non-clusteredindex, containing also system fields not defined by the user. */staticdict_index_t*dict_index_build_internal_non_clust(/*================================*/ /* out, own: the internal representation of the non-clustered index */ dict_table_t* table, /* in: table */ dict_index_t* index) /* in: user representation of a non-clustered index */ { dict_field_t* field; dict_index_t* new_index; dict_index_t* clust_index; ulint i; ut_ad(table && index); ut_ad(0 == (index->type & DICT_CLUSTERED));#ifdef UNIV_SYNC_DEBUG ut_ad(mutex_own(&(dict_sys->mutex)));#endif /* UNIV_SYNC_DEBUG */ ut_ad(table->magic_n == DICT_TABLE_MAGIC_N); /* The clustered index should be the first in the list of indexes */ clust_index = UT_LIST_GET_FIRST(table->indexes); ut_ad(clust_index); ut_ad(clust_index->type & DICT_CLUSTERED); ut_ad(!(clust_index->type & DICT_UNIVERSAL)); /* Create a new index */ new_index = dict_mem_index_create(table->name, index->name, index->space, index->type, index->n_fields + 1 + clust_index->n_uniq); /* Copy other relevant data from the old index struct to the new struct: it inherits the values */ new_index->n_user_defined_cols = index->n_fields; new_index->id = index->id; /* Copy fields from index to new_index */ dict_index_copy(new_index, index, 0, index->n_fields); /* Set the auxiliary variables in the clust_index unique columns as undefined */ for (i = 0; i < clust_index->n_uniq; i++) { field = dict_index_get_nth_field(clust_index, i); field->col->aux = ULINT_UNDEFINED; } /* Mark with 0 table columns already contained in new_index */ for (i = 0; i < new_index->n_def; i++) { field = dict_index_get_nth_field(new_index, i); /* If there is only a prefix of the column in the index field, do not mark the column as contained in the index */ if (field->prefix_len == 0) { field->col->aux = 0; } } /* Add to new_index the columns necessary to determine the clustered index entry uniquely */ for (i = 0; i < clust_index->n_uniq; i++) { field = dict_index_get_nth_field(clust_index, i); if (field->col->aux == ULINT_UNDEFINED) { dict_index_add_col(new_index, field->col, 0, field->prefix_len); } } if ((index->type) & DICT_UNIQUE) { new_index->n_uniq = index->n_fields; } else { new_index->n_uniq = new_index->n_def; } /* Set the n_fields value in new_index to the actual defined number of fields */ new_index->n_fields = new_index->n_def; new_index->cached = TRUE; return(new_index);} /*====================== FOREIGN KEY PROCESSING ========================*//*************************************************************************Checks if a table is referenced by foreign keys. */ibooldict_table_referenced_by_foreign_key(/*=================================*/ /* out: TRUE if table is referenced by a foreign key */ dict_table_t* table) /* in: InnoDB table */{ if (UT_LIST_GET_LEN(table->referenced_list) > 0) { return(TRUE); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -