1
by brian
clean slate |
1 |
/* Copyright (C) 2000-2002 MySQL 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "ftdefs.h" |
|
17 |
#include "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 |
||
38 |
int main(int argc, char *argv[]) |
|
39 |
{
|
|
40 |
MI_INFO *file; |
|
41 |
int i,j; |
|
42 |
||
43 |
MY_INIT(argv[0]); |
|
44 |
get_options(argc,argv); |
|
45 |
bzero((char*)recinfo,sizeof(recinfo)); |
|
46 |
||
47 |
/* First define 2 columns */
|
|
48 |
recinfo[0].type=FIELD_SKIP_ENDSPACE; |
|
49 |
recinfo[0].length=docid_length; |
|
50 |
recinfo[1].type=FIELD_BLOB; |
|
51 |
recinfo[1].length= 4+portable_sizeof_char_ptr; |
|
52 |
||
53 |
/* Define a key over the first column */
|
|
54 |
keyinfo[0].seg=keyseg; |
|
55 |
keyinfo[0].keysegs=1; |
|
56 |
keyinfo[0].block_length= 0; /* Default block length */ |
|
57 |
keyinfo[0].seg[0].type= HA_KEYTYPE_TEXT; |
|
58 |
keyinfo[0].seg[0].flag= HA_BLOB_PART; |
|
59 |
keyinfo[0].seg[0].start=recinfo[0].length; |
|
60 |
keyinfo[0].seg[0].length=key_length; |
|
61 |
keyinfo[0].seg[0].null_bit=0; |
|
62 |
keyinfo[0].seg[0].null_pos=0; |
|
63 |
keyinfo[0].seg[0].bit_start=4; |
|
64 |
keyinfo[0].seg[0].language=MY_CHARSET_CURRENT; |
|
65 |
keyinfo[0].flag = HA_FULLTEXT; |
|
66 |
||
67 |
if (!silent) |
|
68 |
printf("- Creating isam-file\n"); |
|
69 |
if (mi_create(filename,1,keyinfo,2,recinfo,0,NULL,(MI_CREATE_INFO*) 0,0)) |
|
70 |
goto err; |
|
71 |
if (!(file=mi_open(filename,2,0))) |
|
72 |
goto err; |
|
73 |
if (!silent) |
|
74 |
printf("Initializing stopwords\n"); |
|
75 |
ft_init_stopwords(stopwordlist); |
|
76 |
||
77 |
if (!silent) |
|
78 |
printf("- Writing key:s\n"); |
|
79 |
||
80 |
my_errno=0; |
|
81 |
i=0; |
|
82 |
while (create_record(record,df)) |
|
83 |
{
|
|
84 |
error=mi_write(file,record); |
|
85 |
if (error) |
|
86 |
printf("I= %2d mi_write: %d errno: %d\n",i,error,my_errno); |
|
87 |
i++; |
|
88 |
}
|
|
89 |
fclose(df); |
|
90 |
||
91 |
if (mi_close(file)) goto err; |
|
92 |
if (!silent) |
|
93 |
printf("- Reopening file\n"); |
|
94 |
if (!(file=mi_open(filename,2,0))) goto err; |
|
95 |
if (!silent) |
|
96 |
printf("- Reading rows with key\n"); |
|
97 |
for (i=1;create_record(record,qf);i++) |
|
98 |
{
|
|
99 |
FT_DOCLIST *result; |
|
100 |
double w; |
|
101 |
int t, err; |
|
102 |
||
103 |
result=ft_nlq_init_search(file,0,blob_record,(uint) strlen(blob_record),1); |
|
104 |
if (!result) |
|
105 |
{
|
|
106 |
printf("Query %d failed with errno %3d\n",i,my_errno); |
|
107 |
goto err; |
|
108 |
}
|
|
109 |
if (!silent) |
|
110 |
printf("Query %d. Found: %d.\n",i,result->ndocs); |
|
111 |
for (j=0;(err=ft_nlq_read_next(result, read_record))==0;j++) |
|
112 |
{
|
|
113 |
t=uint2korr(read_record); |
|
114 |
w=ft_nlq_get_relevance(result); |
|
115 |
printf("%d %.*s %f\n",i,t,read_record+2,w); |
|
116 |
}
|
|
117 |
if (err != HA_ERR_END_OF_FILE) |
|
118 |
{
|
|
119 |
printf("ft_read_next %d failed with errno %3d\n",j,my_errno); |
|
120 |
goto err; |
|
121 |
}
|
|
122 |
ft_nlq_close_search(result); |
|
123 |
}
|
|
124 |
||
125 |
if (mi_close(file)) goto err; |
|
126 |
my_end(MY_CHECK_ERROR); |
|
127 |
||
128 |
return (0); |
|
129 |
||
130 |
err: |
|
131 |
printf("got error: %3d when using myisam-database\n",my_errno); |
|
132 |
return 1; /* skip warning */ |
|
133 |
||
134 |
}
|
|
135 |
||
136 |
||
137 |
static my_bool |
|
138 |
get_one_option(int optid, const struct my_option *opt __attribute__((unused)), |
|
139 |
char *argument) |
|
140 |
{
|
|
141 |
switch (optid) { |
|
142 |
case 's': |
|
143 |
if (stopwordlist && stopwordlist != ft_precompiled_stopwords) |
|
144 |
break; |
|
145 |
{
|
|
146 |
FILE *f; char s[HA_FT_MAXLEN]; int i=0,n=SWL_INIT; |
|
147 |
||
148 |
if (!(stopwordlist=(const char**) malloc(n*sizeof(char *)))) |
|
149 |
print_error(1,"malloc(%d)",n*sizeof(char *)); |
|
150 |
if (!(f=fopen(argument,"r"))) |
|
151 |
print_error(1,"fopen(%s)",argument); |
|
152 |
while (!feof(f)) |
|
153 |
{
|
|
154 |
if (!(fgets(s,HA_FT_MAXLEN,f))) |
|
155 |
print_error(1,"fgets(s,%d,%s)",HA_FT_MAXLEN,argument); |
|
156 |
if (!(stopwordlist[i++]=strdup(s))) |
|
157 |
print_error(1,"strdup(%s)",s); |
|
158 |
if (i >= n) |
|
159 |
{
|
|
160 |
n+=SWL_PLUS; |
|
161 |
if (!(stopwordlist=(const char**) realloc((char*) stopwordlist, |
|
162 |
n*sizeof(char *)))) |
|
163 |
print_error(1,"realloc(%d)",n*sizeof(char *)); |
|
164 |
}
|
|
165 |
}
|
|
166 |
fclose(f); |
|
167 |
stopwordlist[i]=NULL; |
|
168 |
break; |
|
169 |
}
|
|
170 |
case 'q': silent=1; break; |
|
171 |
case 'S': if (stopwordlist==ft_precompiled_stopwords) stopwordlist=NULL; break; |
|
172 |
case '#': |
|
173 |
DBUG_PUSH (argument); |
|
174 |
break; |
|
175 |
case 'V': |
|
176 |
case '?': |
|
177 |
case 'h': |
|
178 |
usage(); |
|
179 |
exit(1); |
|
180 |
}
|
|
181 |
return 0; |
|
182 |
}
|
|
183 |
||
184 |
||
185 |
static void get_options(int argc, char *argv[]) |
|
186 |
{
|
|
187 |
int ho_error; |
|
188 |
||
189 |
if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option))) |
|
190 |
exit(ho_error); |
|
191 |
||
192 |
if (!(d_file=argv[optind])) print_error(1,"No d_file"); |
|
193 |
if (!(df=fopen(d_file,"r"))) |
|
194 |
print_error(1,"fopen(%s)",d_file); |
|
195 |
if (!(q_file=argv[optind+1])) print_error(1,"No q_file"); |
|
196 |
if (!(qf=fopen(q_file,"r"))) |
|
197 |
print_error(1,"fopen(%s)",q_file); |
|
198 |
return; |
|
199 |
} /* get options */ |
|
200 |
||
201 |
||
202 |
static int create_record(char *pos, FILE *file) |
|
203 |
{
|
|
204 |
uint tmp; char *ptr; |
|
205 |
||
206 |
bzero((char *)pos,MAX_REC_LENGTH); |
|
207 |
||
208 |
/* column 1 - VARCHAR */
|
|
209 |
if (!(fgets(pos+2,MAX_REC_LENGTH-32,file))) |
|
210 |
{
|
|
211 |
if (feof(file)) |
|
212 |
return 0; |
|
213 |
else
|
|
214 |
print_error(1,"fgets(docid) - 1"); |
|
215 |
}
|
|
216 |
tmp=(uint) strlen(pos+2)-1; |
|
217 |
int2store(pos,tmp); |
|
218 |
pos+=recinfo[0].length; |
|
219 |
||
220 |
/* column 2 - BLOB */
|
|
221 |
||
222 |
if (!(fgets(blob_record,MAX_BLOB_LENGTH,file))) |
|
223 |
print_error(1,"fgets(docid) - 2"); |
|
224 |
tmp=(uint) strlen(blob_record); |
|
225 |
int4store(pos,tmp); |
|
226 |
ptr=blob_record; |
|
227 |
memcpy_fixed(pos+4,&ptr,sizeof(char*)); |
|
228 |
return 1; |
|
229 |
}
|
|
230 |
||
231 |
/* VARARGS */
|
|
232 |
||
233 |
static void print_error(int exit_code, const char *fmt,...) |
|
234 |
{
|
|
235 |
va_list args; |
|
236 |
||
237 |
va_start(args,fmt); |
|
238 |
fprintf(stderr,"%s: error: ",my_progname); |
|
239 |
VOID(vfprintf(stderr, fmt, args)); |
|
240 |
VOID(fputc('\n',stderr)); |
|
241 |
fflush(stderr); |
|
242 |
va_end(args); |
|
243 |
exit(exit_code); |
|
244 |
}
|
|
245 |
||
246 |
||
247 |
static void usage() |
|
248 |
{
|
|
249 |
printf("%s [options]\n", my_progname); |
|
250 |
my_print_help(my_long_options); |
|
251 |
my_print_variables(my_long_options); |
|
252 |
}
|