Drizzled Public API Documentation

query_cache_udf_tools.cc

Go to the documentation of this file.
00001 /* 
00002  * Copyright (C) 2010 Djellel Eddine Difallah
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *   * Redistributions of source code must retain the above copyright notice,
00009  *     this list of conditions and the following disclaimer.
00010  *   * Redistributions in binary form must reproduce the above copyright notice,
00011  *     this list of conditions and the following disclaimer in the documentation
00012  *     and/or other materials provided with the distribution.
00013  *   * Neither the name of Djellel Eddine Difallah nor the names of its contributors
00014  *     may be used to endorse or promote products derived from this software
00015  *     without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
00027  * THE POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00036 #include <config.h>
00037 #include <drizzled/plugin/function.h>
00038 #include <drizzled/item/func.h>
00039 #include <drizzled/function/str/strfunc.h>
00040 #include <drizzled/error.h>
00041 #include <drizzled/internal/my_sys.h>
00042 #include <drizzled/charset.h>
00043 
00044 #include <fcntl.h>
00045 
00046 #include "query_cache_udf_tools.h"
00047 #include "query_cache_service.h"
00048 #include "memcached_qc.h"
00049 
00050 #include <drizzled/message/resultset.pb.h>
00051 #include <google/protobuf/io/zero_copy_stream.h>
00052 #include <google/protobuf/io/zero_copy_stream_impl.h>
00053 #include <google/protobuf/io/coded_stream.h>
00054 #include <google/protobuf/text_format.h>
00055 
00056 using namespace std;
00057 using namespace drizzled;
00058 using namespace google;
00059 
00060 
00061 plugin::Create_function<PrintQueryCacheMetaFunction> *print_query_cache_meta_func_factory= NULL;
00062 
00063 void PrintQueryCacheMetaFunction::fix_length_and_dec()
00064 {
00065   max_length= 2 * 1024 * 1024; /* 2MB size limit seems ok... */
00066   args[0]->collation.set(
00067     get_charset_by_csname(args[0]->collation.collation->csname,
00068                           MY_CS_BINSORT), DERIVATION_COERCIBLE);
00069 }
00070 
00071 String *PrintQueryCacheMetaFunction::val_str(String *str)
00072 {
00073   assert(fixed == true);
00074 
00075   String *key_arg= args[0]->val_str(str);
00076 
00077   if (key_arg == NULL)
00078   {
00079     my_error(ER_INVALID_NULL_ARGUMENT, MYF(0), func_name());
00080     null_value= true;
00081     return NULL;
00082   }
00083 
00084   null_value= false;
00085 
00086   map<string, message::Resultset>::iterator it= QueryCacheService::cache.find(String_to_std_string(*key_arg));
00087 
00088   if (it == QueryCacheService::cache.end())
00089   {
00090     my_error(ER_INVALID_NULL_ARGUMENT, MYF(0), func_name());
00091     null_value= true;  
00092     return NULL;
00093   }
00094 
00095   message::Resultset resultset_message= it->second;
00096 
00097   string resultset_text;
00098   protobuf::TextFormat::PrintToString(resultset_message, &resultset_text);
00099 
00100   if (str->alloc(resultset_text.length()))
00101   {
00102     null_value= true;
00103     return NULL;
00104   }
00105 
00106   str->length(resultset_text.length());
00107 
00108   strncpy(str->ptr(), resultset_text.c_str(), resultset_text.length());
00109 
00110   return str;
00111 }
00112 
00113 int64_t QueryCacheFlushFunction::val_int()
00114 {
00115   bool res;
00116   time_t expiration= 0;
00117   null_value= false;
00118 
00119   if ((arg_count != 0 && arg_count != 1) ||!MemcachedQueryCache::getClient())
00120   {
00121     return 0;
00122   }
00123 
00124   if (arg_count == 1)
00125   {
00126     String *tmp_exp= args[0]->val_str(&value);
00127 
00128     expiration= (time_t)atoi(tmp_exp->c_ptr());
00129   }
00130 
00131   res= MemcachedQueryCache::getClient()->flush(expiration);
00132   QueryCacheService::cache.clear();
00133   QueryCacheService::cachedTables.clear();
00134 
00135   if (not res)
00136   {
00137     return 0;
00138   }
00139 
00140   return 1;
00141 }