~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/module/registry.cc

  • Committer: Monty Taylor
  • Date: 2011-03-10 18:09:05 UTC
  • mfrom: (2225.2.2 refactor)
  • mto: This revision was merged to the branch mainline in revision 2228.
  • Revision ID: mordred@inaugust.com-20110310180905-ttx05t7q7ff6nl7c
Merge Olad: Refactoring

Show diffs side-by-side

added added

removed removed

Lines of Context:
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"
 
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>
28
30
 
29
 
#include "drizzled/plugin.h"
30
 
#include "drizzled/show.h"
31
 
#include "drizzled/cursor.h"
 
31
#include <drizzled/plugin.h>
 
32
#include <drizzled/show.h>
 
33
#include <drizzled/cursor.h>
 
34
#include <drizzled/abort_exception.h>
32
35
 
33
36
#include <boost/bind.hpp>
34
37
 
37
40
namespace drizzled
38
41
{
39
42
 
 
43
module::Registry::Registry() :
 
44
  module_registry_(),
 
45
  depend_graph_(new module::Graph()),
 
46
  plugin_registry(),
 
47
  deps_built_(false)
 
48
{ }
 
49
 
40
50
 
41
51
module::Registry::~Registry()
42
52
{
43
 
  std::map<std::string, plugin::Plugin *>::iterator plugin_iter;
 
53
  plugin::Plugin::map::iterator plugin_iter;
44
54
 
45
55
  /* Give all plugins a chance to cleanup, before
46
56
   * all plugins are deleted.
54
64
    ++plugin_iter;
55
65
  }
56
66
 
 
67
  plugin::Plugin::vector error_plugins;
57
68
  plugin_iter= plugin_registry.begin();
58
69
  while (plugin_iter != plugin_registry.end())
59
70
  {
60
 
    delete (*plugin_iter).second;
 
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
    }
61
79
    ++plugin_iter;
62
80
  }
 
81
 
 
82
  for (plugin::Plugin::vector::iterator iter= error_plugins.begin();
 
83
       iter != error_plugins.end(); iter++)
 
84
  {
 
85
    delete *iter;
 
86
  }
 
87
 
63
88
  plugin_registry.clear();
64
89
 
65
90
#if 0
66
91
  @TODO When we delete modules here, we segfault on a bad string. Why?
67
 
    map<string, module::Module *>::iterator module_iter= module_map.begin();
 
92
   ModuleMap::iterator module_iter= module_registry_.begin();
68
93
 
69
 
  while (module_iter != module_map.end())
 
94
  while (module_iter != module_registry_.end())
70
95
  {
71
96
    delete (*module_iter).second;
72
97
    ++module_iter;
73
98
  }
74
 
  module_map.clear();
 
99
  module_registry_.clear();
75
100
#endif
76
 
  std::map<std::string, module::Library *>::iterator library_iter= library_map.begin();
77
 
  while (library_iter != library_map.end())
 
101
  LibraryMap::iterator library_iter= library_registry_.begin();
 
102
  while (library_iter != library_registry_.end())
78
103
  {
79
104
    delete (*library_iter).second;
80
105
    ++library_iter;
81
106
  }
82
 
  library_map.clear();
 
107
  library_registry_.clear();
83
108
}
84
109
 
85
110
void module::Registry::shutdown()
92
117
{
93
118
  std::transform(name.begin(), name.end(), name.begin(), ::tolower);
94
119
 
95
 
  std::map<std::string, module::Module *>::iterator map_iter;
96
 
  map_iter= module_map.find(name);
97
 
  if (map_iter != module_map.end())
 
120
  ModuleMap::iterator map_iter;
 
121
  map_iter= module_registry_.find(name);
 
122
  if (map_iter != module_registry_.end())
98
123
    return (*map_iter).second;
99
 
  return(0);
 
124
  return NULL;
100
125
}
101
126
 
102
127
void module::Registry::add(module::Module *handle)
105
130
  transform(add_str.begin(), add_str.end(),
106
131
            add_str.begin(), ::tolower);
107
132
 
108
 
  module_map[add_str]= handle;
 
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
 
109
141
}
110
142
 
111
143
void module::Registry::remove(module::Module *handle)
114
146
  std::transform(remove_str.begin(), remove_str.end(),
115
147
                 remove_str.begin(), ::tolower);
116
148
 
117
 
  module_map.erase(remove_str);
 
149
  module_registry_.erase(remove_str);
118
150
}
119
151
 
120
152
void module::Registry::copy(plugin::Plugin::vector &arg)
128
160
  assert(arg.size() == plugin_registry.size());
129
161
}
130
162
 
131
 
vector<module::Module *> module::Registry::getList(bool active)
132
 
{
133
 
  module::Module *plugin= NULL;
 
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
  }
134
211
 
135
212
  std::vector<module::Module *> plugins;
136
 
  plugins.reserve(module_map.size());
137
 
 
138
 
  std::map<std::string, module::Module *>::iterator map_iter;
139
 
  for (map_iter= module_map.begin();
140
 
       map_iter != module_map.end();
141
 
       map_iter++)
 
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)
142
220
  {
143
 
    plugin= (*map_iter).second;
144
 
    if (active)
145
 
      plugins.push_back(plugin);
146
 
    else if (plugin->isInited)
147
 
      plugins.push_back(plugin);
 
221
    Module *mod_ptr= depend_graph_->properties(*i).getModule();
 
222
    if (mod_ptr != NULL)
 
223
    {
 
224
      plugins.push_back(mod_ptr);
 
225
    }  
148
226
  }
149
227
 
150
228
  return plugins;
165
243
  if (library != NULL)
166
244
  {
167
245
    /* Add this dll to the map */
168
 
    library_map.insert(make_pair(plugin_name, library));
 
246
    library_registry_.insert(make_pair(plugin_name, library));
169
247
  }
170
248
 
171
249
  return library;
174
252
void module::Registry::removeLibrary(const std::string &plugin_name)
175
253
{
176
254
  std::map<std::string, module::Library *>::iterator iter=
177
 
    library_map.find(plugin_name);
178
 
  if (iter != library_map.end())
 
255
    library_registry_.find(plugin_name);
 
256
  if (iter != library_registry_.end())
179
257
  {
180
 
    library_map.erase(iter);
181
 
    delete (*iter).second;
 
258
    library_registry_.erase(iter);
 
259
    delete iter->second;
182
260
  }
183
261
}
184
262
 
185
263
module::Library *module::Registry::findLibrary(const std::string &plugin_name) const
186
264
{
187
 
  std::map<std::string, module::Library *>::const_iterator iter=
188
 
    library_map.find(plugin_name);
189
 
  if (iter != library_map.end())
190
 
    return (*iter).second;
 
265
  LibraryMap::const_iterator iter= library_registry_.find(plugin_name);
 
266
  if (iter != library_registry_.end())
 
267
    return iter->second;
191
268
  return NULL;
192
269
}
193
270