~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/pbxt/src/myxt_xt.cc

  • Committer: Mark Atwood
  • Date: 2011-08-12 04:08:33 UTC
  • mfrom: (2385.2.17 refactor5)
  • Revision ID: me@mark.atwood.name-20110812040833-u6j85nc6ahuc0dtz
mergeĀ lp:~olafvdspek/drizzle/refactor5

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2005 PrimeBase Technologies GmbH
2
 
 *
3
 
 * PrimeBase XT
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License as published by
7
 
 * the Free Software Foundation; either version 2 of the License, or
8
 
 * (at your option) any later version.
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
18
 
 *
19
 
 * 2006-05-16   Paul McCullagh
20
 
 *
21
 
 * H&G2JCtL
22
 
 *
23
 
 * These functions implement the parts of PBXT which must conform to the
24
 
 * key and row format used by MySQL. 
25
 
 */
26
 
 
27
 
#include "xt_config.h"
28
 
#include "xt_defs.h"
29
 
 
30
 
#ifdef DRIZZLED
31
 
#include <drizzled/internal/my_pthread.h>
32
 
#include <drizzled/plugin.h>
33
 
#include <drizzled/plugin/client.h>
34
 
#include <drizzled/plugin/null_client.h>
35
 
#include <drizzled/plugin/listen.h>
36
 
#include <drizzled/show.h>
37
 
#include <drizzled/data_home.h>
38
 
#include <drizzled/field/blob.h>
39
 
#include <drizzled/field/enum.h>
40
 
#include <drizzled/field/varstring.h>
41
 
#include <drizzled/current_session.h>
42
 
#include <drizzled/sql_lex.h>
43
 
#include <drizzled/session.h>
44
 
#include <drizzled/charset_info.h>
45
 
#include <plugin/myisam/my_handler.h>
46
 
#include <plugin/myisam/myisampack.h>
47
 
#include <boost/filesystem.hpp>
48
 
//extern "C" struct charset_info_st *session_charset(Session *session);
49
 
extern pthread_key_t THR_Session;
50
 
 
51
 
namespace fs=boost::filesystem;
52
 
using namespace drizzled;
53
 
#else
54
 
#include "mysql_priv.h"
55
 
#include <mysql/plugin.h>
56
 
#endif
57
 
 
58
 
#ifdef HAVE_ISNAN
59
 
#include <math.h>
60
 
#endif
61
 
#include <strings.h>
62
 
 
63
 
#include "ha_pbxt.h"
64
 
 
65
 
#include "myxt_xt.h"
66
 
#include "strutil_xt.h"
67
 
#include "database_xt.h"
68
 
#include "cache_xt.h"
69
 
#include "datalog_xt.h"
70
 
#include "memory_xt.h"
71
 
 
72
 
static void             myxt_bitmap_init(XTThreadPtr self, MX_BITMAP *map, u_int n_bits);
73
 
static void             myxt_bitmap_free(XTThreadPtr self, MX_BITMAP *map);
74
 
 
75
 
#ifdef DRIZZLED
76
 
#define swap_variables(TYPE, a, b) \
77
 
  do {                             \
78
 
    TYPE dummy;                    \
79
 
    dummy= a;                      \
80
 
    a= b;                          \
81
 
    b= dummy;                      \
82
 
  } while (0)
83
 
 
84
 
 
85
 
#define CMP_NUM(a,b) (((a) < (b)) ? -1 : ((a) == (b)) ? 0 : 1)
86
 
#else
87
 
#define get_rec_bits(bit_ptr, bit_ofs, bit_len) \
88
 
        (((((uint16) (bit_ptr)[1] << 8) | (uint16) (bit_ptr)[0]) >> (bit_ofs)) & \
89
 
   ((1 << (bit_len)) - 1))
90
 
#endif
91
 
 
92
 
#define FIX_LENGTH(cs, pos, length, char_length) \
93
 
                                                do { \
94
 
                                                        if ((length) > char_length) \
95
 
                                                                char_length= my_charpos(cs, pos, pos+length, char_length); \
96
 
                                                        set_if_smaller(char_length,length); \
97
 
                                                } while(0)
98
 
 
99
 
#ifdef store_key_length_inc
100
 
#undef store_key_length_inc
101
 
#endif
102
 
#define store_key_length_inc(key,length) \
103
 
{ if ((length) < 255) \
104
 
        { *(key)++=(length); } \
105
 
        else \
106
 
        { *(key)=255; mi_int2store((key)+1,(length)); (key)+=3; } \
107
 
}
108
 
 
109
 
#define set_rec_bits(bits, bit_ptr, bit_ofs, bit_len) \
110
 
{ \
111
 
        (bit_ptr)[0]= ((bit_ptr)[0] & ~(((1 << (bit_len)) - 1) << (bit_ofs))) | \
112
 
                ((bits) << (bit_ofs)); \
113
 
        if ((bit_ofs) + (bit_len) > 8) \
114
 
    (bit_ptr)[1]= ((bit_ptr)[1] & ~((1 << ((bit_len) - 8 + (bit_ofs))) - 1)) | \
115
 
                  ((bits) >> (8 - (bit_ofs))); \
116
 
}
117
 
 
118
 
#define clr_rec_bits(bit_ptr, bit_ofs, bit_len) \
119
 
        set_rec_bits(0, bit_ptr, bit_ofs, bit_len)
120
 
 
121
 
#ifdef DRIZZLED
122
 
static const char hexchars[]= "0123456789abcdef";
123
 
 
124
 
static bool tablename_to_filename(const char *from, char *to, size_t to_length)
125
 
{
126
 
 
127
 
  size_t length= 0;
128
 
  for (; *from  && length < to_length; length++, from++)
129
 
  {
130
 
    if ((*from >= '0' && *from <= '9') ||
131
 
        (*from >= 'A' && *from <= 'Z') ||
132
 
        (*from >= 'a' && *from <= 'z') ||
133
 
/* OSX defines an extra set of high-bit and multi-byte characters
134
 
   that cannot be used on the filesystem. Instead of trying to sort
135
 
   those out, we'll just escape encode all high-bit-set chars on OSX.
136
 
   It won't really hurt anything - it'll just make some filenames ugly. */
137
 
#if !defined(TARGET_OS_OSX)
138
 
        ((unsigned char)*from >= 128) ||
139
 
#endif
140
 
        (*from == '_') ||
141
 
        (*from == ' ') ||
142
 
        (*from == '-'))
143
 
    {
144
 
      to[length]= *from;
145
 
      continue;
146
 
    }
147
 
 
148
 
    if (length + 3 >= to_length)
149
 
      return true;
150
 
 
151
 
    /* We need to escape this char in a way that can be reversed */
152
 
    to[length++]= '@';
153
 
    to[length++]= hexchars[(*from >> 4) & 15];
154
 
    to[length]= hexchars[(*from) & 15];
155
 
  }
156
 
 
157
 
  if (/*internal::check_if_legal_tablename(to) &&*/
158
 
      length + 4 < to_length)
159
 
  {
160
 
    memcpy(to + length, "@@@", 4);
161
 
    length+= 3;
162
 
  }
163
 
  return false;
164
 
}
165
 
#endif
166
 
 
167
 
static ulong my_calc_blob_length(uint length, xtWord1 *pos)
168
 
{
169
 
        switch (length) {
170
 
        case 1:
171
 
                return (uint) (uchar) *pos;
172
 
        case 2:
173
 
                return (uint) uint2korr(pos);
174
 
        case 3:
175
 
                return uint3korr(pos);
176
 
        case 4:
177
 
                return uint4korr(pos);
178
 
        default:
179
 
                break;
180
 
        }
181
 
        return 0; /* Impossible */
182
 
}
183
 
 
184
 
static void my_store_blob_length(byte *pos,uint pack_length,uint length)
185
 
{
186
 
        switch (pack_length) {
187
 
        case 1:
188
 
                *pos= (uchar) length;
189
 
                break;
190
 
        case 2:
191
 
                int2store(pos,length);
192
 
                break;
193
 
        case 3:
194
 
                int3store(pos,length);
195
 
                break;
196
 
        case 4:
197
 
                int4store(pos,length);
198
 
        default:
199
 
                break;
200
 
        }
201
 
        return;
202
 
}
203
 
 
204
 
static int my_compare_text(MX_CONST_CHARSET_INFO *charset_info, uchar *a, uint a_length,
205
 
                                uchar *b, uint b_length, my_bool part_key,
206
 
                                my_bool XT_UNUSED(skip_end_space))
207
 
{
208
 
        if (!part_key)
209
 
                /* The last parameter is diff_if_only_endspace_difference, which means
210
 
                 * that end spaces are not ignored. We actually always want
211
 
                 * to ignore end spaces!
212
 
                 */
213
 
                return charset_info->coll->strnncollsp(charset_info, a, a_length,
214
 
                                b, b_length, /*(my_bool)!skip_end_space*/0);
215
 
        return charset_info->coll->strnncoll(charset_info, a, a_length,
216
 
                        b, b_length, part_key);
217
 
}
218
 
 
219
 
/*
220
 
 * -----------------------------------------------------------------------
221
 
 * Create a key
222
 
 */
223
 
 
224
 
/*
225
 
 * Derived from _mi_pack_key()
226
 
 */
227
 
xtPublic u_int myxt_create_key_from_key(XTIndexPtr ind, xtWord1 *key, xtWord1 *old, u_int k_length)
228
 
{
229
 
        xtWord1                 *start_key = key;
230
 
        XTIndexSegRec   *keyseg = ind->mi_seg;
231
 
 
232
 
        for (u_int i=0; i<ind->mi_seg_count && (int) k_length > 0; i++, old += keyseg->length, keyseg++)
233
 
        {
234
 
#ifndef DRIZZLED
235
 
                enum ha_base_keytype    type = (enum ha_base_keytype) keyseg->type;
236
 
#endif
237
 
                u_int                                   length = keyseg->length < k_length ? keyseg->length : k_length;
238
 
                u_int                                   char_length;
239
 
                xtWord1                                 *pos;
240
 
                MX_CONST_CHARSET_INFO   *cs = keyseg->charset;
241
 
 
242
 
                if (keyseg->null_bit) {
243
 
                        k_length--;
244
 
                        if (!(*key++ = (xtWord1) 1 - *old++)) {                                 /* Copy null marker */
245
 
                                k_length -= length;
246
 
                                if (keyseg->flag & (HA_VAR_LENGTH_PART | HA_BLOB_PART)) {
247
 
                                        k_length -= 2;                                                                  /* Skip length */
248
 
                                        old += 2;
249
 
                                }
250
 
                                continue;                                                                                       /* Found NULL */
251
 
                        }
252
 
                }
253
 
                char_length= (cs && cs->mbmaxlen > 1) ? length/cs->mbmaxlen : length;
254
 
                pos = old;
255
 
                if (keyseg->flag & HA_SPACE_PACK) {
256
 
                        uchar *end = pos + length;
257
 
#ifndef DRIZZLED
258
 
                        if (type != HA_KEYTYPE_NUM) {
259
 
#endif
260
 
                                while (end > pos && end[-1] == ' ')
261
 
                                        end--;
262
 
#ifndef DRIZZLED
263
 
                        }
264
 
                        else {
265
 
                                while (pos < end && pos[0] == ' ')
266
 
                                        pos++;
267
 
                        }
268
 
#endif
269
 
                        k_length -= length;
270
 
                        length = (u_int) (end-pos);
271
 
                        FIX_LENGTH(cs, pos, length, char_length);
272
 
                        store_key_length_inc(key, char_length);
273
 
                        memcpy((byte*) key,pos,(size_t) char_length);
274
 
                        key += char_length;
275
 
                        continue;
276
 
                }
277
 
                if (keyseg->flag & (HA_VAR_LENGTH_PART | HA_BLOB_PART)) {
278
 
                        /* Length of key-part used with mi_rkey() always 2 */
279
 
                        u_int tmp_length = uint2korr(pos);
280
 
                        k_length -= 2 + length;
281
 
                        pos += 2;
282
 
                        set_if_smaller(length, tmp_length);     /* Safety */
283
 
                        FIX_LENGTH(cs, pos, length, char_length);
284
 
                        store_key_length_inc(key,char_length);
285
 
                        old +=2;                                        /* Skip length */
286
 
                        memcpy((char *) key, pos, (size_t) char_length);
287
 
                        key += char_length;
288
 
                        continue;
289
 
                }
290
 
                if (keyseg->flag & HA_SWAP_KEY)
291
 
                {                                               /* Numerical column */
292
 
                        pos+=length;
293
 
                        k_length-=length;
294
 
                        while (length--) {
295
 
                                *key++ = *--pos;
296
 
                        }
297
 
                        continue;
298
 
                }
299
 
                FIX_LENGTH(cs, pos, length, char_length);
300
 
                memcpy((byte*) key, pos, char_length);
301
 
                if (length > char_length)
302
 
                        cs->cset->fill(cs, (char *) (key + char_length), length - char_length, ' ');
303
 
                key += length;
304
 
                k_length -= length;
305
 
        }
306
 
 
307
 
        return (u_int) (key - start_key);
308
 
}
309
 
 
310
 
/* Derived from _mi_make_key */
311
 
xtPublic u_int myxt_create_key_from_row(XTIndexPtr ind, xtWord1 *key, xtWord1 *record, xtBool *no_duplicate)
312
 
{
313
 
        register XTIndexSegRec  *keyseg = ind->mi_seg;
314
 
        xtWord1                                 *pos;
315
 
        xtWord1                                 *end;
316
 
        xtWord1                                 *start;
317
 
 
318
 
#ifdef HAVE_valgrind
319
 
       if (ind->mi_fix_key)
320
 
               memset((byte*) key, 0,(size_t) (ind->mi_key_size) );
321
 
#endif
322
 
 
323
 
#ifdef HAVE_valgrind
324
 
       if (ind->mi_fix_key)
325
 
               memset((byte*) key, 0,(size_t) (ind->mi_key_size) );
326
 
#endif
327
 
 
328
 
        start = key;
329
 
        for (u_int i=0; i<ind->mi_seg_count; i++, keyseg++)
330
 
        {
331
 
#ifndef DRIZZLED
332
 
                enum ha_base_keytype    type = (enum ha_base_keytype) keyseg->type;
333
 
#endif
334
 
                u_int                                   length = keyseg->length;
335
 
                u_int                                   char_length;
336
 
                MX_CONST_CHARSET_INFO   *cs = keyseg->charset;
337
 
 
338
 
                if (keyseg->null_bit) {
339
 
                        if (record[keyseg->null_pos] & keyseg->null_bit) {
340
 
                                *key++ = 0;                             /* NULL in key */
341
 
                                
342
 
                                /* The point is, if a key contains a NULL value
343
 
                                 * the duplicate checking must be disabled.
344
 
                                 * This is because a NULL value is not considered
345
 
                                 * equal to any other value.
346
 
                                 */ 
347
 
                                if (no_duplicate)
348
 
                                        *no_duplicate = FALSE;
349
 
                                continue;
350
 
                        }
351
 
                        *key++ = 1;                                     /* Not NULL */
352
 
                }
353
 
 
354
 
                char_length= ((cs && cs->mbmaxlen > 1) ? length/cs->mbmaxlen : length);
355
 
 
356
 
                pos = record + keyseg->start;
357
 
#ifndef DRIZZLED
358
 
                if (type == HA_KEYTYPE_BIT)
359
 
                {
360
 
                        if (keyseg->bit_length)
361
 
                        {
362
 
                                uchar bits = get_rec_bits((uchar*) record + keyseg->bit_pos,
363
 
                                                                                                                                 keyseg->bit_start, keyseg->bit_length);
364
 
                                *key++ = bits;
365
 
                                length--;
366
 
                        }
367
 
                        memcpy((byte*) key, pos, length);
368
 
                        key+= length;
369
 
                        continue;
370
 
                }
371
 
#endif
372
 
                if (keyseg->flag & HA_SPACE_PACK)
373
 
                {
374
 
                        end = pos + length;
375
 
#ifndef DRIZZLED
376
 
                        if (type != HA_KEYTYPE_NUM) {
377
 
#endif
378
 
                                while (end > pos && end[-1] == ' ')
379
 
                                        end--;
380
 
#ifndef DRIZZLED
381
 
                        }
382
 
                        else {
383
 
                                while (pos < end && pos[0] == ' ')
384
 
                                        pos++;
385
 
                        }
386
 
#endif
387
 
                        length = (u_int) (end-pos);
388
 
                        FIX_LENGTH(cs, pos, length, char_length);
389
 
                        store_key_length_inc(key,char_length);
390
 
                        memcpy((byte*) key,(byte*) pos,(size_t) char_length);
391
 
                        key += char_length;
392
 
                        continue;
393
 
                }
394
 
                if (keyseg->flag & HA_VAR_LENGTH_PART) {
395
 
                        uint pack_length= (keyseg->bit_start == 1 ? 1 : 2);
396
 
                        uint tmp_length= (pack_length == 1 ? (uint) *(uchar*) pos :
397
 
                                                                                                uint2korr(pos));
398
 
                        pos += pack_length;                     /* Skip VARCHAR length */
399
 
                        set_if_smaller(length,tmp_length);
400
 
                        FIX_LENGTH(cs, pos, length, char_length);
401
 
                        store_key_length_inc(key,char_length);
402
 
                        memcpy((byte*) key,(byte*) pos,(size_t) char_length);
403
 
                        key += char_length;
404
 
                        continue;
405
 
                }
406
 
                if (keyseg->flag & HA_BLOB_PART)
407
 
                {
408
 
                        u_int tmp_length = my_calc_blob_length(keyseg->bit_start, pos);
409
 
                        memcpy((byte*) &pos,pos+keyseg->bit_start,sizeof(char*));
410
 
                        set_if_smaller(length,tmp_length);
411
 
                        FIX_LENGTH(cs, pos, length, char_length);
412
 
                        store_key_length_inc(key,char_length);
413
 
                        memcpy((byte*) key,(byte*) pos,(size_t) char_length);
414
 
                        key+= char_length;
415
 
                        continue;
416
 
                }
417
 
                if (keyseg->flag & HA_SWAP_KEY)
418
 
                {                                               /* Numerical column */
419
 
#ifdef HAVE_ISNAN
420
 
#ifndef DRIZZLED
421
 
                        if (type == HA_KEYTYPE_FLOAT)
422
 
                        {
423
 
                                float nr;
424
 
                                float4get(nr,pos);
425
 
                                if (isnan(nr))
426
 
                                {
427
 
                                        /* Replace NAN with zero */
428
 
                                        bzero(key,length);
429
 
                                        key+=length;
430
 
                                        continue;
431
 
                                }
432
 
                        }
433
 
                        else 
434
 
#endif                  
435
 
                        if (type == HA_KEYTYPE_DOUBLE) {
436
 
                                double nr;
437
 
 
438
 
                                float8get(nr,pos);
439
 
                                if (isnan(nr)) {
440
 
                                        bzero(key,length);
441
 
                                        key+=length;
442
 
                                        continue;
443
 
                                }
444
 
                        }
445
 
#endif
446
 
                        pos+=length;
447
 
                        while (length--) {
448
 
                                *key++ = *--pos;
449
 
                        }
450
 
                        continue;
451
 
                }
452
 
                FIX_LENGTH(cs, pos, length, char_length);
453
 
                memcpy((byte*) key, pos, char_length);
454
 
                if (length > char_length)
455
 
                        cs->cset->fill(cs, (char *) key + char_length, length - char_length, ' ');
456
 
                key += length;
457
 
        }
458
 
 
459
 
        return ind->mi_fix_key ? ind->mi_key_size : (u_int) (key - start);              /* Return keylength */
460
 
}
461
 
 
462
 
xtPublic u_int myxt_create_foreign_key_from_row(XTIndexPtr ind, xtWord1 *key, xtWord1 *record, XTIndexPtr fkey_ind, xtBool *no_null)
463
 
