|
| 1 | +/* Copyright (c) 2017, 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 |
| 14 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 15 | + |
| 16 | +#include <cstring> |
| 17 | +#include "pfs_example_component_population.h" |
| 18 | +#include "pfs_example_continent.h" |
| 19 | +#include "pfs_example_country.h" |
| 20 | +#include "my_sys.h" |
| 21 | + |
| 22 | +/** |
| 23 | + @page EXAMPLE_COMPONENT An example component |
| 24 | +
|
| 25 | + Component Name : pfs_example_component_population \n |
| 26 | + Source location : components/pfs_component |
| 27 | +
|
| 28 | + This file contains a definition of the pfs_example_component_population. |
| 29 | +*/ |
| 30 | + |
| 31 | +/* clang-format off */ |
| 32 | +/* Records to be inserted into pfs_example_continent table from component code */ |
| 33 | +Continent_record continent_array[] = |
| 34 | +{ |
| 35 | + {"bar1", 4, true}, |
| 36 | + {"bar2", 4, true} |
| 37 | +}; |
| 38 | + |
| 39 | +/* Records to be inserted into pfs_example_country table from component code */ |
| 40 | +Country_record country_array[] = |
| 41 | +{ |
| 42 | + {"foo1", 4, "bar1", 4, {2016, false}, {10000, false}, {1.11, false}, true}, |
| 43 | + {"foo2", 4, "bar2", 4, {2016, false}, {1000, false}, {2.22, false}, true} |
| 44 | +}; |
| 45 | +/* clang-format on */ |
| 46 | + |
| 47 | +#define MAX_BUFFER_LENGTH 80 |
| 48 | + |
| 49 | +#define WRITE_LOG(lit_log_text) \ |
| 50 | + if (outfile) \ |
| 51 | + { \ |
| 52 | + strcpy (log_text, lit_log_text); \ |
| 53 | + fwrite((uchar*)log_text, sizeof(char), strlen(log_text), outfile); \ |
| 54 | + } |
| 55 | + |
| 56 | +/* Log file */ |
| 57 | +FILE *outfile = NULL; |
| 58 | +const char *filename= "pfs_example_component_population.log"; |
| 59 | +char log_text[MAX_BUFFER_LENGTH] = {'\0'}; |
| 60 | + |
| 61 | +/* Collection of table shares to be added to performance schema */ |
| 62 | +PFS_engine_table_share_proxy *share_list[2] = {NULL, NULL}; |
| 63 | +unsigned int share_list_count = 2; |
| 64 | + |
| 65 | +/* Prepare and insert rows in pfs_example_continent table */ |
| 66 | +int |
| 67 | +continent_prepare_insert_row() |
| 68 | +{ |
| 69 | + int result = 0; |
| 70 | + Continent_Table_Handle handle; |
| 71 | + int array_size = sizeof(continent_array) / sizeof(continent_array[0]); |
| 72 | + |
| 73 | + for (int i = 0; i < array_size; i++) |
| 74 | + { |
| 75 | + /* Prepare a sample row to be inserted from here */ |
| 76 | + strncpy(handle.current_row.name, |
| 77 | + continent_array[i].name, |
| 78 | + continent_array[i].name_length); |
| 79 | + handle.current_row.name_length = continent_array[i].name_length; |
| 80 | + handle.current_row.m_exist = continent_array[i].m_exist; |
| 81 | + |
| 82 | + /* Insert a row in the table to be added */ |
| 83 | + result = write_rows_from_component(&handle); |
| 84 | + if (result) |
| 85 | + break; |
| 86 | + } |
| 87 | + |
| 88 | + return result; |
| 89 | +} |
| 90 | + |
| 91 | +/* Prepare and insert rows in pfs_example_country table */ |
| 92 | +int |
| 93 | +country_prepare_insert_row() |
| 94 | +{ |
| 95 | + int result = 0; |
| 96 | + Country_Table_Handle handle; |
| 97 | + int array_size = sizeof(country_array) / sizeof(country_array[0]); |
| 98 | + |
| 99 | + for (int i = 0; i < array_size; i++) |
| 100 | + { |
| 101 | + /* Prepare a sample row to be inserted from here */ |
| 102 | + strncpy(handle.current_row.name, |
| 103 | + country_array[i].name, |
| 104 | + country_array[i].name_length); |
| 105 | + handle.current_row.name_length = country_array[i].name_length; |
| 106 | + strncpy(handle.current_row.continent_name, |
| 107 | + country_array[i].continent_name, |
| 108 | + country_array[i].continent_name_length); |
| 109 | + handle.current_row.continent_name_length = |
| 110 | + country_array[i].continent_name_length; |
| 111 | + handle.current_row.year = country_array[i].year; |
| 112 | + handle.current_row.population = country_array[i].population; |
| 113 | + handle.current_row.growth_factor = country_array[i].growth_factor; |
| 114 | + handle.current_row.m_exist = country_array[i].m_exist; |
| 115 | + |
| 116 | + /* Insert a row in the table to be added */ |
| 117 | + result = country_write_row_values((PSI_table_handle *)&handle); |
| 118 | + if (result) |
| 119 | + break; |
| 120 | + } |
| 121 | + |
| 122 | + return result; |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | +* Initialize the pfs_example_component_population at server start or |
| 127 | +* component installation. |
| 128 | +* |
| 129 | +* - Instantiate and initialize PFS_engine_table_share_proxy. |
| 130 | +* - Prepare and insert rows in tables from here. |
| 131 | +* - Call add_table method of pfs_plugin_table service. |
| 132 | +* |
| 133 | +* @retval 0 success |
| 134 | +* @retval non-zero failure |
| 135 | +*/ |
| 136 | + |
| 137 | +mysql_service_status_t |
| 138 | +pfs_example_component_population_init() |
| 139 | +{ |
| 140 | + mysql_service_status_t result = 0; |
| 141 | + /* If fopen fails, outfile will be NULL so there will be no write |
| 142 | + in WRITE_LOG |
| 143 | + */ |
| 144 | + outfile= fopen(filename, "w+"); |
| 145 | + |
| 146 | + WRITE_LOG("pfs_example_component_population init:\n"); |
| 147 | + |
| 148 | + /* Initialize mutex */ |
| 149 | + native_mutex_init(&LOCK_continent_records_array, NULL); |
| 150 | + native_mutex_init(&LOCK_country_records_array, NULL); |
| 151 | + |
| 152 | + /* Instantiate and initialize PFS_engine_table_share_proxy */ |
| 153 | + init_continent_share(&continent_st_share); |
| 154 | + init_country_share(&country_st_share); |
| 155 | + |
| 156 | + /* From here, prepare rows for tables and insert */ |
| 157 | + if (continent_prepare_insert_row() || country_prepare_insert_row()) |
| 158 | + { |
| 159 | + WRITE_LOG("Error returned from prepare_insert_row()\n"); |
| 160 | + result = true; |
| 161 | + goto error; |
| 162 | + } |
| 163 | + |
| 164 | + /* Prepare the shares list to be passed to the service call */ |
| 165 | + share_list[0] = &continent_st_share; |
| 166 | + share_list[1] = &country_st_share; |
| 167 | + |
| 168 | + /** |
| 169 | + * Call add_table function of pfs_plugin_table service to |
| 170 | + * add component tables in performance schema. |
| 171 | + */ |
| 172 | + if (mysql_service_pfs_plugin_table->add_tables(&share_list[0], |
| 173 | + share_list_count)) |
| 174 | + { |
| 175 | + WRITE_LOG("Error returned from add_tables()\n"); |
| 176 | + result = true; |
| 177 | + goto error; |
| 178 | + } |
| 179 | + else |
| 180 | + { |
| 181 | + WRITE_LOG ("Passed add_tables()\n"); |
| 182 | + } |
| 183 | + |
| 184 | +error: |
| 185 | + WRITE_LOG ("End of init\n\n"); |
| 186 | + fclose(outfile); |
| 187 | + if (result) |
| 188 | + { |
| 189 | + /* Destroy mutexes */ |
| 190 | + native_mutex_destroy(&LOCK_continent_records_array); |
| 191 | + native_mutex_destroy(&LOCK_country_records_array); |
| 192 | + } |
| 193 | + return result; |
| 194 | +} |
| 195 | + |
| 196 | +/** |
| 197 | +* Terminate the pfs_example_component_population at server shutdown or |
| 198 | +* component deinstallation. |
| 199 | +* |
| 200 | +* - Delete/Drop component tables from Performance Schema. |
| 201 | +* |
| 202 | +* @retval 0 success |
| 203 | +* @retval non-zero failure |
| 204 | +*/ |
| 205 | +mysql_service_status_t |
| 206 | +pfs_example_component_population_deinit() |
| 207 | +{ |
| 208 | + mysql_service_status_t result = 0; |
| 209 | + /* If fopen fails, outfile will be NULL so there will be no write |
| 210 | + in WRITE_LOG |
| 211 | + */ |
| 212 | + outfile= fopen(filename, "a+"); |
| 213 | + |
| 214 | + WRITE_LOG("pfs_example_component_population_deinit:\n"); |
| 215 | + |
| 216 | + /** |
| 217 | + * Call delete_tables function of pfs_plugin_table service to |
| 218 | + * delete component tables from performance schema |
| 219 | + */ |
| 220 | + if (mysql_service_pfs_plugin_table->delete_tables(&share_list[0], |
| 221 | + share_list_count)) |
| 222 | + { |
| 223 | + WRITE_LOG("Error returned from delete_table()\n"); |
| 224 | + result = 1; |
| 225 | + goto error; |
| 226 | + } |
| 227 | + else |
| 228 | + { |
| 229 | + WRITE_LOG ("Passed delete_tables()\n"); |
| 230 | + } |
| 231 | + |
| 232 | +error: |
| 233 | + if (!result) |
| 234 | + { |
| 235 | + /* Destroy mutexes */ |
| 236 | + native_mutex_destroy(&LOCK_continent_records_array); |
| 237 | + native_mutex_destroy(&LOCK_country_records_array); |
| 238 | + } |
| 239 | + |
| 240 | + WRITE_LOG ("End of deinit\n\n"); |
| 241 | + fclose(outfile); |
| 242 | + return result; |
| 243 | +} |
| 244 | + |
| 245 | +/* pfs_example_component_population doesn't provide any service */ |
| 246 | +BEGIN_COMPONENT_PROVIDES(pfs_example_component_population) |
| 247 | +END_COMPONENT_PROVIDES() |
| 248 | + |
| 249 | +/* pfs_example_component requires/uses pfs_plugin_table service */ |
| 250 | +REQUIRES_SERVICE_PLACEHOLDER(pfs_plugin_table); |
| 251 | + |
| 252 | +BEGIN_COMPONENT_REQUIRES(pfs_example_component_population) |
| 253 | + REQUIRES_SERVICE(pfs_plugin_table) |
| 254 | +END_COMPONENT_REQUIRES() |
| 255 | + |
| 256 | +/* A list of metadata to describe the Component. */ |
| 257 | +BEGIN_COMPONENT_METADATA(pfs_example_component_population) |
| 258 | + METADATA("mysql.author", "Oracle Corporation") |
| 259 | + METADATA("mysql.license", "GPL") |
| 260 | + METADATA("test_property", "1") |
| 261 | +END_COMPONENT_METADATA() |
| 262 | + |
| 263 | +/* Declaration of the Component. */ |
| 264 | +DECLARE_COMPONENT(pfs_example_component_population, "mysql:pfs_example_component_population") |
| 265 | + pfs_example_component_population_init, |
| 266 | + pfs_example_component_population_deinit |
| 267 | +END_DECLARE_COMPONENT() |
| 268 | + |
| 269 | +/* Defines list of Components contained in this library. Note that for now we |
| 270 | + assume that library will have exactly one Component. */ |
| 271 | +DECLARE_LIBRARY_COMPONENTS |
| 272 | + &COMPONENT_REF(pfs_example_component_population) |
| 273 | +END_DECLARE_LIBRARY_COMPONENTS |
0 commit comments