1 /* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
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, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
22 
23 #ifndef _my_compare_h
24 #define _my_compare_h
25 
26 #include "myisampack.h"
27 #ifdef	__cplusplus
28 extern "C" {
29 #endif
30 
31 #include "m_ctype.h"                            /* CHARSET_INFO */
32 
33 /*
34   There is a hard limit for the maximum number of keys as there are only
35   8 bits in the index file header for the number of keys in a table.
36   This means that 0..255 keys can exist for a table. The idea of
37   HA_MAX_POSSIBLE_KEY is to ensure that one can use myisamchk & tools on
38   a MyISAM table for which one has more keys than MyISAM is normally
39   compiled for. If you don't have this, you will get a core dump when
40   running myisamchk compiled for 128 keys on a table with 255 keys.
41 */
42 
43 #define HA_MAX_POSSIBLE_KEY         255         /* For myisamchk */
44 /*
45   The following defines can be increased if necessary.
46   But beware the dependency of MI_MAX_POSSIBLE_KEY_BUFF and HA_MAX_KEY_LENGTH.
47 */
48 
49 #define HA_MAX_KEY_LENGTH           4000        /* Max length in bytes */
50 #define HA_MAX_KEY_SEG              16          /* Max segments for key */
51 
52 #define HA_MAX_POSSIBLE_KEY_BUFF    (HA_MAX_KEY_LENGTH + 24+ 6+6)
53 #define HA_MAX_KEY_BUFF  (HA_MAX_KEY_LENGTH+HA_MAX_KEY_SEG*6+8+8)
54 
55 typedef struct st_HA_KEYSEG		/* Key-portion */
56 {
57   const CHARSET_INFO *charset;
58   uint32 start;				/* Start of key in record */
59   uint32 null_pos;			/* position to NULL indicator */
60   uint16 bit_pos;                       /* Position to bit part */
61   uint16 flag;
62   uint16 length;			/* Keylength */
63   uint16 language;
64   uint8  type;				/* Type of key (for sort) */
65   uint8  null_bit;			/* bitmask to test for NULL */
66   uint8  bit_start,bit_end;		/* if bit field */
67   uint8  bit_length;                    /* Length of bit part */
68 } HA_KEYSEG;
69 
70 #define get_key_length(length,key) \
71 { if (*(uchar*) (key) != 255) \
72     length= (uint) *(uchar*) ((key)++); \
73   else \
74   { length= mi_uint2korr((key)+1); (key)+=3; } \
75 }
76 
77 #define get_key_length_rdonly(length,key) \
78 { if (*(uchar*) (key) != 255) \
79     length= ((uint) *(uchar*) ((key))); \
80   else \
81   { length= mi_uint2korr((key)+1); } \
82 }
83 
84 #define get_key_pack_length(length,length_pack,key) \
85 { if (*(uchar*) (key) != 255) \
86   { length= (uint) *(uchar*) ((key)++); length_pack= 1; }\
87   else \
88   { length=mi_uint2korr((key)+1); (key)+= 3; length_pack= 3; } \
89 }
90 
91 #define store_key_length_inc(key,length) \
92 { if ((length) < 255) \
93   { *(key)++= (length); } \
94   else \
95   { *(key)=255; mi_int2store((key)+1,(length)); (key)+=3; } \
96 }
97 
98 #define size_to_store_key_length(length) ((length) < 255 ? 1 : 3)
99 
100 #define get_rec_bits(bit_ptr, bit_ofs, bit_len) \
101   (((((uint16) (bit_ptr)[1] << 8) | (uint16) (bit_ptr)[0]) >> (bit_ofs)) & \
102    ((1 << (bit_len)) - 1))
103 
104 #define set_rec_bits(bits, bit_ptr, bit_ofs, bit_len) \
105 { \
106   (bit_ptr)[0]= ((bit_ptr)[0] & ~(((1 << (bit_len)) - 1) << (bit_ofs))) | \
107                 ((bits) << (bit_ofs)); \
108   if ((bit_ofs) + (bit_len) > 8) \
109     (bit_ptr)[1]= ((bit_ptr)[1] & ~((1 << ((bit_len) - 8 + (bit_ofs))) - 1)) | \
110                   ((bits) >> (8 - (bit_ofs))); \
111 }
112 
113 #define clr_rec_bits(bit_ptr, bit_ofs, bit_len) \
114   set_rec_bits(0, bit_ptr, bit_ofs, bit_len)
115 
116 extern int ha_compare_text(const CHARSET_INFO *, uchar *, uint, uchar *, uint ,
117 			   my_bool, my_bool);
118 extern int ha_key_cmp(register HA_KEYSEG *keyseg, register uchar *a,
119 		      register uchar *b, uint key_length, uint nextflag,
120 		      uint *diff_pos);
121 
122 /*
123   Inside an in-memory data record, memory pointers to pieces of the
124   record (like BLOBs) are stored in their native byte order and in
125   this amount of bytes.
126 */
127 #define portable_sizeof_char_ptr 8
128 
129 
130 /**
131   Return values of index_cond_func_xxx functions.
132 
133   0=ICP_NO_MATCH  - index tuple doesn't satisfy the pushed index condition (the
134                 engine should discard the tuple and go to the next one)
135   1=ICP_MATCH     - index tuple satisfies the pushed index condition (the engine
136                 should fetch and return the record)
137   2=ICP_OUT_OF_RANGE - index tuple is out range that we're scanning, e.g. this
138                    if we're scanning "t.key BETWEEN 10 AND 20" and got a
139                    "t.key=21" tuple (the engine should stop scanning and return
140                    HA_ERR_END_OF_FILE right away).
141 */
142 
143 typedef enum icp_result {
144   ICP_NO_MATCH,
145   ICP_MATCH,
146   ICP_OUT_OF_RANGE
147 } ICP_RESULT;
148 
149 
150 #ifdef	__cplusplus
151 }
152 #endif
153 
154 #endif /* _my_compare_h */
155