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_TABLE_INSTANCE_BASE_H
27
#define DRIZZLED_TABLE_INSTANCE_BASE_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>
35
#include <boost/scoped_ptr.hpp>
37
#include "drizzled/typelib.h"
38
#include "drizzled/memory/root.h"
39
#include "drizzled/message.h"
40
#include "drizzled/util/string.h"
42
#include "drizzled/lex_string.h"
43
#include "drizzled/key_map.h"
45
#include "drizzled/table/cache.h"
47
#include <drizzled/field.h>
53
const static std::string NO_PROTOBUFFER_AVAILABLE("NO PROTOBUFFER AVAILABLE");
57
class EventObserverList;
69
typedef std::vector<std::string> StringVector;
72
typedef boost::shared_ptr<TableShare> shared_ptr;
73
typedef std::vector <shared_ptr> vector;
75
TableShare(const identifier::Table::Type type_arg);
77
TableShare(const identifier::Table &identifier, const identifier::Table::Key &key); // Used by placeholder
79
TableShare(const identifier::Table &identifier); // Just used during createTable()
81
TableShare(const identifier::Table::Type type_arg,
82
const identifier::Table &identifier,
83
char *path_arg= NULL, uint32_t path_length_arg= 0); // Shares for cache
85
virtual ~TableShare();
28
TABLE_SHARE() {} /* Remove gcc warning */
88
30
/** Category of this table. */
89
31
enum_table_category table_category;
92
bool isTemporaryCategory() const
94
return (table_category == TABLE_CATEGORY_TEMPORARY);
97
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 */
102
44
/* The following is copied to each Table on OPEN */
103
typedef std::vector<Field *> Fields;
109
const Fields getFields() const
119
Field ** getFields(bool)
124
void setFields(uint32_t arg)
129
uint32_t positionFields(Field **arg) const
131
return (arg - (Field **)&_fields[0]);
134
void pushField(Field *arg)
137
_fields.push_back(arg);
140
46
Field **found_next_number_field;
143
47
Field *timestamp_field; /* Used only during open */
147
Field *getTimestampField() const /* Used only during open */
149
return timestamp_field;
152
void setTimestampField(Field *arg) /* Used only during open */
154
timestamp_field= arg;
159
KeyInfo *key_info; /* data of keys in database */
162
KeyInfo &getKeyInfo(uint32_t arg) const
164
return key_info[arg];
166
std::vector<uint> blob_field; /* Index to blobs in Field arrray*/
169
/* hash of field names (contains pointers to elements of field array) */
170
typedef boost::unordered_map < std::string, Field **, util::insensitive_hash, util::insensitive_equal_to> FieldMap;
171
typedef std::pair< std::string, Field ** > FieldMapPair;
172
FieldMap name_hash; /* hash of field names */
175
size_t getNamedFieldSize() const
177
return name_hash.size();
180
Field **getNamedField(const std::string &arg)
182
FieldMap::iterator iter= name_hash.find(arg);
184
if (iter == name_hash.end())
187
return (*iter).second;
191
memory::Root mem_root;
193
void *alloc_root(size_t arg)
195
return mem_root.alloc_root(arg);
198
char *strmake_root(const char *str_arg, size_t len_arg)
200
return mem_root.strmake_root(str_arg, len_arg);
203
memory::Root *getMemRoot()
208
std::vector<std::string> _keynames;
210
void addKeyName(std::string arg)
212
std::transform(arg.begin(), arg.end(),
213
arg.begin(), ::toupper);
214
_keynames.push_back(arg);
218
bool doesKeyNameExist(const char *name_arg, uint32_t name_length, uint32_t &position) const
220
return doesKeyNameExist(std::string(name_arg, name_length), position);
223
bool doesKeyNameExist(std::string arg, uint32_t &position) const
225
std::transform(arg.begin(), arg.end(),
226
arg.begin(), ::toupper);
228
std::vector<std::string>::const_iterator iter= std::find(_keynames.begin(), _keynames.end(), arg);
230
if (iter == _keynames.end())
232
position= UINT32_MAX; //historical, required for finding primary key from unique
236
position= iter - _keynames.begin();
242
std::vector<TYPELIB> intervals; /* pointer to interval info */
248
virtual void unlock()
252
std::vector<unsigned char> default_values; /* row with default values */
255
// @note This needs to be made to be const in the future
256
unsigned char *getDefaultValues()
258
return &default_values[0];
260
void resizeDefaultValues(size_t arg)
262
default_values.resize(arg);
265
const charset_info_st *table_charset; /* Default charset of string fields */
267
boost::dynamic_bitset<> all_set;
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 */
53
const CHARSET_INFO *table_charset; /* Default charset of string fields */
270
57
Key which is used for looking-up table in table cache and in the list
271
58
of thread's temporary tables. Has the form of:
272
"database_name\0table_name\0" + optional part for temporary tables.
59
"database_name\0table_name\0" + optional part for temporary tables.
274
61
Note that all three 'table_cache_key', 'db' and 'table_name' members
275
62
must be set (and be non-zero) for tables in table cache. They also
276
63
should correspond to each other.
277
64
To ensure this one can use set_table_cache() methods.
280
identifier::Table::Key private_key_for_cache; // This will not exist in the final design.
281
std::vector<char> private_normalized_path; // This will not exist in the final design.
66
LEX_STRING table_cache_key;
282
67
LEX_STRING db; /* Pointer to db */
283
68
LEX_STRING table_name; /* Table name (for open) */
284
LEX_STRING path; /* Path to table (from datadir) */
69
LEX_STRING path; /* Path to .frm file (from datadir) */
285
70
LEX_STRING normalized_path; /* unpack_filename(path) */
289
const char *getNormalizedPath() const
291
return normalized_path.str;
294
const char *getPath() const
299
const identifier::Table::Key& getCacheKey() const // This should never be called when we aren't looking at a cache.
301
assert(private_key_for_cache.size());
302
return private_key_for_cache;
305
size_t getCacheKeySize() const
307
return private_key_for_cache.size();
311
void setPath(char *str_arg, uint32_t size_arg)
314
path.length= size_arg;
317
void setNormalizedPath(char *str_arg, uint32_t size_arg)
319
normalized_path.str= str_arg;
320
normalized_path.length= size_arg;
325
const char *getTableName() const
327
return table_name.str;
330
uint32_t getTableNameSize() const
332
return table_name.length;
335
const std::string &getTableName(std::string &name_arg) const
338
name_arg.append(table_name.str, table_name.length);
343
const char *getSchemaName() const
348
const std::string &getSchemaName(std::string &schema_name_arg) const
350
schema_name_arg.clear();
351
schema_name_arg.append(db.str, db.length);
353
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 */
356
81
uint32_t block_size; /* create information */
362
uint64_t getVersion() const
367
void refreshVersion();
82
uint32_t version, mysql_version;
375
83
uint32_t timestamp_offset; /* Set to offset+1 of record */
377
uint32_t reclength; /* Recordlength */
378
uint32_t stored_rec_length; /* Stored record length*/
381
uint32_t sizeStoredRecord() const
383
return stored_rec_length;
386
uint32_t getRecordLength() const
391
void setRecordLength(uint32_t arg)
396
const Field_blob *getBlobFieldAt(uint32_t arg) const
398
if (arg < blob_fields)
399
return (Field_blob*) _fields[blob_field[arg]];
405
/* Max rows is a hint to HEAP during a create tmp table */
408
boost::scoped_ptr<message::Table> _table_message;
412
@note Without a _table_message, we assume we are building a STANDARD table.
413
This will be modified once we use Identifiers in the Share itself.
415
message::Table::TableType getTableType() const
417
return getTableMessage() ? getTableMessage()->type() : message::Table::STANDARD;
420
const std::string &getTableTypeAsString() const
422
if (getTableMessage())
423
return message::type(getTableMessage()->type());
425
return NO_PROTOBUFFER_AVAILABLE;
428
/* This is only used in one location currently */
429
inline message::Table *getTableMessage() const
431
return _table_message.get();
434
void setTableMessage(const message::Table &arg)
436
assert(not getTableMessage());
437
_table_message.reset(new(std::nothrow) message::Table(arg));
440
const message::Table::Field &field(int32_t field_position) const
442
assert(getTableMessage());
443
return getTableMessage()->field(field_position);
446
inline bool hasComment() const
448
return (getTableMessage()) ? getTableMessage()->options().has_comment() : false;
451
inline const char *getComment()
453
return (getTableMessage() && getTableMessage()->has_options()) ? getTableMessage()->options().comment().c_str() : NULL;
456
inline uint32_t getCommentLength() const
458
return (getTableMessage()) ? getTableMessage()->options().comment().length() : 0;
461
inline uint64_t getMaxRows() const
466
inline void setMaxRows(uint64_t arg)
472
* Returns true if the supplied Field object
473
* is part of the table's primary key.
475
bool fieldInPrimaryKey(Field *field) const;
477
plugin::StorageEngine *storage_engine; /* storage engine plugin */
478
inline plugin::StorageEngine *db_type() const /* table_type for handler */
480
return storage_engine;
482
inline plugin::StorageEngine *getEngine() const /* table_type for handler */
484
return storage_engine;
488
identifier::Table::Type tmp_table;
491
identifier::Table::Type getType() const
497
uint32_t _ref_count; /* How many Table objects uses this */
500
uint32_t getTableCount() const
505
void incrementTableCount()
512
uint32_t decrementTableCount()
84
uint32_t reclength; /* Recordlength */
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 handlerton *db_type() const /* table_type for handler */
92
return db_plugin ? plugin_data(db_plugin, handlerton*) : NULL;
94
enum row_type row_type; /* How rows are stored */
95
enum tmp_table_type tmp_table;
96
enum ha_choice transactional;
97
enum ha_choice page_checksum;
99
uint32_t ref_count; /* How many Table objects uses this */
100
uint32_t open_count; /* Number of tables in open list */
101
uint32_t blob_ptr_size; /* 4 or 8 */
102
uint32_t key_block_size; /* create key_block_size, if used */
517
103
uint32_t null_bytes;
518
104
uint32_t last_null_bit_pos;
520
uint32_t _field_size; /* Number of fields */
523
void setFieldSize(uint32_t arg)
528
uint32_t sizeFields() const
105
uint32_t fields; /* Number of fields */
106
uint32_t stored_fields; /* Number of stored fields
107
(i.e. without generated-only ones) */
533
108
uint32_t rec_buff_length; /* Size of table->record[] buffer */
536
uint32_t sizeKeys() const
109
uint32_t keys, key_parts;
541
110
uint32_t max_key_length, max_unique_length, total_key_length;
542
111
uint32_t uniques; /* Number of UNIQUE index */
543
112
uint32_t null_fields; /* number of null fields */
544
113
uint32_t blob_fields; /* number of blob fields */
546
bool has_variable_width; /* number of varchar fields */
549
bool hasVariableWidth() const
551
return has_variable_width; // We should calculate this.
553
void setVariableWidth()
555
has_variable_width= true;
114
uint32_t timestamp_field_offset; /* Field number for timestamp field */
115
uint32_t varchar_fields; /* number of varchar fields */
557
116
uint32_t db_create_options; /* Create options from database */
558
117
uint32_t db_options_in_use; /* Options in use */
559
118
uint32_t db_record_offset; /* if HA_REC_IN_SEQ */
560
119
uint32_t rowid_field_offset; /* Field_nr +1 to rowid field */
564
* Currently the replication services component uses
565
* the primary_key member to determine which field is the table's
566
* primary key. However, as it exists, because this member is scalar, it
567
* only supports a single-column primary key. Is there a better way
568
* to ask for the fields which are in a primary key?
120
/* Index of auto-updated TIMESTAMP field in field array */
571
121
uint32_t primary_key;
574
uint32_t getPrimaryKey() const
579
bool hasPrimaryKey() const
581
return primary_key != MAX_KEY;
584
/* Index of auto-updated TIMESTAMP field in field array */
585
122
uint32_t next_number_index; /* autoincrement key number */
586
123
uint32_t next_number_key_offset; /* autoinc keypart offset in a key */
587
124
uint32_t next_number_keypart; /* autoinc keypart number in a key */
588
125
uint32_t error, open_errno, errarg; /* error from open_table_def() */
591
uint8_t blob_ptr_size; /* 4 or 8 */
594
uint8_t sizeBlobPtr() const
596
return blob_ptr_size;
126
uint32_t column_bitmap_size;
128
uint32_t vfields; /* Number of virtual fields */
129
bool null_field_first;
599
130
bool db_low_byte_first; /* Portable row format */
602
Set of keys in use, implemented as a Bitmap.
603
Excludes keys disabled by ALTER Table ... DISABLE KEYS.
606
key_map keys_for_keyread;
609
event_observers is a class containing all the event plugins that have
610
registered an interest in this table.
612
virtual plugin::EventObserverList *getTableObservers()
617
virtual void setTableObservers(plugin::EventObserverList *)
621
Set share's identifier information.
629
void setIdentifier(const identifier::Table &identifier_arg);
632
Initialize share for temporary tables
637
key Table_cache_key, as generated from create_table_def_key.
638
must start with db name.
639
key_length Length of key
640
table_name Table name
641
path Path to table (possible in lower case)
648
void init(const char *new_table_name,
649
const char *new_path);
652
void open_table_error(int pass_error, int db_errno, int pass_errarg);
656
static TableShare::shared_ptr getShareCreate(Session *session,
657
const identifier::Table &identifier,
660
friend std::ostream& operator<<(std::ostream& output, const TableShare &share)
662
output << "TableShare:(";
663
output << share.getSchemaName();
665
output << share.getTableName();
667
output << share.getTableTypeAsString();
669
output << share.getPath();
672
return output; // for multiple << operators.
676
friend class drizzled::table::Singular;
678
Field *make_field(const message::Table::Field &pfield,
680
uint32_t field_length,
682
unsigned char *null_pos,
683
unsigned char null_bit,
685
enum_field_types field_type,
686
const charset_info_st * field_charset,
687
Field::utype unireg_check,
689
const char *field_name);
691
Field *make_field(const message::Table::Field &pfield,
693
uint32_t field_length,
695
unsigned char *null_pos,
696
unsigned char null_bit,
698
enum_field_types field_type,
699
const charset_info_st * field_charset,
700
Field::utype unireg_check,
702
const char *field_name,
706
int open_table_def(Session& session, const identifier::Table &identifier);
708
int open_table_from_share(Session *session,
709
const identifier::Table &identifier,
711
uint32_t db_stat, uint32_t ha_open_flags,
714
int open_table_from_share_inner(Session *session,
718
int open_table_cursor_inner(const identifier::Table &identifier,
719
uint32_t db_stat, uint32_t ha_open_flags,
721
bool &error_reported);
723
bool parse_table_proto(Session& session, message::Table &table);
132
bool name_lock, replace_with_name_lock;
133
bool waiting_on_cond; /* Protection against free */
134
uint32_t table_map_id; /* for row-based replication */
135
uint64_t table_map_version;
138
Cache for row-based replication table share checks that does not
139
need to be repeated. Possible values are: -1 when cache value is
140
not calculated yet, 0 when table *shall not* be replicated, 1 when
141
table *may* be replicated.
143
int cached_row_logging_check;
146
Set share's table cache key and update its db and table name appropriately.
149
set_table_cache_key()
150
key_buff Buffer with already built table cache key to be
151
referenced from share.
152
key_length Key length.
155
Since 'key_buff' buffer will be referenced from share it should has same
156
life-time as share itself.
157
This method automatically ensures that TABLE_SHARE::table_name/db have
158
appropriate values by using table cache key as their source.
161
void set_table_cache_key(char *key_buff, uint32_t key_length)
163
table_cache_key.str= key_buff;
164
table_cache_key.length= key_length;
166
Let us use the fact that the key is "db/0/table_name/0" + optional
167
part for temporary tables.
169
db.str= table_cache_key.str;
170
db.length= strlen(db.str);
171
table_name.str= db.str + db.length + 1;
172
table_name.length= strlen(table_name.str);
177
Set share's table cache key and update its db and table name appropriately.
180
set_table_cache_key()
181
key_buff Buffer to be used as storage for table cache key
182
(should be at least key_length bytes).
183
key Value for table cache key.
184
key_length Key length.
187
Since 'key_buff' buffer will be used as storage for table cache key
188
it should has same life-time as share itself.
191
void set_table_cache_key(char *key_buff, const char *key, uint32_t key_length)
193
memcpy(key_buff, key, key_length);
194
set_table_cache_key(key_buff, key_length);
197
inline bool honor_global_locks()
199
return (table_category == TABLE_CATEGORY_USER);
202
inline uint32_t get_table_def_version()
726
} /* namespace drizzled */
728
#endif /* DRIZZLED_TABLE_INSTANCE_BASE_H */