1/* Copyright (C) 2006 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
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 as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA */
15
16#include "maria_def.h"
17
18#include "ma_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
27int maria_rnext(MARIA_HA *info, uchar *buf, int inx)
28{
29 int error,changed;
30 uint flag;
31 MARIA_SHARE *share= info->s;
32 MARIA_KEYDEF *keyinfo;
33 ICP_RESULT icp_res= ICP_MATCH;
34 uint update_mask= HA_STATE_NEXT_FOUND;
35 DBUG_ENTER("maria_rnext");
36
37 if ((inx = _ma_check_index(info,inx)) < 0)
38 DBUG_RETURN(my_errno);
39 flag=SEARCH_BIGGER; /* Read next */
40 if (info->cur_row.lastpos == HA_OFFSET_ERROR &&
41 info->update & HA_STATE_PREV_FOUND)
42 flag=0; /* Read first */
43
44 if (fast_ma_readinfo(info))
45 DBUG_RETURN(my_errno);
46 keyinfo= share->keyinfo + inx;
47 if (share->lock_key_trees)
48 mysql_rwlock_rdlock(&keyinfo->root_lock);
49 changed= _ma_test_if_changed(info);
50 if (!flag)
51 {
52 switch (keyinfo->key_alg){
53#ifdef HAVE_RTREE_KEYS
54 case HA_KEY_ALG_RTREE:
55 error=maria_rtree_get_first(info, inx,
56 info->last_key.data_length +
57 info->last_key.ref_length);
58
59 break;
60#endif
61 case HA_KEY_ALG_BTREE:
62 default:
63 error= _ma_search_first(info, keyinfo, share->state.key_root[inx]);
64 break;
65 }
66 /*
67 "search first" failed. This means we have no pivot for
68 "search next", or in other words MI_INFO::lastkey is
69 likely uninitialized.
70
71 Normally SQL layer would never request "search next" if
72 "search first" failed. But HANDLER may do anything.
73
74 As mi_rnext() without preceding mi_rkey()/mi_rfirst()
75 equals to mi_rfirst(), we must restore original state
76 as if failing mi_rfirst() was not called.
77 */
78 if (error)
79 update_mask|= HA_STATE_PREV_FOUND;
80 }
81 else
82 {
83 switch (keyinfo->key_alg) {
84#ifdef HAVE_RTREE_KEYS
85 case HA_KEY_ALG_RTREE:
86 /*
87 Note that rtree doesn't support that the table
88 may be changed since last call, so we do need
89 to skip rows inserted by other threads like in btree
90 */
91 error= maria_rtree_get_next(info, inx, info->last_key.data_length +
92 info->last_key.ref_length);
93 break;
94#endif
95 case HA_KEY_ALG_BTREE:
96 default:
97 if (!changed)
98 error= _ma_search_next(info, &info->last_key,
99 flag | info->last_key.flag,
100 share->state.key_root[inx]);
101 else
102 error= _ma_search(info, &info->last_key, flag | info->last_key.flag,
103 share->state.key_root[inx]);
104 }
105 }
106
107 if (!error)
108 {
109 while (!(*share->row_is_visible)(info) ||
110 ((icp_res= ma_check_index_cond(info, inx, buf)) == ICP_NO_MATCH))
111 {
112 /*
113 If we are at the last key on the key page, allow writers to
114 access the index.
115 */
116 if (info->int_keypos >= info->int_maxpos &&
117 ma_yield_and_check_if_killed(info, inx))
118 {
119 /* my_errno is set by ma_yield_and_check_if_killed() */
120 error= 1;
121 break;
122 }
123
124 /* Skip rows inserted by other threads since we got a lock */
125 if ((error= _ma_search_next(info, &info->last_key,
126 SEARCH_BIGGER,
127 share->state.key_root[inx])))
128 break;
129 }
130 }
131 if (share->lock_key_trees)
132 mysql_rwlock_unlock(&keyinfo->root_lock);
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_ma_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_ma_writeinfo(info);
147 DBUG_RETURN(info->cur_row.lastpos == HA_OFFSET_ERROR ? my_errno : 0);
148 }
149 else if (!(*info->read_record)(info, buf, info->cur_row.lastpos))
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} /* maria_rnext */
157