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