~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/qcache.cc

  • Committer: Monty Taylor
  • Date: 2008-12-08 01:15:27 UTC
  • mto: This revision was merged to the branch mainline in revision 670.
  • Revision ID: monty@inaugust.com-20081208011527-lq9m47jsmiiqn999
Replaced my hacked up m4/ac_system_extensions.m4 with the one from gnulib.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
 
4
 *  Copyright (C) 2008 Mark Atwood
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include "config.h"
21
 
#include "drizzled/plugin/query_cache.h"
22
 
#include "drizzled/plugin/registry.h"
23
 
 
24
 
#include "drizzled/gettext.h"
25
 
 
26
 
#include <vector>
27
 
 
28
 
class Session;
29
 
 
30
 
using namespace std;
31
 
 
32
 
namespace drizzled
33
 
{
34
 
 
35
 
vector<plugin::QueryCache *> all_query_cache;
36
 
 
37
 
/* Namespaces are here to prevent global symbol clashes with these classes */
38
 
 
39
 
class TryFetchAndSendIterate
40
 
 : public unary_function<plugin::QueryCache *, bool>
41
 
{
42
 
  Session *session;
43
 
  bool is_transactional;
44
 
public:
45
 
  TryFetchAndSendIterate(Session *session_arg, bool is_transactional_arg) :
46
 
    unary_function<plugin::QueryCache *, bool>(),
47
 
    session(session_arg), is_transactional(is_transactional_arg) { }
48
 
 
49
 
  inline result_type operator()(argument_type handler)
50
 
  {
51
 
    if (handler->tryFetchAndSend(session, is_transactional))
52
 
    {
53
 
      errmsg_printf(ERRMSG_LVL_ERROR,
54
 
                    _("qcache plugin '%s' try_fetch_and_send() failed"),
55
 
                    handler->getName().c_str());
56
 
      return true;
57
 
    }
58
 
    return false;
59
 
  }
60
 
};
61
 
 
62
 
class SetIterate
63
 
 : public unary_function<plugin::QueryCache *, bool>
64
 
{
65
 
  Session *session;
66
 
  bool is_transactional;
67
 
public:
68
 
  SetIterate(Session *session_arg, bool is_transactional_arg) :
69
 
    unary_function<plugin::QueryCache *, bool>(),
70
 
    session(session_arg), is_transactional(is_transactional_arg) { }
71
 
 
72
 
  inline result_type operator()(argument_type handler)
73
 
  {
74
 
 
75
 
    if (handler->set(session, is_transactional))
76
 
    {
77
 
      errmsg_printf(ERRMSG_LVL_ERROR, _("qcache plugin '%s' set() failed"),
78
 
                    handler->getName().c_str());
79
 
      return true;
80
 
    }
81
 
    return false;
82
 
  }
83
 
};
84
 
 
85
 
class InvalidateTableIterate
86
 
 : public unary_function<plugin::QueryCache *, bool>
87
 
{
88
 
  Session *session;
89
 
  bool is_transactional;
90
 
public:
91
 
  InvalidateTableIterate(Session *session_arg, bool is_transactional_arg) :
92
 
    unary_function<plugin::QueryCache *, bool>(),
93
 
    session(session_arg), is_transactional(is_transactional_arg) { }
94
 
 
95
 
  inline result_type operator()(argument_type handler)
96
 
  {
97
 
 
98
 
    if (handler->invalidateTable(session, is_transactional))
99
 
    {
100
 
      errmsg_printf(ERRMSG_LVL_ERROR,
101
 
                    _("qcache plugin '%s' invalidateTable() failed"),
102
 
                    handler->getName().c_str());
103
 
      return true;
104
 
    }
105
 
    return false;
106
 
  }
107
 
};
108
 
 
109
 
 
110
 
class InvalidateDbIterate
111
 
 : public unary_function<plugin::QueryCache *, bool>
112
 
{
113
 
  Session *session;
114
 
  const char *dbname;
115
 
  bool is_transactional;
116
 
public:
117
 
  InvalidateDbIterate(Session *session_arg, const char *dbname_arg,
118
 
                      bool is_transactional_arg) :
119
 
    unary_function<plugin::QueryCache *, bool>(),
120
 
    session(session_arg), dbname(dbname_arg),
121
 
    is_transactional(is_transactional_arg) { }
122
 
 
123
 
  inline result_type operator()(argument_type handler)
124
 
  {
125
 
    if (handler->invalidateDb(session, dbname, is_transactional))
126
 
    {
127
 
      errmsg_printf(ERRMSG_LVL_ERROR,
128
 
                    _("qcache plugin '%s' invalidateDb() failed"),
129
 
                    handler->getName().c_str());
130
 
      return true;
131
 
    }
132
 
    return false;
133
 
  }
134
 
};
135
 
 
136
 
class FlushIterate
137
 
 : public unary_function<plugin::QueryCache *, bool>
138
 
{
139
 
  Session *session;
140
 
public:
141
 
  FlushIterate(Session *session_arg) :
142
 
    unary_function<plugin::QueryCache *, bool>(), session(session_arg) { }
143
 
 
144
 
  inline result_type operator()(argument_type handler)
145
 
  {
146
 
    if (handler->flush(session))
147
 
    {
148
 
      errmsg_printf(ERRMSG_LVL_ERROR, _("qcache plugin '%s' flush() failed"),
149
 
                    handler->getName().c_str());
150
 
      return true;
151
 
    }
152
 
    return false;
153
 
  }
154
 
};
155
 
 
156
 
bool plugin::QueryCache::addPlugin(plugin::QueryCache *handler)
157
 
{
158
 
  all_query_cache.push_back(handler);
159
 
  return false;
160
 
}
161
 
 
162
 
void plugin::QueryCache::removePlugin(plugin::QueryCache *handler)
163
 
{
164
 
  all_query_cache.erase(find(all_query_cache.begin(), all_query_cache.end(),
165
 
                        handler));
166
 
}
167
 
 
168
 
 
169
 
bool plugin::QueryCache::tryFetchAndSendDo(Session *session,
170
 
                                           bool transactional)
171
 
{
172
 
  /* Use find_if instead of foreach so that we can collect return codes */
173
 
  vector<plugin::QueryCache *>::iterator iter=
174
 
    find_if(all_query_cache.begin(), all_query_cache.end(),
175
 
            TryFetchAndSendIterate(session, transactional));
176
 
  /* If iter is == end() here, that means that all of the plugins returned
177
 
   * false, which in this case means they all succeeded. Since we want to 
178
 
   * return false on success, we return the value of the two being != 
179
 
   */
180
 
  return iter != all_query_cache.end();
181
 
}
182
 
 
183
 
bool plugin::QueryCache::setDo(Session *session, bool transactional)
184
 
{
185
 
  /* Use find_if instead of foreach so that we can collect return codes */
186
 
  vector<plugin::QueryCache *>::iterator iter=
187
 
    find_if(all_query_cache.begin(), all_query_cache.end(),
188
 
            SetIterate(session, transactional));
189
 
  /* If iter is == end() here, that means that all of the plugins returned
190
 
   * false, which in this case means they all succeeded. Since we want to 
191
 
   * return false on success, we return the value of the two being != 
192
 
   */
193
 
  return iter != all_query_cache.end();
194
 
}
195
 
 
196
 
bool plugin::QueryCache::invalidateTableDo(Session *session,
197
 
                                         bool transactional)
198
 
{
199
 
  /* Use find_if instead of foreach so that we can collect return codes */
200
 
  vector<plugin::QueryCache *>::iterator iter=
201
 
    find_if(all_query_cache.begin(), all_query_cache.end(),
202
 
            InvalidateTableIterate(session, transactional));
203
 
  /* If iter is == end() here, that means that all of the plugins returned
204
 
   * false, which in this case means they all succeeded. Since we want to 
205
 
   * return false on success, we return the value of the two being != 
206
 
   */
207
 
  return iter != all_query_cache.end();
208
 
}
209
 
 
210
 
bool plugin::QueryCache::invalidateDbDo(Session *session, const char *dbname,
211
 
                                        bool transactional)
212
 
{
213
 
  /* Use find_if instead of foreach so that we can collect return codes */
214
 
  vector<plugin::QueryCache *>::iterator iter=
215
 
    find_if(all_query_cache.begin(), all_query_cache.end(),
216
 
            InvalidateDbIterate(session, dbname, transactional));
217
 
  /* If iter is == end() here, that means that all of the plugins returned
218
 
   * false, which in this case means they all succeeded. Since we want to 
219
 
   * return false on success, we return the value of the two being != 
220
 
   */
221
 
  return iter != all_query_cache.end();
222
 
}
223
 
 
224
 
bool plugin::QueryCache::flushDo(Session *session)
225
 
{
226
 
  /* Use find_if instead of foreach so that we can collect return codes */
227
 
  vector<plugin::QueryCache *>::iterator iter=
228
 
    find_if(all_query_cache.begin(), all_query_cache.end(),
229
 
            FlushIterate(session));
230
 
  /* If iter is == end() here, that means that all of the plugins returned
231
 
   * false, which in this case means they all succeeded. Since we want to 
232
 
   * return false on success, we return the value of the two being != 
233
 
   */
234
 
  return iter != all_query_cache.end();
235
 
}
236
 
 
237
 
} /* namespace drizzled */
 
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= (qcache_t *) malloc(sizeof(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
      /* TRANSLATORS: The leading word "qcache" is the name
 
39
         of the plugin api, and so should not be translated. */
 
40
      sql_print_error(_("qcache plugin '%s' init() failed"),
 
41
                      plugin->name.str);
 
42
      goto err;
 
43
    }
 
44
  }
 
