~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/storage_engine.cc

  • Committer: Mark Atwood
  • Date: 2011-05-08 15:52:16 UTC
  • mfrom: (2302.1.8 refactor)
  • Revision ID: me@mark.atwood.name-20110508155216-bkg30r4ciqq8xy62
mergeĀ lp:~olafvdspek/drizzle/refactor

Show diffs side-by-side

added added

removed removed

Lines of Context:
62
62
namespace drizzled {
63
63
namespace plugin {
64
64
 
65
 
static EngineVector vector_of_engines;
66
 
static EngineVector vector_of_schema_engines;
 
65
static EngineVector g_engines;
 
66
static EngineVector g_schema_engines;
67
67
 
68
68
const std::string DEFAULT_STRING("default");
69
69
const std::string UNKNOWN_STRING("UNKNOWN");
73
73
 
74
74
EngineVector &StorageEngine::getSchemaEngines()
75
75
{
76
 
  return vector_of_schema_engines;
 
76
  return g_schema_engines;
77
77
}
78
78
 
79
79
StorageEngine::StorageEngine(const std::string name_arg,
94
94
  statement_ctx.markModifiedNonTransData();
95
95
}
96
96
 
97
 
 
98
97
int StorageEngine::renameTable(Session &session, const identifier::Table &from, const identifier::Table &to)
99
98
{
100
 
  int error;
101
99
  setTransactionReadWrite(session);
102
 
 
103
100
  if (unlikely(plugin::EventObserver::beforeRenameTable(session, from, to)))
104
 
  {
 
101
    return ER_EVENT_OBSERVER_PLUGIN;
 
102
  int error= doRenameTable(session, from, to);
 
103
  if (unlikely(plugin::EventObserver::afterRenameTable(session, from, to, error)))
105
104
    error= ER_EVENT_OBSERVER_PLUGIN;
106
 
  }
107
 
  else
108
 
  {
109
 
    error =  doRenameTable(session, from, to);
110
 
    if (unlikely(plugin::EventObserver::afterRenameTable(session, from, to, error)))
111
 
    {
112
 
      error= ER_EVENT_OBSERVER_PLUGIN;
113
 
    }
114
 
  }
115
 
  
116
105
  return error;
117
106
}
118
107
 
132
121
    !0  Error
133
122
*/
134
123
int StorageEngine::doDropTable(Session&, const identifier::Table &identifier)
135
 
                               
136
124
{
137
125
  int error= 0;
138
126
  int enoent_or_zero= ENOENT;                   // Error if no file was deleted
159
147
 
160
148
bool StorageEngine::addPlugin(StorageEngine *engine)
161
149
{
162
 
 
163
 
  vector_of_engines.push_back(engine);
 
150
  g_engines.push_back(engine);
164
151
 
165
152
  if (engine->getTableDefinitionFileExtension().length())
166
153
  {
169
156
  }
170
157
 
171
158
  if (engine->check_flag(HTON_BIT_SCHEMA_DICTIONARY))
172
 
    vector_of_schema_engines.push_back(engine);
 
159
    g_schema_engines.push_back(engine);
173
160
 
174
161
  return false;
175
162
}
176
163
 
177
164
void StorageEngine::removePlugin(StorageEngine *)
178
165
{
179
 
  if (shutdown_has_begun == false)
180
 
  {
181
 
    vector_of_engines.clear();
182
 
    vector_of_schema_engines.clear();
183
 
 
184
 
    shutdown_has_begun= true;
185
 
  }
 
166
  if (shutdown_has_begun)
 
167
    return;
 
168
  shutdown_has_begun= true;
 
169
  g_engines.clear();
 
170
  g_schema_engines.clear();
186
171
}
187
172
 
188
 
class FindEngineByName
189
 
  : public std::unary_function<StorageEngine *, bool>
190
 
{
191
 
  const std::string &predicate;
192
 
 
193
 
public:
194
 
  explicit FindEngineByName(const std::string &target_arg) :
195
 
    predicate(target_arg)
196
 
  {
197
 
  }
198
 
 
199
 
  result_type operator() (argument_type engine)
200
 
  {
201
 
    return boost::iequals(engine->getName(), predicate);
202
 
  }
203
 
};
204
 
 
205
173
StorageEngine *StorageEngine::findByName(const std::string &predicate)
206
174
{
207
 
  EngineVector::iterator iter= std::find_if(vector_of_engines.begin(),
208
 
                                            vector_of_engines.end(),
209
 
                                            FindEngineByName(predicate));
210
 
  if (iter != vector_of_engines.end())
 
175
  BOOST_FOREACH(EngineVector::reference it, g_engines)
211
176
  {
212
 
    StorageEngine *engine= *iter;
213
 
    if (engine->is_user_selectable())
214
 
      return engine;
 
177
    if (not boost::iequals(it->getName(), predicate))
 
178
      continue;
 
179
    if (it->is_user_selectable())
 
180
      return it;
 
181
    break;
215
182
  }
216
 
 
217
183
  return NULL;
218
184
}
219
185
 
221
187
{
222
188
  if (boost::iequals(predicate, DEFAULT_STRING))
223
189
    return session.getDefaultStorageEngine();
224
 
 
225
 
  EngineVector::iterator iter= std::find_if(vector_of_engines.begin(),
226
 
                                            vector_of_engines.end(),
227
 
                                            FindEngineByName(predicate));
228
 
  if (iter != vector_of_engines.end())
229
 
  {
230
 
    StorageEngine *engine= *iter;
231
 
    if (engine->is_user_selectable())
232
 
      return engine;
233
 
  }
234
 
 
235
 
  return NULL;
 
190
  return findByName(predicate);
236
191
}
237
192
 
238
 
class StorageEngineCloseConnection : public std::unary_function<StorageEngine *, void>
239
 
{
240
 
  Session *session;
241
 
public:
242
 
  StorageEngineCloseConnection(Session *session_arg) : session(session_arg) {}
243
 
  /*
244
 
    there's no need to rollback here as all transactions must
245
 
    be rolled back already
246
 
  */
247
 
  inline result_type operator() (argument_type engine)
248
 
  {
249
 
    if (*session->getEngineData(engine))
250
 
      engine->close_connection(session);
251
 
  }
252
 
};
253
 
 
254
193
/**
255
194
  @note
256
195
    don't bother to rollback here, it's done already
257
196
*/
258
 
void StorageEngine::closeConnection(Session* session)
 
197
void StorageEngine::closeConnection(Session& session)
259
198
{
260
 
  std::for_each(vector_of_engines.begin(), vector_of_engines.end(),
261
 
                StorageEngineCloseConnection(session));
 
199
  BOOST_FOREACH(EngineVector::reference it, g_engines)
 
200
  {
 
201
    if (*session.getEngineData(it))
 
202
      it->close_connection(&session);
 
203
  }
262
204
}
263
205
 
264
206
bool StorageEngine::flushLogs(StorageEngine *engine)
265
207
{
266
 
  if (engine == NULL)
267
 
  {
268
 
    if (std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
269
 
                     std::mem_fun(&StorageEngine::flush_logs))
270
 
        != vector_of_engines.begin())
271
 
      return true;
272
 
  }
273
 
  else
274
 
  {
275
 
    if (engine->flush_logs())
276
 
      return true;
277
 
  }
 
208
  if (not engine)
 
209
  {
 
210
    if (std::find_if(g_engines.begin(), g_engines.end(), std::mem_fun(&StorageEngine::flush_logs))
 
211
        != g_engines.begin()) // Shouldn't this be .end()?
 
212
      return true;
 
213
  }
 
214
  else if (engine->flush_logs())
 
215
    return true;
278
216
  return false;
279
217
}
280
218
 
306
244
  }
307
245
};
308
246
 
