~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/handler.h

  • Committer: Monty Taylor
  • Date: 2009-03-02 23:14:32 UTC
  • mto: This revision was merged to the branch mainline in revision 910.
  • Revision ID: mordred@inaugust.com-20090302231432-i35xehp7uzo6hjjw
Updated build system to use new version numbering. Just remember to run ./config/autorun.sh before running make distcheck for release and all should be peachy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2008 Sun Microsystems
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#ifndef DRIZZLED_CURSOR_H
21
 
#define DRIZZLED_CURSOR_H
 
20
#ifndef DRIZZLED_HANDLER_H
 
21
#define DRIZZLED_HANDLER_H
22
22
 
23
 
#include <drizzled/atomics.h>
24
 
#include <drizzled/definitions.h>
 
23
#include <drizzled/xid.h>
25
24
#include <drizzled/discrete_interval.h>
26
 
#include <drizzled/error_t.h>
 
25
 
 
26
/* Definitions for parameters to do with handler-routines */
 
27
 
 
28
#include <storage/myisam/keycache.h>
 
29
#include <mysys/thr_lock.h>
 
30
#include <mysys/hash.h>
 
31
#include <drizzled/sql_string.h>
 
32
#include <drizzled/sql_list.h>
 
33
#include <drizzled/handlerton.h>
 
34
#include <drizzled/handler_structs.h>
27
35
#include <drizzled/ha_statistics.h>
28
 
#include <drizzled/handler_structs.h>
29
 
#include <drizzled/identifier.h>
30
 
#include <drizzled/key_map.h>
31
 
#include <drizzled/message/table.h>
32
 
#include <drizzled/sql_list.h>
33
 
#include <drizzled/thr_lock.h>
34
 
#include <drizzled/xid.h>
35
 
 
36
 
#include <bitset>
37
 
#include <algorithm>
38
 
 
39
 
#include <drizzled/visibility.h>
40
 
 
41
 
namespace drizzled
42
 
