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 02110-1335 USA */
15 
16 #include "maria_def.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 
maria_rprev(MARIA_HA * info,uchar * buf,int inx)25 int maria_rprev(MARIA_HA *info, uchar *buf, int inx)
26 {
27   int error,changed;
28   register uint flag;
29   MARIA_SHARE *share= info->s;
30   MARIA_KEYDEF *keyinfo;
31   check_result_t check= CHECK_POS;
32   DBUG_ENTER("maria_rprev");
33 
34   if ((inx = _ma_check_index(info,inx)) < 0)
35     DBUG_RETURN(my_errno);
36   flag=SEARCH_SMALLER;				/* Read previous */
37   if (info->cur_row.lastpos == HA_OFFSET_ERROR &&
38       info->update & HA_STATE_NEXT_FOUND)
39     flag=0;					/* Read last */
40 
41   if (fast_ma_readinfo(info))
42     DBUG_RETURN(my_errno);
43   keyinfo= share->keyinfo + inx;
44   changed= _ma_test_if_changed(info);
45   if (share->lock_key_trees)
46     mysql_rwlock_rdlock(&keyinfo->root_lock);
47   if (!flag)
48     error= _ma_search_last(info, keyinfo, share->state.key_root[inx]);
49   else if (!changed)
50     error= _ma_search_next(info, &info->last_key,
51                            flag | info->last_key.flag,
52                            share->state.key_root[inx]);
53   else
54     error= _ma_search(info, &info->last_key, flag | info->last_key.flag,
55                       share->state.key_root[inx]);
56 
57   if (!error)
58   {
59     my_off_t cur_keypage= info->last_keypage;
60     while (!(*share->row_is_visible)(info) ||
61            ((check= ma_check_index_cond(info, inx, buf)) == CHECK_NEG))
62     {
63       /*
64         If we are at the last (i.e. first?) key on the key page,
65         allow writers to access the index.
66       */
67       if (info->last_keypage != cur_keypage)
68       {
69         cur_keypage= info->last_keypage;
70         if (ma_yield_and_check_if_killed(info, inx))
71         {
72           error= 1;
73           break;
74         }
75       }
76 
77       /* Skip rows that are inserted by other threads since we got a lock */
78       if  ((error= _ma_search_next(info, &info->last_key,
79                                    SEARCH_SMALLER,
80                                    share->state.key_root[inx])))
81         break;
82     }
83   }
84   if (share->lock_key_trees)
85     mysql_rwlock_unlock(&keyinfo->root_lock);
86   info->update&= (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED);
87   info->update|= HA_STATE_PREV_FOUND;
88 
89   if (error || check != CHECK_POS)
90   {
91     fast_ma_writeinfo(info);
92     if (my_errno == HA_ERR_KEY_NOT_FOUND)
93       my_errno= HA_ERR_END_OF_FILE;
94   }
95   else if (!buf)
96   {
97     fast_ma_writeinfo(info);
98     DBUG_RETURN(info->cur_row.lastpos == HA_OFFSET_ERROR ? my_errno : 0);
99   }
100   else if (!(*info->read_record)(info, buf, info->cur_row.lastpos))
101   {
102     info->update|= HA_STATE_AKTIV;		/* Record is read */
103     DBUG_RETURN(0);
104   }
105   DBUG_RETURN(my_errno);
106 } /* maria_rprev */
107