~jlukas79/+junk/mysql-server

« back to all changes in this revision

Viewing changes to storage/maria/ma_ft_update.c

manual merge 6.0-main --> 6.0-bka-review

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
 
 
18
/* functions to work with full-text indices */
 
19
 
 
20
#include "ma_ftdefs.h"
 
21
#include <math.h>
 
22
 
 
23
void _ma_ft_segiterator_init(MARIA_HA *info, uint keynr, const uchar *record,
 
24
                             FT_SEG_ITERATOR *ftsi)
 
25
{
 
26
  DBUG_ENTER("_ma_ft_segiterator_init");
 
27
 
 
28
  ftsi->num=info->s->keyinfo[keynr].keysegs;
 
29
  ftsi->seg=info->s->keyinfo[keynr].seg;
 
30
  ftsi->rec=record;
 
31
  DBUG_VOID_RETURN;
 
32
}
 
33
 
 
34
void _ma_ft_segiterator_dummy_init(const uchar *record, uint len,
 
35
                                   FT_SEG_ITERATOR *ftsi)
 
36
{
 
37
  DBUG_ENTER("_ma_ft_segiterator_dummy_init");
 
38
 
 
39
  ftsi->num=1;
 
40
  ftsi->seg=0;
 
41
  ftsi->pos=record;
 
42
  ftsi->len=len;
 
43
  DBUG_VOID_RETURN;
 
44
}
 
45
 
 
46
/*
 
47
  This function breaks convention "return 0 in success"
 
48
  but it's easier to use like this
 
49
 
 
50
     while(_ma_ft_segiterator())
 
51
 
 
52
  so "1" means "OK", "0" means "EOF"
 
53
*/
 
54
 
 
55
uint _ma_ft_segiterator(register FT_SEG_ITERATOR *ftsi)
 
56
{
 
57
  DBUG_ENTER("_ma_ft_segiterator");
 
58
 
 
59
  if (!ftsi->num)
 
60
    DBUG_RETURN(0);
 
61
 
 
62
  ftsi->num--;
 
63
  if (!ftsi->seg)
 
64
    DBUG_RETURN(1);
 
65
 
 
66
  ftsi->seg--;
 
67
 
 
68
  if (ftsi->seg->null_bit &&
 
69
      (ftsi->rec[ftsi->seg->null_pos] & ftsi->seg->null_bit))
 
70
  {
 
71
    ftsi->pos=0;
 
72
    DBUG_RETURN(1);
 
73
  }
 
74
  ftsi->pos= ftsi->rec+ftsi->seg->start;
 
75
  if (ftsi->seg->flag & HA_VAR_LENGTH_PART)
 
76
  {
 
77
    uint pack_length= (ftsi->seg->bit_start);
 
78
    ftsi->len= (pack_length == 1 ? (uint) * ftsi->pos :
 
79
                uint2korr(ftsi->pos));
 
80
    ftsi->pos+= pack_length;                     /* Skip VARCHAR length */
 
81
    DBUG_RETURN(1);
 
82
  }
 
83
  if (ftsi->seg->flag & HA_BLOB_PART)
 
84
  {
 
85
    ftsi->len= _ma_calc_blob_length(ftsi->seg->bit_start,ftsi->pos);
 
86
    memcpy_fixed((char*) &ftsi->pos, ftsi->pos+ftsi->seg->bit_start,
 
87
                 sizeof(char*));
 
88
    DBUG_RETURN(1);
 
89
  }
 
90
  ftsi->len=ftsi->seg->length;
 
91
  DBUG_RETURN(1);
 
92
}
 
93
 
 
94
 
 
95
/* parses a document i.e. calls maria_ft_parse for every keyseg */
 
96
 
 
97
uint _ma_ft_parse(TREE *parsed, MARIA_HA *info, uint keynr, const uchar *record,
 
98
                  MYSQL_FTPARSER_PARAM *param, MEM_ROOT *mem_root)
 
99
{
 
100
  FT_SEG_ITERATOR ftsi;
 
101
  struct st_mysql_ftparser *parser;
 
102
  DBUG_ENTER("_ma_ft_parse");
 
103
 
 
104
  _ma_ft_segiterator_init(info, keynr, record, &ftsi);
 
105
 
 
106
  maria_ft_parse_init(parsed, info->s->keyinfo[keynr].seg->charset);
 
107
  parser= info->s->keyinfo[keynr].parser;
 
108
  while (_ma_ft_segiterator(&ftsi))
 
109
  {
 
110
    /** @todo this casts ftsi.pos (const) to non-const */
 
111
    if (ftsi.pos)
 
112
      if (maria_ft_parse(parsed, (uchar *)ftsi.pos, ftsi.len, parser, param,
 
113
                         mem_root))
 
114
        DBUG_RETURN(1);
 
115
  }
 
116
  DBUG_RETURN(0);
 
117
}
 
