cleanup: simplify Queue<>, add const

also add const to methods in List<> and Hash_set<>
while we're at it
This commit is contained in:
Vicențiu Ciorbaru 2024-05-29 21:12:23 +02:00 committed by Sergei Golubchik
parent 553815ea24
commit 26e5654301
7 changed files with 43 additions and 29 deletions

View File

@ -52,7 +52,7 @@ private:
uchar **m_sort_keys; uchar **m_sort_keys;
size_t m_compare_length; size_t m_compare_length;
Sort_param *m_sort_param; Sort_param *m_sort_param;
Queue<uchar*,uchar*,size_t> m_queue; Queue<uchar*, size_t> m_queue;
}; };
@ -68,7 +68,7 @@ int Bounded_queue::init(ha_rows max_elements, size_t cmplen,
if (max_elements >= UINT_MAX - 1) if (max_elements >= UINT_MAX - 1)
return 1; return 1;
// We allocate space for one extra element, for replace when queue is full. // We allocate space for one extra element, for replace when queue is full.
return m_queue.init((uint)max_elements + 1, 0, true, return m_queue.init((uint)max_elements + 1, true,
(decltype(m_queue)::Queue_compare)get_ptr_compare(cmplen), (decltype(m_queue)::Queue_compare)get_ptr_compare(cmplen),
&m_compare_length); &m_compare_length);
} }

View File

