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