118
 
 
119
FT_WORD * _ma_ft_parserecord(MARIA_HA *info, uint keynr, const uchar *record,
 
120
                             MEM_ROOT *mem_root)
 
121
{
 
122
  TREE ptree;
 
123
  MYSQL_FTPARSER_PARAM *param;
 
124
  DBUG_ENTER("_ma_ft_parserecord");
 
125
  if (! (param= maria_ftparser_call_initializer(info, keynr, 0)))
 
126
    DBUG_RETURN(NULL);
 
127
  bzero((char*) &ptree, sizeof(ptree));
 
128
  param->flags= 0;
 
129
  if (_ma_ft_parse(&ptree, info, keynr, record, param, mem_root))
 
130
    DBUG_RETURN(NULL);
 
131
 
 
132
  DBUG_RETURN(maria_ft_linearize(&ptree, mem_root));
 
133
}
 
134
 
 
135
static int _ma_ft_store(MARIA_HA *info, uint keynr, uchar *keybuf,
 
136
                        FT_WORD *wlist, my_off_t filepos)
 
137
{
 
138
  DBUG_ENTER("_ma_ft_store");
 
139
 
 
140
  for (; wlist->pos; wlist++)
 
141
  {
 
142
    MARIA_KEY key;
 
143
    _ma_ft_make_key(info, &key, keynr, keybuf, wlist, filepos);
 
144
    if (_ma_ck_write(info, &key))
 
145
      DBUG_RETURN(1);
 
146
   }
 
147
   DBUG_RETURN(0);
 
148
}
 
149
 
 
150
static int _ma_ft_erase(MARIA_HA *info, uint keynr, uchar *keybuf,
 
151
                        FT_WORD *wlist, my_off_t filepos)
 
152
{
 
153
  uint err=0;
 
154
  DBUG_ENTER("_ma_ft_erase");
 
155
 
 
156
  for (; wlist->pos; wlist++)
 
157
  {
 
158
    MARIA_KEY key;
 
159
    _ma_ft_make_key(info, &key, keynr, keybuf, wlist, filepos);
 
160
    if (_ma_ck_delete(info, &key))
 
161
      err=1;
 
162
   }
 
163
   DBUG_RETURN(err);
 
164
}
 
165
 
 
166
/*
 
167
  Compares an appropriate parts of two WORD_KEY keys directly out of records
 
168
  returns 1 if they are different
 
169
*/
 
170
 
 
171
#define THOSE_TWO_DAMN_KEYS_ARE_REALLY_DIFFERENT 1
 
172
#define GEE_THEY_ARE_ABSOLUTELY_IDENTICAL        0
 
173
 
 
174
int _ma_ft_cmp(MARIA_HA *info, uint keynr, const uchar *rec1, const uchar *rec2)
 
175
{
 
176
  FT_SEG_ITERATOR ftsi1, ftsi2;
 
177
  CHARSET_INFO *cs=info->s->keyinfo[keynr].seg->charset;
 
178
  DBUG_ENTER("_ma_ft_cmp");
 
179
 
 
180
  _ma_ft_segiterator_init(info, keynr, rec1, &ftsi1);
 
181
  _ma_ft_segiterator_init(info, keynr, rec2, &ftsi2);
 
182
 
 
183
  while (_ma_ft_segiterator(&ftsi1) && _ma_ft_segiterator(&ftsi2))
 
184
  {
 
185
    if ((ftsi1.pos != ftsi2.pos) &&
 
186
        (!ftsi1.pos || !ftsi2.pos ||
 
187
         ha_compare_text(cs, ftsi1.pos,ftsi1.len,
 
188
                         ftsi2.pos,ftsi2.len,0,0)))
 
189
      DBUG_RETURN(THOSE_TWO_DAMN_KEYS_ARE_REALLY_DIFFERENT);
 
190
  }
 
191
  DBUG_RETURN(GEE_THEY_ARE_ABSOLUTELY_IDENTICAL);
 
192
}
 
193
 
 
194
 
 
195
/* update a document entry */
 
196
 
 
197
int _ma_ft_update(MARIA_HA *info, uint keynr, uchar *keybuf,
 
198
                  const uchar *oldrec, const uchar *newrec, my_off_t pos)
 
