23 #include "myisamdef.h"
26 static ha_rows _mi_record_pos(
MI_INFO *,
const uchar *, key_part_map,
27 enum ha_rkey_function);
28 static double _mi_search_pos(
MI_INFO *,
MI_KEYDEF *,uchar *, uint,uint,my_off_t);
49 ha_rows mi_records_in_range(
MI_INFO *info,
int inx,
52 ha_rows start_pos,end_pos,res;
53 DBUG_ENTER(
"mi_records_in_range");
55 if ((inx = _mi_check_index(info,inx)) < 0)
56 DBUG_RETURN(HA_POS_ERROR);
58 if (fast_mi_readinfo(info))
59 DBUG_RETURN(HA_POS_ERROR);
60 info->update&= (HA_STATE_CHANGED+HA_STATE_ROW_CHANGED);
61 if (info->s->concurrent_insert)
64 switch(info->s->keyinfo[inx].key_alg){
65 #ifdef HAVE_RTREE_KEYS
66 case HA_KEY_ALG_RTREE:
86 key_buff= info->lastkey+info->s->base.max_key_length;
87 start_key_len= _mi_pack_key(info,inx, key_buff,
88 (uchar*) min_key->key, min_key->keypart_map,
90 res= rtree_estimate(info, inx, key_buff, start_key_len,
91 myisam_read_vec[min_key->flag]);
96 case HA_KEY_ALG_BTREE:
98 start_pos= (min_key ? _mi_record_pos(info, min_key->key,
99 min_key->keypart_map, min_key->flag)
101 end_pos= (max_key ? _mi_record_pos(info, max_key->key,
102 max_key->keypart_map, max_key->flag)
103 : info->state->records + (ha_rows) 1);
104 res= (end_pos < start_pos ? (ha_rows) 0 :
105 (end_pos == start_pos ? (ha_rows) 1 : end_pos-start_pos));
106 if (start_pos == HA_POS_ERROR || end_pos == HA_POS_ERROR)
110 if (info->s->concurrent_insert)
112 fast_mi_writeinfo(info);
114 DBUG_PRINT(
"info",(
"records: %ld",(ulong) (res)));
121 static ha_rows _mi_record_pos(
MI_INFO *info,
const uchar *key,
122 key_part_map keypart_map,
123 enum ha_rkey_function search_flag)
125 uint inx=(uint) info->lastinx, nextflag, key_len;
130 DBUG_ENTER(
"_mi_record_pos");
131 DBUG_PRINT(
"enter",(
"search_flag: %d",search_flag));
132 DBUG_ASSERT(keypart_map);
134 key_buff=info->lastkey+info->s->base.max_key_length;
135 key_len=_mi_pack_key(info,inx,key_buff,(uchar*) key, keypart_map,
137 DBUG_EXECUTE(
"key",_mi_print_key(DBUG_FILE,keyinfo->seg,
138 (uchar*) key_buff,key_len););
139 nextflag=myisam_read_vec[search_flag];
140 if (!(nextflag & (SEARCH_FIND | SEARCH_NO_FIND | SEARCH_LAST)))
141 key_len=USE_WHOLE_KEY;
177 pos=_mi_search_pos(info,keyinfo,key_buff,key_len,
178 nextflag | SEARCH_SAVE_BUFF | SEARCH_UPDATE,
179 info->s->state.key_root[inx]);
182 DBUG_PRINT(
"exit",(
"pos: %ld",(ulong) (pos*info->state->records)));
183 DBUG_RETURN((ulong) (pos*info->state->records+0.5));
185 DBUG_RETURN(HA_POS_ERROR);
192 static double _mi_search_pos(
register MI_INFO *info,
194 uchar *key, uint key_len, uint nextflag,
195 register my_off_t pos)
198 uint nod_flag,keynr,UNINIT_VAR(max_keynr);
202 DBUG_ENTER(
"_mi_search_pos");
204 if (pos == HA_OFFSET_ERROR)
207 if (!(buff=_mi_fetch_keypage(info,keyinfo,pos,DFLT_INIT_HITS,info->buff,1)))
209 flag=(*keyinfo->bin_search)(info,keyinfo,buff,key,key_len,nextflag,
210 &keypos,info->lastkey, &after_key);
211 nod_flag=mi_test_if_nod(buff);
212 keynr=_mi_keynr(info,keyinfo,buff,keypos,&max_keynr);
216 if (flag == MI_FOUND_WRONG_KEY)
223 if (flag > 0 && ! nod_flag)
225 else if ((offset=_mi_search_pos(info,keyinfo,key,key_len,nextflag,
226 _mi_kpos(nod_flag,keypos))) < 0)
236 if ((nextflag & SEARCH_FIND) && nod_flag &&
237 ((keyinfo->flag & (HA_NOSAME | HA_NULL_PART)) != HA_NOSAME ||
238 key_len != USE_WHOLE_KEY))
244 if ((offset=_mi_search_pos(info,keyinfo,key,key_len,SEARCH_FIND,
245 _mi_kpos(nod_flag,keypos))) < 0)
249 DBUG_PRINT(
"info",(
"keynr: %d offset: %g max_keynr: %d nod: %d flag: %d",
250 keynr,offset,max_keynr,nod_flag,flag));
251 DBUG_RETURN((keynr+offset)/(max_keynr+1));
253 DBUG_PRINT(
"exit",(
"Error: %d",my_errno));
261 uchar *keypos, uint *ret_max_key)
263 uint nod_flag,keynr,max_key;
264 uchar t_buff[MI_MAX_KEY_BUFF],*end;
266 end= page+mi_getint(page);
267 nod_flag=mi_test_if_nod(page);
270 if (!(keyinfo->flag & (HA_VAR_LENGTH_KEY | HA_BINARY_PACK_KEY)))
272 *ret_max_key= (uint) (end-page)/(keyinfo->keylength+nod_flag);
273 return (uint) (keypos-
page)/(keyinfo->keylength+nod_flag);
280 if (!(*keyinfo->get_key)(keyinfo,nod_flag,&
page,t_buff))
286 *ret_max_key=max_key;