1
/* Copyright (C) 2001-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 */
18
/* TODO: add caching - pre-read several index entries at once */
21
Added optimization for full-text queries with plus-words. It was
22
implemented by sharing maximal document id (max_docid) variable
23
inside plus subtree. max_docid could be used by any word in plus
24
subtree, but it could be updated by plus-word only.
26
Fulltext "smarter index merge" optimization assumes that rows
27
it gets are ordered by doc_id. That is not the case when we
28
search for a word with truncation operator. It may return
29
rows in random order. Thus we may not use "smarter index merge"
30
optimization with "trunc-words".
32
The idea is: there is no need to search for docid smaller than
33
biggest docid inside current plus subtree or any upper plus subtree.
38
max_docid updated by word1
41
max_docid updated by word1
42
+(word1 -word2) +(+word3 word4)
44
max_docid updated by word3
45
+word1 word2 (+word3 word4 (+word5 word6))
46
three subexpressions (including the top-level one),
47
every one has its own max_docid, updated by its plus word.
48
but for the search word6 uses
49
max(word1.max_docid, word3.max_docid, word5.max_docid),
50
while word4 uses, accordingly,
51
max(word1.max_docid, word3.max_docid).
57
/* search with boolean queries */
59
static double _wghts[11]=
72
static double *wghts=_wghts+5; /* wghts[i] = 1.5**i */
74
static double _nwghts[11]=
87
static double *nwghts=_nwghts+5; /* nwghts[i] = -0.5*1.5**i */
89
#define FTB_FLAG_TRUNC 1
90
/* At most one of the following flags can be set */
91
#define FTB_FLAG_YES 2
93
#define FTB_FLAG_WONLY 8
95
typedef struct st_ftb_expr FTB_EXPR;
100
/* ^^^^^^^^^^^^^^^^^^ FTB_{EXPR,WORD} common section */
105
LIST *phrase; /* phrase words */
106
LIST *document; /* for phrase search */
107
uint yesses; /* number of "yes" words matched */
108
uint nos; /* number of "no" words matched */
109
uint ythresh; /* number of "yes" words in expr */
110
uint yweaks; /* number of "yes" words for scan only */
113
typedef struct st_ftb_word
117
/* ^^^^^^^^^^^^^^^^^^ FTB_{EXPR,WORD} common section */
118
my_off_t docid[2]; /* for index search and for scan */
120
FTB_EXPR *max_docid_expr;
122
struct st_ftb_word *prev;
130
typedef struct st_ft_info
132
struct _ft_vft *please;
134
CHARSET_INFO *charset;
144
enum { UNINITIALIZED, READY, INDEX_SEARCH, INDEX_DONE } state;
147
static int FTB_WORD_cmp(my_off_t *v, FTB_WORD *a, FTB_WORD *b)
151
/* if a==curdoc, take it as a < b */
152
if (v && a->docid[0] == *v)
155
/* ORDER BY docid, ndepth DESC */
156
i=CMP_NUM(a->docid[0], b->docid[0]);
158
i=CMP_NUM(b->ndepth,a->ndepth);
162
static int FTB_WORD_cmp_list(CHARSET_INFO *cs, FTB_WORD **a, FTB_WORD **b)
164
/* ORDER BY word DESC, ndepth DESC */
165
int i= ha_compare_text(cs, (uchar*) (*b)->word+1,(*b)->len-1,
166
(uchar*) (*a)->word+1,(*a)->len-1,0,0);
168
i=CMP_NUM((*b)->ndepth,(*a)->ndepth);
173
typedef struct st_my_ftb_param
182
static int ftb_query_add_word(MYSQL_FTPARSER_PARAM *param,
183
char *word, int word_len,
184
MYSQL_FTPARSER_BOOLEAN_INFO *info)
186
MY_FTB_PARAM *ftb_param= param->mysql_ftparam;
188
FTB_EXPR *ftbe, *tmp_expr;
189
FT_WORD *phrase_word;
191
int r= info->weight_adjust;
192
float weight= (float)
193
(info->wasign ? nwghts : wghts)[(r>5)?5:((r<-5)?-5:r)];
195
switch (info->type) {
197
ftbw= (FTB_WORD *)alloc_root(&ftb_param->ftb->mem_root,
199
(info->trunc ? MI_MAX_KEY_BUFF :
200
word_len * ftb_param->ftb->charset->mbmaxlen +
202
ftb_param->ftb->info->s->rec_reflength));
203
ftbw->len= word_len + 1;
206
if (info->yesno > 0) ftbw->flags|= FTB_FLAG_YES;
207
if (info->yesno < 0) ftbw->flags|= FTB_FLAG_NO;
208
if (info->trunc) ftbw->flags|= FTB_FLAG_TRUNC;
209
ftbw->weight= weight;
210
ftbw->up= ftb_param->ftbe;
211
ftbw->docid[0]= ftbw->docid[1]= HA_OFFSET_ERROR;
212
ftbw->ndepth= (info->yesno < 0) + ftb_param->depth;
213
ftbw->key_root= HA_OFFSET_ERROR;
214
memcpy(ftbw->word + 1, word, word_len);
215
ftbw->word[0]= word_len;
216
if (info->yesno > 0) ftbw->up->ythresh++;
217
ftb_param->ftb->queue.max_elements++;
218
ftbw->prev= ftb_param->ftb->last_word;
219
ftb_param->ftb->last_word= ftbw;
220
ftb_param->ftb->with_scan|= (info->trunc & FTB_FLAG_TRUNC);
221
for (tmp_expr= ftb_param->ftbe; tmp_expr->up; tmp_expr= tmp_expr->up)
222
if (! (tmp_expr->flags & FTB_FLAG_YES))
224
ftbw->max_docid_expr= tmp_expr;
226
case FT_TOKEN_STOPWORD:
227
if (! ftb_param->up_quot) break;
228
phrase_word= (FT_WORD *)alloc_root(&ftb_param->ftb->mem_root, sizeof(FT_WORD));
229
tmp_element= (LIST *)alloc_root(&ftb_param->ftb->mem_root, sizeof(LIST));
230
phrase_word->pos= (uchar*) word;
231
phrase_word->len= word_len;
232
tmp_element->data= (void *)phrase_word;
233
ftb_param->ftbe->phrase= list_add(ftb_param->ftbe->phrase, tmp_element);
234
/* Allocate document list at this point.
235
It allows to avoid huge amount of allocs/frees for each row.*/
236
tmp_element= (LIST *)alloc_root(&ftb_param->ftb->mem_root, sizeof(LIST));
237
tmp_element->data= alloc_root(&ftb_param->ftb->mem_root, sizeof(FT_WORD));
238
ftb_param->ftbe->document=
239
list_add(ftb_param->ftbe->document, tmp_element);
241
case FT_TOKEN_LEFT_PAREN:
242
ftbe=(FTB_EXPR *)alloc_root(&ftb_param->ftb->mem_root, sizeof(FTB_EXPR));
244
if (info->yesno > 0) ftbe->flags|= FTB_FLAG_YES;
245
if (info->yesno < 0) ftbe->flags|= FTB_FLAG_NO;
246
ftbe->weight= weight;
247
ftbe->up= ftb_param->ftbe;
248
ftbe->max_docid= ftbe->ythresh= ftbe->yweaks= 0;
249
ftbe->docid[0]= ftbe->docid[1]= HA_OFFSET_ERROR;
252
if (info->quot) ftb_param->ftb->with_scan|= 2;
253
if (info->yesno > 0) ftbe->up->ythresh++;
254
ftb_param->ftbe= ftbe;
256
ftb_param->up_quot= (uchar*) info->quot;
258
case FT_TOKEN_RIGHT_PAREN:
259
if (ftb_param->ftbe->document)
261
/* Circuit document list */
262
for (tmp_element= ftb_param->ftbe->document;
263
tmp_element->next; tmp_element= tmp_element->next) /* no-op */;
264
tmp_element->next= ftb_param->ftbe->document;
265
ftb_param->ftbe->document->prev= tmp_element;
268
if (ftb_param->ftbe->up)
270
DBUG_ASSERT(ftb_param->depth);
271
ftb_param->ftbe= ftb_param->ftbe->up;
273
ftb_param->up_quot= 0;
284
static int ftb_parse_query_internal(MYSQL_FTPARSER_PARAM *param,
285
char *query, int len)
287
MY_FTB_PARAM *ftb_param= param->mysql_ftparam;
288
MYSQL_FTPARSER_BOOLEAN_INFO info;
289
CHARSET_INFO *cs= ftb_param->ftb->charset;
290
uchar **start= (uchar**) &query;
291
uchar *end= (uchar*) query + len;
296
while (ft_get_word(cs, start, end, &w, &info))
297
param->mysql_add_word(param, (char*) w.pos, w.len, &info);
302
static int _ftb_parse_query(FTB *ftb, uchar *query, uint len,
303
struct st_mysql_ftparser *parser)
305
MYSQL_FTPARSER_PARAM *param;
306
MY_FTB_PARAM ftb_param;
307
DBUG_ENTER("_ftb_parse_query");
310
if (ftb->state != UNINITIALIZED)
312
if (! (param= ftparser_call_initializer(ftb->info, ftb->keynr, 0)))
317
ftb_param.ftbe= ftb->root;
318
ftb_param.up_quot= 0;
320
param->mysql_parse= ftb_parse_query_internal;
321
param->mysql_add_word= ftb_query_add_word;
322
param->mysql_ftparam= (void *)&ftb_param;
323
param->cs= ftb->charset;
324
param->doc= (char*) query;
327
param->mode= MYSQL_FTPARSER_FULL_BOOLEAN_INFO;
328
DBUG_RETURN(parser->parse(param));
332
static int _ftb_no_dupes_cmp(void* not_used __attribute__((unused)),
333
const void *a,const void *b)
335
return CMP_NUM((*((my_off_t*)a)), (*((my_off_t*)b)));
338
/* returns 1 if the search was finished (must-word wasn't found) */
339
static int _ft2_search(FTB *ftb, FTB_WORD *ftbw, my_bool init_search)
344
MI_INFO *info=ftb->info;
345
uint off= 0, extra= HA_FT_WLEN+info->s->base.rec_reflength;
346
uchar *lastkey_buf=ftbw->word+ftbw->off;
348
if (ftbw->flags & FTB_FLAG_TRUNC)
349
lastkey_buf+=ftbw->len;
353
ftbw->key_root=info->s->state.key_root[ftb->keynr];
354
ftbw->keyinfo=info->s->keyinfo+ftb->keynr;
356
r=_mi_search(info, ftbw->keyinfo, (uchar*) ftbw->word, ftbw->len,
357
SEARCH_FIND | SEARCH_BIGGER, ftbw->key_root);
361
uint sflag= SEARCH_BIGGER;
362
my_off_t max_docid=0;
365
for (tmp= ftbw->max_docid_expr; tmp; tmp= tmp->up)
366
set_if_bigger(max_docid, tmp->max_docid);
368
if (ftbw->docid[0] < max_docid)
371
_mi_dpointer(info, (uchar *)(ftbw->word + ftbw->len + HA_FT_WLEN),
374
r=_mi_search(info, ftbw->keyinfo, (uchar*) lastkey_buf,
375
USE_WHOLE_KEY, sflag, ftbw->key_root);
378
can_go_down=(!ftbw->off && (init_search || (ftbw->flags & FTB_FLAG_TRUNC)));
379
/* Skip rows inserted by concurrent insert */
385
off=info->lastkey_length-extra;
386
subkeys=ft_sintXkorr(info->lastkey+off);
388
if (subkeys<0 || info->lastpos < info->state->data_file_length)
390
r= _mi_search_next(info, ftbw->keyinfo, info->lastkey,
391
info->lastkey_length,
392
SEARCH_BIGGER, ftbw->key_root);
395
if (!r && !ftbw->off)
397
r= ha_compare_text(ftb->charset,
399
info->lastkey_length-extra-1,
400
(uchar*) ftbw->word+1,
402
(my_bool) (ftbw->flags & FTB_FLAG_TRUNC),0);
405
if (r) /* not found */
407
if (!ftbw->off || !(ftbw->flags & FTB_FLAG_TRUNC))
409
ftbw->docid[0]=HA_OFFSET_ERROR;
410
if ((ftbw->flags & FTB_FLAG_YES) && ftbw->up->up==0)
413
This word MUST BE present in every document returned,
414
so we can stop the search right now
416
ftb->state=INDEX_DONE;
417
return 1; /* search is done */
423
/* going up to the first-level tree to continue search there */
424
_mi_dpointer(info, (uchar*) (lastkey_buf+HA_FT_WLEN), ftbw->key_root);
425
ftbw->key_root=info->s->state.key_root[ftb->keynr];
426
ftbw->keyinfo=info->s->keyinfo+ftb->keynr;
428
return _ft2_search(ftb, ftbw, 0);
431
/* matching key found */
432
memcpy(lastkey_buf, info->lastkey, info->lastkey_length);
433
if (lastkey_buf == ftbw->word)
434
ftbw->len=info->lastkey_length-extra;
440
yep, going down, to the second-level tree
441
TODO here: subkey-based optimization
444
ftbw->key_root=info->lastpos;
445
ftbw->keyinfo=& info->s->ft2_keyinfo;
446
r=_mi_search_first(info, ftbw->keyinfo, ftbw->key_root);
447
DBUG_ASSERT(r==0); /* found something */
448
memcpy(lastkey_buf+off, info->lastkey, info->lastkey_length);
450
ftbw->docid[0]=info->lastpos;
451
if (ftbw->flags & FTB_FLAG_YES && !(ftbw->flags & FTB_FLAG_TRUNC))
452
ftbw->max_docid_expr->max_docid= info->lastpos;
456
static void _ftb_init_index_search(FT_INFO *ftb)
461
if ((ftb->state != READY && ftb->state !=INDEX_DONE) ||
462
ftb->keynr == NO_SUCH_KEY)
464
ftb->state=INDEX_SEARCH;
466
for (i=ftb->queue.elements; i; i--)
468
ftbw=(FTB_WORD *)(ftb->queue.root[i]);
470
if (ftbw->flags & FTB_FLAG_TRUNC)
473
special treatment for truncation operator
474
1. there are some (besides this) +words
475
| no need to search in the index, it can never ADD new rows
476
| to the result, and to remove half-matched rows we do scan anyway
479
3. in 1 and 2, +/- need not be on the same expr. level,
480
but can be on any upper level, as in +word +(trunc1* trunc2*)
482
| We have to index-search for this prefix.
483
| It may cause duplicates, as in the index (sorted by <word,docid>)
487
| Searching for "aa*" will find row1 twice...
490
for (ftbe=(FTB_EXPR*)ftbw;
491
ftbe->up && !(ftbe->up->flags & FTB_FLAG_TRUNC);
492
ftbe->up->flags|= FTB_FLAG_TRUNC, ftbe=ftbe->up)
494
if (ftbe->flags & FTB_FLAG_NO || /* 2 */
495
ftbe->up->ythresh - ftbe->up->yweaks >
496
(uint) test(ftbe->flags & FTB_FLAG_YES)) /* 1 */
498
FTB_EXPR *top_ftbe=ftbe->up;
499
ftbw->docid[0]=HA_OFFSET_ERROR;
500
for (ftbe=(FTB_EXPR *)ftbw;
501
ftbe != top_ftbe && !(ftbe->flags & FTB_FLAG_NO);
511
if (!is_tree_inited(& ftb->no_dupes))
512
init_tree(& ftb->no_dupes,0,0,sizeof(my_off_t),
513
_ftb_no_dupes_cmp,0,0,0);
515
reset_tree(& ftb->no_dupes);
518
ftbw->off=0; /* in case of reinit */
519
if (_ft2_search(ftb, ftbw, 1))
522
queue_fix(& ftb->queue);
526
FT_INFO * ft_init_boolean_search(MI_INFO *info, uint keynr, uchar *query,
527
uint query_len, CHARSET_INFO *cs)
533
if (!(ftb=(FTB *)my_malloc(sizeof(FTB), MYF(MY_WME))))
535
ftb->please= (struct _ft_vft *) & _ft_vft_boolean;
536
ftb->state=UNINITIALIZED;
540
DBUG_ASSERT(keynr==NO_SUCH_KEY || cs == info->s->keyinfo[keynr].seg->charset);
542
ftb->lastpos=HA_OFFSET_ERROR;
543
bzero(& ftb->no_dupes, sizeof(TREE));
546
init_alloc_root(&ftb->mem_root, 1024, 1024);
547
ftb->queue.max_elements= 0;
548
if (!(ftbe=(FTB_EXPR *)alloc_root(&ftb->mem_root, sizeof(FTB_EXPR))))
551
ftbe->flags=FTB_FLAG_YES;
554
ftbe->max_docid= ftbe->ythresh= ftbe->yweaks= 0;
555
ftbe->docid[0]=ftbe->docid[1]=HA_OFFSET_ERROR;
559
if (unlikely(_ftb_parse_query(ftb, query, query_len,
560
keynr == NO_SUCH_KEY ? &ft_default_parser :
561
info->s->keyinfo[keynr].parser)))
564
Hack: instead of init_queue, we'll use reinit queue to be able
565
to alloc queue with alloc_root()
567
if (! (ftb->queue.root= (uchar **)alloc_root(&ftb->mem_root,
568
(ftb->queue.max_elements + 1) *
571
reinit_queue(&ftb->queue, ftb->queue.max_elements, 0, 0,
572
(int (*)(void*, uchar*, uchar*))FTB_WORD_cmp, 0);
573
for (ftbw= ftb->last_word; ftbw; ftbw= ftbw->prev)
574
queue_insert(&ftb->queue, (uchar *)ftbw);
575
ftb->list=(FTB_WORD **)alloc_root(&ftb->mem_root,
576
sizeof(FTB_WORD *)*ftb->queue.elements);
577
memcpy(ftb->list, ftb->queue.root+1, sizeof(FTB_WORD *)*ftb->queue.elements);
578
my_qsort2(ftb->list, ftb->queue.elements, sizeof(FTB_WORD *),
579
(qsort2_cmp)FTB_WORD_cmp_list, ftb->charset);
580
if (ftb->queue.elements<2) ftb->with_scan &= ~FTB_FLAG_TRUNC;
584
free_root(& ftb->mem_root, MYF(0));
585
my_free((uchar*)ftb,MYF(0));
590
typedef struct st_my_ftb_phrase_param
596
uint document_length;
598
} MY_FTB_PHRASE_PARAM;
601
static int ftb_phrase_add_word(MYSQL_FTPARSER_PARAM *param,
602
char *word, int word_len,
603
MYSQL_FTPARSER_BOOLEAN_INFO *boolean_info __attribute__((unused)))
605
MY_FTB_PHRASE_PARAM *phrase_param= param->mysql_ftparam;
606
FT_WORD *w= (FT_WORD *)phrase_param->document->data;
607
LIST *phrase, *document;
608
w->pos= (uchar*) word;
610
phrase_param->document= phrase_param->document->prev;
611
if (phrase_param->phrase_length > phrase_param->document_length)
613
phrase_param->document_length++;
616
/* TODO: rewrite phrase search to avoid
617
comparing the same word twice. */
618
for (phrase= phrase_param->phrase, document= phrase_param->document->next;
619
phrase; phrase= phrase->next, document= document->next)
621
FT_WORD *phrase_word= (FT_WORD *)phrase->data;
622
FT_WORD *document_word= (FT_WORD *)document->data;
623
if (my_strnncoll(phrase_param->cs, (uchar*) phrase_word->pos,
625
(uchar*) document_word->pos, document_word->len))
628
phrase_param->match++;
633
static int ftb_check_phrase_internal(MYSQL_FTPARSER_PARAM *param,
634
char *document, int len)
637
MY_FTB_PHRASE_PARAM *phrase_param= param->mysql_ftparam;
638
const uchar *docend= (uchar*) document + len;
639
while (ft_simple_get_word(phrase_param->cs, (uchar**) &document, docend,
642
param->mysql_add_word(param, (char*) word.pos, word.len, 0);
643
if (phrase_param->match)
651
Checks if given buffer matches phrase list.
657
phrase broken into list phrase
661
1 is returned if phrase found, 0 else.
662
-1 is returned if error occurs.
665
static int _ftb_check_phrase(FTB *ftb, const uchar *document, uint len,
666
FTB_EXPR *ftbe, struct st_mysql_ftparser *parser)
668
MY_FTB_PHRASE_PARAM ftb_param;
669
MYSQL_FTPARSER_PARAM *param;
670
DBUG_ENTER("_ftb_check_phrase");
673
if (! (param= ftparser_call_initializer(ftb->info, ftb->keynr, 1)))
676
ftb_param.phrase= ftbe->phrase;
677
ftb_param.document= ftbe->document;
678
ftb_param.cs= ftb->charset;
679
ftb_param.phrase_length= list_length(ftbe->phrase);
680
ftb_param.document_length= 1;
683
param->mysql_parse= ftb_check_phrase_internal;
684
param->mysql_add_word= ftb_phrase_add_word;
685
param->mysql_ftparam= (void *)&ftb_param;
686
param->cs= ftb->charset;
687
param->doc= (char *) document;
690
param->mode= MYSQL_FTPARSER_WITH_STOPWORDS;
691
if (unlikely(parser->parse(param)))
693
DBUG_RETURN(ftb_param.match ? 1 : 0);
697
static int _ftb_climb_the_tree(FTB *ftb, FTB_WORD *ftbw, FT_SEG_ITERATOR *ftsi_orig)
699
FT_SEG_ITERATOR ftsi;
701
float weight=ftbw->weight;
702
int yn_flag= ftbw->flags, ythresh, mode=(ftsi_orig != 0);
703
my_off_t curdoc=ftbw->docid[mode];
704
struct st_mysql_ftparser *parser= ftb->keynr == NO_SUCH_KEY ?
706
ftb->info->s->keyinfo[ftb->keynr].parser;
708
for (ftbe=ftbw->up; ftbe; ftbe=ftbe->up)
710
ythresh = ftbe->ythresh - (mode ? 0 : ftbe->yweaks);
711
if (ftbe->docid[mode] != curdoc)
714
ftbe->yesses=ftbe->nos=0;
715
ftbe->docid[mode]=curdoc;
719
if (yn_flag & FTB_FLAG_YES)
721
weight /= ftbe->ythresh;
722
ftbe->cur_weight += weight;
723
if ((int) ++ftbe->yesses == ythresh)
726
weight=ftbe->cur_weight*ftbe->weight;
727
if (mode && ftbe->phrase)
731
memcpy(&ftsi, ftsi_orig, sizeof(ftsi));
732
while (_mi_ft_segiterator(&ftsi) && !found)
736
found= _ftb_check_phrase(ftb, ftsi.pos, ftsi.len, ftbe, parser);
737
if (unlikely(found < 0))
748
if (yn_flag & FTB_FLAG_NO)
751
NOTE: special sort function of queue assures that all
752
(yn_flag & FTB_FLAG_NO) != 0
753
events for every particular subexpression will
754
"auto-magically" happen BEFORE all the
755
(yn_flag & FTB_FLAG_YES) != 0 events. So no
756
already matched expression can become not-matched again.
765
ftbe->cur_weight += weight;
766
if ((int) ftbe->yesses < ythresh)
768
if (!(yn_flag & FTB_FLAG_WONLY))
769
yn_flag= ((int) ftbe->yesses++ == ythresh) ? ftbe->flags : FTB_FLAG_WONLY ;
770
weight*= ftbe->weight;
777
int ft_boolean_read_next(FT_INFO *ftb, char *record)
781
MI_INFO *info=ftb->info;
784
if (ftb->state != INDEX_SEARCH && ftb->state != INDEX_DONE)
788
if ((int) _mi_check_index(info, ftb->keynr) < 0)
790
if (_mi_readinfo(info, F_RDLCK, 1))
792
/* black magic OFF */
794
if (!ftb->queue.elements)
795
return my_errno=HA_ERR_END_OF_FILE;
797
/* Attention!!! Address of a local variable is used here! See err: label */
798
ftb->queue.first_cmp_arg=(void *)&curdoc;
800
while (ftb->state == INDEX_SEARCH &&
801
(curdoc=((FTB_WORD *)queue_top(& ftb->queue))->docid[0]) !=
804
while (curdoc == (ftbw=(FTB_WORD *)queue_top(& ftb->queue))->docid[0])
806
if (unlikely(_ftb_climb_the_tree(ftb, ftbw, 0)))
808
my_errno= HA_ERR_OUT_OF_MEM;
813
_ft2_search(ftb, ftbw, 0);
814
queue_replaced(& ftb->queue);
818
if (ftbe->docid[0]==curdoc && ftbe->cur_weight>0 &&
819
ftbe->yesses>=(ftbe->ythresh-ftbe->yweaks) && !ftbe->nos)
821
/* curdoc matched ! */
822
if (is_tree_inited(&ftb->no_dupes) &&
823
tree_insert(&ftb->no_dupes, &curdoc, 0,
824
ftb->no_dupes.custom_arg)->count >1)
825
/* but it managed already to get past this line once */
828
info->lastpos=curdoc;
829
/* Clear all states, except that the table was updated */
830
info->update&= (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED);
832
if (!(*info->read_record)(info,curdoc, (uchar*) record))
834
info->update|= HA_STATE_AKTIV; /* Record is read */
835
if (ftb->with_scan &&
836
ft_boolean_find_relevance(ftb,(uchar*) record,0)==0)
837
continue; /* no match */
844
ftb->state=INDEX_DONE;
845
my_errno=HA_ERR_END_OF_FILE;
847
ftb->queue.first_cmp_arg=(void *)0;
852
typedef struct st_my_ftb_find_param
855
FT_SEG_ITERATOR *ftsi;
859
static int ftb_find_relevance_add_word(MYSQL_FTPARSER_PARAM *param,
861
MYSQL_FTPARSER_BOOLEAN_INFO *boolean_info __attribute__((unused)))
863
MY_FTB_FIND_PARAM *ftb_param= param->mysql_ftparam;
864
FT_INFO *ftb= ftb_param->ftb;
867
for (a= 0, b= ftb->queue.elements, c= (a+b)/2; b-a>1; c= (a+b)/2)
870
if (ha_compare_text(ftb->charset, (uchar*)word, len,
871
(uchar*)ftbw->word+1, ftbw->len-1,
872
(my_bool)(ftbw->flags&FTB_FLAG_TRUNC), 0) > 0)
880
if (ha_compare_text(ftb->charset, (uchar*)word, len,
881
(uchar*)ftbw->word + 1,ftbw->len - 1,
882
(my_bool)(ftbw->flags & FTB_FLAG_TRUNC), 0))
884
if (ftbw->docid[1] == ftb->info->lastpos)
886
ftbw->docid[1]= ftb->info->lastpos;
887
if (unlikely(_ftb_climb_the_tree(ftb, ftbw, ftb_param->ftsi)))
894
static int ftb_find_relevance_parse(MYSQL_FTPARSER_PARAM *param,
897
MY_FTB_FIND_PARAM *ftb_param= param->mysql_ftparam;
898
FT_INFO *ftb= ftb_param->ftb;
899
uchar *end= (uchar*) doc + len;
901
while (ft_simple_get_word(ftb->charset, (uchar**) &doc, end, &w, TRUE))
902
param->mysql_add_word(param, (char*) w.pos, w.len, 0);
907
float ft_boolean_find_relevance(FT_INFO *ftb, uchar *record, uint length)
910
FT_SEG_ITERATOR ftsi, ftsi2;
911
my_off_t docid=ftb->info->lastpos;
912
MY_FTB_FIND_PARAM ftb_param;
913
MYSQL_FTPARSER_PARAM *param;
914
struct st_mysql_ftparser *parser= ftb->keynr == NO_SUCH_KEY ?
916
ftb->info->s->keyinfo[ftb->keynr].parser;
918
if (docid == HA_OFFSET_ERROR)
920
if (!ftb->queue.elements)
922
if (! (param= ftparser_call_initializer(ftb->info, ftb->keynr, 0)))
925
if (ftb->state != INDEX_SEARCH && docid <= ftb->lastpos)
930
for (i=0; i < ftb->queue.elements; i++)
932
ftb->list[i]->docid[1]=HA_OFFSET_ERROR;
933
for (x=ftb->list[i]->up; x; x=x->up)
934
x->docid[1]=HA_OFFSET_ERROR;
940
if (ftb->keynr==NO_SUCH_KEY)
941
_mi_ft_segiterator_dummy_init(record, length, &ftsi);
943
_mi_ft_segiterator_init(ftb->info, ftb->keynr, record, &ftsi);
944
memcpy(&ftsi2, &ftsi, sizeof(ftsi));
947
ftb_param.ftsi= &ftsi2;
948
param->mysql_parse= ftb_find_relevance_parse;
949
param->mysql_add_word= ftb_find_relevance_add_word;
950
param->mysql_ftparam= (void *)&ftb_param;
952
param->cs= ftb->charset;
953
param->mode= MYSQL_FTPARSER_SIMPLE_MODE;
954
while (_mi_ft_segiterator(&ftsi))
958
param->doc= (char *)ftsi.pos;
959
param->length= ftsi.len;
960
if (unlikely(parser->parse(param)))
964
if (ftbe->docid[1]==docid && ftbe->cur_weight>0 &&
965
ftbe->yesses>=ftbe->ythresh && !ftbe->nos)
966
{ /* row matched ! */
967
return ftbe->cur_weight;
970
{ /* match failed ! */
976
void ft_boolean_close_search(FT_INFO *ftb)
978
if (is_tree_inited(& ftb->no_dupes))
980
delete_tree(& ftb->no_dupes);
982
free_root(& ftb->mem_root, MYF(0));
983
my_free((uchar*)ftb,MYF(0));
987
float ft_boolean_get_relevance(FT_INFO *ftb)
989
return ftb->root->cur_weight;
993
void ft_boolean_reinit_search(FT_INFO *ftb)
995
_ftb_init_index_search(ftb);