1
/* Copyright (C) 2006 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
/* Written by Sergei A. Golubchik, who has a shared copyright to this code
17
added support for long options (my_getopt) 22.5.2002 by Jani Tolonen */
19
#include "ma_ftdefs.h"
20
#include "maria_ft_test1.h"
21
#include <my_getopt.h>
23
static int key_field=FIELD_VARCHAR,extra_field=FIELD_SKIP_ENDSPACE;
24
static uint key_length=200,extra_length=50;
25
static int key_type=HA_KEYTYPE_TEXT;
26
static int verbose=0,silent=0,skip_update=0,
27
no_keys=0,no_stopwords=0,no_search=0,no_fulltext=0;
28
static int create_flag=0,error=0;
30
#define MAX_REC_LENGTH 300
31
static char record[MAX_REC_LENGTH],read_record[MAX_REC_LENGTH];
33
static int run_test(const char *filename);
34
static void get_options(int argc, char *argv[]);
35
static void create_record(char *, int);
38
static struct my_option my_long_options[] =
40
{"", 'v', "", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
41
{"", '?', "", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
42
{"", 'h', "", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
43
{"", 'V', "", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
44
{"", 'v', "", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
45
{"", 's', "", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
46
{"", 'N', "", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
47
{"", 'S', "", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
48
{"", 'K', "", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
49
{"", 'F', "", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
50
{"", 'U', "", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
51
{"", '#', "", 0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
52
{ 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
55
int main(int argc, char *argv[])
59
get_options(argc,argv);
62
exit(run_test("FT1"));
65
static MARIA_COLUMNDEF recinfo[3];
66
static MARIA_KEYDEF keyinfo[2];
67
static HA_KEYSEG keyseg[10];
69
static int run_test(const char *filename)
75
bzero((char*) recinfo,sizeof(recinfo));
77
/* First define 2 columns */
78
recinfo[0].type=extra_field;
79
recinfo[0].length= (extra_field == FIELD_BLOB ? 4 + portable_sizeof_char_ptr :
81
if (extra_field == FIELD_VARCHAR)
82
recinfo[0].length+= HA_VARCHAR_PACKLENGTH(extra_length);
83
recinfo[1].type=key_field;
84
recinfo[1].length= (key_field == FIELD_BLOB ? 4+portable_sizeof_char_ptr :
86
if (key_field == FIELD_VARCHAR)
87
recinfo[1].length+= HA_VARCHAR_PACKLENGTH(key_length);
89
/* Define a key over the first column */
90
keyinfo[0].seg=keyseg;
92
keyinfo[0].block_length= 0; /* Default block length */
93
keyinfo[0].seg[0].type= key_type;
94
keyinfo[0].seg[0].flag= (key_field == FIELD_BLOB) ? HA_BLOB_PART:
95
(key_field == FIELD_VARCHAR) ? HA_VAR_LENGTH_PART:0;
96
keyinfo[0].seg[0].start=recinfo[0].length;
97
keyinfo[0].seg[0].length=key_length;
98
keyinfo[0].seg[0].null_bit= 0;
99
keyinfo[0].seg[0].null_pos=0;
100
keyinfo[0].seg[0].language= default_charset_info->number;
101
keyinfo[0].flag = (no_fulltext?HA_PACK_KEY:HA_FULLTEXT);
104
printf("- Creating isam-file\n");
105
if (maria_create(filename,(no_keys?0:1),keyinfo,2,recinfo,0,NULL,
106
(MARIA_CREATE_INFO*) 0, create_flag))
108
if (!(file=maria_open(filename,2,0)))
112
printf("- %s stopwords\n",no_stopwords?"Skipping":"Initializing");
113
maria_ft_init_stopwords(no_stopwords?NULL:maria_ft_precompiled_stopwords);
116
printf("- Writing key:s\n");
119
for (i=NUPD ; i<NDATAS; i++ )
121
create_record(record,i);
122
error=maria_write(file,record);
123
if (verbose || error)
124
printf("I= %2d maria_write: %d errno: %d, record: %s\n",
125
i,error,my_errno,data[i].f0);
131
printf("- Updating rows\n");
133
/* Read through all rows and update them */
136
while ((error=maria_rrnd(file,read_record,pos)) == 0)
138
create_record(record,NUPD-i-1);
139
if (maria_update(file,read_record,record))
141
printf("Can't update row: %.*s, error: %d\n",
142
keyinfo[0].seg[0].length,record,my_errno);
144
if(++i == NUPD) break;
148
printf("Found %d of %d rows\n", i,NUPD);
151
if (maria_close(file)) goto err;
152
if(no_search) return 0;
154
printf("- Reopening file\n");
155
if (!(file=maria_open(filename,2,0))) goto err;
157
printf("- Reading rows with key\n");
158
for (i=0 ; i < NQUERIES ; i++)
161
result=maria_ft_nlq_init_search(file,0,(char*) query[i],strlen(query[i]),1);
164
printf("Query %d: `%s' failed with errno %3d\n",i,query[i],my_errno);
167
printf("Query %d: `%s'. Found: %d. Top five documents:\n",
168
i,query[i],result->ndocs);
172
err= maria_ft_nlq_read_next(result, read_record);
173
if (err==HA_ERR_END_OF_FILE)
175
printf("No more matches!\n");
180
printf("maria_ft_read_next %d failed with errno %3d\n",j,my_errno);
183
w=maria_ft_nlq_get_relevance(result);
184
if (key_field == FIELD_VARCHAR)
188
p=recinfo[0].length+read_record;
190
printf("%10.7f: %.*s\n",w,(int) l,p+2);
193
printf("%10.7f: %.*s\n",w,recinfo[1].length,
194
recinfo[0].length+read_record);
196
maria_ft_nlq_close_search(result);
199
if (maria_close(file)) goto err;
201
my_end(MY_CHECK_ERROR);
205
printf("got error: %3d when using maria-database\n",my_errno);
206
return 1; /* skip warning */
209
static char blob_key[MAX_REC_LENGTH];
210
/* static char blob_record[MAX_REC_LENGTH+20*20]; */
212
void create_record(char *pos, int n)
214
bzero((char*) pos,MAX_REC_LENGTH);
215
if (recinfo[0].type == FIELD_BLOB)
219
strnmov(blob_key,data[n].f0,keyinfo[0].seg[0].length);
220
tmp=strlen(blob_key);
223
memcpy_fixed(pos+4,&ptr,sizeof(char*));
224
pos+=recinfo[0].length;
226
else if (recinfo[0].type == FIELD_VARCHAR)
229
/* -1 is here because pack_length is stored in seg->length */
230
uint pack_length= HA_VARCHAR_PACKLENGTH(keyinfo[0].seg[0].length-1);
231
strnmov(pos+pack_length,data[n].f0,keyinfo[0].seg[0].length);
232
tmp=strlen(pos+pack_length);
233
if (pack_length == 1)
237
pos+=recinfo[0].length;
241
strnmov(pos,data[n].f0,keyinfo[0].seg[0].length);
242
pos+=recinfo[0].length;
244
if (recinfo[1].type == FIELD_BLOB)
248
strnmov(blob_key,data[n].f2,keyinfo[0].seg[0].length);
249
tmp=strlen(blob_key);
252
memcpy_fixed(pos+4,&ptr,sizeof(char*));
253
pos+=recinfo[1].length;
255
else if (recinfo[1].type == FIELD_VARCHAR)
258
/* -1 is here because pack_length is stored in seg->length */
259
uint pack_length= HA_VARCHAR_PACKLENGTH(keyinfo[0].seg[0].length-1);
260
strnmov(pos+pack_length,data[n].f2,keyinfo[0].seg[0].length);
262
if (pack_length == 1)
266
pos+=recinfo[1].length;
270
strnmov(pos,data[n].f2,keyinfo[0].seg[0].length);
271
pos+=recinfo[1].length;
277
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
281
case 'v': verbose=1; break;
282
case 's': silent=1; break;
283
case 'F': no_fulltext=1; no_search=1;
284
case 'U': skip_update=1; break;
285
case 'K': no_keys=no_search=1; break;
286
case 'N': no_search=1; break;
287
case 'S': no_stopwords=1; break;
289
DBUG_PUSH (argument);
302
static void get_options(int argc,char *argv[])
306
if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option)))
314
printf("%s [options]\n", my_progname);
315
my_print_help(my_long_options);
316
my_print_variables(my_long_options);