00001 /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB 00002 00003 This program is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU General Public License as published by 00005 the Free Software Foundation; either version 2 of the License, or 00006 (at your option) any later version. 00007 00008 This program is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 GNU General Public License for more details. 00012 00013 You should have received a copy of the GNU General Public License 00014 along with this program; if not, write to the Free Software 00015 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 00016 00017 /* Written by Sergei A. Golubchik, who has a shared copyright to this code */ 00018 00019 /* functions to work with full-text indices */ 00020 00021 #include "ftdefs.h" 00022 #include <math.h> 00023 00024 void _mi_ft_segiterator_init(MI_INFO *info, uint keynr, const byte *record, 00025 FT_SEG_ITERATOR *ftsi) 00026 { 00027 DBUG_ENTER("_mi_ft_segiterator_init"); 00028 00029 ftsi->num=info->s->keyinfo[keynr].keysegs; 00030 ftsi->seg=info->s->keyinfo[keynr].seg; 00031 ftsi->rec=record; 00032 DBUG_VOID_RETURN; 00033 } 00034 00035 void _mi_ft_segiterator_dummy_init(const byte *record, uint len, 00036 FT_SEG_ITERATOR *ftsi) 00037 { 00038 DBUG_ENTER("_mi_ft_segiterator_dummy_init"); 00039 00040 ftsi->num=1; 00041 ftsi->seg=0; 00042 ftsi->pos=record; 00043 ftsi->len=len; 00044 DBUG_VOID_RETURN; 00045 } 00046 00047 /* 00048 This function breaks convention "return 0 in success" 00049 but it's easier to use like this 00050 00051 while(_mi_ft_segiterator()) 00052 00053 so "1" means "OK", "0" means "EOF" 00054 */ 00055 00056 uint _mi_ft_segiterator(register FT_SEG_ITERATOR *ftsi) 00057 { 00058 DBUG_ENTER("_mi_ft_segiterator"); 00059 00060 if (!ftsi->num) 00061 DBUG_RETURN(0); 00062 00063 ftsi->num--; 00064 if (!ftsi->seg) 00065 DBUG_RETURN(1); 00066 00067 ftsi->seg--; 00068 00069 if (ftsi->seg->null_bit && 00070 (ftsi->rec[ftsi->seg->null_pos] & ftsi->seg->null_bit)) 00071 { 00072 ftsi->pos=0; 00073 DBUG_RETURN(1); 00074 } 00075 ftsi->pos= ftsi->rec+ftsi->seg->start; 00076 if (ftsi->seg->flag & HA_VAR_LENGTH_PART) 00077 { 00078 uint pack_length= (ftsi->seg->bit_start); 00079 ftsi->len= (pack_length == 1 ? (uint) *(uchar*) ftsi->pos : 00080 uint2korr(ftsi->pos)); 00081 ftsi->pos+= pack_length; /* Skip VARCHAR length */ 00082 DBUG_RETURN(1); 00083 } 00084 if (ftsi->seg->flag & HA_BLOB_PART) 00085 { 00086 ftsi->len=_mi_calc_blob_length(ftsi->seg->bit_start,ftsi->pos); 00087 memcpy_fixed((char*) &ftsi->pos, ftsi->pos+ftsi->seg->bit_start, 00088 sizeof(char*)); 00089 DBUG_RETURN(1); 00090 } 00091 ftsi->len=ftsi->seg->length; 00092 DBUG_RETURN(1); 00093 } 00094 00095 00096 /* parses a document i.e. calls ft_parse for every keyseg */ 00097 00098 uint _mi_ft_parse(TREE *parsed, MI_INFO *info, uint keynr, const byte *record, 00099 MYSQL_FTPARSER_PARAM *param, MEM_ROOT *mem_root) 00100 { 00101 FT_SEG_ITERATOR ftsi; 00102 struct st_mysql_ftparser *parser; 00103 DBUG_ENTER("_mi_ft_parse"); 00104 00105 _mi_ft_segiterator_init(info, keynr, record, &ftsi); 00106 00107 ft_parse_init(parsed, info->s->keyinfo[keynr].seg->charset); 00108 parser= info->s->keyinfo[keynr].parser; 00109 while (_mi_ft_segiterator(&ftsi)) 00110 { 00111 if (ftsi.pos) 00112 if (ft_parse(parsed, (byte *)ftsi.pos, ftsi.len, parser, param, mem_root)) 00113 DBUG_RETURN(1); 00114 } 00115 DBUG_RETURN(0); 00116 } 00117 00118 FT_WORD *_mi_ft_parserecord(MI_INFO *info, uint keynr, const byte *record, 00119 MEM_ROOT *mem_root) 00120 { 00121 TREE ptree; 00122 MYSQL_FTPARSER_PARAM *param; 00123 DBUG_ENTER("_mi_ft_parserecord"); 00124 if (! (param= ftparser_call_initializer(info, keynr, 0))) 00125 DBUG_RETURN(NULL); 00126 bzero((char*) &ptree, sizeof(ptree)); 00127 param->flags= 0; 00128 if (_mi_ft_parse(&ptree, info, keynr, record, param, mem_root)) 00129 DBUG_RETURN(NULL); 00130 00131 DBUG_RETURN(ft_linearize(&ptree, mem_root)); 00132 } 00133 00134 static int _mi_ft_store(MI_INFO *info, uint keynr, byte *keybuf, 00135 FT_WORD *wlist, my_off_t filepos) 00136 { 00137 uint key_length; 00138 DBUG_ENTER("_mi_ft_store"); 00139 00140 for (; wlist->pos; wlist++) 00141 { 00142 key_length=_ft_make_key(info,keynr,keybuf,wlist,filepos); 00143 if (_mi_ck_write(info,keynr,(uchar*) keybuf,key_length)) 00144 DBUG_RETURN(1); 00145 } 00146 DBUG_RETURN(0); 00147 } 00148 00149 static int _mi_ft_erase(MI_INFO *info, uint keynr, byte *keybuf, 00150 FT_WORD *wlist, my_off_t filepos) 00151 { 00152 uint key_length, err=0; 00153 DBUG_ENTER("_mi_ft_erase"); 00154 00155 for (; wlist->pos; wlist++) 00156 { 00157 key_length=_ft_make_key(info,keynr,keybuf,wlist,filepos); 00158 if (_mi_ck_delete(info,keynr,(uchar*) keybuf,key_length)) 00159 err=1; 00160 } 00161 DBUG_RETURN(err); 00162 } 00163 00164 /* 00165 Compares an appropriate parts of two WORD_KEY keys directly out of records 00166 returns 1 if they are different 00167 */ 00168 00169 #define THOSE_TWO_DAMN_KEYS_ARE_REALLY_DIFFERENT 1 00170 #define GEE_THEY_ARE_ABSOLUTELY_IDENTICAL 0 00171 00172 int _mi_ft_cmp(MI_INFO *info, uint keynr, const byte *rec1, const byte *rec2) 00173 { 00174 FT_SEG_ITERATOR ftsi1, ftsi2; 00175 CHARSET_INFO *cs=info->s->keyinfo[keynr].seg->charset; 00176 DBUG_ENTER("_mi_ft_cmp"); 00177 _mi_ft_segiterator_init(info, keynr, rec1, &ftsi1); 00178 _mi_ft_segiterator_init(info, keynr, rec2, &ftsi2); 00179 00180 while (_mi_ft_segiterator(&ftsi1) && _mi_ft_segiterator(&ftsi2)) 00181 { 00182 if ((ftsi1.pos != ftsi2.pos) && 00183 (!ftsi1.pos || !ftsi2.pos || 00184 mi_compare_text(cs, (uchar*) ftsi1.pos,ftsi1.len, 00185 (uchar*) ftsi2.pos,ftsi2.len,0,0))) 00186 DBUG_RETURN(THOSE_TWO_DAMN_KEYS_ARE_REALLY_DIFFERENT); 00187 } 00188 DBUG_RETURN(GEE_THEY_ARE_ABSOLUTELY_IDENTICAL); 00189 } 00190 00191 00192 /* update a document entry */ 00193 00194 int _mi_ft_update(MI_INFO *info, uint keynr, byte *keybuf, 00195 const byte *oldrec, const byte *newrec, my_off_t pos) 00196 { 00197 int error= -1; 00198 FT_WORD *oldlist,*newlist, *old_word, *new_word; 00199 CHARSET_INFO *cs=info->s->keyinfo[keynr].seg->charset; 00200 uint key_length; 00201 int cmp, cmp2; 00202 DBUG_ENTER("_mi_ft_update"); 00203 00204 if (!(old_word=oldlist=_mi_ft_parserecord(info, keynr, oldrec, 00205 &info->ft_memroot)) || 00206 !(new_word=newlist=_mi_ft_parserecord(info, keynr, newrec, 00207 &info->ft_memroot))) 00208 goto err; 00209 00210 error=0; 00211 while(old_word->pos && new_word->pos) 00212 { 00213 cmp= mi_compare_text(cs, (uchar*) old_word->pos,old_word->len, 00214 (uchar*) new_word->pos,new_word->len,0,0); 00215 cmp2= cmp ? 0 : (fabs(old_word->weight - new_word->weight) > 1.e-5); 00216 00217 if (cmp < 0 || cmp2) 00218 { 00219 key_length=_ft_make_key(info,keynr,keybuf,old_word,pos); 00220 if ((error=_mi_ck_delete(info,keynr,(uchar*) keybuf,key_length))) 00221 goto err; 00222 } 00223 if (cmp > 0 || cmp2) 00224 { 00225 key_length=_ft_make_key(info,keynr,keybuf,new_word,pos); 00226 if ((error=_mi_ck_write(info,keynr,(uchar*) keybuf,key_length))) 00227 goto err; 00228 } 00229 if (cmp<=0) old_word++; 00230 if (cmp>=0) new_word++; 00231 } 00232 if (old_word->pos) 00233 error=_mi_ft_erase(info,keynr,keybuf,old_word,pos); 00234 else if (new_word->pos) 00235 error=_mi_ft_store(info,keynr,keybuf,new_word,pos); 00236 00237 err: 00238 free_root(&info->ft_memroot, MYF(MY_MARK_BLOCKS_FREE)); 00239 DBUG_RETURN(error); 00240 } 00241 00242 00243 /* adds a document to the collection */ 00244 00245 int _mi_ft_add(MI_INFO *info, uint keynr, byte *keybuf, const byte *record, 00246 my_off_t pos) 00247 { 00248 int error= -1; 00249 FT_WORD *wlist; 00250 DBUG_ENTER("_mi_ft_add"); 00251 DBUG_PRINT("enter",("keynr: %d",keynr)); 00252 00253 if ((wlist=_mi_ft_parserecord(info, keynr, record, &info->ft_memroot))) 00254 error=_mi_ft_store(info,keynr,keybuf,wlist,pos); 00255 00256 free_root(&info->ft_memroot, MYF(MY_MARK_BLOCKS_FREE)); 00257 DBUG_PRINT("exit",("Return: %d",error)); 00258 DBUG_RETURN(error); 00259 } 00260 00261 00262 /* removes a document from the collection */ 00263 00264 int _mi_ft_del(MI_INFO *info, uint keynr, byte *keybuf, const byte *record, 00265 my_off_t pos) 00266 { 00267 int error= -1; 00268 FT_WORD *wlist; 00269 DBUG_ENTER("_mi_ft_del"); 00270 DBUG_PRINT("enter",("keynr: %d",keynr)); 00271 00272 if ((wlist=_mi_ft_parserecord(info, keynr, record, &info->ft_memroot))) 00273 error=_mi_ft_erase(info,keynr,keybuf,wlist,pos); 00274 00275 free_root(&info->ft_memroot, MYF(MY_MARK_BLOCKS_FREE)); 00276 DBUG_PRINT("exit",("Return: %d",error)); 00277 DBUG_RETURN(error); 00278 } 00279 00280 uint _ft_make_key(MI_INFO *info, uint keynr, byte *keybuf, FT_WORD *wptr, 00281 my_off_t filepos) 00282 { 00283 byte buf[HA_FT_MAXBYTELEN+16]; 00284 DBUG_ENTER("_ft_make_key"); 00285 00286 #if HA_FT_WTYPE == HA_KEYTYPE_FLOAT 00287 { 00288 float weight=(float) ((filepos==HA_OFFSET_ERROR) ? 0 : wptr->weight); 00289 mi_float4store(buf,weight); 00290 } 00291 #else 00292 #error 00293 #endif 00294 00295 int2store(buf+HA_FT_WLEN,wptr->len); 00296 memcpy(buf+HA_FT_WLEN+2,wptr->pos,wptr->len); 00297 DBUG_RETURN(_mi_make_key(info,keynr,(uchar*) keybuf,buf,filepos)); 00298 } 00299 00300 00301 /* 00302 convert key value to ft2 00303 */ 00304 00305 uint _mi_ft_convert_to_ft2(MI_INFO *info, uint keynr, uchar *key) 00306 { 00307 my_off_t root; 00308 DYNAMIC_ARRAY *da=info->ft1_to_ft2; 00309 MI_KEYDEF *keyinfo=&info->s->ft2_keyinfo; 00310 uchar *key_ptr= (uchar*) dynamic_array_ptr(da, 0), *end; 00311 uint length, key_length; 00312 DBUG_ENTER("_mi_ft_convert_to_ft2"); 00313 00314 /* we'll generate one pageful at once, and insert the rest one-by-one */ 00315 /* calculating the length of this page ...*/ 00316 length=(keyinfo->block_length-2) / keyinfo->keylength; 00317 set_if_smaller(length, da->elements); 00318 length=length * keyinfo->keylength; 00319 00320 get_key_full_length_rdonly(key_length, key); 00321 while (_mi_ck_delete(info, keynr, key, key_length) == 0) 00322 { 00323 /* 00324 nothing to do here. 00325 _mi_ck_delete() will populate info->ft1_to_ft2 with deleted keys 00326 */ 00327 } 00328 00329 /* creating pageful of keys */ 00330 mi_putint(info->buff,length+2,0); 00331 memcpy(info->buff+2, key_ptr, length); 00332 info->buff_used=info->page_changed=1; /* info->buff is used */ 00333 if ((root= _mi_new(info,keyinfo,DFLT_INIT_HITS)) == HA_OFFSET_ERROR || 00334 _mi_write_keypage(info,keyinfo,root,DFLT_INIT_HITS,info->buff)) 00335 DBUG_RETURN(-1); 00336 00337 /* inserting the rest of key values */ 00338 end= (uchar*) dynamic_array_ptr(da, da->elements); 00339 for (key_ptr+=length; key_ptr < end; key_ptr+=keyinfo->keylength) 00340 if(_mi_ck_real_write_btree(info, keyinfo, key_ptr, 0, &root, SEARCH_SAME)) 00341 DBUG_RETURN(-1); 00342 00343 /* now, writing the word key entry */ 00344 ft_intXstore(key+key_length, - (int) da->elements); 00345 _mi_dpointer(info, key+key_length+HA_FT_WLEN, root); 00346 00347 DBUG_RETURN(_mi_ck_real_write_btree(info, 00348 info->s->keyinfo+keynr, 00349 key, 0, 00350 &info->s->state.key_root[keynr], 00351 SEARCH_SAME)); 00352 } 00353
1.4.7

