Drizzled Public API Documentation

query_cache.cc
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 Sun Microsystems, Inc.
00005  *  Copyright (C) 2010 Djellel Eddine Difallah
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; version 2 of the License.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 #include <config.h>
00022 #include <drizzled/plugin/query_cache.h>
00023 #include <drizzled/errmsg_print.h>
00024 
00025 #include <drizzled/gettext.h>
00026 
00027 #include <algorithm>
00028 #include <vector>
00029 
00030 class Session;
00031 
00032 namespace drizzled
00033 {
00034 typedef std::vector<plugin::QueryCache *> QueryCaches;
00035 QueryCaches all_query_cache;
00036 
00037 /* Namespaces are here to prevent global symbol clashes with these classes */
00038 
00039 class IsCachedIterate
00040  : public std::unary_function<plugin::QueryCache *, bool>
00041 {
00042   Session *session;
00043 public:
00044   IsCachedIterate(Session* session_arg) :
00045     std::unary_function<plugin::QueryCache *, bool>(),
00046     session(session_arg) { }
00047 
00048   inline result_type operator()(argument_type handler)
00049   {
00050     return handler->doIsCached(session);
00051   }
00052 };
00053 
00054 bool plugin::QueryCache::isCached(Session *session)
00055 {
00056   /* Use find_if instead of foreach so that we can collect return codes */
00057   QueryCaches::iterator iter=
00058     std::find_if(all_query_cache.begin(), all_query_cache.end(),
00059             IsCachedIterate(session));
00060   /* If iter is == end() here, that means that all of the plugins returned
00061    * false, which in this case means they all succeeded. Since we want to 
00062    * return false on success, we return the value of the two being != 
00063    */
00064   return iter != all_query_cache.end();
00065 }
00066 
00067 
00068 class SendCachedResultsetIterate
00069  : public std::unary_function<plugin::QueryCache *, bool>
00070 {
00071   Session *session;
00072 public:
00073   SendCachedResultsetIterate(Session *session_arg) :
00074     std::unary_function<plugin::QueryCache *, bool>(),
00075     session(session_arg) { }
00076 
00077   inline result_type operator()(argument_type handler)
00078   {
00079     return handler->doSendCachedResultset(session);
00080   }
00081 };
00082 bool plugin::QueryCache::sendCachedResultset(Session *session)
00083 {
00084   /* Use find_if instead of foreach so that we can collect return codes */
00085   QueryCaches::iterator iter=
00086     std::find_if(all_query_cache.begin(), all_query_cache.end(),
00087                  SendCachedResultsetIterate(session));
00088   /* If iter is == end() here, that means that all of the plugins returned
00089    * false, which in this case means they all succeeded. Since we want to 
00090    * return false on success, we return the value of the two being != 
00091    */
00092   return iter != all_query_cache.end();
00093 }
00094 
00095 class PrepareResultsetIterate : public std::unary_function<plugin::QueryCache *, bool>
00096 {
00097   Session *session;
00098 public:
00099   PrepareResultsetIterate(Session *session_arg) :
00100     std::unary_function<plugin::QueryCache *, bool>(),
00101     session(session_arg) { }
00102 
00103   inline result_type operator()(argument_type handler)
00104   {
00105     return handler->doPrepareResultset(session);
00106   }
00107 };
00108 bool plugin::QueryCache::prepareResultset(Session *session)
00109 {
00110   /* Use find_if instead of foreach so that we can collect return codes */
00111   QueryCaches::iterator iter=
00112     std::find_if(all_query_cache.begin(), all_query_cache.end(),
00113                  PrepareResultsetIterate(session));
00114   /* If iter is == end() here, that means that all of the plugins returned
00115    * false, which in this case means they all succeeded. Since we want to 
00116    * return false on success, we return the value of the two being != 
00117    */
00118   return iter != all_query_cache.end();
00119 }
00120 
00121 class SetResultsetIterate : public std::unary_function<plugin::QueryCache *, bool>
00122 {
00123   Session *session;
00124 public:
00125   SetResultsetIterate(Session *session_arg) :
00126     std::unary_function<plugin::QueryCache *, bool>(),
00127     session(session_arg) { }
00128 
00129   inline result_type operator()(argument_type handler)
00130   {
00131     return handler->doSetResultset(session);
00132   }
00133 };
00134 
00135 bool plugin::QueryCache::setResultset(Session *session)
00136 {
00137   /* Use find_if instead of foreach so that we can collect return codes */
00138   QueryCaches::iterator iter=
00139     std::find_if(all_query_cache.begin(), all_query_cache.end(),
00140                  SetResultsetIterate(session));
00141   /* If iter is == end() here, that means that all of the plugins returned
00142    * false, which in this case means they all succeeded. Since we want to 
00143    * return false on success, we return the value of the two being != 
00144    */
00145   return iter != all_query_cache.end();
00146 }
00147 
00148 class InsertRecordIterate
00149  : public std::unary_function<plugin::QueryCache *, bool>
00150 {
00151   Session *session;
00152   List<Item> &item;
00153 public:
00154   InsertRecordIterate(Session *session_arg, List<Item> &item_arg) :
00155     std::unary_function<plugin::QueryCache *, bool>(),
00156     session(session_arg), item(item_arg) { }
00157 
00158   inline result_type operator()(argument_type handler)
00159   {
00160     return handler->doInsertRecord(session, item);
00161   }
00162 };
00163 bool plugin::QueryCache::insertRecord(Session *session, List<Item> &items)
00164 {
00165   /* Use find_if instead of foreach so that we can collect return codes */
00166   QueryCaches::iterator iter=
00167     std::find_if(all_query_cache.begin(), all_query_cache.end(),
00168                  InsertRecordIterate(session, items));
00169   /* If iter is == end() here, that means that all of the plugins returned
00170    * false, which in this case means they all succeeded. Since we want to 
00171    * return false on success, we return the value of the two being != 
00172    */
00173   return iter != all_query_cache.end();
00174 }
00175 
00176 
00177 
00178 bool plugin::QueryCache::addPlugin(plugin::QueryCache *handler)
00179 {
00180   all_query_cache.push_back(handler);
00181   return false;
00182 }
00183 
00184 void plugin::QueryCache::removePlugin(plugin::QueryCache *handler)
00185 {
00186   all_query_cache.erase(std::find(all_query_cache.begin(), all_query_cache.end(),
00187                                   handler));
00188 }
00189 
00190 } /* namespace drizzled */