~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/storage_engine.cc

  • Committer: Monty Taylor
  • Date: 2010-05-15 18:23:34 UTC
  • mto: (1530.6.1)
  • mto: This revision was merged to the branch mainline in revision 1556.
  • Revision ID: mordred@inaugust.com-20100515182334-bgbmwij0mioklajx
Renamed classes that were in drizzled::plugin but which were not meant
for consumption by plugin authors to drizzled::module - since they
really have to do with plugin module loading. This way when we
look in drizzled/plugin, we see nothing but plugin interfaces. Win.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
#include <google/protobuf/io/zero_copy_stream.h>
33
33
#include <google/protobuf/io/zero_copy_stream_impl.h>
34
34
 
 
35
#include "drizzled/my_hash.h"
35
36
#include "drizzled/cached_directory.h"
36
37
 
37
38
#include <drizzled/definitions.h>
52
53
#include "drizzled/db.h"
53
54
 
54
55
#include <drizzled/table_proto.h>
55
 
#include <drizzled/plugin/event_observer.h>
56
 
 
57
 
#include <drizzled/table/shell.h>
58
 
 
59
 
#include "drizzled/message/cache.h"
60
 
 
61
 
#include <boost/algorithm/string/compare.hpp>
62
56
 
63
57
static bool shutdown_has_begun= false; // Once we put in the container for the vector/etc for engines this will go away.
64
58
 
 
59
using namespace std;
 
60
 
65
61
namespace drizzled
66
62
{
67
63
 
71
67
static EngineVector vector_of_engines;
72
68
static EngineVector vector_of_schema_engines;
73
69
 
74
 
const std::string DEFAULT_STRING("default");
75
70
const std::string UNKNOWN_STRING("UNKNOWN");
76
71
const std::string DEFAULT_DEFINITION_FILE_EXT(".dfe");
77
72
 
82
77
  return vector_of_schema_engines;
83
78
}
84
79
 
85
 
StorageEngine::StorageEngine(const std::string name_arg,
86
 
                             const std::bitset<HTON_BIT_SIZE> &flags_arg) :
 
80
StorageEngine::StorageEngine(const string name_arg,
 
81
                             const bitset<HTON_BIT_SIZE> &flags_arg) :
87
82
  Plugin(name_arg, "StorageEngine"),
88
83
  MonitoredInTransaction(), /* This gives the storage engine a "slot" or ID */
89
84
  flags(flags_arg)
101
96
}
102
97
 
103
98
 
104
 
int StorageEngine::renameTable(Session &session, const TableIdentifier &from, const TableIdentifier &to)
 
99
int StorageEngine::renameTable(Session &session, TableIdentifier &from, TableIdentifier &to)
105
100
{
106
 
  int error;
107
101
  setTransactionReadWrite(session);
108
102
 
109
 
  if (unlikely(plugin::EventObserver::beforeRenameTable(session, from, to)))
110
 
  {
111
 
    error= ER_EVENT_OBSERVER_PLUGIN;
112
 
  }
113
 
  else
114
 
  {
115
 
    error =  doRenameTable(session, from, to);
116
 
    if (unlikely(plugin::EventObserver::afterRenameTable(session, from, to, error)))
117
 
    {
118
 
      error= ER_EVENT_OBSERVER_PLUGIN;
119
 
    }
120
 
  }
121
 
  
122
 
  return error;
 
103
  return doRenameTable(session, from, to);
123
104
}
124
105
 
125
106
/**
137
118
  @retval
138
119
    !0  Error
139
120
*/
140
 
int StorageEngine::doDropTable(Session&, const TableIdentifier &identifier)
 
121
int StorageEngine::doDropTable(Session&, TableIdentifier &identifier)
141
122
                               
142
123
{
143
124
  int error= 0;
192
173
}
193
174
 
194
175
class FindEngineByName
195
 
  : public std::unary_function<StorageEngine *, bool>
 
176
  : public unary_function<StorageEngine *, bool>
