~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/myisam/mi_write.c

Removed/replaced DBUG symbols and standardized TRUE/FALSE

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
/* Write a row to a MyISAM table */
17
17
 
18
18
#include "myisamdef.h"
19
 
#include <drizzled/util/test.h>
20
 
 
21
19
 
22
20
#define MAX_POINTER_LENGTH 8
23
21
 
24
22
        /* Functions declared in this file */
25
23
 
26
24
static int w_search(MI_INFO *info,MI_KEYDEF *keyinfo,
27
 
                    uint32_t comp_flag, unsigned char *key,
28
 
                    uint32_t key_length, my_off_t pos, unsigned char *father_buff,
29
 
                    unsigned char *father_keypos, my_off_t father_page,
30
 
                    bool insert_last);
31
 
static int _mi_balance_page(MI_INFO *info,MI_KEYDEF *keyinfo,unsigned char *key,
32
 
                            unsigned char *curr_buff,unsigned char *father_buff,
33
 
                            unsigned char *father_keypos,my_off_t father_page);
34
 
static unsigned char *_mi_find_last_pos(MI_KEYDEF *keyinfo, unsigned char *page,
35
 
                                unsigned char *key, uint32_t *return_key_length,
36
 
                                unsigned char **after_key);
37
 
int _mi_ck_write_tree(register MI_INFO *info, uint32_t keynr,unsigned char *key,
38
 
                      uint32_t key_length);
39
 
int _mi_ck_write_btree(register MI_INFO *info, uint32_t keynr,unsigned char *key,
40
 
                       uint32_t key_length);
 
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,
 
28
                    my_bool insert_last);
 
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);
41
39
 
42
40
        /* Write new record to database */
43
41
 
44
 
int mi_write(MI_INFO *info, unsigned char *record)
 
42
int mi_write(MI_INFO *info, uchar *record)
45
43
{
46
44
  MYISAM_SHARE *share=info->s;
47
 
  uint32_t i;
 
45
  uint i;
48
46
  int save_errno;
49
47
  my_off_t filepos;
50
 
  unsigned char *buff;
51
 
  bool lock_tree= share->concurrent_insert;
 
48
  uchar *buff;
 
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));
52
52
 
 
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););
53
56
  if (share->options & HA_OPTION_READ_ONLY_DATA)
54
57
  {
55
 
    return(my_errno=EACCES);
 
58
    DBUG_RETURN(my_errno=EACCES);
56
59
  }
57
60
  if (_mi_readinfo(info,F_WRLCK,1))
58
 
    return(my_errno);
 
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))
 
65
    goto err;
 
