~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table.h

  • Committer: Padraig O'Sullivan
  • Date: 2009-09-13 01:03:01 UTC
  • mto: (1126.9.2 captain-20090915-01)
  • mto: This revision was merged to the branch mainline in revision 1133.
  • Revision ID: osullivan.padraig@gmail.com-20090913010301-tcvvezipx1124acy
Added calls to the dtrace delete begin/end probes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
#ifndef DRIZZLED_TABLE_H
24
24
#define DRIZZLED_TABLE_H
25
25
 
26
 
#include <string>
27
 
 
 
26
#include <plugin/myisam/myisam.h>
 
27
#include <mysys/hash.h>
28
28
#include "drizzled/order.h"
29
29
#include "drizzled/filesort_info.h"
30
30
#include "drizzled/natural_join_column.h"
31
31
#include "drizzled/field_iterator.h"
32
 
#include "drizzled/cursor.h"
 
32
#include "drizzled/handler.h"
33
33
#include "drizzled/lex_string.h"
34
34
#include "drizzled/table_list.h"
35
35
#include "drizzled/table_share.h"
36
 
#include "drizzled/atomics.h"
37
 
#include "drizzled/query_id.h"
38
 
 
39
 
namespace drizzled
40
 
{
 
36
 
 
37
#include <string>
 
38
 
 
39
using namespace std;
41
40
 
42
41
class Item;
43
42
class Item_subselect;
44
43
class Select_Lex_Unit;
45
44
class Select_Lex;
46
45
class COND_EQUAL;
47
 
class SecurityContext;
 
46
class Security_context;
48
47
class TableList;
49
48
class Field_timestamp;
50
49
class Field_blob;
51
50
 
52
 
extern uint64_t refresh_version;
53
 
 
54
51
typedef enum enum_table_category TABLE_CATEGORY;
55
 
typedef struct st_columndef MI_COLUMNDEF;
56
52
 
57
53
bool create_myisam_from_heap(Session *session, Table *table,
58
54
                             MI_COLUMNDEF *start_recinfo,
70
66
  TableShare *s; /**< Pointer to the shared metadata about the table */
71
67
  Field **field; /**< Pointer to fields collection */
72
68
 
73
 
  Cursor *cursor; /**< Pointer to the storage engine's Cursor managing this table */
 
69
  handler *file; /**< Pointer to the storage engine's handler managing this table */
74
70
  Table *next;
75
71
  Table *prev;
76
72
 
78
74
  MyBitmap *write_set; /* Active column sets */
79
75
 
80
76
  uint32_t tablenr;
81
 
  uint32_t db_stat; /**< information about the cursor as in Cursor.h */
 
77
  uint32_t db_stat; /**< information about the file as in handler.h */
82
78
 
83
79
  MyBitmap def_read_set; /**< Default read set of columns */
84
80
  MyBitmap def_write_set; /**< Default write set of columns */
85
81
  MyBitmap tmp_set; /* Not sure about this... */
86
82
 
87
 
  Session *in_use; /**< Pointer to the current session using this object */
88
 
  Session *getSession()
89
 
  {
90
 
    return in_use;
91
 
  }
 
83
  Session       *in_use; /**< Pointer to the current session using this object */
92
84
 
93
85
  unsigned char *record[2]; /**< Pointer to "records" */
94
86
  unsigned char *insert_values; /* used by INSERT ... UPDATE */
109
101
  uint32_t status; /* What's in record[0] */
110
102
  /* number of select if it is derived table */
111
103
  uint32_t derived_select_number;
112
 
  int current_lock; /**< Type of lock on table */
 
104
  int   current_lock; /**< Type of lock on table */
113
105
  bool copy_blobs; /**< Should blobs by copied when storing? */
114
106
 
115
107
  /*
142
134
    - setting version to 0 - this will force other threads to close
143
135
      the instance of this table and wait (this is the same approach
144
136
      as used for usual name locks).
145
 
    An exclusively name-locked table currently can have no Cursor
 
137
    An exclusively name-locked table currently can have no handler
146
138
    object associated with it (db_stat is always 0), but please do
147
139
    not rely on that.
148
140
  */
175
167
   the same statement. A non-zero query_id is used to control which tables
176
168
   in the list of pre-opened and locked tables are actually being used.
177
169
  */
178
 
  query_id_t query_id;
 
170
  query_id_t    query_id;
179
171
 
180
 
  /**
181
 
   * Estimate of number of records that satisfy SARGable part of the table
182
 
   * condition, or table->cursor->records if no SARGable condition could be
183
 
   * constructed.
184
 
   * This value is used by join optimizer as an estimate of number of records
185
 
   * that will pass the table condition (condition that depends on fields of
186
 
   * this table and constants)
187
 
   */
 
172
  /*
 
173
    Estimate of number of records that satisfy SARGable part of the table
 
174
    condition, or table->file->records if no SARGable condition could be
 
175
    constructed.
 
176
    This value is used by join optimizer as an estimate of number of records
 
177
    that will pass the table condition (condition that depends on fields of
 
178
    this table and constants)
 
179
  */
188
180
  ha_rows quick_condition_rows;
189
181
 
190
182
  /*
200
192
    as example).
201
193
  */
202
194
  timestamp_auto_set_type timestamp_field_type;
203
 
  table_map map; ///< ID bit of table (1,2,4,8,16...)
 
195
  table_map     map; /* ID bit of table (1,2,4,8,16...) */
204
196
 
205
197
  RegInfo reginfo; /* field connections */
206
198
 
233
225
    For each key that has quick_keys.test(key) == true: estimate of #records
234
226
    and max #key parts that range access would use.
235
227
  */
236
 
  ha_rows quick_rows[MAX_KEY];
 
228
  ha_rows       quick_rows[MAX_KEY];
237
229
 
238
230
  /* Bitmaps of key parts that =const for the entire join. */
239
231
  key_part_map  const_key_parts[MAX_KEY];
241
233
  uint32_t quick_key_parts[MAX_KEY];
242
234
  uint32_t quick_n_ranges[MAX_KEY];
243
235
 
244
 
  memory::Root mem_root;
 
236
  MEM_ROOT mem_root;
245
237
  filesort_info_st sort;
246
238
 
247
 
  Table();
 
239
  Table() : 
 
240
    s(NULL), 
 
241
    field(NULL),
 
242
    file(NULL),
 
243
    next(NULL),
 
244
    prev(NULL),
 
245
    read_set(NULL),
 
246
    write_set(NULL),
 
247
    tablenr(0),
 
248
    db_stat(0),
 
249
    in_use(NULL),
 
250
    insert_values(NULL),
 
251
    key_info(NULL),
 
252
    next_number_field(NULL),
 
253
    found_next_number_field(NULL),
 
254
    timestamp_field(NULL),
 
255
    pos_in_table_list(NULL),
 
256
    group(NULL),
 
257
    alias(NULL),
 
258
    null_flags(NULL),
 
259
    lock_position(0),
 
260
    lock_data_start(0),
 
261
    lock_count(0),
 
262
    used_fields(0),
 
263
    status(0),
 
264
    derived_select_number(0),
 
265
    current_lock(F_UNLCK),
 
266
    copy_blobs(false),
 
267
    maybe_null(false),
 
268
    null_row(false),
 
269
    force_index(false),
 
270
    distinct(false),
 
271
    const_table(false),
 
272
    no_rows(false),
 
273
    key_read(false),
 
274
    no_keyread(false),
 
275
    open_placeholder(false),
 
276
    locked_by_name(false),
 
277
    no_cache(false),
 
278
    auto_increment_field_not_null(false),
 
279
    alias_name_used(false),
 
280
    query_id(0), 
 
281
    quick_condition_rows(0),
 
282
    timestamp_field_type(TIMESTAMP_NO_AUTO_SET),
 
283
    map(0)
 
284
  {
 
285
    record[0]= (unsigned char *) 0;
 
286
    record[1]= (unsigned char *) 0;
 
287
 
 
288
    covering_keys.reset();
 
289
 
 
290
    quick_keys.reset();
 
291
    merge_keys.reset();
 
292
 
 
293
    keys_in_use_for_query.reset();
 
294
    keys_in_use_for_group_by.reset();
 
295
    keys_in_use_for_order_by.reset();
 
296
 
 
297
    memset(quick_rows, 0, sizeof(query_id_t) * MAX_KEY);
 
298
    memset(const_key_parts, 0, sizeof(ha_rows) * MAX_KEY);
 
299
 
 
300
    memset(quick_key_parts, 0, sizeof(unsigned int) * MAX_KEY);
 
301
    memset(quick_n_ranges, 0, sizeof(unsigned int) * MAX_KEY);
 
302
 
 
303
    init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
 
304
    memset(&sort, 0, sizeof(filesort_info_st));
 
305
  }
248
306
 
249
307
  int report_error(int error);
250
 
  /**
251
 
   * Free information allocated by openfrm
252
 
   *
253
 
   * @param If true if we also want to free table_share
254
 
   */
255
308
  int closefrm(bool free_share);
256
309
 
257
 
  void resetTable(Session *session, TableShare *share, uint32_t db_stat_arg);
 
310
  void resetTable(Session *session, TableShare *share, uint32_t db_stat_arg)
 
311
  {
 
312
    s= share;
 
313
    field= NULL;
 
314
 
 
315
    file= NULL;
 
316
    next= NULL;
 
317
    prev= NULL;
 
318
 
 
319
    read_set= NULL;
 
320
    write_set= NULL;
 
321
 
 
322
    tablenr= 0;
 
323
    db_stat= db_stat_arg;
 
324
 
 
325
    in_use= session;
 
326
    record[0]= (unsigned char *) 0;
 
327
    record[1]= (unsigned char *) 0;
 
328
 
 
329
    insert_values= NULL;
 
330
    key_info= NULL;
 
331
    next_number_field= NULL;
 
332
    found_next_number_field= NULL;
 
333
    timestamp_field= NULL;
 
334
 
 
335
    pos_in_table_list= NULL;
 
336
    group= NULL;
 
337
    alias= NULL;
 
338
    null_flags= NULL;
 
339
     
 
340
    lock_position= 0;
 
341
    lock_data_start= 0;
 
342
    lock_count= 0;
 
343
    used_fields= 0;
 
344
    status= 0;
 
345
    derived_select_number= 0;
 
346
    current_lock= F_UNLCK;
 
347
    copy_blobs= false;
 
348
 
 
349
    maybe_null= false;
 
350
 
 
351
    null_row= false;
 
352
 
 
353
    force_index= false;
 
354
    distinct= false;
 
355
    const_table= false;
 
356
    no_rows= false;
 
357
    key_read= false;
 
358
    no_keyread= false;
 
359
 
 
360
    open_placeholder= false;
 
361
    locked_by_name= false;
 
362
    no_cache= false;
 
363
 
 
364
    auto_increment_field_not_null= false;
 
365
    alias_name_used= false;
 
366
    
 
367
    query_id= 0;
 
368
    quick_condition_rows= 0;
 
369
     
 
370
    timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
 
371
    map= 0;
 
372
 
 
373
    reginfo.reset();
 
374
 
 
375
    covering_keys.reset();
 
376
 
 
377
    quick_keys.reset();
 
378
    merge_keys.reset();
 
379
 
 
380
    keys_in_use_for_query.reset();
 
381
    keys_in_use_for_group_by.reset();
 
382
    keys_in_use_for_order_by.reset();
 
383
 
 
384
    memset(quick_rows, 0, sizeof(query_id_t) * MAX_KEY);
 
385
    memset(const_key_parts, 0, sizeof(ha_rows) * MAX_KEY);
 
386
 
 
387
    memset(quick_key_parts, 0, sizeof(unsigned int) * MAX_KEY);
 
388
    memset(quick_n_ranges, 0, sizeof(unsigned int) * MAX_KEY);
 
389
 
 
390
    init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
 
391
    memset(&sort, 0, sizeof(filesort_info_st));
 
392
  }
258
393
 
259
394
  /* SHARE methods */
260
 
  inline const TableShare *getShare() const { assert(s); return s; } /* Get rid of this long term */
 
395
  inline TableShare *getShare() { return s; } /* Get rid of this long term */
261
396
  inline void setShare(TableShare *new_share) { s= new_share; } /* Get rid of this long term */
262
397
  inline uint32_t sizeKeys() { return s->keys; }
263
398
  inline uint32_t sizeFields() { return s->fields; }
269
404
  inline unsigned char *getDefaultValues() { return s->default_values; }
270
405
 
271
406
  inline bool isDatabaseLowByteFirst() { return s->db_low_byte_first; } /* Portable row format */
 
407
  inline bool isCrashed() { return s->crashed; }
272
408
  inline bool isNameLock() { return s->name_lock; }
273
409
  inline bool isReplaceWithNameLock() { return s->replace_with_name_lock; }
274
410
  inline bool isWaitingOnCondition() { return s->waiting_on_cond; } /* Protection against free */
275
411
 
276
 
  uint32_t index_flags(uint32_t idx) const
277
 
  {
278
 
    return s->storage_engine->index_flags(s->key_info[idx].algorithm);
279
 
  }
280
 
 
281
 
  inline plugin::StorageEngine *getEngine() const       /* table_type for handler */
282
 
  {
283
 
    return s->storage_engine;
284
 
  }
285
 
 
286
 
  Cursor &getCursor() const     /* table_type for handler */
287
 
  {
288
 
    assert(cursor);
289
 
    return *cursor;
290
 
  }
291
 
 
292
412
  /* For TMP tables, should be pulled out as a class */
293
 
  void updateCreateInfo(message::Table *table_proto);
 
413
  void updateCreateInfo(HA_CREATE_INFO *create_info,
 
414
                        drizzled::message::Table *table_proto);
294
415
  void setup_tmp_table_column_bitmaps(unsigned char *bitmaps);
295
416
  bool create_myisam_tmp_table(KEY *keyinfo,
296
417
                               MI_COLUMNDEF *start_recinfo,
313
434
  /* See if this can be blown away */
314
435
  inline uint32_t getDBStat () { return db_stat; }
315
436
  inline uint32_t setDBStat () { return db_stat; }
316
 
  /**
317
 
   * Create Item_field for each column in the table.
318
 
   *
319
 
   * @param[out] a pointer to an empty list used to store items
320
 
   *
321
 
   * @details
322
 
   *
323
 
   * Create Item_field object for each column in the table and
324
 
   * initialize it with the corresponding Field. New items are
325
 
   * created in the current Session memory root.
326
 
   *
327
 
   * @retval
328
 
   *  false on success
329
 
   * @retval
330
 
   *  true when out of memory
331
 
   */
332
437
  bool fill_item_list(List<Item> *item_list) const;
333
438
  void clear_column_bitmaps(void);
334
439
  void prepare_for_position(void);
346
451
    read_set= read_set_arg;
347
452
    write_set= write_set_arg;
348
453
  }
 
454
  /**
 
455
   * Find field in table, no side effects, only purpose is to check for field
 
456
   * in table object and get reference to the field if found.
 
457
   *
 
458
   * @param Name of field searched for
 
459
   *
 
460
   * @retval
 
461
   *  0 field is not found
 
462
   * @retval
 
463
   *  non-0 pointer to field
 
464
   */
 
465
  Field *find_field_in_table_sef(const char *name);
349
466
 
350
467
  void restore_column_map(my_bitmap_map *old);
351
468
 
377
494
    read_set->setAll();
378
495
  }
379
496
 
380
 
  inline void clearReadSet(uint32_t index)
381
 
  {
382
 
    read_set->clearBit(index);
383
 
  }
384
 
 
385
 
  inline void clearReadSet()
386
 
  {
387
 
    read_set->clearAll();
388
 
  }
389
 
 
390
497
  inline bool isWriteSet(uint32_t index)
391
498
  {
392
499
    return write_set->isBitSet(index);
402
509
    write_set->setAll();
403
510
  }
404
511
 
405
 
  inline void clearWriteSet(uint32_t index)
406
 
  {
407
 
    write_set->clearBit(index);
408
 
  }
409
 
 
410
 
  inline void clearWriteSet()
411
 
  {
412
 
    write_set->clearAll();
413
 
  }
414
 
 
415
512
  /* Is table open or should be treated as such by name-locking? */
416
513
  inline bool is_name_opened()
417
514
  {
440
537
    memset(null_flags, 255, s->null_bytes);
441
538
  }
442
539
 
443
 
  bool renameAlterTemporaryTable(TableIdentifier &identifier);
 
540
  bool rename_temporary_table(const char *db, const char *table_name);
444
541
  void free_io_cache();
445
542
  void filesort_free_buffers(bool full= false);
446
543
  void intern_close_table();
447
 
 
448
 
  void print_error(int error, myf errflag)
449
 
  {
450
 
    s->storage_engine->print_error(error, errflag, *this);
451
 
  }
452
 
 
453
 
  /**
454
 
    @return
455
 
    key if error because of duplicated keys
456
 
  */
457
 
  uint32_t get_dup_key(int error)
458
 
  {
459
 
    cursor->errkey  = (uint32_t) -1;
460
 
    if (error == HA_ERR_FOUND_DUPP_KEY || error == HA_ERR_FOREIGN_DUPLICATE_KEY ||
461
 
        error == HA_ERR_FOUND_DUPP_UNIQUE ||
462
 
        error == HA_ERR_DROP_INDEX_FK)
463
 
      cursor->info(HA_STATUS_ERRKEY | HA_STATUS_NO_LOCK);
464
 
 
465
 
    return(cursor->errkey);
466
 
  }
467
 
 
468
 
  /*
469
 
    This is a short term fix. Long term we will used the TableIdentifier to do the actual comparison.
470
 
  */
471
 
  bool operator<(const Table &right) const
472
 
  {
473
 
    int result= strcasecmp(this->getShare()->getSchemaName(), right.getShare()->getSchemaName());
474
 
 
475
 
    if (result <  0)
476
 
      return true;
477
 
 
478
 
    if (result >  0)
479
 
      return false;
480
 
 
481
 
    result= strcasecmp(this->getShare()->getTableName(), right.getShare()->getTableName());
482
 
 
483
 
    if (result <  0)
484
 
      return true;
485
 
 
486
 
    if (result >  0)
487
 
      return false;
488
 
 
489
 
    if (this->getShare()->getTableProto()->type()  < right.getShare()->getTableProto()->type())
490
 
      return true;
491
 
 
492
 
    return false;
493
 
  }
494
 
 
495
 
  static bool compare(const Table *a, const Table *b)
496
 
  {
497
 
    return *a < *b;
498
 
  }
499
 
 
500
 
  friend std::ostream& operator<<(std::ostream& output, const Table &table)
501
 
  {
502
 
    output << "Table:(";
503
 
    output << table.getShare()->getSchemaName();
504
 
    output << ", ";
505
 
    output <<  table.getShare()->getTableName();
506
 
    output << ", ";
507
 
    output <<  table.getShare()->getTableTypeAsString();
508
 
    output << ")";
509
 
 
510
 
    return output;  // for multiple << operators.
511
 
  }
512
 
 
513
544
};
514
545
 
