00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
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 }
00154 }
00155 }
00156