| 1 | /* Copyright (c) 2010, 2012, 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 | |
| 16 | /** |
| 17 | @file storage/perfschema/table_setup_actors.cc |
| 18 | Table SETUP_ACTORS (implementation). |
| 19 | */ |
| 20 | |
| 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 | PFS_engine_table_share |
| 33 | table_setup_actors::m_share= |
| 34 | { |
| 35 | { C_STRING_WITH_LEN("setup_actors" ) }, |
| 36 | &pfs_editable_acl, |
| 37 | table_setup_actors::create, |
| 38 | table_setup_actors::write_row, |
| 39 | table_setup_actors::delete_all_rows, |
| 40 | table_setup_actors::get_row_count, |
| 41 | 1000, /* records */ |
| 42 | sizeof(PFS_simple_index), |
| 43 | &m_table_lock, |
| 44 | { C_STRING_WITH_LEN("CREATE TABLE setup_actors(" |
| 45 | "HOST CHAR(60) collate utf8_bin default '%' not null," |
| 46 | "USER CHAR(16) collate utf8_bin default '%' not null," |
| 47 | "ROLE CHAR(16) collate utf8_bin default '%' not null)" ) } |
| 48 | }; |
| 49 | |
| 50 | PFS_engine_table* table_setup_actors::create() |
| 51 | { |
| 52 | return new table_setup_actors(); |
| 53 | } |
| 54 | |
| 55 | int table_setup_actors::write_row(TABLE *table, unsigned char *buf, |
| 56 | Field **fields) |
| 57 | { |
| 58 | Field *f; |
| 59 | String user_data("%" , 1, &my_charset_utf8_bin); |
| 60 | String host_data("%" , 1, &my_charset_utf8_bin); |
| 61 | String role_data("%" , 1, &my_charset_utf8_bin); |
| 62 | String *user= &user_data; |
| 63 | String *host= &host_data; |
| 64 | String *role= &role_data; |
| 65 | |
| 66 | for (; (f= *fields) ; fields++) |
| 67 | { |
| 68 | if (bitmap_is_set(table->write_set, f->field_index)) |
| 69 | { |
| 70 | switch(f->field_index) |
| 71 | { |
| 72 | case 0: /* HOST */ |
| 73 | host= get_field_char_utf8(f, &host_data); |
| 74 | break; |
| 75 | case 1: /* USER */ |
| 76 | user= get_field_char_utf8(f, &user_data); |
| 77 | break; |
| 78 | case 2: /* ROLE */ |
| 79 | role= get_field_char_utf8(f, &role_data); |
| 80 | break; |
| 81 | default: |
| 82 | DBUG_ASSERT(false); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if (user->length() == 0 || host->length() == 0 || role->length() == 0) |
| 88 | return HA_ERR_WRONG_COMMAND; |
| 89 | |
| 90 | return insert_setup_actor(user, host, role); |
| 91 | } |
| 92 | |
| 93 | int table_setup_actors::delete_all_rows(void) |
| 94 | { |
| 95 | return reset_setup_actor(); |
| 96 | } |
| 97 | |
| 98 | ha_rows table_setup_actors::get_row_count(void) |
| 99 | { |
| 100 | return setup_actor_count(); |
| 101 | } |
| 102 | |
| 103 | table_setup_actors::table_setup_actors() |
| 104 | : PFS_engine_table(&m_share, &m_pos), |
| 105 | m_row_exists(false), m_pos(0), m_next_pos(0) |
| 106 | {} |
| 107 | |
| 108 | void table_setup_actors::reset_position(void) |
| 109 | { |
| 110 | m_pos.m_index= 0; |
| 111 | m_next_pos.m_index= 0; |
| 112 | } |
| 113 | |
| 114 | int table_setup_actors::rnd_next() |
| 115 | { |
| 116 | PFS_setup_actor *pfs; |
| 117 | |
| 118 | for (m_pos.set_at(&m_next_pos); |
| 119 | m_pos.m_index < setup_actor_max; |
| 120 | m_pos.next()) |
| 121 | { |
| 122 | pfs= &setup_actor_array[m_pos.m_index]; |
| 123 | if (pfs->m_lock.is_populated()) |
| 124 | { |
| 125 | make_row(pfs); |
| 126 | m_next_pos.set_after(&m_pos); |
| 127 | return 0; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | return HA_ERR_END_OF_FILE; |
| 132 | } |
| 133 | |
| 134 | int table_setup_actors::rnd_pos(const void *pos) |
| 135 | { |
| 136 | PFS_setup_actor *pfs; |
| 137 | |
| 138 | set_position(pos); |
| 139 | |
| 140 | DBUG_ASSERT(m_pos.m_index < setup_actor_max); |
| 141 | pfs= &setup_actor_array[m_pos.m_index]; |
| 142 | if (pfs->m_lock.is_populated()) |
| 143 | { |
| 144 | make_row(pfs); |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | return HA_ERR_RECORD_DELETED; |
| 149 | } |
| 150 | |
| 151 | void table_setup_actors::make_row(PFS_setup_actor *pfs) |
| 152 | { |
| 153 | pfs_lock lock; |
| 154 | |
| 155 | m_row_exists= false; |
| 156 | |
| 157 | pfs->m_lock.begin_optimistic_lock(&lock); |
| 158 | |
| 159 | m_row.m_hostname_length= pfs->m_hostname_length; |
| 160 | if (unlikely((m_row.m_hostname_length == 0) || |
| 161 | (m_row.m_hostname_length > sizeof(m_row.m_hostname)))) |
| 162 | return; |
| 163 | memcpy(m_row.m_hostname, pfs->m_hostname, m_row.m_hostname_length); |
| 164 | |
| 165 | m_row.m_username_length= pfs->m_username_length; |
| 166 | if (unlikely((m_row.m_username_length == 0) || |
| 167 | (m_row.m_username_length > sizeof(m_row.m_username)))) |
| 168 | return; |
| 169 | memcpy(m_row.m_username, pfs->m_username, m_row.m_username_length); |
| 170 | |
| 171 | m_row.m_rolename_length= pfs->m_rolename_length; |
| 172 | if (unlikely((m_row.m_rolename_length == 0) || |
| 173 | (m_row.m_rolename_length > sizeof(m_row.m_rolename)))) |
| 174 | return; |
| 175 | memcpy(m_row.m_rolename, pfs->m_rolename, m_row.m_rolename_length); |
| 176 | |
| 177 | if (pfs->m_lock.end_optimistic_lock(&lock)) |
| 178 | m_row_exists= true; |
| 179 | } |
| 180 | |
| 181 | int table_setup_actors::read_row_values(TABLE *table, |
| 182 | unsigned char *buf, |
| 183 | Field **fields, |
| 184 | bool read_all) |
| 185 | { |
| 186 | Field *f; |
| 187 | |
| 188 | if (unlikely(! m_row_exists)) |
| 189 | return HA_ERR_RECORD_DELETED; |
| 190 | |
| 191 | /* Set the null bits */ |
| 192 | DBUG_ASSERT(table->s->null_bytes == 1); |
| 193 | |
| 194 | for (; (f= *fields) ; fields++) |
| 195 | { |
| 196 | if (read_all || bitmap_is_set(table->read_set, f->field_index)) |
| 197 | { |
| 198 | switch(f->field_index) |
| 199 | { |
| 200 | case 0: /* HOST */ |
| 201 | set_field_char_utf8(f, m_row.m_hostname, m_row.m_hostname_length); |
| 202 | break; |
| 203 | case 1: /* USER */ |
| 204 | set_field_char_utf8(f, m_row.m_username, m_row.m_username_length); |
| 205 | break; |
| 206 | case 2: /* ROLE */ |
| 207 | set_field_char_utf8(f, m_row.m_rolename, m_row.m_rolename_length); |
| 208 | break; |
| 209 | default: |
| 210 | DBUG_ASSERT(false); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | int table_setup_actors::update_row_values(TABLE *table, |
| 219 | const unsigned char *old_buf, |
| 220 | const unsigned char *new_buf, |
| 221 | Field **fields) |
| 222 | { |
| 223 | Field *f; |
| 224 | |
| 225 | for (; (f= *fields) ; fields++) |
| 226 | { |
| 227 | if (bitmap_is_set(table->write_set, f->field_index)) |
| 228 | { |
| 229 | switch(f->field_index) |
| 230 | { |
| 231 | case 0: /* HOST */ |
| 232 | case 1: /* USER */ |
| 233 | case 2: /* ROLE */ |
| 234 | return HA_ERR_WRONG_COMMAND; |
| 235 | break; |
| 236 | default: |
| 237 | DBUG_ASSERT(false); |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | int table_setup_actors::delete_row_values(TABLE *table, |
| 246 | const unsigned char *buf, |
| 247 | Field **fields) |
| 248 | { |
| 249 | DBUG_ASSERT(m_row_exists); |
| 250 | |
| 251 | CHARSET_INFO *cs= &my_charset_utf8_bin; |
| 252 | String user(m_row.m_username, m_row.m_username_length, cs); |
| 253 | String role(m_row.m_rolename, m_row.m_rolename_length, cs); |
| 254 | String host(m_row.m_hostname, m_row.m_hostname_length, cs); |
| 255 | |
| 256 | return delete_setup_actor(&user, &host, &role); |
| 257 | } |
| 258 | |
| 259 | |