myxt_xt.cc 91.4 KB
Newer Older
Paul McCullagh's avatar
Paul McCullagh committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/* Copyright (c) 2005 PrimeBase Technologies GmbH
 *
 * PrimeBase XT
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * 2006-05-16	Paul McCullagh
 *
 * H&G2JCtL
 *
 * These functions implement the parts of PBXT which must conform to the
 * key and row format used by MySQL. 
 */

#include "xt_config.h"

#ifdef DRIZZLED
#include <drizzled/server_includes.h>
#include <drizzled/plugin.h>
#include <drizzled/show.h>
#include <drizzled/field/blob.h>
#include <drizzled/field/enum.h>
#include <drizzled/field/varstring.h>
#include <drizzled/current_session.h>
#include <drizzled/sql_lex.h>
#include <drizzled/session.h>
39
//extern "C" struct charset_info_st *session_charset(Session *session);
Paul McCullagh's avatar
Paul McCullagh committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
extern pthread_key_t THR_Session;
#else
#include "mysql_priv.h"
#include <mysql/plugin.h>
#endif

#ifdef HAVE_ISNAN
#include <math.h>
#endif

#include "ha_pbxt.h"

#include "myxt_xt.h"
#include "strutil_xt.h"
#include "database_xt.h"
#include "cache_xt.h"
#include "datalog_xt.h"

58 59 60
static void		myxt_bitmap_init(XTThreadPtr self, MX_BITMAP *map, u_int n_bits);
static void		myxt_bitmap_free(XTThreadPtr self, MX_BITMAP *map);

Paul McCullagh's avatar
Paul McCullagh committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
#ifdef DRIZZLED
#define swap_variables(TYPE, a, b) \
  do {                             \
    TYPE dummy;                    \
    dummy= a;                      \
    a= b;                          \
    b= dummy;                      \
  } while (0)


#define CMP_NUM(a,b) (((a) < (b)) ? -1 : ((a) == (b)) ? 0 : 1)
#else
#define get_rec_bits(bit_ptr, bit_ofs, bit_len) \
	(((((uint16) (bit_ptr)[1] << 8) | (uint16) (bit_ptr)[0]) >> (bit_ofs)) & \
   ((1 << (bit_len)) - 1))
#endif

#define FIX_LENGTH(cs, pos, length, char_length) \
						do { \
							if ((length) > char_length) \
								char_length= my_charpos(cs, pos, pos+length, char_length); \
							set_if_smaller(char_length,length); \
						} while(0)

#ifdef store_key_length_inc
#undef store_key_length_inc
#endif
#define store_key_length_inc(key,length) \
{ if ((length) < 255) \
	{ *(key)++=(length); } \
	else \
	{ *(key)=255; mi_int2store((key)+1,(length)); (key)+=3; } \
}

#define set_rec_bits(bits, bit_ptr, bit_ofs, bit_len) \
{ \
	(bit_ptr)[0]= ((bit_ptr)[0] & ~(((1 << (bit_len)) - 1) << (bit_ofs))) | \
                ((bits) << (bit_ofs)); \
	if ((bit_ofs) + (bit_len) > 8) \
    (bit_ptr)[1]= ((bit_ptr)[1] & ~((1 << ((bit_len) - 8 + (bit_ofs))) - 1)) | \
                  ((bits) >> (8 - (bit_ofs))); \
}

#define clr_rec_bits(bit_ptr, bit_ofs, bit_len) \
	set_rec_bits(0, bit_ptr, bit_ofs, bit_len)

static ulong my_calc_blob_length(uint length, xtWord1 *pos)
{
	switch (length) {
	case 1:
		return (uint) (uchar) *pos;
	case 2:
		return (uint) uint2korr(pos);
	case 3:
		return uint3korr(pos);
	case 4:
		return uint4korr(pos);
	default:
		break;
	}
	return 0; /* Impossible */
}

static void my_store_blob_length(byte *pos,uint pack_length,uint length)
{
	switch (pack_length) {
	case 1:
		*pos= (uchar) length;
		break;
	case 2:
		int2store(pos,length);
		break;
	case 3:
		int3store(pos,length);
		break;
	case 4:
		int4store(pos,length);
	default:
		break;
	}
	return;
}

static int my_compare_text(MX_CONST_CHARSET_INFO *charset_info, uchar *a, uint a_length,
				uchar *b, uint b_length, my_bool part_key,
146
				my_bool XT_UNUSED(skip_end_space))
Paul McCullagh's avatar
Paul McCullagh committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
{
	if (!part_key)
		/* The last parameter is diff_if_only_endspace_difference, which means
		 * that end spaces are not ignored. We actually always want
		 * to ignore end spaces!
		 */
		return charset_info->coll->strnncollsp(charset_info, a, a_length,
				b, b_length, /*(my_bool)!skip_end_space*/0);
	return charset_info->coll->strnncoll(charset_info, a, a_length,
			b, b_length, part_key);
}

/*
 * -----------------------------------------------------------------------
 * Create a key
 */

/*
 * Derived from _mi_pack_key()
 */
xtPublic u_int myxt_create_key_from_key(XTIndexPtr ind, xtWord1 *key, xtWord1 *old, u_int k_length)
{
	xtWord1			*start_key = key;
	XTIndexSegRec	*keyseg = ind->mi_seg;

	for (u_int i=0; i<ind->mi_seg_count && (int) k_length > 0; i++, old += keyseg->length, keyseg++)
	{
174
#ifndef DRIZZLED
Paul McCullagh's avatar
Paul McCullagh committed
175
		enum ha_base_keytype	type = (enum ha_base_keytype) keyseg->type;
176
#endif
Paul McCullagh's avatar
Paul McCullagh committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
		u_int					length = keyseg->length < k_length ? keyseg->length : k_length;
		u_int					char_length;
		xtWord1					*pos;
		MX_CONST_CHARSET_INFO	*cs = keyseg->charset;

		if (keyseg->null_bit) {
			k_length--;
			if (!(*key++ = (xtWord1) 1 - *old++)) {					/* Copy null marker */
				k_length -= length;
				if (keyseg->flag & (HA_VAR_LENGTH_PART | HA_BLOB_PART)) {
					k_length -= 2;									/* Skip length */
 					old += 2;
				}
				continue;											/* Found NULL */
			}
		}
		char_length= (cs && cs->mbmaxlen > 1) ? length/cs->mbmaxlen : length;
		pos = old;
		if (keyseg->flag & HA_SPACE_PACK) {
			uchar *end = pos + length;
197
#ifndef DRIZZLED
Paul McCullagh's avatar
Paul McCullagh committed
198
			if (type != HA_KEYTYPE_NUM) {
199
#endif
Paul McCullagh's avatar
Paul McCullagh committed
200 201
				while (end > pos && end[-1] == ' ')
					end--;
202
#ifndef DRIZZLED
Paul McCullagh's avatar
Paul McCullagh committed
203 204 205 206 207
			}
			else {
				while (pos < end && pos[0] == ' ')
					pos++;
			}
208
#endif
Paul McCullagh's avatar
Paul McCullagh committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
			k_length -= length;
			length = (u_int) (end-pos);
			FIX_LENGTH(cs, pos, length, char_length);
			store_key_length_inc(key, char_length);
			memcpy((byte*) key,pos,(size_t) char_length);
			key += char_length;
			continue;
		}
		if (keyseg->flag & (HA_VAR_LENGTH_PART | HA_BLOB_PART)) {
			/* Length of key-part used with mi_rkey() always 2 */
			u_int tmp_length = uint2korr(pos);
			k_length -= 2 + length;
			pos += 2;
			set_if_smaller(length, tmp_length);	/* Safety */
			FIX_LENGTH(cs, pos, length, char_length);
			store_key_length_inc(key,char_length);
			old +=2;					/* Skip length */
			memcpy((char *) key, pos, (size_t) char_length);
			key += char_length;
			continue;
		}
		if (keyseg->flag & HA_SWAP_KEY)
		{						/* Numerical column */
			pos+=length;
			k_length-=length;
			while (length--) {
				*key++ = *--pos;
			}
			continue;
		}
		FIX_LENGTH(cs, pos, length, char_length);
		memcpy((byte*) key, pos, char_length);
		if (length > char_length)
			cs->cset->fill(cs, (char *) (key + char_length), length - char_length, ' ');
		key += length;
		k_length -= length;
	}

	return (u_int) (key - start_key);
}

/* Derived from _mi_make_key */
xtPublic u_int myxt_create_key_from_row(XTIndexPtr ind, xtWord1 *key, xtWord1 *record, xtBool *no_duplicate)
{
	register XTIndexSegRec	*keyseg = ind->mi_seg;
	xtWord1					*pos;
	xtWord1					*end;
	xtWord1					*start;

	start = key;
 	for (u_int i=0; i<ind->mi_seg_count; i++, keyseg++)
	{
		enum ha_base_keytype	type = (enum ha_base_keytype) keyseg->type;
 		u_int					length = keyseg->length;
 		u_int					char_length;
 		MX_CONST_CHARSET_INFO	*cs = keyseg->charset;

		if (keyseg->null_bit) {
			if (record[keyseg->null_pos] & keyseg->null_bit) {
				*key++ = 0;				/* NULL in key */
				
				/* The point is, if a key contains a NULL value
				 * the duplicate checking must be disabled.
				 * This is because a NULL value is not considered
				 * equal to any other value.
				 */ 
				if (no_duplicate)
					*no_duplicate = FALSE;
				continue;
			}
			*key++ = 1;					/* Not NULL */
		}

		char_length= ((cs && cs->mbmaxlen > 1) ? length/cs->mbmaxlen : length);

		pos = record + keyseg->start;
285
#ifndef DRIZZLED
Paul McCullagh's avatar
Paul McCullagh committed
286 287 288 289 290 291 292 293 294 295 296 297 298
		if (type == HA_KEYTYPE_BIT)
		{
			if (keyseg->bit_length)
			{
				uchar bits = get_rec_bits((uchar*) record + keyseg->bit_pos,
																 keyseg->bit_start, keyseg->bit_length);
				*key++ = bits;
				length--;
			}
			memcpy((byte*) key, pos, length);
			key+= length;
			continue;
		}
299
#endif
Paul McCullagh's avatar
Paul McCullagh committed
300 301 302
		if (keyseg->flag & HA_SPACE_PACK)
		{
			end = pos + length;
303
#ifndef DRIZZLED
Paul McCullagh's avatar
Paul McCullagh committed
304
			if (type != HA_KEYTYPE_NUM) {
305
#endif
Paul McCullagh's avatar
Paul McCullagh committed
306 307
				while (end > pos && end[-1] == ' ')
					end--;
308
#ifndef DRIZZLED
Paul McCullagh's avatar
Paul McCullagh committed
309 310 311 312 313
			}
			else {
				while (pos < end && pos[0] == ' ')
					pos++;
			}
314
#endif
Paul McCullagh's avatar
Paul McCullagh committed
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
			length = (u_int) (end-pos);
			FIX_LENGTH(cs, pos, length, char_length);
			store_key_length_inc(key,char_length);
			memcpy((byte*) key,(byte*) pos,(size_t) char_length);
			key += char_length;
			continue;
		}
		if (keyseg->flag & HA_VAR_LENGTH_PART) {
			uint pack_length= (keyseg->bit_start == 1 ? 1 : 2);
			uint tmp_length= (pack_length == 1 ? (uint) *(uchar*) pos :
												uint2korr(pos));
			pos += pack_length;			/* Skip VARCHAR length */
			set_if_smaller(length,tmp_length);
			FIX_LENGTH(cs, pos, length, char_length);
			store_key_length_inc(key,char_length);
			memcpy((byte*) key,(byte*) pos,(size_t) char_length);
			key += char_length;
			continue;
		}
		if (keyseg->flag & HA_BLOB_PART)
		{
			u_int tmp_length = my_calc_blob_length(keyseg->bit_start, pos);
			memcpy((byte*) &pos,pos+keyseg->bit_start,sizeof(char*));
			set_if_smaller(length,tmp_length);
			FIX_LENGTH(cs, pos, length, char_length);
			store_key_length_inc(key,char_length);
			memcpy((byte*) key,(byte*) pos,(size_t) char_length);
			key+= char_length;
			continue;
		}
		if (keyseg->flag & HA_SWAP_KEY)
		{						/* Numerical column */
#ifdef HAVE_ISNAN
348
#ifndef DRIZZLED
Paul McCullagh's avatar
Paul McCullagh committed
349 350 351 352 353 354 355 356 357 358 359 360
			if (type == HA_KEYTYPE_FLOAT)
			{
				float nr;
				float4get(nr,pos);
				if (isnan(nr))
				{
					/* Replace NAN with zero */
					bzero(key,length);
					key+=length;
					continue;
				}
			}
361 362 363
			else 
#endif			
			if (type == HA_KEYTYPE_DOUBLE) {
Paul McCullagh's avatar
Paul McCullagh committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
				double nr;

				float8get(nr,pos);
				if (isnan(nr)) {
					bzero(key,length);
					key+=length;
					continue;
				}
			}
#endif
			pos+=length;
			while (length--) {
				*key++ = *--pos;
			}
			continue;
		}
 		FIX_LENGTH(cs, pos, length, char_length);
		memcpy((byte*) key, pos, char_length);
		if (length > char_length)
			cs->cset->fill(cs, (char *) key + char_length, length - char_length, ' ');
		key += length;
	}

	return ind->mi_fix_key ? ind->mi_key_size : (u_int) (key - start);		/* Return keylength */
}

xtPublic u_int myxt_create_foreign_key_from_row(XTIndexPtr ind, xtWord1 *key, xtWord1 *record, XTIndexPtr fkey_ind, xtBool *no_null)
{
	register XTIndexSegRec	*keyseg = ind->mi_seg;
	register XTIndexSegRec	*fkey_keyseg = fkey_ind->mi_seg;
	xtWord1					*pos;
	xtWord1					*end;
	xtWord1					*start;

	start = key;
 	for (u_int i=0; i<ind->mi_seg_count; i++, keyseg++, fkey_keyseg++)
	{
		enum ha_base_keytype	type = (enum ha_base_keytype) keyseg->type;
 		u_int					length = keyseg->length;
 		u_int					char_length;
 		MX_CONST_CHARSET_INFO	*cs = keyseg->charset;
		xtBool					is_null = FALSE;

		if (keyseg->null_bit) {
			if (record[keyseg->null_pos] & keyseg->null_bit) {
				is_null = TRUE;
				if (no_null)
					*no_null = FALSE;
			}
		}

		if (fkey_keyseg->null_bit) {
			if (is_null) {
				*key++ = 0;				/* NULL in key */
				
				/* The point is, if a key contains a NULL value
				 * the duplicate checking must be disabled.
				 * This is because a NULL value is not considered
				 * equal to any other value.
				 */ 
				continue;
			}
			*key++ = 1;					/* Not NULL */
		}

		char_length= ((cs && cs->mbmaxlen > 1) ? length/cs->mbmaxlen : length);

		pos = record + keyseg->start;
432
#ifndef DRIZZLED
Paul McCullagh's avatar
Paul McCullagh committed
433 434 435 436 437 438 439 440 441 442 443 444 445
		if (type == HA_KEYTYPE_BIT)
		{
			if (keyseg->bit_length)
			{
				uchar bits = get_rec_bits((uchar*) record + keyseg->bit_pos,
																 keyseg->bit_start, keyseg->bit_length);
				*key++ = bits;
				length--;
			}
			memcpy((byte*) key, pos, length);
			key+= length;
			continue;
		}
446
#endif
Paul McCullagh's avatar
Paul McCullagh committed
447 448 449
		if (keyseg->flag & HA_SPACE_PACK)
		{
			end = pos + length;
450
#ifndef DRIZZLED
Paul McCullagh's avatar
Paul McCullagh committed
451
			if (type != HA_KEYTYPE_NUM) {
452
#endif
Paul McCullagh's avatar
Paul McCullagh committed
453 454
				while (end > pos && end[-1] == ' ')
					end--;
455
#ifndef DRIZZLED
Paul McCullagh's avatar
Paul McCullagh committed
456 457 458 459 460
			}
			else {
				while (pos < end && pos[0] == ' ')
					pos++;
			}
461
#endif
Paul McCullagh's avatar
Paul McCullagh committed
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
			length = (u_int) (end-pos);
			FIX_LENGTH(cs, pos, length, char_length);
			store_key_length_inc(key,char_length);
			memcpy((byte*) key,(byte*) pos,(size_t) char_length);
			key += char_length;
			continue;
		}
		if (keyseg->flag & HA_VAR_LENGTH_PART) {
			uint pack_length= (keyseg->bit_start == 1 ? 1 : 2);
			uint tmp_length= (pack_length == 1 ? (uint) *(uchar*) pos :
												uint2korr(pos));
			pos += pack_length;			/* Skip VARCHAR length */
			set_if_smaller(length,tmp_length);
			FIX_LENGTH(cs, pos, length, char_length);
			store_key_length_inc(key,char_length);
			memcpy((byte*) key,(byte*) pos,(size_t) char_length);
			key += char_length;
			continue;
		}
		if (keyseg->flag & HA_BLOB_PART)
		{
			u_int tmp_length = my_calc_blob_length(keyseg->bit_start, pos);
			memcpy((byte*) &pos,pos+keyseg->bit_start,sizeof(char*));
			set_if_smaller(length,tmp_length);
			FIX_LENGTH(cs, pos, length, char_length);
			store_key_length_inc(key,char_length);
			memcpy((byte*) key,(byte*) pos,(size_t) char_length);
			key+= char_length;
			continue;
		}
		if (keyseg->flag & HA_SWAP_KEY)
		{						/* Numerical column */
#ifdef HAVE_ISNAN
495
#ifndef DRIZZLED
Paul McCullagh's avatar
Paul McCullagh committed
496 497 498 499 500 501 502 503 504 505 506 507
			if (type == HA_KEYTYPE_FLOAT)
			{
				float nr;
				float4get(nr,pos);
				if (isnan(nr))
				{
					/* Replace NAN with zero */
					bzero(key,length);
					key+=length;
					continue;
				}
			}
508 509 510
			else 
#endif
			if (type == HA_KEYTYPE_DOUBLE) {
Paul McCullagh's avatar
Paul McCullagh committed
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
				double nr;

				float8get(nr,pos);
				if (isnan(nr)) {
					bzero(key,length);
					key+=length;
					continue;
				}
			}
#endif
			pos+=length;
			while (length--) {
				*key++ = *--pos;
			}
			continue;
		}
 		FIX_LENGTH(cs, pos, length, char_length);
		memcpy((byte*) key, pos, char_length);
		if (length > char_length)
			cs->cset->fill(cs, (char *) key + char_length, length - char_length, ' ');
		key += length;
	}

	return fkey_ind->mi_fix_key ? fkey_ind->mi_key_size : (u_int) (key - start);		/* Return keylength */
}

