~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/catalog/cache.cc

Reverted 1103

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