1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
3
This library is free software; you can redistribute it and/or
4
modify it under the terms of the GNU Library General Public
5
License as published by the Free Software Foundation; either
6
version 2 of the License, or (at your option) any later version.
8
This library is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
Library General Public License for more details.
13
You should have received a copy of the GNU Library General Public
14
License along with this library; if not, write to the Free
15
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18
/* The hash functions used for saveing keys */
19
/* One of key_length or key_length_offset must be given */
20
/* Key length of 0 isn't allowed */
22
#include "mysys_priv.h"
27
#define NO_RECORD ((uint) -1)
33
static uint hash_mask(uint hashnr,uint buffmax,uint maxlength);
34
static void movelink(HASH_LINK *array,uint pos,uint next_link,uint newlink);
35
static uint calc_hashnr(const byte *key,uint length);
36
static uint calc_hashnr_caseup(const byte *key,uint length);
37
static int hashcmp(HASH *hash,HASH_LINK *pos,const byte *key,uint length);
40
my_bool _hash_init(HASH *hash,uint size,uint key_offset,uint key_length,
42
void (*free_element)(void*),uint flags CALLER_INFO_PROTO)
44
DBUG_ENTER("hash_init");
45
DBUG_PRINT("enter",("hash: %lx size: %d",hash,size));
48
if (my_init_dynamic_array_ci(&hash->array,sizeof(HASH_LINK),size,0))
50
hash->free=0; /* Allow call to hash_free */
53
hash->key_offset=key_offset;
54
hash->key_length=key_length;
56
hash->current_record= NO_RECORD; /* For the future */
57
hash->get_key=get_key;
58
hash->free=free_element;
60
if (flags & HASH_CASE_INSENSITIVE)
61
hash->calc_hashnr=calc_hashnr_caseup;
63
hash->calc_hashnr=calc_hashnr;
68
void hash_free(HASH *hash)
70
DBUG_ENTER("hash_free");
74
HASH_LINK *data=dynamic_element(&hash->array,0,HASH_LINK*);
75
for (i=0,records=hash->records ; i < records ; i++)
76
(*hash->free)(data[i].data);
79
delete_dynamic(&hash->array);
84
/* some helper functions */
87
This function is char* instead of byte* as HPUX11 compiler can't
88
handle inline functions that are not defined as native types
92
hash_key(HASH *hash,const byte *record,uint *length,my_bool first)
95
return (*hash->get_key)(record,length,first);
96
*length=hash->key_length;
97
return (byte*) record+hash->key_offset;
100
/* Calculate pos according to keys */
102
static uint hash_mask(uint hashnr,uint buffmax,uint maxlength)
104
if ((hashnr & (buffmax-1)) < maxlength) return (hashnr & (buffmax-1));
105
return (hashnr & ((buffmax >> 1) -1));
108
static uint hash_rec_mask(HASH *hash,HASH_LINK *pos,uint buffmax,
112
byte *key= (byte*) hash_key(hash,pos->data,&length,0);
113
return hash_mask((*hash->calc_hashnr)(key,length),buffmax,maxlength);
116
#ifndef NEW_HASH_FUNCTION
118
/* Calc hashvalue for a key */
120
static uint calc_hashnr(const byte *key,uint length)
122
register uint nr=1, nr2=4;
125
nr^= (((nr & 63)+nr2)*((uint) (uchar) *key++))+ (nr << 8);
131
/* Calc hashvalue for a key, case indepenently */
133
static uint calc_hashnr_caseup(const byte *key,uint length)
135
register uint nr=1, nr2=4;
138
nr^= (((nr & 63)+nr2)*((uint) (uchar) toupper(*key++)))+ (nr << 8);
147
* Fowler/Noll/Vo hash
149
* The basis of the hash algorithm was taken from an idea sent by email to the
150
* IEEE Posix P1003.2 mailing list from Phong Vo (kpv@research.att.com) and
151
* Glenn Fowler (gsf@research.att.com). Landon Curt Noll (chongo@toad.com)
152
* later improved on their algorithm.
154
* The magic is in the interesting relationship between the special prime
155
* 16777619 (2^24 + 403) and 2^32 and 2^8.
157
* This hash produces the fewest collisions of any function that we've seen so
158
* far, and works well on both numbers and strings.
161
uint calc_hashnr(const byte *key, uint len)
163
const byte *end=key+len;
165
for (hash = 0; key < end; key++)
168
hash ^= (uint) *(uchar*) key;
173
uint calc_hashnr_caseup(const byte *key, uint len)
175
const byte *end=key+len;
177
for (hash = 0; key < end; key++)
180
hash ^= (uint) (uchar) toupper(*key);
188
#ifndef __SUNPRO_C /* SUNPRO can't handle this */
191
unsigned int rec_hashnr(HASH *hash,const byte *record)
194
byte *key= (byte*) hash_key(hash,record,&length,0);
195
return (*hash->calc_hashnr)(key,length);
199
/* Search after a record based on a key */
200
/* Sets info->current_ptr to found record */
202
gptr hash_search(HASH *hash,const byte *key,uint length)
206
DBUG_ENTER("hash_search");
211
idx=hash_mask((*hash->calc_hashnr)(key,length ? length :
213
hash->blength,hash->records);
216
pos= dynamic_element(&hash->array,idx,HASH_LINK*);
217
if (!hashcmp(hash,pos,key,length))
219
DBUG_PRINT("exit",("found key at %d",idx));
220
hash->current_record= idx;
221
DBUG_RETURN (pos->data);
225
flag=0; /* Reset flag */
226
if (hash_rec_mask(hash,pos,hash->blength,hash->records) != idx)
227
break; /* Wrong link */
230
while ((idx=pos->next) != NO_RECORD);
232
hash->current_record= NO_RECORD;
236
/* Get next record with identical key */
237
/* Can only be called if previous calls was hash_search */
239
gptr hash_next(HASH *hash,const byte *key,uint length)
244
if (hash->current_record != NO_RECORD)
246
HASH_LINK *data=dynamic_element(&hash->array,0,HASH_LINK*);
247
for (idx=data[hash->current_record].next; idx != NO_RECORD ; idx=pos->next)
250
if (!hashcmp(hash,pos,key,length))
252
hash->current_record= idx;
256
hash->current_record=NO_RECORD;
262
/* Change link from pos to new_link */
264
static void movelink(HASH_LINK *array,uint find,uint next_link,uint newlink)
269
old_link=array+next_link;
271
while ((next_link=old_link->next) != find);
272
old_link->next= newlink;
276
/* Compare a key in a record to a whole key. Return 0 if identical */
278
static int hashcmp(HASH *hash,HASH_LINK *pos,const byte *key,uint length)
281
byte *rec_key= (byte*) hash_key(hash,pos->data,&rec_keylength,1);
282
return (length && length != rec_keylength) ||
283
(hash->flags & HASH_CASE_INSENSITIVE ?
284
my_casecmp(rec_key,key,rec_keylength) :
285
memcmp(rec_key,key,rec_keylength));
289
/* Write a hash-key to the hash-index */
291
my_bool hash_insert(HASH *info,const byte *record)
294
uint halfbuff,hash_nr,first_index,idx;
295
byte *ptr_to_rec,*ptr_to_rec2;
296
HASH_LINK *data,*empty,*gpos,*gpos2,*pos;
298
LINT_INIT(gpos); LINT_INIT(gpos2);
299
LINT_INIT(ptr_to_rec); LINT_INIT(ptr_to_rec2);
302
if (!(empty=(HASH_LINK*) alloc_dynamic(&info->array)))
303
return(TRUE); /* No more memory */
305
info->current_record= NO_RECORD;
306
data=dynamic_element(&info->array,0,HASH_LINK*);
307
halfbuff= info->blength >> 1;
309
idx=first_index=info->records-halfbuff;
310
if (idx != info->records) /* If some records */
315
hash_nr=rec_hashnr(info,pos->data);
316
if (flag == 0) /* First loop; Check if ok */
317
if (hash_mask(hash_nr,info->blength,info->records) != first_index)
319
if (!(hash_nr & halfbuff))
320
{ /* Key will not move */
321
if (!(flag & LOWFIND))
325
flag=LOWFIND | HIGHFIND;
326
/* key shall be moved to the current empty position */
328
ptr_to_rec=pos->data;
329
empty=pos; /* This place is now free */
333
flag=LOWFIND | LOWUSED; /* key isn't changed */
335
ptr_to_rec=pos->data;
340
if (!(flag & LOWUSED))
342
/* Change link of previous LOW-key */
343
gpos->data=ptr_to_rec;
344
gpos->next=(uint) (pos-data);
345
flag= (flag & HIGHFIND) | (LOWFIND | LOWUSED);
348
ptr_to_rec=pos->data;
352
{ /* key will be moved */
353
if (!(flag & HIGHFIND))
355
flag= (flag & LOWFIND) | HIGHFIND;
356
/* key shall be moved to the last (empty) position */
357
gpos2 = empty; empty=pos;
358
ptr_to_rec2=pos->data;
362
if (!(flag & HIGHUSED))
364
/* Change link of previous hash-key and save */
365
gpos2->data=ptr_to_rec2;
366
gpos2->next=(uint) (pos-data);
367
flag= (flag & LOWFIND) | (HIGHFIND | HIGHUSED);
370
ptr_to_rec2=pos->data;
374
while ((idx=pos->next) != NO_RECORD);
376
if ((flag & (LOWFIND | LOWUSED)) == LOWFIND)
378
gpos->data=ptr_to_rec;
379
gpos->next=NO_RECORD;
381
if ((flag & (HIGHFIND | HIGHUSED)) == HIGHFIND)
383
gpos2->data=ptr_to_rec2;
384
gpos2->next=NO_RECORD;
387
/* Check if we are at the empty position */
389
idx=hash_mask(rec_hashnr(info,record),info->blength,info->records+1);
393
pos->data=(byte*) record;
398
/* Check if more records in same hash-nr family */
400
gpos=data+hash_rec_mask(info,pos,info->blength,info->records+1);
403
pos->data=(byte*) record;
404
pos->next=(uint) (empty - data);
408
pos->data=(byte*) record;
410
movelink(data,(uint) (pos-data),(uint) (gpos-data),(uint) (empty-data));
413
if (++info->records == info->blength)
414
info->blength+= info->blength;
419
/******************************************************************************
420
** Remove one record from hash-table. The record with the same record
422
** if there is a free-function it's called for record if found
423
******************************************************************************/
425
my_bool hash_delete(HASH *hash,byte *record)
427
uint blength,pos2,pos_hashnr,lastpos_hashnr,idx,empty_index;
428
HASH_LINK *data,*lastpos,*gpos,*pos,*pos3,*empty;
429
DBUG_ENTER("hash_delete");
433
blength=hash->blength;
434
data=dynamic_element(&hash->array,0,HASH_LINK*);
435
/* Search after record with key */
436
pos=data+ hash_mask(rec_hashnr(hash,record),blength,hash->records);
439
while (pos->data != record)
442
if (pos->next == NO_RECORD)
443
DBUG_RETURN(1); /* Key not found */
447
if ( --(hash->records) < hash->blength >> 1) hash->blength>>=1;
448
hash->current_record= NO_RECORD;
449
lastpos=data+hash->records;
451
/* Remove link to record */
452
empty=pos; empty_index=(uint) (empty-data);
454
gpos->next=pos->next; /* unlink current ptr */
455
else if (pos->next != NO_RECORD)
457
empty=data+(empty_index=pos->next);
458
pos->data=empty->data;
459
pos->next=empty->next;
462
if (empty == lastpos) /* last key at wrong pos or no next link */
465
/* Move the last key (lastpos) */
466
lastpos_hashnr=rec_hashnr(hash,lastpos->data);
467
/* pos is where lastpos should be */
468
pos=data+hash_mask(lastpos_hashnr,hash->blength,hash->records);
469
if (pos == empty) /* Move to empty position. */
474
pos_hashnr=rec_hashnr(hash,pos->data);
475
/* pos3 is where the pos should be */
476
pos3= data+hash_mask(pos_hashnr,hash->blength,hash->records);
478
{ /* pos is on wrong posit */
479
empty[0]=pos[0]; /* Save it here */
480
pos[0]=lastpos[0]; /* This should be here */
481
movelink(data,(uint) (pos-data),(uint) (pos3-data),empty_index);
484
pos2= hash_mask(lastpos_hashnr,blength,hash->records+1);
485
if (pos2 == hash_mask(pos_hashnr,blength,hash->records+1))
486
{ /* Identical key-positions */
487
if (pos2 != hash->records)
490
movelink(data,(uint) (lastpos-data),(uint) (pos-data),empty_index);
493
idx= (uint) (pos-data); /* Link pos->next after lastpos */
495
else idx= NO_RECORD; /* Different positions merge */
498
movelink(data,idx,empty_index,pos->next);
499
pos->next=empty_index;
502
VOID(pop_dynamic(&hash->array));
504
(*hash->free)((byte*) record);
509
Update keys when record has changed.
510
This is much more efficent than using a delete & insert.
513
my_bool hash_update(HASH *hash,byte *record,byte *old_key,uint old_key_length)
515
uint idx,new_index,new_pos_index,blength,records,empty;
516
HASH_LINK org_link,*data,*previous,*pos;
517
DBUG_ENTER("hash_update");
519
data=dynamic_element(&hash->array,0,HASH_LINK*);
520
blength=hash->blength; records=hash->records;
522
/* Search after record with key */
524
idx=hash_mask((*hash->calc_hashnr)(old_key,(old_key_length ?
528
new_index=hash_mask(rec_hashnr(hash,record),blength,records);
529
if (idx == new_index)
530
DBUG_RETURN(0); /* Nothing to do (No record check) */
535
if ((pos= data+idx)->data == record)
538
if ((idx=pos->next) == NO_RECORD)
539
DBUG_RETURN(1); /* Not found in links */
541
hash->current_record= NO_RECORD;
545
/* Relink record from current chain */
549
if (pos->next != NO_RECORD)
552
*pos= data[pos->next];
556
previous->next=pos->next; /* unlink pos */
558
/* Move data to correct position */
560
new_pos_index=hash_rec_mask(hash,pos,blength,records);
561
if (new_index != new_pos_index)
562
{ /* Other record in wrong position */
564
movelink(data,new_index,new_pos_index,empty);
565
org_link.next=NO_RECORD;
566
data[new_index]= org_link;
569
{ /* Link in chain at right position */
570
org_link.next=data[new_index].next;
571
data[empty]=org_link;
572
data[new_index].next=empty;
578
byte *hash_element(HASH *hash,uint idx)
580
if (idx < hash->records)
581
return dynamic_element(&hash->array,idx,HASH_LINK*)->data;
588
my_bool hash_check(HASH *hash)
591
uint i,rec_link,found,max_links,seek,links,idx;
592
uint records,blength;
593
HASH_LINK *data,*hash_info;
595
records=hash->records; blength=hash->blength;
596
data=dynamic_element(&hash->array,0,HASH_LINK*);
599
for (i=found=max_links=seek=0 ; i < records ; i++)
601
if (hash_rec_mask(hash,data+i,blength,records) == i)
603
found++; seek++; links=1;
604
for (idx=data[i].next ;
605
idx != NO_RECORD && found < records + 1;
611
("Found pointer outside array to %d from link starting at %d",
617
if ((rec_link=hash_rec_mask(hash,hash_info,blength,records)) != i)
620
("Record in wrong link at %d: Start %d Record: %lx Record-link %d", idx,i,hash_info->data,rec_link));
626
if (links > max_links) max_links=links;
629
if (found != records)
631
DBUG_PRINT("error",("Found %ld of %ld records"));
636
("records: %ld seeks: %d max links: %d hitrate: %.2f",
637
records,seek,max_links,(float) seek / (float) records));