~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/storage_engine.cc

  • Committer: Brian Aker
  • Date: 2010-10-15 03:36:56 UTC
  • mfrom: (1843.7.6 refactor)
  • Revision ID: brian@tangent.org-20101015033656-9u3aptcc6ipoc1vj
This removes two of our dead plugins (the bad schedulers).
Also this fixes help messages to say "schema" instead of "database"
everywhere..

Show diffs side-by-side

added added

removed removed

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