diff options
37 files changed, 408 insertions, 463 deletions
diff --git a/heap/hp_hash.c b/heap/hp_hash.c index d040f37aea0..38ed581fe58 100644 --- a/heap/hp_hash.c +++ b/heap/hp_hash.c @@ -29,19 +29,15 @@ hp_rb_records_in_range() info HEAP handler inx Index to use - start_key Start of range. Null pointer if from first key - start_key_len Length of start key - start_search_flag Flag if start key should be included or not - end_key End of range. Null pointer if to last key - end_key_len Length of end key - end_search_flag Flag if start key should be included or not + min_key Min key. Is = 0 if no min range + max_key Max key. Is = 0 if no max range NOTES - start_search_flag can have one of the following values: + min_key.flag can have one of the following values: HA_READ_KEY_EXACT Include the key in the range HA_READ_AFTER_KEY Don't include key in range - end_search_flag can have one of the following values: + max_key.flag can have one of the following values: HA_READ_BEFORE_KEY Don't include key in range HA_READ_AFTER_KEY Include all 'end_key' values in the range @@ -52,11 +48,8 @@ the range. */ -ha_rows hp_rb_records_in_range(HP_INFO *info, int inx, const byte *start_key, - uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key, uint end_key_len, - enum ha_rkey_function end_search_flag) +ha_rows hp_rb_records_in_range(HP_INFO *info, int inx, key_range *min_key, + key_range *max_key) { ha_rows start_pos, end_pos; HP_KEYDEF *keyinfo= info->s->keydef + inx; @@ -67,12 +60,12 @@ ha_rows hp_rb_records_in_range(HP_INFO *info, int inx, const byte *start_key, info->lastinx= inx; custom_arg.keyseg= keyinfo->seg; custom_arg.search_flag= SEARCH_FIND | SEARCH_SAME; - if (start_key) + if (min_key) { custom_arg.key_length= hp_rb_pack_key(keyinfo, (uchar*) info->recbuf, - (uchar*) start_key, - start_key_len); - start_pos= tree_record_pos(rb_tree, info->recbuf, start_search_flag, + (uchar*) min_key->key, + min_key->length); + start_pos= tree_record_pos(rb_tree, info->recbuf, min_key->flag, &custom_arg); } else @@ -80,11 +73,12 @@ ha_rows hp_rb_records_in_range(HP_INFO *info, int inx, const byte *start_key, start_pos= 0; } - if (end_key) + if (max_key) { custom_arg.key_length= hp_rb_pack_key(keyinfo, (uchar*) info->recbuf, - (uchar*) end_key, end_key_len); - end_pos= tree_record_pos(rb_tree, info->recbuf, end_search_flag, + (uchar*) max_key->key, + max_key->length); + end_pos= tree_record_pos(rb_tree, info->recbuf, max_key->flag, &custom_arg); } else @@ -100,12 +94,13 @@ ha_rows hp_rb_records_in_range(HP_INFO *info, int inx, const byte *start_key, (end_pos == start_pos ? (ha_rows) 1 : end_pos - start_pos)); } + /* Search after a record based on a key */ /* Sets info->current_ptr to found record */ /* next_flag: Search=0, next=1, prev =2, same =3 */ byte *hp_search(HP_INFO *info, HP_KEYDEF *keyinfo, const byte *key, - uint nextflag) + uint nextflag) { reg1 HASH_INFO *pos,*prev_ptr; int flag; diff --git a/include/heap.h b/include/heap.h index b536937c8c0..63f2abbabc7 100644 --- a/include/heap.h +++ b/include/heap.h @@ -182,11 +182,8 @@ extern int heap_disable_indexes(HP_INFO *info); extern int heap_enable_indexes(HP_INFO *info); extern int heap_indexes_are_disabled(HP_INFO *info); extern void heap_update_auto_increment(HP_INFO *info, const byte *record); -ha_rows hp_rb_records_in_range(HP_INFO *info, int inx, const byte *start_key, - uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key, uint end_key_len, - enum ha_rkey_function end_search_flag); +ha_rows hp_rb_records_in_range(HP_INFO *info, int inx, key_range *min_key, + key_range *max_key); int heap_rkey(HP_INFO *info, byte *record, int inx, const byte *key, uint key_len, enum ha_rkey_function find_flag); extern gptr heap_find(HP_INFO *info,int inx,const byte *key); diff --git a/include/my_base.h b/include/my_base.h index 4a2f3f85083..f912cb4278c 100644 --- a/include/my_base.h +++ b/include/my_base.h @@ -349,6 +349,16 @@ enum data_file_type { STATIC_RECORD,DYNAMIC_RECORD,COMPRESSED_RECORD }; +/* For key ranges */ + +typedef struct st_key_range +{ + const byte *key; + uint length; + enum ha_rkey_function flag; +} key_range; + + /* For number of records */ #ifdef BIG_TABLES #define rows2double(A) ulonglong2double(A) diff --git a/include/my_bitmap.h b/include/my_bitmap.h index 5b3da011f54..a4511bf3414 100644 --- a/include/my_bitmap.h +++ b/include/my_bitmap.h @@ -55,6 +55,12 @@ extern void bitmap_set_bit(MY_BITMAP *map, uint bitmap_bit); extern void bitmap_set_prefix(MY_BITMAP *map, uint prefix_size); extern void bitmap_subtract(MY_BITMAP *map, const MY_BITMAP *map2); extern void bitmap_union(MY_BITMAP *map, const MY_BITMAP *map2); + +/* Fast, not thread safe, bitmap functions */ +#define bitmap_fast_set_bit(MAP, BIT) (MAP)->bitmap[(BIT) / 8] |= (1 << ((BIT) & 7)) +#define bitmap_fast_clear_bit(MAP, BIT) (MAP)->bitmap[(BIT) / 8] &= ~ (1 << ((BIT) & 7)) +#define bitmap_fast_is_set(MAP, BIT) (MAP)->bitmap[(BIT) / 8] & (1 << ((BIT) & 7)) + #ifdef __cplusplus } #endif diff --git a/include/myisam.h b/include/myisam.h index 93e2dc15574..c99e9a30b08 100644 --- a/include/myisam.h +++ b/include/myisam.h @@ -231,10 +231,7 @@ extern int mi_extra(struct st_myisam_info *file, enum ha_extra_function function, void *extra_arg); extern ha_rows mi_records_in_range(struct st_myisam_info *info,int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag); + key_range *min_key, key_range *max_key); extern int mi_log(int activate_log); extern int mi_is_changed(struct st_myisam_info *info); extern int mi_delete_all_rows(struct st_myisam_info *info); diff --git a/include/myisammrg.h b/include/myisammrg.h index 8b09e1a9231..de8a36c2d0a 100644 --- a/include/myisammrg.h +++ b/include/myisammrg.h @@ -101,10 +101,7 @@ extern int myrg_extra(MYRG_INFO *file,enum ha_extra_function function, void *extra_arg); extern void myrg_extrafunc(MYRG_INFO *info,invalidator_by_filename inv); extern ha_rows myrg_records_in_range(MYRG_INFO *info,int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag); + key_range *min_key, key_range *max_key); extern ulonglong myrg_position(MYRG_INFO *info); #ifdef __cplusplus diff --git a/myisam/mi_range.c b/myisam/mi_range.c index caa57ce6187..db01ada16dd 100644 --- a/myisam/mi_range.c +++ b/myisam/mi_range.c @@ -30,15 +30,27 @@ static uint _mi_keynr(MI_INFO *info,MI_KEYDEF *keyinfo,uchar *page, uchar *keypos,uint *ret_max_key); - /* If start_key = 0 assume read from start */ - /* If end_key = 0 assume read to end */ - /* Returns HA_POS_ERROR on error */ - -ha_rows mi_records_in_range(MI_INFO *info, int inx, const byte *start_key, - uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key, uint end_key_len, - enum ha_rkey_function end_search_flag) +/* + Estimate how many records there is in a given range + + SYNOPSIS + mi_records_in_range() + info MyISAM handler + inx Index to use + min_key Min key. Is = 0 if no min range + max_key Max key. Is = 0 if no max range + + NOTES + We should ONLY return 0 if there is no rows in range + + RETURN + HA_POS_ERROR error (or we can't estimate number of rows) + number Estimated number of rows +*/ + + +ha_rows mi_records_in_range(MI_INFO *info, int inx, key_range *min_key, + key_range *max_key) { ha_rows start_pos,end_pos,res; DBUG_ENTER("mi_records_in_range"); @@ -54,27 +66,31 @@ ha_rows mi_records_in_range(MI_INFO *info, int inx, const byte *start_key, switch(info->s->keyinfo[inx].key_alg){ case HA_KEY_ALG_RTREE: - { - uchar * key_buff; - if (start_key_len == 0) - start_key_len=USE_WHOLE_KEY; - key_buff=info->lastkey+info->s->base.max_key_length; - start_key_len= _mi_pack_key(info,inx,key_buff,(uchar*) start_key, - start_key_len, (HA_KEYSEG**) 0); - res=rtree_estimate(info, inx, key_buff, start_key_len, myisam_read_vec[start_search_flag]); - res=res?res:1; - break; - } + { + uchar * key_buff; + uint start_key_len; + + key_buff= info->lastkey+info->s->base.max_key_length; + start_key_len= _mi_pack_key(info,inx, key_buff, + (uchar*) min_key->key, min_key->length, + (HA_KEYSEG**) 0); + res= rtree_estimate(info, inx, key_buff, start_key_len, + myisam_read_vec[min_key->flag]); + res= res ? res : 1; /* Don't return 0 */ + break; + } case HA_KEY_ALG_BTREE: default: - start_pos= (start_key ? - _mi_record_pos(info,start_key,start_key_len,start_search_flag) : - (ha_rows) 0); - end_pos= (end_key ? - _mi_record_pos(info,end_key,end_key_len,end_search_flag) : - info->state->records+ (ha_rows) 1); - res=end_pos < start_pos ? (ha_rows) 0 : - (end_pos == start_pos ? (ha_rows) 1 : end_pos-start_pos); + start_pos= (min_key ? + _mi_record_pos(info, min_key->key, min_key->length, + min_key->flag) : + (ha_rows) 0); + end_pos= (max_key ? + _mi_record_pos(info, max_key->key, max_key->length, + max_key->flag) : + info->state->records+ (ha_rows) 1); + res= (end_pos < start_pos ? (ha_rows) 0 : + (end_pos == start_pos ? (ha_rows) 1 : end_pos-start_pos)); if (start_pos == HA_POS_ERROR || end_pos == HA_POS_ERROR) res=HA_POS_ERROR; } diff --git a/myisam/mi_test2.c b/myisam/mi_test2.c index d3c3cc2c492..56d8357536a 100644 --- a/myisam/mi_test2.c +++ b/myisam/mi_test2.c @@ -606,13 +606,20 @@ int main(int argc, char *argv[]) mi_status(file,&info,HA_STATUS_VARIABLE); for (i=0 ; i < info.keys ; i++) { + key_range min_key, max_key; if (mi_rfirst(file,read_record,(int) i) || mi_rlast(file,read_record2,(int) i)) goto err; copy_key(file,(uint) i,(uchar*) read_record,(uchar*) key); copy_key(file,(uint) i,(uchar*) read_record2,(uchar*) key2); - range_records=mi_records_in_range(file,(int) i,key,0,HA_READ_KEY_EXACT, - key2,0,HA_READ_AFTER_KEY); + min_key.key= key; + min_key.length= USE_WHOLE_KEY; + min_key.flag= HA_READ_KEY_EXACT; + max_key.key= key2; + max_key.length= USE_WHOLE_KEY; + max_key.flag= HA_READ_AFTER_KEY; + + range_records= mi_records_in_range(file,(int) i, &min_key, &max_key); if (range_records < info.records*8/10 || range_records > info.records*12/10) { @@ -634,12 +641,19 @@ int main(int argc, char *argv[]) for (k=rnd(1000)+1 ; k>0 && key1[k] == 0 ; k--) ; if (j != 0 && k != 0) { + key_range min_key, max_key; if (j > k) swap(int,j,k); sprintf(key,"%6d",j); sprintf(key2,"%6d",k); - range_records=mi_records_in_range(file,0,key,0,HA_READ_AFTER_KEY, - key2,0,HA_READ_BEFORE_KEY); + + min_key.key= key; + min_key.length= USE_WHOLE_KEY; + min_key.flag= HA_READ_AFTER_KEY; + max_key.key= key2; + max_key.length= USE_WHOLE_KEY; + max_key.flag= HA_READ_BEFORE_KEY; + range_records= mi_records_in_range(file, 0, &min_key, &max_key); records=0; for (j++ ; j < k ; j++) records+=key1[j]; diff --git a/myisam/rt_test.c b/myisam/rt_test.c index bbeb8fce2d1..c1126c1939a 100644 --- a/myisam/rt_test.c +++ b/myisam/rt_test.c @@ -39,7 +39,6 @@ int main(int argc __attribute__((unused)),char *argv[] __attribute__((unused))) } - int run_test(const char *filename) { MI_INFO *file; @@ -48,6 +47,7 @@ int run_test(const char *filename) MI_COLUMNDEF recinfo[20]; MI_KEYDEF keyinfo[20]; HA_KEYSEG keyseg[20]; + key_range range; int silent=0; int opt_unique=0; @@ -66,15 +66,12 @@ int run_test(const char *filename) int upd= 10; ha_rows hrows; - - /* Define a column for NULLs and DEL markers*/ recinfo[0].type=FIELD_NORMAL; recinfo[0].length=1; /* For NULL bits */ rec_length=1; - /* Define 2*ndims columns for coordinates*/ for (i=1; i<=2*ndims ;i++){ @@ -83,7 +80,6 @@ int run_test(const char *filename) rec_length+=key_length; } - /* Define a key with 2*ndims segments */ keyinfo[0].seg=keyseg; @@ -101,8 +97,7 @@ int run_test(const char *filename) keyinfo[0].seg[i].language=default_charset_info->number; } - - if(!silent) + if (!silent) printf("- Creating isam-file\n"); bzero((char*) &create_info,sizeof(create_info)); @@ -115,15 +110,11 @@ int run_test(const char *filename) recinfo,uniques,&uniquedef,&create_info,create_flag)) goto err; - - - - if(!silent) + if (!silent) printf("- Open isam-file\n"); if (!(file=mi_open(filename,2,HA_OPEN_ABORT_IF_LOCKED))) goto err; - if (!silent) printf("- Writing key:s\n"); @@ -144,11 +135,9 @@ int run_test(const char *filename) } } - - if((error=read_with_pos(file,silent))) + if ((error=read_with_pos(file,silent))) goto err; - if (!silent) printf("- Reading rows with key\n"); @@ -160,12 +149,12 @@ int run_test(const char *filename) bzero((char*) read_record,MAX_REC_LENGTH); error=mi_rkey(file,read_record,0,record+1,0,HA_READ_MBR_EQUAL); - if(error && error!=HA_ERR_KEY_NOT_FOUND) + if (error && error!=HA_ERR_KEY_NOT_FOUND) { printf(" mi_rkey: %3d errno: %3d\n",error,my_errno); goto err; } - if(error == HA_ERR_KEY_NOT_FOUND) + if (error == HA_ERR_KEY_NOT_FOUND) { print_record(record,mi_position(file)," NOT FOUND\n"); continue; @@ -173,10 +162,6 @@ int run_test(const char *filename) print_record(read_record,mi_position(file),"\n"); } - - - - if (!silent) printf("- Deleting rows\n"); for (i=0; i < nrecords/4; i++) @@ -184,7 +169,7 @@ int run_test(const char *filename) my_errno=0; bzero((char*) read_record,MAX_REC_LENGTH); error=mi_rrnd(file,read_record,i == 0 ? 0L : HA_OFFSET_ERROR); - if(error) + if (error) { printf("pos: %2d mi_rrnd: %3d errno: %3d\n",i,error,my_errno); goto err; @@ -192,14 +177,13 @@ int run_test(const char *filename) print_record(read_record,mi_position(file),"\n"); error=mi_delete(file,read_record); - if(error) + if (error) { printf("pos: %2d mi_delete: %3d errno: %3d\n",i,error,my_errno); goto err; } } - if (!silent) printf("- Updating rows with position\n"); for (i=0; i < (nrecords - nrecords/4) ; i++) @@ -207,9 +191,9 @@ int run_test(const char *filename) my_errno=0; bzero((char*) read_record,MAX_REC_LENGTH); error=mi_rrnd(file,read_record,i == 0 ? 0L : HA_OFFSET_ERROR); - if(error) + if (error) { - if(error==HA_ERR_RECORD_DELETED) + if (error==HA_ERR_RECORD_DELETED) continue; printf("pos: %2d mi_rrnd: %3d errno: %3d\n",i,error,my_errno); goto err; @@ -219,19 +203,16 @@ int run_test(const char *filename) printf("\t-> "); print_record(record,mi_position(file),"\n"); error=mi_update(file,read_record,record); - if(error) + if (error) { printf("pos: %2d mi_update: %3d errno: %3d\n",i,error,my_errno); goto err; } } - - if((error=read_with_pos(file,silent))) + if ((error=read_with_pos(file,silent))) goto err; - - if (!silent) printf("- Test mi_rkey then a sequence of mi_rnext_same\n"); @@ -246,25 +227,20 @@ int run_test(const char *filename) print_record(read_record,mi_position(file)," mi_rkey\n"); row_count=1; - - do { - if((error=mi_rnext_same(file,read_record))) + for (;;) + { + if ((error=mi_rnext_same(file,read_record))) { - if(error==HA_ERR_END_OF_FILE) + if (error==HA_ERR_END_OF_FILE) break; printf("mi_next: %3d errno: %3d\n",error,my_errno); goto err; } print_record(read_record,mi_position(file)," mi_rnext_same\n"); row_count++; - }while(1); + } printf(" %d rows\n",row_count); - - - - - if (!silent) printf("- Test mi_rfirst then a sequence of mi_rnext\n"); @@ -277,10 +253,11 @@ int run_test(const char *filename) row_count=1; print_record(read_record,mi_position(file)," mi_frirst\n"); - for(i=0;i<nrecords;i++) { - if((error=mi_rnext(file,read_record,0))) + for (i=0;i<nrecords;i++) + { + if ((error=mi_rnext(file,read_record,0))) { - if(error==HA_ERR_END_OF_FILE) + if (error==HA_ERR_END_OF_FILE) break; printf("mi_next: %3d errno: %3d\n",error,my_errno); goto err; @@ -290,16 +267,18 @@ int run_test(const char *filename) } printf(" %d rows\n",row_count); - if (!silent) printf("- Test mi_records_in_range()\n"); create_record1(record, nrecords*4/5); print_record(record,0,"\n"); - hrows=mi_records_in_range(file,0,record+1,0,HA_READ_MBR_INTERSECT,record+1,0,0); + + range.key= record+1; + range.length= 1000; /* Big enough */ + range.flag= HA_READ_MBR_INTERSECT; + hrows= mi_records_in_range(file,0, &range, (key_range*) 0); printf(" %ld rows\n", (long) hrows); - if (mi_close(file)) goto err; my_end(MY_CHECK_ERROR); @@ -325,11 +304,11 @@ static int read_with_pos (MI_INFO * file,int silent) my_errno=0; bzero((char*) read_record,MAX_REC_LENGTH); error=mi_rrnd(file,read_record,i == 0 ? 0L : HA_OFFSET_ERROR); - if(error) + if (error) { - if(error==HA_ERR_END_OF_FILE) + if (error==HA_ERR_END_OF_FILE) break; - if(error==HA_ERR_RECORD_DELETED) + if (error==HA_ERR_RECORD_DELETED) continue; printf("pos: %2d mi_rrnd: %3d errno: %3d\n",i,error,my_errno); return error; diff --git a/myisam/sp_test.c b/myisam/sp_test.c index 5cbf5e87579..29c5f47471a 100644 --- a/myisam/sp_test.c +++ b/myisam/sp_test.c @@ -32,10 +32,10 @@ static void print_key(const char *key,const char * tail); static int run_test(const char *filename); static int read_with_pos(MI_INFO * file, int silent); -static int rtree_CreateLineStringWKB(double *ords, uint n_dims, uint n_points, uchar *wkb); +static int rtree_CreateLineStringWKB(double *ords, uint n_dims, uint n_points, + uchar *wkb); static void rtree_PrintWKB(uchar *wkb, uint n_dims); - static char blob_key[MAX_REC_LENGTH]; @@ -46,7 +46,6 @@ int main(int argc __attribute__((unused)),char *argv[]) } - int run_test(const char *filename) { MI_INFO *file; @@ -55,7 +54,7 @@ int run_test(const char *filename) MI_COLUMNDEF recinfo[20]; MI_KEYDEF keyinfo[20]; HA_KEYSEG keyseg[20]; - + key_range min_range, max_range; int silent=0; int create_flag=0; int null_fields=0; @@ -100,7 +99,7 @@ int run_test(const char *filename) keyinfo[0].seg[0].bit_start=4; /* Long BLOB */ - if(!silent) + if (!silent) printf("- Creating isam-file\n"); bzero((char*) &create_info,sizeof(create_info)); @@ -113,17 +112,12 @@ int run_test(const char *filename) recinfo,uniques,&uniquedef,&create_info,create_flag)) goto err; - - - - if(!silent) + if (!silent) printf("- Open isam-file\n"); if (!(file=mi_open(filename,2,HA_OPEN_ABORT_IF_LOCKED))) goto err; - - if (!silent) printf("- Writing key:s\n"); @@ -143,11 +137,9 @@ int run_test(const char *filename) } } - - if((error=read_with_pos(file,silent))) + if ((error=read_with_pos(file,silent))) goto err; - if (!silent) printf("- Deleting rows with position\n"); for (i=0; i < nrecords/4; i++) @@ -155,23 +147,20 @@ int run_test(const char *filename) my_errno=0; bzero((char*) read_record,MAX_REC_LENGTH); error=mi_rrnd(file,read_record,i == 0 ? 0L : HA_OFFSET_ERROR); - if(error) + if (error) { printf("pos: %2d mi_rrnd: %3d errno: %3d\n",i,error,my_errno); goto err; } print_record(read_record,mi_position(file),"\n"); error=mi_delete(file,read_record); - if(error) + if (error) { printf("pos: %2d mi_delete: %3d errno: %3d\n",i,error,my_errno); goto err; } } - - - if (!silent) printf("- Updating rows with position\n"); for (i=0; i < nrecords/2 ; i++) @@ -179,9 +168,9 @@ int run_test(const char *filename) my_errno=0; bzero((char*) read_record,MAX_REC_LENGTH); error=mi_rrnd(file,read_record,i == 0 ? 0L : HA_OFFSET_ERROR); - if(error) + if (error) { - if(error==HA_ERR_RECORD_DELETED) + if (error==HA_ERR_RECORD_DELETED) continue; printf("pos: %2d mi_rrnd: %3d errno: %3d\n",i,error,my_errno); goto err; @@ -191,20 +180,16 @@ int run_test(const char *filename) printf("\t-> "); print_record(record,mi_position(file),"\n"); error=mi_update(file,read_record,record); - if(error) + if (error) { printf("pos: %2d mi_update: %3d errno: %3d\n",i,error,my_errno); goto err; } } - - - if((error=read_with_pos(file,silent))) + if ((error=read_with_pos(file,silent))) goto err; - - if (!silent) printf("- Test mi_rkey then a sequence of mi_rnext_same\n"); @@ -219,25 +204,20 @@ int run_test(const char *filename) print_record(read_record,mi_position(file)," mi_rkey\n"); row_count=1; - - do { - if((error=mi_rnext_same(file,read_record))) + for (;;) + { + if ((error=mi_rnext_same(file,read_record))) { - if(error==HA_ERR_END_OF_FILE) + if (error==HA_ERR_END_OF_FILE) break; printf("mi_next: %3d errno: %3d\n",error,my_errno); goto err; } print_record(read_record,mi_position(file)," mi_rnext_same\n"); row_count++; - }while(1); + } printf(" %d rows\n",row_count); - - - - - if (!silent) printf("- Test mi_rfirst then a sequence of mi_rnext\n"); @@ -251,9 +231,9 @@ int run_test(const char *filename) print_record(read_record,mi_position(file)," mi_frirst\n"); for(i=0;i<nrecords;i++) { - if((error=mi_rnext(file,read_record,0))) + if ((error=mi_rnext(file,read_record,0))) { - if(error==HA_ERR_END_OF_FILE) + if (error==HA_ERR_END_OF_FILE) break; printf("mi_next: %3d errno: %3d\n",error,my_errno); goto err; @@ -263,22 +243,22 @@ int run_test(const char *filename) } printf(" %d rows\n",row_count); - - - if (!silent) printf("- Test mi_records_in_range()\n"); create_key(key, nrecords*upd); print_key(key," INTERSECT\n"); - hrows=mi_records_in_range(file,0,key,0,HA_READ_MBR_INTERSECT,record+1,0, - HA_READ_KEY_EXACT); + min_range.key= key; + min_range.length= 1000; /* Big enough */ + min_range.flag= HA_READ_MBR_INTERSECT; + max_range.key= record+1; + max_range.length= 1000; /* Big enough */ + max_range.flag= HA_READ_KEY_EXACT; + hrows= mi_records_in_range(file,0, &min_range, &max_range); printf(" %ld rows\n", (long) hrows); - if (mi_close(file)) goto err; my_end(MY_CHECK_ERROR); - return 0; err: @@ -287,7 +267,6 @@ err: } - static int read_with_pos (MI_INFO * file,int silent) { int error; @@ -302,11 +281,11 @@ static int read_with_pos (MI_INFO * file,int silent) my_errno=0; bzero((char*) read_record,MAX_REC_LENGTH); error=mi_rrnd(file,read_record,i == 0 ? 0L : HA_OFFSET_ERROR); - if(error) + if (error) { - if(error==HA_ERR_END_OF_FILE) + if (error==HA_ERR_END_OF_FILE) break; - if(error==HA_ERR_RECORD_DELETED) + if (error==HA_ERR_RECORD_DELETED) continue; printf("pos: %2d mi_rrnd: %3d errno: %3d\n",i,error,my_errno); return error; @@ -351,7 +330,7 @@ static void print_record(char * record, my_off_t offs,const char * tail) pos+=4; printf(" len=%d ",len); memcpy_fixed(&ptr,pos,sizeof(char*)); - if(ptr) + if (ptr) rtree_PrintWKB((uchar*) ptr,SPDIMS); else printf("<NULL> "); @@ -360,7 +339,6 @@ static void print_record(char * record, my_off_t offs,const char * tail) } - #ifdef NOT_USED static void create_point(char *record,uint rownr) { @@ -447,7 +425,6 @@ static void print_key(const char *key,const char * tail) } - #ifdef NOT_USED static int rtree_CreatePointWKB(double *ords, uint n_dims, uchar *wkb) @@ -489,6 +466,7 @@ static int rtree_CreateLineStringWKB(double *ords, uint n_dims, uint n_points, return 9 + n_points * n_dims * 8; } + static void rtree_PrintWKB(uchar *wkb, uint n_dims) { uint wkb_type; diff --git a/myisammrg/myrg_range.c b/myisammrg/myrg_range.c index 7644ae40c7b..aafdf70525c 100644 --- a/myisammrg/myrg_range.c +++ b/myisammrg/myrg_range.c @@ -16,20 +16,15 @@ #include "myrg_def.h" -ha_rows myrg_records_in_range(MYRG_INFO *info, int inx, const byte *start_key, - uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key, uint end_key_len, - enum ha_rkey_function end_search_flag) +ha_rows myrg_records_in_range(MYRG_INFO *info, int inx, + key_range *min_key, key_range *max_key) { ha_rows records=0, res; MYRG_TABLE *table; for (table=info->open_tables ; table != info->end_table ; table++) { - res=mi_records_in_range(table->table, inx, - start_key, start_key_len, start_search_flag, - end_key, end_key_len, end_search_flag); + res= mi_records_in_range(table->table, inx, min_key, max_key); if (res == HA_POS_ERROR) return HA_POS_ERROR; if (records > HA_POS_ERROR - res) diff --git a/mysys/my_bitmap.c b/mysys/my_bitmap.c index da16457c299..0f8984e6b3d 100644 --- a/mysys/my_bitmap.c +++ b/mysys/my_bitmap.c @@ -104,7 +104,7 @@ void bitmap_set_bit(MY_BITMAP *map, uint bitmap_bit) { DBUG_ASSERT(map->bitmap && bitmap_bit < map->bitmap_size*8); bitmap_lock(map); - map->bitmap[bitmap_bit / 8] |= (1 << (bitmap_bit & 7)); + bitmap_fast_set_bit(map, bitmap_bit); bitmap_unlock(map); } @@ -144,7 +144,7 @@ void bitmap_clear_bit(MY_BITMAP *map, uint bitmap_bit) { DBUG_ASSERT(map->bitmap && bitmap_bit < map->bitmap_size*8); bitmap_lock(map); - map->bitmap[bitmap_bit / 8] &= ~ (1 << (bitmap_bit & 7)); + bitmap_fast_clear_bit(map, bitmap_bit); bitmap_unlock(map); } @@ -220,7 +220,7 @@ my_bool bitmap_is_set_all(const MY_BITMAP *map) my_bool bitmap_is_set(const MY_BITMAP *map, uint bitmap_bit) { DBUG_ASSERT(map->bitmap && bitmap_bit < map->bitmap_size*8); - return map->bitmap[bitmap_bit / 8] & (1 << (bitmap_bit & 7)); + return bitmap_fast_is_set(map, bitmap_bit); } diff --git a/sql/examples/ha_example.cc b/sql/examples/ha_example.cc index b8ae5967475..2d17caf1a83 100644 --- a/sql/examples/ha_example.cc +++ b/sql/examples/ha_example.cc @@ -625,14 +625,11 @@ int ha_example::rename_table(const char * from, const char * to) Called from opt_range.cc by check_quick_keys(). */ -ha_rows ha_example::records_in_range(int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag) +ha_rows ha_example::records_in_range(uint inx, key_range *min_key, + key_range *max_key) { - DBUG_ENTER("ha_example::records_in_range "); - DBUG_RETURN(records); + DBUG_ENTER("ha_example::records_in_range"); + DBUG_RETURN(10); // low number to force index usage } diff --git a/sql/field.cc b/sql/field.cc index edaa29dbaa0..1ad7e039363 100644 --- a/sql/field.cc +++ b/sql/field.cc @@ -4252,12 +4252,13 @@ int Field_string::cmp(const char *a_ptr, const char *b_ptr) like in latin_de 'ae' and 0xe4 */ return field_charset->coll->strnncollsp(field_charset, - (const uchar*) a_ptr, field_length, - (const uchar*) b_ptr, field_length); + (const uchar*) a_ptr, field_length, + (const uchar*) b_ptr, + field_length); } return field_charset->coll->strnncoll(field_charset, - (const uchar*) a_ptr, field_length, - (const uchar*) b_ptr, field_length); + (const uchar*) a_ptr, field_length, + (const uchar*) b_ptr, field_length); } void Field_string::sort_string(char *to,uint length) diff --git a/sql/ha_berkeley.cc b/sql/ha_berkeley.cc index 593b9575fb6..4dde893116f 100644 --- a/sql/ha_berkeley.cc +++ b/sql/ha_berkeley.cc @@ -1542,7 +1542,7 @@ int ha_berkeley::index_next_same(byte * buf, const byte *key, uint keylen) { error=read_row(cursor->c_get(cursor, &last_key, &row, DB_NEXT), (char*) buf, active_index, &row, &last_key, 1); - if (!error && ::key_cmp(table, key, active_index, keylen)) + if (!error && ::key_cmp_if_same(table, key, active_index, keylen)) error=HA_ERR_END_OF_FILE; } DBUG_RETURN(error); @@ -1987,11 +1987,8 @@ double ha_berkeley::scan_time() return rows2double(records/3); } -ha_rows ha_berkeley::records_in_range(int keynr, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag) +ha_rows ha_berkeley::records_in_range(uint keynr, key_range *start_key, + key_range *end_key) { DBT key; DB_KEY_RANGE start_range, end_range; @@ -2000,25 +1997,27 @@ ha_rows ha_berkeley::records_in_range(int keynr, DBUG_ENTER("records_in_range"); if ((start_key && kfile->key_range(kfile,transaction, - pack_key(&key, keynr, key_buff, start_key, - start_key_len), - &start_range,0)) || + pack_key(&key, keynr, key_buff, + start_key->key, + start_key->length), + &start_range,0)) || (end_key && kfile->key_range(kfile,transaction, - pack_key(&key, keynr, key_buff, end_key, - end_key_len), + pack_key(&key, keynr, key_buff, + end_key->key, + end_key->length), &end_range,0))) DBUG_RETURN(HA_BERKELEY_RANGE_COUNT); // Better than returning an error /* purecov: inspected */ if (!start_key) - start_pos=0.0; - else if (start_search_flag == HA_READ_KEY_EXACT) + start_pos= 0.0; + else if (start_key->flag == HA_READ_KEY_EXACT) start_pos=start_range.less; else start_pos=start_range.less+start_range.equal; if (!end_key) - end_pos=1.0; - else if (end_search_flag == HA_READ_BEFORE_KEY) + end_pos= 1.0; + else if (end_key->flag == HA_READ_BEFORE_KEY) end_pos=end_range.less; else end_pos=end_range.less+end_range.equal; diff --git a/sql/ha_berkeley.h b/sql/ha_berkeley.h index 88af2326eac..4bc8e3a5777 100644 --- a/sql/ha_berkeley.h +++ b/sql/ha_berkeley.h @@ -143,12 +143,7 @@ class ha_berkeley: public handler int optimize(THD* thd, HA_CHECK_OPT* check_opt); int check(THD* thd, HA_CHECK_OPT* check_opt); - ha_rows records_in_range(int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag); - + ha_rows records_in_range(uint inx, key_range *min_key, key_range *max_key); int create(const char *name, register TABLE *form, HA_CREATE_INFO *create_info); int delete_table(const char *name); diff --git a/sql/ha_heap.cc b/sql/ha_heap.cc index 12b922e6fc0..9915dc90dc1 100644 --- a/sql/ha_heap.cc +++ b/sql/ha_heap.cc @@ -363,28 +363,20 @@ int ha_heap::rename_table(const char * from, const char * to) return heap_rename(from,to); } -ha_rows ha_heap::records_in_range(int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag) + +ha_rows ha_heap::records_in_range(uint inx, key_range *min_key, + key_range *max_key) { - KEY *pos=table->key_info+inx; - if (pos->algorithm == HA_KEY_ALG_BTREE) - { - return hp_rb_records_in_range(file, inx, start_key, start_key_len, - start_search_flag, end_key, end_key_len, - end_search_flag); - } - else - { - if (start_key_len != end_key_len || - start_key_len != pos->key_length || - start_search_flag != HA_READ_KEY_EXACT || - end_search_flag != HA_READ_AFTER_KEY) - return HA_POS_ERROR; // Can't only use exact keys - return 10; // Good guess - } + KEY *key=table->key_info+inx; + if (key->algorithm == HA_KEY_ALG_BTREE) + return hp_rb_records_in_range(file, inx, min_key, max_key); + + if (min_key->length != max_key->length || + min_key->length != key->key_length || + min_key->flag != HA_READ_KEY_EXACT || + max_key->flag != HA_READ_AFTER_KEY) + return HA_POS_ERROR; // Can't only use exact keys + return 10; // Good guess } diff --git a/sql/ha_heap.h b/sql/ha_heap.h index 2f849d2574b..f55eda91149 100644 --- a/sql/ha_heap.h +++ b/sql/ha_heap.h @@ -86,10 +86,7 @@ class ha_heap: public handler int disable_indexes(uint mode); int enable_indexes(uint mode); int indexes_are_disabled(void); - ha_rows records_in_range(int inx, const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag); + ha_rows records_in_range(uint inx, key_range *min_key, key_range *max_key); int delete_table(const char *from); int rename_table(const char * from, const char * to); int create(const char *name, TABLE *form, HA_CREATE_INFO *create_info); diff --git a/sql/ha_innodb.cc b/sql/ha_innodb.cc index 201ec8f183f..ff491a95043 100644 --- a/sql/ha_innodb.cc +++ b/sql/ha_innodb.cc @@ -3925,18 +3925,11 @@ ha_innobase::records_in_range( /*==========================*/ /* out: estimated number of rows */ - int keynr, /* in: index number */ - const mysql_byte* start_key, /* in: start key value of the - range, may also be empty */ - uint start_key_len, /* in: start key val len, may - also be 0 */ - enum ha_rkey_function start_search_flag,/* in: start search condition - e.g., 'greater than' */ - const mysql_byte* end_key, /* in: range end key val, may - also be empty */ - uint end_key_len, /* in: range end key val len, - may also be 0 */ - enum ha_rkey_function end_search_flag)/* in: range end search cond */ + uint keynr, /* in: index number */ + key_range *min_key, /* in: start key value of the + range, may also be 0 */ + key_range *max_key) /* in: range end key val, may + also be 0 */ { row_prebuilt_t* prebuilt = (row_prebuilt_t*) innobase_prebuilt; KEY* key; @@ -3957,12 +3950,6 @@ ha_innobase::records_in_range( DBUG_ENTER("records_in_range"); - /* We do not know if MySQL can call this function before calling - external_lock(). To be safe, update the thd of the current table - handle. */ - - update_thd(current_thd); - prebuilt->trx->op_info = (char*)"estimating records in index range"; /* In case MySQL calls this in the middle of a SELECT query, release @@ -3986,17 +3973,21 @@ ha_innobase::records_in_range( range_start, (byte*) key_val_buff, (ulint)upd_and_key_val_buff_len, index, - (byte*) start_key, - (ulint) start_key_len); + (byte*) (min_key ? min_key->key : + (const mysql_byte*) 0), + (ulint) (min_key ? min_key->length : 0)); row_sel_convert_mysql_key_to_innobase( range_end, (byte*) key_val_buff2, buff2_len, index, - (byte*) end_key, - (ulint) end_key_len); - - mode1 = convert_search_mode_to_innobase(start_search_flag); - mode2 = convert_search_mode_to_innobase(end_search_flag); + (byte*) (max_key ? max_key->key : + (const mysql_byte*) 0), + (ulint) (max_key ? max_key->length : 0)); + + mode1 = convert_search_mode_to_innobase(min_key ? min_key->flag : + HA_READ_KEY_EXACT); + mode2 = convert_search_mode_to_innobase(max_key ? max_key->flag : + HA_READ_KEY_EXACT); n_rows = btr_estimate_n_rows_in_range(index, range_start, mode1, range_end, mode2); diff --git a/sql/ha_innodb.h b/sql/ha_innodb.h index 83a3edc4126..c585fd9c463 100644 --- a/sql/ha_innodb.h +++ b/sql/ha_innodb.h @@ -166,11 +166,7 @@ class ha_innobase: public handler int start_stmt(THD *thd); void position(byte *record); - ha_rows records_in_range(int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag); + ha_rows records_in_range(uint inx, key_range *min_key, key_range *max_key); ha_rows estimate_number_of_rows(); int create(const char *name, register TABLE *form, diff --git a/sql/ha_isam.cc b/sql/ha_isam.cc index 09c2aeceafc..85ab25a31d9 100644 --- a/sql/ha_isam.cc +++ b/sql/ha_isam.cc @@ -382,18 +382,21 @@ int ha_isam::create(const char *name, register TABLE *form, } +static key_range no_range= { (byte*) 0, 0, HA_READ_KEY_EXACT }; -ha_rows ha_isam::records_in_range(int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag) +ha_rows ha_isam::records_in_range(uint inx, key_range *min_key, + key_range *max_key) { + /* ISAM checks if 'key' pointer <> 0 to know if there is no range */ + if (!min_key) + min_key= &no_range; + if (!max_key) + max_key= &no_range; return (ha_rows) nisam_records_in_range(file, - inx, - start_key,start_key_len, - start_search_flag, - end_key,end_key_len, - end_search_flag); + (int) inx, + min_key->key, min_key->length, + min_key->flag, + max_key->key, max_key->length, + max_key->flag); } #endif /* HAVE_ISAM */ diff --git a/sql/ha_isam.h b/sql/ha_isam.h index 2c8ec274145..8a887ababde 100644 --- a/sql/ha_isam.h +++ b/sql/ha_isam.h @@ -70,11 +70,7 @@ class ha_isam: public handler void info(uint); int extra(enum ha_extra_function operation); int external_lock(THD *thd, int lock_type); - ha_rows records_in_range(int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag); + ha_rows records_in_range(uint inx, key_range *min_key, key_range *max_key); int create(const char *name, TABLE *form, HA_CREATE_INFO *create_info); THR_LOCK_DATA **store_lock(THD *thd, THR_LOCK_DATA **to, diff --git a/sql/ha_myisam.cc b/sql/ha_myisam.cc index 1b24633953d..feac5c51dce 100644 --- a/sql/ha_myisam.cc +++ b/sql/ha_myisam.cc @@ -1515,19 +1515,15 @@ longlong ha_myisam::get_auto_increment() SYNOPSIS records_in_range() inx Index to use - start_key Start of range. Null pointer if from first key - start_key_len Length of start key - start_search_flag Flag if start key should be included or not - end_key End of range. Null pointer if to last key - end_key_len Length of end key - end_search_flag Flag if start key should be included or not + min_key Start of range. Null pointer if from first key + max_key End of range. Null pointer if to last key NOTES - start_search_flag can have one of the following values: + min_key.flag can have one of the following values: HA_READ_KEY_EXACT Include the key in the range HA_READ_AFTER_KEY Don't include key in range - end_search_flag can have one of the following values: + max_key.flag can have one of the following values: HA_READ_BEFORE_KEY Don't include key in range HA_READ_AFTER_KEY Include all 'end_key' values in the range @@ -1538,18 +1534,10 @@ longlong ha_myisam::get_auto_increment() the range. */ -ha_rows ha_myisam::records_in_range(int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag) +ha_rows ha_myisam::records_in_range(uint inx, key_range *min_key, + key_range *max_key) { - return (ha_rows) mi_records_in_range(file, - inx, - start_key,start_key_len, - start_search_flag, - end_key,end_key_len, - end_search_flag); + return (ha_rows) mi_records_in_range(file, (int) inx, min_key, max_key); } diff --git a/sql/ha_myisam.h b/sql/ha_myisam.h index 206a1c62a2f..77887220903 100644 --- a/sql/ha_myisam.h +++ b/sql/ha_myisam.h @@ -110,11 +110,7 @@ class ha_myisam: public handler int indexes_are_disabled(void); void start_bulk_insert(ha_rows rows); int end_bulk_insert(); - ha_rows records_in_range(int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag); + ha_rows records_in_range(uint inx, key_range *min_key, key_range *max_key); void update_create_info(HA_CREATE_INFO *create_info); int create(const char *name, TABLE *form, HA_CREATE_INFO *create_info); THR_LOCK_DATA **store_lock(THD *thd, THR_LOCK_DATA **to, diff --git a/sql/ha_myisammrg.cc b/sql/ha_myisammrg.cc index 041d9eaead3..9aa6d039efb 100644 --- a/sql/ha_myisammrg.cc +++ b/sql/ha_myisammrg.cc @@ -199,20 +199,14 @@ void ha_myisammrg::position(const byte *record) ha_store_ptr(ref, ref_length, (my_off_t) position); } -ha_rows ha_myisammrg::records_in_range(int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag) + +ha_rows ha_myisammrg::records_in_range(uint inx, key_range *min_key, + key_range *max_key) { - return (ha_rows) myrg_records_in_range(file, - inx, - start_key,start_key_len, - start_search_flag, - end_key,end_key_len, - end_search_flag); + return (ha_rows) myrg_records_in_range(file, (int) inx, min_key, max_key); } + void ha_myisammrg::info(uint flag) { MYMERGE_INFO info; diff --git a/sql/ha_myisammrg.h b/sql/ha_myisammrg.h index c0f81a77a1e..fd36c78202d 100644 --- a/sql/ha_myisammrg.h +++ b/sql/ha_myisammrg.h @@ -70,11 +70,7 @@ class ha_myisammrg: public handler int rnd_next(byte *buf); int rnd_pos(byte * buf, byte *pos); void position(const byte *record); - ha_rows records_in_range(int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag); + ha_rows records_in_range(uint inx, key_range *min_key, key_range *max_key); my_off_t row_position() { return myrg_position(file); } void info(uint); int extra(enum ha_extra_function operation); diff --git a/sql/ha_ndbcluster.cc b/sql/ha_ndbcluster.cc index 3bc322878d1..21056ef4a8f 100644 --- a/sql/ha_ndbcluster.cc +++ b/sql/ha_ndbcluster.cc @@ -2748,22 +2748,19 @@ void ha_ndbcluster::set_dbname(const char *path_name) ha_rows -ha_ndbcluster::records_in_range(int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag) -{ - ha_rows records= 10; - KEY* key_info= table->key_info + inx; +ha_ndbcluster::records_in_range(uint inx, key_range *min_key, + key_range *max_key) +{ + ha_rows records= 10; /* Good guess when you don't know anything */ + KEY *key_info= table->key_info + inx; uint key_length= key_info->key_length; DBUG_ENTER("records_in_range"); - DBUG_PRINT("enter", ("inx: %d", inx)); - DBUG_PRINT("enter", ("start_key: %x, start_key_len: %d", start_key, start_key_len)); - DBUG_PRINT("enter", ("start_search_flag: %d", start_search_flag)); - DBUG_PRINT("enter", ("end_key: %x, end_key_len: %d", end_key, end_key_len)); - DBUG_PRINT("enter", ("end_search_flag: %d", end_search_flag)); + DBUG_PRINT("enter", ("inx: %u", inx)); + DBUG_DUMP("start_key", min_key->key, min_key->length); + DBUG_DUMP("end_key", max_key->key, max_key->length); + DBUG_PRINT("enter", ("start_search_flag: %u end_search_flag: %u", + min_key->flag, max_key->flag)); /* Check that start_key_len is equal to @@ -2772,7 +2769,7 @@ ha_ndbcluster::records_in_range(int inx, */ NDB_INDEX_TYPE idx_type= get_index_type(inx); if ((idx_type == UNIQUE_INDEX || idx_type == PRIMARY_KEY_INDEX) && - start_key_len < key_length) + min_key->length < key_length) { DBUG_PRINT("warning", ("Tried to use index which required" "full key length: %d, HA_POS_ERROR", diff --git a/sql/ha_ndbcluster.h b/sql/ha_ndbcluster.h index bd601f39fc4..029ebc11e46 100644 --- a/sql/ha_ndbcluster.h +++ b/sql/ha_ndbcluster.h @@ -121,12 +121,7 @@ class ha_ndbcluster: public handler } double scan_time(); - ha_rows records_in_range(int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag); - + ha_rows records_in_range(uint inx, key_range *min_key, key_range *max_key); static Ndb* seize_ndb(); static void release_ndb(Ndb* ndb); diff --git a/sql/handler.cc b/sql/handler.cc index 7374242ebf8..f8eca1b93ef 100644 --- a/sql/handler.cc +++ b/sql/handler.cc @@ -1184,7 +1184,7 @@ int handler::index_next_same(byte *buf, const byte *key, uint keylen) int error; if (!(error=index_next(buf))) { - if (key_cmp(table, key, active_index, keylen)) + if (key_cmp_if_same(table, key, active_index, keylen)) { table->status=STATUS_NOT_FOUND; error=HA_ERR_END_OF_FILE; @@ -1388,6 +1388,7 @@ int ha_discover(const char* dbname, const char* name, read_range_first() start_key Start key. Is 0 if no min range end_key End key. Is 0 if no max range + eq_range_arg Set to 1 if start_key == end_key sorted Set to 1 if result should be sorted per key NOTES @@ -1401,11 +1402,12 @@ int ha_discover(const char* dbname, const char* name, int handler::read_range_first(const key_range *start_key, const key_range *end_key, - bool sorted) + bool eq_range_arg, bool sorted) { int result; DBUG_ENTER("handler::read_range_first"); + eq_range= eq_range_arg; end_range= 0; if (end_key) { @@ -1416,7 +1418,6 @@ int handler::read_range_first(const key_range *start_key, } range_key_part= table->key_info[active_index].key_part; - if (!start_key) // Read first record result= index_first(table->record[0]); else @@ -1438,7 +1439,6 @@ int handler::read_range_first(const key_range *start_key, SYNOPSIS read_range_next() - eq_range Set to 1 if start_key == end_key NOTES Record is read into table->record[0] @@ -1449,17 +1449,19 @@ int handler::read_range_first(const key_range *start_key, # Error code */ -int handler::read_range_next(bool eq_range) +int handler::read_range_next() { int result; DBUG_ENTER("handler::read_range_next"); if (eq_range) - result= index_next_same(table->record[0], - end_range->key, - end_range->length); - else - result= index_next(table->record[0]); + { + /* We trust that index_next_same always gives a row in range */ + DBUG_RETURN(index_next_same(table->record[0], + end_range->key, + end_range->length)); + } + result= index_next(table->record[0]); if (result) DBUG_RETURN(result); DBUG_RETURN(compare_key(end_range) <= 0 ? 0 : HA_ERR_END_OF_FILE); @@ -1467,16 +1469,18 @@ int handler::read_range_next(bool eq_range) /* - Compare if found key is over max-value + Compare if found key (in row) is over max-value SYNOPSIS compare_key - range key to compare to row + range range to compare to row. May be 0 for no range NOTES - For this to work, the row must be stored in table->record[0] + See key.cc::key_cmp() for details RETURN + The return value is SIGN(key_in_row - range_key): + 0 Key is equal to range or 'range' == 0 (no range) -1 Key is less than range 1 Key is larger than range @@ -1484,37 +1488,11 @@ int handler::read_range_next(bool eq_range) int handler::compare_key(key_range *range) { - KEY_PART_INFO *key_part= range_key_part; - uint store_length; - + int cmp; if (!range) return 0; // No max range - - for (const char *key= (const char*) range->key, *end=key+range->length; - key < end; - key+= store_length, key_part++) - { - int cmp; - store_length= key_part->store_length; - if (key_part->null_bit) - { - if (*key) - { - if (!key_part->field->is_null()) - return 1; - continue; - } - else if (key_part->field->is_null()) - return 0; - key++; // Skip null byte - store_length--; - } - if ((cmp=key_part->field->key_cmp((byte*) key, key_part->length)) < 0) - return -1; - if (cmp > 0) - return 1; - } - return key_compare_result_on_equal; + cmp= key_cmp(range_key_part, range->key, range->length); + if (!cmp) + cmp= key_compare_result_on_equal; + return cmp; } - - diff --git a/sql/handler.h b/sql/handler.h index 4f721a01412..bbeefa7c916 100644 --- a/sql/handler.h +++ b/sql/handler.h @@ -234,14 +234,6 @@ typedef struct st_ha_check_opt } HA_CHECK_OPT; -typedef struct st_key_range -{ - const byte *key; - uint length; - enum ha_rkey_function flag; -} key_range; - - class handler :public Sql_alloc { protected: @@ -268,6 +260,7 @@ public: key_range save_end_range, *end_range; KEY_PART_INFO *range_key_part; int key_compare_result_on_equal; + bool eq_range; uint errkey; /* Last dup key */ uint sortkey, key_used_on_scan; @@ -330,13 +323,14 @@ public: return (my_errno=HA_ERR_WRONG_COMMAND); } virtual int read_range_first(const key_range *start_key, - const key_range *end_key, - bool sorted); - virtual int read_range_next(bool eq_range); + const key_range *end_key, + bool eq_range, bool sorted); + virtual int read_range_next(); int compare_key(key_range *range); virtual int ft_init() { return -1; } - virtual FT_INFO *ft_init_ext(uint flags,uint inx,const byte *key, uint keylen) + virtual FT_INFO *ft_init_ext(uint flags,uint inx,const byte *key, + uint keylen) { return NULL; } virtual int ft_read(byte *buf) { return -1; } virtual int rnd_init(bool scan=1)=0; @@ -345,11 +339,8 @@ public: virtual int rnd_pos(byte * buf, byte *pos)=0; virtual int read_first_row(byte *buf, uint primary_key); virtual int restart_rnd_next(byte *buf, byte *pos); - virtual ha_rows records_in_range(int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag) + virtual ha_rows records_in_range(uint inx, key_range *min_key, + key_range *max_key) { return (ha_rows) 10; } virtual void position(const byte *record)=0; virtual my_off_t row_position() { return HA_OFFSET_ERROR; } diff --git a/sql/key.cc b/sql/key.cc index a2c3b2c8989..9425a368669 100644 --- a/sql/key.cc +++ b/sql/key.cc @@ -156,9 +156,28 @@ void key_restore(TABLE *table,byte *key,uint idx,uint key_length) } /* key_restore */ - /* Compare if a key has changed */ +/* + Compare if a key has changed + + SYNOPSIS + key_cmp_if_same() + table TABLE + key key to compare to row + idx Index used + key_length Length of key + + NOTES + In theory we could just call field->cmp() for all field types, + but as we are only interested if a key has changed (not if the key is + larger or smaller than the previous value) we can do things a bit + faster by using memcmp() instead. + + RETURN + 0 If key is equal + 1 Key has changed +*/ -int key_cmp(TABLE *table,const byte *key,uint idx,uint key_length) +bool key_cmp_if_same(TABLE *table,const byte *key,uint idx,uint key_length) { uint length; KEY_PART_INFO *key_part; @@ -281,3 +300,56 @@ bool check_if_key_used(TABLE *table, uint idx, List<Item> &fields) return check_if_key_used(table, table->primary_key, fields); return 0; } + + +/* + Compare key in row to a given key + + SYNOPSIS + key_cmp() + key_part Key part handler + key Key to compare to value in table->record[0] + key_length length of 'key' + + RETURN + The return value is SIGN(key_in_row - range_key): + + 0 Key is equal to range or 'range' == 0 (no range) + -1 Key is less than range + 1 Key is larger than range +*/ + +int key_cmp(KEY_PART_INFO *key_part, const byte *key, uint key_length) +{ + uint store_length; + + for (const byte *end=key + key_length; + key < end; + key+= store_length, key_part++) + { + int cmp; + store_length= key_part->store_length; + if (key_part->null_bit) + { + /* This key part allows null values; NULL is lower than everything */ + register bool field_is_null= key_part->field->is_null(); + if (*key) // If range key is null + { + /* the range is expecting a null value */ + if (!field_is_null) + return 1; // Found key is > range + /* null -- exact match, go to next key part */ + continue; + } + else if (field_is_null) + return -1; // NULL is less than any value + key++; // Skip null byte + store_length--; + } + if ((cmp=key_part->field->key_cmp((byte*) key, key_part->length)) < 0) + return -1; + if (cmp > 0) + return 1; + } + return 0; // Keys are equal +} diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h index 8cae95e5070..14bd16332ef 100644 --- a/sql/mysql_priv.h +++ b/sql/mysql_priv.h @@ -754,11 +754,12 @@ void mysql_print_status(THD *thd); int find_ref_key(TABLE *form,Field *field, uint *offset); void key_copy(byte *key,TABLE *form,uint index,uint key_length); void key_restore(TABLE *form,byte *key,uint index,uint key_length); -int key_cmp(TABLE *form,const byte *key,uint index,uint key_length); +bool key_cmp_if_same(TABLE *form,const byte *key,uint index,uint key_length); void key_unpack(String *to,TABLE *form,uint index); bool check_if_key_used(TABLE *table, uint idx, List<Item> &fields); -bool init_errmessage(void); +int key_cmp(KEY_PART_INFO *key_part, const byte *key, uint key_length); +bool init_errmessage(void); void sql_perror(const char *message); void sql_print_error(const char *format,...) __attribute__ ((format (printf, 1, 2))); diff --git a/sql/opt_range.cc b/sql/opt_range.cc index 1e7bf90738c..3cc64c35223 100644 --- a/sql/opt_range.cc +++ b/sql/opt_range.cc @@ -1800,8 +1800,8 @@ SEL_ARG * SEL_ARG::insert(SEL_ARG *key) { SEL_ARG *element,**par,*last_element; - LINT_INIT(par); LINT_INIT(last_element); + for (element= this; element != &null_element ; ) { last_element=element; @@ -2296,26 +2296,32 @@ check_quick_keys(PARAM *param,uint idx,SEL_ARG *key_tree, { if (tmp_min_flag & GEOM_FLAG) { - tmp= param->table->file-> - records_in_range((int) keynr, (byte*)(param->min_key), - min_key_length, - (ha_rkey_function)(tmp_min_flag ^ GEOM_FLAG), - (byte *)NullS, 0, HA_READ_KEY_EXACT); + key_range min_range; + min_range.key= (byte*) param->min_key; + min_range.length= min_key_length; + /* In this case tmp_min_flag contains the handler-read-function */ + min_range.flag= (ha_rkey_function) (tmp_min_flag ^ GEOM_FLAG); + + tmp= param->table->file->records_in_range(keynr, &min_range, + (key_range*) 0); } else { - tmp=param->table->file-> - records_in_range((int) keynr, - (byte*) (!min_key_length ? NullS : - param->min_key), - min_key_length, - tmp_min_flag & NEAR_MIN ? - HA_READ_AFTER_KEY : HA_READ_KEY_EXACT, - (byte*) (!max_key_length ? NullS : - param->max_key), - max_key_length, - (tmp_max_flag & NEAR_MAX ? - HA_READ_BEFORE_KEY : HA_READ_AFTER_KEY)); + key_range min_range, max_range; + + min_range.key= (byte*) param->min_key; + min_range.length= min_key_length; + min_range.flag= (tmp_min_flag & NEAR_MIN ? HA_READ_AFTER_KEY : + HA_READ_KEY_EXACT); + max_range.key= param->max_key; + max_range.length= max_key_length; + max_range.flag= (tmp_max_flag & NEAR_MAX ? + HA_READ_BEFORE_KEY : HA_READ_AFTER_KEY); + tmp=param->table->file->records_in_range(keynr, + (min_key_length ? &min_range : + (key_range*) 0), + (max_key_length ? &max_range : + (key_range*) 0)); } } end: @@ -2604,7 +2610,7 @@ int QUICK_SELECT::get_next() if (range) { // Already read through key - result= file->read_range_next(test(range->flag & EQ_RANGE)); + result= file->read_range_next(); if (result != HA_ERR_END_OF_FILE) DBUG_RETURN(result); } @@ -2628,6 +2634,7 @@ int QUICK_SELECT::get_next() result= file->read_range_first(range->min_length ? &start_key : 0, range->max_length ? &end_key : 0, + test(range->flag & EQ_RANGE), sorted); if (range->flag == (UNIQUE_RANGE | EQ_RANGE)) range=0; // Stop searching @@ -2815,40 +2822,14 @@ int QUICK_SELECT_DESC::get_next() int QUICK_SELECT_DESC::cmp_prev(QUICK_RANGE *range_arg) { + int cmp; if (range_arg->flag & NO_MIN_RANGE) return 0; /* key can't be to small */ - KEY_PART *key_part = key_parts; - uint store_length; - - for (char *key = range_arg->min_key, *end = key + range_arg->min_length; - key < end; - key += store_length, key_part++) - { - int cmp; - store_length= key_part->store_length; - if (key_part->null_bit) - { - // this key part allows null values; NULL is lower than everything else - if (*key) - { - // the range is expecting a null value - if (!key_part->field->is_null()) - return 0; // not null -- still inside the range - continue; // null -- exact match, go to next key part - } - else if (key_part->field->is_null()) - return 1; // null -- outside the range - key++; - store_length--; - } - if ((cmp = key_part->field->key_cmp((byte*) key, - key_part->length)) > 0) - return 0; - if (cmp < 0) - return 1; - } - return (range_arg->flag & NEAR_MIN) ? 1 : 0; // Exact match + cmp= key_cmp(key_part_info, range_arg->min_key, range_arg->min_length); + if (cmp > 0 || cmp == 0 && !(range_arg->flag & NEAR_MIN)) + return 0; + return 1; // outside of range } diff --git a/sql/opt_range.h b/sql/opt_range.h index 2df9d93e1ef..2072ded15d1 100644 --- a/sql/opt_range.h +++ b/sql/opt_range.h @@ -80,13 +80,18 @@ public: MEM_ROOT alloc; KEY_PART *key_parts; + KEY_PART_INFO *key_part_info; ha_rows records; double read_time; QUICK_SELECT(THD *thd, TABLE *table,uint index_arg,bool no_alloc=0); virtual ~QUICK_SELECT(); void reset(void) { next=0; it.rewind(); } - int init() { return error=file->index_init(index); } + int init() + { + key_part_info= head->key_info[index].key_part; + return error=file->index_init(index); + } virtual int get_next(); virtual bool reverse_sorted() { return 0; } bool unique_key_range(); diff --git a/sql/opt_sum.cc b/sql/opt_sum.cc index 4fdcd093132..8c1cd9ce1cb 100644 --- a/sql/opt_sum.cc +++ b/sql/opt_sum.cc @@ -716,7 +716,7 @@ static bool find_key_for_maxmin(bool max_fl, TABLE_REF *ref, static int reckey_in_range(bool max_fl, TABLE_REF *ref, Field* field, COND *cond, uint range_fl, uint prefix_len) { - if (key_cmp(field->table, ref->key_buff, ref->key, prefix_len)) + if (key_cmp_if_same(field->table, ref->key_buff, ref->key, prefix_len)) return 1; if (!cond || (range_fl & (max_fl ? NO_MIN_RANGE : NO_MAX_RANGE))) return 0; diff --git a/sql/sql_acl.cc b/sql/sql_acl.cc index 9c7fe3e2993..447a5d9ae80 100644 --- a/sql/sql_acl.cc +++ b/sql/sql_acl.cc @@ -1820,7 +1820,7 @@ GRANT_TABLE::GRANT_TABLE(TABLE *form, TABLE *col_privs) } my_hash_insert(&hash_columns, (byte *) mem_check); } while (!col_privs->file->index_next(col_privs->record[0]) && - !key_cmp(col_privs,key,0,key_len)); + !key_cmp_if_same(col_privs,key,0,key_len)); } } @@ -2043,7 +2043,7 @@ static int replace_column_table(GRANT_TABLE *g_t, } } } while (!table->file->index_next(table->record[0]) && - !key_cmp(table,key,0,key_length)); + !key_cmp_if_same(table,key,0,key_length)); } end: diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 5db4a0042ea..5e2525f046a 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -6162,8 +6162,8 @@ join_read_prev_same(READ_RECORD *info) if ((error=table->file->index_prev(table->record[0]))) return report_error(table, error); - if (key_cmp(table, tab->ref.key_buff, tab->ref.key, - tab->ref.key_length)) + if (key_cmp_if_same(table, tab->ref.key_buff, tab->ref.key, + tab->ref.key_length)) { table->status=STATUS_NOT_FOUND; error= -1; |