1 /* Copyright (C) 2006 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
2 This program is free software; you can redistribute it and/or modify
3 it under the terms of the GNU General Public License as published by
4 the Free Software Foundation; version 2 of the License.
5 This program is distributed in the hope that it will be useful,
6 but WITHOUT ANY WARRANTY; without even the implied warranty of
7 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8 GNU General Public License for more details.
9 You should have received a copy of the GNU General Public License
10 along with this program; if not, write to the Free Software
11 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
12
13 /* Written by Sergei A. Golubchik, who has a shared copyright to this code
14 added support for long options (my_getopt) 22.5.2002 by Jani Tolonen */
15
16 #include "ma_ftdefs.h"
17 #include "maria_ft_eval.h"
18 #include <stdarg.h>
19 #include <my_getopt.h>
20
21 static void print_error(int exit_code, const char *fmt,...);
22 static void get_options(int argc, char *argv[]);
23 static int create_record(char *pos, FILE *file);
24 static void usage();
25
26 static struct my_option my_long_options[] =
27 {
28 {"", 's', "", 0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
29 {"", 'q', "", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
30 {"", 'S', "", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
31 {"", '#', "", 0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
32 {"", 'V', "", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
33 {"", '?', "", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
34 {"", 'h', "", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
35 { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
36 };
37
main(int argc,char * argv[])38 int main(int argc, char *argv[])
39 {
40 MARIA_HA *file;
41 int i,j;
42
43 MY_INIT(argv[0]);
44 get_options(argc,argv);
45 bzero((char*)recinfo,sizeof(recinfo));
46
47 maria_init();
48 /* First define 2 columns */
49 recinfo[0].type=FIELD_SKIP_ENDSPACE;
50 recinfo[0].length=docid_length;
51 recinfo[1].type=FIELD_BLOB;
52 recinfo[1].length= 4+portable_sizeof_char_ptr;
53
54 /* Define a key over the first column */
55 keyinfo[0].seg=keyseg;
56 keyinfo[0].keysegs=1;
57 keyinfo[0].block_length= 0; /* Default block length */
58 keyinfo[0].seg[0].type= HA_KEYTYPE_TEXT;
59 keyinfo[0].seg[0].flag= HA_BLOB_PART;
60 keyinfo[0].seg[0].start=recinfo[0].length;
61 keyinfo[0].seg[0].length=key_length;
62 keyinfo[0].seg[0].null_bit=0;
63 keyinfo[0].seg[0].null_pos=0;
64 keyinfo[0].seg[0].bit_start=4;
65 keyinfo[0].seg[0].language=MY_CHARSET_CURRENT;
66 keyinfo[0].flag = HA_FULLTEXT;
67
68 if (!silent)
69 printf("- Creating isam-file\n");
70 if (maria_create(filename,1,keyinfo,2,recinfo,0,NULL,(MARIA_CREATE_INFO*) 0,0))
71 goto err;
72 if (!(file=maria_open(filename,2,0)))
73 goto err;
74 if (!silent)
75 printf("Initializing stopwords\n");
76 maria_ft_init_stopwords(stopwordlist);
77
78 if (!silent)
79 printf("- Writing key:s\n");
80
81 my_errno=0;
82 i=0;
83 while (create_record(record,df))
84 {
85 error=maria_write(file,record);
86 if (error)
87 printf("I= %2d maria_write: %d errno: %d\n",i,error,my_errno);
88 i++;
89 }
90 fclose(df);
91
92 if (maria_close(file)) goto err;
93 if (!silent)
94 printf("- Reopening file\n");
95 if (!(file=maria_open(filename,2,0))) goto err;
96 if (!silent)
97 printf("- Reading rows with key\n");
98 for (i=1;create_record(record,qf);i++)
99 {
100 FT_DOCLIST *result;
101 double w;
102 int t, err;
103
104 result=maria_ft_nlq_init_search(file,0,blob_record,(uint) strlen(blob_record),1);
105 if (!result)
106 {
107 printf("Query %d failed with errno %3d\n",i,my_errno);
108 goto err;
109 }
110 if (!silent)
111 printf("Query %d. Found: %d.\n",i,result->ndocs);
112 for (j=0;(err=maria_ft_nlq_read_next(result, read_record))==0;j++)
113 {
114 t=uint2korr(read_record);
115 w=maria_ft_nlq_get_relevance(result);
116 printf("%d %.*s %f\n",i,t,read_record+2,w);
117 }
118 if (err != HA_ERR_END_OF_FILE)
119 {
120 printf("maria_ft_read_next %d failed with errno %3d\n",j,my_errno);
121 goto err;
122 }
123 maria_ft_nlq_close_search(result);
124 }
125
126 if (maria_close(file)) goto err;
127 maria_end();
128 my_end(MY_CHECK_ERROR);
129
130 return (0);
131
132 err:
133 printf("got error: %3d when using maria-database\n",my_errno);
134 return 1; /* skip warning */
135
136 }
137
138
139 static my_bool
get_one_option(int optid,const struct my_option * opt,char * argument)140 get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
141 char *argument)
142 {
143 switch (optid) {
144 case 's':
145 if (stopwordlist && stopwordlist != maria_ft_precompiled_stopwords)
146 break;
147 {
148 FILE *f; char s[HA_FT_MAXLEN]; int i=0,n=SWL_INIT;
149
150 if (!(stopwordlist=(const char**) malloc(n*sizeof(char *))))
151 print_error(1,"malloc(%d)",n*sizeof(char *));
152 if (!(f=fopen(argument,"r")))
153 print_error(1,"fopen(%s)",argument);
154 while (!feof(f))
155 {
156 if (!(fgets(s,HA_FT_MAXLEN,f)))
157 print_error(1,"fgets(s,%d,%s)",HA_FT_MAXLEN,argument);
158 if (!(stopwordlist[i++]=strdup(s)))
159 print_error(1,"strdup(%s)",s);
160 if (i >= n)
161 {
162 n+=SWL_PLUS;
163 if (!(stopwordlist=(const char**) realloc((char*) stopwordlist,
164 n*sizeof(char *))))
165 print_error(1,"realloc(%d)",n*sizeof(char *));
166 }
167 }
168 fclose(f);
169 stopwordlist[i]=NULL;
170 break;
171 }
172 case 'q': silent=1; break;
173 case 'S': if (stopwordlist==maria_ft_precompiled_stopwords) stopwordlist=NULL; break;
174 case '#':
175 DBUG_PUSH (argument);
176 break;
177 case 'V':
178 case '?':
179 case 'h':
180 usage();
181 exit(1);
182 }
183 return 0;
184 }
185
186
get_options(int argc,char * argv[])187 static void get_options(int argc, char *argv[])
188 {
189 int ho_error;
190
191 if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option)))
192 exit(ho_error);
193
194 if (!(d_file=argv[optind])) print_error(1,"No d_file");
195 if (!(df=fopen(d_file,"r")))
196 print_error(1,"fopen(%s)",d_file);
197 if (!(q_file=argv[optind+1])) print_error(1,"No q_file");
198 if (!(qf=fopen(q_file,"r")))
199 print_error(1,"fopen(%s)",q_file);
200 return;
201 } /* get options */
202
203
create_record(char * pos,FILE * file)204 static int create_record(char *pos, FILE *file)
205 {
206 uint tmp; char *ptr;
207
208 bzero((char *)pos,MAX_REC_LENGTH);
209
210 /* column 1 - VARCHAR */
211 if (!(fgets(pos+2,MAX_REC_LENGTH-32,file)))
212 {
213 if (feof(file))
214 return 0;
215 else
216 print_error(1,"fgets(docid) - 1");
217 }
218 tmp=(uint) strlen(pos+2)-1;
219 int2store(pos,tmp);
220 pos+=recinfo[0].length;
221
222 /* column 2 - BLOB */
223
224 if (!(fgets(blob_record,MAX_BLOB_LENGTH,file)))
225 print_error(1,"fgets(docid) - 2");
226 tmp=(uint) strlen(blob_record);
227 int4store(pos,tmp);
228 ptr=blob_record;
229 memcpy_fixed(pos+4,&ptr,sizeof(char*));
230 return 1;
231 }
232
233 /* VARARGS */
234
print_error(int exit_code,const char * fmt,...)235 static void print_error(int exit_code, const char *fmt,...)
236 {
237 va_list args;
238
239 va_start(args,fmt);
240 fprintf(stderr,"%s: error: ",my_progname);
241 VOID(vfprintf(stderr, fmt, args));
242 VOID(fputc('\n',stderr));
243 fflush(stderr);
244 va_end(args);
245 exit(exit_code);
246 }
247
248
usage()249 static void usage()
250 {
251 printf("%s [options]\n", my_progname);
252 my_print_help(my_long_options);
253 my_print_variables(my_long_options);
254 }
255