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 : /* Read record based on a key */
17 :
18 : #include "maria_def.h"
19 : #include "ma_rt_index.h"
20 :
21 : /**
22 : Read a record using key
23 :
24 : @note
25 : Ordinary search_flag is 0 ; Give error if no record with key
26 : */
27 :
28 : int maria_rkey(MARIA_HA *info, uchar *buf, int inx, const uchar *key_data,
29 : key_part_map keypart_map, enum ha_rkey_function search_flag)
30 75966 : {
31 : uchar *key_buff;
32 75966 : MARIA_SHARE *share= info->s;
33 : MARIA_KEYDEF *keyinfo;
34 : HA_KEYSEG *last_used_keyseg;
35 : uint32 nextflag;
36 : MARIA_KEY key;
37 75966 : DBUG_ENTER("maria_rkey");
38 75966 : DBUG_PRINT("enter", ("base: 0x%lx buf: 0x%lx inx: %d search_flag: %d",
39 : (long) info, (long) buf, inx, search_flag));
40 :
41 75966 : if ((inx = _ma_check_index(info,inx)) < 0)
42 0 : DBUG_RETURN(my_errno);
43 :
44 75966 : info->update&= (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED);
45 75966 : info->last_key_func= search_flag;
46 75966 : keyinfo= share->keyinfo + inx;
47 :
48 75966 : key_buff= info->lastkey_buff+info->s->base.max_key_length;
49 :
50 75966 : if (info->once_flags & USE_PACKED_KEYS)
51 : {
52 0 : info->once_flags&= ~USE_PACKED_KEYS; /* Reset flag */
53 : /*
54 : key is already packed!; This happens when we are using a MERGE TABLE
55 : In this key 'key_part_map' is the length of the key !
56 : */
57 0 : bmove(key_buff, key_data, keypart_map);
58 0 : key.data= key_buff;
59 0 : key.keyinfo= keyinfo;
60 0 : key.data_length= keypart_map;
61 0 : key.ref_length= 0;
62 0 : key.flag= 0;
63 :
64 0 : last_used_keyseg= keyinfo->seg + info->last_used_keyseg;
65 : }
66 : else
67 : {
68 75966 : DBUG_ASSERT(keypart_map);
69 : /* Save the packed key for later use in the second buffer of lastkey. */
70 75966 : _ma_pack_key(info, &key, inx, key_buff, key_data,
71 : keypart_map, &last_used_keyseg);
72 : /* Save packed_key_length for use by the MERGE engine. */
73 75966 : info->pack_key_length= key.data_length;
74 75966 : info->last_used_keyseg= (uint16) (last_used_keyseg -
75 : keyinfo->seg);
76 75966 : DBUG_EXECUTE("key", _ma_print_key(DBUG_FILE, &key););
77 : }
78 :
79 75966 : if (fast_ma_readinfo(info))
80 75966 : goto err;
81 75966 : if (share->lock_key_trees)
82 0 : rw_rdlock(&keyinfo->root_lock);
83 :
84 75966 : nextflag= maria_read_vec[search_flag] | key.flag;
85 :
86 75966 : switch (keyinfo->key_alg) {
87 : #ifdef HAVE_RTREE_KEYS
88 : case HA_KEY_ALG_RTREE:
89 0 : if (maria_rtree_find_first(info, &key, nextflag) < 0)
90 : {
91 0 : maria_print_error(info->s, HA_ERR_CRASHED);
92 0 : my_errno= HA_ERR_CRASHED;
93 0 : info->cur_row.lastpos= HA_OFFSET_ERROR;
94 : }
95 : break;
96 : #endif
97 : case HA_KEY_ALG_BTREE:
98 : default:
99 75966 : if (!_ma_search(info, &key, nextflag, info->s->state.key_root[inx]))
100 : {
101 : MARIA_KEY lastkey;
102 74104 : lastkey.keyinfo= keyinfo;
103 74104 : lastkey.data= info->lastkey_buff;
104 : /*
105 : Found a key, but it might not be usable. We cannot use rows that
106 : are inserted by other threads after we got our table lock
107 : ("concurrent inserts"). The record may not even be present yet.
108 : Keys are inserted into the index(es) before the record is
109 : inserted into the data file.
110 : */
111 74104 : if ((*share->row_is_visible)(info))
112 0 : break;
113 :
114 : /* The key references a concurrently inserted record. */
115 0 : if (search_flag == HA_READ_KEY_EXACT &&
116 : last_used_keyseg == keyinfo->seg + keyinfo->keysegs)
117 : {
118 : /* Simply ignore the key if it matches exactly. (Bug #29838) */
119 0 : my_errno= HA_ERR_KEY_NOT_FOUND;
120 0 : info->cur_row.lastpos= HA_OFFSET_ERROR;
121 0 : break;
122 : }
123 :
124 : do
125 : {
126 : uint not_used[2];
127 : /*
128 : Skip rows that are inserted by other threads since we got
129 : a lock. Note that this can only happen if we are not
130 : searching after a full length exact key, because the keys
131 : are sorted according to position.
132 : */
133 0 : lastkey.data_length= info->last_key.data_length;
134 0 : lastkey.ref_length= info->last_key.ref_length;
135 0 : lastkey.flag= info->last_key.flag;
136 0 : if (_ma_search_next(info, &lastkey, maria_readnext_vec[search_flag],
137 : info->s->state.key_root[inx]))
138 0 : break; /* purecov: inspected */
139 : /*
140 : Check that the found key does still match the search.
141 : _ma_search_next() delivers the next key regardless of its
142 : value.
143 : */
144 0 : if (!(nextflag & (SEARCH_BIGGER | SEARCH_SMALLER)) &&
145 : ha_key_cmp(keyinfo->seg, info->last_key.data, key.data,
146 : key.data_length, SEARCH_FIND, not_used))
147 : {
148 : /* purecov: begin inspected */
149 0 : my_errno= HA_ERR_KEY_NOT_FOUND;
150 0 : info->cur_row.lastpos= HA_OFFSET_ERROR;
151 0 : break;
152 : /* purecov: end */
153 : }
154 0 : } while (!(*share->row_is_visible)(info));
155 : }
156 : }
157 75966 : if (share->lock_key_trees)
158 0 : rw_unlock(&keyinfo->root_lock);
159 :
160 75966 : if (info->cur_row.lastpos == HA_OFFSET_ERROR)
161 : {
162 1862 : fast_ma_writeinfo(info);
163 : goto err;
164 : }
165 :
166 : /* Calculate length of the found key; Used by maria_rnext_same */
167 74104 : if ((keyinfo->flag & HA_VAR_LENGTH_KEY))
168 57962 : info->last_rkey_length= _ma_keylength_part(keyinfo, info->lastkey_buff,
169 : last_used_keyseg);
170 : else
171 16142 : info->last_rkey_length= key.data_length;
172 :
173 : /* Check if we don't want to have record back, only error message */
174 74104 : if (!buf)
175 : {
176 0 : fast_ma_writeinfo(info);
177 0 : DBUG_RETURN(0);
178 : }
179 74104 : if (!(*info->read_record)(info, buf, info->cur_row.lastpos))
180 : {
181 74104 : info->update|= HA_STATE_AKTIV; /* Record is read */
182 74104 : DBUG_RETURN(0);
183 : }
184 :
185 0 : info->cur_row.lastpos= HA_OFFSET_ERROR; /* Didn't find row */
186 :
187 1862 : err:
188 : /* Store last used key as a base for read next */
189 1862 : memcpy(info->last_key.data, key_buff, key.data_length);
190 1862 : info->last_key.data_length= key.data_length;
191 1862 : info->last_key.ref_length= info->s->base.rec_reflength;
192 1862 : info->last_key.flag= 0;
193 : /* Create key with rowid 0 */
194 1862 : bzero((char*) info->last_key.data + info->last_key.data_length,
195 : info->s->base.rec_reflength);
196 :
197 1862 : if (search_flag == HA_READ_AFTER_KEY)
198 0 : info->update|=HA_STATE_NEXT_FOUND; /* Previous gives last row */
199 1862 : DBUG_RETURN(my_errno);
200 : } /* _ma_rkey */
|