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/error.h>
00026 #include <drizzled/catalog.h>
00027 #include <drizzled/plugin/catalog.h>
00028
00029 #include <boost/unordered_map.hpp>
00030 #include <boost/thread/mutex.hpp>
00031
00032 namespace drizzled {
00033
00034 namespace plugin {
00035 class Catalog;
00036 }
00037 namespace generator {
00038 namespace catalog {
00039 class Cache;
00040 class Instance;
00041
00042 }
00043 }
00044
00045 namespace catalog {
00046
00047 namespace lock {
00048 class Create;
00049 class Erase;
00050
00051 }
00052
00053 class Cache
00054 {
00055 static inline Cache &singleton()
00056 {
00057 static Cache open_cache;
00058
00059 return open_cache;
00060 }
00061
00062 size_t size() const
00063 {
00064 return cache.size();
00065 }
00066
00067 void rehash(size_t arg)
00068 {
00069 cache.rehash(arg);
00070 }
00071
00072 Instance::shared_ptr find(const identifier::Catalog &identifier, drizzled::error_t &error);
00073 bool exist(const identifier::Catalog &identifier);
00074 bool erase(const identifier::Catalog &identifier, drizzled::error_t &error);
00075 bool insert(const identifier::Catalog &identifier, Instance::shared_ptr instance, drizzled::error_t &error);
00076 bool lock(const identifier::Catalog &identifier, drizzled::error_t &error);
00077 bool unlock(const identifier::Catalog &identifier, drizzled::error_t &error);
00078
00079 friend class drizzled::generator::catalog::Cache;
00080 friend class drizzled::plugin::Catalog;
00081 friend class drizzled::catalog::lock::Erase;
00082 friend class drizzled::catalog::lock::Create;
00083
00084 void copy(catalog::Instance::vector &vector);
00085
00086 typedef boost::unordered_map< identifier::Catalog, catalog::Instance::shared_ptr> unordered_map;
00087
00088 unordered_map cache;
00089 boost::mutex _mutex;
00090 };
00091
00092
00093 namespace lock {
00094
00095 class Erase
00096 {
00097 bool _locked;
00098 const identifier::Catalog &identifier;
00099 drizzled::error_t error;
00100
00101 public:
00102 Erase(const identifier::Catalog &identifier_arg) :
00103 _locked(false),
00104 identifier(identifier_arg)
00105 {
00106 init();
00107 }
00108
00109 bool locked () const
00110 {
00111 return _locked;
00112 }
00113
00114 ~Erase()
00115 {
00116 if (_locked)
00117 {
00118 if (not catalog::Cache::singleton().unlock(identifier, error))
00119 {
00120 my_error(error, identifier);
00121 assert(0);
00122 }
00123 }
00124 }
00125
00126 private:
00127 void init()
00128 {
00129
00130 if (not catalog::Cache::singleton().lock(identifier, error))
00131 {
00132 assert(0);
00133 return;
00134 }
00135
00136 _locked= true;
00137 }
00138 };
00139
00140
00141 class Create
00142 {
00143 bool _locked;
00144 const identifier::Catalog &identifier;
00145 drizzled::error_t error;
00146
00147 public:
00148 Create(const identifier::Catalog &identifier_arg) :
00149 _locked(false),
00150 identifier(identifier_arg)
00151 {
00152 init();
00153 }
00154
00155 bool locked () const
00156 {
00157 return _locked;
00158 }
00159
00160 ~Create()
00161 {
00162 if (_locked)
00163 {
00164 if (not catalog::Cache::singleton().unlock(identifier, error))
00165 {
00166 my_error(error, identifier);
00167 assert(0);
00168 }
00169 }
00170 }
00171
00172
00173 private:
00174 void init()
00175 {
00176
00177 if (not catalog::Cache::singleton().lock(identifier, error))
00178 {
00179 my_error(error, identifier);
00180 return;
00181 }
00182
00183 _locked= true;
00184 }
00185 };
00186
00187 }
00188 }
00189 }
00190