~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2006 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
17
/* Definitions for parameters to do with handler-routines */
18
19
#ifdef USE_PRAGMA_INTERFACE
20
#pragma interface			/* gcc class implementation */
21
#endif
22
212.5.27 by Monty Taylor
Moved my_handler to mysys.
23
#include <mysys/my_handler.h>
212.4.2 by Monty Taylor
Fixed the includes in places to make the myisam header file move work.
24
#include <storage/myisam/keycache.h>
1 by brian
clean slate
25
26
#ifndef NO_HASH
27
#define NO_HASH				/* Not yet implemented */
28
#endif
29
30
// the following is for checking tables
31
32
#define HA_ADMIN_ALREADY_DONE	  1
33
#define HA_ADMIN_OK               0
34
#define HA_ADMIN_NOT_IMPLEMENTED -1
35
#define HA_ADMIN_FAILED		 -2
36
#define HA_ADMIN_CORRUPT         -3
37
#define HA_ADMIN_INTERNAL_ERROR  -4
38
#define HA_ADMIN_INVALID         -5
39
#define HA_ADMIN_REJECT          -6
40
#define HA_ADMIN_TRY_ALTER       -7
41
#define HA_ADMIN_WRONG_CHECKSUM  -8
42
#define HA_ADMIN_NOT_BASE_TABLE  -9
43
#define HA_ADMIN_NEEDS_UPGRADE  -10
44
#define HA_ADMIN_NEEDS_ALTER    -11
45
#define HA_ADMIN_NEEDS_CHECK    -12
46
47
/* Bits to show what an alter table will do */
212.5.45 by Monty Taylor
Removed excess AM_CPPFLAGS from the tree. Now the only thing that should be in the include path should be -I${top_srcdir} and -I${top_builddir}w
48
#include <drizzled/sql_bitmap.h>
1 by brian
clean slate
49
50
#define HA_MAX_ALTER_FLAGS 39
51
typedef Bitmap<HA_MAX_ALTER_FLAGS> HA_ALTER_FLAGS;
52
53
#define HA_ADD_INDEX                  (0)
54
#define HA_DROP_INDEX                 (1)
55
#define HA_ALTER_INDEX                (2)
56
#define HA_RENAME_INDEX               (3)
57
#define HA_ADD_UNIQUE_INDEX           (4)
58
#define HA_DROP_UNIQUE_INDEX          (5)
59
#define HA_ALTER_UNIQUE_INDEX         (6)
60
#define HA_RENAME_UNIQUE_INDEX        (7)
61
#define HA_ADD_PK_INDEX               (8)
62
#define HA_DROP_PK_INDEX              (9)
63
#define HA_ALTER_PK_INDEX             (10)
64
#define HA_ADD_COLUMN                 (11)
65
#define HA_DROP_COLUMN                (12)
66
#define HA_CHANGE_COLUMN              (13)
67
#define HA_ALTER_COLUMN_NAME          (14)
68
#define HA_ALTER_COLUMN_TYPE          (15)
69
#define HA_ALTER_COLUMN_ORDER         (16)
70
#define HA_ALTER_COLUMN_NULLABLE      (17)
71
#define HA_COLUMN_DEFAULT_VALUE       (18)
72
#define HA_COLUMN_STORAGE             (19)
73
#define HA_COLUMN_FORMAT              (20)
74
#define HA_ADD_FOREIGN_KEY            (21)
75
#define HA_DROP_FOREIGN_KEY           (22)
76
#define HA_ALTER_FOREIGN_KEY          (23)
77
#define HA_ADD_CONSTRAINT             (24)
78
#define HA_CHANGE_CHARACTER_SET       (30)
79
#define HA_SET_DEFAULT_CHARACTER_SET  (31)
80
#define HA_CHANGE_AUTOINCREMENT_VALUE (32)
81
#define HA_ALTER_STORAGE              (33)
82
#define HA_ALTER_TABLESPACE           (34)
83
#define HA_ALTER_ROW_FORMAT           (35)
84
#define HA_RENAME_TABLE               (36)
85
#define HA_ALTER_STORAGE_ENGINE       (37)
86
#define HA_RECREATE                   (38)
87
/* Remember to increase HA_MAX_ALTER_FLAGS when adding more flags! */
88
89
/* Return values for check_if_supported_alter */
90
91
#define HA_ALTER_ERROR               -1
92
#define HA_ALTER_SUPPORTED_WAIT_LOCK  0
93
#define HA_ALTER_SUPPORTED_NO_LOCK    1
94
#define HA_ALTER_NOT_SUPPORTED        2
95
96
/* Bits in table_flags() to show what database can do */
97
98
#define HA_NO_TRANSACTIONS     (1 << 0) /* Doesn't support transactions */
99
#define HA_PARTIAL_COLUMN_READ (1 << 1) /* read may not return all columns */
100
#define HA_TABLE_SCAN_ON_INDEX (1 << 2) /* No separate data/index file */
101
/*
102
  The following should be set if the following is not true when scanning
103
  a table with rnd_next()
104
  - We will see all rows (including deleted ones)
105
  - Row positions are 'table->s->db_record_offset' apart
106
  If this flag is not set, filesort will do a postion() call for each matched
107
  row to be able to find the row later.
108
*/
109
#define HA_REC_NOT_IN_SEQ      (1 << 3)
110
/* This is now a dead option, just left for compatibility */
111
#define HA_CAN_GEOMETRY        (1 << 4)
112
/*
113
  Reading keys in random order is as fast as reading keys in sort order
114
  (Used in records.cc to decide if we should use a record cache and by
115
  filesort to decide if we should sort key + data or key + pointer-to-row
116
*/
117
#define HA_FAST_KEY_READ       (1 << 5)
118
/*
119
  Set the following flag if we on delete should force all key to be read
120
  and on update read all keys that changes
121
*/
122
#define HA_REQUIRES_KEY_COLUMNS_FOR_DELETE (1 << 6)
123
#define HA_NULL_IN_KEY         (1 << 7) /* One can have keys with NULL */
124
#define HA_DUPLICATE_POS       (1 << 8)    /* ha_position() gives dup row */
125
#define HA_NO_BLOBS            (1 << 9) /* Doesn't support blobs */
126
#define HA_CAN_INDEX_BLOBS     (1 << 10)
127
#define HA_AUTO_PART_KEY       (1 << 11) /* auto-increment in multi-part key */
128
#define HA_REQUIRE_PRIMARY_KEY (1 << 12) /* .. and can't create a hidden one */
129
#define HA_STATS_RECORDS_IS_EXACT (1 << 13) /* stats.records is exact */
130
/*
131
  INSERT_DELAYED only works with handlers that uses MySQL internal table
132
  level locks
133
*/
134
#define HA_CAN_INSERT_DELAYED  (1 << 14)
135
/*
136
  If we get the primary key columns for free when we do an index read
137
  It also implies that we have to retrive the primary key when using
138
  position() and rnd_pos().
139
*/
140
#define HA_PRIMARY_KEY_IN_READ_INDEX (1 << 15)
141
/*
142
  If HA_PRIMARY_KEY_REQUIRED_FOR_POSITION is set, it means that to position()
143
  uses a primary key. Without primary key, we can't call position().
144
*/ 
145
#define HA_PRIMARY_KEY_REQUIRED_FOR_POSITION (1 << 16) 
99 by Brian Aker
Second pass at removing old varchar.
146
#define HA_CAN_RTREEKEYS       (1 << 17) /* Historical, no longer supported */
1 by brian
clean slate
147
#define HA_NOT_DELETE_WITH_CACHE (1 << 18)
148
/*
149
  The following is we need to a primary key to delete (and update) a row.
150
  If there is no primary key, all columns needs to be read on update and delete
151
*/
152
#define HA_PRIMARY_KEY_REQUIRED_FOR_DELETE (1 << 19)
153
#define HA_NO_PREFIX_CHAR_KEYS (1 << 20)
99 by Brian Aker
Second pass at removing old varchar.
154
#define HA_CAN_FULLTEXT        (1 << 21) /* Historical, no longer supported */
155
#define HA_CAN_SQL_HANDLER     (1 << 22) /* Historical, no longer supported */
1 by brian
clean slate
156
#define HA_NO_AUTO_INCREMENT   (1 << 23)
157
#define HA_HAS_CHECKSUM        (1 << 24)
158
/* Table data are stored in separate files (for lower_case_table_names) */
159
#define HA_FILE_BASED	       (1 << 26)
99 by Brian Aker
Second pass at removing old varchar.
160
#define HA_NO_VARCHAR	       (1 << 27) /* Historical, no longer supported */
161
#define HA_CAN_BIT_FIELD       (1 << 28) /* Historical, no longer supported */
1 by brian
clean slate
162
#define HA_NEED_READ_RANGE_BUFFER (1 << 29) /* for read_multi_range */
163
#define HA_ANY_INDEX_MAY_BE_UNIQUE (1 << 30)
80.1.1 by Brian Aker
LL() cleanup
164
#define HA_NO_COPY_ON_ALTER    (1LL << 31)
165
#define HA_HAS_RECORDS	       (1LL << 32) /* records() gives exact count*/
1 by brian
clean slate
166
/* Has it's own method of binlog logging */
99 by Brian Aker
Second pass at removing old varchar.
167
#define HA_HAS_OWN_BINLOGGING  (1LL << 33) /* Historical, no longer supported */
80.1.1 by Brian Aker
LL() cleanup
168
#define HA_MRR_CANT_SORT       (1LL << 34)
1 by brian
clean slate
169
170
/*
171
  Engine is capable of row-format and statement-format logging,
172
  respectively
173
*/
80.1.1 by Brian Aker
LL() cleanup
174
#define HA_BINLOG_ROW_CAPABLE  (1LL << 35)
175
#define HA_BINLOG_STMT_CAPABLE (1LL << 36)
1 by brian
clean slate
176
80.1.1 by Brian Aker
LL() cleanup
177
#define HA_ONLINE_ALTER        (1LL << 37)
1 by brian
clean slate
178
179
/*
180
  Set of all binlog flags. Currently only contain the capabilities
181
  flags.
182
 */
