~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/myisam/mi_write.c

  • Committer: Monty Taylor
  • Date: 2008-09-15 00:46:33 UTC
  • mfrom: (383.1.55 client-split)
  • Revision ID: monty@inaugust.com-20080915004633-fmjw27fi41cxs35w
Merged from client-split.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
/* Write a row to a MyISAM table */
17
17
 
18
 
#include "myisam_priv.h"
19
 
 
20
 
#include "drizzled/internal/m_string.h"
21
 
#include <drizzled/util/test.h>
22
 
 
23
 
using namespace drizzled;
 
18
#include "myisamdef.h"
24
19
 
25
20
#define MAX_POINTER_LENGTH 8
26
21
 
27
22
        /* Functions declared in this file */
28
23
 
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,
 
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,
33
28
                    bool insert_last);
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,
41
 
                      uint32_t key_length);
42
 
int _mi_ck_write_btree(register MI_INFO *info, uint32_t keynr,unsigned char *key,
43
 
                       uint32_t key_length);
 
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,
 
34
                                uchar **after_key);
 
35
int _mi_ck_write_tree(register MI_INFO *info, uint keynr,uchar *key,
 
36
                      uint key_length);
 
37
int _mi_ck_write_btree(register MI_INFO *info, uint keynr,uchar *key,
 
38
                       uint key_length);
44
39
 
45
40
        /* Write new record to database */
46
41
 
47
 
int mi_write(MI_INFO *info, unsigned char *record)
 
42
int mi_write(MI_INFO *info, uchar *record)
48
43
{
49
44
  MYISAM_SHARE *share=info->s;
50
 
  uint32_t i;
 
45
  uint i;
51
46
  int save_errno;
52
 
  internal::my_off_t filepos;
53
 
  unsigned char *buff;
 
47
  my_off_t filepos;
 
48
  uchar *buff;
54
49
  bool lock_tree= share->concurrent_insert;
55
50
 
56
51
  if (share->options & HA_OPTION_READ_ONLY_DATA)
57
52
  {
58
 
    return(errno=EACCES);
 
53
    return(my_errno=EACCES);
59
54
  }
60
55
  if (_mi_readinfo(info,F_WRLCK,1))
61
 
    return(errno);
 
56
    return(my_errno);
62
57
  filepos= ((share->state.dellink != HA_OFFSET_ERROR &&
63
58
             !info->append_insert_at_end) ?
64
59
            share->state.dellink :
68
63
      share->base.records == (ha_rows) 1 &&
69
64
      info->state->records == (ha_rows) 1)
70
65
  {                                             /* System file */
71
 
    errno=HA_ERR_RECORD_FILE_FULL;
 
66
    my_errno=HA_ERR_RECORD_FILE_FULL;
72
67
    goto err2;
73
68
  }
74
69
  if (info->state->key_file_length >= share->base.margin_key_file_length)
75
70
  {
76
 
    errno=HA_ERR_INDEX_FILE_FULL;
 
71
    my_errno=HA_ERR_INDEX_FILE_FULL;
77
72
    goto err2;
78
73
  }
79
74
  if (_mi_mark_file_changed(info))
100
95
                                  is_tree_inited(&info->bulk_insert[i])));
101
96
      if (local_lock_tree)
102
97
      {
 
98
        rw_wrlock(&share->key_root_lock[i]);
103
99
        share->keyinfo[i].version++;
104
100
      }
105
101
      {
106
102
        if (share->keyinfo[i].ck_insert(info,i,buff,
107
103
                        _mi_make_key(info,i,buff,record,filepos)))
108
104
        {
 
105
          if (local_lock_tree)
 
106
            rw_unlock(&share->key_root_lock[i]);
109
107
          goto err;
110
108
        }
111
109
      }
112
110
 
113
111
      /* The above changed info->lastkey2. Inform mi_rnext_same(). */
114
112
      info->update&= ~HA_STATE_RNEXT_SAME;
 
113
 
 
114
      if (local_lock_tree)
 
115
        rw_unlock(&share->key_root_lock[i]);
115
116
    }
116
117
  }
117
118
  if (share->calc_checksum)
129
130
                 HA_STATE_ROW_CHANGED);
130
131
  info->state->records++;
131
132
  info->lastpos=filepos;
132
 
  _mi_writeinfo(info, WRITEINFO_UPDATE_KEYFILE);
 
