1 /* Copyright (c) 2010, 2019, 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 BOUNDED_QUEUE_INCLUDED 24 #define BOUNDED_QUEUE_INCLUDED 25 26 #include "my_base.h" 27 #include "my_sys.h" 28 #include "mysys_err.h" 29 #include "priority_queue.h" 30 #include "sql/malloc_allocator.h" 31 32 /** 33 A priority queue with a fixed, limited size. 34 35 This is a wrapper on top of Priority_queue. 36 It keeps the top-N elements which are inserted. 37 38 Elements of type Element_type are pushed into the queue. 39 For each element, we call a user-supplied Key_generator::make_sortkey(), 40 to generate a key of type Key_type for the element. 41 Instances of Key_type are compared with the user-supplied Key_compare. 42 43 Pointers to the top-N elements are stored in the sort_keys array given 44 to the init() function below. To access elements in sorted order, 45 sort the array and access it sequentially. 46 */ 47 template <typename Element_type, typename Key_type, typename Key_generator, 48 typename Key_compare = std::less<Key_type>> 49 class Bounded_queue { 50 public: 51 typedef Priority_queue< 52 Key_type, std::vector<Key_type, Malloc_allocator<Key_type>>, Key_compare> 53 Queue_type; 54 55 typedef typename Queue_type::allocator_type allocator_type; 56 57 Bounded_queue( 58 size_t element_size = sizeof(Element_type), 59 const allocator_type &alloc = allocator_type(PSI_NOT_INSTRUMENTED)) m_queue(Key_compare (),alloc)60 : m_queue(Key_compare(), alloc), 61 m_sort_keys(nullptr), 62 m_sort_param(nullptr), 63 m_element_size(element_size) {} 64 65 /** 66 Initialize the queue. 67 68 @param max_elements The size of the queue. 69 @param sort_param Sort parameters. We call sort_param->make_sortkey() 70 to generate keys for elements. 71 @param[in,out] sort_keys Array of keys to sort. 72 Must be initialized by caller. 73 Will be filled with pointers to the top-N elements. 74 75 @retval false OK, true Could not allocate memory. 76 77 We do *not* take ownership of any of the input pointer arguments. 78 */ init(ha_rows max_elements,Key_generator * sort_param,Key_type * sort_keys)79 bool init(ha_rows max_elements, Key_generator *sort_param, 80 Key_type *sort_keys) { 81 m_sort_keys = sort_keys; 82 m_sort_param = sort_param; 83 DBUG_EXECUTE_IF("bounded_queue_init_fail", 84 my_error(EE_OUTOFMEMORY, MYF(ME_FATALERROR), 42); 85 return true;); 86 87 // We allocate space for one extra element, for replace when queue is full. 88 if (m_queue.reserve(max_elements + 1)) return true; 89 // We cannot have packed keys in the queue. 90 m_queue.m_compare_length = sort_param->max_compare_length(); 91 // We can have variable length keys though. 92 if (sort_param->using_varlen_keys()) m_queue.m_param = sort_param; 93 return false; 94 } 95 96 /** 97 Pushes an element on the queue. 98 If the queue is already full, we discard one element. 99 Calls m_sort_param::make_sortkey() to generate a key for the element. 100 101 @param element The element to be pushed. 102 */ push(Element_type element)103 void push(Element_type element) { 104 /* 105 Add one extra byte to each key, so that sort-key generating functions 106 won't be returning out-of-space. Since we know there's always room 107 given a "m_element_size"-sized buffer even in the worst case (by 108 definition), we could in principle make a special mode flag in 109 Sort_param::make_sortkey() instead for the case of fixed-length records, 110 but this is much simpler. 111 */ 112 DBUG_ASSERT(m_element_size < 0xFFFFFFFF); 113 const uint element_size = m_element_size + 1; 114 115 if (m_queue.size() == m_queue.capacity()) { 116 const Key_type &pq_top = m_queue.top(); 117 const uint MY_ATTRIBUTE((unused)) rec_sz = 118 m_sort_param->make_sortkey(pq_top, element_size, element); 119 DBUG_ASSERT(rec_sz <= m_element_size); 120 m_queue.update_top(); 121 } else { 122 const uint MY_ATTRIBUTE((unused)) rec_sz = m_sort_param->make_sortkey( 123 m_sort_keys[m_queue.size()], element_size, element); 124 DBUG_ASSERT(rec_sz <= m_element_size); 125 m_queue.push(m_sort_keys[m_queue.size()]); 126 } 127 } 128 129 /** 130 The number of elements in the queue. 131 */ num_elements()132 size_t num_elements() const { return m_queue.size(); } 133 134 private: 135 Queue_type m_queue; 136 Key_type *m_sort_keys; 137 Key_generator *m_sort_param; 138 size_t m_element_size; 139 }; 140 141 #endif // BOUNDED_QUEUE_INCLUDED 142