~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table_share.h

  • Committer: Stewart Smith
  • Date: 2010-02-15 03:55:09 UTC
  • mto: (1273.13.96 build)
  • mto: This revision was merged to the branch mainline in revision 1308.
  • Revision ID: stewart@flamingspork.com-20100215035509-y6sry4q4yymph2by
move SUBSTR, SUBSTRING and SUBSTR_INDEX to plugins. add parser hooks for substr being a plugin now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
  instance of table share per one table in the database.
24
24
*/
25
25
 
26
 
#ifndef DRIZZLED_DEFINITION_TABLE_H
27
 
#define DRIZZLED_DEFINITION_TABLE_H
 
26
#ifndef DRIZZLED_TABLE_SHARE_H
 
27
#define DRIZZLED_TABLE_SHARE_H
28
28
 
29
29
#include <string>
30
30
 
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
 
 
36
31
#include "drizzled/typelib.h"
 
32
#include "drizzled/my_hash.h"
37
33
#include "drizzled/memory/root.h"
38
34
#include "drizzled/message/table.pb.h"
39
 
#include "drizzled/util/string.h"
40
 
 
41
 
#include "drizzled/table/cache.h"
42
35
 
43
36
namespace drizzled
44
37
{
45
38
 
46
 
extern uint64_t refresh_version;
47
 
 
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");
53
 
 
54
 
namespace plugin
55
 
{
56
 
class EventObserverList;
57
 
}
58
 
 
59
 
class Field_blob;
60
 
 
61
39
class TableShare
62
40
{
63
 
  typedef std::vector<std::string> StringVector;
64
 
 
65
41
public:
66
 
  typedef boost::shared_ptr<TableShare> shared_ptr;
67
 
  typedef std::vector <shared_ptr> vector;
68
 
 
69
 
  TableShare(const TableIdentifier::Type type_arg);
70
 
 
71
 
  TableShare(const TableIdentifier &identifier, const TableIdentifier::Key &key); // Used by placeholder
72
 
 
73
 
  TableShare(const TableIdentifier &identifier); // Just used during createTable()
74
 
 
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
78
 
 
79
 
  ~TableShare();
80
 
 
81
 
private:
 
42
  TableShare() :
 
43
    table_category(TABLE_UNKNOWN_CATEGORY),
 
44
    open_count(0),
 
45
    field(NULL),
 
46
    found_next_number_field(NULL),
 
47
    timestamp_field(NULL),
 
48
    key_info(NULL),
 
49
    blob_field(NULL),
 
50
    intervals(NULL),
 
51
    default_values(NULL),
 
52
    block_size(0),
 
53
    version(0),
 
54
    timestamp_offset(0),
 
55
    reclength(0),
 
56
    stored_rec_length(0),
 
57
    row_type(ROW_TYPE_DEFAULT),
 
58
    max_rows(0),
 
59
    table_proto(NULL),
 
60
    storage_engine(NULL),
 
61
    tmp_table(NO_TMP_TABLE),
 
62
    ref_count(0),
 
63
    null_bytes(0),
 
64
    last_null_bit_pos(0),
 
65
    fields(0),
 
66
    rec_buff_length(0),
 
67
    keys(0),
 
68
    key_parts(0),
 
69
    max_key_length(0),
 
70
    max_unique_length(0),
 
71
    total_key_length(0),
 
72
    uniques(0),
 
73
    null_fields(0),
 
74
    blob_fields(0),
 
75
    timestamp_field_offset(0),
 
76
    varchar_fields(0),
 
77
    db_create_options(0),
 
78
    db_options_in_use(0),
 
79
    db_record_offset(0),
 
80
    rowid_field_offset(0),
 
81
    primary_key(0),
 
82
    next_number_index(0),
 
83
    next_number_key_offset(0),
 
84
    next_number_keypart(0),
 
85
    error(0),
 
86
    open_errno(0),
 
87
    errarg(0),
 
88
    column_bitmap_size(0),
 
89
    blob_ptr_size(0),
 
90
    db_low_byte_first(false),
 
91
    name_lock(false),
 
92
    replace_with_name_lock(false),
 
93
    waiting_on_cond(false),
 
94
    keys_in_use(0),
 
95
    keys_for_keyread(0)
 
96
  {
 
97
    init();
 
98
  }
 
99
 
 
100
  TableShare(const char *key,
 
101
             uint32_t key_length,
 
102
             const char *new_table_name,
 
103
             const char *new_path) :
 
104
    table_category(TABLE_UNKNOWN_CATEGORY),
 
105
    open_count(0),
 
106
    field(NULL),
 
107
    found_next_number_field(NULL),
 
108
    timestamp_field(NULL),
 
109
    key_info(NULL),
 
110
    blob_field(NULL),
 
111
    intervals(NULL),
 
112
    default_values(NULL),
 
113
    block_size(0),
 
114
    version(0),
 
115
    timestamp_offset(0),
 
116
    reclength(0),
 
117
    stored_rec_length(0),
 
118
    row_type(ROW_TYPE_DEFAULT),
 
119
    max_rows(0),
 
120
    table_proto(NULL),
 
121
    storage_engine(NULL),
 
122
    tmp_table(NO_TMP_TABLE),
 
123
    ref_count(0),
 
124
    null_bytes(0),
 
125
    last_null_bit_pos(0),
 
126
    fields(0),
 
127
    rec_buff_length(0),
 
128
    keys(0),
 
129
    key_parts(0),
 
130
    max_key_length(0),
 
131
    max_unique_length(0),
 
132
    total_key_length(0),
 
133
    uniques(0),
 
134
    null_fields(0),
 
135
    blob_fields(0),
 
136
    timestamp_field_offset(0),
 
137
    varchar_fields(0),
 
138
    db_create_options(0),
 
139
    db_options_in_use(0),
 
140
    db_record_offset(0),
 
141
    rowid_field_offset(0),
 
142
    primary_key(0),
 
143
    next_number_index(0),
 
144
    next_number_key_offset(0),
 
145
    next_number_keypart(0),
 
146
    error(0),
 
147
    open_errno(0),
 
148
    errarg(0),
 
149
    column_bitmap_size(0),
 
150
    blob_ptr_size(0),
 
151
    db_low_byte_first(false),
 
152
    name_lock(false),
 
153
    replace_with_name_lock(false),
 
154
    waiting_on_cond(false),
 
155
    keys_in_use(0),
 
156
    keys_for_keyread(0)
 
157
  {
 
158
    init(key, key_length, new_table_name, new_path);
 
159
  }
 
160
 
82
161
  /** Category of this table. */
83
162
  enum_table_category table_category;
84
163
 
85
 
public:
86
 
 
87
 
  bool isTemporaryCategory() const
88
 
  {
89
 
    return (table_category == TABLE_CATEGORY_TEMPORARY);
90
 
  }
91
 
 
92
 
  void setTableCategory(enum_table_category arg)
93
 
  {
94
 
    table_category= arg;
95
 
  }
 
164
  uint32_t open_count;                  /* Number of tables in open list */
96
165
 
97
166
  /* The following is copied to each Table on OPEN */
98
 
  typedef std::vector<Field *> Fields;
99
 
private:
100
 
  Fields field;
101
 
public:
102
 
  const Fields getFields() const
103
 
  {
104
 
    return field;
105
 
  }
106
 
 
107
 
  Field ** getFields(bool)
108
 
  {
109
 
    return &field[0];
110
 
  }
111
 
 
112
 
  void setFields(uint32_t arg)
113
 
  {
114
 
    field.resize(arg);
115
 
  }
116
 
 
117
 
  uint32_t positionFields(Field **arg) const
118
 
  {
119
 
    return (arg - (Field **)&field[0]);
120
 
  }
121
 
 
122
 
  void pushField(Field *arg)
123
 
  {
124
 
    fields++;
125
 
    field.push_back(arg);
126
 
  }
127
 
 
128
 
 
 
167
  Field **field;
129
168
  Field **found_next_number_field;
130
 
private:
131
169
  Field *timestamp_field;               /* Used only during open */
132
 
public:
133
 
 
134
 
  Field *getTimestampField() const               /* Used only during open */
135
 
  {
136
 
    return timestamp_field;
137
 
  }
138
 
 
139
 
  void setTimestampField(Field *arg) /* Used only during open */
140
 
  {
141
 
    timestamp_field= arg;
142
 
  }
143
 
 
144
 
 
145
 
private:
146
 
  KeyInfo  *key_info;                   /* data of keys in database */
147
 
public:
148
 
  KeyInfo &getKeyInfo(uint32_t arg) const
149
 
  {
150
 
    return key_info[arg];
151
 
  }
152
 
  std::vector<uint>     blob_field;                     /* Index to blobs in Field arrray*/
 
170
  KEY  *key_info;                       /* data of keys in database */
 
171
  uint  *blob_field;                    /* Index to blobs in Field arrray*/
153
172
 
154
173
  /* hash of field names (contains pointers to elements of field array) */
155
 
private:
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 */
159
 
public:
160
 
  size_t getNamedFieldSize() const
161
 
  {
162
 
    return name_hash.size();
163
 
  }
164
 
 
165
 
  Field **getNamedField(const std::string &arg)
166
 
  {
167
 
    FieldMap::iterator iter= name_hash.find(arg);
168
 
 
169
 
    if (iter == name_hash.end())
170
 
        return 0;
171
 
 
172
 
    return (*iter).second;
173
 
  }
174
 
 
175
 
private:
 
174
  HASH  name_hash;                      /* hash of field names */
176
175
  memory::Root mem_root;
177
 
 
178
 
  void *alloc_root(size_t arg)
179
 
  {
180
 
    return mem_root.alloc_root(arg);
181
 
  }
182
 
 
183
 
  char *strmake_root(const char *str_arg, size_t len_arg)
184
 
  {
185
 
    return mem_root.strmake_root(str_arg, len_arg);
186
 
  }
187
 
 
188
 
  memory::Root *getMemRoot()
189
 
  {
190
 
    return &mem_root;
191
 
  }
192
 
 
193
 
  std::vector<std::string> _keynames;
194
 
 
195
 
  void addKeyName(std::string arg)
196
 
  {
197
 
    std::transform(arg.begin(), arg.end(),
198
 
                   arg.begin(), ::toupper);
199
 
    _keynames.push_back(arg);
200
 
  }
201
 
public:
202
 
  bool doesKeyNameExist(const char *name_arg, uint32_t name_length, uint32_t &position) const
203
 
  {
204
 
    return doesKeyNameExist(std::string(name_arg, name_length), position);
205
 
  }
206
 
 
207
 
  bool doesKeyNameExist(std::string arg, uint32_t &position) const
208
 
  {
209
 
    std::transform(arg.begin(), arg.end(),
210
 
                   arg.begin(), ::toupper);
211
 
 
212
 
    std::vector<std::string>::const_iterator iter= std::find(_keynames.begin(), _keynames.end(), arg);
213
 
 
214
 
    if (iter == _keynames.end())
215
 
    {
216
 
      position= UINT32_MAX; //historical, required for finding primary key from unique
217
 
      return false;
218
 
    }
219
 
 
220
 
    position= iter -  _keynames.begin();
221
 
 
222
 
    return true;
223
 
  }
224
 
 
225
 
private:
226
 
  std::vector<TYPELIB> intervals;                       /* pointer to interval info */
227
 
 
228
 
  boost::mutex mutex;                /* For locking the share  */
229
 
  boost::condition_variable cond;                       /* To signal that share is ready */
230
 
 
231
 
 
232
 
  void lock()
233
 
  {
234
 
    mutex.lock();
235
 
  }
236
 
 
237
 
  void unlock()
238
 
  {
239
 
    mutex.unlock();
240
 
  }
241
 
 
242
 
  std::vector<unsigned char> default_values;            /* row with default values */
243
 
public:
244
 
  // @note This needs to be made to be const in the future
245
 
  unsigned char *getDefaultValues()
246
 
  {
247
 
    return &default_values[0];
248
 
  }
249
 
  void resizeDefaultValues(size_t arg)
250
 
  {
251
 
    default_values.resize(arg);
252
 
  }
253
 
 
 
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 */
 
181
 
 
182
  unsigned char *default_values;                /* row with default values */
254
183
  const CHARSET_INFO *table_charset; /* Default charset of string fields */
255
184
 
256
 
  boost::dynamic_bitset<> all_set;
257
 
 
 
185
  MyBitmap all_set;
258
186
  /*
259
187
    Key which is used for looking-up table in table cache and in the list
260
188
    of thread's temporary tables. Has the form of:
265
193
    should correspond to each other.
266
194
    To ensure this one can use set_table_cache() methods.
267
195
  */
268
 
private:
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.
 
196
  LEX_STRING table_cache_key;
271
197
  LEX_STRING db;                        /* Pointer to db */
272
198
  LEX_STRING table_name;                /* Table name (for open) */
273
199
  LEX_STRING path;      /* Path to table (from datadir) */
274
200
  LEX_STRING normalized_path;           /* unpack_filename(path) */
275
 
public:
276
 
 
277
 
  const char *getNormalizedPath() const
278
 
  {
279
 
    return normalized_path.str;
280
 
  }
281
 
 
282
 
  const char *getPath() const
283
 
  {
284
 
    return path.str;
285
 
  }
286
 
 
287
 
  const TableIdentifier::Key& getCacheKey() const // This should never be called when we aren't looking at a cache.
288
 
  {
289
 
    assert(private_key_for_cache.size());
290
 
    return private_key_for_cache;
291
 
  }
292
 
 
293
 
  size_t getCacheKeySize() const
294
 
  {
295
 
    return private_key_for_cache.size();
296
 
  }
297
 
 
298
 
private:
299
 
  void setPath(char *str_arg, uint32_t size_arg)
300
 
  {
301
 
    path.str= str_arg;
302
 
    path.length= size_arg;
303
 
  }
304
 
 
305
 
  void setNormalizedPath(char *str_arg, uint32_t size_arg)
306
 
  {
307
 
    normalized_path.str= str_arg;
308
 
    normalized_path.length= size_arg;
309
 
  }
310
 
public:
311
 
 
312
 
  const char *getTableName() const
313
 
  {
314
 
    return table_name.str;
315
 
  }
316
 
 
317
 
  uint32_t getTableNameSize() const
318
 
  {
319
 
    return table_name.length;
320
 
  }
321
 
 
322
 
  const std::string &getTableName(std::string &name_arg) const
323
 
  {
324
 
    name_arg.clear();
325
 
    name_arg.append(table_name.str, table_name.length);
326
 
 
327
 
    return name_arg;
328
 
  }
329
 
 
330
 
  const char *getSchemaName() const
331
 
  {
332
 
    return db.str;
333
 
  }
334
 
 
335
 
  const std::string &getSchemaName(std::string &schema_name_arg) const
336
 
  {
337
 
    schema_name_arg.clear();
338
 
    schema_name_arg.append(db.str, db.length);
339
 
 
340
 
    return schema_name_arg;
341
 
  }
342
201
 
343
202
  uint32_t   block_size;                   /* create information */
344
 
 
345
 
private:
346
203
  uint64_t   version;
347
 
public:
348
 
  uint64_t getVersion() const
349
 
  {
350
 
    return version;
351
 
  }
352
 
 
353
 
  void refreshVersion()
354
 
  {
355
 
   version= refresh_version;
356
 
  }
357
 
 
358
 
  void resetVersion()
359
 
  {
360
 
    version= 0;
361
 
  }
362
 
 
363
204
  uint32_t   timestamp_offset;          /* Set to offset+1 of record */
364
 
private:
365
205
  uint32_t   reclength;                 /* Recordlength */
366
 
public:
367
206
  uint32_t   stored_rec_length;         /* Stored record length*/
368
 
 
369
 
  uint32_t getRecordLength() const
370
 
  {
371
 
    return reclength;
372
 
  }
373
 
 
374
 
  void setRecordLength(uint32_t arg)
375
 
  {
376
 
    reclength= arg;
377
 
  }
378
 
 
379
 
  const Field_blob *getBlobFieldAt(uint32_t arg) const
380
 
  {
381
 
    if (arg < blob_fields)
382
 
      return (Field_blob*) field[blob_field[arg]];
383
 
 
384
 
    return NULL;
385
 
  }
 
207
  enum row_type row_type;               /* How rows are stored */
386
208
 
387
209
private:
388
210
  /* Max rows is a hint to HEAP during a create tmp table */
391
213
  message::Table *table_proto;
392
214
public:
393
215
 
394
 
  /*
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.
397
 
  */
398
 
  message::Table::TableType getTableType() const
399
 
  {
400
 
    return table_proto ? table_proto->type() : message::Table::STANDARD;
401
 
  }
402
 
 
403
 
  const std::string &getTableTypeAsString() const
404
 
  {
405
 
    if (table_proto)
406
 
    {
407
 
      switch (table_proto->type())
408
 
      {
409
 
      default:
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;
418
 
      }
419
 
    }
420
 
    else
421
 
    {
422
 
      return NO_PROTOBUFFER_AVAILABLE;
423
 
    }
424
 
  }
425
 
 
426
216
  /* This is only used in one location currently */
427
 
  inline message::Table *getTableProto() const
428
 
  {
429
 
    return table_proto;
430
 
  }
431
 
 
432
217
  inline void setTableProto(message::Table *arg)
433
218
  {
434
219
    assert(table_proto == NULL);
435
220
    table_proto= arg;
436
221
  }
437
222
 
438
 
  inline bool hasComment() const
 
223
  inline bool hasComment()
439
224
  {
440
225
    return (table_proto) ?  table_proto->options().has_comment() : false; 
441
226
  }
445
230
    return (table_proto && table_proto->has_options()) ?  table_proto->options().comment().c_str() : NULL; 
446
231
  }
447
232
 
448
 
  inline uint32_t getCommentLength() const
 
233
  inline uint32_t getCommentLength()
449
234
  {
450
235
    return (table_proto) ? table_proto->options().comment().length() : 0; 
451
236
  }
452
237
 
453
 
  inline uint64_t getMaxRows() const
 
238
  inline bool hasKeyBlockSize()
 
239
  {
 
240
    return (table_proto) ? table_proto->options().has_key_block_size() : false;
 
241
  }
 
242
 
 
243
  inline uint32_t getKeyBlockSize()
 
244
  {
 
245
    return (table_proto) ? table_proto->options().key_block_size() : 0;
 
246
  }
 
247
 
 
248
  inline uint64_t getMaxRows()
454
249
  {
455
250
    return max_rows;
456
251
  }
460
255
    max_rows= arg;
461
256
  }
