mirror of
https://github.com/MariaDB/server.git
synced 2025-01-17 12:32:27 +01:00
68375e0f00
client/mysql.cc: new ctypes client/mysqldump.c: new ctypes client/mysqltest.c: new ctypes client/sql_string.cc: new ctypes client/sql_string.h: new ctypes extra/mysql_install.c: new ctypes extra/replace.c: new ctypes extra/resolve_stack_dump.c: new ctypes extra/resolveip.c: new ctypes heap/hp_hash.c: new ctypes include/m_ctype.h: new ctypes include/my_sys.h: new ctypes isam/_key.c: new ctypes isam/_search.c: new ctypes libmysql/Makefile.shared: new ctypes libmysql/libmysql.c: new ctypes myisam/ft_dump.c: new ctypes myisam/ft_parser.c: new ctypes myisam/mi_test1.c: new ctypes mysys/charset.c: new ctypes mysys/default.c: new ctypes mysys/getvar.c: new ctypes mysys/hash.c: new ctypes mysys/mf_casecnv.c: new ctypes mysys/mf_dirname.c: new ctypes mysys/mf_format.c: new ctypes mysys/mf_iocache2.c: new ctypes mysys/mf_soundex.c: new ctypes mysys/mf_wfile.c: new ctypes mysys/my_error.c: new ctypes mysys/my_getwd.c: new ctypes mysys/my_init.c: new ctypes mysys/my_vsnprintf.c: new ctypes mysys/typelib.c: new ctypes sql/convert.cc: new ctypes sql/des_key_file.cc: new ctypes sql/field.cc: new ctypes sql/field.h: new ctypes sql/field_conv.cc: new ctypes sql/filesort.cc: new ctypes sql/ha_innodb.cc: new ctypes sql/hostname.cc: new ctypes sql/init.cc: new ctypes sql/item.cc: new ctypes sql/item_func.cc: new ctypes sql/item_strfunc.cc: new ctypes sql/item_sum.cc: new ctypes sql/item_timefunc.cc: new ctypes sql/key.cc: new ctypes sql/log.cc: new ctypes sql/mysql_priv.h: new ctypes sql/mysqld.cc: new ctypes sql/opt_range.cc: new ctypes sql/procedure.cc: new ctypes sql/slave.cc: new ctypes sql/sql_acl.cc: new ctypes sql/sql_analyse.cc: new ctypes sql/sql_base.cc: new ctypes sql/sql_cache.cc: new ctypes sql/sql_db.cc: new ctypes sql/sql_handler.cc: new ctypes sql/sql_lex.cc: new ctypes sql/sql_parse.cc: new ctypes sql/sql_show.cc: new ctypes sql/sql_string.cc: new ctypes sql/sql_string.h: new ctypes sql/sql_table.cc: new ctypes sql/sql_yacc.yy: new ctypes sql/table.cc: new ctypes sql/time.cc: new ctypes strings/Makefile.am: new ctypes strings/ctype-big5.c: new ctypes strings/ctype-czech.c: new ctypes strings/ctype-gbk.c: new ctypes strings/ctype-latin1_de.c: new ctypes strings/ctype-sjis.c: new ctypes strings/ctype-tis620.c: new ctypes strings/ctype.c: new ctypes strings/str2int.c: new ctypes strings/strto.c: new ctypes tools/mysqlmanager.c: new ctypes
428 lines
9.9 KiB
C
428 lines
9.9 KiB
C
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
|
|
|
|
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.
|
|
|
|
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.
|
|
|
|
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;
|
|
}
|
|
|
|
#ifndef NEW_HASH_FUNCTION
|
|
|
|
/* Calc hashvalue for a key */
|
|
|
|
ulong _hp_hashnr(register HP_KEYDEF *keydef, register const byte *key)
|
|
{
|
|
register ulong nr=1, nr2=4;
|
|
HP_KEYSEG *seg,*endseg;
|
|
|
|
for (seg=keydef->seg,endseg=seg+keydef->keysegs ; seg < endseg ; seg++)
|
|
{
|
|
uchar *pos=(uchar*) key;
|
|
key+=seg->length;
|
|
if (seg->null_bit)
|
|
{
|
|
key++; /* Skipp null byte */
|
|
if (*pos) /* Found null */
|
|
{
|
|
nr^= (nr << 1) | 1;
|
|
continue;
|
|
}
|
|
pos++;
|
|
}
|
|
if (seg->type == HA_KEYTYPE_TEXT)
|
|
{
|
|
for (; pos < (uchar*) key ; pos++)
|
|
{
|
|
nr^=(ulong) ((((uint) nr & 63)+nr2) *
|
|
((uint) default_charset_info->sort_order[(uint) *pos])) + (nr << 8);
|
|
nr2+=3;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (; pos < (uchar*) key ; pos++)
|
|
{
|
|
nr^=(ulong) ((((uint) nr & 63)+nr2)*((uint) *pos)) + (nr << 8);
|
|
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)
|
|
{
|
|
register ulong nr=1, nr2=4;
|
|
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;
|
|
if (seg->null_bit)
|
|
{
|
|
if (rec[seg->null_pos] & seg->null_bit)
|
|
{
|
|
nr^= (nr << 1) | 1;
|
|
continue;
|
|
}
|
|
}
|
|
if (seg->type == HA_KEYTYPE_TEXT)
|
|
{
|
|
for (; pos < end ; pos++)
|
|
{
|
|
nr^=(ulong) ((((uint) nr & 63)+nr2)*
|
|
((uint) default_charset_info->sort_order[(uint) *pos]))+ (nr << 8);
|
|
nr2+=3;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (; pos < end ; pos++)
|
|
{
|
|
nr^=(ulong) ((((uint) nr & 63)+nr2)*((uint) *pos))+ (nr << 8);
|
|
nr2+=3;
|
|
}
|
|
}
|
|
}
|
|
return((ulong) nr);
|
|
}
|
|
|
|
#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;
|
|
if (seg->null_bit)
|
|
{
|
|
key++;
|
|
if (*pos)
|
|
{
|
|
nr^= (nr << 1) | 1;
|
|
continue;
|
|
}
|
|
pos++;
|
|
}
|
|
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;
|
|
if (seg->null_bit)
|
|
{
|
|
if (rec[seg->null_pos] & seg->null_bit)
|
|
{
|
|
nr^= (nr << 1) | 1;
|
|
continue;
|
|
}
|
|
}
|
|
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
|
|
|
|
|
|
/* 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++)
|
|
{
|
|
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;
|
|
}
|
|
if (seg->type == HA_KEYTYPE_TEXT)
|
|
{
|
|
if (my_sortcmp(default_charset_info,rec1+seg->start,rec2+seg->start,seg->length))
|
|
return 1;
|
|
}
|
|
else
|
|
{
|
|
if (bcmp(rec1+seg->start,rec2+seg->start,seg->length))
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* Compare a key in a record to a whole key */
|
|
|
|
int _hp_key_cmp(HP_KEYDEF *keydef, const byte *rec, const byte *key)
|
|
{
|
|
HP_KEYSEG *seg,*endseg;
|
|
|
|
for (seg=keydef->seg,endseg=seg+keydef->keysegs ;
|
|
seg < endseg ;
|
|
key+= (seg++)->length)
|
|
{
|
|
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;
|
|
}
|
|
if (seg->type == HA_KEYTYPE_TEXT)
|
|
{
|
|
/*
|
|
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))
|
|
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++)
|
|
{
|
|
if (seg->null_bit)
|
|
*key++= test(rec[seg->null_pos] & seg->null_bit);
|
|
memcpy(key,rec+seg->start,(size_t) seg->length);
|
|
key+=seg->length;
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
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;
|
|
}
|