drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
drizzled/key.cc
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15
16
17/* Functions to handle keys and fields in forms */
18
19#include <config.h>
20#include <drizzled/table.h>
21#include <drizzled/key.h>
22#include <drizzled/field/blob.h>
23#include <drizzled/util/test.h>
24#include <drizzled/plugin/storage_engine.h>
25#include <drizzled/key_part_info.h>
26
27#include <boost/dynamic_bitset.hpp>
28
29#include <string>
30
31#include <algorithm>
32
33using namespace std;
34
35namespace drizzled
36{
37
38/*
39 Search after a key that starts with 'field'
40
41 SYNOPSIS
42 find_ref_key()
43 key First key to check
44 key_count How many keys to check
45 record Start of record
46 field Field to search after
47 key_length On partial match, contains length of fields before
48 field
49 keypart key part # of a field
50
51 NOTES
52 Used when calculating key for NEXT_NUMBER
53
54 IMPLEMENTATION
55 If no key starts with field test if field is part of some key. If we find
56 one, then return first key and set key_length to the number of bytes
57 preceding 'field'.
58
59 RETURN
60 -1 field is not part of the key
61 # Key part for key matching key.
62 key_length is set to length of key before (not including) field
63*/
64
65int find_ref_key(KeyInfo *key, uint32_t key_count, unsigned char *record, Field *field,
66 uint32_t *key_length, uint32_t *keypart)
67{
68 int i;
69 KeyInfo *key_info;
70 uint32_t fieldpos;
71
72 fieldpos= field->offset(record);
73
74 /* Test if some key starts as fieldpos */
75 for (i= 0, key_info= key ;
76 i < (int) key_count ;
77 i++, key_info++)
78 {
79 if (key_info->key_part[0].offset == fieldpos)
80 { /* Found key. Calc keylength */
81 *key_length= *keypart= 0;
82 return i; /* Use this key */
83 }
84 }
85
86 /* Test if some key contains fieldpos */
87 for (i= 0, key_info= key;
88 i < (int) key_count ;
89 i++, key_info++)
90 {
91 uint32_t j;
92 KeyPartInfo *key_part;
93 *key_length=0;
94 for (j=0, key_part=key_info->key_part ;
95 j < key_info->key_parts ;
96 j++, key_part++)
97 {
98 if (key_part->offset == fieldpos)
99 {
100 *keypart= j;
101 return i; /* Use this key */
102 }
103 *key_length+= key_part->store_length;
104 }
105 }
106 return(-1); /* No key is ok */
107}
108
109
110void key_copy(unsigned char *to_key, unsigned char *from_record, KeyInfo *key_info,
111 unsigned int key_length)
112{
113 uint32_t length;
114 KeyPartInfo *key_part;
115
116 if (key_length == 0)
117 key_length= key_info->key_length;
118 for (key_part= key_info->key_part; (int) key_length > 0; key_part++)
119 {
120 if (key_part->null_bit)
121 {
122 *to_key++= test(from_record[key_part->null_offset] &
123 key_part->null_bit);
124 key_length--;
125 }
126 if (key_part->key_part_flag & HA_BLOB_PART ||
127 key_part->key_part_flag & HA_VAR_LENGTH_PART)
128 {
129 key_length-= HA_KEY_BLOB_LENGTH;
130 length= min((uint16_t)key_length, key_part->length);
131 key_part->field->get_key_image(to_key, length);
132 to_key+= HA_KEY_BLOB_LENGTH;
133 }
134 else
135 {
136 length= min((uint16_t)key_length, key_part->length);
137 Field *field= key_part->field;
138 const charset_info_st * const cs= field->charset();
139 uint32_t bytes= field->get_key_image(to_key, length);
140 if (bytes < length)
141 cs->cset->fill(cs, (char*) to_key + bytes, length - bytes, ' ');
142 }
143 to_key+= length;
144 key_length-= length;
145 }
146}
147
148
152
153void key_zero_nulls(unsigned char *tuple, KeyInfo *key_info)
154{
155 KeyPartInfo *key_part= key_info->key_part;
156 KeyPartInfo *key_part_end= key_part + key_info->key_parts;
157 for (; key_part != key_part_end; key_part++)
158 {
159 if (key_part->null_bit && *tuple)
160 memset(tuple+1, 0, key_part->store_length-1);
161 tuple+= key_part->store_length;
162 }
163}
164
165
166/*
167 Restore a key from some buffer to record.
168
169 This function converts a key into record format. It can be used in cases
170 when we want to return a key as a result row.
171
172 @param to_record record buffer where the key will be restored to
173 @param from_key buffer that contains a key
174 @param key_info descriptor of the index
175 @param key_length specifies length of all keyparts that will be restored
176*/
177
178void key_restore(unsigned char *to_record, unsigned char *from_key, KeyInfo *key_info,
179 uint16_t key_length)
180{
181 uint32_t length;
182 KeyPartInfo *key_part;
183
184 if (key_length == 0)
185 {
186 key_length= key_info->key_length;
187 }
188 for (key_part= key_info->key_part ; (int) key_length > 0 ; key_part++)
189 {
190 unsigned char used_uneven_bits= 0;
191 if (key_part->null_bit)
192 {
193 if (*from_key++)
194 to_record[key_part->null_offset]|= key_part->null_bit;
195 else
196 to_record[key_part->null_offset]&= ~key_part->null_bit;
197 key_length--;
198 }
199 if (key_part->key_part_flag & HA_BLOB_PART)
200 {
201 /*
202 This in fact never happens, as we have only partial BLOB
203 keys yet anyway, so it's difficult to find any sence to
204 restore the part of a record.
205 Maybe this branch is to be removed, but now we
206 have to ignore GCov compaining.
207
208 This may make more sense once we push down block lengths to the engine (aka partial retrieval).
209 */
210 uint32_t blob_length= uint2korr(from_key);
211 Field_blob *field= (Field_blob*) key_part->field;
212
213 field->setReadSet();
214 from_key+= HA_KEY_BLOB_LENGTH;
215 key_length-= HA_KEY_BLOB_LENGTH;
216 field->set_ptr_offset(to_record - field->getTable()->getInsertRecord(),
217 (ulong) blob_length, from_key);
218 length= key_part->length;
219 }
220 else if (key_part->key_part_flag & HA_VAR_LENGTH_PART)
221 {
222 Field *field= key_part->field;
223 ptrdiff_t ptrdiff= to_record - field->getTable()->getInsertRecord();
224
225 field->setReadSet();
226 field->setWriteSet();
227 field->move_field_offset(ptrdiff);
228 key_length-= HA_KEY_BLOB_LENGTH;
229 length= min(key_length, key_part->length);
230 field->set_key_image(from_key, length);
231 from_key+= HA_KEY_BLOB_LENGTH;
232 field->move_field_offset(-ptrdiff);
233 }
234 else
235 {
236 length= min(key_length, key_part->length);
237 /* skip the byte with 'uneven' bits, if used */
238 memcpy(to_record + key_part->offset, from_key + used_uneven_bits
239 , (size_t) length - used_uneven_bits);
240 }
241 from_key+= length;
242 key_length-= length;
243 }
244}
245
246
266
267bool key_cmp_if_same(Table *table,const unsigned char *key,uint32_t idx,uint32_t key_length)
268{
269 uint32_t store_length;
270 KeyPartInfo *key_part;
271 const unsigned char *key_end= key + key_length;;
272
273 for (key_part=table->key_info[idx].key_part;
274 key < key_end ;
275 key_part++, key+= store_length)
276 {
277 uint32_t length;
278 store_length= key_part->store_length;
279
280 if (key_part->null_bit)
281 {
282 if (*key != test(table->getInsertRecord()[key_part->null_offset] &
283 key_part->null_bit))
284 return 1;
285 if (*key)
286 continue;
287 key++;
288 store_length--;
289 }
290 if (key_part->key_part_flag & (HA_BLOB_PART | HA_VAR_LENGTH_PART |
291 HA_BIT_PART))
292 {
293 if (key_part->field->key_cmp(key, key_part->length))
294 return 1;
295 continue;
296 }
297 length= min((uint32_t) (key_end-key), store_length);
298 if (key_part->field->type() == DRIZZLE_TYPE_VARCHAR)
299 {
300 const charset_info_st * const cs= key_part->field->charset();
301 uint32_t char_length= key_part->length / cs->mbmaxlen;
302 const unsigned char *pos= table->getInsertRecord() + key_part->offset;
303 if (length > char_length)
304 {
305 char_length= my_charpos(cs, pos, pos + length, char_length);
306 set_if_smaller(char_length, length);
307 }
308 if (cs->coll->strnncollsp(cs,
309 (const unsigned char*) key, length,
310 (const unsigned char*) pos, char_length, 0))
311 return 1;
312 continue;
313 }
314 if (memcmp(key,table->getInsertRecord()+key_part->offset,length))
315 return 1;
316 }
317 return 0;
318}
319
320/*
321 unpack key-fields from record to some buffer.
322
323 This is used mainly to get a good error message. We temporary
324 change the column bitmap so that all columns are readable.
325
326 @param
327 to Store value here in an easy to read form
328 @param
329 table Table to use
330 @param
331 idx Key number
332*/
333
334void key_unpack(String *to, const Table *table, uint32_t idx)
335{
336 KeyPartInfo *key_part,*key_part_end;
337 Field *field;
338 String tmp;
339
340 to->length(0);
341 for (key_part=table->key_info[idx].key_part,key_part_end=key_part+
342 table->key_info[idx].key_parts ;
343 key_part < key_part_end;
344 key_part++)
345 {
346 if (to->length())
347 to->append('-');
348 if (key_part->null_bit)
349 {
350 if (table->getInsertRecord()[key_part->null_offset] & key_part->null_bit)
351 {
352 to->append(STRING_WITH_LEN("NULL"));
353 continue;
354 }
355 }
356 if ((field= key_part->field))
357 {
358 const charset_info_st * const cs= field->charset();
359 field->setReadSet();
360 field->val_str_internal(&tmp);
361 if (cs->mbmaxlen > 1 &&
362 table->getField(key_part->fieldnr - 1)->field_length !=
363 key_part->length)
364 {
365 /*
366 Prefix key, multi-byte charset.
367 For the columns of type CHAR(N), the above val_str()
368 call will return exactly "key_part->length" bytes,
369 which can break a multi-byte characters in the middle.
370 Align, returning not more than "char_length" characters.
371 */
372 uint32_t charpos, char_length= key_part->length / cs->mbmaxlen;
373 if ((charpos= my_charpos(cs, tmp.c_ptr(), tmp.c_ptr() + tmp.length(), char_length)) < key_part->length)
374 tmp.length(charpos);
375 }
376
377 if (key_part->length < field->pack_length())
378 tmp.length(min(tmp.length(), static_cast<size_t>(key_part->length)));
379 to->append(tmp);
380 }
381 else
382 to->append(STRING_WITH_LEN("???"));
383 }
384}
385
386
387/*
388 Check if key uses field that is marked in passed field bitmap.
389
390 SYNOPSIS
391 is_key_used()
392 table Table object with which keys and fields are associated.
393 idx Key to be checked.
394 fields Bitmap of fields to be checked.
395
396 NOTE
397 This function uses Table::tmp_set bitmap so the caller should care
398 about saving/restoring its state if it also uses this bitmap.
399
400 RETURN VALUE
401 TRUE Key uses field from bitmap
402 FALSE Otherwise
403*/
404
405bool is_key_used(Table *table, uint32_t idx, const boost::dynamic_bitset<>& fields)
406{
407 table->tmp_set.reset();
408 table->mark_columns_used_by_index_no_reset(idx, table->tmp_set);
409 if (table->tmp_set.is_subset_of(fields))
410 return 1;
411
412 /*
413 If table handler has primary key as part of the index, check that primary
414 key is not updated
415 */
416 if (idx != table->getShare()->getPrimaryKey() && table->getShare()->hasPrimaryKey() &&
417 (table->cursor->getEngine()->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX)))
418 {
419 return is_key_used(table, table->getShare()->getPrimaryKey(), fields);
420 }
421 return 0;
422}
423
424
438
439int key_cmp(KeyPartInfo *key_part, const unsigned char *key, uint32_t key_length)
440{
441 uint32_t store_length;
442
443 for (const unsigned char *end=key + key_length;
444 key < end;
445 key+= store_length, key_part++)
446 {
447 int cmp;
448 store_length= key_part->store_length;
449 if (key_part->null_bit)
450 {
451 /* This key part allows null values; NULL is lower than everything */
452 bool field_is_null= key_part->field->is_null();
453 if (*key) // If range key is null
454 {
455 /* the range is expecting a null value */
456 if (!field_is_null)
457 return 1; // Found key is > range
458 /* null -- exact match, go to next key part */
459 continue;
460 }
461 else if (field_is_null)
462 return -1; // NULL is less than any value
463 key++; // Skip null byte
464 store_length--;
465 }
466 if ((cmp=key_part->field->key_cmp(key, key_part->length)) < 0)
467 return -1;
468 if (cmp > 0)
469 return 1;
470 }
471 return 0; // Keys are equal
472}
473
474
475} /* namespace drizzled */
TODO: Rename this file - func.h is stupid.
void key_zero_nulls(unsigned char *tuple, KeyInfo *key_info)
static void store_length(unsigned char *to, uint32_t length, uint32_t pack_length)
Definition filesort.cc:755
int key_cmp(KeyPartInfo *key_part, const unsigned char *key, uint32_t key_length)
bool key_cmp_if_same(Table *table, const unsigned char *key, uint32_t idx, uint32_t key_length)
STL namespace.