~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/module/registry.cc

  • Committer: Monty Taylor
  • Date: 2010-11-08 18:26:08 UTC
  • mto: This revision was merged to the branch mainline in revision 1931.
  • Revision ID: mordred@inaugust.com-20101108182608-lci86acl7r53sbi3
Replaced auto_ptr with scoped_ptr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2008 Sun Microsystems
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include <config.h>
 
20
#include "config.h"
21
21
 
22
22
#include <string>
23
23
#include <vector>
24
24
#include <map>
25
25
 
26
 
#include <drizzled/module/registry.h>
27
 
#include <drizzled/module/library.h>
28
 
#include <drizzled/module/graph.h>
29
 
#include <drizzled/module/vertex_handle.h>
30
 
 
31
 
#include <drizzled/plugin.h>
32
 
#include <drizzled/show.h>
33
 
#include <drizzled/cursor.h>
34
 
#include <drizzled/abort_exception.h>
35
 
 
36
 
#include <boost/bind.hpp>
 
26
#include "drizzled/module/registry.h"
 
27
#include "drizzled/module/library.h"
 
28
 
 
29
#include "drizzled/plugin.h"
 
30
#include "drizzled/show.h"
 
31
#include "drizzled/cursor.h"
37
32
 
38
33
using namespace std;
39
34
 
40
35
namespace drizzled
41
36
{
42
37
 
43
 
module::Registry::Registry() :
44
 
  module_registry_(),
45
 
  depend_graph_(new module::Graph()),
46
 
  plugin_registry(),
47
 
  deps_built_(false)
48
 
{ }
49
 
 
50
38
 
51
39
module::Registry::~Registry()
52
40
{
53
 
  plugin::Plugin::map::iterator plugin_iter;
 
41
  map<string, plugin::Plugin *>::iterator plugin_iter;
54
42
 
55
43
  /* Give all plugins a chance to cleanup, before
56
44
   * all plugins are deleted.
64
52
    ++plugin_iter;
65
53
  }
66
54
 
67
 
  plugin::Plugin::vector error_plugins;
68
55
  plugin_iter= plugin_registry.begin();
69
56
  while (plugin_iter != plugin_registry.end())
70
57
  {
71
 
    if ((*plugin_iter).second->removeLast())
72
 
    {
73
 
      error_plugins.push_back((*plugin_iter).second);
74
 
    }
75
 
    else
76
 
    {
77
 
      delete (*plugin_iter).second;
78
 
    }
 
58
    delete (*plugin_iter).second;
79
59
    ++plugin_iter;
80
60
  }
81
 
 
82
 
  for (plugin::Plugin::vector::iterator iter= error_plugins.begin();
83
 
       iter != error_plugins.end(); iter++)
84
 
  {
85
 
    delete *iter;
86
 
  }
87
 
 
88
61
  plugin_registry.clear();
89
62
 
90
 
#if 0
91
 
  @TODO When we delete modules here, we segfault on a bad string. Why?
92
 
   ModuleMap::iterator module_iter= module_registry_.begin();
93
 
 
94
 
  while (module_iter != module_registry_.end())
 
63
  /*
 
64
    @TODO When we delete modules here, we segfault on a bad string. Why?
 
65
    map<string, module::Module *>::iterator module_iter= module_map.begin();
 
66
  while (module_iter != module_map.end())
95
67
  {
96
68
    delete (*module_iter).second;
97
69
    ++module_iter;
98
70
  }
99
 
  module_registry_.clear();
100
 
#endif
101
 
  LibraryMap::iterator library_iter= library_registry_.begin();
102
 
  while (library_iter != library_registry_.end())
 
71
  module_map.clear();
 
72
  */
 
73
  map<string, module::Library *>::iterator library_iter= library_map.begin();
 
74
  while (library_iter != library_map.end())
103
75
  {
104
76
    delete (*library_iter).second;
105
77
    ++library_iter;
106
78
  }
107
 
  library_registry_.clear();
 
79
  library_map.clear();
108
80
}
109
81
 
110
82
void module::Registry::shutdown()
113
85
  delete &registry;
114
86
}
115
87
 
