31
#include <boost/unordered_map.hpp>
32
#include <boost/thread/condition_variable.hpp>
31
34
#include "drizzled/typelib.h"
32
#include "drizzled/my_hash.h"
33
35
#include "drizzled/memory/root.h"
34
36
#include "drizzled/message/table.pb.h"
37
#include "drizzled/util/string.h"
42
typedef boost::unordered_map< TableIdentifier::Key, TableShare *> TableDefinitionCache;
44
const static std::string STANDARD_STRING("STANDARD");
45
const static std::string TEMPORARY_STRING("TEMPORARY");
46
const static std::string INTERNAL_STRING("INTERNAL");
47
const static std::string FUNCTION_STRING("FUNCTION");
51
class EventObserverList;
58
typedef std::vector<std::string> StringVector;
43
table_category(TABLE_UNKNOWN_CATEGORY),
46
found_next_number_field(NULL),
47
timestamp_field(NULL),
57
row_type(ROW_TYPE_DEFAULT),
61
tmp_table(NO_TMP_TABLE),
75
timestamp_field_offset(0),
80
rowid_field_offset(0),
83
next_number_key_offset(0),
84
next_number_keypart(0),
88
column_bitmap_size(0),
90
db_low_byte_first(false),
92
replace_with_name_lock(false),
93
waiting_on_cond(false),
100
TableShare(const char *key,
102
const char *new_table_name,
103
const char *new_path) :
104
table_category(TABLE_UNKNOWN_CATEGORY),
107
found_next_number_field(NULL),
108
timestamp_field(NULL),
112
default_values(NULL),
117
stored_rec_length(0),
118
row_type(ROW_TYPE_DEFAULT),
121
storage_engine(NULL),
122
tmp_table(NO_TMP_TABLE),
125
last_null_bit_pos(0),
131
max_unique_length(0),
136
timestamp_field_offset(0),
138
db_create_options(0),
139
db_options_in_use(0),
141
rowid_field_offset(0),
143
next_number_index(0),
144
next_number_key_offset(0),
145
next_number_keypart(0),
149
column_bitmap_size(0),
151
db_low_byte_first(false),
153
replace_with_name_lock(false),
154
waiting_on_cond(false),
158
init(key, key_length, new_table_name, new_path);
60
TableShare(TableIdentifier::Type type_arg);
62
TableShare(TableIdentifier &identifier, const TableIdentifier::Key &key); // Used by placeholder
64
TableShare(const TableIdentifier &identifier); // Just used during createTable()
66
TableShare(TableIdentifier::Type type_arg,
67
TableIdentifier &identifier,
68
char *path_arg= NULL, uint32_t path_length_arg= 0); // Shares for cache
161
73
/** Category of this table. */
162
74
enum_table_category table_category;
164
76
uint32_t open_count; /* Number of tables in open list */
79
bool isTemporaryCategory() const
81
return (table_category == TABLE_CATEGORY_TEMPORARY);
84
void setTableCategory(enum_table_category arg)
166
89
/* The following is copied to each Table on OPEN */
90
typedef std::vector<Field *> Fields;
94
const Fields getFields() const
99
Field ** getFields(bool)
104
void setFields(uint32_t arg)
109
uint32_t positionFields(Field **arg) const
111
return (arg - (Field **)&field[0]);
114
void pushField(Field *arg)
117
field.push_back(arg);
168
121
Field **found_next_number_field;
169
123
Field *timestamp_field; /* Used only during open */
170
KEY *key_info; /* data of keys in database */
171
uint *blob_field; /* Index to blobs in Field arrray*/
126
Field *getTimestampField() const /* Used only during open */
128
return timestamp_field;
131
void setTimestampField(Field *arg) /* Used only during open */
133
timestamp_field= arg;
138
KeyInfo *key_info; /* data of keys in database */
140
KeyInfo &getKeyInfo(uint32_t arg) const
142
return key_info[arg];
144
std::vector<uint> blob_field; /* Index to blobs in Field arrray*/
173
146
/* hash of field names (contains pointers to elements of field array) */
174
HASH name_hash; /* hash of field names */
148
typedef boost::unordered_map < std::string, Field **, util::insensitive_hash, util::insensitive_equal_to> FieldMap;
149
typedef std::pair< std::string, Field ** > FieldMapPair;
150
FieldMap name_hash; /* hash of field names */
152
size_t getNamedFieldSize() const
154
return name_hash.size();
157
Field **getNamedField(const std::string &arg)
159
FieldMap::iterator iter= name_hash.find(arg);
161
if (iter == name_hash.end())
164
return (*iter).second;
175
168
memory::Root mem_root;
176
TYPELIB keynames; /* Pointers to keynames */
177
TYPELIB fieldnames; /* Pointer to fieldnames */
178
TYPELIB *intervals; /* pointer to interval info */
179
pthread_mutex_t mutex; /* For locking the share */
180
pthread_cond_t cond; /* To signal that share is ready */
182
unsigned char *default_values; /* row with default values */
170
void *alloc_root(size_t arg)
172
return mem_root.alloc_root(arg);
175
char *strmake_root(const char *str_arg, size_t len_arg)
177
return mem_root.strmake_root(str_arg, len_arg);
180
memory::Root *getMemRoot()
186
std::vector<std::string> _keynames;
188
void addKeyName(std::string arg)
190
std::transform(arg.begin(), arg.end(),
191
arg.begin(), ::toupper);
192
_keynames.push_back(arg);
195
bool doesKeyNameExist(const char *name_arg, uint32_t name_length, uint32_t &position) const
197
std::string arg(name_arg, name_length);
198
std::transform(arg.begin(), arg.end(),
199
arg.begin(), ::toupper);
201
std::vector<std::string>::const_iterator iter= std::find(_keynames.begin(), _keynames.end(), arg);
203
if (iter == _keynames.end())
206
position= iter - _keynames.begin();
211
bool doesKeyNameExist(std::string arg, uint32_t &position) const
213
std::transform(arg.begin(), arg.end(),
214
arg.begin(), ::toupper);
216
std::vector<std::string>::const_iterator iter= std::find(_keynames.begin(), _keynames.end(), arg);
218
if (iter == _keynames.end())
220
position= -1; //historical, required for finding primary key from unique
224
position= iter - _keynames.begin();
230
std::vector<TYPELIB> intervals; /* pointer to interval info */
232
boost::mutex mutex; /* For locking the share */
233
boost::condition_variable cond; /* To signal that share is ready */
248
std::vector<unsigned char> default_values; /* row with default values */
250
unsigned char * getDefaultValues()
252
return &default_values[0];
254
void resizeDefaultValues(size_t arg)
256
default_values.resize(arg);
183
259
const CHARSET_INFO *table_charset; /* Default charset of string fields */
185
261
MyBitmap all_set;
263
std::vector<my_bitmap_map> all_bitmap;
187
267
Key which is used for looking-up table in table cache and in the list
188
268
of thread's temporary tables. Has the form of:
193
273
should correspond to each other.
194
274
To ensure this one can use set_table_cache() methods.
196
LEX_STRING table_cache_key;
277
TableIdentifier::Key private_key_for_cache; // This will not exist in the final design.
278
std::vector<char> private_normalized_path; // This will not exist in the final design.
197
279
LEX_STRING db; /* Pointer to db */
198
280
LEX_STRING table_name; /* Table name (for open) */
199
281
LEX_STRING path; /* Path to table (from datadir) */
200
282
LEX_STRING normalized_path; /* unpack_filename(path) */
285
const char *getNormalizedPath() const
287
return normalized_path.str;
290
const char *getPath() const
295
const TableIdentifier::Key& getCacheKey() const // This should never be called when we aren't looking at a cache.
297
assert(private_key_for_cache.size());
298
return private_key_for_cache;
301
size_t getCacheKeySize() const
303
return private_key_for_cache.size();
306
void setPath(char *str_arg, uint32_t size_arg)
309
path.length= size_arg;
312
void setNormalizedPath(char *str_arg, uint32_t size_arg)
314
normalized_path.str= str_arg;
315
normalized_path.length= size_arg;
318
const char *getTableName() const
320
return table_name.str;
323
uint32_t getTableNameSize() const
325
return table_name.length;
328
const std::string &getTableName(std::string &name_arg) const
331
name_arg.append(table_name.str, table_name.length);
336
const char *getSchemaName() const
341
const std::string &getSchemaName(std::string &schema_name_arg) const
343
schema_name_arg.clear();
344
schema_name_arg.append(db.str, db.length);
346
return schema_name_arg;
202
349
uint32_t block_size; /* create information */
203
352
uint64_t version;
354
uint64_t getVersion() const
359
void refreshVersion()
361
version= refresh_version;
204
369
uint32_t timestamp_offset; /* Set to offset+1 of record */
205
371
uint32_t reclength; /* Recordlength */
206
373
uint32_t stored_rec_length; /* Stored record length*/
207
enum row_type row_type; /* How rows are stored */
375
uint32_t getRecordLength() const
380
void setRecordLength(uint32_t arg)
385
const Field_blob *getBlobFieldAt(uint32_t arg) const
387
if (arg < blob_fields)
388
return (Field_blob*) field[blob_field[arg]];
210
394
/* Max rows is a hint to HEAP during a create tmp table */
312
584
key_map keys_in_use;
313
585
key_map keys_for_keyread;
588
event_observers is a class containing all the event plugins that have
589
registered an interest in this table.
592
plugin::EventObserverList *event_observers;
594
plugin::EventObserverList *getTableObservers()
596
return event_observers;
599
void setTableObservers(plugin::EventObserverList *observers)
601
event_observers= observers;
316
Set share's table cache key and update its db and table name appropriately.
605
Set share's identifier information.
319
set_table_cache_key()
320
key_buff Buffer with already built table cache key to be
321
referenced from share.
322
key_length Key length.
325
Since 'key_buff' buffer will be referenced from share it should has same
326
life-time as share itself.
327
This method automatically ensures that TableShare::table_name/db have
328
appropriate values by using table cache key as their source.
331
void set_table_cache_key(char *key_buff, uint32_t key_length)
333
table_cache_key.str= key_buff;
334
table_cache_key.length= key_length;
336
Let us use the fact that the key is "db/0/table_name/0" + optional
337
part for temporary tables.
339
db.str= table_cache_key.str;
340
db.length= strlen(db.str);
341
table_name.str= db.str + db.length + 1;
342
table_name.length= strlen(table_name.str);
347
Set share's table cache key and update its db and table name appropriately.
350
set_table_cache_key()
351
key_buff Buffer to be used as storage for table cache key
352
(should be at least key_length bytes).
353
key Value for table cache key.
354
key_length Key length.
357
Since 'key_buff' buffer will be used as storage for table cache key
358
it should has same life-time as share itself.
361
void set_table_cache_key(char *key_buff, const char *key, uint32_t key_length)
363
memcpy(key_buff, key, key_length);
364
set_table_cache_key(key_buff, key_length);
613
void setIdentifier(TableIdentifier &identifier_arg);
367
615
inline bool honor_global_locks()
383
631
path Path to table (possible in lower case)
386
This is different from alloc_table_share() because temporary tables
387
don't have to be shared between threads or put into the table def
388
cache, so we can do some things notable simpler and faster
390
If table is not put in session->temporary_tables (happens only when
391
one uses OPEN TEMPORARY) then one can specify 'db' as key and
392
use key_length= 0 as neither table_cache_key or key_length will be used).
400
638
void init(const char *new_table_name,
401
const char *new_path)
403
init("", 0, new_table_name, new_path);
406
void init(const char *key,
407
uint32_t key_length, const char *new_table_name,
408
const char *new_path)
410
memset(this, 0, sizeof(TableShare));
411
memory::init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
412
table_category= TABLE_CATEGORY_TEMPORARY;
413
tmp_table= INTERNAL_TMP_TABLE;
415
db.length= strlen(key);
416
table_cache_key.str= (char*) key;
417
table_cache_key.length= key_length;
418
table_name.str= (char*) new_table_name;
419
table_name.length= strlen(new_table_name);
420
path.str= (char*) new_path;
421
normalized_path.str= (char*) new_path;
422
path.length= normalized_path.length= strlen(new_path);
428
Free table share and memory used by it
435
share->mutex must be locked when we come here if it's not a temp table
438
void free_table_share()
440
memory::Root new_mem_root;
441
assert(ref_count == 0);
444
If someone is waiting for this to be deleted, inform it about this.
445
Don't do a delete until we know that no one is refering to this anymore.
447
if (tmp_table == NO_TMP_TABLE)
449
/* share->mutex is locked in release_table_share() */
450
while (waiting_on_cond)
452
pthread_cond_broadcast(&cond);
453
pthread_cond_wait(&cond, &mutex);
455
/* No thread refers to this anymore */
456
pthread_mutex_unlock(&mutex);
457
pthread_mutex_destroy(&mutex);
458
pthread_cond_destroy(&cond);
460
hash_free(&name_hash);
462
storage_engine= NULL;
467
/* We must copy mem_root from share because share is allocated through it */
468
memcpy(&new_mem_root, &mem_root, sizeof(new_mem_root));
469
free_root(&new_mem_root, MYF(0)); // Free's share
639
const char *new_path);
472
642
void open_table_error(int pass_error, int db_errno, int pass_errarg);
477
Create a table cache key
481
key Create key here (must be of size MAX_DBKEY_LENGTH)
482
table_list Table definition
485
The table cache_key is created from:
489
if the table is a tmp table, we add the following to make each tmp table
492
4 bytes for master thread id
493
4 bytes pseudo thread id
499
static inline uint32_t createKey(char *key, std::string& db_arg,
500
std::string& table_name_arg)
502
return createKey(key, db_arg.c_str(), table_name_arg.c_str());
505
static inline uint32_t createKey(char *key, const char *db_arg, const char *table_name_arg)
510
key_pos= strcpy(key_pos, db_arg) + strlen(db_arg);
511
key_pos= strcpy(key_pos+1, table_name_arg) +
512
strlen(table_name_arg);
513
key_length= (uint32_t)(key_pos-key)+1;
518
644
static void cacheStart(void);
519
static void cacheStop(void);
520
645
static void release(TableShare *share);
521
static void release(const char *key, uint32_t key_length);
522
static TableShare *getShare(const char *db, const char *table_name);
523
static TableShare *getShare(Session *session,
524
TableList *table_list, char *key,
525
uint32_t key_length, uint32_t, int *error);
646
static void release(TableIdentifier &identifier);
647
static const TableDefinitionCache &getCache();
648
static TableShare *getShare(TableIdentifier &identifier);
649
static TableShare *getShareCreate(Session *session,
650
TableIdentifier &identifier,
653
friend std::ostream& operator<<(std::ostream& output, const TableShare &share)
655
output << "TableShare:(";
656
output << share.getSchemaName();
658
output << share.getTableName();
660
output << share.getTableTypeAsString();
662
output << share.getPath();
665
return output; // for multiple << operators.
668
Field *make_field(unsigned char *ptr,
669
uint32_t field_length,
671
unsigned char *null_pos,
672
unsigned char null_bit,
674
enum_field_types field_type,
675
const CHARSET_INFO * field_charset,
676
Field::utype unireg_check,
678
const char *field_name);
680
int open_table_def(Session& session, TableIdentifier &identifier);
682
int open_table_from_share(Session *session,
683
const TableIdentifier &identifier,
685
uint32_t db_stat, uint32_t ha_open_flags,
687
int parse_table_proto(Session& session, message::Table &table);
689
int inner_parse_table_proto(Session& session, message::Table &table);
528
692
} /* namespace drizzled */