~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
1643.6.5 by Djellel E. Difallah
retrieve data from the local cache
5
 *  Copyright (C) 2010 Djellel Eddine Difallah
499.2.10 by Mark Atwood
add editor format hints, and other useful metadata comments
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; version 2 of the License.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 */
20
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
21
#include "config.h"
1130.1.1 by Monty Taylor
Merged in plugin-slot-reorg patches.
22
#include "drizzled/plugin/query_cache.h"
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
23
#include "drizzled/errmsg_print.h"
1130.1.1 by Monty Taylor
Merged in plugin-slot-reorg patches.
24
25
#include "drizzled/gettext.h"
26
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
27
#include <algorithm>
968.2.37 by Monty Taylor
Converted qcache plugin.
28
#include <vector>
29
1241.9.26 by Monty Taylor
Removed forward declares from server_includes.h
30
class Session;
31
968.2.37 by Monty Taylor
Converted qcache plugin.
32
using namespace std;
33
1130.3.10 by Monty Taylor
Cleaned up service namespacing.
34
namespace drizzled
35
{
1643.6.18 by Djellel E. Difallah
typedef of vector<plugin::QueryCache *> into QueryCaches
36
typedef vector<plugin::QueryCache *> QueryCaches;
37
QueryCaches all_query_cache;
968.2.37 by Monty Taylor
Converted qcache plugin.
38
39
/* Namespaces are here to prevent global symbol clashes with these classes */
40
1643.6.3 by Djellel E. Difallah
Refactoring of the QC Plugin's interface methods, adding mutual exclusion mechanism between sessions to cache a query, Store Meta inoformation of all the tables and select fields of the query
41
class IsCachedIterate
42
 : public unary_function<plugin::QueryCache *, bool>
43
{
44
  Session *session;
45
public:
46
  IsCachedIterate(Session* session_arg) :
47
    unary_function<plugin::QueryCache *, bool>(),
48
    session(session_arg) { }
49
50
  inline result_type operator()(argument_type handler)
51
  {
52
    return handler->doIsCached(session);
53
  }
54
};
55
56
bool plugin::QueryCache::isCached(Session *session)
57
{
58
  /* Use find_if instead of foreach so that we can collect return codes */
1643.6.18 by Djellel E. Difallah
typedef of vector<plugin::QueryCache *> into QueryCaches
59
  QueryCaches::iterator iter=
1643.6.3 by Djellel E. Difallah
Refactoring of the QC Plugin's interface methods, adding mutual exclusion mechanism between sessions to cache a query, Store Meta inoformation of all the tables and select fields of the query
60
    find_if(all_query_cache.begin(), all_query_cache.end(),
61
            IsCachedIterate(session));
62
  /* If iter is == end() here, that means that all of the plugins returned
63
   * false, which in this case means they all succeeded. Since we want to 
64
   * return false on success, we return the value of the two being != 
65
   */
66
  return iter != all_query_cache.end();
67
}
68
69
70
class SendCachedResultsetIterate
71
 : public unary_function<plugin::QueryCache *, bool>
72
{
73
  Session *session;
74
public:
75
  SendCachedResultsetIterate(Session *session_arg) :
76
    unary_function<plugin::QueryCache *, bool>(),
77
    session(session_arg) { }
78
79
  inline result_type operator()(argument_type handler)
80
  {
1643.6.5 by Djellel E. Difallah
retrieve data from the local cache
81
    return handler->doSendCachedResultset(session);
1643.6.3 by Djellel E. Difallah
Refactoring of the QC Plugin's interface methods, adding mutual exclusion mechanism between sessions to cache a query, Store Meta inoformation of all the tables and select fields of the query
82
  }
83
};
84
bool plugin::QueryCache::sendCachedResultset(Session *session)
85
{
86
  /* Use find_if instead of foreach so that we can collect return codes */
1643.6.18 by Djellel E. Difallah
typedef of vector<plugin::QueryCache *> into QueryCaches
87
  QueryCaches::iterator iter=
1643.6.3 by Djellel E. Difallah
Refactoring of the QC Plugin's interface methods, adding mutual exclusion mechanism between sessions to cache a query, Store Meta inoformation of all the tables and select fields of the query
88
    find_if(all_query_cache.begin(), all_query_cache.end(),
89
            SendCachedResultsetIterate(session));
90
  /* If iter is == end() here, that means that all of the plugins returned
91
   * false, which in this case means they all succeeded. Since we want to 
92
   * return false on success, we return the value of the two being != 
93
   */
94
  return iter != all_query_cache.end();
95
}
1643.6.1 by Djellel E. Difallah
Added hook points and the interface for the Query Cache plugin
96
97
class PrepareResultsetIterate
98
 : public unary_function<plugin::QueryCache *, bool>
99
{
100
  Session *session;
101
public:
102
  PrepareResultsetIterate(Session *session_arg) :
1130.1.1 by Monty Taylor
Merged in plugin-slot-reorg patches.
103
    unary_function<plugin::QueryCache *, bool>(), session(session_arg) { }
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
104
968.2.37 by Monty Taylor
Converted qcache plugin.
105
  inline result_type operator()(argument_type handler)
772.1.1 by Toru Maesaka
Defined the first version of the Query Cache interface.
106
  {
1643.6.3 by Djellel E. Difallah
Refactoring of the QC Plugin's interface methods, adding mutual exclusion mechanism between sessions to cache a query, Store Meta inoformation of all the tables and select fields of the query
107
    return handler->doPrepareResultset(session);
1643.6.1 by Djellel E. Difallah
Added hook points and the interface for the Query Cache plugin
108
  }
109
};
1643.6.3 by Djellel E. Difallah
Refactoring of the QC Plugin's interface methods, adding mutual exclusion mechanism between sessions to cache a query, Store Meta inoformation of all the tables and select fields of the query
110
bool plugin::QueryCache::prepareResultset(Session *session)
111
{
112
  /* Use find_if instead of foreach so that we can collect return codes */
1643.6.18 by Djellel E. Difallah
typedef of vector<plugin::QueryCache *> into QueryCaches
113
  QueryCaches::iterator iter=
1643.6.3 by Djellel E. Difallah
Refactoring of the QC Plugin's interface methods, adding mutual exclusion mechanism between sessions to cache a query, Store Meta inoformation of all the tables and select fields of the query
114
    find_if(all_query_cache.begin(), all_query_cache.end(),
115
            PrepareResultsetIterate(session));
116
  /* If iter is == end() here, that means that all of the plugins returned
117
   * false, which in this case means they all succeeded. Since we want to 
118
   * return false on success, we return the value of the two being != 
119
   */
120
  return iter != all_query_cache.end();
121
}
1643.6.1 by Djellel E. Difallah
Added hook points and the interface for the Query Cache plugin
122
123
class SetResultsetIterate
124
 : public unary_function<plugin::QueryCache *, bool>
125
{
126
  Session *session;
127
public:
128
  SetResultsetIterate(Session *session_arg) :
129
    unary_function<plugin::QueryCache *, bool>(),
130
    session(session_arg) { }
131
132
  inline result_type operator()(argument_type handler)
133
  {
1643.6.3 by Djellel E. Difallah
Refactoring of the QC Plugin's interface methods, adding mutual exclusion mechanism between sessions to cache a query, Store Meta inoformation of all the tables and select fields of the query
134
    return handler->doSetResultset(session);
1643.6.1 by Djellel E. Difallah
Added hook points and the interface for the Query Cache plugin
135
  }
136
};
137
1643.6.3 by Djellel E. Difallah
Refactoring of the QC Plugin's interface methods, adding mutual exclusion mechanism between sessions to cache a query, Store Meta inoformation of all the tables and select fields of the query
138
bool plugin::QueryCache::setResultset(Session *session)
139
{
140
  /* Use find_if instead of foreach so that we can collect return codes */
1643.6.18 by Djellel E. Difallah
typedef of vector<plugin::QueryCache *> into QueryCaches
141
  QueryCaches::iterator iter=
1643.6.3 by Djellel E. Difallah
Refactoring of the QC Plugin's interface methods, adding mutual exclusion mechanism between sessions to cache a query, Store Meta inoformation of all the tables and select fields of the query
142
    find_if(all_query_cache.begin(), all_query_cache.end(),
143
            SetResultsetIterate(session));
144
  /* If iter is == end() here, that means that all of the plugins returned
145
   * false, which in this case means they all succeeded. Since we want to 
146
   * return false on success, we return the value of the two being != 
147
   */
148
  return iter != all_query_cache.end();
149
}
150
1643.6.1 by Djellel E. Difallah
Added hook points and the interface for the Query Cache plugin
151
class InsertRecordIterate
152
 : public unary_function<plugin::QueryCache *, bool>
153
{
154
  Session *session;
155
  List<Item> &item;
156
public:
157
  InsertRecordIterate(Session *session_arg, List<Item> &item_arg) :
158
    unary_function<plugin::QueryCache *, bool>(),
159
    session(session_arg), item(item_arg) { }
160
161
  inline result_type operator()(argument_type handler)
162
  {
1643.6.3 by Djellel E. Difallah
Refactoring of the QC Plugin's interface methods, adding mutual exclusion mechanism between sessions to cache a query, Store Meta inoformation of all the tables and select fields of the query
163
    return handler->doInsertRecord(session, item);
1643.6.1 by Djellel E. Difallah
Added hook points and the interface for the Query Cache plugin
164
  }
165
};
1643.6.3 by Djellel E. Difallah
Refactoring of the QC Plugin's interface methods, adding mutual exclusion mechanism between sessions to cache a query, Store Meta inoformation of all the tables and select fields of the query
166
bool plugin::QueryCache::insertRecord(Session *session, List<Item> &items)
167
{
168
  /* Use find_if instead of foreach so that we can collect return codes */
1643.6.18 by Djellel E. Difallah
typedef of vector<plugin::QueryCache *> into QueryCaches
169
  QueryCaches::iterator iter=
1643.6.3 by Djellel E. Difallah
Refactoring of the QC Plugin's interface methods, adding mutual exclusion mechanism between sessions to cache a query, Store Meta inoformation of all the tables and select fields of the query
170
    find_if(all_query_cache.begin(), all_query_cache.end(),
171
            InsertRecordIterate(session, items));
172
  /* If iter is == end() here, that means that all of the plugins returned
173
   * false, which in this case means they all succeeded. Since we want to 
174
   * return false on success, we return the value of the two being != 
175
   */
176
  return iter != all_query_cache.end();
177
}
1643.6.1 by Djellel E. Difallah
Added hook points and the interface for the Query Cache plugin
178
179
968.2.37 by Monty Taylor
Converted qcache plugin.
180
1130.1.19 by Monty Taylor
Added error reporting to plugin registration.
181
bool plugin::QueryCache::addPlugin(plugin::QueryCache *handler)
1130.1.1 by Monty Taylor
Merged in plugin-slot-reorg patches.
182
{
183
  all_query_cache.push_back(handler);
1130.1.19 by Monty Taylor
Added error reporting to plugin registration.
184
  return false;
1130.1.1 by Monty Taylor
Merged in plugin-slot-reorg patches.
185
}
186
1130.1.18 by Monty Taylor
Changed ::add() and ::remove() to ::addPlugin() and ::removePlugin() so that
187
void plugin::QueryCache::removePlugin(plugin::QueryCache *handler)
1130.1.1 by Monty Taylor
Merged in plugin-slot-reorg patches.
188
{
189
  all_query_cache.erase(find(all_query_cache.begin(), all_query_cache.end(),
190
                        handler));
191
}
192
1130.3.10 by Monty Taylor
Cleaned up service namespacing.
193
} /* namespace drizzled */