~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table.h

  • Committer: Brian Aker
  • Date: 2010-08-09 18:04:12 UTC
  • mfrom: (1689.3.7 staging)
  • Revision ID: brian@gaz-20100809180412-olurwh51ojllev6p
Merge in heap conversion, and case insensitive patch, and remove need for
M_HASH in session.

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
20
20
 
21
21
/* Structs that defines the Table */
22
22
 
23
 
 
24
 
 
25
23
#ifndef DRIZZLED_TABLE_H
26
24
#define DRIZZLED_TABLE_H
27
25
 
28
26
#include <string>
29
 
#include <boost/dynamic_bitset.hpp>
30
 
 
31
 
#include <drizzled/order.h>
32
 
#include <drizzled/filesort_info.h>
33
 
#include <drizzled/natural_join_column.h>
34
 
#include <drizzled/field_iterator.h>
35
 
#include <drizzled/cursor.h>
36
 
#include <drizzled/lex_string.h>
37
 
#include <drizzled/table/instance.h>
38
 
#include <drizzled/atomics.h>
39
 
#include <drizzled/query_id.h>
40
 
 
41
 
#include <drizzled/visibility.h>
 
27
 
 
28
#include "drizzled/order.h"
 
29
#include "drizzled/filesort_info.h"
 
30
#include "drizzled/natural_join_column.h"
 
31
#include "drizzled/field_iterator.h"
 
32
#include "drizzled/cursor.h"
 
33
#include "drizzled/lex_string.h"
 
34
#include "drizzled/table_list.h"
 
35
#include "drizzled/table_share.h"
 
36
#include "drizzled/atomics.h"
 
37
#include "drizzled/query_id.h"
42
38
 
43
39
namespace drizzled
44
40
{
45
41
 
46
 
class COND_EQUAL;
47
 
class Field_blob;
48
42
class Item;
49
43
class Item_subselect;
 
44
class Select_Lex_Unit;
 
45
class Select_Lex;
 
46
class COND_EQUAL;
50
47
class SecurityContext;
51
 
class Select_Lex;
52
 
class Select_Lex_Unit;
53
48
class TableList;
54
 
namespace field { class Epoch; }
55
 
namespace plugin { class StorageEngine; }
 
49
class Field_timestamp;
 
50
class Field_blob;
 
51
 
 
52
extern uint64_t refresh_version;
56
53
 
57
54
typedef enum enum_table_category TABLE_CATEGORY;
58
55
typedef struct st_columndef MI_COLUMNDEF;
61
58
 * Class representing a set of records, either in a temporary, 
62
59
 * normal, or derived table.
63
60
 */
64
 
class DRIZZLED_API Table 
 
61
class Table 
65
62
{
 
63
public:
 
64
  TableShare *s; /**< Pointer to the shared metadata about the table */
 
65
 
 
66
private:
66
67
  Field **field; /**< Pointer to fields collection */
 
68
public:
67
69
 
68
 
public:
69
70
  Field **getFields() const
70
71
  {
71
72
    return field;
87
88
  }
88
89
 
89
90
  Cursor *cursor; /**< Pointer to the storage engine's Cursor managing this table */
90
 
 
91
91
private:
92
92
  Table *next;
93
 
 
94
93
public:
95
94
  Table *getNext() const
96
95
  {
131
130
    prev= arg;
132
131
  }
133
132
 
134
 
  boost::dynamic_bitset<> *read_set; /* Active column sets */
135
 
  boost::dynamic_bitset<> *write_set; /* Active column sets */
 
133
  MyBitmap *read_set; /* Active column sets */
 
134
  MyBitmap *write_set; /* Active column sets */
136
135
 
137
136
  uint32_t tablenr;
138
137
  uint32_t db_stat; /**< information about the cursor as in Cursor.h */
139
138
 
140
 
  boost::dynamic_bitset<> def_read_set; /**< Default read set of columns */
141
 
  boost::dynamic_bitset<> def_write_set; /**< Default write set of columns */
142
 
  boost::dynamic_bitset<> tmp_set; /* Not sure about this... */
 
139
  MyBitmap def_read_set; /**< Default read set of columns */
 
140
  MyBitmap def_write_set; /**< Default write set of columns */
 
141
  MyBitmap tmp_set; /* Not sure about this... */
143
142
 
144
143
  Session *in_use; /**< Pointer to the current session using this object */
145
144
  Session *getSession()
147
146
    return in_use;
148
147
  }
149
148
 
150
 
  unsigned char *getInsertRecord() const
 
149
  unsigned char *getInsertRecord()
151
150
  {
152
151
    return record[0];
153
152
  }
162
161
  KeyInfo  *key_info; /**< data of keys in database */
163
162
  Field *next_number_field; /**< Set if next_number is activated. @TODO What the heck is the difference between this and the next member? */
164
163
  Field *found_next_number_field; /**< Points to the "next-number" field (autoincrement field) */
165
 
  field::Epoch *timestamp_field; /**< Points to the auto-setting timestamp field, if any */
 
164
  Field_timestamp *timestamp_field; /**< Points to the auto-setting timestamp field, if any */
166
165
 
167
166
  TableList *pos_in_table_list; /* Element referring to this table */
168
 
  Order *group;
 
167
  order_st *group;
169
168
  
170
169
  const char *getAlias() const
171
170
  {
172
 
    return _alias.c_str();
173
 
  }
174
 
 
175
 
  void clearAlias()
176
 
  {
177
 
    _alias.clear();
178
 
  }
179
 
 
180
 
  void setAlias(const char *arg)
181
 
  {
182
 
    _alias= arg;
183
 
  }
184
 
 
185
 
private:
186
 
  std::string _alias; /**< alias or table name if no alias */
187
 
public:
 
171
    return alias;
 
172
  }
 
