~drizzle-trunk/drizzle/development

908.2.1 by Monty Taylor
First pass at refactoring plugins - factored out sql_map.
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
908.2.1 by Monty Taylor
First pass at refactoring plugins - factored out sql_map.
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; version 2 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
19
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
20
#include "config.h"
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
21
22
#include <string>
23
#include <vector>
24
#include <map>
25
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
26
#include "drizzled/module/registry.h"
27
#include "drizzled/module/library.h"
2095.3.7 by Monty Taylor
Fixed the dependency graph so that it doesn't get included in sql_yacc.yy.
28
#include "drizzled/module/graph.h"
29
#include "drizzled/module/vertex_handle.h"
908.2.1 by Monty Taylor
First pass at refactoring plugins - factored out sql_map.
30
31
#include "drizzled/plugin.h"
971.1.46 by Monty Taylor
Made plugin registration go through Plugin_registry.
32
#include "drizzled/show.h"
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
33
#include "drizzled/cursor.h"
2095.3.6 by Monty Taylor
Actually properly process module dependencies. w00t.
34
#include "drizzled/abort_exception.h"
908.2.1 by Monty Taylor
First pass at refactoring plugins - factored out sql_map.
35
1966.2.6 by Brian Aker
This is from the catalog patch (I'm pushing it up as its own little thing
36
#include <boost/bind.hpp>
37
908.2.1 by Monty Taylor
First pass at refactoring plugins - factored out sql_map.
38
using namespace std;
39
1130.1.1 by Monty Taylor
Merged in plugin-slot-reorg patches.
40
namespace drizzled
41
{
1110.1.5 by Monty Taylor
Renamed PluginRegistry to plugin::Registry.
42
2095.3.7 by Monty Taylor
Fixed the dependency graph so that it doesn't get included in sql_yacc.yy.
43
module::Registry::Registry() :
44
  module_registry_(),
45
  depend_graph_(new module::Graph()),
46
  plugin_registry(),
47
  deps_built_(false)
48
{ }
49
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
50
51
module::Registry::~Registry()
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
52
{
2068.2.1 by Monty Taylor
Changed the plugin_registry to store a pair of plugin_type/plugin_name
53
  plugin::Plugin::map::iterator plugin_iter;
1784.4.1 by Paul McCullagh
Fixed PBXT recovery and shutdown, added shutdownPlugin() call to all plugins before the plugins are deleted
54
55
  /* Give all plugins a chance to cleanup, before
56
   * all plugins are deleted.
57
   * This can be used if shutdown code references
58
   * other plugins.
59
   */
60
  plugin_iter= plugin_registry.begin();
61
  while (plugin_iter != plugin_registry.end())
62
  {
63
    (*plugin_iter).second->shutdownPlugin();
64
    ++plugin_iter;
65
  }
66
67
  plugin_iter= plugin_registry.begin();
1324.2.3 by Monty Taylor
Remove plugin deinit.
68
  while (plugin_iter != plugin_registry.end())
69
  {
70
    delete (*plugin_iter).second;
71
    ++plugin_iter;
72
  }
73
  plugin_registry.clear();
74
1966.2.6 by Brian Aker
This is from the catalog patch (I'm pushing it up as its own little thing
75
#if 0
76
  @TODO When we delete modules here, we segfault on a bad string. Why?
2068.2.1 by Monty Taylor
Changed the plugin_registry to store a pair of plugin_type/plugin_name
77
   ModuleMap::iterator module_iter= module_registry_.begin();
1966.2.6 by Brian Aker
This is from the catalog patch (I'm pushing it up as its own little thing
78
2068.2.1 by Monty Taylor
Changed the plugin_registry to store a pair of plugin_type/plugin_name
79
  while (module_iter != module_registry_.end())
1324.2.3 by Monty Taylor
Remove plugin deinit.
80
  {
81
    delete (*module_iter).second;
82
    ++module_iter;
83
  }
2068.2.1 by Monty Taylor
Changed the plugin_registry to store a pair of plugin_type/plugin_name
84
  module_registry_.clear();
1966.2.6 by Brian Aker
This is from the catalog patch (I'm pushing it up as its own little thing
85
#endif
2068.2.1 by Monty Taylor
Changed the plugin_registry to store a pair of plugin_type/plugin_name
86
  LibraryMap::iterator library_iter= library_registry_.begin();
87
  while (library_iter != library_registry_.end())
1324.2.3 by Monty Taylor
Remove plugin deinit.
88
  {
89
    delete (*library_iter).second;
90
    ++library_iter;
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
91
  }
2068.2.1 by Monty Taylor
Changed the plugin_registry to store a pair of plugin_type/plugin_name
92
  library_registry_.clear();
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
93
}
94
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
95
void module::Registry::shutdown()
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
96
{
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
97
  module::Registry& registry= singleton();
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
98
  delete &registry;
99
}
100
1966.2.6 by Brian Aker
This is from the catalog patch (I'm pushing it up as its own little thing
101
module::Module *module::Registry::find(std::string name)
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
102
{
1966.2.6 by Brian Aker
This is from the catalog patch (I'm pushing it up as its own little thing
103
  std::transform(name.begin(), name.end(), name.begin(), ::tolower);
908.2.1 by Monty Taylor
First pass at refactoring plugins - factored out sql_map.
104
2068.2.1 by Monty Taylor
Changed the plugin_registry to store a pair of plugin_type/plugin_name
105
  ModuleMap::iterator map_iter;
106
  map_iter= module_registry_.find(name);
107
  if (map_iter != module_registry_.end())
971.1.54 by Monty Taylor
Trimmed out some plugin type stuff.
108
    return (*map_iter).second;
2095.3.1 by Monty Taylor
Re-purpose the old plugin sysvar slot in the struct to be a depends list.
109
  return NULL;
908.2.1 by Monty Taylor
First pass at refactoring plugins - factored out sql_map.
110
}
111
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
112
void module::Registry::add(module::Module *handle)
908.2.1 by Monty Taylor
First pass at refactoring plugins - factored out sql_map.
113
{
1966.2.6 by Brian Aker
This is from the catalog patch (I'm pushing it up as its own little thing
114
  std::string add_str(handle->getName());
908.2.1 by Monty Taylor
First pass at refactoring plugins - factored out sql_map.
115
  transform(add_str.begin(), add_str.end(),
116
            add_str.begin(), ::tolower);
117
2068.2.1 by Monty Taylor
Changed the plugin_registry to store a pair of plugin_type/plugin_name
118
  module_registry_[add_str]= handle;
2095.3.3 by Monty Taylor
Insert module into the vertex list.
119
2095.3.4 by Monty Taylor
Split out module vertex and then actually used it.
120
  Vertex vertex_info(add_str, handle);
2095.3.7 by Monty Taylor
Fixed the dependency graph so that it doesn't get included in sql_yacc.yy.
121
  VertexDesc handle_vertex= boost::add_vertex(depend_graph_->getGraph());
122
  depend_graph_->properties(handle_vertex)= vertex_info;
2095.3.5 by Monty Taylor
Use the catalog registry.
123
2095.3.7 by Monty Taylor
Fixed the dependency graph so that it doesn't get included in sql_yacc.yy.
124
  handle->setVertexHandle(new VertexHandle(handle_vertex));
2095.3.5 by Monty Taylor
Use the catalog registry.
125
1130.2.2 by Monty Taylor
Added support for the global list of plugin::Plugin objects to slot::Function
126
}
127
1702.4.1 by LinuxJedi
Remove module pointer from registry when module doesn't load to avoid a double-free on shutdown.
128
void module::Registry::remove(module::Module *handle)
129
{
1966.2.6 by Brian Aker
This is from the catalog patch (I'm pushing it up as its own little thing
130
  std::string remove_str(handle->getName());
131
  std::transform(remove_str.begin(), remove_str.end(),
132
                 remove_str.begin(), ::tolower);
1702.4.1 by LinuxJedi
Remove module pointer from registry when module doesn't load to avoid a double-free on shutdown.
133
2068.2.1 by Monty Taylor
Changed the plugin_registry to store a pair of plugin_type/plugin_name
134
  module_registry_.erase(remove_str);
1702.4.1 by LinuxJedi
Remove module pointer from registry when module doesn't load to avoid a double-free on shutdown.
135
}
908.2.1 by Monty Taylor
First pass at refactoring plugins - factored out sql_map.
136
1966.2.6 by Brian Aker
This is from the catalog patch (I'm pushing it up as its own little thing
137
void module::Registry::copy(plugin::Plugin::vector &arg)
138
{    
139
  arg.reserve(plugin_registry.size());
140
141
  std::transform(plugin_registry.begin(),
142
                 plugin_registry.end(),
143
                 std::back_inserter(arg),
144
                 boost::bind(&plugin::Plugin::map::value_type::second, _1) );
145
  assert(arg.size() == plugin_registry.size());
146
}
147
2095.3.6 by Monty Taylor
Actually properly process module dependencies. w00t.
148
void module::Registry::buildDeps()
149
{
150
  ModuleMap::iterator map_iter= module_registry_.begin();
151
  while (map_iter != module_registry_.end())
152
  {
153
    Module *handle= (*map_iter).second;
154
    Module::Depends::const_iterator handle_deps= handle->getDepends().begin();
155
    while (handle_deps != handle->getDepends().end())
156
    {
157
      std::string dep_str((*handle_deps));
158
      transform(dep_str.begin(), dep_str.end(),
159
                dep_str.begin(), ::tolower);
160
161
      bool found_dep= false;
2095.3.7 by Monty Taylor
Fixed the dependency graph so that it doesn't get included in sql_yacc.yy.
162
      vertex_iter it= boost::vertices(depend_graph_->getGraph()).first;
163
      while (it != vertices(depend_graph_->getGraph()).second)
2095.3.6 by Monty Taylor
Actually properly process module dependencies. w00t.
164
      {
2095.3.7 by Monty Taylor
Fixed the dependency graph so that it doesn't get included in sql_yacc.yy.
165
        if (depend_graph_->properties(*it).getName() == dep_str)
2095.3.6 by Monty Taylor
Actually properly process module dependencies. w00t.
166
        {
167
          found_dep= true;
2095.3.7 by Monty Taylor
Fixed the dependency graph so that it doesn't get included in sql_yacc.yy.
168
          add_edge(handle->getVertexHandle()->getVertexDesc(), *it, depend_graph_->getGraph());
2095.3.6 by Monty Taylor
Actually properly process module dependencies. w00t.
169
          break;
170
        }
171
        ++it;
172
      }
173
      if (not found_dep)
174
      {
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
175
        errmsg_printf(error::ERROR,
2095.3.6 by Monty Taylor
Actually properly process module dependencies. w00t.
176
                      _("Couldn't process plugin module dependencies. "
177
                        "%s depends on %s but %s is not to be loaded.\n"),
178
                      handle->getName().c_str(),
179
                      dep_str.c_str(), dep_str.c_str());
180
        DRIZZLE_ABORT;
181
      }
182
183
      ++handle_deps;
184
    }
185
    ++map_iter;
186
  }
187
  deps_built_= true;
188
}
189
2095.3.5 by Monty Taylor
Use the catalog registry.
190
module::Registry::ModuleList module::Registry::getList()
908.2.1 by Monty Taylor
First pass at refactoring plugins - factored out sql_map.
191
{
2095.3.6 by Monty Taylor
Actually properly process module dependencies. w00t.
192
  if (not deps_built_)
193
  {
194
    buildDeps();
195
  }
196
1966.2.6 by Brian Aker
This is from the catalog patch (I'm pushing it up as its own little thing
197
  std::vector<module::Module *> plugins;
2095.3.5 by Monty Taylor
Use the catalog registry.
198
199
  VertexList vertex_list;
200
2095.3.7 by Monty Taylor
Fixed the dependency graph so that it doesn't get included in sql_yacc.yy.
201
  boost::topological_sort(depend_graph_->getGraph(), std::back_inserter(vertex_list));
2095.3.5 by Monty Taylor
Use the catalog registry.
202
203
  for (VertexList::iterator i = vertex_list.begin();
204
       i != vertex_list.end(); ++i)
908.2.1 by Monty Taylor
First pass at refactoring plugins - factored out sql_map.
205
  {
2095.3.7 by Monty Taylor
Fixed the dependency graph so that it doesn't get included in sql_yacc.yy.
206
    Module *mod_ptr= depend_graph_->properties(*i).getModule();
2095.3.6 by Monty Taylor
Actually properly process module dependencies. w00t.
207
    if (mod_ptr != NULL)
2095.3.5 by Monty Taylor
Use the catalog registry.
208
    {
2095.3.6 by Monty Taylor
Actually properly process module dependencies. w00t.
209
      plugins.push_back(mod_ptr);
2095.3.5 by Monty Taylor
Use the catalog registry.
210
    }  
908.2.1 by Monty Taylor
First pass at refactoring plugins - factored out sql_map.
211
  }
971.1.54 by Monty Taylor
Trimmed out some plugin type stuff.
212
213
  return plugins;
908.2.1 by Monty Taylor
First pass at refactoring plugins - factored out sql_map.
214
}
971.1.46 by Monty Taylor
Made plugin registration go through Plugin_registry.
215
1966.2.6 by Brian Aker
This is from the catalog patch (I'm pushing it up as its own little thing
216
module::Library *module::Registry::addLibrary(const std::string &plugin_name,
1530.2.3 by Monty Taylor
Changed the builtin plugin code path to work exactly the same as dynamic.
217
                                              bool builtin)
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
218
{
219
220
  /* If this dll is already loaded just return it */
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
221
  module::Library *library= findLibrary(plugin_name);
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
222
  if (library != NULL)
223
  {
224
    return library;
225
  }
226
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
227
  library= module::Library::loadLibrary(plugin_name, builtin);
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
228
  if (library != NULL)
229
  {
230
    /* Add this dll to the map */
2068.2.1 by Monty Taylor
Changed the plugin_registry to store a pair of plugin_type/plugin_name
231
    library_registry_.insert(make_pair(plugin_name, library));
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
232
  }
233
234
  return library;
235
}
236
1966.2.6 by Brian Aker
This is from the catalog patch (I'm pushing it up as its own little thing
237
void module::Registry::removeLibrary(const std::string &plugin_name)
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
238
{
1966.2.6 by Brian Aker
This is from the catalog patch (I'm pushing it up as its own little thing
239
  std::map<std::string, module::Library *>::iterator iter=
2068.2.1 by Monty Taylor
Changed the plugin_registry to store a pair of plugin_type/plugin_name
240
    library_registry_.find(plugin_name);
241
  if (iter != library_registry_.end())
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
242
  {
2068.2.1 by Monty Taylor
Changed the plugin_registry to store a pair of plugin_type/plugin_name
243
    library_registry_.erase(iter);
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
244
    delete (*iter).second;
245
  }
246
}
247
1966.2.6 by Brian Aker
This is from the catalog patch (I'm pushing it up as its own little thing
248
module::Library *module::Registry::findLibrary(const std::string &plugin_name) const
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
249
{
2068.2.1 by Monty Taylor
Changed the plugin_registry to store a pair of plugin_type/plugin_name
250
  LibraryMap::const_iterator iter= library_registry_.find(plugin_name);
251
  if (iter != library_registry_.end())
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
252
    return (*iter).second;
253
  return NULL;
254
}
255
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
256
void module::Registry::shutdownModules()
257
{
258
  module_shutdown(*this);
259
}
260
1130.1.1 by Monty Taylor
Merged in plugin-slot-reorg patches.
261
} /* namespace drizzled */