196
177
{
197
 
  const std::string &predicate;
 
178
  const string &target;
198
179
 
199
180
public:
200
 
  explicit FindEngineByName(const std::string &target_arg) :
201
 
    predicate(target_arg)
 
181
  explicit FindEngineByName(const string &target_arg) :
 
182
    target(target_arg)
202
183
  {
203
184
  }
204
 
 
205
185
  result_type operator() (argument_type engine)
206
186
  {
207
 
    return boost::iequals(engine->getName(), predicate);
 
187
    string engine_name(engine->getName());
 
188
 
 
189
    transform(engine_name.begin(), engine_name.end(),
 
190
              engine_name.begin(), ::tolower);
 
191
    return engine_name == target;
208
192
  }
209
193
};
210
194
 
211
 
StorageEngine *StorageEngine::findByName(const std::string &predicate)
 
195
StorageEngine *StorageEngine::findByName(const string &find_str)
212
196
{
213
 
  EngineVector::iterator iter= std::find_if(vector_of_engines.begin(),
214
 
                                            vector_of_engines.end(),
215
 
                                            FindEngineByName(predicate));
 
197
  string search_string(find_str);
 
198
  transform(search_string.begin(), search_string.end(),
 
199
            search_string.begin(), ::tolower);
 
200
 
 
201
  
 
202
  EngineVector::iterator iter= find_if(vector_of_engines.begin(),
 
203
                                       vector_of_engines.end(),
 
204
                                       FindEngineByName(search_string));
216
205
  if (iter != vector_of_engines.end())
217
206
  {
218
207
    StorageEngine *engine= *iter;
223
212
  return NULL;
224
213
}
225
214
 
226
 
StorageEngine *StorageEngine::findByName(Session& session, const std::string &predicate)
 
215
StorageEngine *StorageEngine::findByName(Session& session, const string &find_str)
227
216
{
228
 
  if (boost::iequals(predicate, DEFAULT_STRING))
 
217
  string search_string(find_str);
 
218
  transform(search_string.begin(), search_string.end(),
 
219
            search_string.begin(), ::tolower);
 
220
 
 
221
  if (search_string.compare("default") == 0)
229
222
    return session.getDefaultStorageEngine();
230
223
 
231
 
  EngineVector::iterator iter= std::find_if(vector_of_engines.begin(),
232
 
                                            vector_of_engines.end(),
233
 
                                            FindEngineByName(predicate));
 
224
  EngineVector::iterator iter= find_if(vector_of_engines.begin(),
 
225
                                       vector_of_engines.end(),
 
226
                                       FindEngineByName(search_string));
234
227
  if (iter != vector_of_engines.end())
235
228
  {
236
229
    StorageEngine *engine= *iter;
241
234
  return NULL;
242
235
}
243
236
 
244
 
class StorageEngineCloseConnection : public std::unary_function<StorageEngine *, void>
 
237
class StorageEngineCloseConnection : public unary_function<StorageEngine *, void>
245
238
{
246
239
  Session *session;
247
240
public:
263
256
*/
264
257
void StorageEngine::closeConnection(Session* session)
265
258
{
266
 
  std::for_each(vector_of_engines.begin(), vector_of_engines.end(),
267
 
                StorageEngineCloseConnection(session));
 
259
  for_each(vector_of_engines.begin(), vector_of_engines.end(),
 
260
           StorageEngineCloseConnection(session));
268
261
}
269
262
 
270
263
bool StorageEngine::flushLogs(StorageEngine *engine)
271
264
{
272
265
  if (engine == NULL)
273
266
  {
274
 
    if (std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
275
 
                     std::mem_fun(&StorageEngine::flush_logs))
 
267
    if (find_if(vector_of_engines.begin(), vector_of_engines.end(),
 
268
                mem_fun(&StorageEngine::flush_logs))
276
269
        != vector_of_engines.begin())
277
270
      return true;
278
271
  }
284
277
  return false;
285
278
}
286
279
 
287
 
class StorageEngineGetTableDefinition: public std::unary_function<StorageEngine *,bool>
 
280
class StorageEngineGetTableDefinition: public unary_function<StorageEngine *,bool>
288
281
{
289
282
  Session& session;
290
 
  const TableIdentifier &identifier;
 
283
  TableIdentifier &identifier;
291
284
  message::Table &table_message;
292
285
  int &err;
293
286
 
294
287
public:
295
288
  StorageEngineGetTableDefinition(Session& session_arg,
296
 
                                  const TableIdentifier &identifier_arg,
 
289
                                  TableIdentifier &identifier_arg,
297
290
                                  message::Table &table_message_arg,
298
291
                                  int &err_arg) :
299
292
    session(session_arg), 
312
305
  }
313
306
};
314
307
 
315
 
class StorageEngineDoesTableExist: public std::unary_function<StorageEngine *, bool>
 
308
class StorageEngineDoesTableExist: public unary_function<StorageEngine *, bool>
316
309
{
317
310
  Session& session;
318
 
  const TableIdentifier &identifier;
 
311
  TableIdentifier &identifier;
319
312
 
320
313
public:
321
 
  StorageEngineDoesTableExist(Session& session_arg, const TableIdentifier &identifier_arg) :
 
314
  StorageEngineDoesTableExist(Session& session_arg, TableIdentifier &identifier_arg) :
322
315
    session(session_arg), 
323
316
    identifier(identifier_arg) 
324
317
  { }
333
326
  Utility method which hides some of the details of getTableDefinition()
334
327
*/
335
328
bool plugin::StorageEngine::doesTableExist(Session &session,
336
 
                                           const TableIdentifier &identifier,
 
329
                                           TableIdentifier &identifier,
337
330
                                           bool include_temporary_tables)
338
331
{
339
332
  if (include_temporary_tables)
343
336
  }
344
337
 
345
338
  EngineVector::iterator iter=
346
 
    std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
347
 
                 StorageEngineDoesTableExist(session, identifier));
 
339
    find_if(vector_of_engines.begin(), vector_of_engines.end(),
 
340
            StorageEngineDoesTableExist(session, identifier));
348
341
 
349
342
  if (iter == vector_of_engines.end())
350
343
  {
354
347
  return true;
355
348
}
356
349
 
357
 
bool plugin::StorageEngine::doDoesTableExist(Session&, const drizzled::TableIdentifier&)
 
350
bool plugin::StorageEngine::doDoesTableExist(Session&, TableIdentifier&)
358
351
{
359
 
  std::cerr << " Engine was called for doDoesTableExist() and does not implement it: " << this->getName() << "\n";
 
352
  cerr << " Engine was called for doDoesTableExist() and does not implement it: " << this->getName() << "\n";
360
353
  assert(0);
361
354
  return false;
362
355
}
367
360
  or any dropped tables that need to be removed from disk
368
361
*/
369
362
int StorageEngine::getTableDefinition(Session& session,
370
 
                                      const TableIdentifier &identifier,
371
 
                                      message::table::shared_ptr &table_message,
 
363
                                      TableIdentifier &identifier,
 
364
                                      message::Table &table_message,
372
365
                                      bool include_temporary_tables)
373
366
{
374
367
  int err= ENOENT;
375
368
 
376
369
  if (include_temporary_tables)
377
370
  {
378
 
    Table *table= session.find_temporary_table(identifier);
379
 
    if (table)
380
 
    {
381
 
      table_message.reset(new message::Table(*table->getShare()->getTableProto()));
 
371
    if (session.doGetTableDefinition(identifier, table_message) == EEXIST)
382
372
      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;
 
373
  }
 
374
 
393
375
  EngineVector::iterator iter=
394
 
    std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
395
 
                 StorageEngineGetTableDefinition(session, identifier, message, err));
 
376
    find_if(vector_of_engines.begin(), vector_of_engines.end(),
 
377
            StorageEngineGetTableDefinition(session, identifier, table_message, err));
396
378
 
397
379
  if (iter == vector_of_engines.end())
398
380
  {
399
381
    return ENOENT;
400
382
  }
401
 
  table_message.reset(new message::Table(message));
402
 
 
403
 
 drizzled::message::Cache::singleton().insert(identifier, table_message);
404
383
 
405
384
  return err;
406
385
}
439
418
   returns ENOENT if the file doesn't exists.
440
419
*/
441
420
int StorageEngine::dropTable(Session& session,
442
 
                             const TableIdentifier &identifier)
 
421
                             TableIdentifier &identifier)
443
422
{
444
423
  int error= 0;
445
424
  int error_proto;
446
 
  message::table::shared_ptr src_proto;
 
425
  message::Table src_proto;
447
426
  StorageEngine *engine;
448
427
 
449
428
  error_proto= StorageEngine::getTableDefinition(session, identifier, src_proto);
450
429
 
451
430
  if (error_proto == ER_CORRUPT_TABLE_DEFINITION)
452
431
  {
453
 
    std::string error_message;
454
 
    identifier.getSQLPath(error_message);
 
432
    string error_message;
455
433
 
 
434
    error_message.append(identifier.getSQLPath());
456
435
    error_message.append(" : ");
457
 
    error_message.append(src_proto->InitializationErrorString());
 
436
    error_message.append(src_proto.InitializationErrorString());
458
437
 
459
438
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), error_message.c_str());
460
439
 
461
440
    return ER_CORRUPT_TABLE_DEFINITION;
462
441
  }
