~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to drizzled/plugin/storage_engine.h

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-03-18 12:12:31 UTC
  • Revision ID: james.westby@ubuntu.com-20100318121231-k6g1xe6cshbwa0f8
Tags: upstream-2010.03.1347
ImportĀ upstreamĀ versionĀ 2010.03.1347

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Sun Microsystems
 
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; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
#ifndef DRIZZLED_PLUGIN_STORAGE_ENGINE_H
 
21
#define DRIZZLED_PLUGIN_STORAGE_ENGINE_H
 
22
 
 
23
 
 
24
#include <drizzled/definitions.h>
 
25
#include <drizzled/plugin.h>
 
26
#include <drizzled/handler_structs.h>
 
27
#include <drizzled/message/schema.pb.h>
 
28
#include <drizzled/message/table.pb.h>
 
29
#include "drizzled/plugin/plugin.h"
 
30
#include "drizzled/sql_string.h"
 
31
#include "drizzled/table_identifier.h"
 
32
#include "drizzled/cached_directory.h"
 
33
#include "drizzled/plugin/monitored_in_transaction.h"
 
34
 
 
35
#include "drizzled/hash.h"
 
36
 
 
37
#include <bitset>
 
38
#include <string>
 
39
#include <vector>
 
40
#include <set>
 
41
 
 
42
namespace drizzled
 
