75
63
#define CSM_EXT ".CSM" // Meta file
78
static int read_meta_file(int meta_file, ha_rows *rows);
79
static int write_meta_file(int meta_file, ha_rows rows, bool dirty);
66
static TINA_SHARE *get_share(const char *table_name, Table *table);
67
static int free_share(TINA_SHARE *share);
68
static int read_meta_file(File meta_file, ha_rows *rows);
69
static int write_meta_file(File meta_file, ha_rows rows, bool dirty);
71
extern "C" void tina_get_status(void* param, int concurrent_insert);
72
extern "C" void tina_update_status(void* param);
73
extern "C" bool tina_check_status(void* param);
81
75
/* Stuff for shares */
82
76
pthread_mutex_t tina_mutex;
77
static HASH tina_open_tables;
78
static handler *tina_create_handler(handlerton *hton,
84
83
/*****************************************************************************
86
85
*****************************************************************************/
89
If frm_error() is called in table.cc this is called to find out what file
90
extensions exist for this Cursor.
88
Used for sorting chains with qsort().
92
static const char *ha_tina_exts[] = {
98
class Tina : public drizzled::plugin::StorageEngine
100
typedef std::map<string, TinaShare*> TinaMap;
101
TinaMap tina_open_tables;
103
Tina(const string& name_arg)
104
: drizzled::plugin::StorageEngine(name_arg,
105
HTON_TEMPORARY_ONLY |
106
HTON_NO_AUTO_INCREMENT |
107
HTON_SKIP_STORE_LOCK),
112
pthread_mutex_destroy(&tina_mutex);
115
virtual Cursor *create(Table &table)
117
return new ha_tina(*this, table);
120
const char **bas_ext() const {
124
int doCreateTable(Session &,
126
const drizzled::TableIdentifier &identifier,
127
drizzled::message::Table&);
129
int doGetTableDefinition(Session& session,
130
const drizzled::TableIdentifier &identifier,
131
drizzled::message::Table &table_message);
133
int doDropTable(Session&, const drizzled::TableIdentifier &identifier);
134
TinaShare *findOpenTable(const string table_name);
135
void addOpenTable(const string &table_name, TinaShare *);
136
void deleteOpenTable(const string &table_name);
139
uint32_t max_keys() const { return 0; }
140
uint32_t max_key_parts() const { return 0; }
141
uint32_t max_key_length() const { return 0; }
142
bool doDoesTableExist(Session& session, const drizzled::TableIdentifier &identifier);
143
int doRenameTable(Session&, const drizzled::TableIdentifier &from, const drizzled::TableIdentifier &to);
145
void doGetTableIdentifiers(drizzled::CachedDirectory &directory,
146
const drizzled::SchemaIdentifier &schema_identifier,
147
drizzled::TableIdentifier::vector &set_of_identifiers);
150
void Tina::doGetTableIdentifiers(drizzled::CachedDirectory&,
151
const drizzled::SchemaIdentifier&,
152
drizzled::TableIdentifier::vector&)
156
int Tina::doRenameTable(Session &session,
157
const drizzled::TableIdentifier &from, const drizzled::TableIdentifier &to)
160
for (const char **ext= bas_ext(); *ext ; ext++)
162
if (rename_file_ext(from.getPath().c_str(), to.getPath().c_str(), *ext))
164
if ((error=errno) != ENOENT)
170
session.getMessageCache().renameTableMessage(from, to);
175
bool Tina::doDoesTableExist(Session &session, const drizzled::TableIdentifier &identifier)
177
return session.getMessageCache().doesTableMessageExist(identifier);
181
int Tina::doDropTable(Session &session,
182
const drizzled::TableIdentifier &identifier)
185
int enoent_or_zero= ENOENT; // Error if no file was deleted
187
for (const char **ext= bas_ext(); *ext ; ext++)
189
std::string full_name= identifier.getPath();
190
full_name.append(*ext);
192
if (internal::my_delete_with_symlink(full_name.c_str(), MYF(0)))
194
if ((error= errno) != ENOENT)
199
enoent_or_zero= 0; // No error for ENOENT
201
error= enoent_or_zero;
204
session.getMessageCache().removeTableMessage(identifier);
209
TinaShare *Tina::findOpenTable(const string table_name)
211
TinaMap::iterator find_iter=
212
tina_open_tables.find(table_name);
214
if (find_iter != tina_open_tables.end())
215
return (*find_iter).second;
220
void Tina::addOpenTable(const string &table_name, TinaShare *share)
222
tina_open_tables[table_name]= share;
225
void Tina::deleteOpenTable(const string &table_name)
227
tina_open_tables.erase(table_name);
231
int Tina::doGetTableDefinition(Session &session,
232
const drizzled::TableIdentifier &identifier,
233
drizzled::message::Table &table_message)
235
if (session.getMessageCache().getTableMessage(identifier, table_message))
242
static Tina *tina_engine= NULL;
244
static int tina_init_func(drizzled::module::Context &context)
247
tina_engine= new Tina("CSV");
248
context.add(tina_engine);
90
int sort_set (tina_set *a, tina_set *b)
93
We assume that intervals do not intersect. So, it is enought to compare
94
any two points. Here we take start of intervals for comparison.
96
return ( a->begin > b->begin ? 1 : ( a->begin < b->begin ? -1 : 0 ) );
99
static unsigned char* tina_get_key(TINA_SHARE *share, size_t *length, bool)
101
*length=share->table_name_length;
102
return (unsigned char*) share->table_name;
105
static int tina_init_func(void *p)
107
handlerton *tina_hton;
109
tina_hton= (handlerton *)p;
250
110
pthread_mutex_init(&tina_mutex,MY_MUTEX_INIT_FAST);
256
TinaShare::TinaShare(const std::string &table_name_arg) :
257
table_name(table_name_arg),
258
data_file_name(table_name_arg),
260
saved_data_file_length(0),
261
update_file_opened(false),
262
tina_write_opened(false),
267
data_file_name.append(CSV_EXT);
270
TinaShare::~TinaShare()
272
pthread_mutex_destroy(&mutex);
111
(void) hash_init(&tina_open_tables,system_charset_info,32,0,0,
112
(hash_get_key) tina_get_key,0,0);
113
tina_hton->state= SHOW_OPTION_YES;
114
tina_hton->create= tina_create_handler;
115
tina_hton->flags= (HTON_CAN_RECREATE | HTON_SUPPORT_LOG_TABLES |
120
static int tina_done_func(void *)
122
hash_free(&tina_open_tables);
123
pthread_mutex_destroy(&tina_mutex);
276
130
Simple lock controls.
278
TinaShare *ha_tina::get_share(const std::string &table_name)
132
static TINA_SHARE *get_share(const char *table_name, Table *)
280
pthread_mutex_lock(&tina_mutex);
282
Tina *a_tina= static_cast<Tina *>(getEngine());
283
share= a_tina->findOpenTable(table_name);
285
std::string meta_file_name;
135
char meta_file_name[FN_REFLEN];
286
136
struct stat file_stat;
140
pthread_mutex_lock(&tina_mutex);
141
length=(uint) strlen(table_name);
289
144
If share is not present in the hash, create a new share and
290
145
initialize its members.
147
if (!(share=(TINA_SHARE*) hash_search(&tina_open_tables,
148
(unsigned char*) table_name,
294
share= new TinaShare(table_name);
298
pthread_mutex_unlock(&tina_mutex);
302
meta_file_name.assign(table_name);
303
meta_file_name.append(CSM_EXT);
305
if (stat(share->data_file_name.c_str(), &file_stat))
307
pthread_mutex_unlock(&tina_mutex);
151
if (!my_multi_malloc(MYF(MY_WME | MY_ZEROFILL),
152
&share, sizeof(*share),
156
pthread_mutex_unlock(&tina_mutex);
161
share->table_name_length= length;
162
share->table_name= tmp_name;
163
share->crashed= false;
164
share->rows_recorded= 0;
165
share->update_file_opened= false;
166
share->tina_write_opened= false;
167
share->data_file_version= 0;
168
strcpy(share->table_name, table_name);
169
fn_format(share->data_file_name, table_name, "", CSV_EXT,
170
MY_REPLACE_EXT|MY_UNPACK_FILENAME);
171
fn_format(meta_file_name, table_name, "", CSM_EXT,
172
MY_REPLACE_EXT|MY_UNPACK_FILENAME);
174
if (stat(share->data_file_name, &file_stat))
312
176
share->saved_data_file_length= file_stat.st_size;
314
a_tina->addOpenTable(share->table_name, share);
178
if (my_hash_insert(&tina_open_tables, (unsigned char*) share))
180
thr_lock_init(&share->lock);
316
181
pthread_mutex_init(&share->mutex,MY_MUTEX_INIT_FAST);
696
If frm_error() is called in table.cc this is called to find out what file
697
extensions exist for this handler.
699
static const char *ha_tina_exts[] = {
705
const char **ha_tina::bas_ext() const
711
Three functions below are needed to enable concurrent insert functionality
712
for CSV engine. For more details see mysys/thr_lock.c
715
void tina_get_status(void* param, int)
717
ha_tina *tina= (ha_tina*) param;
721
void tina_update_status(void* param)
723
ha_tina *tina= (ha_tina*) param;
724
tina->update_status();
727
/* this should exist and return 0 for concurrent insert to work */
728
bool tina_check_status(void *)
734
Save the state of the table
740
This function is used to retrieve the file length. During the lock
741
phase of concurrent insert. For more details see comment to
742
ha_tina::update_status below.
745
void ha_tina::get_status()
747
local_saved_data_file_length= share->saved_data_file_length;
752
Correct the state of the table. Called by unlock routines
753
before the write lock is released.
759
When we employ concurrent insert lock, we save current length of the file
760
during the lock phase. We do not read further saved value, as we don't
761
want to interfere with undergoing concurrent insert. Writers update file
762
length info during unlock with update_status().
765
For log tables concurrent insert works different. The reason is that
766
log tables are always opened and locked. And as they do not unlock
767
tables, the file length after writes should be updated in a different
771
void ha_tina::update_status()
773
/* correct local_saved_data_file_length for writers */
774
share->saved_data_file_length= local_saved_data_file_length;
781
779
Open a database file. Keep in mind that tables are caches, so
782
780
this will not be called for every request. Any sort of positions
783
781
that need to be reset should be kept in the ::extra() call.
785
int ha_tina::doOpen(const TableIdentifier &identifier, int , uint32_t )
783
int ha_tina::open(const char *name, int, uint32_t open_options)
787
if (not (share= get_share(identifier.getPath().c_str())))
785
if (!(share= get_share(name, table)))
786
return(HA_ERR_OUT_OF_MEM);
788
if (share->crashed && !(open_options & HA_OPEN_FOR_REPAIR))
793
791
return(HA_ERR_CRASHED_ON_USAGE);
796
794
local_data_file_version= share->data_file_version;
797
if ((data_file= internal::my_open(share->data_file_name.c_str(), O_RDONLY, MYF(0))) == -1)
795
if ((data_file= my_open(share->data_file_name, O_RDONLY, MYF(0))) == -1)
801
Init locking. Pass Cursor object to the locking routines,
799
Init locking. Pass handler object to the locking routines,
802
800
so that they could save/update local_saved_data_file_length value
803
801
during locking. This is needed to enable concurrent inserts.
803
thr_lock_data_init(&share->lock, &lock, (void*) this);
805
804
ref_length=sizeof(off_t);
806
share->lock.get_status= tina_get_status;
807
share->lock.update_status= tina_update_status;
808
share->lock.check_status= tina_check_status;
811
815
Close a database file. We remove ourselves from the shared strucutre.
812
816
If it is empty we destroy it.
1232
internal::my_close(update_temp_file, MYF(0));
1247
my_close(update_temp_file, MYF(0));
1233
1248
share->update_file_opened= false;
1254
Repair CSV table in the case, it is crashed.
1258
session The thread, performing repair
1259
check_opt The options for repair. We do not use it currently.
1262
If the file is empty, change # of rows in the file and complete recovery.
1263
Otherwise, scan the table looking for bad rows. If none were found,
1264
we mark file as a good one and return. If a bad row was encountered,
1265
we truncate the datafile up to the last good row.
1267
TODO: Make repair more clever - it should try to recover subsequent
1268
rows (after the first bad one) as well.
1271
int ha_tina::repair(Session* session, HA_CHECK_OPT *)
1273
char repaired_fname[FN_REFLEN];
1277
ha_rows rows_repaired= 0;
1278
off_t write_begin= 0, write_end;
1281
if (!share->saved_data_file_length)
1283
share->rows_recorded= 0;
1287
/* Don't assert in field::val() functions */
1288
table->use_all_columns();
1289
if (!(buf= (unsigned char*) malloc(table->s->reclength)))
1290
return(HA_ERR_OUT_OF_MEM);
1292
/* position buffer to the start of the file */
1293
if (init_data_file())
1294
return(HA_ERR_CRASHED_ON_REPAIR);
1297
Local_saved_data_file_length is initialized during the lock phase.
1298
Sometimes this is not getting executed before ::repair (e.g. for
1299
the log tables). We set it manually here.
1301
local_saved_data_file_length= share->saved_data_file_length;
1302
/* set current position to the beginning of the file */
1303
current_position= next_position= 0;
1305
init_alloc_root(&blobroot, BLOB_MEMROOT_ALLOC_SIZE, 0);
1307
/* Read the file row-by-row. If everything is ok, repair is not needed. */
1308
while (!(rc= find_current_row(buf)))
1310
session_inc_row_count(session);
1312
current_position= next_position;
1315
free_root(&blobroot, MYF(0));
1319
if (rc == HA_ERR_END_OF_FILE)
1322
All rows were read ok until end of file, the file does not need repair.
1323
If rows_recorded != rows_repaired, we should update rows_recorded value
1324
to the current amount of rows.
1326
share->rows_recorded= rows_repaired;
1331
Otherwise we've encountered a bad row => repair is needed.
1332
Let us create a temporary file.
1334
if ((repair_file= my_create(fn_format(repaired_fname, share->table_name,
1336
MY_REPLACE_EXT|MY_UNPACK_FILENAME),
1337
0, O_RDWR | O_TRUNC,MYF(MY_WME))) < 0)
1338
return(HA_ERR_CRASHED_ON_REPAIR);
1340
file_buff->init_buff(data_file);
1343
/* we just truncated the file up to the first bad row. update rows count. */
1344
share->rows_recorded= rows_repaired;
1346
/* write repaired file */
1349
write_end= std::min(file_buff->end(), current_position);
1350
if ((write_end - write_begin) &&
1351
(my_write(repair_file, (unsigned char*)file_buff->ptr(),
1352
write_end - write_begin, MYF_RW)))
1355
write_begin= write_end;
1356
if (write_end== current_position)
1359
file_buff->read_next(); /* shift the buffer */
1363
Close the files and rename repaired file to the datafile.
1364
We have to close the files, as on Windows one cannot rename
1365
a file, which descriptor is still open. EACCES will be returned
1366
when trying to delete the "to"-file in my_rename().
1368
if (my_close(data_file,MYF(0)) || my_close(repair_file, MYF(0)) ||
1369
my_rename(repaired_fname, share->data_file_name, MYF(0)))
1372
/* Open the file again, it should now be repaired */
1373
if ((data_file= my_open(share->data_file_name, O_RDWR|O_APPEND,
1377
/* Set new file size. The file size will be updated by ::update_status() */
1378
local_saved_data_file_length= (size_t) current_position;
1381
share->crashed= false;
1382
return(HA_ADMIN_OK);
1239
1386
DELETE without WHERE calls this
1297
if ((create_file= internal::my_create(internal::fn_format(name_buff, identifier.getPath().c_str(), "", CSM_EXT,
1298
MY_REPLACE_EXT|MY_UNPACK_FILENAME), 0,
1299
O_RDWR | O_TRUNC,MYF(MY_WME))) < 0)
1449
if ((create_file= my_create(fn_format(name_buff, name, "", CSM_EXT,
1450
MY_REPLACE_EXT|MY_UNPACK_FILENAME), 0,
1451
O_RDWR | O_TRUNC,MYF(MY_WME))) < 0)
1302
1454
write_meta_file(create_file, 0, false);
1303
internal::my_close(create_file, MYF(0));
1455
my_close(create_file, MYF(0));
1305
if ((create_file= internal::my_create(internal::fn_format(name_buff, identifier.getPath().c_str(), "", CSV_EXT,
1306
MY_REPLACE_EXT|MY_UNPACK_FILENAME),0,
1307
O_RDWR | O_TRUNC,MYF(MY_WME))) < 0)
1457
if ((create_file= my_create(fn_format(name_buff, name, "", CSV_EXT,
1458
MY_REPLACE_EXT|MY_UNPACK_FILENAME),0,
1459
O_RDWR | O_TRUNC,MYF(MY_WME))) < 0)
1310
internal::my_close(create_file, MYF(0));
1312
session.getMessageCache().storeTableMessage(identifier, create_proto);
1318
DRIZZLE_DECLARE_PLUGIN
1462
my_close(create_file, MYF(0));
1467
int ha_tina::check(Session* session, HA_CHECK_OPT *)
1471
const char *old_proc_info;
1472
ha_rows count= share->rows_recorded;
1474
old_proc_info= get_session_proc_info(session);
1475
set_session_proc_info(session, "Checking table");
1476
if (!(buf= (unsigned char*) malloc(table->s->reclength)))
1477
return(HA_ERR_OUT_OF_MEM);
1479
/* position buffer to the start of the file */
1480
if (init_data_file())
1481
return(HA_ERR_CRASHED);
1484
Local_saved_data_file_length is initialized during the lock phase.
1485
Check does not use store_lock in certain cases. So, we set it
1488
local_saved_data_file_length= share->saved_data_file_length;
1489
/* set current position to the beginning of the file */
1490
current_position= next_position= 0;
1492
init_alloc_root(&blobroot, BLOB_MEMROOT_ALLOC_SIZE, 0);
1494
/* Read the file row-by-row. If everything is ok, repair is not needed. */
1495
while (!(rc= find_current_row(buf)))
1497
session_inc_row_count(session);
1499
current_position= next_position;
1502
free_root(&blobroot, MYF(0));
1505
set_session_proc_info(session, old_proc_info);
1507
if ((rc != HA_ERR_END_OF_FILE) || count)
1509
share->crashed= true;
1510
return(HA_ADMIN_CORRUPT);
1513
return(HA_ADMIN_OK);
1517
bool ha_tina::check_if_incompatible_data(HA_CREATE_INFO *, uint32_t)
1519
return COMPATIBLE_DATA_YES;
1522
mysql_declare_plugin(csv)
1524
DRIZZLE_STORAGE_ENGINE_PLUGIN,
1323
1527
"Brian Aker, MySQL AB",
1324
1528
"CSV storage engine",
1325
1529
PLUGIN_LICENSE_GPL,
1326
1530
tina_init_func, /* Plugin Init */
1531
tina_done_func, /* Plugin Deinit */
1532
NULL, /* status variables */
1327
1533
NULL, /* system variables */
1328
1534
NULL /* config options */
1330
DRIZZLE_DECLARE_PLUGIN_END;
1536
mysql_declare_plugin_end;