~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/module/registry.cc

  • Committer: pcrews
  • Date: 2011-05-24 17:36:24 UTC
  • mfrom: (1099.4.232 drizzle)
  • Revision ID: pcrews@lucid32-20110524173624-mwr1bvq6fa1r01ao
Updated translations + 2011.05.18 tarball tag

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