~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/handler.h

  • Committer: Brian Aker
  • Date: 2009-08-18 07:19:56 UTC
  • mfrom: (1116.1.3 stewart)
  • mto: This revision was merged to the branch mainline in revision 1118.
  • Revision ID: brian@gaz-20090818071956-nfpoe9rp3i7p50kx
Merge my branch from Stewart into one branch

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 <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>
27
34
#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
#include <drizzled/atomics.h>
 
36
 
 
37
#include <drizzled/message/table.pb.h>
 
38
 
 
39
/* Bits to show what an alter table will do */
 
40
#include <drizzled/sql_bitmap.h>
 
41
 
 
42
#include <drizzled/handler.h>
35
43
 
36
44
#include <bitset>
37
45
#include <algorithm>
38
46
 
39
 
#include <drizzled/visibility.h>
40
 
 
41
 
namespace drizzled
42
 
{
43
 
 
44
47
#define HA_MAX_ALTER_FLAGS 40
45
 
 
46
48
typedef std::bitset<HA_MAX_ALTER_FLAGS> HA_ALTER_FLAGS;
47
49
 
48
 
class AlterInfo;
49
 
class CreateField;
50
 
class ForeignKeyInfo;
 
50
extern drizzled::atomic<uint32_t> refresh_version;  /* Increments on each reload */
 
51
 
 
52
 
 
53
typedef bool (*qc_engine_callback)(Session *session, char *table_key,
 
54
                                      uint32_t key_length,
 
55
                                      uint64_t *engine_data);
 
56
 
 
57
 
 
58
/* The handler for a table type.  Will be included in the Table structure */
 
59
 
 
60
class Table;
 
61
class TableList;
 
62
class TableShare;
 
63
class Select_Lex_Unit;
 
64
struct st_foreign_key_info;
 
65
typedef struct st_foreign_key_info FOREIGN_KEY_INFO;
 
66
struct order_st;
 
67
 
51
68
class Item;
52
 
class Item_ident;
 
69
struct st_table_log_memory_entry;
 
70
 
53
71
class LEX;
54
72
class Select_Lex;
55
 
class Select_Lex_Unit;
56
 
class String;
57
 
class Table;
58
 
class TableList;
59
 
class TableShare;
 
73
class Alter_info;
60
74
class select_result;
 
75
class CreateField;
61
76
class sys_var_str;
62
 
struct Order;
 
77
class Item_ident;
 
78
typedef struct st_sort_field SORT_FIELD;
63
79
 
64
80
typedef List<Item> List_item;
 
81
 
 
82
typedef struct st_savepoint SAVEPOINT;
 
83
extern uint32_t savepoint_alloc_size;
65
84
extern KEY_CREATE_INFO default_key_create_info;
66
85
 
67
86
/* Forward declaration for condition pushdown to storage engine */
68
87
typedef class Item COND;
69
88
 
70
 
typedef struct system_status_var system_status_var;
71
 
 
72
 
namespace optimizer { class CostVector; }
73
 
namespace plugin { class StorageEngine; }
74
 
 
 
89
typedef struct system_status_var SSV;
 
90
 
 
91
class COST_VECT;
 
92
 
 
93
uint32_t calculate_key_len(Table *, uint, const unsigned char *, key_part_map);
75
94
/*
76
95
  bitmap with first N+1 bits set
77
96
  (keypart_map for a key prefix of [0..N] keyparts)
93
112
}
94
113
 
95
114
/**
96
 
  The Cursor class is the interface for dynamically loadable
 
115
  The handler class is the interface for dynamically loadable
97
116
  storage engines. Do not add ifdefs and take care when adding or
98
117
  changing virtual functions to avoid vtable confusion
99
118
 
103
122
     storage engine
104
123
 
105
124
  2. KeyTupleFormat - used to pass index search tuples (aka "keys") to
106
 
     storage engine. See optimizer/range.cc for description of this format.
 
125
     storage engine. See opt_range.cc for description of this format.
107
126
 
108
127
  TableRecordFormat
109
128
  =================
128
147
  present, its length is one byte <not-sure> which must be set to 0xFF
129
148
  at all times. </not-sure>
130
149
 
 
150
  If the table has columns of type BIT, then certain bits from those columns
 
151
  may be stored in null_bytes as well. Grep around for Field_bit for
 
152
  details.
 
153
 
131
154
  For blob columns (see Field_blob), the record buffer stores length of the
132
155
  data, following by memory pointer to the blob data. The pointer is owned
133
156
  by the storage engine and is valid until the next operation.
135
158
  If a blob column has NULL value, then its length and blob data pointer
136
159
  must be set to 0.
137
160
*/
138
 
class DRIZZLED_API Cursor
 
161
 
 
162
class handler :public Sql_alloc
139
163
{
140
 
  friend class SEAPITesterCursor;
141
 
  Table &table;               /* The current open table */
142
 
  plugin::StorageEngine &engine;      /* storage engine of this Cursor */
 
164
public:
 
165
  typedef uint64_t Table_flags;
143
166
 
144
167
protected:
 
168
  TableShare *table_share;   /* The table definition */
 
169
  Table *table;               /* The current open table */
 
170
  Table_flags cached_table_flags;       /* Set on init() and open() */
 
171
 
145
172
  ha_rows estimation_rows_to_insert;
146
 
 
147
173
public:
148
 
  inline plugin::StorageEngine *getEngine() const       /* table_type for handler */
149
 
  {
150
 
    return &engine;
151
 
  }
152
 
  unsigned char *ref;                           /* Pointer to current row */
 
174
  StorageEngine *engine;      /* storage engine of this handler */
 
175
  unsigned char *ref;                           /* Pointer to current row */
153
176
  unsigned char *dup_ref;                       /* Pointer to duplicate row */
154
177
 
155
 
  TableShare *getShare();
156
 
 
157
 
  Table *getTable() const
158
 
  {
159
 
    return &table;
160
 
  }
161
 
 
162
178
  ha_statistics stats;
163
179
  /** MultiRangeRead-related members: */
164
180
  range_seq_t mrr_iter;    /* Interator to traverse the range sequence */
165
181
  RANGE_SEQ_IF mrr_funcs;  /* Range sequence traversal functions */
166
 
 
 
182
  HANDLER_BUFFER *multi_range_buffer; /* MRR buffer info */
167
183
  uint32_t ranges_in_seq; /* Total number of ranges in the traversed sequence */
168
184
  /* true <=> source MRR ranges and the output are ordered */
169
185
  bool mrr_is_output_sorted;
172
188
  bool mrr_have_range;
173
189
 
174
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;
175
196
 
176
197
  /** Current range (the one we're now returning rows from) */
177
198
  KEY_MULTI_RANGE mrr_cur_range;
178
199
 
179
200
  /** The following are for read_range() */
180
201
  key_range save_end_range, *end_range;
181
 
  KeyPartInfo *range_key_part;
 
202
  KEY_PART_INFO *range_key_part;
182
203
  int key_compare_result_on_equal;
183
204
 
184
205
  uint32_t errkey;                              /* Last dup key */
188
209
  uint32_t ref_length;
189
210
  enum {NONE=0, INDEX, RND} inited;
190
211
  bool locked;
 
212
  bool implicit_emptied;                /* Can be !=0 only if HEAP */
191
213
 
192
214
  /**
193
215
    next_insert_id is the next value which should be inserted into the
199
221
    get_auto_increment().
200
222
  */
201
223
  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
224
  /**
216
225
    insert id for the current row (*autogenerated*; if not
217
226
    autogenerated, it's 0).
225
234
  */
226
235
  Discrete_interval auto_inc_interval_for_cur_row;
227
236
 
228
 
  Cursor(plugin::StorageEngine &engine_arg, Table &share_arg);
229
 
  virtual ~Cursor(void);
230
 
  virtual Cursor *clone(memory::Root *mem_root);
 
237
  handler(StorageEngine *engine_arg, TableShare *share_arg)
 
238
    :table_share(share_arg), table(0),
 
239
    estimation_rows_to_insert(0), engine(engine_arg),
 
240
    ref(0), in_range_check_pushed_down(false),
 
241
    key_used_on_scan(MAX_KEY), active_index(MAX_KEY),
 
242
    ref_length(sizeof(my_off_t)),
 
243
    inited(NONE),
 
244
    locked(false), implicit_emptied(0),
 
245
    next_insert_id(0), insert_id_for_cur_row(0)
 
246
    {}
 
247
  virtual ~handler(void);
 
248
  virtual handler *clone(MEM_ROOT *mem_root);
 
249
  /** This is called after create to allow us to set up cached variables */
 
250
  void init()
 
251
  {
 
252
    cached_table_flags= table_flags();
 
253
  }
231
254
 
232
255
  /* ha_ methods: pubilc wrappers for private virtual API */
233
256
 
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();
 
257
  int ha_open(Table *table, const char *name, int mode, int test_if_locked);
 
258
  int ha_index_init(uint32_t idx, bool sorted);
 
259
  int ha_index_end();
 
260
  int ha_rnd_init(bool scan);
 
261
  int ha_rnd_end();
239
262
  int ha_reset();
240
263
 
241
264
  /* this is necessary in many places, e.g. in HANDLER command */
242
265
  int ha_index_or_rnd_end();
 
266
  Table_flags ha_table_flags() const;
243
267
 
244
268
  /**
245
269
    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.
 
270
    handler class, hence they are *not* virtual. For the inheritance
 
271
    interface, see the (private) functions write_row(), update_row(),
 
272
    and delete_row() below.
249
273
  */
250
274
  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));
 