@ -6654,7 +6654,7 @@ subselect_rowid_merge_engine::init(MY_BITMAP *non_null_key_parts,
if (merge_keys[i]->sort_keys()) if (merge_keys[i]->sort_keys())
return TRUE; return TRUE;
if (pq.init(merge_keys_count, 0, false, if (pq.init(merge_keys_count, false,
subselect_rowid_merge_engine::cmp_keys_by_cur_rownum)) subselect_rowid_merge_engine::cmp_keys_by_cur_rownum))
return TRUE; return TRUE;
@ -6713,8 +6713,9 @@ subselect_rowid_merge_engine::cmp_keys_by_null_selectivity(Ordered_key **k1,
*/ */
int int
subselect_rowid_merge_engine::cmp_keys_by_cur_rownum(void *, Ordered_key *k1, subselect_rowid_merge_engine::cmp_keys_by_cur_rownum(void *,
Ordered_key *k2) const Ordered_key *k1,
const Ordered_key *k2)
{ {
rownum_t r1= k1->current(); rownum_t r1= k1->current();
rownum_t r2= k2->current(); rownum_t r2= k2->current();

View File

@ -1368,7 +1368,7 @@ public:
return FALSE; return FALSE;
}; };
/* Return the current index element. */ /* Return the current index element. */
rownum_t current() rownum_t current() const
{ {
DBUG_ASSERT(key_buff_elements && cur_key_idx < key_buff_elements); DBUG_ASSERT(key_buff_elements && cur_key_idx < key_buff_elements);
return key_buff[cur_key_idx]; return key_buff[cur_key_idx];
@ -1507,7 +1507,7 @@ protected:
Priority queue of Ordered_key indexes, one per NULLable column. Priority queue of Ordered_key indexes, one per NULLable column.
This queue is used by the partial match algorithm in method exec(). This queue is used by the partial match algorithm in method exec().
*/ */
Queue<Ordered_key, Ordered_key> pq; Queue<Ordered_key> pq;
protected: protected:
/* /*
Comparison function to compare keys in order of decreasing bitmap Comparison function to compare keys in order of decreasing bitmap
@ -1518,7 +1518,9 @@ protected:
Comparison function used by the priority queue pq, the 'smaller' key Comparison function used by the priority queue pq, the 'smaller' key
is the one with the smaller current row number. is the one with the smaller current row number.
*/ */
static int cmp_keys_by_cur_rownum(void *arg, Ordered_key *k1, Ordered_key *k2); static int cmp_keys_by_cur_rownum(void *arg,
const Ordered_key *k1,
const Ordered_key *k2);
bool test_null_row(rownum_t row_num); bool test_null_row(rownum_t row_num);
bool exists_complementing_null_row(MY_BITMAP *keys_to_complement); bool exists_complementing_null_row(MY_BITMAP *keys_to_complement);

View File

@ -62,18 +62,28 @@ public:
@retval FALSE OK. The value either was inserted or existed @retval FALSE OK. The value either was inserted or existed
in the hash. in the hash.
*/ */
bool insert(T *value) bool insert(const T *value)
{ {
return my_hash_insert(&m_hash, reinterpret_cast<const uchar*>(value)); return my_hash_insert(&m_hash, reinterpret_cast<const uchar*>(value));
} }
bool remove(T *value) bool remove(const T *value)
{ {
return my_hash_delete(&m_hash, reinterpret_cast<uchar*>(value)); return my_hash_delete(&m_hash,
reinterpret_cast<uchar*>(const_cast<T*>(value)));
} }
T *find(const void *key, size_t klen) const T *find(const void *key, size_t klen) const
{ {
return (T*)my_hash_search(&m_hash, reinterpret_cast<const uchar *>(key), klen); return (T*)my_hash_search(&m_hash, reinterpret_cast<const uchar *>(key), klen);
} }
T *find(const T *other) const
{
DBUG_ASSERT(m_hash.get_key);
size_t klen;
uchar *key= m_hash.get_key(reinterpret_cast<const uchar *>(other),
&klen, false);
return find(key, klen);
}
/** Is this hash set empty? */ /** Is this hash set empty? */
bool is_empty() const { return m_hash.records == 0; } bool is_empty() const { return m_hash.records == 0; }
/** Returns the number of unique elements. */ /** Returns the number of unique elements. */

View File

@ -496,8 +496,8 @@ public:
inline List() :base_list() {} inline List() :base_list() {}
inline List(const List<T> &tmp, MEM_ROOT *mem_root) : inline List(const List<T> &tmp, MEM_ROOT *mem_root) :
base_list(tmp, mem_root) {} base_list(tmp, mem_root) {}
inline bool push_back(T *a) { return base_list::push_back(a); } inline bool push_back(const T *a) { return base_list::push_back((void *)a); }
inline bool push_back(T *a, MEM_ROOT *mem_root) inline bool push_back(const T *a, MEM_ROOT *mem_root)
{ return base_list::push_back((void*) a, mem_root); } { return base_list::push_back((void*) a, mem_root); }
inline bool push_front(const T *a) { return base_list::push_front(a); } inline bool push_front(const T *a) { return base_list::push_front(a); }
inline bool push_front(const T *a, MEM_ROOT *mem_root) inline bool push_front(const T *a, MEM_ROOT *mem_root)

View File

@ -16,24 +16,25 @@
#ifndef QUEUE_INCLUDED #ifndef QUEUE_INCLUDED
#define QUEUE_INCLUDED #define QUEUE_INCLUDED
#include <my_global.h>
#include "queues.h" #include "queues.h"
/** /**
A typesafe wrapper of QUEUE, a priority heap A typesafe wrapper of QUEUE, a priority heap
*/ */
template<typename Element, typename Key, typename Param=void> template<typename Element, typename Param=void>
class Queue class Queue
{ {
public: public:
typedef int (*Queue_compare)(Param *, Key *, Key *); typedef int (*Queue_compare)(Param *, const Element *, const Element *);
Queue() { m_queue.root= 0; } Queue() { m_queue.root= 0; }
~Queue() { delete_queue(&m_queue); } ~Queue() { delete_queue(&m_queue); }
int init(uint max_elements, uint offset_to_key, bool max_at_top, int init(uint max_elements, bool max_at_top, Queue_compare compare,
Queue_compare compare, Param *param= 0) Param *param= 0)
{ {
return init_queue(&m_queue, max_elements, offset_to_key, max_at_top, return init_queue(&m_queue, max_elements, 0, max_at_top,
(queue_compare)compare, param, 0, 0); (queue_compare)compare, (void *)param, 0, 0);
} }
size_t elements() const { return m_queue.elements; } size_t elements() const { return m_queue.elements; }
@ -42,11 +43,11 @@ public:
bool is_empty() const { return elements() == 0; } bool is_empty() const { return elements() == 0; }
Element *top() const { return (Element*)queue_top(&m_queue); } Element *top() const { return (Element*)queue_top(&m_queue); }
void push(Element *element) { queue_insert(&m_queue, (uchar*)element); } void push(const Element *element) { queue_insert(&m_queue, (uchar*)element); }
Element *pop() { return (Element *)queue_remove_top(&m_queue); } Element *pop() { return (Element *)queue_remove_top(&m_queue); }
void clear() { queue_remove_all(&m_queue); } void clear() { queue_remove_all(&m_queue); }
void propagate_top() { queue_replace_top(&m_queue); } void propagate_top() { queue_replace_top(&m_queue); }
void replace_top(Element *element) void replace_top(const Element *element)
{ {
queue_top(&m_queue)= (uchar*)element; queue_top(&m_queue)= (uchar*)element;
propagate_top(); propagate_top();

View File

@ -78,21 +78,21 @@ int mhnsw_insert(TABLE *table, KEY *keyinfo)
return err == HA_ERR_END_OF_FILE ? 0 : err; return err == HA_ERR_END_OF_FILE ? 0 : err;
} }
static int cmp_float(void *, float *a, float *b)
{
return *a < *b ? -1 : *a == *b ? 0 : 1;
}
struct Node struct Node
{ {
float distance; float distance;
uchar ref[1000]; uchar ref[1000];
}; };
static int cmp_float(void *, const Node *a, const Node *b)
{
return a->distance < b->distance ? -1 : a->distance == b->distance ? 0 : 1;
}
int mhnsw_read_first(TABLE *table, Item *dist, ulonglong limit) int mhnsw_read_first(TABLE *table, Item *dist, ulonglong limit)
{ {
TABLE *graph= table->hlindex; TABLE *graph= table->hlindex;
Queue<Node, float> todo, result; Queue<Node> todo, result;
Node *cur; Node *cur;
String *str, strbuf; String *str, strbuf;
const size_t ref_length= table->file->ref_length; const size_t ref_length= table->file->ref_length;
@ -105,10 +105,10 @@ int mhnsw_read_first(TABLE *table, Item *dist, ulonglong limit)
DBUG_ASSERT(graph); DBUG_ASSERT(graph);
if (todo.init(1000, 0, 0, cmp_float)) // XXX + autoextent if (todo.init(1000, 0, cmp_float)) // XXX + autoextent
return HA_ERR_OUT_OF_MEM; return HA_ERR_OUT_OF_MEM;
if (result.init(limit, 0, 1, cmp_float)) if (result.init(limit, 1, cmp_float))
return HA_ERR_OUT_OF_MEM; return HA_ERR_OUT_OF_MEM;
if ((err= graph->file->ha_index_init(0, 1))) if ((err= graph->file->ha_index_init(0, 1)))