~stewart/drizzle/embedded-innodb-create-select-transaction-arrgh

« back to all changes in this revision

Viewing changes to storage/myisam/ft_test1.c

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000-2002, 2004 MySQL AB
 
2
 
 
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.
 
6
 
 
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.
 
11
 
 
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 */
 
15
 
 
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 */
 
18
 
 
19
#include "ftdefs.h"
 
20
#include "ft_test1.h"
 
21
#include <my_getopt.h>
 
22
 
 
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;
 
29
 
 
30
#define MAX_REC_LENGTH 300
 
31
static char record[MAX_REC_LENGTH],read_record[MAX_REC_LENGTH];
 
32
 
 
33
static int run_test(const char *filename);
 
34
static void get_options(int argc, char *argv[]);
 
35
static void create_record(char *, int);
 
36
static void usage();
 
37
 
 
38
static struct my_option my_long_options[] =
 
39
{
 
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}
 
53
};
 
54
 
 
55
int main(int argc, char *argv[])
 
56
{
 
57
  MY_INIT(argv[0]);
 
58
 
 
59
  get_options(argc,argv);
 
60
 
 
61
  exit(run_test("FT1"));
 
62
}
 
63
 
 
64
static MI_COLUMNDEF recinfo[3];
 
65
static MI_KEYDEF keyinfo[2];
 
66
static HA_KEYSEG keyseg[10];
 
67
 
 
68
static int run_test(const char *filename)
 