462
257
 
463
 
  /**
464
 
   * Returns true if the supplied Field object
465
 
   * is part of the table's primary key.
466
 
 */
467
 
  bool fieldInPrimaryKey(Field *field) const;
468
 
 
469
258
  plugin::StorageEngine *storage_engine;                        /* storage engine plugin */
470
259
  inline plugin::StorageEngine *db_type() const /* table_type for handler */
471
260
  {
475
264
  {
476
265
    return storage_engine;
477
266
  }
478
 
 
479
 
private:
480
 
  TableIdentifier::Type tmp_table;
481
 
public:
482
 
 
483
 
  TableIdentifier::Type getType() const
484
 
  {
485
 
    return tmp_table;
486
 
  }
487
 
 
488
 
private:
 
267
  enum tmp_table_type tmp_table;
 
268
 
489
269
  uint32_t ref_count;       /* How many Table objects uses this */
490
 
 
491
 
public:
492
 
  uint32_t getTableCount() const
493
 
  {
494
 
    return ref_count;
495
 
  }
496
 
 
497
 
  void incrementTableCount()
498
 
  {
499
 
    lock();
500
 
    ref_count++;
501
 
    unlock();
502
 
  }
503
 
 
504
270
  uint32_t null_bytes;
505
271
  uint32_t last_null_bit_pos;
506
272
  uint32_t fields;                              /* Number of fields */
507
 
 
508
 
  uint32_t sizeFields() const
509
 
  {
510
 
    return fields;
511
 
  }
512
 
 
513
273
  uint32_t rec_buff_length;                 /* Size of table->record[] buffer */
514
 
  uint32_t keys;
515
 
 
516
 
  uint32_t sizeKeys() const
517
 
  {
518
 
    return keys;
519
 
  }
520
 
  uint32_t key_parts;
 
274
  uint32_t keys, key_parts;
521
275
  uint32_t max_key_length, max_unique_length, total_key_length;
522
276
  uint32_t uniques;                         /* Number of UNIQUE index */
523
277
  uint32_t null_fields;                 /* number of null fields */
524
278
  uint32_t blob_fields;                 /* number of blob fields */
525
 
private:
526
 
  bool has_variable_width;                  /* number of varchar fields */
527
 
public:
528
 
  bool hasVariableWidth() const
529
 
  {
530
 
    return has_variable_width; // We should calculate this.
531
 
  }
532
 
  void setVariableWidth()
533
 
  {
534
 
    has_variable_width= true;
535
 
  }
 
279
  uint32_t timestamp_field_offset;              /* Field number for timestamp field */
 
280
  uint32_t varchar_fields;                  /* number of varchar fields */
536
281
  uint32_t db_create_options;           /* Create options from database */
537
282
  uint32_t db_options_in_use;           /* Options in use */
538
283
  uint32_t db_record_offset;            /* if HA_REC_IN_SEQ */
545
290
   * primary key.  However, as it exists, because this member is scalar, it
546
291
   * only supports a single-column primary key. Is there a better way
547
292
   * to ask for the fields which are in a primary key?
548
 
 */
549
 
private:
 
293
   */
550
294
  uint32_t primary_key;
551
 
public:
552
 
 
553
 
  uint32_t getPrimaryKey() const
554
 
  {
555
 
    return primary_key;
556
 
  }
557
 
 
558
 
  bool hasPrimaryKey() const
559
 
  {
560
 
    return primary_key != MAX_KEY;
561
 
  }
562
 
 
563
295
  /* Index of auto-updated TIMESTAMP field in field array */
564
296
  uint32_t next_number_index;               /* autoincrement key number */
565
297
  uint32_t next_number_key_offset;          /* autoinc keypart offset in a key */
566
298
  uint32_t next_number_keypart;             /* autoinc keypart number in a key */
567
299
  uint32_t error, open_errno, errarg;       /* error from open_table_def() */
 
300
  uint32_t column_bitmap_size;
568
301
 
569
302
  uint8_t blob_ptr_size;                        /* 4 or 8 */
570
303
  bool db_low_byte_first;               /* Portable row format */
 
304
  bool name_lock;
 
305
  bool replace_with_name_lock;
 
306
  bool waiting_on_cond;                 /* Protection against free */
571
307
 
572
308
  /*
573
309
    Set of keys in use, implemented as a Bitmap.
576
312
  key_map keys_in_use;
577
313
  key_map keys_for_keyread;
578
314
 
579
 
  /* 
580
 
    event_observers is a class containing all the event plugins that have 
581
 
    registered an interest in this table.
582
 
  */
583
 
  private:
584
 
  plugin::EventObserverList *event_observers;
585
 
  public:
586
 
  plugin::EventObserverList *getTableObservers() 
587
 
  { 
588
 
    return event_observers;
589
 
  }
590
 
  
591
 
  void setTableObservers(plugin::EventObserverList *observers) 
592
 
  { 
593
 
    event_observers= observers;
594
 
  }
595
 
  
596
315
  /*
597
 
    Set share's identifier information.
 
316
    Set share's table cache key and update its db and table name appropriately.
598
317
 
599
318
    SYNOPSIS
600
 
    setIdentifier()
 
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.
601
323
 
602
324
    NOTES
603
 
  */
604
 
 
605
 
  void setIdentifier(const TableIdentifier &identifier_arg);
 
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.
 
329
  */
 
330
 
 
331
  void set_table_cache_key(char *key_buff, uint32_t key_length)
 
332
  {
 
333
    table_cache_key.str= key_buff;
 
334
    table_cache_key.length= key_length;
 
335
    /*
 
336
      Let us use the fact that the key is "db/0/table_name/0" + optional
 
337
      part for temporary tables.
 
338
    */
 
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);
 
343
  }
 
