~drizzle-trunk/drizzle/development

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
146
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
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
787
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
823
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
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
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
965
966
967
968
969
970
971
972
973
974
975
976
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
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
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
/*****************************************************************************

Copyright (c) 2007, 2009, Innobase Oy. All Rights Reserved.

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; version 2 of the License.

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

*****************************************************************************/

/**************************************************//**
@file handler/i_s.cc
InnoDB INFORMATION SCHEMA tables interface to MySQL.

Created July 18, 2007 Vasil Dimov
*******************************************************/

#include "config.h"
#include <drizzled/error.h>
#include "drizzled/charset_info.h"
#include "drizzled/internal/my_sys.h"
#include <drizzled/my_hash.h>
#include <drizzled/plugin.h>
#include <drizzled/field.h>
#include <drizzled/table.h>
#include <drizzled/plugin/info_schema_table.h>
#include <drizzled/time_functions.h>
#include "drizzled/global_charset_info.h"


#include "i_s.h"


extern "C" {
#include "trx0i_s.h"
#include "trx0trx.h" /* for TRX_QUE_STATE_STR_MAX_LEN */
#include "buf0buddy.h" /* for i_s_cmpmem */
#include "buf0buf.h" /* for buf_pool and PAGE_ZIP_MIN_SIZE */
#include "ha_prototypes.h" /* for innobase_convert_name() */
#include "srv0start.h" /* for srv_was_started */
}
#include "handler0vars.h"

using namespace drizzled;

static const char plugin_author[] = "Innobase Oy";

#define OK(expr)		\
	if ((expr) != 0) {	\
		return(1);	\
	}

#define RETURN_IF_INNODB_NOT_STARTED(plugin_name)			\
do {									\
	if (!srv_was_started) {						\
		push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,	\
				    ER_CANT_FIND_SYSTEM_REC,		\
				    "InnoDB: SELECTing from "		\
				    "INFORMATION_SCHEMA.%s but "	\
				    "the InnoDB storage engine "	\
				    "is not installed", plugin_name);	\
		return(0);						\
	}								\
} while (0)

#define STRUCT_FLD(name, value)	value

plugin::InfoSchemaTable *innodb_trx_schema_table= NULL;
plugin::InfoSchemaTable *innodb_locks_schema_table= NULL;
plugin::InfoSchemaTable *innodb_lock_waits_schema_table= NULL;
plugin::InfoSchemaTable *innodb_cmp_schema_table= NULL;
plugin::InfoSchemaTable *innodb_cmp_reset_schema_table= NULL;
plugin::InfoSchemaTable *innodb_cmpmem_schema_table= NULL;
plugin::InfoSchemaTable *innodb_cmpmem_reset_schema_table= NULL;

static TrxISMethods trx_methods;
static CmpISMethods cmp_methods;
static CmpResetISMethods cmp_reset_methods;
static CmpmemISMethods cmpmem_methods;
static CmpmemResetISMethods cmpmem_reset_methods;

/*
Use the following types mapping:

C type	ST_FIELD_INFO::field_type
---------------------------------
long			DRIZZLE_TYPE_LONGLONG
(field_length=MY_INT64_NUM_DECIMAL_DIGITS)

long unsigned		DRIZZLE_TYPE_LONGLONG
(field_length=MY_INT64_NUM_DECIMAL_DIGITS, field_flags=MY_I_S_UNSIGNED)

char*			DRIZZLE_TYPE_STRING
(field_length=n)

float			DRIZZLE_TYPE_FLOAT
(field_length=0 is ignored)

void*			DRIZZLE_TYPE_LONGLONG
(field_length=MY_INT64_NUM_DECIMAL_DIGITS, field_flags=MY_I_S_UNSIGNED)

boolean (if else)	DRIZZLE_TYPE_LONG
(field_length=1)

time_t			DRIZZLE_TYPE_DATETIME
(field_length=0 ignored)
---------------------------------
*/

/*******************************************************************//**
Auxiliary function to store time_t value in MYSQL_TYPE_DATETIME
field.
@return	0 on success */
static
int
field_store_time_t(
/*===============*/
	Field*	field,	/*!< in/out: target field for storage */
	time_t	time)	/*!< in: value to store */
{
	DRIZZLE_TIME	my_time;
	struct tm	tm_time;

#if 0
	/* use this if you are sure that `variables' and `time_zone'
	are always initialized */
	session->variables.time_zone->gmt_sec_to_TIME(
		&my_time, (time_t) time);
#else
	localtime_r(&time, &tm_time);
	localtime_to_TIME(&my_time, &tm_time);
	my_time.time_type = DRIZZLE_TIMESTAMP_DATETIME;
#endif

	return(field->store_time(&my_time, DRIZZLE_TIMESTAMP_DATETIME));
}

