Drizzled Public API Documentation

cache.cc
1 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3  *
4  * Copyright (C) 2010 Brian Aker
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 #include <config.h>
22 
23 #include <drizzled/catalog/cache.h>
24 #include <drizzled/util/find_ptr.h>
25 
26 namespace drizzled {
27 namespace catalog {
28 
29 Cache::unordered_map Cache::cache;
30 boost::mutex Cache::_mutex;
31 
32 Instance::shared_ptr Cache::find(const identifier::Catalog &identifier, error_t &error)
33 {
34  boost::mutex::scoped_lock scopedLock(_mutex);
35  if (const unordered_map::mapped_type* ptr= find_ptr(cache, identifier))
36  {
37  error= *ptr ? EE_OK : ER_CATALOG_NO_LOCK;
38  return *ptr;
39  }
40  error= ER_CATALOG_DOES_NOT_EXIST;
41  return catalog::Instance::shared_ptr();
42 }
43 
44 bool Cache::exist(const identifier::Catalog &identifier)
45 {
46  boost::mutex::scoped_lock scopedLock(_mutex);
47  return find_ptr(cache, identifier);
48 }
49 
50 bool Cache::erase(const identifier::Catalog &identifier, error_t &error)
51 {
52  boost::mutex::scoped_lock scopedLock(_mutex);
53  if (find_ptr(cache, identifier))
54  {
55  if (cache.erase(identifier))
56  return true;
57  assert(false); // This should be imposssible
58  }
59  error= ER_CATALOG_DOES_NOT_EXIST;
60  return false;
61 }
62 
63 bool Cache::unlock(const identifier::Catalog &identifier, error_t &error)
64 {
65  boost::mutex::scoped_lock scopedLock(_mutex);
66  if (const unordered_map::mapped_type* ptr= find_ptr(cache, identifier))
67  {
68  if (not *ptr)
69  {
70  if (cache.erase(identifier))
71  return true;
72  assert(false); // This should be imposssible
73  }
74  error= EE_OK;
75  }
76  else
77  {
78  error= ER_CATALOG_DOES_NOT_EXIST;
79  }
80  return false;
81 }
82 
83 bool Cache::lock(const identifier::Catalog &identifier, error_t &error)
84 {
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()));
87  if (not ret.second)
88  error= ret.first->second ? EE_OK : ER_CATALOG_NO_LOCK;
89  return ret.second;
90 }
91 
92 bool Cache::insert(const identifier::Catalog &identifier, catalog::Instance::shared_ptr instance, error_t &error)
93 {
94  boost::mutex::scoped_lock scopedLock(_mutex);
95  std::pair<unordered_map::iterator, bool> ret= cache.insert(std::make_pair(identifier, instance));
96  if (not ret.second)
97  error= ret.first->second ? EE_OK : ER_CATALOG_NO_LOCK;
98  return ret.second;
99 }
100 
101 void Cache::copy(catalog::Instance::vector &vector)
102 {
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());
107 }
108 
109 
110 } /* namespace catalog */
111 } /* namespace drizzled */
TODO: Rename this file - func.h is stupid.