344
 
 
345
 
 
346
  /*
 
347
    Set share's table cache key and update its db and table name appropriately.
 
348
 
 
349
    SYNOPSIS
 
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.
 
355
 
 
356
    NOTE
 
357
    Since 'key_buff' buffer will be used as storage for table cache key
 
358
    it should has same life-time as share itself.
 
359
  */
 
360
 
 
361
  void set_table_cache_key(char *key_buff, const char *key, uint32_t key_length)
 
362
  {
 
363
    memcpy(key_buff, key, key_length);
 
364
    set_table_cache_key(key_buff, key_length);
 
365
  }
 
366
 
 
367
  inline bool honor_global_locks()
 
368
  {
 
369
    return (table_category == TABLE_CATEGORY_USER);
 
370
  }
 
371
 
606
372
 
607
373
  /*
608
374
    Initialize share for temporary tables
617
383
    path        Path to table (possible in lower case)
618
384
 
619
385
    NOTES
620
 
    
 
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
 
389
 
 
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).
621
393
  */
622
394
 
623
 
private:
 
395
  void init()
 
396
  {
 
397
    init("", 0, "", "");
 
398
  }
 
399
 
624
400
  void init(const char *new_table_name,
625
 
            const char *new_path);
626
 
public:
 
401
            const char *new_path)
 
