drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
mi_rkey.cc
1/* Copyright (C) 2000-2006 MySQL 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 St, Fifth Floor, Boston, MA 02110-1301 USA */
15
16/* Read record based on a key */
17
18#include "myisam_priv.h"
19#include <drizzled/message/table.h>
20
21using namespace drizzled;
22
23 /* Read a record using key */
24 /* Ordinary search_flag is 0 ; Give error if no record with key */
25
26int mi_rkey(MI_INFO *info, unsigned char *buf, int inx, const unsigned char *key,
27 key_part_map keypart_map, enum ha_rkey_function search_flag)
28{
29 unsigned char *key_buff;
30 MYISAM_SHARE *share=info->s;
31 MI_KEYDEF *keyinfo;
32 HA_KEYSEG *last_used_keyseg;
33 uint32_t pack_key_length, use_key_length, nextflag;
34 uint32_t myisam_search_flag;
35 int res= 0;
36
37 if ((inx = _mi_check_index(info,inx)) < 0)
38 return(errno);
39
40 info->update&= (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED);
41 info->last_key_func= search_flag;
42 keyinfo= share->keyinfo + inx;
43
44 if (info->once_flags & USE_PACKED_KEYS)
45 {
46 info->once_flags&= ~USE_PACKED_KEYS; /* Reset flag */
47 /*
48 key is already packed!; This happens when we are using a MERGE TABLE
49 In this key 'key_part_map' is the length of the key !
50 */
51 key_buff=info->lastkey+info->s->base.max_key_length;
52 pack_key_length= keypart_map;
53 memmove(key_buff, key, pack_key_length);
54 last_used_keyseg= info->s->keyinfo[inx].seg + info->last_used_keyseg;
55 }
56 else
57 {
58 assert(keypart_map);
59 /* Save the packed key for later use in the second buffer of lastkey. */
60 key_buff=info->lastkey+info->s->base.max_key_length;
61 pack_key_length=_mi_pack_key(info,(uint) inx, key_buff, (unsigned char*) key,
62 keypart_map, &last_used_keyseg);
63 /* Save packed_key_length for use by the MERGE engine. */
64 info->pack_key_length= pack_key_length;
65 info->last_used_keyseg= (uint16_t) (last_used_keyseg -
66 info->s->keyinfo[inx].seg);
67 }
68
69 if (fast_mi_readinfo(info))
70 goto err;
71
72 nextflag=myisam_read_vec[search_flag];
73 use_key_length=pack_key_length;
74 if (!(nextflag & (SEARCH_FIND | SEARCH_NO_FIND | SEARCH_LAST)))
75 use_key_length=USE_WHOLE_KEY;
76
77 switch (info->s->keyinfo[inx].key_alg) {
78 case message::Table::Index::BTREE:
79 default:
80 myisam_search_flag= myisam_read_vec[search_flag];
81 if (!_mi_search(info, keyinfo, key_buff, use_key_length,
82 myisam_search_flag, info->s->state.key_root[inx]))
83 {
84 /*
85 Found a key, but it might not be usable. We cannot use rows that
86 are inserted by other threads after we got our table lock
87 ("concurrent inserts"). The record may not even be present yet.
88 Keys are inserted into the index(es) before the record is
89 inserted into the data file. When we got our table lock, we
90 saved the current data_file_length. Concurrent inserts always go
91 to the end of the file. So we can test if the found key
92 references a new record.
93
94 If we are searching for a partial key (or using >, >=, < or <=) and
95 the data is outside of the data file, we need to continue searching
96 for the first key inside the data file.
97
98 We do also continue searching if an index condition check function
99 is available.
100 */
101 while ((info->lastpos >= info->state->data_file_length &&
102 (search_flag != HA_READ_KEY_EXACT ||
103 last_used_keyseg != keyinfo->seg + keyinfo->keysegs)) ||
104 (info->index_cond_func &&
105 !(res= mi_check_index_cond(info, inx, buf))))
106 {
107 uint32_t not_used[2];
108 /*
109 Skip rows that are inserted by other threads since we got a lock
110 Note that this can only happen if we are not searching after an
111 full length exact key, because the keys are sorted
112 according to position
113 */
114 if (_mi_search_next(info, keyinfo, info->lastkey,
115 info->lastkey_length,
116 myisam_readnext_vec[search_flag],
117 info->s->state.key_root[inx]))
118 break;
119 /*
120 Check that the found key does still match the search.
121 _mi_search_next() delivers the next key regardless of its
122 value.
123 */
124 if (search_flag == HA_READ_KEY_EXACT &&
125 ha_key_cmp(keyinfo->seg, key_buff, info->lastkey, use_key_length,
126 SEARCH_FIND, not_used))
127 {
128 errno= HA_ERR_KEY_NOT_FOUND;
129 info->lastpos= HA_OFFSET_ERROR;
130 break;
131 }
132 }
133 if (res == 2)
134 {
135 info->lastpos= HA_OFFSET_ERROR;
136 return((errno= HA_ERR_KEY_NOT_FOUND));
137 }
138 /*
139 Error if no row found within the data file. (Bug #29838)
140 Do not overwrite errno if already at HA_OFFSET_ERROR.
141 */
142 if (info->lastpos != HA_OFFSET_ERROR &&
143 info->lastpos >= info->state->data_file_length)
144 {
145 info->lastpos= HA_OFFSET_ERROR;
146 errno= HA_ERR_KEY_NOT_FOUND;
147 }
148 }
149 }
150
151 /* Calculate length of the found key; Used by mi_rnext_same */
152 if ((keyinfo->flag & HA_VAR_LENGTH_KEY) && last_used_keyseg &&
153 info->lastpos != HA_OFFSET_ERROR)
154 info->last_rkey_length= _mi_keylength_part(keyinfo, info->lastkey,
155 last_used_keyseg);
156 else
157 info->last_rkey_length= pack_key_length;
158
159 /* Check if we don't want to have record back, only error message */
160 if (!buf)
161 return(info->lastpos == HA_OFFSET_ERROR ? errno : 0);
162
163 if (!(*info->read_record)(info,info->lastpos,buf))
164 {
165 info->update|= HA_STATE_AKTIV; /* Record is read */
166 return(0);
167 }
168
169 info->lastpos = HA_OFFSET_ERROR; /* Didn't find key */
170
171 /* Store last used key as a base for read next */
172 memcpy(info->lastkey,key_buff,pack_key_length);
173 info->last_rkey_length= pack_key_length;
174 memset(info->lastkey+pack_key_length, 0, info->s->base.rec_reflength);
175 info->lastkey_length=pack_key_length+info->s->base.rec_reflength;
176
177 if (search_flag == HA_READ_AFTER_KEY)
178 info->update|=HA_STATE_NEXT_FOUND; /* Previous gives last row */
179err:
180 return(errno);
181} /* _mi_rkey */
TODO: Rename this file - func.h is stupid.