515
546
Table *create_virtual_tmp_table(Session *session, List<CreateField> &field_list);
537
568
class select_union;
538
569
class Tmp_Table_Param;
539
570
 
 
571
typedef struct st_changed_table_list
 
572
{
 
573
  struct        st_changed_table_list *next;
 
574
  char          *key;
 
575
  uint32_t key_length;
 
576
} CHANGED_TableList;
 
577
 
540
578
struct open_table_list_st
541
579
{
542
 
  std::string   db;
543
 
  std::string   table;
 
580
  string        db;
 
581
  string        table;
544
582
  uint32_t in_use;
545
583
  uint32_t locked;
546
584
 
551
589
 
552
590
};
553
591
 
554
 
TableShare *alloc_table_share(TableList *table_list, char *key,
555
 
                               uint32_t key_length);
556
 
int open_table_def(Session& session, TableIdentifier &identifier, TableShare *share);
557
 
void open_table_error(TableShare *share, int error, int db_errno, int errarg);
558
 
int open_table_from_share(Session *session, TableShare *share, const char *alias,
559
 
                          uint32_t db_stat, uint32_t ha_open_flags,
560
 
                          Table *outparam);
561
 
void free_blobs(Table *table);
562
 
int set_zone(int nr,int min_zone,int max_zone);
563
 