402
  {
 
403
    init("", 0, new_table_name, new_path);
 
404
  }
 
405
 
 
406
  void init(const char *key,
 
407
            uint32_t key_length, const char *new_table_name,
 
408
            const char *new_path)
 
409
  {
 
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;
 
414
    db.str=                 (char*) key;
 
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);
 
423
 
 
424
    return;
 
425
  }
 
426
 
 
427
  /*
 
428
    Free table share and memory used by it
 
429
 
 
430
    SYNOPSIS
 
431
    free_table_share()
 
432
    share               Table share
 
433
 
 
434
    NOTES
 
435
    share->mutex must be locked when we come here if it's not a temp table
 
436
  */
 
437
 
 
438
  void free_table_share()
 
439
  {
 
440
    memory::Root new_mem_root;
 
441
    assert(ref_count == 0);
 
442
 
 
443
    /*
 
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.
 
446
    */
 
447
    if (tmp_table == NO_TMP_TABLE)
 
448
    {
 
449
      /* share->mutex is locked in release_table_share() */
 
450
      while (waiting_on_cond)
 
451
      {
 
452
        pthread_cond_broadcast(&cond);
 
453
        pthread_cond_wait(&cond, &mutex);
 
454
      }
 
455
      /* No thread refers to this anymore */
 
456
      pthread_mutex_unlock(&mutex);
 
457
      pthread_mutex_destroy(&mutex);
 
458
      pthread_cond_destroy(&cond);
 
459
    }
 
460
    hash_free(&name_hash);
 
461
 
 
462
    storage_engine= NULL;
 
463
 
 
464
    delete table_proto;
 
465
    table_proto= NULL;
 
466
 
 
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
 
470
  }