173
 
 
174
  const char *alias; /**< alias or table name if no alias */
188
175
 
189
176
  unsigned char *null_flags;
190
177
 
284
271
    statement then the variable contains TIMESTAMP_NO_AUTO_SET (i.e. 0).
285
272
 
286
273
    Value of this variable is set for each statement in open_table() and
287
 
    if needed cleared later in statement processing code (see update_query()
 
274
    if needed cleared later in statement processing code (see mysql_update()
288
275
    as example).
289
276
  */
290
277
  timestamp_auto_set_type timestamp_field_type;
312
299
    The set is implemented as a bitmap.
313
300
  */
314
301
  key_map keys_in_use_for_query;
315
 
 
316
302
  /* Map of keys that can be used to calculate GROUP BY without sorting */
317
303
  key_map keys_in_use_for_group_by;
318
 
 
319
304
  /* Map of keys that can be used to calculate ORDER BY without sorting */
320
305
  key_map keys_in_use_for_order_by;
321
306
 
369
354
    return mem_root.strmake_root(str_arg, len_arg);
370
355
  }
371
356
 
372
 
  filesort_info sort;
 
357
  filesort_info_st sort;
373
358
 
374
359
  Table();
375
 
  virtual ~Table();
 
360
  virtual ~Table() { };
376
361
 
377
362
  int report_error(int error);
378
363
  /**
386
371
  void resetTable(Session *session, TableShare *share, uint32_t db_stat_arg);
387
372
 
388
373
  /* SHARE methods */
389
 
  virtual const TableShare *getShare() const= 0; /* Get rid of this long term */
390
 
  virtual TableShare *getMutableShare()= 0; /* Get rid of this long term */
391
 
  virtual bool hasShare() const= 0; /* Get rid of this long term */
392
 
  virtual void setShare(TableShare *new_share)= 0; /* Get rid of this long term */
393
 
 
394
 
  virtual void release(void)= 0;
395
 
 
396
 
  uint32_t sizeKeys() { return getMutableShare()->sizeKeys(); }
