~posulliv/drizzle/memcached_applier

« back to all changes in this revision

Viewing changes to drizzled/sql_plugin.cc

  • Committer: Jay Pipes
  • Date: 2009-08-03 14:23:22 UTC
  • mfrom: (1039.2.68 staging)
  • mto: This revision was merged to the branch mainline in revision 1078.
  • Revision ID: jpipes@serialcoder-20090803142322-1g67h7su9mocg9ig
Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
 
45
45
using namespace std;
46
46
using namespace drizzled;
47
 
using namespace drizzled::plugin;
48
47
 
49
 
typedef Manifest builtin_plugin[];
 
48
typedef plugin::Manifest builtin_plugin[];
50
49
extern builtin_plugin DRIZZLED_BUILTIN_LIST;
51
 
static Manifest *drizzled_builtins[]=
 
50
static plugin::Manifest *drizzled_builtins[]=
52
51
{
53
 
  DRIZZLED_BUILTIN_LIST,(Manifest *)0
 
52
  DRIZZLED_BUILTIN_LIST,(plugin::Manifest *)NULL
54
53
};
 
54
class sys_var_pluginvar;
 
55
static vector<sys_var_pluginvar *> plugin_sysvar_vec;
55
56
 
56
57
char *opt_plugin_load= NULL;
57
58
const char *opt_plugin_load_default= QUOTE_ARG(DRIZZLED_PLUGIN_LIST);
124
125
class sys_var_pluginvar: public sys_var
125
126
{
126
127
public:
127
 
  Handle *plugin;
 
128
  plugin::Handle *plugin;
128
129
  struct st_mysql_sys_var *plugin_var;
129
130
 
130
 
  static void *operator new(size_t size, MEM_ROOT *mem_root)
131
 
  { return (void*) alloc_root(mem_root, (uint32_t) size); }
132
 
  static void operator delete(void *, size_t)
133
 
  { TRASH(ptr_arg, size); }
134
 
 
135
 
  sys_var_pluginvar(const char *name_arg,
 
131
  sys_var_pluginvar(const std::string name_arg,
136
132
                    struct st_mysql_sys_var *plugin_var_arg)
137
133
    :sys_var(name_arg), plugin_var(plugin_var_arg) {}
138
134
  sys_var_pluginvar *cast_pluginvar() { return this; }
156
152
/* prototypes */
157
153
static bool plugin_load_list(MEM_ROOT *tmp_root, int *argc, char **argv,
158
154
                             const char *list);
159
 
static int test_plugin_options(MEM_ROOT *, Handle *,
 
155
static int test_plugin_options(MEM_ROOT *, plugin::Handle *,
160
156
                               int *, char **);
161
 
static bool register_builtin(Handle *,
162
 
                             Handle **);
 
157
static bool register_builtin(plugin::Handle *,
 
158
                             plugin::Handle **);
163
159
static void unlock_variables(Session *session, struct system_variables *vars);
164
160
static void cleanup_variables(Session *session, struct system_variables *vars);
165
161
static void plugin_vars_free_values(sys_var *vars);
238
234
  Plugin support code
239
235
****************************************************************************/
240
236
 
241
 
static Library *plugin_dl_find(const LEX_STRING *dl)
 
237
static plugin::Library *plugin_dl_find(const LEX_STRING *dl)
242
238
{
243
239
  uint32_t i;
244
 
  Library *tmp;
 
240
  plugin::Library *tmp;
245
241
 
246
242
  for (i= 0; i < plugin_dl_array.elements; i++)
247
243
  {
248
 
    tmp= *dynamic_element(&plugin_dl_array, i, Library **);
 
244
    tmp= *dynamic_element(&plugin_dl_array, i, plugin::Library **);
249
245
    if (! my_strnncoll(files_charset_info,
250
246
                       (const unsigned char *)dl->str, dl->length,
251
247
                       (const unsigned char *)tmp->dl.str, tmp->dl.length))
254
250
  return(0);
255
251
}
256
252
 
257
 
static Library *plugin_dl_insert_or_reuse(Library *plugin_dl)
 
253
static plugin::Library *plugin_dl_insert_or_reuse(plugin::Library *plugin_dl)
258
254
{
259
255
  uint32_t i;
260
 
  Library *tmp;
 
256
  plugin::Library *tmp;
261
257
 
262
258
  for (i= 0; i < plugin_dl_array.elements; i++)
263
259
  {
264
 
    tmp= *dynamic_element(&plugin_dl_array, i, Library **);
 
260
    tmp= *dynamic_element(&plugin_dl_array, i, plugin::Library **);
265
261
    {
266
 
      memcpy(tmp, plugin_dl, sizeof(Library));
 
262
      memcpy(tmp, plugin_dl, sizeof(plugin::Library));
267
263
      return(tmp);
268
264
    }
269
265
  }
270
266
  if (insert_dynamic(&plugin_dl_array, (unsigned char*)&plugin_dl))
271
267
    return(0);
272
268
  tmp= *dynamic_element(&plugin_dl_array, plugin_dl_array.elements - 1,
273
 
                        Library **)=
274
 
      (Library *) memdup_root(&plugin_mem_root, (unsigned char*)plugin_dl,
275
 
                                           sizeof(Library));
 
269
                        plugin::Library **)=
 
270
      (plugin::Library *) memdup_root(&plugin_mem_root,
 
271
                                      (unsigned char*)plugin_dl,
 
272
                                      sizeof(plugin::Library));
276
273
  return(tmp);
277
274
}
278
275
 
279
 
static inline void free_plugin_mem(Library *p)
 
276
static inline void free_plugin_mem(plugin::Library *p)
280
277
{
281
278
  if (p->handle)
282
279
    dlclose(p->handle);
284
281
}
285
282
 
286
283
 
287
 
static Library *plugin_dl_add(const LEX_STRING *dl, int report)
 
284
static plugin::Library *plugin_dl_add(const LEX_STRING *dl, int report)
288
285
{
289
286
  string dlpath;
290
287
  uint32_t plugin_dir_len;
291
 
  Library *tmp, plugin_dl;
 
288
  plugin::Library *tmp, plugin_dl;
292
289
  void *sym;
293
290
  plugin_dir_len= strlen(opt_plugin_dir);
294
291
  dlpath.reserve(FN_REFLEN);
347
344
    return(0);
348
345
  }
349
346
 
350
 
  plugin_dl.plugins= static_cast<Manifest *>(sym);
 
347
  plugin_dl.plugins= static_cast<plugin::Manifest *>(sym);
351
348
 
352
349
  /* Duplicate and convert dll name */
353
350
  plugin_dl.dl.length= dl->length * files_charset_info->mbmaxlen + 1;
366
363
  {
367
364
    free_plugin_mem(&plugin_dl);
368
365
    if (report & REPORT_TO_USER)
369
 
      my_error(ER_OUTOFMEMORY, MYF(0), sizeof(Library));
 
366
      my_error(ER_OUTOFMEMORY, MYF(0), sizeof(plugin::Library));
370
367
    if (report & REPORT_TO_LOG)
371
 
      errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_OUTOFMEMORY), sizeof(Library));
 
368
      errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_OUTOFMEMORY),
 
369
                    sizeof(plugin::Library));