627
471
 
628
472
  void open_table_error(int pass_error, int db_errno, int pass_errarg);
629
473
 
 
474
 
 
475
 
 
476
  /*
 
477
    Create a table cache key
 
478
 
 
479
    SYNOPSIS
 
480
    createKey()
 
481
    key                 Create key here (must be of size MAX_DBKEY_LENGTH)
 
482
    table_list          Table definition
 
483
 
 
484
    IMPLEMENTATION
 
485
    The table cache_key is created from:
 
486
    db_name + \0
 
487
    table_name + \0
 
488
 
 
489
    if the table is a tmp table, we add the following to make each tmp table
 
490
    unique on the slave:
 
491
 
 
492
    4 bytes for master thread id
 
493
    4 bytes pseudo thread id
 
494
 
 
495
    RETURN
 
496
    Length of key
 
497
  */
 
498
 
 
499
  static inline uint32_t createKey(char *key, std::string& db_arg,
 
500
                                   std::string& table_name_arg)
 
501
  {
 
502
    return createKey(key, db_arg.c_str(), table_name_arg.c_str());
 
503
  }
 
504
 
 
505
  static inline uint32_t createKey(char *key, const char *db_arg, const char *table_name_arg)
 
506
  {
 
507
    uint32_t key_length;
 
508
    char *key_pos= key;
 
509
 
 
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;
 
514
 
 
515
    return key_length;
 
516
  }
 
