~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/myisam/mi_write.c

  • Committer: Monty Taylor
  • Date: 2008-10-16 06:32:30 UTC
  • mto: (511.1.5 codestyle)
  • mto: This revision was merged to the branch mainline in revision 521.
  • Revision ID: monty@inaugust.com-20081016063230-4brxsra0qsmsg84q
Added -Wunused-macros.

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 "fulltext.h"
19
 
#include "rt_index.h"
 
18
#include "myisamdef.h"
 
19
#include <drizzled/util/test.h>
 
20
 
20
21
 
21
22
#define MAX_POINTER_LENGTH 8
22
23
 
23
24
        /* Functions declared in this file */
24
25
 
25
26
static int w_search(MI_INFO *info,MI_KEYDEF *keyinfo,
26
 
                    uint comp_flag, uchar *key,
27
 
                    uint key_length, my_off_t pos, uchar *father_buff,
28
 
                    uchar *father_keypos, my_off_t father_page,
29
 
                    my_bool insert_last);
30
 
static int _mi_balance_page(MI_INFO *info,MI_KEYDEF *keyinfo,uchar *key,
31
 
                            uchar *curr_buff,uchar *father_buff,
32
 
                            uchar *father_keypos,my_off_t father_page);
33
 
static uchar *_mi_find_last_pos(MI_KEYDEF *keyinfo, uchar *page,
34
 
                                uchar *key, uint *return_key_length,
35
 
                                uchar **after_key);
36
 
int _mi_ck_write_tree(register MI_INFO *info, uint keynr,uchar *key,
37
 
                      uint key_length);
38
 
int _mi_ck_write_btree(register MI_INFO *info, uint keynr,uchar *key,
39
 
                       uint key_length);
 
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);
40
41
 
41
42
        /* Write new record to database */
42
43
 
43
 
int mi_write(MI_INFO *info, uchar *record)
 
