~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/memcached_query_cache/query_cache_service.cc

  • Committer: Andrew Hutchings
  • Date: 2010-09-08 19:03:09 UTC
  • mfrom: (1750 staging)
  • mto: (1750.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 1751.
  • Revision ID: andrew@linuxjedi.co.uk-20100908190309-mya1nu7xvo1fpvk8
Merge trunk into branch

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
#include "config.h"
 
31
#include "drizzled/session.h"
 
32
#include "query_cache_service.h"
 
33
#include "drizzled/table_list.h"
 
34
 
 
35
using namespace std;
 
36
 
 
37
namespace drizzled
 
38
{
 
39
 
 
40
QueryCacheService::CacheEntries QueryCacheService::cache;
 
41
QueryCacheService::CachedTablesEntries QueryCacheService::cachedTables;
 
42
 
 
43
message::Resultset *QueryCacheService::setCurrentResultsetMessage(Session *in_session)
 
44
{
 
45
  message::Resultset *resultset= in_session->getResultsetMessage();
 
46
 
 
47
  if (unlikely(resultset == NULL))
 
48
  {
 
49
    /* Allocate and initialize a new Resultset message 
 
50
     * for this Session object.  Session is responsible for
 
51
     * deleting Resultset message when done with it.
 
52
     */
 
53
    resultset= new (nothrow) message::Resultset();
 
54
    in_session->setResultsetMessage(resultset);
 
55
 
 
56
    /* for Atomic purpose, reserve an entry for the following query
 
57
     */
 
58
    cache[in_session->query_cache_key]= message::Resultset();
 
59
  }
 
60
  return resultset;
 
61
}
 
62
 
 
63
void QueryCacheService::setResultsetHeader(message::Resultset &resultset,
 
64
                                          Session *in_session,
 
65
                                          TableList *in_table)
 
66
{
 
67
  /* Set up the Select header */
 
68
  message::SelectHeader *header= resultset.mutable_select_header();
 
69
  
 
70
  (void) in_session;
 
71
 
 
72
  /* Extract all the tables mentioned in the query and 
 
73
   * add the metadata to the SelectHeader
 
74
   */
 
75
 
 
76
  for (TableList* tmp_table= in_table; tmp_table; tmp_table= tmp_table->next_global)
 
77
  {
 
78
    message::TableMeta *table_meta= header->add_table_meta();
 
79
    table_meta->set_schema_name(tmp_table->db, strlen(tmp_table->db));
 
80
    table_meta->set_table_name(tmp_table->table_name, strlen(tmp_table->table_name));
 
81
    /* Populate the cached tables hash */
 
82
    QueryCacheService::cachedTables[strcat(tmp_table->db, tmp_table->table_name)].push_back(in_session->query_cache_key);
 
83
  } 
 
84
 
 
85
  /* Extract the returned fields 
 
86
   * and add the field data to the SelectHeader
 
87
   */
 
88
  List_iterator_fast<Item> it(in_session->lex->select_lex.item_list);
 
89
  Item *item;
 
90
  while ((item=it++))
 
91
  {
 
92
    SendField field;
 
93
    item->make_field(&field);
 
94
    
 
95
    message::FieldMeta *field_meta= header->add_field_meta();
 
96
    field_meta->set_field_name(field.col_name, strlen(field.col_name));    
 
97
    field_meta->set_schema_name(field.db_name, strlen(field.db_name));
 
98
    field_meta->set_table_name(field.table_name, strlen(field.table_name));
 
99
    field_meta->set_field_alias(field.org_col_name, strlen(field.org_col_name));
 
100
    field_meta->set_table_alias(field.org_table_name, strlen(field.org_table_name));
 
101
   }
 
102
}
 
103
 
 
104
bool QueryCacheService::addRecord(Session *in_session, List<Item> &list)
 
105
{
 
106
  message::Resultset *resultset= in_session->getResultsetMessage();
 
107
  
 
108
  if (resultset != NULL)
 
109
  {
 
110
    message::SelectData *data= resultset->mutable_select_data();
 
111
    data->set_segment_id(1);
 
112
    data->set_end_segment(true);
 
113
    message::SelectRecord *record= data->add_record();
 
114
 
 
115
    List_iterator_fast<Item> li(list);
 
116
    
 
117
    char buff[MAX_FIELD_WIDTH];
 
118
    String buffer(buff, sizeof(buff), &my_charset_bin);
 
119
    
 
120
    Item *current_field;
 
121
    while ((current_field= li++))
 
122
    {
 
123
      if (current_field->is_null())
 
124
      {
 
125
        record->add_is_null(true);
 
126
        record->add_record_value("", 0);
 
127
      } 
 
128
      else 
 
129
      {
 
130
        String *string_value= current_field->val_str(&buffer);
 
131
        record->add_is_null(false);
 
132
        record->add_record_value(string_value->c_ptr(), string_value->length());
 
133
        string_value->free();
 
134
      }
 
135
    }
 
136
    return false;
 
137
  }
 
138
  return true;
 
139
}
 
140
 
 
141
bool QueryCacheService::isCached(string query)
 
142
{
 
143
  CacheEntries::iterator it= QueryCacheService::cache.find(query);
 
144
  if (it != QueryCacheService::cache.end())
 
145
     return true;
 
146
  return false;
 
147
}
 
148
} /* namespace drizzled */