~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/csv/ha_tina.cc

Updated an include guard thanks to a nice catch during code review from Jay. Thanks Jay!

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
 
41
41
 -Brian
42
42
*/
43
 
#include "config.h"
 
43
#include <drizzled/server_includes.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/internal/my_sys.h"
 
50
#include "drizzled/memory/multi_malloc.h"
51
51
 
52
52
#include "ha_tina.h"
53
53
 
54
 
#include <fcntl.h>
55
 
 
56
54
#include <string>
57
 
#include <map>
58
55
 
59
56
using namespace std;
60
 
using namespace drizzled;
61
57
 
62
58
/*
63
59
  unsigned char + unsigned char + uint64_t + uint64_t + uint64_t + uint64_t + unsigned char
73
69
#define CSM_EXT ".CSM"               // Meta file
74
70
 
75
71
 
76
 
static int read_meta_file(int meta_file, ha_rows *rows);
77
 
static int write_meta_file(int meta_file, ha_rows rows, bool dirty);
 
72
static TINA_SHARE *get_share(const char *table_name, Table *table);
 
73
static int free_share(TINA_SHARE *share);
 
74
static int read_meta_file(File meta_file, ha_rows *rows);
 
75
static int write_meta_file(File meta_file, ha_rows rows, bool dirty);
78
76
 
79
 
void tina_get_status(void* param, int concurrent_insert);
80
 
void tina_update_status(void* param);
81
 
bool tina_check_status(void* param);
 
77
extern "C" void tina_get_status(void* param, int concurrent_insert);
 
78
extern "C" void tina_update_status(void* param);
 
79
extern "C" bool tina_check_status(void* param);
82
80
 
83
81
/* Stuff for shares */
84
82
pthread_mutex_t tina_mutex;
 
83
static HASH tina_open_tables;
85
84
 
86
85
/*****************************************************************************
87
86
 ** TINA tables
99
98
  return ( a->begin > b->begin ? 1 : ( a->begin < b->begin ? -1 : 0 ) );
100
99
}
101
100
 
 
101
static unsigned char* tina_get_key(TINA_SHARE *share, size_t *length, bool)
 
102
{
 
103
  *length=share->table_name_length;
 
104
  return (unsigned char*) share->table_name;
 
105
}
 
106
 
102
107
 
103
108
/*
104
109
  If frm_error() is called in table.cc this is called to find out what file
112
117
 
113
118
class Tina : public drizzled::plugin::StorageEngine
114
119
{
115
 
  typedef std::map<string, TinaShare*> TinaMap;
116
 
  TinaMap tina_open_tables;
117
120
public:
118
121
  Tina(const string& name_arg)
119
122
   : drizzled::plugin::StorageEngine(name_arg,
120
123
                                     HTON_TEMPORARY_ONLY |
121
124
                                     HTON_NO_AUTO_INCREMENT |
 
125
                                     HTON_HAS_DATA_DICTIONARY |
122
126
                                     HTON_SKIP_STORE_LOCK |
123
 
                                     HTON_FILE_BASED),
124
 
    tina_open_tables()
125
 
  {}
126
 
  virtual ~Tina()
127
 
  {
128
 
    pthread_mutex_destroy(&tina_mutex);
129
 
  }
130
 
 
 
127
                                     HTON_FILE_BASED) {}
131
128
  virtual Cursor *create(TableShare &table,
132
 
                         drizzled::memory::Root *mem_root)
 
129
                          MEM_ROOT *mem_root)
133
130
  {
134
131
    return new (mem_root) ha_tina(*this, table);
135
132
  }
138
135
    return ha_tina_exts;
139
136
  }
140
137
 
141
 
  int doCreateTable(Session &,
142
 
                    Table &table_arg,
143
 
                    drizzled::TableIdentifier &identifier,
 
138
  int doCreateTable(Session *,
 
139
                    const char *table_name,
 
140
                    Table& table_arg,
144
141
                    drizzled::message::Table&);
145
142
 
146
143
  int doGetTableDefinition(Session& session,
147
 
                           TableIdentifier &identifier,
148
 
                           drizzled::message::Table &table_message);
 
144
                           const char* path,
 
145
                           const char *db,
 
146
                           const char *table_name,
 
147
                           const bool is_tmp,
 
148
                           drizzled::message::Table *table_proto);
149
149
 
150
150
  /* Temp only engine, so do not return values. */
