2012-02-17 12:19:38 +01:00
|
|
|
/*
|
|
|
|
Copyright (c) 2010, 2011, Monty Program Ab
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; version 2 of the License.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
Update FSF address
This commit is based on the work of Michal Schorm, rebased on the
earliest MariaDB version.
Th command line used to generate this diff was:
find ./ -type f \
-exec sed -i -e 's/Foundation, Inc., 59 Temple Place, Suite 330, Boston, /Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, /g' {} \; \
-exec sed -i -e 's/Foundation, Inc. 59 Temple Place.* Suite 330, Boston, /Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, /g' {} \; \
-exec sed -i -e 's/MA.*.....-1307.*USA/MA 02110-1335 USA/g' {} \; \
-exec sed -i -e 's/Foundation, Inc., 59 Temple/Foundation, Inc., 51 Franklin/g' {} \; \
-exec sed -i -e 's/Place, Suite 330, Boston, MA.*02111-1307.*USA/Street, Fifth Floor, Boston, MA 02110-1335 USA/g' {} \; \
-exec sed -i -e 's/MA.*.....-1307/MA 02110-1335/g' {} \;
2019-05-10 19:49:46 +02:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
|
2012-02-17 12:19:38 +01:00
|
|
|
|
2010-07-10 12:37:30 +02:00
|
|
|
#ifndef SQL_EXPRESSION_CACHE_INCLUDED
|
|
|
|
#define SQL_EXPRESSION_CACHE_INCLUDED
|
|
|
|
|
|
|
|
#include "sql_select.h"
|
|
|
|
|
2015-03-25 18:27:10 +01:00
|
|
|
|
2010-07-10 12:37:30 +02:00
|
|
|
/**
|
|
|
|
Interface for expression cache
|
|
|
|
|
|
|
|
@note
|
|
|
|
Parameters of an expression cache interface are set on the creation of the
|
|
|
|
cache. They are passed when a cache object of the implementation class is
|
|
|
|
constructed. That's why they are not visible in this interface.
|
|
|
|
*/
|
|
|
|
|
2010-10-27 05:03:59 +02:00
|
|
|
extern ulong subquery_cache_miss, subquery_cache_hit;
|
2010-07-10 12:37:30 +02:00
|
|
|
|
|
|
|
class Expression_cache :public Sql_alloc
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum result {ERROR, HIT, MISS};
|
|
|
|
|
2023-02-07 12:57:20 +01:00
|
|
|
Expression_cache()= default;
|
|
|
|
virtual ~Expression_cache() = default;
|
2010-07-10 12:37:30 +02:00
|
|
|
/**
|
|
|
|
Shall check the presence of expression value in the cache for a given
|
|
|
|
set of values of the expression parameters. Return the result of the
|
|
|
|
expression if it's found in the cache.
|
|
|
|
*/
|
|
|
|
virtual result check_value(Item **value)= 0;
|
|
|
|
/**
|
|
|
|
Shall put the value of an expression for given set of its parameters
|
|
|
|
into the expression cache
|
|
|
|
*/
|
|
|
|
virtual my_bool put_value(Item *value)= 0;
|
2010-09-06 14:34:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
Print cache parameters
|
|
|
|
*/
|
|
|
|
virtual void print(String *str, enum_query_type query_type)= 0;
|
2011-07-19 22:19:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
Is this cache initialized
|
|
|
|
*/
|
|
|
|
virtual bool is_inited()= 0;
|
|
|
|
/**
|
|
|
|
Initialize this cache
|
|
|
|
*/
|
|
|
|
virtual void init()= 0;
|
2015-03-25 18:27:10 +01:00
|
|
|
|
|
|
|
/**
|
2015-07-01 19:03:29 +02:00
|
|
|
Save this object's statistics into Expression_cache_tracker object
|
2015-03-25 18:27:10 +01:00
|
|
|
*/
|
2015-07-01 19:03:29 +02:00
|
|
|
virtual void update_tracker()= 0;
|
2010-07-10 12:37:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct st_table_ref;
|
|
|
|
struct st_join_table;
|
|
|
|
class Item_field;
|
|
|
|
|
|
|
|
|
2015-07-01 19:03:29 +02:00
|
|
|
class Expression_cache_tracker :public Sql_alloc
|
2015-03-25 18:27:10 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum expr_cache_state {UNINITED, STOPPED, OK};
|
2015-07-01 19:03:29 +02:00
|
|
|
Expression_cache_tracker(Expression_cache *c) :
|
2015-03-25 18:27:10 +01:00
|
|
|
cache(c), hit(0), miss(0), state(UNINITED)
|
|
|
|
{}
|
|
|
|
|
2022-04-26 22:03:34 +02:00
|
|
|
private:
|
|
|
|
// This can be NULL if the cache is already deleted
|
2015-03-25 18:27:10 +01:00
|
|
|
Expression_cache *cache;
|
2022-04-26 22:03:34 +02:00
|
|
|
|
|
|
|
public:
|
2015-03-25 18:27:10 +01:00
|
|
|
ulong hit, miss;
|
|
|
|
enum expr_cache_state state;
|
|
|
|
|
|
|
|
static const char* state_str[3];
|
|
|
|
void set(ulong h, ulong m, enum expr_cache_state s)
|
|
|
|
{hit= h; miss= m; state= s;}
|
|
|
|
|
2022-04-26 22:03:34 +02:00
|
|
|
void detach_from_cache() { cache= NULL; }
|
2015-07-01 19:03:29 +02:00
|
|
|
void fetch_current_stats()
|
2015-03-25 18:27:10 +01:00
|
|
|
{
|
|
|
|
if (cache)
|
2015-07-01 19:03:29 +02:00
|
|
|
cache->update_tracker();
|
2015-03-25 18:27:10 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-07-10 12:37:30 +02:00
|
|
|
/**
|
|
|
|
Implementation of expression cache over a temporary table
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Expression_cache_tmptable :public Expression_cache
|
|
|
|
{
|
|
|
|
public:
|
2011-07-19 22:19:10 +02:00
|
|
|
Expression_cache_tmptable(THD *thd, List<Item> &dependants, Item *value);
|
2010-07-10 12:37:30 +02:00
|
|
|
virtual ~Expression_cache_tmptable();
|
|
|
|
virtual result check_value(Item **value);
|
|
|
|
virtual my_bool put_value(Item *value);
|
|
|
|
|
2010-09-06 14:34:24 +02:00
|
|
|
void print(String *str, enum_query_type query_type);
|
2011-07-19 22:19:10 +02:00
|
|
|
bool is_inited() { return inited; };
|
|
|
|
void init();
|
2010-09-06 14:34:24 +02:00
|
|
|
|
2015-07-01 19:03:29 +02:00
|
|
|
void set_tracker(Expression_cache_tracker *st)
|
2015-03-25 18:27:10 +01:00
|
|
|
{
|
2015-07-01 19:03:29 +02:00
|
|
|
tracker= st;
|
|
|
|
update_tracker();
|
2015-03-25 18:27:10 +01:00
|
|
|
}
|
2015-07-01 19:03:29 +02:00
|
|
|
virtual void update_tracker()
|
2015-03-25 18:27:10 +01:00
|
|
|
{
|
2015-07-01 19:03:29 +02:00
|
|
|
if (tracker)
|
|
|
|
{
|
|
|
|
tracker->set(hit, miss, (inited ? (cache_table ?
|
|
|
|
Expression_cache_tracker::OK :
|
|
|
|
Expression_cache_tracker::STOPPED) :
|
|
|
|
Expression_cache_tracker::UNINITED));
|
|
|
|
}
|
2015-03-25 18:27:10 +01:00
|
|
|
}
|
|
|
|
|
2010-07-10 12:37:30 +02:00
|
|
|
private:
|
2011-08-12 12:54:41 +02:00
|
|
|
void disable_cache();
|
2010-07-10 12:37:30 +02:00
|
|
|
|
|
|
|
/* tmp table parameters */
|
|
|
|
TMP_TABLE_PARAM cache_table_param;
|
|
|
|
/* temporary table to store this cache */
|
|
|
|
TABLE *cache_table;
|
|
|
|
/* Thread handle for the temporary table */
|
|
|
|
THD *table_thd;
|
2015-03-25 18:27:10 +01:00
|
|
|
/* EXPALIN/ANALYZE statistics */
|
2015-07-01 19:03:29 +02:00
|
|
|
Expression_cache_tracker *tracker;
|
2010-07-10 12:37:30 +02:00
|
|
|
/* TABLE_REF for index lookup */
|
|
|
|
struct st_table_ref ref;
|
|
|
|
/* Cached result */
|
|
|
|
Item_field *cached_result;
|
2011-07-19 22:19:10 +02:00
|
|
|
/* List of parameter items */
|
|
|
|
List<Item> &items;
|
2010-07-10 12:37:30 +02:00
|
|
|
/* Value Item example */
|
|
|
|
Item *val;
|
2011-07-28 16:10:29 +02:00
|
|
|
/* hit/miss counters */
|
2015-03-25 18:27:10 +01:00
|
|
|
ulong hit, miss;
|
2010-07-10 12:37:30 +02:00
|
|
|
/* Set on if the object has been succesfully initialized with init() */
|
|
|
|
bool inited;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* SQL_EXPRESSION_CACHE_INCLUDED */
|