1 | /* Copyright (c) 2011, 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 | #include "my_global.h" |
17 | #include "my_pthread.h" |
18 | #include "table_users.h" |
19 | #include "pfs_instr_class.h" |
20 | #include "pfs_instr.h" |
21 | #include "pfs_account.h" |
22 | #include "pfs_user.h" |
23 | #include "pfs_visitor.h" |
24 | |
25 | THR_LOCK table_users::m_table_lock; |
26 | |
27 | PFS_engine_table_share |
28 | table_users::m_share= |
29 | { |
30 | { C_STRING_WITH_LEN("users" ) }, |
31 | &pfs_truncatable_acl, |
32 | &table_users::create, |
33 | NULL, /* write_row */ |
34 | table_users::delete_all_rows, |
35 | NULL, /* get_row_count */ |
36 | 1000, /* records */ |
37 | sizeof(PFS_simple_index), /* ref length */ |
38 | &m_table_lock, |
39 | { C_STRING_WITH_LEN("CREATE TABLE users(" |
40 | "USER CHAR(16) collate utf8_bin default null," |
41 | "CURRENT_CONNECTIONS bigint not null," |
42 | "TOTAL_CONNECTIONS bigint not null)" ) } |
43 | }; |
44 | |
45 | PFS_engine_table* table_users::create() |
46 | { |
47 | return new table_users(); |
48 | } |
49 | |
50 | int |
51 | table_users::delete_all_rows(void) |
52 | { |
53 | reset_events_waits_by_thread(); |
54 | reset_events_waits_by_account(); |
55 | reset_events_waits_by_user(); |
56 | reset_events_stages_by_thread(); |
57 | reset_events_stages_by_account(); |
58 | reset_events_stages_by_user(); |
59 | reset_events_statements_by_thread(); |
60 | reset_events_statements_by_account(); |
61 | reset_events_statements_by_user(); |
62 | purge_all_account(); |
63 | purge_all_user(); |
64 | return 0; |
65 | } |
66 | |
67 | table_users::table_users() |
68 | : cursor_by_user(& m_share), |
69 | m_row_exists(false) |
70 | {} |
71 | |
72 | void table_users::make_row(PFS_user *pfs) |
73 | { |
74 | pfs_lock lock; |
75 | |
76 | m_row_exists= false; |
77 | pfs->m_lock.begin_optimistic_lock(&lock); |
78 | |
79 | if (m_row.m_user.make_row(pfs)) |
80 | return; |
81 | |
82 | PFS_connection_stat_visitor visitor; |
83 | PFS_connection_iterator::visit_user(pfs, true, true, & visitor); |
84 | |
85 | if (! pfs->m_lock.end_optimistic_lock(& lock)) |
86 | return; |
87 | |
88 | m_row.m_connection_stat.set(& visitor.m_stat); |
89 | m_row_exists= true; |
90 | } |
91 | |
92 | int table_users::read_row_values(TABLE *table, |
93 | unsigned char *buf, |
94 | Field **fields, |
95 | bool read_all) |
96 | { |
97 | Field *f; |
98 | |
99 | if (unlikely(! m_row_exists)) |
100 | return HA_ERR_RECORD_DELETED; |
101 | |
102 | /* Set the null bits */ |
103 | DBUG_ASSERT(table->s->null_bytes == 1); |
104 | buf[0]= 0; |
105 | |
106 | for (; (f= *fields) ; fields++) |
107 | { |
108 | if (read_all || bitmap_is_set(table->read_set, f->field_index)) |
109 | { |
110 | switch(f->field_index) |
111 | { |
112 | case 0: /* USER */ |
113 | m_row.m_user.set_field(f); |
114 | break; |
115 | case 1: /* CURRENT_CONNECTIONS */ |
116 | case 2: /* TOTAL_CONNECTIONS */ |
117 | m_row.m_connection_stat.set_field(f->field_index - 1, f); |
118 | break; |
119 | default: |
120 | DBUG_ASSERT(false); |
121 | } |
122 | } |
123 | } |
124 | return 0; |
125 | } |
126 | |
127 | |