You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1917 lines
58 KiB

26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
  1. /* Copyright (C) 2000-2006 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. /* key handling functions */
  13. #include "fulltext.h"
  14. #include "m_ctype.h"
  15. static my_bool _mi_get_prev_key(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *page,
  16. uchar *key, uchar *keypos,
  17. uint *return_key_length);
  18. /* Check index */
  19. int _mi_check_index(MI_INFO *info, int inx)
  20. {
  21. if (inx == -1) /* Use last index */
  22. inx=info->lastinx;
  23. if (inx < 0 || ! mi_is_key_active(info->s->state.key_map, inx))
  24. {
  25. my_errno=HA_ERR_WRONG_INDEX;
  26. return -1;
  27. }
  28. if (info->lastinx != inx) /* Index changed */
  29. {
  30. info->lastinx = inx;
  31. info->page_changed=1;
  32. info->update= ((info->update & (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED)) |
  33. HA_STATE_NEXT_FOUND | HA_STATE_PREV_FOUND);
  34. }
  35. if (info->opt_flag & WRITE_CACHE_USED && flush_io_cache(&info->rec_cache))
  36. return(-1);
  37. return(inx);
  38. } /* mi_check_index */
  39. /*
  40. ** Search after row by a key
  41. ** Position to row is stored in info->lastpos
  42. ** Return: -1 if not found
  43. ** 1 if one should continue search on higher level
  44. */
  45. int _mi_search(register MI_INFO *info, register MI_KEYDEF *keyinfo,
  46. uchar *key, uint key_len, uint nextflag, register my_off_t pos)
  47. {
  48. my_bool last_key;
  49. int error,flag;
  50. uint nod_flag;
  51. uchar *keypos,*maxpos;
  52. uchar lastkey[MI_MAX_KEY_BUFF],*buff;
  53. DBUG_ENTER("_mi_search");
  54. DBUG_PRINT("enter",("pos: %lu nextflag: %u lastpos: %lu",
  55. (ulong) pos, nextflag, (ulong) info->lastpos));
  56. DBUG_EXECUTE("key",_mi_print_key(DBUG_FILE,keyinfo->seg,key,key_len););
  57. if (pos == HA_OFFSET_ERROR)
  58. {
  59. my_errno=HA_ERR_KEY_NOT_FOUND; /* Didn't find key */
  60. info->lastpos= HA_OFFSET_ERROR;
  61. if (!(nextflag & (SEARCH_SMALLER | SEARCH_BIGGER | SEARCH_LAST)))
  62. DBUG_RETURN(-1); /* Not found ; return error */
  63. DBUG_RETURN(1); /* Search at upper levels */
  64. }
  65. if (!(buff=_mi_fetch_keypage(info,keyinfo,pos,DFLT_INIT_HITS,info->buff,
  66. test(!(nextflag & SEARCH_SAVE_BUFF)))))
  67. goto err;
  68. DBUG_DUMP("page",(byte*) buff,mi_getint(buff));
  69. flag=(*keyinfo->bin_search)(info,keyinfo,buff,key,key_len,nextflag,
  70. &keypos,lastkey, &last_key);
  71. if (flag == MI_FOUND_WRONG_KEY)
  72. DBUG_RETURN(-1);
  73. nod_flag=mi_test_if_nod(buff);
  74. maxpos=buff+mi_getint(buff)-1;
  75. if (flag)
  76. {
  77. if ((error=_mi_search(info,keyinfo,key,key_len,nextflag,
  78. _mi_kpos(nod_flag,keypos))) <= 0)
  79. DBUG_RETURN(error);
  80. if (flag >0)
  81. {
  82. if (nextflag & (SEARCH_SMALLER | SEARCH_LAST) &&
  83. keypos == buff+2+nod_flag)
  84. DBUG_RETURN(1); /* Bigger than key */
  85. }
  86. else if (nextflag & SEARCH_BIGGER && keypos >= maxpos)
  87. DBUG_RETURN(1); /* Smaller than key */
  88. }
  89. else
  90. {
  91. if ((nextflag & SEARCH_FIND) && nod_flag &&
  92. ((keyinfo->flag & (HA_NOSAME | HA_NULL_PART)) != HA_NOSAME ||
  93. key_len != USE_WHOLE_KEY))
  94. {
  95. if ((error=_mi_search(info,keyinfo,key,key_len,SEARCH_FIND,
  96. _mi_kpos(nod_flag,keypos))) >= 0 ||
  97. my_errno != HA_ERR_KEY_NOT_FOUND)
  98. DBUG_RETURN(error);
  99. info->last_keypage= HA_OFFSET_ERROR; /* Buffer not in mem */
  100. }
  101. }
  102. if (pos != info->last_keypage)
  103. {
  104. uchar *old_buff=buff;
  105. if (!(buff=_mi_fetch_keypage(info,keyinfo,pos,DFLT_INIT_HITS,info->buff,
  106. test(!(nextflag & SEARCH_SAVE_BUFF)))))
  107. goto err;
  108. keypos=buff+(keypos-old_buff);
  109. maxpos=buff+(maxpos-old_buff);
  110. }
  111. if ((nextflag & (SEARCH_SMALLER | SEARCH_LAST)) && flag != 0)
  112. {
  113. uint not_used[2];
  114. if (_mi_get_prev_key(info,keyinfo, buff, info->lastkey, keypos,
  115. &info->lastkey_length))
  116. goto err;
  117. if (!(nextflag & SEARCH_SMALLER) &&
  118. ha_key_cmp(keyinfo->seg, info->lastkey, key, key_len, SEARCH_FIND,
  119. not_used))
  120. {
  121. my_errno=HA_ERR_KEY_NOT_FOUND; /* Didn't find key */
  122. goto err;
  123. }
  124. }
  125. else
  126. {
  127. info->lastkey_length=(*keyinfo->get_key)(keyinfo,nod_flag,&keypos,lastkey);
  128. if (!info->lastkey_length)
  129. goto err;
  130. memcpy(info->lastkey,lastkey,info->lastkey_length);
  131. }
  132. info->lastpos=_mi_dpos(info,0,info->lastkey+info->lastkey_length);
  133. /* Save position for a possible read next / previous */
  134. info->int_keypos=info->buff+ (keypos-buff);
  135. info->int_maxpos=info->buff+ (maxpos-buff);
  136. info->int_nod_flag=nod_flag;
  137. info->int_keytree_version=keyinfo->version;
  138. info->last_search_keypage=info->last_keypage;
  139. info->page_changed=0;
  140. info->buff_used= (info->buff != buff); /* If we have to reread buff */
  141. DBUG_PRINT("exit",("found key at %lu",(ulong) info->lastpos));
  142. DBUG_RETURN(0);
  143. err:
  144. DBUG_PRINT("exit",("Error: %d",my_errno));
  145. info->lastpos= HA_OFFSET_ERROR;
  146. info->page_changed=1;
  147. DBUG_RETURN (-1);
  148. } /* _mi_search */
  149. /* Search after key in page-block */
  150. /* If packed key puts smaller or identical key in buff */
  151. /* ret_pos point to where find or bigger key starts */
  152. /* ARGSUSED */
  153. int _mi_bin_search(MI_INFO *info, register MI_KEYDEF *keyinfo, uchar *page,
  154. uchar *key, uint key_len, uint comp_flag, uchar **ret_pos,
  155. uchar *buff __attribute__((unused)), my_bool *last_key)
  156. {
  157. reg4 int start,mid,end,save_end;
  158. int flag;
  159. uint totlength,nod_flag,not_used[2];
  160. DBUG_ENTER("_mi_bin_search");
  161. LINT_INIT(flag);
  162. totlength=keyinfo->keylength+(nod_flag=mi_test_if_nod(page));
  163. start=0; mid=1;
  164. save_end=end=(int) ((mi_getint(page)-2-nod_flag)/totlength-1);
  165. DBUG_PRINT("test",("mi_getint: %d end: %d",mi_getint(page),end));
  166. page+=2+nod_flag;
  167. while (start != end)
  168. {
  169. mid= (start+end)/2;
  170. if ((flag=ha_key_cmp(keyinfo->seg,page+(uint) mid*totlength,key,key_len,
  171. comp_flag, not_used))
  172. >= 0)
  173. end=mid;
  174. else
  175. start=mid+1;
  176. }
  177. if (mid != start)
  178. flag=ha_key_cmp(keyinfo->seg,page+(uint) start*totlength,key,key_len,
  179. comp_flag, not_used);
  180. if (flag < 0)
  181. start++; /* point at next, bigger key */
  182. *ret_pos=page+(uint) start*totlength;
  183. *last_key= end == save_end;
  184. DBUG_PRINT("exit",("flag: %d keypos: %d",flag,start));
  185. DBUG_RETURN(flag);
  186. } /* _mi_bin_search */
  187. /*
  188. Locate a packed key in a key page.
  189. SYNOPSIS
  190. _mi_seq_search()
  191. info Open table information.
  192. keyinfo Key definition information.
  193. page Key page (beginning).
  194. key Search key.
  195. key_len Length to use from search key or USE_WHOLE_KEY
  196. comp_flag Search flags like SEARCH_SAME etc.
  197. ret_pos RETURN Position in key page behind this key.
  198. buff RETURN Copy of previous or identical unpacked key.
  199. last_key RETURN If key is last in page.
  200. DESCRIPTION
  201. Used instead of _mi_bin_search() when key is packed.
  202. Puts smaller or identical key in buff.
  203. Key is searched sequentially.
  204. RETURN
  205. > 0 Key in 'buff' is smaller than search key.
  206. 0 Key in 'buff' is identical to search key.
  207. < 0 Not found.
  208. */
  209. int _mi_seq_search(MI_INFO *info, register MI_KEYDEF *keyinfo, uchar *page,
  210. uchar *key, uint key_len, uint comp_flag, uchar **ret_pos,
  211. uchar *buff, my_bool *last_key)
  212. {
  213. int flag;
  214. uint nod_flag,length,not_used[2];
  215. uchar t_buff[MI_MAX_KEY_BUFF],*end;
  216. DBUG_ENTER("_mi_seq_search");
  217. LINT_INIT(flag); LINT_INIT(length);
  218. end= page+mi_getint(page);
  219. nod_flag=mi_test_if_nod(page);
  220. page+=2+nod_flag;
  221. *ret_pos=page;
  222. t_buff[0]=0; /* Avoid bugs */
  223. while (page < end)
  224. {
  225. length=(*keyinfo->get_key)(keyinfo,nod_flag,&page,t_buff);
  226. if (length == 0 || page > end)
  227. {
  228. mi_print_error(info->s, HA_ERR_CRASHED);
  229. my_errno=HA_ERR_CRASHED;
  230. DBUG_PRINT("error",("Found wrong key: length: %u page: %lx end: %lx",
  231. length, (long) page, (long) end));
  232. DBUG_RETURN(MI_FOUND_WRONG_KEY);
  233. }
  234. if ((flag=ha_key_cmp(keyinfo->seg,t_buff,key,key_len,comp_flag,
  235. not_used)) >= 0)
  236. break;
  237. #ifdef EXTRA_DEBUG
  238. DBUG_PRINT("loop",("page: %lx key: '%s' flag: %d", (long) page, t_buff,
  239. flag));
  240. #endif
  241. memcpy(buff,t_buff,length);
  242. *ret_pos=page;
  243. }
  244. if (flag == 0)
  245. memcpy(buff,t_buff,length); /* Result is first key */
  246. *last_key= page == end;
  247. DBUG_PRINT("exit",("flag: %d ret_pos: %lx", flag, (long) *ret_pos));
  248. DBUG_RETURN(flag);
  249. } /* _mi_seq_search */
  250. int _mi_prefix_search(MI_INFO *info, register MI_KEYDEF *keyinfo, uchar *page,
  251. uchar *key, uint key_len, uint nextflag, uchar **ret_pos,
  252. uchar *buff, my_bool *last_key)
  253. {
  254. /*
  255. my_flag is raw comparison result to be changed according to
  256. SEARCH_NO_FIND,SEARCH_LAST and HA_REVERSE_SORT flags.
  257. flag is the value returned by ha_key_cmp and as treated as final
  258. */
  259. int flag=0, my_flag=-1;
  260. uint nod_flag, length, len, matched, cmplen, kseg_len;
  261. uint prefix_len,suffix_len;
  262. int key_len_skip, seg_len_pack, key_len_left;
  263. uchar *end, *kseg, *vseg;
  264. uchar *sort_order=keyinfo->seg->charset->sort_order;
  265. uchar tt_buff[MI_MAX_KEY_BUFF+2], *t_buff=tt_buff+2;
  266. uchar *saved_from, *saved_to, *saved_vseg;
  267. uint saved_length=0, saved_prefix_len=0;
  268. uint length_pack;
  269. DBUG_ENTER("_mi_prefix_search");
  270. LINT_INIT(length);
  271. LINT_INIT(prefix_len);
  272. LINT_INIT(seg_len_pack);
  273. LINT_INIT(saved_from);
  274. LINT_INIT(saved_to);
  275. LINT_INIT(saved_vseg);
  276. t_buff[0]=0; /* Avoid bugs */
  277. end= page+mi_getint(page);
  278. nod_flag=mi_test_if_nod(page);
  279. page+=2+nod_flag;
  280. *ret_pos=page;
  281. kseg=key;
  282. get_key_pack_length(kseg_len,length_pack,kseg);
  283. key_len_skip=length_pack+kseg_len;
  284. key_len_left=(int) key_len- (int) key_len_skip;
  285. /* If key_len is 0, then lenght_pack is 1, then key_len_left is -1. */
  286. cmplen=(key_len_left>=0) ? kseg_len : key_len-length_pack;
  287. DBUG_PRINT("info",("key: '%.*s'",kseg_len,kseg));
  288. /*
  289. Keys are compressed the following way:
  290. If the max length of first key segment <= 127 bytes the prefix is
  291. 1 byte else it's 2 byte
  292. (prefix) length The high bit is set if this is a prefix for the prev key.
  293. [suffix length] Packed length of suffix if the previous was a prefix.
  294. (suffix) data Key data bytes (past the common prefix or whole segment).
  295. [next-key-seg] Next key segments (([packed length], data), ...)
  296. pointer Reference to the data file (last_keyseg->length).
  297. */
  298. matched=0; /* how many char's from prefix were alredy matched */
  299. len=0; /* length of previous key unpacked */
  300. while (page < end)
  301. {
  302. uint packed= *page & 128;
  303. vseg=page;
  304. if (keyinfo->seg->length >= 127)
  305. {
  306. suffix_len=mi_uint2korr(vseg) & 32767;
  307. vseg+=2;
  308. }
  309. else
  310. suffix_len= *vseg++ & 127;
  311. if (packed)
  312. {
  313. if (suffix_len == 0)
  314. {
  315. /* == 0x80 or 0x8000, same key, prefix length == old key length. */
  316. prefix_len=len;
  317. }
  318. else
  319. {
  320. /* > 0x80 or 0x8000, this is prefix lgt, packed suffix lgt follows. */
  321. prefix_len=suffix_len;
  322. get_key_length(suffix_len,vseg);
  323. }
  324. }
  325. else
  326. {
  327. /* Not packed. No prefix used from last key. */
  328. prefix_len=0;
  329. }
  330. len=prefix_len+suffix_len;
  331. seg_len_pack=get_pack_length(len);
  332. t_buff=tt_buff+3-seg_len_pack;
  333. store_key_length(t_buff,len);
  334. if (prefix_len > saved_prefix_len)
  335. memcpy(t_buff+seg_len_pack+saved_prefix_len,saved_vseg,
  336. prefix_len-saved_prefix_len);
  337. saved_vseg=vseg;
  338. saved_prefix_len=prefix_len;
  339. DBUG_PRINT("loop",("page: '%.*s%.*s'",prefix_len,t_buff+seg_len_pack,
  340. suffix_len,vseg));
  341. {
  342. uchar *from=vseg+suffix_len;
  343. HA_KEYSEG *keyseg;
  344. uint l;
  345. for (keyseg=keyinfo->seg+1 ; keyseg->type ; keyseg++ )
  346. {
  347. if (keyseg->flag & HA_NULL_PART)
  348. {
  349. if (!(*from++))
  350. continue;
  351. }
  352. if (keyseg->flag & (HA_VAR_LENGTH_PART | HA_BLOB_PART | HA_SPACE_PACK))
  353. {
  354. get_key_length(l,from);
  355. }
  356. else
  357. l=keyseg->length;
  358. from+=l;
  359. }
  360. from+=keyseg->length;
  361. page=from+nod_flag;
  362. length=from-vseg;
  363. }
  364. if (page > end)
  365. {
  366. mi_print_error(info->s, HA_ERR_CRASHED);
  367. my_errno=HA_ERR_CRASHED;
  368. DBUG_PRINT("error",("Found wrong key: length: %u page: %lx end: %lx",
  369. length, (long) page, (long) end));
  370. DBUG_RETURN(MI_FOUND_WRONG_KEY);
  371. }
  372. if (matched >= prefix_len)
  373. {
  374. /* We have to compare. But we can still skip part of the key */
  375. uint left;
  376. uchar *k=kseg+prefix_len;
  377. /*
  378. If prefix_len > cmplen then we are in the end-space comparison
  379. phase. Do not try to acces the key any more ==> left= 0.
  380. */
  381. left= ((len <= cmplen) ? suffix_len :
  382. ((prefix_len < cmplen) ? cmplen - prefix_len : 0));
  383. matched=prefix_len+left;
  384. if (sort_order)
  385. {
  386. for (my_flag=0;left;left--)
  387. if ((my_flag= (int) sort_order[*vseg++] - (int) sort_order[*k++]))
  388. break;
  389. }
  390. else
  391. {
  392. for (my_flag=0;left;left--)
  393. if ((my_flag= (int) *vseg++ - (int) *k++))
  394. break;
  395. }
  396. if (my_flag>0) /* mismatch */
  397. break;
  398. if (my_flag==0) /* match */
  399. {
  400. /*
  401. ** len cmplen seg_left_len more_segs
  402. ** < matched=len; continue search
  403. ** > = prefix ? found : (matched=len; continue search)
  404. ** > < - ok, found
  405. ** = < - ok, found
  406. ** = = - ok, found
  407. ** = = + next seg
  408. */
  409. if (len < cmplen)
  410. {
  411. if ((keyinfo->seg->type != HA_KEYTYPE_TEXT &&
  412. keyinfo->seg->type != HA_KEYTYPE_VARTEXT1 &&
  413. keyinfo->seg->type != HA_KEYTYPE_VARTEXT2))
  414. my_flag= -1;
  415. else
  416. {
  417. /* We have to compare k and vseg as if they were space extended */
  418. uchar *k_end= k+ (cmplen - len);
  419. for ( ; k < k_end && *k == ' '; k++) ;
  420. if (k == k_end)
  421. goto cmp_rest; /* should never happen */
  422. if (*k < (uchar) ' ')
  423. {
  424. my_flag= 1; /* Compared string is smaller */
  425. break;
  426. }
  427. my_flag= -1; /* Continue searching */
  428. }
  429. }
  430. else if (len > cmplen)
  431. {
  432. uchar *vseg_end;
  433. if ((nextflag & SEARCH_PREFIX) && key_len_left == 0)
  434. goto fix_flag;
  435. /* We have to compare k and vseg as if they were space extended */
  436. for (vseg_end= vseg + (len-cmplen) ;
  437. vseg < vseg_end && *vseg == (uchar) ' ';
  438. vseg++, matched++) ;
  439. DBUG_ASSERT(vseg < vseg_end);
  440. if (*vseg > (uchar) ' ')
  441. {
  442. my_flag= 1; /* Compared string is smaller */
  443. break;
  444. }
  445. my_flag= -1; /* Continue searching */
  446. }
  447. else
  448. {
  449. cmp_rest:
  450. if (key_len_left>0)
  451. {
  452. uint not_used[2];
  453. if ((flag = ha_key_cmp(keyinfo->seg+1,vseg,
  454. k, key_len_left, nextflag, not_used)) >= 0)
  455. break;
  456. }
  457. else
  458. {
  459. /*
  460. at this line flag==-1 if the following lines were already
  461. visited and 0 otherwise, i.e. flag <=0 here always !!!
  462. */
  463. fix_flag:
  464. DBUG_ASSERT(flag <= 0);
  465. if (nextflag & (SEARCH_NO_FIND | SEARCH_LAST))
  466. flag=(nextflag & (SEARCH_BIGGER | SEARCH_LAST)) ? -1 : 1;
  467. if (flag>=0)
  468. break;
  469. }
  470. }
  471. }
  472. matched-=left;
  473. }
  474. /* else (matched < prefix_len) ---> do nothing. */
  475. memcpy(buff,t_buff,saved_length=seg_len_pack+prefix_len);
  476. saved_to=buff+saved_length;
  477. saved_from=saved_vseg;
  478. saved_length=length;
  479. *ret_pos=page;
  480. }
  481. if (my_flag)
  482. flag=(keyinfo->seg->flag & HA_REVERSE_SORT) ? -my_flag : my_flag;
  483. if (flag == 0)
  484. {
  485. memcpy(buff,t_buff,saved_length=seg_len_pack+prefix_len);
  486. saved_to=buff+saved_length;
  487. saved_from=saved_vseg;
  488. saved_length=length;
  489. }
  490. if (saved_length)
  491. memcpy(saved_to,saved_from,saved_length);
  492. *last_key= page == end;
  493. DBUG_PRINT("exit",("flag: %d ret_pos: %lx", flag, (long) *ret_pos));
  494. DBUG_RETURN(flag);
  495. } /* _mi_prefix_search */
  496. /* Get pos to a key_block */
  497. my_off_t _mi_kpos(uint nod_flag, uchar *after_key)
  498. {
  499. after_key-=nod_flag;
  500. switch (nod_flag) {
  501. #if SIZEOF_OFF_T > 4
  502. case 7:
  503. return mi_uint7korr(after_key)*MI_MIN_KEY_BLOCK_LENGTH;
  504. case 6:
  505. return mi_uint6korr(after_key)*MI_MIN_KEY_BLOCK_LENGTH;
  506. case 5:
  507. return mi_uint5korr(after_key)*MI_MIN_KEY_BLOCK_LENGTH;
  508. #else
  509. case 7:
  510. after_key++;
  511. case 6:
  512. after_key++;
  513. case 5:
  514. after_key++;
  515. #endif
  516. case 4:
  517. return ((my_off_t) mi_uint4korr(after_key))*MI_MIN_KEY_BLOCK_LENGTH;
  518. case 3:
  519. return ((my_off_t) mi_uint3korr(after_key))*MI_MIN_KEY_BLOCK_LENGTH;
  520. case 2:
  521. return (my_off_t) (mi_uint2korr(after_key)*MI_MIN_KEY_BLOCK_LENGTH);
  522. case 1:
  523. return (uint) (*after_key)*MI_MIN_KEY_BLOCK_LENGTH;
  524. case 0: /* At leaf page */
  525. default: /* Impossible */
  526. return(HA_OFFSET_ERROR);
  527. }
  528. } /* _kpos */
  529. /* Save pos to a key_block */
  530. void _mi_kpointer(register MI_INFO *info, register uchar *buff, my_off_t pos)
  531. {
  532. pos/=MI_MIN_KEY_BLOCK_LENGTH;
  533. switch (info->s->base.key_reflength) {
  534. #if SIZEOF_OFF_T > 4
  535. case 7: mi_int7store(buff,pos); break;
  536. case 6: mi_int6store(buff,pos); break;
  537. case 5: mi_int5store(buff,pos); break;
  538. #else
  539. case 7: *buff++=0;
  540. /* fall trough */
  541. case 6: *buff++=0;
  542. /* fall trough */
  543. case 5: *buff++=0;
  544. /* fall trough */
  545. #endif
  546. case 4: mi_int4store(buff,pos); break;
  547. case 3: mi_int3store(buff,pos); break;
  548. case 2: mi_int2store(buff,(uint) pos); break;
  549. case 1: buff[0]= (uchar) pos; break;
  550. default: abort(); /* impossible */
  551. }
  552. } /* _mi_kpointer */
  553. /* Calc pos to a data-record from a key */
  554. my_off_t _mi_dpos(MI_INFO *info, uint nod_flag, uchar *after_key)
  555. {
  556. my_off_t pos;
  557. after_key-=(nod_flag + info->s->rec_reflength);
  558. switch (info->s->rec_reflength) {
  559. #if SIZEOF_OFF_T > 4
  560. case 8: pos= (my_off_t) mi_uint8korr(after_key); break;
  561. case 7: pos= (my_off_t) mi_uint7korr(after_key); break;
  562. case 6: pos= (my_off_t) mi_uint6korr(after_key); break;
  563. case 5: pos= (my_off_t) mi_uint5korr(after_key); break;
  564. #else
  565. case 8: pos= (my_off_t) mi_uint4korr(after_key+4); break;
  566. case 7: pos= (my_off_t) mi_uint4korr(after_key+3); break;
  567. case 6: pos= (my_off_t) mi_uint4korr(after_key+2); break;
  568. case 5: pos= (my_off_t) mi_uint4korr(after_key+1); break;
  569. #endif
  570. case 4: pos= (my_off_t) mi_uint4korr(after_key); break;
  571. case 3: pos= (my_off_t) mi_uint3korr(after_key); break;
  572. case 2: pos= (my_off_t) mi_uint2korr(after_key); break;
  573. default:
  574. pos=0L; /* Shut compiler up */
  575. }
  576. return (info->s->options &
  577. (HA_OPTION_PACK_RECORD | HA_OPTION_COMPRESS_RECORD)) ? pos :
  578. pos*info->s->base.pack_reclength;
  579. }
  580. /* Calc position from a record pointer ( in delete link chain ) */
  581. my_off_t _mi_rec_pos(MYISAM_SHARE *s, uchar *ptr)
  582. {
  583. my_off_t pos;
  584. switch (s->rec_reflength) {
  585. #if SIZEOF_OFF_T > 4
  586. case 8:
  587. pos= (my_off_t) mi_uint8korr(ptr);
  588. if (pos == HA_OFFSET_ERROR)
  589. return HA_OFFSET_ERROR; /* end of list */
  590. break;
  591. case 7:
  592. pos= (my_off_t) mi_uint7korr(ptr);
  593. if (pos == (((my_off_t) 1) << 56) -1)
  594. return HA_OFFSET_ERROR; /* end of list */
  595. break;
  596. case 6:
  597. pos= (my_off_t) mi_uint6korr(ptr);
  598. if (pos == (((my_off_t) 1) << 48) -1)
  599. return HA_OFFSET_ERROR; /* end of list */
  600. break;
  601. case 5:
  602. pos= (my_off_t) mi_uint5korr(ptr);
  603. if (pos == (((my_off_t) 1) << 40) -1)
  604. return HA_OFFSET_ERROR; /* end of list */
  605. break;
  606. #else
  607. case 8:
  608. case 7:
  609. case 6:
  610. case 5:
  611. ptr+= (s->rec_reflength-4);
  612. /* fall through */
  613. #endif
  614. case 4:
  615. pos= (my_off_t) mi_uint4korr(ptr);
  616. if (pos == (my_off_t) (uint32) ~0L)
  617. return HA_OFFSET_ERROR;
  618. break;
  619. case 3:
  620. pos= (my_off_t) mi_uint3korr(ptr);
  621. if (pos == (my_off_t) (1 << 24) -1)
  622. return HA_OFFSET_ERROR;
  623. break;
  624. case 2:
  625. pos= (my_off_t) mi_uint2korr(ptr);
  626. if (pos == (my_off_t) (1 << 16) -1)
  627. return HA_OFFSET_ERROR;
  628. break;
  629. default: abort(); /* Impossible */
  630. }
  631. return ((s->options &
  632. (HA_OPTION_PACK_RECORD | HA_OPTION_COMPRESS_RECORD)) ? pos :
  633. pos*s->base.pack_reclength);
  634. }
  635. /* save position to record */
  636. void _mi_dpointer(MI_INFO *info, uchar *buff, my_off_t pos)
  637. {
  638. if (!(info->s->options &
  639. (HA_OPTION_PACK_RECORD | HA_OPTION_COMPRESS_RECORD)) &&
  640. pos != HA_OFFSET_ERROR)
  641. pos/=info->s->base.pack_reclength;
  642. switch (info->s->rec_reflength) {
  643. #if SIZEOF_OFF_T > 4
  644. case 8: mi_int8store(buff,pos); break;
  645. case 7: mi_int7store(buff,pos); break;
  646. case 6: mi_int6store(buff,pos); break;
  647. case 5: mi_int5store(buff,pos); break;
  648. #else
  649. case 8: *buff++=0;
  650. /* fall trough */
  651. case 7: *buff++=0;
  652. /* fall trough */
  653. case 6: *buff++=0;
  654. /* fall trough */
  655. case 5: *buff++=0;
  656. /* fall trough */
  657. #endif
  658. case 4: mi_int4store(buff,pos); break;
  659. case 3: mi_int3store(buff,pos); break;
  660. case 2: mi_int2store(buff,(uint) pos); break;
  661. default: abort(); /* Impossible */
  662. }
  663. } /* _mi_dpointer */
  664. /* Get key from key-block */
  665. /* page points at previous key; its advanced to point at next key */
  666. /* key should contain previous key */
  667. /* Returns length of found key + pointers */
  668. /* nod_flag is a flag if we are on nod */
  669. /* same as _mi_get_key but used with fixed length keys */
  670. uint _mi_get_static_key(register MI_KEYDEF *keyinfo, uint nod_flag,
  671. register uchar **page, register uchar *key)
  672. {
  673. memcpy((byte*) key,(byte*) *page,
  674. (size_t) (keyinfo->keylength+nod_flag));
  675. *page+=keyinfo->keylength+nod_flag;
  676. return(keyinfo->keylength);
  677. } /* _mi_get_static_key */
  678. /*
  679. get key witch is packed against previous key or key with a NULL column.
  680. SYNOPSIS
  681. _mi_get_pack_key()
  682. keyinfo key definition information.
  683. nod_flag If nod: Length of node pointer, else zero.
  684. page_pos RETURN position in key page behind this key.
  685. key IN/OUT in: prev key, out: unpacked key.
  686. RETURN
  687. key_length + length of data pointer
  688. */
  689. uint _mi_get_pack_key(register MI_KEYDEF *keyinfo, uint nod_flag,
  690. register uchar **page_pos, register uchar *key)
  691. {
  692. reg1 HA_KEYSEG *keyseg;
  693. uchar *start_key,*page=*page_pos;
  694. uint length;
  695. start_key=key;
  696. for (keyseg=keyinfo->seg ; keyseg->type ;keyseg++)
  697. {
  698. if (keyseg->flag & HA_PACK_KEY)
  699. {
  700. /* key with length, packed to previous key */
  701. uchar *start=key;
  702. uint packed= *page & 128,tot_length,rest_length;
  703. if (keyseg->length >= 127)
  704. {
  705. length=mi_uint2korr(page) & 32767;
  706. page+=2;
  707. }
  708. else
  709. length= *page++ & 127;
  710. if (packed)
  711. {
  712. if (length > (uint) keyseg->length)
  713. {
  714. mi_print_error(keyinfo->share, HA_ERR_CRASHED);
  715. my_errno=HA_ERR_CRASHED;
  716. return 0; /* Error */
  717. }
  718. if (length == 0) /* Same key */
  719. {
  720. if (keyseg->flag & HA_NULL_PART)
  721. *key++=1; /* Can't be NULL */
  722. get_key_length(length,key);
  723. key+= length; /* Same diff_key as prev */
  724. if (length > keyseg->length)
  725. {
  726. DBUG_PRINT("error",
  727. ("Found too long null packed key: %u of %u at %lx",
  728. length, keyseg->length, (long) *page_pos));
  729. DBUG_DUMP("key",(char*) *page_pos,16);
  730. mi_print_error(keyinfo->share, HA_ERR_CRASHED);
  731. my_errno=HA_ERR_CRASHED;
  732. return 0;
  733. }
  734. continue;
  735. }
  736. if (keyseg->flag & HA_NULL_PART)
  737. {
  738. key++; /* Skip null marker*/
  739. start++;
  740. }
  741. get_key_length(rest_length,page);
  742. tot_length=rest_length+length;
  743. /* If the stored length has changed, we must move the key */
  744. if (tot_length >= 255 && *start != 255)
  745. {
  746. /* length prefix changed from a length of one to a length of 3 */
  747. bmove_upp((char*) key+length+3,(char*) key+length+1,length);
  748. *key=255;
  749. mi_int2store(key+1,tot_length);
  750. key+=3+length;
  751. }
  752. else if (tot_length < 255 && *start == 255)
  753. {
  754. bmove(key+1,key+3,length);
  755. *key=tot_length;
  756. key+=1+length;
  757. }
  758. else
  759. {
  760. store_key_length_inc(key,tot_length);
  761. key+=length;
  762. }
  763. memcpy(key,page,rest_length);
  764. page+=rest_length;
  765. key+=rest_length;
  766. continue;
  767. }
  768. else
  769. {
  770. if (keyseg->flag & HA_NULL_PART)
  771. {
  772. if (!length--) /* Null part */
  773. {
  774. *key++=0;
  775. continue;
  776. }
  777. *key++=1; /* Not null */
  778. }
  779. }
  780. if (length > (uint) keyseg->length)
  781. {
  782. DBUG_PRINT("error",("Found too long packed key: %u of %u at %lx",
  783. length, keyseg->length, (long) *page_pos));
  784. DBUG_DUMP("key",(char*) *page_pos,16);
  785. mi_print_error(keyinfo->share, HA_ERR_CRASHED);
  786. my_errno=HA_ERR_CRASHED;
  787. return 0; /* Error */
  788. }
  789. store_key_length_inc(key,length);
  790. }
  791. else
  792. {
  793. if (keyseg->flag & HA_NULL_PART)
  794. {
  795. if (!(*key++ = *page++))
  796. continue;
  797. }
  798. if (keyseg->flag &
  799. (HA_VAR_LENGTH_PART | HA_BLOB_PART | HA_SPACE_PACK))
  800. {
  801. uchar *tmp=page;
  802. get_key_length(length,tmp);
  803. length+=(uint) (tmp-page);
  804. }
  805. else
  806. length=keyseg->length;
  807. }
  808. memcpy((byte*) key,(byte*) page,(size_t) length);
  809. key+=length;
  810. page+=length;
  811. }
  812. length=keyseg->length+nod_flag;
  813. bmove((byte*) key,(byte*) page,length);
  814. *page_pos= page+length;
  815. return ((uint) (key-start_key)+keyseg->length);
  816. } /* _mi_get_pack_key */
  817. /* key that is packed relatively to previous */
  818. uint _mi_get_binary_pack_key(register MI_KEYDEF *keyinfo, uint nod_flag,
  819. register uchar **page_pos, register uchar *key)
  820. {
  821. reg1 HA_KEYSEG *keyseg;
  822. uchar *start_key,*page,*page_end,*from,*from_end;
  823. uint length,tmp;
  824. DBUG_ENTER("_mi_get_binary_pack_key");
  825. page= *page_pos;
  826. page_end=page+MI_MAX_KEY_BUFF+1;
  827. start_key=key;
  828. /*
  829. Keys are compressed the following way:
  830. prefix length Packed length of prefix common with prev key (1 or 3 bytes)
  831. for each key segment:
  832. [is null] Null indicator if can be null (1 byte, zero means null)
  833. [length] Packed length if varlength (1 or 3 bytes)
  834. key segment 'length' bytes of key segment value
  835. pointer Reference to the data file (last_keyseg->length).
  836. get_key_length() is a macro. It gets the prefix length from 'page'
  837. and puts it into 'length'. It increments 'page' by 1 or 3, depending
  838. on the packed length of the prefix length.
  839. */
  840. get_key_length(length,page);
  841. if (length)
  842. {
  843. if (length > keyinfo->maxlength)
  844. {
  845. DBUG_PRINT("error",("Found too long binary packed key: %u of %u at %lx",
  846. length, keyinfo->maxlength, (long) *page_pos));
  847. DBUG_DUMP("key",(char*) *page_pos,16);
  848. mi_print_error(keyinfo->share, HA_ERR_CRASHED);
  849. my_errno=HA_ERR_CRASHED;
  850. DBUG_RETURN(0); /* Wrong key */
  851. }
  852. /* Key is packed against prev key, take prefix from prev key. */
  853. from= key;
  854. from_end= key + length;
  855. }
  856. else
  857. {
  858. /* Key is not packed against prev key, take all from page buffer. */
  859. from= page;
  860. from_end= page_end;
  861. }
  862. /*
  863. The trouble is that key can be split in two parts:
  864. The first part (prefix) is in from .. from_end - 1.
  865. The second part starts at page.
  866. The split can be at every byte position. So we need to check for
  867. the end of the first part before using every byte.
  868. */
  869. for (keyseg=keyinfo->seg ; keyseg->type ;keyseg++)
  870. {
  871. if (keyseg->flag & HA_NULL_PART)
  872. {
  873. /* If prefix is used up, switch to rest. */
  874. if (from == from_end) { from=page; from_end=page_end; }
  875. if (!(*key++ = *from++))
  876. continue; /* Null part */
  877. }
  878. if (keyseg->flag & (HA_VAR_LENGTH_PART | HA_BLOB_PART | HA_SPACE_PACK))
  879. {
  880. /* If prefix is used up, switch to rest. */
  881. if (from == from_end) { from=page; from_end=page_end; }
  882. /* Get length of dynamic length key part */
  883. if ((length= (*key++ = *from++)) == 255)
  884. {
  885. /* If prefix is used up, switch to rest. */
  886. if (from == from_end) { from=page; from_end=page_end; }
  887. length= (uint) ((*key++ = *from++)) << 8;
  888. /* If prefix is used up, switch to rest. */
  889. if (from == from_end) { from=page; from_end=page_end; }
  890. length+= (uint) ((*key++ = *from++));
  891. }
  892. }
  893. else
  894. length=keyseg->length;
  895. if ((tmp=(uint) (from_end-from)) <= length)
  896. {
  897. key+=tmp; /* Use old key */
  898. length-=tmp;
  899. from=page; from_end=page_end;
  900. }
  901. DBUG_PRINT("info",("key: %lx from: %lx length: %u",
  902. (long) key, (long) from, length));
  903. memmove((byte*) key, (byte*) from, (size_t) length);
  904. key+=length;
  905. from+=length;
  906. }
  907. /*
  908. Last segment (type == 0) contains length of data pointer.
  909. If we have mixed key blocks with data pointer and key block pointer,
  910. we have to copy both.
  911. */
  912. length=keyseg->length+nod_flag;
  913. if ((tmp=(uint) (from_end-from)) <= length)
  914. {
  915. /* Remaining length is less or equal max possible length. */
  916. memcpy(key+tmp,page,length-tmp); /* Get last part of key */
  917. *page_pos= page+length-tmp;
  918. }
  919. else
  920. {
  921. /*
  922. Remaining length is greater than max possible length.
  923. This can happen only if we switched to the new key bytes already.
  924. 'page_end' is calculated with MI_MAX_KEY_BUFF. So it can be far
  925. behind the real end of the key.
  926. */
  927. if (from_end != page_end)
  928. {
  929. DBUG_PRINT("error",("Error when unpacking key"));
  930. mi_print_error(keyinfo->share, HA_ERR_CRASHED);
  931. my_errno=HA_ERR_CRASHED;
  932. DBUG_RETURN(0); /* Error */
  933. }
  934. /* Copy data pointer and, if appropriate, key block pointer. */
  935. memcpy((byte*) key,(byte*) from,(size_t) length);
  936. *page_pos= from+length;
  937. }
  938. DBUG_RETURN((uint) (key-start_key)+keyseg->length);
  939. }
  940. /* Get key at position without knowledge of previous key */
  941. /* Returns pointer to next key */
  942. uchar *_mi_get_key(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *page,
  943. uchar *key, uchar *keypos, uint *return_key_length)
  944. {
  945. uint nod_flag;
  946. DBUG_ENTER("_mi_get_key");
  947. nod_flag=mi_test_if_nod(page);
  948. if (! (keyinfo->flag & (HA_VAR_LENGTH_KEY | HA_BINARY_PACK_KEY)))
  949. {
  950. bmove((byte*) key,(byte*) keypos,keyinfo->keylength+nod_flag);
  951. DBUG_RETURN(keypos+keyinfo->keylength+nod_flag);
  952. }
  953. else
  954. {
  955. page+=2+nod_flag;
  956. key[0]=0; /* safety */
  957. while (page <= keypos)
  958. {
  959. *return_key_length=(*keyinfo->get_key)(keyinfo,nod_flag,&page,key);
  960. if (*return_key_length == 0)
  961. {
  962. mi_print_error(info->s, HA_ERR_CRASHED);
  963. my_errno=HA_ERR_CRASHED;
  964. DBUG_RETURN(0);
  965. }
  966. }
  967. }
  968. DBUG_PRINT("exit",("page: %lx length: %u", (long) page,
  969. *return_key_length));
  970. DBUG_RETURN(page);
  971. } /* _mi_get_key */
  972. /* Get key at position without knowledge of previous key */
  973. /* Returns 0 if ok */
  974. static my_bool _mi_get_prev_key(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *page,
  975. uchar *key, uchar *keypos,
  976. uint *return_key_length)
  977. {
  978. uint nod_flag;
  979. DBUG_ENTER("_mi_get_prev_key");
  980. nod_flag=mi_test_if_nod(page);
  981. if (! (keyinfo->flag & (HA_VAR_LENGTH_KEY | HA_BINARY_PACK_KEY)))
  982. {
  983. *return_key_length=keyinfo->keylength;
  984. bmove((byte*) key,(byte*) keypos- *return_key_length-nod_flag,
  985. *return_key_length);
  986. DBUG_RETURN(0);
  987. }
  988. else
  989. {
  990. page+=2+nod_flag;
  991. key[0]=0; /* safety */
  992. while (page < keypos)
  993. {
  994. *return_key_length=(*keyinfo->get_key)(keyinfo,nod_flag,&page,key);
  995. if (*return_key_length == 0)
  996. {
  997. mi_print_error(info->s, HA_ERR_CRASHED);
  998. my_errno=HA_ERR_CRASHED;
  999. DBUG_RETURN(1);
  1000. }
  1001. }
  1002. }
  1003. DBUG_RETURN(0);
  1004. } /* _mi_get_key */
  1005. /* Get last key from key-page */
  1006. /* Return pointer to where key starts */
  1007. uchar *_mi_get_last_key(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *page,
  1008. uchar *lastkey, uchar *endpos, uint *return_key_length)
  1009. {
  1010. uint nod_flag;
  1011. uchar *lastpos;
  1012. DBUG_ENTER("_mi_get_last_key");
  1013. DBUG_PRINT("enter",("page: %lx endpos: %lx", (long) page, (long) endpos));
  1014. nod_flag=mi_test_if_nod(page);
  1015. if (! (keyinfo->flag & (HA_VAR_LENGTH_KEY | HA_BINARY_PACK_KEY)))
  1016. {
  1017. lastpos=endpos-keyinfo->keylength-nod_flag;
  1018. *return_key_length=keyinfo->keylength;
  1019. if (lastpos > page)
  1020. bmove((byte*) lastkey,(byte*) lastpos,keyinfo->keylength+nod_flag);
  1021. }
  1022. else
  1023. {
  1024. lastpos=(page+=2+nod_flag);
  1025. lastkey[0]=0;
  1026. while (page < endpos)
  1027. {
  1028. lastpos=page;
  1029. *return_key_length=(*keyinfo->get_key)(keyinfo,nod_flag,&page,lastkey);
  1030. if (*return_key_length == 0)
  1031. {
  1032. DBUG_PRINT("error",("Couldn't find last key: page: %lx",
  1033. (long) page));
  1034. mi_print_error(info->s, HA_ERR_CRASHED);
  1035. my_errno=HA_ERR_CRASHED;
  1036. DBUG_RETURN(0);
  1037. }
  1038. }
  1039. }
  1040. DBUG_PRINT("exit",("lastpos: %lx length: %u", (long) lastpos,
  1041. *return_key_length));
  1042. DBUG_RETURN(lastpos);
  1043. } /* _mi_get_last_key */
  1044. /* Calculate length of key */
  1045. uint _mi_keylength(MI_KEYDEF *keyinfo, register uchar *key)
  1046. {
  1047. reg1 HA_KEYSEG *keyseg;
  1048. uchar *start;
  1049. if (! (keyinfo->flag & (HA_VAR_LENGTH_KEY | HA_BINARY_PACK_KEY)))
  1050. return (keyinfo->keylength);
  1051. start=key;
  1052. for (keyseg=keyinfo->seg ; keyseg->type ; keyseg++)
  1053. {
  1054. if (keyseg->flag & HA_NULL_PART)
  1055. if (!*key++)
  1056. continue;
  1057. if (keyseg->flag & (HA_SPACE_PACK | HA_BLOB_PART | HA_VAR_LENGTH_PART))
  1058. {
  1059. uint length;
  1060. get_key_length(length,key);
  1061. key+=length;
  1062. }
  1063. else
  1064. key+= keyseg->length;
  1065. }
  1066. return((uint) (key-start)+keyseg->length);
  1067. } /* _mi_keylength */
  1068. /*
  1069. Calculate length of part key.
  1070. Used in mi_rkey() to find the key found for the key-part that was used.
  1071. This is needed in case of multi-byte character sets where we may search
  1072. after '0xDF' but find 'ss'
  1073. */
  1074. uint _mi_keylength_part(MI_KEYDEF *keyinfo, register uchar *key,
  1075. HA_KEYSEG *end)
  1076. {
  1077. reg1 HA_KEYSEG *keyseg;
  1078. uchar *start= key;
  1079. for (keyseg=keyinfo->seg ; keyseg != end ; keyseg++)
  1080. {
  1081. if (keyseg->flag & HA_NULL_PART)
  1082. if (!*key++)
  1083. continue;
  1084. if (keyseg->flag & (HA_SPACE_PACK | HA_BLOB_PART | HA_VAR_LENGTH_PART))
  1085. {
  1086. uint length;
  1087. get_key_length(length,key);
  1088. key+=length;
  1089. }
  1090. else
  1091. key+= keyseg->length;
  1092. }
  1093. return (uint) (key-start);
  1094. }
  1095. /* Move a key */
  1096. uchar *_mi_move_key(MI_KEYDEF *keyinfo, uchar *to, uchar *from)
  1097. {
  1098. reg1 uint length;
  1099. memcpy((byte*) to, (byte*) from,
  1100. (size_t) (length=_mi_keylength(keyinfo,from)));
  1101. return to+length;
  1102. }
  1103. /* Find next/previous record with same key */
  1104. /* This can't be used when database is touched after last read */
  1105. int _mi_search_next(register MI_INFO *info, register MI_KEYDEF *keyinfo,
  1106. uchar *key, uint key_length, uint nextflag, my_off_t pos)
  1107. {
  1108. int error;
  1109. uint nod_flag;
  1110. uchar lastkey[MI_MAX_KEY_BUFF];
  1111. DBUG_ENTER("_mi_search_next");
  1112. DBUG_PRINT("enter",("nextflag: %u lastpos: %lu int_keypos: %lu",
  1113. nextflag, (ulong) info->lastpos,
  1114. (ulong) info->int_keypos));
  1115. DBUG_EXECUTE("key",_mi_print_key(DBUG_FILE,keyinfo->seg,key,key_length););
  1116. /* Force full read if we are at last key or if we are not on a leaf
  1117. and the key tree has changed since we used it last time
  1118. Note that even if the key tree has changed since last read, we can use
  1119. the last read data from the leaf if we haven't used the buffer for
  1120. something else.
  1121. */
  1122. if (((nextflag & SEARCH_BIGGER) && info->int_keypos >= info->int_maxpos) ||
  1123. info->page_changed ||
  1124. (info->int_keytree_version != keyinfo->version &&
  1125. (info->int_nod_flag || info->buff_used)))
  1126. DBUG_RETURN(_mi_search(info,keyinfo,key, USE_WHOLE_KEY,
  1127. nextflag | SEARCH_SAVE_BUFF, pos));
  1128. if (info->buff_used)
  1129. {
  1130. if (!_mi_fetch_keypage(info,keyinfo,info->last_search_keypage,
  1131. DFLT_INIT_HITS,info->buff,0))
  1132. DBUG_RETURN(-1);
  1133. info->buff_used=0;
  1134. }
  1135. /* Last used buffer is in info->buff */
  1136. nod_flag=mi_test_if_nod(info->buff);
  1137. if (nextflag & SEARCH_BIGGER) /* Next key */
  1138. {
  1139. my_off_t tmp_pos=_mi_kpos(nod_flag,info->int_keypos);
  1140. if (tmp_pos != HA_OFFSET_ERROR)
  1141. {
  1142. if ((error=_mi_search(info,keyinfo,key, USE_WHOLE_KEY,
  1143. nextflag | SEARCH_SAVE_BUFF, tmp_pos)) <=0)
  1144. DBUG_RETURN(error);
  1145. }
  1146. memcpy(lastkey,key,key_length);
  1147. if (!(info->lastkey_length=(*keyinfo->get_key)(keyinfo,nod_flag,
  1148. &info->int_keypos,lastkey)))
  1149. DBUG_RETURN(-1);
  1150. }
  1151. else /* Previous key */
  1152. {
  1153. uint length;
  1154. /* Find start of previous key */
  1155. info->int_keypos=_mi_get_last_key(info,keyinfo,info->buff,lastkey,
  1156. info->int_keypos, &length);
  1157. if (!info->int_keypos)
  1158. DBUG_RETURN(-1);
  1159. if (info->int_keypos == info->buff+2)
  1160. DBUG_RETURN(_mi_search(info,keyinfo,key, USE_WHOLE_KEY,
  1161. nextflag | SEARCH_SAVE_BUFF, pos));
  1162. if ((error=_mi_search(info,keyinfo,key, USE_WHOLE_KEY,
  1163. nextflag | SEARCH_SAVE_BUFF,
  1164. _mi_kpos(nod_flag,info->int_keypos))) <= 0)
  1165. DBUG_RETURN(error);
  1166. /* QQ: We should be able to optimize away the following call */
  1167. if (! _mi_get_last_key(info,keyinfo,info->buff,lastkey,
  1168. info->int_keypos,&info->lastkey_length))
  1169. DBUG_RETURN(-1);
  1170. }
  1171. memcpy(info->lastkey,lastkey,info->lastkey_length);
  1172. info->lastpos=_mi_dpos(info,0,info->lastkey+info->lastkey_length);
  1173. DBUG_PRINT("exit",("found key at %lu",(ulong) info->lastpos));
  1174. DBUG_RETURN(0);
  1175. } /* _mi_search_next */
  1176. /* Search after position for the first row in an index */
  1177. /* This is stored in info->lastpos */
  1178. int _mi_search_first(register MI_INFO *info, register MI_KEYDEF *keyinfo,
  1179. register my_off_t pos)
  1180. {
  1181. uint nod_flag;
  1182. uchar *page;
  1183. DBUG_ENTER("_mi_search_first");
  1184. if (pos == HA_OFFSET_ERROR)
  1185. {
  1186. my_errno=HA_ERR_KEY_NOT_FOUND;
  1187. info->lastpos= HA_OFFSET_ERROR;
  1188. DBUG_RETURN(-1);
  1189. }
  1190. do
  1191. {
  1192. if (!_mi_fetch_keypage(info,keyinfo,pos,DFLT_INIT_HITS,info->buff,0))
  1193. {
  1194. info->lastpos= HA_OFFSET_ERROR;
  1195. DBUG_RETURN(-1);
  1196. }
  1197. nod_flag=mi_test_if_nod(info->buff);
  1198. page=info->buff+2+nod_flag;
  1199. } while ((pos=_mi_kpos(nod_flag,page)) != HA_OFFSET_ERROR);
  1200. if (!(info->lastkey_length=(*keyinfo->get_key)(keyinfo,nod_flag,&page,
  1201. info->lastkey)))
  1202. DBUG_RETURN(-1); /* Crashed */
  1203. info->int_keypos=page; info->int_maxpos=info->buff+mi_getint(info->buff)-1;
  1204. info->int_nod_flag=nod_flag;
  1205. info->int_keytree_version=keyinfo->version;
  1206. info->last_search_keypage=info->last_keypage;
  1207. info->page_changed=info->buff_used=0;
  1208. info->lastpos=_mi_dpos(info,0,info->lastkey+info->lastkey_length);
  1209. DBUG_PRINT("exit",("found key at %lu", (ulong) info->lastpos));
  1210. DBUG_RETURN(0);
  1211. } /* _mi_search_first */
  1212. /* Search after position for the last row in an index */
  1213. /* This is stored in info->lastpos */
  1214. int _mi_search_last(register MI_INFO *info, register MI_KEYDEF *keyinfo,
  1215. register my_off_t pos)
  1216. {
  1217. uint nod_flag;
  1218. uchar *buff,*page;
  1219. DBUG_ENTER("_mi_search_last");
  1220. if (pos == HA_OFFSET_ERROR)
  1221. {
  1222. my_errno=HA_ERR_KEY_NOT_FOUND; /* Didn't find key */
  1223. info->lastpos= HA_OFFSET_ERROR;
  1224. DBUG_RETURN(-1);
  1225. }
  1226. buff=info->buff;
  1227. do
  1228. {
  1229. if (!_mi_fetch_keypage(info,keyinfo,pos,DFLT_INIT_HITS,buff,0))
  1230. {
  1231. info->lastpos= HA_OFFSET_ERROR;
  1232. DBUG_RETURN(-1);
  1233. }
  1234. page= buff+mi_getint(buff);
  1235. nod_flag=mi_test_if_nod(buff);
  1236. } while ((pos=_mi_kpos(nod_flag,page)) != HA_OFFSET_ERROR);
  1237. if (!_mi_get_last_key(info,keyinfo,buff,info->lastkey,page,
  1238. &info->lastkey_length))
  1239. DBUG_RETURN(-1);
  1240. info->lastpos=_mi_dpos(info,0,info->lastkey+info->lastkey_length);
  1241. info->int_keypos=info->int_maxpos=page;
  1242. info->int_nod_flag=nod_flag;
  1243. info->int_keytree_version=keyinfo->version;
  1244. info->last_search_keypage=info->last_keypage;
  1245. info->page_changed=info->buff_used=0;
  1246. DBUG_PRINT("exit",("found key at %lu",(ulong) info->lastpos));
  1247. DBUG_RETURN(0);
  1248. } /* _mi_search_last */
  1249. /****************************************************************************
  1250. **
  1251. ** Functions to store and pack a key in a page
  1252. **
  1253. ** mi_calc_xx_key_length takes the following arguments:
  1254. ** nod_flag If nod: Length of nod-pointer
  1255. ** next_key Position to pos after the new key in buffer
  1256. ** org_key Key that was before the next key in buffer
  1257. ** prev_key Last key before current key
  1258. ** key Key that will be stored
  1259. ** s_temp Information how next key will be packed
  1260. ****************************************************************************/
  1261. /* Static length key */
  1262. int
  1263. _mi_calc_static_key_length(MI_KEYDEF *keyinfo,uint nod_flag,
  1264. uchar *next_pos __attribute__((unused)),
  1265. uchar *org_key __attribute__((unused)),
  1266. uchar *prev_key __attribute__((unused)),
  1267. uchar *key, MI_KEY_PARAM *s_temp)
  1268. {
  1269. s_temp->key=key;
  1270. return (int) (s_temp->totlength=keyinfo->keylength+nod_flag);
  1271. }
  1272. /* Variable length key */
  1273. int
  1274. _mi_calc_var_key_length(MI_KEYDEF *keyinfo,uint nod_flag,
  1275. uchar *next_pos __attribute__((unused)),
  1276. uchar *org_key __attribute__((unused)),
  1277. uchar *prev_key __attribute__((unused)),
  1278. uchar *key, MI_KEY_PARAM *s_temp)
  1279. {
  1280. s_temp->key=key;
  1281. return (int) (s_temp->totlength=_mi_keylength(keyinfo,key)+nod_flag);
  1282. }
  1283. /*
  1284. length of key with a variable length first segment which is prefix
  1285. compressed (myisamchk reports 'packed + stripped')
  1286. Keys are compressed the following way:
  1287. If the max length of first key segment <= 127 bytes the prefix is
  1288. 1 byte else it's 2 byte
  1289. prefix byte(s) The high bit is set if this is a prefix for the prev key
  1290. length Packed length if the previous was a prefix byte
  1291. [length] data bytes ('length' bytes)
  1292. next-key-seg Next key segments
  1293. If the first segment can have NULL:
  1294. The length is 0 for NULLS and 1+length for not null columns.
  1295. */
  1296. int
  1297. _mi_calc_var_pack_key_length(MI_KEYDEF *keyinfo,uint nod_flag,uchar *next_key,
  1298. uchar *org_key, uchar *prev_key, uchar *key,
  1299. MI_KEY_PARAM *s_temp)
  1300. {
  1301. reg1 HA_KEYSEG *keyseg;
  1302. int length;
  1303. uint key_length,ref_length,org_key_length=0,
  1304. length_pack,new_key_length,diff_flag,pack_marker;
  1305. uchar *start,*end,*key_end,*sort_order;
  1306. bool same_length;
  1307. length_pack=s_temp->ref_length=s_temp->n_ref_length=s_temp->n_length=0;
  1308. same_length=0; keyseg=keyinfo->seg;
  1309. key_length=_mi_keylength(keyinfo,key)+nod_flag;
  1310. sort_order=0;
  1311. if ((keyinfo->flag & HA_FULLTEXT) &&
  1312. ((keyseg->type == HA_KEYTYPE_TEXT) ||
  1313. (keyseg->type == HA_KEYTYPE_VARTEXT1) ||
  1314. (keyseg->type == HA_KEYTYPE_VARTEXT2)) &&
  1315. !use_strnxfrm(keyseg->charset))
  1316. sort_order=keyseg->charset->sort_order;
  1317. /* diff flag contains how many bytes is needed to pack key */
  1318. if (keyseg->length >= 127)
  1319. {
  1320. diff_flag=2;
  1321. pack_marker=32768;
  1322. }
  1323. else
  1324. {
  1325. diff_flag= 1;
  1326. pack_marker=128;
  1327. }
  1328. s_temp->pack_marker=pack_marker;
  1329. /* Handle the case that the first part have NULL values */
  1330. if (keyseg->flag & HA_NULL_PART)
  1331. {
  1332. if (!*key++)
  1333. {
  1334. s_temp->key=key;
  1335. s_temp->key_length= 0;
  1336. s_temp->totlength=key_length-1+diff_flag;
  1337. s_temp->next_key_pos=0; /* No next key */
  1338. return (s_temp->totlength);
  1339. }
  1340. s_temp->store_not_null=1;
  1341. key_length--; /* We don't store NULL */
  1342. if (prev_key && !*prev_key++)
  1343. org_key=prev_key=0; /* Can't pack against prev */
  1344. else if (org_key)
  1345. org_key++; /* Skip NULL */
  1346. }
  1347. else
  1348. s_temp->store_not_null=0;
  1349. s_temp->prev_key=org_key;
  1350. /* The key part will start with a packed length */
  1351. get_key_pack_length(new_key_length,length_pack,key);
  1352. end=key_end= key+ new_key_length;
  1353. start=key;
  1354. /* Calc how many characters are identical between this and the prev. key */
  1355. if (prev_key)
  1356. {
  1357. get_key_length(org_key_length,prev_key);
  1358. s_temp->prev_key=prev_key; /* Pointer at data */
  1359. /* Don't use key-pack if length == 0 */
  1360. if (new_key_length && new_key_length == org_key_length)
  1361. same_length=1;
  1362. else if (new_key_length > org_key_length)
  1363. end=key + org_key_length;
  1364. if (sort_order) /* SerG */
  1365. {
  1366. while (key < end && sort_order[*key] == sort_order[*prev_key])
  1367. {
  1368. key++; prev_key++;
  1369. }
  1370. }
  1371. else
  1372. {
  1373. while (key < end && *key == *prev_key)
  1374. {
  1375. key++; prev_key++;
  1376. }
  1377. }
  1378. }
  1379. s_temp->key=key;
  1380. s_temp->key_length= (uint) (key_end-key);
  1381. if (same_length && key == key_end)
  1382. {
  1383. /* identical variable length key */
  1384. s_temp->ref_length= pack_marker;
  1385. length=(int) key_length-(int) (key_end-start)-length_pack;
  1386. length+= diff_flag;
  1387. if (next_key)
  1388. { /* Can't combine with next */
  1389. s_temp->n_length= *next_key; /* Needed by _mi_store_key */
  1390. next_key=0;
  1391. }
  1392. }
  1393. else
  1394. {
  1395. if (start != key)
  1396. { /* Starts as prev key */
  1397. ref_length= (uint) (key-start);
  1398. s_temp->ref_length= ref_length + pack_marker;
  1399. length= (int) (key_length - ref_length);
  1400. length-= length_pack;
  1401. length+= diff_flag;
  1402. length+= ((new_key_length-ref_length) >= 255) ? 3 : 1;/* Rest_of_key */
  1403. }
  1404. else
  1405. {
  1406. s_temp->key_length+=s_temp->store_not_null; /* If null */
  1407. length= key_length - length_pack+ diff_flag;
  1408. }
  1409. }
  1410. s_temp->totlength=(uint) length;
  1411. s_temp->prev_length=0;
  1412. DBUG_PRINT("test",("tot_length: %u length: %d uniq_key_length: %u",
  1413. key_length, length, s_temp->key_length));
  1414. /* If something after that hasn't length=0, test if we can combine */
  1415. if ((s_temp->next_key_pos=next_key))
  1416. {
  1417. uint packed,n_length;
  1418. packed = *next_key & 128;
  1419. if (diff_flag == 2)
  1420. {
  1421. n_length= mi_uint2korr(next_key) & 32767; /* Length of next key */
  1422. next_key+=2;
  1423. }
  1424. else
  1425. n_length= *next_key++ & 127;
  1426. if (!packed)
  1427. n_length-= s_temp->store_not_null;
  1428. if (n_length || packed) /* Don't pack 0 length keys */
  1429. {
  1430. uint next_length_pack, new_ref_length=s_temp->ref_length;
  1431. if (packed)
  1432. {
  1433. /* If first key and next key is packed (only on delete) */
  1434. if (!prev_key && org_key)
  1435. {
  1436. get_key_length(org_key_length,org_key);
  1437. key=start;
  1438. if (sort_order) /* SerG */
  1439. {
  1440. while (key < end && sort_order[*key] == sort_order[*org_key])
  1441. {
  1442. key++; org_key++;
  1443. }
  1444. }
  1445. else
  1446. {
  1447. while (key < end && *key == *org_key)
  1448. {
  1449. key++; org_key++;
  1450. }
  1451. }
  1452. if ((new_ref_length= (uint) (key - start)))
  1453. new_ref_length+=pack_marker;
  1454. }
  1455. if (!n_length)
  1456. {
  1457. /*
  1458. We put a different key between two identical variable length keys
  1459. Extend next key to have same prefix as this key
  1460. */
  1461. if (new_ref_length) /* prefix of previus key */
  1462. { /* make next key longer */
  1463. s_temp->part_of_prev_key= new_ref_length;
  1464. s_temp->prev_length= org_key_length -
  1465. (new_ref_length-pack_marker);
  1466. s_temp->n_ref_length= s_temp->part_of_prev_key;
  1467. s_temp->n_length= s_temp->prev_length;
  1468. n_length= get_pack_length(s_temp->prev_length);
  1469. s_temp->prev_key+= (new_ref_length - pack_marker);
  1470. length+= s_temp->prev_length + n_length;
  1471. }
  1472. else
  1473. { /* Can't use prev key */
  1474. s_temp->part_of_prev_key=0;
  1475. s_temp->prev_length= org_key_length;
  1476. s_temp->n_ref_length=s_temp->n_length= org_key_length;
  1477. length+= org_key_length;
  1478. }
  1479. return (int) length;
  1480. }
  1481. ref_length=n_length;
  1482. /* Get information about not packed key suffix */
  1483. get_key_pack_length(n_length,next_length_pack,next_key);
  1484. /* Test if new keys has fewer characters that match the previous key */
  1485. if (!new_ref_length)
  1486. { /* Can't use prev key */
  1487. s_temp->part_of_prev_key= 0;
  1488. s_temp->prev_length= ref_length;
  1489. s_temp->n_ref_length= s_temp->n_length= n_length+ref_length;
  1490. return (int) length+ref_length-next_length_pack;
  1491. }
  1492. if (ref_length+pack_marker > new_ref_length)
  1493. {
  1494. uint new_pack_length=new_ref_length-pack_marker;
  1495. /* We must copy characters from the original key to the next key */
  1496. s_temp->part_of_prev_key= new_ref_length;
  1497. s_temp->prev_length= ref_length - new_pack_length;
  1498. s_temp->n_ref_length=s_temp->n_length=n_length + s_temp->prev_length;
  1499. s_temp->prev_key+= new_pack_length;
  1500. length-= (next_length_pack - get_pack_length(s_temp->n_length));
  1501. return (int) length + s_temp->prev_length;
  1502. }
  1503. }
  1504. else
  1505. {
  1506. /* Next key wasn't a prefix of previous key */
  1507. ref_length=0;
  1508. next_length_pack=0;
  1509. }
  1510. DBUG_PRINT("test",("length: %d next_key: %lx", length,
  1511. (long) next_key));
  1512. {
  1513. uint tmp_length;
  1514. key=(start+=ref_length);
  1515. if (key+n_length < key_end) /* Normalize length based */
  1516. key_end=key+n_length;
  1517. if (sort_order) /* SerG */
  1518. {
  1519. while (key < key_end && sort_order[*key] ==
  1520. sort_order[*next_key])
  1521. {
  1522. key++; next_key++;
  1523. }
  1524. }
  1525. else
  1526. {
  1527. while (key < key_end && *key == *next_key)
  1528. {
  1529. key++; next_key++;
  1530. }
  1531. }
  1532. if (!(tmp_length=(uint) (key-start)))
  1533. { /* Key can't be re-packed */
  1534. s_temp->next_key_pos=0;
  1535. return length;
  1536. }
  1537. ref_length+=tmp_length;
  1538. n_length-=tmp_length;
  1539. length-=tmp_length+next_length_pack; /* We gained these chars */
  1540. }
  1541. if (n_length == 0 && ref_length == new_key_length)
  1542. {
  1543. s_temp->n_ref_length=pack_marker; /* Same as prev key */
  1544. }
  1545. else
  1546. {
  1547. s_temp->n_ref_length=ref_length | pack_marker;
  1548. length+= get_pack_length(n_length);
  1549. s_temp->n_length=n_length;
  1550. }
  1551. }
  1552. }
  1553. return length;
  1554. }
  1555. /* Length of key which is prefix compressed */
  1556. int
  1557. _mi_calc_bin_pack_key_length(MI_KEYDEF *keyinfo,uint nod_flag,uchar *next_key,
  1558. uchar *org_key, uchar *prev_key, uchar *key,
  1559. MI_KEY_PARAM *s_temp)
  1560. {
  1561. uint length,key_length,ref_length;
  1562. s_temp->totlength=key_length=_mi_keylength(keyinfo,key)+nod_flag;
  1563. #ifdef HAVE_purify
  1564. s_temp->n_length= s_temp->n_ref_length=0; /* For valgrind */
  1565. #endif
  1566. s_temp->key=key;
  1567. s_temp->prev_key=org_key;
  1568. if (prev_key) /* If not first key in block */
  1569. {
  1570. /* pack key against previous key */
  1571. /*
  1572. As keys may be identical when running a sort in myisamchk, we
  1573. have to guard against the case where keys may be identical
  1574. */
  1575. uchar *end;
  1576. end=key+key_length;
  1577. for ( ; *key == *prev_key && key < end; key++,prev_key++) ;
  1578. s_temp->ref_length= ref_length=(uint) (key-s_temp->key);
  1579. length=key_length - ref_length + get_pack_length(ref_length);
  1580. }
  1581. else
  1582. {
  1583. /* No previous key */
  1584. s_temp->ref_length=ref_length=0;
  1585. length=key_length+1;
  1586. }
  1587. if ((s_temp->next_key_pos=next_key)) /* If another key after */
  1588. {
  1589. /* pack key against next key */
  1590. uint next_length,next_length_pack;
  1591. get_key_pack_length(next_length,next_length_pack,next_key);
  1592. /* If first key and next key is packed (only on delete) */
  1593. if (!prev_key && org_key && next_length)
  1594. {
  1595. uchar *end;
  1596. for (key= s_temp->key, end=key+next_length ;
  1597. *key == *org_key && key < end;
  1598. key++,org_key++) ;
  1599. ref_length= (uint) (key - s_temp->key);
  1600. }
  1601. if (next_length > ref_length)
  1602. {
  1603. /* We put a key with different case between two keys with the same prefix
  1604. Extend next key to have same prefix as
  1605. this key */
  1606. s_temp->n_ref_length= ref_length;
  1607. s_temp->prev_length= next_length-ref_length;
  1608. s_temp->prev_key+= ref_length;
  1609. return (int) (length+ s_temp->prev_length - next_length_pack +
  1610. get_pack_length(ref_length));
  1611. }
  1612. /* Check how many characters are identical to next key */
  1613. key= s_temp->key+next_length;
  1614. while (*key++ == *next_key++) ;
  1615. if ((ref_length= (uint) (key - s_temp->key)-1) == next_length)
  1616. {
  1617. s_temp->next_key_pos=0;
  1618. return length; /* can't pack next key */
  1619. }
  1620. s_temp->prev_length=0;
  1621. s_temp->n_ref_length=ref_length;
  1622. return (int) (length-(ref_length - next_length) - next_length_pack +
  1623. get_pack_length(ref_length));
  1624. }
  1625. return (int) length;
  1626. }
  1627. /*
  1628. ** store a key packed with _mi_calc_xxx_key_length in page-buffert
  1629. */
  1630. /* store key without compression */
  1631. void _mi_store_static_key(MI_KEYDEF *keyinfo __attribute__((unused)),
  1632. register uchar *key_pos,
  1633. register MI_KEY_PARAM *s_temp)
  1634. {
  1635. memcpy((byte*) key_pos,(byte*) s_temp->key,(size_t) s_temp->totlength);
  1636. }
  1637. /* store variable length key with prefix compression */
  1638. #define store_pack_length(test,pos,length) { \
  1639. if (test) { *((pos)++) = (uchar) (length); } else \
  1640. { *((pos)++) = (uchar) ((length) >> 8); *((pos)++) = (uchar) (length); } }
  1641. void _mi_store_var_pack_key(MI_KEYDEF *keyinfo __attribute__((unused)),
  1642. register uchar *key_pos,
  1643. register MI_KEY_PARAM *s_temp)
  1644. {
  1645. uint length;
  1646. uchar *start;
  1647. start=key_pos;
  1648. if (s_temp->ref_length)
  1649. {
  1650. /* Packed against previous key */
  1651. store_pack_length(s_temp->pack_marker == 128,key_pos,s_temp->ref_length);
  1652. /* If not same key after */
  1653. if (s_temp->ref_length != s_temp->pack_marker)
  1654. store_key_length_inc(key_pos,s_temp->key_length);
  1655. }
  1656. else
  1657. {
  1658. /* Not packed against previous key */
  1659. store_pack_length(s_temp->pack_marker == 128,key_pos,s_temp->key_length);
  1660. }
  1661. bmove((byte*) key_pos,(byte*) s_temp->key,
  1662. (length=s_temp->totlength-(uint) (key_pos-start)));
  1663. if (!s_temp->next_key_pos) /* No following key */
  1664. return;
  1665. key_pos+=length;
  1666. if (s_temp->prev_length)
  1667. {
  1668. /* Extend next key because new key didn't have same prefix as prev key */
  1669. if (s_temp->part_of_prev_key)
  1670. {
  1671. store_pack_length(s_temp->pack_marker == 128,key_pos,
  1672. s_temp->part_of_prev_key);
  1673. store_key_length_inc(key_pos,s_temp->n_length);
  1674. }
  1675. else
  1676. {
  1677. s_temp->n_length+= s_temp->store_not_null;
  1678. store_pack_length(s_temp->pack_marker == 128,key_pos,
  1679. s_temp->n_length);
  1680. }
  1681. memcpy(key_pos, s_temp->prev_key, s_temp->prev_length);
  1682. }
  1683. else if (s_temp->n_ref_length)
  1684. {
  1685. store_pack_length(s_temp->pack_marker == 128,key_pos,s_temp->n_ref_length);
  1686. if (s_temp->n_ref_length == s_temp->pack_marker)
  1687. return; /* Identical key */
  1688. store_key_length(key_pos,s_temp->n_length);
  1689. }
  1690. else
  1691. {
  1692. s_temp->n_length+= s_temp->store_not_null;
  1693. store_pack_length(s_temp->pack_marker == 128,key_pos,s_temp->n_length);
  1694. }
  1695. }
  1696. /* variable length key with prefix compression */
  1697. void _mi_store_bin_pack_key(MI_KEYDEF *keyinfo __attribute__((unused)),
  1698. register uchar *key_pos,
  1699. register MI_KEY_PARAM *s_temp)
  1700. {
  1701. store_key_length_inc(key_pos,s_temp->ref_length);
  1702. memcpy((char*) key_pos,(char*) s_temp->key+s_temp->ref_length,
  1703. (size_t) s_temp->totlength-s_temp->ref_length);
  1704. if (s_temp->next_key_pos)
  1705. {
  1706. key_pos+=(uint) (s_temp->totlength-s_temp->ref_length);
  1707. store_key_length_inc(key_pos,s_temp->n_ref_length);
  1708. if (s_temp->prev_length) /* If we must extend key */
  1709. {
  1710. memcpy(key_pos,s_temp->prev_key,s_temp->prev_length);
  1711. }
  1712. }
  1713. }