45
  return 0;
 
46
 
 
47
err:
 
48
  free(p);
 
49
  return 1;
 
50
}
 
51
 
 
52
int qcache_finalizer(st_plugin_int *plugin)
 
53
{
 
54
  qcache_t *p= (qcache_t *) plugin->data;
 
55
 
 
56
  if (plugin->plugin->deinit)
 
57
  {
 
58
    if (plugin->plugin->deinit((void *)p))
 
59
    {
 
60
      /* TRANSLATORS: The leading word "qcache" is the name
 
61
         of the plugin api, and so should not be translated. */
 
62
      sql_print_error(_("qcache plugin '%s' deinit() failed"),
 
63
                      plugin->name.str);
 
64
    }
 
65
  }
 
66
 
 
67
  if (p) free(p);
 
68
 
 
69
  return 0;
 
70
}
 
71
 
 
72
/* The plugin_foreach() iterator requires that we
 
73
   convert all the parameters of a plugin api entry point
 
74
   into just one single void ptr, plus the session.
 
75
   So we will take all the additional paramters of qcache_do1,
 
76
   and marshall them into a struct of this type, and
 
77
   then just pass in a pointer to it.
 
78
*/
 
79
typedef struct qcache_do1_parms_st
 
80
{
 
81
  void *parm1;
 
82
  void *parm2;
 
83
} qcache_do1_parms_t;
 
