~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/uniques.cc

  • Committer: Eric Day
  • Date: 2010-01-07 20:02:38 UTC
  • mfrom: (1259.4.2 staging)
  • mto: This revision was merged to the branch mainline in revision 1271.
  • Revision ID: eday@oddments.org-20100107200238-uqw8v6kv9pl7nny5
Merged trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
  deletes in disk order.
31
31
*/
32
32
 
33
 
#include <drizzled/server_includes.h>
34
 
#include <drizzled/sql_sort.h>
35
 
#include <drizzled/session.h>
 
33
#include "config.h"
 
34
 
 
35
#include <math.h>
 
36
 
36
37
#include <queue>
37
38
 
 
39
#include "drizzled/sql_sort.h"
 
40
#include "drizzled/session.h"
 
41
#include "drizzled/sql_list.h"
 
42
#include "drizzled/internal/iocache.h"
 
43
 
38
44
#if defined(CMATH_NAMESPACE)
39
45
using namespace CMATH_NAMESPACE;
40
46
#endif
41
47
 
 
48
using namespace drizzled;
42
49
using namespace std;
43
50
 
44
51
 
45
 
int unique_write_to_file(unsigned char* key, element_count,
 
52
int unique_write_to_file(unsigned char* key, uint32_t,
46
53
                         Unique *unique)
47
54
{
48
55
  /*
51
58
    when tree implementation chooses to store pointer to key in TREE_ELEMENT
52
59
    (instead of storing the element itself there)
53
60
  */
54
 
  return my_b_write(&unique->file, key, unique->size) ? 1 : 0;
 
61
  return my_b_write(unique->file, key, unique->size) ? 1 : 0;
55
62
}
56
63
 
57
64
int unique_write_to_ptrs(unsigned char* key,
58
 
                         element_count, Unique *unique)
 
65
                         uint32_t, Unique *unique)
59
66
{
60
67
  memcpy(unique->record_pointers, key, unique->size);
61
68
  unique->record_pointers+=unique->size;
64
71
 
65
72
Unique::Unique(qsort_cmp2 comp_func, void * comp_func_fixed_arg,
66
73
               uint32_t size_arg, size_t max_in_memory_size_arg)
67
 
  :max_in_memory_size(max_in_memory_size_arg), size(size_arg), elements(0)
 
74
  : max_in_memory_size(max_in_memory_size_arg),
 
75
    file(static_cast<IO_CACHE *>(memory::sql_calloc(sizeof(IO_CACHE)))),
 
76
    size(size_arg),
 
77
    elements(0)
68
78
{
69
 
  my_b_clear(&file);
 
79
  my_b_clear(file);
70
80
  init_tree(&tree, (ulong) (max_in_memory_size / 16), 0, size, comp_func, false,
71
81
            NULL, comp_func_fixed_arg);
72
82
  /* If the following fail's the next add will also fail */
76
86
  */
77
87
  max_elements= (ulong) (max_in_memory_size /
78
88
                         ALIGN_SIZE(sizeof(TREE_ELEMENT)+size));
79
 
  open_cached_file(&file, drizzle_tmpdir,TEMP_PREFIX, DISK_BUFFER_SIZE,
 
89
  open_cached_file(file, drizzle_tmpdir,TEMP_PREFIX, DISK_BUFFER_SIZE,
80
90
                   MYF(MY_WME));
81
91
}
82
92
 
320
330
 
321
331
Unique::~Unique()
322
332
{
323
 
  close_cached_file(&file);
 
333
  close_cached_file(file);
324
334
  delete_tree(&tree);
325
335
  delete_dynamic(&file_ptrs);
326
336
}
332
342
  BUFFPEK file_ptr;
333
343
  elements+= tree.elements_in_tree;
334
344
  file_ptr.count=tree.elements_in_tree;
335
 
  file_ptr.file_pos=my_b_tell(&file);
 
345
  file_ptr.file_pos=my_b_tell(file);
336
346
 
337
347
  if (tree_walk(&tree, (tree_walk_action) unique_write_to_file,
338
348
                (void*) this, left_root_right) ||
361
371
  if (elements)
362
372
  {
363
373
    reset_dynamic(&file_ptrs);
364
 
    reinit_io_cache(&file, WRITE_CACHE, 0L, 0, 1);
 
374
    reinit_io_cache(file, WRITE_CACHE, 0L, 0, 1);
365
375
  }
366
376
  elements= 0;
367
377
}
584
594
  /* flush current tree to the file to have some memory for merge buffer */
585
595
  if (flush())
586
596
    return 1;
587
 
  if (flush_io_cache(&file) || reinit_io_cache(&file, READ_CACHE, 0L, 0, 0))
 
597
  if (flush_io_cache(file) || reinit_io_cache(file, READ_CACHE, 0L, 0, 0))
588
598
    return 1;
589
599
  if (!(merge_buffer= (unsigned char *) malloc(max_in_memory_size)))
590
600
    return 1;
592
602
                  (BUFFPEK *) file_ptrs.buffer,
593
603
                  (BUFFPEK *) file_ptrs.buffer + file_ptrs.elements,
594
604
                  action, walk_action_arg,
595
 
                  tree.compare, tree.custom_arg, &file);
 
605
                  tree.compare, tree.custom_arg, file);
596
606
  free((char*) merge_buffer);
597
607
  return res;
598
608
}
607
617
  SORTPARAM sort_param;
608
618
  table->sort.found_records=elements+tree.elements_in_tree;
609
619
 
610
 
  if (my_b_tell(&file) == 0)
 
620
  if (my_b_tell(file) == 0)
611
621
  {
612
622
    /* Whole tree is in memory;  Don't use disk if you don't need to */
613
623
    if ((record_pointers=table->sort.record_pointers= (unsigned char*)
656
666
  sort_param.cmp_context.key_compare_arg= tree.custom_arg;
657
667
 
658
668
  /* Merge the buffers to one file, removing duplicates */
659
 
  if (merge_many_buff(&sort_param,sort_buffer,file_ptr,&maxbuffer,&file))
660
 
    goto err;
661
 
  if (flush_io_cache(&file) ||
662
 
      reinit_io_cache(&file,READ_CACHE,0L,0,0))
663
 
    goto err;
664
 
  if (merge_buffers(&sort_param, &file, outfile, sort_buffer, file_ptr,
 
669
  if (merge_many_buff(&sort_param,sort_buffer,file_ptr,&maxbuffer,file))
 
670
    goto err;
 
671
  if (flush_io_cache(file) ||
 
672
      reinit_io_cache(file,READ_CACHE,0L,0,0))
 
673
    goto err;
 
674
  if (merge_buffers(&sort_param, file, outfile, sort_buffer, file_ptr,
665
675
                    file_ptr, file_ptr+maxbuffer,0))
666
676
    goto err;
667
677
  error=0;