397
 
  uint32_t sizeFields() { return getMutableShare()->sizeFields(); }
398
 
  uint32_t getRecordLength() const { return getShare()->getRecordLength(); }
399
 
  uint32_t sizeBlobFields() { return getMutableShare()->blob_fields; }
400
 
  uint32_t *getBlobField() { return &getMutableShare()->blob_field[0]; }
401
 
 
402
 
public:
403
 
  virtual bool hasVariableWidth() const
404
 
  {
405
 
    return getShare()->hasVariableWidth(); // We should calculate this.
406
 
  }
407
 
 
408
 
  virtual void setVariableWidth(void);
 
374
  inline const TableShare *getShare() const { assert(s); return s; } /* Get rid of this long term */
 
375
  inline bool hasShare() const { return s ? true : false ; } /* Get rid of this long term */
 
376
  inline TableShare *getMutableShare() { assert(s); return s; } /* Get rid of this long term */
 
377
  inline void setShare(TableShare *new_share) { s= new_share; } /* Get rid of this long term */
 
378
  inline uint32_t sizeKeys() { return s->sizeKeys(); }
 
379
  inline uint32_t sizeFields() { return s->sizeFields(); }
 
380
  inline uint32_t getRecordLength() const { return s->getRecordLength(); }
 
381
  inline uint32_t sizeBlobFields() { return s->blob_fields; }
 
382
  inline uint32_t *getBlobField() { return &s->blob_field[0]; }
409
383
 
410
384
  Field_blob *getBlobFieldAt(uint32_t arg) const
411
385
  {
412
 
    if (arg < getShare()->blob_fields)
413
 
      return (Field_blob*) field[getShare()->blob_field[arg]]; /*NOTE: Using 'Table.field' NOT SharedTable.field. */
 
386
    if (arg < s->blob_fields)
 
387
      return (Field_blob*) field[s->blob_field[arg]]; /*NOTE: Using 'Table.field' NOT SharedTable.field. */
414
388
 
415
389
    return NULL;
416
390
  }
417
 
  inline uint8_t getBlobPtrSize() const { return getShare()->sizeBlobPtr(); }
418
 
  inline uint32_t getNullBytes() const { return getShare()->null_bytes; }
419
 
  inline uint32_t getNullFields() const { return getShare()->null_fields; }
420
 
  inline unsigned char *getDefaultValues() { return  getMutableShare()->getDefaultValues(); }
421
 
  inline const char *getSchemaName()  const { return getShare()->getSchemaName(); }
422
 
  inline const char *getTableName()  const { return getShare()->getTableName(); }
423
 
 
424
 
  inline bool isDatabaseLowByteFirst() const { return getShare()->db_low_byte_first; } /* Portable row format */
425
 
  inline bool isNameLock() const { return open_placeholder; }
426
 
 
427
 
  uint32_t index_flags(uint32_t idx) const;
 
391
  inline uint8_t getBlobPtrSize() { return s->blob_ptr_size; }
 
392
  inline uint32_t getNullBytes() { return s->null_bytes; }
 
393
  inline uint32_t getNullFields() { return s->null_fields; }
 
394
  inline unsigned char *getDefaultValues() { return  s->getDefaultValues(); }
 
395
  inline const char *getSchemaName()  const { return s->getSchemaName(); }
 
396
  inline const char *getTableName()  const { return s->getTableName(); }
 
397
 
 
398
  inline bool isDatabaseLowByteFirst() { return s->db_low_byte_first; } /* Portable row format */
 
399
  inline bool isNameLock() const { return s->isNameLock(); }
 
400
  inline bool isReplaceWithNameLock() { return s->replace_with_name_lock; }
 
401
 
 
402
  uint32_t index_flags(uint32_t idx) const
 
403
  {
 
404
    return s->storage_engine->index_flags(s->getKeyInfo(idx).algorithm);
 
405
  }
428
406
 
429
407
  inline plugin::StorageEngine *getEngine() const   /* table_type for handler */
