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 bool Catalog::create(const identifier::Catalog& identifier)
00054 {
00055   message::catalog::shared_ptr message= message::catalog::make_shared(identifier);
00056   return create(identifier, message);
00057 }
00058 
00059 bool Catalog::create(const identifier::Catalog& identifier, message::catalog::shared_ptr &message)
00060 {
00061   assert(message);
00062 
00063   catalog::lock::Create lock(identifier);
00064 
00065   if (not lock.locked())
00066   {
00067     my_error(ER_CATALOG_NO_LOCK, MYF(0), identifier.getName().c_str());
00068     return false;
00069   }
00070 
00071   size_t create_count= 0;
00072   BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
00073   {
00074     if (ref->create(identifier, message))
00075       create_count++;
00076   }
00077   assert(create_count < 2);
00078 
00079   if (not create_count)
00080   {
00081     my_error(ER_CATALOG_CANNOT_CREATE, MYF(0), identifier.getName().c_str());
00082     return false;
00083   }
00084 
00085   return true;
00086 }
00087 
00088 bool Catalog::drop(const identifier::Catalog& identifier)
00089 {
00090   if (identifier == drizzled::catalog::local_identifier())
00091   {
00092     my_error(drizzled::ER_CATALOG_NO_DROP_LOCAL, MYF(0));
00093     return false;
00094   }
00095 
00096   catalog::lock::Erase lock(identifier);
00097   if (not lock.locked())
00098   {
00099     my_error(ER_CATALOG_NO_LOCK, MYF(0), identifier.getName().c_str());
00100     return false; 
00101   }
00102 
00103   
00104   size_t drop_count= 0;
00105   BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
00106   {
00107     if (ref->drop(identifier))
00108       drop_count++;
00109   }
00110   assert(drop_count < 2);
00111 
00112   if (not drop_count)
00113   {
00114     my_error(ER_CATALOG_DOES_NOT_EXIST, MYF(0), identifier.getName().c_str());
00115     return false;
00116   }
00117 
00118   return true;
00119 }
00120 
00121 bool Catalog::lock(const identifier::Catalog& identifier)
00122 {
00123   drizzled::error_t error;
00124   
00125   // We insert a lock into the cache, if this fails we bail.
00126   if (not catalog::Cache::lock(identifier, error))
00127   {
00128     my_error(error, identifier);
00129 
00130     return false;
00131   }
00132 
00133   return true;
00134 }
00135 
00136 
00137 bool Catalog::unlock(const identifier::Catalog& identifier)
00138 {
00139   drizzled::error_t error;
00140   if (not catalog::Cache::unlock(identifier, error))
00141   {
00142     my_error(error, identifier);
00143   }
00144 
00145   return false;
00146 }
00147 
00148 bool plugin::Catalog::addPlugin(plugin::Catalog *arg)
00149 {
00150   Engines::singleton().catalogs().push_back(arg->engine());
00151 
00152   return false;
00153 }
00154 
00155 bool plugin::Catalog::exist(const identifier::Catalog& identifier)
00156 {
00157   if (catalog::Cache::exist(identifier))
00158     return true;
00159 
00160   BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
00161   {
00162     if (ref->exist(identifier))
00163       return true;
00164   }
00165 
00166   return false;
00167 }
00168 
00169 void plugin::Catalog::getIdentifiers(identifier::catalog::vector &identifiers)
00170 {
00171   BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
00172   {
00173     ref->getIdentifiers(identifiers);
00174   }
00175 }
00176 
00177 void plugin::Catalog::getMessages(message::catalog::vector &messages)
00178 {
00179   BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
00180   {
00181     ref->getMessages(messages);
00182   }
00183 }
00184 
00185 message::catalog::shared_ptr plugin::Catalog::getMessage(const identifier::Catalog& identifier)
00186 {
00187   drizzled::error_t error;
00188   catalog::Instance::shared_ptr instance= catalog::Cache::find(identifier, error);
00189   message::catalog::shared_ptr message;
00190 
00191   if (instance and instance->message())
00192   {
00193     return instance->message();
00194   }
00195 
00196   BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
00197   {
00198     if ((message= ref->getMessage(identifier)))
00199       return message;
00200   }
00201 
00202   return message;
00203 }
00204 
00205 catalog::Instance::shared_ptr plugin::Catalog::getInstance(const identifier::Catalog& identifier)
00206 {
00207   drizzled::error_t error;
00208   catalog::Instance::shared_ptr instance= catalog::Cache::find(identifier, error);
00209 
00210   if (instance)
00211     return instance;
00212 
00213   BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
00214   {
00215     message::catalog::shared_ptr message;
00216     if (message= ref->getMessage(identifier))
00217     {
00218       instance= catalog::Instance::make_shared(message);
00219       // If this should fail inserting into the cache, we are in a world of
00220       // pain.
00221       catalog::Cache::insert(identifier, instance, error);
00222 
00223       return instance;
00224     }
00225   }
00226 
00227   return catalog::Instance::shared_ptr();
00228 }
00229 
00230 
00231 void plugin::Catalog::removePlugin(plugin::Catalog *)
00232 {
00233 }
00234 
00235 } /* namespace plugin */
00236 } /* namespace drizzled */