{
 
36
 
 
37
/* Bits to show what an alter table will do */
 
38
#include <drizzled/sql_bitmap.h>
43
39
 
44
40
#define HA_MAX_ALTER_FLAGS 40
45
 
 
46
 
typedef std::bitset<HA_MAX_ALTER_FLAGS> HA_ALTER_FLAGS;
47
 
 
48
 
class AlterInfo;
49
 
class CreateField;
50
 
class ForeignKeyInfo;
 
41
typedef Bitmap<HA_MAX_ALTER_FLAGS> HA_ALTER_FLAGS;
 
42
 
 
43
 
 
44
typedef bool (*qc_engine_callback)(Session *session, char *table_key,
 
45
                                      uint32_t key_length,
 
46
                                      uint64_t *engine_data);
 
47
 
 
48
 
 
49
/* The handler for a table type.  Will be included in the Table structure */
 
50
 
 
51
class Table;
 
52
class TableList;
 
53
class TABLE_SHARE;
 
54
class Select_Lex_Unit;
 
55
struct st_foreign_key_info;
 
56
typedef struct st_foreign_key_info FOREIGN_KEY_INFO;
 
57
struct order_st;
 
58
 
51
59
class Item;
52
 
class Item_ident;
 
60
struct st_table_log_memory_entry;
 
61
 
53
62
class LEX;
54
63
class Select_Lex;
55
 
class Select_Lex_Unit;
56
 
class String;
57
 
class Table;
58
 
class TableList;
59
 
class TableShare;
 
64
class Alter_info;
60
65
class select_result;
 
66
class Create_field;
61
67
class sys_var_str;
62
 
struct Order;
 
68
class Item_ident;
 
69
typedef struct st_sort_field SORT_FIELD;
63
70
 
64
71
typedef List<Item> List_item;
 
72
 
 
73
typedef struct st_savepoint SAVEPOINT;
 
74
extern uint32_t savepoint_alloc_size;
65
75
extern KEY_CREATE_INFO default_key_create_info;
66
76
 
67
77
/* Forward declaration for condition pushdown to storage engine */
68
78
typedef class Item COND;
69
79
 
70
 
typedef struct system_status_var system_status_var;
71
 
 
72
 
namespace optimizer { class CostVector; }
73
 
namespace plugin { class StorageEngine; }
74
 
 
 
80
typedef struct system_status_var SSV;
 
81
 
 
82
class COST_VECT;
 
83
 
 
84
uint16_t &mrr_persistent_flag_storage(range_seq_t seq, uint32_t idx);
 
85
char* &mrr_get_ptr_by_idx(range_seq_t seq, uint32_t idx);
 
86
 
 
87
uint32_t calculate_key_len(Table *, uint, const unsigned char *, key_part_map);
75
88
/*
76
89
  bitmap with first N+1 bits set
77
90
  (keypart_map for a key prefix of [0..N] keyparts)
93
106
}
94
107
 
95
108
/**
96
 
  The Cursor class is the interface for dynamically loadable
 
109
  The handler class is the interface for dynamically loadable
97
110
  storage engines. Do not add ifdefs and take care when adding or
98
111
  changing virtual functions to avoid vtable confusion
99
112
 
103
116
     storage engine
104
117
 
105
118
  2. KeyTupleFormat - used to pass index search tuples (aka "keys") to
106
 
     storage engine. See optimizer/range.cc for description of this format.
 
119
     storage engine. See opt_range.cc for description of this format.
107
120
 
108
121
  TableRecordFormat
109
122
  =================
128
141
  present, its length is one byte <not-sure> which must be set to 0xFF
129
142
  at all times. </not-sure>
130
143
 
 
144
  If the table has columns of type BIT, then certain bits from those columns
 
145
  may be stored in null_bytes as well. Grep around for Field_bit for
 
146
  details.
 
147
 
131
148
  For blob columns (see Field_blob), the record buffer stores length of the
132
149
  data, following by memory pointer to the blob data. The pointer is owned
133
150
  by the storage engine and is valid until the next operation.
135
152
  If a blob column has NULL value, then its length and blob data pointer
136
153
  must be set to 0.
137
154
*/
138
 
class DRIZZLED_API Cursor
 
155
 
 
156
class handler :public Sql_alloc
139
157
{
140
 
  friend class SEAPITesterCursor;
141
 
  Table &table;               /* The current open table */
142
 
  plugin::StorageEngine &engine;      /* storage engine of this Cursor */
 
158
public:
 
159
  typedef uint64_t Table_flags;
143
160
 
144
161
protected:
 
162
  TABLE_SHARE *table_share;   /* The table definition */
 
163
  Table *table;               /* The current open table */
 
164
  Table_flags cached_table_flags;       /* Set on init() and open() */
 
165
 
145
166
  ha_rows estimation_rows_to_insert;
146
 
 
147
167
public:
148
 
  inline plugin::StorageEngine *getEngine() const       /* table_type for handler */
149
 
  {
150
 
    return &engine;
151
 
  }
 
168
  handlerton *ht;                 /* storage engine of this handler */
152
169
  unsigned char *ref;                           /* Pointer to current row */
153
170
  unsigned char *dup_ref;                       /* Pointer to duplicate row */
154
171
 
155
 
  TableShare *getShare();
156
 
 
157
 
  Table *getTable() const
158
 
  {
159
 
    return &table;
160
 
  }
161
 
 
162
172
  ha_statistics stats;
163
173
  /** MultiRangeRead-related members: */
164
174
  range_seq_t mrr_iter;    /* Interator to traverse the range sequence */
165
175
  RANGE_SEQ_IF mrr_funcs;  /* Range sequence traversal functions */
166
 
 
 
176
  HANDLER_BUFFER *multi_range_buffer; /* MRR buffer info */
167
177
  uint32_t ranges_in_seq; /* Total number of ranges in the traversed sequence */
168
178
  /* true <=> source MRR ranges and the output are ordered */
169
179
  bool mrr_is_output_sorted;
170
180
 
171
181
  /** true <=> we're currently traversing a range in mrr_cur_range. */
172
182
  bool mrr_have_range;
173
 
 
174
 
  bool eq_range;
175
 
 
176
183
  /** Current range (the one we're now returning rows from) */
177
184
  KEY_MULTI_RANGE mrr_cur_range;
178
185
 
179
186
  /** The following are for read_range() */
180
187
  key_range save_end_range, *end_range;
181
 
  KeyPartInfo *range_key_part;
 
188
  KEY_PART_INFO *range_key_part;
182
189
  int key_compare_result_on_equal;
 
190
  bool eq_range;
 
191
  /*
 
192
    true <=> the engine guarantees that returned records are within the range
 
193
    being scanned.
 
194
  */
 
195
  bool in_range_check_pushed_down;
183
196
 
184
197
  uint32_t errkey;                              /* Last dup key */
185
198
  uint32_t key_used_on_scan;
188
201
  uint32_t ref_length;
189
202
  enum {NONE=0, INDEX, RND} inited;
190
203
  bool locked;
 
204
  bool implicit_emptied;                /* Can be !=0 only if HEAP */
 
205
  const Item *pushed_cond;
 
206
 
 
207
  Item *pushed_idx_cond;
 
208
  uint32_t pushed_idx_cond_keyno;  /* The index which the above condition is for */
191
209
 
192
210
  /**
193
211
    next_insert_id is the next value which should be inserted into the
199
217
    get_auto_increment().
200
218
  */
201
219
  uint64_t next_insert_id;
202
 
  uint64_t getNextInsertId()
203
 
  {
204
 
    return next_insert_id;
205
 
  }
206
 
 
207
 
  /**
208
 
    Used by SHOW TABLE STATUS to get the current auto_inc from the engine
209
 
  */
210
 
  uint64_t getAutoIncrement()
211
 
  {
212
 
    return stats.auto_increment_value;
213
 
  }
214
 
 
215
220
  /**
216
221
    insert id for the current row (*autogenerated*; if not
217
222
    autogenerated, it's 0).
225
230
  */
226
231
  Discrete_interval auto_inc_interval_for_cur_row;
227
232
 
228
 
  Cursor(plugin::StorageEngine &engine_arg, Table &share_arg);
229
 
  virtual ~Cursor(void);
230
 
  virtual Cursor *clone(memory::Root *mem_root);
 
233
  handler(handlerton *ht_arg, TABLE_SHARE *share_arg)
 
234
    :table_share(share_arg), table(0),
 
235
    estimation_rows_to_insert(0), ht(ht_arg),
 
236
    ref(0), in_range_check_pushed_down(false),
 
237
    key_used_on_scan(MAX_KEY), active_index(MAX_KEY),
 
238
    ref_length(sizeof(my_off_t)),
 
239
    inited(NONE),
 
240
    locked(false), implicit_emptied(0),
 
241
    pushed_cond(0), pushed_idx_cond(NULL), pushed_idx_cond_keyno(MAX_KEY),
 
242
    next_insert_id(0), insert_id_for_cur_row(0)
 
243
    {}
 
244
  virtual ~handler(void)
 
245
  {
 
246
    assert(locked == false);
 
247
    /* TODO: assert(inited == NONE); */
 
248
  }
 
249
  virtual handler *clone(MEM_ROOT *mem_root);
 
250
  /** This is called after create to allow us to set up cached variables */
 
251
  void init()
 
252
  {
 
253
    cached_table_flags= table_flags();
 
254
  }
231
255
 
232
256
  /* ha_ methods: pubilc wrappers for private virtual API */
233
257
 
234
 
  int ha_open(const identifier::Table &identifier, int mode, int test_if_locked);
235
 
  int startIndexScan(uint32_t idx, bool sorted) __attribute__ ((warn_unused_result));
236
 
  int endIndexScan();
237
 
  int startTableScan(bool scan) __attribute__ ((warn_unused_result));
238
 
  int endTableScan();
 
258
  int ha_open(Table *table, const char *name, int mode, int test_if_locked);
 
259
  int ha_index_init(uint32_t idx, bool sorted);
 
260
  int ha_index_end();
 
261
  int ha_rnd_init(bool scan);
 
262
  int ha_rnd_end();
239
263
  int ha_reset();
240
264
 
241
265
  /* this is necessary in many places, e.g. in HANDLER command */
242
266
  int ha_index_or_rnd_end();
 
267
  Table_flags ha_table_flags() const;
243
268
 
244
269
  /**
245
270
    These functions represent the public interface to *users* of the
246
 
    Cursor class, hence they are *not* virtual. For the inheritance
247
 
    interface, see the (private) functions doInsertRecord(), doUpdateRecord(),
248
 
    and doDeleteRecord() below.
 
271
    handler class, hence they are *not* virtual. For the inheritance
 
272
    interface, see the (private) functions write_row(), update_row(),
 
273
    and delete_row() below.
249
274
  */
250
275
  int ha_external_lock(Session *session, int lock_type);
251
 
  int insertRecord(unsigned char * buf) __attribute__ ((warn_unused_result));
252
 
  int updateRecord(const unsigned char * old_data, unsigned char * new_data) __attribute__ ((warn_unused_result));
253
 
  int deleteRecord(const unsigned char * buf) __attribute__ ((warn_unused_result));
 
276
  int ha_write_row(unsigned char * buf);
 
277
  int ha_update_row(const unsigned char * old_data, unsigned char * new_data);
 
278
  int ha_delete_row(const unsigned char * buf);
254
279
  void ha_release_auto_increment();
255
280
 
 
281
  int ha_check_for_upgrade(HA_CHECK_OPT *check_opt);
256
282
  /** to be actually called to get 'check()' functionality*/
257
283
  int ha_check(Session *session, HA_CHECK_OPT *check_opt);
258
 
 
 
284
  int ha_repair(Session* session, HA_CHECK_OPT* check_opt);
259
285
  void ha_start_bulk_insert(ha_rows rows);
260
286
  int ha_end_bulk_insert();
 
287
  int ha_bulk_update_row(const unsigned char *old_data, unsigned char *new_data,
 
288
                         uint32_t *dup_key_found);
261
289
  int ha_delete_all_rows();
262
290
  int ha_reset_auto_increment(uint64_t value);
 
291
  int ha_optimize(Session* session, HA_CHECK_OPT* check_opt);
263
292
  int ha_analyze(Session* session, HA_CHECK_OPT* check_opt);
264
 
 
 
293
  bool ha_check_and_repair(Session *session);
265
294
  int ha_disable_indexes(uint32_t mode);
266
295
  int ha_enable_indexes(uint32_t mode);
267
296
  int ha_discard_or_import_tablespace(bool discard);
268
 
  void closeMarkForDelete(const char *name);
 
297
  void ha_prepare_for_alter();
 
298
  int ha_rename_table(const char *from, const char *to);
 
299
  int ha_delete_table(const char *name);
 
300
  void ha_drop_table(const char *name);
 
301
 
 
302
  int ha_create(const char *name, Table *form, HA_CREATE_INFO *info);
 
303
 
 
304
  int ha_create_handler_files(const char *name, const char *old_name,
 
305
                              int action_flag, HA_CREATE_INFO *info);
269
306
 
270
307
  void adjust_next_insert_id_after_explicit_value(uint64_t nr);
271
308
  int update_auto_increment();
 
309
  void print_keydup_error(uint32_t key_nr, const char *msg);
 
310
  virtual void print_error(int error, myf errflag);
 
311
  virtual bool get_error_message(int error, String *buf);
 
312
  uint32_t get_dup_key(int error);
 
313
  virtual void change_table_ptr(Table *table_arg, TABLE_SHARE *share);
272
314
 
273
315
  /* Estimates calculation */
274
316
  virtual double scan_time(void)
275
 
  { return static_cast<double>(stats.data_file_length) / IO_SIZE + 2; }
 
317
  { return uint64_t2double(stats.data_file_length) / IO_SIZE + 2; }
276
318
  virtual double read_time(uint32_t, uint32_t ranges, ha_rows rows)
277
 
  { return ranges + rows; }
 
319
  { return rows2double(ranges+rows); }
278
320
 
279
321
  virtual double index_only_read_time(uint32_t keynr, double records);
280
322
 
281
323
  virtual ha_rows multi_range_read_info_const(uint32_t keyno, RANGE_SEQ_IF *seq,
282
324
                                              void *seq_init_param,
283
325
                                              uint32_t n_ranges, uint32_t *bufsz,
284
 
                                              uint32_t *flags, optimizer::CostVector *cost);
 
326
                                              uint32_t *flags, COST_VECT *cost);
285
327
  virtual int multi_range_read_info(uint32_t keyno, uint32_t n_ranges, uint32_t keys,
286
 
                                    uint32_t *bufsz, uint32_t *flags, optimizer::CostVector *cost);
 
328
                                    uint32_t *bufsz, uint32_t *flags, COST_VECT *cost);
287
329
  virtual int multi_range_read_init(RANGE_SEQ_IF *seq, void *seq_init_param,
288
 
                                    uint32_t n_ranges, uint32_t mode);
 
330
                                    uint32_t n_ranges, uint32_t mode,
 
331
                                    HANDLER_BUFFER *buf);
289
332
  virtual int multi_range_read_next(char **range_info);
290
333
 
291
334
 
295
338
  /**
296
339
    This method is used to analyse the error to see whether the error
297
340
    is ignorable or not, certain handlers can have more error that are
298
 
    ignorable than others. E.g. the partition Cursor can get inserts
 
341
    ignorable than others. E.g. the partition handler can get inserts
299
342
    into a range where there is no partition and this is an ignorable
300
343
    error.
301
344
    HA_ERR_FOUND_DUP_UNIQUE is a special case in MyISAM that means the
309
352
    (table_flags() & (HA_HAS_RECORDS | HA_STATS_RECORDS_IS_EXACT)) != 0
310
353
  */
