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 namespace drizzled {
00031 
00032 typedef std::vector<plugin::QueryCache *> QueryCaches;
00033 QueryCaches all_query_cache;
00034 
00035 /* Namespaces are here to prevent global symbol clashes with these classes */
00036 
00037 class IsCachedIterate
00038  : public std::unary_function<plugin::QueryCache *, bool>
00039 {
00040   Session *session;
00041 public:
00042   IsCachedIterate(Session* session_arg) :
00043     std::unary_function<plugin::QueryCache *, bool>(),
00044     session(session_arg) { }
00045 
00046   inline result_type operator()(argument_type handler)
00047   {
00048     return handler->doIsCached(session);
00049   }
00050 };
00051 
00052 bool plugin::QueryCache::isCached(Session *session)
00053 {
00054   /* Use find_if instead of foreach so that we can collect return codes */
00055   QueryCaches::iterator iter=
00056     std::find_if(all_query_cache.begin(), all_query_cache.end(),
00057             IsCachedIterate(session));
00058   /* If iter is == end() here, that means that all of the plugins returned
00059    * false, which in this case means they all succeeded. Since we want to 
00060    * return false on success, we return the value of the two being != 
00061    */
00062   return iter != all_query_cache.end();
00063 }
00064 
00065 
00066 class SendCachedResultsetIterate
00067  : public std::unary_function<plugin::QueryCache *, bool>
00068 {
00069   Session *session;
00070 public:
00071   SendCachedResultsetIterate(Session *session_arg) :
00072     std::unary_function<plugin::QueryCache *, bool>(),
00073     session(session_arg) { }
00074 
00075   inline result_type operator()(argument_type handler)
00076   {
00077     return handler->doSendCachedResultset(session);
00078   }
00079 };
00080 bool plugin::QueryCache::sendCachedResultset(Session *session)
00081 {
00082   /* Use find_if instead of foreach so that we can collect return codes */
00083   QueryCaches::iterator iter=
00084     std::find_if(all_query_cache.begin(), all_query_cache.end(),
00085                  SendCachedResultsetIterate(session));
00086   /* If iter is == end() here, that means that all of the plugins returned
00087    * false, which in this case means they all succeeded. Since we want to 
00088    * return false on success, we return the value of the two being != 
00089    */
00090   return iter != all_query_cache.end();
00091 }
00092 
00093 class PrepareResultsetIterate : public std::unary_function<plugin::QueryCache *, bool>
00094 {
00095   Session *session;
00096 public:
00097   PrepareResultsetIterate(Session *session_arg) :
00098     std::unary_function<plugin::QueryCache *, bool>(),
00099     session(session_arg) { }
00100 
00101   inline result_type operator()(argument_type handler)
00102   {
00103     return handler->doPrepareResultset(session);
00104   }
00105 };
00106 bool plugin::QueryCache::prepareResultset(Session *session)
00107 {
00108   /* Use find_if instead of foreach so that we can collect return codes */
00109   QueryCaches::iterator iter=
00110     std::find_if(all_query_cache.begin(), all_query_cache.end(),
00111                  PrepareResultsetIterate(session));
00112   /* If iter is == end() here, that means that all of the plugins returned
00113    * false, which in this case means they all succeeded. Since we want to 
00114    * return false on success, we return the value of the two being != 
00115    */
00116   return iter != all_query_cache.end();
00117 }
00118 
00119 class SetResultsetIterate : public std::unary_function<plugin::QueryCache *, bool>
00120 {
00121   Session *session;
00122 public:
00123   SetResultsetIterate(Session *session_arg) :
00124     std::unary_function<plugin::QueryCache *, bool>(),
00125     session(session_arg) { }
00126 
00127   inline result_type operator()(argument_type handler)
00128   {
00129     return handler->doSetResultset(session);
00130   }
00131 };
00132 
00133 bool plugin::QueryCache::setResultset(Session *session)
00134 {
00135   /* Use find_if instead of foreach so that we can collect return codes */
00136   QueryCaches::iterator iter=
00137     std::find_if(all_query_cache.begin(), all_query_cache.end(),
00138                  SetResultsetIterate(session));
00139   /* If iter is == end() here, that means that all of the plugins returned
00140    * false, which in this case means they all succeeded. Since we want to 
00141    * return false on success, we return the value of the two being != 
00142    */
00143   return iter != all_query_cache.end();
00144 }
00145 
00146 class InsertRecordIterate
00147  : public std::unary_function<plugin::QueryCache *, bool>
00148 {
00149   Session *session;
00150   List<Item> &item;
00151 public:
00152   InsertRecordIterate(Session *session_arg, List<Item> &item_arg) :
00153     std::unary_function<plugin::QueryCache *, bool>(),
00154     session(session_arg), item(item_arg) { }
00155 
00156   inline result_type operator()(argument_type handler)
00157   {
00158     return handler->doInsertRecord(session, item);
00159   }
00160 };
00161 bool plugin::QueryCache::insertRecord(Session *session, List<Item> &items)
00162 {
00163   /* Use find_if instead of foreach so that we can collect return codes */
00164   QueryCaches::iterator iter=
00165     std::find_if(all_query_cache.begin(), all_query_cache.end(),
00166                  InsertRecordIterate(session, items));
00167   /* If iter is == end() here, that means that all of the plugins returned
00168    * false, which in this case means they all succeeded. Since we want to 
00169    * return false on success, we return the value of the two being != 
00170    */
00171   return iter != all_query_cache.end();
00172 }
00173 
00174 
00175 
00176 bool plugin::QueryCache::addPlugin(plugin::QueryCache *handler)
00177 {
00178   all_query_cache.push_back(handler);
00179   return false;
00180 }
00181 
00182 void plugin::QueryCache::removePlugin(plugin::QueryCache *handler)
00183 {
00184   all_query_cache.erase(std::find(all_query_cache.begin(), all_query_cache.end(),
00185                                   handler));
00186 }
00187 
00188 } /* namespace drizzled */