183
#define HA_BINLOG_FLAGS (HA_BINLOG_ROW_CAPABLE | HA_BINLOG_STMT_CAPABLE)
184
185
/* bits in index_flags(index_number) for what you can do with index */
186
#define HA_READ_NEXT            1       /* TODO really use this flag */
187
#define HA_READ_PREV            2       /* supports ::index_prev */
188
#define HA_READ_ORDER           4       /* index_next/prev follow sort order */
189
#define HA_READ_RANGE           8       /* can find all records in a range */
190
#define HA_ONLY_WHOLE_INDEX	16	/* Can't use part key searches */
191
#define HA_KEYREAD_ONLY         64	/* Support HA_EXTRA_KEYREAD */
192
/*
193
  Index scan will not return records in rowid order. Not guaranteed to be
194
  set for unordered (e.g. HASH) indexes.
195
*/
196
#define HA_KEY_SCAN_NOT_ROR     128 
197
#define HA_DO_INDEX_COND_PUSHDOWN  256 /* Supports Index Condition Pushdown */
198
199
200
201
/*
202
  HA_PARTITION_FUNCTION_SUPPORTED indicates that the function is
203
  supported at all.
204
  HA_FAST_CHANGE_PARTITION means that optimised variants of the changes
205
  exists but they are not necessarily done online.
206
207
  HA_ONLINE_DOUBLE_WRITE means that the handler supports writing to both
208
  the new partition and to the old partitions when updating through the
209
  old partitioning schema while performing a change of the partitioning.
210
  This means that we can support updating of the table while performing
211
  the copy phase of the change. For no lock at all also a double write
212
  from new to old must exist and this is not required when this flag is
213
  set.
214
  This is actually removed even before it was introduced the first time.
215
  The new idea is that handlers will handle the lock level already in
216
  store_lock for ALTER TABLE partitions.
217
218
  HA_PARTITION_ONE_PHASE is a flag that can be set by handlers that take
219
  care of changing the partitions online and in one phase. Thus all phases
220
  needed to handle the change are implemented inside the storage engine.
221
  The storage engine must also support auto-discovery since the frm file
222
  is changed as part of the change and this change must be controlled by
223
  the storage engine. A typical engine to support this is NDB (through
224
  WL #2498).
225
*/
226
#define HA_PARTITION_FUNCTION_SUPPORTED         (1L << 1)
227
#define HA_FAST_CHANGE_PARTITION                (1L << 2)
228
#define HA_PARTITION_ONE_PHASE                  (1L << 3)
229
230
/* operations for disable/enable indexes */
231
#define HA_KEY_SWITCH_NONUNIQ      0
232
#define HA_KEY_SWITCH_ALL          1
233
#define HA_KEY_SWITCH_NONUNIQ_SAVE 2
234
#define HA_KEY_SWITCH_ALL_SAVE     3
235
236
/*
237
  Note: the following includes binlog and closing 0.
238
  so: innodb + bdb + ndb + binlog + myisam + myisammrg + archive +
239
      example + csv + heap + blackhole + federated + 0
240
  (yes, the sum is deliberately inaccurate)
241
  TODO remove the limit, use dynarrays
242
*/
243
#define MAX_HA 15
244
245
/*
246
  Parameters for open() (in register form->filestat)
247
  HA_GET_INFO does an implicit HA_ABORT_IF_LOCKED
248
*/
249
250
#define HA_OPEN_KEYFILE		1
251
#define HA_OPEN_RNDFILE		2
252
#define HA_GET_INDEX		4
253
#define HA_GET_INFO		8	/* do a ha_info() after open */
254
#define HA_READ_ONLY		16	/* File opened as readonly */
255
/* Try readonly if can't open with read and write */
256
#define HA_TRY_READ_ONLY	32
257
#define HA_WAIT_IF_LOCKED	64	/* Wait if locked on open */
258
#define HA_ABORT_IF_LOCKED	128	/* skip if locked on open.*/
259
#define HA_BLOCK_LOCK		256	/* unlock when reading some records */
260
#define HA_OPEN_TEMPORARY	512
261
262
/* For transactional LOCK TABLE. handler::lock_table() */
263
#define HA_LOCK_IN_SHARE_MODE      F_RDLCK
264
#define HA_LOCK_IN_EXCLUSIVE_MODE  F_WRLCK
265
266
/* Some key definitions */
267
#define HA_KEY_NULL_LENGTH	1
268
#define HA_KEY_BLOB_LENGTH	2
269
270
#define HA_LEX_CREATE_TMP_TABLE	1
271
#define HA_LEX_CREATE_IF_NOT_EXISTS 2
272
#define HA_LEX_CREATE_TABLE_LIKE 4
273
#define HA_OPTION_NO_CHECKSUM	(1L << 17)
274
#define HA_OPTION_NO_DELAY_KEY_WRITE (1L << 18)
275
#define HA_MAX_REC_LENGTH	65535
276
277
/* Table caching type */
278
#define HA_CACHE_TBL_NONTRANSACT 0
279
#define HA_CACHE_TBL_NOCACHE     1
280
#define HA_CACHE_TBL_ASKTRANSACT 2
281
#define HA_CACHE_TBL_TRANSACT    4
282
283
/* Options of START TRANSACTION statement (and later of SET TRANSACTION stmt) */
284
#define MYSQL_START_TRANS_OPT_WITH_CONS_SNAPSHOT 1
285
286
/* Flags for method is_fatal_error */
287
#define HA_CHECK_DUP_KEY 1
288
#define HA_CHECK_DUP_UNIQUE 2
289
#define HA_CHECK_DUP (HA_CHECK_DUP_KEY + HA_CHECK_DUP_UNIQUE)
290
291
enum legacy_db_type
292
{
293
  DB_TYPE_UNKNOWN=0,DB_TYPE_DIAB_ISAM=1,
294
  DB_TYPE_HASH,DB_TYPE_MISAM,DB_TYPE_PISAM,
295
  DB_TYPE_RMS_ISAM, DB_TYPE_HEAP, DB_TYPE_ISAM,
296
  DB_TYPE_MRG_ISAM, DB_TYPE_MYISAM, DB_TYPE_MRG_MYISAM,
297
  DB_TYPE_BERKELEY_DB, DB_TYPE_INNODB,
298
  DB_TYPE_GEMINI, DB_TYPE_NDBCLUSTER,
299
  DB_TYPE_EXAMPLE_DB, DB_TYPE_ARCHIVE_DB, DB_TYPE_CSV_DB,
300
  DB_TYPE_FEDERATED_DB,
301
  DB_TYPE_BLACKHOLE_DB,
302
  DB_TYPE_PARTITION_DB,
303
  DB_TYPE_BINLOG,
304
  DB_TYPE_SOLID,
305
  DB_TYPE_PBXT,
306
  DB_TYPE_TABLE_FUNCTION,
307
  DB_TYPE_MEMCACHE,
308
  DB_TYPE_FALCON,
309
  DB_TYPE_MARIA,
310
  DB_TYPE_FIRST_DYNAMIC=42,
311
  DB_TYPE_DEFAULT=127 // Must be last
312
};
313
314
enum row_type { ROW_TYPE_NOT_USED=-1, ROW_TYPE_DEFAULT, ROW_TYPE_FIXED,
315
		ROW_TYPE_DYNAMIC, ROW_TYPE_COMPRESSED,
316
		ROW_TYPE_REDUNDANT, ROW_TYPE_COMPACT, ROW_TYPE_PAGE };
317
318
enum column_format_type { COLUMN_FORMAT_TYPE_NOT_USED= -1,
319
                          COLUMN_FORMAT_TYPE_DEFAULT=   0,
320
                          COLUMN_FORMAT_TYPE_FIXED=     1,
321
                          COLUMN_FORMAT_TYPE_DYNAMIC=   2 };
322
323
enum enum_binlog_func {
324
  BFN_RESET_LOGS=        1,
325
  BFN_RESET_SLAVE=       2,
326
  BFN_BINLOG_WAIT=       3,
327
  BFN_BINLOG_END=        4,
328
  BFN_BINLOG_PURGE_FILE= 5
329
};
330
331
enum enum_binlog_command {
332
  LOGCOM_CREATE_TABLE,
333
  LOGCOM_ALTER_TABLE,
334
  LOGCOM_RENAME_TABLE,
335
  LOGCOM_DROP_TABLE,
336
  LOGCOM_CREATE_DB,
337
  LOGCOM_ALTER_DB,
338
  LOGCOM_DROP_DB
339
};
340
341
/* struct to hold information about the table that should be created */
342
343
/* Bits in used_fields */
344
#define HA_CREATE_USED_AUTO             (1L << 0)
99 by Brian Aker
Second pass at removing old varchar.
345
#define HA_CREATE_USED_RAID             (1L << 1) /* Historical, no longer supported */
1 by brian
clean slate
346
#define HA_CREATE_USED_UNION            (1L << 2)
347
#define HA_CREATE_USED_INSERT_METHOD    (1L << 3)
348
#define HA_CREATE_USED_MIN_ROWS         (1L << 4)
349
#define HA_CREATE_USED_MAX_ROWS         (1L << 5)
350
#define HA_CREATE_USED_AVG_ROW_LENGTH   (1L << 6)
351
#define HA_CREATE_USED_PACK_KEYS        (1L << 7)
352
#define HA_CREATE_USED_CHARSET          (1L << 8)
353
#define HA_CREATE_USED_DEFAULT_CHARSET  (1L << 9)
354
#define HA_CREATE_USED_DATADIR          (1L << 10)
355
#define HA_CREATE_USED_INDEXDIR         (1L << 11)
356
#define HA_CREATE_USED_ENGINE           (1L << 12)
357
#define HA_CREATE_USED_CHECKSUM         (1L << 13)
358
#define HA_CREATE_USED_DELAY_KEY_WRITE  (1L << 14)
359
#define HA_CREATE_USED_ROW_FORMAT       (1L << 15)
360
#define HA_CREATE_USED_COMMENT          (1L << 16)
361
#define HA_CREATE_USED_PASSWORD         (1L << 17)
362
#define HA_CREATE_USED_CONNECTION       (1L << 18)
363
#define HA_CREATE_USED_KEY_BLOCK_SIZE   (1L << 19)
364
#define HA_CREATE_USED_TRANSACTIONAL    (1L << 20)
365
#define HA_CREATE_USED_PAGE_CHECKSUM    (1L << 21)
244.1.1 by Harrison Fisk
Port Ebay/Google memory storage engine variable width columns.
366
#define HA_CREATE_USED_BLOCK_SIZE       (1L << 22)
1 by brian
clean slate
367
368
typedef uint64_t my_xid; // this line is the same as in log_event.h
369
#define MYSQL_XID_PREFIX "MySQLXid"
370
#define MYSQL_XID_PREFIX_LEN 8 // must be a multiple of 8
371
#define MYSQL_XID_OFFSET (MYSQL_XID_PREFIX_LEN+sizeof(server_id))
372
#define MYSQL_XID_GTRID_LEN (MYSQL_XID_OFFSET+sizeof(my_xid))
373
374
#define XIDDATASIZE MYSQL_XIDDATASIZE
375
#define MAXGTRIDSIZE 64
376
#define MAXBQUALSIZE 64
377
378
#define COMPATIBLE_DATA_YES 0
379
#define COMPATIBLE_DATA_NO  1
380
243.1.1 by Jay Pipes
* Pulled Object_creation_ctx and Default_creation_ctx out of mysql_priv.h
381
typedef bool (*qc_engine_callback)(THD *thd, char *table_key,
382
                                      uint key_length,
383
                                      uint64_t *engine_data);
384
1 by brian
clean slate
385
/**
386
  struct xid_t is binary compatible with the XID structure as
387
  in the X/Open CAE Specification, Distributed Transaction Processing:
388
  The XA Specification, X/Open Company Ltd., 1991.
389
  http://www.opengroup.org/bookstore/catalog/c193.htm
390
391
  @see MYSQL_XID in mysql/plugin.h
392
*/
393
struct xid_t {
394
  long formatID;
395
  long gtrid_length;
396
  long bqual_length;
397
  char data[XIDDATASIZE];  // not \0-terminated !
398
399
  xid_t() {}                                /* Remove gcc warning */  
400
  bool eq(struct xid_t *xid)
401
  { return eq(xid->gtrid_length, xid->bqual_length, xid->data); }
402
  bool eq(long g, long b, const char *d)
403
  { return g == gtrid_length && b == bqual_length && !memcmp(d, data, g+b); }
404
  void set(struct xid_t *xid)
405
  { memcpy(this, xid, xid->length()); }
406
  void set(long f, const char *g, long gl, const char *b, long bl)
407
  {
408
    formatID= f;
409
    memcpy(data, g, gtrid_length= gl);
410
    memcpy(data+gl, b, bqual_length= bl);
411
  }
412
  void set(uint64_t xid)
413
  {
414
    my_xid tmp;
415
    formatID= 1;
416
    set(MYSQL_XID_PREFIX_LEN, 0, MYSQL_XID_PREFIX);
417
    memcpy(data+MYSQL_XID_PREFIX_LEN, &server_id, sizeof(server_id));
418
    tmp= xid;
419
    memcpy(data+MYSQL_XID_OFFSET, &tmp, sizeof(tmp));
420
    gtrid_length=MYSQL_XID_GTRID_LEN;
421
  }
422
  void set(long g, long b, const char *d)
423
  {
424
    formatID= 1;
425
    gtrid_length= g;
426
    bqual_length= b;
427
    memcpy(data, d, g+b);
428
  }
429
  bool is_null() { return formatID == -1; }
430
  void null() { formatID= -1; }
431
  my_xid quick_get_my_xid()
432
  {
433
    my_xid tmp;
434
    memcpy(&tmp, data+MYSQL_XID_OFFSET, sizeof(tmp));
435
    return tmp;
436
  }
437
  my_xid get_my_xid()
438
  {
439
    return gtrid_length == MYSQL_XID_GTRID_LEN && bqual_length == 0 &&
440
           !memcmp(data+MYSQL_XID_PREFIX_LEN, &server_id, sizeof(server_id)) &&
441
           !memcmp(data, MYSQL_XID_PREFIX, MYSQL_XID_PREFIX_LEN) ?
442
           quick_get_my_xid() : 0;
443
  }
444
  uint length()
445
  {
446
    return sizeof(formatID)+sizeof(gtrid_length)+sizeof(bqual_length)+
447
           gtrid_length+bqual_length;
448
  }
449
  uchar *key()
450
  {
451
    return (uchar *)&gtrid_length;
452
  }
453
  uint key_length()
454
  {
455
    return sizeof(gtrid_length)+sizeof(bqual_length)+gtrid_length+bqual_length;
456
  }
457
};
458
typedef struct xid_t XID;
459
460
/* for recover() handlerton call */
461
#define MIN_XID_LIST_SIZE  128
462
#define MAX_XID_LIST_SIZE  (1024*128)
463
464
struct handlerton;
465
466
/* The handler for a table type.  Will be included in the TABLE structure */
467
468
struct st_table;
469
typedef struct st_table TABLE;
470
typedef struct st_table_share TABLE_SHARE;
471
struct st_foreign_key_info;
472
typedef struct st_foreign_key_info FOREIGN_KEY_INFO;
473
typedef bool (stat_print_fn)(THD *thd, const char *type, uint type_len,
474
                             const char *file, uint file_len,
475
                             const char *status, uint status_len);
