~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/handler.h

  • Committer: Brian Aker
  • Date: 2008-10-06 06:47:29 UTC
  • Revision ID: brian@tangent.org-20081006064729-2i9mhjkzyvow9xsm
Remove uint.

Show diffs side-by-side

added added

removed removed

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