463
442
 
464
 
  if (src_proto)
465
 
    engine= StorageEngine::findByName(session, src_proto->engine().name());
466
 
  else
467
 
    engine= StorageEngine::findByName(session, "");
 
443
  engine= StorageEngine::findByName(session, src_proto.engine().name());
468
444
 
469
445
  if (not engine)
470
446
  {
471
 
    std::string error_message;
472
 
    identifier.getSQLPath(error_message);
473
 
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), error_message.c_str());
 
447
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), identifier.getSQLPath().c_str());
474
448
 
475
449
    return ER_CORRUPT_TABLE_DEFINITION;
476
450
  }
485
459
 
486
460
int StorageEngine::dropTable(Session& session,
487
461
                             StorageEngine &engine,
488
 
                             const TableIdentifier &identifier)
 
462
                             TableIdentifier &identifier)
489
463
{
490
464
  int error;
491
465
 
492
466
  engine.setTransactionReadWrite(session);
493
 
  
494
 
  if (unlikely(plugin::EventObserver::beforeDropTable(session, identifier)))
495
 
  {
496
 
    error= ER_EVENT_OBSERVER_PLUGIN;
497
 
  }
498
 
  else
499
 
  {
500
 
    error= engine.doDropTable(session, identifier);
501
 
    if (unlikely(plugin::EventObserver::afterDropTable(session, identifier, error)))
502
 
    {
503
 
      error= ER_EVENT_OBSERVER_PLUGIN;
504
 
    }
505
 
  }
506
 
 
507
 
  drizzled::message::Cache::singleton().erase(identifier);
 
467
  error= engine.doDropTable(session, identifier);
508
468
 
509
469
  return error;
510
470
}
519
479
   1  error
