~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/cursor.h

  • Committer: Stewart Smith
  • Date: 2010-03-18 12:01:34 UTC
  • mto: (1666.2.3 build)
  • mto: This revision was merged to the branch mainline in revision 1596.
  • Revision ID: stewart@flamingspork.com-20100318120134-45fdnsw8g3j6c7oy
move RAND() into a plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
#include <drizzled/xid.h>
24
24
#include <drizzled/discrete_interval.h>
25
 
#include <drizzled/identifier.h>
26
 
#include <drizzled/definitions.h>
27
 
#include <drizzled/key_map.h>
 
25
#include <drizzled/table_identifier.h>
28
26
 
29
27
/* Definitions for parameters to do with Cursor-routines */
30
28
 
38
36
 
39
37
#include <drizzled/message/table.pb.h>
40
38
 
 
39
/* Bits to show what an alter table will do */
 
40
#include <drizzled/sql_bitmap.h>
 
41
 
 
42
#include <drizzled/cursor.h>
 
43
 
41
44
#include <bitset>
42
45
#include <algorithm>
43
46
 
46
49
 
47
50
#define HA_MAX_ALTER_FLAGS 40
48
51
 
 
52
 
49
53
typedef std::bitset<HA_MAX_ALTER_FLAGS> HA_ALTER_FLAGS;
50
54
 
51
55
extern uint64_t refresh_version;  /* Increments on each reload */
52
56
 
 
57
 
 
58
typedef bool (*qc_engine_callback)(Session *session, char *table_key,
 
59
                                      uint32_t key_length,
 
60
                                      uint64_t *engine_data);
 
61
 
 
62
 
 
63
/* The Cursor for a table type.  Will be included in the Table structure */
 
64
 
53
65
class Table;
54
66
class TableList;
55
67
class TableShare;
56
68
class Select_Lex_Unit;
57
 
class ForeignKeyInfo;
58
 
struct Order;
 
69
struct st_foreign_key_info;
 
70
typedef struct st_foreign_key_info FOREIGN_KEY_INFO;
 
71
struct order_st;
59
72
 
60
73
class Item;
 
74
struct st_table_log_memory_entry;
61
75
 
62
76
class LEX;
63
77
class Select_Lex;
66
80
class CreateField;
67
81
class sys_var_str;
68
82
class Item_ident;
 
83
typedef struct st_sort_field SORT_FIELD;
69
84
 
70
85
typedef List<Item> List_item;
71
86
extern KEY_CREATE_INFO default_key_create_info;
75
90
 
76
91
typedef struct system_status_var system_status_var;
77
92
 
78
 
namespace optimizer
79
 
{
80
 
  class CostVector;
81
 
}
 
93
class COST_VECT;
82
94
 
 
95
uint32_t calculate_key_len(Table *, uint, const unsigned char *, key_part_map);
83
96
/*
84
97
  bitmap with first N+1 bits set
85
98
  (keypart_map for a key prefix of [0..N] keyparts)
136
149
  present, its length is one byte <not-sure> which must be set to 0xFF
137
150
  at all times. </not-sure>
138
151
 
 
152
  If the table has columns of type BIT, then certain bits from those columns
 
153
  may be stored in null_bytes as well. Grep around for Field_bit for
 
154
  details.
 
155
 
139
156
  For blob columns (see Field_blob), the record buffer stores length of the
140
157
  data, following by memory pointer to the blob data. The pointer is owned
141
158
  by the storage engine and is valid until the next operation.
143
160
  If a blob column has NULL value, then its length and blob data pointer
144
161
  must be set to 0.
145
162
*/
146
 
class Cursor
 
