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>
28
Instance::shared_ptr Cache::find(const identifier::Catalog &identifier, drizzled::error_t &error)
30
boost::mutex::scoped_lock scopedLock(_mutex);
31
unordered_map::iterator iter= cache.find(identifier);
32
if (iter != cache.end())
34
if (not (*iter).second)
36
error= ER_CATALOG_NO_LOCK;
43
return (*iter).second;
46
error= ER_CATALOG_DOES_NOT_EXIST;
47
return catalog::Instance::shared_ptr();
50
bool Cache::exist(const identifier::Catalog &identifier)
52
boost::mutex::scoped_lock scopedLock(_mutex);
53
unordered_map::iterator iter= cache.find(identifier);
54
if (iter != cache.end())
62
bool Cache::erase(const identifier::Catalog &identifier, drizzled::error_t &error)
64
boost::mutex::scoped_lock scopedLock(_mutex);
66
unordered_map::iterator iter= cache.find(identifier);
67
if (iter != cache.end())
69
unordered_map::size_type erased= cache.erase(identifier);
74
assert(0); // This should be imposssible
76
error= ER_CATALOG_DOES_NOT_EXIST;
81
bool Cache::unlock(const identifier::Catalog &identifier, drizzled::error_t &error)
83
boost::mutex::scoped_lock scopedLock(_mutex);
85
unordered_map::iterator iter= cache.find(identifier);
86
if (iter != cache.end())
88
if (not (*iter).second)
90
unordered_map::size_type erased= cache.erase(identifier);
95
assert(0); // This should be imposssible
101
error= ER_CATALOG_DOES_NOT_EXIST;
107
bool Cache::lock(const identifier::Catalog &identifier, drizzled::error_t &error)
109
boost::mutex::scoped_lock scopedLock(_mutex);
110
std::pair<unordered_map::iterator, bool> ret= cache.insert(std::make_pair(identifier, catalog::Instance::shared_ptr()));
112
if (ret.second == false)
114
if (ret.first->second)
120
error= ER_CATALOG_NO_LOCK;
127
bool Cache::insert(const identifier::Catalog &identifier, catalog::Instance::shared_ptr instance, drizzled::error_t &error)
129
boost::mutex::scoped_lock scopedLock(_mutex);
130
std::pair<unordered_map::iterator, bool> ret= cache.insert(std::make_pair(identifier, instance));
132
if (ret.second == false)
134
if (ret.first->second)
140
error= ER_CATALOG_NO_LOCK;
147
void Cache::copy(catalog::Instance::vector &vector)
149
boost::mutex::scoped_lock scopedLock(_mutex);
151
vector.reserve(catalog::Cache::singleton().size());
153
std::transform(cache.begin(),
155
std::back_inserter(vector),
156
boost::bind(&unordered_map::value_type::second, _1) );
157
assert(vector.size() == cache.size());
161
} /* namespace catalog */
162
} /* namespace drizzled */