~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/blackhole/ha_blackhole.cc

Merge Stewart's dead code removal

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
  You should have received a copy of the GNU General Public License
13
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
 
 
18
 
#include <drizzled/error.h>
19
 
#include <drizzled/global_charset_info.h>
20
 
#include <drizzled/internal/m_string.h>
21
 
#include <drizzled/internal/my_pthread.h>
22
 
#include <drizzled/message/table.h>
23
 
#include <drizzled/plugin/storage_engine.h>
 
14
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#include <drizzled/server_includes.h>
24
17
#include <drizzled/table.h>
25
 
 
26
 
 
27
18
#include "ha_blackhole.h"
28
19
 
29
 
#include <fcntl.h>
30
 
 
31
 
#include <fstream>
32
 
#include <map>
33
20
#include <string>
34
21
 
35
 
#include <google/protobuf/io/zero_copy_stream.h>
36
 
#include <google/protobuf/io/zero_copy_stream_impl.h>
37
 
 
38
 
 
39
22
using namespace std;
40
 
using namespace google;
41
 
using namespace drizzled;
42
 
 
43
 
#define BLACKHOLE_EXT ".blk"
44
 
 
45
 
static pthread_mutex_t blackhole_mutex;
 
23
 
 
24
static const string engine_name("BLACKHOLE");
46
25
 
47
26
 
48
27
static const char *ha_blackhole_exts[] = {
49
 
  BLACKHOLE_EXT,
50
28
  NULL
51
29
};
52
30
 