/*******************************************************************//**
Auxiliary function to store char* value in MYSQL_TYPE_STRING field.
@return	0 on success */
static
int
field_store_string(
/*===============*/
	Field*		field,	/*!< in/out: target field for storage */
	const char*	str)	/*!< in: NUL-terminated utf-8 string,
				or NULL */
{
	int	ret;

	if (str != NULL) {

		ret = field->store(str, strlen(str),
				   system_charset_info);
		field->set_notnull();
	} else {

		ret = 0; /* success */
		field->set_null();
	}

	return(ret);
}

/*******************************************************************//**
Auxiliary function to store ulint value in DRIZZLE_TYPE_LONGLONG field.
If the value is ULINT_UNDEFINED then the field it set to NULL.
@return	0 on success */
static
int
field_store_ulint(
/*==============*/
	Field*	field,	/*!< in/out: target field for storage */
	ulint	n)	/*!< in: value to store */
{
	int	ret;

	if (n != ULINT_UNDEFINED) {

		ret = field->store(n);
		field->set_notnull();
	} else {

		ret = 0; /* success */
		field->set_null();
	}

	return(ret);
}

/* Fields of the dynamic table INFORMATION_SCHEMA.innodb_trx */
static plugin::ColumnInfo	innodb_trx_fields_info[] =
{
#define IDX_TRX_ID		0
        plugin::ColumnInfo("trx_id",
                  TRX_ID_MAX_LEN + 1,
                  DRIZZLE_TYPE_VARCHAR,
                  0,
                  0,
                  ""),

#define IDX_TRX_STATE		1
        plugin::ColumnInfo("trx_state",
                  TRX_QUE_STATE_STR_MAX_LEN + 1,
                  DRIZZLE_TYPE_VARCHAR,
                  0,
                  0,
                  ""),

#define IDX_TRX_STARTED		2
        plugin::ColumnInfo("trx_started",
                  0,
                  DRIZZLE_TYPE_DATETIME,
                  0,
                  0,
                  ""),

#define IDX_TRX_REQUESTED_LOCK_ID	3
        plugin::ColumnInfo("trx_requested_lock_id",
                  TRX_I_S_LOCK_ID_MAX_LEN + 1,
                  DRIZZLE_TYPE_VARCHAR,
                  0,
                  MY_I_S_MAYBE_NULL,
                  ""),

#define IDX_TRX_WAIT_STARTED	4
        plugin::ColumnInfo("trx_wait_started",
                  0,
                  DRIZZLE_TYPE_DATETIME,
                  0,
                  MY_I_S_MAYBE_NULL,
                  ""),

#define IDX_TRX_WEIGHT		5
        plugin::ColumnInfo("trx_weight",
                  MY_INT64_NUM_DECIMAL_DIGITS,
                  DRIZZLE_TYPE_LONGLONG,
                  0,
                  MY_I_S_UNSIGNED,
                  ""),

#define IDX_TRX_DRIZZLE_THREAD_ID	6
        plugin::ColumnInfo("trx_mysql_thread_id",
                  MY_INT64_NUM_DECIMAL_DIGITS,
                  DRIZZLE_TYPE_LONGLONG,
                  0,
                  MY_I_S_UNSIGNED,
                  ""),

#define IDX_TRX_QUERY		7
        plugin::ColumnInfo("trx_query",
                  TRX_I_S_TRX_QUERY_MAX_LEN,
                  DRIZZLE_TYPE_VARCHAR,
                  0,
                  MY_I_S_MAYBE_NULL,
                  ""),

        plugin::ColumnInfo()
};