372
370
    return(0);
373
371
  }
374
372
  return(tmp);
381
379
 
382
380
  for (i= 0; i < plugin_dl_array.elements; i++)
383
381
  {
384
 
    Library *tmp= *dynamic_element(&plugin_dl_array, i,
385
 
                                               Library **);
 
382
    plugin::Library *tmp= *dynamic_element(&plugin_dl_array, i,
 
383
                                           plugin::Library **);
386
384
    if (! my_strnncoll(files_charset_info,
387
385
                       (const unsigned char *)dl->str, dl->length,
388
386
                       (const unsigned char *)tmp->dl.str, tmp->dl.length))
390
388
      /* Do not remove this element, unless no other plugin uses this dll. */
391
389
      {
392
390
        free_plugin_mem(tmp);
393
 
        memset(tmp, 0, sizeof(Library));
 
391
        memset(tmp, 0, sizeof(plugin::Library));
394
392
      }
395
393
      break;
396
394
    }
400
398
 
401
399
 
402
400
 
403
 
static Handle *plugin_insert_or_reuse(Handle *plugin)
 
401
static plugin::Handle *plugin_insert_or_reuse(plugin::Handle *plugin)
404
402
{
405
403
  if (insert_dynamic(&plugin_array, (unsigned char*)&plugin))
406
404
    return(0);
407
405
  plugin= *dynamic_element(&plugin_array, plugin_array.elements - 1,
408
 
                        Handle **);
 
406
                        plugin::Handle **);
