~ubuntu-branches/ubuntu/saucy/drizzle/saucy-proposed

« back to all changes in this revision

Viewing changes to drizzled/module/registry.cc

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2012-06-19 10:46:49 UTC
  • mfrom: (1.1.6)
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20120619104649-e2l0ggd4oz3um0f4
Tags: upstream-7.1.36-stable
ImportĀ upstreamĀ versionĀ 7.1.36-stable

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
#include <drizzled/show.h>
33
33
#include <drizzled/cursor.h>
34
34
#include <drizzled/abort_exception.h>
 
35
#include <drizzled/util/find_ptr.h>
35
36
 
36
37
#include <boost/bind.hpp>
 
38
#include <boost/foreach.hpp>
37
39
 
38
40
using namespace std;
39
41
 
40
 
namespace drizzled
41
 
{
 
42
namespace drizzled {
42
43
 
43
44
module::Registry::Registry() :
44
45
  module_registry_(),
50
51
 
51
52
module::Registry::~Registry()
52
53
{
53
 
  plugin::Plugin::map::iterator plugin_iter;
54
 
 
55
54
  /* Give all plugins a chance to cleanup, before
56
55
   * all plugins are deleted.
57
56
   * This can be used if shutdown code references
58
57
   * other plugins.
59
58
   */
60
 
  plugin_iter= plugin_registry.begin();
61
 
  while (plugin_iter != plugin_registry.end())
62
 
  {
63
 
    (*plugin_iter).second->shutdownPlugin();
64
 
    ++plugin_iter;
65
 
  }
 
59
  BOOST_FOREACH(plugin::Plugin::map::reference it, plugin_registry)
 
60
    it.second->shutdownPlugin();
66
61
 
67
62
  plugin::Plugin::vector error_plugins;
68
 
  plugin_iter= plugin_registry.begin();
69
 
  while (plugin_iter != plugin_registry.end())
 
63
  BOOST_FOREACH(plugin::Plugin::map::reference it, plugin_registry)
70
64
  {
71
 
    if ((*plugin_iter).second->removeLast())
72
 
    {
73
 
      error_plugins.push_back((*plugin_iter).second);
74
 
    }
 
65
    if (it.second->removeLast())
 
66
      error_plugins.push_back(it.second);
75
67
    else
76
 
    {
77
 
      delete (*plugin_iter).second;
78
 
    }
79
 
    ++plugin_iter;
 
68
      delete it.second;
80
69
  }
81
70
 
82
 
  for (plugin::Plugin::vector::iterator iter= error_plugins.begin();
83
 
       iter != error_plugins.end(); iter++)
84
 
  {
85
 
    delete *iter;
86
 
  }
 
71
  BOOST_FOREACH(plugin::Plugin::vector::reference it, error_plugins)
 
72
    delete it;
87
73
 
88
74
  plugin_registry.clear();
89
75
 
90
76
#if 0
 
77
  /*
91
78
  @TODO When we delete modules here, we segfault on a bad string. Why?
92
 
   ModuleMap::iterator module_iter= module_registry_.begin();
 
79
   */
93
80
 
94
 
  while (module_iter != module_registry_.end())
95
 
  {
96
 
    delete (*module_iter).second;
97
 
    ++module_iter;
98
 
  }
 
81
  BOOST_FOREACH(ModuleMap::reference it, module_registry_)
 
82
    delete it.second;
99
83
  module_registry_.clear();
100
84
#endif
101
 
  LibraryMap::iterator library_iter= library_registry_.begin();
102
 
  while (library_iter != library_registry_.end())
103
 
  {
104
 
    delete (*library_iter).second;
105
 
    ++library_iter;
106
 
  }
 
85
  BOOST_FOREACH(LibraryMap::reference it, library_registry_)
 
86
    delete it.second;
107
87
  library_registry_.clear();
108
88
}
109
89
 
110
90
void module::Registry::shutdown()
111
91
{
112
 
  module::Registry& registry= singleton();
113
 
  delete &registry;
 
92
  delete &singleton();
114
93
}
115
94
 
116
 
module::Module *module::Registry::find(std::string name)
 
95
module::Module* module::Registry::find(const std::string& name)
117
96
{
118
 
  std::transform(name.begin(), name.end(), name.begin(), ::tolower);
119
 
 
120
 
  ModuleMap::iterator map_iter;
121
 
  map_iter= module_registry_.find(name);
122
 
  if (map_iter != module_registry_.end())
123
 
    return (*map_iter).second;
124
 
  return NULL;
 
97
  return find_ptr2(module_registry_, boost::to_lower_copy(name));
125
98
}
126
99
 
127
100
void module::Registry::add(module::Module *handle)
128
101
{
129
 
  std::string add_str(handle->getName());
130
 
  transform(add_str.begin(), add_str.end(),
131
 
            add_str.begin(), ::tolower);
 
102
  std::string add_str(boost::to_lower_copy(handle->getName()));
132
103
 
133
104
  module_registry_[add_str]= handle;
134
105
 
137
108
  depend_graph_->properties(handle_vertex)= vertex_info;
138
109
 
139
110
  handle->setVertexHandle(new VertexHandle(handle_vertex));
140
 
 
141
111
}
142
112
 
143
113
void module::Registry::remove(module::Module *handle)
144
114
{
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());
 
115
  module_registry_.erase(boost::to_lower_copy(handle->getName()));
161
116
}
162
117
 
163
118
void module::Registry::buildDeps()
164
119
{
165
 
  ModuleMap::iterator map_iter= module_registry_.begin();
166
 
  while (map_iter != module_registry_.end())
 
120
  BOOST_FOREACH(ModuleMap::reference map_iter, module_registry_)
167
121
  {
168
 
    Module *handle= (*map_iter).second;
169
 
    Module::Depends::const_iterator handle_deps= handle->getDepends().begin();
170
 
    while (handle_deps != handle->getDepends().end())
 
122
    Module* handle= map_iter.second;
 
123
    BOOST_FOREACH(Module::Depends::const_reference handle_deps, handle->getDepends())
171
124
    {
172
 
      std::string dep_str((*handle_deps));
173
 
      transform(dep_str.begin(), dep_str.end(),
174
 
                dep_str.begin(), ::tolower);
175
 
 
 
125
      std::string dep_str(boost::to_lower_copy(handle_deps));
176
126
      bool found_dep= false;
177
 
      vertex_iter it= boost::vertices(depend_graph_->getGraph()).first;
178
 
      while (it != vertices(depend_graph_->getGraph()).second)
 
127
      for (vertex_iter it= boost::vertices(depend_graph_->getGraph()).first; it != vertices(depend_graph_->getGraph()).second; it++)
179
128
      {
180
129
        if (depend_graph_->properties(*it).getName() == dep_str)
181
130
        {
183
132
          add_edge(handle->getVertexHandle()->getVertexDesc(), *it, depend_graph_->getGraph());
184
133
          break;
185
134
        }
186
 
        ++it;
187
135
      }
188
136
      if (not found_dep)
189
137
      {
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());
 
138
        errmsg_printf(error::ERROR, _("Couldn't process plugin module dependencies. %s depends on %s but %s is not to be loaded.\n"),
 
139
          handle->getName().c_str(), dep_str.c_str(), dep_str.c_str());
195
140
        DRIZZLE_ABORT;
196
141
      }
197
 
 
198
 
      ++handle_deps;
199
142
    }
200
 
    ++map_iter;
201
143
  }
202
144
  deps_built_= true;
203
145
}
205
147
module::Registry::ModuleList module::Registry::getList()
206
148
{
207
149
  if (not deps_built_)
208
 
  {
209
150
    buildDeps();
210
 
  }
211
 
 
212
 
  std::vector<module::Module *> plugins;
213
 
 
214
151
  VertexList vertex_list;
215
 
 
216
152
  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)
 