520
480
*/
521
481
int StorageEngine::createTable(Session &session,
522
 
                               const TableIdentifier &identifier,
 
482
                               TableIdentifier &identifier,
 
483
                               bool update_create_info,
523
484
                               message::Table& table_message)
524
485
{
525
486
  int error= 1;
526
 
  TableShare share(identifier);
527
 
  table::Shell table(share);
 
487
  Table table;
 
488
  TableShare share(identifier.getSchemaName().c_str(), 0, identifier.getTableName().c_str(), identifier.getPath().c_str());
528
489
  message::Table tmp_proto;
529
490
 
530
 
  if (share.parse_table_proto(session, table_message) || share.open_table_from_share(&session, identifier, "", 0, 0, table))
 
491
  if (share.parse_table_proto(session, table_message) || share.open_table_from_share(&session, "", 0, 0, table))
531
492
  { 
532
493
    // @note Error occured, we should probably do a little more here.
533
494
  }
534
495
  else
535
496
  {
 
497
    if (update_create_info)
 
498
      table.updateCreateInfo(&table_message);
 
499
 
536
500
    /* Check for legal operations against the Engine using the proto (if used) */
537
501
    if (table_message.type() == message::Table::TEMPORARY &&
538
502
        share.storage_engine->check_flag(HTON_BIT_TEMPORARY_NOT_SUPPORTED) == true)
556
520
 
557
521
    if (error)
558
522
    {
559
 
      std::string path;
560
 
      identifier.getSQLPath(path);
561
 
      my_error(ER_CANT_CREATE_TABLE, MYF(ME_BELL+ME_WAITTANG), path.c_str(), error);
 
523
      my_error(ER_CANT_CREATE_TABLE, MYF(ME_BELL+ME_WAITTANG), identifier.getSQLPath().c_str(), error);
562
524
    }
563
525
 
564
 
    table.delete_table();
 
526
    table.delete_table(false);
565
527
  }
566
528
 
567
529
  return(error != 0);
568
530
}
569
531
 
