-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathpfs_example_employee_salary.cc
316 lines (262 loc) · 10.2 KB
/
pfs_example_employee_salary.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* Copyright (c) 2017, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include "plugin/pfs_table_plugin/pfs_example_employee_salary.h"
#include "mysql/psi/mysql_mutex.h"
PFS_engine_table_share_proxy esalary_st_share;
mysql_mutex_t LOCK_esalary_records_array;
/* Total number of rows in table. */
unsigned int esalary_rows_in_table = 0;
std::vector<Esalary_Record> esalary_records_vector;
/**
* Instantiate Esalary_Table_Handle at plugin code when corresponding table
* in performance schema is opened.
*/
PSI_table_handle *esalary_open_table(PSI_pos **pos) {
auto *temp = new Esalary_Table_Handle();
temp->current_row.e_number.is_null = true;
temp->current_row.e_salary.is_null = true;
temp->current_row.e_dob_length = 0;
temp->current_row.e_tob_length = 0;
*pos = (PSI_pos *)(&temp->m_pos);
return (PSI_table_handle *)temp;
}
/**
* Destroy the Esalary_Table_Handle at plugin code when corresponding table
* in performance schema is closed.
*/
void esalary_close_table(PSI_table_handle *handle) {
auto *temp = (Esalary_Table_Handle *)handle;
delete temp;
}
static void copy_record(Esalary_Record *dest, Esalary_Record *source) {
dest->e_number = source->e_number;
dest->e_salary = source->e_salary;
dest->e_dob_length = source->e_dob_length;
strncpy(dest->e_dob, source->e_dob, dest->e_dob_length);
dest->e_tob_length = source->e_tob_length;
strncpy(dest->e_tob, source->e_tob, dest->e_tob_length);
dest->m_exist = source->m_exist;
}
/* Define implementation of PFS_engine_table_proxy. */
int esalary_rnd_next(PSI_table_handle *handle) {
auto *h = (Esalary_Table_Handle *)handle;
for (h->m_pos.set_at(&h->m_next_pos); h->m_pos.has_more(); h->m_pos.next()) {
Esalary_Record *record = &esalary_records_vector.at(h->m_pos.get_index());
if (record->m_exist) {
/* Make the current row from records_array buffer */
copy_record(&h->current_row, record);
h->m_next_pos.set_after(&h->m_pos);
return 0;
}
}
return PFS_HA_ERR_END_OF_FILE;
}
int esalary_rnd_init(PSI_table_handle *h [[maybe_unused]],
bool scan [[maybe_unused]]) {
return 0;
}
int esalary_rnd_pos(PSI_table_handle *handle) {
auto *h = (Esalary_Table_Handle *)handle;
Esalary_Record *record = &esalary_records_vector[h->m_pos.get_index()];
if (record->m_exist) {
/* Make the current row from records_array buffer */
copy_record(&h->current_row, record);
}
return 0;
}
/* Initialize the table index */
int esalary_index_init(PSI_table_handle *handle [[maybe_unused]],
uint idx [[maybe_unused]], bool sorted [[maybe_unused]],
PSI_index_handle **index [[maybe_unused]]) {
/* Do nothing as there are no index */
return 0;
}
/* For each key in index, read value specified in query */
int esalary_index_read(PSI_index_handle *index [[maybe_unused]],
PSI_key_reader *reader [[maybe_unused]],
unsigned int idx [[maybe_unused]],
int find_flag [[maybe_unused]]) {
/* Do nothing as there are no index */
return 0;
}
/* Read the next indexed value */
int esalary_index_next(PSI_table_handle *handle [[maybe_unused]]) {
/* Do nothing as there are no index */
return PFS_HA_ERR_END_OF_FILE;
}
/* Reset cursor position */
void esalary_reset_position(PSI_table_handle *handle) {
auto *h = (Esalary_Table_Handle *)handle;
h->m_pos.reset();
h->m_next_pos.reset();
}
/* Read current row from the current_row and display them in the table */
int esalary_read_column_value(PSI_table_handle *handle, PSI_field *field,
uint index) {
auto *h = (Esalary_Table_Handle *)handle;
switch (index) {
case 0: /* EMPLOYEE_NUMBER */
col_int_svc->set(field, h->current_row.e_number);
break;
case 1: /* EMPLOYEE_SALARY */
col_bigint_svc->set(field, h->current_row.e_salary);
break;
case 2: /* DATE_OF_BIRTH */
col_date_svc->set(field, h->current_row.e_dob,
h->current_row.e_dob_length);
break;
case 3: /* TIME_OF_BIRTH */
col_time_svc->set(field, h->current_row.e_tob,
h->current_row.e_tob_length);
break;
default: /* We should never reach here */
assert(0);
break;
}
return 0;
}
/* Store row data into records array */
int esalary_write_row_values(PSI_table_handle *handle) {
auto *h = (Esalary_Table_Handle *)handle;
bool found = false;
mysql_mutex_lock(&LOCK_esalary_records_array);
h->current_row.m_exist = true;
int const size = esalary_records_vector.size();
for (int i = 0; i < size; i++) {
Esalary_Record *record = &esalary_records_vector.at(i);
if (!record->m_exist) {
copy_record(record, &h->current_row);
found = true;
break;
}
}
if (!found) esalary_records_vector.push_back(h->current_row);
esalary_rows_in_table++;
mysql_mutex_unlock(&LOCK_esalary_records_array);
return 0;
}
/* Read field data from Field and store that into buffer */
int esalary_write_column_value(PSI_table_handle *handle, PSI_field *field,
unsigned int index) {
auto *h = (Esalary_Table_Handle *)handle;
char *dob_val = &h->current_row.e_dob[0];
unsigned int *dob_len = &h->current_row.e_dob_length;
char *tob_val = &h->current_row.e_tob[0];
unsigned int *tob_len = &h->current_row.e_tob_length;
switch (index) {
case 0: /* EMPLOYEE_NUMBER */
col_int_svc->get(field, &h->current_row.e_number);
break;
case 1: /* EMPLOYEE_SALARY */
col_bigint_svc->get(field, &h->current_row.e_salary);
break;
case 2: /* DATE_OF_BIRTH */
col_date_svc->get(field, dob_val, dob_len);
break;
case 3: /* TIME_OF_BIRTH */
col_time_svc->get(field, tob_val, tob_len);
break;
default: /* We should never reach here */
assert(0);
break;
}
return 0;
}
/* Update row data in records array */
int esalary_update_row_values(PSI_table_handle *handle) {
auto *h = (Esalary_Table_Handle *)handle;
Esalary_Record *cur = &esalary_records_vector[h->m_pos.get_index()];
assert(cur->m_exist == true);
mysql_mutex_lock(&LOCK_esalary_records_array);
copy_record(cur, &h->current_row);
mysql_mutex_unlock(&LOCK_esalary_records_array);
return 0;
}
int esalary_update_column_value(PSI_table_handle *handle, PSI_field *field,
unsigned int index) {
auto *h = (Esalary_Table_Handle *)handle;
char *dob_val = &h->current_row.e_dob[0];
unsigned int *dob_len = &h->current_row.e_dob_length;
char *tob_val = &h->current_row.e_tob[0];
unsigned int *tob_len = &h->current_row.e_tob_length;
switch (index) {
case 0: /* EMPLOYEE_NUMBER */
col_int_svc->get(field, &h->current_row.e_number);
break;
case 1: /* EMPLOYEE_SALARY */
col_bigint_svc->get(field, &h->current_row.e_salary);
break;
case 2: /* DATE_OF_BIRTH */
col_date_svc->get(field, dob_val, dob_len);
break;
case 3: /* TIME_OF_BIRTH */
col_time_svc->get(field, tob_val, tob_len);
break;
default: /* We should never reach here */
assert(0);
break;
}
return 0;
}
/* Delete row data from records array */
int esalary_delete_row_values(PSI_table_handle *handle) {
auto *h = (Esalary_Table_Handle *)handle;
Esalary_Record *cur = &esalary_records_vector.at(h->m_pos.get_index());
assert(cur->m_exist == true);
mysql_mutex_lock(&LOCK_esalary_records_array);
cur->m_exist = false;
esalary_rows_in_table--;
mysql_mutex_unlock(&LOCK_esalary_records_array);
return 0;
}
int esalary_delete_all_rows() {
mysql_mutex_lock(&LOCK_esalary_records_array);
esalary_records_vector.clear();
esalary_rows_in_table = 0;
mysql_mutex_unlock(&LOCK_esalary_records_array);
return 0;
}
unsigned long long esalary_get_row_count() { return esalary_rows_in_table; }
void init_esalary_share(PFS_engine_table_share_proxy *share) {
share->m_table_name = "pfs_example_employee_salary";
share->m_table_name_length = 27;
share->m_table_definition =
"EMPLOYEE_NUMBER INTEGER, EMPLOYEE_SALARY BIGINT, DATE_OF_BIRTH DATE, "
"TIME_OF_BIRTH TIME";
share->m_ref_length = sizeof(Esalary_POS);
share->m_acl = EDITABLE;
share->get_row_count = esalary_get_row_count;
share->delete_all_rows = esalary_delete_all_rows;
/* Initialize PFS_engine_table_proxy */
share->m_proxy_engine_table = {esalary_rnd_next,
esalary_rnd_init,
esalary_rnd_pos,
esalary_index_init,
esalary_index_read,
esalary_index_next,
esalary_read_column_value,
esalary_reset_position,
esalary_write_column_value,
esalary_write_row_values,
esalary_update_column_value,
esalary_update_row_values,
esalary_delete_row_values,
esalary_open_table,
esalary_close_table};
}