44
int mi_write(MI_INFO *info, unsigned char *record)
44
45
{
45
46
  MYISAM_SHARE *share=info->s;
46
 
  uint i;
 
47
  uint32_t i;
47
48
  int save_errno;
48
49
  my_off_t filepos;
49
 
  uchar *buff;
50
 
  my_bool lock_tree= share->concurrent_insert;
51
 
  DBUG_ENTER("mi_write");
52
 
  DBUG_PRINT("enter",("isam: %d  data: %d",info->s->kfile,info->dfile));
 
50
  unsigned char *buff;
 
51
  bool lock_tree= share->concurrent_insert;
53
52
 
54
 
  DBUG_EXECUTE_IF("myisam_pretend_crashed_table_on_usage",
55
 
                  mi_print_error(info->s, HA_ERR_CRASHED);
56
 
                  DBUG_RETURN(my_errno= HA_ERR_CRASHED););
57
53
  if (share->options & HA_OPTION_READ_ONLY_DATA)
58
54
  {
59
 
    DBUG_RETURN(my_errno=EACCES);
 
55
    return(my_errno=EACCES);
60
56
  }
61
57
  if (_mi_readinfo(info,F_WRLCK,1))
62
 
    DBUG_RETURN(my_errno);
63
 
  dont_break();                         /* Dont allow SIGHUP or SIGINT */
64
 
#if !defined(NO_LOCKING) && defined(USE_RECORD_LOCK)
65
 
  if (!info->locked && my_lock(info->dfile,F_WRLCK,0L,F_TO_EOF,
66
 
                               MYF(MY_SEEK_NOT_DONE) | info->lock_wait))
67
 
    goto err;
68
 
#endif
 
58
    return(my_errno);
69
59
  filepos= ((share->state.dellink != HA_OFFSET_ERROR &&
70
60
             !info->append_insert_at_end) ?
71
61
            share->state.dellink :
102
92
  {
103
93
    if (mi_is_key_active(share->state.key_map, i))
104
94
    {
105
 
      my_bool local_lock_tree= (lock_tree &&
 
95
      bool local_lock_tree= (lock_tree &&
106
96
                                !(info->bulk_insert &&
107
97
                                  is_tree_inited(&info->bulk_insert[i])));
108
98
      if (local_lock_tree)
110
100
        rw_wrlock(&share->key_root_lock[i]);
111
101
        share->keyinfo[i].version++;
112
102
      }
113
 
      if (share->keyinfo[i].flag & HA_FULLTEXT )
114
 
      {
115
 
        if (_mi_ft_add(info,i, buff, record, filepos))
116
 
        {
117
 
          if (local_lock_tree)
118
 
            rw_unlock(&share->key_root_lock[i]);
119
 
          DBUG_PRINT("error",("Got error: %d on write",my_errno));
120
 
          goto err;
121
 
        }
122
 
      }
123
 
      else
124
103
      {
125
104
        if (share->keyinfo[i].ck_insert(info,i,buff,
126
105
                        _mi_make_key(info,i,buff,record,filepos)))
127
106
        {
128
107
          if (local_lock_tree)
129
108
            rw_unlock(&share->key_root_lock[i]);
130
 
          DBUG_PRINT("error",("Got error: %d on write",my_errno));
131
109
          goto err;
132
110
        }
133
111
      }
154
132
                 HA_STATE_ROW_CHANGED);
155
133
  info->state->records++;
156
134
  info->lastpos=filepos;
157
 
  myisam_log_record(MI_LOG_WRITE,info,record,filepos,0);
158
 
  VOID(_mi_writeinfo(info, WRITEINFO_UPDATE_KEYFILE));
 
135
  _mi_writeinfo(info, WRITEINFO_UPDATE_KEYFILE);
159
136
  if (info->invalidator != 0)
160
137
  {
161
 
    DBUG_PRINT("info", ("invalidator... '%s' (update)", info->filename));
162
138
    (*info->invalidator)(info->filename);
163
139
    info->invalidator=0;
164
140
  }
174
150
  if (share->is_log_table)
175
151
    mi_update_status((void*) info);
176
152
 
177
 
  allow_break();                                /* Allow SIGHUP & SIGINT */
178
 
  DBUG_RETURN(0);
 
153
  return(0);
179
154
 
180
155
err:
181
156
  save_errno=my_errno;
184
159
  {
185
160
    if (info->bulk_insert)
186
161
    {
187
 
      uint j;
 
162
      uint32_t j;
188
163
      for (j=0 ; j < share->base.keys ; j++)
189
164
        mi_flush_bulk_insert(info, j);
190
165
    }
193
168
    {
194
169
      if (mi_is_key_active(share->state.key_map, i))
195
170
      {
196
 
        my_bool local_lock_tree= (lock_tree &&
 
171
        bool local_lock_tree= (lock_tree &&
197
172
                                  !(info->bulk_insert &&
198
173
                                    is_tree_inited(&info->bulk_insert[i])));
199
174
        if (local_lock_tree)
200
175
          rw_wrlock(&share->key_root_lock[i]);
201
 
        if (share->keyinfo[i].flag & HA_FULLTEXT)
202
 
        {
203
 
          if (_mi_ft_del(info,i, buff,record,filepos))
204
 
          {
205
 
            if (local_lock_tree)
206
 
              rw_unlock(&share->key_root_lock[i]);
207
 
            break;
208
 
          }
209
 
        }
210
 
        else
211
176
        {
212
 
          uint key_length=_mi_make_key(info,i,buff,record,filepos);
 
177
          uint32_t key_length=_mi_make_key(info,i,buff,record,filepos);
213
178
          if (_mi_ck_delete(info,i,buff,key_length))
214
179
          {
215
180
            if (local_lock_tree)
231
196
  my_errno=save_errno;
232
197
err2:
233
198
  save_errno=my_errno;
234
 
  myisam_log_record(MI_LOG_WRITE,info,record,filepos,my_errno);
235
 
  VOID(_mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE));
236
 
  allow_break();                        /* Allow SIGHUP & SIGINT */
237
 
  DBUG_RETURN(my_errno=save_errno);
 
199
  _mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE);
 
200
  return(my_errno=save_errno);
238
201
} /* mi_write */
239
202
 
240
203
 
241
204
        /* Write one key to btree */
242
205
 
243
 
int _mi_ck_write(MI_INFO *info, uint keynr, uchar *key, uint key_length)
 
206
int _mi_ck_write(MI_INFO *info, uint32_t keynr, unsigned char *key, uint32_t key_length)
244
207
{
245
 
  DBUG_ENTER("_mi_ck_write");
246
 
 
247
208
  if (info->bulk_insert && is_tree_inited(&info->bulk_insert[keynr]))
248
209
  {
249
 
    DBUG_RETURN(_mi_ck_write_tree(info, keynr, key, key_length));
 
210
    return(_mi_ck_write_tree(info, keynr, key, key_length));
250
211
  }
251
212
  else
252
213
  {
253
 
    DBUG_RETURN(_mi_ck_write_btree(info, keynr, key, key_length));
 
214
    return(_mi_ck_write_btree(info, keynr, key, key_length));
254
215
  }
255
216
} /* _mi_ck_write */
256
217
 
259
220
 *                Normal insert code                                  *
260
221
 **********************************************************************/
261
222
 
262
 
int _mi_ck_write_btree(register MI_INFO *info, uint keynr, uchar *key,
263
 
                       uint key_length)
 
223
int _mi_ck_write_btree(register MI_INFO *info, uint32_t keynr, unsigned char *key,
 
224
                       uint32_t key_length)
264
225
{
265
 
  int error;
266
 
  uint comp_flag;
 
226
  uint32_t error;
 
227
  uint32_t comp_flag;
267
228
  MI_KEYDEF *keyinfo=info->s->keyinfo+keynr;
268
229
  my_off_t  *root=&info->s->state.key_root[keynr];
269
 
  DBUG_ENTER("_mi_ck_write_btree");
270
230
 
271
231
  if (keyinfo->flag & HA_SORT_ALLOWS_SAME)
272
232
    comp_flag=SEARCH_BIGGER;                    /* Put after same key */
273
 
  else if (keyinfo->flag & (HA_NOSAME|HA_FULLTEXT))
 
233
  else if (keyinfo->flag & (HA_NOSAME))
274
234
  {
275
235
    comp_flag=SEARCH_FIND | SEARCH_UPDATE;      /* No duplicates */
276
236
    if (keyinfo->flag & HA_NULL_ARE_EQUAL)
281
241
 
282
242
  error=_mi_ck_real_write_btree(info, keyinfo, key, key_length,
283
243
                                root, comp_flag);
284
 
  if (info->ft1_to_ft2)
285
 
  {
286
 
    if (!error)
287
 
      error= _mi_ft_convert_to_ft2(info, keynr, key);
288
 
    delete_dynamic(info->ft1_to_ft2);
289
 
    my_free((uchar*)info->ft1_to_ft2, MYF(0));
290
 
    info->ft1_to_ft2=0;
291
 
  }
292
 
  DBUG_RETURN(error);
 
244
  return(error);
293
245
} /* _mi_ck_write_btree */
294
246
 
295
247
int _mi_ck_real_write_btree(MI_INFO *info, MI_KEYDEF *keyinfo,
296
 
    uchar *key, uint key_length, my_off_t *root, uint comp_flag)
 
248
    unsigned char *key, uint32_t key_length, my_off_t *root, uint32_t comp_flag)
297
249
{
298
250
  int error;
299
 
  DBUG_ENTER("_mi_ck_real_write_btree");
300
251
  /* key_length parameter is used only if comp_flag is SEARCH_FIND */
301
252
  if (*root == HA_OFFSET_ERROR ||
302
253
      (error=w_search(info, keyinfo, comp_flag, key, key_length,
303
 
                      *root, (uchar *) 0, (uchar*) 0,
 
254
                      *root, (unsigned char *) 0, (unsigned char*) 0,
304
255
                      (my_off_t) 0, 1)) > 0)
305
256
    error=_mi_enlarge_root(info,keyinfo,key,root);
306
 
  DBUG_RETURN(error);
 
257
  return(error);
307
258
} /* _mi_ck_real_write_btree */
308
259
 
309
260
 
310
261
        /* Make a new root with key as only pointer */
311
262
 
312
 
int _mi_enlarge_root(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *key,
 
263
int _mi_enlarge_root(MI_INFO *info, MI_KEYDEF *keyinfo, unsigned char *key,
313
264
                     my_off_t *root)
314
265
{
315
 
  uint t_length,nod_flag;
 
266
  uint32_t t_length,nod_flag;
316
267
  MI_KEY_PARAM s_temp;
317
268
  MYISAM_SHARE *share=info->s;
318
 
  DBUG_ENTER("_mi_enlarge_root");
319
269
 
320
270
  nod_flag= (*root != HA_OFFSET_ERROR) ?  share->base.key_reflength : 0;
321
271
  _mi_kpointer(info,info->buff+2,*root); /* if nod */
322
 
  t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,(uchar*) 0,
323
 
                                (uchar*) 0, (uchar*) 0, key,&s_temp);
 
272
  t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,(unsigned char*) 0,
 
273
                                (unsigned char*) 0, (unsigned char*) 0, key,&s_temp);
324
274
  mi_putint(info->buff,t_length+2+nod_flag,nod_flag);
325
275
  (*keyinfo->store_key)(keyinfo,info->buff+2+nod_flag,&s_temp);
326
276
  info->buff_used=info->page_changed=1;         /* info->buff is used */
327
277
  if ((*root= _mi_new(info,keyinfo,DFLT_INIT_HITS)) == HA_OFFSET_ERROR ||
328
278
      _mi_write_keypage(info,keyinfo,*root,DFLT_INIT_HITS,info->buff))
329
 
    DBUG_RETURN(-1);
330
 
  DBUG_RETURN(0);
 
279
    return(-1);
 
280
  return(0);
331
281
} /* _mi_enlarge_root */
332
282
 
333
283
 
339
289
        */
340
290
 
341
291
static int w_search(register MI_INFO *info, register MI_KEYDEF *keyinfo,
342
 
                    uint comp_flag, uchar *key, uint key_length, my_off_t page,
343
 
                    uchar *father_buff, uchar *father_keypos,
344
 
                    my_off_t father_page, my_bool insert_last)
 
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)
345
295
{
346
296
  int error,flag;
347
 
  uint nod_flag, search_key_length;
348
 
  uchar *temp_buff,*keypos;
349
 
  uchar keybuff[MI_MAX_KEY_BUFF];
350
 
  my_bool was_last_key;
 
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;
351
301
  my_off_t next_page, dupp_key_pos;
352
 
  DBUG_ENTER("w_search");
353
 
  DBUG_PRINT("enter",("page: %ld", (long) page));
354
302
 
355
303
  search_key_length= (comp_flag & SEARCH_FIND) ? key_length : USE_WHOLE_KEY;
356
 
  if (!(temp_buff= (uchar*) my_alloca((uint) keyinfo->block_length+
 
304
  if (!(temp_buff= (unsigned char*) my_alloca((uint) keyinfo->block_length+
357
305
                                      MI_MAX_KEY_BUFF*2)))
358
 
    DBUG_RETURN(-1);
 
306
    return(-1);
359
307
  if (!_mi_fetch_keypage(info,keyinfo,page,DFLT_INIT_HITS,temp_buff,0))
360
308
    goto err;
361
309
 
364
312
  nod_flag=mi_test_if_nod(temp_buff);
365
313
  if (flag == 0)
366
314
  {
367
 
    uint tmp_key_length;
 
315
    uint32_t tmp_key_length;
368
316
        /* get position to record with duplicated key */
369
317
    tmp_key_length=(*keyinfo->get_key)(keyinfo,nod_flag,&keypos,keybuff);
370
318
    if (tmp_key_length)
372
320
    else
373
321
      dupp_key_pos= HA_OFFSET_ERROR;
374
322
 
375
 
    if (keyinfo->flag & HA_FULLTEXT)
376
 
    {
377
 
      uint off;
378
 
      int  subkeys;
379
 
 
380
 
      get_key_full_length_rdonly(off, keybuff);
381
 
      subkeys=ft_sintXkorr(keybuff+off);
382
 
      comp_flag=SEARCH_SAME;
383
 
      if (subkeys >= 0)
384
 
      {
385
 
        /* normal word, one-level tree structure */
386
 
        flag=(*keyinfo->bin_search)(info, keyinfo, temp_buff, key,
387
 
                                    USE_WHOLE_KEY, comp_flag,
388
 
                                    &keypos, keybuff, &was_last_key);
389
 
      }
390
 
      else
391
 
      {
392
 
        /* popular word. two-level tree. going down */
393
 
        my_off_t root=dupp_key_pos;
394
 
        keyinfo=&info->s->ft2_keyinfo;
395
 
        get_key_full_length_rdonly(off, key);
396
 
        key+=off;
397
 
        keypos-=keyinfo->keylength+nod_flag; /* we'll modify key entry 'in vivo' */
398
 
        error=_mi_ck_real_write_btree(info, keyinfo, key, 0,
399
 
                                      &root, comp_flag);
400
 
        _mi_dpointer(info, keypos+HA_FT_WLEN, root);
401
 
        subkeys--; /* should there be underflow protection ? */
402
 
        DBUG_ASSERT(subkeys < 0);
403
 
        ft_intXstore(keypos, subkeys);
404
 
        if (!error)
405
 
          error=_mi_write_keypage(info,keyinfo,page,DFLT_INIT_HITS,temp_buff);
406
 
        my_afree((uchar*) temp_buff);
407
 
        DBUG_RETURN(error);
408
 
      }
409
 
    }
410
 
    else /* not HA_FULLTEXT, normal HA_NOSAME key */
411
323
    {
412
324
      info->dupp_key_pos= dupp_key_pos;
413
 
      my_afree((uchar*) temp_buff);
 
325
      my_afree((unsigned char*) temp_buff);
414
326
      my_errno=HA_ERR_FOUND_DUPP_KEY;
415
 
      DBUG_RETURN(-1);
 
327
      return(-1);
416
328
    }
417
329
  }
418
330
  if (flag == MI_FOUND_WRONG_KEY)
419
 
    DBUG_RETURN(-1);
 
331
    return(-1);
420
332
  if (!was_last_key)
421
333
    insert_last=0;
422
334
  next_page=_mi_kpos(nod_flag,keypos);
429
341
    if (_mi_write_keypage(info,keyinfo,page,DFLT_INIT_HITS,temp_buff))
430
342
      goto err;
431
343
  }
432
 
  my_afree((uchar*) temp_buff);
433
 
  DBUG_RETURN(error);
 
344
  my_afree((unsigned char*) temp_buff);
 
345
  return(error);
434
346
err:
435
 
  my_afree((uchar*) temp_buff);
436
 
  DBUG_PRINT("exit",("Error: %d",my_errno));
437
 
  DBUG_RETURN (-1);
 
347
  my_afree((unsigned char*) temp_buff);
 
348
  return (-1);
438
349
} /* w_search */
439
350
 
440
351
 
464
375
*/
465
376
 
466
377
int _mi_insert(register MI_INFO *info, register MI_KEYDEF *keyinfo,
467
 
               uchar *key, uchar *anc_buff, uchar *key_pos, uchar *key_buff,
468
 
               uchar *father_buff, uchar *father_key_pos, my_off_t father_page,
469
 
               my_bool insert_last)
 
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)
470
381
{
471
 
  uint a_length,nod_flag;
 
382
  uint32_t a_length,nod_flag;
472
383
  int t_length;
473
 
  uchar *endpos, *prev_key;
 
384
  unsigned char *endpos, *prev_key;
474
385
  MI_KEY_PARAM s_temp;
475
 
  DBUG_ENTER("_mi_insert");
476
 
  DBUG_PRINT("enter",("key_pos: 0x%lx", (long) key_pos));
477
 
  DBUG_EXECUTE("key",_mi_print_key(DBUG_FILE,keyinfo->seg,key,USE_WHOLE_KEY););
478
386
 
479
387
  nod_flag=mi_test_if_nod(anc_buff);
480
388
  a_length=mi_getint(anc_buff);
481
389
  endpos= anc_buff+ a_length;
482
 
  prev_key=(key_pos == anc_buff+2+nod_flag ? (uchar*) 0 : key_buff);
 
390
  prev_key=(key_pos == anc_buff+2+nod_flag ? (unsigned char*) 0 : key_buff);
483
391
  t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,
484
 
                                (key_pos == endpos ? (uchar*) 0 : key_pos),
 
392
                                (key_pos == endpos ? (unsigned char*) 0 : key_pos),
485
393
                                prev_key, prev_key,
486
394
                                key,&s_temp);
487
 
#ifndef DBUG_OFF
488
 
  if (key_pos != anc_buff+2+nod_flag && (keyinfo->flag &
489
 
                                         (HA_BINARY_PACK_KEY | HA_PACK_KEY)))
490
 
  {
491
 
    DBUG_DUMP("prev_key",(uchar*) key_buff,_mi_keylength(keyinfo,key_buff));
492
 
  }
493
 
  if (keyinfo->flag & HA_PACK_KEY)
494
 
  {
495
 
    DBUG_PRINT("test",("t_length: %d  ref_len: %d",
496
 
                       t_length,s_temp.ref_length));
497
 
    DBUG_PRINT("test",("n_ref_len: %d  n_length: %d  key_pos: 0x%lx",
498
 
                       s_temp.n_ref_length,s_temp.n_length, (long) s_temp.key));
499
 
  }
500
 
#endif
 
395
 
501
396
  if (t_length > 0)
502
397
  {
503
398
    if (t_length >= keyinfo->maxlength*2+MAX_POINTER_LENGTH)
504
399
    {
505
400
      mi_print_error(info->s, HA_ERR_CRASHED);
506
401
      my_errno=HA_ERR_CRASHED;
507
 
      DBUG_RETURN(-1);
 
402
      return(-1);
508
403
    }
509
 
    bmove_upp((uchar*) endpos+t_length,(uchar*) endpos,(uint) (endpos-key_pos));
 
404
    bmove_upp((unsigned char*) endpos+t_length,(unsigned char*) endpos,(uint) (endpos-key_pos));
510
405
  }
511
406
  else
512
407
  {
514
409
    {
515
410
      mi_print_error(info->s, HA_ERR_CRASHED);
516
411
      my_errno=HA_ERR_CRASHED;
517
 
      DBUG_RETURN(-1);
 
412
      return(-1);
518
413
    }
519
 
    bmove(key_pos,key_pos-t_length,(uint) (endpos-key_pos)+t_length);
 
414
    memcpy(key_pos, key_pos - t_length, endpos - key_pos + t_length);
520
415
  }
521
416
  (*keyinfo->store_key)(keyinfo,key_pos,&s_temp);
522
417
  a_length+=t_length;
523
418
  mi_putint(anc_buff,a_length,nod_flag);
524
419
  if (a_length <= keyinfo->block_length)
525
420
  {
526
 
    if (keyinfo->block_length - a_length < 32 &&
527
 
        keyinfo->flag & HA_FULLTEXT && key_pos == endpos &&
528
 
        info->s->base.key_reflength <= info->s->base.rec_reflength &&
529
 
        info->s->options & (HA_OPTION_PACK_RECORD | HA_OPTION_COMPRESS_RECORD))
530
 
    {
531
 
      /*
532
 
        Normal word. One-level tree. Page is almost full.
533
 
        Let's consider converting.
534
 
        We'll compare 'key' and the first key at anc_buff
535
 
       */
536
 
      uchar *a=key, *b=anc_buff+2+nod_flag;
537
 
      uint alen, blen, ft2len=info->s->ft2_keyinfo.keylength;
538
 
      /* the very first key on the page is always unpacked */
539
 
      DBUG_ASSERT((*b & 128) == 0);
540
 
#if HA_FT_MAXLEN >= 127
541
 
      blen= mi_uint2korr(b); b+=2;
542
 
#else
543
 
      blen= *b++;
544
 
#endif
545
 
      get_key_length(alen,a);
546
 
      DBUG_ASSERT(info->ft1_to_ft2==0);
547
 
      if (alen == blen &&
548
 
          ha_compare_text(keyinfo->seg->charset, a, alen, b, blen, 0, 0)==0)
549
 
      {
550
 
        /* yup. converting */
551
 
        info->ft1_to_ft2=(DYNAMIC_ARRAY *)
552
 
          my_malloc(sizeof(DYNAMIC_ARRAY), MYF(MY_WME));
553
 
        my_init_dynamic_array(info->ft1_to_ft2, ft2len, 300, 50);
554
 
 
555
 
        /*
556
 
          now, adding all keys from the page to dynarray
557
 
          if the page is a leaf (if not keys will be deleted later)
558
 
        */
559
 
        if (!nod_flag)
560
 
        {
561
 
          /* let's leave the first key on the page, though, because
562
 
             we cannot easily dispatch an empty page here */
563
 
          b+=blen+ft2len+2;
564
 
          for (a=anc_buff+a_length ; b < a ; b+=ft2len+2)
565
 
            insert_dynamic(info->ft1_to_ft2, b);
566
 
 
567
 
          /* fixing the page's length - it contains only one key now */
568
 
          mi_putint(anc_buff,2+blen+ft2len+2,0);
569
 
        }
570
 
        /* the rest will be done when we're back from recursion */
571
 
      }
572
 
    }
573
 
    DBUG_RETURN(0);                             /* There is room on page */
 
421
    return(0);                          /* There is room on page */
574
422
  }
575
423
  /* Page is full */
576
424
  if (nod_flag)
577
425
    insert_last=0;
578
426
  if (!(keyinfo->flag & (HA_VAR_LENGTH_KEY | HA_BINARY_PACK_KEY)) &&
579
427
      father_buff && !insert_last)
580
 
    DBUG_RETURN(_mi_balance_page(info,keyinfo,key,anc_buff,father_buff,
 
428
    return(_mi_balance_page(info,keyinfo,key,anc_buff,father_buff,
581
429
                                 father_key_pos,father_page));
582
 
  DBUG_RETURN(_mi_split_page(info,keyinfo,key,anc_buff,key_buff, insert_last));
 
430
  return(_mi_split_page(info,keyinfo,key,anc_buff,key_buff, insert_last));
583
431
} /* _mi_insert */
584
432
 
585
433
 
586
434
        /* split a full page in two and assign emerging item to key */
587
435
 
588
436
int _mi_split_page(register MI_INFO *info, register MI_KEYDEF *keyinfo,
589
 
                   uchar *key, uchar *buff, uchar *key_buff,
590
 
                   my_bool insert_last_key)
 
437
                   unsigned char *key, unsigned char *buff, unsigned char *key_buff,
 
438
                   bool insert_last_key)
591
439
{
592
 
  uint length,a_length,key_ref_length,t_length,nod_flag,key_length;
593
 
  uchar *key_pos,*pos, *after_key= NULL;
 
440
  uint32_t length,a_length,key_ref_length,t_length,nod_flag,key_length;
 
441
  unsigned char *key_pos,*pos, *after_key= NULL;
594
442
  my_off_t new_pos;
595
443
  MI_KEY_PARAM s_temp;
596
 
  DBUG_ENTER("mi_split_page");
597
 
  DBUG_DUMP("buff",(uchar*) buff,mi_getint(buff));
598
444
 
599
445
  if (info->s->keyinfo+info->lastinx == keyinfo)
600
446
    info->page_changed=1;                       /* Info->buff is used */
607
453
    key_pos=_mi_find_half_pos(nod_flag,keyinfo,buff,key_buff, &key_length,
608
454
                              &after_key);
609
455
  if (!key_pos)
610
 
    DBUG_RETURN(-1);
 
456
    return(-1);
611
457
 
612
458
  length=(uint) (key_pos-buff);
613
459
  a_length=mi_getint(buff);
616
462
  key_pos=after_key;
617
463
  if (nod_flag)
618
464
  {
619
 
    DBUG_PRINT("test",("Splitting nod"));
620
465
    pos=key_pos-nod_flag;
621
 
    memcpy((uchar*) info->buff+2,(uchar*) pos,(size_t) nod_flag);
 
466
    memcpy(info->buff + 2, pos, nod_flag);
622
467
  }
623
468
 
624
469
        /* Move middle item to key and pointer to new page */
625
470
  if ((new_pos=_mi_new(info,keyinfo,DFLT_INIT_HITS)) == HA_OFFSET_ERROR)
626
 
    DBUG_RETURN(-1);
 
471
    return(-1);
627
472
  _mi_kpointer(info,_mi_move_key(keyinfo,key,key_buff),new_pos);
628
473
 
629
474
        /* Store new page */
630
475
  if (!(*keyinfo->get_key)(keyinfo,nod_flag,&key_pos,key_buff))
631
 
    DBUG_RETURN(-1);
 
476
    return(-1);
632
477
 
633
 
  t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,(uchar *) 0,
634
 
                                (uchar*) 0, (uchar*) 0,
 
478
  t_length=(*keyinfo->pack_key)(keyinfo,nod_flag,(unsigned char *) 0,
 
479
                                (unsigned char*) 0, (unsigned char*) 0,
635
480
                                key_buff, &s_temp);
636
481
  length=(uint) ((buff+a_length)-key_pos);
637
 
  memcpy((uchar*) info->buff+key_ref_length+t_length,(uchar*) key_pos,
638
 
         (size_t) length);
 
482
  memcpy(info->buff+key_ref_length+t_length, key_pos, length);
639
483
  (*keyinfo->store_key)(keyinfo,info->buff+key_ref_length,&s_temp);
640
484
  mi_putint(info->buff,length+t_length+key_ref_length,nod_flag);
641
485
 
642
486
  if (_mi_write_keypage(info,keyinfo,new_pos,DFLT_INIT_HITS,info->buff))
643
 
    DBUG_RETURN(-1);
644
 
  DBUG_DUMP("key",(uchar*) key,_mi_keylength(keyinfo,key));
645
 
  DBUG_RETURN(2);                               /* Middle key up */
 
487
    return(-1);
 
488
  return(2);                            /* Middle key up */
646
489
} /* _mi_split_page */
647
490
 
648
491
 
654
497
          after_key will contain the position to where the next key starts
655
498
        */
656
499
 
657
 
uchar *_mi_find_half_pos(uint nod_flag, MI_KEYDEF *keyinfo, uchar *page,
658
 
                         uchar *key, uint *return_key_length,
659
 
                         uchar **after_key)
 
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)
660
503
{
661
 
  uint keys,length,key_ref_length;
662
 
  uchar *end,*lastpos;
663
 
  DBUG_ENTER("_mi_find_half_pos");
 
504
  uint32_t keys,length,key_ref_length;
 
505
  unsigned char *end,*lastpos;
664
506
 
665
507
  key_ref_length=2+nod_flag;
666
508
  length=mi_getint(page)-key_ref_length;
675
517
    end=page+keys*key_ref_length;
676
518
    *after_key=end+key_ref_length;
677
519
    memcpy(key,end,key_ref_length);
678
 
    DBUG_RETURN(end);
 
520
    return(end);
679
521
  }
680
522
 
681
523
  end=page+length/2-key_ref_length;             /* This is aprox. half */
684
526
  {
685
527
    lastpos=page;
686
528
    if (!(length=(*keyinfo->get_key)(keyinfo,nod_flag,&page,key)))
687
 
      DBUG_RETURN(0);
 
529
      return(0);
688
530
  } while (page < end);
689
531
  *return_key_length=length;
690
532
  *after_key=page;
691
 
  DBUG_PRINT("exit",("returns: 0x%lx  page: 0x%lx  half: 0x%lx",
692
 
                     (long) lastpos, (long) page, (long) end));
693
 
  DBUG_RETURN(lastpos);
 
533
  return(lastpos);
694
534
} /* _mi_find_half_pos */
695
535
 
696
536
 
700
540
          key will contain the last key
701
541
        */
702
542
 
703
 
static uchar *_mi_find_last_pos(MI_KEYDEF *keyinfo, uchar *page,
704
 
                                uchar *key, uint *return_key_length,
705
 
                                uchar **after_key)
 
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)
706
546
{
707
 
  uint keys, length, last_length, key_ref_length;
708
 
  uchar *end, *lastpos, *prevpos= NULL;
709
 
  uchar key_buff[MI_MAX_KEY_BUFF];
710
 
  DBUG_ENTER("_mi_find_last_pos");
 
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];
711
553
 
712
554
  key_ref_length=2;
713
555
  length=mi_getint(page)-key_ref_length;
721
563
    end=page+keys*length;
722
564
    *after_key=end+length;
723
565
    memcpy(key,end,length);
724
 
    DBUG_RETURN(end);
 
566
    return(end);
725
567
  }
726
568
 
727
569
  end= page + length - key_ref_length;
737
579
    {
738
580
      mi_print_error(keyinfo->share, HA_ERR_CRASHED);
739
581
      my_errno=HA_ERR_CRASHED;
740
 
      DBUG_RETURN(0);
 
582
      return(0);
741
583
    }
742
584
  }