{
464
 
        register XTIndexSegRec  *keyseg = ind->mi_seg;
465
 
        register XTIndexSegRec  *fkey_keyseg = fkey_ind->mi_seg;
466
 
        xtWord1                                 *pos;
467
 
        xtWord1                                 *end;
468
 
        xtWord1                                 *start;
469
 
 
470
 
        start = key;
471
 
        for (u_int i=0; i<ind->mi_seg_count; i++, keyseg++, fkey_keyseg++)
472
 
        {
473
 
#ifndef DRIZZLED
474
 
                enum ha_base_keytype    type = (enum ha_base_keytype) keyseg->type;
475
 
#endif
476
 
                u_int                                   length = keyseg->length;
477
 
                u_int                                   char_length;
478
 
                MX_CONST_CHARSET_INFO   *cs = keyseg->charset;
479
 
                xtBool                                  is_null = FALSE;
480
 
 
481
 
                if (keyseg->null_bit) {
482
 
                        if (record[keyseg->null_pos] & keyseg->null_bit) {
483
 
                                is_null = TRUE;
484
 
                                if (no_null)
485
 
                                        *no_null = FALSE;
486
 
                        }
487
 
                }
488
 
 
489
 
                if (fkey_keyseg->null_bit) {
490
 
                        if (is_null) {
491
 
                                *key++ = 0;                             /* NULL in key */
492
 
                                
493
 
                                /* The point is, if a key contains a NULL value
494
 
                                 * the duplicate checking must be disabled.
495
 
                                 * This is because a NULL value is not considered
496
 
                                 * equal to any other value.
497
 
                                 */ 
498
 
                                continue;
499
 
                        }
500
 
                        *key++ = 1;                                     /* Not NULL */
501
 
                }
502
 
 
503
 
                char_length= ((cs && cs->mbmaxlen > 1) ? length/cs->mbmaxlen : length);
504
 
 
505
 
                pos = record + keyseg->start;
506
 
#ifndef DRIZZLED
507
 
                if (type == HA_KEYTYPE_BIT)
508
 
                {
509
 
                        if (keyseg->bit_length)
510
 
                        {
511
 
                                uchar bits = get_rec_bits((uchar*) record + keyseg->bit_pos,
512
 
                                                                                                                                 keyseg->bit_start, keyseg->bit_length);
513
 
                                *key++ = bits;
514
 
                                length--;
515
 
                        }
516
 
                        memcpy((byte*) key, pos, length);
517
 
                        key+= length;
518
 
                        continue;
519
 
                }
520
 
#endif
521
 
                if (keyseg->flag & HA_SPACE_PACK)
522
 
                {
523
 
                        end = pos + length;
524
 
#ifndef DRIZZLED
525
 
                        if (type != HA_KEYTYPE_NUM) {
526
 
#endif
527
 
                                while (end > pos && end[-1] == ' ')
528
 
                                        end--;
529
 
#ifndef DRIZZLED
530
 
                        }
531
 
                        else {
532
 
                                while (pos < end && pos[0] == ' ')
533
 
                                        pos++;
534
 
                        }
535
 
#endif
536
 
                        length = (u_int) (end-pos);
537
 
                        FIX_LENGTH(cs, pos, length, char_length);
538
 
                        store_key_length_inc(key,char_length);
539
 
                        memcpy((byte*) key,(byte*) pos,(size_t) char_length);
540
 
                        key += char_length;
541
 
                        continue;
542
 
                }
543
 
                if (keyseg->flag & HA_VAR_LENGTH_PART) {
544
 
                        uint pack_length= (keyseg->bit_start == 1 ? 1 : 2);
545
 
                        uint tmp_length= (pack_length == 1 ? (uint) *(uchar*) pos :
546
 
                                                                                                uint2korr(pos));
547
 
                        pos += pack_length;                     /* Skip VARCHAR length */
548
 
                        set_if_smaller(length,tmp_length);
549
 
                        FIX_LENGTH(cs, pos, length, char_length);
550
 
                        store_key_length_inc(key,char_length);
551
 
                        memcpy((byte*) key,(byte*) pos,(size_t) char_length);
552
 
                        key += char_length;
553
 
                        continue;
554
 
                }
555
 
                if (keyseg->flag & HA_BLOB_PART)
556
 
                {
557
 
                        u_int tmp_length = my_calc_blob_length(keyseg->bit_start, pos);
558
 
                        memcpy((byte*) &pos,pos+keyseg->bit_start,sizeof(char*));
559
 
                        set_if_smaller(length,tmp_length);
560
 
                        FIX_LENGTH(cs, pos, length, char_length);
561
 
                        store_key_length_inc(key,char_length);
562
 
                        memcpy((byte*) key,(byte*) pos,(size_t) char_length);
563
 
                        key+= char_length;
564
 
                        continue;
565
 
                }
566
 
                if (keyseg->flag & HA_SWAP_KEY)
567
 
                {                                               /* Numerical column */
568
 
#ifdef HAVE_ISNAN
569
 
#ifndef DRIZZLED
570
 
                        if (type == HA_KEYTYPE_FLOAT)
571
 
                        {
572
 
                                float nr;
573
 
                                float4get(nr,pos);
574
 
                                if (isnan(nr))
575
 
                                {
576
 
                                        /* Replace NAN with zero */
577
 
                                        bzero(key,length);
578
 
                                        key+=length;
579
 
                                        continue;
580
 
                                }
581
 
                        }
582
 
                        else 
583
 
#endif
584
 
                        if (type == HA_KEYTYPE_DOUBLE) {
585
 
                                double nr;
586
 
 
587
 
                                float8get(nr,pos);
588
 
                                if (isnan(nr)) {
589
 
                                        bzero(key,length);
590
 
                                        key+=length;
591
 
                                        continue;
592
 
                                }
593
 
                        }
594
 
#endif
595
 
                        pos+=length;
596
 
                        while (length--) {
597
 
                                *key++ = *--pos;
598
 
                        }
599
 
                        continue;
600
 
                }
601
 
                FIX_LENGTH(cs, pos, length, char_length);
602
 
                memcpy((byte*) key, pos, char_length);
603
 
                if (length > char_length)
604
 
                        cs->cset->fill(cs, (char *) key + char_length, length - char_length, ' ');
605
 
                key += length;
606
 
        }
607
 
 
608
 
        return (u_int) (key - start);
609
 
}
610
 
 
611
 
/* I may be overcautious here, but can I assume that
612
 
 * null_ptr refers to my buffer. If I cannot, then I
613
 
 * cannot use the set_notnull() method.
614
 
 */
615
 
static void mx_set_notnull_in_record(STRUCT_TABLE *table, Field *field, char *record)
616
 
{
617
 
        if (field->null_ptr)
618
 
                record[(uint) (field->null_ptr - (uchar *) table->getDefaultValues())] &= (uchar) ~field->null_bit;
619
 
}
620
 
 
621
 
static xtBool mx_is_null_in_record(STRUCT_TABLE *table, Field *field, char *record)
622
 
{
623
 
        if (field->null_ptr) {
624
 
                if (record[(uint) (field->null_ptr - (uchar *) table->getDefaultValues())] & (uchar) field->null_bit)
625
 
                        return TRUE;
626
 
        }
627
 
        return FALSE;
628
 
}
629
 
 
630
 
/*
631
 
 * PBXT uses a completely different disk format to MySQL so I need a
632
 
 * method that just returns the byte length and
633
 
 * pointer to the data in a row.
634
 
 */
635
 
static char *mx_get_length_and_data(STRUCT_TABLE *table, Field *field, char *dest, xtWord4 *len)
636
 
{
637
 
        char *from;
638
 
        
639
 
        from = dest + field->offset(table->getDefaultValues());
640
 
        switch (field->real_type()) {
641
 
#ifndef DRIZZLED
642
 
                case MYSQL_TYPE_TINY_BLOB:
643
 
                case MYSQL_TYPE_MEDIUM_BLOB:
644
 
                case MYSQL_TYPE_LONG_BLOB:
645
 
#endif
646
 
                case MYSQL_TYPE_BLOB: {
647
 
                        /* TODO - Check: this was the original comment: I must set
648
 
                         * *data to non-NULL value, *data == 0, means SQL NULL value.
649
 
                         */
650
 
                        char    *data;
651
 
 
652
 
                        /* GOTCHA: There is no way this can work! field is shared
653
 
                         * between threads.
654
 
                        char    *save = field->ptr;
655
 
 
656
 
                        field->ptr = (char *) from;
657
 
                        ((Field_blob *) field)->get_ptr(&data);
658
 
                        field->ptr = save;                                      // Restore org row pointer
659
 
                        */
660
 
 
661
 
                        xtWord4 packlength = ((Field_blob *) field)->pack_length_no_ptr();
662
 
 
663
 
                        memcpy(&data, ((char *) from)+packlength, sizeof(char*));
664
 
                        
665
 
                        //*len = ((Field_blob *) field)->get_length((byte *) from);
666
 
                        *len = ((Field_blob *) field)->get_length((byte *) from, GET_TABLE_SHARE(table)->db_low_byte_first);
667
 
                        return data;
668
 
                }
669
 
#ifndef DRIZZLED
670
 
                case MYSQL_TYPE_STRING:
671
 
                        /* To write this function you would think Field_string::pack
672
 
                         * would serve as a good example, but as far as I can tell
673
 
                         * it has a bug: the test from[length-1] == ' ' assumes
674
 
                         * 1-byte chars.
675
 
                         *
676
 
                         * But this is not relevant because I believe lengthsp
677
 
                         * will give me the correct answer!
678
 
                         */
679
 
                        *len = field->charset()->cset->lengthsp(field->charset(), from, field->field_length);
680
 
                        return from;
681
 
                case MYSQL_TYPE_VAR_STRING: {
682
 
                        uint length=uint2korr(from);
683
 
 
684
 
                        *len = length;
685
 
                        return from+HA_KEY_BLOB_LENGTH;
686
 
                }
687
 
#endif
688
 
                case MYSQL_TYPE_VARCHAR: {
689
 
                        uint length;
690
 
 
691
 
                        if (((Field_varstring *) field)->pack_length_no_ptr() == 1)
692
 
                                length = *((unsigned char *) from);
693
 
                        else
694
 
                                length = uint2korr(from);
695
 
                        
696
 
                        *len = length;
697
 
                        return from+((Field_varstring *) field)->pack_length_no_ptr();
698
 
                }
699
 
#ifndef DRIZZLED
700
 
                case MYSQL_TYPE_DECIMAL:
701
 
                case MYSQL_TYPE_TINY:
702
 
                case MYSQL_TYPE_SHORT:
703
 
                case MYSQL_TYPE_LONG:
704
 
                case MYSQL_TYPE_FLOAT:
705
 
                case MYSQL_TYPE_DOUBLE:
706
 
                case MYSQL_TYPE_NULL:
707
 
                case MYSQL_TYPE_TIMESTAMP:
708
 
                case MYSQL_TYPE_LONGLONG:
709
 
                case MYSQL_TYPE_INT24:
710
 
                case MYSQL_TYPE_DATE:
711
 
                case MYSQL_TYPE_TIME:
712
 
                case MYSQL_TYPE_DATETIME:
713
 
                case MYSQL_TYPE_YEAR:
714
 
                case MYSQL_TYPE_NEWDATE:
715
 
                case MYSQL_TYPE_BIT:
716
 
                case MYSQL_TYPE_NEWDECIMAL:
717
 
                case MYSQL_TYPE_ENUM:
718
 
                case MYSQL_TYPE_SET:
719
 
                case MYSQL_TYPE_GEOMETRY:
720
 
#else
721
 
                case DRIZZLE_TYPE_LONG:
722
 
                case DRIZZLE_TYPE_DOUBLE:
723
 
                case DRIZZLE_TYPE_NULL:
724
 
                case DRIZZLE_TYPE_TIMESTAMP:
725
 
                case DRIZZLE_TYPE_LONGLONG:
726
 
                case DRIZZLE_TYPE_DATETIME:
727
 
                case DRIZZLE_TYPE_TIME:
728
 
                case DRIZZLE_TYPE_DATE:
729
 
                case DRIZZLE_TYPE_DECIMAL:
730
 
                case DRIZZLE_TYPE_ENUM:
731
 
                case DRIZZLE_TYPE_UUID:
732
 
#endif
733
 
                        break;
734
 
        }
735
 
 
736
 
        *len = field->pack_length();
737
 
        return from;
738
 
}
739
 
 
740
 
/*
741
 
 * Set the length and data value of a field.
742
 
 * 
743
 
 * If input data is NULL this is a NULL value. In this case
744
 
 * we assume the null bit has been set and prepared
745
 
 * the field as follows:
746
 
 * 
747
 
 * According to the InnoDB implementation, we need
748
 
 * to zero out the field data...
749
 
 * "MySQL seems to assume the field for an SQL NULL
750
 
 * value is set to zero or space. Not taking this into
751
 
 * account caused seg faults with NULL BLOB fields, and
752
 
 * bug number 154 in the MySQL bug database: GROUP BY
753
 
 * and DISTINCT could treat NULL values inequal".
754
 
 */
755
 
static void mx_set_length_and_data(STRUCT_TABLE *table, Field *field, char *dest, xtWord4 len, char *data)
756
 
{
757
 
        char *from;
758
 
        
759
 
        from = dest + field->offset(table->getDefaultValues());
760
 
        switch (field->real_type()) {
761
 
#ifndef DRIZZLED
762
 
                case MYSQL_TYPE_TINY_BLOB:
763
 
                case MYSQL_TYPE_MEDIUM_BLOB:
764
 
                case MYSQL_TYPE_LONG_BLOB:
765
 
#endif
766
 
                case MYSQL_TYPE_BLOB: {
767
 
                        /* GOTCHA: There is no way that this can work.
768
 
                         * field is shared, because table is shared!
769
 
                        char *save = field->ptr;
770
 
                 
771
 
                        field->ptr = (char *) from;
772
 
                        ((Field_blob *) field)->set_ptr(len, data);
773
 
                        field->ptr = save;                                      // Restore org row pointer
774
 
                        */
775
 
                        xtWord4 packlength = ((Field_blob *) field)->pack_length_no_ptr();
776
 
 
777
 
                        ((Field_blob *) field)->store_length((byte *) from, len, GET_TABLE_SHARE(table)->db_low_byte_first);
778
 
                        memcpy_fixed(((char *) from)+packlength, &data, sizeof(char*));
779
 
 
780
 
                        if (data)
781
 
                                mx_set_notnull_in_record(table, field, dest);
782
 
                        return;
783
 
                }
784
 
#ifndef DRIZZLED
785
 
                case MYSQL_TYPE_STRING:
786
 
                        if (data) {
787
 
                                mx_set_notnull_in_record(field, dest);
788
 
                                memcpy(from, data, len);
789
 
                        }
790
 
                        else
791
 
                                len = 0;
792
 
 
793
 
                        /* And I think that fill will do this for me... */
794
 
                        field->charset()->cset->fill(field->charset(), from + len, field->field_length - len, ' ');
795
 
                        return;
796
 
                case MYSQL_TYPE_VAR_STRING:
797
 
                        int2store(from, len);
798
 
                        if (data) {
799
 
                                mx_set_notnull_in_record(field, dest);
800
 
                                memcpy(from+HA_KEY_BLOB_LENGTH, data, len);
801
 
                        }
802
 
                        return;
803
 
#endif
804
 
                case MYSQL_TYPE_VARCHAR:
805
 
                        if (((Field_varstring *) field)->pack_length_no_ptr() == 1)
806
 
                                *((unsigned char *) from) = (unsigned char) len;
807
 
                        else
808
 
                                int2store(from, len);
809
 
                        if (data) {
810
 
                                mx_set_notnull_in_record(table, field, dest);
811
 
                                memcpy(from+((Field_varstring *) field)->pack_length_no_ptr(), data, len);
812
 
                        }
813
 
                        return;
814
 
#ifndef DRIZZLED
815
 
                case MYSQL_TYPE_DECIMAL:
816
 
                case MYSQL_TYPE_TINY:
817
 
                case MYSQL_TYPE_SHORT:
818
 
                case MYSQL_TYPE_LONG:
819
 
                case MYSQL_TYPE_FLOAT:
820
 
                case MYSQL_TYPE_DOUBLE:
821
 
                case MYSQL_TYPE_NULL:
822
 
                case MYSQL_TYPE_TIMESTAMP:
823
 
                case MYSQL_TYPE_LONGLONG:
824
 
                case MYSQL_TYPE_INT24:
825
 
                case MYSQL_TYPE_DATE:
826
 
                case MYSQL_TYPE_TIME:
827
 
                case MYSQL_TYPE_DATETIME:
828
 
                case MYSQL_TYPE_YEAR:
829
 
                case MYSQL_TYPE_NEWDATE:
830
 
                case MYSQL_TYPE_BIT:
831
 
                case MYSQL_TYPE_NEWDECIMAL:
832
 
                case MYSQL_TYPE_ENUM:
833
 
                case MYSQL_TYPE_SET:
834
 
                case MYSQL_TYPE_GEOMETRY:
835
 
#else
836
 
                case DRIZZLE_TYPE_LONG:
837
 
                case DRIZZLE_TYPE_DOUBLE:
838
 
                case DRIZZLE_TYPE_NULL:
839
 
                case DRIZZLE_TYPE_TIMESTAMP:
840
 
                case DRIZZLE_TYPE_LONGLONG:
841
 
                case DRIZZLE_TYPE_DATETIME:
842
 
                case DRIZZLE_TYPE_TIME:
843
 
                case DRIZZLE_TYPE_DATE:
844
 
                case DRIZZLE_TYPE_DECIMAL:
845
 
                case DRIZZLE_TYPE_ENUM:
846
 
                case DRIZZLE_TYPE_UUID:
847
 
#endif
848
 
                        break;
849
 
        }
850
 
 
851
 
        if (data) {
852
 
                mx_set_notnull_in_record(table, field, dest);
853
 
                memcpy(from, data, len);
854
 
        }
855
 
        else
856
 
                bzero(from, field->pack_length());
857
 
}
858
 
 
859
 
xtPublic void myxt_set_null_row_from_key(XTOpenTablePtr XT_UNUSED(ot), XTIndexPtr ind, xtWord1 *record)
860
 
{
861
 
        register XTIndexSegRec *keyseg = ind->mi_seg;
862
 
 
863
 
        for (u_int i=0; i<ind->mi_seg_count; i++, keyseg++) {
864
 
                ASSERT_NS(keyseg->null_bit);
865
 
                record[keyseg->null_pos] |= keyseg->null_bit;
866
 
        }
867
 
}
868
 
 
869
 
xtPublic void myxt_set_default_row_from_key(XTOpenTablePtr ot, XTIndexPtr ind, xtWord1 *record)
870
 
{
871
 
        XTTableHPtr             tab = ot->ot_table;
872
 
        STRUCT_TABLE    *table = tab->tab_dic.dic_my_table;
873
 
        XTIndexSegRec   *keyseg = ind->mi_seg;
874
 
 
875
 
        xt_lock_mutex_ns(&tab->tab_dic_field_lock);
876
 
 
877
 
        for (u_int i=0; i<ind->mi_seg_count; i++, keyseg++) {
878
 
                
879
 
                u_int col_idx = keyseg->col_idx;
880
 
                Field *field = GET_TABLE_FIELDS(table)[col_idx];
881
 
                byte  *field_save = field->ptr;
882
 
 
883
 
                field->ptr = GET_TABLE_SHARE(table)->getDefaultValues() + keyseg->start;
884
 
                memcpy(record + keyseg->start, field->ptr, field->pack_length());
885
 
                record[keyseg->null_pos] &= ~keyseg->null_bit;
886
 
                record[keyseg->null_pos] |= GET_TABLE_SHARE(table)->getDefaultValues()[keyseg->null_pos] & keyseg->null_bit;
887
 
 
888
 
                field->ptr = field_save;
889
 
        }
890
 
 
891
 
        xt_unlock_mutex_ns(&tab->tab_dic_field_lock);
892
 
}
893
 
 
894
 
/* Derived from _mi_put_key_in_record */
895
 
xtPublic xtBool myxt_create_row_from_key(XTOpenTablePtr XT_UNUSED(ot), XTIndexPtr ind, xtWord1 *b_value, u_int key_len, xtWord1 *dest_buff)
896
 
