~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/myisam/sort.cc

  • Committer: Mark Atwood
  • Date: 2011-08-17 19:14:47 UTC
  • mfrom: (2385.3.17 rf)
  • Revision ID: me@mark.atwood.name-20110817191447-h86yzddvycd0xmof
mergeĀ lp:~olafvdspek/drizzle/refactor6

Show diffs side-by-side

added added

removed removed

Lines of Context:
121
121
    info->write_key=write_merge_key;
122
122
  }
123
123
 
124
 
  my_b_clear(&tempfile);
125
 
  my_b_clear(&tempfile_for_exceptions);
 
124
  tempfile.clear();
 
125
  tempfile_for_exceptions.clear();
126
126
  memset(&buffpek, 0, sizeof(buffpek));
127
127
  unsigned char** sort_keys= (unsigned char **) NULL; 
128
128
  int error= 1;
154
154
      while ((maxbuffer= (size_t)(records/(keys-1)+1)) != skr);
155
155
 
156
156
    sort_keys=(unsigned char **)malloc(keys*(sort_length+sizeof(char*)));
157
 
    my_init_dynamic_array(&buffpek, sizeof(BUFFPEK), maxbuffer, maxbuffer/2);
 
157
    buffpek.init(sizeof(BUFFPEK), maxbuffer, maxbuffer/2);
158
158
    break;
159
159
  }
160
160
  if (memavl < MIN_SORT_MEMORY)
188
188
      if (merge_many_buff(info,keys,sort_keys, (BUFFPEK*)buffpek.buffer, &maxbuffer, &tempfile))
189
189
        goto err;
190
190
    }
191
 
    if (internal::flush_io_cache(&tempfile) ||
 
191
    if (tempfile.flush() ||
192
192
        tempfile.reinit_io_cache(internal::READ_CACHE,0L,0,0))
193
193
      goto err;
194
194
    if (!no_messages)
200
200
  if (flush_pending_blocks(info))
201
201
    goto err;
202
202
 
203
 
  if (my_b_inited(&tempfile_for_exceptions))
 
203
  if (tempfile_for_exceptions.inited())
204
204
  {
205
205
    MI_INFO *idx=info->sort_info->info;
206
206
    uint32_t     keyno=info->key;
209
209
    if (not no_messages)
210
210
      printf("  - Adding exceptions\n");
211
211
 
212
 
    if (flush_io_cache(&tempfile_for_exceptions) || tempfile_for_exceptions.reinit_io_cache(internal::READ_CACHE,0L,0,0))
 
212
    if (tempfile_for_exceptions.flush() || tempfile_for_exceptions.reinit_io_cache(internal::READ_CACHE,0L,0,0))
213
213
    {
214
214
      goto err;
215
215
    }
216
216
 
217
 
    while (!my_b_read(&tempfile_for_exceptions,(unsigned char*)&key_length,
218
 
                      sizeof(key_length))
219
 
        && !my_b_read(&tempfile_for_exceptions,(unsigned char*)sort_keys,
220
 
                      (uint) key_length))
 
217
    while (not tempfile_for_exceptions.read(&key_length, sizeof(key_length))
 
218
        && not tempfile_for_exceptions.read(sort_keys, key_length))
221
219
    {
222
 
        if (_mi_ck_write(idx,keyno,(unsigned char*) sort_keys,key_length-ref_length))
223
 
          goto err;
 
220
      if (_mi_ck_write(idx,keyno,(unsigned char*) sort_keys,key_length-ref_length))
 
221
        goto err;
224
222
    }
225
223
  }
226
224
 
228
226
 
229
227
err:
230
228
  free(sort_keys);
231
 
  delete_dynamic(&buffpek);
 
229
  buffpek.free();
232
230
  tempfile.close_cached_file();
233
231
  tempfile_for_exceptions.close_cached_file();
234
232
 
235
 
  return(error ? -1 : 0);
 
233
  return error ? -1 : 0;
236
234
} /* _create_index_by_sort */
237
235
 
238
236
 
261
259
 
262
260
    if (++idx == keys)
263
261
    {
264
 
      if (info->write_keys(info,sort_keys,idx-1,(BUFFPEK *)alloc_dynamic(buffpek),
265
 
                     tempfile))
266
 
      return(HA_POS_ERROR);
 
262
      if (info->write_keys(info,sort_keys,idx-1,(BUFFPEK *)buffpek->alloc(), tempfile))
 
263
        return HA_POS_ERROR;
267
264
 
268
265
      sort_keys[0]=(unsigned char*) (sort_keys+keys);
269
266
      memcpy(sort_keys[0],sort_keys[idx-1],(size_t) info->key_length);
275
272
    return(HA_POS_ERROR);
276
273
  if (buffpek->size())
277
274
  {
278
 
    if (info->write_keys(info,sort_keys,idx,(BUFFPEK *)alloc_dynamic(buffpek),
279
 
                   tempfile))
280
 
      return(HA_POS_ERROR);
 
275
    if (info->write_keys(info,sort_keys,idx,(BUFFPEK *)buffpek->alloc(), tempfile))
 
276
      return HA_POS_ERROR;
281
277
    *maxbuffer=buffpek->size() - 1;
282
278
  }
283
279
  else
336
332
    sinfo->sort_keys=0;
337
333
  }
