~brianaker/drizzle/regression-622465

« back to all changes in this revision

Viewing changes to drizzled/catalog/cache.cc

  • Committer: Brian Aker
  • Date: 2011-01-11 07:12:09 UTC
  • mfrom: (2068.5.6 catalogs)
  • Revision ID: brian@gir-3-20110111071209-ntbex8btgayoq00v
MergeĀ inĀ catalogs.

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
#include "config.h"
 
22
#include "drizzled/catalog.h"
 
23
#include "drizzled/catalog/cache.h"
 
24
 
 
25
namespace drizzled {
 
26
namespace catalog {
 
27
 
 
28
Instance::shared_ptr Cache::find(const identifier::Catalog &identifier, catalog::error_t &error)
 
29
{
 
30
  boost::mutex::scoped_lock scopedLock(_mutex);
 
31
  unordered_map::iterator iter= cache.find(identifier);
 
32
  if (iter != cache.end())
 
33
  {
 
34
    if (not (*iter).second)
 
35
      error= LOCKED;
 
36
 
 
37
    return (*iter).second;
 
38
  }
 
39
 
 
40
  error= NOT_FOUND;
 
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
  unordered_map::iterator iter= cache.find(identifier);
 
48
  if (iter != cache.end())
 
49
  {
 
50
    return true;
 
51
  }
 
52
 
 
53
  return false;
 
54
}
 
55
 
 
56
bool Cache::erase(const identifier::Catalog &identifier, catalog::error_t &error)
 
57
{
 
58
  boost::mutex::scoped_lock scopedLock(_mutex);
 
59
 
 
60
  unordered_map::iterator iter= cache.find(identifier);
 
61
  if (iter != cache.end())
 
62
  {
 
63
    unordered_map::size_type erased= cache.erase(identifier);
 
64
 
 
65
    if (erased)
 
66
      return true;
 
67
 
 
68
    assert(0); // This should be imposssible
 
69
  }
 
70
  error= NOT_FOUND;
 
71
 
 
72
  return false;
 
73
}
 
74
 
 
75
bool Cache::unlock(const identifier::Catalog &identifier, catalog::error_t &error)
 
76
{
 
77
  boost::mutex::scoped_lock scopedLock(_mutex);
 
78
 
 
79
  unordered_map::iterator iter= cache.find(identifier);
 
80
  if (iter != cache.end())
 
81
  {
 
82
    if (not (*iter).second)
 
83
    {
 
84
      unordered_map::size_type erased= cache.erase(identifier);
 
85
 
 
86
      if (erased)
 
87
        return true;
 
88
 
 
89
      assert(0); // This should be imposssible
 
90
    }
 
91
    error= FOUND;
 
92
  }
 
93
  else
 
94
  {
 
95
    error= NOT_FOUND;
 
96
  }
 
97
 
 
98
  return false;
 
99
}
 
100
 
 
101
bool Cache::lock(const identifier::Catalog &identifier, catalog::error_t &error)
 
102
{
 
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()));
 
105
 
 
106
  if (ret.second == false)
 
107
  {
 
108
    if (ret.first->second)
 
109
    {
 
110
      error= FOUND;
 
111
    }
 
112
    else
 
113
    {
 
114
      error= LOCKED;
 
115
    }
 
116
  }
 
117
 
 
118
  return ret.second;
 
119
}
 
120
 
 
121
bool Cache::insert(const identifier::Catalog &identifier, catalog::Instance::shared_ptr instance, catalog::error_t &error)
 
122
{
 
123
  boost::mutex::scoped_lock scopedLock(_mutex);
 
124
  std::pair<unordered_map::iterator, bool> ret= cache.insert(std::make_pair(identifier, instance));
 
125
 
 
126
  if (ret.second == false)
 
127
  {
 
128
    if (ret.first->second)
 
129
    {
 
130
      error= FOUND;
 
131
    }
 
132
    else
 
133
    {
 
134
      error= LOCKED;
 
135
    }
 
136
  }
 
137
 
 
138
  return ret.second;
 
139
}
 
140
 
 
141
void Cache::copy(catalog::Instance::vector &vector)
 
142
{
 
143
  boost::mutex::scoped_lock scopedLock(_mutex);
 
144
 
 
145
  vector.reserve(catalog::Cache::singleton().size());
 
146
 
 
147
  std::transform(cache.begin(),
 
148
                 cache.end(),
 
149
                 std::back_inserter(vector),
 
150
                 boost::bind(&unordered_map::value_type::second, _1) );
 
151
  assert(vector.size() == cache.size());
 
152
}
 
153
 
 
154
 
 
155
} /* namespace catalog */
 
156
} /* namespace drizzled */