743
585
  *return_key_length=last_length;
744
586
  *after_key=lastpos;
745
 
  DBUG_PRINT("exit",("returns: 0x%lx  page: 0x%lx  end: 0x%lx",
746
 
                     (long) prevpos,(long) page,(long) end));
747
 
  DBUG_RETURN(prevpos);
 
587
  return(prevpos);
748
588
} /* _mi_find_last_pos */
749
589
 
750
590
 
752
592
        /* returns 0 if balance was done */
753
593
 
754
594
static int _mi_balance_page(register MI_INFO *info, MI_KEYDEF *keyinfo,
755
 
                            uchar *key, uchar *curr_buff, uchar *father_buff,
756
 
                            uchar *father_key_pos, my_off_t father_page)
 
595
                            unsigned char *key, unsigned char *curr_buff, unsigned char *father_buff,
 
596
                            unsigned char *father_key_pos, my_off_t father_page)
757
597
{
758
 
  my_bool right;
759
 
  uint k_length,father_length,father_keylength,nod_flag,curr_keylength,
 
598
  bool right;
 
599
  uint32_t k_length,father_length,father_keylength,nod_flag,curr_keylength,
760
600
       right_length,left_length,new_right_length,new_left_length,extra_length,
761
601
       length,keys;
762
 
  uchar *pos,*buff,*extra_buff;
 
602
  unsigned char *pos,*buff,*extra_buff;
763
603
  my_off_t next_page,new_pos;
764
 
  uchar tmp_part_key[MI_MAX_KEY_BUFF];
765
 
  DBUG_ENTER("_mi_balance_page");
 
604
  unsigned char tmp_part_key[MI_MAX_KEY_BUFF];
766
605
 
767
606
  k_length=keyinfo->keylength;
768
607
  father_length=mi_getint(father_buff);
779
618
    next_page= _mi_kpos(info->s->base.key_reflength,
780
619
                        father_key_pos+father_keylength);
781
620
    buff=info->buff;
782
 
    DBUG_PRINT("test",("use right page: %lu", (ulong) next_page));
783
621
  }