338
334
 
339
 
  for (i= 0, sinfo= sort_param ;
340
 
       i < sort_info->total_keys ;
341
 
       i++,
342
 
         delete_dynamic(&sinfo->buffpek),
 
335
  for (i= 0, sinfo= sort_param; i < sort_info->total_keys; i++,
 
336
         sinfo->buffpek.free(),
343
337
         sinfo->tempfile.close_cached_file(),
344
338
         sinfo->tempfile_for_exceptions.close_cached_file(),
345
339
         sinfo++)
388
382
          continue;
389
383
        }
390
384
      }
391
 
      if (flush_io_cache(&sinfo->tempfile) || sinfo->tempfile.reinit_io_cache(internal::READ_CACHE,0L,0,0))
 
385
      if (sinfo->tempfile.flush() || sinfo->tempfile.reinit_io_cache(internal::READ_CACHE,0L,0,0))
392
386
      {
393
387
        got_error=1;
394
388
        continue;
403
397
        continue;
404
398
      }
405
399
    }
406
 
    if (my_b_inited(&sinfo->tempfile_for_exceptions))
 
400
    if (sinfo->tempfile_for_exceptions.inited())
407
401
    {
408
402
      uint32_t key_length;
409
403
 
410
404
      if (param->testflag & T_VERBOSE)
411
405
        printf("Key %d  - Dumping 'long' keys\n", sinfo->key+1);
412
406
 
413
 
      if (flush_io_cache(&sinfo->tempfile_for_exceptions) || sinfo->tempfile_for_exceptions.reinit_io_cache(internal::READ_CACHE,0L,0,0))
 
407
      if (sinfo->tempfile_for_exceptions.flush() || sinfo->tempfile_for_exceptions.reinit_io_cache(internal::READ_CACHE,0L,0,0))
414
408
      {
415
409
        got_error=1;
416
410
        continue;
417
411
      }
418
412
 
419
 
      while (!got_error &&
420
 
             !my_b_read(&sinfo->tempfile_for_exceptions,(unsigned char*)&key_length,
421
 
                        sizeof(key_length)))
 
413
      while (!got_error && not sinfo->tempfile_for_exceptions.read(&key_length, sizeof(key_length)))
422
414
      {
423
415
        unsigned char ft_buf[10];
424
 
        if (key_length > sizeof(ft_buf) ||
425
 
            my_b_read(&sinfo->tempfile_for_exceptions, (unsigned char*)ft_buf,
426
 
                      (uint)key_length) ||
427
 
            _mi_ck_write(info, sinfo->key, (unsigned char*)ft_buf,
428
 
                         key_length - info->s->rec_reflength))
429
 
          got_error=1;
 
416
        if (key_length > sizeof(ft_buf) || sinfo->tempfile_for_exceptions.read(ft_buf, (uint)key_length) 
 
417
          || _mi_ck_write(info, sinfo->key, (unsigned char*)ft_buf, key_length - info->s->rec_reflength))
 
418
          got_error= 1;
430
419
      }
431
420
    }
432
421
  }
442
431
  unsigned char **end;
443
432
  uint32_t sort_length=info->key_length;
444
433
 
445
 
  internal::my_qsort2((unsigned char*) sort_keys,count,sizeof(unsigned char*),(qsort2_cmp) info->key_cmp,
446
 
            info);
447
 
  if (!my_b_inited(tempfile) && tempfile->open_cached_file(P_tmpdir, "ST", DISK_BUFFER_SIZE, info->sort_info->param->myf_rw))
 
434
  internal::my_qsort2((unsigned char*) sort_keys,count,sizeof(unsigned char*),(qsort2_cmp) info->key_cmp, info);
 
435
  if (not tempfile->inited() && tempfile->open_cached_file(P_tmpdir, "ST", DISK_BUFFER_SIZE, info->sort_info->param->myf_rw))
448
436
    return(1);
449
437
 
450
 
  buffpek->file_pos=my_b_tell(tempfile);
 
438
  buffpek->file_pos= tempfile->tell();
451
439
  buffpek->count=count;
452
440
 
453
441
  for (end=sort_keys+count ; sort_keys != end ; sort_keys++)
