1
/* Copyright (C) 2000-2005 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
/* Written by Sergei A. Golubchik, who has a shared copyright to this code */
20
typedef struct st_ft_docstat {
26
typedef struct st_my_ft_parser_param
32
static int FT_WORD_cmp(CHARSET_INFO* cs, FT_WORD *w1, FT_WORD *w2)
34
return ha_compare_text(cs, (uchar*) w1->pos, w1->len,
35
(uchar*) w2->pos, w2->len, 0, 0);
38
static int walk_and_copy(FT_WORD *word,uint32 count,FT_DOCSTAT *docstat)
40
word->weight=LWS_IN_USE;
41
docstat->sum+=word->weight;
42
memcpy_fixed((docstat->list)++,word,sizeof(FT_WORD));
46
/* transforms tree of words into the array, applying normalization */
48
FT_WORD * ft_linearize(TREE *wtree, MEM_ROOT *mem_root)
52
DBUG_ENTER("ft_linearize");
54
if ((wlist=(FT_WORD *) alloc_root(mem_root, sizeof(FT_WORD)*
55
(1+wtree->elements_in_tree))))
58
docstat.uniq=wtree->elements_in_tree;
60
tree_walk(wtree,(tree_walk_action)&walk_and_copy,&docstat,left_root_right);
66
docstat.list->pos=NULL;
68
for (p=wlist;p->pos;p++)
70
p->weight=PRENORM_IN_USE;
73
for (p=wlist;p->pos;p++)
75
p->weight/=NORM_IN_USE;
81
my_bool ft_boolean_check_syntax_string(const uchar *str)
86
(strlen((char*) str)+1 != sizeof(ft_boolean_syntax)) ||
87
(str[0] != ' ' && str[1] != ' '))
89
for (i=0; i<sizeof(ft_boolean_syntax); i++)
91
/* limiting to 7-bit ascii only */
92
if ((unsigned char)(str[i]) > 127 || my_isalnum(default_charset_info, str[i]))
95
if (str[i] == str[j] && (i != 11 || j != 10))
109
uchar ft_get_word(CHARSET_INFO *cs, uchar **start, uchar *end,
110
FT_WORD *word, MYSQL_FTPARSER_BOOLEAN_INFO *param)
117
param->yesno=(FTB_YES==' ') ? 1 : (param->quot != 0);
118
param->weight_adjust= param->wasign= 0;
119
param->type= FT_TOKEN_EOF;
123
for (; doc < end; doc+= (mbl > 0 ? mbl : (mbl < 0 ? -mbl : 1)))
125
mbl= cs->cset->ctype(cs, &ctype, (uchar*)doc, (uchar*)end);
126
if (true_word_char(ctype, *doc))
128
if (*doc == FTB_RQUOT && param->quot)
130
param->quot= (char*) doc;
132
param->type= FT_TOKEN_RIGHT_PAREN;
137
if (*doc == FTB_LBR || *doc == FTB_RBR || *doc == FTB_LQUOT)
139
/* param->prev=' '; */
141
if (*doc == FTB_LQUOT)
142
param->quot= (char*) *start;
143
param->type= (*doc == FTB_RBR ? FT_TOKEN_RIGHT_PAREN : FT_TOKEN_LEFT_PAREN);
146
if (param->prev == ' ')
148
if (*doc == FTB_YES ) { param->yesno=+1; continue; } else
149
if (*doc == FTB_EGAL) { param->yesno= 0; continue; } else
150
if (*doc == FTB_NO ) { param->yesno=-1; continue; } else
151
if (*doc == FTB_INC ) { param->weight_adjust++; continue; } else
152
if (*doc == FTB_DEC ) { param->weight_adjust--; continue; } else
153
if (*doc == FTB_NEG ) { param->wasign= !param->wasign; continue; }
157
param->yesno=(FTB_YES==' ') ? 1 : (param->quot != 0);
158
param->weight_adjust= param->wasign= 0;
162
for (word->pos= doc; doc < end; length++,
163
doc+= (mbl > 0 ? mbl : (mbl < 0 ? -mbl : 1)))
165
mbl= cs->cset->ctype(cs, &ctype, (uchar*)doc, (uchar*)end);
166
if (true_word_char(ctype, *doc))
168
else if (!misc_word_char(*doc) || mwc)
173
param->prev='A'; /* be sure *prev is true_word_char */
174
word->len= (uint)(doc-word->pos) - mwc;
175
if ((param->trunc=(doc<end && *doc == FTB_TRUNC)))
178
if (((length >= ft_min_word_len && !is_stopword((char*) word->pos,
180
|| param->trunc) && length < ft_max_word_len)
183
param->type= FT_TOKEN_WORD;
186
else if (length) /* make sure length > 0 (if start contains spaces only) */
189
param->type= FT_TOKEN_STOPWORD;
196
param->quot= (char*) doc;
197
param->type= 3; /* FT_RBR */
204
uchar ft_simple_get_word(CHARSET_INFO *cs, uchar **start, const uchar *end,
205
FT_WORD *word, my_bool skip_stopwords)
211
DBUG_ENTER("ft_simple_get_word");
215
for (;; doc+= (mbl > 0 ? mbl : (mbl < 0 ? -mbl : 1)))
219
mbl= cs->cset->ctype(cs, &ctype, (uchar*)doc, (uchar*)end);
220
if (true_word_char(ctype, *doc))
225
for (word->pos= doc; doc < end; length++,
226
doc+= (mbl > 0 ? mbl : (mbl < 0 ? -mbl : 1)))
228
mbl= cs->cset->ctype(cs, &ctype, (uchar*)doc, (uchar*)end);
229
if (true_word_char(ctype, *doc))
231
else if (!misc_word_char(*doc) || mwc)
237
word->len= (uint)(doc-word->pos) - mwc;
239
if (skip_stopwords == FALSE ||
240
(length >= ft_min_word_len && length < ft_max_word_len &&
241
!is_stopword((char*) word->pos, word->len)))
250
void ft_parse_init(TREE *wtree, CHARSET_INFO *cs)
252
DBUG_ENTER("ft_parse_init");
253
if (!is_tree_inited(wtree))
254
init_tree(wtree,0,0,sizeof(FT_WORD),(qsort_cmp2)&FT_WORD_cmp,0,NULL, cs);
259
static int ft_add_word(MYSQL_FTPARSER_PARAM *param,
260
char *word, int word_len,
261
MYSQL_FTPARSER_BOOLEAN_INFO *boolean_info __attribute__((unused)))
265
MY_FT_PARSER_PARAM *ft_param=param->mysql_ftparam;
266
DBUG_ENTER("ft_add_word");
267
wtree= ft_param->wtree;
268
if (param->flags & MYSQL_FTFLAGS_NEED_COPY)
271
DBUG_ASSERT(wtree->with_delete == 0);
272
ptr= (uchar *)alloc_root(ft_param->mem_root, word_len);
273
memcpy(ptr, word, word_len);
277
w.pos= (uchar*) word;
279
if (!tree_insert(wtree, &w, 0, wtree->custom_arg))
288
static int ft_parse_internal(MYSQL_FTPARSER_PARAM *param,
289
char *doc_arg, int doc_len)
291
uchar *doc= (uchar*) doc_arg;
292
uchar *end= doc + doc_len;
293
MY_FT_PARSER_PARAM *ft_param=param->mysql_ftparam;
294
TREE *wtree= ft_param->wtree;
296
DBUG_ENTER("ft_parse_internal");
298
while (ft_simple_get_word(wtree->custom_arg, &doc, end, &w, TRUE))
299
if (param->mysql_add_word(param, (char*) w.pos, w.len, 0))
305
int ft_parse(TREE *wtree, uchar *doc, int doclen,
306
struct st_mysql_ftparser *parser,
307
MYSQL_FTPARSER_PARAM *param, MEM_ROOT *mem_root)
309
MY_FT_PARSER_PARAM my_param;
310
DBUG_ENTER("ft_parse");
313
my_param.wtree= wtree;
314
my_param.mem_root= mem_root;
316
param->mysql_parse= ft_parse_internal;
317
param->mysql_add_word= ft_add_word;
318
param->mysql_ftparam= &my_param;
319
param->cs= wtree->custom_arg;
320
param->doc= (char*) doc;
321
param->length= doclen;
322
param->mode= MYSQL_FTPARSER_SIMPLE_MODE;
323
DBUG_RETURN(parser->parse(param));
326
#define MAX_PARAM_NR 2
327
MYSQL_FTPARSER_PARAM *ftparser_call_initializer(MI_INFO *info,
328
uint keynr, uint paramnr)
331
struct st_mysql_ftparser *parser;
332
if (! info->ftparser_param)
334
/* info->ftparser_param can not be zero after the initialization,
335
because it always includes built-in fulltext parser. And built-in
336
parser can be called even if the table has no fulltext indexes and
337
no varchar/text fields. */
338
if (! info->s->ftparsers)
340
/* It's ok that modification to shared structure is done w/o mutex
341
locks, because all threads would set the same variables to the
343
uint i, j, keys= info->s->state.header.keys, ftparsers= 1;
344
for (i= 0; i < keys; i++)
346
MI_KEYDEF *keyinfo= &info->s->keyinfo[i];
347
if (keyinfo->flag & HA_FULLTEXT)
353
keyinfo->ftparser_nr= ftparsers++;
356
if (info->s->keyinfo[j].flag & HA_FULLTEXT &&
357
keyinfo->parser == info->s->keyinfo[j].parser)
359
keyinfo->ftparser_nr= info->s->keyinfo[j].ftparser_nr;
365
info->s->ftparsers= ftparsers;
368
We have to allocate two MYSQL_FTPARSER_PARAM structures per plugin
369
because in a boolean search a parser is called recursively
370
ftb_find_relevance* calls ftb_check_phrase*
373
info->ftparser_param= (MYSQL_FTPARSER_PARAM *)
374
my_malloc(MAX_PARAM_NR * sizeof(MYSQL_FTPARSER_PARAM) *
375
info->s->ftparsers, MYF(MY_WME|MY_ZEROFILL));
376
init_alloc_root(&info->ft_memroot, FTPARSER_MEMROOT_ALLOC_SIZE, 0);
377
if (! info->ftparser_param)
380
if (keynr == NO_SUCH_KEY)
383
parser= &ft_default_parser;
387
ftparser_nr= info->s->keyinfo[keynr].ftparser_nr;
388
parser= info->s->keyinfo[keynr].parser;
390
DBUG_ASSERT(paramnr < MAX_PARAM_NR);
391
ftparser_nr= ftparser_nr*MAX_PARAM_NR + paramnr;
392
if (! info->ftparser_param[ftparser_nr].mysql_add_word)
394
/* Note, that mysql_add_word is used here as a flag:
395
mysql_add_word == 0 - parser is not initialized
396
mysql_add_word != 0 - parser is initialized, or no
397
initialization needed. */
398
info->ftparser_param[ftparser_nr].mysql_add_word=
399
(int (*)(struct st_mysql_ftparser_param *, char *, int,
400
MYSQL_FTPARSER_BOOLEAN_INFO *)) 1;
401
if (parser->init && parser->init(&info->ftparser_param[ftparser_nr]))
404
return &info->ftparser_param[ftparser_nr];
407
void ftparser_call_deinitializer(MI_INFO *info)
409
uint i, j, keys= info->s->state.header.keys;
410
free_root(&info->ft_memroot, MYF(0));
411
if (! info->ftparser_param)
413
for (i= 0; i < keys; i++)
415
MI_KEYDEF *keyinfo= &info->s->keyinfo[i];
416
for (j=0; j < MAX_PARAM_NR; j++)
418
MYSQL_FTPARSER_PARAM *ftparser_param=
419
&info->ftparser_param[keyinfo->ftparser_nr*MAX_PARAM_NR + j];
420
if (keyinfo->flag & HA_FULLTEXT && ftparser_param->mysql_add_word)
422
if (keyinfo->parser->deinit)
423
keyinfo->parser->deinit(ftparser_param);
424
ftparser_param->mysql_add_word= 0;