311
354
  virtual ha_rows records();
312
 
  virtual uint64_t tableSize();
313
 
  virtual uint64_t rowSize();
314
355
  /**
315
356
    Return upper bound of current number of records in the table
316
357
    (max. of how many records one will retrieve when doing a full table scan)
320
361
  virtual ha_rows estimate_rows_upper_bound()
321
362
  { return stats.records+EXTRA_RECORDS; }
322
363
 
 
364
  /**
 
365
    Get the row type from the storage engine.  If this method returns
 
366
    ROW_TYPE_NOT_USED, the information in HA_CREATE_INFO should be used.
 
367
  */
 
368
  virtual enum row_type get_row_type() const { return ROW_TYPE_NOT_USED; }
 
369
 
323
370
  virtual const char *index_type(uint32_t)
324
371
  { assert(0); return "";}
325
372
 
326
373
 
 
374
  /**
 
375
    Signal that the table->read_set and table->write_set table maps changed
 
376
    The handler is allowed to set additional bits in the above map in this
 
377
    call. Normally the handler should ignore all calls until we have done
 
378
    a ha_rnd_init() or ha_index_init(), write_row(), update_row or delete_row()
 
379
    as there may be several calls to this routine.
 
380
  */
 
381
  virtual void column_bitmaps_signal();
327
382
  uint32_t get_index(void) const { return active_index; }
328
383
  virtual int close(void)=0;
329
384
 
330
385
  /**
 
386
    @retval  0   Bulk update used by handler
 
387
    @retval  1   Bulk update not used, normal operation used
 
388
  */
 
389
  virtual bool start_bulk_update() { return 1; }
 
390
  /**
 
391
    @retval  0   Bulk delete used by handler
 
392
    @retval  1   Bulk delete not used, normal operation used
 
393
  */
 
394
  virtual bool start_bulk_delete() { return 1; }
 
395
  /**
 
396
    After this call all outstanding updates must be performed. The number
 
397
    of duplicate key errors are reported in the duplicate key parameter.
 
398
    It is allowed to continue to the batched update after this call, the
 
399
    handler has to wait until end_bulk_update with changing state.
 
400
 
 
401
    @param    dup_key_found       Number of duplicate keys found
 
402
 
 
403
    @retval  0           Success
 
404
    @retval  >0          Error code
 
405
  */
 
406
  virtual int exec_bulk_update(uint32_t *)
 
407
  {
 
408
    assert(false);
 
409
    return HA_ERR_WRONG_COMMAND;
 
410
  }
 
411
  /**
 
412
    Perform any needed clean-up, no outstanding updates are there at the
 
413
    moment.
 
414
  */
 
415
  virtual void end_bulk_update() { return; }
 
416
  /**
 
417
    Execute all outstanding deletes and close down the bulk delete.
 
418
 
 
419
    @retval 0             Success
 
420
    @retval >0            Error code
 
421
  */
 
422
  virtual int end_bulk_delete()
 
423
  {
 
424
    assert(false);
 
425
    return HA_ERR_WRONG_COMMAND;
 
426
  }
 
427
  /**
331
428
     @brief
332
429
     Positions an index cursor to the index specified in the handle. Fetches the
333
430
     row if available. If the key value is null, begin at the first key of the
334
431
     index.
335
432
  */
336
 
  virtual int index_read_map(unsigned char * buf, const unsigned char *key,
 
433
  virtual int index_read_map(unsigned char * buf, const unsigned char * key,
337
434
                             key_part_map keypart_map,
338
435
                             enum ha_rkey_function find_flag)
339
436
  {
340
 
    uint32_t key_len= calculate_key_len(active_index, keypart_map);
 
437
    uint32_t key_len= calculate_key_len(table, active_index, key, keypart_map);
341
438
    return  index_read(buf, key, key_len, find_flag);
342
439
  }
343
440
  /**
350
447
                                 const unsigned char * key,
351
448
                                 key_part_map keypart_map,
352
449
                                 enum ha_rkey_function find_flag);
353
 
  virtual int index_next(unsigned char *) __attribute__ ((warn_unused_result))
 
450
  virtual int index_next(unsigned char *)
354
451
   { return  HA_ERR_WRONG_COMMAND; }
355
452
  virtual int index_prev(unsigned char *)
356
453
   { return  HA_ERR_WRONG_COMMAND; }
359
456
  virtual int index_last(unsigned char *)
360
457
   { return  HA_ERR_WRONG_COMMAND; }
361
458
  virtual int index_next_same(unsigned char *, const unsigned char *, uint32_t);
362
 
 
363
 
private:
364
 
  uint32_t calculate_key_len(uint32_t key_position, key_part_map keypart_map_arg);
365
 
public:
366
 
 
367
459
  /**
368
460
     @brief
369
461
     The following functions works like index_read, but it find the last
372
464
  virtual int index_read_last_map(unsigned char * buf, const unsigned char * key,
373
465
                                  key_part_map keypart_map)
374
466
  {
375
 
    uint32_t key_len= calculate_key_len(active_index, keypart_map);
 
467
    uint32_t key_len= calculate_key_len(table, active_index, key, keypart_map);
376
468
    return index_read_last(buf, key, key_len);
377
469
  }
378
470
  virtual int read_range_first(const key_range *start_key,
380
472
                               bool eq_range, bool sorted);
381
473
  virtual int read_range_next();
382
474
  int compare_key(key_range *range);
 
475
  int compare_key2(key_range *range);
383
476
  virtual int rnd_next(unsigned char *)=0;
384
477
  virtual int rnd_pos(unsigned char *, unsigned char *)=0;
 
478
  /**
 
479
    One has to use this method when to find
 
480
    random position by record as the plain
 
481
    position() call doesn't work for some
 
482
    handlers for random position.
 
483
  */
 
484
  virtual int rnd_pos_by_record(unsigned char *record);
385
485
  virtual int read_first_row(unsigned char *buf, uint32_t primary_key);
 
486
  /**
 
487
    The following function is only needed for tables that may be temporary
 
488
    tables during joins.
 
489
  */
 
490
  virtual int restart_rnd_next(unsigned char *, unsigned char *)
 
491
    { return HA_ERR_WRONG_COMMAND; }
386
492
  virtual int rnd_same(unsigned char *, uint32_t)
387
493
    { return HA_ERR_WRONG_COMMAND; }
388
494
  virtual ha_rows records_in_range(uint32_t, key_range *, key_range *)
417
523
  */
418
524
  virtual void try_semi_consistent_read(bool) {}
419
525
  virtual void unlock_row(void) {}
 
526
  virtual int start_stmt(Session *, thr_lock_type)
 
527
  {return 0;}
420
528
  virtual void get_auto_increment(uint64_t offset, uint64_t increment,
421
529
                                  uint64_t nb_desired_values,
422
530
                                  uint64_t *first_value,
423
 
                                  uint64_t *nb_reserved_values)= 0;
424
 
 
 
531
                                  uint64_t *nb_reserved_values);
425
532
  void set_next_insert_id(uint64_t id)
426
533
  {
427
534
    next_insert_id= id;
442
549
      insert_id_for_cur_row;
443
550
  }
444
551
 
 
552
  virtual void update_create_info(HA_CREATE_INFO *) {}
 
553
  int check_old_types(void);
 
554
  virtual int assign_to_keycache(Session*, HA_CHECK_OPT *)
 
555
  { return HA_ADMIN_NOT_IMPLEMENTED; }
445
556
  /* end of the list of admin commands */
446
557
 
447
558
  virtual int indexes_are_disabled(void) {return 0;}
 
559
  virtual char *update_table_comment(const char * comment)
 
560
  { return (char*) comment;}
448
561
  virtual void append_create_info(String *)
449
562
  {}
450
563
  /**
457
570
    @retval   true            Foreign key defined on table or index
458
571
    @retval   false           No foreign key defined
459
572
  */
 
573
  virtual bool is_fk_defined_on_table_or_index(uint32_t)
 
574
  { return false; }
460
575
  virtual char* get_foreign_key_create_info(void)
461
 
  { return NULL;}  /* gets foreign key create string from InnoDB */
462
 
  /** used in ALTER Table; if changing storage engine is allowed.
463
 
      e.g. not be allowed if table has foreign key constraints in engine.
464
 
   */
465
 
  virtual bool can_switch_engines(void) { return true; }
 
576
  { return(NULL);}  /* gets foreign key create string from InnoDB */
 
577
  /** used in ALTER Table; 1 if changing storage engine is allowed */
 
578
  virtual bool can_switch_engines(void) { return 1; }
466
579
  /** used in REPLACE; is > 0 if table is referred by a FOREIGN KEY */