66
#endif
59
67
  filepos= ((share->state.dellink != HA_OFFSET_ERROR &&
60
68
             !info->append_insert_at_end) ?
61
69
            share->state.dellink :
92
100
  {
93
101
    if (mi_is_key_active(share->state.key_map, i))
94
102
    {
95
 
      bool local_lock_tree= (lock_tree &&
 
103
      my_bool local_lock_tree= (lock_tree &&
96
104
                                !(info->bulk_insert &&
97
105
                                  is_tree_inited(&info->bulk_insert[i])));
98
106
      if (local_lock_tree)
106
114
        {
107
115
          if (local_lock_tree)
108
116
            rw_unlock(&share->key_root_lock[i]);
 
117
          DBUG_PRINT("error",("Got error: %d on write",my_errno));
109
118
          goto err;
110
119
        }
111
120
      }
132
141
                 HA_STATE_ROW_CHANGED);
133
142
  info->state->records++;
134
143
  info->lastpos=filepos;
135
 
  _mi_writeinfo(info, WRITEINFO_UPDATE_KEYFILE);
 
144
  myisam_log_record(MI_LOG_WRITE,info,record,filepos,0);
 
145
  VOID(_mi_writeinfo(info, WRITEINFO_UPDATE_KEYFILE));
136
146
  if (info->invalidator != 0)
137
147
  {
 
148
    DBUG_PRINT("info", ("invalidator... '%s' (update)", info->filename));
138
149
    (*info->invalidator)(info->filename);
139
150
    info->invalidator=0;
140
151
  }
150
161
  if (share->is_log_table)
151
162
    mi_update_status((void*) info);
152
163
 
153
 
  return(0);
 
164
  DBUG_RETURN(0);
154
165
 
155
166
err:
156
167
  save_errno=my_errno;
159
170
  {
160
171
    if (info->bulk_insert)
161
172
    {
162
 
      uint32_t j;
 
173
      uint j;
163
174
      for (j=0 ; j < share->base.keys ; j++)
164
175
        mi_flush_bulk_insert(info, j);
165
176
    }
168
179
    {
169
180
      if (mi_is_key_active(share->state.key_map, i))
170
181
      {
171
 
        bool local_lock_tree= (lock_tree &&
 
182
        my_bool local_lock_tree= (lock_tree &&
172
183
                                  !(info->bulk_insert &&
173
184
                                    is_tree_inited(&info->bulk_insert[i])));
174
185
        if (local_lock_tree)
175
186
          rw_wrlock(&share->key_root_lock[i]);
176
187
        {
177
 
          uint32_t key_length=_mi_make_key(info,i,buff,record,filepos);
 
188
          uint key_length=_mi_make_key(info,i,buff,record,filepos);
178
189
          if (_mi_ck_delete(info,i,buff,key_length))
179
190
          {
180
191
            if (local_lock_tree)
196
207
  my_errno=save_errno;
197
208
err2:
198
209
  save_errno=my_errno;
199
 
  _mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE);
200
 
  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
213
} /* mi_write */
202
214
 
203
215
 
204
216
        /* Write one key to btree */
205
217
 
206
 
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)
207
219
{
 
220
  DBUG_ENTER("_mi_ck_write");
 
221
 
208
222
  if (info->bulk_insert && is_tree_inited(&info->bulk_insert[keynr]))
209
223
  {
210
 
    return(_mi_ck_write_tree(info, keynr, key, key_length));
 
224
    DBUG_RETURN(_mi_ck_write_tree(info, keynr, key, key_length));
211
225
  }
212
226
  else
213
227
  {
214
 
    return(_mi_ck_write_btree(info, keynr, key, key_length));
 
228
    DBUG_RETURN(_mi_ck_write_btree(info, keynr, key, key_length));
215
229
  }
216
230
} /* _mi_ck_write */
217
231
 
220
234
 *                Normal insert code                                  *
221
235
 **********************************************************************/
222
236
 
223
 
int _mi_ck_write_btree(register MI_INFO *info, uint32_t keynr, unsigned char *key,
224
 
                       uint32_t key_length)
 
237
int _mi_ck_write_btree(register MI_INFO *info, uint keynr, uchar *key,
 
238
                       uint key_length)
225
239
{
226
 
  uint32_t error;
227
 
  uint32_t comp_flag;
 
240
  uint error;
 
241
  uint comp_flag;
228
242
  MI_KEYDEF *keyinfo=info->s->keyinfo+keynr;
229
243
  my_off_t  *root=&info->s->state.key_root[keynr];
 
244
  DBUG_ENTER("_mi_ck_write_btree");
230
245
 
231
246
  if (keyinfo->flag & HA_SORT_ALLOWS_SAME)
232
247
    comp_flag=SEARCH_BIGGER;                    /* Put after same key */
241
256
 
242
257
  error=_mi_ck_real_write_btree(info, keyinfo, key, key_length,
243
258
                                root, comp_flag);
244
 
  return(error);
 
259
  DBUG_RETURN(error);
245
260
} /* _mi_ck_write_btree */
246
261
 
247
262
int _mi_ck_real_write_btree(MI_INFO *info, MI_KEYDEF *keyinfo,
248
 
    unsigned char *key, uint32_t key_length, my_off_t *root, uint32_t comp_flag)
 
263
    uchar *key, uint key_length, my_off_t *root, uint comp_flag)
249
264
{
250
265
  int error;
 
266
  DBUG_ENTER("_mi_ck_real_write_btree");
251
267
  /* key_length parameter is used only if comp_flag is SEARCH_FIND */
252
268
  if (*root == HA_OFFSET_ERROR ||
253
269
      (error=w_search(info, keyinfo, comp_flag, key, key_length,
254
 
                      *root, (unsigned char *) 0, (unsigned char*) 0,
 
270
                      *root, (uchar *) 0, (uchar*) 0,
255
271
                      (my_off_t) 0, 1)) > 0)
256
272
    error=_mi_enlarge_root(info,keyinfo,key,root);
257
 
  return(error);
 
273
  DBUG_RETURN(error);
258
274
} /* _mi_ck_real_write_btree */
259
275
 
260
276
 
261
277
        /* Make a new root with key as only pointer */
262
278
 
263
 
int _mi_enlarge_root(MI_INFO *info, MI_KEYDEF *keyinfo, unsigned char *key,
 
279
int _mi_enlarge_root(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *key,
264
280
                     my_off_t *root)
265
281
{
266
 
  uint32_t t_length,nod_flag;
 
282
  uint t_length,nod_flag;
267
283
  MI_KEY_PARAM s_temp;
268
284
  MYISAM_SHARE *share=info->s;
 
285
  DBUG_ENTER("_mi_enlarge_root");
269
286
 
270
287
  nod_flag= (*root != HA_OFFSET_ERROR) ?  share->base.key_reflength : 0;
271
288
  _mi_kpointer(info,info->buff+2,*root); /* if nod */
272
 
  t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,(unsigned char*) 0,
273
 
                                (unsigned char*) 0, (unsigned char*) 0, key,&s_temp);
 
289
  t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,(uchar*) 0,
 
290
                                (uchar*) 0, (uchar*) 0, key,&s_temp);
274
291
  mi_putint(info->buff,t_length+2+nod_flag,nod_flag);
275
292
  (*keyinfo->store_key)(keyinfo,info->buff+2+nod_flag,&s_temp);
276
293
  info->buff_used=info->page_changed=1;         /* info->buff is used */
277
294
  if ((*root= _mi_new(info,keyinfo,DFLT_INIT_HITS)) == HA_OFFSET_ERROR ||
278
295
      _mi_write_keypage(info,keyinfo,*root,DFLT_INIT_HITS,info->buff))
279
 
    return(-1);
280
 
  return(0);
 
296
    DBUG_RETURN(-1);
 
297
  DBUG_RETURN(0);
281
298
} /* _mi_enlarge_root */
282
299
 
283
300
 
289
306
        */
290
307
 
291
308
static int w_search(register MI_INFO *info, register MI_KEYDEF *keyinfo,
292
 
                    uint32_t comp_flag, unsigned char *key, uint32_t key_length, my_off_t page,
293
 
                    unsigned char *father_buff, unsigned char *father_keypos,
294
 
                    my_off_t father_page, bool insert_last)
 
309
                    uint comp_flag, uchar *key, uint key_length, my_off_t page,
 
310
                    uchar *father_buff, uchar *father_keypos,
 
311
                    my_off_t father_page, my_bool insert_last)
295
312
{
296
313
  int error,flag;
297
 
  uint32_t nod_flag, search_key_length;
298
 
  unsigned char *temp_buff,*keypos;
299
 
  unsigned char keybuff[MI_MAX_KEY_BUFF];
300
 
  bool was_last_key;
 
314
  uint nod_flag, search_key_length;
 
315
  uchar *temp_buff,*keypos;
 
316
  uchar keybuff[MI_MAX_KEY_BUFF];
 
317
  my_bool was_last_key;
301
318
  my_off_t next_page, dupp_key_pos;
 
319
  DBUG_ENTER("w_search");
 
320
  DBUG_PRINT("enter",("page: %ld", (long) page));
302
321
 
303
322
  search_key_length= (comp_flag & SEARCH_FIND) ? key_length : USE_WHOLE_KEY;
304
 
  if (!(temp_buff= (unsigned char*) my_alloca((uint) keyinfo->block_length+
 
323
  if (!(temp_buff= (uchar*) my_alloca((uint) keyinfo->block_length+
305
324
                                      MI_MAX_KEY_BUFF*2)))
306
 
    return(-1);
 
325
    DBUG_RETURN(-1);
307
326
  if (!_mi_fetch_keypage(info,keyinfo,page,DFLT_INIT_HITS,temp_buff,0))
308
327
    goto err;
309
328
 
312
331
  nod_flag=mi_test_if_nod(temp_buff);
313
332
  if (flag == 0)
314
333
  {
315
 
    uint32_t tmp_key_length;
 
334
    uint tmp_key_length;
316
335
        /* get position to record with duplicated key */
317
336
    tmp_key_length=(*keyinfo->get_key)(keyinfo,nod_flag,&keypos,keybuff);
318
337
    if (tmp_key_length)
322
341
 
323
342
    {
324
343
      info->dupp_key_pos= dupp_key_pos;
325
 
      my_afree((unsigned char*) temp_buff);
 
344
      my_afree((uchar*) temp_buff);
326
345
      my_errno=HA_ERR_FOUND_DUPP_KEY;
327
 
      return(-1);
 
346
      DBUG_RETURN(-1);
328
347
    }
329
348
  }
330
349
  if (flag == MI_FOUND_WRONG_KEY)
331
 
    return(-1);
 
350
    DBUG_RETURN(-1);
332
351
  if (!was_last_key)
333
352
    insert_last=0;
334
353
  next_page=_mi_kpos(nod_flag,keypos);
341
360
    if (_mi_write_keypage(info,keyinfo,page,DFLT_INIT_HITS,temp_buff))
342
361
      goto err;
343
362
  }
344
 
  my_afree((unsigned char*) temp_buff);
345
 
  return(error);
 
363
  my_afree((uchar*) temp_buff);
 
364
  DBUG_RETURN(error);
346
365
err:
347
 
  my_afree((unsigned char*) temp_buff);
348
 
  return (-1);
 
366
  my_afree((uchar*) temp_buff);
 
367
  DBUG_PRINT("exit",("Error: %d",my_errno));
 
368
  DBUG_RETURN (-1);
349
369
} /* w_search */
350
370
 
351
371
 
375
395
*/
376
396
 
377
397
int _mi_insert(register MI_INFO *info, register MI_KEYDEF *keyinfo,
378
 
               unsigned char *key, unsigned char *anc_buff, unsigned char *key_pos, unsigned char *key_buff,
379
 
               unsigned char *father_buff, unsigned char *father_key_pos, my_off_t father_page,
380
 
               bool insert_last)
 
398
               uchar *key, uchar *anc_buff, uchar *key_pos, uchar *key_buff,
 
399
               uchar *father_buff, uchar *father_key_pos, my_off_t father_page,
 
400
               my_bool insert_last)
381
401
{
382
 
  uint32_t a_length,nod_flag;
 
402
  uint a_length,nod_flag;
383
403
  int t_length;
384
 
  unsigned char *endpos, *prev_key;
 
404
  uchar *endpos, *prev_key;
385
405
  MI_KEY_PARAM s_temp;
 
406
  DBUG_ENTER("_mi_insert");
 
407
  DBUG_PRINT("enter",("key_pos: 0x%lx", (long) key_pos));
 
408
  DBUG_EXECUTE("key",_mi_print_key(DBUG_FILE,keyinfo->seg,key,USE_WHOLE_KEY););
386
409
 
387
410
  nod_flag=mi_test_if_nod(anc_buff);
388
411
  a_length=mi_getint(anc_buff);
389
412
  endpos= anc_buff+ a_length;
390
 
  prev_key=(key_pos == anc_buff+2+nod_flag ? (unsigned char*) 0 : key_buff);
 
413
  prev_key=(key_pos == anc_buff+2+nod_flag ? (uchar*) 0 : key_buff);
391
414
  t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,
392
 
                                (key_pos == endpos ? (unsigned char*) 0 : key_pos),
 
415
                                (key_pos == endpos ? (uchar*) 0 : key_pos),
393
416
                                prev_key, prev_key,
394
417
                                key,&s_temp);
395
 
 
 
418
#ifndef DBUG_OFF
 
419
  if (key_pos != anc_buff+2+nod_flag && (keyinfo->flag &
 
420
                                         (HA_BINARY_PACK_KEY | HA_PACK_KEY)))
 
421
  {
 
422
    DBUG_DUMP("prev_key",(uchar*) key_buff,_mi_keylength(keyinfo,key_buff));
 
423
  }
 
424
  if (keyinfo->flag & HA_PACK_KEY)
 
425
  {
 
426
    DBUG_PRINT("test",("t_length: %d  ref_len: %d",
 
427
                       t_length,s_temp.ref_length));
 
428
    DBUG_PRINT("test",("n_ref_len: %d  n_length: %d  key_pos: 0x%lx",
 
429
                       s_temp.n_ref_length,s_temp.n_length, (long) s_temp.key));
 
430
  }
 
431
#endif
396
432
  if (t_length > 0)
397
433
  {
398
434
    if (t_length >= keyinfo->maxlength*2+MAX_POINTER_LENGTH)
399
435
    {
400
436
      mi_print_error(info->s, HA_ERR_CRASHED);
401
437
      my_errno=HA_ERR_CRASHED;
402
 
      return(-1);
 
438
      DBUG_RETURN(-1);
403
439
    }
404
 
    bmove_upp((unsigned char*) endpos+t_length,(unsigned char*) endpos,(uint) (endpos-key_pos));
 
440
    bmove_upp((uchar*) endpos+t_length,(uchar*) endpos,(uint) (endpos-key_pos));
405
441
  }
406
442
  else
407
443
  {
409
445
    {
410
446
      mi_print_error(info->s, HA_ERR_CRASHED);
411
447
      my_errno=HA_ERR_CRASHED;
412
 
      return(-1);
 
448
      DBUG_RETURN(-1);
413
449
    }
414
 
    memcpy(key_pos, key_pos - t_length, endpos - key_pos + t_length);
 
450
    bmove(key_pos,key_pos-t_length,(uint) (endpos-key_pos)+t_length);
415
451
  }
416
452
  (*keyinfo->store_key)(keyinfo,key_pos,&s_temp);
417
453
  a_length+=t_length;
418
454
  mi_putint(anc_buff,a_length,nod_flag);
419
455
  if (a_length <= keyinfo->block_length)
420
456
  {
421
 
    return(0);                          /* There is room on page */
 
457
    DBUG_RETURN(0);                             /* There is room on page */
422
458
  }
423
459
  /* Page is full */
424
460
  if (nod_flag)
425
461
    insert_last=0;
426
462
  if (!(keyinfo->flag & (HA_VAR_LENGTH_KEY | HA_BINARY_PACK_KEY)) &&
427
463
      father_buff && !insert_last)
428
 
    return(_mi_balance_page(info,keyinfo,key,anc_buff,father_buff,
 
464
    DBUG_RETURN(_mi_balance_page(info,keyinfo,key,anc_buff,father_buff,
429
465
                                 father_key_pos,father_page));
430
 
  return(_mi_split_page(info,keyinfo,key,anc_buff,key_buff, insert_last));
 
466
  DBUG_RETURN(_mi_split_page(info,keyinfo,key,anc_buff,key_buff, insert_last));
431
467
} /* _mi_insert */
432
468
 
433
469
 
434
470
        /* split a full page in two and assign emerging item to key */
435
471
 
436
472
int _mi_split_page(register MI_INFO *info, register MI_KEYDEF *keyinfo,
437
 
                   unsigned char *key, unsigned char *buff, unsigned char *key_buff,
438
 
                   bool insert_last_key)
 
473
                   uchar *key, uchar *buff, uchar *key_buff,
 
474
                   my_bool insert_last_key)
439
475
{
440
 
  uint32_t length,a_length,key_ref_length,t_length,nod_flag,key_length;
441
 
  unsigned char *key_pos,*pos, *after_key= NULL;
 
476
  uint length,a_length,key_ref_length,t_length,nod_flag,key_length;
 
477
  uchar *key_pos,*pos, *after_key= NULL;
442
478
  my_off_t new_pos;
443
479
  MI_KEY_PARAM s_temp;
 
480
  DBUG_ENTER("mi_split_page");
 
481
  DBUG_DUMP("buff",(uchar*) buff,mi_getint(buff));
444
482
 
445
483
  if (info->s->keyinfo+info->lastinx == keyinfo)
446
484
    info->page_changed=1;                       /* Info->buff is used */
453
491
    key_pos=_mi_find_half_pos(nod_flag,keyinfo,buff,key_buff, &key_length,
454
492
                              &after_key);
455
493
  if (!key_pos)
456
 
    return(-1);
 
494
    DBUG_RETURN(-1);
457
495
 
458
496
  length=(uint) (key_pos-buff);
459
497
  a_length=mi_getint(buff);
462
500
  key_pos=after_key;
463
501
  if (nod_flag)
464
502
  {
 
503
    DBUG_PRINT("test",("Splitting nod"));
465
504
    pos=key_pos-nod_flag;
466
 
    memcpy(info->buff + 2, pos, nod_flag);
 
505
    memcpy((uchar*) info->buff+2,(uchar*) pos,(size_t) nod_flag);
467
506
  }
468
507
 
469
508
        /* Move middle item to key and pointer to new page */
470
509
  if ((new_pos=_mi_new(info,keyinfo,DFLT_INIT_HITS)) == HA_OFFSET_ERROR)
471
 
    return(-1);
 
510
    DBUG_RETURN(-1);
472
511
  _mi_kpointer(info,_mi_move_key(keyinfo,key,key_buff),new_pos);
473
512
 
474
513
        /* Store new page */
475
514
  if (!(*keyinfo->get_key)(keyinfo,nod_flag,&key_pos,key_buff))
476
 
    return(-1);
 
515
    DBUG_RETURN(-1);
477
516
 
478
 
  t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,(unsigned char *) 0,
479
 
                                (unsigned char*) 0, (unsigned char*) 0,
 
517
  t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,(uchar *) 0,
 
518
                                (uchar*) 0, (uchar*) 0,
480
519
                                key_buff, &s_temp);
481
520
  length=(uint) ((buff+a_length)-key_pos);
482
 
  memcpy(info->buff+key_ref_length+t_length, key_pos, length);
 
521
  memcpy((uchar*) info->buff+key_ref_length+t_length,(uchar*) key_pos,
 
522
         (size_t) length);
483
523
  (*keyinfo->store_key)(keyinfo,info->buff+key_ref_length,&s_temp);
484
524
  mi_putint(info->buff,length+t_length+key_ref_length,nod_flag);
485
525
 
486
526
  if (_mi_write_keypage(info,keyinfo,new_pos,DFLT_INIT_HITS,info->buff))
487
 
    return(-1);
488
 
  return(2);                            /* Middle key up */
 
527
    DBUG_RETURN(-1);
 
528
  DBUG_DUMP("key",(uchar*) key,_mi_keylength(keyinfo,key));
 
529
  DBUG_RETURN(2);                               /* Middle key up */
489
530
} /* _mi_split_page */
490
531
 
491
532
 
497
538
          after_key will contain the position to where the next key starts
498
539
        */
499
540
 
500
 
unsigned char *_mi_find_half_pos(uint32_t nod_flag, MI_KEYDEF *keyinfo, unsigned char *page,
501
 
                         unsigned char *key, uint32_t *return_key_length,
502
 
                         unsigned char **after_key)
 
541
uchar *_mi_find_half_pos(uint nod_flag, MI_KEYDEF *keyinfo, uchar *page,
 
542
                         uchar *key, uint *return_key_length,
 
543
                         uchar **after_key)
503
544
{
504
 
  uint32_t keys,length,key_ref_length;
505
 
  unsigned char *end,*lastpos;
 
545
  uint keys,length,key_ref_length;
 
546
  uchar *end,*lastpos;
 
547
  DBUG_ENTER("_mi_find_half_pos");
506
548
 
507
549
  key_ref_length=2+nod_flag;
508
550
  length=mi_getint(page)-key_ref_length;
517
559
    end=page+keys*key_ref_length;
518
560
    *after_key=end+key_ref_length;
519
561
    memcpy(key,end,key_ref_length);
520
 
    return(end);
 
562
    DBUG_RETURN(end);
521
563
  }
522
564
 
523
565
  end=page+length/2-key_ref_length;             /* This is aprox. half */
526
568
  {
527
569
    lastpos=page;
528
570
    if (!(length=(*keyinfo->get_key)(keyinfo,nod_flag,&page,key)))
529
 
      return(0);
 
571
      DBUG_RETURN(0);
530
572
  } while (page < end);
531
573
  *return_key_length=length;
532
574
  *after_key=page;
533
 
  return(lastpos);
 
575
  DBUG_PRINT("exit",("returns: 0x%lx  page: 0x%lx  half: 0x%lx",
 
576
                     (long) lastpos, (long) page, (long) end));
 
577
  DBUG_RETURN(lastpos);
534
578
} /* _mi_find_half_pos */
535
579
 
536
580
 
540
584
          key will contain the last key
541
585
        */
542
586
 
543
 
static unsigned char *_mi_find_last_pos(MI_KEYDEF *keyinfo, unsigned char *page,
544
 
                                unsigned char *key, uint32_t *return_key_length,
545
 
                                unsigned char **after_key)
 
587
static uchar *_mi_find_last_pos(MI_KEYDEF *keyinfo, uchar *page,
 
588
                                uchar *key, uint *return_key_length,
 
589
                                uchar **after_key)
546
590
{
547
 
  uint32_t keys;
548
 
  uint32_t length;
549
 
  uint32_t last_length= 0;
550
 
  uint32_t key_ref_length;
551
 
  unsigned char *end, *lastpos, *prevpos= NULL;
552
 
  unsigned char key_buff[MI_MAX_KEY_BUFF];
 
591
  uint keys, length, last_length, key_ref_length;
 
592
  uchar *end, *lastpos, *prevpos= NULL;
 
593
  uchar key_buff[MI_MAX_KEY_BUFF];
 
594
  DBUG_ENTER("_mi_find_last_pos");
553
595
 
554
596
  key_ref_length=2;
555
597
  length=mi_getint(page)-key_ref_length;
563
605
    end=page+keys*length;
564
606
    *after_key=end+length;
565
607
    memcpy(key,end,length);
566
 
    return(end);
 
608
    DBUG_RETURN(end);
567
609
  }
568
610
 
569
611
  end= page + length - key_ref_length;
579
621
    {
580
622
      mi_print_error(keyinfo->share, HA_ERR_CRASHED);
581
623
      my_errno=HA_ERR_CRASHED;
582
 
      return(0);
 
624
      DBUG_RETURN(0);
583
625
    }
584
626
  }
