1 /* Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved. 2 3 This program is free software; you can redistribute it and/or modify 4 it under the terms of the GNU General Public License, version 2.0, 5 as published by the Free Software Foundation. 6 7 This program is also distributed with certain software (including 8 but not limited to OpenSSL) that is licensed under separate terms, 9 as designated in a particular file or component or in included license 10 documentation. The authors of MySQL hereby grant you an additional 11 permission to link the program and your derivative works with the 12 separately licensed software that they have included with MySQL. 13 14 This program is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License, version 2.0, for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with this program; if not, write to the Free Software 21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ 22 23 #ifndef UNIQUES_INCLUDED 24 #define UNIQUES_INCLUDED 25 26 #include "my_tree.h" // TREE 27 #include "prealloced_array.h" // Prealloced_array 28 #include "sql_alloc.h" // Sql_alloc 29 30 class Cost_model_table; 31 32 /* 33 Unique -- class for unique (removing of duplicates). 34 Puts all values to the TREE. If the tree becomes too big, 35 it's dumped to the file. User can request sorted values, or 36 just iterate through them. In the last case tree merging is performed in 37 memory simultaneously with iteration, so it should be ~2-3x faster. 38 */ 39 40 class Unique :public Sql_alloc 41 { 42 Prealloced_array<Merge_chunk, 16, true> file_ptrs; 43 ulong max_elements; 44 ulonglong max_in_memory_size; 45 IO_CACHE file; 46 TREE tree; 47 uchar *record_pointers; 48 bool flush(); 49 uint size; 50 51 public: 52 ulong elements; 53 Unique(qsort_cmp2 comp_func, void *comp_func_fixed_arg, 54 uint size_arg, ulonglong max_in_memory_size_arg); 55 ~Unique(); elements_in_tree()56 ulong elements_in_tree() { return tree.elements_in_tree; } unique_add(void * ptr)57 inline bool unique_add(void *ptr) 58 { 59 DBUG_ENTER("unique_add"); 60 DBUG_PRINT("info", ("tree %u - %lu", tree.elements_in_tree, max_elements)); 61 if (tree.elements_in_tree > max_elements && flush()) 62 DBUG_RETURN(1); 63 DBUG_RETURN(!tree_insert(&tree, ptr, 0, tree.custom_arg)); 64 } 65 66 bool get(TABLE *table); 67 68 typedef Bounds_checked_array<uint> Imerge_cost_buf_type; 69 70 static double get_use_cost(Imerge_cost_buf_type buffer, 71 uint nkeys, uint key_size, 72 ulonglong max_in_memory_size, 73 const Cost_model_table *cost_model); 74 75 // Returns the number of elements needed in Imerge_cost_buf_type. get_cost_calc_buff_size(ulong nkeys,uint key_size,ulonglong max_in_memory_size)76 inline static size_t get_cost_calc_buff_size(ulong nkeys, uint key_size, 77 ulonglong max_in_memory_size) 78 { 79 ulonglong max_elems_in_tree= 80 (max_in_memory_size / ALIGN_SIZE(sizeof(TREE_ELEMENT)+key_size)); 81 return 1 + static_cast<size_t>(nkeys/max_elems_in_tree); 82 } 83 84 void reset(); 85 bool walk(tree_walk_action action, void *walk_action_arg); 86 get_size()87 uint get_size() const { return size; } get_max_in_memory_size()88 ulonglong get_max_in_memory_size() const { return max_in_memory_size; } 89 90 friend int unique_write_to_file(uchar* key, element_count count, Unique *unique); 91 friend int unique_write_to_ptrs(uchar* key, element_count count, Unique *unique); 92 }; 93 94 95 96 97 #endif // UNIQUES_INCLUDED 98