| 1 | /* Copyright (c) 2008, 2010, 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_file_instances.cc |
| 18 | Table FILE_INSTANCES (implementation). |
| 19 | */ |
| 20 | |
| 21 | #include "my_global.h" |
| 22 | #include "my_pthread.h" |
| 23 | #include "pfs_instr.h" |
| 24 | #include "pfs_column_types.h" |
| 25 | #include "pfs_column_values.h" |
| 26 | #include "table_file_instances.h" |
| 27 | #include "pfs_global.h" |
| 28 | |
| 29 | THR_LOCK table_file_instances::m_table_lock; |
| 30 | |
| 31 | PFS_engine_table_share |
| 32 | table_file_instances::m_share= |
| 33 | { |
| 34 | { C_STRING_WITH_LEN("file_instances" ) }, |
| 35 | &pfs_readonly_acl, |
| 36 | &table_file_instances::create, |
| 37 | NULL, /* write_row */ |
| 38 | NULL, /* delete_all_rows */ |
| 39 | NULL, /* get_row_count */ |
| 40 | 1000, /* records */ |
| 41 | sizeof(PFS_simple_index), |
| 42 | &m_table_lock, |
| 43 | { C_STRING_WITH_LEN("CREATE TABLE file_instances(" |
| 44 | "FILE_NAME VARCHAR(512) not null," |
| 45 | "EVENT_NAME VARCHAR(128) not null," |
| 46 | "OPEN_COUNT INTEGER unsigned not null)" ) } |
| 47 | }; |
| 48 | |
| 49 | PFS_engine_table* table_file_instances::create(void) |
| 50 | { |
| 51 | return new table_file_instances(); |
| 52 | } |
| 53 | |
| 54 | table_file_instances::table_file_instances() |
| 55 | : PFS_engine_table(&m_share, &m_pos), |
| 56 | m_row_exists(false), m_pos(0), m_next_pos(0) |
| 57 | {} |
| 58 | |
| 59 | void table_file_instances::reset_position(void) |
| 60 | { |
| 61 | m_pos.m_index= 0; |
| 62 | m_next_pos.m_index= 0; |
| 63 | } |
| 64 | |
| 65 | int table_file_instances::rnd_next(void) |
| 66 | { |
| 67 | PFS_file *pfs; |
| 68 | |
| 69 | for (m_pos.set_at(&m_next_pos); |
| 70 | m_pos.m_index < file_max; |
| 71 | m_pos.next()) |
| 72 | { |
| 73 | pfs= &file_array[m_pos.m_index]; |
| 74 | if (pfs->m_lock.is_populated()) |
| 75 | { |
| 76 | make_row(pfs); |
| 77 | m_next_pos.set_after(&m_pos); |
| 78 | return 0; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return HA_ERR_END_OF_FILE; |
| 83 | } |
| 84 | |
| 85 | int table_file_instances::rnd_pos(const void *pos) |
| 86 | { |
| 87 | PFS_file *pfs; |
| 88 | |
| 89 | set_position(pos); |
| 90 | DBUG_ASSERT(m_pos.m_index < file_max); |
| 91 | pfs= &file_array[m_pos.m_index]; |
| 92 | |
| 93 | if (! pfs->m_lock.is_populated()) |
| 94 | return HA_ERR_RECORD_DELETED; |
| 95 | |
| 96 | make_row(pfs); |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | void table_file_instances::make_row(PFS_file *pfs) |
| 101 | { |
| 102 | pfs_lock lock; |
| 103 | PFS_file_class *safe_class; |
| 104 | |
| 105 | m_row_exists= false; |
| 106 | |
| 107 | /* Protect this reader against a file delete */ |
| 108 | pfs->m_lock.begin_optimistic_lock(&lock); |
| 109 | |
| 110 | safe_class= sanitize_file_class(pfs->m_class); |
| 111 | if (unlikely(safe_class == NULL)) |
| 112 | return; |
| 113 | |
| 114 | m_row.m_filename= pfs->m_filename; |
| 115 | m_row.m_filename_length= pfs->m_filename_length; |
| 116 | m_row.m_event_name= safe_class->m_name; |
| 117 | m_row.m_event_name_length= safe_class->m_name_length; |
| 118 | m_row.m_open_count= pfs->m_file_stat.m_open_count; |
| 119 | |
| 120 | if (pfs->m_lock.end_optimistic_lock(&lock)) |
| 121 | m_row_exists= true; |
| 122 | } |
| 123 | |
| 124 | int table_file_instances::read_row_values(TABLE *table, |
| 125 | unsigned char *, |
| 126 | Field **fields, |
| 127 | bool read_all) |
| 128 | { |
| 129 | Field *f; |
| 130 | |
| 131 | if (unlikely(! m_row_exists)) |
| 132 | return HA_ERR_RECORD_DELETED; |
| 133 | |
| 134 | /* Set the null bits */ |
| 135 | DBUG_ASSERT(table->s->null_bytes == 0); |
| 136 | |
| 137 | for (; (f= *fields) ; fields++) |
| 138 | { |
| 139 | if (read_all || bitmap_is_set(table->read_set, f->field_index)) |
| 140 | { |
| 141 | switch(f->field_index) |
| 142 | { |
| 143 | case 0: /* FILENAME */ |
| 144 | set_field_varchar_utf8(f, m_row.m_filename, m_row.m_filename_length); |
| 145 | break; |
| 146 | case 1: /* EVENT_NAME */ |
| 147 | set_field_varchar_utf8(f, m_row.m_event_name, |
| 148 | m_row.m_event_name_length); |
| 149 | break; |
| 150 | case 2: /* OPEN_COUNT */ |
| 151 | set_field_ulong(f, m_row.m_open_count); |
| 152 | break; |
| 153 | default: |
| 154 | DBUG_ASSERT(false); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | |