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 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);
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);
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 }
00111 }