467
 
  virtual int get_foreign_key_list(Session *, List<ForeignKeyInfo> *)
 
580
  virtual int get_foreign_key_list(Session *, List<FOREIGN_KEY_INFO> *)
468
581
  { return 0; }
469
582
  virtual uint32_t referenced_by_foreign_key() { return 0;}
 
583
  virtual void init_table_handle_for_HANDLER()
 
584
  { return; }       /* prepare InnoDB for HANDLER */
470
585
  virtual void free_foreign_key_create_info(char *) {}
471
 
 
 
586
  /** The following can be called without an open handler */
 
587
  virtual const char *table_type() const =0;
 
588
  /**
 
589
    If frm_error() is called then we will use this to find out what file
 
590
    extentions exist for the storage engine. This is also used by the default
 
591
    rename_table and delete_table method in handler.cc.
 
592
 
 
593
    For engines that have two file name extentions (separate meta/index file
 
594
    and data file), the order of elements is relevant. First element of engine
 
595
    file name extentions array should be meta/index file extention. Second
 
596
    element - data file extention. This order is assumed by
 
597
    prepare_for_repair() when REPAIR Table ... USE_FRM is issued.
 
598
  */
 
599
  virtual const char **bas_ext() const =0;
 
600
 
 
601
  virtual int get_default_no_partitions(HA_CREATE_INFO *) { return 1;}
 
602
  virtual bool get_no_parts(const char *, uint32_t *no_parts)
 
603
  {
 
604
    *no_parts= 0;
 
605
    return 0;
 
606
  }
 
607
 
 
608
  virtual uint32_t index_flags(uint32_t idx, uint32_t part, bool all_parts) const =0;
 
609
 
 
610
  virtual int add_index(Table *, KEY *, uint32_t)
 
611
  { return (HA_ERR_WRONG_COMMAND); }
 
612
  virtual int prepare_drop_index(Table *, uint32_t *, uint32_t)
 
613
  { return (HA_ERR_WRONG_COMMAND); }
 
614
  virtual int final_drop_index(Table *)
 
615
  { return (HA_ERR_WRONG_COMMAND); }
 
616
 
 
617
  uint32_t max_record_length() const
 
618
  { return cmin((unsigned int)HA_MAX_REC_LENGTH, max_supported_record_length()); }
 
619
  uint32_t max_keys() const
 
620
  { return cmin((unsigned int)MAX_KEY, max_supported_keys()); }
 
621
  uint32_t max_key_parts() const
 
622
  { return cmin((unsigned int)MAX_REF_PARTS, max_supported_key_parts()); }
 
623
  uint32_t max_key_length() const
 
624
  { return cmin((unsigned int)MAX_KEY_LENGTH, max_supported_key_length()); }
 
625
  uint32_t max_key_part_length(void) const
 
626
  { return cmin((unsigned int)MAX_KEY_LENGTH, max_supported_key_part_length()); }
 
627
 
 
628
  virtual uint32_t max_supported_record_length(void) const
 
629
  { return HA_MAX_REC_LENGTH; }
 
630
  virtual uint32_t max_supported_keys(void) const { return 0; }
 
631
  virtual uint32_t max_supported_key_parts(void) const { return MAX_REF_PARTS; }
 
632
  virtual uint32_t max_supported_key_length(void) const { return MAX_KEY_LENGTH; }
 
633
  virtual uint32_t max_supported_key_part_length(void) const { return 255; }
 
634
 
 
635
  virtual bool low_byte_first(void) const { return 1; }
 
636
  virtual uint32_t checksum(void) const { return 0; }
 
637
  virtual bool is_crashed(void) const  { return 0; }
 
638
  virtual bool auto_repair(void) const { return 0; }
 
639
 
 
640
 
 
641
#define CHF_CREATE_FLAG 0
 
642
#define CHF_DELETE_FLAG 1
 
643
#define CHF_RENAME_FLAG 2
 
644
 
 
645
 
 
646
  /**
 
647
    @note lock_count() can return > 1 if the table is MERGE or partitioned.
 
648
  */
 
649
  virtual uint32_t lock_count(void) const { return 1; }
472
650
  /**
473
651
    Is not invoked for non-transactional temporary tables.
474
652
 
476
654
    or partitioned.
477
655
 
478
656
    @note that one can NOT rely on table->in_use in store_lock().  It may
479
 
    refer to a different thread if called from abortLockForThread().
 
657
    refer to a different thread if called from mysql_lock_abort_for_thread().
480
658
 
481
659
    @note If the table is MERGE, store_lock() can return less locks
482
660
    than lock_count() claimed. This can happen when the MERGE children
483
661
    are not attached when this is called from another thread.
484
662
  */
485
 
  virtual THR_LOCK_DATA **store_lock(Session *,
 
663
  virtual THR_LOCK_DATA **store_lock(Session *session,
486
664
                                     THR_LOCK_DATA **to,
487
 
                                     enum thr_lock_type)
488
 
  {
489
 
    assert(0); // Impossible programming situation
490
 
 
491
 
    return(to);
492
 
  }
 
665
                                     enum thr_lock_type lock_type)=0;
 
666
 
 
667
  /** Type of table for caching query */
 
668
  virtual uint8_t table_cache_type() { return HA_CACHE_TBL_NONTRANSACT; }
 
669
 
493
670
 
494
671
 /*
495
672
   @retval true   Primary key (if there is one) is clustered
502
679
   return memcmp(ref1, ref2, ref_length);
503
680
 }
504
681
 
505
 
  virtual bool isOrdered(void)
 
682
 /*
 
683
   Condition pushdown to storage engines
 
684
 */
 
685
 
 
686
 /**
 
687
   Push condition down to the table handler.
 
688
 
 
689
   @param  cond   Condition to be pushed. The condition tree must not be
 
690
                  modified by the by the caller.
 
691
 
 
692
   @return
 
693
     The 'remainder' condition that caller must use to filter out records.
 
694
     NULL means the handler will not return rows that do not match the
 
695
     passed condition.
 
696
 
 
697
   @note
 
698
   The pushed conditions form a stack (from which one can remove the
 
699
   last pushed condition using cond_pop).
 
700
   The table handler filters out rows using (pushed_cond1 AND pushed_cond2
 
701
   AND ... AND pushed_condN)
 
702
   or less restrictive condition, depending on handler's capabilities.
 
703
 
 
704
   handler->ha_reset() call empties the condition stack.
 
705
   Calls to rnd_init/rnd_end, index_init/index_end etc do not affect the
 
706
   condition stack.
 
707
 */
 
708
 virtual const COND *cond_push(const COND *cond) { return cond; }
 
709
 
 
710
 /**
 
711
   Pop the top condition from the condition stack of the handler instance.
 
712
 
 
713
   Pops the top if condition stack, if stack is not empty.
 
714
 */
 
715
 virtual void cond_pop(void) { return; }
 
716
 
 
717
 virtual Item *idx_cond_push(uint32_t, Item *idx_cond)
 
718
 { return idx_cond; }
 
719
 
 
720
 /*
 
721
    Part of old fast alter table, to be depricated
 
722
  */
 
723
 virtual bool
 
724
   check_if_incompatible_data(HA_CREATE_INFO *, uint32_t)
 
725
 { return COMPATIBLE_DATA_NO; }
 
726
 
 
727
 /* On-line ALTER Table interface */
 
728
 
 
729
 /**
 
730
    Check if a storage engine supports a particular alter table on-line
 
731
 
 
732
    @param    altered_table     A temporary table show what table is to
 
733
                                change to
 
734
    @param    create_info       Information from the parsing phase about new
 
735
                                table properties.
 
736
    @param    alter_flags       Bitmask that shows what will be changed
 
737
    @param    table_changes     Shows if table layout has changed (for
 
738
                                backwards compatibility with
 
739
                                check_if_incompatible_data
 
740
 
 
741
    @retval   HA_ALTER_ERROR                Unexpected error
 
742
    @retval   HA_ALTER_SUPPORTED_WAIT_LOCK  Supported, but requires DDL lock
 
743
    @retval   HA_ALTER_SUPPORTED_NO_LOCK    Supported
 
744
    @retval   HA_ALTER_NOT_SUPPORTED        Not supported
 
745
 
 
746
    @note
 
747
      The default implementation is implemented to support fast
 
748
      alter table (storage engines that support some changes by
 
749
      just changing the frm file) without any change in the handler
 
750
      implementation.
 
751
 */
 
752
 virtual int check_if_supported_alter(Table *, HA_CREATE_INFO *create_info,
 
753
                                      HA_ALTER_FLAGS * alter_flags, uint32_t table_changes)
 
754
 {
 
755
   if (this->check_if_incompatible_data(create_info, table_changes)
 
756
       == COMPATIBLE_DATA_NO)
 
757
     return(HA_ALTER_NOT_SUPPORTED);
 
758
   else if ((*alter_flags & HA_ALTER_STORED_VCOL).is_set())
 
759
     return(HA_ALTER_NOT_SUPPORTED);
 
760
   else
 
761
     return(HA_ALTER_SUPPORTED_WAIT_LOCK);
 
762
 }
 
