~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_handler.c

Cleanup around SAFEMALLOC

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; version 2 of the License.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
1
/* Copyright (C) 2002-2006 MySQL AB
 
2
   
 
3
   This library is free software; you can redistribute it and/or
 
4
   modify it under the terms of the GNU Library General Public
 
5
   License as published by the Free Software Foundation; version 2
 
6
   of the License.
 
7
   
 
8
   This library is distributed in the hope that it will be useful,
 
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
   Library General Public License for more details.
 
12
 
 
13
   You should have received a copy of the GNU Library General Public
 
14
   License along with this library; if not, write to the Free
 
15
   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
16
   MA 02111-1307, USA */
 
17
 
 
18
#include "mysys_priv.h"
 
19
 
 
20
#include <mystrings/m_ctype.h>
 
21
#include <drizzled/base.h>
 
22
#include <my_handler.h>
 
23
#include <my_sys.h>
 
24
 
 
25
#include "my_handler_errors.h"
 
26
 
 
27
/**
 
28
  Swap the contents of two variables.
18
29
 */
19
 
 
20
 
#include "config.h"
21
 
 
22
 
#include "drizzled/charset_info.h"
23
 
#include <drizzled/base.h>
24
 
#include <plugin/myisam/my_handler.h>
25
 
#include "drizzled/internal/my_sys.h"
26
 
 
27
 
#include <cassert>
28
 
#include <algorithm>
29
 
 
30
 
using namespace drizzled;
31
 
using namespace std;
32
 
 
33
 
template<class T>
34
 
int CMP_NUM(const T& a, const T&b)
35
 
{
36
 
  return (a < b) ? -1 : (a == b) ? 0 : 1;
37
 
}
38
 
 
39
 
 
40
 
