diff options
Diffstat (limited to 'sql/records.cc')
-rw-r--r-- | sql/records.cc | 123 |
1 files changed, 54 insertions, 69 deletions
diff --git a/sql/records.cc b/sql/records.cc index 8a152cda4c2..817caada8e2 100644 --- a/sql/records.cc +++ b/sql/records.cc @@ -26,7 +26,7 @@ Functions for easy reading of records, possible through a cache */ -#include <my_global.h> +#include "mariadb.h" #include "records.h" #include "sql_priv.h" #include "records.h" @@ -77,20 +77,19 @@ bool init_read_record_idx(READ_RECORD *info, THD *thd, TABLE *table, bzero((char*) info,sizeof(*info)); info->thd= thd; info->table= table; - info->record= table->record[0]; info->print_error= print_error; info->unlock_row= rr_unlock_row; table->status=0; /* And it's always found */ if (!table->file->inited && - (error= table->file->ha_index_init(idx, 1))) + unlikely(error= table->file->ha_index_init(idx, 1))) { if (print_error) table->file->print_error(error, MYF(0)); } - /* read_record will be changed to rr_index in rr_index_first */ - info->read_record= reverse ? rr_index_last : rr_index_first; + /* read_record_func will be changed to rr_index in rr_index_first */ + info->read_record_func= reverse ? rr_index_last : rr_index_first; DBUG_RETURN(error != 0); } @@ -194,7 +193,6 @@ bool init_read_record(READ_RECORD *info,THD *thd, TABLE *table, bzero((char*) info,sizeof(*info)); info->thd=thd; info->table=table; - info->forms= &info->table; /* Only one table */ info->addon_field= addon_field; if ((table->s->tmp_table == INTERNAL_TMP_TABLE) && @@ -204,19 +202,17 @@ bool init_read_record(READ_RECORD *info,THD *thd, TABLE *table, if (addon_field) { info->rec_buf= (uchar*) filesort->addon_buf.str; - info->ref_length= filesort->addon_buf.length; + info->ref_length= (uint)filesort->addon_buf.length; info->unpack= filesort->unpack; } else { empty_record(table); - info->record= table->record[0]; - info->ref_length= table->file->ref_length; + info->ref_length= (uint)table->file->ref_length; } info->select=select; info->print_error=print_error; info->unlock_row= rr_unlock_row; - info->ignore_not_found_rows= 0; table->status= 0; /* Rows are always found */ tempfile= 0; @@ -228,13 +224,13 @@ bool init_read_record(READ_RECORD *info,THD *thd, TABLE *table, if (tempfile && !(select && select->quick)) { DBUG_PRINT("info",("using rr_from_tempfile")); - info->read_record= (addon_field ? - rr_unpack_from_tempfile : rr_from_tempfile); + info->read_record_func= + addon_field ? rr_unpack_from_tempfile : rr_from_tempfile; info->io_cache= tempfile; reinit_io_cache(info->io_cache,READ_CACHE,0L,0,0); info->ref_pos=table->file->ref; if (!table->file->inited) - if (table->file->ha_rnd_init_with_error(0)) + if (unlikely(table->file->ha_rnd_init_with_error(0))) DBUG_RETURN(1); /* @@ -259,32 +255,32 @@ bool init_read_record(READ_RECORD *info,THD *thd, TABLE *table, if (! init_rr_cache(thd, info)) { DBUG_PRINT("info",("using rr_from_cache")); - info->read_record=rr_from_cache; + info->read_record_func= rr_from_cache; } } } else if (select && select->quick) { DBUG_PRINT("info",("using rr_quick")); - info->read_record=rr_quick; + info->read_record_func= rr_quick; } else if (filesort && filesort->record_pointers) { DBUG_PRINT("info",("using record_pointers")); - if (table->file->ha_rnd_init_with_error(0)) + if (unlikely(table->file->ha_rnd_init_with_error(0))) DBUG_RETURN(1); info->cache_pos= filesort->record_pointers; info->cache_end= (info->cache_pos+ filesort->return_rows * info->ref_length); - info->read_record= (addon_field ? - rr_unpack_from_buffer : rr_from_pointers); + info->read_record_func= + addon_field ? rr_unpack_from_buffer : rr_from_pointers; } else if (table->file->keyread_enabled()) { int error; - info->read_record= rr_index_first; + info->read_record_func= rr_index_first; if (!table->file->inited && - (error= table->file->ha_index_init(table->file->keyread, 1))) + unlikely((error= table->file->ha_index_init(table->file->keyread, 1)))) { if (print_error) table->file->print_error(error, MYF(0)); @@ -294,8 +290,8 @@ bool init_read_record(READ_RECORD *info,THD *thd, TABLE *table, else { DBUG_PRINT("info",("using rr_sequential")); - info->read_record=rr_sequential; - if (table->file->ha_rnd_init_with_error(1)) + info->read_record_func= rr_sequential; + if (unlikely(table->file->ha_rnd_init_with_error(1))) DBUG_RETURN(1); /* We can use record cache if we don't update dynamic length tables */ if (!table->no_cache && @@ -327,7 +323,7 @@ void end_read_record(READ_RECORD *info) { if (info->table->is_created()) (void) info->table->file->extra(HA_EXTRA_NO_CACHE); - if (info->read_record != rr_quick) // otherwise quick_range does it + if (info->read_record_func != rr_quick) // otherwise quick_range does it (void) info->table->file->ha_index_or_rnd_end(); info->table=0; } @@ -372,11 +368,8 @@ static int rr_quick(READ_RECORD *info) int tmp; while ((tmp= info->select->quick->get_next())) { - if (info->thd->killed || (tmp != HA_ERR_RECORD_DELETED)) - { - tmp= rr_handle_error(info, tmp); - break; - } + tmp= rr_handle_error(info, tmp); + break; } return tmp; } @@ -405,8 +398,8 @@ static int rr_index_first(READ_RECORD *info) return tmp; } - tmp= info->table->file->ha_index_first(info->record); - info->read_record= rr_index; + tmp= info->table->file->ha_index_first(info->record()); + info->read_record_func= rr_index; if (tmp) tmp= rr_handle_error(info, tmp); return tmp; @@ -428,8 +421,8 @@ static int rr_index_first(READ_RECORD *info) static int rr_index_last(READ_RECORD *info) { - int tmp= info->table->file->ha_index_last(info->record); - info->read_record= rr_index_desc; + int tmp= info->table->file->ha_index_last(info->record()); + info->read_record_func= rr_index_desc; if (tmp) tmp= rr_handle_error(info, tmp); return tmp; @@ -454,7 +447,7 @@ static int rr_index_last(READ_RECORD *info) static int rr_index(READ_RECORD *info) { - int tmp= info->table->file->ha_index_next(info->record); + int tmp= info->table->file->ha_index_next(info->record()); if (tmp) tmp= rr_handle_error(info, tmp); return tmp; @@ -479,7 +472,7 @@ static int rr_index(READ_RECORD *info) static int rr_index_desc(READ_RECORD *info) { - int tmp= info->table->file->ha_index_prev(info->record); + int tmp= info->table->file->ha_index_prev(info->record()); if (tmp) tmp= rr_handle_error(info, tmp); return tmp; @@ -489,17 +482,10 @@ static int rr_index_desc(READ_RECORD *info) int rr_sequential(READ_RECORD *info) { int tmp; - while ((tmp= info->table->file->ha_rnd_next(info->record))) + while ((tmp= info->table->file->ha_rnd_next(info->record()))) { - /* - rnd_next can return RECORD_DELETED for MyISAM when one thread is - reading and another deleting without locks. - */ - if (info->thd->killed || (tmp != HA_ERR_RECORD_DELETED)) - { - tmp= rr_handle_error(info, tmp); - break; - } + tmp= rr_handle_error(info, tmp); + break; } return tmp; } @@ -512,11 +498,10 @@ static int rr_from_tempfile(READ_RECORD *info) { if (my_b_read(info->io_cache,info->ref_pos,info->ref_length)) return -1; /* End of file */ - if (!(tmp= info->table->file->ha_rnd_pos(info->record,info->ref_pos))) + if (!(tmp= info->table->file->ha_rnd_pos(info->record(), info->ref_pos))) break; /* The following is extremely unlikely to happen */ - if (tmp == HA_ERR_RECORD_DELETED || - (tmp == HA_ERR_KEY_NOT_FOUND && info->ignore_not_found_rows)) + if (tmp == HA_ERR_KEY_NOT_FOUND) continue; tmp= rr_handle_error(info, tmp); break; @@ -563,12 +548,11 @@ int rr_from_pointers(READ_RECORD *info) cache_pos= info->cache_pos; info->cache_pos+= info->ref_length; - if (!(tmp= info->table->file->ha_rnd_pos(info->record,cache_pos))) + if (!(tmp= info->table->file->ha_rnd_pos(info->record(), cache_pos))) break; /* The following is extremely unlikely to happen */ - if (tmp == HA_ERR_RECORD_DELETED || - (tmp == HA_ERR_KEY_NOT_FOUND && info->ignore_not_found_rows)) + if (tmp == HA_ERR_KEY_NOT_FOUND) continue; tmp= rr_handle_error(info, tmp); break; @@ -603,33 +587,34 @@ static int rr_unpack_from_buffer(READ_RECORD *info) } /* cacheing of records from a database */ +static const uint STRUCT_LENGTH= 3 + MAX_REFLENGTH; + static int init_rr_cache(THD *thd, READ_RECORD *info) { - uint rec_cache_size; + uint rec_cache_size, cache_records; DBUG_ENTER("init_rr_cache"); - info->struct_length= 3+MAX_REFLENGTH; info->reclength= ALIGN_SIZE(info->table->s->reclength+1); - if (info->reclength < info->struct_length) - info->reclength= ALIGN_SIZE(info->struct_length); + if (info->reclength < STRUCT_LENGTH) + info->reclength= ALIGN_SIZE(STRUCT_LENGTH); info->error_offset= info->table->s->reclength; - info->cache_records= (thd->variables.read_rnd_buff_size / - (info->reclength+info->struct_length)); - rec_cache_size= info->cache_records*info->reclength; - info->rec_cache_size= info->cache_records*info->ref_length; + cache_records= thd->variables.read_rnd_buff_size / + (info->reclength + STRUCT_LENGTH); + rec_cache_size= cache_records * info->reclength; + info->rec_cache_size= cache_records * info->ref_length; // We have to allocate one more byte to use uint3korr (see comments for it) - if (info->cache_records <= 2 || - !(info->cache=(uchar*) my_malloc_lock(rec_cache_size+info->cache_records* - info->struct_length+1, - MYF(MY_THREAD_SPECIFIC)))) + if (cache_records <= 2 || + !(info->cache= (uchar*) my_malloc_lock(rec_cache_size + cache_records * + STRUCT_LENGTH + 1, + MYF(MY_THREAD_SPECIFIC)))) DBUG_RETURN(1); #ifdef HAVE_valgrind // Avoid warnings in qsort - bzero(info->cache,rec_cache_size+info->cache_records* info->struct_length+1); + bzero(info->cache, rec_cache_size + cache_records * STRUCT_LENGTH + 1); #endif - DBUG_PRINT("info",("Allocated buffert for %d records",info->cache_records)); + DBUG_PRINT("info", ("Allocated buffer for %d records", cache_records)); info->read_positions=info->cache+rec_cache_size; info->cache_pos=info->cache_end=info->cache; DBUG_RETURN(0); @@ -649,7 +634,7 @@ static int rr_from_cache(READ_RECORD *info) { if (info->cache_pos != info->cache_end) { - if (info->cache_pos[info->error_offset]) + if (unlikely(info->cache_pos[info->error_offset])) { shortget(error,info->cache_pos); if (info->print_error) @@ -658,7 +643,7 @@ static int rr_from_cache(READ_RECORD *info) else { error=0; - memcpy(info->record,info->cache_pos, + memcpy(info->record(), info->cache_pos, (size_t) info->table->s->reclength); } info->cache_pos+=info->reclength; @@ -684,8 +669,7 @@ static int rr_from_cache(READ_RECORD *info) int3store(ref_position,(long) i); ref_position+=3; } - my_qsort(info->read_positions, length, info->struct_length, - (qsort_cmp) rr_cmp); + my_qsort(info->read_positions, length, STRUCT_LENGTH, (qsort_cmp) rr_cmp); position=info->read_positions; for (i=0 ; i < length ; i++) @@ -695,7 +679,8 @@ static int rr_from_cache(READ_RECORD *info) record=uint3korr(position); position+=3; record_pos=info->cache+record*info->reclength; - if ((error=(int16) info->table->file->ha_rnd_pos(record_pos,info->ref_pos))) + if (unlikely((error= (int16) info->table->file-> + ha_rnd_pos(record_pos,info->ref_pos)))) { record_pos[info->error_offset]=1; shortstore(record_pos,error); |