/* I may be overcautious here, but can I assume that
 * null_ptr refers to my buffer. If I cannot, then I
 * cannot use the set_notnull() method.
 */
static void mx_set_notnull_in_record(Field *field, char *record)
{
	if (field->null_ptr)
		record[(uint) (field->null_ptr - (uchar *) field->table->record[0])] &= (uchar) ~field->null_bit;
}

static xtBool mx_is_null_in_record(Field *field, char *record)
{
	if (field->null_ptr) {
		if (record[(uint) (field->null_ptr - (uchar *) field->table->record[0])] & (uchar) field->null_bit)
			return TRUE;
	}
	return FALSE;
}

/*
 * PBXT uses a completely different disk format to MySQL so I need a
 * method that just returns the byte length and
 * pointer to the data in a row.
 */
static char *mx_get_length_and_data(Field *field, char *dest, xtWord4 *len)
{
	char *from;
	
#if MYSQL_VERSION_ID < 50114
	from = dest + field->offset();
#else
	from = dest + field->offset(field->table->record[0]);
#endif
	switch (field->real_type()) {
#ifndef DRIZZLED
		case MYSQL_TYPE_TINY_BLOB:
		case MYSQL_TYPE_MEDIUM_BLOB:
		case MYSQL_TYPE_LONG_BLOB:
#endif
		case MYSQL_TYPE_BLOB: {
			/* TODO - Check: this was the original comment: I must set
			 * *data to non-NULL value, *data == 0, means SQL NULL value.
			 */
			char	*data;

			/* GOTCHA: There is no way this can work! field is shared
			 * between threads.
			char	*save = field->ptr;

			field->ptr = (char *) from;
			((Field_blob *) field)->get_ptr(&data);
			field->ptr = save;					// Restore org row pointer
			*/

			xtWord4 packlength = ((Field_blob *) field)->pack_length() - field->table->s->blob_ptr_size;
			memcpy(&data, ((char *) from)+packlength, sizeof(char*));
			
			*len = ((Field_blob *) field)->get_length((byte *) from);
			return data;
		}
#ifndef DRIZZLED
		case MYSQL_TYPE_STRING:
			/* To write this function you would think Field_string::pack
			 * would serve as a good example, but as far as I can tell
			 * it has a bug: the test from[length-1] == ' ' assumes
			 * 1-byte chars.
			 *
			 * But this is not relevant because I believe lengthsp
			 * will give me the correct answer!
			 */
			*len = field->charset()->cset->lengthsp(field->charset(), from, field->field_length);
			return from;
		case MYSQL_TYPE_VAR_STRING: {
			uint length=uint2korr(from);

			*len = length;
			return from+HA_KEY_BLOB_LENGTH;
		}
#endif
		case MYSQL_TYPE_VARCHAR: {
			uint length;

			if (((Field_varstring *) field)->length_bytes == 1)
				length = *((unsigned char *) from);
			else
				length = uint2korr(from);
			
			*len = length;
			return from+((Field_varstring *) field)->length_bytes;
		}
#ifndef DRIZZLED
		case MYSQL_TYPE_DECIMAL:
		case MYSQL_TYPE_TINY:
		case MYSQL_TYPE_SHORT:
		case MYSQL_TYPE_LONG:
		case MYSQL_TYPE_FLOAT:
		case MYSQL_TYPE_DOUBLE:
		case MYSQL_TYPE_NULL:
		case MYSQL_TYPE_TIMESTAMP:
		case MYSQL_TYPE_LONGLONG:
		case MYSQL_TYPE_INT24:
		case MYSQL_TYPE_DATE:
		case MYSQL_TYPE_TIME:
		case MYSQL_TYPE_DATETIME:
		case MYSQL_TYPE_YEAR:
		case MYSQL_TYPE_NEWDATE:
		case MYSQL_TYPE_BIT:
		case MYSQL_TYPE_NEWDECIMAL:
		case MYSQL_TYPE_ENUM:
		case MYSQL_TYPE_SET:
		case MYSQL_TYPE_GEOMETRY:
#else
		case DRIZZLE_TYPE_LONG:
		case DRIZZLE_TYPE_DOUBLE:
		case DRIZZLE_TYPE_NULL:
		case DRIZZLE_TYPE_TIMESTAMP:
		case DRIZZLE_TYPE_LONGLONG:
		case DRIZZLE_TYPE_DATETIME:
		case DRIZZLE_TYPE_DATE:
		case DRIZZLE_TYPE_NEWDECIMAL:
		case DRIZZLE_TYPE_ENUM:
#endif
			break;
	}

	*len = field->pack_length();
	return from;
}

/*
 * Set the length and data value of a field.
 * 
 * If input data is NULL this is a NULL value. In this case
 * we assume the null bit has been set and prepared
 * the field as follows:
 * 
 * According to the InnoDB implementation, we need
 * to zero out the field data...
 * "MySQL seems to assume the field for an SQL NULL
 * value is set to zero or space. Not taking this into
 * account caused seg faults with NULL BLOB fields, and
 * bug number 154 in the MySQL bug database: GROUP BY
 * and DISTINCT could treat NULL values inequal".
 */
static void mx_set_length_and_data(Field *field, char *dest, xtWord4 len, char *data)
{
	char *from;
	
#if MYSQL_VERSION_ID < 50114
	from = dest + field->offset();
#else
	from = dest + field->offset(field->table->record[0]);
#endif
	switch (field->real_type()) {
#ifndef DRIZZLED
		case MYSQL_TYPE_TINY_BLOB:
		case MYSQL_TYPE_MEDIUM_BLOB:
		case MYSQL_TYPE_LONG_BLOB:
#endif
		case MYSQL_TYPE_BLOB: {
			/* GOTCHA: There is no way that this can work.
			 * field is shared, because table is shared!
			char *save = field->ptr;
		 
			field->ptr = (char *) from;
			((Field_blob *) field)->set_ptr(len, data);
			field->ptr = save;					// Restore org row pointer
			*/
			xtWord4 packlength = ((Field_blob *) field)->pack_length() - field->table->s->blob_ptr_size;

			((Field_blob *) field)->store_length((byte *) from, packlength, len);
			memcpy_fixed(((char *) from)+packlength, &data, sizeof(char*));

			if (data)
				mx_set_notnull_in_record(field, dest);
			return;
		}
#ifndef DRIZZLED
		case MYSQL_TYPE_STRING:
			if (data) {
				mx_set_notnull_in_record(field, dest);
				memcpy(from, data, len);
			}
			else
				len = 0;

			/* And I think that fill will do this for me... */
			field->charset()->cset->fill(field->charset(), from + len, field->field_length - len, ' ');
			return;
		case MYSQL_TYPE_VAR_STRING:
			int2store(from, len);
			if (data) {
				mx_set_notnull_in_record(field, dest);
				memcpy(from+HA_KEY_BLOB_LENGTH, data, len);
			}
			return;
#endif
		case MYSQL_TYPE_VARCHAR:
			if (((Field_varstring *) field)->length_bytes == 1)
				*((unsigned char *) from) = (unsigned char) len;
			else
				int2store(from, len);
			if (data) {
				mx_set_notnull_in_record(field, dest);
				memcpy(from+((Field_varstring *) field)->length_bytes, data, len);
			}
			return;
#ifndef DRIZZLED
		case MYSQL_TYPE_DECIMAL:
		case MYSQL_TYPE_TINY:
		case MYSQL_TYPE_SHORT:
		case MYSQL_TYPE_LONG:
		case MYSQL_TYPE_FLOAT:
		case MYSQL_TYPE_DOUBLE:
		case MYSQL_TYPE_NULL:
		case MYSQL_TYPE_TIMESTAMP:
		case MYSQL_TYPE_LONGLONG:
		case MYSQL_TYPE_INT24:
		case MYSQL_TYPE_DATE:
		case MYSQL_TYPE_TIME:
		case MYSQL_TYPE_DATETIME:
		case MYSQL_TYPE_YEAR:
		case MYSQL_TYPE_NEWDATE:
		case MYSQL_TYPE_BIT:
		case MYSQL_TYPE_NEWDECIMAL:
		case MYSQL_TYPE_ENUM:
		case MYSQL_TYPE_SET:
		case MYSQL_TYPE_GEOMETRY:
#else
		case DRIZZLE_TYPE_LONG:
		case DRIZZLE_TYPE_DOUBLE:
		case DRIZZLE_TYPE_NULL:
		case DRIZZLE_TYPE_TIMESTAMP:
		case DRIZZLE_TYPE_LONGLONG:
		case DRIZZLE_TYPE_DATETIME:
		case DRIZZLE_TYPE_DATE:
		case DRIZZLE_TYPE_NEWDECIMAL:
		case DRIZZLE_TYPE_ENUM:
#endif
			break;
	}

	if (data) {
		mx_set_notnull_in_record(field, dest);
		memcpy(from, data, len);
	}
	else
		bzero(from, field->pack_length());
}

787
xtPublic void myxt_set_null_row_from_key(XTOpenTablePtr XT_UNUSED(ot), XTIndexPtr ind, xtWord1 *record)
Paul McCullagh's avatar
Paul McCullagh committed
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
{
	register XTIndexSegRec *keyseg = ind->mi_seg;

	for (u_int i=0; i<ind->mi_seg_count; i++, keyseg++) {
		ASSERT_NS(keyseg->null_bit);
		record[keyseg->null_pos] |= keyseg->null_bit;
	}
}

xtPublic void myxt_set_default_row_from_key(XTOpenTablePtr ot, XTIndexPtr ind, xtWord1 *record)
{
	XTTableHPtr		tab = ot->ot_table;
	TABLE			*table = tab->tab_dic.dic_my_table;
	XTIndexSegRec	*keyseg = ind->mi_seg;

	xt_lock_mutex_ns(&tab->tab_dic_field_lock);

	for (u_int i=0; i<ind->mi_seg_count; i++, keyseg++) {
		
		u_int col_idx = keyseg->col_idx;
		Field *field = table->field[col_idx];
		byte  *field_save = field->ptr;

		field->ptr = table->s->default_values + keyseg->start;
		memcpy(record + keyseg->start, field->ptr, field->pack_length());
		record[keyseg->null_pos] &= ~keyseg->null_bit;
		record[keyseg->null_pos] |= table->s->default_values[keyseg->null_pos] & keyseg->null_bit;

		field->ptr = field_save;
	}

	xt_unlock_mutex_ns(&tab->tab_dic_field_lock);
}

/* Derived from _mi_put_key_in_record */
823
xtPublic xtBool myxt_create_row_from_key(XTOpenTablePtr XT_UNUSED(ot), XTIndexPtr ind, xtWord1 *b_value, u_int key_len, xtWord1 *dest_buff)
Paul McCullagh's avatar
Paul McCullagh committed
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
{
	byte					*record = (byte *) dest_buff;
	register byte			*key;
	byte					*pos,*key_end;
	register XTIndexSegRec	*keyseg = ind->mi_seg;

	/* GOTCHA: When selecting from multiple
	 * indexes the key values are "merged" into the
	 * same buffer!!
	 * This means that this function must not affect
	 * the value of any other feilds.
	 *
	 * I was setting all to NULL:
	memset(dest_buff, 0xFF, table->s->null_bytes);
	*/
	key = (byte *) b_value;
	key_end = key + key_len;
	for (u_int i=0; i<ind->mi_seg_count; i++, keyseg++) {
		if (keyseg->null_bit) {
			if (!*key++)
			{
				record[keyseg->null_pos] |= keyseg->null_bit;
				continue;
			}
			record[keyseg->null_pos] &= ~keyseg->null_bit;
		}
850
#ifndef DRIZZLED
Paul McCullagh's avatar
Paul McCullagh committed
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
		if (keyseg->type == HA_KEYTYPE_BIT)
		{
			uint length = keyseg->length;

			if (keyseg->bit_length)
			{
				uchar bits= *key++;
				set_rec_bits(bits, record + keyseg->bit_pos, keyseg->bit_start,
										 keyseg->bit_length);
				length--;
			}
			else
			{
				clr_rec_bits(record + keyseg->bit_pos, keyseg->bit_start,
										 keyseg->bit_length);
			}
			memcpy(record + keyseg->start, (byte*) key, length);
			key+= length;
			continue;
		}
871
#endif
Paul McCullagh's avatar
Paul McCullagh committed
872 873 874 875 876 877 878 879 880
		if (keyseg->flag & HA_SPACE_PACK)
		{
			uint length;
			get_key_length(length,key);
#ifdef CHECK_KEYS
			if (length > keyseg->length || key+length > key_end)
				goto err;
#endif
			pos = record+keyseg->start;
881
#ifndef DRIZZLED
Paul McCullagh's avatar
Paul McCullagh committed
882 883
			if (keyseg->type != (int) HA_KEYTYPE_NUM)
			{
884
#endif
Paul McCullagh's avatar
Paul McCullagh committed
885 886
				memcpy(pos,key,(size_t) length);
				bfill(pos+length,keyseg->length-length,' ');
887
#ifndef DRIZZLED
Paul McCullagh's avatar
Paul McCullagh committed
888 889 890 891 892 893
			}
			else
			{
				bfill(pos,keyseg->length-length,' ');
				memcpy(pos+keyseg->length-length,key,(size_t) length);
			}
894
#endif
Paul McCullagh's avatar
Paul McCullagh committed
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
			key+=length;
			continue;
		}

		if (keyseg->flag & HA_VAR_LENGTH_PART)
		{
			uint length;
			get_key_length(length,key);
#ifdef CHECK_KEYS
			if (length > keyseg->length || key+length > key_end)
	goto err;
#endif
			/* Store key length */
			if (keyseg->bit_start == 1)
				*(uchar*) (record+keyseg->start)= (uchar) length;
			else
				int2store(record+keyseg->start, length);
			/* And key data */
			memcpy(record+keyseg->start + keyseg->bit_start, (byte*) key, length);
			key+= length;
		}
		else if (keyseg->flag & HA_BLOB_PART)
		{
			uint length;
			get_key_length(length,key);
#ifdef CHECK_KEYS
			if (length > keyseg->length || key+length > key_end)
				goto err;
#endif
			/* key is a pointer into ot_ind_rbuf, which should be
			 * safe until we move to the next index item!
			 */
			byte *key_ptr = key; // Cannot take the address of a register variable!
			memcpy(record+keyseg->start+keyseg->bit_start,
			 (char*) &key_ptr,sizeof(char*));

			my_store_blob_length(record+keyseg->start,
					(uint) keyseg->bit_start,length);
			key+=length;
		}
		else if (keyseg->flag & HA_SWAP_KEY)
		{
			byte *to=	record+keyseg->start+keyseg->length;
			byte *end= key+keyseg->length;
#ifdef CHECK_KEYS
			if (end > key_end)
				goto err;
#endif
			do {
				*--to= *key++;
			} while (key != end);
			continue;
		}
		else
		{
#ifdef CHECK_KEYS
			if (key+keyseg->length > key_end)
				goto err;
#endif
			memcpy(record+keyseg->start,(byte*) key,
			 (size_t) keyseg->length);
			key+= keyseg->length;
		}
	
	}
	return OK;

#ifdef CHECK_KEYS
	err:
	return FAILED;				/* Crashed row */
965
#endif
Paul McCullagh's avatar
Paul McCullagh committed
966 967 968 969 970 971 972 973 974 975
}

