~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/qcache.cc

Merged plugin-registration.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
#include <drizzled/server_includes.h>
21
21
#include <drizzled/qcache.h>
22
22
#include <drizzled/gettext.h>
 
23
#include <vector>
 
24
 
 
25
using namespace std;
 
26
 
 
27
static vector<QueryCache *> all_query_cache;
 
28
 
 
29
static void add_query_cache(QueryCache *handler)
 
30
{
 
31
  all_query_cache.push_back(handler);
 
32
}
 
33
 
 
34
static void remove_query_cache(QueryCache *handler)
 
35
{
 
36
  all_query_cache.erase(find(all_query_cache.begin(), all_query_cache.end(),
 
37
                        handler));
 
38
}
23
39
 
24
40
int qcache_initializer(st_plugin_int *plugin)
25
41
{
26
 
  qcache_t *p;
27
 
 
28
 
  p= new qcache_t;
29
 
  if (p == NULL) return 1;
30
 
  memset(p, 0, sizeof(qcache_t));
31
 
 
32
 
  plugin->data= (void *)p;
 
42
  QueryCache *p;
 
43
 
33
44
 
34
45
  if (plugin->plugin->init)
35
46
  {
36
 
    if (plugin->plugin->init((void *)p))
 
47
    if (plugin->plugin->init(&p))
37
48
    {
38
49
      errmsg_printf(ERRMSG_LVL_ERROR, _("qcache plugin '%s' init() failed"),
39
50
                    plugin->name.str);
40
 
      goto err;
 
51
      return 1;
41
52
    }
42
53
  }
43
54
 
 
55
  add_query_cache(p);
 
56
  plugin->data= p;
 
57
 
44
58
  return 0;
45
59
 
46
 
err:
47
 
  delete p;
48
 
  return 1;
49
60
}
50
61
 
51
62
int qcache_finalizer(st_plugin_int *plugin)
52
63
{
53
 
  qcache_t *p= (qcache_t *) plugin->data;
54
 
 
55
64
  if (plugin->plugin->deinit)
56
65
  {
57
 
    if (plugin->plugin->deinit((void *)p))
 
66
    if (plugin->plugin->deinit(plugin->data))
58
67
    {
59
68
      errmsg_printf(ERRMSG_LVL_ERROR, _("qcache plugin '%s' deinit() failed"),
60
69
                    plugin->name.str);
61
70
    }
62
71
  }
63
 
 
64
 
  if (p) delete p;
65
72
  return 0;
66
73
}
67
74
 
68
 
/*
69
 
  The plugin_foreach() iterator requires that we convert all the parameters
70
 
  of a plugin API entry point into a single void pointer, plus the session.
71
 
  So we need the following structure for qcache_invalidate_db() which requires
72
 
  multiple arguments.
73
 
*/
74
 
typedef struct db_invalidation_parms
75
 
{
76
 
  const char *dbname;
77
 
  bool transactional;
78
 
} db_invalidation_parms_t;
79
 
 
80
 
 
81
 
/*
82
 
  Following functions:
83
 
 
84
 
    qcache_try_fetch_and_send_iterate();
85
 
    qcache_set_iterate();
86
 
    qcache_invalidate_table_iterate();
87
 
    qcache_invalidate_db_iterate();
88
 
    qcache_flush_iterate();
89
 
 
90
 
  are called by plugin_foreach() _once_ for each Query Cache plugin.
91
 
*/
92
 
 
93
 
static bool qcache_try_fetch_and_send_iterate(Session *session, 
94
 
                                              plugin_ref plugin, void *p)
95
 