uint32_t convert_period_to_month(uint32_t period);
564
 
uint32_t convert_month_to_period(uint32_t month);
565
 
 
566
 
int test_if_number(char *str,int *res,bool allow_wildcards);
567
 
void change_byte(unsigned char *,uint,char,char);
568
 
 
569
 
namespace optimizer { class SqlSelect; }
570
 
 
571
 
ha_rows filesort(Session *session,
572
 
                 Table *form,
573
 
                 st_sort_field *sortorder,
574
 
                 uint32_t s_length,
575
 
                 optimizer::SqlSelect *select,
576
 
                 ha_rows max_rows,
577
 
                 bool sort_positions,
578
 
                 ha_rows *examined_rows);
579
 
 
580
 
void filesort_free_buffers(Table *table, bool full);
581
 
void change_double_for_sort(double nr,unsigned char *to);
582
 
double my_double_round(double value, int64_t dec, bool dec_unsigned,
583
 
                       bool truncate);
584
 
int get_quick_record(optimizer::SqlSelect *select);
585
 
 
586
 
void find_date(char *pos,uint32_t *vek,uint32_t flag);
587
 
TYPELIB *convert_strings_to_array_type(char * *typelibs, char * *end);
588
 
TYPELIB *typelib(memory::Root *mem_root, List<String> &strings);
589
 
ulong get_form_pos(int file, unsigned char *head, TYPELIB *save_names);
590
 
ulong next_io_size(ulong pos);
591
 
void append_unescaped(String *res, const char *pos, uint32_t length);
592
 
 
593
 
int rename_file_ext(const char * from,const char * to,const char * ext);
594
 
bool check_column_name(const char *name);
595
 
bool check_db_name(SchemaIdentifier &schema);
596
 
bool check_table_name(const char *name, uint32_t length);
597
 
 
598
 
} /* namespace drizzled */
599
 
 
600
592
#endif /* DRIZZLED_TABLE_H */