151
 
  void doGetTableNames(drizzled::CachedDirectory &, SchemaIdentifier&, set<string>&) { };
152
 
 
153
 
  int doDropTable(Session&, TableIdentifier &identifier);
154
 
  TinaShare *findOpenTable(const string table_name);
155
 
  void addOpenTable(const string &table_name, TinaShare *);
156
 
  void deleteOpenTable(const string &table_name);
157
 
 
 
151
  void doGetTableNames(CachedDirectory &, string& , set<string>&) { };
 
152
 
 
153
  int doDropTable(Session&, const string table_path);
158
154
 
159
155
  uint32_t max_keys()          const { return 0; }
160
156
  uint32_t max_key_parts()     const { return 0; }
161
157
  uint32_t max_key_length()    const { return 0; }
162
 
  bool doDoesTableExist(Session& session, TableIdentifier &identifier);
163
 
  int doRenameTable(Session&, TableIdentifier &from, TableIdentifier &to);
164
 
 
165
 
  void doGetTableIdentifiers(drizzled::CachedDirectory &directory,
166
 
                             drizzled::SchemaIdentifier &schema_identifier,
167
 
                             drizzled::TableIdentifiers &set_of_identifiers);
168
158
};
169
159
 
170
 
void Tina::doGetTableIdentifiers(drizzled::CachedDirectory&,
171
 
                                 drizzled::SchemaIdentifier&,
172
 
                                 drizzled::TableIdentifiers&)
173
 
{
174
 
}
175
 
 
176
 
int Tina::doRenameTable(Session &session,
177
 
                        TableIdentifier &from, TableIdentifier &to)
178
 
{
179
 
  int error= 0;
180
 
  for (const char **ext= bas_ext(); *ext ; ext++)
181
 
  {
182
 
    if (rename_file_ext(from.getPath().c_str(), to.getPath().c_str(), *ext))
183
 
    {
184
 
      if ((error=errno) != ENOENT)
185
 
        break;
186
 
      error= 0;
187
 
    }
188
 
  }
189
 
 
190
 
  session.renameTableMessage(from, to);
191
 
 
192
 
  return error;
193
 
}
194
 
 
195
 
bool Tina::doDoesTableExist(Session &session, TableIdentifier &identifier)
196
 
{
197
 
  return session.doesTableMessageExist(identifier);
198
 
}
199
 
 
200
 
 
201
 
int Tina::doDropTable(Session &session,
202
 
                      TableIdentifier &identifier)
 
160
int Tina::doDropTable(Session&,
 
161
                        const string table_path)
203
162
{
204
163
  int error= 0;
205
164
  int enoent_or_zero= ENOENT;                   // Error if no file was deleted
206
165
  char buff[FN_REFLEN];
 
166
  ProtoCache::iterator iter;
207
167
 
208
168
  for (const char **ext= bas_ext(); *ext ; ext++)
209
169
  {
210
 
    internal::fn_format(buff, identifier.getPath().c_str(), "", *ext,
211
 
                        MY_UNPACK_FILENAME|MY_APPEND_EXT);
212
 
    if (internal::my_delete_with_symlink(buff, MYF(0)))
 
170
    fn_format(buff, table_path.c_str(), "", *ext,
 
171
              MY_UNPACK_FILENAME|MY_APPEND_EXT);
 
172
    if (my_delete_with_symlink(buff, MYF(0)))
213
173
    {
214
 
      if ((error= errno) != ENOENT)
 
174
      if ((error= my_errno) != ENOENT)
215
175
        break;
216
176
    }
217
177
    else
219
179
    error= enoent_or_zero;
220
180
  }
221
181
 
222
 
  session.removeTableMessage(identifier);
223
 
 
224
 
  return error;
225
 
}
226
 
 
227
 
TinaShare *Tina::findOpenTable(const string table_name)
228
 
{
229
 
  TinaMap::iterator find_iter=
230
 
    tina_open_tables.find(table_name);
231
 
 
232
 
  if (find_iter != tina_open_tables.end())
233
 
    return (*find_iter).second;
234
 
  else
235
 
    return NULL;
236
 
}
237
 
 
238
 
void Tina::addOpenTable(const string &table_name, TinaShare *share)
239
 
