~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/myisam/mi_write.c

  • Committer: Monty Taylor
  • Date: 2008-07-15 21:40:58 UTC
  • mfrom: (77.1.113 codestyle32)
  • mto: This revision was merged to the branch mainline in revision 176.
  • Revision ID: mordred@camelot-20080715214058-rm3phulldos9xehv
Merged from codestyle.

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