{
897
 
        byte                                    *record = (byte *) dest_buff;
898
 
        register byte                   *key;
899
 
        byte                                    *pos,*key_end;
900
 
        register XTIndexSegRec  *keyseg = ind->mi_seg;
901
 
 
902
 
        /* GOTCHA: When selecting from multiple
903
 
         * indexes the key values are "merged" into the
904
 
         * same buffer!!
905
 
         * This means that this function must not affect
906
 
         * the value of any other feilds.
907
 
         *
908
 
         * I was setting all to NULL:
909
 
        memset(dest_buff, 0xFF, GET_TABLE_SHARE(table)->null_bytes);
910
 
        */
911
 
        key = (byte *) b_value;
912
 
        key_end = key + key_len;
913
 
        for (u_int i=0; i<ind->mi_seg_count; i++, keyseg++) {
914
 
                if (keyseg->null_bit) {
915
 
                        if (!*key++)
916
 
                        {
917
 
                                record[keyseg->null_pos] |= keyseg->null_bit;
918
 
                                continue;
919
 
                        }
920
 
                        record[keyseg->null_pos] &= ~keyseg->null_bit;
921
 
                }
922
 
#ifndef DRIZZLED
923
 
                if (keyseg->type == HA_KEYTYPE_BIT)
924
 
                {
925
 
                        uint length = keyseg->length;
926
 
 
927
 
                        if (keyseg->bit_length)
928
 
                        {
929
 
                                uchar bits= *key++;
930
 
                                set_rec_bits(bits, record + keyseg->bit_pos, keyseg->bit_start,
931
 
                                                                                 keyseg->bit_length);
932
 
                                length--;
933
 
                        }
934
 
                        else
935
 
                        {
936
 
                                clr_rec_bits(record + keyseg->bit_pos, keyseg->bit_start,
937
 
                                                                                 keyseg->bit_length);
938
 
                        }
939
 
                        memcpy(record + keyseg->start, (byte*) key, length);
940
 
                        key+= length;
941
 
                        continue;
942
 
                }
943
 
#endif
944
 
                if (keyseg->flag & HA_SPACE_PACK)
945
 
                {
946
 
                        uint length;
947
 
                        get_key_length(length,key);
948
 
#ifdef CHECK_KEYS
949
 
                        if (length > keyseg->length || key+length > key_end)
950
 
                                goto err;
951
 
#endif
952
 
                        pos = record+keyseg->start;
953
 
#ifndef DRIZZLED
954
 
                        if (keyseg->type != (int) HA_KEYTYPE_NUM)
955
 
                        {
956
 
#endif
957
 
                                memcpy(pos,key,(size_t) length);
958
 
                                bfill(pos+length,keyseg->length-length,' ');
959
 
#ifndef DRIZZLED
960
 
                        }
961
 
                        else
962
 
                        {
963
 
                                bfill(pos,keyseg->length-length,' ');
964
 
                                memcpy(pos+keyseg->length-length,key,(size_t) length);
965
 
                        }
966
 
#endif
967
 
                        key+=length;
968
 
                        continue;
969
 
                }
970
 
 
971
 
                if (keyseg->flag & HA_VAR_LENGTH_PART)
972
 
                {
973
 
                        uint length;
974
 
                        get_key_length(length,key);
975
 
#ifdef CHECK_KEYS
976
 
                        if (length > keyseg->length || key+length > key_end)
977
 
        goto err;
978
 
#endif
979
 
                        /* Store key length */
980
 
                        if (keyseg->bit_start == 1)
981
 
                                *(uchar*) (record+keyseg->start)= (uchar) length;
982
 
                        else
983
 
                                int2store(record+keyseg->start, length);
984
 
                        /* And key data */
985
 
                        memcpy(record+keyseg->start + keyseg->bit_start, (byte*) key, length);
986
 
                        key+= length;
987
 
                }
988
 
                else if (keyseg->flag & HA_BLOB_PART)
989
 
                {
990
 
                        uint length;
991
 
                        get_key_length(length,key);
992
 
#ifdef CHECK_KEYS
993
 
                        if (length > keyseg->length || key+length > key_end)
994
 
                                goto err;
995
 
#endif
996
 
                        /* key is a pointer into ot_ind_rbuf, which should be
997
 
                         * safe until we move to the next index item!
998
 
                         */
999
 
                        byte *key_ptr = key; // Cannot take the address of a register variable!
1000
 
                        memcpy(record+keyseg->start+keyseg->bit_start,
1001
 
                         (char*) &key_ptr,sizeof(char*));
1002
 
 
1003
 
                        my_store_blob_length(record+keyseg->start,
1004
 
                                        (uint) keyseg->bit_start,length);
1005
 
                        key+=length;
1006
 
                }
1007
 
                else if (keyseg->flag & HA_SWAP_KEY)
1008
 
                {
1009
 
                        byte *to=       record+keyseg->start+keyseg->length;
1010
 
                        byte *end= key+keyseg->length;
1011
 
#ifdef CHECK_KEYS
1012
 
                        if (end > key_end)
1013
 
                                goto err;
1014
 
#endif
1015
 
                        do {
1016
 
                                *--to= *key++;
1017
 
                        } while (key != end);
1018
 
                        continue;
1019
 
                }
1020
 
                else
1021
 
                {
1022
 
#ifdef CHECK_KEYS
1023
 
                        if (key+keyseg->length > key_end)
1024
 
                                goto err;
1025
 
#endif
1026
 
                        memcpy(record+keyseg->start,(byte*) key,
1027
 
                         (size_t) keyseg->length);
1028
 
                        key+= keyseg->length;
1029
 
                }
1030
 
        
1031
 
        }
1032
 
        return OK;
1033
 
 
1034
 
#ifdef CHECK_KEYS
1035
 
        err:
1036
 
        return FAILED;                          /* Crashed row */
1037
 
#endif
1038
 
}
1039
 
 
1040
 
/*
1041
 
 * -----------------------------------------------------------------------
1042
 
 * Compare keys
1043
 
 */
1044
 
 
1045
 
static int my_compare_bin(uchar *a, uint a_length, uchar *b, uint b_length,
1046
 
                                                                                         my_bool part_key, my_bool skip_end_space)
1047
 
{
1048
 
        uint length= a_length < b_length ? a_length : b_length;
1049
 
        uchar *end= a+ length;
1050
 
        int flag;
1051
 
 
1052
 
        while (a < end)
1053
 
                if ((flag= (int) *a++ - (int) *b++))
1054
 
                        return flag;
1055
 
        if (part_key && b_length < a_length)
1056
 
                return 0;
1057
 
        if (skip_end_space && a_length != b_length)
1058
 
        {
1059
 
                int swap= 1;
1060
 
                /*
1061
 
                        We are using space compression. We have to check if longer key
1062
 
                        has next character < ' ', in which case it's less than the shorter
1063
 
                        key that has an implicite space afterwards.
1064
 
 
1065
 
                        This code is identical to the one in
1066
 
                        strings/ctype-simple.c:my_strnncollsp_simple
1067
 
                */
1068
 
                if (a_length < b_length)
1069
 
                {
1070
 
                        /* put shorter key in a */
1071
 
                        a_length= b_length;
1072
 
                        a= b;
1073
 
                        swap= -1;                                       /* swap sign of result */
1074
 
                }
1075
 
                for (end= a + a_length-length; a < end ; a++)
1076
 
                {
1077
 
                        if (*a != ' ')
1078
 
                                return (*a < ' ') ? -swap : swap;
1079
 
                }
1080
 
                return 0;
1081
 
        }
1082
 
        return (int) (a_length-b_length);
1083
 
}
1084
 
 
1085
 
xtPublic u_int myxt_get_key_length(XTIndexPtr ind, xtWord1 *key_buf)
1086
 
{
1087
 
        register XTIndexSegRec  *keyseg = ind->mi_seg;
1088
 
        register uchar                  *key_data = (uchar *) key_buf;
1089
 
        uint                                    seg_len;
1090
 
        uint                                    pack_len;
1091
 
 
1092
 
        for (u_int i=0; i<ind->mi_seg_count; i++, keyseg++) {
1093
 
                /* Handle NULL part */
1094
 
                if (keyseg->null_bit) {
1095
 
                        if (!*key_data++)       
1096
 
                                continue;
1097
 
                }
1098
 
 
1099
 
                switch ((enum ha_base_keytype) keyseg->type) {
1100
 
                        case HA_KEYTYPE_TEXT:                                                                                    /* Ascii; Key is converted */
1101
 
                                if (keyseg->flag & HA_SPACE_PACK) {
1102
 
                                        get_key_pack_length(seg_len, pack_len, key_data);
1103
 
                                }
1104
 
                                else
1105
 
                                        seg_len = keyseg->length;
1106
 
                                key_data += seg_len;
1107
 
                                break;
1108
 
                        case HA_KEYTYPE_BINARY:
1109
 
                                if (keyseg->flag & HA_SPACE_PACK) {
1110
 
                                        get_key_pack_length(seg_len, pack_len, key_data);
1111
 
                                }
1112
 
                                else
1113
 
                                        seg_len = keyseg->length;
1114
 
                                key_data += seg_len;
1115
 
                                break;
1116
 
                        case HA_KEYTYPE_VARTEXT1:
1117
 
                        case HA_KEYTYPE_VARTEXT2:
1118
 
                                get_key_pack_length(seg_len, pack_len, key_data);
1119
 
                                key_data += seg_len;
1120
 
                                break;
1121
 
                        case HA_KEYTYPE_VARBINARY1:
1122
 
                        case HA_KEYTYPE_VARBINARY2:
1123
 
                                get_key_pack_length(seg_len, pack_len, key_data);
1124
 
                                key_data += seg_len;
1125
 
                                break;
1126
 
#ifndef DRIZZLED
1127
 
                        case HA_KEYTYPE_NUM: {
1128
 
                                /* Numeric key */
1129
 
                                if (keyseg->flag & HA_SPACE_PACK)
1130
 
                                        seg_len = *key_data++;
1131
 
                                else
1132
 
                                        seg_len = keyseg->length;
1133
 
                                key_data += seg_len;
1134
 
                                break;
1135
 
                        }
1136
 
                        case HA_KEYTYPE_INT8:
1137
 
                        case HA_KEYTYPE_SHORT_INT:
1138
 
                        case HA_KEYTYPE_USHORT_INT:
1139
 
                        case HA_KEYTYPE_INT24:
1140
 
                        case HA_KEYTYPE_FLOAT:
1141
 
                        case HA_KEYTYPE_BIT:
1142
 
#endif
1143
 
                        case HA_KEYTYPE_LONG_INT:
1144
 
                        case HA_KEYTYPE_ULONG_INT:
1145
 
                        case HA_KEYTYPE_DOUBLE:
1146
 
                        case HA_KEYTYPE_LONGLONG:
1147
 
                        case HA_KEYTYPE_ULONGLONG:
1148
 
                                key_data += keyseg->length;
1149
 
                                break;
1150
 
                        case HA_KEYTYPE_END:
1151
 
                                goto end;
1152
 
                }
1153
 
        }
1154
 
 
1155
 
        end:
1156
 
        u_int ilen = (xtWord1 *) key_data - key_buf;
1157
 
        if (ilen > XT_INDEX_MAX_KEY_SIZE)
1158
 
                ind->mi_key_corrupted = TRUE;
1159
 
        return ilen;
1160
 
}
1161
 
 
1162
 
/* Derived from ha_key_cmp */
1163
 
xtPublic int myxt_compare_key(XTIndexPtr ind, int search_flags, uint key_length, xtWord1 *key_value, xtWord1 *b_value)
1164
 
{
1165
 
        register XTIndexSegRec  *keyseg = ind->mi_seg;
1166
 
        int                                             flag;
1167
 
        register uchar                  *a = (uchar *) key_value;
1168
 
        uint                                    a_length;
1169
 
        register uchar                  *b = (uchar *) b_value;
1170
 
        uint                                    b_length;
1171
 
        uint                                    next_key_length;
1172
 
        uchar                                   *end;
1173
 
        uint                                    piks;
1174
 
        uint                                    pack_len;
1175
 
 
1176
 
        for (uint i=0; i < ind->mi_seg_count && (int) key_length > 0; key_length = next_key_length, keyseg++, i++) {
1177
 
                piks = !(keyseg->flag & HA_NO_SORT);
1178
 
 
1179
 
                /* Handle NULL part */
1180
 
                if (keyseg->null_bit) {
1181
 
                        /* 1 is not null, 0 is null */
1182
 
                        int b_not_null = (int) *b++;
1183
 
 
1184
 
                        key_length--;
1185
 
                        if ((int) *a != b_not_null && piks)
1186
 
                        {
1187
 
                                flag = (int) *a - b_not_null;
1188
 
                                return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
1189
 
                        }
1190
 
                        if (!*a++) {            
1191
 
                                /* If key was NULL */
1192
 
                                if (search_flags == (SEARCH_FIND | SEARCH_UPDATE))
1193
 
                                        search_flags = SEARCH_SAME;                                                              /* Allow duplicate keys */
1194
 
                                else if (search_flags & SEARCH_NULL_ARE_NOT_EQUAL)
1195
 
                                {
1196
 
                                        /*
1197
 
                                         * This is only used from mi_check() to calculate cardinality.
1198
 
                                         * It can't be used when searching for a key as this would cause
1199
 
                                         * compare of (a,b) and (b,a) to return the same value.
1200
 
                                         */
1201
 
                                        return -1;
1202
 
                                }
1203
 
                                /* PMC - I don't know why I had next_key_length = key_length - keyseg->length;
1204
 
                                 * This was my comment: even when null we have the complete length
1205
 
                                 *
1206
 
                                 * The truth is, a NULL only takes up one byte in the key, and this has already
1207
 
                                 * been subtracted.
1208
 
                                 */
1209
 
                                next_key_length = key_length;
1210
 
                                continue;                                                                                                                        /* To next key part */
1211
 
                        }
1212
 
                }
1213
 
                
1214
 
                /* Both components are not null... */
1215
 
                if (keyseg->length < key_length) {
1216
 
                        end = a + keyseg->length;
1217
 
                        next_key_length = key_length - keyseg->length;
1218
 
                }
1219
 
                else {
1220
 
                        end = a + key_length;
1221
 
                        next_key_length = 0;
1222
 
                }
1223
 
 
1224
 
                switch ((enum ha_base_keytype) keyseg->type) {
1225
 
                        case HA_KEYTYPE_TEXT:                                                                                    /* Ascii; Key is converted */
1226
 
                                if (keyseg->flag & HA_SPACE_PACK) {
1227
 
                                        get_key_pack_length(a_length, pack_len, a);
1228
 
                                        next_key_length = key_length - a_length - pack_len;
1229
 
                                        get_key_pack_length(b_length, pack_len, b);
1230
 
 
1231
 
                                        if (piks && (flag = my_compare_text(keyseg->charset, a, a_length, b, b_length,
1232
 
                                                                        (my_bool) ((search_flags & SEARCH_PREFIX) && next_key_length <= 0),
1233
 
                                                                        (my_bool)!(search_flags & SEARCH_PREFIX))))
1234
 
                                                return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
1235
 
                                        a += a_length;
1236
 
                                }
1237
 
                                else {
1238
 
                                        a_length = (uint) (end - a);
1239
 
                                        b_length = keyseg->length;
1240
 
                                        if (piks && (flag = my_compare_text(keyseg->charset, a, a_length, b, b_length,
1241
 
                                                                        (my_bool) ((search_flags & SEARCH_PREFIX) && next_key_length <= 0),
1242
 
                                                                        (my_bool)!(search_flags & SEARCH_PREFIX))))
1243
 
                                                return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
1244
 
                                        a = end;
1245
 
                                }
1246
 
                                b += b_length;
1247
 
                                break;
1248
 
                        case HA_KEYTYPE_BINARY:
1249
 
                                if (keyseg->flag & HA_SPACE_PACK) {
1250
 
                                        get_key_pack_length(a_length, pack_len, a);
1251
 
                                        next_key_length = key_length - a_length - pack_len;
1252
 
                                        get_key_pack_length(b_length, pack_len, b);
1253
 
 
1254
 
                                        if (piks && (flag = my_compare_bin(a, a_length, b, b_length,
1255
 
                                                                (my_bool) ((search_flags & SEARCH_PREFIX) && next_key_length <= 0), 1)))
1256
 
                                                return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
1257
 
                                }
1258
 
                                else {
1259
 
                                        a_length = keyseg->length;
1260
 
                                        b_length = keyseg->length;
1261
 
                                        if (piks && (flag = my_compare_bin(a, a_length, b, b_length,
1262
 
                                                                        (my_bool) ((search_flags & SEARCH_PREFIX) && next_key_length <= 0), 0)))
1263
 
                                                return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
1264
 
                                }
1265
 
                                a += a_length;
1266
 
                                b += b_length;
1267
 
                                break;
1268
 
                        case HA_KEYTYPE_VARTEXT1:
1269
 
                        case HA_KEYTYPE_VARTEXT2:
1270
 
                        {
1271
 
                                get_key_pack_length(a_length, pack_len, a);
1272
 
                                next_key_length = key_length - a_length - pack_len;
1273
 
                                get_key_pack_length(b_length, pack_len, b);
1274
 
 
1275
 
                                if (piks && (flag = my_compare_text(keyseg->charset, a, a_length, b, b_length,
1276
 
                                                                (my_bool) ((search_flags & SEARCH_PREFIX) && next_key_length <= 0),
1277
 
                                                                (my_bool) ((search_flags & (SEARCH_FIND | SEARCH_UPDATE)) == SEARCH_FIND))))
1278
 
                                        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
1279
 
                                a += a_length;
1280
 
                                b += b_length;
1281
 
                                break;
1282
 
                        }
1283
 
                        case HA_KEYTYPE_VARBINARY1:
1284
 
                        case HA_KEYTYPE_VARBINARY2:
1285
 
                        {                               
1286
 
                                get_key_pack_length(a_length, pack_len, a);
1287
 
                                next_key_length = key_length - a_length - pack_len;
1288
 
                                get_key_pack_length(b_length, pack_len, b);
1289
 
 
1290
 
                                if (piks && (flag=my_compare_bin(a, a_length, b, b_length,
1291
 
                                                (my_bool) ((search_flags & SEARCH_PREFIX) && next_key_length <= 0), 0)))
1292
 
                                        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
1293
 
                                a += a_length;
1294
 
                                b += b_length;
1295
 
                                break;
1296
 
                        }
1297
 
#ifndef DRIZZLED
1298
 
                        case HA_KEYTYPE_INT8:
1299
 
                        {
1300
 
                                int i_1 = (int) *((signed char *) a);
1301
 
                                int i_2 = (int) *((signed char *) b);
1302
 
                                if (piks && (flag = CMP_NUM(i_1,i_2)))
1303
 
                                        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
1304
 
                                a = end;
1305
 
                                b += keyseg->length;
1306
 
                                break;
1307
 
                        }
1308
 
                        case HA_KEYTYPE_SHORT_INT: {
1309
 
                                int16_t s_1 = sint2korr(a);
1310
 
                                int16_t s_2 = sint2korr(b);
1311
 
                                if (piks && (flag = CMP_NUM(s_1, s_2)))
1312
 
                                        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
1313
 
                                a = end;
1314
 
                                b += keyseg->length;
1315
 
                                break;
1316
 
                        }
1317
 
                        case HA_KEYTYPE_USHORT_INT: {
1318
 
                                uint16_t us_1= sint2korr(a);
1319
 
                                uint16_t us_2= sint2korr(b);
1320
 
                                if (piks && (flag = CMP_NUM(us_1, us_2)))
1321
 
                                        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
1322
 
                                a =     end;
1323
 
                                b += keyseg->length;
1324
 
                                break;
1325
 
                        }
1326
 
#endif
1327
 
                        case HA_KEYTYPE_LONG_INT: {
1328
 
                                int32_t l_1 = sint4korr(a);
1329
 
                                int32_t l_2 = sint4korr(b);
1330
 
                                if (piks && (flag = CMP_NUM(l_1, l_2)))
1331
 
                                        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
1332
 
                                a = end;
1333
 
                                b += keyseg->length;
1334
 
                                break;
1335
 
                        }
1336
 
                        case HA_KEYTYPE_ULONG_INT: {
1337
 
                                uint32_t u_1 = sint4korr(a);
1338
 
                                uint32_t u_2 = sint4korr(b);
1339
 
                                if (piks && (flag = CMP_NUM(u_1, u_2)))
1340
 
                                        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
1341
 
                                a = end;
1342
 
                                b += keyseg->length;
1343
 
                                break;
1344
 
                        }
1345
 
#ifndef DRIZZLED
1346
 
                        case HA_KEYTYPE_INT24: {
1347
 
                                int32 l_1 = sint3korr(a);
1348
 
                                int32 l_2 = sint3korr(b);
1349
 
                                if (piks && (flag = CMP_NUM(l_1, l_2)))
1350
 
                                        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
1351
 
                                a = end;
1352
 
                                b += keyseg->length;
1353
 
                                break;
1354
 
                        }
1355
 
                        case HA_KEYTYPE_UINT24: {
1356
 
                                int32_t l_1 = uint3korr(a);
1357
 
                                int32_t l_2 = uint3korr(b);
1358
 
                                if (piks && (flag = CMP_NUM(l_1, l_2)))
1359
 
                                        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
1360
 
                                a = end;
1361
 
                                b += keyseg->length;
1362
 
                                break;
1363
 
                        }
1364
 
                        case HA_KEYTYPE_FLOAT: {
1365
 
                                float f_1, f_2;
1366
 
 
1367
 
                                float4get(f_1, a);
1368
 
                                float4get(f_2, b);
1369
 
                                /*
1370
 
                                 * The following may give a compiler warning about floating point
1371
 
                                 * comparison not being safe, but this is ok in this context as
1372
 
                                 * we are bascily doing sorting
1373
 
                                 */
1374
 
                                if (piks && (flag = CMP_NUM(f_1, f_2)))
1375
 
                                        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
1376
 
                                a = end;
1377
 
                                b += keyseg->length;
1378
 
                                break;
1379
 
                        }
1380
 
#endif
1381
 
                        case HA_KEYTYPE_DOUBLE: {
1382
 
                                double d_1, d_2;
1383
 
 
1384
 
                                float8get(d_1, a);
1385
 
                                float8get(d_2, b);
1386
 
                                /*
1387
 
                                 * The following may give a compiler warning about floating point
1388
 
                                 * comparison not being safe, but this is ok in this context as
1389
 
                                 * we are bascily doing sorting
1390
 
                                 */
1391
 
                                if (piks && (flag = CMP_NUM(d_1, d_2)))
1392
 
                                        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
1393
 
                                a = end;
1394
 
                                b += keyseg->length;
1395
 
                                break;
1396
 
                        }
1397
 
#ifndef DRIZZLED
1398
 
                        case HA_KEYTYPE_NUM: {
1399
 
                                /* Numeric key */
1400
 
                                if (keyseg->flag & HA_SPACE_PACK) {
1401
 
                                        a_length = *a++;
1402
 
                                        end = a + a_length;
1403
 
                                        next_key_length = key_length - a_length - 1;
1404
 
                                        b_length = *b++;
1405
 
                                }
1406
 
                                else {
1407
 
                                        a_length = (int) (end - a);
1408
 
                                        b_length = keyseg->length;
1409
 
                                }
1410
 
 
1411
 
                                /* remove pre space from keys */
1412
 
                                for ( ; a_length && *a == ' ' ; a++, a_length--) ;
1413
 
                                for ( ; b_length && *b == ' ' ; b++, b_length--) ;
1414
 
 
1415
 
                                if (keyseg->flag & HA_REVERSE_SORT) {
1416
 
                                        swap_variables(uchar *, a, b);
1417
 
                                        swap_variables(uint, a_length, b_length);
1418
 
                                }
1419
 
                                
1420
 
                                if (piks) {
1421
 
                                        if (*a == '-') {
1422
 
                                                if (*b != '-')
1423
 
                                                        return -1;
1424
 
                                                a++; b++;
1425
 
                                                swap_variables(uchar *, a, b);
1426
 
                                                swap_variables(uint, a_length, b_length);
1427
 
                                                a_length--; b_length--;
1428
 
                                        }
1429
 
                                        else if (*b == '-')
1430
 
                                                return 1;
1431
 
                                        while (a_length && (*a == '+' || *a == '0')) {
1432
 
                                                a++; a_length--;
1433
 
                                        }
1434
 
                                        
1435
 
                                        while (b_length && (*b == '+' || *b == '0')) {
1436
 
                                                b++; b_length--;
1437
 
                                        }
1438
 
                                
1439
 
                                        if (a_length != b_length)
1440
 
                                                return (a_length < b_length) ? -1 : 1;
1441
 
                                        while (b_length) {
1442
 
                                                if (*a++ !=     *b++)
1443
 
                                                        return ((int) a[-1] - (int) b[-1]);
1444
 
                                                b_length--;
1445
 
                                        }
1446
 
                                }
1447
 
                                a = end;
1448
 
                                b += b_length;
1449
 
                                break;
1450
 
                        }
1451
 
#endif
1452
 
#ifdef HAVE_LONG_LONG
1453
 
                        case HA_KEYTYPE_LONGLONG: {
1454
 
                                longlong ll_a = sint8korr(a);
1455
 
                                longlong ll_b = sint8korr(b);
1456
 
                                if (piks && (flag = CMP_NUM(ll_a,ll_b)))
1457
 
                                        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
1458
 
                                a = end;
1459
 
                                b += keyseg->length;
1460
 
                                break;
1461
 
                        }
1462
 
                        case HA_KEYTYPE_ULONGLONG: {                                    
1463
 
                                ulonglong ll_a = uint8korr(a);
1464
 
                                ulonglong ll_b = uint8korr(b);
1465
 
                                if (piks && (flag = CMP_NUM(ll_a,ll_b)))
1466
 
                                        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
1467
 
                                a = end;
1468
 
                                b += keyseg->length;
1469
 
                                break;
1470
 
                        }
1471
 
#endif
1472
 
#ifndef DRIZZLED
1473
 
                        case HA_KEYTYPE_BIT:
1474
 
                                /* TODO: What here? */
1475
 
                                break;
1476
 
#endif
1477
 
                        case HA_KEYTYPE_END:                                                                                            /* Ready */
1478
 
                                goto end;
1479
 
                }
1480
 
        }
1481
 
 
1482
 
        end:
1483
 
        return 0;
1484
 
}
1485
 
 
1486
 
