~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/qcache.cc

  • Committer: Monty Taylor
  • Date: 2009-04-25 19:24:49 UTC
  • mto: (997.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 1003.
  • Revision ID: mordred@inaugust.com-20090425192449-0htujbt2r9jzupn5
Moved heap.

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
5
 
 *  Copyright (C) 2010 Djellel Eddine Difallah
 
4
 *  Copyright (C) 2008 Mark Atwood, Toru Maesaka
6
5
 *
7
6
 *  This program is free software; you can redistribute it and/or modify
8
7
 *  it under the terms of the GNU General Public License as published by
18
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
18
 */
20
19
 
21
 
#include "config.h"
22
 
#include "drizzled/plugin/query_cache.h"
23
 
#include "drizzled/errmsg_print.h"
24
 
 
25
 
#include "drizzled/gettext.h"
26
 
 
27
 
#include <algorithm>
 
20
#include <drizzled/server_includes.h>
 
21
#include <drizzled/qcache.h>
 
22
#include <drizzled/gettext.h>
 
23
#include "drizzled/plugin_registry.h"
28
24
#include <vector>
29
25
 
30
 
class Session;
31
 
 
32
 
namespace drizzled
33
 
{
34
 
typedef std::vector<plugin::QueryCache *> QueryCaches;
35
 
QueryCaches all_query_cache;
 
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
 
36
42
 
37
43
/* Namespaces are here to prevent global symbol clashes with these classes */
38
44
 
39
 
class IsCachedIterate
40
 
 : public std::unary_function<plugin::QueryCache *, bool>
41
 
{
42
 
  Session *session;
43
 
public:
44
 
  IsCachedIterate(Session* session_arg) :
45
 
    std::unary_function<plugin::QueryCache *, bool>(),
46
 
    session(session_arg) { }
47
 
 
48
 
  inline result_type operator()(argument_type handler)
49
 
  {
50
 
    return handler->doIsCached(session);
51
 
  }
52
 
};
53
 
 
54
 
bool plugin::QueryCache::isCached(Session *session)
55
 
{
56
 
  /* Use find_if instead of foreach so that we can collect return codes */
57
 
  QueryCaches::iterator iter=
58
 
    std::find_if(all_query_cache.begin(), all_query_cache.end(),
59
 
            IsCachedIterate(session));
60
 
  /* If iter is == end() here, that means that all of the plugins returned
61
 
   * false, which in this case means they all succeeded. Since we want to 
62
 
   * return false on success, we return the value of the two being != 
63
 
   */
64
 
  return iter != all_query_cache.end();
65
 
}
66
 
 
67
 
 
68
 
class SendCachedResultsetIterate
69
 
 : public std::unary_function<plugin::QueryCache *, bool>
70
 
{
71
 
  Session *session;
72
 
public:
73
 
  SendCachedResultsetIterate(Session *session_arg) :
74
 
    std::unary_function<plugin::QueryCache *, bool>(),
75
 
    session(session_arg) { }
76
 
 
77
 
  inline result_type operator()(argument_type handler)
78
 
  {
79
 
    return handler->doSendCachedResultset(session);
80
 
  }
81
 
};
82
 
bool plugin::QueryCache::sendCachedResultset(Session *session)
83
 
{
84
 
  /* Use find_if instead of foreach so that we can collect return codes */
85
 
  QueryCaches::iterator iter=
86
 
    std::find_if(all_query_cache.begin(), all_query_cache.end(),
87
 
                 SendCachedResultsetIterate(session));
88
 
  /* If iter is == end() here, that means that all of the plugins returned
89
 
   * false, which in this case means they all succeeded. Since we want to 
90
 
   * return false on success, we return the value of the two being != 
91
 
   */
92
 
  return iter != all_query_cache.end();
93
 
}
94
 
 
95
 
class PrepareResultsetIterate : public std::unary_function<plugin::QueryCache *, bool>
96
 
{
97
 
  Session *session;
98
 
public:
99
 
  PrepareResultsetIterate(Session *session_arg) :
100
 
    std::unary_function<plugin::QueryCache *, bool>(),
101
 
    session(session_arg) { }
102
 
 
103
 
  inline result_type operator()(argument_type handler)
104
 
  {
105
 
    return handler->doPrepareResultset(session);
106
 
  }
107
 
};
108
 
bool plugin::QueryCache::prepareResultset(Session *session)
109
 
{
110
 
  /* Use find_if instead of foreach so that we can collect return codes */
111
 
  QueryCaches::iterator iter=
112
 
    std::find_if(all_query_cache.begin(), all_query_cache.end(),
113
 
                 PrepareResultsetIterate(session));
114
 
  /* If iter is == end() here, that means that all of the plugins returned
115
 
   * false, which in this case means they all succeeded. Since we want to 
116
 
   * return false on success, we return the value of the two being != 
117
 
   */
118
 
  return iter != all_query_cache.end();
119
 
}
120
 
 
121
 
class SetResultsetIterate : public std::unary_function<plugin::QueryCache *, bool>
122
 
{
123
 
  Session *session;
124
 
public:
125
 
  SetResultsetIterate(Session *session_arg) :
126
 
    std::unary_function<plugin::QueryCache *, bool>(),
127
 
    session(session_arg) { }
128
 
 
129
 
  inline result_type operator()(argument_type handler)
130
 
  {
131
 
    return handler->doSetResultset(session);
132
 
  }
133
 
};
134
 
 
135
 
bool plugin::QueryCache::setResultset(Session *session)
136
 
{
137
 
  /* Use find_if instead of foreach so that we can collect return codes */
138
 
  QueryCaches::iterator iter=
139
 
    std::find_if(all_query_cache.begin(), all_query_cache.end(),
140
 
                 SetResultsetIterate(session));
141
 
  /* If iter is == end() here, that means that all of the plugins returned
142
 
   * false, which in this case means they all succeeded. Since we want to 
143
 
   * return false on success, we return the value of the two being != 
144
 
   */
145
 
  return iter != all_query_cache.end();
146
 
}
147
 
 
148
 
class InsertRecordIterate
149
 
 : public std::unary_function<plugin::QueryCache *, bool>
150
 
{
151
 
  Session *session;
152
 
  List<Item> &item;
153
 
public:
154
 
  InsertRecordIterate(Session *session_arg, List<Item> &item_arg) :
155
 
    std::unary_function<plugin::QueryCache *, bool>(),
156
 
    session(session_arg), item(item_arg) { }
157
 
 
158
 
  inline result_type operator()(argument_type handler)
159
 
  {
160
 
    return handler->doInsertRecord(session, item);
161
 
  }
162
 
};
163
 
bool plugin::QueryCache::insertRecord(Session *session, List<Item> &items)
164
 
{
165
 
  /* Use find_if instead of foreach so that we can collect return codes */
166
 
  QueryCaches::iterator iter=
167
 
    std::find_if(all_query_cache.begin(), all_query_cache.end(),
168
 
                 InsertRecordIterate(session, items));
169
 
  /* If iter is == end() here, that means that all of the plugins returned
170
 
   * false, which in this case means they all succeeded. Since we want to 
171
 
   * return false on success, we return the value of the two being != 
172
 
   */
173
 
  return iter != all_query_cache.end();
174
 
}
175
 
 
176
 
 
177
 
 
178
 
bool plugin::QueryCache::addPlugin(plugin::QueryCache *handler)
179
 
{
180
 
  all_query_cache.push_back(handler);
181
 
  return false;
182
 
}
183
 
 
184
 
void plugin::QueryCache::removePlugin(plugin::QueryCache *handler)
185
 
{
186
 
  all_query_cache.erase(std::find(all_query_cache.begin(), all_query_cache.end(),
187
 
                                  handler));
188
 
}
189
 
 
 
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
} /* namespace query_cache */
190
166
} /* namespace drizzled */
 
167
 
 
168
using namespace drizzled::query_cache;
 
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
{
 
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();
 
194
}
 
195
 
 
196
bool drizzle_qcache_set(Session *session, bool transactional)
 
197
{
 
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();
 
207
}
 
208
 
 
209
bool drizzle_qcache_invalidate_table(Session *session, bool transactional)
 
210
{
 
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();
 
220
}
 
221
 
 
222
bool drizzle_qcache_invalidate_db(Session *session, const char *dbname,
 
223
                                  bool transactional)
 
224
{
 
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();
 
234
}
 
235
 
 
236
bool drizzle_qcache_flush(Session *session)
 
237
{
 
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();
 
247
}