/
fts0fts.cc
7691 lines (6150 loc) · 192 KB
/
fts0fts.cc
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
Copyright (c) 2011, 2018, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2016, 2019, MariaDB Corporation.
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/fts0fts.cc
Full Text Search interface
***********************************************************************/
#include "trx0roll.h"
#include "row0mysql.h"
#include "row0upd.h"
#include "dict0types.h"
#include "dict0stats_bg.h"
#include "row0sel.h"
#include "fts0fts.h"
#include "fts0priv.h"
#include "fts0types.h"
#include "fts0types.ic"
#include "fts0vlc.ic"
#include "fts0plugin.h"
#include "dict0priv.h"
#include "dict0stats.h"
#include "btr0pcur.h"
#include "sync0sync.h"
static const ulint FTS_MAX_ID_LEN = 32;
/** Column name from the FTS config table */
#define FTS_MAX_CACHE_SIZE_IN_MB "cache_size_in_mb"
/** Verify if a aux table name is a obsolete table
by looking up the key word in the obsolete table names */
#define FTS_IS_OBSOLETE_AUX_TABLE(table_name) \
(strstr((table_name), "DOC_ID") != NULL \
|| strstr((table_name), "ADDED") != NULL \
|| strstr((table_name), "STOPWORDS") != NULL)
/** This is maximum FTS cache for each table and would be
a configurable variable */
ulong fts_max_cache_size;
/** Whether the total memory used for FTS cache is exhausted, and we will
need a sync to free some memory */
bool fts_need_sync = false;
/** Variable specifying the total memory allocated for FTS cache */
ulong fts_max_total_cache_size;
/** This is FTS result cache limit for each query and would be
a configurable variable */
size_t fts_result_cache_limit;
/** Variable specifying the maximum FTS max token size */
ulong fts_max_token_size;
/** Variable specifying the minimum FTS max token size */
ulong fts_min_token_size;
// FIXME: testing
static ib_time_t elapsed_time = 0;
static ulint n_nodes = 0;
#ifdef FTS_CACHE_SIZE_DEBUG
/** The cache size permissible lower limit (1K) */
static const ulint FTS_CACHE_SIZE_LOWER_LIMIT_IN_MB = 1;
/** The cache size permissible upper limit (1G) */
static const ulint FTS_CACHE_SIZE_UPPER_LIMIT_IN_MB = 1024;
#endif
/** Time to sleep after DEADLOCK error before retrying operation. */
static const ulint FTS_DEADLOCK_RETRY_WAIT = 100000;
/** variable to record innodb_fts_internal_tbl_name for information
schema table INNODB_FTS_INSERTED etc. */
char* fts_internal_tbl_name = NULL;
char* fts_internal_tbl_name2 = NULL;
/** InnoDB default stopword list:
There are different versions of stopwords, the stop words listed
below comes from "Google Stopword" list. Reference:
http://meta.wikimedia.org/wiki/Stop_word_list/google_stop_word_list.
The final version of InnoDB default stopword list is still pending
for decision */
const char *fts_default_stopword[] =
{
"a",
"about",
"an",
"are",
"as",
"at",
"be",
"by",
"com",
"de",
"en",
"for",
"from",
"how",
"i",
"in",
"is",
"it",
"la",
"of",
"on",
"or",
"that",
"the",
"this",
"to",
"was",
"what",
"when",
"where",
"who",
"will",
"with",
"und",
"the",
"www",
NULL
};
/** For storing table info when checking for orphaned tables. */
struct fts_aux_table_t {
table_id_t id; /*!< Table id */
table_id_t parent_id; /*!< Parent table id */
table_id_t index_id; /*!< Table FT index id */
char* name; /*!< Name of the table */
};
/** FTS auxiliary table suffixes that are common to all FT indexes. */
const char* fts_common_tables[] = {
"BEING_DELETED",
"BEING_DELETED_CACHE",
"CONFIG",
"DELETED",
"DELETED_CACHE",
NULL
};
/** FTS auxiliary INDEX split intervals. */
const fts_index_selector_t fts_index_selector[] = {
{ 9, "INDEX_1" },
{ 65, "INDEX_2" },
{ 70, "INDEX_3" },
{ 75, "INDEX_4" },
{ 80, "INDEX_5" },
{ 85, "INDEX_6" },
{ 0 , NULL }
};
/** Default config values for FTS indexes on a table. */
static const char* fts_config_table_insert_values_sql =
"BEGIN\n"
"\n"
"INSERT INTO $config_table VALUES('"
FTS_MAX_CACHE_SIZE_IN_MB "', '256');\n"
""
"INSERT INTO $config_table VALUES('"
FTS_OPTIMIZE_LIMIT_IN_SECS "', '180');\n"
""
"INSERT INTO $config_table VALUES ('"
FTS_SYNCED_DOC_ID "', '0');\n"
""
"INSERT INTO $config_table VALUES ('"
FTS_TOTAL_DELETED_COUNT "', '0');\n"
"" /* Note: 0 == FTS_TABLE_STATE_RUNNING */
"INSERT INTO $config_table VALUES ('"
FTS_TABLE_STATE "', '0');\n";
/** FTS tokenize parmameter for plugin parser */
struct fts_tokenize_param_t {
fts_doc_t* result_doc; /*!< Result doc for tokens */
ulint add_pos; /*!< Added position for tokens */
};
/** Run SYNC on the table, i.e., write out data from the cache to the
FTS auxiliary INDEX table and clear the cache at the end.
@param[in,out] sync sync state
@param[in] unlock_cache whether unlock cache lock when write node
@param[in] wait whether wait when a sync is in progress
@param[in] has_dict whether has dict operation lock
@return DB_SUCCESS if all OK */
static
dberr_t
fts_sync(
fts_sync_t* sync,
bool unlock_cache,
bool wait,
bool has_dict);
/****************************************************************//**
Release all resources help by the words rb tree e.g., the node ilist. */
static
void
fts_words_free(
/*===========*/
ib_rbt_t* words) /*!< in: rb tree of words */
MY_ATTRIBUTE((nonnull));
#ifdef FTS_CACHE_SIZE_DEBUG
/****************************************************************//**
Read the max cache size parameter from the config table. */
static
void
fts_update_max_cache_size(
/*======================*/
fts_sync_t* sync); /*!< in: sync state */
#endif
/*********************************************************************//**
This function fetches the document just inserted right before
we commit the transaction, and tokenize the inserted text data
and insert into FTS auxiliary table and its cache.
@return TRUE if successful */
static
ulint
fts_add_doc_by_id(
/*==============*/
fts_trx_table_t*ftt, /*!< in: FTS trx table */
doc_id_t doc_id, /*!< in: doc id */
ib_vector_t* fts_indexes MY_ATTRIBUTE((unused)));
/*!< in: affected fts indexes */
/******************************************************************//**
Update the last document id. This function could create a new
transaction to update the last document id.
@return DB_SUCCESS if OK */
static
dberr_t
fts_update_sync_doc_id(
/*===================*/
const dict_table_t* table, /*!< in: table */
const char* table_name, /*!< in: table name, or NULL */
doc_id_t doc_id, /*!< in: last document id */
trx_t* trx) /*!< in: update trx, or NULL */
MY_ATTRIBUTE((nonnull(1)));
/** Tokenize a document.
@param[in,out] doc document to tokenize
@param[out] result tokenization result
@param[in] parser pluggable parser */
static
void
fts_tokenize_document(
fts_doc_t* doc,
fts_doc_t* result,
st_mysql_ftparser* parser);
/** Continue to tokenize a document.
@param[in,out] doc document to tokenize
@param[in] add_pos add this position to all tokens from this tokenization
@param[out] result tokenization result
@param[in] parser pluggable parser */
static
void
fts_tokenize_document_next(
fts_doc_t* doc,
ulint add_pos,
fts_doc_t* result,
st_mysql_ftparser* parser);
/** Create the vector of fts_get_doc_t instances.
@param[in,out] cache fts cache
@return vector of fts_get_doc_t instances */
static
ib_vector_t*
fts_get_docs_create(
fts_cache_t* cache);
/** Free the FTS cache.
@param[in,out] cache to be freed */
static
void
fts_cache_destroy(fts_cache_t* cache)
{
rw_lock_free(&cache->lock);
rw_lock_free(&cache->init_lock);
mutex_free(&cache->optimize_lock);
mutex_free(&cache->deleted_lock);
mutex_free(&cache->doc_id_lock);
os_event_destroy(cache->sync->event);
if (cache->stopword_info.cached_stopword) {
rbt_free(cache->stopword_info.cached_stopword);
}
if (cache->sync_heap->arg) {
mem_heap_free(static_cast<mem_heap_t*>(cache->sync_heap->arg));
}
mem_heap_free(cache->cache_heap);
}
/** Get a character set based on precise type.
@param prtype precise type
@return the corresponding character set */
UNIV_INLINE
CHARSET_INFO*
fts_get_charset(ulint prtype)
{
#ifdef UNIV_DEBUG
switch (prtype & DATA_MYSQL_TYPE_MASK) {
case MYSQL_TYPE_BIT:
case MYSQL_TYPE_STRING:
case MYSQL_TYPE_VAR_STRING:
case MYSQL_TYPE_TINY_BLOB:
case MYSQL_TYPE_MEDIUM_BLOB:
case MYSQL_TYPE_BLOB:
case MYSQL_TYPE_LONG_BLOB:
case MYSQL_TYPE_VARCHAR:
break;
default:
ut_error;
}
#endif /* UNIV_DEBUG */
uint cs_num = (uint) dtype_get_charset_coll(prtype);
if (CHARSET_INFO* cs = get_charset(cs_num, MYF(MY_WME))) {
return(cs);
}
ib::fatal() << "Unable to find charset-collation " << cs_num;
return(NULL);
}
/****************************************************************//**
This function loads the default InnoDB stopword list */
static
void
fts_load_default_stopword(
/*======================*/
fts_stopword_t* stopword_info) /*!< in: stopword info */
{
fts_string_t str;
mem_heap_t* heap;
ib_alloc_t* allocator;
ib_rbt_t* stop_words;
allocator = stopword_info->heap;
heap = static_cast<mem_heap_t*>(allocator->arg);
if (!stopword_info->cached_stopword) {
stopword_info->cached_stopword = rbt_create_arg_cmp(
sizeof(fts_tokenizer_word_t), innobase_fts_text_cmp,
&my_charset_latin1);
}
stop_words = stopword_info->cached_stopword;
str.f_n_char = 0;
for (ulint i = 0; fts_default_stopword[i]; ++i) {
char* word;
fts_tokenizer_word_t new_word;
/* We are going to duplicate the value below. */
word = const_cast<char*>(fts_default_stopword[i]);
new_word.nodes = ib_vector_create(
allocator, sizeof(fts_node_t), 4);
str.f_len = ut_strlen(word);
str.f_str = reinterpret_cast<byte*>(word);
fts_string_dup(&new_word.text, &str, heap);
rbt_insert(stop_words, &new_word, &new_word);
}
stopword_info->status = STOPWORD_FROM_DEFAULT;
}
/****************************************************************//**
Callback function to read a single stopword value.
@return Always return TRUE */
static
ibool
fts_read_stopword(
/*==============*/
void* row, /*!< in: sel_node_t* */
void* user_arg) /*!< in: pointer to ib_vector_t */
{
ib_alloc_t* allocator;
fts_stopword_t* stopword_info;
sel_node_t* sel_node;
que_node_t* exp;
ib_rbt_t* stop_words;
dfield_t* dfield;
fts_string_t str;
mem_heap_t* heap;
ib_rbt_bound_t parent;
sel_node = static_cast<sel_node_t*>(row);
stopword_info = static_cast<fts_stopword_t*>(user_arg);
stop_words = stopword_info->cached_stopword;
allocator = static_cast<ib_alloc_t*>(stopword_info->heap);
heap = static_cast<mem_heap_t*>(allocator->arg);
exp = sel_node->select_list;
/* We only need to read the first column */
dfield = que_node_get_val(exp);
str.f_n_char = 0;
str.f_str = static_cast<byte*>(dfield_get_data(dfield));
str.f_len = dfield_get_len(dfield);
/* Only create new node if it is a value not already existed */
if (str.f_len != UNIV_SQL_NULL
&& rbt_search(stop_words, &parent, &str) != 0) {
fts_tokenizer_word_t new_word;
new_word.nodes = ib_vector_create(
allocator, sizeof(fts_node_t), 4);
new_word.text.f_str = static_cast<byte*>(
mem_heap_alloc(heap, str.f_len + 1));
memcpy(new_word.text.f_str, str.f_str, str.f_len);
new_word.text.f_n_char = 0;
new_word.text.f_len = str.f_len;
new_word.text.f_str[str.f_len] = 0;
rbt_insert(stop_words, &new_word, &new_word);
}
return(TRUE);
}
/******************************************************************//**
Load user defined stopword from designated user table
@return TRUE if load operation is successful */
static
ibool
fts_load_user_stopword(
/*===================*/
fts_t* fts, /*!< in: FTS struct */
const char* stopword_table_name, /*!< in: Stopword table
name */
fts_stopword_t* stopword_info) /*!< in: Stopword info */
{
pars_info_t* info;
que_t* graph;
dberr_t error = DB_SUCCESS;
ibool ret = TRUE;
trx_t* trx;
ibool has_lock = fts->fts_status & TABLE_DICT_LOCKED;
trx = trx_create();
trx->op_info = "Load user stopword table into FTS cache";
if (!has_lock) {
mutex_enter(&dict_sys->mutex);
}
/* Validate the user table existence and in the right
format */
stopword_info->charset = fts_valid_stopword_table(stopword_table_name);
if (!stopword_info->charset) {
ret = FALSE;
goto cleanup;
} else if (!stopword_info->cached_stopword) {
/* Create the stopword RB tree with the stopword column
charset. All comparison will use this charset */
stopword_info->cached_stopword = rbt_create_arg_cmp(
sizeof(fts_tokenizer_word_t), innobase_fts_text_cmp,
(void*)stopword_info->charset);
}
info = pars_info_create();
pars_info_bind_id(info, TRUE, "table_stopword", stopword_table_name);
pars_info_bind_function(info, "my_func", fts_read_stopword,
stopword_info);
graph = fts_parse_sql_no_dict_lock(
info,
"DECLARE FUNCTION my_func;\n"
"DECLARE CURSOR c IS"
" SELECT value"
" FROM $table_stopword;\n"
"BEGIN\n"
"\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;");
for (;;) {
error = fts_eval_sql(trx, graph);
if (error == DB_SUCCESS) {
fts_sql_commit(trx);
stopword_info->status = STOPWORD_USER_TABLE;
break;
} else {
fts_sql_rollback(trx);
if (error == DB_LOCK_WAIT_TIMEOUT) {
ib::warn() << "Lock wait timeout reading user"
" stopword table. Retrying!";
trx->error_state = DB_SUCCESS;
} else {
ib::error() << "Error '" << ut_strerr(error)
<< "' while reading user stopword"
" table.";
ret = FALSE;
break;
}
}
}
que_graph_free(graph);
cleanup:
if (!has_lock) {
mutex_exit(&dict_sys->mutex);
}
trx_free(trx);
return(ret);
}
/******************************************************************//**
Initialize the index cache. */
static
void
fts_index_cache_init(
/*=================*/
ib_alloc_t* allocator, /*!< in: the allocator to use */
fts_index_cache_t* index_cache) /*!< in: index cache */
{
ulint i;
ut_a(index_cache->words == NULL);
index_cache->words = rbt_create_arg_cmp(
sizeof(fts_tokenizer_word_t), innobase_fts_text_cmp,
(void*) index_cache->charset);
ut_a(index_cache->doc_stats == NULL);
index_cache->doc_stats = ib_vector_create(
allocator, sizeof(fts_doc_stats_t), 4);
for (i = 0; i < FTS_NUM_AUX_INDEX; ++i) {
ut_a(index_cache->ins_graph[i] == NULL);
ut_a(index_cache->sel_graph[i] == NULL);
}
}
/*********************************************************************//**
Initialize FTS cache. */
void
fts_cache_init(
/*===========*/
fts_cache_t* cache) /*!< in: cache to initialize */
{
ulint i;
/* Just to make sure */
ut_a(cache->sync_heap->arg == NULL);
cache->sync_heap->arg = mem_heap_create(1024);
cache->total_size = 0;
mutex_enter((ib_mutex_t*) &cache->deleted_lock);
cache->deleted_doc_ids = ib_vector_create(
cache->sync_heap, sizeof(fts_update_t), 4);
mutex_exit((ib_mutex_t*) &cache->deleted_lock);
/* Reset the cache data for all the FTS indexes. */
for (i = 0; i < ib_vector_size(cache->indexes); ++i) {
fts_index_cache_t* index_cache;
index_cache = static_cast<fts_index_cache_t*>(
ib_vector_get(cache->indexes, i));
fts_index_cache_init(cache->sync_heap, index_cache);
}
}
/****************************************************************//**
Create a FTS cache. */
fts_cache_t*
fts_cache_create(
/*=============*/
dict_table_t* table) /*!< in: table owns the FTS cache */
{
mem_heap_t* heap;
fts_cache_t* cache;
heap = static_cast<mem_heap_t*>(mem_heap_create(512));
cache = static_cast<fts_cache_t*>(
mem_heap_zalloc(heap, sizeof(*cache)));
cache->cache_heap = heap;
rw_lock_create(fts_cache_rw_lock_key, &cache->lock, SYNC_FTS_CACHE);
rw_lock_create(
fts_cache_init_rw_lock_key, &cache->init_lock,
SYNC_FTS_CACHE_INIT);
mutex_create(LATCH_ID_FTS_DELETE, &cache->deleted_lock);
mutex_create(LATCH_ID_FTS_OPTIMIZE, &cache->optimize_lock);
mutex_create(LATCH_ID_FTS_DOC_ID, &cache->doc_id_lock);
/* This is the heap used to create the cache itself. */
cache->self_heap = ib_heap_allocator_create(heap);
/* This is a transient heap, used for storing sync data. */
cache->sync_heap = ib_heap_allocator_create(heap);
cache->sync_heap->arg = NULL;
cache->sync = static_cast<fts_sync_t*>(
mem_heap_zalloc(heap, sizeof(fts_sync_t)));
cache->sync->table = table;
cache->sync->event = os_event_create(0);
/* Create the index cache vector that will hold the inverted indexes. */
cache->indexes = ib_vector_create(
cache->self_heap, sizeof(fts_index_cache_t), 2);
fts_cache_init(cache);
cache->stopword_info.cached_stopword = NULL;
cache->stopword_info.charset = NULL;
cache->stopword_info.heap = cache->self_heap;
cache->stopword_info.status = STOPWORD_NOT_INIT;
return(cache);
}
/*******************************************************************//**
Add a newly create index into FTS cache */
void
fts_add_index(
/*==========*/
dict_index_t* index, /*!< FTS index to be added */
dict_table_t* table) /*!< table */
{
fts_t* fts = table->fts;
fts_cache_t* cache;
fts_index_cache_t* index_cache;
ut_ad(fts);
cache = table->fts->cache;
rw_lock_x_lock(&cache->init_lock);
ib_vector_push(fts->indexes, &index);
index_cache = fts_find_index_cache(cache, index);
if (!index_cache) {
/* Add new index cache structure */
index_cache = fts_cache_index_cache_create(table, index);
}
rw_lock_x_unlock(&cache->init_lock);
}
/*******************************************************************//**
recalibrate get_doc structure after index_cache in cache->indexes changed */
static
void
fts_reset_get_doc(
/*==============*/
fts_cache_t* cache) /*!< in: FTS index cache */
{
fts_get_doc_t* get_doc;
ulint i;
ut_ad(rw_lock_own(&cache->init_lock, RW_LOCK_X));
ib_vector_reset(cache->get_docs);
for (i = 0; i < ib_vector_size(cache->indexes); i++) {
fts_index_cache_t* ind_cache;
ind_cache = static_cast<fts_index_cache_t*>(
ib_vector_get(cache->indexes, i));
get_doc = static_cast<fts_get_doc_t*>(
ib_vector_push(cache->get_docs, NULL));
memset(get_doc, 0x0, sizeof(*get_doc));
get_doc->index_cache = ind_cache;
get_doc->cache = cache;
}
ut_ad(ib_vector_size(cache->get_docs)
== ib_vector_size(cache->indexes));
}
/*******************************************************************//**
Check an index is in the table->indexes list
@return TRUE if it exists */
static
ibool
fts_in_dict_index(
/*==============*/
dict_table_t* table, /*!< in: Table */
dict_index_t* index_check) /*!< in: index to be checked */
{
dict_index_t* index;
for (index = dict_table_get_first_index(table);
index != NULL;
index = dict_table_get_next_index(index)) {
if (index == index_check) {
return(TRUE);
}
}
return(FALSE);
}
/*******************************************************************//**
Check an index is in the fts->cache->indexes list
@return TRUE if it exists */
static
ibool
fts_in_index_cache(
/*===============*/
dict_table_t* table, /*!< in: Table */
dict_index_t* index) /*!< in: index to be checked */
{
ulint i;
for (i = 0; i < ib_vector_size(table->fts->cache->indexes); i++) {
fts_index_cache_t* index_cache;
index_cache = static_cast<fts_index_cache_t*>(
ib_vector_get(table->fts->cache->indexes, i));
if (index_cache->index == index) {
return(TRUE);
}
}
return(FALSE);
}
/*******************************************************************//**
Check indexes in the fts->indexes is also present in index cache and
table->indexes list
@return TRUE if all indexes match */
ibool
fts_check_cached_index(
/*===================*/
dict_table_t* table) /*!< in: Table where indexes are dropped */
{
ulint i;
if (!table->fts || !table->fts->cache) {
return(TRUE);
}
ut_a(ib_vector_size(table->fts->indexes)
== ib_vector_size(table->fts->cache->indexes));
for (i = 0; i < ib_vector_size(table->fts->indexes); i++) {
dict_index_t* index;
index = static_cast<dict_index_t*>(
ib_vector_getp(table->fts->indexes, i));
if (!fts_in_index_cache(table, index)) {
return(FALSE);
}
if (!fts_in_dict_index(table, index)) {
return(FALSE);
}
}
return(TRUE);
}
/*******************************************************************//**
Drop auxiliary tables related to an FTS index
@return DB_SUCCESS or error number */
dberr_t
fts_drop_index(
/*===========*/
dict_table_t* table, /*!< in: Table where indexes are dropped */
dict_index_t* index, /*!< in: Index to be dropped */
trx_t* trx) /*!< in: Transaction for the drop */
{
ib_vector_t* indexes = table->fts->indexes;
dberr_t err = DB_SUCCESS;
ut_a(indexes);
if ((ib_vector_size(indexes) == 1
&& (index == static_cast<dict_index_t*>(
ib_vector_getp(table->fts->indexes, 0))))
|| ib_vector_is_empty(indexes)) {
doc_id_t current_doc_id;
doc_id_t first_doc_id;
/* If we are dropping the only FTS index of the table,
remove it from optimize thread */
fts_optimize_remove_table(table);
DICT_TF2_FLAG_UNSET(table, DICT_TF2_FTS);
/* If Doc ID column is not added internally by FTS index,
we can drop all FTS auxiliary tables. Otherwise, we will
need to keep some common table such as CONFIG table, so
as to keep track of incrementing Doc IDs */
if (!DICT_TF2_FLAG_IS_SET(
table, DICT_TF2_FTS_HAS_DOC_ID)) {
err = fts_drop_tables(trx, table);
err = fts_drop_index_tables(trx, index);
while (index->index_fts_syncing
&& !trx_is_interrupted(trx)) {
DICT_BG_YIELD(trx);
}
fts_free(table);
return(err);
}
while (index->index_fts_syncing
&& !trx_is_interrupted(trx)) {
DICT_BG_YIELD(trx);
}
current_doc_id = table->fts->cache->next_doc_id;
first_doc_id = table->fts->cache->first_doc_id;
fts_cache_clear(table->fts->cache);
fts_cache_destroy(table->fts->cache);
table->fts->cache = fts_cache_create(table);
table->fts->cache->next_doc_id = current_doc_id;
table->fts->cache->first_doc_id = first_doc_id;
} else {
fts_cache_t* cache = table->fts->cache;
fts_index_cache_t* index_cache;
rw_lock_x_lock(&cache->init_lock);
index_cache = fts_find_index_cache(cache, index);
if (index_cache != NULL) {
while (index->index_fts_syncing
&& !trx_is_interrupted(trx)) {
DICT_BG_YIELD(trx);
}
if (index_cache->words) {
fts_words_free(index_cache->words);
rbt_free(index_cache->words);
}
ib_vector_remove(cache->indexes, *(void**) index_cache);
}
if (cache->get_docs) {
fts_reset_get_doc(cache);
}
rw_lock_x_unlock(&cache->init_lock);
}
err = fts_drop_index_tables(trx, index);
ib_vector_remove(indexes, (const void*) index);
return(err);
}
/****************************************************************//**
Free the query graph but check whether dict_sys->mutex is already
held */
void
fts_que_graph_free_check_lock(
/*==========================*/
fts_table_t* fts_table, /*!< in: FTS table */
const fts_index_cache_t*index_cache, /*!< in: FTS index cache */
que_t* graph) /*!< in: query graph */
{
ibool has_dict = FALSE;
if (fts_table && fts_table->table) {
ut_ad(fts_table->table->fts);
has_dict = fts_table->table->fts->fts_status
& TABLE_DICT_LOCKED;
} else if (index_cache) {
ut_ad(index_cache->index->table->fts);
has_dict = index_cache->index->table->fts->fts_status
& TABLE_DICT_LOCKED;
}
if (!has_dict) {
mutex_enter(&dict_sys->mutex);
}
ut_ad(mutex_own(&dict_sys->mutex));
que_graph_free(graph);
if (!has_dict) {
mutex_exit(&dict_sys->mutex);
}
}
/****************************************************************//**
Create an FTS index cache. */
CHARSET_INFO*
fts_index_get_charset(
/*==================*/
dict_index_t* index) /*!< in: FTS index */
{
CHARSET_INFO* charset = NULL;
dict_field_t* field;
ulint prtype;
field = dict_index_get_nth_field(index, 0);
prtype = field->col->prtype;
charset = fts_get_charset(prtype);
#ifdef FTS_DEBUG
/* Set up charset info for this index. Please note all
field of the FTS index should have the same charset */
for (i = 1; i < index->n_fields; i++) {
CHARSET_INFO* fld_charset;
field = dict_index_get_nth_field(index, i);
prtype = field->col->prtype;
fld_charset = fts_get_charset(prtype);
/* All FTS columns should have the same charset */
if (charset) {
ut_a(charset == fld_charset);
} else {
charset = fld_charset;
}
}
#endif
return(charset);
}
/****************************************************************//**
Create an FTS index cache.
@return Index Cache */
fts_index_cache_t*
fts_cache_index_cache_create(
/*=========================*/
dict_table_t* table, /*!< in: table with FTS index */