/*
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
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
#ifndef SQL_EXPRESSION_CACHE_INCLUDED
#define SQL_EXPRESSION_CACHE_INCLUDED
#include "sql_select.h"
/**
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.
*/
extern ulong subquery_cache_miss, subquery_cache_hit;
class Expression_cache :public Sql_alloc
{
public:
enum result {ERROR, HIT, MISS};
Expression_cache()= default;
virtual ~Expression_cache() = default;
/**
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;
/**
Print cache parameters
*/
virtual void print(String *str, enum_query_type query_type)= 0;
/**
Is this cache initialized
*/
virtual bool is_inited()= 0;
/**
Initialize this cache
*/
virtual void init()= 0;
/**
Save this object's statistics into Expression_cache_tracker object
*/
virtual void update_tracker()= 0;
};
struct st_table_ref;
struct st_join_table;
class Item_field;
class Expression_cache_tracker :public Sql_alloc
{
public:
enum expr_cache_state {UNINITED, STOPPED, OK};
Expression_cache_tracker(Expression_cache *c) :
cache(c), hit(0), miss(0), state(UNINITED)
{}
private:
// This can be NULL if the cache is already deleted
Expression_cache *cache;
public:
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;}
void detach_from_cache() { cache= NULL; }
void fetch_current_stats()
{
if (cache)
cache->update_tracker();
}
};
/**
Implementation of expression cache over a temporary table
*/
class Expression_cache_tmptable :public Expression_cache
{
public:
Expression_cache_tmptable(THD *thd, List