430
408
  {
431
 
    return getShare()->getEngine();
 
409
    return s->storage_engine;
432
410
  }
433
411
 
434
412
  Cursor &getCursor() const /* table_type for handler */
437
415
    return *cursor;
438
416
  }
439
417
 
 
418
  /* For TMP tables, should be pulled out as a class */
 
419
  void setup_tmp_table_column_bitmaps(unsigned char *bitmaps);
 
420
  bool create_myisam_tmp_table(KeyInfo *keyinfo,
 
421
                               MI_COLUMNDEF *start_recinfo,
 
422
                               MI_COLUMNDEF **recinfo,
 
423
                               uint64_t options);
 
424
  void free_tmp_table(Session *session);
 
425
  bool open_tmp_table();
440
426
  size_t max_row_length(const unsigned char *data);
441
427
  uint32_t find_shortest_key(const key_map *usable_keys);
442
428
  bool compare_record(Field **ptr);
443
 
  bool records_are_comparable();
444
 
  bool compare_records();
 
429
  bool compare_record();
445
430
  /* TODO: the (re)storeRecord's may be able to be further condensed */
446
431
  void storeRecord();
447
432
  void storeRecordAsInsert();
450
435
  void restoreRecordAsDefault();
451
436
  void emptyRecord();
452
437
 
453
 
 
454
438
  /* See if this can be blown away */
455
439
  inline uint32_t getDBStat () { return db_stat; }
456
440
  inline uint32_t setDBStat () { return db_stat; }
473
457
  bool fill_item_list(List<Item> *item_list) const;
474
458
  void clear_column_bitmaps(void);
475
459
  void prepare_for_position(void);
476
 
  void mark_columns_used_by_index_no_reset(uint32_t index, boost::dynamic_bitset<>& bitmap);
 
460
  void mark_columns_used_by_index_no_reset(uint32_t index, MyBitmap *map);
477
461
  void mark_columns_used_by_index_no_reset(uint32_t index);
478
462
  void mark_columns_used_by_index(uint32_t index);
479
463
  void restore_column_maps_after_mark_index();
481
465
  void mark_columns_needed_for_update(void);
482
466
  void mark_columns_needed_for_delete(void);
483
467
  void mark_columns_needed_for_insert(void);
484
 
  void column_bitmaps_set(boost::dynamic_bitset<>& read_set_arg,
485
 
                          boost::dynamic_bitset<>& write_set_arg);
486
 
 
487
 
  void restore_column_map(const boost::dynamic_bitset<>& old);
488
 
 
489
 
  const boost::dynamic_bitset<> use_all_columns(boost::dynamic_bitset<>& map);
 
468
  inline void column_bitmaps_set(MyBitmap *read_set_arg,
 
469
                                 MyBitmap *write_set_arg)
 
470
  {
 
471
    read_set= read_set_arg;
 
472
    write_set= write_set_arg;
 
473
  }
 
474
 
 
475
  void restore_column_map(my_bitmap_map *old);
 
476
 
 
477
  my_bitmap_map *use_all_columns(MyBitmap *bitmap);
490
478
  inline void use_all_columns()
491
479
  {
492
 
    column_bitmaps_set(getMutableShare()->all_set, getMutableShare()->all_set);
 
480
    column_bitmaps_set(&s->all_set, &s->all_set);
493
481
  }
494
482
 
495
483
  inline void default_column_bitmaps()
499
487
  }
500
488
 
501
489
  /* Both of the below should go away once we can move this bit to the field objects */
502
 
  inline bool isReadSet(uint32_t index) const
 
490
  inline bool isReadSet(uint32_t index)
503
491
  {
504
 
    return read_set->test(index);
 
492
    return read_set->isBitSet(index);
505
493
  }
506
494
 
507
495
  inline void setReadSet(uint32_t index)
508
496
  {
509
 
    read_set->set(index);
 
497
    read_set->setBit(index);
510
498
  }