476
enum ha_stat_type { HA_ENGINE_STATUS, HA_ENGINE_LOGS, HA_ENGINE_MUTEX };
477
extern st_plugin_int *hton2plugin[MAX_HA];
478
479
/*
480
  handlerton is a singleton structure - one instance per storage engine -
481
  to provide access to storage engine functionality that works on the
482
  "global" level (unlike handler class that works on a per-table basis)
483
484
  usually handlerton instance is defined statically in ha_xxx.cc as
485
486
  static handlerton { ... } xxx_hton;
487
488
  savepoint_*, prepare, recover, and *_by_xid pointers can be 0.
489
*/
490
struct handlerton
491
{
492
  /*
493
    Historical marker for if the engine is available of not
494
  */
495
  SHOW_COMP_OPTION state;
496
497
  /*
498
    Historical number used for frm file to determine the correct storage engine.
499
    This is going away and new engines will just use "name" for this.
500
  */
501
  enum legacy_db_type db_type;
502
  /*
503
    each storage engine has it's own memory area (actually a pointer)
504
    in the thd, for storing per-connection information.
505
    It is accessed as
506
507
      thd->ha_data[xxx_hton.slot]
508
509
   slot number is initialized by MySQL after xxx_init() is called.
510
   */
511
   uint slot;
512
   /*
513
     to store per-savepoint data storage engine is provided with an area
514
     of a requested size (0 is ok here).
515
     savepoint_offset must be initialized statically to the size of
516
     the needed memory to store per-savepoint information.
517
     After xxx_init it is changed to be an offset to savepoint storage
518
     area and need not be used by storage engine.
519
     see binlog_hton and binlog_savepoint_set/rollback for an example.
520
   */
521
   uint savepoint_offset;
522
   /*
523
     handlerton methods:
524
525
     close_connection is only called if
526
     thd->ha_data[xxx_hton.slot] is non-zero, so even if you don't need
527
     this storage area - set it to something, so that MySQL would know
528
     this storage engine was accessed in this connection
529
   */
530
   int  (*close_connection)(handlerton *hton, THD *thd);
531
   /*
532
     sv points to an uninitialized storage area of requested size
533
     (see savepoint_offset description)
534
   */
535
   int  (*savepoint_set)(handlerton *hton, THD *thd, void *sv);
536
   /*
537
     sv points to a storage area, that was earlier passed
538
     to the savepoint_set call
539
   */
540
   int  (*savepoint_rollback)(handlerton *hton, THD *thd, void *sv);
541
   int  (*savepoint_release)(handlerton *hton, THD *thd, void *sv);
542
   /*
543
     'all' is true if it's a real commit, that makes persistent changes
544
     'all' is false if it's not in fact a commit but an end of the
545
     statement that is part of the transaction.
546
     NOTE 'all' is also false in auto-commit mode where 'end of statement'
547
     and 'real commit' mean the same event.
548
   */
549
   int  (*commit)(handlerton *hton, THD *thd, bool all);
550
   int  (*rollback)(handlerton *hton, THD *thd, bool all);
551
   int  (*prepare)(handlerton *hton, THD *thd, bool all);
552
   int  (*recover)(handlerton *hton, XID *xid_list, uint len);
553
   int  (*commit_by_xid)(handlerton *hton, XID *xid);
554
   int  (*rollback_by_xid)(handlerton *hton, XID *xid);
555
   void *(*create_cursor_read_view)(handlerton *hton, THD *thd);
556
   void (*set_cursor_read_view)(handlerton *hton, THD *thd, void *read_view);
557
   void (*close_cursor_read_view)(handlerton *hton, THD *thd, void *read_view);
558
   handler *(*create)(handlerton *hton, TABLE_SHARE *table, MEM_ROOT *mem_root);
559
   void (*drop_database)(handlerton *hton, char* path);
560
   int (*start_consistent_snapshot)(handlerton *hton, THD *thd);
561
   bool (*flush_logs)(handlerton *hton);
562
   bool (*show_status)(handlerton *hton, THD *thd, stat_print_fn *print, enum ha_stat_type stat);
563
   int (*fill_files_table)(handlerton *hton, THD *thd,
564
                           TABLE_LIST *tables,
565
                           class Item *cond);
205 by Brian Aker
uint32 -> uin32_t
566
   uint32_t flags;                                /* global handler flags */
1 by brian
clean slate
567
   int (*release_temporary_latches)(handlerton *hton, THD *thd);
568
569
   int (*discover)(handlerton *hton, THD* thd, const char *db, 
570
                   const char *name,
571
                   uchar **frmblob, 
572
                   size_t *frmlen);
573
   int (*table_exists_in_engine)(handlerton *hton, THD* thd, const char *db,
574
                                 const char *name);
205 by Brian Aker
uint32 -> uin32_t
575
   uint32_t license; /* Flag for Engine License */
1 by brian
clean slate
576
   void *data; /* Location for engines to keep personal structures */
577
};
578
579
580
/* Possible flags of a handlerton (there can be 32 of them) */
581
#define HTON_NO_FLAGS                 0
582
#define HTON_CLOSE_CURSORS_AT_COMMIT (1 << 0)
583
#define HTON_ALTER_NOT_SUPPORTED     (1 << 1) //Engine does not support alter
584
#define HTON_CAN_RECREATE            (1 << 2) //Delete all is used fro truncate
585
#define HTON_HIDDEN                  (1 << 3) //Engine does not appear in lists
586
#define HTON_FLUSH_AFTER_RENAME      (1 << 4)
587
#define HTON_NOT_USER_SELECTABLE     (1 << 5)
588
#define HTON_TEMPORARY_NOT_SUPPORTED (1 << 6) //Having temporary tables not supported
589
#define HTON_SUPPORT_LOG_TABLES      (1 << 7) //Engine supports log tables
590
#define HTON_NO_PARTITION            (1 << 8) //You can not partition these tables
591
592
class Ha_trx_info;
593
594
struct THD_TRANS
595
{
596
  /* true is not all entries in the ht[] support 2pc */
597
  bool        no_2pc;
598
  /* storage engines that registered in this transaction */
599
  Ha_trx_info *ha_list;
600
  /* 
601
    The purpose of this flag is to keep track of non-transactional
602
    tables that were modified in scope of:
603
    - transaction, when the variable is a member of
604
    THD::transaction.all
605
    - top-level statement or sub-statement, when the variable is a
606
    member of THD::transaction.stmt
607
    This member has the following life cycle:
608
    * stmt.modified_non_trans_table is used to keep track of
609
    modified non-transactional tables of top-level statements. At
610
    the end of the previous statement and at the beginning of the session,
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
611
    it is reset to false.  If such functions
1 by brian
clean slate
612
    as mysql_insert, mysql_update, mysql_delete etc modify a
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
613
    non-transactional table, they set this flag to true.  At the
1 by brian
clean slate
614
    end of the statement, the value of stmt.modified_non_trans_table 
615
    is merged with all.modified_non_trans_table and gets reset.
616
    * all.modified_non_trans_table is reset at the end of transaction
617
    
618
    * Since we do not have a dedicated context for execution of a
619
    sub-statement, to keep track of non-transactional changes in a
620
    sub-statement, we re-use stmt.modified_non_trans_table. 
621
    At entrance into a sub-statement, a copy of the value of
622
    stmt.modified_non_trans_table (containing the changes of the
623
    outer statement) is saved on stack. Then 
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
624
    stmt.modified_non_trans_table is reset to false and the
1 by brian
clean slate
625
    substatement is executed. Then the new value is merged with the
626
    saved value.
627
  */
628
  bool modified_non_trans_table;
629
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
630
  void reset() { no_2pc= false; modified_non_trans_table= false; }
1 by brian
clean slate
631
};
632
633
634
/**
635
  Either statement transaction or normal transaction - related
636
  thread-specific storage engine data.
637
638
  If a storage engine participates in a statement/transaction,
639
  an instance of this class is present in
640
  thd->transaction.{stmt|all}.ha_list. The addition to
641
  {stmt|all}.ha_list is made by trans_register_ha().
642
643
  When it's time to commit or rollback, each element of ha_list
644
  is used to access storage engine's prepare()/commit()/rollback()
645
  methods, and also to evaluate if a full two phase commit is
646
  necessary.
647
648
  @sa General description of transaction handling in handler.cc.
649
*/
650
651
class Ha_trx_info
652
{
653
public:
654
  /** Register this storage engine in the given transaction context. */
655
  void register_ha(THD_TRANS *trans, handlerton *ht_arg)
656
  {
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
657
    assert(m_flags == 0);
658
    assert(m_ht == NULL);
659
    assert(m_next == NULL);
1 by brian
clean slate
660
661
    m_ht= ht_arg;
662
    m_flags= (int) TRX_READ_ONLY; /* Assume read-only at start. */
663
664
    m_next= trans->ha_list;
665
    trans->ha_list= this;
666
  }
667
668
  /** Clear, prepare for reuse. */
669
  void reset()
670
  {
671
    m_next= NULL;
672
    m_ht= NULL;
673
    m_flags= 0;
674
  }
675
676
  Ha_trx_info() { reset(); }
677
678
  void set_trx_read_write()
679
  {
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
680
    assert(is_started());
1 by brian
clean slate
681
    m_flags|= (int) TRX_READ_WRITE;
682
  }
683
  bool is_trx_read_write() const
684
  {
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
685
    assert(is_started());
1 by brian
clean slate
686
    return m_flags & (int) TRX_READ_WRITE;
687
  }
688
  bool is_started() const { return m_ht != NULL; }
689
  /** Mark this transaction read-write if the argument is read-write. */
690
  void coalesce_trx_with(const Ha_trx_info *stmt_trx)
691
  {
692
    /*
693
      Must be called only after the transaction has been started.
694
      Can be called many times, e.g. when we have many
695
      read-write statements in a transaction.
696
    */
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
697
    assert(is_started());
1 by brian
clean slate
698
    if (stmt_trx->is_trx_read_write())
699
      set_trx_read_write();
700
  }
701
  Ha_trx_info *next() const
702
  {
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
703
    assert(is_started());
1 by brian
clean slate
704
    return m_next;
705
  }
706
  handlerton *ht() const
707
  {
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
708
    assert(is_started());
1 by brian
clean slate
709
    return m_ht;
710
  }
711
private:
712
  enum { TRX_READ_ONLY= 0, TRX_READ_WRITE= 1 };
713
  /** Auxiliary, used for ha_list management */
714
  Ha_trx_info *m_next;
715
  /**
716
    Although a given Ha_trx_info instance is currently always used
717
    for the same storage engine, 'ht' is not-NULL only when the
718
    corresponding storage is a part of a transaction.
719
  */
720
  handlerton *m_ht;
721
  /**
722
    Transaction flags related to this engine.
723
    Not-null only if this instance is a part of transaction.
724
    May assume a combination of enum values above.
725
  */
726
  uchar       m_flags;
727
};
728
729
730
enum enum_tx_isolation { ISO_READ_UNCOMMITTED, ISO_READ_COMMITTED,
731
                         ISO_REPEATABLE_READ, ISO_SERIALIZABLE};
732
733
typedef struct {
734
  uint64_t data_file_length;
735
  uint64_t max_data_file_length;
736
  uint64_t index_file_length;
737
  uint64_t delete_length;
738
  ha_rows records;
61 by Brian Aker
Conversion of handler type.
739
  uint32_t mean_rec_length;
1 by brian
clean slate
740
  time_t create_time;
741
  time_t check_time;
742
  time_t update_time;
743
  uint64_t check_sum;
744
} PARTITION_INFO;
745
746
#define UNDEF_NODEGROUP 65535
747
class Item;
748
struct st_table_log_memory_entry;
749
205 by Brian Aker
uint32 -> uin32_t
750
#define NOT_A_PARTITION_ID ((uint32_t)-1)
1 by brian
clean slate
751
752
enum ha_choice { HA_CHOICE_UNDEF, HA_CHOICE_NO, HA_CHOICE_YES };
753
754
typedef struct st_ha_create_information
755
{
756
  CHARSET_INFO *table_charset, *default_table_charset;
757
  LEX_STRING connect_string;
758
  LEX_STRING comment;
759
  const char *data_file_name, *index_file_name;
760
  const char *alias;
761
  uint64_t max_rows,min_rows;
762
  uint64_t auto_increment_value;
61 by Brian Aker
Conversion of handler type.
763
  uint32_t table_options;
764
  uint32_t avg_row_length;
765
  uint32_t used_fields;
766
  uint32_t key_block_size;
244.1.1 by Harrison Fisk
Port Ebay/Google memory storage engine variable width columns.
767
  uint32_t block_size;
1 by brian
clean slate
768
  SQL_LIST merge_list;
769
  handlerton *db_type;
770
  enum row_type row_type;
771
  uint null_bits;                       /* NULL bits at start of record */
772
  uint options;				/* OR of HA_CREATE_ options */
773
  uint merge_insert_method;
774
  uint extra_size;                      /* length of extra data segment */
775
  /* 0 not used, 1 if not transactional, 2 if transactional */
776
  enum ha_choice transactional;
777
  bool table_existed;			/* 1 in create if table existed */
778
  bool frm_only;                        /* 1 if no ha_create_table() */
779
  bool varchar;                         /* 1 if table has a VARCHAR */
780
  enum ha_choice page_checksum;         /* If we have page_checksums */
781
} HA_CREATE_INFO;
782
783
typedef struct st_ha_alter_information
784
{
785
  KEY  *key_info_buffer;
786
  uint key_count;
787
  uint index_drop_count;
788
  uint *index_drop_buffer;
789
  uint index_add_count;
790
  uint *index_add_buffer;
791
  void *data;
792
} HA_ALTER_INFO;
793
794
795
typedef struct st_key_create_information
796
{
797
  enum ha_key_alg algorithm;
61 by Brian Aker
Conversion of handler type.
798
  uint32_t block_size;
1 by brian
clean slate
799
  LEX_STRING parser_name;
800
  LEX_STRING comment;
801
} KEY_CREATE_INFO;
802
803
804
/*
805
  Class for maintaining hooks used inside operations on tables such
806
  as: create table functions, delete table functions, and alter table
807
  functions.
808
809
  Class is using the Template Method pattern to separate the public
810
  usage interface from the private inheritance interface.  This
811
  imposes no overhead, since the public non-virtual function is small
812
  enough to be inlined.
813
814
  The hooks are usually used for functions that does several things,
815
  e.g., create_table_from_items(), which both create a table and lock
816
  it.
817
 */