199
{
 
200
  int error= -1;
 
201
  FT_WORD *oldlist,*newlist, *old_word, *new_word;
 
202
  CHARSET_INFO *cs=info->s->keyinfo[keynr].seg->charset;
 
203
  int cmp, cmp2;
 
204
  DBUG_ENTER("_ma_ft_update");
 
205
 
 
206
  if (!(old_word=oldlist=_ma_ft_parserecord(info, keynr, oldrec,
 
207
                                            &info->ft_memroot)) ||
 
208
      !(new_word=newlist=_ma_ft_parserecord(info, keynr, newrec,
 
209
                                            &info->ft_memroot)))
 
210
    goto err;
 
211
 
 
212
  error=0;
 
213
  while(old_word->pos && new_word->pos)
 
214
  {
 
215
    cmp= ha_compare_text(cs, (uchar*) old_word->pos,old_word->len,
 
216
                             (uchar*) new_word->pos,new_word->len,0,0);
 
217
    cmp2= cmp ? 0 : (fabs(old_word->weight - new_word->weight) > 1.e-5);
 
218
 
 
219
    if (cmp < 0 || cmp2)
 
220
    {
 
221
      MARIA_KEY key;
 
222
      _ma_ft_make_key(info, &key, keynr, keybuf, old_word, pos);
 
223
      if ((error= _ma_ck_delete(info, &key)))
 
224
        goto err;
 
225
    }
 
226
    if (cmp > 0 || cmp2)
 
227
    {
 
228
      MARIA_KEY key;
 
229
      _ma_ft_make_key(info, &key, keynr, keybuf, new_word,pos);
 
230
      if ((error= _ma_ck_write(info, &key)))
 
231
        goto err;
 
232
    }
 
233
    if (cmp<=0) old_word++;
 
234
    if (cmp>=0) new_word++;
 
235
 }
 
236
 if (old_word->pos)
 
237
   error= _ma_ft_erase(info,keynr,keybuf,old_word,pos);
 
238
 else if (new_word->pos)
 
239
   error= _ma_ft_store(info,keynr,keybuf,new_word,pos);
 
240
 
 
241
err:
 
242
  free_root(&info->ft_memroot, MYF(MY_MARK_BLOCKS_FREE));
 
243
  DBUG_RETURN(error);
 
244
}
 
245
 
 
246
 
 
247
/* adds a document to the collection */
 
248
 
 
249
int _ma_ft_add(MARIA_HA *info, uint keynr, uchar *keybuf, const uchar *record,
 
250
               my_off_t pos)
 
251
{
 
252
  int error= -1;
 
253
  FT_WORD *wlist;
 
254
  DBUG_ENTER("_ma_ft_add");
 
255
  DBUG_PRINT("enter",("keynr: %d",keynr));
 
256
 
 
257
  if ((wlist= _ma_ft_parserecord(info, keynr, record, &info->ft_memroot)))
 
258
    error= _ma_ft_store(info,keynr,keybuf,wlist,pos);
 
259
  free_root(&info->ft_memroot, MYF(MY_MARK_BLOCKS_FREE));
 
260
  DBUG_PRINT("exit",("Return: %d",error));
 
261
  DBUG_RETURN(error);
 
262
}
 
263
 
 
264
 
 
265
/* removes a document from the collection */
 
266
 
 
267
int _ma_ft_del(MARIA_HA *info, uint keynr, uchar *keybuf, const uchar *record,
 
268
               my_off_t pos)
 
269
{
 
270
  int error= -1;
 
271
  FT_WORD *wlist;
 
272
  DBUG_ENTER("_ma_ft_del");
 
273
  DBUG_PRINT("enter",("keynr: %d",keynr));
 
274
 
 
275
  if ((wlist= _ma_ft_parserecord(info, keynr, record, &info->ft_memroot)))
 
276
    error= _ma_ft_erase(info,keynr,keybuf,wlist,pos);
 
277
  free_root(&info->ft_memroot, MYF(MY_MARK_BLOCKS_FREE));
 
278
  DBUG_PRINT("exit",("Return: %d",error));
 
279
  DBUG_RETURN(error);
 
280
}
 
281
 
 
282
 
 
283
MARIA_KEY *_ma_ft_make_key(MARIA_HA *info, MARIA_KEY *key, uint keynr,
 
284
                           uchar *keybuf,
 
285
                           FT_WORD *wptr, my_off_t filepos)
 