43
{
 
44
 
 
45
class TableList;
 
46
class Session;
 
47
class Cursor;
 
48
typedef struct st_hash HASH;
 
49
 
 
50
class TableShare;
 
51
typedef drizzle_lex_string LEX_STRING;
 
52
typedef bool (stat_print_fn)(Session *session, const char *type, uint32_t type_len,
 
53
                             const char *file, uint32_t file_len,
 
54
                             const char *status, uint32_t status_len);
 
55
 
 
56
/* Possible flags of a StorageEngine (there can be 32 of them) */
 
57
enum engine_flag_bits {
 
58
  HTON_BIT_ALTER_NOT_SUPPORTED,       // Engine does not support alter
 
59
  HTON_BIT_HIDDEN,                    // Engine does not appear in lists
 
60
  HTON_BIT_NOT_USER_SELECTABLE,
 
61
  HTON_BIT_TEMPORARY_NOT_SUPPORTED,   // Having temporary tables not supported
 
62
  HTON_BIT_TEMPORARY_ONLY,
 
63
  HTON_BIT_FILE_BASED, // use for check_lowercase_names
 
64
  HTON_BIT_HAS_DATA_DICTIONARY,
 
65
  HTON_BIT_DOES_TRANSACTIONS,
 
66
  HTON_BIT_STATS_RECORDS_IS_EXACT,
 
67
  HTON_BIT_NULL_IN_KEY,
 
68
  HTON_BIT_CAN_INDEX_BLOBS,
 
69
  HTON_BIT_PRIMARY_KEY_REQUIRED_FOR_POSITION,
 
70
  HTON_BIT_PRIMARY_KEY_IN_READ_INDEX,
 
71
  HTON_BIT_PARTIAL_COLUMN_READ,
 
72
  HTON_BIT_TABLE_SCAN_ON_INDEX,
 
73
  HTON_BIT_FAST_KEY_READ,
 
74
  HTON_BIT_NO_BLOBS,
 
75
  HTON_BIT_HAS_RECORDS,
 
76
  HTON_BIT_NO_AUTO_INCREMENT,
 
77
  HTON_BIT_DUPLICATE_POS,
 
78
  HTON_BIT_AUTO_PART_KEY,
 
79
  HTON_BIT_REQUIRE_PRIMARY_KEY,
 
80
  HTON_BIT_REQUIRES_KEY_COLUMNS_FOR_DELETE,
 
81
  HTON_BIT_PRIMARY_KEY_REQUIRED_FOR_DELETE,
 
82
  HTON_BIT_NO_PREFIX_CHAR_KEYS,
 
83
  HTON_BIT_HAS_CHECKSUM,
 
84
  HTON_BIT_SKIP_STORE_LOCK,
 
85
  HTON_BIT_SCHEMA_DICTIONARY,
 
86
  HTON_BIT_SIZE
 
87
};
 
88
 
 
89
static const std::bitset<HTON_BIT_SIZE> HTON_NO_FLAGS(0);
 
90
static const std::bitset<HTON_BIT_SIZE> HTON_ALTER_NOT_SUPPORTED(1 << HTON_BIT_ALTER_NOT_SUPPORTED);
 
91
static const std::bitset<HTON_BIT_SIZE> HTON_HIDDEN(1 << HTON_BIT_HIDDEN);
 
92
static const std::bitset<HTON_BIT_SIZE> HTON_NOT_USER_SELECTABLE(1 << HTON_BIT_NOT_USER_SELECTABLE);
 
93
static const std::bitset<HTON_BIT_SIZE> HTON_TEMPORARY_NOT_SUPPORTED(1 << HTON_BIT_TEMPORARY_NOT_SUPPORTED);
 
94
static const std::bitset<HTON_BIT_SIZE> HTON_TEMPORARY_ONLY(1 << HTON_BIT_TEMPORARY_ONLY);
 
95
static const std::bitset<HTON_BIT_SIZE> HTON_FILE_BASED(1 << HTON_BIT_FILE_BASED);
 
96
static const std::bitset<HTON_BIT_SIZE> HTON_HAS_DATA_DICTIONARY(1 << HTON_BIT_HAS_DATA_DICTIONARY);
 
97
static const std::bitset<HTON_BIT_SIZE> HTON_HAS_DOES_TRANSACTIONS(1 << HTON_BIT_DOES_TRANSACTIONS);
 
98
static const std::bitset<HTON_BIT_SIZE> HTON_STATS_RECORDS_IS_EXACT(1 << HTON_BIT_STATS_RECORDS_IS_EXACT);
 
99
static const std::bitset<HTON_BIT_SIZE> HTON_NULL_IN_KEY(1 << HTON_BIT_NULL_IN_KEY);
 
100
static const std::bitset<HTON_BIT_SIZE> HTON_CAN_INDEX_BLOBS(1 << HTON_BIT_CAN_INDEX_BLOBS);
 
101
static const std::bitset<HTON_BIT_SIZE> HTON_PRIMARY_KEY_REQUIRED_FOR_POSITION(1 << HTON_BIT_PRIMARY_KEY_REQUIRED_FOR_POSITION);
 
102
static const std::bitset<HTON_BIT_SIZE> HTON_PRIMARY_KEY_IN_READ_INDEX(1 << HTON_BIT_PRIMARY_KEY_IN_READ_INDEX);
 
103
static const std::bitset<HTON_BIT_SIZE> HTON_PARTIAL_COLUMN_READ(1 << HTON_BIT_PARTIAL_COLUMN_READ);
 
104
static const std::bitset<HTON_BIT_SIZE> HTON_TABLE_SCAN_ON_INDEX(1 << HTON_BIT_TABLE_SCAN_ON_INDEX);
 
105
static const std::bitset<HTON_BIT_SIZE> HTON_FAST_KEY_READ(1 << HTON_BIT_FAST_KEY_READ);
 
106
static const std::bitset<HTON_BIT_SIZE> HTON_NO_BLOBS(1 << HTON_BIT_NO_BLOBS);
 
107
static const std::bitset<HTON_BIT_SIZE> HTON_HAS_RECORDS(1 << HTON_BIT_HAS_RECORDS);
 
108
static const std::bitset<HTON_BIT_SIZE> HTON_NO_AUTO_INCREMENT(1 << HTON_BIT_NO_AUTO_INCREMENT);
 
109
static const std::bitset<HTON_BIT_SIZE> HTON_DUPLICATE_POS(1 << HTON_BIT_DUPLICATE_POS);
 
110
static const std::bitset<HTON_BIT_SIZE> HTON_AUTO_PART_KEY(1 << HTON_BIT_AUTO_PART_KEY);
 
111
static const std::bitset<HTON_BIT_SIZE> HTON_REQUIRE_PRIMARY_KEY(1 << HTON_BIT_REQUIRE_PRIMARY_KEY);
 
112
static const std::bitset<HTON_BIT_SIZE> HTON_REQUIRES_KEY_COLUMNS_FOR_DELETE(1 << HTON_BIT_REQUIRES_KEY_COLUMNS_FOR_DELETE);
 
113
static const std::bitset<HTON_BIT_SIZE> HTON_PRIMARY_KEY_REQUIRED_FOR_DELETE(1 << HTON_BIT_PRIMARY_KEY_REQUIRED_FOR_DELETE);
 
114
static const std::bitset<HTON_BIT_SIZE> HTON_NO_PREFIX_CHAR_KEYS(1 << HTON_BIT_NO_PREFIX_CHAR_KEYS);
 
115
static const std::bitset<HTON_BIT_SIZE> HTON_HAS_CHECKSUM(1 << HTON_BIT_HAS_CHECKSUM);
 
116
static const std::bitset<HTON_BIT_SIZE> HTON_SKIP_STORE_LOCK(1 << HTON_BIT_SKIP_STORE_LOCK);
 
117
static const std::bitset<HTON_BIT_SIZE> HTON_HAS_SCHEMA_DICTIONARY(1 << HTON_BIT_SCHEMA_DICTIONARY);
 
118
 
 
119
 
 
120
class Table;
 
121
class NamedSavepoint;
 
122
 
 
123
namespace plugin
 
124
{
 
125
 
 
126
typedef hash_map<std::string, StorageEngine *> EngineMap;
 
127
typedef std::vector<StorageEngine *> EngineVector;
 
128
 
 
129
typedef std::set<std::string> TableNameList;
 
130
typedef std::set<std::string> SchemaNameList;
 
131
 
 
132
extern const std::string UNKNOWN_STRING;
 
133
extern const std::string DEFAULT_DEFINITION_FILE_EXT;
 
134
 
 
135
/*
 
136
  StorageEngine is a singleton structure - one instance per storage engine -
 
137
  to provide access to storage engine functionality that works on the
 
138
  "global" level (unlike Cursor class that works on a per-table basis)
 
139
 
 
140
  usually StorageEngine instance is defined statically in ha_xxx.cc as
 
141
 
 
142
  static StorageEngine { ... } xxx_engine;
 
143
*/
 
144
class StorageEngine : public Plugin,
 
145
                      public MonitoredInTransaction
 
146
{
 
147
public:
 
148
  typedef uint64_t Table_flags;
 
149
 
 
150
private:
 
151
  const std::bitset<HTON_BIT_SIZE> flags; /* global Cursor flags */
 
152
 
 
153
  virtual void setTransactionReadWrite(Session& session);
 
154
 
 
155
  /*
 
156
   * Indicates to a storage engine the start of a
 
157
   * new SQL statement.
 
158
   */
 
159
  virtual void doStartStatement(Session *session)
 
160
  {
 
161
    (void) session;
 
162
  }
 
163
 
 
164
  /*
 
165
   * Indicates to a storage engine the end of
 
166
   * the current SQL statement in the supplied
 
167
   * Session.
 
168
   */
 
169
  virtual void doEndStatement(Session *session)
 
170
  {
 
171
    (void) session;
 
172
  }
 
173
 
 
174
protected:
 
175
  std::string table_definition_ext;
 
176
 
 
177
public:
 
178
  const std::string& getTableDefinitionFileExtension()
 
179
  {
 
180
    return table_definition_ext;
 
181
  }
 
182
 
 
183
private:
 
184
  std::vector<std::string> aliases;
 
185
 
 
186
public:
 
187
  const std::vector<std::string>& getAliases() const
 
188
  {
 
189
    return aliases;
 
190
  }
 
191
 
 
192
  void addAlias(std::string alias)
 
193
  {
 
194
    aliases.push_back(alias);
 
195
  }
 
196
 
 
197
protected:
 
198
 
 
199
  /**
 
200
    @brief
 
201
    Used as a protobuf storage currently by TEMP only engines.
 
202
  */
 
203
  typedef std::map <std::string, message::Table> ProtoCache;
 
204
  ProtoCache proto_cache;
 
205
  pthread_mutex_t proto_cache_mutex;
 
206
 
 
207
public:
 
208
 
 
209
  StorageEngine(const std::string name_arg,
 
210
                const std::bitset<HTON_BIT_SIZE> &flags_arg= HTON_NO_FLAGS);
 
211
 
 
212
  virtual ~StorageEngine();
 
213
 
 
214
  virtual int doGetTableDefinition(Session& session,
 
215
                                   const char *path,
 
216
                                   const char *db,
 
217
                                   const char *table_name,
 
218
                                   const bool is_tmp,
 
219
                                   message::Table *table_proto)
 
220
  {
 
221
    (void)session;
 
222
    (void)path;
 
223
    (void)db;
 
224
    (void)table_name;
 
225
    (void)is_tmp;
 
226
    (void)table_proto;
 
227
 
 
228
    return ENOENT;
 
229
  }
 
230
 
 
231
  /* Old style cursor errors */
 
232
protected:
 
233
  void print_keydup_error(uint32_t key_nr, const char *msg, Table &table);
 
234
  void print_error(int error, myf errflag, Table *table= NULL);
 
235
  virtual bool get_error_message(int error, String *buf);
 
236
public:
 
237
  virtual void print_error(int error, myf errflag, Table& table);
 
238
 
 
239
  bool is_user_selectable() const
 
240
  {
 
241
    return not flags.test(HTON_BIT_NOT_USER_SELECTABLE);
 
242
  }
 
243
 
 
244
  bool check_flag(const engine_flag_bits flag) const
 
245
  {
 
246
    return flags.test(flag);
 
247
  }
 
248
 
 
249
  // @todo match check_flag interface
 
250
  virtual uint32_t index_flags(enum  ha_key_alg) const { return 0; }
 
251
  virtual void startStatement(Session *session)
 
252
  {
 
253
    doStartStatement(session);
 
254
  }
 
255
  virtual void endStatement(Session *session)
 
256
  {
 
257
    doEndStatement(session);
 
258
  }
 
259
 
 
260
  /*
 
261
   * Called during Session::cleanup() for all engines
 
262
   */
 
263
  virtual int close_connection(Session  *)
 
264
  {
 
265
    return 0;
 
266
  }
 
267
  virtual Cursor *create(TableShare &, memory::Root *)= 0;
 
268
  /* args: path */
 
269
  virtual bool flush_logs() { return false; }
 
270
  virtual bool show_status(Session *, stat_print_fn *, enum ha_stat_type)
 
271
  {
 
272
    return false;
 
273
  }
 
274
 
 
275
  /**
 
276
    If frm_error() is called then we will use this to find out what file
 
277
    extentions exist for the storage engine. This is also used by the default
 
278
    rename_table and delete_table method in Cursor.cc.
 
279
 
 
280
    For engines that have two file name extentions (separate meta/index file
 
281
    and data file), the order of elements is relevant. First element of engine
 
282
    file name extentions array should be meta/index file extention. Second
 
283
    element - data file extention. This order is assumed by
 
284
    prepare_for_repair() when REPAIR Table ... USE_FRM is issued.
 
285
  */
 
286
  virtual const char **bas_ext() const =0;
 
287
 
 
288
protected:
 
289
  virtual int doCreateTable(Session *session,
 
290
                            const char *table_name,
 
291
                            Table& table_arg,
 
292
                            message::Table& proto)= 0;
 
293
 
 
294
  virtual int doRenameTable(Session* session,
 
295
                            const char *from, const char *to);
 
296
 
 
297
public:
 
298
 
 
299
  int renameTable(Session *session, const char *from, const char *to) 
 
300
  {
 
301
    setTransactionReadWrite(*session);
 
302
 
 
303
    return doRenameTable(session, from, to);
 
304
  }
 
305
 
 
306
  // @todo move these to protected
 
307
  virtual void doGetTableNames(CachedDirectory &directory,
 
308
                               std::string& db_name,
 
309
                               TableNameList &set_of_names);
 
310
  virtual int doDropTable(Session& session,
 
311
                          const std::string &table_path)= 0;
 
312
 
 
313
  const char *checkLowercaseNames(const char *path, char *tmp_path);
 
314
 
 
315
  /* Class Methods for operating on plugin */
 
316
  static bool addPlugin(plugin::StorageEngine *engine);
 
317
  static void removePlugin(plugin::StorageEngine *engine);
 
318
 
 
319
  static int getTableDefinition(Session& session,
 
320
                                TableIdentifier &identifier,
 
321
                                message::Table *table_proto= NULL,
 
322
                                bool include_temporary_tables= true);
 
323
  static int getTableDefinition(Session& session,
 
324
                                const char* path,
 
325
                                const char *db,
 
326
                                const char *table_name,
 
327
                                const bool is_tmp,
 
328
                                message::Table *table_proto= NULL,
 
329
                                bool include_temporary_tables= true);
 
330
  static bool doesTableExist(Session& session,
 
331
                             TableIdentifier &identifier,
 
332
                             bool include_temporary_tables= true);
 
333
 
 
334
  static plugin::StorageEngine *findByName(std::string find_str);
 
335
  static plugin::StorageEngine *findByName(Session& session,
 
336
                                           std::string find_str);
 
337
  static void closeConnection(Session* session);
 
338
  static void dropDatabase(char* path);
 
339
  static bool flushLogs(plugin::StorageEngine *db_type);
 
340
  static int dropTable(Session& session,
 
341
                       TableIdentifier &identifier);
 
342
  static void getTableNames(const std::string& db_name, TableNameList &set_of_names);
 
343
 
 
344
  // Check to see if any SE objects to creation.
 
345
  static bool canCreateTable(drizzled::TableIdentifier &identifier);
 
346
  virtual bool doCanCreateTable(const drizzled::TableIdentifier &identifier)
 
347
  { (void)identifier;  return true; }
 
348
 
 
349
  // @note All schema methods defined here
 
350
  static void getSchemaNames(SchemaNameList &set_of_names);
 
351
  static bool getSchemaDefinition(const std::string &schema_name, message::Schema &proto);
 
352
  static bool doesSchemaExist(const std::string &schema_name);
 
353
  static const CHARSET_INFO *getSchemaCollation(const std::string &schema_name);
 
354
  static bool createSchema(const drizzled::message::Schema &schema_message);
 
355
  static bool dropSchema(const std::string &schema_name);
 
356
  static bool alterSchema(const drizzled::message::Schema &schema_message);
 
357
 
 
358
  // @note make private/protected
 
359
  virtual void doGetSchemaNames(SchemaNameList &set_of_names)
 
360
  { (void)set_of_names; }
 
361
 
 
362
  virtual bool doGetSchemaDefinition(const std::string &schema_name, drizzled::message::Schema &proto)
 
363
  { 
 
364
    (void)schema_name;
 
365
    (void)proto; 
 
366
 
 
367
    return false; 
 
368
  }
 
369
 
 
370
  virtual bool doCreateSchema(const drizzled::message::Schema &schema_message)
 
371
  { (void)schema_message; return false; }
 
372
 
 
373
  virtual bool doAlterSchema(const drizzled::message::Schema &schema_message)
 
374
  { (void)schema_message; return false; }
 
375
 
 
376
  virtual bool doDropSchema(const std::string &schema_name)
 
377
  { (void)schema_name; return false; }
 
378
 
 
379
  static inline const std::string &resolveName(const StorageEngine *engine)
 
380
  {
 
381
    return engine == NULL ? UNKNOWN_STRING : engine->getName();
 
382
  }
 
383
 
 
384
  static int createTable(Session& session,
 
385
                         TableIdentifier &identifier,
 
386
                         bool update_create_info,
 
387
                         message::Table& table_proto);
 
388
 
 
389
  static void removeLostTemporaryTables(Session &session, const char *directory);
 
390
 
 
391
  Cursor *getCursor(TableShare &share, memory::Root *alloc);
 
392
 
 
393
  uint32_t max_record_length() const
 
394
  { return std::min((unsigned int)HA_MAX_REC_LENGTH, max_supported_record_length()); }
 
395
  uint32_t max_keys() const
 
396
  { return std::min((unsigned int)MAX_KEY, max_supported_keys()); }
 
397
  uint32_t max_key_parts() const
 
398
  { return std::min((unsigned int)MAX_REF_PARTS, max_supported_key_parts()); }
 
399
  uint32_t max_key_length() const
 
400
  { return std::min((unsigned int)MAX_KEY_LENGTH, max_supported_key_length()); }
 
401
  uint32_t max_key_part_length(void) const
 
402
  { return std::min((unsigned int)MAX_KEY_LENGTH, max_supported_key_part_length()); }
 
403
 
 
404
  virtual uint32_t max_supported_record_length(void) const
 
405
  { return HA_MAX_REC_LENGTH; }
 
406
  virtual uint32_t max_supported_keys(void) const { return 0; }
 
407
  virtual uint32_t max_supported_key_parts(void) const { return MAX_REF_PARTS; }
 
408
  virtual uint32_t max_supported_key_length(void) const { return MAX_KEY_LENGTH; }
 
409
  virtual uint32_t max_supported_key_part_length(void) const { return 255; }
 
410
 
 
411
  /* TODO-> Make private */
 
412
  static int readDefinitionFromPath(TableIdentifier &identifier, message::Table &proto);
 
413
  static int deleteDefinitionFromPath(TableIdentifier &identifier);
 
414
  static int renameDefinitionFromPath(TableIdentifier &dest, TableIdentifier &src);
 
415
  static int writeDefinitionFromPath(TableIdentifier &identifier, message::Table &proto);
 
416
 
 
417
public:
 
418
  /* 
 
419
   * The below are simple virtual overrides for the plugin::MonitoredInTransaction
 
420
   * interface.
 
421
   */
 
422
  virtual bool participatesInSqlTransaction() const
 
423
  {
 
424
    return false; /* plugin::StorageEngine is non-transactional in terms of SQL */
 
425
  }
 
426
  virtual bool participatesInXaTransaction() const
 
427
  {
 
428
    return false; /* plugin::StorageEngine is non-transactional in terms of XA */
 
429
  }
 
430
  virtual bool alwaysRegisterForXaTransaction() const
 
431
  {
 
432
    return false;
 
433
  }
 
434
};
 
435
 
 
436
} /* namespace plugin */
 
437
} /* namespace drizzled */
 
438
 
 
439
#endif /* DRIZZLED_PLUGIN_STORAGE_ENGINE_H */