275
  int ha_write_row(unsigned char * buf);
 
276
  int ha_update_row(const unsigned char * old_data, unsigned char * new_data);
 
277
  int ha_delete_row(const unsigned char * buf);
254
278
  void ha_release_auto_increment();
255
279
 
256
280
  /** to be actually called to get 'check()' functionality*/
258
282
 
259
283
  void ha_start_bulk_insert(ha_rows rows);
260
284
  int ha_end_bulk_insert();
 
285
  int ha_bulk_update_row(const unsigned char *old_data, unsigned char *new_data,
 
286
                         uint32_t *dup_key_found);
261
287
  int ha_delete_all_rows();
262
288
  int ha_reset_auto_increment(uint64_t value);
 
289
  int ha_optimize(Session* session, HA_CHECK_OPT* check_opt);
263
290
  int ha_analyze(Session* session, HA_CHECK_OPT* check_opt);
264
291
 
265
292
  int ha_disable_indexes(uint32_t mode);
266
293
  int ha_enable_indexes(uint32_t mode);
267
294
  int ha_discard_or_import_tablespace(bool discard);
268
 
  void closeMarkForDelete(const char *name);
 
295
  void ha_drop_table(const char *name);
269
296
 
270
297
  void adjust_next_insert_id_after_explicit_value(uint64_t nr);
