~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/storage_engine.cc

  • Committer: Monty Taylor
  • Date: 2010-11-25 01:53:19 UTC
  • mto: (1953.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1955.
  • Revision ID: mordred@inaugust.com-20101125015319-ia85msn25uemopgc
Re-enabled -Wformat and then cleaned up the carnage.

Show diffs side-by-side

added added

removed removed

Lines of Context:
62
62
 
63
63
static bool shutdown_has_begun= false; // Once we put in the container for the vector/etc for engines this will go away.
64
64
 
 
65
using namespace std;
 
66
 
65
67
namespace drizzled
66
68
{
67
69
 
82
84
  return vector_of_schema_engines;
83
85
}
84
86
 
85
 
StorageEngine::StorageEngine(const std::string name_arg,
86
 
                             const std::bitset<HTON_BIT_SIZE> &flags_arg) :
 
87
StorageEngine::StorageEngine(const string name_arg,
 
88
                             const bitset<HTON_BIT_SIZE> &flags_arg) :
87
89
  Plugin(name_arg, "StorageEngine"),
88
90
  MonitoredInTransaction(), /* This gives the storage engine a "slot" or ID */
89
91
  flags(flags_arg)
192
194
}
193
195
 
194
196
class FindEngineByName
195
 
  : public std::unary_function<StorageEngine *, bool>
 
197
  : public unary_function<StorageEngine *, bool>
196
198
{
197
 
  const std::string &predicate;
 
199
  const string &predicate;
198
200
 
199
201
public:
200
 
  explicit FindEngineByName(const std::string &target_arg) :
 
202
  explicit FindEngineByName(const string &target_arg) :
201
203
    predicate(target_arg)
202
204
  {
203
205
  }
208
210
  }
209
211
};
210
212
 
211
 
StorageEngine *StorageEngine::findByName(const std::string &predicate)
 
