~drizzle-trunk/drizzle/development

499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
499.2.10 by Mark Atwood
add editor format hints, and other useful metadata comments
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
3
 *
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
4
 *  Copyright (C) 2008 Mark Atwood, Toru Maesaka
499.2.10 by Mark Atwood
add editor format hints, and other useful metadata comments
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
520.2.2 by Mark Atwood
new plugin types, configvar and qcache
20
#include <drizzled/server_includes.h>
21
#include <drizzled/qcache.h>
549 by Monty Taylor
Took gettext.h out of header files.
22
#include <drizzled/gettext.h>
971.1.46 by Monty Taylor
Made plugin registration go through Plugin_registry.
23
#include "drizzled/plugin_registry.h"
968.2.37 by Monty Taylor
Converted qcache plugin.
24
#include <vector>
25
26
using namespace std;
27
28
static vector<QueryCache *> all_query_cache;
29
971.1.46 by Monty Taylor
Made plugin registration go through Plugin_registry.
30
void add_query_cache(QueryCache *handler)
968.2.37 by Monty Taylor
Converted qcache plugin.
31
{
32
  all_query_cache.push_back(handler);
33
}
34
971.1.46 by Monty Taylor
Made plugin registration go through Plugin_registry.
35
void remove_query_cache(QueryCache *handler)
968.2.37 by Monty Taylor
Converted qcache plugin.
36
{
37
  all_query_cache.erase(find(all_query_cache.begin(), all_query_cache.end(),
38
                        handler));
39
}
520.2.2 by Mark Atwood
new plugin types, configvar and qcache
40
41
968.2.37 by Monty Taylor
Converted qcache plugin.
42
43
/* Namespaces are here to prevent global symbol clashes with these classes */
44
45
namespace drizzled {
46
namespace query_cache {
47
48
class TryFetchAndSendIterate
49
 : public unary_function<QueryCache *, bool>
50
{
51
  Session *session;
52
  bool is_transactional;
53
public:
54
  TryFetchAndSendIterate(Session *session_arg, bool is_transactional_arg) :
55
    unary_function<QueryCache *, bool>(),
56
    session(session_arg), is_transactional(is_transactional_arg) { }
57
58
  inline result_type operator()(argument_type handler)
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
59
  {
968.2.37 by Monty Taylor
Converted qcache plugin.
60
    if (handler->try_fetch_and_send(session, is_transactional))
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
61
    {
62
      errmsg_printf(ERRMSG_LVL_ERROR,
63
                    _("qcache plugin '%s' try_fetch_and_send() failed"),
968.2.37 by Monty Taylor
Converted qcache plugin.
64
                    handler->getName().c_str());
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
65
      return true;
66
    }
968.2.37 by Monty Taylor
Converted qcache plugin.
67
    return false;
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
68
  }
968.2.37 by Monty Taylor
Converted qcache plugin.
69
};
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
70
968.2.37 by Monty Taylor
Converted qcache plugin.
71
class SetIterate
72
 : public unary_function<QueryCache *, bool>
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
73
{
968.2.37 by Monty Taylor
Converted qcache plugin.
74
  Session *session;
75
  bool is_transactional;
76
public:
77
  SetIterate(Session *session_arg, bool is_transactional_arg) :
78
    unary_function<QueryCache *, bool>(),
79
    session(session_arg), is_transactional(is_transactional_arg) { }
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
80
968.2.37 by Monty Taylor
Converted qcache plugin.
81
  inline result_type operator()(argument_type handler)
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
82
  {
968.2.37 by Monty Taylor
Converted qcache plugin.
83
84
    if (handler->set(session, is_transactional))
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
85
    {
86
      errmsg_printf(ERRMSG_LVL_ERROR, _("qcache plugin '%s' set() failed"),
968.2.37 by Monty Taylor
Converted qcache plugin.
87
                    handler->getName().c_str());
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
88
      return true;
89
    }
968.2.37 by Monty Taylor
Converted qcache plugin.
90
    return false;
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
91
  }
968.2.37 by Monty Taylor
Converted qcache plugin.
92
};
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
93
968.2.37 by Monty Taylor
Converted qcache plugin.
94
class InvalidateTableIterate
95
 : public unary_function<QueryCache *, bool>
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
96
{
968.2.37 by Monty Taylor
Converted qcache plugin.
97
  Session *session;
98
  bool is_transactional;
99
public:
100
  InvalidateTableIterate(Session *session_arg, bool is_transactional_arg) :
101
    unary_function<QueryCache *, bool>(),
102
    session(session_arg), is_transactional(is_transactional_arg) { }
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
103
968.2.37 by Monty Taylor
Converted qcache plugin.
104
  inline result_type operator()(argument_type handler)
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
105
  {
968.2.37 by Monty Taylor
Converted qcache plugin.
106
107
    if (handler->invalidate_table(session, is_transactional))
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
108
    {
109
      errmsg_printf(ERRMSG_LVL_ERROR,
110
                    _("qcache plugin '%s' invalidate_table() failed"),
968.2.37 by Monty Taylor
Converted qcache plugin.
111
                    handler->getName().c_str());
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
112
      return true;
113
    }
968.2.37 by Monty Taylor
Converted qcache plugin.
114
    return false;
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
115
  }
968.2.37 by Monty Taylor
Converted qcache plugin.
116
};
117
118
119
class InvalidateDbIterate
120
 : public unary_function<QueryCache *, bool>
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
121
{
968.2.37 by Monty Taylor
Converted qcache plugin.
122
  Session *session;
123
  const char *dbname;
124
  bool is_transactional;
125
public:
126
  InvalidateDbIterate(Session *session_arg, const char *dbname_arg,
127
                      bool is_transactional_arg) :
128
    unary_function<QueryCache *, bool>(),
129
    session(session_arg), dbname(dbname_arg),
130
    is_transactional(is_transactional_arg) { }
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
131
968.2.37 by Monty Taylor
Converted qcache plugin.
132
  inline result_type operator()(argument_type handler)
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
133
  {
968.2.37 by Monty Taylor
Converted qcache plugin.
134
    if (handler->invalidate_db(session, dbname, is_transactional))
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
135
    {
136
      errmsg_printf(ERRMSG_LVL_ERROR,
137
                    _("qcache plugin '%s' invalidate_db() failed"),
968.2.37 by Monty Taylor
Converted qcache plugin.
138
                    handler->getName().c_str());
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
139
      return true;
140
    }
968.2.37 by Monty Taylor
Converted qcache plugin.
141
    return false;
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
142
  }
968.2.37 by Monty Taylor
Converted qcache plugin.
143
};
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
144
968.2.37 by Monty Taylor
Converted qcache plugin.
145
class FlushIterate
146
 : public unary_function<QueryCache *, bool>
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
147
{
968.2.37 by Monty Taylor
Converted qcache plugin.
148
  Session *session;
149
public:
150
  FlushIterate(Session *session_arg) :
151
    unary_function<QueryCache *, bool>(), session(session_arg) { }
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
152
968.2.37 by Monty Taylor
Converted qcache plugin.
153
  inline result_type operator()(argument_type handler)
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
154
  {
968.2.37 by Monty Taylor
Converted qcache plugin.
155
    if (handler->flush(session))
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
156
    {
157
      errmsg_printf(ERRMSG_LVL_ERROR, _("qcache plugin '%s' flush() failed"),
968.2.37 by Monty Taylor
Converted qcache plugin.
158
                    handler->getName().c_str());
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
159
      return true;
160
    }
968.2.37 by Monty Taylor
Converted qcache plugin.
161
    return false;
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
162
  }
968.2.37 by Monty Taylor
Converted qcache plugin.
163
};
164
165
} /* namespace query_cache */
166
} /* namespace drizzled */
167
168
using namespace drizzled::query_cache;
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
169
170
/*
171
  Following functions:
172
173
    drizzle_qcache_try_fetch_and_send();
174
    drizzle_qcache_set();
175
    drizzle_qcache_invalidate_table();
176
    drizzle_qcache_invalidate_db();
177
    drizzle_qcache_flush();
178
179
  are the entry points to the query cache plugin that is called by the
180
  rest of the Drizzle server code.
181
*/
182
183
bool drizzle_qcache_try_fetch_and_send(Session *session, bool transactional)
184
{
968.2.37 by Monty Taylor
Converted qcache plugin.
185
  /* Use find_if instead of foreach so that we can collect return codes */
186
  vector<QueryCache *>::iterator iter=
187
    find_if(all_query_cache.begin(), all_query_cache.end(),
188
            TryFetchAndSendIterate(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();
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
194
}
195
196
bool drizzle_qcache_set(Session *session, bool transactional)
197
{
968.2.37 by Monty Taylor
Converted qcache plugin.
198
  /* Use find_if instead of foreach so that we can collect return codes */
199
  vector<QueryCache *>::iterator iter=
200
    find_if(all_query_cache.begin(), all_query_cache.end(),
201
            SetIterate(session, transactional));
202
  /* If iter is == end() here, that means that all of the plugins returned
203
   * false, which in this case means they all succeeded. Since we want to 
204
   * return false on success, we return the value of the two being != 
205
   */
206
  return iter != all_query_cache.end();
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
207
}
208
209
bool drizzle_qcache_invalidate_table(Session *session, bool transactional)
210
{
968.2.37 by Monty Taylor
Converted qcache plugin.
211
  /* Use find_if instead of foreach so that we can collect return codes */
212
  vector<QueryCache *>::iterator iter=
213
    find_if(all_query_cache.begin(), all_query_cache.end(),
214
            InvalidateTableIterate(session, transactional));
215
  /* If iter is == end() here, that means that all of the plugins returned
216
   * false, which in this case means they all succeeded. Since we want to 
217
   * return false on success, we return the value of the two being != 
218
   */
219
  return iter != all_query_cache.end();
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
220
}
221
222
bool drizzle_qcache_invalidate_db(Session *session, const char *dbname,
223
                                  bool transactional)
224
{
968.2.37 by Monty Taylor
Converted qcache plugin.
225
  /* Use find_if instead of foreach so that we can collect return codes */
226
  vector<QueryCache *>::iterator iter=
227
    find_if(all_query_cache.begin(), all_query_cache.end(),
228
            InvalidateDbIterate(session, dbname, transactional));
229
  /* If iter is == end() here, that means that all of the plugins returned
230
   * false, which in this case means they all succeeded. Since we want to 
231
   * return false on success, we return the value of the two being != 
232
   */
233
  return iter != all_query_cache.end();
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
234
}
235
236
bool drizzle_qcache_flush(Session *session)
237
{
968.2.37 by Monty Taylor
Converted qcache plugin.
238
  /* Use find_if instead of foreach so that we can collect return codes */
239
  vector<QueryCache *>::iterator iter=
240
    find_if(all_query_cache.begin(), all_query_cache.end(),
241
            FlushIterate(session));
242
  /* If iter is == end() here, that means that all of the plugins returned
243
   * false, which in this case means they all succeeded. Since we want to 
244
   * return false on success, we return the value of the two being != 
245
   */
246
  return iter != all_query_cache.end();
520.2.2 by Mark Atwood
new plugin types, configvar and qcache
247
}