/*******************************************************************//**
Read data from cache buffer and fill the INFORMATION_SCHEMA.innodb_trx
table with it.
@return	0 on success */
static
int
fill_innodb_trx_from_cache(
/*=======================*/
	trx_i_s_cache_t*	cache,	/*!< in: cache to read from */
	Table*			table,	/*!< in/out: fill this table */
        plugin::InfoSchemaTable *schema_table)
{
	Field**	fields;
	ulint	rows_num;
	char	lock_id[TRX_I_S_LOCK_ID_MAX_LEN + 1];
	ulint	i;

	fields = table->field;

	rows_num = trx_i_s_cache_get_rows_used(cache,
					       I_S_INNODB_TRX);

	for (i = 0; i < rows_num; i++) {

		i_s_trx_row_t*	row;
		char		trx_id[TRX_ID_MAX_LEN + 1];

		row = (i_s_trx_row_t*)
			trx_i_s_cache_get_nth_row(
				cache, I_S_INNODB_TRX, i);

		/* trx_id */
		ut_snprintf(trx_id, sizeof(trx_id), TRX_ID_FMT, row->trx_id);
		OK(field_store_string(fields[IDX_TRX_ID], trx_id));

		/* trx_state */
		OK(field_store_string(fields[IDX_TRX_STATE],
				      row->trx_state));

		/* trx_started */
		OK(field_store_time_t(fields[IDX_TRX_STARTED],
				      (time_t) row->trx_started));

		/* trx_requested_lock_id */
		/* trx_wait_started */
		if (row->trx_wait_started != 0) {

			OK(field_store_string(
				   fields[IDX_TRX_REQUESTED_LOCK_ID],
				   trx_i_s_create_lock_id(
					   row->requested_lock_row,
					   lock_id, sizeof(lock_id))));
			/* field_store_string() sets it no notnull */

			OK(field_store_time_t(
				   fields[IDX_TRX_WAIT_STARTED],
				   (time_t) row->trx_wait_started));
			fields[IDX_TRX_WAIT_STARTED]->set_notnull();
		} else {

			fields[IDX_TRX_REQUESTED_LOCK_ID]->set_null();
			fields[IDX_TRX_WAIT_STARTED]->set_null();
		}

		/* trx_weight */
		OK(fields[IDX_TRX_WEIGHT]->store((int64_t) row->trx_weight,
						 true));

		/* trx_mysql_thread_id */
		OK(fields[IDX_TRX_DRIZZLE_THREAD_ID]->store(
			   row->trx_mysql_thread_id));

		/* trx_query */
		OK(field_store_string(fields[IDX_TRX_QUERY],
				      row->trx_query));

                schema_table->addRow(table->record[0],
                                     table->s->reclength);
	}

	return(0);
}

/*******************************************************************//**
Bind the dynamic table INFORMATION_SCHEMA.innodb_trx
@return	0 on success */
int
innodb_trx_init()
/*============*/
{
	if ((innodb_trx_schema_table= new plugin::InfoSchemaTable("INNODB_TRX")) == NULL)
		return(1);

	innodb_trx_schema_table->setColumnInfo(innodb_trx_fields_info);
	innodb_trx_schema_table->setInfoSchemaMethods(&trx_methods);

	return(0);
}


/* Fields of the dynamic table INFORMATION_SCHEMA.innodb_locks */
static plugin::ColumnInfo innodb_locks_fields_info[] =
{
#define IDX_LOCK_ID		0
        plugin::ColumnInfo("lock_id",
                  TRX_I_S_LOCK_ID_MAX_LEN + 1,
                  DRIZZLE_TYPE_VARCHAR,
                  0,
                  0,
                  ""),

#define IDX_LOCK_TRX_ID		1
        plugin::ColumnInfo("lock_trx_id",
                  TRX_ID_MAX_LEN + 1,
                  DRIZZLE_TYPE_VARCHAR,
                  0,
                  0,
                  ""),

#define IDX_LOCK_MODE		2
        plugin::ColumnInfo("lock_mode",
	 /* S[,GAP] X[,GAP] IS[,GAP] IX[,GAP] AUTO_INC UNKNOWN */
                  32,
                  DRIZZLE_TYPE_VARCHAR,
                  0,
                  0,
                  ""),

#define IDX_LOCK_TYPE		3
        plugin::ColumnInfo("lock_type",
                  32, /* RECORD|TABLE|UNKNOWN */
                  DRIZZLE_TYPE_VARCHAR,
                  0,
                  0,
                  ""),

#define IDX_LOCK_TABLE		4
        plugin::ColumnInfo("lock_table",
                  1024,
                  DRIZZLE_TYPE_VARCHAR,
                  0,
                  0,
                  ""),

#define IDX_LOCK_INDEX		5
        plugin::ColumnInfo("lock_index",
                  1024,
                  DRIZZLE_TYPE_VARCHAR,
                  0,
                  MY_I_S_MAYBE_NULL,
                  ""),

#define IDX_LOCK_SPACE		6
        plugin::ColumnInfo("lock_space",
                  MY_INT64_NUM_DECIMAL_DIGITS,
                  DRIZZLE_TYPE_LONGLONG,
                  0,
                  MY_I_S_UNSIGNED | MY_I_S_MAYBE_NULL,
                  ""),

#define IDX_LOCK_PAGE		7
        plugin::ColumnInfo("lock_page",
                  MY_INT64_NUM_DECIMAL_DIGITS,
                  DRIZZLE_TYPE_LONGLONG,
                  0,
                  MY_I_S_UNSIGNED | MY_I_S_MAYBE_NULL,
                  ""),

#define IDX_LOCK_REC		8
        plugin::ColumnInfo("lock_rec",
                  MY_INT64_NUM_DECIMAL_DIGITS,
                  DRIZZLE_TYPE_LONGLONG,
                  0,
                  MY_I_S_UNSIGNED | MY_I_S_MAYBE_NULL,
                  ""),

#define IDX_LOCK_DATA		9
        plugin::ColumnInfo("lock_data",
                  TRX_I_S_LOCK_DATA_MAX_LEN,
                  DRIZZLE_TYPE_VARCHAR,
                  0,
                  MY_I_S_MAYBE_NULL,
                  ""),

        plugin::ColumnInfo()
};

