~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/definition/table.h

  • Committer: Brian Aker
  • Date: 2010-12-18 10:14:05 UTC
  • mfrom: (2008.1.3 clean)
  • Revision ID: brian@tangent.org-20101218101405-qjbse29shi9coklg
Merge of user identifier work

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, Inc.
 
4
 *  Copyright (C) 2009 Sun Microsystems
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_INSTANCE_BASE_H
27
 
#define DRIZZLED_TABLE_INSTANCE_BASE_H
 
26
#ifndef DRIZZLED_DEFINITION_TABLE_H
 
27
#define DRIZZLED_DEFINITION_TABLE_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
 
#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
 
 
 
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"
48
42
 
49
43
namespace drizzled
50
44
{
51
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");
52
52
const static std::string NO_PROTOBUFFER_AVAILABLE("NO PROTOBUFFER AVAILABLE");
53
53
 
54
54
namespace plugin
55
55
{
56
56
class EventObserverList;
57
 
class StorageEngine;
58
 
}
59
 
 
60
 
namespace table {
61
 
class Singular;
62
57
}
63
58
 
64
59
class Field_blob;
71
66
  typedef boost::shared_ptr<TableShare> shared_ptr;
72
67
  typedef std::vector <shared_ptr> vector;
73
68
 
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,
 
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,
82
77
             char *path_arg= NULL, uint32_t path_length_arg= 0); // Shares for cache
83
78
 
84
 
  virtual ~TableShare();
 
79
  ~TableShare();
85
80
 
86
81
private:
87
82
  /** Category of this table. */
88
83
  enum_table_category table_category;
89
84
 
90
85
public:
 
86
 
91
87
  bool isTemporaryCategory() const
92
88
  {
93
89
    return (table_category == TABLE_CATEGORY_TEMPORARY);
100
96
 
101
97
  /* The following is copied to each Table on OPEN */
102
98
  typedef std::vector<Field *> Fields;
103
 
 
104
99
private:
105
 
  Fields _fields;
106
 
 
 
100
  Fields field;
107
101
public:
108
102
  const Fields getFields() const
109
103
  {
110
 
    return _fields;
111
 
  }
112
 
 
113
 
  Fields getFields()
114
 
  {
115
 
    return _fields;
 
104
    return field;
116
105
  }
117
106
 
118
107
  Field ** getFields(bool)
119
108
  {
120
 
    return &_fields[0];
 
109
    return &field[0];
121
110
  }
122
111
 
123
112
  void setFields(uint32_t arg)
124
113
  {
125
 
    _fields.resize(arg);
 
114
    field.resize(arg);
126
115
  }
127
116
 
128
117
  uint32_t positionFields(Field **arg) const
129
118
  {
130
 
    return (arg - (Field **)&_fields[0]);
 
119
    return (arg - (Field **)&field[0]);
131
120
  }
132
121
 
133
122
  void pushField(Field *arg)
134
123
  {
135
 
    _field_size++;
136
 
    _fields.push_back(arg);
 
124
    fields++;
 
125
    field.push_back(arg);
137
126
  }
138
127
 
 
128
 
139
129
  Field **found_next_number_field;
140
 
 
141
130
private:
142
131
  Field *timestamp_field;               /* Used only during open */
143
 
 
144
132
public:
145
133
 
146
134
  Field *getTimestampField() const               /* Used only during open */
156
144
 
157
145
private:
158
146
  KeyInfo  *key_info;                   /* data of keys in database */
159
 
 
160
147
public:
161
148
  KeyInfo &getKeyInfo(uint32_t arg) const
162
149
  {
164
151
  }
165
152
  std::vector<uint>     blob_field;                     /* Index to blobs in Field arrray*/
166
153
 
167
 
private:
168
154
  /* hash of field names (contains pointers to elements of field array) */
 
155
private:
169
156
  typedef boost::unordered_map < std::string, Field **, util::insensitive_hash, util::insensitive_equal_to> FieldMap;
170
157
  typedef std::pair< std::string, Field ** > FieldMapPair;
171
158
  FieldMap name_hash; /* hash of field names */
172
 
 
173
159
public:
174
160
  size_t getNamedFieldSize() const
175
161
  {
212
198
                   arg.begin(), ::toupper);
213
199
    _keynames.push_back(arg);
214
200
  }