570
 
Cursor *StorageEngine::getCursor(Table &arg)
 
532
Cursor *StorageEngine::getCursor(TableShare &share, memory::Root *alloc)
571
533
{
572
 
  return create(arg);
 
534
  return create(share, alloc);
573
535
}
574
536
 
 
537
class AddTableName : 
 
538
  public unary_function<StorageEngine *, void>
 
539
{
 
540
  CachedDirectory &directory;
 
541
  SchemaIdentifier &identifier;
 
542
  TableNameList &set_of_names;
 
543
 
 
544
public:
 
545
 
 
546
  AddTableName(CachedDirectory &directory_arg, SchemaIdentifier &identifier_arg, set<string>& of_names) :
 
547
    directory(directory_arg),
 
548
    identifier(identifier_arg),
 
549
    set_of_names(of_names)
 
550
  {
 
551
  }
 
552
 
 
553
  result_type operator() (argument_type engine)
 
554
  {
 
555
    engine->doGetTableNames(directory, identifier, set_of_names);
 
556
  }
 
557
};
 
558
 
575
559
class AddTableIdentifier : 
576
 
  public std::unary_function<StorageEngine *, void>
 
560
  public unary_function<StorageEngine *, void>
577
561
{
578
562
  CachedDirectory &directory;
579
 
  const SchemaIdentifier &identifier;
580
 
  TableIdentifier::vector &set_of_identifiers;
 
563
  SchemaIdentifier &identifier;
 
564
  TableIdentifiers &set_of_identifiers;
581
565
 
582
566
public:
583
567
 
584
 
  AddTableIdentifier(CachedDirectory &directory_arg, const SchemaIdentifier &identifier_arg, TableIdentifier::vector &of_names) :
 
568
  AddTableIdentifier(CachedDirectory &directory_arg, SchemaIdentifier &identifier_arg, TableIdentifiers &of_names) :
585
569
    directory(directory_arg),
586
570
    identifier(identifier_arg),
587
571
    set_of_identifiers(of_names)
595
579
};
596
580
 
597
581
 
598
 
void StorageEngine::getIdentifiers(Session &session, const SchemaIdentifier &schema_identifier, TableIdentifier::vector &set_of_identifiers)
599
 
{
600
 
  static SchemaIdentifier INFORMATION_SCHEMA_IDENTIFIER("information_schema");
601
 
  static SchemaIdentifier DATA_DICTIONARY_IDENTIFIER("data_dictionary");
602
 
 
603
 
  CachedDirectory directory(schema_identifier.getPath(), set_of_table_definition_ext);
604
 
 
605
 
  if (schema_identifier == INFORMATION_SCHEMA_IDENTIFIER)
606
 
  { }
607
 
  else if (schema_identifier == DATA_DICTIONARY_IDENTIFIER)
608
 
  { }
609
 
  else
610
 
  {
611
 
    if (directory.fail())
612
 
    {
613
 
      errno= directory.getError();
614
 
      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
 
      }
620
 
      else
621
 
      {
622
 
        my_error(ER_CANT_READ_DIR, MYF(ME_BELL+ME_WAITTANG), directory.getPath(), errno);
623
 
      }
624
 
 
625
 
      return;
626
 
    }
627
 
  }
628
 
 
629
 
  std::for_each(vector_of_engines.begin(), vector_of_engines.end(),
630
 
                AddTableIdentifier(directory, schema_identifier, set_of_identifiers));
 
582
static SchemaIdentifier INFORMATION_SCHEMA_IDENTIFIER("information_schema");
 
583
static SchemaIdentifier DATA_DICTIONARY_IDENTIFIER("data_dictionary");
 
584
 
 
585
void StorageEngine::getTableNames(Session &session, SchemaIdentifier &schema_identifier, TableNameList &set_of_names)
 
