MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
table_setup_actors.cc
Go to the documentation of this file.
1 /* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
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 as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software Foundation,
14  51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
15 
21 #include "my_global.h"
22 #include "my_pthread.h"
23 #include "pfs_instr_class.h"
24 #include "pfs_column_types.h"
25 #include "pfs_column_values.h"
26 #include "pfs_setup_actor.h"
27 #include "table_setup_actors.h"
28 #include "pfs_global.h"
29 
30 THR_LOCK table_setup_actors::m_table_lock;
31 
32 static const TABLE_FIELD_TYPE field_types[]=
33 {
34  {
35  { C_STRING_WITH_LEN("HOST") },
36  { C_STRING_WITH_LEN("char(60)") },
37  { NULL, 0}
38  },
39  {
40  { C_STRING_WITH_LEN("USER") },
41  { C_STRING_WITH_LEN("char(16)") },
42  { NULL, 0}
43  },
44  {
45  { C_STRING_WITH_LEN("ROLE") },
46  { C_STRING_WITH_LEN("char(16)") },
47  { NULL, 0}
48  }
49 };
50 
52 table_setup_actors::m_field_def=
53 { 3, field_types };
54 
57 {
58  { C_STRING_WITH_LEN("setup_actors") },
61  table_setup_actors::write_row,
62  table_setup_actors::delete_all_rows,
63  table_setup_actors::get_row_count,
64  1000, /* records */
65  sizeof(PFS_simple_index),
66  &m_table_lock,
67  &m_field_def,
68  false /* checked */
69 };
70 
72 {
73  return new table_setup_actors();
74 }
75 
76 int table_setup_actors::write_row(TABLE *table, unsigned char *buf,
77  Field **fields)
78 {
79  Field *f;
80  String user_data("%", 1, &my_charset_utf8_bin);
81  String host_data("%", 1, &my_charset_utf8_bin);
82  String role_data("%", 1, &my_charset_utf8_bin);
83  String *user= &user_data;
84  String *host= &host_data;
85  String *role= &role_data;
86 
87  for (; (f= *fields) ; fields++)
88  {
89  if (bitmap_is_set(table->write_set, f->field_index))
90  {
91  switch(f->field_index)
92  {
93  case 0: /* HOST */
94  host= get_field_char_utf8(f, &host_data);
95  break;
96  case 1: /* USER */
97  user= get_field_char_utf8(f, &user_data);
98  break;
99  case 2: /* ROLE */
100  role= get_field_char_utf8(f, &role_data);
101  break;
102  default:
103  DBUG_ASSERT(false);
104  }
105  }
106  }
107 
108  if (user->length() == 0 || host->length() == 0 || role->length() == 0)
109  return HA_ERR_WRONG_COMMAND;
110 
111  return insert_setup_actor(user, host, role);
112 }
113 
114 int table_setup_actors::delete_all_rows(void)
115 {
116  return reset_setup_actor();
117 }
118 
119 ha_rows table_setup_actors::get_row_count(void)
120 {
121  return setup_actor_count();
122 }
123 
124 table_setup_actors::table_setup_actors()
125  : PFS_engine_table(&m_share, &m_pos),
126  m_row_exists(false), m_pos(0), m_next_pos(0)
127 {}
128 
130 {
131  m_pos.m_index= 0;
132  m_next_pos.m_index= 0;
133 }
134 
136 {
137  PFS_setup_actor *pfs;
138 
139  for (m_pos.set_at(&m_next_pos);
140  m_pos.m_index < setup_actor_max;
141  m_pos.next())
142  {
143  pfs= &setup_actor_array[m_pos.m_index];
144  if (pfs->m_lock.is_populated())
145  {
146  make_row(pfs);
147  m_next_pos.set_after(&m_pos);
148  return 0;
149  }
150  }
151 
152  return HA_ERR_END_OF_FILE;
153 }
154 
155 int table_setup_actors::rnd_pos(const void *pos)
156 {
157  PFS_setup_actor *pfs;
158 
159  set_position(pos);
160 
161  DBUG_ASSERT(m_pos.m_index < setup_actor_max);
162  pfs= &setup_actor_array[m_pos.m_index];
163  if (pfs->m_lock.is_populated())
164  {
165  make_row(pfs);
166  return 0;
167  }
168 
169  return HA_ERR_RECORD_DELETED;
170 }
171 
172 void table_setup_actors::make_row(PFS_setup_actor *pfs)
173 {
174  pfs_lock lock;
175 
176  m_row_exists= false;
177 
178  pfs->m_lock.begin_optimistic_lock(&lock);
179 
181  if (unlikely((m_row.m_hostname_length == 0) ||
182  (m_row.m_hostname_length > sizeof(m_row.m_hostname))))
183  return;
184  memcpy(m_row.m_hostname, pfs->m_hostname, m_row.m_hostname_length);
185 
187  if (unlikely((m_row.m_username_length == 0) ||
188  (m_row.m_username_length > sizeof(m_row.m_username))))
189  return;
190  memcpy(m_row.m_username, pfs->m_username, m_row.m_username_length);
191 
193  if (unlikely((m_row.m_rolename_length == 0) ||
194  (m_row.m_rolename_length > sizeof(m_row.m_rolename))))
195  return;
196  memcpy(m_row.m_rolename, pfs->m_rolename, m_row.m_rolename_length);
197 
198  if (pfs->m_lock.end_optimistic_lock(&lock))
199  m_row_exists= true;
200 }
201 
203  unsigned char *buf,
204  Field **fields,
205  bool read_all)
206 {
207  Field *f;
208 
209  if (unlikely(! m_row_exists))
210  return HA_ERR_RECORD_DELETED;
211 
212  /* Set the null bits */
213  DBUG_ASSERT(table->s->null_bytes == 1);
214 
215  for (; (f= *fields) ; fields++)
216  {
217  if (read_all || bitmap_is_set(table->read_set, f->field_index))
218  {
219  switch(f->field_index)
220  {
221  case 0: /* HOST */
223  break;
224  case 1: /* USER */
226  break;
227  case 2: /* ROLE */
229  break;
230  default:
231  DBUG_ASSERT(false);
232  }
233  }
234  }
235 
236  return 0;
237 }
238 
240  const unsigned char *old_buf,
241  unsigned char *new_buf,
242  Field **fields)
243 {
244  Field *f;
245 
246  for (; (f= *fields) ; fields++)
247  {
248  if (bitmap_is_set(table->write_set, f->field_index))
249  {
250  switch(f->field_index)
251  {
252  case 0: /* HOST */
253  case 1: /* USER */
254  case 2: /* ROLE */
255  return HA_ERR_WRONG_COMMAND;
256  break;
257  default:
258  DBUG_ASSERT(false);
259  }
260  }
261  }
262 
263  return 0;
264 }
265 
267  const unsigned char *buf,
268  Field **fields)
269 {
270  DBUG_ASSERT(m_row_exists);
271 
272  CHARSET_INFO *cs= &my_charset_utf8_bin;
273  String user(m_row.m_username, m_row.m_username_length, cs);
274  String role(m_row.m_rolename, m_row.m_rolename_length, cs);
275  String host(m_row.m_hostname, m_row.m_hostname_length, cs);
276 
277  return delete_setup_actor(&user, &host, &role);
278 }
279