xtPublic u_int myxt_key_seg_length(XTIndexSegRec *keyseg, u_int key_offset, xtWord1 *key_value)
1487
 
{
1488
 
        register xtWord1        *a = (xtWord1 *) key_value + key_offset;
1489
 
        u_int                           a_length;
1490
 
        u_int                           has_null = 0;
1491
 
        u_int                           key_length = 0;
1492
 
        u_int                           pack_len;
1493
 
 
1494
 
        /* Handle NULL part */
1495
 
        if (keyseg->null_bit) {
1496
 
                has_null++;
1497
 
                /* If the value is null, then it only requires one byte: */
1498
 
                if (!*a++)
1499
 
                        return has_null;
1500
 
        }
1501
 
        
1502
 
        key_length = has_null + keyseg->length;
1503
 
 
1504
 
        switch ((enum ha_base_keytype) keyseg->type) {
1505
 
                case HA_KEYTYPE_TEXT:                                                                                    /* Ascii; Key is converted */
1506
 
                        if (keyseg->flag & HA_SPACE_PACK) {
1507
 
                                get_key_pack_length(a_length, pack_len, a);
1508
 
                                key_length = has_null + a_length + pack_len;
1509
 
                        }
1510
 
                        break;
1511
 
                case HA_KEYTYPE_BINARY:
1512
 
                        if (keyseg->flag & HA_SPACE_PACK) {
1513
 
                                get_key_pack_length(a_length, pack_len, a);
1514
 
                                key_length = has_null + a_length + pack_len;
1515
 
                        }
1516
 
                        break;
1517
 
                case HA_KEYTYPE_VARTEXT1:
1518
 
                case HA_KEYTYPE_VARTEXT2:
1519
 
                case HA_KEYTYPE_VARBINARY1:
1520
 
                case HA_KEYTYPE_VARBINARY2: {                           
1521
 
                        get_key_pack_length(a_length, pack_len, a);
1522
 
                        key_length = has_null + a_length + pack_len;
1523
 
                        break;
1524
 
                }
1525
 
#ifndef DRIZZLED
1526
 
                case HA_KEYTYPE_INT8:
1527
 
                case HA_KEYTYPE_SHORT_INT:
1528
 
                case HA_KEYTYPE_USHORT_INT:
1529
 
                case HA_KEYTYPE_INT24:
1530
 
                case HA_KEYTYPE_FLOAT:
1531
 
                case HA_KEYTYPE_UINT24:
1532
 
#endif          
1533
 
                case HA_KEYTYPE_LONG_INT:
1534
 
                case HA_KEYTYPE_ULONG_INT:
1535
 
                case HA_KEYTYPE_DOUBLE:
1536
 
                        break;
1537
 
#ifndef DRIZZLED
1538
 
                case HA_KEYTYPE_NUM: {
1539
 
                        /* Numeric key */
1540
 
                        if (keyseg->flag & HA_SPACE_PACK) {
1541
 
                                a_length = *a++;
1542
 
                                key_length = has_null + a_length + 1;
1543
 
                        }
1544
 
                        break;
1545
 
                }
1546
 
#endif
1547
 
#ifdef HAVE_LONG_LONG
1548
 
                case HA_KEYTYPE_LONGLONG:
1549
 
                case HA_KEYTYPE_ULONGLONG:
1550
 
                        break;
1551
 
#endif
1552
 
#ifndef DRIZZLED
1553
 
                case HA_KEYTYPE_BIT:
1554
 
                        /* TODO: What here? */
1555
 
                        break;
1556
 
#endif
1557
 
                case HA_KEYTYPE_END:                                                                                            /* Ready */
1558
 
                        break;
1559
 
        }
1560
 
 
1561
 
        return key_length;
1562
 
}
1563
 
 
1564
 
/*
1565
 
 * -----------------------------------------------------------------------
1566
 
 * Load and store rows
1567
 
 */
1568
 
 
1569
 
xtPublic xtWord4 myxt_store_row_length(XTOpenTablePtr ot, char *rec_buff)
1570
 
{
1571
 
        STRUCT_TABLE    *table = ot->ot_table->tab_dic.dic_my_table;
1572
 
        char                    *sdata;
1573
 
        xtWord4                 dlen;
1574
 
        xtWord4                 item_size;
1575
 
        xtWord4                 row_size = 0;
1576
 
 
1577
 
        for (Field* const *field=GET_TABLE_FIELDS(table); *field ; field++) {
1578
 
                if ((*field)->is_null_in_record((const uchar *) rec_buff)) {
1579
 
                        sdata = NULL;
1580
 
                        dlen = 0;
1581
 
                        item_size = 1;
1582
 
                }
1583
 
                else {
1584
 
                        sdata = mx_get_length_and_data(table, *field, rec_buff, &dlen);
1585
 
                        if (!dlen) {
1586
 
                                /* Empty, but not null (blobs may return NULL, when
1587
 
                                 * length is 0.
1588
 
                                 */
1589
 
                                sdata = rec_buff; // Any valid pointer will do
1590
 
                                item_size = 1 + dlen;
1591
 
                        }
1592
 
                        else if (dlen <= 240)
1593
 
                                item_size = 1 + dlen;
1594
 
                        else if (dlen <= 0xFFFF)
1595
 
                                item_size = 3 + dlen;
1596
 
                        else if (dlen <= 0xFFFFFF)
1597
 
                                item_size = 4 + dlen;
1598
 
                        else
1599
 
                                item_size = 5 + dlen;
1600
 
                }
1601
 
 
1602
 
                row_size += item_size;
1603
 
        }
1604
 
        return row_size;
1605
 
}
1606
 
 
1607
 
xtPublic xtWord4 myxt_store_row_data(XTOpenTablePtr ot, xtWord4 row_size, char *rec_buff)
1608
 
{
1609
 
        STRUCT_TABLE    *table = ot->ot_table->tab_dic.dic_my_table;
1610
 
        char                    *sdata;
1611
 
        xtWord4                 dlen;
1612
 
        xtWord4                 item_size;
1613
 
 
1614
 
        for (Field * const* field=GET_TABLE_FIELDS(table); *field; field++) {
1615
 
                if (mx_is_null_in_record(table, *field, rec_buff)) {
1616
 
                        sdata = NULL;
1617
 
                        dlen = 0;
1618
 
                        item_size = 1;
1619
 
                }
1620
 
                else {
1621
 
                        sdata = mx_get_length_and_data(table, *field, rec_buff, &dlen);
1622
 
                        if (!dlen) {
1623
 
                                /* Empty, but not null (blobs may return NULL, when
1624
 
                                 * length is 0.
1625
 
                                 */
1626
 
                                sdata = rec_buff; // Any valid pointer will do
1627
 
                                item_size = 1 + dlen;
1628
 
                        }
1629
 
                        else if (dlen <= 240)
1630
 
                                item_size = 1 + dlen;
1631
 
                        else if (dlen <= 0xFFFF)
1632
 
                                item_size = 3 + dlen;
1633
 
                        else if (dlen <= 0xFFFFFF)
1634
 
                                item_size = 4 + dlen;
1635
 
                        else
1636
 
                                item_size = 5 + dlen;
1637
 
                }
1638
 
 
1639
 
                if (row_size + item_size > ot->ot_row_wbuf_size) {
1640
 
                        if (!xt_realloc_ns((void **) &ot->ot_row_wbuffer, row_size + item_size))
1641
 
                                return 0;
1642
 
                        ot->ot_row_wbuf_size = row_size + item_size;
1643
 
                }
1644
 
 
1645
 
                if (!sdata)
1646
 
                        ot->ot_row_wbuffer[row_size] = 255;
1647
 
                else if (dlen <= 240) {
1648
 
                        ot->ot_row_wbuffer[row_size] = (unsigned char) dlen;
1649
 
                        memcpy(&ot->ot_row_wbuffer[row_size+1], sdata, dlen);
1650
 
                }
1651
 
                else if (dlen <= 0xFFFF) {
1652
 
                        ot->ot_row_wbuffer[row_size] = 254;
1653
 
                        XT_SET_DISK_2(&ot->ot_row_wbuffer[row_size+1], dlen);
1654
 
                        memcpy(&ot->ot_row_wbuffer[row_size+3], sdata, dlen);
1655
 
                }
1656
 
                else if (dlen <= 0xFFFFFF) {
1657
 
                        ot->ot_row_wbuffer[row_size] = 253;
1658
 
                        XT_SET_DISK_3(&ot->ot_row_wbuffer[row_size+1], dlen);
1659
 
                        memcpy(&ot->ot_row_wbuffer[row_size+4], sdata, dlen);
1660
 
                }
1661
 
                else {
1662
 
                        ot->ot_row_wbuffer[row_size] = 252;
1663
 
                        XT_SET_DISK_4(&ot->ot_row_wbuffer[row_size+1], dlen);
1664
 
                        memcpy(&ot->ot_row_wbuffer[row_size+5], sdata, dlen);
1665
 
                }
1666
 
 
1667
 
                row_size += item_size;
1668
 
        }
1669
 
        return row_size;
1670
 
}
1671
 
 
1672
 
/* Count the number and size of whole columns in the given buffer. */
1673
 
xtPublic size_t myxt_load_row_length(XTOpenTablePtr ot, size_t buffer_size, xtWord1 *source_buf, u_int *ret_col_cnt)
1674
 
{
1675
 
        u_int   col_cnt;
1676
 
        xtWord4 len;
1677
 
        size_t  size = 0;
1678
 
        u_int   i;
1679
 
 
1680
 
        col_cnt = ot->ot_table->tab_dic.dic_no_of_cols;
1681
 
        if (ret_col_cnt)
1682
 
                col_cnt = *ret_col_cnt;
1683
 
        for (i=0; i<col_cnt; i++) {
1684
 
                if (size + 1 > buffer_size)
1685
 
                        goto done;
1686
 
                switch (*source_buf) {
1687
 
                        case 255: // Indicate NULL value
1688
 
                                size++;
1689
 
                                source_buf++;
1690
 
                                break;
1691
 
                        case 254: // 2 bytes length
1692
 
                                if (size + 3 > buffer_size)
1693
 
                                        goto done;
1694
 
                                len = XT_GET_DISK_2(source_buf + 1);
1695
 
                                if (size + 3 + len > buffer_size)
1696
 
                                        goto done;
1697
 
                                size += 3 + len;
1698
 
                                source_buf += 3 + len;
1699
 
                                break;
1700
 
                        case 253: // 3 bytes length
1701
 
                                if (size + 4 > buffer_size)
1702
 
                                        goto done;
1703
 
                                len = XT_GET_DISK_3(source_buf + 1);
1704
 
                                if (size + 4 + len > buffer_size)
1705
 
                                        goto done;
1706
 
                                size += 4 + len;
1707
 
                                source_buf += 4 + len;
1708
 
                                break;
1709
 
                        case 252: // 4 bytes length
1710
 
                                if (size + 5 > buffer_size)
1711
 
                                        goto done;
1712
 
                                len = XT_GET_DISK_4(source_buf + 1);
1713
 
                                if (size + 5 + len > buffer_size)
1714
 
                                        goto done;
1715
 
                                size += 5 + len;
1716
 
                                source_buf += 5 + len;
1717
 
                                break;
1718
 
                        default: // Length byte
1719
 
                                len = *source_buf;
1720
 
                                if (size + 1 + len > buffer_size)
1721
 
                                        goto done;
1722
 
                                size += 1 + len;
1723
 
                                source_buf += 1 + len;
1724
 
                                break;
1725
 
                }
1726
 
        }
1727
 
        
1728
 
        done:
1729
 
        if (ret_col_cnt)
1730
 
                *ret_col_cnt = i;
1731
 
        return size;
1732
 
}
1733
 
 
1734
 
/* Unload from PBXT variable length format to the MySQL row format. */
1735
 
xtPublic xtWord4 myxt_load_row_data(XTOpenTablePtr ot, xtWord1 *source_buf, xtWord1 *dest_buff, u_int col_cnt)
1736
 
{
1737
 
        xtWord1                 *input_buf = source_buf;
1738
 
        STRUCT_TABLE    *table;
1739
 
        xtWord4                 len;
1740
 
        Field                   *curr_field;
1741
 
        xtBool                  is_null;
1742
 
        u_int                   i = 0;
1743
 
 
1744
 
        if (!(table = ot->ot_table->tab_dic.dic_my_table)) {
1745
 
                xt_register_taberr(XT_REG_CONTEXT, XT_ERR_NO_DICTIONARY, ot->ot_table->tab_name);
1746
 
                return 0;
1747
 
        }
1748
 
 
1749
 
        /* According to the InnoDB implementation:
1750
 
         * "MySQL assumes that all columns
1751
 
         * have the SQL NULL bit set unless it
1752
 
         * is a nullable column with a non-NULL value".
1753
 
         */
1754
 
        memset(dest_buff, 0xFF, GET_TABLE_SHARE(table)->null_bytes);
1755
 
        for (Field * const *field=GET_TABLE_FIELDS(table); *field && (!col_cnt || i<col_cnt); field++, i++) {
1756
 
                curr_field = *field;
1757
 
                is_null = FALSE;
1758
 
                switch (*source_buf) {
1759
 
                        case 255: // Indicate NULL value
1760
 
                                is_null = TRUE;
1761
 
                                len = 0;
1762
 
                                source_buf++;
1763
 
                                break;
1764
 
                        case 254: // 2 bytes length
1765
 
                                len = XT_GET_DISK_2(source_buf + 1);
1766
 
                                source_buf += 3;
1767
 
                                break;
1768
 
                        case 253: // 3 bytes length
1769
 
                                len = XT_GET_DISK_3(source_buf + 1);
1770
 
                                source_buf += 4;
1771
 
                                break;
1772
 
                        case 252: // 4 bytes length
1773
 
                                len = XT_GET_DISK_4(source_buf + 1);
1774
 
                                source_buf += 5;
1775
 
                                break;
1776
 
                        default: // Length byte
1777
 
                                if (*source_buf > 240) {
1778
 
                                        xt_register_xterr(XT_REG_CONTEXT, XT_ERR_BAD_RECORD_FORMAT);
1779
 
                                        return 0;
1780
 
                                }
1781
 
                                len = *source_buf;
1782
 
                                source_buf++;
1783
 
                                break;
1784
 
                }
1785
 
 
1786
 
                if (is_null)
1787
 
                        mx_set_length_and_data(table, curr_field, (char *) dest_buff, 0, NULL);
1788
 
                else
1789
 
                        mx_set_length_and_data(table, curr_field, (char *) dest_buff, len, (char *) source_buf);
1790
 
 
1791
 
                source_buf += len;
1792
 
        }
1793
 
        return (xtWord4) (source_buf - input_buf);
1794
 
}
1795
 
 
1796
 
xtPublic xtBool myxt_load_row(XTOpenTablePtr ot, xtWord1 *source_buf, xtWord1 *dest_buff, u_int col_cnt)
1797
 
{
1798
 
        return myxt_load_row_data(ot, source_buf, dest_buff, col_cnt) != 0;
1799
 
}
1800
 
 
1801
 
xtPublic xtBool myxt_find_column(XTOpenTablePtr ot, u_int *col_idx, const char *col_name)
1802
 
{
1803
 
        STRUCT_TABLE    *table = ot->ot_table->tab_dic.dic_my_table;
1804
 
        u_int                   i=0;
1805
 
 
1806
 
        for (Field * const *field=GET_TABLE_FIELDS(table); *field; field++, i++) {
1807
 
                if (!my_strcasecmp(system_charset_info, (*field)->field_name, col_name)) {
1808
 
                        *col_idx = i;
1809
 
                        return OK;
1810
 
                }
1811
 
        }
1812
 
        return FALSE;
1813
 
}
1814
 
 
1815
 
xtPublic void myxt_get_column_name(XTOpenTablePtr ot, u_int col_idx, u_int len, char *col_name)
1816
 
