~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/storage_engine.cc

  • Committer: Stewart Smith
  • Date: 2010-11-03 03:29:48 UTC
  • mto: (1902.1.1 build) (1910.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1903.
  • Revision ID: stewart@flamingspork.com-20101103032948-ocaksrku0oqxw8aa
fix more docs warnings: underline/overline too short

Show diffs side-by-side

added added

removed removed

Lines of Context:
56
56
 
57
57
#include <drizzled/table/shell.h>
58
58
 
59
 
#include "drizzled/message/cache.h"
60
 
 
61
59
#include <boost/algorithm/string/compare.hpp>
62
60
 
63
61
static bool shutdown_has_begun= false; // Once we put in the container for the vector/etc for engines this will go away.
64
62
 
 
63
using namespace std;
 
64
 
65
65
namespace drizzled
66
66
{
67
67
 
82
82
  return vector_of_schema_engines;
83
83
}
84
84
 
85
 
StorageEngine::StorageEngine(const std::string name_arg,
86
 
                             const std::bitset<HTON_BIT_SIZE> &flags_arg) :
 
85
StorageEngine::StorageEngine(const string name_arg,
 
86
                             const bitset<HTON_BIT_SIZE> &flags_arg) :
87
87
  Plugin(name_arg, "StorageEngine"),
88
88
  MonitoredInTransaction(), /* This gives the storage engine a "slot" or ID */
89
89
  flags(flags_arg)
192
192
}
193
193
 
194
194
class FindEngineByName
195
 
  : public std::unary_function<StorageEngine *, bool>
 
195
  : public unary_function<StorageEngine *, bool>
196
196
{
197
 
  const std::string &predicate;
 
197
  const string &predicate;
198
198
 
199
199
public:
200
 
  explicit FindEngineByName(const std::string &target_arg) :
 
200
  explicit FindEngineByName(const string &target_arg) :
201
201
    predicate(target_arg)
202
202
  {
203
203
  }
208
208
  }
209
209
};
210
210
 
211
 
StorageEngine *StorageEngine::findByName(const std::string &predicate)
 