784
622
  else
785
623
  {
788
626
    next_page= _mi_kpos(info->s->base.key_reflength,father_key_pos);
789
627
                                        /* Fix that curr_buff is to left */
790
628
    buff=curr_buff; curr_buff=info->buff;
791
 
    DBUG_PRINT("test",("use left page: %lu", (ulong) next_page));
792
629
  }                                     /* father_key_pos ptr to parting key */
793
630
 
794
631
  if (!_mi_fetch_keypage(info,keyinfo,next_page,DFLT_INIT_HITS,info->buff,0))
795
632
    goto err;
796
 
  DBUG_DUMP("next",(uchar*) info->buff,mi_getint(info->buff));
797
633
 
798
634
        /* Test if there is room to share keys */
799
635
 
812
648
    if (left_length < new_left_length)
813
649
    {                                           /* Move keys buff -> leaf */
814
650
      pos=curr_buff+left_length;
815
 
      memcpy((uchar*) pos,(uchar*) father_key_pos, (size_t) k_length);
816
 
      memcpy((uchar*) pos+k_length, (uchar*) buff+2,
817
 
             (size_t) (length=new_left_length - left_length - k_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);
818
654
      pos=buff+2+length;
819
 
      memcpy((uchar*) father_key_pos,(uchar*) pos,(size_t) k_length);
820
 
      bmove((uchar*) buff+2,(uchar*) pos+k_length,new_right_length);
 
655
      memcpy(father_key_pos, pos, k_length);
 
656
      memcpy(buff+2, pos+k_length, new_right_length);
821
657
    }
822
658
    else
823
659
    {                                           /* Move keys -> buff */
824
660
 
825
 
      bmove_upp((uchar*) buff+new_right_length,(uchar*) buff+right_length,
 
661
      bmove_upp((unsigned char*) buff+new_right_length,(unsigned char*) buff+right_length,
826
662
                right_length-2);
827
663
      length=new_right_length-right_length-k_length;
828
 
      memcpy((uchar*) buff+2+length,father_key_pos,(size_t) k_length);
 
664
      memcpy(buff+2+length,father_key_pos, k_length);
829
665
      pos=curr_buff+new_left_length;
830
 
      memcpy((uchar*) father_key_pos,(uchar*) pos,(size_t) k_length);
831
 
      memcpy((uchar*) buff+2,(uchar*) pos+k_length,(size_t) length);
 
666
      memcpy(father_key_pos, pos, k_length);
 
667
      memcpy(buff+2, pos+k_length, length);
832
668
    }
833
669
 
834
670
    if (_mi_write_keypage(info,keyinfo,next_page,DFLT_INIT_HITS,info->buff) ||
835
671
        _mi_write_keypage(info,keyinfo,father_page,DFLT_INIT_HITS,father_buff))
836
672
      goto err;
837
 
    DBUG_RETURN(0);
 
673
    return(0);
838
674
  }
839
675
 
840
676
        /* curr_buff[] and buff[] are full, lets split and make new nod */
845
681
    new_left_length-=curr_keylength;
846
682
  extra_length=nod_flag+left_length+right_length-
847
683
    new_left_length-new_right_length-curr_keylength;
848
 
  DBUG_PRINT("info",("left_length: %d  right_length: %d  new_left_length: %d  new_right_length: %d  extra_length: %d",
849
 
                     left_length, right_length,
850
 
                     new_left_length, new_right_length,
851
 
                     extra_length));
852
684
  mi_putint(curr_buff,new_left_length,nod_flag);
853
685
  mi_putint(buff,new_right_length,nod_flag);
854
686
  mi_putint(extra_buff,extra_length+2,nod_flag);
855
687
 
856
688
  /* move first largest keys to new page  */
857
689
  pos=buff+right_length-extra_length;
858
 
  memcpy((uchar*) extra_buff+2,pos,(size_t) extra_length);
 
690
  memcpy(extra_buff+2, pos, extra_length);
859
691
  /* Save new parting key */
860
692
  memcpy(tmp_part_key, pos-k_length,k_length);
861
693
  /* Make place for new keys */
862
 
  bmove_upp((uchar*) buff+new_right_length,(uchar*) pos-k_length,
 
694
  bmove_upp((unsigned char*) buff+new_right_length,(unsigned char*) pos-k_length,
863
695
            right_length-extra_length-k_length-2);
864
696
  /* Copy keys from left page */
865
697
  pos= curr_buff+new_left_length;
866
 
  memcpy((uchar*) buff+2,(uchar*) pos+k_length,
867
 
         (size_t) (length=left_length-new_left_length-k_length));
 
698
  length= left_length - new_left_length - k_length;
 
699
  memcpy(buff+2, pos+k_length, length);
868
700
  /* Copy old parting key */
869
 
  memcpy((uchar*) buff+2+length,father_key_pos,(size_t) k_length);
 
701
  memcpy(buff+2+length, father_key_pos, k_length);
870
702
 
871
703
  /* Move new parting keys up to caller */
872
 
  memcpy((uchar*) (right ? key : father_key_pos),pos,(size_t) k_length);
873
 
  memcpy((uchar*) (right ? father_key_pos : key),tmp_part_key, k_length);
 
704
  memcpy((right ? key : father_key_pos), pos, k_length);
 
705
  memcpy((right ? father_key_pos : key), tmp_part_key, k_length);
874
706
 
875
707
  if ((new_pos=_mi_new(info,keyinfo,DFLT_INIT_HITS)) == HA_OFFSET_ERROR)
876
708
    goto err;
881
713
                        DFLT_INIT_HITS,extra_buff))
