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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
15 :
16 : #include "maria_def.h"
17 : #include "ma_rt_index.h"
18 :
19 : /*
20 : Read next row with the same key as previous read, but abort if
21 : the key changes.
22 : One may have done a write, update or delete of the previous row.
23 :
24 : NOTE! Even if one changes the previous row, the next read is done
25 : based on the position of the last used key!
26 : */
27 :
28 : int maria_rnext_same(MARIA_HA *info, uchar *buf)
29 0 : {
30 : int error;
31 : uint inx,not_used[2];
32 : MARIA_KEYDEF *keyinfo;
33 0 : DBUG_ENTER("maria_rnext_same");
34 :
35 0 : if ((int) (inx= info->lastinx) < 0 ||
36 : info->cur_row.lastpos == HA_OFFSET_ERROR)
37 0 : DBUG_RETURN(my_errno=HA_ERR_WRONG_INDEX);
38 0 : if (fast_ma_readinfo(info))
39 0 : DBUG_RETURN(my_errno);
40 :
41 0 : keyinfo= info->s->keyinfo+inx;
42 0 : if (info->s->lock_key_trees)
43 0 : rw_rdlock(&keyinfo->root_lock);
44 :
45 0 : switch (keyinfo->key_alg) {
46 : #ifdef HAVE_RTREE_KEYS
47 : case HA_KEY_ALG_RTREE:
48 0 : if ((error=maria_rtree_find_next(info,inx,
49 : maria_read_vec[info->last_key_func])))
50 : {
51 0 : error=1;
52 0 : my_errno=HA_ERR_END_OF_FILE;
53 0 : info->cur_row.lastpos= HA_OFFSET_ERROR;
54 0 : break;
55 : }
56 : break;
57 : #endif
58 : case HA_KEY_ALG_BTREE:
59 : default:
60 0 : if (!(info->update & HA_STATE_RNEXT_SAME))
61 : {
62 : /* First rnext_same; Store old key */
63 0 : memcpy(info->lastkey_buff2, info->last_key.data,
64 : info->last_rkey_length);
65 : }
66 : for (;;)
67 : {
68 0 : if ((error= _ma_search_next(info, &info->last_key,
69 : SEARCH_BIGGER,
70 : info->s->state.key_root[inx])))
71 0 : break;
72 0 : if (ha_key_cmp(keyinfo->seg, info->last_key.data,
73 : info->lastkey_buff2,
74 : info->last_rkey_length, SEARCH_FIND,
75 : not_used))
76 : {
77 0 : error=1;
78 0 : my_errno=HA_ERR_END_OF_FILE;
79 0 : info->cur_row.lastpos= HA_OFFSET_ERROR;
80 0 : break;
81 : }
82 : /* Skip rows that are inserted by other threads since we got a lock */
83 0 : if ((info->s->row_is_visible)(info))
84 : break;
85 : }
86 : }
87 0 : if (info->s->lock_key_trees)
88 0 : rw_unlock(&keyinfo->root_lock);
89 : /* Don't clear if database-changed */
90 0 : info->update&= (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED);
91 0 : info->update|= HA_STATE_NEXT_FOUND | HA_STATE_RNEXT_SAME;
92 :
93 0 : if (error)
94 : {
95 0 : if (my_errno == HA_ERR_KEY_NOT_FOUND)
96 0 : my_errno=HA_ERR_END_OF_FILE;
97 : }
98 0 : else if (!buf)
99 : {
100 0 : DBUG_RETURN(info->cur_row.lastpos == HA_OFFSET_ERROR ? my_errno : 0);
101 : }
102 0 : else if (!(*info->read_record)(info, buf, info->cur_row.lastpos))
103 : {
104 0 : info->update|= HA_STATE_AKTIV; /* Record is read */
105 0 : DBUG_RETURN(0);
106 : }
107 0 : DBUG_RETURN(my_errno);
108 : } /* maria_rnext_same */
|