585
627
  *return_key_length=last_length;
586
628
  *after_key=lastpos;
587
 
  return(prevpos);
 
629
  DBUG_PRINT("exit",("returns: 0x%lx  page: 0x%lx  end: 0x%lx",
 
630
                     (long) prevpos,(long) page,(long) end));
 
631
  DBUG_RETURN(prevpos);
588
632
} /* _mi_find_last_pos */
589
633
 
590
634
 
592
636
        /* returns 0 if balance was done */
593
637
 
594
638
static int _mi_balance_page(register MI_INFO *info, MI_KEYDEF *keyinfo,
595
 
                            unsigned char *key, unsigned char *curr_buff, unsigned char *father_buff,
596
 
                            unsigned char *father_key_pos, my_off_t father_page)
 
639
                            uchar *key, uchar *curr_buff, uchar *father_buff,
 
640
                            uchar *father_key_pos, my_off_t father_page)
597
641
{
598
 
  bool right;
599
 
  uint32_t k_length,father_length,father_keylength,nod_flag,curr_keylength,
 
642
  my_bool right;
 
643
  uint k_length,father_length,father_keylength,nod_flag,curr_keylength,
600
644
       right_length,left_length,new_right_length,new_left_length,extra_length,
601
645
       length,keys;
602
 
  unsigned char *pos,*buff,*extra_buff;
 
646
  uchar *pos,*buff,*extra_buff;
603
647
  my_off_t next_page,new_pos;
604
 
  unsigned char tmp_part_key[MI_MAX_KEY_BUFF];
 
648
  uchar tmp_part_key[MI_MAX_KEY_BUFF];
 
649
  DBUG_ENTER("_mi_balance_page");
605
650
 
606
651
  k_length=keyinfo->keylength;
607
652
  father_length=mi_getint(father_buff);
618
663
    next_page= _mi_kpos(info->s->base.key_reflength,
619
664
                        father_key_pos+father_keylength);
620
665
    buff=info->buff;
 
666
    DBUG_PRINT("test",("use right page: %lu", (ulong) next_page));
621
667
  }
