~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/csv/ha_tina.cc

Remove PLUGIN and MODULES.

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
 
41
41
 -Brian
42
42
*/
43
 
#include <drizzled/server_includes.h>
 
43
#include "config.h"
44
44
#include <drizzled/field.h>
45
45
#include <drizzled/field/blob.h>
46
46
#include <drizzled/field/timestamp.h>
47
47
#include <drizzled/error.h>
48
48
#include <drizzled/table.h>
49
49
#include <drizzled/session.h>
50
 
#include "drizzled/memory/multi_malloc.h"
 
50
#include "drizzled/internal/my_sys.h"
51
51
 
52
52
#include "ha_tina.h"
53
53
 
 
54
#include <fcntl.h>
 
55
 
54
56
#include <string>
 
57
#include <map>
55
58
 
56
59
using namespace std;
57
60
 
58
 
static const string engine_name("CSV");
59
 
 
60
61
/*
61
62
  unsigned char + unsigned char + uint64_t + uint64_t + uint64_t + uint64_t + unsigned char
62
63
*/
71
72
#define CSM_EXT ".CSM"               // Meta file
72
73
 
73
74
 
74
 
static TINA_SHARE *get_share(const char *table_name, Table *table);
75
 
static int free_share(TINA_SHARE *share);
76
 
static int read_meta_file(File meta_file, ha_rows *rows);
77
 
static int write_meta_file(File meta_file, ha_rows rows, bool dirty);
 
75
static int read_meta_file(int meta_file, ha_rows *rows);
 
76
static int write_meta_file(int meta_file, ha_rows rows, bool dirty);
78
77
 
79
78
extern "C" void tina_get_status(void* param, int concurrent_insert);
80
79
extern "C" void tina_update_status(void* param);
82
81
 
83
82
/* Stuff for shares */
84
83
pthread_mutex_t tina_mutex;
85
 
static HASH tina_open_tables;
86
84
 
87
85
/*****************************************************************************
88
86
 ** TINA tables
100
98
  return ( a->begin > b->begin ? 1 : ( a->begin < b->begin ? -1 : 0 ) );
101
99
}
102
100
 
103
 
static unsigned char* tina_get_key(TINA_SHARE *share, size_t *length, bool)
104
 
{
105
 
  *length=share->table_name_length;
106
 
  return (unsigned char*) share->table_name;
107
 
}
108
 
 
109
101
 
110
102
/*
111
103
  If frm_error() is called in table.cc this is called to find out what file
119
111
 
120
112
class Tina : public drizzled::plugin::StorageEngine
121
113
{
 
114
  typedef std::map<string, TinaShare*> TinaMap;
 
115
  TinaMap tina_open_tables;
122
116
public:
123
117
  Tina(const string& name_arg)
124
118
   : drizzled::plugin::StorageEngine(name_arg,
125
119
                                     HTON_TEMPORARY_ONLY |
126
120
                                     HTON_NO_AUTO_INCREMENT |
127
121
                                     HTON_HAS_DATA_DICTIONARY |
128
 
                                     HTON_FILE_BASED) {}
 
122
                                     HTON_SKIP_STORE_LOCK |
 
123
                                     HTON_FILE_BASED),
 
124
    tina_open_tables()
 
125
  {}
129
126
  virtual Cursor *create(TableShare &table,
130
 
                          MEM_ROOT *mem_root)
 
127
                         drizzled::memory::Root *mem_root)
131
128
  {
132
129
    return new (mem_root) ha_tina(*this, table);
133
130
  }
149
146
                           drizzled::message::Table *table_proto);
150
147
 
151
148
  /* Temp only engine, so do not return values. */
152
 
  void doGetTableNames(CachedDirectory &, string& , set<string>&) { };
 
149
  void doGetTableNames(drizzled::CachedDirectory &, string& , set<string>&) { };
153
150
 
154
151
  int doDropTable(Session&, const string table_path);
 
152
  TinaShare *findOpenTable(const string table_name);
 
153
  void addOpenTable(const string &table_name, TinaShare *);
 