763
 /**
 
764
   Tell storage engine to prepare for the on-line alter table (pre-alter)
 
765
 
 
766
   @param     session               The thread handle
 
767
   @param     altered_table     A temporary table show what table is to
 
768
                                change to
 
769
   @param     alter_info        Storage place for data used during phase1
 
770
                                and phase2
 
771
   @param     alter_flags       Bitmask that shows what will be changed
 
772
 
 
773
   @retval   0      OK
 
774
   @retval   error  error code passed from storage engine
 
775
 */
 
776
 virtual int alter_table_phase1(Session *, Table *, HA_CREATE_INFO *, HA_ALTER_INFO *,
 
777
                                HA_ALTER_FLAGS *)
 
778
 {
 
779
   return HA_ERR_UNSUPPORTED;
 
780
 }
 
781
 /**
 
782
    Tell storage engine to perform the on-line alter table (alter)
 
783
 
 
784
    @param    session               The thread handle
 
785
    @param    altered_table     A temporary table show what table is to
 
786
                                change to
 
787
    @param    alter_info        Storage place for data used during phase1
 
788
                                and phase2
 
789
    @param    alter_flags       Bitmask that shows what will be changed
 
790
 
 
791
    @retval  0      OK
 
792
    @retval  error  error code passed from storage engine
 
793
 
 
794
    @note
 
795
      If check_if_supported_alter returns HA_ALTER_SUPPORTED_WAIT_LOCK
 
796
      this call is to be wrapped with a DDL lock. This is currently NOT
 
797
      supported.
 
798
 */
 
799
 virtual int alter_table_phase2(Session *, Table *, HA_CREATE_INFO *, HA_ALTER_INFO *,
 
800
                                HA_ALTER_FLAGS *)
 
801
 {
 
802
   return HA_ERR_UNSUPPORTED;
 
803
 }
 
804
 /**
 
805
    Tell storage engine that changed frm file is now on disk and table
 
806
    has been re-opened (post-alter)
 
807
 
 
808
    @param    session               The thread handle
 
809
    @param    table             The altered table, re-opened
 
810
 */
 
811
 virtual int alter_table_phase3(Session *, Table *)
 
812
 {
 
813
   return HA_ERR_UNSUPPORTED;
 
814
 }
 
815
 
 
816
  /**
 
817
    use_hidden_primary_key() is called in case of an update/delete when
 
818
    (table_flags() and HA_PRIMARY_KEY_REQUIRED_FOR_DELETE) is defined
 
819
    but we don't have a primary key
 
820
  */
 
821
  virtual void use_hidden_primary_key();
 
822
 
 
823
  /**
 
824
    Lock table.
 
825
 
 
826
    @param    session                     Thread handle
 
827
    @param    lock_type               HA_LOCK_IN_SHARE_MODE     (F_RDLCK)
 
828
                                      HA_LOCK_IN_EXCLUSIVE_MODE (F_WRLCK)
 
829
    @param    lock_timeout            -1 default timeout
 
830
                                      0  no wait
 
831
                                      >0 wait timeout in milliseconds.
 
832
 
 
833
   @note
 
834
      lock_timeout >0 is not used by MySQL currently. If the storage
 
835
      engine does not support NOWAIT (lock_timeout == 0) it should
 
836
      return an error. But if it does not support WAIT X (lock_timeout
 
837
      >0) it should treat it as lock_timeout == -1 and wait a default
 
838
      (or even hard-coded) timeout.
 
839
 
 
840
    @retval HA_ERR_WRONG_COMMAND      Storage engine does not support
 
841
                                      lock_table()
 
842
    @retval HA_ERR_UNSUPPORTED        Storage engine does not support NOWAIT
 
843
    @retval HA_ERR_LOCK_WAIT_TIMEOUT  Lock request timed out or
 
844
                                      lock conflict with NOWAIT option
 
845
    @retval HA_ERR_LOCK_DEADLOCK      Deadlock detected
 
846
  */
 
847
  virtual int lock_table(Session *, int, int)
506
848
  {
507
 
    return false;
 
849
    return HA_ERR_WRONG_COMMAND;
508
850
  }
509
 
 
 
851
  /*
 
852
    This procedure defines if the storage engine supports virtual columns.
 
853
    Default false means "not supported".
 
854
  */
 
855
  virtual bool check_if_supported_virtual_columns(void)
 
856
  { return false; }
510
857
 
511
858
protected:
512
859
  /* Service methods for use by storage engines. */
513
 
  void ha_statistic_increment(uint64_t system_status_var::*offset) const;
 
860
  void ha_statistic_increment(ulong SSV::*offset) const;
514
861
  void **ha_data(Session *) const;
 
862
  Session *ha_session(void) const;
 
863
 
 
864
  /**
 
865
    Default rename_table() and delete_table() rename/delete files with a
 
866
    given name and extensions from bas_ext().
 
867
 
 
868
    These methods can be overridden, but their default implementation
 
869
    provide useful functionality.
 
870
  */
 
871
  virtual int rename_table(const char *from, const char *to);
 
872
  /**
 
873
    Delete a table in the engine. Called for base as well as temporary
 
874
    tables.
 
875
  */
 
876
  virtual int delete_table(const char *name);
515
877
 
516
878
private:
517
879
  /* Private helpers */
518
 
  inline void setTransactionReadWrite();
 
880
  inline void mark_trx_read_write();
519
881
private:
520
882
  /*
521
883
    Low-level primitives for storage engines.  These should be
523
885
    the corresponding 'ha_*' method above.
524
886
  */
525
887
 
526
 
  virtual int open(const char *, int , uint32_t ) { assert(0); return -1; }
527
 
  virtual int doOpen(const identifier::Table &identifier, int mode, uint32_t test_if_locked);
528
 
  virtual int doStartIndexScan(uint32_t idx, bool)
 
888
  virtual int open(const char *name, int mode, uint32_t test_if_locked)=0;
 
889
  virtual int index_init(uint32_t idx, bool)
529
890
  { active_index= idx; return 0; }
530
 
  virtual int doEndIndexScan() { active_index= MAX_KEY; return 0; }
 
891
  virtual int index_end() { active_index= MAX_KEY; return 0; }
531
892
  /**
532
 
    doStartTableScan() can be called two times without doEndTableScan() in between
 
893
    rnd_init() can be called two times without rnd_end() in between
533
894
    (it only makes sense if scan=1).
534
895
    then the second call should prepare for the new table scan (e.g
535
896
    if rnd_init allocates the cursor, second call should position it
536
897
    to the start of the table, no need to deallocate and allocate it again
537
898
  */
538
 
  virtual int doStartTableScan(bool scan) __attribute__ ((warn_unused_result)) = 0;
539
 
  virtual int doEndTableScan() { return 0; }
540
 
  virtual int doInsertRecord(unsigned char *)
541
 
  {
542
 
    return HA_ERR_WRONG_COMMAND;
543
 
  }
544
 
 
545
 
  virtual int doUpdateRecord(const unsigned char *, unsigned char *)
546
 
  {
547
 
    return HA_ERR_WRONG_COMMAND;
548
 
  }
549
 
 
550
 
  virtual int doDeleteRecord(const unsigned char *)
 
899
  virtual int rnd_init(bool scan)= 0;
 
900
  virtual int rnd_end() { return 0; }
 
901
  virtual int write_row(unsigned char *)
 
902
  {
 
903
    return HA_ERR_WRONG_COMMAND;
 
904
  }
 
905
 
 
906
  virtual int update_row(const unsigned char *, unsigned char *)
 
907
  {
 
908
    return HA_ERR_WRONG_COMMAND;
 
909
  }
 
910
 
 
911
  virtual int delete_row(const unsigned char *)
551
912
  {
552
913
    return HA_ERR_WRONG_COMMAND;
553
914
  }
557
918
    by that statement.
558
919
  */
559
920
  virtual int reset() { return 0; }
 
921
  virtual Table_flags table_flags(void) const= 0;
560
922
 
561
923
  /**
562
924
    Is not invoked for non-transactional temporary tables.
563
925
 
564
926
    Tells the storage engine that we intend to read or write data
565
 
    from the table. This call is prefixed with a call to Cursor::store_lock()
566
 
    and is invoked only for those Cursor instances that stored the lock.
 
927
    from the table. This call is prefixed with a call to handler::store_lock()
 
928
    and is invoked only for those handler instances that stored the lock.
567
929
 
568
930
    Calls to rnd_init/index_init are prefixed with this call. When table
569
931
    IO is complete, we call external_lock(F_UNLCK).
584
946
  {
585
947
    return 0;
586
948
  }
587
 
  virtual void release_auto_increment(void) { return; }
 
949
  virtual void release_auto_increment(void) { return; };
588
950
  /** admin commands - called from mysql_admin_table */
