MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mi_rnext.c
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 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 St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 #include "myisamdef.h"
17 
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  int res= 0;
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 preceeding 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  !(res= mi_check_index_cond(info, inx, buf))))
106  {
107  /*
108  Skip rows that are either inserted by other threads since
109  we got a lock or do not match pushed index conditions
110  */
111  if ((error=_mi_search_next(info,info->s->keyinfo+inx,
112  info->lastkey,
113  info->lastkey_length,
114  SEARCH_BIGGER,
115  info->s->state.key_root[inx])))
116  break;
117  }
118  if (!error && res == 2)
119  {
120  if (info->s->concurrent_insert)
121  mysql_rwlock_unlock(&info->s->key_root_lock[inx]);
122  info->lastpos= HA_OFFSET_ERROR;
123  DBUG_RETURN(my_errno= HA_ERR_END_OF_FILE);
124  }
125  }
126 
127  if (info->s->concurrent_insert)
128  {
129  if (!error)
130  {
131  while (info->lastpos >= info->state->data_file_length)
132  {
133  /* Skip rows inserted by other threads since we got a lock */
134  if ((error=_mi_search_next(info,info->s->keyinfo+inx,
135  info->lastkey,
136  info->lastkey_length,
137  SEARCH_BIGGER,
138  info->s->state.key_root[inx])))
139  break;
140  }
141  }
142  mysql_rwlock_unlock(&info->s->key_root_lock[inx]);
143  }
144  /* Don't clear if database-changed */
145  info->update&= (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED);
146  info->update|= update_mask;
147 
148  if (error)
149  {
150  if (my_errno == HA_ERR_KEY_NOT_FOUND)
151  my_errno=HA_ERR_END_OF_FILE;
152  }
153  else if (!buf)
154  {
155  DBUG_RETURN(info->lastpos==HA_OFFSET_ERROR ? my_errno : 0);
156  }
157  else if (!(*info->read_record)(info,info->lastpos,buf))
158  {
159  info->update|= HA_STATE_AKTIV; /* Record is read */
160  DBUG_RETURN(0);
161  }
162  DBUG_PRINT("error",("Got error: %d, errno: %d",error, my_errno));
163  DBUG_RETURN(my_errno);
164 } /* mi_rnext */