1 /* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is also distributed with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23 #include "myisamdef.h"
24
25 #include "rt_index.h"
26
27 /*
28 Read next row with the same key as previous read
29 One may have done a write, update or delete of the previous row.
30 NOTE! Even if one changes the previous row, the next read is done
31 based on the position of the last used key!
32 */
33
mi_rnext(MI_INFO * info,uchar * buf,int inx)34 int mi_rnext(MI_INFO *info, uchar *buf, int inx)
35 {
36 int error,changed;
37 uint flag;
38 int res= 0;
39 uint update_mask= HA_STATE_NEXT_FOUND;
40 DBUG_ENTER("mi_rnext");
41
42 if ((inx = _mi_check_index(info,inx)) < 0)
43 DBUG_RETURN(my_errno);
44 flag=SEARCH_BIGGER; /* Read next */
45 if (info->lastpos == HA_OFFSET_ERROR && info->update & HA_STATE_PREV_FOUND)
46 flag=0; /* Read first */
47
48 if (fast_mi_readinfo(info))
49 DBUG_RETURN(my_errno);
50 if (info->s->concurrent_insert)
51 mysql_rwlock_rdlock(&info->s->key_root_lock[inx]);
52 changed=_mi_test_if_changed(info);
53 if (!flag)
54 {
55 switch(info->s->keyinfo[inx].key_alg){
56 #ifdef HAVE_RTREE_KEYS
57 case HA_KEY_ALG_RTREE:
58 error=rtree_get_first(info,inx,info->lastkey_length);
59 break;
60 #endif
61 case HA_KEY_ALG_BTREE:
62 default:
63 error=_mi_search_first(info,info->s->keyinfo+inx,
64 info->s->state.key_root[inx]);
65 break;
66 }
67 /*
68 "search first" failed. This means we have no pivot for
69 "search next", or in other words MI_INFO::lastkey is
70 likely uninitialized.
71
72 Normally SQL layer would never request "search next" if
73 "search first" failed. But HANDLER may do anything.
74
75 As mi_rnext() without preceeding mi_rkey()/mi_rfirst()
76 equals to mi_rfirst(), we must restore original state
77 as if failing mi_rfirst() was not called.
78 */
79 if (error)
80 update_mask|= HA_STATE_PREV_FOUND;
81 }
82 else
83 {
84 switch (info->s->keyinfo[inx].key_alg) {
85 #ifdef HAVE_RTREE_KEYS
86 case HA_KEY_ALG_RTREE:
87 /*
88 Note that rtree doesn't support that the table
89 may be changed since last call, so we do need
90 to skip rows inserted by other threads like in btree
91 */
92 error= rtree_get_next(info,inx,info->lastkey_length);
93 break;
94 #endif
95 case HA_KEY_ALG_BTREE:
96 default:
97 if (!changed)
98 error= _mi_search_next(info,info->s->keyinfo+inx,info->lastkey,
99 info->lastkey_length,flag,
100 info->s->state.key_root[inx]);
101 else
102 error= _mi_search(info,info->s->keyinfo+inx,info->lastkey,
103 USE_WHOLE_KEY,flag, info->s->state.key_root[inx]);
104 }
105 }
106
107 if (!error)
108 {
109 while ((info->s->concurrent_insert &&
110 info->lastpos >= info->state->data_file_length) ||
111 (info->index_cond_func &&
112 !(res= mi_check_index_cond(info, inx, buf))))
113 {
114 /*
115 Skip rows that are either inserted by other threads since
116 we got a lock or do not match pushed index conditions
117 */
118 if ((error=_mi_search_next(info,info->s->keyinfo+inx,
119 info->lastkey,
120 info->lastkey_length,
121 SEARCH_BIGGER,
122 info->s->state.key_root[inx])))
123 break;
124 }
125 if (!error && res == 2)
126 {
127 if (info->s->concurrent_insert)
128 mysql_rwlock_unlock(&info->s->key_root_lock[inx]);
129 info->lastpos= HA_OFFSET_ERROR;
130 DBUG_RETURN(my_errno= HA_ERR_END_OF_FILE);
131 }
132 }
133
134 if (info->s->concurrent_insert)
135 {
136 if (!error)
137 {
138 while (info->lastpos >= info->state->data_file_length)
139 {
140 /* Skip rows inserted by other threads since we got a lock */
141 if ((error=_mi_search_next(info,info->s->keyinfo+inx,
142 info->lastkey,
143 info->lastkey_length,
144 SEARCH_BIGGER,
145 info->s->state.key_root[inx])))
146 break;
147 }
148 }
149 mysql_rwlock_unlock(&info->s->key_root_lock[inx]);
150 }
151 /* Don't clear if database-changed */
152 info->update&= (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED);
153 info->update|= update_mask;
154
155 if (error)
156 {
157 if (my_errno == HA_ERR_KEY_NOT_FOUND)
158 my_errno=HA_ERR_END_OF_FILE;
159 }
160 else if (!buf)
161 {
162 DBUG_RETURN(info->lastpos==HA_OFFSET_ERROR ? my_errno : 0);
163 }
164 else if (!(*info->read_record)(info,info->lastpos,buf))
165 {
166 info->update|= HA_STATE_AKTIV; /* Record is read */
167 DBUG_RETURN(0);
168 }
169 DBUG_PRINT("error",("Got error: %d, errno: %d",error, my_errno));
170 DBUG_RETURN(my_errno);
171 } /* mi_rnext */
172