589
 
  virtual int check(Session *)
 
951
  virtual int check_for_upgrade(HA_CHECK_OPT *)
 
952
  { return 0; }
 
953
  virtual int check(Session *, HA_CHECK_OPT *)
590
954
  { return HA_ADMIN_NOT_IMPLEMENTED; }
591
955
 
 
956
  /**
 
957
     In this method check_opt can be modified
 
958
     to specify CHECK option to use to call check()
 
959
     upon the table.
 
960
  */
 
961
  virtual int repair(Session *, HA_CHECK_OPT *)
 
962
  { return HA_ADMIN_NOT_IMPLEMENTED; }
592
963
  virtual void start_bulk_insert(ha_rows)
593
964
  {}
594
965
  virtual int end_bulk_insert(void) { return 0; }
596
967
                         uint32_t, enum ha_rkey_function)
597
968
   { return  HA_ERR_WRONG_COMMAND; }
598
969
  virtual int index_read_last(unsigned char *, const unsigned char *, uint32_t)
599
 
   { return (errno= HA_ERR_WRONG_COMMAND); }
 
970
   { return (my_errno= HA_ERR_WRONG_COMMAND); }
 
971
  /**
 
972
    This method is similar to update_row, however the handler doesn't need
 
973
    to execute the updates at this point in time. The handler can be certain
 
974
    that another call to bulk_update_row will occur OR a call to
 
975
    exec_bulk_update before the set of updates in this query is concluded.
 
976
 
 
977
    @param    old_data       Old record
 
978
    @param    new_data       New record
 
979
    @param    dup_key_found  Number of duplicate keys found
 
980
 
 
981
    @retval  0   Bulk delete used by handler
 
982
    @retval  1   Bulk delete not used, normal operation used
 
983
  */
 
984
  virtual int bulk_update_row(const unsigned char *, unsigned char *, uint32_t *)
 
985
  {
 
986
    assert(false);
 
987
    return HA_ERR_WRONG_COMMAND;
 
988
  }
600
989
  /**
601
990
    This is called to delete all rows in a table
602
 
    If the Cursor don't support this, then this function will
 
991
    If the handler don't support this, then this function will
603
992
    return HA_ERR_WRONG_COMMAND and MySQL will delete the rows one
604
993
    by one.
605
994
  */
606
995
  virtual int delete_all_rows(void)
607
 
  { return (errno=HA_ERR_WRONG_COMMAND); }
 
996
  { return (my_errno=HA_ERR_WRONG_COMMAND); }
608
997
  /**
609
998
    Reset the auto-increment counter to the given value, i.e. the next row
610
999
    inserted will get the given value. This is called e.g. after TRUNCATE
613
1002
  */
614
1003
  virtual int reset_auto_increment(uint64_t)
615
1004
  { return HA_ERR_WRONG_COMMAND; }
616
 
 
617
 
  virtual int analyze(Session *)
618
 
  { return HA_ADMIN_NOT_IMPLEMENTED; }
619
 
 
 
1005
  virtual int optimize(Session *, HA_CHECK_OPT *)
 
1006
  { return HA_ADMIN_NOT_IMPLEMENTED; }
 
1007
  virtual int analyze(Session *, HA_CHECK_OPT *)
 
1008
  { return HA_ADMIN_NOT_IMPLEMENTED; }
 
1009
  virtual bool check_and_repair(Session *)
 
1010
  { return true; }
620
1011
  virtual int disable_indexes(uint32_t)
621
1012
  { return HA_ERR_WRONG_COMMAND; }
622
 
 
623
1013
  virtual int enable_indexes(uint32_t)
624
1014
  { return HA_ERR_WRONG_COMMAND; }
625
 
 
626
1015
  virtual int discard_or_import_tablespace(bool)
627
 
  { return (errno=HA_ERR_WRONG_COMMAND); }
628
 
 
629
 
  /* 
630
 
    @todo this is just for the HEAP engine, it should
631
 
    be removed at some point in the future (and
632
 
    no new engine should ever use it). Right
633
 
    now HEAP does rely on it, so we cannot remove it.
634
 
  */
 
1016
  { return (my_errno=HA_ERR_WRONG_COMMAND); }
 
1017
  virtual void prepare_for_alter(void) { return; }
635
1018
  virtual void drop_table(const char *name);
 
1019
  virtual int create(const char *, Table *, HA_CREATE_INFO *)=0;
 
1020
 
 
1021
  virtual int create_handler_files(const char *, const char *, int, HA_CREATE_INFO *)
 
1022
  { return false; }
 
1023
};
 
1024
 
 
1025
 
 
1026
 
 
1027
/**
 
1028
  A Disk-Sweep MRR interface implementation
 
1029
 
 
1030
  This implementation makes range (and, in the future, 'ref') scans to read
 
1031
  table rows in disk sweeps.
 
1032
 
 
1033
  Currently it is used by MyISAM and InnoDB. Potentially it can be used with
 
1034
  any table handler that has non-clustered indexes and on-disk rows.
 
1035
*/
 
1036
 
 
1037
class DsMrr_impl
 
1038
{
 
1039
public:
 
1040
  typedef void (handler::*range_check_toggle_func_t)(bool on);
 
1041
 
 
1042
  DsMrr_impl()
 
1043
    : h2(NULL) {};
 
1044
 
 
1045
  handler *h; /* The "owner" handler object. It is used for scanning the index */
 
1046
  Table *table; /* Always equal to h->table */
 
1047
private:
 
1048
  /*
 
1049
    Secondary handler object. It is used to retrieve full table rows by
 
1050
    calling rnd_pos().
 
1051
  */
 
1052
  handler *h2;
 
1053
 
 
1054
  /* Buffer to store rowids, or (rowid, range_id) pairs */
 
1055
  unsigned char *rowids_buf;
 
1056
  unsigned char *rowids_buf_cur;   /* Current position when reading/writing */
 
1057
  unsigned char *rowids_buf_last;  /* When reading: end of used buffer space */
 
1058
  unsigned char *rowids_buf_end;   /* End of the buffer */
 
1059
 
 
1060
  bool dsmrr_eof; /* true <=> We have reached EOF when reading index tuples */
 
1061
 
 
1062
  /* true <=> need range association, buffer holds {rowid, range_id} pairs */
 
1063
  bool is_mrr_assoc;
 
1064
 
 
1065
  bool use_default_impl; /* true <=> shortcut all calls to default MRR impl */
 
1066
public:
 
1067
  void init(handler *h_arg, Table *table_arg)
 
1068
  {
 
1069
    h= h_arg;
 
1070
    table= table_arg;
 
1071
  }
 
1072
  int dsmrr_init(handler *h, KEY *key, RANGE_SEQ_IF *seq_funcs,
 
1073
                 void *seq_init_param, uint32_t n_ranges, uint32_t mode,
 
1074
                 HANDLER_BUFFER *buf);
 
1075
  void dsmrr_close();
 
1076
  int dsmrr_fill_buffer(handler *h);
 
1077
  int dsmrr_next(handler *h, char **range_info);
 
1078
 
 
1079
  int dsmrr_info(uint32_t keyno, uint32_t n_ranges, uint32_t keys, uint32_t *bufsz,
 
1080
                 uint32_t *flags, COST_VECT *cost);
 
1081
 
 
1082
  ha_rows dsmrr_info_const(uint32_t keyno, RANGE_SEQ_IF *seq,
 
1083
                            void *seq_init_param, uint32_t n_ranges, uint32_t *bufsz,
 
1084
                            uint32_t *flags, COST_VECT *cost);
 
1085
private:
 
1086
  bool key_uses_partial_cols(uint32_t keyno);
 
1087
  bool choose_mrr_impl(uint32_t keyno, ha_rows rows, uint32_t *flags, uint32_t *bufsz,
 
1088
                       COST_VECT *cost);
 
1089
  bool get_disk_sweep_mrr_cost(uint32_t keynr, ha_rows rows, uint32_t flags,
 
1090
                               uint32_t *buffer_size, COST_VECT *cost);
636
1091
};
637
1092
 
638
1093
extern const char *ha_row_type[];
 
1094
extern const char *tx_isolation_names[];
 
1095
extern const char *binlog_format_names[];
 
1096
extern TYPELIB tx_isolation_typelib;
 
1097
extern TYPELIB myisam_stats_method_typelib;
 
1098
extern uint32_t total_ha, total_ha_2pc;
 
1099
 
 
1100
       /* Wrapper functions */
 
1101
#define ha_commit(session) (ha_commit_trans((session), true))
 
1102
#define ha_rollback(session) (ha_rollback_trans((session), true))
639
1103
 
640
1104
/* basic stuff */
641
 
