~posulliv/drizzle/optimizer-style-cleanup

« back to all changes in this revision

Viewing changes to plugin/blackhole/ha_blackhole.cc

  • Committer: Padraig O'Sullivan
  • Date: 2010-04-17 01:38:47 UTC
  • mfrom: (1237.9.238 bad-staging)
  • Revision ID: osullivan.padraig@gmail.com-20100417013847-ibjioqsfbmf5yg4g
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
 
39
39
#define BLACKHOLE_EXT ".blk"
40
40
 
 
41
static pthread_mutex_t blackhole_mutex;
 
42
 
 
43
 
41
44
static const char *ha_blackhole_exts[] = {
42
45
  NULL
43
46
};
49
52
 
50
53
public:
51
54
  BlackholeEngine(const string &name_arg)
52
 
   : drizzled::plugin::StorageEngine(name_arg, HTON_FILE_BASED |
 
55
   : drizzled::plugin::StorageEngine(name_arg,
53
56
                                     HTON_NULL_IN_KEY |
54
57
                                     HTON_CAN_INDEX_BLOBS |
55
58
                                     HTON_SKIP_STORE_LOCK |
56
 
                                     HTON_AUTO_PART_KEY |
57
 
                                     HTON_HAS_DATA_DICTIONARY),
 
59
                                     HTON_AUTO_PART_KEY),
58
60
    blackhole_open_tables()
59
61
  {
60
62
    table_definition_ext= BLACKHOLE_EXT;
61
63
  }
62
64
 
 
65
  virtual ~BlackholeEngine()
 
66
  {
 
67
    pthread_mutex_destroy(&blackhole_mutex);
 
68
  }
 
69
 
63
70
  virtual Cursor *create(TableShare &table,
64
71
                         drizzled::memory::Root *mem_root)
65
72
  {
70
77
    return ha_blackhole_exts;
71
78
  }
72
79
 
73
 
  int doCreateTable(Session*,
74
 
                    const char *,
 
80
  int doCreateTable(Session&,
75
81
                    Table&,
 
82
                    drizzled::TableIdentifier &identifier,
76
83
                    drizzled::message::Table&);
77
84
 
78
 
  int doDropTable(Session&, const string &table_name);
 
85
  int doDropTable(Session&, TableIdentifier &identifier);
79
86
 
80
87
  BlackholeShare *findOpenTable(const string table_name);
81
88
  void addOpenTable(const string &table_name, BlackholeShare *);
82
89
  void deleteOpenTable(const string &table_name);
83
90
 
84
91
  int doGetTableDefinition(Session& session,
85
 
                           const char* path,
86
 
                           const char *db,
87
 
                           const char *table_name,
88
 
                           const bool is_tmp,
89
 
                           drizzled::message::Table *table_proto);
 
92
                           TableIdentifier &identifier,
 
93
                           drizzled::message::Table &table_message);
90
94
 
91
95
  void doGetTableNames(drizzled::CachedDirectory &directory,
92
 
                       string&, set<string>& set_of_names)
 
96
                       SchemaIdentifier &,
 
97
                       set<string>& set_of_names)
93
98
  {
94
99
    drizzled::CachedDirectory::Entries entries= directory.getEntries();
95
100
 
133
138
            HA_KEYREAD_ONLY);
134
139
  }
135
140
 
 
141
  bool doDoesTableExist(Session& session, TableIdentifier &identifier);
 
142
  int doRenameTable(Session&, TableIdentifier &from, TableIdentifier &to);
 
143
  void doGetTableIdentifiers(drizzled::CachedDirectory &directory,
 
144
                             drizzled::SchemaIdentifier &schema_identifier,
 
145
                             drizzled::TableIdentifiers &set_of_identifiers);
136
146
};
137
147
 
138
148
 
 
149
void BlackholeEngine::doGetTableIdentifiers(drizzled::CachedDirectory &directory,
 
150
                                            drizzled::SchemaIdentifier &schema_identifier,
 
151
                                            drizzled::TableIdentifiers &set_of_identifiers)
 
152
{
 
153
  drizzled::CachedDirectory::Entries entries= directory.getEntries();
 
154
 
 
155
  for (drizzled::CachedDirectory::Entries::iterator entry_iter= entries.begin();
 
156
       entry_iter != entries.end(); ++entry_iter)
 
157
  {
 
158
    drizzled::CachedDirectory::Entry *entry= *entry_iter;
 
159
    const string *filename= &entry->filename;
 
160
 
 
161
    assert(filename->size());
 
162
 
 
163
    const char *ext= strchr(filename->c_str(), '.');
 
164
 
 
165
    if (ext == NULL || my_strcasecmp(system_charset_info, ext, BLACKHOLE_EXT) ||
 
166
        (filename->compare(0, strlen(TMP_FILE_PREFIX), TMP_FILE_PREFIX) == 0))
 
167
    {  }
 
168
    else
 
169
    {
 
170
      char uname[NAME_LEN + 1];
 
171
      uint32_t file_name_len;
 
172
 
 
173
      file_name_len= filename_to_tablename(filename->c_str(), uname, sizeof(uname));
 
174
      // TODO: Remove need for memory copy here
 
175
      uname[file_name_len - sizeof(BLACKHOLE_EXT) + 1]= '\0'; // Subtract ending, place NULL
 
176
 
 
177
      set_of_identifiers.push_back(TableIdentifier(schema_identifier, uname));
 
178
    }
 
179
  }
 
180
}
 
181
 
 
182
int BlackholeEngine::doRenameTable(Session&, TableIdentifier &from, TableIdentifier &to)
 
