~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/catalog/cache.h

  • Committer: Brian Aker
  • Date: 2011-01-05 17:49:11 UTC
  • mfrom: (2046.2.3 timestamp)
  • mto: This revision was merged to the branch mainline in revision 2064.
  • Revision ID: brian@tangent.org-20110105174911-144g51mjy00bqldp
Update microtime code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
#ifndef DRIZZLED_CATALOG_CACHE_H
22
 
#define DRIZZLED_CATALOG_CACHE_H
23
 
 
24
 
#include <boost/bind.hpp>
25
 
 
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
 
 
35
 
namespace plugin {
36
 
class Catalog;
37
 
}
38
 
namespace generator {
39
 
namespace catalog {
40
 
class Cache;
41
 
class Instance;
42
 
 
43
 
} //namespace catalog
44
 
} //namespace generator
45
 
 
46
 
namespace catalog {
47
 
 
48
 
namespace lock {
49
 
class Create;
50
 
class Erase;
51
 
 
52
 
} //namespace lock
53
 
 
54
 
class Cache
55
 
{
56
 
  static inline Cache &singleton()
57
 
  {
58
 
    static Cache open_cache;
59
 
 
60
 
    return open_cache;
61
 
  }
62
 
 
63
 
  size_t size() const
64
 
  {
65
 
    return cache.size();
66
 
  }
67
 
 
68
 
  void rehash(size_t arg)
69
 
  {
70
 
    cache.rehash(arg);
71
 
  }
72
 
 
73
 
  Instance::shared_ptr find(const identifier::Catalog &identifier, drizzled::error_t &error);
74
 
  bool exist(const identifier::Catalog &identifier);
75
 
  bool erase(const identifier::Catalog &identifier, drizzled::error_t &error);
76
 
  bool insert(const identifier::Catalog &identifier, Instance::shared_ptr instance, drizzled::error_t &error);
77
 
  bool lock(const identifier::Catalog &identifier, drizzled::error_t &error);
78
 
  bool unlock(const identifier::Catalog &identifier, drizzled::error_t &error);
79
 
 
80
 
  friend class drizzled::generator::catalog::Cache;
81
 
  friend class drizzled::plugin::Catalog;
82
 
  friend class drizzled::catalog::lock::Erase;
83
 
  friend class drizzled::catalog::lock::Create;
84
 
 
85
 
  void copy(catalog::Instance::vector &vector);
86
 
 
87
 
  typedef boost::unordered_map< identifier::Catalog, catalog::Instance::shared_ptr> unordered_map;
88
 
 
89
 
  unordered_map cache;
90
 
  boost::mutex _mutex;
91
 
};
92
 
 
93
 
 
94
 
namespace lock {
95
 
 
96
 
class Erase
97
 
{
98
 
  bool _locked;
99
 
  const identifier::Catalog &identifier;
100
 
  drizzled::error_t error;
101
 
 
102
 
public:
103
 
  Erase(const identifier::Catalog &identifier_arg) :
104
 
    _locked(false),
105
 
    identifier(identifier_arg)
106
 
  {
107
 
    init();
108
 
  }
109
 
 
110
 
  bool locked () const
111
 
  {
112
 
    return _locked;
113
 
  }
114
 
 
115
 
  ~Erase()
116
 
  {
117
 
    if (_locked)
118
 
    {
119
 
      if (not catalog::Cache::singleton().unlock(identifier, error))
120
 
      {
121
 
        my_error(error, identifier);
122
 
        assert(0);
123
 
      }
124
 
    }
125
 
  }
126
 
 
127
 
private:
128
 
  void init()
129
 
  {
130
 
    // We insert a lock into the cache, if this fails we bail.
131
 
    if (not catalog::Cache::singleton().lock(identifier, error))
132
 
    {
133
 
      assert(0);
134
 
      return;
135
 
    }
136
 
 
137
 
    _locked= true;
138
 
  }
139
 
};
140
 
 
141
 
 
142
 
class Create
143
 
{
144
 
  bool _locked;
145
 
  const identifier::Catalog &identifier;
146
 
  drizzled::error_t error;
147
 
 
148
 
public:
149
 
  Create(const identifier::Catalog &identifier_arg) :
150
 
    _locked(false),
151
 
    identifier(identifier_arg)
152
 
  {
153
 
    init();
154
 
  }
155
 
 
156
 
  bool locked () const
157
 
  {
158
 
    return _locked;
159
 
  }
160
 
 
161
 
  ~Create()
162
 
  {
163
 
    if (_locked)
164
 
    {
165
 
      if (not catalog::Cache::singleton().unlock(identifier, error))
166
 
      {
167
 
        my_error(error, identifier);
168
 
        assert(0);
169
 
      }
170
 
    }
171
 
  }
172
 
 
173
 
 
174
 
private:
175
 
  void init()
176
 
  {
177
 
    // We insert a lock into the cache, if this fails we bail.
178
 
    if (not catalog::Cache::singleton().lock(identifier, error))
179
 
    {
180
 
      my_error(error, identifier);
181
 
      return;
182
 
    }
183
 
 
184
 
    _locked= true;
185
 
  }
186
 
};
187
 
 
188
 
} /* namespace lock */
189
 
} /* namespace catalog */
190
 
} /* namespace drizzled */
191
 
 
192
 
#endif /* DRIZZLED_CATALOG_CACHE_H */