622
668
  else
623
669
  {
626
672
    next_page= _mi_kpos(info->s->base.key_reflength,father_key_pos);
627
673
                                        /* Fix that curr_buff is to left */
628
674
    buff=curr_buff; curr_buff=info->buff;
 
675
    DBUG_PRINT("test",("use left page: %lu", (ulong) next_page));
629
676
  }                                     /* father_key_pos ptr to parting key */
630
677
 
631
678
  if (!_mi_fetch_keypage(info,keyinfo,next_page,DFLT_INIT_HITS,info->buff,0))
632
679
    goto err;
 
680
  DBUG_DUMP("next",(uchar*) info->buff,mi_getint(info->buff));
633
681
 
634
682
        /* Test if there is room to share keys */
635
683
 
648
696
    if (left_length < new_left_length)
649
697
    {                                           /* Move keys buff -> leaf */
650
698
      pos=curr_buff+left_length;
651
 
      memcpy(pos, father_key_pos, k_length);
652
 
      length= new_left_length - left_length - k_length;
653
 
      memcpy(pos+k_length, buff+2, length);
 
699
      memcpy((uchar*) pos,(uchar*) father_key_pos, (size_t) k_length);
 
700
      memcpy((uchar*) pos+k_length, (uchar*) buff+2,
 
701
             (size_t) (length=new_left_length - left_length - k_length));
654
702
      pos=buff+2+length;
655
 
      memcpy(father_key_pos, pos, k_length);
656
 
      memcpy(buff+2, pos+k_length, new_right_length);
 
703
      memcpy((uchar*) father_key_pos,(uchar*) pos,(size_t) k_length);
 
704
      bmove((uchar*) buff+2,(uchar*) pos+k_length,new_right_length);
657
705
    }
