~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/unique.h

pandora-build v0.100 - Fixes several bugs found by cb1kenobi. Add several thoughts from folks at LCA.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Sun Microsystems
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
 
 
21
#ifndef DRIZZLED_UNIQUE_H
 
22
#define DRIZZLED_UNIQUE_H
 
23
 
 
24
#include "drizzled/my_tree.h"
 
25
/*
 
26
   Unique -- class for unique (removing of duplicates).
 
27
   Puts all values to the TREE. If the tree becomes too big,
 
28
   it's dumped to the file. User can request sorted values, or
 
29
   just iterate through them. In the last case tree merging is performed in
 
30
   memory simultaneously with iteration, so it should be ~2-3x faster.
 
31
 */
 
32
 
 
33
typedef struct st_io_cache IO_CACHE;
 
34
 
 
35
class Unique :public drizzled::memory::SqlAlloc
 
36
{
 
37
  DYNAMIC_ARRAY file_ptrs;
 
38
  ulong max_elements;
 
39
  size_t max_in_memory_size;
 
40
  IO_CACHE *file;
 
41
  TREE tree;
 
42
  unsigned char *record_pointers;
 
43
  bool flush();
 
44
  uint32_t size;
 
45
 
 
46
public:
 
47
  ulong elements;
 
48
  Unique(qsort_cmp2 comp_func, void *comp_func_fixed_arg,
 
49
         uint32_t size_arg, size_t max_in_memory_size_arg);
 
50
  ~Unique();
 
51
  ulong elements_in_tree() { return tree.elements_in_tree; }
 
52
  inline bool unique_add(void *ptr)
 
53
  {
 
54
    if (tree.elements_in_tree > max_elements && flush())
 
55
      return(1);
 
56
    return(!tree_insert(&tree, ptr, 0, tree.custom_arg));
 
57
  }
 
58
 
 
59
  bool get(Table *table);
 
60
  static double get_use_cost(uint32_t *buffer, uint32_t nkeys, uint32_t key_size,
 
61
                             size_t max_in_memory_size);
 
62
  inline static int get_cost_calc_buff_size(ulong nkeys, uint32_t key_size,
 
63
                                            size_t max_in_memory_size)
 
64
  {
 
65
    register size_t max_elems_in_tree=
 
66
      (1 + max_in_memory_size / ALIGN_SIZE(sizeof(TREE_ELEMENT)+key_size));
 
67
    return (int) (sizeof(uint32_t)*(1 + nkeys/max_elems_in_tree));
 
68
  }
 
69
 
 
70
  void reset();
 
71
  bool walk(tree_walk_action action, void *walk_action_arg);
 
72
 
 
73
  friend int unique_write_to_file(unsigned char* key, uint32_t count, Unique *unique);
 
74
  friend int unique_write_to_ptrs(unsigned char* key, uint32_t count, Unique *unique);
 
75
};
 
76
 
 
77
#endif /* DRIZZLED_UNIQUE_H */