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;
55
51
if (share->options & HA_OPTION_READ_ONLY_DATA)
171
168
if (mi_is_key_active(share->state.key_map, i))
173
bool local_lock_tree= (lock_tree &&
170
my_bool local_lock_tree= (lock_tree &&
174
171
!(info->bulk_insert &&
175
172
is_tree_inited(&info->bulk_insert[i])));
176
173
if (local_lock_tree)
177
pthread_rwlock_wrlock(&share->key_root_lock[i]);
174
rw_wrlock(&share->key_root_lock[i]);
179
uint32_t key_length=_mi_make_key(info,i,buff,record,filepos);
176
uint key_length=_mi_make_key(info,i,buff,record,filepos);
180
177
if (_mi_ck_delete(info,i,buff,key_length))
182
179
if (local_lock_tree)
183
pthread_rwlock_unlock(&share->key_root_lock[i]);
180
rw_unlock(&share->key_root_lock[i]);
187
184
if (local_lock_tree)
188
pthread_rwlock_unlock(&share->key_root_lock[i]);
185
rw_unlock(&share->key_root_lock[i]);
198
195
my_errno=save_errno;
200
197
save_errno=my_errno;
201
_mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE);
198
myisam_log_record(MI_LOG_WRITE,info,record,filepos,my_errno);
199
VOID(_mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE));
202
200
return(my_errno=save_errno);
206
204
/* Write one key to btree */
208
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)
210
208
if (info->bulk_insert && is_tree_inited(&info->bulk_insert[keynr]))
222
220
* Normal insert code *
223
221
**********************************************************************/
225
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,
230
228
MI_KEYDEF *keyinfo=info->s->keyinfo+keynr;
231
229
my_off_t *root=&info->s->state.key_root[keynr];
247
245
} /* _mi_ck_write_btree */
249
247
int _mi_ck_real_write_btree(MI_INFO *info, MI_KEYDEF *keyinfo,
250
unsigned char *key, uint32_t key_length, my_off_t *root, uint32_t comp_flag)
248
uchar *key, uint key_length, my_off_t *root, uint comp_flag)
253
251
/* key_length parameter is used only if comp_flag is SEARCH_FIND */
254
252
if (*root == HA_OFFSET_ERROR ||
255
253
(error=w_search(info, keyinfo, comp_flag, key, key_length,
256
*root, (unsigned char *) 0, (unsigned char*) 0,
254
*root, (uchar *) 0, (uchar*) 0,
257
255
(my_off_t) 0, 1)) > 0)
258
256
error=_mi_enlarge_root(info,keyinfo,key,root);
263
261
/* Make a new root with key as only pointer */
265
int _mi_enlarge_root(MI_INFO *info, MI_KEYDEF *keyinfo, unsigned char *key,
263
int _mi_enlarge_root(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *key,
268
uint32_t t_length,nod_flag;
266
uint t_length,nod_flag;
269
267
MI_KEY_PARAM s_temp;
270
268
MYISAM_SHARE *share=info->s;
272
270
nod_flag= (*root != HA_OFFSET_ERROR) ? share->base.key_reflength : 0;
273
271
_mi_kpointer(info,info->buff+2,*root); /* if nod */
274
t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,(unsigned char*) 0,
275
(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);
276
274
mi_putint(info->buff,t_length+2+nod_flag,nod_flag);
277
275
(*keyinfo->store_key)(keyinfo,info->buff+2+nod_flag,&s_temp);
278
276
info->buff_used=info->page_changed=1; /* info->buff is used */
293
291
static int w_search(register MI_INFO *info, register MI_KEYDEF *keyinfo,
294
uint32_t comp_flag, unsigned char *key, uint32_t key_length, my_off_t page,
295
unsigned char *father_buff, unsigned char *father_keypos,
296
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)
299
uint32_t nod_flag, search_key_length;
300
unsigned char *temp_buff,*keypos;
301
unsigned char keybuff[MI_MAX_KEY_BUFF];
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;
303
301
my_off_t next_page, dupp_key_pos;
305
303
search_key_length= (comp_flag & SEARCH_FIND) ? key_length : USE_WHOLE_KEY;
306
if (!(temp_buff= (unsigned char*) malloc(keyinfo->block_length+
304
if (!(temp_buff= (uchar*) my_alloca((uint) keyinfo->block_length+
309
307
if (!_mi_fetch_keypage(info,keyinfo,page,DFLT_INIT_HITS,temp_buff,0))
379
377
int _mi_insert(register MI_INFO *info, register MI_KEYDEF *keyinfo,
380
unsigned char *key, unsigned char *anc_buff, unsigned char *key_pos, unsigned char *key_buff,
381
unsigned char *father_buff, unsigned char *father_key_pos, 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,
384
uint32_t a_length,nod_flag;
382
uint a_length,nod_flag;
386
unsigned char *endpos, *prev_key;
384
uchar *endpos, *prev_key;
387
385
MI_KEY_PARAM s_temp;
389
387
nod_flag=mi_test_if_nod(anc_buff);
390
388
a_length=mi_getint(anc_buff);
391
389
endpos= anc_buff+ a_length;
392
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);
393
391
t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,
394
(key_pos == endpos ? (unsigned char*) 0 : key_pos),
392
(key_pos == endpos ? (uchar*) 0 : key_pos),
395
393
prev_key, prev_key,
436
434
/* split a full page in two and assign emerging item to key */
438
436
int _mi_split_page(register MI_INFO *info, register MI_KEYDEF *keyinfo,
439
unsigned char *key, unsigned char *buff, unsigned char *key_buff,
440
bool insert_last_key)
437
uchar *key, uchar *buff, uchar *key_buff,
438
my_bool insert_last_key)
442
uint32_t length,a_length,key_ref_length,t_length,nod_flag,key_length;
443
unsigned char *key_pos,*pos, *after_key= NULL;
440
uint length,a_length,key_ref_length,t_length,nod_flag,key_length;
441
uchar *key_pos,*pos, *after_key= NULL;
444
442
my_off_t new_pos;
445
443
MI_KEY_PARAM s_temp;
477
475
if (!(*keyinfo->get_key)(keyinfo,nod_flag,&key_pos,key_buff))
480
t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,(unsigned char *) 0,
481
(unsigned char*) 0, (unsigned char*) 0,
478
t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,(uchar *) 0,
479
(uchar*) 0, (uchar*) 0,
482
480
key_buff, &s_temp);
483
481
length=(uint) ((buff+a_length)-key_pos);
484
memcpy(info->buff+key_ref_length+t_length, key_pos, length);
482
memcpy((uchar*) info->buff+key_ref_length+t_length,(uchar*) key_pos,
485
484
(*keyinfo->store_key)(keyinfo,info->buff+key_ref_length,&s_temp);
486
485
mi_putint(info->buff,length+t_length+key_ref_length,nod_flag);
499
498
after_key will contain the position to where the next key starts
502
unsigned char *_mi_find_half_pos(uint32_t nod_flag, MI_KEYDEF *keyinfo, unsigned char *page,
503
unsigned char *key, uint32_t *return_key_length,
504
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,
506
uint32_t keys,length,key_ref_length;
507
unsigned char *end,*lastpos;
505
uint keys,length,key_ref_length;
509
508
key_ref_length=2+nod_flag;
510
509
length=mi_getint(page)-key_ref_length;
542
541
key will contain the last key
545
static unsigned char *_mi_find_last_pos(MI_KEYDEF *keyinfo, unsigned char *page,
546
unsigned char *key, uint32_t *return_key_length,
547
unsigned char **after_key)
544
static uchar *_mi_find_last_pos(MI_KEYDEF *keyinfo, uchar *page,
545
uchar *key, uint *return_key_length,
551
uint32_t last_length= 0;
552
uint32_t key_ref_length;
553
unsigned char *end, *lastpos, *prevpos= NULL;
554
unsigned char key_buff[MI_MAX_KEY_BUFF];
552
uchar *end, *lastpos, *prevpos= NULL;
553
uchar key_buff[MI_MAX_KEY_BUFF];
556
555
key_ref_length=2;
557
556
length=mi_getint(page)-key_ref_length;
594
593
/* returns 0 if balance was done */
596
595
static int _mi_balance_page(register MI_INFO *info, MI_KEYDEF *keyinfo,
597
unsigned char *key, unsigned char *curr_buff, unsigned char *father_buff,
598
unsigned char *father_key_pos, my_off_t father_page)
596
uchar *key, uchar *curr_buff, uchar *father_buff,
597
uchar *father_key_pos, my_off_t father_page)
601
uint32_t k_length,father_length,father_keylength,nod_flag,curr_keylength,
600
uint k_length,father_length,father_keylength,nod_flag,curr_keylength,
602
601
right_length,left_length,new_right_length,new_left_length,extra_length,
604
unsigned char *pos,*buff,*extra_buff;
603
uchar *pos,*buff,*extra_buff;
605
604
my_off_t next_page,new_pos;
606
unsigned char tmp_part_key[MI_MAX_KEY_BUFF];
605
uchar tmp_part_key[MI_MAX_KEY_BUFF];
608
607
k_length=keyinfo->keylength;
609
608
father_length=mi_getint(father_buff);
650
649
if (left_length < new_left_length)
651
650
{ /* Move keys buff -> leaf */
652
651
pos=curr_buff+left_length;
653
memcpy(pos, father_key_pos, k_length);
654
length= new_left_length - left_length - k_length;
655
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));
656
655
pos=buff+2+length;
657
memcpy(father_key_pos, pos, k_length);
658
memmove(buff+2, pos+k_length, new_right_length);
656
memcpy((uchar*) father_key_pos,(uchar*) pos,(size_t) k_length);
657
bmove((uchar*) buff+2,(uchar*) pos+k_length,new_right_length);
661
660
{ /* Move keys -> buff */
663
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,
665
664
length=new_right_length-right_length-k_length;
666
memcpy(buff+2+length,father_key_pos, k_length);
665
memcpy((uchar*) buff+2+length,father_key_pos,(size_t) k_length);
667
666
pos=curr_buff+new_left_length;
668
memcpy(father_key_pos, pos, k_length);
669
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);
672
671
if (_mi_write_keypage(info,keyinfo,next_page,DFLT_INIT_HITS,info->buff) ||
690
689
/* move first largest keys to new page */
691
690
pos=buff+right_length-extra_length;
692
memcpy(extra_buff+2, pos, extra_length);
691
memcpy((uchar*) extra_buff+2,pos,(size_t) extra_length);
693
692
/* Save new parting key */
694
693
memcpy(tmp_part_key, pos-k_length,k_length);
695
694
/* Make place for new keys */
696
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,
697
696
right_length-extra_length-k_length-2);
698
697
/* Copy keys from left page */
699
698
pos= curr_buff+new_left_length;
700
length= left_length - new_left_length - k_length;
701
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));
702
701
/* Copy old parting key */
703
memcpy(buff+2+length, father_key_pos, k_length);
702
memcpy((uchar*) buff+2+length,father_key_pos,(size_t) k_length);
705
704
/* Move new parting keys up to caller */
706
memcpy((right ? key : father_key_pos), pos, k_length);
707
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);
709
708
if ((new_pos=_mi_new(info,keyinfo,DFLT_INIT_HITS)) == HA_OFFSET_ERROR)
746
745
/* typeof(_mi_keys_compare)=qsort_cmp2 */
748
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)
750
uint32_t not_used[2];
751
750
return ha_key_cmp(param->info->s->keyinfo[param->keynr].seg,
752
751
key1, key2, USE_WHOLE_KEY, SEARCH_SAME,
757
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)
760
759
Probably I can use info->lastkey here, but I'm not sure,
761
760
and to be safe I'd better use local lastkey.
763
unsigned char lastkey[MI_MAX_KEY_BUFF];
762
uchar lastkey[MI_MAX_KEY_BUFF];
765
764
MI_KEYDEF *keyinfo;
769
768
if (param->info->s->concurrent_insert)
771
pthread_rwlock_wrlock(¶m->info->s->key_root_lock[param->keynr]);
770
rw_wrlock(¶m->info->s->key_root_lock[param->keynr]);
772
771
param->info->s->keyinfo[param->keynr].version++;
780
779
keylen - param->info->s->rec_reflength);
782
781
if (param->info->s->concurrent_insert)
783
pthread_rwlock_unlock(¶m->info->s->key_root_lock[param->keynr]);
782
rw_unlock(¶m->info->s->key_root_lock[param->keynr]);
790
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)
792
791
MYISAM_SHARE *share=info->s;
793
792
MI_KEYDEF *key=share->keyinfo;
794
793
bulk_insert_param *params;
795
uint32_t i, num_keys, total_keylength;
794
uint i, num_keys, total_keylength;
796
795
uint64_t key_map;
798
797
assert(!info->bulk_insert &&
817
816
if (rows && rows*total_keylength < cache_size)
818
cache_size= (uint32_t)rows;
817
cache_size= (ulong)rows;
820
819
cache_size/=total_keylength*16;
822
821
info->bulk_insert=(TREE *)
823
malloc((sizeof(TREE)*share->base.keys+
824
sizeof(bulk_insert_param)*num_keys));
822
my_malloc((sizeof(TREE)*share->base.keys+
823
sizeof(bulk_insert_param)*num_keys),MYF(0));
826
825
if (!info->bulk_insert)
827
826
return(HA_ERR_OUT_OF_MEM);