163
 
 
164
class Cursor :public memory::SqlAlloc
147
165
{
148
 
  Table &table;               /* The current open table */
149
 
  plugin::StorageEngine &engine;      /* storage engine of this Cursor */
150
 
 
151
166
protected:
 
167
  TableShare *table_share;   /* The table definition */
 
168
  Table *table;               /* The current open table */
 
169
 
152
170
  ha_rows estimation_rows_to_insert;
153
 
 
154
171
public:
 
172
  plugin::StorageEngine *engine;      /* storage engine of this Cursor */
155
173
  inline plugin::StorageEngine *getEngine() const       /* table_type for handler */
156
174
  {
157
 
    return &engine;
 
175
    return engine;
158
176
  }
159
177
  unsigned char *ref;                           /* Pointer to current row */
160
178
  unsigned char *dup_ref;                       /* Pointer to duplicate row */
161
179
 
162
 
  TableShare *getShare();
163
 
 
164
 
  Table *getTable() const
165
 
  {
166
 
    return &table;
167
 
  }
168
 
 
169
180
  ha_statistics stats;
170
181
  /** MultiRangeRead-related members: */
171
182
  range_seq_t mrr_iter;    /* Interator to traverse the range sequence */
172
183
  RANGE_SEQ_IF mrr_funcs;  /* Range sequence traversal functions */
173
 
 
 
184
  HANDLER_BUFFER *multi_range_buffer; /* MRR buffer info */
174
185
  uint32_t ranges_in_seq; /* Total number of ranges in the traversed sequence */
175
186
  /* true <=> source MRR ranges and the output are ordered */
176
187
  bool mrr_is_output_sorted;
179
190
  bool mrr_have_range;
180
191
 
181
192
  bool eq_range;
 
193
  /*
 
194
    true <=> the engine guarantees that returned records are within the range
 
195
    being scanned.
 
196
  */
 
197
  bool in_range_check_pushed_down;
182
198
 
183
199
  /** Current range (the one we're now returning rows from) */
184
200
  KEY_MULTI_RANGE mrr_cur_range;
185
201
 
186
202
  /** The following are for read_range() */
187
203
  key_range save_end_range, *end_range;
188
 
  KeyPartInfo *range_key_part;
 
204
  KEY_PART_INFO *range_key_part;
189
205
  int key_compare_result_on_equal;
190
206
 
191
207
  uint32_t errkey;                              /* Last dup key */
195
211
  uint32_t ref_length;
196
212
  enum {NONE=0, INDEX, RND} inited;
197
213
  bool locked;
 
214
  bool implicit_emptied;                /* Can be !=0 only if HEAP */
198
215
 
199
216
  /**
200
217
    next_insert_id is the next value which should be inserted into the
224
241
  */
225
242
  Discrete_interval auto_inc_interval_for_cur_row;
226
243
 
227
 
  Cursor(plugin::StorageEngine &engine_arg, Table &share_arg);
 
244
  Cursor(plugin::StorageEngine &engine_arg, TableShare &share_arg);
228
245
  virtual ~Cursor(void);
229
246
  virtual Cursor *clone(memory::Root *mem_root);
230
247
 
231
248
  /* ha_ methods: pubilc wrappers for private virtual API */
232
249
 
233
 
  int ha_open(const TableIdentifier &identifier, int mode, int test_if_locked);
234
 
  int startIndexScan(uint32_t idx, bool sorted);
235
 
  int endIndexScan();
236
 
  int startTableScan(bool scan);
237
 
  int endTableScan();
 
250
  int ha_open(Table *table, const char *name, int mode, int test_if_locked);
 
251
  int ha_index_init(uint32_t idx, bool sorted);
 
252
  int ha_index_end();
 
253
  int ha_rnd_init(bool scan);
 
254
  int ha_rnd_end();
238
255
  int ha_reset();
239
256
 
240
257
  /* this is necessary in many places, e.g. in HANDLER command */
243
260
  /**
244
261
    These functions represent the public interface to *users* of the
245
262
    Cursor class, hence they are *not* virtual. For the inheritance
246
 
    interface, see the (private) functions doInsertRecord(), doUpdateRecord(),
247
 
    and doDeleteRecord() below.
 
263
    interface, see the (private) functions write_row(), update_row(),
 
264
    and delete_row() below.
248
265
  */
249
266
  int ha_external_lock(Session *session, int lock_type);
250
 
  int insertRecord(unsigned char * buf);
251
 
  int updateRecord(const unsigned char * old_data, unsigned char * new_data);
252
 
  int deleteRecord(const unsigned char * buf);
 
267
  int ha_write_row(unsigned char * buf);
 
268
  int ha_update_row(const unsigned char * old_data, unsigned char * new_data);
 
269
  int ha_delete_row(const unsigned char * buf);
253
270
  void ha_release_auto_increment();
254
271
 
255
272
  /** to be actually called to get 'check()' functionality*/
268
285
 
269
286
  void adjust_next_insert_id_after_explicit_value(uint64_t nr);
270
287
  int update_auto_increment();
 
288
  virtual void change_table_ptr(Table *table_arg, TableShare *share);
271
289
 
272
290
  /* Estimates calculation */
273
291
  virtual double scan_time(void)
280
298
  virtual ha_rows multi_range_read_info_const(uint32_t keyno, RANGE_SEQ_IF *seq,
281
299
                                              void *seq_init_param,
282
300
                                              uint32_t n_ranges, uint32_t *bufsz,
283
 
                                              uint32_t *flags, optimizer::CostVector *cost);
 
301
                                              uint32_t *flags, COST_VECT *cost);
