~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2006 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
/* Functions to check if a row is unique */
17
18
#include "myisamdef.h"
212.5.18 by Monty Taylor
Moved m_ctype, m_string and my_bitmap. Removed t_ctype.
19
#include <mystrings/m_ctype.h>
1 by brian
clean slate
20
481 by Brian Aker
Remove all of uchar.
21
bool mi_check_unique(MI_INFO *info, MI_UNIQUEDEF *def, unsigned char *record,
1 by brian
clean slate
22
			ha_checksum unique_hash, my_off_t disk_pos)
23
{
24
  my_off_t lastpos=info->lastpos;
25
  MI_KEYDEF *key= &info->s->keyinfo[def->key];
481 by Brian Aker
Remove all of uchar.
26
  unsigned char *key_buff=info->lastkey2;
1 by brian
clean slate
27
28
  mi_unique_store(record+key->seg->start, unique_hash);
29
  _mi_make_key(info,def->key,key_buff,record,0);
30
31
  /* The above changed info->lastkey2. Inform mi_rnext_same(). */
32
  info->update&= ~HA_STATE_RNEXT_SAME;
33
34
  if (_mi_search(info,info->s->keyinfo+def->key,key_buff,MI_UNIQUE_HASH_LENGTH,
35
		 SEARCH_FIND,info->s->state.key_root[def->key]))
36
  {
37
    info->page_changed=1;			/* Can't optimize read next */
38
    info->lastpos= lastpos;
51.1.114 by Jay Pipes
DBUG symbol removal
39
    return(0);				/* No matching rows */
1 by brian
clean slate
40
  }
41
42
  for (;;)
43
  {
44
    if (info->lastpos != disk_pos &&
45
	!(*info->s->compare_unique)(info,def,record,info->lastpos))
46
    {
47
      my_errno=HA_ERR_FOUND_DUPP_UNIQUE;
48
      info->errkey= (int) def->key;
49
      info->dupp_key_pos= info->lastpos;
50
      info->page_changed=1;			/* Can't optimize read next */
51
      info->lastpos=lastpos;
51.1.114 by Jay Pipes
DBUG symbol removal
52
      return(1);				/* Found identical  */
1 by brian
clean slate
53
    }
54
    if (_mi_search_next(info,info->s->keyinfo+def->key, info->lastkey,
55
			MI_UNIQUE_HASH_LENGTH, SEARCH_BIGGER,
56
			info->s->state.key_root[def->key]) ||
212.6.12 by Mats Kindahl
Removing redundant use of casts in MyISAM storage for memcmp(), memcpy(), memset(), and memmove().
57
	memcmp(info->lastkey, key_buff, MI_UNIQUE_HASH_LENGTH))
1 by brian
clean slate
58
    {
59
      info->page_changed=1;			/* Can't optimize read next */
60
      info->lastpos=lastpos;
51.1.114 by Jay Pipes
DBUG symbol removal
61
      return(0);				/* end of tree */
1 by brian
clean slate
62
    }
63
  }
64
}
65
66
67
/*
68
  Calculate a hash for a row
69
70
  TODO
71
    Add support for bit fields
72
*/
73
481 by Brian Aker
Remove all of uchar.
74
ha_checksum mi_unique_hash(MI_UNIQUEDEF *def, const unsigned char *record)
1 by brian
clean slate
75
{
481 by Brian Aker
Remove all of uchar.
76
  const unsigned char *pos, *end;
1 by brian
clean slate
77
  ha_checksum crc= 0;
290 by Brian Aker
Update for ulong change over.
78
  uint32_t seed1=0, seed2= 4;
1 by brian
clean slate
79
  HA_KEYSEG *keyseg;
80
81
  for (keyseg=def->seg ; keyseg < def->end ; keyseg++)
82
  {
83
    enum ha_base_keytype type=(enum ha_base_keytype) keyseg->type;
482 by Brian Aker
Remove uint.
84
    uint32_t length=keyseg->length;
1 by brian
clean slate
85
86
    if (keyseg->null_bit)
87
    {
88
      if (record[keyseg->null_pos] & keyseg->null_bit)
89
      {
90
	/*
91
	  Change crc in a way different from an empty string or 0.
92
	  (This is an optimisation;  The code will work even if this isn't
93
	  done)
94
	*/
95
	crc=((crc << 8) + 511+
96
	     (crc >> (8*sizeof(ha_checksum)-8)));
97
	continue;
98
      }
99
    }
100
    pos= record+keyseg->start;
101
    if (keyseg->flag & HA_VAR_LENGTH_PART)
102
    {
482 by Brian Aker
Remove uint.
103
      uint32_t pack_length=  keyseg->bit_start;
104
      uint32_t tmp_length= (pack_length == 1 ? (uint) *(unsigned char*) pos :
1 by brian
clean slate
105
                        uint2korr(pos));
106
      pos+= pack_length;			/* Skip VARCHAR length */
107
      set_if_smaller(length,tmp_length);
108
    }
109
    else if (keyseg->flag & HA_BLOB_PART)
110
    {
482 by Brian Aker
Remove uint.
111
      uint32_t tmp_length=_mi_calc_blob_length(keyseg->bit_start,pos);
212.6.12 by Mats Kindahl
Removing redundant use of casts in MyISAM storage for memcmp(), memcpy(), memset(), and memmove().
112
      memcpy(&pos,pos+keyseg->bit_start,sizeof(char*));
1 by brian
clean slate
113
      if (!length || length > tmp_length)
114
	length=tmp_length;			/* The whole blob */
115
    }
116
    end= pos+length;
117
    if (type == HA_KEYTYPE_TEXT || type == HA_KEYTYPE_VARTEXT1 ||
118
        type == HA_KEYTYPE_VARTEXT2)
119
    {
120
      keyseg->charset->coll->hash_sort(keyseg->charset,
481 by Brian Aker
Remove all of uchar.
121
                                       (const unsigned char*) pos, length, &seed1,
1 by brian
clean slate
122
                                       &seed2);
123
      crc^= seed1;
124
    }
125
    else
126
      while (pos != end)
127
	crc=((crc << 8) +
481 by Brian Aker
Remove all of uchar.
128
	     (((unsigned char)  *(unsigned char*) pos++))) +
1 by brian
clean slate
129
	  (crc >> (8*sizeof(ha_checksum)-8));
130
  }
131
  return crc;
132
}
133
134
135
/*
136
  compare unique key for two rows
137
138
  TODO
139
    Add support for bit fields
140
141
  RETURN
142
    0   if both rows have equal unique value
143
    #   Rows are different
144
*/
145
481 by Brian Aker
Remove all of uchar.
146
int mi_unique_comp(MI_UNIQUEDEF *def, const unsigned char *a, const unsigned char *b,
281 by Brian Aker
Converted myisam away from my_bool
147
		   bool null_are_equal)
