drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
registry.cc
1/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2008 Sun Microsystems, Inc.
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
20#include <config.h>
21
22#include <string>
23#include <vector>
24#include <map>
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#include <drizzled/util/find_ptr.h>
36
37#include <boost/bind/bind.hpp>
38
39using namespace std;
40
41namespace drizzled {
42
43module::Registry::Registry() :
44 module_registry_(),
45 depend_graph_(new module::Graph()),
46 plugin_registry(),
47 deps_built_(false)
48{ }
49
50
51module::Registry::~Registry()
52{
53 /* Give all plugins a chance to cleanup, before
54 * all plugins are deleted.
55 * This can be used if shutdown code references
56 * other plugins.
57 */
58 for (plugin::Plugin::map::reference it : plugin_registry)
59 it.second->shutdownPlugin();
60
61 /* Destroy non-removeLast plugins first, removeLast (ErrorMessage) strictly
62 * last. Same stable partition as before, expressed via unique_ptr rather
63 * than manual delete. Each erase/destruct dispatches the entry's own
64 * deleter (C++ delete now, Rust free-fn later) with no type check.
65 */
66 std::vector<plugin::Plugin::owned_ptr> error_plugins;
67 for (auto it= plugin_registry.begin(); it != plugin_registry.end(); )
68 {
69 if (it->second->removeLast())
70 {
71 error_plugins.push_back(std::move(it->second));
72 it= plugin_registry.erase(it);
73 }
74 else
75 {
76 it= plugin_registry.erase(it);
77 }
78 }
79
80 /* ErrorMessage handlers destruct here, strictly after all others. */
81 error_plugins.clear();
82
83#if 0
84 /*
85 @TODO When we delete modules here, we segfault on a bad string. Why?
86 */
87
88 for (ModuleMap::reference it : module_registry_)
89 delete it.second;
90 module_registry_.clear();
91#endif
92 for (LibraryMap::reference it : library_registry_)
93 delete it.second;
94 library_registry_.clear();
95}
96
97module::Module* module::Registry::find(const std::string& name)
98{
99 return find_ptr2(module_registry_, boost::to_lower_copy(name));
100}
101
102void module::Registry::add(module::Module *handle)
103{
104 std::string add_str(boost::to_lower_copy(handle->getName()));
105
106 module_registry_[add_str]= handle;
107
108 Vertex vertex_info(add_str, handle);
109 VertexDesc handle_vertex= boost::add_vertex(depend_graph_->getGraph());
110 depend_graph_->properties(handle_vertex)= vertex_info;
111
112 handle->setVertexHandle(new VertexHandle(handle_vertex));
113}
114
115void module::Registry::remove(module::Module *handle)
116{
117 module_registry_.erase(boost::to_lower_copy(handle->getName()));
118}
119
120void module::Registry::buildDeps()
121{
122 for (ModuleMap::reference map_iter : module_registry_)
123 {
124 Module* handle= map_iter.second;
125 for (Module::Depends::const_reference handle_deps : handle->getDepends())
126 {
127 std::string dep_str(boost::to_lower_copy(handle_deps));
128 bool found_dep= false;
129 for (vertex_iter it= boost::vertices(depend_graph_->getGraph()).first; it != vertices(depend_graph_->getGraph()).second; it++)
130 {
131 if (depend_graph_->properties(*it).getName() == dep_str)
132 {
133 found_dep= true;
134 add_edge(handle->getVertexHandle()->getVertexDesc(), *it, depend_graph_->getGraph());
135 break;
136 }
137 }
138 if (not found_dep)
139 {
140 errmsg_printf(error::ERROR, _("Couldn't process plugin module dependencies. %s depends on %s but %s is not to be loaded.\n"),
141 handle->getName().c_str(), dep_str.c_str(), dep_str.c_str());
142 DRIZZLE_ABORT;
143 }
144 }
145 }
146 deps_built_= true;
147}
148
149module::Registry::ModuleList module::Registry::getList()
150{
151 if (not deps_built_)
152 buildDeps();
153 VertexList vertex_list;
154 boost::topological_sort(depend_graph_->getGraph(), std::back_inserter(vertex_list));
155 ModuleList plugins;
156 for (VertexList::reference it : vertex_list)
157 {
158 if (Module* mod_ptr= depend_graph_->properties(it).getModule())
159 plugins.push_back(mod_ptr);
160 }
161 return plugins;
162}
163
164module::Library *module::Registry::addLibrary(const std::string &plugin_name, bool builtin)
165{
166 /* If this dll is already loaded just return it */
167 module::Library *library= findLibrary(plugin_name);
168 if (library)
169 return library;
170
171 library= module::Library::loadLibrary(plugin_name, builtin);
172 if (library)
173 {
174 /* Add this dll to the map */
175 library_registry_.insert(make_pair(plugin_name, library));
176 }
177 return library;
178}
179
180void module::Registry::removeLibrary(const std::string &plugin_name)
181{
182 LibraryMap::iterator iter= library_registry_.find(plugin_name);
183 if (iter != library_registry_.end())
184 {
185 delete iter->second;
186 library_registry_.erase(iter);
187 }
188}
189
190module::Library *module::Registry::findLibrary(const std::string &plugin_name) const
191{
192 return find_ptr2(library_registry_, plugin_name);
193}
194
195void module::Registry::shutdownModules()
196{
197 module_shutdown(*this);
198}
199
200} /* namespace drizzled */
TODO: Rename this file - func.h is stupid.
STL namespace.