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