586
{
 
587
  CachedDirectory directory(schema_identifier.getPath(), set_of_table_definition_ext);
 
588
 
 
589
  if (schema_identifier == INFORMATION_SCHEMA_IDENTIFIER)
 
590
  { }
 
591
  else if (schema_identifier == DATA_DICTIONARY_IDENTIFIER)
 
592
  { }
 
593
  else
 
594
  {
 
595
    if (directory.fail())
 
596
    {
 
597
      errno= directory.getError();
 
598
      if (errno == ENOENT)
 
599
        my_error(ER_BAD_DB_ERROR, MYF(ME_BELL+ME_WAITTANG), schema_identifier.getSQLPath().c_str());
 
600
      else
 
601
        my_error(ER_CANT_READ_DIR, MYF(ME_BELL+ME_WAITTANG), directory.getPath(), errno);
 
602
      return;
 
603
    }
 
604
  }
 
605
 
 
606
  for_each(vector_of_engines.begin(), vector_of_engines.end(),
 
607
           AddTableName(directory, schema_identifier, set_of_names));
 
608
 
 
609
  session.doGetTableNames(directory, schema_identifier, set_of_names);
 
610
}
 
611
 
 
612
void StorageEngine::getTableIdentifiers(Session &session, SchemaIdentifier &schema_identifier, TableIdentifiers &set_of_identifiers)
 
613
{
 
614
  CachedDirectory directory(schema_identifier.getPath(), set_of_table_definition_ext);
 
615
 
 
616
  if (schema_identifier == INFORMATION_SCHEMA_IDENTIFIER)
 
617
  { }
 
618
  else if (schema_identifier == DATA_DICTIONARY_IDENTIFIER)
 
619
  { }
 
620
  else
 
621
  {
 
622
    if (directory.fail())
 
623
    {
 
624
      errno= directory.getError();
 
625
      if (errno == ENOENT)
 
626
        my_error(ER_BAD_DB_ERROR, MYF(ME_BELL+ME_WAITTANG), schema_identifier.getSQLPath().c_str());
 
627
      else
 
628
        my_error(ER_CANT_READ_DIR, MYF(ME_BELL+ME_WAITTANG), directory.getPath(), errno);
 
629
      return;
 
630
    }
 
631
  }
 
632
 
 
633
  for_each(vector_of_engines.begin(), vector_of_engines.end(),
 
634
           AddTableIdentifier(directory, schema_identifier, set_of_identifiers));
631
635
 
632
636
  session.doGetTableIdentifiers(directory, schema_identifier, set_of_identifiers);
633
637
}
634
638
 
635
 
class DropTable: public std::unary_function<TableIdentifier&, bool>
 
639
class DropTable: public unary_function<TableIdentifier&, bool>
636
640
{
637
641
  Session &session;
638
642
  StorageEngine *engine;
651
655
};
652
656
 
653
657
/* This will later be converted to TableIdentifiers */
654
 
class DropTables: public std::unary_function<StorageEngine *, void>
 
658
class DropTables: public unary_function<StorageEngine *, void>
655
659
{
656
660
  Session &session;
657
 
  TableIdentifier::vector &table_identifiers;
 
661
  TableIdentifierList &table_identifiers;
658
662
 
659
663
public:
660
664
 
661
 
  DropTables(Session &session_arg, TableIdentifier::vector &table_identifiers_arg) :
 
665
  DropTables(Session &session_arg, TableIdentifierList &table_identifiers_arg) :
662
666
    session(session_arg),
663
667
    table_identifiers(table_identifiers_arg)
664
668
  { }
667
671
  {
668
672
    // True returning from DropTable means the table has been successfully
669
673
    // 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)),
 
674
    table_identifiers.erase(remove_if(table_identifiers.begin(),
 
675
                                      table_identifiers.end(),
 
676
                                      DropTable(session, engine)),
673
677
                            table_identifiers.end());
674
678
  }
675
679
};
682
686
void StorageEngine::removeLostTemporaryTables(Session &session, const char *directory)
683
687
{
684
688
  CachedDirectory dir(directory, set_of_table_definition_ext);
685
 
  TableIdentifier::vector table_identifiers;
 
689
  TableIdentifierList table_identifiers;
686
690
 
687
691
  if (dir.fail())
688
692
  {
698
702
       fileIter != files.end(); fileIter++)
699
703
  {
700
704
    size_t length;
701
 
    std::string path;
 
705
    string path;
702
706
    CachedDirectory::Entry *entry= *fileIter;
703
707
 
704
708
    /* We remove the file extension. */
716
720
    }
717
721
  }