215
 
 
216
201
public:
217
202
  bool doesKeyNameExist(const char *name_arg, uint32_t name_length, uint32_t &position) const
218
203
  {
240
225
private:
241
226
  std::vector<TYPELIB> intervals;                       /* pointer to interval info */
242
227
 
243
 
public:
244
 
  virtual void lock()
245
 
  { }
246
 
 
247
 
  virtual void unlock()
248
 
  { }
249
 
 
250
 
private:
 
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
 
251
242
  std::vector<unsigned char> default_values;            /* row with default values */
252
 
 
253
243
public:
254
244
  // @note This needs to be made to be const in the future
255
245
  unsigned char *getDefaultValues()
261
251
    default_values.resize(arg);
262
252
  }
263
253
 
264
 
  const charset_info_st *table_charset; /* Default charset of string fields */
 
254
  const CHARSET_INFO *table_charset; /* Default charset of string fields */
265
255
 
266
256
  boost::dynamic_bitset<> all_set;
267
257
 
276
266
    To ensure this one can use set_table_cache() methods.
277
267
  */
278
268
private:
279
 
  identifier::Table::Key private_key_for_cache; // This will not exist in the final design.
 
269
  TableIdentifier::Key private_key_for_cache; // This will not exist in the final design.
280
270
  std::vector<char> private_normalized_path; // This will not exist in the final design.
281
271
  LEX_STRING db;                        /* Pointer to db */
282
272
  LEX_STRING table_name;                /* Table name (for open) */
283
273
  LEX_STRING path;      /* Path to table (from datadir) */
284
274
  LEX_STRING normalized_path;           /* unpack_filename(path) */
285
 
 
286
275
public:
287
276
 
288
277
  const char *getNormalizedPath() const
295
284
    return path.str;
296
285
  }
297
286
 
298
 
  const identifier::Table::Key& getCacheKey() const // This should never be called when we aren't looking at a cache.
 
287
  const TableIdentifier::Key& getCacheKey() const // This should never be called when we aren't looking at a cache.
299
288
  {
300
289
    assert(private_key_for_cache.size());
301
290
    return private_key_for_cache;
318
307
    normalized_path.str= str_arg;
319
308
    normalized_path.length= size_arg;
320
309
  }
321
 
 
322
310
public:
323
311
 
324
312
  const char *getTableName() const
356
344
 
357
345
private:
358
346
  uint64_t   version;
359
 
 
360
347
public:
361
348
  uint64_t getVersion() const
362
349
  {
363
350
    return version;
364
351
  }
365
352
 
366
 
  void refreshVersion();
 
353
  void refreshVersion()
 
354
  {
 
355
   version= refresh_version;
 
356
  }
367
357
 
368
358
  void resetVersion()
369
359
  {
370
360
    version= 0;
371
361
  }
372
362
 
373
 
private:
374
363
  uint32_t   timestamp_offset;          /* Set to offset+1 of record */
375
 
 
376
 
  uint32_t reclength;                   /* Recordlength */
377
 
  uint32_t stored_rec_length;         /* Stored record length*/
378
 
 
 
364
private:
 
365
  uint32_t   reclength;                 /* Recordlength */
379
366
public:
380
 
  uint32_t sizeStoredRecord() const
381
 
  {
382
 
    return stored_rec_length;
383
 
  }
 
367
  uint32_t   stored_rec_length;         /* Stored record length*/
384
368
 
385
369
  uint32_t getRecordLength() const
