18 #include "myisamdef.h"
22 ha_checksum unique_hash, my_off_t disk_pos)
24 my_off_t lastpos=info->lastpos;
25 MI_KEYDEF *key= &info->s->keyinfo[def->key];
26 uchar *key_buff=info->lastkey2;
27 DBUG_ENTER(
"mi_check_unique");
29 mi_unique_store(record+key->seg->start, unique_hash);
30 _mi_make_key(info,def->key,key_buff,record,0);
33 info->update&= ~HA_STATE_RNEXT_SAME;
35 if (_mi_search(info,info->s->keyinfo+def->key,key_buff,MI_UNIQUE_HASH_LENGTH,
36 SEARCH_FIND,info->s->state.key_root[def->key]))
39 info->lastpos= lastpos;
45 if (info->lastpos != disk_pos &&
46 !(*info->s->compare_unique)(info,def,record,info->lastpos))
48 my_errno=HA_ERR_FOUND_DUPP_UNIQUE;
49 info->errkey= (int) def->key;
50 info->dupp_key_pos= info->lastpos;
52 info->lastpos=lastpos;
53 DBUG_PRINT(
"info",(
"Found duplicate"));
56 if (_mi_search_next(info,info->s->keyinfo+def->key, info->lastkey,
57 MI_UNIQUE_HASH_LENGTH, SEARCH_BIGGER,
58 info->s->state.key_root[def->key]) ||
59 memcmp(info->lastkey, key_buff, MI_UNIQUE_HASH_LENGTH))
62 info->lastpos=lastpos;
76 ha_checksum mi_unique_hash(
MI_UNIQUEDEF *def,
const uchar *record)
78 const uchar *pos, *end;
80 ulong seed1=0, seed2= 4;
83 for (keyseg=def->seg ; keyseg < def->end ; keyseg++)
85 enum ha_base_keytype
type=(
enum ha_base_keytype) keyseg->type;
86 uint length=keyseg->length;
90 if (record[keyseg->null_pos] & keyseg->null_bit)
97 crc=((crc << 8) + 511+
98 (crc >> (8*
sizeof(ha_checksum)-8)));
102 pos= record+keyseg->start;
103 if (keyseg->flag & HA_VAR_LENGTH_PART)
105 uint pack_length= keyseg->bit_start;
106 uint tmp_length= (pack_length == 1 ? (uint) *(uchar*) pos :
109 set_if_smaller(length,tmp_length);
111 else if (keyseg->flag & HA_BLOB_PART)
113 uint tmp_length=_mi_calc_blob_length(keyseg->bit_start,pos);
114 memcpy(&pos, pos+keyseg->bit_start,
sizeof(
char*));
115 if (!length || length > tmp_length)
119 if (type == HA_KEYTYPE_TEXT || type == HA_KEYTYPE_VARTEXT1 ||
120 type == HA_KEYTYPE_VARTEXT2)
122 keyseg->charset->coll->hash_sort(keyseg->charset,
123 (
const uchar*) pos, length, &seed1,
130 (((uchar) *(uchar*) pos++))) +
131 (crc >> (8*
sizeof(ha_checksum)-8));
148 int mi_unique_comp(
MI_UNIQUEDEF *def,
const uchar *a,
const uchar *b,
149 my_bool null_are_equal)
151 const uchar *pos_a, *pos_b, *end;
154 for (keyseg=def->seg ; keyseg < def->end ; keyseg++)
156 enum ha_base_keytype type=(
enum ha_base_keytype) keyseg->type;
157 uint a_length, b_length;
158 a_length= b_length= keyseg->length;
161 if (keyseg->null_bit)
164 if ((tmp=(a[keyseg->null_pos] & keyseg->null_bit)) !=
165 (uint) (b[keyseg->null_pos] & keyseg->null_bit))
174 pos_a= a+keyseg->start;
175 pos_b= b+keyseg->start;
176 if (keyseg->flag & HA_VAR_LENGTH_PART)
178 uint pack_length= keyseg->bit_start;
179 if (pack_length == 1)
181 a_length= (uint) *(uchar*) pos_a++;
182 b_length= (uint) *(uchar*) pos_b++;
186 a_length= uint2korr(pos_a);
187 b_length= uint2korr(pos_b);
191 set_if_smaller(a_length, keyseg->length);
192 set_if_smaller(b_length, keyseg->length);
194 else if (keyseg->flag & HA_BLOB_PART)
197 a_length= _mi_calc_blob_length(keyseg->bit_start,pos_a);
198 b_length= _mi_calc_blob_length(keyseg->bit_start,pos_b);
206 set_if_smaller(a_length, keyseg->length);
207 set_if_smaller(b_length, keyseg->length);
209 memcpy(&pos_a, pos_a+keyseg->bit_start,
sizeof(
char*));
210 memcpy(&pos_b, pos_b+keyseg->bit_start,
sizeof(
char*));
212 if (type == HA_KEYTYPE_TEXT || type == HA_KEYTYPE_VARTEXT1 ||
213 type == HA_KEYTYPE_VARTEXT2)
215 if (ha_compare_text(keyseg->charset, (uchar *) pos_a, a_length,
216 (uchar *) pos_b, b_length, 0, 1))
221 if (a_length != b_length)
226 if (*pos_a++ != *pos_b++)