284
302
  virtual int multi_range_read_info(uint32_t keyno, uint32_t n_ranges, uint32_t keys,
285
 
                                    uint32_t *bufsz, uint32_t *flags, optimizer::CostVector *cost);
 
303
                                    uint32_t *bufsz, uint32_t *flags, COST_VECT *cost);
286
304
  virtual int multi_range_read_init(RANGE_SEQ_IF *seq, void *seq_init_param,
287
 
                                    uint32_t n_ranges, uint32_t mode);
 
305
                                    uint32_t n_ranges, uint32_t mode,
 
306
                                    HANDLER_BUFFER *buf);
288
307
  virtual int multi_range_read_next(char **range_info);
289
308
 
290
309
 
319
338
  virtual ha_rows estimate_rows_upper_bound()
320
339
  { return stats.records+EXTRA_RECORDS; }
321
340
 
 
341
  /**
 
342
    Get the row type from the storage engine.  If this method returns
 
343
    ROW_TYPE_NOT_USED, the information in HA_CREATE_INFO should be used.
 
344
  */
 
345
  virtual enum row_type get_row_type() const { return ROW_TYPE_NOT_USED; }
 
346
 
322
347
  virtual const char *index_type(uint32_t)
323
348
  { assert(0); return "";}
324
349
 
332
357
     row if available. If the key value is null, begin at the first key of the
333
358
     index.
334
359
  */
