Drizzled Public API Documentation

unique.h
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 
00021 #pragma once
00022 
00023 #include <drizzled/tree.h>
00024 /*
00025    Unique -- class for unique (removing of duplicates).
00026    Puts all values to the TREE. If the tree becomes too big,
00027    it's dumped to the file. User can request sorted values, or
00028    just iterate through them. In the last case tree merging is performed in
00029    memory simultaneously with iteration, so it should be ~2-3x faster.
00030  */
00031 
00032 namespace drizzled
00033 {
00034 
00035 namespace internal
00036 {
00037 typedef struct st_io_cache IO_CACHE;
00038 }
00039 
00040 class Unique : public memory::SqlAlloc
00041 {
00042   size_t max_in_memory_size;
00043   TREE tree;
00044   unsigned char *record_pointers;
00045   uint32_t size;
00046 
00047 public:
00048   ulong elements;
00049   Unique(qsort_cmp2 comp_func, void *comp_func_fixed_arg,
00050    uint32_t size_arg, size_t max_in_memory_size_arg);
00051   ~Unique();
00052   ulong elements_in_tree() { return tree.elements_in_tree; }
00053   inline bool unique_add(void *ptr)
00054   {
00055     return (not tree_insert(&tree, ptr, 0, tree.custom_arg));
00056   }
00057 
00058   bool get(Table *table);
00059   static double get_use_cost(uint32_t *buffer, uint32_t nkeys, uint32_t key_size,
00060                              size_t max_in_memory_size);
00061   inline static int get_cost_calc_buff_size(ulong nkeys, uint32_t key_size,
00062                                             size_t sortbuff_size)
00063   {
00064     register size_t max_elems_in_tree=
00065       (1 + sortbuff_size / ALIGN_SIZE(sizeof(TREE_ELEMENT)+key_size));
00066     return (int) (sizeof(uint32_t)*(1 + nkeys/max_elems_in_tree));
00067   }
00068 
00069   void reset();
00070   bool walk(tree_walk_action action, void *walk_action_arg);
00071 
00072   friend int unique_write_to_file(unsigned char* key, uint32_t count, Unique *unique);
00073   friend int unique_write_to_ptrs(unsigned char* key, uint32_t count, Unique *unique);
00074 };
00075 
00076 } /* namespace drizzled */
00077