drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
hp_write.cc
1/* Copyright (C) 2000-2002, 2004-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/* Write a record to heap-databas */
17
18#include "heap_priv.h"
19
20#include <drizzled/error_t.h>
21
22#define LOWFIND 1
23#define LOWUSED 2
24#define HIGHFIND 4
25#define HIGHUSED 8
26
27using namespace drizzled;
28
29static HASH_INFO *hp_find_free_hash(HP_SHARE *info, HP_BLOCK *block,
30 uint32_t records);
31
32int heap_write(HP_INFO *info, const unsigned char *record)
33{
34 HP_KEYDEF *keydef, *end;
35 unsigned char *pos;
36 HP_SHARE *share=info->getShare();
37
38 if ((share->records >= share->max_records && share->max_records) ||
39 (share->recordspace.total_data_length + share->index_length >= share->max_table_size))
40 {
41 return(errno=HA_ERR_RECORD_FILE_FULL);
42 }
43
44 if (!(pos=hp_allocate_chunkset(&share->recordspace, 1)))
45 return(errno);
46 share->changed=1;
47
48 for (keydef = share->keydef, end = keydef + share->keys; keydef < end;
49 keydef++)
50 {
51 if (hp_write_key(info, keydef, record, pos))
52 goto err;
53 }
54
55 hp_copy_record_data_to_chunkset(share, record, pos);
56
57 if (++share->records == share->blength)
58 share->blength+= share->blength;
59
60 info->current_ptr=pos;
61 info->current_hash_ptr=0;
62 info->update|=HA_STATE_AKTIV;
63 if (share->auto_key)
64 heap_update_auto_increment(info, record);
65 return(0);
66
67err:
68 info->errkey= keydef - share->keydef;
69 while (keydef >= share->keydef)
70 {
71 if (hp_delete_key(info, keydef, record, pos, 0))
72 break;
73 keydef--;
74 }
75
76 hp_free_chunks(&share->recordspace, pos);
77
78 return(errno);
79} /* heap_write */
80
81/*
82 Write a hash-key to the hash-index
83 SYNOPSIS
84 info Heap table info
85 keyinfo Key info
86 record Table record to added
87 recpos Memory buffer where the table record will be stored if added
88 successfully
89 NOTE
90 Hash index uses HP_BLOCK structure as a 'growable array' of HASH_INFO
91 structs. Array size == number of entries in hash index.
92 hp_mask(hp_rec_hashnr()) maps hash entries values to hash array positions.
93 If there are several hash entries with the same hash array position P,
94 they are connected in a linked list via HASH_INFO::next_key. The first
95 list element is located at position P, next elements are located at
96 positions for which there is no record that should be located at that
97 position. The order of elements in the list is arbitrary.
98
99 RETURN
100 0 - OK
101 -1 - Out of memory
102 HA_ERR_FOUND_DUPP_KEY - Duplicate record on unique key. The record was
103 still added and the caller must call hp_delete_key for it.
104*/
105
106int hp_write_key(HP_INFO *info, HP_KEYDEF *keyinfo,
107 const unsigned char *record, unsigned char *recpos)
108{
109 HP_SHARE *share = info->getShare();
110 int flag;
111 uint32_t halfbuff,hashnr,first_index;
112 unsigned char *ptr_to_rec= NULL,*ptr_to_rec2= NULL;
113 HASH_INFO *empty, *gpos= NULL, *gpos2= NULL, *pos;
114
115 flag=0;
116 if (!(empty= hp_find_free_hash(share,&keyinfo->block,share->records)))
117 return(-1); /* No more memory */
118 halfbuff= (long) share->blength >> 1;
119 pos= hp_find_hash(&keyinfo->block,(first_index=share->records-halfbuff));
120
121 /*
122 We're about to add one more hash array position, with hash_mask=#records.
123 The number of hash positions will change and some entries might need to
124 be relocated to the newly added position. Those entries are currently
125 members of the list that starts at #first_index position (this is
126 guaranteed by properties of hp_mask(hp_rec_hashnr(X)) mapping function)
127 At #first_index position currently there may be either:
128 a) An entry with hashnr != first_index. We don't need to move it.
129 or
130 b) A list of items with hash_mask=first_index. The list contains entries
131 of 2 types:
132 1) entries that should be relocated to the list that starts at new
133 position we're adding ('uppper' list)
134 2) entries that should be left in the list starting at #first_index
135 position ('lower' list)
136 */
137 if (pos != empty) /* If some records */
138 {
139 do
140 {
141 hashnr = hp_rec_hashnr(keyinfo, pos->ptr_to_rec);
142 if (flag == 0)
143 {
144 /*
145 First loop, bail out if we're dealing with case a) from above
146 comment
147 */
148 if (hp_mask(hashnr, share->blength, share->records) != first_index)
149 break;
150 }
151 /*
152 flag & LOWFIND - found a record that should be put into lower position
153 flag & LOWUSED - lower position occupied by the record
154 Same for HIGHFIND and HIGHUSED and 'upper' position
155
156 gpos - ptr to last element in lower position's list
157 gpos2 - ptr to last element in upper position's list
158
159 ptr_to_rec - ptr to last entry that should go into lower list.
160 ptr_to_rec2 - same for upper list.
161 */
162 if (!(hashnr & halfbuff))
163 {
164 /* Key should be put into 'lower' list */
165 if (!(flag & LOWFIND))
166 {
167 /* key is the first element to go into lower position */
168 if (flag & HIGHFIND)
169 {
170 flag=LOWFIND | HIGHFIND;
171 /* key shall be moved to the current empty position */
172 gpos=empty;
173 ptr_to_rec=pos->ptr_to_rec;
174 empty=pos; /* This place is now free */
175 }
176 else
177 {
178 /*
179 We can only get here at first iteration: key is at 'lower'
180 position pos and should be left here.
181 */
182 flag=LOWFIND | LOWUSED;
183 gpos=pos;
184 ptr_to_rec=pos->ptr_to_rec;
185 }
186 }
187 else
188 {
189 /* Already have another key for lower position */
190 if (!(flag & LOWUSED))
191 {
192 /* Change link of previous lower-list key */
193 gpos->ptr_to_rec=ptr_to_rec;
194 gpos->next_key=pos;
195 flag= (flag & HIGHFIND) | (LOWFIND | LOWUSED);
196 }
197 gpos=pos;
198 ptr_to_rec=pos->ptr_to_rec;
199 }
200 }
201 else
202 {
203 /* key will be put into 'higher' list */
204 if (!(flag & HIGHFIND))
205 {
206 flag= (flag & LOWFIND) | HIGHFIND;
207 /* key shall be moved to the last (empty) position */
208 gpos2= empty;
209 empty= pos;
210 ptr_to_rec2=pos->ptr_to_rec;
211 }
212 else
213 {
214 if (!(flag & HIGHUSED))
215 {
216 /* Change link of previous upper-list key and save */
217 gpos2->ptr_to_rec=ptr_to_rec2;
218 gpos2->next_key=pos;
219 flag= (flag & LOWFIND) | (HIGHFIND | HIGHUSED);
220 }
221 gpos2=pos;
222 ptr_to_rec2=pos->ptr_to_rec;
223 }
224 }
225 }
226 while ((pos=pos->next_key));
227
228 if ((flag & (LOWFIND | HIGHFIND)) == (LOWFIND | HIGHFIND))
229 {
230 /*
231 If both 'higher' and 'lower' list have at least one element, now
232 there are two hash buckets instead of one.
233 */
234 keyinfo->hash_buckets++;
235 }
236
237 if ((flag & (LOWFIND | LOWUSED)) == LOWFIND)
238 {
239 gpos->ptr_to_rec=ptr_to_rec;
240 gpos->next_key=0;
241 }
242 if ((flag & (HIGHFIND | HIGHUSED)) == HIGHFIND)
243 {
244 gpos2->ptr_to_rec=ptr_to_rec2;
245 gpos2->next_key=0;
246 }
247 }
248 /* Check if we are at the empty position */
249
250 pos=hp_find_hash(&keyinfo->block, hp_mask(hp_rec_hashnr(keyinfo, record),
251 share->blength, share->records + 1));
252 if (pos == empty)
253 {
254 pos->ptr_to_rec=recpos;
255 pos->next_key=0;
256 keyinfo->hash_buckets++;
257 }
258 else
259 {
260 /* Check if more records in same hash-nr family */
261 empty[0]=pos[0];
262 gpos=hp_find_hash(&keyinfo->block,
263 hp_mask(hp_rec_hashnr(keyinfo, pos->ptr_to_rec),
264 share->blength, share->records + 1));
265 if (pos == gpos)
266 {
267 pos->ptr_to_rec=recpos;
268 pos->next_key=empty;
269 }
270 else
271 {
272 keyinfo->hash_buckets++;
273 pos->ptr_to_rec=recpos;
274 pos->next_key=0;
275 hp_movelink(pos, gpos, empty);
276 }
277
278 /* Check if duplicated keys */
279 if ((keyinfo->flag & HA_NOSAME) && pos == gpos &&
280 (!(keyinfo->flag & HA_NULL_PART_KEY) ||
281 !hp_if_null_in_key(keyinfo, record)))
282 {
283 pos=empty;
284 do
285 {
286 if (! hp_rec_key_cmp(keyinfo, record, pos->ptr_to_rec, 1))
287 {
288 return(errno=HA_ERR_FOUND_DUPP_KEY);
289 }
290 } while ((pos=pos->next_key));
291 }
292 }
293 return(0);
294}
295
296 /* Returns ptr to block, and allocates block if neaded */
297
298static HASH_INFO *hp_find_free_hash(HP_SHARE *info,
299 HP_BLOCK *block, uint32_t records)
300{
301 uint32_t block_pos;
302 size_t length;
303
304 if (records < block->last_allocated)
305 return hp_find_hash(block,records);
306 if (!(block_pos=(records % block->records_in_block)))
307 {
308 if (hp_get_new_block(block,&length))
309 return(NULL);
310 info->index_length+=length;
311 }
312 block->last_allocated=records+1;
313 return((HASH_INFO*) ((unsigned char*) block->level_info[0].last_blocks+
314 block_pos*block->recbuffer));
315}
TODO: Rename this file - func.h is stupid.