drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
table/instance/base.h
1/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2009 Sun Microsystems, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/*
22 This class is shared between different table objects. There is one
23 instance of table share per one table in the database.
24*/
25
26#pragma once
27
28#include <string>
29
30#include <unordered_map>
31#include <boost/thread/condition_variable.hpp>
32#include <boost/dynamic_bitset.hpp>
33#include <memory>
34#include <boost/scoped_ptr.hpp>
35
37#include <drizzled/message.h>
38#include <drizzled/util/string.h>
39#include <drizzled/key_map.h>
40#include <drizzled/field.h>
41#include <drizzled/util/find_ptr.h>
42#include <drizzled/key_info.h>
43
44namespace drizzled {
45
46const static std::string NO_PROTOBUFFER_AVAILABLE("NO PROTOBUFFER AVAILABLE");
47
48class TableShare
49{
50 typedef std::vector<std::string> StringVector;
51
52public:
53 typedef std::shared_ptr<TableShare> shared_ptr;
54 typedef std::vector <shared_ptr> vector;
55
56 TableShare(const identifier::Table::Type type_arg);
57
58 TableShare(const identifier::Table &identifier, const identifier::Table::Key &key); // Used by placeholder
59
60 TableShare(const identifier::Table &identifier); // Just used during createTable()
61
62 TableShare(const identifier::Table::Type type_arg,
63 const identifier::Table &identifier,
64 const char *path_arg= NULL, uint32_t path_length_arg= 0); // Shares for cache
65
66 virtual ~TableShare();
67
68private:
71
72public:
73 bool isTemporaryCategory() const
74 {
76 }
77
78 void setTableCategory(enum_table_category arg)
79 {
80 table_category= arg;
81 }
82
83 /* The following is copied to each Table on OPEN */
84 typedef std::vector<Field *> Fields;
85
86private:
87 Fields _fields;
88
89public:
90 const Fields getFields() const
91 {
92 return _fields;
93 }
94
95 Fields getFields()
96 {
97 return _fields;
98 }
99
100 Field ** getFields(bool)
101 {
102 return &_fields[0];
103 }
104
105 void setFields(uint32_t arg)
106 {
107 _fields.resize(arg);
108 }
109
110 uint32_t positionFields(Field **arg) const
111 {
112 return (arg - (Field **)&_fields[0]);
113 }
114
115 void pushField(Field *arg)
116 {
117 _field_size++;
118 _fields.push_back(arg);
119 }
120
121 Field **found_next_number_field;
122
123private:
124 Field *timestamp_field; /* Used only during open */
125
126public:
127
128 Field *getTimestampField() const /* Used only during open */
129 {
130 return timestamp_field;
131 }
132
133 void setTimestampField(Field *arg) /* Used only during open */
134 {
135 timestamp_field= arg;
136 }
137
138
139private:
140 KeyInfo *key_info; /* data of keys in database */
141
142public:
143 KeyInfo &getKeyInfo(uint32_t arg) const
144 {
145 return key_info[arg];
146 }
147 std::vector<uint> blob_field; /* Index to blobs in Field arrray*/
148
149private:
150 /* hash of field names (contains pointers to elements of field array) */
151 typedef std::unordered_map<std::string, Field**, util::insensitive_hash, util::insensitive_equal_to> FieldMap;
152 FieldMap name_hash; /* hash of field names */
153
154public:
155 size_t getNamedFieldSize() const
156 {
157 return name_hash.size();
158 }
159
160 Field **getNamedField(const std::string &arg)
161 {
162 return find_ptr2(name_hash, arg);
163 }
164
165private:
166 memory::Root mem_root;
167
168 unsigned char* alloc(size_t arg)
169 {
170 return mem_root.alloc(arg);
171 }
172
173 memory::Root& mem()
174 {
175 return mem_root;
176 }
177
178 typedef std::vector<std::string> keynames_t;
179
180 keynames_t _keynames;
181
182 void addKeyName(const std::string& arg)
183 {
184 _keynames.push_back(boost::to_upper_copy(arg));
185 }
186
187public:
188 uint32_t doesKeyNameExist(const std::string& arg) const
189 {
190 keynames_t::const_iterator it= find(_keynames.begin(), _keynames.end(), boost::to_upper_copy(arg));
191 return it == _keynames.end() ? UINT32_MAX : it - _keynames.begin(); // historical, required for finding primary key from unique
192 }
193
194private:
195 std::vector<TYPELIB> intervals; /* pointer to interval info */
196
197public:
198 virtual void lock()
199 { }
200
201 virtual void unlock()
202 { }
203
204private:
205 std::vector<unsigned char> default_values; /* row with default values */
206
207public:
208 // @note This needs to be made to be const in the future
209 unsigned char *getDefaultValues()
210 {
211 return &default_values[0];
212 }
213 void resizeDefaultValues(size_t arg)
214 {
215 default_values.resize(arg);
216 }
217
218 const charset_info_st *table_charset; /* Default charset of string fields */
219
220 boost::dynamic_bitset<> all_set;
221
222 /*
223 Key which is used for looking-up table in table cache and in the list
224 of thread's temporary tables. Has the form of:
225 "database_name\0table_name\0" + optional part for temporary tables.
226
227 Note that all three 'table_cache_key', 'db' and 'table_name' members
228 must be set (and be non-zero) for tables in table cache. They also
229 should correspond to each other.
230 To ensure this one can use set_table_cache() methods.
231 */
232private:
233 identifier::Table::Key private_key_for_cache; // This will not exist in the final design.
234 std::vector<char> private_normalized_path; // This will not exist in the final design.
235 drizzled::identifier::Table *table_identifier;
236 str_ref path; /* Path to table (from datadir) */
237 str_ref normalized_path; /* unpack_filename(path) */
238
239public:
240
241 const char *getNormalizedPath() const
242 {
243 return normalized_path.data();
244 }
245
246 const char *getPath() const
247 {
248 return path.data();
249 }
250
251 const identifier::Table::Key& getCacheKey() const // This should never be called when we aren't looking at a cache.
252 {
253 assert(private_key_for_cache.size());
254 return private_key_for_cache;
255 }
256
257 size_t getCacheKeySize() const
258 {
259 return private_key_for_cache.size();
260 }
261
262 str_ref getTableNameRef() const
263 {
264 return str_ref(table_identifier->getTableName());
265 }
266
267 const char *getTableName() const
268 {
269 return table_identifier->getTableName().c_str();
270 }
271
272 str_ref getSchemaNameRef() const
273 {
274 return str_ref(table_identifier->getSchemaName());
275 }
276
277 const char *getSchemaName() const
278 {
279 return table_identifier->getSchemaName().c_str();
280 }
281
282 const drizzled::identifier::Table &getTableIdentifier() const
283 {
284 return *table_identifier;
285 }
286
287 uint32_t block_size; /* create information */
288
289private:
290 uint64_t version;
291
292public:
293 uint64_t getVersion() const
294 {
295 return version;
296 }
297
298 void refreshVersion();
299
300 void resetVersion()
301 {
302 version= 0;
303 }
304
305private:
306 uint32_t timestamp_offset; /* Set to offset+1 of record */
307
308 uint32_t reclength; /* Recordlength */
309 uint32_t stored_rec_length; /* Stored record length*/
310
311public:
312 uint32_t sizeStoredRecord() const
313 {
314 return stored_rec_length;
315 }
316
317 uint32_t getRecordLength() const
318 {
319 return reclength;
320 }
321
322 void setRecordLength(uint32_t arg)
323 {
324 reclength= arg;
325 }
326
327 const Field_blob *getBlobFieldAt(uint32_t arg) const
328 {
329 if (arg < blob_fields)
330 return (Field_blob*) _fields[blob_field[arg]];
331
332 return NULL;
333 }
334
335private:
336 /* Max rows is a hint to HEAP during a create tmp table */
337 uint64_t max_rows;
338
339 boost::scoped_ptr<message::Table> _table_message;
340
341public:
342 /*
343 @note Without a _table_message, we assume we are building a STANDARD table.
344 This will be modified once we use Identifiers in the Share itself.
345 */
346 message::Table::TableType getTableType() const
347 {
348 return getTableMessage() ? getTableMessage()->type() : message::Table::STANDARD;
349 }
350
351 const std::string &getTableTypeAsString() const
352 {
353 if (getTableMessage())
354 return message::type(getTableMessage()->type());
355
356 return NO_PROTOBUFFER_AVAILABLE;
357 }
358
359 /* This is only used in one location currently */
360 inline message::Table *getTableMessage() const
361 {
362 return _table_message.get();
363 }
364
365 void setTableMessage(const message::Table &arg)
366 {
367 assert(not getTableMessage());
368 _table_message.reset(new message::Table(arg));
369 }
370
371 const message::Table::Field &field(int32_t field_position) const
372 {
373 assert(getTableMessage());
374 return getTableMessage()->field(field_position);
375 }
376
377 inline bool hasComment() const
378 {
379 return getTableMessage() ? getTableMessage()->options().has_comment() : false;
380 }
381
382 inline const char *getComment()
383 {
384 return (getTableMessage() && getTableMessage()->has_options()) ? getTableMessage()->options().comment().c_str() : NULL;
385 }
386
387 inline uint32_t getCommentLength() const
388 {
389 return (getTableMessage()) ? getTableMessage()->options().comment().length() : 0;
390 }
391
392 inline uint64_t getMaxRows() const
393 {
394 return max_rows;
395 }
396
397 inline void setMaxRows(uint64_t arg)
398 {
399 max_rows= arg;
400 }
401
406 bool fieldInPrimaryKey(Field *field) const;
407
408 plugin::StorageEngine *storage_engine; /* storage engine plugin */
409 inline plugin::StorageEngine *db_type() const /* table_type for handler */
410 {
411 return storage_engine;
412 }
413 inline plugin::StorageEngine *getEngine() const /* table_type for handler */
414 {
415 return storage_engine;
416 }
417
418private:
419 identifier::Table::Type tmp_table;
420public:
421
422 identifier::Table::Type getType() const
423 {
424 return tmp_table;
425 }
426
427private:
428 uint32_t _ref_count; /* How many Table objects uses this */
429
430public:
431 uint32_t getTableCount() const
432 {
433 return _ref_count;
434 }
435
436 void incrementTableCount()
437 {
438 lock();
439 _ref_count++;
440 unlock();
441 }
442
443 uint32_t decrementTableCount()
444 {
445 return --_ref_count;
446 }
447
448 uint32_t null_bytes;
449 uint32_t last_null_bit_pos;
450private:
451 uint32_t _field_size; /* Number of fields */
452
453public:
454 void setFieldSize(uint32_t arg)
455 {
456 _field_size= arg;
457 }
458
459 uint32_t sizeFields() const
460 {
461 return _field_size;
462 }
463
464 uint32_t rec_buff_length; /* Size of table->record[] buffer */
465 uint32_t keys;
466
467 uint32_t sizeKeys() const
468 {
469 return keys;
470 }
471 uint32_t key_parts;
472 uint32_t max_key_length, max_unique_length, total_key_length;
473 uint32_t uniques; /* Number of UNIQUE index */
474 uint32_t null_fields; /* number of null fields */
475 uint32_t blob_fields; /* number of blob fields */
476private:
477 bool has_variable_width; /* number of varchar fields */
478
479public:
480 bool hasVariableWidth() const
481 {
482 return has_variable_width; // We should calculate this.
483 }
484 void setVariableWidth()
485 {
486 has_variable_width= true;
487 }
488 uint32_t db_create_options; /* Create options from database */
489 uint32_t db_options_in_use; /* Options in use */
490 uint32_t db_record_offset; /* if HA_REC_IN_SEQ */
491 uint32_t rowid_field_offset; /* Field_nr +1 to rowid field */
501private:
502 uint32_t primary_key;
503public:
504
505 uint32_t getPrimaryKey() const
506 {
507 return primary_key;
508 }
509
510 bool hasPrimaryKey() const
511 {
512 return primary_key != MAX_KEY;
513 }
514
515 /* Index of auto-updated TIMESTAMP field in field array */
516 uint32_t next_number_index; /* autoincrement key number */
517 uint32_t next_number_key_offset; /* autoinc keypart offset in a key */
518 uint32_t next_number_keypart; /* autoinc keypart number in a key */
519 uint32_t error, open_errno, errarg; /* error from open_table_def() */
520
521private:
522 uint8_t blob_ptr_size; /* 4 or 8 */
523
524public:
525 uint8_t sizeBlobPtr() const
526 {
527 return blob_ptr_size;
528 }
529
530 bool db_low_byte_first; /* Portable row format */
531
532 /*
533 Set of keys in use, implemented as a Bitmap.
534 Excludes keys disabled by ALTER Table ... DISABLE KEYS.
535 */
536 key_map keys_in_use;
537 key_map keys_for_keyread;
538
539 /*
540 event_observers is a class containing all the event plugins that have
541 registered an interest in this table.
542 */
543 virtual plugin::EventObserverList *getTableObservers()
544 {
545 return NULL;
546 }
547
548 virtual void setTableObservers(plugin::EventObserverList *)
549 { }
550
551 /*
552 Set share's identifier information.
553
554 SYNOPSIS
555 setIdentifier()
556
557 NOTES
558 */
559
560 void setIdentifier(const identifier::Table &identifier_arg);
561
562 /*
563 Initialize share for temporary tables
564
565 SYNOPSIS
566 init()
567 share Share to fill
568 key Table_cache_key, as generated from create_table_def_key.
569 must start with db name.
570 key_length Length of key
571 table_name Table name
572 path Path to table (possible in lower case)
573
574 NOTES
575
576 */
577
578private:
579 void init(const char *new_table_name,
580 const char *new_path);
581
582protected:
583 void open_table_error(int pass_error, int db_errno, int pass_errarg);
584
585public:
586
587 static TableShare::shared_ptr getShareCreate(Session*, const identifier::Table&, int &error);
588
589 friend std::ostream& operator<<(std::ostream& output, const TableShare &share)
590 {
591 return output << "TableShare:(" << share.getSchemaNameRef() << ", " << share.getTableName() << ", " << share.getTableTypeAsString() << ", " << share.getPath() << ")";
592 }
593
594protected:
595 friend class drizzled::table::Singular;
596
597 Field *make_field(const message::Table::Field &pfield,
598 unsigned char *ptr,
599 uint32_t field_length,
600 bool is_nullable,
601 unsigned char *null_pos,
602 unsigned char null_bit,
603 uint8_t decimals,
604 enum_field_types field_type,
605 const charset_info_st * field_charset,
606 Field::utype unireg_check,
607 TYPELIB *interval,
608 const char *field_name);
609
610 Field *make_field(const message::Table::Field &pfield,
611 unsigned char *ptr,
612 uint32_t field_length,
613 bool is_nullable,
614 unsigned char *null_pos,
615 unsigned char null_bit,
616 uint8_t decimals,
617 enum_field_types field_type,
618 const charset_info_st * field_charset,
619 Field::utype unireg_check,
620 TYPELIB *interval,
621 const char *field_name,
622 bool is_unsigned);
623
624public:
625 int open_table_def(Session& session, const identifier::Table &identifier);
626
627 int open_table_from_share(Session *session,
628 const identifier::Table &identifier,
629 const char *alias,
630 uint32_t db_stat, uint32_t ha_open_flags,
631 Table &outparam);
632private:
633 int open_table_from_share_inner(Session *session,
634 const char *alias,
635 uint32_t db_stat,
636 Table &outparam);
637 int open_table_cursor_inner(const identifier::Table &identifier,
638 uint32_t db_stat, uint32_t ha_open_flags,
639 Table &outparam,
640 bool &error_reported);
641public:
642 bool parse_table_proto(Session& session, const message::Table &table);
643
644 virtual bool is_replicated() const
645 {
646 return false;
647 }
648};
649
650} /* namespace drizzled */
651
enum_table_category table_category
bool fieldInPrimaryKey(Field *field) const
Definition base.cc:202
TODO: Rename this file - func.h is stupid.
enum_table_category
Definition enum.h:94
@ TABLE_CATEGORY_TEMPORARY
Definition enum.h:111
Memory root declarations.