213
StorageEngine *StorageEngine::findByName(const string &predicate)
212
214
{
213
 
  EngineVector::iterator iter= std::find_if(vector_of_engines.begin(),
214
 
                                            vector_of_engines.end(),
215
 
                                            FindEngineByName(predicate));
 
215
  EngineVector::iterator iter= find_if(vector_of_engines.begin(),
 
216
                                       vector_of_engines.end(),
 
217
                                       FindEngineByName(predicate));
216
218
  if (iter != vector_of_engines.end())
217
219
  {
218
220
    StorageEngine *engine= *iter;
223
225
  return NULL;
224
226
}
225
227
 
226
 
StorageEngine *StorageEngine::findByName(Session& session, const std::string &predicate)
 
228
StorageEngine *StorageEngine::findByName(Session& session, const string &predicate)
227
229
{
228
230
  if (boost::iequals(predicate, DEFAULT_STRING))
229
231
    return session.getDefaultStorageEngine();
230
232
 
231
 
  EngineVector::iterator iter= std::find_if(vector_of_engines.begin(),
232
 
                                            vector_of_engines.end(),
233
 
                                            FindEngineByName(predicate));
 
233
  EngineVector::iterator iter= find_if(vector_of_engines.begin(),
 
234
                                       vector_of_engines.end(),
 
235
                                       FindEngineByName(predicate));
234
236
  if (iter != vector_of_engines.end())
235
237
  {
236
238
    StorageEngine *engine= *iter;
241
243
  return NULL;
242
244
}
243
245
 
244
 
class StorageEngineCloseConnection : public std::unary_function<StorageEngine *, void>
 
246
class StorageEngineCloseConnection : public unary_function<StorageEngine *, void>
245
247
{
246
248
  Session *session;
247
249
public:
263
265
*/
264
266
void StorageEngine::closeConnection(Session* session)
265
267
{
266
 
  std::for_each(vector_of_engines.begin(), vector_of_engines.end(),
267
 
                StorageEngineCloseConnection(session));
 
268
  for_each(vector_of_engines.begin(), vector_of_engines.end(),
 
269
           StorageEngineCloseConnection(session));
268
270
}
269
271
 
270
272
bool StorageEngine::flushLogs(StorageEngine *engine)
271
273
{
272
274
  if (engine == NULL)
273
275
  {
274
 
    if (std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
275
 
                     std::mem_fun(&StorageEngine::flush_logs))
 
276
    if (find_if(vector_of_engines.begin(), vector_of_engines.end(),
 
277
                mem_fun(&StorageEngine::flush_logs))
276
278
        != vector_of_engines.begin())
277
279
      return true;
278
280
  }
284
286
  return false;
285
287
}
286
288
 
287
 
class StorageEngineGetTableDefinition: public std::unary_function<StorageEngine *,bool>
 
289
class StorageEngineGetTableDefinition: public unary_function<StorageEngine *,bool>
288
290
{
289
291
  Session& session;
290
292
  const TableIdentifier &identifier;
312
314
  }
313
315
};
314
316
 
315
 
class StorageEngineDoesTableExist: public std::unary_function<StorageEngine *, bool>
 
317
class StorageEngineDoesTableExist: public unary_function<StorageEngine *, bool>
316
318
{
317
319
  Session& session;
318
320
  const TableIdentifier &identifier;
343
345
  }
344
346
 
345
347
  EngineVector::iterator iter=
346
 
    std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
347
 
                 StorageEngineDoesTableExist(session, identifier));
 
348
    find_if(vector_of_engines.begin(), vector_of_engines.end(),
 
349
            StorageEngineDoesTableExist(session, identifier));
348
350
 
349
351
  if (iter == vector_of_engines.end())
350
352
  {
356
358
 
357
359
bool plugin::StorageEngine::doDoesTableExist(Session&, const drizzled::TableIdentifier&)
358
360
{
359
 
  std::cerr << " Engine was called for doDoesTableExist() and does not implement it: " << this->getName() << "\n";
 
361
  cerr << " Engine was called for doDoesTableExist() and does not implement it: " << this->getName() << "\n";
360
362
  assert(0);
361
363
  return false;
362
364
}
391
393
 
392
394
  message::Table message;
393
395
  EngineVector::iterator iter=
394
 
    std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
395
 
                 StorageEngineGetTableDefinition(session, identifier, message, err));
 
396
    find_if(vector_of_engines.begin(), vector_of_engines.end(),
 
397
            StorageEngineGetTableDefinition(session, identifier, message, err));
396
398
 
397
399
  if (iter == vector_of_engines.end())
398
400
  {
450
452
 
451
453
  if (error_proto == ER_CORRUPT_TABLE_DEFINITION)
452
454
  {
453
 
    std::string error_message;
454
 
    identifier.getSQLPath(error_message);
 
455
    string error_message;
455
456
 
 
457
    error_message.append(const_cast<TableIdentifier &>(identifier).getSQLPath());
456
458
    error_message.append(" : ");
457
459
    error_message.append(src_proto->InitializationErrorString());
458
460
 
468
470
 
469
471
  if (not engine)
470
472
  {
471
 
    std::string error_message;
472
 
    identifier.getSQLPath(error_message);
473
 
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), error_message.c_str());
 
473
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), const_cast<TableIdentifier &>(identifier).getSQLPath().c_str());
474
474
 
475
475
    return ER_CORRUPT_TABLE_DEFINITION;
476
476
  }
556
556
 
557
557
    if (error)
558
558
    {
559
 
      std::string path;
560
 
      identifier.getSQLPath(path);
561
 
      my_error(ER_CANT_CREATE_TABLE, MYF(ME_BELL+ME_WAITTANG), path.c_str(), error);
 
559
      my_error(ER_CANT_CREATE_TABLE, MYF(ME_BELL+ME_WAITTANG), const_cast<TableIdentifier &>(identifier).getSQLPath().c_str(), error);
562
560
    }
563
561
 
564
562
    table.delete_table();
573
571
}
574
572
 
575
573
class AddTableIdentifier : 
576
 
  public std::unary_function<StorageEngine *, void>
 
574
  public unary_function<StorageEngine *, void>
577
575
{
578
576
  CachedDirectory &directory;
579
577
  const SchemaIdentifier &identifier;
580
 
  TableIdentifier::vector &set_of_identifiers;
 
578
  TableIdentifiers &set_of_identifiers;
581
579
 
582
580
public:
583
581
 
584
 
  AddTableIdentifier(CachedDirectory &directory_arg, const SchemaIdentifier &identifier_arg, TableIdentifier::vector &of_names) :
 
582
  AddTableIdentifier(CachedDirectory &directory_arg, const SchemaIdentifier &identifier_arg, TableIdentifiers &of_names) :
585
583
    directory(directory_arg),
586
584
    identifier(identifier_arg),
587
585
    set_of_identifiers(of_names)
595
593
};
596
594
 