133
  VOID(_mi_writeinfo(info, WRITEINFO_UPDATE_KEYFILE));
 
134
  if (info->invalidator != 0)
 
135
  {
 
136
    (*info->invalidator)(info->filename);
 
137
    info->invalidator=0;
 
138
  }
133
139
 
134
140
  /*
135
141
    Update status of the table. We need to do so after each row write
139
145
    not critical to use outdated share->is_log_table value (2) locking
140
146
    mutex here for every write is too expensive.
141
147
  */
142
 
  if (share->is_log_table) // Log table do not exist in Drizzle
143
 
    assert(0);
 
148
  if (share->is_log_table)
 
149
    mi_update_status((void*) info);
144
150
 
145
151
  return(0);
146
152
 
147
153
err:
148
 
  save_errno=errno;
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)
 
154
  save_errno=my_errno;
 
155
  if (my_errno == HA_ERR_FOUND_DUPP_KEY || my_errno == HA_ERR_RECORD_FILE_FULL ||
 
156
      my_errno == HA_ERR_NULL_IN_SPATIAL || my_errno == HA_ERR_OUT_OF_MEM)
151
157
  {
152
158
    if (info->bulk_insert)
153
159
    {
154
 
      uint32_t j;
 
160
      uint j;
155
161
      for (j=0 ; j < share->base.keys ; j++)
156
162
        mi_flush_bulk_insert(info, j);
157
163
    }
160
166
    {
161
167
      if (mi_is_key_active(share->state.key_map, i))
162
168
      {
 
169
        bool local_lock_tree= (lock_tree &&
 
170
                                  !(info->bulk_insert &&
 
171
                                    is_tree_inited(&info->bulk_insert[i])));
 
172
        if (local_lock_tree)
 
173
          rw_wrlock(&share->key_root_lock[i]);
163
174
        {
164
 
          uint32_t key_length=_mi_make_key(info,i,buff,record,filepos);
 
175
          uint key_length=_mi_make_key(info,i,buff,record,filepos);
165
176
          if (_mi_ck_delete(info,i,buff,key_length))
166
177
          {
 
178
            if (local_lock_tree)
 
179
              rw_unlock(&share->key_root_lock[i]);
167
180
            break;
168
181
          }
169
182
        }
 
183
        if (local_lock_tree)
 
184
          rw_unlock(&share->key_root_lock[i]);
170
185
      }
171
186
    }
172
187
  }
176
191
    mi_mark_crashed(info);
177
192
  }
178
193
  info->update= (HA_STATE_CHANGED | HA_STATE_WRITTEN | HA_STATE_ROW_CHANGED);
179
 
  errno=save_errno;
 
194
  my_errno=save_errno;
180
195
err2:
181
 
  save_errno=errno;
182
 
  _mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE);
183
 
  return(errno=save_errno);
 
196
  save_errno=my_errno;
 
197
  VOID(_mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE));
 
198
  return(my_errno=save_errno);
184
199
} /* mi_write */
185
200
 
186
201
 
187
202
        /* Write one key to btree */
188
203
 
189
 
int _mi_ck_write(MI_INFO *info, uint32_t keynr, unsigned char *key, uint32_t key_length)
 
204
int _mi_ck_write(MI_INFO *info, uint keynr, uchar *key, uint key_length)
190
205
{
191
206
  if (info->bulk_insert && is_tree_inited(&info->bulk_insert[keynr]))
192
207
  {
203
218
 *                Normal insert code                                  *
204
219
 **********************************************************************/
205
220
 
206
 
int _mi_ck_write_btree(register MI_INFO *info, uint32_t keynr, unsigned char *key,
207
 
                       uint32_t key_length)
 
221
int _mi_ck_write_btree(register MI_INFO *info, uint keynr, uchar *key,
 
222
                       uint key_length)
208
223
{
209
 
  uint32_t error;
210
 
  uint32_t comp_flag;
 
224
  uint error;
 
225
  uint comp_flag;
211
226
  MI_KEYDEF *keyinfo=info->s->keyinfo+keynr;
212
 
  internal::my_off_t  *root=&info->s->state.key_root[keynr];
 
227
  my_off_t  *root=&info->s->state.key_root[keynr];
213
228
 
214
229
  if (keyinfo->flag & HA_SORT_ALLOWS_SAME)
215
230
    comp_flag=SEARCH_BIGGER;                    /* Put after same key */
228
243
} /* _mi_ck_write_btree */
229
244
 
230
245
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)
 
246
    uchar *key, uint key_length, my_off_t *root, uint comp_flag)
