21#include "myisam_priv.h"
22#include <drizzled/message/table.h>
26static ha_rows _mi_record_pos(MI_INFO *,
const unsigned char *, key_part_map,
27 enum ha_rkey_function);
28static double _mi_search_pos(MI_INFO *,MI_KEYDEF *,
unsigned char *, uint,uint,internal::my_off_t);
29static uint32_t _mi_keynr(MI_INFO *info,MI_KEYDEF *,
unsigned char *,
unsigned char *,uint32_t *);
49ha_rows mi_records_in_range(MI_INFO *info,
int inx,
52 ha_rows start_pos,end_pos,res;
54 if ((inx = _mi_check_index(info,inx)) < 0)
57 if (fast_mi_readinfo(info))
59 info->update&= (HA_STATE_CHANGED+HA_STATE_ROW_CHANGED);
61 switch(info->s->keyinfo[inx].key_alg){
62 case message::Table::Index::BTREE:
64 start_pos= (min_key ? _mi_record_pos(info, min_key->key,
65 min_key->keypart_map, min_key->flag)
67 end_pos= (max_key ? _mi_record_pos(info, max_key->key,
68 max_key->keypart_map, max_key->flag)
69 : info->state->records + (ha_rows) 1);
70 res= (end_pos < start_pos ? (ha_rows) 0 :
71 (end_pos == start_pos ? (ha_rows) 1 : end_pos-start_pos));
72 if (start_pos == HA_POS_ERROR || end_pos == HA_POS_ERROR)
76 fast_mi_writeinfo(info);
84static ha_rows _mi_record_pos(MI_INFO *info,
const unsigned char *key,
85 key_part_map keypart_map,
86 enum ha_rkey_function search_flag)
88 uint32_t inx=(uint) info->lastinx, nextflag, key_len;
89 MI_KEYDEF *keyinfo=info->s->keyinfo+inx;
90 unsigned char *key_buff;
95 key_buff=info->lastkey+info->s->base.max_key_length;
96 key_len=_mi_pack_key(info,inx,key_buff,(
unsigned char*) key, keypart_map,
98 nextflag=myisam_read_vec[search_flag];
99 if (!(nextflag & (SEARCH_FIND | SEARCH_NO_FIND | SEARCH_LAST)))
100 key_len=USE_WHOLE_KEY;
136 pos=_mi_search_pos(info,keyinfo,key_buff,key_len,
137 nextflag | SEARCH_SAVE_BUFF | SEARCH_UPDATE,
138 info->s->state.key_root[inx]);
141 return((uint32_t) (pos*info->state->records+0.5));
143 return(HA_POS_ERROR);
150static double _mi_search_pos(MI_INFO *info,
152 unsigned char *key, uint32_t key_len, uint32_t nextflag,
153 internal::my_off_t pos)
156 uint32_t nod_flag, keynr, max_keynr= 0;
158 unsigned char *keypos,*buff;
161 if (pos == HA_OFFSET_ERROR)
164 if (!(buff=_mi_fetch_keypage(info,keyinfo,pos,DFLT_INIT_HITS,info->buff,1)))
166 flag=(*keyinfo->bin_search)(info,keyinfo,buff,key,key_len,nextflag,
167 &keypos,info->lastkey, &after_key);
168 nod_flag=mi_test_if_nod(buff);
169 keynr=_mi_keynr(info,keyinfo,buff,keypos,&max_keynr);
173 if (flag == MI_FOUND_WRONG_KEY)
180 if (flag > 0 && ! nod_flag)
182 else if ((offset=_mi_search_pos(info,keyinfo,key,key_len,nextflag,
183 _mi_kpos(nod_flag,keypos))) < 0)
193 if ((nextflag & SEARCH_FIND) && nod_flag &&
194 ((keyinfo->flag & (HA_NOSAME | HA_NULL_PART)) != HA_NOSAME ||
195 key_len != USE_WHOLE_KEY))
201 if ((offset=_mi_search_pos(info,keyinfo,key,key_len,SEARCH_FIND,
202 _mi_kpos(nod_flag,keypos))) < 0)
206 return((keynr+offset)/(max_keynr+1));
214static uint32_t _mi_keynr(MI_INFO *info, MI_KEYDEF *keyinfo,
unsigned char *page,
215 unsigned char *keypos, uint32_t *ret_max_key)
217 uint32_t nod_flag,keynr,max_key;
218 unsigned char t_buff[MI_MAX_KEY_BUFF],*end;
220 end= page+mi_getint(page);
221 nod_flag=mi_test_if_nod(page);
224 if (!(keyinfo->flag & (HA_VAR_LENGTH_KEY | HA_BINARY_PACK_KEY)))
226 *ret_max_key= (uint) (end-page)/(keyinfo->keylength+nod_flag);
227 return (uint) (keypos-page)/(keyinfo->keylength+nod_flag);
234 if (!(*keyinfo->get_key)(keyinfo,nod_flag,&page,t_buff))
240 *ret_max_key=max_key;
TODO: Rename this file - func.h is stupid.