1 | /* |
2 | Copyright (c) 2000, 2010, Oracle and/or its affiliates |
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 | #include "rt_index.h" |
19 | |
20 | /* |
21 | Read next row with the same key as previous read |
22 | One may have done a write, update or delete of the previous row. |
23 | NOTE! Even if one changes the previous row, the next read is done |
24 | based on the position of the last used key! |
25 | */ |
26 | |
27 | int mi_rnext(MI_INFO *info, uchar *buf, int inx) |
28 | { |
29 | int error,changed; |
30 | uint flag; |
31 | ICP_RESULT icp_res= ICP_MATCH; |
32 | uint update_mask= HA_STATE_NEXT_FOUND; |
33 | DBUG_ENTER("mi_rnext" ); |
34 | |
35 | if ((inx = _mi_check_index(info,inx)) < 0) |
36 | DBUG_RETURN(my_errno); |
37 | flag=SEARCH_BIGGER; /* Read next */ |
38 | if (info->lastpos == HA_OFFSET_ERROR && info->update & HA_STATE_PREV_FOUND) |
39 | flag=0; /* Read first */ |
40 | |
41 | if (fast_mi_readinfo(info)) |
42 | DBUG_RETURN(my_errno); |
43 | if (info->s->concurrent_insert) |
44 | mysql_rwlock_rdlock(&info->s->key_root_lock[inx]); |
45 | changed=_mi_test_if_changed(info); |
46 | if (!flag) |
47 | { |
48 | switch(info->s->keyinfo[inx].key_alg){ |
49 | #ifdef HAVE_RTREE_KEYS |
50 | case HA_KEY_ALG_RTREE: |
51 | error=rtree_get_first(info,inx,info->lastkey_length); |
52 | break; |
53 | #endif |
54 | case HA_KEY_ALG_BTREE: |
55 | default: |
56 | error=_mi_search_first(info,info->s->keyinfo+inx, |
57 | info->s->state.key_root[inx]); |
58 | break; |
59 | } |
60 | /* |
61 | "search first" failed. This means we have no pivot for |
62 | "search next", or in other words MI_INFO::lastkey is |
63 | likely uninitialized. |
64 | |
65 | Normally SQL layer would never request "search next" if |
66 | "search first" failed. But HANDLER may do anything. |
67 | |
68 | As mi_rnext() without preceding mi_rkey()/mi_rfirst() |
69 | equals to mi_rfirst(), we must restore original state |
70 | as if failing mi_rfirst() was not called. |
71 | */ |
72 | if (error) |
73 | update_mask|= HA_STATE_PREV_FOUND; |
74 | } |
75 | else |
76 | { |
77 | switch (info->s->keyinfo[inx].key_alg) { |
78 | #ifdef HAVE_RTREE_KEYS |
79 | case HA_KEY_ALG_RTREE: |
80 | /* |
81 | Note that rtree doesn't support that the table |
82 | may be changed since last call, so we do need |
83 | to skip rows inserted by other threads like in btree |
84 | */ |
85 | error= rtree_get_next(info,inx,info->lastkey_length); |
86 | break; |
87 | #endif |
88 | case HA_KEY_ALG_BTREE: |
89 | default: |
90 | if (!changed) |
91 | error= _mi_search_next(info,info->s->keyinfo+inx,info->lastkey, |
92 | info->lastkey_length,flag, |
93 | info->s->state.key_root[inx]); |
94 | else |
95 | error= _mi_search(info,info->s->keyinfo+inx,info->lastkey, |
96 | USE_WHOLE_KEY,flag, info->s->state.key_root[inx]); |
97 | } |
98 | } |
99 | |
100 | if (!error) |
101 | { |
102 | while ((info->s->concurrent_insert && |
103 | info->lastpos >= info->state->data_file_length) || |
104 | (info->index_cond_func && |
105 | (icp_res= mi_check_index_cond(info, inx, buf)) == ICP_NO_MATCH)) |
106 | { |
107 | /* |
108 | If we are at the last key on the key page, allow writers to |
109 | access the index. |
110 | */ |
111 | if (info->int_keypos >= info->int_maxpos && |
112 | mi_yield_and_check_if_killed(info, inx)) |
113 | { |
114 | error= 1; |
115 | break; |
116 | } |
117 | |
118 | /* |
119 | Skip rows that are either inserted by other threads since |
120 | we got a lock or do not match pushed index conditions |
121 | */ |
122 | if ((error=_mi_search_next(info,info->s->keyinfo+inx, |
123 | info->lastkey, |
124 | info->lastkey_length, |
125 | SEARCH_BIGGER, |
126 | info->s->state.key_root[inx]))) |
127 | break; |
128 | } |
129 | } |
130 | |
131 | if (info->s->concurrent_insert) |
132 | mysql_rwlock_unlock(&info->s->key_root_lock[inx]); |
133 | |
134 | /* Don't clear if database-changed */ |
135 | info->update&= (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED); |
136 | info->update|= update_mask; |
137 | |
138 | if (error || icp_res != ICP_MATCH) |
139 | { |
140 | fast_mi_writeinfo(info); |
141 | if (my_errno == HA_ERR_KEY_NOT_FOUND) |
142 | my_errno=HA_ERR_END_OF_FILE; |
143 | } |
144 | else if (!buf) |
145 | { |
146 | fast_mi_writeinfo(info); |
147 | DBUG_RETURN(info->lastpos==HA_OFFSET_ERROR ? my_errno : 0); |
148 | } |
149 | else if (!(*info->read_record)(info,info->lastpos,buf)) |
150 | { |
151 | info->update|= HA_STATE_AKTIV; /* Record is read */ |
152 | DBUG_RETURN(0); |
153 | } |
154 | DBUG_PRINT("error" ,("Got error: %d, errno: %d" ,error, my_errno)); |
155 | DBUG_RETURN(my_errno); |
156 | } /* mi_rnext */ |
157 | |