void ha_init_errors(void);
642
 
 
643
 
class SortField;
644
 
SortField *make_unireg_sortorder(Order *order, uint32_t *length,
645
 
                                 SortField *sortorder);
 
1105
int ha_init_errors(void);
 
1106
int ha_init(void);
 
1107
int ha_end(void);
 
1108
int ha_initialize_handlerton(st_plugin_int *plugin);
 
1109
int ha_finalize_handlerton(st_plugin_int *plugin);
 
1110
 
 
1111
TYPELIB *ha_known_exts(void);
 
1112
void ha_close_connection(Session* session);
 
1113
bool ha_flush_logs(handlerton *db_type);
 
1114
void ha_drop_database(char* path);
 
1115
int ha_create_table(Session *session, const char *path,
 
1116
                    const char *db, const char *table_name,
 
1117
                    HA_CREATE_INFO *create_info,
 
1118
                    bool update_create_info);
 
1119
int ha_delete_table(Session *session, const char *path,
 
1120
                    const char *db, const char *alias, bool generate_warning);
 
1121
 
 
1122
/* statistics and info */
 
1123
bool ha_show_status(Session *session, handlerton *db_type, enum ha_stat_type stat);
 
1124
 
 
1125
int ha_find_files(Session *session,const char *db,const char *path,
 
1126
                  const char *wild, bool dir, List<LEX_STRING>* files);
 
1127
int ha_table_exists_in_engine(Session* session, const char* db, const char* name, handlerton **hton= NULL);
 
1128
 
 
1129
/* key cache */
 
1130
extern "C" int ha_init_key_cache(const char *name, KEY_CACHE *key_cache);
 
1131
int ha_resize_key_cache(KEY_CACHE *key_cache);
 
1132
int ha_change_key_cache_param(KEY_CACHE *key_cache);
 
1133
int ha_change_key_cache(KEY_CACHE *old_key_cache, KEY_CACHE *new_key_cache);
 
1134
int ha_end_key_cache(KEY_CACHE *key_cache);
 
1135
 
 
1136
/* report to InnoDB that control passes to the client */
 
1137
int ha_release_temporary_latches(Session *session);
 
1138
 
 
1139
/* transactions: interface to handlerton functions */
 
1140
int ha_start_consistent_snapshot(Session *session);
 
1141
int ha_commit_or_rollback_by_xid(XID *xid, bool commit);
 
1142
int ha_commit_one_phase(Session *session, bool all);
 
1143
int ha_rollback_trans(Session *session, bool all);
 
1144
int ha_prepare(Session *session);
 
1145
int ha_recover(HASH *commit_list);
 
1146
 
 
1147
/* transactions: these functions never call handlerton functions directly */
 
1148
int ha_commit_trans(Session *session, bool all);
 
1149
int ha_autocommit_or_rollback(Session *session, int error);
 
1150
int ha_enable_transaction(Session *session, bool on);
 
1151
 
 
1152
/* savepoints */
 
1153
int ha_rollback_to_savepoint(Session *session, SAVEPOINT *sv);
 
1154
int ha_savepoint(Session *session, SAVEPOINT *sv);
 
1155
int ha_release_savepoint(Session *session, SAVEPOINT *sv);
 
1156
 
 
1157
/* these are called by storage engines */
 
1158
void trans_register_ha(Session *session, bool all, handlerton *ht);
 
1159
 
 
1160
void table_case_convert(char * name, uint32_t length);
 
1161
const char *table_case_name(HA_CREATE_INFO *info, const char *name);
 
1162
 
 
1163
extern char reg_ext[FN_EXTLEN];
 
1164
extern uint32_t reg_ext_length;
 
1165
extern ulong specialflag;
 
1166
extern uint32_t lower_case_table_names;
 
1167
uint32_t filename_to_tablename(const char *from, char *to, uint32_t to_length);
 
1168
uint32_t tablename_to_filename(const char *from, char *to, uint32_t to_length);
 
1169
 
 
1170
 
 
1171
bool mysql_ha_open(Session *session, TableList *tables, bool reopen);
 
1172
bool mysql_ha_close(Session *session, TableList *tables);
 
1173
bool mysql_ha_read(Session *, TableList *,enum enum_ha_read_modes,char *,
 
1174
                   List<Item> *,enum ha_rkey_function,Item *,ha_rows,ha_rows);
 
1175
void mysql_ha_flush(Session *session);
 
1176
void mysql_ha_rm_tables(Session *session, TableList *tables, bool is_locked);
 
1177
void mysql_ha_cleanup(Session *session);
 
1178
 
 
1179
/*
 
1180
  Storage engine has to assume the transaction will end up with 2pc if
 
1181
   - there is more than one 2pc-capable storage engine available
 
1182
   - in the current transaction 2pc was not disabled yet
 
1183
*/
 
1184
#define trans_need_2pc(session, all)                   ((total_ha_2pc > 1) && \
 
1185
        !((all ? &session->transaction.all : &session->transaction.stmt)->no_2pc))
 
1186
 
 
1187
 
 
1188
bool mysql_xa_recover(Session *session);
 
1189
 
 
1190
SORT_FIELD * make_unireg_sortorder(order_st *order, uint32_t *length,
 
1191
                                   SORT_FIELD *sortorder);
646
1192
int setup_order(Session *session, Item **ref_pointer_array, TableList *tables,
647
 
                List<Item> &fields, List <Item> &all_fields, Order *order);
 
1193
                List<Item> &fields, List <Item> &all_fields, order_st *order);
648
1194
int setup_group(Session *session, Item **ref_pointer_array, TableList *tables,
649
 
                List<Item> &fields, List<Item> &all_fields, Order *order,
 
1195
                List<Item> &fields, List<Item> &all_fields, order_st *order,
650
1196
                bool *hidden_group_fields);
651
1197
bool fix_inner_refs(Session *session, List<Item> &all_fields, Select_Lex *select,
652
1198
                    Item **ref_pointer_array);
653
1199
 
654
1200
bool handle_select(Session *session, LEX *lex, select_result *result,
655
1201
                   uint64_t setup_tables_done_option);
 
1202
bool mysql_select(Session *session, Item ***rref_pointer_array,
 
1203
                  TableList *tables, uint32_t wild_num,  List<Item> &list,
 
1204
                  COND *conds, uint32_t og_num, order_st *order, order_st *group,
 
1205
                  Item *having, order_st *proc_param, uint64_t select_type,
 
1206
                  select_result *result, Select_Lex_Unit *unit,
 
1207
                  Select_Lex *select_lex);
656
1208
void free_underlaid_joins(Session *session, Select_Lex *select);
657
 
 
658
 
