~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/storage_engine.cc

  • Committer: Andrew Hutchings
  • Date: 2010-12-06 19:36:53 UTC
  • mfrom: (1976 staging)
  • mto: This revision was merged to the branch mainline in revision 1991.
  • Revision ID: andrew@linuxjedi.co.uk-20101206193653-l85vryv18jb0yxx8
Merge with trunk

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