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