153
  ModuleList plugins;
 
154
  BOOST_FOREACH(VertexList::reference it, vertex_list)
220
155
  {
221
 
    Module *mod_ptr= depend_graph_->properties(*i).getModule();
222
 
    if (mod_ptr != NULL)
223
 
    {
 
156
    if (Module* mod_ptr= depend_graph_->properties(it).getModule())
224
157
      plugins.push_back(mod_ptr);
225
 
    }  
226
158
  }
227
 
 
228
159
  return plugins;
229
160
}
230
161
 
231
 
module::Library *module::Registry::addLibrary(const std::string &plugin_name,
232
 
                                              bool builtin)
 
162
module::Library *module::Registry::addLibrary(const std::string &plugin_name, bool builtin)
233
163
{
234
 
 
235
164
  /* If this dll is already loaded just return it */
236
165
  module::Library *library= findLibrary(plugin_name);
237
 
  if (library != NULL)
238
 
  {
 
166
  if (library)
239
167
    return library;
240
 
  }
241
168
 
242
169
  library= module::Library::loadLibrary(plugin_name, builtin);
243
 
  if (library != NULL)
 
170
  if (library)
244
171
  {
245
172
    /* Add this dll to the map */
246
173
    library_registry_.insert(make_pair(plugin_name, library));
247
174
  }
248
 
 
249
175
  return library;
250
176
}
251
177
 
252
178
void module::Registry::removeLibrary(const std::string &plugin_name)
253
179
{
254
 
  std::map<std::string, module::Library *>::iterator iter=
255
 
    library_registry_.find(plugin_name);
 
180
  LibraryMap::iterator iter= library_registry_.find(plugin_name);
256
181
  if (iter != library_registry_.end())
257
182
  {
 
183
    delete iter->second;
258
184
    library_registry_.erase(iter);
259
 
    delete iter->second;
260
185
  }
261
186
}
262
187
 
263
188
module::Library *module::Registry::findLibrary(const std::string &plugin_name) const
264
189
{
265
 
  LibraryMap::const_iterator iter= library_registry_.find(plugin_name);
266
 
  if (iter != library_registry_.end())
267
 
    return iter->second;
268
 
  return NULL;
 
190
  return find_ptr2(library_registry_, plugin_name);
269
191
}
270
192
 
271
193
void module::Registry::shutdownModules()