1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2009 Sun Microsystems
4
* Copyright (C) 2009 Sun Microsystems, Inc.
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.
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
31
31
#include <boost/unordered_map.hpp>
32
32
#include <boost/thread/condition_variable.hpp>
33
#include <boost/dynamic_bitset.hpp>
34
#include <boost/shared_ptr.hpp>
35
#include <boost/scoped_ptr.hpp>
34
37
#include "drizzled/typelib.h"
35
38
#include "drizzled/memory/root.h"
36
#include "drizzled/message/table.pb.h"
39
#include "drizzled/message.h"
37
40
#include "drizzled/util/string.h"
42
#include "drizzled/lex_string.h"
43
#include "drizzled/key_map.h"
45
#include "drizzled/table/cache.h"
47
#include <drizzled/field.h>
42
typedef boost::unordered_map< TableIdentifier::Key, TableShare *> TableDefinitionCache;
44
const static std::string STANDARD_STRING("STANDARD");
45
const static std::string TEMPORARY_STRING("TEMPORARY");
46
const static std::string INTERNAL_STRING("INTERNAL");
47
const static std::string FUNCTION_STRING("FUNCTION");
53
const static std::string NO_PROTOBUFFER_AVAILABLE("NO PROTOBUFFER AVAILABLE");
51
57
class EventObserverList;
58
69
typedef std::vector<std::string> StringVector;
60
TableShare(TableIdentifier::Type type_arg);
62
TableShare(TableIdentifier &identifier, const TableIdentifier::Key &key); // Used by placeholder
64
TableShare(const TableIdentifier &identifier); // Just used during createTable()
66
TableShare(TableIdentifier::Type type_arg,
67
TableIdentifier &identifier,
72
typedef boost::shared_ptr<TableShare> shared_ptr;
73
typedef std::vector <shared_ptr> vector;
75
TableShare(const identifier::Table::Type type_arg);
77
TableShare(const identifier::Table &identifier, const identifier::Table::Key &key); // Used by placeholder
79
TableShare(const identifier::Table &identifier); // Just used during createTable()
81
TableShare(const identifier::Table::Type type_arg,
82
const identifier::Table &identifier,
68
83
char *path_arg= NULL, uint32_t path_length_arg= 0); // Shares for cache
85
virtual ~TableShare();
73
88
/** Category of this table. */
74
89
enum_table_category table_category;
76
uint32_t open_count; /* Number of tables in open list */
79
92
bool isTemporaryCategory() const
81
94
return (table_category == TABLE_CATEGORY_TEMPORARY);
89
102
/* The following is copied to each Table on OPEN */
90
103
typedef std::vector<Field *> Fields;
94
109
const Fields getFields() const
99
119
Field ** getFields(bool)
104
124
void setFields(uint32_t arg)
109
129
uint32_t positionFields(Field **arg) const
111
return (arg - (Field **)&field[0]);
131
return (arg - (Field **)&_fields[0]);
114
134
void pushField(Field *arg)
117
field.push_back(arg);
137
_fields.push_back(arg);
121
140
Field **found_next_number_field;
123
143
Field *timestamp_field; /* Used only during open */
126
147
Field *getTimestampField() const /* Used only during open */
144
166
std::vector<uint> blob_field; /* Index to blobs in Field arrray*/
146
169
/* hash of field names (contains pointers to elements of field array) */
148
170
typedef boost::unordered_map < std::string, Field **, util::insensitive_hash, util::insensitive_equal_to> FieldMap;
149
171
typedef std::pair< std::string, Field ** > FieldMapPair;
150
172
FieldMap name_hash; /* hash of field names */
152
175
size_t getNamedFieldSize() const
191
213
arg.begin(), ::toupper);
192
214
_keynames.push_back(arg);
195
218
bool doesKeyNameExist(const char *name_arg, uint32_t name_length, uint32_t &position) const
197
std::string arg(name_arg, name_length);
198
std::transform(arg.begin(), arg.end(),
199
arg.begin(), ::toupper);
201
std::vector<std::string>::const_iterator iter= std::find(_keynames.begin(), _keynames.end(), arg);
203
if (iter == _keynames.end())
206
position= iter - _keynames.begin();
220
return doesKeyNameExist(std::string(name_arg, name_length), position);
211
223
bool doesKeyNameExist(std::string arg, uint32_t &position) const
230
242
std::vector<TYPELIB> intervals; /* pointer to interval info */
232
boost::mutex mutex; /* For locking the share */
233
boost::condition_variable cond; /* To signal that share is ready */
248
virtual void unlock()
248
252
std::vector<unsigned char> default_values; /* row with default values */
250
unsigned char * getDefaultValues()
255
// @note This needs to be made to be const in the future
256
unsigned char *getDefaultValues()
252
258
return &default_values[0];
256
262
default_values.resize(arg);
259
const CHARSET_INFO *table_charset; /* Default charset of string fields */
263
std::vector<my_bitmap_map> all_bitmap;
265
const charset_info_st *table_charset; /* Default charset of string fields */
267
boost::dynamic_bitset<> all_set;
267
270
Key which is used for looking-up table in table cache and in the list
268
271
of thread's temporary tables. Has the form of:
274
277
To ensure this one can use set_table_cache() methods.
277
TableIdentifier::Key private_key_for_cache; // This will not exist in the final design.
280
identifier::Table::Key private_key_for_cache; // This will not exist in the final design.
278
281
std::vector<char> private_normalized_path; // This will not exist in the final design.
279
282
LEX_STRING db; /* Pointer to db */
280
283
LEX_STRING table_name; /* Table name (for open) */
281
284
LEX_STRING path; /* Path to table (from datadir) */
282
285
LEX_STRING normalized_path; /* unpack_filename(path) */
285
289
const char *getNormalizedPath() const
352
359
uint64_t version;
354
362
uint64_t getVersion() const
359
void refreshVersion()
361
version= refresh_version;
367
void refreshVersion();
364
369
void resetVersion()
369
375
uint32_t timestamp_offset; /* Set to offset+1 of record */
371
uint32_t reclength; /* Recordlength */
377
uint32_t reclength; /* Recordlength */
378
uint32_t stored_rec_length; /* Stored record length*/
373
uint32_t stored_rec_length; /* Stored record length*/
381
uint32_t sizeStoredRecord() const
383
return stored_rec_length;
375
386
uint32_t getRecordLength() const
394
405
/* Max rows is a hint to HEAP during a create tmp table */
395
406
uint64_t max_rows;
397
message::Table *table_proto;
408
boost::scoped_ptr<message::Table> _table_message;
401
@note Without a table_proto, we assume we are building a STANDARD table.
412
@note Without a _table_message, we assume we are building a STANDARD table.
402
413
This will be modified once we use Identifiers in the Share itself.
404
415
message::Table::TableType getTableType() const
406
return table_proto ? table_proto->type() : message::Table::STANDARD;
417
return getTableMessage() ? getTableMessage()->type() : message::Table::STANDARD;
409
420
const std::string &getTableTypeAsString() const
411
switch (table_proto->type())
414
case message::Table::STANDARD:
415
return STANDARD_STRING;
416
case message::Table::TEMPORARY:
417
return TEMPORARY_STRING;
418
case message::Table::INTERNAL:
419
return INTERNAL_STRING;
420
case message::Table::FUNCTION:
421
return FUNCTION_STRING;
422
if (getTableMessage())
423
return message::type(getTableMessage()->type());
425
return NO_PROTOBUFFER_AVAILABLE;
425
428
/* This is only used in one location currently */
426
inline message::Table *getTableProto() const
431
inline void setTableProto(message::Table *arg)
433
assert(table_proto == NULL);
429
inline message::Table *getTableMessage() const
431
return _table_message.get();
434
void setTableMessage(const message::Table &arg)
436
assert(not getTableMessage());
437
_table_message.reset(new(std::nothrow) message::Table(arg));
440
const message::Table::Field &field(int32_t field_position) const
442
assert(getTableMessage());
443
return getTableMessage()->field(field_position);
437
446
inline bool hasComment() const
439
return (table_proto) ? table_proto->options().has_comment() : false;
448
return (getTableMessage()) ? getTableMessage()->options().has_comment() : false;
442
451
inline const char *getComment()
444
return (table_proto && table_proto->has_options()) ? table_proto->options().comment().c_str() : NULL;
453
return (getTableMessage() && getTableMessage()->has_options()) ? getTableMessage()->options().comment().c_str() : NULL;
447
456
inline uint32_t getCommentLength() const
449
return (table_proto) ? table_proto->options().comment().length() : 0;
458
return (getTableMessage()) ? getTableMessage()->options().comment().length() : 0;
452
461
inline uint64_t getMaxRows() const
479
TableIdentifier::Type tmp_table;
488
identifier::Table::Type tmp_table;
482
TableIdentifier::Type getType() const
491
identifier::Table::Type getType() const
484
493
return tmp_table;
488
uint32_t ref_count; /* How many Table objects uses this */
497
uint32_t _ref_count; /* How many Table objects uses this */
490
500
uint32_t getTableCount() const
495
505
void incrementTableCount()
512
uint32_t decrementTableCount()
502
517
uint32_t null_bytes;
503
518
uint32_t last_null_bit_pos;
504
uint32_t fields; /* Number of fields */
520
uint32_t _field_size; /* Number of fields */
523
void setFieldSize(uint32_t arg)
506
528
uint32_t sizeFields() const
511
533
uint32_t rec_buff_length; /* Size of table->record[] buffer */
520
542
uint32_t uniques; /* Number of UNIQUE index */
521
543
uint32_t null_fields; /* number of null fields */
522
544
uint32_t blob_fields; /* number of blob fields */
523
uint32_t timestamp_field_offset; /* Field number for timestamp field */
524
uint32_t varchar_fields; /* number of varchar fields */
546
bool has_variable_width; /* number of varchar fields */
549
bool hasVariableWidth() const
551
return has_variable_width; // We should calculate this.
553
void setVariableWidth()
555
has_variable_width= true;
525
557
uint32_t db_create_options; /* Create options from database */
526
558
uint32_t db_options_in_use; /* Options in use */
527
559
uint32_t db_record_offset; /* if HA_REC_IN_SEQ */
554
586
uint32_t next_number_key_offset; /* autoinc keypart offset in a key */
555
587
uint32_t next_number_keypart; /* autoinc keypart number in a key */
556
588
uint32_t error, open_errno, errarg; /* error from open_table_def() */
557
uint32_t column_bitmap_size;
559
591
uint8_t blob_ptr_size; /* 4 or 8 */
594
uint8_t sizeBlobPtr() const
596
return blob_ptr_size;
560
599
bool db_low_byte_first; /* Portable row format */
565
bool isNameLock() const
570
bool replace_with_name_lock;
573
bool waiting_on_cond; /* Protection against free */
575
bool isWaitingOnCondition()
577
return waiting_on_cond;
581
602
Set of keys in use, implemented as a Bitmap.
582
603
Excludes keys disabled by ALTER Table ... DISABLE KEYS.
588
609
event_observers is a class containing all the event plugins that have
589
610
registered an interest in this table.
592
plugin::EventObserverList *event_observers;
594
plugin::EventObserverList *getTableObservers()
612
virtual plugin::EventObserverList *getTableObservers()
596
return event_observers;
599
void setTableObservers(plugin::EventObserverList *observers)
601
event_observers= observers;
617
virtual void setTableObservers(plugin::EventObserverList *)
605
621
Set share's identifier information.
613
void setIdentifier(TableIdentifier &identifier_arg);
615
inline bool honor_global_locks()
617
return (table_category == TABLE_CATEGORY_USER);
629
void setIdentifier(const identifier::Table &identifier_arg);
622
632
Initialize share for temporary tables
638
648
void init(const char *new_table_name,
639
649
const char *new_path);
642
652
void open_table_error(int pass_error, int db_errno, int pass_errarg);
644
static void cacheStart(void);
645
static void release(TableShare *share);
646
static void release(TableIdentifier &identifier);
647
static const TableDefinitionCache &getCache();
648
static TableShare *getShare(TableIdentifier &identifier);
649
static TableShare *getShareCreate(Session *session,
650
TableIdentifier &identifier,
656
static TableShare::shared_ptr getShareCreate(Session *session,
657
const identifier::Table &identifier,
653
660
friend std::ostream& operator<<(std::ostream& output, const TableShare &share)
665
672
return output; // for multiple << operators.
668
Field *make_field(unsigned char *ptr,
676
friend class drizzled::table::Singular;
678
Field *make_field(const message::Table::Field &pfield,
669
680
uint32_t field_length,
670
681
bool is_nullable,
671
682
unsigned char *null_pos,
672
683
unsigned char null_bit,
673
684
uint8_t decimals,
674
685
enum_field_types field_type,
675
const CHARSET_INFO * field_charset,
686
const charset_info_st * field_charset,
676
687
Field::utype unireg_check,
677
688
TYPELIB *interval,
678
689
const char *field_name);
680
int open_table_def(Session& session, TableIdentifier &identifier);
691
Field *make_field(const message::Table::Field &pfield,
693
uint32_t field_length,
695
unsigned char *null_pos,
696
unsigned char null_bit,
698
enum_field_types field_type,
699
const charset_info_st * field_charset,
700
Field::utype unireg_check,
702
const char *field_name,
706
int open_table_def(Session& session, const identifier::Table &identifier);
682
708
int open_table_from_share(Session *session,
683
const TableIdentifier &identifier,
709
const identifier::Table &identifier,
684
710
const char *alias,
685
711
uint32_t db_stat, uint32_t ha_open_flags,
686
712
Table &outparam);
687
int parse_table_proto(Session& session, message::Table &table);
689
int inner_parse_table_proto(Session& session, message::Table &table);
714
int open_table_from_share_inner(Session *session,
718
int open_table_cursor_inner(const identifier::Table &identifier,
719
uint32_t db_stat, uint32_t ha_open_flags,
721
bool &error_reported);
723
bool parse_table_proto(Session& session, message::Table &table);
692
726
} /* namespace drizzled */
694
#endif /* DRIZZLED_TABLE_SHARE_H */
728
#endif /* DRIZZLED_TABLE_INSTANCE_BASE_H */