~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/blackhole/ha_blackhole.cc

  • Committer: Olaf van der Spek
  • Date: 2011-03-23 10:31:37 UTC
  • mto: (2247.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 2248.
  • Revision ID: olafvdspek@gmail.com-20110323103137-lwevis2tfchgu18u
Propogate return void

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2005 MySQL AB
2
 
 
3
 
  This program is free software; you can redistribute it and/or modify
4
 
  it under the terms of the GNU General Public License as published by
5
 
  the Free Software Foundation; version 2 of the License.
6
 
 
7
 
  This program is distributed in the hope that it will be useful,
8
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 
  GNU General Public License for more details.
11
 
 
12
 
  You should have received a copy of the GNU General Public License
13
 
  along with this program; if not, write to the Free Software
14
 
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
 
 
16
 
#include "config.h"
17
 
#include <drizzled/table.h>
18
 
#include <drizzled/error.h>
19
 
#include "drizzled/internal/my_pthread.h"
20
 
 
21
 
#include "ha_blackhole.h"
22
 
 
23
 
#include <fcntl.h>
24
 
 
25
 
#include <string>
26
 
#include <map>
27
 
#include <fstream>
28
 
#include <drizzled/message/table.pb.h>
29
 
#include "drizzled/internal/m_string.h"
30
 
#include <google/protobuf/io/zero_copy_stream.h>
31
 
#include <google/protobuf/io/zero_copy_stream_impl.h>
32
 
#include "drizzled/global_charset_info.h"
33
 
 
34
 
 
35
 
using namespace std;
36
 
using namespace google;
37
 
using namespace drizzled;
38
 
 
39
 
#define BLACKHOLE_EXT ".blk"
40
 
 
41
 
static pthread_mutex_t blackhole_mutex;
42
 
 
43
 
 
44
 
static const char *ha_blackhole_exts[] = {
45
 
  BLACKHOLE_EXT,
46
 
  NULL
47
 
};
48
 
 
49
 
class BlackholeEngine : public drizzled::plugin::StorageEngine
50
 
{
51
 
  typedef std::map<std::string, BlackholeShare*> BlackholeMap;
52
 
  BlackholeMap blackhole_open_tables;
53
 
 
54
 
public:
55
 
  BlackholeEngine(const string &name_arg)
56
 
   : drizzled::plugin::StorageEngine(name_arg,
57
 
                                     HTON_NULL_IN_KEY |
58
 
                                     HTON_CAN_INDEX_BLOBS |
59
 
                                     HTON_SKIP_STORE_LOCK |
60
 
                                     HTON_AUTO_PART_KEY),
61
 
    blackhole_open_tables()
62
 
  {
63
 
    table_definition_ext= BLACKHOLE_EXT;
64
 
  }
65
 
 
66
 
  virtual ~BlackholeEngine()
67
 
  {
68
 
    pthread_mutex_destroy(&blackhole_mutex);
69
 
  }
70
 
 
71
 
  virtual Cursor *create(Table &table)
72
 
  {
73
 
    return new ha_blackhole(*this, table);
74
 
  }
75
 
 
76
 
  const char **bas_ext() const {
77
 
    return ha_blackhole_exts;
78
 
  }
79
 
 
80
 
  int doCreateTable(Session&,
81
 
                    Table&,
82
 
                    const drizzled::TableIdentifier &identifier,
83
 
                    drizzled::message::Table&);
84
 
 
85
 
  int doDropTable(Session&, const drizzled::TableIdentifier &identifier);
86
 
 
87
 
  BlackholeShare *findOpenTable(const string table_name);
88
 
  void addOpenTable(const string &table_name, BlackholeShare *);
89
 
  void deleteOpenTable(const string &table_name);
90
 
 
91
 
  int doGetTableDefinition(Session& session,
92
 
                           const drizzled::TableIdentifier &identifier,
93
 
                           drizzled::message::Table &table_message);
94
 
 
95
 
  /* The following defines can be increased if necessary */
96
 
  uint32_t max_supported_keys()          const { return BLACKHOLE_MAX_KEY; }
97
 
  uint32_t max_supported_key_length()    const { return BLACKHOLE_MAX_KEY_LENGTH; }
98
 
  uint32_t max_supported_key_part_length() const { return BLACKHOLE_MAX_KEY_LENGTH; }
99
 
 
100
 
  uint32_t index_flags(enum  ha_key_alg) const
101
 
  {
102
 
    return (HA_READ_NEXT |
103
 
            HA_READ_PREV |
104
 
            HA_READ_RANGE |
105
 
            HA_READ_ORDER |
106
 
            HA_KEYREAD_ONLY);
107
 
  }
108
 
 
109
 
  bool doDoesTableExist(Session& session, const drizzled::TableIdentifier &identifier);
110
 
  int doRenameTable(Session&, const drizzled::TableIdentifier &from, const drizzled::TableIdentifier &to);
111
 
  void doGetTableIdentifiers(drizzled::CachedDirectory &directory,
112
 
                             const drizzled::SchemaIdentifier &schema_identifier,
113
 
                             drizzled::TableIdentifier::vector &set_of_identifiers);
114
 
};
115
 
 
116
 
 
117
 
void BlackholeEngine::doGetTableIdentifiers(drizzled::CachedDirectory &directory,
118
 
                                            const drizzled::SchemaIdentifier &schema_identifier,
119
 
                                            drizzled::TableIdentifier::vector &set_of_identifiers)
120
 
{
121
 
  drizzled::CachedDirectory::Entries entries= directory.getEntries();
122
 
 
123
 
  for (drizzled::CachedDirectory::Entries::iterator entry_iter= entries.begin();
124
 
       entry_iter != entries.end(); ++entry_iter)
125
 
  {
126
 
    drizzled::CachedDirectory::Entry *entry= *entry_iter;
127
 
    const string *filename= &entry->filename;
128
 
 
129
 
    assert(filename->size());
130
 
 
131
 
    const char *ext= strchr(filename->c_str(), '.');
132
 
 
133
 
    if (ext == NULL || my_strcasecmp(system_charset_info, ext, BLACKHOLE_EXT) ||
134
 
        (filename->compare(0, strlen(TMP_FILE_PREFIX), TMP_FILE_PREFIX) == 0))
135
 
    {  }
136
 
    else
137
 
    {
138
 
      char uname[NAME_LEN + 1];
139
 
      uint32_t file_name_len;
140
 
 
141
 
      file_name_len= TableIdentifier::filename_to_tablename(filename->c_str(), uname, sizeof(uname));
142
 
      // TODO: Remove need for memory copy here
143
 
      uname[file_name_len - sizeof(BLACKHOLE_EXT) + 1]= '\0'; // Subtract ending, place NULL
144
 
 
145
 
      set_of_identifiers.push_back(TableIdentifier(schema_identifier, uname));
146
 
    }
147
 
  }
148
 
}
149
 
 
150
 
int BlackholeEngine::doRenameTable(Session&, const drizzled::TableIdentifier &from, const drizzled::TableIdentifier &to)
151
 
{
152
 
  int error= 0;
153
 
 
154
 
  for (const char **ext= bas_ext(); *ext ; ext++)
155
 
  {
156
 
    if (rename_file_ext(from.getPath().c_str(), to.getPath().c_str(), *ext))
157
 
    {
158
 
      if ((error=errno) != ENOENT)
159
 
        break;
160
 
      error= 0;
161
 
    }
162
 
  }
163
 
  return error;
164
 
}
165
 
 
166
 
BlackholeShare *BlackholeEngine::findOpenTable(const string table_name)
167
 
{
168
 
  BlackholeMap::iterator find_iter=
169
 
    blackhole_open_tables.find(table_name);
170
 
 
171
 
  if (find_iter != blackhole_open_tables.end())
172
 
    return (*find_iter).second;
173
 
  else
174
 
    return NULL;
175
 
}
176
 
 
177
 
void BlackholeEngine::addOpenTable(const string &table_name, BlackholeShare *share)
178
 
{
179
 
  blackhole_open_tables[table_name]= share;
180
 
}
181
 
 
182
 
void BlackholeEngine::deleteOpenTable(const string &table_name)
183
 
{
184
 
  blackhole_open_tables.erase(table_name);
185
 
}
186
 
 
187
 
 
188
 
 
189
 
/*****************************************************************************
190
 
** BLACKHOLE tables
191
 
*****************************************************************************/
192
 
 
193
 
ha_blackhole::ha_blackhole(drizzled::plugin::StorageEngine &engine_arg,
194
 
                           Table &table_arg)
195
 
  :Cursor(engine_arg, table_arg), share(NULL)
196
 
{ }
197
 
 
198
 
int ha_blackhole::open(const char *name, int, uint32_t)
199
 
{
200
 
  if (!(share= get_share(name)))
201
 
    return(HA_ERR_OUT_OF_MEM);
202
 
 
203
 
  lock.init(&share->lock);
204
 
  return 0;
205
 
}
206
 
 
207
 
int ha_blackhole::close(void)
208
 
{
209
 
  free_share();
210
 
  return 0;
211
 
}
212
 
 
213
 
int BlackholeEngine::doCreateTable(Session&,
214
 
                                   Table&,
215
 
                                   const drizzled::TableIdentifier &identifier,
216
 
                                   drizzled::message::Table& proto)
217
 
{
218
 
  string serialized_proto;
219
 
  string new_path;
220
 
 
221
 
  new_path= identifier.getPath();
222
 
  new_path+= BLACKHOLE_EXT;
223
 
  fstream output(new_path.c_str(), ios::out | ios::binary);
224
 
 
225
 
 
226
 
  if (! output)
227
 
    return 1;
228
 
 
229
 
  if (! proto.SerializeToOstream(&output))
230
 
  {
231
 
    output.close();
232
 
    unlink(new_path.c_str());
233
 
    return 1;
234
 
  }
235
 
 
236
 
  return 0;
237
 
}
238
 
 
239
 
 
240
 
int BlackholeEngine::doDropTable(Session&,
241
 
                                 const drizzled::TableIdentifier &identifier)
242
 
{
243
 
  string new_path(identifier.getPath());
244
 
 
245
 
  new_path+= BLACKHOLE_EXT;
246
 
 
247
 
  int error= unlink(new_path.c_str());
248
 
 
249
 
  if (error != 0)
250
 
  {
251
 
    error= errno= errno;
252
 
  }
253
 
 
254
 
  return error;
255
 
}
256
 
 
257
 
 
258
 
bool BlackholeEngine::doDoesTableExist(Session&,
259
 
                                       const drizzled::TableIdentifier &identifier)
260
 
{
261
 
  string proto_path(identifier.getPath());
262
 
  proto_path.append(BLACKHOLE_EXT);
263
 
 
264
 
  if (access(proto_path.c_str(), F_OK))
265
 
  {
266
 
    return false;
267
 
  }
268
 
 
269
 
  return true;
270
 
}
271
 
 
272
 
 
273
 
int BlackholeEngine::doGetTableDefinition(Session&,
274
 
                                          const drizzled::TableIdentifier &identifier,
275
 
                                          drizzled::message::Table &table_proto)
276
 
{
277
 
  string new_path;
278
 
 
279
 
  new_path= identifier.getPath();
280
 
  new_path+= BLACKHOLE_EXT;
281
 
 
282
 
  int fd= open(new_path.c_str(), O_RDONLY);
283
 
 
284
 
  if (fd == -1)
285
 
  {
286
 
    return errno;
287
 
  }
288
 
 
289
 
  google::protobuf::io::ZeroCopyInputStream* input=
290
 
    new google::protobuf::io::FileInputStream(fd);
291
 
 
292
 
  if (not input)
293
 
    return HA_ERR_CRASHED_ON_USAGE;
294
 
 
295
 
  if (not table_proto.ParseFromZeroCopyStream(input))
296
 
  {
297
 
    close(fd);
298
 
    delete input;
299
 
    if (not table_proto.IsInitialized())
300
 
    {
301
 
      my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
302
 
               table_proto.InitializationErrorString().c_str());
303
 
      return ER_CORRUPT_TABLE_DEFINITION;
304
 
    }
305
 
 
306
 
    return HA_ERR_CRASHED_ON_USAGE;
307
 
  }
308
 
 
309
 
  delete input;
310
 
 
311
 
  return EEXIST;
312
 
}
313
 
 
314
 