718
722
 
719
 
  std::for_each(vector_of_engines.begin(), vector_of_engines.end(),
720
 
                DropTables(session, table_identifiers));
721
 
 
 
723
  for_each(vector_of_engines.begin(), vector_of_engines.end(),
 
724
           DropTables(session, table_identifiers));
 
725
  
722
726
  /*
723
727
    Now we just clean up anything that might left over.
724
728
 
725
729
    We rescan because some of what might have been there should
726
730
    now be all nice and cleaned up.
727
731
  */
728
 
  std::set<std::string> all_exts= set_of_table_definition_ext;
 
732
  set<string> all_exts= set_of_table_definition_ext;
729
733
 
730
734
  for (EngineVector::iterator iter= vector_of_engines.begin();
731
735
       iter != vector_of_engines.end() ; iter++)
740
744
  for (CachedDirectory::Entries::iterator fileIter= files.begin();
741
745
       fileIter != files.end(); fileIter++)
742
746
  {
743
 
    std::string path;
 
747
    string path;
744
748
    CachedDirectory::Entry *entry= *fileIter;
745
749
 
746
750
    path+= directory;
758
762
  @note
759
763
    In case of delete table it's only safe to use the following parts of
760
764
    the 'table' structure:
761
 
    - table->getShare()->path
 
765
    - table->s->path
762
766
    - table->alias
763
767
*/
764
768
void StorageEngine::print_error(int error, myf errflag, Table &table)
795
799
    {
796
800
      const char *err_msg= ER(ER_DUP_ENTRY_WITH_KEY_NAME);
797
801
 
 
802
      if (key_nr == 0 &&
 
803
          (table->key_info[0].key_part[0].field->flags &
 
804
           AUTO_INCREMENT_FLAG)
 
805
          && (current_session)->lex->sql_command == SQLCOM_ALTER_TABLE)
 
806
      {
 
807
        err_msg= ER(ER_DUP_ENTRY_AUTOINCREMENT_CASE);
 
808
      }
 
809
 
798
810
      print_keydup_error(key_nr, err_msg, *table);
799
 
 
800
811
      return;
801
812
    }
802
813
    textno=ER_DUP_KEY;
823
834
        str.length(max_length-4);
824
835
        str.append(STRING_WITH_LEN("..."));
825
836
      }
826
 
      my_error(ER_FOREIGN_DUPLICATE_KEY, MYF(0), table->getShare()->getTableName(),
 
837
      my_error(ER_FOREIGN_DUPLICATE_KEY, MYF(0), table->s->getTableName(),
827
838
        str.c_ptr(), key_nr+1);
828
839
      return;
829
840
    }
901
912
    break;
902
913
  case HA_ERR_NO_SUCH_TABLE:
903
914
    assert(table);
904
 
    my_error(ER_NO_SUCH_TABLE, MYF(0), table->getShare()->getSchemaName(),
905
 
             table->getShare()->getTableName());
 
915
    my_error(ER_NO_SUCH_TABLE, MYF(0), table->s->getSchemaName(),
 
916
             table->s->getTableName());
906
917
    return;
907
918
  case HA_ERR_RBR_LOGGING_FAILED:
908
919
    textno= ER_BINLOG_ROW_LOGGING_FAILED;
958
969
      return;
959
970
    }
960
971
  }
961
 
  my_error(textno, errflag, table->getShare()->getTableName(), error);
 
972
  my_error(textno, errflag, table->s->getTableName(), error);
962
973
}
963
974
 
964
975
 
1005
1016
}
1006
1017
 
1007
1018
 
1008
 
int StorageEngine::deleteDefinitionFromPath(const TableIdentifier &identifier)
 
1019
int StorageEngine::deleteDefinitionFromPath(TableIdentifier &identifier)
1009
1020
{
1010
 
  std::string path(identifier.getPath());
 
1021
  string path(identifier.getPath());
1011
1022
 
1012
1023
  path.append(DEFAULT_DEFINITION_FILE_EXT);
1013
1024
 
1014
1025
  return internal::my_delete(path.c_str(), MYF(0));
1015
1026
}
1016
1027
 
1017
 
int StorageEngine::renameDefinitionFromPath(const TableIdentifier &dest, const TableIdentifier &src)
 