409
407
  return(plugin);
410
408
}
411
409
 
420
418
{
421
419
  PluginRegistry &registry= PluginRegistry::getPluginRegistry();
422
420
 
423
 
  Manifest *manifest;
 
421
  plugin::Manifest *manifest;
424
422
  if (! initialized)
425
423
    return(0);
426
424
 
432
430
      errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_UDF_EXISTS), name->str);
433
431
    return(true);
434
432
  }
435
 
  Library *library= plugin_dl_add(dl, report);
 
433
  plugin::Library *library= plugin_dl_add(dl, report);
436
434
  if (library == NULL)
437
435
    return true;
438
436
 
439
 
  Handle *tmp= NULL;
 
437
  plugin::Handle *tmp= NULL;
440
438
  /* Find plugin by name */
441
439
  for (manifest= library->plugins; manifest->name; manifest++)
442
440
  {
445
443
                       (const unsigned char *)manifest->name,
446
444
                       strlen(manifest->name)))
447
445
    {
448
 
      tmp= new (std::nothrow) Handle(manifest, library);
 
446
      tmp= new (std::nothrow) plugin::Handle(manifest, library);
449
447
      if (tmp == NULL)
450
448
        return true;
451
449
 
475
473
}
476
474
 
477
475
 
478
 
static void plugin_del(Handle *plugin)
 
476
static void plugin_del(plugin::Handle *plugin)
479
477
{
480
478
  PluginRegistry &registry= PluginRegistry::getPluginRegistry();
481
479
  if (plugin->isInited)
513
511
    plugin= *dynamic_element(&plugin_array, idx, drizzled::plugin::Handle **);
514
512
    plugin_del(plugin);
515
513
  }
 
514
  drizzle_del_plugin_sysvar();
516
515
}
517
516
 
518
517
static bool plugin_initialize(drizzled::plugin::Handle *plugin)
580
579
int plugin_init(int *argc, char **argv, int flags)
581
580
{
582
581
  uint32_t idx;
583
 
  Manifest **builtins;
584
 
  Manifest *manifest;
585
 
  Handle *handle;
 
582
  plugin::Manifest **builtins;
 
583
  plugin::Manifest *manifest;
 
584
  plugin::Handle *handle;
586
585
  MEM_ROOT tmp_root;
587
586
 
588
587
  if (initialized)
597
596
 
598
597
 
599
598
  if (my_init_dynamic_array(&plugin_dl_array,
600
 
                            sizeof(Library *),16,16) ||
 
599
                            sizeof(plugin::Library *),16,16) ||
601
600
      my_init_dynamic_array(&plugin_array,
602
 
                            sizeof(Handle *),16,16))
 
601
                            sizeof(plugin::Handle *),16,16))
603
602
    goto err;
604
603
 
605
604
  initialized= 1;