309
 
class StorageEngineDoesTableExist: public std::unary_function<StorageEngine *, bool>
310
 
{
311
 
  Session& session;
312
 
  const identifier::Table &identifier;
313
 
 
314
 
public:
315
 
  StorageEngineDoesTableExist(Session& session_arg, const identifier::Table &identifier_arg) :
316
 
    session(session_arg), 
317
 
    identifier(identifier_arg) 
318
 
  { }
319
 
 
320
 
  result_type operator() (argument_type engine)
321
 
  {
322
 
    return engine->doDoesTableExist(session, identifier);
323
 
  }
324
 
};
325
 
 
326
247
/**
327
248
  Utility method which hides some of the details of getTableDefinition()
328
249
*/
330
251
                                           const identifier::Table &identifier,
331
252
                                           bool include_temporary_tables)
332
253
{
333
 
  if (include_temporary_tables)
334
 
  {
335
 
    if (session.open_tables.doDoesTableExist(identifier))
336
 
      return true;
337
 
  }
338
 
 
339
 
  EngineVector::iterator iter=
340
 
    std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
341
 
                 StorageEngineDoesTableExist(session, identifier));
342
 
 
343
 
  if (iter == vector_of_engines.end())
344
 
  {
345
 
    return false;
346
 
  }
347
 
 
348
 
  return true;
 
254
  if (include_temporary_tables && session.open_tables.doDoesTableExist(identifier))
 
255
      return true;
 
256
  BOOST_FOREACH(EngineVector::reference it, g_engines)
 
257
  {
 
258
    if (it->doDoesTableExist(session, identifier))
 
259
      return true;
 
260
  }
 
261
  return false;
349
262
}
350
263
 
351
264
bool plugin::StorageEngine::doDoesTableExist(Session&, const drizzled::identifier::Table&)
359
272
                                                          const identifier::Table& identifier,
360
273
                                                          bool include_temporary_tables)