286
{
 
287
  uchar buf[HA_FT_MAXBYTELEN+16];
 
288
  DBUG_ENTER("_ma_ft_make_key");
 
289
 
 
290
#if HA_FT_WTYPE == HA_KEYTYPE_FLOAT
 
291
  {
 
292
    float weight=(float) ((filepos==HA_OFFSET_ERROR) ? 0 : wptr->weight);
 
293
    mi_float4store(buf,weight);
 
294
  }
 
295
#else
 
296
#error
 
297
#endif
 
298
 
 
299
  int2store(buf+HA_FT_WLEN,wptr->len);
 
300
  memcpy(buf+HA_FT_WLEN+2,wptr->pos,wptr->len);
 
301
  /* Can't be spatial so it's ok to call _ma_make_key directly here */
 
302
  DBUG_RETURN(_ma_make_key(info, key, keynr, keybuf, buf, filepos, 0));
 
303
}
 
304
 
 
305
 
 
306
/*
 
307
  convert key value to ft2
 
308
*/
 
309
 
 
310
uint _ma_ft_convert_to_ft2(MARIA_HA *info, MARIA_KEY *key)
 
311
{
 
312
  MARIA_SHARE *share= info->s;
 
313
  my_off_t root;
 
314
  DYNAMIC_ARRAY *da=info->ft1_to_ft2;
 
315
  MARIA_KEYDEF *keyinfo=&share->ft2_keyinfo;
 
316
  uchar *key_ptr= (uchar*) dynamic_array_ptr(da, 0), *end;
 
317
  uint length, key_length;
 
318
  MARIA_PINNED_PAGE tmp_page_link, *page_link= &tmp_page_link;
 
319
  MARIA_KEY tmp_key;
 
320
  DBUG_ENTER("_ma_ft_convert_to_ft2");
 
321
 
 
322
  /* we'll generate one pageful at once, and insert the rest one-by-one */
 
323
  /* calculating the length of this page ...*/
 
324
  length=(keyinfo->block_length-2) / keyinfo->keylength;
 
325
  set_if_smaller(length, da->elements);
 
326
  length=length * keyinfo->keylength;
 
327
 
 
328
  get_key_full_length_rdonly(key_length, key->data);
 
329
  while (_ma_ck_delete(info, key) == 0)
 
330
  {
 
331
    /*
 
332
      nothing to do here.
 
333
      _ma_ck_delete() will populate info->ft1_to_ft2 with deleted keys
 
334
    */
 
335
  }
 
336
 
 
337
  /* creating pageful of keys */
 
338
  bzero(info->buff, share->keypage_header);
 
339
  _ma_store_keynr(share, info->buff, keyinfo->key_nr);
 
340
  _ma_store_page_used(share, info->buff, length + share->keypage_header);
 
341
  memcpy(info->buff + share->keypage_header, key_ptr, length);
 
342
  info->keyread_buff_used= info->page_changed=1;      /* info->buff is used */
 
343
  /**
 
344
    @todo RECOVERY BUG this is not logged yet. Ok as this code is never
 
345
    called, but soon it will be.
 
346
  */
 
347
  if ((root= _ma_new(info, DFLT_INIT_HITS, &page_link)) == HA_OFFSET_ERROR ||
 
348
      _ma_write_keypage(info, keyinfo, root, page_link->write_lock,
 
349
                        DFLT_INIT_HITS, info->buff))
 
350
    DBUG_RETURN(-1);
 
351
 
 
352
  /* inserting the rest of key values */
 
353
  end= (uchar*) dynamic_array_ptr(da, da->elements);
 
354
  tmp_key.keyinfo= keyinfo;
 
355
  tmp_key.data_length= keyinfo->keylength;
 
356
  tmp_key.ref_length= 0;
 
357
  tmp_key.flag= 0;
 
358
  for (key_ptr+=length; key_ptr < end; key_ptr+=keyinfo->keylength)
 
359
  {
 
360
    tmp_key.data= key_ptr;
 
361
    if (_ma_ck_real_write_btree(info, key, &root, SEARCH_SAME))
 
362
      DBUG_RETURN(-1);
 
363
  }
 
364
 
 
365
  /* now, writing the word key entry */
 
366
  ft_intXstore(key->data + key_length, - (int) da->elements);
 
367
  _ma_dpointer(share, key->data + key_length + HA_FT_WLEN, root);
 
368
 
 
369
  DBUG_RETURN(_ma_ck_real_write_btree(info, key,
 
370
                                      &share->state.key_root[key->keyinfo->
 
371
                                                             key_nr],
 
372
                                      SEARCH_SAME));
 
373
}