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/table.pb.h>
28
#include "drizzled/plugin/plugin.h"
29
#include <drizzled/registry.h>
41
typedef struct st_mysql_lex_string LEX_STRING;
42
typedef bool (stat_print_fn)(Session *session, const char *type, uint32_t type_len,
43
const char *file, uint32_t file_len,
44
const char *status, uint32_t status_len);
46
/* Possible flags of a StorageEngine (there can be 32 of them) */
47
enum engine_flag_bits {
48
HTON_BIT_ALTER_NOT_SUPPORTED, // Engine does not support alter
49
HTON_BIT_CAN_RECREATE, // Delete all is used for truncate
50
HTON_BIT_HIDDEN, // Engine does not appear in lists
51
HTON_BIT_FLUSH_AFTER_RENAME,
52
HTON_BIT_NOT_USER_SELECTABLE,
53
HTON_BIT_TEMPORARY_NOT_SUPPORTED, // Having temporary tables not supported
54
HTON_BIT_TEMPORARY_ONLY,
55
HTON_BIT_FILE_BASED, // use for check_lowercase_names
56
HTON_BIT_HAS_DATA_DICTIONARY,
60
static const std::bitset<HTON_BIT_SIZE> HTON_NO_FLAGS(0);
61
static const std::bitset<HTON_BIT_SIZE> HTON_ALTER_NOT_SUPPORTED(1 << HTON_BIT_ALTER_NOT_SUPPORTED);
62
static const std::bitset<HTON_BIT_SIZE> HTON_CAN_RECREATE(1 << HTON_BIT_CAN_RECREATE);
63
static const std::bitset<HTON_BIT_SIZE> HTON_HIDDEN(1 << HTON_BIT_HIDDEN);
64
static const std::bitset<HTON_BIT_SIZE> HTON_FLUSH_AFTER_RENAME(1 << HTON_BIT_FLUSH_AFTER_RENAME);
65
static const std::bitset<HTON_BIT_SIZE> HTON_NOT_USER_SELECTABLE(1 << HTON_BIT_NOT_USER_SELECTABLE);
66
static const std::bitset<HTON_BIT_SIZE> HTON_TEMPORARY_NOT_SUPPORTED(1 << HTON_BIT_TEMPORARY_NOT_SUPPORTED);
67
static const std::bitset<HTON_BIT_SIZE> HTON_TEMPORARY_ONLY(1 << HTON_BIT_TEMPORARY_ONLY);
68
static const std::bitset<HTON_BIT_SIZE> HTON_FILE_BASED(1 << HTON_BIT_FILE_BASED);
69
static const std::bitset<HTON_BIT_SIZE> HTON_HAS_DATA_DICTIONARY(1 << HTON_BIT_HAS_DATA_DICTIONARY);
78
const std::string UNKNOWN_STRING("UNKNOWN");
80
class TableNameIteratorImplementation;
82
StorageEngine is a singleton structure - one instance per storage engine -
83
to provide access to storage engine functionality that works on the
84
"global" level (unlike handler class that works on a per-table basis)
86
usually StorageEngine instance is defined statically in ha_xxx.cc as
88
static StorageEngine { ... } xxx_engine;
90
savepoint_*, prepare, recover, and *_by_xid pointers can be 0.
92
class StorageEngine : public Plugin
95
Name used for storage engine.
97
const bool two_phase_commit;
100
const std::bitset<HTON_BIT_SIZE> flags; /* global handler flags */
102
to store per-savepoint data storage engine is provided with an area
103
of a requested size (0 is ok here).
104
savepoint_offset must be initialized statically to the size of
105
the needed memory to store per-savepoint information.
106
After xxx_init it is changed to be an offset to savepoint storage
107
area and need not be used by storage engine.
108
see binlog_engine and binlog_savepoint_set/rollback for an example.
110
size_t savepoint_offset;
111
size_t orig_savepoint_offset;
112
std::vector<std::string> aliases;
114
void setTransactionReadWrite(Session* session);
119
* Implementing classes should override these to provide savepoint
122
virtual int savepoint_set_hook(Session *, void *) { return 0; }
124
virtual int savepoint_rollback_hook(Session *, void *) { return 0; }
126
virtual int savepoint_release_hook(Session *, void *) { return 0; }
130
StorageEngine(const std::string name_arg,
131
const std::bitset<HTON_BIT_SIZE> &flags_arg= HTON_NO_FLAGS,
132
size_t savepoint_offset_arg= 0,
133
bool support_2pc= false);
135
virtual ~StorageEngine();
137
virtual int getTableProtoImplementation(const char* path,
138
drizzled::message::Table *table_proto)
146
each storage engine has it's own memory area (actually a pointer)
147
in the session, for storing per-connection information.
150
session->ha_data[xxx_engine.slot]
152
slot number is initialized by MySQL after xxx_init() is called.
156
inline uint32_t getSlot (void) { return slot; }
157
inline void setSlot (uint32_t value) { slot= value; }
159
const std::vector<std::string>& getAliases()
164
void addAlias(std::string alias)
166
aliases.push_back(alias);
171
return two_phase_commit;
175
bool is_enabled() const
180
bool is_user_selectable() const
182
return not flags.test(HTON_BIT_NOT_USER_SELECTABLE);
185
bool check_flag(const engine_flag_bits flag) const
187
return flags.test(flag);
190
void enable() { enabled= true; }
191
void disable() { enabled= false; }
194
StorageEngine methods:
196
close_connection is only called if
197
session->ha_data[xxx_engine.slot] is non-zero, so even if you don't need
198
this storage area - set it to something, so that MySQL would know
199
this storage engine was accessed in this connection
201
virtual int close_connection(Session *)
206
'all' is true if it's a real commit, that makes persistent changes
207
'all' is false if it's not in fact a commit but an end of the
208
statement that is part of the transaction.
209
NOTE 'all' is also false in auto-commit mode where 'end of statement'
210
and 'real commit' mean the same event.
212
virtual int commit(Session *, bool)
217
virtual int rollback(Session *, bool)
223
The void * points to an uninitialized storage area of requested size
224
(see savepoint_offset description)
226
int savepoint_set(Session *session, void *sp)
228
return savepoint_set_hook(session, (unsigned char *)sp+savepoint_offset);
232
The void * points to a storage area, that was earlier passed
233
to the savepoint_set call
235
int savepoint_rollback(Session *session, void *sp)
237
return savepoint_rollback_hook(session,
238
(unsigned char *)sp+savepoint_offset);
241
int savepoint_release(Session *session, void *sp)
243
return savepoint_release_hook(session,
244
(unsigned char *)sp+savepoint_offset);
247
virtual int prepare(Session *, bool) { return 0; }
248
virtual int recover(XID *, uint32_t) { return 0; }
249
virtual int commit_by_xid(XID *) { return 0; }
250
virtual int rollback_by_xid(XID *) { return 0; }
251
virtual handler *create(TableShare *, MEM_ROOT *)= 0;
253
virtual void drop_database(char*) { }
254
virtual int start_consistent_snapshot(Session *) { return 0; }
255
virtual bool flush_logs() { return false; }
256
virtual bool show_status(Session *, stat_print_fn *, enum ha_stat_type)
261
/* args: current_session, tables, cond */
262
virtual int fill_files_table(Session *, TableList *,
263
Item *) { return 0; }
264
virtual int release_temporary_latches(Session *) { return false; }
267
If frm_error() is called then we will use this to find out what file
268
extentions exist for the storage engine. This is also used by the default
269
rename_table and delete_table method in handler.cc.
271
For engines that have two file name extentions (separate meta/index file
272
and data file), the order of elements is relevant. First element of engine
273
file name extentions array should be meta/index file extention. Second
274
element - data file extention. This order is assumed by
275
prepare_for_repair() when REPAIR Table ... USE_FRM is issued.
277
virtual const char **bas_ext() const =0;
280
virtual int createTableImplementation(Session *session,
281
const char *table_name,
283
HA_CREATE_INFO *create_info,
284
drizzled::message::Table* proto)= 0;
286
virtual int renameTableImplementation(Session* session,
287
const char *from, const char *to);
289
virtual int deleteTableImplementation(Session* session,
290
const std::string table_path);
293
int createTable(Session *session, const char *path, Table *table_arg,
294
HA_CREATE_INFO *create_info,
295
drizzled::message::Table *proto)
297
char name_buff[FN_REFLEN];
298
const char *table_name;
300
table_name= checkLowercaseNames(path, name_buff);
302
setTransactionReadWrite(session);
304
return createTableImplementation(session, table_name, table_arg,
308
int renameTable(Session *session, const char *from, const char *to)
310
setTransactionReadWrite(session);
312
return renameTableImplementation(session, from, to);
315
int deleteTable(Session* session, const std::string table_path)
317
setTransactionReadWrite(session);
319
return deleteTableImplementation(session, table_path);
322
const char *checkLowercaseNames(const char *path, char *tmp_path);
324
virtual TableNameIteratorImplementation* tableNameIterator(const std::string &database)
331
static void add(plugin::StorageEngine *engine);
332
static void remove(plugin::StorageEngine *engine);
334
static int getTableProto(const char* path, message::Table *table_proto);
336
static plugin::StorageEngine *findByName(Session *session,
337
std::string find_str);
338
static void closeConnection(Session* session);
339
static void dropDatabase(char* path);
340
static int commitOrRollbackByXID(XID *xid, bool commit);
341
static int releaseTemporaryLatches(Session *session);
342
static bool flushLogs(plugin::StorageEngine *db_type);
343
static int recover(HASH *commit_list);
344
static int startConsistentSnapshot(Session *session);
345
static int deleteTable(Session *session, const char *path, const char *db,
346
const char *alias, bool generate_warning);
347
static inline const std::string &resolveName(const StorageEngine *engine)
349
return engine == NULL ? UNKNOWN_STRING : engine->getName();
354
class TableNameIteratorImplementation
359
TableNameIteratorImplementation(const std::string &database) : db(database)
361
virtual ~TableNameIteratorImplementation() {};
363
virtual int next(std::string *name)= 0;
367
class TableNameIterator
370
::drizzled::Registry<plugin::StorageEngine *>::iterator engine_iter;
371
plugin::TableNameIteratorImplementation *current_implementation;
372
plugin::TableNameIteratorImplementation *default_implementation;
373
std::string database;
375
TableNameIterator(const std::string &db);
376
~TableNameIterator();
378
int next(std::string *name);
382
} /* namespace plugin */
383
} /* namespace drizzled */
387
Return the default storage engine plugin::StorageEngine for thread
389
@param ha_default_storage_engine(session)
390
@param session current thread
393
pointer to plugin::StorageEngine
395
drizzled::plugin::StorageEngine *ha_default_storage_engine(Session *session);
397
handler *get_new_handler(TableShare *share, MEM_ROOT *alloc,
398
drizzled::plugin::StorageEngine *db_type);
402
/** @TODO remove each of the following convenience naming methods */
404
static inline void ha_close_connection(Session *session)
406
drizzled::plugin::StorageEngine::closeConnection(session);
409
static inline void ha_drop_database(char* path)
411
drizzled::plugin::StorageEngine::dropDatabase(path);
414
static inline int ha_commit_or_rollback_by_xid(XID *xid, bool commit)
416
return drizzled::plugin::StorageEngine::commitOrRollbackByXID(xid, commit);
419
/* report to InnoDB that control passes to the client */
420
static inline int ha_release_temporary_latches(Session *session)
422
return drizzled::plugin::StorageEngine::releaseTemporaryLatches(session);
425
static inline bool ha_flush_logs(drizzled::plugin::StorageEngine *engine)
427
return drizzled::plugin::StorageEngine::flushLogs(engine);
430
static inline int ha_recover(HASH *commit_list)
432
return drizzled::plugin::StorageEngine::recover(commit_list);
435
static inline int ha_start_consistent_snapshot(Session *session)
437
return drizzled::plugin::StorageEngine::startConsistentSnapshot(session);
440
static inline int ha_delete_table(Session *session, const char *path,
441
const char *db, const char *alias, bool generate_warning)
443
return drizzled::plugin::StorageEngine::deleteTable(session, path, db,
444
alias, generate_warning);
448
#endif /* DRIZZLED_PLUGIN_STORAGE_ENGINE_H */