drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
mi_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/* Functions to handle keys */
17
18#include "myisam_priv.h"
19#include <drizzled/charset.h>
20#include <math.h>
21#include <cassert>
22
23using namespace drizzled;
24using namespace std;
25
26#define CHECK_KEYS /* Enable safety checks */
27
28#define FIX_LENGTH(cs, pos, length, char_length) \
29 do { \
30 if (length > char_length) \
31 char_length= my_charpos(cs, pos, pos+length, char_length); \
32 drizzled::set_if_smaller(char_length,length); \
33 } while(0)
34
35static int _mi_put_key_in_record(MI_INFO *info,uint32_t keynr,unsigned char *record);
36
37/*
38 Make a intern key from a record
39
40 SYNOPSIS
41 _mi_make_key()
42 info MyiSAM handler
43 keynr key number
44 key Store created key here
45 record Record
46 filepos Position to record in the data file
47
48 RETURN
49 Length of key
50*/
51
52uint32_t _mi_make_key(MI_INFO *info, uint32_t keynr, unsigned char *key,
53 const unsigned char *record, drizzled::internal::my_off_t filepos)
54{
55 unsigned char *pos;
56 unsigned char *start;
57 HA_KEYSEG *keyseg;
58
59 start=key;
60 for (keyseg=info->s->keyinfo[keynr].seg ; keyseg->type ;keyseg++)
61 {
62 enum drizzled::ha_base_keytype type=(enum drizzled::ha_base_keytype) keyseg->type;
63 uint32_t length=keyseg->length;
64 uint32_t char_length;
65 const drizzled::charset_info_st * const cs=keyseg->charset;
66
67 if (keyseg->null_bit)
68 {
69 if (record[keyseg->null_pos] & keyseg->null_bit)
70 {
71 *key++= 0; /* NULL in key */
72 continue;
73 }
74 *key++=1; /* Not NULL */
75 }
76
77 char_length= ((cs && cs->mbmaxlen > 1) ? length/cs->mbmaxlen :
78 length);
79
80 pos= (unsigned char*) record+keyseg->start;
81
82 if (keyseg->flag & HA_SPACE_PACK)
83 {
84 length= cs->cset->lengthsp(cs, (char*) pos, length);
85
86 FIX_LENGTH(cs, pos, length, char_length);
87 store_key_length_inc(key,char_length);
88 memcpy(key, pos, char_length);
89 key+=char_length;
90 continue;
91 }
92 if (keyseg->flag & HA_VAR_LENGTH_PART)
93 {
94 uint32_t pack_length= (keyseg->bit_start == 1 ? 1 : 2);
95 uint32_t tmp_length= (pack_length == 1 ? (uint) *(unsigned char*) pos :
96 uint2korr(pos));
97 pos+= pack_length; /* Skip VARCHAR length */
98 drizzled::set_if_smaller(length,tmp_length);
99 FIX_LENGTH(cs, pos, length, char_length);
100 store_key_length_inc(key,char_length);
101 memcpy(key, pos, char_length);
102 key+= char_length;
103 continue;
104 }
105 else if (keyseg->flag & HA_BLOB_PART)
106 {
107 uint32_t tmp_length=_mi_calc_blob_length(keyseg->bit_start,pos);
108 memcpy(&pos, pos+keyseg->bit_start, sizeof(char*));
109 drizzled::set_if_smaller(length,tmp_length);
110 FIX_LENGTH(cs, pos, length, char_length);
111 store_key_length_inc(key,char_length);
112 memcpy(key, pos, char_length);
113 key+= char_length;
114 continue;
115 }
116 else if (keyseg->flag & HA_SWAP_KEY)
117 { /* Numerical column */
118 if (type == drizzled::HA_KEYTYPE_DOUBLE)
119 {
120 double nr;
121 float8get(nr,pos);
122 if (isnan(nr))
123 {
124 memset(key, 0, length);
125 key+=length;
126 continue;
127 }
128 }
129 pos+=length;
130 while (length--)
131 {
132 *key++ = *--pos;
133 }
134 continue;
135 }
136 FIX_LENGTH(cs, pos, length, char_length);
137 memcpy(key, pos, char_length);
138 if (length > char_length)
139 cs->cset->fill(cs, (char*) key+char_length, length-char_length, ' ');
140 key+= length;
141 }
142 _mi_dpointer(info,key,filepos);
143 return((uint) (key-start)); /* Return keylength */
144} /* _mi_make_key */
145
146
147/*
148 Pack a key to intern format from given format (c_rkey)
149
150 SYNOPSIS
151 _mi_pack_key()
152 info MyISAM handler
153 uint32_t keynr key number
154 key Store packed key here
155 old Not packed key
156 keypart_map bitmap of used keyparts
157 last_used_keyseg out parameter. May be NULL
158
159 RETURN
160 length of packed key
161
162 last_use_keyseg Store pointer to the keyseg after the last used one
163*/
164
165uint32_t _mi_pack_key(MI_INFO *info, uint32_t keynr, unsigned char *key, unsigned char *old,
166 drizzled::key_part_map keypart_map, HA_KEYSEG **last_used_keyseg)
167{
168 unsigned char *start_key=key;
169 HA_KEYSEG *keyseg;
170
171 /* only key prefixes are supported */
172 assert(((keypart_map+1) & keypart_map) == 0);
173
174 for (keyseg= info->s->keyinfo[keynr].seg ; keyseg->type && keypart_map;
175 old+= keyseg->length, keyseg++)
176 {
177 enum drizzled::ha_base_keytype type= (enum drizzled::ha_base_keytype) keyseg->type;
178 uint32_t length= keyseg->length;
179 uint32_t char_length;
180 unsigned char *pos;
181 const drizzled::charset_info_st * const cs=keyseg->charset;
182 keypart_map>>= 1;
183 if (keyseg->null_bit)
184 {
185 if (!(*key++= (char) 1-*old++)) /* Copy null marker */
186 {
187 if (keyseg->flag & (HA_VAR_LENGTH_PART | HA_BLOB_PART))
188 old+= 2;
189 continue; /* Found NULL */
190 }
191 }
192 char_length= (cs && cs->mbmaxlen > 1) ? length/cs->mbmaxlen : length;
193 pos=old;
194 if (keyseg->flag & HA_SPACE_PACK)
195 {
196 unsigned char *end=pos+length;
197
198 if (type != drizzled::HA_KEYTYPE_BINARY)
199 {
200 while (end > pos && end[-1] == ' ')
201 end--;
202 }
203 length=(uint) (end-pos);
204 FIX_LENGTH(cs, pos, length, char_length);
205 store_key_length_inc(key,char_length);
206 memcpy(key, pos, char_length);
207 key+= char_length;
208 continue;
209 }
210 else if (keyseg->flag & (HA_VAR_LENGTH_PART | HA_BLOB_PART))
211 {
212 /* Length of key-part used with mi_rkey() always 2 */
213 uint32_t tmp_length=uint2korr(pos);
214 pos+=2;
215 drizzled::set_if_smaller(length,tmp_length); /* Safety */
216 FIX_LENGTH(cs, pos, length, char_length);
217 store_key_length_inc(key,char_length);
218 old+=2; /* Skip length */
219 memcpy(key, pos, char_length);
220 key+= char_length;
221 continue;
222 }
223 else if (keyseg->flag & HA_SWAP_KEY)
224 { /* Numerical column */
225 pos+=length;
226 while (length--)
227 *key++ = *--pos;
228 continue;
229 }
230 FIX_LENGTH(cs, pos, length, char_length);
231 memcpy(key, pos, char_length);
232 if (length > char_length)
233 cs->cset->fill(cs, (char*) key+char_length, length-char_length, ' ');
234 key+= length;
235 }
236 if (last_used_keyseg)
237 *last_used_keyseg= keyseg;
238
239 return((uint) (key-start_key));
240} /* _mi_pack_key */
241
242
243
244/*
245 Store found key in record
246
247 SYNOPSIS
248 _mi_put_key_in_record()
249 info MyISAM handler
250 keynr Key number that was used
251 record Store key here
252
253 Last read key is in info->lastkey
254
255 NOTES
256 Used when only-keyread is wanted
257
258 RETURN
259 0 ok
260 1 error
261*/
262
263static int _mi_put_key_in_record(MI_INFO *info, uint32_t keynr,
264 unsigned char *record)
265{
266 unsigned char *key;
267 unsigned char *pos,*key_end;
268 HA_KEYSEG *keyseg;
269 unsigned char *blob_ptr;
270
271 blob_ptr= (unsigned char*) info->lastkey2; /* Place to put blob parts */
272 key=(unsigned char*) info->lastkey; /* KEy that was read */
273 key_end=key+info->lastkey_length;
274 for (keyseg=info->s->keyinfo[keynr].seg ; keyseg->type ;keyseg++)
275 {
276 if (keyseg->null_bit)
277 {
278 if (!*key++)
279 {
280 record[keyseg->null_pos]|= keyseg->null_bit;
281 continue;
282 }
283 record[keyseg->null_pos]&= ~keyseg->null_bit;
284 }
285
286 if (keyseg->flag & HA_SPACE_PACK)
287 {
288 uint32_t length;
289 get_key_length(length,key);
290#ifdef CHECK_KEYS
291 if (length > keyseg->length || key+length > key_end)
292 goto err;
293#endif
294 pos= record+keyseg->start;
295
296 memcpy(pos, key, length);
297 keyseg->charset->cset->fill(keyseg->charset,
298 (char*) pos + length,
299 keyseg->length - length,
300 ' ');
301 key+=length;
302 continue;
303 }
304
305 if (keyseg->flag & HA_VAR_LENGTH_PART)
306 {
307 uint32_t length;
308 get_key_length(length,key);
309#ifdef CHECK_KEYS
310 if (length > keyseg->length || key+length > key_end)
311 goto err;
312#endif
313 /* Store key length */
314 if (keyseg->bit_start == 1)
315 *(unsigned char*) (record+keyseg->start)= (unsigned char) length;
316 else
317 int2store(record+keyseg->start, length);
318 /* And key data */
319 memcpy(record+keyseg->start + keyseg->bit_start, key, length);
320 key+= length;
321 }
322 else if (keyseg->flag & HA_BLOB_PART)
323 {
324 uint32_t length;
325 get_key_length(length,key);
326#ifdef CHECK_KEYS
327 if (length > keyseg->length || key+length > key_end)
328 goto err;
329#endif
330 memcpy(record+keyseg->start+keyseg->bit_start,
331 &blob_ptr,sizeof(char*));
332 memcpy(blob_ptr,key,length);
333 blob_ptr+=length;
334
335 /* The above changed info->lastkey2. Inform mi_rnext_same(). */
336 info->update&= ~HA_STATE_RNEXT_SAME;
337
338 _my_store_blob_length(record+keyseg->start,
339 (uint) keyseg->bit_start,length);
340 key+=length;
341 }
342 else if (keyseg->flag & HA_SWAP_KEY)
343 {
344 unsigned char *to= record+keyseg->start+keyseg->length;
345 unsigned char *end= key+keyseg->length;
346#ifdef CHECK_KEYS
347 if (end > key_end)
348 goto err;
349#endif
350 do
351 {
352 *--to= *key++;
353 } while (key != end);
354 continue;
355 }
356 else
357 {
358#ifdef CHECK_KEYS
359 if (key+keyseg->length > key_end)
360 goto err;
361#endif
362 memcpy(record+keyseg->start, key, keyseg->length);
363 key+= keyseg->length;
364 }
365 }
366 return(0);
367
368err:
369 return(1); /* Crashed row */
370} /* _mi_put_key_in_record */
371
372
373 /* Here when key reads are used */
374
375int _mi_read_key_record(MI_INFO *info, drizzled::internal::my_off_t filepos, unsigned char *buf)
376{
377 fast_mi_writeinfo(info);
378 if (filepos != HA_OFFSET_ERROR)
379 {
380 if (info->lastinx >= 0)
381 { /* Read only key */
382 if (_mi_put_key_in_record(info,(uint) info->lastinx,buf))
383 {
384 mi_print_error(info->s, HA_ERR_CRASHED);
385 errno=HA_ERR_CRASHED;
386 return -1;
387 }
388 info->update|= HA_STATE_AKTIV; /* We should find a record */
389 return 0;
390 }
391 errno=HA_ERR_WRONG_INDEX;
392 }
393 return(-1); /* Wrong data to read */
394}
395
396
397/*
398 Save current key tuple to record and call index condition check function
399
400 SYNOPSIS
401 mi_check_index_cond()
402 info MyISAM handler
403 keynr Index we're running a scan on
404 record Record buffer to use (it is assumed that index check function
405 will look for column values there)
406
407 RETURN
408 -1 Error
409 0 Index condition is not satisfied, continue scanning
410 1 Index condition is satisfied
411 2 Index condition is not satisfied, end the scan.
412*/
413
414int mi_check_index_cond(MI_INFO *info, uint32_t keynr, unsigned char *record)
415{
416 if (_mi_put_key_in_record(info, keynr, record))
417 {
418 mi_print_error(info->s, HA_ERR_CRASHED);
419 errno=HA_ERR_CRASHED;
420 return -1;
421 }
422 return info->index_cond_func(info->index_cond_func_arg);
423}
424
425
426/*
427 Retrieve auto_increment info
428
429 SYNOPSIS
430 retrieve_auto_increment()
431 info MyISAM handler
432 record Row to update
433
434 IMPLEMENTATION
435 For signed columns we don't retrieve the auto increment value if it's
436 less than zero.
437*/
438
439uint64_t retrieve_auto_increment(MI_INFO *info,const unsigned char *record)
440{
441 uint64_t value= 0; /* Store unsigned values here */
442 int64_t s_value= 0; /* Store signed values here */
443 HA_KEYSEG *keyseg= info->s->keyinfo[info->s->base.auto_key-1].seg;
444 const unsigned char *key= (unsigned char*) record + keyseg->start;
445
446 switch (keyseg->type) {
447 case drizzled::HA_KEYTYPE_BINARY:
448 value=(uint64_t) *(unsigned char*) key;
449 break;
450 case drizzled::HA_KEYTYPE_LONG_INT:
451 s_value= (int64_t) sint4korr(key);
452 break;
453 case drizzled::HA_KEYTYPE_ULONG_INT:
454 value=(uint64_t) uint4korr(key);
455 break;
456 case drizzled::HA_KEYTYPE_DOUBLE: /* This shouldn't be used */
457 {
458 double f_1;
459 float8get(f_1,key);
460 /* Ignore negative values */
461 value = (f_1 < 0.0) ? 0 : (uint64_t) f_1;
462 break;
463 }
464 case drizzled::HA_KEYTYPE_LONGLONG:
465 s_value= sint8korr(key);
466 break;
467 case drizzled::HA_KEYTYPE_ULONGLONG:
468 value= uint8korr(key);
469 break;
470 default:
471 assert(0);
472 value=0; /* Error */
473 break;
474 }
475
476 /*
477 The following code works becasue if s_value < 0 then value is 0
478 and if s_value == 0 then value will contain either s_value or the
479 correct value.
480 */
481 return (s_value > 0) ? (uint64_t) s_value : value;
482}
Convert from decimal to binary representation.
Definition common_fwd.h:215
TODO: Rename this file - func.h is stupid.
STL namespace.