116
 
module::Module *module::Registry::find(std::string name)
 
88
module::Module *module::Registry::find(string name)
117
89
{
118
 
  std::transform(name.begin(), name.end(), name.begin(), ::tolower);
 
90
  transform(name.begin(), name.end(), name.begin(), ::tolower);
119
91
 
120
 
  ModuleMap::iterator map_iter;
121
 
  map_iter= module_registry_.find(name);
122
 
  if (map_iter != module_registry_.end())
 
92
  map<string, module::Module *>::iterator map_iter;
 
93
  map_iter= module_map.find(name);
 
94
  if (map_iter != module_map.end())
123
95
    return (*map_iter).second;
124
 
  return NULL;
 
96
  return(0);
125
97
}
126
98
 
127
99
void module::Registry::add(module::Module *handle)
128
100
{
129
 
  std::string add_str(handle->getName());
 
101
  string add_str(handle->getName());
130
102
  transform(add_str.begin(), add_str.end(),
131
103
            add_str.begin(), ::tolower);
132
104
 
133
 
  module_registry_[add_str]= handle;
134
 
 
135
 
  Vertex vertex_info(add_str, handle);
136
 
  VertexDesc handle_vertex= boost::add_vertex(depend_graph_->getGraph());
137
 
  depend_graph_->properties(handle_vertex)= vertex_info;
138
 
 
139
 
  handle->setVertexHandle(new VertexHandle(handle_vertex));
140
 
 
 
105
  module_map[add_str]= handle;
141
106
}
142
107
 
143
108
void module::Registry::remove(module::Module *handle)
144
109
{
145
 
  std::string remove_str(handle->getName());
146
 
  std::transform(remove_str.begin(), remove_str.end(),
147
 
                 remove_str.begin(), ::tolower);
148
 
 
149
 
  module_registry_.erase(remove_str);
150
 
}
151
 
 
152
 
void module::Registry::copy(plugin::Plugin::vector &arg)
153
 
{    
154
 
  arg.reserve(plugin_registry.size());
155
 
 
156
 
  std::transform(plugin_registry.begin(),
157
 
                 plugin_registry.end(),
158
 
                 std::back_inserter(arg),
159
 
                 boost::bind(&plugin::Plugin::map::value_type::second, _1) );
160
 
  assert(arg.size() == plugin_registry.size());
161
 
}
162
 
 
163
 
void module::Registry::buildDeps()
164
 
{
165
 
  ModuleMap::iterator map_iter= module_registry_.begin();
166
 
  while (map_iter != module_registry_.end())
167
 
  {
168
 
    Module *handle= (*map_iter).second;
169
 
    Module::Depends::const_iterator handle_deps= handle->getDepends().begin();
170
 
    while (handle_deps != handle->getDepends().end())
171
 
    {
172
 
      std::string dep_str((*handle_deps));
173
 
      transform(dep_str.begin(), dep_str.end(),
174
 
                dep_str.begin(), ::tolower);
175
 
 
176
 
      bool found_dep= false;
177
 
      vertex_iter it= boost::vertices(depend_graph_->getGraph()).first;
178
 
      while (it != vertices(depend_graph_->getGraph()).second)
179
 
      {
180
 
        if (depend_graph_->properties(*it).getName() == dep_str)
181
 
        {
182
 
          found_dep= true;
183
 
          add_edge(handle->getVertexHandle()->getVertexDesc(), *it, depend_graph_->getGraph());
184
 
          break;
185
 
        }
186
 
        ++it;
187
 
      }
188
 
      if (not found_dep)
189
 
      {
190
 
        errmsg_printf(error::ERROR,
191
 
                      _("Couldn't process plugin module dependencies. "
192
 
                        "%s depends on %s but %s is not to be loaded.\n"),
193
 
                      handle->getName().c_str(),
194
 
                      dep_str.c_str(), dep_str.c_str());
195
 
        DRIZZLE_ABORT;
196
 
      }
197
 
 
198
 
      ++handle_deps;
199
 
    }
200
 
    ++map_iter;
201
 
  }
202
 
  deps_built_= true;
203
 
}
204
 
 
205
 
