MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ft_stopwords.c
1 /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2 
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.
6 
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.
11 
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
15 
16 /* Written by Sergei A. Golubchik, who has a shared copyright to this code */
17 
18 #include "ftdefs.h"
19 #include "my_compare.h"
20 
21 
22 static CHARSET_INFO *ft_stopword_cs= NULL;
23 
24 
25 typedef struct st_ft_stopwords
26 {
27  const char * pos;
28  uint len;
29 } FT_STOPWORD;
30 
31 static TREE *stopwords3=NULL;
32 
33 static int FT_STOPWORD_cmp(void* cmp_arg __attribute__((unused)),
34  FT_STOPWORD *w1, FT_STOPWORD *w2)
35 {
36  return ha_compare_text(ft_stopword_cs,
37  (uchar *)w1->pos,w1->len,
38  (uchar *)w2->pos,w2->len,0,0);
39 }
40 
41 static void FT_STOPWORD_free(FT_STOPWORD *w, TREE_FREE action,
42  void *arg __attribute__((unused)))
43 {
44  if (action == free_free)
45  my_free((void*)w->pos);
46 }
47 
48 static int ft_add_stopword(const char *w)
49 {
50  FT_STOPWORD sw;
51  return !w ||
52  (((sw.len= (uint) strlen(sw.pos=w)) >= ft_min_word_len) &&
53  (tree_insert(stopwords3, &sw, 0, stopwords3->custom_arg)==NULL));
54 }
55 
56 int ft_init_stopwords()
57 {
58  if (!stopwords3)
59  {
60  if (!(stopwords3=(TREE *)my_malloc(sizeof(TREE),MYF(0))))
61  return -1;
62  init_tree(stopwords3,0,0,sizeof(FT_STOPWORD),(qsort_cmp2)&FT_STOPWORD_cmp,
63  0,
64  (ft_stopword_file ? (tree_element_free)&FT_STOPWORD_free : 0),
65  NULL);
66  /*
67  Stopword engine currently does not support tricky
68  character sets UCS2, UTF16, UTF32.
69  Use latin1 to compare stopwords in case of these character sets.
70  It's also fine to use latin1 with the built-in stopwords.
71  */
72  ft_stopword_cs= default_charset_info->mbminlen == 1 ?
73  default_charset_info : &my_charset_latin1;
74  }
75 
76  if (ft_stopword_file)
77  {
78  File fd;
79  uint len;
80  uchar *buffer, *start, *end;
81  FT_WORD w;
82  int error=-1;
83 
84  if (!*ft_stopword_file)
85  return 0;
86 
87  if ((fd=my_open(ft_stopword_file, O_RDONLY, MYF(MY_WME))) == -1)
88  return -1;
89  len=(uint)my_seek(fd, 0L, MY_SEEK_END, MYF(0));
90  my_seek(fd, 0L, MY_SEEK_SET, MYF(0));
91  if (!(start=buffer=my_malloc(len+1, MYF(MY_WME))))
92  goto err0;
93  len=my_read(fd, buffer, len, MYF(MY_WME));
94  end=start+len;
95  while (ft_simple_get_word(ft_stopword_cs, &start, end, &w, TRUE))
96  {
97  if (ft_add_stopword(my_strndup((char*) w.pos, w.len, MYF(0))))
98  goto err1;
99  }
100  error=0;
101 err1:
102  my_free(buffer);
103 err0:
104  my_close(fd, MYF(MY_WME));
105  return error;
106  }
107  else
108  {
109  /* compatibility mode: to be removed */
110  char **sws=(char **)ft_precompiled_stopwords;
111 
112  for (;*sws;sws++)
113  {
114  if (ft_add_stopword(*sws))
115  return -1;
116  }
117  ft_stopword_file="(built-in)"; /* for SHOW VARIABLES */
118  }
119  return 0;
120 }
121 
122 int is_stopword(char *word, uint len)
123 {
124  FT_STOPWORD sw;
125  sw.pos=word;
126  sw.len=len;
127  return tree_search(stopwords3,&sw, stopwords3->custom_arg) != NULL;
128 }
129 
130 
131 void ft_free_stopwords()
132 {
133  if (stopwords3)
134  {
135  delete_tree(stopwords3); /* purecov: inspected */
136  my_free(stopwords3);
137  stopwords3=0;
138  }
139  ft_stopword_file= 0;
140 }