{
1817
 
        STRUCT_TABLE    *table = ot->ot_table->tab_dic.dic_my_table;
1818
 
        Field                   *field;
1819
 
 
1820
 
        field = GET_TABLE_FIELDS(table)[col_idx];
1821
 
        xt_strcpy(len, col_name, field->field_name);
1822
 
}
1823
 
 
1824
 
xtPublic void myxt_get_column_as_string(XTOpenTablePtr ot, char *buffer, u_int col_idx, u_int len, char *value)
1825
 
{
1826
 
        XTTableHPtr             tab = ot->ot_table;
1827
 
        XTThreadPtr             self = ot->ot_thread;
1828
 
        STRUCT_TABLE    *table = tab->tab_dic.dic_my_table;
1829
 
        Field                   *field = GET_TABLE_FIELDS(table)[col_idx];
1830
 
        char                    buf_val[MAX_FIELD_WIDTH];
1831
 
        String                  val(buf_val, sizeof(buf_val), &my_charset_bin);
1832
 
 
1833
 
        if (mx_is_null_in_record(table, field, buffer))
1834
 
                xt_strcpy(len, value, "NULL");
1835
 
        else {
1836
 
                byte    *save;
1837
 
 
1838
 
#ifndef DRIZZLED
1839
 
                /* Required by store() - or an assertion will fail: */
1840
 
                if (table->read_set)
1841
 
                        MX_BIT_SET(table->read_set, col_idx);
1842
 
#endif
1843
 
 
1844
 
                save = field->ptr;
1845
 
                xt_lock_mutex(self, &tab->tab_dic_field_lock);
1846
 
                pushr_(xt_unlock_mutex, &tab->tab_dic_field_lock);
1847
 
                field->ptr = (byte *) buffer + field->offset(table->getDefaultValues());
1848
 
                field->val_str_internal(&val);
1849
 
                field->ptr = save;                                      // Restore org row pointer
1850
 
                freer_(); // xt_unlock_mutex(&tab->tab_dic_field_lock)
1851
 
                xt_strcpy(len, value, val.c_ptr());
1852
 
        }
1853
 
}
1854
 
 
1855
 
xtPublic xtBool myxt_set_column(XTOpenTablePtr ot, char *buffer, u_int col_idx, const char *value, u_int len)
1856
 
{
1857
 
        XTTableHPtr             tab = ot->ot_table;
1858
 
        XTThreadPtr             self = ot->ot_thread;
1859
 
        STRUCT_TABLE    *table = tab->tab_dic.dic_my_table;
1860
 
        Field                   *field = GET_TABLE_FIELDS(table)[col_idx];
1861
 
        byte                    *save;
1862
 
        int                             error;
1863
 
 
1864
 
#ifndef DRIZZLED
1865
 
        /* Required by store() - or an assertion will fail: */
1866
 
        if (table->write_set)
1867
 
                MX_BIT_SET(table->write_set, col_idx);
1868
 
#endif
1869
 
 
1870
 
        mx_set_notnull_in_record(table, field, buffer);
1871
 
 
1872
 
        save = field->ptr;
1873
 
        xt_lock_mutex(self, &tab->tab_dic_field_lock);
1874
 
        pushr_(xt_unlock_mutex, &tab->tab_dic_field_lock);
1875
 
        field->ptr = (byte *) buffer + field->offset(table->getDefaultValues());
1876
 
        error = field->store(value, len, &my_charset_utf8_general_ci);
1877
 
        field->ptr = save;                                      // Restore org row pointer
1878
 
        freer_(); // xt_unlock_mutex(&tab->tab_dic_field_lock)
1879
 
        return error ? FAILED : OK;
1880
 
}
1881
 
 
1882
 
xtPublic void myxt_get_column_data(XTOpenTablePtr ot, char *buffer, u_int col_idx, char **value, size_t *len)
1883
 
{
1884
 
        STRUCT_TABLE    *table = ot->ot_table->tab_dic.dic_my_table;
1885
 
        Field                   *field = GET_TABLE_FIELDS(table)[col_idx];
1886
 
        char                    *sdata;
1887
 
        xtWord4                 dlen;
1888
 
 
1889
 
        sdata = mx_get_length_and_data(table, field, buffer, &dlen);
1890
 
        *value = sdata;
1891
 
        *len = dlen;
1892
 
}
1893
 
 
1894
 
xtPublic xtBool myxt_store_row(XTOpenTablePtr ot, XTTabRecInfoPtr rec_info, char *rec_buff)
1895
 
{
1896
 
        if (ot->ot_rec_fixed) {
1897
 
                rec_info->ri_fix_rec_buf = (XTTabRecFixDPtr) ot->ot_row_wbuffer;
1898
 
                rec_info->ri_rec_buf_size = ot->ot_rec_size;
1899
 
                rec_info->ri_ext_rec = NULL;
1900
 
 
1901
 
                rec_info->ri_fix_rec_buf->tr_rec_type_1 = XT_TAB_STATUS_FIXED;
1902
 
                memcpy(rec_info->ri_fix_rec_buf->rf_data, rec_buff, ot->ot_rec_size - XT_REC_FIX_HEADER_SIZE);
1903
 
        }
1904
 
        else {
1905
 
                xtWord4 row_size;
1906
 
 
1907
 
                if (!(row_size = myxt_store_row_data(ot, XT_REC_EXT_HEADER_SIZE, rec_buff)))
1908
 
                        return FAILED;
1909
 
                if (row_size - XT_REC_FIX_EXT_HEADER_DIFF <= ot->ot_rec_size) { 
1910
 
                        rec_info->ri_fix_rec_buf = (XTTabRecFixDPtr) &ot->ot_row_wbuffer[XT_REC_FIX_EXT_HEADER_DIFF];
1911
 
                        rec_info->ri_rec_buf_size = row_size - XT_REC_FIX_EXT_HEADER_DIFF;
1912
 
                        rec_info->ri_ext_rec = NULL;
1913
 
 
1914
 
                        rec_info->ri_fix_rec_buf->tr_rec_type_1 = XT_TAB_STATUS_VARIABLE;
1915
 
                }
1916
 
                else {
1917
 
                        rec_info->ri_fix_rec_buf = (XTTabRecFixDPtr) ot->ot_row_wbuffer;
1918
 
                        rec_info->ri_rec_buf_size = ot->ot_rec_size;
1919
 
                        rec_info->ri_ext_rec = (XTTabRecExtDPtr) ot->ot_row_wbuffer;
1920
 
                        rec_info->ri_log_data_size = row_size - ot->ot_rec_size;
1921
 
                        rec_info->ri_log_buf = (XTactExtRecEntryDPtr) &ot->ot_row_wbuffer[ot->ot_rec_size - offsetof(XTactExtRecEntryDRec, er_data)];
1922
 
 
1923
 
                        rec_info->ri_ext_rec->tr_rec_type_1 = XT_TAB_STATUS_EXT_DLOG;
1924
 
                        XT_SET_DISK_4(rec_info->ri_ext_rec->re_log_dat_siz_4, rec_info->ri_log_data_size);
1925
 
                }
1926
 
        }
1927
 
        return OK;
1928
 
}
1929
 
 
1930
 
static void mx_print_string(uchar *s, uint count)
1931
 
{
1932
 
        while (count > 0) {
1933
 
                if (s[count - 1] != ' ')
1934
 
                        break;
1935
 
                count--;
1936
 
        }
1937
 
        printf("\"");
1938
 
        for (u_int i=0; i<count; i++, s++)
1939
 
                printf("%c", *s);
1940
 
        printf("\"");
1941
 
}
1942
 
 
1943
 
xtPublic void myxt_print_key(XTIndexPtr ind, xtWord1 *key_value)
1944
 
{
1945
 
        register XTIndexSegRec  *keyseg = ind->mi_seg;
1946
 
        register uchar                  *b = (uchar *) key_value;
1947
 
        uint                                    b_length;
1948
 
        uint                                    pack_len;
1949
 
 
1950
 
        for (u_int i = 0; i < ind->mi_seg_count; i++, keyseg++) {
1951
 
                if (i!=0)
1952
 
                        printf(" ");
1953
 
                if (keyseg->null_bit) {
1954
 
                        if (!*b++) {
1955
 
                                printf("NULL");
1956
 
                                continue;
1957
 
                        }
1958
 
                }
1959
 
                switch ((enum ha_base_keytype) keyseg->type) {
1960
 
                        case HA_KEYTYPE_TEXT:                                                                                    /* Ascii; Key is converted */
1961
 
                                if (keyseg->flag & HA_SPACE_PACK) {
1962
 
                                        get_key_pack_length(b_length, pack_len, b);
1963
 
                                }
1964
 
                                else
1965
 
                                        b_length = keyseg->length;
1966
 
                                mx_print_string(b, b_length);
1967
 
                                b += b_length;
1968
 
                                break;
1969
 
                        case HA_KEYTYPE_LONG_INT: {
1970
 
                                int32_t l_2 = sint4korr(b);
1971
 
                                b += keyseg->length;
1972
 
                                printf("%ld", (long) l_2);
1973
 
                                break;
1974
 
                        }
1975
 
                        case HA_KEYTYPE_ULONG_INT: {
1976
 
                                xtWord4 u_2 = sint4korr(b);
1977
 
                                b += keyseg->length;
1978
 
                                printf("%lu", (u_long) u_2);
1979
 
                                break;
1980
 
                        }
1981
 
                        default:
1982
 
                                break;
1983
 
                }
1984
 
        }
1985
 
}
1986
 
 
1987
 
/*
1988
 
 * -----------------------------------------------------------------------
1989
 
 * MySQL Data Dictionary
1990
 
 */
1991
 
 
1992
 
static void my_close_table(STRUCT_TABLE *share)
1993
 
{
1994
 
        delete share;
1995
 
}
1996
 
 
1997
 
/*
1998
 
 * This function returns NULL if the table cannot be opened 
1999
 
 * because this is not a MySQL thread.
2000
 
 */ 
2001
 
static STRUCT_TABLE *my_open_table(XTThreadPtr self, XTDatabaseHPtr XT_UNUSED(db), XTPathStrPtr tab_path, xtWord1 table_type)
2002
 
{
2003
 
        THD                     *thd = current_thd;
2004
 
        char            *tab_file_name;
2005
 
        char            database_name[XT_IDENTIFIER_NAME_SIZE];
2006
 
        char            tab_name[XT_IDENTIFIER_NAME_SIZE];
2007
 
        uint32_t        tab_name_len;   
2008
 
        TableShare      *share;
2009
 
        int                     error;
2010
 
 
2011
 
        /* If we have no MySQL thread, then we cannot open this table!
2012
 
         * What this means is the thread is probably the sweeper or the
2013
 
         * compactor.
2014
 
         */
2015
 
        if (!thd)
2016
 
                return NULL;
2017
 
 
2018
 
        tab_file_name = xt_last_name_of_path(tab_path->ps_path);
2019
 
        tab_name_len = TableIdentifier::filename_to_tablename(tab_file_name, tab_name, XT_IDENTIFIER_NAME_SIZE);
2020
 
 
2021
 
        xt_2nd_last_name_of_path(XT_IDENTIFIER_NAME_SIZE, database_name, tab_path->ps_path);
2022
 
 
2023
 
        TableIdentifier *ident = NULL;
2024
 
        
2025
 
        if (table_type == XT_TABLE_TYPE_TEMPORARY) {
2026
 
                std::string tmp_path(drizzle_tmpdir);
2027
 
                tmp_path.append("/");
2028
 
                tmp_path.append(tab_file_name);
2029
 
                ident = new TableIdentifier(database_name, tab_name, tmp_path);
2030
 
        }
2031
 
        else if (table_type == XT_TABLE_TYPE_STANDARD) {
2032
 
                ident = new TableIdentifier(
2033
 
                        std::string(database_name), 
2034
 
                        std::string(tab_name, tab_name_len), 
2035
 
                        message::Table::STANDARD);
2036
 
        }
2037
 
        else {
2038
 
          fs::path n(getDataHomeCatalog());
2039
 
          n /= database_name;
2040
 
          n /= tab_file_name;
2041
 
                ident = new TableIdentifier(database_name, tab_name, n.file_string());
2042
 
        }
2043
 
        
2044
 
        share = new TableShare(message::Table::STANDARD);
2045
 
        if ((error = share->open_table_def(*thd, *ident))) {
2046
 
          xt_throw_sulxterr(XT_CONTEXT, XT_ERR_LOADING_MYSQL_DIC, tab_path->ps_path, (u_long) error);
2047
 
          delete ident;
2048
 
          return NULL;
2049
 
        }
2050
 
        delete ident;
2051
 
 
2052
 
        return share;
2053
 
}
2054
 
 
2055
 
/*
2056
 
static bool my_match_index(XTDDIndex *ind, KEY *index)
2057
 
{
2058
 
        KEY_PART_INFO   *key_part;
2059
 
        KEY_PART_INFO   *key_part_end;
2060
 
        u_int                   j;
2061
 
        XTDDColumnRef   *cref;
2062
 
 
2063
 
        if (index->key_parts != ind->co_cols.size())
2064
 
                return false;
2065
 
 
2066
 
        j=0;
2067
 
        key_part_end = index->key_part + index->key_parts;
2068
 
        for (key_part = index->key_part; key_part != key_part_end; key_part++, j++) {
2069
 
                if (!(cref = ind->co_cols.itemAt(j)))
2070
 
                        return false;
2071
 
                if (myxt_strcasecmp(cref->cr_col_name, (char *) key_part->field->field_name) != 0)
2072
 
                        return false;
2073
 
        }
2074
 
 
2075
 
        if (ind->co_type == XT_DD_KEY_PRIMARY) {
2076
 
                if (!(index->flags & HA_NOSAME))
2077
 
                        return false;
2078
 
        }
2079
 
        else {
2080
 
                if (ind->co_type == XT_DD_INDEX_UNIQUE) {
2081
 
                        if (!(index->flags & HA_NOSAME))
2082
 
                                return false;
2083
 
                }
2084
 
                if (ind->co_ind_name) {
2085
 
                        if (myxt_strcasecmp(ind->co_ind_name, index->name) != 0)
2086
 
                                return false;
2087
 
                }
2088
 
        }
2089
 
 
2090
 
        return true;
2091
 
}
2092
 
 
2093
 
static XTDDIndex *my_find_index(XTDDTable *dd_tab, KEY *index)
2094
 
{
2095
 
        XTDDIndex *ind;
2096
 
 
2097
 
        for (u_int i=0; i<dd_tab->dt_indexes.size(); i++)
2098
 
        {
2099
 
                ind = dd_tab->dt_indexes.itemAt(i);
2100
 
                if (my_match_index(ind, index))
2101
 
                        return ind;
2102
 
        }
2103
 
        return NULL;
2104
 
}
2105
 
*/
2106
 
 
2107
 
static void my_deref_index_data(struct XTThread *self, XTIndexPtr mi)
2108
 
{
2109
 
        enter_();
2110
 
        /* The dirty list of cache pages should be empty here! */
2111
 
        /* This is not the case if we were not able to flush data. E.g. when running out of disk space */
2112
 
        //ASSERT(!mi->mi_dirty_list);
2113
 
        ASSERT(!mi->mi_free_list);
2114
 
 
2115
 
        xt_spinlock_free(self, &mi->mi_dirty_lock);
2116
 
        XT_INDEX_FREE_LOCK(self, mi);
2117
 
#ifndef DRIZZLED
2118
 
        myxt_bitmap_free(self, &mi->mi_col_map);
2119
 
#endif
2120
 
        if (mi->mi_free_list)
2121
 
                xt_free(self, mi->mi_free_list);
2122
 
 
2123
 
        xt_free(self, mi);
2124
 
        exit_();
2125
 
}
2126
 
 
2127
 
static xtBool my_is_not_null_int4(XTIndexSegPtr seg)
2128
 
{
2129
 
        return (seg->type == HA_KEYTYPE_LONG_INT && !(seg->flag & HA_NULL_PART));
2130
 
}
2131
 
 
2132
 
/* MY_BITMAP definition in Drizzle does not like if
2133
 
 * I use a NULL pointer to calculate the offset!?
2134
 
 */
2135
 
#define MX_OFFSETOF(x, y)               ((size_t)(&((x *) 8)->y) - 8)
2136
 
 
2137
 
/* Derived from ha_myisam::create and mi_create */
2138
 
static XTIndexPtr my_create_index(XTThreadPtr self, STRUCT_TABLE *table_arg, u_int idx, KeyInfo *index)
2139
 
