2005-10-27 09:29:40 +02:00
|
|
|
/**********************************************************************
|
|
|
|
Data dictionary memory object creation
|
|
|
|
|
|
|
|
(c) 1996 Innobase Oy
|
|
|
|
|
|
|
|
Created 1/8/1996 Heikki Tuuri
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
#include "dict0mem.h"
|
|
|
|
|
|
|
|
#ifdef UNIV_NONINL
|
|
|
|
#include "dict0mem.ic"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "rem0rec.h"
|
|
|
|
#include "data0type.h"
|
|
|
|
#include "mach0data.h"
|
|
|
|
#include "dict0dict.h"
|
|
|
|
#include "que0que.h"
|
|
|
|
#include "pars0pars.h"
|
|
|
|
#include "lock0lock.h"
|
|
|
|
|
|
|
|
#define DICT_HEAP_SIZE 100 /* initial memory heap size when
|
|
|
|
creating a table or index object */
|
|
|
|
|
|
|
|
/**************************************************************************
|
|
|
|
Creates a table memory object. */
|
|
|
|
|
|
|
|
dict_table_t*
|
|
|
|
dict_mem_table_create(
|
|
|
|
/*==================*/
|
|
|
|
/* out, own: table object */
|
|
|
|
const char* name, /* in: table name */
|
|
|
|
ulint space, /* in: space where the clustered index of
|
|
|
|
the table is placed; this parameter is
|
|
|
|
ignored if the table is made a member of
|
|
|
|
a cluster */
|
|
|
|
ulint n_cols, /* in: number of columns */
|
2006-02-27 10:33:26 +01:00
|
|
|
ulint flags) /* in: table flags */
|
2005-10-27 09:29:40 +02:00
|
|
|
{
|
|
|
|
dict_table_t* table;
|
|
|
|
mem_heap_t* heap;
|
2006-02-23 20:25:29 +01:00
|
|
|
|
2005-10-27 09:29:40 +02:00
|
|
|
ut_ad(name);
|
|
|
|
|
|
|
|
heap = mem_heap_create(DICT_HEAP_SIZE);
|
|
|
|
|
|
|
|
table = mem_heap_alloc(heap, sizeof(dict_table_t));
|
|
|
|
|
|
|
|
table->heap = heap;
|
|
|
|
|
2007-04-02 07:39:41 +02:00
|
|
|
table->flags = (unsigned int) flags;
|
2005-10-27 09:29:40 +02:00
|
|
|
table->name = mem_heap_strdup(heap, name);
|
|
|
|
table->dir_path_of_temp_table = NULL;
|
2007-06-11 16:39:04 +02:00
|
|
|
table->version_number = 0;
|
2007-04-02 07:39:41 +02:00
|
|
|
table->space = (unsigned int) space;
|
2005-10-27 09:29:40 +02:00
|
|
|
table->ibd_file_missing = FALSE;
|
|
|
|
table->tablespace_discarded = FALSE;
|
|
|
|
table->n_def = 0;
|
2007-04-02 07:39:41 +02:00
|
|
|
table->n_cols = (unsigned int) (n_cols + DATA_N_SYS_COLS);
|
2005-10-27 09:29:40 +02:00
|
|
|
|
|
|
|
table->n_mysql_handles_opened = 0;
|
|
|
|
table->n_foreign_key_checks_running = 0;
|
2006-02-23 20:25:29 +01:00
|
|
|
|
2005-10-27 09:29:40 +02:00
|
|
|
table->cached = FALSE;
|
2007-04-04 13:05:33 +02:00
|
|
|
table->to_be_dropped = 0;
|
2006-02-23 20:25:29 +01:00
|
|
|
|
2005-10-27 09:29:40 +02:00
|
|
|
table->cols = mem_heap_alloc(heap, (n_cols + DATA_N_SYS_COLS)
|
2006-08-29 11:30:31 +02:00
|
|
|
* sizeof(dict_col_t));
|
2006-09-19 12:14:07 +02:00
|
|
|
table->col_names = NULL;
|
2005-10-27 09:29:40 +02:00
|
|
|
UT_LIST_INIT(table->indexes);
|
|
|
|
|
|
|
|
table->auto_inc_lock = mem_heap_alloc(heap, lock_get_size());
|
|
|
|
|
|
|
|
table->query_cache_inv_trx_id = ut_dulint_zero;
|
|
|
|
|
|
|
|
UT_LIST_INIT(table->locks);
|
|
|
|
UT_LIST_INIT(table->foreign_list);
|
|
|
|
UT_LIST_INIT(table->referenced_list);
|
2007-04-04 13:05:33 +02:00
|
|
|
UT_LIST_INIT(table->prebuilts);
|
2005-10-27 09:29:40 +02:00
|
|
|
|
2006-09-19 12:14:07 +02:00
|
|
|
#ifdef UNIV_DEBUG
|
2005-10-27 09:29:40 +02:00
|
|
|
table->does_not_fit_in_memory = FALSE;
|
2006-09-19 12:14:07 +02:00
|
|
|
#endif /* UNIV_DEBUG */
|
2005-10-27 09:29:40 +02:00
|
|
|
|
|
|
|
table->stat_initialized = FALSE;
|
|
|
|
|
|
|
|
table->stat_modified_counter = 0;
|
2006-02-23 20:25:29 +01:00
|
|
|
|
2006-09-19 12:14:07 +02:00
|
|
|
table->big_rows = 0;
|
2006-06-13 22:23:26 +02:00
|
|
|
|
2006-05-08 08:18:59 +02:00
|
|
|
mutex_create(&table->autoinc_mutex, SYNC_DICT_AUTOINC_MUTEX);
|
2005-10-27 09:29:40 +02:00
|
|
|
|
|
|
|
table->autoinc_inited = FALSE;
|
2006-09-19 12:14:07 +02:00
|
|
|
#ifdef UNIV_DEBUG
|
2005-10-27 09:29:40 +02:00
|
|
|
table->magic_n = DICT_TABLE_MAGIC_N;
|
2006-09-19 12:14:07 +02:00
|
|
|
#endif /* UNIV_DEBUG */
|
2005-10-27 09:29:40 +02:00
|
|
|
return(table);
|
|
|
|
}
|
|
|
|
|
2006-04-12 11:32:17 +02:00
|
|
|
/********************************************************************
|
|
|
|
Free a table memory object. */
|
2005-10-27 09:29:40 +02:00
|
|
|
|
|
|
|
void
|
2006-04-12 11:32:17 +02:00
|
|
|
dict_mem_table_free(
|
|
|
|
/*================*/
|
|
|
|
dict_table_t* table) /* in: table */
|
2005-10-27 09:29:40 +02:00
|
|
|
{
|
2006-04-12 11:32:17 +02:00
|
|
|
ut_ad(table);
|
|
|
|
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
|
|
|
|
|
|
|
|
mutex_free(&(table->autoinc_mutex));
|
|
|
|
mem_heap_free(table->heap);
|
2005-10-27 09:29:40 +02:00
|
|
|
}
|
|
|
|
|
2006-09-19 12:14:07 +02:00
|
|
|
/********************************************************************
|
2007-01-30 10:24:18 +01:00
|
|
|
Append 'name' to 'col_names' (@see dict_table_t::col_names). */
|
2006-09-19 12:14:07 +02:00
|
|
|
static
|
|
|
|
const char*
|
|
|
|
dict_add_col_name(
|
|
|
|
/*==============*/
|
|
|
|
/* out: new column names array */
|
|
|
|
const char* col_names, /* in: existing column names, or
|
|
|
|
NULL */
|
|
|
|
ulint cols, /* in: number of existing columns */
|
|
|
|
const char* name, /* in: new column name */
|
2007-01-30 10:24:18 +01:00
|
|
|
mem_heap_t* heap) /* in: heap */
|
2006-09-19 12:14:07 +02:00
|
|
|
{
|
2007-01-30 10:24:18 +01:00
|
|
|
ulint old_len;
|
|
|
|
ulint new_len;
|
|
|
|
ulint total_len;
|
|
|
|
char* res;
|
2006-09-19 12:14:07 +02:00
|
|
|
|
2007-01-30 10:24:18 +01:00
|
|
|
ut_ad(!cols == !col_names);
|
2006-09-19 12:14:07 +02:00
|
|
|
|
|
|
|
/* Find out length of existing array. */
|
|
|
|
if (col_names) {
|
2007-01-30 10:24:18 +01:00
|
|
|
const char* s = col_names;
|
|
|
|
ulint i;
|
2006-09-19 12:14:07 +02:00
|
|
|
|
|
|
|
for (i = 0; i < cols; i++) {
|
|
|
|
s += strlen(s) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
old_len = s - col_names;
|
|
|
|
} else {
|
|
|
|
old_len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_len = strlen(name) + 1;
|
|
|
|
total_len = old_len + new_len;
|
|
|
|
|
2007-01-30 10:24:18 +01:00
|
|
|
res = mem_heap_alloc(heap, total_len);
|
2006-09-19 12:14:07 +02:00
|
|
|
|
|
|
|
if (old_len > 0) {
|
|
|
|
memcpy(res, col_names, old_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(res + old_len, name, new_len);
|
|
|
|
|
|
|
|
return(res);
|
|
|
|
}
|
|
|
|
|
2005-10-27 09:29:40 +02:00
|
|
|
/**************************************************************************
|
|
|
|
Adds a column definition to a table. */
|
|
|
|
|
|
|
|
void
|
|
|
|
dict_mem_table_add_col(
|
|
|
|
/*===================*/
|
|
|
|
dict_table_t* table, /* in: table */
|
2007-01-30 10:24:18 +01:00
|
|
|
mem_heap_t* heap, /* in: temporary memory heap, or NULL */
|
|
|
|
const char* name, /* in: column name, or NULL */
|
2005-10-27 09:29:40 +02:00
|
|
|
ulint mtype, /* in: main datatype */
|
|
|
|
ulint prtype, /* in: precise type */
|
2006-09-19 12:14:07 +02:00
|
|
|
ulint len) /* in: precision */
|
2005-10-27 09:29:40 +02:00
|
|
|
{
|
|
|
|
dict_col_t* col;
|
2006-09-19 12:14:07 +02:00
|
|
|
ulint mbminlen;
|
|
|
|
ulint mbmaxlen;
|
2007-01-30 10:24:18 +01:00
|
|
|
ulint i;
|
2006-02-23 20:25:29 +01:00
|
|
|
|
2007-01-30 10:24:18 +01:00
|
|
|
ut_ad(table);
|
2005-10-27 09:29:40 +02:00
|
|
|
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
|
2007-01-30 10:24:18 +01:00
|
|
|
ut_ad(!heap == !name);
|
2006-02-23 20:25:29 +01:00
|
|
|
|
2007-01-30 10:24:18 +01:00
|
|
|
i = table->n_def++;
|
2005-10-27 09:29:40 +02:00
|
|
|
|
2007-01-30 10:24:18 +01:00
|
|
|
if (name) {
|
|
|
|
if (UNIV_UNLIKELY(table->n_def == table->n_cols)) {
|
|
|
|
heap = table->heap;
|
|
|
|
}
|
|
|
|
if (UNIV_LIKELY(i) && UNIV_UNLIKELY(!table->col_names)) {
|
|
|
|
/* All preceding column names are empty. */
|
2007-06-19 11:39:27 +02:00
|
|
|
char* s = mem_heap_calloc(heap, table->n_def);
|
2007-01-30 10:24:18 +01:00
|
|
|
table->col_names = s;
|
|
|
|
}
|
2006-09-19 12:14:07 +02:00
|
|
|
|
2007-01-30 10:24:18 +01:00
|
|
|
table->col_names = dict_add_col_name(table->col_names,
|
|
|
|
i, name, heap);
|
|
|
|
}
|
|
|
|
|
|
|
|
col = dict_table_get_nth_col(table, i);
|
2005-10-27 09:29:40 +02:00
|
|
|
|
2007-01-30 10:24:18 +01:00
|
|
|
col->ind = i;
|
2005-10-27 09:29:40 +02:00
|
|
|
col->ord_part = 0;
|
|
|
|
|
2007-04-02 07:39:41 +02:00
|
|
|
col->mtype = (unsigned int) mtype;
|
|
|
|
col->prtype = (unsigned int) prtype;
|
|
|
|
col->len = (unsigned int) len;
|
2006-02-23 20:25:29 +01:00
|
|
|
|
2006-09-19 12:14:07 +02:00
|
|
|
dtype_get_mblen(mtype, prtype, &mbminlen, &mbmaxlen);
|
2005-10-27 09:29:40 +02:00
|
|
|
|
2007-04-02 07:39:41 +02:00
|
|
|
col->mbminlen = (unsigned int) mbminlen;
|
|
|
|
col->mbmaxlen = (unsigned int) mbmaxlen;
|
2005-10-27 09:29:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**************************************************************************
|
|
|
|
Creates an index memory object. */
|
|
|
|
|
|
|
|
dict_index_t*
|
|
|
|
dict_mem_index_create(
|
|
|
|
/*==================*/
|
|
|
|
/* out, own: index object */
|
|
|
|
const char* table_name, /* in: table name */
|
|
|
|
const char* index_name, /* in: index name */
|
|
|
|
ulint space, /* in: space where the index tree is
|
|
|
|
placed, ignored if the index is of
|
|
|
|
the clustered type */
|
|
|
|
ulint type, /* in: DICT_UNIQUE,
|
|
|
|
DICT_CLUSTERED, ... ORed */
|
|
|
|
ulint n_fields) /* in: number of fields */
|
|
|
|
{
|
|
|
|
dict_index_t* index;
|
|
|
|
mem_heap_t* heap;
|
2006-02-23 20:25:29 +01:00
|
|
|
|
2005-10-27 09:29:40 +02:00
|
|
|
ut_ad(table_name && index_name);
|
|
|
|
|
|
|
|
heap = mem_heap_create(DICT_HEAP_SIZE);
|
|
|
|
index = mem_heap_alloc(heap, sizeof(dict_index_t));
|
|
|
|
|
2007-05-18 13:01:58 +02:00
|
|
|
index->id = ut_dulint_zero;
|
2005-10-27 09:29:40 +02:00
|
|
|
index->heap = heap;
|
2006-02-23 20:25:29 +01:00
|
|
|
|
2005-10-27 09:29:40 +02:00
|
|
|
index->type = type;
|
2007-04-02 07:39:41 +02:00
|
|
|
index->space = (unsigned int) space;
|
2006-09-19 12:14:07 +02:00
|
|
|
index->page = 0;
|
2005-10-27 09:29:40 +02:00
|
|
|
index->name = mem_heap_strdup(heap, index_name);
|
|
|
|
index->table_name = table_name;
|
|
|
|
index->table = NULL;
|
|
|
|
index->n_def = index->n_nullable = 0;
|
2007-04-02 07:39:41 +02:00
|
|
|
index->n_fields = (unsigned int) n_fields;
|
2005-10-27 09:29:40 +02:00
|
|
|
index->fields = mem_heap_alloc(heap, 1 + n_fields
|
2006-08-29 11:30:31 +02:00
|
|
|
* sizeof(dict_field_t));
|
|
|
|
/* The '1 +' above prevents allocation
|
|
|
|
of an empty mem block */
|
2005-10-27 09:29:40 +02:00
|
|
|
index->stat_n_diff_key_vals = NULL;
|
|
|
|
|
|
|
|
index->cached = FALSE;
|
2007-04-04 13:05:33 +02:00
|
|
|
index->to_be_dropped = FALSE;
|
2007-05-18 13:01:58 +02:00
|
|
|
index->trx_id = ut_dulint_zero;
|
2006-09-19 12:14:07 +02:00
|
|
|
memset(&index->lock, 0, sizeof index->lock);
|
|
|
|
#ifdef UNIV_DEBUG
|
2005-10-27 09:29:40 +02:00
|
|
|
index->magic_n = DICT_INDEX_MAGIC_N;
|
2006-09-19 12:14:07 +02:00
|
|
|
#endif /* UNIV_DEBUG */
|
2005-10-27 09:29:40 +02:00
|
|
|
return(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**************************************************************************
|
|
|
|
Creates and initializes a foreign constraint memory object. */
|
|
|
|
|
|
|
|
dict_foreign_t*
|
|
|
|
dict_mem_foreign_create(void)
|
|
|
|
/*=========================*/
|
|
|
|
/* out, own: foreign constraint struct */
|
|
|
|
{
|
|
|
|
dict_foreign_t* foreign;
|
|
|
|
mem_heap_t* heap;
|
|
|
|
|
|
|
|
heap = mem_heap_create(100);
|
|
|
|
|
|
|
|
foreign = mem_heap_alloc(heap, sizeof(dict_foreign_t));
|
|
|
|
|
|
|
|
foreign->heap = heap;
|
|
|
|
|
|
|
|
foreign->id = NULL;
|
|
|
|
|
|
|
|
foreign->type = 0;
|
|
|
|
foreign->foreign_table_name = NULL;
|
|
|
|
foreign->foreign_table = NULL;
|
|
|
|
foreign->foreign_col_names = NULL;
|
|
|
|
|
|
|
|
foreign->referenced_table_name = NULL;
|
|
|
|
foreign->referenced_table = NULL;
|
|
|
|
foreign->referenced_col_names = NULL;
|
|
|
|
|
|
|
|
foreign->n_fields = 0;
|
|
|
|
|
|
|
|
foreign->foreign_index = NULL;
|
|
|
|
foreign->referenced_index = NULL;
|
|
|
|
|
|
|
|
return(foreign);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**************************************************************************
|
|
|
|
Adds a field definition to an index. NOTE: does not take a copy
|
|
|
|
of the column name if the field is a column. The memory occupied
|
|
|
|
by the column name may be released only after publishing the index. */
|
|
|
|
|
|
|
|
void
|
|
|
|
dict_mem_index_add_field(
|
|
|
|
/*=====================*/
|
|
|
|
dict_index_t* index, /* in: index */
|
|
|
|
const char* name, /* in: column name */
|
|
|
|
ulint prefix_len) /* in: 0 or the column prefix length
|
|
|
|
in a MySQL index like
|
|
|
|
INDEX (textcol(25)) */
|
|
|
|
{
|
|
|
|
dict_field_t* field;
|
2006-02-23 20:25:29 +01:00
|
|
|
|
2007-01-30 10:24:18 +01:00
|
|
|
ut_ad(index);
|
2005-10-27 09:29:40 +02:00
|
|
|
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
|
2006-02-23 20:25:29 +01:00
|
|
|
|
2005-10-27 09:29:40 +02:00
|
|
|
index->n_def++;
|
|
|
|
|
2006-02-23 20:25:29 +01:00
|
|
|
field = dict_index_get_nth_field(index, index->n_def - 1);
|
2005-10-27 09:29:40 +02:00
|
|
|
|
|
|
|
field->name = name;
|
2007-04-02 07:39:41 +02:00
|
|
|
field->prefix_len = (unsigned int) prefix_len;
|
2005-10-27 09:29:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**************************************************************************
|
|
|
|
Frees an index memory object. */
|
|
|
|
|
|
|
|
void
|
|
|
|
dict_mem_index_free(
|
|
|
|
/*================*/
|
|
|
|
dict_index_t* index) /* in: index */
|
|
|
|
{
|
2006-04-12 11:32:17 +02:00
|
|
|
ut_ad(index);
|
|
|
|
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
|
|
|
|
|
2005-10-27 09:29:40 +02:00
|
|
|
mem_heap_free(index->heap);
|
|
|
|
}
|