20 #include "mysys_priv.h"
25 #define NO_RECORD ((uint) -1)
36 static uint my_hash_mask(my_hash_value_type hashnr,
37 size_t buffmax,
size_t maxlength);
38 static void movelink(
HASH_LINK *array,uint pos,uint next_link,uint newlink);
39 static int hashcmp(
const HASH *hash,
HASH_LINK *pos,
const uchar *key,
42 static my_hash_value_type calc_hash(
const HASH *hash,
43 const uchar *key,
size_t length)
46 hash->charset->coll->hash_sort(hash->charset,(uchar*) key,length,&nr1,&nr2);
47 return (my_hash_value_type)nr1;
77 ulong
size,
size_t key_offset,
size_t key_length,
78 my_hash_get_key get_key,
79 void (*free_element)(
void*), uint
flags)
81 DBUG_ENTER(
"my_hash_init");
82 DBUG_PRINT(
"enter",(
"hash: 0x%lx size: %u", (
long) hash, (uint) size));
85 hash->key_offset=key_offset;
86 hash->key_length=key_length;
88 hash->get_key=get_key;
89 hash->free=free_element;
91 hash->charset=charset;
92 DBUG_RETURN(my_init_dynamic_array_ci(&hash->array,
108 static inline void my_hash_free_elements(
HASH *hash)
115 (*hash->free)((data++)->data);
131 void my_hash_free(
HASH *hash)
133 DBUG_ENTER(
"my_hash_free");
134 DBUG_PRINT(
"enter",(
"hash: 0x%lx", (
long) hash));
136 my_hash_free_elements(hash);
138 delete_dynamic(&hash->array);
152 void my_hash_reset(
HASH *hash)
154 DBUG_ENTER(
"my_hash_reset");
155 DBUG_PRINT(
"enter",(
"hash: 0x%lxd", (
long) hash));
157 my_hash_free_elements(hash);
158 reset_dynamic(&hash->array);
172 my_hash_key(
const HASH *hash,
const uchar *
record,
size_t *length,
176 return (
char*) (*hash->get_key)(record,length,first);
177 *length=hash->key_length;
178 return (
char*) record+hash->key_offset;
183 static uint my_hash_mask(my_hash_value_type hashnr,
size_t buffmax,
186 if ((hashnr & (buffmax-1)) < maxlength)
return (hashnr & (buffmax-1));
187 return (hashnr & ((buffmax >> 1) -1));
190 static uint my_hash_rec_mask(
const HASH *hash,
HASH_LINK *pos,
191 size_t buffmax,
size_t maxlength)
194 uchar *key= (uchar*) my_hash_key(hash, pos->data, &length, 0);
195 return my_hash_mask(calc_hash(hash, key, length), buffmax, maxlength);
202 #if !defined(__USLC__) && !defined(__sgi)
205 my_hash_value_type rec_hashnr(
HASH *hash,
const uchar *record)
208 uchar *key= (uchar*) my_hash_key(hash, record, &length, 0);
209 return calc_hash(hash,key,length);
213 uchar* my_hash_search(
const HASH *hash,
const uchar *key,
size_t length)
215 HASH_SEARCH_STATE state;
216 return my_hash_first(hash, key, length, &state);
219 uchar* my_hash_search_using_hash_value(
const HASH *hash,
220 my_hash_value_type hash_value,
224 HASH_SEARCH_STATE state;
225 return my_hash_first_from_hash_value(hash, hash_value,
226 key, length, &state);
229 my_hash_value_type my_calc_hash(
const HASH *hash,
230 const uchar *key,
size_t length)
232 return calc_hash(hash, key, length ? length : hash->key_length);
243 uchar* my_hash_first(
const HASH *hash,
const uchar *key,
size_t length,
244 HASH_SEARCH_STATE *current_record)
247 if (my_hash_inited(hash))
248 res= my_hash_first_from_hash_value(hash,
249 calc_hash(hash, key, length ? length : hash->key_length),
250 key, length, current_record);
257 uchar* my_hash_first_from_hash_value(
const HASH *hash,
258 my_hash_value_type hash_value,
261 HASH_SEARCH_STATE *current_record)
265 DBUG_ENTER(
"my_hash_first_from_hash_value");
270 idx= my_hash_mask(hash_value,
271 hash->blength, hash->records);
274 pos= dynamic_element(&hash->array,idx,
HASH_LINK*);
275 if (!hashcmp(hash,pos,key,length))
277 DBUG_PRINT(
"exit",(
"found key at %d",idx));
278 *current_record= idx;
279 DBUG_RETURN (pos->data);
284 if (my_hash_rec_mask(hash, pos, hash->blength, hash->records) != idx)
288 while ((idx=pos->next) != NO_RECORD);
290 *current_record= NO_RECORD;
297 uchar* my_hash_next(
const HASH *hash,
const uchar *key,
size_t length,
298 HASH_SEARCH_STATE *current_record)
303 if (*current_record != NO_RECORD)
306 for (idx=data[*current_record].next; idx != NO_RECORD ; idx=pos->next)
309 if (!hashcmp(hash,pos,key,length))
311 *current_record= idx;
315 *current_record= NO_RECORD;
323 static void movelink(
HASH_LINK *array,uint find,uint next_link,uint newlink)
328 old_link=array+next_link;
330 while ((next_link=old_link->next) != find);
331 old_link->next= newlink;
354 static int hashcmp(
const HASH *hash,
HASH_LINK *pos,
const uchar *key,
357 size_t rec_keylength;
358 uchar *rec_key= (uchar*) my_hash_key(hash, pos->data, &rec_keylength, 1);
359 return ((length && length != rec_keylength) ||
360 my_strnncoll(hash->charset, (uchar*) rec_key, rec_keylength,
361 (uchar*) key, rec_keylength));
367 my_bool my_hash_insert(
HASH *info,
const uchar *record)
370 size_t idx,halfbuff,first_index;
371 my_hash_value_type hash_nr;
372 uchar *UNINIT_VAR(ptr_to_rec),*UNINIT_VAR(ptr_to_rec2);
373 HASH_LINK *data,*empty,*UNINIT_VAR(gpos),*UNINIT_VAR(gpos2),*pos;
375 if (HASH_UNIQUE & info->flags)
377 uchar *key= (uchar*) my_hash_key(info, record, &idx, 1);
378 if (my_hash_search(info, key, idx))
383 if (!(empty=(
HASH_LINK*) alloc_dynamic(&info->array)))
386 data=dynamic_element(&info->array,0,
HASH_LINK*);
387 halfbuff= info->blength >> 1;
389 idx=first_index=info->records-halfbuff;
390 if (idx != info->records)
395 hash_nr=rec_hashnr(info,pos->data);
397 if (my_hash_mask(hash_nr, info->blength, info->records) != first_index)
399 if (!(hash_nr & halfbuff))
401 if (!(flag & LOWFIND))
405 flag=LOWFIND | HIGHFIND;
408 ptr_to_rec=pos->data;
413 flag=LOWFIND | LOWUSED;
415 ptr_to_rec=pos->data;
420 if (!(flag & LOWUSED))
423 gpos->data=ptr_to_rec;
424 gpos->next= (uint) (pos-data);
425 flag= (flag & HIGHFIND) | (LOWFIND | LOWUSED);
428 ptr_to_rec=pos->data;
433 if (!(flag & HIGHFIND))
435 flag= (flag & LOWFIND) | HIGHFIND;
437 gpos2 = empty; empty=pos;
438 ptr_to_rec2=pos->data;
442 if (!(flag & HIGHUSED))
445 gpos2->data=ptr_to_rec2;
446 gpos2->next=(uint) (pos-data);
447 flag= (flag & LOWFIND) | (HIGHFIND | HIGHUSED);
450 ptr_to_rec2=pos->data;
454 while ((idx=pos->next) != NO_RECORD);
456 if ((flag & (LOWFIND | LOWUSED)) == LOWFIND)
458 gpos->data=ptr_to_rec;
459 gpos->next=NO_RECORD;
461 if ((flag & (HIGHFIND | HIGHUSED)) == HIGHFIND)
463 gpos2->data=ptr_to_rec2;
464 gpos2->next=NO_RECORD;
469 idx= my_hash_mask(rec_hashnr(info, record), info->blength, info->records + 1);
473 pos->data=(uchar*) record;
480 gpos= data + my_hash_rec_mask(info, pos, info->blength, info->records + 1);
483 pos->data=(uchar*) record;
484 pos->next=(uint) (empty - data);
488 pos->data=(uchar*) record;
490 movelink(data,(uint) (pos-data),(uint) (gpos-data),(uint) (empty-data));
493 if (++info->records == info->blength)
494 info->blength+= info->blength;
505 my_bool my_hash_delete(
HASH *hash, uchar *record)
507 uint blength,pos2,idx,empty_index;
508 my_hash_value_type pos_hashnr, lastpos_hashnr;
509 HASH_LINK *data,*lastpos,*gpos,*pos,*pos3,*empty;
510 DBUG_ENTER(
"my_hash_delete");
514 blength=hash->blength;
515 data=dynamic_element(&hash->array,0,
HASH_LINK*);
517 pos= data + my_hash_mask(rec_hashnr(hash, record), blength, hash->records);
520 while (pos->data != record)
523 if (pos->next == NO_RECORD)
528 if ( --(hash->records) < hash->blength >> 1) hash->blength>>=1;
529 lastpos=data+hash->records;
532 empty=pos; empty_index=(uint) (empty-data);
534 gpos->next=pos->next;
535 else if (pos->next != NO_RECORD)
537 empty=data+(empty_index=pos->next);
538 pos->data=empty->data;
539 pos->next=empty->next;
542 if (empty == lastpos)
546 lastpos_hashnr=rec_hashnr(hash,lastpos->data);
548 pos= data + my_hash_mask(lastpos_hashnr, hash->blength, hash->records);
554 pos_hashnr=rec_hashnr(hash,pos->data);
556 pos3= data + my_hash_mask(pos_hashnr, hash->blength, hash->records);
561 movelink(data,(uint) (pos-data),(uint) (pos3-data),empty_index);
564 pos2= my_hash_mask(lastpos_hashnr, blength, hash->records + 1);
565 if (pos2 == my_hash_mask(pos_hashnr, blength, hash->records + 1))
567 if (pos2 != hash->records)
570 movelink(data,(uint) (lastpos-data),(uint) (pos-data),empty_index);
573 idx= (uint) (pos-data);
578 movelink(data,idx,empty_index,pos->next);
579 pos->next=empty_index;
582 (void) pop_dynamic(&hash->array);
584 (*hash->free)((uchar*) record);
593 my_bool my_hash_update(
HASH *hash, uchar *record, uchar *old_key,
594 size_t old_key_length)
596 uint new_index,new_pos_index,blength,records;
599 DBUG_ENTER(
"my_hash_update");
601 if (HASH_UNIQUE & hash->flags)
603 HASH_SEARCH_STATE state;
604 uchar *found, *new_key= (uchar*) my_hash_key(hash, record, &idx, 1);
605 if ((found= my_hash_first(hash, new_key, idx, &state)))
612 while ((found= my_hash_next(hash, new_key, idx, &state)));
616 data=dynamic_element(&hash->array,0,
HASH_LINK*);
617 blength=hash->blength; records=hash->records;
621 idx= my_hash_mask(calc_hash(hash, old_key, (old_key_length ?
625 new_index= my_hash_mask(rec_hashnr(hash, record), blength, records);
626 if (idx == new_index)
632 if ((pos= data+idx)->data == record)
635 if ((idx=pos->next) == NO_RECORD)
645 if (pos->next != NO_RECORD)
648 *pos= data[pos->next];
652 previous->next=pos->next;
655 if (new_index == empty)
669 data[empty]= org_link;
671 data[empty].next= NO_RECORD;
675 new_pos_index= my_hash_rec_mask(hash, pos, blength, records);
676 if (new_index != new_pos_index)
679 movelink(data,new_index,new_pos_index,empty);
680 org_link.next=NO_RECORD;
681 data[new_index]= org_link;
685 org_link.next=data[new_index].next;
686 data[empty]=org_link;
687 data[new_index].next=empty;
693 uchar *my_hash_element(
HASH *hash, ulong idx)
695 if (idx < hash->records)
696 return dynamic_element(&hash->array,idx,
HASH_LINK*)->data;
706 void my_hash_replace(
HASH *hash, HASH_SEARCH_STATE *current_record,
709 if (*current_record != NO_RECORD)
710 dynamic_element(&hash->array, *current_record,
HASH_LINK*)->data= new_row;
716 my_bool my_hash_check(
HASH *hash)
719 uint
i,rec_link,found,max_links,seek,links,idx;
720 uint records,blength;
723 records=hash->records; blength=hash->blength;
724 data=dynamic_element(&hash->array,0,
HASH_LINK*);
727 for (i=found=max_links=seek=0 ; i < records ; i++)
729 if (my_hash_rec_mask(hash, data + i, blength, records) == i)
731 found++; seek++; links=1;
732 for (idx=data[i].next ;
733 idx != NO_RECORD && found < records + 1;
739 (
"Found pointer outside array to %d from link starting at %d",
745 if ((rec_link= my_hash_rec_mask(hash, hash_info,
746 blength, records)) != i)
748 DBUG_PRINT(
"error", (
"Record in wrong link at %d: Start %d "
749 "Record: 0x%lx Record-link %d",
750 idx, i, (
long) hash_info->data, rec_link));
756 if (links > max_links) max_links=links;
759 if (found != records)
761 DBUG_PRINT(
"error",(
"Found %u of %u records", found, records));
766 (
"records: %u seeks: %d max links: %d hitrate: %.2f",
767 records,seek,max_links,(
float) seek / (
float) records));