Mysql源码学习――没那么简单的Hash(三)

2014-11-24 13:40:47 · 作者: · 浏览: 3

if (flag & HIGHFIND)

{

flag=LOWFIND | HIGHFIND;

/* key shall be moved to the current empty position */

gpos=empty;

ptr_to_rec=pos->data;

empty=pos; /* This place is now free */

}

else

{

flag=LOWFIND | LOWUSED; /* key isn't changed */

gpos=pos;

ptr_to_rec=pos->data;

}

}

else

{

if (!(flag & LOWUSED))

{

/* Change link of previous LOW-key */

gpos->data=ptr_to_rec;

gpos->next= (uint) (pos-data);

flag= (flag & HIGHFIND) | (LOWFIND | LOWUSED);

}

gpos=pos;

ptr_to_rec=pos->data;

}

}

else

{ /* key will be moved */

if (!(flag & HIGHFIND))

{

flag= (flag & LOWFIND) | HIGHFIND;

/* key shall be moved to the last (empty) position */

gpos2 = empty; empty=pos;

ptr_to_rec2=pos->data;

}

else

{

if (!(flag & HIGHUSED))

{

/* Change link of previous hash-key and save */

gpos2->data=ptr_to_rec2;

gpos2->next=(uint) (pos-data);

flag= (flag & LOWFIND) | (HIGHFIND | HIGHUSED);

}

gpos2=pos;

ptr_to_rec2=pos->data;

}

}

}

while ((idx=pos->next) != NO_RECORD);

if ((flag & (LOWFIND | LOWUSED)) == LOWFIND)

{

gpos->data=ptr_to_rec;

gpos->next=NO_RECORD;

}

if ((flag & (HIGHFIND | HIGHUSED)) == HIGHFIND)

{

gpos2->data=ptr_to_rec2;

gpos2->next=NO_RECORD;

}

}

/* Check if we are at the empty position */

idx= my_hash_mask(rec_hashnr(info, record), info->blength, info->records + 1);

pos=data+idx;

if (pos == empty)

{

pos->data=(uchar*) record;

pos->next=NO_RECORD;

}

else

{

/* Check if more records in same hash-nr family */

empty[0]=pos[0];

gpos= data + my_hash_rec_mask(info, pos, info->blength, info->records + 1);

if (pos == gpos)

{

pos->data=(uchar*) record;

pos->next=(uint) (empty - data);

}

else

{

pos->data=(uchar*) record;

pos->next=NO_RECORD;

movelink(data,(uint) (pos-data),(uint) (gpos-data),(uint) (empty-data));

}

}

if (++info->records == info->blength)

info->blength+= info->blength;

return(0);

}

同时给出动态hash函数如下:

static uint my_hash_mask(my_hash_value_type hashnr, size_t buffmax,

size_t maxlength)

{

if ((hashnr & (buffmax-1)) < maxlength) return (hashnr & (buffmax-1));

return (hashnr & ((buffmax >> 1) -1));

}

可以看出,hash函数是hash key