| 1 | /* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. |
| 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 St, Fifth Floor, Boston, MA 02110-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 "ftdefs.h" |
| 21 | #include <math.h> |
| 22 | |
| 23 | void _mi_ft_segiterator_init(MI_INFO *info, uint keynr, const uchar *record, |
| 24 | FT_SEG_ITERATOR *ftsi) |
| 25 | { |
| 26 | DBUG_ENTER("_mi_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 | |
| 36 | void _mi_ft_segiterator_dummy_init(const uchar *record, uint len, |
| 37 | FT_SEG_ITERATOR *ftsi) |
| 38 | { |
| 39 | DBUG_ENTER("_mi_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(_mi_ft_segiterator()) |
| 53 | |
| 54 | so "1" means "OK", "0" means "EOF" |
| 55 | */ |
| 56 | |
| 57 | uint _mi_ft_segiterator(register FT_SEG_ITERATOR *ftsi) |
| 58 | { |
| 59 | DBUG_ENTER("_mi_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) *(uchar*) 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=_mi_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 ft_parse for every keyseg */ |
| 97 | |
| 98 | uint _mi_ft_parse(TREE *parsed, MI_INFO *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("_mi_ft_parse" ); |
| 104 | |
| 105 | _mi_ft_segiterator_init(info, keynr, record, &ftsi); |
| 106 | |
| 107 | ft_parse_init(parsed, info->s->keyinfo[keynr].seg->charset); |
| 108 | parser= info->s->keyinfo[keynr].parser; |
| 109 | while (_mi_ft_segiterator(&ftsi)) |
| 110 | { |
| 111 | if (ftsi.pos) |
| 112 | if (ft_parse(parsed, (uchar *)ftsi.pos, ftsi.len, parser, param, mem_root)) |
| 113 | DBUG_RETURN(1); |
| 114 | } |
| 115 | DBUG_RETURN(0); |
| 116 | } |
| 117 | |
| 118 | FT_WORD *_mi_ft_parserecord(MI_INFO *info, uint keynr, const uchar *record, |
| 119 | MEM_ROOT *mem_root) |
| 120 | { |
| 121 | TREE ptree; |
| 122 | MYSQL_FTPARSER_PARAM *param; |
| 123 | DBUG_ENTER("_mi_ft_parserecord" ); |
| 124 | if (! (param= ftparser_call_initializer(info, keynr, 0))) |
| 125 | DBUG_RETURN(NULL); |
| 126 | bzero((char*) &ptree, sizeof(ptree)); |
| 127 | param->flags= 0; |
| 128 | if (_mi_ft_parse(&ptree, info, keynr, record, param, mem_root)) |
| 129 | DBUG_RETURN(NULL); |
| 130 | |
| 131 | DBUG_RETURN(ft_linearize(&ptree, mem_root)); |
| 132 | } |
| 133 | |
| 134 | static int _mi_ft_store(MI_INFO *info, uint keynr, uchar *keybuf, |
| 135 | FT_WORD *wlist, my_off_t filepos) |
| 136 | { |
| 137 | uint key_length; |
| 138 | DBUG_ENTER("_mi_ft_store" ); |
| 139 | |
| 140 | for (; wlist->pos; wlist++) |
| 141 | { |
| 142 | key_length=_ft_make_key(info,keynr,keybuf,wlist,filepos); |
| 143 | if (_mi_ck_write(info,keynr,(uchar*) keybuf,key_length)) |
| 144 | DBUG_RETURN(1); |
| 145 | } |
| 146 | DBUG_RETURN(0); |
| 147 | } |
| 148 | |
| 149 | static int _mi_ft_erase(MI_INFO *info, uint keynr, uchar *keybuf, |
| 150 | FT_WORD *wlist, my_off_t filepos) |
| 151 | { |
| 152 | uint key_length, err=0; |
| 153 | DBUG_ENTER("_mi_ft_erase" ); |
| 154 | |
| 155 | for (; wlist->pos; wlist++) |
| 156 | { |
| 157 | key_length=_ft_make_key(info,keynr,keybuf,wlist,filepos); |
| 158 | if (_mi_ck_delete(info,keynr,(uchar*) keybuf,key_length)) |
| 159 | err=1; |
| 160 | } |
| 161 | DBUG_RETURN(err); |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | Compares an appropriate parts of two WORD_KEY keys directly out of records |
| 166 | returns 1 if they are different |
| 167 | */ |
| 168 | |
| 169 | #define THOSE_TWO_DAMN_KEYS_ARE_REALLY_DIFFERENT 1 |
| 170 | #define GEE_THEY_ARE_ABSOLUTELY_IDENTICAL 0 |
| 171 | |
| 172 | int _mi_ft_cmp(MI_INFO *info, uint keynr, const uchar *rec1, const uchar *rec2) |
| 173 | { |
| 174 | FT_SEG_ITERATOR ftsi1, ftsi2; |
| 175 | CHARSET_INFO *cs=info->s->keyinfo[keynr].seg->charset; |
| 176 | DBUG_ENTER("_mi_ft_cmp" ); |
| 177 | _mi_ft_segiterator_init(info, keynr, rec1, &ftsi1); |
| 178 | _mi_ft_segiterator_init(info, keynr, rec2, &ftsi2); |
| 179 | |
| 180 | while (_mi_ft_segiterator(&ftsi1) && _mi_ft_segiterator(&ftsi2)) |
| 181 | { |
| 182 | if ((ftsi1.pos != ftsi2.pos) && |
| 183 | (!ftsi1.pos || !ftsi2.pos || |
| 184 | ha_compare_text(cs, (uchar*) ftsi1.pos,ftsi1.len, |
| 185 | (uchar*) ftsi2.pos,ftsi2.len,0))) |
| 186 | DBUG_RETURN(THOSE_TWO_DAMN_KEYS_ARE_REALLY_DIFFERENT); |
| 187 | } |
| 188 | DBUG_RETURN(GEE_THEY_ARE_ABSOLUTELY_IDENTICAL); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | /* update a document entry */ |
| 193 | |
| 194 | int _mi_ft_update(MI_INFO *info, uint keynr, uchar *keybuf, |
| 195 | const uchar *oldrec, const uchar *newrec, my_off_t pos) |
| 196 | { |
| 197 | int error= -1; |
| 198 | FT_WORD *oldlist,*newlist, *old_word, *new_word; |
| 199 | CHARSET_INFO *cs=info->s->keyinfo[keynr].seg->charset; |
| 200 | uint key_length; |
| 201 | int cmp, cmp2; |
| 202 | DBUG_ENTER("_mi_ft_update" ); |
| 203 | |
| 204 | if (!(old_word=oldlist=_mi_ft_parserecord(info, keynr, oldrec, |
| 205 | &info->ft_memroot)) || |
| 206 | !(new_word=newlist=_mi_ft_parserecord(info, keynr, newrec, |
| 207 | &info->ft_memroot))) |
| 208 | goto err; |
| 209 | |
| 210 | error=0; |
| 211 | while(old_word->pos && new_word->pos) |
| 212 | { |
| 213 | cmp= ha_compare_text(cs, (uchar*) old_word->pos,old_word->len, |
| 214 | (uchar*) new_word->pos,new_word->len,0); |
| 215 | cmp2= cmp ? 0 : (fabs(old_word->weight - new_word->weight) > 1.e-5); |
| 216 | |
| 217 | if (cmp < 0 || cmp2) |
| 218 | { |
| 219 | key_length=_ft_make_key(info,keynr,keybuf,old_word,pos); |
| 220 | if ((error=_mi_ck_delete(info,keynr,(uchar*) keybuf,key_length))) |
| 221 | goto err; |
| 222 | } |
| 223 | if (cmp > 0 || cmp2) |
| 224 | { |
| 225 | key_length=_ft_make_key(info,keynr,keybuf,new_word,pos); |
| 226 | if ((error=_mi_ck_write(info,keynr,(uchar*) keybuf,key_length))) |
| 227 | goto err; |
| 228 | } |
| 229 | if (cmp<=0) old_word++; |
| 230 | if (cmp>=0) new_word++; |
| 231 | } |
| 232 | if (old_word->pos) |
| 233 | error=_mi_ft_erase(info,keynr,keybuf,old_word,pos); |
| 234 | else if (new_word->pos) |
| 235 | error=_mi_ft_store(info,keynr,keybuf,new_word,pos); |
| 236 | |
| 237 | err: |
| 238 | free_root(&info->ft_memroot, MYF(MY_MARK_BLOCKS_FREE)); |
| 239 | DBUG_RETURN(error); |
| 240 | } |
| 241 | |
| 242 | |
| 243 | /* adds a document to the collection */ |
| 244 | |
| 245 | int _mi_ft_add(MI_INFO *info, uint keynr, uchar *keybuf, const uchar *record, |
| 246 | my_off_t pos) |
| 247 | { |
| 248 | int error= -1; |
| 249 | FT_WORD *wlist; |
| 250 | DBUG_ENTER("_mi_ft_add" ); |
| 251 | DBUG_PRINT("enter" ,("keynr: %d" ,keynr)); |
| 252 | |
| 253 | if ((wlist=_mi_ft_parserecord(info, keynr, record, &info->ft_memroot))) |
| 254 | error=_mi_ft_store(info,keynr,keybuf,wlist,pos); |
| 255 | |
| 256 | free_root(&info->ft_memroot, MYF(MY_MARK_BLOCKS_FREE)); |
| 257 | DBUG_PRINT("exit" ,("Return: %d" ,error)); |
| 258 | DBUG_RETURN(error); |
| 259 | } |
| 260 | |
| 261 | |
| 262 | /* removes a document from the collection */ |
| 263 | |
| 264 | int _mi_ft_del(MI_INFO *info, uint keynr, uchar *keybuf, const uchar *record, |
| 265 | my_off_t pos) |
| 266 | { |
| 267 | int error= -1; |
| 268 | FT_WORD *wlist; |
| 269 | DBUG_ENTER("_mi_ft_del" ); |
| 270 | DBUG_PRINT("enter" ,("keynr: %d" ,keynr)); |
| 271 | |
| 272 | if ((wlist=_mi_ft_parserecord(info, keynr, record, &info->ft_memroot))) |
| 273 | error=_mi_ft_erase(info,keynr,keybuf,wlist,pos); |
| 274 | |
| 275 | free_root(&info->ft_memroot, MYF(MY_MARK_BLOCKS_FREE)); |
| 276 | DBUG_PRINT("exit" ,("Return: %d" ,error)); |
| 277 | DBUG_RETURN(error); |
| 278 | } |
| 279 | |
| 280 | uint _ft_make_key(MI_INFO *info, uint keynr, uchar *keybuf, FT_WORD *wptr, |
| 281 | my_off_t filepos) |
| 282 | { |
| 283 | uchar buf[HA_FT_MAXBYTELEN+16]; |
| 284 | float weight=(float) ((filepos==HA_OFFSET_ERROR) ? 0 : wptr->weight); |
| 285 | DBUG_ENTER("_ft_make_key" ); |
| 286 | |
| 287 | mi_float4store(buf,weight); |
| 288 | int2store(buf+HA_FT_WLEN,wptr->len); |
| 289 | memcpy(buf+HA_FT_WLEN+2,wptr->pos,wptr->len); |
| 290 | DBUG_RETURN(_mi_make_key(info,keynr,(uchar*) keybuf,buf,filepos)); |
| 291 | } |
| 292 | |
| 293 | |
| 294 | /* |
| 295 | convert key value to ft2 |
| 296 | */ |
| 297 | |
| 298 | uint _mi_ft_convert_to_ft2(MI_INFO *info, uint keynr, uchar *key) |
| 299 | { |
| 300 | my_off_t root; |
| 301 | DYNAMIC_ARRAY *da=info->ft1_to_ft2; |
| 302 | MI_KEYDEF *keyinfo=&info->s->ft2_keyinfo; |
| 303 | uchar *key_ptr= (uchar*) dynamic_array_ptr(da, 0), *end; |
| 304 | uint length, key_length; |
| 305 | DBUG_ENTER("_mi_ft_convert_to_ft2" ); |
| 306 | |
| 307 | /* we'll generate one pageful at once, and insert the rest one-by-one */ |
| 308 | /* calculating the length of this page ...*/ |
| 309 | length=(keyinfo->block_length-2) / keyinfo->keylength; |
| 310 | set_if_smaller(length, da->elements); |
| 311 | length=length * keyinfo->keylength; |
| 312 | |
| 313 | get_key_full_length_rdonly(key_length, key); |
| 314 | while (_mi_ck_delete(info, keynr, key, key_length) == 0) |
| 315 | { |
| 316 | /* |
| 317 | nothing to do here. |
| 318 | _mi_ck_delete() will populate info->ft1_to_ft2 with deleted keys |
| 319 | */ |
| 320 | } |
| 321 | |
| 322 | /* creating pageful of keys */ |
| 323 | mi_putint(info->buff,length+2,0); |
| 324 | memcpy(info->buff+2, key_ptr, length); |
| 325 | info->buff_used=info->page_changed=1; /* info->buff is used */ |
| 326 | if ((root= _mi_new(info,keyinfo,DFLT_INIT_HITS)) == HA_OFFSET_ERROR || |
| 327 | _mi_write_keypage(info,keyinfo,root,DFLT_INIT_HITS,info->buff)) |
| 328 | DBUG_RETURN(-1); |
| 329 | |
| 330 | /* inserting the rest of key values */ |
| 331 | end= (uchar*) dynamic_array_ptr(da, da->elements); |
| 332 | for (key_ptr+=length; key_ptr < end; key_ptr+=keyinfo->keylength) |
| 333 | if(_mi_ck_real_write_btree(info, keyinfo, key_ptr, 0, &root, SEARCH_SAME)) |
| 334 | DBUG_RETURN(-1); |
| 335 | |
| 336 | /* now, writing the word key entry */ |
| 337 | ft_intXstore(key+key_length, - (int) da->elements); |
| 338 | _mi_dpointer(info, key+key_length+HA_FT_WLEN, root); |
| 339 | |
| 340 | DBUG_RETURN(_mi_ck_real_write_btree(info, |
| 341 | info->s->keyinfo+keynr, |
| 342 | key, 0, |
| 343 | &info->s->state.key_root[keynr], |
| 344 | SEARCH_SAME)); |
| 345 | } |
| 346 | |
| 347 | |