hp_hash.c 10.2 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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
   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 */

/* The hash functions used for saveing keys */

#include "heapdef.h"
#include <m_ctype.h>

	/* 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)
{
  reg1 HASH_INFO *pos,*prev_ptr;
  int flag;
  uint old_nextflag;
  HP_SHARE *share=info->s;
  DBUG_ENTER("_hp_search");

  old_nextflag=nextflag;
  flag=1;
  prev_ptr=0;

  if (share->records)
  {
    pos=hp_find_hash(&keyinfo->block,_hp_mask(_hp_hashnr(keyinfo,key),
					      share->blength,share->records));
    do
    {
      if (!_hp_key_cmp(keyinfo,pos->ptr_to_rec,key))
      {
	switch (nextflag) {
	case 0:					/* Search after key */
	  DBUG_PRINT("exit",("found key at %d",pos->ptr_to_rec));
	  info->current_hash_ptr=pos;
	  DBUG_RETURN(info->current_ptr= pos->ptr_to_rec);
	case 1:					/* Search next */
	  if (pos->ptr_to_rec == info->current_ptr)
	    nextflag=0;
	  break;
	case 2:					/* Search previous */
	  if (pos->ptr_to_rec == info->current_ptr)
	  {
	    my_errno=HA_ERR_KEY_NOT_FOUND;	/* If gpos == 0 */
	    info->current_hash_ptr=prev_ptr;
	    DBUG_RETURN(info->current_ptr=prev_ptr ? prev_ptr->ptr_to_rec : 0);
	  }
	  prev_ptr=pos;				/* Prev. record found */
	  break;
	case 3:					/* Search same */
	  if (pos->ptr_to_rec == info->current_ptr)
	  {
	    info->current_hash_ptr=pos;
	    DBUG_RETURN(info->current_ptr);
	  }
	}
      }
      if (flag)
      {
	flag=0;					/* Reset flag */
	if (hp_find_hash(&keyinfo->block,
			 _hp_mask(_hp_rec_hashnr(keyinfo,pos->ptr_to_rec),
				  share->blength,share->records)) != pos)
	  break;				/* Wrong link */
      }
    }
    while ((pos=pos->next_key));
  }
  my_errno=HA_ERR_KEY_NOT_FOUND;
  if (nextflag == 2 && ! info->current_ptr)
  {
    /* Do a previous from end */
    info->current_hash_ptr=prev_ptr;
    DBUG_RETURN(info->current_ptr=prev_ptr ? prev_ptr->ptr_to_rec : 0);
  }

  if (old_nextflag && nextflag)
    my_errno=HA_ERR_RECORD_CHANGED;		/* Didn't find old record */
  DBUG_PRINT("exit",("Error: %d",my_errno));
  info->current_hash_ptr=0;  
  DBUG_RETURN((info->current_ptr= 0));
}


/*
  Search next after last read;  Assumes that the table hasn't changed
  since last read !
*/

byte *_hp_search_next(HP_INFO *info, HP_KEYDEF *keyinfo, const byte *key,
		      HASH_INFO *pos)
{
  DBUG_ENTER("_hp_search_next");

  while ((pos= pos->next_key))
  {
    if (!_hp_key_cmp(keyinfo,pos->ptr_to_rec,key))
    {
      info->current_hash_ptr=pos;
      DBUG_RETURN (info->current_ptr= pos->ptr_to_rec);
    }
  }
  my_errno=HA_ERR_KEY_NOT_FOUND;
  DBUG_PRINT("exit",("Error: %d",my_errno));
  info->current_hash_ptr=0;
  DBUG_RETURN ((info->current_ptr= 0));
}


	/* Calculate pos according to keys */

ulong _hp_mask(ulong hashnr, ulong buffmax, ulong maxlength)
{
  if ((hashnr & (buffmax-1)) < maxlength) return (hashnr & (buffmax-1));
  return (hashnr & ((buffmax >> 1) -1));
}


	/* Change link from pos to new_link */

void _hp_movelink(HASH_INFO *pos, HASH_INFO *next_link, HASH_INFO *newlink)
{
  HASH_INFO *old_link;
  do
  {
    old_link=next_link;
  }
  while ((next_link=next_link->next_key) != pos);
  old_link->next_key=newlink;
  return;
}

148
#ifndef NEW_HASH_FUNCTION
bk@work.mysql.com's avatar
bk@work.mysql.com committed
149 150 151 152 153

	/* Calc hashvalue for a key */