818
class TABLEOP_HOOKS
819
{
820
public:
821
  TABLEOP_HOOKS() {}
822
  virtual ~TABLEOP_HOOKS() {}
823
824
  inline void prelock(TABLE **tables, uint count)
825
  {
826
    do_prelock(tables, count);
827
  }
828
829
  inline int postlock(TABLE **tables, uint count)
830
  {
831
    return do_postlock(tables, count);
832
  }
833
private:
834
  /* Function primitive that is called prior to locking tables */
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
835
  virtual void do_prelock(TABLE **tables __attribute__((unused)),
836
                          uint count __attribute__((unused)))
1 by brian
clean slate
837
  {
838
    /* Default is to do nothing */
839
  }
840
841
  /**
842
     Primitive called after tables are locked.
843
844
     If an error is returned, the tables will be unlocked and error
845
     handling start.
846
847
     @return Error code or zero.
848
   */
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
849
  virtual int do_postlock(TABLE **tables __attribute__((unused)),
850
                          uint count __attribute__((unused)))
1 by brian
clean slate
851
  {
852
    return 0;                           /* Default is to do nothing */
853
  }
854
};
855
856
typedef struct st_savepoint SAVEPOINT;
61 by Brian Aker
Conversion of handler type.
857
extern uint32_t savepoint_alloc_size;
1 by brian
clean slate
858
extern KEY_CREATE_INFO default_key_create_info;
859
860
/* Forward declaration for condition pushdown to storage engine */
861
typedef class Item COND;
862
863
typedef struct st_ha_check_opt
864
{
865
  st_ha_check_opt() {}                        /* Remove gcc warning */
61 by Brian Aker
Conversion of handler type.
866
  uint32_t sort_buffer_size;
1 by brian
clean slate
867
  uint flags;       /* isam layer flags (e.g. for myisamchk) */
868
  uint sql_flags;   /* sql layer flags - for something myisamchk cannot do */
869
  KEY_CACHE *key_cache;	/* new key cache when changing key cache */
870
  void init();
871
} HA_CHECK_OPT;
872
873
874
875
/*
876
  This is a buffer area that the handler can use to store rows.
877
  'end_of_used_area' should be kept updated after calls to
878
  read-functions so that other parts of the code can use the
879
  remaining area (until next read calls is issued).
880
*/
881
882
typedef struct st_handler_buffer
883
{
884
  uchar *buffer;         /* Buffer one can start using */
885
  uchar *buffer_end;     /* End of buffer */
886
  uchar *end_of_used_area;     /* End of area that was used by handler */
887
} HANDLER_BUFFER;
888
889
typedef struct system_status_var SSV;
890
891
892
typedef void *range_seq_t;
893
894
typedef struct st_range_seq_if
895
{
896
  /*
897
    Initialize the traversal of range sequence
898
    
899
    SYNOPSIS
900
      init()
901
        init_params  The seq_init_param parameter 
902
        n_ranges     The number of ranges obtained 
903
        flags        A combination of HA_MRR_SINGLE_POINT, HA_MRR_FIXED_KEY
904
905
    RETURN
906
      An opaque value to be used as RANGE_SEQ_IF::next() parameter
907
  */
908
  range_seq_t (*init)(void *init_params, uint n_ranges, uint flags);
909
910
911
  /*
912
    Get the next range in the range sequence
913
914
    SYNOPSIS
915
      next()
916
        seq    The value returned by RANGE_SEQ_IF::init()
917
        range  OUT Information about the next range
918
    
919
    RETURN
920
      0 - Ok, the range structure filled with info about the next range
921
      1 - No more ranges
922
  */
923
  uint (*next) (range_seq_t seq, KEY_MULTI_RANGE *range);
924
} RANGE_SEQ_IF;
925
206 by Brian Aker
Removed final uint dead types.
926
uint16_t &mrr_persistent_flag_storage(range_seq_t seq, uint idx);
1 by brian
clean slate
927
char* &mrr_get_ptr_by_idx(range_seq_t seq, uint idx);
928
929
class COST_VECT
930
{ 
931
public:
932
  double io_count;     /* number of I/O                 */
933
  double avg_io_cost;  /* cost of an average I/O oper.  */
934
  double cpu_cost;     /* cost of operations in CPU     */
935
  double mem_cost;     /* cost of used memory           */ 
936
  double import_cost;  /* cost of remote operations     */
937
  
938
  enum { IO_COEFF=1 };
939
  enum { CPU_COEFF=1 };
940
  enum { MEM_COEFF=1 };
941
  enum { IMPORT_COEFF=1 };
942
943
  COST_VECT() {}                              // keep gcc happy
944
945
  double total_cost() 
946
  {
947
    return IO_COEFF*io_count*avg_io_cost + CPU_COEFF * cpu_cost +
948
           MEM_COEFF*mem_cost + IMPORT_COEFF*import_cost;
949
  }
950
951
  void zero()
952
  {
953
    avg_io_cost= 1.0;
954
    io_count= cpu_cost= mem_cost= import_cost= 0.0;
955
  }
956
957
  void multiply(double m)
958
  {
959
    io_count *= m;
960
    cpu_cost *= m;
961
    import_cost *= m;
962
    /* Don't multiply mem_cost */
963
  }
964
965
  void add(const COST_VECT* cost)
966
  {
967
    double io_count_sum= io_count + cost->io_count;
968
    add_io(cost->io_count, cost->avg_io_cost);
969
    io_count= io_count_sum;
970
    cpu_cost += cost->cpu_cost;
971
  }
972
  void add_io(double add_io_cnt, double add_avg_cost)
973
  {
974
    double io_count_sum= io_count + add_io_cnt;
975
    avg_io_cost= (io_count * avg_io_cost + 
976
                  add_io_cnt * add_avg_cost) / io_count_sum;
977
    io_count= io_count_sum;
978
  }
979
};
980
981
void get_sweep_read_cost(TABLE *table, ha_rows nrows, bool interrupted, 
982
                         COST_VECT *cost);