const char *ha_blackhole::index_type(uint32_t)
315
 
{
316
 
  return("BTREE");
317
 
}
318
 
 
319
 
int ha_blackhole::doInsertRecord(unsigned char *)
320
 
{
321
 
  return(getTable()->next_number_field ? update_auto_increment() : 0);
322
 
}
323
 
 
324
 
int ha_blackhole::doStartTableScan(bool)
325
 
{
326
 
  return(0);
327
 
}
328
 
 
329
 
 
330
 
int ha_blackhole::rnd_next(unsigned char *)
331
 
{
332
 
  return(HA_ERR_END_OF_FILE);
333
 
}
334
 
 
335
 
 
336
 
int ha_blackhole::rnd_pos(unsigned char *, unsigned char *)
337
 
{
338
 
  assert(0);
339
 
  return(0);
340
 
}
341
 
 
342
 
 
343
 
void ha_blackhole::position(const unsigned char *)
344
 
{
345
 
  assert(0);
346
 
  return;
347
 
}
348
 
 
349
 
 
350
 
int ha_blackhole::info(uint32_t flag)
351
 
{
352
 
  memset(&stats, 0, sizeof(stats));
353
 
  if (flag & HA_STATUS_AUTO)
354
 
    stats.auto_increment_value= 1;
355
 
  return(0);
356
 
}
357
 
 
358
 
 
359
 