53
31
class BlackholeEngine : public drizzled::plugin::StorageEngine
54
32
{
55
 
  typedef std::map<std::string, BlackholeShare*> BlackholeMap;
56
 
  BlackholeMap blackhole_open_tables;
57
 
 
58
33
public:
59
34
  BlackholeEngine(const string &name_arg)
60
 
   : drizzled::plugin::StorageEngine(name_arg,
61
 
                                     HTON_NULL_IN_KEY |
62
 
                                     HTON_CAN_INDEX_BLOBS |
63
 
                                     HTON_SKIP_STORE_LOCK |
64
 
                                     HTON_AUTO_PART_KEY),
65
 
    blackhole_open_tables()
66
 
  {
67
 
    table_definition_ext= BLACKHOLE_EXT;
68
 
  }
69
 
 
70
 
  virtual ~BlackholeEngine()
71
 
  {
72
 
    pthread_mutex_destroy(&blackhole_mutex);
73
 
  }
74
 
 
75
 
  virtual Cursor *create(Table &table)
76
 
  {
77
 
    return new ha_blackhole(*this, table);
 
35
   : drizzled::plugin::StorageEngine(name_arg, HTON_FILE_BASED | HTON_CAN_RECREATE) {}
 
36
  virtual Cursor *create(TableShare *table,
 
37
                          MEM_ROOT *mem_root)
 
38
  {
 
39
    return new (mem_root) ha_blackhole(this, table);
78
40
  }
79
41
 
80
42
  const char **bas_ext() const {
81
43
    return ha_blackhole_exts;
82
44
  }
83
45
 
84
 
  int doCreateTable(Session&,
85
 
                    Table&,
86
 
                    const drizzled::identifier::Table &identifier,
87
 
                    drizzled::message::Table&);
88
 
 
89
 
  int doDropTable(Session&, const drizzled::identifier::Table &identifier);
90
 
 
91
 
  BlackholeShare *findOpenTable(const string table_name);
92
 
  void addOpenTable(const string &table_name, BlackholeShare *);
93
 
  void deleteOpenTable(const string &table_name);
94
 
 
95
 
  int doGetTableDefinition(Session& session,
96
 
                           const drizzled::identifier::Table &identifier,
97
 
                           drizzled::message::Table &table_message);
98
 
 
99
 
  /* The following defines can be increased if necessary */
100
 
  uint32_t max_supported_keys()          const { return BLACKHOLE_MAX_KEY; }
101
 
  uint32_t max_supported_key_length()    const { return BLACKHOLE_MAX_KEY_LENGTH; }
102
 
  uint32_t max_supported_key_part_length() const { return BLACKHOLE_MAX_KEY_LENGTH; }
103
 
 
104
 
  uint32_t index_flags(enum  ha_key_alg) const
105
 
  {
106
 
    return (HA_READ_NEXT |
107
 
            HA_READ_PREV |
108
 
            HA_READ_RANGE |
109
 
            HA_READ_ORDER |
110
 
            HA_KEYREAD_ONLY);
111
 
  }
112
 
 
113
 
  bool doDoesTableExist(Session& session, const drizzled::identifier::Table &identifier);
114
 
  int doRenameTable(Session&, const drizzled::identifier::Table &from, const drizzled::identifier::Table &to);
115
 
  void doGetTableIdentifiers(drizzled::CachedDirectory &directory,
116
 
                             const drizzled::identifier::Schema &schema_identifier,
117
 
                             drizzled::identifier::Table::vector &set_of_identifiers);
 
46
  int createTableImplementation(Session*, const char *, Table *,
 
47
                                HA_CREATE_INFO *, drizzled::message::Table*);
 
48
 
 
49
  int deleteTableImplementation(Session*, const string table_name); 
118
50
};
119
51
 
120
 
 
121
 
void BlackholeEngine::doGetTableIdentifiers(drizzled::CachedDirectory &directory,
122
 
                                            const drizzled::identifier::Schema &schema_identifier,
123
 
                                            drizzled::identifier::Table::vector &set_of_identifiers)
124
 
{
125
 
  drizzled::CachedDirectory::Entries entries= directory.getEntries();
126
 
 
127
 
  for (drizzled::CachedDirectory::Entries::iterator entry_iter= entries.begin();
128
 
       entry_iter != entries.end(); ++entry_iter)
129
 
  {
130
 
    drizzled::CachedDirectory::Entry *entry= *entry_iter;
131
 
    const string *filename= &entry->filename;
132
 
 
133
 
    assert(filename->size());
134
 
 
135
 
    const char *ext= strchr(filename->c_str(), '.');
136
 
 
137
 
    if (ext == NULL || my_strcasecmp(system_charset_info, ext, BLACKHOLE_EXT) ||
138
 
        (filename->compare(0, strlen(TMP_FILE_PREFIX), TMP_FILE_PREFIX) == 0))
139
 
    {  }
140
 
    else
141
 
    {
142
 
      char uname[NAME_LEN + 1];
143
 
      uint32_t file_name_len;
144
 
 
145
 
      file_name_len= identifier::Table::filename_to_tablename(filename->c_str(), uname, sizeof(uname));
146
 
      // TODO: Remove need for memory copy here
147
 
      uname[file_name_len - sizeof(BLACKHOLE_EXT) + 1]= '\0'; // Subtract ending, place NULL
148
 
 
149
 
      set_of_identifiers.push_back(identifier::Table(schema_identifier, uname));
150
 
    }
151
 
  }
152
 
}
153
 
 
154
 
int BlackholeEngine::doRenameTable(Session&, const drizzled::identifier::Table &from, const drizzled::identifier::Table &to)
155
 
{
156
 
  int error= 0;
157
 
 
158
 
  for (const char **ext= bas_ext(); *ext ; ext++)
159
 
  {
160
 
    if (rename_file_ext(from.getPath().c_str(), to.getPath().c_str(), *ext))
161
 
    {
162
 
      if ((error=errno) != ENOENT)
163
 
        break;
164
 
      error= 0;
165
 
    }
166
 
  }
167
 
  return error;
168
 
}
169
 
 
170
 
BlackholeShare *BlackholeEngine::findOpenTable(const string table_name)
171
 
{
172
 
  BlackholeMap::iterator find_iter=
173
 
    blackhole_open_tables.find(table_name);
174
 
 
175
 
  if (find_iter != blackhole_open_tables.end())
176
 
    return (*find_iter).second;
177
 
  else
178
 
    return NULL;
179
 
}
180
 
 
181
 
void BlackholeEngine::addOpenTable(const string &table_name, BlackholeShare *share)
182
 
{
183
 
  blackhole_open_tables[table_name]= share;
184
 
}
185
 
 
186
 
void BlackholeEngine::deleteOpenTable(const string &table_name)
187
 
{
188
 
  blackhole_open_tables.erase(table_name);
189
 
}
190
 
 
191
 
 
 
52
/* Static declarations for shared structures */
 
53
 
 
54
static pthread_mutex_t blackhole_mutex;
 
55
static HASH blackhole_open_tables;
 
56
 
 
57
static st_blackhole_share *get_share(const char *table_name);
 
58
static void free_share(st_blackhole_share *share);
192
59
 
193
60
/*****************************************************************************
194
61
** BLACKHOLE tables
195
62
*****************************************************************************/
196
63
 
197
 
ha_blackhole::ha_blackhole(drizzled::plugin::StorageEngine &engine_arg,
198
 
                           Table &table_arg)
199
 
  :Cursor(engine_arg, table_arg), share(NULL)
200
 
{ }
 
64
ha_blackhole::ha_blackhole(drizzled::plugin::StorageEngine *engine_arg,
 
65
                           TableShare *table_arg)
 
66
  :Cursor(engine_arg, table_arg)
 
67
{}
 
68
 
 
69
uint32_t ha_blackhole::index_flags(uint32_t inx, uint32_t, bool) const
 
70
{
 
71
  return ((table_share->key_info[inx].algorithm == HA_KEY_ALG_FULLTEXT) ?
 
72
          0 : HA_READ_NEXT | HA_READ_PREV | HA_READ_RANGE |
 
73
          HA_READ_ORDER | HA_KEYREAD_ONLY);
 
74
}
201
75
 
202
76
int ha_blackhole::open(const char *name, int, uint32_t)
203
77
{
204
78
  if (!(share= get_share(name)))
205
79
    return(HA_ERR_OUT_OF_MEM);
206
80
 
207
 
  lock.init(&share->lock);
208
 
  return 0;
 
81
  thr_lock_data_init(&share->lock, &lock, NULL);
 
82
  return(0);
209
83
}
210
84
 
211
85
int ha_blackhole::close(void)
212
86
{
213
 
  free_share();
214
 
  return 0;
215
 
}
216
 
 
217
 
int BlackholeEngine::doCreateTable(Session&,
218
 
                                   Table&,
219
 
                                   const drizzled::identifier::Table &identifier,
220
 
                                   drizzled::message::Table& proto)
221
 
{
222
 
  string serialized_proto;
223
 
  string new_path;
224
 
 
225
 
  new_path= identifier.getPath();
226
 
  new_path+= BLACKHOLE_EXT;
227
 
  fstream output(new_path.c_str(), ios::out | ios::binary);
228
 
 
229
 
 
230
 
  if (! output)
231
 
    return 1;
232
 
 
233
 
  if (! proto.SerializeToOstream(&output))
234
 
  {
235
 
    output.close();
236
 
    unlink(new_path.c_str());
237
 
    return 1;
238
 
  }
239
 
 
240
 
  return 0;
241
 
}
242
 
 
243
 
 
244
 
int BlackholeEngine::doDropTable(Session&,
245
 
                                 const drizzled::identifier::Table &identifier)
246
 
{
247
 
  string new_path(identifier.getPath());
248
 
 
249
 
  new_path+= BLACKHOLE_EXT;
250
 
 
251
 
  int error= unlink(new_path.c_str());
252
 
 
253
 
  if (error != 0)
254
 
  {
255
 
    error= errno= errno;
256
 
  }
257
 
 
258
 
  return error;
259
 
}
260
 
 
261
 
 
262
 
bool BlackholeEngine::doDoesTableExist(Session&,
263
 
                                       const drizzled::identifier::Table &identifier)
264
 
{
265
 
  string proto_path(identifier.getPath());
266
 
  proto_path.append(BLACKHOLE_EXT);
267
 
 
268
 
  if (access(proto_path.c_str(), F_OK))
269
 
  {
270
 
    return false;
271
 
  }
272
 
 
273
 
  return true;
274
 
}
275
 
 
276
 
 
277
 
int BlackholeEngine::doGetTableDefinition(Session&,
278
 
                                          const drizzled::identifier::Table &identifier,
279
 
                                          drizzled::message::Table &table_proto)
280
 
{
281
 
  string new_path;
282
 
 
283
 
  new_path= identifier.getPath();
284
 
  new_path+= BLACKHOLE_EXT;
285
 
 
286
 
  int fd= open(new_path.c_str(), O_RDONLY);
287
 
 
288
 
  if (fd == -1)
289
 
  {
 
87
  free_share(share);
 
88
  return(0);
 
89
}
 
90
 
 
91
int BlackholeEngine::createTableImplementation(Session*, const char *path,
 
92
                                               Table *, HA_CREATE_INFO *,
 
93
                                               drizzled::message::Table*)
 
94
{
 
95
  FILE *blackhole_table;
 
96
 
 
97
  /* Create an empty file for the Drizzle core to track whether
 
98
     a blackhole table exists */
 
99
  if ((blackhole_table= fopen(path, "w")) == NULL)
 
100
    return(1);
 
101
 
 
102
  /* This file should never have to be reopened */
 
103
  fclose(blackhole_table);
 
104
 
 
105
  return(0);
 
106
}
 
107
 
 
108
int BlackholeEngine::deleteTableImplementation(Session*, const string path)
 
109
{
 
110
  if (unlink(path.c_str()) != 0)
 
111
  {
 
112
    my_errno= errno;
290
113
    return errno;
291
114
  }
292
 
 
293
 
  google::protobuf::io::ZeroCopyInputStream* input=
294
 
    new google::protobuf::io::FileInputStream(fd);
295
 
 
296
 
  if (not input)
297
 
    return HA_ERR_CRASHED_ON_USAGE;
298
 
 
299
 
  if (not table_proto.ParseFromZeroCopyStream(input))
300
 
  {
301
 
    close(fd);
302
 
    delete input;
303
 
    if (not table_proto.IsInitialized())
304
 
    {
305
 
      my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
306
 
               table_proto.name().empty() ? " " : table_proto.name().c_str(),
307
 
               table_proto.InitializationErrorString().c_str());
308
 
 
309
 
      return ER_CORRUPT_TABLE_DEFINITION;
310
 
    }
311
 
 
312
 
    return HA_ERR_CRASHED_ON_USAGE;
313
 
  }
314
 
 
315
 
  delete input;
316
 
 
317
 
  return EEXIST;
 
115
  return(0);
318
116
}
319
117
 
320
118
const char *ha_blackhole::index_type(uint32_t)
322
120
  return("BTREE");
323
121
}
324
122
 
