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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
|
/*****************************************************************************
Copyright (c) 2007, 2012, Oracle and/or its affiliates. All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.
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 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 Street, Suite 500, Boston, MA 02110-1335 USA
*****************************************************************************/
/******************************************************************//**
@file fts/fts0config.cc
Full Text Search configuration table.
Created 2007/5/9 Sunny Bains
***********************************************************************/
#include "trx0roll.h"
#include "row0sel.h"
#include "fts0priv.h"
#ifndef UNIV_NONINL
#include "fts0types.ic"
#include "fts0vlc.ic"
#endif
/******************************************************************//**
Callback function for fetching the config value.
@return always returns TRUE */
static
ibool
fts_config_fetch_value(
/*===================*/
void* row, /*!< in: sel_node_t* */
void* user_arg) /*!< in: pointer to
ib_vector_t */
{
sel_node_t* node = static_cast<sel_node_t*>(row);
fts_string_t* value = static_cast<fts_string_t*>(user_arg);
dfield_t* dfield = que_node_get_val(node->select_list);
dtype_t* type = dfield_get_type(dfield);
ulint len = dfield_get_len(dfield);
void* data = dfield_get_data(dfield);
ut_a(dtype_get_mtype(type) == DATA_VARCHAR);
if (len != UNIV_SQL_NULL) {
ulint max_len = ut_min(value->f_len - 1, len);
memcpy(value->f_str, data, max_len);
value->f_len = max_len;
value->f_str[value->f_len] = '\0';
}
return(TRUE);
}
/******************************************************************//**
Get value from the config table. The caller must ensure that enough
space is allocated for value to hold the column contents.
@return DB_SUCCESS or error code */
UNIV_INTERN
dberr_t
fts_config_get_value(
/*=================*/
trx_t* trx, /*!< transaction */
fts_table_t* fts_table, /*!< in: the indexed
FTS table */
const char* name, /*!< in: get config value for
this parameter name */
fts_string_t* value) /*!< out: value read from
config table */
{
pars_info_t* info;
que_t* graph;
dberr_t error;
ulint name_len = strlen(name);
info = pars_info_create();
*value->f_str = '\0';
ut_a(value->f_len > 0);
pars_info_bind_function(info, "my_func", fts_config_fetch_value,
value);
/* The len field of value must be set to the max bytes that
it can hold. On a successful read, the len field will be set
to the actual number of bytes copied to value. */
pars_info_bind_varchar_literal(info, "name", (byte*) name, name_len);
fts_table->suffix = "CONFIG";
graph = fts_parse_sql(
fts_table,
info,
"DECLARE FUNCTION my_func;\n"
"DECLARE CURSOR c IS SELECT value FROM %s"
" WHERE key = :name;\n"
"BEGIN\n"
""
"OPEN c;\n"
"WHILE 1 = 1 LOOP\n"
" FETCH c INTO my_func();\n"
" IF c % NOTFOUND THEN\n"
" EXIT;\n"
" END IF;\n"
"END LOOP;\n"
"CLOSE c;");
trx->op_info = "getting FTS config value";
error = fts_eval_sql(trx, graph);
mutex_enter(&dict_sys->mutex);
que_graph_free(graph);
mutex_exit(&dict_sys->mutex);
return(error);
}
/*********************************************************************//**
Create the config table name for retrieving index specific value.
@return index config parameter name */
UNIV_INTERN
char*
fts_config_create_index_param_name(
/*===============================*/
const char* param, /*!< in: base name of param */
const dict_index_t* index) /*!< in: index for config */
{
ulint len;
char* name;
/* The format of the config name is: name_<index_id>. */
len = strlen(param);
/* Caller is responsible for deleting name. */
name = static_cast<char*>(ut_malloc(
len + FTS_AUX_MIN_TABLE_ID_LENGTH + 2));
strcpy(name, param);
name[len] = '_';
fts_write_object_id(index->id, name + len + 1);
return(name);
}
/******************************************************************//**
Get value specific to an FTS index from the config table. The caller
must ensure that enough space is allocated for value to hold the
column contents.
@return DB_SUCCESS or error code */
UNIV_INTERN
dberr_t
fts_config_get_index_value(
/*=======================*/
trx_t* trx, /*!< transaction */
dict_index_t* index, /*!< in: index */
const char* param, /*!< in: get config value for
this parameter name */
fts_string_t* value) /*!< out: value read from
config table */
{
char* name;
dberr_t error;
fts_table_t fts_table;
FTS_INIT_FTS_TABLE(&fts_table, "CONFIG", FTS_COMMON_TABLE,
index->table);
/* We are responsible for free'ing name. */
name = fts_config_create_index_param_name(param, index);
error = fts_config_get_value(trx, &fts_table, name, value);
ut_free(name);
return(error);
}
/******************************************************************//**
Set the value in the config table for name.
@return DB_SUCCESS or error code */
UNIV_INTERN
dberr_t
fts_config_set_value(
/*=================*/
trx_t* trx, /*!< transaction */
fts_table_t* fts_table, /*!< in: the indexed
FTS table */
const char* name, /*!< in: get config value for
this parameter name */
const fts_string_t*
value) /*!< in: value to update */
{
pars_info_t* info;
que_t* graph;
dberr_t error;
undo_no_t undo_no;
undo_no_t n_rows_updated;
ulint name_len = strlen(name);
info = pars_info_create();
pars_info_bind_varchar_literal(info, "name", (byte*) name, name_len);
pars_info_bind_varchar_literal(info, "value",
value->f_str, value->f_len);
fts_table->suffix = "CONFIG";
graph = fts_parse_sql(
fts_table, info,
"BEGIN UPDATE %s SET value = :value WHERE key = :name;");
trx->op_info = "setting FTS config value";
undo_no = trx->undo_no;
error = fts_eval_sql(trx, graph);
fts_que_graph_free_check_lock(fts_table, NULL, graph);
n_rows_updated = trx->undo_no - undo_no;
/* Check if we need to do an insert. */
if (n_rows_updated == 0) {
info = pars_info_create();
pars_info_bind_varchar_literal(
info, "name", (byte*) name, name_len);
pars_info_bind_varchar_literal(
info, "value", value->f_str, value->f_len);
graph = fts_parse_sql(
fts_table, info,
"BEGIN\n"
"INSERT INTO %s VALUES(:name, :value);");
trx->op_info = "inserting FTS config value";
error = fts_eval_sql(trx, graph);
fts_que_graph_free_check_lock(fts_table, NULL, graph);
}
return(error);
}
/******************************************************************//**
Set the value specific to an FTS index in the config table.
@return DB_SUCCESS or error code */
UNIV_INTERN
dberr_t
fts_config_set_index_value(
/*=======================*/
trx_t* trx, /*!< transaction */
dict_index_t* index, /*!< in: index */
const char* param, /*!< in: get config value for
this parameter name */
fts_string_t* value) /*!< out: value read from
config table */
{
char* name;
dberr_t error;
fts_table_t fts_table;
FTS_INIT_FTS_TABLE(&fts_table, "CONFIG", FTS_COMMON_TABLE,
index->table);
/* We are responsible for free'ing name. */
name = fts_config_create_index_param_name(param, index);
error = fts_config_set_value(trx, &fts_table, name, value);
ut_free(name);
return(error);
}
/******************************************************************//**
Get an ulint value from the config table.
@return DB_SUCCESS if all OK else error code */
UNIV_INTERN
dberr_t
fts_config_get_index_ulint(
/*=======================*/
trx_t* trx, /*!< in: transaction */
dict_index_t* index, /*!< in: FTS index */
const char* name, /*!< in: param name */
ulint* int_value) /*!< out: value */
{
dberr_t error;
fts_string_t value;
/* We set the length of value to the max bytes it can hold. This
information is used by the callback that reads the value.*/
value.f_len = FTS_MAX_CONFIG_VALUE_LEN;
value.f_str = static_cast<byte*>(ut_malloc(value.f_len + 1));
error = fts_config_get_index_value(trx, index, name, &value);
if (UNIV_UNLIKELY(error != DB_SUCCESS)) {
ut_print_timestamp(stderr);
fprintf(stderr, " InnoDB: Error: (%s) reading `%s'\n",
ut_strerr(error), name);
} else {
*int_value = strtoul((char*) value.f_str, NULL, 10);
}
ut_free(value.f_str);
return(error);
}
/******************************************************************//**
Set an ulint value in the config table.
@return DB_SUCCESS if all OK else error code */
UNIV_INTERN
dberr_t
fts_config_set_index_ulint(
/*=======================*/
trx_t* trx, /*!< in: transaction */
dict_index_t* index, /*!< in: FTS index */
const char* name, /*!< in: param name */
ulint int_value) /*!< in: value */
{
dberr_t error;
fts_string_t value;
/* We set the length of value to the max bytes it can hold. This
information is used by the callback that reads the value.*/
value.f_len = FTS_MAX_CONFIG_VALUE_LEN;
value.f_str = static_cast<byte*>(ut_malloc(value.f_len + 1));
// FIXME: Get rid of snprintf
ut_a(FTS_MAX_INT_LEN < FTS_MAX_CONFIG_VALUE_LEN);
value.f_len = ut_snprintf(
(char*) value.f_str, FTS_MAX_INT_LEN, "%lu", int_value);
error = fts_config_set_index_value(trx, index, name, &value);
if (UNIV_UNLIKELY(error != DB_SUCCESS)) {
ut_print_timestamp(stderr);
fprintf(stderr, " InnoDB: Error: (%s) writing `%s'\n",
ut_strerr(error), name);
}
ut_free(value.f_str);
return(error);
}
/******************************************************************//**
Get an ulint value from the config table.
@return DB_SUCCESS if all OK else error code */
UNIV_INTERN
dberr_t
fts_config_get_ulint(
/*=================*/
trx_t* trx, /*!< in: transaction */
fts_table_t* fts_table, /*!< in: the indexed
FTS table */
const char* name, /*!< in: param name */
ulint* int_value) /*!< out: value */
{
dberr_t error;
fts_string_t value;
/* We set the length of value to the max bytes it can hold. This
information is used by the callback that reads the value.*/
value.f_len = FTS_MAX_CONFIG_VALUE_LEN;
value.f_str = static_cast<byte*>(ut_malloc(value.f_len + 1));
error = fts_config_get_value(trx, fts_table, name, &value);
if (UNIV_UNLIKELY(error != DB_SUCCESS)) {
ut_print_timestamp(stderr);
fprintf(stderr, " InnoDB: Error: (%s) reading `%s'\n",
ut_strerr(error), name);
} else {
*int_value = strtoul((char*) value.f_str, NULL, 10);
}
ut_free(value.f_str);
return(error);
}
/******************************************************************//**
Set an ulint value in the config table.
@return DB_SUCCESS if all OK else error code */
UNIV_INTERN
dberr_t
fts_config_set_ulint(
/*=================*/
trx_t* trx, /*!< in: transaction */
fts_table_t* fts_table, /*!< in: the indexed
FTS table */
const char* name, /*!< in: param name */
ulint int_value) /*!< in: value */
{
dberr_t error;
fts_string_t value;
/* We set the length of value to the max bytes it can hold. This
information is used by the callback that reads the value.*/
value.f_len = FTS_MAX_CONFIG_VALUE_LEN;
value.f_str = static_cast<byte*>(ut_malloc(value.f_len + 1));
// FIXME: Get rid of snprintf
ut_a(FTS_MAX_INT_LEN < FTS_MAX_CONFIG_VALUE_LEN);
value.f_len = snprintf(
(char*) value.f_str, FTS_MAX_INT_LEN, "%lu", int_value);
error = fts_config_set_value(trx, fts_table, name, &value);
if (UNIV_UNLIKELY(error != DB_SUCCESS)) {
ut_print_timestamp(stderr);
fprintf(stderr, " InnoDB: Error: (%s) writing `%s'\n",
ut_strerr(error), name);
}
ut_free(value.f_str);
return(error);
}
/******************************************************************//**
Increment the value in the config table for column name.
@return DB_SUCCESS or error code */
UNIV_INTERN
dberr_t
fts_config_increment_value(
/*=======================*/
trx_t* trx, /*!< transaction */
fts_table_t* fts_table, /*!< in: the indexed
FTS table */
const char* name, /*!< in: increment config value
for this parameter name */
ulint delta) /*!< in: increment by this
much */
{
dberr_t error;
fts_string_t value;
que_t* graph = NULL;
ulint name_len = strlen(name);
pars_info_t* info = pars_info_create();
/* We set the length of value to the max bytes it can hold. This
information is used by the callback that reads the value.*/
value.f_len = FTS_MAX_CONFIG_VALUE_LEN;
value.f_str = static_cast<byte*>(ut_malloc(value.f_len + 1));
*value.f_str = '\0';
pars_info_bind_varchar_literal(info, "name", (byte*) name, name_len);
pars_info_bind_function(
info, "my_func", fts_config_fetch_value, &value);
fts_table->suffix = "CONFIG";
graph = fts_parse_sql(
fts_table, info,
"DECLARE FUNCTION my_func;\n"
"DECLARE CURSOR c IS SELECT value FROM %s"
" WHERE key = :name FOR UPDATE;\n"
"BEGIN\n"
""
"OPEN c;\n"
"WHILE 1 = 1 LOOP\n"
" FETCH c INTO my_func();\n"
" IF c % NOTFOUND THEN\n"
" EXIT;\n"
" END IF;\n"
"END LOOP;\n"
"CLOSE c;");
trx->op_info = "read FTS config value";
error = fts_eval_sql(trx, graph);
fts_que_graph_free_check_lock(fts_table, NULL, graph);
if (UNIV_UNLIKELY(error == DB_SUCCESS)) {
ulint int_value;
int_value = strtoul((char*) value.f_str, NULL, 10);
int_value += delta;
ut_a(FTS_MAX_CONFIG_VALUE_LEN > FTS_MAX_INT_LEN);
// FIXME: Get rid of snprintf
value.f_len = snprintf(
(char*) value.f_str, FTS_MAX_INT_LEN, "%lu", int_value);
fts_config_set_value(trx, fts_table, name, &value);
}
if (UNIV_UNLIKELY(error != DB_SUCCESS)) {
ut_print_timestamp(stderr);
fprintf(stderr, " InnoDB: Error: (%s) "
"while incrementing %s.\n", ut_strerr(error), name);
}
ut_free(value.f_str);
return(error);
}
/******************************************************************//**
Increment the per index value in the config table for column name.
@return DB_SUCCESS or error code */
UNIV_INTERN
dberr_t
fts_config_increment_index_value(
/*=============================*/
trx_t* trx, /*!< transaction */
dict_index_t* index, /*!< in: FTS index */
const char* param, /*!< in: increment config value
for this parameter name */
ulint delta) /*!< in: increment by this
much */
{
char* name;
dberr_t error;
fts_table_t fts_table;
FTS_INIT_FTS_TABLE(&fts_table, "CONFIG", FTS_COMMON_TABLE,
index->table);
/* We are responsible for free'ing name. */
name = fts_config_create_index_param_name(param, index);
error = fts_config_increment_value(trx, &fts_table, name, delta);
ut_free(name);
return(error);
}
|