{
2140
 
        XTIndexPtr                              ind;
2141
 
        KeyPartInfo                     *key_part;
2142
 
        KeyPartInfo                     *key_part_end;
2143
 
        XTIndexSegRec                   *seg;
2144
 
        Field                                   *field;
2145
 
        enum ha_base_keytype    type;
2146
 
        uint                                    options = 0;
2147
 
        u_int                                   key_length = 0;
2148
 
        xtBool                                  partial_field;
2149
 
    MX_BITMAP               mi_col_map;
2150
 
 
2151
 
        enter_();
2152
 
 
2153
 
        pushsr_(ind, my_deref_index_data, (XTIndexPtr) xt_calloc(self, MX_OFFSETOF(XTIndexRec, mi_seg) + sizeof(XTIndexSegRec) * index->key_parts));
2154
 
 
2155
 
        XT_INDEX_INIT_LOCK(self, ind);
2156
 
        xt_spinlock_init_with_autoname(self, &ind->mi_dirty_lock);
2157
 
        ind->mi_index_no = idx;
2158
 
        ind->mi_flags = (index->flags & (HA_NOSAME | HA_NULL_ARE_EQUAL | HA_UNIQUE_CHECK));
2159
 
        //ind->mi_low_byte_first = TS(table_arg)->db_low_byte_first;
2160
 
        ind->mi_key_corrupted = FALSE;
2161
 
        ind->mi_fix_key = TRUE;
2162
 
        ind->mi_select_total = 0;
2163
 
        ind->mi_subset_of = 0;
2164
 
#ifdef DRIZZLED
2165
 
    mi_col_map.resize(GET_TABLE_SHARE(table_arg)->fields);      
2166
 
    mi_col_map.reset();
2167
 
    ind->mi_col_map= mi_col_map.to_ulong();
2168
 
    ind->mi_col_map_size= GET_TABLE_SHARE(table_arg)->fields;
2169
 
#else
2170
 
        myxt_bitmap_init(self, &ind->mi_col_map, GET_TABLE_SHARE(table_arg)->fields);
2171
 
#endif
2172
 
        
2173
 
        ind->mi_seg_count = (uint) index->key_parts;
2174
 
        key_part_end = index->key_part + index->key_parts;
2175
 
        seg = ind->mi_seg;
2176
 
        for (key_part = index->key_part; key_part != key_part_end; key_part++, seg++) {
2177
 
                partial_field = FALSE;
2178
 
                field = key_part->field;
2179
 
 
2180
 
                type = field->key_type();
2181
 
                seg->flag = key_part->key_part_flag;
2182
 
 
2183
 
                if (options & HA_OPTION_PACK_KEYS ||
2184
 
                        (index->flags & (HA_PACK_KEY | HA_BINARY_PACK_KEY | HA_SPACE_PACK_USED)))
2185
 
                {
2186
 
                        if (key_part->length > 8 && (type == HA_KEYTYPE_TEXT || 
2187
 
#ifndef DRIZZLED
2188
 
                                type == HA_KEYTYPE_NUM ||
2189
 
#endif
2190
 
                                (type == HA_KEYTYPE_BINARY && !field->zero_pack())))
2191
 
                        {
2192
 
                                /* No blobs here */
2193
 
                                if (key_part == index->key_part)
2194
 
                                        ind->mi_flags |= HA_PACK_KEY;
2195
 
#ifndef DRIZZLED
2196
 
                                if (!(field->flags & ZEROFILL_FLAG) &&
2197
 
                                        (field->type() == MYSQL_TYPE_STRING ||
2198
 
                                        field->type() == MYSQL_TYPE_VAR_STRING ||
2199
 
                                        ((int) (key_part->length - field->decimals())) >= 4))
2200
 
                                seg->flag |= HA_SPACE_PACK;
2201
 
#endif
2202
 
                        }
2203
 
                }
2204
 
 
2205
 
                seg->col_idx = field->position();
2206
 
                seg->is_recs_in_range = 1;
2207
 
                seg->is_selectivity = 1;
2208
 
                seg->type = (int) type;
2209
 
                seg->start = key_part->offset;
2210
 
                seg->length = key_part->length;
2211
 
                seg->bit_start = seg->bit_end = 0;
2212
 
                seg->bit_length = seg->bit_pos = 0;
2213
 
                seg->charset = field->charset();
2214
 
 
2215
 
                if (field->null_ptr) {
2216
 
                        key_length++;
2217
 
                        seg->flag |= HA_NULL_PART;
2218
 
                        seg->null_bit = field->null_bit;
2219
 
                        seg->null_pos = (uint) (field->null_ptr - (uchar*) table_arg->getDefaultValues());
2220
 
                }
2221
 
                else {
2222
 
                        seg->null_bit = 0;
2223
 
                        seg->null_pos = 0;
2224
 
                }
2225
 
 
2226
 
                if (field->real_type() == MYSQL_TYPE_ENUM
2227
 
#ifndef DRIZZLED
2228
 
                        || field->real_type() == MYSQL_TYPE_SET
2229
 
#endif
2230
 
                        ) {
2231
 
                        /* This values are not indexed as string!!
2232
 
                         * The index will not be built correctly if this value is non-NULL.
2233
 
                         */
2234
 
                        seg->charset = NULL;
2235
 
                }
2236
 
 
2237
 
                if (field->type() == MYSQL_TYPE_BLOB
2238
 
#ifndef DRIZZLED
2239
 
                        || field->type() == MYSQL_TYPE_GEOMETRY
2240
 
#endif
2241
 
                        ) {
2242
 
                        seg->flag |= HA_BLOB_PART;
2243
 
                        /* save number of bytes used to pack length */
2244
 
                        seg->bit_start = (uint) ((Field_blob *) field)->pack_length_no_ptr();
2245
 
                }
2246
 
#ifndef DRIZZLED
2247
 
                else if (field->type() == MYSQL_TYPE_BIT) {
2248
 
                        seg->bit_length = ((Field_bit *) field)->bit_len;
2249
 
                        seg->bit_start = ((Field_bit *) field)->bit_ofs;
2250
 
                        seg->bit_pos = (uint) (((Field_bit *) field)->bit_ptr - (uchar*) table_arg->getInsertRecord());
2251
 
                }
2252
 
#else
2253
 
                /* Drizzle uses HA_KEYTYPE_ULONG_INT keys for enums > 1 byte, which is not consistent with MySQL, so we fix it here  */
2254
 
                else if (field->type() == MYSQL_TYPE_ENUM) {
2255
 
                        switch (seg->length) {
2256
 
                                case 2: 
2257
 
#ifdef DRIZZLED
2258
 
                                        ASSERT_NS(FALSE);
2259
 
#else
2260
 
                                        seg->type = HA_KEYTYPE_USHORT_INT;
2261
 
                                        break;
2262
 
                                case 3:
2263
 
                                        seg->type = HA_KEYTYPE_UINT24;
2264
 
                                        break;
2265
 
#endif
2266
 
                        }
2267
 
                }
2268
 
#endif
2269
 
 
2270
 
                switch (seg->type) {
2271
 
                        case HA_KEYTYPE_VARTEXT1:
2272
 
                        case HA_KEYTYPE_VARTEXT2:
2273
 
                        case HA_KEYTYPE_VARBINARY1:
2274
 
                        case HA_KEYTYPE_VARBINARY2:
2275
 
                                if (!(seg->flag & HA_BLOB_PART)) {
2276
 
                                        /* Make a flag that this is a VARCHAR */
2277
 
                                        seg->flag |= HA_VAR_LENGTH_PART;
2278
 
                                        /* Store in bit_start number of bytes used to pack the length */
2279
 
                                        seg->bit_start = ((seg->type == HA_KEYTYPE_VARTEXT1 || seg->type == HA_KEYTYPE_VARBINARY1) ? 1 : 2);
2280
 
                                }
2281
 
                                break;
2282
 
                }
2283
 
 
2284
 
                /* All packed fields start with a length (1 or 3 bytes): */
2285
 
                if (seg->flag & (HA_VAR_LENGTH_PART | HA_BLOB_PART | HA_SPACE_PACK)) {
2286
 
                        key_length++;                           /* At least one length byte */
2287
 
                        if (seg->length >= 255) /* prefix may be 3 bytes */
2288
 
                        key_length +=2;
2289
 
                }
2290
 
 
2291
 
                key_length += seg->length;
2292
 
                if (seg->length > 40)
2293
 
                        ind->mi_fix_key = FALSE;
2294
 
 
2295
 
                /* Determine if only part of the field is in the key:
2296
 
                 * This is important for index coverage!
2297
 
                 * Note, BLOB fields are never retrieved from
2298
 
                 * an index!
2299
 
                 */
2300
 
                if (field->type() == MYSQL_TYPE_BLOB)
2301
 
                        partial_field = TRUE;
2302
 
                else if (field->real_type() == MYSQL_TYPE_VARCHAR               // For varbinary type
2303
 
#ifndef DRIZZLED
2304
 
                        || field->real_type() == MYSQL_TYPE_VAR_STRING          // For varbinary type
2305
 
                        || field->real_type() == MYSQL_TYPE_STRING                      // For binary type
2306
 
#endif
2307
 
                        )
2308
 
                {
2309
 
                        Field   *tab_field = GET_TABLE_FIELDS(table_arg)[key_part->fieldnr-1];
2310
 
                        u_int   field_len = tab_field->key_length();
2311
 
 
2312
 
                        if (key_part->length != field_len)
2313
 
                                partial_field = TRUE;
2314
 
                }
2315
 
 
2316
 
                /* NOTE: do not set if the field is only partially in the index!!! */
2317
 
                if (!partial_field)
2318
 
#ifdef DRIZZLED
2319
 
                        MX_BIT_FAST_TEST_AND_SET(&mi_col_map, field->position());
2320
 
#else
2321
 
                        MX_BIT_FAST_TEST_AND_SET(&ind->mi_col_map, field->position());
2322
 
#endif
2323
 
        }
2324
 
 
2325
 
        if (key_length > XT_INDEX_MAX_KEY_SIZE)
2326
 
                xt_throw_sulxterr(XT_CONTEXT, XT_ERR_KEY_TOO_LARGE, index->name, (u_long) XT_INDEX_MAX_KEY_SIZE);
2327
 
 
2328
 
        /* This is the maximum size of the index on disk: */
2329
 
        ind->mi_key_size = key_length;
2330
 
        ind->mi_max_items = (XT_INDEX_PAGE_SIZE-2) / (key_length+XT_RECORD_REF_SIZE);
2331
 
 
2332
 
        if (ind->mi_fix_key) {
2333
 
                /* Special case for not-NULL 4 byte int value: */
2334
 
                switch (ind->mi_seg_count) {
2335
 
                        case 1:
2336
 
                                ind->mi_single_type = ind->mi_seg[0].type;
2337
 
                                if (ind->mi_seg[0].type == HA_KEYTYPE_LONG_INT ||
2338
 
                                        ind->mi_seg[0].type == HA_KEYTYPE_ULONG_INT) {
2339
 
                                        if (!(ind->mi_seg[0].flag & HA_NULL_PART))
2340
 
                                                ind->mi_scan_branch = xt_scan_branch_single;
2341
 
                                }
2342
 
                                break;
2343
 
                        case 2:
2344
 
                                if (my_is_not_null_int4(&ind->mi_seg[0]) &&
2345
 
                                        my_is_not_null_int4(&ind->mi_seg[1])) {
2346
 
                                        ind->mi_scan_branch = xt_scan_branch_fix_simple;
2347
 
                                        ind->mi_simple_comp_key = xt_compare_2_int4;
2348
 
                                }
2349
 
                                break;
2350
 
                        case 3:
2351
 
                                if (my_is_not_null_int4(&ind->mi_seg[0]) &&
2352
 
                                        my_is_not_null_int4(&ind->mi_seg[1]) &&
2353
 
                                        my_is_not_null_int4(&ind->mi_seg[2])) {
2354
 
                                        ind->mi_scan_branch = xt_scan_branch_fix_simple;
2355
 
                                        ind->mi_simple_comp_key = xt_compare_3_int4;
2356
 
                                }
2357
 
                                break;
2358
 
                }
2359
 
                if (!ind->mi_scan_branch)
2360
 
                        ind->mi_scan_branch = xt_scan_branch_fix;
2361
 
                ind->mi_prev_item = xt_prev_branch_item_fix;
2362
 
                ind->mi_last_item = xt_last_branch_item_fix;
2363
 
        }
2364
 
        else {
2365
 
                ind->mi_scan_branch = xt_scan_branch_var;
2366
 
                ind->mi_prev_item = xt_prev_branch_item_var;
2367
 
                ind->mi_last_item = xt_last_branch_item_var;
2368
 
        }
2369
 
        ind->mi_lazy_delete = ind->mi_fix_key && ind->mi_max_items >= 4;
2370
 
 
2371
 
        XT_NODE_ID(ind->mi_root) = 0;
2372
 
 
2373
 
        popr_(); // Discard my_deref_index_data(ind)
2374
 
 
2375
 
        return_(ind);
2376
 
}
2377
 
 
2378
 
/* We estimate the size of BLOBs depending on the number
2379
 
 * of BLOBs in the table.
2380
 
 */
2381
 
static u_int mx_blob_field_size_total[] = {
2382
 
        500,    // 1
2383
 
        400,    // 2
2384
 
        350,    // 3
2385
 
        320,    // 4
2386
 
        300,    // 5
2387
 
        280,    // 6
2388
 
        260,    // 7
2389
 
        240,    // 8
2390
 
        220,    // 9
2391
 
        210             // 10
2392
 
};
2393
 
 
2394
 
static u_int mxvarchar_field_min_ave[] = {
2395
 
        120,    // 1
2396
 
        105,    // 2
2397
 
        90,             // 3
2398
 
        65,             // 4
2399
 
        50,             // 5
2400
 
        40,             // 6
2401
 
        40,             // 7
2402
 
        40,             // 8
2403
 
        40,             // 9
2404
 
        40              // 10
2405
 
};
2406
 
 
2407
 
xtPublic void myxt_setup_dictionary(XTThreadPtr self, XTDictionaryPtr dic)
2408
 
{
2409
 
        STRUCT_TABLE    *my_tab = dic->dic_my_table;
2410
 
        u_int   field_count;
2411
 
        u_int   var_field_count = 0;
2412
 
        u_int   varchar_field_count = 0;
2413
 
        u_int   blob_field_count = 0;
2414
 
        u_int   large_blob_field_count = 0;
2415
 
        xtWord8 min_data_size = 0;
2416
 
        xtWord8 max_data_size = 0;
2417
 
        xtWord8 ave_data_size = 0;
2418
 
        xtWord8 min_row_size = 0;
2419
 
        xtWord8 max_row_size = 0;
2420
 
        xtWord8 ave_row_size = 0;
2421
 
        xtWord8 min_ave_row_size = 0;
2422
 
        xtWord8 max_ave_row_size = 0;
2423
 
        u_int   dic_rec_size;
2424
 
        xtBool  dic_rec_fixed;
2425
 
        Field   *curr_field;
2426
 
        Field * const *field;
2427
 
 
2428
 
        /* How many columns are required for all indexes. */
2429
 
        KeyInfo                         *index;
2430
 
        KeyPartInfo     *key_part;
2431
 
        KeyPartInfo     *key_part_end;
2432
 
 
2433
 
#ifndef XT_USE_LAZY_DELETE
2434
 
        dic->dic_no_lazy_delete = TRUE;
2435
 
#endif
2436
 
 
2437
 
        dic->dic_ind_cols_req = 0;
2438
 
        for (uint i=0; i<GET_TABLE_SHARE(my_tab)->keys; i++) {
2439
 
                index = &my_tab->getKeyInfo(i);
2440
 
 
2441
 
                key_part_end = index->key_part + index->key_parts;
2442
 
                for (key_part = index->key_part; key_part != key_part_end; key_part++) {
2443
 
                        curr_field = key_part->field;
2444
 
 
2445
 
                        if ((u_int) curr_field->position()+1 > dic->dic_ind_cols_req)
2446
 
                                dic->dic_ind_cols_req = curr_field->position()+1;
2447
 
                }
2448
 
        }
2449
 
 
2450
 
        /* We will work out how many columns are required for all blobs: */
2451
 
        dic->dic_blob_cols_req = 0;     
2452
 
        field_count = 0;
2453
 
        for (field=GET_TABLE_FIELDS(my_tab); (curr_field = *field); field++) {
2454
 
                field_count++;
2455
 
                min_data_size = curr_field->key_length();
2456
 
                max_data_size = curr_field->key_length();
2457
 
                enum_field_types tno = curr_field->type();
2458
 
 
2459
 
                min_ave_row_size = 40;
2460
 
                max_ave_row_size = 128;
2461
 
                if (tno == MYSQL_TYPE_BLOB) {
2462
 
                        blob_field_count++;
2463
 
                        min_data_size = 0;
2464
 
                        max_data_size = ((Field_blob *) curr_field)->max_data_length();
2465
 
                        /* Set the average length higher for BLOBs: */
2466
 
                        if (max_data_size == 0xFFFF ||
2467
 
                                max_data_size == 0xFFFFFF) {
2468
 
                                if (large_blob_field_count < 10)
2469
 
                                        max_ave_row_size = mx_blob_field_size_total[large_blob_field_count];
2470
 
                                else
2471
 
                                        max_ave_row_size = 200;
2472
 
                                large_blob_field_count++;
2473
 
                        }
2474
 
                        else if (max_data_size == 0xFFFFFFFF) {
2475
 
                                /* Scale the estimated size of the blob depending on how many BLOBs
2476
 
                                 * are in the table!
2477
 
                                 */
2478
 
                                if (large_blob_field_count < 10)
2479
 
                                        max_ave_row_size = mx_blob_field_size_total[large_blob_field_count];
2480
 
                                else
2481
 
                                        max_ave_row_size = 200;
2482
 
                                large_blob_field_count++;
2483
 
                                if ((u_int) curr_field->position()+1 > dic->dic_blob_cols_req)
2484
 
                                        dic->dic_blob_cols_req = curr_field->position()+1;
2485
 
                                dic->dic_blob_count++;
2486
 
                                xt_realloc(self, (void **) &dic->dic_blob_cols, sizeof(Field *) * dic->dic_blob_count);
2487
 
                                dic->dic_blob_cols[dic->dic_blob_count-1] = curr_field;
2488
 
                        }
2489
 
                }
2490
 
                else if (tno == MYSQL_TYPE_VARCHAR
2491
 
#ifndef DRIZZLED
2492
 
                        || tno == MYSQL_TYPE_VAR_STRING
2493
 
#endif
2494
 
                        ) {
2495
 
                        /* GOTCHA: MYSQL_TYPE_VAR_STRING does not exist as MYSQL_TYPE_VARCHAR define, but
2496
 
                         * is used when creating a table with
2497
 
                         * VARCHAR()
2498
 
                         */
2499
 
                        min_data_size = 0;
2500
 
                        if (varchar_field_count < 10)
2501
 
                                min_ave_row_size = mxvarchar_field_min_ave[varchar_field_count];
2502
 
                        else
2503
 
                                min_ave_row_size = 40;
2504
 
                        varchar_field_count++;
2505
 
                }
2506
 
 
2507
 
                if (max_data_size == min_data_size)
2508
 
                        ave_data_size = max_data_size;
2509
 
                else {
2510
 
                        var_field_count++;
2511
 
                        /* Take the average a 25% of the maximum: */
2512
 
                        ave_data_size = max_data_size / 4;
2513
 
 
2514
 
                        /* Set the average based on min and max parameters: */
2515
 
                        if (ave_data_size < min_ave_row_size)
2516
 
                                ave_data_size = min_ave_row_size;
2517
 
                        else if (ave_data_size > max_ave_row_size)
2518
 
                                ave_data_size = max_ave_row_size;
2519
 
 
2520
 
                        if (ave_data_size > max_data_size)
2521
 
                                ave_data_size = max_data_size;
2522
 
                }
2523
 
 
2524
 
                /* Add space for the length indicators: */
2525
 
                if (min_data_size <= 240)
2526
 
                        min_row_size += 1 + min_data_size;
2527
 
                else if (min_data_size <= 0xFFFF)
2528
 
                        min_row_size += 3 + min_data_size;
2529
 
                else if (min_data_size <= 0xFFFFFF)
2530
 
                        min_row_size += 4 + min_data_size;
2531
 
                else
2532
 
                        min_row_size += 5 + min_data_size;
2533
 
 
2534
 
                if (max_data_size <= 240)
2535
 
                        max_row_size += 1 + max_data_size;
2536
 
                else if (max_data_size <= 0xFFFF)
2537
 
                        max_row_size += 3 + max_data_size;
2538
 
                else if (max_data_size <= 0xFFFFFF)
2539
 
                        max_row_size += 4 + max_data_size;
2540
 
                else
2541
 
                        max_row_size += 5 + max_data_size;
2542
 
 
2543
 
                if (ave_data_size <= 240)
2544
 
                        ave_row_size += 1 + ave_data_size;
2545
 
                else /* Should not be more than this! */
2546
 
                        ave_row_size += 3 + ave_data_size;
2547
 
 
2548
 
                /* This is the length of the record required for all indexes: */
2549
 
                /* This was calculated incorrectly. Not a serius bug because it
2550
 
                 * is only used in the case of fixed length row, and in this
2551
 
                 * case the dic_ind_rec_len is set correctly below.
2552
 
                 */
2553
 
                if (field_count == dic->dic_ind_cols_req)
2554
 
                        dic->dic_ind_rec_len = max_row_size;
2555
 
        }
2556
 
 
2557
 
        dic->dic_min_row_size = min_row_size;
2558
 
        dic->dic_max_row_size = max_row_size;
2559
 
        dic->dic_ave_row_size = ave_row_size;
2560
 
        dic->dic_no_of_cols = field_count;
2561
 
 
2562
 
        if (dic->dic_def_ave_row_size) {
2563
 
                /* The average row size has been set: */
2564
 
                dic_rec_size = offsetof(XTTabRecFix, rf_data) + GET_TABLE_SHARE(my_tab)->getRecordLength();
2565
 
 
2566
 
                /* The conditions for a fixed record are: */
2567
 
                if (dic->dic_def_ave_row_size >= (xtWord8) GET_TABLE_SHARE(my_tab)->getRecordLength() &&
2568
 
                        dic_rec_size <= XT_TAB_MAX_FIX_REC_LENGTH &&
2569
 
                        !blob_field_count) {
2570
 
                        dic_rec_fixed = TRUE;
2571
 
                }
2572
 
                else {
2573
 
                        xtWord8 new_rec_size;
2574
 
 
2575
 
                        dic_rec_fixed = FALSE;
2576
 
                        if (dic->dic_def_ave_row_size > max_row_size)
2577
 
                                new_rec_size = offsetof(XTTabRecFix, rf_data) + max_row_size;
2578
 
                        else
2579
 
                                new_rec_size = offsetof(XTTabRecFix, rf_data) + dic->dic_def_ave_row_size;
2580
 
 
2581
 
                        /* The maximum record size 64K for explicit AVG_ROW_LENGTH! */
2582
 
                        if (new_rec_size > XT_TAB_MAX_FIX_REC_LENGTH_SPEC)
2583
 
                                new_rec_size = XT_TAB_MAX_FIX_REC_LENGTH_SPEC;
2584
 
 
2585
 
                        dic_rec_size = (u_int) new_rec_size;
2586
 
                }
2587
 
        }
2588
 
        else {
2589
 
                /* If the average size is within 10% if of the maximum size, then we
2590
 
                 * we handle these rows as fixed size rows.
2591
 
                 * Fixed size rows use the internal MySQL format.
2592
 
                 */
2593
 
                dic_rec_size = offsetof(XTTabRecFix, rf_data) + GET_TABLE_SHARE(my_tab)->getRecordLength();
2594
 
                /* Fixed length records must be less than 16K in size,
2595
 
                 * have an average size which is very close (20%) to the maximum size or
2596
 
                 * be less than a minimum size,
2597
 
                 * and not contain any BLOBs:
2598
 
                 */
2599
 
                if (dic_rec_size <= XT_TAB_MAX_FIX_REC_LENGTH &&
2600
 
                        (ave_row_size + ave_row_size / 4 >= max_row_size ||
2601
 
                        dic_rec_size < XT_TAB_MIN_VAR_REC_LENGTH) &&
2602
 
                        !blob_field_count) {
2603
 
                        dic_rec_fixed = TRUE;
2604
 
                }
2605
 
                else {
2606
 
                        dic_rec_fixed = FALSE;
2607
 
                        /* Note I add offsetof(XTTabRecFix, rf_data) insteard of
2608
 
                         * offsetof(XTTabRecExt, re_data) here!
2609
 
                         * The reason is that, we want to include the average size
2610
 
                         * record in the fixed data part. To do this we only need to
2611
 
                         * calculate a fixed header size, because in the cases in which
2612
 
                         * it fits, we will only be using a fixed header!
2613
 
                         */
2614
 
                        dic_rec_size = (u_int) (offsetof(XTTabRecFix, rf_data) + ave_row_size);
2615
 
                        /* The maximum record size (16K for autorow sizing)! */
2616
 
                        if (dic_rec_size > XT_TAB_MAX_FIX_REC_LENGTH)
2617
 
                                dic_rec_size = XT_TAB_MAX_FIX_REC_LENGTH;
2618
 
                }
2619
 
        }
2620
 
 
2621
 
        /* Ensure that handle data record size is big enough to
2622
 
         * include the extended record reference, in the case of
2623
 
         * variable length rows
2624
 
         */
2625
 
        if (!dic_rec_fixed) {
2626
 
                if (dic_rec_size < offsetof(XTTabRecExtDRec, re_data))
2627
 
                        dic_rec_size = offsetof(XTTabRecExtDRec, re_data);
2628
 
        }
2629
 
#ifdef DEBUG
2630
 
        else {
2631
 
                ASSERT_NS(dic_rec_size > offsetof(XTTabRecFix, rf_data));
2632
 
        }
2633
 
#endif
2634
 
 
2635
 
        if (!dic->dic_rec_size) {
2636
 
                dic->dic_rec_size = dic_rec_size;
2637
 
                dic->dic_rec_fixed = dic_rec_fixed;
2638
 
        }
2639
 
        else {
2640
 
                /* This just confirms that our original calculation on
2641
 
                 * create table agrees with the current calculation.
2642
 
                 * (i.e. if non-zero values were loaded from the table).
2643
 
                 *
2644
 
                 * It may be the criteria for calculating the data record size
2645
 
                 * and whether to used a fixed or variable record has changed,
2646
 
                 * but we need to stick to the current physical layout of the
2647
 
                 * table.
2648
 
                 *
2649
 
                 * Note that this can occur in rename table when the
2650
 
                 * method of calculation has changed.
2651
 
                 *
2652
 
                 * On rename, the format of the table does not change, so we
2653
 
                 * will not take the calculated values.
2654
 
                 */
2655
 
                //ASSERT(dic->dic_rec_size == dic_rec_size);
2656
 
                //ASSERT(dic->dic_rec_fixed == dic_rec_fixed);
2657
 
        }
2658
 
 
2659
 
        if (dic_rec_fixed) {
2660
 
                /* Recalculate the length of the required required to address all
2661
 
                 * index columns!
2662
 
                 */              
2663
 
                if (field_count == dic->dic_ind_cols_req)
2664
 
                        dic->dic_ind_rec_len = GET_TABLE_SHARE(my_tab)->getRecordLength();
2665
 
                else {
2666
 
                        field=GET_TABLE_FIELDS(my_tab);
2667
 
                        
2668
 
                        curr_field = field[dic->dic_ind_cols_req];
2669
 
                        dic->dic_ind_rec_len = curr_field->offset(my_tab->getDefaultValues());
2670
 
                }
2671
 
        }
2672
 
 
2673
 
        /* We now calculate how many of the first columns in the row
2674
 
         * will definitely fit into the buffer, when the record is
2675
 
         * of type extended.
2676
 
         *
2677
 
         * In this way we can figure out if we need to load the extended
2678
 
         * record at all.
2679
 
         */
2680
 
        dic->dic_fix_col_count = 0;
2681
 
        if (!dic_rec_fixed) {
2682
 
                xtWord8 max_rec_size = offsetof(XTTabRecExt, re_data);
2683
 
 
2684
 
                for (Field * const *f=GET_TABLE_FIELDS(my_tab); (curr_field = *f); f++) {
2685
 
                        max_data_size = curr_field->key_length();
2686
 
                        enum_field_types tno = curr_field->type();
2687
 
                        if (tno == MYSQL_TYPE_BLOB)
2688
 
                                max_data_size = ((Field_blob *) curr_field)->max_data_length();
2689
 
                        if (max_data_size <= 240)
2690
 
                                max_rec_size += 1 + max_data_size;
2691
 
                        else if (max_data_size <= 0xFFFF)
2692
 
                                max_rec_size += 3 + max_data_size;
2693
 
                        else if (max_data_size <= 0xFFFFFF)
2694
 
                                max_rec_size += 4 + max_data_size;
2695
 
                        else
2696
 
                                max_rec_size += 5 + max_data_size;
2697
 
                        if (max_rec_size > (xtWord8) dic_rec_size)
2698
 
                                break;
2699
 
                        dic->dic_fix_col_count++;
2700
 
                }               
2701
 
                ASSERT(dic->dic_fix_col_count < dic->dic_no_of_cols);
2702
 
        }
2703
 
 
2704
 
        dic->dic_key_count = GET_TABLE_SHARE(my_tab)->keys;
2705
 
        dic->dic_mysql_buf_size = GET_TABLE_SHARE(my_tab)->rec_buff_length;
2706
 
        dic->dic_mysql_rec_size = GET_TABLE_SHARE(my_tab)->getRecordLength();
2707
 
}
2708
 
 
2709
 