{
240
 
  tina_open_tables[table_name]= share;
241
 
}
242
 
 
243
 
void Tina::deleteOpenTable(const string &table_name)
244
 
{
245
 
  tina_open_tables.erase(table_name);
246
 
}
247
 
 
248
 
 
249
 
int Tina::doGetTableDefinition(Session &session,
250
 
                               drizzled::TableIdentifier &identifier,
251
 
                               drizzled::message::Table &table_message)
252
 
{
253
 
  if (session.getTableMessage(identifier, table_message))
254
 
    return EEXIST;
255
 
 
256
 
  return ENOENT;
 
182
  pthread_mutex_lock(&proto_cache_mutex);
 
183
  iter= proto_cache.find(table_path.c_str());
 
184
 
 
185
  if (iter!= proto_cache.end())
 
186
    proto_cache.erase(iter);
 
187
  pthread_mutex_unlock(&proto_cache_mutex);
 
188
 
 
189
  return error;
 
190
}
 
191
 
 
192
int Tina::doGetTableDefinition(Session&,
 
193
                               const char* path,
 
194
                               const char *,
 
195
                               const char *,
 
196
                               const bool,
 
197
                               drizzled::message::Table *table_proto)
 
198
{
 
199
  int error= ENOENT;
 
200
  ProtoCache::iterator iter;
 
201
 
 
202
  pthread_mutex_lock(&proto_cache_mutex);
 
203
  iter= proto_cache.find(path);
 
204
 
 
205
  if (iter!= proto_cache.end())
 
206
  {
 
207
    if (table_proto)
 
208
      table_proto->CopyFrom(((*iter).second));
 
209
    error= EEXIST;
 
210
  }
 
211
  pthread_mutex_unlock(&proto_cache_mutex);
 
212
 
 
213
  return error;
257
214
}
258
215
 
259
216
 
260
217
static Tina *tina_engine= NULL;
261
218
 
262
 
static int tina_init_func(drizzled::plugin::Context &context)
 
219
static int tina_init_func(drizzled::plugin::Registry &registry)
263
220
{
264
221
 
265
222
  tina_engine= new Tina("CSV");
266
 
  context.add(tina_engine);
 
223
  registry.add(tina_engine);
267
224
 
268
225
  pthread_mutex_init(&tina_mutex,MY_MUTEX_INIT_FAST);
269
 
  return 0;
270
 
}
271
 
 
272
 
 
273
 
 
274
 
TinaShare::TinaShare(const char *table_name_arg)
275
 
  : table_name(table_name_arg), use_count(0), saved_data_file_length(0),
276
 
    update_file_opened(false), tina_write_opened(false),
277
 
    crashed(false), rows_recorded(0), data_file_version(0)
278
 
{
279
 
  thr_lock_init(&lock);
280
 
  internal::fn_format(data_file_name, table_name_arg, "", CSV_EXT,
281
 
            MY_REPLACE_EXT|MY_UNPACK_FILENAME);
282
 
}
283
 
 
284
 
TinaShare::~TinaShare()
285
 
{
286
 
  thr_lock_delete(&lock);
287
 
  pthread_mutex_destroy(&mutex);
288
 
}
 
226
  (void) hash_init(&tina_open_tables,system_charset_info,32,0,0,
 
227
                   (hash_get_key) tina_get_key,0,0);
 
228
  return 0;
 
229
}
 
230
 
 
231
static int tina_done_func(drizzled::plugin::Registry &registry)
 
232
{
 
233
  registry.remove(tina_engine);
 
234
  delete tina_engine;
 
235
 
 
236
  hash_free(&tina_open_tables);
 
237
  pthread_mutex_destroy(&tina_mutex);
 
238
 
 
239
  return 0;
 
240
}
 
241
 
289
242
 
290
243
/*
291
244
  Simple lock controls.
292
245
*/
293
 
TinaShare *ha_tina::get_share(const char *table_name)
 
