1 | /* Copyright (c) 2000, 2001, 2004-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-1301 USA */ |
16 | |
17 | #include "myisamdef.h" |
18 | |
19 | /* |
20 | Read previous row with the same key as previous read |
21 | One may have done a write, update or delete of the previous row. |
22 | NOTE! Even if one changes the previous row, the next read is done |
23 | based on the position of the last used key! |
24 | */ |
25 | |
26 | int mi_rprev(MI_INFO *info, uchar *buf, int inx) |
27 | { |
28 | int error,changed; |
29 | register uint flag; |
30 | MYISAM_SHARE *share=info->s; |
31 | ICP_RESULT icp_res= ICP_MATCH; |
32 | DBUG_ENTER("mi_rprev" ); |
33 | |
34 | if ((inx = _mi_check_index(info,inx)) < 0) |
35 | DBUG_RETURN(my_errno); |
36 | flag=SEARCH_SMALLER; /* Read previous */ |
37 | if (info->lastpos == HA_OFFSET_ERROR && info->update & HA_STATE_NEXT_FOUND) |
38 | flag=0; /* Read last */ |
39 | |
40 | if (fast_mi_readinfo(info)) |
41 | DBUG_RETURN(my_errno); |
42 | changed=_mi_test_if_changed(info); |
43 | if (share->concurrent_insert) |
44 | mysql_rwlock_rdlock(&share->key_root_lock[inx]); |
45 | if (!flag) |
46 | error=_mi_search_last(info, share->keyinfo+inx, |
47 | share->state.key_root[inx]); |
48 | else if (!changed) |
49 | error=_mi_search_next(info,share->keyinfo+inx,info->lastkey, |
50 | info->lastkey_length,flag, |
51 | share->state.key_root[inx]); |
52 | else |
53 | error=_mi_search(info,share->keyinfo+inx,info->lastkey, |
54 | USE_WHOLE_KEY, flag, share->state.key_root[inx]); |
55 | |
56 | if (!error) |
57 | { |
58 | my_off_t cur_keypage= info->last_keypage; |
59 | while ((share->concurrent_insert && |
60 | info->lastpos >= info->state->data_file_length) || |
61 | (info->index_cond_func && |
62 | (icp_res= mi_check_index_cond(info, inx, buf)) == ICP_NO_MATCH)) |
63 | { |
64 | /* |
65 | If we are at the last (i.e. first?) key on the key page, |
66 | allow writers to access the index. |
67 | */ |
68 | if (info->last_keypage != cur_keypage) |
69 | { |
70 | cur_keypage= info->last_keypage; |
71 | if (mi_yield_and_check_if_killed(info, inx)) |
72 | { |
73 | error= 1; |
74 | break; |
75 | } |
76 | } |
77 | |
78 | /* |
79 | Skip rows that are either inserted by other threads since |
80 | we got a lock or do not match pushed index conditions |
81 | */ |
82 | if ((error=_mi_search_next(info,share->keyinfo+inx,info->lastkey, |
83 | info->lastkey_length, |
84 | SEARCH_SMALLER, |
85 | share->state.key_root[inx]))) |
86 | break; |
87 | } |
88 | } |
89 | |
90 | if (share->concurrent_insert) |
91 | mysql_rwlock_unlock(&share->key_root_lock[inx]); |
92 | |
93 | info->update&= (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED); |
94 | info->update|= HA_STATE_PREV_FOUND; |
95 | |
96 | if (error || icp_res != ICP_MATCH) |
97 | { |
98 | fast_mi_writeinfo(info); |
99 | if (my_errno == HA_ERR_KEY_NOT_FOUND) |
100 | my_errno=HA_ERR_END_OF_FILE; |
101 | } |
102 | else if (!buf) |
103 | { |
104 | fast_mi_writeinfo(info); |
105 | DBUG_RETURN(info->lastpos==HA_OFFSET_ERROR ? my_errno : 0); |
106 | } |
107 | else if (!(*info->read_record)(info,info->lastpos,buf)) |
108 | { |
109 | info->update|= HA_STATE_AKTIV; /* Record is read */ |
110 | DBUG_RETURN(0); |
111 | } |
112 | DBUG_RETURN(my_errno); |
113 | } /* mi_rprev */ |
114 | |