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

« back to all changes in this revision

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