1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2010 Brian Aker
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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))
57
assert(false); // This should be imposssible
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))
72
assert(false); // This should be imposssible
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());
110
} /* namespace catalog */
111
} /* namespace drizzled */