41
41
#include <drizzled/sql_base.h>
42
42
#include <drizzled/show.h>
43
43
#include <drizzled/item/cmpfunc.h>
44
#include <drizzled/replication_services.h>
44
#include <drizzled/transaction_services.h>
45
45
#include <drizzled/check_stack_overrun.h>
46
46
#include <drizzled/lock.h>
47
47
#include <drizzled/listen.h>
94
94
void table_cache_free(void)
96
refresh_version++; // Force close of open tables
99
hash_delete(&open_cache,(unsigned char*) unused_tables);
96
close_cached_tables(NULL, NULL, false, false);
101
97
if (!open_cache.records) // Safety first
102
98
hash_free(&open_cache);
238
234
****************************************************************************/
241
void Table::intern_close_table()
237
void intern_close_table(Table *table)
242
238
{ // Free all structures
244
if (file) // Not true if name lock
245
closefrm(true); // close file
239
free_io_cache(table);
240
if (table->file) // Not true if name lock
241
table->closefrm(true); // close file
259
255
void free_cache_entry(void *entry)
261
257
Table *table= static_cast<Table *>(entry);
262
table->intern_close_table();
258
intern_close_table(table);
263
259
if (!table->in_use)
265
261
table->next->prev=table->prev; /* remove from used chain */
271
267
unused_tables= NULL;
277
274
/* Free resources allocated by filesort() and read_record() */
279
void Table::free_io_cache()
276
void free_io_cache(Table *table)
278
if (table->sort.io_cache)
283
close_cached_file(sort.io_cache);
284
delete sort.io_cache;
280
close_cached_file(table->sort.io_cache);
281
delete table->sort.io_cache;
282
table->sort.io_cache= 0;
302
299
and tables must be NULL.
305
bool Session::close_cached_tables(TableList *tables, bool wait_for_refresh, bool wait_for_placeholders)
302
bool close_cached_tables(Session *session, TableList *tables,
303
bool wait_for_refresh, bool wait_for_placeholders)
307
305
bool result= false;
308
Session *session= this;
306
assert(session || (!wait_for_refresh && !tables));
310
308
pthread_mutex_lock(&LOCK_open); /* Optionally lock for remove tables from open_cahe if not in use */
314
312
refresh_version++; // Force close of open tables
315
313
while (unused_tables)
316
if (hash_delete(&open_cache,(unsigned char*) unused_tables))
317
printf("Warning: Couldn't delete open table from hash\n");
316
319
hash_delete(&open_cache,(unsigned char*) unused_tables);
318
322
if (wait_for_refresh)
325
329
request is aborted. They loop in open_and_lock_tables() and
326
330
enter open_table(). Here they notice the table is refreshed and
327
331
wait for COND_refresh. Then they loop again in
328
openTablesLock() and this time open_table() succeeds. At
332
open_and_lock_tables() and this time open_table() succeeds. At
329
333
this moment, if we (the FLUSH TABLES thread) are scheduled and
330
334
on another FLUSH TABLES enter close_cached_tables(), they could
331
335
awake while we sleep below, waiting for others threads (us) to
340
344
The fix for this problem is to set some_tables_deleted for all
341
345
threads with open tables. These threads can still get their
342
346
locks, but will immediately release them again after checking
343
this variable. They will then loop in openTablesLock()
347
this variable. They will then loop in open_and_lock_tables()
344
348
again. There they will wait until we update all tables version
429
434
result= session->reopen_tables(true, true);
431
436
/* Set version for table */
432
for (Table *table= session->open_tables; table ; table= table->next)
437
for (Table *table=session->open_tables; table ; table= table->next)
435
440
Preserve the version (0) of write locked tables so that a impending
733
unlink from session->temporary tables and close temporary table
736
void Session::close_temporary_table(Table *table,
737
bool free_share, bool delete_table)
741
table->prev->next= table->next;
742
if (table->prev->next)
743
table->next->prev= table->prev;
747
/* removing the item from the list */
748
assert(table == temporary_tables);
750
slave must reset its temporary list pointer to zero to exclude
751
passing non-zero value to end_slave via rli->save_temporary_tables
752
when no temp tables opened, see an invariant below.
754
temporary_tables= table->next;
755
if (temporary_tables)
756
table->next->prev= NULL;
758
close_temporary(table, free_share, delete_table);
763
Close and delete a temporary table
766
This dosn't unlink table from session->temporary
767
If this is needed, use close_temporary_table()
770
void close_temporary(Table *table, bool free_share, bool delete_table)
772
StorageEngine *table_type= table->s->db_type();
774
free_io_cache(table);
775
table->closefrm(false);
778
rm_temporary_table(table_type, table->s->path.str);
782
table->s->free_table_share();
789
Used by ALTER Table when the table is a temporary one. It changes something
790
only if the ALTER contained a RENAME clause (otherwise, table_name is the old
792
Prepares a table cache key, which is the concatenation of db, table_name and
793
session->slave_proxy_id, separated by '\0'.
796
bool rename_temporary_table(Table *table, const char *db, const char *table_name)
800
TableShare *share= table->s;
802
if (!(key=(char*) alloc_root(&share->mem_root, MAX_DBKEY_LENGTH)))
803
return true; /* purecov: inspected */
805
key_length= TableShare::createKey(key, db, table_name);
806
share->set_table_cache_key(key, key_length);
725
812
/* move table first in unused links */
942
1029
table->s->table_cache_key.str,
943
1030
table->s->table_cache_key.length))
945
table->intern_close_table();
1032
intern_close_table(table);
947
1034
If there was an error during opening of table (for example if it
948
1035
does not exist) '*table' object can be wiped out. To be able
1130
Table *Session::openTable(TableList *table_list, bool *refresh, uint32_t flags)
1217
Table *Session::open_table(TableList *table_list, bool *refresh, uint32_t flags)
1132
1219
register Table *table;
1133
1220
char key[MAX_DBKEY_LENGTH];
1349
1436
if (table_list->create)
1351
char path[FN_REFLEN];
1354
length= build_table_filename(path, sizeof(path),
1355
table_list->db, table_list->table_name,
1358
if (StorageEngine::getTableProto(path, NULL) != EEXIST)
1438
if (ha_table_exists_in_engine(this, table_list->db,
1439
table_list->table_name)
1440
!= HA_ERR_TABLE_EXIST)
1361
1443
Table to be created, so we need to create placeholder in table-cache.
1614
1696
situations like FLUSH TABLES or ALTER Table. In general
1615
1697
case one should just repeat open_tables()/lock_tables()
1616
1698
combination when one needs tables to be reopened (for
1617
example see openTablesLock()).
1699
example see open_and_lock_tables()).
1619
1701
@note One should have lock on LOCK_open when calling this.
2111
2193
end+= sprintf(query, "DELETE FROM `%s`.`%s`", share->db.str,
2112
2194
share->table_name.str);
2113
replication_services.rawStatement(session, query, (size_t)(end - query));
2195
transaction_services.rawStatement(session, query, (size_t)(end - query));
2211
2293
not opened yet. Try to open the table.
2213
2295
if (tables->table == NULL)
2214
tables->table= openTable(tables, &refresh, flags);
2296
tables->table= open_table(tables, &refresh, flags);
2216
2298
if (tables->table == NULL)
2289
2371
table_list->table table
2292
Table *Session::openTableLock(TableList *table_list, thr_lock_type lock_type)
2374
Table *Session::open_ltable(TableList *table_list, thr_lock_type lock_type)
2297
2379
set_proc_info("Opening table");
2298
2380
current_tablenr= 0;
2299
while (!(table= openTable(table_list, &refresh)) &&
2381
while (!(table= open_table(table_list, &refresh, 0)) &&
2347
int Session::lock_tables(TableList *tables, uint32_t count, bool *need_reopen)
2429
int lock_tables(Session *session, TableList *tables, uint32_t count, bool *need_reopen)
2349
2431
TableList *table;
2350
Session *session= this;
2353
2434
We can't meet statement requiring prelocking if we already
2403
Table *Session::open_temporary_table(const char *path, const char *db_arg,
2404
const char *table_name_arg, bool link_in_list,
2405
open_table_mode open_mode)
2484
Table *open_temporary_table(Session *session, const char *path, const char *db,
2485
const char *table_name, bool link_in_list,
2486
open_table_mode open_mode)
2407
Table *new_tmp_table;
2408
2489
TableShare *share;
2409
2490
char cache_key[MAX_DBKEY_LENGTH], *saved_cache_key, *tmp_path;
2410
2491
uint32_t key_length, path_length;
2411
2492
TableList table_list;
2413
table_list.db= (char*) db_arg;
2414
table_list.table_name= (char*) table_name_arg;
2494
table_list.db= (char*) db;
2495
table_list.table_name= (char*) table_name;
2415
2496
/* Create the cache_key for temporary tables */
2416
2497
key_length= table_list.create_table_def_key(cache_key);
2417
2498
path_length= strlen(path);
2419
if (!(new_tmp_table= (Table*) malloc(sizeof(*new_tmp_table) + sizeof(*share) +
2500
if (!(tmp_table= (Table*) malloc(sizeof(*tmp_table) + sizeof(*share) +
2420
2501
path_length + 1 + key_length)))
2423
share= (TableShare*) (new_tmp_table+1);
2504
share= (TableShare*) (tmp_table+1);
2424
2505
tmp_path= (char*) (share+1);
2425
2506
saved_cache_key= strcpy(tmp_path, path)+path_length+1;
2426
2507
memcpy(saved_cache_key, cache_key, key_length);
2431
2512
First open the share, and then open the table from the share we just opened.
2433
if (open_table_def(this, share) ||
2434
open_table_from_share(this, share, table_name_arg,
2514
if (open_table_def(session, share) ||
2515
open_table_from_share(session, share, table_name,
2435
2516
(open_mode == OTM_ALTER) ? 0 :
2436
2517
(uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
2439
2520
(EXTRA_RECORD | OPEN_FRM_FILE_ONLY)
2440
2521
: (EXTRA_RECORD),
2441
2522
ha_open_options,
2442
new_tmp_table, open_mode))
2523
tmp_table, open_mode))
2444
2525
/* No need to lock share->mutex as this is not needed for tmp tables */
2445
2526
share->free_table_share();
2446
free((char*) new_tmp_table);
2527
free((char*) tmp_table);
2450
new_tmp_table->reginfo.lock_type= TL_WRITE; // Simulate locked
2531
tmp_table->reginfo.lock_type= TL_WRITE; // Simulate locked
2451
2532
if (open_mode == OTM_ALTER)
2457
2538
share->tmp_table= TMP_TABLE_FRM_FILE_ONLY;
2460
share->tmp_table= (new_tmp_table->file->has_transactions() ?
2541
share->tmp_table= (tmp_table->file->has_transactions() ?
2461
2542
TRANSACTIONAL_TMP_TABLE : NON_TRANSACTIONAL_TMP_TABLE);
2463
2544
if (link_in_list)
2465
2546
/* growing temp list at the head */
2466
new_tmp_table->next= this->temporary_tables;
2467
if (new_tmp_table->next)
2468
new_tmp_table->next->prev= new_tmp_table;
2469
this->temporary_tables= new_tmp_table;
2470
this->temporary_tables->prev= 0;
2472
new_tmp_table->pos_in_table_list= 0;
2474
return new_tmp_table;
2547
tmp_table->next= session->temporary_tables;
2548
if (tmp_table->next)
2549
tmp_table->next->prev= tmp_table;
2550
session->temporary_tables= tmp_table;
2551
session->temporary_tables->prev= 0;
2553
tmp_table->pos_in_table_list= 0;
2559
bool rm_temporary_table(StorageEngine *base, char *path)
2565
if(delete_table_proto_file(path))
2566
error=1; /* purecov: inspected */
2568
if (base->deleteTable(current_session, path))
2571
errmsg_printf(ERRMSG_LVL_WARN, _("Could not remove temporary table: '%s', error: %d"),
4399
4499
false if all is OK
4402
int Session::setup_conds(TableList *leaves, COND **conds)
4502
int setup_conds(Session *session, TableList *leaves, COND **conds)
4404
Session *session= this;
4405
4504
Select_Lex *select_lex= session->lex->current_select;
4406
4505
TableList *table= NULL; // For HP compilers
4407
4506
void *save_session_marker= session->session_marker;