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, Inc.
4
* Copyright (C) 2009 Sun Microsystems
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_INSTANCE_BASE_H
27
#define DRIZZLED_TABLE_INSTANCE_BASE_H
26
#ifndef DRIZZLED_TABLE_SHARE_H
27
#define DRIZZLED_TABLE_SHARE_H
31
31
#include <boost/unordered_map.hpp>
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>
37
#include <drizzled/memory/root.h>
38
#include <drizzled/message.h>
39
#include <drizzled/util/string.h>
41
#include <drizzled/lex_string.h>
42
#include <drizzled/key_map.h>
44
#include <drizzled/table/cache.h>
46
#include <drizzled/field.h>
33
#include "drizzled/typelib.h"
34
#include "drizzled/memory/root.h"
35
#include "drizzled/message/table.pb.h"
36
#include "drizzled/util/string.h"
52
const static std::string NO_PROTOBUFFER_AVAILABLE("NO PROTOBUFFER AVAILABLE");
41
typedef boost::unordered_map< TableIdentifier::Key, TableShare *> TableDefinitionCache;
43
const static std::string STANDARD_STRING("STANDARD");
44
const static std::string TEMPORARY_STRING("TEMPORARY");
45
const static std::string INTERNAL_STRING("INTERNAL");
46
const static std::string FUNCTION_STRING("FUNCTION");
56
50
class EventObserverList;
68
57
typedef std::vector<std::string> StringVector;
71
typedef boost::shared_ptr<TableShare> shared_ptr;
72
typedef std::vector <shared_ptr> vector;
74
TableShare(const identifier::Table::Type type_arg);
76
TableShare(const identifier::Table &identifier, const identifier::Table::Key &key); // Used by placeholder
78
TableShare(const identifier::Table &identifier); // Just used during createTable()
80
TableShare(const identifier::Table::Type type_arg,
81
const identifier::Table &identifier,
59
TableShare(TableIdentifier::Type type_arg);
61
TableShare(TableIdentifier &identifier, const TableIdentifier::Key &key); // Used by placeholder
63
TableShare(const TableIdentifier &identifier); // Just used during createTable()
65
TableShare(TableIdentifier::Type type_arg,
66
TableIdentifier &identifier,
82
67
char *path_arg= NULL, uint32_t path_length_arg= 0); // Shares for cache
84
virtual ~TableShare();
87
72
/** Category of this table. */
88
73
enum_table_category table_category;
75
uint32_t open_count; /* Number of tables in open list */
91
78
bool isTemporaryCategory() const
93
80
return (table_category == TABLE_CATEGORY_TEMPORARY);
101
88
/* The following is copied to each Table on OPEN */
102
89
typedef std::vector<Field *> Fields;
108
93
const Fields getFields() const
118
98
Field ** getFields(bool)
123
103
void setFields(uint32_t arg)
128
108
uint32_t positionFields(Field **arg) const
130
return (arg - (Field **)&_fields[0]);
110
return (arg - (Field **)&field[0]);
133
113
void pushField(Field *arg)
136
_fields.push_back(arg);
116
field.push_back(arg);
139
120
Field **found_next_number_field;
142
122
Field *timestamp_field; /* Used only during open */
146
125
Field *getTimestampField() const /* Used only during open */
165
143
std::vector<uint> blob_field; /* Index to blobs in Field arrray*/
168
145
/* hash of field names (contains pointers to elements of field array) */
169
147
typedef boost::unordered_map < std::string, Field **, util::insensitive_hash, util::insensitive_equal_to> FieldMap;
170
148
typedef std::pair< std::string, Field ** > FieldMapPair;
171
149
FieldMap name_hash; /* hash of field names */
174
151
size_t getNamedFieldSize() const
212
190
arg.begin(), ::toupper);
213
191
_keynames.push_back(arg);
217
194
bool doesKeyNameExist(const char *name_arg, uint32_t name_length, uint32_t &position) const
219
return doesKeyNameExist(std::string(name_arg, name_length), position);
196
std::string arg(name_arg, name_length);
197
std::transform(arg.begin(), arg.end(),
198
arg.begin(), ::toupper);
200
std::vector<std::string>::const_iterator iter= std::find(_keynames.begin(), _keynames.end(), arg);
202
if (iter == _keynames.end())
205
position= iter - _keynames.begin();
222
210
bool doesKeyNameExist(std::string arg, uint32_t &position) const
241
229
std::vector<TYPELIB> intervals; /* pointer to interval info */
247
virtual void unlock()
232
pthread_mutex_t mutex; /* For locking the share */
233
pthread_cond_t cond; /* To signal that share is ready */
251
236
std::vector<unsigned char> default_values; /* row with default values */
254
// @note This needs to be made to be const in the future
255
unsigned char *getDefaultValues()
238
unsigned char * getDefaultValues()
257
240
return &default_values[0];
261
244
default_values.resize(arg);
264
const charset_info_st *table_charset; /* Default charset of string fields */
266
boost::dynamic_bitset<> all_set;
247
const CHARSET_INFO *table_charset; /* Default charset of string fields */
251
std::vector<my_bitmap_map> all_bitmap;
269
255
Key which is used for looking-up table in table cache and in the list
270
256
of thread's temporary tables. Has the form of:
276
262
To ensure this one can use set_table_cache() methods.
279
identifier::Table::Key private_key_for_cache; // This will not exist in the final design.
265
TableIdentifier::Key private_key_for_cache; // This will not exist in the final design.
280
266
std::vector<char> private_normalized_path; // This will not exist in the final design.
281
267
LEX_STRING db; /* Pointer to db */
282
268
LEX_STRING table_name; /* Table name (for open) */
283
269
LEX_STRING path; /* Path to table (from datadir) */
284
270
LEX_STRING normalized_path; /* unpack_filename(path) */
288
273
const char *getNormalizedPath() const
358
340
uint64_t version;
361
342
uint64_t getVersion() const
366
void refreshVersion();
347
void refreshVersion()
349
version= refresh_version;
368
352
void resetVersion()
374
357
uint32_t timestamp_offset; /* Set to offset+1 of record */
376
uint32_t reclength; /* Recordlength */
377
uint32_t stored_rec_length; /* Stored record length*/
359
uint32_t reclength; /* Recordlength */
380
uint32_t sizeStoredRecord() const
382
return stored_rec_length;
361
uint32_t stored_rec_length; /* Stored record length*/
385
363
uint32_t getRecordLength() const
404
382
/* Max rows is a hint to HEAP during a create tmp table */
405
383
uint64_t max_rows;
407
boost::scoped_ptr<message::Table> _table_message;
385
message::Table *table_proto;
411
@note Without a _table_message, we assume we are building a STANDARD table.
389
@note Without a table_proto, we assume we are building a STANDARD table.
412
390
This will be modified once we use Identifiers in the Share itself.
414
392
message::Table::TableType getTableType() const
416
return getTableMessage() ? getTableMessage()->type() : message::Table::STANDARD;
394
return table_proto ? table_proto->type() : message::Table::STANDARD;
419
397
const std::string &getTableTypeAsString() const
421
if (getTableMessage())
422
return message::type(getTableMessage()->type());
424
return NO_PROTOBUFFER_AVAILABLE;
399
switch (table_proto->type())
402
case message::Table::STANDARD:
403
return STANDARD_STRING;
404
case message::Table::TEMPORARY:
405
return TEMPORARY_STRING;
406
case message::Table::INTERNAL:
407
return INTERNAL_STRING;
408
case message::Table::FUNCTION:
409
return FUNCTION_STRING;
427
413
/* This is only used in one location currently */
428
inline message::Table *getTableMessage() const
430
return _table_message.get();
433
void setTableMessage(const message::Table &arg)
435
assert(not getTableMessage());
436
_table_message.reset(new(std::nothrow) message::Table(arg));
439
const message::Table::Field &field(int32_t field_position) const
441
assert(getTableMessage());
442
return getTableMessage()->field(field_position);
414
inline message::Table *getTableProto() const
419
inline void setTableProto(message::Table *arg)
421
assert(table_proto == NULL);
445
425
inline bool hasComment() const
447
return (getTableMessage()) ? getTableMessage()->options().has_comment() : false;
427
return (table_proto) ? table_proto->options().has_comment() : false;
450
430
inline const char *getComment()
452
return (getTableMessage() && getTableMessage()->has_options()) ? getTableMessage()->options().comment().c_str() : NULL;
432
return (table_proto && table_proto->has_options()) ? table_proto->options().comment().c_str() : NULL;
455
435
inline uint32_t getCommentLength() const
457
return (getTableMessage()) ? getTableMessage()->options().comment().length() : 0;
437
return (table_proto) ? table_proto->options().comment().length() : 0;
460
440
inline uint64_t getMaxRows() const
487
identifier::Table::Type tmp_table;
467
TableIdentifier::Type tmp_table;
490
identifier::Table::Type getType() const
470
TableIdentifier::Type getType() const
492
472
return tmp_table;
496
uint32_t _ref_count; /* How many Table objects uses this */
476
uint32_t ref_count; /* How many Table objects uses this */
499
478
uint32_t getTableCount() const
504
483
void incrementTableCount()
511
uint32_t decrementTableCount()
516
488
uint32_t null_bytes;
517
489
uint32_t last_null_bit_pos;
519
uint32_t _field_size; /* Number of fields */
522
void setFieldSize(uint32_t arg)
490
uint32_t fields; /* Number of fields */
527
492
uint32_t sizeFields() const
532
497
uint32_t rec_buff_length; /* Size of table->record[] buffer */
541
506
uint32_t uniques; /* Number of UNIQUE index */
542
507
uint32_t null_fields; /* number of null fields */
543
508
uint32_t blob_fields; /* number of blob fields */
545
bool has_variable_width; /* number of varchar fields */
548
bool hasVariableWidth() const
550
return has_variable_width; // We should calculate this.
552
void setVariableWidth()
554
has_variable_width= true;
509
uint32_t timestamp_field_offset; /* Field number for timestamp field */
510
uint32_t varchar_fields; /* number of varchar fields */
556
511
uint32_t db_create_options; /* Create options from database */
557
512
uint32_t db_options_in_use; /* Options in use */
558
513
uint32_t db_record_offset; /* if HA_REC_IN_SEQ */
585
540
uint32_t next_number_key_offset; /* autoinc keypart offset in a key */
586
541
uint32_t next_number_keypart; /* autoinc keypart number in a key */
587
542
uint32_t error, open_errno, errarg; /* error from open_table_def() */
543
uint32_t column_bitmap_size;
590
545
uint8_t blob_ptr_size; /* 4 or 8 */
593
uint8_t sizeBlobPtr() const
595
return blob_ptr_size;
598
546
bool db_low_byte_first; /* Portable row format */
551
bool isNameLock() const
556
bool replace_with_name_lock;
559
bool waiting_on_cond; /* Protection against free */
561
bool isWaitingOnCondition()
563
return waiting_on_cond;
601
567
Set of keys in use, implemented as a Bitmap.
602
568
Excludes keys disabled by ALTER Table ... DISABLE KEYS.
608
574
event_observers is a class containing all the event plugins that have
609
575
registered an interest in this table.
611
virtual plugin::EventObserverList *getTableObservers()
578
plugin::EventObserverList *event_observers;
580
plugin::EventObserverList *getTableObservers()
582
return event_observers;
616
virtual void setTableObservers(plugin::EventObserverList *)
585
void setTableObservers(plugin::EventObserverList *observers)
587
event_observers= observers;
620
591
Set share's identifier information.
628
void setIdentifier(const identifier::Table &identifier_arg);
599
void setIdentifier(TableIdentifier &identifier_arg);
601
inline bool honor_global_locks()
603
return (table_category == TABLE_CATEGORY_USER);
631
608
Initialize share for temporary tables
647
624
void init(const char *new_table_name,
648
625
const char *new_path);
651
628
void open_table_error(int pass_error, int db_errno, int pass_errarg);
655
static TableShare::shared_ptr getShareCreate(Session *session,
656
const identifier::Table &identifier,
630
static void cacheStart(void);
631
static void cacheStop(void);
632
static void release(TableShare *share);
633
static void release(TableIdentifier &identifier);
634
static const TableDefinitionCache &getCache();
635
static TableShare *getShare(TableIdentifier &identifier);
636
static TableShare *getShareCreate(Session *session,
637
TableIdentifier &identifier,
659
640
friend std::ostream& operator<<(std::ostream& output, const TableShare &share)
671
652
return output; // for multiple << operators.
675
friend class drizzled::table::Singular;
677
Field *make_field(const message::Table::Field &pfield,
655
Field *make_field(unsigned char *ptr,
679
656
uint32_t field_length,
680
657
bool is_nullable,
681
658
unsigned char *null_pos,
682
659
unsigned char null_bit,
683
660
uint8_t decimals,
684
661
enum_field_types field_type,
685
const charset_info_st * field_charset,
662
const CHARSET_INFO * field_charset,
686
663
Field::utype unireg_check,
687
664
TYPELIB *interval,
688
665
const char *field_name);
690
Field *make_field(const message::Table::Field &pfield,
692
uint32_t field_length,
694
unsigned char *null_pos,
695
unsigned char null_bit,
697
enum_field_types field_type,
698
const charset_info_st * field_charset,
699
Field::utype unireg_check,
701
const char *field_name,
705
int open_table_def(Session& session, const identifier::Table &identifier);
667
int open_table_def(Session& session, TableIdentifier &identifier);
707
669
int open_table_from_share(Session *session,
708
const identifier::Table &identifier,
670
const TableIdentifier &identifier,
709
671
const char *alias,
710
672
uint32_t db_stat, uint32_t ha_open_flags,
711
673
Table &outparam);
674
int parse_table_proto(Session& session, message::Table &table);
713
int open_table_from_share_inner(Session *session,
717
int open_table_cursor_inner(const identifier::Table &identifier,
718
uint32_t db_stat, uint32_t ha_open_flags,
720
bool &error_reported);
722
bool parse_table_proto(Session& session, message::Table &table);
724
virtual bool replicate() const
676
int inner_parse_table_proto(Session& session, message::Table &table);
730
679
} /* namespace drizzled */
732
#endif /* DRIZZLED_TABLE_INSTANCE_BASE_H */
681
#endif /* DRIZZLED_TABLE_SHARE_H */