diff options
Diffstat (limited to 'sql/ha_partition.cc')
-rw-r--r-- | sql/ha_partition.cc | 1236 |
1 files changed, 767 insertions, 469 deletions
diff --git a/sql/ha_partition.cc b/sql/ha_partition.cc index e146680e160..b4181fc6d7f 100644 --- a/sql/ha_partition.cc +++ b/sql/ha_partition.cc @@ -53,21 +53,20 @@ #pragma implementation // gcc: Class implementation #endif -#include "mysql_priv.h" +#include "sql_priv.h" +#include "sql_parse.h" // append_file_to_dir #include "create_options.h" #ifdef WITH_PARTITION_STORAGE_ENGINE #include "ha_partition.h" - -#include <mysql/plugin.h> +#include "sql_table.h" // tablename_to_filename +#include "key.h" +#include "sql_plugin.h" +#include "table.h" /* HA_DATA_PARTITION */ #include "debug_sync.h" static const char *ha_par_ext= ".par"; -#ifdef NOT_USED -static int free_share(PARTITION_SHARE * share); -static PARTITION_SHARE *get_share(const char *table_name, TABLE * table); -#endif /**************************************************************************** MODULE create/delete handler object @@ -284,12 +283,13 @@ void ha_partition::init_handler_variables() /* this allows blackhole to work properly */ - m_no_locks= 0; + m_num_locks= 0; m_part_info= NULL; m_create_handler= FALSE; m_is_sub_partitioned= 0; m_is_clone_of= NULL; m_clone_mem_root= NULL; + m_part_ids_sorted_by_num_of_records= NULL; #ifdef DONT_HAVE_TO_BE_INITALIZED m_start_key.flag= 0; @@ -324,9 +324,9 @@ ha_partition::~ha_partition() for (i= 0; i < m_tot_parts; i++) delete m_file[i]; } - - my_free(m_ordered_rec_buffer, MYF(MY_ALLOW_ZERO_PTR)); + my_free(m_ordered_rec_buffer); m_ordered_rec_buffer= NULL; + my_free(m_part_ids_sorted_by_num_of_records); clear_handler_file(); @@ -376,7 +376,7 @@ ha_partition::~ha_partition() The flag HA_READ_ORDER will be reset for the time being to indicate no ordered output is available from partition handler indexes. Later a merge sort will be performed using the underlying handlers. - 5) primary_key_is_clustered, has_transactions and low_byte_first is + 5) primary_key_is_clustered and has_transactions are calculated here. */ @@ -405,14 +405,14 @@ bool ha_partition::initialize_partition(MEM_ROOT *mem_root) else if (get_from_handler_file(table_share->normalized_path.str, mem_root, false)) { - my_message(ER_UNKNOWN_ERROR, "Failed to read from the .par file", MYF(0)); + my_error(ER_FAILED_READ_FROM_PAR_FILE, MYF(0)); DBUG_RETURN(1); } /* We create all underlying table handlers here. We do it in this special method to be able to report allocation errors. - Set up low_byte_first, primary_key_is_clustered and + Set up primary_key_is_clustered and has_transactions since they are called often in all kinds of places, other parameters are calculated on demand. Verify that all partitions have the same table_flags. @@ -543,9 +543,9 @@ int ha_partition::create_handler_files(const char *path, strxmov(name, path, ha_par_ext, NullS); strxmov(old_name, old_path, ha_par_ext, NullS); if ((action_flag == CHF_DELETE_FLAG && - my_delete(name, MYF(MY_WME))) || + mysql_file_delete(key_file_partition, name, MYF(MY_WME))) || (action_flag == CHF_RENAME_FLAG && - my_rename(old_name, name, MYF(MY_WME)))) + mysql_file_rename(key_file_partition, old_name, name, MYF(MY_WME)))) { DBUG_RETURN(TRUE); } @@ -630,8 +630,8 @@ int ha_partition::drop_partitions(const char *path) { List_iterator<partition_element> part_it(m_part_info->partitions); char part_name_buff[FN_REFLEN]; - uint no_parts= m_part_info->partitions.elements; - uint no_subparts= m_part_info->no_subparts; + uint num_parts= m_part_info->partitions.elements; + uint num_subparts= m_part_info->num_subparts; uint i= 0; uint name_variant; int ret_error; @@ -661,7 +661,7 @@ int ha_partition::drop_partitions(const char *path) do { partition_element *sub_elem= sub_it++; - part= i * no_subparts + j; + part= i * num_subparts + j; create_subpartition_name(part_name_buff, path, part_elem->partition_name, sub_elem->partition_name, name_variant); @@ -671,7 +671,7 @@ int ha_partition::drop_partitions(const char *path) error= ret_error; if (deactivate_ddl_log_entry(sub_elem->log_entry->entry_pos)) error= 1; - } while (++j < no_subparts); + } while (++j < num_subparts); } else { @@ -690,8 +690,8 @@ int ha_partition::drop_partitions(const char *path) else part_elem->part_state= PART_IS_DROPPED; } - } while (++i < no_parts); - VOID(sync_ddl_log()); + } while (++i < num_parts); + (void) sync_ddl_log(); DBUG_RETURN(error); } @@ -721,9 +721,9 @@ int ha_partition::rename_partitions(const char *path) List_iterator<partition_element> temp_it(m_part_info->temp_partitions); char part_name_buff[FN_REFLEN]; char norm_name_buff[FN_REFLEN]; - uint no_parts= m_part_info->partitions.elements; + uint num_parts= m_part_info->partitions.elements; uint part_count= 0; - uint no_subparts= m_part_info->no_subparts; + uint num_subparts= m_part_info->num_subparts; uint i= 0; uint j= 0; int error= 0; @@ -773,7 +773,7 @@ int ha_partition::rename_partitions(const char *path) error= 1; else sub_elem->log_entry= NULL; /* Indicate success */ - } while (++j < no_subparts); + } while (++j < num_subparts); } else { @@ -790,7 +790,7 @@ int ha_partition::rename_partitions(const char *path) part_elem->log_entry= NULL; /* Indicate success */ } } while (++i < temp_partitions); - VOID(sync_ddl_log()); + (void) sync_ddl_log(); } i= 0; do @@ -829,7 +829,7 @@ int ha_partition::rename_partitions(const char *path) do { sub_elem= sub_it++; - part= i * no_subparts + j; + part= i * num_subparts + j; create_subpartition_name(norm_name_buff, path, part_elem->partition_name, sub_elem->partition_name, @@ -842,7 +842,7 @@ int ha_partition::rename_partitions(const char *path) error= ret_error; else if (deactivate_ddl_log_entry(sub_elem->log_entry->entry_pos)) error= 1; - VOID(sync_ddl_log()); + (void) sync_ddl_log(); } file= m_new_file[part]; create_subpartition_name(part_name_buff, path, @@ -858,7 +858,7 @@ int ha_partition::rename_partitions(const char *path) error= 1; else sub_elem->log_entry= NULL; - } while (++j < no_subparts); + } while (++j < num_subparts); } else { @@ -873,7 +873,7 @@ int ha_partition::rename_partitions(const char *path) error= ret_error; else if (deactivate_ddl_log_entry(part_elem->log_entry->entry_pos)) error= 1; - VOID(sync_ddl_log()); + (void) sync_ddl_log(); } file= m_new_file[i]; create_partition_name(part_name_buff, path, @@ -890,8 +890,8 @@ int ha_partition::rename_partitions(const char *path) part_elem->log_entry= NULL; } } - } while (++i < no_parts); - VOID(sync_ddl_log()); + } while (++i < num_parts); + (void) sync_ddl_log(); DBUG_RETURN(error); } @@ -900,9 +900,12 @@ int ha_partition::rename_partitions(const char *path) #define ANALYZE_PARTS 2 #define CHECK_PARTS 3 #define REPAIR_PARTS 4 +#define ASSIGN_KEYCACHE_PARTS 5 +#define PRELOAD_KEYS_PARTS 6 static const char *opt_op_name[]= {NULL, - "optimize", "analyze", "check", "repair" }; + "optimize", "analyze", "check", "repair", + "assign_to_keycache", "preload_keys"}; /* Optimize table @@ -987,7 +990,44 @@ int ha_partition::repair(THD *thd, HA_CHECK_OPT *check_opt) DBUG_RETURN(handle_opt_partitions(thd, check_opt, REPAIR_PARTS)); } +/** + Assign to keycache + + @param thd Thread object + @param check_opt Check/analyze/repair/optimize options + + @return + @retval >0 Error + @retval 0 Success +*/ + +int ha_partition::assign_to_keycache(THD *thd, HA_CHECK_OPT *check_opt) +{ + DBUG_ENTER("ha_partition::assign_to_keycache"); + + DBUG_RETURN(handle_opt_partitions(thd, check_opt, ASSIGN_KEYCACHE_PARTS)); +} + + +/** + Preload to keycache + + @param thd Thread object + @param check_opt Check/analyze/repair/optimize options + + @return + @retval >0 Error + @retval 0 Success +*/ + +int ha_partition::preload_keys(THD *thd, HA_CHECK_OPT *check_opt) +{ + DBUG_ENTER("ha_partition::preload_keys"); + + DBUG_RETURN(handle_opt_partitions(thd, check_opt, PRELOAD_KEYS_PARTS)); +} + /* Handle optimize/analyze/check/repair of one partition @@ -1018,6 +1058,10 @@ static int handle_opt_part(THD *thd, HA_CHECK_OPT *check_opt, error= file->ha_check(thd, check_opt); else if (flag == REPAIR_PARTS) error= file->ha_repair(thd, check_opt); + else if (flag == ASSIGN_KEYCACHE_PARTS) + error= file->assign_to_keycache(thd, check_opt); + else if (flag == PRELOAD_KEYS_PARTS) + error= file->preload_keys(thd, check_opt); else { DBUG_ASSERT(FALSE); @@ -1045,7 +1089,7 @@ static bool print_admin_msg(THD* thd, const char* msg_type, va_list args; Protocol *protocol= thd->protocol; uint length, msg_length; - char msgbuf[HA_MAX_MSG_BUF]; + char msgbuf[MYSQL_ERRMSG_SIZE]; char name[SAFE_NAME_LEN*2+2]; va_start(args, fmt); @@ -1104,8 +1148,8 @@ int ha_partition::handle_opt_partitions(THD *thd, HA_CHECK_OPT *check_opt, uint flag) { List_iterator<partition_element> part_it(m_part_info->partitions); - uint no_parts= m_part_info->no_parts; - uint no_subparts= m_part_info->no_subparts; + uint num_parts= m_part_info->num_parts; + uint num_subparts= m_part_info->num_subparts; uint i= 0; int error; DBUG_ENTER("ha_partition::handle_opt_partitions"); @@ -1119,7 +1163,7 @@ int ha_partition::handle_opt_partitions(THD *thd, HA_CHECK_OPT *check_opt, it should only do named partitions, otherwise all partitions */ if (!(thd->lex->alter_info.flags & ALTER_ADMIN_PARTITION) || - part_elem->part_state == PART_CHANGED) + part_elem->part_state == PART_ADMIN) { if (m_is_sub_partitioned) { @@ -1129,16 +1173,9 @@ int ha_partition::handle_opt_partitions(THD *thd, HA_CHECK_OPT *check_opt, do { sub_elem= subpart_it++; - part= i * no_subparts + j; + part= i * num_subparts + j; DBUG_PRINT("info", ("Optimize subpartition %u (%s)", part, sub_elem->partition_name)); -#ifdef NOT_USED - if (print_admin_msg(thd, "note", table_share->db.str, table->alias, - opt_op_name[flag], - "Start to operate on subpartition %s", - sub_elem->partition_name)) - DBUG_RETURN(HA_ADMIN_INTERNAL_ERROR); -#endif if ((error= handle_opt_part(thd, check_opt, m_file[part], flag))) { /* print a line which partition the error belongs to */ @@ -1152,21 +1189,20 @@ int ha_partition::handle_opt_partitions(THD *thd, HA_CHECK_OPT *check_opt, "Subpartition %s returned error", sub_elem->partition_name); } + /* reset part_state for the remaining partitions */ + do + { + if (part_elem->part_state == PART_ADMIN) + part_elem->part_state= PART_NORMAL; + } while ((part_elem= part_it++)); DBUG_RETURN(error); } - } while (++j < no_subparts); + } while (++j < num_subparts); } else { DBUG_PRINT("info", ("Optimize partition %u (%s)", i, part_elem->partition_name)); -#ifdef NOT_USED - if (print_admin_msg(thd, "note", table_share->db.str, table->alias, - opt_op_name[flag], - "Start to operate on partition %s", - part_elem->partition_name)) - DBUG_RETURN(HA_ADMIN_INTERNAL_ERROR); -#endif if ((error= handle_opt_part(thd, check_opt, m_file[i], flag))) { /* print a line which partition the error belongs to */ @@ -1179,11 +1215,18 @@ int ha_partition::handle_opt_partitions(THD *thd, HA_CHECK_OPT *check_opt, opt_op_name[flag], "Partition %s returned error", part_elem->partition_name); } + /* reset part_state for the remaining partitions */ + do + { + if (part_elem->part_state == PART_ADMIN) + part_elem->part_state= PART_NORMAL; + } while ((part_elem= part_it++)); DBUG_RETURN(error); } } + part_elem->part_state= PART_NORMAL; } - } while (++i < no_parts); + } while (++i < num_parts); DBUG_RETURN(FALSE); } @@ -1301,15 +1344,15 @@ int ha_partition::prepare_new_partition(TABLE *tbl, assumes that external_lock() is last call that may fail here. Otherwise see description for cleanup_new_partition(). */ - if ((error= file->ha_external_lock(ha_thd(), m_lock_type))) + if ((error= file->ha_external_lock(ha_thd(), F_WRLCK))) goto error_external_lock; DBUG_PRINT("info", ("partition %s external locked", part_name)); DBUG_RETURN(0); error_external_lock: - VOID(file->ha_close()); + (void) file->ha_close(); error_open: - VOID(file->ha_delete_table(part_name)); + (void) file->ha_delete_table(part_name); error_create: DBUG_RETURN(error); } @@ -1406,10 +1449,10 @@ int ha_partition::change_partitions(HA_CREATE_INFO *create_info, List_iterator<partition_element> part_it(m_part_info->partitions); List_iterator <partition_element> t_it(m_part_info->temp_partitions); char part_name_buff[FN_REFLEN]; - uint no_parts= m_part_info->partitions.elements; - uint no_subparts= m_part_info->no_subparts; + uint num_parts= m_part_info->partitions.elements; + uint num_subparts= m_part_info->num_subparts; uint i= 0; - uint no_remain_partitions, part_count, orig_count; + uint num_remain_partitions, part_count, orig_count; handler **new_file_array; int error= 1; bool first; @@ -1425,7 +1468,7 @@ int ha_partition::change_partitions(HA_CREATE_INFO *create_info, part_name_buff))); m_reorged_parts= 0; if (!m_part_info->is_sub_partitioned()) - no_subparts= 1; + num_subparts= 1; /* Step 1: @@ -1434,7 +1477,7 @@ int ha_partition::change_partitions(HA_CREATE_INFO *create_info, */ if (temp_partitions) { - m_reorged_parts= temp_partitions * no_subparts; + m_reorged_parts= temp_partitions * num_subparts; } else { @@ -1444,9 +1487,9 @@ int ha_partition::change_partitions(HA_CREATE_INFO *create_info, if (part_elem->part_state == PART_CHANGED || part_elem->part_state == PART_REORGED_DROPPED) { - m_reorged_parts+= no_subparts; + m_reorged_parts+= num_subparts; } - } while (++i < no_parts); + } while (++i < num_parts); } if (m_reorged_parts && !(m_reorged_file= (handler**)sql_calloc(sizeof(handler*)* @@ -1461,10 +1504,10 @@ int ha_partition::change_partitions(HA_CREATE_INFO *create_info, Calculate number of partitions after change and allocate space for their handler references. */ - no_remain_partitions= 0; + num_remain_partitions= 0; if (temp_partitions) { - no_remain_partitions= no_parts * no_subparts; + num_remain_partitions= num_parts * num_subparts; } else { @@ -1477,17 +1520,17 @@ int ha_partition::change_partitions(HA_CREATE_INFO *create_info, part_elem->part_state == PART_TO_BE_ADDED || part_elem->part_state == PART_CHANGED) { - no_remain_partitions+= no_subparts; + num_remain_partitions+= num_subparts; } - } while (++i < no_parts); + } while (++i < num_parts); } if (!(new_file_array= (handler**)sql_calloc(sizeof(handler*)* - (2*(no_remain_partitions + 1))))) + (2*(num_remain_partitions + 1))))) { - mem_alloc_error(sizeof(handler*)*2*(no_remain_partitions+1)); + mem_alloc_error(sizeof(handler*)*2*(num_remain_partitions+1)); DBUG_RETURN(ER_OUTOFMEMORY); } - m_added_file= &new_file_array[no_remain_partitions + 1]; + m_added_file= &new_file_array[num_remain_partitions + 1]; /* Step 3: @@ -1506,9 +1549,9 @@ int ha_partition::change_partitions(HA_CREATE_INFO *create_info, part_elem->part_state == PART_REORGED_DROPPED) { memcpy((void*)&m_reorged_file[part_count], - (void*)&m_file[i*no_subparts], - sizeof(handler*)*no_subparts); - part_count+= no_subparts; + (void*)&m_file[i*num_subparts], + sizeof(handler*)*num_subparts); + part_count+= num_subparts; } else if (first && temp_partitions && part_elem->part_state == PART_TO_BE_ADDED) @@ -1523,11 +1566,11 @@ int ha_partition::change_partitions(HA_CREATE_INFO *create_info, ones used to be. */ first= FALSE; - DBUG_ASSERT(((i*no_subparts) + m_reorged_parts) <= m_file_tot_parts); - memcpy((void*)m_reorged_file, &m_file[i*no_subparts], + DBUG_ASSERT(((i*num_subparts) + m_reorged_parts) <= m_file_tot_parts); + memcpy((void*)m_reorged_file, &m_file[i*num_subparts], sizeof(handler*)*m_reorged_parts); } - } while (++i < no_parts); + } while (++i < num_parts); } /* @@ -1545,11 +1588,11 @@ int ha_partition::change_partitions(HA_CREATE_INFO *create_info, partition_element *part_elem= part_it++; if (part_elem->part_state == PART_NORMAL) { - DBUG_ASSERT(orig_count + no_subparts <= m_file_tot_parts); + DBUG_ASSERT(orig_count + num_subparts <= m_file_tot_parts); memcpy((void*)&new_file_array[part_count], (void*)&m_file[orig_count], - sizeof(handler*)*no_subparts); - part_count+= no_subparts; - orig_count+= no_subparts; + sizeof(handler*)*num_subparts); + part_count+= num_subparts; + orig_count+= num_subparts; } else if (part_elem->part_state == PART_CHANGED || part_elem->part_state == PART_TO_BE_ADDED) @@ -1565,16 +1608,16 @@ int ha_partition::change_partitions(HA_CREATE_INFO *create_info, mem_alloc_error(sizeof(handler)); DBUG_RETURN(ER_OUTOFMEMORY); } - } while (++j < no_subparts); + } while (++j < num_subparts); if (part_elem->part_state == PART_CHANGED) - orig_count+= no_subparts; + orig_count+= num_subparts; else if (temp_partitions && first) { - orig_count+= (no_subparts * temp_partitions); + orig_count+= (num_subparts * temp_partitions); first= FALSE; } } - } while (++i < no_parts); + } while (++i < num_parts); first= FALSE; /* Step 5: @@ -1611,7 +1654,7 @@ int ha_partition::change_partitions(HA_CREATE_INFO *create_info, part_elem->partition_name, sub_elem->partition_name, name_variant); - part= i * no_subparts + j; + part= i * num_subparts + j; DBUG_PRINT("info", ("Add subpartition %s", part_name_buff)); if ((error= prepare_new_partition(table, create_info, new_file_array[part], @@ -1622,7 +1665,7 @@ int ha_partition::change_partitions(HA_CREATE_INFO *create_info, DBUG_RETURN(error); } m_added_file[part_count++]= new_file_array[part]; - } while (++j < no_subparts); + } while (++j < num_subparts); } else { @@ -1641,7 +1684,7 @@ int ha_partition::change_partitions(HA_CREATE_INFO *create_info, m_added_file[part_count++]= new_file_array[i]; } } - } while (++i < no_parts); + } while (++i < num_parts); /* Step 6: @@ -1658,7 +1701,7 @@ int ha_partition::change_partitions(HA_CREATE_INFO *create_info, part_elem->part_state= PART_IS_CHANGED; else if (part_elem->part_state == PART_REORGED_DROPPED) part_elem->part_state= PART_TO_BE_DROPPED; - } while (++i < no_parts); + } while (++i < num_parts); for (i= 0; i < temp_partitions; i++) { partition_element *part_elem= t_it++; @@ -1707,9 +1750,9 @@ int ha_partition::copy_partitions(ulonglong * const copied, if (m_part_info->linear_hash_ind) { if (m_part_info->part_type == HASH_PARTITION) - set_linear_hash_mask(m_part_info, m_part_info->no_parts); + set_linear_hash_mask(m_part_info, m_part_info->num_parts); else - set_linear_hash_mask(m_part_info, m_part_info->no_subparts); + set_linear_hash_mask(m_part_info, m_part_info->num_subparts); } while (reorg_part < m_reorged_parts) @@ -1904,7 +1947,7 @@ uint ha_partition::del_ren_cre_table(const char *from, if (get_from_handler_file(from, ha_thd()->mem_root, false)) DBUG_RETURN(TRUE); DBUG_ASSERT(m_file_buffer); - DBUG_PRINT("enter", ("from: (%s) to: (%s)", from, to)); + DBUG_PRINT("enter", ("from: (%s) to: (%s)", from, to ? to : "(nil)")); name_buffer_ptr= m_name_buffer_ptr; file= m_file; if (to == NULL && table_arg == NULL) @@ -2010,7 +2053,7 @@ partition_element *ha_partition::find_partition_element(uint part_id) uint curr_part_id= 0; List_iterator_fast <partition_element> part_it(m_part_info->partitions); - for (i= 0; i < m_part_info->no_parts; i++) + for (i= 0; i < m_part_info->num_parts; i++) { partition_element *part_elem; part_elem= part_it++; @@ -2018,7 +2061,7 @@ partition_element *ha_partition::find_partition_element(uint part_id) { uint j; List_iterator_fast <partition_element> sub_it(part_elem->subpartitions); - for (j= 0; j < m_part_info->no_subparts; j++) + for (j= 0; j < m_part_info->num_subparts; j++) { part_elem= sub_it++; if (part_id == curr_part_id++) @@ -2029,8 +2072,7 @@ partition_element *ha_partition::find_partition_element(uint part_id) return part_elem; } DBUG_ASSERT(0); - my_error(ER_OUT_OF_RESOURCES, MYF(0)); - current_thd->fatal_error(); // Abort + my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR)); return NULL; } @@ -2141,7 +2183,7 @@ bool ha_partition::create_handler_file(const char *name) { partition_element *part_elem, *subpart_elem; uint i, j, part_name_len, subpart_name_len; - uint tot_partition_words, tot_name_len, no_parts; + uint tot_partition_words, tot_name_len, num_parts; uint tot_parts= 0; uint tot_len_words, tot_len_byte, chksum, tot_name_words; char *name_buffer_ptr; @@ -2154,11 +2196,11 @@ bool ha_partition::create_handler_file(const char *name) List_iterator_fast <partition_element> part_it(m_part_info->partitions); DBUG_ENTER("create_handler_file"); - no_parts= m_part_info->partitions.elements; - DBUG_PRINT("info", ("table name = %s, no_parts = %u", name, - no_parts)); + num_parts= m_part_info->partitions.elements; + DBUG_PRINT("info", ("table name = %s, num_parts = %u", name, + num_parts)); tot_name_len= 0; - for (i= 0; i < no_parts; i++) + for (i= 0; i < num_parts; i++) { part_elem= part_it++; if (part_elem->part_state != PART_NORMAL && @@ -2176,7 +2218,7 @@ bool ha_partition::create_handler_file(const char *name) else { List_iterator_fast <partition_element> sub_it(part_elem->subpartitions); - for (j= 0; j < m_part_info->no_subparts; j++) + for (j= 0; j < m_part_info->num_subparts; j++) { subpart_elem= sub_it++; tablename_to_filename(subpart_elem->partition_name, @@ -2215,7 +2257,7 @@ bool ha_partition::create_handler_file(const char *name) name_buffer_ptr= (char*) (engine_array + tot_partition_words * PAR_WORD_SIZE + PAR_WORD_SIZE); part_it.rewind(); - for (i= 0; i < no_parts; i++) + for (i= 0; i < num_parts; i++) { part_elem= part_it++; if (part_elem->part_state != PART_NORMAL && @@ -2233,7 +2275,7 @@ bool ha_partition::create_handler_file(const char *name) else { List_iterator_fast <partition_element> sub_it(part_elem->subpartitions); - for (j= 0; j < m_part_info->no_subparts; j++) + for (j= 0; j < m_part_info->num_subparts; j++) { subpart_elem= sub_it++; tablename_to_filename(part_elem->partition_name, part_name, @@ -2264,15 +2306,16 @@ bool ha_partition::create_handler_file(const char *name) to be used at open, delete_table and rename_table */ fn_format(file_name, name, "", ha_par_ext, MY_APPEND_EXT); - if ((file= my_create(file_name, CREATE_MODE, O_RDWR | O_TRUNC, - MYF(MY_WME))) >= 0) + if ((file= mysql_file_create(key_file_partition, + file_name, CREATE_MODE, O_RDWR | O_TRUNC, + MYF(MY_WME))) >= 0) { - result= my_write(file, (uchar *) file_buffer, tot_len_byte, - MYF(MY_WME | MY_NABP)) != 0; + result= mysql_file_write(file, (uchar *) file_buffer, tot_len_byte, + MYF(MY_WME | MY_NABP)) != 0; /* Write connection information (for federatedx engine) */ part_it.rewind(); - for (i= 0; i < no_parts && !result; i++) + for (i= 0; i < num_parts && !result; i++) { uchar buffer[4]; part_elem= part_it++; @@ -2286,7 +2329,7 @@ bool ha_partition::create_handler_file(const char *name) break; } } - VOID(my_close(file, MYF(0))); + (void) mysql_file_close(file, MYF(0)); } else result= TRUE; @@ -2383,7 +2426,7 @@ bool ha_partition::new_handlers_from_part_info(MEM_ROOT *mem_root) } m_file_tot_parts= m_tot_parts; bzero((char*) m_file, alloc_len); - DBUG_ASSERT(m_part_info->no_parts > 0); + DBUG_ASSERT(m_part_info->num_parts > 0); i= 0; part_count= 0; @@ -2396,7 +2439,7 @@ bool ha_partition::new_handlers_from_part_info(MEM_ROOT *mem_root) part_elem= part_it++; if (m_is_sub_partitioned) { - for (j= 0; j < m_part_info->no_subparts; j++) + for (j= 0; j < m_part_info->num_subparts; j++) { if (!(m_file[part_count++]= get_new_handler(table_share, mem_root, part_elem->engine_type))) @@ -2413,7 +2456,7 @@ bool ha_partition::new_handlers_from_part_info(MEM_ROOT *mem_root) DBUG_PRINT("info", ("engine_type: %u", (uint) ha_legacy_type(part_elem->engine_type))); } - } while (++i < m_part_info->no_parts); + } while (++i < m_part_info->num_parts); if (part_elem->engine_type == myisam_hton) { DBUG_PRINT("info", ("MyISAM")); @@ -2453,18 +2496,19 @@ bool ha_partition::read_par_file(const char *name) DBUG_RETURN(false); fn_format(buff, name, "", ha_par_ext, MY_APPEND_EXT); - /* Following could be done with my_stat to read in whole file */ - if ((file= my_open(buff, O_RDONLY | O_SHARE, MYF(MY_WME))) < 0) - DBUG_RETURN(true); - if (my_read(file, (uchar *) & buff[0], PAR_WORD_SIZE, MYF(MY_NABP))) + /* Following could be done with mysql_file_stat to read in whole file */ + if ((file= mysql_file_open(key_file_partition, + buff, O_RDONLY | O_SHARE, MYF(0))) < 0) + DBUG_RETURN(TRUE); + if (mysql_file_read(file, (uchar *) &buff[0], PAR_WORD_SIZE, MYF(MY_NABP))) goto err1; len_words= uint4korr(buff); len_bytes= PAR_WORD_SIZE * len_words; - if (my_seek(file, 0, MY_SEEK_SET, MYF(0)) == MY_FILEPOS_ERROR) + if (mysql_file_seek(file, 0, MY_SEEK_SET, MYF(0)) == MY_FILEPOS_ERROR) goto err1; if (!(file_buffer= (char*) alloc_root(&m_mem_root, len_bytes))) goto err1; - if (my_read(file, (uchar *) file_buffer, len_bytes, MYF(MY_NABP))) + if (mysql_file_read(file, (uchar *) file_buffer, len_bytes, MYF(MY_NABP))) goto err2; chksum= 0; @@ -2513,12 +2557,12 @@ bool ha_partition::read_par_file(const char *name) m_connect_string[i]= connect_string; } - VOID(my_close(file, MYF(0))); + (void) mysql_file_close(file, MYF(0)); DBUG_RETURN(false); err2: err1: - VOID(my_close(file, MYF(0))); + (void) mysql_file_close(file, MYF(0)); DBUG_RETURN(true); } @@ -2620,12 +2664,11 @@ bool ha_partition::get_from_handler_file(const char *name, MEM_ROOT *mem_root, A destructor for partition-specific TABLE_SHARE data. */ -void ha_data_partition_destroy(void *ha_data) +void ha_data_partition_destroy(HA_DATA_PARTITION* ha_part_data) { - if (ha_data) + if (ha_part_data) { - HA_DATA_PARTITION *ha_part_data= (HA_DATA_PARTITION*) ha_data; - pthread_mutex_destroy(&ha_part_data->LOCK_auto_inc); + mysql_mutex_destroy(&ha_part_data->LOCK_auto_inc); } } @@ -2700,6 +2743,16 @@ int ha_partition::open(const char *name, int mode, uint test_if_locked) m_start_key.key= (const uchar*)ptr; } } + if (!m_part_ids_sorted_by_num_of_records) + { + if (!(m_part_ids_sorted_by_num_of_records= + (uint32*) my_malloc(m_tot_parts * sizeof(uint32), MYF(MY_WME)))) + DBUG_RETURN(error); + uint32 i; + /* Initialize it with all partition ids. */ + for (i= 0; i < m_tot_parts; i++) + m_part_ids_sorted_by_num_of_records[i]= i; + } /* Initialize the bitmap we use to minimize ha_start_bulk_insert calls */ if (bitmap_init(&m_bulk_insert_started, NULL, m_tot_parts + 1, FALSE)) @@ -2755,7 +2808,7 @@ int ha_partition::open(const char *name, int mode, uint test_if_locked) if ((error= (*file)->ha_open(table, name_buff, mode, test_if_locked))) goto err_handler; bzero(&table->s->connect_string, sizeof(LEX_STRING)); - m_no_locks+= (*file)->lock_count(); + m_num_locks+= (*file)->lock_count(); name_buffer_ptr+= strlen(name_buffer_ptr) + 1; } while (*(++file)); } @@ -2805,31 +2858,33 @@ int ha_partition::open(const char *name, int mode, uint test_if_locked) goto err_handler; /* - Use table_share->ha_data to share auto_increment_value among all handlers - for the same table. + Use table_share->ha_part_data to share auto_increment_value among + all handlers for the same table. */ if (is_not_tmp_table) - pthread_mutex_lock(&table_share->mutex); - if (!table_share->ha_data) + mysql_mutex_lock(&table_share->LOCK_ha_data); + if (!table_share->ha_part_data) { - HA_DATA_PARTITION *ha_data; /* currently only needed for auto_increment */ - table_share->ha_data= ha_data= (HA_DATA_PARTITION*) + table_share->ha_part_data= (HA_DATA_PARTITION*) alloc_root(&table_share->mem_root, sizeof(HA_DATA_PARTITION)); - if (!ha_data) + if (!table_share->ha_part_data) { if (is_not_tmp_table) - pthread_mutex_unlock(&table_share->mutex); + mysql_mutex_unlock(&table_share->LOCK_ha_data); goto err_handler; } - DBUG_PRINT("info", ("table_share->ha_data 0x%p", ha_data)); - bzero(ha_data, sizeof(HA_DATA_PARTITION)); - table_share->ha_data_destroy= ha_data_partition_destroy; - VOID(pthread_mutex_init(&ha_data->LOCK_auto_inc, MY_MUTEX_INIT_FAST)); + DBUG_PRINT("info", ("table_share->ha_part_data 0x%p", + table_share->ha_part_data)); + bzero(table_share->ha_part_data, sizeof(HA_DATA_PARTITION)); + table_share->ha_part_data_destroy= ha_data_partition_destroy; + mysql_mutex_init(key_PARTITION_LOCK_auto_inc, + &table_share->ha_part_data->LOCK_auto_inc, + MY_MUTEX_INIT_FAST); } if (is_not_tmp_table) - pthread_mutex_unlock(&table_share->mutex); + mysql_mutex_unlock(&table_share->LOCK_ha_data); /* Some handlers update statistics as part of the open call. This will in some cases corrupt the statistics of the partition handler and thus @@ -3142,8 +3197,8 @@ int ha_partition::start_stmt(THD *thd, thr_lock_type lock_type) uint ha_partition::lock_count() const { DBUG_ENTER("ha_partition::lock_count"); - DBUG_PRINT("info", ("m_no_locks %d", m_no_locks)); - DBUG_RETURN(m_no_locks); + DBUG_PRINT("info", ("m_num_locks %d", m_num_locks)); + DBUG_RETURN(m_num_locks); } @@ -3280,10 +3335,9 @@ int ha_partition::write_row(uchar * buf) longlong func_value; bool have_auto_increment= table->next_number_field && buf == table->record[0]; my_bitmap_map *old_map; - HA_DATA_PARTITION *ha_data= (HA_DATA_PARTITION*) table_share->ha_data; THD *thd= ha_thd(); timestamp_auto_set_type saved_timestamp_type= table->timestamp_field_type; - ulong saved_sql_mode= thd->variables.sql_mode; + ulonglong saved_sql_mode= thd->variables.sql_mode; bool saved_auto_inc_field_not_null= table->auto_increment_field_not_null; #ifdef NOT_NEEDED uchar *rec0= m_rec0; @@ -3302,8 +3356,8 @@ int ha_partition::write_row(uchar * buf) */ if (have_auto_increment) { - if (!ha_data->auto_inc_initialized && - !table->s->next_number_keypart) + if (!table_share->ha_part_data->auto_inc_initialized && + !table_share->next_number_keypart) { /* If auto_increment in table_share is not initialized, start by @@ -3473,8 +3527,8 @@ int ha_partition::update_row(const uchar *old_data, uchar *new_data) exit: /* - If updating an auto_increment column, update - table_share->ha_data->next_auto_inc_val if needed. + if updating an auto_increment column, update + table_share->ha_part_data->next_auto_inc_val if needed. (not to be used if auto_increment on secondary field in a multi-column index) mysql_update does not set table->next_number_field, so we use @@ -3487,8 +3541,7 @@ exit: bitmap_is_set(table->write_set, table->found_next_number_field->field_index)) { - HA_DATA_PARTITION *ha_data= (HA_DATA_PARTITION*) table_share->ha_data; - if (!ha_data->auto_inc_initialized) + if (!table_share->ha_part_data->auto_inc_initialized) info(HA_STATUS_AUTO); set_auto_increment_if_higher(table->found_next_number_field); } @@ -3569,33 +3622,119 @@ int ha_partition::delete_row(const uchar *buf) int ha_partition::delete_all_rows() { int error; - bool truncate= FALSE; handler **file; - THD *thd= ha_thd(); DBUG_ENTER("ha_partition::delete_all_rows"); - if (thd->lex->sql_command == SQLCOM_TRUNCATE) - { - HA_DATA_PARTITION *ha_data= (HA_DATA_PARTITION*) table_share->ha_data; - lock_auto_increment(); - ha_data->next_auto_inc_val= 0; - ha_data->auto_inc_initialized= FALSE; - unlock_auto_increment(); - truncate= TRUE; - } file= m_file; do { if ((error= (*file)->ha_delete_all_rows())) DBUG_RETURN(error); - /* Ignore the error */ - if (truncate) - (void) (*file)->ha_reset_auto_increment(0); } while (*(++file)); DBUG_RETURN(0); } +/** + Manually truncate the table. + + @retval 0 Success. + @retval > 0 Error code. +*/ + +int ha_partition::truncate() +{ + int error; + handler **file; + DBUG_ENTER("ha_partition::truncate"); + + /* + TRUNCATE also means resetting auto_increment. Hence, reset + it so that it will be initialized again at the next use. + */ + lock_auto_increment(); + table_share->ha_part_data->next_auto_inc_val= 0; + table_share->ha_part_data->auto_inc_initialized= FALSE; + unlock_auto_increment(); + + file= m_file; + do + { + if ((error= (*file)->ha_truncate())) + DBUG_RETURN(error); + } while (*(++file)); + DBUG_RETURN(0); +} + + +/** + Truncate a set of specific partitions. + + @remark Auto increment value will be truncated in that partition as well! + + ALTER TABLE t TRUNCATE PARTITION ... +*/ + +int ha_partition::truncate_partition(Alter_info *alter_info, bool *binlog_stmt) +{ + int error= 0; + List_iterator<partition_element> part_it(m_part_info->partitions); + uint num_parts= m_part_info->num_parts; + uint num_subparts= m_part_info->num_subparts; + uint i= 0; + DBUG_ENTER("ha_partition::truncate_partition"); + + /* Only binlog when it starts any call to the partitions handlers */ + *binlog_stmt= false; + + if (set_part_state(alter_info, m_part_info, PART_ADMIN)) + DBUG_RETURN(HA_ERR_NO_PARTITION_FOUND); + + /* + TRUNCATE also means resetting auto_increment. Hence, reset + it so that it will be initialized again at the next use. + */ + lock_auto_increment(); + table_share->ha_part_data->next_auto_inc_val= 0; + table_share->ha_part_data->auto_inc_initialized= FALSE; + unlock_auto_increment(); + + *binlog_stmt= true; + + do + { + partition_element *part_elem= part_it++; + if (part_elem->part_state == PART_ADMIN) + { + if (m_is_sub_partitioned) + { + List_iterator<partition_element> + subpart_it(part_elem->subpartitions); + partition_element *sub_elem; + uint j= 0, part; + do + { + sub_elem= subpart_it++; + part= i * num_subparts + j; + DBUG_PRINT("info", ("truncate subpartition %u (%s)", + part, sub_elem->partition_name)); + if ((error= m_file[part]->ha_truncate())) + break; + } while (++j < num_subparts); + } + else + { + DBUG_PRINT("info", ("truncate partition %u (%s)", i, + part_elem->partition_name)); + error= m_file[i]->ha_truncate(); + } + part_elem->part_state= PART_NORMAL; + } + } while (!error && (++i < num_parts)); + DBUG_RETURN(error); +} + + /* Start a large batch of insert rows @@ -4044,12 +4183,6 @@ void ha_partition::position(const uchar *record) if (pad_length) memset((ref + PARTITION_BYTES_IN_POS + file->ref_length), 0, pad_length); -#ifdef SUPPORTING_PARTITION_OVER_DIFFERENT_ENGINES -#ifdef HAVE_valgrind - bzero(ref + PARTITION_BYTES_IN_POS + ref_length, - max_ref_length-ref_length); -#endif /* HAVE_valgrind */ -#endif DBUG_VOID_RETURN; } @@ -4345,10 +4478,9 @@ int ha_partition::index_read_map(uchar *buf, const uchar *key, int ha_partition::common_index_read(uchar *buf, bool have_start_key) { int error; - uint key_len; + uint UNINIT_VAR(key_len); /* used if have_start_key==TRUE */ bool reverse_order= FALSE; DBUG_ENTER("ha_partition::common_index_read"); - LINT_INIT(key_len); /* used if have_start_key==TRUE */ DBUG_PRINT("info", ("m_ordered %u m_ordered_scan_ong %u have_start_key %u", m_ordered, m_ordered_scan_ongoing, have_start_key)); @@ -5194,6 +5326,24 @@ int ha_partition::handle_ordered_prev(uchar *buf) and read_time calls */ +/** + Helper function for sorting according to number of rows in descending order. +*/ + +int ha_partition::compare_number_of_records(ha_partition *me, + const uint32 *a, + const uint32 *b) +{ + handler **file= me->m_file; + /* Note: sorting in descending order! */ + if (file[*a]->stats.records > file[*b]->stats.records) + return -1; + if (file[*a]->stats.records < file[*b]->stats.records) + return 1; + return 0; +} + + /* General method to gather info from handler @@ -5261,27 +5411,29 @@ int ha_partition::handle_ordered_prev(uchar *buf) int ha_partition::info(uint flag) { + uint no_lock_flag= flag & HA_STATUS_NO_LOCK; + uint extra_var_flag= flag & HA_STATUS_VARIABLE_EXTRA; DBUG_ENTER("ha_partition::info"); if (flag & HA_STATUS_AUTO) { bool auto_inc_is_first_in_idx= (table_share->next_number_keypart == 0); - HA_DATA_PARTITION *ha_data= (HA_DATA_PARTITION*) table_share->ha_data; DBUG_PRINT("info", ("HA_STATUS_AUTO")); if (!table->found_next_number_field) stats.auto_increment_value= 0; - else if (ha_data->auto_inc_initialized) + else if (table_share->ha_part_data->auto_inc_initialized) { lock_auto_increment(); - stats.auto_increment_value= ha_data->next_auto_inc_val; + stats.auto_increment_value= table_share->ha_part_data->next_auto_inc_val; unlock_auto_increment(); } else { lock_auto_increment(); /* to avoid two concurrent initializations, check again when locked */ - if (ha_data->auto_inc_initialized) - stats.auto_increment_value= ha_data->next_auto_inc_val; + if (table_share->ha_part_data->auto_inc_initialized) + stats.auto_increment_value= + table_share->ha_part_data->next_auto_inc_val; else { handler *file, **file_array; @@ -5292,7 +5444,7 @@ int ha_partition::info(uint flag) do { file= *file_array; - file->info(HA_STATUS_AUTO); + file->info(HA_STATUS_AUTO | no_lock_flag); set_if_bigger(auto_increment_value, file->stats.auto_increment_value); } while (*(++file_array)); @@ -5301,10 +5453,11 @@ int ha_partition::info(uint flag) stats.auto_increment_value= auto_increment_value; if (auto_inc_is_first_in_idx) { - set_if_bigger(ha_data->next_auto_inc_val, auto_increment_value); - ha_data->auto_inc_initialized= TRUE; + set_if_bigger(table_share->ha_part_data->next_auto_inc_val, + auto_increment_value); + table_share->ha_part_data->auto_inc_initialized= TRUE; DBUG_PRINT("info", ("initializing next_auto_inc_val to %lu", - (ulong) ha_data->next_auto_inc_val)); + (ulong) table_share->ha_part_data->next_auto_inc_val)); } } unlock_auto_increment(); @@ -5345,7 +5498,7 @@ int ha_partition::info(uint flag) if (bitmap_is_set(&(m_part_info->used_partitions), (file_array - m_file))) { file= *file_array; - file->info(HA_STATUS_VARIABLE); + file->info(HA_STATUS_VARIABLE | no_lock_flag | extra_var_flag); stats.records+= file->stats.records; stats.deleted+= file->stats.deleted; stats.data_file_length+= file->stats.data_file_length; @@ -5427,7 +5580,7 @@ int ha_partition::info(uint flag) if (!(flag & HA_STATUS_VARIABLE) || !bitmap_is_set(&(m_part_info->used_partitions), (file_array - m_file))) - file->info(HA_STATUS_VARIABLE); + file->info(HA_STATUS_VARIABLE | no_lock_flag | extra_var_flag); if (file->stats.records > max_records) { max_records= file->stats.records; @@ -5435,9 +5588,18 @@ int ha_partition::info(uint flag) } i++; } while (*(++file_array)); + /* + Sort the array of part_ids by number of records in + in descending order. + */ + my_qsort2((void*) m_part_ids_sorted_by_num_of_records, + m_tot_parts, + sizeof(uint32), + (qsort2_cmp) compare_number_of_records, + this); file= m_file[handler_instance]; - file->info(HA_STATUS_CONST); + file->info(HA_STATUS_CONST | no_lock_flag); stats.block_size= file->stats.block_size; stats.create_time= file->stats.create_time; ref_length= m_ref_length; @@ -5453,7 +5615,7 @@ int ha_partition::info(uint flag) Note: all engines does not support HA_STATUS_ERRKEY, so set errkey. */ file->errkey= errkey; - file->info(HA_STATUS_ERRKEY); + file->info(HA_STATUS_ERRKEY | no_lock_flag); errkey= file->errkey; } if (flag & HA_STATUS_TIME) @@ -5470,7 +5632,7 @@ int ha_partition::info(uint flag) do { file= *file_array; - file->info(HA_STATUS_TIME); + file->info(HA_STATUS_TIME | no_lock_flag); if (file->stats.update_time > stats.update_time) stats.update_time= file->stats.update_time; } while (*(++file_array)); @@ -5479,12 +5641,12 @@ int ha_partition::info(uint flag) } -void ha_partition::get_dynamic_partition_info(PARTITION_INFO *stat_info, +void ha_partition::get_dynamic_partition_info(PARTITION_STATS *stat_info, uint part_id) { handler *file= m_file[part_id]; file->info(HA_STATUS_CONST | HA_STATUS_TIME | HA_STATUS_VARIABLE | - HA_STATUS_NO_LOCK); + HA_STATUS_VARIABLE_EXTRA | HA_STATUS_NO_LOCK); stat_info->records= file->stats.records; stat_info->mean_rec_length= file->stats.mean_rec_length; @@ -5502,34 +5664,35 @@ void ha_partition::get_dynamic_partition_info(PARTITION_INFO *stat_info, } -/* - General function to prepare handler for certain behavior +/** + General function to prepare handler for certain behavior. - SYNOPSIS - extra() + @param[in] operation operation to execute operation Operation type for extra call - RETURN VALUE - >0 Error code - 0 Success + @return status + @retval 0 success + @retval >0 error code + + @detail - DESCRIPTION extra() is called whenever the server wishes to send a hint to the storage engine. The MyISAM engine implements the most hints. We divide the parameters into the following categories: - 1) Parameters used by most handlers - 2) Parameters used by some non-MyISAM handlers - 3) Parameters used only by MyISAM - 4) Parameters only used by temporary tables for query processing - 5) Parameters only used by MyISAM internally - 6) Parameters not used at all - 7) Parameters only used by federated tables for query processing - 8) Parameters only used by NDB + 1) Operations used by most handlers + 2) Operations used by some non-MyISAM handlers + 3) Operations used only by MyISAM + 4) Operations only used by temporary tables for query processing + 5) Operations only used by MyISAM internally + 6) Operations not used at all + 7) Operations only used by federated tables for query processing + 8) Operations only used by NDB + 9) Operations only used by MERGE The partition handler need to handle category 1), 2) and 3). - 1) Parameters used by most handlers + 1) Operations used by most handlers ----------------------------------- HA_EXTRA_RESET: This option is used by most handlers and it resets the handler state @@ -5568,7 +5731,7 @@ void ha_partition::get_dynamic_partition_info(PARTITION_INFO *stat_info, ensure disk based tables are flushed at end of query execution. Currently is never used. - 2) Parameters used by some non-MyISAM handlers + 2) Operations used by some non-MyISAM handlers ---------------------------------------------- HA_EXTRA_KEYREAD_PRESERVE_FIELDS: This is a strictly InnoDB feature that is more or less undocumented. @@ -5587,7 +5750,7 @@ void ha_partition::get_dynamic_partition_info(PARTITION_INFO *stat_info, SQL constructs. Not used by MyISAM. - 3) Parameters used only by MyISAM + 3) Operations used only by MyISAM --------------------------------- HA_EXTRA_NORMAL: Only used in MyISAM to reset quick mode, not implemented by any other @@ -5716,7 +5879,7 @@ void ha_partition::get_dynamic_partition_info(PARTITION_INFO *stat_info, Only used by MyISAM, called when altering table, closing tables to enforce a reopen of the table files. - 4) Parameters only used by temporary tables for query processing + 4) Operations only used by temporary tables for query processing ---------------------------------------------------------------- HA_EXTRA_RESET_STATE: Same as reset() except that buffers are not released. If there is @@ -5747,7 +5910,7 @@ void ha_partition::get_dynamic_partition_info(PARTITION_INFO *stat_info, tables used in query processing. Not handled by partition handler. - 5) Parameters only used by MyISAM internally + 5) Operations only used by MyISAM internally -------------------------------------------- HA_EXTRA_REINIT_CACHE: This call reinitializes the READ CACHE described above if there is one @@ -5782,19 +5945,19 @@ void ha_partition::get_dynamic_partition_info(PARTITION_INFO *stat_info, HA_EXTRA_CHANGE_KEY_TO_UNIQUE: Only used by MyISAM, never called. - 6) Parameters not used at all + 6) Operations not used at all ----------------------------- HA_EXTRA_KEY_CACHE: HA_EXTRA_NO_KEY_CACHE: This parameters are no longer used and could be removed. - 7) Parameters only used by federated tables for query processing + 7) Operations only used by federated tables for query processing ---------------------------------------------------------------- HA_EXTRA_INSERT_WITH_UPDATE: Inform handler that an "INSERT...ON DUPLICATE KEY UPDATE" will be executed. This condition is unset by HA_EXTRA_NO_IGNORE_DUP_KEY. - 8) Parameters only used by NDB + 8) Operations only used by NDB ------------------------------ HA_EXTRA_DELETE_CANNOT_BATCH: HA_EXTRA_UPDATE_CANNOT_BATCH: @@ -5802,6 +5965,14 @@ void ha_partition::get_dynamic_partition_info(PARTITION_INFO *stat_info, and should perform them immediately. This may be needed when table has AFTER DELETE/UPDATE triggers which access to subject table. These flags are reset by the handler::extra(HA_EXTRA_RESET) call. + + 9) Operations only used by MERGE + ------------------------------ + HA_EXTRA_ADD_CHILDREN_LIST: + HA_EXTRA_ATTACH_CHILDREN: + HA_EXTRA_IS_ATTACHED_CHILDREN: + HA_EXTRA_DETACH_CHILDREN: + Special actions for MERGE tables. Ignore. */ int ha_partition::extra(enum ha_extra_function operation) @@ -5842,7 +6013,7 @@ int ha_partition::extra(enum ha_extra_function operation) if (!m_extra_cache) m_extra_cache_part_id= m_part_spec.start_part; DBUG_ASSERT(m_extra_cache_part_id == m_part_spec.start_part); - VOID(m_file[m_part_spec.start_part]->extra(HA_EXTRA_PREPARE_FOR_UPDATE)); + (void) m_file[m_part_spec.start_part]->extra(HA_EXTRA_PREPARE_FOR_UPDATE); } break; case HA_EXTRA_NORMAL: @@ -5918,13 +6089,22 @@ int ha_partition::extra(enum ha_extra_function operation) /* Category 7), used by federated handlers */ case HA_EXTRA_INSERT_WITH_UPDATE: DBUG_RETURN(loop_extra(operation)); - /* Category 8) Parameters only used by NDB */ + /* Category 8) Operations only used by NDB */ case HA_EXTRA_DELETE_CANNOT_BATCH: case HA_EXTRA_UPDATE_CANNOT_BATCH: { /* Currently only NDB use the *_CANNOT_BATCH */ break; } + /* Category 9) Operations only used by MERGE */ + case HA_EXTRA_ADD_CHILDREN_LIST: + case HA_EXTRA_ATTACH_CHILDREN: + case HA_EXTRA_IS_ATTACHED_CHILDREN: + case HA_EXTRA_DETACH_CHILDREN: + { + /* Special actions for MERGE tables. Ignore. */ + break; + } /* http://dev.mysql.com/doc/refman/5.1/en/partitioning-limitations.html says we no longer support logging to partitioned tables, so we fail @@ -5954,7 +6134,7 @@ int ha_partition::extra(enum ha_extra_function operation) 0 Success DESCRIPTION - Called at end of each statement to reste buffers + Called at end of each statement to reset buffers */ int ha_partition::reset(void) @@ -6111,13 +6291,14 @@ void ha_partition::late_extra_cache(uint partition_id) if (m_extra_cache) { if (m_extra_cache_size == 0) - VOID(file->extra(HA_EXTRA_CACHE)); + (void) file->extra(HA_EXTRA_CACHE); else - VOID(file->extra_opt(HA_EXTRA_CACHE, m_extra_cache_size)); + (void) file->extra_opt(HA_EXTRA_CACHE, m_extra_cache_size); } if (m_extra_prepare_for_update) { - VOID(file->extra(HA_EXTRA_PREPARE_FOR_UPDATE)); + DBUG_ASSERT(m_extra_cache); + (void) file->extra(HA_EXTRA_PREPARE_FOR_UPDATE); } m_extra_cache_part_id= partition_id; DBUG_VOID_RETURN; @@ -6143,7 +6324,7 @@ void ha_partition::late_extra_no_cache(uint partition_id) if (!m_extra_cache && !m_extra_prepare_for_update) DBUG_VOID_RETURN; file= m_file[partition_id]; - VOID(file->extra(HA_EXTRA_NO_CACHE)); + (void) file->extra(HA_EXTRA_NO_CACHE); DBUG_ASSERT(partition_id == m_extra_cache_part_id); m_extra_cache_part_id= NO_CURRENT_PART_ID; DBUG_VOID_RETURN; @@ -6171,21 +6352,73 @@ const key_map *ha_partition::keys_to_use_for_scanning() DBUG_RETURN(m_file[0]->keys_to_use_for_scanning()); } -#define MAX_PARTS_FOR_OPTIMIZER_CALLS 10 -/* - Prepare start variables for estimating optimizer costs. - @param[out] num_used_parts Number of partitions after pruning. - @param[out] check_min_num Number of partitions to call. - @param[out] first first used partition. +/** + Minimum number of rows to base optimizer estimate on. +*/ + +ha_rows ha_partition::min_rows_for_estimate() +{ + uint i, max_used_partitions, tot_used_partitions; + DBUG_ENTER("ha_partition::min_rows_for_estimate"); + + tot_used_partitions= bitmap_bits_set(&m_part_info->used_partitions); + DBUG_ASSERT(tot_used_partitions); + + /* + Allow O(log2(tot_partitions)) increase in number of used partitions. + This gives O(tot_rows/log2(tot_partitions)) rows to base the estimate on. + I.e when the total number of partitions doubles, allow one more + partition to be checked. + */ + i= 2; + max_used_partitions= 1; + while (i < m_tot_parts) + { + max_used_partitions++; + i= i << 1; + } + if (max_used_partitions > tot_used_partitions) + max_used_partitions= tot_used_partitions; + + /* stats.records is already updated by the info(HA_STATUS_VARIABLE) call. */ + DBUG_PRINT("info", ("max_used_partitions: %u tot_rows: %lu", + max_used_partitions, + (ulong) stats.records)); + DBUG_PRINT("info", ("tot_used_partitions: %u min_rows_to_check: %lu", + tot_used_partitions, + (ulong) stats.records * max_used_partitions + / tot_used_partitions)); + DBUG_RETURN(stats.records * max_used_partitions / tot_used_partitions); +} + + +/** + Get the biggest used partition. + + Starting at the N:th biggest partition and skips all non used + partitions, returning the biggest used partition found + + @param[in,out] part_index Skip the *part_index biggest partitions + + @return The biggest used partition with index not lower than *part_index. + @retval NO_CURRENT_PART_ID No more partition used. + @retval != NO_CURRENT_PART_ID partition id of biggest used partition with + index >= *part_index supplied. Note that + *part_index will be updated to the next + partition index to use. */ -void ha_partition::partitions_optimizer_call_preparations(uint *first, - uint *num_used_parts, - uint *check_min_num) + +uint ha_partition::get_biggest_used_partition(uint *part_index) { - *first= bitmap_get_first_set(&(m_part_info->used_partitions)); - *num_used_parts= bitmap_bits_set(&(m_part_info->used_partitions)); - *check_min_num= min(MAX_PARTS_FOR_OPTIMIZER_CALLS, *num_used_parts); + uint part_id; + while ((*part_index) < m_tot_parts) + { + part_id= m_part_ids_sorted_by_num_of_records[(*part_index)++]; + if (bitmap_is_set(&m_part_info->used_partitions, part_id)) + return part_id; + } + return NO_CURRENT_PART_ID; } @@ -6201,115 +6434,107 @@ void ha_partition::partitions_optimizer_call_preparations(uint *first, double ha_partition::scan_time() { - double scan_time= 0.0; - uint first, part_id, num_used_parts, check_min_num, partitions_called= 0; + double scan_time= 0; + handler **file; DBUG_ENTER("ha_partition::scan_time"); - partitions_optimizer_call_preparations(&first, &num_used_parts, &check_min_num); - for (part_id= first; partitions_called < num_used_parts ; part_id++) - { - if (!bitmap_is_set(&(m_part_info->used_partitions), part_id)) - continue; - scan_time+= m_file[part_id]->scan_time(); - partitions_called++; - if (partitions_called >= check_min_num && scan_time != 0.0) - { - DBUG_RETURN(scan_time * - (double) num_used_parts / (double) partitions_called); - } - } + for (file= m_file; *file; file++) + if (bitmap_is_set(&(m_part_info->used_partitions), (file - m_file))) + scan_time+= (*file)->scan_time(); DBUG_RETURN(scan_time); } -/* - Estimate rows for records_in_range or estimate_rows_upper_bound. +/** + Find number of records in a range. + @param inx Index number + @param min_key Start of range + @param max_key End of range - @param is_records_in_range call records_in_range instead of - estimate_rows_upper_bound. - @param inx (only for records_in_range) index to use. - @param min_key (only for records_in_range) start of range. - @param max_key (only for records_in_range) end of range. + @return Number of rows in range. - @return Number of rows or HA_POS_ERROR. + Given a starting key, and an ending key estimate the number of rows that + will exist between the two. max_key may be empty which in case determine + if start_key matches any rows. */ -ha_rows ha_partition::estimate_rows(bool is_records_in_range, uint inx, - key_range *min_key, key_range *max_key) + +ha_rows ha_partition::records_in_range(uint inx, key_range *min_key, + key_range *max_key) { - ha_rows rows, estimated_rows= 0; - uint first, part_id, num_used_parts, check_min_num, partitions_called= 0; + ha_rows min_rows_to_check, rows, estimated_rows=0, checked_rows= 0; + uint partition_index= 0, part_id; DBUG_ENTER("ha_partition::records_in_range"); - partitions_optimizer_call_preparations(&first, &num_used_parts, &check_min_num); - for (part_id= first; partitions_called < num_used_parts ; part_id++) + min_rows_to_check= min_rows_for_estimate(); + + while ((part_id= get_biggest_used_partition(&partition_index)) + != NO_CURRENT_PART_ID) { - if (!bitmap_is_set(&(m_part_info->used_partitions), part_id)) - continue; - if (is_records_in_range) - rows= m_file[part_id]->records_in_range(inx, min_key, max_key); - else - rows= m_file[part_id]->estimate_rows_upper_bound(); + rows= m_file[part_id]->records_in_range(inx, min_key, max_key); + + DBUG_PRINT("info", ("part %u match %lu rows of %lu", part_id, (ulong) rows, + (ulong) m_file[part_id]->stats.records)); + if (rows == HA_POS_ERROR) DBUG_RETURN(HA_POS_ERROR); estimated_rows+= rows; - partitions_called++; - if (partitions_called >= check_min_num && estimated_rows) + checked_rows+= m_file[part_id]->stats.records; + /* + Returning 0 means no rows can be found, so we must continue + this loop as long as we have estimated_rows == 0. + Also many engines return 1 to indicate that there may exist + a matching row, we do not normalize this by dividing by number of + used partitions, but leave it to be returned as a sum, which will + reflect that we will need to scan each partition's index. + + Note that this statistics may not always be correct, so we must + continue even if the current partition has 0 rows, since we might have + deleted rows from the current partition, or inserted to the next + partition. + */ + if (estimated_rows && checked_rows && + checked_rows >= min_rows_to_check) { - DBUG_RETURN(estimated_rows * num_used_parts / partitions_called); + DBUG_PRINT("info", + ("records_in_range(inx %u): %lu (%lu * %lu / %lu)", + inx, + (ulong) (estimated_rows * stats.records / checked_rows), + (ulong) estimated_rows, + (ulong) stats.records, + (ulong) checked_rows)); + DBUG_RETURN(estimated_rows * stats.records / checked_rows); } } + DBUG_PRINT("info", ("records_in_range(inx %u): %lu", + inx, + (ulong) estimated_rows)); DBUG_RETURN(estimated_rows); } -/* - Find number of records in a range - - SYNOPSIS - records_in_range() - inx Index number - min_key Start of range - max_key End of range - - RETURN VALUE - Number of rows in range - - DESCRIPTION - Given a starting key, and an ending key estimate the number of rows that - will exist between the two. end_key may be empty which in case determine - if start_key matches any rows. - - Called from opt_range.cc by check_quick_keys(). - - monty: MUST be called for each range and added. - Note that MySQL will assume that if this returns 0 there is no - matching rows for the range! -*/ - -ha_rows ha_partition::records_in_range(uint inx, key_range *min_key, - key_range *max_key) -{ - DBUG_ENTER("ha_partition::records_in_range"); - - DBUG_RETURN(estimate_rows(TRUE, inx, min_key, max_key)); -} - - -/* - Estimate upper bound of number of rows - - SYNOPSIS - estimate_rows_upper_bound() +/** + Estimate upper bound of number of rows. - RETURN VALUE - Number of rows + @return Number of rows. */ ha_rows ha_partition::estimate_rows_upper_bound() { + ha_rows rows, tot_rows= 0; + handler **file= m_file; DBUG_ENTER("ha_partition::estimate_rows_upper_bound"); - DBUG_RETURN(estimate_rows(FALSE, 0, NULL, NULL)); + do + { + if (bitmap_is_set(&(m_part_info->used_partitions), (file - m_file))) + { + rows= (*file)->estimate_rows_upper_bound(); + if (rows == HA_POS_ERROR) + DBUG_RETURN(HA_POS_ERROR); + tot_rows+= rows; + } + } while (*(++file)); + DBUG_RETURN(tot_rows); } @@ -6444,12 +6669,14 @@ enum row_type ha_partition::get_row_type() const void ha_partition::print_error(int error, myf errflag) { + THD *thd= ha_thd(); DBUG_ENTER("ha_partition::print_error"); /* Should probably look for my own errors first */ DBUG_PRINT("enter", ("error: %d", error)); - if (error == HA_ERR_NO_PARTITION_FOUND) + if ((error == HA_ERR_NO_PARTITION_FOUND) && + ! (thd->lex->alter_info.flags & ALTER_TRUNCATE_PARTITION)) m_part_info->print_no_partition_found(table); else { @@ -6493,9 +6720,42 @@ bool ha_partition::get_error_message(int error, String *buf) */ uint ha_partition::alter_table_flags(uint flags) { + uint flags_to_return, flags_to_check; DBUG_ENTER("ha_partition::alter_table_flags"); - DBUG_RETURN(ht->alter_table_flags(flags) | - m_file[0]->alter_table_flags(flags)); + + flags_to_return= ht->alter_table_flags(flags); + flags_to_return|= m_file[0]->alter_table_flags(flags); + + /* + If one partition fails we must be able to revert the change for the other, + already altered, partitions. So both ADD and DROP can only be supported in + pairs. + */ + flags_to_check= HA_INPLACE_ADD_INDEX_NO_READ_WRITE; + flags_to_check|= HA_INPLACE_DROP_INDEX_NO_READ_WRITE; + if ((flags_to_return & flags_to_check) != flags_to_check) + flags_to_return&= ~flags_to_check; + flags_to_check= HA_INPLACE_ADD_UNIQUE_INDEX_NO_READ_WRITE; + flags_to_check|= HA_INPLACE_DROP_UNIQUE_INDEX_NO_READ_WRITE; + if ((flags_to_return & flags_to_check) != flags_to_check) + flags_to_return&= ~flags_to_check; + flags_to_check= HA_INPLACE_ADD_PK_INDEX_NO_READ_WRITE; + flags_to_check|= HA_INPLACE_DROP_PK_INDEX_NO_READ_WRITE; + if ((flags_to_return & flags_to_check) != flags_to_check) + flags_to_return&= ~flags_to_check; + flags_to_check= HA_INPLACE_ADD_INDEX_NO_WRITE; + flags_to_check|= HA_INPLACE_DROP_INDEX_NO_WRITE; + if ((flags_to_return & flags_to_check) != flags_to_check) + flags_to_return&= ~flags_to_check; + flags_to_check= HA_INPLACE_ADD_UNIQUE_INDEX_NO_WRITE; + flags_to_check|= HA_INPLACE_DROP_UNIQUE_INDEX_NO_WRITE; + if ((flags_to_return & flags_to_check) != flags_to_check) + flags_to_return&= ~flags_to_check; + flags_to_check= HA_INPLACE_ADD_PK_INDEX_NO_WRITE; + flags_to_check|= HA_INPLACE_DROP_PK_INDEX_NO_WRITE; + if ((flags_to_return & flags_to_check) != flags_to_check) + flags_to_return&= ~flags_to_check; + DBUG_RETURN(flags_to_return); } @@ -6523,42 +6783,202 @@ bool ha_partition::check_if_incompatible_data(HA_CREATE_INFO *create_info, /** - Support of fast or online add/drop index + Helper class for [final_]add_index, see handler.h */ -int ha_partition::add_index(TABLE *table_arg, KEY *key_info, uint num_of_keys) + +class ha_partition_add_index : public handler_add_index { - handler **file; +public: + handler_add_index **add_array; + ha_partition_add_index(TABLE* table_arg, KEY* key_info_arg, + uint num_of_keys_arg) + : handler_add_index(table_arg, key_info_arg, num_of_keys_arg) + {} + ~ha_partition_add_index() {} +}; + + +/** + Support of in-place add/drop index + + @param table_arg Table to add index to + @param key_info Struct over the new keys to add + @param num_of_keys Number of keys to add + @param[out] add Data to be submitted with final_add_index + + @return Operation status + @retval 0 Success + @retval != 0 Failure (error code returned, and all operations rollbacked) +*/ + +int ha_partition::add_index(TABLE *table_arg, KEY *key_info, uint num_of_keys, + handler_add_index **add) +{ + uint i; int ret= 0; + THD *thd= ha_thd(); + ha_partition_add_index *part_add_index; + DBUG_ENTER("ha_partition::add_index"); /* There has already been a check in fix_partition_func in mysql_alter_table before this call, which checks for unique/primary key violations of the partitioning function. So no need for extra check here. */ - for (file= m_file; *file; file++) - if ((ret= (*file)->add_index(table_arg, key_info, num_of_keys))) + + /* + This will be freed at the end of the statement. + And destroyed at final_add_index. (Sql_alloc does not free in delete). + */ + part_add_index= new (thd->mem_root) + ha_partition_add_index(table_arg, key_info, num_of_keys); + if (!part_add_index) + DBUG_RETURN(HA_ERR_OUT_OF_MEM); + part_add_index->add_array= (handler_add_index **) + thd->alloc(sizeof(void *) * m_tot_parts); + if (!part_add_index->add_array) + { + delete part_add_index; + DBUG_RETURN(HA_ERR_OUT_OF_MEM); + } + + for (i= 0; i < m_tot_parts; i++) + { + if ((ret= m_file[i]->add_index(table_arg, key_info, num_of_keys, + &part_add_index->add_array[i]))) goto err; - return ret; + } + *add= part_add_index; + DBUG_RETURN(ret); err: - if (file > m_file) + /* Rollback all prepared partitions. i - 1 .. 0 */ + while (i) { - uint *key_numbers= (uint*) ha_thd()->alloc(sizeof(uint) * num_of_keys); - KEY *old_key_info= table_arg->key_info; - uint i; - /* Use the newly added key_info as table->key_info to remove them. */ - for (i= 0; i < num_of_keys; i++) - key_numbers[i]= i; - table_arg->key_info= key_info; - while (--file >= m_file) + i--; + (void) m_file[i]->final_add_index(part_add_index->add_array[i], false); + } + delete part_add_index; + DBUG_RETURN(ret); +} + + +/** + Second phase of in-place add index. + + @param add Info from add_index + @param commit Should we commit or rollback the add_index operation + + @return Operation status + @retval 0 Success + @retval != 0 Failure (error code returned) + + @note If commit is false, index changes are rolled back by dropping the + added indexes. If commit is true, nothing is done as the indexes + were already made active in ::add_index() +*/ + +int ha_partition::final_add_index(handler_add_index *add, bool commit) +{ + ha_partition_add_index *part_add_index; + uint i; + int ret= 0; + + DBUG_ENTER("ha_partition::final_add_index"); + + if (!add) + { + DBUG_ASSERT(!commit); + DBUG_RETURN(0); + } + part_add_index= static_cast<class ha_partition_add_index*>(add); + + for (i= 0; i < m_tot_parts; i++) + { + if ((ret= m_file[i]->final_add_index(part_add_index->add_array[i], commit))) + goto err; + DBUG_EXECUTE_IF("ha_partition_fail_final_add_index", { + /* Simulate a failure by rollback the second partition */ + if (m_tot_parts > 1) + { + i++; + m_file[i]->final_add_index(part_add_index->add_array[i], false); + /* Set an error that is specific to ha_partition. */ + ret= HA_ERR_NO_PARTITION_FOUND; + goto err; + } + }); + } + delete part_add_index; + DBUG_RETURN(ret); +err: + uint j; + uint *key_numbers= NULL; + KEY *old_key_info= NULL; + uint num_of_keys= 0; + int error; + + /* How could this happen? Needed to create a covering test case :) */ + DBUG_ASSERT(ret == HA_ERR_NO_PARTITION_FOUND); + + if (i > 0) + { + num_of_keys= part_add_index->num_of_keys; + key_numbers= (uint*) ha_thd()->alloc(sizeof(uint) * num_of_keys); + if (!key_numbers) { - (void) (*file)->prepare_drop_index(table_arg, key_numbers, num_of_keys); - (void) (*file)->final_drop_index(table_arg); + sql_print_error("Failed with error handling of adding index:\n" + "committing index failed, and when trying to revert " + "already committed partitions we failed allocating\n" + "memory for the index for table '%s'", + table_share->table_name.str); + DBUG_RETURN(HA_ERR_OUT_OF_MEM); } - table_arg->key_info= old_key_info; + old_key_info= table->key_info; + /* + Use the newly added key_info as table->key_info to remove them. + Note that this requires the subhandlers to use name lookup of the + index. They must use given table->key_info[key_number], they cannot + use their local view of the keys, since table->key_info only include + the indexes to be removed here. + */ + for (j= 0; j < num_of_keys; j++) + key_numbers[j]= j; + table->key_info= part_add_index->key_info; } - return ret; -} + for (j= 0; j < m_tot_parts; j++) + { + if (j < i) + { + /* Remove the newly added index */ + error= m_file[j]->prepare_drop_index(table, key_numbers, num_of_keys); + if (error || m_file[j]->final_drop_index(table)) + { + sql_print_error("Failed with error handling of adding index:\n" + "committing index failed, and when trying to revert " + "already committed partitions we failed removing\n" + "the index for table '%s' partition nr %d", + table_share->table_name.str, j); + } + } + else if (j > i) + { + /* Rollback non finished partitions */ + if (m_file[j]->final_add_index(part_add_index->add_array[j], false)) + { + /* How could this happen? */ + sql_print_error("Failed with error handling of adding index:\n" + "Rollback of add_index failed for table\n" + "'%s' partition nr %d", + table_share->table_name.str, j); + } + } + } + if (i > 0) + table->key_info= old_key_info; + delete part_add_index; + DBUG_RETURN(ret); +} int ha_partition::prepare_drop_index(TABLE *table_arg, uint *key_num, uint num_of_keys) @@ -6737,11 +7157,10 @@ int ha_partition::reset_auto_increment(ulonglong value) { handler **file= m_file; int res; - HA_DATA_PARTITION *ha_data= (HA_DATA_PARTITION*) table_share->ha_data; DBUG_ENTER("ha_partition::reset_auto_increment"); lock_auto_increment(); - ha_data->auto_inc_initialized= FALSE; - ha_data->next_auto_inc_val= 0; + table_share->ha_part_data->auto_inc_initialized= FALSE; + table_share->ha_part_data->next_auto_inc_val= 0; do { if ((res= (*file)->ha_reset_auto_increment(value)) != 0) @@ -6755,7 +7174,7 @@ int ha_partition::reset_auto_increment(ulonglong value) /** This method is called by update_auto_increment which in turn is called by the individual handlers as part of write_row. We use the - table_share->ha_data->next_auto_inc_val, or search all + table_share->ha_part_data->next_auto_inc_val, or search all partitions for the highest auto_increment_value if not initialized or if auto_increment field is a secondary part of a key, we must search every partition when holding a mutex to be sure of correctness. @@ -6808,13 +7227,12 @@ void ha_partition::get_auto_increment(ulonglong offset, ulonglong increment, else { THD *thd= ha_thd(); - HA_DATA_PARTITION *ha_data= (HA_DATA_PARTITION*) table_share->ha_data; /* This is initialized in the beginning of the first write_row call. */ - DBUG_ASSERT(ha_data->auto_inc_initialized); + DBUG_ASSERT(table_share->ha_part_data->auto_inc_initialized); /* - Get a lock for handling the auto_increment in table_share->ha_data + Get a lock for handling the auto_increment in table_share->ha_part_data for avoiding two concurrent statements getting the same number. */ @@ -6833,16 +7251,17 @@ void ha_partition::get_auto_increment(ulonglong offset, ulonglong increment, if (!auto_increment_safe_stmt_log_lock && thd->lex->sql_command != SQLCOM_INSERT && mysql_bin_log.is_open() && - !thd->current_stmt_binlog_row_based && - (thd->options & OPTION_BIN_LOG)) + !thd->is_current_stmt_binlog_format_row() && + (thd->variables.option_bits & OPTION_BIN_LOG)) { DBUG_PRINT("info", ("locking auto_increment_safe_stmt_log_lock")); auto_increment_safe_stmt_log_lock= TRUE; } /* this gets corrected (for offset/increment) in update_auto_increment */ - *first_value= ha_data->next_auto_inc_val; - ha_data->next_auto_inc_val+= nb_desired_values * increment; + *first_value= table_share->ha_part_data->next_auto_inc_val; + table_share->ha_part_data->next_auto_inc_val+= + nb_desired_values * increment; unlock_auto_increment(); DBUG_PRINT("info", ("*first_value: %lu", (ulong) *first_value)); @@ -6862,10 +7281,9 @@ void ha_partition::release_auto_increment() } else if (next_insert_id) { - HA_DATA_PARTITION *ha_data= (HA_DATA_PARTITION*) table_share->ha_data; ulonglong next_auto_inc_val; lock_auto_increment(); - next_auto_inc_val= ha_data->next_auto_inc_val; + next_auto_inc_val= table_share->ha_part_data->next_auto_inc_val; /* If the current auto_increment values is lower than the reserved value, and the reserved value was reserved by this thread, @@ -6880,10 +7298,10 @@ void ha_partition::release_auto_increment() with SET INSERT_ID, i.e. forced/non generated values. */ if (thd->auto_inc_intervals_forced.maximum() < next_insert_id) - ha_data->next_auto_inc_val= next_insert_id; + table_share->ha_part_data->next_auto_inc_val= next_insert_id; } - DBUG_PRINT("info", ("ha_data->next_auto_inc_val: %lu", - (ulong) ha_data->next_auto_inc_val)); + DBUG_PRINT("info", ("table_share->ha_part_data->next_auto_inc_val: %lu", + (ulong) table_share->ha_part_data->next_auto_inc_val)); /* Unlock the multi row statement lock taken in get_auto_increment */ if (auto_increment_safe_stmt_log_lock) @@ -6983,127 +7401,6 @@ int ha_partition::indexes_are_disabled(void) } -/**************************************************************************** - MODULE Partition Share -****************************************************************************/ -/* - Service routines for ... methods. -------------------------------------------------------------------------- - Variables for partition share methods. A hash used to track open tables. - A mutex for the hash table and an init variable to check if hash table - is initialized. - There is also a constant ending of the partition handler file name. -*/ - -#ifdef NOT_USED -static HASH partition_open_tables; -static pthread_mutex_t partition_mutex; -static int partition_init= 0; - - -/* - Function we use in the creation of our hash to get key. -*/ - -static uchar *partition_get_key(PARTITION_SHARE *share, size_t *length, - my_bool not_used __attribute__ ((unused))) -{ - *length= share->table_name_length; - return (uchar *) share->table_name; -} - -/* - Example of simple lock controls. The "share" it creates is structure we - will pass to each partition handler. Do you have to have one of these? - Well, you have pieces that are used for locking, and they are needed to - function. -*/ - -static PARTITION_SHARE *get_share(const char *table_name, TABLE *table) -{ - PARTITION_SHARE *share; - uint length; - char *tmp_name; - - /* - So why does this exist? There is no way currently to init a storage - engine. - Innodb and BDB both have modifications to the server to allow them to - do this. Since you will not want to do this, this is probably the next - best method. - */ - if (!partition_init) - { - /* Hijack a mutex for init'ing the storage engine */ - pthread_mutex_lock(&LOCK_mysql_create_db); - if (!partition_init) - { - partition_init++; - VOID(pthread_mutex_init(&partition_mutex, MY_MUTEX_INIT_FAST)); - (void) hash_init(&partition_open_tables, system_charset_info, 32, 0, 0, - (hash_get_key) partition_get_key, 0, 0); - } - pthread_mutex_unlock(&LOCK_mysql_create_db); - } - pthread_mutex_lock(&partition_mutex); - length= (uint) strlen(table_name); - - if (!(share= (PARTITION_SHARE *) hash_search(&partition_open_tables, - (uchar *) table_name, length))) - { - if (!(share= (PARTITION_SHARE *) - my_multi_malloc(MYF(MY_WME | MY_ZEROFILL), - &share, (uint) sizeof(*share), - &tmp_name, (uint) length + 1, NullS))) - { - pthread_mutex_unlock(&partition_mutex); - return NULL; - } - - share->use_count= 0; - share->table_name_length= length; - share->table_name= tmp_name; - strmov(share->table_name, table_name); - if (my_hash_insert(&partition_open_tables, (uchar *) share)) - goto error; - thr_lock_init(&share->lock); - pthread_mutex_init(&share->mutex, MY_MUTEX_INIT_FAST); - } - share->use_count++; - pthread_mutex_unlock(&partition_mutex); - - return share; - -error: - pthread_mutex_unlock(&partition_mutex); - my_free((uchar*) share, MYF(0)); - - return NULL; -} - - -/* - Free lock controls. We call this whenever we close a table. If the table - had the last reference to the share then we free memory associated with - it. -*/ - -static int free_share(PARTITION_SHARE *share) -{ - pthread_mutex_lock(&partition_mutex); - if (!--share->use_count) - { - hash_delete(&partition_open_tables, (uchar *) share); - thr_lock_delete(&share->lock); - pthread_mutex_destroy(&share->mutex); - my_free((uchar*) share, MYF(0)); - } - pthread_mutex_unlock(&partition_mutex); - - return 0; -} -#endif /* NOT_USED */ - struct st_mysql_storage_engine partition_storage_engine= { MYSQL_HANDLERTON_INTERFACE_VERSION }; @@ -7120,7 +7417,8 @@ mysql_declare_plugin(partition) 0x0100, /* 1.0 */ NULL, /* status variables */ NULL, /* system variables */ - NULL /* config options */ + NULL, /* config options */ + 0, /* flags */ } mysql_declare_plugin_end; maria_declare_plugin(partition) |