~ubuntu-branches/ubuntu/trusty/mariadb-5.5/trusty-proposed

« back to all changes in this revision

Viewing changes to storage/maria/ma_ft_test1.c

  • Committer: Package Import Robot
  • Author(s): Otto Kekäläinen
  • Date: 2013-12-22 10:27:05 UTC
  • Revision ID: package-import@ubuntu.com-20131222102705-mndw7s12mz0szrcn
Tags: upstream-5.5.32
Import upstream version 5.5.32

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2006 MySQL AB & MySQL Finland AB & TCX DataKonsult 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 "ma_ftdefs.h"
 
20
#include "maria_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
  maria_init();
 
61
 
 
62
  exit(run_test("FT1"));
 
63
}
 
64
 
 
65
static MARIA_COLUMNDEF recinfo[3];
 
66
static MARIA_KEYDEF keyinfo[2];
 
67
static HA_KEYSEG keyseg[10];
 
68
 
 
69
static int run_test(const char *filename)
 
70
{
 
71
  MARIA_HA *file;
 
72
  int i,j;
 
73
  my_off_t pos;
 
74
 
 
75
  bzero((char*) recinfo,sizeof(recinfo));
 
76
 
 
77
  /* First define 2 columns */
 
78
  recinfo[0].type=extra_field;
 
79
  recinfo[0].length= (extra_field == FIELD_BLOB ? 4 + portable_sizeof_char_ptr :
 
80
              extra_length);
 
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 :
 
85
                      key_length);
 
86
  if (key_field == FIELD_VARCHAR)
 
87
    recinfo[1].length+= HA_VARCHAR_PACKLENGTH(key_length);
 
88
 
 
89
  /* Define a key over the first column */
 
90
  keyinfo[0].seg=keyseg;
 
91
  keyinfo[0].keysegs=1;
 
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);
 
102
 
 
103
  if (!silent)
 
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))
 
107
    goto err;
 
108
  if (!(file=maria_open(filename,2,0)))
 
109
    goto err;
 
110
 
 
111
  if (!silent)
 
112
    printf("- %s stopwords\n",no_stopwords?"Skipping":"Initializing");
 
113
  maria_ft_init_stopwords(no_stopwords?NULL:maria_ft_precompiled_stopwords);
 
114
 
 
115
  if (!silent)
 
116
    printf("- Writing key:s\n");
 
117
 
 
118
  my_errno=0;
 
119
  for (i=NUPD ; i<NDATAS; i++ )
 
120
  {
 
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);
 
126
  }
 
127
 
 
128
  if (!skip_update)
 
129
  {
 
130
    if (!silent)
 
131
      printf("- Updating rows\n");
 
132
 
 
133
    /* Read through all rows and update them */
 
134
    pos=(ha_rows) 0;
 
135
    i=0;
 
136
    while ((error=maria_rrnd(file,read_record,pos)) == 0)
 
137
    {
 
138
      create_record(record,NUPD-i-1);
 
139
      if (maria_update(file,read_record,record))
 
140
      {
 
141
        printf("Can't update row: %.*s, error: %d\n",
 
142
               keyinfo[0].seg[0].length,record,my_errno);
 
143
      }
 
144
      if(++i == NUPD) break;
 
145
      pos=HA_OFFSET_ERROR;
 
146
    }
 
147
    if (i != NUPD)
 
148
      printf("Found %d of %d rows\n", i,NUPD);
 
149
  }
 
150
 
 
151
  if (maria_close(file)) goto err;
 
152
  if(no_search) return 0;
 
153
  if (!silent)
 
154
    printf("- Reopening file\n");
 
155
  if (!(file=maria_open(filename,2,0))) goto err;
 
156
  if (!silent)
 
157
    printf("- Reading rows with key\n");
 
158
  for (i=0 ; i < NQUERIES ; i++)
 
