File: rpl_sys_table_access.cc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (403 lines) | stat: -rw-r--r-- 11,961 bytes parent folder | download
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/* Copyright (c) 2020, 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 "sql/rpl_sys_table_access.h"

#include <stddef.h>
#include <sstream>  // std::ostringstream

#include "my_dbug.h"
#include "sql-common/json_dom.h"  // Json_wrapper
#include "sql/current_thd.h"
#include "sql/log.h"
#include "sql/rpl_sys_key_access.h"
#include "sql/sql_base.h"
#include "sql/sql_class.h"
#include "sql/sql_lex.h"
#include "sql/sql_parse.h"
#include "sql/table.h"
#include "sql/transaction.h"

Rpl_sys_table_access::Rpl_sys_table_access(const std::string &schema_name,
                                           const std::string &table_name,
                                           uint max_num_field)
    : m_lock_type(TL_READ),
      m_schema_name(schema_name),
      m_table_name(table_name),
      m_max_num_field(max_num_field) {}

Rpl_sys_table_access::~Rpl_sys_table_access() { close(true); }

bool Rpl_sys_table_access::store_field(Field *field, std::string fld,
                                       CHARSET_INFO *cs) {
  DBUG_TRACE;
  field->set_notnull();
  return field->store(fld.c_str(), fld.length(), cs) != TYPE_OK;
}

bool Rpl_sys_table_access::store_field(Field *field, long long fld,
                                       bool unsigned_val) {
  DBUG_TRACE;
  field->set_notnull();
  return field->store(fld, unsigned_val) != TYPE_OK;
}

bool Rpl_sys_table_access::store_field(Field *field,
                                       const Json_wrapper &wrapper) {
  DBUG_TRACE;
  if (field) {
    field->set_notnull();
    Field_json *json_field = down_cast<Field_json *>(field);
    return json_field->store_json(&wrapper) != TYPE_OK;
  }
  return true;
}

bool Rpl_sys_table_access::get_field(Field *field, std::string &fld,
                                     CHARSET_INFO *cs) {
  DBUG_TRACE;

  char buff[MAX_FIELD_WIDTH];
  String buff_str(buff, sizeof(buff), cs);

  field->val_str(&buff_str);
  fld.assign(buff_str.c_ptr_safe(), buff_str.length());
  return false;
}

bool Rpl_sys_table_access::get_field(Field *field, uint &fld) {
  fld = (uint)field->val_int();
  return false;
}

bool Rpl_sys_table_access::get_field(Field *field, Json_wrapper &fld) {
  DBUG_TRACE;
  if (field && field->type() == MYSQL_TYPE_JSON) {
    Field_json *json_field = down_cast<Field_json *>(field);
    return json_field->val_json(&fld);
  }

  return true;
}

bool Rpl_sys_table_access::open(enum thr_lock_type lock_type) {
  assert(nullptr == m_thd);
  m_lock_type = lock_type;
  m_current_thd = current_thd;
  m_error = false;

  THD *thd = nullptr;
  thd = new THD;
  thd->thread_stack = (char *)&thd;
  thd->store_globals();
  thd->security_context()->skip_grants();
  thd->system_thread = SYSTEM_THREAD_BACKGROUND;
  thd->set_new_thread_id();
  thd->variables.option_bits &= ~OPTION_BIN_LOG;
  thd->variables.option_bits &= ~OPTION_AUTOCOMMIT;
  thd->variables.option_bits |= OPTION_NOT_AUTOCOMMIT;
  thd->set_skip_readonly_check();
  m_thd = thd;

  // m_table_list[0] is m_schema_name.m_table_name
  // m_table_list[1] is m_schema_version_name.m_table_version_name
  m_table_list = new Table_ref[m_table_list_size];

  Table_ref *table_version = &m_table_list[m_table_version_index];
  *table_version =
      Table_ref(m_schema_version_name.c_str(), m_schema_version_name.length(),
                m_table_version_name.c_str(), m_table_version_name.length(),
                m_table_version_name.c_str(), m_lock_type);
  table_version->open_strategy = Table_ref::OPEN_IF_EXISTS;
  table_version->next_local = nullptr;
  table_version->next_global = nullptr;

  Table_ref *table_data = &m_table_list[m_table_data_index];
  *table_data = Table_ref(m_schema_name.c_str(), m_schema_name.length(),
                          m_table_name.c_str(), m_table_name.length(),
                          m_table_name.c_str(), m_lock_type);
  table_data->open_strategy = Table_ref::OPEN_IF_EXISTS;
  table_data->next_local = table_version;
  table_data->next_global = table_version;

  uint flags =
      (MYSQL_OPEN_IGNORE_GLOBAL_READ_LOCK | MYSQL_LOCK_IGNORE_GLOBAL_READ_ONLY |
       MYSQL_OPEN_IGNORE_FLUSH | MYSQL_LOCK_IGNORE_TIMEOUT);
  if (open_and_lock_tables(m_thd, m_table_list, flags)) {
    /* purecov: begin inspected */
    m_error = true;
    goto err;
    /* purecov: end */
  }

  if (table_data->table->s->fields < m_max_num_field) {
    /* purecov: begin inspected */
    m_error = true;
    goto err;
    /* purecov: end */
  }

  table_version->table->use_all_columns();
  table_data->table->use_all_columns();

