~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to plugin/memcached_query_cache/query_cache_udf_tools.cc

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2012-06-19 10:46:49 UTC
  • mfrom: (1.1.6)
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20120619104649-e2l0ggd4oz3um0f4
Tags: upstream-7.1.36-stable
ImportĀ upstreamĀ versionĀ 7.1.36-stable

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* 
2
 
 * Copyright (C) 2010 Djellel Eddine Difallah
3
 
 * All rights reserved.
4
 
 *
5
 
 * Redistribution and use in source and binary forms, with or without
6
 
 * modification, are permitted provided that the following conditions are met:
7
 
 *
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.
16
 
 *
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.
28
 
 */
29
 
 
30
 
/**
31
 
 * @file
32
 
 *
33
 
 * Implements the PRINT_QUERY_CACHE_META(key) and QUERY_CACHE_FLUSH(expiry_time) UDFs.
34
 
 */
35
 
 
36
 
#include <config.h>
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>
43
 
 
44
 
#include <fcntl.h>
45
 
 
46
 
#include "query_cache_udf_tools.h"
47
 
#include "query_cache_service.h"
48
 
#include "memcached_qc.h"
49
 
 
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>
55
 
 
56
 
using namespace std;
57
 
using namespace drizzled;
58
 
using namespace google;
59
 
 
60
 
 
61
 
plugin::Create_function<PrintQueryCacheMetaFunction> *print_query_cache_meta_func_factory= NULL;
62
 
 
63
 
void PrintQueryCacheMetaFunction::fix_length_and_dec()
64
 
{
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);
69
 
}
70
 
 
71
 
String *PrintQueryCacheMetaFunction::val_str(String *str)
72
 
{
73
 
  assert(fixed == true);
74
 
 
75
 
  String *key_arg= args[0]->val_str(str);
76
 
 
77
 
  if (key_arg == NULL)
78
 
  {
79
 
    my_error(ER_INVALID_NULL_ARGUMENT, MYF(0), func_name());
80
 
    null_value= true;
81
 
    return NULL;
82
 
  }
83
 
 
84
 
  null_value= false;
85
 
 
86
 
  map<string, message::Resultset>::iterator it= QueryCacheService::cache.find(String_to_std_string(*key_arg));
87
 
 
88
 
  if (it == QueryCacheService::cache.end())
89
 
  {
90
 
    my_error(ER_INVALID_NULL_ARGUMENT, MYF(0), func_name());
91
 
    null_value= true;  
92
 
    return NULL;
93
 
  }
94
 
 
95
 
  message::Resultset resultset_message= it->second;
96
 
 
97
 
  string resultset_text;
98
 
  protobuf::TextFormat::PrintToString(resultset_message, &resultset_text);
99
 
 
100
 
  if (str->alloc(resultset_text.length()))
101
 
  {
102
 
    null_value= true;
103
 
    return NULL;
104
 
  }
105
 
 
106
 
  str->length(resultset_text.length());
107
 
 
108
 
  strncpy(str->ptr(), resultset_text.c_str(), resultset_text.length());
109
 
 
110
 
  return str;
111
 
}
112
 
 
113
 
int64_t QueryCacheFlushFunction::val_int()
114
 
{
115
 
  bool res;
116
 
  time_t expiration= 0;
117
 
  null_value= false;
118
 
 
119
 
  if ((arg_count != 0 && arg_count != 1) ||!MemcachedQueryCache::getClient())
120
 
  {
121
 
    return 0;
122
 
  }
123
 
 
124
 
  if (arg_count == 1)
125
 
  {
126
 
    String *tmp_exp= args[0]->val_str(&value);
127
 
 
128
 
    expiration= (time_t)atoi(tmp_exp->c_ptr());
129
 
  }
130
 
 
131
 
  res= MemcachedQueryCache::getClient()->flush(expiration);
132
 
  QueryCacheService::cache.clear();
133
 
  QueryCacheService::cachedTables.clear();
134
 
 
135
 
  if (not res)
136
 
  {
137
 
    return 0;
138
 
  }
139
 
 
140
 
  return 1;
141
 
}