271
298
  int update_auto_increment();
 
299
  void print_keydup_error(uint32_t key_nr, const char *msg);
 
300
  virtual void print_error(int error, myf errflag);
 
301
  virtual bool get_error_message(int error, String *buf);
 
302
  uint32_t get_dup_key(int error);
 
303
  virtual void change_table_ptr(Table *table_arg, TableShare *share);
272
304
 
273
305
  /* Estimates calculation */
274
306
  virtual double scan_time(void)
275
 
  { return static_cast<double>(stats.data_file_length) / IO_SIZE + 2; }
 
307
  { return uint64_t2double(stats.data_file_length) / IO_SIZE + 2; }
276
308
  virtual double read_time(uint32_t, uint32_t ranges, ha_rows rows)
277
 
  { return ranges + rows; }
 
309
  { return rows2double(ranges+rows); }
278
310
 
279
311
  virtual double index_only_read_time(uint32_t keynr, double records);
280
312
 
281
313
  virtual ha_rows multi_range_read_info_const(uint32_t keyno, RANGE_SEQ_IF *seq,
282
314
                                              void *seq_init_param,
283
315
                                              uint32_t n_ranges, uint32_t *bufsz,
284
 
                                              uint32_t *flags, optimizer::CostVector *cost);
 
316
                                              uint32_t *flags, COST_VECT *cost);
285
317
  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);
 
318
                                    uint32_t *bufsz, uint32_t *flags, COST_VECT *cost);
287
319
  virtual int multi_range_read_init(RANGE_SEQ_IF *seq, void *seq_init_param,
288
 
                                    uint32_t n_ranges, uint32_t mode);
 
320
                                    uint32_t n_ranges, uint32_t mode,
 
321
                                    HANDLER_BUFFER *buf);
289
322
  virtual int multi_range_read_next(char **range_info);
290
323
 
291
324
 
295
328
  /**
296
329
    This method is used to analyse the error to see whether the error
297
330
    is ignorable or not, certain handlers can have more error that are
298
 
    ignorable than others. E.g. the partition Cursor can get inserts
 
331
    ignorable than others. E.g. the partition handler can get inserts
299
332
    into a range where there is no partition and this is an ignorable
300
333
    error.
301
334
    HA_ERR_FOUND_DUP_UNIQUE is a special case in MyISAM that means the
309
342
    (table_flags() & (HA_HAS_RECORDS | HA_STATS_RECORDS_IS_EXACT)) != 0
310
343
  */
311
344
  virtual ha_rows records();
312
 
  virtual uint64_t tableSize();
313
 
  virtual uint64_t rowSize();
314
345
  /**
315
346
    Return upper bound of current number of records in the table
316
347
    (max. of how many records one will retrieve when doing a full table scan)
320
351
  virtual ha_rows estimate_rows_upper_bound()
321
352
  { return stats.records+EXTRA_RECORDS; }
322
353
 
 
354
  /**
 
355
    Get the row type from the storage engine.  If this method returns
 
356
    ROW_TYPE_NOT_USED, the information in HA_CREATE_INFO should be used.
 
357
  */
 
358
  virtual enum row_type get_row_type() const { return ROW_TYPE_NOT_USED; }
 
359
 
323
360
  virtual const char *index_type(uint32_t)
324
361
  { assert(0); return "";}
325
362
 
328
365
  virtual int close(void)=0;
329
366
 
330
367
  /**
 
368
    @retval  0   Bulk update used by handler
 
369
    @retval  1   Bulk update not used, normal operation used
 
370
  */
 
371
  virtual bool start_bulk_update() { return 1; }
 
372
  /**
 
373
    @retval  0   Bulk delete used by handler
 
374
    @retval  1   Bulk delete not used, normal operation used
 
375
  */
 
376
  virtual bool start_bulk_delete() { return 1; }
 
377
  /**
 
378
    After this call all outstanding updates must be performed. The number
 
379
    of duplicate key errors are reported in the duplicate key parameter.
 
380
    It is allowed to continue to the batched update after this call, the
 
381
    handler has to wait until end_bulk_update with changing state.
 
382
 
 
383
    @param    dup_key_found       Number of duplicate keys found
 
384
 
 
385
    @retval  0           Success
 
386
    @retval  >0          Error code
 
387
  */
 
388
  virtual int exec_bulk_update(uint32_t *)
 
389
  {
 
390
    assert(false);
 
391
    return HA_ERR_WRONG_COMMAND;
 
392
  }
 
393
  /**
 
394
    Perform any needed clean-up, no outstanding updates are there at the
 
395
    moment.
 
396
  */
 
397
  virtual void end_bulk_update() { return; }
 
398
  /**
 
399
    Execute all outstanding deletes and close down the bulk delete.
 
400
 
 
401
    @retval 0             Success
 
402
    @retval >0            Error code
 
403
  */
 
404
  virtual int end_bulk_delete()
 
405
  {
 
406
    assert(false);
 
407
    return HA_ERR_WRONG_COMMAND;
 
408
  }
 