int ha_blackhole::index_read_map(unsigned char *, const unsigned char *,
360
 
                                 key_part_map, enum ha_rkey_function)
361
 
{
362
 
  return(HA_ERR_END_OF_FILE);
363
 
}
364
 
 
365
 
 
366
 
int ha_blackhole::index_read_idx_map(unsigned char *, uint32_t, const unsigned char *,
367
 
                                     key_part_map, enum ha_rkey_function)
368
 
{
369
 
  return(HA_ERR_END_OF_FILE);
370
 
}
371
 
 
372
 
 
373
 
int ha_blackhole::index_read_last_map(unsigned char *, const unsigned char *, key_part_map)
374
 
{
375
 
  return(HA_ERR_END_OF_FILE);
376
 
}
377
 
 
378
 
 
379
 
int ha_blackhole::index_next(unsigned char *)
380
 
{
381
 
  return(HA_ERR_END_OF_FILE);
382
 
}
383
 
 
384
 
 
385
 
int ha_blackhole::index_prev(unsigned char *)
386
 
{
387
 
  return(HA_ERR_END_OF_FILE);
388
 
}
389
 
 
390
 
 
391
 
int ha_blackhole::index_first(unsigned char *)
392
 
{
393
 
  return(HA_ERR_END_OF_FILE);
394
 
}
395
 
 
396
 
 
397
 