static u_int my_get_best_superset(XTThreadPtr XT_UNUSED(self), XTDictionaryPtr dic, XTIndexPtr ind)
2710
 
{
2711
 
        XTIndexPtr      super_ind;
2712
 
        u_int           super = 0;
2713
 
        u_int           super_seg_count = ind->mi_seg_count;
2714
 
 
2715
 
        for (u_int i=0; i<dic->dic_key_count; i++) {
2716
 
                super_ind = dic->dic_keys[i];
2717
 
                if (ind->mi_index_no != super_ind->mi_index_no &&
2718
 
                        super_seg_count < super_ind->mi_seg_count) {
2719
 
                        for (u_int j=0; j<ind->mi_seg_count; j++) {
2720
 
                                if (ind->mi_seg[j].col_idx != super_ind->mi_seg[j].col_idx)
2721
 
                                        goto next;
2722
 
                        }
2723
 
                        super_seg_count = super_ind->mi_seg_count;
2724
 
                        super = i+1;
2725
 
                        next:;
2726
 
                }
2727
 
        }
2728
 
        return super;
2729
 
}
2730
 
 
2731
 
/*
2732
 
 * Return FAILED if the MySQL dictionary is not available.
2733
 
 */
2734
 
xtPublic xtBool myxt_load_dictionary(XTThreadPtr self, XTDictionaryPtr dic, XTDatabaseHPtr db, XTPathStrPtr tab_path)
2735
 
{
2736
 
        STRUCT_TABLE *my_tab;
2737
 
 
2738
 
        if (!(my_tab = my_open_table(self, db, tab_path, dic->dic_table_type)))
2739
 
                return FAILED;
2740
 
        dic->dic_my_table = my_tab;
2741
 
#ifdef DRIZZLED
2742
 
        dic->dic_def_ave_row_size = (xtWord8) GET_TABLE_SHARE(my_tab)->getTableProto()->options().avg_row_length();
2743
 
#else
2744
 
        dic->dic_def_ave_row_size = (xtWord8) GET_TABLE_SHARE(my_tab)->avg_row_length;
2745
 
#endif
2746
 
        myxt_setup_dictionary(self, dic);
2747
 
        dic->dic_keys = (XTIndexPtr *) xt_calloc(self, sizeof(XTIndexPtr) * GET_TABLE_SHARE(my_tab)->keys);
2748
 
        for (uint i=0; i<GET_TABLE_SHARE(my_tab)->keys; i++)
2749
 
                dic->dic_keys[i] = my_create_index(self, my_tab, i, &my_tab->getKeyInfo(i));
2750
 
 
2751
 
        /* Check if any key is a subset of another: */
2752
 
        for (u_int i=0; i<dic->dic_key_count; i++)
2753
 
                dic->dic_keys[i]->mi_subset_of = my_get_best_superset(self, dic, dic->dic_keys[i]);
2754
 
 
2755
 
        return OK;
2756
 
}
2757
 
 
2758
 
xtPublic void myxt_free_dictionary(XTThreadPtr self, XTDictionaryPtr dic)
2759
 
{
2760
 
        if (dic->dic_table) {
2761
 
                dic->dic_table->release(self);
2762
 
                dic->dic_table = NULL;
2763
 
        }
2764
 
 
2765
 
        if (dic->dic_my_table) {
2766
 
                my_close_table(dic->dic_my_table);
2767
 
                dic->dic_my_table = NULL;
2768
 
        }
2769
 
 
2770
 
        if (dic->dic_blob_cols) {
2771
 
                xt_free(self, dic->dic_blob_cols);
2772
 
                dic->dic_blob_cols = NULL;
2773
 
        }
2774
 
        dic->dic_blob_count = 0;
2775
 
 
2776
 
        /* If we have opened a table, then this data is freed with the dictionary: */
2777
 
        if (dic->dic_keys) {
2778
 
                for (uint i=0; i<dic->dic_key_count; i++) {
2779
 
                        if (dic->dic_keys[i])
2780
 
                                my_deref_index_data(self, (XTIndexPtr) dic->dic_keys[i]);
2781
 
                }
2782
 
                xt_free(self, dic->dic_keys);
2783
 
                dic->dic_key_count = 0;
2784
 
                dic->dic_keys = NULL;
2785
 
        }
2786
 
}
2787
 
 
2788
 
xtPublic void myxt_move_dictionary(XTDictionaryPtr dic, XTDictionaryPtr source_dic)
2789
 
{
2790
 
        dic->dic_my_table = source_dic->dic_my_table;
2791
 
        source_dic->dic_my_table = NULL;
2792
 
 
2793
 
        if (!dic->dic_rec_size) {
2794
 
                dic->dic_rec_size = source_dic->dic_rec_size;
2795
 
                dic->dic_rec_fixed = source_dic->dic_rec_fixed;
2796
 
        }
2797
 
        else {
2798
 
                /* This just confirms that our original calculation on
2799
 
                 * create table agrees with the current calculation.
2800
 
                 * (i.e. if non-zero values were loaded from the table).
2801
 
                 *
2802
 
                 * It may be the criteria for calculating the data record size
2803
 
                 * and whether to used a fixed or variable record has changed,
2804
 
                 * but we need to stick to the current physical layout of the
2805
 
                 * table.
2806
 
                 */
2807
 
                ASSERT_NS(dic->dic_rec_size == source_dic->dic_rec_size);
2808
 
                ASSERT_NS(dic->dic_rec_fixed == source_dic->dic_rec_fixed);
2809
 
        }
2810
 
 
2811
 
        dic->dic_tab_flags = source_dic->dic_tab_flags;
2812
 
        dic->dic_blob_cols_req = source_dic->dic_blob_cols_req;
2813
 
        dic->dic_blob_count = source_dic->dic_blob_count;
2814
 
        dic->dic_blob_cols = source_dic->dic_blob_cols;
2815
 
        source_dic->dic_blob_cols = NULL;
2816
 
 
2817
 
        dic->dic_mysql_buf_size = source_dic->dic_mysql_buf_size;
2818
 
        dic->dic_mysql_rec_size = source_dic->dic_mysql_rec_size;
2819
 
        dic->dic_key_count = source_dic->dic_key_count;
2820
 
        dic->dic_keys = source_dic->dic_keys;
2821
 
 
2822
 
        /* Set this to zero, bcause later xt_flush_tables() may be called. 
2823
 
         * This can occur when using the BLOB streaming engine,
2824
 
         * in command ALTER TABLE x ENGINE = PBXT;
2825
 
         */
2826
 
        source_dic->dic_key_count = 0;
2827
 
        source_dic->dic_keys = NULL;
2828
 
 
2829
 
        dic->dic_min_row_size = source_dic->dic_min_row_size;
2830
 
        dic->dic_max_row_size = source_dic->dic_max_row_size;
2831
 
        dic->dic_ave_row_size = source_dic->dic_ave_row_size;
2832
 
        dic->dic_def_ave_row_size = source_dic->dic_def_ave_row_size;
2833
 
 
2834
 
        dic->dic_no_of_cols = source_dic->dic_no_of_cols;
2835
 
        dic->dic_fix_col_count = source_dic->dic_fix_col_count;
2836
 
        dic->dic_ind_cols_req = source_dic->dic_ind_cols_req;
2837
 
        dic->dic_ind_rec_len = source_dic->dic_ind_rec_len;
2838
 
}
2839
 
 
2840
 
static void my_free_dd_table(XTThreadPtr self, XTDDTable *dd_tab)
2841
 
{
2842
 
        if (dd_tab)
2843
 
                dd_tab->release(self);
2844
 
}
2845
 
 
2846
 
static void ha_create_dd_index(XTThreadPtr self, XTDDIndex *ind, KeyInfo *key)
2847
 
{
2848
 
        KeyPartInfo     *key_part;
2849
 
        KeyPartInfo     *key_part_end;
2850
 
        XTDDColumnRef   *cref;
2851
 
 
2852
 
        if (strcmp(key->name, "PRIMARY") == 0)
2853
 
                ind->co_type = XT_DD_KEY_PRIMARY;
2854
 
        else if (key->flags & HA_NOSAME)
2855
 
                ind->co_type = XT_DD_INDEX_UNIQUE;
2856
 
        else
2857
 
                ind->co_type = XT_DD_INDEX;
2858
 
 
2859
 
        if (ind->co_type == XT_DD_KEY_PRIMARY)
2860
 
                ind->co_name = xt_dup_string(self, key->name);
2861
 
        else
2862
 
                ind->co_ind_name = xt_dup_string(self, key->name);
2863
 
 
2864
 
        key_part_end = key->key_part + key->key_parts;
2865
 
        for (key_part = key->key_part; key_part != key_part_end; key_part++) {
2866
 
                if (!(cref = new XTDDColumnRef()))
2867
 
                        xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
2868
 
                cref->init(self);
2869
 
                ind->co_cols.append(self, cref);
2870
 
                cref->cr_col_name = xt_dup_string(self, (char *) key_part->field->field_name);
2871
 
        }
2872
 
}
2873
 
 
2874
 
static char *my_type_to_string(XTThreadPtr self, Field *field, STRUCT_TABLE *XT_UNUSED(my_tab))
2875
 
{
2876
 
        char            buffer[MAX_FIELD_WIDTH + 400];
2877
 
        const char      *ptr;
2878
 
        String          type((char *) buffer, sizeof(buffer), system_charset_info);
2879
 
        xtWord4         len;
2880
 
 
2881
 
        /* GOTCHA:
2882
 
         * - Above sets the string length to the same as the buffer,
2883
 
         *   so we must set the length to zero.
2884
 
         * - The result is not necessarilly zero terminated.
2885
 
         * - We cannot assume that the input buffer is the one
2886
 
         *   we get back (for example text field).
2887
 
         */
2888
 
        type.length(0);
2889
 
        field->sql_type(type);
2890
 
        ptr = type.ptr();
2891
 
        len = type.length();
2892
 
 
2893
 
        if (len >= sizeof(buffer))
2894
 
                len = sizeof(buffer)-1;
2895
 
 
2896
 
        if (ptr != buffer)
2897
 
                xt_strcpy(sizeof(buffer), buffer, ptr);
2898
 
 
2899
 
        buffer[len] = 0;
2900
 
                        
2901
 
        if (field->has_charset()) {
2902
 
                /* Always include the charset so that we can compare types
2903
 
                 * for FK/PK releations.
2904
 
                 */
2905
 
                xt_strcat(sizeof(buffer), buffer, " CHARACTER SET ");
2906
 
                xt_strcat(sizeof(buffer), buffer, (char *) field->charset()->csname);
2907
 
 
2908
 
                /* For string types dump collation name only if 
2909
 
                 * collation is not primary for the given charset
2910
 
                 */
2911
 
                if (!(field->charset()->state & MY_CS_PRIMARY)) {
2912
 
                        xt_strcat(sizeof(buffer), buffer, " COLLATE ");
2913
 
                        xt_strcat(sizeof(buffer), buffer, (char *) field->charset()->name);
2914
 
                }
2915
 
        }
2916
 
 
2917
 
        return xt_dup_string(self, buffer); // type.length()
2918
 
}
2919
 
 
2920
 
xtPublic XTDDTable *myxt_create_table_from_table(XTThreadPtr self, STRUCT_TABLE *my_tab)
2921
 
{
2922
 
        XTDDTable               *dd_tab;
2923
 
        Field                   *curr_field;
2924
 
        XTDDColumn              *col;
2925
 
        XTDDIndex               *ind;
2926
 
 
2927
 
        if (!(dd_tab = new XTDDTable()))
2928
 
                xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
2929
 
        dd_tab->init(self);
2930
 
        pushr_(my_free_dd_table, dd_tab);
2931
 
 
2932
 
        for (Field * const *field=GET_TABLE_FIELDS(my_tab); (curr_field = *field); field++) {
2933
 
                col = XTDDColumnFactory::createFromMySQLField(self, my_tab, curr_field);
2934
 
                dd_tab->dt_cols.append(self, col);
2935
 
        }
2936
 
 
2937
 
        for (uint i=0; i<GET_TABLE_SHARE(my_tab)->keys; i++) {
2938
 
                if (!(ind = (XTDDIndex *) new XTDDIndex(XT_DD_UNKNOWN)))
2939
 
                        xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
2940
 
                dd_tab->dt_indexes.append(self, ind);
2941
 
                ind->co_table = dd_tab;
2942
 
                ind->in_index = i;
2943
 
                ha_create_dd_index(self, ind, &my_tab->getKeyInfo(i));
2944
 
        }
2945
 
 
2946
 
        popr_(); // my_free_dd_table(dd_tab)
2947
 
        return dd_tab;
2948
 
}
2949
 
 
2950
 
/*
2951
 
 * -----------------------------------------------------------------------
2952
 
 * MySQL CHARACTER UTILITIES
2953
 
 */
2954
 
 
2955
 
xtPublic void myxt_static_convert_identifier(XTThreadPtr XT_UNUSED(self), MX_CONST_CHARSET_INFO *cs, char *from, char *to, size_t to_len)
2956
 
{
2957
 
#ifdef DRIZZLED
2958
 
        ((void)cs);
2959
 
         xt_strcpy(to_len, to, from);
2960
 
#else
2961
 
        uint errors;
2962
 
 
2963
 
        /*
2964
 
         * Bug#4417
2965
 
         * Check that identifiers and strings are not converted 
2966
 
         * when the client character set is binary.
2967
 
         */
2968
 
        if (cs == &my_charset_utf8_general_ci || cs == &my_charset_bin)
2969
 
                xt_strcpy(to_len, to, from);
2970
 
        else
2971
 
                strconvert(cs, from, &my_charset_utf8_general_ci, to, to_len, &errors);
2972
 
#endif
2973
 
}
2974
 
 
2975
 
// cs == current_thd->charset()
2976
 
xtPublic char *myxt_convert_identifier(XTThreadPtr self, MX_CONST_CHARSET_INFO *cs, char *from)
2977
 
{
2978
 
#ifdef DRIZZLED
2979
 
        char *to = xt_dup_string(self, from);
2980
 
        ((void)cs);
2981
 
#else
2982
 
        uint    errors;
2983
 
        u_int   len;
2984
 
        char    *to;
2985
 
 
2986
 
        if (cs == &my_charset_utf8_general_ci || cs == &my_charset_bin)
2987
 
                to = xt_dup_string(self, from);
2988
 
        else {
2989
 
                len = strlen(from) * 3 + 1;
2990
 
                to = (char *) xt_malloc(self, len);
2991
 
                strconvert(cs, from, &my_charset_utf8_general_ci, to, len, &errors);
2992
 
        }
2993
 
#endif
2994
 
        return to;
2995
 
}
2996
 
 
2997
 
xtPublic char *myxt_convert_table_name(XTThreadPtr self, char *from)
2998
 
{
2999
 
        u_int   len;
3000
 
        char    *to;
3001
 
 
3002
 
        len = strlen(from) * 5 + 1;
3003
 
        to = (char *) xt_malloc(self, len);
3004
 
        tablename_to_filename(from, to, len);
3005
 
        return to;
3006
 
}
3007
 
 
3008
 
/*
3009
 
 * This works because if you create a table
3010
 
 * with a '#' in it, MySQL will translate it
3011
 
 * to @0023 in the file name.
3012
 
 */
3013
 
xtPublic xtBool myxt_temp_table_name(const char *table)
3014
 
{
3015
 
        char    *name;
3016
 
        xtBool  yup = FALSE;
3017
 
 
3018
 
        name = xt_last_name_of_path(table);
3019
 
#ifdef DRIZZED
3020
 
        yup = (strncmp(name, "#sql", 4) == 0);
3021
 
#else
3022
 
        if (*name == '#')
3023
 
                yup = (strncmp(name, "#sql-", 5) == 0) || (strncmp(name, "#sql2-", 6) == 0);
3024
 
#endif
3025
 
        return yup;
3026
 
}
3027
 
 
3028
 
