Drizzled Public API Documentation

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