drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
drizzled/plugin/catalog.cc
1/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2010 Brian Aker
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; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <config.h>
22
23#include <drizzled/plugin/catalog.h>
24#include <drizzled/catalog/cache.h>
25#include <drizzled/catalog/local.h>
26#include <drizzled/error.h>
27
28namespace drizzled
29{
30namespace plugin
31{
32
33// Private container we use for holding the instances of engines passed to
34// use from the catalog plugins.
35class Engines {
36 catalog::Engine::vector _catalogs;
37
38public:
39 static Engines& singleton()
40 {
41 static Engines ptr;
42 return ptr;
43 }
44
45 catalog::Engine::vector &catalogs()
46 {
47 return _catalogs;
48 }
49};
50
51bool Catalog::create(const identifier::Catalog& identifier)
52{
53 message::catalog::shared_ptr message= message::catalog::make_shared(identifier);
54 return create(identifier, message);
55}
56
57bool Catalog::create(const identifier::Catalog& identifier, message::catalog::shared_ptr &message)
58{
59 assert(message);
60
61 catalog::lock::Create lock(identifier);
62
63 if (not lock.locked())
64 {
65 my_error(ER_CATALOG_NO_LOCK, MYF(0), identifier.getName().c_str());
66 return false;
67 }
68
69 size_t create_count= 0;
70 for (catalog::Engine::vector::const_reference ref : Engines::singleton().catalogs())
71 {
72 if (ref->create(identifier, message))
73 create_count++;
74 }
75 assert(create_count < 2);
76
77 if (not create_count)
78 {
79 my_error(ER_CATALOG_CANNOT_CREATE, MYF(0), identifier.getName().c_str());
80 return false;
81 }
82
83 return true;
84}
85
86bool Catalog::drop(const identifier::Catalog& identifier)
87{
88 if (identifier == drizzled::catalog::local_identifier())
89 {
90 my_error(drizzled::ER_CATALOG_NO_DROP_LOCAL, MYF(0));
91 return false;
92 }
93
94 catalog::lock::Erase lock(identifier);
95 if (not lock.locked())
96 {
97 my_error(ER_CATALOG_NO_LOCK, MYF(0), identifier.getName().c_str());
98 return false;
99 }
100
101
102 size_t drop_count= 0;
103 for (catalog::Engine::vector::const_reference ref : Engines::singleton().catalogs())
104 {
105 if (ref->drop(identifier))
106 {
107 drop_count++;
108 }
109 }
110 assert(drop_count < 2);
111
112 if (not drop_count)
113 {
114 my_error(ER_CATALOG_DOES_NOT_EXIST, MYF(0), identifier.getName().c_str());
115 return false;
116 }
117
118 return true;
119}
120
121bool Catalog::lock(const identifier::Catalog& identifier)
122{
123 drizzled::error_t error;
124
125 // We insert a lock into the cache, if this fails we bail.
126 if (not catalog::Cache::lock(identifier, error))
127 {
128 my_error(error, identifier);
129
130 return false;
131 }
132
133 return true;
134}
135
136
137bool Catalog::unlock(const identifier::Catalog& identifier)
138{
139 drizzled::error_t error;
140 if (not catalog::Cache::unlock(identifier, error))
141 {
142 my_error(error, identifier);
143 }
144
145 return false;
146}
147
148bool plugin::Catalog::addPlugin(plugin::Catalog &arg)
149{
150 Engines::singleton().catalogs().push_back(arg.engine());
151
152 return false;
153}
154
155bool plugin::Catalog::exist(const identifier::Catalog& identifier)
156{
157 if (catalog::Cache::exist(identifier))
158 {
159 return true;
160 }
161
162 for (catalog::Engine::vector::const_reference ref : Engines::singleton().catalogs())
163 {
164 if (ref->exist(identifier))
165 {
166 return true;
167 }
168 }
169
170 return false;
171}
172
173void plugin::Catalog::getIdentifiers(identifier::catalog::vector &identifiers)
174{
175 for (catalog::Engine::vector::const_reference ref : Engines::singleton().catalogs())
176 {
177 ref->getIdentifiers(identifiers);
178 }
179}
180
181void plugin::Catalog::getMessages(message::catalog::vector &messages)
182{
183 for (catalog::Engine::vector::const_reference ref : Engines::singleton().catalogs())
184 {
185 ref->getMessages(messages);
186 }
187}
188
189message::catalog::shared_ptr plugin::Catalog::getMessage(const identifier::Catalog& identifier)
190{
191 drizzled::error_t error;
192 catalog::Instance::shared_ptr instance= catalog::Cache::find(identifier, error);
193 message::catalog::shared_ptr message;
194
195 if (instance and instance->message())
196 {
197 return instance->message();
198 }
199
200 for (catalog::Engine::vector::const_reference ref : Engines::singleton().catalogs())
201 {
202 if ((message= ref->getMessage(identifier)))
203 {
204 return message;
205 }
206 }
207
208 return message;
209}
210
211catalog::Instance::shared_ptr plugin::Catalog::getInstance(const identifier::Catalog& identifier)
212{
213 drizzled::error_t error;
214 catalog::Instance::shared_ptr instance= catalog::Cache::find(identifier, error);
215
216 if (instance)
217 {
218 return instance;
219 }
220
221 for (catalog::Engine::vector::const_reference ref : Engines::singleton().catalogs())
222 {
223 message::catalog::shared_ptr message;
224 if ((message= ref->getMessage(identifier)))
225 {
226 instance= catalog::Instance::make_shared(message);
227 // If this should fail inserting into the cache, we are in a world of
228 // pain.
229 catalog::Cache::insert(identifier, instance, error);
230
231 return instance;
232 }
233 }
234
235 return catalog::Instance::shared_ptr();
236}
237
238
239void plugin::Catalog::removePlugin(plugin::Catalog &)
240{
241}
242
243} /* namespace plugin */
244} /* namespace drizzled */
TODO: Rename this file - func.h is stupid.