325
 
int ha_blackhole::doInsertRecord(unsigned char *)
 
123
int ha_blackhole::write_row(unsigned char *)
326
124
{
327
 
  return(getTable()->next_number_field ? update_auto_increment() : 0);
 
125
  return(table->next_number_field ? update_auto_increment() : 0);
328
126
}
329
127
 
330
 
int ha_blackhole::doStartTableScan(bool)
 
128
int ha_blackhole::rnd_init(bool)
331
129
{
332
130
  return(0);
333
131
}
361
159
  return(0);
362
160
}
363
161
 
 
162
THR_LOCK_DATA **ha_blackhole::store_lock(Session *session,
 
163
                                         THR_LOCK_DATA **to,
 
164
                                         enum thr_lock_type lock_type)
 
165
{
 
166
  if (lock_type != TL_IGNORE && lock.type == TL_UNLOCK)
 
167
  {
 
168
    /*
 
169
      Here is where we get into the guts of a row level lock.
 
170
      If TL_UNLOCK is set
 
171
      If we are not doing a LOCK Table or DISCARD/IMPORT
 
172
      TABLESPACE, then allow multiple writers
 
173
    */
 
174
 
 
175
    if ((lock_type >= TL_WRITE_CONCURRENT_INSERT &&
 
176
         lock_type <= TL_WRITE) && !session_tablespace_op(session))
 
177
      lock_type = TL_WRITE_ALLOW_WRITE;
 
178
 
 
179
    /*
 
180
      In queries of type INSERT INTO t1 SELECT ... FROM t2 ...
 
181
      MySQL would use the lock TL_READ_NO_INSERT on t2, and that
 
182
      would conflict with TL_WRITE_ALLOW_WRITE, blocking all inserts
 
183
      to t2. Convert the lock to a normal read lock to allow
 
184
      concurrent inserts to t2.
 
185
    */
 
186
 
 
187
    if (lock_type == TL_READ_NO_INSERT)
 
188
      lock_type = TL_READ;
 
189
 
 
190
    lock.type= lock_type;
 
191
  }
 
192
  *to++= &lock;
 
193
  return(to);
 
194
}
 
