Drizzled Public API Documentation

cache.h
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 #pragma once
22 
23 #include <boost/bind.hpp>
24 
25 #include <drizzled/identifier.h>
26 #include <drizzled/error.h>
27 #include <drizzled/catalog.h>
28 #include <drizzled/plugin/catalog.h>
29 
30 #include <boost/unordered_map.hpp>
31 #include <boost/thread/mutex.hpp>
32 
33 namespace drizzled {
34 namespace catalog {
35 
36 class Cache
37 {
38 public:
39  static size_t size()
40  {
41  return cache.size();
42  }
43 
44  static Instance::shared_ptr find(const identifier::Catalog&, error_t&);
45  static bool exist(const identifier::Catalog&);
46  static bool erase(const identifier::Catalog&, error_t&);
47  static bool insert(const identifier::Catalog&, Instance::shared_ptr, error_t&);
48  static bool lock(const identifier::Catalog&, error_t&);
49  static bool unlock(const identifier::Catalog&, error_t&);
50  static void copy(catalog::Instance::vector&);
51 
52  typedef boost::unordered_map<identifier::Catalog, catalog::Instance::shared_ptr> unordered_map;
53 
54  static unordered_map cache;
55  static boost::mutex _mutex;
56 };
57 
58 
59 namespace lock {
60 
61 class Erase
62 {
63  bool _locked;
64  const identifier::Catalog &identifier;
65  error_t error;
66 
67 public:
68  Erase(const identifier::Catalog &identifier_arg) :
69  _locked(false),
70  identifier(identifier_arg)
71  {
72  init();
73  }
74 
75  bool locked () const
76  {
77  return _locked;
78  }
79 
80  ~Erase()
81  {
82  if (_locked)
83  {
84  if (not catalog::Cache::unlock(identifier, error))
85  {
86  my_error(error, identifier);
87  assert(0);
88  }
89  }
90  }
91 
92 private:
93  void init()
94  {
95  // We insert a lock into the cache, if this fails we bail.
96  if (not catalog::Cache::lock(identifier, error))
97  {
98  assert(0);
99  return;
100  }
101 
102  _locked= true;
103  }
104 };
105 
106 
107 class Create
108 {
109  bool _locked;
110  const identifier::Catalog &identifier;
111  error_t error;
112 
113 public:
114  Create(const identifier::Catalog &identifier_arg) :
115  _locked(false),
116  identifier(identifier_arg)
117  {
118  init();
119  }
120 
121  bool locked () const
122  {
123  return _locked;
124  }
125 
126  ~Create()
127  {
128  if (_locked)
129  {
130  if (not catalog::Cache::unlock(identifier, error))
131  {
132  my_error(error, identifier);
133  assert(0);
134  }
135  }
136  }
137 
138 
139 private:
140  void init()
141  {
142  // We insert a lock into the cache, if this fails we bail.
143  if (not catalog::Cache::lock(identifier, error))
144  {
145  my_error(error, identifier);
146  return;
147  }
148 
149  _locked= true;
150  }
151 };
152 
153 } /* namespace lock */
154 } /* namespace catalog */
155 } /* namespace drizzled */
156