23 #include <drizzled/catalog/cache.h>
24 #include <drizzled/util/find_ptr.h>
29 Cache::unordered_map Cache::cache;
30 boost::mutex Cache::_mutex;
32 Instance::shared_ptr Cache::find(
const identifier::Catalog &identifier, error_t &error)
34 boost::mutex::scoped_lock scopedLock(_mutex);
35 if (
const unordered_map::mapped_type* ptr= find_ptr(cache, identifier))
37 error= *ptr ? EE_OK : ER_CATALOG_NO_LOCK;
40 error= ER_CATALOG_DOES_NOT_EXIST;
41 return catalog::Instance::shared_ptr();
44 bool Cache::exist(
const identifier::Catalog &identifier)
46 boost::mutex::scoped_lock scopedLock(_mutex);
47 return find_ptr(cache, identifier);
50 bool Cache::erase(
const identifier::Catalog &identifier, error_t &error)
52 boost::mutex::scoped_lock scopedLock(_mutex);
53 if (find_ptr(cache, identifier))
55 if (cache.erase(identifier))
59 error= ER_CATALOG_DOES_NOT_EXIST;
63 bool Cache::unlock(
const identifier::Catalog &identifier, error_t &error)
65 boost::mutex::scoped_lock scopedLock(_mutex);
66 if (
const unordered_map::mapped_type* ptr= find_ptr(cache, identifier))
70 if (cache.erase(identifier))
78 error= ER_CATALOG_DOES_NOT_EXIST;
83 bool Cache::lock(
const identifier::Catalog &identifier, error_t &error)
85 boost::mutex::scoped_lock scopedLock(_mutex);
86 std::pair<unordered_map::iterator, bool> ret= cache.insert(std::make_pair(identifier, catalog::Instance::shared_ptr()));
88 error= ret.first->second ? EE_OK : ER_CATALOG_NO_LOCK;
92 bool Cache::insert(
const identifier::Catalog &identifier, catalog::Instance::shared_ptr instance, error_t &error)
94 boost::mutex::scoped_lock scopedLock(_mutex);
95 std::pair<unordered_map::iterator, bool> ret= cache.insert(std::make_pair(identifier, instance));
97 error= ret.first->second ? EE_OK : ER_CATALOG_NO_LOCK;
101 void Cache::copy(catalog::Instance::vector &vector)
103 boost::mutex::scoped_lock scopedLock(_mutex);
104 vector.reserve(catalog::Cache::size());
105 std::transform(cache.begin(), cache.end(), std::back_inserter(vector), boost::bind(&unordered_map::value_type::second, _1));
106 assert(vector.size() == cache.size());
TODO: Rename this file - func.h is stupid.