module::Registry::ModuleList module::Registry::getList()
206
 
{
207
 
  if (not deps_built_)
208
 
  {
209
 
    buildDeps();
210
 
  }
211
 
 
212
 
  std::vector<module::Module *> plugins;
213
 
 
214
 
  VertexList vertex_list;
215
 
 
216
 
  boost::topological_sort(depend_graph_->getGraph(), std::back_inserter(vertex_list));
217
 
 
218
 
  for (VertexList::iterator i = vertex_list.begin();
219
 
       i != vertex_list.end(); ++i)
220
 
  {
221
 
    Module *mod_ptr= depend_graph_->properties(*i).getModule();
222
 
    if (mod_ptr != NULL)
223
 
    {
224
 
      plugins.push_back(mod_ptr);
225
 
    }  
 
110
  string remove_str(handle->getName());
 
111
  transform(remove_str.begin(), remove_str.end(),
 
112
            remove_str.begin(), ::tolower);
 
113
 
 
114
  module_map.erase(remove_str);
 
115
}
 
116
 
 
117
vector<module::Module *> module::Registry::getList(bool active)
 
118
{
 
119
  module::Module *plugin= NULL;
 
120
 
 
121
  vector<module::Module *> plugins;
 
122
  plugins.reserve(module_map.size());
 
123
 
 
124
  map<string, module::Module *>::iterator map_iter;
 
125
  for (map_iter= module_map.begin();
 
126
       map_iter != module_map.end();
 
127
       map_iter++)
 
128
  {
 
129
    plugin= (*map_iter).second;
 
130
    if (active)
 
131
      plugins.push_back(plugin);
 
132
    else if (plugin->isInited)
 
133
      plugins.push_back(plugin);
226
134
  }
227
135
 
228
136
  return plugins;
229
137
}
230
138
 
231
 
module::Library *module::Registry::addLibrary(const std::string &plugin_name,
 
139
module::Library *module::Registry::addLibrary(const string &plugin_name,
232
140
                                              bool builtin)
233
141
{
234
142
 
243
151
  if (library != NULL)
244
152
  {
245
153
    /* Add this dll to the map */
246
 
    library_registry_.insert(make_pair(plugin_name, library));
 
154
    library_map.insert(make_pair(plugin_name, library));
247
155
  }
248
156
 
249
157
  return library;
250
158
}
251
159
 
252
 
void module::Registry::removeLibrary(const std::string &plugin_name)
 
160
void module::Registry::removeLibrary(const string &plugin_name)
253
161
{
254
 
  std::map<std::string, module::Library *>::iterator iter=
255
 
    library_registry_.find(plugin_name);
256
 
  if (iter != library_registry_.end())
 
162
  map<string, module::Library *>::iterator iter=
 
163
    library_map.find(plugin_name);
 
164
  if (iter != library_map.end())
257
165
  {
258
 
    library_registry_.erase(iter);
 
166
    library_map.erase(iter);
259
167
    delete (*iter).second;
260
168
  }
261
169
}
262
170
 
263
 
module::Library *module::Registry::findLibrary(const std::string &plugin_name) const
 
171
module::Library *module::Registry::findLibrary(const string &plugin_name) const
264
172
{
265
 
  LibraryMap::const_iterator iter= library_registry_.find(plugin_name);
266
 
  if (iter != library_registry_.end())
 
173
  map<string, module::Library *>::const_iterator iter=
 
174
    library_map.find(plugin_name);
 
175
  if (iter != library_map.end())
267
176
    return (*iter).second;
268
177
  return NULL;
269
178
}