~drizzle-trunk/drizzle/development

1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
21
#include <config.h>
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
22
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
23
#include <drizzled/plugin/catalog.h>
24
#include <drizzled/catalog/cache.h>
25
#include <drizzled/catalog/local.h>
26
#include <drizzled/error.h>
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
27
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
28
#include <boost/foreach.hpp>
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
29
30
namespace drizzled
31
{
32
namespace plugin
33
{
34
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
35
// Private container we use for holding the instances of engines passed to
36
// use from the catalog plugins.
37
class Engines {
38
  catalog::Engine::vector _catalogs;
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
39
40
public:
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
41
  static Engines& singleton()
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
42
  {
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
43
    static Engines ptr;
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
44
    return ptr;
45
  }
46
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
47
  catalog::Engine::vector &catalogs()
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
48
  {
49
    return _catalogs;
50
  }
51
};
52
53
Catalog::~Catalog()
54
{
55
}
56
2246.4.8 by Olaf van der Spek
Remove const_reference and reference from identifier::Catalog
57
bool Catalog::create(const identifier::Catalog& identifier)
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
58
{
1960.1.12 by Brian Aker
Add in the schema and table make_shared methods.
59
  message::catalog::shared_ptr message= message::catalog::make_shared(identifier);
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
60
  return create(identifier, message);
61
}
62
2246.4.8 by Olaf van der Spek
Remove const_reference and reference from identifier::Catalog
63
bool Catalog::create(const identifier::Catalog& identifier, message::catalog::shared_ptr &message)
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
64
{
65
  assert(message);
66
2039.6.3 by Brian Aker
Update for session to have a catalog object.
67
  catalog::lock::Create lock(identifier);
2017.3.1 by Brian Aker
Merge catalog with current trunk.
68
69
  if (not lock.locked())
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
70
  {
2017.3.1 by Brian Aker
Merge catalog with current trunk.
71
    my_error(ER_CATALOG_NO_LOCK, MYF(0), identifier.getName().c_str());
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
72
    return false;
73
  }
74
2039.6.2 by Brian Aker
Update code to test/handle conflicting create/drop schema.
75
  size_t create_count= 0;
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
76
  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
77
  {
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
78
    if (ref->create(identifier, message))
2039.6.2 by Brian Aker
Update code to test/handle conflicting create/drop schema.
79
      create_count++;
80
  }
81
  assert(create_count < 2);
82
83
  if (not create_count)
84
  {
85
    my_error(ER_CATALOG_CANNOT_CREATE, MYF(0), identifier.getName().c_str());
86
    return false;
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
87
  }
88
2017.3.1 by Brian Aker
Merge catalog with current trunk.
89
  return true;
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
90
}
91
2246.4.8 by Olaf van der Spek
Remove const_reference and reference from identifier::Catalog
92
bool Catalog::drop(const identifier::Catalog& identifier)
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
93
{
2039.6.6 by Brian Aker
Update so that CATALOG is correctly being displayed.
94
  if (identifier == drizzled::catalog::local_identifier())
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
95
  {
96
    my_error(drizzled::ER_CATALOG_NO_DROP_LOCAL, MYF(0));
97
    return false;
98
  }
99
2039.6.3 by Brian Aker
Update for session to have a catalog object.
100
  catalog::lock::Erase lock(identifier);
2017.3.1 by Brian Aker
Merge catalog with current trunk.
101
  if (not lock.locked())
102
  {
103
    my_error(ER_CATALOG_NO_LOCK, MYF(0), identifier.getName().c_str());
104
    return false; 
105
  }
106
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
107
  
2039.6.2 by Brian Aker
Update code to test/handle conflicting create/drop schema.
108
  size_t drop_count= 0;
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
109
  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
110
  {
2039.6.2 by Brian Aker
Update code to test/handle conflicting create/drop schema.
111
    if (ref->drop(identifier))
112
      drop_count++;
113
  }
114
  assert(drop_count < 2);
115
116
  if (not drop_count)
117
  {
118
    my_error(ER_CATALOG_DOES_NOT_EXIST, MYF(0), identifier.getName().c_str());
119
    return false;
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
120
  }
121
2017.3.1 by Brian Aker
Merge catalog with current trunk.
122
  return true;
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
123
}
124
2246.4.8 by Olaf van der Spek
Remove const_reference and reference from identifier::Catalog
125
bool Catalog::lock(const identifier::Catalog& identifier)
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
126
{
2073.2.1 by Brian Aker
Remove custom error.
127
  drizzled::error_t error;
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
128
  
129
  // We insert a lock into the cache, if this fails we bail.
130
  if (not catalog::Cache::singleton().lock(identifier, error))
131
  {
2073.2.1 by Brian Aker
Remove custom error.
132
    my_error(error, identifier);
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
133
134
    return false;
135
  }
136
137
  return true;
138
}
139
140
2246.4.8 by Olaf van der Spek
Remove const_reference and reference from identifier::Catalog
141
bool Catalog::unlock(const identifier::Catalog& identifier)
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
142
{
2073.2.1 by Brian Aker
Remove custom error.
143
  drizzled::error_t error;
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
144
  if (not catalog::Cache::singleton().unlock(identifier, error))
145
  {
2073.2.1 by Brian Aker
Remove custom error.
146
    my_error(error, identifier);
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
147
  }
148
149
  return false;
150
}
151
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
152
bool plugin::Catalog::addPlugin(plugin::Catalog *arg)
153
{
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
154
  Engines::singleton().catalogs().push_back(arg->engine());
155
156
  return false;
157
}
158
2246.4.8 by Olaf van der Spek
Remove const_reference and reference from identifier::Catalog
159
bool plugin::Catalog::exist(const identifier::Catalog& identifier)
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
160
{
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
161
  if (catalog::Cache::singleton().exist(identifier))
162
    return true;
163
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
164
  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
165
  {
166
    if (ref->exist(identifier))
167
      return true;
168
  }
169
170
  return false;
171
}
172
2252.1.9 by Olaf van der Spek
Common fwd
173
void plugin::Catalog::getIdentifiers(identifier::catalog::vector &identifiers)
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
174
{
175
  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
176
  {
177
    ref->getIdentifiers(identifiers);
178
  }
179
}
180
181
void plugin::Catalog::getMessages(message::catalog::vector &messages)
182
{
183
  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
184
  {
185
    ref->getMessages(messages);
186
  }
187
}
188
2246.4.8 by Olaf van der Spek
Remove const_reference and reference from identifier::Catalog
189
message::catalog::shared_ptr plugin::Catalog::getMessage(const identifier::Catalog& identifier)
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
190
{
2073.2.1 by Brian Aker
Remove custom error.
191
  drizzled::error_t error;
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
192
  catalog::Instance::shared_ptr instance= catalog::Cache::singleton().find(identifier, error);
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
193
  message::catalog::shared_ptr message;
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
194
195
  if (instance and instance->message())
196
  {
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
197
    return instance->message();
198
  }
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
199
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
200
  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
201
  {
202
    if ((message= ref->getMessage(identifier)))
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
203
      return message;
204
  }
205
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
206
  return message;
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
207
}
208
2246.4.8 by Olaf van der Spek
Remove const_reference and reference from identifier::Catalog
209
catalog::Instance::shared_ptr plugin::Catalog::getInstance(const identifier::Catalog& identifier)
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
210
{
2073.2.1 by Brian Aker
Remove custom error.
211
  drizzled::error_t error;
2039.6.3 by Brian Aker
Update for session to have a catalog object.
212
  catalog::Instance::shared_ptr instance= catalog::Cache::singleton().find(identifier, error);
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
213
214
  if (instance)
2039.6.3 by Brian Aker
Update for session to have a catalog object.
215
    return instance;
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
216
217
  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
218
  {
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
219
    message::catalog::shared_ptr message;
220
    if (message= ref->getMessage(identifier))
2039.6.3 by Brian Aker
Update for session to have a catalog object.
221
    {
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
222
      instance= catalog::Instance::make_shared(message);
2039.6.3 by Brian Aker
Update for session to have a catalog object.
223
      // If this should fail inserting into the cache, we are in a world of
224
      // pain.
225
      catalog::Cache::singleton().insert(identifier, instance, error);
226
227
      return instance;
228
    }
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
229
  }
230
2039.6.3 by Brian Aker
Update for session to have a catalog object.
231
  return catalog::Instance::shared_ptr();
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
232
}
233
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
234
2068.5.6 by Brian Aker
Remove dead reference call for deleting plugin (we don't need cleanup).
235
void plugin::Catalog::removePlugin(plugin::Catalog *)
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
236
{
237
}
238
239
} /* namespace plugin */
240
} /* namespace drizzled */