211
StorageEngine *StorageEngine::findByName(const string &predicate)
212
212
{
213
 
  EngineVector::iterator iter= std::find_if(vector_of_engines.begin(),
214
 
                                            vector_of_engines.end(),
215
 
                                            FindEngineByName(predicate));
 
213
  EngineVector::iterator iter= find_if(vector_of_engines.begin(),
 
214
                                       vector_of_engines.end(),
 
215
                                       FindEngineByName(predicate));
216
216
  if (iter != vector_of_engines.end())
217
217
  {
218
218
    StorageEngine *engine= *iter;
223
223
  return NULL;
224
224
}
225
225
 
226
 
StorageEngine *StorageEngine::findByName(Session& session, const std::string &predicate)
 
226
StorageEngine *StorageEngine::findByName(Session& session, const string &predicate)
227
227
{
228
228
  if (boost::iequals(predicate, DEFAULT_STRING))
229
229
    return session.getDefaultStorageEngine();
230
230
 
231
 
  EngineVector::iterator iter= std::find_if(vector_of_engines.begin(),
232
 
                                            vector_of_engines.end(),
233
 
                                            FindEngineByName(predicate));
 
231
  EngineVector::iterator iter= find_if(vector_of_engines.begin(),
 
232
                                       vector_of_engines.end(),
 
233
                                       FindEngineByName(predicate));
234
234
  if (iter != vector_of_engines.end())
235
235
  {
236
236
    StorageEngine *engine= *iter;
241
241
  return NULL;
242
242
}
243
243
 
244
 
class StorageEngineCloseConnection : public std::unary_function<StorageEngine *, void>
 
244
class StorageEngineCloseConnection : public unary_function<StorageEngine *, void>
245
245
{
246
246
  Session *session;
247
247
public:
263
263
*/
264
264
void StorageEngine::closeConnection(Session* session)
265
265
{
266
 
  std::for_each(vector_of_engines.begin(), vector_of_engines.end(),
267
 
                StorageEngineCloseConnection(session));
 
266
  for_each(vector_of_engines.begin(), vector_of_engines.end(),
 
267
           StorageEngineCloseConnection(session));
268
268
}
269
269
 
270
270
bool StorageEngine::flushLogs(StorageEngine *engine)
271
271
{
272
272
  if (engine == NULL)
273
273
  {
274
 
    if (std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
275
 
                     std::mem_fun(&StorageEngine::flush_logs))
 
274
    if (find_if(vector_of_engines.begin(), vector_of_engines.end(),
 
275
                mem_fun(&StorageEngine::flush_logs))
276
276
        != vector_of_engines.begin())
277
277
      return true;
278
278
  }
284
284
  return false;
285
285
}
286
286
 
287
 
class StorageEngineGetTableDefinition: public std::unary_function<StorageEngine *,bool>
 
287
class StorageEngineGetTableDefinition: public unary_function<StorageEngine *,bool>
288
288
{
289
289
  Session& session;
290
290
  const TableIdentifier &identifier;
312
312
  }
313
313
};
314
314
 
315
 
class StorageEngineDoesTableExist: public std::unary_function<StorageEngine *, bool>
 
315
class StorageEngineDoesTableExist: public unary_function<StorageEngine *, bool>
316
316
{
317
317
  Session& session;
318
318
  const TableIdentifier &identifier;
343
343
  }
344
344
 
345
345
  EngineVector::iterator iter=
346
 
    std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
347
 
                 StorageEngineDoesTableExist(session, identifier));
 
346
    find_if(vector_of_engines.begin(), vector_of_engines.end(),
 
347
            StorageEngineDoesTableExist(session, identifier));
348
348
 
349
349
  if (iter == vector_of_engines.end())
350
350
  {
356
356
 
357
357
bool plugin::StorageEngine::doDoesTableExist(Session&, const drizzled::TableIdentifier&)
358
358
{
359
 
  std::cerr << " Engine was called for doDoesTableExist() and does not implement it: " << this->getName() << "\n";
 
359
  cerr << " Engine was called for doDoesTableExist() and does not implement it: " << this->getName() << "\n";
360
360
  assert(0);
361
361
  return false;
362
362
}
368
368
*/
369
369
int StorageEngine::getTableDefinition(Session& session,
370
370
                                      const TableIdentifier &identifier,
371
 
                                      message::table::shared_ptr &table_message,
 
371
                                      message::Table &table_message,
372
372
                                      bool include_temporary_tables)
373
373
{
374
374
  int err= ENOENT;
375
375
 
376
376
  if (include_temporary_tables)
377
377
  {
378
 
    Table *table= session.find_temporary_table(identifier);
379
 
    if (table)
380
 
    {
381
 
      table_message.reset(new message::Table(*table->getShare()->getTableProto()));
 
378
    if (session.doGetTableDefinition(identifier, table_message) == EEXIST)
382
379
      return EEXIST;
383
 
    }
384
 
  }
385
 
 
386
 
  drizzled::message::table::shared_ptr table_ptr;
387
 
  if ((table_ptr= drizzled::message::Cache::singleton().find(identifier)))
388
 
  {
389
 
    table_message= table_ptr;
390
 
  }
391
 
 
392
 
  message::Table message;
 
380
  }
 
381
 
393
382
  EngineVector::iterator iter=
394
 
    std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
395
 
                 StorageEngineGetTableDefinition(session, identifier, message, err));
 
383
    find_if(vector_of_engines.begin(), vector_of_engines.end(),
 
384
            StorageEngineGetTableDefinition(session, identifier, table_message, err));
396
385
 
397
386
  if (iter == vector_of_engines.end())
398
387
  {
399
388
    return ENOENT;
400
389
  }
401
 
  table_message.reset(new message::Table(message));
402
 
 
403
 
 drizzled::message::Cache::singleton().insert(identifier, table_message);
404
390
 
405
391
  return err;
406
392
}
443
429
{
444
430
  int error= 0;
445
431
  int error_proto;
446
 
  message::table::shared_ptr src_proto;
 
432
  message::Table src_proto;
447
433
  StorageEngine *engine;
448
434
 
449
435
  error_proto= StorageEngine::getTableDefinition(session, identifier, src_proto);
450
436
 
451
437
  if (error_proto == ER_CORRUPT_TABLE_DEFINITION)
452
438
  {
453
 
    std::string error_message;
454
 
    identifier.getSQLPath(error_message);
 
439
    string error_message;
455
440
 
 
441
    error_message.append(const_cast<TableIdentifier &>(identifier).getSQLPath());
456
442
    error_message.append(" : ");
457
 
    error_message.append(src_proto->InitializationErrorString());
 
443
    error_message.append(src_proto.InitializationErrorString());
458
444
 
459
445
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), error_message.c_str());
460
446
 
461
447
    return ER_CORRUPT_TABLE_DEFINITION;
462
448
  }
463
449
 
464
 
  if (src_proto)
465
 
    engine= StorageEngine::findByName(session, src_proto->engine().name());
466
 
  else
467
 
    engine= StorageEngine::findByName(session, "");
 
450
  engine= StorageEngine::findByName(session, src_proto.engine().name());
468
451
 
469
452
  if (not engine)
470
453
  {
471
 
    std::string error_message;
472
 
    identifier.getSQLPath(error_message);
473
 
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), error_message.c_str());
 
