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 */
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 */
19
#include <my_getopt.h>
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);
26
static struct my_option my_long_options[] =
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}
38
int main(int argc, char *argv[])
44
get_options(argc,argv);
45
bzero((char*)recinfo,sizeof(recinfo));
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;
53
/* Define a key over the first column */
54
keyinfo[0].seg=keyseg;
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;
68
printf("- Creating isam-file\n");
69
if (mi_create(filename,1,keyinfo,2,recinfo,0,NULL,(MI_CREATE_INFO*) 0,0))
71
if (!(file=mi_open(filename,2,0)))
74
printf("Initializing stopwords\n");
75
ft_init_stopwords(stopwordlist);
78
printf("- Writing key:s\n");
82
while (create_record(record,df))
84
error=mi_write(file,record);
86
printf("I= %2d mi_write: %d errno: %d\n",i,error,my_errno);
91
if (mi_close(file)) goto err;
93
printf("- Reopening file\n");
94
if (!(file=mi_open(filename,2,0))) goto err;
96
printf("- Reading rows with key\n");
97
for (i=1;create_record(record,qf);i++)
103
result=ft_nlq_init_search(file,0,blob_record,(uint) strlen(blob_record),1);
106
printf("Query %d failed with errno %3d\n",i,my_errno);
110
printf("Query %d. Found: %d.\n",i,result->ndocs);
111
for (j=0;(err=ft_nlq_read_next(result, read_record))==0;j++)
113
t=uint2korr(read_record);
114
w=ft_nlq_get_relevance(result);
115
printf("%d %.*s %f\n",i,t,read_record+2,w);
117
if (err != HA_ERR_END_OF_FILE)
119
printf("ft_read_next %d failed with errno %3d\n",j,my_errno);
122
ft_nlq_close_search(result);
125
if (mi_close(file)) goto err;
126
my_end(MY_CHECK_ERROR);
131
printf("got error: %3d when using myisam-database\n",my_errno);
132
return 1; /* skip warning */
138
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
143
if (stopwordlist && stopwordlist != ft_precompiled_stopwords)
146
FILE *f; char s[HA_FT_MAXLEN]; int i=0,n=SWL_INIT;
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);
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);
161
if (!(stopwordlist=(const char**) realloc((char*) stopwordlist,
163
print_error(1,"realloc(%d)",n*sizeof(char *));
167
stopwordlist[i]=NULL;
170
case 'q': silent=1; break;
171
case 'S': if (stopwordlist==ft_precompiled_stopwords) stopwordlist=NULL; break;
173
DBUG_PUSH (argument);
185
static void get_options(int argc, char *argv[])
189
if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option)))
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);
202
static int create_record(char *pos, FILE *file)
206
bzero((char *)pos,MAX_REC_LENGTH);
208
/* column 1 - VARCHAR */
209
if (!(fgets(pos+2,MAX_REC_LENGTH-32,file)))
214
print_error(1,"fgets(docid) - 1");
216
tmp=(uint) strlen(pos+2)-1;
218
pos+=recinfo[0].length;
220
/* column 2 - BLOB */
222
if (!(fgets(blob_record,MAX_BLOB_LENGTH,file)))
223
print_error(1,"fgets(docid) - 2");
224
tmp=(uint) strlen(blob_record);
227
memcpy_fixed(pos+4,&ptr,sizeof(char*));
233
static void print_error(int exit_code, const char *fmt,...)
238
fprintf(stderr,"%s: error: ",my_progname);
239
VOID(vfprintf(stderr, fmt, args));
240
VOID(fputc('\n',stderr));
249
printf("%s [options]\n", my_progname);
250
my_print_help(my_long_options);
251
my_print_variables(my_long_options);