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
22
#include "drizzled/catalog.h"
23
#include "drizzled/catalog/cache.h"
28
Instance::shared_ptr Cache::find(const identifier::Catalog &identifier, catalog::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)
37
return (*iter).second;
41
return catalog::Instance::shared_ptr();
44
bool Cache::exist(const identifier::Catalog &identifier)
46
boost::mutex::scoped_lock scopedLock(_mutex);
47
unordered_map::iterator iter= cache.find(identifier);
48
if (iter != cache.end())
56
bool Cache::erase(const identifier::Catalog &identifier, catalog::error_t &error)
58
boost::mutex::scoped_lock scopedLock(_mutex);
60
unordered_map::iterator iter= cache.find(identifier);
61
if (iter != cache.end())
63
unordered_map::size_type erased= cache.erase(identifier);
68
assert(0); // This should be imposssible
75
bool Cache::unlock(const identifier::Catalog &identifier, catalog::error_t &error)
77
boost::mutex::scoped_lock scopedLock(_mutex);
79
unordered_map::iterator iter= cache.find(identifier);
80
if (iter != cache.end())
82
if (not (*iter).second)
84
unordered_map::size_type erased= cache.erase(identifier);
89
assert(0); // This should be imposssible
101
bool Cache::lock(const identifier::Catalog &identifier, catalog::error_t &error)
103
boost::mutex::scoped_lock scopedLock(_mutex);
104
std::pair<unordered_map::iterator, bool> ret= cache.insert(std::make_pair(identifier, catalog::Instance::shared_ptr()));
106
if (ret.second == false)
108
if (ret.first->second)
121
bool Cache::insert(const identifier::Catalog &identifier, catalog::Instance::shared_ptr instance, catalog::error_t &error)
123
boost::mutex::scoped_lock scopedLock(_mutex);
124
std::pair<unordered_map::iterator, bool> ret= cache.insert(std::make_pair(identifier, instance));
126
if (ret.second == false)
128
if (ret.first->second)
141
void Cache::copy(catalog::Instance::vector &vector)
143
boost::mutex::scoped_lock scopedLock(_mutex);
145
vector.reserve(catalog::Cache::singleton().size());
147
std::transform(cache.begin(),
149
std::back_inserter(vector),
150
boost::bind(&unordered_map::value_type::second, _1) );
151
assert(vector.size() == cache.size());
155
} /* namespace catalog */
156
} /* namespace drizzled */