386
370
  {
395
379
  const Field_blob *getBlobFieldAt(uint32_t arg) const
396
380
  {
397
381
    if (arg < blob_fields)
398
 
      return (Field_blob*) _fields[blob_field[arg]];
 
382
      return (Field_blob*) field[blob_field[arg]];
399
383
 
400
384
    return NULL;
401
385
  }
404
388
  /* Max rows is a hint to HEAP during a create tmp table */
405
389
  uint64_t max_rows;
406
390
 
407
 
  boost::scoped_ptr<message::Table> _table_message;
408
 
 
 
391
  message::Table *table_proto;
409
392
public:
 
393
 
410
394
  /*
411
 
    @note Without a _table_message, we assume we are building a STANDARD table.
 
395
    @note Without a table_proto, we assume we are building a STANDARD table.
412
396
    This will be modified once we use Identifiers in the Share itself.
413
397
  */
414
398
  message::Table::TableType getTableType() const
415
399
  {
416
 
    return getTableMessage() ? getTableMessage()->type() : message::Table::STANDARD;
 
400
    return table_proto ? table_proto->type() : message::Table::STANDARD;
417
401
  }
418
402
 
419
403
  const std::string &getTableTypeAsString() const
420
404
  {
421
 
    if (getTableMessage())
422
 
      return message::type(getTableMessage()->type());
423
 
 
424
 
    return NO_PROTOBUFFER_AVAILABLE;
 
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
    }
425
424
  }
426
425
 
427
426
  /* This is only used in one location currently */
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);
 
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;
443
436
  }
444
437
 
445
438
  inline bool hasComment() const
446
439
  {
447
 
    return (getTableMessage()) ?  getTableMessage()->options().has_comment() : false; 
 
440
    return (table_proto) ?  table_proto->options().has_comment() : false; 
448
441
  }
449
442
 
450
443
  inline const char *getComment()
451
444
  {
452
 
    return (getTableMessage() && getTableMessage()->has_options()) ?  getTableMessage()->options().comment().c_str() : NULL; 
 
445
    return (table_proto && table_proto->has_options()) ?  table_proto->options().comment().c_str() : NULL; 
453
446
  }
454
447
 
455
448
  inline uint32_t getCommentLength() const
456
449
  {
457
 
    return (getTableMessage()) ? getTableMessage()->options().comment().length() : 0; 
 
450
    return (table_proto) ? table_proto->options().comment().length() : 0; 
458
451
  }
459
452
 
460
453
  inline uint64_t getMaxRows() const
484
477
  }
485
478
 
486
479
private:
487
 
  identifier::Table::Type tmp_table;
 
480
  TableIdentifier::Type tmp_table;
488
481
public:
489
482
 
490
 
  identifier::Table::Type getType() const
 
483
  TableIdentifier::Type getType() const
491
484
  {
492
485
    return tmp_table;
493
486
  }
494
487
 
495
488
private:
496
 
  uint32_t _ref_count;       /* How many Table objects uses this */
 
489
  uint32_t ref_count;       /* How many Table objects uses this */
497
490
 
498
491
public:
499
492
  uint32_t getTableCount() const
500
493
  {
501
 
    return _ref_count;
 
494
    return ref_count;
502
495
  }
503
496
 
504
497
  void incrementTableCount()
505
498
  {
506
499
    lock();
507
 
    _ref_count++;
 
500
    ref_count++;
508
501
    unlock();
509
502
  }
510
503
 
511
 
  uint32_t decrementTableCount()
512
 
  {
513
 
    return --_ref_count;
514
 
  }
515
 
 
516
504
  uint32_t null_bytes;
517
505
  uint32_t last_null_bit_pos;
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
  uint32_t fields;                              /* Number of fields */
526
507
 
527
508
  uint32_t sizeFields() const
528
509
  {
529
 
    return _field_size;
 
510
    return fields;
530
511
  }
531
512
 
532
513
  uint32_t rec_buff_length;                 /* Size of table->record[] buffer */
543
524
  uint32_t blob_fields;                 /* number of blob fields */
544
525
private:
545
526
  bool has_variable_width;                  /* number of varchar fields */
546
 
 
547
527
public:
548
528
  bool hasVariableWidth() const