/*
 * -----------------------------------------------------------------------
 * Compare keys
 */

static int my_compare_bin(uchar *a, uint a_length, uchar *b, uint b_length,
											 my_bool part_key, my_bool skip_end_space)
{
976
	uint length= a_length < b_length ? a_length : b_length;
Paul McCullagh's avatar
Paul McCullagh committed
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
	uchar *end= a+ length;
	int flag;

	while (a < end)
		if ((flag= (int) *a++ - (int) *b++))
			return flag;
	if (part_key && b_length < a_length)
		return 0;
	if (skip_end_space && a_length != b_length)
	{
		int swap= 1;
		/*
			We are using space compression. We have to check if longer key
			has next character < ' ', in which case it's less than the shorter
			key that has an implicite space afterwards.

			This code is identical to the one in
			strings/ctype-simple.c:my_strnncollsp_simple
		*/
		if (a_length < b_length)
		{
			/* put shorter key in a */
			a_length= b_length;
			a= b;
			swap= -1;					/* swap sign of result */
		}
		for (end= a + a_length-length; a < end ; a++)
		{
			if (*a != ' ')
				return (*a < ' ') ? -swap : swap;
		}
		return 0;
	}
	return (int) (a_length-b_length);
}

xtPublic u_int myxt_get_key_length(XTIndexPtr ind, xtWord1 *key_buf)
{
	register XTIndexSegRec	*keyseg = ind->mi_seg;
	register uchar			*key_data = (uchar *) key_buf;
	uint					seg_len;
	uint					pack_len;

	for (u_int i=0; i<ind->mi_seg_count; i++, keyseg++) {
		/* Handle NULL part */
		if (keyseg->null_bit) {
			if (!*key_data++)	
				continue;
		}

		switch ((enum ha_base_keytype) keyseg->type) {
			case HA_KEYTYPE_TEXT:											 /* Ascii; Key is converted */
				if (keyseg->flag & HA_SPACE_PACK) {
					get_key_pack_length(seg_len, pack_len, key_data);
				}
				else
					seg_len = keyseg->length;
				key_data += seg_len;
				break;
			case HA_KEYTYPE_BINARY:
				if (keyseg->flag & HA_SPACE_PACK) {
					get_key_pack_length(seg_len, pack_len, key_data);
				}
				else
					seg_len = keyseg->length;
				key_data += seg_len;
				break;
			case HA_KEYTYPE_VARTEXT1:
			case HA_KEYTYPE_VARTEXT2:
				get_key_pack_length(seg_len, pack_len, key_data);
				key_data += seg_len;
				break;
			case HA_KEYTYPE_VARBINARY1:
			case HA_KEYTYPE_VARBINARY2:
				get_key_pack_length(seg_len, pack_len, key_data);
				key_data += seg_len;
				break;
1054
#ifndef DRIZZLED
Paul McCullagh's avatar
Paul McCullagh committed
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
			case HA_KEYTYPE_NUM: {
				/* Numeric key */
				if (keyseg->flag & HA_SPACE_PACK)
					seg_len = *key_data++;
				else
					seg_len = keyseg->length;
				key_data += seg_len;
				break;
			}
			case HA_KEYTYPE_INT8:
			case HA_KEYTYPE_SHORT_INT:
			case HA_KEYTYPE_USHORT_INT:
1067 1068 1069 1070
			case HA_KEYTYPE_INT24:
			case HA_KEYTYPE_FLOAT:
			case HA_KEYTYPE_BIT:
#endif
Paul McCullagh's avatar
Paul McCullagh committed
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
			case HA_KEYTYPE_LONG_INT:
			case HA_KEYTYPE_ULONG_INT:
			case HA_KEYTYPE_UINT24:
			case HA_KEYTYPE_DOUBLE:
			case HA_KEYTYPE_LONGLONG:
			case HA_KEYTYPE_ULONGLONG:
				key_data += keyseg->length;
				break;
			case HA_KEYTYPE_END:
				goto end;
		}
	}

	end:
	return (xtWord1 *) key_data - key_buf;
}

/* Derived from ha_key_cmp */
xtPublic int myxt_compare_key(XTIndexPtr ind, int search_flags, uint key_length, xtWord1 *key_value, xtWord1 *b_value)
{
	register XTIndexSegRec	*keyseg = ind->mi_seg;
	int						flag;
	register uchar			*a = (uchar *) key_value;
	uint					a_length;
	register uchar			*b = (uchar *) b_value;
	uint					b_length;
	uint					next_key_length;
	uchar					*end;
	uint					piks;
	uint					pack_len;

	for (uint i=0; i < ind->mi_seg_count && (int) key_length > 0; key_length = next_key_length, keyseg++, i++) {
		piks = !(keyseg->flag & HA_NO_SORT);

		/* Handle NULL part */
		if (keyseg->null_bit) {
			/* 1 is not null, 0 is null */
			int b_not_null = (int) *b++;

			key_length--;
			if ((int) *a != b_not_null && piks)
			{
				flag = (int) *a - b_not_null;
				return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
			}
			if (!*a++) {		
				/* If key was NULL */
				if (search_flags == (SEARCH_FIND | SEARCH_UPDATE))
					search_flags = SEARCH_SAME;								 /* Allow duplicate keys */
				else if (search_flags & SEARCH_NULL_ARE_NOT_EQUAL)
				{
					/*
					 * This is only used from mi_check() to calculate cardinality.
					 * It can't be used when searching for a key as this would cause
					 * compare of (a,b) and (b,a) to return the same value.
					 */
					return -1;
				}
				/* PMC - I don't know why I had next_key_length = key_length - keyseg->length;
				 * This was my comment: even when null we have the complete length
				 *
				 * The truth is, a NULL only takes up one byte in the key, and this has already
				 * been subtracted.
				 */
				next_key_length = key_length;
				continue;															 /* To next key part */
			}
		}
		
		/* Both components are not null... */
		if (keyseg->length < key_length) {
			end = a + keyseg->length;
			next_key_length = key_length - keyseg->length;
		}
		else {
			end = a + key_length;
			next_key_length = 0;
		}

		switch ((enum ha_base_keytype) keyseg->type) {
			case HA_KEYTYPE_TEXT:											 /* Ascii; Key is converted */
				if (keyseg->flag & HA_SPACE_PACK) {
					get_key_pack_length(a_length, pack_len, a);
					next_key_length = key_length - a_length - pack_len;
					get_key_pack_length(b_length, pack_len, b);

					if (piks && (flag = my_compare_text(keyseg->charset, a, a_length, b, b_length,
									(my_bool) ((search_flags & SEARCH_PREFIX) && next_key_length <= 0),
									(my_bool)!(search_flags & SEARCH_PREFIX))))
						return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
					a += a_length;
				}
				else {
					a_length = (uint) (end - a);
					b_length = keyseg->length;
					if (piks && (flag = my_compare_text(keyseg->charset, a, a_length, b, b_length,
									(my_bool) ((search_flags & SEARCH_PREFIX) && next_key_length <= 0),
									(my_bool)!(search_flags & SEARCH_PREFIX))))
						return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
					a = end;
				}
				b += b_length;
				break;
			case HA_KEYTYPE_BINARY:
				if (keyseg->flag & HA_SPACE_PACK) {
					get_key_pack_length(a_length, pack_len, a);
					next_key_length = key_length - a_length - pack_len;
					get_key_pack_length(b_length, pack_len, b);

					if (piks && (flag = my_compare_bin(a, a_length, b, b_length,
								(my_bool) ((search_flags & SEARCH_PREFIX) && next_key_length <= 0), 1)))
						return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
				}
				else {
					a_length = keyseg->length;
					b_length = keyseg->length;
					if (piks && (flag = my_compare_bin(a, a_length, b, b_length,
									(my_bool) ((search_flags & SEARCH_PREFIX) && next_key_length <= 0), 0)))
						return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
				}
				a += a_length;
				b += b_length;
				break;
			case HA_KEYTYPE_VARTEXT1:
			case HA_KEYTYPE_VARTEXT2:
			{
				get_key_pack_length(a_length, pack_len, a);
				next_key_length = key_length - a_length - pack_len;
				get_key_pack_length(b_length, pack_len, b);

				if (piks && (flag = my_compare_text(keyseg->charset, a, a_length, b, b_length,
								(my_bool) ((search_flags & SEARCH_PREFIX) && next_key_length <= 0),
								(my_bool) ((search_flags & (SEARCH_FIND | SEARCH_UPDATE)) == SEARCH_FIND))))
					return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
				a += a_length;
				b += b_length;
				break;
			}
			case HA_KEYTYPE_VARBINARY1:
			case HA_KEYTYPE_VARBINARY2:
			{				
				get_key_pack_length(a_length, pack_len, a);
				next_key_length = key_length - a_length - pack_len;
				get_key_pack_length(b_length, pack_len, b);

				if (piks && (flag=my_compare_bin(a, a_length, b, b_length,
						(my_bool) ((search_flags & SEARCH_PREFIX) && next_key_length <= 0), 0)))
					return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
				a += a_length;
				b += b_length;
				break;
			}
1223
#ifndef DRIZZLED
Paul McCullagh's avatar
Paul McCullagh committed
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
			case HA_KEYTYPE_INT8:
			{
				int i_1 = (int) *((signed char *) a);
				int i_2 = (int) *((signed char *) b);
				if (piks && (flag = CMP_NUM(i_1,i_2)))
					return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
				a = end;
				b += keyseg->length;
				break;
			}
			case HA_KEYTYPE_SHORT_INT: {
				int16 s_1 = sint2korr(a);
				int16 s_2 = sint2korr(b);
				if (piks && (flag = CMP_NUM(s_1, s_2)))
					return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
				a = end;
				b += keyseg->length;
				break;
			}
			case HA_KEYTYPE_USHORT_INT: {
				uint16 us_1= sint2korr(a);
				uint16 us_2= sint2korr(b);
				if (piks && (flag = CMP_NUM(us_1, us_2)))
					return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
				a =	end;
				b += keyseg->length;
				break;
			}
1252
#endif
Paul McCullagh's avatar
Paul McCullagh committed
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
			case HA_KEYTYPE_LONG_INT: {
				int32 l_1 = sint4korr(a);
				int32 l_2 = sint4korr(b);
				if (piks && (flag = CMP_NUM(l_1, l_2)))
					return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
				a = end;
				b += keyseg->length;
				break;
			}
			case HA_KEYTYPE_ULONG_INT: {
				uint32 u_1 = sint4korr(a);
				uint32 u_2 = sint4korr(b);
				if (piks && (flag = CMP_NUM(u_1, u_2)))
					return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
				a = end;
				b += keyseg->length;
				break;
			}
1271
#ifndef DRIZZLED
Paul McCullagh's avatar
Paul McCullagh committed
1272 1273 1274 1275 1276 1277 1278 1279 1280
			case HA_KEYTYPE_INT24: {
				int32 l_1 = sint3korr(a);
				int32 l_2 = sint3korr(b);
				if (piks && (flag = CMP_NUM(l_1, l_2)))
					return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
				a = end;
				b += keyseg->length;
				break;
			}
1281
#endif
Paul McCullagh's avatar
Paul McCullagh committed
1282 1283 1284 1285 1286 1287 1288 1289 1290
			case HA_KEYTYPE_UINT24: {
				int32 l_1 = uint3korr(a);
				int32 l_2 = uint3korr(b);
				if (piks && (flag = CMP_NUM(l_1, l_2)))
					return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
				a = end;
				b += keyseg->length;
				break;
			}
1291
#ifndef DRIZZLED
Paul McCullagh's avatar
Paul McCullagh committed
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
			case HA_KEYTYPE_FLOAT: {
				float f_1, f_2;

				float4get(f_1, a);
				float4get(f_2, b);
				/*
				 * The following may give a compiler warning about floating point
				 * comparison not being safe, but this is ok in this context as
				 * we are bascily doing sorting
				 */
				if (piks && (flag = CMP_NUM(f_1, f_2)))
					return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
				a = end;
				b += keyseg->length;
				break;
			}
1308
#endif
Paul McCullagh's avatar
Paul McCullagh committed
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
			case HA_KEYTYPE_DOUBLE: {
				double d_1, d_2;

				float8get(d_1, a);
				float8get(d_2, b);
				/*
				 * The following may give a compiler warning about floating point
				 * comparison not being safe, but this is ok in this context as
				 * we are bascily doing sorting
				 */
				if (piks && (flag = CMP_NUM(d_1, d_2)))
					return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
				a = end;
				b += keyseg->length;
				break;
			}
1325
#ifndef DRIZZLED
Paul McCullagh's avatar
Paul McCullagh committed
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
			case HA_KEYTYPE_NUM: {
				/* Numeric key */
				if (keyseg->flag & HA_SPACE_PACK) {
					a_length = *a++;
					end = a + a_length;
					next_key_length = key_length - a_length - 1;
					b_length = *b++;
				}
				else {
					a_length = (int) (end - a);
					b_length = keyseg->length;
				}

				/* remove pre space from keys */
				for ( ; a_length && *a == ' ' ; a++, a_length--) ;
				for ( ; b_length && *b == ' ' ; b++, b_length--) ;

				if (keyseg->flag & HA_REVERSE_SORT) {
					swap_variables(uchar *, a, b);
					swap_variables(uint, a_length, b_length);
				}
				
				if (piks) {
					if (*a == '-') {
						if (*b != '-')
							return -1;
						a++; b++;
						swap_variables(uchar *, a, b);
						swap_variables(uint, a_length, b_length);
						a_length--; b_length--;
					}
					else if (*b == '-')
						return 1;
					while (a_length && (*a == '+' || *a == '0')) {
						a++; a_length--;
					}
					
					while (b_length && (*b == '+' || *b == '0')) {
						b++; b_length--;
					}
				
					if (a_length != b_length)
						return (a_length < b_length) ? -1 : 1;
					while (b_length) {
						if (*a++ !=	*b++)
							return ((int) a[-1] - (int) b[-1]);
						b_length--;
					}
				}
				a = end;
				b += b_length;
				break;
			}
1379
#endif
Paul McCullagh's avatar
Paul McCullagh committed
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
#ifdef HAVE_LONG_LONG
			case HA_KEYTYPE_LONGLONG: {
				longlong ll_a = sint8korr(a);
				longlong ll_b = sint8korr(b);
				if (piks && (flag = CMP_NUM(ll_a,ll_b)))
					return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
				a = end;
				b += keyseg->length;
				break;
			}
			case HA_KEYTYPE_ULONGLONG: {					
				ulonglong ll_a = uint8korr(a);
				ulonglong ll_b = uint8korr(b);
				if (piks && (flag = CMP_NUM(ll_a,ll_b)))
					return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
				a = end;
				b += keyseg->length;
				break;
			}
#endif
1400
#ifndef DRIZZLED
Paul McCullagh's avatar
Paul McCullagh committed
1401 1402 1403
			case HA_KEYTYPE_BIT:
				/* TODO: What here? */
				break;
1404
#endif
Paul McCullagh's avatar
Paul McCullagh committed
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
			case HA_KEYTYPE_END:												/* Ready */
				goto end;
		}
	}

	end:
	return 0;
}