335
 
  virtual int index_read_map(unsigned char * buf, const unsigned char *key,
 
360
  virtual int index_read_map(unsigned char * buf, const unsigned char * key,
336
361
                             key_part_map keypart_map,
337
362
                             enum ha_rkey_function find_flag)
338
363
  {
339
 
    uint32_t key_len= calculate_key_len(active_index, keypart_map);
 
364
    uint32_t key_len= calculate_key_len(table, active_index, key, keypart_map);
340
365
    return  index_read(buf, key, key_len, find_flag);
341
366
  }
342
367
  /**
358
383
  virtual int index_last(unsigned char *)
359
384
   { return  HA_ERR_WRONG_COMMAND; }
360
385
  virtual int index_next_same(unsigned char *, const unsigned char *, uint32_t);
361
 
 
362
 
private:
363
 
  uint32_t calculate_key_len(uint32_t key_position, key_part_map keypart_map_arg);
364
 
public:
365
 
 
366
386
  /**
367
387
     @brief
368
388
     The following functions works like index_read, but it find the last
371
391
  virtual int index_read_last_map(unsigned char * buf, const unsigned char * key,
372
392
                                  key_part_map keypart_map)
373
393
  {
374
 
    uint32_t key_len= calculate_key_len(active_index, keypart_map);
 
394
    uint32_t key_len= calculate_key_len(table, active_index, key, keypart_map);
375
395
    return index_read_last(buf, key, key_len);
376
396
  }
377
397
  virtual int read_range_first(const key_range *start_key,
379
399
                               bool eq_range, bool sorted);
380
400
  virtual int read_range_next();
381
401
  int compare_key(key_range *range);
 
402
  int compare_key2(key_range *range);
382
403
  virtual int rnd_next(unsigned char *)=0;
383
404
  virtual int rnd_pos(unsigned char *, unsigned char *)=0;
 
405
  /**
 
406
    One has to use this method when to find
 
407
    random position by record as the plain
 
408
    position() call doesn't work for some
 
409
    handlers for random position.
 
410
  */
 
411
  virtual int rnd_pos_by_record(unsigned char *record);
384
412
  virtual int read_first_row(unsigned char *buf, uint32_t primary_key);
 
413
  /**
 
414
    The following function is only needed for tables that may be temporary
 
415
    tables during joins.
 
416
  */
 
417
  virtual int restart_rnd_next(unsigned char *, unsigned char *)
 
418
    { return HA_ERR_WRONG_COMMAND; }
385
419
  virtual int rnd_same(unsigned char *, uint32_t)
386
420
    { return HA_ERR_WRONG_COMMAND; }
387
421
  virtual ha_rows records_in_range(uint32_t, key_range *, key_range *)
419
453
  virtual void get_auto_increment(uint64_t offset, uint64_t increment,
420
454
                                  uint64_t nb_desired_values,
421
455
                                  uint64_t *first_value,
422
 
                                  uint64_t *nb_reserved_values)= 0;
423
 
 
 
456
                                  uint64_t *nb_reserved_values);
424
457
  void set_next_insert_id(uint64_t id)
425
458
  {
426
459
    next_insert_id= id;
441
474
      insert_id_for_cur_row;
442
475
  }
443
476
 
 
477
  virtual void update_create_info(HA_CREATE_INFO *) {}
 
478
  int check_old_types(void);
444
479
  /* end of the list of admin commands */
445
480
 
446
481
  virtual int indexes_are_disabled(void) {return 0;}
463
498
   */
464
499
  virtual bool can_switch_engines(void) { return true; }
465
500
  /** used in REPLACE; is > 0 if table is referred by a FOREIGN KEY */
466
 
  virtual int get_foreign_key_list(Session *, List<ForeignKeyInfo> *)
 
501
  virtual int get_foreign_key_list(Session *, List<FOREIGN_KEY_INFO> *)
467
502
  { return 0; }
468
503
  virtual uint32_t referenced_by_foreign_key() { return 0;}
469
504
  virtual void free_foreign_key_create_info(char *) {}
 
505
  /** The following can be called without an open Cursor */
 
506
 
 
507
  virtual int add_index(Table *, KEY *, uint32_t)
 
508
  { return (HA_ERR_WRONG_COMMAND); }
 
509
  virtual int prepare_drop_index(Table *, uint32_t *, uint32_t)
 
510
  { return (HA_ERR_WRONG_COMMAND); }
 
511
  virtual int final_drop_index(Table *)
 
512
  { return (HA_ERR_WRONG_COMMAND); }
 
513
 
 
514
  virtual uint32_t checksum(void) const { return 0; }
470
515
 
471
516
  /**
472
517
    Is not invoked for non-transactional temporary tables.
475
520
    or partitioned.
476
521
 
477
522
    @note that one can NOT rely on table->in_use in store_lock().  It may
478
 
    refer to a different thread if called from abortLockForThread().
 
523
    refer to a different thread if called from mysql_lock_abort_for_thread().
479
524
 
480
525
    @note If the table is MERGE, store_lock() can return less locks
481
526
    than lock_count() claimed. This can happen when the MERGE children
509
554
 
510
555
protected:
511
556
  /* Service methods for use by storage engines. */
512
 
  void ha_statistic_increment(uint64_t system_status_var::*offset) const;
 
