~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table/instance/base.h

  • Committer: Brian Aker
  • Date: 2011-02-22 06:12:02 UTC
  • mfrom: (2190.1.6 drizzle-build)
  • Revision ID: brian@tangent.org-20110222061202-k03czxykqy4x9hjs
List update, header fixes, multiple symbols, and David deletes some code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2009 Sun Microsystems
 
4
 *  Copyright (C) 2009 Sun Microsystems, Inc.
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
23
23
  instance of table share per one table in the database.
24
24
*/
25
25
 
26
 
#ifndef DRIZZLED_TABLE_SHARE_H
27
 
#define DRIZZLED_TABLE_SHARE_H
 
26
#ifndef DRIZZLED_TABLE_INSTANCE_BASE_H
 
27
#define DRIZZLED_TABLE_INSTANCE_BASE_H
28
28
 
29
29
#include <string>
30
30
 
32
32
#include <boost/thread/condition_variable.hpp>
33
33
#include <boost/dynamic_bitset.hpp>
34
34
#include <boost/shared_ptr.hpp>
35
 
 
36
 
#include "drizzled/typelib.h"
37
 
#include "drizzled/memory/root.h"
38
 
#include "drizzled/message/table.pb.h"
39
 
#include "drizzled/util/string.h"
 
35
#include <boost/scoped_ptr.hpp>
 
36
 
 
37
#include <drizzled/memory/root.h>
 
38
#include <drizzled/message.h>
 
39
#include <drizzled/util/string.h>
 
40
 
 
41
#include <drizzled/lex_string.h>
 
42
#include <drizzled/key_map.h>
 
43
 
 
44
#include <drizzled/table/cache.h>
 
45
 
 
46
#include <drizzled/field.h>
 
47
 
40
48
 