159
  {
 
160
    FT_DOCLIST *result;
 
161
    result=maria_ft_nlq_init_search(file,0,(char*) query[i],strlen(query[i]),1);
 
162
    if(!result)
 
163
    {
 
164
      printf("Query %d: `%s' failed with errno %3d\n",i,query[i],my_errno);
 
165
      continue;
 
166
    }
 
167
    printf("Query %d: `%s'. Found: %d. Top five documents:\n",
 
168
           i,query[i],result->ndocs);
 
169
    for (j=0;j<5;j++)
 
170
    {
 
171
      double w; int err;
 
172
      err= maria_ft_nlq_read_next(result, read_record);
 
173
      if (err==HA_ERR_END_OF_FILE)
 
174
      {
 
175
        printf("No more matches!\n");
 
176
        break;
 
177
      }
 
178
      else if (err)
 
179
      {
 
180
        printf("maria_ft_read_next %d failed with errno %3d\n",j,my_errno);
 
181
        break;
 
182
      }
 
183
      w=maria_ft_nlq_get_relevance(result);
 
184
      if (key_field == FIELD_VARCHAR)
 
185
      {
 
186
        uint l;
 
187
        char *p;
 
188
        p=recinfo[0].length+read_record;
 
189
        l=uint2korr(p);
 
190
        printf("%10.7f: %.*s\n",w,(int) l,p+2);
 
191
      }
 
192
      else
 
193
        printf("%10.7f: %.*s\n",w,recinfo[1].length,
 
194
               recinfo[0].length+read_record);
 
195
    }
 
196
    maria_ft_nlq_close_search(result);
 
197
  }
 
198
 
 
199
  if (maria_close(file)) goto err;
 
200
  maria_end();
 
201
  my_end(MY_CHECK_ERROR);
 
202
 
 
203
  return (0);
 
204
err:
 
205
  printf("got error: %3d when using maria-database\n",my_errno);
 
206
  return 1;                     /* skip warning */
 
207
}
 
208
 
 
209
static char blob_key[MAX_REC_LENGTH];
 
210
/* static char blob_record[MAX_REC_LENGTH+20*20]; */
 
211
 
 
212
void create_record(char *pos, int n)
 
213
{
 
214
  bzero((char*) pos,MAX_REC_LENGTH);
 
215
  if (recinfo[0].type == FIELD_BLOB)
 
216
  {
 
217
    uint tmp;
 
218
    char *ptr;
 
219
    strnmov(blob_key,data[n].f0,keyinfo[0].seg[0].length);
 
220
    tmp=strlen(blob_key);
 
221
    int4store(pos,tmp);
 
222
    ptr=blob_key;
 
223
    memcpy_fixed(pos+4,&ptr,sizeof(char*));
 
224
    pos+=recinfo[0].length;
 
225
  }
 
226
  else if (recinfo[0].type == FIELD_VARCHAR)
 
227
  {
 
228
    uint tmp;
 
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)
 
234
      *pos= (char) tmp;
 
235
    else
 
236
      int2store(pos,tmp);
 
237
    pos+=recinfo[0].length;
 
238
  }
 
239
  else
 
240
  {
 
241
    strnmov(pos,data[n].f0,keyinfo[0].seg[0].length);
 
242
    pos+=recinfo[0].length;
 
243
  }
 
244
  if (recinfo[1].type == FIELD_BLOB)
 
245
  {
 
246
    uint tmp;
 
247
    char *ptr;
 
248
    strnmov(blob_key,data[n].f2,keyinfo[0].seg[0].length);
 
249
    tmp=strlen(blob_key);
 
250
    int4store(pos,tmp);
 
251
    ptr=blob_key;
 
252
    memcpy_fixed(pos+4,&ptr,sizeof(char*));
 
253
    pos+=recinfo[1].length;
 
254
  }
 
255
  else if (recinfo[1].type == FIELD_VARCHAR)
 
256
  {
 
257
    uint tmp;
 
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);
 
261
    tmp=strlen(pos+1);
 
262
    if (pack_length == 1)
 
263
      *pos= (char) tmp;
 
264
    else
 
265
      int2store(pos,tmp);
 
266
    pos+=recinfo[1].length;
 
267
  }
 
268
  else
 
269
  {
 
270
    strnmov(pos,data[n].f2,keyinfo[0].seg[0].length);
 
271
    pos+=recinfo[1].length;
 
272
  }
 
273
}
 
274
 
 
275
 
 
276
static my_bool
 
277
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
 
278
               char *argument)
 
279
{
 
280
  switch(optid) {
 
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;
 
288
  case '#':
 
289
    DBUG_PUSH (argument);
 
290
    break;
 
291
  case 'V':
 
292
  case '?':
 
293
  case 'h':
 
294
    usage();
 
295
    exit(1);
 
296
  }
 
297
  return 0;
 
298
}
 
299
 
 
300
/* Read options */
 
301
 
 
302
static void get_options(int argc,char *argv[])
 
303
{
 
304
  int ho_error;
 
305
 
 
306
  if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option)))
 
307
    exit(ho_error);
 
308
  return;
 
309
} /* get options */
 
310
 
 
311
 
 
312
static void usage()
 
313
{
 
314
  printf("%s [options]\n", my_progname);
 
315
  my_print_help(my_long_options);
 
316
  my_print_variables(my_long_options);
 
317
}