Drizzled Public API Documentation

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 
28 #include <boost/foreach.hpp>
29 
30 namespace drizzled
31 {
32 namespace plugin
33 {
34 
35 // Private container we use for holding the instances of engines passed to
36 // use from the catalog plugins.
37 class Engines {
38  catalog::Engine::vector _catalogs;
39 
40 public:
41  static Engines& singleton()
42  {
43  static Engines ptr;
44  return ptr;
45  }
46 
47  catalog::Engine::vector &catalogs()
48  {
49  return _catalogs;
50  }
51 };
52 
53 bool Catalog::create(const identifier::Catalog& identifier)
54 {
55  message::catalog::shared_ptr message= message::catalog::make_shared(identifier);
56  return create(identifier, message);
57 }
58 
59 bool Catalog::create(const identifier::Catalog& identifier, message::catalog::shared_ptr &message)
60 {
61  assert(message);
62 
63  catalog::lock::Create lock(identifier);
64 
65  if (not lock.locked())
66  {
67  my_error(ER_CATALOG_NO_LOCK, MYF(0), identifier.getName().c_str());
68  return false;
69  }
70 
71  size_t create_count= 0;
72  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
73  {
74  if (ref->create(identifier, message))
75  create_count++;
76  }
77  assert(create_count < 2);
78 
79  if (not create_count)
80  {
81  my_error(ER_CATALOG_CANNOT_CREATE, MYF(0), identifier.getName().c_str());
82  return false;
83  }
84 
85  return true;
86 }
87 
88 bool Catalog::drop(const identifier::Catalog& identifier)
89 {
90  if (identifier == drizzled::catalog::local_identifier())
91  {
92  my_error(drizzled::ER_CATALOG_NO_DROP_LOCAL, MYF(0));
93  return false;
94  }
95 
96  catalog::lock::Erase lock(identifier);
97  if (not lock.locked())
98  {
99  my_error(ER_CATALOG_NO_LOCK, MYF(0), identifier.getName().c_str());
100  return false;
101  }
102 
103 
104  size_t drop_count= 0;
105  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
106  {
107  if (ref->drop(identifier))
108  {
109  drop_count++;
110  }
111  }
112  assert(drop_count < 2);
113 
114  if (not drop_count)
115  {
116  my_error(ER_CATALOG_DOES_NOT_EXIST, MYF(0), identifier.getName().c_str());
117  return false;
118  }
119 
120  return true;
121 }
122 
123 bool Catalog::lock(const identifier::Catalog& identifier)
124 {
125  drizzled::error_t error;
126 
127  // We insert a lock into the cache, if this fails we bail.
128  if (not catalog::Cache::lock(identifier, error))
129  {
130  my_error(error, identifier);
131 
132  return false;
133  }
134 
135  return true;
136 }
137 
138 
139 bool Catalog::unlock(const identifier::Catalog& identifier)
140 {
141  drizzled::error_t error;
142  if (not catalog::Cache::unlock(identifier, error))
143  {
144  my_error(error, identifier);
145  }
146 
147  return false;
148 }
149 
150 bool plugin::Catalog::addPlugin(plugin::Catalog *arg)
151 {
152  Engines::singleton().catalogs().push_back(arg->engine());
153 
154  return false;
155 }
156 
157 bool plugin::Catalog::exist(const identifier::Catalog& identifier)
158 {
159  if (catalog::Cache::exist(identifier))
160  {
161  return true;
162  }
163 
164  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
165  {
166  if (ref->exist(identifier))
167  {
168  return true;
169  }
170  }
171 
172  return false;
173 }
174 
175 void plugin::Catalog::getIdentifiers(identifier::catalog::vector &identifiers)
176 {
177  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
178  {
179  ref->getIdentifiers(identifiers);
180  }
181 }
182 
183 void plugin::Catalog::getMessages(message::catalog::vector &messages)
184 {
185  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
186  {
187  ref->getMessages(messages);
188  }
189 }
190 
191 message::catalog::shared_ptr plugin::Catalog::getMessage(const identifier::Catalog& identifier)
192 {
193  drizzled::error_t error;
194  catalog::Instance::shared_ptr instance= catalog::Cache::find(identifier, error);
195  message::catalog::shared_ptr message;
196 
197  if (instance and instance->message())
198  {
199  return instance->message();
200  }
201 
202  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
203  {
204  if ((message= ref->getMessage(identifier)))
205  {
206  return message;
207  }
208  }
209 
210  return message;
211 }
212 
213 catalog::Instance::shared_ptr plugin::Catalog::getInstance(const identifier::Catalog& identifier)
214 {
215  drizzled::error_t error;
216  catalog::Instance::shared_ptr instance= catalog::Cache::find(identifier, error);
217 
218  if (instance)
219  {
220  return instance;
221  }
222 
223  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
224  {
225  message::catalog::shared_ptr message;
226  if (message= ref->getMessage(identifier))
227  {
228  instance= catalog::Instance::make_shared(message);
229  // If this should fail inserting into the cache, we are in a world of
230  // pain.
231  catalog::Cache::insert(identifier, instance, error);
232 
233  return instance;
234  }
235  }
236 
237  return catalog::Instance::shared_ptr();
238 }
239 
240 
241 void plugin::Catalog::removePlugin(plugin::Catalog *)
242 {
243 }
244 
245 } /* namespace plugin */
246 } /* namespace drizzled */
TODO: Rename this file - func.h is stupid.
Definition: engine.cc:41