drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
storage_engine.cc
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#include <config.h>
21
22#include <fcntl.h>
23#include <unistd.h>
24
25#include <string>
26#include <vector>
27#include <set>
28#include <fstream>
29#include <algorithm>
30#include <functional>
31
32#include <google/protobuf/io/zero_copy_stream.h>
33#include <google/protobuf/io/zero_copy_stream_impl.h>
34
37#include <drizzled/base.h>
38#include <drizzled/cursor.h>
39#include <drizzled/plugin/storage_engine.h>
40#include <drizzled/session.h>
41#include <drizzled/error.h>
42#include <drizzled/gettext.h>
43#include <drizzled/data_home.h>
44#include <drizzled/errmsg_print.h>
45#include <drizzled/xid.h>
46#include <drizzled/sql_table.h>
47#include <drizzled/charset.h>
48#include <drizzled/internal/my_sys.h>
49#include <drizzled/table_proto.h>
50#include <drizzled/plugin/event_observer.h>
51#include <drizzled/table/shell.h>
52#include <drizzled/message/cache.h>
53#include <drizzled/key.h>
54#include <drizzled/session/transactions.h>
55#include <drizzled/open_tables_state.h>
56
57#include <boost/algorithm/string/compare.hpp>
58
59static bool shutdown_has_begun= false; // Once we put in the container for the vector/etc for engines this will go away.
60
61namespace drizzled {
62namespace plugin {
63
64static EngineVector g_engines;
65static EngineVector g_schema_engines;
66
67const std::string DEFAULT_STRING("default");
68const std::string UNKNOWN_STRING("UNKNOWN");
69const std::string DEFAULT_DEFINITION_FILE_EXT(".dfe");
70
71static std::set<std::string> set_of_table_definition_ext;
72
73EngineVector &StorageEngine::getSchemaEngines()
74{
75 return g_schema_engines;
76}
77
78const std::string defaultTableDefinitionFileExt()
79{
80 return DEFAULT_DEFINITION_FILE_EXT;
81}
82
83StorageEngine::StorageEngine(const std::string &name_arg,
84 const std::bitset<HTON_BIT_SIZE> &flags_arg) :
85 Plugin(name_arg, "StorageEngine"),
86 MonitoredInTransaction(), /* This gives the storage engine a "slot" or ID */
87 flags(flags_arg)
88{
89}
90
91StorageEngine::~StorageEngine()
92{
93}
94
95void StorageEngine::setTransactionReadWrite(Session& session)
96{
97 TransactionContext &statement_ctx= session.transaction.stmt;
98 statement_ctx.markModifiedNonTransData();
99}
100
101int StorageEngine::renameTable(Session &session, const identifier::Table &from, const identifier::Table &to)
102{
103 setTransactionReadWrite(session);
104 if (DRIZZLE_UNLIKELY(plugin::EventObserver::beforeRenameTable(session, from, to)))
105 return ER_EVENT_OBSERVER_PLUGIN;
106 int error= doRenameTable(session, from, to);
107 if (DRIZZLE_UNLIKELY(plugin::EventObserver::afterRenameTable(session, from, to, error)))
108 error= ER_EVENT_OBSERVER_PLUGIN;
109 return error;
110}
111
128{
129 int error= 0;
130 int enoent_or_zero= ENOENT; // Error if no file was deleted
131 char buff[FN_REFLEN];
132
133 for (const char **ext= bas_ext(); *ext ; ext++)
134 {
135 internal::fn_format(buff, identifier.getPath().c_str(), "", *ext,
136 MY_UNPACK_FILENAME|MY_APPEND_EXT);
137 if (internal::my_delete_with_symlink(buff, MYF(0)))
138 {
139 if ((error= errno) != ENOENT)
140 break;
141 }
142 else
143 {
144 enoent_or_zero= 0; // No error for ENOENT
145 }
146
147 error= enoent_or_zero;
148 }
149 return error;
150}
151
152bool StorageEngine::addPlugin(StorageEngine &engine)
153{
154 g_engines.push_back(&engine);
155
156 if (engine.getTableDefinitionFileExtension().length())
157 {
158 assert(engine.getTableDefinitionFileExtension().length() == plugin::defaultTableDefinitionFileExt().length());
159 set_of_table_definition_ext.insert(engine.getTableDefinitionFileExtension());
160 }
161
162 if (engine.check_flag(HTON_BIT_SCHEMA_DICTIONARY))
163 g_schema_engines.push_back(&engine);
164
165 return false;
166}
167
168void StorageEngine::removePlugin(StorageEngine &)
169{
170 if (shutdown_has_begun)
171 return;
172 shutdown_has_begun= true;
173 g_engines.clear();
174 g_schema_engines.clear();
175}
176
177StorageEngine *StorageEngine::findByName(const std::string &predicate)
178{
179 for (EngineVector::reference it : g_engines)
180 {
181 if (not boost::iequals(it->getName(), predicate))
182 continue;
183 if (it->is_user_selectable())
184 return it;
185 break;
186 }
187 return NULL;
188}
189
190StorageEngine *StorageEngine::findByName(Session& session, const std::string &predicate)
191{
192 if (boost::iequals(predicate, DEFAULT_STRING))
193 return session.getDefaultStorageEngine();
194 return findByName(predicate);
195}
196
202{
203 for (EngineVector::reference it : g_engines)
204 {
205 if (*session.getEngineData(it))
206 it->close_connection(&session);
207 }
208}
209
210bool StorageEngine::flushLogs(StorageEngine *engine)
211{
212 if (not engine)
213 {
214 if (std::find_if(g_engines.begin(), g_engines.end(), std::mem_fn(&StorageEngine::flush_logs))
215 != g_engines.begin()) // Shouldn't this be .end()?
216 return true;
217 }
218 else if (engine->flush_logs())
219 return true;
220 return false;
221}
222
223class StorageEngineGetTableDefinition
224{
225 Session& session;
226 const identifier::Table &identifier;
227 message::Table &table_message;
228 drizzled::error_t &err;
229
230public:
231 StorageEngineGetTableDefinition(Session& session_arg,
232 const identifier::Table &identifier_arg,
233 message::Table &table_message_arg,
234 drizzled::error_t &err_arg) :
235 session(session_arg),
236 identifier(identifier_arg),
237 table_message(table_message_arg),
238 err(err_arg) {}
239
240 bool operator() (StorageEngine *engine)
241 {
242 int ret= engine->doGetTableDefinition(session, identifier, table_message);
243
244 if (ret != ENOENT)
245 err= static_cast<drizzled::error_t>(ret);
246
247 return err == static_cast<drizzled::error_t>(EEXIST) or err != static_cast<drizzled::error_t>(ENOENT);
248 }
249};
250
255 const identifier::Table &identifier,
256 bool include_temporary_tables)
257{
258 if (include_temporary_tables && session.open_tables.doDoesTableExist(identifier))
259 return true;
260 for (EngineVector::reference it : g_engines)
261 {
262 if (it->doDoesTableExist(session, identifier))
263 return true;
264 }
265 return false;
266}
267
268bool plugin::StorageEngine::doDoesTableExist(Session&, const drizzled::identifier::Table&)
269{
270 std::cerr << " Engine was called for doDoesTableExist() and does not implement it: " << getName() << "\n";
271 assert(0);
272 return false;
273}
274
275message::table::shared_ptr StorageEngine::getTableMessage(Session& session,
276 const identifier::Table& identifier,
277 bool include_temporary_tables)
278{
279 drizzled::error_t error= static_cast<drizzled::error_t>(ENOENT);
280 if (include_temporary_tables)
281 {
282 if (Table *table= session.open_tables.find_temporary_table(identifier))
283 {
284 return message::table::shared_ptr(new message::Table(*table->getShare()->getTableMessage()));
285 }
286 }
287
288 drizzled::message::table::shared_ptr table_ptr;
289 if ((table_ptr= drizzled::message::Cache::singleton().find(identifier)))
290 {
291 (void)table_ptr;
292 }
293
294 message::Table message;
295 EngineVector::iterator iter= std::find_if(g_engines.begin(), g_engines.end(),
296 StorageEngineGetTableDefinition(session, identifier, message, error));
297
298 if (iter == g_engines.end())
299 {
300 return message::table::shared_ptr();
301 }
302 message::table::shared_ptr table_message(new message::Table(message));
303
304 drizzled::message::Cache::singleton().insert(identifier, table_message);
305
306 return table_message;
307}
308
309class DropTableByIdentifier
310{
311 Session& session;
312 const identifier::Table& identifier;
313 drizzled::error_t &error;
314
315public:
316
317 DropTableByIdentifier(Session& session_arg,
318 const identifier::Table& identifier_arg,
319 drizzled::error_t &error_arg) :
320 session(session_arg),
321 identifier(identifier_arg),
322 error(error_arg)
323 { }
324
325 bool operator() (EngineVector::value_type engine)
326 {
327 if (not engine->doDoesTableExist(session, identifier))
328 return false;
329
330 int local_error= engine->doDropTable(session, identifier);
331
332
333 if (not local_error)
334 return true;
335
336 switch (local_error)
337 {
338 case HA_ERR_NO_SUCH_TABLE:
339 case ENOENT:
340 error= static_cast<drizzled::error_t>(HA_ERR_NO_SUCH_TABLE);
341 return false;
342
343 default:
344 error= static_cast<drizzled::error_t>(local_error);
345 return true;
346 }
347 }
348};
349
350
351bool StorageEngine::dropTable(Session& session,
352 const identifier::Table& identifier,
353 drizzled::error_t &error)
354{
355 error= EE_OK;
356
357 EngineVector::const_iterator iter= std::find_if(g_engines.begin(), g_engines.end(),
358 DropTableByIdentifier(session, identifier, error));
359
360 if (error)
361 {
362 return false;
363 }
364 else if (iter == g_engines.end())
365 {
366 error= ER_BAD_TABLE_ERROR;
367 return false;
368 }
369
370 drizzled::message::Cache::singleton().erase(identifier);
371
372 return true;
373}
374
375bool StorageEngine::dropTable(Session& session,
376 const identifier::Table &identifier)
377{
378 drizzled::error_t error;
379 return dropTable(session, identifier, error);
380}
381
382bool StorageEngine::dropTable(Session& session,
383 StorageEngine &engine,
384 const identifier::Table& identifier,
385 drizzled::error_t &error)
386{
387 error= EE_OK;
388 engine.setTransactionReadWrite(session);
389
390 assert(identifier.isTmp());
391
392 if (DRIZZLE_UNLIKELY(plugin::EventObserver::beforeDropTable(session, identifier)))
393 {
394 error= ER_EVENT_OBSERVER_PLUGIN;
395 }
396 else
397 {
398 error= static_cast<drizzled::error_t>(engine.doDropTable(session, identifier));
399
400 if (DRIZZLE_UNLIKELY(plugin::EventObserver::afterDropTable(session, identifier, error)))
401 {
402 error= ER_EVENT_OBSERVER_PLUGIN;
403 }
404 }
405 drizzled::message::Cache::singleton().erase(identifier);
406 return not error;
407}
408
409
419 const identifier::Table &identifier,
420 message::Table& table_message)
421{
422 drizzled::error_t error= EE_OK;
423
424 TableShare share(identifier);
425 table::Shell table(share);
426 message::Table tmp_proto;
427
428 if (share.parse_table_proto(session, table_message) || share.open_table_from_share(&session, identifier, "", 0, 0, table))
429 {
430 // @note Error occured, we should probably do a little more here.
431 // ER_CORRUPT_TABLE_DEFINITION,ER_CORRUPT_TABLE_DEFINITION_ENUM
432
433 my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
434
435 return false;
436 }
437 else
438 {
439 /* Check for legal operations against the Engine using the proto (if used) */
440 if (table_message.type() == message::Table::TEMPORARY &&
441 share.storage_engine->check_flag(HTON_BIT_TEMPORARY_NOT_SUPPORTED) == true)
442 {
443 error= HA_ERR_UNSUPPORTED;
444 }
445 else if (table_message.type() != message::Table::TEMPORARY &&
446 share.storage_engine->check_flag(HTON_BIT_TEMPORARY_ONLY) == true)
447 {
448 error= HA_ERR_UNSUPPORTED;
449 }
450 else
451 {
452 share.storage_engine->setTransactionReadWrite(session);
453
454 error= static_cast<drizzled::error_t>(share.storage_engine->doCreateTable(session,
455 table,
456 identifier,
457 table_message));
458 }
459
460 if (error == ER_TABLE_PERMISSION_DENIED)
461 my_error(ER_TABLE_PERMISSION_DENIED, identifier);
462 else if (error)
463 my_error(ER_CANT_CREATE_TABLE, MYF(ME_BELL+ME_WAITTANG), identifier.getSQLPath().c_str(), error);
464 table.delete_table();
465 }
466 return(error == EE_OK);
467}
468
469Cursor *StorageEngine::getCursor(Table &arg)
470{
471 return create(arg);
472}
473
474void StorageEngine::getIdentifiers(Session &session, const identifier::Schema &schema_identifier, identifier::table::vector &set_of_identifiers)
475{
476 CachedDirectory directory(schema_identifier.getPath(), set_of_table_definition_ext);
477
478 if (schema_identifier == INFORMATION_SCHEMA_IDENTIFIER)
479 {
480 }
481 else if (schema_identifier == DATA_DICTIONARY_IDENTIFIER)
482 {
483 }
484 else if (directory.fail())
485 {
486 errno= directory.getError();
487 if (errno == ENOENT)
488 my_error(ER_BAD_DB_ERROR, MYF(ME_BELL+ME_WAITTANG), schema_identifier.getSQLPath().c_str());
489 else
490 my_error(ER_CANT_READ_DIR, MYF(ME_BELL+ME_WAITTANG), directory.getPath(), errno);
491 return;
492 }
493
494 for (EngineVector::reference it : g_engines)
495 it->doGetTableIdentifiers(directory, schema_identifier, set_of_identifiers);
496
497 session.open_tables.doGetTableIdentifiers(directory, schema_identifier, set_of_identifiers);
498}
499
500class DropTable
501{
502 Session &session;
503 StorageEngine *engine;
504
505public:
506
507 DropTable(Session &session_arg, StorageEngine *engine_arg) :
508 session(session_arg),
509 engine(engine_arg)
510 { }
511
512 bool operator() (identifier::Table& identifier)
513 {
514 return engine->doDropTable(session, identifier) == 0;
515 }
516};
517
518/*
519 This only works for engines which use file based DFE.
520
521 Note-> Unlike MySQL, we do not, on purpose, delete files that do not match any engines.
522*/
523void StorageEngine::removeLostTemporaryTables(Session &session, const char *directory)
524{
525 CachedDirectory dir(directory, set_of_table_definition_ext);
526 identifier::table::vector table_identifiers;
527
528 if (dir.fail())
529 {
530 errno= dir.getError();
531 my_error(ER_CANT_READ_DIR, MYF(0), directory, errno);
532
533 return;
534 }
535
536 CachedDirectory::Entries files= dir.getEntries();
537
538 for (CachedDirectory::Entries::iterator fileIter= files.begin();
539 fileIter != files.end(); fileIter++)
540 {
541 size_t length;
542 std::string path;
543 CachedDirectory::Entry *entry= *fileIter;
544
545 /* We remove the file extension. */
546 length= entry->filename.length();
547 entry->filename.resize(length - plugin::defaultTableDefinitionFileExt().length());
548
549 path+= directory;
550 path+= FN_LIBCHAR;
551 path+= entry->filename;
552 message::Table definition;
553 if (StorageEngine::readTableFile(path, definition))
554 {
555 identifier::Table identifier(identifier::Catalog(definition.catalog()),
556 definition.schema(),
557 definition.name(),
558 path);
559 table_identifiers.push_back(identifier);
560 }
561 }
562
563 for (EngineVector::reference it : g_engines)
564 {
565 table_identifiers.erase(std::remove_if(table_identifiers.begin(), table_identifiers.end(), DropTable(session, it)),
566 table_identifiers.end());
567 }
568
569 /*
570 Now we just clean up anything that might left over.
571
572 We rescan because some of what might have been there should
573 now be all nice and cleaned up.
574 */
575 std::set<std::string> all_exts= set_of_table_definition_ext;
576
577 for (EngineVector::iterator iter= g_engines.begin();
578 iter != g_engines.end() ; iter++)
579 {
580 for (const char **ext= (*iter)->bas_ext(); *ext ; ext++)
581 all_exts.insert(*ext);
582 }
583
584 CachedDirectory rescan(directory, all_exts);
585
586 files= rescan.getEntries();
587 for (CachedDirectory::Entries::iterator fileIter= files.begin();
588 fileIter != files.end(); fileIter++)
589 {
590 std::string path;
591 CachedDirectory::Entry *entry= *fileIter;
592
593 path+= directory;
594 path+= FN_LIBCHAR;
595 path+= entry->filename;
596
597 unlink(path.c_str());
598 }
599}
600
601
611void StorageEngine::print_error(int error, myf errflag, const Table &table) const
612{
613 drizzled::error_t textno= ER_GET_ERRNO;
614 switch (error) {
615 case EACCES:
616 textno=ER_OPEN_AS_READONLY;
617 break;
618 case EAGAIN:
619 textno=ER_FILE_USED;
620 break;
621 case ENOENT:
622 textno=ER_FILE_NOT_FOUND;
623 break;
624 case HA_ERR_KEY_NOT_FOUND:
625 case HA_ERR_NO_ACTIVE_RECORD:
626 case HA_ERR_END_OF_FILE:
627 textno=ER_KEY_NOT_FOUND;
628 break;
629 case HA_ERR_WRONG_MRG_TABLE_DEF:
630 textno=ER_WRONG_MRG_TABLE;
631 break;
632 case HA_ERR_FOUND_DUPP_KEY:
633 {
634 uint32_t key_nr= table.get_dup_key(error);
635 if ((int) key_nr >= 0)
636 {
637 const char *err_msg= ER(ER_DUP_ENTRY_WITH_KEY_NAME);
638
639 print_keydup_error(key_nr, err_msg, table);
640
641 return;
642 }
643 textno=ER_DUP_KEY;
644 break;
645 }
646 case HA_ERR_FOREIGN_DUPLICATE_KEY:
647 {
648 uint32_t key_nr= table.get_dup_key(error);
649 if ((int) key_nr >= 0)
650 {
651 uint32_t max_length;
652
653 /* Write the key in the error message */
654 char key[MAX_KEY_LENGTH];
655 String str(key,sizeof(key),system_charset_info);
656
657 /* Table is opened and defined at this point */
658 key_unpack(&str, &table,(uint32_t) key_nr);
659 max_length= (DRIZZLE_ERRMSG_SIZE-
660 (uint32_t) strlen(ER(ER_FOREIGN_DUPLICATE_KEY)));
661 if (str.length() >= max_length)
662 {
663 str.length(max_length-4);
664 str.append(STRING_WITH_LEN("..."));
665 }
666 my_error(ER_FOREIGN_DUPLICATE_KEY, MYF(0), table.getShare()->getTableName(),
667 str.c_ptr(), key_nr+1);
668 return;
669 }
670 textno= ER_DUP_KEY;
671 break;
672 }
673 case HA_ERR_FOUND_DUPP_UNIQUE:
674 textno=ER_DUP_UNIQUE;
675 break;
676 case HA_ERR_RECORD_CHANGED:
677 textno=ER_CHECKREAD;
678 break;
679 case HA_ERR_CRASHED:
680 textno=ER_NOT_KEYFILE;
681 break;
682 case HA_ERR_WRONG_IN_RECORD:
683 textno= ER_CRASHED_ON_USAGE;
684 break;
685 case HA_ERR_CRASHED_ON_USAGE:
686 textno=ER_CRASHED_ON_USAGE;
687 break;
688 case HA_ERR_NOT_A_TABLE:
689 textno= static_cast<drizzled::error_t>(error);
690 break;
691 case HA_ERR_CRASHED_ON_REPAIR:
692 textno=ER_CRASHED_ON_REPAIR;
693 break;
694 case HA_ERR_OUT_OF_MEM:
695 textno=ER_OUT_OF_RESOURCES;
696 break;
697 case HA_ERR_WRONG_COMMAND:
698 textno=ER_ILLEGAL_HA;
699 break;
700 case HA_ERR_OLD_FILE:
701 textno=ER_OLD_KEYFILE;
702 break;
703 case HA_ERR_UNSUPPORTED:
704 textno=ER_UNSUPPORTED_EXTENSION;
705 break;
706 case HA_ERR_RECORD_FILE_FULL:
707 case HA_ERR_INDEX_FILE_FULL:
708 textno=ER_RECORD_FILE_FULL;
709 break;
710 case HA_ERR_LOCK_WAIT_TIMEOUT:
711 textno=ER_LOCK_WAIT_TIMEOUT;
712 break;
713 case HA_ERR_LOCK_TABLE_FULL:
714 textno=ER_LOCK_TABLE_FULL;
715 break;
716 case HA_ERR_LOCK_DEADLOCK:
717 textno=ER_LOCK_DEADLOCK;
718 break;
719 case HA_ERR_READ_ONLY_TRANSACTION:
720 textno=ER_READ_ONLY_TRANSACTION;
721 break;
722 case HA_ERR_CANNOT_ADD_FOREIGN:
723 textno=ER_CANNOT_ADD_FOREIGN;
724 break;
725 case HA_ERR_ROW_IS_REFERENCED:
726 {
727 String str;
728 get_error_message(error, &str);
729 my_error(ER_ROW_IS_REFERENCED_2, MYF(0), str.c_str());
730 return;
731 }
732 case HA_ERR_NO_REFERENCED_ROW:
733 {
734 String str;
735 get_error_message(error, &str);
736 my_error(ER_NO_REFERENCED_ROW_2, MYF(0), str.c_str());
737 return;
738 }
739 case HA_ERR_TABLE_DEF_CHANGED:
740 textno=ER_TABLE_DEF_CHANGED;
741 break;
742 case HA_ERR_NO_SUCH_TABLE:
743 {
744 my_error(ER_TABLE_UNKNOWN, table.getShare()->getTableIdentifier());
745 return;
746 }
747 case HA_ERR_LOG_ROW_FOR_REPLICATION_FAILED:
748 textno= ER_LOG_ROW_FOR_REPLICATION_FAILED;
749 break;
750 case HA_ERR_DROP_INDEX_FK:
751 {
752 const char *ptr= "???";
753 uint32_t key_nr= table.get_dup_key(error);
754 if ((int) key_nr >= 0)
755 ptr= table.key_info[key_nr].name;
756 my_error(ER_DROP_INDEX_FK, MYF(0), ptr);
757 return;
758 }
759 case HA_ERR_TABLE_NEEDS_UPGRADE:
760 textno=ER_TABLE_NEEDS_UPGRADE;
761 break;
762 case HA_ERR_TABLE_READONLY:
763 textno= ER_OPEN_AS_READONLY;
764 break;
765 case HA_ERR_AUTOINC_READ_FAILED:
766 textno= ER_AUTOINC_READ_FAILED;
767 break;
768 case HA_ERR_AUTOINC_ERANGE:
769 textno= ER_WARN_DATA_OUT_OF_RANGE;
770 break;
771 case HA_ERR_LOCK_OR_ACTIVE_TRANSACTION:
772 my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
773 return;
774 default:
775 {
776 /*
777 The error was "unknown" to this function.
778 Ask Cursor if it has got a message for this error
779 */
780 bool temporary= false;
781 String str;
782 temporary= get_error_message(error, &str);
783 if (!str.empty())
784 {
785 const char* engine_name= getName().c_str();
786 if (temporary)
787 my_error(ER_GET_TEMPORARY_ERRMSG, MYF(0), error, str.ptr(), engine_name);
788 else
789 my_error(ER_GET_ERRMSG, MYF(0), error, str.ptr(), engine_name);
790 }
791 else
792 {
793 my_error(ER_GET_ERRNO,errflag,error);
794 }
795 return;
796 }
797 }
798
799 my_error(textno, errflag, table.getShare()->getTableName(), error);
800}
801
802
813{
814 return false;
815}
816
817
818void StorageEngine::print_keydup_error(uint32_t key_nr, const char *msg, const Table &table) const
819{
820 /* Write the duplicated key in the error message */
821 char key[MAX_KEY_LENGTH];
822 String str(key,sizeof(key),system_charset_info);
823
824 if (key_nr == MAX_KEY)
825 {
826 /* Key is unknown */
827 str.copy("", 0, system_charset_info);
828 my_printf_error(ER_DUP_ENTRY, msg, MYF(0), str.c_ptr(), "*UNKNOWN*");
829 }
830 else
831 {
832 /* Table is opened and defined at this point */
833 key_unpack(&str, &table, (uint32_t) key_nr);
834 uint32_t max_length=DRIZZLE_ERRMSG_SIZE-(uint32_t) strlen(msg);
835 if (str.length() >= max_length)
836 {
837 str.length(max_length-4);
838 str.append(STRING_WITH_LEN("..."));
839 }
840 my_printf_error(ER_DUP_ENTRY, msg,
841 MYF(0), str.c_ptr(), table.key_info[key_nr].name);
842 }
843}
844
845
846int StorageEngine::deleteDefinitionFromPath(const identifier::Table &identifier)
847{
848 std::string path(identifier.getPath());
849
850 path.append(plugin::defaultTableDefinitionFileExt());
851
852 return internal::my_delete(path.c_str(), MYF(0));
853}
854
855int StorageEngine::renameDefinitionFromPath(const identifier::Table &dest, const identifier::Table &src)
856{
857 message::Table table_message;
858 std::string src_path(src.getPath());
859 std::string dest_path(dest.getPath());
860
861 src_path.append(plugin::defaultTableDefinitionFileExt());
862 dest_path.append(plugin::defaultTableDefinitionFileExt());
863
864 bool was_read= StorageEngine::readTableFile(src_path.c_str(), table_message);
865
866 if (not was_read)
867 {
868 return ENOENT;
869 }
870
871 dest.copyToTableMessage(table_message);
872
873 int error= StorageEngine::writeDefinitionFromPath(dest, table_message);
874
875 if (not error)
876 {
877 if (unlink(src_path.c_str()))
878 perror(src_path.c_str());
879 }
880
881 return error;
882}
883
884int StorageEngine::writeDefinitionFromPath(const identifier::Table &identifier, const message::Table &table_message)
885{
886 char definition_file_tmp[FN_REFLEN];
887 std::string file_name(identifier.getPath());
888
889 file_name.append(plugin::defaultTableDefinitionFileExt());
890
891 snprintf(definition_file_tmp, sizeof(definition_file_tmp), "%sXXXXXX", file_name.c_str());
892
893 int fd= mkstemp(definition_file_tmp);
894
895 if (fd == -1)
896 {
897 perror(definition_file_tmp);
898 return errno;
899 }
900
901 google::protobuf::io::ZeroCopyOutputStream* output=
902 new google::protobuf::io::FileOutputStream(fd);
903
904 bool success;
905
906 try
907 {
908 success= table_message.SerializeToZeroCopyStream(output);
909 }
910 catch (...)
911 {
912 success= false;
913 }
914
915 if (not success)
916 {
917 my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), identifier.getSQLPath().c_str(), table_message.InitializationErrorString().c_str());
918 delete output;
919
920 if (close(fd) == -1)
921 perror(definition_file_tmp);
922
923 if (unlink(definition_file_tmp) == -1)
924 perror(definition_file_tmp);
925
926 return ER_CORRUPT_TABLE_DEFINITION;
927 }
928
929 delete output;
930
931 if (close(fd) == -1)
932 {
933 int error= errno;
934 perror(definition_file_tmp);
935
936 if (unlink(definition_file_tmp))
937 perror(definition_file_tmp);
938
939 return error;
940 }
941
942 if (rename(definition_file_tmp, file_name.c_str()) == -1)
943 {
944 int error= errno;
945 perror(definition_file_tmp);
946
947 if (unlink(definition_file_tmp))
948 perror(definition_file_tmp);
949
950 return error;
951 }
952
953 return 0;
954}
955
960{
961 for (EngineVector::reference it : g_engines)
962 {
963 if (not it->doCanCreateTable(identifier))
964 return false;
965 }
966 return true;
967}
968
969bool StorageEngine::readTableFile(const std::string &path, message::Table &table_message)
970{
971 std::fstream input(path.c_str(), std::ios::in | std::ios::binary);
972
973 if (input.good())
974 {
975 try {
976 if (table_message.ParseFromIstream(&input))
977 {
978 return true;
979 }
980 my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
981 table_message.name().empty() ? path.c_str() : table_message.name().c_str(),
982 table_message.InitializationErrorString().empty() ? "": table_message.InitializationErrorString().c_str());
983 }
984 catch (...)
985 {
986 my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
987 table_message.name().empty() ? path.c_str() : table_message.name().c_str(),
988 table_message.InitializationErrorString().empty() ? "": table_message.InitializationErrorString().c_str());
989 }
990 }
991 else
992 {
993 perror(path.c_str());
994 }
995
996 return false;
997}
998
999std::ostream& operator<<(std::ostream& output, const StorageEngine &engine)
1000{
1001 return output << "StorageEngine:(" << engine.getName() << ")";
1002}
1003
1004} /* namespace plugin */
1005} /* namespace drizzled */
Defines the interface to the CachedDirectory class.
static void closeConnection(Session &)
virtual bool get_error_message(int error, String *buf) const
static bool doesTableExist(Session &session, const drizzled::identifier::Table &identifier, bool include_temporary_tables=true)
virtual const char ** bas_ext() const =0
static bool createTable(Session &session, const identifier::Table &identifier, message::Table &table_message)
static bool canCreateTable(const drizzled::identifier::Table &identifier)
virtual void print_error(int error, myf errflag, const Table &table) const
virtual int doDropTable(Session &session, const drizzled::identifier::Table &identifier)=0
TODO: Rename this file - func.h is stupid.