~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

Use List::begin()

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