ulong _hp_hashnr(register HP_KEYDEF *keydef, register const byte *key)
{
154 155
  /*register*/ 
  ulong nr=1, nr2=4;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
156 157 158 159
  HP_KEYSEG *seg,*endseg;

  for (seg=keydef->seg,endseg=seg+keydef->keysegs ; seg < endseg ; seg++)
  {
160 161
    uchar *pos=(uchar*) key;
    key+=seg->length;
162 163 164 165 166 167 168 169 170 171
    if (seg->null_bit)
    {
      key++;					/* Skipp null byte */
      if (*pos)					/* Found null */
      {
	nr^= (nr << 1) | 1;
	continue;
      }
      pos++;
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
172 173
    if (seg->type == HA_KEYTYPE_TEXT)
    {
174 175 176 177
      if (default_charset_info->hash_sort)
        default_charset_info->hash_sort(default_charset_info,
        				pos,((uchar*)key)-pos,&nr,&nr2);
      else
178
      for (; pos < (uchar*) key ; pos++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
179
      {
180
	nr^=(ulong) ((((uint) nr & 63)+nr2) * 
181
		     ((uint) default_charset_info->sort_order[(uint) *pos])) + (nr << 8);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
182 183 184 185 186
	nr2+=3;
      }
    }
    else
    {
187
      for (; pos < (uchar*) key ; pos++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
188
      {
189
	nr^=(ulong) ((((uint) nr & 63)+nr2)*((uint) *pos)) + (nr << 8);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
190 191 192 193 194 195 196 197 198 199 200
	nr2+=3;
      }
    }
  }
  return((ulong) nr);
}

	/* Calc hashvalue for a key in a record */

ulong _hp_rec_hashnr(register HP_KEYDEF *keydef, register const byte *rec)
{
201 202
  /*register*/
  ulong nr=1, nr2=4;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
203 204 205 206
  HP_KEYSEG *seg,*endseg;

  for (seg=keydef->seg,endseg=seg+keydef->keysegs ; seg < endseg ; seg++)
  {
207
    uchar *pos=(uchar*) rec+seg->start,*end=pos+seg->length;
208 209 210 211 212 213 214 215
    if (seg->null_bit)
    {
      if (rec[seg->null_pos] & seg->null_bit)
      {
	nr^= (nr << 1) | 1;
	continue;
      }
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
216 217
    if (seg->type == HA_KEYTYPE_TEXT)
    {
218 219 220 221
      if (default_charset_info->hash_sort)
        default_charset_info->hash_sort(default_charset_info,
        				pos,end-pos,&nr,&nr2);
      else
222
      for (; pos < end ; pos++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
223
      {
224
	nr^=(ulong) ((((uint) nr & 63)+nr2)*
225
		     ((uint) default_charset_info->sort_order[(uint) *pos]))+ (nr << 8);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
226 227 228 229 230
	nr2+=3;
      }
    }
    else
    {
231
      for (; pos < end ; pos++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
232 233 234 235 236 237 238 239 240
      {
	nr^=(ulong) ((((uint) nr & 63)+nr2)*((uint) *pos))+ (nr << 8);
	nr2+=3;
      }
    }
  }
  return((ulong) nr);
}

241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
#else

/*
 * Fowler/Noll/Vo hash
 *
 * The basis of the hash algorithm was taken from an idea sent by email to the
 * IEEE Posix P1003.2 mailing list from Phong Vo (kpv@research.att.com) and
 * Glenn Fowler (gsf@research.att.com).  Landon Curt Noll (chongo@toad.com)
 * later improved on their algorithm.
 *
 * The magic is in the interesting relationship between the special prime
 * 16777619 (2^24 + 403) and 2^32 and 2^8.
 *
 * This hash produces the fewest collisions of any function that we've seen so
 * far, and works well on both numbers and strings.
 */

ulong _hp_hashnr(register HP_KEYDEF *keydef, register const byte *key)
{
  register ulong nr=0;
  HP_KEYSEG *seg,*endseg;

  for (seg=keydef->seg,endseg=seg+keydef->keysegs ; seg < endseg ; seg++)
  {
    uchar *pos=(uchar*) key;
    key+=seg->length;
267 268 269 270 271 272 273 274 275 276
    if (seg->null_bit)
    {
      key++;
      if (*pos)
      {
	nr^= (nr << 1) | 1;
	continue;
      }
      pos++;
    }
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
    if (seg->type == HA_KEYTYPE_TEXT)
    {
      for (; pos < (uchar*) key ; pos++)
      {
	nr *=16777619; 
	nr ^=((uint) my_sort_order[(uint) *pos]);
      }
    }
    else
    {
      for ( ; pos < (uchar*) key ; pos++)
      {
	nr *=16777619; 
	nr ^=(uint) *pos;
      }
    }
  }
  return((ulong) nr);
}

	/* Calc hashvalue for a key in a record */

ulong _hp_rec_hashnr(register HP_KEYDEF *keydef, register const byte *rec)
{
  register ulong nr=0;
  HP_KEYSEG *seg,*endseg;

  for (seg=keydef->seg,endseg=seg+keydef->keysegs ; seg < endseg ; seg++)
  {
    uchar *pos=(uchar*) rec+seg->start,*end=pos+seg->length;
307 308 309 310 311 312 313 314
    if (seg->null_bit)
    {
      if (rec[seg->null_pos] & seg->null_bit)
      {
	nr^= (nr << 1) | 1;
	continue;
      }
    }
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
    if (seg->type == HA_KEYTYPE_TEXT)
    {
      for ( ; pos < end ; pos++)
      {
	nr *=16777619; 
	nr ^=(uint) my_sort_order[(uint) *pos];
      }
    }
    else
    {
      for ( ; pos < end ; pos++)
      {
	nr *=16777619; 
	nr ^=(uint) *pos;
      }
    }
  }
  return((ulong) nr);
}

#endif


bk@work.mysql.com's avatar
bk@work.mysql.com committed
338 339 340 341 342 343 344 345
	/* Compare keys for two records. Returns 0 if they are identical */

int _hp_rec_key_cmp(HP_KEYDEF *keydef, const byte *rec1, const byte *rec2)
{
  HP_KEYSEG *seg,*endseg;

  for (seg=keydef->seg,endseg=seg+keydef->keysegs ; seg < endseg ; seg++)
  {
346 347 348 349 350 351 352 353
    if (seg->null_bit)
    {
      if ((rec1[seg->null_pos] & seg->null_bit) !=
	  (rec2[seg->null_pos] & seg->null_bit))
	return 1;
      if (rec1[seg->null_pos] & seg->null_bit)
	continue;
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
354 355
    if (seg->type == HA_KEYTYPE_TEXT)
    {
356
      if (my_sortcmp(default_charset_info,rec1+seg->start,rec2+seg->start,seg->length))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
357 358 359 360 361 362 363 364 365 366 367
	return 1;
    }
    else
    {
      if (bcmp(rec1+seg->start,rec2+seg->start,seg->length))
	return 1;
    }
  }
  return 0;
}

368
	/* Compare a key in a record to a whole key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
369 370 371 372 373

int _hp_key_cmp(HP_KEYDEF *keydef, const byte *rec, const byte *key)
{
  HP_KEYSEG *seg,*endseg;

374 375 376
  for (seg=keydef->seg,endseg=seg+keydef->keysegs ;
       seg < endseg ;
       key+= (seg++)->length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
377
  {
378 379 380 381 382 383 384 385
    if (seg->null_bit)
    {
      int found_null=test(rec[seg->null_pos] & seg->null_bit);
      if (found_null != (int) *key++)
	return 1;
      if (found_null)
	continue;
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
386 387
    if (seg->type == HA_KEYTYPE_TEXT)
    {
388 389 390 391 392 393
      /* 
         BAR TODO: this will not use default_charset_info
         I need Ram to apply his HEAP patches with
         CHARSET_INFO field in HP segments
      */
      if (my_sortcmp(default_charset_info,rec+seg->start,key,seg->length))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
	return 1;
    }
    else
    {
      if (bcmp(rec+seg->start,key,seg->length))
	return 1;
    }
  }
  return 0;
}


	/* Copy a key from a record to a keybuffer */

void _hp_make_key(HP_KEYDEF *keydef, byte *key, const byte *rec)
{
  HP_KEYSEG *seg,*endseg;

  for (seg=keydef->seg,endseg=seg+keydef->keysegs ; seg < endseg ; seg++)
  {
414 415
    if (seg->null_bit)
      *key++= test(rec[seg->null_pos] & seg->null_bit);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
416 417 418 419
    memcpy(key,rec+seg->start,(size_t) seg->length);
    key+=seg->length;
  }
}
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438


/*
  Test if any of the key parts are NULL.
  Return:
    1 if any of the key parts was NULL
    0 otherwise
*/

my_bool hp_if_null_in_key(HP_KEYDEF *keydef, const byte *record)
{
  HP_KEYSEG *seg,*endseg;
  for (seg=keydef->seg,endseg=seg+keydef->keysegs ; seg < endseg ; seg++)
  {
    if (seg->null_bit && (record[seg->null_pos] & seg->null_bit))
      return 1;
  }
  return 0;
}