983
984
/*
985
  The below two are not used (and not handled) in this milestone of this WL
986
  entry because there seems to be no use for them at this stage of
987
  implementation.
988
*/
989
#define HA_MRR_SINGLE_POINT 1
990
#define HA_MRR_FIXED_KEY  2
991
992
/* 
993
  Indicates that RANGE_SEQ_IF::next(&range) doesn't need to fill in the
994
  'range' parameter.
995
*/
996
#define HA_MRR_NO_ASSOCIATION 4
997
998
/* 
999
  The MRR user will provide ranges in key order, and MRR implementation
1000
  must return rows in key order.
1001
*/
1002
#define HA_MRR_SORTED 8
1003
1004
/* MRR implementation doesn't have to retrieve full records */
1005
#define HA_MRR_INDEX_ONLY 16
1006
1007
/* 
1008
  The passed memory buffer is of maximum possible size, the caller can't
1009
  assume larger buffer.
1010
*/
1011
#define HA_MRR_LIMITS 32
1012
1013
1014
/*
1015
  Flag set <=> default MRR implementation is used
1016
  (The choice is made by **_info[_const]() function which may set this
1017
   flag. SQL layer remembers the flag value and then passes it to
1018
   multi_read_range_init().
1019
*/
1020
#define HA_MRR_USE_DEFAULT_IMPL 64
1021
1022
/*
1023
  Used only as parameter to multi_range_read_info():
1024
  Flag set <=> the caller guarantees that the bounds of the scanned ranges
1025
  will not have NULL values.
1026
*/
1027
#define HA_MRR_NO_NULL_ENDPOINTS 128
1028
1029
1030
class ha_statistics
1031
{
1032
public:
1033
  uint64_t data_file_length;		/* Length off data file */
1034
  uint64_t max_data_file_length;	/* Length off data file */
1035
  uint64_t index_file_length;
1036
  uint64_t max_index_file_length;
1037
  uint64_t delete_length;		/* Free bytes */
1038
  uint64_t auto_increment_value;
1039
  /*
1040
    The number of records in the table. 
1041
      0    - means the table has exactly 0 rows
1042
    other  - if (table_flags() & HA_STATS_RECORDS_IS_EXACT)
1043
               the value is the exact number of records in the table
1044
             else
1045
               it is an estimate
1046
  */
1047
  ha_rows records;
1048
  ha_rows deleted;			/* Deleted records */
61 by Brian Aker
Conversion of handler type.
1049
  uint32_t mean_rec_length;		/* physical reclength */
1 by brian
clean slate
1050
  time_t create_time;			/* When table was created */
1051
  time_t check_time;
1052
  time_t update_time;
1053
  uint block_size;			/* index block size */
1054
1055
  ha_statistics():
1056
    data_file_length(0), max_data_file_length(0),
1057
    index_file_length(0), delete_length(0), auto_increment_value(0),
1058
    records(0), deleted(0), mean_rec_length(0), create_time(0),
1059
    check_time(0), update_time(0), block_size(0)
1060
  {}
1061
};
1062
1063
uint calculate_key_len(TABLE *, uint, const uchar *, key_part_map);
1064
/*
1065
  bitmap with first N+1 bits set
1066
  (keypart_map for a key prefix of [0..N] keyparts)
1067
*/
1068
#define make_keypart_map(N) (((key_part_map)2 << (N)) - 1)
1069
/*
1070
  bitmap with first N bits set
1071
  (keypart_map for a key prefix of [0..N-1] keyparts)
1072
*/
1073
#define make_prev_keypart_map(N) (((key_part_map)1 << (N)) - 1)
1074
1075
/**
1076
  The handler class is the interface for dynamically loadable
1077
  storage engines. Do not add ifdefs and take care when adding or
1078
  changing virtual functions to avoid vtable confusion
1079
1080
  Functions in this class accept and return table columns data. Two data
1081
  representation formats are used:
1082
  1. TableRecordFormat - Used to pass [partial] table records to/from
1083
     storage engine
1084
1085
  2. KeyTupleFormat - used to pass index search tuples (aka "keys") to
1086
     storage engine. See opt_range.cc for description of this format.
1087
1088
  TableRecordFormat
1089
  =================
1090
  [Warning: this description is work in progress and may be incomplete]
1091
  The table record is stored in a fixed-size buffer:
1092
   
1093
    record: null_bytes, column1_data, column2_data, ...
1094
  
1095
  The offsets of the parts of the buffer are also fixed: every column has 
1096
  an offset to its column{i}_data, and if it is nullable it also has its own
1097
  bit in null_bytes. 
1098
1099
  The record buffer only includes data about columns that are marked in the
1100
  relevant column set (table->read_set and/or table->write_set, depending on
1101
  the situation). 
1102
  <not-sure>It could be that it is required that null bits of non-present
1103
  columns are set to 1</not-sure>
1104
1105
  VARIOUS EXCEPTIONS AND SPECIAL CASES
1106
1107
  f the table has no nullable columns, then null_bytes is still 
1108
  present, its length is one byte <not-sure> which must be set to 0xFF 
1109
  at all times. </not-sure>
1110
  
1111
  If the table has columns of type BIT, then certain bits from those columns
1112
  may be stored in null_bytes as well. Grep around for Field_bit for
1113
  details.
1114
1115
  For blob columns (see Field_blob), the record buffer stores length of the 
1116
  data, following by memory pointer to the blob data. The pointer is owned 
1117
  by the storage engine and is valid until the next operation.
1118
1119
  If a blob column has NULL value, then its length and blob data pointer
1120
  must be set to 0.
1121
*/
1122
1123
class handler :public Sql_alloc
1124
{
1125
public:
1126
  typedef uint64_t Table_flags;
1127
protected:
1128
  struct st_table_share *table_share;   /* The table definition */
1129
  struct st_table *table;               /* The current open table */
1130
  Table_flags cached_table_flags;       /* Set on init() and open() */
1131
1132
  ha_rows estimation_rows_to_insert;
1133
public:
1134
  handlerton *ht;                 /* storage engine of this handler */
1135
  uchar *ref;				/* Pointer to current row */
1136
  uchar *dup_ref;			/* Pointer to duplicate row */
1137
1138
  ha_statistics stats;
1139
  /** MultiRangeRead-related members: */
1140
  range_seq_t mrr_iter;    /* Interator to traverse the range sequence */
1141
  RANGE_SEQ_IF mrr_funcs;  /* Range sequence traversal functions */
1142
  HANDLER_BUFFER *multi_range_buffer; /* MRR buffer info */
1143
  uint ranges_in_seq; /* Total number of ranges in the traversed sequence */
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1144
  /* true <=> source MRR ranges and the output are ordered */
1 by brian
clean slate
1145
  bool mrr_is_output_sorted;
1146
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1147
  /** true <=> we're currently traversing a range in mrr_cur_range. */
1 by brian
clean slate
1148
  bool mrr_have_range;
1149
  /** Current range (the one we're now returning rows from) */
1150
  KEY_MULTI_RANGE mrr_cur_range;
1151
1152
  /** The following are for read_range() */
1153
  key_range save_end_range, *end_range;
1154
  KEY_PART_INFO *range_key_part;
1155
  int key_compare_result_on_equal;
1156
  bool eq_range;
1157
  /* 
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1158
    true <=> the engine guarantees that returned records are within the range
1 by brian
clean slate
1159
    being scanned.
1160
  */
1161
  bool in_range_check_pushed_down;
1162
1163
  uint errkey;				/* Last dup key */
1164
  uint key_used_on_scan;
1165
  uint active_index;
1166
  /** Length of ref (1-8 or the clustered key length) */
1167
  uint ref_length;
1168
  enum {NONE=0, INDEX, RND} inited;
1169
  bool locked;
1170
  bool implicit_emptied;                /* Can be !=0 only if HEAP */
1171
  const Item *pushed_cond;
1172
1173
  Item *pushed_idx_cond;
1174
  uint pushed_idx_cond_keyno;  /* The index which the above condition is for */
1175
1176
  /**
1177
    next_insert_id is the next value which should be inserted into the
1178
    auto_increment column: in a inserting-multi-row statement (like INSERT
1179
    SELECT), for the first row where the autoinc value is not specified by the
1180
    statement, get_auto_increment() called and asked to generate a value,
1181
    next_insert_id is set to the next value, then for all other rows
1182
    next_insert_id is used (and increased each time) without calling
1183
    get_auto_increment().
1184
  */
1185
  uint64_t next_insert_id;
1186
  /**
1187
    insert id for the current row (*autogenerated*; if not
1188
    autogenerated, it's 0).
1189
    At first successful insertion, this variable is stored into
1190
    THD::first_successful_insert_id_in_cur_stmt.
1191
  */
1192
  uint64_t insert_id_for_cur_row;
1193
  /**
1194
    Interval returned by get_auto_increment() and being consumed by the
1195
    inserter.
1196
  */
1197
  Discrete_interval auto_inc_interval_for_cur_row;
1198
1199
  handler(handlerton *ht_arg, TABLE_SHARE *share_arg)
1200
    :table_share(share_arg), table(0),
1201
    estimation_rows_to_insert(0), ht(ht_arg),
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1202
    ref(0), in_range_check_pushed_down(false),
1 by brian
clean slate
1203
    key_used_on_scan(MAX_KEY), active_index(MAX_KEY),
1204
    ref_length(sizeof(my_off_t)),
137 by Brian Aker
Removed dead FT bits. Small refactoring in sql_plugin.cc
1205
    inited(NONE),
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1206
    locked(false), implicit_emptied(0),
1 by brian
clean slate
1207
    pushed_cond(0), pushed_idx_cond(NULL), pushed_idx_cond_keyno(MAX_KEY),
1208
    next_insert_id(0), insert_id_for_cur_row(0)
1209
    {}
1210
  virtual ~handler(void)
1211
  {
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1212
    assert(locked == false);
1213
    /* TODO: assert(inited == NONE); */
1 by brian
clean slate
1214
  }
1215
  virtual handler *clone(MEM_ROOT *mem_root);
1216
  /** This is called after create to allow us to set up cached variables */
1217
  void init()
1218
  {
1219
    cached_table_flags= table_flags();
1220
  }
1221
  /* ha_ methods: pubilc wrappers for private virtual API */
1222
1223
  int ha_open(TABLE *table, const char *name, int mode, int test_if_locked);
1224
  int ha_index_init(uint idx, bool sorted)
1225
  {
1226
    int result;
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1227
    assert(inited==NONE);
1 by brian
clean slate
1228
    if (!(result= index_init(idx, sorted)))
1229
      inited=INDEX;
1230
    end_range= NULL;
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1231
    return(result);
1 by brian
clean slate
1232
  }
1233
  int ha_index_end()
1234
  {
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1235
    assert(inited==INDEX);
1 by brian
clean slate
1236
    inited=NONE;
1237
    end_range= NULL;
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1238
    return(index_end());
1 by brian
clean slate
1239
  }
1240
  int ha_rnd_init(bool scan)
1241
  {
1242
    int result;
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1243
    assert(inited==NONE || (inited==RND && scan));
1 by brian
clean slate
1244
    inited= (result= rnd_init(scan)) ? NONE: RND;
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1245
    return(result);
1 by brian
clean slate
1246
  }
1247
  int ha_rnd_end()
1248
  {
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1249
    assert(inited==RND);
1 by brian
clean slate
1250
    inited=NONE;
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1251
    return(rnd_end());
1 by brian
clean slate
1252
  }
1253
  int ha_reset();
1254
  /* this is necessary in many places, e.g. in HANDLER command */
1255
  int ha_index_or_rnd_end()
1256
  {
1257
    return inited == INDEX ? ha_index_end() : inited == RND ? ha_rnd_end() : 0;
1258
  }
1259
  Table_flags ha_table_flags() const { return cached_table_flags; }
1260
  /**
1261
    These functions represent the public interface to *users* of the
1262
    handler class, hence they are *not* virtual. For the inheritance
1263
    interface, see the (private) functions write_row(), update_row(),
1264
    and delete_row() below.
1265
  */
1266
  int ha_external_lock(THD *thd, int lock_type);
1267
  int ha_write_row(uchar * buf);
1268
  int ha_update_row(const uchar * old_data, uchar * new_data);
1269
  int ha_delete_row(const uchar * buf);
1270
  void ha_release_auto_increment();
1271
1272
  int ha_check_for_upgrade(HA_CHECK_OPT *check_opt);
1273
  /** to be actually called to get 'check()' functionality*/
1274
  int ha_check(THD *thd, HA_CHECK_OPT *check_opt);
1275
  int ha_repair(THD* thd, HA_CHECK_OPT* check_opt);
1276
  void ha_start_bulk_insert(ha_rows rows)
1277
  {
1278
    estimation_rows_to_insert= rows;
1279
    start_bulk_insert(rows);
1280
  }
1281
  int ha_end_bulk_insert()
1282
  {
1283
    estimation_rows_to_insert= 0;
1284
    return end_bulk_insert();
1285
  }
1286
  int ha_bulk_update_row(const uchar *old_data, uchar *new_data,
1287
                         uint *dup_key_found);
1288
  int ha_delete_all_rows();
1289
  int ha_reset_auto_increment(uint64_t value);
1290
  int ha_optimize(THD* thd, HA_CHECK_OPT* check_opt);
1291
  int ha_analyze(THD* thd, HA_CHECK_OPT* check_opt);
1292
  bool ha_check_and_repair(THD *thd);
1293
  int ha_disable_indexes(uint mode);
1294
  int ha_enable_indexes(uint mode);
200 by Brian Aker
my_bool from handler and set_var
1295
  int ha_discard_or_import_tablespace(bool discard);
1 by brian
clean slate
1296
  void ha_prepare_for_alter();
1297
  int ha_rename_table(const char *from, const char *to);
1298
  int ha_delete_table(const char *name);
1299
  void ha_drop_table(const char *name);
1300
1301
  int ha_create(const char *name, TABLE *form, HA_CREATE_INFO *info);
1302
1303
  int ha_create_handler_files(const char *name, const char *old_name,
1304
                              int action_flag, HA_CREATE_INFO *info);
1305
1306
  void adjust_next_insert_id_after_explicit_value(uint64_t nr);
1307
  int update_auto_increment();
1308
  void print_keydup_error(uint key_nr, const char *msg);
1309
  virtual void print_error(int error, myf errflag);
1310
  virtual bool get_error_message(int error, String *buf);
1311
  uint get_dup_key(int error);
1312
  virtual void change_table_ptr(TABLE *table_arg, TABLE_SHARE *share)
1313
  {
1314
    table= table_arg;
1315
    table_share= share;
1316
  }
1317
  /* Estimates calculation */
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1318
  virtual double scan_time(void)
151 by Brian Aker
Ulonglong to uint64_t
1319
  { return uint64_t2double(stats.data_file_length) / IO_SIZE + 2; }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1320
  virtual double read_time(uint index __attribute__((unused)),
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1321
                           uint ranges, ha_rows rows)
1 by brian
clean slate
1322
  { return rows2double(ranges+rows); }
1323
1324
  virtual double index_only_read_time(uint keynr, double records);
1325
  
1326
  virtual ha_rows multi_range_read_info_const(uint keyno, RANGE_SEQ_IF *seq,
1327
                                              void *seq_init_param, 
1328
                                              uint n_ranges, uint *bufsz,
1329
                                              uint *flags, COST_VECT *cost);
1330
  virtual int multi_range_read_info(uint keyno, uint n_ranges, uint keys,
1331
                                    uint *bufsz, uint *flags, COST_VECT *cost);
1332
  virtual int multi_range_read_init(RANGE_SEQ_IF *seq, void *seq_init_param,
1333
                                    uint n_ranges, uint mode,
1334
                                    HANDLER_BUFFER *buf);
1335
  virtual int multi_range_read_next(char **range_info);
1336
1337
1338
  virtual const key_map *keys_to_use_for_scanning() { return &key_map_empty; }
1339
  bool has_transactions()
1340
  { return (ha_table_flags() & HA_NO_TRANSACTIONS) == 0; }
1341
  virtual uint extra_rec_buf_length() const { return 0; }
1342
1343
  /**
1344
    This method is used to analyse the error to see whether the error
1345
    is ignorable or not, certain handlers can have more error that are
1346
    ignorable than others. E.g. the partition handler can get inserts
1347
    into a range where there is no partition and this is an ignorable
1348
    error.
1349
    HA_ERR_FOUND_DUP_UNIQUE is a special case in MyISAM that means the
1350
    same thing as HA_ERR_FOUND_DUP_KEY but can in some cases lead to
1351
    a slightly different error message.
1352
  */
1353
  virtual bool is_fatal_error(int error, uint flags)
1354
  {
1355
    if (!error ||
1356
        ((flags & HA_CHECK_DUP_KEY) &&
1357
         (error == HA_ERR_FOUND_DUPP_KEY ||
1358
          error == HA_ERR_FOUND_DUPP_UNIQUE)))
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1359
      return false;
1360
    return true;
1 by brian
clean slate
1361
  }
1362
1363
  /**
1364
    Number of rows in table. It will only be called if
1365
    (table_flags() & (HA_HAS_RECORDS | HA_STATS_RECORDS_IS_EXACT)) != 0
1366
  */
1367
  virtual ha_rows records() { return stats.records; }
1368
  /**
1369
    Return upper bound of current number of records in the table
1370
    (max. of how many records one will retrieve when doing a full table scan)
1371
    If upper bound is not known, HA_POS_ERROR should be returned as a max
1372
    possible upper bound.
1373
  */
1374
  virtual ha_rows estimate_rows_upper_bound()
1375
  { return stats.records+EXTRA_RECORDS; }
1376
1377
  /**
1378
    Get the row type from the storage engine.  If this method returns
1379
    ROW_TYPE_NOT_USED, the information in HA_CREATE_INFO should be used.
1380
  */
1381
  virtual enum row_type get_row_type() const { return ROW_TYPE_NOT_USED; }
1382
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1383
  virtual const char *index_type(uint key_number __attribute__((unused)))
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1384
  { assert(0); return "";}
1 by brian
clean slate
1385
1386
1387
  /**
1388
    Signal that the table->read_set and table->write_set table maps changed
1389
    The handler is allowed to set additional bits in the above map in this
1390
    call. Normally the handler should ignore all calls until we have done
1391
    a ha_rnd_init() or ha_index_init(), write_row(), update_row or delete_row()
1392
    as there may be several calls to this routine.
1393
  */
1394
  virtual void column_bitmaps_signal();
1395
  uint get_index(void) const { return active_index; }
1396
  virtual int close(void)=0;
1397
1398
  /**
1399
    @retval  0   Bulk update used by handler
1400
    @retval  1   Bulk update not used, normal operation used
1401
  */
1402
  virtual bool start_bulk_update() { return 1; }
1403
  /**
1404
    @retval  0   Bulk delete used by handler
1405
    @retval  1   Bulk delete not used, normal operation used
1406
  */
1407
  virtual bool start_bulk_delete() { return 1; }
1408
  /**
1409
    After this call all outstanding updates must be performed. The number
1410
    of duplicate key errors are reported in the duplicate key parameter.
1411
    It is allowed to continue to the batched update after this call, the
1412
    handler has to wait until end_bulk_update with changing state.
1413
1414
    @param    dup_key_found       Number of duplicate keys found
1415
1416
    @retval  0           Success
1417
    @retval  >0          Error code
1418
  */
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1419
  virtual int exec_bulk_update(uint *dup_key_found __attribute__((unused)))
1 by brian
clean slate
1420
  {
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1421
    assert(false);
1 by brian
clean slate
1422
    return HA_ERR_WRONG_COMMAND;
1423
  }
1424
  /**
1425
    Perform any needed clean-up, no outstanding updates are there at the
1426
    moment.
1427
  */
1428
  virtual void end_bulk_update() { return; }
1429
  /**
1430
    Execute all outstanding deletes and close down the bulk delete.
1431
1432
    @retval 0             Success
1433
    @retval >0            Error code
1434
  */
1435
  virtual int end_bulk_delete()
1436
  {
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1437
    assert(false);
1 by brian
clean slate
1438
    return HA_ERR_WRONG_COMMAND;
1439
  }
1440
  /**
1441
     @brief
1442
     Positions an index cursor to the index specified in the handle. Fetches the
1443
     row if available. If the key value is null, begin at the first key of the
1444
     index.
1445
  */
1446
  virtual int index_read_map(uchar * buf, const uchar * key,
1447
                             key_part_map keypart_map,
1448
                             enum ha_rkey_function find_flag)
1449
  {
1450
    uint key_len= calculate_key_len(table, active_index, key, keypart_map);
1451
    return  index_read(buf, key, key_len, find_flag);
1452
  }
1453
  /**
1454
     @brief
1455
     Positions an index cursor to the index specified in the handle. Fetches the
1456
     row if available. If the key value is null, begin at the first key of the
1457
     index.
1458
  */
1459
  virtual int index_read_idx_map(uchar * buf, uint index, const uchar * key,
1460
                                 key_part_map keypart_map,
1461
                                 enum ha_rkey_function find_flag);
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1462
  virtual int index_next(uchar * buf __attribute__((unused)))
1463
   { return  HA_ERR_WRONG_COMMAND; }
1464
  virtual int index_prev(uchar * buf __attribute__((unused)))
1465
   { return  HA_ERR_WRONG_COMMAND; }
1466
  virtual int index_first(uchar * buf __attribute__((unused)))
1467
   { return  HA_ERR_WRONG_COMMAND; }
1468
  virtual int index_last(uchar * buf __attribute__((unused)))
1469
   { return  HA_ERR_WRONG_COMMAND; }
1470
  virtual int index_next_same(uchar *buf __attribute__((unused)),
1471
                              const uchar *key __attribute__((unused)),
1472
                              uint keylen __attribute__((unused)));
1 by brian
clean slate
1473
  /**
1474
     @brief
1475
     The following functions works like index_read, but it find the last
1476
     row with the current key value or prefix.
1477
  */
1478
  virtual int index_read_last_map(uchar * buf, const uchar * key,
1479
                                  key_part_map keypart_map)
1480
  {
1481
    uint key_len= calculate_key_len(table, active_index, key, keypart_map);
1482
    return index_read_last(buf, key, key_len);
1483
  }
1484
  virtual int read_range_first(const key_range *start_key,
1485
                               const key_range *end_key,
1486
                               bool eq_range, bool sorted);
1487
  virtual int read_range_next();
1488
  int compare_key(key_range *range);
1489
  int compare_key2(key_range *range);
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1490
  virtual int rnd_next(uchar *buf __attribute__((unused)))=0;
1491
  virtual int rnd_pos(uchar * buf __attribute__((unused)),
1492
                      uchar *pos __attribute__((unused)))=0;
1 by brian
clean slate
1493
  /**
1494
    One has to use this method when to find
1495
    random position by record as the plain
1496
    position() call doesn't work for some
1497
    handlers for random position.
1498
  */
1499
  virtual int rnd_pos_by_record(uchar *record);
1500
  virtual int read_first_row(uchar *buf, uint primary_key);
1501
  /**
1502
    The following function is only needed for tables that may be temporary
1503
    tables during joins.
1504
  */
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1505
  virtual int restart_rnd_next(uchar *buf __attribute__((unused)),
1506
                               uchar *pos __attribute__((unused)))
1507
    { return HA_ERR_WRONG_COMMAND; }
1508
  virtual int rnd_same(uchar *buf __attribute__((unused)),
1509
                       uint inx __attribute__((unused)))
1510
    { return HA_ERR_WRONG_COMMAND; }
1511
  virtual ha_rows records_in_range(uint inx __attribute__((unused)),
1512
                                   key_range *min_key __attribute__((unused)),
1513
                                   key_range *max_key __attribute__((unused)))
1 by brian
clean slate
1514
    { return (ha_rows) 10; }
1515
  virtual void position(const uchar *record)=0;
1516
  virtual int info(uint)=0; // see my_base.h for full description
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1517
  virtual uint32_t calculate_key_hash_value(Field **field_array __attribute__((unused)))
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1518
  { assert(0); return 0; }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1519
  virtual int extra(enum ha_extra_function operation __attribute__((unused)))
1 by brian
clean slate
1520
  { return 0; }
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1521
  virtual int extra_opt(enum ha_extra_function operation,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1522
                        uint32_t cache_size __attribute__((unused)))
1 by brian
clean slate
1523
  { return extra(operation); }
1524
1525
  /**
1526
    In an UPDATE or DELETE, if the row under the cursor was locked by another
1527
    transaction, and the engine used an optimistic read of the last
1528
    committed row value under the cursor, then the engine returns 1 from this
1529
    function. MySQL must NOT try to update this optimistic value. If the
1530
    optimistic value does not match the WHERE condition, MySQL can decide to
1531
    skip over this row. Currently only works for InnoDB. This can be used to
1532
    avoid unnecessary lock waits.
1533
1534
    If this method returns nonzero, it will also signal the storage
1535
    engine that the next read will be a locking re-read of the row.
1536
  */
1537
  virtual bool was_semi_consistent_read() { return 0; }
1538
  /**
1539
    Tell the engine whether it should avoid unnecessary lock waits.
1540
    If yes, in an UPDATE or DELETE, if the row under the cursor was locked
1541
    by another transaction, the engine may try an optimistic read of
1542
    the last committed row value under the cursor.
1543
  */
1544
  virtual void try_semi_consistent_read(bool) {}
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1545
  virtual void unlock_row(void) {}
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1546
  virtual int start_stmt(THD *thd __attribute__((unused)),
1547
                         thr_lock_type lock_type __attribute__((unused)))
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1548
  {return 0;}
1 by brian
clean slate
1549
  virtual void get_auto_increment(uint64_t offset, uint64_t increment,
1550
                                  uint64_t nb_desired_values,
1551
                                  uint64_t *first_value,
1552
                                  uint64_t *nb_reserved_values);
1553
  void set_next_insert_id(uint64_t id)
1554
  {
1555
    next_insert_id= id;
1556
  }
1557
  void restore_auto_increment(uint64_t prev_insert_id)
1558
  {
1559
    /*
1560
      Insertion of a row failed, re-use the lastly generated auto_increment
1561
      id, for the next row. This is achieved by resetting next_insert_id to
1562
      what it was before the failed insertion (that old value is provided by
1563
      the caller). If that value was 0, it was the first row of the INSERT;
1564
      then if insert_id_for_cur_row contains 0 it means no id was generated
1565
      for this first row, so no id was generated since the INSERT started, so
1566
      we should set next_insert_id to 0; if insert_id_for_cur_row is not 0, it
1567
      is the generated id of the first and failed row, so we use it.
1568
    */
1569
    next_insert_id= (prev_insert_id > 0) ? prev_insert_id :
1570
      insert_id_for_cur_row;
1571
  }
1572
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1573
  virtual void update_create_info(HA_CREATE_INFO *create_info __attribute__((unused))) {}
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1574
  int check_old_types(void);
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1575
  virtual int assign_to_keycache(THD* thd __attribute__((unused)),
1576
                                 HA_CHECK_OPT* check_opt __attribute__((unused)))
1 by brian
clean slate
1577
  { return HA_ADMIN_NOT_IMPLEMENTED; }
1578
  /* end of the list of admin commands */
1579
1580
  virtual int indexes_are_disabled(void) {return 0;}
1581
  virtual char *update_table_comment(const char * comment)
1582
  { return (char*) comment;}
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1583
  virtual void append_create_info(String *packet __attribute__((unused)))
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1584
  {}
1 by brian
clean slate
1585
  /**
1586
      If index == MAX_KEY then a check for table is made and if index <
1587
      MAX_KEY then a check is made if the table has foreign keys and if
1588
      a foreign key uses this index (and thus the index cannot be dropped).
1589
1590
    @param  index            Index to check if foreign key uses it
1591
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1592
    @retval   true            Foreign key defined on table or index
1593
    @retval   false           No foreign key defined
1 by brian
clean slate
1594
  */
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1595
  virtual bool is_fk_defined_on_table_or_index(uint index __attribute__((unused)))
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1596
  { return false; }
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1597
  virtual char* get_foreign_key_create_info(void)
1 by brian
clean slate
1598
  { return(NULL);}  /* gets foreign key create string from InnoDB */
1599
  /** used in ALTER TABLE; 1 if changing storage engine is allowed */
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1600
  virtual bool can_switch_engines(void) { return 1; }
1 by brian
clean slate
1601
  /** used in REPLACE; is > 0 if table is referred by a FOREIGN KEY */
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1602
  virtual int get_foreign_key_list(THD *thd __attribute__((unused)),
1603
                                   List<FOREIGN_KEY_INFO> *f_key_list __attribute__((unused)))
1 by brian
clean slate
1604
  { return 0; }
1605
  virtual uint referenced_by_foreign_key() { return 0;}
1606
  virtual void init_table_handle_for_HANDLER()
1607
  { return; }       /* prepare InnoDB for HANDLER */
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1608
  virtual void free_foreign_key_create_info(char* str __attribute__((unused))) {}
1 by brian
clean slate
1609
  /** The following can be called without an open handler */
1610
  virtual const char *table_type() const =0;
1611
  /**
1612
    If frm_error() is called then we will use this to find out what file
1613
    extentions exist for the storage engine. This is also used by the default
1614
    rename_table and delete_table method in handler.cc.
1615
1616
    For engines that have two file name extentions (separate meta/index file
1617
    and data file), the order of elements is relevant. First element of engine
1618
    file name extentions array should be meta/index file extention. Second
1619
    element - data file extention. This order is assumed by
1620
    prepare_for_repair() when REPAIR TABLE ... USE_FRM is issued.
1621
  */
1622
  virtual const char **bas_ext() const =0;
1623
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1624
  virtual int get_default_no_partitions(HA_CREATE_INFO *info __attribute__((unused))) { return 1;}
1625
  virtual bool get_no_parts(const char *name __attribute__((unused)),
1 by brian
clean slate
1626
                            uint *no_parts)
1627
  {
1628
    *no_parts= 0;
1629
    return 0;
1630
  }
1631
61 by Brian Aker
Conversion of handler type.
1632
  virtual uint32_t index_flags(uint idx, uint part, bool all_parts) const =0;
1 by brian
clean slate
1633
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1634
  virtual int add_index(TABLE *table_arg __attribute__((unused)),
1635
                        KEY *key_info __attribute__((unused)),
1636
                        uint num_of_keys __attribute__((unused)))
1637
  { return (HA_ERR_WRONG_COMMAND); }
1638
  virtual int prepare_drop_index(TABLE *table_arg __attribute__((unused)),
1639
                                 uint *key_num __attribute__((unused)),
1640
                                 uint num_of_keys __attribute__((unused)))
1641
  { return (HA_ERR_WRONG_COMMAND); }
1642
  virtual int final_drop_index(TABLE *table_arg __attribute__((unused)))
1 by brian
clean slate
1643
  { return (HA_ERR_WRONG_COMMAND); }
1644
1645
  uint max_record_length() const
1646
  { return min(HA_MAX_REC_LENGTH, max_supported_record_length()); }
1647
  uint max_keys() const
1648
  { return min(MAX_KEY, max_supported_keys()); }
1649
  uint max_key_parts() const
1650
  { return min(MAX_REF_PARTS, max_supported_key_parts()); }
1651
  uint max_key_length() const
1652
  { return min(MAX_KEY_LENGTH, max_supported_key_length()); }
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1653
  uint max_key_part_length(void) const
1 by brian
clean slate
1654
  { return min(MAX_KEY_LENGTH, max_supported_key_part_length()); }
1655
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1656
  virtual uint max_supported_record_length(void) const
1657
  { return HA_MAX_REC_LENGTH; }
1658
  virtual uint max_supported_keys(void) const { return 0; }
1659
  virtual uint max_supported_key_parts(void) const { return MAX_REF_PARTS; }
1660
  virtual uint max_supported_key_length(void) const { return MAX_KEY_LENGTH; }
1661
  virtual uint max_supported_key_part_length(void) const { return 255; }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1662
  virtual uint min_record_length(uint options __attribute__((unused))) const
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1663
  { return 1; }
1 by brian
clean slate
1664
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1665
  virtual bool low_byte_first(void) const { return 1; }
1666
  virtual uint checksum(void) const { return 0; }
1667
  virtual bool is_crashed(void) const  { return 0; }
1668
  virtual bool auto_repair(void) const { return 0; }
1 by brian
clean slate
1669
1670
1671
#define CHF_CREATE_FLAG 0
1672
#define CHF_DELETE_FLAG 1
1673
#define CHF_RENAME_FLAG 2
1674
1675
1676
  /**
1677
    @note lock_count() can return > 1 if the table is MERGE or partitioned.
1678
  */
1679
  virtual uint lock_count(void) const { return 1; }
1680
  /**
1681
    Is not invoked for non-transactional temporary tables.
1682
1683
    @note store_lock() can return more than one lock if the table is MERGE
1684
    or partitioned.
1685
1686
    @note that one can NOT rely on table->in_use in store_lock().  It may
1687
    refer to a different thread if called from mysql_lock_abort_for_thread().
1688
1689
    @note If the table is MERGE, store_lock() can return less locks
1690
    than lock_count() claimed. This can happen when the MERGE children
1691
    are not attached when this is called from another thread.
1692
  */
1693
  virtual THR_LOCK_DATA **store_lock(THD *thd,
1694
                                     THR_LOCK_DATA **to,
1695
                                     enum thr_lock_type lock_type)=0;
1696
1697
  /** Type of table for caching query */
206 by Brian Aker
Removed final uint dead types.
1698
  virtual uint8_t table_cache_type() { return HA_CACHE_TBL_NONTRANSACT; }
1 by brian
clean slate
1699
1700
1701
  /**
1702
    @brief Register a named table with a call back function to the query cache.
1703
1704
    @param thd The thread handle
1705
    @param table_key A pointer to the table name in the table cache
1706
    @param key_length The length of the table name
1707
    @param[out] engine_callback The pointer to the storage engine call back
1708
      function
1709
    @param[out] engine_data Storage engine specific data which could be
1710
      anything
1711
1712
    This method offers the storage engine, the possibility to store a reference
1713
    to a table name which is going to be used with query cache. 
1714
    The method is called each time a statement is written to the cache and can
1715
    be used to verify if a specific statement is cachable. It also offers
1716
    the possibility to register a generic (but static) call back function which
1717
    is called each time a statement is matched against the query cache.
1718
1719
    @note If engine_data supplied with this function is different from
1720
      engine_data supplied with the callback function, and the callback returns
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1721
      false, a table invalidation on the current table will occur.
1 by brian
clean slate
1722
1723
    @return Upon success the engine_callback will point to the storage engine
1724
      call back function, if any, and engine_data will point to any storage
1725
      engine data used in the specific implementation.
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1726
      @retval true Success
1727
      @retval false The specified table or current statement should not be
1 by brian
clean slate
1728
        cached
1729
  */
1730
200 by Brian Aker
my_bool from handler and set_var
1731
  virtual bool
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1732
    register_query_cache_table(THD *thd __attribute__((unused)),
1733
                               char *table_key __attribute__((unused)),
1734
                               uint key_length __attribute__((unused)),
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1735
                               qc_engine_callback *engine_callback,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1736
                               uint64_t *engine_data __attribute__((unused)))
1 by brian
clean slate
1737
  {
1738
    *engine_callback= 0;
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1739
    return true;
1 by brian
clean slate
1740
  }
1741
1742
1743
 /*
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1744
   @retval true   Primary key (if there is one) is clustered
1 by brian
clean slate
1745
                  key covering all fields
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1746
   @retval false  otherwise
1 by brian
clean slate
1747
 */
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1748
 virtual bool primary_key_is_clustered() { return false; }
1 by brian
clean slate
1749
 virtual int cmp_ref(const uchar *ref1, const uchar *ref2)
1750
 {
1751
   return memcmp(ref1, ref2, ref_length);
1752
 }
1753
1754
 /*
1755
   Condition pushdown to storage engines
1756
 */
1757
1758
 /**
1759
   Push condition down to the table handler.
1760
1761
   @param  cond   Condition to be pushed. The condition tree must not be
1762
                  modified by the by the caller.
1763
1764
   @return
1765
     The 'remainder' condition that caller must use to filter out records.
1766
     NULL means the handler will not return rows that do not match the
1767
     passed condition.
1768
1769
   @note
1770
   The pushed conditions form a stack (from which one can remove the
1771
   last pushed condition using cond_pop).
1772
   The table handler filters out rows using (pushed_cond1 AND pushed_cond2 
1773
   AND ... AND pushed_condN)
1774
   or less restrictive condition, depending on handler's capabilities.
1775
1776
   handler->ha_reset() call empties the condition stack.
1777
   Calls to rnd_init/rnd_end, index_init/index_end etc do not affect the
1778
   condition stack.
1779
 */ 
1780
 virtual const COND *cond_push(const COND *cond) { return cond; }
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1781
1 by brian
clean slate
1782
 /**
1783
   Pop the top condition from the condition stack of the handler instance.
1784
1785
   Pops the top if condition stack, if stack is not empty.
1786
 */
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1787
 virtual void cond_pop(void) { return; }
1 by brian
clean slate
1788
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1789
 virtual Item
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1790
   *idx_cond_push(uint keyno __attribute__((unused)),
1791
                  Item* idx_cond __attribute__((unused)))
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1792
 { return idx_cond; }
1 by brian
clean slate
1793
1794
 /*
1795
    Part of old fast alter table, to be depricated
1796
  */
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1797
 virtual bool
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1798
   check_if_incompatible_data(HA_CREATE_INFO *create_info __attribute__((unused)),
1799
                              uint table_changes __attribute__((unused)))
1 by brian
clean slate
1800
 { return COMPATIBLE_DATA_NO; }
1801
1802
 /* On-line ALTER TABLE interface */
1803
1804
 /**
1805
    Check if a storage engine supports a particular alter table on-line
1806
1807
    @param    altered_table     A temporary table show what table is to
1808
                                change to
1809
    @param    create_info       Information from the parsing phase about new
1810
                                table properties.
1811
    @param    alter_flags       Bitmask that shows what will be changed
1812
    @param    table_changes     Shows if table layout has changed (for
1813
                                backwards compatibility with
1814
                                check_if_incompatible_data
1815
1816
    @retval   HA_ALTER_ERROR                Unexpected error
1817
    @retval   HA_ALTER_SUPPORTED_WAIT_LOCK  Supported, but requires DDL lock
1818
    @retval   HA_ALTER_SUPPORTED_NO_LOCK    Supported
1819
    @retval   HA_ALTER_NOT_SUPPORTED        Not supported
1820
1821
    @note
1822
      The default implementation is implemented to support fast
1823
      alter table (storage engines that support some changes by
1824
      just changing the frm file) without any change in the handler
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1825
      implementation.
1 by brian
clean slate
1826
 */
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1827
 virtual int
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1828
   check_if_supported_alter(TABLE *altered_table __attribute__((unused)),
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1829
                            HA_CREATE_INFO *create_info,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1830
                            HA_ALTER_FLAGS *alter_flags __attribute__((unused)),
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1831
                            uint table_changes)
1 by brian
clean slate
1832
 {
1833
   if (this->check_if_incompatible_data(create_info, table_changes)
1834
       == COMPATIBLE_DATA_NO)
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1835
     return(HA_ALTER_NOT_SUPPORTED);
1 by brian
clean slate
1836
   else
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1837
     return(HA_ALTER_SUPPORTED_WAIT_LOCK);
1 by brian
clean slate
1838
 }
1839
 /**
1840
   Tell storage engine to prepare for the on-line alter table (pre-alter)
1841
1842
   @param     thd               The thread handle
1843
   @param     altered_table     A temporary table show what table is to
1844
                                change to
1845
   @param     alter_info        Storage place for data used during phase1
1846
                                and phase2
1847
   @param     alter_flags       Bitmask that shows what will be changed
1848
1849
   @retval   0      OK
1850
   @retval   error  error code passed from storage engine
1851
 */
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1852
 virtual int alter_table_phase1(THD *thd __attribute__((unused)),
1853
                                TABLE *altered_table __attribute__((unused)),
1854
                                HA_CREATE_INFO *create_info __attribute__((unused)),
1855
                                HA_ALTER_INFO *alter_info __attribute__((unused)),
1856
                                HA_ALTER_FLAGS *alter_flags  __attribute__((unused)))
1 by brian
clean slate
1857
 {
1858
   return HA_ERR_UNSUPPORTED;
1859
 }
1860
 /**
1861
    Tell storage engine to perform the on-line alter table (alter)
1862
1863
    @param    thd               The thread handle
1864
    @param    altered_table     A temporary table show what table is to
1865
                                change to
1866
    @param    alter_info        Storage place for data used during phase1
1867
                                and phase2
1868
    @param    alter_flags       Bitmask that shows what will be changed
1869
1870
    @retval  0      OK
1871
    @retval  error  error code passed from storage engine
1872
1873
    @note
1874
      If check_if_supported_alter returns HA_ALTER_SUPPORTED_WAIT_LOCK
1875
      this call is to be wrapped with a DDL lock. This is currently NOT
1876
      supported.
1877
 */
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1878
 virtual int alter_table_phase2(THD *thd  __attribute__((unused)),
1879
                                TABLE *altered_table  __attribute__((unused)),
1880
                                HA_CREATE_INFO *create_info __attribute__((unused)),
1881
                                HA_ALTER_INFO *alter_info __attribute__((unused)),
1882
                                HA_ALTER_FLAGS *alter_flags __attribute__((unused)))
1 by brian
clean slate
1883
 {
1884
   return HA_ERR_UNSUPPORTED;
1885
 }
1886
 /**
1887
    Tell storage engine that changed frm file is now on disk and table
1888
    has been re-opened (post-alter)
1889
1890
    @param    thd               The thread handle
1891
    @param    table             The altered table, re-opened
1892
 */
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1893
 virtual int alter_table_phase3(THD *thd __attribute__((unused)),
1894
                                TABLE *table __attribute__((unused)))
1 by brian
clean slate
1895
 {
1896
   return HA_ERR_UNSUPPORTED;
1897
 }
1898
1899
  /**
1900
    use_hidden_primary_key() is called in case of an update/delete when
1901
    (table_flags() and HA_PRIMARY_KEY_REQUIRED_FOR_DELETE) is defined
1902
    but we don't have a primary key
1903
  */
1904
  virtual void use_hidden_primary_key();
1905
1906
  /**
1907
    Lock table.
1908
1909
    @param    thd                     Thread handle
1910
    @param    lock_type               HA_LOCK_IN_SHARE_MODE     (F_RDLCK)
1911
                                      HA_LOCK_IN_EXCLUSIVE_MODE (F_WRLCK)
1912
    @param    lock_timeout            -1 default timeout
1913
                                      0  no wait
1914
                                      >0 wait timeout in milliseconds.
1915
1916
   @note
1917
      lock_timeout >0 is not used by MySQL currently. If the storage
1918
      engine does not support NOWAIT (lock_timeout == 0) it should
1919
      return an error. But if it does not support WAIT X (lock_timeout
1920
      >0) it should treat it as lock_timeout == -1 and wait a default
1921
      (or even hard-coded) timeout.
1922
1923
    @retval HA_ERR_WRONG_COMMAND      Storage engine does not support
1924
                                      lock_table()
1925
    @retval HA_ERR_UNSUPPORTED        Storage engine does not support NOWAIT
1926
    @retval HA_ERR_LOCK_WAIT_TIMEOUT  Lock request timed out or
1927
                                      lock conflict with NOWAIT option
1928
    @retval HA_ERR_LOCK_DEADLOCK      Deadlock detected
1929
  */
1930
  virtual int lock_table(THD *thd         __attribute__((unused)),
1931
                         int lock_type    __attribute__((unused)),
1932
                         int lock_timeout __attribute__((unused)))
1933
  {
1934
    return HA_ERR_WRONG_COMMAND;
1935
  }
1936
1937
protected:
1938
  /* Service methods for use by storage engines. */
1939
  void ha_statistic_increment(ulong SSV::*offset) const;
1940
  void **ha_data(THD *) const;
1941
  THD *ha_thd(void) const;
1942
1943
  /**
1944
    Default rename_table() and delete_table() rename/delete files with a
1945
    given name and extensions from bas_ext().
1946
1947
    These methods can be overridden, but their default implementation
1948
    provide useful functionality.
1949
  */
1950
  virtual int rename_table(const char *from, const char *to);
1951
  /**
1952
    Delete a table in the engine. Called for base as well as temporary
1953
    tables.
1954
  */
1955
  virtual int delete_table(const char *name);
1956
1957
private:
1958
  /* Private helpers */
1959
  inline void mark_trx_read_write();
1960
private:
1961
  /*
1962
    Low-level primitives for storage engines.  These should be
1963
    overridden by the storage engine class. To call these methods, use
1964
    the corresponding 'ha_*' method above.
1965
  */
1966
1967
  virtual int open(const char *name, int mode, uint test_if_locked)=0;
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1968
  virtual int index_init(uint idx,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1969
                         bool sorted __attribute__((unused)))
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1970
  { active_index= idx; return 0; }
1 by brian
clean slate
1971
  virtual int index_end() { active_index= MAX_KEY; return 0; }
1972
  /**
1973
    rnd_init() can be called two times without rnd_end() in between
1974
    (it only makes sense if scan=1).
1975
    then the second call should prepare for the new table scan (e.g
1976
    if rnd_init allocates the cursor, second call should position it
1977
    to the start of the table, no need to deallocate and allocate it again
1978
  */
1979
  virtual int rnd_init(bool scan)= 0;
1980
  virtual int rnd_end() { return 0; }
1981
  virtual int write_row(uchar *buf __attribute__((unused)))
1982
  {
1983
    return HA_ERR_WRONG_COMMAND;
1984
  }
1985
1986
  virtual int update_row(const uchar *old_data __attribute__((unused)),
1987
                         uchar *new_data __attribute__((unused)))
1988
  {
1989
    return HA_ERR_WRONG_COMMAND;
1990
  }
1991
1992
  virtual int delete_row(const uchar *buf __attribute__((unused)))
1993
  {
1994
    return HA_ERR_WRONG_COMMAND;
1995
  }
1996
  /**
1997
    Reset state of file to after 'open'.
1998
    This function is called after every statement for all tables used
1999
    by that statement.
2000
  */
2001
  virtual int reset() { return 0; }
2002
  virtual Table_flags table_flags(void) const= 0;
2003
2004
  /**
2005
    Is not invoked for non-transactional temporary tables.
2006
2007
    Tells the storage engine that we intend to read or write data
2008
    from the table. This call is prefixed with a call to handler::store_lock()
2009
    and is invoked only for those handler instances that stored the lock.
2010
2011
    Calls to rnd_init/index_init are prefixed with this call. When table
2012
    IO is complete, we call external_lock(F_UNLCK).
2013
    A storage engine writer should expect that each call to
2014
    ::external_lock(F_[RD|WR]LOCK is followed by a call to
2015
    ::external_lock(F_UNLCK). If it is not, it is a bug in MySQL.
2016
2017
    The name and signature originate from the first implementation
2018
    in MyISAM, which would call fcntl to set/clear an advisory
2019
    lock on the data file in this method.
2020
2021
    @param   lock_type    F_RDLCK, F_WRLCK, F_UNLCK
2022
2023
    @return  non-0 in case of failure, 0 in case of success.
2024
    When lock_type is F_UNLCK, the return value is ignored.
2025
  */
2026
  virtual int external_lock(THD *thd __attribute__((unused)),
2027
                            int lock_type __attribute__((unused)))
2028
  {
2029
    return 0;
2030
  }
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
2031
  virtual void release_auto_increment(void) { return; };
1 by brian
clean slate
2032
  /** admin commands - called from mysql_admin_table */
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
2033
  virtual int check_for_upgrade(HA_CHECK_OPT *check_opt __attribute__((unused)))
1 by brian
clean slate
2034
  { return 0; }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
2035
  virtual int check(THD* thd __attribute__((unused)),
2036
                    HA_CHECK_OPT* check_opt __attribute__((unused)))
1 by brian
clean slate
2037
  { return HA_ADMIN_NOT_IMPLEMENTED; }
2038
2039
  /**
2040
     In this method check_opt can be modified
2041
     to specify CHECK option to use to call check()
2042
     upon the table.
2043
  */
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
2044
  virtual int repair(THD* thd __attribute__((unused)),
2045
                     HA_CHECK_OPT* check_opt __attribute__((unused)))
1 by brian
clean slate
2046
  { return HA_ADMIN_NOT_IMPLEMENTED; }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
2047
  virtual void start_bulk_insert(ha_rows rows __attribute__((unused)))
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
2048
  {}
2049
  virtual int end_bulk_insert(void) { return 0; }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
2050
  virtual int index_read(uchar * buf __attribute__((unused)),
2051
                         const uchar * key __attribute__((unused)),
2052
                         uint key_len __attribute__((unused)),
2053
                         enum ha_rkey_function find_flag __attribute__((unused)))
1 by brian
clean slate
2054
   { return  HA_ERR_WRONG_COMMAND; }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
2055
  virtual int index_read_last(uchar * buf __attribute__((unused)),
2056
                              const uchar * key __attribute__((unused)),
2057
                              uint key_len __attribute__((unused)))
1 by brian
clean slate
2058
   { return (my_errno= HA_ERR_WRONG_COMMAND); }
2059
  /**
2060
    This method is similar to update_row, however the handler doesn't need
2061
    to execute the updates at this point in time. The handler can be certain
2062
    that another call to bulk_update_row will occur OR a call to
2063
    exec_bulk_update before the set of updates in this query is concluded.
2064
2065
    @param    old_data       Old record
2066
    @param    new_data       New record
2067
    @param    dup_key_found  Number of duplicate keys found
2068
2069
    @retval  0   Bulk delete used by handler
2070
    @retval  1   Bulk delete not used, normal operation used
2071
  */
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
2072
  virtual int bulk_update_row(const uchar *old_data __attribute__((unused)),
2073
                              uchar *new_data __attribute__((unused)),
2074
                              uint *dup_key_found __attribute__((unused)))
1 by brian
clean slate
2075
  {
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
2076
    assert(false);
1 by brian
clean slate
2077
    return HA_ERR_WRONG_COMMAND;
2078
  }
2079
  /**
2080
    This is called to delete all rows in a table
2081
    If the handler don't support this, then this function will
2082
    return HA_ERR_WRONG_COMMAND and MySQL will delete the rows one
2083
    by one.
2084
  */
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
2085
  virtual int delete_all_rows(void)
1 by brian
clean slate
2086
  { return (my_errno=HA_ERR_WRONG_COMMAND); }
2087
  /**
2088
    Reset the auto-increment counter to the given value, i.e. the next row
2089
    inserted will get the given value. This is called e.g. after TRUNCATE
2090
    is emulated by doing a 'DELETE FROM t'. HA_ERR_WRONG_COMMAND is
2091
    returned by storage engines that don't support this operation.
2092
  */
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
2093
  virtual int reset_auto_increment(uint64_t value __attribute__((unused)))
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
2094
  { return HA_ERR_WRONG_COMMAND; }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
2095
  virtual int optimize(THD* thd __attribute__((unused)),
2096
                       HA_CHECK_OPT* check_opt __attribute__((unused)))
2097
  { return HA_ADMIN_NOT_IMPLEMENTED; }
2098
  virtual int analyze(THD* thd __attribute__((unused)),
2099
                      HA_CHECK_OPT* check_opt __attribute__((unused)))
2100
  { return HA_ADMIN_NOT_IMPLEMENTED; }
2101
  virtual bool check_and_repair(THD *thd __attribute__((unused)))
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
2102
  { return true; }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
2103
  virtual int disable_indexes(uint mode __attribute__((unused)))
2104
  { return HA_ERR_WRONG_COMMAND; }
2105
  virtual int enable_indexes(uint mode __attribute__((unused)))
2106
  { return HA_ERR_WRONG_COMMAND; }
2107
  virtual int discard_or_import_tablespace(bool discard __attribute__((unused)))
1 by brian
clean slate
2108
  { return (my_errno=HA_ERR_WRONG_COMMAND); }
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
2109
  virtual void prepare_for_alter(void) { return; }
1 by brian
clean slate
2110
  virtual void drop_table(const char *name);
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
2111
  virtual int create(const char *name __attribute__((unused)),
2112
                     TABLE *form __attribute__((unused)),
2113
                     HA_CREATE_INFO *info __attribute__((unused)))=0;
1 by brian
clean slate
2114
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
2115
  virtual int create_handler_files(const char *name __attribute__((unused)),
2116
                                   const char *old_name __attribute__((unused)),
2117
                                   int action_flag __attribute__((unused)),
2118
                                   HA_CREATE_INFO *info __attribute__((unused)))
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
2119
  { return false; }
1 by brian
clean slate
2120
};
2121
2122
2123
2124
/**
2125
  A Disk-Sweep MRR interface implementation
2126
2127
  This implementation makes range (and, in the future, 'ref') scans to read
2128
  table rows in disk sweeps. 
2129
  
2130
  Currently it is used by MyISAM and InnoDB. Potentially it can be used with
2131
  any table handler that has non-clustered indexes and on-disk rows.
2132
*/
2133
2134
class DsMrr_impl
2135
{
2136
public:
2137
  typedef void (handler::*range_check_toggle_func_t)(bool on);
2138
2139
  DsMrr_impl()
2140
    : h2(NULL) {};
2141
2142
  handler *h; /* The "owner" handler object. It is used for scanning the index */
2143
  TABLE *table; /* Always equal to h->table */
2144
private:
2145
  /*
2146
    Secondary handler object. It is used to retrieve full table rows by
2147
    calling rnd_pos().
2148
  */
2149
  handler *h2;
2150
2151
  /* Buffer to store rowids, or (rowid, range_id) pairs */
2152
  uchar *rowids_buf;
2153
  uchar *rowids_buf_cur;   /* Current position when reading/writing */
2154
  uchar *rowids_buf_last;  /* When reading: end of used buffer space */
2155
  uchar *rowids_buf_end;   /* End of the buffer */
2156
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
2157
  bool dsmrr_eof; /* true <=> We have reached EOF when reading index tuples */
1 by brian
clean slate
2158
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
2159
  /* true <=> need range association, buffer holds {rowid, range_id} pairs */
1 by brian
clean slate
2160
  bool is_mrr_assoc;
2161
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
2162
  bool use_default_impl; /* true <=> shortcut all calls to default MRR impl */
1 by brian
clean slate
2163
public:
2164
  void init(handler *h_arg, TABLE *table_arg)
2165
  {
2166
    h= h_arg; 
2167
    table= table_arg;
2168
  }
2169
  int dsmrr_init(handler *h, KEY *key, RANGE_SEQ_IF *seq_funcs, 
2170
                 void *seq_init_param, uint n_ranges, uint mode, 
2171
                 HANDLER_BUFFER *buf);
2172
  void dsmrr_close();
2173
  int dsmrr_fill_buffer(handler *h);
2174
  int dsmrr_next(handler *h, char **range_info);
2175
2176
  int dsmrr_info(uint keyno, uint n_ranges, uint keys, uint *bufsz,
2177
                 uint *flags, COST_VECT *cost);
2178
2179
  ha_rows dsmrr_info_const(uint keyno, RANGE_SEQ_IF *seq, 
2180
                            void *seq_init_param, uint n_ranges, uint *bufsz,
2181
                            uint *flags, COST_VECT *cost);
2182
private:
2183
  bool key_uses_partial_cols(uint keyno);
2184
  bool choose_mrr_impl(uint keyno, ha_rows rows, uint *flags, uint *bufsz, 
2185
                       COST_VECT *cost);
2186
  bool get_disk_sweep_mrr_cost(uint keynr, ha_rows rows, uint flags, 
2187
                               uint *buffer_size, COST_VECT *cost);
2188
};
2189
2190
extern const char *ha_row_type[];
2191
extern const char *tx_isolation_names[];
2192
extern const char *binlog_format_names[];
2193
extern TYPELIB tx_isolation_typelib;
2194
extern TYPELIB myisam_stats_method_typelib;
61 by Brian Aker
Conversion of handler type.
2195
extern uint32_t total_ha, total_ha_2pc;
1 by brian
clean slate
2196
2197
       /* Wrapper functions */
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
2198
#define ha_commit(thd) (ha_commit_trans((thd), true))
2199
#define ha_rollback(thd) (ha_rollback_trans((thd), true))
1 by brian
clean slate
2200
2201
/* lookups */
2202
handlerton *ha_default_handlerton(THD *thd);
2203
plugin_ref ha_resolve_by_name(THD *thd, const LEX_STRING *name);
2204
plugin_ref ha_lock_engine(THD *thd, handlerton *hton);
2205
handlerton *ha_resolve_by_legacy_type(THD *thd, enum legacy_db_type db_type);
2206
handler *get_new_handler(TABLE_SHARE *share, MEM_ROOT *alloc,
2207
                         handlerton *db_type);
