~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/blackhole/ha_blackhole.cc

Fix for spare (from Monty).

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#include "ha_blackhole.h"
22
22
 
23
23
#include <string>
 
24
#include <map>
24
25
#include <fstream>
25
26
#include <drizzled/message/table.pb.h>
26
27
#include <google/protobuf/io/zero_copy_stream.h>
37
38
 
38
39
class BlackholeEngine : public drizzled::plugin::StorageEngine
39
40
{
 
41
  typedef map<string, BlackholeShare*> BlackholeMap;
 
42
  BlackholeMap blackhole_open_tables;
 
43
 
40
44
public:
41
45
  BlackholeEngine(const string &name_arg)
42
46
   : drizzled::plugin::StorageEngine(name_arg, HTON_FILE_BASED |
44
48
                                     HTON_CAN_INDEX_BLOBS |
45
49
                                     HTON_SKIP_STORE_LOCK |
46
50
                                     HTON_AUTO_PART_KEY |
47
 
                                     HTON_HAS_DATA_DICTIONARY)
 
51
                                     HTON_HAS_DATA_DICTIONARY),
 
52
    blackhole_open_tables()
48
53
  {
49
54
    table_definition_ext= BLACKHOLE_EXT;
50
55
  }
66
71
 
67
72
  int doDropTable(Session&, const string table_name);
68
73
 
 
74
  BlackholeShare *findOpenTable(const string table_name);
 
75
  void addOpenTable(const string &table_name, BlackholeShare *);
 
76
  void deleteOpenTable(const string &table_name);
 
77
 
69
78
  int doGetTableDefinition(Session& session,
70
79
                           const char* path,
71
80
                           const char *db,
119
128
 
120
129
};
121
130
 
 
131
 
 
132
BlackholeShare *BlackholeEngine::findOpenTable(const string table_name)
 
133
{
 
134
  BlackholeMap::iterator find_iter=
 
135
    blackhole_open_tables.find(table_name);
 
136
 
 
137
  if (find_iter != blackhole_open_tables.end())
 
138
    return (*find_iter).second;
 
139
  else
 
140
    return NULL;
 
141
}
 
142
 
 
143
void BlackholeEngine::addOpenTable(const string &table_name, BlackholeShare *share)
 
144
{
 
145
  blackhole_open_tables[table_name]= share;
 
146
}
 
147
 
 
148
void BlackholeEngine::deleteOpenTable(const string &table_name)
 
149
{
 
150
  blackhole_open_tables.erase(table_name);
 
151
}
 
152
 
 
153
 
122
154
/* Static declarations for shared structures */
123
155
 
124
156
static pthread_mutex_t blackhole_mutex;
125
 
static HASH blackhole_open_tables;
126
157
 
127
 
static st_blackhole_share *get_share(const char *table_name);
128
 
static void free_share(st_blackhole_share *share);
129
158
 