69
{
 
70
  MI_INFO *file;
 
71
  int i,j;
 
72
  my_off_t pos;
 
73
 
 
74
  bzero((char*) recinfo,sizeof(recinfo));
 
75
 
 
76
  /* First define 2 columns */
 
77
  recinfo[0].type=extra_field;
 
78
  recinfo[0].length= (extra_field == FIELD_BLOB ? 4 + portable_sizeof_char_ptr :
 
79
              extra_length);
 
80
  if (extra_field == FIELD_VARCHAR)
 
81
    recinfo[0].length+= HA_VARCHAR_PACKLENGTH(extra_length);
 
82
  recinfo[1].type=key_field;
 
83
  recinfo[1].length= (key_field == FIELD_BLOB ? 4+portable_sizeof_char_ptr :
 
84
                      key_length);
 
85
  if (key_field == FIELD_VARCHAR)
 
86
    recinfo[1].length+= HA_VARCHAR_PACKLENGTH(key_length);
 
87
 
 
88
  /* Define a key over the first column */
 
89
  keyinfo[0].seg=keyseg;
 
90
  keyinfo[0].keysegs=1;
 
91
  keyinfo[0].block_length= 0;                   /* Default block length */
 
92
  keyinfo[0].seg[0].type= key_type;
 
93
  keyinfo[0].seg[0].flag= (key_field == FIELD_BLOB) ? HA_BLOB_PART:
 
94
                          (key_field == FIELD_VARCHAR) ? HA_VAR_LENGTH_PART:0;
 
95
  keyinfo[0].seg[0].start=recinfo[0].length;
 
96
  keyinfo[0].seg[0].length=key_length;
 
97
  keyinfo[0].seg[0].null_bit= 0;
 
98
  keyinfo[0].seg[0].null_pos=0;
 
99
  keyinfo[0].seg[0].language= default_charset_info->number;
 
100
  keyinfo[0].flag = (no_fulltext?HA_PACK_KEY:HA_FULLTEXT);
 
101
 
 
102
  if (!silent)
 
103
    printf("- Creating isam-file\n");
 
104
  if (mi_create(filename,(no_keys?0:1),keyinfo,2,recinfo,0,NULL,
 
105
                (MI_CREATE_INFO*) 0, create_flag))
 
106
    goto err;
 
107
  if (!(file=mi_open(filename,2,0)))
 
108
    goto err;
 
109
 
 
110
  if (!silent)
 
111
    printf("- %s stopwords\n",no_stopwords?"Skipping":"Initializing");
 
112
  ft_init_stopwords(no_stopwords?NULL:ft_precompiled_stopwords);
 
113
 
 
114
  if (!silent)
 
115
    printf("- Writing key:s\n");
 
116
 
 
117
  my_errno=0;
 
118
  for (i=NUPD ; i<NDATAS; i++ )
 
119
  {
 
120
    create_record(record,i);
 
121
    error=mi_write(file,record);
 
122
    if (verbose || error)
 
123
      printf("I= %2d  mi_write: %d  errno: %d, record: %s\n",
 
124
        i,error,my_errno,data[i].f0);
 
125
  }
 
126
 
 
127
  if (!skip_update)
 
128
  {
 
129
    if (!silent)
 
130
      printf("- Updating rows\n");
 
131
 
 
132
    /* Read through all rows and update them */
 
133
    pos=(ha_rows) 0;
 
134
    i=0;
 
135
    while ((error=mi_rrnd(file,read_record,pos)) == 0)
 
136
    {
 
137
      create_record(record,NUPD-i-1);
 
138
      if (mi_update(file,read_record,record))
 
139
      {
 
140
        printf("Can't update row: %.*s, error: %d\n",
 
141
               keyinfo[0].seg[0].length,record,my_errno);
 
142
      }
 
143
      if(++i == NUPD) break;
 
144
      pos=HA_OFFSET_ERROR;
 
145
    }
 
146
    if (i != NUPD)
 
147
      printf("Found %d of %d rows\n", i,NUPD);
 
148
  }
 
149
 
 
150
  if (mi_close(file)) goto err;
 
151
  if(no_search) return 0;
 
152
  if (!silent)
 
153
    printf("- Reopening file\n");
 
154
  if (!(file=mi_open(filename,2,0))) goto err;
 
155
  if (!silent)
 
156
    printf("- Reading rows with key\n");
 
157
  for (i=0 ; i < NQUERIES ; i++)
 
158
  {
 
159
    FT_DOCLIST *result;
 
160
    result=ft_nlq_init_search(file,0,(char*) query[i],strlen(query[i]),1);
 
161
    if(!result)
 
162
    {
 
163
      printf("Query %d: `%s' failed with errno %3d\n",i,query[i],my_errno);
 
164
      continue;
 
165
    }
 
166
    printf("Query %d: `%s'. Found: %d. Top five documents:\n",
 
167
           i,query[i],result->ndocs);
 
168
    for (j=0;j<5;j++)
 
169
    {
 
170
      double w; int err;
 
171
      err= ft_nlq_read_next(result, read_record);
 
172
      if (err==HA_ERR_END_OF_FILE)
 
173
      {
 
174
        printf("No more matches!\n");
 
175
        break;
 
176
      }
 
177
      else if (err)
 
178
      {
 
179
        printf("ft_read_next %d failed with errno %3d\n",j,my_errno);
 
180
        break;
 
181
      }
 
182
      w=ft_nlq_get_relevance(result);
 
183
      if (key_field == FIELD_VARCHAR)
 
184
      {
 
185
        uint l;
 
186
        char *p;
 
187
        p=recinfo[0].length+read_record;
 
188
        l=uint2korr(p);
 
189
        printf("%10.7f: %.*s\n",w,(int) l,p+2);
 
190
      }
 
191
      else
 
192
        printf("%10.7f: %.*s\n",w,recinfo[1].length,
 
193
               recinfo[0].length+read_record);
 
194
    }
 
195
    ft_nlq_close_search(result);
 
196
  }
 
197
 
 
198
  if (mi_close(file)) goto err;
 
199
  my_end(MY_CHECK_ERROR);
 
200
 
 
201
  return (0);
 
202
err:
 
203
  printf("got error: %3d when using myisam-database\n",my_errno);
 
204
  return 1;                     /* skip warning */
 
205
}
 
206
 
 
207
static char blob_key[MAX_REC_LENGTH];
 
208
/* static char blob_record[MAX_REC_LENGTH+20*20]; */
 
209
 
 
210
void create_record(char *pos, int n)
 