2208
handlerton *ha_checktype(THD *thd, enum legacy_db_type database_type,
2209
                          bool no_substitute, bool report_error);
2210
2211
2212
static inline enum legacy_db_type ha_legacy_type(const handlerton *db_type)
2213
{
2214
  return (db_type == NULL) ? DB_TYPE_UNKNOWN : db_type->db_type;
2215
}
2216
2217
static inline const char *ha_resolve_storage_engine_name(const handlerton *db_type)
2218
{
2219
  return db_type == NULL ? "UNKNOWN" : hton2plugin[db_type->slot]->name.str;
2220
}
2221
205 by Brian Aker
uint32 -> uin32_t
2222
static inline bool ha_check_storage_engine_flag(const handlerton *db_type, uint32_t flag)
1 by brian
clean slate
2223
{
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
2224
  return db_type == NULL ? false : test(db_type->flags & flag);
1 by brian
clean slate
2225
}
2226
2227
static inline bool ha_storage_engine_is_enabled(const handlerton *db_type)
2228
{
2229
  return (db_type && db_type->create) ?
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
2230
         (db_type->state == SHOW_OPTION_YES) : false;
1 by brian
clean slate
2231
}
2232
2233
/* basic stuff */
2234
int ha_init_errors(void);
2235
int ha_init(void);
2236
int ha_end(void);
2237
int ha_initialize_handlerton(st_plugin_int *plugin);
2238
int ha_finalize_handlerton(st_plugin_int *plugin);
2239
2240
TYPELIB *ha_known_exts(void);
2241
void ha_close_connection(THD* thd);
2242
bool ha_flush_logs(handlerton *db_type);
2243
void ha_drop_database(char* path);
2244
int ha_create_table(THD *thd, const char *path,
2245
                    const char *db, const char *table_name,
2246
                    HA_CREATE_INFO *create_info,
2247
                    bool update_create_info);
