Drizzled Public API Documentation

cache.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/catalog/cache.h>
00024 #include <drizzled/util/find_ptr.h>
00025 
00026 namespace drizzled {
00027 namespace catalog {
00028 
00029 Instance::shared_ptr Cache::find(const identifier::Catalog &identifier, drizzled::error_t &error)
00030 {
00031   boost::mutex::scoped_lock scopedLock(_mutex);
00032   if (const unordered_map::mapped_type* ptr= find_ptr(cache, identifier))
00033   {
00034     error= *ptr ? EE_OK : ER_CATALOG_NO_LOCK;
00035     return *ptr;
00036   }
00037   error= ER_CATALOG_DOES_NOT_EXIST;
00038   return catalog::Instance::shared_ptr();
00039 }
00040 
00041 bool Cache::exist(const identifier::Catalog &identifier)
00042 {
00043   boost::mutex::scoped_lock scopedLock(_mutex);
00044   return find_ptr(cache, identifier);
00045 }
00046 
00047 bool Cache::erase(const identifier::Catalog &identifier, drizzled::error_t &error)
00048 {
00049   boost::mutex::scoped_lock scopedLock(_mutex);
00050   if (find_ptr(cache, identifier))
00051   {
00052     if (cache.erase(identifier))
00053       return true;
00054     assert(false); // This should be imposssible
00055   }
00056   error= ER_CATALOG_DOES_NOT_EXIST;
00057   return false;
00058 }
00059 
00060 bool Cache::unlock(const identifier::Catalog &identifier, drizzled::error_t &error)
00061 {
00062   boost::mutex::scoped_lock scopedLock(_mutex);
00063   if (const unordered_map::mapped_type* ptr= find_ptr(cache, identifier))
00064   {
00065     if (not *ptr)
00066     {
00067       if (cache.erase(identifier))
00068         return true;
00069       assert(false); // This should be imposssible
00070     }
00071     error= EE_OK;
00072   }
00073   else
00074   {
00075     error= ER_CATALOG_DOES_NOT_EXIST;
00076   }
00077   return false;
00078 }
00079 
00080 bool Cache::lock(const identifier::Catalog &identifier, drizzled::error_t &error)
00081 {
00082   boost::mutex::scoped_lock scopedLock(_mutex);
00083   std::pair<unordered_map::iterator, bool> ret= cache.insert(std::make_pair(identifier, catalog::Instance::shared_ptr()));
00084 
00085   if (ret.second == false)
00086   {
00087     if (ret.first->second)
00088     {
00089       error= EE_OK;
00090     }
00091     else
00092     {
00093       error= ER_CATALOG_NO_LOCK;
00094     }
00095   }
00096 
00097   return ret.second;
00098 }
00099 
00100 bool Cache::insert(const identifier::Catalog &identifier, catalog::Instance::shared_ptr instance, drizzled::error_t &error)
00101 {
00102   boost::mutex::scoped_lock scopedLock(_mutex);
00103   std::pair<unordered_map::iterator, bool> ret= cache.insert(std::make_pair(identifier, instance));
00104 
00105   if (ret.second == false)
00106   {
00107     if (ret.first->second)
00108     {
00109       error= EE_OK;
00110     }
00111     else
00112     {
00113       error= ER_CATALOG_NO_LOCK;
00114     }
00115   }
00116 
00117   return ret.second;
00118 }
00119 
00120 void Cache::copy(catalog::Instance::vector &vector)
00121 {
00122   boost::mutex::scoped_lock scopedLock(_mutex);
00123 
00124   vector.reserve(catalog::Cache::singleton().size());
00125 
00126   std::transform(cache.begin(),
00127                  cache.end(),
00128                  std::back_inserter(vector),
00129                  boost::bind(&unordered_map::value_type::second, _1) );
00130   assert(vector.size() == cache.size());
00131 }
00132 
00133 
00134 } /* namespace catalog */
00135 } /* namespace drizzled */