| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2007, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file fts/fts0config.cc |
| 29 |
Full Text Search configuration table. |
| 30 |
|
| 31 |
Created 2007/5/9 Sunny Bains |
| 32 |
***********************************************************************/ |
| 33 |
|
| 34 |
#include <stdlib.h> |
| 35 |
|
| 36 |
#include "fts0priv.h" |
| 37 |
|
| 38 |
#include "row0sel.h" |
| 39 |
#include "trx0roll.h" |
| 40 |
|
| 41 |
/** Callback function for fetching the config value. |
| 42 |
@return always returns true */ |
| 43 |
static bool fts_config_fetch_value(void *row, /*!< in: sel_node_t* */ |
| 44 |
void *user_arg) /*!< in: pointer to |
| 45 |
ib_vector_t */ |
| 46 |
{ |
| 47 |
sel_node_t *node = static_cast<sel_node_t *>(row); |
| 48 |
fts_string_t *value = static_cast<fts_string_t *>(user_arg); |
| 49 |
|
| 50 |
dfield_t *dfield = que_node_get_val(node->select_list); |
| 51 |
dtype_t *type = dfield_get_type(dfield); |
| 52 |
ulint len = dfield_get_len(dfield); |
| 53 |
void *data = dfield_get_data(dfield); |
| 54 |
|
| 55 |
ut_a(dtype_get_mtype(type) == DATA_VARCHAR); |
| 56 |
|
| 57 |
if (len != UNIV_SQL_NULL) { |
| 58 |
ulint max_len = std::min(value->f_len - 1, len); |
| 59 |
|
| 60 |
memcpy(value->f_str, data, max_len); |
| 61 |
value->f_len = max_len; |
| 62 |
value->f_str[value->f_len] = '\0'; |
| 63 |
} |
| 64 |
|
| 65 |
return true; |
| 66 |
} |
| 67 |
|
| 68 |
/** Get value from the config table. The caller must ensure that enough |
| 69 |
space is allocated for value to hold the column contents. |
| 70 |
@return DB_SUCCESS or error code */ |
| 71 |
dberr_t fts_config_get_value(trx_t *trx, /*!< transaction */ |
| 72 |
fts_table_t *fts_table, /*!< in: the indexed |
| 73 |
FTS table */ |
| 74 |
const char *name, /*!< in: get config value for |
| 75 |
this parameter name */ |
| 76 |
fts_string_t *value) /*!< out: value read from |
| 77 |
config table */ |
| 78 |
{ |
| 79 |
pars_info_t *info; |
| 80 |
que_t *graph; |
| 81 |
dberr_t error; |
| 82 |
ulint name_len = strlen(name); |
| 83 |
char table_name[MAX_FULL_NAME_LEN]; |
| 84 |
|
| 85 |
info = pars_info_create(); |
| 86 |
|
| 87 |
*value->f_str = '\0'; |
| 88 |
ut_a(value->f_len > 0); |
| 89 |
|
| 90 |
pars_info_bind_function(info, "my_func", fts_config_fetch_value, value); |
| 91 |
|
| 92 |
/* The len field of value must be set to the max bytes that |
| 93 |
it can hold. On a successful read, the len field will be set |
| 94 |
to the actual number of bytes copied to value. */ |
| 95 |
pars_info_bind_varchar_literal(info, "name", (byte *)name, name_len); |
| 96 |
|
| 97 |
fts_table->suffix = FTS_SUFFIX_CONFIG; |
| 98 |
fts_get_table_name(fts_table, table_name); |
| 99 |
pars_info_bind_id(info, true, "table_name", table_name); |
| 100 |
|
| 101 |
graph = fts_parse_sql(fts_table, info, |
| 102 |
"DECLARE FUNCTION my_func;\n" |
| 103 |
"DECLARE CURSOR c IS SELECT value FROM $table_name" |
| 104 |
" WHERE key = :name;\n" |
| 105 |
"BEGIN\n" |
| 106 |
"" |
| 107 |
"OPEN c;\n" |
| 108 |
"WHILE 1 = 1 LOOP\n" |
| 109 |
" FETCH c INTO my_func();\n" |
| 110 |
" IF c % NOTFOUND THEN\n" |
| 111 |
" EXIT;\n" |
| 112 |
" END IF;\n" |
| 113 |
"END LOOP;\n" |
| 114 |
"CLOSE c;"); |
| 115 |
|
| 116 |
trx->op_info = "getting FTS config value"; |
| 117 |
|
| 118 |
error = fts_eval_sql(trx, graph); |
| 119 |
|
| 120 |
que_graph_free(graph); |
| 121 |
|
| 122 |
return (error); |
| 123 |
} |
| 124 |
|
| 125 |
/** Create the config table name for retrieving index specific value. |
| 126 |
@return index config parameter name */ |
| 127 |
char *fts_config_create_index_param_name( |
| 128 |
const char *param, /*!< in: base name of param */ |
| 129 |
const dict_index_t *index) /*!< in: index for config */ |
| 130 |
{ |
| 131 |
ulint len; |
| 132 |
char *name; |
| 133 |
|
| 134 |
/* The format of the config name is: name_<index_id>. */ |
| 135 |
len = strlen(param); |
| 136 |
|
| 137 |
/* Caller is responsible for deleting name. */ |
| 138 |
name = static_cast<char *>(ut::malloc_withkey( |
| 139 |
UT_NEW_THIS_FILE_PSI_KEY, len + FTS_AUX_MIN_TABLE_ID_LENGTH + 2)); |
| 140 |
::strcpy(name, param); |
| 141 |
name[len] = '_'; |
| 142 |
|
| 143 |
fts_write_object_id(index->id, name + len + 1); |
| 144 |
|
| 145 |
return (name); |
| 146 |
} |
| 147 |
|
| 148 |
/** Get value specific to an FTS index from the config table. The caller |
| 149 |
must ensure that enough space is allocated for value to hold the |
| 150 |
column contents. |
| 151 |
@return DB_SUCCESS or error code */ |
| 152 |
dberr_t fts_config_get_index_value(trx_t *trx, /*!< transaction */ |
| 153 |
dict_index_t *index, /*!< in: index */ |
| 154 |
const char *param, /*!< in: get config value |
| 155 |
for this parameter name */ |
| 156 |
fts_string_t *value) /*!< out: value read |
| 157 |
from config table */ |
| 158 |
{ |
| 159 |
char *name; |
| 160 |
dberr_t error; |
| 161 |
fts_table_t fts_table; |
| 162 |
|
| 163 |
FTS_INIT_FTS_TABLE(&fts_table, FTS_SUFFIX_CONFIG, FTS_COMMON_TABLE, |
| 164 |
index->table); |
| 165 |
|
| 166 |
/* We are responsible for free'ing name. */ |
| 167 |
name = fts_config_create_index_param_name(param, index); |
| 168 |
|
| 169 |
error = fts_config_get_value(trx, &fts_table, name, value); |
| 170 |
|
| 171 |
ut::free(name); |
| 172 |
|
| 173 |
return (error); |
| 174 |
} |
| 175 |
|
| 176 |
/** Set the value in the config table for name. |
| 177 |
@return DB_SUCCESS or error code */ |
| 178 |
dberr_t fts_config_set_value( |
| 179 |
trx_t *trx, /*!< transaction */ |
| 180 |
fts_table_t *fts_table, /*!< in: the indexed |
| 181 |
FTS table */ |
| 182 |
const char *name, /*!< in: get config value for |
| 183 |
this parameter name */ |
| 184 |
const fts_string_t *value) /*!< in: value to update */ |
| 185 |
{ |
| 186 |
pars_info_t *info; |
| 187 |
que_t *graph; |
| 188 |
dberr_t error; |
| 189 |
undo_no_t undo_no; |
| 190 |
undo_no_t n_rows_updated; |
| 191 |
ulint name_len = strlen(name); |
| 192 |
char table_name[MAX_FULL_NAME_LEN]; |
| 193 |
|
| 194 |
info = pars_info_create(); |
| 195 |
|
| 196 |
pars_info_bind_varchar_literal(info, "name", (byte *)name, name_len); |
| 197 |
pars_info_bind_varchar_literal(info, "value", value->f_str, value->f_len); |
| 198 |
|
| 199 |
fts_table->suffix = FTS_SUFFIX_CONFIG; |
| 200 |
fts_get_table_name(fts_table, table_name); |
| 201 |
pars_info_bind_id(info, true, "table_name", table_name); |
| 202 |
|
| 203 |
graph = fts_parse_sql(fts_table, info, |
| 204 |
"BEGIN UPDATE $table_name SET value = :value" |
| 205 |
" WHERE key = :name;"); |
| 206 |
|
| 207 |
trx->op_info = "setting FTS config value"; |
| 208 |
|
| 209 |
undo_no = trx->undo_no; |
| 210 |
|
| 211 |
error = fts_eval_sql(trx, graph); |
| 212 |
|
| 213 |
que_graph_free(graph); |
| 214 |
|
| 215 |
n_rows_updated = trx->undo_no - undo_no; |
| 216 |
|
| 217 |
/* Check if we need to do an insert. */ |
| 218 |
if (n_rows_updated == 0) { |
| 219 |
info = pars_info_create(); |
| 220 |
|
| 221 |
pars_info_bind_varchar_literal(info, "name", (byte *)name, name_len); |
| 222 |
|
| 223 |
pars_info_bind_varchar_literal(info, "value", value->f_str, value->f_len); |
| 224 |
|
| 225 |
fts_get_table_name(fts_table, table_name); |
| 226 |
pars_info_bind_id(info, true, "table_name", table_name); |
| 227 |
|
| 228 |
graph = fts_parse_sql(fts_table, info, |
| 229 |
"BEGIN\n" |
| 230 |
"INSERT INTO $table_name VALUES(:name, :value);"); |
| 231 |
|
| 232 |
trx->op_info = "inserting FTS config value"; |
| 233 |
|
| 234 |
error = fts_eval_sql(trx, graph); |
| 235 |
|
| 236 |
que_graph_free(graph); |
| 237 |
} |
| 238 |
|
| 239 |
return (error); |
| 240 |
} |
| 241 |
|
| 242 |
/** Set the value specific to an FTS index in the config table. |
| 243 |
@return DB_SUCCESS or error code */ |
| 244 |
dberr_t fts_config_set_index_value(trx_t *trx, /*!< transaction */ |
| 245 |
dict_index_t *index, /*!< in: index */ |
| 246 |
const char *param, /*!< in: get config value |
| 247 |
for this parameter name */ |
| 248 |
fts_string_t *value) /*!< out: value read |
| 249 |
from config table */ |
| 250 |
{ |
| 251 |
char *name; |
| 252 |
dberr_t error; |
| 253 |
fts_table_t fts_table; |
| 254 |
|
| 255 |
FTS_INIT_FTS_TABLE(&fts_table, FTS_SUFFIX_CONFIG, FTS_COMMON_TABLE, |
| 256 |
index->table); |
| 257 |
|
| 258 |
/* We are responsible for free'ing name. */ |
| 259 |
name = fts_config_create_index_param_name(param, index); |
| 260 |
|
| 261 |
error = fts_config_set_value(trx, &fts_table, name, value); |
| 262 |
|
| 263 |
ut::free(name); |
| 264 |
|
| 265 |
return (error); |
| 266 |
} |
| 267 |
|
| 268 |
#ifdef FTS_OPTIMIZE_DEBUG |
| 269 |
/** Get an ulint value from the config table. |
| 270 |
@return DB_SUCCESS if all OK else error code */ |
| 271 |
dberr_t fts_config_get_index_ulint(trx_t *trx, /*!< in: transaction */ |
| 272 |
dict_index_t *index, /*!< in: FTS index */ |
| 273 |
const char *name, /*!< in: param name */ |
| 274 |
ulint *int_value) /*!< out: value */ |
| 275 |
{ |
| 276 |
dberr_t error; |
| 277 |
fts_string_t value; |
| 278 |
|
| 279 |
/* We set the length of value to the max bytes it can hold. This |
| 280 |
information is used by the callback that reads the value.*/ |
| 281 |
value.f_len = FTS_MAX_CONFIG_VALUE_LEN; |
| 282 |
value.f_str = static_cast<byte *>( |
| 283 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, value.f_len + 1)); |
| 284 |
|
| 285 |
error = fts_config_get_index_value(trx, index, name, &value); |
| 286 |
|
| 287 |
if (UNIV_UNLIKELY(error != DB_SUCCESS)) { |
| 288 |
ib::error(ER_IB_MSG_457) |
| 289 |
<< "(" << ut_strerr(error) << ") reading `" << name << "'"; |
| 290 |
} else { |
| 291 |
*int_value = strtoul((char *)value.f_str, NULL, 10); |
| 292 |
} |
| 293 |
|
| 294 |
ut::free(value.f_str); |
| 295 |
|
| 296 |
return (error); |
| 297 |
} |
| 298 |
|
| 299 |
/** Set an ulint value in the config table. |
| 300 |
@return DB_SUCCESS if all OK else error code */ |
| 301 |
dberr_t fts_config_set_index_ulint(trx_t *trx, /*!< in: transaction */ |
| 302 |
dict_index_t *index, /*!< in: FTS index */ |
| 303 |
const char *name, /*!< in: param name */ |
| 304 |
ulint int_value) /*!< in: value */ |
| 305 |
{ |
| 306 |
dberr_t error; |
| 307 |
fts_string_t value; |
| 308 |
|
| 309 |
/* We set the length of value to the max bytes it can hold. This |
| 310 |
information is used by the callback that reads the value.*/ |
| 311 |
value.f_len = FTS_MAX_CONFIG_VALUE_LEN; |
| 312 |
value.f_str = static_cast<byte *>( |
| 313 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, value.f_len + 1)); |
| 314 |
|
| 315 |
// FIXME: Get rid of snprintf |
| 316 |
ut_a(FTS_MAX_INT_LEN < FTS_MAX_CONFIG_VALUE_LEN); |
| 317 |
|
| 318 |
value.f_len = |
| 319 |
snprintf((char *)value.f_str, FTS_MAX_INT_LEN, "%lu", int_value); |
| 320 |
|
| 321 |
error = fts_config_set_index_value(trx, index, name, &value); |
| 322 |
|
| 323 |
if (UNIV_UNLIKELY(error != DB_SUCCESS)) { |
| 324 |
ib::error(ER_IB_MSG_458) |
| 325 |
<< "(" << ut_strerr(error) << ") writing `" << name << "'"; |
| 326 |
} |
| 327 |
|
| 328 |
ut::free(value.f_str); |
| 329 |
|
| 330 |
return (error); |
| 331 |
} |
| 332 |
#endif /* FTS_OPTIMIZE_DEBUG */ |
| 333 |
|
| 334 |
/** Get an ulint value from the config table. |
| 335 |
@return DB_SUCCESS if all OK else error code */ |
| 336 |
dberr_t fts_config_get_ulint(trx_t *trx, /*!< in: transaction */ |
| 337 |
fts_table_t *fts_table, /*!< in: the indexed |
| 338 |
FTS table */ |
| 339 |
const char *name, /*!< in: param name */ |
| 340 |
ulint *int_value) /*!< out: value */ |
| 341 |
{ |
| 342 |
dberr_t error; |
| 343 |
fts_string_t value; |
| 344 |
|
| 345 |
/* We set the length of value to the max bytes it can hold. This |
| 346 |
information is used by the callback that reads the value.*/ |
| 347 |
value.f_len = FTS_MAX_CONFIG_VALUE_LEN; |
| 348 |
value.f_str = static_cast<byte *>( |
| 349 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, value.f_len + 1)); |
| 350 |
|
| 351 |
error = fts_config_get_value(trx, fts_table, name, &value); |
| 352 |
|
| 353 |
if (UNIV_UNLIKELY(error != DB_SUCCESS)) { |
| 354 |
ib::error(ER_IB_MSG_459) |
| 355 |
<< "(" << ut_strerr(error) << ") reading `" << name << "'"; |
| 356 |
} else { |
| 357 |
*int_value = strtoul((char *)value.f_str, nullptr, 10); |
| 358 |
} |
| 359 |
|
| 360 |
ut::free(value.f_str); |
| 361 |
|
| 362 |
return (error); |
| 363 |
} |
| 364 |
|
| 365 |
/** Set an ulint value in the config table. |
| 366 |
@return DB_SUCCESS if all OK else error code */ |
| 367 |
dberr_t fts_config_set_ulint(trx_t *trx, /*!< in: transaction */ |
| 368 |
fts_table_t *fts_table, /*!< in: the indexed |
| 369 |
FTS table */ |
| 370 |
const char *name, /*!< in: param name */ |
| 371 |
ulint int_value) /*!< in: value */ |
| 372 |
{ |
| 373 |
dberr_t error; |
| 374 |
fts_string_t value; |
| 375 |
|
| 376 |
/* We set the length of value to the max bytes it can hold. This |
| 377 |
information is used by the callback that reads the value.*/ |
| 378 |
value.f_len = FTS_MAX_CONFIG_VALUE_LEN; |
| 379 |
value.f_str = static_cast<byte *>( |
| 380 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, value.f_len + 1)); |
| 381 |
|
| 382 |
static_assert(FTS_MAX_INT_LEN < FTS_MAX_CONFIG_VALUE_LEN); |
| 383 |
|
| 384 |
value.f_len = |
| 385 |
snprintf((char *)value.f_str, FTS_MAX_INT_LEN, ULINTPF, int_value); |
| 386 |
|
| 387 |
error = fts_config_set_value(trx, fts_table, name, &value); |
| 388 |
|
| 389 |
if (UNIV_UNLIKELY(error != DB_SUCCESS)) { |
| 390 |
ib::error(ER_IB_MSG_460) |
| 391 |
<< "(" << ut_strerr(error) << ") writing `" << name << "'"; |
| 392 |
} |
| 393 |
|
| 394 |
ut::free(value.f_str); |
| 395 |
|
| 396 |
return (error); |
| 397 |
} |