409
  /**
331
410
     @brief
332
411
     Positions an index cursor to the index specified in the handle. Fetches the
333
412
     row if available. If the key value is null, begin at the first key of the
334
413
     index.
335
414
  */
336
 
  virtual int index_read_map(unsigned char * buf, const unsigned char *key,
 
415
  virtual int index_read_map(unsigned char * buf, const unsigned char * key,
337
416
                             key_part_map keypart_map,
338
417
                             enum ha_rkey_function find_flag)
339
418
  {
340
 
    uint32_t key_len= calculate_key_len(active_index, keypart_map);
 
419
    uint32_t key_len= calculate_key_len(table, active_index, key, keypart_map);
341
420
    return  index_read(buf, key, key_len, find_flag);
342
421
  }
343
422
  /**
350
429
                                 const unsigned char * key,
351
430
                                 key_part_map keypart_map,
352
431
                                 enum ha_rkey_function find_flag);
353
 
  virtual int index_next(unsigned char *) __attribute__ ((warn_unused_result))
 
432
  virtual int index_next(unsigned char *)
354
433
   { return  HA_ERR_WRONG_COMMAND; }
355
434
  virtual int index_prev(unsigned char *)
356
435
   { return  HA_ERR_WRONG_COMMAND; }
359
438
  virtual int index_last(unsigned char *)
360
439
   { return  HA_ERR_WRONG_COMMAND; }
361
440
  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
441
  /**
368
442
     @brief
369
443
     The following functions works like index_read, but it find the last
372
446
  virtual int index_read_last_map(unsigned char * buf, const unsigned char * key,
373
447
                                  key_part_map keypart_map)
374
448
  {
375
 
    uint32_t key_len= calculate_key_len(active_index, keypart_map);
 
449
    uint32_t key_len= calculate_key_len(table, active_index, key, keypart_map);
376
450
    return index_read_last(buf, key, key_len);
377
451
  }
378
452
  virtual int read_range_first(const key_range *start_key,
380
454
                               bool eq_range, bool sorted);
381
455
  virtual int read_range_next();
382
456
  int compare_key(key_range *range);
 
457
  int compare_key2(key_range *range);
383
458
  virtual int rnd_next(unsigned char *)=0;
384
459
  virtual int rnd_pos(unsigned char *, unsigned char *)=0;
 
460
  /**
 
461
    One has to use this method when to find
 
462
    random position by record as the plain
 
463
    position() call doesn't work for some
 
464
    handlers for random position.
 
465
  */
 
466
  virtual int rnd_pos_by_record(unsigned char *record);
385
467
  virtual int read_first_row(unsigned char *buf, uint32_t primary_key);
 
468
  /**
 
469
    The following function is only needed for tables that may be temporary
 
470
    tables during joins.
 
471
  */
 
472
  virtual int restart_rnd_next(unsigned char *, unsigned char *)
 
473
    { return HA_ERR_WRONG_COMMAND; }
386
474
  virtual int rnd_same(unsigned char *, uint32_t)
387
475
    { return HA_ERR_WRONG_COMMAND; }
388
476
  virtual ha_rows records_in_range(uint32_t, key_range *, key_range *)
417
505
  */
418
506
  virtual void try_semi_consistent_read(bool) {}
419
507
  virtual void unlock_row(void) {}
 
508
  virtual int start_stmt(Session *, thr_lock_type)
 
509
  {return 0;}
420
510
  virtual void get_auto_increment(uint64_t offset, uint64_t increment,
421
511
                                  uint64_t nb_desired_values,
422
512
                                  uint64_t *first_value,
423
 
                                  uint64_t *nb_reserved_values)= 0;
424
 
 
 
513
                                  uint64_t *nb_reserved_values);
425
514
  void set_next_insert_id(uint64_t id)
426
515
  {
427
516
    next_insert_id= id;
442
531
      insert_id_for_cur_row;
443
532
  }
444
533
 
 
534
  virtual void update_create_info(HA_CREATE_INFO *) {}
 
535
  int check_old_types(void);
445
536
  /* end of the list of admin commands */
446
537
 
447
538
  virtual int indexes_are_disabled(void) {return 0;}
464
555
   */
465
556
  virtual bool can_switch_engines(void) { return true; }
466
557
  /** used in REPLACE; is > 0 if table is referred by a FOREIGN KEY */
467
 
  virtual int get_foreign_key_list(Session *, List<ForeignKeyInfo> *)
 
558
  virtual int get_foreign_key_list(Session *, List<FOREIGN_KEY_INFO> *)
468
559
  { return 0; }
469
560
  virtual uint32_t referenced_by_foreign_key() { return 0;}
470
561
  virtual void free_foreign_key_create_info(char *) {}
 
562
  /** The following can be called without an open handler */
 
563
 
 
564
  virtual uint32_t index_flags(uint32_t idx, uint32_t part, bool all_parts) const =0;
 
565
 
 
566
  virtual int add_index(Table *, KEY *, uint32_t)
 
567
  { return (HA_ERR_WRONG_COMMAND); }
 
568
  virtual int prepare_drop_index(Table *, uint32_t *, uint32_t)
 
569
  { return (HA_ERR_WRONG_COMMAND); }
 
570
  virtual int final_drop_index(Table *)
 