/*******************************************************************//**
Read data from cache buffer and fill the INFORMATION_SCHEMA.innodb_locks
table with it.
@return	0 on success */
static
int
fill_innodb_locks_from_cache(
/*=========================*/
	trx_i_s_cache_t*	cache,	/*!< in: cache to read from */
	Session*		session,/*!< in: MySQL client connection */
	Table*			table,	/*!< in/out: fill this table */
        plugin::InfoSchemaTable *schema_table)
{
	Field**	fields;
	ulint	rows_num;
	char	lock_id[TRX_I_S_LOCK_ID_MAX_LEN + 1];
	ulint	i;

	fields = table->field;

	rows_num = trx_i_s_cache_get_rows_used(cache,
					       I_S_INNODB_LOCKS);

	for (i = 0; i < rows_num; i++) {

		i_s_locks_row_t*	row;

		/* note that the decoded database or table name is
		never expected to be longer than NAME_LEN;
		NAME_LEN for database name
		2 for surrounding quotes around database name
		NAME_LEN for table name
		2 for surrounding quotes around table name
		1 for the separating dot (.)
		9 for the #mysql50# prefix */
		char			buf[2 * NAME_LEN + 14];
		const char*		bufend;

		char			lock_trx_id[TRX_ID_MAX_LEN + 1];

		row = (i_s_locks_row_t*)
			trx_i_s_cache_get_nth_row(
				cache, I_S_INNODB_LOCKS, i);

		/* lock_id */
		trx_i_s_create_lock_id(row, lock_id, sizeof(lock_id));
		OK(field_store_string(fields[IDX_LOCK_ID],
				      lock_id));

		/* lock_trx_id */
		ut_snprintf(lock_trx_id, sizeof(lock_trx_id),
			    TRX_ID_FMT, row->lock_trx_id);
		OK(field_store_string(fields[IDX_LOCK_TRX_ID], lock_trx_id));

		/* lock_mode */
		OK(field_store_string(fields[IDX_LOCK_MODE],
				      row->lock_mode));

		/* lock_type */
		OK(field_store_string(fields[IDX_LOCK_TYPE],
				      row->lock_type));

		/* lock_table */
		bufend = innobase_convert_name(buf, sizeof(buf),
					       row->lock_table,
					       strlen(row->lock_table),
					       session, TRUE);
		OK(fields[IDX_LOCK_TABLE]->store(buf, bufend - buf,
						 system_charset_info));

		/* lock_index */
		if (row->lock_index != NULL) {

			bufend = innobase_convert_name(buf, sizeof(buf),
						       row->lock_index,
						       strlen(row->lock_index),
						       session, FALSE);
			OK(fields[IDX_LOCK_INDEX]->store(buf, bufend - buf,
							 system_charset_info));
			fields[IDX_LOCK_INDEX]->set_notnull();
		} else {

			fields[IDX_LOCK_INDEX]->set_null();
		}

		/* lock_space */
		OK(field_store_ulint(fields[IDX_LOCK_SPACE],
				     row->lock_space));

		/* lock_page */
		OK(field_store_ulint(fields[IDX_LOCK_PAGE],
				     row->lock_page));

		/* lock_rec */
		OK(field_store_ulint(fields[IDX_LOCK_REC],
				     row->lock_rec));

		/* lock_data */
		OK(field_store_string(fields[IDX_LOCK_DATA],
				      row->lock_data));

                schema_table->addRow(table->record[0],
                                     table->s->reclength);
	}

	return(0);
}

