Drizzled Public API Documentation

cache.h
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 #pragma once
00022 
00023 #include <boost/bind.hpp>
00024 
00025 #include <drizzled/identifier.h>
00026 #include <drizzled/error.h>
00027 #include <drizzled/catalog.h>
00028 #include <drizzled/plugin/catalog.h>
00029 
00030 #include <boost/unordered_map.hpp>
00031 #include <boost/thread/mutex.hpp>
00032 
00033 namespace drizzled {
00034 namespace catalog  {
00035 
00036 class Cache
00037 {
00038 public:
00039   static size_t size()
00040   {
00041     return cache.size();
00042   }
00043 
00044   static Instance::shared_ptr find(const identifier::Catalog&, error_t&);
00045   static bool exist(const identifier::Catalog&);
00046   static bool erase(const identifier::Catalog&, error_t&);
00047   static bool insert(const identifier::Catalog&, Instance::shared_ptr, error_t&);
00048   static bool lock(const identifier::Catalog&, error_t&);
00049   static bool unlock(const identifier::Catalog&, error_t&);
00050   static void copy(catalog::Instance::vector&);
00051 
00052   typedef boost::unordered_map<identifier::Catalog, catalog::Instance::shared_ptr> unordered_map;
00053 
00054   static unordered_map cache;
00055   static boost::mutex _mutex;
00056 };
00057 
00058 
00059 namespace lock {
00060 
00061 class Erase
00062 {
00063   bool _locked;
00064   const identifier::Catalog &identifier;
00065   error_t error;
00066 
00067 public:
00068   Erase(const identifier::Catalog &identifier_arg) :
00069     _locked(false),
00070     identifier(identifier_arg)
00071   {
00072     init();
00073   }
00074 
00075   bool locked () const
00076   {
00077     return _locked;
00078   }
00079 
00080   ~Erase()
00081   {
00082     if (_locked)
00083     {
00084       if (not catalog::Cache::unlock(identifier, error))
00085       {
00086         my_error(error, identifier);
00087         assert(0);
00088       }
00089     }
00090   }
00091 
00092 private:
00093   void init()
00094   {
00095     // We insert a lock into the cache, if this fails we bail.
00096     if (not catalog::Cache::lock(identifier, error))
00097     {
00098       assert(0);
00099       return;
00100     }
00101 
00102     _locked= true;
00103   }
00104 };
00105 
00106 
00107 class Create
00108 {
00109   bool _locked;
00110   const identifier::Catalog &identifier;
00111   error_t error;
00112 
00113 public:
00114   Create(const identifier::Catalog &identifier_arg) :
00115     _locked(false),
00116     identifier(identifier_arg)
00117   {
00118     init();
00119   }
00120 
00121   bool locked () const
00122   {
00123     return _locked;
00124   }
00125 
00126   ~Create()
00127   {
00128     if (_locked)
00129     {
00130       if (not catalog::Cache::unlock(identifier, error))
00131       {
00132         my_error(error, identifier);
00133         assert(0);
00134       }
00135     }
00136   }
00137 
00138 
00139 private:
00140   void init()
00141   {
00142     // We insert a lock into the cache, if this fails we bail.
00143     if (not catalog::Cache::lock(identifier, error))
00144     {
00145       my_error(error, identifier);
00146       return;
00147     }
00148 
00149     _locked= true;
00150   }
00151 };
00152 
00153 } /* namespace lock */
00154 } /* namespace catalog */
00155 } /* namespace drizzled */
00156