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