232
247
{
233
248
  int error;
234
249
  /* key_length parameter is used only if comp_flag is SEARCH_FIND */
235
250
  if (*root == HA_OFFSET_ERROR ||
236
251
      (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)
 
252
                      *root, (uchar *) 0, (uchar*) 0,
 
253
                      (my_off_t) 0, 1)) > 0)
239
254
    error=_mi_enlarge_root(info,keyinfo,key,root);
240
255
  return(error);
241
256
} /* _mi_ck_real_write_btree */
243
258
 
244
259
        /* Make a new root with key as only pointer */
245
260
 
246
 
int _mi_enlarge_root(MI_INFO *info, MI_KEYDEF *keyinfo, unsigned char *key,
247
 
                     internal::my_off_t *root)
 
261
int _mi_enlarge_root(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *key,
 
262
                     my_off_t *root)
248
263
{
249
 
  uint32_t t_length,nod_flag;
 
264
  uint t_length,nod_flag;
250
265
  MI_KEY_PARAM s_temp;
251
266
  MYISAM_SHARE *share=info->s;
252
267
 
253
268
  nod_flag= (*root != HA_OFFSET_ERROR) ?  share->base.key_reflength : 0;
254
269
  _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);
 
270
  t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,(uchar*) 0,
 
271
                                (uchar*) 0, (uchar*) 0, key,&s_temp);
257
272
  mi_putint(info->buff,t_length+2+nod_flag,nod_flag);
258
273
  (*keyinfo->store_key)(keyinfo,info->buff+2+nod_flag,&s_temp);
259
274
  info->buff_used=info->page_changed=1;         /* info->buff is used */
272
287
        */
273
288
 
274
289
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)
 
290
                    uint comp_flag, uchar *key, uint key_length, my_off_t page,
 
291
                    uchar *father_buff, uchar *father_keypos,
 
292
                    my_off_t father_page, bool insert_last)
278
293
{
279
294
  int error,flag;
280
 
  uint32_t nod_flag, search_key_length;
281
 
  unsigned char *temp_buff,*keypos;
282
 
  unsigned char keybuff[MI_MAX_KEY_BUFF];
 
295
  uint nod_flag, search_key_length;
 
296
  uchar *temp_buff,*keypos;
 
297
  uchar keybuff[MI_MAX_KEY_BUFF];
283
298
  bool was_last_key;
284
 
  internal::my_off_t next_page, dupp_key_pos;
 
299
  my_off_t next_page, dupp_key_pos;
285
300
 
286
301
  search_key_length= (comp_flag & SEARCH_FIND) ? key_length : USE_WHOLE_KEY;
287
 
  if (!(temp_buff= (unsigned char*) malloc(keyinfo->block_length+
288
 
                                           MI_MAX_KEY_BUFF*2)))
 
302
  if (!(temp_buff= (uchar*) my_alloca((uint) keyinfo->block_length+
 
303
                                      MI_MAX_KEY_BUFF*2)))
289
304
    return(-1);
290
305
  if (!_mi_fetch_keypage(info,keyinfo,page,DFLT_INIT_HITS,temp_buff,0))
291
306
    goto err;
295
310
  nod_flag=mi_test_if_nod(temp_buff);
296
311
  if (flag == 0)
297
312
  {
298
 
    uint32_t tmp_key_length;
 
313
    uint tmp_key_length;
299
314
        /* get position to record with duplicated key */
300
315
    tmp_key_length=(*keyinfo->get_key)(keyinfo,nod_flag,&keypos,keybuff);
301
316
    if (tmp_key_length)
305
320
 
306
321
    {
307
322
      info->dupp_key_pos= dupp_key_pos;
308
 
      free(temp_buff);
309
 
      errno=HA_ERR_FOUND_DUPP_KEY;
 
323
      my_afree((uchar*) temp_buff);
 
324
      my_errno=HA_ERR_FOUND_DUPP_KEY;
310
325
      return(-1);
311
326
    }
312
327
  }
324
339
    if (_mi_write_keypage(info,keyinfo,page,DFLT_INIT_HITS,temp_buff))
325
340
      goto err;
326
341
  }