1 by brian
clean slate
148
{
481 by Brian Aker
Remove all of uchar.
149
  const unsigned char *pos_a, *pos_b, *end;
1 by brian
clean slate
150
  HA_KEYSEG *keyseg;
151
152
  for (keyseg=def->seg ; keyseg < def->end ; keyseg++)
153
  {
154
    enum ha_base_keytype type=(enum ha_base_keytype) keyseg->type;
482 by Brian Aker
Remove uint.
155
    uint32_t a_length, b_length;
1 by brian
clean slate
156
    a_length= b_length= keyseg->length;
157
158
    /* If part is NULL it's regarded as different */
159
    if (keyseg->null_bit)
160
    {
482 by Brian Aker
Remove uint.
161
      uint32_t tmp;
1 by brian
clean slate
162
      if ((tmp=(a[keyseg->null_pos] & keyseg->null_bit)) !=
163
	  (uint) (b[keyseg->null_pos] & keyseg->null_bit))
164
	return 1;
165
      if (tmp)
166
      {
167
	if (!null_are_equal)
168
	  return 1;
169
	continue;
170
      }
171
    }
172
    pos_a= a+keyseg->start;
173
    pos_b= b+keyseg->start;
174
    if (keyseg->flag & HA_VAR_LENGTH_PART)
175
    {
482 by Brian Aker
Remove uint.
176
      uint32_t pack_length= keyseg->bit_start;
1 by brian
clean slate
177
      if (pack_length == 1)
178
      {
481 by Brian Aker
Remove all of uchar.
179
        a_length= (uint) *(unsigned char*) pos_a++;
180
        b_length= (uint) *(unsigned char*) pos_b++;
1 by brian
clean slate
181
      }
182
      else
183
      {
184
        a_length= uint2korr(pos_a);
185
        b_length= uint2korr(pos_b);
186
        pos_a+= 2;				/* Skip VARCHAR length */
187
        pos_b+= 2;
188
      }
189
      set_if_smaller(a_length, keyseg->length); /* Safety */
190
      set_if_smaller(b_length, keyseg->length); /* safety */
191
    }
192
    else if (keyseg->flag & HA_BLOB_PART)
193
    {
194
      /* Only compare 'length' characters if length != 0 */
195
      a_length= _mi_calc_blob_length(keyseg->bit_start,pos_a);
196
      b_length= _mi_calc_blob_length(keyseg->bit_start,pos_b);
197
      /* Check that a and b are of equal length */
198
      if (keyseg->length)
199
      {
200
        /*
201
          This is used in some cases when we are not interested in comparing
202
          the whole length of the blob.
203
        */
204
        set_if_smaller(a_length, keyseg->length);
205
        set_if_smaller(b_length, keyseg->length);
206
      }
212.6.12 by Mats Kindahl
Removing redundant use of casts in MyISAM storage for memcmp(), memcpy(), memset(), and memmove().
207
      memcpy(&pos_a,pos_a+keyseg->bit_start,sizeof(char*));
208
      memcpy(&pos_b,pos_b+keyseg->bit_start,sizeof(char*));
1 by brian
clean slate
209
    }
210
    if (type == HA_KEYTYPE_TEXT || type == HA_KEYTYPE_VARTEXT1 ||
211
        type == HA_KEYTYPE_VARTEXT2)
212
    {
481 by Brian Aker
Remove all of uchar.
213
      if (ha_compare_text(keyseg->charset, (unsigned char *) pos_a, a_length,
214
                                           (unsigned char *) pos_b, b_length, 0, 1))
1 by brian
clean slate
215
        return 1;
216
    }
217
    else
218
    {
219
      if (a_length != b_length)
220
        return 1;
221
      end= pos_a+a_length;
222
      while (pos_a != end)
223
      {
224
	if (*pos_a++ != *pos_b++)
225
	  return 1;
226
      }
227
    }
228
  }
229
  return 0;
230
}