drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
storage_engine.h
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, 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; 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#pragma once
21
23#include <drizzled/error_t.h>
24#include <drizzled/handler_structs.h>
25#include <drizzled/message.h>
26#include <drizzled/message/cache.h>
27#include <drizzled/plugin.h>
28#include <drizzled/plugin/monitored_in_transaction.h>
29#include <drizzled/plugin/plugin.h>
30#include <drizzled/sql_string.h>
31
32#include <bitset>
33#include <string>
34#include <vector>
35#include <set>
36
37#include <drizzled/visibility.h>
38
39namespace drizzled {
40
41struct HASH;
42
43typedef bool (stat_print_fn)(Session *session, const char *type, uint32_t type_len,
44 const char *file, uint32_t file_len,
45 const char *status, uint32_t status_len);
46
47/* Possible flags of a StorageEngine (there can be 32 of them) */
48enum engine_flag_bits {
49 HTON_BIT_ALTER_NOT_SUPPORTED, // Engine does not support alter
50 HTON_BIT_HIDDEN, // Engine does not appear in lists
51 HTON_BIT_NOT_USER_SELECTABLE,
52 HTON_BIT_TEMPORARY_NOT_SUPPORTED, // Having temporary tables not supported
53 HTON_BIT_TEMPORARY_ONLY,
54 HTON_BIT_STATS_RECORDS_IS_EXACT,
55 HTON_BIT_NULL_IN_KEY,
56 HTON_BIT_CAN_INDEX_BLOBS,
57 HTON_BIT_PRIMARY_KEY_IN_READ_INDEX,
58 HTON_BIT_PARTIAL_COLUMN_READ,
59 HTON_BIT_TABLE_SCAN_ON_INDEX,
60 HTON_BIT_FAST_KEY_READ,
61 HTON_BIT_NO_BLOBS,
62 HTON_BIT_HAS_RECORDS,
63 HTON_BIT_NO_AUTO_INCREMENT,
64 HTON_BIT_DUPLICATE_POS,
65 HTON_BIT_AUTO_PART_KEY,
66 HTON_BIT_REQUIRE_PRIMARY_KEY,
67 HTON_BIT_REQUIRES_KEY_COLUMNS_FOR_DELETE,
68 HTON_BIT_PRIMARY_KEY_REQUIRED_FOR_DELETE,
69 HTON_BIT_NO_PREFIX_CHAR_KEYS,
70 HTON_BIT_HAS_CHECKSUM,
71 HTON_BIT_SKIP_STORE_LOCK,
72 HTON_BIT_SCHEMA_DICTIONARY,
73 HTON_BIT_FOREIGN_KEYS,
74 HTON_BIT_SIZE
75};
76
77static const std::bitset<HTON_BIT_SIZE> HTON_NO_FLAGS(0);
78static const std::bitset<HTON_BIT_SIZE> HTON_ALTER_NOT_SUPPORTED(1 << HTON_BIT_ALTER_NOT_SUPPORTED);
79static const std::bitset<HTON_BIT_SIZE> HTON_HIDDEN(1 << HTON_BIT_HIDDEN);
80static const std::bitset<HTON_BIT_SIZE> HTON_NOT_USER_SELECTABLE(1 << HTON_BIT_NOT_USER_SELECTABLE);
81static const std::bitset<HTON_BIT_SIZE> HTON_TEMPORARY_NOT_SUPPORTED(1 << HTON_BIT_TEMPORARY_NOT_SUPPORTED);
82static const std::bitset<HTON_BIT_SIZE> HTON_TEMPORARY_ONLY(1 << HTON_BIT_TEMPORARY_ONLY);
83static const std::bitset<HTON_BIT_SIZE> HTON_STATS_RECORDS_IS_EXACT(1 << HTON_BIT_STATS_RECORDS_IS_EXACT);
84static const std::bitset<HTON_BIT_SIZE> HTON_NULL_IN_KEY(1 << HTON_BIT_NULL_IN_KEY);
85static const std::bitset<HTON_BIT_SIZE> HTON_CAN_INDEX_BLOBS(1 << HTON_BIT_CAN_INDEX_BLOBS);
86static const std::bitset<HTON_BIT_SIZE> HTON_PRIMARY_KEY_IN_READ_INDEX(1 << HTON_BIT_PRIMARY_KEY_IN_READ_INDEX);
87static const std::bitset<HTON_BIT_SIZE> HTON_PARTIAL_COLUMN_READ(1 << HTON_BIT_PARTIAL_COLUMN_READ);
88static const std::bitset<HTON_BIT_SIZE> HTON_TABLE_SCAN_ON_INDEX(1 << HTON_BIT_TABLE_SCAN_ON_INDEX);
89static const std::bitset<HTON_BIT_SIZE> HTON_FAST_KEY_READ(1 << HTON_BIT_FAST_KEY_READ);
90static const std::bitset<HTON_BIT_SIZE> HTON_NO_BLOBS(1 << HTON_BIT_NO_BLOBS);
91static const std::bitset<HTON_BIT_SIZE> HTON_HAS_RECORDS(1 << HTON_BIT_HAS_RECORDS);
92static const std::bitset<HTON_BIT_SIZE> HTON_NO_AUTO_INCREMENT(1 << HTON_BIT_NO_AUTO_INCREMENT);
93static const std::bitset<HTON_BIT_SIZE> HTON_DUPLICATE_POS(1 << HTON_BIT_DUPLICATE_POS);
94static const std::bitset<HTON_BIT_SIZE> HTON_AUTO_PART_KEY(1 << HTON_BIT_AUTO_PART_KEY);
95static const std::bitset<HTON_BIT_SIZE> HTON_REQUIRE_PRIMARY_KEY(1 << HTON_BIT_REQUIRE_PRIMARY_KEY);
96static const std::bitset<HTON_BIT_SIZE> HTON_REQUIRES_KEY_COLUMNS_FOR_DELETE(1 << HTON_BIT_REQUIRES_KEY_COLUMNS_FOR_DELETE);
97static const std::bitset<HTON_BIT_SIZE> HTON_PRIMARY_KEY_REQUIRED_FOR_DELETE(1 << HTON_BIT_PRIMARY_KEY_REQUIRED_FOR_DELETE);
98static const std::bitset<HTON_BIT_SIZE> HTON_NO_PREFIX_CHAR_KEYS(1 << HTON_BIT_NO_PREFIX_CHAR_KEYS);
99static const std::bitset<HTON_BIT_SIZE> HTON_HAS_CHECKSUM(1 << HTON_BIT_HAS_CHECKSUM);
100static const std::bitset<HTON_BIT_SIZE> HTON_SKIP_STORE_LOCK(1 << HTON_BIT_SKIP_STORE_LOCK);
101static const std::bitset<HTON_BIT_SIZE> HTON_HAS_SCHEMA_DICTIONARY(1 << HTON_BIT_SCHEMA_DICTIONARY);
102static const std::bitset<HTON_BIT_SIZE> HTON_HAS_FOREIGN_KEYS(1 << HTON_BIT_FOREIGN_KEYS);
103
104
105namespace plugin {
106
107typedef std::vector<StorageEngine *> EngineVector;
108
109typedef std::set<std::string> TableNameList;
110
111extern const std::string UNKNOWN_STRING;
112
113const std::string defaultTableDefinitionFileExt();
114
115/*
116 StorageEngine is a singleton structure - one instance per storage engine -
117 to provide access to storage engine functionality that works on the
118 "global" level (unlike Cursor class that works on a per-table basis)
119
120 usually StorageEngine instance is defined statically in ha_xxx.cc as
121
122 static StorageEngine { ... } xxx_engine;
123*/
124class DRIZZLED_API StorageEngine :
125 public Plugin,
126 public MonitoredInTransaction
127{
128 friend class SEAPITester;
129public:
130 typedef uint64_t Table_flags;
131
132private:
133 static EngineVector &getSchemaEngines();
134 const std::bitset<HTON_BIT_SIZE> flags; /* global Cursor flags */
135
136
137 virtual void setTransactionReadWrite(Session& session);
138
139 /*
140 * Indicates to a storage engine the start of a
141 * new SQL statement.
142 */
143 virtual void doStartStatement(Session *session)
144 {
145 (void) session;
146 }
147
148 /*
149 * Indicates to a storage engine the end of
150 * the current SQL statement in the supplied
151 * Session.
152 */
153 virtual void doEndStatement(Session *session)
154 {
155 (void) session;
156 }
157
158protected:
159 std::string table_definition_ext;
160
161public:
162 const std::string& getTableDefinitionFileExtension()
163 {
164 return table_definition_ext;
165 }
166
167 virtual message::Table::Index::IndexType default_index_type() const
168 {
169 return message::Table::Index::BTREE;
170 }
171
172private:
173 std::vector<std::string> aliases;
174
175public:
176 const std::vector<std::string>& getAliases() const
177 {
178 return aliases;
179 }
180
181 void addAlias(std::string alias)
182 {
183 aliases.push_back(alias);
184 }
185
186protected:
187
192 typedef std::map <std::string, message::Table> ProtoCache;
193 ProtoCache proto_cache;
194 pthread_mutex_t proto_cache_mutex;
195
196public:
197 StorageEngine(const std::string &name_arg,
198 const std::bitset<HTON_BIT_SIZE> &flags_arg= HTON_NO_FLAGS);
199
200 virtual ~StorageEngine();
201
202protected:
203 virtual int doGetTableDefinition(Session &session,
204 const drizzled::identifier::Table &identifier,
205 message::Table &table_message)
206 {
207 (void)session;
208 (void)identifier;
209 (void)table_message;
210
211 return ENOENT;
212 }
213
214 /* Old style cursor errors */
215 void print_keydup_error(uint32_t key_nr, const char *msg, const Table &table) const;
216 virtual bool get_error_message(int error, String *buf) const;
217
218public:
219 virtual void print_error(int error, myf errflag, const Table& table) const;
220
221 bool is_user_selectable() const
222 {
223 return not flags.test(HTON_BIT_NOT_USER_SELECTABLE);
224 }
225
226 bool check_flag(const engine_flag_bits flag) const
227 {
228 return flags.test(flag);
229 }
230
231 // @todo match check_flag interface
232 virtual uint32_t index_flags(drizzled::message::Table::Index::IndexType) const { return 0; }
233 virtual void startStatement(Session *session)
234 {
235 doStartStatement(session);
236 }
237 virtual void endStatement(Session *session)
238 {
239 doEndStatement(session);
240 }
241
242 /*
243 * Called during Session::cleanup() for all engines
244 */
245 virtual int close_connection(Session *)
246 {
247 return 0;
248 }
249
250 virtual Cursor *create(Table &)= 0;
251 /* args: path */
252 virtual bool flush_logs() { return false; }
253 virtual bool show_status(Session *, stat_print_fn *, enum ha_stat_type)
254 {
255 return false;
256 }
257
269 virtual const char **bas_ext() const =0;
270
271protected:
272 virtual int doCreateTable(Session &session,
273 Table &table_arg,
274 const drizzled::identifier::Table &identifier,
275 const message::Table &message)= 0;
276
277 virtual int doRenameTable(Session &session,
279
280 virtual int doDropTable(Session &session,
281 const drizzled::identifier::Table &identifier)= 0;
282
283 virtual void doGetTableIdentifiers(CachedDirectory &directory,
284 const drizzled::identifier::Schema &schema_identifier,
285 identifier::table::vector &set_of_identifiers)= 0;
286
287 virtual bool doDoesTableExist(Session& session, const drizzled::identifier::Table &identifier);
288
289 virtual bool doCanCreateTable(const drizzled::identifier::Table &identifier)
290 { (void)identifier; return true; }
291
292public:
293
294 friend class AddSchemaNames;
295 friend class AddTableIdentifier;
296 friend class AlterSchema;
297 friend class CanCreateTable;
298 friend class CreateSchema;
299 friend class DropSchema;
300 friend class DropTable;
301 friend class DropTables;
302 friend class FindEngineByName;
303 friend class Ha_delete_table_error_handler;
304 friend class StorageEngineCloseConnection;
305 friend class StorageEngineDoesTableExist;
308 friend class DropTableByIdentifier;
309
310 int renameTable(Session &session, const drizzled::identifier::Table &from, const drizzled::identifier::Table &to);
311
312 /* Class Methods for operating on plugin */
313 static bool addPlugin(plugin::StorageEngine &engine);
314 static void removePlugin(plugin::StorageEngine &engine);
315
316 static message::table::shared_ptr getTableMessage(Session& session,
317 const drizzled::identifier::Table &identifier,
318 bool include_temporary_tables= true);
319 static bool doesTableExist(Session &session,
320 const drizzled::identifier::Table &identifier,
321 bool include_temporary_tables= true);
322
323 static plugin::StorageEngine *findByName(const std::string &find_str);
324 static plugin::StorageEngine *findByName(Session& session, const std::string &find_str);
325
326 static void closeConnection(Session&);
327 static void dropDatabase(char* path);
328 static bool flushLogs(plugin::StorageEngine *db_type);
329
330 static bool dropTable(Session& session,
331 const drizzled::identifier::Table &identifier);
332 static bool dropTable(Session& session,
333 const drizzled::identifier::Table &identifier,
334 drizzled::error_t &error);
335
336 static bool dropTable(Session& session,
337 StorageEngine &engine,
338 const identifier::Table& identifier,
339 drizzled::error_t &error);
340
341 static void getIdentifiers(Session &session,
342 const identifier::Schema &schema_identifier,
343 identifier::table::vector &set_of_identifiers);
344
345 // Check to see if any SE objects to creation.
346 static bool canCreateTable(const drizzled::identifier::Table &identifier);
347
348 // @note All schema methods defined here
349 static void getIdentifiers(Session &session, identifier::schema::vector &schemas);
350 static message::schema::shared_ptr getSchemaDefinition(const drizzled::identifier::Table &identifier);
351 static message::schema::shared_ptr getSchemaDefinition(const drizzled::identifier::Schema &identifier);
352 static bool doesSchemaExist(const drizzled::identifier::Schema &identifier);
353 static const charset_info_st *getSchemaCollation(const drizzled::identifier::Schema &identifier);
354 static bool createSchema(const drizzled::message::Schema &schema_message);
355 static bool dropSchema(Session &session,
356 const identifier::Schema& identifier,
357 message::schema::const_reference schema_message);
358 static bool alterSchema(const drizzled::message::Schema &schema_message);
359
360 // @note make private/protected
361protected:
362 virtual void doGetSchemaIdentifiers(identifier::schema::vector&)
363 { }
364
365 virtual drizzled::message::schema::shared_ptr doGetSchemaDefinition(const drizzled::identifier::Schema&)
366 {
367 return drizzled::message::schema::shared_ptr();
368 }
369
370 virtual bool doCreateSchema(const drizzled::message::Schema&)
371 { return false; }
372
373 virtual bool doAlterSchema(const drizzled::message::Schema&)
374 { return false; }
375
376 virtual bool doDropSchema(const drizzled::identifier::Schema&)
377 { return false; }
378
379public:
380 static inline const std::string &resolveName(const StorageEngine *engine)
381 {
382 return engine == NULL ? UNKNOWN_STRING : engine->getName();
383 }
384
385 static bool createTable(Session &session,
386 const identifier::Table &identifier,
387 message::Table& table_message);
388
389 static void removeLostTemporaryTables(Session &session, const char *directory);
390
391 Cursor *getCursor(Table &share);
392
393 uint32_t max_record_length() const
394 { return std::min(HA_MAX_REC_LENGTH, max_supported_record_length()); }
395 uint32_t max_keys() const
396 { return std::min(MAX_KEY, max_supported_keys()); }
397 uint32_t max_key_parts() const
398 { return std::min(MAX_REF_PARTS, max_supported_key_parts()); }
399 uint32_t max_key_length() const
400 { return std::min(MAX_KEY_LENGTH, max_supported_key_length()); }
401 uint32_t max_key_part_length(void) const
402 { return std::min(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 */
412protected:
413 static int deleteDefinitionFromPath(const drizzled::identifier::Table &identifier);
414 static int renameDefinitionFromPath(const drizzled::identifier::Table &dest, const drizzled::identifier::Table &src);
415 static int writeDefinitionFromPath(const drizzled::identifier::Table &identifier, const message::Table &proto);
416 static bool readTableFile(const std::string &path, message::Table &table_message);
417
418public:
419 /*
420 * The below are simple virtual overrides for the plugin::MonitoredInTransaction
421 * interface.
422 */
423 virtual bool participatesInSqlTransaction() const
424 {
425 return false; /* plugin::StorageEngine is non-transactional in terms of SQL */
426 }
427 virtual bool participatesInXaTransaction() const
428 {
429 return false; /* plugin::StorageEngine is non-transactional in terms of XA */
430 }
432 {
433 return false;
434 }
435
436 virtual bool validateCreateTableOption(const std::string &key, const std::string &state)
437 {
438 (void)key;
439 (void)state;
440
441 return false;
442 }
443
444 virtual bool validateCreateSchemaOption(const std::string &key, const std::string &state)
445 {
446 (void)key;
447 (void)state;
448
449 return false;
450 }
451};
452
453std::ostream& operator<<(std::ostream& output, const StorageEngine &engine);
454
455} /* namespace plugin */
456} /* namespace drizzled */
457
virtual bool alwaysRegisterForXaTransaction() const
virtual bool participatesInXaTransaction() const
virtual bool participatesInSqlTransaction() const
virtual const char ** bas_ext() const =0
std::map< std::string, message::Table > ProtoCache
Used as a protobuf storage currently by TEMP only engines.
virtual int doDropTable(Session &session, const drizzled::identifier::Table &identifier)=0
Convert from decimal to binary representation.
Definition common_fwd.h:215
TODO: Rename this file - func.h is stupid.
Visibility Control Macros.
#define DRIZZLED_API
Definition visibility.h:38