154
  void deleteOpenTable(const string &table_name);
 
155
 
155
156
 
156
157
  uint32_t max_keys()          const { return 0; }
157
158
  uint32_t max_key_parts()     const { return 0; }
172
173
              MY_UNPACK_FILENAME|MY_APPEND_EXT);
173
174
    if (my_delete_with_symlink(buff, MYF(0)))
174
175
    {
175
 
      if ((error= my_errno) != ENOENT)
 
176
      if ((error= errno) != ENOENT)
176
177
        break;
177
178
    }
178
179
    else
190
191
  return error;
191
192
}
192
193
 
 
194
TinaShare *Tina::findOpenTable(const string table_name)
 
195
{
 
196
  TinaMap::iterator find_iter=
 
197
    tina_open_tables.find(table_name);
 
198
 
 
199
  if (find_iter != tina_open_tables.end())
 
200
    return (*find_iter).second;
 
201
  else
 
202
    return NULL;
 
203
}
 
204
 
 
205
void Tina::addOpenTable(const string &table_name, TinaShare *share)
 
206
{
 
207
  tina_open_tables[table_name]= share;
 
208
}
 
209
 
 
210
void Tina::deleteOpenTable(const string &table_name)
 
211
{
 
212
  tina_open_tables.erase(table_name);
 
213
}
 
214
 
 
215
 
193
216
int Tina::doGetTableDefinition(Session&,
194
217
                               const char* path,
195
218
                               const char *,
220
243
static int tina_init_func(drizzled::plugin::Registry &registry)
221
244
{
222
245
 
223
 
  tina_engine= new Tina(engine_name);
 
246
  tina_engine= new Tina("CSV");
224
247
  registry.add(tina_engine);
225
248
 
226
249
  pthread_mutex_init(&tina_mutex,MY_MUTEX_INIT_FAST);
227
 
  (void) hash_init(&tina_open_tables,system_charset_info,32,0,0,
228
 
                   (hash_get_key) tina_get_key,0,0);
229
250
  return 0;
230
251
}
231
252
 
234
255
  registry.remove(tina_engine);
235
256
  delete tina_engine;
236
257
 
237
 
  hash_free(&tina_open_tables);
238
258
  pthread_mutex_destroy(&tina_mutex);
239
259
 
240
260
  return 0;
241
261
}
242
262
 
243
263
 
 
264
TinaShare::TinaShare(const char *table_name_arg)
 
265
  : table_name(table_name_arg), use_count(0), saved_data_file_length(0),
 
266
    update_file_opened(false), tina_write_opened(false),
 
267
    crashed(false), rows_recorded(0), data_file_version(0)
 
268
{
 
269
  thr_lock_init(&lock);
 
270
  fn_format(data_file_name, table_name_arg, "", CSV_EXT,
 
271
            MY_REPLACE_EXT|MY_UNPACK_FILENAME);
 
272
}
 
273
 
 
274
TinaShare::~TinaShare()
 
275
{
 
276
  thr_lock_delete(&lock);
 
277
  pthread_mutex_destroy(&mutex);
 
278
}
 
279
 
244
280
/*
245
281
  Simple lock controls.
246
282
*/
247
 
static TINA_SHARE *get_share(const char *table_name, Table *)
 
