18
18
#include "myisamdef.h"
20
#include <mystrings/m_string.h>
21
#include <drizzled/util/test.h>
24
20
#define MAX_POINTER_LENGTH 8
26
22
/* Functions declared in this file */
28
24
static int w_search(MI_INFO *info,MI_KEYDEF *keyinfo,
29
uint32_t comp_flag, unsigned char *key,
30
uint32_t key_length, my_off_t pos, unsigned char *father_buff,
31
unsigned char *father_keypos, my_off_t father_page,
33
static int _mi_balance_page(MI_INFO *info,MI_KEYDEF *keyinfo,unsigned char *key,
34
unsigned char *curr_buff,unsigned char *father_buff,
35
unsigned char *father_keypos,my_off_t father_page);
36
static unsigned char *_mi_find_last_pos(MI_KEYDEF *keyinfo, unsigned char *page,
37
unsigned char *key, uint32_t *return_key_length,
38
unsigned char **after_key);
39
int _mi_ck_write_tree(register MI_INFO *info, uint32_t keynr,unsigned char *key,
41
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,
44
40
/* Write new record to database */
46
int mi_write(MI_INFO *info, unsigned char *record)
42
int mi_write(MI_INFO *info, uchar *record)
48
44
MYISAM_SHARE *share=info->s;
53
bool lock_tree= share->concurrent_insert;
49
my_bool lock_tree= share->concurrent_insert;
50
DBUG_ENTER("mi_write");
51
DBUG_PRINT("enter",("isam: %d data: %d",info->s->kfile,info->dfile));
53
DBUG_EXECUTE_IF("myisam_pretend_crashed_table_on_usage",
54
mi_print_error(info->s, HA_ERR_CRASHED);
55
DBUG_RETURN(my_errno= HA_ERR_CRASHED););
55
56
if (share->options & HA_OPTION_READ_ONLY_DATA)
57
return(my_errno=EACCES);
58
DBUG_RETURN(my_errno=EACCES);
59
60
if (_mi_readinfo(info,F_WRLCK,1))
61
DBUG_RETURN(my_errno);
62
#if !defined(NO_LOCKING) && defined(USE_RECORD_LOCK)
63
if (!info->locked && my_lock(info->dfile,F_WRLCK,0L,F_TO_EOF,
64
MYF(MY_SEEK_NOT_DONE) | info->lock_wait))
61
67
filepos= ((share->state.dellink != HA_OFFSET_ERROR &&
62
68
!info->append_insert_at_end) ?
63
69
share->state.dellink :
166
180
if (mi_is_key_active(share->state.key_map, i))
168
bool local_lock_tree= (lock_tree &&
182
my_bool local_lock_tree= (lock_tree &&
169
183
!(info->bulk_insert &&
170
184
is_tree_inited(&info->bulk_insert[i])));
171
185
if (local_lock_tree)
172
pthread_rwlock_wrlock(&share->key_root_lock[i]);
186
rw_wrlock(&share->key_root_lock[i]);
174
uint32_t key_length=_mi_make_key(info,i,buff,record,filepos);
188
uint key_length=_mi_make_key(info,i,buff,record,filepos);
175
189
if (_mi_ck_delete(info,i,buff,key_length))
177
191
if (local_lock_tree)
178
pthread_rwlock_unlock(&share->key_root_lock[i]);
192
rw_unlock(&share->key_root_lock[i]);
182
196
if (local_lock_tree)
183
pthread_rwlock_unlock(&share->key_root_lock[i]);
197
rw_unlock(&share->key_root_lock[i]);
193
207
my_errno=save_errno;
195
209
save_errno=my_errno;
196
_mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE);
197
return(my_errno=save_errno);
210
myisam_log_record(MI_LOG_WRITE,info,record,filepos,my_errno);
211
VOID(_mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE));
212
DBUG_RETURN(my_errno=save_errno);
201
216
/* Write one key to btree */
203
int _mi_ck_write(MI_INFO *info, uint32_t keynr, unsigned char *key, uint32_t key_length)
218
int _mi_ck_write(MI_INFO *info, uint keynr, uchar *key, uint key_length)
220
DBUG_ENTER("_mi_ck_write");
205
222
if (info->bulk_insert && is_tree_inited(&info->bulk_insert[keynr]))
207
return(_mi_ck_write_tree(info, keynr, key, key_length));
224
DBUG_RETURN(_mi_ck_write_tree(info, keynr, key, key_length));
211
return(_mi_ck_write_btree(info, keynr, key, key_length));
228
DBUG_RETURN(_mi_ck_write_btree(info, keynr, key, key_length));
213
230
} /* _mi_ck_write */
217
234
* Normal insert code *
218
235
**********************************************************************/
220
int _mi_ck_write_btree(register MI_INFO *info, uint32_t keynr, unsigned char *key,
237
int _mi_ck_write_btree(register MI_INFO *info, uint keynr, uchar *key,
225
242
MI_KEYDEF *keyinfo=info->s->keyinfo+keynr;
226
243
my_off_t *root=&info->s->state.key_root[keynr];
244
DBUG_ENTER("_mi_ck_write_btree");
228
246
if (keyinfo->flag & HA_SORT_ALLOWS_SAME)
229
247
comp_flag=SEARCH_BIGGER; /* Put after same key */
239
257
error=_mi_ck_real_write_btree(info, keyinfo, key, key_length,
240
258
root, comp_flag);
259
if (info->ft1_to_ft2)
262
error= _mi_ft_convert_to_ft2(info, keynr, key);
263
delete_dynamic(info->ft1_to_ft2);
264
my_free((uchar*)info->ft1_to_ft2, MYF(0));
242
268
} /* _mi_ck_write_btree */
244
270
int _mi_ck_real_write_btree(MI_INFO *info, MI_KEYDEF *keyinfo,
245
unsigned char *key, uint32_t key_length, my_off_t *root, uint32_t comp_flag)
271
uchar *key, uint key_length, my_off_t *root, uint comp_flag)
274
DBUG_ENTER("_mi_ck_real_write_btree");
248
275
/* key_length parameter is used only if comp_flag is SEARCH_FIND */
249
276
if (*root == HA_OFFSET_ERROR ||
250
277
(error=w_search(info, keyinfo, comp_flag, key, key_length,
251
*root, (unsigned char *) 0, (unsigned char*) 0,
278
*root, (uchar *) 0, (uchar*) 0,
252
279
(my_off_t) 0, 1)) > 0)
253
280
error=_mi_enlarge_root(info,keyinfo,key,root);
255
282
} /* _mi_ck_real_write_btree */
258
285
/* Make a new root with key as only pointer */
260
int _mi_enlarge_root(MI_INFO *info, MI_KEYDEF *keyinfo, unsigned char *key,
287
int _mi_enlarge_root(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *key,
263
uint32_t t_length,nod_flag;
290
uint t_length,nod_flag;
264
291
MI_KEY_PARAM s_temp;
265
292
MYISAM_SHARE *share=info->s;
293
DBUG_ENTER("_mi_enlarge_root");
267
295
nod_flag= (*root != HA_OFFSET_ERROR) ? share->base.key_reflength : 0;
268
296
_mi_kpointer(info,info->buff+2,*root); /* if nod */
269
t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,(unsigned char*) 0,
270
(unsigned char*) 0, (unsigned char*) 0, key,&s_temp);
297
t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,(uchar*) 0,
298
(uchar*) 0, (uchar*) 0, key,&s_temp);
271
299
mi_putint(info->buff,t_length+2+nod_flag,nod_flag);
272
300
(*keyinfo->store_key)(keyinfo,info->buff+2+nod_flag,&s_temp);
273
301
info->buff_used=info->page_changed=1; /* info->buff is used */
274
302
if ((*root= _mi_new(info,keyinfo,DFLT_INIT_HITS)) == HA_OFFSET_ERROR ||
275
303
_mi_write_keypage(info,keyinfo,*root,DFLT_INIT_HITS,info->buff))
278
306
} /* _mi_enlarge_root */
288
316
static int w_search(register MI_INFO *info, register MI_KEYDEF *keyinfo,
289
uint32_t comp_flag, unsigned char *key, uint32_t key_length, my_off_t page,
290
unsigned char *father_buff, unsigned char *father_keypos,
291
my_off_t father_page, bool insert_last)
317
uint comp_flag, uchar *key, uint key_length, my_off_t page,
318
uchar *father_buff, uchar *father_keypos,
319
my_off_t father_page, my_bool insert_last)
294
uint32_t nod_flag, search_key_length;
295
unsigned char *temp_buff,*keypos;
296
unsigned char keybuff[MI_MAX_KEY_BUFF];
322
uint nod_flag, search_key_length;
323
uchar *temp_buff,*keypos;
324
uchar keybuff[MI_MAX_KEY_BUFF];
325
my_bool was_last_key;
298
326
my_off_t next_page, dupp_key_pos;
327
DBUG_ENTER("w_search");
328
DBUG_PRINT("enter",("page: %ld", (long) page));
300
330
search_key_length= (comp_flag & SEARCH_FIND) ? key_length : USE_WHOLE_KEY;
301
if (!(temp_buff= (unsigned char*) malloc(keyinfo->block_length+
331
if (!(temp_buff= (uchar*) my_alloca((uint) keyinfo->block_length+
304
334
if (!_mi_fetch_keypage(info,keyinfo,page,DFLT_INIT_HITS,temp_buff,0))
374
405
int _mi_insert(register MI_INFO *info, register MI_KEYDEF *keyinfo,
375
unsigned char *key, unsigned char *anc_buff, unsigned char *key_pos, unsigned char *key_buff,
376
unsigned char *father_buff, unsigned char *father_key_pos, my_off_t father_page,
406
uchar *key, uchar *anc_buff, uchar *key_pos, uchar *key_buff,
407
uchar *father_buff, uchar *father_key_pos, my_off_t father_page,
379
uint32_t a_length,nod_flag;
410
uint a_length,nod_flag;
381
unsigned char *endpos, *prev_key;
412
uchar *endpos, *prev_key;
382
413
MI_KEY_PARAM s_temp;
414
DBUG_ENTER("_mi_insert");
415
DBUG_PRINT("enter",("key_pos: 0x%lx", (long) key_pos));
416
DBUG_EXECUTE("key",_mi_print_key(DBUG_FILE,keyinfo->seg,key,USE_WHOLE_KEY););
384
418
nod_flag=mi_test_if_nod(anc_buff);
385
419
a_length=mi_getint(anc_buff);
386
420
endpos= anc_buff+ a_length;
387
prev_key=(key_pos == anc_buff+2+nod_flag ? (unsigned char*) 0 : key_buff);
421
prev_key=(key_pos == anc_buff+2+nod_flag ? (uchar*) 0 : key_buff);
388
422
t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,
389
(key_pos == endpos ? (unsigned char*) 0 : key_pos),
423
(key_pos == endpos ? (uchar*) 0 : key_pos),
390
424
prev_key, prev_key,
427
if (key_pos != anc_buff+2+nod_flag && (keyinfo->flag &
428
(HA_BINARY_PACK_KEY | HA_PACK_KEY)))
430
DBUG_DUMP("prev_key",(uchar*) key_buff,_mi_keylength(keyinfo,key_buff));
432
if (keyinfo->flag & HA_PACK_KEY)
434
DBUG_PRINT("test",("t_length: %d ref_len: %d",
435
t_length,s_temp.ref_length));
436
DBUG_PRINT("test",("n_ref_len: %d n_length: %d key_pos: 0x%lx",
437
s_temp.n_ref_length,s_temp.n_length, (long) s_temp.key));
393
440
if (t_length > 0)
395
442
if (t_length >= keyinfo->maxlength*2+MAX_POINTER_LENGTH)
397
444
mi_print_error(info->s, HA_ERR_CRASHED);
398
445
my_errno=HA_ERR_CRASHED;
401
bmove_upp((unsigned char*) endpos+t_length,(unsigned char*) endpos,(uint) (endpos-key_pos));
448
bmove_upp((uchar*) endpos+t_length,(uchar*) endpos,(uint) (endpos-key_pos));
407
454
mi_print_error(info->s, HA_ERR_CRASHED);
408
455
my_errno=HA_ERR_CRASHED;
411
memmove(key_pos, key_pos - t_length, endpos - key_pos + t_length);
458
bmove(key_pos,key_pos-t_length,(uint) (endpos-key_pos)+t_length);
413
460
(*keyinfo->store_key)(keyinfo,key_pos,&s_temp);
414
461
a_length+=t_length;
415
462
mi_putint(anc_buff,a_length,nod_flag);
416
463
if (a_length <= keyinfo->block_length)
418
return(0); /* There is room on page */
465
DBUG_RETURN(0); /* There is room on page */
420
467
/* Page is full */
423
470
if (!(keyinfo->flag & (HA_VAR_LENGTH_KEY | HA_BINARY_PACK_KEY)) &&
424
471
father_buff && !insert_last)
425
return(_mi_balance_page(info,keyinfo,key,anc_buff,father_buff,
472
DBUG_RETURN(_mi_balance_page(info,keyinfo,key,anc_buff,father_buff,
426
473
father_key_pos,father_page));
427
return(_mi_split_page(info,keyinfo,key,anc_buff,key_buff, insert_last));
474
DBUG_RETURN(_mi_split_page(info,keyinfo,key,anc_buff,key_buff, insert_last));
428
475
} /* _mi_insert */
431
478
/* split a full page in two and assign emerging item to key */
433
480
int _mi_split_page(register MI_INFO *info, register MI_KEYDEF *keyinfo,
434
unsigned char *key, unsigned char *buff, unsigned char *key_buff,
435
bool insert_last_key)
481
uchar *key, uchar *buff, uchar *key_buff,
482
my_bool insert_last_key)
437
uint32_t length,a_length,key_ref_length,t_length,nod_flag,key_length;
438
unsigned char *key_pos,*pos, *after_key= NULL;
484
uint length,a_length,key_ref_length,t_length,nod_flag,key_length;
485
uchar *key_pos,*pos, *after_key= NULL;
439
486
my_off_t new_pos;
440
487
MI_KEY_PARAM s_temp;
488
DBUG_ENTER("mi_split_page");
489
DBUG_DUMP("buff",(uchar*) buff,mi_getint(buff));
442
491
if (info->s->keyinfo+info->lastinx == keyinfo)
443
492
info->page_changed=1; /* Info->buff is used */
459
508
key_pos=after_key;
511
DBUG_PRINT("test",("Splitting nod"));
462
512
pos=key_pos-nod_flag;
463
memcpy(info->buff + 2, pos, nod_flag);
513
memcpy((uchar*) info->buff+2,(uchar*) pos,(size_t) nod_flag);
466
516
/* Move middle item to key and pointer to new page */
467
517
if ((new_pos=_mi_new(info,keyinfo,DFLT_INIT_HITS)) == HA_OFFSET_ERROR)
469
519
_mi_kpointer(info,_mi_move_key(keyinfo,key,key_buff),new_pos);
471
521
/* Store new page */
472
522
if (!(*keyinfo->get_key)(keyinfo,nod_flag,&key_pos,key_buff))
475
t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,(unsigned char *) 0,
476
(unsigned char*) 0, (unsigned char*) 0,
525
t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,(uchar *) 0,
526
(uchar*) 0, (uchar*) 0,
477
527
key_buff, &s_temp);
478
528
length=(uint) ((buff+a_length)-key_pos);
479
memcpy(info->buff+key_ref_length+t_length, key_pos, length);
529
memcpy((uchar*) info->buff+key_ref_length+t_length,(uchar*) key_pos,
480
531
(*keyinfo->store_key)(keyinfo,info->buff+key_ref_length,&s_temp);
481
532
mi_putint(info->buff,length+t_length+key_ref_length,nod_flag);
483
534
if (_mi_write_keypage(info,keyinfo,new_pos,DFLT_INIT_HITS,info->buff))
485
return(2); /* Middle key up */
536
DBUG_DUMP("key",(uchar*) key,_mi_keylength(keyinfo,key));
537
DBUG_RETURN(2); /* Middle key up */
486
538
} /* _mi_split_page */
494
546
after_key will contain the position to where the next key starts
497
unsigned char *_mi_find_half_pos(uint32_t nod_flag, MI_KEYDEF *keyinfo, unsigned char *page,
498
unsigned char *key, uint32_t *return_key_length,
499
unsigned char **after_key)
549
uchar *_mi_find_half_pos(uint nod_flag, MI_KEYDEF *keyinfo, uchar *page,
550
uchar *key, uint *return_key_length,
501
uint32_t keys,length,key_ref_length;
502
unsigned char *end,*lastpos;
553
uint keys,length,key_ref_length;
555
DBUG_ENTER("_mi_find_half_pos");
504
557
key_ref_length=2+nod_flag;
505
558
length=mi_getint(page)-key_ref_length;
537
592
key will contain the last key
540
static unsigned char *_mi_find_last_pos(MI_KEYDEF *keyinfo, unsigned char *page,
541
unsigned char *key, uint32_t *return_key_length,
542
unsigned char **after_key)
595
static uchar *_mi_find_last_pos(MI_KEYDEF *keyinfo, uchar *page,
596
uchar *key, uint *return_key_length,
546
uint32_t last_length= 0;
547
uint32_t key_ref_length;
548
unsigned char *end, *lastpos, *prevpos= NULL;
549
unsigned char key_buff[MI_MAX_KEY_BUFF];
599
uint keys, length, last_length, key_ref_length;
600
uchar *end, *lastpos, *prevpos= NULL;
601
uchar key_buff[MI_MAX_KEY_BUFF];
602
DBUG_ENTER("_mi_find_last_pos");
551
604
key_ref_length=2;
552
605
length=mi_getint(page)-key_ref_length;
589
644
/* returns 0 if balance was done */
591
646
static int _mi_balance_page(register MI_INFO *info, MI_KEYDEF *keyinfo,
592
unsigned char *key, unsigned char *curr_buff, unsigned char *father_buff,
593
unsigned char *father_key_pos, my_off_t father_page)
647
uchar *key, uchar *curr_buff, uchar *father_buff,
648
uchar *father_key_pos, my_off_t father_page)
596
uint32_t k_length,father_length,father_keylength,nod_flag,curr_keylength,
651
uint k_length,father_length,father_keylength,nod_flag,curr_keylength,
597
652
right_length,left_length,new_right_length,new_left_length,extra_length,
599
unsigned char *pos,*buff,*extra_buff;
654
uchar *pos,*buff,*extra_buff;
600
655
my_off_t next_page,new_pos;
601
unsigned char tmp_part_key[MI_MAX_KEY_BUFF];
656
uchar tmp_part_key[MI_MAX_KEY_BUFF];
657
DBUG_ENTER("_mi_balance_page");
603
659
k_length=keyinfo->keylength;
604
660
father_length=mi_getint(father_buff);
623
680
next_page= _mi_kpos(info->s->base.key_reflength,father_key_pos);
624
681
/* Fix that curr_buff is to left */
625
682
buff=curr_buff; curr_buff=info->buff;
683
DBUG_PRINT("test",("use left page: %lu", (ulong) next_page));
626
684
} /* father_key_pos ptr to parting key */
628
686
if (!_mi_fetch_keypage(info,keyinfo,next_page,DFLT_INIT_HITS,info->buff,0))
688
DBUG_DUMP("next",(uchar*) info->buff,mi_getint(info->buff));
631
690
/* Test if there is room to share keys */
645
704
if (left_length < new_left_length)
646
705
{ /* Move keys buff -> leaf */
647
706
pos=curr_buff+left_length;
648
memcpy(pos, father_key_pos, k_length);
649
length= new_left_length - left_length - k_length;
650
memcpy(pos+k_length, buff+2, length);
707
memcpy((uchar*) pos,(uchar*) father_key_pos, (size_t) k_length);
708
memcpy((uchar*) pos+k_length, (uchar*) buff+2,
709
(size_t) (length=new_left_length - left_length - k_length));
651
710
pos=buff+2+length;
652
memcpy(father_key_pos, pos, k_length);
653
memmove(buff+2, pos+k_length, new_right_length);
711
memcpy((uchar*) father_key_pos,(uchar*) pos,(size_t) k_length);
712
bmove((uchar*) buff+2,(uchar*) pos+k_length,new_right_length);
656
715
{ /* Move keys -> buff */
658
bmove_upp((unsigned char*) buff+new_right_length,(unsigned char*) buff+right_length,
717
bmove_upp((uchar*) buff+new_right_length,(uchar*) buff+right_length,
660
719
length=new_right_length-right_length-k_length;
661
memcpy(buff+2+length,father_key_pos, k_length);
720
memcpy((uchar*) buff+2+length,father_key_pos,(size_t) k_length);
662
721
pos=curr_buff+new_left_length;
663
memcpy(father_key_pos, pos, k_length);
664
memcpy(buff+2, pos+k_length, length);
722
memcpy((uchar*) father_key_pos,(uchar*) pos,(size_t) k_length);
723
memcpy((uchar*) buff+2,(uchar*) pos+k_length,(size_t) length);
667
726
if (_mi_write_keypage(info,keyinfo,next_page,DFLT_INIT_HITS,info->buff) ||
668
727
_mi_write_keypage(info,keyinfo,father_page,DFLT_INIT_HITS,father_buff))
673
732
/* curr_buff[] and buff[] are full, lets split and make new nod */
678
737
new_left_length-=curr_keylength;
679
738
extra_length=nod_flag+left_length+right_length-
680
739
new_left_length-new_right_length-curr_keylength;
740
DBUG_PRINT("info",("left_length: %d right_length: %d new_left_length: %d new_right_length: %d extra_length: %d",
741
left_length, right_length,
742
new_left_length, new_right_length,
681
744
mi_putint(curr_buff,new_left_length,nod_flag);
682
745
mi_putint(buff,new_right_length,nod_flag);
683
746
mi_putint(extra_buff,extra_length+2,nod_flag);
685
748
/* move first largest keys to new page */
686
749
pos=buff+right_length-extra_length;
687
memcpy(extra_buff+2, pos, extra_length);
750
memcpy((uchar*) extra_buff+2,pos,(size_t) extra_length);
688
751
/* Save new parting key */
689
752
memcpy(tmp_part_key, pos-k_length,k_length);
690
753
/* Make place for new keys */
691
bmove_upp((unsigned char*) buff+new_right_length,(unsigned char*) pos-k_length,
754
bmove_upp((uchar*) buff+new_right_length,(uchar*) pos-k_length,
692
755
right_length-extra_length-k_length-2);
693
756
/* Copy keys from left page */
694
757
pos= curr_buff+new_left_length;
695
length= left_length - new_left_length - k_length;
696
memcpy(buff+2, pos+k_length, length);
758
memcpy((uchar*) buff+2,(uchar*) pos+k_length,
759
(size_t) (length=left_length-new_left_length-k_length));
697
760
/* Copy old parting key */
698
memcpy(buff+2+length, father_key_pos, k_length);
761
memcpy((uchar*) buff+2+length,father_key_pos,(size_t) k_length);
700
763
/* Move new parting keys up to caller */
701
memcpy((right ? key : father_key_pos), pos, k_length);
702
memcpy((right ? father_key_pos : key), tmp_part_key, k_length);
764
memcpy((uchar*) (right ? key : father_key_pos),pos,(size_t) k_length);
765
memcpy((uchar*) (right ? father_key_pos : key),tmp_part_key, k_length);
704
767
if ((new_pos=_mi_new(info,keyinfo,DFLT_INIT_HITS)) == HA_OFFSET_ERROR)
726
789
} bulk_insert_param;
728
int _mi_ck_write_tree(register MI_INFO *info, uint32_t keynr, unsigned char *key,
791
int _mi_ck_write_tree(register MI_INFO *info, uint keynr, uchar *key,
795
DBUG_ENTER("_mi_ck_write_tree");
733
797
error= tree_insert(&info->bulk_insert[keynr], key,
734
798
key_length + info->s->rec_reflength,
735
799
info->bulk_insert[keynr].custom_arg) ? 0 : HA_ERR_OUT_OF_MEM ;
738
802
} /* _mi_ck_write_tree */
741
805
/* typeof(_mi_keys_compare)=qsort_cmp2 */
743
static int keys_compare(bulk_insert_param *param, unsigned char *key1, unsigned char *key2)
807
static int keys_compare(bulk_insert_param *param, uchar *key1, uchar *key2)
745
uint32_t not_used[2];
746
810
return ha_key_cmp(param->info->s->keyinfo[param->keynr].seg,
747
811
key1, key2, USE_WHOLE_KEY, SEARCH_SAME,
752
static int keys_free(unsigned char *key, TREE_FREE mode, bulk_insert_param *param)
816
static int keys_free(uchar *key, TREE_FREE mode, bulk_insert_param *param)
755
819
Probably I can use info->lastkey here, but I'm not sure,
756
820
and to be safe I'd better use local lastkey.
758
unsigned char lastkey[MI_MAX_KEY_BUFF];
822
uchar lastkey[MI_MAX_KEY_BUFF];
760
824
MI_KEYDEF *keyinfo;
764
828
if (param->info->s->concurrent_insert)
766
pthread_rwlock_wrlock(¶m->info->s->key_root_lock[param->keynr]);
830
rw_wrlock(¶m->info->s->key_root_lock[param->keynr]);
767
831
param->info->s->keyinfo[param->keynr].version++;
775
839
keylen - param->info->s->rec_reflength);
777
841
if (param->info->s->concurrent_insert)
778
pthread_rwlock_unlock(¶m->info->s->key_root_lock[param->keynr]);
842
rw_unlock(¶m->info->s->key_root_lock[param->keynr]);
785
int mi_init_bulk_insert(MI_INFO *info, uint32_t cache_size, ha_rows rows)
849
int mi_init_bulk_insert(MI_INFO *info, ulong cache_size, ha_rows rows)
787
851
MYISAM_SHARE *share=info->s;
788
852
MI_KEYDEF *key=share->keyinfo;
789
853
bulk_insert_param *params;
790
uint32_t i, num_keys, total_keylength;
854
uint i, num_keys, total_keylength;
856
DBUG_ENTER("_mi_init_bulk_insert");
857
DBUG_PRINT("enter",("cache_size: %lu", cache_size));
793
assert(!info->bulk_insert &&
859
DBUG_ASSERT(!info->bulk_insert &&
794
860
(!rows || rows >= MI_MIN_ROWS_TO_USE_BULK_INSERT));
796
862
mi_clear_all_keys_active(key_map);
808
874
if (num_keys==0 ||
809
875
num_keys * MI_MIN_SIZE_BULK_INSERT_TREE > cache_size)
812
878
if (rows && rows*total_keylength < cache_size)
813
cache_size= (uint32_t)rows;
879
cache_size= (ulong)rows;
815
881
cache_size/=total_keylength*16;
817
883
info->bulk_insert=(TREE *)
818
malloc((sizeof(TREE)*share->base.keys+
819
sizeof(bulk_insert_param)*num_keys));
884
my_malloc((sizeof(TREE)*share->base.keys+
885
sizeof(bulk_insert_param)*num_keys),MYF(0));
821
887
if (!info->bulk_insert)
822
return(HA_ERR_OUT_OF_MEM);
888
DBUG_RETURN(HA_ERR_OUT_OF_MEM);
824
890
params=(bulk_insert_param *)(info->bulk_insert+share->base.keys);
825
891
for (i=0 ; i < share->base.keys ; i++)