454
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), const_cast<TableIdentifier &>(identifier).getSQLPath().c_str());
474
455
 
475
456
    return ER_CORRUPT_TABLE_DEFINITION;
476
457
  }
504
485
    }
505
486
  }
506
487
 
507
 
  drizzled::message::Cache::singleton().erase(identifier);
508
488
 
509
489
  return error;
510
490
}
556
536
 
557
537
    if (error)
558
538
    {
559
 
      std::string path;
560
 
      identifier.getSQLPath(path);
561
 
      my_error(ER_CANT_CREATE_TABLE, MYF(ME_BELL+ME_WAITTANG), path.c_str(), error);
 
539
      my_error(ER_CANT_CREATE_TABLE, MYF(ME_BELL+ME_WAITTANG), const_cast<TableIdentifier &>(identifier).getSQLPath().c_str(), error);
562
540
    }
563
541
 
564
542
    table.delete_table();
573
551
}
574
552
 
575
553
class AddTableIdentifier : 
576
 
  public std::unary_function<StorageEngine *, void>
 
554
  public unary_function<StorageEngine *, void>
577
555
{
578
556
  CachedDirectory &directory;
579
557
  const SchemaIdentifier &identifier;
580
 
  TableIdentifier::vector &set_of_identifiers;
 
558
  TableIdentifiers &set_of_identifiers;
581
559
 
582
560
public:
583
561
 
584
 
  AddTableIdentifier(CachedDirectory &directory_arg, const SchemaIdentifier &identifier_arg, TableIdentifier::vector &of_names) :
 
562
  AddTableIdentifier(CachedDirectory &directory_arg, const SchemaIdentifier &identifier_arg, TableIdentifiers &of_names) :
585
563
    directory(directory_arg),
586
564
    identifier(identifier_arg),
587
565
    set_of_identifiers(of_names)
595
573
};
596
574
 
597
575
 
598
 
void StorageEngine::getIdentifiers(Session &session, const SchemaIdentifier &schema_identifier, TableIdentifier::vector &set_of_identifiers)
 
576
void StorageEngine::getIdentifiers(Session &session, const SchemaIdentifier &schema_identifier, TableIdentifiers &set_of_identifiers)
599
577
{
600
578
  static SchemaIdentifier INFORMATION_SCHEMA_IDENTIFIER("information_schema");
601
579
  static SchemaIdentifier DATA_DICTIONARY_IDENTIFIER("data_dictionary");
612
590
    {
613
591
      errno= directory.getError();
614
592
      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
 
      }
 
593
        my_error(ER_BAD_DB_ERROR, MYF(ME_BELL+ME_WAITTANG), const_cast<SchemaIdentifier &>(schema_identifier).getSQLPath().c_str());
620
594
      else
621
 
      {
622
595
        my_error(ER_CANT_READ_DIR, MYF(ME_BELL+ME_WAITTANG), directory.getPath(), errno);
623
 
      }
624
 
 
625
596
      return;
626
597
    }
627
598
  }
628
599
 
629
 
  std::for_each(vector_of_engines.begin(), vector_of_engines.end(),
630
 
                AddTableIdentifier(directory, schema_identifier, set_of_identifiers));
 
600
  for_each(vector_of_engines.begin(), vector_of_engines.end(),
 
601
           AddTableIdentifier(directory, schema_identifier, set_of_identifiers));
631
602
 
632
603
  session.doGetTableIdentifiers(directory, schema_identifier, set_of_identifiers);
633
604
}
634
605
 
635
 
class DropTable: public std::unary_function<TableIdentifier&, bool>
 
606
class DropTable: public unary_function<TableIdentifier&, bool>
636
607
{
637
608
  Session &session;
638
609
  StorageEngine *engine;
651
622
};
652
623
 
653
624
/* This will later be converted to TableIdentifiers */
654
 
class DropTables: public std::unary_function<StorageEngine *, void>
 
625
class DropTables: public unary_function<StorageEngine *, void>
655
626
{
656
627
  Session &session;
657
 
  TableIdentifier::vector &table_identifiers;
 
628
  TableIdentifiers &table_identifiers;
658
629
 
659
630
public:
660
631
 
661
 
  DropTables(Session &session_arg, TableIdentifier::vector &table_identifiers_arg) :
 
632
  DropTables(Session &session_arg, TableIdentifiers &table_identifiers_arg) :
662
633
    session(session_arg),
663
634
    table_identifiers(table_identifiers_arg)
664
635
  { }
667
638
  {
668
639
    // True returning from DropTable means the table has been successfully
669
640
    // 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)),
 
641
    table_identifiers.erase(remove_if(table_identifiers.begin(),
 
642
                                      table_identifiers.end(),
 
643
                                      DropTable(session, engine)),
673
644
                            table_identifiers.end());
674
645
  }
675
646
};
682
653
void StorageEngine::removeLostTemporaryTables(Session &session, const char *directory)
683
654
{
684
655
  CachedDirectory dir(directory, set_of_table_definition_ext);
685
 
  TableIdentifier::vector table_identifiers;
 
656
  TableIdentifiers table_identifiers;
686
657
 
687
658
  if (dir.fail())
688
659
  {
698
669
       fileIter != files.end(); fileIter++)
699
670
  {
700
671
    size_t length;
701
 
    std::string path;
 
672
    string path;
702
673
    CachedDirectory::Entry *entry= *fileIter;
703
674
 
704
675
    /* We remove the file extension. */
716
687
    }
717
688
  }