557
  void ha_statistic_increment(ulong system_status_var::*offset) const;
513
558
  void **ha_data(Session *) const;
 
559
  Session *ha_session(void) const;
514
560
 
515
561
private:
516
562
  /* Private helpers */
522
568
    the corresponding 'ha_*' method above.
523
569
  */
524
570
 
525
 
  virtual int open(const char *, int , uint32_t ) { assert(0); return -1; }
526
 
  virtual int doOpen(const TableIdentifier &identifier, int mode, uint32_t test_if_locked);
527
 
  virtual int doStartIndexScan(uint32_t idx, bool)
 
571
  virtual int open(const char *name, int mode, uint32_t test_if_locked)=0;
 
572
  virtual int index_init(uint32_t idx, bool)
528
573
  { active_index= idx; return 0; }
529
 
  virtual int doEndIndexScan() { active_index= MAX_KEY; return 0; }
 
574
  virtual int index_end() { active_index= MAX_KEY; return 0; }
530
575
  /**
531
 
    doStartTableScan() can be called two times without doEndTableScan() in between
 
576
    rnd_init() can be called two times without rnd_end() in between
532
577
    (it only makes sense if scan=1).
533
578
    then the second call should prepare for the new table scan (e.g
534
579
    if rnd_init allocates the cursor, second call should position it
535
580
    to the start of the table, no need to deallocate and allocate it again
536
581
  */
537
 
  virtual int doStartTableScan(bool scan)= 0;
538
 
  virtual int doEndTableScan() { return 0; }
539
 
  virtual int doInsertRecord(unsigned char *)
540
 
  {
541
 
    return HA_ERR_WRONG_COMMAND;
542
 
  }
543
 
 
544
 
  virtual int doUpdateRecord(const unsigned char *, unsigned char *)
545
 
  {
546
 
    return HA_ERR_WRONG_COMMAND;
547
 
  }
548
 
 
549
 
  virtual int doDeleteRecord(const unsigned char *)
 
582
  virtual int rnd_init(bool scan)= 0;
 
583
  virtual int rnd_end() { return 0; }
 
584
  virtual int write_row(unsigned char *)
 
585
  {
 
586
    return HA_ERR_WRONG_COMMAND;
 
587
  }
 
588
 
 
589
  virtual int update_row(const unsigned char *, unsigned char *)
 
590
  {
 
591
    return HA_ERR_WRONG_COMMAND;
 
592
  }
 
593
 
 
594
  virtual int delete_row(const unsigned char *)
550
595
  {
551
596
    return HA_ERR_WRONG_COMMAND;
552
597
  }
583
628
  {
584
629
    return 0;
585
630
  }
586
 
  virtual void release_auto_increment(void) { return; }
 
631
  virtual void release_auto_increment(void) { return; };
587
632
  /** admin commands - called from mysql_admin_table */
588
633
  virtual int check(Session *)
589
634
  { return HA_ADMIN_NOT_IMPLEMENTED; }
639
684
/* basic stuff */
640
685
void ha_init_errors(void);
641
686
 
642
 
class SortField;
643
 
SortField *make_unireg_sortorder(Order *order, uint32_t *length,
644
 
                                 SortField *sortorder);
 
687
SORT_FIELD * make_unireg_sortorder(order_st *order, uint32_t *length,
 
688
                                   SORT_FIELD *sortorder);
645
689
int setup_order(Session *session, Item **ref_pointer_array, TableList *tables,
646
 
                List<Item> &fields, List <Item> &all_fields, Order *order);
 
690
                List<Item> &fields, List <Item> &all_fields, order_st *order);
647
691
int setup_group(Session *session, Item **ref_pointer_array, TableList *tables,
648
 
                List<Item> &fields, List<Item> &all_fields, Order *order,
 
692
                List<Item> &fields, List<Item> &all_fields, order_st *order,
649
693
                bool *hidden_group_fields);