454
442
  {
455
 
    if (my_b_write(tempfile,(unsigned char*) *sort_keys,(uint) sort_length))
 
443
    if (tempfile->write(*sort_keys, sort_length))
456
444
      return(1);
457
445
  }
458
446
  return(0);
466
454
  uint16_t len = _mi_keylength(info->keyinfo, (unsigned char*) bufs);
467
455
 
468
456
  /* The following is safe as this is a local file */
469
 
  if ((err= my_b_write(to_file, (unsigned char*)&len, sizeof(len))))
 
457
  if ((err= to_file->write(&len, sizeof(len))))
470
458
    return (err);
471
 
  if ((err= my_b_write(to_file,bufs, (uint) len)))
 
459
  if ((err= to_file->write(bufs, len)))
472
460
    return (err);
473
461
  return (0);
474
462
}
482
470
  unsigned char **end;
483
471
  int err;
484
472
 
485
 
  internal::my_qsort2((unsigned char*) sort_keys,count,sizeof(unsigned char*),(qsort2_cmp) info->key_cmp,
486
 
            info);
487
 
  if (!my_b_inited(tempfile) && tempfile->open_cached_file(P_tmpdir, "ST", DISK_BUFFER_SIZE, info->sort_info->param->myf_rw))
 
473
  internal::my_qsort2((unsigned char*) sort_keys,count,sizeof(unsigned char*),(qsort2_cmp) info->key_cmp, info);
 
474
  if (not tempfile->inited() && tempfile->open_cached_file(P_tmpdir, "ST", DISK_BUFFER_SIZE, info->sort_info->param->myf_rw))
488
475
    return(1);
489
476
 
490
 
  buffpek->file_pos=my_b_tell(tempfile);
 
477
  buffpek->file_pos= tempfile->tell();
491
478
  buffpek->count=count;
492
479
  for (end=sort_keys+count ; sort_keys != end ; sort_keys++)
493
480
  {
503
490
{
504
491
  uint32_t key_length=info->real_key_length;
505
492
 
506
 
  if (!my_b_inited(tempfile) && tempfile->open_cached_file(P_tmpdir, "ST", DISK_BUFFER_SIZE, info->sort_info->param->myf_rw))
 
493
  if (not tempfile->inited() && tempfile->open_cached_file(P_tmpdir, "ST", DISK_BUFFER_SIZE, info->sort_info->param->myf_rw))
507
494
    return(1);
508
495
 
509
 
  if (my_b_write(tempfile,(unsigned char*)&key_length,sizeof(key_length)) ||
510
 
      my_b_write(tempfile,(unsigned char*)key,(uint) key_length))
 
496
  if (tempfile->write(&key_length, sizeof(key_length)) ||
 
497
      tempfile->write(key, key_length))
511
498
    return(1);
512
499
  return(0);
513
500
} /* write_key */
541
528
 
542
529
  if (*maxbuffer < MERGEBUFF2)
543
530
    return(0);
544
 
  if (flush_io_cache(t_file) || t_file2.open_cached_file(P_tmpdir, "ST",
545
 
                       DISK_BUFFER_SIZE, info->sort_info->param->myf_rw))
 
531
  if (t_file->flush() || t_file2.open_cached_file(P_tmpdir, "ST", DISK_BUFFER_SIZE, info->sort_info->param->myf_rw))
546
532
    return(1);
547
533
 
548
534
  from_file= t_file ; to_file= &t_file2;
560
546
    if (merge_buffers(info,keys,from_file,to_file,sort_keys,lastbuff++,
561
547
                      buffpek+i,buffpek+ *maxbuffer))
562
548
      break;
563
 
    if (flush_io_cache(to_file))
 
549
    if (to_file->flush())
564
550
      break;
565
551
    temp=from_file; from_file=to_file; to_file=temp;
566
552
    *maxbuffer= (int) (lastbuff-buffpek)-1;
656
642
}
657
643
 
658
644
 
659
 
static int  write_merge_key(MI_SORT_PARAM *info,
 
645
static int  write_merge_key(MI_SORT_PARAM*,
660
646
                                  internal::io_cache_st *to_file, unsigned char *key,
661
647
                                  uint32_t sort_length, uint32_t count)
662
648
{
663
 
  (void)info;
664
 
  return my_b_write(to_file, key, (size_t) sort_length*count);
 
649
  return to_file->write(key, sort_length*count);
665
650
}
666
651
 
667
652
/*
707
692
  maxcount=keys/((uint) (Tb-Fb) +1);
708
693
  assert(maxcount > 0);
709
694
  if (to_file)
710
 
    to_start_filepos=my_b_tell(to_file);
 
695
    to_start_filepos= to_file->tell();
711
696
  strpos=(unsigned char*) sort_keys;
712
697
  sort_length=info->key_length;
713
698