2248
int ha_delete_table(THD *thd, handlerton *db_type, const char *path,
2249
                    const char *db, const char *alias, bool generate_warning);
2250
2251
/* statistics and info */
2252
bool ha_show_status(THD *thd, handlerton *db_type, enum ha_stat_type stat);
2253
2254
/* discovery */
2255
int ha_create_table_from_engine(THD* thd, const char *db, const char *name);
2256
int ha_discover(THD* thd, const char* dbname, const char* name,
2257
                uchar** frmblob, size_t* frmlen);
2258
int ha_find_files(THD *thd,const char *db,const char *path,
2259
                  const char *wild, bool dir, List<LEX_STRING>* files);
2260
int ha_table_exists_in_engine(THD* thd, const char* db, const char* name);
2261
2262
/* key cache */
2263
extern "C" int ha_init_key_cache(const char *name, KEY_CACHE *key_cache);
2264
int ha_resize_key_cache(KEY_CACHE *key_cache);
2265
int ha_change_key_cache_param(KEY_CACHE *key_cache);
2266
int ha_change_key_cache(KEY_CACHE *old_key_cache, KEY_CACHE *new_key_cache);
2267
int ha_end_key_cache(KEY_CACHE *key_cache);
2268
2269
/* report to InnoDB that control passes to the client */
2270
int ha_release_temporary_latches(THD *thd);
2271
2272
/* transactions: interface to handlerton functions */
2273
int ha_start_consistent_snapshot(THD *thd);
2274
int ha_commit_or_rollback_by_xid(XID *xid, bool commit);
2275
int ha_commit_one_phase(THD *thd, bool all);
2276
int ha_rollback_trans(THD *thd, bool all);
2277
int ha_prepare(THD *thd);
2278
int ha_recover(HASH *commit_list);
2279
2280
/* transactions: these functions never call handlerton functions directly */
2281
int ha_commit_trans(THD *thd, bool all);
2282
int ha_autocommit_or_rollback(THD *thd, int error);
2283
int ha_enable_transaction(THD *thd, bool on);
2284
2285
/* savepoints */
2286
int ha_rollback_to_savepoint(THD *thd, SAVEPOINT *sv);
2287
int ha_savepoint(THD *thd, SAVEPOINT *sv);
2288
int ha_release_savepoint(THD *thd, SAVEPOINT *sv);
2289
2290
/* these are called by storage engines */
2291
void trans_register_ha(THD *thd, bool all, handlerton *ht);
2292
2293
/*
2294
  Storage engine has to assume the transaction will end up with 2pc if
2295
   - there is more than one 2pc-capable storage engine available
2296
   - in the current transaction 2pc was not disabled yet
2297
*/
2298
#define trans_need_2pc(thd, all)                   ((total_ha_2pc > 1) && \
2299
        !((all ? &thd->transaction.all : &thd->transaction.stmt)->no_2pc))
2300
2301
#ifdef HAVE_NDB_BINLOG
2302
int ha_reset_logs(THD *thd);
2303
int ha_binlog_index_purge_file(THD *thd, const char *file);
2304
void ha_reset_slave(THD *thd);
2305
void ha_binlog_log_query(THD *thd, handlerton *db_type,
2306
                         enum_binlog_command binlog_command,
2307
                         const char *query, uint query_length,
2308
                         const char *db, const char *table_name);
2309
void ha_binlog_wait(THD *thd);
2310
int ha_binlog_end(THD *thd);
2311
#else
2312
#define ha_reset_logs(a) do {} while (0)
2313
#define ha_binlog_index_purge_file(a,b) do {} while (0)
2314
#define ha_reset_slave(a) do {} while (0)
2315
#define ha_binlog_log_query(a,b,c,d,e,f,g) do {} while (0)
2316
#define ha_binlog_wait(a) do {} while (0)
2317
#define ha_binlog_end(a)  do {} while (0)
2318
#endif