41
49
namespace drizzled
42
50
{
43
51
 
44
 
typedef boost::shared_ptr<TableShare> TableSharePtr;
45
 
 
46
 
typedef boost::unordered_map< TableIdentifier::Key, TableSharePtr> TableDefinitionCache;
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");
52
53
 
53
54
namespace plugin
54
55
{
55
56
class EventObserverList;
 
57
class StorageEngine;
 
58
}
 
59
 
 
60
namespace table {
 
61
class Singular;
56
62
}
57
63
 
58
64
class Field_blob;
60
66
class TableShare
61
67
{
62
68
  typedef std::vector<std::string> StringVector;
 
69
 
63
70
public:
64
 
  TableShare(TableIdentifier::Type type_arg);
65
 
 
66
 
  TableShare(TableIdentifier &identifier, const TableIdentifier::Key &key); // Used by placeholder
67
 
 
68
 
  TableShare(const TableIdentifier &identifier); // Just used during createTable()
69
 
 
70
 
  TableShare(TableIdentifier::Type type_arg,
71
 
             TableIdentifier &identifier,
 
71
  typedef boost::shared_ptr<TableShare> shared_ptr;
 
72
  typedef std::vector <shared_ptr> vector;
 
73
 
 
74
  TableShare(const identifier::Table::Type type_arg);
 
75
 
 
76
  TableShare(const identifier::Table &identifier, const identifier::Table::Key &key); // Used by placeholder
 
77
 
 
78
  TableShare(const identifier::Table &identifier); // Just used during createTable()
 
79
 
 
80
  TableShare(const identifier::Table::Type type_arg,
 
81
             const identifier::Table &identifier,
72
82
             char *path_arg= NULL, uint32_t path_length_arg= 0); // Shares for cache
73
83
 
74
 
  ~TableShare();
 
84
  virtual ~TableShare();
75
85
 
76
86
private:
77
87
  /** Category of this table. */
78
88
  enum_table_category table_category;
79
89
 
80
 
  uint32_t open_count;                  /* Number of tables in open list */
81
90
public:
82
 
 
83
91
  bool isTemporaryCategory() const
84
92
  {
85
93
    return (table_category == TABLE_CATEGORY_TEMPORARY);
92
100
 
93
101
  /* The following is copied to each Table on OPEN */
94
102
  typedef std::vector<Field *> Fields;
 
103
 
95
104
private:
96
 
  Fields field;
 
105
  Fields _fields;
 
106
 
97
107
public:
98
108
  const Fields getFields() const
99
109
  {
100
 
    return field;
 
110
    return _fields;
 
111
  }
 
112
 
 
113
  Fields getFields()
 
114
  {
 
115
    return _fields;
101
116
  }
102
117
 
103
118
  Field ** getFields(bool)
104
119
  {
105
 
    return &field[0];
 
120
    return &_fields[0];
106
121
  }
107
122
 
108
123
  void setFields(uint32_t arg)
109
124
  {
110
 
    field.resize(arg);
 
125
    _fields.resize(arg);
111
126
  }
112
127
 
113
128
  uint32_t positionFields(Field **arg) const
114
129
  {
115
 
    return (arg - (Field **)&field[0]);
 
130
    return (arg - (Field **)&_fields[0]);
116
131
  }
117
132
 
118
133
  void pushField(Field *arg)
119
134
  {
120
 
    fields++;
121
 
    field.push_back(arg);
 
135
    _field_size++;
 
136
    _fields.push_back(arg);
122
137
  }
123
138
 
124
 
 
125
139
  Field **found_next_number_field;
 
140
 
126
141
private:
127
142
  Field *timestamp_field;               /* Used only during open */
 
143
 
128
144
public:
129
145
 
130
146
  Field *getTimestampField() const               /* Used only during open */
140
156
 
141
157
private:
142
158
  KeyInfo  *key_info;                   /* data of keys in database */
 
159
 
143
160
public:
144
161
  KeyInfo &getKeyInfo(uint32_t arg) const
145
162
  {
147
164
  }
148
165
  std::vector<uint>     blob_field;                     /* Index to blobs in Field arrray*/
149
166
 
 
167
private:
150
168
  /* hash of field names (contains pointers to elements of field array) */
151
 
private:
152
169
  typedef boost::unordered_map < std::string, Field **, util::insensitive_hash, util::insensitive_equal_to> FieldMap;
153
170
  typedef std::pair< std::string, Field ** > FieldMapPair;
154
171
  FieldMap name_hash; /* hash of field names */
 
172
 
155
173
public:
156
174
  size_t getNamedFieldSize() const
157
175
  {
170
188
 
171
189
private:
172
190
  memory::Root mem_root;
173
 
public:
 
191
 
174
192
  void *alloc_root(size_t arg)
175
193
  {
176
194
    return mem_root.alloc_root(arg);
186
204
    return &mem_root;
187
205
  }
188
206
 
189
 
private:
190
207
  std::vector<std::string> _keynames;
191
208
 
192
209
  void addKeyName(std::string arg)
195
212
                   arg.begin(), ::toupper);
196
213
    _keynames.push_back(arg);
197
214
  }
 
215
 
198
216
public:
199
217
  bool doesKeyNameExist(const char *name_arg, uint32_t name_length, uint32_t &position) const
200
218
  {
201
 
    std::string arg(name_arg, name_length);
202
 
    std::transform(arg.begin(), arg.end(),
203
 
                   arg.begin(), ::toupper);
204
 
 
205
 
    std::vector<std::string>::const_iterator iter= std::find(_keynames.begin(), _keynames.end(), arg);
206
 
 
207
 
    if (iter == _keynames.end())
208
 
      return false;
209
 
 
210
 
    position= iter -  _keynames.begin();
211
 
 
212
 
    return true;
 
219
    return doesKeyNameExist(std::string(name_arg, name_length), position);
213
220
  }
214
221
 
215
222
  bool doesKeyNameExist(std::string arg, uint32_t &position) const
233
240
private:
234
241
  std::vector<TYPELIB> intervals;                       /* pointer to interval info */
235
242
 
236
 
  boost::mutex mutex;                /* For locking the share  */
237
 
  boost::condition_variable cond;                       /* To signal that share is ready */
238
 
 
239
 
 
240
 
  void lock()
241
 
  {
242
 
    mutex.lock();
243
 
  }
244
 
 
245
 
  void unlock()
246
 
  {
247
 
    mutex.unlock();
248
 
  }
249
243
public:
 
244
  virtual void lock()
 
245
  { }
 
246
 
 
247
  virtual void unlock()
 
248
  { }
250
249
 
251
250
private:
252
251
  std::vector<unsigned char> default_values;            /* row with default values */
 
252
 
253
253
public:
254
 
  unsigned char * getDefaultValues()
 
254
  // @note This needs to be made to be const in the future
 
255
  unsigned char *getDefaultValues()
255
256
  {
256
257
    return &default_values[0];
257
258
  }
260
261
    default_values.resize(arg);
261
262
  }
262
263
 
263
 
  const CHARSET_INFO *table_charset; /* Default charset of string fields */
 
264
  const charset_info_st *table_charset; /* Default charset of string fields */
264
265
 
265
266
  boost::dynamic_bitset<> all_set;
266
267
 
275
276
    To ensure this one can use set_table_cache() methods.
276
277
  */
277
278
private:
278
 
  TableIdentifier::Key private_key_for_cache; // This will not exist in the final design.
 
279
  identifier::Table::Key private_key_for_cache; // This will not exist in the final design.
279
280
  std::vector<char> private_normalized_path; // This will not exist in the final design.
280
281
  LEX_STRING db;                        /* Pointer to db */
281
282
  LEX_STRING table_name;                /* Table name (for open) */
282
283
  LEX_STRING path;      /* Path to table (from datadir) */
283
284
  LEX_STRING normalized_path;           /* unpack_filename(path) */
 
285
 
284
286
public:
285
287
 
286
288
  const char *getNormalizedPath() const
293
295
    return path.str;
294
296
  }
295
297
 
296
 
  const TableIdentifier::Key& getCacheKey() const // This should never be called when we aren't looking at a cache.
 
298
  const identifier::Table::Key& getCacheKey() const // This should never be called when we aren't looking at a cache.
297
299
  {
298
300
    assert(private_key_for_cache.size());
299
301
    return private_key_for_cache;
304
306
    return private_key_for_cache.size();
305
307
  }
306
308
 
 
309
private:
307
310
  void setPath(char *str_arg, uint32_t size_arg)
308
311
  {
309
312
    path.str= str_arg;
316
319
    normalized_path.length= size_arg;
317
320
  }
318
321
 
 
322
public:
 
323
 
319
324
  const char *getTableName() const
320
325
  {
321
326
    return table_name.str;
351
356
 
352
357
private:
353
358
  uint64_t   version;
 
359
 
354
360
public:
355
361
  uint64_t getVersion() const
356
362
  {
357
363
    return version;
358
364
  }
359
365
 
360
 
  void refreshVersion()
361
 
  {
362
 
   version= refresh_version;
363
 
  }
 
366
  void refreshVersion();
364
367
 
365
368
  void resetVersion()
366
369
  {
367
370
    version= 0;
368
371
  }
369
372
 
 
373
private:
370
374
  uint32_t   timestamp_offset;          /* Set to offset+1 of record */
371
 
private:
372
 
  uint32_t   reclength;                 /* Recordlength */
 
375
 
 
376
  uint32_t reclength;                   /* Recordlength */
 
377
  uint32_t stored_rec_length;         /* Stored record length*/
 
378
 
373
379
public:
374
 
  uint32_t   stored_rec_length;         /* Stored record length*/
 
380
  uint32_t sizeStoredRecord() const
 
381
  {
 
382
    return stored_rec_length;
 
383
  }
375
384
 
376
385
  uint32_t getRecordLength() const
377
386
  {
386
395
  const Field_blob *getBlobFieldAt(uint32_t arg) const
387
396
  {
388
397
    if (arg < blob_fields)
389
 
      return (Field_blob*) field[blob_field[arg]];
 
398
      return (Field_blob*) _fields[blob_field[arg]];
390
399
 
391
400
    return NULL;
392
401
  }
395
404
  /* Max rows is a hint to HEAP during a create tmp table */
396
405
  uint64_t max_rows;
397
406
 
398
 
  message::Table *table_proto;
 
407
  boost::scoped_ptr<message::Table> _table_message;
 
408
 
399
409
public:
400
 
 
401
410
  /*
402
 
    @note Without a table_proto, we assume we are building a STANDARD table.
 
411
    @note Without a _table_message, we assume we are building a STANDARD table.
403
412
    This will be modified once we use Identifiers in the Share itself.
404
413
  */
405
414
  message::Table::TableType getTableType() const
406
415
  {
407
 
    return table_proto ? table_proto->type() : message::Table::STANDARD;
 
416
    return getTableMessage() ? getTableMessage()->type() : message::Table::STANDARD;
408
417
  }
409
418
 
410
419
  const std::string &getTableTypeAsString() const
411
420
  {
412
 
    switch (table_proto->type())
413
 
    {
414
 
    default:
415
 
    case message::Table::STANDARD:
416
 
      return STANDARD_STRING;
417
 
    case message::Table::TEMPORARY:
418
 
      return TEMPORARY_STRING;
419
 
    case message::Table::INTERNAL:
420
 
      return INTERNAL_STRING;
421
 
    case message::Table::FUNCTION:
422
 
      return FUNCTION_STRING;
423
 
    }
 
421
    if (getTableMessage())
 
422
      return message::type(getTableMessage()->type());
 
423
 
 
424
    return NO_PROTOBUFFER_AVAILABLE;
424
425
  }
425
426
 
426
427
  /* This is only used in one location currently */
427
 
  inline message::Table *getTableProto() const
428
 
  {
429
 
    return table_proto;
430
 
  }
431
 
 
432
 
  inline void setTableProto(message::Table *arg)
433
 
  {
434
 
    assert(table_proto == NULL);
435
 
    table_proto= arg;
 
428
  inline message::Table *getTableMessage() const
 
429
  {
 
430
    return _table_message.get();
 
431
  }
 
432
 
 
433
  void setTableMessage(const message::Table &arg)
 
434
  {
 
435
    assert(not getTableMessage());
 
436
    _table_message.reset(new(std::nothrow) message::Table(arg));
 
437
  }
 
438
 
 
439
  const message::Table::Field &field(int32_t field_position) const
 
440
  {
 
441
    assert(getTableMessage());
 
442
    return getTableMessage()->field(field_position);
436
443
  }
437
444
 
438
445
  inline bool hasComment() const
439
446
  {
440
 
    return (table_proto) ?  table_proto->options().has_comment() : false; 
 
447
    return (getTableMessage()) ?  getTableMessage()->options().has_comment() : false; 
441
448
  }
442
449
 
443
450
  inline const char *getComment()
444
451
  {
445
 
    return (table_proto && table_proto->has_options()) ?  table_proto->options().comment().c_str() : NULL; 
 
452
    return (getTableMessage() && getTableMessage()->has_options()) ?  getTableMessage()->options().comment().c_str() : NULL; 
446
453
  }
447
454
 
448
455
  inline uint32_t getCommentLength() const
449
456
  {
450
 
    return (table_proto) ? table_proto->options().comment().length() : 0; 
 
457
    return (getTableMessage()) ? getTableMessage()->options().comment().length() : 0; 
451
458
  }
452
459
 
453
460
  inline uint64_t getMaxRows() const
477
484
  }
478
485
 
479
486
private:
480
 
  TableIdentifier::Type tmp_table;
 
487
  identifier::Table::Type tmp_table;
481
488
public:
482
489
 
483
 
  TableIdentifier::Type getType() const
 
490
  identifier::Table::Type getType() const
484
491
  {
485
492
    return tmp_table;
486
493
  }
487
494
 
488
495
private:
489
 
  uint32_t ref_count;       /* How many Table objects uses this */
 
496
  uint32_t _ref_count;       /* How many Table objects uses this */
 
497
 
490
498
public:
491
499
  uint32_t getTableCount() const
492
500
  {
493
 
    return ref_count;
 
501
    return _ref_count;
494
502
  }
495
503
 
496
504
  void incrementTableCount()
497
505
  {
498
506
    lock();
499
 
    ref_count++;
 
507
    _ref_count++;
500
508
    unlock();
501
509
  }
502
510
 
 
511
  uint32_t decrementTableCount()
 
512
  {
 
513
    return --_ref_count;
 
514
  }
 
515
 
503
516
  uint32_t null_bytes;
504
517
  uint32_t last_null_bit_pos;
505
 
  uint32_t fields;                              /* Number of fields */
 
518
private:
 
519
  uint32_t _field_size;                         /* Number of fields */
 
520
 
 
521
public:
 
522
  void setFieldSize(uint32_t arg)
 
523
  {
 
524
    _field_size= arg;
 
525
  }
506
526
 
507
527
  uint32_t sizeFields() const
508
528
  {
509
 
    return fields;
 
529
    return _field_size;
510
530
  }
511
531
 
512
532
  uint32_t rec_buff_length;                 /* Size of table->record[] buffer */
521
541
  uint32_t uniques;                         /* Number of UNIQUE index */
522
542
  uint32_t null_fields;                 /* number of null fields */
523
543
  uint32_t blob_fields;                 /* number of blob fields */
524
 
  uint32_t timestamp_field_offset;              /* Field number for timestamp field */
525
544
private:
526
545
  bool has_variable_width;                  /* number of varchar fields */
 
546
 
527
547
public:
528
548
  bool hasVariableWidth() const
529
549
  {
566
586
  uint32_t next_number_keypart;             /* autoinc keypart number in a key */
567
587
  uint32_t error, open_errno, errarg;       /* error from open_table_def() */
568
588
 
 
589
private:
569
590
  uint8_t blob_ptr_size;                        /* 4 or 8 */
 
591
 
 
592
public:
 
593
  uint8_t sizeBlobPtr() const
 
594
  {
 
595
    return blob_ptr_size;
 
596
  }
 
597
 
570
598
  bool db_low_byte_first;               /* Portable row format */
571
599
 
572
 
private:
573
 
  bool name_lock;
574
 
public:
575
 
  bool isNameLock() const
576
 
  {
577
 
    return name_lock;
578
 
  }
579
 
 
580
 
  bool replace_with_name_lock;
581
 
 
582
 
private:
583
 
  bool waiting_on_cond;                 /* Protection against free */
584
 
public:
585
 
  bool isWaitingOnCondition()
586
 
  {
587
 
    return waiting_on_cond;
588
 
  }
589
 
 
590
600
  /*
591
601
    Set of keys in use, implemented as a Bitmap.
592
602
    Excludes keys disabled by ALTER Table ... DISABLE KEYS.
598
608
    event_observers is a class containing all the event plugins that have 
599
609
    registered an interest in this table.
600
610
  */
601
 
  private:
602
 
  plugin::EventObserverList *event_observers;
603
 
  public:
604
 
  plugin::EventObserverList *getTableObservers() 
 
611
  virtual plugin::EventObserverList *getTableObservers() 
605
612
  { 
606
 
    return event_observers;
 
613
    return NULL;
607
614
  }
608
615
  
609
 
  void setTableObservers(plugin::EventObserverList *observers) 
610
 
  { 
611
 
    event_observers= observers;
612
 
  }
 
616
  virtual void setTableObservers(plugin::EventObserverList *) 
 
617
  { }
613
618
  
614
619
  /*
615
620
    Set share's identifier information.
620
625
    NOTES
621
626
  */
622
627
 
623
 
  void setIdentifier(TableIdentifier &identifier_arg);
 
628
  void setIdentifier(const identifier::Table &identifier_arg);
624
629
 
625
630
  /*
626
631
    Initialize share for temporary tables
641
646
private:
642
647
  void init(const char *new_table_name,
643
648
            const char *new_path);
644
 
public:
645
649
 
 
650
protected:
646
651
  void open_table_error(int pass_error, int db_errno, int pass_errarg);
647
652
 
648
 
  static void cacheStart(void);
649
 
  static void release(TableShare *share);
650
 
  static void release(TableSharePtr &share);
651
 
  static void release(TableIdentifier &identifier);
652
 
  static const TableDefinitionCache &getCache();
653
 
  static TableSharePtr getShare(TableIdentifier &identifier);
654
 
  static TableSharePtr getShareCreate(Session *session, 
655
 
                                      TableIdentifier &identifier,
656
 
                                      int *error);
 
653
public:
 
654
 
 
655
  static TableShare::shared_ptr getShareCreate(Session *session, 
 
656
                                               const identifier::Table &identifier,
 
657
                                               int &error);
657
658
 
658
659
  friend std::ostream& operator<<(std::ostream& output, const TableShare &share)
659
660
  {
670
671
    return output;  // for multiple << operators.
671
672
  }
672
673
 
673
 
  Field *make_field(unsigned char *ptr,
 
674
protected:
 
675
  friend class drizzled::table::Singular;
 
676
 
 
677
  Field *make_field(const message::Table::Field &pfield,
 
678
                    unsigned char *ptr,
674
679
                    uint32_t field_length,
675
680
                    bool is_nullable,
676
681
                    unsigned char *null_pos,
677
682
                    unsigned char null_bit,
678
683
                    uint8_t decimals,
679
684
                    enum_field_types field_type,
680
 
                    const CHARSET_INFO * field_charset,
 
685
                    const charset_info_st * field_charset,
681
686
                    Field::utype unireg_check,
682
687
                    TYPELIB *interval,
683
688
                    const char *field_name);
684
689
 
685
 
  int open_table_def(Session& session, TableIdentifier &identifier);
 
690
  Field *make_field(const message::Table::Field &pfield,
 
691
                    unsigned char *ptr,
 
692
                    uint32_t field_length,
 
693
                    bool is_nullable,
 
694
                    unsigned char *null_pos,
 
695
                    unsigned char null_bit,
 
696
                    uint8_t decimals,
 
697
                    enum_field_types field_type,
 
698
                    const charset_info_st * field_charset,
 
699
                    Field::utype unireg_check,
 
700
                    TYPELIB *interval,
 
701
                    const char *field_name, 
 
702
                    bool is_unsigned);
 
703
 
 
704
public:
 
705
  int open_table_def(Session& session, const identifier::Table &identifier);
686
706
 
687
707
  int open_table_from_share(Session *session,
688
 
                            const TableIdentifier &identifier,
 
708
                            const identifier::Table &identifier,
689
709
                            const char *alias,
690
710
                            uint32_t db_stat, uint32_t ha_open_flags,
691
711
                            Table &outparam);
692
 
  int parse_table_proto(Session& session, message::Table &table);
693
712
private:
694
 
  int inner_parse_table_proto(Session& session, message::Table &table);
 
713
  int open_table_from_share_inner(Session *session,
 
714
                                  const char *alias,
 
715
                                  uint32_t db_stat,
 
716
                                  Table &outparam);
 
717
  int open_table_cursor_inner(const identifier::Table &identifier,
 
718
                              uint32_t db_stat, uint32_t ha_open_flags,
 
719
                              Table &outparam,
 
720
                              bool &error_reported);
 
721
public:
 
722
  bool parse_table_proto(Session& session, message::Table &table);
 
723
 
 
724
  virtual bool replicate() const
 
725
  {
 
726
    return false;
 
727
  }
695
728
};
696
729
 
697
730
} /* namespace drizzled */
698
731
 
699
 
#endif /* DRIZZLED_TABLE_SHARE_H */
 
732
#endif /* DRIZZLED_TABLE_INSTANCE_BASE_H */