361
274
{
362
 
  drizzled::error_t error;
363
 
  error= static_cast<drizzled::error_t>(ENOENT);
364
 
 
 
275
  drizzled::error_t error= static_cast<drizzled::error_t>(ENOENT);
365
276
  if (include_temporary_tables)
366
277
  {
367
 
    Table *table= session.open_tables.find_temporary_table(identifier);
368
 
    if (table)
 
278
    if (Table *table= session.open_tables.find_temporary_table(identifier))
369
279
    {
370
280
      return message::table::shared_ptr(new message::Table(*table->getShare()->getTableMessage()));
371
281
    }
379
289
 
380
290
  message::Table message;
381
291
  EngineVector::iterator iter=
382
 
    std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
 
292
    std::find_if(g_engines.begin(), g_engines.end(),
383
293
                 StorageEngineGetTableDefinition(session, identifier, message, error));
384
294
 
385
 
  if (iter == vector_of_engines.end())
 
295
  if (iter == g_engines.end())
386
296
  {
387
297
    return message::table::shared_ptr();
388
298
  }
441
351
{
442
352
  error= EE_OK;
443
353
 
444
 
  EngineVector::const_iterator iter= std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
 
354
  EngineVector::const_iterator iter= std::find_if(g_engines.begin(), g_engines.end(),
445
355
                                                  DropTableByIdentifier(session, identifier, error));
446
356
 
447
357
  if (error)
448
358
  {
449
359
    return false;
450
360
  }
451
 
  else if (iter == vector_of_engines.end())
 
361
  else if (iter == g_engines.end())
452
362
  {
453
363
    error= ER_BAD_TABLE_ERROR;
454
364
    return false;
463
373
                              const identifier::Table &identifier)
464
374
{
465
375
  drizzled::error_t error;
466
 
 
467
 
  if (not dropTable(session, identifier, error))
468
 
  {
469
 
    return false;
470
 
  }
471
 
 
472
 
  return true;
 
376
  return dropTable(session, identifier, error);
473
377
}
474
378
 
475
379
bool StorageEngine::dropTable(Session& session,
495
399
      error= ER_EVENT_OBSERVER_PLUGIN;
496
400
    }
497
401
  }
498
 
 
499
402
  drizzled::message::Cache::singleton().erase(identifier);
500
 
 
501
 
  if (error)
502
 
  {
503
 
    return false;
504
 
  }
505
 
 
506
 
  return true;
 
403
  return not error;
507
404
}
508
405
 
509
406
 
571
468
  return create(arg);
572
469
}
573
470
 
574
 
class AddTableIdentifier : 
575
 
  public std::unary_function<StorageEngine *, void>
576
 
{
577
 
  CachedDirectory &directory;
578
 
  const identifier::Schema &identifier;
579
 
  identifier::table::vector &set_of_identifiers;
580
 
 
581
 
public:
582
 
 
583
 
  AddTableIdentifier(CachedDirectory &directory_arg, const identifier::Schema &identifier_arg, identifier::table::vector &of_names) :
584
 
    directory(directory_arg),
585
 
    identifier(identifier_arg),
586
 
    set_of_identifiers(of_names)
587
 
  {
588
 
  }
589
 
 
590
 
  result_type operator() (argument_type engine)
591
 
  {
592
 
    engine->doGetTableIdentifiers(directory, identifier, set_of_identifiers);
593
 
  }
594
 
};
595
 
 
596
 
 
597
471
void StorageEngine::getIdentifiers(Session &session, const identifier::Schema &schema_identifier, identifier::table::vector &set_of_identifiers)
598
472
{
599
473
  CachedDirectory directory(schema_identifier.getPath(), set_of_table_definition_ext);
614
488
    return;
615
489
  }
616
490
 
617
 
  std::for_each(vector_of_engines.begin(), vector_of_engines.end(),
618
 
                AddTableIdentifier(directory, schema_identifier, set_of_identifiers));
 
491
  BOOST_FOREACH(EngineVector::reference it, g_engines)
 
492
    it->doGetTableIdentifiers(directory, schema_identifier, set_of_identifiers);
619
493
 
620
494
  session.open_tables.doGetTableIdentifiers(directory, schema_identifier, set_of_identifiers);
621
495
}
704
578
    }
705
579
  }
706
580
 
707
 
  std::for_each(vector_of_engines.begin(), vector_of_engines.end(),
 
581
  std::for_each(g_engines.begin(), g_engines.end(),
708
582
                DropTables(session, table_identifiers));
709
583
 
710
584
  /*
715
589
  */
716
590
  std::set<std::string> all_exts= set_of_table_definition_ext;
717
591
 
718
 
  for (EngineVector::iterator iter= vector_of_engines.begin();
719
 
       iter != vector_of_engines.end() ; iter++)
 
592
  for (EngineVector::iterator iter= g_engines.begin();
 
593
       iter != g_engines.end() ; iter++)
720
594
  {
721
595
    for (const char **ext= (*iter)->bas_ext(); *ext ; ext++)
722
596
      all_exts.insert(*ext);
1119
993
bool StorageEngine::canCreateTable(const identifier::Table &identifier)
1120
994
{
1121
995
  EngineVector::iterator iter=
1122
 
    std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
 
996
    std::find_if(g_engines.begin(), g_engines.end(),
1123
997
                 CanCreateTable(identifier));
1124
998
 
1125
 
  if (iter == vector_of_engines.end())
 
999
  if (iter == g_engines.end())
1126
1000
  {
1127
1001
    return true;
1128
1002
  }