1 #ifndef TABLE_CACHE_H_INCLUDED
2 #define TABLE_CACHE_H_INCLUDED
3 /* Copyright (c) 2000, 2012, Oracle and/or its affiliates.
4 Copyright (c) 2010, 2011 Monty Program Ab
5 Copyright (C) 2013 Sergey Vojtovich and MariaDB Foundation
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; version 2 of the License.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
19
20
21 struct Share_free_tables
22 {
23 typedef I_P_List <TABLE, TABLE_share> List;
24 List list;
25 /** Avoid false sharing between instances */
26 char pad[CPU_LEVEL1_DCACHE_LINESIZE];
27 };
28
29
30 struct TDC_element
31 {
32 uchar m_key[NAME_LEN + 1 + NAME_LEN + 1];
33 uint m_key_length;
34 bool flushed;
35 TABLE_SHARE *share;
36
37 /**
38 Protects ref_count, m_flush_tickets, all_tables, flushed, all_tables_refs.
39 */
40 mysql_mutex_t LOCK_table_share;
41 mysql_cond_t COND_release;
42 TDC_element *next, **prev; /* Link to unused shares */
43 uint ref_count; /* How many TABLE objects uses this */
44 uint all_tables_refs; /* Number of refs to all_tables */
45 /**
46 List of tickets representing threads waiting for the share to be flushed.
47 */
48 Wait_for_flush_list m_flush_tickets;
49 /*
50 Doubly-linked (back-linked) lists of used and unused TABLE objects
51 for this share.
52 */
53 All_share_tables_list all_tables;
54 /** Avoid false sharing between TDC_element and free_tables */
55 char pad[CPU_LEVEL1_DCACHE_LINESIZE];
56 Share_free_tables free_tables[1];
57
58 inline void wait_for_refs(uint my_refs);
59 void flush(THD *thd, bool mark_flushed);
60 void flush_unused(bool mark_flushed);
61 };
62
63
64 extern ulong tdc_size;
65 extern ulong tc_size;
66 extern uint32 tc_instances;
67
68 extern bool tdc_init(void);
69 extern void tdc_start_shutdown(void);
70 extern void tdc_deinit(void);
71 extern ulong tdc_records(void);
72 extern void tdc_purge(bool all);
73 extern TDC_element *tdc_lock_share(THD *thd, const char *db,
74 const char *table_name);
75 extern void tdc_unlock_share(TDC_element *element);
76 int tdc_share_is_cached(THD *thd, const char *db, const char *table_name);
77 extern TABLE_SHARE *tdc_acquire_share(THD *thd, TABLE_LIST *tl, uint flags,
78 TABLE **out_table= 0);
79 extern void tdc_release_share(TABLE_SHARE *share);
80 void tdc_remove_referenced_share(THD *thd, TABLE_SHARE *share);
81 void tdc_remove_table(THD *thd, const char *db, const char *table_name);
82
83 extern int tdc_wait_for_old_version(THD *thd, const char *db,
84 const char *table_name,
85 ulong wait_timeout, uint deadlock_weight);
86 extern int tdc_iterate(THD *thd, my_hash_walk_action action, void *argument,
87 bool no_dups= false);
88
89 extern uint tc_records(void);
90 int show_tc_active_instances(THD *thd, SHOW_VAR *var, char *buff,
91 enum enum_var_type scope);
92 extern void tc_purge();
93 extern void tc_add_table(THD *thd, TABLE *table);
94 extern void tc_release_table(TABLE *table);
95 extern TABLE *tc_acquire_table(THD *thd, TDC_element *element);
96
97 /**
98 Create a table cache key for non-temporary table.
99
100 @param key Buffer for key (must be at least NAME_LEN*2+2 bytes).
101 @param db Database name.
102 @param table_name Table name.
103
104 @return Length of key.
105 */
106
tdc_create_key(char * key,const char * db,const char * table_name)107 inline uint tdc_create_key(char *key, const char *db, const char *table_name)
108 {
109 /*
110 In theory caller should ensure that both db and table_name are
111 not longer than NAME_LEN bytes. In practice we play safe to avoid
112 buffer overruns.
113 */
114 return (uint) (strmake(strmake(key, db, NAME_LEN) + 1, table_name,
115 NAME_LEN) - key + 1);
116 }
117 #endif /* TABLE_CACHE_H_INCLUDED */
118