195
 
364
196
 
365
197
int ha_blackhole::index_read_map(unsigned char *, const unsigned char *,
366
198
                                 key_part_map, enum ha_rkey_function)
406
238
}
407
239
 
408
240
 
409
 
BlackholeShare *ha_blackhole::get_share(const char *table_name)
 
241
static st_blackhole_share *get_share(const char *table_name)
410
242
{
 
243
  st_blackhole_share *share;
 
244
  uint32_t length;
 
245
 
 
246
  length= (uint) strlen(table_name);
411
247
  pthread_mutex_lock(&blackhole_mutex);
412
248
 
413
 
  BlackholeEngine *a_engine= static_cast<BlackholeEngine *>(getEngine());
414
 
  share= a_engine->findOpenTable(table_name);
415
 
 
416
 
  if (share == NULL)
 
249
  if (!(share= (st_blackhole_share*) hash_search(&blackhole_open_tables,
 
250
                                                 (unsigned char*) table_name, length)))
417
251
  {
418
 
    share= new (nothrow) BlackholeShare(table_name);
419
 
    if (share == NULL)
 
252
    if (!(share= (st_blackhole_share*) malloc(sizeof(st_blackhole_share) +
 
253
                                              length)))
 
254
      goto error;
 
255
    memset(share, 0, sizeof(st_blackhole_share) + length);
 
256
 
 
257
    share->table_name_length= length;
 
258
    strcpy(share->table_name, table_name);
 
259
 
 
260
    if (my_hash_insert(&blackhole_open_tables, (unsigned char*) share))
420
261
    {
421
 
      pthread_mutex_unlock(&blackhole_mutex);      
422
 
      return NULL;
 
262
      free((unsigned char*) share);
 
263
      share= NULL;
 
264
      goto error;
423
265
    }
424
266
 
425
 
    a_engine->addOpenTable(share->table_name, share);
 
267
    thr_lock_init(&share->lock);
426
268
  }
427
269
  share->use_count++;
 
270
 
 
271
error:
428
272
  pthread_mutex_unlock(&blackhole_mutex);
429
273
  return share;
430
 
 
431
274
}
432
275
 