882
714
    goto err;
883
715
 
884
 
  DBUG_RETURN(1);                               /* Middle key up */
 
716
  return(1);                            /* Middle key up */
885
717
 
886
718
err:
887
 
  DBUG_RETURN(-1);
 
719
  return(-1);
888
720
} /* _mi_balance_page */
889
721
 
890
722
/**********************************************************************
893
725
 
894
726
typedef struct {
895
727
  MI_INFO *info;
896
 
  uint keynr;
 
728
  uint32_t keynr;
897
729
} bulk_insert_param;
898
730
 
899
 
int _mi_ck_write_tree(register MI_INFO *info, uint keynr, uchar *key,
900
 
                      uint key_length)
 
731
int _mi_ck_write_tree(register MI_INFO *info, uint32_t keynr, unsigned char *key,
 
732
                      uint32_t key_length)
901
733
{
902
734
  int error;
903
 
  DBUG_ENTER("_mi_ck_write_tree");
904
735
 
905
736
  error= tree_insert(&info->bulk_insert[keynr], key,
906
737
         key_length + info->s->rec_reflength,
907
738
         info->bulk_insert[keynr].custom_arg) ? 0 : HA_ERR_OUT_OF_MEM ;
908
739
 
909
 
  DBUG_RETURN(error);
 
740
  return(error);
910
741
} /* _mi_ck_write_tree */
911
742
 
