mi_search.c 55.1 KB
Newer Older
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3 4 5 6
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8 9 10 11
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
12

bk@work.mysql.com's avatar
bk@work.mysql.com committed
13 14 15 16 17 18 19 20 21 22
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/* key handling functions */

#include "fulltext.h"
#include "m_ctype.h"

static my_bool _mi_get_prev_key(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *page,
23 24
                                uchar *key, uchar *keypos,
                                uint *return_key_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
25

26
        /* Check index */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
27 28 29

int _mi_check_index(MI_INFO *info, int inx)
{
30
  if (inx == -1)                        /* Use last index */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
31 32 33 34 35 36
    inx=info->lastinx;
  if (inx < 0 || ! (((ulonglong) 1 << inx) & info->s->state.key_map))
  {
    my_errno=HA_ERR_WRONG_INDEX;
    return -1;
  }
37
  if (info->lastinx != inx)             /* Index changed */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
38 39 40 41
  {
    info->lastinx = inx;
    info->page_changed=1;
    info->update= ((info->update & (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED)) |
42
                   HA_STATE_NEXT_FOUND | HA_STATE_PREV_FOUND);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
43 44 45 46 47 48 49
  }
  if (info->opt_flag & WRITE_CACHE_USED && flush_io_cache(&info->rec_cache))
    return(-1);
  return(inx);
} /* mi_check_index */


50 51 52 53 54 55
        /*
        ** Search after row by a key
        ** Position to row is stored in info->lastpos
        ** Return: -1 if not found
        **          1 if one should continue search on higher level
        */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
56 57

int _mi_search(register MI_INFO *info, register MI_KEYDEF *keyinfo,
58
               uchar *key, uint key_len, uint nextflag, register my_off_t pos)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
59 60 61 62 63 64 65
{
  my_bool last_key;
  int error,flag;
  uint nod_flag;
  uchar *keypos,*maxpos;
  uchar lastkey[MI_MAX_KEY_BUFF],*buff;
  DBUG_ENTER("_mi_search");
66 67
  DBUG_PRINT("enter",("pos: %lu  nextflag: %u  lastpos: %lu",
                      (ulong) pos, nextflag, (ulong) info->lastpos));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
68 69 70 71
  DBUG_EXECUTE("key",_mi_print_key(DBUG_FILE,keyinfo->seg,key,key_len););

  if (pos == HA_OFFSET_ERROR)
  {
72
    my_errno=HA_ERR_KEY_NOT_FOUND;                      /* Didn't find key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
73 74
    info->lastpos= HA_OFFSET_ERROR;
    if (!(nextflag & (SEARCH_SMALLER | SEARCH_BIGGER | SEARCH_LAST)))
75 76
      DBUG_RETURN(-1);                          /* Not found ; return error */
    DBUG_RETURN(1);                             /* Search at upper levels */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
77 78
  }

79
  if (!(buff=_mi_fetch_keypage(info,keyinfo,pos,DFLT_INIT_HITS,info->buff,
80
                               test(!(nextflag & SEARCH_SAVE_BUFF)))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
81 82 83 84
    goto err;
  DBUG_DUMP("page",(byte*) buff,mi_getint(buff));

  flag=(*keyinfo->bin_search)(info,keyinfo,buff,key,key_len,nextflag,
85
                              &keypos,lastkey, &last_key);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
86 87 88 89 90 91 92 93
  if (flag == MI_FOUND_WRONG_KEY)
    DBUG_RETURN(-1);
  nod_flag=mi_test_if_nod(buff);
  maxpos=buff+mi_getint(buff)-1;

  if (flag)
  {
    if ((error=_mi_search(info,keyinfo,key,key_len,nextflag,
94
                          _mi_kpos(nod_flag,keypos))) <= 0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
95 96 97 98 99
      DBUG_RETURN(error);

    if (flag >0)
    {
      if (nextflag & (SEARCH_SMALLER | SEARCH_LAST) &&
100 101
          keypos == buff+2+nod_flag)
        DBUG_RETURN(1);                                 /* Bigger than key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
102 103
    }
    else if (nextflag & SEARCH_BIGGER && keypos >= maxpos)
104
      DBUG_RETURN(1);                                   /* Smaller than key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
105 106 107
  }
  else
  {
108 109 110
    if ((nextflag & SEARCH_FIND) && nod_flag &&
	((keyinfo->flag & (HA_NOSAME | HA_NULL_PART)) != HA_NOSAME ||
	 key_len != USE_WHOLE_KEY))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
111 112
    {
      if ((error=_mi_search(info,keyinfo,key,key_len,SEARCH_FIND,
113 114 115
                            _mi_kpos(nod_flag,keypos))) >= 0 ||
          my_errno != HA_ERR_KEY_NOT_FOUND)
        DBUG_RETURN(error);
116
      info->last_keypage= HA_OFFSET_ERROR;              /* Buffer not in mem */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
117 118 119 120 121
    }
  }
  if (pos != info->last_keypage)
  {
    uchar *old_buff=buff;
122
    if (!(buff=_mi_fetch_keypage(info,keyinfo,pos,DFLT_INIT_HITS,info->buff,
123
                                 test(!(nextflag & SEARCH_SAVE_BUFF)))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
124 125 126 127 128 129 130 131 132
      goto err;
    keypos=buff+(keypos-old_buff);
    maxpos=buff+(maxpos-old_buff);
  }

  if ((nextflag & (SEARCH_SMALLER | SEARCH_LAST)) && flag != 0)
  {
    uint not_used;
    if (_mi_get_prev_key(info,keyinfo, buff, info->lastkey, keypos,
133
                         &info->lastkey_length))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
134
      goto err;
135
    if (!(nextflag & SEARCH_SMALLER) &&
136
        ha_key_cmp(keyinfo->seg, info->lastkey, key, key_len, SEARCH_FIND,
137
                    &not_used))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
138
    {
139
      my_errno=HA_ERR_KEY_NOT_FOUND;                    /* Didn't find key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
      goto err;
    }
  }
  else
  {
    info->lastkey_length=(*keyinfo->get_key)(keyinfo,nod_flag,&keypos,lastkey);
    if (!info->lastkey_length)
      goto err;
    memcpy(info->lastkey,lastkey,info->lastkey_length);
  }
  info->lastpos=_mi_dpos(info,0,info->lastkey+info->lastkey_length);
  /* Save position for a possible read next / previous */
  info->int_keypos=info->buff+ (keypos-buff);
  info->int_maxpos=info->buff+ (maxpos-buff);
  info->int_nod_flag=nod_flag;
  info->int_keytree_version=keyinfo->version;
  info->last_search_keypage=info->last_keypage;
  info->page_changed=0;
158
  info->buff_used= (info->buff != buff);        /* If we have to reread buff */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
159

160
  DBUG_PRINT("exit",("found key at %lu",(ulong) info->lastpos));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
161 162 163 164 165 166 167 168 169
  DBUG_RETURN(0);
err:
  DBUG_PRINT("exit",("Error: %d",my_errno));
  info->lastpos= HA_OFFSET_ERROR;
  info->page_changed=1;
  DBUG_RETURN (-1);
} /* _mi_search */


170 171 172 173
        /* Search after key in page-block */
        /* If packed key puts smaller or identical key in buff */
        /* ret_pos point to where find or bigger key starts */
        /* ARGSUSED */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
174 175

int _mi_bin_search(MI_INFO *info, register MI_KEYDEF *keyinfo, uchar *page,
176 177
                   uchar *key, uint key_len, uint comp_flag, uchar **ret_pos,
                   uchar *buff __attribute__((unused)), my_bool *last_key)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
{
  reg4 int start,mid,end,save_end;
  int flag;
  uint totlength,nod_flag,not_used;
  DBUG_ENTER("_mi_bin_search");

  LINT_INIT(flag);
  totlength=keyinfo->keylength+(nod_flag=mi_test_if_nod(page));
  start=0; mid=1;
  save_end=end=(int) ((mi_getint(page)-2-nod_flag)/totlength-1);
  DBUG_PRINT("test",("mi_getint: %d  end: %d",mi_getint(page),end));
  page+=2+nod_flag;

  while (start != end)
  {
    mid= (start+end)/2;
194
    if ((flag=ha_key_cmp(keyinfo->seg,page+(uint) mid*totlength,key,key_len,
195 196
                          comp_flag,&not_used))
        >= 0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
197 198 199 200 201
      end=mid;
    else
      start=mid+1;
  }
  if (mid != start)
202
    flag=ha_key_cmp(keyinfo->seg,page+(uint) start*totlength,key,key_len,
203
                     comp_flag,&not_used);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
204
  if (flag < 0)
205
    start++;                    /* point at next, bigger key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
206 207 208 209 210 211 212
  *ret_pos=page+(uint) start*totlength;
  *last_key= end == save_end;
  DBUG_PRINT("exit",("flag: %d  keypos: %d",flag,start));
  DBUG_RETURN(flag);
} /* _mi_bin_search */


213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
/*
  Locate a packed key in a key page.

  SYNOPSIS
    _mi_seq_search()
    info                        Open table information.
    keyinfo                     Key definition information.
    page                        Key page (beginning).
    key                         Search key.
    key_len                     Length to use from search key or USE_WHOLE_KEY
    comp_flag                   Search flags like SEARCH_SAME etc.
    ret_pos             RETURN  Position in key page behind this key.
    buff                RETURN  Copy of previous or identical unpacked key.
    last_key            RETURN  If key is last in page.

  DESCRIPTION
    Used instead of _mi_bin_search() when key is packed.
    Puts smaller or identical key in buff.
    Key is searched sequentially.

  RETURN
    > 0         Key in 'buff' is smaller than search key.
    0           Key in 'buff' is identical to search key.
    < 0         Not found.
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
238 239

int _mi_seq_search(MI_INFO *info, register MI_KEYDEF *keyinfo, uchar *page,
240 241
                   uchar *key, uint key_len, uint comp_flag, uchar **ret_pos,
                   uchar *buff, my_bool *last_key)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
242 243 244 245 246 247 248 249 250 251 252
{
  int flag;
  uint nod_flag,length,not_used;
  uchar t_buff[MI_MAX_KEY_BUFF],*end;
  DBUG_ENTER("_mi_seq_search");

  LINT_INIT(flag); LINT_INIT(length);
  end= page+mi_getint(page);
  nod_flag=mi_test_if_nod(page);
  page+=2+nod_flag;
  *ret_pos=page;
253
  t_buff[0]=0;                                  /* Avoid bugs */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
254 255 256 257 258 259
  while (page < end)
  {
    length=(*keyinfo->get_key)(keyinfo,nod_flag,&page,t_buff);
    if (length == 0 || page > end)
    {
      my_errno=HA_ERR_CRASHED;
260 261
      DBUG_PRINT("error",("Found wrong key:  length: %u  page: %p  end: %p",
                          length, page, end));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
262 263
      DBUG_RETURN(MI_FOUND_WRONG_KEY);
    }
264
    if ((flag=ha_key_cmp(keyinfo->seg,t_buff,key,key_len,comp_flag,
265
                          &not_used)) >= 0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
266 267
      break;
#ifdef EXTRA_DEBUG
268
    DBUG_PRINT("loop",("page: %p  key: '%s'  flag: %d", page, t_buff, flag));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
269 270 271 272 273
#endif
    memcpy(buff,t_buff,length);
    *ret_pos=page;
  }
  if (flag == 0)
274
    memcpy(buff,t_buff,length);                 /* Result is first key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
275
  *last_key= page == end;
276
  DBUG_PRINT("exit",("flag: %d  ret_pos: %p", flag, *ret_pos));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
277 278 279
  DBUG_RETURN(flag);
} /* _mi_seq_search */

280

281 282 283 284
int _mi_prefix_search(MI_INFO *info, register MI_KEYDEF *keyinfo, uchar *page,
                      uchar *key, uint key_len, uint nextflag, uchar **ret_pos,
                      uchar *buff, my_bool *last_key)
{
285 286 287
  /*
    my_flag is raw comparison result to be changed according to
    SEARCH_NO_FIND,SEARCH_LAST and HA_REVERSE_SORT flags.
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
288
    flag is the value returned by ha_key_cmp and as treated as final
289
  */
290
  int flag=0, my_flag=-1;
291
  uint nod_flag, length, len, matched, cmplen, kseg_len;
292
  uint prefix_len,suffix_len;
293
  int key_len_skip, seg_len_pack, key_len_left;
294 295 296 297
  uchar *end, *kseg, *vseg;
  uchar *sort_order=keyinfo->seg->charset->sort_order;
  uchar tt_buff[MI_MAX_KEY_BUFF+2], *t_buff=tt_buff+2;
  uchar *saved_from, *saved_to, *saved_vseg;
298 299
  uint  saved_length=0, saved_prefix_len=0;
  uint  length_pack;
300 301
  DBUG_ENTER("_mi_prefix_search");

302 303 304 305 306 307 308
  LINT_INIT(length);
  LINT_INIT(prefix_len);
  LINT_INIT(seg_len_pack);
  LINT_INIT(saved_from);
  LINT_INIT(saved_to);
  LINT_INIT(saved_vseg);

309 310 311 312 313 314 315
  t_buff[0]=0;                                  /* Avoid bugs */
  end= page+mi_getint(page);
  nod_flag=mi_test_if_nod(page);
  page+=2+nod_flag;
  *ret_pos=page;
  kseg=key;

316 317 318 319 320
  get_key_pack_length(kseg_len,length_pack,kseg);
  key_len_skip=length_pack+kseg_len;
  key_len_left=(int) key_len- (int) key_len_skip;
  cmplen=(key_len_left>=0) ? kseg_len : key_len-length_pack;
  DBUG_PRINT("info",("key: '%.*s'",kseg_len,kseg));
321

322 323
  /*
    Keys are compressed the following way:
324

325 326 327 328 329 330 331 332
    If the max length of first key segment <= 127 characters the prefix is
    1 byte else it's 2 byte

    prefix         The high bit is set if this is a prefix for the prev key
    length         Packed length if the previous was a prefix byte
    [length]       Length character of data
    next-key-seg   Next key segments
  */
333 334 335 336 337 338 339

  matched=0;  /* how many char's from prefix were alredy matched */
  len=0;      /* length of previous key unpacked */

  while (page < end)
  {
    uint packed= *page & 128;
340

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
    vseg=page;
    if (keyinfo->seg->length >= 127)
    {
      suffix_len=mi_uint2korr(vseg) & 32767;
      vseg+=2;
    }
    else
      suffix_len= *vseg++ & 127;

    if (packed)
    {
      if (suffix_len == 0)                      /* Same key */
        prefix_len=len;
      else
      {
356
        prefix_len=suffix_len;
357 358 359 360 361 362 363 364 365 366
        get_key_length(suffix_len,vseg);
      }
    }
    else
      prefix_len=0;

    len=prefix_len+suffix_len;
    seg_len_pack=get_pack_length(len);
    t_buff=tt_buff+3-seg_len_pack;
    store_key_length(t_buff,len);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
367

368 369 370 371 372 373
    if (prefix_len > saved_prefix_len)
      memcpy(t_buff+seg_len_pack+saved_prefix_len,saved_vseg,
             prefix_len-saved_prefix_len);
    saved_vseg=vseg;
    saved_prefix_len=prefix_len;

374 375
    DBUG_PRINT("loop",("page: '%.*s%.*s'",prefix_len,t_buff+seg_len_pack,
		       suffix_len,vseg));
376 377
    {
      uchar *from=vseg+suffix_len;
378
      HA_KEYSEG *keyseg;
379 380 381 382
      uint l;

      for (keyseg=keyinfo->seg+1 ; keyseg->type ; keyseg++ )
      {
383

384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
        if (keyseg->flag & HA_NULL_PART)
        {
          if (!(*from++))
            continue;
        }
        if (keyseg->flag & (HA_VAR_LENGTH | HA_BLOB_PART | HA_SPACE_PACK))
        {
          get_key_length(l,from);
        }
        else
          l=keyseg->length;

        from+=l;
      }
      from+=keyseg->length;
      page=from+nod_flag;
      length=from-vseg;
    }

    if (page > end)
    {
      my_errno=HA_ERR_CRASHED;
406 407
      DBUG_PRINT("error",("Found wrong key:  length: %u  page: %p  end: %p",
                          length, page, end));
408 409 410 411 412 413 414 415 416 417 418 419 420
      DBUG_RETURN(MI_FOUND_WRONG_KEY);
    }

    if (matched >= prefix_len)
    {
      /* We have to compare. But we can still skip part of the key */
      uint  left;
      uchar *k=kseg+prefix_len;

      left=(len>cmplen) ? cmplen-prefix_len : suffix_len;

      matched=prefix_len+left;

421 422 423 424 425 426 427 428 429 430 431 432
      if (sort_order)
      {
        for (my_flag=0;left;left--)
          if ((my_flag= (int) sort_order[*vseg++] - (int) sort_order[*k++]))
            break;
      }
      else
      {
        for (my_flag=0;left;left--)
          if ((my_flag= (int) *vseg++ - (int) *k++))
            break;
      }
433 434 435

      if (my_flag>0)      /* mismatch */
        break;
436 437 438
      if (my_flag==0) /* match */
      {
	/*
439 440 441 442 443 444 445 446 447 448
        **  len cmplen seg_left_len more_segs
        **     <                               matched=len; continue search
        **     >      =                        prefix ? found : (matched=len; continue search)
        **     >      <                 -      ok, found
        **     =      <                 -      ok, found
        **     =      =                 -      ok, found
        **     =      =                 +      next seg
        */
        if (len < cmplen)
        {
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
	  if ((keyinfo->seg->type != HA_KEYTYPE_TEXT &&
	       keyinfo->seg->type != HA_KEYTYPE_VARTEXT))
	    my_flag= -1;
	  else
	  {
	    /* We have to compare k and vseg as if they where space extended */
	    uchar *end= k+ (cmplen - len);
	    for ( ; k < end && *k == ' '; k++) ;
	    if (k == end)
	      goto cmp_rest;		/* should never happen */
	    if (*k < (uchar) ' ')
	    {
	      my_flag= 1;		/* Compared string is smaller */
	      break;
	    }
	    my_flag= -1;		/* Continue searching */
	  }
466 467 468
        }
        else if (len > cmplen)
        {
469 470 471 472 473 474 475
	  uchar *end;
	  if ((nextflag & SEARCH_PREFIX) && key_len_left == 0)
	    goto fix_flag;

	  /* We have to compare k and vseg as if they where space extended */
	  for (end=vseg + (len-cmplen) ;
	       vseg < end && *vseg == (uchar) ' ';
476 477
	       vseg++, matched++) ;
	  DBUG_ASSERT(vseg < end);
478 479 480 481 482 483 484

	  if (*vseg > (uchar) ' ')
	  {
	    my_flag= 1;			/* Compared string is smaller */
	    break;
	  }
	  my_flag= -1;			/* Continue searching */
485 486
        }
        else
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
	{
      cmp_rest:
	  if (key_len_left>0)
	  {
	    uint not_used;
	    if ((flag = ha_key_cmp(keyinfo->seg+1,vseg,
				   k,key_len_left,nextflag,&not_used)) >= 0)
	      break;
	  }
	  else
	  {
	    /*
	      at this line flag==-1 if the following lines were already
	      visited and 0 otherwise,  i.e. flag <=0 here always !!!
	    */
	fix_flag:
	    DBUG_ASSERT(flag <= 0);
	    if (nextflag & (SEARCH_NO_FIND | SEARCH_LAST))
	      flag=(nextflag & (SEARCH_BIGGER | SEARCH_LAST)) ? -1 : 1;
	    if (flag>=0)
	      break;
	  }
	}
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
      }
      matched-=left;
    }
    /* else (matched < prefix_len) ---> do nothing. */

    memcpy(buff,t_buff,saved_length=seg_len_pack+prefix_len);
    saved_to=buff+saved_length;
    saved_from=saved_vseg;
    saved_length=length;
    *ret_pos=page;
  }
  if (my_flag)
    flag=(keyinfo->seg->flag & HA_REVERSE_SORT) ? -my_flag : my_flag;
  if (flag == 0)
  {
    memcpy(buff,t_buff,saved_length=seg_len_pack+prefix_len);
    saved_to=buff+saved_length;
    saved_from=saved_vseg;
    saved_length=length;
  }
  if (saved_length)
    memcpy(saved_to,saved_from,saved_length);

  *last_key= page == end;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
534

535
  DBUG_PRINT("exit",("flag: %d  ret_pos: %p", flag, *ret_pos));
536 537 538 539 540
  DBUG_RETURN(flag);
} /* _mi_prefix_search */


        /* Get pos to a key_block */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
541 542 543 544 545 546 547

my_off_t _mi_kpos(uint nod_flag, uchar *after_key)
{
  after_key-=nod_flag;
  switch (nod_flag) {
#if SIZEOF_OFF_T > 4
  case 7:
548
    return mi_uint7korr(after_key)*MI_MIN_KEY_BLOCK_LENGTH;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
549
  case 6:
550
    return mi_uint6korr(after_key)*MI_MIN_KEY_BLOCK_LENGTH;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
551
  case 5:
552
    return mi_uint5korr(after_key)*MI_MIN_KEY_BLOCK_LENGTH;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
553 554 555 556 557 558 559 560 561
#else
  case 7:
    after_key++;
  case 6:
    after_key++;
  case 5:
    after_key++;
#endif
  case 4:
562
    return ((my_off_t) mi_uint4korr(after_key))*MI_MIN_KEY_BLOCK_LENGTH;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
563
  case 3:
564
    return ((my_off_t) mi_uint3korr(after_key))*MI_MIN_KEY_BLOCK_LENGTH;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
565
  case 2:
566
    return (my_off_t) (mi_uint2korr(after_key)*MI_MIN_KEY_BLOCK_LENGTH);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
567
  case 1:
568
    return (uint) (*after_key)*MI_MIN_KEY_BLOCK_LENGTH;
569 570
  case 0:                                       /* At leaf page */
  default:                                      /* Impossible */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
571 572 573 574 575
    return(HA_OFFSET_ERROR);
  }
} /* _kpos */


576
        /* Save pos to a key_block */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
577 578 579

void _mi_kpointer(register MI_INFO *info, register uchar *buff, my_off_t pos)
{
580
  pos/=MI_MIN_KEY_BLOCK_LENGTH;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
  switch (info->s->base.key_reflength) {
#if SIZEOF_OFF_T > 4
  case 7: mi_int7store(buff,pos); break;
  case 6: mi_int6store(buff,pos); break;
  case 5: mi_int5store(buff,pos); break;
#else
  case 7: *buff++=0;
    /* fall trough */
  case 6: *buff++=0;
    /* fall trough */
  case 5: *buff++=0;
    /* fall trough */
#endif
  case 4: mi_int4store(buff,pos); break;
  case 3: mi_int3store(buff,pos); break;
  case 2: mi_int2store(buff,(uint) pos); break;
  case 1: buff[0]= (uchar) pos; break;
598
  default: abort();                             /* impossible */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
599 600 601 602
  }
} /* _mi_kpointer */


603
        /* Calc pos to a data-record from a key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
604 605 606 607 608 609 610 611


my_off_t _mi_dpos(MI_INFO *info, uint nod_flag, uchar *after_key)
{
  my_off_t pos;
  after_key-=(nod_flag + info->s->rec_reflength);
  switch (info->s->rec_reflength) {
#if SIZEOF_OFF_T > 4
612
  case 8:  pos= (my_off_t) mi_uint8korr(after_key);  break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
613 614 615 616 617
  case 7:  pos= (my_off_t) mi_uint7korr(after_key);  break;
  case 6:  pos= (my_off_t) mi_uint6korr(after_key);  break;
  case 5:  pos= (my_off_t) mi_uint5korr(after_key);  break;
#else
  case 8:  pos= (my_off_t) mi_uint4korr(after_key+4);   break;
618 619 620
  case 7:  pos= (my_off_t) mi_uint4korr(after_key+3);   break;
  case 6:  pos= (my_off_t) mi_uint4korr(after_key+2);   break;
  case 5:  pos= (my_off_t) mi_uint4korr(after_key+1);   break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
621 622 623 624 625
#endif
  case 4:  pos= (my_off_t) mi_uint4korr(after_key);  break;
  case 3:  pos= (my_off_t) mi_uint3korr(after_key);  break;
  case 2:  pos= (my_off_t) mi_uint2korr(after_key);  break;
  default:
626
    pos=0L;                                     /* Shut compiler up */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
627 628
  }
  return (info->s->options &
629 630
          (HA_OPTION_PACK_RECORD | HA_OPTION_COMPRESS_RECORD)) ? pos :
            pos*info->s->base.pack_reclength;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
631 632 633 634 635 636 637 638 639 640 641 642 643
}


/* Calc position from a record pointer ( in delete link chain ) */

my_off_t _mi_rec_pos(MYISAM_SHARE *s, uchar *ptr)
{
  my_off_t pos;
  switch (s->rec_reflength) {
#if SIZEOF_OFF_T > 4
  case 8:
    pos= (my_off_t) mi_uint8korr(ptr);
    if (pos == HA_OFFSET_ERROR)
644
      return HA_OFFSET_ERROR;                   /* end of list */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
645 646 647 648
    break;
  case 7:
    pos= (my_off_t) mi_uint7korr(ptr);
    if (pos == (((my_off_t) 1) << 56) -1)
649
      return HA_OFFSET_ERROR;                   /* end of list */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
650 651 652 653
    break;
  case 6:
    pos= (my_off_t) mi_uint6korr(ptr);
    if (pos == (((my_off_t) 1) << 48) -1)
654
      return HA_OFFSET_ERROR;                   /* end of list */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
655 656 657 658
    break;
  case 5:
    pos= (my_off_t) mi_uint5korr(ptr);
    if (pos == (((my_off_t) 1) << 40) -1)
659
      return HA_OFFSET_ERROR;                   /* end of list */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
    break;
#else
  case 8:
  case 7:
  case 6:
  case 5:
    ptr+= (s->rec_reflength-4);
    /* fall through */
#endif
  case 4:
    pos= (my_off_t) mi_uint4korr(ptr);
    if (pos == (my_off_t) (uint32) ~0L)
      return  HA_OFFSET_ERROR;
    break;
  case 3:
    pos= (my_off_t) mi_uint3korr(ptr);
    if (pos == (my_off_t) (1 << 24) -1)
      return HA_OFFSET_ERROR;
    break;
  case 2:
    pos= (my_off_t) mi_uint2korr(ptr);
    if (pos == (my_off_t) (1 << 16) -1)
      return HA_OFFSET_ERROR;
    break;
684
  default: abort();                             /* Impossible */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
685 686
  }
  return ((s->options &
687 688
          (HA_OPTION_PACK_RECORD | HA_OPTION_COMPRESS_RECORD)) ? pos :
          pos*s->base.pack_reclength);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
689 690 691
}


692
        /* save position to record */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
693 694 695 696

void _mi_dpointer(MI_INFO *info, uchar *buff, my_off_t pos)
{
  if (!(info->s->options &
697
        (HA_OPTION_PACK_RECORD | HA_OPTION_COMPRESS_RECORD)) &&
bk@work.mysql.com's avatar
bk@work.mysql.com committed
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
      pos != HA_OFFSET_ERROR)
    pos/=info->s->base.pack_reclength;

  switch (info->s->rec_reflength) {
#if SIZEOF_OFF_T > 4
  case 8: mi_int8store(buff,pos); break;
  case 7: mi_int7store(buff,pos); break;
  case 6: mi_int6store(buff,pos); break;
  case 5: mi_int5store(buff,pos); break;
#else
  case 8: *buff++=0;
    /* fall trough */
  case 7: *buff++=0;
    /* fall trough */
  case 6: *buff++=0;
    /* fall trough */
  case 5: *buff++=0;
    /* fall trough */
#endif
  case 4: mi_int4store(buff,pos); break;
  case 3: mi_int3store(buff,pos); break;
  case 2: mi_int2store(buff,(uint) pos); break;
720
  default: abort();                             /* Impossible */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
721 722 723 724
  }
} /* _mi_dpointer */


725 726 727 728 729
        /* Get key from key-block */
        /* page points at previous key; its advanced to point at next key */
        /* key should contain previous key */
        /* Returns length of found key + pointers */
        /* nod_flag is a flag if we are on nod */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
730

731
        /* same as _mi_get_key but used with fixed length keys */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
732 733

uint _mi_get_static_key(register MI_KEYDEF *keyinfo, uint nod_flag,
734
                       register uchar **page, register uchar *key)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
735 736
{
  memcpy((byte*) key,(byte*) *page,
737
         (size_t) (keyinfo->keylength+nod_flag));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
738 739 740 741 742
  *page+=keyinfo->keylength+nod_flag;
  return(keyinfo->keylength);
} /* _mi_get_static_key */


743 744 745 746 747 748 749 750 751 752 753 754 755
/*
  get key witch is packed against previous key or key with a NULL column.

  SYNOPSIS
    _mi_get_pack_key()
    keyinfo                     key definition information.
    nod_flag                    If nod: Length of node pointer, else zero.
    page_pos            RETURN  position in key page behind this key.
    key                 IN/OUT  in: prev key, out: unpacked key.

  RETURN
    key_length + length of data pointer
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
756 757

uint _mi_get_pack_key(register MI_KEYDEF *keyinfo, uint nod_flag,
758
                      register uchar **page_pos, register uchar *key)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
759
{
760
  reg1 HA_KEYSEG *keyseg;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
761 762 763 764 765 766 767 768 769 770 771 772 773
  uchar *start_key,*page=*page_pos;
  uint length;

  start_key=key;
  for (keyseg=keyinfo->seg ; keyseg->type ;keyseg++)
  {
    if (keyseg->flag & HA_PACK_KEY)
    {
      /* key with length, packed to previous key */
      uchar *start=key;
      uint packed= *page & 128,tot_length,rest_length;
      if (keyseg->length >= 127)
      {
774 775
        length=mi_uint2korr(page) & 32767;
        page+=2;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
776 777
      }
      else
778
        length= *page++ & 127;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794

      if (packed)
      {
	if (length > (uint) keyseg->length)
	{
	  my_errno=HA_ERR_CRASHED;
	  return 0;				/* Error */
	}
	if (length == 0)			/* Same key */
	{
	  if (keyseg->flag & HA_NULL_PART)
	    *key++=1;				/* Can't be NULL */
	  get_key_length(length,key);
	  key+= length;				/* Same diff_key as prev */
	  if (length > keyseg->length)
	  {
795 796 797
	    DBUG_PRINT("error",
                       ("Found too long null packed key: %u of %u at %p",
                        length, keyseg->length, *page_pos));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
798 799 800 801 802 803 804
	    DBUG_DUMP("key",(char*) *page_pos,16);
	    my_errno=HA_ERR_CRASHED;
	    return 0;
	  }
	  continue;
	}
	if (keyseg->flag & HA_NULL_PART)
805
	{
806
	  key++;				/* Skip null marker*/
807 808
	  start++;
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833

	get_key_length(rest_length,page);
	tot_length=rest_length+length;

	/* If the stored length has changed, we must move the key */
	if (tot_length >= 255 && *start != 255)
	{
	  /* length prefix changed from a length of one to a length of 3 */
	  bmove_upp((char*) key+length+3,(char*) key+length+1,length);
	  *key=255;
	  mi_int2store(key+1,tot_length);
	  key+=3+length;
	}
	else if (tot_length < 255 && *start == 255)
	{
	  bmove(key+1,key+3,length);
	  *key=tot_length;
	  key+=1+length;
	}
	else
	{
	  store_key_length_inc(key,tot_length);
	  key+=length;
	}
	memcpy(key,page,rest_length);
834 835 836
	page+=rest_length;
	key+=rest_length;
	continue;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
837 838 839
      }
      else
      {
840 841 842 843 844 845 846 847 848
        if (keyseg->flag & HA_NULL_PART)
        {
          if (!length--)                        /* Null part */
          {
            *key++=0;
            continue;
          }
          *key++=1;                             /* Not null */
        }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
849 850 851
      }
      if (length > (uint) keyseg->length)
      {
852
        DBUG_PRINT("error",("Found too long packed key: %u of %u at %p",
853 854 855 856
                            length, keyseg->length, *page_pos));
        DBUG_DUMP("key",(char*) *page_pos,16);
        my_errno=HA_ERR_CRASHED;
        return 0;                               /* Error */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
857 858 859 860 861 862 863
      }
      store_key_length_inc(key,length);
    }
    else
    {
      if (keyseg->flag & HA_NULL_PART)
      {
864 865
        if (!(*key++ = *page++))
          continue;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
866 867
      }
      if (keyseg->flag &
868
          (HA_VAR_LENGTH | HA_BLOB_PART | HA_SPACE_PACK))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
869
      {
870 871 872
        uchar *tmp=page;
        get_key_length(length,tmp);
        length+=(uint) (tmp-page);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
873 874
      }
      else
875
        length=keyseg->length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
    }
    memcpy((byte*) key,(byte*) page,(size_t) length);
    key+=length;
    page+=length;
  }
  length=keyseg->length+nod_flag;
  bmove((byte*) key,(byte*) page,length);
  *page_pos= page+length;
  return ((uint) (key-start_key)+keyseg->length);
} /* _mi_get_pack_key */



/* key that is packed relatively to previous */

uint _mi_get_binary_pack_key(register MI_KEYDEF *keyinfo, uint nod_flag,
892
                             register uchar **page_pos, register uchar *key)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
893
{
894
  reg1 HA_KEYSEG *keyseg;
895
  uchar *start_key,*page,*page_end,*from,*from_end;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
896 897
  uint length,tmp;

898
  page= *page_pos;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
899 900 901 902 903 904 905 906
  page_end=page+MI_MAX_KEY_BUFF+1;
  start_key=key;

  get_key_length(length,page);
  if (length)
  {
    if (length > keyinfo->maxlength)
    {
907
      DBUG_PRINT("error",("Found too long binary packed key: %u of %u at %p",
908
                          length, keyinfo->maxlength, *page_pos));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
909 910
      DBUG_DUMP("key",(char*) *page_pos,16);
      my_errno=HA_ERR_CRASHED;
911
      return 0;                                 /* Wrong key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
912 913 914 915 916
    }
    from=key;  from_end=key+length;
  }
  else
  {
917
    from=page; from_end=page_end;               /* Not packed key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
918 919 920 921 922 923 924 925 926 927 928 929 930
  }

  /*
    The trouble is that key is split in two parts:
     The first part is in from ...from_end-1.
     The second part starts at page
  */
  for (keyseg=keyinfo->seg ; keyseg->type ;keyseg++)
  {
    if (keyseg->flag & HA_NULL_PART)
    {
      if (from == from_end) { from=page;  from_end=page_end; }
      if (!(*key++ = *from++))
931
        continue;                               /* Null part */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
932 933 934 935 936 937 938
    }
    if (keyseg->flag & (HA_VAR_LENGTH | HA_BLOB_PART | HA_SPACE_PACK))
    {
      /* Get length of dynamic length key part */
      if (from == from_end) { from=page;  from_end=page_end; }
      if ((length= (*key++ = *from++)) == 255)
      {
939 940 941 942
        if (from == from_end) { from=page;  from_end=page_end; }
        length= (uint) ((*key++ = *from++)) << 8;
        if (from == from_end) { from=page;  from_end=page_end; }
        length+= (uint) ((*key++ = *from++));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
943 944 945 946 947 948 949
      }
    }
    else
      length=keyseg->length;

    if ((tmp=(uint) (from_end-from)) <= length)
    {
950
      key+=tmp;                                 /* Use old key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
951 952 953
      length-=tmp;
      from=page; from_end=page_end;
    }
954
    DBUG_PRINT("info",("key: %p  from: %p  length: %u",
955 956
		       key, from, length));
    memcpy_overlap((byte*) key, (byte*) from, (size_t) length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
957 958 959 960 961 962
    key+=length;
    from+=length;
  }
  length=keyseg->length+nod_flag;
  if ((tmp=(uint) (from_end-from)) <= length)
  {
963
    memcpy(key+tmp,page,length-tmp);            /* Get last part of key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
964 965 966 967 968 969 970 971
    *page_pos= page+length-tmp;
  }
  else
  {
    if (from_end != page_end)
    {
      DBUG_PRINT("error",("Error when unpacking key"));
      my_errno=HA_ERR_CRASHED;
972
      return 0;                                 /* Error */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
973 974 975 976 977 978 979 980
    }
    memcpy((byte*) key,(byte*) from,(size_t) length);
    *page_pos= from+length;
  }
  return((uint) (key-start_key)+keyseg->length);
}


981 982
        /* Get key at position without knowledge of previous key */
        /* Returns pointer to next key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
983 984

uchar *_mi_get_key(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *page,
985
                   uchar *key, uchar *keypos, uint *return_key_length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
986 987 988 989 990 991 992 993 994 995 996 997 998
{
  uint nod_flag;
  DBUG_ENTER("_mi_get_key");

  nod_flag=mi_test_if_nod(page);
  if (! (keyinfo->flag & (HA_VAR_LENGTH_KEY | HA_BINARY_PACK_KEY)))
  {
    bmove((byte*) key,(byte*) keypos,keyinfo->keylength+nod_flag);
    DBUG_RETURN(keypos+keyinfo->keylength+nod_flag);
  }
  else
  {
    page+=2+nod_flag;
999
    key[0]=0;                                   /* safety */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1000 1001 1002 1003 1004
    while (page <= keypos)
    {
      *return_key_length=(*keyinfo->get_key)(keyinfo,nod_flag,&page,key);
      if (*return_key_length == 0)
      {
1005 1006
        my_errno=HA_ERR_CRASHED;
        DBUG_RETURN(0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1007 1008 1009
      }
    }
  }
1010
  DBUG_PRINT("exit",("page: %p  length: %u", page, *return_key_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1011 1012 1013 1014
  DBUG_RETURN(page);
} /* _mi_get_key */


1015 1016
        /* Get key at position without knowledge of previous key */
        /* Returns 0 if ok */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1017 1018

static my_bool _mi_get_prev_key(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *page,
1019 1020
                                uchar *key, uchar *keypos,
                                uint *return_key_length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1021 1022 1023 1024 1025 1026 1027 1028 1029
{
  uint nod_flag;
  DBUG_ENTER("_mi_get_prev_key");

  nod_flag=mi_test_if_nod(page);
  if (! (keyinfo->flag & (HA_VAR_LENGTH_KEY | HA_BINARY_PACK_KEY)))
  {
    *return_key_length=keyinfo->keylength;
    bmove((byte*) key,(byte*) keypos- *return_key_length-nod_flag,
1030
          *return_key_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1031 1032 1033 1034 1035
    DBUG_RETURN(0);
  }
  else
  {
    page+=2+nod_flag;
1036
    key[0]=0;                                   /* safety */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1037 1038 1039 1040 1041
    while (page < keypos)
    {
      *return_key_length=(*keyinfo->get_key)(keyinfo,nod_flag,&page,key);
      if (*return_key_length == 0)
      {
1042 1043
        my_errno=HA_ERR_CRASHED;
        DBUG_RETURN(1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1044 1045 1046 1047 1048 1049 1050 1051
      }
    }
  }
  DBUG_RETURN(0);
} /* _mi_get_key */



1052 1053
        /* Get last key from key-page */
        /* Return pointer to where key starts */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1054 1055

uchar *_mi_get_last_key(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *page,
1056
                        uchar *lastkey, uchar *endpos, uint *return_key_length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1057 1058 1059 1060
{
  uint nod_flag;
  uchar *lastpos;
  DBUG_ENTER("_mi_get_last_key");
1061
  DBUG_PRINT("enter",("page: %p  endpos:  %p", page, endpos));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080

  nod_flag=mi_test_if_nod(page);
  if (! (keyinfo->flag & (HA_VAR_LENGTH_KEY | HA_BINARY_PACK_KEY)))
  {
    lastpos=endpos-keyinfo->keylength-nod_flag;
    *return_key_length=keyinfo->keylength;
    if (lastpos > page)
      bmove((byte*) lastkey,(byte*) lastpos,keyinfo->keylength+nod_flag);
  }
  else
  {
    lastpos=(page+=2+nod_flag);
    lastkey[0]=0;
    while (page < endpos)
    {
      lastpos=page;
      *return_key_length=(*keyinfo->get_key)(keyinfo,nod_flag,&page,lastkey);
      if (*return_key_length == 0)
      {
1081
        DBUG_PRINT("error",("Couldn't find last key:  page: %p", page));
1082 1083
        my_errno=HA_ERR_CRASHED;
        DBUG_RETURN(0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1084 1085 1086
      }
    }
  }
1087
  DBUG_PRINT("exit",("lastpos: %p  length: %u", lastpos, *return_key_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1088 1089 1090 1091
  DBUG_RETURN(lastpos);
} /* _mi_get_last_key */


1092
        /* Calculate length of key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1093 1094 1095

uint _mi_keylength(MI_KEYDEF *keyinfo, register uchar *key)
{
1096
  reg1 HA_KEYSEG *keyseg;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
  uchar *start;

  if (! (keyinfo->flag & (HA_VAR_LENGTH_KEY | HA_BINARY_PACK_KEY)))
    return (keyinfo->keylength);

  start=key;
  for (keyseg=keyinfo->seg ; keyseg->type ; keyseg++)
  {
    if (keyseg->flag & HA_NULL_PART)
      if (!*key++)
1107
        continue;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
    if (keyseg->flag & (HA_SPACE_PACK | HA_BLOB_PART | HA_VAR_LENGTH))
    {
      uint length;
      get_key_length(length,key);
      key+=length;
    }
    else
      key+= keyseg->length;
  }
  return((uint) (key-start)+keyseg->length);
} /* _mi_keylength */


1121 1122 1123 1124 1125 1126 1127 1128 1129
/*
  Calculate length of part key.

  Used in mi_rkey() to find the key found for the key-part that was used.
  This is needed in case of multi-byte character sets where we may search
  after '0xDF' but find 'ss'
*/

uint _mi_keylength_part(MI_KEYDEF *keyinfo, register uchar *key,
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1130
			HA_KEYSEG *end)
1131
{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1132
  reg1 HA_KEYSEG *keyseg;
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
  uchar *start= key;

  for (keyseg=keyinfo->seg ; keyseg != end ; keyseg++)
  {
    if (keyseg->flag & HA_NULL_PART)
      if (!*key++)
        continue;
    if (keyseg->flag & (HA_SPACE_PACK | HA_BLOB_PART | HA_VAR_LENGTH))
    {
      uint length;
      get_key_length(length,key);
      key+=length;
    }
    else
      key+= keyseg->length;
  }
  return (uint) (key-start);
}

1152
        /* Move a key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1153 1154 1155 1156 1157

uchar *_mi_move_key(MI_KEYDEF *keyinfo, uchar *to, uchar *from)
{
  reg1 uint length;
  memcpy((byte*) to, (byte*) from,
1158
         (size_t) (length=_mi_keylength(keyinfo,from)));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1159 1160 1161
  return to+length;
}

1162 1163
        /* Find next/previous record with same key */
        /* This can't be used when database is touched after last read */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1164 1165

int _mi_search_next(register MI_INFO *info, register MI_KEYDEF *keyinfo,
1166
                    uchar *key, uint key_length, uint nextflag, my_off_t pos)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1167 1168 1169 1170 1171
{
  int error;
  uint nod_flag;
  uchar lastkey[MI_MAX_KEY_BUFF];
  DBUG_ENTER("_mi_search_next");
1172 1173 1174
  DBUG_PRINT("enter",("nextflag: %u  lastpos: %lu  int_keypos: %lu",
                      nextflag, (ulong) info->lastpos,
                      (ulong) info->int_keypos));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1175 1176 1177
  DBUG_EXECUTE("key",_mi_print_key(DBUG_FILE,keyinfo->seg,key,key_length););

  /* Force full read if we are at last key or if we are not on a leaf
1178 1179 1180 1181 1182
     and the key tree has changed since we used it last time
     Note that even if the key tree has changed since last read, we can use
     the last read data from the leaf if we haven't used the buffer for
     something else.
  */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1183 1184 1185 1186 1187

  if (((nextflag & SEARCH_BIGGER) && info->int_keypos >= info->int_maxpos) ||
      info->page_changed ||
      (info->int_keytree_version != keyinfo->version &&
       (info->int_nod_flag || info->buff_used)))
1188
    DBUG_RETURN(_mi_search(info,keyinfo,key, USE_WHOLE_KEY,
1189
                           nextflag | SEARCH_SAVE_BUFF, pos));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1190 1191 1192 1193

  if (info->buff_used)
  {
    if (!_mi_fetch_keypage(info,keyinfo,info->last_search_keypage,
1194
                           DFLT_INIT_HITS,info->buff,0))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1195 1196 1197 1198 1199 1200 1201
      DBUG_RETURN(-1);
    info->buff_used=0;
  }

  /* Last used buffer is in info->buff */
  nod_flag=mi_test_if_nod(info->buff);

1202
  if (nextflag & SEARCH_BIGGER)                                 /* Next key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1203 1204 1205 1206
  {
    my_off_t tmp_pos=_mi_kpos(nod_flag,info->int_keypos);
    if (tmp_pos != HA_OFFSET_ERROR)
    {
1207
      if ((error=_mi_search(info,keyinfo,key, USE_WHOLE_KEY,
1208 1209
                            nextflag | SEARCH_SAVE_BUFF, tmp_pos)) <=0)
        DBUG_RETURN(error);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1210
    }
1211
    memcpy(lastkey,key,key_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1212
    if (!(info->lastkey_length=(*keyinfo->get_key)(keyinfo,nod_flag,
1213
                                                   &info->int_keypos,lastkey)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1214 1215
      DBUG_RETURN(-1);
  }
1216
  else                                                  /* Previous key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1217 1218 1219 1220
  {
    uint length;
    /* Find start of previous key */
    info->int_keypos=_mi_get_last_key(info,keyinfo,info->buff,lastkey,
1221
                                      info->int_keypos, &length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1222 1223 1224
    if (!info->int_keypos)
      DBUG_RETURN(-1);
    if (info->int_keypos == info->buff+2)
1225
      DBUG_RETURN(_mi_search(info,keyinfo,key, USE_WHOLE_KEY,
1226
                             nextflag | SEARCH_SAVE_BUFF, pos));
1227 1228
    if ((error=_mi_search(info,keyinfo,key, USE_WHOLE_KEY,
			  nextflag | SEARCH_SAVE_BUFF,
1229
                          _mi_kpos(nod_flag,info->int_keypos))) <= 0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1230 1231
      DBUG_RETURN(error);

1232
    /* QQ: We should be able to optimize away the following call */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1233
    if (! _mi_get_last_key(info,keyinfo,info->buff,lastkey,
1234
                           info->int_keypos,&info->lastkey_length))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1235 1236 1237 1238
      DBUG_RETURN(-1);
  }
  memcpy(info->lastkey,lastkey,info->lastkey_length);
  info->lastpos=_mi_dpos(info,0,info->lastkey+info->lastkey_length);
1239
  DBUG_PRINT("exit",("found key at %lu",(ulong) info->lastpos));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1240 1241 1242 1243
  DBUG_RETURN(0);
} /* _mi_search_next */


1244 1245
        /* Search after position for the first row in an index */
        /* This is stored in info->lastpos */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1246 1247

int _mi_search_first(register MI_INFO *info, register MI_KEYDEF *keyinfo,
1248
                     register my_off_t pos)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
{
  uint nod_flag;
  uchar *page;
  DBUG_ENTER("_mi_search_first");

  if (pos == HA_OFFSET_ERROR)
  {
    my_errno=HA_ERR_KEY_NOT_FOUND;
    info->lastpos= HA_OFFSET_ERROR;
    DBUG_RETURN(-1);
  }

  do
  {
1263
    if (!_mi_fetch_keypage(info,keyinfo,pos,DFLT_INIT_HITS,info->buff,0))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1264 1265 1266 1267 1268 1269 1270 1271 1272
    {
      info->lastpos= HA_OFFSET_ERROR;
      DBUG_RETURN(-1);
    }
    nod_flag=mi_test_if_nod(info->buff);
    page=info->buff+2+nod_flag;
  } while ((pos=_mi_kpos(nod_flag,page)) != HA_OFFSET_ERROR);

  info->lastkey_length=(*keyinfo->get_key)(keyinfo,nod_flag,&page,
1273
                                           info->lastkey);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1274 1275 1276 1277 1278 1279 1280
  info->int_keypos=page; info->int_maxpos=info->buff+mi_getint(info->buff)-1;
  info->int_nod_flag=nod_flag;
  info->int_keytree_version=keyinfo->version;
  info->last_search_keypage=info->last_keypage;
  info->page_changed=info->buff_used=0;
  info->lastpos=_mi_dpos(info,0,info->lastkey+info->lastkey_length);

1281
  DBUG_PRINT("exit",("found key at %lu", (ulong) info->lastpos));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1282 1283 1284 1285
  DBUG_RETURN(0);
} /* _mi_search_first */


1286 1287
        /* Search after position for the last row in an index */
        /* This is stored in info->lastpos */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1288 1289

int _mi_search_last(register MI_INFO *info, register MI_KEYDEF *keyinfo,
1290
                    register my_off_t pos)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1291 1292 1293 1294 1295 1296 1297
{
  uint nod_flag;
  uchar *buff,*page;
  DBUG_ENTER("_mi_search_last");

  if (pos == HA_OFFSET_ERROR)
  {
1298
    my_errno=HA_ERR_KEY_NOT_FOUND;                      /* Didn't find key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1299 1300 1301 1302 1303 1304 1305
    info->lastpos= HA_OFFSET_ERROR;
    DBUG_RETURN(-1);
  }

  buff=info->buff;
  do
  {
1306
    if (!_mi_fetch_keypage(info,keyinfo,pos,DFLT_INIT_HITS,buff,0))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1307 1308 1309 1310 1311 1312 1313 1314 1315
    {
      info->lastpos= HA_OFFSET_ERROR;
      DBUG_RETURN(-1);
    }
    page= buff+mi_getint(buff);
    nod_flag=mi_test_if_nod(buff);
  } while ((pos=_mi_kpos(nod_flag,page)) != HA_OFFSET_ERROR);

  if (!_mi_get_last_key(info,keyinfo,buff,info->lastkey,page,
1316
                        &info->lastkey_length))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1317 1318 1319 1320 1321 1322 1323 1324
    DBUG_RETURN(-1);
  info->lastpos=_mi_dpos(info,0,info->lastkey+info->lastkey_length);
  info->int_keypos=info->int_maxpos=page;
  info->int_nod_flag=nod_flag;
  info->int_keytree_version=keyinfo->version;
  info->last_search_keypage=info->last_keypage;
  info->page_changed=info->buff_used=0;

1325
  DBUG_PRINT("exit",("found key at %lu",(ulong) info->lastpos));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
  DBUG_RETURN(0);
} /* _mi_search_last */



/****************************************************************************
**
** Functions to store and pack a key in a page
**
** mi_calc_xx_key_length takes the following arguments:
1336 1337 1338 1339 1340 1341
**  nod_flag    If nod: Length of nod-pointer
**  next_key    Position to pos after the new key in buffer
**  org_key     Key that was before the next key in buffer
**  prev_key    Last key before current key
**  key         Key that will be stored
**  s_temp      Information how next key will be packed
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1342 1343 1344 1345 1346 1347
****************************************************************************/

/* Static length key */

int
_mi_calc_static_key_length(MI_KEYDEF *keyinfo,uint nod_flag,
1348 1349 1350 1351
                           uchar *next_pos  __attribute__((unused)),
                           uchar *org_key  __attribute__((unused)),
                           uchar *prev_key __attribute__((unused)),
                           uchar *key, MI_KEY_PARAM *s_temp)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1352 1353 1354 1355 1356 1357 1358 1359 1360
{
  s_temp->key=key;
  return (int) (s_temp->totlength=keyinfo->keylength+nod_flag);
}

/* Variable length key */

int
_mi_calc_var_key_length(MI_KEYDEF *keyinfo,uint nod_flag,
1361 1362 1363 1364
                        uchar *next_pos  __attribute__((unused)),
                        uchar *org_key  __attribute__((unused)),
                        uchar *prev_key __attribute__((unused)),
                        uchar *key, MI_KEY_PARAM *s_temp)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
{
  s_temp->key=key;
  return (int) (s_temp->totlength=_mi_keylength(keyinfo,key)+nod_flag);
}

/*
  length of key with a variable length first segment which is prefix
  compressed (myisamchk reports 'packed + stripped')

  Keys are compressed the following way:

1376
  If the max length of first key segment <= 127 bytes the prefix is
1377
  1 byte else it's 2 byte
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1378

1379
  prefix byte(s) The high bit is set if this is a prefix for the prev key
1380
  length         Packed length if the previous was a prefix byte
1381
  [length]       data bytes ('length' bytes)
1382
  next-key-seg   Next key segments
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1383 1384 1385 1386 1387 1388 1389 1390

  If the first segment can have NULL:
  The length is 0 for NULLS and 1+length for not null columns.

*/

int
_mi_calc_var_pack_key_length(MI_KEYDEF *keyinfo,uint nod_flag,uchar *next_key,
1391 1392
                             uchar *org_key, uchar *prev_key, uchar *key,
                             MI_KEY_PARAM *s_temp)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1393
{
1394
  reg1 HA_KEYSEG *keyseg;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
  int length;
  uint key_length,ref_length,org_key_length=0,
       length_pack,new_key_length,diff_flag,pack_marker;
  uchar *start,*end,*key_end,*sort_order;
  bool same_length;

  length_pack=s_temp->ref_length=s_temp->n_ref_length=s_temp->n_length=0;
  same_length=0; keyseg=keyinfo->seg;
  key_length=_mi_keylength(keyinfo,key)+nod_flag;

  sort_order=0;
  if ((keyinfo->flag & HA_FULLTEXT) &&
      ((keyseg->type == HA_KEYTYPE_TEXT) ||
       (keyseg->type == HA_KEYTYPE_VARTEXT)) &&
1409
      !use_strnxfrm(keyseg->charset))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
    sort_order=keyseg->charset->sort_order;

  /* diff flag contains how many bytes is needed to pack key */
  if (keyseg->length >= 127)
  {
    diff_flag=2;
    pack_marker=32768;
  }
  else
  {
    diff_flag= 1;
    pack_marker=128;
  }
  s_temp->pack_marker=pack_marker;

  /* Handle the case that the first part have NULL values */
  if (keyseg->flag & HA_NULL_PART)
  {
    if (!*key++)
    {
      s_temp->key=key;
      s_temp->ref_length=s_temp->key_length=0;
      s_temp->totlength=key_length-1+diff_flag;
1433
      s_temp->next_key_pos=0;                   /* No next key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1434 1435 1436
      return (s_temp->totlength);
    }
    s_temp->store_not_null=1;
1437
    key_length--;                               /* We don't store NULL */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1438
    if (prev_key && !*prev_key++)
1439
      org_key=prev_key=0;                       /* Can't pack against prev */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1440
    else if (org_key)
1441
      org_key++;                                /* Skip NULL */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
  }
  else
    s_temp->store_not_null=0;
  s_temp->prev_key=org_key;

  /* The key part will start with a packed length */

  get_key_pack_length(new_key_length,length_pack,key);
  end=key_end= key+ new_key_length;
  start=key;

  /* Calc how many characters are identical between this and the prev. key */
  if (prev_key)
  {
    get_key_length(org_key_length,prev_key);
1457
    s_temp->prev_key=prev_key;          /* Pointer at data */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1458 1459 1460 1461
    /* Don't use key-pack if length == 0 */
    if (new_key_length && new_key_length == org_key_length)
      same_length=1;
    else if (new_key_length > org_key_length)
1462
      end=key + org_key_length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1463

1464
    if (sort_order)                             /* SerG */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
    {
      while (key < end && sort_order[*key] == sort_order[*prev_key])
      {
        key++; prev_key++;
      }
    }
    else
    {
      while (key < end && *key == *prev_key)
      {
1475
        key++; prev_key++;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
      }
    }
  }

  s_temp->key=key;
  s_temp->key_length= (uint) (key_end-key);

  if (same_length && key == key_end)
  {
    /* identical variable length key */
    s_temp->ref_length= pack_marker;
    length=(int) key_length-(int) (key_end-start)-length_pack;
    length+= diff_flag;
    if (next_key)
1490 1491
    {                                           /* Can't combine with next */
      s_temp->n_length= *next_key;              /* Needed by _mi_store_key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1492 1493 1494 1495 1496 1497
      next_key=0;
    }
  }
  else
  {
    if (start != key)
1498
    {                                           /* Starts as prev key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
      ref_length= (uint) (key-start);
      s_temp->ref_length= ref_length + pack_marker;
      length= (int) (key_length - ref_length);

      length-= length_pack;
      length+= diff_flag;
      length+= ((new_key_length-ref_length) >= 255) ? 3 : 1;/* Rest_of_key */
    }
    else
    {
1509
      s_temp->key_length+=s_temp->store_not_null;       /* If null */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1510 1511 1512 1513 1514
      length= key_length - length_pack+ diff_flag;
    }
  }
  s_temp->totlength=(uint) length;
  s_temp->prev_length=0;
1515 1516
  DBUG_PRINT("test",("tot_length: %u  length: %d  uniq_key_length: %u",
                     key_length, length, s_temp->key_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1517

1518
        /* If something after that hasn't length=0, test if we can combine */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
  if ((s_temp->next_key_pos=next_key))
  {
    uint packed,n_length;

    packed = *next_key & 128;
    if (diff_flag == 2)
    {
      n_length= mi_uint2korr(next_key) & 32767; /* Length of next key */
      next_key+=2;
    }
    else
      n_length= *next_key++ & 127;
    if (!packed)
      n_length-= s_temp->store_not_null;

1534
    if (n_length || packed)             /* Don't pack 0 length keys */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1535 1536 1537 1538 1539
    {
      uint next_length_pack, new_ref_length=s_temp->ref_length;

      if (packed)
      {
1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
        /* If first key and next key is packed (only on delete) */
        if (!prev_key && org_key)
        {
          get_key_length(org_key_length,org_key);
          key=start;
          if (sort_order)                       /* SerG */
          {
            while (key < end && sort_order[*key] == sort_order[*org_key])
            {
              key++; org_key++;
            }
          }
          else
          {
            while (key < end && *key == *org_key)
            {
              key++; org_key++;
            }
          }
          if ((new_ref_length= (uint) (key - start)))
            new_ref_length+=pack_marker;
        }

        if (!n_length)
        {
          /*
            We put a different key between two identical variable length keys
            Extend next key to have same prefix as this key
          */
          if (new_ref_length)                   /* prefix of previus key */
          {                                     /* make next key longer */
            s_temp->part_of_prev_key= new_ref_length;
            s_temp->prev_length=          org_key_length -
              (new_ref_length-pack_marker);
1574 1575
            s_temp->n_ref_length= s_temp->part_of_prev_key;
            s_temp->n_length= s_temp->prev_length;
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
            n_length=             get_pack_length(s_temp->prev_length);
            s_temp->prev_key+=    (new_ref_length - pack_marker);
            length+=              s_temp->prev_length + n_length;
          }
          else
          {                                     /* Can't use prev key */
            s_temp->part_of_prev_key=0;
            s_temp->prev_length= org_key_length;
            s_temp->n_ref_length=s_temp->n_length=  org_key_length;
            length+=           org_key_length;
            /* +get_pack_length(org_key_length); */
          }
          return (int) length;
        }

        ref_length=n_length;
        get_key_pack_length(n_length,next_length_pack,next_key);

        /* Test if new keys has fewer characters that match the previous key */
        if (!new_ref_length)
        {                                       /* Can't use prev key */
          s_temp->part_of_prev_key=     0;
          s_temp->prev_length=          ref_length;
          s_temp->n_ref_length= s_temp->n_length= n_length+ref_length;
          /* s_temp->prev_key+=         get_pack_length(org_key_length); */
          return (int) length+ref_length-next_length_pack;
        }
        if (ref_length+pack_marker > new_ref_length)
        {
          uint new_pack_length=new_ref_length-pack_marker;
          /* We must copy characters from the original key to the next key */
          s_temp->part_of_prev_key= new_ref_length;
          s_temp->prev_length=      ref_length - new_pack_length;
          s_temp->n_ref_length=s_temp->n_length=n_length + s_temp->prev_length;
          s_temp->prev_key+=        new_pack_length;
/*                                  +get_pack_length(org_key_length); */
          length= length-get_pack_length(ref_length)+
            get_pack_length(new_pack_length);
          return (int) length + s_temp->prev_length;
        }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1616 1617 1618
      }
      else
      {
1619 1620 1621
        /* Next key wasn't a prefix of previous key */
        ref_length=0;
        next_length_pack=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1622
     }
1623
      DBUG_PRINT("test",("length: %d  next_key: %p", length, next_key));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1624 1625

      {
1626 1627 1628 1629 1630 1631
        uint tmp_length;
        key=(start+=ref_length);
        if (key+n_length < key_end)             /* Normalize length based */
          key_end=key+n_length;
        if (sort_order)                         /* SerG */
        {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1632
          while (key < key_end && sort_order[*key] ==
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652
                 sort_order[*next_key])
          {
            key++; next_key++;
          }
        }
        else
        {
          while (key < key_end && *key == *next_key)
          {
            key++; next_key++;
          }
        }
        if (!(tmp_length=(uint) (key-start)))
        {                                       /* Key can't be re-packed */
          s_temp->next_key_pos=0;
          return length;
        }
        ref_length+=tmp_length;
        n_length-=tmp_length;
        length-=tmp_length+next_length_pack;    /* We gained these chars */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1653
      }
1654
      if (n_length == 0 && ref_length == new_key_length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1655
      {
1656
        s_temp->n_ref_length=pack_marker;       /* Same as prev key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1657 1658 1659
      }
      else
      {
1660 1661 1662
        s_temp->n_ref_length=ref_length | pack_marker;
        length+= get_pack_length(n_length);
        s_temp->n_length=n_length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
      }
    }
  }
  return length;
}


/* Length of key which is prefix compressed */

int
_mi_calc_bin_pack_key_length(MI_KEYDEF *keyinfo,uint nod_flag,uchar *next_key,
1674 1675
                             uchar *org_key, uchar *prev_key, uchar *key,
                             MI_KEY_PARAM *s_temp)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1676 1677 1678 1679
{
  uint length,key_length,ref_length;

  s_temp->totlength=key_length=_mi_keylength(keyinfo,key)+nod_flag;
1680 1681 1682
#ifdef HAVE_purify
  s_temp->n_length= s_temp->n_ref_length=0;	/* For valgrind */
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1683 1684
  s_temp->key=key;
  s_temp->prev_key=org_key;
1685
  if (prev_key)                                 /* If not first key in block */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
  {
    /* pack key against previous key */
    /*
      As keys may be identical when running a sort in myisamchk, we
      have to guard against the case where keys may be identical
    */
    uchar *end;
    end=key+key_length;
    for ( ; *key == *prev_key && key < end; key++,prev_key++) ;
    s_temp->ref_length= ref_length=(uint) (key-s_temp->key);
    length=key_length - ref_length + get_pack_length(ref_length);
  }
  else
  {
    /* No previous key */
    s_temp->ref_length=ref_length=0;
    length=key_length+1;
  }
1704
  if ((s_temp->next_key_pos=next_key))          /* If another key after */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
  {
    /* pack key against next key */
    uint next_length,next_length_pack;
    get_key_pack_length(next_length,next_length_pack,next_key);

    /* If first key and next key is packed (only on delete) */
    if (!prev_key && org_key && next_length)
    {
      uchar *end;
      for (key= s_temp->key, end=key+next_length ;
1715 1716
           *key == *org_key && key < end;
           key++,org_key++) ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1717 1718 1719 1720 1721 1722
      ref_length= (uint) (key - s_temp->key);
    }

    if (next_length > ref_length)
    {
      /* We put a key with different case between two keys with the same prefix
1723 1724
         Extend next key to have same prefix as
         this key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1725 1726 1727 1728
      s_temp->n_ref_length= ref_length;
      s_temp->prev_length=  next_length-ref_length;
      s_temp->prev_key+=    ref_length;
      return (int) (length+ s_temp->prev_length - next_length_pack +
1729
                    get_pack_length(ref_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1730 1731 1732 1733 1734 1735 1736
    }
    /* Check how many characters are identical to next key */
    key= s_temp->key+next_length;
    while (*key++ == *next_key++) ;
    if ((ref_length= (uint) (key - s_temp->key)-1) == next_length)
    {
      s_temp->next_key_pos=0;
1737
      return length;                            /* can't pack next key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1738 1739 1740 1741
    }
    s_temp->prev_length=0;
    s_temp->n_ref_length=ref_length;
    return (int) (length-(ref_length - next_length) - next_length_pack +
1742
                  get_pack_length(ref_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754
  }
  return (int) length;
}


/*
** store a key packed with _mi_calc_xxx_key_length in page-buffert
*/

/* store key without compression */

void _mi_store_static_key(MI_KEYDEF *keyinfo __attribute__((unused)),
1755 1756
                          register uchar *key_pos,
                          register MI_KEY_PARAM *s_temp)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769
{
  memcpy((byte*) key_pos,(byte*) s_temp->key,(size_t) s_temp->totlength);
}


/* store variable length key with prefix compression */

#define store_pack_length(test,pos,length) { \
  if (test) { *((pos)++) = (uchar) (length); } else \
  { *((pos)++) = (uchar) ((length) >> 8); *((pos)++) = (uchar) (length);  } }


void _mi_store_var_pack_key(MI_KEYDEF *keyinfo  __attribute__((unused)),
1770 1771
                            register uchar *key_pos,
                            register MI_KEY_PARAM *s_temp)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
{
  uint length;
  uchar *start;

  start=key_pos;

  if (s_temp->ref_length)
  {
    /* Packed against previous key */
    store_pack_length(s_temp->pack_marker == 128,key_pos,s_temp->ref_length);
    /* If not same key after */
    if (s_temp->ref_length != s_temp->pack_marker)
      store_key_length_inc(key_pos,s_temp->key_length);
  }
  else
  {
    /* Not packed against previous key */
    store_pack_length(s_temp->pack_marker == 128,key_pos,s_temp->key_length);
  }
  bmove((byte*) key_pos,(byte*) s_temp->key,
1792
        (length=s_temp->totlength-(uint) (key_pos-start)));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1793

1794
  if (!s_temp->next_key_pos)                    /* No following key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1795 1796 1797 1798 1799 1800 1801 1802 1803
    return;
  key_pos+=length;

  if (s_temp->prev_length)
  {
    /* Extend next key because new key didn't have same prefix as prev key */
    if (s_temp->part_of_prev_key)
    {
      store_pack_length(s_temp->pack_marker == 128,key_pos,
1804
                        s_temp->part_of_prev_key);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1805 1806 1807 1808 1809 1810
      store_key_length_inc(key_pos,s_temp->n_length);
    }
    else
    {
      s_temp->n_length+= s_temp->store_not_null;
      store_pack_length(s_temp->pack_marker == 128,key_pos,
1811
                        s_temp->n_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1812 1813 1814 1815 1816 1817 1818
    }
    memcpy(key_pos, s_temp->prev_key, s_temp->prev_length);
  }
  else if (s_temp->n_ref_length)
  {
    store_pack_length(s_temp->pack_marker == 128,key_pos,s_temp->n_ref_length);
    if (s_temp->n_ref_length == s_temp->pack_marker)
1819
      return;                                   /* Identical key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
    store_key_length(key_pos,s_temp->n_length);
  }
  else
  {
    s_temp->n_length+= s_temp->store_not_null;
    store_pack_length(s_temp->pack_marker == 128,key_pos,s_temp->n_length);
  }
}


/* variable length key with prefix compression */

void _mi_store_bin_pack_key(MI_KEYDEF *keyinfo  __attribute__((unused)),
1833 1834
                            register uchar *key_pos,
                            register MI_KEY_PARAM *s_temp)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1835 1836 1837
{
  store_key_length_inc(key_pos,s_temp->ref_length);
  memcpy((char*) key_pos,(char*) s_temp->key+s_temp->ref_length,
1838
          (size_t) s_temp->totlength-s_temp->ref_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1839 1840 1841 1842 1843

  if (s_temp->next_key_pos)
  {
    key_pos+=(uint) (s_temp->totlength-s_temp->ref_length);
    store_key_length_inc(key_pos,s_temp->n_ref_length);
1844
    if (s_temp->prev_length)                    /* If we must extend key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1845 1846 1847 1848 1849
    {
      memcpy(key_pos,s_temp->prev_key,s_temp->prev_length);
    }
  }
}