xtPublic void myxt_static_convert_table_name(XTThreadPtr XT_UNUSED(self), char *from, char *to, size_t to_len)
3029
 
{
3030
 
        tablename_to_filename(from, to, to_len);
3031
 
}
3032
 
 
3033
 
xtPublic void myxt_static_convert_file_name(char *from, char *to, size_t to_len)
3034
 
{
3035
 
        uint32_t len = TableIdentifier::filename_to_tablename(from, to, to_len);
3036
 
        if (len >= to_len)
3037
 
                len = to_len-1;
3038
 
        to[len] = 0;
3039
 
}
3040
 
 
3041
 
xtPublic int myxt_strcasecmp(char * a, char *b)
3042
 
{
3043
 
        return my_strcasecmp(&my_charset_utf8_general_ci, a, b);
3044
 
}
3045
 
 
3046
 
xtPublic int myxt_isspace(MX_CONST_CHARSET_INFO *cs, char a)
3047
 
{
3048
 
        return my_isspace(cs, a);
3049
 
}
3050
 
 
3051
 
xtPublic int myxt_ispunct(MX_CONST_CHARSET_INFO *cs, char a)
3052
 
{
3053
 
        return my_ispunct(cs, a);
3054
 
}
3055
 
 
3056
 
xtPublic int myxt_isdigit(MX_CONST_CHARSET_INFO *cs, char a)
3057
 
{
3058
 
        return my_isdigit(cs, a);
3059
 
}
3060
 
 
3061
 
xtPublic MX_CONST_CHARSET_INFO *myxt_getcharset(bool convert)
3062
 
{
3063
 
        if (convert) {
3064
 
                THD *thd = current_thd;
3065
 
 
3066
 
                if (thd)
3067
 
                        return (MX_CHARSET_INFO *)thd->charset();
3068
 
        }
3069
 
        return (MX_CHARSET_INFO *)&my_charset_utf8_general_ci;
3070
 
}
3071
 
 
3072
 
xtPublic xtBool myxt_create_thread_possible()
3073
 
{
3074
 
#ifndef DRIZZLED
3075
 
        if (!global_system_variables.table_plugin) {
3076
 
                xt_register_xterr(XT_REG_CONTEXT, XT_ERR_MYSQL_NO_THREAD);
3077
 
                return FAILED;
3078
 
        }
3079
 
#endif
3080
 
        return OK;
3081
 
}
3082
 
 
3083
 
xtPublic void *myxt_create_thread()
3084
 
{
3085
 
#ifdef DRIZZLED
3086
 
        Client          *client;
3087
 
        Session         *session;
3088
 
 
3089
 
        if (drizzled::internal::my_thread_init()) {
3090
 
                xt_register_error(XT_REG_CONTEXT, XT_ERR_MYSQL_ERROR, 0, "Unable to initialize MySQL threading");
3091
 
                return NULL;
3092
 
        }
3093
 
 
3094
 
        if (!(client = new NullClient()))
3095
 
                return NULL;
3096
 
        session = new Session(client);
3097
 
        session->thread_stack = (char *) &session;
3098
 
        session->storeGlobals();
3099
 
        return (void *) session;
3100
 
#else
3101
 
        THD *new_thd;
3102
 
 
3103
 
        if (my_thread_init()) {
3104
 
                xt_register_error(XT_REG_CONTEXT, XT_ERR_MYSQL_ERROR, 0, "Unable to initialize MySQL threading");
3105
 
                return NULL;
3106
 
        }
3107
 
 
3108
 
        /*
3109
 
         * Unfortunately, if PBXT is the default engine, and we are shutting down
3110
 
         * then global_system_variables.table_plugin may be NULL. Which will cause
3111
 
         * a crash if we try to create a thread!
3112
 
         *
3113
 
         * The following call in plugin_shutdown() sets the global reference
3114
 
         * to NULL:
3115
 
         *
3116
 
         * unlock_variables(NULL, &global_system_variables);
3117
 
         *
3118
 
         * Later plugin_deinitialize() is called.
3119
 
         *
3120
 
         * The following stack is an example crash which occurs when I call
3121
 
         * myxt_create_thread() in ha_exit(), to force the error.
3122
 
         *
3123
 
         *   if (pi->state & (PLUGIN_IS_READY | PLUGIN_IS_UNINITIALIZED))
3124
 
         *   pi is NULL!
3125
 
         * #0   0x002ff684 in intern_plugin_lock at sql_plugin.cc:617
3126
 
         * #1   0x0030296d in plugin_thdvar_init at sql_plugin.cc:2432
3127
 
         * #2   0x000db4a4 in THD::init at sql_class.cc:756
3128
 
         * #3   0x000e02ed in THD::THD at sql_class.cc:638
3129
 
         * #4   0x00e2678d in myxt_create_thread at myxt_xt.cc:2990
3130
 
         * #5   0x00e05d43 in ha_exit at ha_pbxt.cc:1011
3131
 
         * #6   0x00e065c2 in pbxt_end at ha_pbxt.cc:1330
3132
 
         * #7   0x00e065df in pbxt_panic at ha_pbxt.cc:1343
3133
 
         * #8   0x0023e57d in ha_finalize_handlerton at handler.cc:392
3134
 
         * #9   0x002ffc8b in plugin_deinitialize at sql_plugin.cc:816
3135
 
         * #10  0x003037d9 in plugin_shutdown at sql_plugin.cc:1572
3136
 
         * #11  0x000f7b2b in clean_up at mysqld.cc:1266
3137
 
         * #12  0x000f7fca in unireg_end at mysqld.cc:1192
3138
 
         * #13  0x000fa021 in kill_server at mysqld.cc:1134
3139
 
         * #14  0x000fa6df in kill_server_thread at mysqld.cc:1155
3140
 
         * #15  0x91fdb155 in _pthread_start
3141
 
         * #16  0x91fdb012 in thread_start
3142
 
         */
3143
 
        if (!global_system_variables.table_plugin) {
3144
 
                xt_register_xterr(XT_REG_CONTEXT, XT_ERR_MYSQL_NO_THREAD);
3145
 
                return NULL;
3146
 
        }
3147
 
 
3148
 
        if (!(new_thd = new THD)) {
3149
 
                my_thread_end();
3150
 
                xt_register_error(XT_REG_CONTEXT, XT_ERR_MYSQL_ERROR, 0, "Unable to create MySQL thread (THD)");
3151
 
                return NULL;
3152
 
        }
3153
 
 
3154
 
        /*
3155
 
         * If PBXT is the default storage engine, then creating any THD objects will add extra 
3156
 
         * references to the PBXT plugin object. because the threads are created but PBXT
3157
 
         * this creates a self reference, and the reference count does not go to zero
3158
 
         * on shutdown.
3159
 
         *
3160
 
         * The server then issues a message that it is forcing shutdown of the plugin.
3161
 
         *
3162
 
         * However, the engine reference is not required by the THDs used by PBXT, so 
3163
 
         * I just remove them here.
3164
 
         */
3165
 
        plugin_unlock(NULL, new_thd->variables.table_plugin);
3166
 
        new_thd->variables.table_plugin = NULL;
3167
 
 
3168
 
        new_thd->thread_stack = (char *) &new_thd;
3169
 
        new_thd->store_globals();
3170
 
        lex_start(new_thd);
3171
 
 
3172
 
        return (void *) new_thd;
3173
 
#endif
3174
 
}
3175
 
 
3176
 
#ifdef DRIZZLED
3177
 
xtPublic void myxt_destroy_thread(void *s, xtBool end_threads)
3178
 
{
3179
 
        Session *session = (Session *) s;
3180
 
 
3181
 
        delete session;
3182
 
 
3183
 
        if (end_threads)
3184
 
          drizzled::internal::my_thread_end();
3185
 
}
3186
 
#else
3187
 
xtPublic void myxt_destroy_thread(void *thread, xtBool end_threads)
3188
 
{
3189
 
        THD *thd = (THD *) thread;
3190
 
 
3191
 
#if MYSQL_VERSION_ID > 60005
3192
 
        /* PMC - This is a HACK! It is required because
3193
 
         * MySQL shuts down MDL before shutting down the
3194
 
         * plug-ins.
3195
 
         */
3196
 
        if (!pbxt_inited)
3197
 
                mdl_init();
3198
 
        close_thread_tables(thd);
3199
 
        if (!pbxt_inited)
3200
 
                mdl_destroy();
3201
 
#else
3202
 
        close_thread_tables(thd);
3203
 
#endif
3204
 
        
3205
 
        delete thd;
3206
 
 
3207
 
        /* Remember that we don't have a THD */
3208
 
        my_pthread_setspecific_ptr(THR_THD, 0);
3209
 
 
3210
 
        if (end_threads)
3211
 
                my_thread_end();
3212
 
}
3213
 
#endif
3214
 
 
3215
 
xtPublic void myxt_delete_remaining_thread()
3216
 
{
3217
 
        THD *thd;
3218
 
 
3219
 
        if ((thd = current_thd))
3220
 
                myxt_destroy_thread((void *) thd, TRUE);
3221
 
}
3222
 
 
3223
 
xtPublic XTThreadPtr myxt_get_self()
3224
 
{
3225
 
        THD *thd;
3226
 
        
3227
 
        if ((thd = current_thd))
3228
 
                return xt_ha_thd_to_self(thd);
3229
 
        return NULL;
3230
 
}
3231
 
 
3232
 
/*
3233
 
 * -----------------------------------------------------------------------
3234
 
 * INFORMATION SCHEMA FUNCTIONS
3235
 
 *
3236
 
 */
3237
 
 
3238
 
#ifndef DRIZZLED
3239
 
static int mx_put_record(THD *thd, TABLE *table)
3240
 
{
3241
 
        return schema_table_store_record(thd, table);
3242
 
}
3243
 
 
3244
 
#ifdef UNUSED_CODE
3245
 
static void mx_put_int(TABLE *table, int column, int value)
3246
 
{
3247
 
        GET_TABLE_FIELDS(table)[column]->store(value, false);
3248
 
}
3249
 
 
3250
 
static void mx_put_real8(TABLE *table, int column, xtReal8 value)
3251
 
{
3252
 
        GET_TABLE_FIELDS(table)[column]->store(value);
3253
 
}
3254
 
 
3255
 
static void mx_put_string(TABLE *table, int column, const char *string, u_int len, charset_info_st *charset)
3256
 
{
3257
 
        GET_TABLE_FIELDS(table)[column]->store(string, len, charset);
3258
 
}
3259
 
#endif
3260
 
 
3261
 
static void mx_put_u_llong(TABLE *table, int column, u_llong value)
3262
 
{
3263
 
        GET_TABLE_FIELDS(table)[column]->store(value, false);
3264
 
}
3265
 
 
3266
 
static void mx_put_string(TABLE *table, int column, const char *string, charset_info_st *charset)
3267
 
{
3268
 
        GET_TABLE_FIELDS(table)[column]->store(string, strlen(string), charset);
3269
 
}
3270
 
 
3271
 
xtPublic int myxt_statistics_fill_table(XTThreadPtr self, void *th, void *ta, void *, MX_CONST void *ch)
3272
 
{
3273
 
        THD                             *thd = (THD *) th;
3274
 
        TABLE_LIST              *tables = (TABLE_LIST *) ta;
3275
 
        charset_info_st *charset = (charset_info_st *) ch;
3276
 
        TABLE                   *table = (TABLE *) tables->table;
3277
 
        int                             err = 0;
3278
 
        int                             col;
3279
 
        const char              *stat_name;
3280
 
        u_llong                 stat_value;
3281
 
        XTStatisticsRec statistics;
3282
 
        XTDatabaseHPtr  db = self->st_database;
3283
 
 
3284
 
        xt_gather_statistics(&statistics);
3285
 
        for (u_int rec_id=0; !err && rec_id<XT_STAT_CURRENT_MAX; rec_id++) {
3286
 
                stat_name = xt_get_stat_meta_data(rec_id)->sm_name;
3287
 
                stat_value = xt_get_statistic(&statistics, db, rec_id);
3288
 
 
3289
 
                col=0;
3290
 
                mx_put_u_llong(table, col++, rec_id+1);
3291
 
                mx_put_string(table, col++, stat_name, charset);
3292
 
                mx_put_u_llong(table, col++, stat_value);
3293
 
                err = mx_put_record(thd, table);
3294
 
        }
3295
 
 
3296
 
        return err;
3297
 
}
3298
 
#endif // !DRIZZLED
3299
 
 
3300
 
xtPublic void myxt_get_status(XTThreadPtr self, XTStringBufferPtr strbuf)
3301
 
{
3302
 
        char string[200];
3303
 
 
3304
 
        xt_sb_concat(self, strbuf, "\n");
3305
 
        xt_get_now(string, 200);
3306
 
        xt_sb_concat(self, strbuf, string);
3307
 
        xt_sb_concat(self, strbuf, " PBXT ");
3308
 
        xt_sb_concat(self, strbuf, xt_get_version());
3309
 
        xt_sb_concat(self, strbuf, " STATUS OUTPUT");
3310
 
        xt_sb_concat(self, strbuf, "\n");
3311
 
 
3312
 
        xt_sb_concat(self, strbuf, "Record cache usage: ");
3313
 
        xt_sb_concat_int8(self, strbuf, xt_tc_get_usage());
3314
 
        xt_sb_concat(self, strbuf, "\n");
3315
 
        xt_sb_concat(self, strbuf, "Record cache size:  ");
3316
 
        xt_sb_concat_int8(self, strbuf, xt_tc_get_size());
3317
 
        xt_sb_concat(self, strbuf, "\n");
3318
 
        xt_sb_concat(self, strbuf, "Record cache high:  ");
3319
 
        xt_sb_concat_int8(self, strbuf, xt_tc_get_high());
3320
 
        xt_sb_concat(self, strbuf, "\n");
3321
 
        xt_sb_concat(self, strbuf, "Index cache usage:  ");
3322
 
        xt_sb_concat_int8(self, strbuf, xt_ind_get_usage());
3323
 
        xt_sb_concat(self, strbuf, "\n");
3324
 
        xt_sb_concat(self, strbuf, "Index cache size:   ");
3325
 
        xt_sb_concat_int8(self, strbuf, xt_ind_get_size());
3326
 
        xt_sb_concat(self, strbuf, "\n");
3327
 
        xt_sb_concat(self, strbuf, "Log cache usage:    ");
3328
 
        xt_sb_concat_int8(self, strbuf, xt_xlog_get_usage());
3329
 
        xt_sb_concat(self, strbuf, "\n");
3330
 
        xt_sb_concat(self, strbuf, "Log cache size:     ");
3331
 
        xt_sb_concat_int8(self, strbuf, xt_xlog_get_size());
3332
 
        xt_sb_concat(self, strbuf, "\n");
3333
 
 
3334
 
        xt_ht_lock(self, xt_db_open_databases);
3335
 
        pushr_(xt_ht_unlock, xt_db_open_databases);
3336
 
 
3337
 
        XTDatabaseHPtr  *dbptr;
3338
 
        size_t len = xt_sl_get_size(xt_db_open_db_by_id);
3339
 
 
3340
 
        if (len > 0) {
3341
 
                xt_sb_concat(self, strbuf, "Data log files:\n");
3342
 
                for (u_int i=0; i<len; i++) {
3343
 
                        dbptr = (XTDatabaseHPtr *) xt_sl_item_at(xt_db_open_db_by_id, i);
3344
 
                        
3345
 
#ifndef XT_USE_GLOBAL_DB
3346
 
                        xt_sb_concat(self, strbuf, "Database: ");
3347
 
                        xt_sb_concat(self, strbuf, (*dbptr)->db_name);
3348
 
                        xt_sb_concat(self, strbuf, "\n");
3349
 
#endif
3350
 
                        xt_dl_log_status(self, *dbptr, strbuf);
3351
 
                }
3352
 
        }
3353
 
        else
3354
 
                xt_sb_concat(self, strbuf, "No data logs in use\n");
3355
 
 
3356
 
        freer_(); // xt_ht_unlock(xt_db_open_databases)
3357
 
}
3358
 
 
3359
 
/*
3360
 
 * -----------------------------------------------------------------------
3361
 
 * MySQL Bit Maps
3362
 
 */
3363
 
 
3364
 
static void myxt_bitmap_init(XTThreadPtr self, MX_BITMAP *map, u_int n_bits)
3365
 
{
3366
 
#ifdef DRIZZLED
3367
 
    (void) self;
3368
 
    map= new boost::dynamic_bitset<>(n_bits);
3369
 
    map->resize(n_bits);
3370
 
    map->reset();
3371
 
#else
3372
 
        my_bitmap_map   *buf;
3373
 
    uint                        size_in_bytes = (((n_bits) + 31) / 32) * 4;
3374
 
        buf = (my_bitmap_map *) xt_malloc(self, size_in_bytes);
3375
 
        map->bitmap= buf;
3376
 
        map->n_bits= n_bits;
3377
 
        create_last_word_mask(map);
3378
 
        bitmap_clear_all(map);
3379
 
#endif
3380
 
}
3381
 
 
3382
 
static void myxt_bitmap_free(XTThreadPtr self, MX_BITMAP *map)
3383
 
{
3384
 
#ifdef DRIZZLED
3385
 
    (void) self;
3386
 
    if (map->empty())
3387
 
        map->clear();
3388
 
    delete map;
3389
 
#else
3390
 
        if (map->bitmap) {
3391
 
                xt_free(self, map->bitmap);
3392
 
                map->bitmap = NULL;
3393
 
        }
3394
 
#endif
3395
 
}
3396
 
 
3397
 
/*
3398
 
 * -----------------------------------------------------------------------
3399
 
 * XTDDColumnFactory methods
3400
 
 */
3401
 
 
3402
 
XTDDColumn *XTDDColumnFactory::createFromMySQLField(XTThread *self, STRUCT_TABLE *my_tab, Field *field)
3403
 
{
3404
 
        XTDDEnumerableColumn *en_col;
3405
 
        XTDDColumn *col;
3406
 
        xtBool is_enum = FALSE;
3407
 
 
3408
 
        switch(field->real_type()) {
3409
 
                case MYSQL_TYPE_ENUM:
3410
 
                        is_enum = TRUE;
3411
 
                        /* fallthrough */
3412
 
 
3413
 
#ifndef DRIZZLED
3414
 
                case MYSQL_TYPE_SET:
3415
 
#endif
3416
 
                        col = en_col = new XTDDEnumerableColumn();
3417
 
                    if (!col)
3418
 
                                xt_throw_errno(XT_CONTEXT, XT_ENOMEM); 
3419
 
                        col->init(self);
3420
 
                        en_col->enum_size = ((Field_enum *)field)->typelib->count;
3421
 
                        en_col->is_enum = is_enum;
3422
 
                        break;
3423
 
 
3424
 
                default:
3425
 
                        col = new XTDDColumn();
3426
 
                        if (!col)
3427
 
                                xt_throw_errno(XT_CONTEXT, XT_ENOMEM); 
3428
 
                        col->init(self);
3429
 
        }
3430
 
 
3431
 
        col->dc_name = xt_dup_string(self, (char *) field->field_name);
3432
 
        col->dc_data_type = my_type_to_string(self, field, my_tab);
3433
 
        col->dc_null_ok = field->null_ptr != NULL;
3434
 
 
3435
 
        return col;
3436
 
}
3437
 
 
3438
 
/*
3439
 
 * -----------------------------------------------------------------------
3440
 
 * utilities
3441
 
 */
3442
 
 
3443
 
/*
3444
 
 * MySQL (not sure about Drizzle) first calls hton->init and then assigns the plugin a thread slot
3445
 
 * which is used by xt_get_self(). This is a problem as pbxt_init() starts a number of daemon threads
3446
 
 * which could try to use the slot before it is assigned. This code waits till slot is inited.
3447
 
 * We cannot directly check hton->slot as in some versions of MySQL it can be 0 before init which is a 
3448
 
 * valid value.
3449
 
 */
3450
 
extern ulong total_ha;
3451
 
 
3452
 
xtPublic void myxt_wait_pbxt_plugin_slot_assigned(XTThread *self)
3453
 
{
3454
 
#ifdef DRIZZLED
3455
 
        static std::string plugin_name("PBXT");
3456
 
 
3457
 
        while (!self->t_quit && !module::Registry::singleton().find(plugin_name))
3458
 
                xt_sleep_milli_second(1);
3459
 
#else
3460
 
        while(!self->t_quit && (pbxt_hton->slot >= total_ha))
3461
 
                xt_sleep_milli_second(1);
3462
 
#endif
3463
 
}