658
706
    else
659
707
    {                                           /* Move keys -> buff */
660
708
 
661
 
      bmove_upp((unsigned char*) buff+new_right_length,(unsigned char*) buff+right_length,
 
709
      bmove_upp((uchar*) buff+new_right_length,(uchar*) buff+right_length,
662
710
                right_length-2);
663
711
      length=new_right_length-right_length-k_length;
664
 
      memcpy(buff+2+length,father_key_pos, k_length);
 
712
      memcpy((uchar*) buff+2+length,father_key_pos,(size_t) k_length);
665
713
      pos=curr_buff+new_left_length;
666
 
      memcpy(father_key_pos, pos, k_length);
667
 
      memcpy(buff+2, pos+k_length, length);
 
714
      memcpy((uchar*) father_key_pos,(uchar*) pos,(size_t) k_length);
 
715
      memcpy((uchar*) buff+2,(uchar*) pos+k_length,(size_t) length);
668
716
    }
669
717
 
670
718
    if (_mi_write_keypage(info,keyinfo,next_page,DFLT_INIT_HITS,info->buff) ||
671
719
        _mi_write_keypage(info,keyinfo,father_page,DFLT_INIT_HITS,father_buff))
672
720
      goto err;
673
 
    return(0);
 
721
    DBUG_RETURN(0);
674
722
  }