183
{
 
184
  int error= 0;
 
185
 
 
186
  for (const char **ext= bas_ext(); *ext ; ext++)
 
187
  {
 
188
    if (rename_file_ext(from.getPath().c_str(), to.getPath().c_str(), *ext))
 
189
    {
 
190
      if ((error=errno) != ENOENT)
 
191
        break;
 
192
      error= 0;
 
193
    }
 
194
  }
 
195
  return error;
 
196
}
 
197
 
139
198
BlackholeShare *BlackholeEngine::findOpenTable(const string table_name)
140
199
{
141
200
  BlackholeMap::iterator find_iter=
158
217
}
159
218
 
160
219
 
161
 
/* Static declarations for shared structures */
162
 
 
163
 
static pthread_mutex_t blackhole_mutex;
164
 
 
165
220
 
166
221
/*****************************************************************************
167
222
** BLACKHOLE tables
187
242
  return 0;
188
243
}
189
244
 
190
 
int BlackholeEngine::doCreateTable(Session*, const char *path,
 
245
int BlackholeEngine::doCreateTable(Session&,
191
246
                                   Table&,
 
247
                                   drizzled::TableIdentifier &identifier,
192
248
                                   drizzled::message::Table& proto)
193
249
{
194
250
  string serialized_proto;
195
251
  string new_path;
196
252
 
197
 
  new_path= path;
 
253
  new_path= identifier.getPath();
198
254
  new_path+= BLACKHOLE_EXT;
199
255
  fstream output(new_path.c_str(), ios::out | ios::binary);
200
256
 
213
269
}
214
270
 
215
271
 
216
 
int BlackholeEngine::doDropTable(Session&, const string &path)
 
272
int BlackholeEngine::doDropTable(Session&,
 
273
                                 TableIdentifier &identifier)
217
274
{
218
 
  string new_path(path);
 
275
  string new_path(identifier.getPath());
219
276
 
220
277
  new_path+= BLACKHOLE_EXT;
221
278
 
230
287
}
231
288
 
232
289
 
 
290
bool BlackholeEngine::doDoesTableExist(Session&,
 
291
                                       TableIdentifier &identifier)
 
292
{
 
293
  string proto_path(identifier.getPath());
 
294
  proto_path.append(BLACKHOLE_EXT);
 
295
 
 
296
  if (access(proto_path.c_str(), F_OK))
 
297
  {
 
298
    return false;
 
299
  }
 
300
 
 
301
  return true;
 
302
}
 
303
 
 
304
 
233
305
int BlackholeEngine::doGetTableDefinition(Session&,
234
 
                                          const char* path,
235
 
                                          const char *,
236
 
                                          const char *,
237
 
                                          const bool,
238
 
                                          drizzled::message::Table *table_proto)
 
306
                                          TableIdentifier &identifier,
 
307
                                          drizzled::message::Table &table_proto)
239
308
{
240
309
  string new_path;
241
310
 
242
 
  new_path= path;
 
311
  new_path= identifier.getPath();
243
312
  new_path+= BLACKHOLE_EXT;
244
313
 
245
314
  int fd= open(new_path.c_str(), O_RDONLY);
252
321
  google::protobuf::io::ZeroCopyInputStream* input=
253
322
    new google::protobuf::io::FileInputStream(fd);
254
323
 
255
 
  if (! input)
 
324
  if (not input)
256
325
    return HA_ERR_CRASHED_ON_USAGE;
257
326
 
258
 
  if (table_proto && ! table_proto->ParseFromZeroCopyStream(input))
 
327
  if (not table_proto.ParseFromZeroCopyStream(input))
259
328
  {
260
329
    close(fd);
261
330
    delete input;
262
 
    if (! table_proto->IsInitialized())
 
331
    if (not table_proto.IsInitialized())
263
332
    {
264
333
      my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
265
 
               table_proto->InitializationErrorString().c_str());
 
334
               table_proto.InitializationErrorString().c_str());
266
335
      return ER_CORRUPT_TABLE_DEFINITION;
267
336
    }
268
337
 
270
339
  }
271
340
 
272
341
  delete input;
 
342
 
273
343
  return EEXIST;
274
344
}
275
345
 
412
482
 
413
483
static drizzled::plugin::StorageEngine *blackhole_engine= NULL;
414
484
 
415
 
static int blackhole_init(drizzled::plugin::Registry &registry)
 
485
static int blackhole_init(drizzled::plugin::Context &context)
416
486
{
417
487
 
418
488
  blackhole_engine= new BlackholeEngine("BLACKHOLE");
419
 
  registry.add(blackhole_engine);
 
489
  context.add(blackhole_engine);
420
490
  
421
491
  pthread_mutex_init(&blackhole_mutex, MY_MUTEX_INIT_FAST);
422
492
 
423
493
  return 0;
424
494
}
425
495
 
426
 
static int blackhole_fini(drizzled::plugin::Registry &registry)
427
 
{
428
 
  registry.remove(blackhole_engine);
429
 
  delete blackhole_engine;
430
 
 
431
 
  pthread_mutex_destroy(&blackhole_mutex);
432
 
 
433
 
  return 0;
434
 
}
435
496
 
436
497
DRIZZLE_DECLARE_PLUGIN
437
498
{
442
503
  "/dev/null storage engine (anything you write to it disappears)",
443
504
  PLUGIN_LICENSE_GPL,
444
505
  blackhole_init,     /* Plugin Init */
445
 
  blackhole_fini,     /* Plugin Deinit */
446
506
  NULL,               /* system variables */
447
507
  NULL                /* config options   */
448
508
}