xtPublic u_int myxt_key_seg_length(XTIndexSegRec *keyseg, u_int key_offset, xtWord1 *key_value)
{
	register xtWord1	*a = (xtWord1 *) key_value + key_offset;
	u_int				a_length;
	u_int				has_null = 0;
	u_int				key_length = 0;
	u_int				pack_len;

	/* Handle NULL part */
	if (keyseg->null_bit) {
		has_null++;
		/* If the value is null, then it only requires one byte: */
		if (!*a++)
			return has_null;
	}
	
	key_length = has_null + keyseg->length;

	switch ((enum ha_base_keytype) keyseg->type) {
		case HA_KEYTYPE_TEXT:											 /* Ascii; Key is converted */
			if (keyseg->flag & HA_SPACE_PACK) {
				get_key_pack_length(a_length, pack_len, a);
				key_length = has_null + a_length + pack_len;
			}
			break;
		case HA_KEYTYPE_BINARY:
			if (keyseg->flag & HA_SPACE_PACK) {
				get_key_pack_length(a_length, pack_len, a);
				key_length = has_null + a_length + pack_len;
			}
			break;
		case HA_KEYTYPE_VARTEXT1:
		case HA_KEYTYPE_VARTEXT2:
		case HA_KEYTYPE_VARBINARY1:
		case HA_KEYTYPE_VARBINARY2: {				
			get_key_pack_length(a_length, pack_len, a);
			key_length = has_null + a_length + pack_len;
			break;
		}
1453
#ifndef DRIZZLED
Paul McCullagh's avatar
Paul McCullagh committed
1454 1455 1456
		case HA_KEYTYPE_INT8:
		case HA_KEYTYPE_SHORT_INT:
		case HA_KEYTYPE_USHORT_INT:
1457 1458 1459
		case HA_KEYTYPE_INT24:
		case HA_KEYTYPE_FLOAT:
#endif		
Paul McCullagh's avatar
Paul McCullagh committed
1460 1461 1462 1463 1464
		case HA_KEYTYPE_LONG_INT:
		case HA_KEYTYPE_ULONG_INT:
		case HA_KEYTYPE_UINT24:
		case HA_KEYTYPE_DOUBLE:
			break;
1465
#ifndef DRIZZLED
Paul McCullagh's avatar
Paul McCullagh committed
1466 1467 1468 1469 1470 1471 1472 1473
		case HA_KEYTYPE_NUM: {
			/* Numeric key */
			if (keyseg->flag & HA_SPACE_PACK) {
				a_length = *a++;
				key_length = has_null + a_length + 1;
			}
			break;
		}
1474
#endif
Paul McCullagh's avatar
Paul McCullagh committed
1475 1476 1477 1478 1479
#ifdef HAVE_LONG_LONG
		case HA_KEYTYPE_LONGLONG:
		case HA_KEYTYPE_ULONGLONG:
			break;
#endif
1480
#ifndef DRIZZLED
Paul McCullagh's avatar
Paul McCullagh committed
1481 1482 1483
		case HA_KEYTYPE_BIT:
			/* TODO: What here? */
			break;
1484
#endif
Paul McCullagh's avatar
Paul McCullagh committed
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
		case HA_KEYTYPE_END:												/* Ready */
			break;
	}

	return key_length;
}

/*
 * -----------------------------------------------------------------------
 * Load and store rows
 */

xtPublic xtWord4 myxt_store_row_length(XTOpenTablePtr ot, char *rec_buff)
{
	TABLE	*table = ot->ot_table->tab_dic.dic_my_table;
	char	*sdata;
	xtWord4	dlen;
	xtWord4	item_size;
	xtWord4 row_size = 0;

 	for (Field **field=table->field ; *field ; field++) {
		if ((*field)->is_null_in_record((const uchar *) rec_buff)) {
 			sdata = NULL;
 			dlen = 0;
 			item_size = 1;
 		}
 		else {
			sdata = mx_get_length_and_data(*field, rec_buff, &dlen);
			if (!dlen) {
				/* Empty, but not null (blobs may return NULL, when
				 * length is 0.
				 */
				sdata = rec_buff; // Any valid pointer will do
				item_size = 1 + dlen;
			}
			else if (dlen <= 240)
				item_size = 1 + dlen;
			else if (dlen <= 0xFFFF)
				item_size = 3 + dlen;
			else if (dlen <= 0xFFFFFF)
				item_size = 4 + dlen;
			else
				item_size = 5 + dlen;
		}

		row_size += item_size;
	}
	return row_size;
}

1535
xtPublic xtWord4 myxt_store_row_data(XTOpenTablePtr ot, xtWord4 row_size, char *rec_buff)
Paul McCullagh's avatar
Paul McCullagh committed
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662
{
	TABLE	*table = ot->ot_table->tab_dic.dic_my_table;
	char	*sdata;
	xtWord4	dlen;
	xtWord4	item_size;

 	for (Field **field=table->field ; *field ; field++) {
		if ((*field)->is_null_in_record((const uchar *) rec_buff)) {
 			sdata = NULL;
 			dlen = 0;
 			item_size = 1;
 		}
 		else {
			sdata = mx_get_length_and_data(*field, rec_buff, &dlen);
			if (!dlen) {
				/* Empty, but not null (blobs may return NULL, when
				 * length is 0.
				 */
				sdata = rec_buff; // Any valid pointer will do
				item_size = 1 + dlen;
			}
			else if (dlen <= 240)
				item_size = 1 + dlen;
			else if (dlen <= 0xFFFF)
				item_size = 3 + dlen;
			else if (dlen <= 0xFFFFFF)
				item_size = 4 + dlen;
			else
				item_size = 5 + dlen;
		}

		if (row_size + item_size > ot->ot_row_wbuf_size) {
			if (!xt_realloc_ns((void **) &ot->ot_row_wbuffer, row_size + item_size))
				return 0;
			ot->ot_row_wbuf_size = row_size + item_size;
		}

		if (!sdata)
			ot->ot_row_wbuffer[row_size] = 255;
		else if (dlen <= 240) {
			ot->ot_row_wbuffer[row_size] = (unsigned char) dlen;
			memcpy(&ot->ot_row_wbuffer[row_size+1], sdata, dlen);
		}
		else if (dlen <= 0xFFFF) {
			ot->ot_row_wbuffer[row_size] = 254;
			XT_SET_DISK_2(&ot->ot_row_wbuffer[row_size+1], dlen);
			memcpy(&ot->ot_row_wbuffer[row_size+3], sdata, dlen);
		}
		else if (dlen <= 0xFFFFFF) {
			ot->ot_row_wbuffer[row_size] = 253;
			XT_SET_DISK_3(&ot->ot_row_wbuffer[row_size+1], dlen);
			memcpy(&ot->ot_row_wbuffer[row_size+4], sdata, dlen);
		}
		else {
			ot->ot_row_wbuffer[row_size] = 252;
			XT_SET_DISK_4(&ot->ot_row_wbuffer[row_size+1], dlen);
			memcpy(&ot->ot_row_wbuffer[row_size+5], sdata, dlen);
		}

		row_size += item_size;
	}
	return row_size;
}

/* Count the number and size of whole columns in the given buffer. */
xtPublic size_t myxt_load_row_length(XTOpenTablePtr ot, size_t buffer_size, xtWord1 *source_buf, u_int *ret_col_cnt)
{
	u_int	col_cnt;
	xtWord4	len;
	size_t	size = 0;
	u_int	i;

	col_cnt = ot->ot_table->tab_dic.dic_no_of_cols;
	if (ret_col_cnt)
		col_cnt = *ret_col_cnt;
 	for (i=0; i<col_cnt; i++) {
		if (size + 1 > buffer_size)
			goto done;
 		switch (*source_buf) {
			case 255: // Indicate NULL value
				size++;
				source_buf++;
				break;
			case 254: // 2 bytes length
				if (size + 3 > buffer_size)
					goto done;
				len = XT_GET_DISK_2(source_buf + 1);
				if (size + 3 + len > buffer_size)
					goto done;
				size += 3 + len;
				source_buf += 3 + len;
				break;
			case 253: // 3 bytes length
				if (size + 4 > buffer_size)
					goto done;
				len = XT_GET_DISK_3(source_buf + 1);
				if (size + 4 + len > buffer_size)
					goto done;
				size += 4 + len;
				source_buf += 4 + len;
				break;
			case 252: // 4 bytes length
				if (size + 5 > buffer_size)
					goto done;
				len = XT_GET_DISK_4(source_buf + 1);
				if (size + 5 + len > buffer_size)
					goto done;
				size += 5 + len;
				source_buf += 5 + len;
				break;
			default: // Length byte
				len = *source_buf;
				if (size + 1 + len > buffer_size)
					goto done;
				size += 1 + len;
				source_buf += 1 + len;
				break;
 		}
	}
	
	done:
	if (ret_col_cnt)
		*ret_col_cnt = i;
	return size;
}

/* Unload from PBXT variable length format to the MySQL row format. */
1663
xtPublic xtWord4 myxt_load_row_data(XTOpenTablePtr ot, xtWord1 *source_buf, xtWord1 *dest_buff, u_int col_cnt)
Paul McCullagh's avatar
Paul McCullagh committed
1664
{
1665
	xtWord1 *input_buf = source_buf;
Paul McCullagh's avatar
Paul McCullagh committed
1666 1667 1668 1669 1670 1671 1672 1673
	TABLE	*table;
	xtWord4	len;
	Field	*curr_field;
	xtBool	is_null;
	u_int	i = 0;

	if (!(table = ot->ot_table->tab_dic.dic_my_table)) {
		xt_register_taberr(XT_REG_CONTEXT, XT_ERR_NO_DICTIONARY, ot->ot_table->tab_name);
1674
		return 0;
Paul McCullagh's avatar
Paul McCullagh committed
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
	}

	/* According to the InnoDB implementation:
	 * "MySQL assumes that all columns
	 * have the SQL NULL bit set unless it
	 * is a nullable column with a non-NULL value".
	 */
	memset(dest_buff, 0xFF, table->s->null_bytes);
 	for (Field **field=table->field ; *field && (!col_cnt || i<col_cnt); field++, i++) {
		curr_field = *field;
 		is_null = FALSE;
 		switch (*source_buf) {
			case 255: // Indicate NULL value
				is_null = TRUE;
				len = 0;
				source_buf++;
				break;
			case 254: // 2 bytes length
				len = XT_GET_DISK_2(source_buf + 1);
				source_buf += 3;
				break;
			case 253: // 3 bytes length
				len = XT_GET_DISK_3(source_buf + 1);
				source_buf += 4;
				break;
			case 252: // 4 bytes length
				len = XT_GET_DISK_4(source_buf + 1);
				source_buf += 5;
				break;
			default: // Length byte
				if (*source_buf > 240) {
					xt_register_xterr(XT_REG_CONTEXT, XT_ERR_BAD_RECORD_FORMAT);
1707
					return 0;
Paul McCullagh's avatar
Paul McCullagh committed
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720
				}
				len = *source_buf;
				source_buf++;
				break;
 		}

		if (is_null)
			mx_set_length_and_data(curr_field, (char *) dest_buff, 0, NULL);
		else
			mx_set_length_and_data(curr_field, (char *) dest_buff, len, (char *) source_buf);

		source_buf += len;
 	}
1721 1722 1723 1724 1725 1726
	return (xtWord4) (source_buf - input_buf);
}

xtPublic xtBool myxt_load_row(XTOpenTablePtr ot, xtWord1 *source_buf, xtWord1 *dest_buff, u_int col_cnt)
{
	return myxt_load_row_data(ot, source_buf, dest_buff, col_cnt) != 0;
Paul McCullagh's avatar
Paul McCullagh committed
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767
}

xtPublic xtBool myxt_find_column(XTOpenTablePtr ot, u_int *col_idx, const char *col_name)
{
	TABLE	*table = ot->ot_table->tab_dic.dic_my_table;
	u_int	i=0;

	for (Field **field=table->field; *field; field++, i++) {
		if (!my_strcasecmp(system_charset_info, (*field)->field_name, col_name)) {
			*col_idx = i;
			return OK;
		}
	}
	return FALSE;
}

xtPublic void myxt_get_column_name(XTOpenTablePtr ot, u_int col_idx, u_int len, char *col_name)
{
	TABLE	*table = ot->ot_table->tab_dic.dic_my_table;
	Field	*field;

	field = table->field[col_idx];
	xt_strcpy(len, col_name, field->field_name);
}

xtPublic void myxt_get_column_as_string(XTOpenTablePtr ot, char *buffer, u_int col_idx, u_int len, char *value)
{
	XTTableHPtr	tab = ot->ot_table;
	XTThreadPtr self = ot->ot_thread;
	TABLE		*table = tab->tab_dic.dic_my_table;
	Field		*field = table->field[col_idx];
	char		buf_val[MAX_FIELD_WIDTH];
	String		val(buf_val, sizeof(buf_val), &my_charset_bin);

	if (mx_is_null_in_record(field, buffer))
		xt_strcpy(len, value, "NULL");
	else {
		byte	*save;

		/* Required by store() - or an assertion will fail: */
		if (table->read_set)
1768
			MX_BIT_SET(table->read_set, col_idx);
Paul McCullagh's avatar
Paul McCullagh committed
1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795

		save = field->ptr;
		xt_lock_mutex(self, &tab->tab_dic_field_lock);
		pushr_(xt_unlock_mutex, &tab->tab_dic_field_lock);
#if MYSQL_VERSION_ID < 50114
		field->ptr = (byte *) buffer + field->offset();
#else
		field->ptr = (byte *) buffer + field->offset(field->table->record[0]);
#endif
		field->val_str(&val);
		field->ptr = save;					// Restore org row pointer
		freer_(); // xt_unlock_mutex(&tab->tab_dic_field_lock)
		xt_strcpy(len, value, val.c_ptr());
	}
}

xtPublic xtBool myxt_set_column(XTOpenTablePtr ot, char *buffer, u_int col_idx, const char *value, u_int len)
{
	XTTableHPtr	tab = ot->ot_table;
	XTThreadPtr self = ot->ot_thread;
	TABLE		*table = tab->tab_dic.dic_my_table;
	Field		*field = table->field[col_idx];
	byte		*save;
	int			error;

	/* Required by store() - or an assertion will fail: */
	if (table->write_set)
1796
		MX_BIT_SET(table->write_set, col_idx);
Paul McCullagh's avatar
Paul McCullagh committed
1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838

	mx_set_notnull_in_record(field, buffer);

	save = field->ptr;
	xt_lock_mutex(self, &tab->tab_dic_field_lock);
	pushr_(xt_unlock_mutex, &tab->tab_dic_field_lock);
#if MYSQL_VERSION_ID < 50114
	field->ptr = (byte *) buffer + field->offset();
#else
	field->ptr = (byte *) buffer + field->offset(field->table->record[0]);
#endif
	error = field->store(value, len, &my_charset_utf8_general_ci);
	field->ptr = save;					// Restore org row pointer
	freer_(); // xt_unlock_mutex(&tab->tab_dic_field_lock)
	return error ? FAILED : OK;
}

xtPublic void myxt_get_column_data(XTOpenTablePtr ot, char *buffer, u_int col_idx, char **value, size_t *len)
{
	TABLE	*table = ot->ot_table->tab_dic.dic_my_table;
	Field	*field = table->field[col_idx];
	char	*sdata;
	xtWord4	dlen;

	sdata = mx_get_length_and_data(field, buffer, &dlen);
	*value = sdata;
	*len = dlen;
}