675
723
 
676
724
        /* curr_buff[] and buff[] are full, lets split and make new nod */
681
729
    new_left_length-=curr_keylength;
682
730
  extra_length=nod_flag+left_length+right_length-
683
731
    new_left_length-new_right_length-curr_keylength;
 
732
  DBUG_PRINT("info",("left_length: %d  right_length: %d  new_left_length: %d  new_right_length: %d  extra_length: %d",
 
733
                     left_length, right_length,
 
734
                     new_left_length, new_right_length,
 
735
                     extra_length));
684
736
  mi_putint(curr_buff,new_left_length,nod_flag);
685
737
  mi_putint(buff,new_right_length,nod_flag);
686
738
  mi_putint(extra_buff,extra_length+2,nod_flag);
687
739
 
688
740
  /* move first largest keys to new page  */
689
741
  pos=buff+right_length-extra_length;
690
 
  memcpy(extra_buff+2, pos, extra_length);
 
742
  memcpy((uchar*) extra_buff+2,pos,(size_t) extra_length);
691
743
  /* Save new parting key */
692
744
  memcpy(tmp_part_key, pos-k_length,k_length);
693
745
  /* Make place for new keys */
694
 
  bmove_upp((unsigned char*) buff+new_right_length,(unsigned char*) pos-k_length,
 
746
  bmove_upp((uchar*) buff+new_right_length,(uchar*) pos-k_length,
695
747
            right_length-extra_length-k_length-2);
696
748
  /* Copy keys from left page */
697
749
  pos= curr_buff+new_left_length;
698
 
  length= left_length - new_left_length - k_length;
699
 
  memcpy(buff+2, pos+k_length, length);
 
750
  memcpy((uchar*) buff+2,(uchar*) pos+k_length,
 
751
         (size_t) (length=left_length-new_left_length-k_length));
700
752
  /* Copy old parting key */
701
 
  memcpy(buff+2+length, father_key_pos, k_length);
 
753
  memcpy((uchar*) buff+2+length,father_key_pos,(size_t) k_length);
702
754
 
703
755
  /* Move new parting keys up to caller */
704
 
  memcpy((right ? key : father_key_pos), pos, k_length);
705
 
  memcpy((right ? father_key_pos : key), tmp_part_key, k_length);
 
756
  memcpy((uchar*) (right ? key : father_key_pos),pos,(size_t) k_length);
 
757
  memcpy((uchar*) (right ? father_key_pos : key),tmp_part_key, k_length);
706
758
 
707
759
  if ((new_pos=_mi_new(info,keyinfo,DFLT_INIT_HITS)) == HA_OFFSET_ERROR)
708
760
    goto err;
713
765
                        DFLT_INIT_HITS,extra_buff))