571
  { return (HA_ERR_WRONG_COMMAND); }
 
572
 
 
573
  uint32_t max_record_length() const
 
574
  { return std::min((unsigned int)HA_MAX_REC_LENGTH, max_supported_record_length()); }
 
575
  uint32_t max_keys() const
 
576
  { return std::min((unsigned int)MAX_KEY, max_supported_keys()); }
 
577
  uint32_t max_key_parts() const
 
578
  { return std::min((unsigned int)MAX_REF_PARTS, max_supported_key_parts()); }
 
579
  uint32_t max_key_length() const
 
580
  { return std::min((unsigned int)MAX_KEY_LENGTH, max_supported_key_length()); }
 
581
  uint32_t max_key_part_length(void) const
 
582
  { return std::min((unsigned int)MAX_KEY_LENGTH, max_supported_key_part_length()); }
 
583
 
 
584
  virtual uint32_t max_supported_record_length(void) const
 
585
  { return HA_MAX_REC_LENGTH; }
 
586
  virtual uint32_t max_supported_keys(void) const { return 0; }
 
587
  virtual uint32_t max_supported_key_parts(void) const { return MAX_REF_PARTS; }
 
588
  virtual uint32_t max_supported_key_length(void) const { return MAX_KEY_LENGTH; }
 
589
  virtual uint32_t max_supported_key_part_length(void) const { return 255; }
 
590
 
 
591
  virtual bool low_byte_first(void) const { return 1; }
 
592
  virtual uint32_t checksum(void) const { return 0; }
 
593
  virtual bool is_crashed(void) const  { return 0; }
 
594
  virtual bool auto_repair(void) const { return 0; }
471
595
 
472
596
  /**
473
597
    Is not invoked for non-transactional temporary tables.
476
600
    or partitioned.
477
601
 
478
602
    @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().
 
603
    refer to a different thread if called from mysql_lock_abort_for_thread().
480
604
 
481
605
    @note If the table is MERGE, store_lock() can return less locks
482
606
    than lock_count() claimed. This can happen when the MERGE children
483
607
    are not attached when this is called from another thread.
484
608
  */
485
 
  virtual THR_LOCK_DATA **store_lock(Session *,
 
609
  virtual THR_LOCK_DATA **store_lock(Session *session,
486
610
                                     THR_LOCK_DATA **to,
487
 
                                     enum thr_lock_type)
488
 
  {
489
 
    assert(0); // Impossible programming situation
490
 
 
491
 
    return(to);
492
 
  }
 
611
                                     enum thr_lock_type lock_type)=0;
493
612
 
494
613
 /*
495
614
   @retval true   Primary key (if there is one) is clustered
502
621
   return memcmp(ref1, ref2, ref_length);
503
622
 }
504
623
 
505
 
  virtual bool isOrdered(void)
 
624
  /**
 
625
    Lock table.
 
626
 
 
627
    @param    session                     Thread handle
 
628
    @param    lock_type               HA_LOCK_IN_SHARE_MODE     (F_RDLCK)
 
629
                                      HA_LOCK_IN_EXCLUSIVE_MODE (F_WRLCK)
 
630
    @param    lock_timeout            -1 default timeout
 
631
                                      0  no wait
 
632
                                      >0 wait timeout in milliseconds.
 
633
 
 
634
   @note
 
635
      lock_timeout >0 is not used by MySQL currently. If the storage
 
636
      engine does not support NOWAIT (lock_timeout == 0) it should
 
637
      return an error. But if it does not support WAIT X (lock_timeout
 
638
      >0) it should treat it as lock_timeout == -1 and wait a default
 
639
      (or even hard-coded) timeout.
 
640
 
 
641
    @retval HA_ERR_WRONG_COMMAND      Storage engine does not support
 
642
                                      lock_table()
 
643
    @retval HA_ERR_UNSUPPORTED        Storage engine does not support NOWAIT
 
644
    @retval HA_ERR_LOCK_WAIT_TIMEOUT  Lock request timed out or
 
645
                                      lock conflict with NOWAIT option
 
646
    @retval HA_ERR_LOCK_DEADLOCK      Deadlock detected
 
647
  */
 
648
  virtual int lock_table(Session *, int, int)
506
649
  {
507
 
    return false;
 
650
    return HA_ERR_WRONG_COMMAND;
508
651
  }
509
652
 
510
 
 
511
653
protected:
512
654
  /* Service methods for use by storage engines. */
513
 
  void ha_statistic_increment(uint64_t system_status_var::*offset) const;
 
655
  void ha_statistic_increment(ulong SSV::*offset) const;
514
656
  void **ha_data(Session *) const;
 
657
  Session *ha_session(void) const;
515
658
 
516
659
private:
517
660
  /* Private helpers */
518
 
  inline void setTransactionReadWrite();
 
661
  inline void mark_trx_read_write();
519
662
private:
520
663
  /*
521
664
    Low-level primitives for storage engines.  These should be
523
666
    the corresponding 'ha_*' method above.
524
667
  */
525
668
 
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)
 
669
  virtual int open(const char *name, int mode, uint32_t test_if_locked)=0;
 
670
  virtual int index_init(uint32_t idx, bool)
529
671
  { active_index= idx; return 0; }
530
 
  virtual int doEndIndexScan() { active_index= MAX_KEY; return 0; }
 