{
96
 
  qcache_t *l= plugin_data(plugin, qcache_t *);
97
 
  bool is_transactional = (bool *)p;
98
 
 
99
 
  if (l && l->qcache_try_fetch_and_send)
 
75
 
 
76
/* Namespaces are here to prevent global symbol clashes with these classes */
 
77
 
 
78
namespace drizzled {
 
79
namespace query_cache {
 
80
 
 
81
class TryFetchAndSendIterate
 
82
 : public unary_function<QueryCache *, bool>
 
83
{
 
84
  Session *session;
 
85
  bool is_transactional;
 
86
public:
 
87
  TryFetchAndSendIterate(Session *session_arg, bool is_transactional_arg) :
 
88
    unary_function<QueryCache *, bool>(),
 
89
    session(session_arg), is_transactional(is_transactional_arg) { }
 
90
 
 
91
  inline result_type operator()(argument_type handler)
100
92
  {
101
 
    if (l->qcache_try_fetch_and_send(session, is_transactional))
 
93
    if (handler->try_fetch_and_send(session, is_transactional))
102
94
    {
103
95
      errmsg_printf(ERRMSG_LVL_ERROR,
104
96
                    _("qcache plugin '%s' try_fetch_and_send() failed"),
105
 
                    (char *)plugin_name(plugin));
 
97
                    handler->getName().c_str());
106
98
      return true;
107
99
    }
 
100
    return false;
108
101
  }
109
 
  return false;
110
 
}
 
102
};
111
103
 
112
 
static bool qcache_set_iterate(Session *session, plugin_ref plugin, void *p)
 
104
class SetIterate
 
105
 : public unary_function<QueryCache *, bool>
113
106
{
114
 
  qcache_t *l = plugin_data(plugin, qcache_t *);
115
 
  bool transactional = (bool *)p;
 
107
  Session *session;
 
108
  bool is_transactional;
 
109
public:
 
110
  SetIterate(Session *session_arg, bool is_transactional_arg) :
 
111
    unary_function<QueryCache *, bool>(),
 
112
    session(session_arg), is_transactional(is_transactional_arg) { }
116
113
 
117
 
  if (l && l->qcache_set)
 
114
  inline result_type operator()(argument_type handler)
118
115
  {
119
 
    if (l->qcache_set(session, transactional))
 
116
 
 
117
    if (handler->set(session, is_transactional))
120
118
    {
121
119
      errmsg_printf(ERRMSG_LVL_ERROR, _("qcache plugin '%s' set() failed"),
122
 
                    (char *)plugin_name(plugin));
 
120
                    handler->getName().c_str());
123
121
      return true;
124
122
    }
 
123
    return false;
125
124
  }
126
 
  return false;
127
 
}
 
125
};
128
126
 
129
 
static bool qcache_invalidate_table_iterate(Session *session,
130
 
                                            plugin_ref plugin, void *p)
 
127
class InvalidateTableIterate
 
128
 : public unary_function<QueryCache *, bool>
131
129
{
132
 
  qcache_t *l = plugin_data(plugin, qcache_t *);
133
 
  bool transactional = (bool *)p;
 
130
  Session *session;
 
131
  bool is_transactional;
 
132
public:
 
133
  InvalidateTableIterate(Session *session_arg, bool is_transactional_arg) :
 
134
    unary_function<QueryCache *, bool>(),
 
135
    session(session_arg), is_transactional(is_transactional_arg) { }
134
136
 
135
 
  if (l && l->qcache_invalidate_table)
 
137
  inline result_type operator()(argument_type handler)
136
138
  {
137
 
    if (l->qcache_invalidate_table(session, transactional))
 
139
 
 
140
    if (handler->invalidate_table(session, is_transactional))
138
141
    {
139
142
      errmsg_printf(ERRMSG_LVL_ERROR,
140
143
                    _("qcache plugin '%s' invalidate_table() failed"),
141
 
                    (char *)plugin_name(plugin));
 
144
                    handler->getName().c_str());
142
145
      return true;
143
146
    }
 
147
    return false;
144
148
  }
145
 
  return false;
146
 
}
147
 
 
148
 
static bool qcache_invalidate_db_iterate(Session *session,
149
 
                                         plugin_ref plugin, void *p)
 
