Drizzled Public API Documentation

catalog.cc

00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2010 Brian Aker
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 #include <config.h>
00022 
00023 #include <drizzled/plugin/catalog.h>
00024 #include <drizzled/catalog/cache.h>
00025 #include <drizzled/catalog/local.h>
00026 #include <drizzled/error.h>
00027 
00028 #include <boost/foreach.hpp>
00029 
00030 namespace drizzled
00031 {
00032 namespace plugin
00033 {
00034 
00035 // Private container we use for holding the instances of engines passed to
00036 // use from the catalog plugins.
00037 class Engines {
00038   catalog::Engine::vector _catalogs;
00039 
00040 public:
00041   static Engines& singleton()
00042   {
00043     static Engines ptr;
00044     return ptr;
00045   }
00046 
00047   catalog::Engine::vector &catalogs()
00048   {
00049     return _catalogs;
00050   }
00051 };
00052 
00053 Catalog::~Catalog()
00054 {
00055 }
00056 
00057 bool Catalog::create(identifier::Catalog::const_reference identifier)
00058 {
00059   message::catalog::shared_ptr message= message::catalog::make_shared(identifier);
00060   return create(identifier, message);
00061 }
00062 
00063 bool Catalog::create(identifier::Catalog::const_reference identifier, message::catalog::shared_ptr &message)
00064 {
00065   assert(message);
00066 
00067   catalog::lock::Create lock(identifier);
00068 
00069   if (not lock.locked())
00070   {
00071     my_error(ER_CATALOG_NO_LOCK, MYF(0), identifier.getName().c_str());
00072     return false;
00073   }
00074 
00075   size_t create_count= 0;
00076   BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
00077   {
00078     if (ref->create(identifier, message))
00079       create_count++;
00080   }
00081   assert(create_count < 2);
00082 
00083   if (not create_count)
00084   {
00085     my_error(ER_CATALOG_CANNOT_CREATE, MYF(0), identifier.getName().c_str());
00086     return false;
00087   }
00088 
00089   return true;
00090 }
00091 
00092 bool Catalog::drop(identifier::Catalog::const_reference identifier)
00093 {
00094   if (identifier == drizzled::catalog::local_identifier())
00095   {
00096     my_error(drizzled::ER_CATALOG_NO_DROP_LOCAL, MYF(0));
00097     return false;
00098   }
00099 
00100   catalog::lock::Erase lock(identifier);
00101   if (not lock.locked())
00102   {
00103     my_error(ER_CATALOG_NO_LOCK, MYF(0), identifier.getName().c_str());
00104     return false; 
00105   }
00106 
00107   
00108   size_t drop_count= 0;
00109   BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
00110   {
00111     if (ref->drop(identifier))
00112       drop_count++;
00113   }
00114   assert(drop_count < 2);
00115 
00116   if (not drop_count)
00117   {
00118     my_error(ER_CATALOG_DOES_NOT_EXIST, MYF(0), identifier.getName().c_str());
00119     return false;
00120   }
00121 
00122   return true;
00123 }
00124 
00125 bool Catalog::lock(identifier::Catalog::const_reference identifier)
00126 {
00127   drizzled::error_t error;
00128   
00129   // We insert a lock into the cache, if this fails we bail.
00130   if (not catalog::Cache::singleton().lock(identifier, error))
00131   {
00132     my_error(error, identifier);
00133 
00134     return false;
00135   }
00136 
00137   return true;
00138 }
00139 
00140 
00141 bool Catalog::unlock(identifier::Catalog::const_reference identifier)
00142 {
00143   drizzled::error_t error;
00144   if (not catalog::Cache::singleton().unlock(identifier, error))
00145   {
00146     my_error(error, identifier);
00147   }
00148 
00149   return false;
00150 }
00151 
00152 bool plugin::Catalog::addPlugin(plugin::Catalog *arg)
00153 {
00154   Engines::singleton().catalogs().push_back(arg->engine());
00155 
00156   return false;
00157 }
00158 
00159 bool plugin::Catalog::exist(identifier::Catalog::const_reference identifier)
00160 {
00161   if (catalog::Cache::singleton().exist(identifier))
00162     return true;
00163 
00164   BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
00165   {
00166     if (ref->exist(identifier))
00167       return true;
00168   }
00169 
00170   return false;
00171 }
00172 
00173 void plugin::Catalog::getIdentifiers(identifier::Catalog::vector &identifiers)
00174 {
00175   BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
00176   {
00177     ref->getIdentifiers(identifiers);
00178   }
00179 }
00180 
00181 void plugin::Catalog::getMessages(message::catalog::vector &messages)
00182 {
00183   BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
00184   {
00185     ref->getMessages(messages);
00186   }
00187 }
00188 
00189 message::catalog::shared_ptr plugin::Catalog::getMessage(identifier::Catalog::const_reference identifier)
00190 {
00191   drizzled::error_t error;
00192   catalog::Instance::shared_ptr instance= catalog::Cache::singleton().find(identifier, error);
00193   message::catalog::shared_ptr message;
00194 
00195   if (instance and instance->message())
00196   {
00197     return instance->message();
00198   }
00199 
00200   BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
00201   {
00202     if ((message= ref->getMessage(identifier)))
00203       return message;
00204   }
00205 
00206   return message;
00207 }
00208 
00209 catalog::Instance::shared_ptr plugin::Catalog::getInstance(identifier::Catalog::const_reference identifier)
00210 {
00211   drizzled::error_t error;
00212   catalog::Instance::shared_ptr instance= catalog::Cache::singleton().find(identifier, error);
00213 
00214   if (instance)
00215     return instance;
00216 
00217   BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
00218   {
00219     message::catalog::shared_ptr message;
00220     if (message= ref->getMessage(identifier))
00221     {
00222       instance= catalog::Instance::make_shared(message);
00223       // If this should fail inserting into the cache, we are in a world of
00224       // pain.
00225       catalog::Cache::singleton().insert(identifier, instance, error);
00226 
00227       return instance;
00228     }
00229   }
00230 
00231   return catalog::Instance::shared_ptr();
00232 }
00233 
00234 
00235 void plugin::Catalog::removePlugin(plugin::Catalog *)
00236 {
00237 }
00238 
00239 } /* namespace plugin */
00240 } /* namespace drizzled */