12
12
You should have received a copy of the GNU General Public License
13
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 */
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
16
/* Write a row to a MyISAM table */
18
#include "myisam_priv.h"
20
#include <drizzled/internal/m_string.h>
21
#include <drizzled/util/test.h>
23
using namespace drizzled;
18
#include "myisamdef.h"
25
20
#define MAX_POINTER_LENGTH 8
27
22
/* Functions declared in this file */
29
24
static int w_search(MI_INFO *info,MI_KEYDEF *keyinfo,
30
uint32_t comp_flag, unsigned char *key,
31
uint32_t key_length, internal::my_off_t pos, unsigned char *father_buff,
32
unsigned char *father_keypos, internal::my_off_t father_page,
34
static int _mi_balance_page(MI_INFO *info,MI_KEYDEF *keyinfo,unsigned char *key,
35
unsigned char *curr_buff,unsigned char *father_buff,
36
unsigned char *father_keypos,internal::my_off_t father_page);
37
static unsigned char *_mi_find_last_pos(MI_KEYDEF *keyinfo, unsigned char *page,
38
unsigned char *key, uint32_t *return_key_length,
39
unsigned char **after_key);
40
int _mi_ck_write_tree(register MI_INFO *info, uint32_t keynr,unsigned char *key,
42
int _mi_ck_write_btree(register MI_INFO *info, uint32_t keynr,unsigned char *key,
25
uint comp_flag, uchar *key,
26
uint key_length, my_off_t pos, uchar *father_buff,
27
uchar *father_keypos, my_off_t father_page,
29
static int _mi_balance_page(MI_INFO *info,MI_KEYDEF *keyinfo,uchar *key,
30
uchar *curr_buff,uchar *father_buff,
31
uchar *father_keypos,my_off_t father_page);
32
static uchar *_mi_find_last_pos(MI_KEYDEF *keyinfo, uchar *page,
33
uchar *key, uint *return_key_length,
35
int _mi_ck_write_tree(register MI_INFO *info, uint keynr,uchar *key,
37
int _mi_ck_write_btree(register MI_INFO *info, uint keynr,uchar *key,
45
40
/* Write new record to database */
47
int mi_write(MI_INFO *info, unsigned char *record)
42
int mi_write(MI_INFO *info, uchar *record)
49
44
MYISAM_SHARE *share=info->s;
52
internal::my_off_t filepos;
54
bool lock_tree= share->concurrent_insert;
49
my_bool lock_tree= share->concurrent_insert;
56
51
if (share->options & HA_OPTION_READ_ONLY_DATA)
53
return(my_errno=EACCES);
60
55
if (_mi_readinfo(info,F_WRLCK,1))
62
57
filepos= ((share->state.dellink != HA_OFFSET_ERROR &&
63
58
!info->append_insert_at_end) ?
64
59
share->state.dellink :
96
91
if (mi_is_key_active(share->state.key_map, i))
98
bool local_lock_tree= (lock_tree &&
93
my_bool local_lock_tree= (lock_tree &&
99
94
!(info->bulk_insert &&
100
95
is_tree_inited(&info->bulk_insert[i])));
101
96
if (local_lock_tree)
98
rw_wrlock(&share->key_root_lock[i]);
103
99
share->keyinfo[i].version++;
106
102
if (share->keyinfo[i].ck_insert(info,i,buff,
107
103
_mi_make_key(info,i,buff,record,filepos)))
106
rw_unlock(&share->key_root_lock[i]);
113
111
/* The above changed info->lastkey2. Inform mi_rnext_same(). */
114
112
info->update&= ~HA_STATE_RNEXT_SAME;
115
rw_unlock(&share->key_root_lock[i]);
117
118
if (share->calc_checksum)
139
146
not critical to use outdated share->is_log_table value (2) locking
140
147
mutex here for every write is too expensive.
142
if (share->is_log_table) // Log table do not exist in Drizzle
149
if (share->is_log_table)
150
mi_update_status((void*) info);
149
if (errno == HA_ERR_FOUND_DUPP_KEY || errno == HA_ERR_RECORD_FILE_FULL ||
150
errno == HA_ERR_NULL_IN_SPATIAL || errno == HA_ERR_OUT_OF_MEM)
156
if (my_errno == HA_ERR_FOUND_DUPP_KEY || my_errno == HA_ERR_RECORD_FILE_FULL ||
157
my_errno == HA_ERR_NULL_IN_SPATIAL || my_errno == HA_ERR_OUT_OF_MEM)
152
159
if (info->bulk_insert)
155
162
for (j=0 ; j < share->base.keys ; j++)
156
163
mi_flush_bulk_insert(info, j);
161
168
if (mi_is_key_active(share->state.key_map, i))
170
my_bool local_lock_tree= (lock_tree &&
171
!(info->bulk_insert &&
172
is_tree_inited(&info->bulk_insert[i])));
174
rw_wrlock(&share->key_root_lock[i]);
164
uint32_t key_length=_mi_make_key(info,i,buff,record,filepos);
176
uint key_length=_mi_make_key(info,i,buff,record,filepos);
165
177
if (_mi_ck_delete(info,i,buff,key_length))
180
rw_unlock(&share->key_root_lock[i]);
185
rw_unlock(&share->key_root_lock[i]);
176
192
mi_mark_crashed(info);
178
194
info->update= (HA_STATE_CHANGED | HA_STATE_WRITTEN | HA_STATE_ROW_CHANGED);
182
_mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE);
183
return(errno=save_errno);
198
myisam_log_record(MI_LOG_WRITE,info,record,filepos,my_errno);
199
VOID(_mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE));
200
return(my_errno=save_errno);
187
204
/* Write one key to btree */
189
int _mi_ck_write(MI_INFO *info, uint32_t keynr, unsigned char *key, uint32_t key_length)
206
int _mi_ck_write(MI_INFO *info, uint keynr, uchar *key, uint key_length)
191
208
if (info->bulk_insert && is_tree_inited(&info->bulk_insert[keynr]))
203
220
* Normal insert code *
204
221
**********************************************************************/
206
int _mi_ck_write_btree(register MI_INFO *info, uint32_t keynr, unsigned char *key,
223
int _mi_ck_write_btree(register MI_INFO *info, uint keynr, uchar *key,
211
228
MI_KEYDEF *keyinfo=info->s->keyinfo+keynr;
212
internal::my_off_t *root=&info->s->state.key_root[keynr];
229
my_off_t *root=&info->s->state.key_root[keynr];
214
231
if (keyinfo->flag & HA_SORT_ALLOWS_SAME)
215
232
comp_flag=SEARCH_BIGGER; /* Put after same key */
228
245
} /* _mi_ck_write_btree */
230
247
int _mi_ck_real_write_btree(MI_INFO *info, MI_KEYDEF *keyinfo,
231
unsigned char *key, uint32_t key_length, internal::my_off_t *root, uint32_t comp_flag)
248
uchar *key, uint key_length, my_off_t *root, uint comp_flag)
234
251
/* key_length parameter is used only if comp_flag is SEARCH_FIND */
235
252
if (*root == HA_OFFSET_ERROR ||
236
253
(error=w_search(info, keyinfo, comp_flag, key, key_length,
237
*root, (unsigned char *) 0, (unsigned char*) 0,
238
(internal::my_off_t) 0, 1)) > 0)
254
*root, (uchar *) 0, (uchar*) 0,
255
(my_off_t) 0, 1)) > 0)
239
256
error=_mi_enlarge_root(info,keyinfo,key,root);
241
258
} /* _mi_ck_real_write_btree */
244
261
/* Make a new root with key as only pointer */
246
int _mi_enlarge_root(MI_INFO *info, MI_KEYDEF *keyinfo, unsigned char *key,
247
internal::my_off_t *root)
263
int _mi_enlarge_root(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *key,
249
uint32_t t_length,nod_flag;
266
uint t_length,nod_flag;
250
267
MI_KEY_PARAM s_temp;
251
268
MYISAM_SHARE *share=info->s;
253
270
nod_flag= (*root != HA_OFFSET_ERROR) ? share->base.key_reflength : 0;
254
271
_mi_kpointer(info,info->buff+2,*root); /* if nod */
255
t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,(unsigned char*) 0,
256
(unsigned char*) 0, (unsigned char*) 0, key,&s_temp);
272
t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,(uchar*) 0,
273
(uchar*) 0, (uchar*) 0, key,&s_temp);
257
274
mi_putint(info->buff,t_length+2+nod_flag,nod_flag);
258
275
(*keyinfo->store_key)(keyinfo,info->buff+2+nod_flag,&s_temp);
259
276
info->buff_used=info->page_changed=1; /* info->buff is used */
274
291
static int w_search(register MI_INFO *info, register MI_KEYDEF *keyinfo,
275
uint32_t comp_flag, unsigned char *key, uint32_t key_length, internal::my_off_t page,
276
unsigned char *father_buff, unsigned char *father_keypos,
277
internal::my_off_t father_page, bool insert_last)
292
uint comp_flag, uchar *key, uint key_length, my_off_t page,
293
uchar *father_buff, uchar *father_keypos,
294
my_off_t father_page, my_bool insert_last)
280
uint32_t nod_flag, search_key_length;
281
unsigned char *temp_buff,*keypos;
282
unsigned char keybuff[MI_MAX_KEY_BUFF];
284
internal::my_off_t next_page, dupp_key_pos;
297
uint nod_flag, search_key_length;
298
uchar *temp_buff,*keypos;
299
uchar keybuff[MI_MAX_KEY_BUFF];
300
my_bool was_last_key;
301
my_off_t next_page, dupp_key_pos;
286
303
search_key_length= (comp_flag & SEARCH_FIND) ? key_length : USE_WHOLE_KEY;
287
if (!(temp_buff= (unsigned char*) malloc(keyinfo->block_length+
304
if (!(temp_buff= (uchar*) my_alloca((uint) keyinfo->block_length+
290
307
if (!_mi_fetch_keypage(info,keyinfo,page,DFLT_INIT_HITS,temp_buff,0))
360
377
int _mi_insert(register MI_INFO *info, register MI_KEYDEF *keyinfo,
361
unsigned char *key, unsigned char *anc_buff, unsigned char *key_pos, unsigned char *key_buff,
362
unsigned char *father_buff, unsigned char *father_key_pos, internal::my_off_t father_page,
378
uchar *key, uchar *anc_buff, uchar *key_pos, uchar *key_buff,
379
uchar *father_buff, uchar *father_key_pos, my_off_t father_page,
365
uint32_t a_length,nod_flag;
382
uint a_length,nod_flag;
367
unsigned char *endpos, *prev_key;
384
uchar *endpos, *prev_key;
368
385
MI_KEY_PARAM s_temp;
370
387
nod_flag=mi_test_if_nod(anc_buff);
371
388
a_length=mi_getint(anc_buff);
372
389
endpos= anc_buff+ a_length;
373
prev_key=(key_pos == anc_buff+2+nod_flag ? (unsigned char*) 0 : key_buff);
390
prev_key=(key_pos == anc_buff+2+nod_flag ? (uchar*) 0 : key_buff);
374
391
t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,
375
(key_pos == endpos ? (unsigned char*) 0 : key_pos),
392
(key_pos == endpos ? (uchar*) 0 : key_pos),
376
393
prev_key, prev_key,
381
398
if (t_length >= keyinfo->maxlength*2+MAX_POINTER_LENGTH)
383
400
mi_print_error(info->s, HA_ERR_CRASHED);
384
errno=HA_ERR_CRASHED;
401
my_errno=HA_ERR_CRASHED;
387
internal::bmove_upp((unsigned char*) endpos+t_length,(unsigned char*) endpos,(uint) (endpos-key_pos));
404
bmove_upp((uchar*) endpos+t_length,(uchar*) endpos,(uint) (endpos-key_pos));
391
408
if (-t_length >= keyinfo->maxlength*2+MAX_POINTER_LENGTH)
393
410
mi_print_error(info->s, HA_ERR_CRASHED);
394
errno=HA_ERR_CRASHED;
411
my_errno=HA_ERR_CRASHED;
397
memmove(key_pos, key_pos - t_length, endpos - key_pos + t_length);
414
memcpy(key_pos,key_pos-t_length,(uint) (endpos-key_pos)+t_length);
399
416
(*keyinfo->store_key)(keyinfo,key_pos,&s_temp);
400
417
a_length+=t_length;
417
434
/* split a full page in two and assign emerging item to key */
419
436
int _mi_split_page(register MI_INFO *info, register MI_KEYDEF *keyinfo,
420
unsigned char *key, unsigned char *buff, unsigned char *key_buff,
421
bool insert_last_key)
437
uchar *key, uchar *buff, uchar *key_buff,
438
my_bool insert_last_key)
423
uint32_t length,a_length,key_ref_length,t_length,nod_flag,key_length;
424
unsigned char *key_pos,*pos, *after_key= NULL;
425
internal::my_off_t new_pos;
440
uint length,a_length,key_ref_length,t_length,nod_flag,key_length;
441
uchar *key_pos,*pos, *after_key= NULL;
426
443
MI_KEY_PARAM s_temp;
428
445
if (info->s->keyinfo+info->lastinx == keyinfo)
458
475
if (!(*keyinfo->get_key)(keyinfo,nod_flag,&key_pos,key_buff))
461
t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,(unsigned char *) 0,
462
(unsigned char*) 0, (unsigned char*) 0,
478
t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,(uchar *) 0,
479
(uchar*) 0, (uchar*) 0,
463
480
key_buff, &s_temp);
464
481
length=(uint) ((buff+a_length)-key_pos);
465
memcpy(info->buff+key_ref_length+t_length, key_pos, length);
482
memcpy((uchar*) info->buff+key_ref_length+t_length,(uchar*) key_pos,
466
484
(*keyinfo->store_key)(keyinfo,info->buff+key_ref_length,&s_temp);
467
485
mi_putint(info->buff,length+t_length+key_ref_length,nod_flag);
480
498
after_key will contain the position to where the next key starts
483
unsigned char *_mi_find_half_pos(uint32_t nod_flag, MI_KEYDEF *keyinfo, unsigned char *page,
484
unsigned char *key, uint32_t *return_key_length,
485
unsigned char **after_key)
501
uchar *_mi_find_half_pos(uint nod_flag, MI_KEYDEF *keyinfo, uchar *page,
502
uchar *key, uint *return_key_length,
487
uint32_t keys,length,key_ref_length;
488
unsigned char *end,*lastpos;
505
uint keys,length,key_ref_length;
490
508
key_ref_length=2+nod_flag;
491
509
length=mi_getint(page)-key_ref_length;
523
541
key will contain the last key
526
static unsigned char *_mi_find_last_pos(MI_KEYDEF *keyinfo, unsigned char *page,
527
unsigned char *key, uint32_t *return_key_length,
528
unsigned char **after_key)
544
static uchar *_mi_find_last_pos(MI_KEYDEF *keyinfo, uchar *page,
545
uchar *key, uint *return_key_length,
532
uint32_t last_length= 0;
533
uint32_t key_ref_length;
534
unsigned char *end, *lastpos, *prevpos= NULL;
535
unsigned char key_buff[MI_MAX_KEY_BUFF];
552
uchar *end, *lastpos, *prevpos= NULL;
553
uchar key_buff[MI_MAX_KEY_BUFF];
537
555
key_ref_length=2;
538
556
length=mi_getint(page)-key_ref_length;
575
593
/* returns 0 if balance was done */
577
595
static int _mi_balance_page(register MI_INFO *info, MI_KEYDEF *keyinfo,
578
unsigned char *key, unsigned char *curr_buff, unsigned char *father_buff,
579
unsigned char *father_key_pos, internal::my_off_t father_page)
596
uchar *key, uchar *curr_buff, uchar *father_buff,
597
uchar *father_key_pos, my_off_t father_page)
582
uint32_t k_length,father_length,father_keylength,nod_flag,curr_keylength,
600
uint k_length,father_length,father_keylength,nod_flag,curr_keylength,
583
601
right_length,left_length,new_right_length,new_left_length,extra_length,
585
unsigned char *pos,*buff,*extra_buff;
586
internal::my_off_t next_page,new_pos;
587
unsigned char tmp_part_key[MI_MAX_KEY_BUFF];
603
uchar *pos,*buff,*extra_buff;
604
my_off_t next_page,new_pos;
605
uchar tmp_part_key[MI_MAX_KEY_BUFF];
589
607
k_length=keyinfo->keylength;
590
608
father_length=mi_getint(father_buff);
631
649
if (left_length < new_left_length)
632
650
{ /* Move keys buff -> leaf */
633
651
pos=curr_buff+left_length;
634
memcpy(pos, father_key_pos, k_length);
635
length= new_left_length - left_length - k_length;
636
memcpy(pos+k_length, buff+2, length);
652
memcpy((uchar*) pos,(uchar*) father_key_pos, (size_t) k_length);
653
memcpy((uchar*) pos+k_length, (uchar*) buff+2,
654
(size_t) (length=new_left_length - left_length - k_length));
637
655
pos=buff+2+length;
638
memcpy(father_key_pos, pos, k_length);
639
memmove(buff+2, pos+k_length, new_right_length);
656
memcpy((uchar*) father_key_pos,(uchar*) pos,(size_t) k_length);
657
memcpy((uchar*) buff+2,(uchar*) pos+k_length,new_right_length);
642
660
{ /* Move keys -> buff */
644
internal::bmove_upp((unsigned char*) buff+new_right_length,(unsigned char*) buff+right_length,
662
bmove_upp((uchar*) buff+new_right_length,(uchar*) buff+right_length,
646
664
length=new_right_length-right_length-k_length;
647
memcpy(buff+2+length,father_key_pos, k_length);
665
memcpy((uchar*) buff+2+length,father_key_pos,(size_t) k_length);
648
666
pos=curr_buff+new_left_length;
649
memcpy(father_key_pos, pos, k_length);
650
memcpy(buff+2, pos+k_length, length);
667
memcpy((uchar*) father_key_pos,(uchar*) pos,(size_t) k_length);
668
memcpy((uchar*) buff+2,(uchar*) pos+k_length,(size_t) length);
653
671
if (_mi_write_keypage(info,keyinfo,next_page,DFLT_INIT_HITS,info->buff) ||
671
689
/* move first largest keys to new page */
672
690
pos=buff+right_length-extra_length;
673
memcpy(extra_buff+2, pos, extra_length);
691
memcpy((uchar*) extra_buff+2,pos,(size_t) extra_length);
674
692
/* Save new parting key */
675
693
memcpy(tmp_part_key, pos-k_length,k_length);
676
694
/* Make place for new keys */
677
internal::bmove_upp((unsigned char*) buff+new_right_length,(unsigned char*) pos-k_length,
695
bmove_upp((uchar*) buff+new_right_length,(uchar*) pos-k_length,
678
696
right_length-extra_length-k_length-2);
679
697
/* Copy keys from left page */
680
698
pos= curr_buff+new_left_length;
681
length= left_length - new_left_length - k_length;
682
memcpy(buff+2, pos+k_length, length);
699
memcpy((uchar*) buff+2,(uchar*) pos+k_length,
700
(size_t) (length=left_length-new_left_length-k_length));
683
701
/* Copy old parting key */
684
memcpy(buff+2+length, father_key_pos, k_length);
702
memcpy((uchar*) buff+2+length,father_key_pos,(size_t) k_length);
686
704
/* Move new parting keys up to caller */
687
memcpy((right ? key : father_key_pos), pos, k_length);
688
memcpy((right ? father_key_pos : key), tmp_part_key, k_length);
705
memcpy((uchar*) (right ? key : father_key_pos),pos,(size_t) k_length);
706
memcpy((uchar*) (right ? father_key_pos : key),tmp_part_key, k_length);
690
708
if ((new_pos=_mi_new(info,keyinfo,DFLT_INIT_HITS)) == HA_OFFSET_ERROR)
727
745
/* typeof(_mi_keys_compare)=qsort_cmp2 */
729
static int keys_compare(bulk_insert_param *param, unsigned char *key1, unsigned char *key2)
747
static int keys_compare(bulk_insert_param *param, uchar *key1, uchar *key2)
731
uint32_t not_used[2];
732
750
return ha_key_cmp(param->info->s->keyinfo[param->keynr].seg,
733
751
key1, key2, USE_WHOLE_KEY, SEARCH_SAME,
738
static int keys_free(unsigned char *key, TREE_FREE mode, bulk_insert_param *param)
756
static int keys_free(uchar *key, TREE_FREE mode, bulk_insert_param *param)
741
759
Probably I can use info->lastkey here, but I'm not sure,
742
760
and to be safe I'd better use local lastkey.
744
unsigned char lastkey[MI_MAX_KEY_BUFF];
762
uchar lastkey[MI_MAX_KEY_BUFF];
746
764
MI_KEYDEF *keyinfo;
750
768
if (param->info->s->concurrent_insert)
770
rw_wrlock(¶m->info->s->key_root_lock[param->keynr]);
752
771
param->info->s->keyinfo[param->keynr].version++;
759
778
return _mi_ck_write_btree(param->info,param->keynr,lastkey,
760
779
keylen - param->info->s->rec_reflength);
781
if (param->info->s->concurrent_insert)
782
rw_unlock(¶m->info->s->key_root_lock[param->keynr]);
768
int mi_init_bulk_insert(MI_INFO *info, uint32_t cache_size, ha_rows rows)
789
int mi_init_bulk_insert(MI_INFO *info, ulong cache_size, ha_rows rows)
770
791
MYISAM_SHARE *share=info->s;
771
792
MI_KEYDEF *key=share->keyinfo;
772
793
bulk_insert_param *params;
773
uint32_t i, num_keys, total_keylength;
794
uint i, num_keys, total_keylength;
774
795
uint64_t key_map;
776
797
assert(!info->bulk_insert &&