149
};
 
150
 
 
151
 
 
152
class InvalidateDbIterate
 
153
 : public unary_function<QueryCache *, bool>
150
154
{
151
 
  qcache_t *l = plugin_data(plugin, qcache_t *);
152
 
  db_invalidation_parms_t *parms = (db_invalidation_parms_t *)p;
 
155
  Session *session;
 
156
  const char *dbname;
 
157
  bool is_transactional;
 
158
public:
 
159
  InvalidateDbIterate(Session *session_arg, const char *dbname_arg,
 
160
                      bool is_transactional_arg) :
 
161
    unary_function<QueryCache *, bool>(),
 
162
    session(session_arg), dbname(dbname_arg),
 
163
    is_transactional(is_transactional_arg) { }
153
164
 
154
 
  if (l && l->qcache_invalidate_db)
 
165
  inline result_type operator()(argument_type handler)
155
166
  {
156
 
    if (l->qcache_invalidate_db(session, parms->dbname, parms->transactional))
 
167
    if (handler->invalidate_db(session, dbname, is_transactional))
157
168
    {
158
169
      errmsg_printf(ERRMSG_LVL_ERROR,
159
170
                    _("qcache plugin '%s' invalidate_db() failed"),
160
 
                    (char *)plugin_name(plugin));
 
171
                    handler->getName().c_str());
161
172
      return true;
162
173
    }
 
174
    return false;
163
175
  }
164
 
  return false;
165
 
}
 
176
};
166
177
 
167
 
static bool qcache_flush_iterate(Session *session, plugin_ref plugin, void *p)
 
178
class FlushIterate
 
179
 : public unary_function<QueryCache *, bool>
168
180
{
169
 
  qcache_t *l = plugin_data(plugin, qcache_t *);
170
 
  
171
 
  if (p) return true; /* flush has no parameters, return failure */
 
181
  Session *session;
 
182
public:
 
183
  FlushIterate(Session *session_arg) :
 
184
    unary_function<QueryCache *, bool>(), session(session_arg) { }
172
185
 
173
 
  if (l && l->qcache_flush)
 
186
  inline result_type operator()(argument_type handler)
174
187
  {
175
 
    if (l->qcache_flush(session))
 
188
    if (handler->flush(session))
176
189
    {
177
190
      errmsg_printf(ERRMSG_LVL_ERROR, _("qcache plugin '%s' flush() failed"),
178
 
                    (char *)plugin_name(plugin));
 
191
                    handler->getName().c_str());
179
192
      return true;
180
193
    }
 
194
    return false;
181
195
  }
182
 
  return false;
183
 
}
184
 
 
 
196
};
 
197
 
 
198
} /* namespace query_cache */
 
199
} /* namespace drizzled */
 
200
 
 
201
using namespace drizzled::query_cache;
185
202
 
186
203
/*
187
204
  Following functions:
198
215
 
199
216
bool drizzle_qcache_try_fetch_and_send(Session *session, bool transactional)
200
217
{
201
 
  bool foreach_rv;
202
 
  foreach_rv= plugin_foreach(session,
203
 
                             qcache_try_fetch_and_send_iterate,
204
 
                             DRIZZLE_QCACHE_PLUGIN,
205
 
                             (void *) &transactional);
206
 
  return foreach_rv;
 
218
  /* Use find_if instead of foreach so that we can collect return codes */
 
219
  vector<QueryCache *>::iterator iter=
 
220
    find_if(all_query_cache.begin(), all_query_cache.end(),
 
221
            TryFetchAndSendIterate(session, transactional));
 
222
  /* If iter is == end() here, that means that all of the plugins returned
 
223
   * false, which in this case means they all succeeded. Since we want to 
 
224
   * return false on success, we return the value of the two being != 
 
225
   */
 
226
  return iter != all_query_cache.end();
207
227
}
208
228
 