xtPublic xtBool myxt_store_row(XTOpenTablePtr ot, XTTabRecInfoPtr rec_info, char *rec_buff)
{
	if (ot->ot_rec_fixed) {
		rec_info->ri_fix_rec_buf = (XTTabRecFixDPtr) ot->ot_row_wbuffer;
		rec_info->ri_rec_buf_size = ot->ot_rec_size;
		rec_info->ri_ext_rec = NULL;

		rec_info->ri_fix_rec_buf->tr_rec_type_1 = XT_TAB_STATUS_FIXED;
		memcpy(rec_info->ri_fix_rec_buf->rf_data, rec_buff, ot->ot_rec_size - XT_REC_FIX_HEADER_SIZE);
	}
	else {
		xtWord4 row_size;

1839
		if (!(row_size = myxt_store_row_data(ot, XT_REC_EXT_HEADER_SIZE, rec_buff)))
Paul McCullagh's avatar
Paul McCullagh committed
1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927
			return FAILED;
		if (row_size - XT_REC_FIX_EXT_HEADER_DIFF <= ot->ot_rec_size) {	
			rec_info->ri_fix_rec_buf = (XTTabRecFixDPtr) &ot->ot_row_wbuffer[XT_REC_FIX_EXT_HEADER_DIFF];
			rec_info->ri_rec_buf_size = row_size - XT_REC_FIX_EXT_HEADER_DIFF;
			rec_info->ri_ext_rec = NULL;

			rec_info->ri_fix_rec_buf->tr_rec_type_1 = XT_TAB_STATUS_VARIABLE;
		}
		else {
			rec_info->ri_fix_rec_buf = (XTTabRecFixDPtr) ot->ot_row_wbuffer;
			rec_info->ri_rec_buf_size = ot->ot_rec_size;
			rec_info->ri_ext_rec = (XTTabRecExtDPtr) ot->ot_row_wbuffer;
			rec_info->ri_log_data_size = row_size - ot->ot_rec_size;
			rec_info->ri_log_buf = (XTactExtRecEntryDPtr) &ot->ot_row_wbuffer[ot->ot_rec_size - offsetof(XTactExtRecEntryDRec, er_data)];

			rec_info->ri_ext_rec->tr_rec_type_1 = XT_TAB_STATUS_EXT_DLOG;
			XT_SET_DISK_4(rec_info->ri_ext_rec->re_log_dat_siz_4, rec_info->ri_log_data_size);
		}
	}
	return OK;
}

static void mx_print_string(uchar *s, uint count)
{
	while (count > 0) {
		if (s[count - 1] != ' ')
			break;
		count--;
	}
	printf("\"");
	for (u_int i=0; i<count; i++, s++)
		printf("%c", *s);
	printf("\"");
}

xtPublic void myxt_print_key(XTIndexPtr ind, xtWord1 *key_value)
{
	register XTIndexSegRec	*keyseg = ind->mi_seg;
	register uchar			*b = (uchar *) key_value;
	uint					b_length;
	uint					pack_len;

	for (u_int i = 0; i < ind->mi_seg_count; i++, keyseg++) {
		if (i!=0)
			printf(" ");
		if (keyseg->null_bit) {
			if (!*b++) {
				printf("NULL");
				continue;
			}
		}
		switch ((enum ha_base_keytype) keyseg->type) {
			case HA_KEYTYPE_TEXT:											 /* Ascii; Key is converted */
				if (keyseg->flag & HA_SPACE_PACK) {
					get_key_pack_length(b_length, pack_len, b);
				}
				else
					b_length = keyseg->length;
				mx_print_string(b, b_length);
				b += b_length;
				break;
			case HA_KEYTYPE_LONG_INT: {
				int32 l_2 = sint4korr(b);
				b += keyseg->length;
				printf("%ld", (long) l_2);
				break;
			}
			case HA_KEYTYPE_ULONG_INT: {
				xtWord4 u_2 = sint4korr(b);
				b += keyseg->length;
				printf("%lu", (u_long) u_2);
				break;
			}
			default:
				break;
		}
	}
}

/*
 * -----------------------------------------------------------------------
 * MySQL Data Dictionary
 */

#define TS(x)					(x)->s

static void my_close_table(TABLE *table)
{
1928 1929 1930 1931 1932 1933
#ifdef DRIZZLED
	TABLE_SHARE	*share;

	share = (TABLE_SHARE *) ((char *) table + sizeof(TABLE));
	share->free_table_share();
#else
Paul McCullagh's avatar
Paul McCullagh committed
1934 1935 1936 1937 1938 1939 1940 1941 1942
	closefrm(table, 1);  // TODO: Q, why did Stewart remove this?
#endif
	xt_free_ns(table);
}

/*
 * This function returns NULL if the table cannot be opened 
 * because this is not a MySQL thread.
 */ 
1943
static TABLE *my_open_table(XTThreadPtr self, XTDatabaseHPtr XT_UNUSED(db), XTPathStrPtr tab_path)
Paul McCullagh's avatar
Paul McCullagh committed
1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003
{
	THD			*thd = current_thd;
	char		path_buffer[PATH_MAX];
	char		*table_name;
	char		database_name[XT_IDENTIFIER_NAME_SIZE];
	char		*ptr;
	size_t		size;
	char		*buffer, *path, *db_name, *name;
	TABLE_SHARE	*share;
	int			error;
	TABLE		*table;

	/* If we have no MySQL thread, then we cannot open this table!
	 * What this means is the thread is probably the sweeper or the
	 * compactor.
	 */
	if (!thd)
		return NULL;

	/* GOTCHA: Check if the table name is a partitian,
	 * if so we need to remove the partition
	 * extension, in order for this to work!
	 *
	 * Reason: the parts of a partition table do not
	 * have .frm files!!
	 */
	xt_strcpy(PATH_MAX, path_buffer, tab_path->ps_path);
	table_name = xt_last_name_of_path(path_buffer);
	if ((ptr = strstr(table_name, "#P#")))
		*ptr = 0;

	xt_2nd_last_name_of_path(XT_IDENTIFIER_NAME_SIZE, database_name, path_buffer);

	size = sizeof(TABLE) + sizeof(TABLE_SHARE) + 
		strlen(path_buffer) + 1 +
		strlen(database_name) + 1 + strlen(table_name) + 1;
	if (!(buffer = (char *) xt_malloc(self, size)))
		return NULL;
	table = (TABLE *) buffer;
	buffer += sizeof(TABLE);
	share = (TABLE_SHARE *) buffer;
	buffer += sizeof(TABLE_SHARE);

	path = buffer;
	strcpy(path, path_buffer);
	buffer += strlen(path_buffer) + 1;
	db_name = buffer;
	strcpy(db_name, database_name);
	buffer += strlen(database_name) + 1;
	name = buffer;
	strcpy(name, table_name);

	/* Required to call 'open_table_from_share'! */
	LEX *old_lex, new_lex;

	old_lex = thd->lex;
	thd->lex = &new_lex;
	new_lex.current_select= NULL;
	lex_start(thd);

2004 2005
#ifdef DRIZZLED
	share->init(db_name, 0, name, path);
2006
	if ((error = open_table_def(*thd, share)) ||
2007 2008 2009 2010 2011
		(error = open_table_from_share(thd, share, "", 0, (uint32_t) READ_ALL, 0, table, OTM_OPEN)))
	{
		xt_free(self, table);
		lex_end(&new_lex);
		thd->lex = old_lex;
2012
		xt_throw_sulxterr(XT_CONTEXT, XT_ERR_LOADING_MYSQL_DIC, tab_path->ps_path, (u_long) error);
2013 2014 2015
		return NULL;
	}
#else
Paul McCullagh's avatar
Paul McCullagh committed
2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029
#if MYSQL_VERSION_ID < 60000
#if MYSQL_VERSION_ID < 50123
	init_tmp_table_share(share, db_name, 0, name, path);
#else
	init_tmp_table_share(thd, share, db_name, 0, name, path);
#endif
#else
#if MYSQL_VERSION_ID < 60004
	init_tmp_table_share(share, db_name, 0, name, path);
#else
	init_tmp_table_share(thd, share, db_name, 0, name, path);
#endif
#endif

2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041
	/* If MySQL shutsdown while we are just starting up, they
	 * they kill the plugin sub-system before calling
	 * shutdown for the engine!
	 */
	if (!ha_resolve_by_legacy_type(thd, DB_TYPE_PBXT)) {
		xt_free(self, table);
		lex_end(&new_lex);
		thd->lex = old_lex;
		xt_throw_xterr(XT_CONTEXT, XT_ERR_MYSQL_SHUTDOWN);
		return NULL;
	}

Paul McCullagh's avatar
Paul McCullagh committed
2042 2043 2044 2045
	if ((error = open_table_def(thd, share, 0))) {
		xt_free(self, table);
		lex_end(&new_lex);
		thd->lex = old_lex;
2046
		xt_throw_sulxterr(XT_CONTEXT, XT_ERR_LOADING_MYSQL_DIC, tab_path->ps_path, (u_long) error);
Paul McCullagh's avatar
Paul McCullagh committed
2047 2048 2049
		return NULL;
	}

2050
#if MYSQL_VERSION_ID >= 50404
Paul McCullagh's avatar
Paul McCullagh committed
2051 2052 2053 2054 2055 2056 2057 2058
	if ((error = open_table_from_share(thd, share, "", 0, (uint) READ_ALL, 0, table, OTM_OPEN)))
#else
	if ((error = open_table_from_share(thd, share, "", 0, (uint) READ_ALL, 0, table, FALSE)))
#endif
	{
		xt_free(self, table);
		lex_end(&new_lex);
		thd->lex = old_lex;
2059
		xt_throw_sulxterr(XT_CONTEXT, XT_ERR_LOADING_MYSQL_DIC, tab_path->ps_path, (u_long) error);
Paul McCullagh's avatar
Paul McCullagh committed
2060 2061
		return NULL;
	}
2062
#endif
Paul McCullagh's avatar
Paul McCullagh committed
2063 2064 2065 2066 2067 2068 2069 2070 2071

	lex_end(&new_lex);
	thd->lex = old_lex;

	/* GOTCHA: I am the plug-in!!! Therefore, I should not hold 
	 * a reference to myself. By holding this reference I prevent
	 * plugin_shutdown() and reap_plugins() in sql_plugin.cc
	 * from doing their job on shutdown!
	 */
2072
#ifndef DRIZZLED
Paul McCullagh's avatar
Paul McCullagh committed
2073 2074
	plugin_unlock(NULL, table->s->db_plugin);
	table->s->db_plugin = NULL;
2075
#endif
Paul McCullagh's avatar
Paul McCullagh committed
2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153
	return table;
}

/*
static bool my_match_index(XTDDIndex *ind, KEY *index)
{
	KEY_PART_INFO	*key_part;
	KEY_PART_INFO	*key_part_end;
	u_int			j;
	XTDDColumnRef	*cref;

	if (index->key_parts != ind->co_cols.size())
		return false;

	j=0;
	key_part_end = index->key_part + index->key_parts;
	for (key_part = index->key_part; key_part != key_part_end; key_part++, j++) {
		if (!(cref = ind->co_cols.itemAt(j)))
			return false;
		if (myxt_strcasecmp(cref->cr_col_name, (char *) key_part->field->field_name) != 0)
			return false;
	}

	if (ind->co_type == XT_DD_KEY_PRIMARY) {
		if (!(index->flags & HA_NOSAME))
			return false;
	}
	else {
		if (ind->co_type == XT_DD_INDEX_UNIQUE) {
			if (!(index->flags & HA_NOSAME))
				return false;
		}
		if (ind->co_ind_name) {
			if (myxt_strcasecmp(ind->co_ind_name, index->name) != 0)
				return false;
		}
	}

	return true;
}

static XTDDIndex *my_find_index(XTDDTable *dd_tab, KEY *index)
{
	XTDDIndex *ind;

	for (u_int i=0; i<dd_tab->dt_indexes.size(); i++)
	{
		ind = dd_tab->dt_indexes.itemAt(i);
		if (my_match_index(ind, index))
			return ind;
	}
	return NULL;
}
*/

static void my_deref_index_data(struct XTThread *self, XTIndexPtr mi)
{
	enter_();
	/* The dirty list of cache pages should be empty here! */
	ASSERT(!mi->mi_dirty_list);
	ASSERT(!mi->mi_free_list);

	xt_free_mutex(&mi->mi_flush_lock);
	xt_spinlock_free(self, &mi->mi_dirty_lock);
	XT_INDEX_FREE_LOCK(self, mi);
	myxt_bitmap_free(self, &mi->mi_col_map);
	if (mi->mi_free_list)
		xt_free(self, mi->mi_free_list);

	xt_free(self, mi);
	exit_();
}

static xtBool my_is_not_null_int4(XTIndexSegPtr seg)
{
	return (seg->type == HA_KEYTYPE_LONG_INT && !(seg->flag & HA_NULL_PART));
}

2154 2155 2156 2157 2158
/* MY_BITMAP definition in Drizzle does not like if
 * I use a NULL pointer to calculate the offset!?
 */
#define MX_OFFSETOF(x, y)		((size_t)(&((x *) 8)->y) - 8)