650
694
bool fix_inner_refs(Session *session, List<Item> &all_fields, Select_Lex *select,
651
695
                    Item **ref_pointer_array);
664
708
                         int *timestamps, int *timestamps_with_niladic);
665
709
 
666
710
bool mysql_create_table(Session *session,
667
 
                        const TableIdentifier &identifier,
 
711
                        TableIdentifier &identifier,
668
712
                        HA_CREATE_INFO *create_info,
669
713
                        message::Table &table_proto,
670
714
                        AlterInfo *alter_info,
672
716
                        bool is_if_not_exists);
673
717
 
674
718
bool mysql_create_table_no_lock(Session *session,
675
 
                                const TableIdentifier &identifier,
 
719
                                TableIdentifier &identifier,
676
720
                                HA_CREATE_INFO *create_info,
677
721
                                message::Table &table_proto,
678
722
                                AlterInfo *alter_info,
679
 
                                bool tmp_table, uint32_t select_field_count,
 
723
                                bool tmp_table,
 
724
                                uint32_t select_field_count,
680
725
                                bool is_if_not_exists);
681
726
 
682
727
bool mysql_create_like_table(Session* session,
683
 
                             const TableIdentifier &destination_identifier,
 
728
                             TableIdentifier &destination_identifier,
684
729
                             TableList* table, TableList* src_table,
685
730
                             message::Table &create_table_proto,
686
731
                             bool is_if_not_exists,
687
732
                             bool is_engine_set);
688
733
 
689
 
bool mysql_rename_table(Session &session,
690
 
                        plugin::StorageEngine *base,
691
 
                        const TableIdentifier &old_identifier,
692
 
                        const TableIdentifier &new_identifier);
 
734
bool mysql_rename_table(plugin::StorageEngine *base, const char *old_db,
 
735
                        const char * old_name, const char *new_db,
 
736
                        const char * new_name, uint32_t flags);
693
737
 
694
738
bool mysql_prepare_update(Session *session, TableList *table_list,
695
 
                          Item **conds, uint32_t order_num, Order *order);
 
739
                          Item **conds, uint32_t order_num, order_st *order);
696
740
int mysql_update(Session *session,TableList *tables,List<Item> &fields,
697
741
                 List<Item> &values,COND *conds,
698
 
                 uint32_t order_num, Order *order, ha_rows limit,
 
742
                 uint32_t order_num, order_st *order, ha_rows limit,
699
743
                 enum enum_duplicates handle_duplicates, bool ignore);
700
744
bool mysql_prepare_insert(Session *session, TableList *table_list, Table *table,
701
745
                          List<Item> &fields, List_item *values,
718
762
                             uint32_t key_length, uint32_t db_flags, int *error);
719
763
TableShare *get_cached_table_share(const char *db, const char *table_name);
720
764
bool reopen_name_locked_table(Session* session, TableList* table_list, bool link_in);
 
765
Table *table_cache_insert_placeholder(Session *session, const char *key,
 
766
                                      uint32_t key_length);
 
767
bool lock_table_name_if_not_cached(Session *session, const char *db,
 
768
                                   const char *table_name, Table **table);
 
769
bool reopen_table(Table *table);
721
770
bool reopen_tables(Session *session,bool get_locks,bool in_refresh);
 
771
void close_data_files_and_morph_locks(Session *session, const char *db,
 
772
                                      const char *table_name);
722
773
void close_handle_and_leave_table_as_lock(Table *table);
723
774
bool wait_for_tables(Session *session);
724
775
bool table_is_used(Table *table, bool wait_for_name_lock);
725
 
Table *drop_locked_tables(Session *session, const drizzled::TableIdentifier &identifier);
726
 
void abort_locked_tables(Session *session, const drizzled::TableIdentifier &identifier);
 
776
Table *drop_locked_tables(Session *session,const char *db, const char *table_name);
 
777
void abort_locked_tables(Session *session,const char *db, const char *table_name);
727
778
extern Field *not_found_field;
728
779
extern Field *view_ref_found;
729
780