597
595
 
598
 
void StorageEngine::getIdentifiers(Session &session, const SchemaIdentifier &schema_identifier, TableIdentifier::vector &set_of_identifiers)
 
596
void StorageEngine::getIdentifiers(Session &session, const SchemaIdentifier &schema_identifier, TableIdentifiers &set_of_identifiers)
599
597
{
600
598
  static SchemaIdentifier INFORMATION_SCHEMA_IDENTIFIER("information_schema");
601
599
  static SchemaIdentifier DATA_DICTIONARY_IDENTIFIER("data_dictionary");
612
610
    {
613
611
      errno= directory.getError();
614
612
      if (errno == ENOENT)
615
 
      {
616
 
        std::string path;
617
 
        schema_identifier.getSQLPath(path);
618
 
        my_error(ER_BAD_DB_ERROR, MYF(ME_BELL+ME_WAITTANG), path.c_str());
619
 
      }
 
613
        my_error(ER_BAD_DB_ERROR, MYF(ME_BELL+ME_WAITTANG), const_cast<SchemaIdentifier &>(schema_identifier).getSQLPath().c_str());
620
614
      else
621
 
      {
622
615
        my_error(ER_CANT_READ_DIR, MYF(ME_BELL+ME_WAITTANG), directory.getPath(), errno);
623
 
      }
624
 
 
625
616
      return;
626
617
    }
627
618
  }
628
619
 
629
 
  std::for_each(vector_of_engines.begin(), vector_of_engines.end(),
630
 
                AddTableIdentifier(directory, schema_identifier, set_of_identifiers));
 
620
  for_each(vector_of_engines.begin(), vector_of_engines.end(),
 
621
           AddTableIdentifier(directory, schema_identifier, set_of_identifiers));
631
622
 
632
623
  session.doGetTableIdentifiers(directory, schema_identifier, set_of_identifiers);
633
624
}
634
625
 
635
 
class DropTable: public std::unary_function<TableIdentifier&, bool>
 
626
class DropTable: public unary_function<TableIdentifier&, bool>
636
627
{
637
628
  Session &session;
638
629
  StorageEngine *engine;
651
642
};
652
643
 
653
644
/* This will later be converted to TableIdentifiers */
654
 
class DropTables: public std::unary_function<StorageEngine *, void>
 
