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.

599 lines
17 KiB

21 years ago
20 years ago
20 years ago
  1. /* Copyright (C) 2002-2006 MySQL AB
  2. This library is free software; you can redistribute it and/or
  3. modify it under the terms of the GNU Library General Public
  4. License as published by the Free Software Foundation; version 2
  5. of the License.
  6. This library is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  9. Library General Public License for more details.
  10. You should have received a copy of the GNU Library General Public
  11. License along with this library; if not, write to the Free
  12. Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  13. MA 02111-1307, USA */
  14. #include <my_global.h>
  15. #include <m_ctype.h>
  16. #include <my_base.h>
  17. #include <my_handler.h>
  18. #include <my_sys.h>
  19. #include "my_handler_errors.h"
  20. int ha_compare_text(CHARSET_INFO *charset_info, uchar *a, uint a_length,
  21. uchar *b, uint b_length, my_bool part_key,
  22. my_bool skip_end_space)
  23. {
  24. if (!part_key)
  25. return charset_info->coll->strnncollsp(charset_info, a, a_length,
  26. b, b_length, (my_bool)!skip_end_space);
  27. return charset_info->coll->strnncoll(charset_info, a, a_length,
  28. b, b_length, part_key);
  29. }
  30. static int compare_bin(uchar *a, uint a_length, uchar *b, uint b_length,
  31. my_bool part_key, my_bool skip_end_space)
  32. {
  33. uint length= min(a_length,b_length);
  34. uchar *end= a+ length;
  35. int flag;
  36. while (a < end)
  37. if ((flag= (int) *a++ - (int) *b++))
  38. return flag;
  39. if (part_key && b_length < a_length)
  40. return 0;
  41. if (skip_end_space && a_length != b_length)
  42. {
  43. int swap= 1;
  44. /*
  45. We are using space compression. We have to check if longer key
  46. has next character < ' ', in which case it's less than the shorter
  47. key that has an implicite space afterwards.
  48. This code is identical to the one in
  49. strings/ctype-simple.c:my_strnncollsp_simple
  50. */
  51. if (a_length < b_length)
  52. {
  53. /* put shorter key in a */
  54. a_length= b_length;
  55. a= b;
  56. swap= -1; /* swap sign of result */
  57. }
  58. for (end= a + a_length-length; a < end ; a++)
  59. {
  60. if (*a != ' ')
  61. return (*a < ' ') ? -swap : swap;
  62. }
  63. return 0;
  64. }
  65. return (int) (a_length-b_length);
  66. }
  67. /*
  68. Compare two keys
  69. SYNOPSIS
  70. ha_key_cmp()
  71. keyseg Array of key segments of key to compare
  72. a First key to compare, in format from _mi_pack_key()
  73. This is normally key specified by user
  74. b Second key to compare. This is always from a row
  75. key_length Length of key to compare. This can be shorter than
  76. a to just compare sub keys
  77. next_flag How keys should be compared
  78. If bit SEARCH_FIND is not set the keys includes the row
  79. position and this should also be compared
  80. diff_pos OUT Number of first keypart where values differ, counting
  81. from one.
  82. diff_pos[1] OUT (b + diff_pos[1]) points to first value in tuple b
  83. that is different from corresponding value in tuple a.
  84. EXAMPLES
  85. Example1: if the function is called for tuples
  86. ('aaa','bbb') and ('eee','fff'), then
  87. diff_pos[0] = 1 (as 'aaa' != 'eee')
  88. diff_pos[1] = 0 (offset from beggining of tuple b to 'eee' keypart).
  89. Example2: if the index function is called for tuples
  90. ('aaa','bbb') and ('aaa','fff'),
  91. diff_pos[0] = 2 (as 'aaa' != 'eee')
  92. diff_pos[1] = 3 (offset from beggining of tuple b to 'fff' keypart,
  93. here we assume that first key part is CHAR(3) NOT NULL)
  94. NOTES
  95. Number-keys can't be splited
  96. RETURN VALUES
  97. <0 If a < b
  98. 0 If a == b
  99. >0 If a > b
  100. */
  101. #define FCMP(A,B) ((int) (A) - (int) (B))
  102. int ha_key_cmp(register HA_KEYSEG *keyseg, register uchar *a,
  103. register uchar *b, uint key_length, uint nextflag,
  104. uint *diff_pos)
  105. {
  106. int flag;
  107. int16 s_1,s_2;
  108. int32 l_1,l_2;
  109. uint32 u_1,u_2;
  110. float f_1,f_2;
  111. double d_1,d_2;
  112. uint next_key_length;
  113. uchar *orig_b= b;
  114. *diff_pos=0;
  115. for ( ; (int) key_length >0 ; key_length=next_key_length, keyseg++)
  116. {
  117. uchar *end;
  118. uint piks=! (keyseg->flag & HA_NO_SORT);
  119. (*diff_pos)++;
  120. diff_pos[1]= (uint)(b - orig_b);
  121. /* Handle NULL part */
  122. if (keyseg->null_bit)
  123. {
  124. key_length--;
  125. if (*a != *b && piks)
  126. {
  127. flag = (int) *a - (int) *b;
  128. return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
  129. }
  130. b++;
  131. if (!*a++) /* If key was NULL */
  132. {
  133. if (nextflag == (SEARCH_FIND | SEARCH_UPDATE))
  134. nextflag=SEARCH_SAME; /* Allow duplicate keys */
  135. else if (nextflag & SEARCH_NULL_ARE_NOT_EQUAL)
  136. {
  137. /*
  138. This is only used from mi_check() to calculate cardinality.
  139. It can't be used when searching for a key as this would cause
  140. compare of (a,b) and (b,a) to return the same value.
  141. */
  142. return -1;
  143. }
  144. next_key_length=key_length;
  145. continue; /* To next key part */
  146. }
  147. }
  148. end= a+ min(keyseg->length,key_length);
  149. next_key_length=key_length-keyseg->length;
  150. switch ((enum ha_base_keytype) keyseg->type) {
  151. case HA_KEYTYPE_TEXT: /* Ascii; Key is converted */
  152. if (keyseg->flag & HA_SPACE_PACK)
  153. {
  154. int a_length,b_length,pack_length;
  155. get_key_length(a_length,a);
  156. get_key_pack_length(b_length,pack_length,b);
  157. next_key_length=key_length-b_length-pack_length;
  158. if (piks &&
  159. (flag=ha_compare_text(keyseg->charset,a,a_length,b,b_length,
  160. (my_bool) ((nextflag & SEARCH_PREFIX) &&
  161. next_key_length <= 0),
  162. (my_bool)!(nextflag & SEARCH_PREFIX))))
  163. return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
  164. a+=a_length;
  165. b+=b_length;
  166. break;
  167. }
  168. else
  169. {
  170. uint length=(uint) (end-a), a_length=length, b_length=length;
  171. if (piks &&
  172. (flag= ha_compare_text(keyseg->charset, a, a_length, b, b_length,
  173. (my_bool) ((nextflag & SEARCH_PREFIX) &&
  174. next_key_length <= 0),
  175. (my_bool)!(nextflag & SEARCH_PREFIX))))
  176. return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
  177. a=end;
  178. b+=length;
  179. }
  180. break;
  181. case HA_KEYTYPE_BINARY:
  182. case HA_KEYTYPE_BIT:
  183. if (keyseg->flag & HA_SPACE_PACK)
  184. {
  185. int a_length,b_length,pack_length;
  186. get_key_length(a_length,a);
  187. get_key_pack_length(b_length,pack_length,b);
  188. next_key_length=key_length-b_length-pack_length;
  189. if (piks &&
  190. (flag=compare_bin(a,a_length,b,b_length,
  191. (my_bool) ((nextflag & SEARCH_PREFIX) &&
  192. next_key_length <= 0),1)))
  193. return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
  194. a+=a_length;
  195. b+=b_length;
  196. break;
  197. }
  198. else
  199. {
  200. uint length=keyseg->length;
  201. if (piks &&
  202. (flag=compare_bin(a,length,b,length,
  203. (my_bool) ((nextflag & SEARCH_PREFIX) &&
  204. next_key_length <= 0),0)))
  205. return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
  206. a+=length;
  207. b+=length;
  208. }
  209. break;
  210. case HA_KEYTYPE_VARTEXT1:
  211. case HA_KEYTYPE_VARTEXT2:
  212. {
  213. int a_length,b_length,pack_length;
  214. get_key_length(a_length,a);
  215. get_key_pack_length(b_length,pack_length,b);
  216. next_key_length=key_length-b_length-pack_length;
  217. if (piks &&
  218. (flag= ha_compare_text(keyseg->charset,a,a_length,b,b_length,
  219. (my_bool) ((nextflag & SEARCH_PREFIX) &&
  220. next_key_length <= 0),
  221. (my_bool) ((nextflag & (SEARCH_FIND |
  222. SEARCH_UPDATE)) ==
  223. SEARCH_FIND &&
  224. ! (keyseg->flag &
  225. HA_END_SPACE_ARE_EQUAL)))))
  226. return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
  227. a+= a_length;
  228. b+= b_length;
  229. break;
  230. }
  231. break;
  232. case HA_KEYTYPE_VARBINARY1:
  233. case HA_KEYTYPE_VARBINARY2:
  234. {
  235. int a_length,b_length,pack_length;
  236. get_key_length(a_length,a);
  237. get_key_pack_length(b_length,pack_length,b);
  238. next_key_length=key_length-b_length-pack_length;
  239. if (piks &&
  240. (flag=compare_bin(a,a_length,b,b_length,
  241. (my_bool) ((nextflag & SEARCH_PREFIX) &&
  242. next_key_length <= 0), 0)))
  243. return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
  244. a+=a_length;
  245. b+=b_length;
  246. break;
  247. }
  248. break;
  249. case HA_KEYTYPE_INT8:
  250. {
  251. int i_1= (int) *((signed char*) a);
  252. int i_2= (int) *((signed char*) b);
  253. if (piks && (flag = CMP_NUM(i_1,i_2)))
  254. return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
  255. a= end;
  256. b++;
  257. break;
  258. }
  259. case HA_KEYTYPE_SHORT_INT:
  260. s_1= mi_sint2korr(a);
  261. s_2= mi_sint2korr(b);
  262. if (piks && (flag = CMP_NUM(s_1,s_2)))
  263. return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
  264. a= end;
  265. b+= 2; /* sizeof(short int); */
  266. break;
  267. case HA_KEYTYPE_USHORT_INT:
  268. {
  269. uint16 us_1,us_2;
  270. us_1= mi_sint2korr(a);
  271. us_2= mi_sint2korr(b);
  272. if (piks && (flag = CMP_NUM(us_1,us_2)))
  273. return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
  274. a= end;
  275. b+=2; /* sizeof(short int); */
  276. break;
  277. }
  278. case HA_KEYTYPE_LONG_INT:
  279. l_1= mi_sint4korr(a);
  280. l_2= mi_sint4korr(b);
  281. if (piks && (flag = CMP_NUM(l_1,l_2)))
  282. return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
  283. a= end;
  284. b+= 4; /* sizeof(long int); */
  285. break;
  286. case HA_KEYTYPE_ULONG_INT:
  287. u_1= mi_sint4korr(a);
  288. u_2= mi_sint4korr(b);
  289. if (piks && (flag = CMP_NUM(u_1,u_2)))
  290. return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
  291. a= end;
  292. b+= 4; /* sizeof(long int); */
  293. break;
  294. case HA_KEYTYPE_INT24:
  295. l_1=mi_sint3korr(a);
  296. l_2=mi_sint3korr(b);
  297. if (piks && (flag = CMP_NUM(l_1,l_2)))
  298. return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
  299. a= end;
  300. b+= 3;
  301. break;
  302. case HA_KEYTYPE_UINT24:
  303. l_1=mi_uint3korr(a);
  304. l_2=mi_uint3korr(b);
  305. if (piks && (flag = CMP_NUM(l_1,l_2)))
  306. return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
  307. a= end;
  308. b+= 3;
  309. break;
  310. case HA_KEYTYPE_FLOAT:
  311. mi_float4get(f_1,a);
  312. mi_float4get(f_2,b);
  313. /*
  314. The following may give a compiler warning about floating point
  315. comparison not being safe, but this is ok in this context as
  316. we are bascily doing sorting
  317. */
  318. if (piks && (flag = CMP_NUM(f_1,f_2)))
  319. return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
  320. a= end;
  321. b+= 4; /* sizeof(float); */
  322. break;
  323. case HA_KEYTYPE_DOUBLE:
  324. mi_float8get(d_1,a);
  325. mi_float8get(d_2,b);
  326. /*
  327. The following may give a compiler warning about floating point
  328. comparison not being safe, but this is ok in this context as
  329. we are bascily doing sorting
  330. */
  331. if (piks && (flag = CMP_NUM(d_1,d_2)))
  332. return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
  333. a= end;
  334. b+= 8; /* sizeof(double); */
  335. break;
  336. case HA_KEYTYPE_NUM: /* Numeric key */
  337. {
  338. int swap_flag= 0;
  339. int alength,blength;
  340. if (keyseg->flag & HA_REVERSE_SORT)
  341. {
  342. swap_variables(uchar*, a, b);
  343. swap_flag=1; /* Remember swap of a & b */
  344. end= a+ (int) (end-b);
  345. }
  346. if (keyseg->flag & HA_SPACE_PACK)
  347. {
  348. alength= *a++; blength= *b++;
  349. end=a+alength;
  350. next_key_length=key_length-blength-1;
  351. }
  352. else
  353. {
  354. alength= (int) (end-a);
  355. blength=keyseg->length;
  356. /* remove pre space from keys */
  357. for ( ; alength && *a == ' ' ; a++, alength--) ;
  358. for ( ; blength && *b == ' ' ; b++, blength--) ;
  359. }
  360. if (piks)
  361. {
  362. if (*a == '-')
  363. {
  364. if (*b != '-')
  365. return -1;
  366. a++; b++;
  367. swap_variables(uchar*, a, b);
  368. swap_variables(int, alength, blength);
  369. swap_flag=1-swap_flag;
  370. alength--; blength--;
  371. end=a+alength;
  372. }
  373. else if (*b == '-')
  374. return 1;
  375. while (alength && (*a == '+' || *a == '0'))
  376. {
  377. a++; alength--;
  378. }
  379. while (blength && (*b == '+' || *b == '0'))
  380. {
  381. b++; blength--;
  382. }
  383. if (alength != blength)
  384. return (alength < blength) ? -1 : 1;
  385. while (a < end)
  386. if (*a++ != *b++)
  387. return ((int) a[-1] - (int) b[-1]);
  388. }
  389. else
  390. {
  391. b+=(end-a);
  392. a=end;
  393. }
  394. if (swap_flag) /* Restore pointers */
  395. swap_variables(uchar*, a, b);
  396. break;
  397. }
  398. #ifdef HAVE_LONG_LONG
  399. case HA_KEYTYPE_LONGLONG:
  400. {
  401. longlong ll_a,ll_b;
  402. ll_a= mi_sint8korr(a);
  403. ll_b= mi_sint8korr(b);
  404. if (piks && (flag = CMP_NUM(ll_a,ll_b)))
  405. return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
  406. a= end;
  407. b+= 8;
  408. break;
  409. }
  410. case HA_KEYTYPE_ULONGLONG:
  411. {
  412. ulonglong ll_a,ll_b;
  413. ll_a= mi_uint8korr(a);
  414. ll_b= mi_uint8korr(b);
  415. if (piks && (flag = CMP_NUM(ll_a,ll_b)))
  416. return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
  417. a= end;
  418. b+= 8;
  419. break;
  420. }
  421. #endif
  422. case HA_KEYTYPE_END: /* Ready */
  423. goto end; /* diff_pos is incremented */
  424. }
  425. }
  426. (*diff_pos)++;
  427. end:
  428. if (!(nextflag & SEARCH_FIND))
  429. {
  430. uint i;
  431. if (nextflag & (SEARCH_NO_FIND | SEARCH_LAST)) /* Find record after key */
  432. return (nextflag & (SEARCH_BIGGER | SEARCH_LAST)) ? -1 : 1;
  433. flag=0;
  434. for (i=keyseg->length ; i-- > 0 ; )
  435. {
  436. if (*a++ != *b++)
  437. {
  438. flag= FCMP(a[-1],b[-1]);
  439. break;
  440. }
  441. }
  442. if (nextflag & SEARCH_SAME)
  443. return (flag); /* read same */
  444. if (nextflag & SEARCH_BIGGER)
  445. return (flag <= 0 ? -1 : 1); /* read next */
  446. return (flag < 0 ? -1 : 1); /* read previous */
  447. }
  448. return 0;
  449. } /* ha_key_cmp */
  450. /*
  451. Find the first NULL value in index-suffix values tuple
  452. SYNOPSIS
  453. ha_find_null()
  454. keyseg Array of keyparts for key suffix
  455. a Key suffix value tuple
  456. DESCRIPTION
  457. Find the first NULL value in index-suffix values tuple.
  458. TODO
  459. Consider optimizing this function or its use so we don't search for
  460. NULL values in completely NOT NULL index suffixes.
  461. RETURN
  462. First key part that has NULL as value in values tuple, or the last key
  463. part (with keyseg->type==HA_TYPE_END) if values tuple doesn't contain
  464. NULLs.
  465. */
  466. HA_KEYSEG *ha_find_null(HA_KEYSEG *keyseg, uchar *a)
  467. {
  468. for (; (enum ha_base_keytype) keyseg->type != HA_KEYTYPE_END; keyseg++)
  469. {
  470. uchar *end;
  471. if (keyseg->null_bit)
  472. {
  473. if (!*a++)
  474. return keyseg;
  475. }
  476. end= a+ keyseg->length;
  477. switch ((enum ha_base_keytype) keyseg->type) {
  478. case HA_KEYTYPE_TEXT:
  479. case HA_KEYTYPE_BINARY:
  480. case HA_KEYTYPE_BIT:
  481. if (keyseg->flag & HA_SPACE_PACK)
  482. {
  483. int a_length;
  484. get_key_length(a_length, a);
  485. a += a_length;
  486. break;
  487. }
  488. else
  489. a= end;
  490. break;
  491. case HA_KEYTYPE_VARTEXT1:
  492. case HA_KEYTYPE_VARTEXT2:
  493. case HA_KEYTYPE_VARBINARY1:
  494. case HA_KEYTYPE_VARBINARY2:
  495. {
  496. int a_length;
  497. get_key_length(a_length, a);
  498. a+= a_length;
  499. break;
  500. }
  501. case HA_KEYTYPE_NUM:
  502. if (keyseg->flag & HA_SPACE_PACK)
  503. {
  504. int alength= *a++;
  505. end= a+alength;
  506. }
  507. a= end;
  508. break;
  509. case HA_KEYTYPE_INT8:
  510. case HA_KEYTYPE_SHORT_INT:
  511. case HA_KEYTYPE_USHORT_INT:
  512. case HA_KEYTYPE_LONG_INT:
  513. case HA_KEYTYPE_ULONG_INT:
  514. case HA_KEYTYPE_INT24:
  515. case HA_KEYTYPE_UINT24:
  516. #ifdef HAVE_LONG_LONG
  517. case HA_KEYTYPE_LONGLONG:
  518. case HA_KEYTYPE_ULONGLONG:
  519. #endif
  520. case HA_KEYTYPE_FLOAT:
  521. case HA_KEYTYPE_DOUBLE:
  522. a= end;
  523. break;
  524. case HA_KEYTYPE_END: /* purecov: inspected */
  525. /* keep compiler happy */
  526. DBUG_ASSERT(0);
  527. break;
  528. }
  529. }
  530. return keyseg;
  531. }
  532. /*
  533. Register handler error messages for usage with my_error()
  534. NOTES
  535. This is safe to call multiple times as my_error_register()
  536. will ignore calls to register already registered error numbers.
  537. */
  538. void my_handler_error_register(void)
  539. {
  540. /*
  541. If you got compilation error here about compile_time_assert array, check
  542. that every HA_ERR_xxx constant has a corresponding error message in
  543. handler_error_messages[] list (check mysys/ma_handler_errors.h and
  544. include/my_base.h).
  545. */
  546. compile_time_assert(HA_ERR_FIRST + array_elements(handler_error_messages) ==
  547. HA_ERR_LAST + 1);
  548. my_error_register(handler_error_messages, HA_ERR_FIRST,
  549. HA_ERR_FIRST+ array_elements(handler_error_messages)-1);
  550. }
  551. void my_handler_error_unregister(void)
  552. {
  553. my_error_unregister(HA_ERR_FIRST,
  554. HA_ERR_FIRST+ array_elements(handler_error_messages)-1);
  555. }