~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/myisam/mi_range.c

Removed/replaced a couple DBUG calls

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
/*
17
17
  Gives a approximated number of how many records there is between two keys.
18
18
  Used when optimizing querries.
19
19
 */
20
20
 
21
 
#include "myisam_priv.h"
22
 
 
23
 
using namespace drizzled;
24
 
 
25
 
static ha_rows _mi_record_pos(MI_INFO *, const unsigned char *, key_part_map,
 
21
#include "myisamdef.h"
 
22
 
 
23
static ha_rows _mi_record_pos(MI_INFO *, const uchar *, key_part_map,
26
24
                              enum ha_rkey_function);
27
 
static double _mi_search_pos(MI_INFO *,MI_KEYDEF *,unsigned char *, uint,uint,internal::my_off_t);
28
 
static uint32_t _mi_keynr(MI_INFO *info,MI_KEYDEF *,unsigned char *, unsigned char *,uint32_t *);
 
25
static double _mi_search_pos(MI_INFO *,MI_KEYDEF *,uchar *, uint,uint,my_off_t);
 
26
static uint _mi_keynr(MI_INFO *info,MI_KEYDEF *,uchar *, uchar *,uint *);
29
27
 
30
28
/*
31
29
  Estimate how many records there is in a given range
44
42
    HA_POS_ERROR  error (or we can't estimate number of rows)
45
43
    number        Estimated number of rows
46
44
*/
47
 
 
 
45
  
48
46
ha_rows mi_records_in_range(MI_INFO *info, int inx,
49
47
                            key_range *min_key, key_range *max_key)
50
48
{
51
49
  ha_rows start_pos,end_pos,res;
 
50
  DBUG_ENTER("mi_records_in_range");
52
51
 
53
52
  if ((inx = _mi_check_index(info,inx)) < 0)
54
 
    return(HA_POS_ERROR);
 
53
    DBUG_RETURN(HA_POS_ERROR);
55
54
 
56
55
  if (fast_mi_readinfo(info))
57
 
    return(HA_POS_ERROR);
 
56
    DBUG_RETURN(HA_POS_ERROR);
58
57
  info->update&= (HA_STATE_CHANGED+HA_STATE_ROW_CHANGED);
 
58
  if (info->s->concurrent_insert)
 
59
    rw_rdlock(&info->s->key_root_lock[inx]);
59
60
 
60
61
  switch(info->s->keyinfo[inx].key_alg){
61
62
  case HA_KEY_ALG_BTREE:
72
73
      res=HA_POS_ERROR;
73
74
  }
74
75
 
 
76
  if (info->s->concurrent_insert)
 
77
    rw_unlock(&info->s->key_root_lock[inx]);
75
78
  fast_mi_writeinfo(info);
76
79
 
77
 
  return(res);
 
80
  DBUG_PRINT("info",("records: %ld",(ulong) (res)));
 
81
  DBUG_RETURN(res);
78
82
}
79
83
 
80
84
 
81
85
        /* Find relative position (in records) for key in index-tree */
82
86
 
83
 
static ha_rows _mi_record_pos(MI_INFO *info, const unsigned char *key,
 
87
static ha_rows _mi_record_pos(MI_INFO *info, const uchar *key,
84
88
                              key_part_map keypart_map,
85
89
                              enum ha_rkey_function search_flag)
86
90
{
87
 
  uint32_t inx=(uint) info->lastinx, nextflag, key_len;
 
91
  uint inx=(uint) info->lastinx, nextflag, key_len;
88
92
  MI_KEYDEF *keyinfo=info->s->keyinfo+inx;
89
 
  unsigned char *key_buff;
 
93
  uchar *key_buff;
90
94
  double pos;
91
95
 
92
 
  assert(keypart_map);
 
96
  DBUG_ENTER("_mi_record_pos");
 
97
  DBUG_PRINT("enter",("search_flag: %d",search_flag));
 
98
  DBUG_ASSERT(keypart_map);
93
99
 
94
100
  key_buff=info->lastkey+info->s->base.max_key_length;
95
 
  key_len=_mi_pack_key(info,inx,key_buff,(unsigned char*) key, keypart_map,
 
101
  key_len=_mi_pack_key(info,inx,key_buff,(uchar*) key, keypart_map,
96
102
                       (HA_KEYSEG**) 0);
 
103
  DBUG_EXECUTE("key",_mi_print_key(DBUG_FILE,keyinfo->seg,
 
104
                                    (uchar*) key_buff,key_len););
97
105
  nextflag=myisam_read_vec[search_flag];
98
106
  if (!(nextflag & (SEARCH_FIND | SEARCH_NO_FIND | SEARCH_LAST)))
99
107
    key_len=USE_WHOLE_KEY;
137
145
                     info->s->state.key_root[inx]);
138
146
  if (pos >= 0.0)
139
147
  {
140
 
    return((uint32_t) (pos*info->state->records+0.5));
 
148
    DBUG_PRINT("exit",("pos: %ld",(ulong) (pos*info->state->records)));
 
149
    DBUG_RETURN((ulong) (pos*info->state->records+0.5));
141
150
  }
142
 
  return(HA_POS_ERROR);
 
151
  DBUG_RETURN(HA_POS_ERROR);
143
152
}
144
153
 
145
154
 
148
157
 
149
158
static double _mi_search_pos(register MI_INFO *info,
150
159
                             register MI_KEYDEF *keyinfo,
151
 
                             unsigned char *key, uint32_t key_len, uint32_t nextflag,
152
 
                             register internal::my_off_t pos)
 
160
                             uchar *key, uint key_len, uint nextflag,
 
161
                             register my_off_t pos)
153
162
{
154
163
  int flag;
155
 
  uint32_t nod_flag, keynr, max_keynr= 0;
156
 
  bool after_key;
157
 
  unsigned char *keypos,*buff;
 
164
  uint nod_flag, keynr, max_keynr= 0;
 
165
  my_bool after_key;
 
166
  uchar *keypos,*buff;
158
167
  double offset;
 
168
  DBUG_ENTER("_mi_search_pos");
159
169
 
160
170
  if (pos == HA_OFFSET_ERROR)
161
 
    return(0.5);
 
171
    DBUG_RETURN(0.5);
162
172
 
163
173
  if (!(buff=_mi_fetch_keypage(info,keyinfo,pos,DFLT_INIT_HITS,info->buff,1)))
164
174
    goto err;
170
180
  if (flag)
171
181
  {
172
182
    if (flag == MI_FOUND_WRONG_KEY)
173
 
      return(-1);                               /* error */
 
183
      DBUG_RETURN(-1);                          /* error */
174
184
    /*
175
185
      Didn't found match. keypos points at next (bigger) key
176
186
      Try to find a smaller, better matching key.
180
190
      offset= 1.0;
181
191
    else if ((offset=_mi_search_pos(info,keyinfo,key,key_len,nextflag,
182
192
                                    _mi_kpos(nod_flag,keypos))) < 0)
183
 
      return(offset);
 
193
      DBUG_RETURN(offset);
184
194
  }
185
195
  else
186
196
  {
199
209
      */
200
210
      if ((offset=_mi_search_pos(info,keyinfo,key,key_len,SEARCH_FIND,
201
211
                                 _mi_kpos(nod_flag,keypos))) < 0)
202
 
        return(offset);                 /* Read error */
 
212
        DBUG_RETURN(offset);                    /* Read error */
203
213
    }
204
214
  }
205
 
  return((keynr+offset)/(max_keynr+1));
 
215
  DBUG_PRINT("info",("keynr: %d  offset: %g  max_keynr: %d  nod: %d  flag: %d",
 
216
                     keynr,offset,max_keynr,nod_flag,flag));
 
217
  DBUG_RETURN((keynr+offset)/(max_keynr+1));
206
218
err:
207
 
  return (-1.0);
 
219
  DBUG_PRINT("exit",("Error: %d",my_errno));
 
220
  DBUG_RETURN (-1.0);
208
221
}
209
222
 
210
223
 
211
224
        /* Get keynummer of current key and max number of keys in nod */
212
225
 
213
 
static uint32_t _mi_keynr(MI_INFO *info, register MI_KEYDEF *keyinfo, unsigned char *page,
214
 
                      unsigned char *keypos, uint32_t *ret_max_key)
 
226
static uint _mi_keynr(MI_INFO *info, register MI_KEYDEF *keyinfo, uchar *page,
 
227
                      uchar *keypos, uint *ret_max_key)
215
228
{
216
 
  uint32_t nod_flag,keynr,max_key;
217
 
  unsigned char t_buff[MI_MAX_KEY_BUFF],*end;
 
229
  uint nod_flag,keynr,max_key;
 
230
  uchar t_buff[MI_MAX_KEY_BUFF],*end;
218
231
 
219
232
  end= page+mi_getint(page);
220
233
  nod_flag=mi_test_if_nod(page);