~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
 *
1010 by Brian Aker
Replacing Sun employee copyright headers (aka... anything done by a Sun
4
 *  Copyright (C) 2008 Sun Microsystems
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
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
165
/*
166
  Following functions:
167
1085.1.2 by Monty Taylor
Fixed -Wmissing-declarations
168
    try_fetch_and_send();
169
    set();
170
    invalidate_table();
171
    invalidate_db();
172
    flush();
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
173
174
  are the entry points to the query cache plugin that is called by the
175
  rest of the Drizzle server code.
176
*/
177
1085.1.2 by Monty Taylor
Fixed -Wmissing-declarations
178
bool try_fetch_and_send(Session *session, bool transactional)
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
179
{
968.2.37 by Monty Taylor
Converted qcache plugin.
180
  /* Use find_if instead of foreach so that we can collect return codes */
181
  vector<QueryCache *>::iterator iter=
182
    find_if(all_query_cache.begin(), all_query_cache.end(),
183
            TryFetchAndSendIterate(session, transactional));
184
  /* If iter is == end() here, that means that all of the plugins returned
185
   * false, which in this case means they all succeeded. Since we want to 
186
   * return false on success, we return the value of the two being != 
187
   */
188
  return iter != all_query_cache.end();
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
189
}
190
1085.1.2 by Monty Taylor
Fixed -Wmissing-declarations
191
bool set(Session *session, bool transactional)
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
192
{
968.2.37 by Monty Taylor
Converted qcache plugin.
193
  /* Use find_if instead of foreach so that we can collect return codes */
194
  vector<QueryCache *>::iterator iter=
195
    find_if(all_query_cache.begin(), all_query_cache.end(),
196
            SetIterate(session, transactional));
197
  /* If iter is == end() here, that means that all of the plugins returned
198
   * false, which in this case means they all succeeded. Since we want to 
199
   * return false on success, we return the value of the two being != 
200
   */
201
  return iter != all_query_cache.end();
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
202
}
203
1085.1.2 by Monty Taylor
Fixed -Wmissing-declarations
204
bool invalidate_table(Session *session, bool transactional)
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
205
{
968.2.37 by Monty Taylor
Converted qcache plugin.
206
  /* Use find_if instead of foreach so that we can collect return codes */
207
  vector<QueryCache *>::iterator iter=
208
    find_if(all_query_cache.begin(), all_query_cache.end(),
209
            InvalidateTableIterate(session, transactional));
210
  /* If iter is == end() here, that means that all of the plugins returned
211
   * false, which in this case means they all succeeded. Since we want to 
212
   * return false on success, we return the value of the two being != 
213
   */
214
  return iter != all_query_cache.end();
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
215
}
216
1085.1.2 by Monty Taylor
Fixed -Wmissing-declarations
217
bool invalidate_db(Session *session, const char *dbname,
218
                                         bool transactional)
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
219
{
968.2.37 by Monty Taylor
Converted qcache plugin.
220
  /* Use find_if instead of foreach so that we can collect return codes */
221
  vector<QueryCache *>::iterator iter=
222
    find_if(all_query_cache.begin(), all_query_cache.end(),
223
            InvalidateDbIterate(session, dbname, transactional));
224
  /* If iter is == end() here, that means that all of the plugins returned
225
   * false, which in this case means they all succeeded. Since we want to 
226
   * return false on success, we return the value of the two being != 
227
   */
228
  return iter != all_query_cache.end();
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
229
}
230
1085.1.2 by Monty Taylor
Fixed -Wmissing-declarations
231
bool flush(Session *session)
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
232
{
968.2.37 by Monty Taylor
Converted qcache plugin.
233
  /* Use find_if instead of foreach so that we can collect return codes */
234
  vector<QueryCache *>::iterator iter=
235
    find_if(all_query_cache.begin(), all_query_cache.end(),
236
            FlushIterate(session));
237
  /* If iter is == end() here, that means that all of the plugins returned
238
   * false, which in this case means they all succeeded. Since we want to 
239
   * return false on success, we return the value of the two being != 
240
   */
241
  return iter != all_query_cache.end();
520.2.2 by Mark Atwood
new plugin types, configvar and qcache
242
}
1085.1.2 by Monty Taylor
Fixed -Wmissing-declarations
243
244
} /* namespace query_cache */
245
} /* namespace drizzled */
246