/*******************************************************************//**
Bind the dynamic table INFORMATION_SCHEMA.innodb_locks
@return	0 on success */
int
innodb_locks_init()
/*==============*/
{

	if ((innodb_locks_schema_table= new plugin::InfoSchemaTable("INNODB_LOCKS")) == NULL)
		return(1);

	innodb_locks_schema_table->setColumnInfo(innodb_locks_fields_info);
	innodb_locks_schema_table->setInfoSchemaMethods(&trx_methods);
	return(0);
}


/* Fields of the dynamic table INFORMATION_SCHEMA.innodb_lock_waits */
static plugin::ColumnInfo innodb_lock_waits_fields_info[] =
{
#define IDX_REQUESTING_TRX_ID	0
        plugin::ColumnInfo("requesting_trx_id",
                  TRX_ID_MAX_LEN + 1,
                  DRIZZLE_TYPE_VARCHAR,
                  0,
                  0,
                  ""),

#define IDX_REQUESTED_LOCK_ID	1
        plugin::ColumnInfo("requested_lock_id",
                  TRX_I_S_LOCK_ID_MAX_LEN + 1,
                  DRIZZLE_TYPE_VARCHAR,
                  0,
                  0,
                  ""),

#define IDX_BLOCKING_TRX_ID	2
        plugin::ColumnInfo("blocking_trx_id",
                  TRX_ID_MAX_LEN + 1,
                  DRIZZLE_TYPE_VARCHAR,
                  0,
                  0,
                  ""),

#define IDX_BLOCKING_LOCK_ID	3
        plugin::ColumnInfo("blocking_lock_id",
                  TRX_I_S_LOCK_ID_MAX_LEN + 1,
                  DRIZZLE_TYPE_VARCHAR,
                  0,
                  0,
                  ""),

        plugin::ColumnInfo()
};

/*******************************************************************//**
Read data from cache buffer and fill the
INFORMATION_SCHEMA.innodb_lock_waits table with it.
@return	0 on success */
static
int
fill_innodb_lock_waits_from_cache(
/*==============================*/
	trx_i_s_cache_t*	cache,	/*!< in: cache to read from */
	Table*			table,	/*!< in/out: fill this table */
        plugin::InfoSchemaTable *schema_table)
{
	Field**	fields;
	ulint	rows_num;
	char	requested_lock_id[TRX_I_S_LOCK_ID_MAX_LEN + 1];
	char	blocking_lock_id[TRX_I_S_LOCK_ID_MAX_LEN + 1];
	ulint	i;

	fields = table->field;

	rows_num = trx_i_s_cache_get_rows_used(cache,
					       I_S_INNODB_LOCK_WAITS);

	for (i = 0; i < rows_num; i++) {

		i_s_lock_waits_row_t*	row;

		char	requesting_trx_id[TRX_ID_MAX_LEN + 1];
		char	blocking_trx_id[TRX_ID_MAX_LEN + 1];

		row = (i_s_lock_waits_row_t*)
			trx_i_s_cache_get_nth_row(
				cache, I_S_INNODB_LOCK_WAITS, i);

		/* requesting_trx_id */
		ut_snprintf(requesting_trx_id, sizeof(requesting_trx_id),
			    TRX_ID_FMT, row->requested_lock_row->lock_trx_id);
		OK(field_store_string(fields[IDX_REQUESTING_TRX_ID],
				      requesting_trx_id));

		/* requested_lock_id */
		OK(field_store_string(
			   fields[IDX_REQUESTED_LOCK_ID],
			   trx_i_s_create_lock_id(
				   row->requested_lock_row,
				   requested_lock_id,
				   sizeof(requested_lock_id))));

		/* blocking_trx_id */
		ut_snprintf(blocking_trx_id, sizeof(blocking_trx_id),
			    TRX_ID_FMT, row->blocking_lock_row->lock_trx_id);
		OK(field_store_string(fields[IDX_BLOCKING_TRX_ID],
				      blocking_trx_id));

		/* blocking_lock_id */
		OK(field_store_string(
			   fields[IDX_BLOCKING_LOCK_ID],
			   trx_i_s_create_lock_id(
				   row->blocking_lock_row,
				   blocking_lock_id,
				   sizeof(blocking_lock_id))));

                schema_table->addRow(table->record[0],
                                     table->s->reclength);
	}

	return(0);
}

/*******************************************************************//**
Bind the dynamic table INFORMATION_SCHEMA.innodb_lock_waits
@return	0 on success */
int
innodb_lock_waits_init()
/*===================*/
{

	if ((innodb_lock_waits_schema_table= new plugin::InfoSchemaTable("INNODB_LOCK_WAITS")) == NULL)
		return(1);

	innodb_lock_waits_schema_table->setColumnInfo(innodb_lock_waits_fields_info);
	innodb_lock_waits_schema_table->setInfoSchemaMethods(&trx_methods);


	return(0);
}