1028
int StorageEngine::renameDefinitionFromPath(TableIdentifier &dest, TableIdentifier &src)
1018
1029
{
1019
1030
  message::Table table_message;
1020
 
  std::string src_path(src.getPath());
1021
 
  std::string dest_path(dest.getPath());
 
1031
  string src_path(src.getPath());
 
1032
  string dest_path(dest.getPath());
1022
1033
 
1023
1034
  src_path.append(DEFAULT_DEFINITION_FILE_EXT);
1024
1035
  dest_path.append(DEFAULT_DEFINITION_FILE_EXT);
1043
1054
  return error;
1044
1055
}
1045
1056
 
1046
 
int StorageEngine::writeDefinitionFromPath(const TableIdentifier &identifier, message::Table &table_message)
 
1057
int StorageEngine::writeDefinitionFromPath(TableIdentifier &identifier, message::Table &table_message)
1047
1058
{
1048
1059
  char definition_file_tmp[FN_REFLEN];
1049
 
  std::string file_name(identifier.getPath());
 
1060
  string file_name(identifier.getPath());
1050
1061
 
1051
1062
  file_name.append(DEFAULT_DEFINITION_FILE_EXT);
1052
1063
 
1063
1074
  google::protobuf::io::ZeroCopyOutputStream* output=
1064
1075
    new google::protobuf::io::FileOutputStream(fd);
1065
1076
 
1066
 
  bool success;
1067
 
 
1068
 
  try
1069
 
  {
1070
 
    success= table_message.SerializeToZeroCopyStream(output);
1071
 
  }
1072
 
  catch (...)
1073
 
  {
1074
 
    success= false;
1075
 
  }
1076
 
 
1077
 
  if (not success)
 
1077
  if (not table_message.SerializeToZeroCopyStream(output))
1078
1078
  {
1079
1079
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
1080
1080
             table_message.InitializationErrorString().c_str());
1116
1116
  return 0;
1117
1117
}
1118
1118
 
1119
 
class CanCreateTable: public std::unary_function<StorageEngine *, bool>
 
1119
class CanCreateTable: public unary_function<StorageEngine *, bool>
1120
1120
{
1121
 
  const TableIdentifier &identifier;
 
1121
  TableIdentifier &identifier;
1122
1122
 
1123
1123
public:
1124
 
  CanCreateTable(const TableIdentifier &identifier_arg) :
 
1124
  CanCreateTable(TableIdentifier &identifier_arg) :
1125
1125
    identifier(identifier_arg)
1126
1126
  { }
1127
1127
 
1135
1135
/**
1136
1136
  @note on success table can be created.
1137
1137
*/
1138
 
bool StorageEngine::canCreateTable(const TableIdentifier &identifier)
 
1138
bool StorageEngine::canCreateTable(drizzled::TableIdentifier &identifier)
1139
1139
{
1140
1140
  EngineVector::iterator iter=
1141
 
    std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
1142
 
                 CanCreateTable(identifier));
 
1141
    find_if(vector_of_engines.begin(), vector_of_engines.end(),
 
1142
            CanCreateTable(identifier));
1143
1143
 
1144
1144
  if (iter == vector_of_engines.end())
1145
1145
  {
1151
1151
 
1152
1152
bool StorageEngine::readTableFile(const std::string &path, message::Table &table_message)
1153
1153
{
1154
 
  std::fstream input(path.c_str(), std::ios::in | std::ios::binary);
 
1154
  fstream input(path.c_str(), ios::in | ios::binary);
1155
1155
 
1156
1156
  if (input.good())
1157
1157
  {
1158
 
    try {
1159
 
      if (table_message.ParseFromIstream(&input))
1160
 
      {
1161
 
        return true;
1162
 
      }
1163
 
    }
1164
 
    catch (...)
 
1158
    if (table_message.ParseFromIstream(&input))
1165
1159
    {
1166
 
      my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
1167
 
               table_message.InitializationErrorString().empty() ? "": table_message.InitializationErrorString().c_str());
 
1160
      return true;
1168
1161
    }
 
1162
 
 
1163
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
 
1164
             table_message.InitializationErrorString().c_str());
1169
1165
  }
1170
1166
  else
1171
1167
  {