549
529
  {
586
566
  uint32_t next_number_keypart;             /* autoinc keypart number in a key */
587
567
  uint32_t error, open_errno, errarg;       /* error from open_table_def() */
588
568
 
589
 
private:
590
569
  uint8_t blob_ptr_size;                        /* 4 or 8 */
591
 
 
592
 
public:
593
 
  uint8_t sizeBlobPtr() const
594
 
  {
595
 
    return blob_ptr_size;
596
 
  }
597
 
 
598
570
  bool db_low_byte_first;               /* Portable row format */
599
571
 
600
572
  /*
608
580
    event_observers is a class containing all the event plugins that have 
609
581
    registered an interest in this table.
610
582
  */
611
 
  virtual plugin::EventObserverList *getTableObservers() 
 
583
  private:
 
584
  plugin::EventObserverList *event_observers;
 
585
  public:
 
586
  plugin::EventObserverList *getTableObservers() 
612
587
  { 
613
 
    return NULL;
 
588
    return event_observers;
614
589
  }
615
590
  
616
 
  virtual void setTableObservers(plugin::EventObserverList *) 
617
 
  { }
 
591
  void setTableObservers(plugin::EventObserverList *observers) 
 
592
  { 
 
593
    event_observers= observers;
 
594
  }
618
595
  
619
596
  /*
620
597
    Set share's identifier information.
625
602
    NOTES
626
603
  */
627
604
 
628
 
  void setIdentifier(const identifier::Table &identifier_arg);
 
605
  void setIdentifier(const TableIdentifier &identifier_arg);
629
606
 
630
607
  /*
631
608
    Initialize share for temporary tables
646
623
private:
647
624
  void init(const char *new_table_name,
648
625
            const char *new_path);
 
626
public:
649
627
 
650
 
protected:
651
628
  void open_table_error(int pass_error, int db_errno, int pass_errarg);
652
629
 
653
 
public:
654
 
 
 
630
  static void release(TableShare *share);
 
631
  static void release(TableShare::shared_ptr &share);
 
632
  static void release(const TableIdentifier &identifier);
655
633
  static TableShare::shared_ptr getShareCreate(Session *session, 
656
 
                                               const identifier::Table &identifier,
 
634
                                               const TableIdentifier &identifier,
657
635
                                               int &error);
658
636
 
659
637
  friend std::ostream& operator<<(std::ostream& output, const TableShare &share)
671
649
    return output;  // for multiple << operators.
672
650
  }
673
651
 
674
 
protected:
675
 
  friend class drizzled::table::Singular;
676
 
 
677
 
  Field *make_field(const message::Table::Field &pfield,
678
 
                    unsigned char *ptr,
 
652
  Field *make_field(unsigned char *ptr,
679
653
                    uint32_t field_length,
680
654
                    bool is_nullable,
681
655
                    unsigned char *null_pos,
682
656
                    unsigned char null_bit,
683
657
                    uint8_t decimals,
684
658
                    enum_field_types field_type,
685
 
                    const charset_info_st * field_charset,
 
659
                    const CHARSET_INFO * field_charset,
686
660
                    Field::utype unireg_check,
687
661
                    TYPELIB *interval,
688
662
                    const char *field_name);
689
663
 
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);
 
664
  int open_table_def(Session& session, const TableIdentifier &identifier);
706
665
 
707
666
  int open_table_from_share(Session *session,
708
 
                            const identifier::Table &identifier,
 
667
                            const TableIdentifier &identifier,
709
668
                            const char *alias,
710
669
                            uint32_t db_stat, uint32_t ha_open_flags,
711
670
                            Table &outparam);
714
673
                                  const char *alias,
715
674
                                  uint32_t db_stat,
716
675
                                  Table &outparam);
717
 
  int open_table_cursor_inner(const identifier::Table &identifier,
 
676
  int open_table_cursor_inner(const TableIdentifier &identifier,
718
677
                              uint32_t db_stat, uint32_t ha_open_flags,
719
678
                              Table &outparam,
720
679
                              bool &error_reported);
721
680
public:
722
 
  bool parse_table_proto(Session& session, message::Table &table);
723
 
 
724
 
  virtual bool replicate() const
725
 
  {
726
 
    return false;
727
 
  }
 
681
  int parse_table_proto(Session& session, message::Table &table);
 
682
private:
 
683
  int inner_parse_table_proto(Session& session, message::Table &table);
728
684
};
729
685
 
730
686
} /* namespace drizzled */
731
687
 
732
 
#endif /* DRIZZLED_TABLE_INSTANCE_BASE_H */
 
688
#endif /* DRIZZLED_DEFINITION_TABLE_H */