/*******************************************************************//**
Common function to fill any of the dynamic tables:
INFORMATION_SCHEMA.innodb_trx
INFORMATION_SCHEMA.innodb_locks
INFORMATION_SCHEMA.innodb_lock_waits
@return	0 on success */
int
TrxISMethods::fillTable(
/*======================*/
	Session*	session,/*!< in: thread */
	Table*	table,	/*!< in/out: tables to fill */
        plugin::InfoSchemaTable *schema_table)
{
	const char*		table_name;
	int			ret;
	trx_i_s_cache_t*	cache;

	/* minimize the number of places where global variables are
	referenced */
	cache = trx_i_s_cache;

	/* which table we have to fill? */
	table_name = schema_table->getName().c_str();
	/* or table_name = tables->schema_table->table_name; */

	RETURN_IF_INNODB_NOT_STARTED(table_name);

	/* update the cache */
	trx_i_s_cache_start_write(cache);
	trx_i_s_possibly_fetch_data_into_cache(cache);
	trx_i_s_cache_end_write(cache);

	if (trx_i_s_cache_is_truncated(cache)) {

		/* XXX show warning to user if possible */
		fprintf(stderr, "Warning: data in %s truncated due to "
			"memory limit of %d bytes\n", table_name,
			TRX_I_S_MEM_LIMIT);
	}

	ret = 0;

	trx_i_s_cache_start_read(cache);

	if (innobase_strcasecmp(table_name, "innodb_trx") == 0) {

		if (fill_innodb_trx_from_cache(
			cache, table, schema_table) != 0) {

			ret = 1;
		}

	} else if (innobase_strcasecmp(table_name, "innodb_locks") == 0) {

		if (fill_innodb_locks_from_cache(
			cache, session, table, schema_table) != 0) {

			ret = 1;
		}

	} else if (innobase_strcasecmp(table_name, "innodb_lock_waits") == 0) {

		if (fill_innodb_lock_waits_from_cache(
			cache, table, schema_table) != 0) {

			ret = 1;
		}

	} else {

		/* huh! what happened!? */
		fprintf(stderr,
			"InnoDB: trx_i_s_common_fill_table() was "
			"called to fill unknown table: %s.\n"
			"This function only knows how to fill "
			"innodb_trx, innodb_locks and "
			"innodb_lock_waits tables.\n", table_name);

		ret = 1;
	}

	trx_i_s_cache_end_read(cache);

#if 0
	return(ret);
#else
	/* if this function returns something else than 0 then a
	deadlock occurs between the mysqld server and mysql client,
	see http://bugs.mysql.com/29900 ; when that bug is resolved
	we can enable the return(ret) above */
	return(0);
#endif
}

/* Fields of the dynamic table information_schema.innodb_cmp. */
static plugin::ColumnInfo	i_s_cmp_fields_info[] =
{
        plugin::ColumnInfo("page_size",
                  5,
                  DRIZZLE_TYPE_LONG,
                  0,
                  0,
                  "Compressed Page Size"),

        plugin::ColumnInfo("compress_ops",
                  MY_INT32_NUM_DECIMAL_DIGITS,
                  DRIZZLE_TYPE_LONG,
                  0,
                  0,
                  "Total Number of Compressions"),

        plugin::ColumnInfo("compress_ops_ok",
                  MY_INT32_NUM_DECIMAL_DIGITS,
                  DRIZZLE_TYPE_LONG,
                  0,
                  0,
                  "Total Number of Successful Compressions"),

        plugin::ColumnInfo("compress_time",
                  MY_INT32_NUM_DECIMAL_DIGITS,
                  DRIZZLE_TYPE_LONG,
                  0,
                  0,
                  "Total Duration of Compressions in Seconds"),

        plugin::ColumnInfo("uncompress_ops",
                  MY_INT32_NUM_DECIMAL_DIGITS,
                  DRIZZLE_TYPE_LONG,
                  0,
                  0,
                  "Total Number of Decompressions"),

        plugin::ColumnInfo("uncompress_time",
                  MY_INT32_NUM_DECIMAL_DIGITS,
                  DRIZZLE_TYPE_LONG,
                  0,
                  0,
                  "Total Duration of Decompressions in Seconds"),

        plugin::ColumnInfo()
};