611
610
  {
612
611
    for (manifest= *builtins; manifest->name; manifest++)
613
612
    {
614
 
      handle= new (std::nothrow) Handle(manifest);
 
613
      handle= new (std::nothrow) plugin::Handle(manifest);
615
614
      if (handle == NULL)
616
615
        return true;
617
616
 
644
643
  */
645
644
  for (idx= 0; idx < plugin_array.elements; idx++)
646
645
  {
647
 
    handle= *dynamic_element(&plugin_array, idx, Handle **);
 
646
    handle= *dynamic_element(&plugin_array, idx, plugin::Handle **);
648
647
    if (handle->isInited == false)
649
648
    {
650
649
      if (plugin_initialize(handle))
665
664
}
666
665
 
667
666
 
668
 
static bool register_builtin(Handle *tmp,
669
 
                             Handle **ptr)
 
667
static bool register_builtin(plugin::Handle *tmp,
 
668
                             plugin::Handle **ptr)
670
669
{
671
670
 
672
671
  PluginRegistry &registry= PluginRegistry::getPluginRegistry();
678
677
    return(1);
679
678
 
680
679
  *ptr= *dynamic_element(&plugin_array, plugin_array.elements - 1,
681
 
                         Handle **);
 
680
                         plugin::Handle **);
682
681
 
683
682
  registry.add(*ptr);
684
683
 
694
693
{
695
694
  char buffer[FN_REFLEN];
696
695
  LEX_STRING name= {buffer, 0}, dl= {NULL, 0}, *str= &name;
697
 
  Library *plugin_dl;
698
 
  Manifest *plugin;
 
696
  plugin::Library *plugin_dl;
 
697
  plugin::Manifest *plugin;
699
698
  char *p= buffer;
700
699
  while (list)
701
700
  {
771
770
{
772
771
  uint32_t idx;
773
772
  size_t count= plugin_array.elements;
774
 
  vector<Handle *> plugins;
775
 
  vector<Library *> dl;
 
773
  vector<plugin::Handle *> plugins;
 
774
  vector<plugin::Library *> dl;
776
775
 
777
776
  if (initialized)
778
777
  {
796
795
  dl.reserve(count);
797
796
  for (idx= 0; idx < count; idx++)
798
797
    dl.push_back(*dynamic_element(&plugin_dl_array, idx,
799
 
                 Library **));
 
798
                 plugin::Library **));
800
799
  for (idx= 0; idx < count; idx++)
801
800
    free_plugin_mem(dl[idx]);
802
801
  delete_dynamic(&plugin_dl_array);
1116
1115
{
1117
1116
  sys_var *var;
1118
1117
  sys_var_pluginvar *pi= NULL;
1119
 
  Handle *plugin;
 
1118
  plugin::Handle *plugin;
1120
1119
 
1121
1120
  pthread_rwlock_rdlock(&LOCK_system_variables_hash);
1122
1121
  if ((var= intern_find_sys_var(str, length, false)) &&
1860
1859
}
1861
1860
 
1862
1861
 
1863
 
static int construct_options(MEM_ROOT *mem_root, Handle *tmp,
 
1862
static int construct_options(MEM_ROOT *mem_root, plugin::Handle *tmp,
1864
1863
                             my_option *options, bool can_disable)
1865
1864
{
1866
1865
  const char *plugin_name= tmp->getManifest().name;
2075
2074
}
2076
2075
 
2077
2076
 
2078
 
static my_option *construct_help_options(MEM_ROOT *mem_root, Handle *p)
 
2077
static my_option *construct_help_options(MEM_ROOT *mem_root, plugin::Handle *p)
2079
2078
{
2080
2079
  st_mysql_sys_var **opt;
2081
2080
  my_option *opts;
2104
2103
  return(opts);
2105
2104
}
2106
2105
 
 
2106
void drizzle_add_plugin_sysvar(sys_var_pluginvar *var)
 
2107
{
 
2108
  plugin_sysvar_vec.push_back(var);
 
2109
}
 
2110
 
 
2111
void drizzle_del_plugin_sysvar()
 
2112
{
 
2113
  vector<sys_var_pluginvar *>::iterator iter= plugin_sysvar_vec.begin();
 
2114
  while(iter != plugin_sysvar_vec.end())
 
2115
  {
 
2116
    delete *iter;
 
2117
    ++iter;
 
2118
  }
 
2119
  plugin_sysvar_vec.clear();
 
2120
}
2107
2121
 
2108
2122
/*
2109
2123
  SYNOPSIS
2118
2132
  NOTE:
2119
2133
    Requires that a write-lock is held on LOCK_system_variables_hash
2120
2134
*/
2121
 
static int test_plugin_options(MEM_ROOT *tmp_root, Handle *tmp,
 
2135
static int test_plugin_options(MEM_ROOT *tmp_root, plugin::Handle *tmp,
2122
2136
                               int *argc, char **argv)
2123
2137
{
2124
2138
  struct sys_var_chain chain= { NULL, NULL };
2125
2139
  bool can_disable;
2126
 
  MEM_ROOT *mem_root= alloc_root_inited(&tmp->mem_root) ?
2127
 
                      &tmp->mem_root : &plugin_mem_root;
2128
2140
  st_mysql_sys_var **opt;
2129
2141
  my_option *opts= NULL;
2130
 
  char *p, *varname;
2131
2142
  int error;
2132
2143
  st_mysql_sys_var *o;
2133
 
  sys_var *v;
2134
2144
  struct st_bookmark *var;
2135
2145
  uint32_t len, count= EXTRA_OPTIONS;
2136
2146
 
2175
2185
  {
2176
2186
    for (opt= tmp->getManifest().system_vars; opt && *opt; opt++)
2177
2187
    {
 
2188
      sys_var *v;
2178
2189
      if (((o= *opt)->flags & PLUGIN_VAR_NOSYSVAR))
2179
2190
        continue;
2180
2191
 
2181
2192
      if ((var= find_bookmark(tmp->getName().c_str(), o->name, o->flags)))
2182
 
        v= new (mem_root) sys_var_pluginvar(var->key + 1, o);
 
2193
        v= new sys_var_pluginvar(var->key + 1, o);
2183
2194
      else
2184
2195
      {
2185
2196
        len= tmp->getName().length() + strlen(o->name) + 2;
2186
 
        varname= (char*) alloc_root(mem_root, len);
2187
 
        sprintf(varname,"%s-%s",tmp->getName().c_str(),o->name);
2188
 
        my_casedn_str(&my_charset_utf8_general_ci, varname);
2189
 
 
2190
 
        for (p= varname; *p; p++)
 
2197
        string vname(tmp->getName());
 
2198
        vname.push_back('-');
 
2199
        vname.append(o->name);
 
2200
        transform(vname.begin(), vname.end(), vname.begin(), ::tolower);
 
2201
        string::iterator p= vname.begin();      
 
2202
        while  (p != vname.end())
 
2203
        {
2191
2204
          if (*p == '-')
2192
2205
            *p= '_';
 
2206
          ++p;
 
2207
        }
2193
2208
 
2194
 
        v= new (mem_root) sys_var_pluginvar(varname, o);
 
2209
        v= new sys_var_pluginvar(vname, o);
2195
2210
      }
2196
2211
      assert(v); /* check that an object was actually constructed */
2197
2212
 
 
2213
      drizzle_add_plugin_sysvar(static_cast<sys_var_pluginvar *>(v));
2198
2214
      /*
2199
2215
        Add to the chain of variables.
2200
2216
        Done like this for easier debugging so that the
2240
2256
void my_print_help_inc_plugins(my_option *main_options)
2241
2257
{
2242
2258
  vector<my_option> all_options;
2243
 
  Handle *p;
 
2259
  plugin::Handle *p;
2244
2260
  MEM_ROOT mem_root;
2245
2261
  my_option *opt= NULL;
2246
2262
 
2249
2265
  if (initialized)
2250
2266
    for (uint32_t idx= 0; idx < plugin_array.elements; idx++)
2251
2267
    {
2252
 
      p= *dynamic_element(&plugin_array, idx, Handle **);
 
2268
      p= *dynamic_element(&plugin_array, idx, plugin::Handle **);
2253
2269
 
2254
2270
      if (p->getManifest().system_vars == NULL)
2255
2271
        continue;