672
  virtual int index_end() { active_index= MAX_KEY; return 0; }
531
673
  /**
532
 
    doStartTableScan() can be called two times without doEndTableScan() in between
 
674
    rnd_init() can be called two times without rnd_end() in between
533
675
    (it only makes sense if scan=1).
534
676
    then the second call should prepare for the new table scan (e.g
535
677
    if rnd_init allocates the cursor, second call should position it
536
678
    to the start of the table, no need to deallocate and allocate it again
537
679
  */
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 *)
 
680
  virtual int rnd_init(bool scan)= 0;
 
681
  virtual int rnd_end() { return 0; }
 
682
  virtual int write_row(unsigned char *)
 
683
  {
 
684
    return HA_ERR_WRONG_COMMAND;
 
685
  }
 
686
 
 
687
  virtual int update_row(const unsigned char *, unsigned char *)
 
688
  {
 
689
    return HA_ERR_WRONG_COMMAND;
 
690
  }
 
691
 
 
692
  virtual int delete_row(const unsigned char *)
551
693
  {
552
694
    return HA_ERR_WRONG_COMMAND;
553
695
  }
557
699
    by that statement.
558
700
  */
559
701
  virtual int reset() { return 0; }
 
702
  virtual Table_flags table_flags(void) const= 0;
560
703
 
561
704
  /**
562
705
    Is not invoked for non-transactional temporary tables.
563
706
 
564
707
    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.
 
708
    from the table. This call is prefixed with a call to handler::store_lock()
 
709
    and is invoked only for those handler instances that stored the lock.
567
710
 
568
711
    Calls to rnd_init/index_init are prefixed with this call. When table
569
712
    IO is complete, we call external_lock(F_UNLCK).
584
727
  {
585
728
    return 0;
586
729
  }
587
 
  virtual void release_auto_increment(void) { return; }
 
730
  virtual void release_auto_increment(void) { return; };
588
731
  /** admin commands - called from mysql_admin_table */
589
 
  virtual int check(Session *)
 
732
  virtual int check(Session *, HA_CHECK_OPT *)
590
733
  { return HA_ADMIN_NOT_IMPLEMENTED; }
591
734
 
592
735
  virtual void start_bulk_insert(ha_rows)
596
739
                         uint32_t, enum ha_rkey_function)
597
740
   { return  HA_ERR_WRONG_COMMAND; }
598
741
  virtual int index_read_last(unsigned char *, const unsigned char *, uint32_t)
599
 
   { return (errno= HA_ERR_WRONG_COMMAND); }
 
742
   { return (my_errno= HA_ERR_WRONG_COMMAND); }
 
743
  /**
 
744
    This method is similar to update_row, however the handler doesn't need
 
745
    to execute the updates at this point in time. The handler can be certain
 
746
    that another call to bulk_update_row will occur OR a call to
 
747
    exec_bulk_update before the set of updates in this query is concluded.
 
748
 
 
749
    @param    old_data       Old record
 
750
    @param    new_data       New record
 
751
    @param    dup_key_found  Number of duplicate keys found
 
752
 
 
753
    @retval  0   Bulk delete used by handler
 
754
    @retval  1   Bulk delete not used, normal operation used
 
755
  */
 
756
  virtual int bulk_update_row(const unsigned char *, unsigned char *, uint32_t *)
 
757
  {
 
758
    assert(false);
 
759
    return HA_ERR_WRONG_COMMAND;
 
760
  }
600
761
  /**
601
762
    This is called to delete all rows in a table
602
 
    If the Cursor don't support this, then this function will
 
763
    If the handler don't support this, then this function will
603
764
    return HA_ERR_WRONG_COMMAND and MySQL will delete the rows one
604
765
    by one.
605
766
  */
606
767
  virtual int delete_all_rows(void)
607
 
  { return (errno=HA_ERR_WRONG_COMMAND); }
 
768
  { return (my_errno=HA_ERR_WRONG_COMMAND); }
608
769
  /**
609
770
    Reset the auto-increment counter to the given value, i.e. the next row
610
771
    inserted will get the given value. This is called e.g. after TRUNCATE
613
774
  */
614
775
  virtual int reset_auto_increment(uint64_t)
615
776
  { return HA_ERR_WRONG_COMMAND; }
616
 
 
617
 
  virtual int analyze(Session *)
 
777
  virtual int optimize(Session *, HA_CHECK_OPT *)
 
778
  { return HA_ADMIN_NOT_IMPLEMENTED; }
 
779
  virtual int analyze(Session *, HA_CHECK_OPT *)
618
780
  { return HA_ADMIN_NOT_IMPLEMENTED; }
619
781
 
620
782
  virtual int disable_indexes(uint32_t)
621
783
  { return HA_ERR_WRONG_COMMAND; }
622
 
 
623
784
  virtual int enable_indexes(uint32_t)
624
785
  { return HA_ERR_WRONG_COMMAND; }
625
 
 
626
786
  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
 
  */
 
787
  { return (my_errno=HA_ERR_WRONG_COMMAND); }
635
788
  virtual void drop_table(const char *name);
636
789
};
637
790
 
638
791
extern const char *ha_row_type[];
 
792
extern const char *tx_isolation_names[];
 
793
extern const char *binlog_format_names[];
 
794
extern TYPELIB tx_isolation_typelib;
 