718
689
 
719
 
  std::for_each(vector_of_engines.begin(), vector_of_engines.end(),
720
 
                DropTables(session, table_identifiers));
721
 
 
 
690
  for_each(vector_of_engines.begin(), vector_of_engines.end(),
 
691
           DropTables(session, table_identifiers));
 
692
  
722
693
  /*
723
694
    Now we just clean up anything that might left over.
724
695
 
725
696
    We rescan because some of what might have been there should
726
697
    now be all nice and cleaned up.
727
698
  */
728
 
  std::set<std::string> all_exts= set_of_table_definition_ext;
 
699
  set<string> all_exts= set_of_table_definition_ext;
729
700
 
730
701
  for (EngineVector::iterator iter= vector_of_engines.begin();
731
702
       iter != vector_of_engines.end() ; iter++)
740
711
  for (CachedDirectory::Entries::iterator fileIter= files.begin();
741
712
       fileIter != files.end(); fileIter++)
742
713
  {
743
 
    std::string path;
 
714
    string path;
744
715
    CachedDirectory::Entry *entry= *fileIter;
745
716
 
746
717
    path+= directory;
1007
978
 
1008
979
int StorageEngine::deleteDefinitionFromPath(const TableIdentifier &identifier)
1009
980
{
1010
 
  std::string path(identifier.getPath());
 
981
  string path(identifier.getPath());
1011
982
 
1012
983
  path.append(DEFAULT_DEFINITION_FILE_EXT);
1013
984
 
1017
988
int StorageEngine::renameDefinitionFromPath(const TableIdentifier &dest, const TableIdentifier &src)
1018
989
{
1019
990
  message::Table table_message;
1020
 
  std::string src_path(src.getPath());
1021
 
  std::string dest_path(dest.getPath());
 
991
  string src_path(src.getPath());
 
992
  string dest_path(dest.getPath());
1022
993
 
1023
994
  src_path.append(DEFAULT_DEFINITION_FILE_EXT);
1024
995
  dest_path.append(DEFAULT_DEFINITION_FILE_EXT);
1046
1017
int StorageEngine::writeDefinitionFromPath(const TableIdentifier &identifier, message::Table &table_message)
1047
1018
{
1048
1019
  char definition_file_tmp[FN_REFLEN];
1049
 
  std::string file_name(identifier.getPath());
 
1020
  string file_name(identifier.getPath());
1050
1021
 
1051
1022
  file_name.append(DEFAULT_DEFINITION_FILE_EXT);
1052
1023
 
1065
1036
 
1066
1037
  bool success;
1067
1038
 
1068
 
  try
1069
 
  {
 
1039
  try {
1070
1040
    success= table_message.SerializeToZeroCopyStream(output);
1071
1041
  }
1072
1042
  catch (...)
1116
1086
  return 0;
1117
1087
}
1118
1088
 
1119
 
class CanCreateTable: public std::unary_function<StorageEngine *, bool>
 
1089
class CanCreateTable: public unary_function<StorageEngine *, bool>
1120
1090
{
1121
1091
  const TableIdentifier &identifier;
1122
1092
 
1138
1108
bool StorageEngine::canCreateTable(const TableIdentifier &identifier)
1139
1109
{
1140
1110
  EngineVector::iterator iter=
1141
 
    std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
1142
 
                 CanCreateTable(identifier));
 
1111
    find_if(vector_of_engines.begin(), vector_of_engines.end(),
 
1112
            CanCreateTable(identifier));
1143
1113
 
1144
1114
  if (iter == vector_of_engines.end())
1145
1115
  {
1151
1121
 
1152
1122
bool StorageEngine::readTableFile(const std::string &path, message::Table &table_message)
1153
1123
{
1154
 
  std::fstream input(path.c_str(), std::ios::in | std::ios::binary);
 
1124
  fstream input(path.c_str(), ios::in | ios::binary);
1155
1125
 
1156
1126
  if (input.good())
1157
1127
  {