| 1 | /* Copyright (c) 2000-2007 MySQL AB, 2009 Sun Microsystems, Inc. |
| 2 | Use is subject to license terms. |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License as published by |
| 6 | the Free Software Foundation; version 2 of the License. |
| 7 | |
| 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program; if not, write to the Free Software |
| 15 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */ |
| 16 | |
| 17 | /* Read record based on a key */ |
| 18 | |
| 19 | #include "myisamdef.h"cxx . . $ `<file>/storage/myisam/myisamdef.h`/ |
| 20 | #include "rt_index.h"cxx . . $ `<file>/storage/myisam/rt_index.h`/ |
| 21 | |
| 22 | /* Read a record using key */ |
| 23 | /* Ordinary search_flag is 0 ; Give error if no record with key */ |
| 24 | |
| 25 | int mi_rkeycxx . . $ mi_rkey(92a7d007bfcb778b).(MI_INFOcxx . . $ MI_INFO# *info, ucharcxx . . $ uchar# *buf, int inx, const ucharcxx . . $ uchar# *key, |
| 26 | key_part_mapcxx . . $ key_part_map# keypart_map, enum ha_rkey_functioncxx . . $ ha_rkey_function# search_flag) |
| 27 | { |
| 28 | ucharcxx . . $ uchar# *key_buff; |
| 29 | MYISAM_SHAREcxx . . $ MYISAM_SHARE# *share=info->scxx . . $ st_myisam_info#s.; |
| 30 | MI_KEYDEFcxx . . $ MI_KEYDEF# *keyinfo; |
| 31 | HA_KEYSEGcxx . . $ HA_KEYSEG# *last_used_keyseg; |
| 32 | uintcxx . . $ uint# pack_key_length, use_key_length, nextflag; |
| 33 | check_result_tcxx . . $ check_result_t# res= CHECK_NEGcxx . . $ CHECK_NEG.; |
| 34 | DBUG_ENTERcxx . . $ `include/my_dbug.h:165:9`!("mi_rkey"); |
| 35 | DBUG_PRINTcxx . . $ `include/my_dbug.h:174:9`!("enter", ("base: %p buf: %p inx: %d search_flag: %d", |
| 36 | info, buf, inx, search_flag)); |
| 37 | |
| 38 | if ((inx = _mi_check_indexcxx . . $ _mi_check_index(a395578934ddaf1c).(info,inx)) < 0) |
| 39 | DBUG_RETURNcxx . . $ `include/my_dbug.h:168:9`!(my_errnocxx . . $ `include/my_pthread.h:720:9`!); |
| 40 | |
| 41 | info->updatecxx . . $ st_myisam_info#update.&= (HA_STATE_CHANGEDcxx . . $ `include/my_base.h:617:9`! | HA_STATE_ROW_CHANGEDcxx . . $ `include/my_base.h:627:9`!); |
| 42 | info->last_key_funccxx . . $ st_myisam_info#last_key_func.= search_flag; |
| 43 | keyinfo= share->keyinfocxx . . $ st_mi_isam_share#keyinfo. + inx; |
| 44 | |
| 45 | if (info->once_flagscxx . . $ st_myisam_info#once_flags. & USE_PACKED_KEYScxx . . $ `storage/myisam/myisamdef.h:329:9`!) |
| 46 | { |
| 47 | info->once_flagscxx . . $ st_myisam_info#once_flags.&= ~USE_PACKED_KEYScxx . . $ `storage/myisam/myisamdef.h:329:9`!; /* Reset flag */ |
| 48 | /* |
| 49 | key is already packed!; This happens when we are using a MERGE TABLE |
| 50 | In this key 'key_part_map' is the length of the key ! |
| 51 | */ |
| 52 | key_buff=info->lastkeycxx . . $ st_myisam_info#lastkey.+info->scxx . . $ st_myisam_info#s.->basecxx . . $ st_mi_isam_share#base..max_key_lengthcxx . . $ st_mi_base_info#max_key_length.; |
| 53 | pack_key_length= keypart_map; |
| 54 | bmovecxx . . $ `include/m_string.h:48:10`!(key_buff, key, pack_key_length); |
| 55 | last_used_keyseg= info->scxx . . $ st_myisam_info#s.->keyinfocxx . . $ st_mi_isam_share#keyinfo.[inx].segcxx . . $ st_mi_keydef#seg. + info->last_used_keysegcxx . . $ st_myisam_info#last_used_keyseg.; |
| 56 | } |
| 57 | else |
| 58 | { |
| 59 | DBUG_ASSERTcxx . . $ `include/my_dbug.h:207:9`!(keypart_map); |
| 60 | /* Save the packed key for later use in the second buffer of lastkey. */ |
| 61 | key_buff=info->lastkeycxx . . $ st_myisam_info#lastkey.+info->scxx . . $ st_myisam_info#s.->basecxx . . $ st_mi_isam_share#base..max_key_lengthcxx . . $ st_mi_base_info#max_key_length.; |
| 62 | pack_key_length=_mi_pack_keycxx . . $ _mi_pack_key(42a9307647547506).(info,(uintcxx . . $ uint#) inx, key_buff, (ucharcxx . . $ uchar#*) key, |
| 63 | keypart_map, &last_used_keyseg); |
| 64 | /* Save packed_key_length for use by the MERGE engine. */ |
| 65 | info->pack_key_lengthcxx . . $ st_myisam_info#pack_key_length.= pack_key_length; |
| 66 | info->last_used_keysegcxx . . $ st_myisam_info#last_used_keyseg.= (uint16cxx . . $ uint16#) (last_used_keyseg - |
| 67 | info->scxx . . $ st_myisam_info#s.->keyinfocxx . . $ st_mi_isam_share#keyinfo.[inx].segcxx . . $ st_mi_keydef#seg.); |
| 68 | DBUG_EXECUTEcxx . . $ `include/my_dbug.h:171:9`!("key",_mi_print_key(DBUG_FILE, keyinfo->seg, |
| 69 | key_buff, pack_key_length);); |
| 70 | } |
| 71 | |
| 72 | if (fast_mi_readinfocxx . . $ `storage/myisam/myisamdef.h:673:9`!(info)) |
| 73 | goto err; |
| 74 | |
| 75 | if (share->concurrent_insertcxx . . $ st_mi_isam_share#concurrent_insert.) |
| 76 | mysql_rwlock_rdlockcxx . . $ `include/mysql/psi/mysql_thread.h:452:11`!(&share->key_root_lock[inx]); |
| 77 | |
| 78 | nextflag=myisam_read_veccxx . . $ myisam_read_vec.[search_flag]; |
| 79 | use_key_length=pack_key_length; |
| 80 | if (!(nextflag & (SEARCH_FINDcxx . . $ `include/my_base.h:582:9`! | SEARCH_NO_FINDcxx . . $ `include/my_base.h:583:9`! | SEARCH_LASTcxx . . $ `include/my_base.h:590:9`!))) |
| 81 | use_key_length=USE_WHOLE_KEYcxx . . $ `storage/myisam/myisamdef.h:319:9`!; |
| 82 | |
| 83 | switch (info->scxx . . $ st_myisam_info#s.->keyinfocxx . . $ st_mi_isam_share#keyinfo.[inx].key_algcxx . . $ st_mi_keydef#key_alg.) { |
| 84 | case HA_KEY_ALG_RTREEcxx . . $ HA_KEY_ALG_RTREE.: |
| 85 | if (rtree_find_firstcxx . . $ rtree_find_first(8ba19a865fc28679).(info,inx,key_buff,use_key_length,nextflag) < 0) |
| 86 | { |
| 87 | mi_print_errorcxx . . $ `storage/myisam/myisamdef.h:361:9`!(info->s, HA_ERR_CRASHEDcxx . . $ `include/my_base.h:471:9`!); |
| 88 | my_errnocxx . . $ `include/my_pthread.h:720:9`!=HA_ERR_CRASHEDcxx . . $ `include/my_base.h:471:9`!; |
| 89 | if (share->concurrent_insertcxx . . $ st_mi_isam_share#concurrent_insert.) |
| 90 | mysql_rwlock_unlockcxx . . $ `include/mysql/psi/mysql_thread.h:535:9`!(&share->key_root_lock[inx]); |
| 91 | fast_mi_writeinfocxx . . $ `storage/myisam/myisamdef.h:672:9`!(info); |
| 92 | goto err; |
| 93 | } |
| 94 | break; |
| 95 | case HA_KEY_ALG_BTREEcxx . . $ HA_KEY_ALG_BTREE.: |
| 96 | default: |
| 97 | if (!_mi_searchcxx . . $ _mi_search(11619ded83ef7296).(info, keyinfo, key_buff, use_key_length, |
| 98 | myisam_read_veccxx . . $ myisam_read_vec.[search_flag], info->scxx . . $ st_myisam_info#s.->statecxx . . $ st_mi_isam_share#state..key_rootcxx . . $ st_mi_state_info#key_root.[inx])) |
| 99 | { |
| 100 | /* |
| 101 | Found a key, but it might not be usable. We cannot use rows that |
| 102 | are inserted by other threads after we got our table lock |
| 103 | ("concurrent inserts"). The record may not even be present yet. |
| 104 | Keys are inserted into the index(es) before the record is |
| 105 | inserted into the data file. When we got our table lock, we |
| 106 | saved the current data_file_length. Concurrent inserts always go |
| 107 | to the end of the file. So we can test if the found key |
| 108 | references a new record. |
| 109 | |
| 110 | If we are searching for a partial key (or using >, >=, < or <=) and |
| 111 | the data is outside of the data file, we need to continue searching |
| 112 | for the first key inside the data file. |
| 113 | |
| 114 | We do also continue searching if an index condition check function |
| 115 | is available. |
| 116 | */ |
| 117 | while ((info->lastposcxx . . $ st_myisam_info#lastpos. >= info->statecxx . . $ st_myisam_info#state.->data_file_lengthcxx . . $ st_mi_status_info#data_file_length. && |
| 118 | (search_flag != HA_READ_KEY_EXACTcxx . . $ HA_READ_KEY_EXACT. || |
| 119 | last_used_keyseg != keyinfo->segcxx . . $ st_mi_keydef#seg. + keyinfo->keysegscxx . . $ st_mi_keydef#keysegs.)) || |
| 120 | (res= mi_check_index_tuplecxx . . $ mi_check_index_tuple(8d6ff5a0859961cd).(info, inx, buf)) == CHECK_NEGcxx . . $ CHECK_NEG.) |
| 121 | { |
| 122 | uintcxx . . $ uint# not_used[2]; |
| 123 | /* |
| 124 | Skip rows that are inserted by other threads since we got a lock |
| 125 | Note that this can only happen if we are not searching after an |
| 126 | full length exact key, because the keys are sorted |
| 127 | according to position |
| 128 | */ |
| 129 | if (_mi_search_nextcxx . . $ _mi_search_next(11619ded83ef7296).(info, keyinfo, info->lastkeycxx . . $ st_myisam_info#lastkey., |
| 130 | info->lastkey_lengthcxx . . $ st_myisam_info#lastkey_length., |
| 131 | myisam_readnext_veccxx . . $ myisam_readnext_vec.[search_flag], |
| 132 | info->scxx . . $ st_myisam_info#s.->statecxx . . $ st_mi_isam_share#state..key_rootcxx . . $ st_mi_state_info#key_root.[inx])) |
| 133 | { |
| 134 | info->lastposcxx . . $ st_myisam_info#lastpos.= HA_OFFSET_ERRORcxx . . $ `include/my_base.h:695:9`!; |
| 135 | break; |
| 136 | } |
| 137 | /* |
| 138 | Check that the found key does still match the search. |
| 139 | _mi_search_next() delivers the next key regardless of its |
| 140 | value. |
| 141 | */ |
| 142 | if (search_flag == HA_READ_KEY_EXACTcxx . . $ HA_READ_KEY_EXACT. && |
| 143 | ha_key_cmpcxx . . $ ha_key_cmp(691a6ef61d426540).(keyinfo->segcxx . . $ st_mi_keydef#seg., key_buff, info->lastkeycxx . . $ st_myisam_info#lastkey., use_key_length, |
| 144 | SEARCH_FINDcxx . . $ `include/my_base.h:582:9`!, not_used)) |
| 145 | { |
| 146 | my_errnocxx . . $ `include/my_pthread.h:720:9`!= HA_ERR_KEY_NOT_FOUNDcxx . . $ `include/my_base.h:466:9`!; |
| 147 | info->lastposcxx . . $ st_myisam_info#lastpos.= HA_OFFSET_ERRORcxx . . $ `include/my_base.h:695:9`!; |
| 148 | break; |
| 149 | } |
| 150 | /* |
| 151 | If we are at the last key on the key page, allow writers to |
| 152 | access the index. |
| 153 | */ |
| 154 | if (info->int_keyposcxx . . $ st_myisam_info#int_keypos. >= info->int_maxposcxx . . $ st_myisam_info#int_maxpos. && |
| 155 | mi_yield_and_check_if_killedcxx . . $ mi_yield_and_check_if_killed(34a1a3a12ffdfe3).(info, inx)) |
| 156 | { |
| 157 | /* Aborted by user */ |
| 158 | DBUG_ASSERTcxx . . $ `include/my_dbug.h:207:9`!(info->lastpos == HA_OFFSET_ERROR && |
| 159 | my_errno == HA_ERR_ABORTED_BY_USER); |
| 160 | res= CHECK_ERRORcxx . . $ CHECK_ERROR.; |
| 161 | buf= 0; /* Fast abort */ |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | if (res == CHECK_OUT_OF_RANGEcxx . . $ CHECK_OUT_OF_RANGE.) |
| 166 | { |
| 167 | /* Change error from HA_ERR_END_OF_FILE */ |
| 168 | DBUG_ASSERTcxx . . $ `include/my_dbug.h:207:9`!(info->lastpos == HA_OFFSET_ERROR); |
| 169 | my_errnocxx . . $ `include/my_pthread.h:720:9`!= HA_ERR_KEY_NOT_FOUNDcxx . . $ `include/my_base.h:466:9`!; |
| 170 | } |
| 171 | /* |
| 172 | Error if no row found within the data file. (Bug #29838) |
| 173 | Do not overwrite my_errno if already at HA_OFFSET_ERROR. |
| 174 | */ |
| 175 | if (info->lastposcxx . . $ st_myisam_info#lastpos. != HA_OFFSET_ERRORcxx . . $ `include/my_base.h:695:9`! && |
| 176 | info->lastposcxx . . $ st_myisam_info#lastpos. >= info->statecxx . . $ st_myisam_info#state.->data_file_lengthcxx . . $ st_mi_status_info#data_file_length.) |
| 177 | { |
| 178 | info->lastposcxx . . $ st_myisam_info#lastpos.= HA_OFFSET_ERRORcxx . . $ `include/my_base.h:695:9`!; |
| 179 | my_errnocxx . . $ `include/my_pthread.h:720:9`!= HA_ERR_KEY_NOT_FOUNDcxx . . $ `include/my_base.h:466:9`!; |
| 180 | } |
| 181 | } |
| 182 | else |
| 183 | { |
| 184 | DBUG_ASSERTcxx . . $ `include/my_dbug.h:207:9`!(info->lastpos == HA_OFFSET_ERROR); |
| 185 | } |
| 186 | } |
| 187 | if (share->concurrent_insertcxx . . $ st_mi_isam_share#concurrent_insert.) |
| 188 | mysql_rwlock_unlockcxx . . $ `include/mysql/psi/mysql_thread.h:535:9`!(&share->key_root_lock[inx]); |
| 189 | |
| 190 | info->last_rkey_lengthcxx . . $ st_myisam_info#last_rkey_length.= pack_key_length; |
| 191 | |
| 192 | if (info->lastposcxx . . $ st_myisam_info#lastpos. == HA_OFFSET_ERRORcxx . . $ `include/my_base.h:695:9`!) /* No such record */ |
| 193 | { |
| 194 | fast_mi_writeinfocxx . . $ `storage/myisam/myisamdef.h:672:9`!(info); |
| 195 | if (!buf) |
| 196 | DBUG_RETURNcxx . . $ `include/my_dbug.h:168:9`!(my_errnocxx . . $ `include/my_pthread.h:720:9`!); |
| 197 | } |
| 198 | else |
| 199 | { |
| 200 | /* Calculate length of the found key; Used by mi_rnext_same */ |
| 201 | if ((keyinfo->flagcxx . . $ st_mi_keydef#flag. & HA_VAR_LENGTH_KEYcxx . . $ `include/my_base.h:288:9`!) && last_used_keyseg) |
| 202 | info->last_rkey_lengthcxx . . $ st_myisam_info#last_rkey_length.= _mi_keylength_partcxx . . $ _mi_keylength_part(5444fd1400bddc61).(keyinfo, info->lastkeycxx . . $ st_myisam_info#lastkey., |
| 203 | last_used_keyseg); |
| 204 | |
| 205 | /* Check if we don't want to have record back, only error message */ |
| 206 | if (!buf) |
| 207 | { |
| 208 | fast_mi_writeinfocxx . . $ `storage/myisam/myisamdef.h:672:9`!(info); |
| 209 | DBUG_RETURNcxx . . $ `include/my_dbug.h:168:9`!(0); |
| 210 | } |
| 211 | if (!(*info->read_recordcxx . . $ st_myisam_info#read_record.)(info,info->lastposcxx . . $ st_myisam_info#lastpos.,buf)) |
| 212 | { |
| 213 | info->updatecxx . . $ st_myisam_info#update.|= HA_STATE_AKTIVcxx . . $ `include/my_base.h:618:9`!; /* Record is read */ |
| 214 | DBUG_RETURNcxx . . $ `include/my_dbug.h:168:9`!(0); |
| 215 | } |
| 216 | DBUG_PRINTcxx . . $ `include/my_dbug.h:174:9`!("error", ("Didn't find row. Error %d", my_errno)); |
| 217 | info->lastposcxx . . $ st_myisam_info#lastpos.= HA_OFFSET_ERRORcxx . . $ `include/my_base.h:695:9`!; /* Didn't find row */ |
| 218 | } |
| 219 | |
| 220 | /* Store last used key as a base for read next */ |
| 221 | memcpycxx . . $ memcpy(cc817cb175f282d).(info->lastkeycxx . . $ st_myisam_info#lastkey.,key_buff,pack_key_length); |
| 222 | info->last_rkey_lengthcxx . . $ st_myisam_info#last_rkey_length.= pack_key_length; |
| 223 | bzerocxx . . $ `include/m_string.h:61:10`!((char*) info->lastkey+pack_key_length,info->s->base.rec_reflength); |
| 224 | info->lastkey_lengthcxx . . $ st_myisam_info#lastkey_length.=pack_key_length+info->scxx . . $ st_myisam_info#s.->basecxx . . $ st_mi_isam_share#base..rec_reflengthcxx . . $ st_mi_base_info#rec_reflength.; |
| 225 | |
| 226 | if (search_flag == HA_READ_AFTER_KEYcxx . . $ HA_READ_AFTER_KEY.) |
| 227 | info->updatecxx . . $ st_myisam_info#update.|=HA_STATE_NEXT_FOUNDcxx . . $ `include/my_base.h:621:9`!; /* Previous gives last row */ |
| 228 | err: |
| 229 | DBUG_RETURNcxx . . $ `include/my_dbug.h:168:9`!(my_errnocxx . . $ `include/my_pthread.h:720:9`!); |
| 230 | } /* _mi_rkey */ |
| 231 | |
| 232 | |
| 233 | /* |
| 234 | Yield to possible other writers during a index scan. |
| 235 | Check also if we got killed by the user and if yes, return |
| 236 | HA_ERR_LOCK_WAIT_TIMEOUT |
| 237 | |
| 238 | return 0 ok |
| 239 | return 1 Query has been requested to be killed |
| 240 | */ |
| 241 | |
| 242 | my_boolcxx . . $ my_bool# mi_yield_and_check_if_killedcxx . . $ mi_yield_and_check_if_killed(34a1a3a12ffdfe3).(MI_INFOcxx . . $ MI_INFO# *info, int inx) |
| 243 | { |
| 244 | MYISAM_SHAREcxx . . $ MYISAM_SHARE# *share; |
| 245 | if (mi_killedcxx . . $ mi_killed.(info)) |
| 246 | { |
| 247 | /* purecov: begin tested */ |
| 248 | info->lastposcxx . . $ st_myisam_info#lastpos.= HA_OFFSET_ERRORcxx . . $ `include/my_base.h:695:9`!; |
| 249 | /* Set error that we where aborted by kill from application */ |
| 250 | my_errnocxx . . $ `include/my_pthread.h:720:9`!= HA_ERR_ABORTED_BY_USERcxx . . $ `include/my_base.h:546:9`!; |
| 251 | return 1; |
| 252 | /* purecov: end */ |
| 253 | |
| 254 | } |
| 255 | |
| 256 | if ((share= info->scxx . . $ st_myisam_info#s.)->concurrent_insertcxx . . $ st_mi_isam_share#concurrent_insert.) |
| 257 | { |
| 258 | /* Give writers a chance to access index */ |
| 259 | mysql_rwlock_unlockcxx . . $ `include/mysql/psi/mysql_thread.h:535:9`!(&share->key_root_lock[inx]); |
| 260 | mysql_rwlock_rdlockcxx . . $ `include/mysql/psi/mysql_thread.h:452:11`!(&share->key_root_lock[inx]); |
| 261 | } |
| 262 | return 0; |
| 263 | } |