18
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
#include "drizzled/plugin/query_cache.h"
23
#include "drizzled/errmsg_print.h"
25
#include "drizzled/gettext.h"
34
typedef std::vector<plugin::QueryCache *> QueryCaches;
35
QueryCaches all_query_cache;
37
/* Namespaces are here to prevent global symbol clashes with these classes */
40
: public std::unary_function<plugin::QueryCache *, bool>
44
IsCachedIterate(Session* session_arg) :
45
std::unary_function<plugin::QueryCache *, bool>(),
46
session(session_arg) { }
48
inline result_type operator()(argument_type handler)
50
return handler->doIsCached(session);
54
bool plugin::QueryCache::isCached(Session *session)
56
/* Use find_if instead of foreach so that we can collect return codes */
57
QueryCaches::iterator iter=
58
std::find_if(all_query_cache.begin(), all_query_cache.end(),
59
IsCachedIterate(session));
60
/* If iter is == end() here, that means that all of the plugins returned
61
* false, which in this case means they all succeeded. Since we want to
62
* return false on success, we return the value of the two being !=
64
return iter != all_query_cache.end();
68
class SendCachedResultsetIterate
69
: public std::unary_function<plugin::QueryCache *, bool>
73
SendCachedResultsetIterate(Session *session_arg) :
74
std::unary_function<plugin::QueryCache *, bool>(),
75
session(session_arg) { }
77
inline result_type operator()(argument_type handler)
79
return handler->doSendCachedResultset(session);
82
bool plugin::QueryCache::sendCachedResultset(Session *session)
84
/* Use find_if instead of foreach so that we can collect return codes */
85
QueryCaches::iterator iter=
86
std::find_if(all_query_cache.begin(), all_query_cache.end(),
87
SendCachedResultsetIterate(session));
88
/* If iter is == end() here, that means that all of the plugins returned
89
* false, which in this case means they all succeeded. Since we want to
90
* return false on success, we return the value of the two being !=
92
return iter != all_query_cache.end();
95
class PrepareResultsetIterate : public std::unary_function<plugin::QueryCache *, bool>
99
PrepareResultsetIterate(Session *session_arg) :
100
std::unary_function<plugin::QueryCache *, bool>(),
101
session(session_arg) { }
103
inline result_type operator()(argument_type handler)
105
return handler->doPrepareResultset(session);
108
bool plugin::QueryCache::prepareResultset(Session *session)
110
/* Use find_if instead of foreach so that we can collect return codes */
111
QueryCaches::iterator iter=
112
std::find_if(all_query_cache.begin(), all_query_cache.end(),
113
PrepareResultsetIterate(session));
114
/* If iter is == end() here, that means that all of the plugins returned
115
* false, which in this case means they all succeeded. Since we want to
116
* return false on success, we return the value of the two being !=
118
return iter != all_query_cache.end();
121
class SetResultsetIterate : public std::unary_function<plugin::QueryCache *, bool>
125
SetResultsetIterate(Session *session_arg) :
126
std::unary_function<plugin::QueryCache *, bool>(),
127
session(session_arg) { }
129
inline result_type operator()(argument_type handler)
131
return handler->doSetResultset(session);
135
bool plugin::QueryCache::setResultset(Session *session)
137
/* Use find_if instead of foreach so that we can collect return codes */
138
QueryCaches::iterator iter=
139
std::find_if(all_query_cache.begin(), all_query_cache.end(),
140
SetResultsetIterate(session));
141
/* If iter is == end() here, that means that all of the plugins returned
142
* false, which in this case means they all succeeded. Since we want to
143
* return false on success, we return the value of the two being !=
145
return iter != all_query_cache.end();
148
class InsertRecordIterate
149
: public std::unary_function<plugin::QueryCache *, bool>
154
InsertRecordIterate(Session *session_arg, List<Item> &item_arg) :
155
std::unary_function<plugin::QueryCache *, bool>(),
156
session(session_arg), item(item_arg) { }
158
inline result_type operator()(argument_type handler)
160
return handler->doInsertRecord(session, item);
163
bool plugin::QueryCache::insertRecord(Session *session, List<Item> &items)
165
/* Use find_if instead of foreach so that we can collect return codes */
166
QueryCaches::iterator iter=
167
std::find_if(all_query_cache.begin(), all_query_cache.end(),
168
InsertRecordIterate(session, items));
169
/* If iter is == end() here, that means that all of the plugins returned
170
* false, which in this case means they all succeeded. Since we want to
171
* return false on success, we return the value of the two being !=
173
return iter != all_query_cache.end();
178
bool plugin::QueryCache::addPlugin(plugin::QueryCache *handler)
180
all_query_cache.push_back(handler);
184
void plugin::QueryCache::removePlugin(plugin::QueryCache *handler)
186
all_query_cache.erase(std::find(all_query_cache.begin(), all_query_cache.end(),
190
} /* namespace drizzled */
20
#include <drizzled/server_includes.h>
21
#include <drizzled/qcache.h>
22
#include <drizzled/gettext.h>
24
int qcache_initializer(st_plugin_int *plugin)
29
if (p == NULL) return 1;
30
memset(p, 0, sizeof(qcache_t));
32
plugin->data= (void *)p;
34
if (plugin->plugin->init)
36
if (plugin->plugin->init((void *)p))
38
errmsg_printf(ERRMSG_LVL_ERROR, _("qcache plugin '%s' init() failed"),
43
plugin->state= PLUGIN_IS_READY;
52
int qcache_finalizer(st_plugin_int *plugin)
54
qcache_t *p= (qcache_t *) plugin->data;
56
if (plugin->plugin->deinit)
58
if (plugin->plugin->deinit((void *)p))
60
errmsg_printf(ERRMSG_LVL_ERROR, _("qcache plugin '%s' deinit() failed"),
70
The plugin_foreach() iterator requires that we convert all the parameters
71
of a plugin API entry point into a single void pointer, plus the session.
72
So we need the following structure for qcache_invalidate_db() which requires
75
typedef struct db_invalidation_parms
79
} db_invalidation_parms_t;
85
qcache_try_fetch_and_send_iterate();
87
qcache_invalidate_table_iterate();
88
qcache_invalidate_db_iterate();
89
qcache_flush_iterate();
91
are called by plugin_foreach() _once_ for each Query Cache plugin.
94
static bool qcache_try_fetch_and_send_iterate(Session *session,
95
plugin_ref plugin, void *p)
97
qcache_t *l= plugin_data(plugin, qcache_t *);
98
bool is_transactional = (bool *)p;
100
if (l && l->qcache_try_fetch_and_send)
102
if (l->qcache_try_fetch_and_send(session, is_transactional))
104
errmsg_printf(ERRMSG_LVL_ERROR,
105
_("qcache plugin '%s' try_fetch_and_send() failed"),
106
(char *)plugin_name(plugin));
113
static bool qcache_set_iterate(Session *session, plugin_ref plugin, void *p)
115
qcache_t *l = plugin_data(plugin, qcache_t *);
116
bool transactional = (bool *)p;
118
if (l && l->qcache_set)
120
if (l->qcache_set(session, transactional))
122
errmsg_printf(ERRMSG_LVL_ERROR, _("qcache plugin '%s' set() failed"),
123
(char *)plugin_name(plugin));
130
static bool qcache_invalidate_table_iterate(Session *session,
131
plugin_ref plugin, void *p)
133
qcache_t *l = plugin_data(plugin, qcache_t *);
134
bool transactional = (bool *)p;
136
if (l && l->qcache_invalidate_table)
138
if (l->qcache_invalidate_table(session, transactional))
140
errmsg_printf(ERRMSG_LVL_ERROR,
141
_("qcache plugin '%s' invalidate_table() failed"),
142
(char *)plugin_name(plugin));
149
static bool qcache_invalidate_db_iterate(Session *session,
150
plugin_ref plugin, void *p)
152
qcache_t *l = plugin_data(plugin, qcache_t *);
153
db_invalidation_parms_t *parms = (db_invalidation_parms_t *)p;
155
if (l && l->qcache_invalidate_db)
157
if (l->qcache_invalidate_db(session, parms->dbname, parms->transactional))
159
errmsg_printf(ERRMSG_LVL_ERROR,
160
_("qcache plugin '%s' invalidate_db() failed"),
161
(char *)plugin_name(plugin));
168
static bool qcache_flush_iterate(Session *session, plugin_ref plugin, void *p)
170
qcache_t *l = plugin_data(plugin, qcache_t *);
172
if (p) return true; /* flush has no parameters, return failure */
174
if (l && l->qcache_flush)
176
if (l->qcache_flush(session))
178
errmsg_printf(ERRMSG_LVL_ERROR, _("qcache plugin '%s' flush() failed"),
179
(char *)plugin_name(plugin));
190
drizzle_qcache_try_fetch_and_send();
191
drizzle_qcache_set();
192
drizzle_qcache_invalidate_table();
193
drizzle_qcache_invalidate_db();
194
drizzle_qcache_flush();
196
are the entry points to the query cache plugin that is called by the
197
rest of the Drizzle server code.
200
bool drizzle_qcache_try_fetch_and_send(Session *session, bool transactional)
203
foreach_rv= plugin_foreach(session,
204
qcache_try_fetch_and_send_iterate,
205
DRIZZLE_QCACHE_PLUGIN,
206
(void *) &transactional);
210
bool drizzle_qcache_set(Session *session, bool transactional)
213
foreach_rv= plugin_foreach(session,
215
DRIZZLE_QCACHE_PLUGIN,
216
(void *) &transactional);
220
bool drizzle_qcache_invalidate_table(Session *session, bool transactional)
223
foreach_rv= plugin_foreach(session,
224
qcache_invalidate_table_iterate,
225
DRIZZLE_QCACHE_PLUGIN,
226
(void *) &transactional);
230
bool drizzle_qcache_invalidate_db(Session *session, const char *dbname,
234
db_invalidation_parms_t parms;
236
/* marshall the parameters */
237
parms.dbname = dbname;
238
parms.transactional = transactional;
240
foreach_rv= plugin_foreach(session,
241
qcache_invalidate_db_iterate,
242
DRIZZLE_QCACHE_PLUGIN,
247
bool drizzle_qcache_flush(Session *session)
250
foreach_rv= plugin_foreach(session,
251
qcache_flush_iterate,
252
DRIZZLE_QCACHE_PLUGIN,