795
extern uint32_t total_ha, total_ha_2pc;
 
796
 
 
797
       /* Wrapper functions */
 
798
#define ha_commit(session) (ha_commit_trans((session), true))
 
799
#define ha_rollback(session) (ha_rollback_trans((session), true))
639
800
 
640
801
/* 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);
 
802
int ha_init_errors(void);
 
803
int ha_init(void);
 
804
int ha_end(void);
 
805
 
 
806
void add_storage_engine(StorageEngine *engine);
 
807
void remove_storage_engine(StorageEngine *engine);
 
808
 
 
809
void ha_close_connection(Session* session);
 
810
bool ha_flush_logs(StorageEngine *db_type);
 
811
void ha_drop_database(char* path);
 
812
int ha_create_table(Session *session, const char *path,
 
813
                    const char *db, const char *table_name,
 
814
                    HA_CREATE_INFO *create_info,
 
815
                    bool update_create_info,
 
816
                    drizzled::message::Table *table_proto);
 
817
int ha_delete_table(Session *session, const char *path,
 
818
                    const char *db, const char *alias, bool generate_warning);
 
819
 
 
820
/* statistics and info */
 
821
bool ha_show_status(Session *session, StorageEngine *db_type, enum ha_stat_type stat);
 
822
 
 
823
int ha_find_files(Session *session,const char *db,const char *path,
 
824
                  const char *wild, bool dir, List<LEX_STRING>* files);
 
825
 
 
826
/* report to InnoDB that control passes to the client */
 
827
int ha_release_temporary_latches(Session *session);
 
828
 
 
829
/* transactions: interface to StorageEngine functions */
 
830
int ha_start_consistent_snapshot(Session *session);
 
831
int ha_commit_or_rollback_by_xid(XID *xid, bool commit);
 
832
int ha_commit_one_phase(Session *session, bool all);
 
833
int ha_rollback_trans(Session *session, bool all);
 
834
int ha_recover(HASH *commit_list);
 
835
 
 
836
/* transactions: these functions never call StorageEngine functions directly */
 
837
int ha_commit_trans(Session *session, bool all);
 
838
int ha_autocommit_or_rollback(Session *session, int error);
 
839
int ha_enable_transaction(Session *session, bool on);
 
840
 
 
841
/* savepoints */
 
842
int ha_rollback_to_savepoint(Session *session, SAVEPOINT *sv);
 
843
int ha_savepoint(Session *session, SAVEPOINT *sv);
 
844
int ha_release_savepoint(Session *session, SAVEPOINT *sv);
 
845
 
 
846
/* these are called by storage engines */
 
847
void trans_register_ha(Session *session, bool all, StorageEngine *engine);
 
848
 
 
849
uint32_t filename_to_tablename(const char *from, char *to, uint32_t to_length);
 
850
bool tablename_to_filename(const char *from, char *to, size_t to_length);
 
851
 
 
852
 
 
853
/*
 
854
  Storage engine has to assume the transaction will end up with 2pc if
 
855
   - there is more than one 2pc-capable storage engine available
 
856
   - in the current transaction 2pc was not disabled yet
 
857
*/
 
858
#define trans_need_2pc(session, all)                   ((total_ha_2pc > 1) && \
 
859
        !((all ? &session->transaction.all : &session->transaction.stmt)->no_2pc))
 
860
 
 
861
 
 
862
bool mysql_xa_recover(Session *session);
 
863
 
 
864
SORT_FIELD * make_unireg_sortorder(order_st *order, uint32_t *length,
 
865
                                   SORT_FIELD *sortorder);
646
866
int setup_order(Session *session, Item **ref_pointer_array, TableList *tables,
647
 
                List<Item> &fields, List <Item> &all_fields, Order *order);
 
867
                List<Item> &fields, List <Item> &all_fields, order_st *order);
648
868
int setup_group(Session *session, Item **ref_pointer_array, TableList *tables,
649
 
                List<Item> &fields, List<Item> &all_fields, Order *order,
 
869
                List<Item> &fields, List<Item> &all_fields, order_st *order,
650
870
                bool *hidden_group_fields);
651
871
bool fix_inner_refs(Session *session, List<Item> &all_fields, Select_Lex *select,
652
872
                    Item **ref_pointer_array);
653
873
 
654
874
bool handle_select(Session *session, LEX *lex, select_result *result,
655
875
                   uint64_t setup_tables_done_option);
 
876
bool mysql_select(Session *session, Item ***rref_pointer_array,
 
877
                  TableList *tables, uint32_t wild_num,  List<Item> &list,
 
878
                  COND *conds, uint32_t og_num, order_st *order, order_st *group,
 
879
                  Item *having, uint64_t select_type,
 
880
                  select_result *result, Select_Lex_Unit *unit,
 
881
                  Select_Lex *select_lex);
656
882
void free_underlaid_joins(Session *session, Select_Lex *select);
 
883
bool mysql_explain_union(Session *session, Select_Lex_Unit *unit,
 
884
                         select_result *result);
 
885
int mysql_explain_select(Session *session, Select_Lex *sl, char const *type,
 
886
                         select_result *result);
657
887
 
658
 
