1 /* Copyright (c) 2020, 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, version 2.0,
5   as published by the Free Software Foundation.
6 
7   This program is also distributed with certain software (including
8   but not limited to OpenSSL) that is licensed under separate terms,
9   as designated in a particular file or component or in included license
10   documentation.  The authors of MySQL hereby grant you an additional
11   permission to link the program and your derivative works with the
12   separately licensed software that they have included with MySQL.
13 
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License, version 2.0, for more details.
18 
19   You should have received a copy of the GNU General Public License
20   along with this program; if not, write to the Free Software
21   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 // Implements
24 #include "storage/ndb/plugin/ndb_sync_pending_objects_table.h"
25 
26 #include <cstring>  // std::strlen
27 
28 #include "my_dbug.h"                                  // DBUG_ASSERT
29 #include "storage/ndb/plugin/ha_ndbcluster_binlog.h"  // ndbcluster_binlog_retrieve_sync_pending_objects
30 
ndb_pending_objects_row_count()31 static unsigned long long ndb_pending_objects_row_count() {
32   // Retrieve row count from the metadata sync implementation
33   return ndbcluster_binlog_get_sync_pending_objects_count();
34 }
35 
ndb_pending_objects_open_table(PSI_pos ** pos)36 static PSI_table_handle *ndb_pending_objects_open_table(PSI_pos **pos) {
37   // Constructs a table object and returns an opaque pointer
38   auto row_pos = reinterpret_cast<uint32_t **>(pos);
39   /*
40     Creates an instance of the table. Note that this is deallocated during the
41     table close which is implemented in the base class. See the
42     ndb_pfs_close_table() function in ndb_pfs_table.cc
43   */
44   Ndb_sync_pending_objects_table *table = new Ndb_sync_pending_objects_table();
45   *row_pos = table->get_position_address();
46   PSI_table_handle *handle = reinterpret_cast<PSI_table_handle *>(table);
47   return handle;
48 }
49 
Ndb_sync_pending_objects_table_share()50 Ndb_sync_pending_objects_table_share::Ndb_sync_pending_objects_table_share()
51     : Ndb_pfs_table_share() {
52   m_table_name = "ndb_sync_pending_objects";
53   m_table_name_length = std::strlen(m_table_name);
54   m_table_definition =
55       "`SCHEMA_NAME` varchar(64),"
56       "`NAME` varchar(64),"
57       "`TYPE` enum('LOGFILE GROUP', 'TABLESPACE', 'SCHEMA', 'TABLE') NOT NULL";
58   get_row_count = ndb_pending_objects_row_count;
59 
60   m_proxy_engine_table.open_table = ndb_pending_objects_open_table;
61 }
62 
rnd_init()63 int Ndb_sync_pending_objects_table::rnd_init() {
64   // Retrieve information and store it in m_pending_objects
65   ndbcluster_binlog_retrieve_sync_pending_objects(this);
66   set_num_rows(m_pending_objects.size());
67   reset_pos();
68   return 0;
69 }
70 
71 extern SERVICE_TYPE_NO_CONST(pfs_plugin_column_string_v1) * pfscol_string;
72 extern SERVICE_TYPE_NO_CONST(pfs_plugin_column_enum_v1) * pfscol_enum;
73 
read_column_value(PSI_field * field,uint32_t index)74 int Ndb_sync_pending_objects_table::read_column_value(PSI_field *field,
75                                                       uint32_t index) {
76   DBUG_ASSERT(!is_empty() && rows_pending_read());
77   PSI_ulonglong bigint_value;
78 
79   const unsigned int row_index = get_position();
80   const Pending_object &obj = m_pending_objects[row_index - 1];
81 
82   switch (index) {
83     case 0: /* SCHEMA_NAME: Name of the schema */
84       pfscol_string->set_varchar_utf8(
85           field, obj.m_schema_name == "" ? nullptr : obj.m_schema_name.c_str());
86       break;
87     case 1: /* NAME: Object name */
88       pfscol_string->set_varchar_utf8(
89           field, obj.m_name == "" ? nullptr : obj.m_name.c_str());
90       break;
91     case 2: /* TYPE */
92       // type + 1 since index 0 is used for empty strings in enum
93       bigint_value.val = obj.m_type + 1;
94       bigint_value.is_null = false;
95       pfscol_enum->set(field, bigint_value);
96       break;
97     default:
98       DBUG_ASSERT(false);
99   }
100   return 0;
101 }
102 
close()103 void Ndb_sync_pending_objects_table::close() {
104   m_pending_objects.clear();
105   reset_pos();
106 }
107 
108 // Instantiate the table share
109 Ndb_sync_pending_objects_table_share pending_objects_table_share;
110 PFS_engine_table_share_proxy *ndb_sync_pending_objects_share =
111     &pending_objects_table_share;
112