~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/query_cache.cc

  • Committer: Andrew Hutchings
  • Date: 2011-01-21 11:23:19 UTC
  • mto: (2100.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 2101.
  • Revision ID: andrew@linuxjedi.co.uk-20110121112319-nj1cvg0yt3nnf2rr
Add errors page to drizzle client docs
Add link to specific error in migration docs
Minor changes to migration docs

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