bool handle_derived(LEX *lex, bool (*processor)(Session *session,
 
1209
bool mysql_explain_union(Session *session, Select_Lex_Unit *unit,
 
1210
                         select_result *result);
 
1211
int mysql_explain_select(Session *session, Select_Lex *sl, char const *type,
 
1212
                         select_result *result);
 
1213
bool mysql_union(Session *session, LEX *lex, select_result *result,
 
1214
                 Select_Lex_Unit *unit, uint64_t setup_tables_done_option);
 
1215
bool mysql_handle_derived(LEX *lex, bool (*processor)(Session *session,
659
1216
                                                      LEX *lex,
660
1217
                                                      TableList *table));
661
 
bool derived_prepare(Session *session, LEX *lex, TableList *t);
662
 
bool derived_filling(Session *session, LEX *lex, TableList *t);
663
 
int prepare_create_field(CreateField *sql_field,
 
1218
bool mysql_derived_prepare(Session *session, LEX *lex, TableList *t);
 
1219
bool mysql_derived_filling(Session *session, LEX *lex, TableList *t);
 
1220
void sp_prepare_create_field(Session *session, Create_field *sql_field);
 
1221
int prepare_create_field(Create_field *sql_field,
664
1222
                         uint32_t *blob_columns,
665
 
                         int *timestamps, int *timestamps_with_niladic);
666
 
 
667
 
bool create_table(Session *session,
668
 
                  const identifier::Table &identifier,
669
 
                  HA_CREATE_INFO *create_info,
670
 
                  message::Table &table_proto,
671
 
                  AlterInfo *alter_info,
672
 
                  bool tmp_table, uint32_t select_field_count,
673
 
                  bool is_if_not_exists);
674
 
 
675
 
bool create_table_no_lock(Session *session,
676
 
                          const identifier::Table &identifier,
677
 
                          HA_CREATE_INFO *create_info,
678
 
                          message::Table &table_proto,
679
 
                          AlterInfo *alter_info,
680
 
                          bool tmp_table, uint32_t select_field_count,
681
 
                          bool is_if_not_exists);
682
 
 
683
 
bool create_like_table(Session* session,
684
 
                       identifier::Table::const_reference destination_identifier,
685
 
                       identifier::Table::const_reference source_identifier,
686
 
                       message::Table &create_table_proto,
687
 
                       bool is_if_not_exists,
688
 
                       bool is_engine_set);
689
 
 
690
 
bool rename_table(Session &session,
691
 
                        plugin::StorageEngine *base,
692
 
                        const identifier::Table &old_identifier,
693
 
                        const identifier::Table &new_identifier);
694
 
 
695
 
bool prepare_update(Session *session, TableList *table_list,
696
 
                          Item **conds, uint32_t order_num, Order *order);
697
 
int update_query(Session *session,TableList *tables,List<Item> &fields,
 
1223
                         int *timestamps, int *timestamps_with_niladic,
 
1224
                         int64_t table_flags);
 
1225
bool mysql_create_table(Session *session,const char *db, const char *table_name,
 
1226
                        HA_CREATE_INFO *create_info,
 
1227
                        Alter_info *alter_info,
 
1228
                        bool tmp_table, uint32_t select_field_count);
 
1229
bool mysql_create_table_no_lock(Session *session, const char *db,
 
1230
                                const char *table_name,
 
1231
                                HA_CREATE_INFO *create_info,
 
1232
                                Alter_info *alter_info,
 
1233
                                bool tmp_table, uint32_t select_field_count,
 
1234
                                bool lock_open_lock);
 
1235
 
 
1236
bool mysql_alter_table(Session *session, char *new_db, char *new_name,
 
1237
                       HA_CREATE_INFO *create_info,
 
1238
                       TableList *table_list,
 
1239
                       Alter_info *alter_info,
 
1240
                       uint32_t order_num, order_st *order, bool ignore);
 
1241
bool mysql_recreate_table(Session *session, TableList *table_list);
 
1242
bool mysql_create_like_table(Session *session, TableList *table,
 
1243
                             TableList *src_table,
 
1244
                             HA_CREATE_INFO *create_info);
 
1245
bool mysql_rename_table(handlerton *base, const char *old_db,
 
1246
                        const char * old_name, const char *new_db,
 
1247
                        const char * new_name, uint32_t flags);
 
1248
bool mysql_prepare_update(Session *session, TableList *table_list,
 
1249
                          Item **conds, uint32_t order_num, order_st *order);
 
1250
int mysql_update(Session *session,TableList *tables,List<Item> &fields,
698
1251
                 List<Item> &values,COND *conds,
699
 
                 uint32_t order_num, Order *order, ha_rows limit,
 
1252
                 uint32_t order_num, order_st *order, ha_rows limit,
700
1253
                 enum enum_duplicates handle_duplicates, bool ignore);
701
 
bool prepare_insert(Session *session, TableList *table_list, Table *table,
 
1254
bool mysql_multi_update(Session *session, TableList *table_list,
 
1255
                        List<Item> *fields, List<Item> *values,
 
1256
                        COND *conds, uint64_t options,
 
1257
                        enum enum_duplicates handle_duplicates, bool ignore,
 
1258
                        Select_Lex_Unit *unit, Select_Lex *select_lex);
 
1259
bool mysql_prepare_insert(Session *session, TableList *table_list, Table *table,
702
1260
                          List<Item> &fields, List_item *values,
703
1261
                          List<Item> &update_fields,
704
1262
                          List<Item> &update_values, enum_duplicates duplic,
705
1263
                          COND **where, bool select_insert,
706
1264
                          bool check_fields, bool abort_on_warning);
707
 
bool insert_query(Session *session,TableList *table,List<Item> &fields,
 
1265
bool mysql_insert(Session *session,TableList *table,List<Item> &fields,
708
1266
                  List<List_item> &values, List<Item> &update_fields,
709
1267
                  List<Item> &update_values, enum_duplicates flag,
710
1268
                  bool ignore);
711
1269
int check_that_all_fields_are_given_values(Session *session, Table *entry,
712
1270
                                           TableList *table_list);
713
 
int prepare_delete(Session *session, TableList *table_list, Item **conds);
714
 
bool delete_query(Session *session, TableList *table_list, COND *conds,
 
1271
int mysql_prepare_delete(Session *session, TableList *table_list, Item **conds);
 
1272
bool mysql_delete(Session *session, TableList *table_list, COND *conds,
715
1273
                  SQL_LIST *order, ha_rows rows, uint64_t options,
716
1274
                  bool reset_auto_increment);
717
 
bool truncate(Session& session, TableList *table_list);
718
 
TableShare *get_table_share(Session *session, TableList *table_list, char *key,
 
1275
bool mysql_truncate(Session *session, TableList *table_list, bool dont_send_ok);
 
1276
uint32_t create_table_def_key(Session *session, char *key, TableList *table_list,
 
1277
                              bool tmp_table);
 
1278
TABLE_SHARE *get_table_share(Session *session, TableList *table_list, char *key,
719
1279
                             uint32_t key_length, uint32_t db_flags, int *error);
720
 
TableShare *get_cached_table_share(const char *db, const char *table_name);
 
1280
void release_table_share(TABLE_SHARE *share, enum release_type type);
 
1281
TABLE_SHARE *get_cached_table_share(const char *db, const char *table_name);
 
1282
Table *open_ltable(Session *session, TableList *table_list, thr_lock_type update,
 
1283
                   uint32_t lock_flags);
 
1284
Table *open_table(Session *session, TableList *table_list, bool *refresh, uint32_t flags);
 
1285
bool name_lock_locked_table(Session *session, TableList *tables);
721
1286
bool reopen_name_locked_table(Session* session, TableList* table_list, bool link_in);
 
1287
Table *table_cache_insert_placeholder(Session *session, const char *key,
 
1288
                                      uint32_t key_length);
 
1289
bool lock_table_name_if_not_cached(Session *session, const char *db,
 
1290
                                   const char *table_name, Table **table);
 
1291
Table *find_locked_table(Session *session, const char *db,const char *table_name);
 
1292
void detach_merge_children(Table *table, bool clear_refs);
 
1293
bool fix_merge_after_open(TableList *old_child_list, TableList **old_last,
 
1294
                          TableList *new_child_list, TableList **new_last);
 
1295
bool reopen_table(Table *table);
722
1296
bool reopen_tables(Session *session,bool get_locks,bool in_refresh);
 
1297
void close_data_files_and_morph_locks(Session *session, const char *db,
 
1298
                                      const char *table_name);
723
1299
void close_handle_and_leave_table_as_lock(Table *table);
 
1300
bool open_new_frm(Session *session, TABLE_SHARE *share, const char *alias,
 
1301
                  uint32_t db_stat, uint32_t prgflag,
 
1302
                  uint32_t ha_open_flags, Table *outparam,
 
1303
                  TableList *table_desc, MEM_ROOT *mem_root);
724
1304
bool wait_for_tables(Session *session);
725
1305
bool table_is_used(Table *table, bool wait_for_name_lock);
726
 
Table *drop_locked_tables(Session *session, const drizzled::identifier::Table &identifier);
727
 
void abort_locked_tables(Session *session, const drizzled::identifier::Table &identifier);
 
1306
Table *drop_locked_tables(Session *session,const char *db, const char *table_name);
 
1307
void abort_locked_tables(Session *session,const char *db, const char *table_name);
 
1308
void execute_init_command(Session *session, sys_var_str *init_command_var,
 
1309
                          pthread_rwlock_t *var_mutex);
728
1310
extern Field *not_found_field;
729
1311
extern Field *view_ref_found;
730
1312
 
732
1314
find_field_in_tables(Session *session, Item_ident *item,
733
1315
                     TableList *first_table, TableList *last_table,
734
1316
                     Item **ref, find_item_error_report_type report_error,
735
 
                     bool register_tree_change);
 
1317
                     bool check_privileges, bool register_tree_change);
736
1318
Field *
737
1319
find_field_in_table_ref(Session *session, TableList *table_list,
738
1320
                        const char *name, uint32_t length,
739
1321
                        const char *item_name, const char *db_name,
740
1322
                        const char *table_name, Item **ref,
741
 
                        bool allow_rowid,
 
1323
                        bool check_privileges, bool allow_rowid,
742
1324
                        uint32_t *cached_field_index_ptr,
743
1325
                        bool register_tree_change, TableList **actual_table);
744
1326
Field *
745
1327
find_field_in_table(Session *session, Table *table, const char *name, uint32_t length,
746
1328
                    bool allow_rowid, uint32_t *cached_field_index_ptr);
747
 
 
748
 
} /* namespace drizzled */
749
 
 
750
 
#endif /* DRIZZLED_CURSOR_H */
 
1329
Field *
 
1330
find_field_in_table_sef(Table *table, const char *name);
 
1331
int update_virtual_fields_marked_for_write(Table *table,
 
1332
                                           bool ignore_stored=true);
 
1333
 
 
1334
 
 
1335
#endif /* DRIZZLED_HANDLER_H */