1 /* Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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 case HA_KEY_ALG_RTREE:
57 error=rtree_get_first(info,inx,info->lastkey_length);
58 break;
59 case HA_KEY_ALG_BTREE:
60 default:
61 error=_mi_search_first(info,info->s->keyinfo+inx,
62 info->s->state.key_root[inx]);
63 break;
64 }
65 /*
66 "search first" failed. This means we have no pivot for
67 "search next", or in other words MI_INFO::lastkey is
68 likely uninitialized.
69
70 Normally SQL layer would never request "search next" if
71 "search first" failed. But HANDLER may do anything.
72
73 As mi_rnext() without preceding mi_rkey()/mi_rfirst()
74 equals to mi_rfirst(), we must restore original state
75 as if failing mi_rfirst() was not called.
76 */
77 if (error)
78 update_mask|= HA_STATE_PREV_FOUND;
79 }
80 else
81 {
82 switch (info->s->keyinfo[inx].key_alg) {
83 case HA_KEY_ALG_RTREE:
84 /*
85 Note that rtree doesn't support that the table
86 may be changed since last call, so we do need
87 to skip rows inserted by other threads like in btree
88 */
89 error= rtree_get_next(info,inx,info->lastkey_length);
90 break;
91 case HA_KEY_ALG_BTREE:
92 default:
93 if (!changed)
94 error= _mi_search_next(info,info->s->keyinfo+inx,info->lastkey,
95 info->lastkey_length,flag,
96 info->s->state.key_root[inx]);
97 else
98 error= _mi_search(info,info->s->keyinfo+inx,info->lastkey,
99 USE_WHOLE_KEY,flag, info->s->state.key_root[inx]);
100 }
101 }
102
103 if (!error)
104 {
105 while ((info->s->concurrent_insert &&
106 info->lastpos >= info->state->data_file_length) ||
107 (info->index_cond_func &&
108 !(res= mi_check_index_cond(info, inx, buf))))
109 {
110 /*
111 Skip rows that are either inserted by other threads since
112 we got a lock or do not match pushed index conditions
113 */
114 if ((error=_mi_search_next(info,info->s->keyinfo+inx,
115 info->lastkey,
116 info->lastkey_length,
117 SEARCH_BIGGER,
118 info->s->state.key_root[inx])))
119 break;
120 }
121 if (!error && res == 2)
122 {
123 if (info->s->concurrent_insert)
124 mysql_rwlock_unlock(&info->s->key_root_lock[inx]);
125 info->lastpos= HA_OFFSET_ERROR;
126 set_my_errno(HA_ERR_END_OF_FILE);
127 DBUG_RETURN(HA_ERR_END_OF_FILE);
128 }
129 }
130
131 if (info->s->concurrent_insert)
132 {
133 if (!error)
134 {
135 while (info->lastpos >= info->state->data_file_length)
136 {
137 /* Skip rows inserted by other threads since we got a lock */
138 if ((error=_mi_search_next(info,info->s->keyinfo+inx,
139 info->lastkey,
140 info->lastkey_length,
141 SEARCH_BIGGER,
142 info->s->state.key_root[inx])))
143 break;
144 }
145 }
146 mysql_rwlock_unlock(&info->s->key_root_lock[inx]);
147 }
148 /* Don't clear if database-changed */
149 info->update&= (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED);
150 info->update|= update_mask;
151
152 if (error)
153 {
154 if (my_errno() == HA_ERR_KEY_NOT_FOUND)
155 set_my_errno(HA_ERR_END_OF_FILE);
156 }
157 else if (!buf)
158 {
159 DBUG_RETURN(info->lastpos==HA_OFFSET_ERROR ? my_errno() : 0);
160 }
161 else if (!(*info->read_record)(info,info->lastpos,buf))
162 {
163 info->update|= HA_STATE_AKTIV; /* Record is read */
164 DBUG_RETURN(0);
165 }
166 DBUG_PRINT("error",("Got error: %d, errno: %d",error, my_errno()));
167 DBUG_RETURN(my_errno());
168 } /* mi_rnext */
169