511
499
 
512
500
  inline void setReadSet()
513
501
  {
514
 
    read_set->set();
 
502
    read_set->setAll();
515
503
  }
516
504
 
517
505
  inline void clearReadSet(uint32_t index)
518
506
  {
519
 
    read_set->reset(index);
 
507
    read_set->clearBit(index);
520
508
  }
521
509
 
522
510
  inline void clearReadSet()
523
511
  {
524
 
    read_set->reset();
 
512
    read_set->clearAll();
525
513
  }
526
514
 
527
515
  inline bool isWriteSet(uint32_t index)
528
516
  {
529
 
    return write_set->test(index);
 
517
    return write_set->isBitSet(index);
530
518
  }
531
519
 
532
520
  inline void setWriteSet(uint32_t index)
533
521
  {
534
 
    write_set->set(index);
 
522
    write_set->setBit(index);
535
523
  }
536
524
 
537
525
  inline void setWriteSet()
538
526
  {
539
 
    write_set->set();
 
527
    write_set->setAll();
540
528
  }
541
529
 
542
530
  inline void clearWriteSet(uint32_t index)
543
531
  {
544
 
    write_set->reset(index);
 
532
    write_set->clearBit(index);
545
533
  }
546
534
 
547
535
  inline void clearWriteSet()
548
536
  {
549
 
    write_set->reset();
 
537
    write_set->clearAll();
550
538
  }
551
539
 
552
540
  /* Is table open or should be treated as such by name-locking? */
554
542
  {
555
543
    return db_stat || open_placeholder;
556
544
  }
557
 
 
558
545
  /*
559
546
    Is this instance of the table should be reopen or represents a name-lock?
560
547
  */
561
 
  bool needs_reopen_or_name_lock() const;
 
548
  inline bool needs_reopen_or_name_lock()
 
549
  { 
 
550
    return s->getVersion() != refresh_version;
 
551
  }
562
552
 
563
553
  /**
564
554
    clean/setup table fields and map.
572
562
  {
573
563
    null_row= 1;
574
564
    status|= STATUS_NULL_ROW;
575
 
    memset(null_flags, 255, getShare()->null_bytes);
 
565
    memset(null_flags, 255, s->null_bytes);
576
566
  }
577
567
 
578
568
  void free_io_cache();
579
569
  void filesort_free_buffers(bool full= false);
580
570
  void intern_close_table();
581
571
 
582
 
  void print_error(int error, myf errflag) const;
 
572
  void print_error(int error, myf errflag)
 
573
  {
 
574
    s->storage_engine->print_error(error, errflag, *this);
 
575
  }
583
576
 
584
577
  /**
585
578
    @return
586
579
    key if error because of duplicated keys
587
580
  */
588
 
  uint32_t get_dup_key(int error) const
 
581
  uint32_t get_dup_key(int error)
