~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/memcached_stats/memcached_stats.cc

  • Committer: Monty Taylor
  • Date: 2009-05-24 23:54:21 UTC
  • mfrom: (1014.2.12 kill-malloc)
  • mto: This revision was merged to the branch mainline in revision 1039.
  • Revision ID: mordred@inaugust.com-20090524235421-x5vfss90auzbn896
Merged Monty from lp:~mordred/drizzle/kill-malloc

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* 
2
 
 * Copyright (c) 2009, Padraig O'Sullivan
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 Padraig O'Sullivan 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 "drizzled/server_includes.h"
31
 
#include "drizzled/show.h"
32
 
#include "drizzled/gettext.h"
33
 
#include "drizzled/plugin/info_schema_table.h"
34
 
 
35
 
#include "stats_table.h"
36
 
#include "analysis_table.h"
37
 
#include "sysvar_holder.h"
38
 
 
39
 
#include <string>
40
 
#include <map>
41
 
 
42
 
using namespace std;
43
 
using namespace drizzled;
44
 
 
45
 
/*
46
 
 * Vectors of columns for I_S tables.
47
 
 */
48
 
static vector<const plugin::ColumnInfo *> memcached_stats_columns;
49
 
static vector<const plugin::ColumnInfo *> memcached_analysis_columns;
50
 
 
51
 
/*
52
 
 * Methods for I_S tables.
53
 
 */
54
 
static plugin::InfoSchemaMethods *memcached_stats_methods= NULL;
55
 
static plugin::InfoSchemaMethods *memcached_analysis_methods= NULL;
56
 
 
57
 
/*
58
 
 * I_S tables.
59
 
 */
60
 
static plugin::InfoSchemaTable *memcached_stats_table= NULL;
61
 
static plugin::InfoSchemaTable *memcached_analysis_table= NULL;
62
 
 
63
 
/*
64
 
 * System variable related variables.
65
 
 */
66
 
static char *sysvar_memcached_servers= NULL;
67
 
 
68
 
/**
69
 
 * Populate the vectors of columns for each I_S table.
70
 
 *
71
 
 * @return false on success; true on failure.
72
 
 */
73
 
static bool initColumns()
74
 
{
75
 
  if (createMemcachedStatsColumns(memcached_stats_columns))
76
 
  {
77
 
    return true;
78
 
  }
79
 
 
80
 
  if (createMemcachedAnalysisColumns(memcached_analysis_columns))
81
 
  {
82
 
    return true;
83
 
  }
84
 
 
85
 
  return false;
86
 
}
87
 
 
88
 
/**
89
 
 * Clear the vectors of columns for each I_S table.
90
 
 */
91
 
static void cleanupColumns()
92
 
{
93
 
  clearMemcachedColumns(memcached_stats_columns);
94
 
  clearMemcachedColumns(memcached_analysis_columns);
95
 
}
96
 
 
97
 
/**
98
 
 * Initialize the methods for each I_S table.
99
 
 *
100
 
 * @return false on success; true on failure
101
 
 */
102
 
static bool initMethods()
103
 
{
104
 
  memcached_stats_methods= new(std::nothrow) 
105
 
    MemcachedStatsISMethods();
106
 
  if (! memcached_stats_methods)
107
 
  {
108
 
    return true;
109
 
  }
110
 
 
111
 
  memcached_analysis_methods= new(std::nothrow) 
112
 
    MemcachedAnalysisISMethods();
113
 
  if (! memcached_analysis_methods)
114
 
  {
115
 
    return true;
116
 
  }
117
 
 
118
 
  return false;
119
 
}
120
 
 
121
 
/**
122
 
 * Delete memory allocated for the I_S table methods.
123
 
 */
124
 
static void cleanupMethods()
125
 
{
126
 
  delete memcached_stats_methods;
127
 
  delete memcached_analysis_methods;
128
 
}
129
 
 
130
 
/**
131
 
 * Initialize the I_S tables related to memcached.
132
 
 *
133
 
 * @return false on success; true on failure
134
 
 */
135
 
static bool initMemcachedTables()
136
 
{
137
 
  memcached_stats_table= new(std::nothrow) plugin::InfoSchemaTable("MEMCACHED_STATS",
138
 
                                                           memcached_stats_columns,
139
 
                                                           -1, -1, false, false, 0,
140
 
                                                           memcached_stats_methods);
141
 
  if (! memcached_stats_table)
142
 
  {
143
 
    return true;
144
 
  }
145
 
 
146
 
  memcached_analysis_table= 
147
 
    new(std::nothrow) plugin::InfoSchemaTable("MEMCACHED_ANALYSIS",
148
 
                                      memcached_analysis_columns,
149
 
                                      -1, -1, false, false, 0,
150
 
                                      memcached_analysis_methods);
151
 
  if (! memcached_analysis_table)
152
 
  {
153
 
    return true;
154
 
  }
155
 
 
156
 
  return false;
157
 
}
158
 
 
159
 
