1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
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.
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.
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
20
#ifndef DRIZZLED_PLUGIN_STORAGE_ENGINE_H
21
#define DRIZZLED_PLUGIN_STORAGE_ENGINE_H
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"
35
#include "drizzled/hash.h"
48
typedef struct st_hash HASH;
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);
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,
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,
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,
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);
121
class NamedSavepoint;
126
typedef hash_map<std::string, StorageEngine *> EngineMap;
127
typedef std::vector<StorageEngine *> EngineVector;
129
typedef std::set<std::string> TableNameList;
130
typedef std::set<std::string> SchemaNameList;
132
extern const std::string UNKNOWN_STRING;
133
extern const std::string DEFAULT_DEFINITION_FILE_EXT;
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)
140
usually StorageEngine instance is defined statically in ha_xxx.cc as
142
static StorageEngine { ... } xxx_engine;
144
class StorageEngine : public Plugin,
145
public MonitoredInTransaction
148
typedef uint64_t Table_flags;
151
const std::bitset<HTON_BIT_SIZE> flags; /* global Cursor flags */
153
virtual void setTransactionReadWrite(Session& session);
156
* Indicates to a storage engine the start of a
159
virtual void doStartStatement(Session *session)
165
* Indicates to a storage engine the end of
166
* the current SQL statement in the supplied
169
virtual void doEndStatement(Session *session)
175
std::string table_definition_ext;
178
const std::string& getTableDefinitionFileExtension()
180
return table_definition_ext;
184
std::vector<std::string> aliases;
187
const std::vector<std::string>& getAliases() const
192
void addAlias(std::string alias)
194
aliases.push_back(alias);
201
Used as a protobuf storage currently by TEMP only engines.
203
typedef std::map <std::string, message::Table> ProtoCache;
204
ProtoCache proto_cache;
205
pthread_mutex_t proto_cache_mutex;
209
StorageEngine(const std::string name_arg,
210
const std::bitset<HTON_BIT_SIZE> &flags_arg= HTON_NO_FLAGS);
212
virtual ~StorageEngine();
214
virtual int doGetTableDefinition(Session& session,
217
const char *table_name,
219
message::Table *table_proto)
231
/* Old style cursor errors */
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);
237
virtual void print_error(int error, myf errflag, Table& table);
239
bool is_user_selectable() const
241
return not flags.test(HTON_BIT_NOT_USER_SELECTABLE);
244
bool check_flag(const engine_flag_bits flag) const
246
return flags.test(flag);
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)
253
doStartStatement(session);
255
virtual void endStatement(Session *session)
257
doEndStatement(session);
261
* Called during Session::cleanup() for all engines
263
virtual int close_connection(Session *)
267
virtual Cursor *create(TableShare &, memory::Root *)= 0;
269
virtual bool flush_logs() { return false; }
270
virtual bool show_status(Session *, stat_print_fn *, enum ha_stat_type)
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.
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.
286
virtual const char **bas_ext() const =0;
289
virtual int doCreateTable(Session *session,
290
const char *table_name,
292
message::Table& proto)= 0;
294
virtual int doRenameTable(Session* session,
295
const char *from, const char *to);
299
int renameTable(Session *session, const char *from, const char *to)
301
setTransactionReadWrite(*session);
303
return doRenameTable(session, from, to);
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;
313
const char *checkLowercaseNames(const char *path, char *tmp_path);
315
/* Class Methods for operating on plugin */
316
static bool addPlugin(plugin::StorageEngine *engine);
317
static void removePlugin(plugin::StorageEngine *engine);
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,
326
const char *table_name,
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);
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);
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; }
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);
358
// @note make private/protected
359
virtual void doGetSchemaNames(SchemaNameList &set_of_names)
360
{ (void)set_of_names; }
362
virtual bool doGetSchemaDefinition(const std::string &schema_name, drizzled::message::Schema &proto)
370
virtual bool doCreateSchema(const drizzled::message::Schema &schema_message)
371
{ (void)schema_message; return false; }
373
virtual bool doAlterSchema(const drizzled::message::Schema &schema_message)
374
{ (void)schema_message; return false; }
376
virtual bool doDropSchema(const std::string &schema_name)
377
{ (void)schema_name; return false; }
379
static inline const std::string &resolveName(const StorageEngine *engine)
381
return engine == NULL ? UNKNOWN_STRING : engine->getName();
384
static int createTable(Session& session,
385
TableIdentifier &identifier,
386
bool update_create_info,
387
message::Table& table_proto);
389
static void removeLostTemporaryTables(Session &session, const char *directory);
391
Cursor *getCursor(TableShare &share, memory::Root *alloc);
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()); }
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; }
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);
419
* The below are simple virtual overrides for the plugin::MonitoredInTransaction
422
virtual bool participatesInSqlTransaction() const
424
return false; /* plugin::StorageEngine is non-transactional in terms of SQL */
426
virtual bool participatesInXaTransaction() const
428
return false; /* plugin::StorageEngine is non-transactional in terms of XA */
430
virtual bool alwaysRegisterForXaTransaction() const
436
} /* namespace plugin */
437
} /* namespace drizzled */
439
#endif /* DRIZZLED_PLUGIN_STORAGE_ENGINE_H */