Commit 4b594f07 authored by Varun Gupta's avatar Varun Gupta

More Refactoring

parent 6d93260e
......@@ -888,25 +888,6 @@ Variable_size_keys_descriptor::Variable_size_keys_descriptor(uint length)
}
Variable_size_composite_key_desc::Variable_size_composite_key_desc(uint length)
: Variable_size_keys_descriptor(length), Encode_key()
{
}
Variable_size_composite_key_desc_for_gconcat::
Variable_size_composite_key_desc_for_gconcat(uint length)
: Variable_size_keys_descriptor(length), Encode_key_for_group_concat()
{
}
Variable_size_keys_simple::Variable_size_keys_simple(uint length)
: Variable_size_keys_descriptor(length), Encode_key()
{
}
/*
@brief
Setup the structures that are used when Unique stores packed values
......@@ -930,18 +911,11 @@ Variable_size_keys_descriptor::setup_for_item(THD *thd, Item_sum *item,
uint non_const_args,
uint arg_count)
{
SORT_FIELD *sort,*pos;
if (init())
SORT_FIELD *pos;
if (init(thd, non_const_args))
return true;
DBUG_ASSERT(sort_keys == NULL);
sortorder= (SORT_FIELD*) thd->alloc(sizeof(SORT_FIELD) * non_const_args);
pos= sort= sortorder;
if (!pos)
return true;
sort_keys= new Sort_keys(sortorder, non_const_args);
if (!sort_keys)
return true;
sort=pos= sortorder;
pos= sortorder;
for (uint i= 0; i < arg_count; i++)
{
Item *arg= item->get_arg(i);
......@@ -975,20 +949,11 @@ Variable_size_keys_descriptor::setup_for_item(THD *thd, Item_sum *item,
bool Variable_size_keys_descriptor::setup_for_field(THD *thd, Field *field)
{
SORT_FIELD *sort,*pos;
if (init())
return true;
DBUG_ASSERT(sort_keys == NULL);
sortorder= (SORT_FIELD*) thd->alloc(sizeof(SORT_FIELD));
pos= sort= sortorder;
if (!pos)
return true;
sort_keys= new Sort_keys(sortorder, 1);
if (!sort_keys)
SORT_FIELD *pos;
if (init(thd, 1))
return true;
sort=pos= sortorder;
pos->setup_key_part_for_variable_size_key(field); // Nulls are always excluded
pos= sortorder;
pos->setup_key_part_for_variable_size_key(field);
return false;
}
......@@ -1067,7 +1032,8 @@ uchar* Variable_size_composite_key_desc::make_record(bool exclude_nulls)
return make_encoded_record(sort_keys, exclude_nulls);
}
uchar* Variable_size_composite_key_desc_for_gconcat::make_record(bool exclude_nulls)
uchar*
Variable_size_composite_key_desc_for_gconcat::make_record(bool exclude_nulls)
{
return make_encoded_record(sort_keys, exclude_nulls);
}
......@@ -1079,21 +1045,51 @@ uchar* Variable_size_keys_simple::make_record(bool exclude_nulls)
}
bool Variable_size_composite_key_desc::init()
/*
@brief
Create the sortorder and Sort keys structures for a descriptor
@param thd THD structure
@param count Number of key parts to be allocated
@retval
TRUE ERROR
FALSE structures successfully created
*/
bool Descriptor::init(THD *thd, uint count)
{
if (sortorder)
return false;
DBUG_ASSERT(sort_keys == NULL);
sortorder= (SORT_FIELD*) thd->alloc(sizeof(SORT_FIELD) * count);
if (!sortorder)
return true; // OOM
sort_keys= new Sort_keys(sortorder, count);
if (!sort_keys)
return true; // OOM
return false;
}
bool Variable_size_composite_key_desc::init(THD *thd, uint count)
{
return Encode_key::init(max_length);
return Descriptor::init(thd, count) ||
Encode_variable_size_key::init(max_length);
}
bool Variable_size_composite_key_desc_for_gconcat::init()
bool Variable_size_composite_key_desc_for_gconcat::init(THD *thd, uint count)
{
return Encode_key::init(max_length);
return Descriptor::init(thd, count) ||
Encode_key_for_group_concat::init(max_length);
}
bool Variable_size_keys_simple::init()
bool Variable_size_keys_simple::init(THD *thd, uint count)
{
return Encode_key::init(max_length);
return Descriptor::init(thd, count) ||
Encode_variable_size_key::init(max_length);
}
......@@ -1103,18 +1099,11 @@ Variable_size_composite_key_desc_for_gconcat::setup_for_item(THD *thd,
uint non_const_args,
uint arg_count)
{
SORT_FIELD *sort,*pos;
if (init())
return true;
DBUG_ASSERT(sort_keys == NULL);
sortorder= (SORT_FIELD*) thd->alloc(sizeof(SORT_FIELD) * non_const_args);
pos= sort= sortorder;
if (!pos)
return true;
sort_keys= new Sort_keys(sortorder, non_const_args);
if (!sort_keys)
if (init(thd, non_const_args))
return true;
sort=pos= sortorder;
SORT_FIELD *pos;
pos= sortorder;
for (uint i= 0; i < arg_count; i++)
{
Item *arg= item->get_arg(i);
......@@ -1155,18 +1144,11 @@ Fixed_size_keys_descriptor::setup_for_item(THD *thd, Item_sum *item,
uint non_const_args,
uint arg_count)
{
SORT_FIELD *sort,*pos;
if (sortorder)
return false;
DBUG_ASSERT(sort_keys == NULL);
sortorder= (SORT_FIELD*) thd->alloc(sizeof(SORT_FIELD) * non_const_args);
pos= sort= sortorder;
if (!pos)
return true;
sort_keys= new Sort_keys(sortorder, non_const_args);
if (!sort_keys)
SORT_FIELD *pos;
if (Descriptor::init(thd, non_const_args))
return true;
sort=pos= sortorder;
pos= sortorder;
for (uint i= 0; i < arg_count; i++)
{
Item *arg= item->get_arg(i);
......@@ -1186,19 +1168,10 @@ Fixed_size_keys_descriptor::setup_for_item(THD *thd, Item_sum *item,
bool
Fixed_size_keys_descriptor::setup_for_field(THD *thd, Field *field)
{
SORT_FIELD *sort,*pos;
if (sortorder)
return false;
DBUG_ASSERT(sort_keys == NULL);
sortorder= (SORT_FIELD*) thd->alloc(sizeof(SORT_FIELD));
pos= sort= sortorder;
if (!pos)
return true;
sort_keys= new Sort_keys(sortorder, 1);
if (!sort_keys)
SORT_FIELD *pos;
if (Descriptor::init(thd, 1))
return true;
sort=pos= sortorder;
pos= sortorder;
pos->setup_key_part_for_fixed_size_key(field);
return false;
......@@ -1312,7 +1285,7 @@ Encode_key::~Encode_key()
0 NULL value
>0 length of the packed record
*/
uchar* Encode_key::make_encoded_record(Sort_keys *sort_keys,
uchar* Encode_variable_size_key::make_encoded_record(Sort_keys *sort_keys,
bool exclude_nulls)
{
Field *field;
......
......@@ -18,6 +18,54 @@
#include "filesort.h"
/*
Encode a key into a particular format. The format depends whether
the key is of fixed size or variable size.
@notes
Currently this encoding is only done for variable size keys
*/
class Encode_key
{
protected:
/*
Packed record ptr for a record of the table, the packed value in this
record is added to the unique tree
*/
uchar* rec_ptr;
String tmp_buffer;
public:
virtual ~Encode_key();
virtual uchar* make_encoded_record(Sort_keys *keys, bool exclude_nulls) = 0;
bool init(uint length);
uchar *get_rec_ptr() { return rec_ptr; }
};
class Encode_variable_size_key : public Encode_key
{
public:
Encode_variable_size_key()
{
rec_ptr= NULL;
}
virtual ~Encode_variable_size_key() {}
uchar* make_encoded_record(Sort_keys *keys, bool exclude_nulls) override;
};
class Encode_key_for_group_concat : public Encode_variable_size_key
{
public:
Encode_key_for_group_concat() : Encode_variable_size_key(){}
~Encode_key_for_group_concat() {}
uchar* make_encoded_record(Sort_keys *keys, bool exclude_nulls) override;
};
/*
Descriptor class storing information about the keys that would be
......@@ -25,6 +73,7 @@
extended by other class to support descriptors for keys with fixed and
variable size.
*/
class Descriptor : public Sql_alloc
{
protected:
......@@ -76,41 +125,7 @@ class Descriptor : public Sql_alloc
virtual uchar* make_record(bool exclude_nulls) { return NULL; }
virtual bool is_single_arg() = 0;
};
/*
Class to encode a key into a particular format. The format depends whether
the key is of fixed size or variable size.
@note
Currently this encoding is only done for variable size keys
*/
class Encode_key
{
protected:
/*
Packed record ptr for a record of the table, the packed value in this
record is added to the unique tree
*/
uchar* rec_ptr;
String tmp_buffer;
public:
Encode_key() : rec_ptr(NULL) {}
virtual ~Encode_key();
virtual uchar* make_encoded_record(Sort_keys *keys, bool exclude_nulls);
bool init(uint length);
uchar *get_rec_ptr() { return rec_ptr; }
};
class Encode_key_for_group_concat : public Encode_key
{
public:
Encode_key_for_group_concat() : Encode_key() {}
~Encode_key_for_group_concat() {}
uchar* make_encoded_record(Sort_keys *keys, bool exclude_nulls) override;
virtual bool init(THD *thd, uint count);
};
......@@ -165,7 +180,7 @@ class Fixed_size_keys_for_rowids: public Fixed_size_keys_descriptor
/*
Descriptor for fixed size keys where a keypart can be NULL
Descriptor for fixed size keys where a key part can be NULL
Used currently in JSON_ARRAYAGG
*/
......@@ -221,7 +236,6 @@ class Variable_size_keys_descriptor : public Descriptor
return read_packed_length(ptr);
}
virtual int compare_keys(uchar *a, uchar *b) override { return 0; }
virtual bool init() { return false; }
virtual bool is_single_arg() override { return false; }
virtual bool setup_for_item(THD *thd, Item_sum *item,
......@@ -245,21 +259,23 @@ class Variable_size_keys_descriptor : public Descriptor
/*
Descriptor for variable size keys with only one component
Used by EITS, JSON_ARRAYAGG,
COUNT(DISTINCT col1) AND GROUP_CONCAT(DISTINCT col1) => only one item is allowed
Used by EITS, JSON_ARRAYAGG.
COUNT(DISTINCT col) AND GROUP_CONCAT(DISTINCT col) are also allowed
that the number of arguments with DISTINCT is 1.
*/
class Variable_size_keys_simple : public Variable_size_keys_descriptor,
public Encode_key
public Encode_variable_size_key
{
public:
Variable_size_keys_simple(uint length);
virtual ~Variable_size_keys_simple() {}
Variable_size_keys_simple(uint length)
:Variable_size_keys_descriptor(length), Encode_variable_size_key() {}
~Variable_size_keys_simple() {}
int compare_keys(uchar *a, uchar *b) override;
uchar* make_record(bool exclude_nulls) override;
uchar* get_rec_ptr() { return rec_ptr; }
bool init() override;
bool is_single_arg() override { return true; }
bool init(THD *thd, uint count) override;
};
......@@ -267,14 +283,15 @@ class Variable_size_keys_simple : public Variable_size_keys_descriptor,
Descriptor for variable sized keys with multiple key parts
*/
class Variable_size_composite_key_desc : public Variable_size_keys_descriptor,
public Encode_key
public Encode_variable_size_key
{
public:
Variable_size_composite_key_desc(uint length);
virtual ~Variable_size_composite_key_desc() {}
Variable_size_composite_key_desc(uint length)
: Variable_size_keys_descriptor(length), Encode_variable_size_key() {}
~Variable_size_composite_key_desc() {}
int compare_keys(uchar *a, uchar *b) override;
uchar* make_record(bool exclude_nulls) override;
bool init() override;
bool init(THD *thd, uint count) override;
};
......@@ -284,16 +301,18 @@ class Variable_size_composite_key_desc : public Variable_size_keys_descriptor,
*/
class Variable_size_composite_key_desc_for_gconcat :
public Variable_size_keys_descriptor , public Encode_key_for_group_concat
public Variable_size_keys_descriptor,
public Encode_key_for_group_concat
{
public:
Variable_size_composite_key_desc_for_gconcat(uint length);
virtual ~Variable_size_composite_key_desc_for_gconcat() {}
Variable_size_composite_key_desc_for_gconcat(uint length)
: Variable_size_keys_descriptor(length), Encode_key_for_group_concat() {}
~Variable_size_composite_key_desc_for_gconcat() {}
int compare_keys(uchar *a, uchar *b) override;
uchar* make_record(bool exclude_nulls) override;
bool init() override;
bool setup_for_item(THD *thd, Item_sum *item,
uint non_const_args, uint arg_count) override;
bool init(THD *thd, uint count) override;
};
......@@ -306,7 +325,7 @@ class Unique : public Sql_alloc {
protected:
/*
Storing all relevant information of the expressions whose value are
Storing all meta-data information of the expressions whose value are
being added to the Unique tree
*/
Descriptor *m_descriptor;
......@@ -328,11 +347,11 @@ class Unique : public Sql_alloc {
virtual size_t get_max_in_memory_size() const = 0;
virtual bool is_in_memory() = 0;
// This will be renamed:
virtual ulong elements_in_tree() = 0;
Descriptor *get_descriptor() { return m_descriptor; }
};
/*
Unique_impl -- class for unique (removing of duplicates).
Puts all values to the TREE. If the tree becomes too big,
......@@ -355,13 +374,12 @@ class Unique_impl : public Unique {
uint full_size; /* Size of element + space needed to store the number of
duplicates found for the element. */
uint min_dupl_count; /* Minimum number of occurences of element required for
uint min_dupl_count; /* Minimum number of occurrences of element required for
it to be written to record_pointers.
always 0 for unions, > 0 for intersections */
bool with_counters;
/*
size in bytes used for storing keys in the Unique tree
*/
// size in bytes used for storing keys in the Unique tree
size_t memory_used;
ulong elements;
SORT_INFO sort;
......@@ -384,40 +402,15 @@ class Unique_impl : public Unique {
return record_size > space_left();
}
public:
/*
@brief
Returns the number of elements in the unique instance
@details
If all the elements fit in the memeory, then this returns all the
distinct elements.
*/
ulong get_n_elements() override
{
return is_in_memory() ? elements_in_tree() : elements;
}
SORT_INFO *get_sort() override { return &sort; }
Unique_impl(qsort_cmp2 comp_func, void *comp_func_fixed_arg,
uint size_arg, size_t max_in_memory_size_arg,
uint min_dupl_count_arg, Descriptor *desc);
virtual ~Unique_impl();
ulong elements_in_tree() { return tree.elements_in_tree; }
bool unique_add(void *ptr) override
{
return unique_add(ptr, m_descriptor->get_length_of_key((uchar*)ptr));
}
/*
@brief
Add a record to the Unique tree
@param
ptr key value
size length of the key
@retval
TRUE ERROE
FALSE key successfully inserted in the Unique tree
*/
bool unique_add(void *ptr, uint size_arg)
......@@ -442,6 +435,34 @@ class Unique_impl : public Unique {
DBUG_RETURN(!res);
}
public:
/*
@brief
Returns the number of elements in the unique instance
@details
If all the elements fit in the memory, then this returns all the
distinct elements.
*/
ulong get_n_elements() override
{
return is_in_memory() ? elements_in_tree() : elements;
}
SORT_INFO *get_sort() override { return &sort; }
Unique_impl(qsort_cmp2 comp_func, void *comp_func_fixed_arg,
uint size_arg, size_t max_in_memory_size_arg,
uint min_dupl_count_arg, Descriptor *desc);
~Unique_impl();
ulong elements_in_tree() { return tree.elements_in_tree; }
bool unique_add(void *ptr) override
{
return unique_add(ptr, m_descriptor->get_length_of_key((uchar*)ptr));
}
bool is_in_memory() { return (my_b_tell(&file) == 0); }
void close_for_expansion() { tree.flag= TREE_ONLY_DUPS; }
......@@ -476,10 +497,12 @@ class Unique_impl : public Unique {
size_t get_max_in_memory_size() const { return max_in_memory_size; }
bool is_count_stored() { return with_counters; }
IO_CACHE *get_file () { return &file; }
virtual int write_record_to_file(uchar *key);
int write_record_to_file(uchar *key);
// returns TRUE if the unique tree stores packed values
bool is_variable_sized() { return m_descriptor->is_variable_sized(); }
// returns TRUE if the key to be inserted has only one component
bool is_single_arg() { return m_descriptor->is_single_arg(); }
Descriptor* get_descriptor() { return m_descriptor; }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment