00001 /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB 00002 This program is free software; you can redistribute it and/or modify 00003 it under the terms of the GNU General Public License as published by 00004 the Free Software Foundation; either version 2 of the License, or 00005 (at your option) any later version. 00006 This program is distributed in the hope that it will be useful, 00007 but WITHOUT ANY WARRANTY; without even the implied warranty of 00008 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00009 GNU General Public License for more details. 00010 You should have received a copy of the GNU General Public License 00011 along with this program; if not, write to the Free Software 00012 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 00013 00014 /* Written by Sergei A. Golubchik, who has a shared copyright to this code 00015 added support for long options (my_getopt) 22.5.2002 by Jani Tolonen */ 00016 00017 #include "ftdefs.h" 00018 #include "ft_eval.h" 00019 #include <stdarg.h> 00020 #include <my_getopt.h> 00021 00022 static void print_error(int exit_code, const char *fmt,...); 00023 static void get_options(int argc, char *argv[]); 00024 static int create_record(char *pos, FILE *file); 00025 static void usage(); 00026 00027 static struct my_option my_long_options[] = 00028 { 00029 {"", 's', "", 0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, 00030 {"", 'q', "", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}, 00031 {"", 'S', "", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}, 00032 {"", '#', "", 0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, 00033 {"", 'V', "", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}, 00034 {"", '?', "", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}, 00035 {"", 'h', "", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}, 00036 { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0} 00037 }; 00038 00039 int main(int argc, char *argv[]) 00040 { 00041 MI_INFO *file; 00042 int i,j; 00043 00044 MY_INIT(argv[0]); 00045 get_options(argc,argv); 00046 bzero((char*)recinfo,sizeof(recinfo)); 00047 00048 /* First define 2 columns */ 00049 recinfo[0].type=FIELD_SKIP_ENDSPACE; 00050 recinfo[0].length=docid_length; 00051 recinfo[1].type=FIELD_BLOB; 00052 recinfo[1].length= 4+mi_portable_sizeof_char_ptr; 00053 00054 /* Define a key over the first column */ 00055 keyinfo[0].seg=keyseg; 00056 keyinfo[0].keysegs=1; 00057 keyinfo[0].block_length= 0; /* Default block length */ 00058 keyinfo[0].seg[0].type= HA_KEYTYPE_TEXT; 00059 keyinfo[0].seg[0].flag= HA_BLOB_PART; 00060 keyinfo[0].seg[0].start=recinfo[0].length; 00061 keyinfo[0].seg[0].length=key_length; 00062 keyinfo[0].seg[0].null_bit=0; 00063 keyinfo[0].seg[0].null_pos=0; 00064 keyinfo[0].seg[0].bit_start=4; 00065 keyinfo[0].seg[0].language=MY_CHARSET_CURRENT; 00066 keyinfo[0].flag = HA_FULLTEXT; 00067 00068 if (!silent) 00069 printf("- Creating isam-file\n"); 00070 if (mi_create(filename,1,keyinfo,2,recinfo,0,NULL,(MI_CREATE_INFO*) 0,0)) 00071 goto err; 00072 if (!(file=mi_open(filename,2,0))) 00073 goto err; 00074 if (!silent) 00075 printf("Initializing stopwords\n"); 00076 ft_init_stopwords(stopwordlist); 00077 00078 if (!silent) 00079 printf("- Writing key:s\n"); 00080 00081 my_errno=0; 00082 i=0; 00083 while (create_record(record,df)) 00084 { 00085 error=mi_write(file,record); 00086 if (error) 00087 printf("I= %2d mi_write: %d errno: %d\n",i,error,my_errno); 00088 i++; 00089 } 00090 fclose(df); 00091 00092 if (mi_close(file)) goto err; 00093 if (!silent) 00094 printf("- Reopening file\n"); 00095 if (!(file=mi_open(filename,2,0))) goto err; 00096 if (!silent) 00097 printf("- Reading rows with key\n"); 00098 for (i=1;create_record(record,qf);i++) 00099 { 00100 FT_DOCLIST *result; 00101 double w; 00102 int t, err; 00103 00104 result=ft_nlq_init_search(file,0,blob_record,(uint) strlen(blob_record),1); 00105 if (!result) 00106 { 00107 printf("Query %d failed with errno %3d\n",i,my_errno); 00108 goto err; 00109 } 00110 if (!silent) 00111 printf("Query %d. Found: %d.\n",i,result->ndocs); 00112 for (j=0;(err=ft_nlq_read_next(result, read_record))==0;j++) 00113 { 00114 t=uint2korr(read_record); 00115 w=ft_nlq_get_relevance(result); 00116 printf("%d %.*s %f\n",i,t,read_record+2,w); 00117 } 00118 if (err != HA_ERR_END_OF_FILE) 00119 { 00120 printf("ft_read_next %d failed with errno %3d\n",j,my_errno); 00121 goto err; 00122 } 00123 ft_nlq_close_search(result); 00124 } 00125 00126 if (mi_close(file)) goto err; 00127 my_end(MY_CHECK_ERROR); 00128 00129 return (0); 00130 00131 err: 00132 printf("got error: %3d when using myisam-database\n",my_errno); 00133 return 1; /* skip warning */ 00134 00135 } 00136 00137 00138 static my_bool 00139 get_one_option(int optid, const struct my_option *opt __attribute__((unused)), 00140 char *argument) 00141 { 00142 switch (optid) { 00143 case 's': 00144 if (stopwordlist && stopwordlist != ft_precompiled_stopwords) 00145 break; 00146 { 00147 FILE *f; char s[HA_FT_MAXLEN]; int i=0,n=SWL_INIT; 00148 00149 if (!(stopwordlist=(const char**) malloc(n*sizeof(char *)))) 00150 print_error(1,"malloc(%d)",n*sizeof(char *)); 00151 if (!(f=fopen(argument,"r"))) 00152 print_error(1,"fopen(%s)",argument); 00153 while (!feof(f)) 00154 { 00155 if (!(fgets(s,HA_FT_MAXLEN,f))) 00156 print_error(1,"fgets(s,%d,%s)",HA_FT_MAXLEN,argument); 00157 if (!(stopwordlist[i++]=strdup(s))) 00158 print_error(1,"strdup(%s)",s); 00159 if (i >= n) 00160 { 00161 n+=SWL_PLUS; 00162 if (!(stopwordlist=(const char**) realloc((char*) stopwordlist, 00163 n*sizeof(char *)))) 00164 print_error(1,"realloc(%d)",n*sizeof(char *)); 00165 } 00166 } 00167 fclose(f); 00168 stopwordlist[i]=NULL; 00169 break; 00170 } 00171 case 'q': silent=1; break; 00172 case 'S': if (stopwordlist==ft_precompiled_stopwords) stopwordlist=NULL; break; 00173 case '#': 00174 DBUG_PUSH (argument); 00175 break; 00176 case 'V': 00177 case '?': 00178 case 'h': 00179 usage(); 00180 exit(1); 00181 } 00182 return 0; 00183 } 00184 00185 00186 static void get_options(int argc, char *argv[]) 00187 { 00188 int ho_error; 00189 00190 if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option))) 00191 exit(ho_error); 00192 00193 if (!(d_file=argv[optind])) print_error(1,"No d_file"); 00194 if (!(df=fopen(d_file,"r"))) 00195 print_error(1,"fopen(%s)",d_file); 00196 if (!(q_file=argv[optind+1])) print_error(1,"No q_file"); 00197 if (!(qf=fopen(q_file,"r"))) 00198 print_error(1,"fopen(%s)",q_file); 00199 return; 00200 } /* get options */ 00201 00202 00203 static int create_record(char *pos, FILE *file) 00204 { 00205 uint tmp; char *ptr; 00206 00207 bzero((char *)pos,MAX_REC_LENGTH); 00208 00209 /* column 1 - VARCHAR */ 00210 if (!(fgets(pos+2,MAX_REC_LENGTH-32,file))) 00211 { 00212 if (feof(file)) 00213 return 0; 00214 else 00215 print_error(1,"fgets(docid) - 1"); 00216 } 00217 tmp=(uint) strlen(pos+2)-1; 00218 int2store(pos,tmp); 00219 pos+=recinfo[0].length; 00220 00221 /* column 2 - BLOB */ 00222 00223 if (!(fgets(blob_record,MAX_BLOB_LENGTH,file))) 00224 print_error(1,"fgets(docid) - 2"); 00225 tmp=(uint) strlen(blob_record); 00226 int4store(pos,tmp); 00227 ptr=blob_record; 00228 memcpy_fixed(pos+4,&ptr,sizeof(char*)); 00229 return 1; 00230 } 00231 00232 /* VARARGS */ 00233 00234 static void print_error(int exit_code, const char *fmt,...) 00235 { 00236 va_list args; 00237 00238 va_start(args,fmt); 00239 fprintf(stderr,"%s: error: ",my_progname); 00240 VOID(vfprintf(stderr, fmt, args)); 00241 VOID(fputc('\n',stderr)); 00242 fflush(stderr); 00243 va_end(args); 00244 exit(exit_code); 00245 } 00246 00247 00248 static void usage() 00249 { 00250 printf("%s [options]\n", my_progname); 00251 my_print_help(my_long_options); 00252 my_print_variables(my_long_options); 00253 }
1.4.7