bool handle_derived(LEX *lex, bool (*processor)(Session *session,
 
888
bool mysql_handle_derived(LEX *lex, bool (*processor)(Session *session,
659
889
                                                      LEX *lex,
660
890
                                                      TableList *table));
661
 
bool derived_prepare(Session *session, LEX *lex, TableList *t);
662
 
bool derived_filling(Session *session, LEX *lex, TableList *t);
 
891
bool mysql_derived_prepare(Session *session, LEX *lex, TableList *t);
 
892
bool mysql_derived_filling(Session *session, LEX *lex, TableList *t);
 
893
void sp_prepare_create_field(Session *session, CreateField *sql_field);
663
894
int prepare_create_field(CreateField *sql_field,
664
895
                         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,
 
896
                         int *timestamps, int *timestamps_with_niladic,
 
897
                         int64_t table_flags);
 
898
bool mysql_create_table(Session *session,const char *db, const char *table_name,
 
899
                        HA_CREATE_INFO *create_info,
 
900
                        drizzled::message::Table *table_proto,
 
901
                        Alter_info *alter_info,
 
902
                        bool tmp_table, uint32_t select_field_count);
 
903
bool mysql_create_table_no_lock(Session *session, const char *db,
 
904
                                const char *table_name,
 
905
                                HA_CREATE_INFO *create_info,
 
906
                                drizzled::message::Table *table_proto,
 
907
                                Alter_info *alter_info,
 
908
                                bool tmp_table, uint32_t select_field_count);
 
909
 
 
910
bool mysql_recreate_table(Session *session, TableList *table_list);
 
911
bool mysql_create_like_table(Session *session, TableList *table,
 
912
                             TableList *src_table,
 
913
                             HA_CREATE_INFO *create_info);
 
914
bool mysql_rename_table(StorageEngine *base, const char *old_db,
 
915
                        const char * old_name, const char *new_db,
 
916
                        const char * new_name, uint32_t flags);
 
917
bool mysql_prepare_update(Session *session, TableList *table_list,
 
918
                          Item **conds, uint32_t order_num, order_st *order);
 
919
int mysql_update(Session *session,TableList *tables,List<Item> &fields,
698
920
                 List<Item> &values,COND *conds,
699
 
                 uint32_t order_num, Order *order, ha_rows limit,
 
921
                 uint32_t order_num, order_st *order, ha_rows limit,
700
922
                 enum enum_duplicates handle_duplicates, bool ignore);
701
 
bool prepare_insert(Session *session, TableList *table_list, Table *table,
 
923
bool mysql_prepare_insert(Session *session, TableList *table_list, Table *table,
702
924
                          List<Item> &fields, List_item *values,
703
925
                          List<Item> &update_fields,
704
926
                          List<Item> &update_values, enum_duplicates duplic,
705
927
                          COND **where, bool select_insert,
706
928
                          bool check_fields, bool abort_on_warning);
707
 
bool insert_query(Session *session,TableList *table,List<Item> &fields,
 
929
bool mysql_insert(Session *session,TableList *table,List<Item> &fields,
708
930
                  List<List_item> &values, List<Item> &update_fields,
709
931
                  List<Item> &update_values, enum_duplicates flag,
710
932
                  bool ignore);
711
933
int check_that_all_fields_are_given_values(Session *session, Table *entry,
712
934
                                           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,
 
935
int mysql_prepare_delete(Session *session, TableList *table_list, Item **conds);
 
936
bool mysql_delete(Session *session, TableList *table_list, COND *conds,
715
937
                  SQL_LIST *order, ha_rows rows, uint64_t options,
716
938
                  bool reset_auto_increment);
717
 
bool truncate(Session& session, TableList *table_list);
 
939
bool mysql_truncate(Session *session, TableList *table_list, bool dont_send_ok);
718
940
TableShare *get_table_share(Session *session, TableList *table_list, char *key,
719
941
                             uint32_t key_length, uint32_t db_flags, int *error);
720
942
TableShare *get_cached_table_share(const char *db, const char *table_name);
721
943
bool reopen_name_locked_table(Session* session, TableList* table_list, bool link_in);
 
944
Table *table_cache_insert_placeholder(Session *session, const char *key,
 
945
                                      uint32_t key_length);
 
946
bool lock_table_name_if_not_cached(Session *session, const char *db,
 
947
                                   const char *table_name, Table **table);
 
948
bool reopen_table(Table *table);
722
949
bool reopen_tables(Session *session,bool get_locks,bool in_refresh);
 
950
void close_data_files_and_morph_locks(Session *session, const char *db,
 
951
                                      const char *table_name);
723
952
void close_handle_and_leave_table_as_lock(Table *table);
724
953
bool wait_for_tables(Session *session);
725
954
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);
 
955
Table *drop_locked_tables(Session *session,const char *db, const char *table_name);
 
956
void abort_locked_tables(Session *session,const char *db, const char *table_name);
728
957
extern Field *not_found_field;
729
958
extern Field *view_ref_found;
730
959
 
744
973
Field *
745
974
find_field_in_table(Session *session, Table *table, const char *name, uint32_t length,
746
975
                    bool allow_rowid, uint32_t *cached_field_index_ptr);
747
 
 
748
 
} /* namespace drizzled */
749
 
 
750
 
#endif /* DRIZZLED_CURSOR_H */
 
976
Field *
 
977
find_field_in_table_sef(Table *table, const char *name);
 
978
 
 
979
 
 
980
#endif /* DRIZZLED_HANDLER_H */