int ha_compare_text(const CHARSET_INFO * const charset_info, unsigned char *a, uint32_t a_length,
41
 
                    unsigned char *b, uint32_t b_length, bool part_key,
 
30
#define swap_variables(TYPE, a, b) \
 
31
  do {                             \
 
32
    TYPE dummy;                    \
 
33
    dummy= a;                      \
 
34
    a= b;                          \
 
35
    b= dummy;                      \
 
36
  } while (0)
 
37
 
 
38
int ha_compare_text(const CHARSET_INFO * const charset_info, uchar *a, uint a_length,
 
39
                    uchar *b, uint b_length, bool part_key,
42
40
                    bool skip_end_space)
43
41
{
44
42
  if (!part_key)
49
47
}
50
48
 
51
49
 
52
 
static int compare_bin(unsigned char *a, uint32_t a_length, unsigned char *b, uint32_t b_length,
 
50
static int compare_bin(uchar *a, uint a_length, uchar *b, uint b_length,
53
51
                       bool part_key, bool skip_end_space)
54
52
{
55
 
  uint32_t length= min(a_length,b_length);
56
 
  unsigned char *end= a+ length;
 
53
  uint length= min(a_length,b_length);
 
54
  uchar *end= a+ length;
57
55
  int flag;
58
56
 
59
57
  while (a < end)
104
102
    next_flag   How keys should be compared
105
103
                If bit SEARCH_FIND is not set the keys includes the row
106
104
                position and this should also be compared
107
 
    diff_pos    OUT Number of first keypart where values differ, counting
 
105
    diff_pos    OUT Number of first keypart where values differ, counting 
108
106
                from one.
109
107
    diff_pos[1] OUT  (b + diff_pos[1]) points to first value in tuple b
110
108
                      that is different from corresponding value in tuple a.
111
 
 
112
 
  EXAMPLES
 
109
  
 
110
  EXAMPLES 
113
111
   Example1: if the function is called for tuples
114
112
     ('aaa','bbb') and ('eee','fff'), then
115
113
     diff_pos[0] = 1 (as 'aaa' != 'eee')
132
130
 
133
131
#define FCMP(A,B) ((int) (A) - (int) (B))
134
132
 
135
 
int ha_key_cmp(register HA_KEYSEG *keyseg, register unsigned char *a,
136
 
               register unsigned char *b, uint32_t key_length, uint32_t nextflag,
137
 
               uint32_t *diff_pos)
 
133
int ha_key_cmp(register HA_KEYSEG *keyseg, register uchar *a,
 
134
               register uchar *b, uint key_length, uint nextflag,
 
135
               uint *diff_pos)
138
136
{
139
137
  int flag;
 
138
  int16_t s_1,s_2;
140
139
  int32_t l_1,l_2;
141
140
  uint32_t u_1,u_2;
 
141
  float f_1,f_2;
142
142
  double d_1,d_2;
143
 
  uint32_t next_key_length;
144
 
  unsigned char *orig_b= b;
 
143
  uint next_key_length;
 
144
  uchar *orig_b= b;
145
145
 
146
146
  *diff_pos=0;
147
147
  for ( ; (int) key_length >0 ; key_length=next_key_length, keyseg++)
148
148
  {
149
 
    unsigned char *end;
150
 
    uint32_t piks=! (keyseg->flag & HA_NO_SORT);
 
149
    uchar *end;
 
150
    uint piks=! (keyseg->flag & HA_NO_SORT);
151
151
    (*diff_pos)++;
152
152
    diff_pos[1]= (uint)(b - orig_b);
153
153
 
178
178
        continue;                               /* To next key part */
179
179
      }
180
180
    }
181
 
    end= a+ min((uint32_t)keyseg->length,key_length);
 
181
    end= a+ min(keyseg->length,key_length);
182
182
    next_key_length=key_length-keyseg->length;
183
183
 
184
184
    switch ((enum ha_base_keytype) keyseg->type) {
202
202
      }
203
203
      else
204
204
      {
205
 
        uint32_t length=(uint) (end-a), a_length=length, b_length=length;
 
205
        uint length=(uint) (end-a), a_length=length, b_length=length;
206
206
        if (piks &&
207
207
            (flag= ha_compare_text(keyseg->charset, a, a_length, b, b_length,
208
208
                                   (bool) ((nextflag & SEARCH_PREFIX) &&
214
214
      }
215
215
      break;
216
216
    case HA_KEYTYPE_BINARY:
 
217
    case HA_KEYTYPE_BIT:
217
218
      if (keyseg->flag & HA_SPACE_PACK)
218
219
      {
219
220
        int a_length,b_length,pack_length;
232
233
      }
233
234
      else
234
235
      {
235
 
        uint32_t length=keyseg->length;
 
236
        uint length=keyseg->length;
236
237
        if (piks &&
237
238
            (flag=compare_bin(a,length,b,length,
238
239
                              (bool) ((nextflag & SEARCH_PREFIX) &&
264
265
        b+= b_length;
265
266
        break;
266
267
      }
 
268
      break;
267
269
    case HA_KEYTYPE_VARBINARY1:
268
270
    case HA_KEYTYPE_VARBINARY2:
269
271
      {
281
283
        b+=b_length;
282
284
        break;
283
285
      }
 
286
      break;
 
287
    case HA_KEYTYPE_INT8:
 
288
    {
 
289
      int i_1= (int) *((signed char*) a);
 
290
      int i_2= (int) *((signed char*) b);
 
291
      if (piks && (flag = CMP_NUM(i_1,i_2)))
 
292
        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
 
293
      a= end;
 
294
      b++;
 
295
      break;
 
296
    }
 
297
    case HA_KEYTYPE_SHORT_INT:
 
298
      s_1= mi_sint2korr(a);
 
299
      s_2= mi_sint2korr(b);
 
300
      if (piks && (flag = CMP_NUM(s_1,s_2)))
 
301
        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
 
302
      a=  end;
 
303
      b+= 2; /* sizeof(short int); */
 
304
      break;
 
305
    case HA_KEYTYPE_USHORT_INT:
 
306
      {
 
307
        uint16_t us_1,us_2;
 
308
        us_1= mi_sint2korr(a);
 
309
        us_2= mi_sint2korr(b);
 
310
        if (piks && (flag = CMP_NUM(us_1,us_2)))
 
311
          return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
 
312
        a=  end;
 
313
        b+=2; /* sizeof(short int); */
 
314
        break;
 
315
      }
284
316
    case HA_KEYTYPE_LONG_INT:
285
317
      l_1= mi_sint4korr(a);
286
318
      l_2= mi_sint4korr(b);
297
329
      a=  end;
298
330
      b+= 4; /* sizeof(long int); */
299
331
      break;
 
332
    case HA_KEYTYPE_INT24:
 
333
      l_1=mi_sint3korr(a);
 
334
      l_2=mi_sint3korr(b);
 
335
      if (piks && (flag = CMP_NUM(l_1,l_2)))
 
336
        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
 
337
      a=  end;
 
338
      b+= 3;
 
339
      break;
300
340
    case HA_KEYTYPE_UINT24:
301
341
      l_1=mi_uint3korr(a);
302
342
      l_2=mi_uint3korr(b);
305
345
      a=  end;
306
346
      b+= 3;
307
347
      break;
 
348
    case HA_KEYTYPE_FLOAT:
 
349
      mi_float4get(f_1,a);
 
350
      mi_float4get(f_2,b);
 
351
      /*
 
352
        The following may give a compiler warning about floating point
 
353
        comparison not being safe, but this is ok in this context as
 
354
        we are bascily doing sorting
 
355
      */
 
356
      if (piks && (flag = CMP_NUM(f_1,f_2)))
 
357
        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
 
358
      a=  end;
 
359
      b+= 4; /* sizeof(float); */
 
360
      break;
308
361
    case HA_KEYTYPE_DOUBLE:
309
362
      mi_float8get(d_1,a);
310
363
      mi_float8get(d_2,b);
318
371
      a=  end;
319
372
      b+= 8;  /* sizeof(double); */
320
373
      break;
 
374
    case HA_KEYTYPE_NUM:                                /* Numeric key */
 
375
    {
 
376
      int swap_flag= 0;
 
377
      int alength,blength;
 
378
 
 
379
      if (keyseg->flag & HA_REVERSE_SORT)
 
380
      {
 
381
        swap_variables(uchar*, a, b);
 
382
        swap_flag=1;                            /* Remember swap of a & b */
 
383
        end= a+ (int) (end-b);
 
384
      }
 
385
      if (keyseg->flag & HA_SPACE_PACK)
 
386
      {
 
387
        alength= *a++; blength= *b++;
 
388
        end=a+alength;
 
389
        next_key_length=key_length-blength-1;
 
390
      }
 
391
      else
 
392
      {
 
393
        alength= (int) (end-a);
 
394
        blength=keyseg->length;
 
395
        /* remove pre space from keys */
 
396
        for ( ; alength && *a == ' ' ; a++, alength--) ;
 
397
        for ( ; blength && *b == ' ' ; b++, blength--) ;
 
398
      }
 
399
      if (piks)
 
400
      {
 
401
        if (*a == '-')
 
402
        {
 
403
          if (*b != '-')
 
404
            return -1;
 
405
          a++; b++;
 
406
          swap_variables(uchar*, a, b);
 
407
          swap_variables(int, alength, blength);
 
408
          swap_flag=1-swap_flag;
 
409
          alength--; blength--;
 
410
          end=a+alength;
 
411
        }
 
412
        else if (*b == '-')
 
413
          return 1;
 
414
        while (alength && (*a == '+' || *a == '0'))
 
415
        {
 
416
          a++; alength--;
 
417
        }
 
418
        while (blength && (*b == '+' || *b == '0'))
 
419
        {
 
420
          b++; blength--;
 
421
        }
 
422
        if (alength != blength)
 
423
          return (alength < blength) ? -1 : 1;
 
424
        while (a < end)
 
425
          if (*a++ !=  *b++)
 
426
            return ((int) a[-1] - (int) b[-1]);
 
427
      }
 
428
      else
 
429
      {
 
430
        b+=(end-a);
 
431
        a=end;
 
432
      }
 
433
 
 
434
      if (swap_flag)                            /* Restore pointers */
 
435
        swap_variables(uchar*, a, b);
 
436
      break;
 
437
    }
321
438
    case HA_KEYTYPE_LONGLONG:
322
439
    {
323
440
      int64_t ll_a,ll_b;
348
465
end:
349
466
  if (!(nextflag & SEARCH_FIND))
350
467
  {
351
 
    uint32_t i;
 
468
    uint i;
352
469
    if (nextflag & (SEARCH_NO_FIND | SEARCH_LAST)) /* Find record after key */
353
470
      return (nextflag & (SEARCH_BIGGER | SEARCH_LAST)) ? -1 : 1;
354
471
    flag=0;
391
508
    NULLs.
392
509
*/
393
510
 
394
 
HA_KEYSEG *ha_find_null(HA_KEYSEG *keyseg, unsigned char *a)
 
511
HA_KEYSEG *ha_find_null(HA_KEYSEG *keyseg, uchar *a)
395
512
{
396
513
  for (; (enum ha_base_keytype) keyseg->type != HA_KEYTYPE_END; keyseg++)
397
514
  {
398
 
    unsigned char *end;
 
515
    uchar *end;
399
516
    if (keyseg->null_bit)
400
517
    {
401
518
      if (!*a++)
406
523
    switch ((enum ha_base_keytype) keyseg->type) {
407
524
    case HA_KEYTYPE_TEXT:
408
525
    case HA_KEYTYPE_BINARY:
 
526
    case HA_KEYTYPE_BIT:
409
527
      if (keyseg->flag & HA_SPACE_PACK)
410
528
      {
411
529
        int a_length;
426
544
        a+= a_length;
427
545
        break;
428
546
      }
 
547
    case HA_KEYTYPE_NUM:
 
548
      if (keyseg->flag & HA_SPACE_PACK)
 
549
      {
 
550
        int alength= *a++;
 
551
        end= a+alength;
 
552
      }
 
553
      a= end;
 
554
      break;
 
555
    case HA_KEYTYPE_INT8:
 
556
    case HA_KEYTYPE_SHORT_INT:
 
557
    case HA_KEYTYPE_USHORT_INT:
429
558
    case HA_KEYTYPE_LONG_INT:
430
559
    case HA_KEYTYPE_ULONG_INT:
 
560
    case HA_KEYTYPE_INT24:
431
561
    case HA_KEYTYPE_UINT24:
432
562
    case HA_KEYTYPE_LONGLONG:
433
563
    case HA_KEYTYPE_ULONGLONG:
 
564
    case HA_KEYTYPE_FLOAT:
434
565
    case HA_KEYTYPE_DOUBLE:
435
566
      a= end;
436
567
      break;
437
 
    case HA_KEYTYPE_END:
 
568
    case HA_KEYTYPE_END:                        /* purecov: inspected */
438
569
      /* keep compiler happy */
439
570
      assert(0);
440
571
      break;
445
576
 
446
577
 
447
578
 
 
579
/*
 
580
  Register handler error messages for usage with my_error()
 
581
 
 
582
  NOTES
 
583
    This is safe to call multiple times as my_error_register()
 
584
    will ignore calls to register already registered error numbers.
 
585
*/
 
586
 
 
587
 
 
588
void my_handler_error_register(void)
 
589
{
 
590
  /*
 
591
    If you got compilation error here about compile_time_assert array, check
 
592
    that every HA_ERR_xxx constant has a corresponding error message in
 
593
    handler_error_messages[] list (check mysys/ma_handler_errors.h and
 
594
    include/my_base.h).
 
595
  */
 
596
  compile_time_assert(HA_ERR_FIRST + array_elements(handler_error_messages) ==
 
597
                      HA_ERR_LAST + 1);
 
598
  my_error_register(handler_error_messages, HA_ERR_FIRST,
 
599
                    HA_ERR_FIRST+ array_elements(handler_error_messages)-1);
 
600
}
 
601
 
 
602
 
 
603
void my_handler_error_unregister(void)
 
604
{
 
605
  my_error_unregister(HA_ERR_FIRST,
 
606
                      HA_ERR_FIRST+ array_elements(handler_error_messages)-1);
 
607
}