/**
160
 
 * Delete memory allocated for the I_S tables.
161
 
 */
162
 
static void cleanupMemcachedTables()
163
 
{
164
 
  delete memcached_stats_table;
165
 
  delete memcached_analysis_table;
166
 
}
167
 
 
168
 
/**
169
 
 * Initialize the memcached stats plugin.
170
 
 *
171
 
 * @param[in] registry the drizzled::plugin::Registry singleton
172
 
 * @return false on success; true on failure.
173
 
 */
174
 
static int init(plugin::Registry &registry)
175
 
{
176
 
  if (initMethods())
177
 
  {
178
 
    return 1;
179
 
  }
180
 
 
181
 
  if (initColumns())
182
 
  {
183
 
    return 1;
184
 
  }
185
 
 
186
 
  if (initMemcachedTables())
187
 
  {
188
 
    return 1;
189
 
  }
190
 
 
191
 
  SysvarHolder &sysvar_holder= SysvarHolder::singleton();
192
 
  sysvar_holder.setServersString(sysvar_memcached_servers);
193
 
 
194
 
  /* we are good to go */
195
 
  registry.add(memcached_stats_table);
196
 
  registry.add(memcached_analysis_table);
197
 
 
198
 
  return 0;
199
 
}
200
 
 
201
 
/**
202
 
 * Clean up the memcached stats plugin.
203
 
 *
204
 
 * @param[in] registry the drizzled::plugin::Registry singleton
205
 
 * @return false on success; true on failure
206
 
 */
207
 
static int deinit(plugin::Registry &registry)
208
 
{
209
 
  registry.remove(memcached_stats_table);
210
 
  registry.remove(memcached_analysis_table);
211
 
 
212
 
  cleanupMethods();
213
 
  cleanupColumns();
214
 
  cleanupMemcachedTables();
215
 
 
216
 
  return 0;
217
 
}
218
 
 
219
 
static int check_memc_servers(Session *,
220
 
                              struct st_mysql_sys_var *,
221
 
                              void *save,
222
 
                              struct st_mysql_value *value)
223
 
{
224
 
  char buff[STRING_BUFFER_USUAL_SIZE];
225
 
  int len= sizeof(buff);
226
 
  const char *input= value->val_str(value, buff, &len);
227
 
 
228
 
  if (input)
229
 
  {
230
 
    SysvarHolder &sysvar_holder= SysvarHolder::singleton();
231
 
    sysvar_holder.setServersStringVar(input);
232
 
    *(bool *) save= (bool) true;
233
 
    return 0;
234
 
  }
235
 
 
236
 
  *(bool *) save= (bool) false;
237
 
  return 1;
238
 
}
239
 
 
240
 
static void set_memc_servers(Session *,
241
 
                             struct st_mysql_sys_var *,
242
 
                             void *var_ptr,
243
 
                             const void *save)
244
 
{
245
 
  if (*(bool *) save != false)
246
 
  {
247
 
    SysvarHolder &sysvar_holder= SysvarHolder::singleton();
248
 
    sysvar_holder.updateServersSysvar((const char **) var_ptr);
249
 
  }
250
 
}
251
 
 
252
 
static DRIZZLE_SYSVAR_STR(servers,
253
 
                          sysvar_memcached_servers,
254
 
                          PLUGIN_VAR_OPCMDARG,
255
 
                          N_("List of memcached servers."),
256
 
                          check_memc_servers, /* check func */
257
 
                          set_memc_servers, /* update func */
258
 
                          ""); /* default value */
259
 
 
260
 
static struct st_mysql_sys_var *system_variables[]=
261
 
{
262
 
  DRIZZLE_SYSVAR(servers),
263
 
  NULL
264
 
};
265
 
 
266
 
drizzle_declare_plugin(memcached_stats)
267
 
{
268
 
  "memcached_stats",
269
 
  "1.0",
270
 
  "Padraig O'Sullivan",
271
 
  N_("Memcached Stats as I_S tables"),
272
 
  PLUGIN_LICENSE_BSD,
273
 
  init,   /* Plugin Init      */
274
 
  deinit, /* Plugin Deinit    */
275
 
  NULL,   /* status variables */
276
 
  system_variables, /* system variables */
277
 
  NULL    /* config options   */
278
 
}
279
 
drizzle_declare_plugin_end;