327
 
  free(temp_buff);
 
342
  my_afree((uchar*) temp_buff);
328
343
  return(error);
329
344
err:
330
 
  free(temp_buff);
 
345
  my_afree((uchar*) temp_buff);
331
346
  return (-1);
332
347
} /* w_search */
333
348
 
358
373
*/
359
374
 
360
375
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,
 
376
               uchar *key, uchar *anc_buff, uchar *key_pos, uchar *key_buff,
 
377
               uchar *father_buff, uchar *father_key_pos, my_off_t father_page,
363
378
               bool insert_last)
364
379
{
365
 
  uint32_t a_length,nod_flag;
 
380
  uint a_length,nod_flag;
366
381
  int t_length;
367
 
  unsigned char *endpos, *prev_key;
 
382
  uchar *endpos, *prev_key;
368
383
  MI_KEY_PARAM s_temp;
369
384
 
370
385
  nod_flag=mi_test_if_nod(anc_buff);
371
386
  a_length=mi_getint(anc_buff);
372
387
  endpos= anc_buff+ a_length;
373
 
  prev_key=(key_pos == anc_buff+2+nod_flag ? (unsigned char*) 0 : key_buff);
 
388
  prev_key=(key_pos == anc_buff+2+nod_flag ? (uchar*) 0 : key_buff);
374
389
  t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,
375
 
                                (key_pos == endpos ? (unsigned char*) 0 : key_pos),
 
390
                                (key_pos == endpos ? (uchar*) 0 : key_pos),
376
391
                                prev_key, prev_key,
377
392
                                key,&s_temp);
378
393
 
381
396
    if (t_length >= keyinfo->maxlength*2+MAX_POINTER_LENGTH)
382
397
    {
383
398
      mi_print_error(info->s, HA_ERR_CRASHED);
384
 
      errno=HA_ERR_CRASHED;
 
399
      my_errno=HA_ERR_CRASHED;
385
400
      return(-1);
386
401
    }
387
 
    internal::bmove_upp((unsigned char*) endpos+t_length,(unsigned char*) endpos,(uint) (endpos-key_pos));
 
402
    bmove_upp((uchar*) endpos+t_length,(uchar*) endpos,(uint) (endpos-key_pos));
388
403
  }
389
404
  else
390
405
  {
391
406
    if (-t_length >= keyinfo->maxlength*2+MAX_POINTER_LENGTH)
392
407
    {
393
408
      mi_print_error(info->s, HA_ERR_CRASHED);
394
 
      errno=HA_ERR_CRASHED;
 
409
      my_errno=HA_ERR_CRASHED;
395
410
      return(-1);
396
411
    }
397
 
    memmove(key_pos, key_pos - t_length, endpos - key_pos + t_length);
 
412
    memcpy(key_pos, key_pos - t_length, endpos - key_pos + t_length);
398
413
  }
399
414
  (*keyinfo->store_key)(keyinfo,key_pos,&s_temp);
400
415
  a_length+=t_length;
417
432
        /* split a full page in two and assign emerging item to key */
418
433
 
419
434
int _mi_split_page(register MI_INFO *info, register MI_KEYDEF *keyinfo,
420
 
                   unsigned char *key, unsigned char *buff, unsigned char *key_buff,
 
435
                   uchar *key, uchar *buff, uchar *key_buff,
421
436
                   bool insert_last_key)
422
437
{
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;
 
438
  uint length,a_length,key_ref_length,t_length,nod_flag,key_length;
 
439
  uchar *key_pos,*pos, *after_key= NULL;
 
440
  my_off_t new_pos;
426
441
  MI_KEY_PARAM s_temp;
427
442
 
428
443
  if (info->s->keyinfo+info->lastinx == keyinfo)
458
473
  if (!(*keyinfo->get_key)(keyinfo,nod_flag,&key_pos,key_buff))
459
474
    return(-1);
460
475
 
461
 
  t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,(unsigned char *) 0,
462
 
                                (unsigned char*) 0, (unsigned char*) 0,
 
476
  t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,(uchar *) 0,
 
477
                                (uchar*) 0, (uchar*) 0,
463
478
                                key_buff, &s_temp);
464
479
  length=(uint) ((buff+a_length)-key_pos);
465
480
  memcpy(info->buff+key_ref_length+t_length, key_pos, length);
480
495
          after_key will contain the position to where the next key starts
481
496
        */