Paul McCullagh's avatar
Paul McCullagh committed
2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173
/* Derived from ha_myisam::create and mi_create */
static XTIndexPtr my_create_index(XTThreadPtr self, TABLE *table_arg, u_int idx, KEY *index)
{
	XTIndexPtr				ind;
	KEY_PART_INFO			*key_part;
	KEY_PART_INFO			*key_part_end;
	XTIndexSegRec			*seg;
	Field					*field;
	enum ha_base_keytype	type;
	uint					options = 0;
	u_int					key_length = 0;
	xtBool					partial_field;

	enter_();

2174
	pushsr_(ind, my_deref_index_data, (XTIndexPtr) xt_calloc(self, MX_OFFSETOF(XTIndexRec, mi_seg) + sizeof(XTIndexSegRec) * index->key_parts));
Paul McCullagh's avatar
Paul McCullagh committed
2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199

	XT_INDEX_INIT_LOCK(self, ind);
	xt_init_mutex_with_autoname(self, &ind->mi_flush_lock);
	xt_spinlock_init_with_autoname(self, &ind->mi_dirty_lock);
	ind->mi_index_no = idx;
	ind->mi_flags = (index->flags & (HA_NOSAME | HA_NULL_ARE_EQUAL | HA_UNIQUE_CHECK));
	ind->mi_low_byte_first = TS(table_arg)->db_low_byte_first;
	ind->mi_fix_key = TRUE;
	ind->mi_select_total = 0;
	ind->mi_subset_of = 0;
	myxt_bitmap_init(self, &ind->mi_col_map, TS(table_arg)->fields);
	
	ind->mi_seg_count = (uint) index->key_parts;
	key_part_end = index->key_part + index->key_parts;
	seg = ind->mi_seg;
	for (key_part = index->key_part; key_part != key_part_end; key_part++, seg++) {
		partial_field = FALSE;
		field = key_part->field;

		type = field->key_type();
		seg->flag = key_part->key_part_flag;

		if (options & HA_OPTION_PACK_KEYS ||
			(index->flags & (HA_PACK_KEY | HA_BINARY_PACK_KEY | HA_SPACE_PACK_USED)))
		{
2200 2201 2202 2203
			if (key_part->length > 8 && (type == HA_KEYTYPE_TEXT || 
#ifndef DRIZZLED
				type == HA_KEYTYPE_NUM ||
#endif
Paul McCullagh's avatar
Paul McCullagh committed
2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270
				(type == HA_KEYTYPE_BINARY && !field->zero_pack())))
			{
				/* No blobs here */
				if (key_part == index->key_part)
					ind->mi_flags |= HA_PACK_KEY;
#ifndef DRIZZLED
				if (!(field->flags & ZEROFILL_FLAG) &&
					(field->type() == MYSQL_TYPE_STRING ||
					field->type() == MYSQL_TYPE_VAR_STRING ||
					((int) (key_part->length - field->decimals())) >= 4))
	    			seg->flag |= HA_SPACE_PACK;
#endif
			}
		}

		seg->col_idx = field->field_index;
		seg->is_recs_in_range = 1;
		seg->is_selectivity = 1;
		seg->type = (int) type;
		seg->start = key_part->offset;
		seg->length = key_part->length;
		seg->bit_start = seg->bit_end = 0;
		seg->bit_length = seg->bit_pos = 0;
		seg->charset = field->charset();

		if (field->null_ptr) {
			key_length++;
			seg->flag |= HA_NULL_PART;
			seg->null_bit = field->null_bit;
			seg->null_pos = (uint) (field->null_ptr - (uchar*) table_arg->record[0]);
		}
		else {
			seg->null_bit = 0;
			seg->null_pos = 0;
		}

		if (field->real_type() == MYSQL_TYPE_ENUM
#ifndef DRIZZLED
			|| field->real_type() == MYSQL_TYPE_SET
#endif
			) {
			/* This values are not indexed as string!!
			 * The index will not be built correctly if this value is non-NULL.
			 */
			seg->charset = NULL;
		}

		if (field->type() == MYSQL_TYPE_BLOB
#ifndef DRIZZLED
			|| field->type() == MYSQL_TYPE_GEOMETRY
#endif
			) {
			seg->flag |= HA_BLOB_PART;
			/* save number of bytes used to pack length */
			seg->bit_start = (uint) (field->pack_length() - TS(table_arg)->blob_ptr_size);
		}
#ifndef DRIZZLED
		else if (field->type() == MYSQL_TYPE_BIT) {
			seg->bit_length = ((Field_bit *) field)->bit_len;
			seg->bit_start = ((Field_bit *) field)->bit_ofs;
			seg->bit_pos = (uint) (((Field_bit *) field)->bit_ptr - (uchar*) table_arg->record[0]);
		}
#else
		/* Drizzle uses HA_KEYTYPE_ULONG_INT keys for enums > 1 byte, which is not consistent with MySQL, so we fix it here  */
		else if (field->type() == MYSQL_TYPE_ENUM) {
			switch (seg->length) {
				case 2: 
2271 2272 2273
#ifdef DRIZZLED
					ASSERT_NS(FALSE);
#else
Paul McCullagh's avatar
Paul McCullagh committed
2274 2275
					seg->type = HA_KEYTYPE_USHORT_INT;
					break;
2276
#endif
Paul McCullagh's avatar
Paul McCullagh committed
2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331
				case 3:
					seg->type = HA_KEYTYPE_UINT24;
					break;
			}
		}
#endif

		switch (seg->type) {
			case HA_KEYTYPE_VARTEXT1:
			case HA_KEYTYPE_VARTEXT2:
			case HA_KEYTYPE_VARBINARY1:
			case HA_KEYTYPE_VARBINARY2:
				if (!(seg->flag & HA_BLOB_PART)) {
					/* Make a flag that this is a VARCHAR */
					seg->flag |= HA_VAR_LENGTH_PART;
					/* Store in bit_start number of bytes used to pack the length */
					seg->bit_start = ((seg->type == HA_KEYTYPE_VARTEXT1 || seg->type == HA_KEYTYPE_VARBINARY1) ? 1 : 2);
				}
				break;
		}

		/* All packed fields start with a length (1 or 3 bytes): */
		if (seg->flag & (HA_VAR_LENGTH_PART | HA_BLOB_PART | HA_SPACE_PACK)) {
			key_length++;				/* At least one length byte */
			if (seg->length >= 255)	/* prefix may be 3 bytes */
	    		key_length +=2;
		}

		key_length += seg->length;
		if (seg->length > 40)
			ind->mi_fix_key = FALSE;

		/* Determine if only part of the field is in the key:
		 * This is important for index coverage!
		 * Note, BLOB fields are never retrieved from
		 * an index!
		 */
		if (field->type() == MYSQL_TYPE_BLOB)
			partial_field = TRUE;
		else if (field->real_type() == MYSQL_TYPE_VARCHAR		// For varbinary type
#ifndef DRIZZLED
			|| field->real_type() == MYSQL_TYPE_VAR_STRING		// For varbinary type
			|| field->real_type() == MYSQL_TYPE_STRING			// For binary type
#endif
			)
		{
			Field	*tab_field = table_arg->field[key_part->fieldnr-1];
			u_int	field_len = tab_field->key_length();

			if (key_part->length != field_len)
				partial_field = TRUE;
		}

		/* NOTE: do not set if the field is only partially in the index!!! */
		if (!partial_field)
2332
			MX_BIT_FAST_TEST_AND_SET(&ind->mi_col_map, field->field_index);
Paul McCullagh's avatar
Paul McCullagh committed
2333 2334 2335 2336 2337 2338 2339
	}

	if (key_length > XT_INDEX_MAX_KEY_SIZE)
		xt_throw_sulxterr(XT_CONTEXT, XT_ERR_KEY_TOO_LARGE, index->name, (u_long) XT_INDEX_MAX_KEY_SIZE);

	/* This is the maximum size of the index on disk: */
	ind->mi_key_size = key_length;
2340
	ind->mi_max_items = (XT_INDEX_PAGE_SIZE-2) / (key_length+XT_RECORD_REF_SIZE);
Paul McCullagh's avatar
Paul McCullagh committed
2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378

	if (ind->mi_fix_key) {
		/* Special case for not-NULL 4 byte int value: */
		switch (ind->mi_seg_count) {
			case 1:
				ind->mi_single_type = ind->mi_seg[0].type;
				if (ind->mi_seg[0].type == HA_KEYTYPE_LONG_INT ||
					ind->mi_seg[0].type == HA_KEYTYPE_ULONG_INT) {
					if (!(ind->mi_seg[0].flag & HA_NULL_PART))
						ind->mi_scan_branch = xt_scan_branch_single;
				}
				break;
			case 2:
				if (my_is_not_null_int4(&ind->mi_seg[0]) &&
					my_is_not_null_int4(&ind->mi_seg[1])) {
					ind->mi_scan_branch = xt_scan_branch_fix_simple;
					ind->mi_simple_comp_key = xt_compare_2_int4;
				}
				break;
			case 3:
				if (my_is_not_null_int4(&ind->mi_seg[0]) &&
					my_is_not_null_int4(&ind->mi_seg[1]) &&
					my_is_not_null_int4(&ind->mi_seg[2])) {
					ind->mi_scan_branch = xt_scan_branch_fix_simple;
					ind->mi_simple_comp_key = xt_compare_3_int4;
				}
				break;
		}
		if (!ind->mi_scan_branch)
			ind->mi_scan_branch = xt_scan_branch_fix;
		ind->mi_prev_item = xt_prev_branch_item_fix;
		ind->mi_last_item = xt_last_branch_item_fix;
	}
	else {
		ind->mi_scan_branch = xt_scan_branch_var;
		ind->mi_prev_item = xt_prev_branch_item_var;
		ind->mi_last_item = xt_last_branch_item_var;
	}
2379
	ind->mi_lazy_delete = ind->mi_fix_key && ind->mi_max_items >= 4;
Paul McCullagh's avatar
Paul McCullagh committed
2380 2381 2382 2383 2384 2385 2386 2387

	XT_NODE_ID(ind->mi_root) = 0;

	popr_(); // Discard my_deref_index_data(ind)

	return_(ind);
}

Paul McCullagh's avatar
Paul McCullagh committed
2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416
/* We estimate the size of BLOBs depending on the number
 * of BLOBs in the table.
 */
static u_int mx_blob_field_size_total[] = {
	500,	// 1
	400,	// 2
	350,	// 3
	320,	// 4
	300,	// 5
	280,	// 6
	260,	// 7
	240,	// 8
	220,	// 9
	210		// 10
};

static u_int mxvarchar_field_min_ave[] = {
	120,	// 1
	105,	// 2
	90,		// 3
	65,		// 4
	50,		// 5
	40,		// 6
	40,		// 7
	40,		// 8
	40,		// 9
	40		// 10
};

Paul McCullagh's avatar
Paul McCullagh committed
2417 2418 2419 2420 2421
xtPublic void myxt_setup_dictionary(XTThreadPtr self, XTDictionaryPtr dic)
{
	TABLE	*my_tab = dic->dic_my_table;
	u_int	field_count;
	u_int	var_field_count = 0;
Paul McCullagh's avatar
Paul McCullagh committed
2422 2423 2424
	u_int	varchar_field_count = 0;
	u_int	blob_field_count = 0;
	u_int	large_blob_field_count = 0;
Paul McCullagh's avatar
Paul McCullagh committed
2425 2426 2427 2428 2429 2430
	xtWord8 min_data_size = 0;
	xtWord8 max_data_size = 0;
	xtWord8 ave_data_size = 0;
	xtWord8 min_row_size = 0;
	xtWord8 max_row_size = 0;
	xtWord8 ave_row_size = 0;
Paul McCullagh's avatar
Paul McCullagh committed
2431
	xtWord8 min_ave_row_size = 0;
Paul McCullagh's avatar
Paul McCullagh committed
2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442
	xtWord8 max_ave_row_size = 0;
	u_int	dic_rec_size;
	xtBool	dic_rec_fixed;
	Field	*curr_field;
	Field	**field;

	/* How many columns are required for all indexes. */
	KEY				*index;
	KEY_PART_INFO	*key_part;
	KEY_PART_INFO	*key_part_end;

2443 2444 2445 2446
#ifndef XT_USE_LAZY_DELETE
	dic->dic_no_lazy_delete = TRUE;
#endif

Paul McCullagh's avatar
Paul McCullagh committed
2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468
	dic->dic_ind_cols_req = 0;
	for (uint i=0; i<TS(my_tab)->keys; i++) {
		index = &my_tab->key_info[i];

		key_part_end = index->key_part + index->key_parts;
		for (key_part = index->key_part; key_part != key_part_end; key_part++) {
			curr_field = key_part->field;

			if ((u_int) curr_field->field_index+1 > dic->dic_ind_cols_req)
				dic->dic_ind_cols_req = curr_field->field_index+1;
		}
	}

	/* We will work out how many columns are required for all blobs: */
	dic->dic_blob_cols_req = 0;	
	field_count = 0;
 	for (field=my_tab->field; (curr_field = *field); field++) {
 		field_count++;
 		min_data_size = curr_field->key_length();
 		max_data_size = curr_field->key_length();
		enum_field_types tno = curr_field->type();

Paul McCullagh's avatar
Paul McCullagh committed
2469
		min_ave_row_size = 40;
Paul McCullagh's avatar
Paul McCullagh committed
2470 2471 2472 2473 2474 2475
		max_ave_row_size = 128;
 		if (tno == MYSQL_TYPE_BLOB) {
			blob_field_count++;
			min_data_size = 0;
			max_data_size = ((Field_blob *) curr_field)->max_data_length();
			/* Set the average length higher for BLOBs: */
Paul McCullagh's avatar
Paul McCullagh committed
2476 2477 2478 2479 2480 2481 2482 2483
			if (max_data_size == 0xFFFF ||
				max_data_size == 0xFFFFFF) {
				if (large_blob_field_count < 10)
					max_ave_row_size = mx_blob_field_size_total[large_blob_field_count];
				else
					max_ave_row_size = 200;
				large_blob_field_count++;
			}
Paul McCullagh's avatar
Paul McCullagh committed
2484
			else if (max_data_size == 0xFFFFFFFF) {
Paul McCullagh's avatar
Paul McCullagh committed
2485 2486 2487 2488 2489 2490 2491 2492
				/* Scale the estimated size of the blob depending on how many BLOBs
				 * are in the table!
				 */
				if (large_blob_field_count < 10)
					max_ave_row_size = mx_blob_field_size_total[large_blob_field_count];
				else
					max_ave_row_size = 200;
				large_blob_field_count++;
Paul McCullagh's avatar
Paul McCullagh committed
2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509
				if ((u_int) curr_field->field_index+1 > dic->dic_blob_cols_req)
					dic->dic_blob_cols_req = curr_field->field_index+1;
				dic->dic_blob_count++;
				xt_realloc(self, (void **) &dic->dic_blob_cols, sizeof(Field *) * dic->dic_blob_count);
				dic->dic_blob_cols[dic->dic_blob_count-1] = curr_field;
			}
		}
		else if (tno == MYSQL_TYPE_VARCHAR
#ifndef DRIZZLED
			|| tno == MYSQL_TYPE_VAR_STRING
#endif
			) {
			/* GOTCHA: MYSQL_TYPE_VAR_STRING does not exist as MYSQL_TYPE_VARCHAR define, but
			 * is used when creating a table with
			 * VARCHAR()
			 */
			min_data_size = 0;
Paul McCullagh's avatar
Paul McCullagh committed
2510 2511 2512 2513 2514
			if (varchar_field_count < 10)
				min_ave_row_size = mxvarchar_field_min_ave[varchar_field_count];
			else
				min_ave_row_size = 40;
			varchar_field_count++;
Paul McCullagh's avatar
Paul McCullagh committed
2515 2516 2517 2518 2519 2520 2521 2522
		}

 		if (max_data_size == min_data_size)
 			ave_data_size = max_data_size;
 		else {
 			var_field_count++;
			/* Take the average a 25% of the maximum: */
 			ave_data_size = max_data_size / 4;
Paul McCullagh's avatar
Paul McCullagh committed
2523 2524 2525 2526

			/* Set the average based on min and max parameters: */
 			if (ave_data_size < min_ave_row_size)
 				ave_data_size = min_ave_row_size;
Paul McCullagh's avatar
Paul McCullagh committed
2527 2528
 			else if (ave_data_size > max_ave_row_size)
 				ave_data_size = max_ave_row_size;
Paul McCullagh's avatar
Paul McCullagh committed
2529

Paul McCullagh's avatar
Paul McCullagh committed
2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571
 			if (ave_data_size > max_data_size)
 				ave_data_size = max_data_size;
		}

		/* Add space for the length indicators: */
		if (min_data_size <= 240)
			min_row_size += 1 + min_data_size;
		else if (min_data_size <= 0xFFFF)
			min_row_size += 3 + min_data_size;
		else if (min_data_size <= 0xFFFFFF)
			min_row_size += 4 + min_data_size;
		else
			min_row_size += 5 + min_data_size;

		if (max_data_size <= 240)
			max_row_size += 1 + max_data_size;
		else if (max_data_size <= 0xFFFF)
			max_row_size += 3 + max_data_size;
		else if (max_data_size <= 0xFFFFFF)
			max_row_size += 4 + max_data_size;
		else
			max_row_size += 5 + max_data_size;

		if (ave_data_size <= 240)
			ave_row_size += 1 + ave_data_size;
		else /* Should not be more than this! */
			ave_row_size += 3 + ave_data_size;

		/* This is the length of the record required for all indexes: */
		if (field_count + 1 == dic->dic_ind_cols_req)
			dic->dic_ind_rec_len = max_data_size;
 	}

	dic->dic_min_row_size = min_row_size;
	dic->dic_max_row_size = max_row_size;
	dic->dic_ave_row_size = ave_row_size;
	dic->dic_no_of_cols = field_count;

	if (dic->dic_def_ave_row_size) {
		/* The average row size has been set: */
		dic_rec_size = offsetof(XTTabRecFix, rf_data) + TS(my_tab)->reclength;

Paul McCullagh's avatar
Paul McCullagh committed
2572
		/* The conditions for a fixed record are: */
Paul McCullagh's avatar
Paul McCullagh committed
2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600
		if (dic->dic_def_ave_row_size >= (xtWord8) TS(my_tab)->reclength &&
			dic_rec_size <= XT_TAB_MAX_FIX_REC_LENGTH &&
			!blob_field_count) {
			dic_rec_fixed = TRUE;
		}
		else {
			xtWord8 new_rec_size;

			dic_rec_fixed = FALSE;
			if (dic->dic_def_ave_row_size > max_row_size)
				new_rec_size = offsetof(XTTabRecFix, rf_data) + max_row_size;
			else
				new_rec_size = offsetof(XTTabRecFix, rf_data) + dic->dic_def_ave_row_size;

			/* The maximum record size 64K for explicit AVG_ROW_LENGTH! */
			if (new_rec_size > XT_TAB_MAX_FIX_REC_LENGTH_SPEC)
				new_rec_size = XT_TAB_MAX_FIX_REC_LENGTH_SPEC;

			dic_rec_size = (u_int) new_rec_size;
		}
	}
	else {
		/* If the average size is within 10% if of the maximum size, then we
		 * we handle these rows as fixed size rows.
		 * Fixed size rows use the internal MySQL format.
		 */
		dic_rec_size = offsetof(XTTabRecFix, rf_data) + TS(my_tab)->reclength;
		/* Fixed length records must be less than 16K in size,
Paul McCullagh's avatar
Paul McCullagh committed
2601
		 * have an average size which is very close (20%) to the maximum size or
Paul McCullagh's avatar
Paul McCullagh committed
2602 2603 2604 2605
		 * be less than a minimum size,
		 * and not contain any BLOBs:
		 */
		if (dic_rec_size <= XT_TAB_MAX_FIX_REC_LENGTH &&
Paul McCullagh's avatar
Paul McCullagh committed
2606
			(ave_row_size + ave_row_size / 4 >= max_row_size ||
Paul McCullagh's avatar
Paul McCullagh committed
2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700
			dic_rec_size < XT_TAB_MIN_VAR_REC_LENGTH) &&
			!blob_field_count) {
			dic_rec_fixed = TRUE;
		}
		else {
			dic_rec_fixed = FALSE;
			/* Note I add offsetof(XTTabRecFix, rf_data) insteard of
			 * offsetof(XTTabRecExt, re_data) here!
			 * The reason is that, we want to include the average size
			 * record in the fixed data part. To do this we only need to
			 * calculate a fixed header size, because in the cases in which
			 * it fits, we will only be using a fixed header!
			 */
			dic_rec_size = (u_int) (offsetof(XTTabRecFix, rf_data) + ave_row_size);
			/* The maximum record size (16K for autorow sizing)! */
			if (dic_rec_size > XT_TAB_MAX_FIX_REC_LENGTH)
				dic_rec_size = XT_TAB_MAX_FIX_REC_LENGTH;
		}
	}

	if (!dic->dic_rec_size) {
		dic->dic_rec_size = dic_rec_size;
		dic->dic_rec_fixed = dic_rec_fixed;
	}
	else {
		/* This just confirms that our original calculation on
		 * create table agrees with the current calculation.
		 * (i.e. if non-zero values were loaded from the table).
		 *
		 * It may be the criteria for calculating the data record size
		 * and whether to used a fixed or variable record has changed,
		 * but we need to stick to the current physical layout of the
		 * table.
		 *
		 * Note that this can occur in rename table when the
		 * method of calculation has changed.
		 *
		 * On rename, the format of the table does not change, so we
		 * will not take the calculated values.
		 */
		//ASSERT(dic->dic_rec_size == dic_rec_size);
		//ASSERT(dic->dic_rec_fixed == dic_rec_fixed);
	}

	if (dic_rec_fixed) {
		/* Recalculate the length of the required required to address all
		 * index columns!
		 */		 
		if (field_count == dic->dic_ind_cols_req)
			dic->dic_ind_rec_len = TS(my_tab)->reclength;
		else {
			field=my_tab->field;
			
			curr_field = field[dic->dic_ind_cols_req];
#if MYSQL_VERSION_ID < 50114
			dic->dic_ind_rec_len = curr_field->offset();
#else
			dic->dic_ind_rec_len = curr_field->offset(curr_field->table->record[0]);
#endif
		}
	}

	/* We now calculate how many of the first columns in the row
	 * will definitely fit into the buffer, when the record is
	 * of type extended.
	 *
	 * In this way we can figure out if we need to load the extended
	 * record at all.
	 */
	dic->dic_fix_col_count = 0;
	if (!dic_rec_fixed) {
		xtWord8 max_rec_size = offsetof(XTTabRecExt, re_data);

		for (Field **f=my_tab->field; (curr_field = *f); f++) {
			max_data_size = curr_field->key_length();
			enum_field_types tno = curr_field->type();
			if (tno == MYSQL_TYPE_BLOB)
				max_data_size = ((Field_blob *) curr_field)->max_data_length();
			if (max_data_size <= 240)
				max_rec_size += 1 + max_data_size;
			else if (max_data_size <= 0xFFFF)
				max_rec_size += 3 + max_data_size;
			else if (max_data_size <= 0xFFFFFF)
				max_rec_size += 4 + max_data_size;
			else
				max_rec_size += 5 + max_data_size;
			if (max_rec_size > (xtWord8) dic_rec_size)
				break;
			dic->dic_fix_col_count++;
		}		
		ASSERT(dic->dic_fix_col_count < dic->dic_no_of_cols);
	}

 	dic->dic_key_count = TS(my_tab)->keys;
Paul McCullagh's avatar
Paul McCullagh committed
2701 2702
	dic->dic_mysql_buf_size = TS(my_tab)->rec_buff_length;
	dic->dic_mysql_rec_size = TS(my_tab)->reclength;
Paul McCullagh's avatar
Paul McCullagh committed
2703 2704
}