433
 
void ha_blackhole::free_share()
 
276
static void free_share(st_blackhole_share *share)
434
277
{
435
278
  pthread_mutex_lock(&blackhole_mutex);
436
279
  if (!--share->use_count)
437
 
  {
438
 
    BlackholeEngine *a_engine= static_cast<BlackholeEngine *>(getEngine());
439
 
    a_engine->deleteOpenTable(share->table_name);
440
 
    delete share;
441
 
  }
 
280
    hash_delete(&blackhole_open_tables, (unsigned char*) share);
442
281
  pthread_mutex_unlock(&blackhole_mutex);
443
282
}
444
283
 
445
 
BlackholeShare::BlackholeShare(const string table_name_arg)
446
 
  : use_count(0), table_name(table_name_arg)
447
 
{
448
 
  thr_lock_init(&lock);
449
 
}
450
 
 
451
 
BlackholeShare::~BlackholeShare()
452
 
{
453
 
  lock.deinit();
454
 
}
455
 
 
 
284
static void blackhole_free_key(st_blackhole_share *share)
 
285
{
 
286
  thr_lock_delete(&share->lock);
 
287
  free((unsigned char*) share);
 
288
}
 
289
 
 
290
static unsigned char* blackhole_get_key(st_blackhole_share *share, size_t *length, bool)
 
291
{
 
292
  *length= share->table_name_length;
 
293
  return (unsigned char*) share->table_name;
 
294
}
456
295
 
457
296
static drizzled::plugin::StorageEngine *blackhole_engine= NULL;
458
297
 
459
 
static int blackhole_init(drizzled::module::Context &context)
 
298
static int blackhole_init(drizzled::plugin::Registry &registry)
460
299
{
461
300
 
462
 
  blackhole_engine= new BlackholeEngine("BLACKHOLE");
463
 
  context.add(blackhole_engine);
 
301
  blackhole_engine= new BlackholeEngine(engine_name);
 
302
  registry.add(blackhole_engine);
464
303
  
465
304
  pthread_mutex_init(&blackhole_mutex, MY_MUTEX_INIT_FAST);
466
 
 
467
 
  return 0;
468
 
}
469
 
 
470
 
 
471
 
DRIZZLE_DECLARE_PLUGIN
472
 
{
473
 
  DRIZZLE_VERSION_ID,
 
305
  (void) hash_init(&blackhole_open_tables, system_charset_info,32,0,0,
 
306
                   (hash_get_key) blackhole_get_key,
 
307
                   (hash_free_key) blackhole_free_key, 0);
 
308
 
 
309
  return 0;
 
310
}
 
311
 
 
312
static int blackhole_fini(drizzled::plugin::Registry &registry)
 
313
{
 
314
  registry.remove(blackhole_engine);
 
315
  delete blackhole_engine;
 
316
 
 
317
  hash_free(&blackhole_open_tables);
 
318
  pthread_mutex_destroy(&blackhole_mutex);
 
319
 
 
320
  return 0;
 
321
}
 
322
 
 
323
drizzle_declare_plugin(blackhole)
 
324
{
474
325
  "BLACKHOLE",
475
326
  "1.0",
476
327
  "MySQL AB",
477
328
  "/dev/null storage engine (anything you write to it disappears)",
478
329
  PLUGIN_LICENSE_GPL,
479
330
  blackhole_init,     /* Plugin Init */
480
 
  NULL,               /* depends */
 
331
  blackhole_fini,     /* Plugin Deinit */
 
332
  NULL,               /* status variables */
 
333
  NULL,               /* system variables */
481
334
  NULL                /* config options   */
482
335
}
483
 
DRIZZLE_DECLARE_PLUGIN_END;
 
336
drizzle_declare_plugin_end;