130
159
/*****************************************************************************
131
160
** BLACKHOLE tables
133
162
 
134
163
ha_blackhole::ha_blackhole(drizzled::plugin::StorageEngine &engine_arg,
135
164
                           TableShare &table_arg)
136
 
  :Cursor(engine_arg, table_arg)
 
165
  :Cursor(engine_arg, table_arg), share(NULL)
137
166
{ }
138
167
 
139
168
int ha_blackhole::open(const char *name, int, uint32_t)
147
176
 
148
177
int ha_blackhole::close(void)
149
178
{
150
 
  free_share(share);
 
179
  free_share();
151
180
  return 0;
152
181
}
153
182
 
319
348
}
320
349
 
321
350
 
322
 
static st_blackhole_share *get_share(const char *table_name)
 
351
BlackholeShare *ha_blackhole::get_share(const char *table_name)
323
352
{
324
 
  st_blackhole_share *share;
325
 
  uint32_t length;
326
 
 
327
 
  length= (uint) strlen(table_name);
328
353
  pthread_mutex_lock(&blackhole_mutex);
329
354
 
330
 
  if (!(share= (st_blackhole_share*) hash_search(&blackhole_open_tables,
331
 
                                                 (unsigned char*) table_name, length)))
 
355
  BlackholeEngine *a_engine= static_cast<BlackholeEngine *>(engine);
 
356
  share= a_engine->findOpenTable(table_name);
 
357
 
 
358
  if (share == NULL)
332
359
  {
333
 
    if (!(share= (st_blackhole_share*) malloc(sizeof(st_blackhole_share) +
334
 
                                              length)))
335
 
      goto error;
336
 
    memset(share, 0, sizeof(st_blackhole_share) + length);
337
 
 
338
 
    share->table_name_length= length;
339
 
    strcpy(share->table_name, table_name);
340
 
 
341
 
    if (my_hash_insert(&blackhole_open_tables, (unsigned char*) share))
 
360
    share= new (nothrow) BlackholeShare(table_name);
 
361
    if (share == NULL)
342
362
    {
343
 
      free((unsigned char*) share);
344
 
      share= NULL;
345
 
      goto error;
 
363
      pthread_mutex_unlock(&blackhole_mutex);      
 
364
      return NULL;
346
365
    }
347
366
 
348
 
    thr_lock_init(&share->lock);
 
367
    a_engine->addOpenTable(share->table_name, share);
349
368
  }
350
369
  share->use_count++;
351
 
 
352
 
error:
353
370
  pthread_mutex_unlock(&blackhole_mutex);
354
371
  return share;
 
372
 
355
373
}
356
374
 
357
 
static void free_share(st_blackhole_share *share)
 
375
void ha_blackhole::free_share()
358
376
{
359
377
  pthread_mutex_lock(&blackhole_mutex);
360
378
  if (!--share->use_count)
361
 
    hash_delete(&blackhole_open_tables, (unsigned char*) share);
 
379
  {
 
380
    BlackholeEngine *a_engine= static_cast<BlackholeEngine *>(engine);
 
381
    a_engine->deleteOpenTable(share->table_name);
 
382
    delete share;
 
383
  }
362
384
  pthread_mutex_unlock(&blackhole_mutex);
363
385
}
364
386
 
365
 
static void blackhole_free_key(st_blackhole_share *share)
366
 
{
367
 
  thr_lock_delete(&share->lock);
368
 
  free((unsigned char*) share);
369
 
}
370
 
 
371
 
static unsigned char* blackhole_get_key(st_blackhole_share *share, size_t *length, bool)
372
 
{
373
 
  *length= share->table_name_length;
374
 
  return (unsigned char*) share->table_name;
375
 
}
 
387
BlackholeShare::BlackholeShare(const string table_name_arg)
 
388
  : use_count(0), table_name(table_name_arg)
 
389
{
 
390
  thr_lock_init(&lock);
 
391
}
 
392
 
 
393
BlackholeShare::~BlackholeShare()
 
394
{
 
395
  thr_lock_delete(&lock);
 
396
}
 
397
 
376
398
 
377
399
static drizzled::plugin::StorageEngine *blackhole_engine= NULL;
378
400
 
383
405
  registry.add(blackhole_engine);
384
406
  
385
407
  pthread_mutex_init(&blackhole_mutex, MY_MUTEX_INIT_FAST);
386
 
  (void) hash_init(&blackhole_open_tables, system_charset_info,32,0,0,
387
 
                   (hash_get_key) blackhole_get_key,
388
 
                   (hash_free_key) blackhole_free_key, 0);
389
408
 
390
409
  return 0;
391
410
}
395
414
  registry.remove(blackhole_engine);
396
415
  delete blackhole_engine;
397
416
 
398
 
  hash_free(&blackhole_open_tables);
399
417
  pthread_mutex_destroy(&blackhole_mutex);
400
418
 
401
419
  return 0;