283
TinaShare *ha_tina::get_share(const char *table_name)
248
284
{
249
 
  TINA_SHARE *share;
 
285
  pthread_mutex_lock(&tina_mutex);
 
286
 
 
287
  Tina *a_tina= static_cast<Tina *>(engine);
 
288
  share= a_tina->findOpenTable(table_name);
 
289
 
250
290
  char meta_file_name[FN_REFLEN];
251
291
  struct stat file_stat;
252
 
  char *tmp_name;
253
 
  uint32_t length;
254
 
 
255
 
  pthread_mutex_lock(&tina_mutex);
256
 
  length=(uint) strlen(table_name);
257
292
 
258
293
  /*
259
294
    If share is not present in the hash, create a new share and
260
295
    initialize its members.
261
296
  */
262
 
  if (!(share=(TINA_SHARE*) hash_search(&tina_open_tables,
263
 
                                        (unsigned char*) table_name,
264
 
                                       length)))
 
297
  if (! share)
265
298
  {
266
 
    if (!drizzled::memory::multi_malloc(true,
267
 
                         &share, sizeof(*share),
268
 
                         &tmp_name, length+1,
269
 
                         NULL))
 
299
    share= new TinaShare(table_name);
 
300
 
 
301
    if (share == NULL)
270
302
    {
271
303
      pthread_mutex_unlock(&tina_mutex);
272
304
      return NULL;
273
305
    }
274
306
 
275
 
    share->use_count= 0;
276
 
    share->table_name_length= length;
277
 
    share->table_name= tmp_name;
278
 
    share->crashed= false;
279
 
    share->rows_recorded= 0;
280
 
    share->update_file_opened= false;
281
 
    share->tina_write_opened= false;
282
 
    share->data_file_version= 0;
283
 
    strcpy(share->table_name, table_name);
284
 
    fn_format(share->data_file_name, table_name, "", CSV_EXT,
285
 
              MY_REPLACE_EXT|MY_UNPACK_FILENAME);
286
307
    fn_format(meta_file_name, table_name, "", CSM_EXT,
287
308
              MY_REPLACE_EXT|MY_UNPACK_FILENAME);
288
309
 
289
310
    if (stat(share->data_file_name, &file_stat))
290
 
      goto error;
 
311
    {
 
312
      pthread_mutex_unlock(&tina_mutex);
 
313
      delete share;
 
314
      return NULL;
 
315
    }
 
316
  
291
317
    share->saved_data_file_length= file_stat.st_size;
292
318
 
293
 
    if (my_hash_insert(&tina_open_tables, (unsigned char*) share))
294
 
      goto error;
295
 
    thr_lock_init(&share->lock);
 
319
    a_tina->addOpenTable(share->table_name, share);
 
320
 
296
321
    pthread_mutex_init(&share->mutex,MY_MUTEX_INIT_FAST);
297
322
 
298
323
    /*
316
341
  pthread_mutex_unlock(&tina_mutex);
317
342
 
318
343
  return share;
319
 
 
320
 
error:
321
 
  pthread_mutex_unlock(&tina_mutex);
322
 
  free((unsigned char*) share);
323
 
 
324
 
  return NULL;
325
344
}
326
345
 
327
346
 
344
363
    non-zero - error occurred
345
364
*/
346
365
 
347
 
static int read_meta_file(File meta_file, ha_rows *rows)
 
366
static int read_meta_file(int meta_file, ha_rows *rows)
348
367
{
349
368
  unsigned char meta_buffer[META_BUFFER_SIZE];
350
369
  unsigned char *ptr= meta_buffer;
397
416
    non-zero - error occurred
398
417
*/
399
418
 
400
 
static int write_meta_file(File meta_file, ha_rows rows, bool dirty)
 
419
static int write_meta_file(int meta_file, ha_rows rows, bool dirty)
401
420
{
402
421
  unsigned char meta_buffer[META_BUFFER_SIZE];
403
422
  unsigned char *ptr= meta_buffer;
450
469
/*
451
470
  Free lock controls.
452
471
*/
453
 
static int free_share(TINA_SHARE *share)
 
472
int ha_tina::free_share()
454
473
{
455
474
  pthread_mutex_lock(&tina_mutex);
456
475
  int result_code= 0;
467
486
      share->tina_write_opened= false;
468
487
    }
469
488
 
470
 
    hash_delete(&tina_open_tables, (unsigned char*) share);
471
 
    thr_lock_delete(&share->lock);
472
 
    pthread_mutex_destroy(&share->mutex);
473
 
    free((unsigned char*) share);
 
489
    Tina *a_tina= static_cast<Tina *>(engine);
 
490
    a_tina->deleteOpenTable(share->table_name);
 
491
    delete share;
474
492
  }
475
493
  pthread_mutex_unlock(&tina_mutex);
476
494
 
677
695
  int eoln_len;
678
696
  int error;
679
697
 
680
 
  free_root(&blobroot, MYF(MY_MARK_BLOCKS_FREE));
 
698
  free_root(&blobroot, MYF(drizzled::memory::MARK_BLOCKS_FREE));
681
699
 
682
700
  /*
683
701
    We do not read further then local_saved_data_file_length in order
864
882
  this will not be called for every request. Any sort of positions
865
883
  that need to be reset should be kept in the ::extra() call.
866
884
*/
867
 
int ha_tina::open(const char *name, int, uint32_t open_options)
 
885
int ha_tina::open(const char *name, int, uint32_t)
868
886
{
869
 
  if (!(share= get_share(name, table)))
 
887
  if (!(share= get_share(name)))
870
888
    return(ENOENT);
871
889
 
872
 
  if (share->crashed && !(open_options & HA_OPEN_FOR_REPAIR))
 
890
  if (share->crashed)
873
891
  {
874
 
    free_share(share);
 
892
    free_share();
875
893
    return(HA_ERR_CRASHED_ON_USAGE);
876
894
  }
877
895
 
903
921
{
904
922
  int rc= 0;
905
923
  rc= my_close(data_file, MYF(0));
906
 
  return(free_share(share) || rc);
 
924
  return(free_share() || rc);
907
925
}
908
926
 
909
927
/*
952
970
  if (!share->update_file_opened)
953
971
  {
954
972
    if ((update_temp_file=
955
 
           my_create(fn_format(updated_fname, share->table_name,
 
973
           my_create(fn_format(updated_fname, share->table_name.c_str(),
956
974
                               "", CSN_EXT,
957
975
                               MY_REPLACE_EXT | MY_UNPACK_FILENAME),
958
976
                     0, O_RDWR | O_TRUNC, MYF(MY_WME))) < 0)
1100
1118
  records_is_known= 0;
1101
1119
  chain_ptr= chain;
1102
1120
 
1103
 
  init_alloc_root(&blobroot, BLOB_MEMROOT_ALLOC_SIZE, 0);
 
1121
  init_alloc_root(&blobroot, BLOB_MEMROOT_ALLOC_SIZE);
1104
1122
 
1105
1123
  return(0);
1106
1124
}
1293
1311
      of the old datafile.
1294
1312
    */
1295
1313
    if (my_close(data_file, MYF(0)) ||
1296
 
        my_rename(fn_format(updated_fname, share->table_name, "", CSN_EXT,
 
1314
        my_rename(fn_format(updated_fname, share->table_name.c_str(),
 
1315
                            "", CSN_EXT,
1297
1316
                            MY_REPLACE_EXT | MY_UNPACK_FILENAME),
1298
1317
                  share->data_file_name, MYF(0)))
1299
1318
      return(-1);
1341
1360
  int rc;
1342
1361
 
1343
1362
  if (!records_is_known)
1344
 
    return(my_errno=HA_ERR_WRONG_COMMAND);
 
1363
    return(errno=HA_ERR_WRONG_COMMAND);
1345
1364
 
1346
1365
  if (!share->tina_write_opened)
1347
1366
    if (init_tina_writer())
1369
1388
                        drizzled::message::Table& create_proto)
1370
1389
{
1371
1390
  char name_buff[FN_REFLEN];
1372
 
  File create_file;
 
1391
  int create_file;
1373
1392
 
1374
1393
  /*
1375
1394
    check columns
1407
1426
}
1408
1427
 
1409
1428
 
1410
 
drizzle_declare_plugin
 
1429
DRIZZLE_DECLARE_PLUGIN
1411
1430
{
 
1431
  DRIZZLE_VERSION_ID,
1412
1432
  "CSV",
1413
1433
  "1.0",
1414
1434
  "Brian Aker, MySQL AB",
1420
1440
  NULL,                       /* system variables                */
1421
1441
  NULL                        /* config options                  */
1422
1442
}
1423
 
drizzle_declare_plugin_end;
 
1443
DRIZZLE_DECLARE_PLUGIN_END;
1424
1444