/*******************************************************************//**
Fill the dynamic table information_schema.innodb_cmp or
innodb_cmp_reset.
@return	0 on success, 1 on failure */
static
int
i_s_cmp_fill_low(
/*=============*/
	Session*	session,/*!< in: thread */
	Table*	table,	/*!< in/out: tables to fill */
        plugin::InfoSchemaTable *schema_table,
	ibool		reset)	/*!< in: TRUE=reset cumulated counts */
{
	int	status	= 0;


	RETURN_IF_INNODB_NOT_STARTED(schema_table->getName().c_str());

	for (uint i = 0; i < PAGE_ZIP_NUM_SSIZE - 1; i++) {
		page_zip_stat_t*	zip_stat = &page_zip_stat[i];

		table->field[0]->store(PAGE_ZIP_MIN_SIZE << i);

		/* The cumulated counts are not protected by any
		mutex.  Thus, some operation in page0zip.c could
		increment a counter between the time we read it and
		clear it.  We could introduce mutex protection, but it
		could cause a measureable performance hit in
		page0zip.c. */
		table->field[1]->store(zip_stat->compressed);
		table->field[2]->store(zip_stat->compressed_ok);
		table->field[3]->store(
			(ulong) (zip_stat->compressed_usec / 1000000));
		table->field[4]->store(zip_stat->decompressed);
		table->field[5]->store(
			(ulong) (zip_stat->decompressed_usec / 1000000));

		if (reset) {
			memset(zip_stat, 0, sizeof *zip_stat);
		}

                schema_table->addRow(table->record[0],
                                     table->s->reclength);
	}

	return(status);
}

/*******************************************************************//**
Fill the dynamic table information_schema.innodb_cmp.
@return	0 on success, 1 on failure */
int
CmpISMethods::fillTable(
/*=========*/
	Session*	session,/*!< in: thread */
	Table*	table,	/*!< in/out: tables to fill */
        plugin::InfoSchemaTable *schema_table)
{
	return(i_s_cmp_fill_low(session, table, schema_table, FALSE));
}

/*******************************************************************//**
Fill the dynamic table information_schema.innodb_cmp_reset.
@return	0 on success, 1 on failure */
int
CmpResetISMethods::fillTable(
/*===============*/
	Session*	session,/*!< in: thread */
	Table*	table,	/*!< in/out: tables to fill */
        plugin::InfoSchemaTable *schema_table)
{
	return(i_s_cmp_fill_low(session, table, schema_table, TRUE));
}

/*******************************************************************//**
Bind the dynamic table information_schema.innodb_cmp.
@return	0 on success */
int
i_s_cmp_init()
/*=========*/
{

	if ((innodb_cmp_schema_table= new plugin::InfoSchemaTable("INNODB_CMP")) == NULL)
		return(1);

	innodb_cmp_schema_table->setColumnInfo(i_s_cmp_fields_info);
	innodb_cmp_schema_table->setInfoSchemaMethods(&cmp_methods);

	return(0);
}

/*******************************************************************//**
Bind the dynamic table information_schema.innodb_cmp_reset.
@return	0 on success */
int
i_s_cmp_reset_init()
/*===============*/
{

	if ((innodb_cmp_reset_schema_table= new plugin::InfoSchemaTable("INNODB_CMP_RESET")) == NULL)
		return(1);

	innodb_cmp_reset_schema_table->setColumnInfo(i_s_cmp_fields_info);
	innodb_cmp_reset_schema_table->setInfoSchemaMethods(&cmp_reset_methods);

	return(0);
}



/* Fields of the dynamic table information_schema.innodb_cmpmem. */
static plugin::ColumnInfo	i_s_cmpmem_fields_info[] =
{
        plugin::ColumnInfo("page_size",
                  5,
                  DRIZZLE_TYPE_LONG,
                  0,
                  0,
                  "Buddy Block Size"),

        plugin::ColumnInfo("pages_used",
                  MY_INT32_NUM_DECIMAL_DIGITS,
                  DRIZZLE_TYPE_LONG,
                  0,
                  0,
                  "Currently in Use"),

        plugin::ColumnInfo("pages_free",
                  MY_INT32_NUM_DECIMAL_DIGITS,
                  DRIZZLE_TYPE_LONG,
                  0,
                  0,
                  "Currently Available"),

        plugin::ColumnInfo("relocation_ops",
                  MY_INT64_NUM_DECIMAL_DIGITS,
                  DRIZZLE_TYPE_LONGLONG,
                  0,
                  0,
                  "Total Number of Relocations"),

        plugin::ColumnInfo("relocation_time",
                  MY_INT32_NUM_DECIMAL_DIGITS,
                  DRIZZLE_TYPE_LONG,
                  0,
                  0,
                  "Total Duration of Relocations, in Seconds"),

        plugin::ColumnInfo()
};