912
743
 
913
744
/* typeof(_mi_keys_compare)=qsort_cmp2 */
914
745
 
915
 
static int keys_compare(bulk_insert_param *param, uchar *key1, uchar *key2)
 
746
static int keys_compare(bulk_insert_param *param, unsigned char *key1, unsigned char *key2)
916
747
{
917
 
  uint not_used[2];
 
748
  uint32_t not_used[2];
918
749
  return ha_key_cmp(param->info->s->keyinfo[param->keynr].seg,
919
750
                    key1, key2, USE_WHOLE_KEY, SEARCH_SAME,
920
751
                    not_used);
921
752
}
922
753
 
923
754
 
924
 
static int keys_free(uchar *key, TREE_FREE mode, bulk_insert_param *param)
 
755
static int keys_free(unsigned char *key, TREE_FREE mode, bulk_insert_param *param)
925
756
{
926
757
  /*
927
758
    Probably I can use info->lastkey here, but I'm not sure,
928
759
    and to be safe I'd better use local lastkey.
929
760
  */
930
 
  uchar lastkey[MI_MAX_KEY_BUFF];
931
 
  uint keylen;
 
761
  unsigned char lastkey[MI_MAX_KEY_BUFF];
 
762
  uint32_t keylen;
932
763
  MI_KEYDEF *keyinfo;
933
764
 
934
765
  switch (mode) {
954
785
}
955
786
 
956
787
 
957
 
int mi_init_bulk_insert(MI_INFO *info, ulong cache_size, ha_rows rows)
 
788
int mi_init_bulk_insert(MI_INFO *info, uint32_t cache_size, ha_rows rows)
958
789
{
959
790
  MYISAM_SHARE *share=info->s;
960
791
  MI_KEYDEF *key=share->keyinfo;
961
792
  bulk_insert_param *params;
962
 
  uint i, num_keys, total_keylength;
963
 
  ulonglong key_map;
964
 
  DBUG_ENTER("_mi_init_bulk_insert");
965
 
  DBUG_PRINT("enter",("cache_size: %lu", cache_size));
 
793
  uint32_t i, num_keys, total_keylength;
 
794
  uint64_t key_map;
966
795
 
967
 
  DBUG_ASSERT(!info->bulk_insert &&
 
796
  assert(!info->bulk_insert &&
968
797
              (!rows || rows >= MI_MIN_ROWS_TO_USE_BULK_INSERT));
969
798
 
970
799
  mi_clear_all_keys_active(key_map);
981
810
 
982
811
  if (num_keys==0 ||
983
812
      num_keys * MI_MIN_SIZE_BULK_INSERT_TREE > cache_size)
984
 
    DBUG_RETURN(0);
 
813
    return(0);
985
814
 
986
815
  if (rows && rows*total_keylength < cache_size)
987
 
    cache_size= (ulong)rows;
 
816
    cache_size= (uint32_t)rows;
988
817
  else
989
818
    cache_size/=total_keylength*16;
990
819
 
993
822
               sizeof(bulk_insert_param)*num_keys),MYF(0));
994
823
 
995
824
  if (!info->bulk_insert)
996
 
    DBUG_RETURN(HA_ERR_OUT_OF_MEM);
 
825
    return(HA_ERR_OUT_OF_MEM);
997
826
 
998
827
  params=(bulk_insert_param *)(info->bulk_insert+share->base.keys);
999
828
  for (i=0 ; i < share->base.keys ; i++)
1013
842
     info->bulk_insert[i].root=0;
1014
843
  }
1015
844
 
1016
 
  DBUG_RETURN(0);
 
845
  return(0);
1017
846
}
1018
847
 
1019
 
void mi_flush_bulk_insert(MI_INFO *info, uint inx)
 
848
void mi_flush_bulk_insert(MI_INFO *info, uint32_t inx)
1020
849
{
1021
850
  if (info->bulk_insert)
1022
851
  {
1029
858
{
1030
859
  if (info->bulk_insert)
1031
860
  {
1032
 
    uint i;
 
861
    uint32_t i;
1033
862
    for (i=0 ; i < info->s->base.keys ; i++)
1034
863
    {
1035
864
      if (is_tree_inited(& info->bulk_insert[i]))
1037
866
        delete_tree(& info->bulk_insert[i]);
1038
867
      }
1039
868
    }
1040
 
    my_free((void *)info->bulk_insert, MYF(0));
 
869
    free((void *)info->bulk_insert);
1041
870
    info->bulk_insert=0;
1042
871
  }
1043
872
}