~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/qcache.cc

  • Committer: Brian Aker
  • Date: 2009-03-27 22:55:28 UTC
  • mto: This revision was merged to the branch mainline in revision 968.
  • Revision ID: brian@tangent.org-20090327225528-8y76cfx8a4oemqv9
Remove ref_count

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Mark Atwood, Toru Maesaka
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
#include <drizzled/server_includes.h>
 
21
#include <drizzled/qcache.h>
 
22
#include <drizzled/gettext.h>
 
23
 
 
24
int qcache_initializer(st_plugin_int *plugin)
 
25
{
 
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;
 
33
 
 
34
  if (plugin->plugin->init)
 
35
  {
 
36
    if (plugin->plugin->init((void *)p))
 
37
    {
 
38
      errmsg_printf(ERRMSG_LVL_ERROR, _("qcache plugin '%s' init() failed"),
 
39
                    plugin->name.str);
 
40
      goto err;
 
41
    }
 
42
  }
 
43
 
 
44
  return 0;
 
45
 
 
46
err:
 
47
  delete p;
 
48
  return 1;
 
49
}
 
50
 
 
51
int qcache_finalizer(st_plugin_int *plugin)
 
52
{
 
53
  qcache_t *p= (qcache_t *) plugin->data;
 
54
 
 
55
  if (plugin->plugin->deinit)
 
56
  {
 
57
    if (plugin->plugin->deinit((void *)p))
 
58
    {
 
59
      errmsg_printf(ERRMSG_LVL_ERROR, _("qcache plugin '%s' deinit() failed"),
 
60
                    plugin->name.str);
 
61
    }
 
62
  }
 
63
 
 
64
  if (p) delete p;
 
65
  return 0;
 
66
}
 
67
 
 
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)
 
100
  {
 
101
    if (l->qcache_try_fetch_and_send(session, is_transactional))
 
102
    {
 
103
      errmsg_printf(ERRMSG_LVL_ERROR,
 
104
                    _("qcache plugin '%s' try_fetch_and_send() failed"),
 
105
                    (char *)plugin_name(plugin));
 
106
      return true;
 
107
    }
 
108
  }
 
109
  return false;
 
110
}
 
111
 
 
112
static bool qcache_set_iterate(Session *session, plugin_ref plugin, void *p)
 
113
{
 
114
  qcache_t *l = plugin_data(plugin, qcache_t *);
 
115
  bool transactional = (bool *)p;
 
116
 
 
117
  if (l && l->qcache_set)
 
118
  {
 
119
    if (l->qcache_set(session, transactional))
 
120
    {
 
121
      errmsg_printf(ERRMSG_LVL_ERROR, _("qcache plugin '%s' set() failed"),
 
122
                    (char *)plugin_name(plugin));
 
123
      return true;
 
124
    }
 
125
  }
 
126
  return false;
 
127
}
 
128
 
 
129
static bool qcache_invalidate_table_iterate(Session *session,
 
130
                                            plugin_ref plugin, void *p)
 
131
{
 
132
  qcache_t *l = plugin_data(plugin, qcache_t *);
 
133
  bool transactional = (bool *)p;
 
134
 
 
135
  if (l && l->qcache_invalidate_table)
 
136
  {
 
137
    if (l->qcache_invalidate_table(session, transactional))
 
138
    {
 
139
      errmsg_printf(ERRMSG_LVL_ERROR,
 
140
                    _("qcache plugin '%s' invalidate_table() failed"),
 
141
                    (char *)plugin_name(plugin));
 
142
      return true;
 
143
    }
 
144
  }
 
145
  return false;
 
146
}
 
147
 
 
148
static bool qcache_invalidate_db_iterate(Session *session,
 
149
                                         plugin_ref plugin, void *p)
 
150
{
 
151
  qcache_t *l = plugin_data(plugin, qcache_t *);
 
152
  db_invalidation_parms_t *parms = (db_invalidation_parms_t *)p;
 
153
 
 
154
  if (l && l->qcache_invalidate_db)
 
155
  {
 
156
    if (l->qcache_invalidate_db(session, parms->dbname, parms->transactional))
 
157
    {
 
158
      errmsg_printf(ERRMSG_LVL_ERROR,
 
159
                    _("qcache plugin '%s' invalidate_db() failed"),
 
160
                    (char *)plugin_name(plugin));
 
161
      return true;
 
162
    }
 
163
  }
 
164
  return false;
 
165
}
 
166
 
 
167
static bool qcache_flush_iterate(Session *session, plugin_ref plugin, void *p)
 
168
{
 
169
  qcache_t *l = plugin_data(plugin, qcache_t *);
 
170
  
 
171
  if (p) return true; /* flush has no parameters, return failure */
 
172
 
 
173
  if (l && l->qcache_flush)
 
174
  {
 
175
    if (l->qcache_flush(session))
 
176
    {
 
177
      errmsg_printf(ERRMSG_LVL_ERROR, _("qcache plugin '%s' flush() failed"),
 
178
                    (char *)plugin_name(plugin));
 
179
      return true;
 
180
    }
 
181
  }
 
182
  return false;
 
183
}
 
184
 
 
185
 
 
186
/*
 
187
  Following functions:
 
188
 
 
189
    drizzle_qcache_try_fetch_and_send();
 
190
    drizzle_qcache_set();
 
191
    drizzle_qcache_invalidate_table();
 
192
    drizzle_qcache_invalidate_db();
 
193
    drizzle_qcache_flush();
 
194
 
 
195
  are the entry points to the query cache plugin that is called by the
 
196
  rest of the Drizzle server code.
 
197
*/
 
198
 
 
199
bool drizzle_qcache_try_fetch_and_send(Session *session, bool transactional)
 
200
{
 
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;
 
207
}
 
208
 
 
209
bool drizzle_qcache_set(Session *session, bool transactional)
 
210
{
 
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;
 
217
}
 
218
 
 
219
bool drizzle_qcache_invalidate_table(Session *session, bool transactional)
 
220
{
 
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;
 
227
}
 
228
 
 
229
bool drizzle_qcache_invalidate_db(Session *session, const char *dbname,
 
230
                                  bool transactional)
 
231
{
 
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;
 
244
}
 
245
 
 
246
bool drizzle_qcache_flush(Session *session)
 
247
{
 
248
  bool foreach_rv;
 
249
  foreach_rv= plugin_foreach(session,
 
250
                             qcache_flush_iterate,
 
251
                             DRIZZLE_QCACHE_PLUGIN,
 
252
                             NULL);
 
253
  return foreach_rv;
 
254
}