22
22
This class is shared between different table objects. There is one
23
23
instance of table share per one table in the database.
26
#ifndef DRIZZLED_DEFINITION_TABLE_H
27
#define DRIZZLED_DEFINITION_TABLE_H
31
#include <boost/unordered_map.hpp>
32
#include <boost/thread/condition_variable.hpp>
33
#include <boost/dynamic_bitset.hpp>
34
#include <boost/shared_ptr.hpp>
36
#include "drizzled/typelib.h"
37
#include "drizzled/memory/root.h"
38
#include "drizzled/message/table.pb.h"
39
#include "drizzled/util/string.h"
41
#include "drizzled/table/cache.h"
46
extern uint64_t refresh_version;
48
const static std::string STANDARD_STRING("STANDARD");
49
const static std::string TEMPORARY_STRING("TEMPORARY");
50
const static std::string INTERNAL_STRING("INTERNAL");
51
const static std::string FUNCTION_STRING("FUNCTION");
52
const static std::string NO_PROTOBUFFER_AVAILABLE("NO PROTOBUFFER AVAILABLE");
56
class EventObserverList;
63
typedef std::vector<std::string> StringVector;
66
typedef boost::shared_ptr<TableShare> shared_ptr;
67
typedef std::vector <shared_ptr> vector;
69
TableShare(const TableIdentifier::Type type_arg);
71
TableShare(const TableIdentifier &identifier, const TableIdentifier::Key &key); // Used by placeholder
73
TableShare(const TableIdentifier &identifier); // Just used during createTable()
75
TableShare(const TableIdentifier::Type type_arg,
76
const TableIdentifier &identifier,
77
char *path_arg= NULL, uint32_t path_length_arg= 0); // Shares for cache
28
TABLE_SHARE() {} /* Remove gcc warning */
82
30
/** Category of this table. */
83
31
enum_table_category table_category;
87
bool isTemporaryCategory() const
89
return (table_category == TABLE_CATEGORY_TEMPORARY);
92
void setTableCategory(enum_table_category arg)
33
/* hash of field names (contains pointers to elements of field array) */
34
HASH name_hash; /* hash of field names */
36
TYPELIB keynames; /* Pointers to keynames */
37
TYPELIB fieldnames; /* Pointer to fieldnames */
38
TYPELIB *intervals; /* pointer to interval info */
39
pthread_mutex_t mutex; /* For locking the share */
40
pthread_cond_t cond; /* To signal that share is ready */
41
TABLE_SHARE *next, /* Link to unused shares */
97
44
/* The following is copied to each Table on OPEN */
98
typedef std::vector<Field *> Fields;
102
const Fields getFields() const
107
Field ** getFields(bool)
112
void setFields(uint32_t arg)
117
uint32_t positionFields(Field **arg) const
119
return (arg - (Field **)&field[0]);
122
void pushField(Field *arg)
125
field.push_back(arg);
129
46
Field **found_next_number_field;
131
47
Field *timestamp_field; /* Used only during open */
134
Field *getTimestampField() const /* Used only during open */
136
return timestamp_field;
139
void setTimestampField(Field *arg) /* Used only during open */
141
timestamp_field= arg;
146
KeyInfo *key_info; /* data of keys in database */
148
KeyInfo &getKeyInfo(uint32_t arg) const
150
return key_info[arg];
152
std::vector<uint> blob_field; /* Index to blobs in Field arrray*/
154
/* hash of field names (contains pointers to elements of field array) */
156
typedef boost::unordered_map < std::string, Field **, util::insensitive_hash, util::insensitive_equal_to> FieldMap;
157
typedef std::pair< std::string, Field ** > FieldMapPair;
158
FieldMap name_hash; /* hash of field names */
160
size_t getNamedFieldSize() const
162
return name_hash.size();
165
Field **getNamedField(const std::string &arg)
167
FieldMap::iterator iter= name_hash.find(arg);
169
if (iter == name_hash.end())
172
return (*iter).second;
176
memory::Root mem_root;
178
void *alloc_root(size_t arg)
180
return mem_root.alloc_root(arg);
183
char *strmake_root(const char *str_arg, size_t len_arg)
185
return mem_root.strmake_root(str_arg, len_arg);
188
memory::Root *getMemRoot()
193
std::vector<std::string> _keynames;
195
void addKeyName(std::string arg)
197
std::transform(arg.begin(), arg.end(),
198
arg.begin(), ::toupper);
199
_keynames.push_back(arg);
202
bool doesKeyNameExist(const char *name_arg, uint32_t name_length, uint32_t &position) const
204
return doesKeyNameExist(std::string(name_arg, name_length), position);
207
bool doesKeyNameExist(std::string arg, uint32_t &position) const
209
std::transform(arg.begin(), arg.end(),
210
arg.begin(), ::toupper);
212
std::vector<std::string>::const_iterator iter= std::find(_keynames.begin(), _keynames.end(), arg);
214
if (iter == _keynames.end())
216
position= UINT32_MAX; //historical, required for finding primary key from unique
220
position= iter - _keynames.begin();
226
std::vector<TYPELIB> intervals; /* pointer to interval info */
228
boost::mutex mutex; /* For locking the share */
229
boost::condition_variable cond; /* To signal that share is ready */
242
std::vector<unsigned char> default_values; /* row with default values */
244
// @note This needs to be made to be const in the future
245
unsigned char *getDefaultValues()
247
return &default_values[0];
249
void resizeDefaultValues(size_t arg)
251
default_values.resize(arg);
48
KEY *key_info; /* data of keys in database */
49
uint *blob_field; /* Index to blobs in Field arrray*/
51
unsigned char *default_values; /* row with default values */
52
LEX_STRING comment; /* Comment about table */
254
53
const CHARSET_INFO *table_charset; /* Default charset of string fields */
256
boost::dynamic_bitset<> all_set;
259
57
Key which is used for looking-up table in table cache and in the list
260
58
of thread's temporary tables. Has the form of:
261
"database_name\0table_name\0" + optional part for temporary tables.
59
"database_name\0table_name\0" + optional part for temporary tables.
263
61
Note that all three 'table_cache_key', 'db' and 'table_name' members
264
62
must be set (and be non-zero) for tables in table cache. They also
265
63
should correspond to each other.
266
64
To ensure this one can use set_table_cache() methods.
269
TableIdentifier::Key private_key_for_cache; // This will not exist in the final design.
270
std::vector<char> private_normalized_path; // This will not exist in the final design.
66
LEX_STRING table_cache_key;
271
67
LEX_STRING db; /* Pointer to db */
272
68
LEX_STRING table_name; /* Table name (for open) */
273
LEX_STRING path; /* Path to table (from datadir) */
69
LEX_STRING path; /* Path to .frm file (from datadir) */
274
70
LEX_STRING normalized_path; /* unpack_filename(path) */
277
const char *getNormalizedPath() const
279
return normalized_path.str;
282
const char *getPath() const
287
const TableIdentifier::Key& getCacheKey() const // This should never be called when we aren't looking at a cache.
289
assert(private_key_for_cache.size());
290
return private_key_for_cache;
293
size_t getCacheKeySize() const
295
return private_key_for_cache.size();
299
void setPath(char *str_arg, uint32_t size_arg)
302
path.length= size_arg;
305
void setNormalizedPath(char *str_arg, uint32_t size_arg)
307
normalized_path.str= str_arg;
308
normalized_path.length= size_arg;
312
const char *getTableName() const
314
return table_name.str;
317
uint32_t getTableNameSize() const
319
return table_name.length;
322
const std::string &getTableName(std::string &name_arg) const
325
name_arg.append(table_name.str, table_name.length);
330
const char *getSchemaName() const
335
const std::string &getSchemaName(std::string &schema_name_arg) const
337
schema_name_arg.clear();
338
schema_name_arg.append(db.str, db.length);
340
return schema_name_arg;
71
LEX_STRING connect_string;
74
Set of keys in use, implemented as a Bitmap.
75
Excludes keys disabled by ALTER Table ... DISABLE KEYS.
78
key_map keys_for_keyread;
79
ha_rows min_rows, max_rows; /* create information */
80
uint32_t avg_row_length; /* create information */
343
81
uint32_t block_size; /* create information */
348
uint64_t getVersion() const
353
void refreshVersion()
355
version= refresh_version;
82
uint32_t version, mysql_version;
363
83
uint32_t timestamp_offset; /* Set to offset+1 of record */
365
84
uint32_t reclength; /* Recordlength */
367
uint32_t stored_rec_length; /* Stored record length*/
369
uint32_t getRecordLength() const
374
void setRecordLength(uint32_t arg)
379
const Field_blob *getBlobFieldAt(uint32_t arg) const
381
if (arg < blob_fields)
382
return (Field_blob*) field[blob_field[arg]];
388
/* Max rows is a hint to HEAP during a create tmp table */
391
message::Table *table_proto;
395
@note Without a table_proto, we assume we are building a STANDARD table.
396
This will be modified once we use Identifiers in the Share itself.
398
message::Table::TableType getTableType() const
400
return table_proto ? table_proto->type() : message::Table::STANDARD;
403
const std::string &getTableTypeAsString() const
407
switch (table_proto->type())
410
case message::Table::STANDARD:
411
return STANDARD_STRING;
412
case message::Table::TEMPORARY:
413
return TEMPORARY_STRING;
414
case message::Table::INTERNAL:
415
return INTERNAL_STRING;
416
case message::Table::FUNCTION:
417
return FUNCTION_STRING;
422
return NO_PROTOBUFFER_AVAILABLE;
426
/* This is only used in one location currently */
427
inline message::Table *getTableProto() const
432
inline void setTableProto(message::Table *arg)
434
assert(table_proto == NULL);
438
inline bool hasComment() const
440
return (table_proto) ? table_proto->options().has_comment() : false;
443
inline const char *getComment()
445
return (table_proto && table_proto->has_options()) ? table_proto->options().comment().c_str() : NULL;
448
inline uint32_t getCommentLength() const
450
return (table_proto) ? table_proto->options().comment().length() : 0;
453
inline uint64_t getMaxRows() const
458
inline void setMaxRows(uint64_t arg)
464
* Returns true if the supplied Field object
465
* is part of the table's primary key.
467
bool fieldInPrimaryKey(Field *field) const;
469
plugin::StorageEngine *storage_engine; /* storage engine plugin */
470
inline plugin::StorageEngine *db_type() const /* table_type for handler */
472
return storage_engine;
474
inline plugin::StorageEngine *getEngine() const /* table_type for handler */
476
return storage_engine;
480
TableIdentifier::Type tmp_table;
483
TableIdentifier::Type getType() const
85
uint32_t stored_rec_length; /* Stored record length
86
(no generated-only virtual fields) */
88
plugin_ref db_plugin; /* storage engine plugin */
89
inline StorageEngine *db_type() const /* table_type for handler */
92
return db_plugin ? plugin_data(db_plugin, StorageEngine*) : NULL;
94
enum row_type row_type; /* How rows are stored */
95
enum tmp_table_type tmp_table;
96
enum ha_choice page_checksum;
489
98
uint32_t ref_count; /* How many Table objects uses this */
492
uint32_t getTableCount() const
497
void incrementTableCount()
99
uint32_t open_count; /* Number of tables in open list */
100
uint32_t blob_ptr_size; /* 4 or 8 */
101
uint32_t key_block_size; /* create key_block_size, if used */
504
102
uint32_t null_bytes;
505
103
uint32_t last_null_bit_pos;
506
104
uint32_t fields; /* Number of fields */
508
uint32_t sizeFields() const
105
uint32_t stored_fields; /* Number of stored fields
106
(i.e. without generated-only ones) */
513
107
uint32_t rec_buff_length; /* Size of table->record[] buffer */
516
uint32_t sizeKeys() const
108
uint32_t keys, key_parts;
521
109
uint32_t max_key_length, max_unique_length, total_key_length;
522
110
uint32_t uniques; /* Number of UNIQUE index */
523
111
uint32_t null_fields; /* number of null fields */
524
112
uint32_t blob_fields; /* number of blob fields */
526
bool has_variable_width; /* number of varchar fields */
528
bool hasVariableWidth() const
530
return has_variable_width; // We should calculate this.
532
void setVariableWidth()
534
has_variable_width= true;
113
uint32_t timestamp_field_offset; /* Field number for timestamp field */
114
uint32_t varchar_fields; /* number of varchar fields */
536
115
uint32_t db_create_options; /* Create options from database */
537
116
uint32_t db_options_in_use; /* Options in use */
538
117
uint32_t db_record_offset; /* if HA_REC_IN_SEQ */
539
118
uint32_t rowid_field_offset; /* Field_nr +1 to rowid field */
543
* Currently the replication services component uses
544
* the primary_key member to determine which field is the table's
545
* primary key. However, as it exists, because this member is scalar, it
546
* only supports a single-column primary key. Is there a better way
547
* to ask for the fields which are in a primary key?
119
/* Index of auto-updated TIMESTAMP field in field array */
550
120
uint32_t primary_key;
553
uint32_t getPrimaryKey() const
558
bool hasPrimaryKey() const
560
return primary_key != MAX_KEY;
563
/* Index of auto-updated TIMESTAMP field in field array */
564
121
uint32_t next_number_index; /* autoincrement key number */
565
122
uint32_t next_number_key_offset; /* autoinc keypart offset in a key */
566
123
uint32_t next_number_keypart; /* autoinc keypart number in a key */
567
124
uint32_t error, open_errno, errarg; /* error from open_table_def() */
125
uint32_t column_bitmap_size;
569
uint8_t blob_ptr_size; /* 4 or 8 */
127
uint32_t vfields; /* Number of virtual fields */
570
128
bool db_low_byte_first; /* Portable row format */
573
Set of keys in use, implemented as a Bitmap.
574
Excludes keys disabled by ALTER Table ... DISABLE KEYS.
577
key_map keys_for_keyread;
580
event_observers is a class containing all the event plugins that have
581
registered an interest in this table.
584
plugin::EventObserverList *event_observers;
586
plugin::EventObserverList *getTableObservers()
588
return event_observers;
591
void setTableObservers(plugin::EventObserverList *observers)
593
event_observers= observers;
597
Set share's identifier information.
605
void setIdentifier(const TableIdentifier &identifier_arg);
608
Initialize share for temporary tables
613
key Table_cache_key, as generated from create_table_def_key.
614
must start with db name.
615
key_length Length of key
616
table_name Table name
617
path Path to table (possible in lower case)
624
void init(const char *new_table_name,
625
const char *new_path);
628
void open_table_error(int pass_error, int db_errno, int pass_errarg);
630
static void release(TableShare *share);
631
static void release(TableShare::shared_ptr &share);
632
static void release(const TableIdentifier &identifier);
633
static TableShare::shared_ptr getShareCreate(Session *session,
634
const TableIdentifier &identifier,
637
friend std::ostream& operator<<(std::ostream& output, const TableShare &share)
639
output << "TableShare:(";
640
output << share.getSchemaName();
642
output << share.getTableName();
644
output << share.getTableTypeAsString();
646
output << share.getPath();
649
return output; // for multiple << operators.
652
Field *make_field(unsigned char *ptr,
653
uint32_t field_length,
655
unsigned char *null_pos,
656
unsigned char null_bit,
658
enum_field_types field_type,
659
const CHARSET_INFO * field_charset,
660
Field::utype unireg_check,
662
const char *field_name);
664
int open_table_def(Session& session, const TableIdentifier &identifier);
666
int open_table_from_share(Session *session,
667
const TableIdentifier &identifier,
669
uint32_t db_stat, uint32_t ha_open_flags,
672
int open_table_from_share_inner(Session *session,
676
int open_table_cursor_inner(const TableIdentifier &identifier,
677
uint32_t db_stat, uint32_t ha_open_flags,
679
bool &error_reported);
681
int parse_table_proto(Session& session, message::Table &table);
683
int inner_parse_table_proto(Session& session, message::Table &table);
130
bool name_lock, replace_with_name_lock;
131
bool waiting_on_cond; /* Protection against free */
132
uint32_t table_map_id; /* for row-based replication */
133
uint64_t table_map_version;
136
Cache for row-based replication table share checks that does not
137
need to be repeated. Possible values are: -1 when cache value is
138
not calculated yet, 0 when table *shall not* be replicated, 1 when
139
table *may* be replicated.
141
int cached_row_logging_check;
144
Set share's table cache key and update its db and table name appropriately.
147
set_table_cache_key()
148
key_buff Buffer with already built table cache key to be
149
referenced from share.
150
key_length Key length.
153
Since 'key_buff' buffer will be referenced from share it should has same
154
life-time as share itself.
155
This method automatically ensures that TABLE_SHARE::table_name/db have
156
appropriate values by using table cache key as their source.
159
void set_table_cache_key(char *key_buff, uint32_t key_length)
161
table_cache_key.str= key_buff;
162
table_cache_key.length= key_length;
164
Let us use the fact that the key is "db/0/table_name/0" + optional
165
part for temporary tables.
167
db.str= table_cache_key.str;
168
db.length= strlen(db.str);
169
table_name.str= db.str + db.length + 1;
170
table_name.length= strlen(table_name.str);
175
Set share's table cache key and update its db and table name appropriately.
178
set_table_cache_key()
179
key_buff Buffer to be used as storage for table cache key
180
(should be at least key_length bytes).
181
key Value for table cache key.
182
key_length Key length.
185
Since 'key_buff' buffer will be used as storage for table cache key
186
it should has same life-time as share itself.
189
void set_table_cache_key(char *key_buff, const char *key, uint32_t key_length)
191
memcpy(key_buff, key, key_length);
192
set_table_cache_key(key_buff, key_length);
195
inline bool honor_global_locks()
197
return (table_category == TABLE_CATEGORY_USER);
200
inline uint32_t get_table_def_version()
686
} /* namespace drizzled */
688
#endif /* DRIZZLED_DEFINITION_TABLE_H */