482
497
 
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)
 
498
uchar *_mi_find_half_pos(uint nod_flag, MI_KEYDEF *keyinfo, uchar *page,
 
499
                         uchar *key, uint *return_key_length,
 
500
                         uchar **after_key)
486
501
{
487
 
  uint32_t keys,length,key_ref_length;
488
 
  unsigned char *end,*lastpos;
 
502
  uint keys,length,key_ref_length;
 
503
  uchar *end,*lastpos;
489
504
 
490
505
  key_ref_length=2+nod_flag;
491
506
  length=mi_getint(page)-key_ref_length;
523
538
          key will contain the last key
524
539
        */
525
540
 
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)
 
541
static uchar *_mi_find_last_pos(MI_KEYDEF *keyinfo, uchar *page,
 
542
                                uchar *key, uint *return_key_length,
 
543
                                uchar **after_key)
529
544
{
530
 
  uint32_t keys;
531
 
  uint32_t 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];
 
545
  uint keys;
 
546
  uint length;
 
547
  uint last_length= 0;
 
548
  uint key_ref_length;
 
549
  uchar *end, *lastpos, *prevpos= NULL;
 
550
  uchar key_buff[MI_MAX_KEY_BUFF];
536
551
 
537
552
  key_ref_length=2;
538
553
  length=mi_getint(page)-key_ref_length;
561
576
    if (!(length=(*keyinfo->get_key)(keyinfo,0,&page,key_buff)))
562
577
    {
563
578
      mi_print_error(keyinfo->share, HA_ERR_CRASHED);
564
 
      errno=HA_ERR_CRASHED;
 
579
      my_errno=HA_ERR_CRASHED;
565
580
      return(0);
566
581
    }
567
582
  }
575
590
        /* returns 0 if balance was done */
576
591
 
577
592
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)
 
593
                            uchar *key, uchar *curr_buff, uchar *father_buff,
 
594
                            uchar *father_key_pos, my_off_t father_page)
580
595
{
581
596
  bool right;
582
 
  uint32_t k_length,father_length,father_keylength,nod_flag,curr_keylength,
 
597
  uint k_length,father_length,father_keylength,nod_flag,curr_keylength,
583
598
       right_length,left_length,new_right_length,new_left_length,extra_length,
584
599
       length,keys;
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];
 
600
  uchar *pos,*buff,*extra_buff;
 
601
  my_off_t next_page,new_pos;
 
602
  uchar tmp_part_key[MI_MAX_KEY_BUFF];
588
603
 
589
604
  k_length=keyinfo->keylength;
590
605
  father_length=mi_getint(father_buff);
636
651
      memcpy(pos+k_length, buff+2, length);
637
652
      pos=buff+2+length;
638
653
      memcpy(father_key_pos, pos, k_length);
639
 
      memmove(buff+2, pos+k_length, new_right_length);
 
654
      memcpy(buff+2, pos+k_length, new_right_length);
640
655
    }
641
656
    else
