~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/myisam/mi_write.c

  • Committer: Monty Taylor
  • Date: 2008-08-01 22:33:44 UTC
  • mto: (236.1.42 codestyle)
  • mto: This revision was merged to the branch mainline in revision 261.
  • Revision ID: monty@inaugust.com-20080801223344-vzhlflfmtijp1imv
First pass at gettexizing the error messages.

Show diffs side-by-side

added added

removed removed

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