2705
static u_int my_get_best_superset(XTThreadPtr XT_UNUSED(self), XTDictionaryPtr dic, XTIndexPtr ind)
Paul McCullagh's avatar
Paul McCullagh committed
2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736
{
	XTIndexPtr	super_ind;
	u_int		super = 0;
	u_int		super_seg_count = ind->mi_seg_count;

	for (u_int i=0; i<dic->dic_key_count; i++) {
		super_ind = dic->dic_keys[i];
		if (ind->mi_index_no != super_ind->mi_index_no &&
			super_seg_count < super_ind->mi_seg_count) {
			for (u_int j=0; j<ind->mi_seg_count; j++) {
				if (ind->mi_seg[j].col_idx != super_ind->mi_seg[j].col_idx)
					goto next;
			}
			super_seg_count = super_ind->mi_seg_count;
			super = i+1;
			next:;
		}
	}
	return super;
}

/*
 * Return FAILED if the MySQL dictionary is not available.
 */
xtPublic xtBool myxt_load_dictionary(XTThreadPtr self, XTDictionaryPtr dic, XTDatabaseHPtr db, XTPathStrPtr tab_path)
{
	TABLE *my_tab;

	if (!(my_tab = my_open_table(self, db, tab_path)))
		return FAILED;
	dic->dic_my_table = my_tab;
2737 2738 2739
#ifdef DRIZZLED
	dic->dic_def_ave_row_size = (xtWord8) my_tab->s->getAvgRowLength();
#else
Paul McCullagh's avatar
Paul McCullagh committed
2740
	dic->dic_def_ave_row_size = (xtWord8) my_tab->s->avg_row_length;
2741
#endif
Paul McCullagh's avatar
Paul McCullagh committed
2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812
	myxt_setup_dictionary(self, dic);
	dic->dic_keys = (XTIndexPtr *) xt_calloc(self, sizeof(XTIndexPtr) * TS(my_tab)->keys);
	for (uint i=0; i<TS(my_tab)->keys; i++)
		dic->dic_keys[i] = my_create_index(self, my_tab, i, &my_tab->key_info[i]);

	/* Check if any key is a subset of another: */
	for (u_int i=0; i<dic->dic_key_count; i++)
		dic->dic_keys[i]->mi_subset_of = my_get_best_superset(self, dic, dic->dic_keys[i]);

	return OK;
}

xtPublic void myxt_free_dictionary(XTThreadPtr self, XTDictionaryPtr dic)
{
	if (dic->dic_table) {
		dic->dic_table->release(self);
		dic->dic_table = NULL;
	}

	if (dic->dic_my_table) {
		my_close_table(dic->dic_my_table);
		dic->dic_my_table = NULL;
	}

	if (dic->dic_blob_cols) {
		xt_free(self, dic->dic_blob_cols);
		dic->dic_blob_cols = NULL;
	}
	dic->dic_blob_count = 0;

	/* If we have opened a table, then this data is freed with the dictionary: */
	if (dic->dic_keys) {
		for (uint i=0; i<dic->dic_key_count; i++) {
			if (dic->dic_keys[i])
				my_deref_index_data(self, (XTIndexPtr) dic->dic_keys[i]);
		}
		xt_free(self, dic->dic_keys);
		dic->dic_key_count = 0;
		dic->dic_keys = NULL;
	}
}

xtPublic void myxt_move_dictionary(XTDictionaryPtr dic, XTDictionaryPtr source_dic)
{
	dic->dic_my_table = source_dic->dic_my_table;
	source_dic->dic_my_table = NULL;

	if (!dic->dic_rec_size) {
		dic->dic_rec_size = source_dic->dic_rec_size;
		dic->dic_rec_fixed = source_dic->dic_rec_fixed;
	}
	else {
		/* This just confirms that our original calculation on
		 * create table agrees with the current calculation.
		 * (i.e. if non-zero values were loaded from the table).
		 *
		 * It may be the criteria for calculating the data record size
		 * and whether to used a fixed or variable record has changed,
		 * but we need to stick to the current physical layout of the
		 * table.
		 */
		ASSERT_NS(dic->dic_rec_size == source_dic->dic_rec_size);
		ASSERT_NS(dic->dic_rec_fixed == source_dic->dic_rec_fixed);
	}

	dic->dic_tab_flags = source_dic->dic_tab_flags;
	dic->dic_blob_cols_req = source_dic->dic_blob_cols_req;
	dic->dic_blob_count = source_dic->dic_blob_count;
	dic->dic_blob_cols = source_dic->dic_blob_cols;
	source_dic->dic_blob_cols = NULL;

Paul McCullagh's avatar
Paul McCullagh committed
2813 2814
	dic->dic_mysql_buf_size = source_dic->dic_mysql_buf_size;
	dic->dic_mysql_rec_size = source_dic->dic_mysql_rec_size;
Paul McCullagh's avatar
Paul McCullagh committed
2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868
 	dic->dic_key_count = source_dic->dic_key_count;
	dic->dic_keys = source_dic->dic_keys;

	/* Set this to zero, bcause later xt_flush_tables() may be called. 
	 * This can occur when using the BLOB streaming engine,
	 * in command ALTER TABLE x ENGINE = PBXT;
	 */
	source_dic->dic_key_count = 0;
	source_dic->dic_keys = NULL;

	dic->dic_min_row_size = source_dic->dic_min_row_size;
	dic->dic_max_row_size = source_dic->dic_max_row_size;
	dic->dic_ave_row_size = source_dic->dic_ave_row_size;
	dic->dic_def_ave_row_size = source_dic->dic_def_ave_row_size;

	dic->dic_no_of_cols = source_dic->dic_no_of_cols;
 	dic->dic_fix_col_count = source_dic->dic_fix_col_count;
 	dic->dic_ind_cols_req = source_dic->dic_ind_cols_req;
 	dic->dic_ind_rec_len = source_dic->dic_ind_rec_len;
}

static void my_free_dd_table(XTThreadPtr self, XTDDTable *dd_tab)
{
	if (dd_tab)
		dd_tab->release(self);
}

static void ha_create_dd_index(XTThreadPtr self, XTDDIndex *ind, KEY *key)
{
	KEY_PART_INFO	*key_part;
	KEY_PART_INFO	*key_part_end;
	XTDDColumnRef	*cref;

	if (strcmp(key->name, "PRIMARY") == 0)
		ind->co_type = XT_DD_KEY_PRIMARY;
	else if (key->flags & HA_NOSAME)
		ind->co_type = XT_DD_INDEX_UNIQUE;
	else
		ind->co_type = XT_DD_INDEX;

	if (ind->co_type == XT_DD_KEY_PRIMARY)
		ind->co_name = xt_dup_string(self, key->name);
	else
		ind->co_ind_name = xt_dup_string(self, key->name);

	key_part_end = key->key_part + key->key_parts;
	for (key_part = key->key_part; key_part != key_part_end; key_part++) {
		if (!(cref = new XTDDColumnRef()))
			xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
		ind->co_cols.append(self, cref);
		cref->cr_col_name = xt_dup_string(self, (char *) key_part->field->field_name);
	}
}

2869
static char *my_type_to_string(XTThreadPtr self, Field *field, TABLE *XT_UNUSED(my_tab))
Paul McCullagh's avatar
Paul McCullagh committed
2870
{
2871 2872
	char		buffer[MAX_FIELD_WIDTH + 400];
	const char 	*ptr;
Paul McCullagh's avatar
Paul McCullagh committed
2873
	String		type((char *) buffer, sizeof(buffer), system_charset_info);
2874
	xtWord4		len;
Paul McCullagh's avatar
Paul McCullagh committed
2875 2876 2877 2878 2879 2880 2881 2882 2883 2884

	/* GOTCHA:
	 * - Above sets the string length to the same as the buffer,
	 *   so we must set the length to zero.
	 * - The result is not necessarilly zero terminated.
	 * - We cannot assume that the input buffer is the one
	 *   we get back (for example text field).
	 */
	type.length(0);
	field->sql_type(type);
2885 2886 2887 2888 2889 2890
	ptr = type.ptr();
	len = type.length();

	if (len >= sizeof(buffer))
		len = sizeof(buffer)-1;

Paul McCullagh's avatar
Paul McCullagh committed
2891
	if (ptr != buffer)
2892
		xt_strcpy(sizeof(buffer), buffer, ptr);
Paul McCullagh's avatar
Paul McCullagh committed
2893

2894 2895
	buffer[len] = 0;
			
Paul McCullagh's avatar
Paul McCullagh committed
2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949
	if (field->has_charset()) {
		/* Always include the charset so that we can compare types
		 * for FK/PK releations.
		 */
		xt_strcat(sizeof(buffer), buffer, " CHARACTER SET ");
		xt_strcat(sizeof(buffer), buffer, (char *) field->charset()->csname);

		/* For string types dump collation name only if 
		 * collation is not primary for the given charset
		 */
		if (!(field->charset()->state & MY_CS_PRIMARY)) {
			xt_strcat(sizeof(buffer), buffer, " COLLATE ");
			xt_strcat(sizeof(buffer), buffer, (char *) field->charset()->name);
		}
	}

	return xt_dup_string(self, buffer); // type.length()
}

xtPublic XTDDTable *myxt_create_table_from_table(XTThreadPtr self, TABLE *my_tab)
{
	XTDDTable		*dd_tab;
	Field			*curr_field;
	XTDDColumn		*col;
	XTDDIndex		*ind;

	if (!(dd_tab = new XTDDTable()))
		xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
	dd_tab->init(self);
	pushr_(my_free_dd_table, dd_tab);

 	for (Field **field=my_tab->field; (curr_field = *field); field++) {
		col = XTDDColumnFactory::createFromMySQLField(self, my_tab, curr_field);
		dd_tab->dt_cols.append(self, col);
	}

	for (uint i=0; i<TS(my_tab)->keys; i++) {
		if (!(ind = (XTDDIndex *) new XTDDIndex(XT_DD_UNKNOWN)))
			xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
		dd_tab->dt_indexes.append(self, ind);
		ind->co_table = dd_tab;
		ind->in_index = i;
		ha_create_dd_index(self, ind, &my_tab->key_info[i]);
	}

	popr_(); // my_free_dd_table(dd_tab)
	return dd_tab;
}

/*
 * -----------------------------------------------------------------------
 * MySQL CHARACTER UTILITIES
 */

2950
xtPublic void myxt_static_convert_identifier(XTThreadPtr XT_UNUSED(self), MX_CHARSET_INFO *cs, char *from, char *to, size_t to_len)
Paul McCullagh's avatar
Paul McCullagh committed
2951
{
2952 2953 2954 2955
#ifdef DRIZZLED
	((void *)cs);
	 xt_strcpy(to_len, to, from);
#else
Paul McCullagh's avatar
Paul McCullagh committed
2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966
	uint errors;

	/*
	 * Bug#4417
	 * Check that identifiers and strings are not converted 
	 * when the client character set is binary.
	 */
	if (cs == &my_charset_utf8_general_ci || cs == &my_charset_bin)
		xt_strcpy(to_len, to, from);
	else
		strconvert(cs, from, &my_charset_utf8_general_ci, to, to_len, &errors);
2967
#endif
Paul McCullagh's avatar
Paul McCullagh committed
2968 2969 2970 2971 2972
}

// cs == current_thd->charset()
xtPublic char *myxt_convert_identifier(XTThreadPtr self, MX_CHARSET_INFO *cs, char *from)
{
2973 2974 2975 2976
#ifdef DRIZZLED
	char *to = xt_dup_string(self, from);
	((void *)cs);
#else
Paul McCullagh's avatar
Paul McCullagh committed
2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987
	uint	errors;
	u_int	len;
	char	*to;

	if (cs == &my_charset_utf8_general_ci || cs == &my_charset_bin)
		to = xt_dup_string(self, from);
	else {
		len = strlen(from) * 3 + 1;
		to = (char *) xt_malloc(self, len);
		strconvert(cs, from, &my_charset_utf8_general_ci, to, len, &errors);
	}
2988
#endif
Paul McCullagh's avatar
Paul McCullagh committed
2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002
	return to;
}

xtPublic char *myxt_convert_table_name(XTThreadPtr self, char *from)
{
	u_int	len;
	char	*to;

	len = strlen(from) * 5 + 1;
	to = (char *) xt_malloc(self, len);
	tablename_to_filename(from, to, len);
	return to;
}

