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