714
766
    goto err;
715
767
 
716
 
  return(1);                            /* Middle key up */
 
768
  DBUG_RETURN(1);                               /* Middle key up */
717
769
 
718
770
err:
719
 
  return(-1);
 
771
  DBUG_RETURN(-1);
720
772
} /* _mi_balance_page */
721
773
 
722
774
/**********************************************************************
725
777
 
726
778
typedef struct {
727
779
  MI_INFO *info;
728
 
  uint32_t keynr;
 
780
  uint keynr;
729
781
} bulk_insert_param;
730
782
 
731
 
int _mi_ck_write_tree(register MI_INFO *info, uint32_t keynr, unsigned char *key,
732
 
                      uint32_t key_length)
 
783
int _mi_ck_write_tree(register MI_INFO *info, uint keynr, uchar *key,
 
784
                      uint key_length)
733
785
{
734
786
  int error;
 
787
  DBUG_ENTER("_mi_ck_write_tree");
735
788
 
736
789
  error= tree_insert(&info->bulk_insert[keynr], key,
737
790
         key_length + info->s->rec_reflength,
738
791
         info->bulk_insert[keynr].custom_arg) ? 0 : HA_ERR_OUT_OF_MEM ;
739
792
 
740
 
  return(error);
 
793
  DBUG_RETURN(error);
741
794
} /* _mi_ck_write_tree */
742
795
 
