~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/query_cache.cc

Merge Stewart's dead code removal

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, Inc.
5
 
 *  Copyright (C) 2010 Djellel Eddine Difallah
 
4
 *  Copyright (C) 2008 Sun Microsystems
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"
 
20
#include "drizzled/server_includes.h"
22
21
#include "drizzled/plugin/query_cache.h"
23
 
#include "drizzled/errmsg_print.h"
 
22
#include "drizzled/plugin/registry.h"
24
23
 
25
24
#include "drizzled/gettext.h"
26
25
 
27
 
#include <algorithm>
28
26
#include <vector>
29
27
 
30
 
class Session;
 
28
using namespace std;
31
29
 
32
30
namespace drizzled
33
31
{
34
 
typedef std::vector<plugin::QueryCache *> QueryCaches;
35
 
QueryCaches all_query_cache;
 
32
 
 
33
vector<plugin::QueryCache *> all_query_cache;
36
34
 
37
35
/* Namespaces are here to prevent global symbol clashes with these classes */
38
36
 
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
 
 
 
37
class TryFetchAndSendIterate
 
38
 : public unary_function<plugin::QueryCache *, bool>
 
39
{
 
40
  Session *session;
 
41
  bool is_transactional;
 
42
public:
 
43
  TryFetchAndSendIterate(Session *session_arg, bool is_transactional_arg) :
 
44
    unary_function<plugin::QueryCache *, bool>(),
 
45
    session(session_arg), is_transactional(is_transactional_arg) { }
 
46
 
 
47
  inline result_type operator()(argument_type handler)
 
48
  {
 
49
    if (handler->tryFetchAndSend(session, is_transactional))
 
50
    {
 
51
      errmsg_printf(ERRMSG_LVL_ERROR,
 
52
                    _("qcache plugin '%s' try_fetch_and_send() failed"),
 
53
                    handler->getName().c_str());
 
54
      return true;
 
55
    }
 
56
    return false;
 
57
  }
 
58
};
 
59
 
 
60
class SetIterate
 
61
 : public unary_function<plugin::QueryCache *, bool>
 
62
{
 
63
  Session *session;
 
64
  bool is_transactional;
 
65
public:
 
66
  SetIterate(Session *session_arg, bool is_transactional_arg) :
 
67
    unary_function<plugin::QueryCache *, bool>(),
 
68
    session(session_arg), is_transactional(is_transactional_arg) { }
 
69
 
 
70
  inline result_type operator()(argument_type handler)
 
71
  {
 
72
 
 
73
    if (handler->set(session, is_transactional))
 
74
    {
 
75
      errmsg_printf(ERRMSG_LVL_ERROR, _("qcache plugin '%s' set() failed"),
 
76
                    handler->getName().c_str());
 
77
      return true;
 
78
    }
 
79
    return false;
 
80
  }
 
81
};
 
82
 
 
83
class InvalidateTableIterate
 
84
 : public unary_function<plugin::QueryCache *, bool>
 
85
{
 
86
  Session *session;
 
87
  bool is_transactional;
 
88
public:
 
89
  InvalidateTableIterate(Session *session_arg, bool is_transactional_arg) :
 
90
    unary_function<plugin::QueryCache *, bool>(),
 
91
    session(session_arg), is_transactional(is_transactional_arg) { }
 
92
 
 
93
  inline result_type operator()(argument_type handler)
 
94
  {
 
95
 
 
96
    if (handler->invalidateTable(session, is_transactional))
 
97
    {
 
98
      errmsg_printf(ERRMSG_LVL_ERROR,
 
99
                    _("qcache plugin '%s' invalidateTable() failed"),
 
100
                    handler->getName().c_str());
 
101
      return true;
 
102
    }
 
103
    return false;
 
104
  }
 
105
};
 
106
 
 
107
 
 
108
class InvalidateDbIterate
 
109
 : public unary_function<plugin::QueryCache *, bool>
 
110
{
 
111
  Session *session;
 
112
  const char *dbname;
 
113
  bool is_transactional;
 
114
public:
 
115
  InvalidateDbIterate(Session *session_arg, const char *dbname_arg,
 
116
                      bool is_transactional_arg) :
 
117
    unary_function<plugin::QueryCache *, bool>(),
 
118
    session(session_arg), dbname(dbname_arg),
 
119
    is_transactional(is_transactional_arg) { }
 
120
 
 
121
  inline result_type operator()(argument_type handler)
 
122
  {
 
123
    if (handler->invalidateDb(session, dbname, is_transactional))
 
124
    {
 
125
      errmsg_printf(ERRMSG_LVL_ERROR,
 
126
                    _("qcache plugin '%s' invalidateDb() failed"),
 
127
                    handler->getName().c_str());
 
128
      return true;
 
129
    }
 
130
    return false;
 
131
  }
 
132
};
 
133
 
 
134
class FlushIterate
 
135
 : public unary_function<plugin::QueryCache *, bool>
 
136
{
 
137
  Session *session;
 
138
public:
 
139
  FlushIterate(Session *session_arg) :
 
140
    unary_function<plugin::QueryCache *, bool>(), session(session_arg) { }
 
141
 
 
142
  inline result_type operator()(argument_type handler)
 
143
  {
 
144
    if (handler->flush(session))
 
145
    {
 
146
      errmsg_printf(ERRMSG_LVL_ERROR, _("qcache plugin '%s' flush() failed"),
 
147
                    handler->getName().c_str());
 
148
      return true;
 
149
    }
 
150
    return false;
 
151
  }
 
152
};
177
153
 
178
154
bool plugin::QueryCache::addPlugin(plugin::QueryCache *handler)
179
155
{
183
159
 
184
160
void plugin::QueryCache::removePlugin(plugin::QueryCache *handler)
185
161
{
186
 
  all_query_cache.erase(std::find(all_query_cache.begin(), all_query_cache.end(),
187
 
                                  handler));
 
162
  all_query_cache.erase(find(all_query_cache.begin(), all_query_cache.end(),
 
163
                        handler));
 
164
}
 
165
 
 
166
 
 
167
bool plugin::QueryCache::tryFetchAndSendDo(Session *session,
 
168
                                           bool transactional)
 
169
{
 
170
  /* Use find_if instead of foreach so that we can collect return codes */
 
171
  vector<plugin::QueryCache *>::iterator iter=
 
172
    find_if(all_query_cache.begin(), all_query_cache.end(),
 
173
            TryFetchAndSendIterate(session, transactional));
 
174
  /* If iter is == end() here, that means that all of the plugins returned
 
175
   * false, which in this case means they all succeeded. Since we want to 
 
176
   * return false on success, we return the value of the two being != 
 
177
   */
 
178
  return iter != all_query_cache.end();
 
179
}
 
180
 
 
181
bool plugin::QueryCache::setDo(Session *session, bool transactional)
 
182
{
 
183
  /* Use find_if instead of foreach so that we can collect return codes */
 
184
  vector<plugin::QueryCache *>::iterator iter=
 
185
    find_if(all_query_cache.begin(), all_query_cache.end(),
 
186
            SetIterate(session, transactional));
 
187
  /* If iter is == end() here, that means that all of the plugins returned
 
188
   * false, which in this case means they all succeeded. Since we want to 
 
189
   * return false on success, we return the value of the two being != 
 
190
   */
 
191
  return iter != all_query_cache.end();
 
192
}
 
193
 
 
194
bool plugin::QueryCache::invalidateTableDo(Session *session,
 
195
                                         bool transactional)
 
196
{
 
197
  /* Use find_if instead of foreach so that we can collect return codes */
 
198
  vector<plugin::QueryCache *>::iterator iter=
 
199
    find_if(all_query_cache.begin(), all_query_cache.end(),
 
200
            InvalidateTableIterate(session, transactional));
 
201
  /* If iter is == end() here, that means that all of the plugins returned
 
202
   * false, which in this case means they all succeeded. Since we want to 
 
203
   * return false on success, we return the value of the two being != 
 
204
   */
 
205
  return iter != all_query_cache.end();
 
206
}
 
207
 
 
208
bool plugin::QueryCache::invalidateDbDo(Session *session, const char *dbname,
 
209
                                        bool transactional)
 
210
{
 
211
  /* Use find_if instead of foreach so that we can collect return codes */
 
212
  vector<plugin::QueryCache *>::iterator iter=
 
213
    find_if(all_query_cache.begin(), all_query_cache.end(),
 
214
            InvalidateDbIterate(session, dbname, 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 plugin::QueryCache::flushDo(Session *session)
 
223
{
 
224
  /* Use find_if instead of foreach so that we can collect return codes */
 
225
  vector<plugin::QueryCache *>::iterator iter=
 
226
    find_if(all_query_cache.begin(), all_query_cache.end(),
 
227
            FlushIterate(session));
 
228
  /* If iter is == end() here, that means that all of the plugins returned
 
229
   * false, which in this case means they all succeeded. Since we want to 
 
230
   * return false on success, we return the value of the two being != 
 
231
   */
 
232
  return iter != all_query_cache.end();
188
233
}
189
234
 
190
235
} /* namespace drizzled */