int ha_blackhole::index_last(unsigned char *)
398
 
{
399
 
  return(HA_ERR_END_OF_FILE);
400
 
}
401
 
 
402
 
 
403
 
BlackholeShare *ha_blackhole::get_share(const char *table_name)
404
 
{
405
 
  pthread_mutex_lock(&blackhole_mutex);
406
 
 
407
 
  BlackholeEngine *a_engine= static_cast<BlackholeEngine *>(getEngine());
408
 
  share= a_engine->findOpenTable(table_name);
409
 
 
410
 
  if (share == NULL)
411
 
  {
412
 
    share= new (nothrow) BlackholeShare(table_name);
413
 
    if (share == NULL)
414
 
    {
415
 
      pthread_mutex_unlock(&blackhole_mutex);      
416
 
      return NULL;
417
 
    }
418
 
 
419
 
    a_engine->addOpenTable(share->table_name, share);
420
 
  }
421
 
  share->use_count++;
422
 
  pthread_mutex_unlock(&blackhole_mutex);
423
 
  return share;
424
 
 
425
 
}
426
 
 
427
 
void ha_blackhole::free_share()
428
 
{
429
 
  pthread_mutex_lock(&blackhole_mutex);
430
 
  if (!--share->use_count)
431
 
  {
432
 
    BlackholeEngine *a_engine= static_cast<BlackholeEngine *>(getEngine());
433
 
    a_engine->deleteOpenTable(share->table_name);
434
 
    delete share;
435
 
  }
436
 
  pthread_mutex_unlock(&blackhole_mutex);
437
 
}
438
 
 
439
 
BlackholeShare::BlackholeShare(const string table_name_arg)
440
 
  : use_count(0), table_name(table_name_arg)
441
 
{
442
 
  thr_lock_init(&lock);
443
 
}
444
 
 
445
 
BlackholeShare::~BlackholeShare()
446
 
{
447
 
  lock.deinit();
448
 
}
449
 
 
450
 
 
451
 
static drizzled::plugin::StorageEngine *blackhole_engine= NULL;
452
 
 
453
 
static int blackhole_init(drizzled::module::Context &context)
454
 
{
455
 
 
456
 
  blackhole_engine= new BlackholeEngine("BLACKHOLE");
457
 
  context.add(blackhole_engine);
458
 
  
459
 
  pthread_mutex_init(&blackhole_mutex, MY_MUTEX_INIT_FAST);
460
 
 
461
 
  return 0;
462
 
}
463
 
 
464
 
 
465
 
DRIZZLE_DECLARE_PLUGIN
466
 
{
467
 
  DRIZZLE_VERSION_ID,
468
 
  "BLACKHOLE",
469
 
  "1.0",
470
 
  "MySQL AB",
471
 
  "/dev/null storage engine (anything you write to it disappears)",
472
 
  PLUGIN_LICENSE_GPL,
473
 
  blackhole_init,     /* Plugin Init */
474
 
  NULL,               /* system variables */
475
 
  NULL                /* config options   */
476
 
}
477
 
DRIZZLE_DECLARE_PLUGIN_END;