~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/qcache.cc

  • Committer: Brian Aker
  • Date: 2009-08-21 18:46:31 UTC
  • mto: This revision was merged to the branch mainline in revision 1123.
  • Revision ID: brian@gaz-20090821184631-e11gja79070fvhk4
Cleanup around page checksum removal

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 Sun Microsystems
 
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
#include "drizzled/plugin/registry.h"
 
24
#include <vector>
 
25
 
 
26
using namespace std;
 
27
 
 
28
static vector<QueryCache *> all_query_cache;
 
29
 
 
30
void add_query_cache(QueryCache *handler)
 
31
{
 
32
  all_query_cache.push_back(handler);
 
33
}
 
34
 
 
35
void remove_query_cache(QueryCache *handler)
 
36
{
 
37
  all_query_cache.erase(find(all_query_cache.begin(), all_query_cache.end(),
 
38
                        handler));
 
39
}
 
40
 
 
41
 
 
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)
 
59
  {
 
60
    if (handler->try_fetch_and_send(session, is_transactional))
 
61
    {
 
62
      errmsg_printf(ERRMSG_LVL_ERROR,
 
63
                    _("qcache plugin '%s' try_fetch_and_send() failed"),
 
64
                    handler->getName().c_str());
 
65
      return true;
 
66
    }
 
67
    return false;
 
68
  }
 
69
};
 
70
 
 
71
class SetIterate
 
72
 : public unary_function<QueryCache *, bool>
 
73
{
 
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) { }
 
80
 
 
81
  inline result_type operator()(argument_type handler)
 
82
  {
 
83
 
 
84
    if (handler->set(session, is_transactional))
 
85
    {
 
86
      errmsg_printf(ERRMSG_LVL_ERROR, _("qcache plugin '%s' set() failed"),
 
87
                    handler->getName().c_str());
 
88
      return true;
 
89
    }
 
90
    return false;
 
91
  }
 
92
};
 
93
 
 
94
class InvalidateTableIterate
 
95
 : public unary_function<QueryCache *, bool>
 
96
{
 
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) { }
 
103
 
 
104
  inline result_type operator()(argument_type handler)
 
105
  {
 
106
 
 
107
    if (handler->invalidate_table(session, is_transactional))
 
108
    {
 
109
      errmsg_printf(ERRMSG_LVL_ERROR,
 
110
                    _("qcache plugin '%s' invalidate_table() failed"),
 
111
                    handler->getName().c_str());
 
112
      return true;
 
113
    }
 
114
    return false;
 
115
  }
 
116
};
 
117
 
 
118
 
 
119
class InvalidateDbIterate
 
120
 : public unary_function<QueryCache *, bool>
 
121
{
 
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) { }
 
131
 
 
132
  inline result_type operator()(argument_type handler)
 
133
  {
 
134
    if (handler->invalidate_db(session, dbname, is_transactional))
 
135
    {
 
136
      errmsg_printf(ERRMSG_LVL_ERROR,
 
137
                    _("qcache plugin '%s' invalidate_db() failed"),
 
138
                    handler->getName().c_str());
 
139
      return true;
 
140
    }
 
141
    return false;
 
142
  }
 
143
};
 
144
 
 
145
class FlushIterate
 
146
 : public unary_function<QueryCache *, bool>
 
147
{
 
148
  Session *session;
 
149
public:
 
150
  FlushIterate(Session *session_arg) :
 
151
    unary_function<QueryCache *, bool>(), session(session_arg) { }
 
152
 
 
153
  inline result_type operator()(argument_type handler)
 
154
  {
 
155
    if (handler->flush(session))
 
156
    {
 
157
      errmsg_printf(ERRMSG_LVL_ERROR, _("qcache plugin '%s' flush() failed"),
 
158
                    handler->getName().c_str());
 
159
      return true;
 
160
    }
 
161
    return false;
 
162
  }
 
163
};
 
164
 
 
165
/*
 
166
  Following functions:
 
167
 
 
168
    try_fetch_and_send();
 
169
    set();
 
170
    invalidate_table();
 
171
    invalidate_db();
 
172
    flush();
 
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
 
 
178
bool try_fetch_and_send(Session *session, bool transactional)
 
179
{
 
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();
 
189
}
 
190
 
 
191
bool set(Session *session, bool transactional)
 
192
{
 
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();
 
202
}
 
203
 
 
204
bool invalidate_table(Session *session, bool transactional)
 
205
{
 
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();
 
215
}
 
216
 
 
217
bool invalidate_db(Session *session, const char *dbname,
 
218
                                         bool transactional)
 
219
{
 
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();
 
229
}
 
230
 
 
231
bool flush(Session *session)
 
232
{
 
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();
 
242
}
 
243
 
 
244
} /* namespace query_cache */
 
245
} /* namespace drizzled */
 
246