3003
xtPublic void myxt_static_convert_table_name(XTThreadPtr XT_UNUSED(self), char *from, char *to, size_t to_len)
Paul McCullagh's avatar
Paul McCullagh committed
3004 3005 3006 3007
{
	tablename_to_filename(from, to, to_len);
}

3008 3009 3010 3011 3012
xtPublic void myxt_static_convert_file_name(char *from, char *to, size_t to_len)
{
	filename_to_tablename(from, to, to_len);
}

Paul McCullagh's avatar
Paul McCullagh committed
3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038
xtPublic int myxt_strcasecmp(char * a, char *b)
{
	return my_strcasecmp(&my_charset_utf8_general_ci, a, b);
}

xtPublic int myxt_isspace(MX_CHARSET_INFO *cs, char a)
{
	return my_isspace(cs, a);
}

xtPublic int myxt_ispunct(MX_CHARSET_INFO *cs, char a)
{
	return my_ispunct(cs, a);
}

xtPublic int myxt_isdigit(MX_CHARSET_INFO *cs, char a)
{
	return my_isdigit(cs, a);
}

xtPublic MX_CHARSET_INFO *myxt_getcharset(bool convert)
{
	if (convert) {
		THD *thd = current_thd;

		if (thd)
3039
			return (MX_CHARSET_INFO *)thd_charset(thd);
Paul McCullagh's avatar
Paul McCullagh committed
3040
	}
3041
	return (MX_CHARSET_INFO *)&my_charset_utf8_general_ci;
Paul McCullagh's avatar
Paul McCullagh committed
3042 3043 3044 3045
}

xtPublic void *myxt_create_thread()
{
3046 3047 3048
#ifdef DRIZZLED
	return (void *) 1;
#else
Paul McCullagh's avatar
Paul McCullagh committed
3049 3050 3051 3052 3053 3054 3055
	THD *new_thd;

	if (my_thread_init()) {
		xt_register_error(XT_REG_CONTEXT, XT_ERR_MYSQL_ERROR, 0, "Unable to initialize MySQL threading");
		return NULL;
	}

3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095
	/*
	 * Unfortunately, if PBXT is the default engine, and we are shutting down
	 * then global_system_variables.table_plugin may be NULL. Which will cause
	 * a crash if we try to create a thread!
	 *
	 * The following call in plugin_shutdown() sets the global reference
	 * to NULL:
	 *
	 * unlock_variables(NULL, &global_system_variables);
	 *
	 * Later plugin_deinitialize() is called.
	 *
	 * The following stack is an example crash which occurs when I call
	 * myxt_create_thread() in ha_exit(), to force the error.
	 *
	 *   if (pi->state & (PLUGIN_IS_READY | PLUGIN_IS_UNINITIALIZED))
	 *   pi is NULL!
	 * #0	0x002ff684 in intern_plugin_lock at sql_plugin.cc:617
	 * #1	0x0030296d in plugin_thdvar_init at sql_plugin.cc:2432
	 * #2	0x000db4a4 in THD::init at sql_class.cc:756
	 * #3	0x000e02ed in THD::THD at sql_class.cc:638
	 * #4	0x00e2678d in myxt_create_thread at myxt_xt.cc:2990
	 * #5	0x00e05d43 in ha_exit at ha_pbxt.cc:1011
	 * #6	0x00e065c2 in pbxt_end at ha_pbxt.cc:1330
	 * #7	0x00e065df in pbxt_panic at ha_pbxt.cc:1343
	 * #8	0x0023e57d in ha_finalize_handlerton at handler.cc:392
	 * #9	0x002ffc8b in plugin_deinitialize at sql_plugin.cc:816
	 * #10	0x003037d9 in plugin_shutdown at sql_plugin.cc:1572
	 * #11	0x000f7b2b in clean_up at mysqld.cc:1266
	 * #12	0x000f7fca in unireg_end at mysqld.cc:1192
	 * #13	0x000fa021 in kill_server at mysqld.cc:1134
	 * #14	0x000fa6df in kill_server_thread at mysqld.cc:1155
	 * #15	0x91fdb155 in _pthread_start
	 * #16	0x91fdb012 in thread_start
	 */
	if (!global_system_variables.table_plugin) {
		xt_register_xterr(XT_REG_CONTEXT, XT_ERR_MYSQL_NO_THREAD);
		return NULL;
	}

Paul McCullagh's avatar
Paul McCullagh committed
3096
	if (!(new_thd = new THD)) {
Paul McCullagh's avatar
Paul McCullagh committed
3097 3098 3099 3100 3101
		my_thread_end();
		xt_register_error(XT_REG_CONTEXT, XT_ERR_MYSQL_ERROR, 0, "Unable to create MySQL thread (THD)");
		return NULL;
	}

Paul McCullagh's avatar
Paul McCullagh committed
3102 3103
	/*
	 * If PBXT is the default storage engine, then creating any THD objects will add extra 
3104 3105 3106 3107 3108 3109 3110 3111
	 * references to the PBXT plugin object. because the threads are created but PBXT
	 * this creates a self reference, and the reference count does not go to zero
	 * on shutdown.
	 *
	 * The server then issues a message that it is forcing shutdown of the plugin.
	 *
	 * However, the engine reference is not required by the THDs used by PBXT, so 
	 * I just remove them here.
Paul McCullagh's avatar
Paul McCullagh committed
3112
	 */
3113 3114 3115
	plugin_unlock(NULL, new_thd->variables.table_plugin);
	new_thd->variables.table_plugin = NULL;

Paul McCullagh's avatar
Paul McCullagh committed
3116 3117 3118 3119 3120
	new_thd->thread_stack = (char *) &new_thd;
	new_thd->store_globals();
	lex_start(new_thd);

	return (void *) new_thd;
3121
#endif
Paul McCullagh's avatar
Paul McCullagh committed
3122 3123
}

3124 3125 3126 3127
#ifdef DRIZZLED
xtPublic void myxt_destroy_thread(void *, xtBool)
{
}
3128 3129 3130 3131

xtPublic void myxt_delete_remaining_thread()
{
}
3132
#else
Paul McCullagh's avatar
Paul McCullagh committed
3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149
xtPublic void myxt_destroy_thread(void *thread, xtBool end_threads)
{
	THD *thd = (THD *) thread;

#if MYSQL_VERSION_ID > 60005
	/* PMC - This is a HACK! It is required because
	 * MySQL shuts down MDL before shutting down the
	 * plug-ins.
	 */
	if (!pbxt_inited)
		mdl_init();
	close_thread_tables(thd);
	if (!pbxt_inited)
		mdl_destroy();
#else
	close_thread_tables(thd);
#endif
Paul McCullagh's avatar
Paul McCullagh committed
3150
	
Paul McCullagh's avatar
Paul McCullagh committed
3151 3152 3153 3154 3155 3156 3157 3158
	delete thd;

	/* Remember that we don't have a THD */
	my_pthread_setspecific_ptr(THR_THD, 0);

	if (end_threads)
		my_thread_end();
}
3159 3160 3161 3162 3163 3164 3165 3166

xtPublic void myxt_delete_remaining_thread()
{
	THD *thd;

	if ((thd = current_thd))
		myxt_destroy_thread((void *) thd, TRUE);
}
3167
#endif
Paul McCullagh's avatar
Paul McCullagh committed
3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226

xtPublic XTThreadPtr myxt_get_self()
{
	THD *thd;
	
	if ((thd = current_thd))
		return xt_ha_thd_to_self(thd);
	return NULL;
}

/*
 * -----------------------------------------------------------------------
 * INFORMATION SCHEMA FUNCTIONS
 *
 */

static int mx_put_record(THD *thd, TABLE *table)
{
	return schema_table_store_record(thd, table);
}

#ifdef UNUSED_CODE
static void mx_put_int(TABLE *table, int column, int value)
{
	table->field[column]->store(value, false);
}

static void mx_put_real8(TABLE *table, int column, xtReal8 value)
{
	table->field[column]->store(value);
}

static void mx_put_string(TABLE *table, int column, const char *string, u_int len, charset_info_st *charset)
{
	table->field[column]->store(string, len, charset);
}
#endif

static void mx_put_u_llong(TABLE *table, int column, u_llong value)
{
	table->field[column]->store(value, false);
}

static void mx_put_string(TABLE *table, int column, const char *string, charset_info_st *charset)
{
	table->field[column]->store(string, strlen(string), charset);
}

xtPublic int myxt_statistics_fill_table(XTThreadPtr self, void *th, void *ta, void *, MX_CONST void *ch)
{
	THD				*thd = (THD *) th;
	TABLE_LIST		*tables = (TABLE_LIST *) ta;
	charset_info_st	*charset = (charset_info_st *) ch;
	TABLE			*table = (TABLE *) tables->table;
	int				err = 0;
	int				col;
	const char		*stat_name;
	u_llong			stat_value;
	XTStatisticsRec	statistics;
3227
	XTDatabaseHPtr	db = self->st_database;
Paul McCullagh's avatar
Paul McCullagh committed
3228 3229 3230 3231

	xt_gather_statistics(&statistics);
	for (u_int rec_id=0; !err && rec_id<XT_STAT_CURRENT_MAX; rec_id++) {
		stat_name = xt_get_stat_meta_data(rec_id)->sm_name;
3232
		stat_value = xt_get_statistic(&statistics, db, rec_id);
Paul McCullagh's avatar
Paul McCullagh committed
3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307

		col=0;
		mx_put_u_llong(table, col++, rec_id+1);
		mx_put_string(table, col++, stat_name, charset);
		mx_put_u_llong(table, col++, stat_value);
		err = mx_put_record(thd, table);
	}

	return err;
}

xtPublic void myxt_get_status(XTThreadPtr self, XTStringBufferPtr strbuf)
{
	char string[200];

	xt_sb_concat(self, strbuf, "\n");
	xt_get_now(string, 200);
	xt_sb_concat(self, strbuf, string);
	xt_sb_concat(self, strbuf, " PBXT ");
	xt_sb_concat(self, strbuf, xt_get_version());
	xt_sb_concat(self, strbuf, " STATUS OUTPUT");
	xt_sb_concat(self, strbuf, "\n");

	xt_sb_concat(self, strbuf, "Record cache usage: ");
	xt_sb_concat_int8(self, strbuf, xt_tc_get_usage());
	xt_sb_concat(self, strbuf, "\n");
	xt_sb_concat(self, strbuf, "Record cache size:  ");
	xt_sb_concat_int8(self, strbuf, xt_tc_get_size());
	xt_sb_concat(self, strbuf, "\n");
	xt_sb_concat(self, strbuf, "Record cache high:  ");
	xt_sb_concat_int8(self, strbuf, xt_tc_get_high());
	xt_sb_concat(self, strbuf, "\n");
	xt_sb_concat(self, strbuf, "Index cache usage:  ");
	xt_sb_concat_int8(self, strbuf, xt_ind_get_usage());
	xt_sb_concat(self, strbuf, "\n");
	xt_sb_concat(self, strbuf, "Index cache size:   ");
	xt_sb_concat_int8(self, strbuf, xt_ind_get_size());
	xt_sb_concat(self, strbuf, "\n");
	xt_sb_concat(self, strbuf, "Log cache usage:    ");
	xt_sb_concat_int8(self, strbuf, xt_xlog_get_usage());
	xt_sb_concat(self, strbuf, "\n");
	xt_sb_concat(self, strbuf, "Log cache size:     ");
	xt_sb_concat_int8(self, strbuf, xt_xlog_get_size());
	xt_sb_concat(self, strbuf, "\n");

	xt_ht_lock(self, xt_db_open_databases);
	pushr_(xt_ht_unlock, xt_db_open_databases);

	XTDatabaseHPtr	*dbptr;
	size_t len = xt_sl_get_size(xt_db_open_db_by_id);

	if (len > 0) {
		xt_sb_concat(self, strbuf, "Data log files:\n");
		for (u_int i=0; i<len; i++) {
			dbptr = (XTDatabaseHPtr *) xt_sl_item_at(xt_db_open_db_by_id, i);
			
#ifndef XT_USE_GLOBAL_DB
			xt_sb_concat(self, strbuf, "Database: ");
			xt_sb_concat(self, strbuf, (*dbptr)->db_name);
			xt_sb_concat(self, strbuf, "\n");
#endif
			xt_dl_log_status(self, *dbptr, strbuf);
		}
	}
	else
		xt_sb_concat(self, strbuf, "No data logs in use\n");

	freer_(); // xt_ht_unlock(xt_db_open_databases)
}

/*
 * -----------------------------------------------------------------------
 * MySQL Bit Maps
 */

3308
static void myxt_bitmap_init(XTThreadPtr self, MX_BITMAP *map, u_int n_bits)
Paul McCullagh's avatar
Paul McCullagh committed
3309 3310 3311 3312
{
	my_bitmap_map	*buf;
    uint			size_in_bytes = (((n_bits) + 31) / 32) * 4;

3313 3314 3315 3316 3317
	buf = (my_bitmap_map *) xt_malloc(self, size_in_bytes);

#ifdef DRIZZLED
	map->init(buf, n_bits);
#else
Paul McCullagh's avatar
Paul McCullagh committed
3318 3319 3320 3321
	map->bitmap= buf;
	map->n_bits= n_bits;
	create_last_word_mask(map);
	bitmap_clear_all(map);
3322
#endif
Paul McCullagh's avatar
Paul McCullagh committed
3323 3324
}

3325
static void myxt_bitmap_free(XTThreadPtr self, MX_BITMAP *map)
Paul McCullagh's avatar
Paul McCullagh committed
3326
{
3327 3328 3329 3330 3331 3332
#ifdef DRIZZLED
	my_bitmap_map *buf = map->getBitmap();
	if (buf)
		xt_free(self, buf);
	map->setBitmap(NULL);
#else
Paul McCullagh's avatar
Paul McCullagh committed
3333 3334 3335 3336
	if (map->bitmap) {
		xt_free(self, map->bitmap);
		map->bitmap = NULL;
	}
3337
#endif
Paul McCullagh's avatar
Paul McCullagh committed
3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380
}

/*
 * -----------------------------------------------------------------------
 * XTDDColumnFactory methods
 */

XTDDColumn *XTDDColumnFactory::createFromMySQLField(XTThread *self, TABLE *my_tab, Field *field)
{
	XTDDEnumerableColumn *en_col;
	XTDDColumn *col;
	xtBool is_enum = FALSE;

	switch(field->real_type()) {
		case MYSQL_TYPE_ENUM:
			is_enum = TRUE;
			/* fallthrough */

#ifndef DRIZZLED
		case MYSQL_TYPE_SET:
#endif
			col = en_col = new XTDDEnumerableColumn();
		    if (!col)
				xt_throw_errno(XT_CONTEXT, XT_ENOMEM); 
			col->init(self);
			en_col->enum_size = ((Field_enum *)field)->typelib->count;
			en_col->is_enum = is_enum;
			break;

		default:
			col = new XTDDColumn();
			if (!col)
				xt_throw_errno(XT_CONTEXT, XT_ENOMEM); 
			col->init(self);
	}

	col->dc_name = xt_dup_string(self, (char *) field->field_name);
	col->dc_data_type = my_type_to_string(self, field, my_tab);
	col->dc_null_ok = field->null_ptr != NULL;

	return col;
}

Paul McCullagh's avatar
Paul McCullagh committed
3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406
/*
 * -----------------------------------------------------------------------
 * utilities
 */

/*
 * MySQL (not sure about Drizzle) first calls hton->init and then assigns the plugin a thread slot
 * which is used by xt_get_self(). This is a problem as pbxt_init() starts a number of daemon threads
 * which could try to use the slot before it is assigned. This code waits till slot is inited.
 * We cannot directly check hton->slot as in some versions of MySQL it can be 0 before init which is a 
 * valid value.
 */
extern ulong total_ha;

xtPublic void myxt_wait_pbxt_plugin_slot_assigned(XTThread *self)
{
#ifdef DRIZZLED
	static LEX_STRING plugin_name = { C_STRING_WITH_LEN("PBXT") };

	while (!self->t_quit && !Registry::singleton().find(&plugin_name))
		xt_sleep_milli_second(1);
#else
	while(!self->t_quit && (pbxt_hton->slot >= total_ha))
		xt_sleep_milli_second(1);
#endif
}