642
657
    {                                           /* Move keys -> buff */
643
658
 
644
 
      internal::bmove_upp((unsigned char*) buff+new_right_length,(unsigned char*) buff+right_length,
 
659
      bmove_upp((uchar*) buff+new_right_length,(uchar*) buff+right_length,
645
660
                right_length-2);
646
661
      length=new_right_length-right_length-k_length;
647
662
      memcpy(buff+2+length,father_key_pos, k_length);
674
689
  /* Save new parting key */
675
690
  memcpy(tmp_part_key, pos-k_length,k_length);
676
691
  /* Make place for new keys */
677
 
  internal::bmove_upp((unsigned char*) buff+new_right_length,(unsigned char*) pos-k_length,
 
692
  bmove_upp((uchar*) buff+new_right_length,(uchar*) pos-k_length,
678
693
            right_length-extra_length-k_length-2);
679
694
  /* Copy keys from left page */
680
695
  pos= curr_buff+new_left_length;
708
723
 
709
724
typedef struct {
710
725
  MI_INFO *info;
711
 
  uint32_t keynr;
 
726
  uint keynr;
712
727
} bulk_insert_param;
713
728
 
714
 
int _mi_ck_write_tree(register MI_INFO *info, uint32_t keynr, unsigned char *key,
715
 
                      uint32_t key_length)
 
729
int _mi_ck_write_tree(register MI_INFO *info, uint keynr, uchar *key,
 
730
                      uint key_length)
716
731
{
717
732
  int error;
718
733
 
726
741
 
727
742
/* typeof(_mi_keys_compare)=qsort_cmp2 */
728
743
 
729
 
static int keys_compare(bulk_insert_param *param, unsigned char *key1, unsigned char *key2)
 
744
static int keys_compare(bulk_insert_param *param, uchar *key1, uchar *key2)
730
745
{
731
 
  uint32_t not_used[2];
 
746
  uint not_used[2];
732
747
  return ha_key_cmp(param->info->s->keyinfo[param->keynr].seg,
733
748
                    key1, key2, USE_WHOLE_KEY, SEARCH_SAME,
734
749
                    not_used);
735
750
}
736
751
 
737
752
 
738
 
static int keys_free(unsigned char *key, TREE_FREE mode, bulk_insert_param *param)
 
753
static int keys_free(uchar *key, TREE_FREE mode, bulk_insert_param *param)
739
754
{
740
755
  /*
741
756
    Probably I can use info->lastkey here, but I'm not sure,
742
757
    and to be safe I'd better use local lastkey.
743
758
  */
744
 
  unsigned char lastkey[MI_MAX_KEY_BUFF];
745
 
  uint32_t keylen;
 
759
  uchar lastkey[MI_MAX_KEY_BUFF];
 
760
  uint keylen;
746
761
  MI_KEYDEF *keyinfo;
747
762
 
748
763
  switch (mode) {
749
764
  case free_init:
750
765
    if (param->info->s->concurrent_insert)
751
766
    {
 
767
      rw_wrlock(&param->info->s->key_root_lock[param->keynr]);
752
768
      param->info->s->keyinfo[param->keynr].version++;
753
769
    }
754
770
    return 0;
759
775
    return _mi_ck_write_btree(param->info,param->keynr,lastkey,
760
776
                              keylen - param->info->s->rec_reflength);
761
777
  case free_end:
 
778
    if (param->info->s->concurrent_insert)
 
779
      rw_unlock(&param->info->s->key_root_lock[param->keynr]);
762
780
    return 0;
763
781
  }
764
782
  return -1;
770
788
  MYISAM_SHARE *share=info->s;
771
789
  MI_KEYDEF *key=share->keyinfo;
772
790
  bulk_insert_param *params;
773
 
  uint32_t i, num_keys, total_keylength;
 
791
  uint i, num_keys, total_keylength;
774
792
  uint64_t key_map;
775
793
 
776
794
  assert(!info->bulk_insert &&
798
816
    cache_size/=total_keylength*16;
799
817
 
800
818
  info->bulk_insert=(TREE *)
801
 
    malloc((sizeof(TREE)*share->base.keys+
802
 
           sizeof(bulk_insert_param)*num_keys));
 
819
    my_malloc((sizeof(TREE)*share->base.keys+
 
820
               sizeof(bulk_insert_param)*num_keys),MYF(0));
803
821
 
804
822
  if (!info->bulk_insert)
805
823
    return(HA_ERR_OUT_OF_MEM);
815
833
      init_tree(&info->bulk_insert[i],
816
834
                cache_size * key[i].maxlength,
817
835
                cache_size * key[i].maxlength, 0,
818
 
                (qsort_cmp2)keys_compare, false,
 
836
                (qsort_cmp2)keys_compare, 0,
819
837
                (tree_element_free) keys_free, (void *)params++);
820
838
    }
821
839
    else
825
843
  return(0);
826
844
}
827
845
 
828
 
void mi_flush_bulk_insert(MI_INFO *info, uint32_t inx)
 
846
void mi_flush_bulk_insert(MI_INFO *info, uint inx)
829
847
{
830
848
  if (info->bulk_insert)
831
849
  {
838
856
{
839
857
  if (info->bulk_insert)
840
858
  {
841
 
    uint32_t i;
 
859
    uint i;
842
860
    for (i=0 ; i < info->s->base.keys ; i++)
843
861
    {
844
862
      if (is_tree_inited(& info->bulk_insert[i]))
846
864
        delete_tree(& info->bulk_insert[i]);
847
865
      }
848
866
    }
849
 
    free((void *)info->bulk_insert);
 
867
    my_free((void *)info->bulk_insert, MYF(0));
850
868
    info->bulk_insert=0;
851
869
  }
852
870
}