589
582
  {
590
583
    cursor->errkey  = (uint32_t) -1;
591
584
    if (error == HA_ERR_FOUND_DUPP_KEY || error == HA_ERR_FOREIGN_DUPLICATE_KEY ||
601
594
  */
602
595
  bool operator<(const Table &right) const
603
596
  {
604
 
    return getShare()->getCacheKey() < right.getShare()->getCacheKey();
 
597
    int result= strcasecmp(this->getShare()->getSchemaName(), right.getShare()->getSchemaName());
 
598
 
 
599
    if (result <  0)
 
600
      return true;
 
601
 
 
602
    if (result >  0)
 
603
      return false;
 
604
 
 
605
    result= strcasecmp(this->getShare()->getTableName(), right.getShare()->getTableName());
 
606
 
 
607
    if (result <  0)
 
608
      return true;
 
609
 
 
610
    if (result >  0)
 
611
      return false;
 
612
 
 
613
    if (this->getShare()->getTableProto()->type()  < right.getShare()->getTableProto()->type())
 
614
      return true;
 
615
 
 
616
    return false;
605
617
  }
606
618
 
607
619
  static bool compare(const Table *a, const Table *b)
611
623
 
612
624
  friend std::ostream& operator<<(std::ostream& output, const Table &table)
613
625
  {
614
 
    if (table.getShare())
615
 
    {
616
 
      output << "Table:(";
617
 
      output << table.getShare()->getSchemaName();
618
 
      output << ", ";
619
 
      output <<  table.getShare()->getTableName();
620
 
      output << ", ";
621
 
      output <<  table.getShare()->getTableTypeAsString();
622
 
      output << ")";
623
 
    }
624
 
    else
625
 
    {
626
 
      output << "Table:(has no share)";
627
 
    }
 
626
    output << "Table:(";
 
627
    output << table.getShare()->getSchemaName();
 
628
    output << ", ";
 
629
    output <<  table.getShare()->getTableName();
 
630
    output << ", ";
 
631
    output <<  table.getShare()->getTableTypeAsString();
 
632
    output << ")";
628
633
 
629
634
    return output;  // for multiple << operators.
630
635
  }
631
636
 
 
637
protected:
 
638
  bool is_placeholder_created;
 
639
 
632
640
public:
633
 
  virtual bool isPlaceHolder(void) const
 
641
  bool isPlaceHolder()
634
642
  {
635
 
    return false;
 
643
    return is_placeholder_created;
636
644
  }
637
645
};
638
646
 
818
826
class select_union;
819
827
class Tmp_Table_Param;
820
828
 
 
829
struct open_table_list_st
 
830
{
 
831
  std::string   db;
 
832
  std::string   table;
 
833
  uint32_t in_use;
 
834
  uint32_t locked;
 
835
 
 
836
  open_table_list_st() :
 
837
    in_use(0),
 
838
    locked(0)
 
839
  { }
 
840
 
 
841
};
 
842
 
821
843
void free_blobs(Table *table);
822
844
int set_zone(int nr,int min_zone,int max_zone);
823
845
uint32_t convert_period_to_month(uint32_t period);
828
850
 
829
851
namespace optimizer { class SqlSelect; }
830
852
 
 
853
ha_rows filesort(Session *session,
 
854
                 Table *form,
 
855
                 st_sort_field *sortorder,
 
856
                 uint32_t s_length,
 
857
                 optimizer::SqlSelect *select,
 
858
                 ha_rows max_rows,
 
859
                 bool sort_positions,
 
860
                 ha_rows *examined_rows);
 
861
 
 
862
void filesort_free_buffers(Table *table, bool full);
831
863
void change_double_for_sort(double nr,unsigned char *to);
 
864
double my_double_round(double value, int64_t dec, bool dec_unsigned,
 
865
                       bool truncate);
832
866
int get_quick_record(optimizer::SqlSelect *select);
833
867
 
834
868
void find_date(char *pos,uint32_t *vek,uint32_t flag);
835
869
TYPELIB *convert_strings_to_array_type(char * *typelibs, char * *end);
836
870
TYPELIB *typelib(memory::Root *mem_root, List<String> &strings);
837
871
ulong get_form_pos(int file, unsigned char *head, TYPELIB *save_names);
 
872
ulong next_io_size(ulong pos);
838
873
void append_unescaped(String *res, const char *pos, uint32_t length);
839
874
 
840
 
DRIZZLED_API int rename_file_ext(const char * from,const char * to,const char * ext);
 
875
int rename_file_ext(const char * from,const char * to,const char * ext);
841
876
bool check_column_name(const char *name);
 
877
bool check_db_name(Session *session, SchemaIdentifier &schema);
842
878
bool check_table_name(const char *name, uint32_t length);
843
879
 
844
880
} /* namespace drizzled */
845
881
 
846
 
#include <drizzled/table/singular.h>
847
 
#include <drizzled/table/concurrent.h>
848
 
 
849
882
#endif /* DRIZZLED_TABLE_H */