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-1301 USA */
19
20
21struct 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
29typedef int64 tdc_version_t;
30#define TDC_VERSION_MAX INT_MAX64
31
32struct TDC_element
33{
34 uchar m_key[NAME_LEN + 1 + NAME_LEN + 1];
35 uint m_key_length;
36 tdc_version_t version;
37 bool flushed;
38 TABLE_SHARE *share;
39
40 /**
41 Protects ref_count, m_flush_tickets, all_tables, flushed, all_tables_refs.
42 */
43 mysql_mutex_t LOCK_table_share;
44 mysql_cond_t COND_release;
45 TDC_element *next, **prev; /* Link to unused shares */
46 uint ref_count; /* How many TABLE objects uses this */
47 uint all_tables_refs; /* Number of refs to all_tables */
48 /**
49 List of tickets representing threads waiting for the share to be flushed.
50 */
51 Wait_for_flush_list m_flush_tickets;
52 /*
53 Doubly-linked (back-linked) lists of used and unused TABLE objects
54 for this share.
55 */
56 All_share_tables_list all_tables;
57 /** Avoid false sharing between TDC_element and free_tables */
58 char pad[CPU_LEVEL1_DCACHE_LINESIZE];
59 Share_free_tables free_tables[1];
60};
61
62
63enum enum_tdc_remove_table_type
64{
65 TDC_RT_REMOVE_ALL,
66 TDC_RT_REMOVE_NOT_OWN,
67 TDC_RT_REMOVE_UNUSED,
68 TDC_RT_REMOVE_NOT_OWN_KEEP_SHARE
69};
70
71extern ulong tdc_size;
72extern ulong tc_size;
73extern uint32 tc_instances;
74extern uint32 tc_active_instances;
75
76extern bool tdc_init(void);
77extern void tdc_start_shutdown(void);
78extern void tdc_deinit(void);
79extern ulong tdc_records(void);
80extern void tdc_purge(bool all);
81extern TDC_element *tdc_lock_share(THD *thd, const char *db,
82 const char *table_name);
83extern void tdc_unlock_share(TDC_element *element);
84extern TABLE_SHARE *tdc_acquire_share(THD *thd, TABLE_LIST *tl, uint flags,
85 TABLE **out_table= 0);
86extern void tdc_release_share(TABLE_SHARE *share);
87extern bool tdc_remove_table(THD *thd, enum_tdc_remove_table_type remove_type,
88 const char *db, const char *table_name,
89 bool kill_delayed_threads);
90
91
92extern int tdc_wait_for_old_version(THD *thd, const char *db,
93 const char *table_name,
94 ulong wait_timeout, uint deadlock_weight,
95 tdc_version_t refresh_version= TDC_VERSION_MAX);
96extern tdc_version_t tdc_refresh_version(void);
97extern tdc_version_t tdc_increment_refresh_version(void);
98extern int tdc_iterate(THD *thd, my_hash_walk_action action, void *argument,
99 bool no_dups= false);
100
101extern uint tc_records(void);
102extern void tc_purge(bool mark_flushed= false);
103extern void tc_add_table(THD *thd, TABLE *table);
104extern void tc_release_table(TABLE *table);
105
106/**
107 Create a table cache key for non-temporary table.
108
109 @param key Buffer for key (must be at least NAME_LEN*2+2 bytes).
110 @param db Database name.
111 @param table_name Table name.
112
113 @return Length of key.
114*/
115
116inline uint tdc_create_key(char *key, const char *db, const char *table_name)
117{
118 /*
119 In theory caller should ensure that both db and table_name are
120 not longer than NAME_LEN bytes. In practice we play safe to avoid
121 buffer overruns.
122 */
123 return (uint) (strmake(strmake(key, db, NAME_LEN) + 1, table_name,
124 NAME_LEN) - key + 1);
125}
126#endif /* TABLE_CACHE_H_INCLUDED */
127