209
229
bool drizzle_qcache_set(Session *session, bool transactional)
210
230
{
211
 
  bool foreach_rv;
212
 
  foreach_rv= plugin_foreach(session,
213
 
                             qcache_set_iterate,
214
 
                             DRIZZLE_QCACHE_PLUGIN,
215
 
                             (void *) &transactional);
216
 
  return foreach_rv;
 
231
  /* Use find_if instead of foreach so that we can collect return codes */
 
232
  vector<QueryCache *>::iterator iter=
 
233
    find_if(all_query_cache.begin(), all_query_cache.end(),
 
234
            SetIterate(session, transactional));
 
235
  /* If iter is == end() here, that means that all of the plugins returned
 
236
   * false, which in this case means they all succeeded. Since we want to 
 
237
   * return false on success, we return the value of the two being != 
 
238
   */
 
239
  return iter != all_query_cache.end();
217
240
}
218
241
 
219
242
bool drizzle_qcache_invalidate_table(Session *session, bool transactional)
220
243
{
221
 
  bool foreach_rv;
222
 
  foreach_rv= plugin_foreach(session,
223
 
                             qcache_invalidate_table_iterate,
224
 
                             DRIZZLE_QCACHE_PLUGIN,
225
 
                             (void *) &transactional);
226
 
  return foreach_rv;
 
244
  /* Use find_if instead of foreach so that we can collect return codes */
 
245
  vector<QueryCache *>::iterator iter=
 
246
    find_if(all_query_cache.begin(), all_query_cache.end(),
 
247
            InvalidateTableIterate(session, transactional));
 
248
  /* If iter is == end() here, that means that all of the plugins returned
 
249
   * false, which in this case means they all succeeded. Since we want to 
 
250
   * return false on success, we return the value of the two being != 
 
251
   */
 
252
  return iter != all_query_cache.end();
227
253
}
228
254
 
229
255
bool drizzle_qcache_invalidate_db(Session *session, const char *dbname,
230
256
                                  bool transactional)
231
257
{
232
 
  bool foreach_rv;
233
 
  db_invalidation_parms_t parms;
234
 
 
235
 
  /* marshall the parameters */
236
 
  parms.dbname = dbname;
237
 
  parms.transactional = transactional;
238
 
 
239
 
  foreach_rv= plugin_foreach(session,
240
 
                             qcache_invalidate_db_iterate,
241
 
                             DRIZZLE_QCACHE_PLUGIN,
242
 
                             (void *) &parms);
243
 
  return foreach_rv;
 
258
  /* Use find_if instead of foreach so that we can collect return codes */
 
259
  vector<QueryCache *>::iterator iter=
 
260
    find_if(all_query_cache.begin(), all_query_cache.end(),
 
261
            InvalidateDbIterate(session, dbname, transactional));
 
262
  /* If iter is == end() here, that means that all of the plugins returned
 
263
   * false, which in this case means they all succeeded. Since we want to 
 
264
   * return false on success, we return the value of the two being != 
 
265
   */
 
266
  return iter != all_query_cache.end();
244
267
}
245
268
 
246
269
bool drizzle_qcache_flush(Session *session)
247
270
{
248
 
  bool foreach_rv;
249
 
  foreach_rv= plugin_foreach(session,
250
 
                             qcache_flush_iterate,
251
 
                             DRIZZLE_QCACHE_PLUGIN,
252
 
                             NULL);
253
 
  return foreach_rv;
 
271
  /* Use find_if instead of foreach so that we can collect return codes */
 
272
  vector<QueryCache *>::iterator iter=
 
273
    find_if(all_query_cache.begin(), all_query_cache.end(),
 
274
            FlushIterate(session));
 
275
  /* If iter is == end() here, that means that all of the plugins returned
 
276
   * false, which in this case means they all succeeded. Since we want to 
 
277
   * return false on success, we return the value of the two being != 
 
278
   */
 
279
  return iter != all_query_cache.end();
254
280
}