| 384 | /* Write a hash-key to the hash-index */ |
| 385 | |
| 386 | my_bool my_hash_insert(HASH *info, const uchar *record) |
| 387 | { |
| 388 | int flag; |
| 389 | size_t idx,halfbuff,first_index; |
| 390 | my_hash_value_type hash_nr; |
| 391 | uchar *UNINIT_VAR(ptr_to_rec),*UNINIT_VAR(ptr_to_rec2); |
| 392 | HASH_LINK *data,*empty,*UNINIT_VAR(gpos),*UNINIT_VAR(gpos2),*pos; |
| 393 | |
| 394 | if (HASH_UNIQUE & info->flags) |
| 395 | { |
| 396 | uchar *key= (uchar*) my_hash_key(info, record, &idx, 1); |
| 397 | if (my_hash_search(info, key, idx)) |
| 398 | return(TRUE); /* Duplicate entry */ |
| 399 | } |
| 400 | |
| 401 | flag=0; |
| 402 | if (!(empty=(HASH_LINK*) alloc_dynamic(&info->array))) |
| 403 | return(TRUE); /* No more memory */ |
| 404 | |
| 405 | data=dynamic_element(&info->array,0,HASH_LINK*); |
| 406 | halfbuff= info->blength >> 1; |
| 407 | |
| 408 | idx=first_index=info->records-halfbuff; |
| 409 | if (idx != info->records) /* If some records */ |
| 410 | { |
| 411 | do |
| 412 | { |
| 413 | pos=data+idx; |
| 414 | hash_nr=rec_hashnr(info,pos->data); |
| 415 | if (flag == 0) /* First loop; Check if ok */ |
| 416 | if (my_hash_mask(hash_nr, info->blength, info->records) != first_index) |
| 417 | break; |
| 418 | if (!(hash_nr & halfbuff)) |
| 419 | { /* Key will not move */ |
| 420 | if (!(flag & LOWFIND)) |
| 421 | { |
| 422 | if (flag & HIGHFIND) |
| 423 | { |
| 424 | flag=LOWFIND | HIGHFIND; |
| 425 | /* key shall be moved to the current empty position */ |
| 426 | gpos=empty; |
| 427 | ptr_to_rec=pos->data; |
| 428 | empty=pos; /* This place is now free */ |
| 429 | } |
| 430 | else |
| 431 | { |
| 432 | flag=LOWFIND | LOWUSED; /* key isn't changed */ |
| 433 | gpos=pos; |
| 434 | ptr_to_rec=pos->data; |
| 435 | } |
| 436 | } |
| 437 | else |
| 438 | { |
| 439 | if (!(flag & LOWUSED)) |
| 440 | { |
| 441 | /* Change link of previous LOW-key */ |
| 442 | gpos->data=ptr_to_rec; |
| 443 | gpos->next= (uint) (pos-data); |