517
 
 
518
  static void cacheStart(void);
 
519
  static void cacheStop(void);
630
520
  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,
635
 
                                               int &error);
636
 
 
637
 
  friend std::ostream& operator<<(std::ostream& output, const TableShare &share)
638
 
  {
639
 
    output << "TableShare:(";
640
 
    output <<  share.getSchemaName();
641
 
    output << ", ";
642
 
    output << share.getTableName();
643
 
    output << ", ";
644
 
    output << share.getTableTypeAsString();
645
 
    output << ", ";
646
 
    output << share.getPath();
647
 
    output << ")";
648
 
 
649
 
    return output;  // for multiple << operators.
650
 
  }
651
 
 
652
 
  Field *make_field(unsigned char *ptr,
653
 
                    uint32_t field_length,
654
 
                    bool is_nullable,
655
 
                    unsigned char *null_pos,
656
 
                    unsigned char null_bit,
657
 
                    uint8_t decimals,
658
 
                    enum_field_types field_type,
659
 
                    const CHARSET_INFO * field_charset,
660
 
                    Field::utype unireg_check,
661
 
                    TYPELIB *interval,
662
 
                    const char *field_name);
663
 
 
664
 
  int open_table_def(Session& session, const TableIdentifier &identifier);
665
 
 
666
 
  int open_table_from_share(Session *session,
667
 
                            const TableIdentifier &identifier,
668
 
                            const char *alias,
669
 
                            uint32_t db_stat, uint32_t ha_open_flags,
670
 
                            Table &outparam);
671
 
private:
672
 
  int open_table_from_share_inner(Session *session,
673
 
                                  const char *alias,
674
 
                                  uint32_t db_stat,
675
 
                                  Table &outparam);
676
 
  int open_table_cursor_inner(const TableIdentifier &identifier,
677
 
                              uint32_t db_stat, uint32_t ha_open_flags,
678
 
                              Table &outparam,
679
 
                              bool &error_reported);
680
 
public:
681
 
  int parse_table_proto(Session& session, message::Table &table);
682
 
private:
683
 
  int inner_parse_table_proto(Session& session, message::Table &table);
 
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);
684
526
};
685
527
 
686
528
} /* namespace drizzled */
687
529
 
688
 
#endif /* DRIZZLED_DEFINITION_TABLE_H */
 
530
#endif /* DRIZZLED_TABLE_SHARE_H */