~ubuntu-branches/ubuntu/saucy/drizzle/saucy-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2010 Brian Aker
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <config.h>

#include <drizzled/plugin/catalog.h>
#include <drizzled/catalog/cache.h>
#include <drizzled/catalog/local.h>
#include <drizzled/error.h>

#include <boost/foreach.hpp>

namespace drizzled
{
namespace plugin
{

// Private container we use for holding the instances of engines passed to
// use from the catalog plugins.
class Engines {
  catalog::Engine::vector _catalogs;

public:
  static Engines& singleton()
  {
    static Engines ptr;
    return ptr;
  }

  catalog::Engine::vector &catalogs()
  {
    return _catalogs;
  }
};

bool Catalog::create(const identifier::Catalog& identifier)
{
  message::catalog::shared_ptr message= message::catalog::make_shared(identifier);
  return create(identifier, message);
}

bool Catalog::create(const identifier::Catalog& identifier, message::catalog::shared_ptr &message)
{
  assert(message);

  catalog::lock::Create lock(identifier);

  if (not lock.locked())
  {
    my_error(ER_CATALOG_NO_LOCK, MYF(0), identifier.getName().c_str());
    return false;
  }

  size_t create_count= 0;
  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
  {
    if (ref->create(identifier, message))
      create_count++;
  }
  assert(create_count < 2);

  if (not create_count)
  {
    my_error(ER_CATALOG_CANNOT_CREATE, MYF(0), identifier.getName().c_str());
    return false;
  }

  return true;
}

bool Catalog::drop(const identifier::Catalog& identifier)
{
  if (identifier == drizzled::catalog::local_identifier())
  {
    my_error(drizzled::ER_CATALOG_NO_DROP_LOCAL, MYF(0));
    return false;
  }

  catalog::lock::Erase lock(identifier);
  if (not lock.locked())
  {
    my_error(ER_CATALOG_NO_LOCK, MYF(0), identifier.getName().c_str());
    return false; 
  }

  
  size_t drop_count= 0;
  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
  {
    if (ref->drop(identifier))
    {
      drop_count++;
    }
  }
  assert(drop_count < 2);

  if (not drop_count)
  {
    my_error(ER_CATALOG_DOES_NOT_EXIST, MYF(0), identifier.getName().c_str());
    return false;
  }

  return true;
}

bool Catalog::lock(const identifier::Catalog& identifier)
{
  drizzled::error_t error;
  
  // We insert a lock into the cache, if this fails we bail.
  if (not catalog::Cache::lock(identifier, error))
  {
    my_error(error, identifier);

    return false;
  }

  return true;
}


bool Catalog::unlock(const identifier::Catalog& identifier)
{
  drizzled::error_t error;
  if (not catalog::Cache::unlock(identifier, error))
  {
    my_error(error, identifier);
  }

  return false;
}

bool plugin::Catalog::addPlugin(plugin::Catalog *arg)
{
  Engines::singleton().catalogs().push_back(arg->engine());

  return false;
}

bool plugin::Catalog::exist(const identifier::Catalog& identifier)
{
  if (catalog::Cache::exist(identifier))
  {
    return true;
  }

  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
  {
    if (ref->exist(identifier))
    {
      return true;
    }
  }

  return false;
}

void plugin::Catalog::getIdentifiers(identifier::catalog::vector &identifiers)
{
  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
  {
    ref->getIdentifiers(identifiers);
  }
}

void plugin::Catalog::getMessages(message::catalog::vector &messages)
{
  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
  {
    ref->getMessages(messages);
  }
}

message::catalog::shared_ptr plugin::Catalog::getMessage(const identifier::Catalog& identifier)
{
  drizzled::error_t error;
  catalog::Instance::shared_ptr instance= catalog::Cache::find(identifier, error);
  message::catalog::shared_ptr message;

  if (instance and instance->message())
  {
    return instance->message();
  }

  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
  {
    if ((message= ref->getMessage(identifier)))
    {
      return message;
    }
  }

  return message;
}

catalog::Instance::shared_ptr plugin::Catalog::getInstance(const identifier::Catalog& identifier)
{
  drizzled::error_t error;
  catalog::Instance::shared_ptr instance= catalog::Cache::find(identifier, error);

  if (instance)
  {
    return instance;
  }

  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
  {
    message::catalog::shared_ptr message;
    if (message= ref->getMessage(identifier))
    {
      instance= catalog::Instance::make_shared(message);
      // If this should fail inserting into the cache, we are in a world of
      // pain.
      catalog::Cache::insert(identifier, instance, error);

      return instance;
    }
  }

  return catalog::Instance::shared_ptr();
}


void plugin::Catalog::removePlugin(plugin::Catalog *)
{
}

} /* namespace plugin */
} /* namespace drizzled */