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