84
 
 
85
/* This gets called by plugin_foreach once for each loaded qcache plugin */
 
86
static bool qcache_do1_iterate (Session *session, plugin_ref plugin, void *p)
 
87
{
 
88
  qcache_t *l= plugin_data(plugin, qcache_t *);
 
89
  qcache_do1_parms_t *parms= (qcache_do1_parms_t *) p;
 
90
 
 
91
  /* call this loaded qcache plugin's qcache_func1 function pointer */
 
92
  if (l && l->qcache_func1)
 
93
  {
 
94
    if (l->qcache_func1(session, parms->parm1, parms->parm2))
 
95
    {
 
96
      /* TRANSLATORS: The leading word "qcache" is the name
 
97
         of the plugin api, and so should not be translated. */
 
98
      sql_print_error(_("qcache plugin '%s' do1() failed"),
 
99
                      (char *)plugin_name(plugin));
 
100
      return true;
 
101
    }
 
102
  }
 
103
  return false;
 
104
}
 
105
 
 
106
/* This is the qcache_do1 entry point.
 
107
   This gets called by the rest of the Drizzle server code */
 
108
bool qcache_do1 (Session *session, void *parm1, void *parm2)
 
109
{
 
110
  qcache_do1_parms_t parms;
 
111
  bool foreach_rv;
 
112
 
 
113
  /* marshall the parameters so they will fit into the foreach */
 
114
  parms.parm1= parm1;
 
115
  parms.parm2= parm2;
 
116
 
 
117
  /* call qcache_do1_iterate
 
118
     once for each loaded qcache plugin */
 
119
  foreach_rv= plugin_foreach(session,
 
120
                             qcache_do1_iterate,
 
121
                             DRIZZLE_QCACHE_PLUGIN,
 
122
                             (void *) &parms);
 
123
  return foreach_rv;
 
124
}
 
125
 
 
126
/* The plugin_foreach() iterator requires that we
 
127
   convert all the parameters of a plugin api entry point
 
128
   into just one single void ptr, plus the session.
 
129
   So we will take all the additional paramters of qcache_do2,
 
130
   and marshall them into a struct of this type, and
 
131
   then just pass in a pointer to it.
 
132
*/
 
133
typedef struct qcache_do2_parms_st
 
134
{
 
135
  void *parm3;
 
136
  void *parm4;
 
137
} qcache_do2_parms_t;
 
138
 
 
139
/* This gets called by plugin_foreach once for each loaded qcache plugin */
 
140
static bool qcache_do2_iterate (Session *session, plugin_ref plugin, void *p)
 
141
{
 
142
  qcache_t *l= plugin_data(plugin, qcache_t *);
 
143
  qcache_do2_parms_t *parms= (qcache_do2_parms_t *) p;
 
144
 
 
145
  /* call this loaded qcache plugin's qcache_func1 function pointer */
 
146
  if (l && l->qcache_func1)
 
147
  {
 
148
    if (l->qcache_func2(session, parms->parm3, parms->parm4))
 
149
    {
 
150
      /* TRANSLATORS: The leading word "qcache" is the name
 
151
         of the plugin api, and so should not be translated. */
 
152
      sql_print_error(_("qcache plugin '%s' qcache_func2() failed"),
 
153
                      (char *)plugin_name(plugin));
 
154
 
 
155
      return true;
 
156
    }
 
157
  }
 
158
  return false;
 
159
}
 
160
 
 
161
/* This is the qcache_do2 entry point.
 
162
   This gets called by the rest of the Drizzle server code */
 
163
bool qcache_do2 (Session *session, void *parm3, void *parm4)
 
164
{
 
165
  qcache_do2_parms_t parms;
 
166
  bool foreach_rv;
 
167
 
 
168
  /* marshall the parameters so they will fit into the foreach */
 
169
  parms.parm3= parm3;
 
170
  parms.parm4= parm4;
 
171
 
 
172
  /* call qcache_do2_iterate
 
173
     once for each loaded qcache plugin */
 
174
  foreach_rv= plugin_foreach(session,
 
175
                             qcache_do2_iterate,
 
176
                             DRIZZLE_QCACHE_PLUGIN,
 
177
                             (void *) &parms);
 
178
  return foreach_rv;
 
179
}