211
{
 
212
  bzero((char*) pos,MAX_REC_LENGTH);
 
213
  if (recinfo[0].type == FIELD_BLOB)
 
214
  {
 
215
    uint tmp;
 
216
    char *ptr;
 
217
    strnmov(blob_key,data[n].f0,keyinfo[0].seg[0].length);
 
218
    tmp=strlen(blob_key);
 
219
    int4store(pos,tmp);
 
220
    ptr=blob_key;
 
221
    memcpy_fixed(pos+4,&ptr,sizeof(char*));
 
222
    pos+=recinfo[0].length;
 
223
  }
 
224
  else if (recinfo[0].type == FIELD_VARCHAR)
 
225
  {
 
226
    uint tmp;
 
227
    /* -1 is here because pack_length is stored in seg->length */
 
228
    uint pack_length= HA_VARCHAR_PACKLENGTH(keyinfo[0].seg[0].length-1);
 
229
    strnmov(pos+pack_length,data[n].f0,keyinfo[0].seg[0].length);
 
230
    tmp=strlen(pos+pack_length);
 
231
    if (pack_length == 1)
 
232
      *pos= (char) tmp;
 
233
    else
 
234
      int2store(pos,tmp);
 
235
    pos+=recinfo[0].length;
 
236
  }
 
237
  else
 
238
  {
 
239
    strnmov(pos,data[n].f0,keyinfo[0].seg[0].length);
 
240
    pos+=recinfo[0].length;
 
241
  }
 
242
  if (recinfo[1].type == FIELD_BLOB)
 
243
  {
 
244
    uint tmp;
 
245
    char *ptr;
 
246
    strnmov(blob_key,data[n].f2,keyinfo[0].seg[0].length);
 
247
    tmp=strlen(blob_key);
 
248
    int4store(pos,tmp);
 
249
    ptr=blob_key;
 
250
    memcpy_fixed(pos+4,&ptr,sizeof(char*));
 
251
    pos+=recinfo[1].length;
 
252
  }
 
253
  else if (recinfo[1].type == FIELD_VARCHAR)
 
254
  {
 
255
    uint tmp;
 
256
    /* -1 is here because pack_length is stored in seg->length */
 
257
    uint pack_length= HA_VARCHAR_PACKLENGTH(keyinfo[0].seg[0].length-1);
 
258
    strnmov(pos+pack_length,data[n].f2,keyinfo[0].seg[0].length);
 
259
    tmp=strlen(pos+1);
 
260
    if (pack_length == 1)
 
261
      *pos= (char) tmp;
 
262
    else
 
263
      int2store(pos,tmp);
 
264
    pos+=recinfo[1].length;
 
265
  }
 
266
  else
 
267
  {
 
268
    strnmov(pos,data[n].f2,keyinfo[0].seg[0].length);
 
269
    pos+=recinfo[1].length;
 
270
  }
 
271
}
 
272
 
 
273
 
 
274
static my_bool
 
275
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
 
276
               char *argument)
 
277
{
 
278
  switch(optid) {
 
279
  case 'v': verbose=1; break;
 
280
  case 's': silent=1; break;
 
281
  case 'F': no_fulltext=1; no_search=1;
 
282
  case 'U': skip_update=1; break;
 
283
  case 'K': no_keys=no_search=1; break;
 
284
  case 'N': no_search=1; break;
 
285
  case 'S': no_stopwords=1; break;
 
286
  case '#':
 
287
    DBUG_PUSH (argument);
 
288
    break;
 
289
  case 'V':
 
290
  case '?':
 
291
  case 'h':
 
292
    usage();
 
293
    exit(1);
 
294
  }
 
295
  return 0;
 
296
}
 
297
 
 
298
/* Read options */
 
299
 
 
300
static void get_options(int argc,char *argv[])
 
301
{
 
302
  int ho_error;
 
303
 
 
304
  if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option)))
 
305
    exit(ho_error);
 
306
  return;
 
307
} /* get options */
 
308
 
 
309
 
 
310
static void usage()
 
311
{
 
312
  printf("%s [options]\n", my_progname);
 
313
  my_print_help(my_long_options);
 
314
  my_print_variables(my_long_options);
 
315
}