~drizzle-trunk/drizzle/development

1643.6.2 by Djellel E. Difallah
Added the Query Cache Plugin for Memcached
1
/* 
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
2
 * Copyright (C) 2010 Djellel Eddine Difallah
1643.6.2 by Djellel E. Difallah
Added the Query Cache Plugin for Memcached
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.
1643.6.11 by Djellel E. Difallah
trunk merge
13
 *   * Neither the name of Djellel Eddine Difallah nor the names of its contributors
1643.6.2 by Djellel E. Difallah
Added the Query Cache Plugin for Memcached
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
1643.6.3 by Djellel E. Difallah
Refactoring of the QC Plugin's interface methods, adding mutual exclusion mechanism between sessions to cache a query, Store Meta inoformation of all the tables and select fields of the query
37
namespace drizzled
38
{
39
1643.6.14 by Djellel E. Difallah
Added the Table Based Invalidation and its test suite
40
QueryCacheService::CacheEntries QueryCacheService::cache;
41
QueryCacheService::CachedTablesEntries QueryCacheService::cachedTables;
1643.6.3 by Djellel E. Difallah
Refactoring of the QC Plugin's interface methods, adding mutual exclusion mechanism between sessions to cache a query, Store Meta inoformation of all the tables and select fields of the query
42
1643.6.16 by Djellel E. Difallah
Fixing memory leaks
43
message::Resultset *QueryCacheService::setCurrentResultsetMessage(Session *in_session)
1643.6.2 by Djellel E. Difallah
Added the Query Cache Plugin for Memcached
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
1643.6.3 by Djellel E. Difallah
Refactoring of the QC Plugin's interface methods, adding mutual exclusion mechanism between sessions to cache a query, Store Meta inoformation of all the tables and select fields of the query
56
    /* for Atomic purpose, reserve an entry for the following query
1643.6.2 by Djellel E. Difallah
Added the Query Cache Plugin for Memcached
57
     */
1643.6.3 by Djellel E. Difallah
Refactoring of the QC Plugin's interface methods, adding mutual exclusion mechanism between sessions to cache a query, Store Meta inoformation of all the tables and select fields of the query
58
    cache[in_session->query_cache_key]= message::Resultset();
1643.6.2 by Djellel E. Difallah
Added the Query Cache Plugin for Memcached
59
  }
1643.6.16 by Djellel E. Difallah
Fixing memory leaks
60
  return resultset;
1643.6.2 by Djellel E. Difallah
Added the Query Cache Plugin for Memcached
61
}
1643.6.14 by Djellel E. Difallah
Added the Table Based Invalidation and its test suite
62
1643.6.2 by Djellel E. Difallah
Added the Query Cache Plugin for Memcached
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();
1643.6.3 by Djellel E. Difallah
Refactoring of the QC Plugin's interface methods, adding mutual exclusion mechanism between sessions to cache a query, Store Meta inoformation of all the tables and select fields of the query
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));
1643.6.14 by Djellel E. Difallah
Added the Table Based Invalidation and its test suite
81
    /* Populate the cached tables hash */
82
    QueryCacheService::cachedTables[strcat(tmp_table->db, tmp_table->table_name)].push_back(in_session->query_cache_key);
1643.6.3 by Djellel E. Difallah
Refactoring of the QC Plugin's interface methods, adding mutual exclusion mechanism between sessions to cache a query, Store Meta inoformation of all the tables and select fields of the query
83
  } 
84
85
  /* Extract the returned fields 
1643.6.11 by Djellel E. Difallah
trunk merge
86
   * and add the field data to the SelectHeader
1643.6.3 by Djellel E. Difallah
Refactoring of the QC Plugin's interface methods, adding mutual exclusion mechanism between sessions to cache a query, Store Meta inoformation of all the tables and select fields of the query
87
   */
88
  List_iterator_fast<Item> it(in_session->lex->select_lex.item_list);
1643.6.2 by Djellel E. Difallah
Added the Query Cache Plugin for Memcached
89
  Item *item;
1643.6.3 by Djellel E. Difallah
Refactoring of the QC Plugin's interface methods, adding mutual exclusion mechanism between sessions to cache a query, Store Meta inoformation of all the tables and select fields of the query
90
  while ((item=it++))
1643.6.2 by Djellel E. Difallah
Added the Query Cache Plugin for Memcached
91
  {
1643.6.3 by Djellel E. Difallah
Refactoring of the QC Plugin's interface methods, adding mutual exclusion mechanism between sessions to cache a query, Store Meta inoformation of all the tables and select fields of the query
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
   }
1643.6.2 by Djellel E. Difallah
Added the Query Cache Plugin for Memcached
102
}
103
104
bool QueryCacheService::addRecord(Session *in_session, List<Item> &list)
105
{
1643.6.16 by Djellel E. Difallah
Fixing memory leaks
106
  message::Resultset *resultset= in_session->getResultsetMessage();
1643.6.3 by Djellel E. Difallah
Refactoring of the QC Plugin's interface methods, adding mutual exclusion mechanism between sessions to cache a query, Store Meta inoformation of all the tables and select fields of the query
107
  
1643.6.16 by Djellel E. Difallah
Fixing memory leaks
108
  if (resultset != NULL)
1643.6.2 by Djellel E. Difallah
Added the Query Cache Plugin for Memcached
109
  {
1643.6.16 by Djellel E. Difallah
Fixing memory leaks
110
    message::SelectData *data= resultset->mutable_select_data();
1643.6.3 by Djellel E. Difallah
Refactoring of the QC Plugin's interface methods, adding mutual exclusion mechanism between sessions to cache a query, Store Meta inoformation of all the tables and select fields of the query
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);
1643.6.16 by Djellel E. Difallah
Fixing memory leaks
116
    
117
    char buff[MAX_FIELD_WIDTH];
118
    String buffer(buff, sizeof(buff), &my_charset_bin);
119
    
1643.6.14 by Djellel E. Difallah
Added the Table Based Invalidation and its test suite
120
    Item *current_field;
121
    while ((current_field= li++))
122
    {
1643.6.16 by Djellel E. Difallah
Fixing memory leaks
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
      }
1643.6.3 by Djellel E. Difallah
Refactoring of the QC Plugin's interface methods, adding mutual exclusion mechanism between sessions to cache a query, Store Meta inoformation of all the tables and select fields of the query
135
    }
136
    return false;
1643.6.2 by Djellel E. Difallah
Added the Query Cache Plugin for Memcached
137
  }
1643.6.3 by Djellel E. Difallah
Refactoring of the QC Plugin's interface methods, adding mutual exclusion mechanism between sessions to cache a query, Store Meta inoformation of all the tables and select fields of the query
138
  return true;
1643.6.2 by Djellel E. Difallah
Added the Query Cache Plugin for Memcached
139
}
140
141
bool QueryCacheService::isCached(string query)
142
{
1643.6.14 by Djellel E. Difallah
Added the Table Based Invalidation and its test suite
143
  CacheEntries::iterator it= QueryCacheService::cache.find(query);
1643.6.2 by Djellel E. Difallah
Added the Query Cache Plugin for Memcached
144
  if (it != QueryCacheService::cache.end())
145
     return true;
146
  return false;
147
}
1643.6.3 by Djellel E. Difallah
Refactoring of the QC Plugin's interface methods, adding mutual exclusion mechanism between sessions to cache a query, Store Meta inoformation of all the tables and select fields of the query
148
} /* namespace drizzled */