MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mi_rprev.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  /*
19  Read previous row with the same key as previous read
20  One may have done a write, update or delete of the previous row.
21  NOTE! Even if one changes the previous row, the next read is done
22  based on the position of the last used key!
23  */
24 
25 int mi_rprev(MI_INFO *info, uchar *buf, int inx)
26 {
27  int error,changed;
28  register uint flag;
29  MYISAM_SHARE *share=info->s;
30  DBUG_ENTER("mi_rprev");
31 
32  if ((inx = _mi_check_index(info,inx)) < 0)
33  DBUG_RETURN(my_errno);
34  flag=SEARCH_SMALLER; /* Read previous */
35  if (info->lastpos == HA_OFFSET_ERROR && info->update & HA_STATE_NEXT_FOUND)
36  flag=0; /* Read last */
37 
38  if (fast_mi_readinfo(info))
39  DBUG_RETURN(my_errno);
40  changed=_mi_test_if_changed(info);
41  if (share->concurrent_insert)
42  mysql_rwlock_rdlock(&share->key_root_lock[inx]);
43  if (!flag)
44  error=_mi_search_last(info, share->keyinfo+inx,
45  share->state.key_root[inx]);
46  else if (!changed)
47  error=_mi_search_next(info,share->keyinfo+inx,info->lastkey,
48  info->lastkey_length,flag,
49  share->state.key_root[inx]);
50  else
51  error=_mi_search(info,share->keyinfo+inx,info->lastkey,
52  USE_WHOLE_KEY, flag, share->state.key_root[inx]);
53 
54  if (!error)
55  {
56  int res= 0;
57  while ((share->concurrent_insert &&
58  info->lastpos >= info->state->data_file_length) ||
59  (info->index_cond_func &&
60  !(res= mi_check_index_cond(info, inx, buf))))
61  {
62  /*
63  Skip rows that are either inserted by other threads since
64  we got a lock or do not match pushed index conditions
65  */
66  if ((error=_mi_search_next(info,share->keyinfo+inx,info->lastkey,
67  info->lastkey_length,
68  SEARCH_SMALLER,
69  share->state.key_root[inx])))
70  break;
71  }
72  if (!error && res == 2)
73  {
74  if (share->concurrent_insert)
75  mysql_rwlock_unlock(&share->key_root_lock[inx]);
76  info->lastpos= HA_OFFSET_ERROR;
77  DBUG_RETURN(my_errno= HA_ERR_END_OF_FILE);
78  }
79  }
80 
81  if (share->concurrent_insert)
82  {
83  if (!error)
84  {
85  while (info->lastpos >= info->state->data_file_length)
86  {
87  /* Skip rows that are inserted by other threads since we got a lock */
88  if ((error=_mi_search_next(info,share->keyinfo+inx,info->lastkey,
89  info->lastkey_length,
90  SEARCH_SMALLER,
91  share->state.key_root[inx])))
92  break;
93  }
94  }
95  mysql_rwlock_unlock(&share->key_root_lock[inx]);
96  }
97 
98  info->update&= (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED);
99  info->update|= HA_STATE_PREV_FOUND;
100  if (error)
101  {
102  if (my_errno == HA_ERR_KEY_NOT_FOUND)
103  my_errno=HA_ERR_END_OF_FILE;
104  }
105  else if (!buf)
106  {
107  DBUG_RETURN(info->lastpos==HA_OFFSET_ERROR ? my_errno : 0);
108  }
109  else if (!(*info->read_record)(info,info->lastpos,buf))
110  {
111  info->update|= HA_STATE_AKTIV; /* Record is read */
112  DBUG_RETURN(0);
113  }
114  DBUG_RETURN(my_errno);
115 } /* mi_rprev */