err:
  if (m_error) {
    /* purecov: begin inspected */
    close(true);
    /* purecov: end */
  }

  return m_error;
}

bool Rpl_sys_table_access::close(bool error, bool ignore_global_read_lock) {
  DBUG_EXECUTE_IF("force_error_on_configuration_table_close", m_error = true;);

  if (!m_thd) return false;

  if (error || m_error) {
    trans_rollback_stmt(m_thd);
    trans_rollback(m_thd);
    m_error = true;
  } else {
    m_error = trans_commit_stmt(m_thd, ignore_global_read_lock) ||
              trans_commit(m_thd, ignore_global_read_lock);
  }

  close_thread_tables(m_thd);
  delete[] m_table_list;
  m_table_list = nullptr;

  m_thd->release_resources();
  delete m_thd;
  m_thd = nullptr;

  if (m_current_thd) m_current_thd->store_globals();
  m_current_thd = nullptr;

  m_lock_type = TL_READ;

  return m_error;
}

TABLE *Rpl_sys_table_access::get_table() {
  if (nullptr != m_table_list) {
    return m_table_list[m_table_data_index].table;
  }

  return nullptr; /* purecov: inspected */
}

void Rpl_sys_table_access::handler_write_row_func(
    Rpl_sys_table_access &table_op, bool &err_val, std::string &err_msg, uint,
    key_part_map) {
  TABLE *table = table_op.get_table();

  int error = table->file->ha_write_row(table->record[0]);
  if (error) {
    table->file->print_error(error, MYF(0));
    err_msg.assign("Error inserting row to the table.");
    err_val = true;
  }
}

void Rpl_sys_table_access::handler_delete_row_func(
    Rpl_sys_table_access &table_op, bool &err_val, std::string &err_msg,
    uint table_index, key_part_map keypart_map) {
  TABLE *table = table_op.get_table();
  Rpl_sys_key_access key_access;
  int error = 0;
  int key_error =
      key_access.init(table, table_index, true, keypart_map, HA_READ_KEY_EXACT);

  if (key_error == HA_ERR_KEY_NOT_FOUND) {
    err_msg.assign("Error no matching row was found to be deleted.");
    err_val = true;
  }

  if (!key_error) {
    do {
      error = table->file->ha_delete_row(table->record[0]);
    } while (!key_access.next() && !error);
  }

  if (error) {
    err_val = true;
    err_msg.assign("Error deleting row from the table.");
    table->file->print_error(error, MYF(0));
  }

  if (key_access.deinit()) {
    err_msg.assign("Error ending key access.");
    err_val = true;
  }
}

std::string Rpl_sys_table_access::get_field_error_msg(
    std::string field_name) const {
  std::ostringstream str_stream;
  str_stream << "Error saving " << field_name << " field of " << m_schema_name
             << "." << m_table_name << ".";
  return str_stream.str();
}

