61
63
extern bool volatile shutdown_in_progress;
63
bool drizzle_rm_tmp_tables();
66
@defgroup Data_Dictionary Data Dictionary
69
Table *unused_tables; /* Used by mysql_test */
70
HASH open_cache; /* Used by mysql_test */
71
static int open_unireg_entry(Session *session, Table *entry, TableList *table_list,
65
TableOpenCache &get_open_cache()
67
static TableOpenCache open_cache; /* Used by mysql_test */
72
static void free_cache_entry(Table *entry);
74
void remove_table(Table *arg)
76
TableOpenCacheRange ppp;
77
ppp= get_open_cache().equal_range(arg->getShare()->getCacheKey());
79
for (TableOpenCache::const_iterator iter= ppp.first;
80
iter != ppp.second; ++iter)
82
Table *found_table= (*iter).second;
84
if (found_table == arg)
86
free_cache_entry(arg);
87
get_open_cache().erase(iter);
93
static bool add_table(Table *arg)
95
TableOpenCache &open_cache(get_open_cache());
97
TableOpenCache::iterator returnable= open_cache.insert(make_pair(arg->getShare()->getCacheKey(), arg));
99
return not (returnable == open_cache.end());
103
Table *tables; /* Used by mysql_test */
105
Table *getTable() const
110
Table *setTable(Table *arg)
119
/* Free cache if too big */
120
while (cached_open_tables() > table_cache_size && getTable())
121
remove_table(getTable());
126
while (getTable() && not getTable()->getShare()->getVersion())
127
remove_table(getTable());
130
void link(Table *table)
134
table->setNext(getTable()); /* Link in last */
135
table->setPrev(getTable()->getPrev());
136
getTable()->setPrev(table);
137
table->getPrev()->setNext(table);
141
table->setPrev(setTable(table));
142
table->setNext(table->getPrev());
143
assert(table->getNext() == table && table->getPrev() == table);
148
void unlink(Table *table)
152
/* Unlink the table from "unused_tables" list. */
153
if (table == getTable())
155
setTable(getTable()->getNext()); // Remove from link
156
if (table == getTable())
161
/* move table first in unused links */
163
void relink(Table *table)
165
if (table != getTable())
169
table->setNext(getTable()); /* Link in unused tables */
170
table->setPrev(getTable()->getPrev());
171
getTable()->getPrev()->setNext(table);
172
getTable()->setPrev(table);
181
remove_table(getTable());
193
static UnusedTables unused_tables;
194
static int open_unireg_entry(Session *session,
72
196
const char *alias,
73
char *cache_key, uint32_t cache_key_length);
74
void free_cache_entry(void *entry);
197
TableIdentifier &identifier);
75
199
unsigned char *table_cache_key(const unsigned char *record,
204
static bool reopen_table(Table *table);
80
207
unsigned char *table_cache_key(const unsigned char *record,
84
211
Table *entry=(Table*) record;
85
*length= entry->s->table_cache_key.length;
86
return (unsigned char*) entry->s->table_cache_key.str;
89
HASH *get_open_cache()
212
*length= entry->getShare()->getCacheKey().size();
213
return (unsigned char*) &entry->getShare()->getCacheKey()[0];
95
216
bool table_cache_init(void)
97
return hash_init(&open_cache, &my_charset_bin,
98
(size_t) table_cache_size+16,
99
0, 0, table_cache_key,
100
free_cache_entry, 0);
221
uint32_t cached_open_tables(void)
223
return get_open_cache().size();
103
226
void table_cache_free(void)
105
228
refresh_version++; // Force close of open tables
107
while (unused_tables)
108
hash_delete(&open_cache, (unsigned char*) unused_tables);
110
if (not open_cache.records) // Safety first
111
hash_free(&open_cache);
114
uint32_t cached_open_tables(void)
116
return open_cache.records;
230
unused_tables.clear();
231
get_open_cache().clear();
121
235
Close cursor handle, but leave the table in the table cache
138
252
void close_handle_and_leave_table_as_lock(Table *table)
140
TableShare *share, *old_share= table->s;
142
memory::Root *mem_root= &table->mem_root;
254
TableShare *share, *old_share= table->getMutableShare();
144
255
assert(table->db_stat);
256
assert(table->getShare()->getType() == message::Table::STANDARD);
147
259
Make a local copy of the table share and free the current one.
148
260
This has to be done to ensure that the table share is removed from
149
261
the table defintion cache as soon as the last instance is removed
151
if (multi_alloc_root(mem_root,
152
&share, sizeof(*share),
153
&key_buff, old_share->table_cache_key.length,
156
memset(share, 0, sizeof(*share));
157
share->set_table_cache_key(key_buff, old_share->table_cache_key.str,
158
old_share->table_cache_key.length);
159
share->tmp_table= message::Table::INTERNAL; // for intern_close_table()
263
TableIdentifier identifier(table->getShare()->getSchemaName(), table->getShare()->getTableName(), message::Table::INTERNAL);
264
const TableIdentifier::Key &key(identifier.getKey());
265
share= new TableShare(identifier.getType(),
267
const_cast<char *>(&key[0]), static_cast<uint32_t>(old_share->getCacheKeySize()));
162
269
table->cursor->close();
163
270
table->db_stat= 0; // Mark cursor closed
164
TableShare::release(table->s);
166
table->cursor->change_table_ptr(table, table->s);
271
TableShare::release(table->getMutableShare());
272
table->setShare(share);
273
table->cursor->change_table_ptr(table, table->getMutableShare());
190
299
We need to have a lock on LOCK_open when calling this
193
void free_cache_entry(void *entry)
302
void free_cache_entry(Table *table)
195
Table *table= static_cast<Table *>(entry);
196
304
table->intern_close_table();
197
305
if (not table->in_use)
199
table->next->prev=table->prev; /* remove from used chain */
200
table->prev->next=table->next;
201
if (table == unused_tables)
203
unused_tables=unused_tables->next;
204
if (table == unused_tables)
307
unused_tables.unlink(table);
211
313
/* Free resources allocated by filesort() and read_record() */
337
446
not for placeholders with Table::open_placeholder set. Waiting for
338
447
latter will cause deadlock in the following scenario, for example:
340
conn1: lock table t1 write;
341
conn2: lock table t2 write;
449
conn1-> lock table t1 write;
450
conn2-> lock table t2 write;
451
conn1-> flush tables;
452
conn2-> flush tables;
345
It also does not make sense to wait for those of placeholders that
346
are employed by CREATE TABLE as in this case table simply does not
454
It also does not make sense to wait for those of placeholders that
455
are employed by CREATE TABLE as in this case table simply does not
349
458
if (table->needs_reopen_or_name_lock() && (table->db_stat ||
350
459
(table->open_placeholder && wait_for_placeholders)))
353
pthread_cond_wait(&COND_refresh,&LOCK_open);
462
pthread_cond_wait(COND_refresh.native_handle(),LOCK_open.native_handle());
589
void Session::doGetTableNames(SchemaIdentifier &schema_identifier,
690
void Session::doGetTableNames(const SchemaIdentifier &schema_identifier,
590
691
std::set<std::string>& set_of_names)
592
for (Table *table= temporary_tables ; table ; table= table->next)
693
for (Table *table= temporary_tables ; table ; table= table->getNext())
594
if (schema_identifier.compare(table->s->getSchemaName()))
695
if (schema_identifier.compare(table->getShare()->getSchemaName()))
596
set_of_names.insert(table->s->table_name.str);
697
set_of_names.insert(table->getShare()->getTableName());
601
702
void Session::doGetTableNames(CachedDirectory &,
602
SchemaIdentifier &schema_identifier,
703
const SchemaIdentifier &schema_identifier,
603
704
std::set<std::string> &set_of_names)
605
706
doGetTableNames(schema_identifier, set_of_names);
608
bool Session::doDoesTableExist(TableIdentifier &identifier)
610
for (Table *table= temporary_tables ; table ; table= table->next)
612
if (table->s->tmp_table == message::Table::TEMPORARY)
614
if (identifier.compare(table->s->getSchemaName(), table->s->table_name.str))
709
void Session::doGetTableIdentifiers(const SchemaIdentifier &schema_identifier,
710
TableIdentifiers &set_of_identifiers)
712
for (Table *table= temporary_tables ; table ; table= table->getNext())
714
if (schema_identifier.compare(table->getShare()->getSchemaName()))
716
set_of_identifiers.push_back(TableIdentifier(table->getShare()->getSchemaName(),
717
table->getShare()->getTableName(),
718
table->getShare()->getPath()));
723
void Session::doGetTableIdentifiers(CachedDirectory &,
724
const SchemaIdentifier &schema_identifier,
725
TableIdentifiers &set_of_identifiers)
727
doGetTableIdentifiers(schema_identifier, set_of_identifiers);
730
bool Session::doDoesTableExist(const TableIdentifier &identifier)
732
for (Table *table= temporary_tables ; table ; table= table->getNext())
734
if (table->getShare()->getType() == message::Table::TEMPORARY)
736
if (identifier.getKey() == table->getShare()->getCacheKey())
624
int Session::doGetTableDefinition(TableIdentifier &identifier,
746
int Session::doGetTableDefinition(const TableIdentifier &identifier,
625
747
message::Table &table_proto)
627
for (Table *table= temporary_tables ; table ; table= table->next)
749
for (Table *table= temporary_tables ; table ; table= table->getNext())
629
if (table->s->tmp_table == message::Table::TEMPORARY)
751
if (table->getShare()->getType() == message::Table::TEMPORARY)
631
if (identifier.compare(table->s->getSchemaName(), table->s->table_name.str))
753
if (identifier.getKey() == table->getShare()->getCacheKey())
633
table_proto.CopyFrom(*(table->s->getTableProto()));
755
table_proto.CopyFrom(*(table->getShare()->getTableProto()));
645
767
char key[MAX_DBKEY_LENGTH];
648
key_length= TableShare::createKey(key, new_db, table_name);
770
key_length= TableIdentifier::createKey(key, new_db, table_name);
650
for (Table *table= temporary_tables ; table ; table= table->next)
772
for (Table *table= temporary_tables ; table ; table= table->getNext())
652
if (table->s->table_cache_key.length == key_length &&
653
not memcmp(table->s->table_cache_key.str, key, key_length))
774
const TableIdentifier::Key &share_key(table->getShare()->getCacheKey());
775
if (share_key.size() == key_length &&
776
not memcmp(&share_key[0], key, key_length))
756
855
void Session::unlink_open_table(Table *find)
758
857
char key[MAX_DBKEY_LENGTH];
759
uint32_t key_length= find->s->table_cache_key.length;
858
uint32_t key_length= find->getShare()->getCacheKeySize();
760
859
Table *list, **prev;
762
safe_mutex_assert_owner(&LOCK_open);
764
memcpy(key, find->s->table_cache_key.str, key_length);
860
safe_mutex_assert_owner(LOCK_open.native_handle());
862
memcpy(key, &find->getMutableShare()->getCacheKey()[0], key_length);
766
864
Note that we need to hold LOCK_open while changing the
767
865
open_tables list. Another thread may work on it.
843
941
cond Condition to wait for
846
void Session::wait_for_condition(pthread_mutex_t *mutex, pthread_cond_t *cond)
944
void Session::wait_for_condition(boost::mutex &mutex, boost::condition_variable &cond)
848
946
/* Wait until the current table is up to date */
849
947
const char *saved_proc_info;
850
mysys_var->current_mutex= mutex;
851
mysys_var->current_cond= cond;
948
mysys_var->current_mutex= mutex.native_handle();
949
mysys_var->current_cond= cond.native_handle();
852
950
saved_proc_info= get_proc_info();
853
951
set_proc_info("Waiting for table");
855
(void) pthread_cond_wait(cond, mutex);
953
(void) pthread_cond_wait(cond.native_handle(), mutex.native_handle());
858
956
We must unlock mutex first to avoid deadlock becasue conditions are
904
1002
char *table_name= table_list->table_name;
905
1003
Table orig_table;
907
safe_mutex_assert_owner(&LOCK_open);
1005
safe_mutex_assert_owner(LOCK_open.native_handle());
909
1007
if (killed || !table)
912
1010
orig_table= *table;
914
if (open_unireg_entry(this, table, table_list, table_name,
915
table->s->table_cache_key.str,
916
table->s->table_cache_key.length))
1012
TableIdentifier identifier(table_list->db, table_list->table_name);
1013
if (open_unireg_entry(this, table, table_name, identifier))
918
1015
table->intern_close_table();
977
1074
case of failure.
980
Table *Session::table_cache_insert_placeholder(const char *key, uint32_t key_length)
1077
Table *Session::table_cache_insert_placeholder(const char *db_name, const char *table_name)
986
safe_mutex_assert_owner(&LOCK_open);
1079
safe_mutex_assert_owner(LOCK_open.native_handle());
989
1082
Create a table entry with the right key and with an old refresh version
990
Note that we must use multi_malloc() here as this is freed by the
993
if (! memory::multi_malloc(true,
994
&table, sizeof(*table),
995
&share, sizeof(*share),
996
&key_buff, key_length,
1001
share->set_table_cache_key(key_buff, key, key_length);
1002
share->tmp_table= message::Table::INTERNAL; // for intern_close_table
1003
table->in_use= this;
1004
table->locked_by_name=1;
1006
if (my_hash_insert(&open_cache, (unsigned char*)table))
1084
TableIdentifier identifier(db_name, table_name, message::Table::INTERNAL);
1085
TablePlaceholder *table= new TablePlaceholder(this, identifier);
1087
if (not add_table(table))
1008
free((unsigned char*) table);
1037
1119
bool Session::lock_table_name_if_not_cached(TableIdentifier &identifier, Table **table)
1039
return lock_table_name_if_not_cached(identifier.getSchemaName().c_str(), identifier.getTableName().c_str(), table);
1042
bool Session::lock_table_name_if_not_cached(const char *new_db,
1043
const char *table_name, Table **table)
1045
char key[MAX_DBKEY_LENGTH];
1047
uint32_t key_length;
1049
key_pos= strcpy(key_pos, new_db) + strlen(new_db);
1050
key_pos= strcpy(key_pos+1, table_name) + strlen(table_name);
1051
key_length= (uint32_t) (key_pos-key)+1;
1053
pthread_mutex_lock(&LOCK_open); /* Obtain a name lock even though table is not in cache (like for create table) */
1055
if (hash_search(&open_cache, (unsigned char *)key, key_length))
1121
const TableIdentifier::Key &key(identifier.getKey());
1123
boost::mutex::scoped_lock scope_lock(LOCK_open); /* Obtain a name lock even though table is not in cache (like for create table) */
1125
TableOpenCache::iterator iter;
1127
iter= get_open_cache().find(key);
1129
if (iter != get_open_cache().end())
1057
pthread_mutex_unlock(&LOCK_open);
1061
if (not (*table= table_cache_insert_placeholder(key, key_length)))
1135
if (not (*table= table_cache_insert_placeholder(identifier.getSchemaName().c_str(), identifier.getTableName().c_str())))
1063
pthread_mutex_unlock(&LOCK_open);
1066
1139
(*table)->open_placeholder= true;
1067
(*table)->next= open_tables;
1140
(*table)->setNext(open_tables);
1068
1141
open_tables= *table;
1069
pthread_mutex_unlock(&LOCK_open);
1217
1290
an implicit "pending locks queue" - see
1218
1291
wait_for_locked_table_names for details.
1220
for (table= (Table*) hash_first(&open_cache, (unsigned char*) key, key_length,
1222
table && table->in_use ;
1223
table= (Table*) hash_next(&open_cache, (unsigned char*) key, key_length,
1293
ppp= get_open_cache().equal_range(key);
1296
for (TableOpenCache::const_iterator iter= ppp.first;
1297
iter != ppp.second; ++iter, table= NULL)
1299
table= (*iter).second;
1301
if (not table->in_use)
1227
1304
Here we flush tables marked for flush.
1228
Normally, table->s->version contains the value of
1305
Normally, table->getShare()->version contains the value of
1229
1306
refresh_version from the moment when this table was
1230
1307
(re-)opened and added to the cache.
1231
1308
If since then we did (or just started) FLUSH TABLES
1232
1309
statement, refresh_version has been increased.
1233
For "name-locked" Table instances, table->s->version is set
1310
For "name-locked" Table instances, table->getShare()->version is set
1234
1311
to 0 (see lock_table_name for details).
1235
1312
In case there is a pending FLUSH TABLES or a name lock, we
1236
1313
need to back off and re-start opening tables.
1237
1314
If we do not back off now, we may dead lock in case of lock
1238
1315
order mismatch with some other thread:
1239
c1: name lock t1; -- sort of exclusive lock
1240
c2: open t2; -- sort of shared lock
1241
c1: name lock t2; -- blocks
1242
c2: open t1; -- blocks
1316
c1-> name lock t1; -- sort of exclusive lock
1317
c2-> open t2; -- sort of shared lock
1318
c1-> name lock t2; -- blocks
1319
c2-> open t1; -- blocks
1244
1321
if (table->needs_reopen_or_name_lock())
1246
1323
if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
1248
1325
/* Force close at once after usage */
1249
version= table->s->version;
1326
version= table->getShare()->getVersion();
1253
1330
/* Avoid self-deadlocks by detecting self-dependencies. */
1254
1331
if (table->open_placeholder && table->in_use == this)
1256
pthread_mutex_unlock(&LOCK_open);
1257
my_error(ER_UPDATE_TABLE_USED, MYF(0), table->s->table_name.str);
1334
my_error(ER_UPDATE_TABLE_USED, MYF(0), table->getMutableShare()->getTableName());
1355
1423
/* make a new table */
1356
table= (Table *)malloc(sizeof(Table));
1357
1425
if (table == NULL)
1359
pthread_mutex_unlock(&LOCK_open);
1363
error= open_unireg_entry(this, table, table_list, alias, key, key_length);
1431
error= open_unireg_entry(this, table, alias, identifier);
1364
1432
if (error != 0)
1367
pthread_mutex_unlock(&LOCK_open);
1370
my_hash_insert(&open_cache, (unsigned char*) table);
1438
(void)add_table(table);
1373
pthread_mutex_unlock(&LOCK_open);
1376
table->next= open_tables; /* Link into simple list */
1444
table->setNext(open_tables); /* Link into simple list */
1379
1447
table->reginfo.lock_type= TL_READ; /* Assume read */
1382
assert(table->s->ref_count > 0 || table->s->tmp_table != message::Table::STANDARD);
1450
assert(table->getShare()->getTableCount() > 0 || table->getShare()->getType() != message::Table::STANDARD);
1384
1452
if (lex->need_correct_ident())
1385
1453
table->alias_name_used= my_strcasecmp(table_alias_charset,
1386
table->s->table_name.str, alias);
1454
table->getMutableShare()->getTableName(), alias);
1387
1455
/* Fix alias if table name changes */
1388
if (strcmp(table->alias, alias))
1456
if (strcmp(table->getAlias(), alias))
1390
1458
uint32_t length=(uint32_t) strlen(alias)+1;
1391
1459
table->alias= (char*) realloc((char*) table->alias, length);
1400
1468
table->maybe_null= false;
1401
1469
table->force_index= false;
1402
1470
table->status=STATUS_NO_RECORD;
1403
table->insert_values= 0;
1471
table->insert_values.clear();
1404
1472
/* Catch wrong handling of the auto_increment_field_not_null. */
1405
1473
assert(!table->auto_increment_field_not_null);
1406
1474
table->auto_increment_field_not_null= false;
1407
1475
if (table->timestamp_field)
1408
1477
table->timestamp_field_type= table->timestamp_field->get_auto_set_type();
1409
1479
table->pos_in_table_list= table_list;
1410
1480
table->clear_column_bitmaps();
1411
1481
assert(table->key_read == 0);
1600
1664
uint32_t opens= 0;
1602
for (table= open_tables; table ; table=table->next)
1666
for (table= open_tables; table ; table=table->getNext())
1604
1670
tables= new Table *[opens];
1607
1674
tables= &open_tables;
1608
1676
tables_ptr =tables;
1610
1678
prev= &open_tables;
1611
1679
for (table= open_tables; table ; table=next)
1613
uint32_t db_stat= table->db_stat;
1615
if (!tables || (!db_stat && reopen_table(table)))
1617
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
1618
hash_delete(&open_cache,(unsigned char*) table);
1625
/* Do not handle locks of MERGE children. */
1626
if (get_locks && !db_stat)
1627
*tables_ptr++= table; // need new lock on this
1628
if (mark_share_as_old)
1630
table->s->version= 0;
1631
table->open_placeholder= false;
1681
next= table->getNext();
1683
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->getAlias());
1684
remove_table(table);
1636
1688
if (tables != tables_ptr) // Should we get back old locks
1638
DRIZZLE_LOCK *local_lock;
1690
DrizzleLock *local_lock;
1640
1692
We should always get these locks. Anyway, we must not go into
1641
1693
wait_for_tables() as it tries to acquire LOCK_open, which is
1768
char *key= table->s->table_cache_key.str;
1769
uint32_t key_length= table->s->table_cache_key.length;
1771
HASH_SEARCH_STATE state;
1772
for (Table *search= (Table*) hash_first(&open_cache, (unsigned char*) key,
1773
key_length, &state);
1775
search= (Table*) hash_next(&open_cache, (unsigned char*) key,
1776
key_length, &state))
1820
const TableIdentifier::Key &key(table->getShare()->getCacheKey());
1822
TableOpenCacheRange ppp;
1823
ppp= get_open_cache().equal_range(key);
1825
for (TableOpenCache::const_iterator iter= ppp.first;
1826
iter != ppp.second; ++iter)
1828
Table *search= (*iter).second;
1778
1829
if (search->in_use == table->in_use)
1779
1830
continue; // Name locked by this thread
1802
1853
session->set_proc_info("Waiting for tables");
1803
pthread_mutex_lock(&LOCK_open); /* Lock for all tables to be refreshed */
1804
while (!session->killed)
1806
session->some_tables_deleted= false;
1807
session->close_old_data_files(false, dropping_tables != 0);
1808
if (!table_is_used(session->open_tables, 1))
1810
(void) pthread_cond_wait(&COND_refresh,&LOCK_open);
1812
if (session->killed)
1813
result= true; // aborted
1816
/* Now we can open all tables without any interference */
1817
session->set_proc_info("Reopen tables");
1818
session->version= refresh_version;
1819
result= session->reopen_tables(false, false);
1821
pthread_mutex_unlock(&LOCK_open);
1855
boost::mutex::scoped_lock lock(LOCK_open);
1856
while (!session->killed)
1858
session->some_tables_deleted= false;
1859
session->close_old_data_files(false, dropping_tables != 0);
1860
if (!table_is_used(session->open_tables, 1))
1862
COND_refresh.wait(lock);
1864
if (session->killed)
1865
result= true; // aborted
1868
/* Now we can open all tables without any interference */
1869
session->set_proc_info("Reopen tables");
1870
session->version= refresh_version;
1871
result= session->reopen_tables(false, false);
1822
1874
session->set_proc_info(0);
1905
1956
other threads trying to get the lock.
1908
void abort_locked_tables(Session *session,const char *db, const char *table_name)
1959
void abort_locked_tables(Session *session, const drizzled::TableIdentifier &identifier)
1911
for (table= session->open_tables; table ; table= table->next)
1962
for (table= session->open_tables; table ; table= table->getNext())
1913
if (!strcmp(table->s->table_name.str, table_name) &&
1914
!strcmp(table->s->getSchemaName(), db))
1964
if (table->getShare()->getCacheKey() == identifier.getKey())
1916
1966
/* If MERGE child, forward lock handling to parent. */
1917
1967
mysql_lock_abort(session, table);
1944
static int open_unireg_entry(Session *session, Table *entry, TableList *table_list,
1994
static int open_unireg_entry(Session *session,
1945
1996
const char *alias,
1946
char *cache_key, uint32_t cache_key_length)
1997
TableIdentifier &identifier)
1949
2000
TableShare *share;
1950
2001
uint32_t discover_retry_count= 0;
1952
safe_mutex_assert_owner(&LOCK_open);
2003
safe_mutex_assert_owner(LOCK_open.native_handle());
1954
if (not (share= TableShare::getShare(session, table_list, cache_key,
1956
table_list->i_s_requested_object,
2005
if (not (share= TableShare::getShareCreate(session,
1960
while ((error= open_table_from_share(session, share, alias,
1961
(uint32_t) (HA_OPEN_KEYFILE |
1965
session->open_options, entry)))
2010
while ((error= share->open_table_from_share(session,
2013
(uint32_t) (HA_OPEN_KEYFILE |
2017
session->open_options, *entry)))
1967
2019
if (error == 7) // Table def changed
1969
share->version= 0; // Mark share as old
2021
share->resetVersion(); // Mark share as old
1970
2022
if (discover_retry_count++) // Retry once
2287
2339
Table *Session::open_temporary_table(TableIdentifier &identifier,
2288
2340
bool link_in_list)
2290
Table *new_tmp_table;
2291
2342
TableShare *share;
2292
char cache_key[MAX_DBKEY_LENGTH], *saved_cache_key, *tmp_path;
2293
uint32_t key_length, path_length;
2294
TableList table_list;
2296
table_list.db= (char*) identifier.getSchemaName().c_str();
2297
table_list.table_name= (char*) identifier.getTableName().c_str();
2298
/* Create the cache_key for temporary tables */
2299
key_length= table_list.create_table_def_key(cache_key);
2300
path_length= identifier.getPath().length();
2302
if (!(new_tmp_table= (Table*) malloc(sizeof(*new_tmp_table) + sizeof(*share) +
2303
path_length + 1 + key_length)))
2344
assert(identifier.isTmp());
2345
share= new TableShare(identifier.getType(),
2347
const_cast<char *>(identifier.getPath().c_str()), static_cast<uint32_t>(identifier.getPath().length()));
2350
Table *new_tmp_table= new Table;
2351
if (not new_tmp_table)
2306
share= (TableShare*) (new_tmp_table+1);
2307
tmp_path= (char*) (share+1);
2308
saved_cache_key= strcpy(tmp_path, identifier.getPath().c_str())+path_length+1;
2309
memcpy(saved_cache_key, cache_key, key_length);
2311
share->init(saved_cache_key, key_length, strchr(saved_cache_key, '\0')+1, tmp_path);
2314
2355
First open the share, and then open the table from the share we just opened.
2316
if (open_table_def(*this, identifier, share) ||
2317
open_table_from_share(this, share, identifier.getTableName().c_str(),
2357
if (share->open_table_def(*this, identifier) ||
2358
share->open_table_from_share(this, identifier, identifier.getTableName().c_str(),
2318
2359
(uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
2320
2361
ha_open_options,
2323
2364
/* No need to lock share->mutex as this is not needed for tmp tables */
2324
share->free_table_share();
2325
free((char*) new_tmp_table);
2366
delete new_tmp_table;
2329
2371
new_tmp_table->reginfo.lock_type= TL_WRITE; // Simulate locked
2330
share->tmp_table= message::Table::TEMPORARY;
2332
2373
if (link_in_list)
2334
2375
/* growing temp list at the head */
2335
new_tmp_table->next= this->temporary_tables;
2336
if (new_tmp_table->next)
2337
new_tmp_table->next->prev= new_tmp_table;
2376
new_tmp_table->setNext(this->temporary_tables);
2377
if (new_tmp_table->getNext())
2379
new_tmp_table->getNext()->setPrev(new_tmp_table);
2338
2381
this->temporary_tables= new_tmp_table;
2339
this->temporary_tables->prev= 0;
2382
this->temporary_tables->setPrev(0);
2341
2384
new_tmp_table->pos_in_table_list= 0;
2488
2531
uint32_t cached_field_index= *cached_field_index_ptr;
2490
2533
/* We assume here that table->field < NO_CACHED_FIELD_INDEX = UINT_MAX */
2491
if (cached_field_index < table->s->fields &&
2534
if (cached_field_index < table->getShare()->sizeFields() &&
2492
2535
!my_strcasecmp(system_charset_info,
2493
table->field[cached_field_index]->field_name, name))
2494
field_ptr= table->field + cached_field_index;
2495
else if (table->s->name_hash.records)
2497
field_ptr= (Field**) hash_search(&table->s->name_hash, (unsigned char*) name,
2536
table->getField(cached_field_index)->field_name, name))
2538
field_ptr= table->getFields() + cached_field_index;
2540
else if (table->getShare()->getNamedFieldSize())
2542
field_ptr= table->getMutableShare()->getNamedField(std::string(name, length));
2502
2546
field_ptr points to field in TableShare. Convert it to the matching
2505
field_ptr= (table->field + (field_ptr - table->s->field));
2549
field_ptr= (table->getFields() + table->getShare()->positionFields(field_ptr));
2510
if (!(field_ptr= table->field))
2554
if (!(field_ptr= table->getFields()))
2511
2555
return((Field *)0);
2512
2556
for (; *field_ptr; ++field_ptr)
2513
2557
if (!my_strcasecmp(system_charset_info, (*field_ptr)->field_name, name))
4437
4482
and afterwards delete those marked unused.
4440
void remove_db_from_cache(SchemaIdentifier &schema_identifier)
4485
void remove_db_from_cache(const SchemaIdentifier &schema_identifier)
4442
safe_mutex_assert_owner(&LOCK_open);
4487
safe_mutex_assert_owner(LOCK_open.native_handle());
4444
for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
4489
for (TableOpenCache::const_iterator iter= get_open_cache().begin();
4490
iter != get_open_cache().end();
4446
Table *table=(Table*) hash_element(&open_cache,idx);
4447
if (not schema_identifier.getPath().compare(table->s->getSchemaName()))
4493
Table *table= (*iter).second;
4495
if (not schema_identifier.getPath().compare(table->getMutableShare()->getSchemaName()))
4449
table->s->version= 0L; /* Free when thread is ready */
4497
table->getMutableShare()->resetVersion(); /* Free when thread is ready */
4450
4498
if (not table->in_use)
4451
relink_unused(table);
4499
unused_tables.relink(table);
4454
while (unused_tables && !unused_tables->s->version)
4455
hash_delete(&open_cache,(unsigned char*) unused_tables);
4503
unused_tables.cullByVersion();
4471
4519
1 Table is in use by another thread
4474
bool remove_table_from_cache(Session *session, const char *db, const char *table_name,
4522
bool remove_table_from_cache(Session *session, TableIdentifier &identifier, uint32_t flags)
4477
char key[MAX_DBKEY_LENGTH];
4479
uint32_t key_length;
4524
const TableIdentifier::Key &key(identifier.getKey());
4481
4525
bool result= false;
4482
4526
bool signalled= false;
4484
key_pos= strcpy(key_pos, db) + strlen(db);
4485
key_pos= strcpy(key_pos+1, table_name) + strlen(table_name);
4486
key_length= (uint32_t) (key_pos-key)+1;
4490
HASH_SEARCH_STATE state;
4491
4530
result= signalled= false;
4493
for (table= (Table*) hash_first(&open_cache, (unsigned char*) key, key_length,
4496
table= (Table*) hash_next(&open_cache, (unsigned char*) key, key_length,
4532
TableOpenCacheRange ppp;
4533
ppp= get_open_cache().equal_range(key);
4535
for (TableOpenCache::const_iterator iter= ppp.first;
4536
iter != ppp.second; ++iter)
4538
Table *table= (*iter).second;
4499
4539
Session *in_use;
4501
table->s->version=0L; /* Free when thread is ready */
4541
table->getMutableShare()->resetVersion(); /* Free when thread is ready */
4502
4542
if (!(in_use=table->in_use))
4504
relink_unused(table);
4544
unused_tables.relink(table);
4506
4546
else if (in_use != session)