1 /* Copyright (c) 2013, 2021, Oracle and/or its affiliates.
2 
3   This program is free software; you can redistribute it and/or modify
4   it under the terms of the GNU General Public License, version 2.0,
5   as published by the Free Software Foundation.
6 
7   This program is also distributed with certain software (including
8   but not limited to OpenSSL) that is licensed under separate terms,
9   as designated in a particular file or component or in included license
10   documentation.  The authors of MySQL hereby grant you an additional
11   permission to link the program and your derivative works with the
12   separately licensed software that they have included with MySQL.
13 
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License, version 2.0, for more details.
18 
19   You should have received a copy of the GNU General Public License
20   along with this program; if not, write to the Free Software
21   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 #ifndef TABLE_UVAR_BY_THREAD_H
24 #define TABLE_UVAR_BY_THREAD_H
25 
26 /**
27   @file storage/perfschema/table_uvar_by_thread.h
28   Table USER_VARIABLES_BY_THREAD (declarations).
29 */
30 
31 #include "pfs_column_types.h"
32 #include "pfs_engine_table.h"
33 #include "pfs_instr_class.h"
34 #include "pfs_instr.h"
35 #include "table_helper.h"
36 
37 /**
38   @addtogroup Performance_schema_tables
39   @{
40 */
41 
42 struct User_variable
43 {
44 public:
User_variableUser_variable45   User_variable()
46   {}
47 
User_variableUser_variable48   User_variable(const User_variable& uv)
49     : m_name(uv.m_name), m_value(uv.m_value)
50   {}
51 
~User_variableUser_variable52   ~User_variable()
53   {}
54 
55   PFS_variable_name_row m_name;
56   PFS_user_variable_value_row m_value;
57 };
58 
59 class User_variables
60 {
61   typedef Prealloced_array<User_variable, 100, false> User_variable_array;
62 
63 public:
User_variables()64   User_variables()
65     : m_pfs(NULL), m_thread_internal_id(0), m_array(PSI_INSTRUMENT_ME)
66   {
67   }
68 
reset()69   void reset()
70   {
71     m_pfs= NULL;
72     m_thread_internal_id= 0;
73     m_array.clear();
74   }
75 
76   void materialize(PFS_thread *pfs, THD *thd);
77 
is_materialized(PFS_thread * pfs)78   bool is_materialized(PFS_thread *pfs)
79   {
80     assert(pfs != NULL);
81     if (m_pfs != pfs)
82       return false;
83     if (m_thread_internal_id != pfs->m_thread_internal_id)
84       return false;
85     return true;
86   }
87 
get(uint index)88   const User_variable *get(uint index) const
89   {
90     if (index >= m_array.size())
91       return NULL;
92 
93     const User_variable *p= & m_array.at(index);
94     return p;
95   }
96 
97 private:
98   PFS_thread *m_pfs;
99   ulonglong m_thread_internal_id;
100   User_variable_array m_array;
101 };
102 
103 /**
104   A row of table
105   PERFORMANCE_SCHEMA.USER_VARIABLES_BY_THREAD.
106 */
107 struct row_uvar_by_thread
108 {
109   /** Column THREAD_ID. */
110   ulonglong m_thread_internal_id;
111   /** Column VARIABLE_NAME. */
112   const PFS_variable_name_row *m_variable_name;
113   /** Column VARIABLE_VALUE. */
114   const PFS_user_variable_value_row *m_variable_value;
115 };
116 
117 /**
118   Position of a cursor on
119   PERFORMANCE_SCHEMA.USER_VARIABLES_BY_THREAD.
120   Index 1 on thread (0 based)
121   Index 2 on user variable (0 based)
122 */
123 struct pos_uvar_by_thread
124 : public PFS_double_index
125 {
pos_uvar_by_threadpos_uvar_by_thread126   pos_uvar_by_thread()
127     : PFS_double_index(0, 0)
128   {}
129 
resetpos_uvar_by_thread130   inline void reset(void)
131   {
132     m_index_1= 0;
133     m_index_2= 0;
134   }
135 
next_threadpos_uvar_by_thread136   inline void next_thread(void)
137   {
138     m_index_1++;
139     m_index_2= 0;
140   }
141 };
142 
143 /** Table PERFORMANCE_SCHEMA.USER_VARIABLES_BY_THREAD. */
144 class table_uvar_by_thread : public PFS_engine_table
145 {
146   typedef pos_uvar_by_thread pos_t;
147 
148 public:
149   /** Table share */
150   static PFS_engine_table_share m_share;
151   static PFS_engine_table* create();
152   static ha_rows get_row_count();
153 
154   virtual int rnd_next();
155   virtual int rnd_pos(const void *pos);
156   virtual void reset_position(void);
157 
158 protected:
159   virtual int read_row_values(TABLE *table,
160                               unsigned char *buf,
161                               Field **fields,
162                               bool read_all);
163 
164   table_uvar_by_thread();
165 
166 public:
~table_uvar_by_thread()167   ~table_uvar_by_thread()
168   { m_THD_cache.reset(); }
169 
170 protected:
171   int materialize(PFS_thread *thread);
172   void make_row(PFS_thread *thread, const User_variable *uvar);
173 
174 private:
175   /** Table share lock. */
176   static THR_LOCK m_table_lock;
177   /** Fields definition. */
178   static TABLE_FIELD_DEF m_field_def;
179 
180   /** Current THD user variables. */
181   User_variables m_THD_cache;
182   /** Current row. */
183   row_uvar_by_thread m_row;
184   /** True is the current row exists. */
185   bool m_row_exists;
186   /** Current position. */
187   pos_t m_pos;
188   /** Next position. */
189   pos_t m_next_pos;
190 };
191 
192 /** @} */
193 #endif
194