246
static TINA_SHARE *get_share(const char *table_name, Table *)
294
247
{
295
 
  pthread_mutex_lock(&tina_mutex);
296
 
 
297
 
  Tina *a_tina= static_cast<Tina *>(engine);
298
 
  share= a_tina->findOpenTable(table_name);
299
 
 
 
248
  TINA_SHARE *share;
300
249
  char meta_file_name[FN_REFLEN];
301
250
  struct stat file_stat;
 
251
  char *tmp_name;
 
252
  uint32_t length;
 
253
 
 
254
  pthread_mutex_lock(&tina_mutex);
 
255
  length=(uint) strlen(table_name);
302
256
 
303
257
  /*
304
258
    If share is not present in the hash, create a new share and
305
259
    initialize its members.
306
260
  */
307
 
  if (! share)
 
261
  if (!(share=(TINA_SHARE*) hash_search(&tina_open_tables,
 
262
                                        (unsigned char*) table_name,
 
263
                                       length)))
308
264
  {
309
 
    share= new TinaShare(table_name);
310
 
 
311
 
    if (share == NULL)
 
265
    if (!drizzled::memory::multi_malloc(true,
 
266
                         &share, sizeof(*share),
 
267
                         &tmp_name, length+1,
 
268
                         NULL))
312
269
    {
313
270
      pthread_mutex_unlock(&tina_mutex);
314
271
      return NULL;
315
272
    }
316
273
 
317
 
    internal::fn_format(meta_file_name, table_name, "", CSM_EXT,
 
274
    share->use_count= 0;
 
275
    share->table_name_length= length;
 
276
    share->table_name= tmp_name;
 
277
    share->crashed= false;
 
278
    share->rows_recorded= 0;
 
279
    share->update_file_opened= false;
 
280
    share->tina_write_opened= false;
 
281
    share->data_file_version= 0;
 
282
    strcpy(share->table_name, table_name);
 
283
    fn_format(share->data_file_name, table_name, "", CSV_EXT,
 
284
              MY_REPLACE_EXT|MY_UNPACK_FILENAME);
 
285
    fn_format(meta_file_name, table_name, "", CSM_EXT,
318
286
              MY_REPLACE_EXT|MY_UNPACK_FILENAME);
319
287
 
320
288
    if (stat(share->data_file_name, &file_stat))
321
 
    {
322
 
      pthread_mutex_unlock(&tina_mutex);
323
 
      delete share;
324
 
      return NULL;
325
 
    }
326
 
  
 
289
      goto error;
327
290
    share->saved_data_file_length= file_stat.st_size;
328
291
 
329
 
    a_tina->addOpenTable(share->table_name, share);
330
 
 
 
292
    if (my_hash_insert(&tina_open_tables, (unsigned char*) share))
 
293
      goto error;
 
294
    thr_lock_init(&share->lock);
331
295
    pthread_mutex_init(&share->mutex,MY_MUTEX_INIT_FAST);
332
296
 
333
297
    /*
336
300
      Usually this will result in auto-repair, and we will get a good
337
301
      meta-file in the end.
338
302
    */
339
 
    if ((share->meta_file= internal::my_open(meta_file_name,
340
 
                                             O_RDWR|O_CREAT, MYF(0))) == -1)
 
303
    if ((share->meta_file= my_open(meta_file_name,
 
304
                                   O_RDWR|O_CREAT, MYF(0))) == -1)
341
305
      share->crashed= true;
342
306
 
343
307
    /*
351
315
  pthread_mutex_unlock(&tina_mutex);
352
316
 
353
317
  return share;
 
318
 
 
319
error:
 
320
  pthread_mutex_unlock(&tina_mutex);
 
321
  free((unsigned char*) share);
 
322
 
 
323
  return NULL;
354
324
}
355
325
 
356
326
 
373
343
    non-zero - error occurred
374
344
*/
375
345
 
376
 
static int read_meta_file(int meta_file, ha_rows *rows)
 
346
static int read_meta_file(File meta_file, ha_rows *rows)
377
347
{
378
348
  unsigned char meta_buffer[META_BUFFER_SIZE];
379
349
  unsigned char *ptr= meta_buffer;
380
350
 
381
351
  lseek(meta_file, 0, SEEK_SET);
382
 
  if (internal::my_read(meta_file, (unsigned char*)meta_buffer, META_BUFFER_SIZE, 0)
 
352
  if (my_read(meta_file, (unsigned char*)meta_buffer, META_BUFFER_SIZE, 0)
383
353
      != META_BUFFER_SIZE)
384
354
    return(HA_ERR_CRASHED_ON_USAGE);
385
355
 
401
371
      ((bool)(*ptr)== true))
402
372
    return(HA_ERR_CRASHED_ON_USAGE);
403
373
 
404
 
  internal::my_sync(meta_file, MYF(MY_WME));
 
374
  my_sync(meta_file, MYF(MY_WME));
405
375
 
406
376
  return(0);
407
377
}
426
396
    non-zero - error occurred
427
397
*/
428
398
 
429
 
static int write_meta_file(int meta_file, ha_rows rows, bool dirty)
 
399
static int write_meta_file(File meta_file, ha_rows rows, bool dirty)
430
400
{
431
401
  unsigned char meta_buffer[META_BUFFER_SIZE];
432
402
  unsigned char *ptr= meta_buffer;
446
416
  *ptr= (unsigned char)dirty;
447
417
 
448
418
  lseek(meta_file, 0, SEEK_SET);
449
 
  if (internal::my_write(meta_file, (unsigned char *)meta_buffer, META_BUFFER_SIZE, 0)
 
419
  if (my_write(meta_file, (unsigned char *)meta_buffer, META_BUFFER_SIZE, 0)
450
420
      != META_BUFFER_SIZE)
451
421
    return(-1);
452
422
 
453
 
  internal::my_sync(meta_file, MYF(MY_WME));
 
423
  my_sync(meta_file, MYF(MY_WME));
454
424
 
455
425
  return(0);
456
426
}
465
435
  (void)write_meta_file(share->meta_file, share->rows_recorded, true);
466
436
 
467
437
  if ((share->tina_write_filedes=
468
 
        internal::my_open(share->data_file_name, O_RDWR|O_APPEND, MYF(0))) == -1)
 
438
        my_open(share->data_file_name, O_RDWR|O_APPEND, MYF(0))) == -1)
469
439
  {
470
440
    share->crashed= true;
471
441
    return(1);
479
449
/*
480
450
  Free lock controls.
481
451
*/
482
 
int ha_tina::free_share()
 
452
static int free_share(TINA_SHARE *share)
483
453
{
484
454
  pthread_mutex_lock(&tina_mutex);
485
455
  int result_code= 0;
487
457
    /* Write the meta file. Mark it as crashed if needed. */
488
458
    (void)write_meta_file(share->meta_file, share->rows_recorded,
489
459
                          share->crashed ? true :false);
490
 
    if (internal::my_close(share->meta_file, MYF(0)))
 
460
    if (my_close(share->meta_file, MYF(0)))
491
461
      result_code= 1;
492
462
    if (share->tina_write_opened)
493
463
    {
494
 
      if (internal::my_close(share->tina_write_filedes, MYF(0)))
 
464
      if (my_close(share->tina_write_filedes, MYF(0)))
495
465
        result_code= 1;
496
466
      share->tina_write_opened= false;
497
467
    }
498
468
 
499
 
    Tina *a_tina= static_cast<Tina *>(engine);
500
 
    a_tina->deleteOpenTable(share->table_name);
501
 
    delete share;
 
469
    hash_delete(&tina_open_tables, (unsigned char*) share);
 
470
    thr_lock_delete(&share->lock);
 
471
    pthread_mutex_destroy(&share->mutex);
 
472
    free((unsigned char*) share);
502
473
  }
503
474
  pthread_mutex_unlock(&tina_mutex);
504
475
 
705
676
  int eoln_len;
706
677
  int error;
707
678
 
708
 
  free_root(&blobroot, MYF(drizzled::memory::MARK_BLOCKS_FREE));
 
679
  free_root(&blobroot, MYF(MY_MARK_BLOCKS_FREE));
709
680
 
710
681
  /*
711
682
    We do not read further then local_saved_data_file_length in order
894
865
*/
895
866
int ha_tina::open(const char *name, int, uint32_t)
896
867
{
897
 
  if (!(share= get_share(name)))
 
868
  if (!(share= get_share(name, table)))
898
869
    return(ENOENT);
899
870
 
900
871
  if (share->crashed)
901
872
  {
902
 
    free_share();
 
873
    free_share(share);
903
874
    return(HA_ERR_CRASHED_ON_USAGE);
904
875
  }
905
876
 
906
877
  local_data_file_version= share->data_file_version;
907
 
  if ((data_file= internal::my_open(share->data_file_name, O_RDONLY, MYF(0))) == -1)
 
878
  if ((data_file= my_open(share->data_file_name, O_RDONLY, MYF(0))) == -1)
908
879
    return(0);
909
880
 
910
881
  /*
930
901
int ha_tina::close(void)
931
902
{
932
903
  int rc= 0;
933
 
  rc= internal::my_close(data_file, MYF(0));
934
 
  return(free_share() || rc);
 
904
  rc= my_close(data_file, MYF(0));
 
905
  return(free_share(share) || rc);
935
906
}
936
907
 
937
908
/*
946
917
  if (share->crashed)
947
918
      return(HA_ERR_CRASHED_ON_USAGE);
948
919
 
949
 
  ha_statistic_increment(&system_status_var::ha_write_count);
 
920
  ha_statistic_increment(&SSV::ha_write_count);
950
921
 
951
922
  size= encode_quote(buf);
952
923
 
955
926
      return(-1);
956
927
 
957
928
   /* use pwrite, as concurrent reader could have changed the position */
958
 
  if (internal::my_write(share->tina_write_filedes, (unsigned char*)buffer.ptr(), size,
 
929
  if (my_write(share->tina_write_filedes, (unsigned char*)buffer.ptr(), size,
959
930
               MYF(MY_WME | MY_NABP)))
960
931
    return(-1);
961
932
 
980
951
  if (!share->update_file_opened)
981
952
  {
982
953
    if ((update_temp_file=
983
 
           internal::my_create(internal::fn_format(updated_fname, share->table_name.c_str(),
 
954
           my_create(fn_format(updated_fname, share->table_name,
984
955
                               "", CSN_EXT,
985
956
                               MY_REPLACE_EXT | MY_UNPACK_FILENAME),
986
957
                     0, O_RDWR | O_TRUNC, MYF(MY_WME))) < 0)
1004
975
  int size;
1005
976
  int rc= -1;
1006
977
 
1007
 
  ha_statistic_increment(&system_status_var::ha_update_count);
 
978
  ha_statistic_increment(&SSV::ha_update_count);
1008
979
 
1009
980
  if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_UPDATE)
1010
981
    table->timestamp_field->set_time();
1024
995
  if (open_update_temp_file_if_needed())
1025
996
    goto err;
1026
997
 
1027
 
  if (internal::my_write(update_temp_file, (unsigned char*)buffer.ptr(), size,
 
998
  if (my_write(update_temp_file, (unsigned char*)buffer.ptr(), size,
1028
999
               MYF(MY_WME | MY_NABP)))
1029
1000
    goto err;
1030
1001
  temp_file_length+= size;
1046
1017
*/
1047
1018
int ha_tina::delete_row(const unsigned char *)
1048
1019
{
1049
 
  ha_statistic_increment(&system_status_var::ha_delete_count);
 
1020
  ha_statistic_increment(&SSV::ha_delete_count);
1050
1021
 
1051
1022
  if (chain_append())
1052
1023
    return(-1);
1080
1051
  if (local_data_file_version != share->data_file_version)
1081
1052
  {
1082
1053
    local_data_file_version= share->data_file_version;
1083
 
    if (internal::my_close(data_file, MYF(0)) ||
1084
 
        (data_file= internal::my_open(share->data_file_name, O_RDONLY, MYF(0))) == -1)
 
1054
    if (my_close(data_file, MYF(0)) ||
 
1055
        (data_file= my_open(share->data_file_name, O_RDONLY, MYF(0))) == -1)
1085
1056
      return 1;
1086
1057
  }
1087
1058
  file_buff->init_buff(data_file);
1128
1099
  records_is_known= 0;
1129
1100
  chain_ptr= chain;
1130
1101
 
1131
 
  init_alloc_root(&blobroot, BLOB_MEMROOT_ALLOC_SIZE);
 
1102
  init_alloc_root(&blobroot, BLOB_MEMROOT_ALLOC_SIZE, 0);
1132
1103
 
1133
1104
  return(0);
1134
1105
}
1154
1125
  if (share->crashed)
1155
1126
      return(HA_ERR_CRASHED_ON_USAGE);
1156
1127
 
1157
 
  ha_statistic_increment(&system_status_var::ha_read_rnd_next_count);
 
1128
  ha_statistic_increment(&SSV::ha_read_rnd_next_count);
1158
1129
 
1159
1130
  current_position= next_position;
1160
1131
 
1180
1151
*/
1181
1152
void ha_tina::position(const unsigned char *)
1182
1153
{
1183
 
  internal::my_store_ptr(ref, ref_length, current_position);
 
1154
  my_store_ptr(ref, ref_length, current_position);
1184
1155
  return;
1185
1156
}
1186
1157
 
1187
1158
 
1188
1159
/*
1189
1160
  Used to fetch a row from a posiion stored with ::position().
1190
 
  internal::my_get_ptr() retrieves the data for you.
 
1161
  my_get_ptr() retrieves the data for you.
1191
1162
*/
1192
1163
 
1193
1164
int ha_tina::rnd_pos(unsigned char * buf, unsigned char *pos)
1194
1165
{
1195
 
  ha_statistic_increment(&system_status_var::ha_read_rnd_count);
1196
 
  current_position= (off_t)internal::my_get_ptr(pos,ref_length);
 
1166
  ha_statistic_increment(&SSV::ha_read_rnd_count);
 
1167
  current_position= (off_t)my_get_ptr(pos,ref_length);
1197
1168
  return(find_current_row(buf));
1198
1169
}
1199
1170
 
1254
1225
      The sort is needed when there were updates/deletes with random orders.
1255
1226
      It sorts so that we move the firts blocks to the beginning.
1256
1227
    */
1257
 
    internal::my_qsort(chain, (size_t)(chain_ptr - chain), sizeof(tina_set),
1258
 
                       (qsort_cmp)sort_set);
 
1228
    my_qsort(chain, (size_t)(chain_ptr - chain), sizeof(tina_set),
 
1229
             (qsort_cmp)sort_set);
1259
1230
 
1260
1231
    off_t write_begin= 0, write_end;
1261
1232
 
1276
1247
      /* if there is something to write, write it */
1277
1248
      if (write_length)
1278
1249
      {
1279
 
        if (internal::my_write(update_temp_file,
 
1250
        if (my_write(update_temp_file,
1280
1251
                     (unsigned char*) (file_buff->ptr() +
1281
1252
                               (write_begin - file_buff->start())),
1282
1253
                     (size_t)write_length, MYF_RW))
1299
1270
 
1300
1271
    }
1301
1272
 
1302
 
    if (internal::my_sync(update_temp_file, MYF(MY_WME)) ||
1303
 
        internal::my_close(update_temp_file, MYF(0)))
 
1273
    if (my_sync(update_temp_file, MYF(MY_WME)) ||
 
1274
        my_close(update_temp_file, MYF(0)))
1304
1275
      return(-1);
1305
1276
 
1306
1277
    share->update_file_opened= false;
1307
1278
 
1308
1279
    if (share->tina_write_opened)
1309
1280
    {
1310
 
      if (internal::my_close(share->tina_write_filedes, MYF(0)))
 
1281
      if (my_close(share->tina_write_filedes, MYF(0)))
1311
1282
        return(-1);
1312
1283
      /*
1313
1284
        Mark that the writer fd is closed, so that init_tina_writer()
1320
1291
      Close opened fildes's. Then move updated file in place
1321
1292
      of the old datafile.
1322
1293
    */
1323
 
    if (internal::my_close(data_file, MYF(0)) ||
1324
 
        internal::my_rename(internal::fn_format(updated_fname,
1325
 
                                                share->table_name.c_str(),
1326
 
                                                "", CSN_EXT,
1327
 
                                                MY_REPLACE_EXT | MY_UNPACK_FILENAME),
1328
 
                            share->data_file_name, MYF(0)))
 
1294
    if (my_close(data_file, MYF(0)) ||
 
1295
        my_rename(fn_format(updated_fname, share->table_name, "", CSN_EXT,
 
1296
                            MY_REPLACE_EXT | MY_UNPACK_FILENAME),
 
1297
                  share->data_file_name, MYF(0)))
1329
1298
      return(-1);
1330
1299
 
1331
1300
    /* Open the file again */
1332
 
    if (((data_file= internal::my_open(share->data_file_name, O_RDONLY, MYF(0))) == -1))
 
1301
    if (((data_file= my_open(share->data_file_name, O_RDONLY, MYF(0))) == -1))
1333
1302
      return(-1);
1334
1303
    /*
1335
1304
      As we reopened the data file, increase share->data_file_version
1356
1325
 
1357
1326
  return(0);
1358
1327
error:
1359
 
  internal::my_close(update_temp_file, MYF(0));
 
1328
  my_close(update_temp_file, MYF(0));
1360
1329
  share->update_file_opened= false;
1361
1330
  return(-1);
1362
1331
}
1371
1340
  int rc;
1372
1341
 
1373
1342
  if (!records_is_known)
1374
 
    return(errno=HA_ERR_WRONG_COMMAND);
 
1343
    return(my_errno=HA_ERR_WRONG_COMMAND);
1375
1344
 
1376
1345
  if (!share->tina_write_opened)
1377
1346
    if (init_tina_writer())
1394
1363
  this (the database will call ::open() if it needs to).
1395
1364
*/
1396
1365
 
1397
 
int Tina::doCreateTable(Session &session,
 
1366
int Tina::doCreateTable(Session *, const char *table_name,
1398
1367
                        Table& table_arg,
1399
 
                        drizzled::TableIdentifier &identifier,
1400
 
                        drizzled::message::Table &create_proto)
 
1368
                        drizzled::message::Table& create_proto)
1401
1369
{
1402
1370
  char name_buff[FN_REFLEN];
1403
 
  int create_file;
 
1371
  File create_file;
1404
1372
 
1405
1373
  /*
1406
1374
    check columns
1415
1383
  }
1416
1384
 
1417
1385
 
1418
 
  if ((create_file= internal::my_create(internal::fn_format(name_buff, identifier.getPath().c_str(), "", CSM_EXT,
1419
 
                                                            MY_REPLACE_EXT|MY_UNPACK_FILENAME), 0,
1420
 
                                        O_RDWR | O_TRUNC,MYF(MY_WME))) < 0)
 
1386
  if ((create_file= my_create(fn_format(name_buff, table_name, "", CSM_EXT,
 
1387
                                        MY_REPLACE_EXT|MY_UNPACK_FILENAME), 0,
 
1388
                              O_RDWR | O_TRUNC,MYF(MY_WME))) < 0)
1421
1389
    return(-1);
1422
1390
 
1423
1391
  write_meta_file(create_file, 0, false);
1424
 
  internal::my_close(create_file, MYF(0));
 
1392
  my_close(create_file, MYF(0));
1425
1393
 
1426
 
  if ((create_file= internal::my_create(internal::fn_format(name_buff, identifier.getPath().c_str(), "", CSV_EXT,
1427
 
                                                            MY_REPLACE_EXT|MY_UNPACK_FILENAME),0,
1428
 
                                        O_RDWR | O_TRUNC,MYF(MY_WME))) < 0)
 
1394
  if ((create_file= my_create(fn_format(name_buff, table_name, "", CSV_EXT,
 
1395
                                        MY_REPLACE_EXT|MY_UNPACK_FILENAME),0,
 
1396
                              O_RDWR | O_TRUNC,MYF(MY_WME))) < 0)
1429
1397
    return(-1);
1430
1398
 
1431
 
  internal::my_close(create_file, MYF(0));
 
1399
  my_close(create_file, MYF(0));
1432
1400
 
1433
 
  session.storeTableMessage(identifier, create_proto);
 
1401
  pthread_mutex_lock(&proto_cache_mutex);
 
1402
  proto_cache.insert(make_pair(table_name, create_proto));
 
1403
  pthread_mutex_unlock(&proto_cache_mutex);
1434
1404
 
1435
1405
  return 0;
1436
1406
}
1438
1408
 
1439
1409
DRIZZLE_DECLARE_PLUGIN
1440
1410
{
1441
 
  DRIZZLE_VERSION_ID,
1442
1411
  "CSV",
1443
1412
  "1.0",
1444
1413
  "Brian Aker, MySQL AB",
1445
1414
  "CSV storage engine",
1446
1415
  PLUGIN_LICENSE_GPL,
1447
1416
  tina_init_func, /* Plugin Init */
 
1417
  tina_done_func, /* Plugin Deinit */
 
1418
  NULL,                       /* status variables                */
1448
1419
  NULL,                       /* system variables                */
1449
1420
  NULL                        /* config options                  */
1450
1421
}