~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table_share.h

  • Committer: Brian Aker
  • Date: 2010-08-08 01:18:02 UTC
  • mto: (1698.1.1 drizzle)
  • mto: This revision was merged to the branch mainline in revision 1696.
  • Revision ID: brian@gaz-20100808011802-go9sp57jxgmcgfxn
More encapsulation of thr_lock

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
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
32
 
36
33
#include "drizzled/typelib.h"
37
34
#include "drizzled/memory/root.h"
38
35
#include "drizzled/message/table.pb.h"
39
36
#include "drizzled/util/string.h"
40
37
 
41
 
#include "drizzled/table/cache.h"
42
 
 
43
38
namespace drizzled
44
39
{
45
40
 
46
 
extern uint64_t refresh_version;
 
41
typedef boost::unordered_map< TableIdentifier::Key, TableShare *> TableDefinitionCache;
47
42
 
48
43
const static std::string STANDARD_STRING("STANDARD");
49
44
const static std::string TEMPORARY_STRING("TEMPORARY");
50
45
const static std::string INTERNAL_STRING("INTERNAL");
51
46
const static std::string FUNCTION_STRING("FUNCTION");
52
 
const static std::string NO_PROTOBUFFER_AVAILABLE("NO PROTOBUFFER AVAILABLE");
53
47
 
54
48
namespace plugin
55
49
{
61
55
class TableShare
62
56
{
63
57
  typedef std::vector<std::string> StringVector;
64
 
 
65
58
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
 
59
  TableShare(TableIdentifier::Type type_arg);
 
60
 
 
61
  TableShare(TableIdentifier &identifier, const TableIdentifier::Key &key); // Used by placeholder
72
62
 
73
63
  TableShare(const TableIdentifier &identifier); // Just used during createTable()
74
64
 
75
 
  TableShare(const TableIdentifier::Type type_arg,
76
 
             const TableIdentifier &identifier,
 
65
  TableShare(TableIdentifier::Type type_arg,
 
66
             TableIdentifier &identifier,
77
67
             char *path_arg= NULL, uint32_t path_length_arg= 0); // Shares for cache
78
68
 
79
69
  ~TableShare();
82
72
  /** Category of this table. */
83
73
  enum_table_category table_category;
84
74
 
 
75
  uint32_t open_count;                  /* Number of tables in open list */
85
76
public:
86
77
 
87
78
  bool isTemporaryCategory() const
174
165
 
175
166
private:
176
167
  memory::Root mem_root;
177
 
 
 
168
public:
178
169
  void *alloc_root(size_t arg)
179
170
  {
180
171
    return mem_root.alloc_root(arg);
190
181
    return &mem_root;
191
182
  }
192
183
 
 
184
private:
193
185
  std::vector<std::string> _keynames;
194
186
 
195
187
  void addKeyName(std::string arg)
201
193
public:
202
194
  bool doesKeyNameExist(const char *name_arg, uint32_t name_length, uint32_t &position) const
203
195
  {
204
 
    return doesKeyNameExist(std::string(name_arg, name_length), position);
 
196
    std::string arg(name_arg, name_length);
 
197
    std::transform(arg.begin(), arg.end(),
 
198
                   arg.begin(), ::toupper);
 
199
 
 
200
    std::vector<std::string>::const_iterator iter= std::find(_keynames.begin(), _keynames.end(), arg);
 
201
 
 
202
    if (iter == _keynames.end())
 
203
      return false;
 
204
 
 
205
    position= iter -  _keynames.begin();
 
206
 
 
207
    return true;
205
208
  }
206
209
 
207
210
  bool doesKeyNameExist(std::string arg, uint32_t &position) const
213
216
 
214
217
    if (iter == _keynames.end())
215
218
    {
216
 
      position= UINT32_MAX; //historical, required for finding primary key from unique
 
219
      position= -1; //historical, required for finding primary key from unique
217
220
      return false;
218
221
    }
219
222
 
225
228
private:
226
229
  std::vector<TYPELIB> intervals;                       /* pointer to interval info */
227
230
 
228
 
  boost::mutex mutex;                /* For locking the share  */
229
 
  boost::condition_variable cond;                       /* To signal that share is ready */
 
231
  pthread_mutex_t mutex;                /* For locking the share  */
 
232
  pthread_cond_t cond;                  /* To signal that share is ready */
230
233
 
231
234
 
232
235
  void lock()
233
236
  {
234
 
    mutex.lock();
 
237
    pthread_mutex_lock(&mutex);
235
238
  }
236
239
 
237
240
  void unlock()
238
241
  {
239
 
    mutex.unlock();
 
242
    pthread_mutex_unlock(&mutex);
240
243
  }
 
244
public:
241
245
 
 
246
private:
242
247
  std::vector<unsigned char> default_values;            /* row with default values */
243
248
public:
244
 
  // @note This needs to be made to be const in the future
245
 
  unsigned char *getDefaultValues()
 
249
  unsigned char * getDefaultValues()
246
250
  {
247
251
    return &default_values[0];
248
252
  }
253
257
 
254
258
  const CHARSET_INFO *table_charset; /* Default charset of string fields */
255
259
 
256
 
  boost::dynamic_bitset<> all_set;
 
260
  MyBitmap all_set;
 
261
private:
 
262
  std::vector<my_bitmap_map> all_bitmap;
257
263
 
 
264
public:
258
265
  /*
259
266
    Key which is used for looking-up table in table cache and in the list
260
267
    of thread's temporary tables. Has the form of:
295
302
    return private_key_for_cache.size();
296
303
  }
297
304
 
298
 
private:
299
305
  void setPath(char *str_arg, uint32_t size_arg)
300
306
  {
301
307
    path.str= str_arg;
307
313
    normalized_path.str= str_arg;
308
314
    normalized_path.length= size_arg;
309
315
  }
310
 
public:
311
316
 
312
317
  const char *getTableName() const
313
318
  {
402
407
 
403
408
  const std::string &getTableTypeAsString() const
404
409
  {
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;
 
410
    switch (table_proto->type())
 
411
    {
 
412
    default:
 
413
    case message::Table::STANDARD:
 
414
      return STANDARD_STRING;
 
415
    case message::Table::TEMPORARY:
 
416
      return TEMPORARY_STRING;
 
417
    case message::Table::INTERNAL:
 
418
      return INTERNAL_STRING;
 
419
    case message::Table::FUNCTION:
 
420
      return FUNCTION_STRING;
423
421
    }
424
422
  }
425
423
 
487
485
 
488
486
private:
489
487
  uint32_t ref_count;       /* How many Table objects uses this */
490
 
 
491
488
public:
492
489
  uint32_t getTableCount() const
493
490
  {
522
519
  uint32_t uniques;                         /* Number of UNIQUE index */
523
520
  uint32_t null_fields;                 /* number of null fields */
524
521
  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
 
  }
 
522
  uint32_t timestamp_field_offset;              /* Field number for timestamp field */
 
523
  uint32_t varchar_fields;                  /* number of varchar fields */
536
524
  uint32_t db_create_options;           /* Create options from database */
537
525
  uint32_t db_options_in_use;           /* Options in use */
538
526
  uint32_t db_record_offset;            /* if HA_REC_IN_SEQ */
565
553
  uint32_t next_number_key_offset;          /* autoinc keypart offset in a key */
566
554
  uint32_t next_number_keypart;             /* autoinc keypart number in a key */
567
555
  uint32_t error, open_errno, errarg;       /* error from open_table_def() */
 
556
  uint32_t column_bitmap_size;
568
557
 
569
558
  uint8_t blob_ptr_size;                        /* 4 or 8 */
570
559
  bool db_low_byte_first;               /* Portable row format */
571
560
 
 
561
private:
 
562
  bool name_lock;
 
563
public:
 
564
  bool isNameLock() const
 
565
  {
 
566
    return name_lock;
 
567
  }
 
568
 
 
569
  bool replace_with_name_lock;
 
570
 
 
571
private:
 
572
  bool waiting_on_cond;                 /* Protection against free */
 
573
public:
 
574
  bool isWaitingOnCondition()
 
575
  {
 
576
    return waiting_on_cond;
 
577
  }
 
578
 
572
579
  /*
573
580
    Set of keys in use, implemented as a Bitmap.
574
581
    Excludes keys disabled by ALTER Table ... DISABLE KEYS.
602
609
    NOTES
603
610
  */
604
611
 
605
 
  void setIdentifier(const TableIdentifier &identifier_arg);
 
612
  void setIdentifier(TableIdentifier &identifier_arg);
 
613
 
 
614
  inline bool honor_global_locks()
 
615
  {
 
616
    return (table_category == TABLE_CATEGORY_USER);
 
617
  }
 
618
 
606
619
 
607
620
  /*
608
621
    Initialize share for temporary tables
627
640
 
628
641
  void open_table_error(int pass_error, int db_errno, int pass_errarg);
629
642
 
 
643
  static void cacheStart(void);
630
644
  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);
 
645
  static void release(TableIdentifier &identifier);
 
646
  static const TableDefinitionCache &getCache();
 
647
  static TableShare *getShare(TableIdentifier &identifier);
 
648
  static TableShare *getShareCreate(Session *session, 
 
649
                                    TableIdentifier &identifier,
 
650
                                    int *error);
636
651
 
637
652
  friend std::ostream& operator<<(std::ostream& output, const TableShare &share)
638
653
  {
661
676
                    TYPELIB *interval,
662
677
                    const char *field_name);
663
678
 
664
 
  int open_table_def(Session& session, const TableIdentifier &identifier);
 
679
  int open_table_def(Session& session, TableIdentifier &identifier);
665
680
 
666
681
  int open_table_from_share(Session *session,
667
682
                            const TableIdentifier &identifier,
668
683
                            const char *alias,
669
684
                            uint32_t db_stat, uint32_t ha_open_flags,
670
685
                            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
686
  int parse_table_proto(Session& session, message::Table &table);
682
687
private:
683
688
  int inner_parse_table_proto(Session& session, message::Table &table);
685
690
 
686
691
} /* namespace drizzled */
687
692
 
688
 
#endif /* DRIZZLED_DEFINITION_TABLE_H */
 
693
#endif /* DRIZZLED_TABLE_SHARE_H */