743
796
 
744
797
/* typeof(_mi_keys_compare)=qsort_cmp2 */
745
798
 
746
 
static int keys_compare(bulk_insert_param *param, unsigned char *key1, unsigned char *key2)
 
799
static int keys_compare(bulk_insert_param *param, uchar *key1, uchar *key2)
747
800
{
748
 
  uint32_t not_used[2];
 
801
  uint not_used[2];
749
802
  return ha_key_cmp(param->info->s->keyinfo[param->keynr].seg,
750
803
                    key1, key2, USE_WHOLE_KEY, SEARCH_SAME,
751
804
                    not_used);
752
805
}
753
806
 
754
807
 
755
 
static int keys_free(unsigned char *key, TREE_FREE mode, bulk_insert_param *param)
 
808
static int keys_free(uchar *key, TREE_FREE mode, bulk_insert_param *param)
756
809
{
757
810
  /*
758
811
    Probably I can use info->lastkey here, but I'm not sure,
759
812
    and to be safe I'd better use local lastkey.
760
813
  */
761
 
  unsigned char lastkey[MI_MAX_KEY_BUFF];
762
 
  uint32_t keylen;
 
814
  uchar lastkey[MI_MAX_KEY_BUFF];
 
815
  uint keylen;
763
816
  MI_KEYDEF *keyinfo;
764
817
 
765
818
  switch (mode) {
785
838
}
786
839
 
787
840
 
788
 
int mi_init_bulk_insert(MI_INFO *info, uint32_t cache_size, ha_rows rows)
 
841
int mi_init_bulk_insert(MI_INFO *info, ulong cache_size, ha_rows rows)
789
842
{
790
843
  MYISAM_SHARE *share=info->s;
791
844
  MI_KEYDEF *key=share->keyinfo;
792
845
  bulk_insert_param *params;
793
 
  uint32_t i, num_keys, total_keylength;
794
 
  uint64_t key_map;
 
846
  uint i, num_keys, total_keylength;
 
847
  ulonglong key_map;
 
848
  DBUG_ENTER("_mi_init_bulk_insert");
 
849
  DBUG_PRINT("enter",("cache_size: %lu", cache_size));
795
850
 
796
 
  assert(!info->bulk_insert &&
 
851
  DBUG_ASSERT(!info->bulk_insert &&
797
852
              (!rows || rows >= MI_MIN_ROWS_TO_USE_BULK_INSERT));
798
853
 
799
854
  mi_clear_all_keys_active(key_map);
810
865
 
811
866
  if (num_keys==0 ||
812
867
      num_keys * MI_MIN_SIZE_BULK_INSERT_TREE > cache_size)
813
 
    return(0);
 
868
    DBUG_RETURN(0);
814
869
 
815
870
  if (rows && rows*total_keylength < cache_size)
816
 
    cache_size= (uint32_t)rows;
 
871
    cache_size= (ulong)rows;
817
872
  else
818
873
    cache_size/=total_keylength*16;
819
874
 
822
877
               sizeof(bulk_insert_param)*num_keys),MYF(0));
823
878
 
824
879
  if (!info->bulk_insert)
825
 
    return(HA_ERR_OUT_OF_MEM);
 
880
    DBUG_RETURN(HA_ERR_OUT_OF_MEM);
826
881
 
827
882
  params=(bulk_insert_param *)(info->bulk_insert+share->base.keys);
828
883
  for (i=0 ; i < share->base.keys ; i++)
842
897
     info->bulk_insert[i].root=0;
843
898
  }
844
899
 
845
 
  return(0);
 
900
  DBUG_RETURN(0);
846
901
}
847
902
 
848
 
void mi_flush_bulk_insert(MI_INFO *info, uint32_t inx)
 
903
void mi_flush_bulk_insert(MI_INFO *info, uint inx)
849
904
{
850
905
  if (info->bulk_insert)
851
906
  {
858
913
{
859
914
  if (info->bulk_insert)
860
915
  {
861
 
    uint32_t i;
 
916
    uint i;
862
917
    for (i=0 ; i < info->s->base.keys ; i++)
863
918
    {
864
919
      if (is_tree_inited(& info->bulk_insert[i]))
866
921
        delete_tree(& info->bulk_insert[i]);
867
922
      }
868
923
    }
869
 
    free((void *)info->bulk_insert);
 
924
    my_free((void *)info->bulk_insert, MYF(0));
870
925
    info->bulk_insert=0;
871
926
  }
872
927
}