bool Rpl_sys_table_access::delete_all_rows() {
  bool error = false;

  TABLE *table = get_table();
  Rpl_sys_key_access key_access;
  int key_error =
      key_access.init(table, Rpl_sys_key_access::enum_key_type::INDEX_NEXT);
  if (!key_error) {
    do {
      error |= (table->file->ha_delete_row(table->record[0]) != 0);
      if (error) {
        return true;
      }
    } while (!error && !key_access.next());
  } else if (HA_ERR_END_OF_FILE == key_error) {
    /* Table is already empty, nothing to delete. */
  } else {
    return true;
  }

  return key_access.deinit();
}

bool Rpl_sys_table_access::increment_version() {
  DBUG_TRACE;
  assert(m_lock_type >= TL_WRITE_ALLOW_WRITE);
  longlong version = 0;

  TABLE *table_version = m_table_list[m_table_version_index].table;
  Field **fields = table_version->field;
  fields[0]->set_notnull();
  fields[0]->store(m_table_name.c_str(), m_table_name.length(),
                   &my_charset_bin);

  Rpl_sys_key_access key_access;
  int error = key_access.init(table_version);

  if (HA_ERR_KEY_NOT_FOUND == error) {
    /* purecov: begin inspected */
    error = 0;
    version = 1;
    /* purecov: end */
  } else if (error) {
    return true; /* purecov: inspected */
  } else {
    version = 1 + fields[1]->val_int();
    error |= table_version->file->ha_delete_row(table_version->record[0]);
  }

  if (!error) {
    fields[1]->set_notnull();
    fields[1]->store(version, true);
    error |= table_version->file->ha_write_row(table_version->record[0]);
  }

  error |= (key_access.deinit() != 0);

  return error;
}

bool Rpl_sys_table_access::update_version(ulonglong version) {
  DBUG_TRACE;
  assert(m_lock_type >= TL_WRITE_ALLOW_WRITE);
  assert(version > 0);

  TABLE *table_version = m_table_list[m_table_version_index].table;
  Field **fields = table_version->field;
  fields[0]->set_notnull();
  fields[0]->store(m_table_name.c_str(), m_table_name.length(),
                   &my_charset_bin);

  Rpl_sys_key_access key_access;
  int error = key_access.init(table_version);

  if (HA_ERR_KEY_NOT_FOUND == error) {
    error = 0; /* purecov: inspected */
  } else if (error) {
    return true; /* purecov: inspected */
  } else {
    error |= table_version->file->ha_delete_row(table_version->record[0]);
  }

  if (!error) {
    fields[1]->set_notnull();
    fields[1]->store(version, true);
    error |= table_version->file->ha_write_row(table_version->record[0]);
  }

  error |= (key_access.deinit() != 0);

  return error;
}

ulonglong Rpl_sys_table_access::get_version() {
  DBUG_TRACE;
  longlong version = 0;

  TABLE *table_version = m_table_list[m_table_version_index].table;
  Field **fields = table_version->field;
  fields[0]->set_notnull();
  fields[0]->store(m_table_name.c_str(), m_table_name.length(),
                   &my_charset_bin);

  Rpl_sys_key_access key_access;
  int error = key_access.init(table_version);

  if (!error) {
    version = fields[1]->val_int();
  }

  key_access.deinit();

  return version;
}

bool Rpl_sys_table_access::delete_version() {
  DBUG_TRACE;
  assert(m_lock_type >= TL_WRITE_ALLOW_WRITE);

  TABLE *table_version = m_table_list[m_table_version_index].table;
  Field **fields = table_version->field;
  fields[0]->set_notnull();
  fields[0]->store(m_table_name.c_str(), m_table_name.length(),
                   &my_charset_bin);

  Rpl_sys_key_access key_access;
  int error = key_access.init(table_version);

  if (HA_ERR_KEY_NOT_FOUND == error) {
    error = 0; /* purecov: inspected */
  } else if (error) {
    return true; /* purecov: inspected */
  } else {
    error |= table_version->file->ha_delete_row(table_version->record[0]);
  }

  error |= (key_access.deinit() != 0);

  return error;
}