/*******************************************************************//**
Fill the dynamic table information_schema.innodb_cmpmem or
innodb_cmpmem_reset.
@return	0 on success, 1 on failure */
static
int
i_s_cmpmem_fill_low(
/*================*/
	Session*	session,/*!< in: thread */
	Table*	table,	/*!< in/out: tables to fill */
        plugin::InfoSchemaTable *schema_table,
	ibool		reset)	/*!< in: TRUE=reset cumulated counts */
{
	int	status	= 0;

	RETURN_IF_INNODB_NOT_STARTED(schema_table->getName().c_str());

	buf_pool_mutex_enter();

	for (uint x = 0; x <= BUF_BUDDY_SIZES; x++) {
		buf_buddy_stat_t*	buddy_stat = &buf_buddy_stat[x];

		table->field[0]->store(BUF_BUDDY_LOW << x);
		table->field[1]->store(buddy_stat->used);
		table->field[2]->store(UNIV_LIKELY(x < BUF_BUDDY_SIZES)
				       ? UT_LIST_GET_LEN(buf_pool->zip_free[x])
				       : 0);
		table->field[3]->store((int64_t) buddy_stat->relocated, true);
		table->field[4]->store(
			(ulong) (buddy_stat->relocated_usec / 1000000));

		if (reset) {
			/* This is protected by buf_pool_mutex. */
			buddy_stat->relocated = 0;
			buddy_stat->relocated_usec = 0;
		}

                schema_table->addRow(table->record[0],
                                     table->s->reclength);
	}

	buf_pool_mutex_exit();
	return(status);
}

/*******************************************************************//**
Fill the dynamic table information_schema.innodb_cmpmem.
@return	0 on success, 1 on failure */
int
CmpmemISMethods::fillTable(
/*============*/
	Session*	session,/*!< in: thread */
	Table*	table, /*!< in/out: tables to fill */
        plugin::InfoSchemaTable *schema_table)	
{
	return(i_s_cmpmem_fill_low(session, table, schema_table, FALSE));
}

/*******************************************************************//**
Fill the dynamic table information_schema.innodb_cmpmem_reset.
@return	0 on success, 1 on failure */
int
CmpmemResetISMethods::fillTable(
/*==================*/
	Session*	session,/*!< in: thread */
	Table*	table,	/*!< in/out: tables to fill */
        plugin::InfoSchemaTable *schema_table)
{
	return(i_s_cmpmem_fill_low(session, table, schema_table, TRUE));
}

/*******************************************************************//**
Bind the dynamic table information_schema.innodb_cmpmem.
@return	0 on success */
int
i_s_cmpmem_init()
/*============*/
{

	if ((innodb_cmpmem_schema_table= new plugin::InfoSchemaTable("INNODB_CMPMEM")) == NULL)
		return(1);

	innodb_cmpmem_schema_table->setColumnInfo(i_s_cmpmem_fields_info);
	innodb_cmpmem_schema_table->setInfoSchemaMethods(&cmpmem_methods);

	return(0);
}

/*******************************************************************//**
Bind the dynamic table information_schema.innodb_cmpmem_reset.
@return	0 on success */
int
i_s_cmpmem_reset_init()
/*==================*/
{
	if ((innodb_cmpmem_reset_schema_table= new plugin::InfoSchemaTable("INNODB_CMPMEM_RESET")) == NULL)
		return(1);

	innodb_cmpmem_reset_schema_table->setColumnInfo(i_s_cmpmem_fields_info);
	innodb_cmpmem_reset_schema_table->setInfoSchemaMethods(&cmpmem_reset_methods);
	return(0);
}


/*******************************************************************//**
Unbind a dynamic INFORMATION_SCHEMA table.
@return	0 on success */
int
i_s_common_deinit(
/*==============*/
	plugin::Registry &registry)	/*!< in/out: table schema object */
{
	registry.remove(innodb_trx_schema_table);
	registry.remove(innodb_locks_schema_table);
	registry.remove(innodb_lock_waits_schema_table);
	registry.remove(innodb_cmp_schema_table);
	registry.remove(innodb_cmp_reset_schema_table);
	registry.remove(innodb_cmpmem_schema_table);
	registry.remove(innodb_cmpmem_reset_schema_table);

	delete innodb_trx_schema_table;
	delete innodb_locks_schema_table;
	delete innodb_lock_waits_schema_table;
	delete innodb_cmp_schema_table;
	delete innodb_cmp_reset_schema_table;
	delete innodb_cmpmem_schema_table;
	delete innodb_cmpmem_reset_schema_table;

	return(0);
}