645
class DropTables: public unary_function<StorageEngine *, void>
655
646
{
656
647
  Session &session;
657
 
  TableIdentifier::vector &table_identifiers;
 
648
  TableIdentifiers &table_identifiers;
658
649
 
659
650
public:
660
651
 
661
 
  DropTables(Session &session_arg, TableIdentifier::vector &table_identifiers_arg) :
 
652
  DropTables(Session &session_arg, TableIdentifiers &table_identifiers_arg) :
662
653
    session(session_arg),
663
654
    table_identifiers(table_identifiers_arg)
664
655
  { }
667
658
  {
668
659
    // True returning from DropTable means the table has been successfully
669
660
    // deleted, so it should be removed from the list of tables to drop
670
 
    table_identifiers.erase(std::remove_if(table_identifiers.begin(),
671
 
                                           table_identifiers.end(),
672
 
                                           DropTable(session, engine)),
 
661
    table_identifiers.erase(remove_if(table_identifiers.begin(),
 
662
                                      table_identifiers.end(),
 
663
                                      DropTable(session, engine)),
673
664
                            table_identifiers.end());
674
665
  }
675
666
};
682
673
void StorageEngine::removeLostTemporaryTables(Session &session, const char *directory)
683
674
{
684
675
  CachedDirectory dir(directory, set_of_table_definition_ext);
685
 
  TableIdentifier::vector table_identifiers;
 
676
  TableIdentifiers table_identifiers;
686
677
 
687
678
  if (dir.fail())
688
679
  {
698
689
       fileIter != files.end(); fileIter++)
699
690
  {
700
691
    size_t length;
701
 
    std::string path;
 
692
    string path;
702
693
    CachedDirectory::Entry *entry= *fileIter;
703
694
 
704
695
    /* We remove the file extension. */
716
707
    }
717
708
  }
718
709
 
719
 
  std::for_each(vector_of_engines.begin(), vector_of_engines.end(),
720
 
                DropTables(session, table_identifiers));
721
 
 
 
710
  for_each(vector_of_engines.begin(), vector_of_engines.end(),
 
711
           DropTables(session, table_identifiers));
 
712
  
722
713
  /*
723
714
    Now we just clean up anything that might left over.
724
715
 
725
716
    We rescan because some of what might have been there should
726
717
    now be all nice and cleaned up.
727
718
  */
728
 
  std::set<std::string> all_exts= set_of_table_definition_ext;
 
719
  set<string> all_exts= set_of_table_definition_ext;
729
720
 
730
721
  for (EngineVector::iterator iter= vector_of_engines.begin();
731
722
       iter != vector_of_engines.end() ; iter++)
740
731
  for (CachedDirectory::Entries::iterator fileIter= files.begin();
741
732
       fileIter != files.end(); fileIter++)
742
733
  {
743
 
    std::string path;
 
734
    string path;
744
735
    CachedDirectory::Entry *entry= *fileIter;
745
736
 
746
737
    path+= directory;
1007
998
 
1008
999
int StorageEngine::deleteDefinitionFromPath(const TableIdentifier &identifier)
1009
1000
{
1010
 
  std::string path(identifier.getPath());
 
1001
  string path(identifier.getPath());
1011
1002
 
1012
1003
  path.append(DEFAULT_DEFINITION_FILE_EXT);
1013
1004
 
1017
1008
int StorageEngine::renameDefinitionFromPath(const TableIdentifier &dest, const TableIdentifier &src)
1018
1009
{
1019
1010
  message::Table table_message;
1020
 
  std::string src_path(src.getPath());
1021
 
  std::string dest_path(dest.getPath());
 
1011
  string src_path(src.getPath());
 
1012
  string dest_path(dest.getPath());
1022
1013
 
1023
1014
  src_path.append(DEFAULT_DEFINITION_FILE_EXT);
1024
1015
  dest_path.append(DEFAULT_DEFINITION_FILE_EXT);
1046
1037
int StorageEngine::writeDefinitionFromPath(const TableIdentifier &identifier, message::Table &table_message)
1047
1038
{
1048
1039
  char definition_file_tmp[FN_REFLEN];
1049
 
  std::string file_name(identifier.getPath());
 
1040
  string file_name(identifier.getPath());
1050
1041
 
1051
1042
  file_name.append(DEFAULT_DEFINITION_FILE_EXT);
1052
1043
 
1065
1056
 
1066
1057
  bool success;
1067
1058
 
1068
 
  try
1069
 
  {
 
1059
  try {
1070
1060
    success= table_message.SerializeToZeroCopyStream(output);
1071
1061
  }
1072
1062
  catch (...)
1116
1106
  return 0;
1117
1107
}
1118
1108
 
1119
 
class CanCreateTable: public std::unary_function<StorageEngine *, bool>
 
1109
class CanCreateTable: public unary_function<StorageEngine *, bool>
1120
1110
{
1121
1111
  const TableIdentifier &identifier;
1122
1112
 
1138
1128
bool StorageEngine::canCreateTable(const TableIdentifier &identifier)
1139
1129
{
1140
1130
  EngineVector::iterator iter=
1141
 
    std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
1142
 
                 CanCreateTable(identifier));
 
1131
    find_if(vector_of_engines.begin(), vector_of_engines.end(),
 
1132
            CanCreateTable(identifier));
1143
1133
 
1144
1134
  if (iter == vector_of_engines.end())
1145
1135
  {
1151
1141
 
1152
1142
bool StorageEngine::readTableFile(const std::string &path, message::Table &table_message)
1153
1143
{
1154
 
  std::fstream input(path.c_str(), std::ios::in | std::ios::binary);
 
1144
  fstream input(path.c_str(), ios::in | ios::binary);
1155
1145
 
1156
1146
  if (input.good())
1157
1147
  {