2
* Copyright (C) 2010 Djellel Eddine Difallah
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* * Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* * Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
* * Neither the name of Djellel Eddine Difallah nor the names of its contributors
14
* may be used to endorse or promote products derived from this software
15
* without specific prior written permission.
17
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27
* THE POSSIBILITY OF SUCH DAMAGE.
33
* Implements the PRINT_QUERY_CACHE_META(key) and QUERY_CACHE_FLUSH(expiry_time) UDFs.
37
#include <drizzled/plugin/function.h>
38
#include <drizzled/item/func.h>
39
#include <drizzled/function/str/strfunc.h>
40
#include <drizzled/error.h>
41
#include <drizzled/internal/my_sys.h>
42
#include <drizzled/charset.h>
46
#include "query_cache_udf_tools.h"
47
#include "query_cache_service.h"
48
#include "memcached_qc.h"
50
#include <drizzled/message/resultset.pb.h>
51
#include <google/protobuf/io/zero_copy_stream.h>
52
#include <google/protobuf/io/zero_copy_stream_impl.h>
53
#include <google/protobuf/io/coded_stream.h>
54
#include <google/protobuf/text_format.h>
57
using namespace drizzled;
58
using namespace google;
61
plugin::Create_function<PrintQueryCacheMetaFunction> *print_query_cache_meta_func_factory= NULL;
63
void PrintQueryCacheMetaFunction::fix_length_and_dec()
65
max_length= 2 * 1024 * 1024; /* 2MB size limit seems ok... */
66
args[0]->collation.set(
67
get_charset_by_csname(args[0]->collation.collation->csname,
68
MY_CS_BINSORT), DERIVATION_COERCIBLE);
71
String *PrintQueryCacheMetaFunction::val_str(String *str)
73
assert(fixed == true);
75
String *key_arg= args[0]->val_str(str);
79
my_error(ER_INVALID_NULL_ARGUMENT, MYF(0), func_name());
86
map<string, message::Resultset>::iterator it= QueryCacheService::cache.find(String_to_std_string(*key_arg));
88
if (it == QueryCacheService::cache.end())
90
my_error(ER_INVALID_NULL_ARGUMENT, MYF(0), func_name());
95
message::Resultset resultset_message= it->second;
97
string resultset_text;
98
protobuf::TextFormat::PrintToString(resultset_message, &resultset_text);
100
if (str->alloc(resultset_text.length()))
106
str->length(resultset_text.length());
108
strncpy(str->ptr(), resultset_text.c_str(), resultset_text.length());
113
int64_t QueryCacheFlushFunction::val_int()
116
time_t expiration= 0;
119
if ((arg_count != 0 && arg_count != 1) ||!MemcachedQueryCache::getClient())
126
String *tmp_exp= args[0]->val_str(&value);
128
expiration= (time_t)atoi(tmp_exp->c_ptr());
131
res= MemcachedQueryCache::getClient()->flush(expiration);
132
QueryCacheService::cache.clear();
133
QueryCacheService::cachedTables.clear();