Drizzled Public API Documentation

dict0mem.cc
00001 /*****************************************************************************
00002 
00003 Copyright (C) 1996, 2010, Innobase Oy. All Rights Reserved.
00004 
00005 This program is free software; you can redistribute it and/or modify it under
00006 the terms of the GNU General Public License as published by the Free Software
00007 Foundation; version 2 of the License.
00008 
00009 This program is distributed in the hope that it will be useful, but WITHOUT
00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
00012 
00013 You should have received a copy of the GNU General Public License along with
00014 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
00015 St, Fifth Floor, Boston, MA 02110-1301 USA
00016 
00017 *****************************************************************************/
00018 
00019 /******************************************************************/
00026 #include "dict0mem.h"
00027 
00028 #ifdef UNIV_NONINL
00029 #include "dict0mem.ic"
00030 #endif
00031 
00032 #include "rem0rec.h"
00033 #include "data0type.h"
00034 #include "mach0data.h"
00035 #include "dict0dict.h"
00036 #ifndef UNIV_HOTBACKUP
00037 # include "lock0lock.h"
00038 #endif /* !UNIV_HOTBACKUP */
00039 
00040 #define DICT_HEAP_SIZE    100 
00043 #ifdef UNIV_PFS_MUTEX
00044 /* Key to register autoinc_mutex with performance schema */
00045 UNIV_INTERN mysql_pfs_key_t autoinc_mutex_key;
00046 #endif /* UNIV_PFS_MUTEX */
00047 
00048 /**********************************************************************/
00051 UNIV_INTERN
00052 dict_table_t*
00053 dict_mem_table_create(
00054 /*==================*/
00055   const char* name, 
00056   ulint   space,  
00060   ulint   n_cols, 
00061   ulint   flags)  
00062 {
00063   dict_table_t* table;
00064   mem_heap_t* heap;
00065 
00066   ut_ad(name);
00067   ut_a(!(flags & (SIZE_MAX << DICT_TF2_BITS)));
00068 
00069   heap = mem_heap_create(DICT_HEAP_SIZE);
00070 
00071   table = static_cast<dict_table_struct *>(mem_heap_zalloc(heap, sizeof(dict_table_t)));
00072 
00073   table->heap = heap;
00074 
00075   table->flags = (unsigned int) flags;
00076   table->name = static_cast<char *>(ut_malloc(strlen(name) + 1));
00077   memcpy(table->name, name, strlen(name) + 1);
00078   table->space = (unsigned int) space;
00079   table->n_cols = (unsigned int) (n_cols + DATA_N_SYS_COLS);
00080 
00081   table->cols = static_cast<dict_col_t *>(mem_heap_alloc(heap, (n_cols + DATA_N_SYS_COLS)
00082              * sizeof(dict_col_t)));
00083 
00084 #ifndef UNIV_HOTBACKUP
00085   table->autoinc_lock = static_cast<lock_t *>(mem_heap_alloc(heap, lock_get_size()));
00086 
00087   mutex_create(autoinc_mutex_key,
00088          &table->autoinc_mutex, SYNC_DICT_AUTOINC_MUTEX);
00089 
00090   table->autoinc = 0;
00091 
00092   /* The number of transactions that are either waiting on the
00093   AUTOINC lock or have been granted the lock. */
00094   table->n_waiting_or_granted_auto_inc_locks = 0;
00095 #endif /* !UNIV_HOTBACKUP */
00096 
00097   ut_d(table->magic_n = DICT_TABLE_MAGIC_N);
00098   return(table);
00099 }
00100 
00101 /****************************************************************/
00103 UNIV_INTERN
00104 void
00105 dict_mem_table_free(
00106 /*================*/
00107   dict_table_t* table)    
00108 {
00109   ut_ad(table);
00110   ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
00111   ut_d(table->cached = FALSE);
00112 
00113 #ifndef UNIV_HOTBACKUP
00114   mutex_free(&(table->autoinc_mutex));
00115 #endif /* UNIV_HOTBACKUP */
00116   ut_free(table->name);
00117   mem_heap_free(table->heap);
00118 }
00119 
00120 /****************************************************************/
00123 static
00124 const char*
00125 dict_add_col_name(
00126 /*==============*/
00127   const char* col_names,  
00129   ulint   cols,   
00130   const char* name,   
00131   mem_heap_t* heap)   
00132 {
00133   ulint old_len;
00134   ulint new_len;
00135   ulint total_len;
00136   char* res;
00137 
00138   ut_ad(!cols == !col_names);
00139 
00140   /* Find out length of existing array. */
00141   if (col_names) {
00142     const char* s = col_names;
00143     ulint   i;
00144 
00145     for (i = 0; i < cols; i++) {
00146       s += strlen(s) + 1;
00147     }
00148 
00149     old_len = s - col_names;
00150   } else {
00151     old_len = 0;
00152   }
00153 
00154   new_len = strlen(name) + 1;
00155   total_len = old_len + new_len;
00156 
00157   res = static_cast<char *>(mem_heap_alloc(heap, total_len));
00158 
00159   if (old_len > 0) {
00160     memcpy(res, col_names, old_len);
00161   }
00162 
00163   memcpy(res + old_len, name, new_len);
00164 
00165   return(res);
00166 }
00167 
00168 /**********************************************************************/
00170 UNIV_INTERN
00171 void
00172 dict_mem_table_add_col(
00173 /*===================*/
00174   dict_table_t* table,  
00175   mem_heap_t* heap, 
00176   const char* name, 
00177   ulint   mtype,  
00178   ulint   prtype, 
00179   ulint   len)  
00180 {
00181   dict_col_t* col;
00182   ulint   i;
00183 
00184   ut_ad(table);
00185   ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
00186   ut_ad(!heap == !name);
00187 
00188   i = table->n_def++;
00189 
00190   if (name) {
00191     if (UNIV_UNLIKELY(table->n_def == table->n_cols)) {
00192       heap = table->heap;
00193     }
00194     if (UNIV_LIKELY(i) && UNIV_UNLIKELY(!table->col_names)) {
00195       /* All preceding column names are empty. */
00196       char* s = static_cast<char *>(mem_heap_zalloc(heap, table->n_def));
00197       table->col_names = s;
00198     }
00199 
00200     table->col_names = dict_add_col_name(table->col_names,
00201                  i, name, heap);
00202   }
00203 
00204   col = dict_table_get_nth_col(table, i);
00205 
00206   dict_mem_fill_column_struct(col, i, mtype, prtype, len);
00207 }
00208 
00209 
00210 /**********************************************************************/
00213 UNIV_INTERN
00214 void
00215 dict_mem_fill_column_struct(
00216 /*========================*/
00217   dict_col_t* column,   
00219   ulint   col_pos,  
00220   ulint   mtype,    
00221   ulint   prtype,   
00222   ulint   col_len)  
00223 {
00224 #ifndef UNIV_HOTBACKUP
00225   ulint mbminlen;
00226   ulint mbmaxlen;
00227 #endif /* !UNIV_HOTBACKUP */
00228 
00229   column->ind = (unsigned int) col_pos;
00230   column->ord_part = 0;
00231   column->mtype = (unsigned int) mtype;
00232   column->prtype = (unsigned int) prtype;
00233   column->len = (unsigned int) col_len;
00234 #ifndef UNIV_HOTBACKUP
00235         dtype_get_mblen(mtype, prtype, &mbminlen, &mbmaxlen);
00236   dict_col_set_mbminmaxlen(column, mbminlen, mbmaxlen);
00237 #endif /* !UNIV_HOTBACKUP */
00238 }
00239 
00240 /**********************************************************************/
00243 UNIV_INTERN
00244 dict_index_t*
00245 dict_mem_index_create(
00246 /*==================*/
00247   const char* table_name, 
00248   const char* index_name, 
00249   ulint   space,    
00252   ulint   type,   
00254   ulint   n_fields) 
00255 {
00256   dict_index_t* index;
00257   mem_heap_t* heap;
00258 
00259   ut_ad(table_name && index_name);
00260 
00261   heap = mem_heap_create(DICT_HEAP_SIZE);
00262   index = static_cast<dict_index_t *>(mem_heap_zalloc(heap, sizeof(dict_index_t)));
00263 
00264   dict_mem_fill_index_struct(index, heap, table_name, index_name,
00265            space, type, n_fields);
00266 
00267   return(index);
00268 }
00269 
00270 /**********************************************************************/
00273 UNIV_INTERN
00274 dict_foreign_t*
00275 dict_mem_foreign_create(void)
00276 /*=========================*/
00277 {
00278   dict_foreign_t* foreign;
00279   mem_heap_t* heap;
00280 
00281   heap = mem_heap_create(100);
00282 
00283   foreign = static_cast<dict_foreign_t *>(mem_heap_zalloc(heap, sizeof(dict_foreign_t)));
00284 
00285   foreign->heap = heap;
00286 
00287   return(foreign);
00288 }
00289 
00290 /**********************************************************************/
00294 UNIV_INTERN
00295 void
00296 dict_mem_index_add_field(
00297 /*=====================*/
00298   dict_index_t* index,    
00299   const char* name,   
00300   ulint   prefix_len) 
00303 {
00304   dict_field_t* field;
00305 
00306   ut_ad(index);
00307   ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
00308 
00309   index->n_def++;
00310 
00311   field = dict_index_get_nth_field(index, index->n_def - 1);
00312 
00313   field->name = name;
00314   field->prefix_len = (unsigned int) prefix_len;
00315 }
00316 
00317 /**********************************************************************/
00319 UNIV_INTERN
00320 void
00321 dict_mem_index_free(
00322 /*================*/
00323   dict_index_t* index)  
00324 {
00325   ut_ad(index);
00326   ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
00327 
00328   mem_heap_free(index->heap);
00329 }