16 #ifndef TABLE_CACHE_INCLUDED
17 #define TABLE_CACHE_INCLUDED
19 #include "my_global.h"
20 #include "sql_class.h"
81 TABLE *m_unused_tables;
91 #ifdef HAVE_PSI_INTERFACE
92 static PSI_mutex_key m_lock_key;
93 static PSI_mutex_info m_mutex_keys[];
101 void check_unused() {}
104 inline void unlink_unused_table(
TABLE *table);
106 inline void free_unused_tables_if_necessary(THD *thd);
122 const char *key, uint key_length,
158 return &m_table_cache[thd->thread_id % table_cache_instances];
164 return (cache - &m_table_cache[0]);
175 enum_tdc_remove_table_type remove_type,
247 uint current_cache_index;
248 TABLE *current_table;
250 inline void move_to_next_table();
260 inline void rewind();
269 void Table_cache::link_unused_table(
TABLE *
table)
273 table->next= m_unused_tables;
274 table->prev= m_unused_tables->prev;
275 m_unused_tables->prev=
table;
276 table->prev->next=
table;
279 m_unused_tables= table->next= table->prev=
table;
286 void Table_cache::unlink_unused_table(
TABLE *table)
288 table->next->prev= table->prev;
289 table->prev->next= table->next;
290 if (table == m_unused_tables)
292 m_unused_tables= m_unused_tables->next;
293 if (table == m_unused_tables)
294 m_unused_tables= NULL;
309 void Table_cache::free_unused_tables_if_necessary(THD *thd)
318 if (m_table_count > table_cache_size_per_instance && m_unused_tables)
321 while (m_table_count > table_cache_size_per_instance &&
324 TABLE *table_to_free= m_unused_tables;
326 intern_close_table(table_to_free);
327 thd->status_var.table_open_cache_overflows++;
352 DBUG_ASSERT(table->in_use == thd);
370 DBUG_ASSERT(! my_hash_search(&m_cache,
371 (uchar*)table->s->table_cache_key.str,
372 table->s->table_cache_key.length));
377 if (my_hash_insert(&m_cache, (uchar*)el))
387 el->used_tables.push_front(table);
391 free_unused_tables_if_necessary(thd);
414 el->used_tables.remove(table);
419 el->free_tables.remove(table);
422 unlink_unused_table(table);
427 if (el->used_tables.is_empty() && el->free_tables.is_empty())
429 (void) my_hash_delete(&m_cache, (uchar*) el);
462 const char *key, uint key_length,
473 hash_value, (uchar*) key, key_length)))
478 if ((table= el->free_tables.front()))
480 DBUG_ASSERT(!table->in_use);
486 el->free_tables.remove(table);
489 unlink_unused_table(table);
495 el->used_tables.push_front(table);
499 DBUG_ASSERT(table->db_stat && table->file);
501 DBUG_ASSERT(! table->file->extra(HA_EXTRA_IS_ATTACHED_CHILDREN));
523 DBUG_ASSERT(table->in_use);
524 DBUG_ASSERT(table->file);
532 el->used_tables.remove(table);
534 el->free_tables.push_front(table);
536 link_unused_table(table);
542 free_unused_tables_if_necessary(thd);
552 : share(share_arg), current_cache_index(0), current_table(NULL)
555 move_to_next_table();
561 void Table_cache_iterator::move_to_next_table()
563 for (; current_cache_index < table_cache_instances; ++current_cache_index)
569 if ((current_table= el->used_tables.front()))
586 TABLE *result= current_table;
591 it(share->
cache_element[current_cache_index]->used_tables, current_table);
597 ++current_cache_index;
598 move_to_next_table();
606 void Table_cache_iterator::rewind()
608 current_cache_index= 0;
610 move_to_next_table();