~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/uniques.cc

  • Committer: Monty Taylor
  • Date: 2009-09-23 17:40:57 UTC
  • mfrom: (1099.4.60)
  • mto: This revision was merged to the branch mainline in revision 1141.
  • Revision ID: mordred@inaugust.com-20090923174057-wuvqhk19h43no9wz
Merged translations from launchpad.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
*/
32
32
 
33
33
#include <drizzled/server_includes.h>
34
 
#include "sql_sort.h"
35
 
 
36
 
 
37
 
int unique_write_to_file(uchar* key,
38
 
                         element_count count __attribute__((unused)),
 
34
#include <drizzled/sql_sort.h>
 
35
#include <drizzled/session.h>
 
36
#include <queue>
 
37
 
 
38
#if defined(CMATH_NAMESPACE)
 
39
using namespace CMATH_NAMESPACE;
 
40
#endif
 
41
 
 
42
using namespace std;
 
43
 
 
44
 
 
45
int unique_write_to_file(unsigned char* key, element_count,
39
46
                         Unique *unique)
40
47
{
41
48
  /*
47
54
  return my_b_write(&unique->file, key, unique->size) ? 1 : 0;
48
55
}
49
56
 
50
 
int unique_write_to_ptrs(uchar* key,
51
 
                         element_count count __attribute__((unused)),
52
 
                         Unique *unique)
 
57
int unique_write_to_ptrs(unsigned char* key,
 
58
                         element_count, Unique *unique)
53
59
{
54
60
  memcpy(unique->record_pointers, key, unique->size);
55
61
  unique->record_pointers+=unique->size;
57
63
}
58
64
 
59
65
Unique::Unique(qsort_cmp2 comp_func, void * comp_func_fixed_arg,
60
 
               uint size_arg, uint64_t max_in_memory_size_arg)
 
66
               uint32_t size_arg, size_t max_in_memory_size_arg)
61
67
  :max_in_memory_size(max_in_memory_size_arg), size(size_arg), elements(0)
62
68
{
63
69
  my_b_clear(&file);
70
76
  */
71
77
  max_elements= (ulong) (max_in_memory_size /
72
78
                         ALIGN_SIZE(sizeof(TREE_ELEMENT)+size));
73
 
  VOID(open_cached_file(&file, mysql_tmpdir,TEMP_PREFIX, DISK_BUFFER_SIZE,
74
 
                   MYF(MY_WME)));
 
79
  open_cached_file(&file, drizzle_tmpdir,TEMP_PREFIX, DISK_BUFFER_SIZE,
 
80
                   MYF(MY_WME));
75
81
}
76
82
 
77
83
 
126
132
      total_buf_elems* log2(n_buffers) / TIME_FOR_COMPARE_ROWID;
127
133
*/
128
134
 
129
 
static double get_merge_buffers_cost(uint *buff_elems __attribute__((unused)),
130
 
                                     uint elem_size,
131
 
                                     uint *first, uint *last)
 
135
static double get_merge_buffers_cost(uint32_t *, uint32_t elem_size,
 
136
                                     uint32_t *first, uint32_t *last)
132
137
{
133
 
  uint total_buf_elems= 0;
134
 
  for (uint *pbuf= first; pbuf <= last; pbuf++)
 
138
  uint32_t total_buf_elems= 0;
 
139
  for (uint32_t *pbuf= first; pbuf <= last; pbuf++)
135
140
    total_buf_elems+= *pbuf;
136
141
  *last= total_buf_elems;
137
142
 
170
175
    Cost of merge in disk seeks.
171
176
*/
172
177
 
173
 
static double get_merge_many_buffs_cost(uint *buffer,
174
 
                                        uint maxbuffer, uint max_n_elems,
175
 
                                        uint last_n_elems, int elem_size)
 
178
static double get_merge_many_buffs_cost(uint32_t *buffer,
 
179
                                        uint32_t maxbuffer, uint32_t max_n_elems,
 
180
                                        uint32_t last_n_elems, int elem_size)
176
181
{
177
182
  register int i;
178
183
  double total_cost= 0.0;
179
 
  uint *buff_elems= buffer; /* #s of elements in each of merged sequences */
 
184
  uint32_t *buff_elems= buffer; /* #s of elements in each of merged sequences */
180
185
 
181
186
  /*
182
187
    Set initial state: first maxbuffer sequences contain max_n_elems elements
194
199
  {
195
200
    while (maxbuffer >= MERGEBUFF2)
196
201
    {
197
 
      uint lastbuff= 0;
 
202
      uint32_t lastbuff= 0;
198
203
      for (i = 0; i <= (int) maxbuffer - MERGEBUFF*3/2; i += MERGEBUFF)
199
204
      {
200
205
        total_cost+=get_merge_buffers_cost(buff_elems, elem_size,
263
268
      these will be random seeks.
264
269
*/
265
270
 
266
 
double Unique::get_use_cost(uint *buffer, uint nkeys, uint key_size,
267
 
                            uint64_t max_in_memory_size)
 
271
double Unique::get_use_cost(uint32_t *buffer, uint32_t nkeys, uint32_t key_size,
 
272
                            size_t max_in_memory_size)
268
273
{
269
274
  ulong max_elements_in_tree;
270
275
  ulong last_tree_elems;
331
336
 
332
337
  if (tree_walk(&tree, (tree_walk_action) unique_write_to_file,
333
338
                (void*) this, left_root_right) ||
334
 
      insert_dynamic(&file_ptrs, (uchar*) &file_ptr))
 
339
      insert_dynamic(&file_ptrs, (unsigned char*) &file_ptr))
335
340
    return 1;
336
341
  delete_tree(&tree);
337
342
  return 0;
368
373
  BUFFPEK.
369
374
*/
370
375
 
371
 
C_MODE_START
 
376
#ifdef __cplusplus
 
377
extern "C" {
 
378
#endif
372
379
 
373
 
static int buffpek_compare(void *arg, uchar *key_ptr1, uchar *key_ptr2)
 
380
static int buffpek_compare(void *arg, unsigned char *key_ptr1, unsigned char *key_ptr2)
374
381
{
375
382
  BUFFPEK_COMPARE_CONTEXT *ctx= (BUFFPEK_COMPARE_CONTEXT *) arg;
376
383
  return ctx->key_compare(ctx->key_compare_arg,
377
 
                          *((uchar **) key_ptr1), *((uchar **)key_ptr2));
378
 
}
379
 
 
380
 
C_MODE_END
381
 
 
 
384
                          *((unsigned char **) key_ptr1), *((unsigned char **)key_ptr2));
 
385
}
 
386
 
 
387
#ifdef __cplusplus
 
388
}
 
389
#endif
 
390
 
 
391
/*
 
392
 The comparison function object, passed to a priority_queue in merge_walk()
 
393
 as its sort function parameter.
 
394
*/
 
395
 
 
396
class buffpek_compare_functor
 
397
{
 
398
  qsort_cmp2 key_compare;
 
399
  void *key_compare_arg;
 
400
  public:
 
401
  buffpek_compare_functor(qsort_cmp2 in_key_compare, void *in_compare_arg)
 
402
    : key_compare(in_key_compare), key_compare_arg(in_compare_arg) { }
 
403
  inline bool operator()(const BUFFPEK *i, const BUFFPEK *j)
 
404
  {
 
405
    return key_compare(key_compare_arg,
 
406
                    i->key, j->key);
 
407
  }
 
408
};
382
409
 
383
410
/*
384
411
  DESCRIPTION
414
441
    <> 0  error
415
442
*/
416
443
 
417
 
static bool merge_walk(uchar *merge_buffer, ulong merge_buffer_size,
418
 
                       uint key_length, BUFFPEK *begin, BUFFPEK *end,
 
444
static bool merge_walk(unsigned char *merge_buffer, ulong merge_buffer_size,
 
445
                       uint32_t key_length, BUFFPEK *begin, BUFFPEK *end,
419
446
                       tree_walk_action walk_action, void *walk_action_arg,
420
447
                       qsort_cmp2 compare, void *compare_arg,
421
448
                       IO_CACHE *file)
422
449
{
423
 
  BUFFPEK_COMPARE_CONTEXT compare_context = { compare, compare_arg };
424
 
  QUEUE queue;
425
450
  if (end <= begin ||
426
 
      merge_buffer_size < (ulong) (key_length * (end - begin + 1)) ||
427
 
      init_queue(&queue, (uint) (end - begin), offsetof(BUFFPEK, key), 0,
428
 
                 buffpek_compare, &compare_context))
 
451
      merge_buffer_size < (ulong) (key_length * (end - begin + 1))) 
429
452
    return 1;
 
453
  priority_queue<BUFFPEK *, vector<BUFFPEK *>, buffpek_compare_functor >
 
454
    queue(buffpek_compare_functor(compare, compare_arg));
430
455
  /* we need space for one key when a piece of merge buffer is re-read */
431
456
  merge_buffer_size-= key_length;
432
 
  uchar *save_key_buff= merge_buffer + merge_buffer_size;
433
 
  uint max_key_count_per_piece= (uint) (merge_buffer_size/(end-begin) /
 
457
  unsigned char *save_key_buff= merge_buffer + merge_buffer_size;
 
458
  uint32_t max_key_count_per_piece= (uint32_t) (merge_buffer_size/(end-begin) /
434
459
                                        key_length);
435
460
  /* if piece_size is aligned reuse_freed_buffer will always hit */
436
 
  uint piece_size= max_key_count_per_piece * key_length;
437
 
  uint bytes_read;               /* to hold return value of read_to_buffer */
 
461
  uint32_t piece_size= max_key_count_per_piece * key_length;
 
462
  uint32_t bytes_read;               /* to hold return value of read_to_buffer */
438
463
  BUFFPEK *top;
439
464
  int res= 1;
440
465
  /*
448
473
    top->base= merge_buffer + (top - begin) * piece_size;
449
474
    top->max_keys= max_key_count_per_piece;
450
475
    bytes_read= read_to_buffer(file, top, key_length);
451
 
    if (bytes_read == (uint) (-1))
 
476
    if (bytes_read == (uint32_t) (-1))
452
477
      goto end;
453
478
    assert(bytes_read);
454
 
    queue_insert(&queue, (uchar *) top);
 
479
    queue.push(top);
455
480
  }
456
 
  top= (BUFFPEK *) queue_top(&queue);
457
 
  while (queue.elements > 1)
 
481
  top= queue.top();
 
482
  while (queue.size() > 1)
458
483
  {
459
484
    /*
460
485
      Every iteration one element is removed from the queue, and one is
470
495
    */
471
496
    top->key+= key_length;
472
497
    if (--top->mem_count)
473
 
      queue_replaced(&queue);
 
498
    {
 
499
      queue.pop();
 
500
      queue.push(top);
 
501
    }
474
502
    else /* next piece should be read */
475
503
    {
476
504
      /* save old_key not to overwrite it in read_to_buffer */
477
505
      memcpy(save_key_buff, old_key, key_length);
478
506
      old_key= save_key_buff;
479
507
      bytes_read= read_to_buffer(file, top, key_length);
480
 
      if (bytes_read == (uint) (-1))
 
508
      if (bytes_read == (uint32_t) (-1))
481
509
        goto end;
482
 
      else if (bytes_read > 0)      /* top->key, top->mem_count are reset */
483
 
        queue_replaced(&queue);     /* in read_to_buffer */
 
510
      else if (bytes_read > 0) /* top->key, top->mem_count are reset */
 
511
      {                        /* in read_to_buffer */
 
512
        queue.pop();
 
513
        queue.push(top);
 
514
      }
484
515
      else
485
516
      {
486
517
        /*
487
 
          Tree for old 'top' element is empty: remove it from the queue and
488
 
          give all its memory to the nearest tree.
 
518
          Tree for old 'top' element is empty: remove it from the queue. 
489
519
        */
490
 
        queue_remove(&queue, 0);
491
 
        reuse_freed_buff(&queue, top, key_length);
 
520
        queue.pop();
492
521
      }
493
522
    }
494
 
    top= (BUFFPEK *) queue_top(&queue);
 
523
    top= queue.top();
495
524
    /* new top has been obtained; if old top is unique, apply the action */
496
525
    if (compare(compare_arg, old_key, top->key))
497
526
    {
514
543
    }
515
544
    while (--top->mem_count);
516
545
    bytes_read= read_to_buffer(file, top, key_length);
517
 
    if (bytes_read == (uint) (-1))
 
546
    if (bytes_read == (uint32_t) (-1))
518
547
      goto end;
519
548
  }
520
549
  while (bytes_read);
521
550
  res= 0;
522
551
end:
523
 
  delete_queue(&queue);
524
552
  return res;
525
553
}
526
554
 
548
576
bool Unique::walk(tree_walk_action action, void *walk_action_arg)
549
577
{
550
578
  int res;
551
 
  uchar *merge_buffer;
 
579
  unsigned char *merge_buffer;
552
580
 
553
581
  if (elements == 0)                       /* the whole tree is in memory */
554
582
    return tree_walk(&tree, action, walk_action_arg, left_root_right);
558
586
    return 1;
559
587
  if (flush_io_cache(&file) || reinit_io_cache(&file, READ_CACHE, 0L, 0, 0))
560
588
    return 1;
561
 
  if (!(merge_buffer= (uchar *) my_malloc((ulong) max_in_memory_size, MYF(0))))
 
589
  if (!(merge_buffer= (unsigned char *) malloc(max_in_memory_size)))
562
590
    return 1;
563
591
  res= merge_walk(merge_buffer, (ulong) max_in_memory_size, size,
564
592
                  (BUFFPEK *) file_ptrs.buffer,
565
593
                  (BUFFPEK *) file_ptrs.buffer + file_ptrs.elements,
566
594
                  action, walk_action_arg,
567
595
                  tree.compare, tree.custom_arg, &file);
568
 
  my_free((char*) merge_buffer, MYF(0));
 
596
  free((char*) merge_buffer);
569
597
  return res;
570
598
}
571
599
 
582
610
  if (my_b_tell(&file) == 0)
583
611
  {
584
612
    /* Whole tree is in memory;  Don't use disk if you don't need to */
585
 
    if ((record_pointers=table->sort.record_pointers= (uchar*)
586
 
         my_malloc(size * tree.elements_in_tree, MYF(0))))
 
613
    if ((record_pointers=table->sort.record_pointers= (unsigned char*)
 
614
         malloc(size * tree.elements_in_tree)))
587
615
    {
588
616
      (void) tree_walk(&tree, (tree_walk_action) unique_write_to_ptrs,
589
617
                       this, left_root_right);
596
624
 
597
625
  IO_CACHE *outfile=table->sort.io_cache;
598
626
  BUFFPEK *file_ptr= (BUFFPEK*) file_ptrs.buffer;
599
 
  uint maxbuffer= file_ptrs.elements - 1;
600
 
  uchar *sort_buffer;
 
627
  uint32_t maxbuffer= file_ptrs.elements - 1;
 
628
  unsigned char *sort_buffer;
601
629
  my_off_t save_pos;
602
630
  bool error=1;
603
631
 
604
632
      /* Open cached file if it isn't open */
605
 
  outfile=table->sort.io_cache=(IO_CACHE*) my_malloc(sizeof(IO_CACHE),
606
 
                                MYF(MY_ZEROFILL));
 
633
  outfile=table->sort.io_cache= new IO_CACHE;
 
634
  memset(outfile, 0, sizeof(IO_CACHE));
607
635
 
608
 
  if (!outfile || (! my_b_inited(outfile) && open_cached_file(outfile,mysql_tmpdir,TEMP_PREFIX,READ_RECORD_BUFFER, MYF(MY_WME))))
 
636
  if (!outfile || (! my_b_inited(outfile) && open_cached_file(outfile,drizzle_tmpdir,TEMP_PREFIX,READ_RECORD_BUFFER, MYF(MY_WME))))
609
637
    return 1;
610
 
  reinit_io_cache(outfile,WRITE_CACHE,0L,0,0);
 
638
  reinit_io_cache(outfile, WRITE_CACHE, 0L, 0, 0);
611
639
 
612
640
  memset(&sort_param, 0, sizeof(sort_param));
613
641
  sort_param.max_rows= elements;
614
642
  sort_param.sort_form=table;
615
643
  sort_param.rec_length= sort_param.sort_length= sort_param.ref_length=
616
644
    size;
617
 
  sort_param.keys= (uint) (max_in_memory_size / sort_param.sort_length);
 
645
  sort_param.keys= (uint32_t) (max_in_memory_size / sort_param.sort_length);
618
646
  sort_param.not_killable=1;
619
647
 
620
 
  if (!(sort_buffer=(uchar*) my_malloc((sort_param.keys+1) *
621
 
                                       sort_param.sort_length,
622
 
                                       MYF(0))))
 
648
  if (!(sort_buffer=(unsigned char*) malloc((sort_param.keys+1) *
 
649
                                            sort_param.sort_length)))
623
650
    return 1;
624
651
  sort_param.unique_buff= sort_buffer+(sort_param.keys*
625
652
                                       sort_param.sort_length);
639
666
    goto err;
640
667
  error=0;
641
668
err:
642
 
  x_free(sort_buffer);
 
669
  if (sort_buffer)
 
670
    free(sort_buffer);
643
671
  if (flush_io_cache(outfile))
644
672
    error=1;
645
673