~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: patrick crews
  • Date: 2011-06-08 03:02:27 UTC
  • mto: This revision was merged to the branch mainline in revision 2329.
  • Revision ID: gleebix@gmail.com-20110608030227-updkyv2652zvfajc
Initial voodoo worked to give us a crashme mode.  Need docs still

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_DEFINITION_TABLE_H
27
 
#define DRIZZLED_DEFINITION_TABLE_H
 
26
#pragma once
28
27
 
29
28
#include <string>
30
29
 
32
31
#include <boost/thread/condition_variable.hpp>
33
32
#include <boost/dynamic_bitset.hpp>
34
33
#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"
40
 
 
41
 
#include "drizzled/table/cache.h"
42
 
 
43
 
namespace drizzled
44
 
{
45
 
 
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");
 
34
#include <boost/scoped_ptr.hpp>
 
35
 
 
36
#include <drizzled/memory/root.h>
 
37
#include <drizzled/message.h>
 
38
#include <drizzled/util/string.h>
 
39
#include <drizzled/lex_string.h>
 
40
#include <drizzled/key_map.h>
 
41
#include <drizzled/field.h>
 
42
 
 
43
namespace drizzled {
 
44
 
52
45
const static std::string NO_PROTOBUFFER_AVAILABLE("NO PROTOBUFFER AVAILABLE");
53
46
 
54
 
namespace plugin
55
 
{
56
 
class EventObserverList;
57
 
}
58
 
 
59
 
class Field_blob;
60
 
 
61
47
class TableShare
62
48
{
63
49
  typedef std::vector<std::string> StringVector;
66
52
  typedef boost::shared_ptr<TableShare> shared_ptr;
67
53
  typedef std::vector <shared_ptr> vector;
68
54
 
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,
 
55
  TableShare(const identifier::Table::Type type_arg);
 
56
 
 
57
  TableShare(const identifier::Table &identifier, const identifier::Table::Key &key); // Used by placeholder
 
58
 
 
59
  TableShare(const identifier::Table &identifier); // Just used during createTable()
 
60
 
 
61
  TableShare(const identifier::Table::Type type_arg,
 
62
             const identifier::Table &identifier,
77
63
             char *path_arg= NULL, uint32_t path_length_arg= 0); // Shares for cache
78
64
 
79
 
  ~TableShare();
 
65
  virtual ~TableShare();
80
66
 
81
67
private:
82
68
  /** Category of this table. */
83
69
  enum_table_category table_category;
84
70
 
85
71
public:
86
 
 
87
72
  bool isTemporaryCategory() const
88
73
  {
89
74
    return (table_category == TABLE_CATEGORY_TEMPORARY);
96
81
 
97
82
  /* The following is copied to each Table on OPEN */
98
83
  typedef std::vector<Field *> Fields;
 
84
 
99
85
private:
100
 
  Fields field;
 
86
  Fields _fields;
 
87
 
101
88
public:
102
89
  const Fields getFields() const
103
90
  {
104
 
    return field;
 
91
    return _fields;
 
92
  }
 
93
 
 
94
  Fields getFields()
 
95
  {
 
96
    return _fields;
105
97
  }
106
98
 
107
99
  Field ** getFields(bool)
108
100
  {
109
 
    return &field[0];
 
101
    return &_fields[0];
110
102
  }
111
103
 
112
104
  void setFields(uint32_t arg)
113
105
  {
114
 
    field.resize(arg);
 
106
    _fields.resize(arg);
115
107
  }
116
108
 
117
109
  uint32_t positionFields(Field **arg) const
118
110
  {
119
 
    return (arg - (Field **)&field[0]);
 
111
    return (arg - (Field **)&_fields[0]);
120
112
  }
121
113
 
122
114
  void pushField(Field *arg)
123
115
  {
124
 
    fields++;
125
 
    field.push_back(arg);
 
116
    _field_size++;
 
117
    _fields.push_back(arg);
126
118
  }
127
119
 
128
 
 
129
120
  Field **found_next_number_field;
 
121
 
130
122
private:
131
123
  Field *timestamp_field;               /* Used only during open */
 
124
 
132
125
public:
133
126
 
134
127
  Field *getTimestampField() const               /* Used only during open */
144
137
 
145
138
private:
146
139
  KeyInfo  *key_info;                   /* data of keys in database */
 
140
 
147
141
public:
148
142
  KeyInfo &getKeyInfo(uint32_t arg) const
149
143
  {
151
145
  }
152
146
  std::vector<uint>     blob_field;                     /* Index to blobs in Field arrray*/
153
147
 
 
148
private:
154
149
  /* hash of field names (contains pointers to elements of field array) */
155
 
private:
156
150
  typedef boost::unordered_map < std::string, Field **, util::insensitive_hash, util::insensitive_equal_to> FieldMap;
157
151
  typedef std::pair< std::string, Field ** > FieldMapPair;
158
152
  FieldMap name_hash; /* hash of field names */
 
153
 
159
154
public:
160
155
  size_t getNamedFieldSize() const
161
156
  {
169
164
    if (iter == name_hash.end())
170
165
        return 0;
171
166
 
172
 
    return (*iter).second;
 
167
    return iter->second;
173
168
  }
174
169
 
175
170
private:
198
193
                   arg.begin(), ::toupper);
199
194
    _keynames.push_back(arg);
200
195
  }
 
196
 
201
197
public:
202
198
  bool doesKeyNameExist(const char *name_arg, uint32_t name_length, uint32_t &position) const
203
199
  {
225
221
private:
226
222
  std::vector<TYPELIB> intervals;                       /* pointer to interval info */
227
223
 
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
 
 
 
224
public:
 
225
  virtual void lock()
 
226
  { }
 
227
 
 
228
  virtual void unlock()
 
229
  { }
 
230
 
 
231
private:
242
232
  std::vector<unsigned char> default_values;            /* row with default values */
 
233
 
243
234
public:
244
235
  // @note This needs to be made to be const in the future
245
236
  unsigned char *getDefaultValues()
251
242
    default_values.resize(arg);
252
243
  }
253
244
 
254
 
  const CHARSET_INFO *table_charset; /* Default charset of string fields */
 
245
  const charset_info_st *table_charset; /* Default charset of string fields */
255
246
 
256
247
  boost::dynamic_bitset<> all_set;
257
248
 
266
257
    To ensure this one can use set_table_cache() methods.
267
258
  */
268
259
private:
269
 
  TableIdentifier::Key private_key_for_cache; // This will not exist in the final design.
 
260
  identifier::Table::Key private_key_for_cache; // This will not exist in the final design.
270
261
  std::vector<char> private_normalized_path; // This will not exist in the final design.
271
262
  LEX_STRING db;                        /* Pointer to db */
272
263
  LEX_STRING table_name;                /* Table name (for open) */
273
264
  LEX_STRING path;      /* Path to table (from datadir) */
274
265
  LEX_STRING normalized_path;           /* unpack_filename(path) */
 
266
 
275
267
public:
276
268
 
277
269
  const char *getNormalizedPath() const
284
276
    return path.str;
285
277
  }
286
278
 
287
 
  const TableIdentifier::Key& getCacheKey() const // This should never be called when we aren't looking at a cache.
 
279
  const identifier::Table::Key& getCacheKey() const // This should never be called when we aren't looking at a cache.
288
280
  {
289
281
    assert(private_key_for_cache.size());
290
282
    return private_key_for_cache;
307
299
    normalized_path.str= str_arg;
308
300
    normalized_path.length= size_arg;
309
301
  }
 
302
 
310
303
public:
311
304
 
312
305
  const char *getTableName() const
344
337
 
345
338
private:
346
339
  uint64_t   version;
 
340
 
347
341
public:
348
342
  uint64_t getVersion() const
349
343
  {
350
344
    return version;
351
345
  }
352
346
 
353
 
  void refreshVersion()
354
 
  {
355
 
   version= refresh_version;
356
 
  }
 
347
  void refreshVersion();
357
348
 
358
349
  void resetVersion()
359
350
  {
360
351
    version= 0;
361
352
  }
362
353
 
 
354
private:
363
355
  uint32_t   timestamp_offset;          /* Set to offset+1 of record */
364
 
private:
365
 
  uint32_t   reclength;                 /* Recordlength */
 
356
 
 
357
  uint32_t reclength;                   /* Recordlength */
 
358
  uint32_t stored_rec_length;         /* Stored record length*/
 
359
 
366
360
public:
367
 
  uint32_t   stored_rec_length;         /* Stored record length*/
 
361
  uint32_t sizeStoredRecord() const
 
362
  {
 
363
    return stored_rec_length;
 
364
  }
368
365
 
369
366
  uint32_t getRecordLength() const
370
367
  {
379
376
  const Field_blob *getBlobFieldAt(uint32_t arg) const
380
377
  {
381
378
    if (arg < blob_fields)
382
 
      return (Field_blob*) field[blob_field[arg]];
 
379
      return (Field_blob*) _fields[blob_field[arg]];
383
380
 
384
381
    return NULL;
385
382
  }
388
385
  /* Max rows is a hint to HEAP during a create tmp table */
389
386
  uint64_t max_rows;
390
387
 
391
 
  message::Table *table_proto;
 
388
  boost::scoped_ptr<message::Table> _table_message;
 
389
 
392
390
public:
393
 
 
394
391
  /*
395
 
    @note Without a table_proto, we assume we are building a STANDARD table.
 
392
    @note Without a _table_message, we assume we are building a STANDARD table.
396
393
    This will be modified once we use Identifiers in the Share itself.
397
394
  */
398
395
  message::Table::TableType getTableType() const
399
396
  {
400
 
    return table_proto ? table_proto->type() : message::Table::STANDARD;
 
397
    return getTableMessage() ? getTableMessage()->type() : message::Table::STANDARD;
401
398
  }
402
399
 
403
400
  const std::string &getTableTypeAsString() const
404
401
  {
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
 
    }
 
402
    if (getTableMessage())
 
403
      return message::type(getTableMessage()->type());
 
404
 
 
405
    return NO_PROTOBUFFER_AVAILABLE;
424
406
  }
425
407
 
426
408
  /* 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;
 
409
  inline message::Table *getTableMessage() const
 
410
  {
 
411
    return _table_message.get();
 
412
  }
 
413
 
 
414
  void setTableMessage(const message::Table &arg)
 
415
  {
 
416
    assert(not getTableMessage());
 
417
    _table_message.reset(new message::Table(arg));
 
418
  }
 
419
 
 
420
  const message::Table::Field &field(int32_t field_position) const
 
421
  {
 
422
    assert(getTableMessage());
 
423
    return getTableMessage()->field(field_position);
436
424
  }
437
425
 
438
426
  inline bool hasComment() const
439
427
  {
440
 
    return (table_proto) ?  table_proto->options().has_comment() : false; 
 
428
    return getTableMessage() ?  getTableMessage()->options().has_comment() : false; 
441
429
  }
442
430
 
443
431
  inline const char *getComment()
444
432
  {
445
 
    return (table_proto && table_proto->has_options()) ?  table_proto->options().comment().c_str() : NULL; 
 
433
    return (getTableMessage() && getTableMessage()->has_options()) ?  getTableMessage()->options().comment().c_str() : NULL; 
446
434
  }
447
435
 
448
436
  inline uint32_t getCommentLength() const
449
437
  {
450
 
    return (table_proto) ? table_proto->options().comment().length() : 0; 
 
438
    return (getTableMessage()) ? getTableMessage()->options().comment().length() : 0; 
451
439
  }
452
440
 
453
441
  inline uint64_t getMaxRows() const
477
465
  }
478
466
 
479
467
private:
480
 
  TableIdentifier::Type tmp_table;
 
468
  identifier::Table::Type tmp_table;
481
469
public:
482
470
 
483
 
  TableIdentifier::Type getType() const
 
471
  identifier::Table::Type getType() const
484
472
  {
485
473
    return tmp_table;
486
474
  }
487
475
 
488
476
private:
489
 
  uint32_t ref_count;       /* How many Table objects uses this */
 
477
  uint32_t _ref_count;       /* How many Table objects uses this */
490
478
 
491
479
public:
492
480
  uint32_t getTableCount() const
493
481
  {
494
 
    return ref_count;
 
482
    return _ref_count;
495
483
  }
496
484
 
497
485
  void incrementTableCount()
498
486
  {
499
487
    lock();
500
 
    ref_count++;
 
488
    _ref_count++;
501
489
    unlock();
502
490
  }
503
491
 
 
492
  uint32_t decrementTableCount()
 
493
  {
 
494
    return --_ref_count;
 
495
  }
 
496
 
504
497
  uint32_t null_bytes;
505
498
  uint32_t last_null_bit_pos;
506
 
  uint32_t fields;                              /* Number of fields */
 
499
private:
 
500
  uint32_t _field_size;                         /* Number of fields */
 
501
 
 
502
public:
 
503
  void setFieldSize(uint32_t arg)
 
504
  {
 
505
    _field_size= arg;
 
506
  }
507
507
 
508
508
  uint32_t sizeFields() const
509
509
  {
510
 
    return fields;
 
510
    return _field_size;
511
511
  }
512
512
 
513
513
  uint32_t rec_buff_length;                 /* Size of table->record[] buffer */
524
524
  uint32_t blob_fields;                 /* number of blob fields */
525
525
private:
526
526
  bool has_variable_width;                  /* number of varchar fields */
 
527
 
527
528
public:
528
529
  bool hasVariableWidth() const
529
530
  {
566
567
  uint32_t next_number_keypart;             /* autoinc keypart number in a key */
567
568
  uint32_t error, open_errno, errarg;       /* error from open_table_def() */
568
569
 
 
570
private:
569
571
  uint8_t blob_ptr_size;                        /* 4 or 8 */
 
572
 
 
573
public:
 
574
  uint8_t sizeBlobPtr() const
 
575
  {
 
576
    return blob_ptr_size;
 
577
  }
 
578
 
570
579
  bool db_low_byte_first;               /* Portable row format */
571
580
 
572
581
  /*
580
589
    event_observers is a class containing all the event plugins that have 
581
590
    registered an interest in this table.
582
591
  */
583
 
  private:
584
 
  plugin::EventObserverList *event_observers;
585
 
  public:
586
 
  plugin::EventObserverList *getTableObservers() 
 
592
  virtual plugin::EventObserverList *getTableObservers() 
587
593
  { 
588
 
    return event_observers;
 
594
    return NULL;
589
595
  }
590
596
  
591
 
  void setTableObservers(plugin::EventObserverList *observers) 
592
 
  { 
593
 
    event_observers= observers;
594
 
  }
 
597
  virtual void setTableObservers(plugin::EventObserverList *) 
 
598
  { }
595
599
  
596
600
  /*
597
601
    Set share's identifier information.
602
606
    NOTES
603
607
  */
604
608
 
605
 
  void setIdentifier(const TableIdentifier &identifier_arg);
 
609
  void setIdentifier(const identifier::Table &identifier_arg);
606
610
 
607
611
  /*
608
612
    Initialize share for temporary tables
623
627
private:
624
628
  void init(const char *new_table_name,
625
629
            const char *new_path);
626
 
public:
627
630
 
 
631
protected:
628
632
  void open_table_error(int pass_error, int db_errno, int pass_errarg);
629
633
 
630
 
  static void release(TableShare *share);
631
 
  static void release(TableShare::shared_ptr &share);
632
 
  static void release(const TableIdentifier &identifier);
 
634
public:
 
635
 
633
636
  static TableShare::shared_ptr getShareCreate(Session *session, 
634
 
                                               const TableIdentifier &identifier,
 
637
                                               const identifier::Table &identifier,
635
638
                                               int &error);
636
639
 
637
640
  friend std::ostream& operator<<(std::ostream& output, const TableShare &share)
649
652
    return output;  // for multiple << operators.
650
653
  }
651
654
 
652
 
  Field *make_field(unsigned char *ptr,
 
655
protected:
 
656
  friend class drizzled::table::Singular;
 
657
 
 
658
  Field *make_field(const message::Table::Field &pfield,
 
659
                    unsigned char *ptr,
653
660
                    uint32_t field_length,
654
661
                    bool is_nullable,
655
662
                    unsigned char *null_pos,
656
663
                    unsigned char null_bit,
657
664
                    uint8_t decimals,
658
665
                    enum_field_types field_type,
659
 
                    const CHARSET_INFO * field_charset,
 
666
                    const charset_info_st * field_charset,
660
667
                    Field::utype unireg_check,
661
668
                    TYPELIB *interval,
662
669
                    const char *field_name);
663
670
 
664
 
  int open_table_def(Session& session, const TableIdentifier &identifier);
 
671
  Field *make_field(const message::Table::Field &pfield,
 
672
                    unsigned char *ptr,
 
673
                    uint32_t field_length,
 
674
                    bool is_nullable,
 
675
                    unsigned char *null_pos,
 
676
                    unsigned char null_bit,
 
677
                    uint8_t decimals,
 
678
                    enum_field_types field_type,
 
679
                    const charset_info_st * field_charset,
 
680
                    Field::utype unireg_check,
 
681
                    TYPELIB *interval,
 
682
                    const char *field_name, 
 
683
                    bool is_unsigned);
 
684
 
 
685
public:
 
686
  int open_table_def(Session& session, const identifier::Table &identifier);
665
687
 
666
688
  int open_table_from_share(Session *session,
667
 
                            const TableIdentifier &identifier,
 
689
                            const identifier::Table &identifier,
668
690
                            const char *alias,
669
691
                            uint32_t db_stat, uint32_t ha_open_flags,
670
692
                            Table &outparam);
673
695
                                  const char *alias,
674
696
                                  uint32_t db_stat,
675
697
                                  Table &outparam);
676
 
  int open_table_cursor_inner(const TableIdentifier &identifier,
 
698
  int open_table_cursor_inner(const identifier::Table &identifier,
677
699
                              uint32_t db_stat, uint32_t ha_open_flags,
678
700
                              Table &outparam,
679
701
                              bool &error_reported);
680
702
public:
681
 
  int parse_table_proto(Session& session, message::Table &table);
682
 
private:
683
 
  int inner_parse_table_proto(Session& session, message::Table &table);
 
703
  bool parse_table_proto(Session& session, const message::Table &table);
 
704
 
 
705
  virtual bool is_replicated() const
 
706
  {
 
707
    return false;
 
708
  }
684
709
};
685
710
 
686
711
} /* namespace drizzled */
687
712
 
688
 
#endif /* DRIZZLED_DEFINITION_TABLE_H */